Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / arch / arm / mach-at91 / gpio.c
blob74d6783eeabbb976c4261d299fcfe11b70d83984
1 /*
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/gpio.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/debugfs.h>
18 #include <linux/seq_file.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/io.h>
24 #include <mach/hardware.h>
25 #include <mach/at91_pio.h>
27 #include "generic.h"
29 struct at91_gpio_chip {
30 struct gpio_chip chip;
31 struct at91_gpio_chip *next; /* Bank sharing same clock */
32 int id; /* ID of register bank */
33 void __iomem *regbase; /* Base of register bank */
34 struct clk *clock; /* associated clock */
37 #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
39 static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip);
40 static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val);
41 static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset);
42 static int at91_gpiolib_direction_output(struct gpio_chip *chip,
43 unsigned offset, int val);
44 static int at91_gpiolib_direction_input(struct gpio_chip *chip,
45 unsigned offset);
47 #define AT91_GPIO_CHIP(name, base_gpio, nr_gpio) \
48 { \
49 .chip = { \
50 .label = name, \
51 .direction_input = at91_gpiolib_direction_input, \
52 .direction_output = at91_gpiolib_direction_output, \
53 .get = at91_gpiolib_get, \
54 .set = at91_gpiolib_set, \
55 .dbg_show = at91_gpiolib_dbg_show, \
56 .base = base_gpio, \
57 .ngpio = nr_gpio, \
58 }, \
61 static struct at91_gpio_chip gpio_chip[] = {
62 AT91_GPIO_CHIP("pioA", 0x00, 32),
63 AT91_GPIO_CHIP("pioB", 0x20, 32),
64 AT91_GPIO_CHIP("pioC", 0x40, 32),
65 AT91_GPIO_CHIP("pioD", 0x60, 32),
66 AT91_GPIO_CHIP("pioE", 0x80, 32),
69 static int gpio_banks;
71 static inline void __iomem *pin_to_controller(unsigned pin)
73 pin /= 32;
74 if (likely(pin < gpio_banks))
75 return gpio_chip[pin].regbase;
77 return NULL;
80 static inline unsigned pin_to_mask(unsigned pin)
82 return 1 << (pin % 32);
86 /*--------------------------------------------------------------------------*/
88 /* Not all hardware capabilities are exposed through these calls; they
89 * only encapsulate the most common features and modes. (So if you
90 * want to change signals in groups, do it directly.)
92 * Bootloaders will usually handle some of the pin multiplexing setup.
93 * The intent is certainly that by the time Linux is fully booted, all
94 * pins should have been fully initialized. These setup calls should
95 * only be used by board setup routines, or possibly in driver probe().
97 * For bootloaders doing all that setup, these calls could be inlined
98 * as NOPs so Linux won't duplicate any setup code
103 * mux the pin to the "GPIO" peripheral role.
105 int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup)
107 void __iomem *pio = pin_to_controller(pin);
108 unsigned mask = pin_to_mask(pin);
110 if (!pio)
111 return -EINVAL;
112 __raw_writel(mask, pio + PIO_IDR);
113 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
114 __raw_writel(mask, pio + PIO_PER);
115 return 0;
117 EXPORT_SYMBOL(at91_set_GPIO_periph);
121 * mux the pin to the "A" internal peripheral role.
123 int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
125 void __iomem *pio = pin_to_controller(pin);
126 unsigned mask = pin_to_mask(pin);
128 if (!pio)
129 return -EINVAL;
131 __raw_writel(mask, pio + PIO_IDR);
132 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
133 __raw_writel(mask, pio + PIO_ASR);
134 __raw_writel(mask, pio + PIO_PDR);
135 return 0;
137 EXPORT_SYMBOL(at91_set_A_periph);
141 * mux the pin to the "B" internal peripheral role.
143 int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
145 void __iomem *pio = pin_to_controller(pin);
146 unsigned mask = pin_to_mask(pin);
148 if (!pio)
149 return -EINVAL;
151 __raw_writel(mask, pio + PIO_IDR);
152 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
153 __raw_writel(mask, pio + PIO_BSR);
154 __raw_writel(mask, pio + PIO_PDR);
155 return 0;
157 EXPORT_SYMBOL(at91_set_B_periph);
161 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
162 * configure it for an input.
164 int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
166 void __iomem *pio = pin_to_controller(pin);
167 unsigned mask = pin_to_mask(pin);
169 if (!pio)
170 return -EINVAL;
172 __raw_writel(mask, pio + PIO_IDR);
173 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
174 __raw_writel(mask, pio + PIO_ODR);
175 __raw_writel(mask, pio + PIO_PER);
176 return 0;
178 EXPORT_SYMBOL(at91_set_gpio_input);
182 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
183 * and configure it for an output.
185 int __init_or_module at91_set_gpio_output(unsigned pin, int value)
187 void __iomem *pio = pin_to_controller(pin);
188 unsigned mask = pin_to_mask(pin);
190 if (!pio)
191 return -EINVAL;
193 __raw_writel(mask, pio + PIO_IDR);
194 __raw_writel(mask, pio + PIO_PUDR);
195 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
196 __raw_writel(mask, pio + PIO_OER);
197 __raw_writel(mask, pio + PIO_PER);
198 return 0;
200 EXPORT_SYMBOL(at91_set_gpio_output);
204 * enable/disable the glitch filter; mostly used with IRQ handling.
206 int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
208 void __iomem *pio = pin_to_controller(pin);
209 unsigned mask = pin_to_mask(pin);
211 if (!pio)
212 return -EINVAL;
213 __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
214 return 0;
216 EXPORT_SYMBOL(at91_set_deglitch);
219 * enable/disable the multi-driver; This is only valid for output and
220 * allows the output pin to run as an open collector output.
222 int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
224 void __iomem *pio = pin_to_controller(pin);
225 unsigned mask = pin_to_mask(pin);
227 if (!pio)
228 return -EINVAL;
230 __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
231 return 0;
233 EXPORT_SYMBOL(at91_set_multi_drive);
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);
243 if (!pio)
244 return -EINVAL;
245 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
246 return 0;
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);
258 u32 pdsr;
260 if (!pio)
261 return -EINVAL;
262 pdsr = __raw_readl(pio + PIO_PDSR);
263 return (pdsr & mask) != 0;
265 EXPORT_SYMBOL(at91_get_gpio_value);
267 /*--------------------------------------------------------------------------*/
269 #ifdef CONFIG_PM
271 static u32 wakeups[MAX_GPIO_BANKS];
272 static u32 backups[MAX_GPIO_BANKS];
274 static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
276 unsigned pin = irq_to_gpio(d->irq);
277 unsigned mask = pin_to_mask(pin);
278 unsigned bank = pin / 32;
280 if (unlikely(bank >= MAX_GPIO_BANKS))
281 return -EINVAL;
283 if (state)
284 wakeups[bank] |= mask;
285 else
286 wakeups[bank] &= ~mask;
288 irq_set_irq_wake(gpio_chip[bank].id, state);
290 return 0;
293 void at91_gpio_suspend(void)
295 int i;
297 for (i = 0; i < gpio_banks; i++) {
298 void __iomem *pio = gpio_chip[i].regbase;
300 backups[i] = __raw_readl(pio + PIO_IMR);
301 __raw_writel(backups[i], pio + PIO_IDR);
302 __raw_writel(wakeups[i], pio + PIO_IER);
304 if (!wakeups[i])
305 clk_disable(gpio_chip[i].clock);
306 else {
307 #ifdef CONFIG_PM_DEBUG
308 printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
309 #endif
314 void at91_gpio_resume(void)
316 int i;
318 for (i = 0; i < gpio_banks; i++) {
319 void __iomem *pio = gpio_chip[i].regbase;
321 if (!wakeups[i])
322 clk_enable(gpio_chip[i].clock);
324 __raw_writel(wakeups[i], pio + PIO_IDR);
325 __raw_writel(backups[i], pio + PIO_IER);
329 #else
330 #define gpio_irq_set_wake NULL
331 #endif
334 /* Several AIC controller irqs are dispatched through this GPIO handler.
335 * To use any AT91_PIN_* as an externally triggered IRQ, first call
336 * at91_set_gpio_input() then maybe enable its glitch filter.
337 * Then just request_irq() with the pin ID; it works like any ARM IRQ
338 * handler, though it always triggers on rising and falling edges.
340 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
341 * configuring them with at91_set_a_periph() or at91_set_b_periph().
342 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
345 static void gpio_irq_mask(struct irq_data *d)
347 unsigned pin = irq_to_gpio(d->irq);
348 void __iomem *pio = pin_to_controller(pin);
349 unsigned mask = pin_to_mask(pin);
351 if (pio)
352 __raw_writel(mask, pio + PIO_IDR);
355 static void gpio_irq_unmask(struct irq_data *d)
357 unsigned pin = irq_to_gpio(d->irq);
358 void __iomem *pio = pin_to_controller(pin);
359 unsigned mask = pin_to_mask(pin);
361 if (pio)
362 __raw_writel(mask, pio + PIO_IER);
365 static int gpio_irq_type(struct irq_data *d, unsigned type)
367 switch (type) {
368 case IRQ_TYPE_NONE:
369 case IRQ_TYPE_EDGE_BOTH:
370 return 0;
371 default:
372 return -EINVAL;
376 static struct irq_chip gpio_irqchip = {
377 .name = "GPIO",
378 .irq_disable = gpio_irq_mask,
379 .irq_mask = gpio_irq_mask,
380 .irq_unmask = gpio_irq_unmask,
381 .irq_set_type = gpio_irq_type,
382 .irq_set_wake = gpio_irq_set_wake,
385 static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
387 unsigned irq_pin;
388 struct irq_data *idata = irq_desc_get_irq_data(desc);
389 struct irq_chip *chip = irq_data_get_irq_chip(idata);
390 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(idata);
391 void __iomem *pio = at91_gpio->regbase;
392 u32 isr;
394 /* temporarily mask (level sensitive) parent IRQ */
395 chip->irq_ack(idata);
396 for (;;) {
397 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
398 * When there none are pending, we're finished unless we need
399 * to process multiple banks (like ID_PIOCDE on sam9263).
401 isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
402 if (!isr) {
403 if (!at91_gpio->next)
404 break;
405 at91_gpio = at91_gpio->next;
406 pio = at91_gpio->regbase;
407 continue;
410 irq_pin = gpio_to_irq(at91_gpio->chip.base);
412 while (isr) {
413 if (isr & 1)
414 generic_handle_irq(irq_pin);
415 irq_pin++;
416 isr >>= 1;
419 chip->irq_unmask(idata);
420 /* now it may re-trigger */
423 /*--------------------------------------------------------------------------*/
425 #ifdef CONFIG_DEBUG_FS
427 static int at91_gpio_show(struct seq_file *s, void *unused)
429 int bank, j;
431 /* print heading */
432 seq_printf(s, "Pin\t");
433 for (bank = 0; bank < gpio_banks; bank++) {
434 seq_printf(s, "PIO%c\t", 'A' + bank);
436 seq_printf(s, "\n\n");
438 /* print pin status */
439 for (j = 0; j < 32; j++) {
440 seq_printf(s, "%i:\t", j);
442 for (bank = 0; bank < gpio_banks; bank++) {
443 unsigned pin = (32 * bank) + j;
444 void __iomem *pio = pin_to_controller(pin);
445 unsigned mask = pin_to_mask(pin);
447 if (__raw_readl(pio + PIO_PSR) & mask)
448 seq_printf(s, "GPIO:%s", __raw_readl(pio + PIO_PDSR) & mask ? "1" : "0");
449 else
450 seq_printf(s, "%s", __raw_readl(pio + PIO_ABSR) & mask ? "B" : "A");
452 seq_printf(s, "\t");
455 seq_printf(s, "\n");
458 return 0;
461 static int at91_gpio_open(struct inode *inode, struct file *file)
463 return single_open(file, at91_gpio_show, NULL);
466 static const struct file_operations at91_gpio_operations = {
467 .open = at91_gpio_open,
468 .read = seq_read,
469 .llseek = seq_lseek,
470 .release = single_release,
473 static int __init at91_gpio_debugfs_init(void)
475 /* /sys/kernel/debug/at91_gpio */
476 (void) debugfs_create_file("at91_gpio", S_IFREG | S_IRUGO, NULL, NULL, &at91_gpio_operations);
477 return 0;
479 postcore_initcall(at91_gpio_debugfs_init);
481 #endif
483 /*--------------------------------------------------------------------------*/
486 * This lock class tells lockdep that GPIO irqs are in a different
487 * category than their parents, so it won't report false recursion.
489 static struct lock_class_key gpio_lock_class;
492 * Called from the processor-specific init to enable GPIO interrupt support.
494 void __init at91_gpio_irq_setup(void)
496 unsigned pioc, irq = gpio_to_irq(0);
497 struct at91_gpio_chip *this, *prev;
499 for (pioc = 0, this = gpio_chip, prev = NULL;
500 pioc++ < gpio_banks;
501 prev = this, this++) {
502 unsigned id = this->id;
503 unsigned i;
505 __raw_writel(~0, this->regbase + PIO_IDR);
507 for (i = 0, irq = gpio_to_irq(this->chip.base); i < 32;
508 i++, irq++) {
509 irq_set_lockdep_class(irq, &gpio_lock_class);
512 * Can use the "simple" and not "edge" handler since it's
513 * shorter, and the AIC handles interrupts sanely.
515 irq_set_chip_and_handler(irq, &gpio_irqchip,
516 handle_simple_irq);
517 set_irq_flags(irq, IRQF_VALID);
520 /* The toplevel handler handles one bank of GPIOs, except
521 * AT91SAM9263_ID_PIOCDE handles three... PIOC is first in
522 * the list, so we only set up that handler.
524 if (prev && prev->next == this)
525 continue;
527 irq_set_chip_data(id, this);
528 irq_set_chained_handler(id, gpio_irq_handler);
530 pr_info("AT91: %d gpio irqs in %d banks\n", irq - gpio_to_irq(0), gpio_banks);
533 /* gpiolib support */
534 static int at91_gpiolib_direction_input(struct gpio_chip *chip,
535 unsigned offset)
537 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
538 void __iomem *pio = at91_gpio->regbase;
539 unsigned mask = 1 << offset;
541 __raw_writel(mask, pio + PIO_ODR);
542 return 0;
545 static int at91_gpiolib_direction_output(struct gpio_chip *chip,
546 unsigned offset, int val)
548 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
549 void __iomem *pio = at91_gpio->regbase;
550 unsigned mask = 1 << offset;
552 __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
553 __raw_writel(mask, pio + PIO_OER);
554 return 0;
557 static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset)
559 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
560 void __iomem *pio = at91_gpio->regbase;
561 unsigned mask = 1 << offset;
562 u32 pdsr;
564 pdsr = __raw_readl(pio + PIO_PDSR);
565 return (pdsr & mask) != 0;
568 static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val)
570 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
571 void __iomem *pio = at91_gpio->regbase;
572 unsigned mask = 1 << offset;
574 __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
577 static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
579 int i;
581 for (i = 0; i < chip->ngpio; i++) {
582 unsigned pin = chip->base + i;
583 void __iomem *pio = pin_to_controller(pin);
584 unsigned mask = pin_to_mask(pin);
585 const char *gpio_label;
587 gpio_label = gpiochip_is_requested(chip, i);
588 if (gpio_label) {
589 seq_printf(s, "[%s] GPIO%s%d: ",
590 gpio_label, chip->label, i);
591 if (__raw_readl(pio + PIO_PSR) & mask)
592 seq_printf(s, "[gpio] %s\n",
593 at91_get_gpio_value(pin) ?
594 "set" : "clear");
595 else
596 seq_printf(s, "[periph %s]\n",
597 __raw_readl(pio + PIO_ABSR) &
598 mask ? "B" : "A");
604 * Called from the processor-specific init to enable GPIO pin support.
606 void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
608 unsigned i;
609 struct at91_gpio_chip *at91_gpio, *last = NULL;
611 BUG_ON(nr_banks > MAX_GPIO_BANKS);
613 gpio_banks = nr_banks;
615 for (i = 0; i < nr_banks; i++) {
616 at91_gpio = &gpio_chip[i];
618 at91_gpio->id = data[i].id;
619 at91_gpio->chip.base = i * 32;
621 at91_gpio->regbase = ioremap(data[i].regbase, 512);
622 if (!at91_gpio->regbase) {
623 pr_err("at91_gpio.%d, failed to map registers, ignoring.\n", i);
624 continue;
627 at91_gpio->clock = clk_get_sys(NULL, at91_gpio->chip.label);
628 if (!at91_gpio->clock) {
629 pr_err("at91_gpio.%d, failed to get clock, ignoring.\n", i);
630 continue;
633 /* enable PIO controller's clock */
634 clk_enable(at91_gpio->clock);
636 /* AT91SAM9263_ID_PIOCDE groups PIOC, PIOD, PIOE */
637 if (last && last->id == at91_gpio->id)
638 last->next = at91_gpio;
639 last = at91_gpio;
641 gpiochip_add(&at91_gpio->chip);