Skip to content

Commit 25904ef

Browse files
committed
drivers: led: add leds-group-multicolor driver
This driver supports multi-color LEDs built with several monochromatic LEDs. ->set_color is the only LED driver API method implemented. Instead of calling led_set_brightness() for each monochromatic LED, led_set_color() can be called on the leds-group-multicolor device to set all colors at once. See the leds-group-multicolor DT binding for details. Signed-off-by: Simon Guinot <simon.guinot@seagate.com>
1 parent 3dae434 commit 25904ef

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

drivers/led/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ zephyr_library_sources_ifdef(CONFIG_HT16K33 ht16k33.c)
99
zephyr_library_sources_ifdef(CONFIG_IS31FL3194 is31fl3194.c)
1010
zephyr_library_sources_ifdef(CONFIG_IS31FL3216A is31fl3216a.c)
1111
zephyr_library_sources_ifdef(CONFIG_IS31FL3733 is31fl3733.c)
12+
zephyr_library_sources_ifdef(CONFIG_LEDS_GROUP_MULTICOLOR leds_group_multicolor.c)
1213
zephyr_library_sources_ifdef(CONFIG_LED_AXP192_AXP2101 led_axp192.c)
1314
zephyr_library_sources_ifdef(CONFIG_LED_DAC led_dac.c)
1415
zephyr_library_sources_ifdef(CONFIG_LED_GPIO led_gpio.c)

drivers/led/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ source "drivers/led/Kconfig.ht16k33"
3434
source "drivers/led/Kconfig.is31fl3194"
3535
source "drivers/led/Kconfig.is31fl3216a"
3636
source "drivers/led/Kconfig.is31fl3733"
37+
source "drivers/led/Kconfig.leds-group-multicolor"
3738
source "drivers/led/Kconfig.lp3943"
3839
source "drivers/led/Kconfig.lp50xx"
3940
source "drivers/led/Kconfig.lp5562"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2025 Seagate Technology LLC
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config LEDS_GROUP_MULTICOLOR
5+
bool "LEDs group multi-color driver"
6+
default y
7+
depends on DT_HAS_LEDS_GROUP_MULTICOLOR_ENABLED
8+
help
9+
Enable this driver to supports multi-color LEDs built with several
10+
monochromatic LEDs.
11+
12+
See the leds-group-multicolor DT binding for details.

drivers/led/leds_group_multicolor.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2025 Seagate Technology LLC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT leds_group_multicolor
8+
9+
/**
10+
* @file
11+
* @brief Driver for multi-color LED built from monochromatic LEDs.
12+
*/
13+
14+
#include <zephyr/drivers/led.h>
15+
#include <zephyr/device.h>
16+
#include <zephyr/kernel.h>
17+
18+
#include <zephyr/logging/log.h>
19+
LOG_MODULE_REGISTER(leds_group_multicolor, CONFIG_LED_LOG_LEVEL);
20+
21+
struct leds_group_multicolor_config {
22+
uint8_t num_leds;
23+
const struct led_dt_spec *led;
24+
};
25+
26+
static int leds_group_multicolor_set_color(const struct device *dev, uint32_t led,
27+
uint8_t num_colors, const uint8_t *color)
28+
{
29+
const struct leds_group_multicolor_config *config = dev->config;
30+
int err = 0;
31+
32+
if (led != 0) {
33+
return -EINVAL;
34+
}
35+
if (num_colors != config->num_leds) {
36+
return -EINVAL;
37+
}
38+
39+
for (uint8_t i = 0; i < num_colors; i++) {
40+
err |= led_set_brightness_dt(&config->led[i], color[i]);
41+
}
42+
43+
return 0;
44+
}
45+
46+
static int leds_group_multicolor_init(const struct device *dev)
47+
{
48+
const struct leds_group_multicolor_config *config = dev->config;
49+
50+
if (!config->num_leds) {
51+
LOG_ERR("%s: no LEDs found (leds property missing ?)", dev->name);
52+
return -ENODEV;
53+
}
54+
55+
for (uint8_t i = 0; i < config->num_leds; i++) {
56+
const struct led_dt_spec *led = &config->led[i];
57+
58+
if (!led_is_ready_dt(led)) {
59+
LOG_ERR("%s: LED device %s is not ready", dev->name, led->dev->name);
60+
return -ENODEV;
61+
}
62+
}
63+
64+
return 0;
65+
}
66+
67+
static DEVICE_API(led, leds_group_multicolor_api) = {
68+
.set_color = leds_group_multicolor_set_color,
69+
};
70+
71+
#define LEDS_GROUP_MULTICOLOR_DEVICE(inst) \
72+
\
73+
static const struct led_dt_spec led_group_multicolor_##inst[] = { \
74+
DT_INST_FOREACH_PROP_ELEM_SEP(inst, leds, LED_DT_SPEC_GET_BY_IDX, (,)) \
75+
}; \
76+
\
77+
static const struct leds_group_multicolor_config \
78+
leds_group_multicolor_config_##inst = { \
79+
.num_leds = ARRAY_SIZE(led_group_multicolor_##inst), \
80+
.led = led_group_multicolor_##inst, \
81+
}; \
82+
\
83+
DEVICE_DT_INST_DEFINE(inst, &leds_group_multicolor_init, NULL, \
84+
NULL, &leds_group_multicolor_config_##inst, \
85+
POST_KERNEL, CONFIG_LED_INIT_PRIORITY, \
86+
&leds_group_multicolor_api);
87+
88+
DT_INST_FOREACH_STATUS_OKAY(LEDS_GROUP_MULTICOLOR_DEVICE)

0 commit comments

Comments
 (0)