]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/cmd_gpio.c
cmd_gpio.c: adjust help message depending on gpio_status being compiled in or not
[karo-tx-uboot.git] / common / cmd_gpio.c
index b97533f313d3960c0b22f03108b779946a533724..93c53a867cba3786ce9e842160fd727fbd6befe8 100644 (file)
@@ -8,10 +8,11 @@
 
 #include <common.h>
 #include <command.h>
+#include <errno.h>
 #include <dm.h>
 #include <asm/gpio.h>
 
-int __weak name_to_gpio(const char *name)
+__weak int name_to_gpio(const char *name)
 {
        return simple_strtoul(name, NULL, 10);
 }
@@ -24,13 +25,6 @@ enum gpio_cmd {
 };
 
 #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
-static const char * const gpio_function[GPIOF_COUNT] = {
-       "input",
-       "output",
-       "unused",
-       "unknown",
-       "func",
-};
 
 /* A few flags used by show_gpio() */
 enum {
@@ -39,22 +33,16 @@ enum {
        FLAG_SHOW_NEWLINE       = 1 << 2,
 };
 
-static void show_gpio(struct udevice *dev, const char *bank_name, int offset,
-                     int *flagsp)
+static void gpio_get_description(struct udevice *dev, const char *bank_name,
+                                int offset, int *flagsp)
 {
-       struct dm_gpio_ops *ops = gpio_get_ops(dev);
-       int func = GPIOF_UNKNOWN;
        char buf[80];
        int ret;
 
-       BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
-
-       if (ops->get_function) {
-               ret = ops->get_function(dev, offset);
-               if (ret >= 0 && ret < ARRAY_SIZE(gpio_function))
-                       func = ret;
-       }
-       if (!(*flagsp & FLAG_SHOW_ALL) && func == GPIOF_UNUSED)
+       ret = gpio_get_function(dev, offset, NULL);
+       if (ret < 0)
+               goto err;
+       if (!(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED)
                return;
        if ((*flagsp & FLAG_SHOW_BANK) && bank_name) {
                if (*flagsp & FLAG_SHOW_NEWLINE) {
@@ -64,20 +52,15 @@ static void show_gpio(struct udevice *dev, const char *bank_name, int offset,
                printf("Bank %s:\n", bank_name);
                *flagsp &= ~FLAG_SHOW_BANK;
        }
-       *buf = '\0';
-       if (ops->get_state) {
-               ret = ops->get_state(dev, offset, buf, sizeof(buf));
-               if (ret) {
-                       puts("<unknown>");
-                       return;
-               }
-       } else {
-               sprintf(buf, "%s%u: %8s %d", bank_name, offset,
-                       gpio_function[func], ops->get_value(dev, offset));
-       }
 
-       puts(buf);
-       puts("\n");
+       ret = gpio_get_status(dev, offset, buf, sizeof(buf));
+       if (ret)
+               goto err;
+
+       printf("%s\n", buf);
+       return;
+err:
+       printf("Error %d\n", ret);
 }
 
 static int do_gpio_status(bool all, const char *gpio_name)
@@ -100,8 +83,10 @@ static int do_gpio_status(bool all, const char *gpio_name)
                if (all)
                        flags |= FLAG_SHOW_ALL;
                bank_name = gpio_get_bank_info(dev, &num_bits);
-               if (!num_bits)
+               if (!num_bits) {
+                       debug("GPIO device %s has no bits\n", dev->name);
                        continue;
+               }
                banklen = bank_name ? strlen(bank_name) : 0;
 
                if (!gpio_name || !bank_name ||
@@ -112,11 +97,12 @@ static int do_gpio_status(bool all, const char *gpio_name)
                        p = gpio_name + banklen;
                        if (gpio_name && *p) {
                                offset = simple_strtoul(p, NULL, 10);
-                               show_gpio(dev, bank_name, offset, &flags);
+                               gpio_get_description(dev, bank_name, offset,
+                                                    &flags);
                        } else {
                                for (offset = 0; offset < num_bits; offset++) {
-                                       show_gpio(dev, bank_name, offset,
-                                                 &flags);
+                                       gpio_get_description(dev, bank_name,
+                                                            offset, &flags);
                                }
                        }
                }
@@ -135,9 +121,9 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        enum gpio_cmd sub_cmd;
        ulong value;
        const char *str_cmd, *str_gpio = NULL;
+       int ret;
 #ifdef CONFIG_DM_GPIO
        bool all = false;
-       int ret;
 #endif
 
        if (argc < 2)
@@ -197,9 +183,10 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                goto show_usage;
 #endif
        /* grab the pin before we tweak it */
-       if (gpio_request(gpio, "cmd_gpio")) {
+       ret = gpio_request(gpio, "cmd_gpio");
+       if (ret && ret != -EBUSY) {
                printf("gpio: requesting pin %u failed\n", gpio);
-               return -1;
+               return CMD_RET_FAILURE;
        }
 
        /* finally, let's do it: set direction and exec command */
@@ -218,13 +205,20 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        printf("gpio: pin %s (gpio %i) value is %lu\n",
                str_gpio, gpio, value);
 
-       gpio_free(gpio);
+       if (ret != -EBUSY)
+               gpio_free(gpio);
 
-       return value;
+       return (ret && ret != -EBUSY) ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
 }
 
+#ifdef gpio_status
+#define gpio_status_help_msg "gpio status [-a] [<bank> | <pin>]  - show [all/claimed] GPIOs"
+#else
+#define gpio_status_help_msg ""
+#endif
+
 U_BOOT_CMD(gpio, 4, 0, do_gpio,
           "query and control gpio pins",
           "<input|set|clear|toggle> <pin>\n"
           "    - input/set/clear/toggle the specified pin\n"
-          "gpio status [-a] [<bank> | <pin>]  - show [all/claimed] GPIOs");
+          gpio_status_help_msg);