]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - cmd/gpio.c
2 * Control GPIO pins on the fly
4 * Copyright (c) 2008-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
16 #include <linux/err.h>
18 __weak
int name_to_gpio(const char *name
)
20 return simple_strtoul(name
, NULL
, 10);
30 #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
32 /* A few flags used by show_gpio() */
34 FLAG_SHOW_ALL
= 1 << 0,
35 FLAG_SHOW_BANK
= 1 << 1,
36 FLAG_SHOW_NEWLINE
= 1 << 2,
39 static void gpio_get_description(struct udevice
*dev
, const char *bank_name
,
40 int offset
, int *flagsp
, bool show_all
)
45 ret
= gpio_get_function(dev
, offset
, NULL
);
48 if (!show_all
&& !(*flagsp
& FLAG_SHOW_ALL
) && ret
== GPIOF_UNUSED
)
50 if ((*flagsp
& FLAG_SHOW_BANK
) && bank_name
) {
51 if (*flagsp
& FLAG_SHOW_NEWLINE
) {
53 *flagsp
&= ~FLAG_SHOW_NEWLINE
;
55 printf("Bank %s:\n", bank_name
);
56 *flagsp
&= ~FLAG_SHOW_BANK
;
59 ret
= gpio_get_status(dev
, offset
, buf
, sizeof(buf
));
66 printf("Error %d\n", ret
);
69 static int do_gpio_status(bool all
, const char *gpio_name
)
77 if (gpio_name
&& !*gpio_name
)
79 for (ret
= uclass_first_device(UCLASS_GPIO
, &dev
);
81 ret
= uclass_next_device(&dev
)) {
82 const char *bank_name
;
85 flags
|= FLAG_SHOW_BANK
;
87 flags
|= FLAG_SHOW_ALL
;
88 bank_name
= gpio_get_bank_info(dev
, &num_bits
);
90 debug("GPIO device %s has no bits\n", dev
->name
);
93 banklen
= bank_name
? strlen(bank_name
) : 0;
95 if (!gpio_name
|| !bank_name
||
96 !strncasecmp(gpio_name
, bank_name
, banklen
)) {
100 p
= gpio_name
+ banklen
;
101 if (gpio_name
&& *p
) {
102 offset
= simple_strtoul(p
, NULL
, 10);
103 gpio_get_description(dev
, bank_name
, offset
,
106 for (offset
= 0; offset
< num_bits
; offset
++) {
107 gpio_get_description(dev
, bank_name
,
108 offset
, &flags
, false);
112 /* Add a newline between bank names */
113 if (!(flags
& FLAG_SHOW_BANK
))
114 flags
|= FLAG_SHOW_NEWLINE
;
121 static int do_gpio(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
125 enum gpio_cmd sub_cmd
;
127 const char *str_cmd
, *str_gpio
= NULL
;
129 #ifdef CONFIG_DM_GPIO
135 return CMD_RET_USAGE
;
139 #ifdef CONFIG_DM_GPIO
140 if (argc
> 0 && !strcmp(*argv
, "-a")) {
148 if (!strncmp(str_cmd
, "status", 2)) {
149 /* Support deprecated gpio_status() */
153 #elif defined(CONFIG_DM_GPIO)
154 return cmd_process_error(cmdtp
, do_gpio_status(all
, str_gpio
));
163 /* parse the behavior */
166 sub_cmd
= GPIOC_INPUT
;
172 sub_cmd
= GPIOC_CLEAR
;
175 sub_cmd
= GPIOC_TOGGLE
;
181 #if defined(CONFIG_DM_GPIO)
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.
189 ret
= gpio_lookup_name(str_gpio
, NULL
, NULL
, &gpio
);
191 printf("GPIO: '%s' not found\n", str_gpio
);
192 return cmd_process_error(cmdtp
, ret
);
195 /* turn the gpio name into a gpio number */
196 gpio
= name_to_gpio(str_gpio
);
200 /* grab the pin before we tweak it */
201 ret
= gpio_request(gpio
, "cmd_gpio");
202 if (ret
&& ret
!= -EBUSY
) {
203 printf("gpio: requesting pin %u failed\n", gpio
);
207 /* finally, let's do it: set direction and exec command */
208 if (sub_cmd
== GPIOC_INPUT
) {
209 gpio_direction_input(gpio
);
210 value
= gpio_get_value(gpio
);
220 value
= gpio_get_value(gpio
);
221 if (!IS_ERR_VALUE(value
))
227 gpio_direction_output(gpio
, value
);
229 printf("gpio: pin %s (gpio %u) value is ", str_gpio
, gpio
);
231 if (IS_ERR_VALUE(value
)) {
232 printf("unknown (ret=%d)\n", value
);
235 printf("%d\n", value
);
238 if (sub_cmd
!= GPIOC_INPUT
&& !IS_ERR_VALUE(value
)) {
239 int nval
= gpio_get_value(gpio
);
241 if (IS_ERR_VALUE(nval
)) {
242 printf(" Warning: no access to GPIO output value\n");
244 } else if (nval
!= value
) {
245 printf(" Warning: value of pin is still %d\n", nval
);
254 * Whilst wrong, the legacy gpio input command returns the pin
255 * value, or CMD_RET_FAILURE (which is indistinguishable from a
258 return (sub_cmd
== GPIOC_INPUT
) ? value
: CMD_RET_SUCCESS
;
263 return CMD_RET_FAILURE
;
266 U_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");