|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Titouan Christophe |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT adafruit_seesaw_neopixel |
| 8 | + |
| 9 | +#include <zephyr/drivers/led.h> |
| 10 | +#include <zephyr/drivers/mfd/mfd_seesaw.h> |
| 11 | +#include <zephyr/dt-bindings/led/led.h> |
| 12 | + |
| 13 | +enum seesaw_mod_neopixel { |
| 14 | + NEOPIXEL_PIN = 0x01, |
| 15 | + NEOPIXEL_SPEED = 0x02, |
| 16 | + NEOPIXEL_BUF_LENGTH = 0x03, |
| 17 | + NEOPIXEL_BUF = 0x04, |
| 18 | + NEOPIXEL_SHOW = 0x05, |
| 19 | +}; |
| 20 | + |
| 21 | +struct seesaw_neopixel_config { |
| 22 | + const struct device *seesaw; |
| 23 | + size_t length; |
| 24 | + const uint8_t *color_mapping; |
| 25 | + uint8_t num_colors; |
| 26 | + uint8_t output_pin; |
| 27 | +}; |
| 28 | + |
| 29 | +static int seesaw_neopixel_set_color(const struct device *dev, uint32_t led, uint8_t num_colors, |
| 30 | + const uint8_t *color) |
| 31 | +{ |
| 32 | + int ret; |
| 33 | + const struct seesaw_neopixel_config *config = dev->config; |
| 34 | + uint16_t offset = config->num_colors * led; |
| 35 | + uint8_t buf[6] = {offset >> 8, offset & 0xff}; |
| 36 | + |
| 37 | + if (led >= config->length || num_colors != config->num_colors) { |
| 38 | + return -EINVAL; |
| 39 | + } |
| 40 | + |
| 41 | + memcpy(buf + 2, color, num_colors); |
| 42 | + |
| 43 | + ret = seesaw_write(config->seesaw, SEESAW_MOD_NEOPIXEL, NEOPIXEL_BUF, buf, 2 + num_colors); |
| 44 | + if (ret) { |
| 45 | + return ret; |
| 46 | + } |
| 47 | + |
| 48 | + return seesaw_cmd(config->seesaw, SEESAW_MOD_NEOPIXEL, NEOPIXEL_SHOW); |
| 49 | +} |
| 50 | + |
| 51 | +static int seesaw_neopixel_set_brightness(const struct device *dev, uint32_t led, uint8_t value) |
| 52 | +{ |
| 53 | + const struct seesaw_neopixel_config *config = dev->config; |
| 54 | + uint8_t b = 255 * value / 100; |
| 55 | + uint8_t color[] = {b, b, b, b}; |
| 56 | + |
| 57 | + if (value > 100) { |
| 58 | + return -EINVAL; |
| 59 | + } |
| 60 | + |
| 61 | + return seesaw_neopixel_set_color(dev, led, config->num_colors, color); |
| 62 | +} |
| 63 | + |
| 64 | +static int seesaw_neopixel_on(const struct device *dev, uint32_t led) |
| 65 | +{ |
| 66 | + return seesaw_neopixel_set_brightness(dev, led, 100); |
| 67 | +} |
| 68 | + |
| 69 | +static int seesaw_neopixel_off(const struct device *dev, uint32_t led) |
| 70 | +{ |
| 71 | + return seesaw_neopixel_set_brightness(dev, led, 0); |
| 72 | +} |
| 73 | + |
| 74 | +static int seesaw_neopixel_init(const struct device *dev) |
| 75 | +{ |
| 76 | + const struct seesaw_neopixel_config *config = dev->config; |
| 77 | + int ret = seesaw_claim_module(config->seesaw, SEESAW_MOD_NEOPIXEL, dev); |
| 78 | + |
| 79 | + if (ret) { |
| 80 | + return ret; |
| 81 | + } |
| 82 | + |
| 83 | + seesaw_write(config->seesaw, SEESAW_MOD_NEOPIXEL, NEOPIXEL_PIN, &config->output_pin, 1); |
| 84 | + seesaw_write_uint16(config->seesaw, SEESAW_MOD_NEOPIXEL, |
| 85 | + NEOPIXEL_BUF_LENGTH, 3 * config->length); |
| 86 | + |
| 87 | + return 0; |
| 88 | +} |
| 89 | + |
| 90 | +static DEVICE_API(led, seesaw_neopixel_api) = { |
| 91 | + .on = seesaw_neopixel_on, |
| 92 | + .off = seesaw_neopixel_off, |
| 93 | + .set_brightness = seesaw_neopixel_set_brightness, |
| 94 | + .set_color = seesaw_neopixel_set_color, |
| 95 | +}; |
| 96 | + |
| 97 | +#define SEESAW_NEOPIXEL_INIT(inst) \ |
| 98 | + static const uint8_t cmap_##idx[] = DT_INST_PROP(inst, color_mapping); \ |
| 99 | + static const struct seesaw_neopixel_config config_##inst = { \ |
| 100 | + .seesaw = DEVICE_DT_GET(DT_INST_PARENT(inst)), \ |
| 101 | + .length = DT_INST_PROP(inst, chain_length), \ |
| 102 | + .color_mapping = cmap_##idx, \ |
| 103 | + .num_colors = sizeof(cmap_##idx), \ |
| 104 | + .output_pin = DT_INST_PROP(inst, output_pin), \ |
| 105 | + }; \ |
| 106 | + DEVICE_DT_INST_DEFINE(inst, seesaw_neopixel_init, NULL, NULL, &config_##inst, POST_KERNEL, \ |
| 107 | + CONFIG_LED_INIT_PRIORITY, &seesaw_neopixel_api); |
| 108 | + |
| 109 | +DT_INST_FOREACH_STATUS_OKAY(SEESAW_NEOPIXEL_INIT) |
0 commit comments