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