blob: 53e9ce666f94eff585dac7d12b3317ea61cd2a88 [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;
Simon Glass459c47a2014-02-26 15:59:26 -070080 int ret;
81
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;
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 Glass2d6b9232014-08-11 09:23:52 -060091 flags |= FLAG_SHOW_BANK;
92 if (all)
93 flags |= FLAG_SHOW_ALL;
Simon Glass459c47a2014-02-26 15:59:26 -070094 bank_name = gpio_get_bank_info(dev, &num_bits);
Simon Glass6b1ef592014-10-04 11:29:44 -060095 if (!num_bits) {
96 debug("GPIO device %s has no bits\n", dev->name);
Simon Glass2d6b9232014-08-11 09:23:52 -060097 continue;
Simon Glass6b1ef592014-10-04 11:29:44 -060098 }
Simon Glass2d6b9232014-08-11 09:23:52 -060099 banklen = bank_name ? strlen(bank_name) : 0;
Simon Glass459c47a2014-02-26 15:59:26 -0700100
101 if (!gpio_name || !bank_name ||
Simon Glass26e8b492019-02-16 20:24:43 -0700102 !strncasecmp(gpio_name, bank_name, banklen)) {
Heinrich Schuchardt02767cb2019-08-22 22:19:41 +0200103 const char *p;
Simon Glass459c47a2014-02-26 15:59:26 -0700104 int offset;
105
Simon Glass2d6b9232014-08-11 09:23:52 -0600106 p = gpio_name + banklen;
107 if (gpio_name && *p) {
Simon Glassff9b9032021-07-24 09:03:30 -0600108 offset = dectoul(p, NULL);
Simon Glass6b1ef592014-10-04 11:29:44 -0600109 gpio_get_description(dev, bank_name, offset,
Simon Glass8edd31b2019-02-16 20:24:42 -0700110 &flags, true);
Simon Glass459c47a2014-02-26 15:59:26 -0700111 } else {
Simon Glass2d6b9232014-08-11 09:23:52 -0600112 for (offset = 0; offset < num_bits; offset++) {
Simon Glass6b1ef592014-10-04 11:29:44 -0600113 gpio_get_description(dev, bank_name,
Simon Glass8edd31b2019-02-16 20:24:42 -0700114 offset, &flags, false);
Simon Glass2d6b9232014-08-11 09:23:52 -0600115 }
Simon Glass459c47a2014-02-26 15:59:26 -0700116 }
117 }
Simon Glass2d6b9232014-08-11 09:23:52 -0600118 /* Add a newline between bank names */
119 if (!(flags & FLAG_SHOW_BANK))
120 flags |= FLAG_SHOW_NEWLINE;
Simon Glass459c47a2014-02-26 15:59:26 -0700121 }
122
123 return ret;
124}
125#endif
126
Simon Glassed38aef2020-05-10 11:40:03 -0600127static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
128 char *const argv[])
Mike Frysinger0e71f802011-04-03 04:40:46 -0400129{
Simon Glass459c47a2014-02-26 15:59:26 -0700130 unsigned int gpio;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400131 enum gpio_cmd sub_cmd;
Simon Glassdb96baa2015-08-31 18:55:36 -0600132 int value;
Simon Glass459c47a2014-02-26 15:59:26 -0700133 const char *str_cmd, *str_gpio = NULL;
Diego Rondini5ffde632022-04-11 12:02:09 +0200134#ifdef CONFIG_CMD_GPIO_READ
135 const char *str_var = NULL;
136#endif
Simon Glass23a6b8e2014-08-11 09:23:53 -0600137 int ret;
Simon Glass459c47a2014-02-26 15:59:26 -0700138#ifdef CONFIG_DM_GPIO
Simon Glass2d6b9232014-08-11 09:23:52 -0600139 bool all = false;
Simon Glass459c47a2014-02-26 15:59:26 -0700140#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -0400141
Simon Glass459c47a2014-02-26 15:59:26 -0700142 if (argc < 2)
143 show_usage:
144 return CMD_RET_USAGE;
145 str_cmd = argv[1];
Simon Glass2d6b9232014-08-11 09:23:52 -0600146 argc -= 2;
147 argv += 2;
148#ifdef CONFIG_DM_GPIO
Diego Rondini5ffde632022-04-11 12:02:09 +0200149 if (argc > 0 && !strncmp(str_cmd, "status", 2) && !strcmp(*argv, "-a")) {
Simon Glass2d6b9232014-08-11 09:23:52 -0600150 all = true;
151 argc--;
152 argv++;
153 }
154#endif
Diego Rondini5ffde632022-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 Glass2d6b9232014-08-11 09:23:52 -0600164 if (argc > 0)
165 str_gpio = *argv;
Simon Glassd572df12016-02-14 16:28:59 -0700166 if (!strncmp(str_cmd, "status", 2)) {
Simon Glass459c47a2014-02-26 15:59:26 -0700167 /* Support deprecated gpio_status() */
Mike Frysinger0e71f802011-04-03 04:40:46 -0400168#ifdef gpio_status
Mike Frysinger0e71f802011-04-03 04:40:46 -0400169 gpio_status();
170 return 0;
Simon Glass459c47a2014-02-26 15:59:26 -0700171#elif defined(CONFIG_DM_GPIO)
Simon Glass2d6b9232014-08-11 09:23:52 -0600172 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
Simon Glass459c47a2014-02-26 15:59:26 -0700173#else
174 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400175#endif
Simon Glass459c47a2014-02-26 15:59:26 -0700176 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400177
Simon Glass459c47a2014-02-26 15:59:26 -0700178 if (!str_gpio)
179 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400180
181 /* parse the behavior */
182 switch (*str_cmd) {
Simon Glasse70271f2019-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 Rondini5ffde632022-04-11 12:02:09 +0200195#ifdef CONFIG_CMD_GPIO_READ
196 case 'r':
197 sub_cmd = GPIOC_READ;
198 break;
199#endif
Simon Glasse70271f2019-01-21 14:53:20 -0700200 default:
201 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400202 }
203
Simon Glass459c47a2014-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 Glassf5330a42016-01-21 19:44:50 -0700213 if (ret) {
214 printf("GPIO: '%s' not found\n", str_gpio);
Simon Glass459c47a2014-02-26 15:59:26 -0700215 return cmd_process_error(cmdtp, ret);
Simon Glassf5330a42016-01-21 19:44:50 -0700216 }
Simon Glass459c47a2014-02-26 15:59:26 -0700217#else
Mike Frysinger0e71f802011-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 Glass459c47a2014-02-26 15:59:26 -0700222#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -0400223 /* grab the pin before we tweak it */
Simon Glass23a6b8e2014-08-11 09:23:53 -0600224 ret = gpio_request(gpio, "cmd_gpio");
225 if (ret && ret != -EBUSY) {
Mike Frysinger15b25502011-04-12 03:02:11 -0400226 printf("gpio: requesting pin %u failed\n", gpio);
227 return -1;
228 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400229
230 /* finally, let's do it: set direction and exec command */
Diego Rondini5ffde632022-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 Frysinger0e71f802011-04-03 04:40:46 -0400236 gpio_direction_input(gpio);
237 value = gpio_get_value(gpio);
238 } else {
239 switch (sub_cmd) {
Simon Glasse70271f2019-01-21 14:53:20 -0700240 case GPIOC_SET:
Simon Glassdb96baa2015-08-31 18:55:36 -0600241 value = 1;
242 break;
Simon Glasse70271f2019-01-21 14:53:20 -0700243 case GPIOC_CLEAR:
Simon Glassdb96baa2015-08-31 18:55:36 -0600244 value = 0;
245 break;
Simon Glasse70271f2019-01-21 14:53:20 -0700246 case GPIOC_TOGGLE:
Simon Glassdb96baa2015-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 Frysinger0e71f802011-04-03 04:40:46 -0400253 }
254 gpio_direction_output(gpio, value);
255 }
Heinrich Schuchardtec8dfc32019-01-06 11:31:54 +0100256 printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio);
Luka Kovacica01e78d2020-01-05 20:10:56 +0100257
258 if (IS_ERR_VALUE(value)) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600259 printf("unknown (ret=%d)\n", value);
Luka Kovacica01e78d2020-01-05 20:10:56 +0100260 goto err;
261 } else {
Simon Glassdb96baa2015-08-31 18:55:36 -0600262 printf("%d\n", value);
Diego Rondini5ffde632022-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 Kovacica01e78d2020-01-05 20:10:56 +0100267 }
268
Diego Rondini5ffde632022-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 Glassdb96baa2015-08-31 18:55:36 -0600274 int nval = gpio_get_value(gpio);
275
Luka Kovacica01e78d2020-01-05 20:10:56 +0100276 if (IS_ERR_VALUE(nval)) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600277 printf(" Warning: no access to GPIO output value\n");
Luka Kovacica01e78d2020-01-05 20:10:56 +0100278 goto err;
279 } else if (nval != value) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600280 printf(" Warning: value of pin is still %d\n", nval);
Luka Kovacica01e78d2020-01-05 20:10:56 +0100281 goto err;
282 }
Simon Glassdb96baa2015-08-31 18:55:36 -0600283 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400284
Simon Glass23a6b8e2014-08-11 09:23:53 -0600285 if (ret != -EBUSY)
286 gpio_free(gpio);
Mike Frysinger0e71f802011-04-03 04:40:46 -0400287
Alex Kiernan1073c232020-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 Kovacica01e78d2020-01-05 20:10:56 +0100294
295err:
296 if (ret != -EBUSY)
297 gpio_free(gpio);
298 return CMD_RET_FAILURE;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400299}
300
Simon Glass2d6b9232014-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 Rondini5ffde632022-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 Glass2d6b9232014-08-11 09:23:52 -0600309 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");