blob: 53e9ce666f94eff585dac7d12b3317ea61cd2a88 [file] [log] [blame]
Mike Frysingera972b8d2011-04-03 04:40:46 -04001/*
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>
Simon Glass9165e842014-08-11 09:23:53 -060011#include <errno.h>
Simon Glass95a260a92014-02-26 15:59:26 -070012#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070014#include <malloc.h>
Diego Rondinidd2b8c12022-04-11 12:02:09 +020015#ifdef CONFIG_CMD_GPIO_READ
16#include <env.h>
17#endif
Mike Frysingera972b8d2011-04-03 04:40:46 -040018#include <asm/gpio.h>
Simon Glass61b29b82020-02-03 07:36:15 -070019#include <linux/err.h>
Mike Frysingera972b8d2011-04-03 04:40:46 -040020
Jeroen Hofstee5b5ac642014-10-08 22:58:03 +020021__weak int name_to_gpio(const char *name)
Ian Campbellfd11bea2014-03-27 20:34:13 +000022{
Simon Glass0b1284e2021-07-24 09:03:30 -060023 return dectoul(name, NULL);
Ian Campbellfd11bea2014-03-27 20:34:13 +000024}
Mike Frysingera972b8d2011-04-03 04:40:46 -040025
26enum gpio_cmd {
Simon Glass66f657d2019-01-21 14:53:20 -070027 GPIOC_INPUT,
28 GPIOC_SET,
29 GPIOC_CLEAR,
30 GPIOC_TOGGLE,
Diego Rondinidd2b8c12022-04-11 12:02:09 +020031#ifdef CONFIG_CMD_GPIO_READ
32 GPIOC_READ,
33#endif
Mike Frysingera972b8d2011-04-03 04:40:46 -040034};
35
Simon Glass95a260a92014-02-26 15:59:26 -070036#if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
Mike Frysingera972b8d2011-04-03 04:40:46 -040037
Simon Glass89e64052014-08-11 09:23:52 -060038/* A few flags used by show_gpio() */
39enum {
40 FLAG_SHOW_ALL = 1 << 0,
41 FLAG_SHOW_BANK = 1 << 1,
42 FLAG_SHOW_NEWLINE = 1 << 2,
43};
44
Simon Glass07575352014-10-04 11:29:44 -060045static void gpio_get_description(struct udevice *dev, const char *bank_name,
Simon Glassb8989b52019-02-16 20:24:42 -070046 int offset, int *flagsp, bool show_all)
Simon Glass95a260a92014-02-26 15:59:26 -070047{
Simon Glass95a260a92014-02-26 15:59:26 -070048 char buf[80];
49 int ret;
50
Simon Glass07575352014-10-04 11:29:44 -060051 ret = gpio_get_function(dev, offset, NULL);
52 if (ret < 0)
53 goto err;
Simon Glassb8989b52019-02-16 20:24:42 -070054 if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED)
Simon Glass89e64052014-08-11 09:23:52 -060055 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 }
Simon Glass95a260a92014-02-26 15:59:26 -070064
Simon Glass07575352014-10-04 11:29:44 -060065 ret = gpio_get_status(dev, offset, buf, sizeof(buf));
66 if (ret)
67 goto err;
68
69 printf("%s\n", buf);
70 return;
71err:
72 printf("Error %d\n", ret);
Simon Glass95a260a92014-02-26 15:59:26 -070073}
74
Simon Glass89e64052014-08-11 09:23:52 -060075static int do_gpio_status(bool all, const char *gpio_name)
Simon Glass95a260a92014-02-26 15:59:26 -070076{
Heiko Schocher54c5d082014-05-22 12:43:05 +020077 struct udevice *dev;
Simon Glass89e64052014-08-11 09:23:52 -060078 int banklen;
79 int flags;
Simon Glass95a260a92014-02-26 15:59:26 -070080 int ret;
81
Simon Glass89e64052014-08-11 09:23:52 -060082 flags = 0;
Simon Glass95a260a92014-02-26 15:59:26 -070083 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
Simon Glass89e64052014-08-11 09:23:52 -060091 flags |= FLAG_SHOW_BANK;
92 if (all)
93 flags |= FLAG_SHOW_ALL;
Simon Glass95a260a92014-02-26 15:59:26 -070094 bank_name = gpio_get_bank_info(dev, &num_bits);
Simon Glass07575352014-10-04 11:29:44 -060095 if (!num_bits) {
96 debug("GPIO device %s has no bits\n", dev->name);
Simon Glass89e64052014-08-11 09:23:52 -060097 continue;
Simon Glass07575352014-10-04 11:29:44 -060098 }
Simon Glass89e64052014-08-11 09:23:52 -060099 banklen = bank_name ? strlen(bank_name) : 0;
Simon Glass95a260a92014-02-26 15:59:26 -0700100
101 if (!gpio_name || !bank_name ||
Simon Glass48ca6902019-02-16 20:24:43 -0700102 !strncasecmp(gpio_name, bank_name, banklen)) {
Heinrich Schuchardte946b5d2019-08-22 22:19:41 +0200103 const char *p;
Simon Glass95a260a92014-02-26 15:59:26 -0700104 int offset;
105
Simon Glass89e64052014-08-11 09:23:52 -0600106 p = gpio_name + banklen;
107 if (gpio_name && *p) {
Simon Glass0b1284e2021-07-24 09:03:30 -0600108 offset = dectoul(p, NULL);
Simon Glass07575352014-10-04 11:29:44 -0600109 gpio_get_description(dev, bank_name, offset,
Simon Glassb8989b52019-02-16 20:24:42 -0700110 &flags, true);
Simon Glass95a260a92014-02-26 15:59:26 -0700111 } else {
Simon Glass89e64052014-08-11 09:23:52 -0600112 for (offset = 0; offset < num_bits; offset++) {
Simon Glass07575352014-10-04 11:29:44 -0600113 gpio_get_description(dev, bank_name,
Simon Glassb8989b52019-02-16 20:24:42 -0700114 offset, &flags, false);
Simon Glass89e64052014-08-11 09:23:52 -0600115 }
Simon Glass95a260a92014-02-26 15:59:26 -0700116 }
117 }
Simon Glass89e64052014-08-11 09:23:52 -0600118 /* Add a newline between bank names */
119 if (!(flags & FLAG_SHOW_BANK))
120 flags |= FLAG_SHOW_NEWLINE;
Simon Glass95a260a92014-02-26 15:59:26 -0700121 }
122
123 return ret;
124}
Mike Frysingera972b8d2011-04-03 04:40:46 -0400125#endif
126
Simon Glass09140112020-05-10 11:40:03 -0600127static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
128 char *const argv[])
Simon Glass95a260a92014-02-26 15:59:26 -0700129{
130 unsigned int gpio;
131 enum gpio_cmd sub_cmd;
Simon Glassb71bea72015-08-31 18:55:36 -0600132 int value;
Simon Glass95a260a92014-02-26 15:59:26 -0700133 const char *str_cmd, *str_gpio = NULL;
Diego Rondinidd2b8c12022-04-11 12:02:09 +0200134#ifdef CONFIG_CMD_GPIO_READ
135 const char *str_var = NULL;
136#endif
Simon Glass9165e842014-08-11 09:23:53 -0600137 int ret;
Simon Glass95a260a92014-02-26 15:59:26 -0700138#ifdef CONFIG_DM_GPIO
Simon Glass89e64052014-08-11 09:23:52 -0600139 bool all = false;
Simon Glass95a260a92014-02-26 15:59:26 -0700140#endif
141
142 if (argc < 2)
Mike Frysingera972b8d2011-04-03 04:40:46 -0400143 show_usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000144 return CMD_RET_USAGE;
Mike Frysingera972b8d2011-04-03 04:40:46 -0400145 str_cmd = argv[1];
Simon Glass89e64052014-08-11 09:23:52 -0600146 argc -= 2;
147 argv += 2;
148#ifdef CONFIG_DM_GPIO
Diego Rondinidd2b8c12022-04-11 12:02:09 +0200149 if (argc > 0 && !strncmp(str_cmd, "status", 2) && !strcmp(*argv, "-a")) {
Simon Glass89e64052014-08-11 09:23:52 -0600150 all = true;
151 argc--;
152 argv++;
153 }
154#endif
Diego Rondinidd2b8c12022-04-11 12:02:09 +0200155#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
Simon Glass89e64052014-08-11 09:23:52 -0600164 if (argc > 0)
165 str_gpio = *argv;
Simon Glass4c80c532016-02-14 16:28:59 -0700166 if (!strncmp(str_cmd, "status", 2)) {
Simon Glass95a260a92014-02-26 15:59:26 -0700167 /* Support deprecated gpio_status() */
168#ifdef gpio_status
169 gpio_status();
170 return 0;
171#elif defined(CONFIG_DM_GPIO)
Simon Glass89e64052014-08-11 09:23:52 -0600172 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
Simon Glass95a260a92014-02-26 15:59:26 -0700173#else
174 goto show_usage;
175#endif
176 }
177
178 if (!str_gpio)
179 goto show_usage;
Mike Frysingera972b8d2011-04-03 04:40:46 -0400180
181 /* parse the behavior */
182 switch (*str_cmd) {
Simon Glass66f657d2019-01-21 14:53:20 -0700183 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;
Diego Rondinidd2b8c12022-04-11 12:02:09 +0200195#ifdef CONFIG_CMD_GPIO_READ
196 case 'r':
197 sub_cmd = GPIOC_READ;
198 break;
199#endif
Simon Glass66f657d2019-01-21 14:53:20 -0700200 default:
201 goto show_usage;
Mike Frysingera972b8d2011-04-03 04:40:46 -0400202 }
203
Simon Glass95a260a92014-02-26 15:59:26 -0700204#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);
Simon Glass66280e82016-01-21 19:44:50 -0700213 if (ret) {
214 printf("GPIO: '%s' not found\n", str_gpio);
Simon Glass95a260a92014-02-26 15:59:26 -0700215 return cmd_process_error(cmdtp, ret);
Simon Glass66280e82016-01-21 19:44:50 -0700216 }
Simon Glass95a260a92014-02-26 15:59:26 -0700217#else
Mike Frysingera972b8d2011-04-03 04:40:46 -0400218 /* turn the gpio name into a gpio number */
219 gpio = name_to_gpio(str_gpio);
220 if (gpio < 0)
221 goto show_usage;
Simon Glass95a260a92014-02-26 15:59:26 -0700222#endif
Mike Frysingera972b8d2011-04-03 04:40:46 -0400223 /* grab the pin before we tweak it */
Simon Glass9165e842014-08-11 09:23:53 -0600224 ret = gpio_request(gpio, "cmd_gpio");
225 if (ret && ret != -EBUSY) {
Mike Frysinger68012012011-04-12 03:02:11 -0400226 printf("gpio: requesting pin %u failed\n", gpio);
227 return -1;
228 }
Mike Frysingera972b8d2011-04-03 04:40:46 -0400229
230 /* finally, let's do it: set direction and exec command */
Diego Rondinidd2b8c12022-04-11 12:02:09 +0200231 if (sub_cmd == GPIOC_INPUT
232#ifdef CONFIG_CMD_GPIO_READ
233 || sub_cmd == GPIOC_READ
234#endif
235 ) {
Mike Frysingera972b8d2011-04-03 04:40:46 -0400236 gpio_direction_input(gpio);
237 value = gpio_get_value(gpio);
238 } else {
239 switch (sub_cmd) {
Simon Glass66f657d2019-01-21 14:53:20 -0700240 case GPIOC_SET:
Simon Glassb71bea72015-08-31 18:55:36 -0600241 value = 1;
242 break;
Simon Glass66f657d2019-01-21 14:53:20 -0700243 case GPIOC_CLEAR:
Simon Glassb71bea72015-08-31 18:55:36 -0600244 value = 0;
245 break;
Simon Glass66f657d2019-01-21 14:53:20 -0700246 case GPIOC_TOGGLE:
Simon Glassb71bea72015-08-31 18:55:36 -0600247 value = gpio_get_value(gpio);
248 if (!IS_ERR_VALUE(value))
249 value = !value;
250 break;
251 default:
252 goto show_usage;
Mike Frysingera972b8d2011-04-03 04:40:46 -0400253 }
254 gpio_direction_output(gpio, value);
255 }
Heinrich Schuchardtb1970012019-01-06 11:31:54 +0100256 printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio);
Luka Kovacic4dbc1072020-01-05 20:10:56 +0100257
258 if (IS_ERR_VALUE(value)) {
Simon Glassb71bea72015-08-31 18:55:36 -0600259 printf("unknown (ret=%d)\n", value);
Luka Kovacic4dbc1072020-01-05 20:10:56 +0100260 goto err;
261 } else {
Simon Glassb71bea72015-08-31 18:55:36 -0600262 printf("%d\n", value);
Diego Rondinidd2b8c12022-04-11 12:02:09 +0200263#ifdef CONFIG_CMD_GPIO_READ
264 if (sub_cmd == GPIOC_READ)
265 env_set_ulong(str_var, (ulong)value);
266#endif
Luka Kovacic4dbc1072020-01-05 20:10:56 +0100267 }
268
Diego Rondinidd2b8c12022-04-11 12:02:09 +0200269 if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)
270#ifdef CONFIG_CMD_GPIO_READ
271 && sub_cmd != GPIOC_READ
272#endif
273 ) {
Simon Glassb71bea72015-08-31 18:55:36 -0600274 int nval = gpio_get_value(gpio);
275
Luka Kovacic4dbc1072020-01-05 20:10:56 +0100276 if (IS_ERR_VALUE(nval)) {
Simon Glassb71bea72015-08-31 18:55:36 -0600277 printf(" Warning: no access to GPIO output value\n");
Luka Kovacic4dbc1072020-01-05 20:10:56 +0100278 goto err;
279 } else if (nval != value) {
Simon Glassb71bea72015-08-31 18:55:36 -0600280 printf(" Warning: value of pin is still %d\n", nval);
Luka Kovacic4dbc1072020-01-05 20:10:56 +0100281 goto err;
282 }
Simon Glassb71bea72015-08-31 18:55:36 -0600283 }
Mike Frysingera972b8d2011-04-03 04:40:46 -0400284
Simon Glass9165e842014-08-11 09:23:53 -0600285 if (ret != -EBUSY)
286 gpio_free(gpio);
Mike Frysingera972b8d2011-04-03 04:40:46 -0400287
Alex Kiernan4af2a332020-03-11 08:46:29 +0000288 /*
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;
Luka Kovacic4dbc1072020-01-05 20:10:56 +0100294
295err:
296 if (ret != -EBUSY)
297 gpio_free(gpio);
298 return CMD_RET_FAILURE;
Mike Frysingera972b8d2011-04-03 04:40:46 -0400299}
300
Simon Glass89e64052014-08-11 09:23:52 -0600301U_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"
Diego Rondinidd2b8c12022-04-11 12:02:09 +0200305#ifdef CONFIG_CMD_GPIO_READ
306 "gpio read <name> <pin>\n"
307 " - set environment variable 'name' to the specified pin\n"
308#endif
Simon Glass89e64052014-08-11 09:23:52 -0600309 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");