blob: ad7d23b5c6fe12e9853a54024eaeeb7875492215 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/system.h>
24
25#include <asm/hardware/sa1111.h>
26
27struct ps2if {
28 struct serio *io;
29 struct sa1111_dev *dev;
30 void __iomem *base;
31 unsigned int open;
32 spinlock_t lock;
33 unsigned int head;
34 unsigned int tail;
35 unsigned char buf[4];
36};
37
38/*
39 * Read all bytes waiting in the PS2 port. There should be
40 * at the most one, but we loop for safety. If there was a
41 * framing error, we have to manually clear the status.
42 */
David Howells7d12e782006-10-05 14:55:46 +010043static irqreturn_t ps2_rxint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 struct ps2if *ps2if = dev_id;
46 unsigned int scancode, flag, status;
47
48 status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
49 while (status & PS2STAT_RXF) {
50 if (status & PS2STAT_STP)
51 sa1111_writel(PS2STAT_STP, ps2if->base + SA1111_PS2STAT);
52
53 flag = (status & PS2STAT_STP ? SERIO_FRAME : 0) |
54 (status & PS2STAT_RXP ? 0 : SERIO_PARITY);
55
56 scancode = sa1111_readl(ps2if->base + SA1111_PS2DATA) & 0xff;
57
58 if (hweight8(scancode) & 1)
59 flag ^= SERIO_PARITY;
60
David Howells7d12e782006-10-05 14:55:46 +010061 serio_interrupt(ps2if->io, scancode, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
64 }
65
66 return IRQ_HANDLED;
67}
68
69/*
70 * Completion of ps2 write
71 */
David Howells7d12e782006-10-05 14:55:46 +010072static irqreturn_t ps2_txint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 struct ps2if *ps2if = dev_id;
75 unsigned int status;
76
77 spin_lock(&ps2if->lock);
78 status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
79 if (ps2if->head == ps2if->tail) {
Ben Nizettee4bd3e52009-04-17 20:35:57 -070080 disable_irq_nosync(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 /* done */
82 } else if (status & PS2STAT_TXE) {
83 sa1111_writel(ps2if->buf[ps2if->tail], ps2if->base + SA1111_PS2DATA);
84 ps2if->tail = (ps2if->tail + 1) & (sizeof(ps2if->buf) - 1);
85 }
86 spin_unlock(&ps2if->lock);
87
88 return IRQ_HANDLED;
89}
90
91/*
92 * Write a byte to the PS2 port. We have to wait for the
93 * port to indicate that the transmitter is empty.
94 */
95static int ps2_write(struct serio *io, unsigned char val)
96{
97 struct ps2if *ps2if = io->port_data;
98 unsigned long flags;
99 unsigned int head;
100
101 spin_lock_irqsave(&ps2if->lock, flags);
102
103 /*
104 * If the TX register is empty, we can go straight out.
105 */
106 if (sa1111_readl(ps2if->base + SA1111_PS2STAT) & PS2STAT_TXE) {
107 sa1111_writel(val, ps2if->base + SA1111_PS2DATA);
108 } else {
109 if (ps2if->head == ps2if->tail)
110 enable_irq(ps2if->dev->irq[1]);
111 head = (ps2if->head + 1) & (sizeof(ps2if->buf) - 1);
112 if (head != ps2if->tail) {
113 ps2if->buf[ps2if->head] = val;
114 ps2if->head = head;
115 }
116 }
117
118 spin_unlock_irqrestore(&ps2if->lock, flags);
119 return 0;
120}
121
122static int ps2_open(struct serio *io)
123{
124 struct ps2if *ps2if = io->port_data;
125 int ret;
126
Russell Kingae99ddb2012-01-26 13:25:47 +0000127 ret = sa1111_enable_device(ps2if->dev);
128 if (ret)
129 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 ret = request_irq(ps2if->dev->irq[0], ps2_rxint, 0,
132 SA1111_DRIVER_NAME(ps2if->dev), ps2if);
133 if (ret) {
134 printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
135 ps2if->dev->irq[0], ret);
Russell Kingae99ddb2012-01-26 13:25:47 +0000136 sa1111_disable_device(ps2if->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return ret;
138 }
139
140 ret = request_irq(ps2if->dev->irq[1], ps2_txint, 0,
141 SA1111_DRIVER_NAME(ps2if->dev), ps2if);
142 if (ret) {
143 printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
144 ps2if->dev->irq[1], ret);
145 free_irq(ps2if->dev->irq[0], ps2if);
Russell Kingae99ddb2012-01-26 13:25:47 +0000146 sa1111_disable_device(ps2if->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 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
158static void ps2_close(struct serio *io)
159{
160 struct ps2if *ps2if = io->port_data;
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 */
Russell Kingcdea4602007-05-30 17:48:45 +0100177static void __devinit ps2_clear_input(struct ps2if *ps2if)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 int maxread = 100;
180
181 while (maxread--) {
182 if ((sa1111_readl(ps2if->base + SA1111_PS2DATA) & 0xff) == 0xff)
183 break;
184 }
185}
186
Dmitry Torokhov010c33c2009-12-11 21:57:04 -0800187static unsigned int __devinit ps2_test_one(struct ps2if *ps2if,
188 unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
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 */
Dmitry Torokhov010c33c2009-12-11 21:57:04 -0800204static int __devinit ps2_test(struct ps2if *ps2if)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
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 */
Russell Kingcdea4602007-05-30 17:48:45 +0100235static int __devinit ps2_probe(struct sa1111_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct ps2if *ps2if;
238 struct serio *serio;
239 int ret;
240
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700241 ps2if = kzalloc(sizeof(struct ps2if), GFP_KERNEL);
242 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (!ps2if || !serio) {
244 ret = -ENOMEM;
245 goto free;
246 }
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 serio->id.type = SERIO_8042;
250 serio->write = ps2_write;
251 serio->open = ps2_open;
252 serio->close = ps2_close;
Kay Sievers4e8718a2009-01-29 22:56:08 -0800253 strlcpy(serio->name, dev_name(&dev->dev), sizeof(serio->name));
254 strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 serio->port_data = ps2if;
256 serio->dev.parent = &dev->dev;
257 ps2if->io = serio;
258 ps2if->dev = dev;
259 sa1111_set_drvdata(dev, ps2if);
260
261 spin_lock_init(&ps2if->lock);
262
263 /*
264 * Request the physical region for this PS2 port.
265 */
266 if (!request_mem_region(dev->res.start,
267 dev->res.end - dev->res.start + 1,
268 SA1111_DRIVER_NAME(dev))) {
269 ret = -EBUSY;
270 goto free;
271 }
272
273 /*
274 * Our parent device has already mapped the region.
275 */
276 ps2if->base = dev->mapbase;
277
278 sa1111_enable_device(ps2if->dev);
279
280 /* Incoming clock is 8MHz */
281 sa1111_writel(0, ps2if->base + SA1111_PS2CLKDIV);
282 sa1111_writel(127, ps2if->base + SA1111_PS2PRECNT);
283
284 /*
285 * Flush any pending input.
286 */
287 ps2_clear_input(ps2if);
288
289 /*
290 * Test the keyboard interface.
291 */
292 ret = ps2_test(ps2if);
293 if (ret)
294 goto out;
295
296 /*
297 * Flush any pending input.
298 */
299 ps2_clear_input(ps2if);
300
301 sa1111_disable_device(ps2if->dev);
302 serio_register_port(ps2if->io);
303 return 0;
304
305 out:
306 sa1111_disable_device(ps2if->dev);
Joe Perches28f65c12011-06-09 09:13:32 -0700307 release_mem_region(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 free:
309 sa1111_set_drvdata(dev, NULL);
310 kfree(ps2if);
311 kfree(serio);
312 return ret;
313}
314
315/*
316 * Remove one device from this driver.
317 */
Dmitry Torokhov010c33c2009-12-11 21:57:04 -0800318static int __devexit ps2_remove(struct sa1111_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 struct ps2if *ps2if = sa1111_get_drvdata(dev);
321
322 serio_unregister_port(ps2if->io);
Joe Perches28f65c12011-06-09 09:13:32 -0700323 release_mem_region(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 sa1111_set_drvdata(dev, NULL);
325
326 kfree(ps2if);
327
328 return 0;
329}
330
331/*
332 * Our device driver structure
333 */
334static struct sa1111_driver ps2_driver = {
335 .drv = {
336 .name = "sa1111-ps2",
Russell King1ebcd762012-01-26 11:19:48 +0000337 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 },
339 .devid = SA1111_DEVID_PS2,
340 .probe = ps2_probe,
Dmitry Torokhov010c33c2009-12-11 21:57:04 -0800341 .remove = __devexit_p(ps2_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342};
343
344static int __init ps2_init(void)
345{
346 return sa1111_driver_register(&ps2_driver);
347}
348
349static void __exit ps2_exit(void)
350{
351 sa1111_driver_unregister(&ps2_driver);
352}
353
354module_init(ps2_init);
355module_exit(ps2_exit);
356
357MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
358MODULE_DESCRIPTION("SA1111 PS2 controller driver");
359MODULE_LICENSE("GPL");