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().

Signed-off-by: Pranav Sanwal <[email protected]>
---

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(-)

diff --git a/cmd/gpio.c b/cmd/gpio.c
index 7a43dc6ab18..6992ed0e79a 100644
--- a/cmd/gpio.c
+++ b/cmd/gpio.c
@@ -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;
        }
-- 
2.43.7

Reply via email to