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+
15 #include <linux/sizes.h>
17 #include <asm/arch/hardware.h>
18 #include <asm/arch/at91_pio.h>
20 #define GPIO_PER_BANK 32
22 static struct at91_port *at91_pio_get_port(unsigned port)
26 return (struct at91_port *)ATMEL_BASE_PIOA;
28 return (struct at91_port *)ATMEL_BASE_PIOB;
30 return (struct at91_port *)ATMEL_BASE_PIOC;
31 #if (ATMEL_PIO_PORTS > 3)
33 return (struct at91_port *)ATMEL_BASE_PIOD;
34 #if (ATMEL_PIO_PORTS > 4)
36 return (struct at91_port *)ATMEL_BASE_PIOE;
40 printf("Error: at91_gpio: Fail to get PIO base!\n");
45 static void at91_set_port_pullup(struct at91_port *at91_port, unsigned offset,
52 writel(mask, &at91_port->puer);
54 writel(mask, &at91_port->pudr);
55 writel(mask, &at91_port->per);
58 int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup)
60 struct at91_port *at91_port = at91_pio_get_port(port);
62 #if defined(CPU_HAS_PIO3)
64 at91_set_pio_pulldown(port, pin, 0);
67 if (at91_port && (pin < GPIO_PER_BANK))
68 at91_set_port_pullup(at91_port, pin, use_pullup);
74 * mux the pin to the "GPIO" peripheral role.
76 int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup)
78 struct at91_port *at91_port = at91_pio_get_port(port);
81 if (at91_port && (pin < GPIO_PER_BANK)) {
83 writel(mask, &at91_port->idr);
84 at91_set_pio_pullup(port, pin, use_pullup);
85 writel(mask, &at91_port->per);
92 * mux the pin to the "A" internal peripheral role.
94 int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup)
96 struct at91_port *at91_port = at91_pio_get_port(port);
99 if (at91_port && (pin < GPIO_PER_BANK)) {
101 writel(mask, &at91_port->idr);
102 at91_set_pio_pullup(port, pin, use_pullup);
103 #if defined(CPU_HAS_PIO3)
104 writel(readl(&at91_port->abcdsr1) & ~mask,
105 &at91_port->abcdsr1);
106 writel(readl(&at91_port->abcdsr2) & ~mask,
107 &at91_port->abcdsr2);
109 writel(mask, &at91_port->asr);
111 writel(mask, &at91_port->pdr);
118 * mux the pin to the "B" internal peripheral role.
120 int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup)
122 struct at91_port *at91_port = at91_pio_get_port(port);
125 if (at91_port && (pin < GPIO_PER_BANK)) {
127 writel(mask, &at91_port->idr);
128 at91_set_pio_pullup(port, pin, use_pullup);
129 #if defined(CPU_HAS_PIO3)
130 writel(readl(&at91_port->abcdsr1) | mask,
131 &at91_port->abcdsr1);
132 writel(readl(&at91_port->abcdsr2) & ~mask,
133 &at91_port->abcdsr2);
135 writel(mask, &at91_port->bsr);
137 writel(mask, &at91_port->pdr);
143 #if defined(CPU_HAS_PIO3)
145 * mux the pin to the "C" internal peripheral role.
147 int at91_set_c_periph(unsigned port, unsigned pin, int use_pullup)
149 struct at91_port *at91_port = at91_pio_get_port(port);
152 if (at91_port && (pin < GPIO_PER_BANK)) {
154 writel(mask, &at91_port->idr);
155 at91_set_pio_pullup(port, pin, use_pullup);
156 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);
167 * mux the pin to the "D" internal peripheral role.
169 int at91_set_d_periph(unsigned port, unsigned pin, int use_pullup)
171 struct at91_port *at91_port = at91_pio_get_port(port);
174 if (at91_port && (pin < GPIO_PER_BANK)) {
176 writel(mask, &at91_port->idr);
177 at91_set_pio_pullup(port, pin, use_pullup);
178 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);
189 #ifdef CONFIG_DM_GPIO
190 static bool at91_get_port_output(struct at91_port *at91_port, int offset)
195 val = readl(&at91_port->osr);
200 static void at91_set_port_input(struct at91_port *at91_port, int 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);
213 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
214 * configure it for an input.
216 int at91_set_pio_input(unsigned port, u32 pin, int use_pullup)
218 struct at91_port *at91_port = at91_pio_get_port(port);
220 if (at91_port && (pin < GPIO_PER_BANK))
221 at91_set_port_input(at91_port, pin, use_pullup);
226 static void at91_set_port_output(struct at91_port *at91_port, int offset,
232 writel(mask, &at91_port->idr);
233 writel(mask, &at91_port->pudr);
235 writel(mask, &at91_port->sodr);
237 writel(mask, &at91_port->codr);
238 writel(mask, &at91_port->oer);
239 writel(mask, &at91_port->per);
243 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
244 * and configure it for an output.
246 int at91_set_pio_output(unsigned port, u32 pin, int value)
248 struct at91_port *at91_port = at91_pio_get_port(port);
250 if (at91_port && (pin < GPIO_PER_BANK))
251 at91_set_port_output(at91_port, pin, value);
257 * enable/disable the glitch filter. mostly used with IRQ handling.
259 int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on)
261 struct at91_port *at91_port = at91_pio_get_port(port);
264 if (at91_port && (pin < GPIO_PER_BANK)) {
267 #if defined(CPU_HAS_PIO3)
268 writel(mask, &at91_port->ifscdr);
270 writel(mask, &at91_port->ifer);
272 writel(mask, &at91_port->ifdr);
279 #if defined(CPU_HAS_PIO3)
281 * enable/disable the debounce filter.
283 int at91_set_pio_debounce(unsigned port, unsigned pin, int is_on, int div)
285 struct at91_port *at91_port = at91_pio_get_port(port);
288 if (at91_port && (pin < GPIO_PER_BANK)) {
291 writel(mask, &at91_port->ifscer);
292 writel(div & PIO_SCDR_DIV, &at91_port->scdr);
293 writel(mask, &at91_port->ifer);
295 writel(mask, &at91_port->ifdr);
303 * enable/disable the pull-down.
304 * If pull-up already enabled while calling the function, we disable it.
306 int at91_set_pio_pulldown(unsigned port, unsigned pin, int is_on)
308 struct at91_port *at91_port = at91_pio_get_port(port);
311 if (at91_port && (pin < GPIO_PER_BANK)) {
314 at91_set_pio_pullup(port, pin, 0);
315 writel(mask, &at91_port->ppder);
317 writel(mask, &at91_port->ppddr);
324 * disable Schmitt trigger
326 int at91_set_pio_disable_schmitt_trig(unsigned port, unsigned pin)
328 struct at91_port *at91_port = at91_pio_get_port(port);
331 if (at91_port && (pin < GPIO_PER_BANK)) {
333 writel(readl(&at91_port->schmitt) | mask,
334 &at91_port->schmitt);
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.
345 int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on)
347 struct at91_port *at91_port = at91_pio_get_port(port);
350 if (at91_port && (pin < GPIO_PER_BANK)) {
353 writel(mask, &at91_port->mder);
355 writel(mask, &at91_port->mddr);
361 static void at91_set_port_value(struct at91_port *at91_port, int offset,
368 writel(mask, &at91_port->sodr);
370 writel(mask, &at91_port->codr);
374 * assuming the pin is muxed as a gpio output, set its value.
376 int at91_set_pio_value(unsigned port, unsigned pin, int value)
378 struct at91_port *at91_port = at91_pio_get_port(port);
380 if (at91_port && (pin < GPIO_PER_BANK))
381 at91_set_port_value(at91_port, pin, value);
386 static int at91_get_port_value(struct at91_port *at91_port, int offset)
391 pdsr = readl(&at91_port->pdsr) & mask;
396 * read the pin's value (works even if it's not muxed as a gpio).
398 int at91_get_pio_value(unsigned port, unsigned pin)
400 struct at91_port *at91_port = at91_pio_get_port(port);
402 if (at91_port && (pin < GPIO_PER_BANK))
403 return at91_get_port_value(at91_port, pin);
408 #ifndef CONFIG_DM_GPIO
409 /* Common GPIO API */
411 int gpio_request(unsigned gpio, const char *label)
416 int gpio_free(unsigned gpio)
421 int gpio_direction_input(unsigned gpio)
423 at91_set_pio_input(at91_gpio_to_port(gpio),
424 at91_gpio_to_pin(gpio), 0);
428 int gpio_direction_output(unsigned gpio, int value)
430 at91_set_pio_output(at91_gpio_to_port(gpio),
431 at91_gpio_to_pin(gpio), value);
435 int gpio_get_value(unsigned gpio)
437 return at91_get_pio_value(at91_gpio_to_port(gpio),
438 at91_gpio_to_pin(gpio));
441 int gpio_set_value(unsigned gpio, int value)
443 at91_set_pio_value(at91_gpio_to_port(gpio),
444 at91_gpio_to_pin(gpio), value);
450 #ifdef CONFIG_DM_GPIO
452 struct at91_port_priv {
453 struct at91_port *regs;
456 /* set GPIO pin 'gpio' as an input */
457 static int at91_gpio_direction_input(struct udevice *dev, unsigned offset)
459 struct at91_port_priv *port = dev_get_priv(dev);
461 at91_set_port_input(port->regs, offset, 0);
466 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
467 static int at91_gpio_direction_output(struct udevice *dev, unsigned offset,
470 struct at91_port_priv *port = dev_get_priv(dev);
472 at91_set_port_output(port->regs, offset, value);
477 /* read GPIO IN value of pin 'gpio' */
478 static int at91_gpio_get_value(struct udevice *dev, unsigned offset)
480 struct at91_port_priv *port = dev_get_priv(dev);
482 return at91_get_port_value(port->regs, offset);
485 /* write GPIO OUT value to pin 'gpio' */
486 static int at91_gpio_set_value(struct udevice *dev, unsigned offset,
489 struct at91_port_priv *port = dev_get_priv(dev);
491 at91_set_port_value(port->regs, offset, value);
496 static int at91_gpio_get_function(struct udevice *dev, unsigned offset)
498 struct at91_port_priv *port = dev_get_priv(dev);
500 /* GPIOF_FUNC is not implemented yet */
501 if (at91_get_port_output(port->regs, offset))
507 static 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,
515 static int at91_gpio_probe(struct udevice *dev)
517 struct at91_port_priv *port = dev_get_priv(dev);
518 struct at91_port_platdata *plat = dev_get_platdata(dev);
519 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
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;
528 U_BOOT_DRIVER(gpio_at91) = {
531 .ops = &gpio_at91_ops,
532 .probe = at91_gpio_probe,
533 .priv_auto_alloc_size = sizeof(struct at91_port_priv),