iio.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-3-08 GuEe-GUI the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. static void *ofw_iio_channel_get_by_index(struct rt_ofw_node *np, int index, int *out_channel)
  13. {
  14. void *iio = RT_NULL;
  15. #ifdef RT_USING_OFW
  16. struct rt_ofw_node *iio_np;
  17. struct rt_ofw_cell_args iio_args;
  18. if (!rt_ofw_parse_phandle_cells(np, "io-channels", "#io-channel-cells", index, &iio_args))
  19. {
  20. iio_np = iio_args.data;
  21. if (!rt_ofw_data(iio_np))
  22. {
  23. rt_platform_ofw_request(iio_np);
  24. }
  25. iio = rt_ofw_data(iio_np);
  26. rt_ofw_node_put(iio_np);
  27. if (out_channel)
  28. {
  29. *out_channel = iio_args.args[0];
  30. }
  31. }
  32. #endif /* RT_USING_OFW */
  33. return iio;
  34. }
  35. void *rt_iio_channel_get_by_index(struct rt_device *dev, int index, int *out_channel)
  36. {
  37. void *iio = RT_NULL;
  38. if (!dev || index < 0)
  39. {
  40. return RT_NULL;
  41. }
  42. if (dev->ofw_node)
  43. {
  44. iio = ofw_iio_channel_get_by_index(dev->ofw_node, index, out_channel);
  45. }
  46. return iio;
  47. }
  48. void *rt_iio_channel_get_by_name(struct rt_device *dev, const char *name, int *out_channel)
  49. {
  50. int index;
  51. if (!dev || !name)
  52. {
  53. return RT_NULL;
  54. }
  55. index = rt_dm_dev_prop_index_of_string(dev, "io-channel-names", name);
  56. return rt_iio_channel_get_by_index(dev, index, out_channel);
  57. }