Skip to content

Commit 6342eff

Browse files
fabiobaltierinashif
authored andcommitted
led: add a struct led_dt_spec and some _dt wrapper APIs
Add a struct led_dt_spec to hold an LED device and index pointer, some initializer and wrapper APIs. This allows simpler LED usage, such as: static const struct led_dt_spec led = LED_DT_SPEC_GET(DT_NODELABEL(led0)); led_on_dt(&led); led_off_dt(&led); Signed-off-by: Fabio Baltieri <[email protected]>
1 parent 0ea2129 commit 6342eff

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

doc/releases/release-notes-4.1.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ Drivers and Sensors
118118

119119
* LED
120120

121+
* Added a new set of devicetree based LED APIs, see :c:struct:`led_dt_spec`.
122+
121123
* LED Strip
122124

123125
* LoRa

include/zephyr/drivers/led.h

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,135 @@ static inline int z_impl_led_off(const struct device *dev, uint32_t led)
330330
return api->off(dev, led);
331331
}
332332

333+
/*
334+
* LED DT helpers.
335+
*/
336+
337+
/**
338+
* @brief Container for an LED information specified in devicetree.
339+
*
340+
* This type contains a pointer to and LED device and an LED index.
341+
*
342+
* @see LED_DT_SPEC_GET
343+
* @see LED_DT_SPEC_GET_OR
344+
*/
345+
struct led_dt_spec {
346+
/** LED device instance. */
347+
const struct device *dev;
348+
/** Index of the LED on the controller. */
349+
uint32_t index;
350+
};
351+
352+
/**
353+
* @brief Set LED brightness from a led_dt_spec.
354+
*
355+
* @param spec LED device specification from devicetree.
356+
* @param value Brightness value to set in percent.
357+
* @return 0 on success, negative on error.
358+
*
359+
* @see led_set_brightness()
360+
*/
361+
static inline int led_set_brightness_dt(const struct led_dt_spec *spec,
362+
uint8_t value)
363+
{
364+
return led_set_brightness(spec->dev, spec->index, value);
365+
}
366+
367+
/**
368+
* @brief Turn on an LED from a struct led_dt_spec.
369+
*
370+
* @param spec LED device specification from devicetree.
371+
* @return 0 on success, negative on error.
372+
*
373+
* @see led_on()
374+
*/
375+
static inline int led_on_dt(const struct led_dt_spec *spec)
376+
{
377+
return led_on(spec->dev, spec->index);
378+
}
379+
380+
/**
381+
* @brief Turn off an LED from a struct led_dt_spec.
382+
*
383+
* @param spec LED device specification from devicetree.
384+
* @return 0 on success, negative on error.
385+
*
386+
* @see led_off()
387+
*/
388+
static inline int led_off_dt(const struct led_dt_spec *spec)
389+
{
390+
return led_off(spec->dev, spec->index);
391+
}
392+
393+
/**
394+
* @brief Validate that the LED device is ready.
395+
*
396+
* @param spec LED specification from devicetree.
397+
*
398+
* @retval true If the LED device is ready for use.
399+
* @retval false If the LED device is not ready for use.
400+
*/
401+
static inline bool led_is_ready_dt(const struct led_dt_spec *spec)
402+
{
403+
return device_is_ready(spec->dev);
404+
}
405+
406+
/**
407+
* @brief Static initializer for a struct led_dt_spec
408+
*
409+
* This returns a static initializer for a struct led_dt_spec given a devicetree
410+
* node identifier.
411+
*
412+
* Example devicetree fragment:
413+
*
414+
* @code{.dts}
415+
* leds {
416+
* compatible = "gpio-leds";
417+
* led0: led_0 {
418+
* ...
419+
* };
420+
* };
421+
* @endcode
422+
*
423+
* Example usage:
424+
*
425+
* @code{.c}
426+
* const struct led_dt_spec spec = LED_DT_SPEC_GET(DT_NODELABEL(led0));
427+
*
428+
* // Initializes 'spec' to:
429+
* // {
430+
* // .dev = DEVICE_DT_GET(DT_PARENT(led0)),
431+
* // .index = 0,
432+
* // }
433+
* @endcode
434+
*
435+
* The device (dev) must still be checked for readiness, e.g. using
436+
* device_is_ready().
437+
*
438+
* @param node_id Devicetree node identifier.
439+
*
440+
* @return Static initializer for a struct led_dt_spec for the property.
441+
*/
442+
#define LED_DT_SPEC_GET(node_id) \
443+
{ \
444+
.dev = DEVICE_DT_GET(DT_PARENT(node_id)), \
445+
.index = DT_NODE_CHILD_IDX(node_id), \
446+
}
447+
448+
/**
449+
* @brief Like LED_DT_SPEC_GET(), with a fallback value if the node does not exist.
450+
*
451+
* @param node_id Devicetree node identifier.
452+
*
453+
* @return Static initializer for a struct led_dt_spec for the property.
454+
*
455+
* @see LED_DT_SPEC_GET
456+
*/
457+
#define LED_DT_SPEC_GET_OR(node_id, default_value) \
458+
COND_CODE_1(DT_NODE_EXISTS(node_id), \
459+
(LED_DT_SPEC_GET(node_id)), \
460+
(default_value))
461+
333462
/**
334463
* @}
335464
*/

0 commit comments

Comments
 (0)