From: Hermes Zhang <chenhuiz@xxxxxxxx> Introduce a new Dual GPIO LED driver. These two GPIOs LED will act as one LED as normal GPIO LED but give the possibility to change the intensity in four levels: OFF, LOW, MIDDLE and HIGH. --- drivers/leds/Kconfig | 9 +++ drivers/leds/Makefile | 1 + drivers/leds/leds-dual-gpio.c | 136 ++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 drivers/leds/leds-dual-gpio.c diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig index b6742b4231bf..bc374d3b40ef 100644 --- a/drivers/leds/Kconfig +++ b/drivers/leds/Kconfig @@ -370,6 +370,15 @@ config LEDS_GPIO defined as platform devices and/or OpenFirmware platform devices. The code to use these bindings can be selected below. +config LEDS_DUAL_GPIO + tristate "LED Support for Dual GPIO connected LEDs" + depends on LEDS_CLASS + depends on GPIOLIB || COMPILE_TEST + help + This option enables support for the two LEDs connected to GPIO + outputs. These two GPIO LEDs act as one LED in the sysfs and + perform different intensity by enable either one of them or both. + config LEDS_LP3944 tristate "LED Support for N.S. LP3944 (Fun Light) I2C chip" depends on LEDS_CLASS diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile index 2a698df9da57..10015cc81f79 100644 --- a/drivers/leds/Makefile +++ b/drivers/leds/Makefile @@ -30,6 +30,7 @@ obj-$(CONFIG_LEDS_DA903X) += leds-da903x.o obj-$(CONFIG_LEDS_DA9052) += leds-da9052.o obj-$(CONFIG_LEDS_FSG) += leds-fsg.o obj-$(CONFIG_LEDS_GPIO) += leds-gpio.o +obj-$(CONFIG_LEDS_DUAL_GPIO) += leds-dual-gpio.o obj-$(CONFIG_LEDS_GPIO_REGISTER) += leds-gpio-register.o obj-$(CONFIG_LEDS_HP6XX) += leds-hp6xx.o obj-$(CONFIG_LEDS_INTEL_SS4200) += leds-ss4200.o diff --git a/drivers/leds/leds-dual-gpio.c b/drivers/leds/leds-dual-gpio.c new file mode 100644 index 000000000000..5d3b9be46f4b --- /dev/null +++ b/drivers/leds/leds-dual-gpio.c @@ -0,0 +1,136 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * LEDs driver for GPIOs + * + * Copyright (C) 2021 Axis Communications AB + * Hermes Zhang <chenhui.zhang@xxxxxxxx> + */ + +#include <linux/err.h> +#include <linux/gpio.h> +#include <linux/gpio/consumer.h> +#include <linux/kernel.h> +#include <linux/leds.h> +#include <linux/module.h> +#include <linux/of.h> +#include <linux/platform_device.h> +#include <linux/property.h> +#include <linux/slab.h> + +#define GPIO_LOGICAL_ON 1 +#define GPIO_LOGICAL_OFF 0 + +struct gpio_dual_leds_priv { + struct gpio_desc *low_gpio; + struct gpio_desc *high_gpio; + struct led_classdev cdev; +}; + + +static void gpio_dual_led_set(struct led_classdev *led_cdev, + enum led_brightness value) +{ + struct gpio_dual_leds_priv *priv; + + priv = container_of(led_cdev, struct gpio_dual_leds_priv, cdev); + + if (value == LED_FULL) { + gpiod_set_value(priv->low_gpio, GPIO_LOGICAL_ON); + gpiod_set_value(priv->high_gpio, GPIO_LOGICAL_ON); + } else if (value < LED_FULL && value > LED_HALF) { + /* Enable high only */ + gpiod_set_value(priv->low_gpio, GPIO_LOGICAL_OFF); + gpiod_set_value(priv->high_gpio, GPIO_LOGICAL_ON); + } else if (value <= LED_HALF && value > LED_OFF) { + /* Enable low only */ + gpiod_set_value(priv->low_gpio, GPIO_LOGICAL_ON); + gpiod_set_value(priv->high_gpio, GPIO_LOGICAL_OFF); + } else { + gpiod_set_value(priv->low_gpio, GPIO_LOGICAL_OFF); + gpiod_set_value(priv->high_gpio, GPIO_LOGICAL_OFF); + } +} + +static int gpio_dual_led_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct device_node *node = dev->of_node; + struct gpio_dual_leds_priv *priv = NULL; + int ret; + const char *state; + + priv = devm_kzalloc(dev, sizeof(struct gpio_dual_leds_priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->low_gpio = devm_gpiod_get(dev, "low", GPIOD_OUT_LOW); + ret = PTR_ERR_OR_ZERO(priv->low_gpio); + if (ret) { + dev_err(dev, "cannot get low-gpios %d\n", ret); + return ret; + } + + priv->high_gpio = devm_gpiod_get(dev, "high", GPIOD_OUT_LOW); + ret = PTR_ERR_OR_ZERO(priv->high_gpio); + if (ret) { + dev_err(dev, "cannot get high-gpios %d\n", ret); + return ret; + } + + priv->cdev.name = of_get_property(node, "label", NULL); + priv->cdev.max_brightness = LED_FULL; + priv->cdev.default_trigger = + of_get_property(node, "linux,default-trigger", NULL); + priv->cdev.brightness_set = gpio_dual_led_set; + + ret = devm_led_classdev_register(dev, &priv->cdev); + if (ret < 0) + return ret; + + if (!of_property_read_string(node, "default-state", &state) + && !strcmp(state, "on")) + gpio_dual_led_set(&priv->cdev, LED_FULL); + else + gpio_dual_led_set(&priv->cdev, LED_OFF); + + platform_set_drvdata(pdev, priv); + + return 0; +} + +static void gpio_dual_led_shutdown(struct platform_device *pdev) +{ + struct gpio_dual_leds_priv *priv = platform_get_drvdata(pdev); + + gpio_dual_led_set(&priv->cdev, LED_OFF); +} + +static int gpio_dual_led_remove(struct platform_device *pdev) +{ + gpio_dual_led_shutdown(pdev); + + return 0; +} + +static const struct of_device_id of_gpio_dual_leds_match[] = { + { .compatible = "gpio-dual-leds", }, + {}, +}; + +MODULE_DEVICE_TABLE(of, of_gpio_dual_leds_match); + +static struct platform_driver gpio_dual_led_driver = { + .probe = gpio_dual_led_probe, + .remove = gpio_dual_led_remove, + .shutdown = gpio_dual_led_shutdown, + .driver = { + .name = "leds-dual-gpio", + .of_match_table = of_gpio_dual_leds_match, + }, +}; + +module_platform_driver(gpio_dual_led_driver); + +MODULE_DESCRIPTION("Dual GPIO LED driver"); +MODULE_LICENSE("GPL v2"); +MODULE_ALIAS("platform:leds-dual-gpio"); -- 2.20.1