Skip to contentSkip to conversation

Rewrite GPIO LED trigger to use trigger-sources

PATCH Applied Linus Walleij 3 participants 7 messages Sep 26, 2023 – Oct 2, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover Rewrite GPIO LED trigger to use trigger-sources

This rewrites the platform-data GPIO LED trigger to instead use fwnode trigger-sources to describe the LED used.

This will work out-of-the-box with e.g. device tree.

Tested with real hardware by modifying a device tree adding trigger-sources to a LED and trigger-source-cells to a gpio chip, setting the trigger to a pushbutton. It works like a charm, once the trigger is set to "gpio".

Adding trigger-sources to GPIO chips in a simple way requires this patch to be merged to the generic dtschema:
https://lore.kernel.org/linux-devicetree/[email protected]/

---
Changes in v2:
- Add a quirk to gpiolib-of to allow to read out trigger-sources as
any regular GPIO.
- Fix a use-after-free bug found by Dan Carpenter.
- Tested on hardware.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Linus Walleij (3):
gpiolib: of: Allow "trigger-sources" to reference a GPIO
dt-bindings: leds: Mention GPIO triggers
leds: triggers: gpio: Rewrite to use trigger-sources

 Documentation/devicetree/bindings/leds/common.yaml |   2 +
 drivers/gpio/gpiolib-of.c                          |  28 +++++
 drivers/leds/trigger/Kconfig                       |   5 +-
 drivers/leds/trigger/ledtrig-gpio.c                | 137 ++++++---------------
 4 files changed, 71 insertions(+), 101 deletions(-)

---
base-commit: 8eb1c4d9b44873d30efc1846148944534f4a017d
change-id: 20230911-gpio-led-trigger-dt-922bbe21fa22

Best regards,

Signature · 1 line
Linus Walleij <[email protected]>
#1 lore raw

PATCH v2 1/3 gpiolib: of: Allow "trigger-sources" to reference a GPIO

The "trigger-sources" phandle used for LED triggers are special:
the DT bindings mandate that such triggers have the same phandle references no matter what the trigger is. A GPIO is just another kind of device that can trigger a LED.

---
 drivers/gpio/gpiolib-of.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/gpio/gpiolib-of.c +28 -0
index 531faabead0f..f4a660bf11fd 100644
@@ -611,6 +611,33 @@ static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np,
 	return desc;
 }
 
+/*
+ * Trigger sources are special, they allow us to use any GPIO as a LED trigger
+ * and have the name "trigger-sources" no matter which kind of phandle it is
+ * pointing to, whether to a GPIO, a USB host, a network PHY etc. So in this case
+ * we allow looking something up that is not named "foo-gpios".
+ */
+static struct gpio_desc *of_find_trigger_gpio(struct device_node *np,
+					      const char *con_id,
+					      unsigned int idx,
+					      enum of_gpio_flags *of_flags)
+{
+	struct gpio_desc *desc;
+
+	if (!IS_ENABLED(CONFIG_LEDS_TRIGGER_GPIO))
+		return ERR_PTR(-ENOENT);
+
+	if (!con_id || strcmp(con_id, "trigger-sources"))
+		return ERR_PTR(-ENOENT);
+
+	desc = of_get_named_gpiod_flags(np, con_id, idx, of_flags);
+	if (!gpiod_not_found(desc))
+		pr_debug("%s is used as a trigger\n", of_node_full_name(np));
+
+	return desc;
+}
+
+
 typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
 						const char *con_id,
 						unsigned int idx,
@@ -618,6 +645,7 @@ typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
 static const of_find_gpio_quirk of_find_gpio_quirks[] = {
 	of_find_gpio_rename,
 	of_find_mt2701_gpio,
+	of_find_trigger_gpio,
 	NULL
 };
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 1/3] gpiolib: of: Allow "trigger-sources" to reference a GPIO

Quoted 6 lines · Linus Walleij

The "trigger-sources" phandle used for LED triggers are special:
the DT bindings mandate that such triggers have the same phandle references no matter what the trigger is. A GPIO is just another kind of device that can trigger a LED.

Bartosz could you queue this patch? Lee already queued patches 2+3, and there is just runtime dependency with no in-tree users so it's fine to merge them in the different trees.

Sorry for missing to CC you directly on the set :/

Yours,
Linus Walleij

#3 lore raw

Re: [PATCH v2 1/3] gpiolib: of: Allow "trigger-sources" to reference a GPIO

In reply to Linus Walleij
Quoted 59 lines · Linus Walleij

The "trigger-sources" phandle used for LED triggers are special:
the DT bindings mandate that such triggers have the same phandle references no matter what the trigger is. A GPIO is just another kind of device that can trigger a LED.

---
 drivers/gpio/gpiolib-of.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/gpio/gpiolib-of.c +28 -1
index 531faabead0f..f4a660bf11fd 100644
@@ -611,6 +611,33 @@ static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np,
        return desc;
 }
 
+/*
+ * Trigger sources are special, they allow us to use any GPIO as a LED trigger
+ * and have the name "trigger-sources" no matter which kind of phandle it is
+ * pointing to, whether to a GPIO, a USB host, a network PHY etc. So in this case
+ * we allow looking something up that is not named "foo-gpios".
+ */
+static struct gpio_desc *of_find_trigger_gpio(struct device_node *np,
+                                             const char *con_id,
+                                             unsigned int idx,
+                                             enum of_gpio_flags *of_flags)
+{
+       struct gpio_desc *desc;
+
+       if (!IS_ENABLED(CONFIG_LEDS_TRIGGER_GPIO))
+               return ERR_PTR(-ENOENT);
+
+       if (!con_id || strcmp(con_id, "trigger-sources"))
+               return ERR_PTR(-ENOENT);
+
+       desc = of_get_named_gpiod_flags(np, con_id, idx, of_flags);
+       if (!gpiod_not_found(desc))
+               pr_debug("%s is used as a trigger\n", of_node_full_name(np));
+
+       return desc;
+}
+
+
 typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
                                                const char *con_id,
                                                unsigned int idx,
@@ -618,6 +645,7 @@ typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
 static const of_find_gpio_quirk of_find_gpio_quirks[] = {
        of_find_gpio_rename,
        of_find_mt2701_gpio,
+       of_find_trigger_gpio,
        NULL
 };
 
 
--
Quoted 2 lines

2.34.1

Queued, thanks!

Bart

#4 lore raw

PATCH v2 2/3 dt-bindings: leds: Mention GPIO triggers

In reply to Linus Walleij

We reuse the trigger-sources phandle to just point to GPIOs we may want to use as LED triggers.

Example:

gpio: gpio@0 {
    compatible "my-gpio";
    gpio-controller;
    #gpio-cells = <2>;
    interrupt-controller;
    #interrupt-cells = <2>;
    #trigger-source-cells = <2>;
};
leds {
    compatible = "gpio-leds";
    led-my-gpio {
        label = "device:blue:myled";
        gpios = <&gpio 0 GPIO_ACTIVE_HIGH>;
        default-state = "off";
        linux,default-trigger = "gpio";
        trigger-sources = <&gpio 1 GPIO_ACTIVE_HIGH>;
    };
};
---
 Documentation/devicetree/bindings/leds/common.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/leds/common.yaml +2 -0
index 5fb7007f3618..b42950643b9d 100644
@@ -191,6 +191,8 @@ properties:
       each of them having its own LED assigned (assuming they are not
       hardwired). In such cases this property should contain phandle(s) of
       related source device(s).
+      Another example is a GPIO line that will be monitored and mirror the
+      state of the line (with or without inversion flags) to the LED.
       In many cases LED can be related to more than one device (e.g. one USB LED
       vs. multiple USB ports). Each source should be represented by a node in
       the device tree and be referenced by a phandle and a set of phandle
Signature · 1 line
2.34.1
#5 lore raw

PATCH v2 3/3 leds: triggers: gpio: Rewrite to use trigger-sources

In reply to Linus Walleij

By providing a GPIO line as "trigger-sources" in the FWNODE (such as from the device tree) and combining with the GPIO trigger, we can support a GPIO LED trigger in a natural way from the hardware description instead of using the custom sysfs and deprecated global GPIO numberspace.

Example:

gpio: gpio@0 {
    compatible "my-gpio";
    gpio-controller;
    #gpio-cells = <2>;
    interrupt-controller;
    #interrupt-cells = <2>;
    #trigger-source-cells = <2>;
};
leds {
    compatible = "gpio-leds";
    led-my-gpio {
        label = "device:blue:myled";
        gpios = <&gpio 0 GPIO_ACTIVE_HIGH>;
        default-state = "off";
        linux,default-trigger = "gpio";
        trigger-sources = <&gpio 1 GPIO_ACTIVE_HIGH>;
    };
};

Make this the norm, unmark the driver as broken.

Delete the sysfs handling of GPIOs.

Since GPIO descriptors inherently can describe inversion, the inversion handling can just be deleted.

---
ChangeLog v1->v2:
- Fix a use-after-free bug found by Dan Carpenter

---
 drivers/leds/trigger/Kconfig        |   5 +-
 drivers/leds/trigger/ledtrig-gpio.c | 137 +++++++++++-------------------------
 2 files changed, 41 insertions(+), 101 deletions(-)
drivers/leds/trigger/Kconfig +1 -4
index 2a57328eca20..d11d80176fc0 100644
@@ -83,13 +83,10 @@ config LEDS_TRIGGER_ACTIVITY
 config LEDS_TRIGGER_GPIO
 	tristate "LED GPIO Trigger"
 	depends on GPIOLIB || COMPILE_TEST
-	depends on BROKEN
 	help
 	  This allows LEDs to be controlled by gpio events. It's good
 	  when using gpios as switches and triggering the needed LEDs
-	  from there. One use case is n810's keypad LEDs that could
-	  be triggered by this trigger when user slides up to show
-	  keypad.
+	  from there. Triggers are defined as device properties.
 
 	  If unsure, say N.
 
drivers/leds/trigger/ledtrig-gpio.c +40 -97

189 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer Applied
#6 lore raw

Re: (subset) [PATCH v2 0/3] Rewrite GPIO LED trigger to use trigger-sources

In reply to Linus Walleij
Quoted 11 lines · Linus Walleij

This rewrites the platform-data GPIO LED trigger to instead use fwnode trigger-sources to describe the LED used.

This will work out-of-the-box with e.g. device tree.

Tested with real hardware by modifying a device tree adding trigger-sources to a LED and trigger-source-cells to a gpio chip, setting the trigger to a pushbutton. It works like a charm, once the trigger is set to "gpio".

[...]

Applied, thanks!

[2/3] dt-bindings: leds: Mention GPIO triggers

      commit: 4aa583f1a6b85c88e3278d2461797d592ac61fcc
[3/3] leds: triggers: gpio: Rewrite to use trigger-sources
      commit: 1222f5dbc7723cc78741343da2e414b80de83c8f

--
Lee Jones [李琼斯]