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;
41 int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup)
43 struct at91_port *at91_port = at91_pio_get_port(port);
46 if (at91_port && (pin < 32)) {
49 writel(1 << pin, &at91_port->puer);
51 writel(1 << pin, &at91_port->pudr);
52 writel(mask, &at91_port->per);
59 * mux the pin to the "GPIO" peripheral role.
61 int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup)
63 struct at91_port *at91_port = at91_pio_get_port(port);
66 if (at91_port && (pin < 32)) {
68 writel(mask, &at91_port->idr);
69 at91_set_pio_pullup(port, pin, use_pullup);
70 writel(mask, &at91_port->per);
77 * mux the pin to the "A" internal peripheral role.
79 int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup)
81 struct at91_port *at91_port = at91_pio_get_port(port);
84 if (at91_port && (pin < 32)) {
86 writel(mask, &at91_port->idr);
87 at91_set_pio_pullup(port, pin, use_pullup);
88 #if defined(CPU_HAS_PIO3)
89 writel(readl(&at91_port->abcdsr1) & ~mask,
91 writel(readl(&at91_port->abcdsr2) & ~mask,
94 writel(mask, &at91_port->asr);
96 writel(mask, &at91_port->pdr);
103 * mux the pin to the "B" internal peripheral role.
105 int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup)
107 struct at91_port *at91_port = at91_pio_get_port(port);
110 if (at91_port && (pin < 32)) {
112 writel(mask, &at91_port->idr);
113 at91_set_pio_pullup(port, pin, use_pullup);
114 #if defined(CPU_HAS_PIO3)
115 writel(readl(&at91_port->abcdsr1) | mask,
116 &at91_port->abcdsr1);
117 writel(readl(&at91_port->abcdsr2) & ~mask,
118 &at91_port->abcdsr2);
120 writel(mask, &at91_port->bsr);
122 writel(mask, &at91_port->pdr);
128 #if defined(CPU_HAS_PIO3)
130 * mux the pin to the "C" internal peripheral role.
132 int at91_set_c_periph(unsigned port, unsigned pin, int use_pullup)
134 struct at91_port *at91_port = at91_pio_get_port(port);
137 if (at91_port && (pin < 32)) {
139 writel(mask, &at91_port->idr);
140 at91_set_pio_pullup(port, pin, use_pullup);
141 writel(readl(&at91_port->abcdsr1) & ~mask,
142 &at91_port->abcdsr1);
143 writel(readl(&at91_port->abcdsr2) | mask,
144 &at91_port->abcdsr2);
145 writel(mask, &at91_port->pdr);
152 * mux the pin to the "D" internal peripheral role.
154 int at91_set_d_periph(unsigned port, unsigned pin, int use_pullup)
156 struct at91_port *at91_port = at91_pio_get_port(port);
159 if (at91_port && (pin < 32)) {
161 writel(mask, &at91_port->idr);
162 at91_set_pio_pullup(port, pin, use_pullup);
163 writel(readl(&at91_port->abcdsr1) | mask,
164 &at91_port->abcdsr1);
165 writel(readl(&at91_port->abcdsr2) | mask,
166 &at91_port->abcdsr2);
167 writel(mask, &at91_port->pdr);
175 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
176 * configure it for an input.
178 int at91_set_pio_input(unsigned port, u32 pin, int use_pullup)
180 struct at91_port *at91_port = at91_pio_get_port(port);
183 if (at91_port && (pin < 32)) {
185 writel(mask, &at91_port->idr);
186 at91_set_pio_pullup(port, pin, use_pullup);
187 writel(mask, &at91_port->odr);
188 writel(mask, &at91_port->per);
195 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
196 * and configure it for an output.
198 int at91_set_pio_output(unsigned port, u32 pin, int value)
200 struct at91_port *at91_port = at91_pio_get_port(port);
203 if ((port < ATMEL_PIO_PORTS) && (pin < 32)) {
205 writel(mask, &at91_port->idr);
206 writel(mask, &at91_port->pudr);
208 writel(mask, &at91_port->sodr);
210 writel(mask, &at91_port->codr);
211 writel(mask, &at91_port->oer);
212 writel(mask, &at91_port->per);
219 * enable/disable the glitch filter. mostly used with IRQ handling.
221 int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on)
223 struct at91_port *at91_port = at91_pio_get_port(port);
226 if (at91_port && (pin < 32)) {
229 #if defined(CPU_HAS_PIO3)
230 writel(mask, &at91_port->ifscdr);
232 writel(mask, &at91_port->ifer);
234 writel(mask, &at91_port->ifdr);
241 #if defined(CPU_HAS_PIO3)
243 * enable/disable the debounce filter.
245 int at91_set_pio_debounce(unsigned port, unsigned pin, int is_on, int div)
247 struct at91_port *at91_port = at91_pio_get_port(port);
250 if (at91_port && (pin < 32)) {
253 writel(mask, &at91_port->ifscer);
254 writel(div & PIO_SCDR_DIV, &at91_port->scdr);
255 writel(mask, &at91_port->ifer);
257 writel(mask, &at91_port->ifdr);
265 * enable/disable the pull-down.
266 * If pull-up already enabled while calling the function, we disable it.
268 int at91_set_pio_pulldown(unsigned port, unsigned pin, int is_on)
270 struct at91_port *at91_port = at91_pio_get_port(port);
273 if (at91_port && (pin < 32)) {
275 writel(mask, &at91_port->pudr);
277 writel(mask, &at91_port->ppder);
279 writel(mask, &at91_port->ppddr);
286 * disable Schmitt trigger
288 int at91_set_pio_disable_schmitt_trig(unsigned port, unsigned pin)
290 struct at91_port *at91_port = at91_pio_get_port(port);
293 if (at91_port && (pin < 32)) {
295 writel(readl(&at91_port->schmitt) | mask,
296 &at91_port->schmitt);
304 * enable/disable the multi-driver. This is only valid for output and
305 * allows the output pin to run as an open collector output.
307 int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on)
309 struct at91_port *at91_port = at91_pio_get_port(port);
312 if (at91_port && (pin < 32)) {
315 writel(mask, &at91_port->mder);
317 writel(mask, &at91_port->mddr);
324 * assuming the pin is muxed as a gpio output, set its value.
326 int at91_set_pio_value(unsigned port, unsigned pin, int value)
328 struct at91_port *at91_port = at91_pio_get_port(port);
331 if (at91_port && (pin < 32)) {
334 writel(mask, &at91_port->sodr);
336 writel(mask, &at91_port->codr);
343 * read the pin's value (works even if it's not muxed as a gpio).
345 int at91_get_pio_value(unsigned port, unsigned pin)
347 struct at91_port *at91_port = at91_pio_get_port(port);
350 if (at91_port && (pin < 32)) {
352 pdsr = readl(&at91_port->pdsr) & mask;
358 /* Common GPIO API */
360 int gpio_request(unsigned gpio, const char *label)
365 int gpio_free(unsigned gpio)
370 int gpio_direction_input(unsigned gpio)
372 at91_set_pio_input(at91_gpio_to_port(gpio),
373 at91_gpio_to_pin(gpio), 0);
377 int gpio_direction_output(unsigned gpio, int value)
379 at91_set_pio_output(at91_gpio_to_port(gpio),
380 at91_gpio_to_pin(gpio), value);
384 int gpio_get_value(unsigned gpio)
386 return at91_get_pio_value(at91_gpio_to_port(gpio),
387 at91_gpio_to_pin(gpio));
390 int gpio_set_value(unsigned gpio, int value)
392 at91_set_pio_value(at91_gpio_to_port(gpio),
393 at91_gpio_to_pin(gpio), value);