2 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
3 * JZ4740 platform GPIO support
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
20 #include <linux/spinlock.h>
21 #include <linux/sysdev.h>
23 #include <linux/gpio.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/bitops.h>
28 #define JZ_GPIO_BASE_A (32*0)
29 #define JZ_GPIO_BASE_B (32*1)
30 #define JZ_GPIO_BASE_C (32*2)
31 #define JZ_GPIO_BASE_D (32*3)
33 #define JZ_GPIO_NUM_A 32
34 #define JZ_GPIO_NUM_B 32
35 #define JZ_GPIO_NUM_C 31
36 #define JZ_GPIO_NUM_D 32
38 #define JZ_IRQ_GPIO_BASE_A (JZ_IRQ_GPIO(0) + JZ_GPIO_BASE_A)
39 #define JZ_IRQ_GPIO_BASE_B (JZ_IRQ_GPIO(0) + JZ_GPIO_BASE_B)
40 #define JZ_IRQ_GPIO_BASE_C (JZ_IRQ_GPIO(0) + JZ_GPIO_BASE_C)
41 #define JZ_IRQ_GPIO_BASE_D (JZ_IRQ_GPIO(0) + JZ_GPIO_BASE_D)
43 #define JZ_IRQ_GPIO_A(num) (JZ_IRQ_GPIO_BASE_A + num)
44 #define JZ_IRQ_GPIO_B(num) (JZ_IRQ_GPIO_BASE_B + num)
45 #define JZ_IRQ_GPIO_C(num) (JZ_IRQ_GPIO_BASE_C + num)
46 #define JZ_IRQ_GPIO_D(num) (JZ_IRQ_GPIO_BASE_D + num)
48 #define JZ_REG_GPIO_PIN 0x00
49 #define JZ_REG_GPIO_DATA 0x10
50 #define JZ_REG_GPIO_DATA_SET 0x14
51 #define JZ_REG_GPIO_DATA_CLEAR 0x18
52 #define JZ_REG_GPIO_MASK 0x20
53 #define JZ_REG_GPIO_MASK_SET 0x24
54 #define JZ_REG_GPIO_MASK_CLEAR 0x28
55 #define JZ_REG_GPIO_PULL 0x30
56 #define JZ_REG_GPIO_PULL_SET 0x34
57 #define JZ_REG_GPIO_PULL_CLEAR 0x38
58 #define JZ_REG_GPIO_FUNC 0x40
59 #define JZ_REG_GPIO_FUNC_SET 0x44
60 #define JZ_REG_GPIO_FUNC_CLEAR 0x48
61 #define JZ_REG_GPIO_SELECT 0x50
62 #define JZ_REG_GPIO_SELECT_SET 0x54
63 #define JZ_REG_GPIO_SELECT_CLEAR 0x58
64 #define JZ_REG_GPIO_DIRECTION 0x60
65 #define JZ_REG_GPIO_DIRECTION_SET 0x64
66 #define JZ_REG_GPIO_DIRECTION_CLEAR 0x68
67 #define JZ_REG_GPIO_TRIGGER 0x70
68 #define JZ_REG_GPIO_TRIGGER_SET 0x74
69 #define JZ_REG_GPIO_TRIGGER_CLEAR 0x78
70 #define JZ_REG_GPIO_FLAG 0x80
71 #define JZ_REG_GPIO_FLAG_CLEAR 0x14
73 #define CHIP_TO_REG(chip, reg) (jz_gpio_base + (((chip)->base) << 3) + reg)
75 #define GPIO_TO_BIT(gpio) BIT(gpio & 0x1f)
76 #define GPIO_TO_REG(gpio, reg) (jz_gpio_base + ((gpio >> 5) << 8) + reg)
78 static void __iomem
*jz_gpio_base
;
82 unsigned int irq_base
;
84 uint32_t suspend_mask
;
85 uint32_t edge_trigger_both
;
87 struct gpio_chip gpio_chip
;
88 struct irq_chip irq_chip
;
91 static struct jz_gpio_chip
*jz_irq_to_chip(unsigned int irq
)
93 return get_irq_chip_data(irq
);
96 static inline void jz_gpio_write_bit(unsigned int gpio
, unsigned int reg
)
98 writel(GPIO_TO_BIT(gpio
), GPIO_TO_REG(gpio
, reg
));
101 int jz_gpio_set_function(int gpio
, enum jz_gpio_function function
)
103 if (function
== JZ_GPIO_FUNC_NONE
) {
104 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_FUNC_CLEAR
);
105 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_SELECT_CLEAR
);
106 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_TRIGGER_CLEAR
);
108 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_FUNC_SET
);
109 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_TRIGGER_CLEAR
);
112 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_SELECT_CLEAR
);
115 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_TRIGGER_SET
);
116 case JZ_GPIO_FUNC2
: /* Falltrough */
117 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_SELECT_SET
);
127 EXPORT_SYMBOL_GPL(jz_gpio_set_function
);
129 int jz_gpio_bulk_request(const struct jz_gpio_bulk_request
*request
, size_t num
)
134 for (i
= 0; i
< num
; ++i
, ++request
) {
135 ret
= gpio_request(request
->gpio
, request
->name
);
138 jz_gpio_set_function(request
->gpio
, request
->function
);
143 for (--request
; i
> 0; --i
, --request
)
144 gpio_free(request
->gpio
);
148 EXPORT_SYMBOL_GPL(jz_gpio_bulk_request
);
150 void jz_gpio_bulk_free(const struct jz_gpio_bulk_request
*request
, size_t num
)
154 for (i
= 0; i
< num
; ++i
, ++request
) {
155 gpio_free(request
->gpio
);
156 jz_gpio_set_function(request
->gpio
, JZ_GPIO_FUNC_NONE
);
160 EXPORT_SYMBOL_GPL(jz_gpio_bulk_free
);
162 void jz_gpio_bulk_suspend(const struct jz_gpio_bulk_request
*request
, size_t num
)
166 for (i
= 0; i
< num
; ++i
, ++request
) {
167 jz_gpio_set_function(request
->gpio
, JZ_GPIO_FUNC_NONE
);
168 jz_gpio_write_bit(request
->gpio
, JZ_REG_GPIO_DIRECTION_CLEAR
);
169 jz_gpio_write_bit(request
->gpio
, JZ_REG_GPIO_PULL_SET
);
172 EXPORT_SYMBOL_GPL(jz_gpio_bulk_suspend
);
174 void jz_gpio_bulk_resume(const struct jz_gpio_bulk_request
*request
, size_t num
)
178 for (i
= 0; i
< num
; ++i
, ++request
) {
179 jz_gpio_set_function(request
->gpio
, request
->function
);
182 EXPORT_SYMBOL_GPL(jz_gpio_bulk_resume
);
184 void jz_gpio_enable_pullup(unsigned gpio
)
186 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_PULL_CLEAR
);
188 EXPORT_SYMBOL_GPL(jz_gpio_enable_pullup
);
190 void jz_gpio_disable_pullup(unsigned gpio
)
192 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_PULL_SET
);
194 EXPORT_SYMBOL_GPL(jz_gpio_disable_pullup
);
196 static int jz_gpio_get_value(struct gpio_chip
*chip
, unsigned gpio
)
198 return !!(readl(CHIP_TO_REG(chip
, JZ_REG_GPIO_PIN
)) & BIT(gpio
));
201 static void jz_gpio_set_value(struct gpio_chip
*chip
, unsigned gpio
, int value
)
203 uint32_t __iomem
*reg
= CHIP_TO_REG(chip
, JZ_REG_GPIO_DATA_SET
);
205 writel(BIT(gpio
), reg
);
208 static int jz_gpio_direction_output(struct gpio_chip
*chip
, unsigned gpio
, int value
)
210 writel(BIT(gpio
), CHIP_TO_REG(chip
, JZ_REG_GPIO_DIRECTION_SET
));
211 jz_gpio_set_value(chip
, gpio
, value
);
216 static int jz_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
218 writel(BIT(gpio
), CHIP_TO_REG(chip
, JZ_REG_GPIO_DIRECTION_CLEAR
));
223 int jz_gpio_port_direction_input(int port
, uint32_t mask
)
225 writel(mask
, GPIO_TO_REG(port
, JZ_REG_GPIO_DIRECTION_CLEAR
));
229 EXPORT_SYMBOL(jz_gpio_port_direction_input
);
231 int jz_gpio_port_direction_output(int port
, uint32_t mask
)
233 writel(mask
, GPIO_TO_REG(port
, JZ_REG_GPIO_DIRECTION_SET
));
237 EXPORT_SYMBOL(jz_gpio_port_direction_output
);
239 void jz_gpio_port_set_value(int port
, uint32_t value
, uint32_t mask
)
241 writel((~value
) & mask
, GPIO_TO_REG(port
, JZ_REG_GPIO_DATA_CLEAR
));
242 writel(value
& mask
, GPIO_TO_REG(port
, JZ_REG_GPIO_DATA_SET
));
244 EXPORT_SYMBOL(jz_gpio_port_set_value
);
246 uint32_t jz_gpio_port_get_value(int port
, uint32_t mask
)
248 uint32_t value
= readl(GPIO_TO_REG(port
, JZ_REG_GPIO_PIN
));
252 EXPORT_SYMBOL(jz_gpio_port_get_value
);
255 #define IRQ_TO_GPIO(irq) (irq - JZ_IRQ_GPIO(0))
256 #define IRQ_TO_BIT(irq) BIT(IRQ_TO_GPIO(irq) & 0x1f)
258 #define IRQ_TO_REG(irq, reg) GPIO_TO_REG(IRQ_TO_GPIO(irq), reg)
260 static void jz_gpio_irq_demux_handler(unsigned int irq
, struct irq_desc
*desc
)
263 unsigned int gpio_irq
;
264 unsigned int gpio_bank
;
265 struct jz_gpio_chip
*chip
= get_irq_desc_data(desc
);
267 gpio_bank
= JZ_IRQ_GPIO0
- irq
;
269 flag
= readl(jz_gpio_base
+ (gpio_bank
<< 8) + JZ_REG_GPIO_FLAG
);
271 gpio_irq
= ffs(flag
) - 1;
273 if (chip
->edge_trigger_both
& BIT(gpio_irq
)) {
274 uint32_t value
= readl(CHIP_TO_REG(&chip
->gpio_chip
, JZ_REG_GPIO_PIN
));
275 if (value
& BIT(gpio_irq
)) {
276 writel(BIT(gpio_irq
),
277 CHIP_TO_REG(&chip
->gpio_chip
, JZ_REG_GPIO_DIRECTION_CLEAR
));
279 writel(BIT(gpio_irq
),
280 CHIP_TO_REG(&chip
->gpio_chip
, JZ_REG_GPIO_DIRECTION_SET
));
284 gpio_irq
+= (gpio_bank
<< 5) + JZ_IRQ_GPIO(0);
286 generic_handle_irq(gpio_irq
);
289 static inline void jz_gpio_set_irq_bit(unsigned int irq
, unsigned int reg
)
291 writel(IRQ_TO_BIT(irq
), IRQ_TO_REG(irq
, reg
));
294 static void jz_gpio_irq_mask(unsigned int irq
)
296 jz_gpio_set_irq_bit(irq
, JZ_REG_GPIO_MASK_SET
);
299 static void jz_gpio_irq_unmask(unsigned int irq
)
301 jz_gpio_set_irq_bit(irq
, JZ_REG_GPIO_MASK_CLEAR
);
304 /* TODO: Check if function is gpio */
305 static unsigned int jz_gpio_irq_startup(unsigned int irq
)
307 struct irq_desc
*desc
= irq_to_desc(irq
);
309 jz_gpio_set_irq_bit(irq
, JZ_REG_GPIO_SELECT_SET
);
311 jz_gpio_irq_unmask(irq
);
312 desc
->status
&= ~IRQ_MASKED
;
317 static void jz_gpio_irq_shutdown(unsigned int irq
)
319 struct irq_desc
*desc
= irq_to_desc(irq
);
321 jz_gpio_irq_mask(irq
);
322 desc
->status
|= IRQ_MASKED
;
324 /* Set direction to input */
325 jz_gpio_set_irq_bit(irq
, JZ_REG_GPIO_DIRECTION_CLEAR
);
326 jz_gpio_set_irq_bit(irq
, JZ_REG_GPIO_SELECT_CLEAR
);
329 static void jz_gpio_irq_ack(unsigned int irq
)
331 jz_gpio_set_irq_bit(irq
, JZ_REG_GPIO_FLAG_CLEAR
);
334 static int jz_gpio_irq_set_type(unsigned int irq
, unsigned int flow_type
)
336 struct jz_gpio_chip
*chip
= jz_irq_to_chip(irq
);
337 struct irq_desc
*desc
= irq_to_desc(irq
);
339 jz_gpio_irq_mask(irq
);
341 if (flow_type
== IRQ_TYPE_EDGE_BOTH
) {
342 uint32_t value
= readl(IRQ_TO_REG(irq
, JZ_REG_GPIO_PIN
));
343 if (value
& IRQ_TO_BIT(irq
))
344 flow_type
= IRQ_TYPE_EDGE_FALLING
;
346 flow_type
= IRQ_TYPE_EDGE_RISING
;
347 chip
->edge_trigger_both
|= IRQ_TO_BIT(irq
);
349 chip
->edge_trigger_both
&= ~IRQ_TO_BIT(irq
);
353 case IRQ_TYPE_EDGE_RISING
:
354 jz_gpio_set_irq_bit(irq
, JZ_REG_GPIO_DIRECTION_SET
);
355 jz_gpio_set_irq_bit(irq
, JZ_REG_GPIO_TRIGGER_SET
);
357 case IRQ_TYPE_EDGE_FALLING
:
358 jz_gpio_set_irq_bit(irq
, JZ_REG_GPIO_DIRECTION_CLEAR
);
359 jz_gpio_set_irq_bit(irq
, JZ_REG_GPIO_TRIGGER_SET
);
361 case IRQ_TYPE_LEVEL_HIGH
:
362 jz_gpio_set_irq_bit(irq
, JZ_REG_GPIO_DIRECTION_SET
);
363 jz_gpio_set_irq_bit(irq
, JZ_REG_GPIO_TRIGGER_CLEAR
);
365 case IRQ_TYPE_LEVEL_LOW
:
366 jz_gpio_set_irq_bit(irq
, JZ_REG_GPIO_DIRECTION_CLEAR
);
367 jz_gpio_set_irq_bit(irq
, JZ_REG_GPIO_TRIGGER_CLEAR
);
373 if (!(desc
->status
& IRQ_MASKED
))
374 jz_gpio_irq_unmask(irq
);
379 static int jz_gpio_irq_set_wake(unsigned int irq
, unsigned int on
)
381 struct jz_gpio_chip
*chip
= jz_irq_to_chip(irq
);
382 spin_lock(&chip
->lock
);
384 chip
->wakeup
|= IRQ_TO_BIT(irq
);
386 chip
->wakeup
&= ~IRQ_TO_BIT(irq
);
387 spin_unlock(&chip
->lock
);
389 set_irq_wake(chip
->irq
, !!(chip
->wakeup
));
393 int gpio_to_irq(unsigned gpio
)
395 return JZ_IRQ_GPIO(0) + gpio
;
397 EXPORT_SYMBOL_GPL(gpio_to_irq
);
399 int irq_to_gpio(unsigned gpio
)
401 return IRQ_TO_GPIO(gpio
);
403 EXPORT_SYMBOL_GPL(irq_to_gpio
);
405 #define JZ_GPIO_CHIP(_bank) { \
406 .irq_base = JZ_IRQ_GPIO_BASE_ ## _bank, \
408 .label = "Bank " # _bank, \
409 .owner = THIS_MODULE, \
410 .set = jz_gpio_set_value, \
411 .get = jz_gpio_get_value, \
412 .direction_output = jz_gpio_direction_output, \
413 .direction_input = jz_gpio_direction_input, \
414 .base = JZ_GPIO_BASE_ ## _bank, \
415 .ngpio = JZ_GPIO_NUM_ ## _bank, \
418 .name = "GPIO Bank " # _bank, \
419 .mask = jz_gpio_irq_mask, \
420 .unmask = jz_gpio_irq_unmask, \
421 .ack = jz_gpio_irq_ack, \
422 .startup = jz_gpio_irq_startup, \
423 .shutdown = jz_gpio_irq_shutdown, \
424 .set_type = jz_gpio_irq_set_type, \
425 .set_wake = jz_gpio_irq_set_wake, \
429 static struct jz_gpio_chip jz_gpio_chips
[] = {
436 int jz_gpio_suspend(void)
438 struct jz_gpio_chip
*chip
= jz_gpio_chips
;
441 for (i
= 0; i
< ARRAY_SIZE(jz_gpio_chips
); ++i
, ++chip
) {
442 gpio
= chip
->gpio_chip
.base
;
443 chip
->suspend_mask
= readl(GPIO_TO_REG(gpio
, JZ_REG_GPIO_MASK
));
444 writel(~(chip
->wakeup
), GPIO_TO_REG(gpio
, JZ_REG_GPIO_MASK_SET
));
447 chip
= jz_gpio_chips
;
449 for (i
= 0; i
< ARRAY_SIZE(jz_gpio_chips
); ++i
, ++chip
) {
450 printk("GPIO %d: \n", i
);
451 printk("\tPin: %.8x\n", readl(CHIP_TO_REG(&chip
->gpio_chip
, JZ_REG_GPIO_PIN
)));
452 printk("\tData: %.8x\n", readl(CHIP_TO_REG(&chip
->gpio_chip
, JZ_REG_GPIO_DATA
)));
453 printk("\tPull: %.8x\n", readl(CHIP_TO_REG(&chip
->gpio_chip
, JZ_REG_GPIO_PULL
)));
454 printk("\tFunc: %.8x\n", readl(CHIP_TO_REG(&chip
->gpio_chip
, JZ_REG_GPIO_FUNC
)));
455 printk("\tSelect: %.8x\n", readl(CHIP_TO_REG(&chip
->gpio_chip
, JZ_REG_GPIO_SELECT
)));
456 printk("\tDirection: %.8x\n", readl(CHIP_TO_REG(&chip
->gpio_chip
, JZ_REG_GPIO_DIRECTION
)));
457 printk("\tTrigger: %.8x\n", readl(CHIP_TO_REG(&chip
->gpio_chip
, JZ_REG_GPIO_TRIGGER
)));
464 int jz_gpio_resume(void)
466 struct jz_gpio_chip
*chip
= jz_gpio_chips
;
468 for (i
= 0; i
< ARRAY_SIZE(jz_gpio_chips
); ++i
, ++chip
) {
469 writel(~(chip
->suspend_mask
), GPIO_TO_REG(chip
->gpio_chip
.base
, JZ_REG_GPIO_MASK_CLEAR
));
475 int __init
jz_gpiolib_init(void)
477 struct jz_gpio_chip
*chip
= jz_gpio_chips
;
480 jz_gpio_base
= ioremap(0x10010000, 0x400);
482 for (i
= 0; i
< ARRAY_SIZE(jz_gpio_chips
); ++i
, ++chip
) {
483 gpiochip_add(&chip
->gpio_chip
);
484 spin_lock_init(&chip
->lock
);
485 chip
->irq
= JZ_IRQ_INTC_GPIO(i
);
486 set_irq_data(chip
->irq
, chip
);
487 set_irq_chained_handler(chip
->irq
, jz_gpio_irq_demux_handler
);
488 for (irq
= chip
->irq_base
; irq
< chip
->irq_base
+ chip
->gpio_chip
.ngpio
; ++irq
) {
489 set_irq_chip_and_handler(irq
, &chip
->irq_chip
, handle_level_irq
);
490 set_irq_chip_data(irq
, chip
);
494 printk("JZ GPIO initalized\n");