Remove all #inclusions of asm/system.h
[linux-2.6.git] / drivers / input / serio / sa1111ps2.c
blobe3c85fafe93752100e483d0297eb066d63d96ea3
1 /*
2 * linux/drivers/input/serio/sa1111ps2.c
4 * Copyright (C) 2002 Russell King
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>
22 #include <asm/io.h>
24 #include <asm/hardware/sa1111.h>
26 struct ps2if {
27 struct serio *io;
28 struct sa1111_dev *dev;
29 void __iomem *base;
30 unsigned int open;
31 spinlock_t lock;
32 unsigned int head;
33 unsigned int tail;
34 unsigned char buf[4];
38 * Read all bytes waiting in the PS2 port. There should be
39 * at the most one, but we loop for safety. If there was a
40 * framing error, we have to manually clear the status.
42 static irqreturn_t ps2_rxint(int irq, void *dev_id)
44 struct ps2if *ps2if = dev_id;
45 unsigned int scancode, flag, status;
47 status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
48 while (status & PS2STAT_RXF) {
49 if (status & PS2STAT_STP)
50 sa1111_writel(PS2STAT_STP, ps2if->base + SA1111_PS2STAT);
52 flag = (status & PS2STAT_STP ? SERIO_FRAME : 0) |
53 (status & PS2STAT_RXP ? 0 : SERIO_PARITY);
55 scancode = sa1111_readl(ps2if->base + SA1111_PS2DATA) & 0xff;
57 if (hweight8(scancode) & 1)
58 flag ^= SERIO_PARITY;
60 serio_interrupt(ps2if->io, scancode, flag);
62 status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
65 return IRQ_HANDLED;
69 * Completion of ps2 write
71 static irqreturn_t ps2_txint(int irq, void *dev_id)
73 struct ps2if *ps2if = dev_id;
74 unsigned int status;
76 spin_lock(&ps2if->lock);
77 status = sa1111_readl(ps2if->base + SA1111_PS2STAT);
78 if (ps2if->head == ps2if->tail) {
79 disable_irq_nosync(irq);
80 /* done */
81 } else if (status & PS2STAT_TXE) {
82 sa1111_writel(ps2if->buf[ps2if->tail], ps2if->base + SA1111_PS2DATA);
83 ps2if->tail = (ps2if->tail + 1) & (sizeof(ps2if->buf) - 1);
85 spin_unlock(&ps2if->lock);
87 return IRQ_HANDLED;
91 * Write a byte to the PS2 port. We have to wait for the
92 * port to indicate that the transmitter is empty.
94 static int ps2_write(struct serio *io, unsigned char val)
96 struct ps2if *ps2if = io->port_data;
97 unsigned long flags;
98 unsigned int head;
100 spin_lock_irqsave(&ps2if->lock, flags);
103 * If the TX register is empty, we can go straight out.
105 if (sa1111_readl(ps2if->base + SA1111_PS2STAT) & PS2STAT_TXE) {
106 sa1111_writel(val, ps2if->base + SA1111_PS2DATA);
107 } else {
108 if (ps2if->head == ps2if->tail)
109 enable_irq(ps2if->dev->irq[1]);
110 head = (ps2if->head + 1) & (sizeof(ps2if->buf) - 1);
111 if (head != ps2if->tail) {
112 ps2if->buf[ps2if->head] = val;
113 ps2if->head = head;
117 spin_unlock_irqrestore(&ps2if->lock, flags);
118 return 0;
121 static int ps2_open(struct serio *io)
123 struct ps2if *ps2if = io->port_data;
124 int ret;
126 sa1111_enable_device(ps2if->dev);
128 ret = request_irq(ps2if->dev->irq[0], ps2_rxint, 0,
129 SA1111_DRIVER_NAME(ps2if->dev), ps2if);
130 if (ret) {
131 printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
132 ps2if->dev->irq[0], ret);
133 return ret;
136 ret = request_irq(ps2if->dev->irq[1], ps2_txint, 0,
137 SA1111_DRIVER_NAME(ps2if->dev), ps2if);
138 if (ret) {
139 printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
140 ps2if->dev->irq[1], ret);
141 free_irq(ps2if->dev->irq[0], ps2if);
142 return ret;
145 ps2if->open = 1;
147 enable_irq_wake(ps2if->dev->irq[0]);
149 sa1111_writel(PS2CR_ENA, ps2if->base + SA1111_PS2CR);
150 return 0;
153 static void ps2_close(struct serio *io)
155 struct ps2if *ps2if = io->port_data;
157 sa1111_writel(0, ps2if->base + SA1111_PS2CR);
159 disable_irq_wake(ps2if->dev->irq[0]);
161 ps2if->open = 0;
163 free_irq(ps2if->dev->irq[1], ps2if);
164 free_irq(ps2if->dev->irq[0], ps2if);
166 sa1111_disable_device(ps2if->dev);
170 * Clear the input buffer.
172 static void __devinit ps2_clear_input(struct ps2if *ps2if)
174 int maxread = 100;
176 while (maxread--) {
177 if ((sa1111_readl(ps2if->base + SA1111_PS2DATA) & 0xff) == 0xff)
178 break;
182 static unsigned int __devinit ps2_test_one(struct ps2if *ps2if,
183 unsigned int mask)
185 unsigned int val;
187 sa1111_writel(PS2CR_ENA | mask, ps2if->base + SA1111_PS2CR);
189 udelay(2);
191 val = sa1111_readl(ps2if->base + SA1111_PS2STAT);
192 return val & (PS2STAT_KBC | PS2STAT_KBD);
196 * Test the keyboard interface. We basically check to make sure that
197 * we can drive each line to the keyboard independently of each other.
199 static int __devinit ps2_test(struct ps2if *ps2if)
201 unsigned int stat;
202 int ret = 0;
204 stat = ps2_test_one(ps2if, PS2CR_FKC);
205 if (stat != PS2STAT_KBD) {
206 printk("PS/2 interface test failed[1]: %02x\n", stat);
207 ret = -ENODEV;
210 stat = ps2_test_one(ps2if, 0);
211 if (stat != (PS2STAT_KBC | PS2STAT_KBD)) {
212 printk("PS/2 interface test failed[2]: %02x\n", stat);
213 ret = -ENODEV;
216 stat = ps2_test_one(ps2if, PS2CR_FKD);
217 if (stat != PS2STAT_KBC) {
218 printk("PS/2 interface test failed[3]: %02x\n", stat);
219 ret = -ENODEV;
222 sa1111_writel(0, ps2if->base + SA1111_PS2CR);
224 return ret;
228 * Add one device to this driver.
230 static int __devinit ps2_probe(struct sa1111_dev *dev)
232 struct ps2if *ps2if;
233 struct serio *serio;
234 int ret;
236 ps2if = kzalloc(sizeof(struct ps2if), GFP_KERNEL);
237 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
238 if (!ps2if || !serio) {
239 ret = -ENOMEM;
240 goto free;
244 serio->id.type = SERIO_8042;
245 serio->write = ps2_write;
246 serio->open = ps2_open;
247 serio->close = ps2_close;
248 strlcpy(serio->name, dev_name(&dev->dev), sizeof(serio->name));
249 strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
250 serio->port_data = ps2if;
251 serio->dev.parent = &dev->dev;
252 ps2if->io = serio;
253 ps2if->dev = dev;
254 sa1111_set_drvdata(dev, ps2if);
256 spin_lock_init(&ps2if->lock);
259 * Request the physical region for this PS2 port.
261 if (!request_mem_region(dev->res.start,
262 dev->res.end - dev->res.start + 1,
263 SA1111_DRIVER_NAME(dev))) {
264 ret = -EBUSY;
265 goto free;
269 * Our parent device has already mapped the region.
271 ps2if->base = dev->mapbase;
273 sa1111_enable_device(ps2if->dev);
275 /* Incoming clock is 8MHz */
276 sa1111_writel(0, ps2if->base + SA1111_PS2CLKDIV);
277 sa1111_writel(127, ps2if->base + SA1111_PS2PRECNT);
280 * Flush any pending input.
282 ps2_clear_input(ps2if);
285 * Test the keyboard interface.
287 ret = ps2_test(ps2if);
288 if (ret)
289 goto out;
292 * Flush any pending input.
294 ps2_clear_input(ps2if);
296 sa1111_disable_device(ps2if->dev);
297 serio_register_port(ps2if->io);
298 return 0;
300 out:
301 sa1111_disable_device(ps2if->dev);
302 release_mem_region(dev->res.start, resource_size(&dev->res));
303 free:
304 sa1111_set_drvdata(dev, NULL);
305 kfree(ps2if);
306 kfree(serio);
307 return ret;
311 * Remove one device from this driver.
313 static int __devexit ps2_remove(struct sa1111_dev *dev)
315 struct ps2if *ps2if = sa1111_get_drvdata(dev);
317 serio_unregister_port(ps2if->io);
318 release_mem_region(dev->res.start, resource_size(&dev->res));
319 sa1111_set_drvdata(dev, NULL);
321 kfree(ps2if);
323 return 0;
327 * Our device driver structure
329 static struct sa1111_driver ps2_driver = {
330 .drv = {
331 .name = "sa1111-ps2",
333 .devid = SA1111_DEVID_PS2,
334 .probe = ps2_probe,
335 .remove = __devexit_p(ps2_remove),
338 static int __init ps2_init(void)
340 return sa1111_driver_register(&ps2_driver);
343 static void __exit ps2_exit(void)
345 sa1111_driver_unregister(&ps2_driver);
348 module_init(ps2_init);
349 module_exit(ps2_exit);
351 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
352 MODULE_DESCRIPTION("SA1111 PS2 controller driver");
353 MODULE_LICENSE("GPL");