]>
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.
15 #ifdef CONFIG_CMD_GPIO_READ
19 #include <linux/err.h>
20 #include <dm/device_compat.h>
22 __weak
int name_to_gpio(const char *name
)
24 return dectoul(name
, NULL
);
32 #ifdef CONFIG_CMD_GPIO_READ
37 #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
39 /* A few flags used by show_gpio() */
41 FLAG_SHOW_ALL
= 1 << 0,
42 FLAG_SHOW_BANK
= 1 << 1,
43 FLAG_SHOW_NEWLINE
= 1 << 2,
46 static void gpio_get_description(struct udevice
*dev
, const char *bank_name
,
47 int offset
, int *flagsp
, bool show_all
)
52 ret
= gpio_get_function(dev
, offset
, NULL
);
55 if (!show_all
&& !(*flagsp
& FLAG_SHOW_ALL
) && ret
== GPIOF_UNUSED
)
57 if ((*flagsp
& FLAG_SHOW_BANK
) && bank_name
) {
58 if (*flagsp
& FLAG_SHOW_NEWLINE
) {
60 *flagsp
&= ~FLAG_SHOW_NEWLINE
;
62 printf("Bank %s:\n", bank_name
);
63 *flagsp
&= ~FLAG_SHOW_BANK
;
66 ret
= gpio_get_status(dev
, offset
, buf
, sizeof(buf
));
74 printf("Error %d\n", ret
);
77 static int do_gpio_status(bool all
, const char *gpio_name
)
85 if (gpio_name
&& !*gpio_name
)
87 for (ret
= uclass_first_device_check(UCLASS_GPIO
, &dev
);
89 ret
= uclass_next_device_check(&dev
)) {
90 const char *bank_name
;
94 printf("GPIO device %s probe error %i\n",
100 flags
|= FLAG_SHOW_BANK
;
102 flags
|= FLAG_SHOW_ALL
;
103 bank_name
= gpio_get_bank_info(dev
, &num_bits
);
105 debug("GPIO device %s has no bits\n", dev
->name
);
108 banklen
= bank_name
? strlen(bank_name
) : 0;
110 if (!gpio_name
|| !bank_name
||
111 !strncasecmp(gpio_name
, bank_name
, banklen
)) {
115 p
= gpio_name
+ banklen
;
116 if (gpio_name
&& *p
) {
117 offset
= dectoul(p
, NULL
);
118 gpio_get_description(dev
, bank_name
, offset
,
121 for (offset
= 0; offset
< num_bits
; offset
++) {
122 gpio_get_description(dev
, bank_name
,
123 offset
, &flags
, false);
127 /* Add a newline between bank names */
128 if (!(flags
& FLAG_SHOW_BANK
))
129 flags
|= FLAG_SHOW_NEWLINE
;
136 static int do_gpio(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
140 enum gpio_cmd sub_cmd
;
142 const char *str_cmd
, *str_gpio
= NULL
;
143 #ifdef CONFIG_CMD_GPIO_READ
144 const char *str_var
= NULL
;
147 #ifdef CONFIG_DM_GPIO
153 return CMD_RET_USAGE
;
157 #ifdef CONFIG_DM_GPIO
158 if (argc
> 0 && !strncmp(str_cmd
, "status", 2) && !strcmp(*argv
, "-a")) {
164 #ifdef CONFIG_CMD_GPIO_READ
165 if (argc
> 0 && !strncmp(str_cmd
, "read", 2)) {
175 if (!strncmp(str_cmd
, "status", 2)) {
176 /* Support deprecated gpio_status() */
180 #elif defined(CONFIG_DM_GPIO)
181 return cmd_process_error(cmdtp
, do_gpio_status(all
, str_gpio
));
190 /* parse the behavior */
193 sub_cmd
= GPIOC_INPUT
;
199 sub_cmd
= GPIOC_CLEAR
;
202 sub_cmd
= GPIOC_TOGGLE
;
204 #ifdef CONFIG_CMD_GPIO_READ
206 sub_cmd
= GPIOC_READ
;
213 #if defined(CONFIG_DM_GPIO)
215 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
216 * framework, so we look up the name here and convert it to a GPIO number.
217 * Once all GPIO drivers are converted to driver model, we can change the
218 * code here to use the GPIO uclass interface instead of the numbered
219 * GPIO compatibility layer.
221 ret
= gpio_lookup_name(str_gpio
, NULL
, NULL
, &gpio
);
223 printf("GPIO: '%s' not found\n", str_gpio
);
224 return cmd_process_error(cmdtp
, ret
);
227 /* turn the gpio name into a gpio number */
228 gpio
= name_to_gpio(str_gpio
);
232 /* grab the pin before we tweak it */
233 ret
= gpio_request(gpio
, "cmd_gpio");
234 if (ret
&& ret
!= -EBUSY
) {
235 printf("gpio: requesting pin %u failed\n", gpio
);
239 /* finally, let's do it: set direction and exec command */
240 if (sub_cmd
== GPIOC_INPUT
241 #ifdef CONFIG_CMD_GPIO_READ
242 || sub_cmd
== GPIOC_READ
245 gpio_direction_input(gpio
);
246 value
= gpio_get_value(gpio
);
256 value
= gpio_get_value(gpio
);
257 if (!IS_ERR_VALUE(value
))
263 gpio_direction_output(gpio
, value
);
265 printf("gpio: pin %s (gpio %u) value is ", str_gpio
, gpio
);
267 if (IS_ERR_VALUE(value
)) {
268 printf("unknown (ret=%d)\n", value
);
271 printf("%d\n", value
);
272 #ifdef CONFIG_CMD_GPIO_READ
273 if (sub_cmd
== GPIOC_READ
)
274 env_set_ulong(str_var
, (ulong
)value
);
278 if (sub_cmd
!= GPIOC_INPUT
&& !IS_ERR_VALUE(value
)
279 #ifdef CONFIG_CMD_GPIO_READ
280 && sub_cmd
!= GPIOC_READ
283 int nval
= gpio_get_value(gpio
);
285 if (IS_ERR_VALUE(nval
)) {
286 printf(" Warning: no access to GPIO output value\n");
288 } else if (nval
!= value
) {
289 printf(" Warning: value of pin is still %d\n", nval
);
298 * Whilst wrong, the legacy gpio input command returns the pin
299 * value, or CMD_RET_FAILURE (which is indistinguishable from a
302 return (sub_cmd
== GPIOC_INPUT
) ? value
: CMD_RET_SUCCESS
;
307 return CMD_RET_FAILURE
;
310 U_BOOT_CMD(gpio
, 4, 0, do_gpio
,
311 "query and control gpio pins",
312 "<input|set|clear|toggle> <pin>\n"
313 " - input/set/clear/toggle the specified pin\n"
314 #ifdef CONFIG_CMD_GPIO_READ
315 "gpio read <name> <pin>\n"
316 " - set environment variable 'name' to the specified pin\n"
318 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");