]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_gpio.c
arm: mx5: clock: add support for changing CPU clock via cmdline
[karo-tx-uboot.git] / common / cmd_gpio.c
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_RET_USAGE;
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         if (gpio_request(gpio, "cmd_gpio")) {
61                 printf("gpio: requesting pin %u failed\n", gpio);
62                 return -1;
63         }
64
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);
69         } else {
70                 switch (sub_cmd) {
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;
75                 }
76                 gpio_direction_output(gpio, value);
77         }
78         printf("gpio: pin %s (gpio %i) value is %lu\n",
79                 str_gpio, gpio, value);
80
81         gpio_free(gpio);
82
83         return value;
84 }
85
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");