Skip to contentSkip to conversation

New multiple GPIOs LED driver

PATCH Superseded Hermes Zhang 5 participants 9 messages Mar 26, 2021 – Mar 29, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover New multiple GPIOs LED driver

From: Hermes Zhang <[email protected]>

changes v2:
- use max_brightness(=2^gpio number) instead of LED_FULL
- alloc priv at once
- move driver code to new simple folder
- update commit message for dt-binding commit
- move dt-binding commit before driver code

Hermes Zhang (2):
dt-binding: leds: Document leds-multi-gpio bindings
leds: leds-multi-gpio: Add multiple GPIOs LED driver

 .../bindings/leds/leds-multi-gpio.yaml        |  50 ++++++
 drivers/leds/Kconfig                          |   3 +
 drivers/leds/Makefile                         |   3 +
 drivers/leds/simple/Kconfig                   |  23 +++
 drivers/leds/simple/Makefile                  |   3 +
 drivers/leds/simple/leds-multi-gpio.c         | 144 ++++++++++++++++++
 6 files changed, 226 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/leds-multi-gpio.yaml
 create mode 100644 drivers/leds/simple/Kconfig
 create mode 100644 drivers/leds/simple/Makefile
 create mode 100644 drivers/leds/simple/leds-multi-gpio.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH v2 1/2 dt-binding: leds: Document leds-multi-gpio bindings

From: Hermes Zhang <[email protected]>

This binding represents LED devices which are controller with multiple GPIO lines in order to achieve more than two brightness states.

---
 .../bindings/leds/leds-multi-gpio.yaml        | 50 +++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/leds-multi-gpio.yaml
Documentation/devicetree/bindings/leds/leds-multi-gpio.yaml new file +50 -0
new file mode 100644
index 000000000000..1549f21e8d6e
@@ -0,0 +1,50 @@
+# SPDX-License-Identifier: GPL-2.0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/leds-multi-gpio.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Multiple GPIOs LED driver
+
+maintainers:
+  - Hermes Zhang <[email protected]>
+
+description:
+  This will support some LED made of multiple GPIOs and the brightness of the
+  LED could map to different states of the GPIOs.
+
+properties:
+  compatible:
+    const: multi-gpio-led
+
+  led-gpios:
+    description: Array of one or more GPIOs pins used to control the LED.
+    minItems: 1
+    maxItems: 8  # Should be enough
+
+  led-states:
+    description: |
+      The array list the supported states here which will map to brightness
+      from 0 to maximum. Each item in the array will present all the GPIOs
+      value by bit.
+    $ref: /schemas/types.yaml#/definitions/uint8-array
+    minItems: 1
+    maxItems: 256 # Should be enough
+
+required:
+  - compatible
+  - led-gpios
+  - led-states
+
+additionalProperties: false
+
+examples:
+  - |
+    gpios-led {
+      compatible = "multi-gpio-led";
+
+      led-gpios = <&gpio0 23 0x1>,
+                  <&gpio0 24 0x1>;
+      led-states = /bits/ 8 <0x00 0x01 0x02 0x03>;
+    };
+...
Signature · 1 line
2.20.1
maintainer
#2 lore raw

Re: [PATCH v2 1/2] dt-binding: leds: Document leds-multi-gpio bindings

Quoted 40 lines · Hermes Zhang

From: Hermes Zhang <[email protected]>

This binding represents LED devices which are controller with multiple GPIO lines in order to achieve more than two brightness states.

---
 .../bindings/leds/leds-multi-gpio.yaml        | 50 +++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/leds-multi-gpio.yaml
Documentation/devicetree/bindings/leds/leds-multi-gpio.yaml new file +22 -0
new file mode 100644
index 000000000000..1549f21e8d6e
@@ -0,0 +1,50 @@
+# SPDX-License-Identifier: GPL-2.0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/leds/leds-multi-gpio.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Multiple GPIOs LED driver
+
+maintainers:
+  - Hermes Zhang <[email protected]>
+
+description:
+  This will support some LED made of multiple GPIOs and the brightness of the
+  LED could map to different states of the GPIOs.
+
+properties:
+  compatible:
+    const: multi-gpio-led
+
+  led-gpios:
+    description: Array of one or more GPIOs pins used to control the LED.
+    minItems: 1

Documentation/devicetree/bindings/leds/leds-multi-gpio.yaml line 32

Quoted 10 lines
Documentation/devicetree/bindings/leds/leds-multi-gpio.yaml new file +10 -0
+    maxItems: 8  # Should be enough
+
+  led-states:
+    description: |
+      The array list the supported states here which will map to brightness
+      from 0 to maximum. Each item in the array will present all the GPIOs
+      value by bit.
+    $ref: /schemas/types.yaml#/definitions/uint8-array
+    minItems: 1
+    maxItems: 256 # Should be enough

Isn't this the same as the standard 'brightness-levels' from backlight binding? The index is the level and the value is the h/w specific setting.

Quoted 21 lines
Documentation/devicetree/bindings/leds/leds-multi-gpio.yaml new file +18 -0
+
+required:
+  - compatible
+  - led-gpios
+  - led-states
+
+additionalProperties: false
+
+examples:
+  - |
+    gpios-led {
+      compatible = "multi-gpio-led";
+
+      led-gpios = <&gpio0 23 0x1>,
+                  <&gpio0 24 0x1>;
+      led-states = /bits/ 8 <0x00 0x01 0x02 0x03>;
+    };
+...
Signature · 1 line
2.20.1
#3 lore raw

Re: [PATCH v2 1/2] dt-binding: leds: Document leds-multi-gpio bindings

Quoted 9 lines · Rob Herring
Quoted 9 lines
Documentation/devicetree/bindings/leds/leds-multi-gpio.yaml new file +9 -0
+
+  led-gpios:
+    description: Array of one or more GPIOs pins used to control the LED.
+    minItems: 1
+    maxItems: 8  # Should be enough
+
+  led-states:
+    description: |
+      The array list the supported states here which will map to brightness
Quoted 8 lines

Documentation/devicetree/bindings/leds/leds-multi-gpio.yaml line 32

Quoted 5 lines
Documentation/devicetree/bindings/leds/leds-multi-gpio.yaml new file +5 -0
+      from 0 to maximum. Each item in the array will present all the GPIOs
+      value by bit.
+    $ref: /schemas/types.yaml#/definitions/uint8-array
+    minItems: 1
+    maxItems: 256 # Should be enough

Isn't this the same as the standard 'brightness-levels' from backlight binding? The index is the level and the value is the h/w specific setting.

Yes, it seems same.

Best Regards,

Hermes

#4 lore raw

PATCH v2 2/2 leds: leds-multi-gpio: Add multiple GPIOs LED driver

In reply to Hermes Zhang

From: Hermes Zhang <[email protected]>

Introduce a new multiple GPIOs LED driver. This LED will made of multiple GPIOs (up to 8) and will map different brightness to different GPIOs states which defined in dts file.

---
 drivers/leds/Kconfig                  |   3 +
 drivers/leds/Makefile                 |   3 +
 drivers/leds/simple/Kconfig           |  23 ++++
 drivers/leds/simple/Makefile          |   3 +
 drivers/leds/simple/leds-multi-gpio.c | 144 ++++++++++++++++++++++++++
 5 files changed, 176 insertions(+)
 create mode 100644 drivers/leds/simple/Kconfig
 create mode 100644 drivers/leds/simple/Makefile
 create mode 100644 drivers/leds/simple/leds-multi-gpio.c
drivers/leds/Kconfig +3 -0
index b6742b4231bf..f95217b2fcf1 100644
@@ -937,4 +937,7 @@ source "drivers/leds/trigger/Kconfig"
 comment "LED Blink"
 source "drivers/leds/blink/Kconfig"
 
+comment "LED Simple"
+source "drivers/leds/simple/Kconfig"
+
 endif # NEW_LEDS
drivers/leds/Makefile +3 -0
index 2a698df9da57..26917d93fa03 100644
@@ -111,3 +111,6 @@ obj-$(CONFIG_LEDS_TRIGGERS)		+= trigger/
 
 # LED Blink
 obj-$(CONFIG_LEDS_BLINK)                += blink/
+
+# LED Blink
+obj-$(CONFIG_LEDS_SIMPLE)		+= simple/
drivers/leds/simple/Kconfig new file +23 -0
new file mode 100644
index 000000000000..7aef82701f86
@@ -0,0 +1,23 @@
+menuconfig LEDS_SIMPLE
+	bool "Simple LED support"
+	depends on LEDS_CLASS
+	help
+	  This option enables simple leds support for the leds class.
+	  If unsure, say Y.
+
+if LEDS_SIMPLE
+
+config LEDS_MULTI_GPIO
+	tristate "LED Support for multiple GPIOs LED"
+	depends on LEDS_CLASS
+	depends on GPIOLIB
+	depends on OF
+	help
+	  This option enables support for a multiple GPIOs LED. Such LED is made
+	  of multiple GPIOs and could change the brightness by setting different
+	  states of the GPIOs.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called leds-multi-gpio.
+
+endif # LEDS_SIMPLE
drivers/leds/simple/Makefile new file +3 -0
new file mode 100644
index 000000000000..2ba655fdc175
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_LEDS_MULTI_GPIO)	+= leds-multi-gpio.o
drivers/leds/simple/leds-multi-gpio.c new file +144 -0

144 lines not on this page · raw message

Signature · 1 line
2.20.1
#5 lore raw

Re: [PATCH v2 2/2] leds: leds-multi-gpio: Add multiple GPIOs LED driver

Hello Hermes,

great to see your improved series. See below for my remarks.

Quoted 35 lines · Hermes Zhang

From: Hermes Zhang <[email protected]>

Introduce a new multiple GPIOs LED driver. This LED will made of multiple GPIOs (up to 8) and will map different brightness to different GPIOs states which defined in dts file.

---
 drivers/leds/Kconfig                  |   3 +
 drivers/leds/Makefile                 |   3 +
 drivers/leds/simple/Kconfig           |  23 ++++
 drivers/leds/simple/Makefile          |   3 +
 drivers/leds/simple/leds-multi-gpio.c | 144 ++++++++++++++++++++++++++
 5 files changed, 176 insertions(+)
 create mode 100644 drivers/leds/simple/Kconfig
 create mode 100644 drivers/leds/simple/Makefile
 create mode 100644 drivers/leds/simple/leds-multi-gpio.c
drivers/leds/Kconfig +3 -0
index b6742b4231bf..f95217b2fcf1 100644
@@ -937,4 +937,7 @@ source "drivers/leds/trigger/Kconfig"
 comment "LED Blink"
 source "drivers/leds/blink/Kconfig"
 
+comment "LED Simple"
+source "drivers/leds/simple/Kconfig"
+
 endif # NEW_LEDS
drivers/leds/Makefile +0 -0
index 2a698df9da57..26917d93fa03 100644

drivers/leds/Makefile obj-$(CONFIG_LEDS_TRIGGERS) += trigger/ · line 116

Quoted 6 lines
drivers/leds/Makefile +3 -0
@@ -111,3 +111,6 @@ obj-$(CONFIG_LEDS_TRIGGERS)		+= trigger/
 
 # LED Blink
 obj-$(CONFIG_LEDS_BLINK)                += blink/
+
+# LED Blink
+obj-$(CONFIG_LEDS_SIMPLE)		+= simple/

That comment should read "LED Simple", right?

Quoted 19 lines
drivers/leds/simple/Kconfig new file +13 -0
new file mode 100644
index 000000000000..7aef82701f86
@@ -0,0 +1,23 @@
+menuconfig LEDS_SIMPLE
+	bool "Simple LED support"
+	depends on LEDS_CLASS
+	help
+	  This option enables simple leds support for the leds class.
+	  If unsure, say Y.
+
+if LEDS_SIMPLE
+
+config LEDS_MULTI_GPIO
+	tristate "LED Support for multiple GPIOs LED"
+	depends on LEDS_CLASS
+	depends on GPIOLIB

drivers/leds/simple/Kconfig line 23

Quoted 10 lines
drivers/leds/simple/Kconfig new file +10 -0
+	depends on OF
+	help
+	  This option enables support for a multiple GPIOs LED. Such LED is made
+	  of multiple GPIOs and could change the brightness by setting different
+	  states of the GPIOs.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called leds-multi-gpio.
+
+endif # LEDS_SIMPLE

I'm wondering why this is part of this driver? I suppose there's no such folder yet, although Pavel suggested. Would it make sense to put that Kconfig menu entry and folder creation to a separate commit?

Quoted 149 lines
drivers/leds/simple/Makefile new file +3 -0
new file mode 100644
index 000000000000..2ba655fdc175
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_LEDS_MULTI_GPIO)	+= leds-multi-gpio.o
drivers/leds/simple/leds-multi-gpio.c new file +134 -0
new file mode 100644
index 000000000000..14fdaa5a2aac
@@ -0,0 +1,144 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2021 Axis Communications AB
+ */
+
+#include <linux/leds.h>
+#include <linux/gpio/consumer.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+
+
+#define MAX_GPIO_NUM  8
+
+struct multi_gpio_led_priv {
+	struct led_classdev cdev;
+
+	struct gpio_descs *gpios;
+
+	u16 nr_states;
+
+	u8 states[0];
+};
+
+
+static void multi_gpio_led_set(struct led_classdev *led_cdev,
+	enum led_brightness value)
+{
+	struct multi_gpio_led_priv *priv;
+	int idx;
+
+	DECLARE_BITMAP(values, MAX_GPIO_NUM);
+
+	priv = container_of(led_cdev, struct multi_gpio_led_priv, cdev);
+
+	idx = value > led_cdev->max_brightness ? led_cdev->max_brightness : value;
+
+	values[0] = priv->states[idx];
+
+	gpiod_set_array_value(priv->gpios->ndescs, priv->gpios->desc,
+	    priv->gpios->info, values);
+}
+
+static int multi_gpio_led_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *node = dev->of_node;
+	struct multi_gpio_led_priv *priv = NULL;
+	int ret;
+	const char *state = NULL;
+	struct led_init_data init_data = {};
+	struct gpio_descs *gpios;
+	u16 nr_states;
+
+	gpios = devm_gpiod_get_array(dev, "led", GPIOD_OUT_LOW);
+	if (IS_ERR(gpios))
+		return PTR_ERR(gpios);
+
+	if (gpios->ndescs >= MAX_GPIO_NUM) {
+		dev_err(dev, "Too many GPIOs\n");
+		return -EINVAL;
+	}
+
+	ret = of_property_count_u8_elems(node, "led-states");
+	if (ret < 0)
+		return ret;
+
+	if (ret != 1 << gpios->ndescs) {
+		dev_err(dev, "led-states number should equal to 2^led-gpios\n");
+		return -EINVAL;
+	}
+
+	nr_states = ret;
+
+	priv = devm_kzalloc(dev, sizeof(struct multi_gpio_led_priv)
+			+ sizeof(u8) * nr_states , GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	ret = of_property_read_u8_array(node, "led-states", priv->states, nr_states);
+	if (ret)
+		return ret;
+
+	priv->gpios = gpios;
+	priv->nr_states = nr_states;
+
+	priv->cdev.max_brightness = nr_states;
+	priv->cdev.default_trigger = of_get_property(node, "linux,default-trigger", NULL);
+	priv->cdev.brightness_set = multi_gpio_led_set;
+
+	init_data.fwnode = of_fwnode_handle(node);
+
+	ret = devm_led_classdev_register_ext(dev, &priv->cdev, &init_data);
+	if (ret < 0)
+		return ret;
+
+	of_property_read_string(node, "default-state", &state);
+	if (!strcmp(state, "on"))
+		multi_gpio_led_set(&priv->cdev, priv->cdev.max_brightness);
+	else
+		multi_gpio_led_set(&priv->cdev, 0);
+
+	platform_set_drvdata(pdev, priv);
+
+	return 0;
+}
+
+static void multi_gpio_led_shutdown(struct platform_device *pdev)
+{
+	struct multi_gpio_led_priv *priv = platform_get_drvdata(pdev);
+
+	multi_gpio_led_set(&priv->cdev, 0);
+}
+
+static int multi_gpio_led_remove(struct platform_device *pdev)
+{
+	multi_gpio_led_shutdown(pdev);
+
+	return 0;
+}
+
+static const struct of_device_id of_multi_gpio_led_match[] = {
+	{ .compatible = "multi-gpio-led", },
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, of_multi_gpio_led_match);
+
+static struct platform_driver multi_gpio_led_driver = {
+	.probe		= multi_gpio_led_probe,
+	.remove		= multi_gpio_led_remove,
+	.shutdown	= multi_gpio_led_shutdown,
+	.driver		= {
+		.name	= "multi-gpio-led",

drivers/leds/simple/leds-multi-gpio.c line 144

Quoted 10 lines
drivers/leds/simple/leds-multi-gpio.c new file +10 -0
+		.of_match_table = of_multi_gpio_led_match,
+	},
+};
+
+module_platform_driver(multi_gpio_led_driver);
+
+MODULE_AUTHOR("Hermes Zhang <[email protected]>");
+MODULE_DESCRIPTION("Multiple GPIOs LED driver");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:leds-multi-gpio");

I did not review thouroughly, but in my mail the indentation looks wrong. Did checkpatch complain?

Greets
Alex

Signature · 4 lines
/"\ ASCII RIBBON | »With the first link, the chain is forged. The first
\ / CAMPAIGN     | speech censured, the first thought forbidden, the
 X  AGAINST      | first freedom denied, chains us all irrevocably.«
/ \ HTML MAIL    | (Jean-Luc Picard, quoting Judge Aaron Satie)
#6 lore raw

Re: [PATCH v2 2/2] leds: leds-multi-gpio: Add multiple GPIOs LED driver

Quoted 9 lines · Alexander Dahl

drivers/leds/simple/leds-multi-gpio.c line 144

Quoted 7 lines
drivers/leds/simple/leds-multi-gpio.c new file +7 -0
+
+module_platform_driver(multi_gpio_led_driver);
+
+MODULE_AUTHOR("Hermes Zhang <[email protected]>");
+MODULE_DESCRIPTION("Multiple GPIOs LED driver");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:leds-multi-gpio");

I did not review thouroughly, but in my mail the indentation looks wrong. Did checkpatch complain?

Sorry, I forgot to check the style before commit, but seems one problem about extra space:

$ chkernel
ERROR: space prohibited before that ',' (ctx:WxW)
#164: FILE: drivers/leds/simple/leds-multi-gpio.c:76:
+            + sizeof(u8) * nr_states , GFP_KERNEL);
                                                   ^

I will fix it in next commit.

Best Regards,

Hermes

#7 lore raw

Re: [PATCH v2 2/2] leds: leds-multi-gpio: Add multiple GPIOs LED driver

In reply to Hermes Zhang

Hi!

Quoted 11 lines

Introduce a new multiple GPIOs LED driver. This LED will made of multiple GPIOs (up to 8) and will map different brightness to different GPIOs states which defined in dts file.

index 000000000000..7aef82701f86

drivers/leds/simple/Kconfig line 6

Quoted 6 lines
drivers/leds/simple/Kconfig new file +6 -0
@@ -0,0 +1,23 @@
+menuconfig LEDS_SIMPLE
+	bool "Simple LED support"
+	depends on LEDS_CLASS
+	help
+	  This option enables simple leds support for the leds class.
+	  If unsure, say Y.

No need for new config symbol.

drivers/leds/simple/Kconfig line 7

Quoted 1 line
drivers/leds/simple/Kconfig new file +1 -0
+	if (ret != 1 << gpios->ndescs) {

I'd do (ret != (1 << gpios->ndescs))

drivers/leds/simple/Kconfig line 9

Quoted 2 lines
drivers/leds/simple/Kconfig new file +2 -0
+	priv = devm_kzalloc(dev, sizeof(struct multi_gpio_led_priv)
+			+ sizeof(u8) * nr_states , GFP_KERNEL);

Sizeof(u8) is always 1, no need for space before , .

drivers/leds/simple/Kconfig line 14

Quoted 5 lines
drivers/leds/simple/Kconfig new file +5 -0
+	of_property_read_string(node, "default-state", &state);
+	if (!strcmp(state, "on"))
+		multi_gpio_led_set(&priv->cdev, priv->cdev.max_brightness);
+	else
+		multi_gpio_led_set(&priv->cdev, 0);

No need for default-state handling, unless you are using it.

Best regards,
Pavel

Signature · 1 line
http://www.livejournal.com/~pavelmachek
#8 lore raw

Re: [PATCH v2 2/2] leds: leds-multi-gpio: Add multiple GPIOs LED driver

Quoted 7 lines · Pavel Machek

drivers/leds/simple/leds-multi-gpio.c line 101

Quoted 5 lines
drivers/leds/simple/leds-multi-gpio.c new file +5 -0
+	of_property_read_string(node, "default-state", &state);
+	if (!strcmp(state, "on"))
+		multi_gpio_led_set(&priv->cdev, priv->cdev.max_brightness);
+	else
+		multi_gpio_led_set(&priv->cdev, 0);

No need for default-state handling, unless you are using it.

We will use it, to make the LED default on or off.

Best Regards,

Hermes