Skip to content

Commit 3dae434

Browse files
committed
include: drivers: led: introduce LED_DT_SPEC_GET_BY_IDX macro
LED_DT_SPEC_GET_BY_IDX allows to initialize a led_dt_spec structure from a phandle array of LED child nodes. A such array can be found in a leds-group-multicolor compatible DT node. Here is an example of DT fragment: leds { compatible = "gpio-leds"; led0: led_0 { ... }; led1: led_1 { ... }; led2: led_2 { ... }; }; multi_led: multi-led { compatible = "leds-group-multicolor"; leds = <&led0>, <&led1>, <&led2>; }; This macro will be used by the leds-group-multicolor driver to initialize the struct led_dt_spec associated to each phandle. Signed-off-by: Simon Guinot <simon.guinot@seagate.com>
1 parent cff33a6 commit 3dae434

File tree

1 file changed

+59
-0
lines changed
  • include/zephyr/drivers

1 file changed

+59
-0
lines changed

include/zephyr/drivers/led.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,65 @@ static inline bool led_is_ready_dt(const struct led_dt_spec *spec)
502502
(LED_DT_SPEC_GET(node_id)), \
503503
(default_value))
504504

505+
/**
506+
* @brief Static initializer for a struct led_dt_spec
507+
*
508+
* This returns a static initializer for a struct led_dt_spec given a devicetree
509+
* node identifier, a property specifying a phandle array of LEDs and an index.
510+
*
511+
* Example devicetree fragment:
512+
*
513+
* @code{.dts}
514+
* leds {
515+
* compatible = "gpio-leds";
516+
*
517+
* led0: led_0 {
518+
* ...
519+
* };
520+
* led1: led_1 {
521+
* ...
522+
* };
523+
* led2: led_2 {
524+
* ...
525+
* };
526+
* };
527+
*
528+
* multi_led: multi-led {
529+
* compatible = "leds-group-multicolor";
530+
* leds = <&led0>, <&led1>, <&led2>;
531+
* };
532+
* @endcode
533+
*
534+
* Example usage:
535+
*
536+
* @code{.c}
537+
* const struct led_dt_spec spec =
538+
* LED_DT_SPEC_GET_BY_IDX(DT_NODELABEL(multi_led), leds, 1);
539+
*
540+
* // Initializes 'spec' to:
541+
* // {
542+
* // .dev = DEVICE_DT_GET(DT_PARENT(led0)),
543+
* // .index = 1,
544+
* // }
545+
* @endcode
546+
*
547+
* The device (dev) must still be checked for readiness, e.g. using
548+
* device_is_ready().
549+
*
550+
* @param node_id devicetree node identifier.
551+
* @param prop lowercase-and-underscores property name
552+
* @param idx logical index into "prop"
553+
*
554+
* @return Static initializer for a struct led_dt_spec for the property.
555+
*/
556+
#define LED_DT_SPEC_GET_BY_IDX(node_id, prop, idx) \
557+
{ \
558+
.dev = DEVICE_DT_GET(DT_PARENT( \
559+
DT_PHANDLE_BY_IDX(node_id, prop, idx))), \
560+
.index = DT_NODE_CHILD_IDX( \
561+
DT_PHANDLE_BY_IDX(node_id, prop, idx)), \
562+
}
563+
505564
/**
506565
* @}
507566
*/

0 commit comments

Comments
 (0)