blob: 567df654a2e104bd9ea83162c39f584b648f5e09 [file] [log] [blame]
SAN People73a59c12006-01-09 17:05:41 +00001/*
Andrew Victor9d041262007-02-05 11:42:07 +01002 * linux/arch/arm/mach-at91/gpio.c
SAN People73a59c12006-01-09 17:05:41 +00003 *
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
Andrew Victorf2173832006-09-27 13:23:00 +010012#include <linux/clk.h>
SAN People73a59c12006-01-09 17:05:41 +000013#include <linux/errno.h>
Nicolas Ferreb134ce82012-02-11 15:56:01 +010014#include <linux/device.h>
Russell King2f8163b2011-07-26 10:53:52 +010015#include <linux/gpio.h>
Thomas Gleixner07d265d2006-07-01 23:01:50 +010016#include <linux/interrupt.h>
17#include <linux/irq.h>
Andrew Victorb66545e2007-11-23 16:09:10 +010018#include <linux/debugfs.h>
19#include <linux/seq_file.h>
SAN People73a59c12006-01-09 17:05:41 +000020#include <linux/kernel.h>
21#include <linux/list.h>
22#include <linux/module.h>
Russell Kingfced80c2008-09-06 12:10:45 +010023#include <linux/io.h>
Nicolas Ferre21f81872012-02-11 15:41:40 +010024#include <linux/irqdomain.h>
25#include <linux/of_address.h>
26#include <linux/of_irq.h>
Nicolas Ferre8014d6f42012-02-14 18:08:14 +010027#include <linux/of_gpio.h>
SAN People73a59c12006-01-09 17:05:41 +000028
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/hardware.h>
30#include <mach/at91_pio.h>
SAN People73a59c12006-01-09 17:05:41 +000031
Andrew Victorf2173832006-09-27 13:23:00 +010032#include "generic.h"
33
Ryan Mallonf373e8c2009-02-10 21:02:08 +010034struct at91_gpio_chip {
35 struct gpio_chip chip;
36 struct at91_gpio_chip *next; /* Bank sharing same clock */
Nicolas Ferre4340cde2012-02-11 15:28:08 +010037 int pioc_hwirq; /* PIO bank interrupt identifier on AIC */
Nicolas Ferre8014d6f42012-02-14 18:08:14 +010038 int pioc_virq; /* PIO bank Linux virtual interrupt */
Nicolas Ferre21f81872012-02-11 15:41:40 +010039 int pioc_idx; /* PIO bank index */
Nicolas Ferre4340cde2012-02-11 15:28:08 +010040 void __iomem *regbase; /* PIO bank virtual address */
Jean-Christophe PLAGNIOL-VILLARD619d4a42011-11-13 13:00:58 +080041 struct clk *clock; /* associated clock */
Nicolas Ferre21f81872012-02-11 15:41:40 +010042 struct irq_domain *domain; /* associated irq domain */
Ryan Mallonf373e8c2009-02-10 21:02:08 +010043};
Andrew Victorf2173832006-09-27 13:23:00 +010044
Ryan Mallonf373e8c2009-02-10 21:02:08 +010045#define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
46
47static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip);
48static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val);
49static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset);
50static int at91_gpiolib_direction_output(struct gpio_chip *chip,
51 unsigned offset, int val);
52static int at91_gpiolib_direction_input(struct gpio_chip *chip,
53 unsigned offset);
Nicolas Ferreb134ce82012-02-11 15:56:01 +010054static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset);
Ryan Mallonf373e8c2009-02-10 21:02:08 +010055
Nicolas Ferre7530cd92011-12-08 15:35:22 +010056#define AT91_GPIO_CHIP(name, nr_gpio) \
Ryan Mallonf373e8c2009-02-10 21:02:08 +010057 { \
58 .chip = { \
59 .label = name, \
Ryan Mallonf373e8c2009-02-10 21:02:08 +010060 .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, \
Nicolas Ferreb134ce82012-02-11 15:56:01 +010065 .to_irq = at91_gpiolib_to_irq, \
Ryan Mallonf373e8c2009-02-10 21:02:08 +010066 .ngpio = nr_gpio, \
67 }, \
68 }
69
70static struct at91_gpio_chip gpio_chip[] = {
Nicolas Ferre7530cd92011-12-08 15:35:22 +010071 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),
Ryan Mallonf373e8c2009-02-10 21:02:08 +010076};
77
Andrew Victorf2173832006-09-27 13:23:00 +010078static int gpio_banks;
79
SAN People73a59c12006-01-09 17:05:41 +000080static inline void __iomem *pin_to_controller(unsigned pin)
81{
SAN People73a59c12006-01-09 17:05:41 +000082 pin /= 32;
Andrew Victorf2173832006-09-27 13:23:00 +010083 if (likely(pin < gpio_banks))
Ryan Mallonf373e8c2009-02-10 21:02:08 +010084 return gpio_chip[pin].regbase;
SAN People73a59c12006-01-09 17:05:41 +000085
86 return NULL;
87}
88
89static inline unsigned pin_to_mask(unsigned pin)
90{
SAN People73a59c12006-01-09 17:05:41 +000091 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/*
David Brownella31c4ee2007-02-12 00:53:13 -0800112 * mux the pin to the "GPIO" peripheral role.
113 */
114int __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}
126EXPORT_SYMBOL(at91_set_GPIO_periph);
127
128
129/*
SAN People73a59c12006-01-09 17:05:41 +0000130 * mux the pin to the "A" internal peripheral role.
131 */
132int __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}
146EXPORT_SYMBOL(at91_set_A_periph);
147
148
149/*
150 * mux the pin to the "B" internal peripheral role.
151 */
152int __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}
166EXPORT_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 */
173int __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}
187EXPORT_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 */
194int __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}
209EXPORT_SYMBOL(at91_set_gpio_output);
210
211
212/*
213 * enable/disable the glitch filter; mostly used with IRQ handling.
214 */
215int __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}
225EXPORT_SYMBOL(at91_set_deglitch);
226
Andrew Victordf666b92006-02-22 21:23:35 +0000227/*
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 */
231int __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}
242EXPORT_SYMBOL(at91_set_multi_drive);
243
SAN People73a59c12006-01-09 17:05:41 +0000244/*
245 * assuming the pin is muxed as a gpio output, set its value.
246 */
247int 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}
257EXPORT_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 */
263int 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}
274EXPORT_SYMBOL(at91_get_gpio_value);
275
276/*--------------------------------------------------------------------------*/
277
Andrew Victor814138f2006-06-19 15:26:54 +0100278#ifdef CONFIG_PM
279
Andrew Victorf2173832006-09-27 13:23:00 +0100280static u32 wakeups[MAX_GPIO_BANKS];
281static u32 backups[MAX_GPIO_BANKS];
Andrew Victor814138f2006-06-19 15:26:54 +0100282
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100283static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
Andrew Victor814138f2006-06-19 15:26:54 +0100284{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100285 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;
Andrew Victor814138f2006-06-19 15:26:54 +0100288
Andrew Victor3ea163e2007-01-09 13:47:29 +0100289 if (unlikely(bank >= MAX_GPIO_BANKS))
Andrew Victor814138f2006-06-19 15:26:54 +0100290 return -EINVAL;
291
292 if (state)
Andrew Victor3ea163e2007-01-09 13:47:29 +0100293 wakeups[bank] |= mask;
Andrew Victor814138f2006-06-19 15:26:54 +0100294 else
Andrew Victor3ea163e2007-01-09 13:47:29 +0100295 wakeups[bank] &= ~mask;
296
Nicolas Ferre8014d6f42012-02-14 18:08:14 +0100297 irq_set_irq_wake(at91_gpio->pioc_virq, state);
Andrew Victor814138f2006-06-19 15:26:54 +0100298
299 return 0;
300}
301
302void at91_gpio_suspend(void)
303{
304 int i;
305
Andrew Victorf2173832006-09-27 13:23:00 +0100306 for (i = 0; i < gpio_banks; i++) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100307 void __iomem *pio = gpio_chip[i].regbase;
Andrew Victor814138f2006-06-19 15:26:54 +0100308
David Brownelle83aff52008-01-04 18:30:24 +0100309 backups[i] = __raw_readl(pio + PIO_IMR);
310 __raw_writel(backups[i], pio + PIO_IDR);
311 __raw_writel(wakeups[i], pio + PIO_IER);
Andrew Victor814138f2006-06-19 15:26:54 +0100312
Nicolas Ferre21f81872012-02-11 15:41:40 +0100313 if (!wakeups[i]) {
314 clk_unprepare(gpio_chip[i].clock);
Jean-Christophe PLAGNIOL-VILLARD619d4a42011-11-13 13:00:58 +0800315 clk_disable(gpio_chip[i].clock);
Nicolas Ferre21f81872012-02-11 15:41:40 +0100316 } else {
Andrew Victor814138f2006-06-19 15:26:54 +0100317#ifdef CONFIG_PM_DEBUG
Andrew Victor3ea163e2007-01-09 13:47:29 +0100318 printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
Andrew Victor814138f2006-06-19 15:26:54 +0100319#endif
320 }
321 }
322}
323
324void at91_gpio_resume(void)
325{
326 int i;
327
Andrew Victorf2173832006-09-27 13:23:00 +0100328 for (i = 0; i < gpio_banks; i++) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100329 void __iomem *pio = gpio_chip[i].regbase;
Andrew Victor814138f2006-06-19 15:26:54 +0100330
Nicolas Ferre21f81872012-02-11 15:41:40 +0100331 if (!wakeups[i]) {
332 if (clk_prepare(gpio_chip[i].clock) == 0)
333 clk_enable(gpio_chip[i].clock);
334 }
Andrew Victor3ea163e2007-01-09 13:47:29 +0100335
David Brownelle83aff52008-01-04 18:30:24 +0100336 __raw_writel(wakeups[i], pio + PIO_IDR);
337 __raw_writel(backups[i], pio + PIO_IER);
Andrew Victorf2173832006-09-27 13:23:00 +0100338 }
Andrew Victor814138f2006-06-19 15:26:54 +0100339}
340
341#else
342#define gpio_irq_set_wake NULL
343#endif
344
SAN People73a59c12006-01-09 17:05:41 +0000345
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
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100357static void gpio_irq_mask(struct irq_data *d)
SAN People73a59c12006-01-09 17:05:41 +0000358{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100359 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;
SAN People73a59c12006-01-09 17:05:41 +0000362
363 if (pio)
364 __raw_writel(mask, pio + PIO_IDR);
365}
366
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100367static void gpio_irq_unmask(struct irq_data *d)
SAN People73a59c12006-01-09 17:05:41 +0000368{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100369 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;
SAN People73a59c12006-01-09 17:05:41 +0000372
373 if (pio)
374 __raw_writel(mask, pio + PIO_IER);
375}
376
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100377static int gpio_irq_type(struct irq_data *d, unsigned type)
SAN People73a59c12006-01-09 17:05:41 +0000378{
David Brownelle83aff52008-01-04 18:30:24 +0100379 switch (type) {
380 case IRQ_TYPE_NONE:
381 case IRQ_TYPE_EDGE_BOTH:
382 return 0;
383 default:
384 return -EINVAL;
385 }
SAN People73a59c12006-01-09 17:05:41 +0000386}
387
David Brownell38c677c2006-08-01 22:26:25 +0100388static struct irq_chip gpio_irqchip = {
389 .name = "GPIO",
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100390 .irq_disable = gpio_irq_mask,
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100391 .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,
SAN People73a59c12006-01-09 17:05:41 +0000395};
396
Russell King10dd5ce2006-11-23 11:41:32 +0000397static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
SAN People73a59c12006-01-09 17:05:41 +0000398{
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100399 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;
Nicolas Ferre8014d6f42012-02-14 18:08:14 +0100403 unsigned long isr;
404 int n;
SAN People73a59c12006-01-09 17:05:41 +0000405
SAN People73a59c12006-01-09 17:05:41 +0000406 /* temporarily mask (level sensitive) parent IRQ */
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100407 chip->irq_ack(idata);
SAN People73a59c12006-01-09 17:05:41 +0000408 for (;;) {
David Brownelle83aff52008-01-04 18:30:24 +0100409 /* 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 */
SAN People73a59c12006-01-09 17:05:41 +0000413 isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
David Brownelle83aff52008-01-04 18:30:24 +0100414 if (!isr) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100415 if (!at91_gpio->next)
David Brownelle83aff52008-01-04 18:30:24 +0100416 break;
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100417 at91_gpio = at91_gpio->next;
418 pio = at91_gpio->regbase;
David Brownelle83aff52008-01-04 18:30:24 +0100419 continue;
420 }
SAN People73a59c12006-01-09 17:05:41 +0000421
Nicolas Ferre8014d6f42012-02-14 18:08:14 +0100422 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);
SAN People73a59c12006-01-09 17:05:41 +0000426 }
427 }
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100428 chip->irq_unmask(idata);
SAN People73a59c12006-01-09 17:05:41 +0000429 /* now it may re-trigger */
430}
431
Andrew Victorf2173832006-09-27 13:23:00 +0100432/*--------------------------------------------------------------------------*/
SAN People73a59c12006-01-09 17:05:41 +0000433
Andrew Victorb66545e2007-11-23 16:09:10 +0100434#ifdef CONFIG_DEBUG_FS
435
436static 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++) {
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800452 unsigned pin = (32 * bank) + j;
Andrew Victorb66545e2007-11-23 16:09:10 +0100453 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
470static int at91_gpio_open(struct inode *inode, struct file *file)
471{
472 return single_open(file, at91_gpio_show, NULL);
473}
474
475static 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
482static 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}
488postcore_initcall(at91_gpio_debugfs_init);
489
490#endif
491
492/*--------------------------------------------------------------------------*/
493
Andrew Victor2b768b62009-02-11 21:39:05 +0100494/*
Nicolas Ferre8014d6f42012-02-14 18:08:14 +0100495 * 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 */
498static struct lock_class_key gpio_lock_class;
499
500#if defined(CONFIG_OF)
501static 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
520static struct irq_domain_ops at91_gpio_ops = {
521 .map = at91_gpio_irq_map,
522 .xlate = irq_domain_xlate_twocell,
523};
524
525int __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
561int __init at91_gpio_of_irq_setup(struct device_node *node,
562 struct device_node *parent)
563{
564 return -EINVAL;
565}
566#endif
567
568/*
Nicolas Ferre21f81872012-02-11 15:41:40 +0100569 * irqdomain initialization: pile up irqdomains on top of AIC range
570 */
571static 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);
Nicolas Ferre8014d6f42012-02-14 18:08:14 +0100579 at91_gpio->domain = irq_domain_add_legacy(NULL, at91_gpio->chip.ngpio,
Nicolas Ferre21f81872012-02-11 15:41:40 +0100580 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/*
Andrew Victorf2173832006-09-27 13:23:00 +0100588 * Called from the processor-specific init to enable GPIO interrupt support.
589 */
590void __init at91_gpio_irq_setup(void)
591{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100592 unsigned pioc;
593 int gpio_irqnbr = 0;
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100594 struct at91_gpio_chip *this, *prev;
Andrew Victorf2173832006-09-27 13:23:00 +0100595
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800596 for (pioc = 0, this = gpio_chip, prev = NULL;
David Brownelle83aff52008-01-04 18:30:24 +0100597 pioc++ < gpio_banks;
598 prev = this, this++) {
Nicolas Ferre8014d6f42012-02-14 18:08:14 +0100599 int offset;
SAN People73a59c12006-01-09 17:05:41 +0000600
David Brownelle83aff52008-01-04 18:30:24 +0100601 __raw_writel(~0, this->regbase + PIO_IDR);
SAN People73a59c12006-01-09 17:05:41 +0000602
Nicolas Ferre21f81872012-02-11 15:41:40 +0100603 /* 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);
David Brownell37aca702008-03-05 00:08:29 +0100609
Andrew Victor814138f2006-06-19 15:26:54 +0100610 /*
611 * Can use the "simple" and not "edge" handler since it's
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200612 * shorter, and the AIC handles interrupts sanely.
Andrew Victor814138f2006-06-19 15:26:54 +0100613 */
Nicolas Ferre21f81872012-02-11 15:41:40 +0100614 irq_set_chip_and_handler(virq, &gpio_irqchip,
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100615 handle_simple_irq);
Nicolas Ferre21f81872012-02-11 15:41:40 +0100616 set_irq_flags(virq, IRQF_VALID);
617 irq_set_chip_data(virq, this);
618
619 gpio_irqnbr++;
SAN People73a59c12006-01-09 17:05:41 +0000620 }
621
David Brownelle83aff52008-01-04 18:30:24 +0100622 /* The toplevel handler handles one bank of GPIOs, except
Nicolas Ferre4340cde2012-02-11 15:28:08 +0100623 * on some SoC it can handles up to three...
624 * We only set up the handler for the first of the list.
David Brownelle83aff52008-01-04 18:30:24 +0100625 */
626 if (prev && prev->next == this)
627 continue;
628
Nicolas Ferre8014d6f42012-02-14 18:08:14 +0100629 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);
SAN People73a59c12006-01-09 17:05:41 +0000632 }
Nicolas Ferre21f81872012-02-11 15:41:40 +0100633 pr_info("AT91: %d gpio irqs in %d banks\n", gpio_irqnbr, gpio_banks);
Andrew Victorf2173832006-09-27 13:23:00 +0100634}
635
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100636/* gpiolib support */
637static 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
648static 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
660static 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
671static 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
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100680static 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
Nicolas Ferreb134ce82012-02-11 15:56:01 +0100706static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset)
707{
708 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
Nicolas Ferre8014d6f42012-02-14 18:08:14 +0100709 int virq;
710
711 if (offset < chip->ngpio)
712 virq = irq_create_mapping(at91_gpio->domain, offset);
713 else
714 virq = -ENXIO;
Nicolas Ferreb134ce82012-02-11 15:56:01 +0100715
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
Nicolas Ferre21f81872012-02-11 15:41:40 +0100721static 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
743clk_err:
744 clk_unprepare(at91_gpio->clock);
745clk_prep_err:
746 clk_put(at91_gpio->clock);
747err:
748 return -EINVAL;
749}
750
751#ifdef CONFIG_OF_GPIO
752static 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
793ioremap_err:
794 iounmap(at91_gpio->regbase);
795}
796
797static 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
811static int __init of_at91_gpio_init(void)
812{
813 return -EINVAL;
814}
815#endif
816
817static 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
837ioremap_err:
838 iounmap(at91_gpio->regbase);
839}
840
Andrew Victorf2173832006-09-27 13:23:00 +0100841/*
842 * Called from the processor-specific init to enable GPIO pin support.
843 */
844void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
845{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100846 unsigned i;
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100847 struct at91_gpio_chip *at91_gpio, *last = NULL;
David Brownelle83aff52008-01-04 18:30:24 +0100848
Andrew Victorf2173832006-09-27 13:23:00 +0100849 BUG_ON(nr_banks > MAX_GPIO_BANKS);
850
Nicolas Ferre21f81872012-02-11 15:41:40 +0100851 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 }
David Brownelle83aff52008-01-04 18:30:24 +0100856
Nicolas Ferre21f81872012-02-11 15:41:40 +0100857 for (i = 0; i < gpio_banks; i++) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100858 at91_gpio = &gpio_chip[i];
859
Nicolas Ferre4340cde2012-02-11 15:28:08 +0100860 /*
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)
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100865 last->next = at91_gpio;
866 last = at91_gpio;
867
868 gpiochip_add(&at91_gpio->chip);
David Brownelle83aff52008-01-04 18:30:24 +0100869 }
SAN People73a59c12006-01-09 17:05:41 +0000870}