]>
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 int __weak
name_to_gpio(const char *name
)
17 return simple_strtoul(name
, NULL
, 10);
27 #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
28 static const char * const gpio_function
[GPIOF_COUNT
] = {
36 /* A few flags used by show_gpio() */
38 FLAG_SHOW_ALL
= 1 << 0,
39 FLAG_SHOW_BANK
= 1 << 1,
40 FLAG_SHOW_NEWLINE
= 1 << 2,
43 static void show_gpio(struct udevice
*dev
, const char *bank_name
, int offset
,
46 struct dm_gpio_ops
*ops
= gpio_get_ops(dev
);
47 int func
= GPIOF_UNKNOWN
;
51 BUILD_BUG_ON(GPIOF_COUNT
!= ARRAY_SIZE(gpio_function
));
53 if (ops
->get_function
) {
54 ret
= ops
->get_function(dev
, offset
);
55 if (ret
>= 0 && ret
< ARRAY_SIZE(gpio_function
))
58 if (!(*flagsp
& FLAG_SHOW_ALL
) && func
== GPIOF_UNUSED
)
60 if ((*flagsp
& FLAG_SHOW_BANK
) && bank_name
) {
61 if (*flagsp
& FLAG_SHOW_NEWLINE
) {
63 *flagsp
&= ~FLAG_SHOW_NEWLINE
;
65 printf("Bank %s:\n", bank_name
);
66 *flagsp
&= ~FLAG_SHOW_BANK
;
70 ret
= ops
->get_state(dev
, offset
, buf
, sizeof(buf
));
76 sprintf(buf
, "%s%u: %8s %d", bank_name
, offset
,
77 gpio_function
[func
], ops
->get_value(dev
, offset
));
84 static int do_gpio_status(bool all
, const char *gpio_name
)
92 if (gpio_name
&& !*gpio_name
)
94 for (ret
= uclass_first_device(UCLASS_GPIO
, &dev
);
96 ret
= uclass_next_device(&dev
)) {
97 const char *bank_name
;
100 flags
|= FLAG_SHOW_BANK
;
102 flags
|= FLAG_SHOW_ALL
;
103 bank_name
= gpio_get_bank_info(dev
, &num_bits
);
106 banklen
= bank_name
? strlen(bank_name
) : 0;
108 if (!gpio_name
|| !bank_name
||
109 !strncmp(gpio_name
, bank_name
, banklen
)) {
110 const char *p
= NULL
;
113 p
= gpio_name
+ banklen
;
114 if (gpio_name
&& *p
) {
115 offset
= simple_strtoul(p
, NULL
, 10);
116 show_gpio(dev
, bank_name
, offset
, &flags
);
118 for (offset
= 0; offset
< num_bits
; offset
++) {
119 show_gpio(dev
, bank_name
, offset
,
124 /* Add a newline between bank names */
125 if (!(flags
& FLAG_SHOW_BANK
))
126 flags
|= FLAG_SHOW_NEWLINE
;
133 static int do_gpio(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
136 enum gpio_cmd sub_cmd
;
138 const char *str_cmd
, *str_gpio
= NULL
;
140 #ifdef CONFIG_DM_GPIO
146 return CMD_RET_USAGE
;
150 #ifdef CONFIG_DM_GPIO
151 if (argc
> 0 && !strcmp(*argv
, "-a")) {
159 if (!strcmp(str_cmd
, "status")) {
160 /* Support deprecated gpio_status() */
164 #elif defined(CONFIG_DM_GPIO)
165 return cmd_process_error(cmdtp
, do_gpio_status(all
, str_gpio
));
174 /* parse the behavior */
176 case 'i': sub_cmd
= GPIO_INPUT
; break;
177 case 's': sub_cmd
= GPIO_SET
; break;
178 case 'c': sub_cmd
= GPIO_CLEAR
; break;
179 case 't': sub_cmd
= GPIO_TOGGLE
; break;
180 default: goto show_usage
;
183 #if defined(CONFIG_DM_GPIO)
185 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
186 * framework, so we look up the name here and convert it to a GPIO number.
187 * Once all GPIO drivers are converted to driver model, we can change the
188 * code here to use the GPIO uclass interface instead of the numbered
189 * GPIO compatibility layer.
191 ret
= gpio_lookup_name(str_gpio
, NULL
, NULL
, &gpio
);
193 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
== GPIO_INPUT
) {
209 gpio_direction_input(gpio
);
210 value
= gpio_get_value(gpio
);
213 case GPIO_SET
: value
= 1; break;
214 case GPIO_CLEAR
: value
= 0; break;
215 case GPIO_TOGGLE
: value
= !gpio_get_value(gpio
); break;
216 default: goto show_usage
;
218 gpio_direction_output(gpio
, value
);
220 printf("gpio: pin %s (gpio %i) value is %lu\n",
221 str_gpio
, gpio
, value
);
229 U_BOOT_CMD(gpio
, 4, 0, do_gpio
,
230 "query and control gpio pins",
231 "<input|set|clear|toggle> <pin>\n"
232 " - input/set/clear/toggle the specified pin\n"
233 "gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs");