blob: df2ce550f30929c1a20d1aca81e538a367f8f97b [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 */
Haojian Zhuang7a4d5072012-04-13 15:15:45 +080014#include <linux/module.h>
Haojian Zhuang389eda12011-10-17 21:26:55 +080015#include <linux/clk.h>
16#include <linux/err.h>
Russell King2f8163b2011-07-26 10:53:52 +010017#include <linux/gpio.h>
Haojian Zhuang157d2642011-10-17 20:37:52 +080018#include <linux/gpio-pxa.h>
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080019#include <linux/init.h>
Rob Herringae4f4cf2015-01-26 22:46:04 -060020#include <linux/interrupt.h>
eric miaoe3630db2008-03-04 11:42:26 +080021#include <linux/irq.h>
Haojian Zhuang7a4d5072012-04-13 15:15:45 +080022#include <linux/irqdomain.h>
Catalin Marinasde88cbb2013-01-18 15:31:37 +000023#include <linux/irqchip/chained_irq.h>
Russell Kingfced80c2008-09-06 12:10:45 +010024#include <linux/io.h>
Haojian Zhuang7a4d5072012-04-13 15:15:45 +080025#include <linux/of.h>
26#include <linux/of_device.h>
Haojian Zhuang157d2642011-10-17 20:37:52 +080027#include <linux/platform_device.h>
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +020028#include <linux/syscore_ops.h>
Daniel Mack4aa78262009-06-19 22:56:09 +020029#include <linux/slab.h>
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080030
Haojian Zhuang157d2642011-10-17 20:37:52 +080031/*
32 * We handle the GPIOs by banks, each bank covers up to 32 GPIOs with
33 * one set of registers. The register offsets are organized below:
34 *
35 * GPLR GPDR GPSR GPCR GRER GFER GEDR
36 * BANK 0 - 0x0000 0x000C 0x0018 0x0024 0x0030 0x003C 0x0048
37 * BANK 1 - 0x0004 0x0010 0x001C 0x0028 0x0034 0x0040 0x004C
38 * BANK 2 - 0x0008 0x0014 0x0020 0x002C 0x0038 0x0044 0x0050
39 *
40 * BANK 3 - 0x0100 0x010C 0x0118 0x0124 0x0130 0x013C 0x0148
41 * BANK 4 - 0x0104 0x0110 0x011C 0x0128 0x0134 0x0140 0x014C
42 * BANK 5 - 0x0108 0x0114 0x0120 0x012C 0x0138 0x0144 0x0150
43 *
Rob Herring684bba22015-01-26 22:46:06 -060044 * BANK 6 - 0x0200 0x020C 0x0218 0x0224 0x0230 0x023C 0x0248
45 *
Haojian Zhuang157d2642011-10-17 20:37:52 +080046 * NOTE:
47 * BANK 3 is only available on PXA27x and later processors.
Rob Herring684bba22015-01-26 22:46:06 -060048 * BANK 4 and 5 are only available on PXA935, PXA1928
49 * BANK 6 is only available on PXA1928
Haojian Zhuang157d2642011-10-17 20:37:52 +080050 */
51
52#define GPLR_OFFSET 0x00
53#define GPDR_OFFSET 0x0C
54#define GPSR_OFFSET 0x18
55#define GPCR_OFFSET 0x24
56#define GRER_OFFSET 0x30
57#define GFER_OFFSET 0x3C
58#define GEDR_OFFSET 0x48
59#define GAFR_OFFSET 0x54
Haojian Zhuangbe241682011-10-17 21:07:15 +080060#define ED_MASK_OFFSET 0x9C /* GPIO edge detection for AP side */
Haojian Zhuang157d2642011-10-17 20:37:52 +080061
Rob Herring1e970b72015-03-02 15:30:58 -060062#define BANK_OFF(n) (((n) / 3) << 8) + (((n) % 3) << 2)
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080063
Eric Miao3b8e2852009-01-07 11:30:49 +080064int pxa_last_gpio;
Daniel Mack9450be72012-07-22 16:55:44 +020065static int irq_base;
Eric Miao3b8e2852009-01-07 11:30:49 +080066
Haojian Zhuang7a4d5072012-04-13 15:15:45 +080067#ifdef CONFIG_OF
68static struct irq_domain *domain;
Daniel Mack72121572012-07-25 17:35:39 +020069static struct device_node *pxa_gpio_of_node;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +080070#endif
71
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080072struct pxa_gpio_chip {
73 struct gpio_chip chip;
Eric Miao0807da52009-01-07 18:01:51 +080074 void __iomem *regbase;
75 char label[10];
76
77 unsigned long irq_mask;
78 unsigned long irq_edge_rise;
79 unsigned long irq_edge_fall;
Robert Jarzmikb95ace52012-04-22 13:37:24 +020080 int (*set_wake)(unsigned int gpio, unsigned int on);
Eric Miao0807da52009-01-07 18:01:51 +080081
82#ifdef CONFIG_PM
83 unsigned long saved_gplr;
84 unsigned long saved_gpdr;
85 unsigned long saved_grer;
86 unsigned long saved_gfer;
87#endif
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080088};
89
Haojian Zhuang2cab0292013-04-07 16:44:33 +080090enum pxa_gpio_type {
Haojian Zhuang4929f5a2011-10-10 16:03:51 +080091 PXA25X_GPIO = 0,
92 PXA26X_GPIO,
93 PXA27X_GPIO,
94 PXA3XX_GPIO,
95 PXA93X_GPIO,
96 MMP_GPIO = 0x10,
Haojian Zhuang2cab0292013-04-07 16:44:33 +080097 MMP2_GPIO,
Rob Herring684bba22015-01-26 22:46:06 -060098 PXA1928_GPIO,
Haojian Zhuang2cab0292013-04-07 16:44:33 +080099};
100
101struct pxa_gpio_id {
102 enum pxa_gpio_type type;
103 int gpio_nums;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800104};
105
Eric Miao0807da52009-01-07 18:01:51 +0800106static DEFINE_SPINLOCK(gpio_lock);
107static struct pxa_gpio_chip *pxa_gpio_chips;
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800108static enum pxa_gpio_type gpio_type;
Haojian Zhuang157d2642011-10-17 20:37:52 +0800109static void __iomem *gpio_reg_base;
Eric Miao0807da52009-01-07 18:01:51 +0800110
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800111static struct pxa_gpio_id pxa25x_id = {
112 .type = PXA25X_GPIO,
113 .gpio_nums = 85,
114};
115
116static struct pxa_gpio_id pxa26x_id = {
117 .type = PXA26X_GPIO,
118 .gpio_nums = 90,
119};
120
121static struct pxa_gpio_id pxa27x_id = {
122 .type = PXA27X_GPIO,
123 .gpio_nums = 121,
124};
125
126static struct pxa_gpio_id pxa3xx_id = {
127 .type = PXA3XX_GPIO,
128 .gpio_nums = 128,
129};
130
131static struct pxa_gpio_id pxa93x_id = {
132 .type = PXA93X_GPIO,
133 .gpio_nums = 192,
134};
135
136static struct pxa_gpio_id mmp_id = {
137 .type = MMP_GPIO,
138 .gpio_nums = 128,
139};
140
141static struct pxa_gpio_id mmp2_id = {
142 .type = MMP2_GPIO,
143 .gpio_nums = 192,
144};
145
Rob Herring684bba22015-01-26 22:46:06 -0600146static struct pxa_gpio_id pxa1928_id = {
147 .type = PXA1928_GPIO,
148 .gpio_nums = 224,
149};
150
Eric Miao0807da52009-01-07 18:01:51 +0800151#define for_each_gpio_chip(i, c) \
152 for (i = 0, c = &pxa_gpio_chips[0]; i <= pxa_last_gpio; i += 32, c++)
153
154static inline void __iomem *gpio_chip_base(struct gpio_chip *c)
155{
156 return container_of(c, struct pxa_gpio_chip, chip)->regbase;
157}
158
Linus Walleija0656852011-06-13 10:42:19 +0200159static inline struct pxa_gpio_chip *gpio_to_pxachip(unsigned gpio)
Eric Miao0807da52009-01-07 18:01:51 +0800160{
161 return &pxa_gpio_chips[gpio_to_bank(gpio)];
162}
163
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800164static inline int gpio_is_pxa_type(int type)
165{
166 return (type & MMP_GPIO) == 0;
167}
168
169static inline int gpio_is_mmp_type(int type)
170{
171 return (type & MMP_GPIO) != 0;
172}
173
Haojian Zhuang157d2642011-10-17 20:37:52 +0800174/* GPIO86/87/88/89 on PXA26x have their direction bits in PXA_GPDR(2 inverted,
175 * as well as their Alternate Function value being '1' for GPIO in GAFRx.
176 */
177static inline int __gpio_is_inverted(int gpio)
178{
179 if ((gpio_type == PXA26X_GPIO) && (gpio > 85))
180 return 1;
181 return 0;
182}
183
184/*
185 * On PXA25x and PXA27x, GAFRx and GPDRx together decide the alternate
186 * function of a GPIO, and GPDRx cannot be altered once configured. It
187 * is attributed as "occupied" here (I know this terminology isn't
188 * accurate, you are welcome to propose a better one :-)
189 */
190static inline int __gpio_is_occupied(unsigned gpio)
191{
192 struct pxa_gpio_chip *pxachip;
193 void __iomem *base;
194 unsigned long gafr = 0, gpdr = 0;
195 int ret, af = 0, dir = 0;
196
197 pxachip = gpio_to_pxachip(gpio);
198 base = gpio_chip_base(&pxachip->chip);
199 gpdr = readl_relaxed(base + GPDR_OFFSET);
200
201 switch (gpio_type) {
202 case PXA25X_GPIO:
203 case PXA26X_GPIO:
204 case PXA27X_GPIO:
205 gafr = readl_relaxed(base + GAFR_OFFSET);
206 af = (gafr >> ((gpio & 0xf) * 2)) & 0x3;
207 dir = gpdr & GPIO_bit(gpio);
208
209 if (__gpio_is_inverted(gpio))
210 ret = (af != 1) || (dir == 0);
211 else
212 ret = (af != 0) || (dir != 0);
213 break;
214 default:
215 ret = gpdr & GPIO_bit(gpio);
216 break;
217 }
218 return ret;
219}
220
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800221static int pxa_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
222{
Daniel Mack9450be72012-07-22 16:55:44 +0200223 return chip->base + offset + irq_base;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800224}
225
226int pxa_irq_to_gpio(int irq)
227{
Daniel Mack9450be72012-07-22 16:55:44 +0200228 return irq - irq_base;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800229}
230
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800231static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
232{
Eric Miao0807da52009-01-07 18:01:51 +0800233 void __iomem *base = gpio_chip_base(chip);
234 uint32_t value, mask = 1 << offset;
235 unsigned long flags;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800236
Eric Miao0807da52009-01-07 18:01:51 +0800237 spin_lock_irqsave(&gpio_lock, flags);
238
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800239 value = readl_relaxed(base + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800240 if (__gpio_is_inverted(chip->base + offset))
241 value |= mask;
242 else
243 value &= ~mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800244 writel_relaxed(value, base + GPDR_OFFSET);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800245
Eric Miao0807da52009-01-07 18:01:51 +0800246 spin_unlock_irqrestore(&gpio_lock, flags);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800247 return 0;
248}
249
250static int pxa_gpio_direction_output(struct gpio_chip *chip,
Eric Miao0807da52009-01-07 18:01:51 +0800251 unsigned offset, int value)
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800252{
Eric Miao0807da52009-01-07 18:01:51 +0800253 void __iomem *base = gpio_chip_base(chip);
254 uint32_t tmp, mask = 1 << offset;
255 unsigned long flags;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800256
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800257 writel_relaxed(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET));
Eric Miao0807da52009-01-07 18:01:51 +0800258
259 spin_lock_irqsave(&gpio_lock, flags);
260
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800261 tmp = readl_relaxed(base + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800262 if (__gpio_is_inverted(chip->base + offset))
263 tmp &= ~mask;
264 else
265 tmp |= mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800266 writel_relaxed(tmp, base + GPDR_OFFSET);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800267
Eric Miao0807da52009-01-07 18:01:51 +0800268 spin_unlock_irqrestore(&gpio_lock, flags);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800269 return 0;
270}
271
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800272static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
273{
Neil Zhang3018fd82014-01-09 17:25:57 +0800274 u32 gplr = readl_relaxed(gpio_chip_base(chip) + GPLR_OFFSET);
275 return !!(gplr & (1 << offset));
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800276}
277
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800278static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
279{
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800280 writel_relaxed(1 << offset, gpio_chip_base(chip) +
Eric Miao0807da52009-01-07 18:01:51 +0800281 (value ? GPSR_OFFSET : GPCR_OFFSET));
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800282}
283
Daniel Mack72121572012-07-25 17:35:39 +0200284#ifdef CONFIG_OF_GPIO
285static int pxa_gpio_of_xlate(struct gpio_chip *gc,
286 const struct of_phandle_args *gpiospec,
287 u32 *flags)
288{
289 if (gpiospec->args[0] > pxa_last_gpio)
290 return -EINVAL;
291
292 if (gc != &pxa_gpio_chips[gpiospec->args[0] / 32].chip)
293 return -EINVAL;
294
295 if (flags)
296 *flags = gpiospec->args[1];
297
298 return gpiospec->args[0] % 32;
299}
300#endif
301
Bill Pemberton38363092012-11-19 13:22:34 -0500302static int pxa_init_gpio_chip(int gpio_end,
Robert Jarzmikb95ace52012-04-22 13:37:24 +0200303 int (*set_wake)(unsigned int, unsigned int))
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800304{
Eric Miao0807da52009-01-07 18:01:51 +0800305 int i, gpio, nbanks = gpio_to_bank(gpio_end) + 1;
306 struct pxa_gpio_chip *chips;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800307
Daniel Mack4aa78262009-06-19 22:56:09 +0200308 chips = kzalloc(nbanks * sizeof(struct pxa_gpio_chip), GFP_KERNEL);
Eric Miao0807da52009-01-07 18:01:51 +0800309 if (chips == NULL) {
310 pr_err("%s: failed to allocate GPIO chips\n", __func__);
311 return -ENOMEM;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800312 }
Eric Miao0807da52009-01-07 18:01:51 +0800313
314 for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
315 struct gpio_chip *c = &chips[i].chip;
316
317 sprintf(chips[i].label, "gpio-%d", i);
Haojian Zhuang157d2642011-10-17 20:37:52 +0800318 chips[i].regbase = gpio_reg_base + BANK_OFF(i);
Robert Jarzmikb95ace52012-04-22 13:37:24 +0200319 chips[i].set_wake = set_wake;
Eric Miao0807da52009-01-07 18:01:51 +0800320
321 c->base = gpio;
322 c->label = chips[i].label;
323
324 c->direction_input = pxa_gpio_direction_input;
325 c->direction_output = pxa_gpio_direction_output;
326 c->get = pxa_gpio_get;
327 c->set = pxa_gpio_set;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800328 c->to_irq = pxa_gpio_to_irq;
Daniel Mack72121572012-07-25 17:35:39 +0200329#ifdef CONFIG_OF_GPIO
330 c->of_node = pxa_gpio_of_node;
331 c->of_xlate = pxa_gpio_of_xlate;
332 c->of_gpio_n_cells = 2;
333#endif
Eric Miao0807da52009-01-07 18:01:51 +0800334
335 /* number of GPIOs on last bank may be less than 32 */
336 c->ngpio = (gpio + 31 > gpio_end) ? (gpio_end - gpio + 1) : 32;
337 gpiochip_add(c);
338 }
339 pxa_gpio_chips = chips;
340 return 0;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800341}
342
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800343/* Update only those GRERx and GFERx edge detection register bits if those
344 * bits are set in c->irq_mask
345 */
346static inline void update_edge_detect(struct pxa_gpio_chip *c)
347{
348 uint32_t grer, gfer;
349
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800350 grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~c->irq_mask;
351 gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~c->irq_mask;
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800352 grer |= c->irq_edge_rise & c->irq_mask;
353 gfer |= c->irq_edge_fall & c->irq_mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800354 writel_relaxed(grer, c->regbase + GRER_OFFSET);
355 writel_relaxed(gfer, c->regbase + GFER_OFFSET);
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800356}
357
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100358static int pxa_gpio_irq_type(struct irq_data *d, unsigned int type)
eric miaoe3630db2008-03-04 11:42:26 +0800359{
Eric Miao0807da52009-01-07 18:01:51 +0800360 struct pxa_gpio_chip *c;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800361 int gpio = pxa_irq_to_gpio(d->irq);
Eric Miao0807da52009-01-07 18:01:51 +0800362 unsigned long gpdr, mask = GPIO_bit(gpio);
eric miaoe3630db2008-03-04 11:42:26 +0800363
Linus Walleija0656852011-06-13 10:42:19 +0200364 c = gpio_to_pxachip(gpio);
eric miaoe3630db2008-03-04 11:42:26 +0800365
366 if (type == IRQ_TYPE_PROBE) {
367 /* Don't mess with enabled GPIOs using preconfigured edges or
368 * GPIOs set to alternate function or to output during probe
369 */
Eric Miao0807da52009-01-07 18:01:51 +0800370 if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
eric miaoe3630db2008-03-04 11:42:26 +0800371 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800372
373 if (__gpio_is_occupied(gpio))
eric miaoe3630db2008-03-04 11:42:26 +0800374 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800375
eric miaoe3630db2008-03-04 11:42:26 +0800376 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
377 }
378
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800379 gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
Eric Miao0807da52009-01-07 18:01:51 +0800380
Eric Miao067455a2008-11-26 18:12:04 +0800381 if (__gpio_is_inverted(gpio))
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800382 writel_relaxed(gpdr | mask, c->regbase + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800383 else
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800384 writel_relaxed(gpdr & ~mask, c->regbase + GPDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800385
386 if (type & IRQ_TYPE_EDGE_RISING)
Eric Miao0807da52009-01-07 18:01:51 +0800387 c->irq_edge_rise |= mask;
eric miaoe3630db2008-03-04 11:42:26 +0800388 else
Eric Miao0807da52009-01-07 18:01:51 +0800389 c->irq_edge_rise &= ~mask;
eric miaoe3630db2008-03-04 11:42:26 +0800390
391 if (type & IRQ_TYPE_EDGE_FALLING)
Eric Miao0807da52009-01-07 18:01:51 +0800392 c->irq_edge_fall |= mask;
eric miaoe3630db2008-03-04 11:42:26 +0800393 else
Eric Miao0807da52009-01-07 18:01:51 +0800394 c->irq_edge_fall &= ~mask;
eric miaoe3630db2008-03-04 11:42:26 +0800395
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800396 update_edge_detect(c);
eric miaoe3630db2008-03-04 11:42:26 +0800397
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100398 pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->irq, gpio,
eric miaoe3630db2008-03-04 11:42:26 +0800399 ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
400 ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
401 return 0;
402}
403
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200404static void pxa_gpio_demux_handler(struct irq_desc *desc)
eric miaoe3630db2008-03-04 11:42:26 +0800405{
Eric Miao0807da52009-01-07 18:01:51 +0800406 struct pxa_gpio_chip *c;
407 int loop, gpio, gpio_base, n;
408 unsigned long gedr;
Chao Xie0d2ee5d2012-07-31 14:13:09 +0800409 struct irq_chip *chip = irq_desc_get_chip(desc);
410
411 chained_irq_enter(chip, desc);
eric miaoe3630db2008-03-04 11:42:26 +0800412
413 do {
eric miaoe3630db2008-03-04 11:42:26 +0800414 loop = 0;
Eric Miao0807da52009-01-07 18:01:51 +0800415 for_each_gpio_chip(gpio, c) {
416 gpio_base = c->chip.base;
eric miaoe3630db2008-03-04 11:42:26 +0800417
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800418 gedr = readl_relaxed(c->regbase + GEDR_OFFSET);
Eric Miao0807da52009-01-07 18:01:51 +0800419 gedr = gedr & c->irq_mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800420 writel_relaxed(gedr, c->regbase + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800421
Wei Yongjund724f1c2012-09-14 10:36:59 +0800422 for_each_set_bit(n, &gedr, BITS_PER_LONG) {
Eric Miao0807da52009-01-07 18:01:51 +0800423 loop = 1;
424
425 generic_handle_irq(gpio_to_irq(gpio_base + n));
Eric Miao0807da52009-01-07 18:01:51 +0800426 }
eric miaoe3630db2008-03-04 11:42:26 +0800427 }
428 } while (loop);
Chao Xie0d2ee5d2012-07-31 14:13:09 +0800429
430 chained_irq_exit(chip, desc);
eric miaoe3630db2008-03-04 11:42:26 +0800431}
432
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100433static void pxa_ack_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800434{
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800435 int gpio = pxa_irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200436 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800437
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800438 writel_relaxed(GPIO_bit(gpio), c->regbase + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800439}
440
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100441static void pxa_mask_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800442{
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800443 int gpio = pxa_irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200444 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800445 uint32_t grer, gfer;
446
447 c->irq_mask &= ~GPIO_bit(gpio);
448
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800449 grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~GPIO_bit(gpio);
450 gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~GPIO_bit(gpio);
451 writel_relaxed(grer, c->regbase + GRER_OFFSET);
452 writel_relaxed(gfer, c->regbase + GFER_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800453}
454
Robert Jarzmikb95ace52012-04-22 13:37:24 +0200455static int pxa_gpio_set_wake(struct irq_data *d, unsigned int on)
456{
457 int gpio = pxa_irq_to_gpio(d->irq);
458 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
459
460 if (c->set_wake)
461 return c->set_wake(gpio, on);
462 else
463 return 0;
464}
465
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100466static void pxa_unmask_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800467{
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800468 int gpio = pxa_irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200469 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800470
471 c->irq_mask |= GPIO_bit(gpio);
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800472 update_edge_detect(c);
eric miaoe3630db2008-03-04 11:42:26 +0800473}
474
475static struct irq_chip pxa_muxed_gpio_chip = {
476 .name = "GPIO",
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100477 .irq_ack = pxa_ack_muxed_gpio,
478 .irq_mask = pxa_mask_muxed_gpio,
479 .irq_unmask = pxa_unmask_muxed_gpio,
480 .irq_set_type = pxa_gpio_irq_type,
Robert Jarzmikb95ace52012-04-22 13:37:24 +0200481 .irq_set_wake = pxa_gpio_set_wake,
eric miaoe3630db2008-03-04 11:42:26 +0800482};
483
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800484static int pxa_gpio_nums(struct platform_device *pdev)
Haojian Zhuang478e2232011-10-14 16:44:07 +0800485{
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800486 const struct platform_device_id *id = platform_get_device_id(pdev);
487 struct pxa_gpio_id *pxa_id = (struct pxa_gpio_id *)id->driver_data;
Haojian Zhuang478e2232011-10-14 16:44:07 +0800488 int count = 0;
489
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800490 switch (pxa_id->type) {
491 case PXA25X_GPIO:
492 case PXA26X_GPIO:
493 case PXA27X_GPIO:
494 case PXA3XX_GPIO:
495 case PXA93X_GPIO:
496 case MMP_GPIO:
497 case MMP2_GPIO:
Rob Herring684bba22015-01-26 22:46:06 -0600498 case PXA1928_GPIO:
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800499 gpio_type = pxa_id->type;
500 count = pxa_id->gpio_nums - 1;
501 break;
502 default:
503 count = -EINVAL;
504 break;
Haojian Zhuang478e2232011-10-14 16:44:07 +0800505 }
Haojian Zhuang478e2232011-10-14 16:44:07 +0800506 return count;
507}
508
Arnd Bergmannf43e04e2012-08-13 14:36:10 +0000509#ifdef CONFIG_OF
Jingoo Han0fb39412014-06-03 21:10:25 +0900510static const struct of_device_id pxa_gpio_dt_ids[] = {
Haojian Zhuangf8731172013-04-09 22:27:50 +0800511 { .compatible = "intel,pxa25x-gpio", .data = &pxa25x_id, },
512 { .compatible = "intel,pxa26x-gpio", .data = &pxa26x_id, },
513 { .compatible = "intel,pxa27x-gpio", .data = &pxa27x_id, },
514 { .compatible = "intel,pxa3xx-gpio", .data = &pxa3xx_id, },
515 { .compatible = "marvell,pxa93x-gpio", .data = &pxa93x_id, },
516 { .compatible = "marvell,mmp-gpio", .data = &mmp_id, },
517 { .compatible = "marvell,mmp2-gpio", .data = &mmp2_id, },
Rob Herring684bba22015-01-26 22:46:06 -0600518 { .compatible = "marvell,pxa1928-gpio", .data = &pxa1928_id, },
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800519 {}
520};
521
522static int pxa_irq_domain_map(struct irq_domain *d, unsigned int irq,
523 irq_hw_number_t hw)
524{
525 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
526 handle_edge_irq);
Rob Herring23393d42015-07-27 15:55:16 -0500527 irq_set_noprobe(irq);
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800528 return 0;
529}
530
531const struct irq_domain_ops pxa_irq_domain_ops = {
532 .map = pxa_irq_domain_map,
Daniel Mack72121572012-07-25 17:35:39 +0200533 .xlate = irq_domain_xlate_twocell,
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800534};
535
Bill Pemberton38363092012-11-19 13:22:34 -0500536static int pxa_gpio_probe_dt(struct platform_device *pdev)
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800537{
Daniel Mack5dbb7c62013-07-11 17:17:53 +0200538 int ret = 0, nr_gpios;
539 struct device_node *np = pdev->dev.of_node;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800540 const struct of_device_id *of_id =
541 of_match_device(pxa_gpio_dt_ids, &pdev->dev);
Haojian Zhuangf8731172013-04-09 22:27:50 +0800542 const struct pxa_gpio_id *gpio_id;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800543
Haojian Zhuangf8731172013-04-09 22:27:50 +0800544 if (!of_id || !of_id->data) {
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800545 dev_err(&pdev->dev, "Failed to find gpio controller\n");
546 return -EFAULT;
547 }
Haojian Zhuangf8731172013-04-09 22:27:50 +0800548 gpio_id = of_id->data;
549 gpio_type = gpio_id->type;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800550
Haojian Zhuangf8731172013-04-09 22:27:50 +0800551 nr_gpios = gpio_id->gpio_nums;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800552 pxa_last_gpio = nr_gpios - 1;
553
554 irq_base = irq_alloc_descs(-1, 0, nr_gpios, 0);
555 if (irq_base < 0) {
556 dev_err(&pdev->dev, "Failed to allocate IRQ numbers\n");
Daniel Mack5dbb7c62013-07-11 17:17:53 +0200557 ret = irq_base;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800558 goto err;
559 }
560 domain = irq_domain_add_legacy(np, nr_gpios, irq_base, 0,
561 &pxa_irq_domain_ops, NULL);
Daniel Mack72121572012-07-25 17:35:39 +0200562 pxa_gpio_of_node = np;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800563 return 0;
564err:
565 iounmap(gpio_reg_base);
566 return ret;
567}
568#else
569#define pxa_gpio_probe_dt(pdev) (-1)
570#endif
571
Bill Pemberton38363092012-11-19 13:22:34 -0500572static int pxa_gpio_probe(struct platform_device *pdev)
eric miaoe3630db2008-03-04 11:42:26 +0800573{
Eric Miao0807da52009-01-07 18:01:51 +0800574 struct pxa_gpio_chip *c;
Haojian Zhuang157d2642011-10-17 20:37:52 +0800575 struct resource *res;
Haojian Zhuang389eda12011-10-17 21:26:55 +0800576 struct clk *clk;
Robert Jarzmikb95ace52012-04-22 13:37:24 +0200577 struct pxa_gpio_platform_data *info;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800578 int gpio, irq, ret, use_of = 0;
Haojian Zhuang157d2642011-10-17 20:37:52 +0800579 int irq0 = 0, irq1 = 0, irq_mux, gpio_offset = 0;
eric miaoe3630db2008-03-04 11:42:26 +0800580
Haojian Zhuangb8f649f2013-04-09 18:12:04 +0800581 info = dev_get_platdata(&pdev->dev);
582 if (info) {
583 irq_base = info->irq_base;
584 if (irq_base <= 0)
585 return -EINVAL;
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800586 pxa_last_gpio = pxa_gpio_nums(pdev);
Daniel Mack9450be72012-07-22 16:55:44 +0200587 } else {
Haojian Zhuangb8f649f2013-04-09 18:12:04 +0800588 irq_base = 0;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800589 use_of = 1;
Haojian Zhuangb8f649f2013-04-09 18:12:04 +0800590 ret = pxa_gpio_probe_dt(pdev);
591 if (ret < 0)
592 return -EINVAL;
Daniel Mack9450be72012-07-22 16:55:44 +0200593 }
594
Haojian Zhuang478e2232011-10-14 16:44:07 +0800595 if (!pxa_last_gpio)
Haojian Zhuang157d2642011-10-17 20:37:52 +0800596 return -EINVAL;
597
598 irq0 = platform_get_irq_byname(pdev, "gpio0");
599 irq1 = platform_get_irq_byname(pdev, "gpio1");
600 irq_mux = platform_get_irq_byname(pdev, "gpio_mux");
601 if ((irq0 > 0 && irq1 <= 0) || (irq0 <= 0 && irq1 > 0)
602 || (irq_mux <= 0))
603 return -EINVAL;
604 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
605 if (!res)
606 return -EINVAL;
607 gpio_reg_base = ioremap(res->start, resource_size(res));
608 if (!gpio_reg_base)
609 return -EINVAL;
610
611 if (irq0 > 0)
612 gpio_offset = 2;
eric miaoe3630db2008-03-04 11:42:26 +0800613
Haojian Zhuang389eda12011-10-17 21:26:55 +0800614 clk = clk_get(&pdev->dev, NULL);
615 if (IS_ERR(clk)) {
616 dev_err(&pdev->dev, "Error %ld to get gpio clock\n",
617 PTR_ERR(clk));
618 iounmap(gpio_reg_base);
619 return PTR_ERR(clk);
620 }
Julia Lawall6ab49f42012-08-26 18:00:55 +0200621 ret = clk_prepare_enable(clk);
Haojian Zhuang389eda12011-10-17 21:26:55 +0800622 if (ret) {
623 clk_put(clk);
624 iounmap(gpio_reg_base);
625 return ret;
626 }
Haojian Zhuang389eda12011-10-17 21:26:55 +0800627
Eric Miao0807da52009-01-07 18:01:51 +0800628 /* Initialize GPIO chips */
Robert Jarzmikb95ace52012-04-22 13:37:24 +0200629 pxa_init_gpio_chip(pxa_last_gpio, info ? info->gpio_set_wake : NULL);
Eric Miao0807da52009-01-07 18:01:51 +0800630
eric miaoe3630db2008-03-04 11:42:26 +0800631 /* clear all GPIO edge detects */
Eric Miao0807da52009-01-07 18:01:51 +0800632 for_each_gpio_chip(gpio, c) {
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800633 writel_relaxed(0, c->regbase + GFER_OFFSET);
634 writel_relaxed(0, c->regbase + GRER_OFFSET);
Laurent Navete37f4af2013-03-20 13:15:59 +0100635 writel_relaxed(~0, c->regbase + GEDR_OFFSET);
Haojian Zhuangbe241682011-10-17 21:07:15 +0800636 /* unmask GPIO edge detect for AP side */
637 if (gpio_is_mmp_type(gpio_type))
638 writel_relaxed(~0, c->regbase + ED_MASK_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800639 }
640
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800641 if (!use_of) {
Rob Herringae4f4cf2015-01-26 22:46:04 -0600642 if (irq0 > 0) {
643 irq = gpio_to_irq(0);
644 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
645 handle_edge_irq);
Rob Herring23393d42015-07-27 15:55:16 -0500646 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
Rob Herringae4f4cf2015-01-26 22:46:04 -0600647 }
648 if (irq1 > 0) {
649 irq = gpio_to_irq(1);
650 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
651 handle_edge_irq);
Rob Herring23393d42015-07-27 15:55:16 -0500652 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
Rob Herringae4f4cf2015-01-26 22:46:04 -0600653 }
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800654
655 for (irq = gpio_to_irq(gpio_offset);
656 irq <= gpio_to_irq(pxa_last_gpio); irq++) {
657 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
658 handle_edge_irq);
Rob Herring23393d42015-07-27 15:55:16 -0500659 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800660 }
eric miaoe3630db2008-03-04 11:42:26 +0800661 }
662
Rob Herringae4f4cf2015-01-26 22:46:04 -0600663 if (irq0 > 0)
664 irq_set_chained_handler(irq0, pxa_gpio_demux_handler);
665 if (irq1 > 0)
666 irq_set_chained_handler(irq1, pxa_gpio_demux_handler);
667
Haojian Zhuang157d2642011-10-17 20:37:52 +0800668 irq_set_chained_handler(irq_mux, pxa_gpio_demux_handler);
669 return 0;
eric miaoe3630db2008-03-04 11:42:26 +0800670}
eric miao663707c2008-03-04 16:13:58 +0800671
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800672static const struct platform_device_id gpio_id_table[] = {
673 { "pxa25x-gpio", (unsigned long)&pxa25x_id },
674 { "pxa26x-gpio", (unsigned long)&pxa26x_id },
675 { "pxa27x-gpio", (unsigned long)&pxa27x_id },
676 { "pxa3xx-gpio", (unsigned long)&pxa3xx_id },
677 { "pxa93x-gpio", (unsigned long)&pxa93x_id },
678 { "mmp-gpio", (unsigned long)&mmp_id },
679 { "mmp2-gpio", (unsigned long)&mmp2_id },
Rob Herring684bba22015-01-26 22:46:06 -0600680 { "pxa1928-gpio", (unsigned long)&pxa1928_id },
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800681 { },
682};
683
Haojian Zhuang157d2642011-10-17 20:37:52 +0800684static struct platform_driver pxa_gpio_driver = {
685 .probe = pxa_gpio_probe,
686 .driver = {
687 .name = "pxa-gpio",
Arnd Bergmannf43e04e2012-08-13 14:36:10 +0000688 .of_match_table = of_match_ptr(pxa_gpio_dt_ids),
Haojian Zhuang157d2642011-10-17 20:37:52 +0800689 },
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800690 .id_table = gpio_id_table,
Haojian Zhuang157d2642011-10-17 20:37:52 +0800691};
Linus Walleijcf3fa172013-04-24 21:41:20 +0200692
693static int __init pxa_gpio_init(void)
694{
695 return platform_driver_register(&pxa_gpio_driver);
696}
697postcore_initcall(pxa_gpio_init);
Haojian Zhuang157d2642011-10-17 20:37:52 +0800698
eric miao663707c2008-03-04 16:13:58 +0800699#ifdef CONFIG_PM
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200700static int pxa_gpio_suspend(void)
eric miao663707c2008-03-04 16:13:58 +0800701{
Eric Miao0807da52009-01-07 18:01:51 +0800702 struct pxa_gpio_chip *c;
703 int gpio;
eric miao663707c2008-03-04 16:13:58 +0800704
Eric Miao0807da52009-01-07 18:01:51 +0800705 for_each_gpio_chip(gpio, c) {
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800706 c->saved_gplr = readl_relaxed(c->regbase + GPLR_OFFSET);
707 c->saved_gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
708 c->saved_grer = readl_relaxed(c->regbase + GRER_OFFSET);
709 c->saved_gfer = readl_relaxed(c->regbase + GFER_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800710
711 /* Clear GPIO transition detect bits */
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800712 writel_relaxed(0xffffffff, c->regbase + GEDR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800713 }
714 return 0;
715}
716
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200717static void pxa_gpio_resume(void)
eric miao663707c2008-03-04 16:13:58 +0800718{
Eric Miao0807da52009-01-07 18:01:51 +0800719 struct pxa_gpio_chip *c;
720 int gpio;
eric miao663707c2008-03-04 16:13:58 +0800721
Eric Miao0807da52009-01-07 18:01:51 +0800722 for_each_gpio_chip(gpio, c) {
eric miao663707c2008-03-04 16:13:58 +0800723 /* restore level with set/clear */
Laurent Navete37f4af2013-03-20 13:15:59 +0100724 writel_relaxed(c->saved_gplr, c->regbase + GPSR_OFFSET);
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800725 writel_relaxed(~c->saved_gplr, c->regbase + GPCR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800726
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800727 writel_relaxed(c->saved_grer, c->regbase + GRER_OFFSET);
728 writel_relaxed(c->saved_gfer, c->regbase + GFER_OFFSET);
729 writel_relaxed(c->saved_gpdr, c->regbase + GPDR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800730 }
eric miao663707c2008-03-04 16:13:58 +0800731}
732#else
733#define pxa_gpio_suspend NULL
734#define pxa_gpio_resume NULL
735#endif
736
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200737struct syscore_ops pxa_gpio_syscore_ops = {
eric miao663707c2008-03-04 16:13:58 +0800738 .suspend = pxa_gpio_suspend,
739 .resume = pxa_gpio_resume,
740};
Haojian Zhuang157d2642011-10-17 20:37:52 +0800741
742static int __init pxa_gpio_sysinit(void)
743{
744 register_syscore_ops(&pxa_gpio_syscore_ops);
745 return 0;
746}
747postcore_initcall(pxa_gpio_sysinit);