|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT arduino_modulino_buttons_leds |
| 8 | + |
| 9 | +#include <zephyr/device.h> |
| 10 | +#include <zephyr/drivers/i2c.h> |
| 11 | +#include <zephyr/drivers/led.h> |
| 12 | +#include <zephyr/kernel.h> |
| 13 | +#include <zephyr/logging/log.h> |
| 14 | + |
| 15 | +LOG_MODULE_REGISTER(modulino_buttons_leds, CONFIG_LED_LOG_LEVEL); |
| 16 | + |
| 17 | +#define MODULINO_BUTTONS_NUM_LEDS 3 |
| 18 | + |
| 19 | +struct modulino_buttons_leds_config { |
| 20 | + struct i2c_dt_spec bus; |
| 21 | +}; |
| 22 | + |
| 23 | +struct modulino_buttons_leds_data { |
| 24 | + uint8_t buf[MODULINO_BUTTONS_NUM_LEDS]; |
| 25 | +}; |
| 26 | + |
| 27 | +static int modulino_buttons_leds_set(const struct device *dev, |
| 28 | + uint32_t led, bool value) |
| 29 | +{ |
| 30 | + const struct modulino_buttons_leds_config *cfg = dev->config; |
| 31 | + struct modulino_buttons_leds_data *data = dev->data; |
| 32 | + int ret; |
| 33 | + |
| 34 | + if (led >= MODULINO_BUTTONS_NUM_LEDS) { |
| 35 | + return -EINVAL; |
| 36 | + } |
| 37 | + |
| 38 | + data->buf[led] = value ? 1 : 0; |
| 39 | + |
| 40 | + ret = i2c_write_dt(&cfg->bus, data->buf, sizeof(data->buf)); |
| 41 | + if (ret < 0) { |
| 42 | + LOG_ERR("i2c write error: %d", ret); |
| 43 | + return ret; |
| 44 | + } |
| 45 | + |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +static int modulino_buttons_leds_on(const struct device *dev, uint32_t led) |
| 50 | +{ |
| 51 | + return modulino_buttons_leds_set(dev, led, true); |
| 52 | +} |
| 53 | + |
| 54 | +static int modulino_buttons_leds_off(const struct device *dev, uint32_t led) |
| 55 | +{ |
| 56 | + return modulino_buttons_leds_set(dev, led, false); |
| 57 | +} |
| 58 | + |
| 59 | +static int modulino_buttons_leds_set_brightness(const struct device *dev, |
| 60 | + uint32_t led, uint8_t value) |
| 61 | +{ |
| 62 | + return modulino_buttons_leds_set(dev, led, value > 0); |
| 63 | +} |
| 64 | + |
| 65 | +static int modulino_buttons_leds_init(const struct device *dev) |
| 66 | +{ |
| 67 | + const struct modulino_buttons_leds_config *cfg = dev->config; |
| 68 | + struct modulino_buttons_leds_data *data = dev->data; |
| 69 | + int ret; |
| 70 | + |
| 71 | + if (!i2c_is_ready_dt(&cfg->bus)) { |
| 72 | + LOG_ERR("Bus device is not ready"); |
| 73 | + return -ENODEV; |
| 74 | + } |
| 75 | + |
| 76 | + /* Reset to all LEDs off */ |
| 77 | + ret = i2c_write_dt(&cfg->bus, data->buf, sizeof(data->buf)); |
| 78 | + if (ret < 0) { |
| 79 | + LOG_ERR("i2c write error: %d", ret); |
| 80 | + return ret; |
| 81 | + } |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +static DEVICE_API(led, modulino_buttons_leds_api) = { |
| 87 | + .on = modulino_buttons_leds_on, |
| 88 | + .off = modulino_buttons_leds_off, |
| 89 | + .set_brightness = modulino_buttons_leds_set_brightness, |
| 90 | +}; |
| 91 | + |
| 92 | +#define MODULINO_BUTTONS_INIT(inst) \ |
| 93 | + static const struct modulino_buttons_leds_config \ |
| 94 | + modulino_buttons_leds_cfg_##inst = { \ |
| 95 | + .bus = I2C_DT_SPEC_GET(DT_INST_PARENT(inst)), \ |
| 96 | + }; \ |
| 97 | + \ |
| 98 | + static struct modulino_buttons_leds_data modulino_buttons_leds_data_##inst; \ |
| 99 | + \ |
| 100 | + DEVICE_DT_INST_DEFINE(inst, modulino_buttons_leds_init, NULL, \ |
| 101 | + &modulino_buttons_leds_data_##inst, \ |
| 102 | + &modulino_buttons_leds_cfg_##inst, \ |
| 103 | + POST_KERNEL, CONFIG_LED_INIT_PRIORITY, \ |
| 104 | + &modulino_buttons_leds_api); |
| 105 | + |
| 106 | + |
| 107 | +DT_INST_FOREACH_STATUS_OKAY(MODULINO_BUTTONS_INIT) |
0 commit comments