Skip to contentSkip to conversation

gpio: improve claim diagnostics and fix gpio-delay offset handling

PATCH Superseded Pranav Sanwal 2 participants 11 messages Jul 29 – Aug 11 GPIO

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover gpio: improve claim diagnostics and fix gpio-delay offset handling

This series improves diagnosability of GPIO offset collisions and fixes three related bugs in the gpio-delay driver that were uncovered while debugging such a collision.

Patch 1 logs, at dev_dbg() level, which label already holds a GPIO offset when a new request collides with it, from within dm_gpio_request() itself, so a collision can be root-caused from the uclass layer when actively debugging.

Patch 2 adds the same diagnostic to the "gpio" command, at printf() level. -EBUSY from gpio_request() is a normal, expected result there (a pin already owned by a driver or hog), so the command already tolerates it silently; this patch only prints which label holds the pin on that already-tolerated path.

Patch 3 fixes gpio_delay_probe()/gpio_delay_xlate() to size and validate against the driver's actual GPIO count (from its "gpios" property) instead of a hardcoded 32, correctly sizing the claimed/name tracking arrays and matching the pattern used by the generic uclass xlate implementation.

Patch 4 fixes gpio_delay_xlate() to propagate the requested index into desc->offset. Previously every consumer's descriptor came back with offset 0 regardless of which index was requested, so on boards where gpio-delay wraps more than one real GPIO line, every consumer past the first silently collided with the first consumer's already-claimed offset 0 and was never toggled. This was the root cause behind the series; the other patches are diagnostic and robustness fixes found along the way.

Patch 5 adds a .remove hook to gpio-delay to release the real GPIOs it wraps. The driver requests them via gpio_request_by_name_nodev(), which strdup()s a label into the wrapped device's own uc_priv->name[]; with no .remove, those requests and labels were never released on teardown.

Patches 3-5 are tagged Fixes: c866f2f197e2 ("gpio: Add GPIO delay driver").

Changes in v2:
- Patch 1: downgraded from dev_warn() to dev_dbg(), since -EBUSY is a normal/expected result for some callers (e.g. cmd/gpio.c, and sandbox tests that deliberately exercise this path), and warning there fired on legitimate operations, not just misconfiguration (Simon)
- New patch 2: moved the user-facing diagnostic into the "gpio" command instead, where -EBUSY is already known to be expected (Simon)
- New patch 5: additional related fix found while further exercising the driver

Pranav Sanwal (5):
gpio: uclass: log when a gpio offset is already claimed
gpio: cmd: warn when a gpio offset is already claimed
gpio: delay: set gpio_count and validate index against it
gpio: delay: fix offset propagation in gpio_delay_xlate()
gpio: delay: free wrapped real GPIOs on remove

 cmd/gpio.c                 | 11 +++++++++--
 drivers/gpio/gpio-delay.c  | 23 ++++++++++++++++++++++-
 drivers/gpio/gpio-uclass.c |  5 ++++-
 3 files changed, 35 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.7
#1 lore raw

PATCH v2 1/5 gpio: uclass: log when a gpio offset is already claimed

dm_gpio_request() returns -EBUSY when the requested offset is already claimed, but gives no indication of which label already holds it. Any caller going through the uclass API directly, rather than the "gpio" command, has no way to see this at all.

Log it at dev_dbg() level: silent by default, available when actively debugging with this device's debug output enabled.

---

Changes in v2:
- Downgraded dev_warn() to dev_dbg(); moved the user-facing warning to patch 2/5 instead. (Simon)

 drivers/gpio/gpio-uclass.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpio/gpio-uclass.c +4 -1
index 7559b8dc7e2..9451b7db4af 100644
@@ -391,8 +391,11 @@ int dm_gpio_request(struct gpio_desc *desc, const char *label)
 	int ret;
 
 	uc_priv = dev_get_uclass_priv(dev);
-	if (gpio_is_claimed(uc_priv, desc->offset))
+	if (gpio_is_claimed(uc_priv, desc->offset)) {
+		dev_dbg(dev, "gpio offset %u already claimed by '%s', requested label '%s'\n",
+			desc->offset, uc_priv->name[desc->offset], label);
 		return -EBUSY;
+	}
 	str = strdup(label);
 	if (!str)
 		return -ENOMEM;
Signature · 1 line
2.43.7
Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCH v2 1/5] gpio: uclass: log when a gpio offset is already claimed

Quoted 7 lines · Pranav Sanwal

gpio: uclass: log when a gpio offset is already claimed

dm_gpio_request() returns -EBUSY when the requested offset is already claimed, but gives no indication of which label already holds it. Any caller going through the uclass API directly, rather than the "gpio" command, has no way to see this at all.

Quoted 7 lines

Log it at dev_dbg() level: silent by default, available when actively debugging with this device's debug output enabled.

drivers/gpio/gpio-uclass.c | 5 ++++-

 1 file changed, 4 insertions(+), 1 deletion(-)
#3 lore raw

PATCH v2 2/5 gpio: cmd: warn when a gpio offset is already claimed

In reply to Pranav Sanwal

The "gpio" command silently swallows -EBUSY from gpio_request(), since a pin already claimed by a driver or hog is a normal, expected condition for "gpio set"/"gpio toggle" etc. This makes an unexpected offset collision (e.g. two consumers unintentionally routed to the same underlying offset) opaque to debug from the command line.

Print which label already holds the pin when this happens, using the device/offset already resolved by gpio_lookup_name().

---

Changes in v2:
- New patch, split out of patch 1/5 at Simon's suggestion.

 cmd/gpio.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
cmd/gpio.c +9 -2
index 7a43dc6ab18..6992ed0e79a 100644
@@ -137,6 +137,8 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
 {
 	unsigned int gpio;
 	enum gpio_cmd sub_cmd;
+	struct udevice *dev;
+	unsigned int offset;
 	int value;
 	const char *str_cmd, *str_gpio = NULL;
 #ifdef CONFIG_CMD_GPIO_READ
@@ -217,7 +219,7 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
 	 * code here to use the GPIO uclass interface instead of the numbered
 	 * GPIO compatibility layer.
 	 */
-	ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
+	ret = gpio_lookup_name(str_gpio, &dev, &offset, &gpio);
 	if (ret) {
 		printf("GPIO: '%s' not found\n", str_gpio);
 		return cmd_process_error(cmdtp, ret);
@@ -230,7 +232,12 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
 #endif
 	/* grab the pin before we tweak it */
 	ret = gpio_request(gpio, "cmd_gpio");
-	if (ret && ret != -EBUSY) {
+	if (IS_ENABLED(CONFIG_DM_GPIO) && ret == -EBUSY) {
+		struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
+
+		printf("gpio: pin %s (gpio %u) already claimed by '%s'\n",
+		       str_gpio, gpio, uc_priv->name[offset]);
+	} else if (ret && ret != -EBUSY) {
 		printf("gpio: requesting pin %u failed\n", gpio);
 		return -1;
 	}
Signature · 1 line
2.43.7
Reviewed-by on v2 2/5
#4 lore raw

Re: [PATCH v2 2/5] gpio: cmd: warn when a gpio offset is already claimed

Quoted 8 lines · Pranav Sanwal

gpio: cmd: warn when a gpio offset is already claimed

The "gpio" command silently swallows -EBUSY from gpio_request(), since a pin already claimed by a driver or hog is a normal, expected condition for "gpio set"/"gpio toggle" etc. This makes an unexpected offset collision (e.g. two consumers unintentionally routed to the same underlying offset) opaque to debug from the command line.

Quoted 7 lines

Print which label already holds the pin when this happens, using the device/offset already resolved by gpio_lookup_name().

cmd/gpio.c | 11 +++++++++--

 1 file changed, 9 insertions(+), 2 deletions(-)
#5 lore raw

PATCH v2 3/5 gpio: delay: set gpio_count and validate index against it

In reply to Pranav Sanwal

gpio_delay_probe() never set the uclass's gpio_count for this device, leaving it at 0. This left gpio_post_probe()'s claimed/name tracking arrays sized for zero entries, and left gpio_delay_xlate()'s bounds check validating the requested index against a hardcoded 32 that has no relation to how many lines this instance actually manages (ngpio, from its own "gpios" property).

Set gpio_count in probe() to the real count, and validate against it in xlate(), matching the pattern already used by the generic gpio_xlate_offs_flags() default implementation.

Fixes c866f2f197e2 ("gpio: Add GPIO delay driver")

---

Changes in v2: None.

 drivers/gpio/gpio-delay.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpio/gpio-delay.c +5 -1
index 9105deecc4f..533e7d2f8ef 100644
@@ -68,12 +68,13 @@ static int gpio_delay_direction_output(struct udevice *dev, unsigned int offset,
 static int gpio_delay_xlate(struct udevice *dev, struct gpio_desc *desc,
 			    struct ofnode_phandle_args *args)
 {
+	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
 	struct gpio_delay_priv *priv = dev_get_priv(dev);
 
 	if (args->args_count < 3)
 		return -EINVAL;
 
-	if (args->args[0] >= 32)
+	if (args->args[0] >= uc_priv->gpio_count)
 		return -EINVAL;
 
 	struct gpio_delay_desc *d = &priv->descs[args->args[0]];
@@ -97,6 +98,7 @@ static const struct dm_gpio_ops gpio_delay_ops = {
 
 static int gpio_delay_probe(struct udevice *dev)
 {
+	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
 	struct gpio_delay_priv *priv = dev_get_priv(dev);
 	struct gpio_delay_desc *d;
 	ofnode node = dev_ofnode(dev);
@@ -112,6 +114,8 @@ static int gpio_delay_probe(struct udevice *dev)
 	if (!priv->descs)
 		return -ENOMEM;
 
+	uc_priv->gpio_count = ngpio;
+
 	/* Request all GPIOs described in the controller node */
 	for (i = 0; i < ngpio; i++) {
 		d = &priv->descs[i];
Signature · 1 line
2.43.7
#6 lore raw

PATCH v2 4/5 gpio: delay: fix offset propagation in gpio_delay_xlate()

In reply to Pranav Sanwal

gpio_delay_xlate() indexed priv->descs[] correctly using args->args[0], but never propagated that same index into desc->offset. Since gpio_desc_init() initializes offset to 0 before xlate() runs, every consumer's returned descriptor ended up with offset 0 regardless of which index it actually requested.

On boards where gpio-delay wraps more than one real GPIO line, this caused every consumer past the first to silently collide with the first consumer's already-claimed offset 0 in dm_gpio_request(), which returns -EBUSY. The second consumer's real GPIO line was therefore never toggled.

Fix gpio_delay_xlate() to record the requested index into desc->offset so each consumer's descriptor is routed to the correct underlying real GPIO.

Fixes c866f2f197e2 ("gpio: Add GPIO delay driver")

---

Changes in v2: None.

 drivers/gpio/gpio-delay.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpio/gpio-delay.c +2 -0
index 533e7d2f8ef..f6c9c7efb0b 100644
@@ -77,6 +77,8 @@ static int gpio_delay_xlate(struct udevice *dev, struct gpio_desc *desc,
 	if (args->args[0] >= uc_priv->gpio_count)
 		return -EINVAL;
 
+	desc->offset = args->args[0];
+
 	struct gpio_delay_desc *d = &priv->descs[args->args[0]];
 
 	d->ramp_up_us = args->args[1];
Signature · 1 line
2.43.7
#7 lore raw

PATCH v2 5/5 gpio: delay: free wrapped real GPIOs on remove

In reply to Pranav Sanwal

gpio_delay_probe() requests the real GPIOs it wraps via gpio_request_by_name_nodev(), which strdup()s a label into the real GPIO device's own uc_priv->name[] array. The driver had no .remove hook, so those requests, and their strdup'd labels, were never released when the gpio-delay device was torn down.

Add gpio_delay_remove() to free each wrapped real GPIO. Guard each free with device_active() on the real GPIO's device: generic DM teardown (e.g. dm_leak_check_end()'s uclass-by-uclass destroy) does not guarantee a consumer is removed before the provider it wraps, so the real GPIO device may already be inactive with its uclass_priv freed by the time this runs.

Fixes c866f2f197e2 ("gpio: Add GPIO delay driver")

---

Changes in v2:
- New patch.

 drivers/gpio/gpio-delay.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/gpio/gpio-delay.c +15 -0
index f6c9c7efb0b..78e79176f3e 100644
@@ -90,6 +90,20 @@ static int gpio_delay_xlate(struct udevice *dev, struct gpio_desc *desc,
 	return 0;
 }
 
+static int gpio_delay_remove(struct udevice *dev)
+{
+	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
+	struct gpio_delay_priv *priv = dev_get_priv(dev);
+	int i;
+
+	for (i = 0; i < uc_priv->gpio_count; i++) {
+		if (device_active(priv->descs[i].real_gpio.dev))
+			dm_gpio_free(dev, &priv->descs[i].real_gpio);
+	}
+
+	return 0;
+}
+
 static const struct dm_gpio_ops gpio_delay_ops = {
 	.direction_output = gpio_delay_direction_output,
 	.direction_input = gpio_delay_direction_input,
@@ -142,4 +156,5 @@ U_BOOT_DRIVER(gpio_delay) = {
 	.ops = &gpio_delay_ops,
 	.priv_auto = sizeof(struct gpio_delay_priv),
 	.probe = gpio_delay_probe,
+	.remove = gpio_delay_remove,
 };
Signature · 1 line
2.43.7
Reviewed-by on v2 5/5
#8 lore raw

Re: [PATCH v2 5/5] gpio: delay: free wrapped real GPIOs on remove

Hi Pranav,

Quoted 26 lines · Pranav Sanwal

gpio: delay: free wrapped real GPIOs on remove

gpio_delay_probe() requests the real GPIOs it wraps via gpio_request_by_name_nodev(), which strdup()s a label into the real GPIO device's own uc_priv->name[] array. The driver had no .remove hook, so those requests, and their strdup'd labels, were never released when the gpio-delay device was torn down.

Add gpio_delay_remove() to free each wrapped real GPIO. Guard each free with device_active() on the real GPIO's device: generic DM teardown (e.g. dm_leak_check_end()'s uclass-by-uclass destroy) does not guarantee a consumer is removed before the provider it wraps, so the real GPIO device may already be inactive with its uclass_priv freed by the time this runs.

Fixes c866f2f197e2 ("gpio: Add GPIO delay driver")

drivers/gpio/gpio-delay.c | 15 +++++++++++++++

 1 file changed, 15 insertions(+)
drivers/gpio/gpio-delay.c +3 -0
@@ -90,6 +90,20 @@ static int gpio_delay_xlate(struct udevice *dev, struct gpio_desc *desc,
+static int gpio_delay_remove(struct udevice *dev)
+{
+     struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);

drivers/gpio/gpio-delay.c static int gpio_delay_xlate(struct udevice *dev, struct gpio_desc *desc, · line 102

Quoted 10 lines
drivers/gpio/gpio-delay.c +10 -0
+     struct gpio_delay_priv *priv = dev_get_priv(dev);
+     int i;
+
+     for (i = 0; i < uc_priv->gpio_count; i++) {
+             if (device_active(priv->descs[i].real_gpio.dev))
+                     dm_gpio_free(dev, &priv->descs[i].real_gpio);
+     }
+
+     return 0;
+}

Please can you add a short in-code comment explaining why the device_active() guard is needed - a future reader touching this loop won't have the commit message to hand and the guard otherwise looks superfluous.

One related question: gpio_delay_probe() bails out on the first failed gpio_request_by_name_nodev() without freeing the descs it has already populated, so any partial-probe leak is not covered by this .remove hook. That's pre-existing rather than something to fix here, but if you're respinning anyway it would be worth cleaning up in probe by calling the same free path on error.

#9 lore raw
In reply to Pranav Sanwal

Hi Pranav,

Quoted 1 line · Pranav Sanwal

Changes in v2:

Could you also add sandbox coverage for gpio-delay ? You shoud add a gpio-delay node to the sandbox test devicetree wrapping at least two real lines, plus a test in test/dm/gpio.c that requests both consumers and toggles them? That would cover the offset-propagation fix in patch 4, the gpio_count/bounds fix in patch 3, and - via dm_leak_check - the remove-path fix in patch 5, all of which are otherwise not covered by tests and might break at any time.

Regards,
Simon

#10 lore raw

AMD General

Hi Simon,

Yes, this is covered by a follow-up series I've already posted:

test: dm: add sandbox coverage for gpio-delay
https://patchwork.ozlabs.org/project/uboot/list/?series=516335

It depends on patches 3 and 4 of this series (the test fails outright without them), so I sent it separately rather than folding it into v2.

Thanks,
Pranav Sanwal

Quoted 23 lines

-----Original Message-----
From: Simon Glass <[email protected]>
Sent: Saturday, August 8, 2026 11:29 PM
To: Sanwal, Pranav <[email protected]>
Cc: [email protected]
Subject: Re: [v2,0/5] gpio: improve claim diagnostics and fix gpio-delay offset handling

Hi Pranav,

Quoted 1 line · Pranav Sanwal

Changes in v2:

Could you also add sandbox coverage for gpio-delay ? You shoud add a gpio- delay node to the sandbox test devicetree wrapping at least two real lines, plus a test in test/dm/gpio.c that requests both consumers and toggles them? That would cover the offset-propagation fix in patch 4, the gpio_count/bounds fix in patch 3, and - via dm_leak_check - the remove-path fix in patch 5, all of which are otherwise not covered by tests and might break at any time.

Regards,
Simon