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/debugfs.h>
17 #include <linux/seq_file.h>
18 #include <linux/kernel.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
23 #include <asm/hardware.h>
24 #include <asm/arch/at91_pio.h>
25 #include <asm/arch/gpio.h>
30 static struct at91_gpio_bank
*gpio
;
31 static int gpio_banks
;
34 static inline void __iomem
*pin_to_controller(unsigned pin
)
36 void __iomem
*sys_base
= (void __iomem
*) AT91_VA_BASE_SYS
;
40 if (likely(pin
< gpio_banks
))
41 return sys_base
+ gpio
[pin
].offset
;
46 static inline unsigned pin_to_mask(unsigned pin
)
49 return 1 << (pin
% 32);
53 /*--------------------------------------------------------------------------*/
55 /* Not all hardware capabilities are exposed through these calls; they
56 * only encapsulate the most common features and modes. (So if you
57 * want to change signals in groups, do it directly.)
59 * Bootloaders will usually handle some of the pin multiplexing setup.
60 * The intent is certainly that by the time Linux is fully booted, all
61 * pins should have been fully initialized. These setup calls should
62 * only be used by board setup routines, or possibly in driver probe().
64 * For bootloaders doing all that setup, these calls could be inlined
65 * as NOPs so Linux won't duplicate any setup code
70 * mux the pin to the "GPIO" peripheral role.
72 int __init_or_module
at91_set_GPIO_periph(unsigned pin
, int use_pullup
)
74 void __iomem
*pio
= pin_to_controller(pin
);
75 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_PER
);
84 EXPORT_SYMBOL(at91_set_GPIO_periph
);
88 * mux the pin to the "A" internal peripheral role.
90 int __init_or_module
at91_set_A_periph(unsigned pin
, int use_pullup
)
92 void __iomem
*pio
= pin_to_controller(pin
);
93 unsigned mask
= pin_to_mask(pin
);
98 __raw_writel(mask
, pio
+ PIO_IDR
);
99 __raw_writel(mask
, pio
+ (use_pullup
? PIO_PUER
: PIO_PUDR
));
100 __raw_writel(mask
, pio
+ PIO_ASR
);
101 __raw_writel(mask
, pio
+ PIO_PDR
);
104 EXPORT_SYMBOL(at91_set_A_periph
);
108 * mux the pin to the "B" internal peripheral role.
110 int __init_or_module
at91_set_B_periph(unsigned pin
, int use_pullup
)
112 void __iomem
*pio
= pin_to_controller(pin
);
113 unsigned mask
= pin_to_mask(pin
);
118 __raw_writel(mask
, pio
+ PIO_IDR
);
119 __raw_writel(mask
, pio
+ (use_pullup
? PIO_PUER
: PIO_PUDR
));
120 __raw_writel(mask
, pio
+ PIO_BSR
);
121 __raw_writel(mask
, pio
+ PIO_PDR
);
124 EXPORT_SYMBOL(at91_set_B_periph
);
128 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
129 * configure it for an input.
131 int __init_or_module
at91_set_gpio_input(unsigned pin
, int use_pullup
)
133 void __iomem
*pio
= pin_to_controller(pin
);
134 unsigned mask
= pin_to_mask(pin
);
139 __raw_writel(mask
, pio
+ PIO_IDR
);
140 __raw_writel(mask
, pio
+ (use_pullup
? PIO_PUER
: PIO_PUDR
));
141 __raw_writel(mask
, pio
+ PIO_ODR
);
142 __raw_writel(mask
, pio
+ PIO_PER
);
145 EXPORT_SYMBOL(at91_set_gpio_input
);
149 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
150 * and configure it for an output.
152 int __init_or_module
at91_set_gpio_output(unsigned pin
, int value
)
154 void __iomem
*pio
= pin_to_controller(pin
);
155 unsigned mask
= pin_to_mask(pin
);
160 __raw_writel(mask
, pio
+ PIO_IDR
);
161 __raw_writel(mask
, pio
+ PIO_PUDR
);
162 __raw_writel(mask
, pio
+ (value
? PIO_SODR
: PIO_CODR
));
163 __raw_writel(mask
, pio
+ PIO_OER
);
164 __raw_writel(mask
, pio
+ PIO_PER
);
167 EXPORT_SYMBOL(at91_set_gpio_output
);
171 * enable/disable the glitch filter; mostly used with IRQ handling.
173 int __init_or_module
at91_set_deglitch(unsigned pin
, int is_on
)
175 void __iomem
*pio
= pin_to_controller(pin
);
176 unsigned mask
= pin_to_mask(pin
);
180 __raw_writel(mask
, pio
+ (is_on
? PIO_IFER
: PIO_IFDR
));
183 EXPORT_SYMBOL(at91_set_deglitch
);
186 * enable/disable the multi-driver; This is only valid for output and
187 * allows the output pin to run as an open collector output.
189 int __init_or_module
at91_set_multi_drive(unsigned pin
, int is_on
)
191 void __iomem
*pio
= pin_to_controller(pin
);
192 unsigned mask
= pin_to_mask(pin
);
197 __raw_writel(mask
, pio
+ (is_on
? PIO_MDER
: PIO_MDDR
));
200 EXPORT_SYMBOL(at91_set_multi_drive
);
202 /*--------------------------------------------------------------------------*/
204 /* new-style GPIO calls; these expect at91_set_GPIO_periph to have been
205 * called, and maybe at91_set_multi_drive() for putout pins.
208 int gpio_direction_input(unsigned pin
)
210 void __iomem
*pio
= pin_to_controller(pin
);
211 unsigned mask
= pin_to_mask(pin
);
213 if (!pio
|| !(__raw_readl(pio
+ PIO_PSR
) & mask
))
215 __raw_writel(mask
, pio
+ PIO_ODR
);
218 EXPORT_SYMBOL(gpio_direction_input
);
220 int gpio_direction_output(unsigned pin
, int value
)
222 void __iomem
*pio
= pin_to_controller(pin
);
223 unsigned mask
= pin_to_mask(pin
);
225 if (!pio
|| !(__raw_readl(pio
+ PIO_PSR
) & mask
))
227 __raw_writel(mask
, pio
+ (value
? PIO_SODR
: PIO_CODR
));
228 __raw_writel(mask
, pio
+ PIO_OER
);
231 EXPORT_SYMBOL(gpio_direction_output
);
233 /*--------------------------------------------------------------------------*/
236 * assuming the pin is muxed as a gpio output, set its value.
238 int at91_set_gpio_value(unsigned pin
, int value
)
240 void __iomem
*pio
= pin_to_controller(pin
);
241 unsigned mask
= pin_to_mask(pin
);
245 __raw_writel(mask
, pio
+ (value
? PIO_SODR
: PIO_CODR
));
248 EXPORT_SYMBOL(at91_set_gpio_value
);
252 * read the pin's value (works even if it's not muxed as a gpio).
254 int at91_get_gpio_value(unsigned pin
)
256 void __iomem
*pio
= pin_to_controller(pin
);
257 unsigned mask
= pin_to_mask(pin
);
262 pdsr
= __raw_readl(pio
+ PIO_PDSR
);
263 return (pdsr
& mask
) != 0;
265 EXPORT_SYMBOL(at91_get_gpio_value
);
267 /*--------------------------------------------------------------------------*/
271 static u32 wakeups
[MAX_GPIO_BANKS
];
272 static u32 backups
[MAX_GPIO_BANKS
];
274 static int gpio_irq_set_wake(unsigned pin
, unsigned state
)
276 unsigned mask
= pin_to_mask(pin
);
277 unsigned bank
= (pin
- PIN_BASE
) / 32;
279 if (unlikely(bank
>= MAX_GPIO_BANKS
))
283 wakeups
[bank
] |= mask
;
285 wakeups
[bank
] &= ~mask
;
287 set_irq_wake(gpio
[bank
].id
, state
);
292 void at91_gpio_suspend(void)
296 for (i
= 0; i
< gpio_banks
; i
++) {
297 u32 pio
= gpio
[i
].offset
;
299 backups
[i
] = at91_sys_read(pio
+ PIO_IMR
);
300 at91_sys_write(pio
+ PIO_IDR
, backups
[i
]);
301 at91_sys_write(pio
+ PIO_IER
, wakeups
[i
]);
304 clk_disable(gpio
[i
].clock
);
306 #ifdef CONFIG_PM_DEBUG
307 printk(KERN_DEBUG
"GPIO-%c may wake for %08x\n", 'A'+i
, wakeups
[i
]);
313 void at91_gpio_resume(void)
317 for (i
= 0; i
< gpio_banks
; i
++) {
318 u32 pio
= gpio
[i
].offset
;
321 clk_enable(gpio
[i
].clock
);
323 at91_sys_write(pio
+ PIO_IDR
, wakeups
[i
]);
324 at91_sys_write(pio
+ PIO_IER
, backups
[i
]);
329 #define gpio_irq_set_wake NULL
333 /* Several AIC controller irqs are dispatched through this GPIO handler.
334 * To use any AT91_PIN_* as an externally triggered IRQ, first call
335 * at91_set_gpio_input() then maybe enable its glitch filter.
336 * Then just request_irq() with the pin ID; it works like any ARM IRQ
337 * handler, though it always triggers on rising and falling edges.
339 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
340 * configuring them with at91_set_a_periph() or at91_set_b_periph().
341 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
344 static void gpio_irq_mask(unsigned pin
)
346 void __iomem
*pio
= pin_to_controller(pin
);
347 unsigned mask
= pin_to_mask(pin
);
350 __raw_writel(mask
, pio
+ PIO_IDR
);
353 static void gpio_irq_unmask(unsigned pin
)
355 void __iomem
*pio
= pin_to_controller(pin
);
356 unsigned mask
= pin_to_mask(pin
);
359 __raw_writel(mask
, pio
+ PIO_IER
);
362 static int gpio_irq_type(unsigned pin
, unsigned type
)
364 return (type
== IRQT_BOTHEDGE
) ? 0 : -EINVAL
;
367 static struct irq_chip gpio_irqchip
= {
369 .mask
= gpio_irq_mask
,
370 .unmask
= gpio_irq_unmask
,
371 .set_type
= gpio_irq_type
,
372 .set_wake
= gpio_irq_set_wake
,
375 static void gpio_irq_handler(unsigned irq
, struct irq_desc
*desc
)
378 struct irq_desc
*gpio
;
382 pio
= get_irq_chip_data(irq
);
384 /* temporarily mask (level sensitive) parent IRQ */
385 desc
->chip
->ack(irq
);
387 /* reading ISR acks the pending (edge triggered) GPIO interrupt */
388 isr
= __raw_readl(pio
+ PIO_ISR
) & __raw_readl(pio
+ PIO_IMR
);
392 pin
= (unsigned) get_irq_data(irq
);
393 gpio
= &irq_desc
[pin
];
397 if (unlikely(gpio
->depth
)) {
399 * The core ARM interrupt handler lazily disables IRQs so
400 * another IRQ must be generated before it actually gets
401 * here to be disabled on the GPIO controller.
406 desc_handle_irq(pin
, gpio
);
413 desc
->chip
->unmask(irq
);
414 /* now it may re-trigger */
417 /*--------------------------------------------------------------------------*/
419 #ifdef CONFIG_DEBUG_FS
421 static int at91_gpio_show(struct seq_file
*s
, void *unused
)
426 seq_printf(s
, "Pin\t");
427 for (bank
= 0; bank
< gpio_banks
; bank
++) {
428 seq_printf(s
, "PIO%c\t", 'A' + bank
);
430 seq_printf(s
, "\n\n");
432 /* print pin status */
433 for (j
= 0; j
< 32; j
++) {
434 seq_printf(s
, "%i:\t", j
);
436 for (bank
= 0; bank
< gpio_banks
; bank
++) {
437 unsigned pin
= PIN_BASE
+ (32 * bank
) + j
;
438 void __iomem
*pio
= pin_to_controller(pin
);
439 unsigned mask
= pin_to_mask(pin
);
441 if (__raw_readl(pio
+ PIO_PSR
) & mask
)
442 seq_printf(s
, "GPIO:%s", __raw_readl(pio
+ PIO_PDSR
) & mask
? "1" : "0");
444 seq_printf(s
, "%s", __raw_readl(pio
+ PIO_ABSR
) & mask
? "B" : "A");
455 static int at91_gpio_open(struct inode
*inode
, struct file
*file
)
457 return single_open(file
, at91_gpio_show
, NULL
);
460 static const struct file_operations at91_gpio_operations
= {
461 .open
= at91_gpio_open
,
464 .release
= single_release
,
467 static int __init
at91_gpio_debugfs_init(void)
469 /* /sys/kernel/debug/at91_gpio */
470 (void) debugfs_create_file("at91_gpio", S_IFREG
| S_IRUGO
, NULL
, NULL
, &at91_gpio_operations
);
473 postcore_initcall(at91_gpio_debugfs_init
);
477 /*--------------------------------------------------------------------------*/
480 * Called from the processor-specific init to enable GPIO interrupt support.
482 void __init
at91_gpio_irq_setup(void)
486 for (pioc
= 0, pin
= PIN_BASE
;
489 void __iomem
*controller
;
490 unsigned id
= gpio
[pioc
].id
;
493 clk_enable(gpio
[pioc
].clock
); /* enable PIO controller's clock */
495 controller
= (void __iomem
*) AT91_VA_BASE_SYS
+ gpio
[pioc
].offset
;
496 __raw_writel(~0, controller
+ PIO_IDR
);
498 set_irq_data(id
, (void *) pin
);
499 set_irq_chip_data(id
, controller
);
501 for (i
= 0; i
< 32; i
++, pin
++) {
503 * Can use the "simple" and not "edge" handler since it's
504 * shorter, and the AIC handles interrupts sanely.
506 set_irq_chip(pin
, &gpio_irqchip
);
507 set_irq_handler(pin
, handle_simple_irq
);
508 set_irq_flags(pin
, IRQF_VALID
);
511 set_irq_chained_handler(id
, gpio_irq_handler
);
513 pr_info("AT91: %d gpio irqs in %d banks\n", pin
- PIN_BASE
, gpio_banks
);
517 * Called from the processor-specific init to enable GPIO pin support.
519 void __init
at91_gpio_init(struct at91_gpio_bank
*data
, int nr_banks
)
521 BUG_ON(nr_banks
> MAX_GPIO_BANKS
);
524 gpio_banks
= nr_banks
;