|
| 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 | + |
| 31 | + if (led != 0) { |
| 32 | + return -EINVAL; |
| 33 | + } |
| 34 | + if (num_colors != config->num_leds) { |
| 35 | + return -EINVAL; |
| 36 | + } |
| 37 | + |
| 38 | + for (uint8_t i = 0; i < num_colors; i++) { |
| 39 | + int err; |
| 40 | + |
| 41 | + err = led_set_brightness_dt(&config->led[i], color[i]); |
| 42 | + if (err) { |
| 43 | + return err; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return 0; |
| 48 | +} |
| 49 | + |
| 50 | +static int leds_group_multicolor_init(const struct device *dev) |
| 51 | +{ |
| 52 | + const struct leds_group_multicolor_config *config = dev->config; |
| 53 | + |
| 54 | + for (uint8_t i = 0; i < config->num_leds; i++) { |
| 55 | + const struct led_dt_spec *led = &config->led[i]; |
| 56 | + |
| 57 | + if (!led_is_ready_dt(led)) { |
| 58 | + LOG_ERR("%s: LED device %s is not ready", dev->name, led->dev->name); |
| 59 | + return -ENODEV; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +static DEVICE_API(led, leds_group_multicolor_api) = { |
| 67 | + .set_color = leds_group_multicolor_set_color, |
| 68 | +}; |
| 69 | + |
| 70 | +#define LED_DT_SPEC_GET_BY_PHANDLE_IDX(node_id, prop, idx) \ |
| 71 | + LED_DT_SPEC_GET(DT_PHANDLE_BY_IDX(node_id, prop, idx)) |
| 72 | + |
| 73 | +#define LEDS_GROUP_MULTICOLOR_DEVICE(inst) \ |
| 74 | + \ |
| 75 | +BUILD_ASSERT(DT_INST_PROP_LEN(inst, leds) > 0, \ |
| 76 | + "at least one LED phandle must be present"); \ |
| 77 | + \ |
| 78 | +static const struct led_dt_spec led_group_multicolor_##inst[] = { \ |
| 79 | + DT_INST_FOREACH_PROP_ELEM_SEP( \ |
| 80 | + inst, leds, LED_DT_SPEC_GET_BY_PHANDLE_IDX, (,)) \ |
| 81 | +}; \ |
| 82 | + \ |
| 83 | +static const struct leds_group_multicolor_config \ |
| 84 | + leds_group_multicolor_config_##inst = { \ |
| 85 | + .num_leds = ARRAY_SIZE(led_group_multicolor_##inst), \ |
| 86 | + .led = led_group_multicolor_##inst, \ |
| 87 | +}; \ |
| 88 | + \ |
| 89 | +DEVICE_DT_INST_DEFINE(inst, &leds_group_multicolor_init, NULL, \ |
| 90 | + NULL, &leds_group_multicolor_config_##inst, \ |
| 91 | + POST_KERNEL, CONFIG_LED_INIT_PRIORITY, \ |
| 92 | + &leds_group_multicolor_api); |
| 93 | + |
| 94 | +DT_INST_FOREACH_STATUS_OKAY(LEDS_GROUP_MULTICOLOR_DEVICE) |
0 commit comments