[ARM] Merge individual ARM sub-trees
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / arm / mach-at91rm9200 / gpio.c
blob3f188508c39194aebab97e79871546a81eb8a0db
1 /*
2 * linux/arch/arm/mach-at91rm9200/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>
20 #include <asm/io.h>
21 #include <asm/hardware.h>
22 #include <asm/arch/at91_pio.h>
23 #include <asm/arch/at91_pmc.h>
24 #include <asm/arch/gpio.h>
26 #include "generic.h"
29 static struct at91_gpio_bank *gpio;
30 static int gpio_banks;
33 static inline void __iomem *pin_to_controller(unsigned pin)
35 void __iomem *sys_base = (void __iomem *) AT91_VA_BASE_SYS;
37 pin -= PIN_BASE;
38 pin /= 32;
39 if (likely(pin < gpio_banks))
40 return sys_base + gpio[pin].offset;
42 return NULL;
45 static inline unsigned pin_to_mask(unsigned pin)
47 pin -= PIN_BASE;
48 return 1 << (pin % 32);
52 /*--------------------------------------------------------------------------*/
54 /* Not all hardware capabilities are exposed through these calls; they
55 * only encapsulate the most common features and modes. (So if you
56 * want to change signals in groups, do it directly.)
58 * Bootloaders will usually handle some of the pin multiplexing setup.
59 * The intent is certainly that by the time Linux is fully booted, all
60 * pins should have been fully initialized. These setup calls should
61 * only be used by board setup routines, or possibly in driver probe().
63 * For bootloaders doing all that setup, these calls could be inlined
64 * as NOPs so Linux won't duplicate any setup code
69 * mux the pin to the "A" internal peripheral role.
71 int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
73 void __iomem *pio = pin_to_controller(pin);
74 unsigned mask = pin_to_mask(pin);
76 if (!pio)
77 return -EINVAL;
79 __raw_writel(mask, pio + PIO_IDR);
80 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
81 __raw_writel(mask, pio + PIO_ASR);
82 __raw_writel(mask, pio + PIO_PDR);
83 return 0;
85 EXPORT_SYMBOL(at91_set_A_periph);
89 * mux the pin to the "B" internal peripheral role.
91 int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
93 void __iomem *pio = pin_to_controller(pin);
94 unsigned mask = pin_to_mask(pin);
96 if (!pio)
97 return -EINVAL;
99 __raw_writel(mask, pio + PIO_IDR);
100 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
101 __raw_writel(mask, pio + PIO_BSR);
102 __raw_writel(mask, pio + PIO_PDR);
103 return 0;
105 EXPORT_SYMBOL(at91_set_B_periph);
109 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
110 * configure it for an input.
112 int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
114 void __iomem *pio = pin_to_controller(pin);
115 unsigned mask = pin_to_mask(pin);
117 if (!pio)
118 return -EINVAL;
120 __raw_writel(mask, pio + PIO_IDR);
121 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
122 __raw_writel(mask, pio + PIO_ODR);
123 __raw_writel(mask, pio + PIO_PER);
124 return 0;
126 EXPORT_SYMBOL(at91_set_gpio_input);
130 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
131 * and configure it for an output.
133 int __init_or_module at91_set_gpio_output(unsigned pin, int value)
135 void __iomem *pio = pin_to_controller(pin);
136 unsigned mask = pin_to_mask(pin);
138 if (!pio)
139 return -EINVAL;
141 __raw_writel(mask, pio + PIO_IDR);
142 __raw_writel(mask, pio + PIO_PUDR);
143 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
144 __raw_writel(mask, pio + PIO_OER);
145 __raw_writel(mask, pio + PIO_PER);
146 return 0;
148 EXPORT_SYMBOL(at91_set_gpio_output);
152 * enable/disable the glitch filter; mostly used with IRQ handling.
154 int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
156 void __iomem *pio = pin_to_controller(pin);
157 unsigned mask = pin_to_mask(pin);
159 if (!pio)
160 return -EINVAL;
161 __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
162 return 0;
164 EXPORT_SYMBOL(at91_set_deglitch);
167 * enable/disable the multi-driver; This is only valid for output and
168 * allows the output pin to run as an open collector output.
170 int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
172 void __iomem *pio = pin_to_controller(pin);
173 unsigned mask = pin_to_mask(pin);
175 if (!pio)
176 return -EINVAL;
178 __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
179 return 0;
181 EXPORT_SYMBOL(at91_set_multi_drive);
183 /*--------------------------------------------------------------------------*/
186 * assuming the pin is muxed as a gpio output, set its value.
188 int at91_set_gpio_value(unsigned pin, int value)
190 void __iomem *pio = pin_to_controller(pin);
191 unsigned mask = pin_to_mask(pin);
193 if (!pio)
194 return -EINVAL;
195 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
196 return 0;
198 EXPORT_SYMBOL(at91_set_gpio_value);
202 * read the pin's value (works even if it's not muxed as a gpio).
204 int at91_get_gpio_value(unsigned pin)
206 void __iomem *pio = pin_to_controller(pin);
207 unsigned mask = pin_to_mask(pin);
208 u32 pdsr;
210 if (!pio)
211 return -EINVAL;
212 pdsr = __raw_readl(pio + PIO_PDSR);
213 return (pdsr & mask) != 0;
215 EXPORT_SYMBOL(at91_get_gpio_value);
217 /*--------------------------------------------------------------------------*/
219 #ifdef CONFIG_PM
221 static u32 wakeups[MAX_GPIO_BANKS];
222 static u32 backups[MAX_GPIO_BANKS];
224 static int gpio_irq_set_wake(unsigned pin, unsigned state)
226 unsigned mask = pin_to_mask(pin);
228 pin -= PIN_BASE;
229 pin /= 32;
231 if (unlikely(pin >= MAX_GPIO_BANKS))
232 return -EINVAL;
234 if (state)
235 wakeups[pin] |= mask;
236 else
237 wakeups[pin] &= ~mask;
239 return 0;
242 void at91_gpio_suspend(void)
244 int i;
246 for (i = 0; i < gpio_banks; i++) {
247 u32 pio = gpio[i].offset;
250 * Note: drivers should have disabled GPIO interrupts that
251 * aren't supposed to be wakeup sources.
252 * But that is not much good on ARM..... disable_irq() does
253 * not update the hardware immediately, so the hardware mask
254 * (IMR) has the wrong value (not current, too much is
255 * permitted).
257 * Our workaround is to disable all non-wakeup IRQs ...
258 * which is exactly what correct drivers asked for in the
259 * first place!
261 backups[i] = at91_sys_read(pio + PIO_IMR);
262 at91_sys_write(pio + PIO_IDR, backups[i]);
263 at91_sys_write(pio + PIO_IER, wakeups[i]);
265 if (!wakeups[i]) {
266 disable_irq_wake(gpio[i].id);
267 at91_sys_write(AT91_PMC_PCDR, 1 << gpio[i].id);
268 } else {
269 enable_irq_wake(gpio[i].id);
270 #ifdef CONFIG_PM_DEBUG
271 printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", "ABCD"[i], wakeups[i]);
272 #endif
277 void at91_gpio_resume(void)
279 int i;
281 for (i = 0; i < gpio_banks; i++) {
282 u32 pio = gpio[i].offset;
284 at91_sys_write(pio + PIO_IDR, wakeups[i]);
285 at91_sys_write(pio + PIO_IER, backups[i]);
286 at91_sys_write(AT91_PMC_PCER, 1 << gpio[i].id);
290 #else
291 #define gpio_irq_set_wake NULL
292 #endif
295 /* Several AIC controller irqs are dispatched through this GPIO handler.
296 * To use any AT91_PIN_* as an externally triggered IRQ, first call
297 * at91_set_gpio_input() then maybe enable its glitch filter.
298 * Then just request_irq() with the pin ID; it works like any ARM IRQ
299 * handler, though it always triggers on rising and falling edges.
301 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
302 * configuring them with at91_set_a_periph() or at91_set_b_periph().
303 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
306 static void gpio_irq_mask(unsigned pin)
308 void __iomem *pio = pin_to_controller(pin);
309 unsigned mask = pin_to_mask(pin);
311 if (pio)
312 __raw_writel(mask, pio + PIO_IDR);
315 static void gpio_irq_unmask(unsigned pin)
317 void __iomem *pio = pin_to_controller(pin);
318 unsigned mask = pin_to_mask(pin);
320 if (pio)
321 __raw_writel(mask, pio + PIO_IER);
324 static int gpio_irq_type(unsigned pin, unsigned type)
326 return (type == IRQT_BOTHEDGE) ? 0 : -EINVAL;
329 static struct irq_chip gpio_irqchip = {
330 .name = "GPIO",
331 .mask = gpio_irq_mask,
332 .unmask = gpio_irq_unmask,
333 .set_type = gpio_irq_type,
334 .set_wake = gpio_irq_set_wake,
337 static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
339 unsigned pin;
340 struct irq_desc *gpio;
341 void __iomem *pio;
342 u32 isr;
344 pio = get_irq_chip_data(irq);
346 /* temporarily mask (level sensitive) parent IRQ */
347 desc->chip->ack(irq);
348 for (;;) {
349 /* reading ISR acks the pending (edge triggered) GPIO interrupt */
350 isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
351 if (!isr)
352 break;
354 pin = (unsigned) get_irq_data(irq);
355 gpio = &irq_desc[pin];
357 while (isr) {
358 if (isr & 1) {
359 if (unlikely(gpio->depth)) {
361 * The core ARM interrupt handler lazily disables IRQs so
362 * another IRQ must be generated before it actually gets
363 * here to be disabled on the GPIO controller.
365 gpio_irq_mask(pin);
367 else
368 desc_handle_irq(pin, gpio);
370 pin++;
371 gpio++;
372 isr >>= 1;
375 desc->chip->unmask(irq);
376 /* now it may re-trigger */
379 /*--------------------------------------------------------------------------*/
382 * Called from the processor-specific init to enable GPIO interrupt support.
384 void __init at91_gpio_irq_setup(void)
386 unsigned pioc, pin;
388 for (pioc = 0, pin = PIN_BASE;
389 pioc < gpio_banks;
390 pioc++) {
391 void __iomem *controller;
392 unsigned id = gpio[pioc].id;
393 unsigned i;
395 clk_enable(gpio[pioc].clock); /* enable PIO controller's clock */
397 controller = (void __iomem *) AT91_VA_BASE_SYS + gpio[pioc].offset;
398 __raw_writel(~0, controller + PIO_IDR);
400 set_irq_data(id, (void *) pin);
401 set_irq_chip_data(id, controller);
403 for (i = 0; i < 32; i++, pin++) {
405 * Can use the "simple" and not "edge" handler since it's
406 * shorter, and the AIC handles interupts sanely.
408 set_irq_chip(pin, &gpio_irqchip);
409 set_irq_handler(pin, handle_simple_irq);
410 set_irq_flags(pin, IRQF_VALID);
413 set_irq_chained_handler(id, gpio_irq_handler);
415 pr_info("AT91: %d gpio irqs in %d banks\n", pin - PIN_BASE, gpio_banks);
419 * Called from the processor-specific init to enable GPIO pin support.
421 void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
423 BUG_ON(nr_banks > MAX_GPIO_BANKS);
425 gpio = data;
426 gpio_banks = nr_banks;