]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
dm: gpio: Support numbered GPIOs
authorSimon Glass <sjg@chromium.org>
Thu, 23 Oct 2014 03:37:01 +0000 (21:37 -0600)
committerSimon Glass <sjg@chromium.org>
Fri, 24 Oct 2014 01:29:07 +0000 (19:29 -0600)
At present banks must be named and it is not possible to refer to GPIOs by
number in driver model. Some boards use numbering - e.g. OMAP. It is fairly
easy to support by detecting the absense of a bank name (which starts with
a letter).

Add support for numbered GPIOs in addition to the existing bank support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>
drivers/gpio/gpio-uclass.c

index f1bbc587961709dec3922c52c76585227acecd9b..a5ffd85c42f1005fc80417886d2033c3148a20d4 100644 (file)
@@ -8,6 +8,7 @@
 #include <dm.h>
 #include <errno.h>
 #include <asm/gpio.h>
+#include <linux/ctype.h>
 
 /**
  * gpio_to_device() - Convert global GPIO number to device, number
@@ -43,35 +44,47 @@ static int gpio_to_device(unsigned int gpio, struct udevice **devp,
 int gpio_lookup_name(const char *name, struct udevice **devp,
                     unsigned int *offsetp, unsigned int *gpiop)
 {
-       struct gpio_dev_priv *uc_priv;
+       struct gpio_dev_priv *uc_priv = NULL;
        struct udevice *dev;
+       ulong offset;
+       int numeric;
        int ret;
 
        if (devp)
                *devp = NULL;
+       numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
        for (ret = uclass_first_device(UCLASS_GPIO, &dev);
             dev;
             ret = uclass_next_device(&dev)) {
-               ulong offset;
                int len;
 
                uc_priv = dev->uclass_priv;
+               if (numeric != -1) {
+                       offset = numeric - uc_priv->gpio_base;
+                       /* Allow GPIOs to be numbered from 0 */
+                       if (offset >= 0 && offset < uc_priv->gpio_count)
+                               break;
+               }
+
                len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
 
                if (!strncasecmp(name, uc_priv->bank_name, len)) {
-                       if (strict_strtoul(name + len, 10, &offset))
-                               continue;
-                       if (devp)
-                               *devp = dev;
-                       if (offsetp)
-                               *offsetp = offset;
-                       if (gpiop)
-                               *gpiop = uc_priv->gpio_base + offset;
-                       return 0;
+                       if (!strict_strtoul(name + len, 10, &offset))
+                               break;
                }
        }
 
-       return ret ? ret : -EINVAL;
+       if (!dev)
+               return ret ? ret : -EINVAL;
+
+       if (devp)
+               *devp = dev;
+       if (offsetp)
+               *offsetp = offset;
+       if (gpiop)
+               *gpiop = uc_priv->gpio_base + offset;
+
+       return 0;
 }
 
 /**