From 7bc99e246ca0f518e56e08ef5b0aec198dba688a Mon Sep 17 00:00:00 2001 From: Andy Sinclair Date: Wed, 24 May 2023 08:41:25 +0100 Subject: [PATCH] drivers: led: npm1300: nPM1300 LED driver Added LED driver for nPM1300 PMIC Signed-off-by: Andy Sinclair --- drivers/led/CMakeLists.txt | 1 + drivers/led/Kconfig | 1 + drivers/led/Kconfig.npm1300 | 11 +++ drivers/led/led_npm1300.c | 100 +++++++++++++++++++++++ dts/bindings/led/nordic,npm1300-led.yaml | 39 +++++++++ 5 files changed, 152 insertions(+) create mode 100644 drivers/led/Kconfig.npm1300 create mode 100644 drivers/led/led_npm1300.c create mode 100644 dts/bindings/led/nordic,npm1300-led.yaml diff --git a/drivers/led/CMakeLists.txt b/drivers/led/CMakeLists.txt index 018a2f362d8..b38ecbf5841 100644 --- a/drivers/led/CMakeLists.txt +++ b/drivers/led/CMakeLists.txt @@ -7,6 +7,7 @@ zephyr_library() zephyr_library_sources_ifdef(CONFIG_HT16K33 ht16k33.c) zephyr_library_sources_ifdef(CONFIG_IS31FL3216A is31fl3216a.c) zephyr_library_sources_ifdef(CONFIG_LED_GPIO led_gpio.c) +zephyr_library_sources_ifdef(CONFIG_LED_NPM1300 led_npm1300.c) zephyr_library_sources_ifdef(CONFIG_LED_PWM led_pwm.c) zephyr_library_sources_ifdef(CONFIG_LED_XEC led_mchp_xec.c) zephyr_library_sources_ifdef(CONFIG_LP3943 lp3943.c) diff --git a/drivers/led/Kconfig b/drivers/led/Kconfig index 293d6347946..91cb28544b9 100644 --- a/drivers/led/Kconfig +++ b/drivers/led/Kconfig @@ -33,6 +33,7 @@ source "drivers/led/Kconfig.lp3943" source "drivers/led/Kconfig.lp503x" source "drivers/led/Kconfig.lp5562" source "drivers/led/Kconfig.lp5569" +source "drivers/led/Kconfig.npm1300" source "drivers/led/Kconfig.pca9633" source "drivers/led/Kconfig.pwm" source "drivers/led/Kconfig.tlc59108" diff --git a/drivers/led/Kconfig.npm1300 b/drivers/led/Kconfig.npm1300 new file mode 100644 index 00000000000..e5180aa1dd1 --- /dev/null +++ b/drivers/led/Kconfig.npm1300 @@ -0,0 +1,11 @@ +# Copyright (c) 2023 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +config LED_NPM1300 + bool "nPM1300 LED driver" + default y + depends on DT_HAS_NORDIC_NPM1300_LED_ENABLED + select I2C + select MFD + help + Enable the nPM1300 LED driver. diff --git a/drivers/led/led_npm1300.c b/drivers/led/led_npm1300.c new file mode 100644 index 00000000000..528700630e9 --- /dev/null +++ b/drivers/led/led_npm1300.c @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * SPDX-License-Identifier: Apache-2.0 + */ + +#define DT_DRV_COMPAT nordic_npm1300_led + +#include + +#include +#include +#include + +/* nPM1300 LED base address */ +#define NPM_LED_BASE 0x0AU + +/* nPM1300 LED register offsets */ +#define NPM_LED_OFFSET_MODE 0x00U +#define NPM_LED_OFFSET_SET 0x03U +#define NPM_LED_OFFSET_CLR 0x04U + +/* nPM1300 Channel counts */ +#define NPM1300_LED_PINS 3U + +/* nPM1300 LED modes */ +#define NPM_LED_HOST 2U + +struct led_npm1300_config { + const struct device *mfd; + uint8_t mode[NPM1300_LED_PINS]; +}; + +static int led_npm1300_on(const struct device *dev, uint32_t led) +{ + const struct led_npm1300_config *config = dev->config; + + if (led >= NPM1300_LED_PINS) { + return -EINVAL; + } + + if (config->mode[led] != NPM_LED_HOST) { + return -EPERM; + } + + return mfd_npm1300_reg_write(config->mfd, NPM_LED_BASE, NPM_LED_OFFSET_SET + (led * 2U), + 1U); +} + +static int led_npm1300_off(const struct device *dev, uint32_t led) +{ + const struct led_npm1300_config *config = dev->config; + + if (led >= NPM1300_LED_PINS) { + return -EINVAL; + } + + if (config->mode[led] != NPM_LED_HOST) { + return -EPERM; + } + + return mfd_npm1300_reg_write(config->mfd, NPM_LED_BASE, NPM_LED_OFFSET_CLR + (led * 2U), + 1U); +} + +static const struct led_driver_api led_npm1300_api = { + .on = led_npm1300_on, + .off = led_npm1300_off, +}; + +static int led_npm1300_init(const struct device *dev) +{ + const struct led_npm1300_config *config = dev->config; + + if (!device_is_ready(config->mfd)) { + return -ENODEV; + } + + for (uint8_t led = 0U; led < NPM1300_LED_PINS; led++) { + int ret = mfd_npm1300_reg_write(config->mfd, NPM_LED_BASE, + NPM_LED_OFFSET_MODE + led, config->mode[led]); + + if (ret < 0) { + return ret; + } + } + + return 0; +} + +#define LED_NPM1300_DEFINE(n) \ + static const struct led_npm1300_config led_npm1300_config##n = { \ + .mfd = DEVICE_DT_GET(DT_INST_PARENT(n)), \ + .mode = {DT_INST_ENUM_IDX(n, nordic_led0_mode), \ + DT_INST_ENUM_IDX(n, nordic_led1_mode), \ + DT_INST_ENUM_IDX(n, nordic_led2_mode)}}; \ + \ + DEVICE_DT_INST_DEFINE(n, &led_npm1300_init, NULL, NULL, &led_npm1300_config##n, \ + POST_KERNEL, CONFIG_LED_INIT_PRIORITY, &led_npm1300_api); + +DT_INST_FOREACH_STATUS_OKAY(LED_NPM1300_DEFINE) diff --git a/dts/bindings/led/nordic,npm1300-led.yaml b/dts/bindings/led/nordic,npm1300-led.yaml new file mode 100644 index 00000000000..90c420832f9 --- /dev/null +++ b/dts/bindings/led/nordic,npm1300-led.yaml @@ -0,0 +1,39 @@ +# Copyright (c) 2023 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +description: | + Nordic nPM1300 LED controller + + The nPM1300 has three LED outputs. + Each LED can automatically display error or charging status, + or be controlled by software. + +compatible: "nordic,npm1300-led" + +properties: + nordic,led0-mode: + type: string + required: true + enum: + - error + - charging + - host + description: LED 0 mode + + nordic,led1-mode: + type: string + required: true + enum: + - error + - charging + - host + description: LED 1 mode + + nordic,led2-mode: + type: string + required: true + enum: + - error + - charging + - host + description: LED 2 mode