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>
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>
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
;
39 if (likely(pin
< gpio_banks
))
40 return sys_base
+ gpio
[pin
].offset
;
45 static inline unsigned pin_to_mask(unsigned pin
)
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
161 __raw_writel(mask
, pio
+ (is_on
? PIO_IFER
: PIO_IFDR
));
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
);
178 __raw_writel(mask
, pio
+ (is_on
? PIO_MDER
: PIO_MDDR
));
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
);
195 __raw_writel(mask
, pio
+ (value
? PIO_SODR
: PIO_CODR
));
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
);
212 pdsr
= __raw_readl(pio
+ PIO_PDSR
);
213 return (pdsr
& mask
) != 0;
215 EXPORT_SYMBOL(at91_get_gpio_value
);
217 /*--------------------------------------------------------------------------*/
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
);
231 if (unlikely(pin
>= MAX_GPIO_BANKS
))
235 wakeups
[pin
] |= mask
;
237 wakeups
[pin
] &= ~mask
;
242 void at91_gpio_suspend(void)
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
257 * Our workaround is to disable all non-wakeup IRQs ...
258 * which is exactly what correct drivers asked for in the
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
]);
266 disable_irq_wake(gpio
[i
].id
);
267 at91_sys_write(AT91_PMC_PCDR
, 1 << gpio
[i
].id
);
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
]);
277 void at91_gpio_resume(void)
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
);
291 #define gpio_irq_set_wake NULL
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
);
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
);
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
= {
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
)
340 struct irq_desc
*gpio
;
344 pio
= get_irq_chip_data(irq
);
346 /* temporarily mask (level sensitive) parent IRQ */
347 desc
->chip
->ack(irq
);
349 /* reading ISR acks the pending (edge triggered) GPIO interrupt */
350 isr
= __raw_readl(pio
+ PIO_ISR
) & __raw_readl(pio
+ PIO_IMR
);
354 pin
= (unsigned) get_irq_data(irq
);
355 gpio
= &irq_desc
[pin
];
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.
368 desc_handle_irq(pin
, gpio
);
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)
388 for (pioc
= 0, pin
= PIN_BASE
;
391 void __iomem
*controller
;
392 unsigned id
= gpio
[pioc
].id
;
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
);
426 gpio_banks
= nr_banks
;