2 * Control GPIO pins on the fly
4 * Copyright (c) 2008-2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
12 #include <asm/blackfin.h>
22 int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
24 if (argc == 2 && !strcmp(argv[1], "status")) {
31 return cmd_usage(cmdtp);
33 /* parse the behavior */
36 case 'i': sub_cmd = GPIO_INPUT; break;
37 case 's': sub_cmd = GPIO_SET; break;
38 case 'c': sub_cmd = GPIO_CLEAR; break;
39 case 't': sub_cmd = GPIO_TOGGLE; break;
40 default: goto show_usage;
43 /* parse the pin with format: [p][port]<#> */
44 const char *str_pin = argv[2];
46 /* grab the [p]<port> portion */
48 if (*str_pin == 'p') ++str_pin;
51 case 'a': port_base = GPIO_PA0; break;
54 case 'b': port_base = GPIO_PB0; break;
57 case 'c': port_base = GPIO_PC0; break;
60 case 'd': port_base = GPIO_PD0; break;
63 case 'e': port_base = GPIO_PE0; break;
66 case 'f': port_base = GPIO_PF0; break;
69 case 'g': port_base = GPIO_PG0; break;
72 case 'h': port_base = GPIO_PH0; break;
75 case 'i': port_base = GPIO_PI0; break;
78 case 'j': port_base = GPIO_PJ0; break;
80 default: goto show_usage;
83 /* grab the <#> portion */
84 ulong pin = simple_strtoul(str_pin + 1, NULL, 10);
88 /* grab the pin before we tweak it */
89 ulong gpio = port_base + pin;
90 gpio_request(gpio, "cmd_gpio");
92 /* finally, let's do it: set direction and exec command */
94 if (sub_cmd == GPIO_INPUT) {
95 gpio_direction_input(gpio);
96 value = gpio_get_value(gpio);
99 case GPIO_SET: value = 1; break;
100 case GPIO_CLEAR: value = 0; break;
101 case GPIO_TOGGLE: value = !gpio_get_value(gpio); break;
102 default: goto show_usage;
104 gpio_direction_output(gpio, value);
106 printf("gpio: pin %lu on port %c (gpio %lu) value is %lu\n",
107 pin, *str_pin, gpio, value);
114 U_BOOT_CMD(gpio, 3, 0, do_gpio,
115 "input/set/clear/toggle gpio output pins",
116 "<input|set|clear|toggle> <port><pin>\n"
117 " - input/set/clear/toggle the specified pin (e.g. PF10)");