2 * linux/arch/arm/mach-at91/gpio.c
4 * Copyright (C) 2005 HP Labs
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/clk.h>
13 #include <linux/errno.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
21 #include <asm/hardware.h>
22 #include <asm/arch/at91_pio.h>
23 #include <asm/arch/gpio.h>
28 static struct at91_gpio_bank *gpio;
29 static int gpio_banks;
32 static inline void __iomem *pin_to_controller(unsigned pin)
34 void __iomem *sys_base = (void __iomem *) AT91_VA_BASE_SYS;
38 if (likely(pin < gpio_banks))
39 return sys_base + gpio[pin].offset;
44 static inline unsigned pin_to_mask(unsigned pin)
47 return 1 << (pin % 32);
51 /*--------------------------------------------------------------------------*/
53 /* Not all hardware capabilities are exposed through these calls; they
54 * only encapsulate the most common features and modes. (So if you
55 * want to change signals in groups, do it directly.)
57 * Bootloaders will usually handle some of the pin multiplexing setup.
58 * The intent is certainly that by the time Linux is fully booted, all
59 * pins should have been fully initialized. These setup calls should
60 * only be used by board setup routines, or possibly in driver probe().
62 * For bootloaders doing all that setup, these calls could be inlined
63 * as NOPs so Linux won't duplicate any setup code
68 * mux the pin to the "GPIO" peripheral role.
70 int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup)
72 void __iomem *pio = pin_to_controller(pin);
73 unsigned mask = pin_to_mask(pin);
77 __raw_writel(mask, pio + PIO_IDR);
78 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
79 __raw_writel(mask, pio + PIO_PER);
82 EXPORT_SYMBOL(at91_set_GPIO_periph);
86 * mux the pin to the "A" internal peripheral role.
88 int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
90 void __iomem *pio = pin_to_controller(pin);
91 unsigned mask = pin_to_mask(pin);
96 __raw_writel(mask, pio + PIO_IDR);
97 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
98 __raw_writel(mask, pio + PIO_ASR);
99 __raw_writel(mask, pio + PIO_PDR);
102 EXPORT_SYMBOL(at91_set_A_periph);
106 * mux the pin to the "B" internal peripheral role.
108 int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
110 void __iomem *pio = pin_to_controller(pin);
111 unsigned mask = pin_to_mask(pin);
116 __raw_writel(mask, pio + PIO_IDR);
117 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
118 __raw_writel(mask, pio + PIO_BSR);
119 __raw_writel(mask, pio + PIO_PDR);
122 EXPORT_SYMBOL(at91_set_B_periph);
126 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
127 * configure it for an input.
129 int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
131 void __iomem *pio = pin_to_controller(pin);
132 unsigned mask = pin_to_mask(pin);
137 __raw_writel(mask, pio + PIO_IDR);
138 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
139 __raw_writel(mask, pio + PIO_ODR);
140 __raw_writel(mask, pio + PIO_PER);
143 EXPORT_SYMBOL(at91_set_gpio_input);
147 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
148 * and configure it for an output.
150 int __init_or_module at91_set_gpio_output(unsigned pin, int value)
152 void __iomem *pio = pin_to_controller(pin);
153 unsigned mask = pin_to_mask(pin);
158 __raw_writel(mask, pio + PIO_IDR);
159 __raw_writel(mask, pio + PIO_PUDR);
160 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
161 __raw_writel(mask, pio + PIO_OER);
162 __raw_writel(mask, pio + PIO_PER);
165 EXPORT_SYMBOL(at91_set_gpio_output);
169 * enable/disable the glitch filter; mostly used with IRQ handling.
171 int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
173 void __iomem *pio = pin_to_controller(pin);
174 unsigned mask = pin_to_mask(pin);
178 __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
181 EXPORT_SYMBOL(at91_set_deglitch);
184 * enable/disable the multi-driver; This is only valid for output and
185 * allows the output pin to run as an open collector output.
187 int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
189 void __iomem *pio = pin_to_controller(pin);
190 unsigned mask = pin_to_mask(pin);
195 __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
198 EXPORT_SYMBOL(at91_set_multi_drive);
200 /*--------------------------------------------------------------------------*/
202 /* new-style GPIO calls; these expect at91_set_GPIO_periph to have been
203 * called, and maybe at91_set_multi_drive() for putout pins.
206 int gpio_direction_input(unsigned pin)
208 void __iomem *pio = pin_to_controller(pin);
209 unsigned mask = pin_to_mask(pin);
211 if (!pio || !(__raw_readl(pio + PIO_PSR) & mask))
213 __raw_writel(mask, pio + PIO_ODR);
216 EXPORT_SYMBOL(gpio_direction_input);
218 int gpio_direction_output(unsigned pin, int value)
220 void __iomem *pio = pin_to_controller(pin);
221 unsigned mask = pin_to_mask(pin);
223 if (!pio || !(__raw_readl(pio + PIO_PSR) & mask))
225 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
226 __raw_writel(mask, pio + PIO_OER);
229 EXPORT_SYMBOL(gpio_direction_output);
231 /*--------------------------------------------------------------------------*/
234 * assuming the pin is muxed as a gpio output, set its value.
236 int at91_set_gpio_value(unsigned pin, int value)
238 void __iomem *pio = pin_to_controller(pin);
239 unsigned mask = pin_to_mask(pin);
243 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
246 EXPORT_SYMBOL(at91_set_gpio_value);
250 * read the pin's value (works even if it's not muxed as a gpio).
252 int at91_get_gpio_value(unsigned pin)
254 void __iomem *pio = pin_to_controller(pin);
255 unsigned mask = pin_to_mask(pin);
260 pdsr = __raw_readl(pio + PIO_PDSR);
261 return (pdsr & mask) != 0;
263 EXPORT_SYMBOL(at91_get_gpio_value);
265 /*--------------------------------------------------------------------------*/
269 static u32 wakeups[MAX_GPIO_BANKS];
270 static u32 backups[MAX_GPIO_BANKS];
272 static int gpio_irq_set_wake(unsigned pin, unsigned state)
274 unsigned mask = pin_to_mask(pin);
275 unsigned bank = (pin - PIN_BASE) / 32;
277 if (unlikely(bank >= MAX_GPIO_BANKS))
281 wakeups[bank] |= mask;
283 wakeups[bank] &= ~mask;
285 set_irq_wake(gpio[bank].id, state);
290 void at91_gpio_suspend(void)
294 for (i = 0; i < gpio_banks; i++) {
295 u32 pio = gpio[i].offset;
297 backups[i] = at91_sys_read(pio + PIO_IMR);
298 at91_sys_write(pio + PIO_IDR, backups[i]);
299 at91_sys_write(pio + PIO_IER, wakeups[i]);
302 clk_disable(gpio[i].clock);
304 #ifdef CONFIG_PM_DEBUG
305 printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
311 void at91_gpio_resume(void)
315 for (i = 0; i < gpio_banks; i++) {
316 u32 pio = gpio[i].offset;
319 clk_enable(gpio[i].clock);
321 at91_sys_write(pio + PIO_IDR, wakeups[i]);
322 at91_sys_write(pio + PIO_IER, backups[i]);
327 #define gpio_irq_set_wake NULL
331 /* Several AIC controller irqs are dispatched through this GPIO handler.
332 * To use any AT91_PIN_* as an externally triggered IRQ, first call
333 * at91_set_gpio_input() then maybe enable its glitch filter.
334 * Then just request_irq() with the pin ID; it works like any ARM IRQ
335 * handler, though it always triggers on rising and falling edges.
337 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
338 * configuring them with at91_set_a_periph() or at91_set_b_periph().
339 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
342 static void gpio_irq_mask(unsigned pin)
344 void __iomem *pio = pin_to_controller(pin);
345 unsigned mask = pin_to_mask(pin);
348 __raw_writel(mask, pio + PIO_IDR);
351 static void gpio_irq_unmask(unsigned pin)
353 void __iomem *pio = pin_to_controller(pin);
354 unsigned mask = pin_to_mask(pin);
357 __raw_writel(mask, pio + PIO_IER);
360 static int gpio_irq_type(unsigned pin, unsigned type)
362 return (type == IRQT_BOTHEDGE) ? 0 : -EINVAL;
365 static struct irq_chip gpio_irqchip = {
367 .mask = gpio_irq_mask,
368 .unmask = gpio_irq_unmask,
369 .set_type = gpio_irq_type,
370 .set_wake = gpio_irq_set_wake,
373 static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
376 struct irq_desc *gpio;
380 pio = get_irq_chip_data(irq);
382 /* temporarily mask (level sensitive) parent IRQ */
383 desc->chip->ack(irq);
385 /* reading ISR acks the pending (edge triggered) GPIO interrupt */
386 isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
390 pin = (unsigned) get_irq_data(irq);
391 gpio = &irq_desc[pin];
395 if (unlikely(gpio->depth)) {
397 * The core ARM interrupt handler lazily disables IRQs so
398 * another IRQ must be generated before it actually gets
399 * here to be disabled on the GPIO controller.
404 desc_handle_irq(pin, gpio);
411 desc->chip->unmask(irq);
412 /* now it may re-trigger */
415 /*--------------------------------------------------------------------------*/
418 * Called from the processor-specific init to enable GPIO interrupt support.
420 void __init at91_gpio_irq_setup(void)
424 for (pioc = 0, pin = PIN_BASE;
427 void __iomem *controller;
428 unsigned id = gpio[pioc].id;
431 clk_enable(gpio[pioc].clock); /* enable PIO controller's clock */
433 controller = (void __iomem *) AT91_VA_BASE_SYS + gpio[pioc].offset;
434 __raw_writel(~0, controller + PIO_IDR);
436 set_irq_data(id, (void *) pin);
437 set_irq_chip_data(id, controller);
439 for (i = 0; i < 32; i++, pin++) {
441 * Can use the "simple" and not "edge" handler since it's
442 * shorter, and the AIC handles interrupts sanely.
444 set_irq_chip(pin, &gpio_irqchip);
445 set_irq_handler(pin, handle_simple_irq);
446 set_irq_flags(pin, IRQF_VALID);
449 set_irq_chained_handler(id, gpio_irq_handler);
451 pr_info("AT91: %d gpio irqs in %d banks\n", pin - PIN_BASE, gpio_banks);
455 * Called from the processor-specific init to enable GPIO pin support.
457 void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
459 BUG_ON(nr_banks > MAX_GPIO_BANKS);
462 gpio_banks = nr_banks;