blob: 4150024e628ea78790b95666c0984c33c183750a [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>
Mike Frysinger0e71f802011-04-03 04:40:46 -040015#include <asm/gpio.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <linux/err.h>
Mike Frysinger0e71f802011-04-03 04:40:46 -040017
Jeroen Hofsteeb613b102014-10-08 22:58:03 +020018__weak int name_to_gpio(const char *name)
Ian Campbell87af5e12014-03-27 20:34:13 +000019{
Simon Glassff9b9032021-07-24 09:03:30 -060020 return dectoul(name, NULL);
Ian Campbell87af5e12014-03-27 20:34:13 +000021}
Mike Frysinger0e71f802011-04-03 04:40:46 -040022
23enum gpio_cmd {
Simon Glasse70271f2019-01-21 14:53:20 -070024 GPIOC_INPUT,
25 GPIOC_SET,
26 GPIOC_CLEAR,
27 GPIOC_TOGGLE,
Mike Frysinger0e71f802011-04-03 04:40:46 -040028};
29
Simon Glass459c47a2014-02-26 15:59:26 -070030#if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
Simon Glass459c47a2014-02-26 15:59:26 -070031
Simon Glass2d6b9232014-08-11 09:23:52 -060032/* A few flags used by show_gpio() */
33enum {
34 FLAG_SHOW_ALL = 1 << 0,
35 FLAG_SHOW_BANK = 1 << 1,
36 FLAG_SHOW_NEWLINE = 1 << 2,
37};
38
Simon Glass6b1ef592014-10-04 11:29:44 -060039static void gpio_get_description(struct udevice *dev, const char *bank_name,
Simon Glass8edd31b2019-02-16 20:24:42 -070040 int offset, int *flagsp, bool show_all)
Simon Glass459c47a2014-02-26 15:59:26 -070041{
Simon Glass459c47a2014-02-26 15:59:26 -070042 char buf[80];
43 int ret;
44
Simon Glass6b1ef592014-10-04 11:29:44 -060045 ret = gpio_get_function(dev, offset, NULL);
46 if (ret < 0)
47 goto err;
Simon Glass8edd31b2019-02-16 20:24:42 -070048 if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED)
Simon Glass2d6b9232014-08-11 09:23:52 -060049 return;
50 if ((*flagsp & FLAG_SHOW_BANK) && bank_name) {
51 if (*flagsp & FLAG_SHOW_NEWLINE) {
52 putc('\n');
53 *flagsp &= ~FLAG_SHOW_NEWLINE;
54 }
55 printf("Bank %s:\n", bank_name);
56 *flagsp &= ~FLAG_SHOW_BANK;
57 }
Simon Glass459c47a2014-02-26 15:59:26 -070058
Simon Glass6b1ef592014-10-04 11:29:44 -060059 ret = gpio_get_status(dev, offset, buf, sizeof(buf));
60 if (ret)
61 goto err;
62
63 printf("%s\n", buf);
64 return;
65err:
66 printf("Error %d\n", ret);
Simon Glass459c47a2014-02-26 15:59:26 -070067}
68
Simon Glass2d6b9232014-08-11 09:23:52 -060069static int do_gpio_status(bool all, const char *gpio_name)
Simon Glass459c47a2014-02-26 15:59:26 -070070{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020071 struct udevice *dev;
Simon Glass2d6b9232014-08-11 09:23:52 -060072 int banklen;
73 int flags;
Simon Glass459c47a2014-02-26 15:59:26 -070074 int ret;
75
Simon Glass2d6b9232014-08-11 09:23:52 -060076 flags = 0;
Simon Glass459c47a2014-02-26 15:59:26 -070077 if (gpio_name && !*gpio_name)
78 gpio_name = NULL;
79 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
80 dev;
81 ret = uclass_next_device(&dev)) {
82 const char *bank_name;
83 int num_bits;
84
Simon Glass2d6b9232014-08-11 09:23:52 -060085 flags |= FLAG_SHOW_BANK;
86 if (all)
87 flags |= FLAG_SHOW_ALL;
Simon Glass459c47a2014-02-26 15:59:26 -070088 bank_name = gpio_get_bank_info(dev, &num_bits);
Simon Glass6b1ef592014-10-04 11:29:44 -060089 if (!num_bits) {
90 debug("GPIO device %s has no bits\n", dev->name);
Simon Glass2d6b9232014-08-11 09:23:52 -060091 continue;
Simon Glass6b1ef592014-10-04 11:29:44 -060092 }
Simon Glass2d6b9232014-08-11 09:23:52 -060093 banklen = bank_name ? strlen(bank_name) : 0;
Simon Glass459c47a2014-02-26 15:59:26 -070094
95 if (!gpio_name || !bank_name ||
Simon Glass26e8b492019-02-16 20:24:43 -070096 !strncasecmp(gpio_name, bank_name, banklen)) {
Heinrich Schuchardt02767cb2019-08-22 22:19:41 +020097 const char *p;
Simon Glass459c47a2014-02-26 15:59:26 -070098 int offset;
99
Simon Glass2d6b9232014-08-11 09:23:52 -0600100 p = gpio_name + banklen;
101 if (gpio_name && *p) {
Simon Glassff9b9032021-07-24 09:03:30 -0600102 offset = dectoul(p, NULL);
Simon Glass6b1ef592014-10-04 11:29:44 -0600103 gpio_get_description(dev, bank_name, offset,
Simon Glass8edd31b2019-02-16 20:24:42 -0700104 &flags, true);
Simon Glass459c47a2014-02-26 15:59:26 -0700105 } else {
Simon Glass2d6b9232014-08-11 09:23:52 -0600106 for (offset = 0; offset < num_bits; offset++) {
Simon Glass6b1ef592014-10-04 11:29:44 -0600107 gpio_get_description(dev, bank_name,
Simon Glass8edd31b2019-02-16 20:24:42 -0700108 offset, &flags, false);
Simon Glass2d6b9232014-08-11 09:23:52 -0600109 }
Simon Glass459c47a2014-02-26 15:59:26 -0700110 }
111 }
Simon Glass2d6b9232014-08-11 09:23:52 -0600112 /* Add a newline between bank names */
113 if (!(flags & FLAG_SHOW_BANK))
114 flags |= FLAG_SHOW_NEWLINE;
Simon Glass459c47a2014-02-26 15:59:26 -0700115 }
116
117 return ret;
118}
119#endif
120
Simon Glassed38aef2020-05-10 11:40:03 -0600121static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
122 char *const argv[])
Mike Frysinger0e71f802011-04-03 04:40:46 -0400123{
Simon Glass459c47a2014-02-26 15:59:26 -0700124 unsigned int gpio;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400125 enum gpio_cmd sub_cmd;
Simon Glassdb96baa2015-08-31 18:55:36 -0600126 int value;
Simon Glass459c47a2014-02-26 15:59:26 -0700127 const char *str_cmd, *str_gpio = NULL;
Simon Glass23a6b8e2014-08-11 09:23:53 -0600128 int ret;
Simon Glass459c47a2014-02-26 15:59:26 -0700129#ifdef CONFIG_DM_GPIO
Simon Glass2d6b9232014-08-11 09:23:52 -0600130 bool all = false;
Simon Glass459c47a2014-02-26 15:59:26 -0700131#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -0400132
Simon Glass459c47a2014-02-26 15:59:26 -0700133 if (argc < 2)
134 show_usage:
135 return CMD_RET_USAGE;
136 str_cmd = argv[1];
Simon Glass2d6b9232014-08-11 09:23:52 -0600137 argc -= 2;
138 argv += 2;
139#ifdef CONFIG_DM_GPIO
140 if (argc > 0 && !strcmp(*argv, "-a")) {
141 all = true;
142 argc--;
143 argv++;
144 }
145#endif
146 if (argc > 0)
147 str_gpio = *argv;
Simon Glassd572df12016-02-14 16:28:59 -0700148 if (!strncmp(str_cmd, "status", 2)) {
Simon Glass459c47a2014-02-26 15:59:26 -0700149 /* Support deprecated gpio_status() */
Mike Frysinger0e71f802011-04-03 04:40:46 -0400150#ifdef gpio_status
Mike Frysinger0e71f802011-04-03 04:40:46 -0400151 gpio_status();
152 return 0;
Simon Glass459c47a2014-02-26 15:59:26 -0700153#elif defined(CONFIG_DM_GPIO)
Simon Glass2d6b9232014-08-11 09:23:52 -0600154 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
Simon Glass459c47a2014-02-26 15:59:26 -0700155#else
156 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400157#endif
Simon Glass459c47a2014-02-26 15:59:26 -0700158 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400159
Simon Glass459c47a2014-02-26 15:59:26 -0700160 if (!str_gpio)
161 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400162
163 /* parse the behavior */
164 switch (*str_cmd) {
Simon Glasse70271f2019-01-21 14:53:20 -0700165 case 'i':
166 sub_cmd = GPIOC_INPUT;
167 break;
168 case 's':
169 sub_cmd = GPIOC_SET;
170 break;
171 case 'c':
172 sub_cmd = GPIOC_CLEAR;
173 break;
174 case 't':
175 sub_cmd = GPIOC_TOGGLE;
176 break;
177 default:
178 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400179 }
180
Simon Glass459c47a2014-02-26 15:59:26 -0700181#if defined(CONFIG_DM_GPIO)
182 /*
183 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
184 * framework, so we look up the name here and convert it to a GPIO number.
185 * Once all GPIO drivers are converted to driver model, we can change the
186 * code here to use the GPIO uclass interface instead of the numbered
187 * GPIO compatibility layer.
188 */
189 ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
Simon Glassf5330a42016-01-21 19:44:50 -0700190 if (ret) {
191 printf("GPIO: '%s' not found\n", str_gpio);
Simon Glass459c47a2014-02-26 15:59:26 -0700192 return cmd_process_error(cmdtp, ret);
Simon Glassf5330a42016-01-21 19:44:50 -0700193 }
Simon Glass459c47a2014-02-26 15:59:26 -0700194#else
Mike Frysinger0e71f802011-04-03 04:40:46 -0400195 /* turn the gpio name into a gpio number */
196 gpio = name_to_gpio(str_gpio);
197 if (gpio < 0)
198 goto show_usage;
Simon Glass459c47a2014-02-26 15:59:26 -0700199#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -0400200 /* grab the pin before we tweak it */
Simon Glass23a6b8e2014-08-11 09:23:53 -0600201 ret = gpio_request(gpio, "cmd_gpio");
202 if (ret && ret != -EBUSY) {
Mike Frysinger15b25502011-04-12 03:02:11 -0400203 printf("gpio: requesting pin %u failed\n", gpio);
204 return -1;
205 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400206
207 /* finally, let's do it: set direction and exec command */
Simon Glasse70271f2019-01-21 14:53:20 -0700208 if (sub_cmd == GPIOC_INPUT) {
Mike Frysinger0e71f802011-04-03 04:40:46 -0400209 gpio_direction_input(gpio);
210 value = gpio_get_value(gpio);
211 } else {
212 switch (sub_cmd) {
Simon Glasse70271f2019-01-21 14:53:20 -0700213 case GPIOC_SET:
Simon Glassdb96baa2015-08-31 18:55:36 -0600214 value = 1;
215 break;
Simon Glasse70271f2019-01-21 14:53:20 -0700216 case GPIOC_CLEAR:
Simon Glassdb96baa2015-08-31 18:55:36 -0600217 value = 0;
218 break;
Simon Glasse70271f2019-01-21 14:53:20 -0700219 case GPIOC_TOGGLE:
Simon Glassdb96baa2015-08-31 18:55:36 -0600220 value = gpio_get_value(gpio);
221 if (!IS_ERR_VALUE(value))
222 value = !value;
223 break;
224 default:
225 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400226 }
227 gpio_direction_output(gpio, value);
228 }
Heinrich Schuchardtec8dfc32019-01-06 11:31:54 +0100229 printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio);
Luka Kovacica01e78d2020-01-05 20:10:56 +0100230
231 if (IS_ERR_VALUE(value)) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600232 printf("unknown (ret=%d)\n", value);
Luka Kovacica01e78d2020-01-05 20:10:56 +0100233 goto err;
234 } else {
Simon Glassdb96baa2015-08-31 18:55:36 -0600235 printf("%d\n", value);
Luka Kovacica01e78d2020-01-05 20:10:56 +0100236 }
237
Simon Glasse70271f2019-01-21 14:53:20 -0700238 if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600239 int nval = gpio_get_value(gpio);
240
Luka Kovacica01e78d2020-01-05 20:10:56 +0100241 if (IS_ERR_VALUE(nval)) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600242 printf(" Warning: no access to GPIO output value\n");
Luka Kovacica01e78d2020-01-05 20:10:56 +0100243 goto err;
244 } else if (nval != value) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600245 printf(" Warning: value of pin is still %d\n", nval);
Luka Kovacica01e78d2020-01-05 20:10:56 +0100246 goto err;
247 }
Simon Glassdb96baa2015-08-31 18:55:36 -0600248 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400249
Simon Glass23a6b8e2014-08-11 09:23:53 -0600250 if (ret != -EBUSY)
251 gpio_free(gpio);
Mike Frysinger0e71f802011-04-03 04:40:46 -0400252
Alex Kiernan1073c232020-03-11 08:46:29 +0000253 /*
254 * Whilst wrong, the legacy gpio input command returns the pin
255 * value, or CMD_RET_FAILURE (which is indistinguishable from a
256 * valid pin value).
257 */
258 return (sub_cmd == GPIOC_INPUT) ? value : CMD_RET_SUCCESS;
Luka Kovacica01e78d2020-01-05 20:10:56 +0100259
260err:
261 if (ret != -EBUSY)
262 gpio_free(gpio);
263 return CMD_RET_FAILURE;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400264}
265
Simon Glass2d6b9232014-08-11 09:23:52 -0600266U_BOOT_CMD(gpio, 4, 0, do_gpio,
267 "query and control gpio pins",
268 "<input|set|clear|toggle> <pin>\n"
269 " - input/set/clear/toggle the specified pin\n"
270 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");