blob: 5f4c7ff114e0589ac800233cb4382406602639f9 [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>
Mike Frysinger0e71f802011-04-03 04:40:46 -040013#include <asm/gpio.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070014#include <linux/err.h>
Mike Frysinger0e71f802011-04-03 04:40:46 -040015
Jeroen Hofsteeb613b102014-10-08 22:58:03 +020016__weak int name_to_gpio(const char *name)
Ian Campbell87af5e12014-03-27 20:34:13 +000017{
18 return simple_strtoul(name, NULL, 10);
19}
Mike Frysinger0e71f802011-04-03 04:40:46 -040020
21enum gpio_cmd {
Simon Glasse70271f2019-01-21 14:53:20 -070022 GPIOC_INPUT,
23 GPIOC_SET,
24 GPIOC_CLEAR,
25 GPIOC_TOGGLE,
Mike Frysinger0e71f802011-04-03 04:40:46 -040026};
27
Simon Glass459c47a2014-02-26 15:59:26 -070028#if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
Simon Glass459c47a2014-02-26 15:59:26 -070029
Simon Glass2d6b9232014-08-11 09:23:52 -060030/* A few flags used by show_gpio() */
31enum {
32 FLAG_SHOW_ALL = 1 << 0,
33 FLAG_SHOW_BANK = 1 << 1,
34 FLAG_SHOW_NEWLINE = 1 << 2,
35};
36
Simon Glass6b1ef592014-10-04 11:29:44 -060037static void gpio_get_description(struct udevice *dev, const char *bank_name,
Simon Glass8edd31b2019-02-16 20:24:42 -070038 int offset, int *flagsp, bool show_all)
Simon Glass459c47a2014-02-26 15:59:26 -070039{
Simon Glass459c47a2014-02-26 15:59:26 -070040 char buf[80];
41 int ret;
42
Simon Glass6b1ef592014-10-04 11:29:44 -060043 ret = gpio_get_function(dev, offset, NULL);
44 if (ret < 0)
45 goto err;
Simon Glass8edd31b2019-02-16 20:24:42 -070046 if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED)
Simon Glass2d6b9232014-08-11 09:23:52 -060047 return;
48 if ((*flagsp & FLAG_SHOW_BANK) && bank_name) {
49 if (*flagsp & FLAG_SHOW_NEWLINE) {
50 putc('\n');
51 *flagsp &= ~FLAG_SHOW_NEWLINE;
52 }
53 printf("Bank %s:\n", bank_name);
54 *flagsp &= ~FLAG_SHOW_BANK;
55 }
Simon Glass459c47a2014-02-26 15:59:26 -070056
Simon Glass6b1ef592014-10-04 11:29:44 -060057 ret = gpio_get_status(dev, offset, buf, sizeof(buf));
58 if (ret)
59 goto err;
60
61 printf("%s\n", buf);
62 return;
63err:
64 printf("Error %d\n", ret);
Simon Glass459c47a2014-02-26 15:59:26 -070065}
66
Simon Glass2d6b9232014-08-11 09:23:52 -060067static int do_gpio_status(bool all, const char *gpio_name)
Simon Glass459c47a2014-02-26 15:59:26 -070068{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020069 struct udevice *dev;
Simon Glass2d6b9232014-08-11 09:23:52 -060070 int banklen;
71 int flags;
Simon Glass459c47a2014-02-26 15:59:26 -070072 int ret;
73
Simon Glass2d6b9232014-08-11 09:23:52 -060074 flags = 0;
Simon Glass459c47a2014-02-26 15:59:26 -070075 if (gpio_name && !*gpio_name)
76 gpio_name = NULL;
77 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
78 dev;
79 ret = uclass_next_device(&dev)) {
80 const char *bank_name;
81 int num_bits;
82
Simon Glass2d6b9232014-08-11 09:23:52 -060083 flags |= FLAG_SHOW_BANK;
84 if (all)
85 flags |= FLAG_SHOW_ALL;
Simon Glass459c47a2014-02-26 15:59:26 -070086 bank_name = gpio_get_bank_info(dev, &num_bits);
Simon Glass6b1ef592014-10-04 11:29:44 -060087 if (!num_bits) {
88 debug("GPIO device %s has no bits\n", dev->name);
Simon Glass2d6b9232014-08-11 09:23:52 -060089 continue;
Simon Glass6b1ef592014-10-04 11:29:44 -060090 }
Simon Glass2d6b9232014-08-11 09:23:52 -060091 banklen = bank_name ? strlen(bank_name) : 0;
Simon Glass459c47a2014-02-26 15:59:26 -070092
93 if (!gpio_name || !bank_name ||
Simon Glass26e8b492019-02-16 20:24:43 -070094 !strncasecmp(gpio_name, bank_name, banklen)) {
Heinrich Schuchardt02767cb2019-08-22 22:19:41 +020095 const char *p;
Simon Glass459c47a2014-02-26 15:59:26 -070096 int offset;
97
Simon Glass2d6b9232014-08-11 09:23:52 -060098 p = gpio_name + banklen;
99 if (gpio_name && *p) {
Simon Glass459c47a2014-02-26 15:59:26 -0700100 offset = simple_strtoul(p, NULL, 10);
Simon Glass6b1ef592014-10-04 11:29:44 -0600101 gpio_get_description(dev, bank_name, offset,
Simon Glass8edd31b2019-02-16 20:24:42 -0700102 &flags, true);
Simon Glass459c47a2014-02-26 15:59:26 -0700103 } else {
Simon Glass2d6b9232014-08-11 09:23:52 -0600104 for (offset = 0; offset < num_bits; offset++) {
Simon Glass6b1ef592014-10-04 11:29:44 -0600105 gpio_get_description(dev, bank_name,
Simon Glass8edd31b2019-02-16 20:24:42 -0700106 offset, &flags, false);
Simon Glass2d6b9232014-08-11 09:23:52 -0600107 }
Simon Glass459c47a2014-02-26 15:59:26 -0700108 }
109 }
Simon Glass2d6b9232014-08-11 09:23:52 -0600110 /* Add a newline between bank names */
111 if (!(flags & FLAG_SHOW_BANK))
112 flags |= FLAG_SHOW_NEWLINE;
Simon Glass459c47a2014-02-26 15:59:26 -0700113 }
114
115 return ret;
116}
117#endif
118
Mike Frysinger0e71f802011-04-03 04:40:46 -0400119static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
120{
Simon Glass459c47a2014-02-26 15:59:26 -0700121 unsigned int gpio;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400122 enum gpio_cmd sub_cmd;
Simon Glassdb96baa2015-08-31 18:55:36 -0600123 int value;
Simon Glass459c47a2014-02-26 15:59:26 -0700124 const char *str_cmd, *str_gpio = NULL;
Simon Glass23a6b8e2014-08-11 09:23:53 -0600125 int ret;
Simon Glass459c47a2014-02-26 15:59:26 -0700126#ifdef CONFIG_DM_GPIO
Simon Glass2d6b9232014-08-11 09:23:52 -0600127 bool all = false;
Simon Glass459c47a2014-02-26 15:59:26 -0700128#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -0400129
Simon Glass459c47a2014-02-26 15:59:26 -0700130 if (argc < 2)
131 show_usage:
132 return CMD_RET_USAGE;
133 str_cmd = argv[1];
Simon Glass2d6b9232014-08-11 09:23:52 -0600134 argc -= 2;
135 argv += 2;
136#ifdef CONFIG_DM_GPIO
137 if (argc > 0 && !strcmp(*argv, "-a")) {
138 all = true;
139 argc--;
140 argv++;
141 }
142#endif
143 if (argc > 0)
144 str_gpio = *argv;
Simon Glassd572df12016-02-14 16:28:59 -0700145 if (!strncmp(str_cmd, "status", 2)) {
Simon Glass459c47a2014-02-26 15:59:26 -0700146 /* Support deprecated gpio_status() */
Mike Frysinger0e71f802011-04-03 04:40:46 -0400147#ifdef gpio_status
Mike Frysinger0e71f802011-04-03 04:40:46 -0400148 gpio_status();
149 return 0;
Simon Glass459c47a2014-02-26 15:59:26 -0700150#elif defined(CONFIG_DM_GPIO)
Simon Glass2d6b9232014-08-11 09:23:52 -0600151 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
Simon Glass459c47a2014-02-26 15:59:26 -0700152#else
153 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400154#endif
Simon Glass459c47a2014-02-26 15:59:26 -0700155 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400156
Simon Glass459c47a2014-02-26 15:59:26 -0700157 if (!str_gpio)
158 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400159
160 /* parse the behavior */
161 switch (*str_cmd) {
Simon Glasse70271f2019-01-21 14:53:20 -0700162 case 'i':
163 sub_cmd = GPIOC_INPUT;
164 break;
165 case 's':
166 sub_cmd = GPIOC_SET;
167 break;
168 case 'c':
169 sub_cmd = GPIOC_CLEAR;
170 break;
171 case 't':
172 sub_cmd = GPIOC_TOGGLE;
173 break;
174 default:
175 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400176 }
177
Simon Glass459c47a2014-02-26 15:59:26 -0700178#if defined(CONFIG_DM_GPIO)
179 /*
180 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
181 * framework, so we look up the name here and convert it to a GPIO number.
182 * Once all GPIO drivers are converted to driver model, we can change the
183 * code here to use the GPIO uclass interface instead of the numbered
184 * GPIO compatibility layer.
185 */
186 ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
Simon Glassf5330a42016-01-21 19:44:50 -0700187 if (ret) {
188 printf("GPIO: '%s' not found\n", str_gpio);
Simon Glass459c47a2014-02-26 15:59:26 -0700189 return cmd_process_error(cmdtp, ret);
Simon Glassf5330a42016-01-21 19:44:50 -0700190 }
Simon Glass459c47a2014-02-26 15:59:26 -0700191#else
Mike Frysinger0e71f802011-04-03 04:40:46 -0400192 /* turn the gpio name into a gpio number */
193 gpio = name_to_gpio(str_gpio);
194 if (gpio < 0)
195 goto show_usage;
Simon Glass459c47a2014-02-26 15:59:26 -0700196#endif
Mike Frysinger0e71f802011-04-03 04:40:46 -0400197 /* grab the pin before we tweak it */
Simon Glass23a6b8e2014-08-11 09:23:53 -0600198 ret = gpio_request(gpio, "cmd_gpio");
199 if (ret && ret != -EBUSY) {
Mike Frysinger15b25502011-04-12 03:02:11 -0400200 printf("gpio: requesting pin %u failed\n", gpio);
201 return -1;
202 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400203
204 /* finally, let's do it: set direction and exec command */
Simon Glasse70271f2019-01-21 14:53:20 -0700205 if (sub_cmd == GPIOC_INPUT) {
Mike Frysinger0e71f802011-04-03 04:40:46 -0400206 gpio_direction_input(gpio);
207 value = gpio_get_value(gpio);
208 } else {
209 switch (sub_cmd) {
Simon Glasse70271f2019-01-21 14:53:20 -0700210 case GPIOC_SET:
Simon Glassdb96baa2015-08-31 18:55:36 -0600211 value = 1;
212 break;
Simon Glasse70271f2019-01-21 14:53:20 -0700213 case GPIOC_CLEAR:
Simon Glassdb96baa2015-08-31 18:55:36 -0600214 value = 0;
215 break;
Simon Glasse70271f2019-01-21 14:53:20 -0700216 case GPIOC_TOGGLE:
Simon Glassdb96baa2015-08-31 18:55:36 -0600217 value = gpio_get_value(gpio);
218 if (!IS_ERR_VALUE(value))
219 value = !value;
220 break;
221 default:
222 goto show_usage;
Mike Frysinger0e71f802011-04-03 04:40:46 -0400223 }
224 gpio_direction_output(gpio, value);
225 }
Heinrich Schuchardtec8dfc32019-01-06 11:31:54 +0100226 printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio);
Simon Glassdb96baa2015-08-31 18:55:36 -0600227 if (IS_ERR_VALUE(value))
228 printf("unknown (ret=%d)\n", value);
229 else
230 printf("%d\n", value);
Simon Glasse70271f2019-01-21 14:53:20 -0700231 if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)) {
Simon Glassdb96baa2015-08-31 18:55:36 -0600232 int nval = gpio_get_value(gpio);
233
234 if (IS_ERR_VALUE(nval))
235 printf(" Warning: no access to GPIO output value\n");
236 else if (nval != value)
237 printf(" Warning: value of pin is still %d\n", nval);
238 }
Mike Frysinger0e71f802011-04-03 04:40:46 -0400239
Simon Glass23a6b8e2014-08-11 09:23:53 -0600240 if (ret != -EBUSY)
241 gpio_free(gpio);
Mike Frysinger0e71f802011-04-03 04:40:46 -0400242
243 return value;
244}
245
Simon Glass2d6b9232014-08-11 09:23:52 -0600246U_BOOT_CMD(gpio, 4, 0, do_gpio,
247 "query and control gpio pins",
248 "<input|set|clear|toggle> <pin>\n"
249 " - input/set/clear/toggle the specified pin\n"
250 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");