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