2 * Driver for NEC VR4100 series General-purpose I/O Unit.
4 * Copyright (C) 2002 MontaVista Software Inc.
5 * Author: Yoichi Yuasa <yyuasa@mvista.com or source@mvista.com>
6 * Copyright (C) 2003-2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/spinlock.h>
31 #include <linux/types.h>
34 #include <asm/vr41xx/giu.h>
35 #include <asm/vr41xx/irq.h>
36 #include <asm/vr41xx/vr41xx.h>
38 MODULE_AUTHOR("Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>");
39 MODULE_DESCRIPTION("NEC VR4100 series General-purpose I/O Unit driver");
40 MODULE_LICENSE("GPL");
42 static int major
; /* default is dynamic major device number */
43 module_param(major
, int, 0);
44 MODULE_PARM_DESC(major
, "Major device number");
46 #define GIUIOSELL 0x00
47 #define GIUIOSELH 0x02
50 #define GIUINTSTATL 0x08
51 #define GIUINTSTATH 0x0a
52 #define GIUINTENL 0x0c
53 #define GIUINTENH 0x0e
54 #define GIUINTTYPL 0x10
55 #define GIUINTTYPH 0x12
56 #define GIUINTALSELL 0x14
57 #define GIUINTALSELH 0x16
58 #define GIUINTHTSELL 0x18
59 #define GIUINTHTSELH 0x1a
60 #define GIUPODATL 0x1c
61 #define GIUPODATEN 0x1c
62 #define GIUPODATH 0x1e
66 #define GIUFEDGEINHL 0x20
67 #define GIUFEDGEINHH 0x22
68 #define GIUREDGEINHL 0x24
69 #define GIUREDGEINHH 0x26
71 #define GIUUSEUPDN 0x1e0
72 #define GIUTERMUPDN 0x1e2
74 #define GPIO_HAS_PULLUPDOWN_IO 0x0001
75 #define GPIO_HAS_OUTPUT_ENABLE 0x0002
76 #define GPIO_HAS_INTERRUPT_EDGE_SELECT 0x0100
78 static spinlock_t giu_lock
;
79 static unsigned long giu_flags
;
80 static unsigned int giu_nr_pins
;
82 static void __iomem
*giu_base
;
84 #define giu_read(offset) readw(giu_base + (offset))
85 #define giu_write(offset, value) writew((value), giu_base + (offset))
87 #define GPIO_PIN_OF_IRQ(irq) ((irq) - GIU_IRQ_BASE)
88 #define GIUINT_HIGH_OFFSET 16
89 #define GIUINT_HIGH_MAX 32
91 static inline uint16_t giu_set(uint16_t offset
, uint16_t set
)
95 data
= giu_read(offset
);
97 giu_write(offset
, data
);
102 static inline uint16_t giu_clear(uint16_t offset
, uint16_t clear
)
106 data
= giu_read(offset
);
108 giu_write(offset
, data
);
113 static void ack_giuint_low(unsigned int irq
)
115 giu_write(GIUINTSTATL
, 1 << GPIO_PIN_OF_IRQ(irq
));
118 static void mask_giuint_low(unsigned int irq
)
120 giu_clear(GIUINTENL
, 1 << GPIO_PIN_OF_IRQ(irq
));
123 static void mask_ack_giuint_low(unsigned int irq
)
127 pin
= GPIO_PIN_OF_IRQ(irq
);
128 giu_clear(GIUINTENL
, 1 << pin
);
129 giu_write(GIUINTSTATL
, 1 << pin
);
132 static void unmask_giuint_low(unsigned int irq
)
134 giu_set(GIUINTENL
, 1 << GPIO_PIN_OF_IRQ(irq
));
137 static struct irq_chip giuint_low_irq_chip
= {
139 .ack
= ack_giuint_low
,
140 .mask
= mask_giuint_low
,
141 .mask_ack
= mask_ack_giuint_low
,
142 .unmask
= unmask_giuint_low
,
145 static void ack_giuint_high(unsigned int irq
)
147 giu_write(GIUINTSTATH
, 1 << (GPIO_PIN_OF_IRQ(irq
) - GIUINT_HIGH_OFFSET
));
150 static void mask_giuint_high(unsigned int irq
)
152 giu_clear(GIUINTENH
, 1 << (GPIO_PIN_OF_IRQ(irq
) - GIUINT_HIGH_OFFSET
));
155 static void mask_ack_giuint_high(unsigned int irq
)
159 pin
= GPIO_PIN_OF_IRQ(irq
) - GIUINT_HIGH_OFFSET
;
160 giu_clear(GIUINTENH
, 1 << pin
);
161 giu_write(GIUINTSTATH
, 1 << pin
);
164 static void unmask_giuint_high(unsigned int irq
)
166 giu_set(GIUINTENH
, 1 << (GPIO_PIN_OF_IRQ(irq
) - GIUINT_HIGH_OFFSET
));
169 static struct irq_chip giuint_high_irq_chip
= {
171 .ack
= ack_giuint_high
,
172 .mask
= mask_giuint_high
,
173 .mask_ack
= mask_ack_giuint_high
,
174 .unmask
= unmask_giuint_high
,
177 static int giu_get_irq(unsigned int irq
)
179 uint16_t pendl
, pendh
, maskl
, maskh
;
182 pendl
= giu_read(GIUINTSTATL
);
183 pendh
= giu_read(GIUINTSTATH
);
184 maskl
= giu_read(GIUINTENL
);
185 maskh
= giu_read(GIUINTENH
);
191 for (i
= 0; i
< 16; i
++) {
192 if (maskl
& (1 << i
))
196 for (i
= 0; i
< 16; i
++) {
197 if (maskh
& (1 << i
))
198 return GIU_IRQ(i
+ GIUINT_HIGH_OFFSET
);
202 printk(KERN_ERR
"spurious GIU interrupt: %04x(%04x),%04x(%04x)\n",
203 maskl
, pendl
, maskh
, pendh
);
205 atomic_inc(&irq_err_count
);
210 void vr41xx_set_irq_trigger(unsigned int pin
, irq_trigger_t trigger
, irq_signal_t signal
)
214 if (pin
< GIUINT_HIGH_OFFSET
) {
216 if (trigger
!= IRQ_TRIGGER_LEVEL
) {
217 giu_set(GIUINTTYPL
, mask
);
218 if (signal
== IRQ_SIGNAL_HOLD
)
219 giu_set(GIUINTHTSELL
, mask
);
221 giu_clear(GIUINTHTSELL
, mask
);
222 if (giu_flags
& GPIO_HAS_INTERRUPT_EDGE_SELECT
) {
224 case IRQ_TRIGGER_EDGE_FALLING
:
225 giu_set(GIUFEDGEINHL
, mask
);
226 giu_clear(GIUREDGEINHL
, mask
);
228 case IRQ_TRIGGER_EDGE_RISING
:
229 giu_clear(GIUFEDGEINHL
, mask
);
230 giu_set(GIUREDGEINHL
, mask
);
233 giu_set(GIUFEDGEINHL
, mask
);
234 giu_set(GIUREDGEINHL
, mask
);
238 set_irq_chip_and_handler(GIU_IRQ(pin
),
239 &giuint_low_irq_chip
,
242 giu_clear(GIUINTTYPL
, mask
);
243 giu_clear(GIUINTHTSELL
, mask
);
244 set_irq_chip_and_handler(GIU_IRQ(pin
),
245 &giuint_low_irq_chip
,
248 giu_write(GIUINTSTATL
, mask
);
249 } else if (pin
< GIUINT_HIGH_MAX
) {
250 mask
= 1 << (pin
- GIUINT_HIGH_OFFSET
);
251 if (trigger
!= IRQ_TRIGGER_LEVEL
) {
252 giu_set(GIUINTTYPH
, mask
);
253 if (signal
== IRQ_SIGNAL_HOLD
)
254 giu_set(GIUINTHTSELH
, mask
);
256 giu_clear(GIUINTHTSELH
, mask
);
257 if (giu_flags
& GPIO_HAS_INTERRUPT_EDGE_SELECT
) {
259 case IRQ_TRIGGER_EDGE_FALLING
:
260 giu_set(GIUFEDGEINHH
, mask
);
261 giu_clear(GIUREDGEINHH
, mask
);
263 case IRQ_TRIGGER_EDGE_RISING
:
264 giu_clear(GIUFEDGEINHH
, mask
);
265 giu_set(GIUREDGEINHH
, mask
);
268 giu_set(GIUFEDGEINHH
, mask
);
269 giu_set(GIUREDGEINHH
, mask
);
273 set_irq_chip_and_handler(GIU_IRQ(pin
),
274 &giuint_high_irq_chip
,
277 giu_clear(GIUINTTYPH
, mask
);
278 giu_clear(GIUINTHTSELH
, mask
);
279 set_irq_chip_and_handler(GIU_IRQ(pin
),
280 &giuint_high_irq_chip
,
283 giu_write(GIUINTSTATH
, mask
);
286 EXPORT_SYMBOL_GPL(vr41xx_set_irq_trigger
);
288 void vr41xx_set_irq_level(unsigned int pin
, irq_level_t level
)
292 if (pin
< GIUINT_HIGH_OFFSET
) {
294 if (level
== IRQ_LEVEL_HIGH
)
295 giu_set(GIUINTALSELL
, mask
);
297 giu_clear(GIUINTALSELL
, mask
);
298 giu_write(GIUINTSTATL
, mask
);
299 } else if (pin
< GIUINT_HIGH_MAX
) {
300 mask
= 1 << (pin
- GIUINT_HIGH_OFFSET
);
301 if (level
== IRQ_LEVEL_HIGH
)
302 giu_set(GIUINTALSELH
, mask
);
304 giu_clear(GIUINTALSELH
, mask
);
305 giu_write(GIUINTSTATH
, mask
);
308 EXPORT_SYMBOL_GPL(vr41xx_set_irq_level
);
310 gpio_data_t
vr41xx_gpio_get_pin(unsigned int pin
)
314 if (pin
>= giu_nr_pins
)
315 return GPIO_DATA_INVAL
;
318 reg
= giu_read(GIUPIODL
);
319 mask
= (uint16_t)1 << pin
;
320 } else if (pin
< 32) {
321 reg
= giu_read(GIUPIODH
);
322 mask
= (uint16_t)1 << (pin
- 16);
323 } else if (pin
< 48) {
324 reg
= giu_read(GIUPODATL
);
325 mask
= (uint16_t)1 << (pin
- 32);
327 reg
= giu_read(GIUPODATH
);
328 mask
= (uint16_t)1 << (pin
- 48);
332 return GPIO_DATA_HIGH
;
334 return GPIO_DATA_LOW
;
336 EXPORT_SYMBOL_GPL(vr41xx_gpio_get_pin
);
338 int vr41xx_gpio_set_pin(unsigned int pin
, gpio_data_t data
)
340 uint16_t offset
, mask
, reg
;
343 if (pin
>= giu_nr_pins
)
348 mask
= (uint16_t)1 << pin
;
349 } else if (pin
< 32) {
351 mask
= (uint16_t)1 << (pin
- 16);
352 } else if (pin
< 48) {
354 mask
= (uint16_t)1 << (pin
- 32);
357 mask
= (uint16_t)1 << (pin
- 48);
360 spin_lock_irqsave(&giu_lock
, flags
);
362 reg
= giu_read(offset
);
363 if (data
== GPIO_DATA_HIGH
)
367 giu_write(offset
, reg
);
369 spin_unlock_irqrestore(&giu_lock
, flags
);
373 EXPORT_SYMBOL_GPL(vr41xx_gpio_set_pin
);
375 int vr41xx_gpio_set_direction(unsigned int pin
, gpio_direction_t dir
)
377 uint16_t offset
, mask
, reg
;
380 if (pin
>= giu_nr_pins
)
385 mask
= (uint16_t)1 << pin
;
386 } else if (pin
< 32) {
388 mask
= (uint16_t)1 << (pin
- 16);
390 if (giu_flags
& GPIO_HAS_OUTPUT_ENABLE
) {
392 mask
= (uint16_t)1 << (pin
- 32);
409 spin_lock_irqsave(&giu_lock
, flags
);
411 reg
= giu_read(offset
);
412 if (dir
== GPIO_OUTPUT
)
416 giu_write(offset
, reg
);
418 spin_unlock_irqrestore(&giu_lock
, flags
);
422 EXPORT_SYMBOL_GPL(vr41xx_gpio_set_direction
);
424 int vr41xx_gpio_pullupdown(unsigned int pin
, gpio_pull_t pull
)
429 if ((giu_flags
& GPIO_HAS_PULLUPDOWN_IO
) != GPIO_HAS_PULLUPDOWN_IO
)
435 mask
= (uint16_t)1 << pin
;
437 spin_lock_irqsave(&giu_lock
, flags
);
439 if (pull
== GPIO_PULL_UP
|| pull
== GPIO_PULL_DOWN
) {
440 reg
= giu_read(GIUTERMUPDN
);
441 if (pull
== GPIO_PULL_UP
)
445 giu_write(GIUTERMUPDN
, reg
);
447 reg
= giu_read(GIUUSEUPDN
);
449 giu_write(GIUUSEUPDN
, reg
);
451 reg
= giu_read(GIUUSEUPDN
);
453 giu_write(GIUUSEUPDN
, reg
);
456 spin_unlock_irqrestore(&giu_lock
, flags
);
460 EXPORT_SYMBOL_GPL(vr41xx_gpio_pullupdown
);
462 static ssize_t
gpio_read(struct file
*file
, char __user
*buf
, size_t len
,
468 pin
= iminor(file
->f_path
.dentry
->d_inode
);
469 if (pin
>= giu_nr_pins
)
472 if (vr41xx_gpio_get_pin(pin
) == GPIO_DATA_HIGH
)
478 if (put_user(value
, buf
))
484 static ssize_t
gpio_write(struct file
*file
, const char __user
*data
,
485 size_t len
, loff_t
*ppos
)
492 pin
= iminor(file
->f_path
.dentry
->d_inode
);
493 if (pin
>= giu_nr_pins
)
496 for (i
= 0; i
< len
; i
++) {
497 if (get_user(c
, data
+ i
))
502 retval
= vr41xx_gpio_set_pin(pin
, GPIO_DATA_LOW
);
505 retval
= vr41xx_gpio_set_pin(pin
, GPIO_DATA_HIGH
);
508 printk(KERN_INFO
"GPIO%d: pull down\n", pin
);
509 retval
= vr41xx_gpio_pullupdown(pin
, GPIO_PULL_DOWN
);
512 printk(KERN_INFO
"GPIO%d: pull up/down disable\n", pin
);
513 retval
= vr41xx_gpio_pullupdown(pin
, GPIO_PULL_DISABLE
);
516 printk(KERN_INFO
"GPIO%d: input\n", pin
);
517 retval
= vr41xx_gpio_set_direction(pin
, GPIO_INPUT
);
520 printk(KERN_INFO
"GPIO%d: output\n", pin
);
521 retval
= vr41xx_gpio_set_direction(pin
, GPIO_OUTPUT
);
524 printk(KERN_INFO
"GPIO%d: output disable\n", pin
);
525 retval
= vr41xx_gpio_set_direction(pin
, GPIO_OUTPUT_DISABLE
);
528 printk(KERN_INFO
"GPIO%d: pull up\n", pin
);
529 retval
= vr41xx_gpio_pullupdown(pin
, GPIO_PULL_UP
);
532 printk(KERN_INFO
"GPIO%d: pull up/down disable\n", pin
);
533 retval
= vr41xx_gpio_pullupdown(pin
, GPIO_PULL_DISABLE
);
546 static int gpio_open(struct inode
*inode
, struct file
*file
)
551 if (pin
>= giu_nr_pins
)
554 return nonseekable_open(inode
, file
);
557 static int gpio_release(struct inode
*inode
, struct file
*file
)
562 if (pin
>= giu_nr_pins
)
568 static const struct file_operations gpio_fops
= {
569 .owner
= THIS_MODULE
,
573 .release
= gpio_release
,
576 static int __devinit
giu_probe(struct platform_device
*dev
)
578 struct resource
*res
;
579 unsigned int trigger
, i
, pin
;
580 struct irq_chip
*chip
;
584 case GPIO_50PINS_PULLUPDOWN
:
585 giu_flags
= GPIO_HAS_PULLUPDOWN_IO
;
591 case GPIO_48PINS_EDGE_SELECT
:
592 giu_flags
= GPIO_HAS_INTERRUPT_EDGE_SELECT
;
596 printk(KERN_ERR
"GIU: unknown ID %d\n", dev
->id
);
600 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
604 giu_base
= ioremap(res
->start
, res
->end
- res
->start
+ 1);
608 retval
= register_chrdev(major
, "GIU", &gpio_fops
);
617 printk(KERN_INFO
"GIU: major number %d\n", major
);
620 spin_lock_init(&giu_lock
);
622 giu_write(GIUINTENL
, 0);
623 giu_write(GIUINTENH
, 0);
625 trigger
= giu_read(GIUINTTYPH
) << 16;
626 trigger
|= giu_read(GIUINTTYPL
);
627 for (i
= GIU_IRQ_BASE
; i
<= GIU_IRQ_LAST
; i
++) {
628 pin
= GPIO_PIN_OF_IRQ(i
);
629 if (pin
< GIUINT_HIGH_OFFSET
)
630 chip
= &giuint_low_irq_chip
;
632 chip
= &giuint_high_irq_chip
;
634 if (trigger
& (1 << pin
))
635 set_irq_chip_and_handler(i
, chip
, handle_edge_irq
);
637 set_irq_chip_and_handler(i
, chip
, handle_level_irq
);
641 irq
= platform_get_irq(dev
, 0);
642 if (irq
< 0 || irq
>= NR_IRQS
)
645 return cascade_irq(irq
, giu_get_irq
);
648 static int __devexit
giu_remove(struct platform_device
*dev
)
658 static struct platform_driver giu_device_driver
= {
660 .remove
= __devexit_p(giu_remove
),
663 .owner
= THIS_MODULE
,
667 static int __init
vr41xx_giu_init(void)
669 return platform_driver_register(&giu_device_driver
);
672 static void __exit
vr41xx_giu_exit(void)
674 platform_driver_unregister(&giu_device_driver
);
677 module_init(vr41xx_giu_init
);
678 module_exit(vr41xx_giu_exit
);