1 /*
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 <command.h>
10 #include <errno.h>
11 #include <dm.h>
12 #include <log.h>
13 #include <malloc.h>
14 #ifdef CONFIG_CMD_GPIO_READ
15 #include <env.h>
16 #endif
17 #include <asm/gpio.h>
18 #include <linux/err.h>
19 #include <dm/device_compat.h>
20
name_to_gpio(const char * name)21 __weak int name_to_gpio(const char *name)
22 {
23 return dectoul(name, NULL);
24 }
25
26 enum gpio_cmd {
27 GPIOC_INPUT,
28 GPIOC_SET,
29 GPIOC_CLEAR,
30 GPIOC_TOGGLE,
31 #ifdef CONFIG_CMD_GPIO_READ
32 GPIOC_READ,
33 #endif
34 };
35
36 #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
37
38 /* A few flags used by show_gpio() */
39 enum {
40 FLAG_SHOW_ALL = 1 << 0,
41 FLAG_SHOW_BANK = 1 << 1,
42 FLAG_SHOW_NEWLINE = 1 << 2,
43 };
44
gpio_get_description(struct udevice * dev,const char * bank_name,int offset,int * flagsp,bool show_all)45 static void gpio_get_description(struct udevice *dev, const char *bank_name,
46 int offset, int *flagsp, bool show_all)
47 {
48 char buf[80];
49 int ret;
50
51 ret = gpio_get_function(dev, offset, NULL);
52 if (ret < 0)
53 goto err;
54 if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED)
55 return;
56 if ((*flagsp & FLAG_SHOW_BANK) && bank_name) {
57 if (*flagsp & FLAG_SHOW_NEWLINE) {
58 putc('\n');
59 *flagsp &= ~FLAG_SHOW_NEWLINE;
60 }
61 printf("Bank %s:\n", bank_name);
62 *flagsp &= ~FLAG_SHOW_BANK;
63 }
64
65 ret = gpio_get_status(dev, offset, buf, sizeof(buf));
66 if (ret)
67 goto err;
68
69 printf("%s\n", buf);
70 return;
71 err:
72 if (ret != -ENOENT)
73 printf("Error %d\n", ret);
74 }
75
do_gpio_status(bool all,const char * gpio_name)76 static int do_gpio_status(bool all, const char *gpio_name)
77 {
78 struct udevice *dev;
79 int banklen;
80 int flags;
81 int ret, err = 0;
82
83 flags = 0;
84 if (gpio_name && !*gpio_name)
85 gpio_name = NULL;
86 for (ret = uclass_first_device_check(UCLASS_GPIO, &dev);
87 dev;
88 ret = uclass_next_device_check(&dev)) {
89 const char *bank_name;
90 int num_bits;
91
92 if (ret) {
93 printf("GPIO device %s probe error %i\n",
94 dev->name, ret);
95 err = ret;
96 continue;
97 }
98
99 flags |= FLAG_SHOW_BANK;
100 if (all)
101 flags |= FLAG_SHOW_ALL;
102 bank_name = gpio_get_bank_info(dev, &num_bits);
103 if (!num_bits) {
104 debug("GPIO device %s has no bits\n", dev->name);
105 continue;
106 }
107 banklen = bank_name ? strlen(bank_name) : 0;
108
109 if (!gpio_name || !bank_name ||
110 !strncasecmp(gpio_name, bank_name, banklen)) {
111 const char *p;
112 int offset;
113
114 p = gpio_name + banklen;
115 if (gpio_name && *p) {
116 offset = dectoul(p, NULL);
117 gpio_get_description(dev, bank_name, offset,
118 &flags, true);
119 } else {
120 for (offset = 0; offset < num_bits; offset++) {
121 gpio_get_description(dev, bank_name,
122 offset, &flags, false);
123 }
124 }
125 }
126 /* Add a newline between bank names */
127 if (!(flags & FLAG_SHOW_BANK))
128 flags |= FLAG_SHOW_NEWLINE;
129 }
130
131 return err;
132 }
133 #endif
134
do_gpio(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])135 static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
136 char *const argv[])
137 {
138 unsigned int gpio;
139 enum gpio_cmd sub_cmd;
140 int value;
141 const char *str_cmd, *str_gpio = NULL;
142 #ifdef CONFIG_CMD_GPIO_READ
143 const char *str_var = NULL;
144 #endif
145 int ret;
146 #ifdef CONFIG_DM_GPIO
147 bool all = false;
148 #endif
149
150 if (argc < 2)
151 show_usage:
152 return CMD_RET_USAGE;
153 str_cmd = argv[1];
154 argc -= 2;
155 argv += 2;
156 #ifdef CONFIG_DM_GPIO
157 if (argc > 0 && !strncmp(str_cmd, "status", 2) && !strcmp(*argv, "-a")) {
158 all = true;
159 argc--;
160 argv++;
161 }
162 #endif
163 #ifdef CONFIG_CMD_GPIO_READ
164 if (argc > 0 && !strncmp(str_cmd, "read", 2)) {
165 if (argc < 2)
166 goto show_usage;
167 str_var = *argv;
168 argc--;
169 argv++;
170 }
171 #endif
172 if (argc > 0)
173 str_gpio = *argv;
174 if (!strncmp(str_cmd, "status", 2)) {
175 /* Support deprecated gpio_status() */
176 #ifdef gpio_status
177 gpio_status();
178 return 0;
179 #elif defined(CONFIG_DM_GPIO)
180 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
181 #else
182 goto show_usage;
183 #endif
184 }
185
186 if (!str_gpio)
187 goto show_usage;
188
189 /* parse the behavior */
190 switch (*str_cmd) {
191 case 'i':
192 sub_cmd = GPIOC_INPUT;
193 break;
194 case 's':
195 sub_cmd = GPIOC_SET;
196 break;
197 case 'c':
198 sub_cmd = GPIOC_CLEAR;
199 break;
200 case 't':
201 sub_cmd = GPIOC_TOGGLE;
202 break;
203 #ifdef CONFIG_CMD_GPIO_READ
204 case 'r':
205 sub_cmd = GPIOC_READ;
206 break;
207 #endif
208 default:
209 goto show_usage;
210 }
211
212 #if defined(CONFIG_DM_GPIO)
213 /*
214 * TODO(sjg@chromium.org): For now we must fit into the existing 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.
219 */
220 ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
221 if (ret) {
222 printf("GPIO: '%s' not found\n", str_gpio);
223 return cmd_process_error(cmdtp, ret);
224 }
225 #else
226 /* turn the gpio name into a gpio number */
227 gpio = name_to_gpio(str_gpio);
228 if (gpio < 0)
229 goto show_usage;
230 #endif
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);
235 return -1;
236 }
237
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
242 #endif
243 ) {
244 gpio_direction_input(gpio);
245 value = gpio_get_value(gpio);
246 } else {
247 switch (sub_cmd) {
248 case GPIOC_SET:
249 value = 1;
250 break;
251 case GPIOC_CLEAR:
252 value = 0;
253 break;
254 case GPIOC_TOGGLE:
255 value = gpio_get_value(gpio);
256 if (!IS_ERR_VALUE(value))
257 value = !value;
258 break;
259 default:
260 goto show_usage;
261 }
262 gpio_direction_output(gpio, value);
263 }
264 printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio);
265
266 if (IS_ERR_VALUE(value)) {
267 printf("unknown (ret=%d)\n", value);
268 goto err;
269 } else {
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);
274 #endif
275 }
276
277 if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)
278 #ifdef CONFIG_CMD_GPIO_READ
279 && sub_cmd != GPIOC_READ
280 #endif
281 ) {
282 int nval = gpio_get_value(gpio);
283
284 if (IS_ERR_VALUE(nval)) {
285 printf(" Warning: no access to GPIO output value\n");
286 goto err;
287 } else if (nval != value) {
288 printf(" Warning: value of pin is still %d\n", nval);
289 goto err;
290 }
291 }
292
293 if (ret != -EBUSY)
294 gpio_free(gpio);
295
296 /*
297 * Whilst wrong, the legacy gpio input command returns the pin
298 * value, or CMD_RET_FAILURE (which is indistinguishable from a
299 * valid pin value).
300 */
301 return (sub_cmd == GPIOC_INPUT) ? value : CMD_RET_SUCCESS;
302
303 err:
304 if (ret != -EBUSY)
305 gpio_free(gpio);
306 return CMD_RET_FAILURE;
307 }
308
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"
316 #endif
317 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");
318