blob: bfd755531f7a737449c937cc9c67f8ae7cb2175d [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>
Haojian Zhuang157d2642011-10-17 20:37:52 +080015#include <linux/gpio-pxa.h>
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080016#include <linux/init.h>
eric miaoe3630db2008-03-04 11:42:26 +080017#include <linux/irq.h>
Russell Kingfced80c2008-09-06 12:10:45 +010018#include <linux/io.h>
Haojian Zhuang157d2642011-10-17 20:37:52 +080019#include <linux/platform_device.h>
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +020020#include <linux/syscore_ops.h>
Daniel Mack4aa78262009-06-19 22:56:09 +020021#include <linux/slab.h>
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080022
Haojian Zhuang157d2642011-10-17 20:37:52 +080023/*
24 * We handle the GPIOs by banks, each bank covers up to 32 GPIOs with
25 * one set of registers. The register offsets are organized below:
26 *
27 * GPLR GPDR GPSR GPCR GRER GFER GEDR
28 * BANK 0 - 0x0000 0x000C 0x0018 0x0024 0x0030 0x003C 0x0048
29 * BANK 1 - 0x0004 0x0010 0x001C 0x0028 0x0034 0x0040 0x004C
30 * BANK 2 - 0x0008 0x0014 0x0020 0x002C 0x0038 0x0044 0x0050
31 *
32 * BANK 3 - 0x0100 0x010C 0x0118 0x0124 0x0130 0x013C 0x0148
33 * BANK 4 - 0x0104 0x0110 0x011C 0x0128 0x0134 0x0140 0x014C
34 * BANK 5 - 0x0108 0x0114 0x0120 0x012C 0x0138 0x0144 0x0150
35 *
36 * NOTE:
37 * BANK 3 is only available on PXA27x and later processors.
38 * BANK 4 and 5 are only available on PXA935
39 */
40
41#define GPLR_OFFSET 0x00
42#define GPDR_OFFSET 0x0C
43#define GPSR_OFFSET 0x18
44#define GPCR_OFFSET 0x24
45#define GRER_OFFSET 0x30
46#define GFER_OFFSET 0x3C
47#define GEDR_OFFSET 0x48
48#define GAFR_OFFSET 0x54
Haojian Zhuangbe241682011-10-17 21:07:15 +080049#define ED_MASK_OFFSET 0x9C /* GPIO edge detection for AP side */
Haojian Zhuang157d2642011-10-17 20:37:52 +080050
51#define BANK_OFF(n) (((n) < 3) ? (n) << 2 : 0x100 + (((n) - 3) << 2))
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080052
Eric Miao3b8e2852009-01-07 11:30:49 +080053int pxa_last_gpio;
54
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080055struct pxa_gpio_chip {
56 struct gpio_chip chip;
Eric Miao0807da52009-01-07 18:01:51 +080057 void __iomem *regbase;
58 char label[10];
59
60 unsigned long irq_mask;
61 unsigned long irq_edge_rise;
62 unsigned long irq_edge_fall;
63
64#ifdef CONFIG_PM
65 unsigned long saved_gplr;
66 unsigned long saved_gpdr;
67 unsigned long saved_grer;
68 unsigned long saved_gfer;
69#endif
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080070};
71
Haojian Zhuang4929f5a2011-10-10 16:03:51 +080072enum {
73 PXA25X_GPIO = 0,
74 PXA26X_GPIO,
75 PXA27X_GPIO,
76 PXA3XX_GPIO,
77 PXA93X_GPIO,
78 MMP_GPIO = 0x10,
79 MMP2_GPIO,
80};
81
Eric Miao0807da52009-01-07 18:01:51 +080082static DEFINE_SPINLOCK(gpio_lock);
83static struct pxa_gpio_chip *pxa_gpio_chips;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +080084static int gpio_type;
Haojian Zhuang157d2642011-10-17 20:37:52 +080085static void __iomem *gpio_reg_base;
Eric Miao0807da52009-01-07 18:01:51 +080086
87#define for_each_gpio_chip(i, c) \
88 for (i = 0, c = &pxa_gpio_chips[0]; i <= pxa_last_gpio; i += 32, c++)
89
90static inline void __iomem *gpio_chip_base(struct gpio_chip *c)
91{
92 return container_of(c, struct pxa_gpio_chip, chip)->regbase;
93}
94
Linus Walleija0656852011-06-13 10:42:19 +020095static inline struct pxa_gpio_chip *gpio_to_pxachip(unsigned gpio)
Eric Miao0807da52009-01-07 18:01:51 +080096{
97 return &pxa_gpio_chips[gpio_to_bank(gpio)];
98}
99
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800100static inline int gpio_is_pxa_type(int type)
101{
102 return (type & MMP_GPIO) == 0;
103}
104
105static inline int gpio_is_mmp_type(int type)
106{
107 return (type & MMP_GPIO) != 0;
108}
109
Haojian Zhuang157d2642011-10-17 20:37:52 +0800110/* GPIO86/87/88/89 on PXA26x have their direction bits in PXA_GPDR(2 inverted,
111 * as well as their Alternate Function value being '1' for GPIO in GAFRx.
112 */
113static inline int __gpio_is_inverted(int gpio)
114{
115 if ((gpio_type == PXA26X_GPIO) && (gpio > 85))
116 return 1;
117 return 0;
118}
119
120/*
121 * On PXA25x and PXA27x, GAFRx and GPDRx together decide the alternate
122 * function of a GPIO, and GPDRx cannot be altered once configured. It
123 * is attributed as "occupied" here (I know this terminology isn't
124 * accurate, you are welcome to propose a better one :-)
125 */
126static inline int __gpio_is_occupied(unsigned gpio)
127{
128 struct pxa_gpio_chip *pxachip;
129 void __iomem *base;
130 unsigned long gafr = 0, gpdr = 0;
131 int ret, af = 0, dir = 0;
132
133 pxachip = gpio_to_pxachip(gpio);
134 base = gpio_chip_base(&pxachip->chip);
135 gpdr = readl_relaxed(base + GPDR_OFFSET);
136
137 switch (gpio_type) {
138 case PXA25X_GPIO:
139 case PXA26X_GPIO:
140 case PXA27X_GPIO:
141 gafr = readl_relaxed(base + GAFR_OFFSET);
142 af = (gafr >> ((gpio & 0xf) * 2)) & 0x3;
143 dir = gpdr & GPIO_bit(gpio);
144
145 if (__gpio_is_inverted(gpio))
146 ret = (af != 1) || (dir == 0);
147 else
148 ret = (af != 0) || (dir != 0);
149 break;
150 default:
151 ret = gpdr & GPIO_bit(gpio);
152 break;
153 }
154 return ret;
155}
156
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800157#ifdef CONFIG_ARCH_PXA
158static inline int __pxa_gpio_to_irq(int gpio)
159{
160 if (gpio_is_pxa_type(gpio_type))
161 return PXA_GPIO_TO_IRQ(gpio);
162 return -1;
163}
164
165static inline int __pxa_irq_to_gpio(int irq)
166{
167 if (gpio_is_pxa_type(gpio_type))
168 return irq - PXA_GPIO_TO_IRQ(0);
169 return -1;
170}
171#else
172static inline int __pxa_gpio_to_irq(int gpio) { return -1; }
173static inline int __pxa_irq_to_gpio(int irq) { return -1; }
174#endif
175
176#ifdef CONFIG_ARCH_MMP
177static inline int __mmp_gpio_to_irq(int gpio)
178{
179 if (gpio_is_mmp_type(gpio_type))
180 return MMP_GPIO_TO_IRQ(gpio);
181 return -1;
182}
183
184static inline int __mmp_irq_to_gpio(int irq)
185{
186 if (gpio_is_mmp_type(gpio_type))
187 return irq - MMP_GPIO_TO_IRQ(0);
188 return -1;
189}
190#else
191static inline int __mmp_gpio_to_irq(int gpio) { return -1; }
192static inline int __mmp_irq_to_gpio(int irq) { return -1; }
193#endif
194
195static int pxa_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
196{
197 int gpio, ret;
198
199 gpio = chip->base + offset;
200 ret = __pxa_gpio_to_irq(gpio);
201 if (ret >= 0)
202 return ret;
203 return __mmp_gpio_to_irq(gpio);
204}
205
206int pxa_irq_to_gpio(int irq)
207{
208 int ret;
209
210 ret = __pxa_irq_to_gpio(irq);
211 if (ret >= 0)
212 return ret;
213 return __mmp_irq_to_gpio(irq);
214}
215
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800216static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
217{
Eric Miao0807da52009-01-07 18:01:51 +0800218 void __iomem *base = gpio_chip_base(chip);
219 uint32_t value, mask = 1 << offset;
220 unsigned long flags;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800221
Eric Miao0807da52009-01-07 18:01:51 +0800222 spin_lock_irqsave(&gpio_lock, flags);
223
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800224 value = readl_relaxed(base + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800225 if (__gpio_is_inverted(chip->base + offset))
226 value |= mask;
227 else
228 value &= ~mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800229 writel_relaxed(value, base + GPDR_OFFSET);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800230
Eric Miao0807da52009-01-07 18:01:51 +0800231 spin_unlock_irqrestore(&gpio_lock, flags);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800232 return 0;
233}
234
235static int pxa_gpio_direction_output(struct gpio_chip *chip,
Eric Miao0807da52009-01-07 18:01:51 +0800236 unsigned offset, int value)
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800237{
Eric Miao0807da52009-01-07 18:01:51 +0800238 void __iomem *base = gpio_chip_base(chip);
239 uint32_t tmp, mask = 1 << offset;
240 unsigned long flags;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800241
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800242 writel_relaxed(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET));
Eric Miao0807da52009-01-07 18:01:51 +0800243
244 spin_lock_irqsave(&gpio_lock, flags);
245
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800246 tmp = readl_relaxed(base + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800247 if (__gpio_is_inverted(chip->base + offset))
248 tmp &= ~mask;
249 else
250 tmp |= mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800251 writel_relaxed(tmp, base + GPDR_OFFSET);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800252
Eric Miao0807da52009-01-07 18:01:51 +0800253 spin_unlock_irqrestore(&gpio_lock, flags);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800254 return 0;
255}
256
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800257static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
258{
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800259 return readl_relaxed(gpio_chip_base(chip) + GPLR_OFFSET) & (1 << offset);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800260}
261
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800262static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
263{
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800264 writel_relaxed(1 << offset, gpio_chip_base(chip) +
Eric Miao0807da52009-01-07 18:01:51 +0800265 (value ? GPSR_OFFSET : GPCR_OFFSET));
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800266}
267
Haojian Zhuang157d2642011-10-17 20:37:52 +0800268static int __devinit pxa_init_gpio_chip(int gpio_end)
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800269{
Eric Miao0807da52009-01-07 18:01:51 +0800270 int i, gpio, nbanks = gpio_to_bank(gpio_end) + 1;
271 struct pxa_gpio_chip *chips;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800272
Daniel Mack4aa78262009-06-19 22:56:09 +0200273 chips = kzalloc(nbanks * sizeof(struct pxa_gpio_chip), GFP_KERNEL);
Eric Miao0807da52009-01-07 18:01:51 +0800274 if (chips == NULL) {
275 pr_err("%s: failed to allocate GPIO chips\n", __func__);
276 return -ENOMEM;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800277 }
Eric Miao0807da52009-01-07 18:01:51 +0800278
279 for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
280 struct gpio_chip *c = &chips[i].chip;
281
282 sprintf(chips[i].label, "gpio-%d", i);
Haojian Zhuang157d2642011-10-17 20:37:52 +0800283 chips[i].regbase = gpio_reg_base + BANK_OFF(i);
Eric Miao0807da52009-01-07 18:01:51 +0800284
285 c->base = gpio;
286 c->label = chips[i].label;
287
288 c->direction_input = pxa_gpio_direction_input;
289 c->direction_output = pxa_gpio_direction_output;
290 c->get = pxa_gpio_get;
291 c->set = pxa_gpio_set;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800292 c->to_irq = pxa_gpio_to_irq;
Eric Miao0807da52009-01-07 18:01:51 +0800293
294 /* number of GPIOs on last bank may be less than 32 */
295 c->ngpio = (gpio + 31 > gpio_end) ? (gpio_end - gpio + 1) : 32;
296 gpiochip_add(c);
297 }
298 pxa_gpio_chips = chips;
299 return 0;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800300}
301
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800302/* Update only those GRERx and GFERx edge detection register bits if those
303 * bits are set in c->irq_mask
304 */
305static inline void update_edge_detect(struct pxa_gpio_chip *c)
306{
307 uint32_t grer, gfer;
308
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800309 grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~c->irq_mask;
310 gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~c->irq_mask;
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800311 grer |= c->irq_edge_rise & c->irq_mask;
312 gfer |= c->irq_edge_fall & c->irq_mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800313 writel_relaxed(grer, c->regbase + GRER_OFFSET);
314 writel_relaxed(gfer, c->regbase + GFER_OFFSET);
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800315}
316
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100317static int pxa_gpio_irq_type(struct irq_data *d, unsigned int type)
eric miaoe3630db2008-03-04 11:42:26 +0800318{
Eric Miao0807da52009-01-07 18:01:51 +0800319 struct pxa_gpio_chip *c;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800320 int gpio = pxa_irq_to_gpio(d->irq);
Eric Miao0807da52009-01-07 18:01:51 +0800321 unsigned long gpdr, mask = GPIO_bit(gpio);
eric miaoe3630db2008-03-04 11:42:26 +0800322
Linus Walleija0656852011-06-13 10:42:19 +0200323 c = gpio_to_pxachip(gpio);
eric miaoe3630db2008-03-04 11:42:26 +0800324
325 if (type == IRQ_TYPE_PROBE) {
326 /* Don't mess with enabled GPIOs using preconfigured edges or
327 * GPIOs set to alternate function or to output during probe
328 */
Eric Miao0807da52009-01-07 18:01:51 +0800329 if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
eric miaoe3630db2008-03-04 11:42:26 +0800330 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800331
332 if (__gpio_is_occupied(gpio))
eric miaoe3630db2008-03-04 11:42:26 +0800333 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800334
eric miaoe3630db2008-03-04 11:42:26 +0800335 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
336 }
337
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800338 gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
Eric Miao0807da52009-01-07 18:01:51 +0800339
Eric Miao067455a2008-11-26 18:12:04 +0800340 if (__gpio_is_inverted(gpio))
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800341 writel_relaxed(gpdr | mask, c->regbase + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800342 else
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800343 writel_relaxed(gpdr & ~mask, c->regbase + GPDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800344
345 if (type & IRQ_TYPE_EDGE_RISING)
Eric Miao0807da52009-01-07 18:01:51 +0800346 c->irq_edge_rise |= mask;
eric miaoe3630db2008-03-04 11:42:26 +0800347 else
Eric Miao0807da52009-01-07 18:01:51 +0800348 c->irq_edge_rise &= ~mask;
eric miaoe3630db2008-03-04 11:42:26 +0800349
350 if (type & IRQ_TYPE_EDGE_FALLING)
Eric Miao0807da52009-01-07 18:01:51 +0800351 c->irq_edge_fall |= mask;
eric miaoe3630db2008-03-04 11:42:26 +0800352 else
Eric Miao0807da52009-01-07 18:01:51 +0800353 c->irq_edge_fall &= ~mask;
eric miaoe3630db2008-03-04 11:42:26 +0800354
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800355 update_edge_detect(c);
eric miaoe3630db2008-03-04 11:42:26 +0800356
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100357 pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->irq, gpio,
eric miaoe3630db2008-03-04 11:42:26 +0800358 ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
359 ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
360 return 0;
361}
362
eric miaoe3630db2008-03-04 11:42:26 +0800363static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
364{
Eric Miao0807da52009-01-07 18:01:51 +0800365 struct pxa_gpio_chip *c;
366 int loop, gpio, gpio_base, n;
367 unsigned long gedr;
eric miaoe3630db2008-03-04 11:42:26 +0800368
369 do {
eric miaoe3630db2008-03-04 11:42:26 +0800370 loop = 0;
Eric Miao0807da52009-01-07 18:01:51 +0800371 for_each_gpio_chip(gpio, c) {
372 gpio_base = c->chip.base;
eric miaoe3630db2008-03-04 11:42:26 +0800373
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800374 gedr = readl_relaxed(c->regbase + GEDR_OFFSET);
Eric Miao0807da52009-01-07 18:01:51 +0800375 gedr = gedr & c->irq_mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800376 writel_relaxed(gedr, c->regbase + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800377
Eric Miao0807da52009-01-07 18:01:51 +0800378 n = find_first_bit(&gedr, BITS_PER_LONG);
379 while (n < BITS_PER_LONG) {
380 loop = 1;
381
382 generic_handle_irq(gpio_to_irq(gpio_base + n));
383 n = find_next_bit(&gedr, BITS_PER_LONG, n + 1);
384 }
eric miaoe3630db2008-03-04 11:42:26 +0800385 }
386 } while (loop);
387}
388
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100389static void pxa_ack_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800390{
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800391 int gpio = pxa_irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200392 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800393
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800394 writel_relaxed(GPIO_bit(gpio), c->regbase + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800395}
396
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100397static void pxa_mask_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800398{
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800399 int gpio = pxa_irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200400 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800401 uint32_t grer, gfer;
402
403 c->irq_mask &= ~GPIO_bit(gpio);
404
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800405 grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~GPIO_bit(gpio);
406 gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~GPIO_bit(gpio);
407 writel_relaxed(grer, c->regbase + GRER_OFFSET);
408 writel_relaxed(gfer, c->regbase + GFER_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800409}
410
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100411static void pxa_unmask_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800412{
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800413 int gpio = pxa_irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200414 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800415
416 c->irq_mask |= GPIO_bit(gpio);
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800417 update_edge_detect(c);
eric miaoe3630db2008-03-04 11:42:26 +0800418}
419
420static struct irq_chip pxa_muxed_gpio_chip = {
421 .name = "GPIO",
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100422 .irq_ack = pxa_ack_muxed_gpio,
423 .irq_mask = pxa_mask_muxed_gpio,
424 .irq_unmask = pxa_unmask_muxed_gpio,
425 .irq_set_type = pxa_gpio_irq_type,
eric miaoe3630db2008-03-04 11:42:26 +0800426};
427
Haojian Zhuang478e2232011-10-14 16:44:07 +0800428static int pxa_gpio_nums(void)
429{
430 int count = 0;
431
432#ifdef CONFIG_ARCH_PXA
433 if (cpu_is_pxa25x()) {
434#ifdef CONFIG_CPU_PXA26x
435 count = 89;
436 gpio_type = PXA26X_GPIO;
437#elif defined(CONFIG_PXA25x)
438 count = 84;
439 gpio_type = PXA26X_GPIO;
440#endif /* CONFIG_CPU_PXA26x */
441 } else if (cpu_is_pxa27x()) {
442 count = 120;
443 gpio_type = PXA27X_GPIO;
444 } else if (cpu_is_pxa93x() || cpu_is_pxa95x()) {
445 count = 191;
446 gpio_type = PXA93X_GPIO;
447 } else if (cpu_is_pxa3xx()) {
448 count = 127;
449 gpio_type = PXA3XX_GPIO;
450 }
451#endif /* CONFIG_ARCH_PXA */
452
453#ifdef CONFIG_ARCH_MMP
454 if (cpu_is_pxa168() || cpu_is_pxa910()) {
455 count = 127;
456 gpio_type = MMP_GPIO;
457 } else if (cpu_is_mmp2()) {
458 count = 191;
459 gpio_type = MMP2_GPIO;
460 }
461#endif /* CONFIG_ARCH_MMP */
462 return count;
463}
464
Haojian Zhuang157d2642011-10-17 20:37:52 +0800465static int __devinit pxa_gpio_probe(struct platform_device *pdev)
eric miaoe3630db2008-03-04 11:42:26 +0800466{
Eric Miao0807da52009-01-07 18:01:51 +0800467 struct pxa_gpio_chip *c;
Haojian Zhuang157d2642011-10-17 20:37:52 +0800468 struct resource *res;
Eric Miao0807da52009-01-07 18:01:51 +0800469 int gpio, irq;
Haojian Zhuang157d2642011-10-17 20:37:52 +0800470 int irq0 = 0, irq1 = 0, irq_mux, gpio_offset = 0;
eric miaoe3630db2008-03-04 11:42:26 +0800471
Haojian Zhuang478e2232011-10-14 16:44:07 +0800472 pxa_last_gpio = pxa_gpio_nums();
473 if (!pxa_last_gpio)
Haojian Zhuang157d2642011-10-17 20:37:52 +0800474 return -EINVAL;
475
476 irq0 = platform_get_irq_byname(pdev, "gpio0");
477 irq1 = platform_get_irq_byname(pdev, "gpio1");
478 irq_mux = platform_get_irq_byname(pdev, "gpio_mux");
479 if ((irq0 > 0 && irq1 <= 0) || (irq0 <= 0 && irq1 > 0)
480 || (irq_mux <= 0))
481 return -EINVAL;
482 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
483 if (!res)
484 return -EINVAL;
485 gpio_reg_base = ioremap(res->start, resource_size(res));
486 if (!gpio_reg_base)
487 return -EINVAL;
488
489 if (irq0 > 0)
490 gpio_offset = 2;
eric miaoe3630db2008-03-04 11:42:26 +0800491
Eric Miao0807da52009-01-07 18:01:51 +0800492 /* Initialize GPIO chips */
Haojian Zhuang157d2642011-10-17 20:37:52 +0800493 pxa_init_gpio_chip(pxa_last_gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800494
eric miaoe3630db2008-03-04 11:42:26 +0800495 /* clear all GPIO edge detects */
Eric Miao0807da52009-01-07 18:01:51 +0800496 for_each_gpio_chip(gpio, c) {
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800497 writel_relaxed(0, c->regbase + GFER_OFFSET);
498 writel_relaxed(0, c->regbase + GRER_OFFSET);
499 writel_relaxed(~0,c->regbase + GEDR_OFFSET);
Haojian Zhuangbe241682011-10-17 21:07:15 +0800500 /* unmask GPIO edge detect for AP side */
501 if (gpio_is_mmp_type(gpio_type))
502 writel_relaxed(~0, c->regbase + ED_MASK_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800503 }
504
Haojian Zhuang87c49e22011-10-10 14:38:46 +0800505#ifdef CONFIG_ARCH_PXA
506 irq = gpio_to_irq(0);
507 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
508 handle_edge_irq);
509 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
510 irq_set_chained_handler(IRQ_GPIO0, pxa_gpio_demux_handler);
511
512 irq = gpio_to_irq(1);
513 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
514 handle_edge_irq);
515 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
516 irq_set_chained_handler(IRQ_GPIO1, pxa_gpio_demux_handler);
517#endif
518
Haojian Zhuang157d2642011-10-17 20:37:52 +0800519 for (irq = gpio_to_irq(gpio_offset);
520 irq <= gpio_to_irq(pxa_last_gpio); irq++) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100521 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
522 handle_edge_irq);
eric miaoe3630db2008-03-04 11:42:26 +0800523 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
524 }
525
Haojian Zhuang157d2642011-10-17 20:37:52 +0800526 irq_set_chained_handler(irq_mux, pxa_gpio_demux_handler);
527 return 0;
eric miaoe3630db2008-03-04 11:42:26 +0800528}
eric miao663707c2008-03-04 16:13:58 +0800529
Haojian Zhuang157d2642011-10-17 20:37:52 +0800530static struct platform_driver pxa_gpio_driver = {
531 .probe = pxa_gpio_probe,
532 .driver = {
533 .name = "pxa-gpio",
534 },
535};
536
537static int __init pxa_gpio_init(void)
538{
539 return platform_driver_register(&pxa_gpio_driver);
540}
541postcore_initcall(pxa_gpio_init);
542
eric miao663707c2008-03-04 16:13:58 +0800543#ifdef CONFIG_PM
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200544static int pxa_gpio_suspend(void)
eric miao663707c2008-03-04 16:13:58 +0800545{
Eric Miao0807da52009-01-07 18:01:51 +0800546 struct pxa_gpio_chip *c;
547 int gpio;
eric miao663707c2008-03-04 16:13:58 +0800548
Eric Miao0807da52009-01-07 18:01:51 +0800549 for_each_gpio_chip(gpio, c) {
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800550 c->saved_gplr = readl_relaxed(c->regbase + GPLR_OFFSET);
551 c->saved_gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
552 c->saved_grer = readl_relaxed(c->regbase + GRER_OFFSET);
553 c->saved_gfer = readl_relaxed(c->regbase + GFER_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800554
555 /* Clear GPIO transition detect bits */
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800556 writel_relaxed(0xffffffff, c->regbase + GEDR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800557 }
558 return 0;
559}
560
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200561static void pxa_gpio_resume(void)
eric miao663707c2008-03-04 16:13:58 +0800562{
Eric Miao0807da52009-01-07 18:01:51 +0800563 struct pxa_gpio_chip *c;
564 int gpio;
eric miao663707c2008-03-04 16:13:58 +0800565
Eric Miao0807da52009-01-07 18:01:51 +0800566 for_each_gpio_chip(gpio, c) {
eric miao663707c2008-03-04 16:13:58 +0800567 /* restore level with set/clear */
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800568 writel_relaxed( c->saved_gplr, c->regbase + GPSR_OFFSET);
569 writel_relaxed(~c->saved_gplr, c->regbase + GPCR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800570
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800571 writel_relaxed(c->saved_grer, c->regbase + GRER_OFFSET);
572 writel_relaxed(c->saved_gfer, c->regbase + GFER_OFFSET);
573 writel_relaxed(c->saved_gpdr, c->regbase + GPDR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800574 }
eric miao663707c2008-03-04 16:13:58 +0800575}
576#else
577#define pxa_gpio_suspend NULL
578#define pxa_gpio_resume NULL
579#endif
580
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200581struct syscore_ops pxa_gpio_syscore_ops = {
eric miao663707c2008-03-04 16:13:58 +0800582 .suspend = pxa_gpio_suspend,
583 .resume = pxa_gpio_resume,
584};
Haojian Zhuang157d2642011-10-17 20:37:52 +0800585
586static int __init pxa_gpio_sysinit(void)
587{
588 register_syscore_ops(&pxa_gpio_syscore_ops);
589 return 0;
590}
591postcore_initcall(pxa_gpio_sysinit);