2 * Control GPIO pins on the fly
4 * Copyright (c) 2008-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
14 int __weak name_to_gpio(const char *name)
16 return simple_strtoul(name, NULL, 10);
26 #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
27 static const char * const gpio_function[GPIOF_COUNT] = {
35 /* A few flags used by show_gpio() */
37 FLAG_SHOW_ALL = 1 << 0,
38 FLAG_SHOW_BANK = 1 << 1,
39 FLAG_SHOW_NEWLINE = 1 << 2,
42 static void show_gpio(struct udevice *dev, const char *bank_name, int offset,
45 struct dm_gpio_ops *ops = gpio_get_ops(dev);
46 int func = GPIOF_UNKNOWN;
50 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
52 if (ops->get_function) {
53 ret = ops->get_function(dev, offset);
54 if (ret >= 0 && ret < ARRAY_SIZE(gpio_function))
57 if (!(*flagsp & FLAG_SHOW_ALL) && func == GPIOF_UNUSED)
59 if ((*flagsp & FLAG_SHOW_BANK) && bank_name) {
60 if (*flagsp & FLAG_SHOW_NEWLINE) {
62 *flagsp &= ~FLAG_SHOW_NEWLINE;
64 printf("Bank %s:\n", bank_name);
65 *flagsp &= ~FLAG_SHOW_BANK;
69 ret = ops->get_state(dev, offset, buf, sizeof(buf));
75 sprintf(buf, "%s%u: %8s %d", bank_name, offset,
76 gpio_function[func], ops->get_value(dev, offset));
83 static int do_gpio_status(bool all, const char *gpio_name)
91 if (gpio_name && !*gpio_name)
93 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
95 ret = uclass_next_device(&dev)) {
96 const char *bank_name;
99 flags |= FLAG_SHOW_BANK;
101 flags |= FLAG_SHOW_ALL;
102 bank_name = gpio_get_bank_info(dev, &num_bits);
105 banklen = bank_name ? strlen(bank_name) : 0;
107 if (!gpio_name || !bank_name ||
108 !strncmp(gpio_name, bank_name, banklen)) {
109 const char *p = NULL;
112 p = gpio_name + banklen;
113 if (gpio_name && *p) {
114 offset = simple_strtoul(p, NULL, 10);
115 show_gpio(dev, bank_name, offset, &flags);
117 for (offset = 0; offset < num_bits; offset++) {
118 show_gpio(dev, bank_name, offset,
123 /* Add a newline between bank names */
124 if (!(flags & FLAG_SHOW_BANK))
125 flags |= FLAG_SHOW_NEWLINE;
132 static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
135 enum gpio_cmd sub_cmd;
137 const char *str_cmd, *str_gpio = NULL;
138 #ifdef CONFIG_DM_GPIO
145 return CMD_RET_USAGE;
149 #ifdef CONFIG_DM_GPIO
150 if (argc > 0 && !strcmp(*argv, "-a")) {
158 if (!strcmp(str_cmd, "status")) {
159 /* Support deprecated gpio_status() */
163 #elif defined(CONFIG_DM_GPIO)
164 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
173 /* parse the behavior */
175 case 'i': sub_cmd = GPIO_INPUT; break;
176 case 's': sub_cmd = GPIO_SET; break;
177 case 'c': sub_cmd = GPIO_CLEAR; break;
178 case 't': sub_cmd = GPIO_TOGGLE; break;
179 default: goto show_usage;
182 #if defined(CONFIG_DM_GPIO)
184 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
185 * framework, so we look up the name here and convert it to a GPIO number.
186 * Once all GPIO drivers are converted to driver model, we can change the
187 * code here to use the GPIO uclass interface instead of the numbered
188 * GPIO compatibility layer.
190 ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
192 return cmd_process_error(cmdtp, ret);
194 /* turn the gpio name into a gpio number */
195 gpio = name_to_gpio(str_gpio);
199 /* grab the pin before we tweak it */
200 if (gpio_request(gpio, "cmd_gpio")) {
201 printf("gpio: requesting pin %u failed\n", gpio);
205 /* finally, let's do it: set direction and exec command */
206 if (sub_cmd == GPIO_INPUT) {
207 gpio_direction_input(gpio);
208 value = gpio_get_value(gpio);
211 case GPIO_SET: value = 1; break;
212 case GPIO_CLEAR: value = 0; break;
213 case GPIO_TOGGLE: value = !gpio_get_value(gpio); break;
214 default: goto show_usage;
216 gpio_direction_output(gpio, value);
218 printf("gpio: pin %s (gpio %i) value is %lu\n",
219 str_gpio, gpio, value);
226 U_BOOT_CMD(gpio, 4, 0, do_gpio,
227 "query and control gpio pins",
228 "<input|set|clear|toggle> <pin>\n"
229 " - input/set/clear/toggle the specified pin\n"
230 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");