]>
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 #define name_to_gpio(name) simple_strtoul(name, NULL, 10)
25 static int do_gpio(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
28 enum gpio_cmd sub_cmd
;
30 const char *str_cmd
, *str_gpio
;
33 if (argc
== 2 && !strcmp(argv
[1], "status")) {
41 return cmd_usage(cmdtp
);
45 /* parse the behavior */
47 case 'i': sub_cmd
= GPIO_INPUT
; break;
48 case 's': sub_cmd
= GPIO_SET
; break;
49 case 'c': sub_cmd
= GPIO_CLEAR
; break;
50 case 't': sub_cmd
= GPIO_TOGGLE
; break;
51 default: goto show_usage
;
54 /* turn the gpio name into a gpio number */
55 gpio
= name_to_gpio(str_gpio
);
59 /* grab the pin before we tweak it */
60 if (gpio_request(gpio
, "cmd_gpio")) {
61 printf("gpio: requesting pin %u failed\n", gpio
);
65 /* finally, let's do it: set direction and exec command */
66 if (sub_cmd
== GPIO_INPUT
) {
67 gpio_direction_input(gpio
);
68 value
= gpio_get_value(gpio
);
71 case GPIO_SET
: value
= 1; break;
72 case GPIO_CLEAR
: value
= 0; break;
73 case GPIO_TOGGLE
: value
= !gpio_get_value(gpio
); break;
74 default: goto show_usage
;
76 gpio_direction_output(gpio
, value
);
78 printf("gpio: pin %s (gpio %i) value is %lu\n",
79 str_gpio
, gpio
, value
);
86 U_BOOT_CMD(gpio
, 3, 0, do_gpio
,
87 "input/set/clear/toggle gpio pins",
88 "<input|set|clear|toggle> <pin>\n"
89 " - input/set/clear/toggle the specified pin");