]>
Commit | Line | Data |
---|---|---|
a972b8d7 MF |
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 <common.h> | |
10 | #include <command.h> | |
9165e842 | 11 | #include <errno.h> |
95a260a9 | 12 | #include <dm.h> |
f7ae49fc | 13 | #include <log.h> |
336d4615 | 14 | #include <malloc.h> |
dd2b8c11 DR |
15 | #ifdef CONFIG_CMD_GPIO_READ |
16 | #include <env.h> | |
17 | #endif | |
a972b8d7 | 18 | #include <asm/gpio.h> |
61b29b82 | 19 | #include <linux/err.h> |
a972b8d7 | 20 | |
5b5ac645 | 21 | __weak int name_to_gpio(const char *name) |
fd11bea2 | 22 | { |
0b1284eb | 23 | return dectoul(name, NULL); |
fd11bea2 | 24 | } |
a972b8d7 MF |
25 | |
26 | enum gpio_cmd { | |
66f657d1 SG |
27 | GPIOC_INPUT, |
28 | GPIOC_SET, | |
29 | GPIOC_CLEAR, | |
30 | GPIOC_TOGGLE, | |
dd2b8c11 DR |
31 | #ifdef CONFIG_CMD_GPIO_READ |
32 | GPIOC_READ, | |
33 | #endif | |
a972b8d7 MF |
34 | }; |
35 | ||
95a260a9 | 36 | #if defined(CONFIG_DM_GPIO) && !defined(gpio_status) |
95a260a9 | 37 | |
89e64054 SG |
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 | ||
0757535a | 45 | static void gpio_get_description(struct udevice *dev, const char *bank_name, |
b8989b53 | 46 | int offset, int *flagsp, bool show_all) |
95a260a9 | 47 | { |
95a260a9 SG |
48 | char buf[80]; |
49 | int ret; | |
50 | ||
0757535a SG |
51 | ret = gpio_get_function(dev, offset, NULL); |
52 | if (ret < 0) | |
53 | goto err; | |
b8989b53 | 54 | if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED) |
89e64054 SG |
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 | } | |
95a260a9 | 64 | |
0757535a SG |
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 | printf("Error %d\n", ret); | |
95a260a9 SG |
73 | } |
74 | ||
89e64054 | 75 | static int do_gpio_status(bool all, const char *gpio_name) |
95a260a9 | 76 | { |
54c5d08a | 77 | struct udevice *dev; |
89e64054 SG |
78 | int banklen; |
79 | int flags; | |
95a260a9 SG |
80 | int ret; |
81 | ||
89e64054 | 82 | flags = 0; |
95a260a9 SG |
83 | if (gpio_name && !*gpio_name) |
84 | gpio_name = NULL; | |
85 | for (ret = uclass_first_device(UCLASS_GPIO, &dev); | |
86 | dev; | |
87 | ret = uclass_next_device(&dev)) { | |
88 | const char *bank_name; | |
89 | int num_bits; | |
90 | ||
89e64054 SG |
91 | flags |= FLAG_SHOW_BANK; |
92 | if (all) | |
93 | flags |= FLAG_SHOW_ALL; | |
95a260a9 | 94 | bank_name = gpio_get_bank_info(dev, &num_bits); |
0757535a SG |
95 | if (!num_bits) { |
96 | debug("GPIO device %s has no bits\n", dev->name); | |
89e64054 | 97 | continue; |
0757535a | 98 | } |
89e64054 | 99 | banklen = bank_name ? strlen(bank_name) : 0; |
95a260a9 SG |
100 | |
101 | if (!gpio_name || !bank_name || | |
48ca6909 | 102 | !strncasecmp(gpio_name, bank_name, banklen)) { |
e946b5d2 | 103 | const char *p; |
95a260a9 SG |
104 | int offset; |
105 | ||
89e64054 SG |
106 | p = gpio_name + banklen; |
107 | if (gpio_name && *p) { | |
0b1284eb | 108 | offset = dectoul(p, NULL); |
0757535a | 109 | gpio_get_description(dev, bank_name, offset, |
b8989b53 | 110 | &flags, true); |
95a260a9 | 111 | } else { |
89e64054 | 112 | for (offset = 0; offset < num_bits; offset++) { |
0757535a | 113 | gpio_get_description(dev, bank_name, |
b8989b53 | 114 | offset, &flags, false); |
89e64054 | 115 | } |
95a260a9 SG |
116 | } |
117 | } | |
89e64054 SG |
118 | /* Add a newline between bank names */ |
119 | if (!(flags & FLAG_SHOW_BANK)) | |
120 | flags |= FLAG_SHOW_NEWLINE; | |
95a260a9 SG |
121 | } |
122 | ||
123 | return ret; | |
124 | } | |
125 | #endif | |
126 | ||
09140113 SG |
127 | static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc, |
128 | char *const argv[]) | |
a972b8d7 | 129 | { |
95a260a9 | 130 | unsigned int gpio; |
a972b8d7 | 131 | enum gpio_cmd sub_cmd; |
b71bea71 | 132 | int value; |
95a260a9 | 133 | const char *str_cmd, *str_gpio = NULL; |
dd2b8c11 DR |
134 | #ifdef CONFIG_CMD_GPIO_READ |
135 | const char *str_var = NULL; | |
136 | #endif | |
9165e842 | 137 | int ret; |
95a260a9 | 138 | #ifdef CONFIG_DM_GPIO |
89e64054 | 139 | bool all = false; |
95a260a9 | 140 | #endif |
a972b8d7 | 141 | |
95a260a9 SG |
142 | if (argc < 2) |
143 | show_usage: | |
144 | return CMD_RET_USAGE; | |
145 | str_cmd = argv[1]; | |
89e64054 SG |
146 | argc -= 2; |
147 | argv += 2; | |
148 | #ifdef CONFIG_DM_GPIO | |
dd2b8c11 | 149 | if (argc > 0 && !strncmp(str_cmd, "status", 2) && !strcmp(*argv, "-a")) { |
89e64054 SG |
150 | all = true; |
151 | argc--; | |
152 | argv++; | |
153 | } | |
dd2b8c11 DR |
154 | #endif |
155 | #ifdef CONFIG_CMD_GPIO_READ | |
156 | if (argc > 0 && !strncmp(str_cmd, "read", 2)) { | |
157 | if (argc < 2) | |
158 | goto show_usage; | |
159 | str_var = *argv; | |
160 | argc--; | |
161 | argv++; | |
162 | } | |
89e64054 SG |
163 | #endif |
164 | if (argc > 0) | |
165 | str_gpio = *argv; | |
4c80c53c | 166 | if (!strncmp(str_cmd, "status", 2)) { |
95a260a9 | 167 | /* Support deprecated gpio_status() */ |
a972b8d7 | 168 | #ifdef gpio_status |
a972b8d7 MF |
169 | gpio_status(); |
170 | return 0; | |
95a260a9 | 171 | #elif defined(CONFIG_DM_GPIO) |
89e64054 | 172 | return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio)); |
95a260a9 SG |
173 | #else |
174 | goto show_usage; | |
a972b8d7 | 175 | #endif |
95a260a9 | 176 | } |
a972b8d7 | 177 | |
95a260a9 SG |
178 | if (!str_gpio) |
179 | goto show_usage; | |
a972b8d7 MF |
180 | |
181 | /* parse the behavior */ | |
182 | switch (*str_cmd) { | |
66f657d1 SG |
183 | case 'i': |
184 | sub_cmd = GPIOC_INPUT; | |
185 | break; | |
186 | case 's': | |
187 | sub_cmd = GPIOC_SET; | |
188 | break; | |
189 | case 'c': | |
190 | sub_cmd = GPIOC_CLEAR; | |
191 | break; | |
192 | case 't': | |
193 | sub_cmd = GPIOC_TOGGLE; | |
194 | break; | |
dd2b8c11 DR |
195 | #ifdef CONFIG_CMD_GPIO_READ |
196 | case 'r': | |
197 | sub_cmd = GPIOC_READ; | |
198 | break; | |
199 | #endif | |
66f657d1 SG |
200 | default: |
201 | goto show_usage; | |
a972b8d7 MF |
202 | } |
203 | ||
95a260a9 SG |
204 | #if defined(CONFIG_DM_GPIO) |
205 | /* | |
206 | * TODO([email protected]): For now we must fit into the existing GPIO | |
207 | * framework, so we look up the name here and convert it to a GPIO number. | |
208 | * Once all GPIO drivers are converted to driver model, we can change the | |
209 | * code here to use the GPIO uclass interface instead of the numbered | |
210 | * GPIO compatibility layer. | |
211 | */ | |
212 | ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio); | |
66280e82 SG |
213 | if (ret) { |
214 | printf("GPIO: '%s' not found\n", str_gpio); | |
95a260a9 | 215 | return cmd_process_error(cmdtp, ret); |
66280e82 | 216 | } |
95a260a9 | 217 | #else |
a972b8d7 MF |
218 | /* turn the gpio name into a gpio number */ |
219 | gpio = name_to_gpio(str_gpio); | |
220 | if (gpio < 0) | |
221 | goto show_usage; | |
95a260a9 | 222 | #endif |
a972b8d7 | 223 | /* grab the pin before we tweak it */ |
9165e842 SG |
224 | ret = gpio_request(gpio, "cmd_gpio"); |
225 | if (ret && ret != -EBUSY) { | |
6801201e MF |
226 | printf("gpio: requesting pin %u failed\n", gpio); |
227 | return -1; | |
228 | } | |
a972b8d7 MF |
229 | |
230 | /* finally, let's do it: set direction and exec command */ | |
dd2b8c11 DR |
231 | if (sub_cmd == GPIOC_INPUT |
232 | #ifdef CONFIG_CMD_GPIO_READ | |
233 | || sub_cmd == GPIOC_READ | |
234 | #endif | |
235 | ) { | |
a972b8d7 MF |
236 | gpio_direction_input(gpio); |
237 | value = gpio_get_value(gpio); | |
238 | } else { | |
239 | switch (sub_cmd) { | |
66f657d1 | 240 | case GPIOC_SET: |
b71bea71 SG |
241 | value = 1; |
242 | break; | |
66f657d1 | 243 | case GPIOC_CLEAR: |
b71bea71 SG |
244 | value = 0; |
245 | break; | |
66f657d1 | 246 | case GPIOC_TOGGLE: |
b71bea71 SG |
247 | value = gpio_get_value(gpio); |
248 | if (!IS_ERR_VALUE(value)) | |
249 | value = !value; | |
250 | break; | |
251 | default: | |
252 | goto show_usage; | |
a972b8d7 MF |
253 | } |
254 | gpio_direction_output(gpio, value); | |
255 | } | |
b1970013 | 256 | printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio); |
4dbc107f LK |
257 | |
258 | if (IS_ERR_VALUE(value)) { | |
b71bea71 | 259 | printf("unknown (ret=%d)\n", value); |
4dbc107f LK |
260 | goto err; |
261 | } else { | |
b71bea71 | 262 | printf("%d\n", value); |
dd2b8c11 DR |
263 | #ifdef CONFIG_CMD_GPIO_READ |
264 | if (sub_cmd == GPIOC_READ) | |
265 | env_set_ulong(str_var, (ulong)value); | |
266 | #endif | |
4dbc107f LK |
267 | } |
268 | ||
dd2b8c11 DR |
269 | if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value) |
270 | #ifdef CONFIG_CMD_GPIO_READ | |
271 | && sub_cmd != GPIOC_READ | |
272 | #endif | |
273 | ) { | |
b71bea71 SG |
274 | int nval = gpio_get_value(gpio); |
275 | ||
4dbc107f | 276 | if (IS_ERR_VALUE(nval)) { |
b71bea71 | 277 | printf(" Warning: no access to GPIO output value\n"); |
4dbc107f LK |
278 | goto err; |
279 | } else if (nval != value) { | |
b71bea71 | 280 | printf(" Warning: value of pin is still %d\n", nval); |
4dbc107f LK |
281 | goto err; |
282 | } | |
b71bea71 | 283 | } |
a972b8d7 | 284 | |
9165e842 SG |
285 | if (ret != -EBUSY) |
286 | gpio_free(gpio); | |
a972b8d7 | 287 | |
4af2a33e AK |
288 | /* |
289 | * Whilst wrong, the legacy gpio input command returns the pin | |
290 | * value, or CMD_RET_FAILURE (which is indistinguishable from a | |
291 | * valid pin value). | |
292 | */ | |
293 | return (sub_cmd == GPIOC_INPUT) ? value : CMD_RET_SUCCESS; | |
4dbc107f LK |
294 | |
295 | err: | |
296 | if (ret != -EBUSY) | |
297 | gpio_free(gpio); | |
298 | return CMD_RET_FAILURE; | |
a972b8d7 MF |
299 | } |
300 | ||
89e64054 SG |
301 | U_BOOT_CMD(gpio, 4, 0, do_gpio, |
302 | "query and control gpio pins", | |
303 | "<input|set|clear|toggle> <pin>\n" | |
304 | " - input/set/clear/toggle the specified pin\n" | |
dd2b8c11 DR |
305 | #ifdef CONFIG_CMD_GPIO_READ |
306 | "gpio read <name> <pin>\n" | |
307 | " - set environment variable 'name' to the specified pin\n" | |
308 | #endif | |
89e64054 | 309 | "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs"); |