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