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