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 <asm/sizes.h>
15 #include <asm/arch/hardware.h>
16 #include <asm/arch/at91_pio.h>
18 static struct at91_port *at91_pio_get_port(unsigned port)
22 return (struct at91_port *)ATMEL_BASE_PIOA;
24 return (struct at91_port *)ATMEL_BASE_PIOB;
26 return (struct at91_port *)ATMEL_BASE_PIOC;
27 #if (ATMEL_PIO_PORTS > 3)
29 return (struct at91_port *)ATMEL_BASE_PIOD;
30 #if (ATMEL_PIO_PORTS > 4)
32 return (struct at91_port *)ATMEL_BASE_PIOE;
40 int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup)
42 struct at91_port *at91_port = at91_pio_get_port(port);
45 if (at91_port && (pin < 32)) {
48 writel(1 << pin, &at91_port->puer);
50 writel(1 << pin, &at91_port->pudr);
51 writel(mask, &at91_port->per);
58 * mux the pin to the "GPIO" peripheral role.
60 int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup)
62 struct at91_port *at91_port = at91_pio_get_port(port);
65 if (at91_port && (pin < 32)) {
67 writel(mask, &at91_port->idr);
68 at91_set_pio_pullup(port, pin, use_pullup);
69 writel(mask, &at91_port->per);
76 * mux the pin to the "A" internal peripheral role.
78 int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup)
80 struct at91_port *at91_port = at91_pio_get_port(port);
83 if (at91_port && (pin < 32)) {
85 writel(mask, &at91_port->idr);
86 at91_set_pio_pullup(port, pin, use_pullup);
87 #if defined(CPU_HAS_PIO3)
88 writel(readl(&at91_port->abcdsr1) & ~mask,
90 writel(readl(&at91_port->abcdsr2) & ~mask,
93 writel(mask, &at91_port->asr);
95 writel(mask, &at91_port->pdr);
102 * mux the pin to the "B" internal peripheral role.
104 int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup)
106 struct at91_port *at91_port = at91_pio_get_port(port);
109 if (at91_port && (pin < 32)) {
111 writel(mask, &at91_port->idr);
112 at91_set_pio_pullup(port, pin, use_pullup);
113 #if defined(CPU_HAS_PIO3)
114 writel(readl(&at91_port->abcdsr1) | mask,
115 &at91_port->abcdsr1);
116 writel(readl(&at91_port->abcdsr2) & ~mask,
117 &at91_port->abcdsr2);
119 writel(mask, &at91_port->bsr);
121 writel(mask, &at91_port->pdr);
127 #if defined(CPU_HAS_PIO3)
129 * mux the pin to the "C" internal peripheral role.
131 int at91_set_c_periph(unsigned port, unsigned pin, int use_pullup)
133 struct at91_port *at91_port = at91_pio_get_port(port);
136 if (at91_port && (pin < 32)) {
138 writel(mask, &at91_port->idr);
139 at91_set_pio_pullup(port, pin, use_pullup);
140 writel(readl(&at91_port->abcdsr1) & ~mask,
141 &at91_port->abcdsr1);
142 writel(readl(&at91_port->abcdsr2) | mask,
143 &at91_port->abcdsr2);
144 writel(mask, &at91_port->pdr);
151 * mux the pin to the "D" internal peripheral role.
153 int at91_set_d_periph(unsigned port, unsigned pin, int use_pullup)
155 struct at91_port *at91_port = at91_pio_get_port(port);
158 if (at91_port && (pin < 32)) {
160 writel(mask, &at91_port->idr);
161 at91_set_pio_pullup(port, pin, use_pullup);
162 writel(readl(&at91_port->abcdsr1) | mask,
163 &at91_port->abcdsr1);
164 writel(readl(&at91_port->abcdsr2) | mask,
165 &at91_port->abcdsr2);
166 writel(mask, &at91_port->pdr);
174 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
175 * configure it for an input.
177 int at91_set_pio_input(unsigned port, u32 pin, int use_pullup)
179 struct at91_port *at91_port = at91_pio_get_port(port);
182 if (at91_port && (pin < 32)) {
184 writel(mask, &at91_port->idr);
185 at91_set_pio_pullup(port, pin, use_pullup);
186 writel(mask, &at91_port->odr);
187 writel(mask, &at91_port->per);
194 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
195 * and configure it for an output.
197 int at91_set_pio_output(unsigned port, u32 pin, int value)
199 struct at91_port *at91_port = at91_pio_get_port(port);
202 if ((port < ATMEL_PIO_PORTS) && (pin < 32)) {
204 writel(mask, &at91_port->idr);
205 writel(mask, &at91_port->pudr);
207 writel(mask, &at91_port->sodr);
209 writel(mask, &at91_port->codr);
210 writel(mask, &at91_port->oer);
211 writel(mask, &at91_port->per);
218 * enable/disable the glitch filter. mostly used with IRQ handling.
220 int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on)
222 struct at91_port *at91_port = at91_pio_get_port(port);
225 if (at91_port && (pin < 32)) {
228 #if defined(CPU_HAS_PIO3)
229 writel(mask, &at91_port->ifscdr);
231 writel(mask, &at91_port->ifer);
233 writel(mask, &at91_port->ifdr);
240 #if defined(CPU_HAS_PIO3)
242 * enable/disable the debounce filter.
244 int at91_set_pio_debounce(unsigned port, unsigned pin, int is_on, int div)
246 struct at91_port *at91_port = at91_pio_get_port(port);
249 if (at91_port && (pin < 32)) {
252 writel(mask, &at91_port->ifscer);
253 writel(div & PIO_SCDR_DIV, &at91_port->scdr);
254 writel(mask, &at91_port->ifer);
256 writel(mask, &at91_port->ifdr);
264 * enable/disable the pull-down.
265 * If pull-up already enabled while calling the function, we disable it.
267 int at91_set_pio_pulldown(unsigned port, unsigned pin, int is_on)
269 struct at91_port *at91_port = at91_pio_get_port(port);
272 if (at91_port && (pin < 32)) {
274 writel(mask, &at91_port->pudr);
276 writel(mask, &at91_port->ppder);
278 writel(mask, &at91_port->ppddr);
285 * disable Schmitt trigger
287 int at91_set_pio_disable_schmitt_trig(unsigned port, unsigned pin)
289 struct at91_port *at91_port = at91_pio_get_port(port);
292 if (at91_port && (pin < 32)) {
294 writel(readl(&at91_port->schmitt) | mask,
295 &at91_port->schmitt);
303 * enable/disable the multi-driver. This is only valid for output and
304 * allows the output pin to run as an open collector output.
306 int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on)
308 struct at91_port *at91_port = at91_pio_get_port(port);
311 if (at91_port && (pin < 32)) {
314 writel(mask, &at91_port->mder);
316 writel(mask, &at91_port->mddr);
323 * assuming the pin is muxed as a gpio output, set its value.
325 int at91_set_pio_value(unsigned port, unsigned pin, int value)
327 struct at91_port *at91_port = at91_pio_get_port(port);
330 if (at91_port && (pin < 32)) {
333 writel(mask, &at91_port->sodr);
335 writel(mask, &at91_port->codr);
342 * read the pin's value (works even if it's not muxed as a gpio).
344 int at91_get_pio_value(unsigned port, unsigned pin)
346 struct at91_port *at91_port = at91_pio_get_port(port);
349 if (at91_port && (pin < 32)) {
351 pdsr = readl(&at91_port->pdsr) & mask;
357 /* Common GPIO API */
359 #define at91_gpio_to_port(gpio) (gpio / 32)
360 #define at91_gpio_to_pin(gpio) (gpio % 32)
362 int gpio_request(unsigned gpio, const char *label)
367 int gpio_free(unsigned gpio)
372 int gpio_direction_input(unsigned gpio)
374 at91_set_pio_input(at91_gpio_to_port(gpio),
375 at91_gpio_to_pin(gpio), 0);
379 int gpio_direction_output(unsigned gpio, int value)
381 at91_set_pio_output(at91_gpio_to_port(gpio),
382 at91_gpio_to_pin(gpio), value);
386 int gpio_get_value(unsigned gpio)
388 return at91_get_pio_value(at91_gpio_to_port(gpio),
389 at91_gpio_to_pin(gpio));
392 int gpio_set_value(unsigned gpio, int value)
394 at91_set_pio_value(at91_gpio_to_port(gpio),
395 at91_gpio_to_pin(gpio), value);