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>
23 #include <asm/system.h>
25 #include <asm/hardware/sa1111.h>
29 struct sa1111_dev
*dev
;
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.
43 static irqreturn_t
ps2_rxint(int irq
, void *dev_id
)
45 struct ps2if
*ps2if
= dev_id
;
46 unsigned int scancode
, flag
, status
;
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
);
53 flag
= (status
& PS2STAT_STP
? SERIO_FRAME
: 0) |
54 (status
& PS2STAT_RXP
? 0 : SERIO_PARITY
);
56 scancode
= sa1111_readl(ps2if
->base
+ SA1111_PS2DATA
) & 0xff;
58 if (hweight8(scancode
) & 1)
61 serio_interrupt(ps2if
->io
, scancode
, flag
);
63 status
= sa1111_readl(ps2if
->base
+ SA1111_PS2STAT
);
70 * Completion of ps2 write
72 static irqreturn_t
ps2_txint(int irq
, void *dev_id
)
74 struct ps2if
*ps2if
= dev_id
;
77 spin_lock(&ps2if
->lock
);
78 status
= sa1111_readl(ps2if
->base
+ SA1111_PS2STAT
);
79 if (ps2if
->head
== ps2if
->tail
) {
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);
86 spin_unlock(&ps2if
->lock
);
92 * Write a byte to the PS2 port. We have to wait for the
93 * port to indicate that the transmitter is empty.
95 static int ps2_write(struct serio
*io
, unsigned char val
)
97 struct ps2if
*ps2if
= io
->port_data
;
101 spin_lock_irqsave(&ps2if
->lock
, flags
);
104 * If the TX register is empty, we can go straight out.
106 if (sa1111_readl(ps2if
->base
+ SA1111_PS2STAT
) & PS2STAT_TXE
) {
107 sa1111_writel(val
, ps2if
->base
+ SA1111_PS2DATA
);
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
;
118 spin_unlock_irqrestore(&ps2if
->lock
, flags
);
122 static int ps2_open(struct serio
*io
)
124 struct ps2if
*ps2if
= io
->port_data
;
127 sa1111_enable_device(ps2if
->dev
);
129 ret
= request_irq(ps2if
->dev
->irq
[0], ps2_rxint
, 0,
130 SA1111_DRIVER_NAME(ps2if
->dev
), ps2if
);
132 printk(KERN_ERR
"sa1111ps2: could not allocate IRQ%d: %d\n",
133 ps2if
->dev
->irq
[0], ret
);
137 ret
= request_irq(ps2if
->dev
->irq
[1], ps2_txint
, 0,
138 SA1111_DRIVER_NAME(ps2if
->dev
), ps2if
);
140 printk(KERN_ERR
"sa1111ps2: could not allocate IRQ%d: %d\n",
141 ps2if
->dev
->irq
[1], ret
);
142 free_irq(ps2if
->dev
->irq
[0], ps2if
);
148 enable_irq_wake(ps2if
->dev
->irq
[0]);
150 sa1111_writel(PS2CR_ENA
, ps2if
->base
+ SA1111_PS2CR
);
154 static void ps2_close(struct serio
*io
)
156 struct ps2if
*ps2if
= io
->port_data
;
158 sa1111_writel(0, ps2if
->base
+ SA1111_PS2CR
);
160 disable_irq_wake(ps2if
->dev
->irq
[0]);
164 free_irq(ps2if
->dev
->irq
[1], ps2if
);
165 free_irq(ps2if
->dev
->irq
[0], ps2if
);
167 sa1111_disable_device(ps2if
->dev
);
171 * Clear the input buffer.
173 static void __devinit
ps2_clear_input(struct ps2if
*ps2if
)
178 if ((sa1111_readl(ps2if
->base
+ SA1111_PS2DATA
) & 0xff) == 0xff)
183 static inline unsigned int
184 ps2_test_one(struct ps2if
*ps2if
, unsigned int mask
)
188 sa1111_writel(PS2CR_ENA
| mask
, ps2if
->base
+ SA1111_PS2CR
);
192 val
= sa1111_readl(ps2if
->base
+ SA1111_PS2STAT
);
193 return val
& (PS2STAT_KBC
| PS2STAT_KBD
);
197 * Test the keyboard interface. We basically check to make sure that
198 * we can drive each line to the keyboard independently of each other.
200 static int __init
ps2_test(struct ps2if
*ps2if
)
205 stat
= ps2_test_one(ps2if
, PS2CR_FKC
);
206 if (stat
!= PS2STAT_KBD
) {
207 printk("PS/2 interface test failed[1]: %02x\n", stat
);
211 stat
= ps2_test_one(ps2if
, 0);
212 if (stat
!= (PS2STAT_KBC
| PS2STAT_KBD
)) {
213 printk("PS/2 interface test failed[2]: %02x\n", stat
);
217 stat
= ps2_test_one(ps2if
, PS2CR_FKD
);
218 if (stat
!= PS2STAT_KBC
) {
219 printk("PS/2 interface test failed[3]: %02x\n", stat
);
223 sa1111_writel(0, ps2if
->base
+ SA1111_PS2CR
);
229 * Add one device to this driver.
231 static int __devinit
ps2_probe(struct sa1111_dev
*dev
)
237 ps2if
= kmalloc(sizeof(struct ps2if
), GFP_KERNEL
);
238 serio
= kmalloc(sizeof(struct serio
), GFP_KERNEL
);
239 if (!ps2if
|| !serio
) {
244 memset(ps2if
, 0, sizeof(struct ps2if
));
245 memset(serio
, 0, sizeof(struct serio
));
247 serio
->id
.type
= SERIO_8042
;
248 serio
->write
= ps2_write
;
249 serio
->open
= ps2_open
;
250 serio
->close
= ps2_close
;
251 strlcpy(serio
->name
, dev
->dev
.bus_id
, sizeof(serio
->name
));
252 strlcpy(serio
->phys
, dev
->dev
.bus_id
, sizeof(serio
->phys
));
253 serio
->port_data
= ps2if
;
254 serio
->dev
.parent
= &dev
->dev
;
257 sa1111_set_drvdata(dev
, ps2if
);
259 spin_lock_init(&ps2if
->lock
);
262 * Request the physical region for this PS2 port.
264 if (!request_mem_region(dev
->res
.start
,
265 dev
->res
.end
- dev
->res
.start
+ 1,
266 SA1111_DRIVER_NAME(dev
))) {
272 * Our parent device has already mapped the region.
274 ps2if
->base
= dev
->mapbase
;
276 sa1111_enable_device(ps2if
->dev
);
278 /* Incoming clock is 8MHz */
279 sa1111_writel(0, ps2if
->base
+ SA1111_PS2CLKDIV
);
280 sa1111_writel(127, ps2if
->base
+ SA1111_PS2PRECNT
);
283 * Flush any pending input.
285 ps2_clear_input(ps2if
);
288 * Test the keyboard interface.
290 ret
= ps2_test(ps2if
);
295 * Flush any pending input.
297 ps2_clear_input(ps2if
);
299 sa1111_disable_device(ps2if
->dev
);
300 serio_register_port(ps2if
->io
);
304 sa1111_disable_device(ps2if
->dev
);
305 release_mem_region(dev
->res
.start
,
306 dev
->res
.end
- dev
->res
.start
+ 1);
308 sa1111_set_drvdata(dev
, NULL
);
315 * Remove one device from this driver.
317 static int ps2_remove(struct sa1111_dev
*dev
)
319 struct ps2if
*ps2if
= sa1111_get_drvdata(dev
);
321 serio_unregister_port(ps2if
->io
);
322 release_mem_region(dev
->res
.start
,
323 dev
->res
.end
- dev
->res
.start
+ 1);
324 sa1111_set_drvdata(dev
, NULL
);
332 * Our device driver structure
334 static struct sa1111_driver ps2_driver
= {
336 .name
= "sa1111-ps2",
338 .devid
= SA1111_DEVID_PS2
,
340 .remove
= ps2_remove
,
343 static int __init
ps2_init(void)
345 return sa1111_driver_register(&ps2_driver
);
348 static void __exit
ps2_exit(void)
350 sa1111_driver_unregister(&ps2_driver
);
353 module_init(ps2_init
);
354 module_exit(ps2_exit
);
356 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
357 MODULE_DESCRIPTION("SA1111 PS2 controller driver");
358 MODULE_LICENSE("GPL");