]> Git Repo - J-u-boot.git/blame - common/cmd_gpio.c
dm: gpio: Enhance gpio command to show only active GPIOs
[J-u-boot.git] / common / cmd_gpio.c
CommitLineData
a972b8d7
MF
1/*
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>
95a260a9 11#include <dm.h>
a972b8d7
MF
12#include <asm/gpio.h>
13
fd11bea2
IC
14int __weak name_to_gpio(const char *name)
15{
16 return simple_strtoul(name, NULL, 10);
17}
a972b8d7
MF
18
19enum gpio_cmd {
20 GPIO_INPUT,
21 GPIO_SET,
22 GPIO_CLEAR,
23 GPIO_TOGGLE,
24};
25
95a260a9 26#if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
89e64054 27static const char * const gpio_function[GPIOF_COUNT] = {
95a260a9
SG
28 "input",
29 "output",
89e64054 30 "unused",
95a260a9 31 "unknown",
89e64054 32 "func",
95a260a9
SG
33};
34
89e64054
SG
35/* A few flags used by show_gpio() */
36enum {
37 FLAG_SHOW_ALL = 1 << 0,
38 FLAG_SHOW_BANK = 1 << 1,
39 FLAG_SHOW_NEWLINE = 1 << 2,
40};
41
42static void show_gpio(struct udevice *dev, const char *bank_name, int offset,
43 int *flagsp)
95a260a9
SG
44{
45 struct dm_gpio_ops *ops = gpio_get_ops(dev);
89e64054 46 int func = GPIOF_UNKNOWN;
95a260a9
SG
47 char buf[80];
48 int ret;
49
89e64054
SG
50 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
51
52 if (ops->get_function) {
53 ret = ops->get_function(dev, offset);
54 if (ret >= 0 && ret < ARRAY_SIZE(gpio_function))
55 func = ret;
56 }
57 if (!(*flagsp & FLAG_SHOW_ALL) && func == GPIOF_UNUSED)
58 return;
59 if ((*flagsp & FLAG_SHOW_BANK) && bank_name) {
60 if (*flagsp & FLAG_SHOW_NEWLINE) {
61 putc('\n');
62 *flagsp &= ~FLAG_SHOW_NEWLINE;
63 }
64 printf("Bank %s:\n", bank_name);
65 *flagsp &= ~FLAG_SHOW_BANK;
66 }
95a260a9
SG
67 *buf = '\0';
68 if (ops->get_state) {
69 ret = ops->get_state(dev, offset, buf, sizeof(buf));
70 if (ret) {
71 puts("<unknown>");
72 return;
73 }
74 } else {
95a260a9
SG
75 sprintf(buf, "%s%u: %8s %d", bank_name, offset,
76 gpio_function[func], ops->get_value(dev, offset));
77 }
78
79 puts(buf);
80 puts("\n");
81}
82
89e64054 83static int do_gpio_status(bool all, const char *gpio_name)
95a260a9 84{
54c5d08a 85 struct udevice *dev;
89e64054
SG
86 int banklen;
87 int flags;
95a260a9
SG
88 int ret;
89
89e64054 90 flags = 0;
95a260a9
SG
91 if (gpio_name && !*gpio_name)
92 gpio_name = NULL;
93 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
94 dev;
95 ret = uclass_next_device(&dev)) {
96 const char *bank_name;
97 int num_bits;
98
89e64054
SG
99 flags |= FLAG_SHOW_BANK;
100 if (all)
101 flags |= FLAG_SHOW_ALL;
95a260a9 102 bank_name = gpio_get_bank_info(dev, &num_bits);
89e64054
SG
103 if (!num_bits)
104 continue;
105 banklen = bank_name ? strlen(bank_name) : 0;
95a260a9
SG
106
107 if (!gpio_name || !bank_name ||
89e64054 108 !strncmp(gpio_name, bank_name, banklen)) {
95a260a9
SG
109 const char *p = NULL;
110 int offset;
111
89e64054
SG
112 p = gpio_name + banklen;
113 if (gpio_name && *p) {
95a260a9 114 offset = simple_strtoul(p, NULL, 10);
89e64054 115 show_gpio(dev, bank_name, offset, &flags);
95a260a9 116 } else {
89e64054
SG
117 for (offset = 0; offset < num_bits; offset++) {
118 show_gpio(dev, bank_name, offset,
119 &flags);
120 }
95a260a9
SG
121 }
122 }
89e64054
SG
123 /* Add a newline between bank names */
124 if (!(flags & FLAG_SHOW_BANK))
125 flags |= FLAG_SHOW_NEWLINE;
95a260a9
SG
126 }
127
128 return ret;
129}
130#endif
131
a972b8d7
MF
132static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
133{
95a260a9 134 unsigned int gpio;
a972b8d7
MF
135 enum gpio_cmd sub_cmd;
136 ulong value;
95a260a9
SG
137 const char *str_cmd, *str_gpio = NULL;
138#ifdef CONFIG_DM_GPIO
89e64054 139 bool all = false;
95a260a9
SG
140 int ret;
141#endif
a972b8d7 142
95a260a9
SG
143 if (argc < 2)
144 show_usage:
145 return CMD_RET_USAGE;
146 str_cmd = argv[1];
89e64054
SG
147 argc -= 2;
148 argv += 2;
149#ifdef CONFIG_DM_GPIO
150 if (argc > 0 && !strcmp(*argv, "-a")) {
151 all = true;
152 argc--;
153 argv++;
154 }
155#endif
156 if (argc > 0)
157 str_gpio = *argv;
95a260a9
SG
158 if (!strcmp(str_cmd, "status")) {
159 /* Support deprecated gpio_status() */
a972b8d7 160#ifdef gpio_status
a972b8d7
MF
161 gpio_status();
162 return 0;
95a260a9 163#elif defined(CONFIG_DM_GPIO)
89e64054 164 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
95a260a9
SG
165#else
166 goto show_usage;
a972b8d7 167#endif
95a260a9 168 }
a972b8d7 169
95a260a9
SG
170 if (!str_gpio)
171 goto show_usage;
a972b8d7
MF
172
173 /* parse the behavior */
174 switch (*str_cmd) {
175 case 'i': sub_cmd = GPIO_INPUT; break;
176 case 's': sub_cmd = GPIO_SET; break;
177 case 'c': sub_cmd = GPIO_CLEAR; break;
178 case 't': sub_cmd = GPIO_TOGGLE; break;
179 default: goto show_usage;
180 }
181
95a260a9
SG
182#if defined(CONFIG_DM_GPIO)
183 /*
184 * TODO([email protected]): For now we must fit into the existing GPIO
185 * framework, so we look up the name here and convert it to a GPIO number.
186 * Once all GPIO drivers are converted to driver model, we can change the
187 * code here to use the GPIO uclass interface instead of the numbered
188 * GPIO compatibility layer.
189 */
190 ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
191 if (ret)
192 return cmd_process_error(cmdtp, ret);
193#else
a972b8d7
MF
194 /* turn the gpio name into a gpio number */
195 gpio = name_to_gpio(str_gpio);
196 if (gpio < 0)
197 goto show_usage;
95a260a9 198#endif
a972b8d7 199 /* grab the pin before we tweak it */
6801201e
MF
200 if (gpio_request(gpio, "cmd_gpio")) {
201 printf("gpio: requesting pin %u failed\n", gpio);
202 return -1;
203 }
a972b8d7
MF
204
205 /* finally, let's do it: set direction and exec command */
206 if (sub_cmd == GPIO_INPUT) {
207 gpio_direction_input(gpio);
208 value = gpio_get_value(gpio);
209 } else {
210 switch (sub_cmd) {
211 case GPIO_SET: value = 1; break;
212 case GPIO_CLEAR: value = 0; break;
213 case GPIO_TOGGLE: value = !gpio_get_value(gpio); break;
214 default: goto show_usage;
215 }
216 gpio_direction_output(gpio, value);
217 }
218 printf("gpio: pin %s (gpio %i) value is %lu\n",
219 str_gpio, gpio, value);
220
221 gpio_free(gpio);
222
223 return value;
224}
225
89e64054
SG
226U_BOOT_CMD(gpio, 4, 0, do_gpio,
227 "query and control gpio pins",
228 "<input|set|clear|toggle> <pin>\n"
229 " - input/set/clear/toggle the specified pin\n"
230 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");
This page took 0.202876 seconds and 4 git commands to generate.