2 * Copyright (C) 2013 Bo Shen <voice.shen@atmel.com>
4 * Copyright (C) 2009 Jens Scharsig (js_at_ng@scharsoft.de)
6 * Copyright (C) 2005 HP Labs
8 * SPDX-License-Identifier: GPL-2.0+
14 #include <linux/sizes.h>
15 #include <asm/arch/hardware.h>
16 #include <asm/arch/at91_pio.h>
17 #include <asm/arch/gpio.h>
19 static struct at91_port *at91_pio_get_port(unsigned port)
23 return (struct at91_port *)ATMEL_BASE_PIOA;
25 return (struct at91_port *)ATMEL_BASE_PIOB;
27 return (struct at91_port *)ATMEL_BASE_PIOC;
28 #if (ATMEL_PIO_PORTS > 3)
30 return (struct at91_port *)ATMEL_BASE_PIOD;
31 #if (ATMEL_PIO_PORTS > 4)
33 return (struct at91_port *)ATMEL_BASE_PIOE;
37 printf("Error: at91_gpio: Fail to get PIO base!\n");
42 int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup)
44 struct at91_port *at91_port = at91_pio_get_port(port);
47 if (at91_port && (pin < 32)) {
50 writel(1 << pin, &at91_port->puer);
52 writel(1 << pin, &at91_port->pudr);
53 writel(mask, &at91_port->per);
60 * mux the pin to the "GPIO" peripheral role.
62 int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup)
64 struct at91_port *at91_port = at91_pio_get_port(port);
67 if (at91_port && (pin < 32)) {
69 writel(mask, &at91_port->idr);
70 at91_set_pio_pullup(port, pin, use_pullup);
71 writel(mask, &at91_port->per);
78 * mux the pin to the "A" internal peripheral role.
80 int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup)
82 struct at91_port *at91_port = at91_pio_get_port(port);
85 if (at91_port && (pin < 32)) {
87 writel(mask, &at91_port->idr);
88 at91_set_pio_pullup(port, pin, use_pullup);
89 #if defined(CPU_HAS_PIO3)
90 writel(readl(&at91_port->abcdsr1) & ~mask,
92 writel(readl(&at91_port->abcdsr2) & ~mask,
95 writel(mask, &at91_port->asr);
97 writel(mask, &at91_port->pdr);
104 * mux the pin to the "B" internal peripheral role.
106 int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup)
108 struct at91_port *at91_port = at91_pio_get_port(port);
111 if (at91_port && (pin < 32)) {
113 writel(mask, &at91_port->idr);
114 at91_set_pio_pullup(port, pin, use_pullup);
115 #if defined(CPU_HAS_PIO3)
116 writel(readl(&at91_port->abcdsr1) | mask,
117 &at91_port->abcdsr1);
118 writel(readl(&at91_port->abcdsr2) & ~mask,
119 &at91_port->abcdsr2);
121 writel(mask, &at91_port->bsr);
123 writel(mask, &at91_port->pdr);
129 #if defined(CPU_HAS_PIO3)
131 * mux the pin to the "C" internal peripheral role.
133 int at91_set_c_periph(unsigned port, unsigned pin, int use_pullup)
135 struct at91_port *at91_port = at91_pio_get_port(port);
138 if (at91_port && (pin < 32)) {
140 writel(mask, &at91_port->idr);
141 at91_set_pio_pullup(port, pin, use_pullup);
142 writel(readl(&at91_port->abcdsr1) & ~mask,
143 &at91_port->abcdsr1);
144 writel(readl(&at91_port->abcdsr2) | mask,
145 &at91_port->abcdsr2);
146 writel(mask, &at91_port->pdr);
153 * mux the pin to the "D" internal peripheral role.
155 int at91_set_d_periph(unsigned port, unsigned pin, int use_pullup)
157 struct at91_port *at91_port = at91_pio_get_port(port);
160 if (at91_port && (pin < 32)) {
162 writel(mask, &at91_port->idr);
163 at91_set_pio_pullup(port, pin, use_pullup);
164 writel(readl(&at91_port->abcdsr1) | mask,
165 &at91_port->abcdsr1);
166 writel(readl(&at91_port->abcdsr2) | mask,
167 &at91_port->abcdsr2);
168 writel(mask, &at91_port->pdr);
176 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
177 * configure it for an input.
179 int at91_set_pio_input(unsigned port, u32 pin, int use_pullup)
181 struct at91_port *at91_port = at91_pio_get_port(port);
184 if (at91_port && (pin < 32)) {
186 writel(mask, &at91_port->idr);
187 at91_set_pio_pullup(port, pin, use_pullup);
188 writel(mask, &at91_port->odr);
189 writel(mask, &at91_port->per);
196 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
197 * and configure it for an output.
199 int at91_set_pio_output(unsigned port, u32 pin, int value)
201 struct at91_port *at91_port = at91_pio_get_port(port);
204 if (at91_port && (port < ATMEL_PIO_PORTS) && (pin < 32)) {
206 writel(mask, &at91_port->idr);
207 writel(mask, &at91_port->pudr);
209 writel(mask, &at91_port->sodr);
211 writel(mask, &at91_port->codr);
212 writel(mask, &at91_port->oer);
213 writel(mask, &at91_port->per);
220 * enable/disable the glitch filter. mostly used with IRQ handling.
222 int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on)
224 struct at91_port *at91_port = at91_pio_get_port(port);
227 if (at91_port && (pin < 32)) {
230 #if defined(CPU_HAS_PIO3)
231 writel(mask, &at91_port->ifscdr);
233 writel(mask, &at91_port->ifer);
235 writel(mask, &at91_port->ifdr);
242 #if defined(CPU_HAS_PIO3)
244 * enable/disable the debounce filter.
246 int at91_set_pio_debounce(unsigned port, unsigned pin, int is_on, int div)
248 struct at91_port *at91_port = at91_pio_get_port(port);
251 if (at91_port && (pin < 32)) {
254 writel(mask, &at91_port->ifscer);
255 writel(div & PIO_SCDR_DIV, &at91_port->scdr);
256 writel(mask, &at91_port->ifer);
258 writel(mask, &at91_port->ifdr);
266 * enable/disable the pull-down.
267 * If pull-up already enabled while calling the function, we disable it.
269 int at91_set_pio_pulldown(unsigned port, unsigned pin, int is_on)
271 struct at91_port *at91_port = at91_pio_get_port(port);
274 if (at91_port && (pin < 32)) {
276 writel(mask, &at91_port->pudr);
278 writel(mask, &at91_port->ppder);
280 writel(mask, &at91_port->ppddr);
287 * disable Schmitt trigger
289 int at91_set_pio_disable_schmitt_trig(unsigned port, unsigned pin)
291 struct at91_port *at91_port = at91_pio_get_port(port);
294 if (at91_port && (pin < 32)) {
296 writel(readl(&at91_port->schmitt) | mask,
297 &at91_port->schmitt);
305 * enable/disable the multi-driver. This is only valid for output and
306 * allows the output pin to run as an open collector output.
308 int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on)
310 struct at91_port *at91_port = at91_pio_get_port(port);
313 if (at91_port && (pin < 32)) {
316 writel(mask, &at91_port->mder);
318 writel(mask, &at91_port->mddr);
325 * assuming the pin is muxed as a gpio output, set its value.
327 int at91_set_pio_value(unsigned port, unsigned pin, int value)
329 struct at91_port *at91_port = at91_pio_get_port(port);
332 if (at91_port && (pin < 32)) {
335 writel(mask, &at91_port->sodr);
337 writel(mask, &at91_port->codr);
344 * read the pin's value (works even if it's not muxed as a gpio).
346 int at91_get_pio_value(unsigned port, unsigned pin)
348 struct at91_port *at91_port = at91_pio_get_port(port);
351 if (at91_port && (pin < 32)) {
353 pdsr = readl(&at91_port->pdsr) & mask;
359 /* Common GPIO API */
361 int gpio_request(unsigned gpio, const char *label)
366 int gpio_free(unsigned gpio)
371 int gpio_direction_input(unsigned gpio)
373 at91_set_pio_input(at91_gpio_to_port(gpio),
374 at91_gpio_to_pin(gpio), 0);
378 int gpio_direction_output(unsigned gpio, int value)
380 at91_set_pio_output(at91_gpio_to_port(gpio),
381 at91_gpio_to_pin(gpio), value);
385 int gpio_get_value(unsigned gpio)
387 return at91_get_pio_value(at91_gpio_to_port(gpio),
388 at91_gpio_to_pin(gpio));
391 int gpio_set_value(unsigned gpio, int value)
393 at91_set_pio_value(at91_gpio_to_port(gpio),
394 at91_gpio_to_pin(gpio), value);