xref: /openbmc/u-boot/drivers/gpio/da8xx_gpio.c (revision 84683638)
1 /*
2  * GPIO driver for TI DaVinci DA8xx SOCs.
3  *
4  * (C) Copyright 2011 Guralp Systems Ltd.
5  * Laurence Withers <lwithers@guralp.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22 
23 #include <common.h>
24 #include <asm/io.h>
25 #include <asm/gpio.h>
26 #include <asm/arch/hardware.h>
27 #include <asm/arch/davinci_misc.h>
28 
29 static struct gpio_registry {
30 	int is_registered;
31 	char name[GPIO_NAME_SIZE];
32 } gpio_registry[MAX_NUM_GPIOS];
33 
34 #define pinmux(x)       (&davinci_syscfg_regs->pinmux[x])
35 
36 static const struct pinmux_config gpio_pinmux[] = {
37 	{ pinmux(1), 8, 7 },	/* GP0[0] */
38 	{ pinmux(1), 8, 6 },
39 	{ pinmux(1), 8, 5 },
40 	{ pinmux(1), 8, 4 },
41 	{ pinmux(1), 8, 3 },
42 	{ pinmux(1), 8, 2 },
43 	{ pinmux(1), 8, 1 },
44 	{ pinmux(1), 8, 0 },
45 	{ pinmux(0), 8, 7 },
46 	{ pinmux(0), 8, 6 },
47 	{ pinmux(0), 8, 5 },
48 	{ pinmux(0), 8, 4 },
49 	{ pinmux(0), 8, 3 },
50 	{ pinmux(0), 8, 2 },
51 	{ pinmux(0), 8, 1 },
52 	{ pinmux(0), 8, 0 },
53 	{ pinmux(4), 8, 7 },	/* GP1[0] */
54 	{ pinmux(4), 8, 6 },
55 	{ pinmux(4), 8, 5 },
56 	{ pinmux(4), 8, 4 },
57 	{ pinmux(4), 8, 3 },
58 	{ pinmux(4), 8, 2 },
59 	{ pinmux(4), 4, 1 },
60 	{ pinmux(4), 4, 0 },
61 	{ pinmux(3), 4, 0 },
62 	{ pinmux(2), 4, 6 },
63 	{ pinmux(2), 4, 5 },
64 	{ pinmux(2), 4, 4 },
65 	{ pinmux(2), 4, 3 },
66 	{ pinmux(2), 4, 2 },
67 	{ pinmux(2), 4, 1 },
68 	{ pinmux(2), 8, 0 },
69 	{ pinmux(6), 8, 7 },	/* GP2[0] */
70 	{ pinmux(6), 8, 6 },
71 	{ pinmux(6), 8, 5 },
72 	{ pinmux(6), 8, 4 },
73 	{ pinmux(6), 8, 3 },
74 	{ pinmux(6), 8, 2 },
75 	{ pinmux(6), 8, 1 },
76 	{ pinmux(6), 8, 0 },
77 	{ pinmux(5), 8, 7 },
78 	{ pinmux(5), 8, 6 },
79 	{ pinmux(5), 8, 5 },
80 	{ pinmux(5), 8, 4 },
81 	{ pinmux(5), 8, 3 },
82 	{ pinmux(5), 8, 2 },
83 	{ pinmux(5), 8, 1 },
84 	{ pinmux(5), 8, 0 },
85 	{ pinmux(8), 8, 7 },	/* GP3[0] */
86 	{ pinmux(8), 8, 6 },
87 	{ pinmux(8), 8, 5 },
88 	{ pinmux(8), 8, 4 },
89 	{ pinmux(8), 8, 3 },
90 	{ pinmux(8), 8, 2 },
91 	{ pinmux(8), 8, 1 },
92 	{ pinmux(8), 8, 0 },
93 	{ pinmux(7), 8, 7 },
94 	{ pinmux(7), 8, 6 },
95 	{ pinmux(7), 8, 5 },
96 	{ pinmux(7), 8, 4 },
97 	{ pinmux(7), 8, 3 },
98 	{ pinmux(7), 8, 2 },
99 	{ pinmux(7), 8, 1 },
100 	{ pinmux(7), 8, 0 },
101 	{ pinmux(10), 8, 7 },	/* GP4[0] */
102 	{ pinmux(10), 8, 6 },
103 	{ pinmux(10), 8, 5 },
104 	{ pinmux(10), 8, 4 },
105 	{ pinmux(10), 8, 3 },
106 	{ pinmux(10), 8, 2 },
107 	{ pinmux(10), 8, 1 },
108 	{ pinmux(10), 8, 0 },
109 	{ pinmux(9), 8, 7 },
110 	{ pinmux(9), 8, 6 },
111 	{ pinmux(9), 8, 5 },
112 	{ pinmux(9), 8, 4 },
113 	{ pinmux(9), 8, 3 },
114 	{ pinmux(9), 8, 2 },
115 	{ pinmux(9), 8, 1 },
116 	{ pinmux(9), 8, 0 },
117 	{ pinmux(12), 8, 7 },	/* GP5[0] */
118 	{ pinmux(12), 8, 6 },
119 	{ pinmux(12), 8, 5 },
120 	{ pinmux(12), 8, 4 },
121 	{ pinmux(12), 8, 3 },
122 	{ pinmux(12), 8, 2 },
123 	{ pinmux(12), 8, 1 },
124 	{ pinmux(12), 8, 0 },
125 	{ pinmux(11), 8, 7 },
126 	{ pinmux(11), 8, 6 },
127 	{ pinmux(11), 8, 5 },
128 	{ pinmux(11), 8, 4 },
129 	{ pinmux(11), 8, 3 },
130 	{ pinmux(11), 8, 2 },
131 	{ pinmux(11), 8, 1 },
132 	{ pinmux(11), 8, 0 },
133 	{ pinmux(19), 8, 6 },	/* GP6[0] */
134 	{ pinmux(19), 8, 5 },
135 	{ pinmux(19), 8, 4 },
136 	{ pinmux(19), 8, 3 },
137 	{ pinmux(19), 8, 2 },
138 	{ pinmux(16), 8, 1 },
139 	{ pinmux(14), 8, 1 },
140 	{ pinmux(14), 8, 0 },
141 	{ pinmux(13), 8, 7 },
142 	{ pinmux(13), 8, 6 },
143 	{ pinmux(13), 8, 5 },
144 	{ pinmux(13), 8, 4 },
145 	{ pinmux(13), 8, 3 },
146 	{ pinmux(13), 8, 2 },
147 	{ pinmux(13), 8, 1 },
148 	{ pinmux(13), 8, 0 },
149 	{ pinmux(18), 8, 1 },	/* GP7[0] */
150 	{ pinmux(18), 8, 0 },
151 	{ pinmux(17), 8, 7 },
152 	{ pinmux(17), 8, 6 },
153 	{ pinmux(17), 8, 5 },
154 	{ pinmux(17), 8, 4 },
155 	{ pinmux(17), 8, 3 },
156 	{ pinmux(17), 8, 2 },
157 	{ pinmux(17), 8, 1 },
158 	{ pinmux(17), 8, 0 },
159 	{ pinmux(16), 8, 7 },
160 	{ pinmux(16), 8, 6 },
161 	{ pinmux(16), 8, 5 },
162 	{ pinmux(16), 8, 4 },
163 	{ pinmux(16), 8, 3 },
164 	{ pinmux(16), 8, 2 },
165 	{ pinmux(19), 8, 0 },	/* GP8[0] */
166 	{ pinmux(3), 4, 7 },
167 	{ pinmux(3), 4, 6 },
168 	{ pinmux(3), 4, 5 },
169 	{ pinmux(3), 4, 4 },
170 	{ pinmux(3), 4, 3 },
171 	{ pinmux(3), 4, 2 },
172 	{ pinmux(2), 4, 7 },
173 	{ pinmux(19), 8, 1 },
174 	{ pinmux(19), 8, 0 },
175 	{ pinmux(18), 8, 7 },
176 	{ pinmux(18), 8, 6 },
177 	{ pinmux(18), 8, 5 },
178 	{ pinmux(18), 8, 4 },
179 	{ pinmux(18), 8, 3 },
180 	{ pinmux(18), 8, 2 },
181 };
182 
183 int gpio_request(unsigned gpio, const char *label)
184 {
185 	if (gpio >= MAX_NUM_GPIOS)
186 		return -1;
187 
188 	if (gpio_registry[gpio].is_registered)
189 		return -1;
190 
191 	gpio_registry[gpio].is_registered = 1;
192 	strncpy(gpio_registry[gpio].name, label, GPIO_NAME_SIZE);
193 	gpio_registry[gpio].name[GPIO_NAME_SIZE - 1] = 0;
194 
195 	davinci_configure_pin_mux(&gpio_pinmux[gpio], 1);
196 
197 	return 0;
198 }
199 
200 int gpio_free(unsigned gpio)
201 {
202 	if (gpio >= MAX_NUM_GPIOS)
203 		return -1;
204 
205 	if (!gpio_registry[gpio].is_registered)
206 		return -1;
207 
208 	gpio_registry[gpio].is_registered = 0;
209 	gpio_registry[gpio].name[0] = '\0';
210 	/* Do not configure as input or change pin mux here */
211 	return 0;
212 }
213 
214 int gpio_direction_input(unsigned gpio)
215 {
216 	struct davinci_gpio *bank;
217 
218 	bank = GPIO_BANK(gpio);
219 	setbits_le32(&bank->dir, 1U << GPIO_BIT(gpio));
220 	return 0;
221 }
222 
223 int gpio_direction_output(unsigned gpio, int value)
224 {
225 	struct davinci_gpio *bank;
226 
227 	bank = GPIO_BANK(gpio);
228 	clrbits_le32(&bank->dir, 1U << GPIO_BIT(gpio));
229 	gpio_set_value(gpio, value);
230 	return 0;
231 }
232 
233 int gpio_get_value(unsigned gpio)
234 {
235 	struct davinci_gpio *bank;
236 	unsigned int ip;
237 
238 	bank = GPIO_BANK(gpio);
239 	ip = in_le32(&bank->in_data) & (1U << GPIO_BIT(gpio));
240 	return ip ? 1 : 0;
241 }
242 
243 int gpio_set_value(unsigned gpio, int value)
244 {
245 	struct davinci_gpio *bank;
246 
247 	bank = GPIO_BANK(gpio);
248 
249 	if (value)
250 		bank->set_data = 1U << GPIO_BIT(gpio);
251 	else
252 		bank->clr_data = 1U << GPIO_BIT(gpio);
253 
254 	return 0;
255 }
256 
257 void gpio_info(void)
258 {
259 	unsigned gpio, dir, val;
260 	struct davinci_gpio *bank;
261 
262 	for (gpio = 0; gpio < MAX_NUM_GPIOS; ++gpio) {
263 		bank = GPIO_BANK(gpio);
264 		dir = in_le32(&bank->dir) & (1U << GPIO_BIT(gpio));
265 		val = gpio_get_value(gpio);
266 
267 		printf("% 4d: %s: %d [%c] %s\n",
268 			gpio, dir ? " in" : "out", val,
269 			gpio_registry[gpio].is_registered ? 'x' : ' ',
270 			gpio_registry[gpio].name);
271 	}
272 }
273