]>
Commit | Line | Data |
---|---|---|
a972b8d7 MF |
1 | /* |
2 | * Control GPIO pins on the fly | |
3 | * | |
4 | * Copyright (c) 2008-2011 Analog Devices Inc. | |
5 | * | |
6 | * Licensed under the GPL-2 or later. | |
7 | */ | |
8 | ||
9 | #include <common.h> | |
10 | #include <command.h> | |
11 | ||
12 | #include <asm/gpio.h> | |
13 | ||
14 | #ifndef name_to_gpio | |
15 | #define name_to_gpio(name) simple_strtoul(name, NULL, 10) | |
16 | #endif | |
17 | ||
18 | enum gpio_cmd { | |
19 | GPIO_INPUT, | |
20 | GPIO_SET, | |
21 | GPIO_CLEAR, | |
22 | GPIO_TOGGLE, | |
23 | }; | |
24 | ||
25 | static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
26 | { | |
27 | int gpio; | |
28 | enum gpio_cmd sub_cmd; | |
29 | ulong value; | |
30 | const char *str_cmd, *str_gpio; | |
31 | ||
32 | #ifdef gpio_status | |
33 | if (argc == 2 && !strcmp(argv[1], "status")) { | |
34 | gpio_status(); | |
35 | return 0; | |
36 | } | |
37 | #endif | |
38 | ||
39 | if (argc != 3) | |
40 | show_usage: | |
41 | return cmd_usage(cmdtp); | |
42 | str_cmd = argv[1]; | |
43 | str_gpio = argv[2]; | |
44 | ||
45 | /* parse the behavior */ | |
46 | switch (*str_cmd) { | |
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; | |
52 | } | |
53 | ||
54 | /* turn the gpio name into a gpio number */ | |
55 | gpio = name_to_gpio(str_gpio); | |
56 | if (gpio < 0) | |
57 | goto show_usage; | |
58 | ||
59 | /* grab the pin before we tweak it */ | |
60 | gpio_request(gpio, "cmd_gpio"); | |
61 | ||
62 | /* finally, let's do it: set direction and exec command */ | |
63 | if (sub_cmd == GPIO_INPUT) { | |
64 | gpio_direction_input(gpio); | |
65 | value = gpio_get_value(gpio); | |
66 | } else { | |
67 | switch (sub_cmd) { | |
68 | case GPIO_SET: value = 1; break; | |
69 | case GPIO_CLEAR: value = 0; break; | |
70 | case GPIO_TOGGLE: value = !gpio_get_value(gpio); break; | |
71 | default: goto show_usage; | |
72 | } | |
73 | gpio_direction_output(gpio, value); | |
74 | } | |
75 | printf("gpio: pin %s (gpio %i) value is %lu\n", | |
76 | str_gpio, gpio, value); | |
77 | ||
78 | gpio_free(gpio); | |
79 | ||
80 | return value; | |
81 | } | |
82 | ||
83 | U_BOOT_CMD(gpio, 3, 0, do_gpio, | |
84 | "input/set/clear/toggle gpio pins", | |
85 | "<input|set|clear|toggle> <pin>\n" | |
86 | " - input/set/clear/toggle the specified pin"); |