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