]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/arm/mach-at91/gpio.c
ARM: at91/gpio: change comments and one variable name
[karo-tx-linux.git] / arch / arm / mach-at91 / gpio.c
1 /*
2  * linux/arch/arm/mach-at91/gpio.c
3  *
4  * Copyright (C) 2005 HP Labs
5  *
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.
10  */
11
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>
23
24 #include <mach/hardware.h>
25 #include <mach/at91_pio.h>
26
27 #include "generic.h"
28
29 struct at91_gpio_chip {
30         struct gpio_chip        chip;
31         struct at91_gpio_chip   *next;          /* Bank sharing same clock */
32         int                     pioc_hwirq;     /* PIO bank interrupt identifier on AIC */
33         void __iomem            *regbase;       /* PIO bank virtual address */
34         struct clk              *clock;         /* associated clock */
35 };
36
37 #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
38
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);
46
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                 },                                                      \
59         }
60
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),
67 };
68
69 static int gpio_banks;
70
71 static inline void __iomem *pin_to_controller(unsigned pin)
72 {
73         pin /= 32;
74         if (likely(pin < gpio_banks))
75                 return gpio_chip[pin].regbase;
76
77         return NULL;
78 }
79
80 static inline unsigned pin_to_mask(unsigned pin)
81 {
82         return 1 << (pin % 32);
83 }
84
85
86 /*--------------------------------------------------------------------------*/
87
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.)
91  *
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().
96  *
97  * For bootloaders doing all that setup, these calls could be inlined
98  * as NOPs so Linux won't duplicate any setup code
99  */
100
101
102 /*
103  * mux the pin to the "GPIO" peripheral role.
104  */
105 int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup)
106 {
107         void __iomem    *pio = pin_to_controller(pin);
108         unsigned        mask = pin_to_mask(pin);
109
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;
116 }
117 EXPORT_SYMBOL(at91_set_GPIO_periph);
118
119
120 /*
121  * mux the pin to the "A" internal peripheral role.
122  */
123 int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
124 {
125         void __iomem    *pio = pin_to_controller(pin);
126         unsigned        mask = pin_to_mask(pin);
127
128         if (!pio)
129                 return -EINVAL;
130
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;
136 }
137 EXPORT_SYMBOL(at91_set_A_periph);
138
139
140 /*
141  * mux the pin to the "B" internal peripheral role.
142  */
143 int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
144 {
145         void __iomem    *pio = pin_to_controller(pin);
146         unsigned        mask = pin_to_mask(pin);
147
148         if (!pio)
149                 return -EINVAL;
150
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;
156 }
157 EXPORT_SYMBOL(at91_set_B_periph);
158
159
160 /*
161  * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
162  * configure it for an input.
163  */
164 int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
165 {
166         void __iomem    *pio = pin_to_controller(pin);
167         unsigned        mask = pin_to_mask(pin);
168
169         if (!pio)
170                 return -EINVAL;
171
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;
177 }
178 EXPORT_SYMBOL(at91_set_gpio_input);
179
180
181 /*
182  * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
183  * and configure it for an output.
184  */
185 int __init_or_module at91_set_gpio_output(unsigned pin, int value)
186 {
187         void __iomem    *pio = pin_to_controller(pin);
188         unsigned        mask = pin_to_mask(pin);
189
190         if (!pio)
191                 return -EINVAL;
192
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;
199 }
200 EXPORT_SYMBOL(at91_set_gpio_output);
201
202
203 /*
204  * enable/disable the glitch filter; mostly used with IRQ handling.
205  */
206 int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
207 {
208         void __iomem    *pio = pin_to_controller(pin);
209         unsigned        mask = pin_to_mask(pin);
210
211         if (!pio)
212                 return -EINVAL;
213         __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
214         return 0;
215 }
216 EXPORT_SYMBOL(at91_set_deglitch);
217
218 /*
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.
221  */
222 int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
223 {
224         void __iomem    *pio = pin_to_controller(pin);
225         unsigned        mask = pin_to_mask(pin);
226
227         if (!pio)
228                 return -EINVAL;
229
230         __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
231         return 0;
232 }
233 EXPORT_SYMBOL(at91_set_multi_drive);
234
235 /*
236  * assuming the pin is muxed as a gpio output, set its value.
237  */
238 int at91_set_gpio_value(unsigned pin, int value)
239 {
240         void __iomem    *pio = pin_to_controller(pin);
241         unsigned        mask = pin_to_mask(pin);
242
243         if (!pio)
244                 return -EINVAL;
245         __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
246         return 0;
247 }
248 EXPORT_SYMBOL(at91_set_gpio_value);
249
250
251 /*
252  * read the pin's value (works even if it's not muxed as a gpio).
253  */
254 int at91_get_gpio_value(unsigned pin)
255 {
256         void __iomem    *pio = pin_to_controller(pin);
257         unsigned        mask = pin_to_mask(pin);
258         u32             pdsr;
259
260         if (!pio)
261                 return -EINVAL;
262         pdsr = __raw_readl(pio + PIO_PDSR);
263         return (pdsr & mask) != 0;
264 }
265 EXPORT_SYMBOL(at91_get_gpio_value);
266
267 /*--------------------------------------------------------------------------*/
268
269 #ifdef CONFIG_PM
270
271 static u32 wakeups[MAX_GPIO_BANKS];
272 static u32 backups[MAX_GPIO_BANKS];
273
274 static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
275 {
276         unsigned        pin = irq_to_gpio(d->irq);
277         unsigned        mask = pin_to_mask(pin);
278         unsigned        bank = pin / 32;
279
280         if (unlikely(bank >= MAX_GPIO_BANKS))
281                 return -EINVAL;
282
283         if (state)
284                 wakeups[bank] |= mask;
285         else
286                 wakeups[bank] &= ~mask;
287
288         irq_set_irq_wake(gpio_chip[bank].pioc_hwirq, state);
289
290         return 0;
291 }
292
293 void at91_gpio_suspend(void)
294 {
295         int i;
296
297         for (i = 0; i < gpio_banks; i++) {
298                 void __iomem    *pio = gpio_chip[i].regbase;
299
300                 backups[i] = __raw_readl(pio + PIO_IMR);
301                 __raw_writel(backups[i], pio + PIO_IDR);
302                 __raw_writel(wakeups[i], pio + PIO_IER);
303
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
310                 }
311         }
312 }
313
314 void at91_gpio_resume(void)
315 {
316         int i;
317
318         for (i = 0; i < gpio_banks; i++) {
319                 void __iomem    *pio = gpio_chip[i].regbase;
320
321                 if (!wakeups[i])
322                         clk_enable(gpio_chip[i].clock);
323
324                 __raw_writel(wakeups[i], pio + PIO_IDR);
325                 __raw_writel(backups[i], pio + PIO_IER);
326         }
327 }
328
329 #else
330 #define gpio_irq_set_wake       NULL
331 #endif
332
333
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.
339  *
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.
343  */
344
345 static void gpio_irq_mask(struct irq_data *d)
346 {
347         unsigned        pin = irq_to_gpio(d->irq);
348         void __iomem    *pio = pin_to_controller(pin);
349         unsigned        mask = pin_to_mask(pin);
350
351         if (pio)
352                 __raw_writel(mask, pio + PIO_IDR);
353 }
354
355 static void gpio_irq_unmask(struct irq_data *d)
356 {
357         unsigned        pin = irq_to_gpio(d->irq);
358         void __iomem    *pio = pin_to_controller(pin);
359         unsigned        mask = pin_to_mask(pin);
360
361         if (pio)
362                 __raw_writel(mask, pio + PIO_IER);
363 }
364
365 static int gpio_irq_type(struct irq_data *d, unsigned type)
366 {
367         switch (type) {
368         case IRQ_TYPE_NONE:
369         case IRQ_TYPE_EDGE_BOTH:
370                 return 0;
371         default:
372                 return -EINVAL;
373         }
374 }
375
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,
383 };
384
385 static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
386 {
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;
393
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).
400                  */
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;
408                 }
409
410                 irq_pin = gpio_to_irq(at91_gpio->chip.base);
411
412                 while (isr) {
413                         if (isr & 1)
414                                 generic_handle_irq(irq_pin);
415                         irq_pin++;
416                         isr >>= 1;
417                 }
418         }
419         chip->irq_unmask(idata);
420         /* now it may re-trigger */
421 }
422
423 /*--------------------------------------------------------------------------*/
424
425 #ifdef CONFIG_DEBUG_FS
426
427 static int at91_gpio_show(struct seq_file *s, void *unused)
428 {
429         int bank, j;
430
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);
435         };
436         seq_printf(s, "\n\n");
437
438         /* print pin status */
439         for (j = 0; j < 32; j++) {
440                 seq_printf(s, "%i:\t", j);
441
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);
446
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");
451
452                         seq_printf(s, "\t");
453                 }
454
455                 seq_printf(s, "\n");
456         }
457
458         return 0;
459 }
460
461 static int at91_gpio_open(struct inode *inode, struct file *file)
462 {
463         return single_open(file, at91_gpio_show, NULL);
464 }
465
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,
471 };
472
473 static int __init at91_gpio_debugfs_init(void)
474 {
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;
478 }
479 postcore_initcall(at91_gpio_debugfs_init);
480
481 #endif
482
483 /*--------------------------------------------------------------------------*/
484
485 /*
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.
488  */
489 static struct lock_class_key gpio_lock_class;
490
491 /*
492  * Called from the processor-specific init to enable GPIO interrupt support.
493  */
494 void __init at91_gpio_irq_setup(void)
495 {
496         unsigned                pioc, irq = gpio_to_irq(0);
497         struct at91_gpio_chip   *this, *prev;
498
499         for (pioc = 0, this = gpio_chip, prev = NULL;
500                         pioc++ < gpio_banks;
501                         prev = this, this++) {
502                 unsigned        pioc_hwirq = this->pioc_hwirq;
503                 unsigned        i;
504
505                 __raw_writel(~0, this->regbase + PIO_IDR);
506
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);
510
511                         /*
512                          * Can use the "simple" and not "edge" handler since it's
513                          * shorter, and the AIC handles interrupts sanely.
514                          */
515                         irq_set_chip_and_handler(irq, &gpio_irqchip,
516                                                  handle_simple_irq);
517                         set_irq_flags(irq, IRQF_VALID);
518                 }
519
520                 /* The toplevel handler handles one bank of GPIOs, except
521                  * on some SoC it can handles up to three...
522                  * We only set up the handler for the first of the list.
523                  */
524                 if (prev && prev->next == this)
525                         continue;
526
527                 irq_set_chip_data(pioc_hwirq, this);
528                 irq_set_chained_handler(pioc_hwirq, gpio_irq_handler);
529         }
530         pr_info("AT91: %d gpio irqs in %d banks\n", irq - gpio_to_irq(0), gpio_banks);
531 }
532
533 /* gpiolib support */
534 static int at91_gpiolib_direction_input(struct gpio_chip *chip,
535                                         unsigned offset)
536 {
537         struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
538         void __iomem *pio = at91_gpio->regbase;
539         unsigned mask = 1 << offset;
540
541         __raw_writel(mask, pio + PIO_ODR);
542         return 0;
543 }
544
545 static int at91_gpiolib_direction_output(struct gpio_chip *chip,
546                                          unsigned offset, int val)
547 {
548         struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
549         void __iomem *pio = at91_gpio->regbase;
550         unsigned mask = 1 << offset;
551
552         __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
553         __raw_writel(mask, pio + PIO_OER);
554         return 0;
555 }
556
557 static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset)
558 {
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;
563
564         pdsr = __raw_readl(pio + PIO_PDSR);
565         return (pdsr & mask) != 0;
566 }
567
568 static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val)
569 {
570         struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
571         void __iomem *pio = at91_gpio->regbase;
572         unsigned mask = 1 << offset;
573
574         __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
575 }
576
577 static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
578 {
579         int i;
580
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;
586
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");
599                 }
600         }
601 }
602
603 /*
604  * Called from the processor-specific init to enable GPIO pin support.
605  */
606 void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
607 {
608         unsigned                i;
609         struct at91_gpio_chip *at91_gpio, *last = NULL;
610
611         BUG_ON(nr_banks > MAX_GPIO_BANKS);
612
613         gpio_banks = nr_banks;
614
615         for (i = 0; i < nr_banks; i++) {
616                 at91_gpio = &gpio_chip[i];
617
618                 at91_gpio->pioc_hwirq = data[i].pioc_hwirq;
619                 at91_gpio->chip.base = i * 32;
620
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;
625                 }
626
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;
631                 }
632
633                 /* enable PIO controller's clock */
634                 clk_enable(at91_gpio->clock);
635
636                 /*
637                  * GPIO controller are grouped on some SoC:
638                  * PIOC, PIOD and PIOE can share the same IRQ line
639                  */
640                 if (last && last->pioc_hwirq == at91_gpio->pioc_hwirq)
641                         last->next = at91_gpio;
642                 last = at91_gpio;
643
644                 gpiochip_add(&at91_gpio->chip);
645         }
646 }