blob: 69916c65f0945f0ff3b5c791a5c98fa37396a68a [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
Robert Jarzmikfc0589c2015-11-28 22:37:42 +010072struct pxa_gpio_bank {
Eric Miao0807da52009-01-07 18:01:51 +080073 void __iomem *regbase;
Eric Miao0807da52009-01-07 18:01:51 +080074 unsigned long irq_mask;
75 unsigned long irq_edge_rise;
76 unsigned long irq_edge_fall;
77
78#ifdef CONFIG_PM
79 unsigned long saved_gplr;
80 unsigned long saved_gpdr;
81 unsigned long saved_grer;
82 unsigned long saved_gfer;
83#endif
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080084};
85
Robert Jarzmikfc0589c2015-11-28 22:37:42 +010086struct pxa_gpio_chip {
87 struct device *dev;
88 struct gpio_chip chip;
89 struct pxa_gpio_bank *banks;
90
91 int irq0;
92 int irq1;
93 int (*set_wake)(unsigned int gpio, unsigned int on);
94};
95
Haojian Zhuang2cab0292013-04-07 16:44:33 +080096enum pxa_gpio_type {
Haojian Zhuang4929f5a2011-10-10 16:03:51 +080097 PXA25X_GPIO = 0,
98 PXA26X_GPIO,
99 PXA27X_GPIO,
100 PXA3XX_GPIO,
101 PXA93X_GPIO,
102 MMP_GPIO = 0x10,
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800103 MMP2_GPIO,
Rob Herring684bba22015-01-26 22:46:06 -0600104 PXA1928_GPIO,
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800105};
106
107struct pxa_gpio_id {
108 enum pxa_gpio_type type;
109 int gpio_nums;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800110};
111
Eric Miao0807da52009-01-07 18:01:51 +0800112static DEFINE_SPINLOCK(gpio_lock);
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100113static struct pxa_gpio_chip *pxa_gpio_chip;
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800114static enum pxa_gpio_type gpio_type;
Eric Miao0807da52009-01-07 18:01:51 +0800115
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800116static struct pxa_gpio_id pxa25x_id = {
117 .type = PXA25X_GPIO,
118 .gpio_nums = 85,
119};
120
121static struct pxa_gpio_id pxa26x_id = {
122 .type = PXA26X_GPIO,
123 .gpio_nums = 90,
124};
125
126static struct pxa_gpio_id pxa27x_id = {
127 .type = PXA27X_GPIO,
128 .gpio_nums = 121,
129};
130
131static struct pxa_gpio_id pxa3xx_id = {
132 .type = PXA3XX_GPIO,
133 .gpio_nums = 128,
134};
135
136static struct pxa_gpio_id pxa93x_id = {
137 .type = PXA93X_GPIO,
138 .gpio_nums = 192,
139};
140
141static struct pxa_gpio_id mmp_id = {
142 .type = MMP_GPIO,
143 .gpio_nums = 128,
144};
145
146static struct pxa_gpio_id mmp2_id = {
147 .type = MMP2_GPIO,
148 .gpio_nums = 192,
149};
150
Rob Herring684bba22015-01-26 22:46:06 -0600151static struct pxa_gpio_id pxa1928_id = {
152 .type = PXA1928_GPIO,
153 .gpio_nums = 224,
154};
155
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100156#define for_each_gpio_bank(i, b, pc) \
157 for (i = 0, b = pc->banks; i <= pxa_last_gpio; i += 32, b++)
Eric Miao0807da52009-01-07 18:01:51 +0800158
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100159static inline struct pxa_gpio_chip *chip_to_pxachip(struct gpio_chip *c)
Eric Miao0807da52009-01-07 18:01:51 +0800160{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100161 struct pxa_gpio_chip *pxa_chip =
162 container_of(c, struct pxa_gpio_chip, chip);
163
164 return pxa_chip;
165}
166static inline void __iomem *gpio_bank_base(struct gpio_chip *c, int gpio)
167{
168 struct pxa_gpio_bank *bank = chip_to_pxachip(c)->banks + (gpio / 32);
169
170 return bank->regbase;
Eric Miao0807da52009-01-07 18:01:51 +0800171}
172
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100173static inline struct pxa_gpio_bank *gpio_to_pxabank(struct gpio_chip *c,
174 unsigned gpio)
Eric Miao0807da52009-01-07 18:01:51 +0800175{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100176 return chip_to_pxachip(c)->banks + gpio / 32;
Eric Miao0807da52009-01-07 18:01:51 +0800177}
178
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800179static inline int gpio_is_pxa_type(int type)
180{
181 return (type & MMP_GPIO) == 0;
182}
183
184static inline int gpio_is_mmp_type(int type)
185{
186 return (type & MMP_GPIO) != 0;
187}
188
Haojian Zhuang157d2642011-10-17 20:37:52 +0800189/* GPIO86/87/88/89 on PXA26x have their direction bits in PXA_GPDR(2 inverted,
190 * as well as their Alternate Function value being '1' for GPIO in GAFRx.
191 */
192static inline int __gpio_is_inverted(int gpio)
193{
194 if ((gpio_type == PXA26X_GPIO) && (gpio > 85))
195 return 1;
196 return 0;
197}
198
199/*
200 * On PXA25x and PXA27x, GAFRx and GPDRx together decide the alternate
201 * function of a GPIO, and GPDRx cannot be altered once configured. It
202 * is attributed as "occupied" here (I know this terminology isn't
203 * accurate, you are welcome to propose a better one :-)
204 */
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100205static inline int __gpio_is_occupied(struct pxa_gpio_chip *pchip, unsigned gpio)
Haojian Zhuang157d2642011-10-17 20:37:52 +0800206{
Haojian Zhuang157d2642011-10-17 20:37:52 +0800207 void __iomem *base;
208 unsigned long gafr = 0, gpdr = 0;
209 int ret, af = 0, dir = 0;
210
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100211 base = gpio_bank_base(&pchip->chip, gpio);
Haojian Zhuang157d2642011-10-17 20:37:52 +0800212 gpdr = readl_relaxed(base + GPDR_OFFSET);
213
214 switch (gpio_type) {
215 case PXA25X_GPIO:
216 case PXA26X_GPIO:
217 case PXA27X_GPIO:
218 gafr = readl_relaxed(base + GAFR_OFFSET);
219 af = (gafr >> ((gpio & 0xf) * 2)) & 0x3;
220 dir = gpdr & GPIO_bit(gpio);
221
222 if (__gpio_is_inverted(gpio))
223 ret = (af != 1) || (dir == 0);
224 else
225 ret = (af != 0) || (dir != 0);
226 break;
227 default:
228 ret = gpdr & GPIO_bit(gpio);
229 break;
230 }
231 return ret;
232}
233
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800234static int pxa_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
235{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100236 return offset + irq_base;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800237}
238
239int pxa_irq_to_gpio(int irq)
240{
Daniel Mack9450be72012-07-22 16:55:44 +0200241 return irq - irq_base;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800242}
243
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800244static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
245{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100246 void __iomem *base = gpio_bank_base(chip, offset);
247 uint32_t value, mask = GPIO_bit(offset);
Eric Miao0807da52009-01-07 18:01:51 +0800248 unsigned long flags;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800249
Eric Miao0807da52009-01-07 18:01:51 +0800250 spin_lock_irqsave(&gpio_lock, flags);
251
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800252 value = readl_relaxed(base + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800253 if (__gpio_is_inverted(chip->base + offset))
254 value |= mask;
255 else
256 value &= ~mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800257 writel_relaxed(value, base + GPDR_OFFSET);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800258
Eric Miao0807da52009-01-07 18:01:51 +0800259 spin_unlock_irqrestore(&gpio_lock, flags);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800260 return 0;
261}
262
263static int pxa_gpio_direction_output(struct gpio_chip *chip,
Eric Miao0807da52009-01-07 18:01:51 +0800264 unsigned offset, int value)
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800265{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100266 void __iomem *base = gpio_bank_base(chip, offset);
267 uint32_t tmp, mask = GPIO_bit(offset);
Eric Miao0807da52009-01-07 18:01:51 +0800268 unsigned long flags;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800269
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800270 writel_relaxed(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET));
Eric Miao0807da52009-01-07 18:01:51 +0800271
272 spin_lock_irqsave(&gpio_lock, flags);
273
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800274 tmp = readl_relaxed(base + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800275 if (__gpio_is_inverted(chip->base + offset))
276 tmp &= ~mask;
277 else
278 tmp |= mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800279 writel_relaxed(tmp, base + GPDR_OFFSET);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800280
Eric Miao0807da52009-01-07 18:01:51 +0800281 spin_unlock_irqrestore(&gpio_lock, flags);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800282 return 0;
283}
284
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800285static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
286{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100287 void __iomem *base = gpio_bank_base(chip, offset);
288 u32 gplr = readl_relaxed(base + GPLR_OFFSET);
289
290 return !!(gplr & GPIO_bit(offset));
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800291}
292
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800293static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
294{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100295 void __iomem *base = gpio_bank_base(chip, offset);
296
297 writel_relaxed(GPIO_bit(offset),
298 base + (value ? GPSR_OFFSET : GPCR_OFFSET));
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800299}
300
Daniel Mack72121572012-07-25 17:35:39 +0200301#ifdef CONFIG_OF_GPIO
302static int pxa_gpio_of_xlate(struct gpio_chip *gc,
303 const struct of_phandle_args *gpiospec,
304 u32 *flags)
305{
306 if (gpiospec->args[0] > pxa_last_gpio)
307 return -EINVAL;
308
Daniel Mack72121572012-07-25 17:35:39 +0200309 if (flags)
310 *flags = gpiospec->args[1];
311
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100312 return gpiospec->args[0];
Daniel Mack72121572012-07-25 17:35:39 +0200313}
314#endif
315
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100316static int pxa_init_gpio_chip(struct pxa_gpio_chip *pchip, int ngpio,
317 void __iomem *regbase)
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800318{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100319 int i, gpio, nbanks = DIV_ROUND_UP(ngpio, 32);
320 struct pxa_gpio_bank *bank;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800321
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100322 pchip->banks = devm_kcalloc(pchip->dev, nbanks, sizeof(*pchip->banks),
323 GFP_KERNEL);
324 if (!pchip->banks)
Eric Miao0807da52009-01-07 18:01:51 +0800325 return -ENOMEM;
Eric Miao0807da52009-01-07 18:01:51 +0800326
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100327 pchip->chip.label = "gpio-pxa";
328 pchip->chip.direction_input = pxa_gpio_direction_input;
329 pchip->chip.direction_output = pxa_gpio_direction_output;
330 pchip->chip.get = pxa_gpio_get;
331 pchip->chip.set = pxa_gpio_set;
332 pchip->chip.to_irq = pxa_gpio_to_irq;
333 pchip->chip.ngpio = ngpio;
Daniel Mack72121572012-07-25 17:35:39 +0200334#ifdef CONFIG_OF_GPIO
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100335 pchip->chip.of_node = pxa_gpio_of_node;
336 pchip->chip.of_xlate = pxa_gpio_of_xlate;
337 pchip->chip.of_gpio_n_cells = 2;
Daniel Mack72121572012-07-25 17:35:39 +0200338#endif
Eric Miao0807da52009-01-07 18:01:51 +0800339
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100340 for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
341 bank = pchip->banks + i;
342 bank->regbase = regbase + BANK_OFF(i);
Eric Miao0807da52009-01-07 18:01:51 +0800343 }
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100344
345 return gpiochip_add(&pchip->chip);
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800346}
347
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800348/* Update only those GRERx and GFERx edge detection register bits if those
349 * bits are set in c->irq_mask
350 */
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100351static inline void update_edge_detect(struct pxa_gpio_bank *c)
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800352{
353 uint32_t grer, gfer;
354
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800355 grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~c->irq_mask;
356 gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~c->irq_mask;
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800357 grer |= c->irq_edge_rise & c->irq_mask;
358 gfer |= c->irq_edge_fall & c->irq_mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800359 writel_relaxed(grer, c->regbase + GRER_OFFSET);
360 writel_relaxed(gfer, c->regbase + GFER_OFFSET);
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800361}
362
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100363static int pxa_gpio_irq_type(struct irq_data *d, unsigned int type)
eric miaoe3630db2008-03-04 11:42:26 +0800364{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100365 struct pxa_gpio_chip *pchip = pxa_gpio_chip;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800366 int gpio = pxa_irq_to_gpio(d->irq);
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100367 struct pxa_gpio_bank *c = gpio_to_pxabank(&pchip->chip, gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800368 unsigned long gpdr, mask = GPIO_bit(gpio);
eric miaoe3630db2008-03-04 11:42:26 +0800369
eric miaoe3630db2008-03-04 11:42:26 +0800370 if (type == IRQ_TYPE_PROBE) {
371 /* Don't mess with enabled GPIOs using preconfigured edges or
372 * GPIOs set to alternate function or to output during probe
373 */
Eric Miao0807da52009-01-07 18:01:51 +0800374 if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
eric miaoe3630db2008-03-04 11:42:26 +0800375 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800376
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100377 if (__gpio_is_occupied(pchip, gpio))
eric miaoe3630db2008-03-04 11:42:26 +0800378 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800379
eric miaoe3630db2008-03-04 11:42:26 +0800380 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
381 }
382
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800383 gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
Eric Miao0807da52009-01-07 18:01:51 +0800384
Eric Miao067455a2008-11-26 18:12:04 +0800385 if (__gpio_is_inverted(gpio))
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800386 writel_relaxed(gpdr | mask, c->regbase + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800387 else
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800388 writel_relaxed(gpdr & ~mask, c->regbase + GPDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800389
390 if (type & IRQ_TYPE_EDGE_RISING)
Eric Miao0807da52009-01-07 18:01:51 +0800391 c->irq_edge_rise |= mask;
eric miaoe3630db2008-03-04 11:42:26 +0800392 else
Eric Miao0807da52009-01-07 18:01:51 +0800393 c->irq_edge_rise &= ~mask;
eric miaoe3630db2008-03-04 11:42:26 +0800394
395 if (type & IRQ_TYPE_EDGE_FALLING)
Eric Miao0807da52009-01-07 18:01:51 +0800396 c->irq_edge_fall |= mask;
eric miaoe3630db2008-03-04 11:42:26 +0800397 else
Eric Miao0807da52009-01-07 18:01:51 +0800398 c->irq_edge_fall &= ~mask;
eric miaoe3630db2008-03-04 11:42:26 +0800399
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800400 update_edge_detect(c);
eric miaoe3630db2008-03-04 11:42:26 +0800401
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100402 pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->irq, gpio,
eric miaoe3630db2008-03-04 11:42:26 +0800403 ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
404 ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
405 return 0;
406}
407
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200408static void pxa_gpio_demux_handler(struct irq_desc *desc)
eric miaoe3630db2008-03-04 11:42:26 +0800409{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100410 int loop, gpio, n, handled = 0;
Eric Miao0807da52009-01-07 18:01:51 +0800411 unsigned long gedr;
Chao Xie0d2ee5d2012-07-31 14:13:09 +0800412 struct irq_chip *chip = irq_desc_get_chip(desc);
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100413 struct pxa_gpio_chip *pchip = pxa_gpio_chip;
414 struct pxa_gpio_bank *c;
Chao Xie0d2ee5d2012-07-31 14:13:09 +0800415
416 chained_irq_enter(chip, desc);
eric miaoe3630db2008-03-04 11:42:26 +0800417
418 do {
eric miaoe3630db2008-03-04 11:42:26 +0800419 loop = 0;
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100420 for_each_gpio_bank(gpio, c, pchip) {
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800421 gedr = readl_relaxed(c->regbase + GEDR_OFFSET);
Eric Miao0807da52009-01-07 18:01:51 +0800422 gedr = gedr & c->irq_mask;
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800423 writel_relaxed(gedr, c->regbase + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800424
Wei Yongjund724f1c2012-09-14 10:36:59 +0800425 for_each_set_bit(n, &gedr, BITS_PER_LONG) {
Eric Miao0807da52009-01-07 18:01:51 +0800426 loop = 1;
427
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100428 generic_handle_irq(gpio_to_irq(gpio + n));
Eric Miao0807da52009-01-07 18:01:51 +0800429 }
eric miaoe3630db2008-03-04 11:42:26 +0800430 }
431 } while (loop);
Chao Xie0d2ee5d2012-07-31 14:13:09 +0800432
433 chained_irq_exit(chip, desc);
eric miaoe3630db2008-03-04 11:42:26 +0800434}
435
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100436static void pxa_ack_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800437{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100438 struct pxa_gpio_chip *pchip = pxa_gpio_chip;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800439 int gpio = pxa_irq_to_gpio(d->irq);
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100440 void __iomem *base = gpio_bank_base(&pchip->chip, gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800441
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100442 writel_relaxed(GPIO_bit(gpio), base + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800443}
444
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100445static void pxa_mask_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800446{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100447 struct pxa_gpio_chip *pchip = pxa_gpio_chip;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800448 int gpio = pxa_irq_to_gpio(d->irq);
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100449 struct pxa_gpio_bank *b = gpio_to_pxabank(&pchip->chip, gpio);
450 void __iomem *base = gpio_bank_base(&pchip->chip, gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800451 uint32_t grer, gfer;
452
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100453 b->irq_mask &= ~GPIO_bit(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800454
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100455 grer = readl_relaxed(base + GRER_OFFSET) & ~GPIO_bit(gpio);
456 gfer = readl_relaxed(base + GFER_OFFSET) & ~GPIO_bit(gpio);
457 writel_relaxed(grer, base + GRER_OFFSET);
458 writel_relaxed(gfer, base + GFER_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800459}
460
Robert Jarzmikb95ace52012-04-22 13:37:24 +0200461static int pxa_gpio_set_wake(struct irq_data *d, unsigned int on)
462{
463 int gpio = pxa_irq_to_gpio(d->irq);
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100464 struct pxa_gpio_chip *pchip = pxa_gpio_chip;
Robert Jarzmikb95ace52012-04-22 13:37:24 +0200465
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100466 if (pchip->set_wake)
467 return pchip->set_wake(gpio, on);
Robert Jarzmikb95ace52012-04-22 13:37:24 +0200468 else
469 return 0;
470}
471
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100472static void pxa_unmask_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800473{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100474 struct pxa_gpio_chip *pchip = pxa_gpio_chip;
Haojian Zhuang4929f5a2011-10-10 16:03:51 +0800475 int gpio = pxa_irq_to_gpio(d->irq);
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100476 struct pxa_gpio_bank *c = gpio_to_pxabank(&pchip->chip, gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800477
478 c->irq_mask |= GPIO_bit(gpio);
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800479 update_edge_detect(c);
eric miaoe3630db2008-03-04 11:42:26 +0800480}
481
482static struct irq_chip pxa_muxed_gpio_chip = {
483 .name = "GPIO",
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100484 .irq_ack = pxa_ack_muxed_gpio,
485 .irq_mask = pxa_mask_muxed_gpio,
486 .irq_unmask = pxa_unmask_muxed_gpio,
487 .irq_set_type = pxa_gpio_irq_type,
Robert Jarzmikb95ace52012-04-22 13:37:24 +0200488 .irq_set_wake = pxa_gpio_set_wake,
eric miaoe3630db2008-03-04 11:42:26 +0800489};
490
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800491static int pxa_gpio_nums(struct platform_device *pdev)
Haojian Zhuang478e2232011-10-14 16:44:07 +0800492{
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800493 const struct platform_device_id *id = platform_get_device_id(pdev);
494 struct pxa_gpio_id *pxa_id = (struct pxa_gpio_id *)id->driver_data;
Haojian Zhuang478e2232011-10-14 16:44:07 +0800495 int count = 0;
496
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800497 switch (pxa_id->type) {
498 case PXA25X_GPIO:
499 case PXA26X_GPIO:
500 case PXA27X_GPIO:
501 case PXA3XX_GPIO:
502 case PXA93X_GPIO:
503 case MMP_GPIO:
504 case MMP2_GPIO:
Rob Herring684bba22015-01-26 22:46:06 -0600505 case PXA1928_GPIO:
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800506 gpio_type = pxa_id->type;
507 count = pxa_id->gpio_nums - 1;
508 break;
509 default:
510 count = -EINVAL;
511 break;
Haojian Zhuang478e2232011-10-14 16:44:07 +0800512 }
Haojian Zhuang478e2232011-10-14 16:44:07 +0800513 return count;
514}
515
Arnd Bergmannf43e04e2012-08-13 14:36:10 +0000516#ifdef CONFIG_OF
Jingoo Han0fb39412014-06-03 21:10:25 +0900517static const struct of_device_id pxa_gpio_dt_ids[] = {
Haojian Zhuangf8731172013-04-09 22:27:50 +0800518 { .compatible = "intel,pxa25x-gpio", .data = &pxa25x_id, },
519 { .compatible = "intel,pxa26x-gpio", .data = &pxa26x_id, },
520 { .compatible = "intel,pxa27x-gpio", .data = &pxa27x_id, },
521 { .compatible = "intel,pxa3xx-gpio", .data = &pxa3xx_id, },
522 { .compatible = "marvell,pxa93x-gpio", .data = &pxa93x_id, },
523 { .compatible = "marvell,mmp-gpio", .data = &mmp_id, },
524 { .compatible = "marvell,mmp2-gpio", .data = &mmp2_id, },
Rob Herring684bba22015-01-26 22:46:06 -0600525 { .compatible = "marvell,pxa1928-gpio", .data = &pxa1928_id, },
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800526 {}
527};
528
529static int pxa_irq_domain_map(struct irq_domain *d, unsigned int irq,
530 irq_hw_number_t hw)
531{
532 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
533 handle_edge_irq);
Rob Herring23393d42015-07-27 15:55:16 -0500534 irq_set_noprobe(irq);
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800535 return 0;
536}
537
538const struct irq_domain_ops pxa_irq_domain_ops = {
539 .map = pxa_irq_domain_map,
Daniel Mack72121572012-07-25 17:35:39 +0200540 .xlate = irq_domain_xlate_twocell,
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800541};
542
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100543static int pxa_gpio_probe_dt(struct platform_device *pdev,
544 struct pxa_gpio_chip *pchip)
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800545{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100546 int nr_gpios;
Daniel Mack5dbb7c62013-07-11 17:17:53 +0200547 struct device_node *np = pdev->dev.of_node;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800548 const struct of_device_id *of_id =
549 of_match_device(pxa_gpio_dt_ids, &pdev->dev);
Haojian Zhuangf8731172013-04-09 22:27:50 +0800550 const struct pxa_gpio_id *gpio_id;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800551
Haojian Zhuangf8731172013-04-09 22:27:50 +0800552 if (!of_id || !of_id->data) {
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800553 dev_err(&pdev->dev, "Failed to find gpio controller\n");
554 return -EFAULT;
555 }
Haojian Zhuangf8731172013-04-09 22:27:50 +0800556 gpio_id = of_id->data;
557 gpio_type = gpio_id->type;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800558
Haojian Zhuangf8731172013-04-09 22:27:50 +0800559 nr_gpios = gpio_id->gpio_nums;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800560 pxa_last_gpio = nr_gpios - 1;
561
562 irq_base = irq_alloc_descs(-1, 0, nr_gpios, 0);
563 if (irq_base < 0) {
564 dev_err(&pdev->dev, "Failed to allocate IRQ numbers\n");
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100565 return irq_base;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800566 }
567 domain = irq_domain_add_legacy(np, nr_gpios, irq_base, 0,
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100568 &pxa_irq_domain_ops, pchip);
Daniel Mack72121572012-07-25 17:35:39 +0200569 pxa_gpio_of_node = np;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800570 return 0;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800571}
572#else
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100573#define pxa_gpio_probe_dt(pdev, pchip) (-1)
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800574#endif
575
Bill Pemberton38363092012-11-19 13:22:34 -0500576static int pxa_gpio_probe(struct platform_device *pdev)
eric miaoe3630db2008-03-04 11:42:26 +0800577{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100578 struct pxa_gpio_chip *pchip;
579 struct pxa_gpio_bank *c;
Haojian Zhuang157d2642011-10-17 20:37:52 +0800580 struct resource *res;
Haojian Zhuang389eda12011-10-17 21:26:55 +0800581 struct clk *clk;
Robert Jarzmikb95ace52012-04-22 13:37:24 +0200582 struct pxa_gpio_platform_data *info;
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100583 void __iomem *gpio_reg_base;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800584 int gpio, irq, ret, use_of = 0;
Haojian Zhuang157d2642011-10-17 20:37:52 +0800585 int irq0 = 0, irq1 = 0, irq_mux, gpio_offset = 0;
eric miaoe3630db2008-03-04 11:42:26 +0800586
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100587 pchip = devm_kzalloc(&pdev->dev, sizeof(*pchip), GFP_KERNEL);
588 if (!pchip)
589 return -ENOMEM;
590 pchip->dev = &pdev->dev;
591
Haojian Zhuangb8f649f2013-04-09 18:12:04 +0800592 info = dev_get_platdata(&pdev->dev);
593 if (info) {
594 irq_base = info->irq_base;
595 if (irq_base <= 0)
596 return -EINVAL;
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800597 pxa_last_gpio = pxa_gpio_nums(pdev);
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100598 pchip->set_wake = info->gpio_set_wake;
Daniel Mack9450be72012-07-22 16:55:44 +0200599 } else {
Haojian Zhuangb8f649f2013-04-09 18:12:04 +0800600 irq_base = 0;
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800601 use_of = 1;
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100602 ret = pxa_gpio_probe_dt(pdev, pchip);
Haojian Zhuangb8f649f2013-04-09 18:12:04 +0800603 if (ret < 0)
604 return -EINVAL;
Daniel Mack9450be72012-07-22 16:55:44 +0200605 }
606
Haojian Zhuang478e2232011-10-14 16:44:07 +0800607 if (!pxa_last_gpio)
Haojian Zhuang157d2642011-10-17 20:37:52 +0800608 return -EINVAL;
609
610 irq0 = platform_get_irq_byname(pdev, "gpio0");
611 irq1 = platform_get_irq_byname(pdev, "gpio1");
612 irq_mux = platform_get_irq_byname(pdev, "gpio_mux");
613 if ((irq0 > 0 && irq1 <= 0) || (irq0 <= 0 && irq1 > 0)
614 || (irq_mux <= 0))
615 return -EINVAL;
616 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Robert Jarzmik8852b2f2015-11-28 22:37:43 +0100617 gpio_reg_base = devm_ioremap(&pdev->dev, res->start,
618 resource_size(res));
Haojian Zhuang157d2642011-10-17 20:37:52 +0800619 if (!gpio_reg_base)
620 return -EINVAL;
621
622 if (irq0 > 0)
623 gpio_offset = 2;
eric miaoe3630db2008-03-04 11:42:26 +0800624
Haojian Zhuang389eda12011-10-17 21:26:55 +0800625 clk = clk_get(&pdev->dev, NULL);
626 if (IS_ERR(clk)) {
627 dev_err(&pdev->dev, "Error %ld to get gpio clock\n",
628 PTR_ERR(clk));
Haojian Zhuang389eda12011-10-17 21:26:55 +0800629 return PTR_ERR(clk);
630 }
Julia Lawall6ab49f42012-08-26 18:00:55 +0200631 ret = clk_prepare_enable(clk);
Haojian Zhuang389eda12011-10-17 21:26:55 +0800632 if (ret) {
633 clk_put(clk);
Haojian Zhuang389eda12011-10-17 21:26:55 +0800634 return ret;
635 }
Haojian Zhuang389eda12011-10-17 21:26:55 +0800636
Eric Miao0807da52009-01-07 18:01:51 +0800637 /* Initialize GPIO chips */
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100638 ret = pxa_init_gpio_chip(pchip, pxa_last_gpio + 1, gpio_reg_base);
639 if (ret) {
640 clk_put(clk);
641 return ret;
642 }
Eric Miao0807da52009-01-07 18:01:51 +0800643
eric miaoe3630db2008-03-04 11:42:26 +0800644 /* clear all GPIO edge detects */
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100645 for_each_gpio_bank(gpio, c, pchip) {
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800646 writel_relaxed(0, c->regbase + GFER_OFFSET);
647 writel_relaxed(0, c->regbase + GRER_OFFSET);
Laurent Navete37f4af2013-03-20 13:15:59 +0100648 writel_relaxed(~0, c->regbase + GEDR_OFFSET);
Haojian Zhuangbe241682011-10-17 21:07:15 +0800649 /* unmask GPIO edge detect for AP side */
650 if (gpio_is_mmp_type(gpio_type))
651 writel_relaxed(~0, c->regbase + ED_MASK_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800652 }
653
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800654 if (!use_of) {
Rob Herringae4f4cf2015-01-26 22:46:04 -0600655 if (irq0 > 0) {
656 irq = gpio_to_irq(0);
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);
Rob Herringae4f4cf2015-01-26 22:46:04 -0600660 }
661 if (irq1 > 0) {
662 irq = gpio_to_irq(1);
663 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
664 handle_edge_irq);
Rob Herring23393d42015-07-27 15:55:16 -0500665 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
Rob Herringae4f4cf2015-01-26 22:46:04 -0600666 }
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800667
668 for (irq = gpio_to_irq(gpio_offset);
669 irq <= gpio_to_irq(pxa_last_gpio); irq++) {
670 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
671 handle_edge_irq);
Rob Herring23393d42015-07-27 15:55:16 -0500672 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
Haojian Zhuang7a4d5072012-04-13 15:15:45 +0800673 }
eric miaoe3630db2008-03-04 11:42:26 +0800674 }
675
Rob Herringae4f4cf2015-01-26 22:46:04 -0600676 if (irq0 > 0)
677 irq_set_chained_handler(irq0, pxa_gpio_demux_handler);
678 if (irq1 > 0)
679 irq_set_chained_handler(irq1, pxa_gpio_demux_handler);
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100680 pxa_gpio_chip = pchip;
Rob Herringae4f4cf2015-01-26 22:46:04 -0600681
Haojian Zhuang157d2642011-10-17 20:37:52 +0800682 irq_set_chained_handler(irq_mux, pxa_gpio_demux_handler);
683 return 0;
eric miaoe3630db2008-03-04 11:42:26 +0800684}
eric miao663707c2008-03-04 16:13:58 +0800685
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800686static const struct platform_device_id gpio_id_table[] = {
687 { "pxa25x-gpio", (unsigned long)&pxa25x_id },
688 { "pxa26x-gpio", (unsigned long)&pxa26x_id },
689 { "pxa27x-gpio", (unsigned long)&pxa27x_id },
690 { "pxa3xx-gpio", (unsigned long)&pxa3xx_id },
691 { "pxa93x-gpio", (unsigned long)&pxa93x_id },
692 { "mmp-gpio", (unsigned long)&mmp_id },
693 { "mmp2-gpio", (unsigned long)&mmp2_id },
Rob Herring684bba22015-01-26 22:46:06 -0600694 { "pxa1928-gpio", (unsigned long)&pxa1928_id },
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800695 { },
696};
697
Haojian Zhuang157d2642011-10-17 20:37:52 +0800698static struct platform_driver pxa_gpio_driver = {
699 .probe = pxa_gpio_probe,
700 .driver = {
701 .name = "pxa-gpio",
Arnd Bergmannf43e04e2012-08-13 14:36:10 +0000702 .of_match_table = of_match_ptr(pxa_gpio_dt_ids),
Haojian Zhuang157d2642011-10-17 20:37:52 +0800703 },
Haojian Zhuang2cab0292013-04-07 16:44:33 +0800704 .id_table = gpio_id_table,
Haojian Zhuang157d2642011-10-17 20:37:52 +0800705};
Linus Walleijcf3fa172013-04-24 21:41:20 +0200706
Robert Jarzmikeae122b2015-11-13 21:22:38 +0100707static int __init pxa_gpio_legacy_init(void)
Linus Walleijcf3fa172013-04-24 21:41:20 +0200708{
Robert Jarzmikeae122b2015-11-13 21:22:38 +0100709 if (of_have_populated_dt())
710 return 0;
711
Linus Walleijcf3fa172013-04-24 21:41:20 +0200712 return platform_driver_register(&pxa_gpio_driver);
713}
Robert Jarzmikeae122b2015-11-13 21:22:38 +0100714postcore_initcall(pxa_gpio_legacy_init);
715
716static int __init pxa_gpio_dt_init(void)
717{
718 if (of_have_populated_dt())
719 return platform_driver_register(&pxa_gpio_driver);
720
721 return 0;
722}
723device_initcall(pxa_gpio_dt_init);
Haojian Zhuang157d2642011-10-17 20:37:52 +0800724
eric miao663707c2008-03-04 16:13:58 +0800725#ifdef CONFIG_PM
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200726static int pxa_gpio_suspend(void)
eric miao663707c2008-03-04 16:13:58 +0800727{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100728 struct pxa_gpio_chip *pchip = pxa_gpio_chip;
729 struct pxa_gpio_bank *c;
Eric Miao0807da52009-01-07 18:01:51 +0800730 int gpio;
eric miao663707c2008-03-04 16:13:58 +0800731
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100732 for_each_gpio_bank(gpio, c, pchip) {
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800733 c->saved_gplr = readl_relaxed(c->regbase + GPLR_OFFSET);
734 c->saved_gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
735 c->saved_grer = readl_relaxed(c->regbase + GRER_OFFSET);
736 c->saved_gfer = readl_relaxed(c->regbase + GFER_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800737
738 /* Clear GPIO transition detect bits */
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800739 writel_relaxed(0xffffffff, c->regbase + GEDR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800740 }
741 return 0;
742}
743
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200744static void pxa_gpio_resume(void)
eric miao663707c2008-03-04 16:13:58 +0800745{
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100746 struct pxa_gpio_chip *pchip = pxa_gpio_chip;
747 struct pxa_gpio_bank *c;
Eric Miao0807da52009-01-07 18:01:51 +0800748 int gpio;
eric miao663707c2008-03-04 16:13:58 +0800749
Robert Jarzmikfc0589c2015-11-28 22:37:42 +0100750 for_each_gpio_bank(gpio, c, pchip) {
eric miao663707c2008-03-04 16:13:58 +0800751 /* restore level with set/clear */
Laurent Navete37f4af2013-03-20 13:15:59 +0100752 writel_relaxed(c->saved_gplr, c->regbase + GPSR_OFFSET);
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800753 writel_relaxed(~c->saved_gplr, c->regbase + GPCR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800754
Haojian Zhuangdf664d22011-10-14 17:24:03 +0800755 writel_relaxed(c->saved_grer, c->regbase + GRER_OFFSET);
756 writel_relaxed(c->saved_gfer, c->regbase + GFER_OFFSET);
757 writel_relaxed(c->saved_gpdr, c->regbase + GPDR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800758 }
eric miao663707c2008-03-04 16:13:58 +0800759}
760#else
761#define pxa_gpio_suspend NULL
762#define pxa_gpio_resume NULL
763#endif
764
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200765struct syscore_ops pxa_gpio_syscore_ops = {
eric miao663707c2008-03-04 16:13:58 +0800766 .suspend = pxa_gpio_suspend,
767 .resume = pxa_gpio_resume,
768};
Haojian Zhuang157d2642011-10-17 20:37:52 +0800769
770static int __init pxa_gpio_sysinit(void)
771{
772 register_syscore_ops(&pxa_gpio_syscore_ops);
773 return 0;
774}
775postcore_initcall(pxa_gpio_sysinit);