aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-pxa/gpio.c
diff options
context:
space:
mode:
authorEric Miao <eric.miao@marvell.com>2009-01-19 23:09:06 -0500
committerEric Miao <eric.miao@marvell.com>2009-03-22 22:11:33 -0400
commit38f539a608c9a3b40b30f1892bd5f9a38f4e5ffe (patch)
treefeb9c3ee23fe75151f73e8916c1afeb7c562e0dd /arch/arm/mach-pxa/gpio.c
parentbd5ce4332328c1fe473690a86b2e6a4157be038f (diff)
[ARM] pxa: move common GPIO handling code into plat-pxa
1. add common GPIO handling code into [arch/arm/plat-pxa] 2. common code in <mach/gpio.h> moved into <plat/gpio.h>, new processors should implement its own <mach/gpio.h>, provide the following required definitions and '#include <plat/gpio.h>' in the end: - GPIO_REGS_VIRT for mapped virtual address of the GPIO registers' physical I/O memory - macros of GPLR(), GPSR(), GPDR() for constant optimization for functions gpio_{set,get}_value() (so that bit-bang code can still have tolerable performance) - NR_BUILTIN_GPIO for the number of onchip GPIO - definitions of __gpio_is_inverted() and __gpio_is_occupied(), they can be either macros or inlined functions Signed-off-by: Eric Miao <eric.miao@marvell.com>
Diffstat (limited to 'arch/arm/mach-pxa/gpio.c')
-rw-r--r--arch/arm/mach-pxa/gpio.c365
1 files changed, 0 insertions, 365 deletions
diff --git a/arch/arm/mach-pxa/gpio.c b/arch/arm/mach-pxa/gpio.c
deleted file mode 100644
index 7c2267036bf1..000000000000
--- a/arch/arm/mach-pxa/gpio.c
+++ /dev/null
@@ -1,365 +0,0 @@
1/*
2 * linux/arch/arm/mach-pxa/gpio.c
3 *
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 */
14
15#include <linux/init.h>
16#include <linux/irq.h>
17#include <linux/io.h>
18#include <linux/sysdev.h>
19#include <linux/bootmem.h>
20
21#include <mach/gpio.h>
22
23int pxa_last_gpio;
24
25/*
26 * We handle the GPIOs by banks, each bank covers up to 32 GPIOs with
27 * one set of registers. The register offsets are organized below:
28 *
29 * GPLR GPDR GPSR GPCR GRER GFER GEDR
30 * BANK 0 - 0x0000 0x000C 0x0018 0x0024 0x0030 0x003C 0x0048
31 * BANK 1 - 0x0004 0x0010 0x001C 0x0028 0x0034 0x0040 0x004C
32 * BANK 2 - 0x0008 0x0014 0x0020 0x002C 0x0038 0x0044 0x0050
33 *
34 * BANK 3 - 0x0100 0x010C 0x0118 0x0124 0x0130 0x013C 0x0148
35 * BANK 4 - 0x0104 0x0110 0x011C 0x0128 0x0134 0x0140 0x014C
36 * BANK 5 - 0x0108 0x0114 0x0120 0x012C 0x0138 0x0144 0x0150
37 *
38 * NOTE:
39 * BANK 3 is only available on PXA27x and later processors.
40 * BANK 4 and 5 are only available on PXA935
41 */
42
43#define GPIO_BANK(n) (GPIO_REGS_VIRT + BANK_OFF(n))
44
45#define GPLR_OFFSET 0x00
46#define GPDR_OFFSET 0x0C
47#define GPSR_OFFSET 0x18
48#define GPCR_OFFSET 0x24
49#define GRER_OFFSET 0x30
50#define GFER_OFFSET 0x3C
51#define GEDR_OFFSET 0x48
52
53struct pxa_gpio_chip {
54 struct gpio_chip chip;
55 void __iomem *regbase;
56 char label[10];
57
58 unsigned long irq_mask;
59 unsigned long irq_edge_rise;
60 unsigned long irq_edge_fall;
61
62#ifdef CONFIG_PM
63 unsigned long saved_gplr;
64 unsigned long saved_gpdr;
65 unsigned long saved_grer;
66 unsigned long saved_gfer;
67#endif
68};
69
70static DEFINE_SPINLOCK(gpio_lock);
71static struct pxa_gpio_chip *pxa_gpio_chips;
72
73#define for_each_gpio_chip(i, c) \
74 for (i = 0, c = &pxa_gpio_chips[0]; i <= pxa_last_gpio; i += 32, c++)
75
76static inline void __iomem *gpio_chip_base(struct gpio_chip *c)
77{
78 return container_of(c, struct pxa_gpio_chip, chip)->regbase;
79}
80
81static inline struct pxa_gpio_chip *gpio_to_chip(unsigned gpio)
82{
83 return &pxa_gpio_chips[gpio_to_bank(gpio)];
84}
85
86static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
87{
88 void __iomem *base = gpio_chip_base(chip);
89 uint32_t value, mask = 1 << offset;
90 unsigned long flags;
91
92 spin_lock_irqsave(&gpio_lock, flags);
93
94 value = __raw_readl(base + GPDR_OFFSET);
95 if (__gpio_is_inverted(chip->base + offset))
96 value |= mask;
97 else
98 value &= ~mask;
99 __raw_writel(value, base + GPDR_OFFSET);
100
101 spin_unlock_irqrestore(&gpio_lock, flags);
102 return 0;
103}
104
105static int pxa_gpio_direction_output(struct gpio_chip *chip,
106 unsigned offset, int value)
107{
108 void __iomem *base = gpio_chip_base(chip);
109 uint32_t tmp, mask = 1 << offset;
110 unsigned long flags;
111
112 __raw_writel(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET));
113
114 spin_lock_irqsave(&gpio_lock, flags);
115
116 tmp = __raw_readl(base + GPDR_OFFSET);
117 if (__gpio_is_inverted(chip->base + offset))
118 tmp &= ~mask;
119 else
120 tmp |= mask;
121 __raw_writel(tmp, base + GPDR_OFFSET);
122
123 spin_unlock_irqrestore(&gpio_lock, flags);
124 return 0;
125}
126
127static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
128{
129 return __raw_readl(gpio_chip_base(chip) + GPLR_OFFSET) & (1 << offset);
130}
131
132static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
133{
134 __raw_writel(1 << offset, gpio_chip_base(chip) +
135 (value ? GPSR_OFFSET : GPCR_OFFSET));
136}
137
138static int __init pxa_init_gpio_chip(int gpio_end)
139{
140 int i, gpio, nbanks = gpio_to_bank(gpio_end) + 1;
141 struct pxa_gpio_chip *chips;
142
143 /* this is early, we have to use bootmem allocator, and we really
144 * want this to be allocated dynamically for different 'gpio_end'
145 */
146 chips = alloc_bootmem_low(nbanks * sizeof(struct pxa_gpio_chip));
147 if (chips == NULL) {
148 pr_err("%s: failed to allocate GPIO chips\n", __func__);
149 return -ENOMEM;
150 }
151
152 for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
153 struct gpio_chip *c = &chips[i].chip;
154
155 sprintf(chips[i].label, "gpio-%d", i);
156 chips[i].regbase = (void __iomem *)GPIO_BANK(i);
157
158 c->base = gpio;
159 c->label = chips[i].label;
160
161 c->direction_input = pxa_gpio_direction_input;
162 c->direction_output = pxa_gpio_direction_output;
163 c->get = pxa_gpio_get;
164 c->set = pxa_gpio_set;
165
166 /* number of GPIOs on last bank may be less than 32 */
167 c->ngpio = (gpio + 31 > gpio_end) ? (gpio_end - gpio + 1) : 32;
168 gpiochip_add(c);
169 }
170 pxa_gpio_chips = chips;
171 return 0;
172}
173
174static int pxa_gpio_irq_type(unsigned int irq, unsigned int type)
175{
176 struct pxa_gpio_chip *c;
177 int gpio = irq_to_gpio(irq);
178 unsigned long gpdr, mask = GPIO_bit(gpio);
179
180 c = gpio_to_chip(gpio);
181
182 if (type == IRQ_TYPE_PROBE) {
183 /* Don't mess with enabled GPIOs using preconfigured edges or
184 * GPIOs set to alternate function or to output during probe
185 */
186 if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
187 return 0;
188
189 if (__gpio_is_occupied(gpio))
190 return 0;
191
192 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
193 }
194
195 gpdr = __raw_readl(c->regbase + GPDR_OFFSET);
196
197 if (__gpio_is_inverted(gpio))
198 __raw_writel(gpdr | mask, c->regbase + GPDR_OFFSET);
199 else
200 __raw_writel(gpdr & ~mask, c->regbase + GPDR_OFFSET);
201
202 if (type & IRQ_TYPE_EDGE_RISING)
203 c->irq_edge_rise |= mask;
204 else
205 c->irq_edge_rise &= ~mask;
206
207 if (type & IRQ_TYPE_EDGE_FALLING)
208 c->irq_edge_fall |= mask;
209 else
210 c->irq_edge_fall &= ~mask;
211
212 __raw_writel(c->irq_edge_rise & c->irq_mask, c->regbase + GRER_OFFSET);
213 __raw_writel(c->irq_edge_fall & c->irq_mask, c->regbase + GFER_OFFSET);
214
215 pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, irq, gpio,
216 ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
217 ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
218 return 0;
219}
220
221static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
222{
223 struct pxa_gpio_chip *c;
224 int loop, gpio, gpio_base, n;
225 unsigned long gedr;
226
227 do {
228 loop = 0;
229 for_each_gpio_chip(gpio, c) {
230 gpio_base = c->chip.base;
231
232 gedr = __raw_readl(c->regbase + GEDR_OFFSET);
233 gedr = gedr & c->irq_mask;
234 __raw_writel(gedr, c->regbase + GEDR_OFFSET);
235
236 n = find_first_bit(&gedr, BITS_PER_LONG);
237 while (n < BITS_PER_LONG) {
238 loop = 1;
239
240 generic_handle_irq(gpio_to_irq(gpio_base + n));
241 n = find_next_bit(&gedr, BITS_PER_LONG, n + 1);
242 }
243 }
244 } while (loop);
245}
246
247static void pxa_ack_muxed_gpio(unsigned int irq)
248{
249 int gpio = irq_to_gpio(irq);
250 struct pxa_gpio_chip *c = gpio_to_chip(gpio);
251
252 __raw_writel(GPIO_bit(gpio), c->regbase + GEDR_OFFSET);
253}
254
255static void pxa_mask_muxed_gpio(unsigned int irq)
256{
257 int gpio = irq_to_gpio(irq);
258 struct pxa_gpio_chip *c = gpio_to_chip(gpio);
259 uint32_t grer, gfer;
260
261 c->irq_mask &= ~GPIO_bit(gpio);
262
263 grer = __raw_readl(c->regbase + GRER_OFFSET) & ~GPIO_bit(gpio);
264 gfer = __raw_readl(c->regbase + GFER_OFFSET) & ~GPIO_bit(gpio);
265 __raw_writel(grer, c->regbase + GRER_OFFSET);
266 __raw_writel(gfer, c->regbase + GFER_OFFSET);
267}
268
269static void pxa_unmask_muxed_gpio(unsigned int irq)
270{
271 int gpio = irq_to_gpio(irq);
272 struct pxa_gpio_chip *c = gpio_to_chip(gpio);
273
274 c->irq_mask |= GPIO_bit(gpio);
275 __raw_writel(c->irq_edge_rise & c->irq_mask, c->regbase + GRER_OFFSET);
276 __raw_writel(c->irq_edge_fall & c->irq_mask, c->regbase + GFER_OFFSET);
277}
278
279static struct irq_chip pxa_muxed_gpio_chip = {
280 .name = "GPIO",
281 .ack = pxa_ack_muxed_gpio,
282 .mask = pxa_mask_muxed_gpio,
283 .unmask = pxa_unmask_muxed_gpio,
284 .set_type = pxa_gpio_irq_type,
285};
286
287void __init pxa_init_gpio(int mux_irq, int start, int end, set_wake_t fn)
288{
289 struct pxa_gpio_chip *c;
290 int gpio, irq;
291
292 pxa_last_gpio = end;
293
294 /* Initialize GPIO chips */
295 pxa_init_gpio_chip(end);
296
297 /* clear all GPIO edge detects */
298 for_each_gpio_chip(gpio, c) {
299 __raw_writel(0, c->regbase + GFER_OFFSET);
300 __raw_writel(0, c->regbase + GRER_OFFSET);
301 __raw_writel(~0,c->regbase + GEDR_OFFSET);
302 }
303
304 for (irq = gpio_to_irq(start); irq <= gpio_to_irq(end); irq++) {
305 set_irq_chip(irq, &pxa_muxed_gpio_chip);
306 set_irq_handler(irq, handle_edge_irq);
307 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
308 }
309
310 /* Install handler for GPIO>=2 edge detect interrupts */
311 set_irq_chained_handler(mux_irq, pxa_gpio_demux_handler);
312 pxa_muxed_gpio_chip.set_wake = fn;
313}
314
315#ifdef CONFIG_PM
316static int pxa_gpio_suspend(struct sys_device *dev, pm_message_t state)
317{
318 struct pxa_gpio_chip *c;
319 int gpio;
320
321 for_each_gpio_chip(gpio, c) {
322 c->saved_gplr = __raw_readl(c->regbase + GPLR_OFFSET);
323 c->saved_gpdr = __raw_readl(c->regbase + GPDR_OFFSET);
324 c->saved_grer = __raw_readl(c->regbase + GRER_OFFSET);
325 c->saved_gfer = __raw_readl(c->regbase + GFER_OFFSET);
326
327 /* Clear GPIO transition detect bits */
328 __raw_writel(0xffffffff, c->regbase + GEDR_OFFSET);
329 }
330 return 0;
331}
332
333static int pxa_gpio_resume(struct sys_device *dev)
334{
335 struct pxa_gpio_chip *c;
336 int gpio;
337
338 for_each_gpio_chip(gpio, c) {
339 /* restore level with set/clear */
340 __raw_writel( c->saved_gplr, c->regbase + GPSR_OFFSET);
341 __raw_writel(~c->saved_gplr, c->regbase + GPCR_OFFSET);
342
343 __raw_writel(c->saved_grer, c->regbase + GRER_OFFSET);
344 __raw_writel(c->saved_gfer, c->regbase + GFER_OFFSET);
345 __raw_writel(c->saved_gpdr, c->regbase + GPDR_OFFSET);
346 }
347 return 0;
348}
349#else
350#define pxa_gpio_suspend NULL
351#define pxa_gpio_resume NULL
352#endif
353
354struct sysdev_class pxa_gpio_sysclass = {
355 .name = "gpio",
356 .suspend = pxa_gpio_suspend,
357 .resume = pxa_gpio_resume,
358};
359
360static int __init pxa_gpio_init(void)
361{
362 return sysdev_class_register(&pxa_gpio_sysclass);
363}
364
365core_initcall(pxa_gpio_init);