blob: 8e52e3dad0af181e0df608466134ea3ba0660bef [file] [log] [blame]
Jens Scharsig8d065462010-02-03 22:46:16 +01001/*
Bo Shenf0adeaa2013-08-13 14:38:32 +08002 * Copyright (C) 2013 Bo Shen <voice.shen@atmel.com>
Jens Scharsig8d065462010-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 Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Jens Scharsig8d065462010-02-03 22:46:16 +01009 */
10
11#include <config.h>
12#include <common.h>
Simon Glass4d717882014-10-29 13:08:57 -060013#include <dm.h>
Reinhard Meyerb06208c2010-11-07 13:26:14 +010014#include <asm/io.h>
Alexey Brodkin267d8e22014-02-26 17:47:58 +040015#include <linux/sizes.h>
Simon Glass4d717882014-10-29 13:08:57 -060016#include <asm/gpio.h>
Jens Scharsig8d065462010-02-03 22:46:16 +010017#include <asm/arch/hardware.h>
Jens Scharsig8d065462010-02-03 22:46:16 +010018#include <asm/arch/at91_pio.h>
Simon Glass4d717882014-10-29 13:08:57 -060019
20#define GPIO_PER_BANK 32
Jens Scharsig8d065462010-02-03 22:46:16 +010021
Bo Shen02d88142013-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, Josh383543a2014-05-07 16:50:45 +080040 printf("Error: at91_gpio: Fail to get PIO base!\n");
Bo Shen02d88142013-08-22 15:24:40 +080041 return NULL;
42 }
43}
44
Simon Glass4d717882014-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 Scharsig8d065462010-02-03 22:46:16 +010058int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup)
59{
Bo Shen02d88142013-08-22 15:24:40 +080060 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsig8d065462010-02-03 22:46:16 +010061
Marek Vasutba91bd52016-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 Glass4d717882014-10-29 13:08:57 -060067 if (at91_port && (pin < GPIO_PER_BANK))
68 at91_set_port_pullup(at91_port, pin, use_pullup);
Bo Shen02d88142013-08-22 15:24:40 +080069
Jens Scharsig8d065462010-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 Shen02d88142013-08-22 15:24:40 +080078 struct at91_port *at91_port = at91_pio_get_port(port);
79 u32 mask;
Jens Scharsig8d065462010-02-03 22:46:16 +010080
Simon Glass4d717882014-10-29 13:08:57 -060081 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsig8d065462010-02-03 22:46:16 +010082 mask = 1 << pin;
Bo Shen02d88142013-08-22 15:24:40 +080083 writel(mask, &at91_port->idr);
Jens Scharsig8d065462010-02-03 22:46:16 +010084 at91_set_pio_pullup(port, pin, use_pullup);
Bo Shen02d88142013-08-22 15:24:40 +080085 writel(mask, &at91_port->per);
Jens Scharsig8d065462010-02-03 22:46:16 +010086 }
Bo Shen02d88142013-08-22 15:24:40 +080087
Jens Scharsig8d065462010-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 Shen02d88142013-08-22 15:24:40 +080096 struct at91_port *at91_port = at91_pio_get_port(port);
97 u32 mask;
Jens Scharsig8d065462010-02-03 22:46:16 +010098
Simon Glass4d717882014-10-29 13:08:57 -060099 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsig8d065462010-02-03 22:46:16 +0100100 mask = 1 << pin;
Bo Shen02d88142013-08-22 15:24:40 +0800101 writel(mask, &at91_port->idr);
Jens Scharsig8d065462010-02-03 22:46:16 +0100102 at91_set_pio_pullup(port, pin, use_pullup);
Bo Shen0ac13452012-05-20 15:50:00 +0000103#if defined(CPU_HAS_PIO3)
Bo Shen02d88142013-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 Shen0ac13452012-05-20 15:50:00 +0000108#else
Bo Shen02d88142013-08-22 15:24:40 +0800109 writel(mask, &at91_port->asr);
Bo Shen0ac13452012-05-20 15:50:00 +0000110#endif
Bo Shen02d88142013-08-22 15:24:40 +0800111 writel(mask, &at91_port->pdr);
Jens Scharsig8d065462010-02-03 22:46:16 +0100112 }
Bo Shen02d88142013-08-22 15:24:40 +0800113
Jens Scharsig8d065462010-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 Shen02d88142013-08-22 15:24:40 +0800122 struct at91_port *at91_port = at91_pio_get_port(port);
123 u32 mask;
Jens Scharsig8d065462010-02-03 22:46:16 +0100124
Simon Glass4d717882014-10-29 13:08:57 -0600125 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsig8d065462010-02-03 22:46:16 +0100126 mask = 1 << pin;
Bo Shen02d88142013-08-22 15:24:40 +0800127 writel(mask, &at91_port->idr);
Jens Scharsig8d065462010-02-03 22:46:16 +0100128 at91_set_pio_pullup(port, pin, use_pullup);
Bo Shen0ac13452012-05-20 15:50:00 +0000129#if defined(CPU_HAS_PIO3)
Bo Shen02d88142013-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 Shen0ac13452012-05-20 15:50:00 +0000134#else
Bo Shen02d88142013-08-22 15:24:40 +0800135 writel(mask, &at91_port->bsr);
Bo Shen0ac13452012-05-20 15:50:00 +0000136#endif
Bo Shen02d88142013-08-22 15:24:40 +0800137 writel(mask, &at91_port->pdr);
Jens Scharsig8d065462010-02-03 22:46:16 +0100138 }
Bo Shen02d88142013-08-22 15:24:40 +0800139
Jens Scharsig8d065462010-02-03 22:46:16 +0100140 return 0;
141}
142
Bo Shen0ac13452012-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 Shen02d88142013-08-22 15:24:40 +0800149 struct at91_port *at91_port = at91_pio_get_port(port);
150 u32 mask;
Bo Shen0ac13452012-05-20 15:50:00 +0000151
Simon Glass4d717882014-10-29 13:08:57 -0600152 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen0ac13452012-05-20 15:50:00 +0000153 mask = 1 << pin;
Bo Shen02d88142013-08-22 15:24:40 +0800154 writel(mask, &at91_port->idr);
Bo Shen0ac13452012-05-20 15:50:00 +0000155 at91_set_pio_pullup(port, pin, use_pullup);
Bo Shen02d88142013-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 Shen0ac13452012-05-20 15:50:00 +0000161 }
Bo Shen02d88142013-08-22 15:24:40 +0800162
Bo Shen0ac13452012-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 Shen02d88142013-08-22 15:24:40 +0800171 struct at91_port *at91_port = at91_pio_get_port(port);
172 u32 mask;
Bo Shen0ac13452012-05-20 15:50:00 +0000173
Simon Glass4d717882014-10-29 13:08:57 -0600174 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen0ac13452012-05-20 15:50:00 +0000175 mask = 1 << pin;
Bo Shen02d88142013-08-22 15:24:40 +0800176 writel(mask, &at91_port->idr);
Bo Shen0ac13452012-05-20 15:50:00 +0000177 at91_set_pio_pullup(port, pin, use_pullup);
Bo Shen02d88142013-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 Shen0ac13452012-05-20 15:50:00 +0000183 }
Bo Shen02d88142013-08-22 15:24:40 +0800184
Bo Shen0ac13452012-05-20 15:50:00 +0000185 return 0;
186}
187#endif
188
Simon Glass4d717882014-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 Scharsig8d065462010-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 Shen02d88142013-08-22 15:24:40 +0800218 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsig8d065462010-02-03 22:46:16 +0100219
Simon Glass4d717882014-10-29 13:08:57 -0600220 if (at91_port && (pin < GPIO_PER_BANK))
221 at91_set_port_input(at91_port, pin, use_pullup);
Bo Shen02d88142013-08-22 15:24:40 +0800222
Jens Scharsig8d065462010-02-03 22:46:16 +0100223 return 0;
224}
225
Simon Glass4d717882014-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 Scharsig8d065462010-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 Shen02d88142013-08-22 15:24:40 +0800248 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsig8d065462010-02-03 22:46:16 +0100249
Simon Glass4d717882014-10-29 13:08:57 -0600250 if (at91_port && (pin < GPIO_PER_BANK))
251 at91_set_port_output(at91_port, pin, value);
Bo Shen02d88142013-08-22 15:24:40 +0800252
Jens Scharsig8d065462010-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 Shen02d88142013-08-22 15:24:40 +0800261 struct at91_port *at91_port = at91_pio_get_port(port);
262 u32 mask;
Jens Scharsig8d065462010-02-03 22:46:16 +0100263
Simon Glass4d717882014-10-29 13:08:57 -0600264 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsig8d065462010-02-03 22:46:16 +0100265 mask = 1 << pin;
Bo Shen0ac13452012-05-20 15:50:00 +0000266 if (is_on) {
267#if defined(CPU_HAS_PIO3)
Bo Shen02d88142013-08-22 15:24:40 +0800268 writel(mask, &at91_port->ifscdr);
Bo Shen0ac13452012-05-20 15:50:00 +0000269#endif
Bo Shen02d88142013-08-22 15:24:40 +0800270 writel(mask, &at91_port->ifer);
Bo Shen0ac13452012-05-20 15:50:00 +0000271 } else {
Bo Shen02d88142013-08-22 15:24:40 +0800272 writel(mask, &at91_port->ifdr);
Bo Shen0ac13452012-05-20 15:50:00 +0000273 }
274 }
Bo Shen02d88142013-08-22 15:24:40 +0800275
Bo Shen0ac13452012-05-20 15:50:00 +0000276 return 0;
277}
278
279#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 Shen02d88142013-08-22 15:24:40 +0800285 struct at91_port *at91_port = at91_pio_get_port(port);
286 u32 mask;
Bo Shen0ac13452012-05-20 15:50:00 +0000287
Simon Glass4d717882014-10-29 13:08:57 -0600288 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen0ac13452012-05-20 15:50:00 +0000289 mask = 1 << pin;
290 if (is_on) {
Bo Shen02d88142013-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 Shen0ac13452012-05-20 15:50:00 +0000294 } else {
Bo Shen02d88142013-08-22 15:24:40 +0800295 writel(mask, &at91_port->ifdr);
Bo Shen0ac13452012-05-20 15:50:00 +0000296 }
297 }
Bo Shen02d88142013-08-22 15:24:40 +0800298
Bo Shen0ac13452012-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 Shen02d88142013-08-22 15:24:40 +0800308 struct at91_port *at91_port = at91_pio_get_port(port);
309 u32 mask;
Bo Shen0ac13452012-05-20 15:50:00 +0000310
Simon Glass4d717882014-10-29 13:08:57 -0600311 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen0ac13452012-05-20 15:50:00 +0000312 mask = 1 << pin;
Marek Vasutba91bd52016-05-04 23:05:23 +0200313 if (is_on) {
314 at91_set_pio_pullup(port, pin, 0);
Bo Shen02d88142013-08-22 15:24:40 +0800315 writel(mask, &at91_port->ppder);
Marek Vasutba91bd52016-05-04 23:05:23 +0200316 } else
Bo Shen02d88142013-08-22 15:24:40 +0800317 writel(mask, &at91_port->ppddr);
Bo Shen0ac13452012-05-20 15:50:00 +0000318 }
Bo Shen02d88142013-08-22 15:24:40 +0800319
Bo Shen0ac13452012-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 Shen02d88142013-08-22 15:24:40 +0800328 struct at91_port *at91_port = at91_pio_get_port(port);
329 u32 mask;
Bo Shen0ac13452012-05-20 15:50:00 +0000330
Simon Glass4d717882014-10-29 13:08:57 -0600331 if (at91_port && (pin < GPIO_PER_BANK)) {
Bo Shen0ac13452012-05-20 15:50:00 +0000332 mask = 1 << pin;
Bo Shen02d88142013-08-22 15:24:40 +0800333 writel(readl(&at91_port->schmitt) | mask,
334 &at91_port->schmitt);
Jens Scharsig8d065462010-02-03 22:46:16 +0100335 }
Bo Shen02d88142013-08-22 15:24:40 +0800336
Jens Scharsig8d065462010-02-03 22:46:16 +0100337 return 0;
338}
Bo Shen0ac13452012-05-20 15:50:00 +0000339#endif
Jens Scharsig8d065462010-02-03 22:46:16 +0100340
341/*
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 Shen02d88142013-08-22 15:24:40 +0800347 struct at91_port *at91_port = at91_pio_get_port(port);
348 u32 mask;
Jens Scharsig8d065462010-02-03 22:46:16 +0100349
Simon Glass4d717882014-10-29 13:08:57 -0600350 if (at91_port && (pin < GPIO_PER_BANK)) {
Jens Scharsig8d065462010-02-03 22:46:16 +0100351 mask = 1 << pin;
352 if (is_on)
Bo Shen02d88142013-08-22 15:24:40 +0800353 writel(mask, &at91_port->mder);
Jens Scharsig8d065462010-02-03 22:46:16 +0100354 else
Bo Shen02d88142013-08-22 15:24:40 +0800355 writel(mask, &at91_port->mddr);
Jens Scharsig8d065462010-02-03 22:46:16 +0100356 }
Bo Shen02d88142013-08-22 15:24:40 +0800357
Jens Scharsig8d065462010-02-03 22:46:16 +0100358 return 0;
359}
360
Simon Glass4d717882014-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 Scharsig8d065462010-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 Shen02d88142013-08-22 15:24:40 +0800378 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsig8d065462010-02-03 22:46:16 +0100379
Simon Glass4d717882014-10-29 13:08:57 -0600380 if (at91_port && (pin < GPIO_PER_BANK))
381 at91_set_port_value(at91_port, pin, value);
Bo Shen02d88142013-08-22 15:24:40 +0800382
Jens Scharsig8d065462010-02-03 22:46:16 +0100383 return 0;
384}
385
Simon Glass4d717882014-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 Scharsig8d065462010-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 Shen02d88142013-08-22 15:24:40 +0800400 struct at91_port *at91_port = at91_pio_get_port(port);
Jens Scharsig8d065462010-02-03 22:46:16 +0100401
Simon Glass4d717882014-10-29 13:08:57 -0600402 if (at91_port && (pin < GPIO_PER_BANK))
403 return at91_get_port_value(at91_port, pin);
Bo Shen02d88142013-08-22 15:24:40 +0800404
Simon Glass4d717882014-10-29 13:08:57 -0600405 return 0;
Jens Scharsig8d065462010-02-03 22:46:16 +0100406}
Bo Shenad1d2ac2013-08-13 14:38:31 +0800407
Simon Glass4d717882014-10-29 13:08:57 -0600408#ifndef CONFIG_DM_GPIO
Bo Shenad1d2ac2013-08-13 14:38:31 +0800409/* Common GPIO API */
410
Bo Shenad1d2ac2013-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 Glass4d717882014-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 Lin98b9b372015-01-31 14:47:34 +0800459 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass4d717882014-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 Lin98b9b372015-01-31 14:47:34 +0800470 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass4d717882014-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 Lin98b9b372015-01-31 14:47:34 +0800480 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass4d717882014-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 Lin98b9b372015-01-31 14:47:34 +0800489 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass4d717882014-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 Lin98b9b372015-01-31 14:47:34 +0800498 struct at91_port_priv *port = dev_get_priv(dev);
Simon Glass4d717882014-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 Glassde0977b2015-03-05 12:25:20 -0700519 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass4d717882014-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