1 /**
2  * @file
3  * @brief PWMs 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_PWMS_H_
13 #define ZEPHYR_INCLUDE_DEVICETREE_PWMS_H_
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /**
20  * @defgroup devicetree-pwms Devicetree PWMs API
21  * @ingroup devicetree
22  * @{
23  */
24 
25 /**
26  * @brief Get the node identifier for the PWM controller from a
27  *        pwms property at an index
28  *
29  * Example devicetree fragment:
30  *
31  *     pwm1: pwm-controller@... { ... };
32  *
33  *     pwm2: pwm-controller@... { ... };
34  *
35  *     n: node {
36  *             pwms = <&pwm1 1 PWM_POLARITY_NORMAL>,
37  *                    <&pwm2 3 PWM_POLARITY_INVERTED>;
38  *     };
39  *
40  * Example usage:
41  *
42  *     DT_PWMS_CTLR_BY_IDX(DT_NODELABEL(n), 0) // DT_NODELABEL(pwm1)
43  *     DT_PWMS_CTLR_BY_IDX(DT_NODELABEL(n), 1) // DT_NODELABEL(pwm2)
44  *
45  * @param node_id node identifier for a node with a pwms property
46  * @param idx logical index into pwms property
47  * @return the node identifier for the PWM controller referenced at
48  *         index "idx"
49  * @see DT_PROP_BY_PHANDLE_IDX()
50  */
51 #define DT_PWMS_CTLR_BY_IDX(node_id, idx) \
52 	DT_PHANDLE_BY_IDX(node_id, pwms, idx)
53 
54 /**
55  * @brief Get the node identifier for the PWM controller from a
56  *        pwms property by name
57  *
58  * Example devicetree fragment:
59  *
60  *     pwm1: pwm-controller@... { ... };
61  *
62  *    pwm2: pwm-controller@... { ... };
63  *
64  *     n: node {
65  *             pwms = <&pwm1 1 PWM_POLARITY_NORMAL>,
66  *                    <&pwm2 3 PWM_POLARITY_INVERTED>;
67  *             pwm-names = "alpha", "beta";
68  *     };
69  *
70  * Example usage:
71  *
72  *     DT_PWMS_CTLR_BY_NAME(DT_NODELABEL(n), alpha) // DT_NODELABEL(pwm1)
73  *     DT_PWMS_CTLR_BY_NAME(DT_NODELABEL(n), beta)  // DT_NODELABEL(pwm2)
74  *
75  * @param node_id node identifier for a node with a pwms property
76  * @param name lowercase-and-underscores name of a pwms element
77  *             as defined by the node's pwm-names property
78  * @return the node identifier for the PWM controller in the named element
79  * @see DT_PHANDLE_BY_NAME()
80  */
81 #define DT_PWMS_CTLR_BY_NAME(node_id, name) \
82 	DT_PHANDLE_BY_NAME(node_id, pwms, name)
83 
84 /**
85  * @brief Equivalent to DT_PWMS_CTLR_BY_IDX(node_id, 0)
86  * @param node_id node identifier for a node with a pwms property
87  * @return the node identifier for the PWM controller at index 0
88  *         in the node's "pwms" property
89  * @see DT_PWMS_CTLR_BY_IDX()
90  */
91 #define DT_PWMS_CTLR(node_id) DT_PWMS_CTLR_BY_IDX(node_id, 0)
92 
93 /**
94  * @brief Get PWM specifier's cell value at an index
95  *
96  * Example devicetree fragment:
97  *
98  *     pwm1: pwm-controller@... {
99  *             compatible = "vnd,pwm";
100  *             #pwm-cells = <2>;
101  *     };
102  *
103  *     pwm2: pwm-controller@... {
104  *             compatible = "vnd,pwm";
105  *             #pwm-cells = <2>;
106  *     };
107  *
108  *     n: node {
109  *             pwms = <&pwm1 1 200000 PWM_POLARITY_NORMAL>,
110  *                    <&pwm2 3 100000 PWM_POLARITY_INVERTED>;
111  *     };
112  *
113  * Bindings fragment for the "vnd,pwm" compatible:
114  *
115  *     pwm-cells:
116  *       - channel
117  *       - period
118  *       - flags
119  *
120  * Example usage:
121  *
122  *     DT_PWMS_CELL_BY_IDX(DT_NODELABEL(n), 0, channel) // 1
123  *     DT_PWMS_CELL_BY_IDX(DT_NODELABEL(n), 1, channel) // 3
124  *     DT_PWMS_CELL_BY_IDX(DT_NODELABEL(n), 0, period)  // 200000
125  *     DT_PWMS_CELL_BY_IDX(DT_NODELABEL(n), 1, period)  // 100000
126  *     DT_PWMS_CELL_BY_IDX(DT_NODELABEL(n), 0, flags)   // PWM_POLARITY_NORMAL
127  *     DT_PWMS_CELL_BY_IDX(DT_NODELABEL(n), 1, flags)   // PWM_POLARITY_INVERTED
128  *
129  * @param node_id node identifier for a node with a pwms property
130  * @param idx logical index into pwms property
131  * @param cell lowercase-and-underscores cell name
132  * @return the cell value at index "idx"
133  * @see DT_PHA_BY_IDX()
134  */
135 #define DT_PWMS_CELL_BY_IDX(node_id, idx, cell) \
136 	DT_PHA_BY_IDX(node_id, pwms, idx, cell)
137 
138 /**
139  * @brief Get a PWM specifier's cell value by name
140  *
141  * Example devicetree fragment:
142  *
143  *     pwm1: pwm-controller@... {
144  *             compatible = "vnd,pwm";
145  *             #pwm-cells = <2>;
146  *     };
147  *
148  *     pwm2: pwm-controller@... {
149  *             compatible = "vnd,pwm";
150  *             #pwm-cells = <2>;
151  *     };
152  *
153  *     n: node {
154  *             pwms = <&pwm1 1 200000 PWM_POLARITY_NORMAL>,
155  *                    <&pwm2 3 100000 PWM_POLARITY_INVERTED>;
156  *             pwm-names = "alpha", "beta";
157  *     };
158  *
159  * Bindings fragment for the "vnd,pwm" compatible:
160  *
161  *     pwm-cells:
162  *       - channel
163  *       - period
164  *       - flags
165  *
166  * Example usage:
167  *
168  *     DT_PWMS_CELL_BY_NAME(DT_NODELABEL(n), alpha, channel) // 1
169  *     DT_PWMS_CELL_BY_NAME(DT_NODELABEL(n), beta, channel)  // 3
170  *     DT_PWMS_CELL_BY_NAME(DT_NODELABEL(n), alpha, period)  // 200000
171  *     DT_PWMS_CELL_BY_NAME(DT_NODELABEL(n), beta, period)   // 100000
172  *     DT_PWMS_CELL_BY_NAME(DT_NODELABEL(n), alpha, flags)   // PWM_POLARITY_NORMAL
173  *     DT_PWMS_CELL_BY_NAME(DT_NODELABEL(n), beta, flags)    // PWM_POLARITY_INVERTED
174  *
175  * @param node_id node identifier for a node with a pwms property
176  * @param name lowercase-and-underscores name of a pwms element
177  *             as defined by the node's pwm-names property
178  * @param cell lowercase-and-underscores cell name
179  * @return the cell value in the specifier at the named element
180  * @see DT_PHA_BY_NAME()
181  */
182 #define DT_PWMS_CELL_BY_NAME(node_id, name, cell) \
183 	DT_PHA_BY_NAME(node_id, pwms, name, cell)
184 
185 /**
186  * @brief Equivalent to DT_PWMS_CELL_BY_IDX(node_id, 0, cell)
187  * @param node_id node identifier for a node with a pwms property
188  * @param cell lowercase-and-underscores cell name
189  * @return the cell value at index 0
190  * @see DT_PWMS_CELL_BY_IDX()
191  */
192 #define DT_PWMS_CELL(node_id, cell) DT_PWMS_CELL_BY_IDX(node_id, 0, cell)
193 
194 /**
195  * @brief Get a PWM specifier's channel cell value at an index
196  *
197  * This macro only works for PWM specifiers with cells named "channel".
198  * Refer to the node's binding to check if necessary.
199  *
200  * This is equivalent to DT_PWMS_CELL_BY_IDX(node_id, idx, channel).
201  *
202  * @param node_id node identifier for a node with a pwms property
203  * @param idx logical index into pwms property
204  * @return the channel cell value at index "idx"
205  * @see DT_PWMS_CELL_BY_IDX()
206  */
207 #define DT_PWMS_CHANNEL_BY_IDX(node_id, idx) \
208 	DT_PWMS_CELL_BY_IDX(node_id, idx, channel)
209 
210 /**
211  * @brief Get a PWM specifier's channel cell value by name
212  *
213  * This macro only works for PWM specifiers with cells named "channel".
214  * Refer to the node's binding to check if necessary.
215  *
216  * This is equivalent to DT_PWMS_CELL_BY_NAME(node_id, name, channel).
217  *
218  * @param node_id node identifier for a node with a pwms property
219  * @param name lowercase-and-underscores name of a pwms element
220  *             as defined by the node's pwm-names property
221  * @return the channel cell value in the specifier at the named element
222  * @see DT_PWMS_CELL_BY_NAME()
223  */
224 #define DT_PWMS_CHANNEL_BY_NAME(node_id, name) \
225 	DT_PWMS_CELL_BY_NAME(node_id, name, channel)
226 
227 /**
228  * @brief Equivalent to DT_PWMS_CHANNEL_BY_IDX(node_id, 0)
229  * @param node_id node identifier for a node with a pwms property
230  * @return the channel cell value at index 0
231  * @see DT_PWMS_CHANNEL_BY_IDX()
232  */
233 #define DT_PWMS_CHANNEL(node_id) DT_PWMS_CHANNEL_BY_IDX(node_id, 0)
234 
235 /**
236  * @brief Get PWM specifier's period cell value at an index
237  *
238  * This macro only works for PWM specifiers with cells named "period".
239  * Refer to the node's binding to check if necessary.
240  *
241  * This is equivalent to DT_PWMS_CELL_BY_IDX(node_id, idx, period).
242  *
243  * @param node_id node identifier for a node with a pwms property
244  * @param idx logical index into pwms property
245  * @return the period cell value at index "idx"
246  * @see DT_PWMS_CELL_BY_IDX()
247  */
248 #define DT_PWMS_PERIOD_BY_IDX(node_id, idx) \
249 	DT_PWMS_CELL_BY_IDX(node_id, idx, period)
250 
251 /**
252  * @brief Get a PWM specifier's period cell value by name
253  *
254  * This macro only works for PWM specifiers with cells named "period".
255  * Refer to the node's binding to check if necessary.
256  *
257  * This is equivalent to DT_PWMS_CELL_BY_NAME(node_id, name, period).
258  *
259  * @param node_id node identifier for a node with a pwms property
260  * @param name lowercase-and-underscores name of a pwms element
261  *             as defined by the node's pwm-names property
262  * @return the period cell value in the specifier at the named element
263  * @see DT_PWMS_CELL_BY_NAME()
264  */
265 #define DT_PWMS_PERIOD_BY_NAME(node_id, name) \
266 	DT_PWMS_CELL_BY_NAME(node_id, name, period)
267 
268 /**
269  * @brief Equivalent to DT_PWMS_PERIOD_BY_IDX(node_id, 0)
270  * @param node_id node identifier for a node with a pwms property
271  * @return the period cell value at index 0
272  * @see DT_PWMS_PERIOD_BY_IDX()
273  */
274 #define DT_PWMS_PERIOD(node_id) DT_PWMS_PERIOD_BY_IDX(node_id, 0)
275 
276 /**
277  * @brief Get a PWM specifier's flags cell value at an index
278  *
279  * This macro expects PWM specifiers with cells named "flags".
280  * If there is no "flags" cell in the PWM specifier, zero is returned.
281  * Refer to the node's binding to check specifier cell names if necessary.
282  *
283  * This is equivalent to DT_PWMS_CELL_BY_IDX(node_id, idx, flags).
284  *
285  * @param node_id node identifier for a node with a pwms property
286  * @param idx logical index into pwms property
287  * @return the flags cell value at index "idx", or zero if there is none
288  * @see DT_PWMS_CELL_BY_IDX()
289  */
290 #define DT_PWMS_FLAGS_BY_IDX(node_id, idx) \
291 	DT_PHA_BY_IDX_OR(node_id, pwms, idx, flags, 0)
292 
293 /**
294  * @brief Get a PWM specifier's flags cell value by name
295  *
296  * This macro expects PWM specifiers with cells named "flags".
297  * If there is no "flags" cell in the PWM specifier, zero is returned.
298  * Refer to the node's binding to check specifier cell names if necessary.
299  *
300  * This is equivalent to DT_PWMS_CELL_BY_NAME(node_id, name, flags) if
301  * there is a flags cell, but expands to zero if there is none.
302  *
303  * @param node_id node identifier for a node with a pwms property
304  * @param name lowercase-and-underscores name of a pwms element
305  *             as defined by the node's pwm-names property
306  * @return the flags cell value in the specifier at the named element,
307  *         or zero if there is none
308  * @see DT_PWMS_CELL_BY_NAME()
309  */
310 #define DT_PWMS_FLAGS_BY_NAME(node_id, name) \
311 	DT_PHA_BY_NAME_OR(node_id, pwms, name, flags, 0)
312 
313 /**
314  * @brief Equivalent to DT_PWMS_FLAGS_BY_IDX(node_id, 0)
315  * @param node_id node identifier for a node with a pwms property
316  * @return the flags cell value at index 0, or zero if there is none
317  * @see DT_PWMS_FLAGS_BY_IDX()
318  */
319 #define DT_PWMS_FLAGS(node_id) DT_PWMS_FLAGS_BY_IDX(node_id, 0)
320 
321 /**
322  * @brief Get the node identifier for the PWM controller from a
323  *        DT_DRV_COMPAT instance's pwms property at an index
324  *
325  * @param inst DT_DRV_COMPAT instance number
326  * @param idx logical index into pwms property
327  * @return the node identifier for the PWM controller referenced at
328  *         index "idx"
329  * @see DT_PWMS_CTLR_BY_IDX()
330  */
331 #define DT_INST_PWMS_CTLR_BY_IDX(inst, idx) \
332 	DT_PWMS_CTLR_BY_IDX(DT_DRV_INST(inst), idx)
333 
334 /**
335  * @brief Get the node identifier for the PWM controller from a
336  *        DT_DRV_COMPAT instance's pwms property by name
337  * @param inst DT_DRV_COMPAT instance number
338  * @param name lowercase-and-underscores name of a pwms element
339  *             as defined by the node's pwm-names property
340  * @return the node identifier for the PWM controller in the named element
341  * @see DT_PWMS_CTLR_BY_NAME()
342  */
343 #define DT_INST_PWMS_CTLR_BY_NAME(inst, name) \
344 	DT_PWMS_CTLR_BY_NAME(DT_DRV_INST(inst), name)
345 
346 /**
347  * @brief Equivalent to DT_INST_PWMS_CTLR_BY_IDX(inst, 0)
348  * @param inst DT_DRV_COMPAT instance number
349  * @return the node identifier for the PWM controller at index 0
350  *         in the instance's "pwms" property
351  * @see DT_PWMS_CTLR_BY_IDX()
352  */
353 #define DT_INST_PWMS_CTLR(inst) DT_INST_PWMS_CTLR_BY_IDX(inst, 0)
354 
355 /**
356  * @brief Get a DT_DRV_COMPAT instance's PWM specifier's cell value
357  *        at an index
358  * @param inst DT_DRV_COMPAT instance number
359  * @param idx logical index into pwms property
360  * @param cell lowercase-and-underscores cell name
361  * @return the cell value at index "idx"
362  */
363 #define DT_INST_PWMS_CELL_BY_IDX(inst, idx, cell) \
364 	DT_PWMS_CELL_BY_IDX(DT_DRV_INST(inst), idx, cell)
365 
366 /**
367  * @brief Get a DT_DRV_COMPAT instance's PWM specifier's cell value by name
368  * @param inst DT_DRV_COMPAT instance number
369  * @param name lowercase-and-underscores name of a pwms element
370  *             as defined by the node's pwm-names property
371  * @param cell lowercase-and-underscores cell name
372  * @return the cell value in the specifier at the named element
373  * @see DT_PWMS_CELL_BY_NAME()
374  */
375 #define DT_INST_PWMS_CELL_BY_NAME(inst, name, cell) \
376 	DT_PWMS_CELL_BY_NAME(DT_DRV_INST(inst), name, cell)
377 
378 /**
379  * @brief Equivalent to DT_INST_PWMS_CELL_BY_IDX(inst, 0, cell)
380  * @param inst DT_DRV_COMPAT instance number
381  * @param cell lowercase-and-underscores cell name
382  * @return the cell value at index 0
383  */
384 #define DT_INST_PWMS_CELL(inst, cell) \
385 	DT_INST_PWMS_CELL_BY_IDX(inst, 0, cell)
386 
387 /**
388  * @brief Equivalent to DT_INST_PWMS_CELL_BY_IDX(inst, idx, channel)
389  * @param inst DT_DRV_COMPAT instance number
390  * @param idx logical index into pwms property
391  * @return the channel cell value at index "idx"
392  * @see DT_INST_PWMS_CELL_BY_IDX()
393  */
394 #define DT_INST_PWMS_CHANNEL_BY_IDX(inst, idx) \
395 	DT_INST_PWMS_CELL_BY_IDX(inst, idx, channel)
396 
397 /**
398  * @brief Equivalent to DT_INST_PWMS_CELL_BY_NAME(inst, name, channel)
399  * @param inst DT_DRV_COMPAT instance number
400  * @param name lowercase-and-underscores name of a pwms element
401  *             as defined by the node's pwm-names property
402  * @return the channel cell value in the specifier at the named element
403  * @see DT_INST_PWMS_CELL_BY_NAME()
404  */
405 #define DT_INST_PWMS_CHANNEL_BY_NAME(inst, name) \
406 	DT_INST_PWMS_CELL_BY_NAME(inst, name, channel)
407 
408 /**
409  * @brief Equivalent to DT_INST_PWMS_CHANNEL_BY_IDX(inst, 0)
410  * @param inst DT_DRV_COMPAT instance number
411  * @return the channel cell value at index 0
412  * @see DT_INST_PWMS_CHANNEL_BY_IDX()
413  */
414 #define DT_INST_PWMS_CHANNEL(inst) DT_INST_PWMS_CHANNEL_BY_IDX(inst, 0)
415 
416 /**
417  * @brief Equivalent to DT_INST_PWMS_CELL_BY_IDX(inst, idx, period)
418  * @param inst DT_DRV_COMPAT instance number
419  * @param idx logical index into pwms property
420  * @return the period cell value at index "idx"
421  * @see DT_INST_PWMS_CELL_BY_IDX()
422  */
423 #define DT_INST_PWMS_PERIOD_BY_IDX(inst, idx) \
424 	DT_INST_PWMS_CELL_BY_IDX(inst, idx, period)
425 
426 /**
427  * @brief Equivalent to DT_INST_PWMS_CELL_BY_NAME(inst, name, period)
428  * @param inst DT_DRV_COMPAT instance number
429  * @param name lowercase-and-underscores name of a pwms element
430  *             as defined by the node's pwm-names property
431  * @return the period cell value in the specifier at the named element
432  * @see DT_INST_PWMS_CELL_BY_NAME()
433  */
434 #define DT_INST_PWMS_PERIOD_BY_NAME(inst, name) \
435 	DT_INST_PWMS_CELL_BY_NAME(inst, name, period)
436 
437 /**
438  * @brief Equivalent to DT_INST_PWMS_PERIOD_BY_IDX(inst, 0)
439  * @param inst DT_DRV_COMPAT instance number
440  * @return the period cell value at index 0
441  * @see DT_INST_PWMS_PERIOD_BY_IDX()
442  */
443 #define DT_INST_PWMS_PERIOD(inst) DT_INST_PWMS_PERIOD_BY_IDX(inst, 0)
444 
445 /**
446  * @brief Equivalent to DT_INST_PWMS_CELL_BY_IDX(inst, idx, flags)
447  * @param inst DT_DRV_COMPAT instance number
448  * @param idx logical index into pwms property
449  * @return the flags cell value at index "idx", or zero if there is none
450  * @see DT_INST_PWMS_CELL_BY_IDX()
451  */
452 #define DT_INST_PWMS_FLAGS_BY_IDX(inst, idx) \
453 	DT_INST_PWMS_CELL_BY_IDX(inst, idx, flags)
454 
455 /**
456  * @brief Equivalent to DT_INST_PWMS_CELL_BY_NAME(inst, name, flags)
457  * @param inst DT_DRV_COMPAT instance number
458  * @param name lowercase-and-underscores name of a pwms element
459  *             as defined by the node's pwm-names property
460  * @return the flags cell value in the specifier at the named element,
461  *         or zero if there is none
462  * @see DT_INST_PWMS_CELL_BY_NAME()
463  */
464 #define DT_INST_PWMS_FLAGS_BY_NAME(inst, name) \
465 	DT_INST_PWMS_CELL_BY_NAME(inst, name, flags)
466 
467 /**
468  * @brief Equivalent to DT_INST_PWMS_FLAGS_BY_IDX(inst, 0)
469  * @param inst DT_DRV_COMPAT instance number
470  * @return the flags cell value at index 0, or zero if there is none
471  * @see DT_INST_PWMS_FLAGS_BY_IDX()
472  */
473 #define DT_INST_PWMS_FLAGS(inst) DT_INST_PWMS_FLAGS_BY_IDX(inst, 0)
474 
475 /**
476  * @}
477  */
478 
479 #ifdef __cplusplus
480 }
481 #endif
482 
483 #endif  /* ZEPHYR_INCLUDE_DEVICETREE_PWMS_H_ */
484