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