blob: 8d946f42acc3e3602fb6407d0fb40ce0e34eac64 [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>
Mike Frysingera972b8d2011-04-03 04:40:46 -040013#include <asm/gpio.h>
14
Jeroen Hofstee5b5ac642014-10-08 22:58:03 +020015__weak int name_to_gpio(const char *name)
Ian Campbellfd11bea2014-03-27 20:34:13 +000016{
17 return simple_strtoul(name, NULL, 10);
18}
Mike Frysingera972b8d2011-04-03 04:40:46 -040019
20enum gpio_cmd {
21 GPIO_INPUT,
22 GPIO_SET,
23 GPIO_CLEAR,
24 GPIO_TOGGLE,
25};
26
Simon Glass95a260a92014-02-26 15:59:26 -070027#if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
Simon Glass89e64052014-08-11 09:23:52 -060028static const char * const gpio_function[GPIOF_COUNT] = {
Simon Glass95a260a92014-02-26 15:59:26 -070029 "input",
30 "output",
Simon Glass89e64052014-08-11 09:23:52 -060031 "unused",
Simon Glass95a260a92014-02-26 15:59:26 -070032 "unknown",
Simon Glass89e64052014-08-11 09:23:52 -060033 "func",
Simon Glass95a260a92014-02-26 15:59:26 -070034};
Mike Frysingera972b8d2011-04-03 04:40:46 -040035
Simon Glass89e64052014-08-11 09:23:52 -060036/* A few flags used by show_gpio() */
37enum {
38 FLAG_SHOW_ALL = 1 << 0,
39 FLAG_SHOW_BANK = 1 << 1,
40 FLAG_SHOW_NEWLINE = 1 << 2,
41};
42
43static void show_gpio(struct udevice *dev, const char *bank_name, int offset,
44 int *flagsp)
Simon Glass95a260a92014-02-26 15:59:26 -070045{
46 struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass89e64052014-08-11 09:23:52 -060047 int func = GPIOF_UNKNOWN;
Simon Glass95a260a92014-02-26 15:59:26 -070048 char buf[80];
49 int ret;
50
Simon Glass89e64052014-08-11 09:23:52 -060051 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
52
53 if (ops->get_function) {
54 ret = ops->get_function(dev, offset);
55 if (ret >= 0 && ret < ARRAY_SIZE(gpio_function))
56 func = ret;
57 }
58 if (!(*flagsp & FLAG_SHOW_ALL) && func == GPIOF_UNUSED)
59 return;
60 if ((*flagsp & FLAG_SHOW_BANK) && bank_name) {
61 if (*flagsp & FLAG_SHOW_NEWLINE) {
62 putc('\n');
63 *flagsp &= ~FLAG_SHOW_NEWLINE;
64 }
65 printf("Bank %s:\n", bank_name);
66 *flagsp &= ~FLAG_SHOW_BANK;
67 }
Simon Glass95a260a92014-02-26 15:59:26 -070068 *buf = '\0';
69 if (ops->get_state) {
70 ret = ops->get_state(dev, offset, buf, sizeof(buf));
71 if (ret) {
72 puts("<unknown>");
73 return;
74 }
75 } else {
Simon Glass95a260a92014-02-26 15:59:26 -070076 sprintf(buf, "%s%u: %8s %d", bank_name, offset,
77 gpio_function[func], ops->get_value(dev, offset));
Mike Frysingera972b8d2011-04-03 04:40:46 -040078 }
Simon Glass95a260a92014-02-26 15:59:26 -070079
80 puts(buf);
81 puts("\n");
82}
83
Simon Glass89e64052014-08-11 09:23:52 -060084static int do_gpio_status(bool all, const char *gpio_name)
Simon Glass95a260a92014-02-26 15:59:26 -070085{
Heiko Schocher54c5d082014-05-22 12:43:05 +020086 struct udevice *dev;
Simon Glass89e64052014-08-11 09:23:52 -060087 int banklen;
88 int flags;
Simon Glass95a260a92014-02-26 15:59:26 -070089 int ret;
90
Simon Glass89e64052014-08-11 09:23:52 -060091 flags = 0;
Simon Glass95a260a92014-02-26 15:59:26 -070092 if (gpio_name && !*gpio_name)
93 gpio_name = NULL;
94 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
95 dev;
96 ret = uclass_next_device(&dev)) {
97 const char *bank_name;
98 int num_bits;
99
Simon Glass89e64052014-08-11 09:23:52 -0600100 flags |= FLAG_SHOW_BANK;
101 if (all)
102 flags |= FLAG_SHOW_ALL;
Simon Glass95a260a92014-02-26 15:59:26 -0700103 bank_name = gpio_get_bank_info(dev, &num_bits);
Simon Glass89e64052014-08-11 09:23:52 -0600104 if (!num_bits)
105 continue;
106 banklen = bank_name ? strlen(bank_name) : 0;
Simon Glass95a260a92014-02-26 15:59:26 -0700107
108 if (!gpio_name || !bank_name ||
Simon Glass89e64052014-08-11 09:23:52 -0600109 !strncmp(gpio_name, bank_name, banklen)) {
Simon Glass95a260a92014-02-26 15:59:26 -0700110 const char *p = NULL;
111 int offset;
112
Simon Glass89e64052014-08-11 09:23:52 -0600113 p = gpio_name + banklen;
114 if (gpio_name && *p) {
Simon Glass95a260a92014-02-26 15:59:26 -0700115 offset = simple_strtoul(p, NULL, 10);
Simon Glass89e64052014-08-11 09:23:52 -0600116 show_gpio(dev, bank_name, offset, &flags);
Simon Glass95a260a92014-02-26 15:59:26 -0700117 } else {
Simon Glass89e64052014-08-11 09:23:52 -0600118 for (offset = 0; offset < num_bits; offset++) {
119 show_gpio(dev, bank_name, offset,
120 &flags);
121 }
Simon Glass95a260a92014-02-26 15:59:26 -0700122 }
123 }
Simon Glass89e64052014-08-11 09:23:52 -0600124 /* Add a newline between bank names */
125 if (!(flags & FLAG_SHOW_BANK))
126 flags |= FLAG_SHOW_NEWLINE;
Simon Glass95a260a92014-02-26 15:59:26 -0700127 }
128
129 return ret;
130}
Mike Frysingera972b8d2011-04-03 04:40:46 -0400131#endif
132
Simon Glass95a260a92014-02-26 15:59:26 -0700133static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
134{
135 unsigned int gpio;
136 enum gpio_cmd sub_cmd;
137 ulong value;
138 const char *str_cmd, *str_gpio = NULL;
Simon Glass9165e842014-08-11 09:23:53 -0600139 int ret;
Simon Glass95a260a92014-02-26 15:59:26 -0700140#ifdef CONFIG_DM_GPIO
Simon Glass89e64052014-08-11 09:23:52 -0600141 bool all = false;
Simon Glass95a260a92014-02-26 15:59:26 -0700142#endif
143
144 if (argc < 2)
Mike Frysingera972b8d2011-04-03 04:40:46 -0400145 show_usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000146 return CMD_RET_USAGE;
Mike Frysingera972b8d2011-04-03 04:40:46 -0400147 str_cmd = argv[1];
Simon Glass89e64052014-08-11 09:23:52 -0600148 argc -= 2;
149 argv += 2;
150#ifdef CONFIG_DM_GPIO
151 if (argc > 0 && !strcmp(*argv, "-a")) {
152 all = true;
153 argc--;
154 argv++;
155 }
156#endif
157 if (argc > 0)
158 str_gpio = *argv;
Simon Glass95a260a92014-02-26 15:59:26 -0700159 if (!strcmp(str_cmd, "status")) {
160 /* Support deprecated gpio_status() */
161#ifdef gpio_status
162 gpio_status();
163 return 0;
164#elif defined(CONFIG_DM_GPIO)
Simon Glass89e64052014-08-11 09:23:52 -0600165 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
Simon Glass95a260a92014-02-26 15:59:26 -0700166#else
167 goto show_usage;
168#endif
169 }
170
171 if (!str_gpio)
172 goto show_usage;
Mike Frysingera972b8d2011-04-03 04:40:46 -0400173
174 /* parse the behavior */
175 switch (*str_cmd) {
176 case 'i': sub_cmd = GPIO_INPUT; break;
177 case 's': sub_cmd = GPIO_SET; break;
178 case 'c': sub_cmd = GPIO_CLEAR; break;
179 case 't': sub_cmd = GPIO_TOGGLE; break;
180 default: goto show_usage;
181 }
182
Simon Glass95a260a92014-02-26 15:59:26 -0700183#if defined(CONFIG_DM_GPIO)
184 /*
185 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
186 * framework, so we look up the name here and convert it to a GPIO number.
187 * Once all GPIO drivers are converted to driver model, we can change the
188 * code here to use the GPIO uclass interface instead of the numbered
189 * GPIO compatibility layer.
190 */
191 ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
192 if (ret)
193 return cmd_process_error(cmdtp, ret);
194#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 */
208 if (sub_cmd == GPIO_INPUT) {
209 gpio_direction_input(gpio);
210 value = gpio_get_value(gpio);
211 } else {
212 switch (sub_cmd) {
213 case GPIO_SET: value = 1; break;
214 case GPIO_CLEAR: value = 0; break;
215 case GPIO_TOGGLE: value = !gpio_get_value(gpio); break;
216 default: goto show_usage;
217 }
218 gpio_direction_output(gpio, value);
219 }
220 printf("gpio: pin %s (gpio %i) value is %lu\n",
221 str_gpio, gpio, value);
222
Simon Glass9165e842014-08-11 09:23:53 -0600223 if (ret != -EBUSY)
224 gpio_free(gpio);
Mike Frysingera972b8d2011-04-03 04:40:46 -0400225
226 return value;
227}
228
Simon Glass89e64052014-08-11 09:23:52 -0600229U_BOOT_CMD(gpio, 4, 0, do_gpio,
230 "query and control gpio pins",
231 "<input|set|clear|toggle> <pin>\n"
232 " - input/set/clear/toggle the specified pin\n"
233 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");