2 * Control GPIO pins on the fly
4 * Copyright (c) 2008-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
14 #include <linux/err.h>
16 __weak int name_to_gpio(const char *name)
18 return simple_strtoul(name, NULL, 10);
28 #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
30 /* A few flags used by show_gpio() */
32 FLAG_SHOW_ALL = 1 << 0,
33 FLAG_SHOW_BANK = 1 << 1,
34 FLAG_SHOW_NEWLINE = 1 << 2,
37 static void gpio_get_description(struct udevice *dev, const char *bank_name,
38 int offset, int *flagsp, bool show_all)
43 ret = gpio_get_function(dev, offset, NULL);
46 if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED)
48 if ((*flagsp & FLAG_SHOW_BANK) && bank_name) {
49 if (*flagsp & FLAG_SHOW_NEWLINE) {
51 *flagsp &= ~FLAG_SHOW_NEWLINE;
53 printf("Bank %s:\n", bank_name);
54 *flagsp &= ~FLAG_SHOW_BANK;
57 ret = gpio_get_status(dev, offset, buf, sizeof(buf));
64 printf("Error %d\n", ret);
67 static int do_gpio_status(bool all, const char *gpio_name)
75 if (gpio_name && !*gpio_name)
77 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
79 ret = uclass_next_device(&dev)) {
80 const char *bank_name;
83 flags |= FLAG_SHOW_BANK;
85 flags |= FLAG_SHOW_ALL;
86 bank_name = gpio_get_bank_info(dev, &num_bits);
88 debug("GPIO device %s has no bits\n", dev->name);
91 banklen = bank_name ? strlen(bank_name) : 0;
93 if (!gpio_name || !bank_name ||
94 !strncasecmp(gpio_name, bank_name, banklen)) {
98 p = gpio_name + banklen;
99 if (gpio_name && *p) {
100 offset = simple_strtoul(p, NULL, 10);
101 gpio_get_description(dev, bank_name, offset,
104 for (offset = 0; offset < num_bits; offset++) {
105 gpio_get_description(dev, bank_name,
106 offset, &flags, false);
110 /* Add a newline between bank names */
111 if (!(flags & FLAG_SHOW_BANK))
112 flags |= FLAG_SHOW_NEWLINE;
119 static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
122 enum gpio_cmd sub_cmd;
124 const char *str_cmd, *str_gpio = NULL;
126 #ifdef CONFIG_DM_GPIO
132 return CMD_RET_USAGE;
136 #ifdef CONFIG_DM_GPIO
137 if (argc > 0 && !strcmp(*argv, "-a")) {
145 if (!strncmp(str_cmd, "status", 2)) {
146 /* Support deprecated gpio_status() */
150 #elif defined(CONFIG_DM_GPIO)
151 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
160 /* parse the behavior */
163 sub_cmd = GPIOC_INPUT;
169 sub_cmd = GPIOC_CLEAR;
172 sub_cmd = GPIOC_TOGGLE;
178 #if defined(CONFIG_DM_GPIO)
180 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
181 * framework, so we look up the name here and convert it to a GPIO number.
182 * Once all GPIO drivers are converted to driver model, we can change the
183 * code here to use the GPIO uclass interface instead of the numbered
184 * GPIO compatibility layer.
186 ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
188 printf("GPIO: '%s' not found\n", str_gpio);
189 return cmd_process_error(cmdtp, ret);
192 /* turn the gpio name into a gpio number */
193 gpio = name_to_gpio(str_gpio);
197 /* grab the pin before we tweak it */
198 ret = gpio_request(gpio, "cmd_gpio");
199 if (ret && ret != -EBUSY) {
200 printf("gpio: requesting pin %u failed\n", gpio);
204 /* finally, let's do it: set direction and exec command */
205 if (sub_cmd == GPIOC_INPUT) {
206 gpio_direction_input(gpio);
207 value = gpio_get_value(gpio);
217 value = gpio_get_value(gpio);
218 if (!IS_ERR_VALUE(value))
224 gpio_direction_output(gpio, value);
226 printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio);
227 if (IS_ERR_VALUE(value))
228 printf("unknown (ret=%d)\n", value);
230 printf("%d\n", value);
231 if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)) {
232 int nval = gpio_get_value(gpio);
234 if (IS_ERR_VALUE(nval))
235 printf(" Warning: no access to GPIO output value\n");
236 else if (nval != value)
237 printf(" Warning: value of pin is still %d\n", nval);
246 U_BOOT_CMD(gpio, 4, 0, do_gpio,
247 "query and control gpio pins",
248 "<input|set|clear|toggle> <pin>\n"
249 " - input/set/clear/toggle the specified pin\n"
250 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");