blob: d000448b11acb430eb4599006435f85afa1cf396 [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>
Russell King2f8163b2011-07-26 10:53:52 +010014#include <linux/gpio.h>
Thomas Gleixner07d265d2006-07-01 23:01:50 +010015#include <linux/interrupt.h>
16#include <linux/irq.h>
Andrew Victorb66545e2007-11-23 16:09:10 +010017#include <linux/debugfs.h>
18#include <linux/seq_file.h>
SAN People73a59c12006-01-09 17:05:41 +000019#include <linux/kernel.h>
20#include <linux/list.h>
21#include <linux/module.h>
Russell Kingfced80c2008-09-06 12:10:45 +010022#include <linux/io.h>
SAN People73a59c12006-01-09 17:05:41 +000023
Russell Kinga09e64f2008-08-05 16:14:15 +010024#include <mach/hardware.h>
25#include <mach/at91_pio.h>
SAN People73a59c12006-01-09 17:05:41 +000026
Andrew Victorf2173832006-09-27 13:23:00 +010027#include "generic.h"
28
Ryan Mallonf373e8c2009-02-10 21:02:08 +010029struct at91_gpio_chip {
30 struct gpio_chip chip;
31 struct at91_gpio_chip *next; /* Bank sharing same clock */
Jean-Christophe PLAGNIOL-VILLARD1a2d9152011-10-17 14:28:38 +080032 int id; /* ID of register bank */
Ryan Mallonf373e8c2009-02-10 21:02:08 +010033 void __iomem *regbase; /* Base of register bank */
Jean-Christophe PLAGNIOL-VILLARD619d4a42011-11-13 13:00:58 +080034 struct clk *clock; /* associated clock */
Ryan Mallonf373e8c2009-02-10 21:02:08 +010035};
Andrew Victorf2173832006-09-27 13:23:00 +010036
Ryan Mallonf373e8c2009-02-10 21:02:08 +010037#define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
38
39static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip);
40static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val);
41static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset);
42static int at91_gpiolib_direction_output(struct gpio_chip *chip,
43 unsigned offset, int val);
44static int at91_gpiolib_direction_input(struct gpio_chip *chip,
45 unsigned offset);
Ryan Mallonf373e8c2009-02-10 21:02:08 +010046
47#define AT91_GPIO_CHIP(name, base_gpio, nr_gpio) \
48 { \
49 .chip = { \
50 .label = name, \
Ryan Mallonf373e8c2009-02-10 21:02:08 +010051 .direction_input = at91_gpiolib_direction_input, \
52 .direction_output = at91_gpiolib_direction_output, \
53 .get = at91_gpiolib_get, \
54 .set = at91_gpiolib_set, \
55 .dbg_show = at91_gpiolib_dbg_show, \
56 .base = base_gpio, \
57 .ngpio = nr_gpio, \
58 }, \
59 }
60
61static struct at91_gpio_chip gpio_chip[] = {
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +080062 AT91_GPIO_CHIP("pioA", 0x00, 32),
63 AT91_GPIO_CHIP("pioB", 0x20, 32),
64 AT91_GPIO_CHIP("pioC", 0x40, 32),
65 AT91_GPIO_CHIP("pioD", 0x60, 32),
66 AT91_GPIO_CHIP("pioE", 0x80, 32),
Ryan Mallonf373e8c2009-02-10 21:02:08 +010067};
68
Andrew Victorf2173832006-09-27 13:23:00 +010069static int gpio_banks;
70
SAN People73a59c12006-01-09 17:05:41 +000071static inline void __iomem *pin_to_controller(unsigned pin)
72{
SAN People73a59c12006-01-09 17:05:41 +000073 pin /= 32;
Andrew Victorf2173832006-09-27 13:23:00 +010074 if (likely(pin < gpio_banks))
Ryan Mallonf373e8c2009-02-10 21:02:08 +010075 return gpio_chip[pin].regbase;
SAN People73a59c12006-01-09 17:05:41 +000076
77 return NULL;
78}
79
80static inline unsigned pin_to_mask(unsigned pin)
81{
SAN People73a59c12006-01-09 17:05:41 +000082 return 1 << (pin % 32);
83}
84
85
86/*--------------------------------------------------------------------------*/
87
88/* Not all hardware capabilities are exposed through these calls; they
89 * only encapsulate the most common features and modes. (So if you
90 * want to change signals in groups, do it directly.)
91 *
92 * Bootloaders will usually handle some of the pin multiplexing setup.
93 * The intent is certainly that by the time Linux is fully booted, all
94 * pins should have been fully initialized. These setup calls should
95 * only be used by board setup routines, or possibly in driver probe().
96 *
97 * For bootloaders doing all that setup, these calls could be inlined
98 * as NOPs so Linux won't duplicate any setup code
99 */
100
101
102/*
David Brownella31c4ee2007-02-12 00:53:13 -0800103 * mux the pin to the "GPIO" peripheral role.
104 */
105int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup)
106{
107 void __iomem *pio = pin_to_controller(pin);
108 unsigned mask = pin_to_mask(pin);
109
110 if (!pio)
111 return -EINVAL;
112 __raw_writel(mask, pio + PIO_IDR);
113 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
114 __raw_writel(mask, pio + PIO_PER);
115 return 0;
116}
117EXPORT_SYMBOL(at91_set_GPIO_periph);
118
119
120/*
SAN People73a59c12006-01-09 17:05:41 +0000121 * mux the pin to the "A" internal peripheral role.
122 */
123int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
124{
125 void __iomem *pio = pin_to_controller(pin);
126 unsigned mask = pin_to_mask(pin);
127
128 if (!pio)
129 return -EINVAL;
130
131 __raw_writel(mask, pio + PIO_IDR);
132 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
133 __raw_writel(mask, pio + PIO_ASR);
134 __raw_writel(mask, pio + PIO_PDR);
135 return 0;
136}
137EXPORT_SYMBOL(at91_set_A_periph);
138
139
140/*
141 * mux the pin to the "B" internal peripheral role.
142 */
143int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
144{
145 void __iomem *pio = pin_to_controller(pin);
146 unsigned mask = pin_to_mask(pin);
147
148 if (!pio)
149 return -EINVAL;
150
151 __raw_writel(mask, pio + PIO_IDR);
152 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
153 __raw_writel(mask, pio + PIO_BSR);
154 __raw_writel(mask, pio + PIO_PDR);
155 return 0;
156}
157EXPORT_SYMBOL(at91_set_B_periph);
158
159
160/*
161 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
162 * configure it for an input.
163 */
164int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
165{
166 void __iomem *pio = pin_to_controller(pin);
167 unsigned mask = pin_to_mask(pin);
168
169 if (!pio)
170 return -EINVAL;
171
172 __raw_writel(mask, pio + PIO_IDR);
173 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
174 __raw_writel(mask, pio + PIO_ODR);
175 __raw_writel(mask, pio + PIO_PER);
176 return 0;
177}
178EXPORT_SYMBOL(at91_set_gpio_input);
179
180
181/*
182 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
183 * and configure it for an output.
184 */
185int __init_or_module at91_set_gpio_output(unsigned pin, int value)
186{
187 void __iomem *pio = pin_to_controller(pin);
188 unsigned mask = pin_to_mask(pin);
189
190 if (!pio)
191 return -EINVAL;
192
193 __raw_writel(mask, pio + PIO_IDR);
194 __raw_writel(mask, pio + PIO_PUDR);
195 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
196 __raw_writel(mask, pio + PIO_OER);
197 __raw_writel(mask, pio + PIO_PER);
198 return 0;
199}
200EXPORT_SYMBOL(at91_set_gpio_output);
201
202
203/*
204 * enable/disable the glitch filter; mostly used with IRQ handling.
205 */
206int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
207{
208 void __iomem *pio = pin_to_controller(pin);
209 unsigned mask = pin_to_mask(pin);
210
211 if (!pio)
212 return -EINVAL;
213 __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
214 return 0;
215}
216EXPORT_SYMBOL(at91_set_deglitch);
217
Andrew Victordf666b92006-02-22 21:23:35 +0000218/*
219 * enable/disable the multi-driver; This is only valid for output and
220 * allows the output pin to run as an open collector output.
221 */
222int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
223{
224 void __iomem *pio = pin_to_controller(pin);
225 unsigned mask = pin_to_mask(pin);
226
227 if (!pio)
228 return -EINVAL;
229
230 __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
231 return 0;
232}
233EXPORT_SYMBOL(at91_set_multi_drive);
234
SAN People73a59c12006-01-09 17:05:41 +0000235/*
236 * assuming the pin is muxed as a gpio output, set its value.
237 */
238int at91_set_gpio_value(unsigned pin, int value)
239{
240 void __iomem *pio = pin_to_controller(pin);
241 unsigned mask = pin_to_mask(pin);
242
243 if (!pio)
244 return -EINVAL;
245 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
246 return 0;
247}
248EXPORT_SYMBOL(at91_set_gpio_value);
249
250
251/*
252 * read the pin's value (works even if it's not muxed as a gpio).
253 */
254int at91_get_gpio_value(unsigned pin)
255{
256 void __iomem *pio = pin_to_controller(pin);
257 unsigned mask = pin_to_mask(pin);
258 u32 pdsr;
259
260 if (!pio)
261 return -EINVAL;
262 pdsr = __raw_readl(pio + PIO_PDSR);
263 return (pdsr & mask) != 0;
264}
265EXPORT_SYMBOL(at91_get_gpio_value);
266
267/*--------------------------------------------------------------------------*/
268
Andrew Victor814138f2006-06-19 15:26:54 +0100269#ifdef CONFIG_PM
270
Andrew Victorf2173832006-09-27 13:23:00 +0100271static u32 wakeups[MAX_GPIO_BANKS];
272static u32 backups[MAX_GPIO_BANKS];
Andrew Victor814138f2006-06-19 15:26:54 +0100273
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100274static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
Andrew Victor814138f2006-06-19 15:26:54 +0100275{
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800276 unsigned pin = irq_to_gpio(d->irq);
277 unsigned mask = pin_to_mask(pin);
278 unsigned bank = pin / 32;
Andrew Victor814138f2006-06-19 15:26:54 +0100279
Andrew Victor3ea163e2007-01-09 13:47:29 +0100280 if (unlikely(bank >= MAX_GPIO_BANKS))
Andrew Victor814138f2006-06-19 15:26:54 +0100281 return -EINVAL;
282
283 if (state)
Andrew Victor3ea163e2007-01-09 13:47:29 +0100284 wakeups[bank] |= mask;
Andrew Victor814138f2006-06-19 15:26:54 +0100285 else
Andrew Victor3ea163e2007-01-09 13:47:29 +0100286 wakeups[bank] &= ~mask;
287
Jean-Christophe PLAGNIOL-VILLARD1a2d9152011-10-17 14:28:38 +0800288 irq_set_irq_wake(gpio_chip[bank].id, state);
Andrew Victor814138f2006-06-19 15:26:54 +0100289
290 return 0;
291}
292
293void at91_gpio_suspend(void)
294{
295 int i;
296
Andrew Victorf2173832006-09-27 13:23:00 +0100297 for (i = 0; i < gpio_banks; i++) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100298 void __iomem *pio = gpio_chip[i].regbase;
Andrew Victor814138f2006-06-19 15:26:54 +0100299
David Brownelle83aff52008-01-04 18:30:24 +0100300 backups[i] = __raw_readl(pio + PIO_IMR);
301 __raw_writel(backups[i], pio + PIO_IDR);
302 __raw_writel(wakeups[i], pio + PIO_IER);
Andrew Victor814138f2006-06-19 15:26:54 +0100303
Andrew Victor3ea163e2007-01-09 13:47:29 +0100304 if (!wakeups[i])
Jean-Christophe PLAGNIOL-VILLARD619d4a42011-11-13 13:00:58 +0800305 clk_disable(gpio_chip[i].clock);
Andrew Victor3ea163e2007-01-09 13:47:29 +0100306 else {
Andrew Victor814138f2006-06-19 15:26:54 +0100307#ifdef CONFIG_PM_DEBUG
Andrew Victor3ea163e2007-01-09 13:47:29 +0100308 printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
Andrew Victor814138f2006-06-19 15:26:54 +0100309#endif
310 }
311 }
312}
313
314void at91_gpio_resume(void)
315{
316 int i;
317
Andrew Victorf2173832006-09-27 13:23:00 +0100318 for (i = 0; i < gpio_banks; i++) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100319 void __iomem *pio = gpio_chip[i].regbase;
Andrew Victor814138f2006-06-19 15:26:54 +0100320
Andrew Victor3ea163e2007-01-09 13:47:29 +0100321 if (!wakeups[i])
Jean-Christophe PLAGNIOL-VILLARD619d4a42011-11-13 13:00:58 +0800322 clk_enable(gpio_chip[i].clock);
Andrew Victor3ea163e2007-01-09 13:47:29 +0100323
David Brownelle83aff52008-01-04 18:30:24 +0100324 __raw_writel(wakeups[i], pio + PIO_IDR);
325 __raw_writel(backups[i], pio + PIO_IER);
Andrew Victorf2173832006-09-27 13:23:00 +0100326 }
Andrew Victor814138f2006-06-19 15:26:54 +0100327}
328
329#else
330#define gpio_irq_set_wake NULL
331#endif
332
SAN People73a59c12006-01-09 17:05:41 +0000333
334/* Several AIC controller irqs are dispatched through this GPIO handler.
335 * To use any AT91_PIN_* as an externally triggered IRQ, first call
336 * at91_set_gpio_input() then maybe enable its glitch filter.
337 * Then just request_irq() with the pin ID; it works like any ARM IRQ
338 * handler, though it always triggers on rising and falling edges.
339 *
340 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
341 * configuring them with at91_set_a_periph() or at91_set_b_periph().
342 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
343 */
344
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100345static void gpio_irq_mask(struct irq_data *d)
SAN People73a59c12006-01-09 17:05:41 +0000346{
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800347 unsigned pin = irq_to_gpio(d->irq);
348 void __iomem *pio = pin_to_controller(pin);
349 unsigned mask = pin_to_mask(pin);
SAN People73a59c12006-01-09 17:05:41 +0000350
351 if (pio)
352 __raw_writel(mask, pio + PIO_IDR);
353}
354
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100355static void gpio_irq_unmask(struct irq_data *d)
SAN People73a59c12006-01-09 17:05:41 +0000356{
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800357 unsigned pin = irq_to_gpio(d->irq);
358 void __iomem *pio = pin_to_controller(pin);
359 unsigned mask = pin_to_mask(pin);
SAN People73a59c12006-01-09 17:05:41 +0000360
361 if (pio)
362 __raw_writel(mask, pio + PIO_IER);
363}
364
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100365static int gpio_irq_type(struct irq_data *d, unsigned type)
SAN People73a59c12006-01-09 17:05:41 +0000366{
David Brownelle83aff52008-01-04 18:30:24 +0100367 switch (type) {
368 case IRQ_TYPE_NONE:
369 case IRQ_TYPE_EDGE_BOTH:
370 return 0;
371 default:
372 return -EINVAL;
373 }
SAN People73a59c12006-01-09 17:05:41 +0000374}
375
David Brownell38c677c2006-08-01 22:26:25 +0100376static struct irq_chip gpio_irqchip = {
377 .name = "GPIO",
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100378 .irq_disable = gpio_irq_mask,
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100379 .irq_mask = gpio_irq_mask,
380 .irq_unmask = gpio_irq_unmask,
381 .irq_set_type = gpio_irq_type,
382 .irq_set_wake = gpio_irq_set_wake,
SAN People73a59c12006-01-09 17:05:41 +0000383};
384
Russell King10dd5ce2006-11-23 11:41:32 +0000385static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
SAN People73a59c12006-01-09 17:05:41 +0000386{
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800387 unsigned irq_pin;
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100388 struct irq_data *idata = irq_desc_get_irq_data(desc);
389 struct irq_chip *chip = irq_data_get_irq_chip(idata);
390 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(idata);
391 void __iomem *pio = at91_gpio->regbase;
SAN People73a59c12006-01-09 17:05:41 +0000392 u32 isr;
393
SAN People73a59c12006-01-09 17:05:41 +0000394 /* temporarily mask (level sensitive) parent IRQ */
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100395 chip->irq_ack(idata);
SAN People73a59c12006-01-09 17:05:41 +0000396 for (;;) {
David Brownelle83aff52008-01-04 18:30:24 +0100397 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
398 * When there none are pending, we're finished unless we need
399 * to process multiple banks (like ID_PIOCDE on sam9263).
400 */
SAN People73a59c12006-01-09 17:05:41 +0000401 isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
David Brownelle83aff52008-01-04 18:30:24 +0100402 if (!isr) {
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100403 if (!at91_gpio->next)
David Brownelle83aff52008-01-04 18:30:24 +0100404 break;
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100405 at91_gpio = at91_gpio->next;
406 pio = at91_gpio->regbase;
David Brownelle83aff52008-01-04 18:30:24 +0100407 continue;
408 }
SAN People73a59c12006-01-09 17:05:41 +0000409
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800410 irq_pin = gpio_to_irq(at91_gpio->chip.base);
SAN People73a59c12006-01-09 17:05:41 +0000411
412 while (isr) {
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100413 if (isr & 1)
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800414 generic_handle_irq(irq_pin);
415 irq_pin++;
SAN People73a59c12006-01-09 17:05:41 +0000416 isr >>= 1;
417 }
418 }
Thomas Gleixnerac93cdb2011-03-24 12:48:18 +0100419 chip->irq_unmask(idata);
SAN People73a59c12006-01-09 17:05:41 +0000420 /* now it may re-trigger */
421}
422
Andrew Victorf2173832006-09-27 13:23:00 +0100423/*--------------------------------------------------------------------------*/
SAN People73a59c12006-01-09 17:05:41 +0000424
Andrew Victorb66545e2007-11-23 16:09:10 +0100425#ifdef CONFIG_DEBUG_FS
426
427static int at91_gpio_show(struct seq_file *s, void *unused)
428{
429 int bank, j;
430
431 /* print heading */
432 seq_printf(s, "Pin\t");
433 for (bank = 0; bank < gpio_banks; bank++) {
434 seq_printf(s, "PIO%c\t", 'A' + bank);
435 };
436 seq_printf(s, "\n\n");
437
438 /* print pin status */
439 for (j = 0; j < 32; j++) {
440 seq_printf(s, "%i:\t", j);
441
442 for (bank = 0; bank < gpio_banks; bank++) {
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800443 unsigned pin = (32 * bank) + j;
Andrew Victorb66545e2007-11-23 16:09:10 +0100444 void __iomem *pio = pin_to_controller(pin);
445 unsigned mask = pin_to_mask(pin);
446
447 if (__raw_readl(pio + PIO_PSR) & mask)
448 seq_printf(s, "GPIO:%s", __raw_readl(pio + PIO_PDSR) & mask ? "1" : "0");
449 else
450 seq_printf(s, "%s", __raw_readl(pio + PIO_ABSR) & mask ? "B" : "A");
451
452 seq_printf(s, "\t");
453 }
454
455 seq_printf(s, "\n");
456 }
457
458 return 0;
459}
460
461static int at91_gpio_open(struct inode *inode, struct file *file)
462{
463 return single_open(file, at91_gpio_show, NULL);
464}
465
466static const struct file_operations at91_gpio_operations = {
467 .open = at91_gpio_open,
468 .read = seq_read,
469 .llseek = seq_lseek,
470 .release = single_release,
471};
472
473static int __init at91_gpio_debugfs_init(void)
474{
475 /* /sys/kernel/debug/at91_gpio */
476 (void) debugfs_create_file("at91_gpio", S_IFREG | S_IRUGO, NULL, NULL, &at91_gpio_operations);
477 return 0;
478}
479postcore_initcall(at91_gpio_debugfs_init);
480
481#endif
482
483/*--------------------------------------------------------------------------*/
484
Andrew Victor2b768b62009-02-11 21:39:05 +0100485/*
486 * This lock class tells lockdep that GPIO irqs are in a different
David Brownell37aca702008-03-05 00:08:29 +0100487 * category than their parents, so it won't report false recursion.
488 */
489static struct lock_class_key gpio_lock_class;
490
Andrew Victorf2173832006-09-27 13:23:00 +0100491/*
492 * Called from the processor-specific init to enable GPIO interrupt support.
493 */
494void __init at91_gpio_irq_setup(void)
495{
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800496 unsigned pioc, irq = gpio_to_irq(0);
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100497 struct at91_gpio_chip *this, *prev;
Andrew Victorf2173832006-09-27 13:23:00 +0100498
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800499 for (pioc = 0, this = gpio_chip, prev = NULL;
David Brownelle83aff52008-01-04 18:30:24 +0100500 pioc++ < gpio_banks;
501 prev = this, this++) {
Jean-Christophe PLAGNIOL-VILLARD1a2d9152011-10-17 14:28:38 +0800502 unsigned id = this->id;
SAN People73a59c12006-01-09 17:05:41 +0000503 unsigned i;
504
David Brownelle83aff52008-01-04 18:30:24 +0100505 __raw_writel(~0, this->regbase + PIO_IDR);
SAN People73a59c12006-01-09 17:05:41 +0000506
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800507 for (i = 0, irq = gpio_to_irq(this->chip.base); i < 32;
508 i++, irq++) {
509 irq_set_lockdep_class(irq, &gpio_lock_class);
David Brownell37aca702008-03-05 00:08:29 +0100510
Andrew Victor814138f2006-06-19 15:26:54 +0100511 /*
512 * Can use the "simple" and not "edge" handler since it's
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200513 * shorter, and the AIC handles interrupts sanely.
Andrew Victor814138f2006-06-19 15:26:54 +0100514 */
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800515 irq_set_chip_and_handler(irq, &gpio_irqchip,
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100516 handle_simple_irq);
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800517 set_irq_flags(irq, IRQF_VALID);
SAN People73a59c12006-01-09 17:05:41 +0000518 }
519
David Brownelle83aff52008-01-04 18:30:24 +0100520 /* The toplevel handler handles one bank of GPIOs, except
521 * AT91SAM9263_ID_PIOCDE handles three... PIOC is first in
522 * the list, so we only set up that handler.
523 */
524 if (prev && prev->next == this)
525 continue;
526
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100527 irq_set_chip_data(id, this);
528 irq_set_chained_handler(id, gpio_irq_handler);
SAN People73a59c12006-01-09 17:05:41 +0000529 }
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800530 pr_info("AT91: %d gpio irqs in %d banks\n", irq, gpio_banks);
Andrew Victorf2173832006-09-27 13:23:00 +0100531}
532
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100533/* gpiolib support */
534static int at91_gpiolib_direction_input(struct gpio_chip *chip,
535 unsigned offset)
536{
537 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
538 void __iomem *pio = at91_gpio->regbase;
539 unsigned mask = 1 << offset;
540
541 __raw_writel(mask, pio + PIO_ODR);
542 return 0;
543}
544
545static int at91_gpiolib_direction_output(struct gpio_chip *chip,
546 unsigned offset, int val)
547{
548 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
549 void __iomem *pio = at91_gpio->regbase;
550 unsigned mask = 1 << offset;
551
552 __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
553 __raw_writel(mask, pio + PIO_OER);
554 return 0;
555}
556
557static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset)
558{
559 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
560 void __iomem *pio = at91_gpio->regbase;
561 unsigned mask = 1 << offset;
562 u32 pdsr;
563
564 pdsr = __raw_readl(pio + PIO_PDSR);
565 return (pdsr & mask) != 0;
566}
567
568static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val)
569{
570 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
571 void __iomem *pio = at91_gpio->regbase;
572 unsigned mask = 1 << offset;
573
574 __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
575}
576
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100577static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
578{
579 int i;
580
581 for (i = 0; i < chip->ngpio; i++) {
582 unsigned pin = chip->base + i;
583 void __iomem *pio = pin_to_controller(pin);
584 unsigned mask = pin_to_mask(pin);
585 const char *gpio_label;
586
587 gpio_label = gpiochip_is_requested(chip, i);
588 if (gpio_label) {
589 seq_printf(s, "[%s] GPIO%s%d: ",
590 gpio_label, chip->label, i);
591 if (__raw_readl(pio + PIO_PSR) & mask)
592 seq_printf(s, "[gpio] %s\n",
593 at91_get_gpio_value(pin) ?
594 "set" : "clear");
595 else
596 seq_printf(s, "[periph %s]\n",
597 __raw_readl(pio + PIO_ABSR) &
598 mask ? "B" : "A");
599 }
600 }
601}
602
Andrew Victorf2173832006-09-27 13:23:00 +0100603/*
604 * Called from the processor-specific init to enable GPIO pin support.
605 */
606void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
607{
David Brownelle83aff52008-01-04 18:30:24 +0100608 unsigned i;
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100609 struct at91_gpio_chip *at91_gpio, *last = NULL;
David Brownelle83aff52008-01-04 18:30:24 +0100610
Andrew Victorf2173832006-09-27 13:23:00 +0100611 BUG_ON(nr_banks > MAX_GPIO_BANKS);
612
Andrew Victorf2173832006-09-27 13:23:00 +0100613 gpio_banks = nr_banks;
David Brownelle83aff52008-01-04 18:30:24 +0100614
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100615 for (i = 0; i < nr_banks; i++) {
616 at91_gpio = &gpio_chip[i];
617
Jean-Christophe PLAGNIOL-VILLARD1a2d9152011-10-17 14:28:38 +0800618 at91_gpio->id = data[i].id;
Jean-Christophe PLAGNIOL-VILLARDd0fbda92011-09-17 21:49:36 +0800619 at91_gpio->chip.base = i * 32;
Jean-Christophe PLAGNIOL-VILLARD80e91cb2011-09-16 23:37:50 +0800620
Jean-Christophe PLAGNIOL-VILLARD1a2d9152011-10-17 14:28:38 +0800621 at91_gpio->regbase = ioremap(data[i].regbase, 512);
Jean-Christophe PLAGNIOL-VILLARD80e91cb2011-09-16 23:37:50 +0800622 if (!at91_gpio->regbase) {
623 pr_err("at91_gpio.%d, failed to map registers, ignoring.\n", i);
624 continue;
625 }
David Brownelle83aff52008-01-04 18:30:24 +0100626
Jean-Christophe PLAGNIOL-VILLARD619d4a42011-11-13 13:00:58 +0800627 at91_gpio->clock = clk_get_sys(NULL, at91_gpio->chip.label);
628 if (!at91_gpio->clock) {
629 pr_err("at91_gpio.%d, failed to get clock, ignoring.\n", i);
630 continue;
631 }
632
Andrew Victor2b768b62009-02-11 21:39:05 +0100633 /* enable PIO controller's clock */
Jean-Christophe PLAGNIOL-VILLARD619d4a42011-11-13 13:00:58 +0800634 clk_enable(at91_gpio->clock);
Andrew Victor2b768b62009-02-11 21:39:05 +0100635
David Brownelle83aff52008-01-04 18:30:24 +0100636 /* AT91SAM9263_ID_PIOCDE groups PIOC, PIOD, PIOE */
Jean-Christophe PLAGNIOL-VILLARD1a2d9152011-10-17 14:28:38 +0800637 if (last && last->id == at91_gpio->id)
Ryan Mallonf373e8c2009-02-10 21:02:08 +0100638 last->next = at91_gpio;
639 last = at91_gpio;
640
641 gpiochip_add(&at91_gpio->chip);
David Brownelle83aff52008-01-04 18:30:24 +0100642 }
SAN People73a59c12006-01-09 17:05:41 +0000643}