Blame view
cmd/gpio.c
5.64 KB
a972b8d70 gpio: generalize ... |
1 2 3 4 5 6 7 8 9 10 |
/* * Control GPIO pins on the fly * * Copyright (c) 2008-2011 Analog Devices Inc. * * Licensed under the GPL-2 or later. */ #include <common.h> #include <command.h> |
9165e8428 dm: gpio: Allow g... |
11 |
#include <errno.h> |
95a260a98 dm: Enable gpio c... |
12 |
#include <dm.h> |
336d4615f dm: core: Create ... |
13 |
#include <malloc.h> |
a972b8d70 gpio: generalize ... |
14 |
#include <asm/gpio.h> |
61b29b826 dm: core: Require... |
15 |
#include <linux/err.h> |
a972b8d70 gpio: generalize ... |
16 |
|
5b5ac6456 gpio: add protype... |
17 |
__weak int name_to_gpio(const char *name) |
fd11bea2c blackfin: make na... |
18 19 20 |
{ return simple_strtoul(name, NULL, 10); } |
a972b8d70 gpio: generalize ... |
21 22 |
enum gpio_cmd { |
66f657d15 gpio: Use more co... |
23 24 25 26 |
GPIOC_INPUT, GPIOC_SET, GPIOC_CLEAR, GPIOC_TOGGLE, |
a972b8d70 gpio: generalize ... |
27 |
}; |
95a260a98 dm: Enable gpio c... |
28 |
#if defined(CONFIG_DM_GPIO) && !defined(gpio_status) |
95a260a98 dm: Enable gpio c... |
29 |
|
89e640542 dm: gpio: Enhance... |
30 31 32 33 34 35 |
/* A few flags used by show_gpio() */ enum { FLAG_SHOW_ALL = 1 << 0, FLAG_SHOW_BANK = 1 << 1, FLAG_SHOW_NEWLINE = 1 << 2, }; |
0757535a7 dm: Move the func... |
36 |
static void gpio_get_description(struct udevice *dev, const char *bank_name, |
b8989b537 gpio: Show inacti... |
37 |
int offset, int *flagsp, bool show_all) |
95a260a98 dm: Enable gpio c... |
38 |
{ |
95a260a98 dm: Enable gpio c... |
39 40 |
char buf[80]; int ret; |
0757535a7 dm: Move the func... |
41 42 43 |
ret = gpio_get_function(dev, offset, NULL); if (ret < 0) goto err; |
b8989b537 gpio: Show inacti... |
44 |
if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED) |
89e640542 dm: gpio: Enhance... |
45 46 47 48 49 50 51 52 53 54 55 |
return; if ((*flagsp & FLAG_SHOW_BANK) && bank_name) { if (*flagsp & FLAG_SHOW_NEWLINE) { putc(' '); *flagsp &= ~FLAG_SHOW_NEWLINE; } printf("Bank %s: ", bank_name); *flagsp &= ~FLAG_SHOW_BANK; } |
95a260a98 dm: Enable gpio c... |
56 |
|
0757535a7 dm: Move the func... |
57 58 59 60 61 62 63 64 65 66 |
ret = gpio_get_status(dev, offset, buf, sizeof(buf)); if (ret) goto err; printf("%s ", buf); return; err: printf("Error %d ", ret); |
95a260a98 dm: Enable gpio c... |
67 |
} |
89e640542 dm: gpio: Enhance... |
68 |
static int do_gpio_status(bool all, const char *gpio_name) |
95a260a98 dm: Enable gpio c... |
69 |
{ |
54c5d08a0 dm: rename device... |
70 |
struct udevice *dev; |
89e640542 dm: gpio: Enhance... |
71 72 |
int banklen; int flags; |
95a260a98 dm: Enable gpio c... |
73 |
int ret; |
89e640542 dm: gpio: Enhance... |
74 |
flags = 0; |
95a260a98 dm: Enable gpio c... |
75 76 77 78 79 80 81 |
if (gpio_name && !*gpio_name) gpio_name = NULL; for (ret = uclass_first_device(UCLASS_GPIO, &dev); dev; ret = uclass_next_device(&dev)) { const char *bank_name; int num_bits; |
89e640542 dm: gpio: Enhance... |
82 83 84 |
flags |= FLAG_SHOW_BANK; if (all) flags |= FLAG_SHOW_ALL; |
95a260a98 dm: Enable gpio c... |
85 |
bank_name = gpio_get_bank_info(dev, &num_bits); |
0757535a7 dm: Move the func... |
86 87 88 |
if (!num_bits) { debug("GPIO device %s has no bits ", dev->name); |
89e640542 dm: gpio: Enhance... |
89 |
continue; |
0757535a7 dm: Move the func... |
90 |
} |
89e640542 dm: gpio: Enhance... |
91 |
banklen = bank_name ? strlen(bank_name) : 0; |
95a260a98 dm: Enable gpio c... |
92 93 |
if (!gpio_name || !bank_name || |
48ca69091 gpio: Use case-in... |
94 |
!strncasecmp(gpio_name, bank_name, banklen)) { |
e946b5d25 cmd: gpio: remove... |
95 |
const char *p; |
95a260a98 dm: Enable gpio c... |
96 |
int offset; |
89e640542 dm: gpio: Enhance... |
97 98 |
p = gpio_name + banklen; if (gpio_name && *p) { |
95a260a98 dm: Enable gpio c... |
99 |
offset = simple_strtoul(p, NULL, 10); |
0757535a7 dm: Move the func... |
100 |
gpio_get_description(dev, bank_name, offset, |
b8989b537 gpio: Show inacti... |
101 |
&flags, true); |
95a260a98 dm: Enable gpio c... |
102 |
} else { |
89e640542 dm: gpio: Enhance... |
103 |
for (offset = 0; offset < num_bits; offset++) { |
0757535a7 dm: Move the func... |
104 |
gpio_get_description(dev, bank_name, |
b8989b537 gpio: Show inacti... |
105 |
offset, &flags, false); |
89e640542 dm: gpio: Enhance... |
106 |
} |
95a260a98 dm: Enable gpio c... |
107 108 |
} } |
89e640542 dm: gpio: Enhance... |
109 110 111 |
/* Add a newline between bank names */ if (!(flags & FLAG_SHOW_BANK)) flags |= FLAG_SHOW_NEWLINE; |
95a260a98 dm: Enable gpio c... |
112 113 114 115 116 |
} return ret; } #endif |
a972b8d70 gpio: generalize ... |
117 118 |
static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { |
95a260a98 dm: Enable gpio c... |
119 |
unsigned int gpio; |
a972b8d70 gpio: generalize ... |
120 |
enum gpio_cmd sub_cmd; |
b71bea712 gpio: Report erro... |
121 |
int value; |
95a260a98 dm: Enable gpio c... |
122 |
const char *str_cmd, *str_gpio = NULL; |
9165e8428 dm: gpio: Allow g... |
123 |
int ret; |
95a260a98 dm: Enable gpio c... |
124 |
#ifdef CONFIG_DM_GPIO |
89e640542 dm: gpio: Enhance... |
125 |
bool all = false; |
95a260a98 dm: Enable gpio c... |
126 |
#endif |
a972b8d70 gpio: generalize ... |
127 |
|
95a260a98 dm: Enable gpio c... |
128 129 130 131 |
if (argc < 2) show_usage: return CMD_RET_USAGE; str_cmd = argv[1]; |
89e640542 dm: gpio: Enhance... |
132 133 134 135 136 137 138 139 140 141 142 |
argc -= 2; argv += 2; #ifdef CONFIG_DM_GPIO if (argc > 0 && !strcmp(*argv, "-a")) { all = true; argc--; argv++; } #endif if (argc > 0) str_gpio = *argv; |
4c80c53c0 gpio: Correct han... |
143 |
if (!strncmp(str_cmd, "status", 2)) { |
95a260a98 dm: Enable gpio c... |
144 |
/* Support deprecated gpio_status() */ |
a972b8d70 gpio: generalize ... |
145 |
#ifdef gpio_status |
a972b8d70 gpio: generalize ... |
146 147 |
gpio_status(); return 0; |
95a260a98 dm: Enable gpio c... |
148 |
#elif defined(CONFIG_DM_GPIO) |
89e640542 dm: gpio: Enhance... |
149 |
return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio)); |
95a260a98 dm: Enable gpio c... |
150 151 |
#else goto show_usage; |
a972b8d70 gpio: generalize ... |
152 |
#endif |
95a260a98 dm: Enable gpio c... |
153 |
} |
a972b8d70 gpio: generalize ... |
154 |
|
95a260a98 dm: Enable gpio c... |
155 156 |
if (!str_gpio) goto show_usage; |
a972b8d70 gpio: generalize ... |
157 158 159 |
/* parse the behavior */ switch (*str_cmd) { |
66f657d15 gpio: Use more co... |
160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
case 'i': sub_cmd = GPIOC_INPUT; break; case 's': sub_cmd = GPIOC_SET; break; case 'c': sub_cmd = GPIOC_CLEAR; break; case 't': sub_cmd = GPIOC_TOGGLE; break; default: goto show_usage; |
a972b8d70 gpio: generalize ... |
174 |
} |
95a260a98 dm: Enable gpio c... |
175 176 177 178 179 180 181 182 183 |
#if defined(CONFIG_DM_GPIO) /* * TODO(sjg@chromium.org): For now we must fit into the existing GPIO * framework, so we look up the name here and convert it to a GPIO number. * Once all GPIO drivers are converted to driver model, we can change the * code here to use the GPIO uclass interface instead of the numbered * GPIO compatibility layer. */ ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio); |
66280e82f gpio: Warn about ... |
184 185 186 |
if (ret) { printf("GPIO: '%s' not found ", str_gpio); |
95a260a98 dm: Enable gpio c... |
187 |
return cmd_process_error(cmdtp, ret); |
66280e82f gpio: Warn about ... |
188 |
} |
95a260a98 dm: Enable gpio c... |
189 |
#else |
a972b8d70 gpio: generalize ... |
190 191 192 193 |
/* turn the gpio name into a gpio number */ gpio = name_to_gpio(str_gpio); if (gpio < 0) goto show_usage; |
95a260a98 dm: Enable gpio c... |
194 |
#endif |
a972b8d70 gpio: generalize ... |
195 |
/* grab the pin before we tweak it */ |
9165e8428 dm: gpio: Allow g... |
196 197 |
ret = gpio_request(gpio, "cmd_gpio"); if (ret && ret != -EBUSY) { |
6801201ee gpio: check reque... |
198 199 200 201 |
printf("gpio: requesting pin %u failed ", gpio); return -1; } |
a972b8d70 gpio: generalize ... |
202 203 |
/* finally, let's do it: set direction and exec command */ |
66f657d15 gpio: Use more co... |
204 |
if (sub_cmd == GPIOC_INPUT) { |
a972b8d70 gpio: generalize ... |
205 206 207 208 |
gpio_direction_input(gpio); value = gpio_get_value(gpio); } else { switch (sub_cmd) { |
66f657d15 gpio: Use more co... |
209 |
case GPIOC_SET: |
b71bea712 gpio: Report erro... |
210 211 |
value = 1; break; |
66f657d15 gpio: Use more co... |
212 |
case GPIOC_CLEAR: |
b71bea712 gpio: Report erro... |
213 214 |
value = 0; break; |
66f657d15 gpio: Use more co... |
215 |
case GPIOC_TOGGLE: |
b71bea712 gpio: Report erro... |
216 217 218 219 220 221 |
value = gpio_get_value(gpio); if (!IS_ERR_VALUE(value)) value = !value; break; default: goto show_usage; |
a972b8d70 gpio: generalize ... |
222 223 224 |
} gpio_direction_output(gpio, value); } |
b1970013d cmd: gpio: use co... |
225 |
printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio); |
4dbc107f4 cmd: gpio: Correc... |
226 227 |
if (IS_ERR_VALUE(value)) { |
b71bea712 gpio: Report erro... |
228 229 |
printf("unknown (ret=%d) ", value); |
4dbc107f4 cmd: gpio: Correc... |
230 231 |
goto err; } else { |
b71bea712 gpio: Report erro... |
232 233 |
printf("%d ", value); |
4dbc107f4 cmd: gpio: Correc... |
234 |
} |
66f657d15 gpio: Use more co... |
235 |
if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)) { |
b71bea712 gpio: Report erro... |
236 |
int nval = gpio_get_value(gpio); |
4dbc107f4 cmd: gpio: Correc... |
237 |
if (IS_ERR_VALUE(nval)) { |
b71bea712 gpio: Report erro... |
238 239 |
printf(" Warning: no access to GPIO output value "); |
4dbc107f4 cmd: gpio: Correc... |
240 241 |
goto err; } else if (nval != value) { |
b71bea712 gpio: Report erro... |
242 243 |
printf(" Warning: value of pin is still %d ", nval); |
4dbc107f4 cmd: gpio: Correc... |
244 245 |
goto err; } |
b71bea712 gpio: Report erro... |
246 |
} |
a972b8d70 gpio: generalize ... |
247 |
|
9165e8428 dm: gpio: Allow g... |
248 249 |
if (ret != -EBUSY) gpio_free(gpio); |
a972b8d70 gpio: generalize ... |
250 |
|
4af2a33ee cmd: gpio: Make `... |
251 252 253 254 255 256 |
/* * Whilst wrong, the legacy gpio input command returns the pin * value, or CMD_RET_FAILURE (which is indistinguishable from a * valid pin value). */ return (sub_cmd == GPIOC_INPUT) ? value : CMD_RET_SUCCESS; |
4dbc107f4 cmd: gpio: Correc... |
257 258 259 260 261 |
err: if (ret != -EBUSY) gpio_free(gpio); return CMD_RET_FAILURE; |
a972b8d70 gpio: generalize ... |
262 |
} |
89e640542 dm: gpio: Enhance... |
263 264 265 266 267 268 269 |
U_BOOT_CMD(gpio, 4, 0, do_gpio, "query and control gpio pins", "<input|set|clear|toggle> <pin> " " - input/set/clear/toggle the specified pin " "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs"); |