diff --git a/drivers/led/CMakeLists.txt b/drivers/led/CMakeLists.txt index 14a26c601aa..73a808d7d5a 100644 --- a/drivers/led/CMakeLists.txt +++ b/drivers/led/CMakeLists.txt @@ -19,6 +19,7 @@ zephyr_library_sources_ifdef(CONFIG_LP3943 lp3943.c) zephyr_library_sources_ifdef(CONFIG_LP50XX lp50xx.c) zephyr_library_sources_ifdef(CONFIG_LP5562 lp5562.c) zephyr_library_sources_ifdef(CONFIG_LP5569 lp5569.c) +zephyr_library_sources_ifdef(CONFIG_MODULINO_BUTTONS_LEDS modulino_buttons_leds.c) zephyr_library_sources_ifdef(CONFIG_NCP5623 ncp5623.c) zephyr_library_sources_ifdef(CONFIG_PCA9633 pca9633.c) zephyr_library_sources_ifdef(CONFIG_TLC59108 tlc59108.c) diff --git a/drivers/led/Kconfig b/drivers/led/Kconfig index 720106576af..bac815242c3 100644 --- a/drivers/led/Kconfig +++ b/drivers/led/Kconfig @@ -38,6 +38,7 @@ source "drivers/led/Kconfig.lp3943" source "drivers/led/Kconfig.lp50xx" source "drivers/led/Kconfig.lp5562" source "drivers/led/Kconfig.lp5569" +source "drivers/led/Kconfig.modulino" source "drivers/led/Kconfig.ncp5623" source "drivers/led/Kconfig.npm1300" source "drivers/led/Kconfig.pca9633" diff --git a/drivers/led/Kconfig.modulino b/drivers/led/Kconfig.modulino new file mode 100644 index 00000000000..c496e85c7d7 --- /dev/null +++ b/drivers/led/Kconfig.modulino @@ -0,0 +1,10 @@ +# Copyright (c) 2025 Google, LLC +# SPDX-License-Identifier: Apache-2.0 + +config MODULINO_BUTTONS_LEDS + bool "Arduino Modulino buttons LEDs" + default y + depends on DT_HAS_ARDUINO_MODULINO_BUTTONS_LEDS_ENABLED + select I2C + help + Enable driver Arduino Modulino Buttons LEDs. diff --git a/drivers/led/modulino_buttons_leds.c b/drivers/led/modulino_buttons_leds.c new file mode 100644 index 00000000000..488443f507c --- /dev/null +++ b/drivers/led/modulino_buttons_leds.c @@ -0,0 +1,107 @@ +/* + * Copyright 2025 Google LLC + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define DT_DRV_COMPAT arduino_modulino_buttons_leds + +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(modulino_buttons_leds, CONFIG_LED_LOG_LEVEL); + +#define MODULINO_BUTTONS_NUM_LEDS 3 + +struct modulino_buttons_leds_config { + struct i2c_dt_spec bus; +}; + +struct modulino_buttons_leds_data { + uint8_t buf[MODULINO_BUTTONS_NUM_LEDS]; +}; + +static int modulino_buttons_leds_set(const struct device *dev, + uint32_t led, bool value) +{ + const struct modulino_buttons_leds_config *cfg = dev->config; + struct modulino_buttons_leds_data *data = dev->data; + int ret; + + if (led >= MODULINO_BUTTONS_NUM_LEDS) { + return -EINVAL; + } + + data->buf[led] = value ? 1 : 0; + + ret = i2c_write_dt(&cfg->bus, data->buf, sizeof(data->buf)); + if (ret < 0) { + LOG_ERR("i2c write error: %d", ret); + return ret; + } + + return 0; +} + +static int modulino_buttons_leds_on(const struct device *dev, uint32_t led) +{ + return modulino_buttons_leds_set(dev, led, true); +} + +static int modulino_buttons_leds_off(const struct device *dev, uint32_t led) +{ + return modulino_buttons_leds_set(dev, led, false); +} + +static int modulino_buttons_leds_set_brightness(const struct device *dev, + uint32_t led, uint8_t value) +{ + return modulino_buttons_leds_set(dev, led, value > 0); +} + +static int modulino_buttons_leds_init(const struct device *dev) +{ + const struct modulino_buttons_leds_config *cfg = dev->config; + struct modulino_buttons_leds_data *data = dev->data; + int ret; + + if (!i2c_is_ready_dt(&cfg->bus)) { + LOG_ERR("Bus device is not ready"); + return -ENODEV; + } + + /* Reset to all LEDs off */ + ret = i2c_write_dt(&cfg->bus, data->buf, sizeof(data->buf)); + if (ret < 0) { + LOG_ERR("i2c write error: %d", ret); + return ret; + } + + return 0; +} + +static DEVICE_API(led, modulino_buttons_leds_api) = { + .on = modulino_buttons_leds_on, + .off = modulino_buttons_leds_off, + .set_brightness = modulino_buttons_leds_set_brightness, +}; + +#define MODULINO_BUTTONS_INIT(inst) \ + static const struct modulino_buttons_leds_config \ + modulino_buttons_leds_cfg_##inst = { \ + .bus = I2C_DT_SPEC_GET(DT_INST_PARENT(inst)), \ + }; \ + \ + static struct modulino_buttons_leds_data modulino_buttons_leds_data_##inst; \ + \ + DEVICE_DT_INST_DEFINE(inst, modulino_buttons_leds_init, NULL, \ + &modulino_buttons_leds_data_##inst, \ + &modulino_buttons_leds_cfg_##inst, \ + POST_KERNEL, CONFIG_LED_INIT_PRIORITY, \ + &modulino_buttons_leds_api); + + +DT_INST_FOREACH_STATUS_OKAY(MODULINO_BUTTONS_INIT) diff --git a/dts/bindings/led/arduino,modulino-buttons-leds.yaml b/dts/bindings/led/arduino,modulino-buttons-leds.yaml new file mode 100644 index 00000000000..ed3510eb78d --- /dev/null +++ b/dts/bindings/led/arduino,modulino-buttons-leds.yaml @@ -0,0 +1,6 @@ +# Copyright (c) 2025 Google, LLC +# SPDX-License-Identifier: Apache-2.0 + +description: Arduino Modulino buttons LEDs + +compatible: "arduino,modulino-buttons-leds"