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 <mach/hardware.h>
24 #include <mach/at91_pio.h>
25 #include <mach/gpio.h>
31 struct at91_gpio_chip
{
32 struct gpio_chip chip
;
33 struct at91_gpio_chip
*next
; /* Bank sharing same clock */
34 struct at91_gpio_bank
*bank
; /* Bank definition */
35 void __iomem
*regbase
; /* Base of register bank */
38 #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
40 static void at91_gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
);
41 static void at91_gpiolib_set(struct gpio_chip
*chip
, unsigned offset
, int val
);
42 static int at91_gpiolib_get(struct gpio_chip
*chip
, unsigned offset
);
43 static int at91_gpiolib_direction_output(struct gpio_chip
*chip
,
44 unsigned offset
, int val
);
45 static int at91_gpiolib_direction_input(struct gpio_chip
*chip
,
47 static int at91_gpiolib_request(struct gpio_chip
*chip
, unsigned offset
);
49 #define AT91_GPIO_CHIP(name, base_gpio, nr_gpio) \
53 .request = at91_gpiolib_request, \
54 .direction_input = at91_gpiolib_direction_input, \
55 .direction_output = at91_gpiolib_direction_output, \
56 .get = at91_gpiolib_get, \
57 .set = at91_gpiolib_set, \
58 .dbg_show = at91_gpiolib_dbg_show, \
64 static struct at91_gpio_chip gpio_chip
[] = {
65 AT91_GPIO_CHIP("A", 0x00 + PIN_BASE
, 32),
66 AT91_GPIO_CHIP("B", 0x20 + PIN_BASE
, 32),
67 AT91_GPIO_CHIP("C", 0x40 + PIN_BASE
, 32),
68 AT91_GPIO_CHIP("D", 0x60 + PIN_BASE
, 32),
69 AT91_GPIO_CHIP("E", 0x80 + PIN_BASE
, 32),
72 static int gpio_banks
;
74 static inline void __iomem
*pin_to_controller(unsigned pin
)
78 if (likely(pin
< gpio_banks
))
79 return gpio_chip
[pin
].regbase
;
84 static inline unsigned pin_to_mask(unsigned pin
)
87 return 1 << (pin
% 32);
91 /*--------------------------------------------------------------------------*/
93 /* Not all hardware capabilities are exposed through these calls; they
94 * only encapsulate the most common features and modes. (So if you
95 * want to change signals in groups, do it directly.)
97 * Bootloaders will usually handle some of the pin multiplexing setup.
98 * The intent is certainly that by the time Linux is fully booted, all
99 * pins should have been fully initialized. These setup calls should
100 * only be used by board setup routines, or possibly in driver probe().
102 * For bootloaders doing all that setup, these calls could be inlined
103 * as NOPs so Linux won't duplicate any setup code
108 * mux the pin to the "GPIO" peripheral role.
110 int __init_or_module
at91_set_GPIO_periph(unsigned pin
, int use_pullup
)
112 void __iomem
*pio
= pin_to_controller(pin
);
113 unsigned mask
= pin_to_mask(pin
);
117 __raw_writel(mask
, pio
+ PIO_IDR
);
118 __raw_writel(mask
, pio
+ (use_pullup
? PIO_PUER
: PIO_PUDR
));
119 __raw_writel(mask
, pio
+ PIO_PER
);
122 EXPORT_SYMBOL(at91_set_GPIO_periph
);
126 * mux the pin to the "A" internal peripheral role.
128 int __init_or_module
at91_set_A_periph(unsigned pin
, int use_pullup
)
130 void __iomem
*pio
= pin_to_controller(pin
);
131 unsigned mask
= pin_to_mask(pin
);
136 __raw_writel(mask
, pio
+ PIO_IDR
);
137 __raw_writel(mask
, pio
+ (use_pullup
? PIO_PUER
: PIO_PUDR
));
138 __raw_writel(mask
, pio
+ PIO_ASR
);
139 __raw_writel(mask
, pio
+ PIO_PDR
);
142 EXPORT_SYMBOL(at91_set_A_periph
);
146 * mux the pin to the "B" internal peripheral role.
148 int __init_or_module
at91_set_B_periph(unsigned pin
, int use_pullup
)
150 void __iomem
*pio
= pin_to_controller(pin
);
151 unsigned mask
= pin_to_mask(pin
);
156 __raw_writel(mask
, pio
+ PIO_IDR
);
157 __raw_writel(mask
, pio
+ (use_pullup
? PIO_PUER
: PIO_PUDR
));
158 __raw_writel(mask
, pio
+ PIO_BSR
);
159 __raw_writel(mask
, pio
+ PIO_PDR
);
162 EXPORT_SYMBOL(at91_set_B_periph
);
166 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
167 * configure it for an input.
169 int __init_or_module
at91_set_gpio_input(unsigned pin
, int use_pullup
)
171 void __iomem
*pio
= pin_to_controller(pin
);
172 unsigned mask
= pin_to_mask(pin
);
177 __raw_writel(mask
, pio
+ PIO_IDR
);
178 __raw_writel(mask
, pio
+ (use_pullup
? PIO_PUER
: PIO_PUDR
));
179 __raw_writel(mask
, pio
+ PIO_ODR
);
180 __raw_writel(mask
, pio
+ PIO_PER
);
183 EXPORT_SYMBOL(at91_set_gpio_input
);
187 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
188 * and configure it for an output.
190 int __init_or_module
at91_set_gpio_output(unsigned pin
, int value
)
192 void __iomem
*pio
= pin_to_controller(pin
);
193 unsigned mask
= pin_to_mask(pin
);
198 __raw_writel(mask
, pio
+ PIO_IDR
);
199 __raw_writel(mask
, pio
+ PIO_PUDR
);
200 __raw_writel(mask
, pio
+ (value
? PIO_SODR
: PIO_CODR
));
201 __raw_writel(mask
, pio
+ PIO_OER
);
202 __raw_writel(mask
, pio
+ PIO_PER
);
205 EXPORT_SYMBOL(at91_set_gpio_output
);
209 * enable/disable the glitch filter; mostly used with IRQ handling.
211 int __init_or_module
at91_set_deglitch(unsigned pin
, int is_on
)
213 void __iomem
*pio
= pin_to_controller(pin
);
214 unsigned mask
= pin_to_mask(pin
);
218 __raw_writel(mask
, pio
+ (is_on
? PIO_IFER
: PIO_IFDR
));
221 EXPORT_SYMBOL(at91_set_deglitch
);
224 * enable/disable the multi-driver; This is only valid for output and
225 * allows the output pin to run as an open collector output.
227 int __init_or_module
at91_set_multi_drive(unsigned pin
, int is_on
)
229 void __iomem
*pio
= pin_to_controller(pin
);
230 unsigned mask
= pin_to_mask(pin
);
235 __raw_writel(mask
, pio
+ (is_on
? PIO_MDER
: PIO_MDDR
));
238 EXPORT_SYMBOL(at91_set_multi_drive
);
241 * assuming the pin is muxed as a gpio output, set its value.
243 int at91_set_gpio_value(unsigned pin
, int value
)
245 void __iomem
*pio
= pin_to_controller(pin
);
246 unsigned mask
= pin_to_mask(pin
);
250 __raw_writel(mask
, pio
+ (value
? PIO_SODR
: PIO_CODR
));
253 EXPORT_SYMBOL(at91_set_gpio_value
);
257 * read the pin's value (works even if it's not muxed as a gpio).
259 int at91_get_gpio_value(unsigned pin
)
261 void __iomem
*pio
= pin_to_controller(pin
);
262 unsigned mask
= pin_to_mask(pin
);
267 pdsr
= __raw_readl(pio
+ PIO_PDSR
);
268 return (pdsr
& mask
) != 0;
270 EXPORT_SYMBOL(at91_get_gpio_value
);
272 /*--------------------------------------------------------------------------*/
276 static u32 wakeups
[MAX_GPIO_BANKS
];
277 static u32 backups
[MAX_GPIO_BANKS
];
279 static int gpio_irq_set_wake(unsigned pin
, unsigned state
)
281 unsigned mask
= pin_to_mask(pin
);
282 unsigned bank
= (pin
- PIN_BASE
) / 32;
284 if (unlikely(bank
>= MAX_GPIO_BANKS
))
288 wakeups
[bank
] |= mask
;
290 wakeups
[bank
] &= ~mask
;
292 set_irq_wake(gpio_chip
[bank
].bank
->id
, state
);
297 void at91_gpio_suspend(void)
301 for (i
= 0; i
< gpio_banks
; i
++) {
302 void __iomem
*pio
= gpio_chip
[i
].regbase
;
304 backups
[i
] = __raw_readl(pio
+ PIO_IMR
);
305 __raw_writel(backups
[i
], pio
+ PIO_IDR
);
306 __raw_writel(wakeups
[i
], pio
+ PIO_IER
);
309 clk_disable(gpio_chip
[i
].bank
->clock
);
311 #ifdef CONFIG_PM_DEBUG
312 printk(KERN_DEBUG
"GPIO-%c may wake for %08x\n", 'A'+i
, wakeups
[i
]);
318 void at91_gpio_resume(void)
322 for (i
= 0; i
< gpio_banks
; i
++) {
323 void __iomem
*pio
= gpio_chip
[i
].regbase
;
326 clk_enable(gpio_chip
[i
].bank
->clock
);
328 __raw_writel(wakeups
[i
], pio
+ PIO_IDR
);
329 __raw_writel(backups
[i
], pio
+ PIO_IER
);
334 #define gpio_irq_set_wake NULL
338 /* Several AIC controller irqs are dispatched through this GPIO handler.
339 * To use any AT91_PIN_* as an externally triggered IRQ, first call
340 * at91_set_gpio_input() then maybe enable its glitch filter.
341 * Then just request_irq() with the pin ID; it works like any ARM IRQ
342 * handler, though it always triggers on rising and falling edges.
344 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
345 * configuring them with at91_set_a_periph() or at91_set_b_periph().
346 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
349 static void gpio_irq_mask(unsigned pin
)
351 void __iomem
*pio
= pin_to_controller(pin
);
352 unsigned mask
= pin_to_mask(pin
);
355 __raw_writel(mask
, pio
+ PIO_IDR
);
358 static void gpio_irq_unmask(unsigned pin
)
360 void __iomem
*pio
= pin_to_controller(pin
);
361 unsigned mask
= pin_to_mask(pin
);
364 __raw_writel(mask
, pio
+ PIO_IER
);
367 static int gpio_irq_type(unsigned pin
, unsigned type
)
371 case IRQ_TYPE_EDGE_BOTH
:
378 static struct irq_chip gpio_irqchip
= {
380 .mask
= gpio_irq_mask
,
381 .unmask
= gpio_irq_unmask
,
382 .set_type
= gpio_irq_type
,
383 .set_wake
= gpio_irq_set_wake
,
386 static void gpio_irq_handler(unsigned irq
, struct irq_desc
*desc
)
389 struct irq_desc
*gpio
;
390 struct at91_gpio_chip
*at91_gpio
;
394 at91_gpio
= get_irq_chip_data(irq
);
395 pio
= at91_gpio
->regbase
;
397 /* temporarily mask (level sensitive) parent IRQ */
398 desc
->chip
->ack(irq
);
400 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
401 * When there none are pending, we're finished unless we need
402 * to process multiple banks (like ID_PIOCDE on sam9263).
404 isr
= __raw_readl(pio
+ PIO_ISR
) & __raw_readl(pio
+ PIO_IMR
);
406 if (!at91_gpio
->next
)
408 at91_gpio
= at91_gpio
->next
;
409 pio
= at91_gpio
->regbase
;
413 pin
= at91_gpio
->chip
.base
;
414 gpio
= &irq_desc
[pin
];
418 if (unlikely(gpio
->depth
)) {
420 * The core ARM interrupt handler lazily disables IRQs so
421 * another IRQ must be generated before it actually gets
422 * here to be disabled on the GPIO controller.
427 generic_handle_irq(pin
);
434 desc
->chip
->unmask(irq
);
435 /* now it may re-trigger */
438 /*--------------------------------------------------------------------------*/
440 #ifdef CONFIG_DEBUG_FS
442 static int at91_gpio_show(struct seq_file
*s
, void *unused
)
447 seq_printf(s
, "Pin\t");
448 for (bank
= 0; bank
< gpio_banks
; bank
++) {
449 seq_printf(s
, "PIO%c\t", 'A' + bank
);
451 seq_printf(s
, "\n\n");
453 /* print pin status */
454 for (j
= 0; j
< 32; j
++) {
455 seq_printf(s
, "%i:\t", j
);
457 for (bank
= 0; bank
< gpio_banks
; bank
++) {
458 unsigned pin
= PIN_BASE
+ (32 * bank
) + j
;
459 void __iomem
*pio
= pin_to_controller(pin
);
460 unsigned mask
= pin_to_mask(pin
);
462 if (__raw_readl(pio
+ PIO_PSR
) & mask
)
463 seq_printf(s
, "GPIO:%s", __raw_readl(pio
+ PIO_PDSR
) & mask
? "1" : "0");
465 seq_printf(s
, "%s", __raw_readl(pio
+ PIO_ABSR
) & mask
? "B" : "A");
476 static int at91_gpio_open(struct inode
*inode
, struct file
*file
)
478 return single_open(file
, at91_gpio_show
, NULL
);
481 static const struct file_operations at91_gpio_operations
= {
482 .open
= at91_gpio_open
,
485 .release
= single_release
,
488 static int __init
at91_gpio_debugfs_init(void)
490 /* /sys/kernel/debug/at91_gpio */
491 (void) debugfs_create_file("at91_gpio", S_IFREG
| S_IRUGO
, NULL
, NULL
, &at91_gpio_operations
);
494 postcore_initcall(at91_gpio_debugfs_init
);
498 /*--------------------------------------------------------------------------*/
501 * This lock class tells lockdep that GPIO irqs are in a different
502 * category than their parents, so it won't report false recursion.
504 static struct lock_class_key gpio_lock_class
;
507 * Called from the processor-specific init to enable GPIO interrupt support.
509 void __init
at91_gpio_irq_setup(void)
512 struct at91_gpio_chip
*this, *prev
;
514 for (pioc
= 0, pin
= PIN_BASE
, this = gpio_chip
, prev
= NULL
;
516 prev
= this, this++) {
517 unsigned id
= this->bank
->id
;
520 __raw_writel(~0, this->regbase
+ PIO_IDR
);
522 for (i
= 0, pin
= this->chip
.base
; i
< 32; i
++, pin
++) {
523 lockdep_set_class(&irq_desc
[pin
].lock
, &gpio_lock_class
);
526 * Can use the "simple" and not "edge" handler since it's
527 * shorter, and the AIC handles interrupts sanely.
529 set_irq_chip(pin
, &gpio_irqchip
);
530 set_irq_handler(pin
, handle_simple_irq
);
531 set_irq_flags(pin
, IRQF_VALID
);
534 /* The toplevel handler handles one bank of GPIOs, except
535 * AT91SAM9263_ID_PIOCDE handles three... PIOC is first in
536 * the list, so we only set up that handler.
538 if (prev
&& prev
->next
== this)
541 set_irq_chip_data(id
, this);
542 set_irq_chained_handler(id
, gpio_irq_handler
);
544 pr_info("AT91: %d gpio irqs in %d banks\n", pin
- PIN_BASE
, gpio_banks
);
547 /* gpiolib support */
548 static int at91_gpiolib_direction_input(struct gpio_chip
*chip
,
551 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
552 void __iomem
*pio
= at91_gpio
->regbase
;
553 unsigned mask
= 1 << offset
;
555 __raw_writel(mask
, pio
+ PIO_ODR
);
559 static int at91_gpiolib_direction_output(struct gpio_chip
*chip
,
560 unsigned offset
, int val
)
562 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
563 void __iomem
*pio
= at91_gpio
->regbase
;
564 unsigned mask
= 1 << offset
;
566 __raw_writel(mask
, pio
+ (val
? PIO_SODR
: PIO_CODR
));
567 __raw_writel(mask
, pio
+ PIO_OER
);
571 static int at91_gpiolib_get(struct gpio_chip
*chip
, unsigned offset
)
573 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
574 void __iomem
*pio
= at91_gpio
->regbase
;
575 unsigned mask
= 1 << offset
;
578 pdsr
= __raw_readl(pio
+ PIO_PDSR
);
579 return (pdsr
& mask
) != 0;
582 static void at91_gpiolib_set(struct gpio_chip
*chip
, unsigned offset
, int val
)
584 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
585 void __iomem
*pio
= at91_gpio
->regbase
;
586 unsigned mask
= 1 << offset
;
588 __raw_writel(mask
, pio
+ (val
? PIO_SODR
: PIO_CODR
));
591 static int at91_gpiolib_request(struct gpio_chip
*chip
, unsigned offset
)
593 unsigned pin
= chip
->base
+ offset
;
594 void __iomem
*pio
= pin_to_controller(pin
);
595 unsigned mask
= pin_to_mask(pin
);
597 /* Cannot request GPIOs that are in alternate function mode */
598 if (!(__raw_readl(pio
+ PIO_PSR
) & mask
))
604 static void at91_gpiolib_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
608 for (i
= 0; i
< chip
->ngpio
; i
++) {
609 unsigned pin
= chip
->base
+ i
;
610 void __iomem
*pio
= pin_to_controller(pin
);
611 unsigned mask
= pin_to_mask(pin
);
612 const char *gpio_label
;
614 gpio_label
= gpiochip_is_requested(chip
, i
);
616 seq_printf(s
, "[%s] GPIO%s%d: ",
617 gpio_label
, chip
->label
, i
);
618 if (__raw_readl(pio
+ PIO_PSR
) & mask
)
619 seq_printf(s
, "[gpio] %s\n",
620 at91_get_gpio_value(pin
) ?
623 seq_printf(s
, "[periph %s]\n",
624 __raw_readl(pio
+ PIO_ABSR
) &
631 * Called from the processor-specific init to enable GPIO pin support.
633 void __init
at91_gpio_init(struct at91_gpio_bank
*data
, int nr_banks
)
636 struct at91_gpio_chip
*at91_gpio
, *last
= NULL
;
638 BUG_ON(nr_banks
> MAX_GPIO_BANKS
);
640 gpio_banks
= nr_banks
;
642 for (i
= 0; i
< nr_banks
; i
++) {
643 at91_gpio
= &gpio_chip
[i
];
645 at91_gpio
->bank
= &data
[i
];
646 at91_gpio
->chip
.base
= PIN_BASE
+ i
* 32;
647 at91_gpio
->regbase
= at91_gpio
->bank
->offset
+
648 (void __iomem
*)AT91_VA_BASE_SYS
;
650 /* enable PIO controller's clock */
651 clk_enable(at91_gpio
->bank
->clock
);
653 /* AT91SAM9263_ID_PIOCDE groups PIOC, PIOD, PIOE */
654 if (last
&& last
->bank
->id
== at91_gpio
->bank
->id
)
655 last
->next
= at91_gpio
;
658 gpiochip_add(&at91_gpio
->chip
);