blob: 65d6df451c6d4f4c5dc9c26a94a02a536b42386e [file] [log] [blame]
Mike Frysinger0e71f802011-04-03 04:40:46 -04001/*
2 * Control GPIO pins on the fly
3 *
4 * Copyright (c) 2008-2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <common.h>
10#include <command.h>
Simon Glass23a6b8e2014-08-11 09:23:53 -060011#include <errno.h>
Simon Glass459c47a2014-02-26 15:59:26 -070012#include <dm.h>
Mike Frysinger0e71f802011-04-03 04:40:46 -040013#include <asm/gpio.h>
14
Jeroen Hofsteeb613b102014-10-08 22:58:03 +020015__weak int name_to_gpio(const char *name)
Ian Campbell87af5e12014-03-27 20:34:13 +000016{
17 return simple_strtoul(name, NULL, 10);
18}
Mike Frysinger0e71f802011-04-03 04:40:46 -040019
20enum gpio_cmd {
21 GPIO_INPUT,
22 GPIO_SET,
23 GPIO_CLEAR,
24 GPIO_TOGGLE,
25};
26
Simon Glass459c47a2014-02-26 15:59:26 -070027#if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
Simon Glass459c47a2014-02-26 15:59:26 -070028
Simon Glass2d6b9232014-08-11 09:23:52 -060029/* A few flags used by show_gpio() */
30enum {
31 FLAG_SHOW_ALL = 1 << 0,
32 FLAG_SHOW_BANK = 1 << 1,
33 FLAG_SHOW_NEWLINE = 1 << 2,
34};
35
Simon Glass6b1ef592014-10-04 11:29:44 -060036static void gpio_get_description(struct udevice *dev, const char *bank_name,
37 int offset, int *flagsp)
Simon Glass459c47a2014-02-26 15:59:26 -070038{
Simon Glass459c47a2014-02-26 15:59:26 -070039 char buf[80];
40 int ret;
41
Simon Glass6b1ef592014-10-04 11:29:44 -060042 ret = gpio_get_function(dev, offset, NULL);
43 if (ret < 0)
44 goto err;
45 if (!(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED)
Simon Glass2d6b9232014-08-11 09:23:52 -060046 return;
47 if ((*flagsp & FLAG_SHOW_BANK) && bank_name) {
48 if (*flagsp & FLAG_SHOW_NEWLINE) {
49 putc('\n');
50 *flagsp &= ~FLAG_SHOW_NEWLINE;
51 }
52 printf("Bank %s:\n", bank_name);
53 *flagsp &= ~FLAG_SHOW_BANK;
54 }
Simon Glass459c47a2014-02-26 15:59:26 -070055
Simon Glass6b1ef592014-10-04 11:29:44 -060056 ret = gpio_get_status(dev, offset, buf, sizeof(buf));
57 if (ret)
58 goto err;
59
60 printf("%s\n", buf);
61 return;
62err:
63 printf("Error %d\n", ret);
Simon Glass459c47a2014-02-26 15:59:26 -070064}
65
Simon Glass2d6b9232014-08-11 09:23:52 -060066static int do_gpio_status(bool all, const char *gpio_name)
Simon Glass459c47a2014-02-26 15:59:26 -070067{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020068 struct udevice *dev;
Simon Glass2d6b9232014-08-11 09:23:52 -060069 int banklen;
70 int flags;
Simon Glass459c47a2014-02-26 15:59:26 -070071 int ret;
72
Simon Glass2d6b9232014-08-11 09:23:52 -060073 flags = 0;
Simon Glass459c47a2014-02-26 15:59:26 -070074 if (gpio_name && !*gpio_name)
75 gpio_name = NULL;
76 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
77 dev;
78 ret = uclass_next_device(&dev)) {
79 const char *bank_name;
80 int num_bits;
81
Simon Glass2d6b9232014-08-11 09:23:52 -060082 flags |= FLAG_SHOW_BANK;
83 if (all)
84 flags |= FLAG_SHOW_ALL;
Simon Glass459c47a2014-02-26 15:59:26 -070085 bank_name = gpio_get_bank_info(dev, &num_bits);
Simon Glass6b1ef592014-10-04 11:29:44 -060086 if (!num_bits) {
87 debug("GPIO device %s has no bits\n", dev->name);
Simon Glass2d6b9232014-08-11 09:23:52 -060088 continue;
Simon Glass6b1ef592014-10-04 11:29:44 -060089 }
Simon Glass2d6b9232014-08-11 09:23:52 -060090 banklen = bank_name ? strlen(bank_name) : 0;
Simon Glass459c47a2014-02-26 15:59:26 -070091
92 if (!gpio_name || !bank_name ||
Simon Glass2d6b9232014-08-11 09:23:52 -060093 !strncmp(gpio_name, bank_name, banklen)) {
Simon Glass459c47a2014-02-26 15:59:26 -070094 const char *p = NULL;
95 int offset;
96
Simon Glass2d6b9232014-08-11 09:23:52 -060097 p = gpio_name + banklen;
98 if (gpio_name && *p) {
Simon Glass459c47a2014-02-26 15:59:26 -070099 offset = simple_strtoul(p, NULL, 10);
Simon Glass6b1ef592014-10-04 11:29:44 -0600100 gpio_get_description(dev, bank_name, offset,
101 &flags);
Simon Glass459c47a2014-02-26 15:59:26 -0700102 } else {
Simon Glass2d6b9232014-08-11 09:23:52 -0600103 for (offset = 0; offset < num_bits; offset++) {
Simon Glass6b1ef592014-10-04 11:29:44 -0600104 gpio_get_description(dev, bank_name,
105 offset, &flags);
Simon Glass2d6b9232014-08-11 09:23:52 -0600106 }
Simon Glass459c47a2014-02-26 15:59:26 -0700107 }
108 }
Simon Glass2d6b9232014-08-11 09:23:52 -0600109 /* Add a newline between bank names */
110 if (!(flags & FLAG_SHOW_BANK))
111 flags |= FLAG_SHOW_NEWLINE;
Simon Glass459c47a2014-02-26 15:59:26 -0700112 }
113
114 return ret;
115}
116#endif
117
Mike Frysinger0e71f802011-04-03 04:40:46 -0400118static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
119{
Simon Glass459c47a2014-02-26 15:59:26 -0700120 unsigned int gpio;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400121 enum gpio_cmd sub_cmd;
122 ulong value;
Simon Glass459c47a2014-02-26 15:59:26 -0700123 const char *str_cmd, *str_gpio = NULL;
Simon Glass23a6b8e2014-08-11 09:23:53 -0600124 int ret;
Simon Glass459c47a2014-02-26 15:59:26 -0700125#ifdef CONFIG_DM_GPIO
Simon Glass2d6b9232014-08-11 09:23:52 -0600126 bool all = false;
Simon Glass459c47a2014-02-26 15:59:26 -0700127#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -0400128
Simon Glass459c47a2014-02-26 15:59:26 -0700129 if (argc < 2)
130 show_usage:
131 return CMD_RET_USAGE;
132 str_cmd = argv[1];
Simon Glass2d6b9232014-08-11 09:23:52 -0600133 argc -= 2;
134 argv += 2;
135#ifdef CONFIG_DM_GPIO
136 if (argc > 0 && !strcmp(*argv, "-a")) {
137 all = true;
138 argc--;
139 argv++;
140 }
141#endif
142 if (argc > 0)
143 str_gpio = *argv;
Simon Glass459c47a2014-02-26 15:59:26 -0700144 if (!strcmp(str_cmd, "status")) {
145 /* Support deprecated gpio_status() */
Mike Frysinger0e71f802011-04-03 04:40:46 -0400146#ifdef gpio_status
Mike Frysinger0e71f802011-04-03 04:40:46 -0400147 gpio_status();
148 return 0;
Simon Glass459c47a2014-02-26 15:59:26 -0700149#elif defined(CONFIG_DM_GPIO)
Simon Glass2d6b9232014-08-11 09:23:52 -0600150 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
Simon Glass459c47a2014-02-26 15:59:26 -0700151#else
152 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400153#endif
Simon Glass459c47a2014-02-26 15:59:26 -0700154 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400155
Simon Glass459c47a2014-02-26 15:59:26 -0700156 if (!str_gpio)
157 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400158
159 /* parse the behavior */
160 switch (*str_cmd) {
161 case 'i': sub_cmd = GPIO_INPUT; break;
162 case 's': sub_cmd = GPIO_SET; break;
163 case 'c': sub_cmd = GPIO_CLEAR; break;
164 case 't': sub_cmd = GPIO_TOGGLE; break;
165 default: goto show_usage;
166 }
167
Simon Glass459c47a2014-02-26 15:59:26 -0700168#if defined(CONFIG_DM_GPIO)
169 /*
170 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
171 * framework, so we look up the name here and convert it to a GPIO number.
172 * Once all GPIO drivers are converted to driver model, we can change the
173 * code here to use the GPIO uclass interface instead of the numbered
174 * GPIO compatibility layer.
175 */
176 ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
177 if (ret)
178 return cmd_process_error(cmdtp, ret);
179#else
Mike Frysinger0e71f802011-04-03 04:40:46 -0400180 /* turn the gpio name into a gpio number */
181 gpio = name_to_gpio(str_gpio);
182 if (gpio < 0)
183 goto show_usage;
Simon Glass459c47a2014-02-26 15:59:26 -0700184#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -0400185 /* grab the pin before we tweak it */
Simon Glass23a6b8e2014-08-11 09:23:53 -0600186 ret = gpio_request(gpio, "cmd_gpio");
187 if (ret && ret != -EBUSY) {
Mike Frysinger15b25502011-04-12 03:02:11 -0400188 printf("gpio: requesting pin %u failed\n", gpio);
189 return -1;
190 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400191
192 /* finally, let's do it: set direction and exec command */
193 if (sub_cmd == GPIO_INPUT) {
194 gpio_direction_input(gpio);
195 value = gpio_get_value(gpio);
196 } else {
197 switch (sub_cmd) {
198 case GPIO_SET: value = 1; break;
199 case GPIO_CLEAR: value = 0; break;
200 case GPIO_TOGGLE: value = !gpio_get_value(gpio); break;
201 default: goto show_usage;
202 }
203 gpio_direction_output(gpio, value);
204 }
205 printf("gpio: pin %s (gpio %i) value is %lu\n",
206 str_gpio, gpio, value);
207
Simon Glass23a6b8e2014-08-11 09:23:53 -0600208 if (ret != -EBUSY)
209 gpio_free(gpio);
Mike Frysinger0e71f802011-04-03 04:40:46 -0400210
211 return value;
212}
213
Simon Glass2d6b9232014-08-11 09:23:52 -0600214U_BOOT_CMD(gpio, 4, 0, do_gpio,
215 "query and control gpio pins",
216 "<input|set|clear|toggle> <pin>\n"
217 " - input/set/clear/toggle the specified pin\n"
218 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");