|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Prevas A/S |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#define DT_DRV_COMPAT dac_leds |
| 7 | + |
| 8 | +#include <errno.h> |
| 9 | +#include <stdint.h> |
| 10 | +#include <zephyr/device.h> |
| 11 | +#include <zephyr/drivers/dac.h> |
| 12 | +#include <zephyr/drivers/led.h> |
| 13 | +#include <zephyr/sys/math_extras.h> |
| 14 | + |
| 15 | +struct led_dac_leds { |
| 16 | + const struct device *dac; |
| 17 | + struct dac_channel_cfg chan_cfg; |
| 18 | + uint32_t dac_max; |
| 19 | + uint32_t dac_min; |
| 20 | +}; |
| 21 | + |
| 22 | +struct led_dac_config { |
| 23 | + const struct led_dac_leds *leds; |
| 24 | + uint8_t num_leds; |
| 25 | +}; |
| 26 | + |
| 27 | +static int led_dac_set_raw(const struct device *dev, uint32_t led, uint32_t value) |
| 28 | +{ |
| 29 | + const struct led_dac_config *config = dev->config; |
| 30 | + |
| 31 | + return dac_write_value(config->leds[led].dac, config->leds[led].chan_cfg.channel_id, value); |
| 32 | +} |
| 33 | + |
| 34 | +static int led_dac_set_brightness(const struct device *dev, uint32_t led, uint8_t pct) |
| 35 | +{ |
| 36 | + const struct led_dac_config *config = dev->config; |
| 37 | + uint32_t value; |
| 38 | + |
| 39 | + if (led >= config->num_leds) { |
| 40 | + return -EINVAL; |
| 41 | + } |
| 42 | + |
| 43 | + value = pct > 0 ? config->leds[led].dac_min + (uint64_t)(config->leds[led].dac_max - |
| 44 | + config->leds[led].dac_min) * |
| 45 | + pct / 100 |
| 46 | + : 0; |
| 47 | + |
| 48 | + return led_dac_set_raw(dev, led, value); |
| 49 | +} |
| 50 | + |
| 51 | +static inline int led_dac_on(const struct device *dev, uint32_t led) |
| 52 | +{ |
| 53 | + const struct led_dac_config *config = dev->config; |
| 54 | + |
| 55 | + if (led >= config->num_leds) { |
| 56 | + return -EINVAL; |
| 57 | + } |
| 58 | + |
| 59 | + return led_dac_set_raw(dev, led, config->leds[led].dac_max); |
| 60 | +} |
| 61 | + |
| 62 | +static inline int led_dac_off(const struct device *dev, uint32_t led) |
| 63 | +{ |
| 64 | + const struct led_dac_config *config = dev->config; |
| 65 | + |
| 66 | + if (led >= config->num_leds) { |
| 67 | + return -EINVAL; |
| 68 | + } |
| 69 | + |
| 70 | + return led_dac_set_raw(dev, led, 0); |
| 71 | +} |
| 72 | + |
| 73 | +static DEVICE_API(led, led_dac_api) = { |
| 74 | + .on = led_dac_on, |
| 75 | + .off = led_dac_off, |
| 76 | + .set_brightness = led_dac_set_brightness, |
| 77 | +}; |
| 78 | + |
| 79 | +static int led_dac_init(const struct device *dev) |
| 80 | +{ |
| 81 | + const struct led_dac_config *config = dev->config; |
| 82 | + int ret; |
| 83 | + |
| 84 | + for (uint8_t i = 0; i < config->num_leds; ++i) { |
| 85 | + const struct led_dac_leds *led = &config->leds[i]; |
| 86 | + |
| 87 | + if (!device_is_ready(led->dac)) { |
| 88 | + return -ENODEV; |
| 89 | + } |
| 90 | + |
| 91 | + ret = dac_channel_setup(led->dac, &led->chan_cfg); |
| 92 | + if (ret != 0) { |
| 93 | + return ret; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | +#define LED_DAC_MAX_MV(n) DT_PROP(n, voltage_max_dac_mv) |
| 101 | +#define LED_DAC_MAX_VAL(n) (BIT(DT_PROP(n, resolution)) - 1) |
| 102 | + |
| 103 | +#define LED_DAC_MAX_BRIGHTNESS(n) \ |
| 104 | + COND_CODE_1(DT_NODE_HAS_PROP(n, voltage_max_brightness_mv), \ |
| 105 | + (DT_PROP(n, voltage_max_brightness_mv) * LED_DAC_MAX_VAL(n) / LED_DAC_MAX_MV(n)), \ |
| 106 | + (LED_DAC_MAX_VAL(n))) |
| 107 | + |
| 108 | +#define LED_DAC_MIN_BRIGHTNESS(n) \ |
| 109 | + COND_CODE_1(DT_NODE_HAS_PROP(n, voltage_min_brightness_mv), \ |
| 110 | + (DT_PROP(n, voltage_min_brightness_mv) * LED_DAC_MAX_VAL(n) / LED_DAC_MAX_MV(n)), \ |
| 111 | + (0)) |
| 112 | + |
| 113 | +#define LED_DAC_DT_GET(n) \ |
| 114 | + { \ |
| 115 | + .dac = DEVICE_DT_GET(DT_PHANDLE(n, dac_dev)), \ |
| 116 | + .chan_cfg = {.channel_id = DT_PROP(n, channel), \ |
| 117 | + .resolution = DT_PROP(n, resolution), \ |
| 118 | + .buffered = DT_PROP(n, output_buffer), \ |
| 119 | + .internal = false}, \ |
| 120 | + .dac_max = LED_DAC_MAX_BRIGHTNESS(n), .dac_min = LED_DAC_MIN_BRIGHTNESS(n) \ |
| 121 | + } |
| 122 | + |
| 123 | +#define LED_DAC_DEFINE(n) \ |
| 124 | + BUILD_ASSERT((DT_NODE_HAS_PROP(n, voltage_max_brightness_mv) || \ |
| 125 | + DT_NODE_HAS_PROP(n, voltage_min_brightness_mv)) == \ |
| 126 | + DT_NODE_HAS_PROP(n, voltage_max_dac_mv), \ |
| 127 | + "'voltage-max-dac-mv' must be set when 'voltage-max-brightness-mv' or " \ |
| 128 | + "'voltage-max-brightness-mv' is set"); \ |
| 129 | + \ |
| 130 | + static const struct led_dac_leds led_dac_##n[] = { \ |
| 131 | + DT_INST_FOREACH_CHILD_SEP(n, LED_DAC_DT_GET, (,))}; \ |
| 132 | + \ |
| 133 | + static const struct led_dac_config led_config_##n = { \ |
| 134 | + .leds = led_dac_##n, \ |
| 135 | + .num_leds = ARRAY_SIZE(led_dac_##n), \ |
| 136 | + }; \ |
| 137 | + \ |
| 138 | + DEVICE_DT_INST_DEFINE(n, &led_dac_init, NULL, NULL, &led_config_##n, POST_KERNEL, \ |
| 139 | + CONFIG_LED_INIT_PRIORITY, &led_dac_api); |
| 140 | + |
| 141 | +DT_INST_FOREACH_STATUS_OKAY(LED_DAC_DEFINE) |
0 commit comments