blob: 8e52e3dad0af181e0df608466134ea3ba0660bef [file] [log] [blame]
Jens Scharsigea8fbba2010-02-03 22:46:16 +01001/*
Bo Shen39b787e2013-08-13 14:38:32 +08002 * Copyright (C) 2013 Bo Shen <voice.shen@atmel.com>
Jens Scharsigea8fbba2010-02-03 22:46:16 +01003 *
4 * Copyright (C) 2009 Jens Scharsig (js_at_ng@scharsoft.de)
5 *
6 * Copyright (C) 2005 HP Labs
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Jens Scharsigea8fbba2010-02-03 22:46:16 +01009 */
10
11#include <config.h>
12#include <common.h>
Simon Glass918354b2014-10-29 13:08:57 -060013#include <dm.h>
Reinhard Meyer86592f62010-11-07 13:26:14 +010014#include <asm/io.h>
Alexey Brodkin1ace4022014-02-26 17:47:58 +040015#include <linux/sizes.h>
Simon Glass918354b2014-10-29 13:08:57 -060016#include <asm/gpio.h>
Jens Scharsigea8fbba2010-02-03 22:46:16 +010017#include <asm/arch/hardware.h>
Jens Scharsigea8fbba2010-02-03 22:46:16 +010018#include <asm/arch/at91_pio.h>
Simon Glass918354b2014-10-29 13:08:57 -060019
20#define GPIO_PER_BANK 32
Jens Scharsigea8fbba2010-02-03 22:46:16 +010021
Bo Shen4bc9b7a2013-08-22 15:24:40 +080022static struct at91_port *at91_pio_get_port(unsigned port)
23{
24 switch (port) {
25 case AT91_PIO_PORTA:
26 return (struct at91_port *)ATMEL_BASE_PIOA;
27 case AT91_PIO_PORTB:
28 return (struct at91_port *)ATMEL_BASE_PIOB;
29 case AT91_PIO_PORTC:
30 return (struct at91_port *)ATMEL_BASE_PIOC;
31#if (ATMEL_PIO_PORTS > 3)
32 case AT91_PIO_PORTD:
33 return (struct at91_port *)ATMEL_BASE_PIOD;
34#if (ATMEL_PIO_PORTS > 4)
35 case AT91_PIO_PORTE:
36 return (struct at91_port *)ATMEL_BASE_PIOE;
37#endif
38#endif
39 default:
Wu, Josh7d82d892014-05-07 16:50:45 +080040 printf("Error: at91_gpio: Fail to get PIO base!\n");
Bo Shen4bc9b7a2013-08-22 15:24:40 +080041 return NULL;
42 }
43}
44
Simon Glass918354b2014-10-29 13:08:57 -060045static void at91_set_port_pullup(struct at91_port *at91_port, unsigned offset,
46 int use_pullup)
47{
48 u32 mask;
49
50 mask = 1 << offset;
51 if (use_pullup)
52 writel(mask, &at91_port->puer);
53 else
54 writel(mask, &at91_port->pudr);
55 writel(mask, &at91_port->per);
56}
57
Jens Scharsigea8fbba2010-02-03 22:46:16 +010058int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup)
59{
Bo Shen4bc9b7a2013-08-22 15:24:40 +080060 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsigea8fbba2010-02-03 22:46:16 +010061
Marek Vasut152ac5f2016-05-04 23:05:23 +020062#if defined(CPU_HAS_PIO3)
63 if (use_pullup)
64 at91_set_pio_pulldown(port, pin, 0);
65#endif
66
Simon Glass918354b2014-10-29 13:08:57 -060067 if (at91_port && (pin < GPIO_PER_BANK))
68 at91_set_port_pullup(at91_port, pin, use_pullup);
Bo Shen4bc9b7a2013-08-22 15:24:40 +080069
Jens Scharsigea8fbba2010-02-03 22:46:16 +010070 return 0;
71}
72
73/*
74 * mux the pin to the "GPIO" peripheral role.
75 */
76int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup)
77{
Bo Shen4bc9b7a2013-08-22 15:24:40 +080078 struct at91_port *at91_port = at91_pio_get_port(port);
79 u32 mask;
Jens Scharsigea8fbba2010-02-03 22:46:16 +010080
Simon Glass918354b2014-10-29 13:08:57 -060081 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsigea8fbba2010-02-03 22:46:16 +010082 mask = 1 << pin;
Bo Shen4bc9b7a2013-08-22 15:24:40 +080083 writel(mask, &at91_port->idr);
Jens Scharsigea8fbba2010-02-03 22:46:16 +010084 at91_set_pio_pullup(port, pin, use_pullup);
Bo Shen4bc9b7a2013-08-22 15:24:40 +080085 writel(mask, &at91_port->per);
Jens Scharsigea8fbba2010-02-03 22:46:16 +010086 }
Bo Shen4bc9b7a2013-08-22 15:24:40 +080087
Jens Scharsigea8fbba2010-02-03 22:46:16 +010088 return 0;
89}
90
91/*
92 * mux the pin to the "A" internal peripheral role.
93 */
94int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup)
95{
Bo Shen4bc9b7a2013-08-22 15:24:40 +080096 struct at91_port *at91_port = at91_pio_get_port(port);
97 u32 mask;
Jens Scharsigea8fbba2010-02-03 22:46:16 +010098
Simon Glass918354b2014-10-29 13:08:57 -060099 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100100 mask = 1 << pin;
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800101 writel(mask, &at91_port->idr);
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100102 at91_set_pio_pullup(port, pin, use_pullup);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000103#if defined(CPU_HAS_PIO3)
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800104 writel(readl(&at91_port->abcdsr1) & ~mask,
105 &at91_port->abcdsr1);
106 writel(readl(&at91_port->abcdsr2) & ~mask,
107 &at91_port->abcdsr2);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000108#else
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800109 writel(mask, &at91_port->asr);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000110#endif
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800111 writel(mask, &at91_port->pdr);
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100112 }
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800113
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100114 return 0;
115}
116
117/*
118 * mux the pin to the "B" internal peripheral role.
119 */
120int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup)
121{
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800122 struct at91_port *at91_port = at91_pio_get_port(port);
123 u32 mask;
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100124
Simon Glass918354b2014-10-29 13:08:57 -0600125 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100126 mask = 1 << pin;
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800127 writel(mask, &at91_port->idr);
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100128 at91_set_pio_pullup(port, pin, use_pullup);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000129#if defined(CPU_HAS_PIO3)
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800130 writel(readl(&at91_port->abcdsr1) | mask,
131 &at91_port->abcdsr1);
132 writel(readl(&at91_port->abcdsr2) & ~mask,
133 &at91_port->abcdsr2);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000134#else
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800135 writel(mask, &at91_port->bsr);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000136#endif
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800137 writel(mask, &at91_port->pdr);
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100138 }
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800139
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100140 return 0;
141}
142
Bo Shen2b3b1c62012-05-20 15:50:00 +0000143#if defined(CPU_HAS_PIO3)
144/*
145 * mux the pin to the "C" internal peripheral role.
146 */
147int at91_set_c_periph(unsigned port, unsigned pin, int use_pullup)
148{
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800149 struct at91_port *at91_port = at91_pio_get_port(port);
150 u32 mask;
Bo Shen2b3b1c62012-05-20 15:50:00 +0000151
Simon Glass918354b2014-10-29 13:08:57 -0600152 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen2b3b1c62012-05-20 15:50:00 +0000153 mask = 1 << pin;
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800154 writel(mask, &at91_port->idr);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000155 at91_set_pio_pullup(port, pin, use_pullup);
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800156 writel(readl(&at91_port->abcdsr1) & ~mask,
157 &at91_port->abcdsr1);
158 writel(readl(&at91_port->abcdsr2) | mask,
159 &at91_port->abcdsr2);
160 writel(mask, &at91_port->pdr);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000161 }
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800162
Bo Shen2b3b1c62012-05-20 15:50:00 +0000163 return 0;
164}
165
166/*
167 * mux the pin to the "D" internal peripheral role.
168 */
169int at91_set_d_periph(unsigned port, unsigned pin, int use_pullup)
170{
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800171 struct at91_port *at91_port = at91_pio_get_port(port);
172 u32 mask;
Bo Shen2b3b1c62012-05-20 15:50:00 +0000173
Simon Glass918354b2014-10-29 13:08:57 -0600174 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen2b3b1c62012-05-20 15:50:00 +0000175 mask = 1 << pin;
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800176 writel(mask, &at91_port->idr);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000177 at91_set_pio_pullup(port, pin, use_pullup);
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800178 writel(readl(&at91_port->abcdsr1) | mask,
179 &at91_port->abcdsr1);
180 writel(readl(&at91_port->abcdsr2) | mask,
181 &at91_port->abcdsr2);
182 writel(mask, &at91_port->pdr);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000183 }
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800184
Bo Shen2b3b1c62012-05-20 15:50:00 +0000185 return 0;
186}
187#endif
188
Simon Glass918354b2014-10-29 13:08:57 -0600189#ifdef CONFIG_DM_GPIO
190static bool at91_get_port_output(struct at91_port *at91_port, int offset)
191{
192 u32 mask, val;
193
194 mask = 1 << offset;
195 val = readl(&at91_port->osr);
196 return val & mask;
197}
198#endif
199
200static void at91_set_port_input(struct at91_port *at91_port, int offset,
201 int use_pullup)
202{
203 u32 mask;
204
205 mask = 1 << offset;
206 writel(mask, &at91_port->idr);
207 at91_set_port_pullup(at91_port, offset, use_pullup);
208 writel(mask, &at91_port->odr);
209 writel(mask, &at91_port->per);
210}
211
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100212/*
213 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
214 * configure it for an input.
215 */
216int at91_set_pio_input(unsigned port, u32 pin, int use_pullup)
217{
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800218 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100219
Simon Glass918354b2014-10-29 13:08:57 -0600220 if (at91_port && (pin < GPIO_PER_BANK))
221 at91_set_port_input(at91_port, pin, use_pullup);
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800222
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100223 return 0;
224}
225
Simon Glass918354b2014-10-29 13:08:57 -0600226static void at91_set_port_output(struct at91_port *at91_port, int offset,
227 int value)
228{
229 u32 mask;
230
231 mask = 1 << offset;
232 writel(mask, &at91_port->idr);
233 writel(mask, &at91_port->pudr);
234 if (value)
235 writel(mask, &at91_port->sodr);
236 else
237 writel(mask, &at91_port->codr);
238 writel(mask, &at91_port->oer);
239 writel(mask, &at91_port->per);
240}
241
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100242/*
243 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
244 * and configure it for an output.
245 */
246int at91_set_pio_output(unsigned port, u32 pin, int value)
247{
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800248 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100249
Simon Glass918354b2014-10-29 13:08:57 -0600250 if (at91_port && (pin < GPIO_PER_BANK))
251 at91_set_port_output(at91_port, pin, value);
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800252
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100253 return 0;
254}
255
256/*
257 * enable/disable the glitch filter. mostly used with IRQ handling.
258 */
259int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on)
260{
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800261 struct at91_port *at91_port = at91_pio_get_port(port);
262 u32 mask;
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100263
Simon Glass918354b2014-10-29 13:08:57 -0600264 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100265 mask = 1 << pin;
Bo Shen2b3b1c62012-05-20 15:50:00 +0000266 if (is_on) {
267#if defined(CPU_HAS_PIO3)
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800268 writel(mask, &at91_port->ifscdr);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000269#endif
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800270 writel(mask, &at91_port->ifer);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000271 } else {
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800272 writel(mask, &at91_port->ifdr);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000273 }
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100274 }
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800275
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100276 return 0;
277}
278
Bo Shen2b3b1c62012-05-20 15:50:00 +0000279#if defined(CPU_HAS_PIO3)
280/*
281 * enable/disable the debounce filter.
282 */
283int at91_set_pio_debounce(unsigned port, unsigned pin, int is_on, int div)
284{
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800285 struct at91_port *at91_port = at91_pio_get_port(port);
286 u32 mask;
Bo Shen2b3b1c62012-05-20 15:50:00 +0000287
Simon Glass918354b2014-10-29 13:08:57 -0600288 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen2b3b1c62012-05-20 15:50:00 +0000289 mask = 1 << pin;
290 if (is_on) {
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800291 writel(mask, &at91_port->ifscer);
292 writel(div & PIO_SCDR_DIV, &at91_port->scdr);
293 writel(mask, &at91_port->ifer);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000294 } else {
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800295 writel(mask, &at91_port->ifdr);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000296 }
297 }
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800298
Bo Shen2b3b1c62012-05-20 15:50:00 +0000299 return 0;
300}
301
302/*
303 * enable/disable the pull-down.
304 * If pull-up already enabled while calling the function, we disable it.
305 */
306int at91_set_pio_pulldown(unsigned port, unsigned pin, int is_on)
307{
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800308 struct at91_port *at91_port = at91_pio_get_port(port);
309 u32 mask;
Bo Shen2b3b1c62012-05-20 15:50:00 +0000310
Simon Glass918354b2014-10-29 13:08:57 -0600311 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen2b3b1c62012-05-20 15:50:00 +0000312 mask = 1 << pin;
Marek Vasut152ac5f2016-05-04 23:05:23 +0200313 if (is_on) {
314 at91_set_pio_pullup(port, pin, 0);
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800315 writel(mask, &at91_port->ppder);
Marek Vasut152ac5f2016-05-04 23:05:23 +0200316 } else
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800317 writel(mask, &at91_port->ppddr);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000318 }
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800319
Bo Shen2b3b1c62012-05-20 15:50:00 +0000320 return 0;
321}
322
323/*
324 * disable Schmitt trigger
325 */
326int at91_set_pio_disable_schmitt_trig(unsigned port, unsigned pin)
327{
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800328 struct at91_port *at91_port = at91_pio_get_port(port);
329 u32 mask;
Bo Shen2b3b1c62012-05-20 15:50:00 +0000330
Simon Glass918354b2014-10-29 13:08:57 -0600331 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen2b3b1c62012-05-20 15:50:00 +0000332 mask = 1 << pin;
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800333 writel(readl(&at91_port->schmitt) | mask,
334 &at91_port->schmitt);
Bo Shen2b3b1c62012-05-20 15:50:00 +0000335 }
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800336
Bo Shen2b3b1c62012-05-20 15:50:00 +0000337 return 0;
338}
339#endif
340
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100341/*
342 * enable/disable the multi-driver. This is only valid for output and
343 * allows the output pin to run as an open collector output.
344 */
345int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on)
346{
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800347 struct at91_port *at91_port = at91_pio_get_port(port);
348 u32 mask;
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100349
Simon Glass918354b2014-10-29 13:08:57 -0600350 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100351 mask = 1 << pin;
352 if (is_on)
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800353 writel(mask, &at91_port->mder);
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100354 else
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800355 writel(mask, &at91_port->mddr);
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100356 }
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800357
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100358 return 0;
359}
360
Simon Glass918354b2014-10-29 13:08:57 -0600361static void at91_set_port_value(struct at91_port *at91_port, int offset,
362 int value)
363{
364 u32 mask;
365
366 mask = 1 << offset;
367 if (value)
368 writel(mask, &at91_port->sodr);
369 else
370 writel(mask, &at91_port->codr);
371}
372
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100373/*
374 * assuming the pin is muxed as a gpio output, set its value.
375 */
376int at91_set_pio_value(unsigned port, unsigned pin, int value)
377{
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800378 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100379
Simon Glass918354b2014-10-29 13:08:57 -0600380 if (at91_port && (pin < GPIO_PER_BANK))
381 at91_set_port_value(at91_port, pin, value);
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800382
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100383 return 0;
384}
385
Simon Glass918354b2014-10-29 13:08:57 -0600386static int at91_get_port_value(struct at91_port *at91_port, int offset)
387{
388 u32 pdsr = 0, mask;
389
390 mask = 1 << offset;
391 pdsr = readl(&at91_port->pdsr) & mask;
392
393 return pdsr != 0;
394}
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100395/*
396 * read the pin's value (works even if it's not muxed as a gpio).
397 */
398int at91_get_pio_value(unsigned port, unsigned pin)
399{
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800400 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100401
Simon Glass918354b2014-10-29 13:08:57 -0600402 if (at91_port && (pin < GPIO_PER_BANK))
403 return at91_get_port_value(at91_port, pin);
Bo Shen4bc9b7a2013-08-22 15:24:40 +0800404
Simon Glass918354b2014-10-29 13:08:57 -0600405 return 0;
Jens Scharsigea8fbba2010-02-03 22:46:16 +0100406}
Bo Shen6edaea82013-08-13 14:38:31 +0800407
Simon Glass918354b2014-10-29 13:08:57 -0600408#ifndef CONFIG_DM_GPIO
Bo Shen6edaea82013-08-13 14:38:31 +0800409/* Common GPIO API */
410
Bo Shen6edaea82013-08-13 14:38:31 +0800411int gpio_request(unsigned gpio, const char *label)
412{
413 return 0;
414}
415
416int gpio_free(unsigned gpio)
417{
418 return 0;
419}
420
421int gpio_direction_input(unsigned gpio)
422{
423 at91_set_pio_input(at91_gpio_to_port(gpio),
424 at91_gpio_to_pin(gpio), 0);
425 return 0;
426}
427
428int gpio_direction_output(unsigned gpio, int value)
429{
430 at91_set_pio_output(at91_gpio_to_port(gpio),
431 at91_gpio_to_pin(gpio), value);
432 return 0;
433}
434
435int gpio_get_value(unsigned gpio)
436{
437 return at91_get_pio_value(at91_gpio_to_port(gpio),
438 at91_gpio_to_pin(gpio));
439}
440
441int gpio_set_value(unsigned gpio, int value)
442{
443 at91_set_pio_value(at91_gpio_to_port(gpio),
444 at91_gpio_to_pin(gpio), value);
445
446 return 0;
447}
Simon Glass918354b2014-10-29 13:08:57 -0600448#endif
449
450#ifdef CONFIG_DM_GPIO
451
452struct at91_port_priv {
453 struct at91_port *regs;
454};
455
456/* set GPIO pin 'gpio' as an input */
457static int at91_gpio_direction_input(struct udevice *dev, unsigned offset)
458{
Axel Lind8958212015-01-31 14:47:34 +0800459 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass918354b2014-10-29 13:08:57 -0600460
461 at91_set_port_input(port->regs, offset, 0);
462
463 return 0;
464}
465
466/* set GPIO pin 'gpio' as an output, with polarity 'value' */
467static int at91_gpio_direction_output(struct udevice *dev, unsigned offset,
468 int value)
469{
Axel Lind8958212015-01-31 14:47:34 +0800470 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass918354b2014-10-29 13:08:57 -0600471
472 at91_set_port_output(port->regs, offset, value);
473
474 return 0;
475}
476
477/* read GPIO IN value of pin 'gpio' */
478static int at91_gpio_get_value(struct udevice *dev, unsigned offset)
479{
Axel Lind8958212015-01-31 14:47:34 +0800480 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass918354b2014-10-29 13:08:57 -0600481
482 return at91_get_port_value(port->regs, offset);
483}
484
485/* write GPIO OUT value to pin 'gpio' */
486static int at91_gpio_set_value(struct udevice *dev, unsigned offset,
487 int value)
488{
Axel Lind8958212015-01-31 14:47:34 +0800489 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass918354b2014-10-29 13:08:57 -0600490
491 at91_set_port_value(port->regs, offset, value);
492
493 return 0;
494}
495
496static int at91_gpio_get_function(struct udevice *dev, unsigned offset)
497{
Axel Lind8958212015-01-31 14:47:34 +0800498 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass918354b2014-10-29 13:08:57 -0600499
500 /* GPIOF_FUNC is not implemented yet */
501 if (at91_get_port_output(port->regs, offset))
502 return GPIOF_OUTPUT;
503 else
504 return GPIOF_INPUT;
505}
506
507static const struct dm_gpio_ops gpio_at91_ops = {
508 .direction_input = at91_gpio_direction_input,
509 .direction_output = at91_gpio_direction_output,
510 .get_value = at91_gpio_get_value,
511 .set_value = at91_gpio_set_value,
512 .get_function = at91_gpio_get_function,
513};
514
515static int at91_gpio_probe(struct udevice *dev)
516{
517 struct at91_port_priv *port = dev_get_priv(dev);
518 struct at91_port_platdata *plat = dev_get_platdata(dev);
Simon Glasse564f052015-03-05 12:25:20 -0700519 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass918354b2014-10-29 13:08:57 -0600520
521 uc_priv->bank_name = plat->bank_name;
522 uc_priv->gpio_count = GPIO_PER_BANK;
523 port->regs = (struct at91_port *)plat->base_addr;
524
525 return 0;
526}
527
528U_BOOT_DRIVER(gpio_at91) = {
529 .name = "gpio_at91",
530 .id = UCLASS_GPIO,
531 .ops = &gpio_at91_ops,
532 .probe = at91_gpio_probe,
533 .priv_auto_alloc_size = sizeof(struct at91_port_priv),
534};
535#endif