blob: e8f5831e573d37a8a00a41b60bea5b6ef02c3efd [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>
SAN People73a59c12006-01-09 17:05:41 +000027
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/hardware.h>
29#include <mach/at91_pio.h>
SAN People73a59c12006-01-09 17:05:41 +000030
Andrew Victorf2173832006-09-27 13:23:00 +010031#include "generic.h"
32
Ryan Mallonf373e8c2009-02-10 21:02:08 +010033struct at91_gpio_chip {
34 struct gpio_chip chip;
35 struct at91_gpio_chip *next; /* Bank sharing same clock */
Nicolas Ferre4340cde2012-02-11 15:28:08 +010036 int pioc_hwirq; /* PIO bank interrupt identifier on AIC */
Nicolas Ferre21f81872012-02-11 15:41:40 +010037 int pioc_idx; /* PIO bank index */
Nicolas Ferre4340cde2012-02-11 15:28:08 +010038 void __iomem *regbase; /* PIO bank virtual address */
Jean-Christophe PLAGNIOL-VILLARD619d4a42011-11-13 13:00:58 +080039 struct clk *clock; /* associated clock */
Nicolas Ferre21f81872012-02-11 15:41:40 +010040 struct irq_domain *domain; /* associated irq domain */
Ryan Mallonf373e8c2009-02-10 21:02:08 +010041};
Andrew Victorf2173832006-09-27 13:23:00 +010042
Ryan Mallonf373e8c2009-02-10 21:02:08 +010043#define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
44
45static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip);
46static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val);
47static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset);
48static int at91_gpiolib_direction_output(struct gpio_chip *chip,
49 unsigned offset, int val);
50static int at91_gpiolib_direction_input(struct gpio_chip *chip,
51 unsigned offset);
Nicolas Ferreb134ce82012-02-11 15:56:01 +010052static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset);
Ryan Mallonf373e8c2009-02-10 21:02:08 +010053
54#define AT91_GPIO_CHIP(name, base_gpio, nr_gpio) \
55 { \
56 .chip = { \
57 .label = name, \
Ryan Mallonf373e8c2009-02-10 21:02:08 +010058 .direction_input = at91_gpiolib_direction_input, \
59 .direction_output = at91_gpiolib_direction_output, \
60 .get = at91_gpiolib_get, \
61 .set = at91_gpiolib_set, \
62 .dbg_show = at91_gpiolib_dbg_show, \
63 .base = base_gpio, \
Nicolas Ferreb134ce82012-02-11 15:56:01 +010064 .to_irq = at91_gpiolib_to_irq, \
Ryan Mallonf373e8c2009-02-10 21:02:08 +010065 .ngpio = nr_gpio, \
66 }, \
67 }
68
69static struct at91_gpio_chip gpio_chip[] = {
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +080070 AT91_GPIO_CHIP("pioA", 0x00, 32),
71 AT91_GPIO_CHIP("pioB", 0x20, 32),
72 AT91_GPIO_CHIP("pioC", 0x40, 32),
73 AT91_GPIO_CHIP("pioD", 0x60, 32),
74 AT91_GPIO_CHIP("pioE", 0x80, 32),
Ryan Mallonf373e8c2009-02-10 21:02:08 +010075};
76
Andrew Victorf2173832006-09-27 13:23:00 +010077static int gpio_banks;
78
SAN People73a59c12006-01-09 17:05:41 +000079static inline void __iomem *pin_to_controller(unsigned pin)
80{
SAN People73a59c12006-01-09 17:05:41 +000081 pin /= 32;
Andrew Victorf2173832006-09-27 13:23:00 +010082 if (likely(pin < gpio_banks))
Ryan Mallonf373e8c2009-02-10 21:02:08 +010083 return gpio_chip[pin].regbase;
SAN People73a59c12006-01-09 17:05:41 +000084
85 return NULL;
86}
87
88static inline unsigned pin_to_mask(unsigned pin)
89{
SAN People73a59c12006-01-09 17:05:41 +000090 return 1 << (pin % 32);
91}
92
93
94/*--------------------------------------------------------------------------*/
95
96/* Not all hardware capabilities are exposed through these calls; they
97 * only encapsulate the most common features and modes. (So if you
98 * want to change signals in groups, do it directly.)
99 *
100 * Bootloaders will usually handle some of the pin multiplexing setup.
101 * The intent is certainly that by the time Linux is fully booted, all
102 * pins should have been fully initialized. These setup calls should
103 * only be used by board setup routines, or possibly in driver probe().
104 *
105 * For bootloaders doing all that setup, these calls could be inlined
106 * as NOPs so Linux won't duplicate any setup code
107 */
108
109
110/*
David Brownella31c4ee2007-02-12 00:53:13 -0800111 * mux the pin to the "GPIO" peripheral role.
112 */
113int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup)
114{
115 void __iomem *pio = pin_to_controller(pin);
116 unsigned mask = pin_to_mask(pin);
117
118 if (!pio)
119 return -EINVAL;
120 __raw_writel(mask, pio + PIO_IDR);
121 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
122 __raw_writel(mask, pio + PIO_PER);
123 return 0;
124}
125EXPORT_SYMBOL(at91_set_GPIO_periph);
126
127
128/*
SAN People73a59c12006-01-09 17:05:41 +0000129 * mux the pin to the "A" internal peripheral role.
130 */
131int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
132{
133 void __iomem *pio = pin_to_controller(pin);
134 unsigned mask = pin_to_mask(pin);
135
136 if (!pio)
137 return -EINVAL;
138
139 __raw_writel(mask, pio + PIO_IDR);
140 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
141 __raw_writel(mask, pio + PIO_ASR);
142 __raw_writel(mask, pio + PIO_PDR);
143 return 0;
144}
145EXPORT_SYMBOL(at91_set_A_periph);
146
147
148/*
149 * mux the pin to the "B" internal peripheral role.
150 */
151int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
152{
153 void __iomem *pio = pin_to_controller(pin);
154 unsigned mask = pin_to_mask(pin);
155
156 if (!pio)
157 return -EINVAL;
158
159 __raw_writel(mask, pio + PIO_IDR);
160 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
161 __raw_writel(mask, pio + PIO_BSR);
162 __raw_writel(mask, pio + PIO_PDR);
163 return 0;
164}
165EXPORT_SYMBOL(at91_set_B_periph);
166
167
168/*
169 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
170 * configure it for an input.
171 */
172int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
173{
174 void __iomem *pio = pin_to_controller(pin);
175 unsigned mask = pin_to_mask(pin);
176
177 if (!pio)
178 return -EINVAL;
179
180 __raw_writel(mask, pio + PIO_IDR);
181 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
182 __raw_writel(mask, pio + PIO_ODR);
183 __raw_writel(mask, pio + PIO_PER);
184 return 0;
185}
186EXPORT_SYMBOL(at91_set_gpio_input);
187
188
189/*
190 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
191 * and configure it for an output.
192 */
193int __init_or_module at91_set_gpio_output(unsigned pin, int value)
194{
195 void __iomem *pio = pin_to_controller(pin);
196 unsigned mask = pin_to_mask(pin);
197
198 if (!pio)
199 return -EINVAL;
200
201 __raw_writel(mask, pio + PIO_IDR);
202 __raw_writel(mask, pio + PIO_PUDR);
203 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
204 __raw_writel(mask, pio + PIO_OER);
205 __raw_writel(mask, pio + PIO_PER);
206 return 0;
207}
208EXPORT_SYMBOL(at91_set_gpio_output);
209
210
211/*
212 * enable/disable the glitch filter; mostly used with IRQ handling.
213 */
214int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
215{
216 void __iomem *pio = pin_to_controller(pin);
217 unsigned mask = pin_to_mask(pin);
218
219 if (!pio)
220 return -EINVAL;
221 __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
222 return 0;
223}
224EXPORT_SYMBOL(at91_set_deglitch);
225
Andrew Victordf666b92006-02-22 21:23:35 +0000226/*
227 * enable/disable the multi-driver; This is only valid for output and
228 * allows the output pin to run as an open collector output.
229 */
230int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
231{
232 void __iomem *pio = pin_to_controller(pin);
233 unsigned mask = pin_to_mask(pin);
234
235 if (!pio)
236 return -EINVAL;
237
238 __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
239 return 0;
240}
241EXPORT_SYMBOL(at91_set_multi_drive);
242
SAN People73a59c12006-01-09 17:05:41 +0000243/*
244 * assuming the pin is muxed as a gpio output, set its value.
245 */
246int at91_set_gpio_value(unsigned pin, int value)
247{
248 void __iomem *pio = pin_to_controller(pin);
249 unsigned mask = pin_to_mask(pin);
250
251 if (!pio)
252 return -EINVAL;
253 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
254 return 0;
255}
256EXPORT_SYMBOL(at91_set_gpio_value);
257
258
259/*
260 * read the pin's value (works even if it's not muxed as a gpio).
261 */
262int at91_get_gpio_value(unsigned pin)
263{
264 void __iomem *pio = pin_to_controller(pin);
265 unsigned mask = pin_to_mask(pin);
266 u32 pdsr;
267
268 if (!pio)
269 return -EINVAL;
270 pdsr = __raw_readl(pio + PIO_PDSR);
271 return (pdsr & mask) != 0;
272}
273EXPORT_SYMBOL(at91_get_gpio_value);
274
275/*--------------------------------------------------------------------------*/
276
Andrew Victor814138f2006-06-19 15:26:54 +0100277#ifdef CONFIG_PM
278
Andrew Victorf2173832006-09-27 13:23:00 +0100279static u32 wakeups[MAX_GPIO_BANKS];
280static u32 backups[MAX_GPIO_BANKS];
Andrew Victor814138f2006-06-19 15:26:54 +0100281
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100282static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
Andrew Victor814138f2006-06-19 15:26:54 +0100283{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100284 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
285 unsigned mask = 1 << d->hwirq;
286 unsigned bank = at91_gpio->pioc_idx;
Andrew Victor814138f2006-06-19 15:26:54 +0100287
Andrew Victor3ea163e2007-01-09 13:47:29 +0100288 if (unlikely(bank >= MAX_GPIO_BANKS))
Andrew Victor814138f2006-06-19 15:26:54 +0100289 return -EINVAL;
290
291 if (state)
Andrew Victor3ea163e2007-01-09 13:47:29 +0100292 wakeups[bank] |= mask;
Andrew Victor814138f2006-06-19 15:26:54 +0100293 else
Andrew Victor3ea163e2007-01-09 13:47:29 +0100294 wakeups[bank] &= ~mask;
295
Nicolas Ferre4340cde2012-02-11 15:28:08 +0100296 irq_set_irq_wake(gpio_chip[bank].pioc_hwirq, state);
Andrew Victor814138f2006-06-19 15:26:54 +0100297
298 return 0;
299}
300
301void at91_gpio_suspend(void)
302{
303 int i;
304
Andrew Victorf2173832006-09-27 13:23:00 +0100305 for (i = 0; i < gpio_banks; i++) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100306 void __iomem *pio = gpio_chip[i].regbase;
Andrew Victor814138f2006-06-19 15:26:54 +0100307
David Brownelle83aff52008-01-04 18:30:24 +0100308 backups[i] = __raw_readl(pio + PIO_IMR);
309 __raw_writel(backups[i], pio + PIO_IDR);
310 __raw_writel(wakeups[i], pio + PIO_IER);
Andrew Victor814138f2006-06-19 15:26:54 +0100311
Nicolas Ferre21f81872012-02-11 15:41:40 +0100312 if (!wakeups[i]) {
313 clk_unprepare(gpio_chip[i].clock);
Jean-Christophe PLAGNIOL-VILLARD619d4a42011-11-13 13:00:58 +0800314 clk_disable(gpio_chip[i].clock);
Nicolas Ferre21f81872012-02-11 15:41:40 +0100315 } else {
Andrew Victor814138f2006-06-19 15:26:54 +0100316#ifdef CONFIG_PM_DEBUG
Andrew Victor3ea163e2007-01-09 13:47:29 +0100317 printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
Andrew Victor814138f2006-06-19 15:26:54 +0100318#endif
319 }
320 }
321}
322
323void at91_gpio_resume(void)
324{
325 int i;
326
Andrew Victorf2173832006-09-27 13:23:00 +0100327 for (i = 0; i < gpio_banks; i++) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100328 void __iomem *pio = gpio_chip[i].regbase;
Andrew Victor814138f2006-06-19 15:26:54 +0100329
Nicolas Ferre21f81872012-02-11 15:41:40 +0100330 if (!wakeups[i]) {
331 if (clk_prepare(gpio_chip[i].clock) == 0)
332 clk_enable(gpio_chip[i].clock);
333 }
Andrew Victor3ea163e2007-01-09 13:47:29 +0100334
David Brownelle83aff52008-01-04 18:30:24 +0100335 __raw_writel(wakeups[i], pio + PIO_IDR);
336 __raw_writel(backups[i], pio + PIO_IER);
Andrew Victorf2173832006-09-27 13:23:00 +0100337 }
Andrew Victor814138f2006-06-19 15:26:54 +0100338}
339
340#else
341#define gpio_irq_set_wake NULL
342#endif
343
SAN People73a59c12006-01-09 17:05:41 +0000344
345/* Several AIC controller irqs are dispatched through this GPIO handler.
346 * To use any AT91_PIN_* as an externally triggered IRQ, first call
347 * at91_set_gpio_input() then maybe enable its glitch filter.
348 * Then just request_irq() with the pin ID; it works like any ARM IRQ
349 * handler, though it always triggers on rising and falling edges.
350 *
351 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
352 * configuring them with at91_set_a_periph() or at91_set_b_periph().
353 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
354 */
355
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100356static void gpio_irq_mask(struct irq_data *d)
SAN People73a59c12006-01-09 17:05:41 +0000357{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100358 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
359 void __iomem *pio = at91_gpio->regbase;
360 unsigned mask = 1 << d->hwirq;
SAN People73a59c12006-01-09 17:05:41 +0000361
362 if (pio)
363 __raw_writel(mask, pio + PIO_IDR);
364}
365
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100366static void gpio_irq_unmask(struct irq_data *d)
SAN People73a59c12006-01-09 17:05:41 +0000367{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100368 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
369 void __iomem *pio = at91_gpio->regbase;
370 unsigned mask = 1 << d->hwirq;
SAN People73a59c12006-01-09 17:05:41 +0000371
372 if (pio)
373 __raw_writel(mask, pio + PIO_IER);
374}
375
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100376static int gpio_irq_type(struct irq_data *d, unsigned type)
SAN People73a59c12006-01-09 17:05:41 +0000377{
David Brownelle83aff52008-01-04 18:30:24 +0100378 switch (type) {
379 case IRQ_TYPE_NONE:
380 case IRQ_TYPE_EDGE_BOTH:
381 return 0;
382 default:
383 return -EINVAL;
384 }
SAN People73a59c12006-01-09 17:05:41 +0000385}
386
David Brownell38c677c2006-08-01 22:26:25 +0100387static struct irq_chip gpio_irqchip = {
388 .name = "GPIO",
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100389 .irq_disable = gpio_irq_mask,
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100390 .irq_mask = gpio_irq_mask,
391 .irq_unmask = gpio_irq_unmask,
392 .irq_set_type = gpio_irq_type,
393 .irq_set_wake = gpio_irq_set_wake,
SAN People73a59c12006-01-09 17:05:41 +0000394};
395
Russell King10dd5ce2006-11-23 11:41:32 +0000396static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
SAN People73a59c12006-01-09 17:05:41 +0000397{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100398 unsigned virq;
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;
SAN People73a59c12006-01-09 17:05:41 +0000403 u32 isr;
404
SAN People73a59c12006-01-09 17:05:41 +0000405 /* temporarily mask (level sensitive) parent IRQ */
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100406 chip->irq_ack(idata);
SAN People73a59c12006-01-09 17:05:41 +0000407 for (;;) {
David Brownelle83aff52008-01-04 18:30:24 +0100408 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
409 * When there none are pending, we're finished unless we need
410 * to process multiple banks (like ID_PIOCDE on sam9263).
411 */
SAN People73a59c12006-01-09 17:05:41 +0000412 isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
David Brownelle83aff52008-01-04 18:30:24 +0100413 if (!isr) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100414 if (!at91_gpio->next)
David Brownelle83aff52008-01-04 18:30:24 +0100415 break;
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100416 at91_gpio = at91_gpio->next;
417 pio = at91_gpio->regbase;
David Brownelle83aff52008-01-04 18:30:24 +0100418 continue;
419 }
SAN People73a59c12006-01-09 17:05:41 +0000420
Nicolas Ferre21f81872012-02-11 15:41:40 +0100421 virq = gpio_to_irq(at91_gpio->chip.base);
SAN People73a59c12006-01-09 17:05:41 +0000422
423 while (isr) {
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100424 if (isr & 1)
Nicolas Ferre21f81872012-02-11 15:41:40 +0100425 generic_handle_irq(virq);
426 virq++;
SAN People73a59c12006-01-09 17:05:41 +0000427 isr >>= 1;
428 }
429 }
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100430 chip->irq_unmask(idata);
SAN People73a59c12006-01-09 17:05:41 +0000431 /* now it may re-trigger */
432}
433
Andrew Victorf2173832006-09-27 13:23:00 +0100434/*--------------------------------------------------------------------------*/
SAN People73a59c12006-01-09 17:05:41 +0000435
Andrew Victorb66545e2007-11-23 16:09:10 +0100436#ifdef CONFIG_DEBUG_FS
437
438static int at91_gpio_show(struct seq_file *s, void *unused)
439{
440 int bank, j;
441
442 /* print heading */
443 seq_printf(s, "Pin\t");
444 for (bank = 0; bank < gpio_banks; bank++) {
445 seq_printf(s, "PIO%c\t", 'A' + bank);
446 };
447 seq_printf(s, "\n\n");
448
449 /* print pin status */
450 for (j = 0; j < 32; j++) {
451 seq_printf(s, "%i:\t", j);
452
453 for (bank = 0; bank < gpio_banks; bank++) {
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800454 unsigned pin = (32 * bank) + j;
Andrew Victorb66545e2007-11-23 16:09:10 +0100455 void __iomem *pio = pin_to_controller(pin);
456 unsigned mask = pin_to_mask(pin);
457
458 if (__raw_readl(pio + PIO_PSR) & mask)
459 seq_printf(s, "GPIO:%s", __raw_readl(pio + PIO_PDSR) & mask ? "1" : "0");
460 else
461 seq_printf(s, "%s", __raw_readl(pio + PIO_ABSR) & mask ? "B" : "A");
462
463 seq_printf(s, "\t");
464 }
465
466 seq_printf(s, "\n");
467 }
468
469 return 0;
470}
471
472static int at91_gpio_open(struct inode *inode, struct file *file)
473{
474 return single_open(file, at91_gpio_show, NULL);
475}
476
477static const struct file_operations at91_gpio_operations = {
478 .open = at91_gpio_open,
479 .read = seq_read,
480 .llseek = seq_lseek,
481 .release = single_release,
482};
483
484static int __init at91_gpio_debugfs_init(void)
485{
486 /* /sys/kernel/debug/at91_gpio */
487 (void) debugfs_create_file("at91_gpio", S_IFREG | S_IRUGO, NULL, NULL, &at91_gpio_operations);
488 return 0;
489}
490postcore_initcall(at91_gpio_debugfs_init);
491
492#endif
493
494/*--------------------------------------------------------------------------*/
495
Andrew Victor2b768b62009-02-11 21:39:05 +0100496/*
Nicolas Ferre21f81872012-02-11 15:41:40 +0100497 * irqdomain initialization: pile up irqdomains on top of AIC range
498 */
499static void __init at91_gpio_irqdomain(struct at91_gpio_chip *at91_gpio)
500{
501 int irq_base;
Nicolas Ferre5bc067b2012-02-13 11:26:25 +0100502#if defined(CONFIG_OF)
503 struct device_node *of_node = at91_gpio->chip.of_node;
504#else
505 struct device_node *of_node = NULL;
506#endif
Nicolas Ferre21f81872012-02-11 15:41:40 +0100507
508 irq_base = irq_alloc_descs(-1, 0, at91_gpio->chip.ngpio, 0);
509 if (irq_base < 0)
510 panic("at91_gpio.%d: error %d: couldn't allocate IRQ numbers.\n",
511 at91_gpio->pioc_idx, irq_base);
Nicolas Ferre5bc067b2012-02-13 11:26:25 +0100512 at91_gpio->domain = irq_domain_add_legacy(of_node,
Nicolas Ferre21f81872012-02-11 15:41:40 +0100513 at91_gpio->chip.ngpio,
514 irq_base, 0,
515 &irq_domain_simple_ops, NULL);
516 if (!at91_gpio->domain)
517 panic("at91_gpio.%d: couldn't allocate irq domain.\n",
518 at91_gpio->pioc_idx);
519}
520
521/*
Andrew Victor2b768b62009-02-11 21:39:05 +0100522 * This lock class tells lockdep that GPIO irqs are in a different
David Brownell37aca702008-03-05 00:08:29 +0100523 * category than their parents, so it won't report false recursion.
524 */
525static struct lock_class_key gpio_lock_class;
526
Andrew Victorf2173832006-09-27 13:23:00 +0100527/*
528 * Called from the processor-specific init to enable GPIO interrupt support.
529 */
530void __init at91_gpio_irq_setup(void)
531{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100532 unsigned pioc;
533 int gpio_irqnbr = 0;
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100534 struct at91_gpio_chip *this, *prev;
Andrew Victorf2173832006-09-27 13:23:00 +0100535
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800536 for (pioc = 0, this = gpio_chip, prev = NULL;
David Brownelle83aff52008-01-04 18:30:24 +0100537 pioc++ < gpio_banks;
538 prev = this, this++) {
Nicolas Ferre4340cde2012-02-11 15:28:08 +0100539 unsigned pioc_hwirq = this->pioc_hwirq;
Nicolas Ferre21f81872012-02-11 15:41:40 +0100540 int offset;
SAN People73a59c12006-01-09 17:05:41 +0000541
David Brownelle83aff52008-01-04 18:30:24 +0100542 __raw_writel(~0, this->regbase + PIO_IDR);
SAN People73a59c12006-01-09 17:05:41 +0000543
Nicolas Ferre21f81872012-02-11 15:41:40 +0100544 /* setup irq domain for this GPIO controller */
545 at91_gpio_irqdomain(this);
546
547 for (offset = 0; offset < this->chip.ngpio; offset++) {
548 unsigned int virq = irq_find_mapping(this->domain, offset);
549 irq_set_lockdep_class(virq, &gpio_lock_class);
David Brownell37aca702008-03-05 00:08:29 +0100550
Andrew Victor814138f2006-06-19 15:26:54 +0100551 /*
552 * Can use the "simple" and not "edge" handler since it's
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200553 * shorter, and the AIC handles interrupts sanely.
Andrew Victor814138f2006-06-19 15:26:54 +0100554 */
Nicolas Ferre21f81872012-02-11 15:41:40 +0100555 irq_set_chip_and_handler(virq, &gpio_irqchip,
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100556 handle_simple_irq);
Nicolas Ferre21f81872012-02-11 15:41:40 +0100557 set_irq_flags(virq, IRQF_VALID);
558 irq_set_chip_data(virq, this);
559
560 gpio_irqnbr++;
SAN People73a59c12006-01-09 17:05:41 +0000561 }
562
David Brownelle83aff52008-01-04 18:30:24 +0100563 /* The toplevel handler handles one bank of GPIOs, except
Nicolas Ferre4340cde2012-02-11 15:28:08 +0100564 * on some SoC it can handles up to three...
565 * We only set up the handler for the first of the list.
David Brownelle83aff52008-01-04 18:30:24 +0100566 */
567 if (prev && prev->next == this)
568 continue;
569
Nicolas Ferre4340cde2012-02-11 15:28:08 +0100570 irq_set_chip_data(pioc_hwirq, this);
571 irq_set_chained_handler(pioc_hwirq, gpio_irq_handler);
SAN People73a59c12006-01-09 17:05:41 +0000572 }
Nicolas Ferre21f81872012-02-11 15:41:40 +0100573 pr_info("AT91: %d gpio irqs in %d banks\n", gpio_irqnbr, gpio_banks);
Andrew Victorf2173832006-09-27 13:23:00 +0100574}
575
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100576/* gpiolib support */
577static int at91_gpiolib_direction_input(struct gpio_chip *chip,
578 unsigned offset)
579{
580 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
581 void __iomem *pio = at91_gpio->regbase;
582 unsigned mask = 1 << offset;
583
584 __raw_writel(mask, pio + PIO_ODR);
585 return 0;
586}
587
588static int at91_gpiolib_direction_output(struct gpio_chip *chip,
589 unsigned offset, int val)
590{
591 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
592 void __iomem *pio = at91_gpio->regbase;
593 unsigned mask = 1 << offset;
594
595 __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
596 __raw_writel(mask, pio + PIO_OER);
597 return 0;
598}
599
600static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset)
601{
602 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
603 void __iomem *pio = at91_gpio->regbase;
604 unsigned mask = 1 << offset;
605 u32 pdsr;
606
607 pdsr = __raw_readl(pio + PIO_PDSR);
608 return (pdsr & mask) != 0;
609}
610
611static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val)
612{
613 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
614 void __iomem *pio = at91_gpio->regbase;
615 unsigned mask = 1 << offset;
616
617 __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
618}
619
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100620static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
621{
622 int i;
623
624 for (i = 0; i < chip->ngpio; i++) {
625 unsigned pin = chip->base + i;
626 void __iomem *pio = pin_to_controller(pin);
627 unsigned mask = pin_to_mask(pin);
628 const char *gpio_label;
629
630 gpio_label = gpiochip_is_requested(chip, i);
631 if (gpio_label) {
632 seq_printf(s, "[%s] GPIO%s%d: ",
633 gpio_label, chip->label, i);
634 if (__raw_readl(pio + PIO_PSR) & mask)
635 seq_printf(s, "[gpio] %s\n",
636 at91_get_gpio_value(pin) ?
637 "set" : "clear");
638 else
639 seq_printf(s, "[periph %s]\n",
640 __raw_readl(pio + PIO_ABSR) &
641 mask ? "B" : "A");
642 }
643 }
644}
645
Nicolas Ferreb134ce82012-02-11 15:56:01 +0100646static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset)
647{
648 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
649 int virq = irq_find_mapping(at91_gpio->domain, offset);
650
651 dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
652 chip->label, offset + chip->base, virq);
653 return virq;
654}
655
Nicolas Ferre21f81872012-02-11 15:41:40 +0100656static int __init at91_gpio_setup_clk(int idx)
657{
658 struct at91_gpio_chip *at91_gpio = &gpio_chip[idx];
659
660 /* retreive PIO controller's clock */
661 at91_gpio->clock = clk_get_sys(NULL, at91_gpio->chip.label);
662 if (IS_ERR(at91_gpio->clock)) {
663 pr_err("at91_gpio.%d, failed to get clock, ignoring.\n", idx);
664 goto err;
665 }
666
667 if (clk_prepare(at91_gpio->clock))
668 goto clk_prep_err;
669
670 /* enable PIO controller's clock */
671 if (clk_enable(at91_gpio->clock)) {
672 pr_err("at91_gpio.%d, failed to enable clock, ignoring.\n", idx);
673 goto clk_err;
674 }
675
676 return 0;
677
678clk_err:
679 clk_unprepare(at91_gpio->clock);
680clk_prep_err:
681 clk_put(at91_gpio->clock);
682err:
683 return -EINVAL;
684}
685
686#ifdef CONFIG_OF_GPIO
687static void __init of_at91_gpio_init_one(struct device_node *np)
688{
689 int alias_idx;
690 struct at91_gpio_chip *at91_gpio;
691
692 if (!np)
693 return;
694
695 alias_idx = of_alias_get_id(np, "gpio");
696 if (alias_idx >= MAX_GPIO_BANKS) {
697 pr_err("at91_gpio, failed alias idx(%d) > MAX_GPIO_BANKS(%d), ignoring.\n",
698 alias_idx, MAX_GPIO_BANKS);
699 return;
700 }
701
702 at91_gpio = &gpio_chip[alias_idx];
703 at91_gpio->chip.base = alias_idx * at91_gpio->chip.ngpio;
704
705 at91_gpio->regbase = of_iomap(np, 0);
706 if (!at91_gpio->regbase) {
707 pr_err("at91_gpio.%d, failed to map registers, ignoring.\n",
708 alias_idx);
709 return;
710 }
711
712 /* Get the interrupts property */
713 if (of_property_read_u32(np, "interrupts", &at91_gpio->pioc_hwirq)) {
714 pr_err("at91_gpio.%d, failed to get interrupts property, ignoring.\n",
715 alias_idx);
716 goto ioremap_err;
717 }
718
719 /* Setup clock */
720 if (at91_gpio_setup_clk(alias_idx))
721 goto ioremap_err;
722
723 at91_gpio->chip.of_node = np;
724 gpio_banks = max(gpio_banks, alias_idx + 1);
725 at91_gpio->pioc_idx = alias_idx;
726 return;
727
728ioremap_err:
729 iounmap(at91_gpio->regbase);
730}
731
732static int __init of_at91_gpio_init(void)
733{
734 struct device_node *np = NULL;
735
736 /*
737 * This isn't ideal, but it gets things hooked up until this
738 * driver is converted into a platform_device
739 */
740 for_each_compatible_node(np, NULL, "atmel,at91rm9200-gpio")
741 of_at91_gpio_init_one(np);
742
743 return gpio_banks > 0 ? 0 : -EINVAL;
744}
745#else
746static int __init of_at91_gpio_init(void)
747{
748 return -EINVAL;
749}
750#endif
751
752static void __init at91_gpio_init_one(int idx, u32 regbase, int pioc_hwirq)
753{
754 struct at91_gpio_chip *at91_gpio = &gpio_chip[idx];
755
756 at91_gpio->chip.base = idx * at91_gpio->chip.ngpio;
757 at91_gpio->pioc_hwirq = pioc_hwirq;
758 at91_gpio->pioc_idx = idx;
759
760 at91_gpio->regbase = ioremap(regbase, 512);
761 if (!at91_gpio->regbase) {
762 pr_err("at91_gpio.%d, failed to map registers, ignoring.\n", idx);
763 return;
764 }
765
766 if (at91_gpio_setup_clk(idx))
767 goto ioremap_err;
768
769 gpio_banks = max(gpio_banks, idx + 1);
770 return;
771
772ioremap_err:
773 iounmap(at91_gpio->regbase);
774}
775
Andrew Victorf2173832006-09-27 13:23:00 +0100776/*
777 * Called from the processor-specific init to enable GPIO pin support.
778 */
779void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
780{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100781 unsigned i;
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100782 struct at91_gpio_chip *at91_gpio, *last = NULL;
David Brownelle83aff52008-01-04 18:30:24 +0100783
Andrew Victorf2173832006-09-27 13:23:00 +0100784 BUG_ON(nr_banks > MAX_GPIO_BANKS);
785
Nicolas Ferre21f81872012-02-11 15:41:40 +0100786 if (of_at91_gpio_init() < 0) {
787 /* No GPIO controller found in device tree */
788 for (i = 0; i < nr_banks; i++)
789 at91_gpio_init_one(i, data[i].regbase, data[i].id);
790 }
David Brownelle83aff52008-01-04 18:30:24 +0100791
Nicolas Ferre21f81872012-02-11 15:41:40 +0100792 for (i = 0; i < gpio_banks; i++) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100793 at91_gpio = &gpio_chip[i];
794
Nicolas Ferre4340cde2012-02-11 15:28:08 +0100795 /*
796 * GPIO controller are grouped on some SoC:
797 * PIOC, PIOD and PIOE can share the same IRQ line
798 */
799 if (last && last->pioc_hwirq == at91_gpio->pioc_hwirq)
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100800 last->next = at91_gpio;
801 last = at91_gpio;
802
803 gpiochip_add(&at91_gpio->chip);
David Brownelle83aff52008-01-04 18:30:24 +0100804 }
SAN People73a59c12006-01-09 17:05:41 +0000805}