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