1 /**
2  * @file
3  * @brief IO channels devicetree macro public API header file.
4  */
5 
6 /*
7  * Copyright (c) 2020, Linaro Ltd.
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_DEVICETREE_IO_CHANNELS_H_
13 #define ZEPHYR_INCLUDE_DEVICETREE_IO_CHANNELS_H_
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /**
20  * @defgroup devicetree-io-channels Devicetree IO Channels API
21  * @ingroup devicetree
22  * @{
23  */
24 
25 /**
26  *
27  * @brief Get the node identifier for the node referenced by an
28  *        io-channels property at an index
29  *
30  * Example devicetree fragment:
31  *
32  *     adc1: adc@... { ... };
33  *
34  *     adc2: adc@... { ... };
35  *
36  *     n: node {
37  *             io-channels = <&adc1 10>, <&adc2 20>;
38  *     };
39  *
40  * Example usage:
41  *
42  *     DT_IO_CHANNELS_CTLR_BY_IDX(DT_NODELABEL(n), 0) // DT_NODELABEL(adc1)
43  *     DT_IO_CHANNELS_CTLR_BY_IDX(DT_NODELABEL(n), 1) // DT_NODELABEL(adc2)
44  *
45  * @param node_id node identifier for a node with an io-channels property
46  * @param idx logical index into io-channels property
47  * @return the node identifier for the node referenced at index "idx"
48  * @see DT_PROP_BY_PHANDLE_IDX()
49  */
50 #define DT_IO_CHANNELS_CTLR_BY_IDX(node_id, idx) \
51 	DT_PHANDLE_BY_IDX(node_id, io_channels, idx)
52 
53 /**
54  * @brief Get the node identifier for the node referenced by an
55  *        io-channels property by name
56  *
57  * Example devicetree fragment:
58  *
59  *     adc1: adc@... { ... };
60  *
61  *     adc2: adc@... { ... };
62  *
63  *     n: node {
64  *             io-channels = <&adc1 10>, <&adc2 20>;
65  *             io-channel-names = "SENSOR", "BANDGAP";
66  *     };
67  *
68  * Example usage:
69  *
70  *  DT_IO_CHANNELS_CTLR_BY_NAME(DT_NODELABEL(n), sensor) // DT_NODELABEL(adc1)
71  *  DT_IO_CHANNELS_CTLR_BY_NAME(DT_NODELABEL(n), bandgap) // DT_NODELABEL(adc2)
72  *
73  * @param node_id node identifier for a node with an io-channels property
74  * @param name lowercase-and-underscores name of an io-channels element
75  *             as defined by the node's io-channel-names property
76  * @return the node identifier for the node referenced at the named element
77  * @see DT_PHANDLE_BY_NAME()
78  */
79 #define DT_IO_CHANNELS_CTLR_BY_NAME(node_id, name) \
80 	DT_PHANDLE_BY_NAME(node_id, io_channels, name)
81 
82 /**
83  * @brief Equivalent to DT_IO_CHANNELS_CTLR_BY_IDX(node_id, 0)
84  * @param node_id node identifier for a node with an io-channels property
85  * @return the node identifier for the node referenced at index 0
86  *         in the node's "io-channels" property
87  * @see DT_IO_CHANNELS_CTLR_BY_IDX()
88  */
89 #define DT_IO_CHANNELS_CTLR(node_id) DT_IO_CHANNELS_CTLR_BY_IDX(node_id, 0)
90 
91 /**
92  * @brief Get the node identifier from a DT_DRV_COMPAT instance's io-channels
93  *        property at an index
94  *
95  * @param inst DT_DRV_COMPAT instance number
96  * @param idx logical index into io-channels property
97  * @return the node identifier for the node referenced at index "idx"
98  * @see DT_IO_CHANNELS_CTLR_BY_IDX()
99  */
100 #define DT_INST_IO_CHANNELS_CTLR_BY_IDX(inst, idx) \
101 	DT_IO_CHANNELS_CTLR_BY_IDX(DT_DRV_INST(inst), idx)
102 
103 /**
104  * @brief Get the node identifier from a DT_DRV_COMPAT instance's io-channels
105  *        property by name
106  * @param inst DT_DRV_COMPAT instance number
107  * @param name lowercase-and-underscores name of an io-channels element
108  *             as defined by the node's io-channel-names property
109  * @return the node identifier for the node referenced at the named element
110  * @see DT_IO_CHANNELS_CTLR_BY_NAME()
111  */
112 #define DT_INST_IO_CHANNELS_CTLR_BY_NAME(inst, name) \
113 	DT_IO_CHANNELS_CTLR_BY_NAME(DT_DRV_INST(inst), name)
114 
115 /**
116  * @brief Equivalent to DT_INST_IO_CHANNELS_CTLR_BY_IDX(inst, 0)
117  * @param inst DT_DRV_COMPAT instance number
118  * @return the node identifier for the node referenced at index 0
119  *         in the node's "io-channels" property
120  * @see DT_IO_CHANNELS_CTLR_BY_IDX()
121  */
122 #define DT_INST_IO_CHANNELS_CTLR(inst) DT_INST_IO_CHANNELS_CTLR_BY_IDX(inst, 0)
123 
124 /**
125  * @brief Get an io-channels specifier input cell at an index
126  *
127  * This macro only works for io-channels specifiers with cells named
128  * "input". Refer to the node's binding to check if necessary.
129  *
130  * Example devicetree fragment:
131  *
132  *     adc1: adc@... {
133  *             compatible = "vnd,adc";
134  *             #io-channel-cells = <1>;
135  *     };
136  *
137  *     adc2: adc@... {
138  *             compatible = "vnd,adc";
139  *             #io-channel-cells = <1>;
140  *     };
141  *
142  *     n: node {
143  *             io-channels = <&adc1 10>, <&adc2 20>;
144  *     };
145  *
146  * Bindings fragment for the vnd,adc compatible:
147  *
148  *    io-channel-cells:
149  *      - input
150  *
151  * Example usage:
152  *
153  *     DT_IO_CHANNELS_INPUT_BY_IDX(DT_NODELABEL(n), 0) // 10
154  *     DT_IO_CHANNELS_INPUT_BY_IDX(DT_NODELABEL(n), 1) // 20
155  *
156  * @param node_id node identifier for a node with an io-channels property
157  * @param idx logical index into io-channels property
158  * @return the input cell in the specifier at index "idx"
159  * @see DT_PHA_BY_IDX()
160  */
161 #define DT_IO_CHANNELS_INPUT_BY_IDX(node_id, idx) \
162 	DT_PHA_BY_IDX(node_id, io_channels, idx, input)
163 
164 /**
165  * @brief Get an io-channels specifier input cell by name
166  *
167  * This macro only works for io-channels specifiers with cells named
168  * "input". Refer to the node's binding to check if necessary.
169  *
170  * Example devicetree fragment:
171  *
172  *     adc1: adc@... {
173  *             compatible = "vnd,adc";
174  *             #io-channel-cells = <1>;
175  *     };
176  *
177  *     adc2: adc@... {
178  *             compatible = "vnd,adc";
179  *             #io-channel-cells = <1>;
180  *     };
181  *
182  *     n: node {
183  *             io-channels = <&adc1 10>, <&adc2 20>;
184  *             io-channel-names = "SENSOR", "BANDGAP";
185  *     };
186  *
187  * Bindings fragment for the vnd,adc compatible:
188  *
189  *    io-channel-cells:
190  *      - input
191  *
192  * Example usage:
193  *
194  *     DT_IO_CHANNELS_INPUT_BY_NAME(DT_NODELABEL(n), sensor) // 10
195  *     DT_IO_CHANNELS_INPUT_BY_NAME(DT_NODELABEL(n), bandgap) // 20
196  *
197  * @param node_id node identifier for a node with an io-channels property
198  * @param name lowercase-and-underscores name of an io-channels element
199  *             as defined by the node's io-channel-names property
200  * @return the input cell in the specifier at the named element
201  * @see DT_PHA_BY_NAME()
202  */
203 #define DT_IO_CHANNELS_INPUT_BY_NAME(node_id, name) \
204 	DT_PHA_BY_NAME(node_id, io_channels, name, input)
205 /**
206  * @brief Equivalent to DT_IO_CHANNELS_INPUT_BY_IDX(node_id, 0)
207  * @param node_id node identifier for a node with an io-channels property
208  * @return the input cell in the specifier at index 0
209  * @see DT_IO_CHANNELS_INPUT_BY_IDX()
210  */
211 #define DT_IO_CHANNELS_INPUT(node_id) DT_IO_CHANNELS_INPUT_BY_IDX(node_id, 0)
212 
213 /**
214  * @brief Get an input cell from the "DT_DRV_INST(inst)" io-channels
215  *        property at an index
216  * @param inst DT_DRV_COMPAT instance number
217  * @param idx logical index into io-channels property
218  * @return the input cell in the specifier at index "idx"
219  * @see DT_IO_CHANNELS_INPUT_BY_IDX()
220  */
221 #define DT_INST_IO_CHANNELS_INPUT_BY_IDX(inst, idx) \
222 	DT_IO_CHANNELS_INPUT_BY_IDX(DT_DRV_INST(inst), idx)
223 
224 /**
225  * @brief Get an input cell from the "DT_DRV_INST(inst)" io-channels
226  *        property by name
227  * @param inst DT_DRV_COMPAT instance number
228  * @param name lowercase-and-underscores name of an io-channels element
229  *             as defined by the instance's io-channel-names property
230  * @return the input cell in the specifier at the named element
231  * @see DT_IO_CHANNELS_INPUT_BY_NAME()
232  */
233 #define DT_INST_IO_CHANNELS_INPUT_BY_NAME(inst, name) \
234 	DT_IO_CHANNELS_INPUT_BY_NAME(DT_DRV_INST(inst), name)
235 
236 /**
237  * @brief Equivalent to DT_INST_IO_CHANNELS_INPUT_BY_IDX(inst, 0)
238  * @param inst DT_DRV_COMPAT instance number
239  * @return the input cell in the specifier at index 0
240  */
241 #define DT_INST_IO_CHANNELS_INPUT(inst) DT_INST_IO_CHANNELS_INPUT_BY_IDX(inst, 0)
242 
243 /**
244  * @}
245  */
246 
247 #ifdef __cplusplus
248 }
249 #endif
250 
251 #endif  /* ZEPHYR_INCLUDE_DEVICETREE_IO_CHANNELS_H_ */
252