2 * Control GPIO pins on the fly
4 * Copyright (c) 2008-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
15 #include <linux/err.h>
17 __weak int name_to_gpio(const char *name)
19 return simple_strtoul(name, NULL, 10);
29 #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
31 /* A few flags used by show_gpio() */
33 FLAG_SHOW_ALL = 1 << 0,
34 FLAG_SHOW_BANK = 1 << 1,
35 FLAG_SHOW_NEWLINE = 1 << 2,
38 static void gpio_get_description(struct udevice *dev, const char *bank_name,
39 int offset, int *flagsp, bool show_all)
44 ret = gpio_get_function(dev, offset, NULL);
47 if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED)
49 if ((*flagsp & FLAG_SHOW_BANK) && bank_name) {
50 if (*flagsp & FLAG_SHOW_NEWLINE) {
52 *flagsp &= ~FLAG_SHOW_NEWLINE;
54 printf("Bank %s:\n", bank_name);
55 *flagsp &= ~FLAG_SHOW_BANK;
58 ret = gpio_get_status(dev, offset, buf, sizeof(buf));
65 printf("Error %d\n", ret);
68 static int do_gpio_status(bool all, const char *gpio_name)
76 if (gpio_name && !*gpio_name)
78 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
80 ret = uclass_next_device(&dev)) {
81 const char *bank_name;
84 flags |= FLAG_SHOW_BANK;
86 flags |= FLAG_SHOW_ALL;
87 bank_name = gpio_get_bank_info(dev, &num_bits);
89 debug("GPIO device %s has no bits\n", dev->name);
92 banklen = bank_name ? strlen(bank_name) : 0;
94 if (!gpio_name || !bank_name ||
95 !strncasecmp(gpio_name, bank_name, banklen)) {
99 p = gpio_name + banklen;
100 if (gpio_name && *p) {
101 offset = simple_strtoul(p, NULL, 10);
102 gpio_get_description(dev, bank_name, offset,
105 for (offset = 0; offset < num_bits; offset++) {
106 gpio_get_description(dev, bank_name,
107 offset, &flags, false);
111 /* Add a newline between bank names */
112 if (!(flags & FLAG_SHOW_BANK))
113 flags |= FLAG_SHOW_NEWLINE;
120 static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
123 enum gpio_cmd sub_cmd;
125 const char *str_cmd, *str_gpio = NULL;
127 #ifdef CONFIG_DM_GPIO
133 return CMD_RET_USAGE;
137 #ifdef CONFIG_DM_GPIO
138 if (argc > 0 && !strcmp(*argv, "-a")) {
146 if (!strncmp(str_cmd, "status", 2)) {
147 /* Support deprecated gpio_status() */
151 #elif defined(CONFIG_DM_GPIO)
152 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
161 /* parse the behavior */
164 sub_cmd = GPIOC_INPUT;
170 sub_cmd = GPIOC_CLEAR;
173 sub_cmd = GPIOC_TOGGLE;
179 #if defined(CONFIG_DM_GPIO)
181 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
182 * framework, so we look up the name here and convert it to a GPIO number.
183 * Once all GPIO drivers are converted to driver model, we can change the
184 * code here to use the GPIO uclass interface instead of the numbered
185 * GPIO compatibility layer.
187 ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
189 printf("GPIO: '%s' not found\n", str_gpio);
190 return cmd_process_error(cmdtp, ret);
193 /* turn the gpio name into a gpio number */
194 gpio = name_to_gpio(str_gpio);
198 /* grab the pin before we tweak it */
199 ret = gpio_request(gpio, "cmd_gpio");
200 if (ret && ret != -EBUSY) {
201 printf("gpio: requesting pin %u failed\n", gpio);
205 /* finally, let's do it: set direction and exec command */
206 if (sub_cmd == GPIOC_INPUT) {
207 gpio_direction_input(gpio);
208 value = gpio_get_value(gpio);
218 value = gpio_get_value(gpio);
219 if (!IS_ERR_VALUE(value))
225 gpio_direction_output(gpio, value);
227 printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio);
229 if (IS_ERR_VALUE(value)) {
230 printf("unknown (ret=%d)\n", value);
233 printf("%d\n", value);
236 if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)) {
237 int nval = gpio_get_value(gpio);
239 if (IS_ERR_VALUE(nval)) {
240 printf(" Warning: no access to GPIO output value\n");
242 } else if (nval != value) {
243 printf(" Warning: value of pin is still %d\n", nval);
252 * Whilst wrong, the legacy gpio input command returns the pin
253 * value, or CMD_RET_FAILURE (which is indistinguishable from a
256 return (sub_cmd == GPIOC_INPUT) ? value : CMD_RET_SUCCESS;
261 return CMD_RET_FAILURE;
264 U_BOOT_CMD(gpio, 4, 0, do_gpio,
265 "query and control gpio pins",
266 "<input|set|clear|toggle> <pin>\n"
267 " - input/set/clear/toggle the specified pin\n"
268 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");