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