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