blob: f4565982ecd9c60fcba1b08e62904a5eec7c453f [file] [log] [blame]
Mike Frysinger0e71f802011-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 Glass23a6b8e2014-08-11 09:23:53 -060011#include <errno.h>
Simon Glass459c47a2014-02-26 15:59:26 -070012#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Diego Rondini5ffde632022-04-11 12:02:09 +020015#ifdef CONFIG_CMD_GPIO_READ
16#include <env.h>
17#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -040018#include <asm/gpio.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <linux/err.h>
Mike Frysinger0e71f802011-04-03 04:40:46 -040020
Jeroen Hofsteeb613b102014-10-08 22:58:03 +020021__weak int name_to_gpio(const char *name)
Ian Campbell87af5e12014-03-27 20:34:13 +000022{
Simon Glassff9b9032021-07-24 09:03:30 -060023 return dectoul(name, NULL);
Ian Campbell87af5e12014-03-27 20:34:13 +000024}
Mike Frysinger0e71f802011-04-03 04:40:46 -040025
26enum gpio_cmd {
Simon Glasse70271f2019-01-21 14:53:20 -070027 GPIOC_INPUT,
28 GPIOC_SET,
29 GPIOC_CLEAR,
30 GPIOC_TOGGLE,
Diego Rondini5ffde632022-04-11 12:02:09 +020031#ifdef CONFIG_CMD_GPIO_READ
32 GPIOC_READ,
33#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -040034};
35
Simon Glass459c47a2014-02-26 15:59:26 -070036#if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
Simon Glass459c47a2014-02-26 15:59:26 -070037
Simon Glass2d6b9232014-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 Glass6b1ef592014-10-04 11:29:44 -060045static void gpio_get_description(struct udevice *dev, const char *bank_name,
Simon Glass8edd31b2019-02-16 20:24:42 -070046 int offset, int *flagsp, bool show_all)
Simon Glass459c47a2014-02-26 15:59:26 -070047{
Simon Glass459c47a2014-02-26 15:59:26 -070048 char buf[80];
49 int ret;
50
Simon Glass6b1ef592014-10-04 11:29:44 -060051 ret = gpio_get_function(dev, offset, NULL);
52 if (ret < 0)
53 goto err;
Simon Glass8edd31b2019-02-16 20:24:42 -070054 if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED)
Simon Glass2d6b9232014-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 Glass459c47a2014-02-26 15:59:26 -070064
Simon Glass6b1ef592014-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 Glass459c47a2014-02-26 15:59:26 -070073}
74
Simon Glass2d6b9232014-08-11 09:23:52 -060075static int do_gpio_status(bool all, const char *gpio_name)
Simon Glass459c47a2014-02-26 15:59:26 -070076{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020077 struct udevice *dev;
Simon Glass2d6b9232014-08-11 09:23:52 -060078 int banklen;
79 int flags;
Michal Suchanek1d524ae2022-10-12 21:57:58 +020080 int ret, err = 0;
Simon Glass459c47a2014-02-26 15:59:26 -070081
Simon Glass2d6b9232014-08-11 09:23:52 -060082 flags = 0;
Simon Glass459c47a2014-02-26 15:59:26 -070083 if (gpio_name && !*gpio_name)
84 gpio_name = NULL;
Michal Suchanek1d524ae2022-10-12 21:57:58 +020085 for (ret = uclass_first_device_check(UCLASS_GPIO, &dev);
Simon Glass459c47a2014-02-26 15:59:26 -070086 dev;
Michal Suchanek1d524ae2022-10-12 21:57:58 +020087 ret = uclass_next_device_check(&dev)) {
Simon Glass459c47a2014-02-26 15:59:26 -070088 const char *bank_name;
89 int num_bits;
90
Michal Suchanek1d524ae2022-10-12 21:57:58 +020091 if (ret) {
92 printf("GPIO device %s probe error %i\n",
93 dev->name, ret);
94 err = ret;
95 continue;
96 }
97
Simon Glass2d6b9232014-08-11 09:23:52 -060098 flags |= FLAG_SHOW_BANK;
99 if (all)
100 flags |= FLAG_SHOW_ALL;
Simon Glass459c47a2014-02-26 15:59:26 -0700101 bank_name = gpio_get_bank_info(dev, &num_bits);
Simon Glass6b1ef592014-10-04 11:29:44 -0600102 if (!num_bits) {
103 debug("GPIO device %s has no bits\n", dev->name);
Simon Glass2d6b9232014-08-11 09:23:52 -0600104 continue;
Simon Glass6b1ef592014-10-04 11:29:44 -0600105 }
Simon Glass2d6b9232014-08-11 09:23:52 -0600106 banklen = bank_name ? strlen(bank_name) : 0;
Simon Glass459c47a2014-02-26 15:59:26 -0700107
108 if (!gpio_name || !bank_name ||
Simon Glass26e8b492019-02-16 20:24:43 -0700109 !strncasecmp(gpio_name, bank_name, banklen)) {
Heinrich Schuchardt02767cb2019-08-22 22:19:41 +0200110 const char *p;
Simon Glass459c47a2014-02-26 15:59:26 -0700111 int offset;
112
Simon Glass2d6b9232014-08-11 09:23:52 -0600113 p = gpio_name + banklen;
114 if (gpio_name && *p) {
Simon Glassff9b9032021-07-24 09:03:30 -0600115 offset = dectoul(p, NULL);
Simon Glass6b1ef592014-10-04 11:29:44 -0600116 gpio_get_description(dev, bank_name, offset,
Simon Glass8edd31b2019-02-16 20:24:42 -0700117 &flags, true);
Simon Glass459c47a2014-02-26 15:59:26 -0700118 } else {
Simon Glass2d6b9232014-08-11 09:23:52 -0600119 for (offset = 0; offset < num_bits; offset++) {
Simon Glass6b1ef592014-10-04 11:29:44 -0600120 gpio_get_description(dev, bank_name,
Simon Glass8edd31b2019-02-16 20:24:42 -0700121 offset, &flags, false);
Simon Glass2d6b9232014-08-11 09:23:52 -0600122 }
Simon Glass459c47a2014-02-26 15:59:26 -0700123 }
124 }
Simon Glass2d6b9232014-08-11 09:23:52 -0600125 /* Add a newline between bank names */
126 if (!(flags & FLAG_SHOW_BANK))
127 flags |= FLAG_SHOW_NEWLINE;
Simon Glass459c47a2014-02-26 15:59:26 -0700128 }
129
Michal Suchanek1d524ae2022-10-12 21:57:58 +0200130 return err;
Simon Glass459c47a2014-02-26 15:59:26 -0700131}
132#endif
133
Simon Glassed38aef2020-05-10 11:40:03 -0600134static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
135 char *const argv[])
Mike Frysinger0e71f802011-04-03 04:40:46 -0400136{
Simon Glass459c47a2014-02-26 15:59:26 -0700137 unsigned int gpio;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400138 enum gpio_cmd sub_cmd;
Simon Glassdb96baa2015-08-31 18:55:36 -0600139 int value;
Simon Glass459c47a2014-02-26 15:59:26 -0700140 const char *str_cmd, *str_gpio = NULL;
Diego Rondini5ffde632022-04-11 12:02:09 +0200141#ifdef CONFIG_CMD_GPIO_READ
142 const char *str_var = NULL;
143#endif
Simon Glass23a6b8e2014-08-11 09:23:53 -0600144 int ret;
Simon Glass459c47a2014-02-26 15:59:26 -0700145#ifdef CONFIG_DM_GPIO
Simon Glass2d6b9232014-08-11 09:23:52 -0600146 bool all = false;
Simon Glass459c47a2014-02-26 15:59:26 -0700147#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -0400148
Simon Glass459c47a2014-02-26 15:59:26 -0700149 if (argc < 2)
150 show_usage:
151 return CMD_RET_USAGE;
152 str_cmd = argv[1];
Simon Glass2d6b9232014-08-11 09:23:52 -0600153 argc -= 2;
154 argv += 2;
155#ifdef CONFIG_DM_GPIO
Diego Rondini5ffde632022-04-11 12:02:09 +0200156 if (argc > 0 && !strncmp(str_cmd, "status", 2) && !strcmp(*argv, "-a")) {
Simon Glass2d6b9232014-08-11 09:23:52 -0600157 all = true;
158 argc--;
159 argv++;
160 }
161#endif
Diego Rondini5ffde632022-04-11 12:02:09 +0200162#ifdef CONFIG_CMD_GPIO_READ
163 if (argc > 0 && !strncmp(str_cmd, "read", 2)) {
164 if (argc < 2)
165 goto show_usage;
166 str_var = *argv;
167 argc--;
168 argv++;
169 }
170#endif
Simon Glass2d6b9232014-08-11 09:23:52 -0600171 if (argc > 0)
172 str_gpio = *argv;
Simon Glassd572df12016-02-14 16:28:59 -0700173 if (!strncmp(str_cmd, "status", 2)) {
Simon Glass459c47a2014-02-26 15:59:26 -0700174 /* Support deprecated gpio_status() */
Mike Frysinger0e71f802011-04-03 04:40:46 -0400175#ifdef gpio_status
Mike Frysinger0e71f802011-04-03 04:40:46 -0400176 gpio_status();
177 return 0;
Simon Glass459c47a2014-02-26 15:59:26 -0700178#elif defined(CONFIG_DM_GPIO)
Simon Glass2d6b9232014-08-11 09:23:52 -0600179 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
Simon Glass459c47a2014-02-26 15:59:26 -0700180#else
181 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400182#endif
Simon Glass459c47a2014-02-26 15:59:26 -0700183 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400184
Simon Glass459c47a2014-02-26 15:59:26 -0700185 if (!str_gpio)
186 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400187
188 /* parse the behavior */
189 switch (*str_cmd) {
Simon Glasse70271f2019-01-21 14:53:20 -0700190 case 'i':
191 sub_cmd = GPIOC_INPUT;
192 break;
193 case 's':
194 sub_cmd = GPIOC_SET;
195 break;
196 case 'c':
197 sub_cmd = GPIOC_CLEAR;
198 break;
199 case 't':
200 sub_cmd = GPIOC_TOGGLE;
201 break;
Diego Rondini5ffde632022-04-11 12:02:09 +0200202#ifdef CONFIG_CMD_GPIO_READ
203 case 'r':
204 sub_cmd = GPIOC_READ;
205 break;
206#endif
Simon Glasse70271f2019-01-21 14:53:20 -0700207 default:
208 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400209 }
210
Simon Glass459c47a2014-02-26 15:59:26 -0700211#if defined(CONFIG_DM_GPIO)
212 /*
213 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
214 * framework, so we look up the name here and convert it to a GPIO number.
215 * Once all GPIO drivers are converted to driver model, we can change the
216 * code here to use the GPIO uclass interface instead of the numbered
217 * GPIO compatibility layer.
218 */
219 ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
Simon Glassf5330a42016-01-21 19:44:50 -0700220 if (ret) {
221 printf("GPIO: '%s' not found\n", str_gpio);
Simon Glass459c47a2014-02-26 15:59:26 -0700222 return cmd_process_error(cmdtp, ret);
Simon Glassf5330a42016-01-21 19:44:50 -0700223 }
Simon Glass459c47a2014-02-26 15:59:26 -0700224#else
Mike Frysinger0e71f802011-04-03 04:40:46 -0400225 /* turn the gpio name into a gpio number */
226 gpio = name_to_gpio(str_gpio);
227 if (gpio < 0)
228 goto show_usage;
Simon Glass459c47a2014-02-26 15:59:26 -0700229#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -0400230 /* grab the pin before we tweak it */
Simon Glass23a6b8e2014-08-11 09:23:53 -0600231 ret = gpio_request(gpio, "cmd_gpio");
232 if (ret && ret != -EBUSY) {
Mike Frysinger15b25502011-04-12 03:02:11 -0400233 printf("gpio: requesting pin %u failed\n", gpio);
234 return -1;
235 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400236
237 /* finally, let's do it: set direction and exec command */
Diego Rondini5ffde632022-04-11 12:02:09 +0200238 if (sub_cmd == GPIOC_INPUT
239#ifdef CONFIG_CMD_GPIO_READ
240 || sub_cmd == GPIOC_READ
241#endif
242 ) {
Mike Frysinger0e71f802011-04-03 04:40:46 -0400243 gpio_direction_input(gpio);
244 value = gpio_get_value(gpio);
245 } else {
246 switch (sub_cmd) {
Simon Glasse70271f2019-01-21 14:53:20 -0700247 case GPIOC_SET:
Simon Glassdb96baa2015-08-31 18:55:36 -0600248 value = 1;
249 break;
Simon Glasse70271f2019-01-21 14:53:20 -0700250 case GPIOC_CLEAR:
Simon Glassdb96baa2015-08-31 18:55:36 -0600251 value = 0;
252 break;
Simon Glasse70271f2019-01-21 14:53:20 -0700253 case GPIOC_TOGGLE:
Simon Glassdb96baa2015-08-31 18:55:36 -0600254 value = gpio_get_value(gpio);
255 if (!IS_ERR_VALUE(value))
256 value = !value;
257 break;
258 default:
259 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400260 }
261 gpio_direction_output(gpio, value);
262 }
Heinrich Schuchardtec8dfc32019-01-06 11:31:54 +0100263 printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio);
Luka Kovacica01e78d2020-01-05 20:10:56 +0100264
265 if (IS_ERR_VALUE(value)) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600266 printf("unknown (ret=%d)\n", value);
Luka Kovacica01e78d2020-01-05 20:10:56 +0100267 goto err;
268 } else {
Simon Glassdb96baa2015-08-31 18:55:36 -0600269 printf("%d\n", value);
Diego Rondini5ffde632022-04-11 12:02:09 +0200270#ifdef CONFIG_CMD_GPIO_READ
271 if (sub_cmd == GPIOC_READ)
272 env_set_ulong(str_var, (ulong)value);
273#endif
Luka Kovacica01e78d2020-01-05 20:10:56 +0100274 }
275
Diego Rondini5ffde632022-04-11 12:02:09 +0200276 if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)
277#ifdef CONFIG_CMD_GPIO_READ
278 && sub_cmd != GPIOC_READ
279#endif
280 ) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600281 int nval = gpio_get_value(gpio);
282
Luka Kovacica01e78d2020-01-05 20:10:56 +0100283 if (IS_ERR_VALUE(nval)) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600284 printf(" Warning: no access to GPIO output value\n");
Luka Kovacica01e78d2020-01-05 20:10:56 +0100285 goto err;
286 } else if (nval != value) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600287 printf(" Warning: value of pin is still %d\n", nval);
Luka Kovacica01e78d2020-01-05 20:10:56 +0100288 goto err;
289 }
Simon Glassdb96baa2015-08-31 18:55:36 -0600290 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400291
Simon Glass23a6b8e2014-08-11 09:23:53 -0600292 if (ret != -EBUSY)
293 gpio_free(gpio);
Mike Frysinger0e71f802011-04-03 04:40:46 -0400294
Alex Kiernan1073c232020-03-11 08:46:29 +0000295 /*
296 * Whilst wrong, the legacy gpio input command returns the pin
297 * value, or CMD_RET_FAILURE (which is indistinguishable from a
298 * valid pin value).
299 */
300 return (sub_cmd == GPIOC_INPUT) ? value : CMD_RET_SUCCESS;
Luka Kovacica01e78d2020-01-05 20:10:56 +0100301
302err:
303 if (ret != -EBUSY)
304 gpio_free(gpio);
305 return CMD_RET_FAILURE;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400306}
307
Simon Glass2d6b9232014-08-11 09:23:52 -0600308U_BOOT_CMD(gpio, 4, 0, do_gpio,
309 "query and control gpio pins",
310 "<input|set|clear|toggle> <pin>\n"
311 " - input/set/clear/toggle the specified pin\n"
Diego Rondini5ffde632022-04-11 12:02:09 +0200312#ifdef CONFIG_CMD_GPIO_READ
313 "gpio read <name> <pin>\n"
314 " - set environment variable 'name' to the specified pin\n"
315#endif
Simon Glass2d6b9232014-08-11 09:23:52 -0600316 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");