blob: 6aeddd68d8af040e048489d4ebff70fa94a79663 [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>
Thomas Gleixner07d265d2006-07-01 23:01:50 +010014#include <linux/interrupt.h>
15#include <linux/irq.h>
Andrew Victorb66545e2007-11-23 16:09:10 +010016#include <linux/debugfs.h>
17#include <linux/seq_file.h>
SAN People73a59c12006-01-09 17:05:41 +000018#include <linux/kernel.h>
19#include <linux/list.h>
20#include <linux/module.h>
21
22#include <asm/io.h>
Russell Kingea75ee92006-06-20 19:53:16 +010023#include <asm/hardware.h>
Andrew Victor55d8bae2006-11-30 17:16:43 +010024#include <asm/arch/at91_pio.h>
SAN People73a59c12006-01-09 17:05:41 +000025#include <asm/arch/gpio.h>
26
Andrew Victorf2173832006-09-27 13:23:00 +010027#include "generic.h"
28
29
30static struct at91_gpio_bank *gpio;
31static int gpio_banks;
32
SAN People73a59c12006-01-09 17:05:41 +000033
34static inline void __iomem *pin_to_controller(unsigned pin)
35{
36 void __iomem *sys_base = (void __iomem *) AT91_VA_BASE_SYS;
37
38 pin -= PIN_BASE;
39 pin /= 32;
Andrew Victorf2173832006-09-27 13:23:00 +010040 if (likely(pin < gpio_banks))
41 return sys_base + gpio[pin].offset;
SAN People73a59c12006-01-09 17:05:41 +000042
43 return NULL;
44}
45
46static inline unsigned pin_to_mask(unsigned pin)
47{
48 pin -= PIN_BASE;
49 return 1 << (pin % 32);
50}
51
52
53/*--------------------------------------------------------------------------*/
54
55/* Not all hardware capabilities are exposed through these calls; they
56 * only encapsulate the most common features and modes. (So if you
57 * want to change signals in groups, do it directly.)
58 *
59 * Bootloaders will usually handle some of the pin multiplexing setup.
60 * The intent is certainly that by the time Linux is fully booted, all
61 * pins should have been fully initialized. These setup calls should
62 * only be used by board setup routines, or possibly in driver probe().
63 *
64 * For bootloaders doing all that setup, these calls could be inlined
65 * as NOPs so Linux won't duplicate any setup code
66 */
67
68
69/*
David Brownella31c4ee2007-02-12 00:53:13 -080070 * mux the pin to the "GPIO" peripheral role.
71 */
72int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup)
73{
74 void __iomem *pio = pin_to_controller(pin);
75 unsigned mask = pin_to_mask(pin);
76
77 if (!pio)
78 return -EINVAL;
79 __raw_writel(mask, pio + PIO_IDR);
80 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
81 __raw_writel(mask, pio + PIO_PER);
82 return 0;
83}
84EXPORT_SYMBOL(at91_set_GPIO_periph);
85
86
87/*
SAN People73a59c12006-01-09 17:05:41 +000088 * mux the pin to the "A" internal peripheral role.
89 */
90int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
91{
92 void __iomem *pio = pin_to_controller(pin);
93 unsigned mask = pin_to_mask(pin);
94
95 if (!pio)
96 return -EINVAL;
97
98 __raw_writel(mask, pio + PIO_IDR);
99 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
100 __raw_writel(mask, pio + PIO_ASR);
101 __raw_writel(mask, pio + PIO_PDR);
102 return 0;
103}
104EXPORT_SYMBOL(at91_set_A_periph);
105
106
107/*
108 * mux the pin to the "B" internal peripheral role.
109 */
110int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
111{
112 void __iomem *pio = pin_to_controller(pin);
113 unsigned mask = pin_to_mask(pin);
114
115 if (!pio)
116 return -EINVAL;
117
118 __raw_writel(mask, pio + PIO_IDR);
119 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
120 __raw_writel(mask, pio + PIO_BSR);
121 __raw_writel(mask, pio + PIO_PDR);
122 return 0;
123}
124EXPORT_SYMBOL(at91_set_B_periph);
125
126
127/*
128 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
129 * configure it for an input.
130 */
131int __init_or_module at91_set_gpio_input(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_ODR);
142 __raw_writel(mask, pio + PIO_PER);
143 return 0;
144}
145EXPORT_SYMBOL(at91_set_gpio_input);
146
147
148/*
149 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
150 * and configure it for an output.
151 */
152int __init_or_module at91_set_gpio_output(unsigned pin, int value)
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 + PIO_PUDR);
162 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
163 __raw_writel(mask, pio + PIO_OER);
164 __raw_writel(mask, pio + PIO_PER);
165 return 0;
166}
167EXPORT_SYMBOL(at91_set_gpio_output);
168
169
170/*
171 * enable/disable the glitch filter; mostly used with IRQ handling.
172 */
173int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
174{
175 void __iomem *pio = pin_to_controller(pin);
176 unsigned mask = pin_to_mask(pin);
177
178 if (!pio)
179 return -EINVAL;
180 __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
181 return 0;
182}
183EXPORT_SYMBOL(at91_set_deglitch);
184
Andrew Victordf666b92006-02-22 21:23:35 +0000185/*
186 * enable/disable the multi-driver; This is only valid for output and
187 * allows the output pin to run as an open collector output.
188 */
189int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
190{
191 void __iomem *pio = pin_to_controller(pin);
192 unsigned mask = pin_to_mask(pin);
193
194 if (!pio)
195 return -EINVAL;
196
197 __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
198 return 0;
199}
200EXPORT_SYMBOL(at91_set_multi_drive);
201
SAN People73a59c12006-01-09 17:05:41 +0000202/*--------------------------------------------------------------------------*/
203
David Brownella31c4ee2007-02-12 00:53:13 -0800204/* new-style GPIO calls; these expect at91_set_GPIO_periph to have been
205 * called, and maybe at91_set_multi_drive() for putout pins.
206 */
207
208int gpio_direction_input(unsigned pin)
209{
210 void __iomem *pio = pin_to_controller(pin);
211 unsigned mask = pin_to_mask(pin);
212
213 if (!pio || !(__raw_readl(pio + PIO_PSR) & mask))
214 return -EINVAL;
Andrew Victor0ebffe32007-02-22 09:38:52 +0100215 __raw_writel(mask, pio + PIO_ODR);
David Brownella31c4ee2007-02-12 00:53:13 -0800216 return 0;
217}
218EXPORT_SYMBOL(gpio_direction_input);
219
David Brownell28735a72007-03-16 13:38:14 -0800220int gpio_direction_output(unsigned pin, int value)
David Brownella31c4ee2007-02-12 00:53:13 -0800221{
222 void __iomem *pio = pin_to_controller(pin);
223 unsigned mask = pin_to_mask(pin);
224
225 if (!pio || !(__raw_readl(pio + PIO_PSR) & mask))
226 return -EINVAL;
David Brownell28735a72007-03-16 13:38:14 -0800227 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
David Brownella31c4ee2007-02-12 00:53:13 -0800228 __raw_writel(mask, pio + PIO_OER);
229 return 0;
230}
231EXPORT_SYMBOL(gpio_direction_output);
232
233/*--------------------------------------------------------------------------*/
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
274static int gpio_irq_set_wake(unsigned pin, unsigned state)
275{
276 unsigned mask = pin_to_mask(pin);
Andrew Victor3ea163e2007-01-09 13:47:29 +0100277 unsigned bank = (pin - PIN_BASE) / 32;
Andrew Victor814138f2006-06-19 15:26:54 +0100278
Andrew Victor3ea163e2007-01-09 13:47:29 +0100279 if (unlikely(bank >= MAX_GPIO_BANKS))
Andrew Victor814138f2006-06-19 15:26:54 +0100280 return -EINVAL;
281
282 if (state)
Andrew Victor3ea163e2007-01-09 13:47:29 +0100283 wakeups[bank] |= mask;
Andrew Victor814138f2006-06-19 15:26:54 +0100284 else
Andrew Victor3ea163e2007-01-09 13:47:29 +0100285 wakeups[bank] &= ~mask;
286
287 set_irq_wake(gpio[bank].id, state);
Andrew Victor814138f2006-06-19 15:26:54 +0100288
289 return 0;
290}
291
292void at91_gpio_suspend(void)
293{
294 int i;
295
Andrew Victorf2173832006-09-27 13:23:00 +0100296 for (i = 0; i < gpio_banks; i++) {
297 u32 pio = gpio[i].offset;
Andrew Victor814138f2006-06-19 15:26:54 +0100298
Andrew Victor814138f2006-06-19 15:26:54 +0100299 backups[i] = at91_sys_read(pio + PIO_IMR);
Andrew Victorf2173832006-09-27 13:23:00 +0100300 at91_sys_write(pio + PIO_IDR, backups[i]);
301 at91_sys_write(pio + PIO_IER, wakeups[i]);
Andrew Victor814138f2006-06-19 15:26:54 +0100302
Andrew Victor3ea163e2007-01-09 13:47:29 +0100303 if (!wakeups[i])
304 clk_disable(gpio[i].clock);
305 else {
Andrew Victor814138f2006-06-19 15:26:54 +0100306#ifdef CONFIG_PM_DEBUG
Andrew Victor3ea163e2007-01-09 13:47:29 +0100307 printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
Andrew Victor814138f2006-06-19 15:26:54 +0100308#endif
309 }
310 }
311}
312
313void at91_gpio_resume(void)
314{
315 int i;
316
Andrew Victorf2173832006-09-27 13:23:00 +0100317 for (i = 0; i < gpio_banks; i++) {
318 u32 pio = gpio[i].offset;
Andrew Victor814138f2006-06-19 15:26:54 +0100319
Andrew Victor3ea163e2007-01-09 13:47:29 +0100320 if (!wakeups[i])
321 clk_enable(gpio[i].clock);
322
Andrew Victorf2173832006-09-27 13:23:00 +0100323 at91_sys_write(pio + PIO_IDR, wakeups[i]);
324 at91_sys_write(pio + PIO_IER, backups[i]);
Andrew Victorf2173832006-09-27 13:23:00 +0100325 }
Andrew Victor814138f2006-06-19 15:26:54 +0100326}
327
328#else
329#define gpio_irq_set_wake NULL
330#endif
331
SAN People73a59c12006-01-09 17:05:41 +0000332
333/* Several AIC controller irqs are dispatched through this GPIO handler.
334 * To use any AT91_PIN_* as an externally triggered IRQ, first call
335 * at91_set_gpio_input() then maybe enable its glitch filter.
336 * Then just request_irq() with the pin ID; it works like any ARM IRQ
337 * handler, though it always triggers on rising and falling edges.
338 *
339 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
340 * configuring them with at91_set_a_periph() or at91_set_b_periph().
341 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
342 */
343
344static void gpio_irq_mask(unsigned pin)
345{
346 void __iomem *pio = pin_to_controller(pin);
347 unsigned mask = pin_to_mask(pin);
348
349 if (pio)
350 __raw_writel(mask, pio + PIO_IDR);
351}
352
353static void gpio_irq_unmask(unsigned pin)
354{
355 void __iomem *pio = pin_to_controller(pin);
356 unsigned mask = pin_to_mask(pin);
357
358 if (pio)
359 __raw_writel(mask, pio + PIO_IER);
360}
361
362static int gpio_irq_type(unsigned pin, unsigned type)
363{
364 return (type == IRQT_BOTHEDGE) ? 0 : -EINVAL;
365}
366
David Brownell38c677c2006-08-01 22:26:25 +0100367static struct irq_chip gpio_irqchip = {
368 .name = "GPIO",
SAN People73a59c12006-01-09 17:05:41 +0000369 .mask = gpio_irq_mask,
370 .unmask = gpio_irq_unmask,
371 .set_type = gpio_irq_type,
Andrew Victor814138f2006-06-19 15:26:54 +0100372 .set_wake = gpio_irq_set_wake,
SAN People73a59c12006-01-09 17:05:41 +0000373};
374
Russell King10dd5ce2006-11-23 11:41:32 +0000375static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
SAN People73a59c12006-01-09 17:05:41 +0000376{
377 unsigned pin;
Russell King10dd5ce2006-11-23 11:41:32 +0000378 struct irq_desc *gpio;
SAN People73a59c12006-01-09 17:05:41 +0000379 void __iomem *pio;
380 u32 isr;
381
Thomas Gleixner07d265d2006-07-01 23:01:50 +0100382 pio = get_irq_chip_data(irq);
SAN People73a59c12006-01-09 17:05:41 +0000383
384 /* temporarily mask (level sensitive) parent IRQ */
385 desc->chip->ack(irq);
386 for (;;) {
Andrew Victor814138f2006-06-19 15:26:54 +0100387 /* reading ISR acks the pending (edge triggered) GPIO interrupt */
SAN People73a59c12006-01-09 17:05:41 +0000388 isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
389 if (!isr)
390 break;
391
Thomas Gleixner07d265d2006-07-01 23:01:50 +0100392 pin = (unsigned) get_irq_data(irq);
SAN People73a59c12006-01-09 17:05:41 +0000393 gpio = &irq_desc[pin];
394
395 while (isr) {
Andrew Victorabbea712006-02-24 22:27:50 +0000396 if (isr & 1) {
Thomas Gleixner07d265d2006-07-01 23:01:50 +0100397 if (unlikely(gpio->depth)) {
Andrew Victorabbea712006-02-24 22:27:50 +0000398 /*
399 * The core ARM interrupt handler lazily disables IRQs so
400 * another IRQ must be generated before it actually gets
401 * here to be disabled on the GPIO controller.
402 */
403 gpio_irq_mask(pin);
404 }
405 else
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700406 desc_handle_irq(pin, gpio);
Andrew Victorabbea712006-02-24 22:27:50 +0000407 }
SAN People73a59c12006-01-09 17:05:41 +0000408 pin++;
409 gpio++;
410 isr >>= 1;
411 }
412 }
413 desc->chip->unmask(irq);
414 /* now it may re-trigger */
415}
416
Andrew Victorf2173832006-09-27 13:23:00 +0100417/*--------------------------------------------------------------------------*/
SAN People73a59c12006-01-09 17:05:41 +0000418
Andrew Victorb66545e2007-11-23 16:09:10 +0100419#ifdef CONFIG_DEBUG_FS
420
421static int at91_gpio_show(struct seq_file *s, void *unused)
422{
423 int bank, j;
424
425 /* print heading */
426 seq_printf(s, "Pin\t");
427 for (bank = 0; bank < gpio_banks; bank++) {
428 seq_printf(s, "PIO%c\t", 'A' + bank);
429 };
430 seq_printf(s, "\n\n");
431
432 /* print pin status */
433 for (j = 0; j < 32; j++) {
434 seq_printf(s, "%i:\t", j);
435
436 for (bank = 0; bank < gpio_banks; bank++) {
437 unsigned pin = PIN_BASE + (32 * bank) + j;
438 void __iomem *pio = pin_to_controller(pin);
439 unsigned mask = pin_to_mask(pin);
440
441 if (__raw_readl(pio + PIO_PSR) & mask)
442 seq_printf(s, "GPIO:%s", __raw_readl(pio + PIO_PDSR) & mask ? "1" : "0");
443 else
444 seq_printf(s, "%s", __raw_readl(pio + PIO_ABSR) & mask ? "B" : "A");
445
446 seq_printf(s, "\t");
447 }
448
449 seq_printf(s, "\n");
450 }
451
452 return 0;
453}
454
455static int at91_gpio_open(struct inode *inode, struct file *file)
456{
457 return single_open(file, at91_gpio_show, NULL);
458}
459
460static const struct file_operations at91_gpio_operations = {
461 .open = at91_gpio_open,
462 .read = seq_read,
463 .llseek = seq_lseek,
464 .release = single_release,
465};
466
467static int __init at91_gpio_debugfs_init(void)
468{
469 /* /sys/kernel/debug/at91_gpio */
470 (void) debugfs_create_file("at91_gpio", S_IFREG | S_IRUGO, NULL, NULL, &at91_gpio_operations);
471 return 0;
472}
473postcore_initcall(at91_gpio_debugfs_init);
474
475#endif
476
477/*--------------------------------------------------------------------------*/
478
Andrew Victorf2173832006-09-27 13:23:00 +0100479/*
480 * Called from the processor-specific init to enable GPIO interrupt support.
481 */
482void __init at91_gpio_irq_setup(void)
483{
484 unsigned pioc, pin;
485
486 for (pioc = 0, pin = PIN_BASE;
487 pioc < gpio_banks;
488 pioc++) {
SAN People73a59c12006-01-09 17:05:41 +0000489 void __iomem *controller;
Andrew Victorf2173832006-09-27 13:23:00 +0100490 unsigned id = gpio[pioc].id;
SAN People73a59c12006-01-09 17:05:41 +0000491 unsigned i;
492
Andrew Victorf2173832006-09-27 13:23:00 +0100493 clk_enable(gpio[pioc].clock); /* enable PIO controller's clock */
494
495 controller = (void __iomem *) AT91_VA_BASE_SYS + gpio[pioc].offset;
SAN People73a59c12006-01-09 17:05:41 +0000496 __raw_writel(~0, controller + PIO_IDR);
497
498 set_irq_data(id, (void *) pin);
Russell King10dd5ce2006-11-23 11:41:32 +0000499 set_irq_chip_data(id, controller);
SAN People73a59c12006-01-09 17:05:41 +0000500
501 for (i = 0; i < 32; i++, pin++) {
Andrew Victor814138f2006-06-19 15:26:54 +0100502 /*
503 * Can use the "simple" and not "edge" handler since it's
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200504 * shorter, and the AIC handles interrupts sanely.
Andrew Victor814138f2006-06-19 15:26:54 +0100505 */
SAN People73a59c12006-01-09 17:05:41 +0000506 set_irq_chip(pin, &gpio_irqchip);
Russell King10dd5ce2006-11-23 11:41:32 +0000507 set_irq_handler(pin, handle_simple_irq);
SAN People73a59c12006-01-09 17:05:41 +0000508 set_irq_flags(pin, IRQF_VALID);
509 }
510
511 set_irq_chained_handler(id, gpio_irq_handler);
SAN People73a59c12006-01-09 17:05:41 +0000512 }
Andrew Victorf2173832006-09-27 13:23:00 +0100513 pr_info("AT91: %d gpio irqs in %d banks\n", pin - PIN_BASE, gpio_banks);
514}
515
516/*
517 * Called from the processor-specific init to enable GPIO pin support.
518 */
519void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
520{
521 BUG_ON(nr_banks > MAX_GPIO_BANKS);
522
523 gpio = data;
524 gpio_banks = nr_banks;
SAN People73a59c12006-01-09 17:05:41 +0000525}