ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / input / serio / sa1111ps2.c
1 /*
2  *  linux/drivers/input/serio/sa1111ps2.c
3  *
4  *  Copyright (C) 2002 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/input.h>
13 #include <linux/serio.h>
14 #include <linux/errno.h>
15 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21
22 #include <asm/io.h>
23 #include <asm/irq.h>
24 #include <asm/system.h>
25
26 #include <asm/hardware/sa1111.h>
27
28 struct ps2if {
29         struct serio            io;
30         struct sa1111_dev       *dev;
31         unsigned long           base;
32         unsigned int            open;
33         spinlock_t              lock;
34         unsigned int            head;
35         unsigned int            tail;
36         unsigned char           buf[4];
37 };
38
39 /*
40  * Read all bytes waiting in the PS2 port.  There should be
41  * at the most one, but we loop for safety.  If there was a
42  * framing error, we have to manually clear the status.
43  */
44 static irqreturn_t ps2_rxint(int irq, void *dev_id, struct pt_regs *regs)
45 {
46         struct ps2if *ps2if = dev_id;
47         unsigned int scancode, flag, status;
48         int handled = IRQ_NONE;
49
50         status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
51         while (status & PS2STAT_RXF) {
52                 if (status & PS2STAT_STP)
53                         sa1111_writel(PS2STAT_STP, ps2if->base + SA1111_PS2STAT);
54
55                 flag = (status & PS2STAT_STP ? SERIO_FRAME : 0) |
56                        (status & PS2STAT_RXP ? 0 : SERIO_PARITY);
57
58                 scancode = sa1111_readl(ps2if->base + SA1111_PS2DATA) & 0xff;
59
60                 if (hweight8(scancode) & 1)
61                         flag ^= SERIO_PARITY;
62
63                 serio_interrupt(&ps2if->io, scancode, flag, regs);
64
65                 status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
66
67                 handled = IRQ_HANDLED;
68         }
69
70         return handled;
71 }
72
73 /*
74  * Completion of ps2 write
75  */
76 static irqreturn_t ps2_txint(int irq, void *dev_id, struct pt_regs *regs)
77 {
78         struct ps2if *ps2if = dev_id;
79         unsigned int status;
80
81         spin_lock(&ps2if->lock);
82         status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
83         if (ps2if->head == ps2if->tail) {
84                 disable_irq(irq);
85                 /* done */
86         } else if (status & PS2STAT_TXE) {
87                 sa1111_writel(ps2if->buf[ps2if->tail], ps2if->base + SA1111_PS2DATA);
88                 ps2if->tail = (ps2if->tail + 1) & (sizeof(ps2if->buf) - 1);
89         }
90         spin_unlock(&ps2if->lock);
91
92         return IRQ_HANDLED;
93 }
94
95 /*
96  * Write a byte to the PS2 port.  We have to wait for the
97  * port to indicate that the transmitter is empty.
98  */
99 static int ps2_write(struct serio *io, unsigned char val)
100 {
101         struct ps2if *ps2if = io->driver;
102         unsigned long flags;
103         unsigned int head;
104
105         spin_lock_irqsave(&ps2if->lock, flags);
106
107         /*
108          * If the TX register is empty, we can go straight out.
109          */
110         if (sa1111_readl(ps2if->base + SA1111_PS2STAT) & PS2STAT_TXE) {
111                 sa1111_writel(val, ps2if->base + SA1111_PS2DATA);
112         } else {
113                 if (ps2if->head == ps2if->tail)
114                         enable_irq(ps2if->dev->irq[1]);
115                 head = (ps2if->head + 1) & (sizeof(ps2if->buf) - 1);
116                 if (head != ps2if->tail) {
117                         ps2if->buf[ps2if->head] = val;
118                         ps2if->head = head;
119                 }
120         }
121
122         spin_unlock_irqrestore(&ps2if->lock, flags);
123         return 0;
124 }
125
126 static int ps2_open(struct serio *io)
127 {
128         struct ps2if *ps2if = io->driver;
129         int ret;
130
131         sa1111_enable_device(ps2if->dev);
132
133         ret = request_irq(ps2if->dev->irq[0], ps2_rxint, 0,
134                           SA1111_DRIVER_NAME(ps2if->dev), ps2if);
135         if (ret) {
136                 printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
137                         ps2if->dev->irq[0], ret);
138                 return ret;
139         }
140
141         ret = request_irq(ps2if->dev->irq[1], ps2_txint, 0,
142                           SA1111_DRIVER_NAME(ps2if->dev), ps2if);
143         if (ret) {
144                 printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
145                         ps2if->dev->irq[1], ret);
146                 free_irq(ps2if->dev->irq[0], ps2if);
147                 return ret;
148         }
149
150         ps2if->open = 1;
151
152         enable_irq_wake(ps2if->dev->irq[0]);
153
154         sa1111_writel(PS2CR_ENA, ps2if->base + SA1111_PS2CR);
155         return 0;
156 }
157
158 static void ps2_close(struct serio *io)
159 {
160         struct ps2if *ps2if = io->driver;
161
162         sa1111_writel(0, ps2if->base + SA1111_PS2CR);
163
164         disable_irq_wake(ps2if->dev->irq[0]);
165
166         ps2if->open = 0;
167
168         free_irq(ps2if->dev->irq[1], ps2if);
169         free_irq(ps2if->dev->irq[0], ps2if);
170
171         sa1111_disable_device(ps2if->dev);
172 }
173
174 /*
175  * Clear the input buffer.
176  */
177 static void __init ps2_clear_input(struct ps2if *ps2if)
178 {
179         int maxread = 100;
180
181         while (maxread--) {
182                 if ((sa1111_readl(ps2if->base + SA1111_PS2DATA) & 0xff) == 0xff)
183                         break;
184         }
185 }
186
187 static inline unsigned int
188 ps2_test_one(struct ps2if *ps2if, unsigned int mask)
189 {
190         unsigned int val;
191
192         sa1111_writel(PS2CR_ENA | mask, ps2if->base + SA1111_PS2CR);
193
194         udelay(2);
195
196         val = sa1111_readl(ps2if->base + SA1111_PS2STAT);
197         return val & (PS2STAT_KBC | PS2STAT_KBD);
198 }
199
200 /*
201  * Test the keyboard interface.  We basically check to make sure that
202  * we can drive each line to the keyboard independently of each other.
203  */
204 static int __init ps2_test(struct ps2if *ps2if)
205 {
206         unsigned int stat;
207         int ret = 0;
208
209         stat = ps2_test_one(ps2if, PS2CR_FKC);
210         if (stat != PS2STAT_KBD) {
211                 printk("PS/2 interface test failed[1]: %02x\n", stat);
212                 ret = -ENODEV;
213         }
214
215         stat = ps2_test_one(ps2if, 0);
216         if (stat != (PS2STAT_KBC | PS2STAT_KBD)) {
217                 printk("PS/2 interface test failed[2]: %02x\n", stat);
218                 ret = -ENODEV;
219         }
220
221         stat = ps2_test_one(ps2if, PS2CR_FKD);
222         if (stat != PS2STAT_KBC) {
223                 printk("PS/2 interface test failed[3]: %02x\n", stat);
224                 ret = -ENODEV;
225         }
226
227         sa1111_writel(0, ps2if->base + SA1111_PS2CR);
228
229         return ret;
230 }
231
232 /*
233  * Add one device to this driver.
234  */
235 static int ps2_probe(struct sa1111_dev *dev)
236 {
237         struct ps2if *ps2if;
238         int ret;
239
240         ps2if = kmalloc(sizeof(struct ps2if), GFP_KERNEL);
241         if (!ps2if) {
242                 return -ENOMEM;
243         }
244
245         memset(ps2if, 0, sizeof(struct ps2if));
246
247         ps2if->io.type          = SERIO_8042;
248         ps2if->io.write         = ps2_write;
249         ps2if->io.open          = ps2_open;
250         ps2if->io.close         = ps2_close;
251         ps2if->io.name          = dev->dev.bus_id;
252         ps2if->io.phys          = dev->dev.bus_id;
253         ps2if->io.driver        = ps2if;
254         ps2if->dev              = dev;
255         sa1111_set_drvdata(dev, ps2if);
256
257         spin_lock_init(&ps2if->lock);
258
259         /*
260          * Request the physical region for this PS2 port.
261          */
262         if (!request_mem_region(dev->res.start,
263                                 dev->res.end - dev->res.start + 1,
264                                 SA1111_DRIVER_NAME(dev))) {
265                 ret = -EBUSY;
266                 goto free;
267         }
268
269         /*
270          * Our parent device has already mapped the region.
271          */
272         ps2if->base = (unsigned long)dev->mapbase;
273
274         sa1111_enable_device(ps2if->dev);
275
276         /* Incoming clock is 8MHz */
277         sa1111_writel(0, ps2if->base + SA1111_PS2CLKDIV);
278         sa1111_writel(127, ps2if->base + SA1111_PS2PRECNT);
279
280         /*
281          * Flush any pending input.
282          */
283         ps2_clear_input(ps2if);
284
285         /*
286          * Test the keyboard interface.
287          */
288         ret = ps2_test(ps2if);
289         if (ret)
290                 goto out;
291
292         /*
293          * Flush any pending input.
294          */
295         ps2_clear_input(ps2if);
296
297         sa1111_disable_device(ps2if->dev);
298         serio_register_port(&ps2if->io);
299         return 0;
300
301  out:
302         sa1111_disable_device(ps2if->dev);
303         release_mem_region(dev->res.start,
304                            dev->res.end - dev->res.start + 1);
305  free:
306         sa1111_set_drvdata(dev, NULL);
307         kfree(ps2if);
308         return ret;
309 }
310
311 /*
312  * Remove one device from this driver.
313  */
314 static int ps2_remove(struct sa1111_dev *dev)
315 {
316         struct ps2if *ps2if = sa1111_get_drvdata(dev);
317
318         serio_unregister_port(&ps2if->io);
319         release_mem_region(dev->res.start,
320                            dev->res.end - dev->res.start + 1);
321         sa1111_set_drvdata(dev, NULL);
322
323         kfree(ps2if);
324
325         return 0;
326 }
327
328 /*
329  * Our device driver structure
330  */
331 static struct sa1111_driver ps2_driver = {
332         .drv = {
333                 .name   = "sa1111-ps2",
334         },
335         .devid          = SA1111_DEVID_PS2,
336         .probe          = ps2_probe,
337         .remove         = ps2_remove,
338 };
339
340 static int __init ps2_init(void)
341 {
342         return sa1111_driver_register(&ps2_driver);
343 }
344
345 static void __exit ps2_exit(void)
346 {
347         sa1111_driver_unregister(&ps2_driver);
348 }
349
350 module_init(ps2_init);
351 module_exit(ps2_exit);
352
353 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
354 MODULE_DESCRIPTION("SA1111 PS2 controller driver");
355 MODULE_LICENSE("GPL");