xref: /rk3399_rockchip-uboot/cmd/gpio.c (revision a4719b90cc2f09e5348b830d61f32ab6d991069a)
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 #include <errno.h>
12 #include <dm.h>
13 #include <asm/gpio.h>
14 
15 __weak int name_to_gpio(const char *name)
16 {
17 	return simple_strtoul(name, NULL, 10);
18 }
19 
20 enum gpio_cmd {
21 	GPIO_INPUT,
22 	GPIO_SET,
23 	GPIO_CLEAR,
24 	GPIO_TOGGLE,
25 };
26 
27 #if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
28 
29 /* A few flags used by show_gpio() */
30 enum {
31 	FLAG_SHOW_ALL		= 1 << 0,
32 	FLAG_SHOW_BANK		= 1 << 1,
33 	FLAG_SHOW_NEWLINE	= 1 << 2,
34 };
35 
36 static void gpio_get_description(struct udevice *dev, const char *bank_name,
37 				 int offset, int *flagsp)
38 {
39 	char buf[80];
40 	int ret;
41 
42 	ret = gpio_get_function(dev, offset, NULL);
43 	if (ret < 0)
44 		goto err;
45 	if (!(*flagsp & FLAG_SHOW_ALL) && ret == GPIOF_UNUSED)
46 		return;
47 	if ((*flagsp & FLAG_SHOW_BANK) && bank_name) {
48 		if (*flagsp & FLAG_SHOW_NEWLINE) {
49 			putc('\n');
50 			*flagsp &= ~FLAG_SHOW_NEWLINE;
51 		}
52 		printf("Bank %s:\n", bank_name);
53 		*flagsp &= ~FLAG_SHOW_BANK;
54 	}
55 
56 	ret = gpio_get_status(dev, offset, buf, sizeof(buf));
57 	if (ret)
58 		goto err;
59 
60 	printf("%s\n", buf);
61 	return;
62 err:
63 	printf("Error %d\n", ret);
64 }
65 
66 static int do_gpio_status(bool all, const char *gpio_name)
67 {
68 	struct udevice *dev;
69 	int banklen;
70 	int flags;
71 
72 	flags = 0;
73 	if (gpio_name && !*gpio_name)
74 		gpio_name = NULL;
75 	for (uclass_first_device(UCLASS_GPIO, &dev);
76 	     dev;
77 	     uclass_next_device(&dev)) {
78 		const char *bank_name;
79 		int num_bits;
80 
81 		flags |= FLAG_SHOW_BANK;
82 		if (all)
83 			flags |= FLAG_SHOW_ALL;
84 		bank_name = gpio_get_bank_info(dev, &num_bits);
85 		if (!num_bits) {
86 			debug("GPIO device %s has no bits\n", dev->name);
87 			continue;
88 		}
89 		banklen = bank_name ? strlen(bank_name) : 0;
90 
91 		if (!gpio_name || !bank_name ||
92 		    !strncmp(gpio_name, bank_name, banklen)) {
93 			const char *p = NULL;
94 			int offset;
95 
96 			p = gpio_name + banklen;
97 			if (gpio_name && *p) {
98 				offset = simple_strtoul(p, NULL, 10);
99 				gpio_get_description(dev, bank_name, offset,
100 						     &flags);
101 			} else {
102 				for (offset = 0; offset < num_bits; offset++) {
103 					gpio_get_description(dev, bank_name,
104 							     offset, &flags);
105 				}
106 			}
107 		}
108 		/* Add a newline between bank names */
109 		if (!(flags & FLAG_SHOW_BANK))
110 			flags |= FLAG_SHOW_NEWLINE;
111 	}
112 
113 	return 0;
114 }
115 #endif
116 
117 static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
118 {
119 	unsigned int gpio;
120 	enum gpio_cmd sub_cmd;
121 	int value;
122 	const char *str_cmd, *str_gpio = NULL;
123 	int ret;
124 #ifdef CONFIG_DM_GPIO
125 	bool all = false;
126 #endif
127 
128 	if (argc < 2)
129  show_usage:
130 		return CMD_RET_USAGE;
131 	str_cmd = argv[1];
132 	argc -= 2;
133 	argv += 2;
134 #ifdef CONFIG_DM_GPIO
135 	if (argc > 0 && !strcmp(*argv, "-a")) {
136 		all = true;
137 		argc--;
138 		argv++;
139 	}
140 #endif
141 	if (argc > 0)
142 		str_gpio = *argv;
143 	if (!strncmp(str_cmd, "status", 2)) {
144 		/* Support deprecated gpio_status() */
145 #ifdef gpio_status
146 		gpio_status();
147 		return 0;
148 #elif defined(CONFIG_DM_GPIO)
149 		return cmd_process_error(cmdtp, do_gpio_status(all, str_gpio));
150 #else
151 		goto show_usage;
152 #endif
153 	}
154 
155 	if (!str_gpio)
156 		goto show_usage;
157 
158 	/* parse the behavior */
159 	switch (*str_cmd) {
160 		case 'i': sub_cmd = GPIO_INPUT;  break;
161 		case 's': sub_cmd = GPIO_SET;    break;
162 		case 'c': sub_cmd = GPIO_CLEAR;  break;
163 		case 't': sub_cmd = GPIO_TOGGLE; break;
164 		default:  goto show_usage;
165 	}
166 
167 #if defined(CONFIG_DM_GPIO)
168 	/*
169 	 * TODO(sjg@chromium.org): For now we must fit into the existing GPIO
170 	 * framework, so we look up the name here and convert it to a GPIO number.
171 	 * Once all GPIO drivers are converted to driver model, we can change the
172 	 * code here to use the GPIO uclass interface instead of the numbered
173 	 * GPIO compatibility layer.
174 	 */
175 	ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
176 	if (ret) {
177 		printf("GPIO: '%s' not found\n", str_gpio);
178 		return cmd_process_error(cmdtp, ret);
179 	}
180 #else
181 	/* turn the gpio name into a gpio number */
182 	gpio = name_to_gpio(str_gpio);
183 	if (gpio < 0)
184 		goto show_usage;
185 #endif
186 	/* grab the pin before we tweak it */
187 	ret = gpio_request(gpio, "cmd_gpio");
188 	if (ret && ret != -EBUSY) {
189 		printf("gpio: requesting pin %u failed\n", gpio);
190 		return -1;
191 	}
192 
193 	/* finally, let's do it: set direction and exec command */
194 	if (sub_cmd == GPIO_INPUT) {
195 		gpio_direction_input(gpio);
196 		value = gpio_get_value(gpio);
197 	} else {
198 		switch (sub_cmd) {
199 		case GPIO_SET:
200 			value = 1;
201 			break;
202 		case GPIO_CLEAR:
203 			value = 0;
204 			break;
205 		case GPIO_TOGGLE:
206 			value = gpio_get_value(gpio);
207 			if (!IS_ERR_VALUE(value))
208 				value = !value;
209 			break;
210 		default:
211 			goto show_usage;
212 		}
213 		gpio_direction_output(gpio, value);
214 	}
215 	printf("gpio: pin %s (gpio %i) value is ", str_gpio, gpio);
216 	if (IS_ERR_VALUE(value))
217 		printf("unknown (ret=%d)\n", value);
218 	else
219 		printf("%d\n", value);
220 	if (sub_cmd != GPIO_INPUT && !IS_ERR_VALUE(value)) {
221 		int nval = gpio_get_value(gpio);
222 
223 		if (IS_ERR_VALUE(nval))
224 			printf("   Warning: no access to GPIO output value\n");
225 		else if (nval != value)
226 			printf("   Warning: value of pin is still %d\n", nval);
227 	}
228 
229 	if (ret != -EBUSY)
230 		gpio_free(gpio);
231 
232 	return value;
233 }
234 
235 U_BOOT_CMD(gpio, 4, 0, do_gpio,
236 	   "query and control gpio pins",
237 	   "<input|set|clear|toggle> <pin>\n"
238 	   "    - input/set/clear/toggle the specified pin\n"
239 	   "gpio status [-a] [<bank> | <pin>]  - show [all/claimed] GPIOs");
240