blob: 76648d27d426bc9e644378312e010d10629e329d [file] [log] [blame]
Laurence Withers3ffd9682011-07-18 09:26:01 -04001/*
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>
Laurence Withers3ffd9682011-07-18 09:26:01 -040026#include <asm/arch/hardware.h>
27#include <asm/arch/davinci_misc.h>
28
Laurence Withers3ffd9682011-07-18 09:26:01 -040029static struct gpio_registry {
30 int is_registered;
31 char name[GPIO_NAME_SIZE];
32} gpio_registry[MAX_NUM_GPIOS];
33
Holger Hans Peter Freyther1356f8e2013-02-07 23:41:01 +000034#if defined(CONFIG_SOC_DA8XX)
Laurence Withers3ffd9682011-07-18 09:26:01 -040035#define pinmux(x) (&davinci_syscfg_regs->pinmux[x])
36
Tomas Novotny59032872013-02-01 06:46:00 +000037#if defined(CONFIG_SOC_DA8XX) && !defined(CONFIG_SOC_DA850)
38static const struct pinmux_config gpio_pinmux[] = {
39 { pinmux(13), 8, 6 }, /* GP0[0] */
40 { pinmux(13), 8, 7 },
41 { pinmux(14), 8, 0 },
42 { pinmux(14), 8, 1 },
43 { pinmux(14), 8, 2 },
44 { pinmux(14), 8, 3 },
45 { pinmux(14), 8, 4 },
46 { pinmux(14), 8, 5 },
47 { pinmux(14), 8, 6 },
48 { pinmux(14), 8, 7 },
49 { pinmux(15), 8, 0 },
50 { pinmux(15), 8, 1 },
51 { pinmux(15), 8, 2 },
52 { pinmux(15), 8, 3 },
53 { pinmux(15), 8, 4 },
54 { pinmux(15), 8, 5 },
55 { pinmux(15), 8, 6 }, /* GP1[0] */
56 { pinmux(15), 8, 7 },
57 { pinmux(16), 8, 0 },
58 { pinmux(16), 8, 1 },
59 { pinmux(16), 8, 2 },
60 { pinmux(16), 8, 3 },
61 { pinmux(16), 8, 4 },
62 { pinmux(16), 8, 5 },
63 { pinmux(16), 8, 6 },
64 { pinmux(16), 8, 7 },
65 { pinmux(17), 8, 0 },
66 { pinmux(17), 8, 1 },
67 { pinmux(17), 8, 2 },
68 { pinmux(17), 8, 3 },
69 { pinmux(17), 8, 4 },
70 { pinmux(17), 8, 5 },
71 { pinmux(17), 8, 6 }, /* GP2[0] */
72 { pinmux(17), 8, 7 },
73 { pinmux(18), 8, 0 },
74 { pinmux(18), 8, 1 },
75 { pinmux(18), 8, 2 },
76 { pinmux(18), 8, 3 },
77 { pinmux(18), 8, 4 },
78 { pinmux(18), 8, 5 },
79 { pinmux(18), 8, 6 },
80 { pinmux(18), 8, 7 },
81 { pinmux(19), 8, 0 },
82 { pinmux(9), 8, 2 },
83 { pinmux(9), 8, 3 },
84 { pinmux(9), 8, 4 },
85 { pinmux(9), 8, 5 },
86 { pinmux(9), 8, 6 },
87 { pinmux(10), 8, 1 }, /* GP3[0] */
88 { pinmux(10), 8, 2 },
89 { pinmux(10), 8, 3 },
90 { pinmux(10), 8, 4 },
91 { pinmux(10), 8, 5 },
92 { pinmux(10), 8, 6 },
93 { pinmux(10), 8, 7 },
94 { pinmux(11), 8, 0 },
95 { pinmux(11), 8, 1 },
96 { pinmux(11), 8, 2 },
97 { pinmux(11), 8, 3 },
98 { pinmux(11), 8, 4 },
99 { pinmux(9), 8, 7 },
100 { pinmux(2), 8, 6 },
101 { pinmux(11), 8, 5 },
102 { pinmux(11), 8, 6 },
103 { pinmux(12), 8, 4 }, /* GP4[0] */
104 { pinmux(12), 8, 5 },
105 { pinmux(12), 8, 6 },
106 { pinmux(12), 8, 7 },
107 { pinmux(13), 8, 0 },
108 { pinmux(13), 8, 1 },
109 { pinmux(13), 8, 2 },
110 { pinmux(13), 8, 3 },
111 { pinmux(13), 8, 4 },
112 { pinmux(13), 8, 5 },
113 { pinmux(11), 8, 7 },
114 { pinmux(12), 8, 0 },
115 { pinmux(12), 8, 1 },
116 { pinmux(12), 8, 2 },
117 { pinmux(12), 8, 3 },
118 { pinmux(9), 8, 1 },
119 { pinmux(7), 8, 3 }, /* GP5[0] */
120 { pinmux(7), 8, 4 },
121 { pinmux(7), 8, 5 },
122 { pinmux(7), 8, 6 },
123 { pinmux(7), 8, 7 },
124 { pinmux(8), 8, 0 },
125 { pinmux(8), 8, 1 },
126 { pinmux(8), 8, 2 },
127 { pinmux(8), 8, 3 },
128 { pinmux(8), 8, 4 },
129 { pinmux(8), 8, 5 },
130 { pinmux(8), 8, 6 },
131 { pinmux(8), 8, 7 },
132 { pinmux(9), 8, 0 },
133 { pinmux(7), 8, 1 },
134 { pinmux(7), 8, 2 },
135 { pinmux(5), 8, 1 }, /* GP6[0] */
136 { pinmux(5), 8, 2 },
137 { pinmux(5), 8, 3 },
138 { pinmux(5), 8, 4 },
139 { pinmux(5), 8, 5 },
140 { pinmux(5), 8, 6 },
141 { pinmux(5), 8, 7 },
142 { pinmux(6), 8, 0 },
143 { pinmux(6), 8, 1 },
144 { pinmux(6), 8, 2 },
145 { pinmux(6), 8, 3 },
146 { pinmux(6), 8, 4 },
147 { pinmux(6), 8, 5 },
148 { pinmux(6), 8, 6 },
149 { pinmux(6), 8, 7 },
150 { pinmux(7), 8, 0 },
151 { pinmux(1), 8, 0 }, /* GP7[0] */
152 { pinmux(1), 8, 1 },
153 { pinmux(1), 8, 2 },
154 { pinmux(1), 8, 3 },
155 { pinmux(1), 8, 4 },
156 { pinmux(1), 8, 5 },
157 { pinmux(1), 8, 6 },
158 { pinmux(1), 8, 7 },
159 { pinmux(2), 8, 0 },
160 { pinmux(2), 8, 1 },
161 { pinmux(2), 8, 2 },
162 { pinmux(2), 8, 3 },
163 { pinmux(2), 8, 4 },
164 { pinmux(2), 8, 5 },
165 { pinmux(0), 1, 0 },
166 { pinmux(0), 1, 1 },
167};
Tom Rini9dbc8362013-03-11 12:02:40 -0400168#else /* CONFIG_SOC_DA8XX && CONFIG_SOC_DA850 */
Laurence Withers3ffd9682011-07-18 09:26:01 -0400169static const struct pinmux_config gpio_pinmux[] = {
170 { pinmux(1), 8, 7 }, /* GP0[0] */
171 { pinmux(1), 8, 6 },
172 { pinmux(1), 8, 5 },
173 { pinmux(1), 8, 4 },
174 { pinmux(1), 8, 3 },
175 { pinmux(1), 8, 2 },
176 { pinmux(1), 8, 1 },
177 { pinmux(1), 8, 0 },
178 { pinmux(0), 8, 7 },
179 { pinmux(0), 8, 6 },
180 { pinmux(0), 8, 5 },
181 { pinmux(0), 8, 4 },
182 { pinmux(0), 8, 3 },
183 { pinmux(0), 8, 2 },
184 { pinmux(0), 8, 1 },
185 { pinmux(0), 8, 0 },
186 { pinmux(4), 8, 7 }, /* GP1[0] */
187 { pinmux(4), 8, 6 },
188 { pinmux(4), 8, 5 },
189 { pinmux(4), 8, 4 },
190 { pinmux(4), 8, 3 },
191 { pinmux(4), 8, 2 },
192 { pinmux(4), 4, 1 },
193 { pinmux(4), 4, 0 },
194 { pinmux(3), 4, 0 },
195 { pinmux(2), 4, 6 },
196 { pinmux(2), 4, 5 },
197 { pinmux(2), 4, 4 },
198 { pinmux(2), 4, 3 },
199 { pinmux(2), 4, 2 },
200 { pinmux(2), 4, 1 },
201 { pinmux(2), 8, 0 },
202 { pinmux(6), 8, 7 }, /* GP2[0] */
203 { pinmux(6), 8, 6 },
204 { pinmux(6), 8, 5 },
205 { pinmux(6), 8, 4 },
206 { pinmux(6), 8, 3 },
207 { pinmux(6), 8, 2 },
208 { pinmux(6), 8, 1 },
209 { pinmux(6), 8, 0 },
210 { pinmux(5), 8, 7 },
211 { pinmux(5), 8, 6 },
212 { pinmux(5), 8, 5 },
213 { pinmux(5), 8, 4 },
214 { pinmux(5), 8, 3 },
215 { pinmux(5), 8, 2 },
216 { pinmux(5), 8, 1 },
217 { pinmux(5), 8, 0 },
218 { pinmux(8), 8, 7 }, /* GP3[0] */
219 { pinmux(8), 8, 6 },
220 { pinmux(8), 8, 5 },
221 { pinmux(8), 8, 4 },
222 { pinmux(8), 8, 3 },
223 { pinmux(8), 8, 2 },
224 { pinmux(8), 8, 1 },
225 { pinmux(8), 8, 0 },
226 { pinmux(7), 8, 7 },
227 { pinmux(7), 8, 6 },
228 { pinmux(7), 8, 5 },
229 { pinmux(7), 8, 4 },
230 { pinmux(7), 8, 3 },
231 { pinmux(7), 8, 2 },
232 { pinmux(7), 8, 1 },
233 { pinmux(7), 8, 0 },
234 { pinmux(10), 8, 7 }, /* GP4[0] */
235 { pinmux(10), 8, 6 },
236 { pinmux(10), 8, 5 },
237 { pinmux(10), 8, 4 },
238 { pinmux(10), 8, 3 },
239 { pinmux(10), 8, 2 },
240 { pinmux(10), 8, 1 },
241 { pinmux(10), 8, 0 },
242 { pinmux(9), 8, 7 },
243 { pinmux(9), 8, 6 },
244 { pinmux(9), 8, 5 },
245 { pinmux(9), 8, 4 },
246 { pinmux(9), 8, 3 },
247 { pinmux(9), 8, 2 },
248 { pinmux(9), 8, 1 },
249 { pinmux(9), 8, 0 },
250 { pinmux(12), 8, 7 }, /* GP5[0] */
251 { pinmux(12), 8, 6 },
252 { pinmux(12), 8, 5 },
253 { pinmux(12), 8, 4 },
254 { pinmux(12), 8, 3 },
255 { pinmux(12), 8, 2 },
256 { pinmux(12), 8, 1 },
257 { pinmux(12), 8, 0 },
258 { pinmux(11), 8, 7 },
259 { pinmux(11), 8, 6 },
260 { pinmux(11), 8, 5 },
261 { pinmux(11), 8, 4 },
262 { pinmux(11), 8, 3 },
263 { pinmux(11), 8, 2 },
264 { pinmux(11), 8, 1 },
265 { pinmux(11), 8, 0 },
266 { pinmux(19), 8, 6 }, /* GP6[0] */
267 { pinmux(19), 8, 5 },
268 { pinmux(19), 8, 4 },
269 { pinmux(19), 8, 3 },
270 { pinmux(19), 8, 2 },
271 { pinmux(16), 8, 1 },
272 { pinmux(14), 8, 1 },
273 { pinmux(14), 8, 0 },
274 { pinmux(13), 8, 7 },
275 { pinmux(13), 8, 6 },
276 { pinmux(13), 8, 5 },
277 { pinmux(13), 8, 4 },
278 { pinmux(13), 8, 3 },
279 { pinmux(13), 8, 2 },
280 { pinmux(13), 8, 1 },
281 { pinmux(13), 8, 0 },
282 { pinmux(18), 8, 1 }, /* GP7[0] */
283 { pinmux(18), 8, 0 },
284 { pinmux(17), 8, 7 },
285 { pinmux(17), 8, 6 },
286 { pinmux(17), 8, 5 },
287 { pinmux(17), 8, 4 },
288 { pinmux(17), 8, 3 },
289 { pinmux(17), 8, 2 },
290 { pinmux(17), 8, 1 },
291 { pinmux(17), 8, 0 },
292 { pinmux(16), 8, 7 },
293 { pinmux(16), 8, 6 },
294 { pinmux(16), 8, 5 },
295 { pinmux(16), 8, 4 },
296 { pinmux(16), 8, 3 },
297 { pinmux(16), 8, 2 },
298 { pinmux(19), 8, 0 }, /* GP8[0] */
299 { pinmux(3), 4, 7 },
300 { pinmux(3), 4, 6 },
301 { pinmux(3), 4, 5 },
302 { pinmux(3), 4, 4 },
303 { pinmux(3), 4, 3 },
304 { pinmux(3), 4, 2 },
305 { pinmux(2), 4, 7 },
306 { pinmux(19), 8, 1 },
307 { pinmux(19), 8, 0 },
308 { pinmux(18), 8, 7 },
309 { pinmux(18), 8, 6 },
310 { pinmux(18), 8, 5 },
311 { pinmux(18), 8, 4 },
312 { pinmux(18), 8, 3 },
313 { pinmux(18), 8, 2 },
314};
Tom Rini9dbc8362013-03-11 12:02:40 -0400315#endif /* CONFIG_SOC_DA8XX && !CONFIG_SOC_DA850 */
316#else /* !CONFIG_SOC_DA8XX */
Holger Hans Peter Freyther1356f8e2013-02-07 23:41:01 +0000317#define davinci_configure_pin_mux(a, b)
Tom Rini9dbc8362013-03-11 12:02:40 -0400318#endif /* CONFIG_SOC_DA8XX */
Laurence Withers3ffd9682011-07-18 09:26:01 -0400319
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600320int gpio_request(unsigned gpio, const char *label)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400321{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600322 if (gpio >= MAX_NUM_GPIOS)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400323 return -1;
324
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600325 if (gpio_registry[gpio].is_registered)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400326 return -1;
327
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600328 gpio_registry[gpio].is_registered = 1;
329 strncpy(gpio_registry[gpio].name, label, GPIO_NAME_SIZE);
330 gpio_registry[gpio].name[GPIO_NAME_SIZE - 1] = 0;
Laurence Withers3ffd9682011-07-18 09:26:01 -0400331
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600332 davinci_configure_pin_mux(&gpio_pinmux[gpio], 1);
Laurence Withers3ffd9682011-07-18 09:26:01 -0400333
334 return 0;
335}
336
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600337int gpio_free(unsigned gpio)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400338{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600339 if (gpio >= MAX_NUM_GPIOS)
340 return -1;
Laurence Withers3ffd9682011-07-18 09:26:01 -0400341
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600342 if (!gpio_registry[gpio].is_registered)
343 return -1;
344
345 gpio_registry[gpio].is_registered = 0;
346 gpio_registry[gpio].name[0] = '\0';
347 /* Do not configure as input or change pin mux here */
348 return 0;
Laurence Withers3ffd9682011-07-18 09:26:01 -0400349}
350
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600351int gpio_direction_input(unsigned gpio)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400352{
353 struct davinci_gpio *bank;
354
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600355 bank = GPIO_BANK(gpio);
356 setbits_le32(&bank->dir, 1U << GPIO_BIT(gpio));
Laurence Withers3ffd9682011-07-18 09:26:01 -0400357 return 0;
358}
359
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600360int gpio_direction_output(unsigned gpio, int value)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400361{
362 struct davinci_gpio *bank;
363
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600364 bank = GPIO_BANK(gpio);
365 clrbits_le32(&bank->dir, 1U << GPIO_BIT(gpio));
366 gpio_set_value(gpio, value);
Laurence Withers3ffd9682011-07-18 09:26:01 -0400367 return 0;
368}
369
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600370int gpio_get_value(unsigned gpio)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400371{
372 struct davinci_gpio *bank;
373 unsigned int ip;
374
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600375 bank = GPIO_BANK(gpio);
376 ip = in_le32(&bank->in_data) & (1U << GPIO_BIT(gpio));
Laurence Withers3ffd9682011-07-18 09:26:01 -0400377 return ip ? 1 : 0;
378}
379
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600380int gpio_set_value(unsigned gpio, int value)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400381{
382 struct davinci_gpio *bank;
383
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600384 bank = GPIO_BANK(gpio);
Laurence Withers3ffd9682011-07-18 09:26:01 -0400385
386 if (value)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600387 bank->set_data = 1U << GPIO_BIT(gpio);
Laurence Withers3ffd9682011-07-18 09:26:01 -0400388 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600389 bank->clr_data = 1U << GPIO_BIT(gpio);
390
391 return 0;
Laurence Withers3ffd9682011-07-18 09:26:01 -0400392}
393
Laurence Withers3ffd9682011-07-18 09:26:01 -0400394void gpio_info(void)
395{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600396 unsigned gpio, dir, val;
Laurence Withers3ffd9682011-07-18 09:26:01 -0400397 struct davinci_gpio *bank;
398
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600399 for (gpio = 0; gpio < MAX_NUM_GPIOS; ++gpio) {
400 bank = GPIO_BANK(gpio);
401 dir = in_le32(&bank->dir) & (1U << GPIO_BIT(gpio));
402 val = gpio_get_value(gpio);
Laurence Withers3ffd9682011-07-18 09:26:01 -0400403
404 printf("% 4d: %s: %d [%c] %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600405 gpio, dir ? " in" : "out", val,
406 gpio_registry[gpio].is_registered ? 'x' : ' ',
407 gpio_registry[gpio].name);
Laurence Withers3ffd9682011-07-18 09:26:01 -0400408 }
409}