Merge branch '2020-05-07-more-kconfig-migrations'
[oweals/u-boot.git] / cmd / gpio.c
index eff36ab2af329df34e83bafee24a633a8380ae76..408a942455b58627a81107f3145fe08e33fbeb9f 100644 (file)
@@ -10,7 +10,9 @@
 #include <command.h>
 #include <errno.h>
 #include <dm.h>
+#include <malloc.h>
 #include <asm/gpio.h>
+#include <linux/err.h>
 
 __weak int name_to_gpio(const char *name)
 {
@@ -223,23 +225,40 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                gpio_direction_output(gpio, value);
        }
        printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio);
-       if (IS_ERR_VALUE(value))
+
+       if (IS_ERR_VALUE(value)) {
                printf("unknown (ret=%d)\n", value);
-       else
+               goto err;
+       } else {
                printf("%d\n", value);
+       }
+
        if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)) {
                int nval = gpio_get_value(gpio);
 
-               if (IS_ERR_VALUE(nval))
+               if (IS_ERR_VALUE(nval)) {
                        printf("   Warning: no access to GPIO output value\n");
-               else if (nval != value)
+                       goto err;
+               } else if (nval != value) {
                        printf("   Warning: value of pin is still %d\n", nval);
+                       goto err;
+               }
        }
 
        if (ret != -EBUSY)
                gpio_free(gpio);
 
-       return value;
+       /*
+        * Whilst wrong, the legacy gpio input command returns the pin
+        * value, or CMD_RET_FAILURE (which is indistinguishable from a
+        * valid pin value).
+        */
+       return (sub_cmd == GPIOC_INPUT) ? value : CMD_RET_SUCCESS;
+
+err:
+       if (ret != -EBUSY)
+               gpio_free(gpio);
+       return CMD_RET_FAILURE;
 }
 
 U_BOOT_CMD(gpio, 4, 0, do_gpio,