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