gpio: improve claim diagnostics and fix gpio-delay offset handling
cover gpio: improve claim diagnostics and fix gpio-delay offset handling
This series improves diagnosability of GPIO offset collisions in the uclass layer and fixes two related bugs in the gpio-delay driver that were uncovered while debugging such a collision.
Patch 1 adds a dev_warn() in dm_gpio_request() that prints which label already holds a GPIO offset when a new request collides with it, so an -EBUSY failure can be root-caused instead of failing silently.
Patch 2 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, matching the pattern used by the generic uclass xlate implementation and correctly sizing the claimed/name tracking arrays.
Patch 3 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; patches 1 and 2 are diagnostic and robustness fixes found along the way.
Patches 2 and 3 are tagged Fixes: c866f2f197e2 ("gpio: Add GPIO delay driver").
Pranav Sanwal (3):
gpio: uclass: 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()
drivers/gpio/gpio-delay.c | 8 +++++++- drivers/gpio/gpio-uclass.c | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 gpio: uclass: warn 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 is holding it, making an offset collision opaque to debug.
Add a dev_warn() printing the label already holding the offset alongside the newly requested label.
--- drivers/gpio/gpio-uclass.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpio/gpio-uclass.c +5 -1
@@ -391,8 +391,12 @@ 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_warn (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.34.1
Re: [PATCH 1/3] gpio: uclass: warn when a gpio offset is already claimed
Hi Pranav,
Quoted 16 lines · Pranav Sanwal
gpio: uclass: warn when a gpio offset is already claimed
dm_gpio_request()returns-EBUSYwhen the requested offset is already claimed, but gives no indication of which label is holding it, making an offset collision opaque to debug.Add a
dev_warn()printing the label already holding the offset alongside the newly requested label.Signed-off-by Pranav Sanwal <[email protected]>
drivers/gpio/gpio-uclass.c| 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpio/gpio-uclass.c int dm_gpio_request(struct gpio_desc *desc, const char *label) · line 402
Quoted 7 lines
drivers/gpio/gpio-uclass.c +4
-1@@ -397,8 +397,12 @@ int dm_gpio_request(struct gpio_desc *desc, const char *label) uc_priv =dev_get_uclass_priv (dev); -if (gpio_is_claimed (uc_priv, desc->offset)) +if (gpio_is_claimed (uc_priv, desc->offset)) { +dev_warn (dev,"gpio offset %u already claimed by '%s', requested label '%s'\n" , + desc->offset, uc_priv->name[desc->offset], label);return -EBUSY ; + }
The -EBUSY return from dm_gpio_request() is a normal, expected condition for some callers, not just an error to be diagnosed. For example, cmd/gpio.c deliberately tolerates it:
ret = gpio_request(gpio, "cmd_gpio");
if (ret && ret != -EBUSY) {so with this patch every 'gpio set' or 'gpio toggle' on a pin already claimed by a driver or hog prints a warning for a legitimate operation. The sandbox tests also trigger this path on purpose (dm_test_gpio_phandles and dm_test_gpio_devm expect -EBUSY), so test runs will emit these warnings too.
We don't really want drivers to print warnings unless something is actually wrong with the device.
Could this go into the gpio command instead?
Regards,
Simon
PATCH 2/3 gpio: delay: set gpio_count and validate index against it
In reply to Pranav Sanwalgpio_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.
--- drivers/gpio/gpio-delay.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpio/gpio-delay.c +5 -1
@@ -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.34.1
Re: [PATCH 2/3] gpio: delay: set gpio_count and validate index against it
Quoted 13 lines · Pranav Sanwal
gpio: delay: set
gpio_countand validate index against it
gpio_delay_probe()never set the uclass'sgpio_countfor 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_countinprobe()to the real count, and validate against it inxlate(), matching the pattern already used by the genericgpio_xlate_offs_flags()default implementation.
Quoted 5 lines
Fixes c866f2f197e2 ("gpio: Add GPIO delay driver")Signed-off-by Pranav Sanwal <[email protected]>
drivers/gpio/gpio-delay.c| 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)
PATCH 3/3 gpio: delay: fix offset propagation in gpio_delay_xlate()
In reply to Pranav Sanwalgpio_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.
--- drivers/gpio/gpio-delay.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/gpio/gpio-delay.c +2 -0
@@ -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.34.1
Re: [PATCH 3/3] gpio: delay: fix offset propagation in gpio_delay_xlate()
Quoted 18 lines · Pranav Sanwal
gpio: delay: fix offset propagation in
gpio_delay_xlate()
gpio_delay_xlate()indexed priv->descs[] correctly using args->args[0], but never propagated that same index intodesc->offset. Sincegpio_desc_init()initializes offset to 0 beforexlate()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 intodesc->offsetso each consumer's descriptor is routed to the correct underlying real GPIO.
Quoted 5 lines
Fixes c866f2f197e2 ("gpio: Add GPIO delay driver")Signed-off-by Pranav Sanwal <[email protected]>
drivers/gpio/gpio-delay.c| 2 ++1 file changed, 2 insertions(+)
Hi Pranav,
Quoted 3 lines · Pranav Sanwal
This series improves diagnosability of GPIO offset collisions in the uclass layer and fixes two related bugs in the gpio-delay driver that were uncovered while debugging such a collision.
Thanks for tracking this down.
One thing is missing, though: tests. The gpio-delay driver has no sandbox coverage at all, which is presumably why these bugs went unnoticed. Please can you add a gpio-delay node to the sandbox test devicetree, wrapping at least two GPIO lines, along with a test in test/dm/gpio.c that requests both consumers? That would have caught the offset-propagation bug in patch 3 directly and would also exercise the bounds check and array sizing from patch 2.
Regards,
Simon
AMD General
Hi Simon
Thanks for the review, and for the ask on test coverage.
Quoted 16 lines
-----Original Message-----
From: Simon Glass <[email protected]>
Sent: Tuesday, July 14, 2026 2:27 AM
To: Sanwal, Pranav <[email protected]>
Cc: [email protected]
Subject: Re: [0/3] gpio: improve claim diagnostics and fix gpio-delay offset handlingHi Pranav,
Quoted 3 lines · Pranav Sanwal
This series improves diagnosability of GPIO offset collisions in the uclass layer and fixes two related bugs in the gpio-delay driver that were uncovered while debugging such a collision.
Quoted 8 lines
Thanks for tracking this down.
One thing is missing, though: tests. The gpio-delay driver has no sandbox coverage at all, which is presumably why these bugs went unnoticed. Please can you add a gpio-delay node to the sandbox test devicetree, wrapping at least two GPIO lines, along with a test in
test/dm/gpio.cthat requests both consumers? That would have caught the offset-propagation bug in patch 3 directly and would also exercise the bounds check and array sizing from patch 2.
While putting together the gpio-delay node/test you asked for, I found that test/dm/gpio.c hasn't actually been compiled in years: its Makefile rule was folded into an unrelated CONFIG_ACPIGEN-gated block, losing the "$" from "obj-$(CONFIG_DM_GPIO)" in the process. Making it build again surfaces a few years of bit-rot in the existing tests, plus a separate pre-existing core-DM issue unrelated to GPIO.
That's more scope than belongs in this series, so I'm sending v2 of the actual fixes now (patch 1 reworked per your comment), and will follow up separately with the test/dm build fix and the gpio-delay sandbox coverage you asked for.
Quoted 2 lines
Regards,
Simon
Thanks,
Pranav Sanwal