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