summaryrefslogtreecommitdiff
path: root/components/drivers/iio/iio.c
blob: 548308dbe68f1a35e16470b322d6ee551dc38a17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-3-08      GuEe-GUI     the first version
 */

#include <rtthread.h>
#include <rtdevice.h>

static void *ofw_iio_channel_get_by_index(struct rt_ofw_node *np, int index, int *out_channel)
{
    void *iio = RT_NULL;
#ifdef RT_USING_OFW
    struct rt_ofw_node *iio_np;
    struct rt_ofw_cell_args iio_args;

    if (!rt_ofw_parse_phandle_cells(np, "io-channels", "#io-channel-cells", index, &iio_args))
    {
        iio_np = iio_args.data;

        if (!rt_ofw_data(iio_np))
        {
            rt_platform_ofw_request(iio_np);
        }

        iio = rt_ofw_data(iio_np);
        rt_ofw_node_put(iio_np);

        if (out_channel)
        {
            *out_channel = iio_args.args[0];
        }
    }
#endif /* RT_USING_OFW */
    return iio;
}

void *rt_iio_channel_get_by_index(struct rt_device *dev, int index, int *out_channel)
{
    void *iio = RT_NULL;

    if (!dev || index < 0)
    {
        return RT_NULL;
    }

    if (dev->ofw_node)
    {
        iio = ofw_iio_channel_get_by_index(dev->ofw_node, index, out_channel);
    }

    return iio;
}

void *rt_iio_channel_get_by_name(struct rt_device *dev, const char *name, int *out_channel)
{
    int index;

    if (!dev || !name)
    {
        return RT_NULL;
    }

    index = rt_dm_dev_prop_index_of_string(dev, "io-channel-names", name);

    return rt_iio_channel_get_by_index(dev, index, out_channel);
}