Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Control GPIO pins on the fly |
| 3 | * |
| 4 | * Copyright (c) 2008-2011 Analog Devices Inc. |
| 5 | * |
| 6 | * Licensed under the GPL-2 or later. |
| 7 | */ |
| 8 | |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 9 | #include <command.h> |
Simon Glass | 9165e84 | 2014-08-11 09:23:53 -0600 | [diff] [blame] | 10 | #include <errno.h> |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Diego Rondini | dd2b8c1 | 2022-04-11 12:02:09 +0200 | [diff] [blame] | 14 | #ifdef CONFIG_CMD_GPIO_READ |
| 15 | #include <env.h> |
| 16 | #endif |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 17 | #include <asm/gpio.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
Paul Barker | ee0522f | 2023-10-16 10:25:31 +0100 | [diff] [blame] | 19 | #include <dm/device_compat.h> |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 20 | |
Jeroen Hofstee | 5b5ac64 | 2014-10-08 22:58:03 +0200 | [diff] [blame] | 21 | __weak int name_to_gpio(const char *name) |
Ian Campbell | fd11bea | 2014-03-27 20:34:13 +0000 | [diff] [blame] | 22 | { |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 23 | return dectoul(name, NULL); |
Ian Campbell | fd11bea | 2014-03-27 20:34:13 +0000 | [diff] [blame] | 24 | } |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 25 | |
| 26 | enum gpio_cmd { |
Simon Glass | 66f657d | 2019-01-21 14:53:20 -0700 | [diff] [blame] | 27 | GPIOC_INPUT, |
| 28 | GPIOC_SET, |
| 29 | GPIOC_CLEAR, |
| 30 | GPIOC_TOGGLE, |
Diego Rondini | dd2b8c1 | 2022-04-11 12:02:09 +0200 | [diff] [blame] | 31 | #ifdef CONFIG_CMD_GPIO_READ |
| 32 | GPIOC_READ, |
| 33 | #endif |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 34 | }; |
| 35 | |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 36 | #if defined(CONFIG_DM_GPIO) && !defined(gpio_status) |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 37 | |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 38 | /* A few flags used by show_gpio() */ |
| 39 | enum { |
| 40 | FLAG_SHOW_ALL = 1 << 0, |
| 41 | FLAG_SHOW_BANK = 1 << 1, |
| 42 | FLAG_SHOW_NEWLINE = 1 << 2, |
| 43 | }; |
| 44 | |
Simon Glass | 0757535 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 45 | static void gpio_get_description(struct udevice *dev, const char *bank_name, |
Simon Glass | b8989b5 | 2019-02-16 20:24:42 -0700 | [diff] [blame] | 46 | int offset, int *flagsp, bool show_all) |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 47 | { |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 48 | char buf[80]; |
| 49 | int ret; |
| 50 | |
Simon Glass | 0757535 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 51 | ret = gpio_get_function(dev, offset, NULL); |
| 52 | if (ret < 0) |
| 53 | goto err; |
Simon Glass | b8989b5 | 2019-02-16 20:24:42 -0700 | [diff] [blame] | 54 | if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED) |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 55 | return; |
| 56 | if ((*flagsp & FLAG_SHOW_BANK) && bank_name) { |
| 57 | if (*flagsp & FLAG_SHOW_NEWLINE) { |
| 58 | putc('\n'); |
| 59 | *flagsp &= ~FLAG_SHOW_NEWLINE; |
| 60 | } |
| 61 | printf("Bank %s:\n", bank_name); |
| 62 | *flagsp &= ~FLAG_SHOW_BANK; |
| 63 | } |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 64 | |
Simon Glass | 0757535 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 65 | ret = gpio_get_status(dev, offset, buf, sizeof(buf)); |
| 66 | if (ret) |
| 67 | goto err; |
| 68 | |
| 69 | printf("%s\n", buf); |
| 70 | return; |
| 71 | err: |
Paul Barker | ee0522f | 2023-10-16 10:25:31 +0100 | [diff] [blame] | 72 | if (ret != -ENOENT) |
| 73 | printf("Error %d\n", ret); |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 76 | static int do_gpio_status(bool all, const char *gpio_name) |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 77 | { |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 78 | struct udevice *dev; |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 79 | int banklen; |
| 80 | int flags; |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 81 | int ret, err = 0; |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 82 | |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 83 | flags = 0; |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 84 | if (gpio_name && !*gpio_name) |
| 85 | gpio_name = NULL; |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 86 | for (ret = uclass_first_device_check(UCLASS_GPIO, &dev); |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 87 | dev; |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 88 | ret = uclass_next_device_check(&dev)) { |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 89 | const char *bank_name; |
| 90 | int num_bits; |
| 91 | |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 92 | if (ret) { |
| 93 | printf("GPIO device %s probe error %i\n", |
| 94 | dev->name, ret); |
| 95 | err = ret; |
| 96 | continue; |
| 97 | } |
| 98 | |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 99 | flags |= FLAG_SHOW_BANK; |
| 100 | if (all) |
| 101 | flags |= FLAG_SHOW_ALL; |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 102 | bank_name = gpio_get_bank_info(dev, &num_bits); |
Simon Glass | 0757535 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 103 | if (!num_bits) { |
| 104 | debug("GPIO device %s has no bits\n", dev->name); |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 105 | continue; |
Simon Glass | 0757535 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 106 | } |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 107 | banklen = bank_name ? strlen(bank_name) : 0; |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 108 | |
| 109 | if (!gpio_name || !bank_name || |
Simon Glass | 48ca690 | 2019-02-16 20:24:43 -0700 | [diff] [blame] | 110 | !strncasecmp(gpio_name, bank_name, banklen)) { |
Heinrich Schuchardt | e946b5d | 2019-08-22 22:19:41 +0200 | [diff] [blame] | 111 | const char *p; |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 112 | int offset; |
| 113 | |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 114 | p = gpio_name + banklen; |
| 115 | if (gpio_name && *p) { |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 116 | offset = dectoul(p, NULL); |
Simon Glass | 0757535 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 117 | gpio_get_description(dev, bank_name, offset, |
Simon Glass | b8989b5 | 2019-02-16 20:24:42 -0700 | [diff] [blame] | 118 | &flags, true); |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 119 | } else { |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 120 | for (offset = 0; offset < num_bits; offset++) { |
Simon Glass | 0757535 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 121 | gpio_get_description(dev, bank_name, |
Simon Glass | b8989b5 | 2019-02-16 20:24:42 -0700 | [diff] [blame] | 122 | offset, &flags, false); |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 123 | } |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 126 | /* Add a newline between bank names */ |
| 127 | if (!(flags & FLAG_SHOW_BANK)) |
| 128 | flags |= FLAG_SHOW_NEWLINE; |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 131 | return err; |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 132 | } |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 133 | #endif |
| 134 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 135 | static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc, |
| 136 | char *const argv[]) |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 137 | { |
| 138 | unsigned int gpio; |
| 139 | enum gpio_cmd sub_cmd; |
Simon Glass | b71bea7 | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 140 | int value; |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 141 | const char *str_cmd, *str_gpio = NULL; |
Diego Rondini | dd2b8c1 | 2022-04-11 12:02:09 +0200 | [diff] [blame] | 142 | #ifdef CONFIG_CMD_GPIO_READ |
| 143 | const char *str_var = NULL; |
| 144 | #endif |
Simon Glass | 9165e84 | 2014-08-11 09:23:53 -0600 | [diff] [blame] | 145 | int ret; |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 146 | #ifdef CONFIG_DM_GPIO |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 147 | bool all = false; |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 148 | #endif |
| 149 | |
| 150 | if (argc < 2) |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 151 | show_usage: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 152 | return CMD_RET_USAGE; |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 153 | str_cmd = argv[1]; |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 154 | argc -= 2; |
| 155 | argv += 2; |
| 156 | #ifdef CONFIG_DM_GPIO |
Diego Rondini | dd2b8c1 | 2022-04-11 12:02:09 +0200 | [diff] [blame] | 157 | if (argc > 0 && !strncmp(str_cmd, "status", 2) && !strcmp(*argv, "-a")) { |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 158 | all = true; |
| 159 | argc--; |
| 160 | argv++; |
| 161 | } |
| 162 | #endif |
Diego Rondini | dd2b8c1 | 2022-04-11 12:02:09 +0200 | [diff] [blame] | 163 | #ifdef CONFIG_CMD_GPIO_READ |
| 164 | if (argc > 0 && !strncmp(str_cmd, "read", 2)) { |
| 165 | if (argc < 2) |
| 166 | goto show_usage; |
| 167 | str_var = *argv; |
| 168 | argc--; |
| 169 | argv++; |
| 170 | } |
| 171 | #endif |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 172 | if (argc > 0) |
| 173 | str_gpio = *argv; |
Simon Glass | 4c80c53 | 2016-02-14 16:28:59 -0700 | [diff] [blame] | 174 | if (!strncmp(str_cmd, "status", 2)) { |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 175 | /* Support deprecated gpio_status() */ |
| 176 | #ifdef gpio_status |
| 177 | gpio_status(); |
| 178 | return 0; |
| 179 | #elif defined(CONFIG_DM_GPIO) |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 180 | return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio)); |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 181 | #else |
| 182 | goto show_usage; |
| 183 | #endif |
| 184 | } |
| 185 | |
| 186 | if (!str_gpio) |
| 187 | goto show_usage; |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 188 | |
| 189 | /* parse the behavior */ |
| 190 | switch (*str_cmd) { |
Simon Glass | 66f657d | 2019-01-21 14:53:20 -0700 | [diff] [blame] | 191 | case 'i': |
| 192 | sub_cmd = GPIOC_INPUT; |
| 193 | break; |
| 194 | case 's': |
| 195 | sub_cmd = GPIOC_SET; |
| 196 | break; |
| 197 | case 'c': |
| 198 | sub_cmd = GPIOC_CLEAR; |
| 199 | break; |
| 200 | case 't': |
| 201 | sub_cmd = GPIOC_TOGGLE; |
| 202 | break; |
Diego Rondini | dd2b8c1 | 2022-04-11 12:02:09 +0200 | [diff] [blame] | 203 | #ifdef CONFIG_CMD_GPIO_READ |
| 204 | case 'r': |
| 205 | sub_cmd = GPIOC_READ; |
| 206 | break; |
| 207 | #endif |
Simon Glass | 66f657d | 2019-01-21 14:53:20 -0700 | [diff] [blame] | 208 | default: |
| 209 | goto show_usage; |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 212 | #if defined(CONFIG_DM_GPIO) |
| 213 | /* |
| 214 | * TODO(sjg@chromium.org): For now we must fit into the existing GPIO |
| 215 | * framework, so we look up the name here and convert it to a GPIO number. |
| 216 | * Once all GPIO drivers are converted to driver model, we can change the |
| 217 | * code here to use the GPIO uclass interface instead of the numbered |
| 218 | * GPIO compatibility layer. |
| 219 | */ |
| 220 | ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio); |
Simon Glass | 66280e8 | 2016-01-21 19:44:50 -0700 | [diff] [blame] | 221 | if (ret) { |
| 222 | printf("GPIO: '%s' not found\n", str_gpio); |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 223 | return cmd_process_error(cmdtp, ret); |
Simon Glass | 66280e8 | 2016-01-21 19:44:50 -0700 | [diff] [blame] | 224 | } |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 225 | #else |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 226 | /* turn the gpio name into a gpio number */ |
| 227 | gpio = name_to_gpio(str_gpio); |
| 228 | if (gpio < 0) |
| 229 | goto show_usage; |
Simon Glass | 95a260a9 | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 230 | #endif |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 231 | /* grab the pin before we tweak it */ |
Simon Glass | 9165e84 | 2014-08-11 09:23:53 -0600 | [diff] [blame] | 232 | ret = gpio_request(gpio, "cmd_gpio"); |
| 233 | if (ret && ret != -EBUSY) { |
Mike Frysinger | 6801201 | 2011-04-12 03:02:11 -0400 | [diff] [blame] | 234 | printf("gpio: requesting pin %u failed\n", gpio); |
| 235 | return -1; |
| 236 | } |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 237 | |
| 238 | /* finally, let's do it: set direction and exec command */ |
Diego Rondini | dd2b8c1 | 2022-04-11 12:02:09 +0200 | [diff] [blame] | 239 | if (sub_cmd == GPIOC_INPUT |
| 240 | #ifdef CONFIG_CMD_GPIO_READ |
| 241 | || sub_cmd == GPIOC_READ |
| 242 | #endif |
| 243 | ) { |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 244 | gpio_direction_input(gpio); |
| 245 | value = gpio_get_value(gpio); |
| 246 | } else { |
| 247 | switch (sub_cmd) { |
Simon Glass | 66f657d | 2019-01-21 14:53:20 -0700 | [diff] [blame] | 248 | case GPIOC_SET: |
Simon Glass | b71bea7 | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 249 | value = 1; |
| 250 | break; |
Simon Glass | 66f657d | 2019-01-21 14:53:20 -0700 | [diff] [blame] | 251 | case GPIOC_CLEAR: |
Simon Glass | b71bea7 | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 252 | value = 0; |
| 253 | break; |
Simon Glass | 66f657d | 2019-01-21 14:53:20 -0700 | [diff] [blame] | 254 | case GPIOC_TOGGLE: |
Simon Glass | b71bea7 | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 255 | value = gpio_get_value(gpio); |
| 256 | if (!IS_ERR_VALUE(value)) |
| 257 | value = !value; |
| 258 | break; |
| 259 | default: |
| 260 | goto show_usage; |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 261 | } |
| 262 | gpio_direction_output(gpio, value); |
| 263 | } |
Heinrich Schuchardt | b197001 | 2019-01-06 11:31:54 +0100 | [diff] [blame] | 264 | printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio); |
Luka Kovacic | 4dbc107 | 2020-01-05 20:10:56 +0100 | [diff] [blame] | 265 | |
| 266 | if (IS_ERR_VALUE(value)) { |
Simon Glass | b71bea7 | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 267 | printf("unknown (ret=%d)\n", value); |
Luka Kovacic | 4dbc107 | 2020-01-05 20:10:56 +0100 | [diff] [blame] | 268 | goto err; |
| 269 | } else { |
Simon Glass | b71bea7 | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 270 | printf("%d\n", value); |
Diego Rondini | dd2b8c1 | 2022-04-11 12:02:09 +0200 | [diff] [blame] | 271 | #ifdef CONFIG_CMD_GPIO_READ |
| 272 | if (sub_cmd == GPIOC_READ) |
| 273 | env_set_ulong(str_var, (ulong)value); |
| 274 | #endif |
Luka Kovacic | 4dbc107 | 2020-01-05 20:10:56 +0100 | [diff] [blame] | 275 | } |
| 276 | |
Diego Rondini | dd2b8c1 | 2022-04-11 12:02:09 +0200 | [diff] [blame] | 277 | if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value) |
| 278 | #ifdef CONFIG_CMD_GPIO_READ |
| 279 | && sub_cmd != GPIOC_READ |
| 280 | #endif |
| 281 | ) { |
Simon Glass | b71bea7 | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 282 | int nval = gpio_get_value(gpio); |
| 283 | |
Luka Kovacic | 4dbc107 | 2020-01-05 20:10:56 +0100 | [diff] [blame] | 284 | if (IS_ERR_VALUE(nval)) { |
Simon Glass | b71bea7 | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 285 | printf(" Warning: no access to GPIO output value\n"); |
Luka Kovacic | 4dbc107 | 2020-01-05 20:10:56 +0100 | [diff] [blame] | 286 | goto err; |
| 287 | } else if (nval != value) { |
Simon Glass | b71bea7 | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 288 | printf(" Warning: value of pin is still %d\n", nval); |
Luka Kovacic | 4dbc107 | 2020-01-05 20:10:56 +0100 | [diff] [blame] | 289 | goto err; |
| 290 | } |
Simon Glass | b71bea7 | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 291 | } |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 292 | |
Simon Glass | 9165e84 | 2014-08-11 09:23:53 -0600 | [diff] [blame] | 293 | if (ret != -EBUSY) |
| 294 | gpio_free(gpio); |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 295 | |
Alex Kiernan | 4af2a33 | 2020-03-11 08:46:29 +0000 | [diff] [blame] | 296 | /* |
| 297 | * Whilst wrong, the legacy gpio input command returns the pin |
| 298 | * value, or CMD_RET_FAILURE (which is indistinguishable from a |
| 299 | * valid pin value). |
| 300 | */ |
| 301 | return (sub_cmd == GPIOC_INPUT) ? value : CMD_RET_SUCCESS; |
Luka Kovacic | 4dbc107 | 2020-01-05 20:10:56 +0100 | [diff] [blame] | 302 | |
| 303 | err: |
| 304 | if (ret != -EBUSY) |
| 305 | gpio_free(gpio); |
| 306 | return CMD_RET_FAILURE; |
Mike Frysinger | a972b8d | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 307 | } |
| 308 | |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 309 | U_BOOT_CMD(gpio, 4, 0, do_gpio, |
| 310 | "query and control gpio pins", |
| 311 | "<input|set|clear|toggle> <pin>\n" |
| 312 | " - input/set/clear/toggle the specified pin\n" |
Diego Rondini | dd2b8c1 | 2022-04-11 12:02:09 +0200 | [diff] [blame] | 313 | #ifdef CONFIG_CMD_GPIO_READ |
| 314 | "gpio read <name> <pin>\n" |
| 315 | " - set environment variable 'name' to the specified pin\n" |
| 316 | #endif |
Simon Glass | 89e6405 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 317 | "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs"); |