blob: bd7944816487d13d06d88ff14c9ad27209d156f3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Laurence Withers3ffd9682011-07-18 09:26:01 -04002/*
3 * GPIO driver for TI DaVinci DA8xx SOCs.
4 *
5 * (C) Copyright 2011 Guralp Systems Ltd.
6 * Laurence Withers <lwithers@guralp.com>
Laurence Withers3ffd9682011-07-18 09:26:01 -04007 */
8
9#include <common.h>
Adam Ford9c286a72018-06-10 22:25:57 -050010#include <dm.h>
11#include <fdtdec.h>
Laurence Withers3ffd9682011-07-18 09:26:01 -040012#include <asm/io.h>
13#include <asm/gpio.h>
Adam Forda1129392018-08-16 23:13:34 -050014#include <dt-bindings/gpio/gpio.h>
Laurence Withers3ffd9682011-07-18 09:26:01 -040015
Keerthy6d9e7a82018-10-03 17:55:14 +053016#include "da8xx_gpio.h"
17
Adam Ford9c286a72018-06-10 22:25:57 -050018#ifndef CONFIG_DM_GPIO
Keerthy6d9e7a82018-10-03 17:55:14 +053019#include <asm/arch/hardware.h>
20#include <asm/arch/davinci_misc.h>
21
Laurence Withers3ffd9682011-07-18 09:26:01 -040022static struct gpio_registry {
23 int is_registered;
24 char name[GPIO_NAME_SIZE];
25} gpio_registry[MAX_NUM_GPIOS];
26
Holger Hans Peter Freyther1356f8e2013-02-07 23:41:01 +000027#if defined(CONFIG_SOC_DA8XX)
Laurence Withers3ffd9682011-07-18 09:26:01 -040028#define pinmux(x) (&davinci_syscfg_regs->pinmux[x])
29
Tomas Novotny59032872013-02-01 06:46:00 +000030#if defined(CONFIG_SOC_DA8XX) && !defined(CONFIG_SOC_DA850)
31static const struct pinmux_config gpio_pinmux[] = {
32 { pinmux(13), 8, 6 }, /* GP0[0] */
33 { pinmux(13), 8, 7 },
34 { pinmux(14), 8, 0 },
35 { pinmux(14), 8, 1 },
36 { pinmux(14), 8, 2 },
37 { pinmux(14), 8, 3 },
38 { pinmux(14), 8, 4 },
39 { pinmux(14), 8, 5 },
40 { pinmux(14), 8, 6 },
41 { pinmux(14), 8, 7 },
42 { pinmux(15), 8, 0 },
43 { pinmux(15), 8, 1 },
44 { pinmux(15), 8, 2 },
45 { pinmux(15), 8, 3 },
46 { pinmux(15), 8, 4 },
47 { pinmux(15), 8, 5 },
48 { pinmux(15), 8, 6 }, /* GP1[0] */
49 { pinmux(15), 8, 7 },
50 { pinmux(16), 8, 0 },
51 { pinmux(16), 8, 1 },
52 { pinmux(16), 8, 2 },
53 { pinmux(16), 8, 3 },
54 { pinmux(16), 8, 4 },
55 { pinmux(16), 8, 5 },
56 { pinmux(16), 8, 6 },
57 { pinmux(16), 8, 7 },
58 { pinmux(17), 8, 0 },
59 { pinmux(17), 8, 1 },
60 { pinmux(17), 8, 2 },
61 { pinmux(17), 8, 3 },
62 { pinmux(17), 8, 4 },
63 { pinmux(17), 8, 5 },
64 { pinmux(17), 8, 6 }, /* GP2[0] */
65 { pinmux(17), 8, 7 },
66 { pinmux(18), 8, 0 },
67 { pinmux(18), 8, 1 },
68 { pinmux(18), 8, 2 },
69 { pinmux(18), 8, 3 },
70 { pinmux(18), 8, 4 },
71 { pinmux(18), 8, 5 },
72 { pinmux(18), 8, 6 },
73 { pinmux(18), 8, 7 },
74 { pinmux(19), 8, 0 },
75 { pinmux(9), 8, 2 },
76 { pinmux(9), 8, 3 },
77 { pinmux(9), 8, 4 },
78 { pinmux(9), 8, 5 },
79 { pinmux(9), 8, 6 },
80 { pinmux(10), 8, 1 }, /* GP3[0] */
81 { pinmux(10), 8, 2 },
82 { pinmux(10), 8, 3 },
83 { pinmux(10), 8, 4 },
84 { pinmux(10), 8, 5 },
85 { pinmux(10), 8, 6 },
86 { pinmux(10), 8, 7 },
87 { pinmux(11), 8, 0 },
88 { pinmux(11), 8, 1 },
89 { pinmux(11), 8, 2 },
90 { pinmux(11), 8, 3 },
91 { pinmux(11), 8, 4 },
92 { pinmux(9), 8, 7 },
93 { pinmux(2), 8, 6 },
94 { pinmux(11), 8, 5 },
95 { pinmux(11), 8, 6 },
96 { pinmux(12), 8, 4 }, /* GP4[0] */
97 { pinmux(12), 8, 5 },
98 { pinmux(12), 8, 6 },
99 { pinmux(12), 8, 7 },
100 { pinmux(13), 8, 0 },
101 { pinmux(13), 8, 1 },
102 { pinmux(13), 8, 2 },
103 { pinmux(13), 8, 3 },
104 { pinmux(13), 8, 4 },
105 { pinmux(13), 8, 5 },
106 { pinmux(11), 8, 7 },
107 { pinmux(12), 8, 0 },
108 { pinmux(12), 8, 1 },
109 { pinmux(12), 8, 2 },
110 { pinmux(12), 8, 3 },
111 { pinmux(9), 8, 1 },
112 { pinmux(7), 8, 3 }, /* GP5[0] */
113 { pinmux(7), 8, 4 },
114 { pinmux(7), 8, 5 },
115 { pinmux(7), 8, 6 },
116 { pinmux(7), 8, 7 },
117 { pinmux(8), 8, 0 },
118 { pinmux(8), 8, 1 },
119 { pinmux(8), 8, 2 },
120 { pinmux(8), 8, 3 },
121 { pinmux(8), 8, 4 },
122 { pinmux(8), 8, 5 },
123 { pinmux(8), 8, 6 },
124 { pinmux(8), 8, 7 },
125 { pinmux(9), 8, 0 },
126 { pinmux(7), 8, 1 },
127 { pinmux(7), 8, 2 },
128 { pinmux(5), 8, 1 }, /* GP6[0] */
129 { pinmux(5), 8, 2 },
130 { pinmux(5), 8, 3 },
131 { pinmux(5), 8, 4 },
132 { pinmux(5), 8, 5 },
133 { pinmux(5), 8, 6 },
134 { pinmux(5), 8, 7 },
135 { pinmux(6), 8, 0 },
136 { pinmux(6), 8, 1 },
137 { pinmux(6), 8, 2 },
138 { pinmux(6), 8, 3 },
139 { pinmux(6), 8, 4 },
140 { pinmux(6), 8, 5 },
141 { pinmux(6), 8, 6 },
142 { pinmux(6), 8, 7 },
143 { pinmux(7), 8, 0 },
144 { pinmux(1), 8, 0 }, /* GP7[0] */
145 { pinmux(1), 8, 1 },
146 { pinmux(1), 8, 2 },
147 { pinmux(1), 8, 3 },
148 { pinmux(1), 8, 4 },
149 { pinmux(1), 8, 5 },
150 { pinmux(1), 8, 6 },
151 { pinmux(1), 8, 7 },
152 { pinmux(2), 8, 0 },
153 { pinmux(2), 8, 1 },
154 { pinmux(2), 8, 2 },
155 { pinmux(2), 8, 3 },
156 { pinmux(2), 8, 4 },
157 { pinmux(2), 8, 5 },
158 { pinmux(0), 1, 0 },
159 { pinmux(0), 1, 1 },
160};
Tom Rini9dbc8362013-03-11 12:02:40 -0400161#else /* CONFIG_SOC_DA8XX && CONFIG_SOC_DA850 */
Laurence Withers3ffd9682011-07-18 09:26:01 -0400162static const struct pinmux_config gpio_pinmux[] = {
163 { pinmux(1), 8, 7 }, /* GP0[0] */
164 { pinmux(1), 8, 6 },
165 { pinmux(1), 8, 5 },
166 { pinmux(1), 8, 4 },
167 { pinmux(1), 8, 3 },
168 { pinmux(1), 8, 2 },
169 { pinmux(1), 8, 1 },
170 { pinmux(1), 8, 0 },
171 { pinmux(0), 8, 7 },
172 { pinmux(0), 8, 6 },
173 { pinmux(0), 8, 5 },
174 { pinmux(0), 8, 4 },
175 { pinmux(0), 8, 3 },
176 { pinmux(0), 8, 2 },
177 { pinmux(0), 8, 1 },
178 { pinmux(0), 8, 0 },
179 { pinmux(4), 8, 7 }, /* GP1[0] */
180 { pinmux(4), 8, 6 },
181 { pinmux(4), 8, 5 },
182 { pinmux(4), 8, 4 },
183 { pinmux(4), 8, 3 },
184 { pinmux(4), 8, 2 },
185 { pinmux(4), 4, 1 },
186 { pinmux(4), 4, 0 },
187 { pinmux(3), 4, 0 },
188 { pinmux(2), 4, 6 },
189 { pinmux(2), 4, 5 },
190 { pinmux(2), 4, 4 },
191 { pinmux(2), 4, 3 },
192 { pinmux(2), 4, 2 },
193 { pinmux(2), 4, 1 },
194 { pinmux(2), 8, 0 },
195 { pinmux(6), 8, 7 }, /* GP2[0] */
196 { pinmux(6), 8, 6 },
197 { pinmux(6), 8, 5 },
198 { pinmux(6), 8, 4 },
199 { pinmux(6), 8, 3 },
200 { pinmux(6), 8, 2 },
201 { pinmux(6), 8, 1 },
202 { pinmux(6), 8, 0 },
203 { pinmux(5), 8, 7 },
204 { pinmux(5), 8, 6 },
205 { pinmux(5), 8, 5 },
206 { pinmux(5), 8, 4 },
207 { pinmux(5), 8, 3 },
208 { pinmux(5), 8, 2 },
209 { pinmux(5), 8, 1 },
210 { pinmux(5), 8, 0 },
211 { pinmux(8), 8, 7 }, /* GP3[0] */
212 { pinmux(8), 8, 6 },
213 { pinmux(8), 8, 5 },
214 { pinmux(8), 8, 4 },
215 { pinmux(8), 8, 3 },
216 { pinmux(8), 8, 2 },
217 { pinmux(8), 8, 1 },
218 { pinmux(8), 8, 0 },
219 { pinmux(7), 8, 7 },
220 { pinmux(7), 8, 6 },
221 { pinmux(7), 8, 5 },
222 { pinmux(7), 8, 4 },
223 { pinmux(7), 8, 3 },
224 { pinmux(7), 8, 2 },
225 { pinmux(7), 8, 1 },
226 { pinmux(7), 8, 0 },
227 { pinmux(10), 8, 7 }, /* GP4[0] */
228 { pinmux(10), 8, 6 },
229 { pinmux(10), 8, 5 },
230 { pinmux(10), 8, 4 },
231 { pinmux(10), 8, 3 },
232 { pinmux(10), 8, 2 },
233 { pinmux(10), 8, 1 },
234 { pinmux(10), 8, 0 },
235 { pinmux(9), 8, 7 },
236 { pinmux(9), 8, 6 },
237 { pinmux(9), 8, 5 },
238 { pinmux(9), 8, 4 },
239 { pinmux(9), 8, 3 },
240 { pinmux(9), 8, 2 },
241 { pinmux(9), 8, 1 },
242 { pinmux(9), 8, 0 },
243 { pinmux(12), 8, 7 }, /* GP5[0] */
244 { pinmux(12), 8, 6 },
245 { pinmux(12), 8, 5 },
246 { pinmux(12), 8, 4 },
247 { pinmux(12), 8, 3 },
248 { pinmux(12), 8, 2 },
249 { pinmux(12), 8, 1 },
250 { pinmux(12), 8, 0 },
251 { pinmux(11), 8, 7 },
252 { pinmux(11), 8, 6 },
253 { pinmux(11), 8, 5 },
254 { pinmux(11), 8, 4 },
255 { pinmux(11), 8, 3 },
256 { pinmux(11), 8, 2 },
257 { pinmux(11), 8, 1 },
258 { pinmux(11), 8, 0 },
259 { pinmux(19), 8, 6 }, /* GP6[0] */
260 { pinmux(19), 8, 5 },
261 { pinmux(19), 8, 4 },
262 { pinmux(19), 8, 3 },
263 { pinmux(19), 8, 2 },
264 { pinmux(16), 8, 1 },
265 { pinmux(14), 8, 1 },
266 { pinmux(14), 8, 0 },
267 { pinmux(13), 8, 7 },
268 { pinmux(13), 8, 6 },
269 { pinmux(13), 8, 5 },
270 { pinmux(13), 8, 4 },
271 { pinmux(13), 8, 3 },
272 { pinmux(13), 8, 2 },
273 { pinmux(13), 8, 1 },
274 { pinmux(13), 8, 0 },
275 { pinmux(18), 8, 1 }, /* GP7[0] */
276 { pinmux(18), 8, 0 },
277 { pinmux(17), 8, 7 },
278 { pinmux(17), 8, 6 },
279 { pinmux(17), 8, 5 },
280 { pinmux(17), 8, 4 },
281 { pinmux(17), 8, 3 },
282 { pinmux(17), 8, 2 },
283 { pinmux(17), 8, 1 },
284 { pinmux(17), 8, 0 },
285 { pinmux(16), 8, 7 },
286 { pinmux(16), 8, 6 },
287 { pinmux(16), 8, 5 },
288 { pinmux(16), 8, 4 },
289 { pinmux(16), 8, 3 },
290 { pinmux(16), 8, 2 },
291 { pinmux(19), 8, 0 }, /* GP8[0] */
292 { pinmux(3), 4, 7 },
293 { pinmux(3), 4, 6 },
294 { pinmux(3), 4, 5 },
295 { pinmux(3), 4, 4 },
296 { pinmux(3), 4, 3 },
297 { pinmux(3), 4, 2 },
298 { pinmux(2), 4, 7 },
299 { pinmux(19), 8, 1 },
300 { pinmux(19), 8, 0 },
301 { pinmux(18), 8, 7 },
302 { pinmux(18), 8, 6 },
303 { pinmux(18), 8, 5 },
304 { pinmux(18), 8, 4 },
305 { pinmux(18), 8, 3 },
306 { pinmux(18), 8, 2 },
307};
Tom Rini9dbc8362013-03-11 12:02:40 -0400308#endif /* CONFIG_SOC_DA8XX && !CONFIG_SOC_DA850 */
309#else /* !CONFIG_SOC_DA8XX */
Holger Hans Peter Freyther1356f8e2013-02-07 23:41:01 +0000310#define davinci_configure_pin_mux(a, b)
Tom Rini9dbc8362013-03-11 12:02:40 -0400311#endif /* CONFIG_SOC_DA8XX */
Laurence Withers3ffd9682011-07-18 09:26:01 -0400312
Adam Ford9c286a72018-06-10 22:25:57 -0500313int gpio_request(unsigned int gpio, const char *label)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400314{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600315 if (gpio >= MAX_NUM_GPIOS)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400316 return -1;
317
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600318 if (gpio_registry[gpio].is_registered)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400319 return -1;
320
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600321 gpio_registry[gpio].is_registered = 1;
322 strncpy(gpio_registry[gpio].name, label, GPIO_NAME_SIZE);
323 gpio_registry[gpio].name[GPIO_NAME_SIZE - 1] = 0;
Laurence Withers3ffd9682011-07-18 09:26:01 -0400324
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600325 davinci_configure_pin_mux(&gpio_pinmux[gpio], 1);
Laurence Withers3ffd9682011-07-18 09:26:01 -0400326
327 return 0;
328}
329
Adam Ford9c286a72018-06-10 22:25:57 -0500330int gpio_free(unsigned int gpio)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400331{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600332 if (gpio >= MAX_NUM_GPIOS)
333 return -1;
Laurence Withers3ffd9682011-07-18 09:26:01 -0400334
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600335 if (!gpio_registry[gpio].is_registered)
336 return -1;
337
338 gpio_registry[gpio].is_registered = 0;
339 gpio_registry[gpio].name[0] = '\0';
340 /* Do not configure as input or change pin mux here */
341 return 0;
Laurence Withers3ffd9682011-07-18 09:26:01 -0400342}
Adam Ford9c286a72018-06-10 22:25:57 -0500343#endif
Laurence Withers3ffd9682011-07-18 09:26:01 -0400344
Adam Ford9c286a72018-06-10 22:25:57 -0500345static int _gpio_direction_output(struct davinci_gpio *bank, unsigned int gpio, int value)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400346{
Adam Ford9c286a72018-06-10 22:25:57 -0500347 clrbits_le32(&bank->dir, 1U << GPIO_BIT(gpio));
348 gpio_set_value(gpio, value);
Laurence Withers3ffd9682011-07-18 09:26:01 -0400349 return 0;
350}
351
Adam Ford9c286a72018-06-10 22:25:57 -0500352static int _gpio_direction_input(struct davinci_gpio *bank, unsigned int gpio)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400353{
Adam Ford9c286a72018-06-10 22:25:57 -0500354 setbits_le32(&bank->dir, 1U << GPIO_BIT(gpio));
Laurence Withers3ffd9682011-07-18 09:26:01 -0400355 return 0;
356}
357
Adam Ford9c286a72018-06-10 22:25:57 -0500358static int _gpio_get_value(struct davinci_gpio *bank, unsigned int gpio)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400359{
Laurence Withers3ffd9682011-07-18 09:26:01 -0400360 unsigned int ip;
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600361 ip = in_le32(&bank->in_data) & (1U << GPIO_BIT(gpio));
Laurence Withers3ffd9682011-07-18 09:26:01 -0400362 return ip ? 1 : 0;
363}
364
Adam Ford9c286a72018-06-10 22:25:57 -0500365static int _gpio_set_value(struct davinci_gpio *bank, unsigned int gpio, int value)
Laurence Withers3ffd9682011-07-18 09:26:01 -0400366{
Laurence Withers3ffd9682011-07-18 09:26:01 -0400367 if (value)
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600368 bank->set_data = 1U << GPIO_BIT(gpio);
Laurence Withers3ffd9682011-07-18 09:26:01 -0400369 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600370 bank->clr_data = 1U << GPIO_BIT(gpio);
371
372 return 0;
Laurence Withers3ffd9682011-07-18 09:26:01 -0400373}
374
Adam Ford9c286a72018-06-10 22:25:57 -0500375static int _gpio_get_dir(struct davinci_gpio *bank, unsigned int gpio)
376{
377 return in_le32(&bank->dir) & (1U << GPIO_BIT(gpio));
378}
379
380#ifndef CONFIG_DM_GPIO
381
Laurence Withers3ffd9682011-07-18 09:26:01 -0400382void gpio_info(void)
383{
Adam Ford9c286a72018-06-10 22:25:57 -0500384 unsigned int gpio, dir, val;
Laurence Withers3ffd9682011-07-18 09:26:01 -0400385 struct davinci_gpio *bank;
386
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600387 for (gpio = 0; gpio < MAX_NUM_GPIOS; ++gpio) {
388 bank = GPIO_BANK(gpio);
Adam Ford9c286a72018-06-10 22:25:57 -0500389 dir = _gpio_get_dir(bank, gpio);
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600390 val = gpio_get_value(gpio);
Laurence Withers3ffd9682011-07-18 09:26:01 -0400391
392 printf("% 4d: %s: %d [%c] %s\n",
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600393 gpio, dir ? " in" : "out", val,
394 gpio_registry[gpio].is_registered ? 'x' : ' ',
395 gpio_registry[gpio].name);
Laurence Withers3ffd9682011-07-18 09:26:01 -0400396 }
397}
Adam Ford9c286a72018-06-10 22:25:57 -0500398
399int gpio_direction_input(unsigned int gpio)
400{
401 struct davinci_gpio *bank;
402
403 bank = GPIO_BANK(gpio);
404 return _gpio_direction_input(bank, gpio);
405}
406
407int gpio_direction_output(unsigned int gpio, int value)
408{
409 struct davinci_gpio *bank;
410
411 bank = GPIO_BANK(gpio);
412 return _gpio_direction_output(bank, gpio, value);
413}
414
415int gpio_get_value(unsigned int gpio)
416{
417 struct davinci_gpio *bank;
418
419 bank = GPIO_BANK(gpio);
420 return _gpio_get_value(bank, gpio);
421}
422
423int gpio_set_value(unsigned int gpio, int value)
424{
425 struct davinci_gpio *bank;
426
427 bank = GPIO_BANK(gpio);
428 return _gpio_set_value(bank, gpio, value);
429}
430
431#else /* CONFIG_DM_GPIO */
432
433static struct davinci_gpio *davinci_get_gpio_bank(struct udevice *dev, unsigned int offset)
434{
435 struct davinci_gpio_bank *bank = dev_get_priv(dev);
Adam Forda1129392018-08-16 23:13:34 -0500436 unsigned int addr;
Adam Ford9c286a72018-06-10 22:25:57 -0500437
Adam Forda1129392018-08-16 23:13:34 -0500438 /*
439 * The device tree is not broken into banks but the infrastructure is
Adam Ford9c286a72018-06-10 22:25:57 -0500440 * expecting it this way, so we'll first include the 0x10 offset, then
441 * calculate the bank manually based on the offset.
Adam Forda1129392018-08-16 23:13:34 -0500442 * Casting 'addr' as Unsigned long is needed to make the math work.
Adam Ford9c286a72018-06-10 22:25:57 -0500443 */
Adam Forda1129392018-08-16 23:13:34 -0500444 addr = ((unsigned long)(struct davinci_gpio *)bank->base) +
445 0x10 + (0x28 * (offset >> 5));
446 return (struct davinci_gpio *)addr;
Adam Ford9c286a72018-06-10 22:25:57 -0500447}
448
449static int davinci_gpio_direction_input(struct udevice *dev, unsigned int offset)
450{
451 struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset);
452
Adam Forda1129392018-08-16 23:13:34 -0500453 /*
454 * Fetch the address based on GPIO, but only pass the masked low 32-bits
455 */
456 _gpio_direction_input(base, (offset & 0x1f));
Adam Ford9c286a72018-06-10 22:25:57 -0500457 return 0;
458}
459
460static int davinci_gpio_direction_output(struct udevice *dev, unsigned int offset,
461 int value)
462{
463 struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset);
464
Adam Forda1129392018-08-16 23:13:34 -0500465 _gpio_direction_output(base, (offset & 0x1f), value);
Adam Ford9c286a72018-06-10 22:25:57 -0500466 return 0;
467}
468
469static int davinci_gpio_get_value(struct udevice *dev, unsigned int offset)
470{
471 struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset);
472
Adam Forda1129392018-08-16 23:13:34 -0500473 return _gpio_get_value(base, (offset & 0x1f));
Adam Ford9c286a72018-06-10 22:25:57 -0500474}
475
476static int davinci_gpio_set_value(struct udevice *dev, unsigned int offset,
477 int value)
478{
479 struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset);
480
Adam Forda1129392018-08-16 23:13:34 -0500481 _gpio_set_value(base, (offset & 0x1f), value);
Adam Ford9c286a72018-06-10 22:25:57 -0500482
483 return 0;
484}
485
486static int davinci_gpio_get_function(struct udevice *dev, unsigned int offset)
487{
488 unsigned int dir;
489 struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset);
490
491 dir = _gpio_get_dir(base, offset);
492
493 if (dir)
494 return GPIOF_INPUT;
495
496 return GPIOF_OUTPUT;
497}
498
Adam Forda4cb26d2018-08-16 23:21:57 -0500499static int davinci_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
500 struct ofnode_phandle_args *args)
501{
502 desc->offset = args->args[0];
503
504 if (args->args[1] & GPIO_ACTIVE_LOW)
505 desc->flags = GPIOD_ACTIVE_LOW;
506 else
507 desc->flags = 0;
508 return 0;
509}
510
Adam Ford9c286a72018-06-10 22:25:57 -0500511static const struct dm_gpio_ops gpio_davinci_ops = {
512 .direction_input = davinci_gpio_direction_input,
513 .direction_output = davinci_gpio_direction_output,
514 .get_value = davinci_gpio_get_value,
515 .set_value = davinci_gpio_set_value,
516 .get_function = davinci_gpio_get_function,
Adam Forda4cb26d2018-08-16 23:21:57 -0500517 .xlate = davinci_gpio_xlate,
Adam Ford9c286a72018-06-10 22:25:57 -0500518};
519
520static int davinci_gpio_probe(struct udevice *dev)
521{
522 struct davinci_gpio_bank *bank = dev_get_priv(dev);
523 struct davinci_gpio_platdata *plat = dev_get_platdata(dev);
524 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
525 const void *fdt = gd->fdt_blob;
526 int node = dev_of_offset(dev);
527
528 uc_priv->bank_name = plat->port_name;
529 uc_priv->gpio_count = fdtdec_get_int(fdt, node, "ti,ngpio", -1);
530 bank->base = (struct davinci_gpio *)plat->base;
531 return 0;
532}
533
534static const struct udevice_id davinci_gpio_ids[] = {
535 { .compatible = "ti,dm6441-gpio" },
Keerthy0e9265c2018-10-03 17:55:13 +0530536 { .compatible = "ti,k2g-gpio" },
Adam Ford9c286a72018-06-10 22:25:57 -0500537 { }
538};
539
540static int davinci_gpio_ofdata_to_platdata(struct udevice *dev)
541{
542 struct davinci_gpio_platdata *plat = dev_get_platdata(dev);
543 fdt_addr_t addr;
544
545 addr = devfdt_get_addr(dev);
546 if (addr == FDT_ADDR_T_NONE)
547 return -EINVAL;
548
549 plat->base = addr;
550 return 0;
551}
552
553U_BOOT_DRIVER(gpio_davinci) = {
554 .name = "gpio_davinci",
555 .id = UCLASS_GPIO,
556 .ops = &gpio_davinci_ops,
557 .ofdata_to_platdata = of_match_ptr(davinci_gpio_ofdata_to_platdata),
558 .of_match = davinci_gpio_ids,
559 .bind = dm_scan_fdt_dev,
560 .platdata_auto_alloc_size = sizeof(struct davinci_gpio_platdata),
561 .probe = davinci_gpio_probe,
562 .priv_auto_alloc_size = sizeof(struct davinci_gpio_bank),
563};
564
565#endif