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.
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>
24 #include <asm/hardware/sa1111.h>
27 #define PS2STAT 0x0004
28 #define PS2DATA 0x0008
29 #define PS2CLKDIV 0x000c
30 #define PS2PRECNT 0x0010
32 #define PS2CR_ENA 0x08
33 #define PS2CR_FKD 0x02
34 #define PS2CR_FKC 0x01
36 #define PS2STAT_STP 0x0100
37 #define PS2STAT_TXE 0x0080
38 #define PS2STAT_TXB 0x0040
39 #define PS2STAT_RXF 0x0020
40 #define PS2STAT_RXB 0x0010
41 #define PS2STAT_ENA 0x0008
42 #define PS2STAT_RXP 0x0004
43 #define PS2STAT_KBD 0x0002
44 #define PS2STAT_KBC 0x0001
48 struct sa1111_dev *dev;
58 * Read all bytes waiting in the PS2 port. There should be
59 * at the most one, but we loop for safety. If there was a
60 * framing error, we have to manually clear the status.
62 static irqreturn_t ps2_rxint(int irq, void *dev_id)
64 struct ps2if *ps2if = dev_id;
65 unsigned int scancode, flag, status;
67 status = sa1111_readl(ps2if->base + PS2STAT);
68 while (status & PS2STAT_RXF) {
69 if (status & PS2STAT_STP)
70 sa1111_writel(PS2STAT_STP, ps2if->base + PS2STAT);
72 flag = (status & PS2STAT_STP ? SERIO_FRAME : 0) |
73 (status & PS2STAT_RXP ? 0 : SERIO_PARITY);
75 scancode = sa1111_readl(ps2if->base + PS2DATA) & 0xff;
77 if (hweight8(scancode) & 1)
80 serio_interrupt(ps2if->io, scancode, flag);
82 status = sa1111_readl(ps2if->base + PS2STAT);
89 * Completion of ps2 write
91 static irqreturn_t ps2_txint(int irq, void *dev_id)
93 struct ps2if *ps2if = dev_id;
96 spin_lock(&ps2if->lock);
97 status = sa1111_readl(ps2if->base + PS2STAT);
98 if (ps2if->head == ps2if->tail) {
99 disable_irq_nosync(irq);
101 } else if (status & PS2STAT_TXE) {
102 sa1111_writel(ps2if->buf[ps2if->tail], ps2if->base + PS2DATA);
103 ps2if->tail = (ps2if->tail + 1) & (sizeof(ps2if->buf) - 1);
105 spin_unlock(&ps2if->lock);
111 * Write a byte to the PS2 port. We have to wait for the
112 * port to indicate that the transmitter is empty.
114 static int ps2_write(struct serio *io, unsigned char val)
116 struct ps2if *ps2if = io->port_data;
120 spin_lock_irqsave(&ps2if->lock, flags);
123 * If the TX register is empty, we can go straight out.
125 if (sa1111_readl(ps2if->base + PS2STAT) & PS2STAT_TXE) {
126 sa1111_writel(val, ps2if->base + PS2DATA);
128 if (ps2if->head == ps2if->tail)
129 enable_irq(ps2if->dev->irq[1]);
130 head = (ps2if->head + 1) & (sizeof(ps2if->buf) - 1);
131 if (head != ps2if->tail) {
132 ps2if->buf[ps2if->head] = val;
137 spin_unlock_irqrestore(&ps2if->lock, flags);
141 static int ps2_open(struct serio *io)
143 struct ps2if *ps2if = io->port_data;
146 ret = sa1111_enable_device(ps2if->dev);
150 ret = request_irq(ps2if->dev->irq[0], ps2_rxint, 0,
151 SA1111_DRIVER_NAME(ps2if->dev), ps2if);
153 printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
154 ps2if->dev->irq[0], ret);
155 sa1111_disable_device(ps2if->dev);
159 ret = request_irq(ps2if->dev->irq[1], ps2_txint, 0,
160 SA1111_DRIVER_NAME(ps2if->dev), ps2if);
162 printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
163 ps2if->dev->irq[1], ret);
164 free_irq(ps2if->dev->irq[0], ps2if);
165 sa1111_disable_device(ps2if->dev);
171 enable_irq_wake(ps2if->dev->irq[0]);
173 sa1111_writel(PS2CR_ENA, ps2if->base + PS2CR);
177 static void ps2_close(struct serio *io)
179 struct ps2if *ps2if = io->port_data;
181 sa1111_writel(0, ps2if->base + PS2CR);
183 disable_irq_wake(ps2if->dev->irq[0]);
187 free_irq(ps2if->dev->irq[1], ps2if);
188 free_irq(ps2if->dev->irq[0], ps2if);
190 sa1111_disable_device(ps2if->dev);
194 * Clear the input buffer.
196 static void ps2_clear_input(struct ps2if *ps2if)
201 if ((sa1111_readl(ps2if->base + PS2DATA) & 0xff) == 0xff)
206 static unsigned int ps2_test_one(struct ps2if *ps2if,
211 sa1111_writel(PS2CR_ENA | mask, ps2if->base + PS2CR);
215 val = sa1111_readl(ps2if->base + PS2STAT);
216 return val & (PS2STAT_KBC | PS2STAT_KBD);
220 * Test the keyboard interface. We basically check to make sure that
221 * we can drive each line to the keyboard independently of each other.
223 static int ps2_test(struct ps2if *ps2if)
228 stat = ps2_test_one(ps2if, PS2CR_FKC);
229 if (stat != PS2STAT_KBD) {
230 printk("PS/2 interface test failed[1]: %02x\n", stat);
234 stat = ps2_test_one(ps2if, 0);
235 if (stat != (PS2STAT_KBC | PS2STAT_KBD)) {
236 printk("PS/2 interface test failed[2]: %02x\n", stat);
240 stat = ps2_test_one(ps2if, PS2CR_FKD);
241 if (stat != PS2STAT_KBC) {
242 printk("PS/2 interface test failed[3]: %02x\n", stat);
246 sa1111_writel(0, ps2if->base + PS2CR);
252 * Add one device to this driver.
254 static int ps2_probe(struct sa1111_dev *dev)
260 ps2if = kzalloc(sizeof(struct ps2if), GFP_KERNEL);
261 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
262 if (!ps2if || !serio) {
268 serio->id.type = SERIO_8042;
269 serio->write = ps2_write;
270 serio->open = ps2_open;
271 serio->close = ps2_close;
272 strlcpy(serio->name, dev_name(&dev->dev), sizeof(serio->name));
273 strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
274 serio->port_data = ps2if;
275 serio->dev.parent = &dev->dev;
278 sa1111_set_drvdata(dev, ps2if);
280 spin_lock_init(&ps2if->lock);
283 * Request the physical region for this PS2 port.
285 if (!request_mem_region(dev->res.start,
286 dev->res.end - dev->res.start + 1,
287 SA1111_DRIVER_NAME(dev))) {
293 * Our parent device has already mapped the region.
295 ps2if->base = dev->mapbase;
297 sa1111_enable_device(ps2if->dev);
299 /* Incoming clock is 8MHz */
300 sa1111_writel(0, ps2if->base + PS2CLKDIV);
301 sa1111_writel(127, ps2if->base + PS2PRECNT);
304 * Flush any pending input.
306 ps2_clear_input(ps2if);
309 * Test the keyboard interface.
311 ret = ps2_test(ps2if);
316 * Flush any pending input.
318 ps2_clear_input(ps2if);
320 sa1111_disable_device(ps2if->dev);
321 serio_register_port(ps2if->io);
325 sa1111_disable_device(ps2if->dev);
326 release_mem_region(dev->res.start, resource_size(&dev->res));
328 sa1111_set_drvdata(dev, NULL);
335 * Remove one device from this driver.
337 static int ps2_remove(struct sa1111_dev *dev)
339 struct ps2if *ps2if = sa1111_get_drvdata(dev);
341 serio_unregister_port(ps2if->io);
342 release_mem_region(dev->res.start, resource_size(&dev->res));
343 sa1111_set_drvdata(dev, NULL);
351 * Our device driver structure
353 static struct sa1111_driver ps2_driver = {
355 .name = "sa1111-ps2",
356 .owner = THIS_MODULE,
358 .devid = SA1111_DEVID_PS2,
360 .remove = ps2_remove,
363 static int __init ps2_init(void)
365 return sa1111_driver_register(&ps2_driver);
368 static void __exit ps2_exit(void)
370 sa1111_driver_unregister(&ps2_driver);
373 module_init(ps2_init);
374 module_exit(ps2_exit);
376 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
377 MODULE_DESCRIPTION("SA1111 PS2 controller driver");
378 MODULE_LICENSE("GPL");