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 #include <linux/debugfs.h>
29 #include <linux/seq_file.h>
31 #include <asm/mach-jz4740/base.h>
33 #define JZ4740_GPIO_BASE_A (32*0)
34 #define JZ4740_GPIO_BASE_B (32*1)
35 #define JZ4740_GPIO_BASE_C (32*2)
36 #define JZ4740_GPIO_BASE_D (32*3)
38 #define JZ4740_GPIO_NUM_A 32
39 #define JZ4740_GPIO_NUM_B 32
40 #define JZ4740_GPIO_NUM_C 31
41 #define JZ4740_GPIO_NUM_D 32
43 #define JZ4740_IRQ_GPIO_BASE_A (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_A)
44 #define JZ4740_IRQ_GPIO_BASE_B (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_B)
45 #define JZ4740_IRQ_GPIO_BASE_C (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_C)
46 #define JZ4740_IRQ_GPIO_BASE_D (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_D)
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 GPIO_TO_BIT(gpio) BIT(gpio & 0x1f)
74 #define GPIO_TO_REG(gpio, reg) (gpio_to_jz_gpio_chip(gpio)->base + (reg))
75 #define CHIP_TO_REG(chip, reg) (gpio_chip_to_jz_gpio_chip(chip)->base + (reg))
79 unsigned int irq_base
;
81 uint32_t suspend_mask
;
82 uint32_t edge_trigger_both
;
88 struct gpio_chip gpio_chip
;
89 struct sys_device sysdev
;
92 static struct jz_gpio_chip jz4740_gpio_chips
[];
94 static inline struct jz_gpio_chip
*gpio_to_jz_gpio_chip(unsigned int gpio
)
96 return &jz4740_gpio_chips
[gpio
>> 5];
99 static inline struct jz_gpio_chip
*gpio_chip_to_jz_gpio_chip(struct gpio_chip
*gpio_chip
)
101 return container_of(gpio_chip
, struct jz_gpio_chip
, gpio_chip
);
104 static inline struct jz_gpio_chip
*irq_to_jz_gpio_chip(struct irq_data
*data
)
106 return irq_data_get_irq_chip_data(data
);
109 static inline void jz_gpio_write_bit(unsigned int gpio
, unsigned int reg
)
111 writel(GPIO_TO_BIT(gpio
), GPIO_TO_REG(gpio
, reg
));
114 int jz_gpio_set_function(int gpio
, enum jz_gpio_function function
)
116 if (function
== JZ_GPIO_FUNC_NONE
) {
117 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_FUNC_CLEAR
);
118 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_SELECT_CLEAR
);
119 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_TRIGGER_CLEAR
);
121 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_FUNC_SET
);
122 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_TRIGGER_CLEAR
);
125 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_SELECT_CLEAR
);
128 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_TRIGGER_SET
);
129 case JZ_GPIO_FUNC2
: /* Falltrough */
130 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_SELECT_SET
);
140 EXPORT_SYMBOL_GPL(jz_gpio_set_function
);
142 int jz_gpio_bulk_request(const struct jz_gpio_bulk_request
*request
, size_t num
)
147 for (i
= 0; i
< num
; ++i
, ++request
) {
148 ret
= gpio_request(request
->gpio
, request
->name
);
151 jz_gpio_set_function(request
->gpio
, request
->function
);
157 for (--request
; i
> 0; --i
, --request
) {
158 gpio_free(request
->gpio
);
159 jz_gpio_set_function(request
->gpio
, JZ_GPIO_FUNC_NONE
);
164 EXPORT_SYMBOL_GPL(jz_gpio_bulk_request
);
166 void jz_gpio_bulk_free(const struct jz_gpio_bulk_request
*request
, size_t num
)
170 for (i
= 0; i
< num
; ++i
, ++request
) {
171 gpio_free(request
->gpio
);
172 jz_gpio_set_function(request
->gpio
, JZ_GPIO_FUNC_NONE
);
176 EXPORT_SYMBOL_GPL(jz_gpio_bulk_free
);
178 void jz_gpio_bulk_suspend(const struct jz_gpio_bulk_request
*request
, size_t num
)
182 for (i
= 0; i
< num
; ++i
, ++request
) {
183 jz_gpio_set_function(request
->gpio
, JZ_GPIO_FUNC_NONE
);
184 jz_gpio_write_bit(request
->gpio
, JZ_REG_GPIO_DIRECTION_CLEAR
);
185 jz_gpio_write_bit(request
->gpio
, JZ_REG_GPIO_PULL_SET
);
188 EXPORT_SYMBOL_GPL(jz_gpio_bulk_suspend
);
190 void jz_gpio_bulk_resume(const struct jz_gpio_bulk_request
*request
, size_t num
)
194 for (i
= 0; i
< num
; ++i
, ++request
)
195 jz_gpio_set_function(request
->gpio
, request
->function
);
197 EXPORT_SYMBOL_GPL(jz_gpio_bulk_resume
);
199 void jz_gpio_enable_pullup(unsigned gpio
)
201 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_PULL_CLEAR
);
203 EXPORT_SYMBOL_GPL(jz_gpio_enable_pullup
);
205 void jz_gpio_disable_pullup(unsigned gpio
)
207 jz_gpio_write_bit(gpio
, JZ_REG_GPIO_PULL_SET
);
209 EXPORT_SYMBOL_GPL(jz_gpio_disable_pullup
);
211 static int jz_gpio_get_value(struct gpio_chip
*chip
, unsigned gpio
)
213 return !!(readl(CHIP_TO_REG(chip
, JZ_REG_GPIO_PIN
)) & BIT(gpio
));
216 static void jz_gpio_set_value(struct gpio_chip
*chip
, unsigned gpio
, int value
)
218 uint32_t __iomem
*reg
= CHIP_TO_REG(chip
, JZ_REG_GPIO_DATA_SET
);
220 writel(BIT(gpio
), reg
);
223 static int jz_gpio_direction_output(struct gpio_chip
*chip
, unsigned gpio
,
226 writel(BIT(gpio
), CHIP_TO_REG(chip
, JZ_REG_GPIO_DIRECTION_SET
));
227 jz_gpio_set_value(chip
, gpio
, value
);
232 static int jz_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
234 writel(BIT(gpio
), CHIP_TO_REG(chip
, JZ_REG_GPIO_DIRECTION_CLEAR
));
239 int jz_gpio_port_direction_input(int port
, uint32_t mask
)
241 writel(mask
, GPIO_TO_REG(port
, JZ_REG_GPIO_DIRECTION_CLEAR
));
245 EXPORT_SYMBOL(jz_gpio_port_direction_input
);
247 int jz_gpio_port_direction_output(int port
, uint32_t mask
)
249 writel(mask
, GPIO_TO_REG(port
, JZ_REG_GPIO_DIRECTION_SET
));
253 EXPORT_SYMBOL(jz_gpio_port_direction_output
);
255 void jz_gpio_port_set_value(int port
, uint32_t value
, uint32_t mask
)
257 writel(~value
& mask
, GPIO_TO_REG(port
, JZ_REG_GPIO_DATA_CLEAR
));
258 writel(value
& mask
, GPIO_TO_REG(port
, JZ_REG_GPIO_DATA_SET
));
260 EXPORT_SYMBOL(jz_gpio_port_set_value
);
262 uint32_t jz_gpio_port_get_value(int port
, uint32_t mask
)
264 uint32_t value
= readl(GPIO_TO_REG(port
, JZ_REG_GPIO_PIN
));
268 EXPORT_SYMBOL(jz_gpio_port_get_value
);
270 int gpio_to_irq(unsigned gpio
)
272 return JZ4740_IRQ_GPIO(0) + gpio
;
274 EXPORT_SYMBOL_GPL(gpio_to_irq
);
276 int irq_to_gpio(unsigned irq
)
278 return irq
- JZ4740_IRQ_GPIO(0);
280 EXPORT_SYMBOL_GPL(irq_to_gpio
);
282 #define IRQ_TO_BIT(irq) BIT(irq_to_gpio(irq) & 0x1f)
284 static void jz_gpio_check_trigger_both(struct jz_gpio_chip
*chip
, unsigned int irq
)
288 uint32_t mask
= IRQ_TO_BIT(irq
);
290 if (!(chip
->edge_trigger_both
& mask
))
295 value
= readl(chip
->base
+ JZ_REG_GPIO_PIN
);
297 reg
+= JZ_REG_GPIO_DIRECTION_CLEAR
;
299 reg
+= JZ_REG_GPIO_DIRECTION_SET
;
304 static void jz_gpio_irq_demux_handler(unsigned int irq
, struct irq_desc
*desc
)
307 unsigned int gpio_irq
;
308 unsigned int gpio_bank
;
309 struct jz_gpio_chip
*chip
= get_irq_desc_data(desc
);
311 gpio_bank
= JZ4740_IRQ_GPIO0
- irq
;
313 flag
= readl(chip
->base
+ JZ_REG_GPIO_FLAG
);
318 gpio_irq
= __fls(flag
);
320 jz_gpio_check_trigger_both(chip
, irq
);
322 gpio_irq
+= (gpio_bank
<< 5) + JZ4740_IRQ_GPIO(0);
324 generic_handle_irq(gpio_irq
);
327 static inline void jz_gpio_set_irq_bit(struct irq_data
*data
, unsigned int reg
)
329 struct jz_gpio_chip
*chip
= irq_to_jz_gpio_chip(data
);
330 writel(IRQ_TO_BIT(data
->irq
), chip
->base
+ reg
);
333 static void jz_gpio_irq_mask(struct irq_data
*data
)
335 jz_gpio_set_irq_bit(data
, JZ_REG_GPIO_MASK_SET
);
338 static void jz_gpio_irq_unmask(struct irq_data
*data
)
340 struct jz_gpio_chip
*chip
= irq_to_jz_gpio_chip(data
);
342 jz_gpio_check_trigger_both(chip
, data
->irq
);
344 jz_gpio_set_irq_bit(data
, JZ_REG_GPIO_MASK_CLEAR
);
347 /* TODO: Check if function is gpio */
348 static unsigned int jz_gpio_irq_startup(struct irq_data
*data
)
350 jz_gpio_set_irq_bit(data
, JZ_REG_GPIO_SELECT_SET
);
351 jz_gpio_irq_unmask(data
);
355 static void jz_gpio_irq_shutdown(struct irq_data
*data
)
357 jz_gpio_irq_mask(data
);
359 /* Set direction to input */
360 jz_gpio_set_irq_bit(data
, JZ_REG_GPIO_DIRECTION_CLEAR
);
361 jz_gpio_set_irq_bit(data
, JZ_REG_GPIO_SELECT_CLEAR
);
364 static void jz_gpio_irq_ack(struct irq_data
*data
)
366 jz_gpio_set_irq_bit(data
, JZ_REG_GPIO_FLAG_CLEAR
);
369 static int jz_gpio_irq_set_type(struct irq_data
*data
, unsigned int flow_type
)
371 struct jz_gpio_chip
*chip
= irq_to_jz_gpio_chip(data
);
372 unsigned int irq
= data
->irq
;
374 if (flow_type
== IRQ_TYPE_EDGE_BOTH
) {
375 uint32_t value
= readl(chip
->base
+ JZ_REG_GPIO_PIN
);
376 if (value
& IRQ_TO_BIT(irq
))
377 flow_type
= IRQ_TYPE_EDGE_FALLING
;
379 flow_type
= IRQ_TYPE_EDGE_RISING
;
380 chip
->edge_trigger_both
|= IRQ_TO_BIT(irq
);
382 chip
->edge_trigger_both
&= ~IRQ_TO_BIT(irq
);
386 case IRQ_TYPE_EDGE_RISING
:
387 jz_gpio_set_irq_bit(data
, JZ_REG_GPIO_DIRECTION_SET
);
388 jz_gpio_set_irq_bit(data
, JZ_REG_GPIO_TRIGGER_SET
);
390 case IRQ_TYPE_EDGE_FALLING
:
391 jz_gpio_set_irq_bit(data
, JZ_REG_GPIO_DIRECTION_CLEAR
);
392 jz_gpio_set_irq_bit(data
, JZ_REG_GPIO_TRIGGER_SET
);
394 case IRQ_TYPE_LEVEL_HIGH
:
395 jz_gpio_set_irq_bit(data
, JZ_REG_GPIO_DIRECTION_SET
);
396 jz_gpio_set_irq_bit(data
, JZ_REG_GPIO_TRIGGER_CLEAR
);
398 case IRQ_TYPE_LEVEL_LOW
:
399 jz_gpio_set_irq_bit(data
, JZ_REG_GPIO_DIRECTION_CLEAR
);
400 jz_gpio_set_irq_bit(data
, JZ_REG_GPIO_TRIGGER_CLEAR
);
409 static int jz_gpio_irq_set_wake(struct irq_data
*data
, unsigned int on
)
411 struct jz_gpio_chip
*chip
= irq_to_jz_gpio_chip(data
);
412 spin_lock(&chip
->lock
);
414 chip
->wakeup
|= IRQ_TO_BIT(data
->irq
);
416 chip
->wakeup
&= ~IRQ_TO_BIT(data
->irq
);
417 spin_unlock(&chip
->lock
);
419 set_irq_wake(chip
->irq
, on
);
423 static struct irq_chip jz_gpio_irq_chip
= {
425 .irq_mask
= jz_gpio_irq_mask
,
426 .irq_unmask
= jz_gpio_irq_unmask
,
427 .irq_ack
= jz_gpio_irq_ack
,
428 .irq_startup
= jz_gpio_irq_startup
,
429 .irq_shutdown
= jz_gpio_irq_shutdown
,
430 .irq_set_type
= jz_gpio_irq_set_type
,
431 .irq_set_wake
= jz_gpio_irq_set_wake
,
432 .flags
= IRQCHIP_SET_TYPE_MASKED
,
436 * This lock class tells lockdep that GPIO irqs are in a different
437 * category than their parents, so it won't report false recursion.
439 static struct lock_class_key gpio_lock_class
;
441 #define JZ4740_GPIO_CHIP(_bank) { \
442 .irq_base = JZ4740_IRQ_GPIO_BASE_ ## _bank, \
444 .label = "Bank " # _bank, \
445 .owner = THIS_MODULE, \
446 .set = jz_gpio_set_value, \
447 .get = jz_gpio_get_value, \
448 .direction_output = jz_gpio_direction_output, \
449 .direction_input = jz_gpio_direction_input, \
450 .base = JZ4740_GPIO_BASE_ ## _bank, \
451 .ngpio = JZ4740_GPIO_NUM_ ## _bank, \
455 static struct jz_gpio_chip jz4740_gpio_chips
[] = {
462 static inline struct jz_gpio_chip
*sysdev_to_chip(struct sys_device
*dev
)
464 return container_of(dev
, struct jz_gpio_chip
, sysdev
);
467 static int jz4740_gpio_suspend(struct sys_device
*dev
, pm_message_t state
)
469 struct jz_gpio_chip
*chip
= sysdev_to_chip(dev
);
471 chip
->suspend_mask
= readl(chip
->base
+ JZ_REG_GPIO_MASK
);
472 writel(~(chip
->wakeup
), chip
->base
+ JZ_REG_GPIO_MASK_SET
);
473 writel(chip
->wakeup
, chip
->base
+ JZ_REG_GPIO_MASK_CLEAR
);
478 static int jz4740_gpio_resume(struct sys_device
*dev
)
480 struct jz_gpio_chip
*chip
= sysdev_to_chip(dev
);
481 uint32_t mask
= chip
->suspend_mask
;
483 writel(~mask
, chip
->base
+ JZ_REG_GPIO_MASK_CLEAR
);
484 writel(mask
, chip
->base
+ JZ_REG_GPIO_MASK_SET
);
489 static struct sysdev_class jz4740_gpio_sysdev_class
= {
491 .suspend
= jz4740_gpio_suspend
,
492 .resume
= jz4740_gpio_resume
,
495 static int jz4740_gpio_chip_init(struct jz_gpio_chip
*chip
, unsigned int id
)
499 chip
->sysdev
.id
= id
;
500 chip
->sysdev
.cls
= &jz4740_gpio_sysdev_class
;
501 ret
= sysdev_register(&chip
->sysdev
);
506 spin_lock_init(&chip
->lock
);
508 chip
->base
= ioremap(JZ4740_GPIO_BASE_ADDR
+ (id
* 0x100), 0x100);
510 gpiochip_add(&chip
->gpio_chip
);
512 chip
->irq
= JZ4740_IRQ_INTC_GPIO(id
);
513 set_irq_data(chip
->irq
, chip
);
514 set_irq_chained_handler(chip
->irq
, jz_gpio_irq_demux_handler
);
516 for (irq
= chip
->irq_base
; irq
< chip
->irq_base
+ chip
->gpio_chip
.ngpio
; ++irq
) {
517 irq_set_lockdep_class(irq
, &gpio_lock_class
);
518 set_irq_chip_data(irq
, chip
);
519 set_irq_chip_and_handler(irq
, &jz_gpio_irq_chip
,
526 static int __init
jz4740_gpio_init(void)
531 ret
= sysdev_class_register(&jz4740_gpio_sysdev_class
);
535 for (i
= 0; i
< ARRAY_SIZE(jz4740_gpio_chips
); ++i
)
536 jz4740_gpio_chip_init(&jz4740_gpio_chips
[i
], i
);
538 printk(KERN_INFO
"JZ4740 GPIO initialized\n");
542 arch_initcall(jz4740_gpio_init
);
544 #ifdef CONFIG_DEBUG_FS
546 static inline void gpio_seq_reg(struct seq_file
*s
, struct jz_gpio_chip
*chip
,
547 const char *name
, unsigned int reg
)
549 seq_printf(s
, "\t%s: %08x\n", name
, readl(chip
->base
+ reg
));
552 static int gpio_regs_show(struct seq_file
*s
, void *unused
)
554 struct jz_gpio_chip
*chip
= jz4740_gpio_chips
;
557 for (i
= 0; i
< ARRAY_SIZE(jz4740_gpio_chips
); ++i
, ++chip
) {
558 seq_printf(s
, "==GPIO %d==\n", i
);
559 gpio_seq_reg(s
, chip
, "Pin", JZ_REG_GPIO_PIN
);
560 gpio_seq_reg(s
, chip
, "Data", JZ_REG_GPIO_DATA
);
561 gpio_seq_reg(s
, chip
, "Mask", JZ_REG_GPIO_MASK
);
562 gpio_seq_reg(s
, chip
, "Pull", JZ_REG_GPIO_PULL
);
563 gpio_seq_reg(s
, chip
, "Func", JZ_REG_GPIO_FUNC
);
564 gpio_seq_reg(s
, chip
, "Select", JZ_REG_GPIO_SELECT
);
565 gpio_seq_reg(s
, chip
, "Direction", JZ_REG_GPIO_DIRECTION
);
566 gpio_seq_reg(s
, chip
, "Trigger", JZ_REG_GPIO_TRIGGER
);
567 gpio_seq_reg(s
, chip
, "Flag", JZ_REG_GPIO_FLAG
);
573 static int gpio_regs_open(struct inode
*inode
, struct file
*file
)
575 return single_open(file
, gpio_regs_show
, NULL
);
578 static const struct file_operations gpio_regs_operations
= {
579 .open
= gpio_regs_open
,
582 .release
= single_release
,
585 static int __init
gpio_debugfs_init(void)
587 (void) debugfs_create_file("jz_regs_gpio", S_IFREG
| S_IRUGO
,
588 NULL
, NULL
, &gpio_regs_operations
);
591 subsys_initcall(gpio_debugfs_init
);