blob: c9d9c702c7d55b52b7896309ada3029bd95000bd [file] [log] [blame]
Philipp Zabel1c44f5f2008-02-04 22:28:22 -08001/*
2 * linux/arch/arm/mach-pxa/gpio.c
3 *
4 * Generic PXA GPIO handling
5 *
6 * Author: Nicolas Pitre
7 * Created: Jun 15, 2001
8 * Copyright: MontaVista Software Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/init.h>
16#include <linux/module.h>
eric miaoe3630db2008-03-04 11:42:26 +080017#include <linux/irq.h>
eric miao663707c2008-03-04 16:13:58 +080018#include <linux/sysdev.h>
Russell Kingfced80c2008-09-06 12:10:45 +010019#include <linux/io.h>
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080020
21#include <asm/gpio.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010022#include <mach/hardware.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010023#include <mach/pxa-regs.h>
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080024
25#include "generic.h"
26
Eric Miaof1647e42008-11-28 14:54:39 +080027#define GPIO0_BASE ((void __iomem *)io_p2v(0x40E00000))
28#define GPIO1_BASE ((void __iomem *)io_p2v(0x40E00004))
29#define GPIO2_BASE ((void __iomem *)io_p2v(0x40E00008))
30#define GPIO3_BASE ((void __iomem *)io_p2v(0x40E00100))
31
32#define GPLR_OFFSET 0x00
33#define GPDR_OFFSET 0x0C
34#define GPSR_OFFSET 0x18
35#define GPCR_OFFSET 0x24
36#define GRER_OFFSET 0x30
37#define GFER_OFFSET 0x3C
38#define GEDR_OFFSET 0x48
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080039
40struct pxa_gpio_chip {
41 struct gpio_chip chip;
42 void __iomem *regbase;
43};
44
45int pxa_last_gpio;
46
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080047static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
48{
49 unsigned long flags;
50 u32 mask = 1 << offset;
51 u32 value;
52 struct pxa_gpio_chip *pxa;
53 void __iomem *gpdr;
54
55 pxa = container_of(chip, struct pxa_gpio_chip, chip);
56 gpdr = pxa->regbase + GPDR_OFFSET;
57 local_irq_save(flags);
58 value = __raw_readl(gpdr);
Eric Miao067455a2008-11-26 18:12:04 +080059 if (__gpio_is_inverted(chip->base + offset))
60 value |= mask;
61 else
62 value &= ~mask;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080063 __raw_writel(value, gpdr);
64 local_irq_restore(flags);
65
66 return 0;
67}
68
69static int pxa_gpio_direction_output(struct gpio_chip *chip,
70 unsigned offset, int value)
71{
72 unsigned long flags;
73 u32 mask = 1 << offset;
74 u32 tmp;
75 struct pxa_gpio_chip *pxa;
76 void __iomem *gpdr;
77
78 pxa = container_of(chip, struct pxa_gpio_chip, chip);
79 __raw_writel(mask,
80 pxa->regbase + (value ? GPSR_OFFSET : GPCR_OFFSET));
81 gpdr = pxa->regbase + GPDR_OFFSET;
82 local_irq_save(flags);
83 tmp = __raw_readl(gpdr);
Eric Miao067455a2008-11-26 18:12:04 +080084 if (__gpio_is_inverted(chip->base + offset))
85 tmp &= ~mask;
86 else
87 tmp |= mask;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080088 __raw_writel(tmp, gpdr);
89 local_irq_restore(flags);
90
91 return 0;
92}
93
94/*
95 * Return GPIO level
96 */
97static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
98{
99 u32 mask = 1 << offset;
100 struct pxa_gpio_chip *pxa;
101
102 pxa = container_of(chip, struct pxa_gpio_chip, chip);
103 return __raw_readl(pxa->regbase + GPLR_OFFSET) & mask;
104}
105
106/*
107 * Set output GPIO level
108 */
109static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
110{
111 u32 mask = 1 << offset;
112 struct pxa_gpio_chip *pxa;
113
114 pxa = container_of(chip, struct pxa_gpio_chip, chip);
115
116 if (value)
117 __raw_writel(mask, pxa->regbase + GPSR_OFFSET);
118 else
119 __raw_writel(mask, pxa->regbase + GPCR_OFFSET);
120}
121
eric miao0e037bb2008-03-03 13:20:20 +0800122#define GPIO_CHIP(_n) \
123 [_n] = { \
124 .regbase = GPIO##_n##_BASE, \
125 .chip = { \
126 .label = "gpio-" #_n, \
127 .direction_input = pxa_gpio_direction_input, \
128 .direction_output = pxa_gpio_direction_output, \
129 .get = pxa_gpio_get, \
130 .set = pxa_gpio_set, \
131 .base = (_n) * 32, \
132 .ngpio = 32, \
133 }, \
134 }
135
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800136static struct pxa_gpio_chip pxa_gpio_chip[] = {
eric miao0e037bb2008-03-03 13:20:20 +0800137 GPIO_CHIP(0),
138 GPIO_CHIP(1),
139 GPIO_CHIP(2),
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800140#if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
eric miao0e037bb2008-03-03 13:20:20 +0800141 GPIO_CHIP(3),
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800142#endif
143};
144
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800145static void __init pxa_init_gpio_chip(int gpio_nr)
146{
147 int i, gpio;
148
149 /* add a GPIO chip for each register bank.
150 * the last PXA25x register only contains 21 GPIOs
151 */
152 for (gpio = 0, i = 0; gpio < gpio_nr; gpio += 32, i++) {
153 if (gpio + 32 > gpio_nr)
154 pxa_gpio_chip[i].chip.ngpio = gpio_nr - gpio;
155 gpiochip_add(&pxa_gpio_chip[i].chip);
156 }
157}
158
eric miaoe3630db2008-03-04 11:42:26 +0800159/*
160 * PXA GPIO edge detection for IRQs:
161 * IRQs are generated on Falling-Edge, Rising-Edge, or both.
162 * Use this instead of directly setting GRER/GFER.
163 */
164
Dmitry Baryshkovd8a42fc2008-04-19 10:42:18 +0100165static unsigned long GPIO_IRQ_rising_edge[4];
166static unsigned long GPIO_IRQ_falling_edge[4];
167static unsigned long GPIO_IRQ_mask[4];
eric miaoe3630db2008-03-04 11:42:26 +0800168
169static int pxa_gpio_irq_type(unsigned int irq, unsigned int type)
170{
171 int gpio, idx;
172
173 gpio = IRQ_TO_GPIO(irq);
174 idx = gpio >> 5;
175
176 if (type == IRQ_TYPE_PROBE) {
177 /* Don't mess with enabled GPIOs using preconfigured edges or
178 * GPIOs set to alternate function or to output during probe
179 */
Eric Miao067455a2008-11-26 18:12:04 +0800180 if ((GPIO_IRQ_rising_edge[idx] & GPIO_bit(gpio)) ||
181 (GPIO_IRQ_falling_edge[idx] & GPIO_bit(gpio)))
eric miaoe3630db2008-03-04 11:42:26 +0800182 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800183
184 if (__gpio_is_occupied(gpio))
eric miaoe3630db2008-03-04 11:42:26 +0800185 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800186
eric miaoe3630db2008-03-04 11:42:26 +0800187 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
188 }
189
Eric Miao067455a2008-11-26 18:12:04 +0800190 if (__gpio_is_inverted(gpio))
191 GPDR(gpio) |= GPIO_bit(gpio);
192 else
193 GPDR(gpio) &= ~GPIO_bit(gpio);
eric miaoe3630db2008-03-04 11:42:26 +0800194
195 if (type & IRQ_TYPE_EDGE_RISING)
196 __set_bit(gpio, GPIO_IRQ_rising_edge);
197 else
198 __clear_bit(gpio, GPIO_IRQ_rising_edge);
199
200 if (type & IRQ_TYPE_EDGE_FALLING)
201 __set_bit(gpio, GPIO_IRQ_falling_edge);
202 else
203 __clear_bit(gpio, GPIO_IRQ_falling_edge);
204
205 GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
206 GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
207
208 pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, irq, gpio,
209 ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
210 ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
211 return 0;
212}
213
214/*
eric miaoe3630db2008-03-04 11:42:26 +0800215 * Demux handler for GPIO>=2 edge detect interrupts
216 */
217
218#define GEDR_BITS (sizeof(gedr) * BITS_PER_BYTE)
219
220static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
221{
222 int loop, bit, n;
223 unsigned long gedr[4];
224
225 do {
226 gedr[0] = GEDR0 & GPIO_IRQ_mask[0] & ~3;
227 gedr[1] = GEDR1 & GPIO_IRQ_mask[1];
228 gedr[2] = GEDR2 & GPIO_IRQ_mask[2];
229 gedr[3] = GEDR3 & GPIO_IRQ_mask[3];
230
231 GEDR0 = gedr[0]; GEDR1 = gedr[1];
232 GEDR2 = gedr[2]; GEDR3 = gedr[3];
233
234 loop = 0;
235 bit = find_first_bit(gedr, GEDR_BITS);
236 while (bit < GEDR_BITS) {
237 loop = 1;
238
239 n = PXA_GPIO_IRQ_BASE + bit;
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100240 generic_handle_irq(n);
eric miaoe3630db2008-03-04 11:42:26 +0800241
242 bit = find_next_bit(gedr, GEDR_BITS, bit + 1);
243 }
244 } while (loop);
245}
246
247static void pxa_ack_muxed_gpio(unsigned int irq)
248{
249 int gpio = irq - IRQ_GPIO(2) + 2;
250 GEDR(gpio) = GPIO_bit(gpio);
251}
252
253static void pxa_mask_muxed_gpio(unsigned int irq)
254{
255 int gpio = irq - IRQ_GPIO(2) + 2;
256 __clear_bit(gpio, GPIO_IRQ_mask);
257 GRER(gpio) &= ~GPIO_bit(gpio);
258 GFER(gpio) &= ~GPIO_bit(gpio);
259}
260
261static void pxa_unmask_muxed_gpio(unsigned int irq)
262{
263 int gpio = irq - IRQ_GPIO(2) + 2;
264 int idx = gpio >> 5;
265 __set_bit(gpio, GPIO_IRQ_mask);
266 GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
267 GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
268}
269
270static struct irq_chip pxa_muxed_gpio_chip = {
271 .name = "GPIO",
272 .ack = pxa_ack_muxed_gpio,
273 .mask = pxa_mask_muxed_gpio,
274 .unmask = pxa_unmask_muxed_gpio,
275 .set_type = pxa_gpio_irq_type,
276};
277
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800278void __init pxa_init_gpio(int mux_irq, int start, int end, set_wake_t fn)
eric miaoe3630db2008-03-04 11:42:26 +0800279{
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800280 int irq, i;
eric miaoe3630db2008-03-04 11:42:26 +0800281
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800282 pxa_last_gpio = end;
eric miaoe3630db2008-03-04 11:42:26 +0800283
284 /* clear all GPIO edge detects */
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800285 for (i = start; i <= end; i += 32) {
286 GFER(i) &= ~GPIO_IRQ_mask[i];
287 GRER(i) &= ~GPIO_IRQ_mask[i];
288 GEDR(i) = GPIO_IRQ_mask[i];
eric miaoe3630db2008-03-04 11:42:26 +0800289 }
290
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800291 for (irq = gpio_to_irq(start); irq <= gpio_to_irq(end); irq++) {
eric miaoe3630db2008-03-04 11:42:26 +0800292 set_irq_chip(irq, &pxa_muxed_gpio_chip);
293 set_irq_handler(irq, handle_edge_irq);
294 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
295 }
296
297 /* Install handler for GPIO>=2 edge detect interrupts */
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800298 set_irq_chained_handler(mux_irq, pxa_gpio_demux_handler);
eric miaob9e25ac2008-03-04 14:19:58 +0800299 pxa_muxed_gpio_chip.set_wake = fn;
eric miaoe3630db2008-03-04 11:42:26 +0800300
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800301 /* Initialize GPIO chips */
302 pxa_init_gpio_chip(end + 1);
eric miaoe3630db2008-03-04 11:42:26 +0800303}
eric miao663707c2008-03-04 16:13:58 +0800304
305#ifdef CONFIG_PM
306
307static unsigned long saved_gplr[4];
308static unsigned long saved_gpdr[4];
309static unsigned long saved_grer[4];
310static unsigned long saved_gfer[4];
311
312static int pxa_gpio_suspend(struct sys_device *dev, pm_message_t state)
313{
314 int i, gpio;
315
316 for (gpio = 0, i = 0; gpio < pxa_last_gpio; gpio += 32, i++) {
317 saved_gplr[i] = GPLR(gpio);
318 saved_gpdr[i] = GPDR(gpio);
319 saved_grer[i] = GRER(gpio);
320 saved_gfer[i] = GFER(gpio);
321
322 /* Clear GPIO transition detect bits */
323 GEDR(gpio) = GEDR(gpio);
324 }
325 return 0;
326}
327
328static int pxa_gpio_resume(struct sys_device *dev)
329{
330 int i, gpio;
331
332 for (gpio = 0, i = 0; gpio < pxa_last_gpio; gpio += 32, i++) {
333 /* restore level with set/clear */
334 GPSR(gpio) = saved_gplr[i];
335 GPCR(gpio) = ~saved_gplr[i];
336
337 GRER(gpio) = saved_grer[i];
338 GFER(gpio) = saved_gfer[i];
339 GPDR(gpio) = saved_gpdr[i];
340 }
341 return 0;
342}
343#else
344#define pxa_gpio_suspend NULL
345#define pxa_gpio_resume NULL
346#endif
347
348struct sysdev_class pxa_gpio_sysclass = {
349 .name = "gpio",
350 .suspend = pxa_gpio_suspend,
351 .resume = pxa_gpio_resume,
352};
353
354static int __init pxa_gpio_init(void)
355{
356 return sysdev_class_register(&pxa_gpio_sysclass);
357}
358
359core_initcall(pxa_gpio_init);