5a41b8f7f888f0887583802a715e6341446fdbdd
[pandora-kernel.git] / drivers / input / serio / maceps2.c
1 /*
2  * SGI O2 MACE PS2 controller driver for linux
3  *
4  * Copyright (C) 2002 Vivien Chappelier
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 version 2 as
8  * published by the Free Software Foundation
9  */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/serio.h>
13 #include <linux/errno.h>
14 #include <linux/interrupt.h>
15 #include <linux/ioport.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <linux/err.h>
21
22 #include <asm/io.h>
23 #include <asm/irq.h>
24 #include <asm/system.h>
25 #include <asm/ip32/mace.h>
26 #include <asm/ip32/ip32_ints.h>
27
28 MODULE_AUTHOR("Vivien Chappelier <vivien.chappelier@linux-mips.org");
29 MODULE_DESCRIPTION("SGI O2 MACE PS2 controller driver");
30 MODULE_LICENSE("GPL");
31
32 #define MACE_PS2_TIMEOUT 10000 /* in 50us unit */
33
34 #define BIT(x) (1UL << (x))
35
36 #define PS2_STATUS_CLOCK_SIGNAL  BIT(0) /* external clock signal */
37 #define PS2_STATUS_CLOCK_INHIBIT BIT(1) /* clken output signal */
38 #define PS2_STATUS_TX_INPROGRESS BIT(2) /* transmission in progress */
39 #define PS2_STATUS_TX_EMPTY      BIT(3) /* empty transmit buffer */
40 #define PS2_STATUS_RX_FULL       BIT(4) /* full receive buffer */
41 #define PS2_STATUS_RX_INPROGRESS BIT(5) /* reception in progress */
42 #define PS2_STATUS_ERROR_PARITY  BIT(6) /* parity error */
43 #define PS2_STATUS_ERROR_FRAMING BIT(7) /* framing error */
44
45 #define PS2_CONTROL_TX_CLOCK_DISABLE BIT(0) /* inhibit clock signal after TX */
46 #define PS2_CONTROL_TX_ENABLE        BIT(1) /* transmit enable */
47 #define PS2_CONTROL_TX_INT_ENABLE    BIT(2) /* enable transmit interrupt */
48 #define PS2_CONTROL_RX_INT_ENABLE    BIT(3) /* enable receive interrupt */
49 #define PS2_CONTROL_RX_CLOCK_ENABLE  BIT(4) /* pause reception if set to 0 */
50 #define PS2_CONTROL_RESET            BIT(5) /* reset */
51
52 struct maceps2_data {
53         struct mace_ps2port *port;
54         int irq;
55 };
56
57 static struct maceps2_data port_data[2];
58 static struct serio *maceps2_port[2];
59 static struct platform_device *maceps2_device;
60
61 static int maceps2_write(struct serio *dev, unsigned char val)
62 {
63         struct mace_ps2port *port = ((struct maceps2_data *)dev->port_data)->port;
64         unsigned int timeout = MACE_PS2_TIMEOUT;
65
66         do {
67                 if (port->status & PS2_STATUS_TX_EMPTY) {
68                         port->tx = val;
69                         return 0;
70                 }
71                 udelay(50);
72         } while (timeout--);
73
74         return -1;
75 }
76
77 static irqreturn_t maceps2_interrupt(int irq, void *dev_id)
78 {
79         struct serio *dev = dev_id;
80         struct mace_ps2port *port = ((struct maceps2_data *)dev->port_data)->port;
81         unsigned long byte;
82
83         if (port->status & PS2_STATUS_RX_FULL) {
84                 byte = port->rx;
85                 serio_interrupt(dev, byte & 0xff, 0);
86         }
87
88         return IRQ_HANDLED;
89 }
90
91 static int maceps2_open(struct serio *dev)
92 {
93         struct maceps2_data *data = (struct maceps2_data *)dev->port_data;
94
95         if (request_irq(data->irq, maceps2_interrupt, 0, "PS2 port", dev)) {
96                 printk(KERN_ERR "Could not allocate PS/2 IRQ\n");
97                 return -EBUSY;
98         }
99
100         /* Reset port */
101         data->port->control = PS2_CONTROL_TX_CLOCK_DISABLE | PS2_CONTROL_RESET;
102         udelay(100);
103
104         /* Enable interrupts */
105         data->port->control = PS2_CONTROL_RX_CLOCK_ENABLE |
106                               PS2_CONTROL_TX_ENABLE |
107                               PS2_CONTROL_RX_INT_ENABLE;
108
109         return 0;
110 }
111
112 static void maceps2_close(struct serio *dev)
113 {
114         struct maceps2_data *data = (struct maceps2_data *)dev->port_data;
115
116         data->port->control = PS2_CONTROL_TX_CLOCK_DISABLE | PS2_CONTROL_RESET;
117         udelay(100);
118         free_irq(data->irq, dev);
119 }
120
121
122 static struct serio * __devinit maceps2_allocate_port(int idx)
123 {
124         struct serio *serio;
125
126         serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
127         if (serio) {
128                 serio->id.type          = SERIO_8042;
129                 serio->write            = maceps2_write;
130                 serio->open             = maceps2_open;
131                 serio->close            = maceps2_close;
132                 snprintf(serio->name, sizeof(serio->name), "MACE PS/2 port%d", idx);
133                 snprintf(serio->phys, sizeof(serio->phys), "mace/serio%d", idx);
134                 serio->port_data        = &port_data[idx];
135                 serio->dev.parent       = &maceps2_device->dev;
136         }
137
138         return serio;
139 }
140
141 static int __devinit maceps2_probe(struct platform_device *dev)
142 {
143         maceps2_port[0] = maceps2_allocate_port(0);
144         maceps2_port[1] = maceps2_allocate_port(1);
145         if (!maceps2_port[0] || !maceps2_port[1]) {
146                 kfree(maceps2_port[0]);
147                 kfree(maceps2_port[1]);
148                 return -ENOMEM;
149         }
150
151         serio_register_port(maceps2_port[0]);
152         serio_register_port(maceps2_port[1]);
153
154         return 0;
155 }
156
157 static int __devexit maceps2_remove(struct platform_device *dev)
158 {
159         serio_unregister_port(maceps2_port[0]);
160         serio_unregister_port(maceps2_port[1]);
161
162         return 0;
163 }
164
165 static struct platform_driver maceps2_driver = {
166         .driver         = {
167                 .name   = "maceps2",
168                 .owner  = THIS_MODULE,
169         },
170         .probe          = maceps2_probe,
171         .remove         = __devexit_p(maceps2_remove),
172 };
173
174 static int __init maceps2_init(void)
175 {
176         int error;
177
178         error = platform_driver_register(&maceps2_driver);
179         if (error)
180                 return error;
181
182         maceps2_device = platform_device_alloc("maceps2", -1);
183         if (!maceps2_device) {
184                 error = -ENOMEM;
185                 goto err_unregister_driver;
186         }
187
188         port_data[0].port = &mace->perif.ps2.keyb;
189         port_data[0].irq  = MACEISA_KEYB_IRQ;
190         port_data[1].port = &mace->perif.ps2.mouse;
191         port_data[1].irq  = MACEISA_MOUSE_IRQ;
192
193         error = platform_device_add(maceps2_device);
194         if (error)
195                 goto err_free_device;
196
197         return 0;
198
199  err_free_device:
200         platform_device_put(maceps2_device);
201  err_unregister_driver:
202         platform_driver_unregister(&maceps2_driver);
203         return error;
204 }
205
206 static void __exit maceps2_exit(void)
207 {
208         platform_device_unregister(maceps2_device);
209         platform_driver_unregister(&maceps2_driver);
210 }
211
212 module_init(maceps2_init);
213 module_exit(maceps2_exit);