2 * Control GPIO pins on the fly
4 * Copyright (c) 2008-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
14 #ifdef CONFIG_CMD_GPIO_READ
18 #include <linux/err.h>
19 #include <dm/device_compat.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));
73 printf("Error %d\n", ret);
76 static int do_gpio_status(bool all, const char *gpio_name)
84 if (gpio_name && !*gpio_name)
86 for (ret = uclass_first_device_check(UCLASS_GPIO, &dev);
88 ret = uclass_next_device_check(&dev)) {
89 const char *bank_name;
93 printf("GPIO device %s probe error %i\n",
99 flags |= FLAG_SHOW_BANK;
101 flags |= FLAG_SHOW_ALL;
102 bank_name = gpio_get_bank_info(dev, &num_bits);
104 debug("GPIO device %s has no bits\n", dev->name);
107 banklen = bank_name ? strlen(bank_name) : 0;
109 if (!gpio_name || !bank_name ||
110 !strncasecmp(gpio_name, bank_name, banklen)) {
114 p = gpio_name + banklen;
115 if (gpio_name && *p) {
116 offset = dectoul(p, NULL);
117 gpio_get_description(dev, bank_name, offset,
120 for (offset = 0; offset < num_bits; offset++) {
121 gpio_get_description(dev, bank_name,
122 offset, &flags, false);
126 /* Add a newline between bank names */
127 if (!(flags & FLAG_SHOW_BANK))
128 flags |= FLAG_SHOW_NEWLINE;
135 static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
139 enum gpio_cmd sub_cmd;
141 const char *str_cmd, *str_gpio = NULL;
142 #ifdef CONFIG_CMD_GPIO_READ
143 const char *str_var = NULL;
146 #ifdef CONFIG_DM_GPIO
152 return CMD_RET_USAGE;
156 #ifdef CONFIG_DM_GPIO
157 if (argc > 0 && !strncmp(str_cmd, "status", 2) && !strcmp(*argv, "-a")) {
163 #ifdef CONFIG_CMD_GPIO_READ
164 if (argc > 0 && !strncmp(str_cmd, "read", 2)) {
174 if (!strncmp(str_cmd, "status", 2)) {
175 /* Support deprecated gpio_status() */
179 #elif defined(CONFIG_DM_GPIO)
180 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
189 /* parse the behavior */
192 sub_cmd = GPIOC_INPUT;
198 sub_cmd = GPIOC_CLEAR;
201 sub_cmd = GPIOC_TOGGLE;
203 #ifdef CONFIG_CMD_GPIO_READ
205 sub_cmd = GPIOC_READ;
212 #if defined(CONFIG_DM_GPIO)
215 * framework, so we look up the name here and convert it to a GPIO number.
216 * Once all GPIO drivers are converted to driver model, we can change the
217 * code here to use the GPIO uclass interface instead of the numbered
218 * GPIO compatibility layer.
220 ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
222 printf("GPIO: '%s' not found\n", str_gpio);
223 return cmd_process_error(cmdtp, ret);
226 /* turn the gpio name into a gpio number */
227 gpio = name_to_gpio(str_gpio);
231 /* grab the pin before we tweak it */
232 ret = gpio_request(gpio, "cmd_gpio");
233 if (ret && ret != -EBUSY) {
234 printf("gpio: requesting pin %u failed\n", gpio);
238 /* finally, let's do it: set direction and exec command */
239 if (sub_cmd == GPIOC_INPUT
240 #ifdef CONFIG_CMD_GPIO_READ
241 || sub_cmd == GPIOC_READ
244 gpio_direction_input(gpio);
245 value = gpio_get_value(gpio);
255 value = gpio_get_value(gpio);
256 if (!IS_ERR_VALUE(value))
262 gpio_direction_output(gpio, value);
264 printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio);
266 if (IS_ERR_VALUE(value)) {
267 printf("unknown (ret=%d)\n", value);
270 printf("%d\n", value);
271 #ifdef CONFIG_CMD_GPIO_READ
272 if (sub_cmd == GPIOC_READ)
273 env_set_ulong(str_var, (ulong)value);
277 if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)
278 #ifdef CONFIG_CMD_GPIO_READ
279 && sub_cmd != GPIOC_READ
282 int nval = gpio_get_value(gpio);
284 if (IS_ERR_VALUE(nval)) {
285 printf(" Warning: no access to GPIO output value\n");
287 } else if (nval != value) {
288 printf(" Warning: value of pin is still %d\n", nval);
297 * Whilst wrong, the legacy gpio input command returns the pin
298 * value, or CMD_RET_FAILURE (which is indistinguishable from a
301 return (sub_cmd == GPIOC_INPUT) ? value : CMD_RET_SUCCESS;
306 return CMD_RET_FAILURE;
309 U_BOOT_CMD(gpio, 4, 0, do_gpio,
310 "query and control gpio pins",
311 "<input|set|clear|toggle> <pin>\n"
312 " - input/set/clear/toggle the specified pin\n"
313 #ifdef CONFIG_CMD_GPIO_READ
314 "gpio read <name> <pin>\n"
315 " - set environment variable 'name' to the specified pin\n"
317 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");