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/smp_lock.h>
31 #include <linux/spinlock.h>
32 #include <linux/types.h>
35 #include <asm/vr41xx/giu.h>
36 #include <asm/vr41xx/irq.h>
37 #include <asm/vr41xx/vr41xx.h>
39 MODULE_AUTHOR("Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>");
40 MODULE_DESCRIPTION("NEC VR4100 series General-purpose I/O Unit driver");
41 MODULE_LICENSE("GPL");
43 static int major; /* default is dynamic major device number */
44 module_param(major, int, 0);
45 MODULE_PARM_DESC(major, "Major device number");
47 #define GIUIOSELL 0x00
48 #define GIUIOSELH 0x02
51 #define GIUINTSTATL 0x08
52 #define GIUINTSTATH 0x0a
53 #define GIUINTENL 0x0c
54 #define GIUINTENH 0x0e
55 #define GIUINTTYPL 0x10
56 #define GIUINTTYPH 0x12
57 #define GIUINTALSELL 0x14
58 #define GIUINTALSELH 0x16
59 #define GIUINTHTSELL 0x18
60 #define GIUINTHTSELH 0x1a
61 #define GIUPODATL 0x1c
62 #define GIUPODATEN 0x1c
63 #define GIUPODATH 0x1e
67 #define GIUFEDGEINHL 0x20
68 #define GIUFEDGEINHH 0x22
69 #define GIUREDGEINHL 0x24
70 #define GIUREDGEINHH 0x26
72 #define GIUUSEUPDN 0x1e0
73 #define GIUTERMUPDN 0x1e2
75 #define GPIO_HAS_PULLUPDOWN_IO 0x0001
76 #define GPIO_HAS_OUTPUT_ENABLE 0x0002
77 #define GPIO_HAS_INTERRUPT_EDGE_SELECT 0x0100
79 static spinlock_t giu_lock;
80 static unsigned long giu_flags;
81 static unsigned int giu_nr_pins;
83 static void __iomem *giu_base;
85 #define giu_read(offset) readw(giu_base + (offset))
86 #define giu_write(offset, value) writew((value), giu_base + (offset))
88 #define GPIO_PIN_OF_IRQ(irq) ((irq) - GIU_IRQ_BASE)
89 #define GIUINT_HIGH_OFFSET 16
90 #define GIUINT_HIGH_MAX 32
92 static inline uint16_t giu_set(uint16_t offset, uint16_t set)
96 data = giu_read(offset);
98 giu_write(offset, data);
103 static inline uint16_t giu_clear(uint16_t offset, uint16_t clear)
107 data = giu_read(offset);
109 giu_write(offset, data);
114 static void ack_giuint_low(unsigned int irq)
116 giu_write(GIUINTSTATL, 1 << GPIO_PIN_OF_IRQ(irq));
119 static void mask_giuint_low(unsigned int irq)
121 giu_clear(GIUINTENL, 1 << GPIO_PIN_OF_IRQ(irq));
124 static void mask_ack_giuint_low(unsigned int irq)
128 pin = GPIO_PIN_OF_IRQ(irq);
129 giu_clear(GIUINTENL, 1 << pin);
130 giu_write(GIUINTSTATL, 1 << pin);
133 static void unmask_giuint_low(unsigned int irq)
135 giu_set(GIUINTENL, 1 << GPIO_PIN_OF_IRQ(irq));
138 static struct irq_chip giuint_low_irq_chip = {
140 .ack = ack_giuint_low,
141 .mask = mask_giuint_low,
142 .mask_ack = mask_ack_giuint_low,
143 .unmask = unmask_giuint_low,
146 static void ack_giuint_high(unsigned int irq)
148 giu_write(GIUINTSTATH, 1 << (GPIO_PIN_OF_IRQ(irq) - GIUINT_HIGH_OFFSET));
151 static void mask_giuint_high(unsigned int irq)
153 giu_clear(GIUINTENH, 1 << (GPIO_PIN_OF_IRQ(irq) - GIUINT_HIGH_OFFSET));
156 static void mask_ack_giuint_high(unsigned int irq)
160 pin = GPIO_PIN_OF_IRQ(irq) - GIUINT_HIGH_OFFSET;
161 giu_clear(GIUINTENH, 1 << pin);
162 giu_write(GIUINTSTATH, 1 << pin);
165 static void unmask_giuint_high(unsigned int irq)
167 giu_set(GIUINTENH, 1 << (GPIO_PIN_OF_IRQ(irq) - GIUINT_HIGH_OFFSET));
170 static struct irq_chip giuint_high_irq_chip = {
172 .ack = ack_giuint_high,
173 .mask = mask_giuint_high,
174 .mask_ack = mask_ack_giuint_high,
175 .unmask = unmask_giuint_high,
178 static int giu_get_irq(unsigned int irq)
180 uint16_t pendl, pendh, maskl, maskh;
183 pendl = giu_read(GIUINTSTATL);
184 pendh = giu_read(GIUINTSTATH);
185 maskl = giu_read(GIUINTENL);
186 maskh = giu_read(GIUINTENH);
192 for (i = 0; i < 16; i++) {
193 if (maskl & (1 << i))
197 for (i = 0; i < 16; i++) {
198 if (maskh & (1 << i))
199 return GIU_IRQ(i + GIUINT_HIGH_OFFSET);
203 printk(KERN_ERR "spurious GIU interrupt: %04x(%04x),%04x(%04x)\n",
204 maskl, pendl, maskh, pendh);
206 atomic_inc(&irq_err_count);
211 void vr41xx_set_irq_trigger(unsigned int pin, irq_trigger_t trigger, irq_signal_t signal)
215 if (pin < GIUINT_HIGH_OFFSET) {
217 if (trigger != IRQ_TRIGGER_LEVEL) {
218 giu_set(GIUINTTYPL, mask);
219 if (signal == IRQ_SIGNAL_HOLD)
220 giu_set(GIUINTHTSELL, mask);
222 giu_clear(GIUINTHTSELL, mask);
223 if (giu_flags & GPIO_HAS_INTERRUPT_EDGE_SELECT) {
225 case IRQ_TRIGGER_EDGE_FALLING:
226 giu_set(GIUFEDGEINHL, mask);
227 giu_clear(GIUREDGEINHL, mask);
229 case IRQ_TRIGGER_EDGE_RISING:
230 giu_clear(GIUFEDGEINHL, mask);
231 giu_set(GIUREDGEINHL, mask);
234 giu_set(GIUFEDGEINHL, mask);
235 giu_set(GIUREDGEINHL, mask);
239 set_irq_chip_and_handler(GIU_IRQ(pin),
240 &giuint_low_irq_chip,
243 giu_clear(GIUINTTYPL, mask);
244 giu_clear(GIUINTHTSELL, mask);
245 set_irq_chip_and_handler(GIU_IRQ(pin),
246 &giuint_low_irq_chip,
249 giu_write(GIUINTSTATL, mask);
250 } else if (pin < GIUINT_HIGH_MAX) {
251 mask = 1 << (pin - GIUINT_HIGH_OFFSET);
252 if (trigger != IRQ_TRIGGER_LEVEL) {
253 giu_set(GIUINTTYPH, mask);
254 if (signal == IRQ_SIGNAL_HOLD)
255 giu_set(GIUINTHTSELH, mask);
257 giu_clear(GIUINTHTSELH, mask);
258 if (giu_flags & GPIO_HAS_INTERRUPT_EDGE_SELECT) {
260 case IRQ_TRIGGER_EDGE_FALLING:
261 giu_set(GIUFEDGEINHH, mask);
262 giu_clear(GIUREDGEINHH, mask);
264 case IRQ_TRIGGER_EDGE_RISING:
265 giu_clear(GIUFEDGEINHH, mask);
266 giu_set(GIUREDGEINHH, mask);
269 giu_set(GIUFEDGEINHH, mask);
270 giu_set(GIUREDGEINHH, mask);
274 set_irq_chip_and_handler(GIU_IRQ(pin),
275 &giuint_high_irq_chip,
278 giu_clear(GIUINTTYPH, mask);
279 giu_clear(GIUINTHTSELH, mask);
280 set_irq_chip_and_handler(GIU_IRQ(pin),
281 &giuint_high_irq_chip,
284 giu_write(GIUINTSTATH, mask);
287 EXPORT_SYMBOL_GPL(vr41xx_set_irq_trigger);
289 void vr41xx_set_irq_level(unsigned int pin, irq_level_t level)
293 if (pin < GIUINT_HIGH_OFFSET) {
295 if (level == IRQ_LEVEL_HIGH)
296 giu_set(GIUINTALSELL, mask);
298 giu_clear(GIUINTALSELL, mask);
299 giu_write(GIUINTSTATL, mask);
300 } else if (pin < GIUINT_HIGH_MAX) {
301 mask = 1 << (pin - GIUINT_HIGH_OFFSET);
302 if (level == IRQ_LEVEL_HIGH)
303 giu_set(GIUINTALSELH, mask);
305 giu_clear(GIUINTALSELH, mask);
306 giu_write(GIUINTSTATH, mask);
309 EXPORT_SYMBOL_GPL(vr41xx_set_irq_level);
311 gpio_data_t vr41xx_gpio_get_pin(unsigned int pin)
315 if (pin >= giu_nr_pins)
316 return GPIO_DATA_INVAL;
319 reg = giu_read(GIUPIODL);
320 mask = (uint16_t)1 << pin;
321 } else if (pin < 32) {
322 reg = giu_read(GIUPIODH);
323 mask = (uint16_t)1 << (pin - 16);
324 } else if (pin < 48) {
325 reg = giu_read(GIUPODATL);
326 mask = (uint16_t)1 << (pin - 32);
328 reg = giu_read(GIUPODATH);
329 mask = (uint16_t)1 << (pin - 48);
333 return GPIO_DATA_HIGH;
335 return GPIO_DATA_LOW;
337 EXPORT_SYMBOL_GPL(vr41xx_gpio_get_pin);
339 int vr41xx_gpio_set_pin(unsigned int pin, gpio_data_t data)
341 uint16_t offset, mask, reg;
344 if (pin >= giu_nr_pins)
349 mask = (uint16_t)1 << pin;
350 } else if (pin < 32) {
352 mask = (uint16_t)1 << (pin - 16);
353 } else if (pin < 48) {
355 mask = (uint16_t)1 << (pin - 32);
358 mask = (uint16_t)1 << (pin - 48);
361 spin_lock_irqsave(&giu_lock, flags);
363 reg = giu_read(offset);
364 if (data == GPIO_DATA_HIGH)
368 giu_write(offset, reg);
370 spin_unlock_irqrestore(&giu_lock, flags);
374 EXPORT_SYMBOL_GPL(vr41xx_gpio_set_pin);
376 int vr41xx_gpio_set_direction(unsigned int pin, gpio_direction_t dir)
378 uint16_t offset, mask, reg;
381 if (pin >= giu_nr_pins)
386 mask = (uint16_t)1 << pin;
387 } else if (pin < 32) {
389 mask = (uint16_t)1 << (pin - 16);
391 if (giu_flags & GPIO_HAS_OUTPUT_ENABLE) {
393 mask = (uint16_t)1 << (pin - 32);
410 spin_lock_irqsave(&giu_lock, flags);
412 reg = giu_read(offset);
413 if (dir == GPIO_OUTPUT)
417 giu_write(offset, reg);
419 spin_unlock_irqrestore(&giu_lock, flags);
423 EXPORT_SYMBOL_GPL(vr41xx_gpio_set_direction);
425 int vr41xx_gpio_pullupdown(unsigned int pin, gpio_pull_t pull)
430 if ((giu_flags & GPIO_HAS_PULLUPDOWN_IO) != GPIO_HAS_PULLUPDOWN_IO)
436 mask = (uint16_t)1 << pin;
438 spin_lock_irqsave(&giu_lock, flags);
440 if (pull == GPIO_PULL_UP || pull == GPIO_PULL_DOWN) {
441 reg = giu_read(GIUTERMUPDN);
442 if (pull == GPIO_PULL_UP)
446 giu_write(GIUTERMUPDN, reg);
448 reg = giu_read(GIUUSEUPDN);
450 giu_write(GIUUSEUPDN, reg);
452 reg = giu_read(GIUUSEUPDN);
454 giu_write(GIUUSEUPDN, reg);
457 spin_unlock_irqrestore(&giu_lock, flags);
461 EXPORT_SYMBOL_GPL(vr41xx_gpio_pullupdown);
463 static ssize_t gpio_read(struct file *file, char __user *buf, size_t len,
469 pin = iminor(file->f_path.dentry->d_inode);
470 if (pin >= giu_nr_pins)
473 if (vr41xx_gpio_get_pin(pin) == GPIO_DATA_HIGH)
479 if (put_user(value, buf))
485 static ssize_t gpio_write(struct file *file, const char __user *data,
486 size_t len, loff_t *ppos)
493 pin = iminor(file->f_path.dentry->d_inode);
494 if (pin >= giu_nr_pins)
497 for (i = 0; i < len; i++) {
498 if (get_user(c, data + i))
503 retval = vr41xx_gpio_set_pin(pin, GPIO_DATA_LOW);
506 retval = vr41xx_gpio_set_pin(pin, GPIO_DATA_HIGH);
509 printk(KERN_INFO "GPIO%d: pull down\n", pin);
510 retval = vr41xx_gpio_pullupdown(pin, GPIO_PULL_DOWN);
513 printk(KERN_INFO "GPIO%d: pull up/down disable\n", pin);
514 retval = vr41xx_gpio_pullupdown(pin, GPIO_PULL_DISABLE);
517 printk(KERN_INFO "GPIO%d: input\n", pin);
518 retval = vr41xx_gpio_set_direction(pin, GPIO_INPUT);
521 printk(KERN_INFO "GPIO%d: output\n", pin);
522 retval = vr41xx_gpio_set_direction(pin, GPIO_OUTPUT);
525 printk(KERN_INFO "GPIO%d: output disable\n", pin);
526 retval = vr41xx_gpio_set_direction(pin, GPIO_OUTPUT_DISABLE);
529 printk(KERN_INFO "GPIO%d: pull up\n", pin);
530 retval = vr41xx_gpio_pullupdown(pin, GPIO_PULL_UP);
533 printk(KERN_INFO "GPIO%d: pull up/down disable\n", pin);
534 retval = vr41xx_gpio_pullupdown(pin, GPIO_PULL_DISABLE);
547 static int gpio_open(struct inode *inode, struct file *file)
553 if (pin >= giu_nr_pins)
556 return nonseekable_open(inode, file);
559 static int gpio_release(struct inode *inode, struct file *file)
564 if (pin >= giu_nr_pins)
570 static const struct file_operations gpio_fops = {
571 .owner = THIS_MODULE,
575 .release = gpio_release,
578 static int __devinit giu_probe(struct platform_device *dev)
580 struct resource *res;
581 unsigned int trigger, i, pin;
582 struct irq_chip *chip;
586 case GPIO_50PINS_PULLUPDOWN:
587 giu_flags = GPIO_HAS_PULLUPDOWN_IO;
593 case GPIO_48PINS_EDGE_SELECT:
594 giu_flags = GPIO_HAS_INTERRUPT_EDGE_SELECT;
598 printk(KERN_ERR "GIU: unknown ID %d\n", dev->id);
602 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
606 giu_base = ioremap(res->start, res->end - res->start + 1);
610 retval = register_chrdev(major, "GIU", &gpio_fops);
619 printk(KERN_INFO "GIU: major number %d\n", major);
622 spin_lock_init(&giu_lock);
624 giu_write(GIUINTENL, 0);
625 giu_write(GIUINTENH, 0);
627 trigger = giu_read(GIUINTTYPH) << 16;
628 trigger |= giu_read(GIUINTTYPL);
629 for (i = GIU_IRQ_BASE; i <= GIU_IRQ_LAST; i++) {
630 pin = GPIO_PIN_OF_IRQ(i);
631 if (pin < GIUINT_HIGH_OFFSET)
632 chip = &giuint_low_irq_chip;
634 chip = &giuint_high_irq_chip;
636 if (trigger & (1 << pin))
637 set_irq_chip_and_handler(i, chip, handle_edge_irq);
639 set_irq_chip_and_handler(i, chip, handle_level_irq);
643 irq = platform_get_irq(dev, 0);
644 if (irq < 0 || irq >= nr_irqs)
647 return cascade_irq(irq, giu_get_irq);
650 static int __devexit giu_remove(struct platform_device *dev)
660 static struct platform_driver giu_device_driver = {
662 .remove = __devexit_p(giu_remove),
665 .owner = THIS_MODULE,
669 static int __init vr41xx_giu_init(void)
671 return platform_driver_register(&giu_device_driver);
674 static void __exit vr41xx_giu_exit(void)
676 platform_driver_unregister(&giu_device_driver);
679 module_init(vr41xx_giu_init);
680 module_exit(vr41xx_giu_exit);