2 * Control GPIO pins on the fly
4 * Copyright (c) 2008-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
15 #ifdef CONFIG_CMD_GPIO_READ
19 #include <linux/err.h>
21 __weak int name_to_gpio(const char *name)
23 return dectoul(name, NULL);
31 #ifdef CONFIG_CMD_GPIO_READ
36 #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
38 /* A few flags used by show_gpio() */
40 FLAG_SHOW_ALL = 1 << 0,
41 FLAG_SHOW_BANK = 1 << 1,
42 FLAG_SHOW_NEWLINE = 1 << 2,
45 static void gpio_get_description(struct udevice *dev, const char *bank_name,
46 int offset, int *flagsp, bool show_all)
51 ret = gpio_get_function(dev, offset, NULL);
54 if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED)
56 if ((*flagsp & FLAG_SHOW_BANK) && bank_name) {
57 if (*flagsp & FLAG_SHOW_NEWLINE) {
59 *flagsp &= ~FLAG_SHOW_NEWLINE;
61 printf("Bank %s:\n", bank_name);
62 *flagsp &= ~FLAG_SHOW_BANK;
65 ret = gpio_get_status(dev, offset, buf, sizeof(buf));
72 printf("Error %d\n", ret);
75 static int do_gpio_status(bool all, const char *gpio_name)
83 if (gpio_name && !*gpio_name)
85 for (ret = uclass_first_device_check(UCLASS_GPIO, &dev);
87 ret = uclass_next_device_check(&dev)) {
88 const char *bank_name;
92 printf("GPIO device %s probe error %i\n",
98 flags |= FLAG_SHOW_BANK;
100 flags |= FLAG_SHOW_ALL;
101 bank_name = gpio_get_bank_info(dev, &num_bits);
103 debug("GPIO device %s has no bits\n", dev->name);
106 banklen = bank_name ? strlen(bank_name) : 0;
108 if (!gpio_name || !bank_name ||
109 !strncasecmp(gpio_name, bank_name, banklen)) {
113 p = gpio_name + banklen;
114 if (gpio_name && *p) {
115 offset = dectoul(p, NULL);
116 gpio_get_description(dev, bank_name, offset,
119 for (offset = 0; offset < num_bits; offset++) {
120 gpio_get_description(dev, bank_name,
121 offset, &flags, false);
125 /* Add a newline between bank names */
126 if (!(flags & FLAG_SHOW_BANK))
127 flags |= FLAG_SHOW_NEWLINE;
134 static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
138 enum gpio_cmd sub_cmd;
140 const char *str_cmd, *str_gpio = NULL;
141 #ifdef CONFIG_CMD_GPIO_READ
142 const char *str_var = NULL;
145 #ifdef CONFIG_DM_GPIO
151 return CMD_RET_USAGE;
155 #ifdef CONFIG_DM_GPIO
156 if (argc > 0 && !strncmp(str_cmd, "status", 2) && !strcmp(*argv, "-a")) {
162 #ifdef CONFIG_CMD_GPIO_READ
163 if (argc > 0 && !strncmp(str_cmd, "read", 2)) {
173 if (!strncmp(str_cmd, "status", 2)) {
174 /* Support deprecated gpio_status() */
178 #elif defined(CONFIG_DM_GPIO)
179 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
188 /* parse the behavior */
191 sub_cmd = GPIOC_INPUT;
197 sub_cmd = GPIOC_CLEAR;
200 sub_cmd = GPIOC_TOGGLE;
202 #ifdef CONFIG_CMD_GPIO_READ
204 sub_cmd = GPIOC_READ;
211 #if defined(CONFIG_DM_GPIO)
213 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
214 * framework, so we look up the name here and convert it to a GPIO number.
215 * Once all GPIO drivers are converted to driver model, we can change the
216 * code here to use the GPIO uclass interface instead of the numbered
217 * GPIO compatibility layer.
219 ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
221 printf("GPIO: '%s' not found\n", str_gpio);
222 return cmd_process_error(cmdtp, ret);
225 /* turn the gpio name into a gpio number */
226 gpio = name_to_gpio(str_gpio);
230 /* grab the pin before we tweak it */
231 ret = gpio_request(gpio, "cmd_gpio");
232 if (ret && ret != -EBUSY) {
233 printf("gpio: requesting pin %u failed\n", gpio);
237 /* finally, let's do it: set direction and exec command */
238 if (sub_cmd == GPIOC_INPUT
239 #ifdef CONFIG_CMD_GPIO_READ
240 || sub_cmd == GPIOC_READ
243 gpio_direction_input(gpio);
244 value = gpio_get_value(gpio);
254 value = gpio_get_value(gpio);
255 if (!IS_ERR_VALUE(value))
261 gpio_direction_output(gpio, value);
263 printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio);
265 if (IS_ERR_VALUE(value)) {
266 printf("unknown (ret=%d)\n", value);
269 printf("%d\n", value);
270 #ifdef CONFIG_CMD_GPIO_READ
271 if (sub_cmd == GPIOC_READ)
272 env_set_ulong(str_var, (ulong)value);
276 if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)
277 #ifdef CONFIG_CMD_GPIO_READ
278 && sub_cmd != GPIOC_READ
281 int nval = gpio_get_value(gpio);
283 if (IS_ERR_VALUE(nval)) {
284 printf(" Warning: no access to GPIO output value\n");
286 } else if (nval != value) {
287 printf(" Warning: value of pin is still %d\n", nval);
296 * Whilst wrong, the legacy gpio input command returns the pin
297 * value, or CMD_RET_FAILURE (which is indistinguishable from a
300 return (sub_cmd == GPIOC_INPUT) ? value : CMD_RET_SUCCESS;
305 return CMD_RET_FAILURE;
308 U_BOOT_CMD(gpio, 4, 0, do_gpio,
309 "query and control gpio pins",
310 "<input|set|clear|toggle> <pin>\n"
311 " - input/set/clear/toggle the specified pin\n"
312 #ifdef CONFIG_CMD_GPIO_READ
313 "gpio read <name> <pin>\n"
314 " - set environment variable 'name' to the specified pin\n"
316 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");