blob: bd5a366aef3282ff332788821d393e534c66a4e8 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Laurence Withersf517afd2011-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 Withersf517afd2011-07-18 09:26:01 -04007 */
8
9#include <common.h>
Adam Ford8e51c0f2018-06-10 22:25:57 -050010#include <dm.h>
11#include <fdtdec.h>
Laurence Withersf517afd2011-07-18 09:26:01 -040012#include <asm/io.h>
13#include <asm/gpio.h>
Adam Ford1eddf542018-08-16 23:13:34 -050014#include <dt-bindings/gpio/gpio.h>
Laurence Withersf517afd2011-07-18 09:26:01 -040015
Keerthy0f3cf2b2018-10-03 17:55:14 +053016#include "da8xx_gpio.h"
17
Simon Glassbcee8d62019-12-06 21:41:35 -070018#if !CONFIG_IS_ENABLED(DM_GPIO)
Keerthy0f3cf2b2018-10-03 17:55:14 +053019#include <asm/arch/hardware.h>
20#include <asm/arch/davinci_misc.h>
21
Laurence Withersf517afd2011-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 Freyther03414ac2013-02-07 23:41:01 +000027#if defined(CONFIG_SOC_DA8XX)
Laurence Withersf517afd2011-07-18 09:26:01 -040028#define pinmux(x) (&davinci_syscfg_regs->pinmux[x])
29
Tomas Novotnyb9f56692013-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 Rini76b40ab2013-03-11 12:02:40 -0400161#else /* CONFIG_SOC_DA8XX && CONFIG_SOC_DA850 */
Laurence Withersf517afd2011-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 Rini76b40ab2013-03-11 12:02:40 -0400308#endif /* CONFIG_SOC_DA8XX && !CONFIG_SOC_DA850 */
309#else /* !CONFIG_SOC_DA8XX */
Holger Hans Peter Freyther03414ac2013-02-07 23:41:01 +0000310#define davinci_configure_pin_mux(a, b)
Tom Rini76b40ab2013-03-11 12:02:40 -0400311#endif /* CONFIG_SOC_DA8XX */
Laurence Withersf517afd2011-07-18 09:26:01 -0400312
Adam Ford8e51c0f2018-06-10 22:25:57 -0500313int gpio_request(unsigned int gpio, const char *label)
Laurence Withersf517afd2011-07-18 09:26:01 -0400314{
Joe Hershberger365d6072011-11-11 15:55:36 -0600315 if (gpio >= MAX_NUM_GPIOS)
Laurence Withersf517afd2011-07-18 09:26:01 -0400316 return -1;
317
Joe Hershberger365d6072011-11-11 15:55:36 -0600318 if (gpio_registry[gpio].is_registered)
Laurence Withersf517afd2011-07-18 09:26:01 -0400319 return -1;
320
Joe Hershberger365d6072011-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 Withersf517afd2011-07-18 09:26:01 -0400324
Joe Hershberger365d6072011-11-11 15:55:36 -0600325 davinci_configure_pin_mux(&gpio_pinmux[gpio], 1);
Laurence Withersf517afd2011-07-18 09:26:01 -0400326
327 return 0;
328}
329
Adam Ford8e51c0f2018-06-10 22:25:57 -0500330int gpio_free(unsigned int gpio)
Laurence Withersf517afd2011-07-18 09:26:01 -0400331{
Joe Hershberger365d6072011-11-11 15:55:36 -0600332 if (gpio >= MAX_NUM_GPIOS)
333 return -1;
334
335 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 Withersf517afd2011-07-18 09:26:01 -0400342}
Adam Ford8e51c0f2018-06-10 22:25:57 -0500343#endif
Laurence Withersf517afd2011-07-18 09:26:01 -0400344
Adam Ford8e51c0f2018-06-10 22:25:57 -0500345static int _gpio_direction_input(struct davinci_gpio *bank, unsigned int gpio)
Laurence Withersf517afd2011-07-18 09:26:01 -0400346{
Adam Ford8e51c0f2018-06-10 22:25:57 -0500347 setbits_le32(&bank->dir, 1U << GPIO_BIT(gpio));
348 return 0;
349}
Laurence Withersf517afd2011-07-18 09:26:01 -0400350
Adam Ford8e51c0f2018-06-10 22:25:57 -0500351static int _gpio_get_value(struct davinci_gpio *bank, unsigned int gpio)
352{
353 unsigned int ip;
Joe Hershberger365d6072011-11-11 15:55:36 -0600354 ip = in_le32(&bank->in_data) & (1U << GPIO_BIT(gpio));
Laurence Withersf517afd2011-07-18 09:26:01 -0400355 return ip ? 1 : 0;
356}
357
Adam Ford8e51c0f2018-06-10 22:25:57 -0500358static int _gpio_set_value(struct davinci_gpio *bank, unsigned int gpio, int value)
Laurence Withersf517afd2011-07-18 09:26:01 -0400359{
Laurence Withersf517afd2011-07-18 09:26:01 -0400360 if (value)
Joe Hershberger365d6072011-11-11 15:55:36 -0600361 bank->set_data = 1U << GPIO_BIT(gpio);
Laurence Withersf517afd2011-07-18 09:26:01 -0400362 else
Joe Hershberger365d6072011-11-11 15:55:36 -0600363 bank->clr_data = 1U << GPIO_BIT(gpio);
364
365 return 0;
Laurence Withersf517afd2011-07-18 09:26:01 -0400366}
367
Adam Ford8e51c0f2018-06-10 22:25:57 -0500368static int _gpio_get_dir(struct davinci_gpio *bank, unsigned int gpio)
369{
370 return in_le32(&bank->dir) & (1U << GPIO_BIT(gpio));
371}
372
Keerthy31350222019-10-24 13:52:28 +0530373static int _gpio_direction_output(struct davinci_gpio *bank, unsigned int gpio,
374 int value)
375{
376 clrbits_le32(&bank->dir, 1U << GPIO_BIT(gpio));
377 _gpio_set_value(bank, gpio, value);
378 return 0;
379}
Simon Glassbcee8d62019-12-06 21:41:35 -0700380
381#if !CONFIG_IS_ENABLED(DM_GPIO)
Adam Ford8e51c0f2018-06-10 22:25:57 -0500382
Laurence Withersf517afd2011-07-18 09:26:01 -0400383void gpio_info(void)
384{
Adam Ford8e51c0f2018-06-10 22:25:57 -0500385 unsigned int gpio, dir, val;
Laurence Withersf517afd2011-07-18 09:26:01 -0400386 struct davinci_gpio *bank;
387
Joe Hershberger365d6072011-11-11 15:55:36 -0600388 for (gpio = 0; gpio < MAX_NUM_GPIOS; ++gpio) {
389 bank = GPIO_BANK(gpio);
Adam Ford8e51c0f2018-06-10 22:25:57 -0500390 dir = _gpio_get_dir(bank, gpio);
Joe Hershberger365d6072011-11-11 15:55:36 -0600391 val = gpio_get_value(gpio);
Laurence Withersf517afd2011-07-18 09:26:01 -0400392
393 printf("% 4d: %s: %d [%c] %s\n",
Joe Hershberger365d6072011-11-11 15:55:36 -0600394 gpio, dir ? " in" : "out", val,
395 gpio_registry[gpio].is_registered ? 'x' : ' ',
396 gpio_registry[gpio].name);
Laurence Withersf517afd2011-07-18 09:26:01 -0400397 }
398}
Adam Ford8e51c0f2018-06-10 22:25:57 -0500399
400int gpio_direction_input(unsigned int gpio)
401{
402 struct davinci_gpio *bank;
403
404 bank = GPIO_BANK(gpio);
405 return _gpio_direction_input(bank, gpio);
406}
407
408int gpio_direction_output(unsigned int gpio, int value)
409{
410 struct davinci_gpio *bank;
411
412 bank = GPIO_BANK(gpio);
413 return _gpio_direction_output(bank, gpio, value);
414}
415
416int gpio_get_value(unsigned int gpio)
417{
418 struct davinci_gpio *bank;
419
420 bank = GPIO_BANK(gpio);
421 return _gpio_get_value(bank, gpio);
422}
423
424int gpio_set_value(unsigned int gpio, int value)
425{
426 struct davinci_gpio *bank;
427
428 bank = GPIO_BANK(gpio);
429 return _gpio_set_value(bank, gpio, value);
430}
431
Simon Glassbcee8d62019-12-06 21:41:35 -0700432#else /* DM_GPIO */
Adam Ford8e51c0f2018-06-10 22:25:57 -0500433
434static struct davinci_gpio *davinci_get_gpio_bank(struct udevice *dev, unsigned int offset)
435{
436 struct davinci_gpio_bank *bank = dev_get_priv(dev);
Adam Ford1eddf542018-08-16 23:13:34 -0500437 unsigned int addr;
Adam Ford8e51c0f2018-06-10 22:25:57 -0500438
Adam Ford1eddf542018-08-16 23:13:34 -0500439 /*
440 * The device tree is not broken into banks but the infrastructure is
Adam Ford8e51c0f2018-06-10 22:25:57 -0500441 * expecting it this way, so we'll first include the 0x10 offset, then
442 * calculate the bank manually based on the offset.
Adam Ford1eddf542018-08-16 23:13:34 -0500443 * Casting 'addr' as Unsigned long is needed to make the math work.
Adam Ford8e51c0f2018-06-10 22:25:57 -0500444 */
Adam Ford1eddf542018-08-16 23:13:34 -0500445 addr = ((unsigned long)(struct davinci_gpio *)bank->base) +
446 0x10 + (0x28 * (offset >> 5));
447 return (struct davinci_gpio *)addr;
Adam Ford8e51c0f2018-06-10 22:25:57 -0500448}
449
450static int davinci_gpio_direction_input(struct udevice *dev, unsigned int offset)
451{
452 struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset);
453
Adam Ford1eddf542018-08-16 23:13:34 -0500454 /*
455 * Fetch the address based on GPIO, but only pass the masked low 32-bits
456 */
457 _gpio_direction_input(base, (offset & 0x1f));
Adam Ford8e51c0f2018-06-10 22:25:57 -0500458 return 0;
459}
460
461static int davinci_gpio_direction_output(struct udevice *dev, unsigned int offset,
462 int value)
463{
464 struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset);
465
Adam Ford1eddf542018-08-16 23:13:34 -0500466 _gpio_direction_output(base, (offset & 0x1f), value);
Adam Ford8e51c0f2018-06-10 22:25:57 -0500467 return 0;
468}
469
470static int davinci_gpio_get_value(struct udevice *dev, unsigned int offset)
471{
472 struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset);
473
Adam Ford1eddf542018-08-16 23:13:34 -0500474 return _gpio_get_value(base, (offset & 0x1f));
Adam Ford8e51c0f2018-06-10 22:25:57 -0500475}
476
477static int davinci_gpio_set_value(struct udevice *dev, unsigned int offset,
478 int value)
479{
480 struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset);
481
Adam Ford1eddf542018-08-16 23:13:34 -0500482 _gpio_set_value(base, (offset & 0x1f), value);
Adam Ford8e51c0f2018-06-10 22:25:57 -0500483
484 return 0;
485}
486
487static int davinci_gpio_get_function(struct udevice *dev, unsigned int offset)
488{
489 unsigned int dir;
490 struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset);
491
492 dir = _gpio_get_dir(base, offset);
493
494 if (dir)
495 return GPIOF_INPUT;
496
497 return GPIOF_OUTPUT;
498}
499
Adam Ford9440b3d2018-08-16 23:21:57 -0500500static int davinci_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
501 struct ofnode_phandle_args *args)
502{
503 desc->offset = args->args[0];
504
505 if (args->args[1] & GPIO_ACTIVE_LOW)
506 desc->flags = GPIOD_ACTIVE_LOW;
507 else
508 desc->flags = 0;
509 return 0;
510}
511
Adam Ford8e51c0f2018-06-10 22:25:57 -0500512static const struct dm_gpio_ops gpio_davinci_ops = {
513 .direction_input = davinci_gpio_direction_input,
514 .direction_output = davinci_gpio_direction_output,
515 .get_value = davinci_gpio_get_value,
516 .set_value = davinci_gpio_set_value,
517 .get_function = davinci_gpio_get_function,
Adam Ford9440b3d2018-08-16 23:21:57 -0500518 .xlate = davinci_gpio_xlate,
Adam Ford8e51c0f2018-06-10 22:25:57 -0500519};
520
521static int davinci_gpio_probe(struct udevice *dev)
522{
523 struct davinci_gpio_bank *bank = dev_get_priv(dev);
524 struct davinci_gpio_platdata *plat = dev_get_platdata(dev);
525 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
526 const void *fdt = gd->fdt_blob;
527 int node = dev_of_offset(dev);
528
529 uc_priv->bank_name = plat->port_name;
530 uc_priv->gpio_count = fdtdec_get_int(fdt, node, "ti,ngpio", -1);
531 bank->base = (struct davinci_gpio *)plat->base;
532 return 0;
533}
534
535static const struct udevice_id davinci_gpio_ids[] = {
536 { .compatible = "ti,dm6441-gpio" },
Keerthy401d74c2018-10-03 17:55:13 +0530537 { .compatible = "ti,k2g-gpio" },
Adam Ford8e51c0f2018-06-10 22:25:57 -0500538 { }
539};
540
541static int davinci_gpio_ofdata_to_platdata(struct udevice *dev)
542{
543 struct davinci_gpio_platdata *plat = dev_get_platdata(dev);
544 fdt_addr_t addr;
545
546 addr = devfdt_get_addr(dev);
547 if (addr == FDT_ADDR_T_NONE)
548 return -EINVAL;
549
550 plat->base = addr;
551 return 0;
552}
553
554U_BOOT_DRIVER(gpio_davinci) = {
555 .name = "gpio_davinci",
556 .id = UCLASS_GPIO,
557 .ops = &gpio_davinci_ops,
558 .ofdata_to_platdata = of_match_ptr(davinci_gpio_ofdata_to_platdata),
559 .of_match = davinci_gpio_ids,
560 .bind = dm_scan_fdt_dev,
561 .platdata_auto_alloc_size = sizeof(struct davinci_gpio_platdata),
562 .probe = davinci_gpio_probe,
563 .priv_auto_alloc_size = sizeof(struct davinci_gpio_bank),
564};
565
566#endif