blob: 079f97fde2c73f68d007c55838628f4bf7e001d0 [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
49
50#define BANK_OFF(n) (((n) < 3) ? (n) << 2 : 0x100 + (((n) - 3) << 2))
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080051
Eric Miao3b8e2852009-01-07 11:30:49 +080052int pxa_last_gpio;
53
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080054struct pxa_gpio_chip {
55 struct gpio_chip chip;
Eric Miao0807da52009-01-07 18:01:51 +080056 void __iomem *regbase;
57 char label[10];
58
59 unsigned long irq_mask;
60 unsigned long irq_edge_rise;
61 unsigned long irq_edge_fall;
62
63#ifdef CONFIG_PM
64 unsigned long saved_gplr;
65 unsigned long saved_gpdr;
66 unsigned long saved_grer;
67 unsigned long saved_gfer;
68#endif
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080069};
70
Haojian Zhuang4929f5a2011-10-10 16:03:51 +080071enum {
72 PXA25X_GPIO = 0,
73 PXA26X_GPIO,
74 PXA27X_GPIO,
75 PXA3XX_GPIO,
76 PXA93X_GPIO,
77 MMP_GPIO = 0x10,
78 MMP2_GPIO,
79};
80
Eric Miao0807da52009-01-07 18:01:51 +080081static DEFINE_SPINLOCK(gpio_lock);
82static struct pxa_gpio_chip *pxa_gpio_chips;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +080083static int gpio_type;
Haojian Zhuang157d2642011-10-17 20:37:52 +080084static void __iomem *gpio_reg_base;
Eric Miao0807da52009-01-07 18:01:51 +080085
86#define for_each_gpio_chip(i, c) \
87 for (i = 0, c = &pxa_gpio_chips[0]; i <= pxa_last_gpio; i += 32, c++)
88
89static inline void __iomem *gpio_chip_base(struct gpio_chip *c)
90{
91 return container_of(c, struct pxa_gpio_chip, chip)->regbase;
92}
93
Linus Walleija0656852011-06-13 10:42:19 +020094static inline struct pxa_gpio_chip *gpio_to_pxachip(unsigned gpio)
Eric Miao0807da52009-01-07 18:01:51 +080095{
96 return &pxa_gpio_chips[gpio_to_bank(gpio)];
97}
98
Haojian Zhuang4929f5a2011-10-10 16:03:51 +080099static inline int gpio_is_pxa_type(int type)
100{
101 return (type & MMP_GPIO) == 0;
102}
103
104static inline int gpio_is_mmp_type(int type)
105{
106 return (type & MMP_GPIO) != 0;
107}
108
Haojian Zhuang157d2642011-10-17 20:37:52 +0800109/* GPIO86/87/88/89 on PXA26x have their direction bits in PXA_GPDR(2 inverted,
110 * as well as their Alternate Function value being '1' for GPIO in GAFRx.
111 */
112static inline int __gpio_is_inverted(int gpio)
113{
114 if ((gpio_type == PXA26X_GPIO) && (gpio > 85))
115 return 1;
116 return 0;
117}
118
119/*
120 * On PXA25x and PXA27x, GAFRx and GPDRx together decide the alternate
121 * function of a GPIO, and GPDRx cannot be altered once configured. It
122 * is attributed as "occupied" here (I know this terminology isn't
123 * accurate, you are welcome to propose a better one :-)
124 */
125static inline int __gpio_is_occupied(unsigned gpio)
126{
127 struct pxa_gpio_chip *pxachip;
128 void __iomem *base;
129 unsigned long gafr = 0, gpdr = 0;
130 int ret, af = 0, dir = 0;
131
132 pxachip = gpio_to_pxachip(gpio);
133 base = gpio_chip_base(&pxachip->chip);
134 gpdr = readl_relaxed(base + GPDR_OFFSET);
135
136 switch (gpio_type) {
137 case PXA25X_GPIO:
138 case PXA26X_GPIO:
139 case PXA27X_GPIO:
140 gafr = readl_relaxed(base + GAFR_OFFSET);
141 af = (gafr >> ((gpio & 0xf) * 2)) & 0x3;
142 dir = gpdr & GPIO_bit(gpio);
143
144 if (__gpio_is_inverted(gpio))
145 ret = (af != 1) || (dir == 0);
146 else
147 ret = (af != 0) || (dir != 0);
148 break;
149 default:
150 ret = gpdr & GPIO_bit(gpio);
151 break;
152 }
153 return ret;
154}
155
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800156#ifdef CONFIG_ARCH_PXA
157static inline int __pxa_gpio_to_irq(int gpio)
158{
159 if (gpio_is_pxa_type(gpio_type))
160 return PXA_GPIO_TO_IRQ(gpio);
161 return -1;
162}
163
164static inline int __pxa_irq_to_gpio(int irq)
165{
166 if (gpio_is_pxa_type(gpio_type))
167 return irq - PXA_GPIO_TO_IRQ(0);
168 return -1;
169}
170#else
171static inline int __pxa_gpio_to_irq(int gpio) { return -1; }
172static inline int __pxa_irq_to_gpio(int irq) { return -1; }
173#endif
174
175#ifdef CONFIG_ARCH_MMP
176static inline int __mmp_gpio_to_irq(int gpio)
177{
178 if (gpio_is_mmp_type(gpio_type))
179 return MMP_GPIO_TO_IRQ(gpio);
180 return -1;
181}
182
183static inline int __mmp_irq_to_gpio(int irq)
184{
185 if (gpio_is_mmp_type(gpio_type))
186 return irq - MMP_GPIO_TO_IRQ(0);
187 return -1;
188}
189#else
190static inline int __mmp_gpio_to_irq(int gpio) { return -1; }
191static inline int __mmp_irq_to_gpio(int irq) { return -1; }
192#endif
193
194static int pxa_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
195{
196 int gpio, ret;
197
198 gpio = chip->base + offset;
199 ret = __pxa_gpio_to_irq(gpio);
200 if (ret >= 0)
201 return ret;
202 return __mmp_gpio_to_irq(gpio);
203}
204
205int pxa_irq_to_gpio(int irq)
206{
207 int ret;
208
209 ret = __pxa_irq_to_gpio(irq);
210 if (ret >= 0)
211 return ret;
212 return __mmp_irq_to_gpio(irq);
213}
214
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800215static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
216{
Eric Miao0807da52009-01-07 18:01:51 +0800217 void __iomem *base = gpio_chip_base(chip);
218 uint32_t value, mask = 1 << offset;
219 unsigned long flags;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800220
Eric Miao0807da52009-01-07 18:01:51 +0800221 spin_lock_irqsave(&gpio_lock, flags);
222
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800223 value = readl_relaxed(base + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800224 if (__gpio_is_inverted(chip->base + offset))
225 value |= mask;
226 else
227 value &= ~mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800228 writel_relaxed(value, base + GPDR_OFFSET);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800229
Eric Miao0807da52009-01-07 18:01:51 +0800230 spin_unlock_irqrestore(&gpio_lock, flags);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800231 return 0;
232}
233
234static int pxa_gpio_direction_output(struct gpio_chip *chip,
Eric Miao0807da52009-01-07 18:01:51 +0800235 unsigned offset, int value)
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800236{
Eric Miao0807da52009-01-07 18:01:51 +0800237 void __iomem *base = gpio_chip_base(chip);
238 uint32_t tmp, mask = 1 << offset;
239 unsigned long flags;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800240
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800241 writel_relaxed(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET));
Eric Miao0807da52009-01-07 18:01:51 +0800242
243 spin_lock_irqsave(&gpio_lock, flags);
244
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800245 tmp = readl_relaxed(base + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800246 if (__gpio_is_inverted(chip->base + offset))
247 tmp &= ~mask;
248 else
249 tmp |= mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800250 writel_relaxed(tmp, base + GPDR_OFFSET);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800251
Eric Miao0807da52009-01-07 18:01:51 +0800252 spin_unlock_irqrestore(&gpio_lock, flags);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800253 return 0;
254}
255
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800256static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
257{
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800258 return readl_relaxed(gpio_chip_base(chip) + GPLR_OFFSET) & (1 << offset);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800259}
260
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800261static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
262{
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800263 writel_relaxed(1 << offset, gpio_chip_base(chip) +
Eric Miao0807da52009-01-07 18:01:51 +0800264 (value ? GPSR_OFFSET : GPCR_OFFSET));
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800265}
266
Haojian Zhuang157d2642011-10-17 20:37:52 +0800267static int __devinit pxa_init_gpio_chip(int gpio_end)
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800268{
Eric Miao0807da52009-01-07 18:01:51 +0800269 int i, gpio, nbanks = gpio_to_bank(gpio_end) + 1;
270 struct pxa_gpio_chip *chips;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800271
Daniel Mack4aa78262009-06-19 22:56:09 +0200272 chips = kzalloc(nbanks * sizeof(struct pxa_gpio_chip), GFP_KERNEL);
Eric Miao0807da52009-01-07 18:01:51 +0800273 if (chips == NULL) {
274 pr_err("%s: failed to allocate GPIO chips\n", __func__);
275 return -ENOMEM;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800276 }
Eric Miao0807da52009-01-07 18:01:51 +0800277
278 for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
279 struct gpio_chip *c = &chips[i].chip;
280
281 sprintf(chips[i].label, "gpio-%d", i);
Haojian Zhuang157d2642011-10-17 20:37:52 +0800282 chips[i].regbase = gpio_reg_base + BANK_OFF(i);
Eric Miao0807da52009-01-07 18:01:51 +0800283
284 c->base = gpio;
285 c->label = chips[i].label;
286
287 c->direction_input = pxa_gpio_direction_input;
288 c->direction_output = pxa_gpio_direction_output;
289 c->get = pxa_gpio_get;
290 c->set = pxa_gpio_set;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800291 c->to_irq = pxa_gpio_to_irq;
Eric Miao0807da52009-01-07 18:01:51 +0800292
293 /* number of GPIOs on last bank may be less than 32 */
294 c->ngpio = (gpio + 31 > gpio_end) ? (gpio_end - gpio + 1) : 32;
295 gpiochip_add(c);
296 }
297 pxa_gpio_chips = chips;
298 return 0;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800299}
300
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800301/* Update only those GRERx and GFERx edge detection register bits if those
302 * bits are set in c->irq_mask
303 */
304static inline void update_edge_detect(struct pxa_gpio_chip *c)
305{
306 uint32_t grer, gfer;
307
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800308 grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~c->irq_mask;
309 gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~c->irq_mask;
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800310 grer |= c->irq_edge_rise & c->irq_mask;
311 gfer |= c->irq_edge_fall & c->irq_mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800312 writel_relaxed(grer, c->regbase + GRER_OFFSET);
313 writel_relaxed(gfer, c->regbase + GFER_OFFSET);
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800314}
315
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100316static int pxa_gpio_irq_type(struct irq_data *d, unsigned int type)
eric miaoe3630db2008-03-04 11:42:26 +0800317{
Eric Miao0807da52009-01-07 18:01:51 +0800318 struct pxa_gpio_chip *c;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800319 int gpio = pxa_irq_to_gpio(d->irq);
Eric Miao0807da52009-01-07 18:01:51 +0800320 unsigned long gpdr, mask = GPIO_bit(gpio);
eric miaoe3630db2008-03-04 11:42:26 +0800321
Linus Walleija0656852011-06-13 10:42:19 +0200322 c = gpio_to_pxachip(gpio);
eric miaoe3630db2008-03-04 11:42:26 +0800323
324 if (type == IRQ_TYPE_PROBE) {
325 /* Don't mess with enabled GPIOs using preconfigured edges or
326 * GPIOs set to alternate function or to output during probe
327 */
Eric Miao0807da52009-01-07 18:01:51 +0800328 if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
eric miaoe3630db2008-03-04 11:42:26 +0800329 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800330
331 if (__gpio_is_occupied(gpio))
eric miaoe3630db2008-03-04 11:42:26 +0800332 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800333
eric miaoe3630db2008-03-04 11:42:26 +0800334 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
335 }
336
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800337 gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
Eric Miao0807da52009-01-07 18:01:51 +0800338
Eric Miao067455a2008-11-26 18:12:04 +0800339 if (__gpio_is_inverted(gpio))
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800340 writel_relaxed(gpdr | mask, c->regbase + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800341 else
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800342 writel_relaxed(gpdr & ~mask, c->regbase + GPDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800343
344 if (type & IRQ_TYPE_EDGE_RISING)
Eric Miao0807da52009-01-07 18:01:51 +0800345 c->irq_edge_rise |= mask;
eric miaoe3630db2008-03-04 11:42:26 +0800346 else
Eric Miao0807da52009-01-07 18:01:51 +0800347 c->irq_edge_rise &= ~mask;
eric miaoe3630db2008-03-04 11:42:26 +0800348
349 if (type & IRQ_TYPE_EDGE_FALLING)
Eric Miao0807da52009-01-07 18:01:51 +0800350 c->irq_edge_fall |= mask;
eric miaoe3630db2008-03-04 11:42:26 +0800351 else
Eric Miao0807da52009-01-07 18:01:51 +0800352 c->irq_edge_fall &= ~mask;
eric miaoe3630db2008-03-04 11:42:26 +0800353
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800354 update_edge_detect(c);
eric miaoe3630db2008-03-04 11:42:26 +0800355
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100356 pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->irq, gpio,
eric miaoe3630db2008-03-04 11:42:26 +0800357 ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
358 ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
359 return 0;
360}
361
eric miaoe3630db2008-03-04 11:42:26 +0800362static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
363{
Eric Miao0807da52009-01-07 18:01:51 +0800364 struct pxa_gpio_chip *c;
365 int loop, gpio, gpio_base, n;
366 unsigned long gedr;
eric miaoe3630db2008-03-04 11:42:26 +0800367
368 do {
eric miaoe3630db2008-03-04 11:42:26 +0800369 loop = 0;
Eric Miao0807da52009-01-07 18:01:51 +0800370 for_each_gpio_chip(gpio, c) {
371 gpio_base = c->chip.base;
eric miaoe3630db2008-03-04 11:42:26 +0800372
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800373 gedr = readl_relaxed(c->regbase + GEDR_OFFSET);
Eric Miao0807da52009-01-07 18:01:51 +0800374 gedr = gedr & c->irq_mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800375 writel_relaxed(gedr, c->regbase + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800376
Eric Miao0807da52009-01-07 18:01:51 +0800377 n = find_first_bit(&gedr, BITS_PER_LONG);
378 while (n < BITS_PER_LONG) {
379 loop = 1;
380
381 generic_handle_irq(gpio_to_irq(gpio_base + n));
382 n = find_next_bit(&gedr, BITS_PER_LONG, n + 1);
383 }
eric miaoe3630db2008-03-04 11:42:26 +0800384 }
385 } while (loop);
386}
387
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100388static void pxa_ack_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800389{
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800390 int gpio = pxa_irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200391 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800392
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800393 writel_relaxed(GPIO_bit(gpio), c->regbase + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800394}
395
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100396static void pxa_mask_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800397{
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800398 int gpio = pxa_irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200399 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800400 uint32_t grer, gfer;
401
402 c->irq_mask &= ~GPIO_bit(gpio);
403
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800404 grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~GPIO_bit(gpio);
405 gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~GPIO_bit(gpio);
406 writel_relaxed(grer, c->regbase + GRER_OFFSET);
407 writel_relaxed(gfer, c->regbase + GFER_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800408}
409
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100410static void pxa_unmask_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800411{
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800412 int gpio = pxa_irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200413 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800414
415 c->irq_mask |= GPIO_bit(gpio);
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800416 update_edge_detect(c);
eric miaoe3630db2008-03-04 11:42:26 +0800417}
418
419static struct irq_chip pxa_muxed_gpio_chip = {
420 .name = "GPIO",
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100421 .irq_ack = pxa_ack_muxed_gpio,
422 .irq_mask = pxa_mask_muxed_gpio,
423 .irq_unmask = pxa_unmask_muxed_gpio,
424 .irq_set_type = pxa_gpio_irq_type,
eric miaoe3630db2008-03-04 11:42:26 +0800425};
426
Haojian Zhuang478e2232011-10-14 16:44:07 +0800427static int pxa_gpio_nums(void)
428{
429 int count = 0;
430
431#ifdef CONFIG_ARCH_PXA
432 if (cpu_is_pxa25x()) {
433#ifdef CONFIG_CPU_PXA26x
434 count = 89;
435 gpio_type = PXA26X_GPIO;
436#elif defined(CONFIG_PXA25x)
437 count = 84;
438 gpio_type = PXA26X_GPIO;
439#endif /* CONFIG_CPU_PXA26x */
440 } else if (cpu_is_pxa27x()) {
441 count = 120;
442 gpio_type = PXA27X_GPIO;
443 } else if (cpu_is_pxa93x() || cpu_is_pxa95x()) {
444 count = 191;
445 gpio_type = PXA93X_GPIO;
446 } else if (cpu_is_pxa3xx()) {
447 count = 127;
448 gpio_type = PXA3XX_GPIO;
449 }
450#endif /* CONFIG_ARCH_PXA */
451
452#ifdef CONFIG_ARCH_MMP
453 if (cpu_is_pxa168() || cpu_is_pxa910()) {
454 count = 127;
455 gpio_type = MMP_GPIO;
456 } else if (cpu_is_mmp2()) {
457 count = 191;
458 gpio_type = MMP2_GPIO;
459 }
460#endif /* CONFIG_ARCH_MMP */
461 return count;
462}
463
Haojian Zhuang157d2642011-10-17 20:37:52 +0800464static int __devinit pxa_gpio_probe(struct platform_device *pdev)
eric miaoe3630db2008-03-04 11:42:26 +0800465{
Eric Miao0807da52009-01-07 18:01:51 +0800466 struct pxa_gpio_chip *c;
Haojian Zhuang157d2642011-10-17 20:37:52 +0800467 struct resource *res;
Eric Miao0807da52009-01-07 18:01:51 +0800468 int gpio, irq;
Haojian Zhuang157d2642011-10-17 20:37:52 +0800469 int irq0 = 0, irq1 = 0, irq_mux, gpio_offset = 0;
eric miaoe3630db2008-03-04 11:42:26 +0800470
Haojian Zhuang478e2232011-10-14 16:44:07 +0800471 pxa_last_gpio = pxa_gpio_nums();
472 if (!pxa_last_gpio)
Haojian Zhuang157d2642011-10-17 20:37:52 +0800473 return -EINVAL;
474
475 irq0 = platform_get_irq_byname(pdev, "gpio0");
476 irq1 = platform_get_irq_byname(pdev, "gpio1");
477 irq_mux = platform_get_irq_byname(pdev, "gpio_mux");
478 if ((irq0 > 0 && irq1 <= 0) || (irq0 <= 0 && irq1 > 0)
479 || (irq_mux <= 0))
480 return -EINVAL;
481 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
482 if (!res)
483 return -EINVAL;
484 gpio_reg_base = ioremap(res->start, resource_size(res));
485 if (!gpio_reg_base)
486 return -EINVAL;
487
488 if (irq0 > 0)
489 gpio_offset = 2;
eric miaoe3630db2008-03-04 11:42:26 +0800490
Eric Miao0807da52009-01-07 18:01:51 +0800491 /* Initialize GPIO chips */
Haojian Zhuang157d2642011-10-17 20:37:52 +0800492 pxa_init_gpio_chip(pxa_last_gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800493
eric miaoe3630db2008-03-04 11:42:26 +0800494 /* clear all GPIO edge detects */
Eric Miao0807da52009-01-07 18:01:51 +0800495 for_each_gpio_chip(gpio, c) {
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800496 writel_relaxed(0, c->regbase + GFER_OFFSET);
497 writel_relaxed(0, c->regbase + GRER_OFFSET);
498 writel_relaxed(~0,c->regbase + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800499 }
500
Haojian Zhuang87c49e22011-10-10 14:38:46 +0800501#ifdef CONFIG_ARCH_PXA
502 irq = gpio_to_irq(0);
503 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
504 handle_edge_irq);
505 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
506 irq_set_chained_handler(IRQ_GPIO0, pxa_gpio_demux_handler);
507
508 irq = gpio_to_irq(1);
509 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
510 handle_edge_irq);
511 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
512 irq_set_chained_handler(IRQ_GPIO1, pxa_gpio_demux_handler);
513#endif
514
Haojian Zhuang157d2642011-10-17 20:37:52 +0800515 for (irq = gpio_to_irq(gpio_offset);
516 irq <= gpio_to_irq(pxa_last_gpio); irq++) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100517 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
518 handle_edge_irq);
eric miaoe3630db2008-03-04 11:42:26 +0800519 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
520 }
521
Haojian Zhuang157d2642011-10-17 20:37:52 +0800522 irq_set_chained_handler(irq_mux, pxa_gpio_demux_handler);
523 return 0;
eric miaoe3630db2008-03-04 11:42:26 +0800524}
eric miao663707c2008-03-04 16:13:58 +0800525
Haojian Zhuang157d2642011-10-17 20:37:52 +0800526static struct platform_driver pxa_gpio_driver = {
527 .probe = pxa_gpio_probe,
528 .driver = {
529 .name = "pxa-gpio",
530 },
531};
532
533static int __init pxa_gpio_init(void)
534{
535 return platform_driver_register(&pxa_gpio_driver);
536}
537postcore_initcall(pxa_gpio_init);
538
eric miao663707c2008-03-04 16:13:58 +0800539#ifdef CONFIG_PM
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200540static int pxa_gpio_suspend(void)
eric miao663707c2008-03-04 16:13:58 +0800541{
Eric Miao0807da52009-01-07 18:01:51 +0800542 struct pxa_gpio_chip *c;
543 int gpio;
eric miao663707c2008-03-04 16:13:58 +0800544
Eric Miao0807da52009-01-07 18:01:51 +0800545 for_each_gpio_chip(gpio, c) {
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800546 c->saved_gplr = readl_relaxed(c->regbase + GPLR_OFFSET);
547 c->saved_gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
548 c->saved_grer = readl_relaxed(c->regbase + GRER_OFFSET);
549 c->saved_gfer = readl_relaxed(c->regbase + GFER_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800550
551 /* Clear GPIO transition detect bits */
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800552 writel_relaxed(0xffffffff, c->regbase + GEDR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800553 }
554 return 0;
555}
556
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200557static void pxa_gpio_resume(void)
eric miao663707c2008-03-04 16:13:58 +0800558{
Eric Miao0807da52009-01-07 18:01:51 +0800559 struct pxa_gpio_chip *c;
560 int gpio;
eric miao663707c2008-03-04 16:13:58 +0800561
Eric Miao0807da52009-01-07 18:01:51 +0800562 for_each_gpio_chip(gpio, c) {
eric miao663707c2008-03-04 16:13:58 +0800563 /* restore level with set/clear */
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800564 writel_relaxed( c->saved_gplr, c->regbase + GPSR_OFFSET);
565 writel_relaxed(~c->saved_gplr, c->regbase + GPCR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800566
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800567 writel_relaxed(c->saved_grer, c->regbase + GRER_OFFSET);
568 writel_relaxed(c->saved_gfer, c->regbase + GFER_OFFSET);
569 writel_relaxed(c->saved_gpdr, c->regbase + GPDR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800570 }
eric miao663707c2008-03-04 16:13:58 +0800571}
572#else
573#define pxa_gpio_suspend NULL
574#define pxa_gpio_resume NULL
575#endif
576
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200577struct syscore_ops pxa_gpio_syscore_ops = {
eric miao663707c2008-03-04 16:13:58 +0800578 .suspend = pxa_gpio_suspend,
579 .resume = pxa_gpio_resume,
580};
Haojian Zhuang157d2642011-10-17 20:37:52 +0800581
582static int __init pxa_gpio_sysinit(void)
583{
584 register_syscore_ops(&pxa_gpio_syscore_ops);
585 return 0;
586}
587postcore_initcall(pxa_gpio_sysinit);