linux-leds.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hermes Zhang <chenhui.zhang@axis.com>
To: Pavel Machek <pavel@ucw.cz>, Dan Murphy <dmurphy@ti.com>
Cc: <kernel@axis.com>, Hermes Zhang <chenhuiz@axis.com>,
	<linux-kernel@vger.kernel.org>, <linux-leds@vger.kernel.org>
Subject: [PATCH] leds: leds-dual-gpio: Add dual GPIO LEDs driver
Date: Thu, 11 Mar 2021 21:04:08 +0800	[thread overview]
Message-ID: <20210311130408.10820-1-chenhui.zhang@axis.com> (raw)

From: Hermes Zhang <chenhuiz@axis.com>

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@axis.com>
+ */
+
+#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


             reply	other threads:[~2021-03-11 13:13 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-11 13:04 Hermes Zhang [this message]
2021-03-11 15:38 ` [PATCH] leds: leds-dual-gpio: Add dual GPIO LEDs driver Marek Behun
2021-03-11 15:39   ` Marek Behun
2021-03-12  4:48     ` Hermes Zhang
2021-03-12  5:59       ` Marek Behun
2021-03-11 17:56 ` Pavel Machek
2021-03-11 18:02 ` Pavel Machek
2021-03-18  2:11   ` Hermes Zhang
2021-03-12  8:31 ` Alexander Dahl
2021-03-12  8:48   ` Hermes Zhang
2021-03-12  9:03     ` Marek Behun

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210311130408.10820-1-chenhui.zhang@axis.com \
    --to=chenhui.zhang@axis.com \
    --cc=chenhuiz@axis.com \
    --cc=dmurphy@ti.com \
    --cc=kernel@axis.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=pavel@ucw.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).