]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_gpio.c
dm: gpio: Enhance gpio command to show only active GPIOs
[karo-tx-uboot.git] / common / cmd_gpio.c
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>
11 #include <dm.h>
12 #include <asm/gpio.h>
13
14 int __weak name_to_gpio(const char *name)
15 {
16         return simple_strtoul(name, NULL, 10);
17 }
18
19 enum gpio_cmd {
20         GPIO_INPUT,
21         GPIO_SET,
22         GPIO_CLEAR,
23         GPIO_TOGGLE,
24 };
25
26 #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
27 static const char * const gpio_function[GPIOF_COUNT] = {
28         "input",
29         "output",
30         "unused",
31         "unknown",
32         "func",
33 };
34
35 /* A few flags used by show_gpio() */
36 enum {
37         FLAG_SHOW_ALL           = 1 << 0,
38         FLAG_SHOW_BANK          = 1 << 1,
39         FLAG_SHOW_NEWLINE       = 1 << 2,
40 };
41
42 static void show_gpio(struct udevice *dev, const char *bank_name, int offset,
43                       int *flagsp)
44 {
45         struct dm_gpio_ops *ops = gpio_get_ops(dev);
46         int func = GPIOF_UNKNOWN;
47         char buf[80];
48         int ret;
49
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         }
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 {
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
83 static int do_gpio_status(bool all, const char *gpio_name)
84 {
85         struct udevice *dev;
86         int banklen;
87         int flags;
88         int ret;
89
90         flags = 0;
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
99                 flags |= FLAG_SHOW_BANK;
100                 if (all)
101                         flags |= FLAG_SHOW_ALL;
102                 bank_name = gpio_get_bank_info(dev, &num_bits);
103                 if (!num_bits)
104                         continue;
105                 banklen = bank_name ? strlen(bank_name) : 0;
106
107                 if (!gpio_name || !bank_name ||
108                     !strncmp(gpio_name, bank_name, banklen)) {
109                         const char *p = NULL;
110                         int offset;
111
112                         p = gpio_name + banklen;
113                         if (gpio_name && *p) {
114                                 offset = simple_strtoul(p, NULL, 10);
115                                 show_gpio(dev, bank_name, offset, &flags);
116                         } else {
117                                 for (offset = 0; offset < num_bits; offset++) {
118                                         show_gpio(dev, bank_name, offset,
119                                                   &flags);
120                                 }
121                         }
122                 }
123                 /* Add a newline between bank names */
124                 if (!(flags & FLAG_SHOW_BANK))
125                         flags |= FLAG_SHOW_NEWLINE;
126         }
127
128         return ret;
129 }
130 #endif
131
132 static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
133 {
134         unsigned int gpio;
135         enum gpio_cmd sub_cmd;
136         ulong value;
137         const char *str_cmd, *str_gpio = NULL;
138 #ifdef CONFIG_DM_GPIO
139         bool all = false;
140         int ret;
141 #endif
142
143         if (argc < 2)
144  show_usage:
145                 return CMD_RET_USAGE;
146         str_cmd = argv[1];
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;
158         if (!strcmp(str_cmd, "status")) {
159                 /* Support deprecated gpio_status() */
160 #ifdef gpio_status
161                 gpio_status();
162                 return 0;
163 #elif defined(CONFIG_DM_GPIO)
164                 return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
165 #else
166                 goto show_usage;
167 #endif
168         }
169
170         if (!str_gpio)
171                 goto show_usage;
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
182 #if defined(CONFIG_DM_GPIO)
183         /*
184          * TODO(sjg@chromium.org): 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
194         /* turn the gpio name into a gpio number */
195         gpio = name_to_gpio(str_gpio);
196         if (gpio < 0)
197                 goto show_usage;
198 #endif
199         /* grab the pin before we tweak it */
200         if (gpio_request(gpio, "cmd_gpio")) {
201                 printf("gpio: requesting pin %u failed\n", gpio);
202                 return -1;
203         }
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
226 U_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");