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 
11 #include <rtthread.h>
12 #include <rtdevice.h>
13 
ofw_iio_channel_get_by_index(struct rt_ofw_node * np,int index,int * out_channel)14 static void *ofw_iio_channel_get_by_index(struct rt_ofw_node *np, int index, int *out_channel)
15 {
16     void *iio = RT_NULL;
17 #ifdef RT_USING_OFW
18     struct rt_ofw_node *iio_np;
19     struct rt_ofw_cell_args iio_args;
20 
21     if (!rt_ofw_parse_phandle_cells(np, "io-channels", "#io-channel-cells", index, &iio_args))
22     {
23         iio_np = iio_args.data;
24 
25         if (!rt_ofw_data(iio_np))
26         {
27             rt_platform_ofw_request(iio_np);
28         }
29 
30         iio = rt_ofw_data(iio_np);
31         rt_ofw_node_put(iio_np);
32 
33         if (out_channel)
34         {
35             *out_channel = iio_args.args[0];
36         }
37     }
38 #endif /* RT_USING_OFW */
39     return iio;
40 }
41 
rt_iio_channel_get_by_index(struct rt_device * dev,int index,int * out_channel)42 void *rt_iio_channel_get_by_index(struct rt_device *dev, int index, int *out_channel)
43 {
44     void *iio = RT_NULL;
45 
46     if (!dev || index < 0)
47     {
48         return RT_NULL;
49     }
50 
51     if (dev->ofw_node)
52     {
53         iio = ofw_iio_channel_get_by_index(dev->ofw_node, index, out_channel);
54     }
55 
56     return iio;
57 }
58 
rt_iio_channel_get_by_name(struct rt_device * dev,const char * name,int * out_channel)59 void *rt_iio_channel_get_by_name(struct rt_device *dev, const char *name, int *out_channel)
60 {
61     int index;
62 
63     if (!dev || !name)
64     {
65         return RT_NULL;
66     }
67 
68     index = rt_dm_dev_prop_index_of_string(dev, "io-channel-names", name);
69 
70     return rt_iio_channel_get_by_index(dev, index, out_channel);
71 }
72