blob: 4150024e628ea78790b95666c0984c33c183750a [file] [log] [blame]
Mike Frysingera972b8d2011-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 Glass9165e842014-08-11 09:23:53 -060011#include <errno.h>
Simon Glass95a260a92014-02-26 15:59:26 -070012#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070014#include <malloc.h>
Mike Frysingera972b8d2011-04-03 04:40:46 -040015#include <asm/gpio.h>
Simon Glass61b29b82020-02-03 07:36:15 -070016#include <linux/err.h>
Mike Frysingera972b8d2011-04-03 04:40:46 -040017
Jeroen Hofstee5b5ac642014-10-08 22:58:03 +020018__weak int name_to_gpio(const char *name)
Ian Campbellfd11bea2014-03-27 20:34:13 +000019{
Simon Glass0b1284e2021-07-24 09:03:30 -060020 return dectoul(name, NULL);
Ian Campbellfd11bea2014-03-27 20:34:13 +000021}
Mike Frysingera972b8d2011-04-03 04:40:46 -040022
23enum gpio_cmd {
Simon Glass66f657d2019-01-21 14:53:20 -070024 GPIOC_INPUT,
25 GPIOC_SET,
26 GPIOC_CLEAR,
27 GPIOC_TOGGLE,
Mike Frysingera972b8d2011-04-03 04:40:46 -040028};
29
Simon Glass95a260a92014-02-26 15:59:26 -070030#if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
Mike Frysingera972b8d2011-04-03 04:40:46 -040031
Simon Glass89e64052014-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 Glass07575352014-10-04 11:29:44 -060039static void gpio_get_description(struct udevice *dev, const char *bank_name,
Simon Glassb8989b52019-02-16 20:24:42 -070040 int offset, int *flagsp, bool show_all)
Simon Glass95a260a92014-02-26 15:59:26 -070041{
Simon Glass95a260a92014-02-26 15:59:26 -070042 char buf[80];
43 int ret;
44
Simon Glass07575352014-10-04 11:29:44 -060045 ret = gpio_get_function(dev, offset, NULL);
46 if (ret < 0)
47 goto err;
Simon Glassb8989b52019-02-16 20:24:42 -070048 if (!show_all && !(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED)
Simon Glass89e64052014-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 Glass95a260a92014-02-26 15:59:26 -070058
Simon Glass07575352014-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 Glass95a260a92014-02-26 15:59:26 -070067}
68
Simon Glass89e64052014-08-11 09:23:52 -060069static int do_gpio_status(bool all, const char *gpio_name)
Simon Glass95a260a92014-02-26 15:59:26 -070070{
Heiko Schocher54c5d082014-05-22 12:43:05 +020071 struct udevice *dev;
Simon Glass89e64052014-08-11 09:23:52 -060072 int banklen;
73 int flags;
Simon Glass95a260a92014-02-26 15:59:26 -070074 int ret;
75
Simon Glass89e64052014-08-11 09:23:52 -060076 flags = 0;
Simon Glass95a260a92014-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 Glass89e64052014-08-11 09:23:52 -060085 flags |= FLAG_SHOW_BANK;
86 if (all)
87 flags |= FLAG_SHOW_ALL;
Simon Glass95a260a92014-02-26 15:59:26 -070088 bank_name = gpio_get_bank_info(dev, &num_bits);
Simon Glass07575352014-10-04 11:29:44 -060089 if (!num_bits) {
90 debug("GPIO device %s has no bits\n", dev->name);
Simon Glass89e64052014-08-11 09:23:52 -060091 continue;
Simon Glass07575352014-10-04 11:29:44 -060092 }
Simon Glass89e64052014-08-11 09:23:52 -060093 banklen = bank_name ? strlen(bank_name) : 0;
Simon Glass95a260a92014-02-26 15:59:26 -070094
95 if (!gpio_name || !bank_name ||
Simon Glass48ca6902019-02-16 20:24:43 -070096 !strncasecmp(gpio_name, bank_name, banklen)) {
Heinrich Schuchardte946b5d2019-08-22 22:19:41 +020097 const char *p;
Simon Glass95a260a92014-02-26 15:59:26 -070098 int offset;
99
Simon Glass89e64052014-08-11 09:23:52 -0600100 p = gpio_name + banklen;
101 if (gpio_name && *p) {
Simon Glass0b1284e2021-07-24 09:03:30 -0600102 offset = dectoul(p, NULL);
Simon Glass07575352014-10-04 11:29:44 -0600103 gpio_get_description(dev, bank_name, offset,
Simon Glassb8989b52019-02-16 20:24:42 -0700104 &flags, true);
Simon Glass95a260a92014-02-26 15:59:26 -0700105 } else {
Simon Glass89e64052014-08-11 09:23:52 -0600106 for (offset = 0; offset < num_bits; offset++) {
Simon Glass07575352014-10-04 11:29:44 -0600107 gpio_get_description(dev, bank_name,
Simon Glassb8989b52019-02-16 20:24:42 -0700108 offset, &flags, false);
Simon Glass89e64052014-08-11 09:23:52 -0600109 }
Simon Glass95a260a92014-02-26 15:59:26 -0700110 }
111 }
Simon Glass89e64052014-08-11 09:23:52 -0600112 /* Add a newline between bank names */
113 if (!(flags & FLAG_SHOW_BANK))
114 flags |= FLAG_SHOW_NEWLINE;
Simon Glass95a260a92014-02-26 15:59:26 -0700115 }
116
117 return ret;
118}
Mike Frysingera972b8d2011-04-03 04:40:46 -0400119#endif
120
Simon Glass09140112020-05-10 11:40:03 -0600121static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,
122 char *const argv[])
Simon Glass95a260a92014-02-26 15:59:26 -0700123{
124 unsigned int gpio;
125 enum gpio_cmd sub_cmd;
Simon Glassb71bea72015-08-31 18:55:36 -0600126 int value;
Simon Glass95a260a92014-02-26 15:59:26 -0700127 const char *str_cmd, *str_gpio = NULL;
Simon Glass9165e842014-08-11 09:23:53 -0600128 int ret;
Simon Glass95a260a92014-02-26 15:59:26 -0700129#ifdef CONFIG_DM_GPIO
Simon Glass89e64052014-08-11 09:23:52 -0600130 bool all = false;
Simon Glass95a260a92014-02-26 15:59:26 -0700131#endif
132
133 if (argc < 2)
Mike Frysingera972b8d2011-04-03 04:40:46 -0400134 show_usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000135 return CMD_RET_USAGE;
Mike Frysingera972b8d2011-04-03 04:40:46 -0400136 str_cmd = argv[1];
Simon Glass89e64052014-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 Glass4c80c532016-02-14 16:28:59 -0700148 if (!strncmp(str_cmd, "status", 2)) {
Simon Glass95a260a92014-02-26 15:59:26 -0700149 /* Support deprecated gpio_status() */
150#ifdef gpio_status
151 gpio_status();
152 return 0;
153#elif defined(CONFIG_DM_GPIO)
Simon Glass89e64052014-08-11 09:23:52 -0600154 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
Simon Glass95a260a92014-02-26 15:59:26 -0700155#else
156 goto show_usage;
157#endif
158 }
159
160 if (!str_gpio)
161 goto show_usage;
Mike Frysingera972b8d2011-04-03 04:40:46 -0400162
163 /* parse the behavior */
164 switch (*str_cmd) {
Simon Glass66f657d2019-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 Frysingera972b8d2011-04-03 04:40:46 -0400179 }
180
Simon Glass95a260a92014-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 Glass66280e82016-01-21 19:44:50 -0700190 if (ret) {
191 printf("GPIO: '%s' not found\n", str_gpio);
Simon Glass95a260a92014-02-26 15:59:26 -0700192 return cmd_process_error(cmdtp, ret);
Simon Glass66280e82016-01-21 19:44:50 -0700193 }
Simon Glass95a260a92014-02-26 15:59:26 -0700194#else
Mike Frysingera972b8d2011-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 Glass95a260a92014-02-26 15:59:26 -0700199#endif
Mike Frysingera972b8d2011-04-03 04:40:46 -0400200 /* grab the pin before we tweak it */
Simon Glass9165e842014-08-11 09:23:53 -0600201 ret = gpio_request(gpio, "cmd_gpio");
202 if (ret && ret != -EBUSY) {
Mike Frysinger68012012011-04-12 03:02:11 -0400203 printf("gpio: requesting pin %u failed\n", gpio);
204 return -1;
205 }
Mike Frysingera972b8d2011-04-03 04:40:46 -0400206
207 /* finally, let's do it: set direction and exec command */
Simon Glass66f657d2019-01-21 14:53:20 -0700208 if (sub_cmd == GPIOC_INPUT) {
Mike Frysingera972b8d2011-04-03 04:40:46 -0400209 gpio_direction_input(gpio);
210 value = gpio_get_value(gpio);
211 } else {
212 switch (sub_cmd) {
Simon Glass66f657d2019-01-21 14:53:20 -0700213 case GPIOC_SET:
Simon Glassb71bea72015-08-31 18:55:36 -0600214 value = 1;
215 break;
Simon Glass66f657d2019-01-21 14:53:20 -0700216 case GPIOC_CLEAR:
Simon Glassb71bea72015-08-31 18:55:36 -0600217 value = 0;
218 break;
Simon Glass66f657d2019-01-21 14:53:20 -0700219 case GPIOC_TOGGLE:
Simon Glassb71bea72015-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 Frysingera972b8d2011-04-03 04:40:46 -0400226 }
227 gpio_direction_output(gpio, value);
228 }
Heinrich Schuchardtb1970012019-01-06 11:31:54 +0100229 printf("gpio: pin %s (gpio %u) value is ", str_gpio, gpio);
Luka Kovacic4dbc1072020-01-05 20:10:56 +0100230
231 if (IS_ERR_VALUE(value)) {
Simon Glassb71bea72015-08-31 18:55:36 -0600232 printf("unknown (ret=%d)\n", value);
Luka Kovacic4dbc1072020-01-05 20:10:56 +0100233 goto err;
234 } else {
Simon Glassb71bea72015-08-31 18:55:36 -0600235 printf("%d\n", value);
Luka Kovacic4dbc1072020-01-05 20:10:56 +0100236 }
237
Simon Glass66f657d2019-01-21 14:53:20 -0700238 if (sub_cmd != GPIOC_INPUT && !IS_ERR_VALUE(value)) {
Simon Glassb71bea72015-08-31 18:55:36 -0600239 int nval = gpio_get_value(gpio);
240
Luka Kovacic4dbc1072020-01-05 20:10:56 +0100241 if (IS_ERR_VALUE(nval)) {
Simon Glassb71bea72015-08-31 18:55:36 -0600242 printf(" Warning: no access to GPIO output value\n");
Luka Kovacic4dbc1072020-01-05 20:10:56 +0100243 goto err;
244 } else if (nval != value) {
Simon Glassb71bea72015-08-31 18:55:36 -0600245 printf(" Warning: value of pin is still %d\n", nval);
Luka Kovacic4dbc1072020-01-05 20:10:56 +0100246 goto err;
247 }
Simon Glassb71bea72015-08-31 18:55:36 -0600248 }
Mike Frysingera972b8d2011-04-03 04:40:46 -0400249
Simon Glass9165e842014-08-11 09:23:53 -0600250 if (ret != -EBUSY)
251 gpio_free(gpio);
Mike Frysingera972b8d2011-04-03 04:40:46 -0400252
Alex Kiernan4af2a332020-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 Kovacic4dbc1072020-01-05 20:10:56 +0100259
260err:
261 if (ret != -EBUSY)
262 gpio_free(gpio);
263 return CMD_RET_FAILURE;
Mike Frysingera972b8d2011-04-03 04:40:46 -0400264}
265
Simon Glass89e64052014-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");