Mike Frysinger | 0e71f80 | 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 | |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
Simon Glass | 23a6b8e | 2014-08-11 09:23:53 -0600 | [diff] [blame] | 11 | #include <errno.h> |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 14 | #include <asm/gpio.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 15 | #include <linux/err.h> |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 16 | |
Jeroen Hofstee | b613b10 | 2014-10-08 22:58:03 +0200 | [diff] [blame] | 17 | __weak int name_to_gpio(const char *name) |
Ian Campbell | 87af5e1 | 2014-03-27 20:34:13 +0000 | [diff] [blame] | 18 | { |
| 19 | return simple_strtoul(name, NULL, 10); |
| 20 | } |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 21 | |
| 22 | enum gpio_cmd { |
Simon Glass | e70271f | 2019-01-21 14:53:20 -0700 | [diff] [blame] | 23 | GPIOC_INPUT, |
| 24 | GPIOC_SET, |
| 25 | GPIOC_CLEAR, |
| 26 | GPIOC_TOGGLE, |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 27 | }; |
| 28 | |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 29 | #if defined(CONFIG_DM_GPIO) && !defined(gpio_status) |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 30 | |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 31 | /* A few flags used by show_gpio() */ |
| 32 | enum { |
| 33 | FLAG_SHOW_ALL = 1 << 0, |
| 34 | FLAG_SHOW_BANK = 1 << 1, |
| 35 | FLAG_SHOW_NEWLINE = 1 << 2, |
| 36 | }; |
| 37 | |
Simon Glass | 6b1ef59 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 38 | static void gpio_get_description(struct udevice *dev, const char *bank_name, |
Simon Glass | 8edd31b | 2019-02-16 20:24:42 -0700 | [diff] [blame] | 39 | int offset, int *flagsp, bool show_all) |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 40 | { |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 41 | char buf[80]; |
| 42 | int ret; |
| 43 | |
Simon Glass | 6b1ef59 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 44 | ret = gpio_get_function(dev, offset, NULL); |
| 45 | if (ret < 0) |
| 46 | goto err; |
Simon Glass | 8edd31b | 2019-02-16 20:24:42 -0700 | [diff] [blame] | 47 | if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED) |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 48 | return; |
| 49 | if ((*flagsp & FLAG_SHOW_BANK) && bank_name) { |
| 50 | if (*flagsp & FLAG_SHOW_NEWLINE) { |
| 51 | putc('\n'); |
| 52 | *flagsp &= ~FLAG_SHOW_NEWLINE; |
| 53 | } |
| 54 | printf("Bank %s:\n", bank_name); |
| 55 | *flagsp &= ~FLAG_SHOW_BANK; |
| 56 | } |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 57 | |
Simon Glass | 6b1ef59 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 58 | ret = gpio_get_status(dev, offset, buf, sizeof(buf)); |
| 59 | if (ret) |
| 60 | goto err; |
| 61 | |
| 62 | printf("%s\n", buf); |
| 63 | return; |
| 64 | err: |
| 65 | printf("Error %d\n", ret); |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 68 | static int do_gpio_status(bool all, const char *gpio_name) |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 69 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 70 | struct udevice *dev; |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 71 | int banklen; |
| 72 | int flags; |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 73 | int ret; |
| 74 | |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 75 | flags = 0; |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 76 | if (gpio_name && !*gpio_name) |
| 77 | gpio_name = NULL; |
| 78 | for (ret = uclass_first_device(UCLASS_GPIO, &dev); |
| 79 | dev; |
| 80 | ret = uclass_next_device(&dev)) { |
| 81 | const char *bank_name; |
| 82 | int num_bits; |
| 83 | |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 84 | flags |= FLAG_SHOW_BANK; |
| 85 | if (all) |
| 86 | flags |= FLAG_SHOW_ALL; |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 87 | bank_name = gpio_get_bank_info(dev, &num_bits); |
Simon Glass | 6b1ef59 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 88 | if (!num_bits) { |
| 89 | debug("GPIO device %s has no bits\n", dev->name); |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 90 | continue; |
Simon Glass | 6b1ef59 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 91 | } |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 92 | banklen = bank_name ? strlen(bank_name) : 0; |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 93 | |
| 94 | if (!gpio_name || !bank_name || |
Simon Glass | 26e8b49 | 2019-02-16 20:24:43 -0700 | [diff] [blame] | 95 | !strncasecmp(gpio_name, bank_name, banklen)) { |
Heinrich Schuchardt | 02767cb | 2019-08-22 22:19:41 +0200 | [diff] [blame] | 96 | const char *p; |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 97 | int offset; |
| 98 | |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 99 | p = gpio_name + banklen; |
| 100 | if (gpio_name && *p) { |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 101 | offset = simple_strtoul(p, NULL, 10); |
Simon Glass | 6b1ef59 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 102 | gpio_get_description(dev, bank_name, offset, |
Simon Glass | 8edd31b | 2019-02-16 20:24:42 -0700 | [diff] [blame] | 103 | &flags, true); |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 104 | } else { |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 105 | for (offset = 0; offset < num_bits; offset++) { |
Simon Glass | 6b1ef59 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 106 | gpio_get_description(dev, bank_name, |
Simon Glass | 8edd31b | 2019-02-16 20:24:42 -0700 | [diff] [blame] | 107 | offset, &flags, false); |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 108 | } |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 111 | /* Add a newline between bank names */ |
| 112 | if (!(flags & FLAG_SHOW_BANK)) |
| 113 | flags |= FLAG_SHOW_NEWLINE; |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return ret; |
| 117 | } |
| 118 | #endif |
| 119 | |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 120 | static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 121 | { |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 122 | unsigned int gpio; |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 123 | enum gpio_cmd sub_cmd; |
Simon Glass | db96baa | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 124 | int value; |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 125 | const char *str_cmd, *str_gpio = NULL; |
Simon Glass | 23a6b8e | 2014-08-11 09:23:53 -0600 | [diff] [blame] | 126 | int ret; |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 127 | #ifdef CONFIG_DM_GPIO |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 128 | bool all = false; |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 129 | #endif |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 130 | |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 131 | if (argc < 2) |
| 132 | show_usage: |
| 133 | return CMD_RET_USAGE; |
| 134 | str_cmd = argv[1]; |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 135 | argc -= 2; |
| 136 | argv += 2; |
| 137 | #ifdef CONFIG_DM_GPIO |
| 138 | if (argc > 0 && !strcmp(*argv, "-a")) { |
| 139 | all = true; |
| 140 | argc--; |
| 141 | argv++; |
| 142 | } |
| 143 | #endif |
| 144 | if (argc > 0) |
| 145 | str_gpio = *argv; |
Simon Glass | d572df1 | 2016-02-14 16:28:59 -0700 | [diff] [blame] | 146 | if (!strncmp(str_cmd, "status", 2)) { |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 147 | /* Support deprecated gpio_status() */ |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 148 | #ifdef gpio_status |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 149 | gpio_status(); |
| 150 | return 0; |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 151 | #elif defined(CONFIG_DM_GPIO) |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 152 | return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio)); |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 153 | #else |
| 154 | goto show_usage; |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 155 | #endif |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 156 | } |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 157 | |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 158 | if (!str_gpio) |
| 159 | goto show_usage; |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 160 | |
| 161 | /* parse the behavior */ |
| 162 | switch (*str_cmd) { |
Simon Glass | e70271f | 2019-01-21 14:53:20 -0700 | [diff] [blame] | 163 | case 'i': |
| 164 | sub_cmd = GPIOC_INPUT; |
| 165 | break; |
| 166 | case 's': |
| 167 | sub_cmd = GPIOC_SET; |
| 168 | break; |
| 169 | case 'c': |
| 170 | sub_cmd = GPIOC_CLEAR; |
| 171 | break; |
| 172 | case 't': |
| 173 | sub_cmd = GPIOC_TOGGLE; |
| 174 | break; |
| 175 | default: |
| 176 | goto show_usage; |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 179 | #if defined(CONFIG_DM_GPIO) |
| 180 | /* |
| 181 | * TODO(sjg@chromium.org): For now we must fit into the existing GPIO |
| 182 | * framework, so we look up the name here and convert it to a GPIO number. |
| 183 | * Once all GPIO drivers are converted to driver model, we can change the |
| 184 | * code here to use the GPIO uclass interface instead of the numbered |
| 185 | * GPIO compatibility layer. |
| 186 | */ |
| 187 | ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio); |
Simon Glass | f5330a4 | 2016-01-21 19:44:50 -0700 | [diff] [blame] | 188 | if (ret) { |
| 189 | printf("GPIO: '%s' not found\n", str_gpio); |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 190 | return cmd_process_error(cmdtp, ret); |
Simon Glass | f5330a4 | 2016-01-21 19:44:50 -0700 | [diff] [blame] | 191 | } |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 192 | #else |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 193 | /* turn the gpio name into a gpio number */ |
| 194 | gpio = name_to_gpio(str_gpio); |
| 195 | if (gpio < 0) |
| 196 | goto show_usage; |
Simon Glass | 459c47a | 2014-02-26 15:59:26 -0700 | [diff] [blame] | 197 | #endif |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 198 | /* grab the pin before we tweak it */ |
Simon Glass | 23a6b8e | 2014-08-11 09:23:53 -0600 | [diff] [blame] | 199 | ret = gpio_request(gpio, "cmd_gpio"); |
| 200 | if (ret && ret != -EBUSY) { |
Mike Frysinger | 15b2550 | 2011-04-12 03:02:11 -0400 | [diff] [blame] | 201 | printf("gpio: requesting pin %u failed\n", gpio); |
| 202 | return -1; |
| 203 | } |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 204 | |
| 205 | /* finally, let's do it: set direction and exec command */ |
Simon Glass | e70271f | 2019-01-21 14:53:20 -0700 | [diff] [blame] | 206 | if (sub_cmd == GPIOC_INPUT) { |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 207 | gpio_direction_input(gpio); |
| 208 | value = gpio_get_value(gpio); |
| 209 | } else { |
| 210 | switch (sub_cmd) { |
Simon Glass | e70271f | 2019-01-21 14:53:20 -0700 | [diff] [blame] | 211 | case GPIOC_SET: |
Simon Glass | db96baa | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 212 | value = 1; |
| 213 | break; |
Simon Glass | e70271f | 2019-01-21 14:53:20 -0700 | [diff] [blame] | 214 | case GPIOC_CLEAR: |
Simon Glass | db96baa | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 215 | value = 0; |
| 216 | break; |
Simon Glass | e70271f | 2019-01-21 14:53:20 -0700 | [diff] [blame] | 217 | case GPIOC_TOGGLE: |
Simon Glass | db96baa | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 218 | value = gpio_get_value(gpio); |
| 219 | if (!IS_ERR_VALUE(value)) |
| 220 | value = !value; |
| 221 | break; |
| 222 | default: |
| 223 | goto show_usage; |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 224 | } |
| 225 | gpio_direction_output(gpio, value); |
| 226 | } |
Heinrich Schuchardt | ec8dfc3 | 2019-01-06 11:31:54 +0100 | [diff] [blame] | 227 | printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio); |
Luka Kovacic | a01e78d | 2020-01-05 20:10:56 +0100 | [diff] [blame] | 228 | |
| 229 | if (IS_ERR_VALUE(value)) { |
Simon Glass | db96baa | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 230 | printf("unknown (ret=%d)\n", value); |
Luka Kovacic | a01e78d | 2020-01-05 20:10:56 +0100 | [diff] [blame] | 231 | goto err; |
| 232 | } else { |
Simon Glass | db96baa | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 233 | printf("%d\n", value); |
Luka Kovacic | a01e78d | 2020-01-05 20:10:56 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Simon Glass | e70271f | 2019-01-21 14:53:20 -0700 | [diff] [blame] | 236 | if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)) { |
Simon Glass | db96baa | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 237 | int nval = gpio_get_value(gpio); |
| 238 | |
Luka Kovacic | a01e78d | 2020-01-05 20:10:56 +0100 | [diff] [blame] | 239 | if (IS_ERR_VALUE(nval)) { |
Simon Glass | db96baa | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 240 | printf(" Warning: no access to GPIO output value\n"); |
Luka Kovacic | a01e78d | 2020-01-05 20:10:56 +0100 | [diff] [blame] | 241 | goto err; |
| 242 | } else if (nval != value) { |
Simon Glass | db96baa | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 243 | printf(" Warning: value of pin is still %d\n", nval); |
Luka Kovacic | a01e78d | 2020-01-05 20:10:56 +0100 | [diff] [blame] | 244 | goto err; |
| 245 | } |
Simon Glass | db96baa | 2015-08-31 18:55:36 -0600 | [diff] [blame] | 246 | } |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 247 | |
Simon Glass | 23a6b8e | 2014-08-11 09:23:53 -0600 | [diff] [blame] | 248 | if (ret != -EBUSY) |
| 249 | gpio_free(gpio); |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 250 | |
Alex Kiernan | 1073c23 | 2020-03-11 08:46:29 +0000 | [diff] [blame] | 251 | /* |
| 252 | * Whilst wrong, the legacy gpio input command returns the pin |
| 253 | * value, or CMD_RET_FAILURE (which is indistinguishable from a |
| 254 | * valid pin value). |
| 255 | */ |
| 256 | return (sub_cmd == GPIOC_INPUT) ? value : CMD_RET_SUCCESS; |
Luka Kovacic | a01e78d | 2020-01-05 20:10:56 +0100 | [diff] [blame] | 257 | |
| 258 | err: |
| 259 | if (ret != -EBUSY) |
| 260 | gpio_free(gpio); |
| 261 | return CMD_RET_FAILURE; |
Mike Frysinger | 0e71f80 | 2011-04-03 04:40:46 -0400 | [diff] [blame] | 262 | } |
| 263 | |
Simon Glass | 2d6b923 | 2014-08-11 09:23:52 -0600 | [diff] [blame] | 264 | U_BOOT_CMD(gpio, 4, 0, do_gpio, |
| 265 | "query and control gpio pins", |
| 266 | "<input|set|clear|toggle> <pin>\n" |
| 267 | " - input/set/clear/toggle the specified pin\n" |
| 268 | "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs"); |