blob: ecf6bbb9103a68bf917916d09751d567afd61a7d [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
Nicolas Ferre7530cd92011-12-08 15:35:22 +010054#define AT91_GPIO_CHIP(name, nr_gpio) \
Ryan Mallonf373e8c2009-02-10 21:02:08 +010055 { \
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, \
Nicolas Ferreb134ce82012-02-11 15:56:01 +010063 .to_irq = at91_gpiolib_to_irq, \
Ryan Mallonf373e8c2009-02-10 21:02:08 +010064 .ngpio = nr_gpio, \
65 }, \
66 }
67
68static struct at91_gpio_chip gpio_chip[] = {
Nicolas Ferre7530cd92011-12-08 15:35:22 +010069 AT91_GPIO_CHIP("pioA", 32),
70 AT91_GPIO_CHIP("pioB", 32),
71 AT91_GPIO_CHIP("pioC", 32),
72 AT91_GPIO_CHIP("pioD", 32),
73 AT91_GPIO_CHIP("pioE", 32),
Ryan Mallonf373e8c2009-02-10 21:02:08 +010074};
75
Andrew Victorf2173832006-09-27 13:23:00 +010076static int gpio_banks;
77
SAN People73a59c12006-01-09 17:05:41 +000078static inline void __iomem *pin_to_controller(unsigned pin)
79{
SAN People73a59c12006-01-09 17:05:41 +000080 pin /= 32;
Andrew Victorf2173832006-09-27 13:23:00 +010081 if (likely(pin < gpio_banks))
Ryan Mallonf373e8c2009-02-10 21:02:08 +010082 return gpio_chip[pin].regbase;
SAN People73a59c12006-01-09 17:05:41 +000083
84 return NULL;
85}
86
87static inline unsigned pin_to_mask(unsigned pin)
88{
SAN People73a59c12006-01-09 17:05:41 +000089 return 1 << (pin % 32);
90}
91
92
93/*--------------------------------------------------------------------------*/
94
95/* Not all hardware capabilities are exposed through these calls; they
96 * only encapsulate the most common features and modes. (So if you
97 * want to change signals in groups, do it directly.)
98 *
99 * Bootloaders will usually handle some of the pin multiplexing setup.
100 * The intent is certainly that by the time Linux is fully booted, all
101 * pins should have been fully initialized. These setup calls should
102 * only be used by board setup routines, or possibly in driver probe().
103 *
104 * For bootloaders doing all that setup, these calls could be inlined
105 * as NOPs so Linux won't duplicate any setup code
106 */
107
108
109/*
David Brownella31c4ee2007-02-12 00:53:13 -0800110 * mux the pin to the "GPIO" peripheral role.
111 */
112int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup)
113{
114 void __iomem *pio = pin_to_controller(pin);
115 unsigned mask = pin_to_mask(pin);
116
117 if (!pio)
118 return -EINVAL;
119 __raw_writel(mask, pio + PIO_IDR);
120 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
121 __raw_writel(mask, pio + PIO_PER);
122 return 0;
123}
124EXPORT_SYMBOL(at91_set_GPIO_periph);
125
126
127/*
SAN People73a59c12006-01-09 17:05:41 +0000128 * mux the pin to the "A" internal peripheral role.
129 */
130int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
131{
132 void __iomem *pio = pin_to_controller(pin);
133 unsigned mask = pin_to_mask(pin);
134
135 if (!pio)
136 return -EINVAL;
137
138 __raw_writel(mask, pio + PIO_IDR);
139 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
140 __raw_writel(mask, pio + PIO_ASR);
141 __raw_writel(mask, pio + PIO_PDR);
142 return 0;
143}
144EXPORT_SYMBOL(at91_set_A_periph);
145
146
147/*
148 * mux the pin to the "B" internal peripheral role.
149 */
150int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
151{
152 void __iomem *pio = pin_to_controller(pin);
153 unsigned mask = pin_to_mask(pin);
154
155 if (!pio)
156 return -EINVAL;
157
158 __raw_writel(mask, pio + PIO_IDR);
159 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
160 __raw_writel(mask, pio + PIO_BSR);
161 __raw_writel(mask, pio + PIO_PDR);
162 return 0;
163}
164EXPORT_SYMBOL(at91_set_B_periph);
165
166
167/*
168 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
169 * configure it for an input.
170 */
171int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
172{
173 void __iomem *pio = pin_to_controller(pin);
174 unsigned mask = pin_to_mask(pin);
175
176 if (!pio)
177 return -EINVAL;
178
179 __raw_writel(mask, pio + PIO_IDR);
180 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
181 __raw_writel(mask, pio + PIO_ODR);
182 __raw_writel(mask, pio + PIO_PER);
183 return 0;
184}
185EXPORT_SYMBOL(at91_set_gpio_input);
186
187
188/*
189 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
190 * and configure it for an output.
191 */
192int __init_or_module at91_set_gpio_output(unsigned pin, int value)
193{
194 void __iomem *pio = pin_to_controller(pin);
195 unsigned mask = pin_to_mask(pin);
196
197 if (!pio)
198 return -EINVAL;
199
200 __raw_writel(mask, pio + PIO_IDR);
201 __raw_writel(mask, pio + PIO_PUDR);
202 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
203 __raw_writel(mask, pio + PIO_OER);
204 __raw_writel(mask, pio + PIO_PER);
205 return 0;
206}
207EXPORT_SYMBOL(at91_set_gpio_output);
208
209
210/*
211 * enable/disable the glitch filter; mostly used with IRQ handling.
212 */
213int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
214{
215 void __iomem *pio = pin_to_controller(pin);
216 unsigned mask = pin_to_mask(pin);
217
218 if (!pio)
219 return -EINVAL;
220 __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
221 return 0;
222}
223EXPORT_SYMBOL(at91_set_deglitch);
224
Andrew Victordf666b92006-02-22 21:23:35 +0000225/*
226 * enable/disable the multi-driver; This is only valid for output and
227 * allows the output pin to run as an open collector output.
228 */
229int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
230{
231 void __iomem *pio = pin_to_controller(pin);
232 unsigned mask = pin_to_mask(pin);
233
234 if (!pio)
235 return -EINVAL;
236
237 __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
238 return 0;
239}
240EXPORT_SYMBOL(at91_set_multi_drive);
241
SAN People73a59c12006-01-09 17:05:41 +0000242/*
243 * assuming the pin is muxed as a gpio output, set its value.
244 */
245int at91_set_gpio_value(unsigned pin, int value)
246{
247 void __iomem *pio = pin_to_controller(pin);
248 unsigned mask = pin_to_mask(pin);
249
250 if (!pio)
251 return -EINVAL;
252 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
253 return 0;
254}
255EXPORT_SYMBOL(at91_set_gpio_value);
256
257
258/*
259 * read the pin's value (works even if it's not muxed as a gpio).
260 */
261int at91_get_gpio_value(unsigned pin)
262{
263 void __iomem *pio = pin_to_controller(pin);
264 unsigned mask = pin_to_mask(pin);
265 u32 pdsr;
266
267 if (!pio)
268 return -EINVAL;
269 pdsr = __raw_readl(pio + PIO_PDSR);
270 return (pdsr & mask) != 0;
271}
272EXPORT_SYMBOL(at91_get_gpio_value);
273
274/*--------------------------------------------------------------------------*/
275
Andrew Victor814138f2006-06-19 15:26:54 +0100276#ifdef CONFIG_PM
277
Andrew Victorf2173832006-09-27 13:23:00 +0100278static u32 wakeups[MAX_GPIO_BANKS];
279static u32 backups[MAX_GPIO_BANKS];
Andrew Victor814138f2006-06-19 15:26:54 +0100280
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100281static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
Andrew Victor814138f2006-06-19 15:26:54 +0100282{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100283 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
284 unsigned mask = 1 << d->hwirq;
285 unsigned bank = at91_gpio->pioc_idx;
Andrew Victor814138f2006-06-19 15:26:54 +0100286
Andrew Victor3ea163e2007-01-09 13:47:29 +0100287 if (unlikely(bank >= MAX_GPIO_BANKS))
Andrew Victor814138f2006-06-19 15:26:54 +0100288 return -EINVAL;
289
290 if (state)
Andrew Victor3ea163e2007-01-09 13:47:29 +0100291 wakeups[bank] |= mask;
Andrew Victor814138f2006-06-19 15:26:54 +0100292 else
Andrew Victor3ea163e2007-01-09 13:47:29 +0100293 wakeups[bank] &= ~mask;
294
Nicolas Ferre4340cde2012-02-11 15:28:08 +0100295 irq_set_irq_wake(gpio_chip[bank].pioc_hwirq, state);
Andrew Victor814138f2006-06-19 15:26:54 +0100296
297 return 0;
298}
299
300void at91_gpio_suspend(void)
301{
302 int i;
303
Andrew Victorf2173832006-09-27 13:23:00 +0100304 for (i = 0; i < gpio_banks; i++) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100305 void __iomem *pio = gpio_chip[i].regbase;
Andrew Victor814138f2006-06-19 15:26:54 +0100306
David Brownelle83aff52008-01-04 18:30:24 +0100307 backups[i] = __raw_readl(pio + PIO_IMR);
308 __raw_writel(backups[i], pio + PIO_IDR);
309 __raw_writel(wakeups[i], pio + PIO_IER);
Andrew Victor814138f2006-06-19 15:26:54 +0100310
Nicolas Ferre21f81872012-02-11 15:41:40 +0100311 if (!wakeups[i]) {
312 clk_unprepare(gpio_chip[i].clock);
Jean-Christophe PLAGNIOL-VILLARD619d4a42011-11-13 13:00:58 +0800313 clk_disable(gpio_chip[i].clock);
Nicolas Ferre21f81872012-02-11 15:41:40 +0100314 } else {
Andrew Victor814138f2006-06-19 15:26:54 +0100315#ifdef CONFIG_PM_DEBUG
Andrew Victor3ea163e2007-01-09 13:47:29 +0100316 printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
Andrew Victor814138f2006-06-19 15:26:54 +0100317#endif
318 }
319 }
320}
321
322void at91_gpio_resume(void)
323{
324 int i;
325
Andrew Victorf2173832006-09-27 13:23:00 +0100326 for (i = 0; i < gpio_banks; i++) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100327 void __iomem *pio = gpio_chip[i].regbase;
Andrew Victor814138f2006-06-19 15:26:54 +0100328
Nicolas Ferre21f81872012-02-11 15:41:40 +0100329 if (!wakeups[i]) {
330 if (clk_prepare(gpio_chip[i].clock) == 0)
331 clk_enable(gpio_chip[i].clock);
332 }
Andrew Victor3ea163e2007-01-09 13:47:29 +0100333
David Brownelle83aff52008-01-04 18:30:24 +0100334 __raw_writel(wakeups[i], pio + PIO_IDR);
335 __raw_writel(backups[i], pio + PIO_IER);
Andrew Victorf2173832006-09-27 13:23:00 +0100336 }
Andrew Victor814138f2006-06-19 15:26:54 +0100337}
338
339#else
340#define gpio_irq_set_wake NULL
341#endif
342
SAN People73a59c12006-01-09 17:05:41 +0000343
344/* Several AIC controller irqs are dispatched through this GPIO handler.
345 * To use any AT91_PIN_* as an externally triggered IRQ, first call
346 * at91_set_gpio_input() then maybe enable its glitch filter.
347 * Then just request_irq() with the pin ID; it works like any ARM IRQ
348 * handler, though it always triggers on rising and falling edges.
349 *
350 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
351 * configuring them with at91_set_a_periph() or at91_set_b_periph().
352 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
353 */
354
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100355static void gpio_irq_mask(struct irq_data *d)
SAN People73a59c12006-01-09 17:05:41 +0000356{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100357 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
358 void __iomem *pio = at91_gpio->regbase;
359 unsigned mask = 1 << d->hwirq;
SAN People73a59c12006-01-09 17:05:41 +0000360
361 if (pio)
362 __raw_writel(mask, pio + PIO_IDR);
363}
364
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100365static void gpio_irq_unmask(struct irq_data *d)
SAN People73a59c12006-01-09 17:05:41 +0000366{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100367 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
368 void __iomem *pio = at91_gpio->regbase;
369 unsigned mask = 1 << d->hwirq;
SAN People73a59c12006-01-09 17:05:41 +0000370
371 if (pio)
372 __raw_writel(mask, pio + PIO_IER);
373}
374
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100375static int gpio_irq_type(struct irq_data *d, unsigned type)
SAN People73a59c12006-01-09 17:05:41 +0000376{
David Brownelle83aff52008-01-04 18:30:24 +0100377 switch (type) {
378 case IRQ_TYPE_NONE:
379 case IRQ_TYPE_EDGE_BOTH:
380 return 0;
381 default:
382 return -EINVAL;
383 }
SAN People73a59c12006-01-09 17:05:41 +0000384}
385
David Brownell38c677c2006-08-01 22:26:25 +0100386static struct irq_chip gpio_irqchip = {
387 .name = "GPIO",
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100388 .irq_disable = gpio_irq_mask,
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100389 .irq_mask = gpio_irq_mask,
390 .irq_unmask = gpio_irq_unmask,
391 .irq_set_type = gpio_irq_type,
392 .irq_set_wake = gpio_irq_set_wake,
SAN People73a59c12006-01-09 17:05:41 +0000393};
394
Russell King10dd5ce2006-11-23 11:41:32 +0000395static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
SAN People73a59c12006-01-09 17:05:41 +0000396{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100397 unsigned virq;
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100398 struct irq_data *idata = irq_desc_get_irq_data(desc);
399 struct irq_chip *chip = irq_data_get_irq_chip(idata);
400 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(idata);
401 void __iomem *pio = at91_gpio->regbase;
SAN People73a59c12006-01-09 17:05:41 +0000402 u32 isr;
403
SAN People73a59c12006-01-09 17:05:41 +0000404 /* temporarily mask (level sensitive) parent IRQ */
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100405 chip->irq_ack(idata);
SAN People73a59c12006-01-09 17:05:41 +0000406 for (;;) {
David Brownelle83aff52008-01-04 18:30:24 +0100407 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
408 * When there none are pending, we're finished unless we need
409 * to process multiple banks (like ID_PIOCDE on sam9263).
410 */
SAN People73a59c12006-01-09 17:05:41 +0000411 isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
David Brownelle83aff52008-01-04 18:30:24 +0100412 if (!isr) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100413 if (!at91_gpio->next)
David Brownelle83aff52008-01-04 18:30:24 +0100414 break;
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100415 at91_gpio = at91_gpio->next;
416 pio = at91_gpio->regbase;
David Brownelle83aff52008-01-04 18:30:24 +0100417 continue;
418 }
SAN People73a59c12006-01-09 17:05:41 +0000419
Nicolas Ferre21f81872012-02-11 15:41:40 +0100420 virq = gpio_to_irq(at91_gpio->chip.base);
SAN People73a59c12006-01-09 17:05:41 +0000421
422 while (isr) {
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100423 if (isr & 1)
Nicolas Ferre21f81872012-02-11 15:41:40 +0100424 generic_handle_irq(virq);
425 virq++;
SAN People73a59c12006-01-09 17:05:41 +0000426 isr >>= 1;
427 }
428 }
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100429 chip->irq_unmask(idata);
SAN People73a59c12006-01-09 17:05:41 +0000430 /* now it may re-trigger */
431}
432
Andrew Victorf2173832006-09-27 13:23:00 +0100433/*--------------------------------------------------------------------------*/
SAN People73a59c12006-01-09 17:05:41 +0000434
Andrew Victorb66545e2007-11-23 16:09:10 +0100435#ifdef CONFIG_DEBUG_FS
436
437static int at91_gpio_show(struct seq_file *s, void *unused)
438{
439 int bank, j;
440
441 /* print heading */
442 seq_printf(s, "Pin\t");
443 for (bank = 0; bank < gpio_banks; bank++) {
444 seq_printf(s, "PIO%c\t", 'A' + bank);
445 };
446 seq_printf(s, "\n\n");
447
448 /* print pin status */
449 for (j = 0; j < 32; j++) {
450 seq_printf(s, "%i:\t", j);
451
452 for (bank = 0; bank < gpio_banks; bank++) {
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800453 unsigned pin = (32 * bank) + j;
Andrew Victorb66545e2007-11-23 16:09:10 +0100454 void __iomem *pio = pin_to_controller(pin);
455 unsigned mask = pin_to_mask(pin);
456
457 if (__raw_readl(pio + PIO_PSR) & mask)
458 seq_printf(s, "GPIO:%s", __raw_readl(pio + PIO_PDSR) & mask ? "1" : "0");
459 else
460 seq_printf(s, "%s", __raw_readl(pio + PIO_ABSR) & mask ? "B" : "A");
461
462 seq_printf(s, "\t");
463 }
464
465 seq_printf(s, "\n");
466 }
467
468 return 0;
469}
470
471static int at91_gpio_open(struct inode *inode, struct file *file)
472{
473 return single_open(file, at91_gpio_show, NULL);
474}
475
476static const struct file_operations at91_gpio_operations = {
477 .open = at91_gpio_open,
478 .read = seq_read,
479 .llseek = seq_lseek,
480 .release = single_release,
481};
482
483static int __init at91_gpio_debugfs_init(void)
484{
485 /* /sys/kernel/debug/at91_gpio */
486 (void) debugfs_create_file("at91_gpio", S_IFREG | S_IRUGO, NULL, NULL, &at91_gpio_operations);
487 return 0;
488}
489postcore_initcall(at91_gpio_debugfs_init);
490
491#endif
492
493/*--------------------------------------------------------------------------*/
494
Andrew Victor2b768b62009-02-11 21:39:05 +0100495/*
Nicolas Ferre21f81872012-02-11 15:41:40 +0100496 * irqdomain initialization: pile up irqdomains on top of AIC range
497 */
498static void __init at91_gpio_irqdomain(struct at91_gpio_chip *at91_gpio)
499{
500 int irq_base;
Nicolas Ferre5bc067b2012-02-13 11:26:25 +0100501#if defined(CONFIG_OF)
502 struct device_node *of_node = at91_gpio->chip.of_node;
503#else
504 struct device_node *of_node = NULL;
505#endif
Nicolas Ferre21f81872012-02-11 15:41:40 +0100506
507 irq_base = irq_alloc_descs(-1, 0, at91_gpio->chip.ngpio, 0);
508 if (irq_base < 0)
509 panic("at91_gpio.%d: error %d: couldn't allocate IRQ numbers.\n",
510 at91_gpio->pioc_idx, irq_base);
Nicolas Ferre5bc067b2012-02-13 11:26:25 +0100511 at91_gpio->domain = irq_domain_add_legacy(of_node,
Nicolas Ferre21f81872012-02-11 15:41:40 +0100512 at91_gpio->chip.ngpio,
513 irq_base, 0,
514 &irq_domain_simple_ops, NULL);
515 if (!at91_gpio->domain)
516 panic("at91_gpio.%d: couldn't allocate irq domain.\n",
517 at91_gpio->pioc_idx);
518}
519
520/*
Andrew Victor2b768b62009-02-11 21:39:05 +0100521 * This lock class tells lockdep that GPIO irqs are in a different
David Brownell37aca702008-03-05 00:08:29 +0100522 * category than their parents, so it won't report false recursion.
523 */
524static struct lock_class_key gpio_lock_class;
525
Andrew Victorf2173832006-09-27 13:23:00 +0100526/*
527 * Called from the processor-specific init to enable GPIO interrupt support.
528 */
529void __init at91_gpio_irq_setup(void)
530{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100531 unsigned pioc;
532 int gpio_irqnbr = 0;
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100533 struct at91_gpio_chip *this, *prev;
Andrew Victorf2173832006-09-27 13:23:00 +0100534
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800535 for (pioc = 0, this = gpio_chip, prev = NULL;
David Brownelle83aff52008-01-04 18:30:24 +0100536 pioc++ < gpio_banks;
537 prev = this, this++) {
Nicolas Ferre4340cde2012-02-11 15:28:08 +0100538 unsigned pioc_hwirq = this->pioc_hwirq;
Nicolas Ferre21f81872012-02-11 15:41:40 +0100539 int offset;
SAN People73a59c12006-01-09 17:05:41 +0000540
David Brownelle83aff52008-01-04 18:30:24 +0100541 __raw_writel(~0, this->regbase + PIO_IDR);
SAN People73a59c12006-01-09 17:05:41 +0000542
Nicolas Ferre21f81872012-02-11 15:41:40 +0100543 /* setup irq domain for this GPIO controller */
544 at91_gpio_irqdomain(this);
545
546 for (offset = 0; offset < this->chip.ngpio; offset++) {
547 unsigned int virq = irq_find_mapping(this->domain, offset);
548 irq_set_lockdep_class(virq, &gpio_lock_class);
David Brownell37aca702008-03-05 00:08:29 +0100549
Andrew Victor814138f2006-06-19 15:26:54 +0100550 /*
551 * Can use the "simple" and not "edge" handler since it's
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200552 * shorter, and the AIC handles interrupts sanely.
Andrew Victor814138f2006-06-19 15:26:54 +0100553 */
Nicolas Ferre21f81872012-02-11 15:41:40 +0100554 irq_set_chip_and_handler(virq, &gpio_irqchip,
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100555 handle_simple_irq);
Nicolas Ferre21f81872012-02-11 15:41:40 +0100556 set_irq_flags(virq, IRQF_VALID);
557 irq_set_chip_data(virq, this);
558
559 gpio_irqnbr++;
SAN People73a59c12006-01-09 17:05:41 +0000560 }
561
David Brownelle83aff52008-01-04 18:30:24 +0100562 /* The toplevel handler handles one bank of GPIOs, except
Nicolas Ferre4340cde2012-02-11 15:28:08 +0100563 * on some SoC it can handles up to three...
564 * We only set up the handler for the first of the list.
David Brownelle83aff52008-01-04 18:30:24 +0100565 */
566 if (prev && prev->next == this)
567 continue;
568
Nicolas Ferre4340cde2012-02-11 15:28:08 +0100569 irq_set_chip_data(pioc_hwirq, this);
570 irq_set_chained_handler(pioc_hwirq, gpio_irq_handler);
SAN People73a59c12006-01-09 17:05:41 +0000571 }
Nicolas Ferre21f81872012-02-11 15:41:40 +0100572 pr_info("AT91: %d gpio irqs in %d banks\n", gpio_irqnbr, gpio_banks);
Andrew Victorf2173832006-09-27 13:23:00 +0100573}
574
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100575/* gpiolib support */
576static int at91_gpiolib_direction_input(struct gpio_chip *chip,
577 unsigned offset)
578{
579 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
580 void __iomem *pio = at91_gpio->regbase;
581 unsigned mask = 1 << offset;
582
583 __raw_writel(mask, pio + PIO_ODR);
584 return 0;
585}
586
587static int at91_gpiolib_direction_output(struct gpio_chip *chip,
588 unsigned offset, int val)
589{
590 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
591 void __iomem *pio = at91_gpio->regbase;
592 unsigned mask = 1 << offset;
593
594 __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
595 __raw_writel(mask, pio + PIO_OER);
596 return 0;
597}
598
599static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset)
600{
601 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
602 void __iomem *pio = at91_gpio->regbase;
603 unsigned mask = 1 << offset;
604 u32 pdsr;
605
606 pdsr = __raw_readl(pio + PIO_PDSR);
607 return (pdsr & mask) != 0;
608}
609
610static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val)
611{
612 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
613 void __iomem *pio = at91_gpio->regbase;
614 unsigned mask = 1 << offset;
615
616 __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
617}
618
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100619static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
620{
621 int i;
622
623 for (i = 0; i < chip->ngpio; i++) {
624 unsigned pin = chip->base + i;
625 void __iomem *pio = pin_to_controller(pin);
626 unsigned mask = pin_to_mask(pin);
627 const char *gpio_label;
628
629 gpio_label = gpiochip_is_requested(chip, i);
630 if (gpio_label) {
631 seq_printf(s, "[%s] GPIO%s%d: ",
632 gpio_label, chip->label, i);
633 if (__raw_readl(pio + PIO_PSR) & mask)
634 seq_printf(s, "[gpio] %s\n",
635 at91_get_gpio_value(pin) ?
636 "set" : "clear");
637 else
638 seq_printf(s, "[periph %s]\n",
639 __raw_readl(pio + PIO_ABSR) &
640 mask ? "B" : "A");
641 }
642 }
643}
644
Nicolas Ferreb134ce82012-02-11 15:56:01 +0100645static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset)
646{
647 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
648 int virq = irq_find_mapping(at91_gpio->domain, offset);
649
650 dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
651 chip->label, offset + chip->base, virq);
652 return virq;
653}
654
Nicolas Ferre21f81872012-02-11 15:41:40 +0100655static int __init at91_gpio_setup_clk(int idx)
656{
657 struct at91_gpio_chip *at91_gpio = &gpio_chip[idx];
658
659 /* retreive PIO controller's clock */
660 at91_gpio->clock = clk_get_sys(NULL, at91_gpio->chip.label);
661 if (IS_ERR(at91_gpio->clock)) {
662 pr_err("at91_gpio.%d, failed to get clock, ignoring.\n", idx);
663 goto err;
664 }
665
666 if (clk_prepare(at91_gpio->clock))
667 goto clk_prep_err;
668
669 /* enable PIO controller's clock */
670 if (clk_enable(at91_gpio->clock)) {
671 pr_err("at91_gpio.%d, failed to enable clock, ignoring.\n", idx);
672 goto clk_err;
673 }
674
675 return 0;
676
677clk_err:
678 clk_unprepare(at91_gpio->clock);
679clk_prep_err:
680 clk_put(at91_gpio->clock);
681err:
682 return -EINVAL;
683}
684
685#ifdef CONFIG_OF_GPIO
686static void __init of_at91_gpio_init_one(struct device_node *np)
687{
688 int alias_idx;
689 struct at91_gpio_chip *at91_gpio;
690
691 if (!np)
692 return;
693
694 alias_idx = of_alias_get_id(np, "gpio");
695 if (alias_idx >= MAX_GPIO_BANKS) {
696 pr_err("at91_gpio, failed alias idx(%d) > MAX_GPIO_BANKS(%d), ignoring.\n",
697 alias_idx, MAX_GPIO_BANKS);
698 return;
699 }
700
701 at91_gpio = &gpio_chip[alias_idx];
702 at91_gpio->chip.base = alias_idx * at91_gpio->chip.ngpio;
703
704 at91_gpio->regbase = of_iomap(np, 0);
705 if (!at91_gpio->regbase) {
706 pr_err("at91_gpio.%d, failed to map registers, ignoring.\n",
707 alias_idx);
708 return;
709 }
710
711 /* Get the interrupts property */
712 if (of_property_read_u32(np, "interrupts", &at91_gpio->pioc_hwirq)) {
713 pr_err("at91_gpio.%d, failed to get interrupts property, ignoring.\n",
714 alias_idx);
715 goto ioremap_err;
716 }
717
718 /* Setup clock */
719 if (at91_gpio_setup_clk(alias_idx))
720 goto ioremap_err;
721
722 at91_gpio->chip.of_node = np;
723 gpio_banks = max(gpio_banks, alias_idx + 1);
724 at91_gpio->pioc_idx = alias_idx;
725 return;
726
727ioremap_err:
728 iounmap(at91_gpio->regbase);
729}
730
731static int __init of_at91_gpio_init(void)
732{
733 struct device_node *np = NULL;
734
735 /*
736 * This isn't ideal, but it gets things hooked up until this
737 * driver is converted into a platform_device
738 */
739 for_each_compatible_node(np, NULL, "atmel,at91rm9200-gpio")
740 of_at91_gpio_init_one(np);
741
742 return gpio_banks > 0 ? 0 : -EINVAL;
743}
744#else
745static int __init of_at91_gpio_init(void)
746{
747 return -EINVAL;
748}
749#endif
750
751static void __init at91_gpio_init_one(int idx, u32 regbase, int pioc_hwirq)
752{
753 struct at91_gpio_chip *at91_gpio = &gpio_chip[idx];
754
755 at91_gpio->chip.base = idx * at91_gpio->chip.ngpio;
756 at91_gpio->pioc_hwirq = pioc_hwirq;
757 at91_gpio->pioc_idx = idx;
758
759 at91_gpio->regbase = ioremap(regbase, 512);
760 if (!at91_gpio->regbase) {
761 pr_err("at91_gpio.%d, failed to map registers, ignoring.\n", idx);
762 return;
763 }
764
765 if (at91_gpio_setup_clk(idx))
766 goto ioremap_err;
767
768 gpio_banks = max(gpio_banks, idx + 1);
769 return;
770
771ioremap_err:
772 iounmap(at91_gpio->regbase);
773}
774
Andrew Victorf2173832006-09-27 13:23:00 +0100775/*
776 * Called from the processor-specific init to enable GPIO pin support.
777 */
778void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
779{
Nicolas Ferre21f81872012-02-11 15:41:40 +0100780 unsigned i;
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100781 struct at91_gpio_chip *at91_gpio, *last = NULL;
David Brownelle83aff52008-01-04 18:30:24 +0100782
Andrew Victorf2173832006-09-27 13:23:00 +0100783 BUG_ON(nr_banks > MAX_GPIO_BANKS);
784
Nicolas Ferre21f81872012-02-11 15:41:40 +0100785 if (of_at91_gpio_init() < 0) {
786 /* No GPIO controller found in device tree */
787 for (i = 0; i < nr_banks; i++)
788 at91_gpio_init_one(i, data[i].regbase, data[i].id);
789 }
David Brownelle83aff52008-01-04 18:30:24 +0100790
Nicolas Ferre21f81872012-02-11 15:41:40 +0100791 for (i = 0; i < gpio_banks; i++) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100792 at91_gpio = &gpio_chip[i];
793
Nicolas Ferre4340cde2012-02-11 15:28:08 +0100794 /*
795 * GPIO controller are grouped on some SoC:
796 * PIOC, PIOD and PIOE can share the same IRQ line
797 */
798 if (last && last->pioc_hwirq == at91_gpio->pioc_hwirq)
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100799 last->next = at91_gpio;
800 last = at91_gpio;
801
802 gpiochip_add(&at91_gpio->chip);
David Brownelle83aff52008-01-04 18:30:24 +0100803 }
SAN People73a59c12006-01-09 17:05:41 +0000804}