Skip to contentSkip to conversation

gpiolib: fix hogs with multiple lines

PATCH Superseded Bartosz Golaszewski 3 participants 3 messages Mar 26 – Mar 27 GPIO

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 gpiolib: fix hogs with multiple lines

After moving GPIO hog handling into GPIOLIB core, we accidentally stopped supporting devicetree hog definitions with multiple lines like so:

	hog {
		gpio-hog;
		gpios = <3 0>, <4 GPIO_ACTIVE_LOW>;
		output-high;
		line-name = "foo";
	};

Restore this functionality to fix reported regressions.

---
Changes in v2:
- Use a default value of 2 for the number of GPIO cells
- Use kzalloc_objs() instead of kcalloc()
- Propagate the error number from fwnode_property_read_u32()
- Remove special cases forced by the lack of default cells value
- Link to v1: https://patch.msgid.link/[email protected]

---
 drivers/gpio/gpiolib.c | 92 +++++++++++++++++++++++++++++---------------------
 1 file changed, 54 insertions(+), 38 deletions(-)
drivers/gpio/gpiolib.c +54 -38
index 7ee45ccdf1265e94b68fe8fa0d10dbc6adccd93a..cc36f1d1f3edb2b5c8609ab5ba72b23e6db480bd 100644
@@ -938,12 +938,18 @@ int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode)
 	struct fwnode_handle *gc_node = dev_fwnode(&gc->gpiodev->dev);
 	struct fwnode_reference_args gpiospec;
 	enum gpiod_flags dflags;
+	const char *name = NULL;
 	struct gpio_desc *desc;
 	unsigned long lflags;
-	const char *name;
+	size_t num_hogs;
 	int ret, argc;
-	u32 gpios[3]; /* We support up to three-cell bindings. */
-	u32 cells;
+	/*
+	 * For devicetree-based systems, this needs to be defined in bindings
+	 * and there's no real default value. For other firmware descriptions
+	 * it makes the most sense to use 2 cells for the GPIO offset and
+	 * request flags.
+	 */
+	u32 cells = 2;
 
 	lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
 	dflags = GPIOD_ASIS;
@@ -952,42 +958,22 @@ int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode)
 	argc = fwnode_property_count_u32(fwnode, "gpios");
 	if (argc < 0)
 		return argc;
-	if (argc > 3)
-		return -EINVAL;
 
-	ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios, argc);
-	if (ret < 0)
+	ret = fwnode_property_read_u32(gc_node, "#gpio-cells", &cells);
+	if (ret && is_of_node(fwnode))
 		return ret;
+	if (!ret && (argc % cells))
+		return -EINVAL;
 
-	if (is_of_node(fwnode)) {
-		/*
-		 * OF-nodes need some additional special handling for
-		 * translating of devicetree flags.
-		 */
-		ret = fwnode_property_read_u32(gc_node, "#gpio-cells", &cells);
-		if (ret)
-			return ret;
-		if (!ret && argc != cells)
-			return -EINVAL;
-
-		memset(&gpiospec, 0, sizeof(gpiospec));
-		gpiospec.fwnode = fwnode;
-		gpiospec.nargs = argc;
+	num_hogs = argc / cells;
 
-		for (int i = 0; i < argc; i++)
-			gpiospec.args[i] = gpios[i];
+	u32 *gpios __free(kfree) = kzalloc_objs(*gpios, argc);
+	if (!gpios)
+		return -ENOMEM;
 
-		ret = of_gpiochip_get_lflags(gc, &gpiospec, &lflags);
-		if (ret)
-			return ret;
-	} else {
-		/*
-		 * GPIO_ACTIVE_LOW is currently the only lookup flag
-		 * supported for non-OF firmware nodes.
-		 */
-		if (gpios[1])
-			lflags |= GPIO_ACTIVE_LOW;
-	}
+	ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios, argc);
+	if (ret < 0)
+		return ret;
 
 	if (fwnode_property_present(fwnode, "input"))
 		dflags |= GPIOD_IN;
@@ -1000,11 +986,41 @@ int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode)
 
 	fwnode_property_read_string(fwnode, "line-name", &name);
 
-	desc = gpiochip_get_desc(gc, gpios[0]);
-	if (IS_ERR(desc))
-		return PTR_ERR(desc);
+	for (unsigned int i = 0; i < num_hogs; i++) {
+		if (is_of_node(fwnode)) {
+			/*
+			 * OF-nodes need some additional special handling for
+			 * translating of devicetree flags.
+			 */
+			memset(&gpiospec, 0, sizeof(gpiospec));
+			gpiospec.fwnode = fwnode;
+			gpiospec.nargs = cells;
+
+			for (int j = 0; j < cells; j++)
+				gpiospec.args[j] = gpios[i * cells + j];
+
+			ret = of_gpiochip_get_lflags(gc, &gpiospec, &lflags);
+			if (ret)
+				return ret;
+		} else {
+			/*
+			 * GPIO_ACTIVE_LOW is currently the only lookup flag
+			 * supported for non-OF firmware nodes.
+			 */
+			if (gpios[i * cells + 1])
+				lflags |= GPIO_ACTIVE_LOW;
+		}
+
+		desc = gpiochip_get_desc(gc, gpios[i * cells]);
+		if (IS_ERR(desc))
+			return PTR_ERR(desc);
 
-	return gpiod_hog(desc, name, lflags, dflags);
+		ret = gpiod_hog(desc, name, lflags, dflags);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
 }
 
 static int gpiochip_hog_lines(struct gpio_chip *gc)

---
base-commit: 880e76d276421113e3017d1b950bf932dc4e3ce3
change-id: 20260325-gpio-hogs-multiple-1718d38c1f5b

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Bartosz Golaszewski

After moving GPIO hog handling into GPIOLIB core, we accidentally stopped supporting devicetree hog definitions with multiple lines like so:

        hog {
                gpio-hog;
                gpios = <3 0>, <4 GPIO_ACTIVE_LOW>;
                output-high;
                line-name = "foo";
        };
Quoted 7 lines

Restore this functionality to fix reported regressions.

Oups, nice catch!

Yours,
Linus Walleij

#2 lore raw
In reply to Bartosz Golaszewski

Hi Bartosz,

Quoted 17 lines · Bartosz Golaszewski

After moving GPIO hog handling into GPIOLIB core, we accidentally stopped supporting devicetree hog definitions with multiple lines like so:

        hog {
                gpio-hog;
                gpios = <3 0>, <4 GPIO_ACTIVE_LOW>;
                output-high;
                line-name = "foo";
        };

Restore this functionality to fix reported regressions.

Quoted 7 lines

---
Changes in v2:
- Use a default value of 2 for the number of GPIO cells
- Use kzalloc_objs() instead of kcalloc()
- Propagate the error number from fwnode_property_read_u32()
- Remove special cases forced by the lack of default cells value
- Link to v1: https://patch.msgid.link/[email protected]

Thanks for the update!

drivers/gpio/gpiolib.c int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode) · line 944

Quoted 11 lines
drivers/gpio/gpiolib.c +2 -1
@@ -938,12 +938,18 @@ int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode)
        struct fwnode_handle *gc_node = dev_fwnode(&gc->gpiodev->dev);
        struct fwnode_reference_args gpiospec;
        enum gpiod_flags dflags;
+       const char *name = NULL;
        struct gpio_desc *desc;
        unsigned long lflags;
-       const char *name;
+       size_t num_hogs;

unsigned int should be sufficient to store the quotient of two positive 32-bit numbers.

Quoted 16 lines
drivers/gpio/gpiolib.c +7 -2
        int ret, argc;
-       u32 gpios[3]; /* We support up to three-cell bindings. */
-       u32 cells;
+       /*
+        * For devicetree-based systems, this needs to be defined in bindings
+        * and there's no real default value. For other firmware descriptions
+        * it makes the most sense to use 2 cells for the GPIO offset and
+        * request flags.
+        */
+       u32 cells = 2;
 
        lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
        dflags = GPIOD_ASIS;
@@ -952,42 +958,22 @@ int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode)
        argc = fwnode_property_count_u32(fwnode, "gpios");
        if (argc < 0)

drivers/gpio/gpiolib.c int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode) · line 965

Quoted 10 lines
drivers/gpio/gpiolib.c +3 -4
                return argc;
-       if (argc > 3)
-               return -EINVAL;
 
-       ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios, argc);
-       if (ret < 0)
+       ret = fwnode_property_read_u32(gc_node, "#gpio-cells", &cells);
+       if (ret && is_of_node(fwnode))
                return ret;
+       if (!ret && (argc % cells))

No need to check for !ret, the %-test should be valid for non-DT, too.

I guess we can't have #gpio-cells = <0> (single-GPIO provider not supporting flags), and it would be very difficult to hog it.

Quoted 49 lines
drivers/gpio/gpiolib.c +9 -30
+               return -EINVAL;
 
-       if (is_of_node(fwnode)) {
-               /*
-                * OF-nodes need some additional special handling for
-                * translating of devicetree flags.
-                */
-               ret = fwnode_property_read_u32(gc_node, "#gpio-cells", &cells);
-               if (ret)
-                       return ret;
-               if (!ret && argc != cells)
-                       return -EINVAL;
-
-               memset(&gpiospec, 0, sizeof(gpiospec));
-               gpiospec.fwnode = fwnode;
-               gpiospec.nargs = argc;
+       num_hogs = argc / cells;
 
-               for (int i = 0; i < argc; i++)
-                       gpiospec.args[i] = gpios[i];
+       u32 *gpios __free(kfree) = kzalloc_objs(*gpios, argc);
+       if (!gpios)
+               return -ENOMEM;
 
-               ret = of_gpiochip_get_lflags(gc, &gpiospec, &lflags);
-               if (ret)
-                       return ret;
-       } else {
-               /*
-                * GPIO_ACTIVE_LOW is currently the only lookup flag
-                * supported for non-OF firmware nodes.
-                */
-               if (gpios[1])
-                       lflags |= GPIO_ACTIVE_LOW;
-       }
+       ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios, argc);
+       if (ret < 0)
+               return ret;
 
        if (fwnode_property_present(fwnode, "input"))
                dflags |= GPIOD_IN;
@@ -1000,11 +986,41 @@ int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode)
 
        fwnode_property_read_string(fwnode, "line-name", &name);
 
-       desc = gpiochip_get_desc(gc, gpios[0]);
-       if (IS_ERR(desc))
-               return PTR_ERR(desc);
+       for (unsigned int i = 0; i < num_hogs; i++) {

drivers/gpio/gpiolib.c int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode) · line 999

Quoted 10 lines
drivers/gpio/gpiolib.c +10 -0
+               if (is_of_node(fwnode)) {
+                       /*
+                        * OF-nodes need some additional special handling for
+                        * translating of devicetree flags.
+                        */
+                       memset(&gpiospec, 0, sizeof(gpiospec));
+                       gpiospec.fwnode = fwnode;
+                       gpiospec.nargs = cells;
+
+                       for (int j = 0; j < cells; j++)

unsigned int

Quoted 18 lines
drivers/gpio/gpiolib.c +17 -0
+                               gpiospec.args[j] = gpios[i * cells + j];
+
+                       ret = of_gpiochip_get_lflags(gc, &gpiospec, &lflags);
+                       if (ret)
+                               return ret;
+               } else {
+                       /*
+                        * GPIO_ACTIVE_LOW is currently the only lookup flag
+                        * supported for non-OF firmware nodes.
+                        */
+                       if (gpios[i * cells + 1])
+                               lflags |= GPIO_ACTIVE_LOW;
+               }
+
+               desc = gpiochip_get_desc(gc, gpios[i * cells]);
+               if (IS_ERR(desc))
+                       return PTR_ERR(desc);
 

drivers/gpio/gpiolib.c int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode) · line 1026

Quoted 10 lines
drivers/gpio/gpiolib.c +6 -1
-       return gpiod_hog(desc, name, lflags, dflags);
+               ret = gpiod_hog(desc, name, lflags, dflags);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
 }
 
 static int gpiochip_hog_lines(struct gpio_chip *gc)

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds