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