blob: 31d2da4100cd46f7b986c3bd5d2ee41b1f8a7015 [file] [log] [blame]
Philipp Zabel1c44f5f2008-02-04 22:28:22 -08001/*
Eric Miao38f539a2009-01-20 12:09:06 +08002 * linux/arch/arm/plat-pxa/gpio.c
Philipp Zabel1c44f5f2008-02-04 22:28:22 -08003 *
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 */
Russell King2f8163b2011-07-26 10:53:52 +010014#include <linux/gpio.h>
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080015#include <linux/init.h>
eric miaoe3630db2008-03-04 11:42:26 +080016#include <linux/irq.h>
Russell Kingfced80c2008-09-06 12:10:45 +010017#include <linux/io.h>
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +020018#include <linux/syscore_ops.h>
Daniel Mack4aa78262009-06-19 22:56:09 +020019#include <linux/slab.h>
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080020
Linus Walleijf55be1b2011-09-28 09:11:30 +010021#include <mach/gpio-pxa.h>
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080022
Eric Miao3b8e2852009-01-07 11:30:49 +080023int pxa_last_gpio;
24
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080025struct pxa_gpio_chip {
26 struct gpio_chip chip;
Eric Miao0807da52009-01-07 18:01:51 +080027 void __iomem *regbase;
28 char label[10];
29
30 unsigned long irq_mask;
31 unsigned long irq_edge_rise;
32 unsigned long irq_edge_fall;
33
34#ifdef CONFIG_PM
35 unsigned long saved_gplr;
36 unsigned long saved_gpdr;
37 unsigned long saved_grer;
38 unsigned long saved_gfer;
39#endif
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080040};
41
Haojian Zhuang4929f5a2011-10-10 16:03:51 +080042enum {
43 PXA25X_GPIO = 0,
44 PXA26X_GPIO,
45 PXA27X_GPIO,
46 PXA3XX_GPIO,
47 PXA93X_GPIO,
48 MMP_GPIO = 0x10,
49 MMP2_GPIO,
50};
51
Eric Miao0807da52009-01-07 18:01:51 +080052static DEFINE_SPINLOCK(gpio_lock);
53static struct pxa_gpio_chip *pxa_gpio_chips;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +080054static int gpio_type;
Eric Miao0807da52009-01-07 18:01:51 +080055
56#define for_each_gpio_chip(i, c) \
57 for (i = 0, c = &pxa_gpio_chips[0]; i <= pxa_last_gpio; i += 32, c++)
58
59static inline void __iomem *gpio_chip_base(struct gpio_chip *c)
60{
61 return container_of(c, struct pxa_gpio_chip, chip)->regbase;
62}
63
Linus Walleija0656852011-06-13 10:42:19 +020064static inline struct pxa_gpio_chip *gpio_to_pxachip(unsigned gpio)
Eric Miao0807da52009-01-07 18:01:51 +080065{
66 return &pxa_gpio_chips[gpio_to_bank(gpio)];
67}
68
Haojian Zhuang4929f5a2011-10-10 16:03:51 +080069static inline int gpio_is_pxa_type(int type)
70{
71 return (type & MMP_GPIO) == 0;
72}
73
74static inline int gpio_is_mmp_type(int type)
75{
76 return (type & MMP_GPIO) != 0;
77}
78
79#ifdef CONFIG_ARCH_PXA
80static inline int __pxa_gpio_to_irq(int gpio)
81{
82 if (gpio_is_pxa_type(gpio_type))
83 return PXA_GPIO_TO_IRQ(gpio);
84 return -1;
85}
86
87static inline int __pxa_irq_to_gpio(int irq)
88{
89 if (gpio_is_pxa_type(gpio_type))
90 return irq - PXA_GPIO_TO_IRQ(0);
91 return -1;
92}
93#else
94static inline int __pxa_gpio_to_irq(int gpio) { return -1; }
95static inline int __pxa_irq_to_gpio(int irq) { return -1; }
96#endif
97
98#ifdef CONFIG_ARCH_MMP
99static inline int __mmp_gpio_to_irq(int gpio)
100{
101 if (gpio_is_mmp_type(gpio_type))
102 return MMP_GPIO_TO_IRQ(gpio);
103 return -1;
104}
105
106static inline int __mmp_irq_to_gpio(int irq)
107{
108 if (gpio_is_mmp_type(gpio_type))
109 return irq - MMP_GPIO_TO_IRQ(0);
110 return -1;
111}
112#else
113static inline int __mmp_gpio_to_irq(int gpio) { return -1; }
114static inline int __mmp_irq_to_gpio(int irq) { return -1; }
115#endif
116
117static int pxa_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
118{
119 int gpio, ret;
120
121 gpio = chip->base + offset;
122 ret = __pxa_gpio_to_irq(gpio);
123 if (ret >= 0)
124 return ret;
125 return __mmp_gpio_to_irq(gpio);
126}
127
128int pxa_irq_to_gpio(int irq)
129{
130 int ret;
131
132 ret = __pxa_irq_to_gpio(irq);
133 if (ret >= 0)
134 return ret;
135 return __mmp_irq_to_gpio(irq);
136}
137
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800138static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
139{
Eric Miao0807da52009-01-07 18:01:51 +0800140 void __iomem *base = gpio_chip_base(chip);
141 uint32_t value, mask = 1 << offset;
142 unsigned long flags;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800143
Eric Miao0807da52009-01-07 18:01:51 +0800144 spin_lock_irqsave(&gpio_lock, flags);
145
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800146 value = readl_relaxed(base + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800147 if (__gpio_is_inverted(chip->base + offset))
148 value |= mask;
149 else
150 value &= ~mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800151 writel_relaxed(value, base + GPDR_OFFSET);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800152
Eric Miao0807da52009-01-07 18:01:51 +0800153 spin_unlock_irqrestore(&gpio_lock, flags);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800154 return 0;
155}
156
157static int pxa_gpio_direction_output(struct gpio_chip *chip,
Eric Miao0807da52009-01-07 18:01:51 +0800158 unsigned offset, int value)
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800159{
Eric Miao0807da52009-01-07 18:01:51 +0800160 void __iomem *base = gpio_chip_base(chip);
161 uint32_t tmp, mask = 1 << offset;
162 unsigned long flags;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800163
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800164 writel_relaxed(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET));
Eric Miao0807da52009-01-07 18:01:51 +0800165
166 spin_lock_irqsave(&gpio_lock, flags);
167
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800168 tmp = readl_relaxed(base + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800169 if (__gpio_is_inverted(chip->base + offset))
170 tmp &= ~mask;
171 else
172 tmp |= mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800173 writel_relaxed(tmp, base + GPDR_OFFSET);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800174
Eric Miao0807da52009-01-07 18:01:51 +0800175 spin_unlock_irqrestore(&gpio_lock, flags);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800176 return 0;
177}
178
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800179static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
180{
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800181 return readl_relaxed(gpio_chip_base(chip) + GPLR_OFFSET) & (1 << offset);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800182}
183
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800184static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
185{
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800186 writel_relaxed(1 << offset, gpio_chip_base(chip) +
Eric Miao0807da52009-01-07 18:01:51 +0800187 (value ? GPSR_OFFSET : GPCR_OFFSET));
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800188}
189
Eric Miao0807da52009-01-07 18:01:51 +0800190static int __init pxa_init_gpio_chip(int gpio_end)
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800191{
Eric Miao0807da52009-01-07 18:01:51 +0800192 int i, gpio, nbanks = gpio_to_bank(gpio_end) + 1;
193 struct pxa_gpio_chip *chips;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800194
Daniel Mack4aa78262009-06-19 22:56:09 +0200195 chips = kzalloc(nbanks * sizeof(struct pxa_gpio_chip), GFP_KERNEL);
Eric Miao0807da52009-01-07 18:01:51 +0800196 if (chips == NULL) {
197 pr_err("%s: failed to allocate GPIO chips\n", __func__);
198 return -ENOMEM;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800199 }
Eric Miao0807da52009-01-07 18:01:51 +0800200
201 for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
202 struct gpio_chip *c = &chips[i].chip;
203
204 sprintf(chips[i].label, "gpio-%d", i);
Arnd Bergmann97b09da2011-10-01 22:03:45 +0200205 chips[i].regbase = GPIO_BANK(i);
Eric Miao0807da52009-01-07 18:01:51 +0800206
207 c->base = gpio;
208 c->label = chips[i].label;
209
210 c->direction_input = pxa_gpio_direction_input;
211 c->direction_output = pxa_gpio_direction_output;
212 c->get = pxa_gpio_get;
213 c->set = pxa_gpio_set;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800214 c->to_irq = pxa_gpio_to_irq;
Eric Miao0807da52009-01-07 18:01:51 +0800215
216 /* number of GPIOs on last bank may be less than 32 */
217 c->ngpio = (gpio + 31 > gpio_end) ? (gpio_end - gpio + 1) : 32;
218 gpiochip_add(c);
219 }
220 pxa_gpio_chips = chips;
221 return 0;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800222}
223
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800224/* Update only those GRERx and GFERx edge detection register bits if those
225 * bits are set in c->irq_mask
226 */
227static inline void update_edge_detect(struct pxa_gpio_chip *c)
228{
229 uint32_t grer, gfer;
230
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800231 grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~c->irq_mask;
232 gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~c->irq_mask;
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800233 grer |= c->irq_edge_rise & c->irq_mask;
234 gfer |= c->irq_edge_fall & c->irq_mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800235 writel_relaxed(grer, c->regbase + GRER_OFFSET);
236 writel_relaxed(gfer, c->regbase + GFER_OFFSET);
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800237}
238
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100239static int pxa_gpio_irq_type(struct irq_data *d, unsigned int type)
eric miaoe3630db2008-03-04 11:42:26 +0800240{
Eric Miao0807da52009-01-07 18:01:51 +0800241 struct pxa_gpio_chip *c;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800242 int gpio = pxa_irq_to_gpio(d->irq);
Eric Miao0807da52009-01-07 18:01:51 +0800243 unsigned long gpdr, mask = GPIO_bit(gpio);
eric miaoe3630db2008-03-04 11:42:26 +0800244
Linus Walleija0656852011-06-13 10:42:19 +0200245 c = gpio_to_pxachip(gpio);
eric miaoe3630db2008-03-04 11:42:26 +0800246
247 if (type == IRQ_TYPE_PROBE) {
248 /* Don't mess with enabled GPIOs using preconfigured edges or
249 * GPIOs set to alternate function or to output during probe
250 */
Eric Miao0807da52009-01-07 18:01:51 +0800251 if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
eric miaoe3630db2008-03-04 11:42:26 +0800252 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800253
254 if (__gpio_is_occupied(gpio))
eric miaoe3630db2008-03-04 11:42:26 +0800255 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800256
eric miaoe3630db2008-03-04 11:42:26 +0800257 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
258 }
259
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800260 gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
Eric Miao0807da52009-01-07 18:01:51 +0800261
Eric Miao067455a2008-11-26 18:12:04 +0800262 if (__gpio_is_inverted(gpio))
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800263 writel_relaxed(gpdr | mask, c->regbase + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800264 else
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800265 writel_relaxed(gpdr & ~mask, c->regbase + GPDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800266
267 if (type & IRQ_TYPE_EDGE_RISING)
Eric Miao0807da52009-01-07 18:01:51 +0800268 c->irq_edge_rise |= mask;
eric miaoe3630db2008-03-04 11:42:26 +0800269 else
Eric Miao0807da52009-01-07 18:01:51 +0800270 c->irq_edge_rise &= ~mask;
eric miaoe3630db2008-03-04 11:42:26 +0800271
272 if (type & IRQ_TYPE_EDGE_FALLING)
Eric Miao0807da52009-01-07 18:01:51 +0800273 c->irq_edge_fall |= mask;
eric miaoe3630db2008-03-04 11:42:26 +0800274 else
Eric Miao0807da52009-01-07 18:01:51 +0800275 c->irq_edge_fall &= ~mask;
eric miaoe3630db2008-03-04 11:42:26 +0800276
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800277 update_edge_detect(c);
eric miaoe3630db2008-03-04 11:42:26 +0800278
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100279 pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->irq, gpio,
eric miaoe3630db2008-03-04 11:42:26 +0800280 ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
281 ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
282 return 0;
283}
284
eric miaoe3630db2008-03-04 11:42:26 +0800285static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
286{
Eric Miao0807da52009-01-07 18:01:51 +0800287 struct pxa_gpio_chip *c;
288 int loop, gpio, gpio_base, n;
289 unsigned long gedr;
eric miaoe3630db2008-03-04 11:42:26 +0800290
291 do {
eric miaoe3630db2008-03-04 11:42:26 +0800292 loop = 0;
Eric Miao0807da52009-01-07 18:01:51 +0800293 for_each_gpio_chip(gpio, c) {
294 gpio_base = c->chip.base;
eric miaoe3630db2008-03-04 11:42:26 +0800295
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800296 gedr = readl_relaxed(c->regbase + GEDR_OFFSET);
Eric Miao0807da52009-01-07 18:01:51 +0800297 gedr = gedr & c->irq_mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800298 writel_relaxed(gedr, c->regbase + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800299
Eric Miao0807da52009-01-07 18:01:51 +0800300 n = find_first_bit(&gedr, BITS_PER_LONG);
301 while (n < BITS_PER_LONG) {
302 loop = 1;
303
304 generic_handle_irq(gpio_to_irq(gpio_base + n));
305 n = find_next_bit(&gedr, BITS_PER_LONG, n + 1);
306 }
eric miaoe3630db2008-03-04 11:42:26 +0800307 }
308 } while (loop);
309}
310
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100311static void pxa_ack_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800312{
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800313 int gpio = pxa_irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200314 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800315
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800316 writel_relaxed(GPIO_bit(gpio), c->regbase + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800317}
318
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100319static void pxa_mask_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800320{
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800321 int gpio = pxa_irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200322 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800323 uint32_t grer, gfer;
324
325 c->irq_mask &= ~GPIO_bit(gpio);
326
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800327 grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~GPIO_bit(gpio);
328 gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~GPIO_bit(gpio);
329 writel_relaxed(grer, c->regbase + GRER_OFFSET);
330 writel_relaxed(gfer, c->regbase + GFER_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800331}
332
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100333static void pxa_unmask_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800334{
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800335 int gpio = pxa_irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200336 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800337
338 c->irq_mask |= GPIO_bit(gpio);
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800339 update_edge_detect(c);
eric miaoe3630db2008-03-04 11:42:26 +0800340}
341
342static struct irq_chip pxa_muxed_gpio_chip = {
343 .name = "GPIO",
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100344 .irq_ack = pxa_ack_muxed_gpio,
345 .irq_mask = pxa_mask_muxed_gpio,
346 .irq_unmask = pxa_unmask_muxed_gpio,
347 .irq_set_type = pxa_gpio_irq_type,
eric miaoe3630db2008-03-04 11:42:26 +0800348};
349
Haojian Zhuang478e2232011-10-14 16:44:07 +0800350static int pxa_gpio_nums(void)
351{
352 int count = 0;
353
354#ifdef CONFIG_ARCH_PXA
355 if (cpu_is_pxa25x()) {
356#ifdef CONFIG_CPU_PXA26x
357 count = 89;
358 gpio_type = PXA26X_GPIO;
359#elif defined(CONFIG_PXA25x)
360 count = 84;
361 gpio_type = PXA26X_GPIO;
362#endif /* CONFIG_CPU_PXA26x */
363 } else if (cpu_is_pxa27x()) {
364 count = 120;
365 gpio_type = PXA27X_GPIO;
366 } else if (cpu_is_pxa93x() || cpu_is_pxa95x()) {
367 count = 191;
368 gpio_type = PXA93X_GPIO;
369 } else if (cpu_is_pxa3xx()) {
370 count = 127;
371 gpio_type = PXA3XX_GPIO;
372 }
373#endif /* CONFIG_ARCH_PXA */
374
375#ifdef CONFIG_ARCH_MMP
376 if (cpu_is_pxa168() || cpu_is_pxa910()) {
377 count = 127;
378 gpio_type = MMP_GPIO;
379 } else if (cpu_is_mmp2()) {
380 count = 191;
381 gpio_type = MMP2_GPIO;
382 }
383#endif /* CONFIG_ARCH_MMP */
384 return count;
385}
386
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800387void __init pxa_init_gpio(int mux_irq, int start, int end, set_wake_t fn)
eric miaoe3630db2008-03-04 11:42:26 +0800388{
Eric Miao0807da52009-01-07 18:01:51 +0800389 struct pxa_gpio_chip *c;
390 int gpio, irq;
eric miaoe3630db2008-03-04 11:42:26 +0800391
Haojian Zhuang478e2232011-10-14 16:44:07 +0800392 pxa_last_gpio = pxa_gpio_nums();
393 if (!pxa_last_gpio)
394 return;
eric miaoe3630db2008-03-04 11:42:26 +0800395
Eric Miao0807da52009-01-07 18:01:51 +0800396 /* Initialize GPIO chips */
397 pxa_init_gpio_chip(end);
398
eric miaoe3630db2008-03-04 11:42:26 +0800399 /* clear all GPIO edge detects */
Eric Miao0807da52009-01-07 18:01:51 +0800400 for_each_gpio_chip(gpio, c) {
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800401 writel_relaxed(0, c->regbase + GFER_OFFSET);
402 writel_relaxed(0, c->regbase + GRER_OFFSET);
403 writel_relaxed(~0,c->regbase + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800404 }
405
Haojian Zhuang87c49e22011-10-10 14:38:46 +0800406#ifdef CONFIG_ARCH_PXA
407 irq = gpio_to_irq(0);
408 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
409 handle_edge_irq);
410 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
411 irq_set_chained_handler(IRQ_GPIO0, pxa_gpio_demux_handler);
412
413 irq = gpio_to_irq(1);
414 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
415 handle_edge_irq);
416 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
417 irq_set_chained_handler(IRQ_GPIO1, pxa_gpio_demux_handler);
418#endif
419
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800420 for (irq = gpio_to_irq(start); irq <= gpio_to_irq(end); irq++) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100421 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
422 handle_edge_irq);
eric miaoe3630db2008-03-04 11:42:26 +0800423 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
424 }
425
426 /* Install handler for GPIO>=2 edge detect interrupts */
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100427 irq_set_chained_handler(mux_irq, pxa_gpio_demux_handler);
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100428 pxa_muxed_gpio_chip.irq_set_wake = fn;
eric miaoe3630db2008-03-04 11:42:26 +0800429}
eric miao663707c2008-03-04 16:13:58 +0800430
431#ifdef CONFIG_PM
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200432static int pxa_gpio_suspend(void)
eric miao663707c2008-03-04 16:13:58 +0800433{
Eric Miao0807da52009-01-07 18:01:51 +0800434 struct pxa_gpio_chip *c;
435 int gpio;
eric miao663707c2008-03-04 16:13:58 +0800436
Eric Miao0807da52009-01-07 18:01:51 +0800437 for_each_gpio_chip(gpio, c) {
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800438 c->saved_gplr = readl_relaxed(c->regbase + GPLR_OFFSET);
439 c->saved_gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
440 c->saved_grer = readl_relaxed(c->regbase + GRER_OFFSET);
441 c->saved_gfer = readl_relaxed(c->regbase + GFER_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800442
443 /* Clear GPIO transition detect bits */
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800444 writel_relaxed(0xffffffff, c->regbase + GEDR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800445 }
446 return 0;
447}
448
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200449static void pxa_gpio_resume(void)
eric miao663707c2008-03-04 16:13:58 +0800450{
Eric Miao0807da52009-01-07 18:01:51 +0800451 struct pxa_gpio_chip *c;
452 int gpio;
eric miao663707c2008-03-04 16:13:58 +0800453
Eric Miao0807da52009-01-07 18:01:51 +0800454 for_each_gpio_chip(gpio, c) {
eric miao663707c2008-03-04 16:13:58 +0800455 /* restore level with set/clear */
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800456 writel_relaxed( c->saved_gplr, c->regbase + GPSR_OFFSET);
457 writel_relaxed(~c->saved_gplr, c->regbase + GPCR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800458
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800459 writel_relaxed(c->saved_grer, c->regbase + GRER_OFFSET);
460 writel_relaxed(c->saved_gfer, c->regbase + GFER_OFFSET);
461 writel_relaxed(c->saved_gpdr, c->regbase + GPDR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800462 }
eric miao663707c2008-03-04 16:13:58 +0800463}
464#else
465#define pxa_gpio_suspend NULL
466#define pxa_gpio_resume NULL
467#endif
468
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200469struct syscore_ops pxa_gpio_syscore_ops = {
eric miao663707c2008-03-04 16:13:58 +0800470 .suspend = pxa_gpio_suspend,
471 .resume = pxa_gpio_resume,
472};