Merge with Linux 2.5.73.
[linux-2.6/linux-mips.git] / drivers / input / serio / ambakmi.c
blob7112bdcda8f86b95f56802589a1424c1cc0b2488
1 /*
2 * linux/drivers/input/serio/ambakmi.c
4 * Copyright (C) 2000-2003 Deep Blue Solutions Ltd.
5 * Copyright (C) 2002 Russell King.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/serio.h>
15 #include <linux/errno.h>
16 #include <linux/interrupt.h>
17 #include <linux/ioport.h>
18 #include <linux/device.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
22 #include <asm/io.h>
23 #include <asm/irq.h>
24 #include <asm/hardware/amba.h>
25 #include <asm/hardware/amba_kmi.h>
27 #define KMI_BASE (kmi->base)
29 struct amba_kmi_port {
30 struct serio io;
31 struct amba_kmi_port *next;
32 unsigned char *base;
33 unsigned int irq;
34 unsigned int divisor;
35 unsigned int open;
36 struct resource *res;
39 static irqreturn_t amba_kmi_int(int irq, void *dev_id, struct pt_regs *regs)
41 struct amba_kmi_port *kmi = dev_id;
42 unsigned int status = readb(KMIIR);
43 int handled = IRQ_NONE;
45 while (status & KMIIR_RXINTR) {
46 serio_interrupt(&kmi->io, readb(KMIDATA), 0, regs);
47 status = readb(KMIIR);
48 handled = IRQ_HANDLED;
51 return handled;
54 static int amba_kmi_write(struct serio *io, unsigned char val)
56 struct amba_kmi_port *kmi = io->driver;
57 unsigned int timeleft = 10000; /* timeout in 100ms */
59 while ((readb(KMISTAT) & KMISTAT_TXEMPTY) == 0 && timeleft--)
60 udelay(10);
62 if (timeleft)
63 writeb(val, KMIDATA);
65 return timeleft ? 0 : SERIO_TIMEOUT;
68 static int amba_kmi_open(struct serio *io)
70 struct amba_kmi_port *kmi = io->driver;
71 int ret;
73 writeb(kmi->divisor, KMICLKDIV);
74 writeb(KMICR_EN, KMICR);
76 ret = request_irq(kmi->irq, amba_kmi_int, 0, kmi->io.phys, kmi);
77 if (ret) {
78 printk(KERN_ERR "kmi: failed to claim IRQ%d\n", kmi->irq);
79 writeb(0, KMICR);
80 return ret;
83 writeb(KMICR_EN | KMICR_RXINTREN, KMICR);
85 return 0;
88 static void amba_kmi_close(struct serio *io)
90 struct amba_kmi_port *kmi = io->driver;
92 writeb(0, KMICR);
94 free_irq(kmi->irq, kmi);
97 static int amba_kmi_probe(struct amba_device *dev, void *id)
99 struct amba_kmi_port *kmi;
101 kmi = kmalloc(sizeof(struct amba_kmi_port), GFP_KERNEL);
102 if (!kmi)
103 return -ENOMEM;
105 memset(kmi, 0, sizeof(struct amba_kmi_port));
107 kmi->io.type = SERIO_8042;
108 kmi->io.write = amba_kmi_write;
109 kmi->io.open = amba_kmi_open;
110 kmi->io.close = amba_kmi_close;
111 kmi->io.name = dev->dev.name;
112 kmi->io.phys = dev->dev.bus_id;
113 kmi->io.driver = kmi;
115 kmi->res = request_mem_region(dev->res.start, KMI_SIZE, kmi->io.phys);
116 if (!kmi->res) {
117 kfree(kmi);
118 return -EBUSY;
121 kmi->base = ioremap(dev->res.start, KMI_SIZE);
122 if (!kmi->base) {
123 release_resource(kmi->res);
124 kfree(kmi);
125 return -ENOMEM;
128 kmi->irq = dev->irq;
129 kmi->divisor = 24 / 8 - 1;
131 amba_set_drvdata(dev, kmi);
133 serio_register_port(&kmi->io);
134 return 0;
137 static int amba_kmi_remove(struct amba_device *dev)
139 struct amba_kmi_port *kmi = amba_get_drvdata(dev);
141 amba_set_drvdata(dev, NULL);
143 serio_unregister_port(&kmi->io);
144 iounmap(kmi->base);
145 release_resource(kmi->res);
146 kfree(kmi);
147 return 0;
150 static int amba_kmi_resume(struct amba_device *dev, u32 level)
152 struct amba_kmi_port *kmi = amba_get_drvdata(dev);
154 if (level == RESUME_ENABLE) {
155 /* kick the serio layer to rescan this port */
156 serio_rescan(&kmi->io);
159 return 0;
162 static struct amba_id amba_kmi_idtable[] = {
164 .id = 0x00041050,
165 .mask = 0x000fffff,
167 { 0, 0 }
170 static struct amba_driver ambakmi_driver = {
171 .drv = {
172 .name = "kmi-pl050",
174 .id_table = amba_kmi_idtable,
175 .probe = amba_kmi_probe,
176 .remove = amba_kmi_remove,
177 .resume = amba_kmi_resume,
180 static int __init amba_kmi_init(void)
182 return amba_driver_register(&ambakmi_driver);
185 static void __exit amba_kmi_exit(void)
187 return amba_driver_unregister(&ambakmi_driver);
190 module_init(amba_kmi_init);
191 module_exit(amba_kmi_exit);
193 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
194 MODULE_DESCRIPTION("AMBA KMI controller driver");
195 MODULE_LICENSE("GPL");