]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/arm/mach-at91/gpio.c
567df654a2e104bd9ea83162c39f584b648f5e09
[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/device.h>
15 #include <linux/gpio.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/debugfs.h>
19 #include <linux/seq_file.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/io.h>
24 #include <linux/irqdomain.h>
25 #include <linux/of_address.h>
26 #include <linux/of_irq.h>
27 #include <linux/of_gpio.h>
28
29 #include <mach/hardware.h>
30 #include <mach/at91_pio.h>
31
32 #include "generic.h"
33
34 struct at91_gpio_chip {
35         struct gpio_chip        chip;
36         struct at91_gpio_chip   *next;          /* Bank sharing same clock */
37         int                     pioc_hwirq;     /* PIO bank interrupt identifier on AIC */
38         int                     pioc_virq;      /* PIO bank Linux virtual interrupt */
39         int                     pioc_idx;       /* PIO bank index */
40         void __iomem            *regbase;       /* PIO bank virtual address */
41         struct clk              *clock;         /* associated clock */
42         struct irq_domain       *domain;        /* associated irq domain */
43 };
44
45 #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
46
47 static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip);
48 static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val);
49 static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset);
50 static int at91_gpiolib_direction_output(struct gpio_chip *chip,
51                                          unsigned offset, int val);
52 static int at91_gpiolib_direction_input(struct gpio_chip *chip,
53                                         unsigned offset);
54 static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset);
55
56 #define AT91_GPIO_CHIP(name, nr_gpio)                                   \
57         {                                                               \
58                 .chip = {                                               \
59                         .label            = name,                       \
60                         .direction_input  = at91_gpiolib_direction_input, \
61                         .direction_output = at91_gpiolib_direction_output, \
62                         .get              = at91_gpiolib_get,           \
63                         .set              = at91_gpiolib_set,           \
64                         .dbg_show         = at91_gpiolib_dbg_show,      \
65                         .to_irq           = at91_gpiolib_to_irq,        \
66                         .ngpio            = nr_gpio,                    \
67                 },                                                      \
68         }
69
70 static struct at91_gpio_chip gpio_chip[] = {
71         AT91_GPIO_CHIP("pioA", 32),
72         AT91_GPIO_CHIP("pioB", 32),
73         AT91_GPIO_CHIP("pioC", 32),
74         AT91_GPIO_CHIP("pioD", 32),
75         AT91_GPIO_CHIP("pioE", 32),
76 };
77
78 static int gpio_banks;
79
80 static inline void __iomem *pin_to_controller(unsigned pin)
81 {
82         pin /= 32;
83         if (likely(pin < gpio_banks))
84                 return gpio_chip[pin].regbase;
85
86         return NULL;
87 }
88
89 static inline unsigned pin_to_mask(unsigned pin)
90 {
91         return 1 << (pin % 32);
92 }
93
94
95 /*--------------------------------------------------------------------------*/
96
97 /* Not all hardware capabilities are exposed through these calls; they
98  * only encapsulate the most common features and modes.  (So if you
99  * want to change signals in groups, do it directly.)
100  *
101  * Bootloaders will usually handle some of the pin multiplexing setup.
102  * The intent is certainly that by the time Linux is fully booted, all
103  * pins should have been fully initialized.  These setup calls should
104  * only be used by board setup routines, or possibly in driver probe().
105  *
106  * For bootloaders doing all that setup, these calls could be inlined
107  * as NOPs so Linux won't duplicate any setup code
108  */
109
110
111 /*
112  * mux the pin to the "GPIO" peripheral role.
113  */
114 int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup)
115 {
116         void __iomem    *pio = pin_to_controller(pin);
117         unsigned        mask = pin_to_mask(pin);
118
119         if (!pio)
120                 return -EINVAL;
121         __raw_writel(mask, pio + PIO_IDR);
122         __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
123         __raw_writel(mask, pio + PIO_PER);
124         return 0;
125 }
126 EXPORT_SYMBOL(at91_set_GPIO_periph);
127
128
129 /*
130  * mux the pin to the "A" internal peripheral role.
131  */
132 int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
133 {
134         void __iomem    *pio = pin_to_controller(pin);
135         unsigned        mask = pin_to_mask(pin);
136
137         if (!pio)
138                 return -EINVAL;
139
140         __raw_writel(mask, pio + PIO_IDR);
141         __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
142         __raw_writel(mask, pio + PIO_ASR);
143         __raw_writel(mask, pio + PIO_PDR);
144         return 0;
145 }
146 EXPORT_SYMBOL(at91_set_A_periph);
147
148
149 /*
150  * mux the pin to the "B" internal peripheral role.
151  */
152 int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
153 {
154         void __iomem    *pio = pin_to_controller(pin);
155         unsigned        mask = pin_to_mask(pin);
156
157         if (!pio)
158                 return -EINVAL;
159
160         __raw_writel(mask, pio + PIO_IDR);
161         __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
162         __raw_writel(mask, pio + PIO_BSR);
163         __raw_writel(mask, pio + PIO_PDR);
164         return 0;
165 }
166 EXPORT_SYMBOL(at91_set_B_periph);
167
168
169 /*
170  * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
171  * configure it for an input.
172  */
173 int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
174 {
175         void __iomem    *pio = pin_to_controller(pin);
176         unsigned        mask = pin_to_mask(pin);
177
178         if (!pio)
179                 return -EINVAL;
180
181         __raw_writel(mask, pio + PIO_IDR);
182         __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
183         __raw_writel(mask, pio + PIO_ODR);
184         __raw_writel(mask, pio + PIO_PER);
185         return 0;
186 }
187 EXPORT_SYMBOL(at91_set_gpio_input);
188
189
190 /*
191  * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
192  * and configure it for an output.
193  */
194 int __init_or_module at91_set_gpio_output(unsigned pin, int value)
195 {
196         void __iomem    *pio = pin_to_controller(pin);
197         unsigned        mask = pin_to_mask(pin);
198
199         if (!pio)
200                 return -EINVAL;
201
202         __raw_writel(mask, pio + PIO_IDR);
203         __raw_writel(mask, pio + PIO_PUDR);
204         __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
205         __raw_writel(mask, pio + PIO_OER);
206         __raw_writel(mask, pio + PIO_PER);
207         return 0;
208 }
209 EXPORT_SYMBOL(at91_set_gpio_output);
210
211
212 /*
213  * enable/disable the glitch filter; mostly used with IRQ handling.
214  */
215 int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
216 {
217         void __iomem    *pio = pin_to_controller(pin);
218         unsigned        mask = pin_to_mask(pin);
219
220         if (!pio)
221                 return -EINVAL;
222         __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
223         return 0;
224 }
225 EXPORT_SYMBOL(at91_set_deglitch);
226
227 /*
228  * enable/disable the multi-driver; This is only valid for output and
229  * allows the output pin to run as an open collector output.
230  */
231 int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
232 {
233         void __iomem    *pio = pin_to_controller(pin);
234         unsigned        mask = pin_to_mask(pin);
235
236         if (!pio)
237                 return -EINVAL;
238
239         __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
240         return 0;
241 }
242 EXPORT_SYMBOL(at91_set_multi_drive);
243
244 /*
245  * assuming the pin is muxed as a gpio output, set its value.
246  */
247 int at91_set_gpio_value(unsigned pin, int value)
248 {
249         void __iomem    *pio = pin_to_controller(pin);
250         unsigned        mask = pin_to_mask(pin);
251
252         if (!pio)
253                 return -EINVAL;
254         __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
255         return 0;
256 }
257 EXPORT_SYMBOL(at91_set_gpio_value);
258
259
260 /*
261  * read the pin's value (works even if it's not muxed as a gpio).
262  */
263 int at91_get_gpio_value(unsigned pin)
264 {
265         void __iomem    *pio = pin_to_controller(pin);
266         unsigned        mask = pin_to_mask(pin);
267         u32             pdsr;
268
269         if (!pio)
270                 return -EINVAL;
271         pdsr = __raw_readl(pio + PIO_PDSR);
272         return (pdsr & mask) != 0;
273 }
274 EXPORT_SYMBOL(at91_get_gpio_value);
275
276 /*--------------------------------------------------------------------------*/
277
278 #ifdef CONFIG_PM
279
280 static u32 wakeups[MAX_GPIO_BANKS];
281 static u32 backups[MAX_GPIO_BANKS];
282
283 static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
284 {
285         struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
286         unsigned        mask = 1 << d->hwirq;
287         unsigned        bank = at91_gpio->pioc_idx;
288
289         if (unlikely(bank >= MAX_GPIO_BANKS))
290                 return -EINVAL;
291
292         if (state)
293                 wakeups[bank] |= mask;
294         else
295                 wakeups[bank] &= ~mask;
296
297         irq_set_irq_wake(at91_gpio->pioc_virq, state);
298
299         return 0;
300 }
301
302 void at91_gpio_suspend(void)
303 {
304         int i;
305
306         for (i = 0; i < gpio_banks; i++) {
307                 void __iomem    *pio = gpio_chip[i].regbase;
308
309                 backups[i] = __raw_readl(pio + PIO_IMR);
310                 __raw_writel(backups[i], pio + PIO_IDR);
311                 __raw_writel(wakeups[i], pio + PIO_IER);
312
313                 if (!wakeups[i]) {
314                         clk_unprepare(gpio_chip[i].clock);
315                         clk_disable(gpio_chip[i].clock);
316                 } else {
317 #ifdef CONFIG_PM_DEBUG
318                         printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
319 #endif
320                 }
321         }
322 }
323
324 void at91_gpio_resume(void)
325 {
326         int i;
327
328         for (i = 0; i < gpio_banks; i++) {
329                 void __iomem    *pio = gpio_chip[i].regbase;
330
331                 if (!wakeups[i]) {
332                         if (clk_prepare(gpio_chip[i].clock) == 0)
333                                 clk_enable(gpio_chip[i].clock);
334                 }
335
336                 __raw_writel(wakeups[i], pio + PIO_IDR);
337                 __raw_writel(backups[i], pio + PIO_IER);
338         }
339 }
340
341 #else
342 #define gpio_irq_set_wake       NULL
343 #endif
344
345
346 /* Several AIC controller irqs are dispatched through this GPIO handler.
347  * To use any AT91_PIN_* as an externally triggered IRQ, first call
348  * at91_set_gpio_input() then maybe enable its glitch filter.
349  * Then just request_irq() with the pin ID; it works like any ARM IRQ
350  * handler, though it always triggers on rising and falling edges.
351  *
352  * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
353  * configuring them with at91_set_a_periph() or at91_set_b_periph().
354  * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
355  */
356
357 static void gpio_irq_mask(struct irq_data *d)
358 {
359         struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
360         void __iomem    *pio = at91_gpio->regbase;
361         unsigned        mask = 1 << d->hwirq;
362
363         if (pio)
364                 __raw_writel(mask, pio + PIO_IDR);
365 }
366
367 static void gpio_irq_unmask(struct irq_data *d)
368 {
369         struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
370         void __iomem    *pio = at91_gpio->regbase;
371         unsigned        mask = 1 << d->hwirq;
372
373         if (pio)
374                 __raw_writel(mask, pio + PIO_IER);
375 }
376
377 static int gpio_irq_type(struct irq_data *d, unsigned type)
378 {
379         switch (type) {
380         case IRQ_TYPE_NONE:
381         case IRQ_TYPE_EDGE_BOTH:
382                 return 0;
383         default:
384                 return -EINVAL;
385         }
386 }
387
388 static struct irq_chip gpio_irqchip = {
389         .name           = "GPIO",
390         .irq_disable    = gpio_irq_mask,
391         .irq_mask       = gpio_irq_mask,
392         .irq_unmask     = gpio_irq_unmask,
393         .irq_set_type   = gpio_irq_type,
394         .irq_set_wake   = gpio_irq_set_wake,
395 };
396
397 static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
398 {
399         struct irq_data *idata = irq_desc_get_irq_data(desc);
400         struct irq_chip *chip = irq_data_get_irq_chip(idata);
401         struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(idata);
402         void __iomem    *pio = at91_gpio->regbase;
403         unsigned long   isr;
404         int             n;
405
406         /* temporarily mask (level sensitive) parent IRQ */
407         chip->irq_ack(idata);
408         for (;;) {
409                 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
410                  * When there none are pending, we're finished unless we need
411                  * to process multiple banks (like ID_PIOCDE on sam9263).
412                  */
413                 isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
414                 if (!isr) {
415                         if (!at91_gpio->next)
416                                 break;
417                         at91_gpio = at91_gpio->next;
418                         pio = at91_gpio->regbase;
419                         continue;
420                 }
421
422                 n = find_first_bit(&isr, BITS_PER_LONG);
423                 while (n < BITS_PER_LONG) {
424                         generic_handle_irq(irq_find_mapping(at91_gpio->domain, n));
425                         n = find_next_bit(&isr, BITS_PER_LONG, n + 1);
426                 }
427         }
428         chip->irq_unmask(idata);
429         /* now it may re-trigger */
430 }
431
432 /*--------------------------------------------------------------------------*/
433
434 #ifdef CONFIG_DEBUG_FS
435
436 static int at91_gpio_show(struct seq_file *s, void *unused)
437 {
438         int bank, j;
439
440         /* print heading */
441         seq_printf(s, "Pin\t");
442         for (bank = 0; bank < gpio_banks; bank++) {
443                 seq_printf(s, "PIO%c\t", 'A' + bank);
444         };
445         seq_printf(s, "\n\n");
446
447         /* print pin status */
448         for (j = 0; j < 32; j++) {
449                 seq_printf(s, "%i:\t", j);
450
451                 for (bank = 0; bank < gpio_banks; bank++) {
452                         unsigned        pin  = (32 * bank) + j;
453                         void __iomem    *pio = pin_to_controller(pin);
454                         unsigned        mask = pin_to_mask(pin);
455
456                         if (__raw_readl(pio + PIO_PSR) & mask)
457                                 seq_printf(s, "GPIO:%s", __raw_readl(pio + PIO_PDSR) & mask ? "1" : "0");
458                         else
459                                 seq_printf(s, "%s", __raw_readl(pio + PIO_ABSR) & mask ? "B" : "A");
460
461                         seq_printf(s, "\t");
462                 }
463
464                 seq_printf(s, "\n");
465         }
466
467         return 0;
468 }
469
470 static int at91_gpio_open(struct inode *inode, struct file *file)
471 {
472         return single_open(file, at91_gpio_show, NULL);
473 }
474
475 static const struct file_operations at91_gpio_operations = {
476         .open           = at91_gpio_open,
477         .read           = seq_read,
478         .llseek         = seq_lseek,
479         .release        = single_release,
480 };
481
482 static int __init at91_gpio_debugfs_init(void)
483 {
484         /* /sys/kernel/debug/at91_gpio */
485         (void) debugfs_create_file("at91_gpio", S_IFREG | S_IRUGO, NULL, NULL, &at91_gpio_operations);
486         return 0;
487 }
488 postcore_initcall(at91_gpio_debugfs_init);
489
490 #endif
491
492 /*--------------------------------------------------------------------------*/
493
494 /*
495  * This lock class tells lockdep that GPIO irqs are in a different
496  * category than their parents, so it won't report false recursion.
497  */
498 static struct lock_class_key gpio_lock_class;
499
500 #if defined(CONFIG_OF)
501 static int at91_gpio_irq_map(struct irq_domain *h, unsigned int virq,
502                                                         irq_hw_number_t hw)
503 {
504         struct at91_gpio_chip   *at91_gpio = h->host_data;
505
506         irq_set_lockdep_class(virq, &gpio_lock_class);
507
508         /*
509          * Can use the "simple" and not "edge" handler since it's
510          * shorter, and the AIC handles interrupts sanely.
511          */
512         irq_set_chip_and_handler(virq, &gpio_irqchip,
513                                  handle_simple_irq);
514         set_irq_flags(virq, IRQF_VALID);
515         irq_set_chip_data(virq, at91_gpio);
516
517         return 0;
518 }
519
520 static struct irq_domain_ops at91_gpio_ops = {
521         .map    = at91_gpio_irq_map,
522         .xlate  = irq_domain_xlate_twocell,
523 };
524
525 int __init at91_gpio_of_irq_setup(struct device_node *node,
526                                      struct device_node *parent)
527 {
528         struct at91_gpio_chip   *prev = NULL;
529         int                     alias_idx = of_alias_get_id(node, "gpio");
530         struct at91_gpio_chip   *at91_gpio = &gpio_chip[alias_idx];
531
532         /* Disable irqs of this PIO controller */
533         __raw_writel(~0, at91_gpio->regbase + PIO_IDR);
534
535         /* Setup irq domain */
536         at91_gpio->domain = irq_domain_add_linear(node, at91_gpio->chip.ngpio,
537                                                 &at91_gpio_ops, at91_gpio);
538         if (!at91_gpio->domain)
539                 panic("at91_gpio.%d: couldn't allocate irq domain (DT).\n",
540                         at91_gpio->pioc_idx);
541
542         /* Setup chained handler */
543         if (at91_gpio->pioc_idx)
544                 prev = &gpio_chip[at91_gpio->pioc_idx - 1];
545
546         /* The toplevel handler handles one bank of GPIOs, except
547          * on some SoC it can handles up to three...
548          * We only set up the handler for the first of the list.
549          */
550         if (prev && prev->next == at91_gpio)
551                 return 0;
552
553         at91_gpio->pioc_virq = irq_create_mapping(irq_find_host(parent),
554                                                         at91_gpio->pioc_hwirq);
555         irq_set_chip_data(at91_gpio->pioc_virq, at91_gpio);
556         irq_set_chained_handler(at91_gpio->pioc_virq, gpio_irq_handler);
557
558         return 0;
559 }
560 #else
561 int __init at91_gpio_of_irq_setup(struct device_node *node,
562                                      struct device_node *parent)
563 {
564         return -EINVAL;
565 }
566 #endif
567
568 /*
569  * irqdomain initialization: pile up irqdomains on top of AIC range
570  */
571 static void __init at91_gpio_irqdomain(struct at91_gpio_chip *at91_gpio)
572 {
573         int irq_base;
574
575         irq_base = irq_alloc_descs(-1, 0, at91_gpio->chip.ngpio, 0);
576         if (irq_base < 0)
577                 panic("at91_gpio.%d: error %d: couldn't allocate IRQ numbers.\n",
578                         at91_gpio->pioc_idx, irq_base);
579         at91_gpio->domain = irq_domain_add_legacy(NULL, at91_gpio->chip.ngpio,
580                                                   irq_base, 0,
581                                                   &irq_domain_simple_ops, NULL);
582         if (!at91_gpio->domain)
583                 panic("at91_gpio.%d: couldn't allocate irq domain.\n",
584                         at91_gpio->pioc_idx);
585 }
586
587 /*
588  * Called from the processor-specific init to enable GPIO interrupt support.
589  */
590 void __init at91_gpio_irq_setup(void)
591 {
592         unsigned                pioc;
593         int                     gpio_irqnbr = 0;
594         struct at91_gpio_chip   *this, *prev;
595
596         for (pioc = 0, this = gpio_chip, prev = NULL;
597                         pioc++ < gpio_banks;
598                         prev = this, this++) {
599                 int offset;
600
601                 __raw_writel(~0, this->regbase + PIO_IDR);
602
603                 /* setup irq domain for this GPIO controller */
604                 at91_gpio_irqdomain(this);
605
606                 for (offset = 0; offset < this->chip.ngpio; offset++) {
607                         unsigned int virq = irq_find_mapping(this->domain, offset);
608                         irq_set_lockdep_class(virq, &gpio_lock_class);
609
610                         /*
611                          * Can use the "simple" and not "edge" handler since it's
612                          * shorter, and the AIC handles interrupts sanely.
613                          */
614                         irq_set_chip_and_handler(virq, &gpio_irqchip,
615                                                  handle_simple_irq);
616                         set_irq_flags(virq, IRQF_VALID);
617                         irq_set_chip_data(virq, this);
618
619                         gpio_irqnbr++;
620                 }
621
622                 /* The toplevel handler handles one bank of GPIOs, except
623                  * on some SoC it can handles up to three...
624                  * We only set up the handler for the first of the list.
625                  */
626                 if (prev && prev->next == this)
627                         continue;
628
629                 this->pioc_virq = irq_create_mapping(NULL, this->pioc_hwirq);
630                 irq_set_chip_data(this->pioc_virq, this);
631                 irq_set_chained_handler(this->pioc_virq, gpio_irq_handler);
632         }
633         pr_info("AT91: %d gpio irqs in %d banks\n", gpio_irqnbr, gpio_banks);
634 }
635
636 /* gpiolib support */
637 static int at91_gpiolib_direction_input(struct gpio_chip *chip,
638                                         unsigned offset)
639 {
640         struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
641         void __iomem *pio = at91_gpio->regbase;
642         unsigned mask = 1 << offset;
643
644         __raw_writel(mask, pio + PIO_ODR);
645         return 0;
646 }
647
648 static int at91_gpiolib_direction_output(struct gpio_chip *chip,
649                                          unsigned offset, int val)
650 {
651         struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
652         void __iomem *pio = at91_gpio->regbase;
653         unsigned mask = 1 << offset;
654
655         __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
656         __raw_writel(mask, pio + PIO_OER);
657         return 0;
658 }
659
660 static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset)
661 {
662         struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
663         void __iomem *pio = at91_gpio->regbase;
664         unsigned mask = 1 << offset;
665         u32 pdsr;
666
667         pdsr = __raw_readl(pio + PIO_PDSR);
668         return (pdsr & mask) != 0;
669 }
670
671 static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val)
672 {
673         struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
674         void __iomem *pio = at91_gpio->regbase;
675         unsigned mask = 1 << offset;
676
677         __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
678 }
679
680 static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
681 {
682         int i;
683
684         for (i = 0; i < chip->ngpio; i++) {
685                 unsigned pin = chip->base + i;
686                 void __iomem *pio = pin_to_controller(pin);
687                 unsigned mask = pin_to_mask(pin);
688                 const char *gpio_label;
689
690                 gpio_label = gpiochip_is_requested(chip, i);
691                 if (gpio_label) {
692                         seq_printf(s, "[%s] GPIO%s%d: ",
693                                    gpio_label, chip->label, i);
694                         if (__raw_readl(pio + PIO_PSR) & mask)
695                                 seq_printf(s, "[gpio] %s\n",
696                                            at91_get_gpio_value(pin) ?
697                                            "set" : "clear");
698                         else
699                                 seq_printf(s, "[periph %s]\n",
700                                            __raw_readl(pio + PIO_ABSR) &
701                                            mask ? "B" : "A");
702                 }
703         }
704 }
705
706 static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset)
707 {
708         struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
709         int virq;
710
711         if (offset < chip->ngpio)
712                 virq = irq_create_mapping(at91_gpio->domain, offset);
713         else
714                 virq = -ENXIO;
715
716         dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
717                                 chip->label, offset + chip->base, virq);
718         return virq;
719 }
720
721 static int __init at91_gpio_setup_clk(int idx)
722 {
723         struct at91_gpio_chip *at91_gpio = &gpio_chip[idx];
724
725         /* retreive PIO controller's clock */
726         at91_gpio->clock = clk_get_sys(NULL, at91_gpio->chip.label);
727         if (IS_ERR(at91_gpio->clock)) {
728                 pr_err("at91_gpio.%d, failed to get clock, ignoring.\n", idx);
729                 goto err;
730         }
731
732         if (clk_prepare(at91_gpio->clock))
733                 goto clk_prep_err;
734
735         /* enable PIO controller's clock */
736         if (clk_enable(at91_gpio->clock)) {
737                 pr_err("at91_gpio.%d, failed to enable clock, ignoring.\n", idx);
738                 goto clk_err;
739         }
740
741         return 0;
742
743 clk_err:
744         clk_unprepare(at91_gpio->clock);
745 clk_prep_err:
746         clk_put(at91_gpio->clock);
747 err:
748         return -EINVAL;
749 }
750
751 #ifdef CONFIG_OF_GPIO
752 static void __init of_at91_gpio_init_one(struct device_node *np)
753 {
754         int alias_idx;
755         struct at91_gpio_chip *at91_gpio;
756
757         if (!np)
758                 return;
759
760         alias_idx = of_alias_get_id(np, "gpio");
761         if (alias_idx >= MAX_GPIO_BANKS) {
762                 pr_err("at91_gpio, failed alias idx(%d) > MAX_GPIO_BANKS(%d), ignoring.\n",
763                                                 alias_idx, MAX_GPIO_BANKS);
764                 return;
765         }
766
767         at91_gpio = &gpio_chip[alias_idx];
768         at91_gpio->chip.base = alias_idx * at91_gpio->chip.ngpio;
769
770         at91_gpio->regbase = of_iomap(np, 0);
771         if (!at91_gpio->regbase) {
772                 pr_err("at91_gpio.%d, failed to map registers, ignoring.\n",
773                                                                 alias_idx);
774                 return;
775         }
776
777         /* Get the interrupts property */
778         if (of_property_read_u32(np, "interrupts", &at91_gpio->pioc_hwirq)) {
779                 pr_err("at91_gpio.%d, failed to get interrupts property, ignoring.\n",
780                                                                 alias_idx);
781                 goto ioremap_err;
782         }
783
784         /* Setup clock */
785         if (at91_gpio_setup_clk(alias_idx))
786                 goto ioremap_err;
787
788         at91_gpio->chip.of_node = np;
789         gpio_banks = max(gpio_banks, alias_idx + 1);
790         at91_gpio->pioc_idx = alias_idx;
791         return;
792
793 ioremap_err:
794         iounmap(at91_gpio->regbase);
795 }
796
797 static int __init of_at91_gpio_init(void)
798 {
799         struct device_node *np = NULL;
800
801         /*
802          * This isn't ideal, but it gets things hooked up until this
803          * driver is converted into a platform_device
804          */
805         for_each_compatible_node(np, NULL, "atmel,at91rm9200-gpio")
806                 of_at91_gpio_init_one(np);
807
808         return gpio_banks > 0 ? 0 : -EINVAL;
809 }
810 #else
811 static int __init of_at91_gpio_init(void)
812 {
813         return -EINVAL;
814 }
815 #endif
816
817 static void __init at91_gpio_init_one(int idx, u32 regbase, int pioc_hwirq)
818 {
819         struct at91_gpio_chip *at91_gpio = &gpio_chip[idx];
820
821         at91_gpio->chip.base = idx * at91_gpio->chip.ngpio;
822         at91_gpio->pioc_hwirq = pioc_hwirq;
823         at91_gpio->pioc_idx = idx;
824
825         at91_gpio->regbase = ioremap(regbase, 512);
826         if (!at91_gpio->regbase) {
827                 pr_err("at91_gpio.%d, failed to map registers, ignoring.\n", idx);
828                 return;
829         }
830
831         if (at91_gpio_setup_clk(idx))
832                 goto ioremap_err;
833
834         gpio_banks = max(gpio_banks, idx + 1);
835         return;
836
837 ioremap_err:
838         iounmap(at91_gpio->regbase);
839 }
840
841 /*
842  * Called from the processor-specific init to enable GPIO pin support.
843  */
844 void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
845 {
846         unsigned i;
847         struct at91_gpio_chip *at91_gpio, *last = NULL;
848
849         BUG_ON(nr_banks > MAX_GPIO_BANKS);
850
851         if (of_at91_gpio_init() < 0) {
852                 /* No GPIO controller found in device tree */
853                 for (i = 0; i < nr_banks; i++)
854                         at91_gpio_init_one(i, data[i].regbase, data[i].id);
855         }
856
857         for (i = 0; i < gpio_banks; i++) {
858                 at91_gpio = &gpio_chip[i];
859
860                 /*
861                  * GPIO controller are grouped on some SoC:
862                  * PIOC, PIOD and PIOE can share the same IRQ line
863                  */
864                 if (last && last->pioc_hwirq == at91_gpio->pioc_hwirq)
865                         last->next = at91_gpio;
866                 last = at91_gpio;
867
868                 gpiochip_add(&at91_gpio->chip);
869         }
870 }