blob: dab6f7097aec1a0c9ea40a64d128dfa665010377 [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
Tom Riniabb9a042024-05-18 20:20:43 -06009#include <common.h>
Mike Frysinger0e71f802011-04-03 04:40:46 -040010#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>
Paul Barker041bb5e2023-10-16 10:25:31 +010020#include <dm/device_compat.h>
Mike Frysinger0e71f802011-04-03 04:40:46 -040021
Jeroen Hofsteeb613b102014-10-08 22:58:03 +020022__weak int name_to_gpio(const char *name)
Ian Campbell87af5e12014-03-27 20:34:13 +000023{
Simon Glassff9b9032021-07-24 09:03:30 -060024 return dectoul(name, NULL);
Ian Campbell87af5e12014-03-27 20:34:13 +000025}
Mike Frysinger0e71f802011-04-03 04:40:46 -040026
27enum gpio_cmd {
Simon Glasse70271f2019-01-21 14:53:20 -070028 GPIOC_INPUT,
29 GPIOC_SET,
30 GPIOC_CLEAR,
31 GPIOC_TOGGLE,
Diego Rondini5ffde632022-04-11 12:02:09 +020032#ifdef CONFIG_CMD_GPIO_READ
33 GPIOC_READ,
34#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -040035};
36
Simon Glass459c47a2014-02-26 15:59:26 -070037#if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
Simon Glass459c47a2014-02-26 15:59:26 -070038
Simon Glass2d6b9232014-08-11 09:23:52 -060039/* A few flags used by show_gpio() */
40enum {
41 FLAG_SHOW_ALL = 1 << 0,
42 FLAG_SHOW_BANK = 1 << 1,
43 FLAG_SHOW_NEWLINE = 1 << 2,
44};
45
Simon Glass6b1ef592014-10-04 11:29:44 -060046static void gpio_get_description(struct udevice *dev, const char *bank_name,
Simon Glass8edd31b2019-02-16 20:24:42 -070047 int offset, int *flagsp, bool show_all)
Simon Glass459c47a2014-02-26 15:59:26 -070048{
Simon Glass459c47a2014-02-26 15:59:26 -070049 char buf[80];
50 int ret;
51
Simon Glass6b1ef592014-10-04 11:29:44 -060052 ret = gpio_get_function(dev, offset, NULL);
53 if (ret < 0)
54 goto err;
Simon Glass8edd31b2019-02-16 20:24:42 -070055 if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED)
Simon Glass2d6b9232014-08-11 09:23:52 -060056 return;
57 if ((*flagsp & FLAG_SHOW_BANK) && bank_name) {
58 if (*flagsp & FLAG_SHOW_NEWLINE) {
59 putc('\n');
60 *flagsp &= ~FLAG_SHOW_NEWLINE;
61 }
62 printf("Bank %s:\n", bank_name);
63 *flagsp &= ~FLAG_SHOW_BANK;
64 }
Simon Glass459c47a2014-02-26 15:59:26 -070065
Simon Glass6b1ef592014-10-04 11:29:44 -060066 ret = gpio_get_status(dev, offset, buf, sizeof(buf));
67 if (ret)
68 goto err;
69
70 printf("%s\n", buf);
71 return;
72err:
Paul Barker041bb5e2023-10-16 10:25:31 +010073 if (ret != -ENOENT)
74 printf("Error %d\n", ret);
Simon Glass459c47a2014-02-26 15:59:26 -070075}
76
Simon Glass2d6b9232014-08-11 09:23:52 -060077static int do_gpio_status(bool all, const char *gpio_name)
Simon Glass459c47a2014-02-26 15:59:26 -070078{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020079 struct udevice *dev;
Simon Glass2d6b9232014-08-11 09:23:52 -060080 int banklen;
81 int flags;
Michal Suchanek1d524ae2022-10-12 21:57:58 +020082 int ret, err = 0;
Simon Glass459c47a2014-02-26 15:59:26 -070083
Simon Glass2d6b9232014-08-11 09:23:52 -060084 flags = 0;
Simon Glass459c47a2014-02-26 15:59:26 -070085 if (gpio_name && !*gpio_name)
86 gpio_name = NULL;
Michal Suchanek1d524ae2022-10-12 21:57:58 +020087 for (ret = uclass_first_device_check(UCLASS_GPIO, &dev);
Simon Glass459c47a2014-02-26 15:59:26 -070088 dev;
Michal Suchanek1d524ae2022-10-12 21:57:58 +020089 ret = uclass_next_device_check(&dev)) {
Simon Glass459c47a2014-02-26 15:59:26 -070090 const char *bank_name;
91 int num_bits;
92
Michal Suchanek1d524ae2022-10-12 21:57:58 +020093 if (ret) {
94 printf("GPIO device %s probe error %i\n",
95 dev->name, ret);
96 err = ret;
97 continue;
98 }
99
Simon Glass2d6b9232014-08-11 09:23:52 -0600100 flags |= FLAG_SHOW_BANK;
101 if (all)
102 flags |= FLAG_SHOW_ALL;
Simon Glass459c47a2014-02-26 15:59:26 -0700103 bank_name = gpio_get_bank_info(dev, &num_bits);
Simon Glass6b1ef592014-10-04 11:29:44 -0600104 if (!num_bits) {
105 debug("GPIO device %s has no bits\n", dev->name);
Simon Glass2d6b9232014-08-11 09:23:52 -0600106 continue;
Simon Glass6b1ef592014-10-04 11:29:44 -0600107 }
Simon Glass2d6b9232014-08-11 09:23:52 -0600108 banklen = bank_name ? strlen(bank_name) : 0;
Simon Glass459c47a2014-02-26 15:59:26 -0700109
110 if (!gpio_name || !bank_name ||
Simon Glass26e8b492019-02-16 20:24:43 -0700111 !strncasecmp(gpio_name, bank_name, banklen)) {
Heinrich Schuchardt02767cb2019-08-22 22:19:41 +0200112 const char *p;
Simon Glass459c47a2014-02-26 15:59:26 -0700113 int offset;
114
Simon Glass2d6b9232014-08-11 09:23:52 -0600115 p = gpio_name + banklen;
116 if (gpio_name && *p) {
Simon Glassff9b9032021-07-24 09:03:30 -0600117 offset = dectoul(p, NULL);
Simon Glass6b1ef592014-10-04 11:29:44 -0600118 gpio_get_description(dev, bank_name, offset,
Simon Glass8edd31b2019-02-16 20:24:42 -0700119 &flags, true);
Simon Glass459c47a2014-02-26 15:59:26 -0700120 } else {
Simon Glass2d6b9232014-08-11 09:23:52 -0600121 for (offset = 0; offset < num_bits; offset++) {
Simon Glass6b1ef592014-10-04 11:29:44 -0600122 gpio_get_description(dev, bank_name,
Simon Glass8edd31b2019-02-16 20:24:42 -0700123 offset, &flags, false);
Simon Glass2d6b9232014-08-11 09:23:52 -0600124 }
Simon Glass459c47a2014-02-26 15:59:26 -0700125 }
126 }
Simon Glass2d6b9232014-08-11 09:23:52 -0600127 /* Add a newline between bank names */
128 if (!(flags & FLAG_SHOW_BANK))
129 flags |= FLAG_SHOW_NEWLINE;
Simon Glass459c47a2014-02-26 15:59:26 -0700130 }
131
Michal Suchanek1d524ae2022-10-12 21:57:58 +0200132 return err;
Simon Glass459c47a2014-02-26 15:59:26 -0700133}
134#endif
135
Simon Glassed38aef2020-05-10 11:40:03 -0600136static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
137 char *const argv[])
Mike Frysinger0e71f802011-04-03 04:40:46 -0400138{
Simon Glass459c47a2014-02-26 15:59:26 -0700139 unsigned int gpio;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400140 enum gpio_cmd sub_cmd;
Simon Glassdb96baa2015-08-31 18:55:36 -0600141 int value;
Simon Glass459c47a2014-02-26 15:59:26 -0700142 const char *str_cmd, *str_gpio = NULL;
Diego Rondini5ffde632022-04-11 12:02:09 +0200143#ifdef CONFIG_CMD_GPIO_READ
144 const char *str_var = NULL;
145#endif
Simon Glass23a6b8e2014-08-11 09:23:53 -0600146 int ret;
Simon Glass459c47a2014-02-26 15:59:26 -0700147#ifdef CONFIG_DM_GPIO
Simon Glass2d6b9232014-08-11 09:23:52 -0600148 bool all = false;
Simon Glass459c47a2014-02-26 15:59:26 -0700149#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -0400150
Simon Glass459c47a2014-02-26 15:59:26 -0700151 if (argc < 2)
152 show_usage:
153 return CMD_RET_USAGE;
154 str_cmd = argv[1];
Simon Glass2d6b9232014-08-11 09:23:52 -0600155 argc -= 2;
156 argv += 2;
157#ifdef CONFIG_DM_GPIO
Diego Rondini5ffde632022-04-11 12:02:09 +0200158 if (argc > 0 && !strncmp(str_cmd, "status", 2) && !strcmp(*argv, "-a")) {
Simon Glass2d6b9232014-08-11 09:23:52 -0600159 all = true;
160 argc--;
161 argv++;
162 }
163#endif
Diego Rondini5ffde632022-04-11 12:02:09 +0200164#ifdef CONFIG_CMD_GPIO_READ
165 if (argc > 0 && !strncmp(str_cmd, "read", 2)) {
166 if (argc < 2)
167 goto show_usage;
168 str_var = *argv;
169 argc--;
170 argv++;
171 }
172#endif
Simon Glass2d6b9232014-08-11 09:23:52 -0600173 if (argc > 0)
174 str_gpio = *argv;
Simon Glassd572df12016-02-14 16:28:59 -0700175 if (!strncmp(str_cmd, "status", 2)) {
Simon Glass459c47a2014-02-26 15:59:26 -0700176 /* Support deprecated gpio_status() */
Mike Frysinger0e71f802011-04-03 04:40:46 -0400177#ifdef gpio_status
Mike Frysinger0e71f802011-04-03 04:40:46 -0400178 gpio_status();
179 return 0;
Simon Glass459c47a2014-02-26 15:59:26 -0700180#elif defined(CONFIG_DM_GPIO)
Simon Glass2d6b9232014-08-11 09:23:52 -0600181 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
Simon Glass459c47a2014-02-26 15:59:26 -0700182#else
183 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400184#endif
Simon Glass459c47a2014-02-26 15:59:26 -0700185 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400186
Simon Glass459c47a2014-02-26 15:59:26 -0700187 if (!str_gpio)
188 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400189
190 /* parse the behavior */
191 switch (*str_cmd) {
Simon Glasse70271f2019-01-21 14:53:20 -0700192 case 'i':
193 sub_cmd = GPIOC_INPUT;
194 break;
195 case 's':
196 sub_cmd = GPIOC_SET;
197 break;
198 case 'c':
199 sub_cmd = GPIOC_CLEAR;
200 break;
201 case 't':
202 sub_cmd = GPIOC_TOGGLE;
203 break;
Diego Rondini5ffde632022-04-11 12:02:09 +0200204#ifdef CONFIG_CMD_GPIO_READ
205 case 'r':
206 sub_cmd = GPIOC_READ;
207 break;
208#endif
Simon Glasse70271f2019-01-21 14:53:20 -0700209 default:
210 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400211 }
212
Simon Glass459c47a2014-02-26 15:59:26 -0700213#if defined(CONFIG_DM_GPIO)
214 /*
215 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
216 * framework, so we look up the name here and convert it to a GPIO number.
217 * Once all GPIO drivers are converted to driver model, we can change the
218 * code here to use the GPIO uclass interface instead of the numbered
219 * GPIO compatibility layer.
220 */
221 ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
Simon Glassf5330a42016-01-21 19:44:50 -0700222 if (ret) {
223 printf("GPIO: '%s' not found\n", str_gpio);
Simon Glass459c47a2014-02-26 15:59:26 -0700224 return cmd_process_error(cmdtp, ret);
Simon Glassf5330a42016-01-21 19:44:50 -0700225 }
Simon Glass459c47a2014-02-26 15:59:26 -0700226#else
Mike Frysinger0e71f802011-04-03 04:40:46 -0400227 /* turn the gpio name into a gpio number */
228 gpio = name_to_gpio(str_gpio);
229 if (gpio < 0)
230 goto show_usage;
Simon Glass459c47a2014-02-26 15:59:26 -0700231#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -0400232 /* grab the pin before we tweak it */
Simon Glass23a6b8e2014-08-11 09:23:53 -0600233 ret = gpio_request(gpio, "cmd_gpio");
234 if (ret && ret != -EBUSY) {
Mike Frysinger15b25502011-04-12 03:02:11 -0400235 printf("gpio: requesting pin %u failed\n", gpio);
236 return -1;
237 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400238
239 /* finally, let's do it: set direction and exec command */
Diego Rondini5ffde632022-04-11 12:02:09 +0200240 if (sub_cmd == GPIOC_INPUT
241#ifdef CONFIG_CMD_GPIO_READ
242 || sub_cmd == GPIOC_READ
243#endif
244 ) {
Mike Frysinger0e71f802011-04-03 04:40:46 -0400245 gpio_direction_input(gpio);
246 value = gpio_get_value(gpio);
247 } else {
248 switch (sub_cmd) {
Simon Glasse70271f2019-01-21 14:53:20 -0700249 case GPIOC_SET:
Simon Glassdb96baa2015-08-31 18:55:36 -0600250 value = 1;
251 break;
Simon Glasse70271f2019-01-21 14:53:20 -0700252 case GPIOC_CLEAR:
Simon Glassdb96baa2015-08-31 18:55:36 -0600253 value = 0;
254 break;
Simon Glasse70271f2019-01-21 14:53:20 -0700255 case GPIOC_TOGGLE:
Simon Glassdb96baa2015-08-31 18:55:36 -0600256 value = gpio_get_value(gpio);
257 if (!IS_ERR_VALUE(value))
258 value = !value;
259 break;
260 default:
261 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400262 }
263 gpio_direction_output(gpio, value);
264 }
Heinrich Schuchardtec8dfc32019-01-06 11:31:54 +0100265 printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio);
Luka Kovacica01e78d2020-01-05 20:10:56 +0100266
267 if (IS_ERR_VALUE(value)) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600268 printf("unknown (ret=%d)\n", value);
Luka Kovacica01e78d2020-01-05 20:10:56 +0100269 goto err;
270 } else {
Simon Glassdb96baa2015-08-31 18:55:36 -0600271 printf("%d\n", value);
Diego Rondini5ffde632022-04-11 12:02:09 +0200272#ifdef CONFIG_CMD_GPIO_READ
273 if (sub_cmd == GPIOC_READ)
274 env_set_ulong(str_var, (ulong)value);
275#endif
Luka Kovacica01e78d2020-01-05 20:10:56 +0100276 }
277
Diego Rondini5ffde632022-04-11 12:02:09 +0200278 if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)
279#ifdef CONFIG_CMD_GPIO_READ
280 && sub_cmd != GPIOC_READ
281#endif
282 ) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600283 int nval = gpio_get_value(gpio);
284
Luka Kovacica01e78d2020-01-05 20:10:56 +0100285 if (IS_ERR_VALUE(nval)) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600286 printf(" Warning: no access to GPIO output value\n");
Luka Kovacica01e78d2020-01-05 20:10:56 +0100287 goto err;
288 } else if (nval != value) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600289 printf(" Warning: value of pin is still %d\n", nval);
Luka Kovacica01e78d2020-01-05 20:10:56 +0100290 goto err;
291 }
Simon Glassdb96baa2015-08-31 18:55:36 -0600292 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400293
Simon Glass23a6b8e2014-08-11 09:23:53 -0600294 if (ret != -EBUSY)
295 gpio_free(gpio);
Mike Frysinger0e71f802011-04-03 04:40:46 -0400296
Alex Kiernan1073c232020-03-11 08:46:29 +0000297 /*
298 * Whilst wrong, the legacy gpio input command returns the pin
299 * value, or CMD_RET_FAILURE (which is indistinguishable from a
300 * valid pin value).
301 */
302 return (sub_cmd == GPIOC_INPUT) ? value : CMD_RET_SUCCESS;
Luka Kovacica01e78d2020-01-05 20:10:56 +0100303
304err:
305 if (ret != -EBUSY)
306 gpio_free(gpio);
307 return CMD_RET_FAILURE;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400308}
309
Simon Glass2d6b9232014-08-11 09:23:52 -0600310U_BOOT_CMD(gpio, 4, 0, do_gpio,
311 "query and control gpio pins",
312 "<input|set|clear|toggle> <pin>\n"
313 " - input/set/clear/toggle the specified pin\n"
Diego Rondini5ffde632022-04-11 12:02:09 +0200314#ifdef CONFIG_CMD_GPIO_READ
315 "gpio read <name> <pin>\n"
316 " - set environment variable 'name' to the specified pin\n"
317#endif
Simon Glass2d6b9232014-08-11 09:23:52 -0600318 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");