Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp337286pxf; Thu, 11 Mar 2021 05:15:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJw+eWuDr4e/u4ygegBtQA3xkQYqaRJh/4Lne0/k+yXvcz63foa4f22aENyYWho/5MDVx8m2 X-Received: by 2002:a17:906:ae96:: with SMTP id md22mr2904581ejb.409.1615468517510; Thu, 11 Mar 2021 05:15:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615468517; cv=none; d=google.com; s=arc-20160816; b=vsFYfuNW0xfxtkU9J/hPsOJT4NU3o81G8bKFgDaG+puA4hqcnUlbeFTtGWvUfmQ5L6 Cp6WManuQ2EES5tqD0NyH9J0OFCQ6bt1QLhnjBMeg2pxIAU4a0cFNM7Htk0KUU7Z8RlG 4zMMc1DOK8rExY2t3eZ4XAWshk/XTmu5lwW9qRBXwUAxZh9fLlGqJBGaDCN4el6mTndw F+pnewUSZsEdmDOZT44Ad2YXwjtizkjbzQ5nFZS2kvXhGWFDvF6RTkTQgpBzx0knABE1 5yIhqoYKUpbE9zeM2pn43Wkr/3yV6LteYB+eKeul9Y0hmEZDJJbF1q3NJrd6m7QJCww4 UIxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=PKxcbrdtFXzVaXcDXMV5bGrOudDtSg/m6UMUlPopE4I=; b=TZ+FLjZ5Fdgv3tSql8PI7KWY+cHf59C+BbBPuMfOI8DBsWRLOlvTIjNBX4ehVqZ+MY 1xDzQYkVgC2dWwGdk4sxSdSfXUQOBtP81cV8Iwio4Eq0h7urRckqwg1m2WQOg9jv4X7+ DbWr1DNElwX8Ko0xYMix84PkH2qVjWyxxH0wen32jXfvH2PuWmkcGazJKfYcEOcxooCX +80D4+bc61EDw79AZbZHI6yUokAuETLpm8OKzpyrPoRUMeaP5zbdwjuqeU3CTtH3+Wt7 7gu6rj2jroMf37kdH9KwNqasaTF4b4tTGbd4EkPakJOpe13TGNlJmVJoazzYTHgNr9ED sEXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@axis.com header.s=axis-central1 header.b=JZTk9isx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=axis.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x14si1572384edr.255.2021.03.11.05.14.54; Thu, 11 Mar 2021 05:15:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@axis.com header.s=axis-central1 header.b=JZTk9isx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=axis.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233416AbhCKNNS (ORCPT + 99 others); Thu, 11 Mar 2021 08:13:18 -0500 Received: from smtp1.axis.com ([195.60.68.17]:15038 "EHLO smtp1.axis.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233014AbhCKNMq (ORCPT ); Thu, 11 Mar 2021 08:12:46 -0500 X-Greylist: delayed 430 seconds by postgrey-1.27 at vger.kernel.org; Thu, 11 Mar 2021 08:12:45 EST DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axis.com; q=dns/txt; s=axis-central1; t=1615468366; x=1647004366; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=PKxcbrdtFXzVaXcDXMV5bGrOudDtSg/m6UMUlPopE4I=; b=JZTk9isx+RbsuR82+vEkmjWfYsBYbfbrYI7B/gw2t8OCDQ1zMAJ/El6j nCU+nuGESF3FJ+usQGRGVCHCJ3mZq+VtQ6XGMJvcaieyHZkuQ1ly9wubX Ar1LiD5FWw8TNTGCE8TZ5ufkfWHUOBqCiIxFmJ2GEYVpK4wI8iDhhMEz4 iybGfLfOdg9Z8yakqASIbxZtA0ywTztNQ/Q+Y7Dk/Berq0z68DzbgHD3s Y4WDrFYiOMV6pA2LbGv/6rYcRj4expTujcRFgGCXvEWh0XlrgH2269ERd rAq0mLOy+cLUCFD4JOltc/Y51bSwSLiWz+jywEi6pQM6B5I2WTzAiVkjO w==; From: Hermes Zhang To: Pavel Machek , Dan Murphy CC: , Hermes Zhang , , Subject: [PATCH] leds: leds-dual-gpio: Add dual GPIO LEDs driver Date: Thu, 11 Mar 2021 21:04:08 +0800 Message-ID: <20210311130408.10820-1-chenhui.zhang@axis.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hermes Zhang 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 + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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