blob: 5d6a86bfc68da67c6909583acfb4de9724868414 [file] [log] [blame]
Philipp Zabel1c44f5f2008-02-04 22:28:22 -08001/*
Eric Miao38f539a2009-01-20 12:09:06 +08002 * linux/arch/arm/plat-pxa/gpio.c
Philipp Zabel1c44f5f2008-02-04 22:28:22 -08003 *
4 * Generic PXA GPIO handling
5 *
6 * Author: Nicolas Pitre
7 * Created: Jun 15, 2001
8 * Copyright: MontaVista Software Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
Russell King2f8163b2011-07-26 10:53:52 +010014#include <linux/gpio.h>
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080015#include <linux/init.h>
eric miaoe3630db2008-03-04 11:42:26 +080016#include <linux/irq.h>
Russell Kingfced80c2008-09-06 12:10:45 +010017#include <linux/io.h>
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +020018#include <linux/syscore_ops.h>
Daniel Mack4aa78262009-06-19 22:56:09 +020019#include <linux/slab.h>
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080020
Eric Miao3b8e2852009-01-07 11:30:49 +080021int pxa_last_gpio;
22
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080023struct pxa_gpio_chip {
24 struct gpio_chip chip;
Eric Miao0807da52009-01-07 18:01:51 +080025 void __iomem *regbase;
26 char label[10];
27
28 unsigned long irq_mask;
29 unsigned long irq_edge_rise;
30 unsigned long irq_edge_fall;
31
32#ifdef CONFIG_PM
33 unsigned long saved_gplr;
34 unsigned long saved_gpdr;
35 unsigned long saved_grer;
36 unsigned long saved_gfer;
37#endif
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080038};
39
Eric Miao0807da52009-01-07 18:01:51 +080040static DEFINE_SPINLOCK(gpio_lock);
41static struct pxa_gpio_chip *pxa_gpio_chips;
42
43#define for_each_gpio_chip(i, c) \
44 for (i = 0, c = &pxa_gpio_chips[0]; i <= pxa_last_gpio; i += 32, c++)
45
46static inline void __iomem *gpio_chip_base(struct gpio_chip *c)
47{
48 return container_of(c, struct pxa_gpio_chip, chip)->regbase;
49}
50
Linus Walleija0656852011-06-13 10:42:19 +020051static inline struct pxa_gpio_chip *gpio_to_pxachip(unsigned gpio)
Eric Miao0807da52009-01-07 18:01:51 +080052{
53 return &pxa_gpio_chips[gpio_to_bank(gpio)];
54}
55
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080056static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
57{
Eric Miao0807da52009-01-07 18:01:51 +080058 void __iomem *base = gpio_chip_base(chip);
59 uint32_t value, mask = 1 << offset;
60 unsigned long flags;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080061
Eric Miao0807da52009-01-07 18:01:51 +080062 spin_lock_irqsave(&gpio_lock, flags);
63
64 value = __raw_readl(base + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +080065 if (__gpio_is_inverted(chip->base + offset))
66 value |= mask;
67 else
68 value &= ~mask;
Eric Miao0807da52009-01-07 18:01:51 +080069 __raw_writel(value, base + GPDR_OFFSET);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080070
Eric Miao0807da52009-01-07 18:01:51 +080071 spin_unlock_irqrestore(&gpio_lock, flags);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080072 return 0;
73}
74
75static int pxa_gpio_direction_output(struct gpio_chip *chip,
Eric Miao0807da52009-01-07 18:01:51 +080076 unsigned offset, int value)
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080077{
Eric Miao0807da52009-01-07 18:01:51 +080078 void __iomem *base = gpio_chip_base(chip);
79 uint32_t tmp, mask = 1 << offset;
80 unsigned long flags;
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080081
Eric Miao0807da52009-01-07 18:01:51 +080082 __raw_writel(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET));
83
84 spin_lock_irqsave(&gpio_lock, flags);
85
86 tmp = __raw_readl(base + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +080087 if (__gpio_is_inverted(chip->base + offset))
88 tmp &= ~mask;
89 else
90 tmp |= mask;
Eric Miao0807da52009-01-07 18:01:51 +080091 __raw_writel(tmp, base + GPDR_OFFSET);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080092
Eric Miao0807da52009-01-07 18:01:51 +080093 spin_unlock_irqrestore(&gpio_lock, flags);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080094 return 0;
95}
96
Philipp Zabel1c44f5f2008-02-04 22:28:22 -080097static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
98{
Eric Miao0807da52009-01-07 18:01:51 +080099 return __raw_readl(gpio_chip_base(chip) + GPLR_OFFSET) & (1 << offset);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800100}
101
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800102static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
103{
Eric Miao0807da52009-01-07 18:01:51 +0800104 __raw_writel(1 << offset, gpio_chip_base(chip) +
105 (value ? GPSR_OFFSET : GPCR_OFFSET));
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800106}
107
Eric Miao0807da52009-01-07 18:01:51 +0800108static int __init pxa_init_gpio_chip(int gpio_end)
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800109{
Eric Miao0807da52009-01-07 18:01:51 +0800110 int i, gpio, nbanks = gpio_to_bank(gpio_end) + 1;
111 struct pxa_gpio_chip *chips;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800112
Daniel Mack4aa78262009-06-19 22:56:09 +0200113 chips = kzalloc(nbanks * sizeof(struct pxa_gpio_chip), GFP_KERNEL);
Eric Miao0807da52009-01-07 18:01:51 +0800114 if (chips == NULL) {
115 pr_err("%s: failed to allocate GPIO chips\n", __func__);
116 return -ENOMEM;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800117 }
Eric Miao0807da52009-01-07 18:01:51 +0800118
119 for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
120 struct gpio_chip *c = &chips[i].chip;
121
122 sprintf(chips[i].label, "gpio-%d", i);
123 chips[i].regbase = (void __iomem *)GPIO_BANK(i);
124
125 c->base = gpio;
126 c->label = chips[i].label;
127
128 c->direction_input = pxa_gpio_direction_input;
129 c->direction_output = pxa_gpio_direction_output;
130 c->get = pxa_gpio_get;
131 c->set = pxa_gpio_set;
132
133 /* number of GPIOs on last bank may be less than 32 */
134 c->ngpio = (gpio + 31 > gpio_end) ? (gpio_end - gpio + 1) : 32;
135 gpiochip_add(c);
136 }
137 pxa_gpio_chips = chips;
138 return 0;
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800139}
140
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800141/* Update only those GRERx and GFERx edge detection register bits if those
142 * bits are set in c->irq_mask
143 */
144static inline void update_edge_detect(struct pxa_gpio_chip *c)
145{
146 uint32_t grer, gfer;
147
148 grer = __raw_readl(c->regbase + GRER_OFFSET) & ~c->irq_mask;
149 gfer = __raw_readl(c->regbase + GFER_OFFSET) & ~c->irq_mask;
150 grer |= c->irq_edge_rise & c->irq_mask;
151 gfer |= c->irq_edge_fall & c->irq_mask;
152 __raw_writel(grer, c->regbase + GRER_OFFSET);
153 __raw_writel(gfer, c->regbase + GFER_OFFSET);
154}
155
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100156static int pxa_gpio_irq_type(struct irq_data *d, unsigned int type)
eric miaoe3630db2008-03-04 11:42:26 +0800157{
Eric Miao0807da52009-01-07 18:01:51 +0800158 struct pxa_gpio_chip *c;
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100159 int gpio = irq_to_gpio(d->irq);
Eric Miao0807da52009-01-07 18:01:51 +0800160 unsigned long gpdr, mask = GPIO_bit(gpio);
eric miaoe3630db2008-03-04 11:42:26 +0800161
Linus Walleija0656852011-06-13 10:42:19 +0200162 c = gpio_to_pxachip(gpio);
eric miaoe3630db2008-03-04 11:42:26 +0800163
164 if (type == IRQ_TYPE_PROBE) {
165 /* Don't mess with enabled GPIOs using preconfigured edges or
166 * GPIOs set to alternate function or to output during probe
167 */
Eric Miao0807da52009-01-07 18:01:51 +0800168 if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
eric miaoe3630db2008-03-04 11:42:26 +0800169 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800170
171 if (__gpio_is_occupied(gpio))
eric miaoe3630db2008-03-04 11:42:26 +0800172 return 0;
eric miao689c04a2008-03-04 17:18:38 +0800173
eric miaoe3630db2008-03-04 11:42:26 +0800174 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
175 }
176
Eric Miao0807da52009-01-07 18:01:51 +0800177 gpdr = __raw_readl(c->regbase + GPDR_OFFSET);
178
Eric Miao067455a2008-11-26 18:12:04 +0800179 if (__gpio_is_inverted(gpio))
Eric Miao0807da52009-01-07 18:01:51 +0800180 __raw_writel(gpdr | mask, c->regbase + GPDR_OFFSET);
Eric Miao067455a2008-11-26 18:12:04 +0800181 else
Eric Miao0807da52009-01-07 18:01:51 +0800182 __raw_writel(gpdr & ~mask, c->regbase + GPDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800183
184 if (type & IRQ_TYPE_EDGE_RISING)
Eric Miao0807da52009-01-07 18:01:51 +0800185 c->irq_edge_rise |= mask;
eric miaoe3630db2008-03-04 11:42:26 +0800186 else
Eric Miao0807da52009-01-07 18:01:51 +0800187 c->irq_edge_rise &= ~mask;
eric miaoe3630db2008-03-04 11:42:26 +0800188
189 if (type & IRQ_TYPE_EDGE_FALLING)
Eric Miao0807da52009-01-07 18:01:51 +0800190 c->irq_edge_fall |= mask;
eric miaoe3630db2008-03-04 11:42:26 +0800191 else
Eric Miao0807da52009-01-07 18:01:51 +0800192 c->irq_edge_fall &= ~mask;
eric miaoe3630db2008-03-04 11:42:26 +0800193
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800194 update_edge_detect(c);
eric miaoe3630db2008-03-04 11:42:26 +0800195
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100196 pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->irq, gpio,
eric miaoe3630db2008-03-04 11:42:26 +0800197 ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
198 ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
199 return 0;
200}
201
eric miaoe3630db2008-03-04 11:42:26 +0800202static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
203{
Eric Miao0807da52009-01-07 18:01:51 +0800204 struct pxa_gpio_chip *c;
205 int loop, gpio, gpio_base, n;
206 unsigned long gedr;
eric miaoe3630db2008-03-04 11:42:26 +0800207
208 do {
eric miaoe3630db2008-03-04 11:42:26 +0800209 loop = 0;
Eric Miao0807da52009-01-07 18:01:51 +0800210 for_each_gpio_chip(gpio, c) {
211 gpio_base = c->chip.base;
eric miaoe3630db2008-03-04 11:42:26 +0800212
Eric Miao0807da52009-01-07 18:01:51 +0800213 gedr = __raw_readl(c->regbase + GEDR_OFFSET);
214 gedr = gedr & c->irq_mask;
215 __raw_writel(gedr, c->regbase + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800216
Eric Miao0807da52009-01-07 18:01:51 +0800217 n = find_first_bit(&gedr, BITS_PER_LONG);
218 while (n < BITS_PER_LONG) {
219 loop = 1;
220
221 generic_handle_irq(gpio_to_irq(gpio_base + n));
222 n = find_next_bit(&gedr, BITS_PER_LONG, n + 1);
223 }
eric miaoe3630db2008-03-04 11:42:26 +0800224 }
225 } while (loop);
226}
227
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100228static void pxa_ack_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800229{
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100230 int gpio = irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200231 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800232
233 __raw_writel(GPIO_bit(gpio), c->regbase + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800234}
235
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100236static void pxa_mask_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800237{
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100238 int gpio = irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200239 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800240 uint32_t grer, gfer;
241
242 c->irq_mask &= ~GPIO_bit(gpio);
243
244 grer = __raw_readl(c->regbase + GRER_OFFSET) & ~GPIO_bit(gpio);
245 gfer = __raw_readl(c->regbase + GFER_OFFSET) & ~GPIO_bit(gpio);
246 __raw_writel(grer, c->regbase + GRER_OFFSET);
247 __raw_writel(gfer, c->regbase + GFER_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800248}
249
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100250static void pxa_unmask_muxed_gpio(struct irq_data *d)
eric miaoe3630db2008-03-04 11:42:26 +0800251{
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100252 int gpio = irq_to_gpio(d->irq);
Linus Walleija0656852011-06-13 10:42:19 +0200253 struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
Eric Miao0807da52009-01-07 18:01:51 +0800254
255 c->irq_mask |= GPIO_bit(gpio);
Eric Miaoa8f6fae2009-04-21 14:39:07 +0800256 update_edge_detect(c);
eric miaoe3630db2008-03-04 11:42:26 +0800257}
258
259static struct irq_chip pxa_muxed_gpio_chip = {
260 .name = "GPIO",
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100261 .irq_ack = pxa_ack_muxed_gpio,
262 .irq_mask = pxa_mask_muxed_gpio,
263 .irq_unmask = pxa_unmask_muxed_gpio,
264 .irq_set_type = pxa_gpio_irq_type,
eric miaoe3630db2008-03-04 11:42:26 +0800265};
266
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800267void __init pxa_init_gpio(int mux_irq, int start, int end, set_wake_t fn)
eric miaoe3630db2008-03-04 11:42:26 +0800268{
Eric Miao0807da52009-01-07 18:01:51 +0800269 struct pxa_gpio_chip *c;
270 int gpio, irq;
eric miaoe3630db2008-03-04 11:42:26 +0800271
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800272 pxa_last_gpio = end;
eric miaoe3630db2008-03-04 11:42:26 +0800273
Eric Miao0807da52009-01-07 18:01:51 +0800274 /* Initialize GPIO chips */
275 pxa_init_gpio_chip(end);
276
eric miaoe3630db2008-03-04 11:42:26 +0800277 /* clear all GPIO edge detects */
Eric Miao0807da52009-01-07 18:01:51 +0800278 for_each_gpio_chip(gpio, c) {
279 __raw_writel(0, c->regbase + GFER_OFFSET);
280 __raw_writel(0, c->regbase + GRER_OFFSET);
281 __raw_writel(~0,c->regbase + GEDR_OFFSET);
eric miaoe3630db2008-03-04 11:42:26 +0800282 }
283
Eric Miaoa58fbcd2009-01-06 17:37:37 +0800284 for (irq = gpio_to_irq(start); irq <= gpio_to_irq(end); irq++) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100285 irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
286 handle_edge_irq);
eric miaoe3630db2008-03-04 11:42:26 +0800287 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
288 }
289
290 /* Install handler for GPIO>=2 edge detect interrupts */
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100291 irq_set_chained_handler(mux_irq, pxa_gpio_demux_handler);
Lennert Buytenheka3f4c922010-11-29 11:18:26 +0100292 pxa_muxed_gpio_chip.irq_set_wake = fn;
eric miaoe3630db2008-03-04 11:42:26 +0800293}
eric miao663707c2008-03-04 16:13:58 +0800294
295#ifdef CONFIG_PM
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200296static int pxa_gpio_suspend(void)
eric miao663707c2008-03-04 16:13:58 +0800297{
Eric Miao0807da52009-01-07 18:01:51 +0800298 struct pxa_gpio_chip *c;
299 int gpio;
eric miao663707c2008-03-04 16:13:58 +0800300
Eric Miao0807da52009-01-07 18:01:51 +0800301 for_each_gpio_chip(gpio, c) {
302 c->saved_gplr = __raw_readl(c->regbase + GPLR_OFFSET);
303 c->saved_gpdr = __raw_readl(c->regbase + GPDR_OFFSET);
304 c->saved_grer = __raw_readl(c->regbase + GRER_OFFSET);
305 c->saved_gfer = __raw_readl(c->regbase + GFER_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800306
307 /* Clear GPIO transition detect bits */
Eric Miao0807da52009-01-07 18:01:51 +0800308 __raw_writel(0xffffffff, c->regbase + GEDR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800309 }
310 return 0;
311}
312
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200313static void pxa_gpio_resume(void)
eric miao663707c2008-03-04 16:13:58 +0800314{
Eric Miao0807da52009-01-07 18:01:51 +0800315 struct pxa_gpio_chip *c;
316 int gpio;
eric miao663707c2008-03-04 16:13:58 +0800317
Eric Miao0807da52009-01-07 18:01:51 +0800318 for_each_gpio_chip(gpio, c) {
eric miao663707c2008-03-04 16:13:58 +0800319 /* restore level with set/clear */
Eric Miao0807da52009-01-07 18:01:51 +0800320 __raw_writel( c->saved_gplr, c->regbase + GPSR_OFFSET);
321 __raw_writel(~c->saved_gplr, c->regbase + GPCR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800322
Eric Miao0807da52009-01-07 18:01:51 +0800323 __raw_writel(c->saved_grer, c->regbase + GRER_OFFSET);
324 __raw_writel(c->saved_gfer, c->regbase + GFER_OFFSET);
325 __raw_writel(c->saved_gpdr, c->regbase + GPDR_OFFSET);
eric miao663707c2008-03-04 16:13:58 +0800326 }
eric miao663707c2008-03-04 16:13:58 +0800327}
328#else
329#define pxa_gpio_suspend NULL
330#define pxa_gpio_resume NULL
331#endif
332
Rafael J. Wysocki2eaa03b2011-04-22 22:03:11 +0200333struct syscore_ops pxa_gpio_syscore_ops = {
eric miao663707c2008-03-04 16:13:58 +0800334 .suspend = pxa_gpio_suspend,
335 .resume = pxa_gpio_resume,
336};