]>
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> |
a972b8d7 | 13 | #include <asm/gpio.h> |
61b29b82 | 14 | #include <linux/err.h> |
a972b8d7 | 15 | |
5b5ac645 | 16 | __weak int name_to_gpio(const char *name) |
fd11bea2 IC |
17 | { |
18 | return simple_strtoul(name, NULL, 10); | |
19 | } | |
a972b8d7 MF |
20 | |
21 | enum gpio_cmd { | |
66f657d1 SG |
22 | GPIOC_INPUT, |
23 | GPIOC_SET, | |
24 | GPIOC_CLEAR, | |
25 | GPIOC_TOGGLE, | |
a972b8d7 MF |
26 | }; |
27 | ||
95a260a9 | 28 | #if defined(CONFIG_DM_GPIO) && !defined(gpio_status) |
95a260a9 | 29 | |
89e64054 SG |
30 | /* A few flags used by show_gpio() */ |
31 | enum { | |
32 | FLAG_SHOW_ALL = 1 << 0, | |
33 | FLAG_SHOW_BANK = 1 << 1, | |
34 | FLAG_SHOW_NEWLINE = 1 << 2, | |
35 | }; | |
36 | ||
0757535a | 37 | static void gpio_get_description(struct udevice *dev, const char *bank_name, |
b8989b53 | 38 | int offset, int *flagsp, bool show_all) |
95a260a9 | 39 | { |
95a260a9 SG |
40 | char buf[80]; |
41 | int ret; | |
42 | ||
0757535a SG |
43 | ret = gpio_get_function(dev, offset, NULL); |
44 | if (ret < 0) | |
45 | goto err; | |
b8989b53 | 46 | if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED) |
89e64054 SG |
47 | return; |
48 | if ((*flagsp & FLAG_SHOW_BANK) && bank_name) { | |
49 | if (*flagsp & FLAG_SHOW_NEWLINE) { | |
50 | putc('\n'); | |
51 | *flagsp &= ~FLAG_SHOW_NEWLINE; | |
52 | } | |
53 | printf("Bank %s:\n", bank_name); | |
54 | *flagsp &= ~FLAG_SHOW_BANK; | |
55 | } | |
95a260a9 | 56 | |
0757535a SG |
57 | ret = gpio_get_status(dev, offset, buf, sizeof(buf)); |
58 | if (ret) | |
59 | goto err; | |
60 | ||
61 | printf("%s\n", buf); | |
62 | return; | |
63 | err: | |
64 | printf("Error %d\n", ret); | |
95a260a9 SG |
65 | } |
66 | ||
89e64054 | 67 | static int do_gpio_status(bool all, const char *gpio_name) |
95a260a9 | 68 | { |
54c5d08a | 69 | struct udevice *dev; |
89e64054 SG |
70 | int banklen; |
71 | int flags; | |
95a260a9 SG |
72 | int ret; |
73 | ||
89e64054 | 74 | flags = 0; |
95a260a9 SG |
75 | if (gpio_name && !*gpio_name) |
76 | gpio_name = NULL; | |
77 | for (ret = uclass_first_device(UCLASS_GPIO, &dev); | |
78 | dev; | |
79 | ret = uclass_next_device(&dev)) { | |
80 | const char *bank_name; | |
81 | int num_bits; | |
82 | ||
89e64054 SG |
83 | flags |= FLAG_SHOW_BANK; |
84 | if (all) | |
85 | flags |= FLAG_SHOW_ALL; | |
95a260a9 | 86 | bank_name = gpio_get_bank_info(dev, &num_bits); |
0757535a SG |
87 | if (!num_bits) { |
88 | debug("GPIO device %s has no bits\n", dev->name); | |
89e64054 | 89 | continue; |
0757535a | 90 | } |
89e64054 | 91 | banklen = bank_name ? strlen(bank_name) : 0; |
95a260a9 SG |
92 | |
93 | if (!gpio_name || !bank_name || | |
48ca6909 | 94 | !strncasecmp(gpio_name, bank_name, banklen)) { |
e946b5d2 | 95 | const char *p; |
95a260a9 SG |
96 | int offset; |
97 | ||
89e64054 SG |
98 | p = gpio_name + banklen; |
99 | if (gpio_name && *p) { | |
95a260a9 | 100 | offset = simple_strtoul(p, NULL, 10); |
0757535a | 101 | gpio_get_description(dev, bank_name, offset, |
b8989b53 | 102 | &flags, true); |
95a260a9 | 103 | } else { |
89e64054 | 104 | for (offset = 0; offset < num_bits; offset++) { |
0757535a | 105 | gpio_get_description(dev, bank_name, |
b8989b53 | 106 | offset, &flags, false); |
89e64054 | 107 | } |
95a260a9 SG |
108 | } |
109 | } | |
89e64054 SG |
110 | /* Add a newline between bank names */ |
111 | if (!(flags & FLAG_SHOW_BANK)) | |
112 | flags |= FLAG_SHOW_NEWLINE; | |
95a260a9 SG |
113 | } |
114 | ||
115 | return ret; | |
116 | } | |
117 | #endif | |
118 | ||
a972b8d7 MF |
119 | static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
120 | { | |
95a260a9 | 121 | unsigned int gpio; |
a972b8d7 | 122 | enum gpio_cmd sub_cmd; |
b71bea71 | 123 | int value; |
95a260a9 | 124 | const char *str_cmd, *str_gpio = NULL; |
9165e842 | 125 | int ret; |
95a260a9 | 126 | #ifdef CONFIG_DM_GPIO |
89e64054 | 127 | bool all = false; |
95a260a9 | 128 | #endif |
a972b8d7 | 129 | |
95a260a9 SG |
130 | if (argc < 2) |
131 | show_usage: | |
132 | return CMD_RET_USAGE; | |
133 | str_cmd = argv[1]; | |
89e64054 SG |
134 | argc -= 2; |
135 | argv += 2; | |
136 | #ifdef CONFIG_DM_GPIO | |
137 | if (argc > 0 && !strcmp(*argv, "-a")) { | |
138 | all = true; | |
139 | argc--; | |
140 | argv++; | |
141 | } | |
142 | #endif | |
143 | if (argc > 0) | |
144 | str_gpio = *argv; | |
4c80c53c | 145 | if (!strncmp(str_cmd, "status", 2)) { |
95a260a9 | 146 | /* Support deprecated gpio_status() */ |
a972b8d7 | 147 | #ifdef gpio_status |
a972b8d7 MF |
148 | gpio_status(); |
149 | return 0; | |
95a260a9 | 150 | #elif defined(CONFIG_DM_GPIO) |
89e64054 | 151 | return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio)); |
95a260a9 SG |
152 | #else |
153 | goto show_usage; | |
a972b8d7 | 154 | #endif |
95a260a9 | 155 | } |
a972b8d7 | 156 | |
95a260a9 SG |
157 | if (!str_gpio) |
158 | goto show_usage; | |
a972b8d7 MF |
159 | |
160 | /* parse the behavior */ | |
161 | switch (*str_cmd) { | |
66f657d1 SG |
162 | case 'i': |
163 | sub_cmd = GPIOC_INPUT; | |
164 | break; | |
165 | case 's': | |
166 | sub_cmd = GPIOC_SET; | |
167 | break; | |
168 | case 'c': | |
169 | sub_cmd = GPIOC_CLEAR; | |
170 | break; | |
171 | case 't': | |
172 | sub_cmd = GPIOC_TOGGLE; | |
173 | break; | |
174 | default: | |
175 | goto show_usage; | |
a972b8d7 MF |
176 | } |
177 | ||
95a260a9 SG |
178 | #if defined(CONFIG_DM_GPIO) |
179 | /* | |
180 | * TODO([email protected]): For now we must fit into the existing GPIO | |
181 | * framework, so we look up the name here and convert it to a GPIO number. | |
182 | * Once all GPIO drivers are converted to driver model, we can change the | |
183 | * code here to use the GPIO uclass interface instead of the numbered | |
184 | * GPIO compatibility layer. | |
185 | */ | |
186 | ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio); | |
66280e82 SG |
187 | if (ret) { |
188 | printf("GPIO: '%s' not found\n", str_gpio); | |
95a260a9 | 189 | return cmd_process_error(cmdtp, ret); |
66280e82 | 190 | } |
95a260a9 | 191 | #else |
a972b8d7 MF |
192 | /* turn the gpio name into a gpio number */ |
193 | gpio = name_to_gpio(str_gpio); | |
194 | if (gpio < 0) | |
195 | goto show_usage; | |
95a260a9 | 196 | #endif |
a972b8d7 | 197 | /* grab the pin before we tweak it */ |
9165e842 SG |
198 | ret = gpio_request(gpio, "cmd_gpio"); |
199 | if (ret && ret != -EBUSY) { | |
6801201e MF |
200 | printf("gpio: requesting pin %u failed\n", gpio); |
201 | return -1; | |
202 | } | |
a972b8d7 MF |
203 | |
204 | /* finally, let's do it: set direction and exec command */ | |
66f657d1 | 205 | if (sub_cmd == GPIOC_INPUT) { |
a972b8d7 MF |
206 | gpio_direction_input(gpio); |
207 | value = gpio_get_value(gpio); | |
208 | } else { | |
209 | switch (sub_cmd) { | |
66f657d1 | 210 | case GPIOC_SET: |
b71bea71 SG |
211 | value = 1; |
212 | break; | |
66f657d1 | 213 | case GPIOC_CLEAR: |
b71bea71 SG |
214 | value = 0; |
215 | break; | |
66f657d1 | 216 | case GPIOC_TOGGLE: |
b71bea71 SG |
217 | value = gpio_get_value(gpio); |
218 | if (!IS_ERR_VALUE(value)) | |
219 | value = !value; | |
220 | break; | |
221 | default: | |
222 | goto show_usage; | |
a972b8d7 MF |
223 | } |
224 | gpio_direction_output(gpio, value); | |
225 | } | |
b1970013 | 226 | printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio); |
b71bea71 SG |
227 | if (IS_ERR_VALUE(value)) |
228 | printf("unknown (ret=%d)\n", value); | |
229 | else | |
230 | printf("%d\n", value); | |
66f657d1 | 231 | if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)) { |
b71bea71 SG |
232 | int nval = gpio_get_value(gpio); |
233 | ||
234 | if (IS_ERR_VALUE(nval)) | |
235 | printf(" Warning: no access to GPIO output value\n"); | |
236 | else if (nval != value) | |
237 | printf(" Warning: value of pin is still %d\n", nval); | |
238 | } | |
a972b8d7 | 239 | |
9165e842 SG |
240 | if (ret != -EBUSY) |
241 | gpio_free(gpio); | |
a972b8d7 MF |
242 | |
243 | return value; | |
244 | } | |
245 | ||
89e64054 SG |
246 | U_BOOT_CMD(gpio, 4, 0, do_gpio, |
247 | "query and control gpio pins", | |
248 | "<input|set|clear|toggle> <pin>\n" | |
249 | " - input/set/clear/toggle the specified pin\n" | |
250 | "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs"); |