[xburst] Add 2.6.37 support
[openwrt.git] / target / linux / xburst / patches-2.6.35 / 009-gpio.patch
1 From 9b2439903f4015233930ddab5b99b34c622c4876 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 17 Jul 2010 11:11:19 +0000
4 Subject: [PATCH] MIPS: JZ4740: Add GPIO support
5
6 Add gpiolib support for JZ4740 SoCs.
7
8 Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
9 Cc: linux-mips@linux-mips.org
10 Cc: linux-kernel@vger.kernel.org
11 Patchwork: https://patchwork.linux-mips.org/patch/1467/
12 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
13 ---
14  arch/mips/include/asm/mach-jz4740/gpio.h |  398 ++++++++++++++++++++
15  arch/mips/jz4740/gpio.c                  |  604 ++++++++++++++++++++++++++++++
16  2 files changed, 1002 insertions(+), 0 deletions(-)
17  create mode 100644 arch/mips/include/asm/mach-jz4740/gpio.h
18  create mode 100644 arch/mips/jz4740/gpio.c
19
20 --- /dev/null
21 +++ b/arch/mips/include/asm/mach-jz4740/gpio.h
22 @@ -0,0 +1,398 @@
23 +/*
24 + *  Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
25 + *  JZ4740 GPIO pin definitions
26 + *
27 + *  This program is free software; you can redistribute it and/or modify it
28 + *  under  the terms of the GNU General Public License as published by the
29 + *  Free Software Foundation;  either version 2 of the License, or (at your
30 + *  option) any later version.
31 + *
32 + *  You should have received a copy of the GNU General Public License along
33 + *  with this program; if not, write to the Free Software Foundation, Inc.,
34 + *  675 Mass Ave, Cambridge, MA 02139, USA.
35 + *
36 + */
37 +
38 +#ifndef _JZ_GPIO_H
39 +#define _JZ_GPIO_H
40 +
41 +#include <linux/types.h>
42 +
43 +enum jz_gpio_function {
44 +    JZ_GPIO_FUNC_NONE,
45 +    JZ_GPIO_FUNC1,
46 +    JZ_GPIO_FUNC2,
47 +    JZ_GPIO_FUNC3,
48 +};
49 +
50 +
51 +/*
52 + Usually a driver for a SoC component has to request several gpio pins and
53 + configure them as funcion pins.
54 + jz_gpio_bulk_request can be used to ease this process.
55 + Usually one would do something like:
56 +
57 + const static struct jz_gpio_bulk_request i2c_pins[] = {
58 +       JZ_GPIO_BULK_PIN(I2C_SDA),
59 +       JZ_GPIO_BULK_PIN(I2C_SCK),
60 + };
61 +
62 + inside the probe function:
63 +
64 +    ret = jz_gpio_bulk_request(i2c_pins, ARRAY_SIZE(i2c_pins));
65 +    if (ret) {
66 +       ...
67 +
68 + inside the remove function:
69 +
70 +    jz_gpio_bulk_free(i2c_pins, ARRAY_SIZE(i2c_pins));
71 +
72 +
73 +*/
74 +struct jz_gpio_bulk_request {
75 +       int gpio;
76 +       const char *name;
77 +       enum jz_gpio_function function;
78 +};
79 +
80 +#define JZ_GPIO_BULK_PIN(pin) { \
81 +    .gpio = JZ_GPIO_ ## pin, \
82 +    .name = #pin, \
83 +    .function = JZ_GPIO_FUNC_ ## pin \
84 +}
85 +
86 +int jz_gpio_bulk_request(const struct jz_gpio_bulk_request *request, size_t num);
87 +void jz_gpio_bulk_free(const struct jz_gpio_bulk_request *request, size_t num);
88 +void jz_gpio_bulk_suspend(const struct jz_gpio_bulk_request *request, size_t num);
89 +void jz_gpio_bulk_resume(const struct jz_gpio_bulk_request *request, size_t num);
90 +void jz_gpio_enable_pullup(unsigned gpio);
91 +void jz_gpio_disable_pullup(unsigned gpio);
92 +int jz_gpio_set_function(int gpio, enum jz_gpio_function function);
93 +
94 +int jz_gpio_port_direction_input(int port, uint32_t mask);
95 +int jz_gpio_port_direction_output(int port, uint32_t mask);
96 +void jz_gpio_port_set_value(int port, uint32_t value, uint32_t mask);
97 +uint32_t jz_gpio_port_get_value(int port, uint32_t mask);
98 +
99 +#include <asm/mach-generic/gpio.h>
100 +
101 +#define JZ_GPIO_PORTA(x) ((x) + 32 * 0)
102 +#define JZ_GPIO_PORTB(x) ((x) + 32 * 1)
103 +#define JZ_GPIO_PORTC(x) ((x) + 32 * 2)
104 +#define JZ_GPIO_PORTD(x) ((x) + 32 * 3)
105 +
106 +/* Port A function pins */
107 +#define JZ_GPIO_MEM_DATA0              JZ_GPIO_PORTA(0)
108 +#define JZ_GPIO_MEM_DATA1              JZ_GPIO_PORTA(1)
109 +#define JZ_GPIO_MEM_DATA2              JZ_GPIO_PORTA(2)
110 +#define JZ_GPIO_MEM_DATA3              JZ_GPIO_PORTA(3)
111 +#define JZ_GPIO_MEM_DATA4              JZ_GPIO_PORTA(4)
112 +#define JZ_GPIO_MEM_DATA5              JZ_GPIO_PORTA(5)
113 +#define JZ_GPIO_MEM_DATA6              JZ_GPIO_PORTA(6)
114 +#define JZ_GPIO_MEM_DATA7              JZ_GPIO_PORTA(7)
115 +#define JZ_GPIO_MEM_DATA8              JZ_GPIO_PORTA(8)
116 +#define JZ_GPIO_MEM_DATA9              JZ_GPIO_PORTA(9)
117 +#define JZ_GPIO_MEM_DATA10             JZ_GPIO_PORTA(10)
118 +#define JZ_GPIO_MEM_DATA11             JZ_GPIO_PORTA(11)
119 +#define JZ_GPIO_MEM_DATA12             JZ_GPIO_PORTA(12)
120 +#define JZ_GPIO_MEM_DATA13             JZ_GPIO_PORTA(13)
121 +#define JZ_GPIO_MEM_DATA14             JZ_GPIO_PORTA(14)
122 +#define JZ_GPIO_MEM_DATA15             JZ_GPIO_PORTA(15)
123 +#define JZ_GPIO_MEM_DATA16             JZ_GPIO_PORTA(16)
124 +#define JZ_GPIO_MEM_DATA17             JZ_GPIO_PORTA(17)
125 +#define JZ_GPIO_MEM_DATA18             JZ_GPIO_PORTA(18)
126 +#define JZ_GPIO_MEM_DATA19             JZ_GPIO_PORTA(19)
127 +#define JZ_GPIO_MEM_DATA20             JZ_GPIO_PORTA(20)
128 +#define JZ_GPIO_MEM_DATA21             JZ_GPIO_PORTA(21)
129 +#define JZ_GPIO_MEM_DATA22             JZ_GPIO_PORTA(22)
130 +#define JZ_GPIO_MEM_DATA23             JZ_GPIO_PORTA(23)
131 +#define JZ_GPIO_MEM_DATA24             JZ_GPIO_PORTA(24)
132 +#define JZ_GPIO_MEM_DATA25             JZ_GPIO_PORTA(25)
133 +#define JZ_GPIO_MEM_DATA26             JZ_GPIO_PORTA(26)
134 +#define JZ_GPIO_MEM_DATA27             JZ_GPIO_PORTA(27)
135 +#define JZ_GPIO_MEM_DATA28             JZ_GPIO_PORTA(28)
136 +#define JZ_GPIO_MEM_DATA29             JZ_GPIO_PORTA(29)
137 +#define JZ_GPIO_MEM_DATA30             JZ_GPIO_PORTA(30)
138 +#define JZ_GPIO_MEM_DATA31             JZ_GPIO_PORTA(31)
139 +
140 +#define JZ_GPIO_FUNC_MEM_DATA0         JZ_GPIO_FUNC1
141 +#define JZ_GPIO_FUNC_MEM_DATA1         JZ_GPIO_FUNC1
142 +#define JZ_GPIO_FUNC_MEM_DATA2         JZ_GPIO_FUNC1
143 +#define JZ_GPIO_FUNC_MEM_DATA3         JZ_GPIO_FUNC1
144 +#define JZ_GPIO_FUNC_MEM_DATA4         JZ_GPIO_FUNC1
145 +#define JZ_GPIO_FUNC_MEM_DATA5         JZ_GPIO_FUNC1
146 +#define JZ_GPIO_FUNC_MEM_DATA6         JZ_GPIO_FUNC1
147 +#define JZ_GPIO_FUNC_MEM_DATA7         JZ_GPIO_FUNC1
148 +#define JZ_GPIO_FUNC_MEM_DATA8         JZ_GPIO_FUNC1
149 +#define JZ_GPIO_FUNC_MEM_DATA9         JZ_GPIO_FUNC1
150 +#define JZ_GPIO_FUNC_MEM_DATA10                JZ_GPIO_FUNC1
151 +#define JZ_GPIO_FUNC_MEM_DATA11                JZ_GPIO_FUNC1
152 +#define JZ_GPIO_FUNC_MEM_DATA12                JZ_GPIO_FUNC1
153 +#define JZ_GPIO_FUNC_MEM_DATA13                JZ_GPIO_FUNC1
154 +#define JZ_GPIO_FUNC_MEM_DATA14                JZ_GPIO_FUNC1
155 +#define JZ_GPIO_FUNC_MEM_DATA15                JZ_GPIO_FUNC1
156 +#define JZ_GPIO_FUNC_MEM_DATA16                JZ_GPIO_FUNC1
157 +#define JZ_GPIO_FUNC_MEM_DATA17                JZ_GPIO_FUNC1
158 +#define JZ_GPIO_FUNC_MEM_DATA18                JZ_GPIO_FUNC1
159 +#define JZ_GPIO_FUNC_MEM_DATA19                JZ_GPIO_FUNC1
160 +#define JZ_GPIO_FUNC_MEM_DATA20                JZ_GPIO_FUNC1
161 +#define JZ_GPIO_FUNC_MEM_DATA21                JZ_GPIO_FUNC1
162 +#define JZ_GPIO_FUNC_MEM_DATA22                JZ_GPIO_FUNC1
163 +#define JZ_GPIO_FUNC_MEM_DATA23                JZ_GPIO_FUNC1
164 +#define JZ_GPIO_FUNC_MEM_DATA24                JZ_GPIO_FUNC1
165 +#define JZ_GPIO_FUNC_MEM_DATA25                JZ_GPIO_FUNC1
166 +#define JZ_GPIO_FUNC_MEM_DATA26                JZ_GPIO_FUNC1
167 +#define JZ_GPIO_FUNC_MEM_DATA27                JZ_GPIO_FUNC1
168 +#define JZ_GPIO_FUNC_MEM_DATA28                JZ_GPIO_FUNC1
169 +#define JZ_GPIO_FUNC_MEM_DATA29                JZ_GPIO_FUNC1
170 +#define JZ_GPIO_FUNC_MEM_DATA30                JZ_GPIO_FUNC1
171 +#define JZ_GPIO_FUNC_MEM_DATA31                JZ_GPIO_FUNC1
172 +
173 +/* Port B function pins */
174 +#define JZ_GPIO_MEM_ADDR0              JZ_GPIO_PORTB(0)
175 +#define JZ_GPIO_MEM_ADDR1              JZ_GPIO_PORTB(1)
176 +#define JZ_GPIO_MEM_ADDR2              JZ_GPIO_PORTB(2)
177 +#define JZ_GPIO_MEM_ADDR3              JZ_GPIO_PORTB(3)
178 +#define JZ_GPIO_MEM_ADDR4              JZ_GPIO_PORTB(4)
179 +#define JZ_GPIO_MEM_ADDR5              JZ_GPIO_PORTB(5)
180 +#define JZ_GPIO_MEM_ADDR6              JZ_GPIO_PORTB(6)
181 +#define JZ_GPIO_MEM_ADDR7              JZ_GPIO_PORTB(7)
182 +#define JZ_GPIO_MEM_ADDR8              JZ_GPIO_PORTB(8)
183 +#define JZ_GPIO_MEM_ADDR9              JZ_GPIO_PORTB(9)
184 +#define JZ_GPIO_MEM_ADDR10             JZ_GPIO_PORTB(10)
185 +#define JZ_GPIO_MEM_ADDR11             JZ_GPIO_PORTB(11)
186 +#define JZ_GPIO_MEM_ADDR12             JZ_GPIO_PORTB(12)
187 +#define JZ_GPIO_MEM_ADDR13             JZ_GPIO_PORTB(13)
188 +#define JZ_GPIO_MEM_ADDR14             JZ_GPIO_PORTB(14)
189 +#define JZ_GPIO_MEM_ADDR15             JZ_GPIO_PORTB(15)
190 +#define JZ_GPIO_MEM_ADDR16             JZ_GPIO_PORTB(16)
191 +#define JZ_GPIO_LCD_CLS                        JZ_GPIO_PORTB(17)
192 +#define JZ_GPIO_LCD_SPL                        JZ_GPIO_PORTB(18)
193 +#define JZ_GPIO_MEM_DCS                        JZ_GPIO_PORTB(19)
194 +#define JZ_GPIO_MEM_RAS                        JZ_GPIO_PORTB(20)
195 +#define JZ_GPIO_MEM_CAS                        JZ_GPIO_PORTB(21)
196 +#define JZ_GPIO_MEM_SDWE               JZ_GPIO_PORTB(22)
197 +#define JZ_GPIO_MEM_CKE                        JZ_GPIO_PORTB(23)
198 +#define JZ_GPIO_MEM_CKO                        JZ_GPIO_PORTB(24)
199 +#define JZ_GPIO_MEM_CS0                        JZ_GPIO_PORTB(25)
200 +#define JZ_GPIO_MEM_CS1                        JZ_GPIO_PORTB(26)
201 +#define JZ_GPIO_MEM_CS2                        JZ_GPIO_PORTB(27)
202 +#define JZ_GPIO_MEM_CS3                        JZ_GPIO_PORTB(28)
203 +#define JZ_GPIO_MEM_RD                 JZ_GPIO_PORTB(29)
204 +#define JZ_GPIO_MEM_WR                 JZ_GPIO_PORTB(30)
205 +#define JZ_GPIO_MEM_WE0                        JZ_GPIO_PORTB(31)
206 +
207 +#define JZ_GPIO_FUNC_MEM_ADDR0         JZ_GPIO_FUNC1
208 +#define JZ_GPIO_FUNC_MEM_ADDR1         JZ_GPIO_FUNC1
209 +#define JZ_GPIO_FUNC_MEM_ADDR2         JZ_GPIO_FUNC1
210 +#define JZ_GPIO_FUNC_MEM_ADDR3         JZ_GPIO_FUNC1
211 +#define JZ_GPIO_FUNC_MEM_ADDR4         JZ_GPIO_FUNC1
212 +#define JZ_GPIO_FUNC_MEM_ADDR5         JZ_GPIO_FUNC1
213 +#define JZ_GPIO_FUNC_MEM_ADDR6         JZ_GPIO_FUNC1
214 +#define JZ_GPIO_FUNC_MEM_ADDR7         JZ_GPIO_FUNC1
215 +#define JZ_GPIO_FUNC_MEM_ADDR8         JZ_GPIO_FUNC1
216 +#define JZ_GPIO_FUNC_MEM_ADDR9         JZ_GPIO_FUNC1
217 +#define JZ_GPIO_FUNC_MEM_ADDR10                JZ_GPIO_FUNC1
218 +#define JZ_GPIO_FUNC_MEM_ADDR11                JZ_GPIO_FUNC1
219 +#define JZ_GPIO_FUNC_MEM_ADDR12                JZ_GPIO_FUNC1
220 +#define JZ_GPIO_FUNC_MEM_ADDR13                JZ_GPIO_FUNC1
221 +#define JZ_GPIO_FUNC_MEM_ADDR14                JZ_GPIO_FUNC1
222 +#define JZ_GPIO_FUNC_MEM_ADDR15                JZ_GPIO_FUNC1
223 +#define JZ_GPIO_FUNC_MEM_ADDR16                JZ_GPIO_FUNC1
224 +#define JZ_GPIO_FUNC_LCD_CLS           JZ_GPIO_FUNC1
225 +#define JZ_GPIO_FUNC_LCD_SPL           JZ_GPIO_FUNC1
226 +#define JZ_GPIO_FUNC_MEM_DCS           JZ_GPIO_FUNC1
227 +#define JZ_GPIO_FUNC_MEM_RAS           JZ_GPIO_FUNC1
228 +#define JZ_GPIO_FUNC_MEM_CAS           JZ_GPIO_FUNC1
229 +#define JZ_GPIO_FUNC_MEM_SDWE          JZ_GPIO_FUNC1
230 +#define JZ_GPIO_FUNC_MEM_CKE           JZ_GPIO_FUNC1
231 +#define JZ_GPIO_FUNC_MEM_CKO           JZ_GPIO_FUNC1
232 +#define JZ_GPIO_FUNC_MEM_CS0           JZ_GPIO_FUNC1
233 +#define JZ_GPIO_FUNC_MEM_CS1           JZ_GPIO_FUNC1
234 +#define JZ_GPIO_FUNC_MEM_CS2           JZ_GPIO_FUNC1
235 +#define JZ_GPIO_FUNC_MEM_CS3           JZ_GPIO_FUNC1
236 +#define JZ_GPIO_FUNC_MEM_RD            JZ_GPIO_FUNC1
237 +#define JZ_GPIO_FUNC_MEM_WR            JZ_GPIO_FUNC1
238 +#define JZ_GPIO_FUNC_MEM_WE0           JZ_GPIO_FUNC1
239 +
240 +
241 +#define JZ_GPIO_MEM_ADDR21             JZ_GPIO_PORTB(17)
242 +#define JZ_GPIO_MEM_ADDR22             JZ_GPIO_PORTB(18)
243 +
244 +#define JZ_GPIO_FUNC_MEM_ADDR21                JZ_GPIO_FUNC2
245 +#define JZ_GPIO_FUNC_MEM_ADDR22                JZ_GPIO_FUNC2
246 +
247 +/* Port C function pins */
248 +#define JZ_GPIO_LCD_DATA0              JZ_GPIO_PORTC(0)
249 +#define JZ_GPIO_LCD_DATA1              JZ_GPIO_PORTC(1)
250 +#define JZ_GPIO_LCD_DATA2              JZ_GPIO_PORTC(2)
251 +#define JZ_GPIO_LCD_DATA3              JZ_GPIO_PORTC(3)
252 +#define JZ_GPIO_LCD_DATA4              JZ_GPIO_PORTC(4)
253 +#define JZ_GPIO_LCD_DATA5              JZ_GPIO_PORTC(5)
254 +#define JZ_GPIO_LCD_DATA6              JZ_GPIO_PORTC(6)
255 +#define JZ_GPIO_LCD_DATA7              JZ_GPIO_PORTC(7)
256 +#define JZ_GPIO_LCD_DATA8              JZ_GPIO_PORTC(8)
257 +#define JZ_GPIO_LCD_DATA9              JZ_GPIO_PORTC(9)
258 +#define JZ_GPIO_LCD_DATA10             JZ_GPIO_PORTC(10)
259 +#define JZ_GPIO_LCD_DATA11             JZ_GPIO_PORTC(11)
260 +#define JZ_GPIO_LCD_DATA12             JZ_GPIO_PORTC(12)
261 +#define JZ_GPIO_LCD_DATA13             JZ_GPIO_PORTC(13)
262 +#define JZ_GPIO_LCD_DATA14             JZ_GPIO_PORTC(14)
263 +#define JZ_GPIO_LCD_DATA15             JZ_GPIO_PORTC(15)
264 +#define JZ_GPIO_LCD_DATA16             JZ_GPIO_PORTC(16)
265 +#define JZ_GPIO_LCD_DATA17             JZ_GPIO_PORTC(17)
266 +#define JZ_GPIO_LCD_PCLK               JZ_GPIO_PORTC(18)
267 +#define JZ_GPIO_LCD_HSYNC              JZ_GPIO_PORTC(19)
268 +#define JZ_GPIO_LCD_VSYNC              JZ_GPIO_PORTC(20)
269 +#define JZ_GPIO_LCD_DE                 JZ_GPIO_PORTC(21)
270 +#define JZ_GPIO_LCD_PS                 JZ_GPIO_PORTC(22)
271 +#define JZ_GPIO_LCD_REV                        JZ_GPIO_PORTC(23)
272 +#define JZ_GPIO_MEM_WE1                        JZ_GPIO_PORTC(24)
273 +#define JZ_GPIO_MEM_WE2                        JZ_GPIO_PORTC(25)
274 +#define JZ_GPIO_MEM_WE3                        JZ_GPIO_PORTC(26)
275 +#define JZ_GPIO_MEM_WAIT               JZ_GPIO_PORTC(27)
276 +#define JZ_GPIO_MEM_FRE                        JZ_GPIO_PORTC(28)
277 +#define JZ_GPIO_MEM_FWE                        JZ_GPIO_PORTC(29)
278 +
279 +#define JZ_GPIO_FUNC_LCD_DATA0         JZ_GPIO_FUNC1
280 +#define JZ_GPIO_FUNC_LCD_DATA1         JZ_GPIO_FUNC1
281 +#define JZ_GPIO_FUNC_LCD_DATA2         JZ_GPIO_FUNC1
282 +#define JZ_GPIO_FUNC_LCD_DATA3         JZ_GPIO_FUNC1
283 +#define JZ_GPIO_FUNC_LCD_DATA4         JZ_GPIO_FUNC1
284 +#define JZ_GPIO_FUNC_LCD_DATA5         JZ_GPIO_FUNC1
285 +#define JZ_GPIO_FUNC_LCD_DATA6         JZ_GPIO_FUNC1
286 +#define JZ_GPIO_FUNC_LCD_DATA7         JZ_GPIO_FUNC1
287 +#define JZ_GPIO_FUNC_LCD_DATA8         JZ_GPIO_FUNC1
288 +#define JZ_GPIO_FUNC_LCD_DATA9         JZ_GPIO_FUNC1
289 +#define JZ_GPIO_FUNC_LCD_DATA10                JZ_GPIO_FUNC1
290 +#define JZ_GPIO_FUNC_LCD_DATA11                JZ_GPIO_FUNC1
291 +#define JZ_GPIO_FUNC_LCD_DATA12                JZ_GPIO_FUNC1
292 +#define JZ_GPIO_FUNC_LCD_DATA13                JZ_GPIO_FUNC1
293 +#define JZ_GPIO_FUNC_LCD_DATA14                JZ_GPIO_FUNC1
294 +#define JZ_GPIO_FUNC_LCD_DATA15                JZ_GPIO_FUNC1
295 +#define JZ_GPIO_FUNC_LCD_DATA16                JZ_GPIO_FUNC1
296 +#define JZ_GPIO_FUNC_LCD_DATA17                JZ_GPIO_FUNC1
297 +#define JZ_GPIO_FUNC_LCD_PCLK          JZ_GPIO_FUNC1
298 +#define JZ_GPIO_FUNC_LCD_VSYNC         JZ_GPIO_FUNC1
299 +#define JZ_GPIO_FUNC_LCD_HSYNC         JZ_GPIO_FUNC1
300 +#define JZ_GPIO_FUNC_LCD_DE            JZ_GPIO_FUNC1
301 +#define JZ_GPIO_FUNC_LCD_PS            JZ_GPIO_FUNC1
302 +#define JZ_GPIO_FUNC_LCD_REV           JZ_GPIO_FUNC1
303 +#define JZ_GPIO_FUNC_MEM_WE1           JZ_GPIO_FUNC1
304 +#define JZ_GPIO_FUNC_MEM_WE2           JZ_GPIO_FUNC1
305 +#define JZ_GPIO_FUNC_MEM_WE3           JZ_GPIO_FUNC1
306 +#define JZ_GPIO_FUNC_MEM_WAIT          JZ_GPIO_FUNC1
307 +#define JZ_GPIO_FUNC_MEM_FRE           JZ_GPIO_FUNC1
308 +#define JZ_GPIO_FUNC_MEM_FWE           JZ_GPIO_FUNC1
309 +
310 +
311 +#define JZ_GPIO_MEM_ADDR19             JZ_GPIO_PORTB(22)
312 +#define JZ_GPIO_MEM_ADDR20             JZ_GPIO_PORTB(23)
313 +
314 +#define JZ_GPIO_FUNC_MEM_ADDR19                JZ_GPIO_FUNC2
315 +#define JZ_GPIO_FUNC_MEM_ADDR20                JZ_GPIO_FUNC2
316 +
317 +/* Port D function pins */
318 +#define JZ_GPIO_CIM_DATA0              JZ_GPIO_PORTD(0)
319 +#define JZ_GPIO_CIM_DATA1              JZ_GPIO_PORTD(1)
320 +#define JZ_GPIO_CIM_DATA2              JZ_GPIO_PORTD(2)
321 +#define JZ_GPIO_CIM_DATA3              JZ_GPIO_PORTD(3)
322 +#define JZ_GPIO_CIM_DATA4              JZ_GPIO_PORTD(4)
323 +#define JZ_GPIO_CIM_DATA5              JZ_GPIO_PORTD(5)
324 +#define JZ_GPIO_CIM_DATA6              JZ_GPIO_PORTD(6)
325 +#define JZ_GPIO_CIM_DATA7              JZ_GPIO_PORTD(7)
326 +#define JZ_GPIO_MSC_CMD                        JZ_GPIO_PORTD(8)
327 +#define JZ_GPIO_MSC_CLK                        JZ_GPIO_PORTD(9)
328 +#define JZ_GPIO_MSC_DATA0              JZ_GPIO_PORTD(10)
329 +#define JZ_GPIO_MSC_DATA1              JZ_GPIO_PORTD(11)
330 +#define JZ_GPIO_MSC_DATA2              JZ_GPIO_PORTD(12)
331 +#define JZ_GPIO_MSC_DATA3              JZ_GPIO_PORTD(13)
332 +#define JZ_GPIO_CIM_MCLK               JZ_GPIO_PORTD(14)
333 +#define JZ_GPIO_CIM_PCLK               JZ_GPIO_PORTD(15)
334 +#define JZ_GPIO_CIM_VSYNC              JZ_GPIO_PORTD(16)
335 +#define JZ_GPIO_CIM_HSYNC              JZ_GPIO_PORTD(17)
336 +#define JZ_GPIO_SPI_CLK                        JZ_GPIO_PORTD(18)
337 +#define JZ_GPIO_SPI_CE0                        JZ_GPIO_PORTD(19)
338 +#define JZ_GPIO_SPI_DT                 JZ_GPIO_PORTD(20)
339 +#define JZ_GPIO_SPI_DR                 JZ_GPIO_PORTD(21)
340 +#define JZ_GPIO_SPI_CE1                        JZ_GPIO_PORTD(22)
341 +#define JZ_GPIO_PWM0                   JZ_GPIO_PORTD(23)
342 +#define JZ_GPIO_PWM1                   JZ_GPIO_PORTD(24)
343 +#define JZ_GPIO_PWM2                   JZ_GPIO_PORTD(25)
344 +#define JZ_GPIO_PWM3                   JZ_GPIO_PORTD(26)
345 +#define JZ_GPIO_PWM4                   JZ_GPIO_PORTD(27)
346 +#define JZ_GPIO_PWM5                   JZ_GPIO_PORTD(28)
347 +#define JZ_GPIO_PWM6                   JZ_GPIO_PORTD(30)
348 +#define JZ_GPIO_PWM7                   JZ_GPIO_PORTD(31)
349 +
350 +#define JZ_GPIO_FUNC_CIM_DATA          JZ_GPIO_FUNC1
351 +#define JZ_GPIO_FUNC_CIM_DATA0         JZ_GPIO_FUNC_CIM_DATA
352 +#define JZ_GPIO_FUNC_CIM_DATA1         JZ_GPIO_FUNC_CIM_DATA
353 +#define JZ_GPIO_FUNC_CIM_DATA2         JZ_GPIO_FUNC_CIM_DATA
354 +#define JZ_GPIO_FUNC_CIM_DATA3         JZ_GPIO_FUNC_CIM_DATA
355 +#define JZ_GPIO_FUNC_CIM_DATA4         JZ_GPIO_FUNC_CIM_DATA
356 +#define JZ_GPIO_FUNC_CIM_DATA5         JZ_GPIO_FUNC_CIM_DATA
357 +#define JZ_GPIO_FUNC_CIM_DATA6         JZ_GPIO_FUNC_CIM_DATA
358 +#define JZ_GPIO_FUNC_CIM_DATA7         JZ_GPIO_FUNC_CIM_DATA
359 +#define JZ_GPIO_FUNC_MSC_CMD           JZ_GPIO_FUNC1
360 +#define JZ_GPIO_FUNC_MSC_CLK           JZ_GPIO_FUNC1
361 +#define JZ_GPIO_FUNC_MSC_DATA          JZ_GPIO_FUNC1
362 +#define JZ_GPIO_FUNC_MSC_DATA0         JZ_GPIO_FUNC_MSC_DATA
363 +#define JZ_GPIO_FUNC_MSC_DATA1         JZ_GPIO_FUNC_MSC_DATA
364 +#define JZ_GPIO_FUNC_MSC_DATA2         JZ_GPIO_FUNC_MSC_DATA
365 +#define JZ_GPIO_FUNC_MSC_DATA3         JZ_GPIO_FUNC_MSC_DATA
366 +#define JZ_GPIO_FUNC_CIM_MCLK          JZ_GPIO_FUNC1
367 +#define JZ_GPIO_FUNC_CIM_PCLK          JZ_GPIO_FUNC1
368 +#define JZ_GPIO_FUNC_CIM_VSYNC         JZ_GPIO_FUNC1
369 +#define JZ_GPIO_FUNC_CIM_HSYNC         JZ_GPIO_FUNC1
370 +#define JZ_GPIO_FUNC_SPI_CLK           JZ_GPIO_FUNC1
371 +#define JZ_GPIO_FUNC_SPI_CE0           JZ_GPIO_FUNC1
372 +#define JZ_GPIO_FUNC_SPI_DT            JZ_GPIO_FUNC1
373 +#define JZ_GPIO_FUNC_SPI_DR            JZ_GPIO_FUNC1
374 +#define JZ_GPIO_FUNC_SPI_CE1           JZ_GPIO_FUNC1
375 +
376 +#define JZ_GPIO_FUNC_PWM               JZ_GPIO_FUNC1
377 +#define JZ_GPIO_FUNC_PWM0              JZ_GPIO_FUNC_PWM
378 +#define JZ_GPIO_FUNC_PWM1              JZ_GPIO_FUNC_PWM
379 +#define JZ_GPIO_FUNC_PWM2              JZ_GPIO_FUNC_PWM
380 +#define JZ_GPIO_FUNC_PWM3              JZ_GPIO_FUNC_PWM
381 +#define JZ_GPIO_FUNC_PWM4              JZ_GPIO_FUNC_PWM
382 +#define JZ_GPIO_FUNC_PWM5              JZ_GPIO_FUNC_PWM
383 +#define JZ_GPIO_FUNC_PWM6              JZ_GPIO_FUNC_PWM
384 +#define JZ_GPIO_FUNC_PWM7              JZ_GPIO_FUNC_PWM
385 +
386 +#define JZ_GPIO_MEM_SCLK_RSTN          JZ_GPIO_PORTD(18)
387 +#define JZ_GPIO_MEM_BCLK               JZ_GPIO_PORTD(19)
388 +#define JZ_GPIO_MEM_SDATO              JZ_GPIO_PORTD(20)
389 +#define JZ_GPIO_MEM_SDATI              JZ_GPIO_PORTD(21)
390 +#define JZ_GPIO_MEM_SYNC               JZ_GPIO_PORTD(22)
391 +#define JZ_GPIO_I2C_SDA                        JZ_GPIO_PORTD(23)
392 +#define JZ_GPIO_I2C_SCK                        JZ_GPIO_PORTD(24)
393 +#define JZ_GPIO_UART0_TXD              JZ_GPIO_PORTD(25)
394 +#define JZ_GPIO_UART0_RXD              JZ_GPIO_PORTD(26)
395 +#define JZ_GPIO_MEM_ADDR17             JZ_GPIO_PORTD(27)
396 +#define JZ_GPIO_MEM_ADDR18             JZ_GPIO_PORTD(28)
397 +#define JZ_GPIO_UART0_CTS              JZ_GPIO_PORTD(30)
398 +#define JZ_GPIO_UART0_RTS              JZ_GPIO_PORTD(31)
399 +
400 +#define JZ_GPIO_FUNC_MEM_SCLK_RSTN     JZ_GPIO_FUNC2
401 +#define JZ_GPIO_FUNC_MEM_BCLK          JZ_GPIO_FUNC2
402 +#define JZ_GPIO_FUNC_MEM_SDATO         JZ_GPIO_FUNC2
403 +#define JZ_GPIO_FUNC_MEM_SDATI         JZ_GPIO_FUNC2
404 +#define JZ_GPIO_FUNC_MEM_SYNC          JZ_GPIO_FUNC2
405 +#define JZ_GPIO_FUNC_I2C_SDA           JZ_GPIO_FUNC2
406 +#define JZ_GPIO_FUNC_I2C_SCK           JZ_GPIO_FUNC2
407 +#define JZ_GPIO_FUNC_UART0_TXD         JZ_GPIO_FUNC2
408 +#define JZ_GPIO_FUNC_UART0_RXD         JZ_GPIO_FUNC2
409 +#define JZ_GPIO_FUNC_MEM_ADDR17                JZ_GPIO_FUNC2
410 +#define JZ_GPIO_FUNC_MEM_ADDR18                JZ_GPIO_FUNC2
411 +#define JZ_GPIO_FUNC_UART0_CTS         JZ_GPIO_FUNC2
412 +#define JZ_GPIO_FUNC_UART0_RTS         JZ_GPIO_FUNC2
413 +
414 +#define JZ_GPIO_UART1_RXD              JZ_GPIO_PORTD(30)
415 +#define JZ_GPIO_UART1_TXD              JZ_GPIO_PORTD(31)
416 +
417 +#define JZ_GPIO_FUNC_UART1_RXD         JZ_GPIO_FUNC3
418 +#define JZ_GPIO_FUNC_UART1_TXD         JZ_GPIO_FUNC3
419 +
420 +#endif
421 --- /dev/null
422 +++ b/arch/mips/jz4740/gpio.c
423 @@ -0,0 +1,604 @@
424 +/*
425 + *  Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
426 + *  JZ4740 platform GPIO support
427 + *
428 + *  This program is free software; you can redistribute it and/or modify it
429 + *  under  the terms of the GNU General  Public License as published by the
430 + *  Free Software Foundation;  either version 2 of the License, or (at your
431 + *  option) any later version.
432 + *
433 + *  You should have received a copy of the GNU General Public License along
434 + *  with this program; if not, write to the Free Software Foundation, Inc.,
435 + *  675 Mass Ave, Cambridge, MA 02139, USA.
436 + *
437 + */
438 +
439 +#include <linux/kernel.h>
440 +#include <linux/module.h>
441 +#include <linux/init.h>
442 +
443 +#include <linux/spinlock.h>
444 +#include <linux/sysdev.h>
445 +#include <linux/io.h>
446 +#include <linux/gpio.h>
447 +#include <linux/delay.h>
448 +#include <linux/interrupt.h>
449 +#include <linux/bitops.h>
450 +
451 +#include <linux/debugfs.h>
452 +#include <linux/seq_file.h>
453 +
454 +#include <asm/mach-jz4740/base.h>
455 +
456 +#define JZ4740_GPIO_BASE_A (32*0)
457 +#define JZ4740_GPIO_BASE_B (32*1)
458 +#define JZ4740_GPIO_BASE_C (32*2)
459 +#define JZ4740_GPIO_BASE_D (32*3)
460 +
461 +#define JZ4740_GPIO_NUM_A 32
462 +#define JZ4740_GPIO_NUM_B 32
463 +#define JZ4740_GPIO_NUM_C 31
464 +#define JZ4740_GPIO_NUM_D 32
465 +
466 +#define JZ4740_IRQ_GPIO_BASE_A (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_A)
467 +#define JZ4740_IRQ_GPIO_BASE_B (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_B)
468 +#define JZ4740_IRQ_GPIO_BASE_C (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_C)
469 +#define JZ4740_IRQ_GPIO_BASE_D (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_D)
470 +
471 +#define JZ_REG_GPIO_PIN                        0x00
472 +#define JZ_REG_GPIO_DATA               0x10
473 +#define JZ_REG_GPIO_DATA_SET           0x14
474 +#define JZ_REG_GPIO_DATA_CLEAR         0x18
475 +#define JZ_REG_GPIO_MASK               0x20
476 +#define JZ_REG_GPIO_MASK_SET           0x24
477 +#define JZ_REG_GPIO_MASK_CLEAR         0x28
478 +#define JZ_REG_GPIO_PULL               0x30
479 +#define JZ_REG_GPIO_PULL_SET           0x34
480 +#define JZ_REG_GPIO_PULL_CLEAR         0x38
481 +#define JZ_REG_GPIO_FUNC               0x40
482 +#define JZ_REG_GPIO_FUNC_SET           0x44
483 +#define JZ_REG_GPIO_FUNC_CLEAR         0x48
484 +#define JZ_REG_GPIO_SELECT             0x50
485 +#define JZ_REG_GPIO_SELECT_SET         0x54
486 +#define JZ_REG_GPIO_SELECT_CLEAR       0x58
487 +#define JZ_REG_GPIO_DIRECTION          0x60
488 +#define JZ_REG_GPIO_DIRECTION_SET      0x64
489 +#define JZ_REG_GPIO_DIRECTION_CLEAR    0x68
490 +#define JZ_REG_GPIO_TRIGGER            0x70
491 +#define JZ_REG_GPIO_TRIGGER_SET                0x74
492 +#define JZ_REG_GPIO_TRIGGER_CLEAR      0x78
493 +#define JZ_REG_GPIO_FLAG               0x80
494 +#define JZ_REG_GPIO_FLAG_CLEAR         0x14
495 +
496 +#define GPIO_TO_BIT(gpio) BIT(gpio & 0x1f)
497 +#define GPIO_TO_REG(gpio, reg) (gpio_to_jz_gpio_chip(gpio)->base + (reg))
498 +#define CHIP_TO_REG(chip, reg) (gpio_chip_to_jz_gpio_chip(chip)->base + (reg))
499 +
500 +struct jz_gpio_chip {
501 +       unsigned int irq;
502 +       unsigned int irq_base;
503 +       uint32_t wakeup;
504 +       uint32_t suspend_mask;
505 +       uint32_t edge_trigger_both;
506 +
507 +       void __iomem *base;
508 +
509 +       spinlock_t lock;
510 +
511 +       struct gpio_chip gpio_chip;
512 +       struct irq_chip irq_chip;
513 +       struct sys_device sysdev;
514 +};
515 +
516 +static struct jz_gpio_chip jz4740_gpio_chips[];
517 +
518 +static inline struct jz_gpio_chip *gpio_to_jz_gpio_chip(unsigned int gpio)
519 +{
520 +       return &jz4740_gpio_chips[gpio >> 5];
521 +}
522 +
523 +static inline struct jz_gpio_chip *gpio_chip_to_jz_gpio_chip(struct gpio_chip *gpio_chip)
524 +{
525 +       return container_of(gpio_chip, struct jz_gpio_chip, gpio_chip);
526 +}
527 +
528 +static inline struct jz_gpio_chip *irq_to_jz_gpio_chip(unsigned int irq)
529 +{
530 +       return get_irq_chip_data(irq);
531 +}
532 +
533 +static inline void jz_gpio_write_bit(unsigned int gpio, unsigned int reg)
534 +{
535 +       writel(GPIO_TO_BIT(gpio), GPIO_TO_REG(gpio, reg));
536 +}
537 +
538 +int jz_gpio_set_function(int gpio, enum jz_gpio_function function)
539 +{
540 +       if (function == JZ_GPIO_FUNC_NONE) {
541 +               jz_gpio_write_bit(gpio, JZ_REG_GPIO_FUNC_CLEAR);
542 +               jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_CLEAR);
543 +               jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_CLEAR);
544 +       } else {
545 +               jz_gpio_write_bit(gpio, JZ_REG_GPIO_FUNC_SET);
546 +               jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_CLEAR);
547 +               switch (function) {
548 +               case JZ_GPIO_FUNC1:
549 +                       jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_CLEAR);
550 +                       break;
551 +               case JZ_GPIO_FUNC3:
552 +                       jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_SET);
553 +               case JZ_GPIO_FUNC2: /* Falltrough */
554 +                       jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_SET);
555 +                       break;
556 +               default:
557 +                       BUG();
558 +                       break;
559 +               }
560 +       }
561 +
562 +       return 0;
563 +}
564 +EXPORT_SYMBOL_GPL(jz_gpio_set_function);
565 +
566 +int jz_gpio_bulk_request(const struct jz_gpio_bulk_request *request, size_t num)
567 +{
568 +       size_t i;
569 +       int ret;
570 +
571 +       for (i = 0; i < num; ++i, ++request) {
572 +               ret = gpio_request(request->gpio, request->name);
573 +               if (ret)
574 +                       goto err;
575 +               jz_gpio_set_function(request->gpio, request->function);
576 +       }
577 +
578 +       return 0;
579 +
580 +err:
581 +       for (--request; i > 0; --i, --request) {
582 +               gpio_free(request->gpio);
583 +               jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE);
584 +       }
585 +
586 +       return ret;
587 +}
588 +EXPORT_SYMBOL_GPL(jz_gpio_bulk_request);
589 +
590 +void jz_gpio_bulk_free(const struct jz_gpio_bulk_request *request, size_t num)
591 +{
592 +       size_t i;
593 +
594 +       for (i = 0; i < num; ++i, ++request) {
595 +               gpio_free(request->gpio);
596 +               jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE);
597 +       }
598 +
599 +}
600 +EXPORT_SYMBOL_GPL(jz_gpio_bulk_free);
601 +
602 +void jz_gpio_bulk_suspend(const struct jz_gpio_bulk_request *request, size_t num)
603 +{
604 +       size_t i;
605 +
606 +       for (i = 0; i < num; ++i, ++request) {
607 +               jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE);
608 +               jz_gpio_write_bit(request->gpio, JZ_REG_GPIO_DIRECTION_CLEAR);
609 +               jz_gpio_write_bit(request->gpio, JZ_REG_GPIO_PULL_SET);
610 +       }
611 +}
612 +EXPORT_SYMBOL_GPL(jz_gpio_bulk_suspend);
613 +
614 +void jz_gpio_bulk_resume(const struct jz_gpio_bulk_request *request, size_t num)
615 +{
616 +       size_t i;
617 +
618 +       for (i = 0; i < num; ++i, ++request)
619 +               jz_gpio_set_function(request->gpio, request->function);
620 +}
621 +EXPORT_SYMBOL_GPL(jz_gpio_bulk_resume);
622 +
623 +void jz_gpio_enable_pullup(unsigned gpio)
624 +{
625 +       jz_gpio_write_bit(gpio, JZ_REG_GPIO_PULL_CLEAR);
626 +}
627 +EXPORT_SYMBOL_GPL(jz_gpio_enable_pullup);
628 +
629 +void jz_gpio_disable_pullup(unsigned gpio)
630 +{
631 +       jz_gpio_write_bit(gpio, JZ_REG_GPIO_PULL_SET);
632 +}
633 +EXPORT_SYMBOL_GPL(jz_gpio_disable_pullup);
634 +
635 +static int jz_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
636 +{
637 +       return !!(readl(CHIP_TO_REG(chip, JZ_REG_GPIO_PIN)) & BIT(gpio));
638 +}
639 +
640 +static void jz_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value)
641 +{
642 +       uint32_t __iomem *reg = CHIP_TO_REG(chip, JZ_REG_GPIO_DATA_SET);
643 +       reg += !value;
644 +       writel(BIT(gpio), reg);
645 +}
646 +
647 +static int jz_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
648 +       int value)
649 +{
650 +       writel(BIT(gpio), CHIP_TO_REG(chip, JZ_REG_GPIO_DIRECTION_SET));
651 +       jz_gpio_set_value(chip, gpio, value);
652 +
653 +       return 0;
654 +}
655 +
656 +static int jz_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
657 +{
658 +       writel(BIT(gpio), CHIP_TO_REG(chip, JZ_REG_GPIO_DIRECTION_CLEAR));
659 +
660 +       return 0;
661 +}
662 +
663 +int jz_gpio_port_direction_input(int port, uint32_t mask)
664 +{
665 +       writel(mask, GPIO_TO_REG(port, JZ_REG_GPIO_DIRECTION_CLEAR));
666 +
667 +       return 0;
668 +}
669 +EXPORT_SYMBOL(jz_gpio_port_direction_input);
670 +
671 +int jz_gpio_port_direction_output(int port, uint32_t mask)
672 +{
673 +       writel(mask, GPIO_TO_REG(port, JZ_REG_GPIO_DIRECTION_SET));
674 +
675 +       return 0;
676 +}
677 +EXPORT_SYMBOL(jz_gpio_port_direction_output);
678 +
679 +void jz_gpio_port_set_value(int port, uint32_t value, uint32_t mask)
680 +{
681 +       writel(~value & mask, GPIO_TO_REG(port, JZ_REG_GPIO_DATA_CLEAR));
682 +       writel(value & mask, GPIO_TO_REG(port, JZ_REG_GPIO_DATA_SET));
683 +}
684 +EXPORT_SYMBOL(jz_gpio_port_set_value);
685 +
686 +uint32_t jz_gpio_port_get_value(int port, uint32_t mask)
687 +{
688 +       uint32_t value = readl(GPIO_TO_REG(port, JZ_REG_GPIO_PIN));
689 +
690 +       return value & mask;
691 +}
692 +EXPORT_SYMBOL(jz_gpio_port_get_value);
693 +
694 +int gpio_to_irq(unsigned gpio)
695 +{
696 +       return JZ4740_IRQ_GPIO(0) + gpio;
697 +}
698 +EXPORT_SYMBOL_GPL(gpio_to_irq);
699 +
700 +int irq_to_gpio(unsigned irq)
701 +{
702 +       return irq - JZ4740_IRQ_GPIO(0);
703 +}
704 +EXPORT_SYMBOL_GPL(irq_to_gpio);
705 +
706 +#define IRQ_TO_BIT(irq) BIT(irq_to_gpio(irq) & 0x1f)
707 +
708 +static void jz_gpio_check_trigger_both(struct jz_gpio_chip *chip, unsigned int irq)
709 +{
710 +       uint32_t value;
711 +       void __iomem *reg;
712 +       uint32_t mask = IRQ_TO_BIT(irq);
713 +
714 +       if (!(chip->edge_trigger_both & mask))
715 +               return;
716 +
717 +       reg = chip->base;
718 +
719 +       value = readl(chip->base + JZ_REG_GPIO_PIN);
720 +       if (value & mask)
721 +               reg += JZ_REG_GPIO_DIRECTION_CLEAR;
722 +       else
723 +               reg += JZ_REG_GPIO_DIRECTION_SET;
724 +
725 +       writel(mask, reg);
726 +}
727 +
728 +static void jz_gpio_irq_demux_handler(unsigned int irq, struct irq_desc *desc)
729 +{
730 +       uint32_t flag;
731 +       unsigned int gpio_irq;
732 +       unsigned int gpio_bank;
733 +       struct jz_gpio_chip *chip = get_irq_desc_data(desc);
734 +
735 +       gpio_bank = JZ4740_IRQ_GPIO0 - irq;
736 +
737 +       flag = readl(chip->base + JZ_REG_GPIO_FLAG);
738 +
739 +       if (!flag)
740 +               return;
741 +
742 +       gpio_irq = __fls(flag);
743 +
744 +       jz_gpio_check_trigger_both(chip, irq);
745 +
746 +       gpio_irq += (gpio_bank << 5) + JZ4740_IRQ_GPIO(0);
747 +
748 +       generic_handle_irq(gpio_irq);
749 +};
750 +
751 +static inline void jz_gpio_set_irq_bit(unsigned int irq, unsigned int reg)
752 +{
753 +       struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(irq);
754 +       writel(IRQ_TO_BIT(irq), chip->base + reg);
755 +}
756 +
757 +static void jz_gpio_irq_mask(unsigned int irq)
758 +{
759 +       jz_gpio_set_irq_bit(irq, JZ_REG_GPIO_MASK_SET);
760 +};
761 +
762 +static void jz_gpio_irq_unmask(unsigned int irq)
763 +{
764 +       struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(irq);
765 +
766 +       jz_gpio_check_trigger_both(chip, irq);
767 +
768 +       jz_gpio_set_irq_bit(irq, JZ_REG_GPIO_MASK_CLEAR);
769 +};
770 +
771 +/* TODO: Check if function is gpio */
772 +static unsigned int jz_gpio_irq_startup(unsigned int irq)
773 +{
774 +       struct irq_desc *desc = irq_to_desc(irq);
775 +
776 +       jz_gpio_set_irq_bit(irq, JZ_REG_GPIO_SELECT_SET);
777 +
778 +       desc->status &= ~IRQ_MASKED;
779 +       jz_gpio_irq_unmask(irq);
780 +
781 +       return 0;
782 +}
783 +
784 +static void jz_gpio_irq_shutdown(unsigned int irq)
785 +{
786 +       struct irq_desc *desc = irq_to_desc(irq);
787 +
788 +       jz_gpio_irq_mask(irq);
789 +       desc->status |= IRQ_MASKED;
790 +
791 +       /* Set direction to input */
792 +       jz_gpio_set_irq_bit(irq, JZ_REG_GPIO_DIRECTION_CLEAR);
793 +       jz_gpio_set_irq_bit(irq, JZ_REG_GPIO_SELECT_CLEAR);
794 +}
795 +
796 +static void jz_gpio_irq_ack(unsigned int irq)
797 +{
798 +       jz_gpio_set_irq_bit(irq, JZ_REG_GPIO_FLAG_CLEAR);
799 +};
800 +
801 +static int jz_gpio_irq_set_type(unsigned int irq, unsigned int flow_type)
802 +{
803 +       struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(irq);
804 +       struct irq_desc *desc = irq_to_desc(irq);
805 +
806 +       jz_gpio_irq_mask(irq);
807 +
808 +       if (flow_type == IRQ_TYPE_EDGE_BOTH) {
809 +               uint32_t value = readl(chip->base + JZ_REG_GPIO_PIN);
810 +               if (value & IRQ_TO_BIT(irq))
811 +                       flow_type = IRQ_TYPE_EDGE_FALLING;
812 +               else
813 +                       flow_type = IRQ_TYPE_EDGE_RISING;
814 +               chip->edge_trigger_both |= IRQ_TO_BIT(irq);
815 +       } else {
816 +               chip->edge_trigger_both &= ~IRQ_TO_BIT(irq);
817 +       }
818 +
819 +       switch (flow_type) {
820 +       case IRQ_TYPE_EDGE_RISING:
821 +               jz_gpio_set_irq_bit(irq, JZ_REG_GPIO_DIRECTION_SET);
822 +               jz_gpio_set_irq_bit(irq, JZ_REG_GPIO_TRIGGER_SET);
823 +               break;
824 +       case IRQ_TYPE_EDGE_FALLING:
825 +               jz_gpio_set_irq_bit(irq, JZ_REG_GPIO_DIRECTION_CLEAR);
826 +               jz_gpio_set_irq_bit(irq, JZ_REG_GPIO_TRIGGER_SET);
827 +               break;
828 +       case IRQ_TYPE_LEVEL_HIGH:
829 +               jz_gpio_set_irq_bit(irq, JZ_REG_GPIO_DIRECTION_SET);
830 +               jz_gpio_set_irq_bit(irq, JZ_REG_GPIO_TRIGGER_CLEAR);
831 +               break;
832 +       case IRQ_TYPE_LEVEL_LOW:
833 +               jz_gpio_set_irq_bit(irq, JZ_REG_GPIO_DIRECTION_CLEAR);
834 +               jz_gpio_set_irq_bit(irq, JZ_REG_GPIO_TRIGGER_CLEAR);
835 +               break;
836 +       default:
837 +               return -EINVAL;
838 +       }
839 +
840 +       if (!(desc->status & IRQ_MASKED))
841 +               jz_gpio_irq_unmask(irq);
842 +
843 +       return 0;
844 +}
845 +
846 +static int jz_gpio_irq_set_wake(unsigned int irq, unsigned int on)
847 +{
848 +       struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(irq);
849 +       spin_lock(&chip->lock);
850 +       if (on)
851 +               chip->wakeup |= IRQ_TO_BIT(irq);
852 +       else
853 +               chip->wakeup &= ~IRQ_TO_BIT(irq);
854 +       spin_unlock(&chip->lock);
855 +
856 +       set_irq_wake(chip->irq, on);
857 +       return 0;
858 +}
859 +
860 +/*
861 + * This lock class tells lockdep that GPIO irqs are in a different
862 + * category than their parents, so it won't report false recursion.
863 + */
864 +static struct lock_class_key gpio_lock_class;
865 +
866 +#define JZ4740_GPIO_CHIP(_bank) { \
867 +       .irq_base = JZ4740_IRQ_GPIO_BASE_ ## _bank, \
868 +       .gpio_chip = { \
869 +               .label = "Bank " # _bank, \
870 +               .owner = THIS_MODULE, \
871 +               .set = jz_gpio_set_value, \
872 +               .get = jz_gpio_get_value, \
873 +               .direction_output = jz_gpio_direction_output, \
874 +               .direction_input = jz_gpio_direction_input, \
875 +               .base = JZ4740_GPIO_BASE_ ## _bank, \
876 +               .ngpio = JZ4740_GPIO_NUM_ ## _bank, \
877 +       }, \
878 +       .irq_chip =  { \
879 +               .name = "GPIO Bank " # _bank, \
880 +               .mask = jz_gpio_irq_mask, \
881 +               .unmask = jz_gpio_irq_unmask, \
882 +               .ack = jz_gpio_irq_ack, \
883 +               .startup = jz_gpio_irq_startup, \
884 +               .shutdown = jz_gpio_irq_shutdown, \
885 +               .set_type = jz_gpio_irq_set_type, \
886 +               .set_wake = jz_gpio_irq_set_wake, \
887 +       }, \
888 +}
889 +
890 +static struct jz_gpio_chip jz4740_gpio_chips[] = {
891 +       JZ4740_GPIO_CHIP(A),
892 +       JZ4740_GPIO_CHIP(B),
893 +       JZ4740_GPIO_CHIP(C),
894 +       JZ4740_GPIO_CHIP(D),
895 +};
896 +
897 +static inline struct jz_gpio_chip *sysdev_to_chip(struct sys_device *dev)
898 +{
899 +       return container_of(dev, struct jz_gpio_chip, sysdev);
900 +}
901 +
902 +static int jz4740_gpio_suspend(struct sys_device *dev, pm_message_t state)
903 +{
904 +       struct jz_gpio_chip *chip = sysdev_to_chip(dev);
905 +
906 +       chip->suspend_mask = readl(chip->base + JZ_REG_GPIO_MASK);
907 +       writel(~(chip->wakeup), chip->base + JZ_REG_GPIO_MASK_SET);
908 +       writel(chip->wakeup, chip->base + JZ_REG_GPIO_MASK_CLEAR);
909 +
910 +       return 0;
911 +}
912 +
913 +static int jz4740_gpio_resume(struct sys_device *dev)
914 +{
915 +       struct jz_gpio_chip *chip = sysdev_to_chip(dev);
916 +       uint32_t mask = chip->suspend_mask;
917 +
918 +       writel(~mask, chip->base + JZ_REG_GPIO_MASK_CLEAR);
919 +       writel(mask, chip->base + JZ_REG_GPIO_MASK_SET);
920 +
921 +       return 0;
922 +}
923 +
924 +static struct sysdev_class jz4740_gpio_sysdev_class = {
925 +       .name = "gpio",
926 +       .suspend = jz4740_gpio_suspend,
927 +       .resume = jz4740_gpio_resume,
928 +};
929 +
930 +static int jz4740_gpio_chip_init(struct jz_gpio_chip *chip, unsigned int id)
931 +{
932 +       int ret, irq;
933 +
934 +       chip->sysdev.id = id;
935 +       chip->sysdev.cls = &jz4740_gpio_sysdev_class;
936 +       ret = sysdev_register(&chip->sysdev);
937 +
938 +       if (ret)
939 +               return ret;
940 +
941 +       spin_lock_init(&chip->lock);
942 +
943 +       chip->base = ioremap(JZ4740_GPIO_BASE_ADDR + (id * 0x100), 0x100);
944 +
945 +       gpiochip_add(&chip->gpio_chip);
946 +
947 +       chip->irq = JZ4740_IRQ_INTC_GPIO(id);
948 +       set_irq_data(chip->irq, chip);
949 +       set_irq_chained_handler(chip->irq, jz_gpio_irq_demux_handler);
950 +
951 +       for (irq = chip->irq_base; irq < chip->irq_base + chip->gpio_chip.ngpio; ++irq) {
952 +               lockdep_set_class(&irq_desc[irq].lock, &gpio_lock_class);
953 +               set_irq_chip_data(irq, chip);
954 +               set_irq_chip_and_handler(irq, &chip->irq_chip, handle_level_irq);
955 +       }
956 +
957 +       return 0;
958 +}
959 +
960 +static int __init jz4740_gpio_init(void)
961 +{
962 +       unsigned int i;
963 +       int ret;
964 +
965 +       ret = sysdev_class_register(&jz4740_gpio_sysdev_class);
966 +       if (ret)
967 +               return ret;
968 +
969 +       for (i = 0; i < ARRAY_SIZE(jz4740_gpio_chips); ++i)
970 +               jz4740_gpio_chip_init(&jz4740_gpio_chips[i], i);
971 +
972 +       printk(KERN_INFO "JZ4740 GPIO initalized\n");
973 +
974 +       return 0;
975 +}
976 +arch_initcall(jz4740_gpio_init);
977 +
978 +#ifdef CONFIG_DEBUG_FS
979 +
980 +static inline void gpio_seq_reg(struct seq_file *s, struct jz_gpio_chip *chip,
981 +       const char *name, unsigned int reg)
982 +{
983 +       seq_printf(s, "\t%s: %08x\n", name, readl(chip->base + reg));
984 +}
985 +
986 +static int gpio_regs_show(struct seq_file *s, void *unused)
987 +{
988 +       struct jz_gpio_chip *chip = jz4740_gpio_chips;
989 +       int i;
990 +
991 +       for (i = 0; i < ARRAY_SIZE(jz4740_gpio_chips); ++i, ++chip) {
992 +               seq_printf(s, "==GPIO %d==\n", i);
993 +               gpio_seq_reg(s, chip, "Pin", JZ_REG_GPIO_PIN);
994 +               gpio_seq_reg(s, chip, "Data", JZ_REG_GPIO_DATA);
995 +               gpio_seq_reg(s, chip, "Mask", JZ_REG_GPIO_MASK);
996 +               gpio_seq_reg(s, chip, "Pull", JZ_REG_GPIO_PULL);
997 +               gpio_seq_reg(s, chip, "Func", JZ_REG_GPIO_FUNC);
998 +               gpio_seq_reg(s, chip, "Select", JZ_REG_GPIO_SELECT);
999 +               gpio_seq_reg(s, chip, "Direction", JZ_REG_GPIO_DIRECTION);
1000 +               gpio_seq_reg(s, chip, "Trigger", JZ_REG_GPIO_TRIGGER);
1001 +               gpio_seq_reg(s, chip, "Flag", JZ_REG_GPIO_FLAG);
1002 +       }
1003 +
1004 +       return 0;
1005 +}
1006 +
1007 +static int gpio_regs_open(struct inode *inode, struct file *file)
1008 +{
1009 +       return single_open(file, gpio_regs_show, NULL);
1010 +}
1011 +
1012 +static const struct file_operations gpio_regs_operations = {
1013 +       .open           = gpio_regs_open,
1014 +       .read           = seq_read,
1015 +       .llseek         = seq_lseek,
1016 +       .release        = single_release,
1017 +};
1018 +
1019 +static int __init gpio_debugfs_init(void)
1020 +{
1021 +       (void) debugfs_create_file("jz_regs_gpio", S_IFREG | S_IRUGO,
1022 +                               NULL, NULL, &gpio_regs_operations);
1023 +       return 0;
1024 +}
1025 +subsys_initcall(gpio_debugfs_init);
1026 +
1027 +#endif