2 * Control GPIO pins on the fly
4 * Copyright (c) 2008-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
14 int __weak name_to_gpio(const char *name)
16 return simple_strtoul(name, NULL, 10);
26 #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
27 static const char * const gpio_function[] = {
33 static void show_gpio(struct udevice *dev, const char *bank_name, int offset)
35 struct dm_gpio_ops *ops = gpio_get_ops(dev);
41 ret = ops->get_state(dev, offset, buf, sizeof(buf));
47 int func = GPIOF_UNKNOWN;
50 if (ops->get_function) {
51 ret = ops->get_function(dev, offset);
52 if (ret >= 0 && ret < ARRAY_SIZE(gpio_function))
55 sprintf(buf, "%s%u: %8s %d", bank_name, offset,
56 gpio_function[func], ops->get_value(dev, offset));
63 static int do_gpio_status(const char *gpio_name)
69 if (gpio_name && !*gpio_name)
71 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
73 ret = uclass_next_device(&dev)) {
74 const char *bank_name;
77 bank_name = gpio_get_bank_info(dev, &num_bits);
79 if (!gpio_name || !bank_name ||
80 !strncmp(gpio_name, bank_name, strlen(bank_name))) {
87 printf("Bank %s:\n", bank_name);
90 if (gpio_name && bank_name) {
91 p = gpio_name + strlen(bank_name);
92 offset = simple_strtoul(p, NULL, 10);
93 show_gpio(dev, bank_name, offset);
95 for (offset = 0; offset < num_bits; offset++)
96 show_gpio(dev, bank_name, offset);
105 static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
108 enum gpio_cmd sub_cmd;
110 const char *str_cmd, *str_gpio = NULL;
111 #ifdef CONFIG_DM_GPIO
117 return CMD_RET_USAGE;
121 if (!strcmp(str_cmd, "status")) {
122 /* Support deprecated gpio_status() */
126 #elif defined(CONFIG_DM_GPIO)
127 return cmd_process_error(cmdtp, do_gpio_status(str_gpio));
136 /* parse the behavior */
138 case 'i': sub_cmd = GPIO_INPUT; break;
139 case 's': sub_cmd = GPIO_SET; break;
140 case 'c': sub_cmd = GPIO_CLEAR; break;
141 case 't': sub_cmd = GPIO_TOGGLE; break;
142 default: goto show_usage;
145 #if defined(CONFIG_DM_GPIO)
147 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
148 * framework, so we look up the name here and convert it to a GPIO number.
149 * Once all GPIO drivers are converted to driver model, we can change the
150 * code here to use the GPIO uclass interface instead of the numbered
151 * GPIO compatibility layer.
153 ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
155 return cmd_process_error(cmdtp, ret);
157 /* turn the gpio name into a gpio number */
158 gpio = name_to_gpio(str_gpio);
162 /* grab the pin before we tweak it */
163 if (gpio_request(gpio, "cmd_gpio")) {
164 printf("gpio: requesting pin %u failed\n", gpio);
168 /* finally, let's do it: set direction and exec command */
169 if (sub_cmd == GPIO_INPUT) {
170 gpio_direction_input(gpio);
171 value = gpio_get_value(gpio);
174 case GPIO_SET: value = 1; break;
175 case GPIO_CLEAR: value = 0; break;
176 case GPIO_TOGGLE: value = !gpio_get_value(gpio); break;
177 default: goto show_usage;
179 gpio_direction_output(gpio, value);
181 printf("gpio: pin %s (gpio %i) value is %lu\n",
182 str_gpio, gpio, value);
189 U_BOOT_CMD(gpio, 3, 0, do_gpio,
190 "query and control gpio pins",
191 "<input|set|clear|toggle> <pin>\n"
192 " - input/set/clear/toggle the specified pin\n"
193 "gpio status [<bank> | <pin>]");