]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/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.
15 __weak
int name_to_gpio(const char *name
)
17 return simple_strtoul(name
, NULL
, 10);
27 #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
29 /* A few flags used by show_gpio() */
31 FLAG_SHOW_ALL
= 1 << 0,
32 FLAG_SHOW_BANK
= 1 << 1,
33 FLAG_SHOW_NEWLINE
= 1 << 2,
36 static void gpio_get_description(struct udevice
*dev
, const char *bank_name
,
37 int offset
, int *flagsp
)
42 ret
= gpio_get_function(dev
, offset
, NULL
);
45 if (!(*flagsp
& FLAG_SHOW_ALL
) && ret
== GPIOF_UNUSED
)
47 if ((*flagsp
& FLAG_SHOW_BANK
) && bank_name
) {
48 if (*flagsp
& FLAG_SHOW_NEWLINE
) {
50 *flagsp
&= ~FLAG_SHOW_NEWLINE
;
52 printf("Bank %s:\n", bank_name
);
53 *flagsp
&= ~FLAG_SHOW_BANK
;
56 ret
= gpio_get_status(dev
, offset
, buf
, sizeof(buf
));
63 printf("Error %d\n", ret
);
66 static int do_gpio_status(bool all
, const char *gpio_name
)
74 if (gpio_name
&& !*gpio_name
)
76 for (ret
= uclass_first_device(UCLASS_GPIO
, &dev
);
78 ret
= uclass_next_device(&dev
)) {
79 const char *bank_name
;
82 flags
|= FLAG_SHOW_BANK
;
84 flags
|= FLAG_SHOW_ALL
;
85 bank_name
= gpio_get_bank_info(dev
, &num_bits
);
87 debug("GPIO device %s has no bits\n", dev
->name
);
90 banklen
= bank_name
? strlen(bank_name
) : 0;
92 if (!gpio_name
|| !bank_name
||
93 !strncmp(gpio_name
, bank_name
, banklen
)) {
97 p
= gpio_name
+ banklen
;
98 if (gpio_name
&& *p
) {
99 offset
= simple_strtoul(p
, NULL
, 10);
100 gpio_get_description(dev
, bank_name
, offset
,
103 for (offset
= 0; offset
< num_bits
; offset
++) {
104 gpio_get_description(dev
, bank_name
,
109 /* Add a newline between bank names */
110 if (!(flags
& FLAG_SHOW_BANK
))
111 flags
|= FLAG_SHOW_NEWLINE
;
118 static int do_gpio(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
121 enum gpio_cmd sub_cmd
;
123 const char *str_cmd
, *str_gpio
= NULL
;
125 #ifdef CONFIG_DM_GPIO
131 return CMD_RET_USAGE
;
135 #ifdef CONFIG_DM_GPIO
136 if (argc
> 0 && !strcmp(*argv
, "-a")) {
144 if (!strcmp(str_cmd
, "status")) {
145 /* Support deprecated gpio_status() */
149 #elif defined(CONFIG_DM_GPIO)
150 return cmd_process_error(cmdtp
, do_gpio_status(all
, str_gpio
));
159 /* parse the behavior */
161 case 'i': sub_cmd
= GPIO_INPUT
; break;
162 case 's': sub_cmd
= GPIO_SET
; break;
163 case 'c': sub_cmd
= GPIO_CLEAR
; break;
164 case 't': sub_cmd
= GPIO_TOGGLE
; break;
165 default: goto show_usage
;
168 #if defined(CONFIG_DM_GPIO)
170 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
171 * framework, so we look up the name here and convert it to a GPIO number.
172 * Once all GPIO drivers are converted to driver model, we can change the
173 * code here to use the GPIO uclass interface instead of the numbered
174 * GPIO compatibility layer.
176 ret
= gpio_lookup_name(str_gpio
, NULL
, NULL
, &gpio
);
178 return cmd_process_error(cmdtp
, ret
);
180 /* turn the gpio name into a gpio number */
181 gpio
= name_to_gpio(str_gpio
);
185 /* grab the pin before we tweak it */
186 ret
= gpio_request(gpio
, "cmd_gpio");
187 if (ret
&& ret
!= -EBUSY
) {
188 printf("gpio: requesting pin %u failed\n", gpio
);
192 /* finally, let's do it: set direction and exec command */
193 if (sub_cmd
== GPIO_INPUT
) {
194 gpio_direction_input(gpio
);
195 value
= gpio_get_value(gpio
);
198 case GPIO_SET
: value
= 1; break;
199 case GPIO_CLEAR
: value
= 0; break;
200 case GPIO_TOGGLE
: value
= !gpio_get_value(gpio
); break;
201 default: goto show_usage
;
203 gpio_direction_output(gpio
, value
);
205 printf("gpio: pin %s (gpio %i) value is %lu\n",
206 str_gpio
, gpio
, value
);
214 U_BOOT_CMD(gpio
, 4, 0, do_gpio
,
215 "query and control gpio pins",
216 "<input|set|clear|toggle> <pin>\n"
217 " - input/set/clear/toggle the specified pin\n"
218 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");