Merge ../linux-2.6-watchdog-mm
[sfrench/cifs-2.6.git] / drivers / input / serio / ct82c710.c
1 /*
2  * $Id: ct82c710.c,v 1.11 2001/09/25 10:12:07 vojtech Exp $
3  *
4  *  Copyright (c) 1999-2001 Vojtech Pavlik
5  */
6
7 /*
8  *  82C710 C&T mouse port chip driver for Linux
9  */
10
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  * Should you need to contact me, the author, you can do so either by
27  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
28  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29  */
30
31 #include <linux/delay.h>
32 #include <linux/module.h>
33 #include <linux/ioport.h>
34 #include <linux/init.h>
35 #include <linux/interrupt.h>
36 #include <linux/serio.h>
37 #include <linux/errno.h>
38 #include <linux/err.h>
39 #include <linux/platform_device.h>
40
41 #include <asm/io.h>
42
43 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
44 MODULE_DESCRIPTION("82C710 C&T mouse port chip driver");
45 MODULE_LICENSE("GPL");
46
47 /*
48  * ct82c710 interface
49  */
50
51 #define CT82C710_DEV_IDLE     0x01              /* Device Idle */
52 #define CT82C710_RX_FULL      0x02              /* Device Char received */
53 #define CT82C710_TX_IDLE      0x04              /* Device XMIT Idle */
54 #define CT82C710_RESET        0x08              /* Device Reset */
55 #define CT82C710_INTS_ON      0x10              /* Device Interrupt On */
56 #define CT82C710_ERROR_FLAG   0x20              /* Device Error */
57 #define CT82C710_CLEAR        0x40              /* Device Clear */
58 #define CT82C710_ENABLE       0x80              /* Device Enable */
59
60 #define CT82C710_IRQ          12
61
62 #define CT82C710_DATA         ct82c710_iores.start
63 #define CT82C710_STATUS       (ct82c710_iores.start + 1)
64
65 static struct serio *ct82c710_port;
66 static struct platform_device *ct82c710_device;
67 static struct resource ct82c710_iores;
68
69 /*
70  * Interrupt handler for the 82C710 mouse port. A character
71  * is waiting in the 82C710.
72  */
73
74 static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id)
75 {
76         return serio_interrupt(ct82c710_port, inb(CT82C710_DATA), 0);
77 }
78
79 /*
80  * Wait for device to send output char and flush any input char.
81  */
82
83 static int ct82c170_wait(void)
84 {
85         int timeout = 60000;
86
87         while ((inb(CT82C710_STATUS) & (CT82C710_RX_FULL | CT82C710_TX_IDLE | CT82C710_DEV_IDLE))
88                        != (CT82C710_DEV_IDLE | CT82C710_TX_IDLE) && timeout) {
89
90                 if (inb_p(CT82C710_STATUS) & CT82C710_RX_FULL) inb_p(CT82C710_DATA);
91
92                 udelay(1);
93                 timeout--;
94         }
95
96         return !timeout;
97 }
98
99 static void ct82c710_close(struct serio *serio)
100 {
101         if (ct82c170_wait())
102                 printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
103
104         outb_p(inb_p(CT82C710_STATUS) & ~(CT82C710_ENABLE | CT82C710_INTS_ON), CT82C710_STATUS);
105
106         if (ct82c170_wait())
107                 printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
108
109         free_irq(CT82C710_IRQ, NULL);
110 }
111
112 static int ct82c710_open(struct serio *serio)
113 {
114         unsigned char status;
115
116         if (request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL))
117                 return -1;
118
119         status = inb_p(CT82C710_STATUS);
120
121         status |= (CT82C710_ENABLE | CT82C710_RESET);
122         outb_p(status, CT82C710_STATUS);
123
124         status &= ~(CT82C710_RESET);
125         outb_p(status, CT82C710_STATUS);
126
127         status |= CT82C710_INTS_ON;
128         outb_p(status, CT82C710_STATUS);        /* Enable interrupts */
129
130         while (ct82c170_wait()) {
131                 printk(KERN_ERR "ct82c710: Device busy in open()\n");
132                 status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON);
133                 outb_p(status, CT82C710_STATUS);
134                 free_irq(CT82C710_IRQ, NULL);
135                 return -1;
136         }
137
138         return 0;
139 }
140
141 /*
142  * Write to the 82C710 mouse device.
143  */
144
145 static int ct82c710_write(struct serio *port, unsigned char c)
146 {
147         if (ct82c170_wait()) return -1;
148         outb_p(c, CT82C710_DATA);
149         return 0;
150 }
151
152 /*
153  * See if we can find a 82C710 device. Read mouse address.
154  */
155
156 static int __init ct82c710_detect(void)
157 {
158         outb_p(0x55, 0x2fa);                            /* Any value except 9, ff or 36 */
159         outb_p(0xaa, 0x3fa);                            /* Inverse of 55 */
160         outb_p(0x36, 0x3fa);                            /* Address the chip */
161         outb_p(0xe4, 0x3fa);                            /* 390/4; 390 = config address */
162         outb_p(0x1b, 0x2fa);                            /* Inverse of e4 */
163         outb_p(0x0f, 0x390);                            /* Write index */
164         if (inb_p(0x391) != 0xe4)                       /* Config address found? */
165                 return -ENODEV;                         /* No: no 82C710 here */
166
167         outb_p(0x0d, 0x390);                            /* Write index */
168         ct82c710_iores.start = inb_p(0x391) << 2;       /* Get mouse I/O address */
169         ct82c710_iores.end = ct82c710_iores.start + 1;
170         ct82c710_iores.flags = IORESOURCE_IO;
171         outb_p(0x0f, 0x390);
172         outb_p(0x0f, 0x391);                            /* Close config mode */
173
174         return 0;
175 }
176
177 static int __devinit ct82c710_probe(struct platform_device *dev)
178 {
179         ct82c710_port = kzalloc(sizeof(struct serio), GFP_KERNEL);
180         if (!ct82c710_port)
181                 return -ENOMEM;
182
183         ct82c710_port->id.type = SERIO_8042;
184         ct82c710_port->dev.parent = &dev->dev;
185         ct82c710_port->open = ct82c710_open;
186         ct82c710_port->close = ct82c710_close;
187         ct82c710_port->write = ct82c710_write;
188         strlcpy(ct82c710_port->name, "C&T 82c710 mouse port",
189                 sizeof(ct82c710_port->name));
190         snprintf(ct82c710_port->phys, sizeof(ct82c710_port->phys),
191                  "isa%16llx/serio0", (unsigned long long)CT82C710_DATA);
192
193         serio_register_port(ct82c710_port);
194
195         return 0;
196 }
197
198 static int __devexit ct82c710_remove(struct platform_device *dev)
199 {
200         serio_unregister_port(ct82c710_port);
201
202         return 0;
203 }
204
205 static struct platform_driver ct82c710_driver = {
206         .driver         = {
207                 .name   = "ct82c710",
208                 .owner  = THIS_MODULE,
209         },
210         .probe          = ct82c710_probe,
211         .remove         = __devexit_p(ct82c710_remove),
212 };
213
214
215 static int __init ct82c710_init(void)
216 {
217         int error;
218
219         error = ct82c710_detect();
220         if (error)
221                 return error;
222
223         error = platform_driver_register(&ct82c710_driver);
224         if (error)
225                 return error;
226
227         ct82c710_device = platform_device_alloc("ct82c710", -1);
228         if (!ct82c710_device) {
229                 error = -ENOMEM;
230                 goto err_unregister_driver;
231         }
232
233         error = platform_device_add_resources(ct82c710_device, &ct82c710_iores, 1);
234         if (error)
235                 goto err_free_device;
236
237         error = platform_device_add(ct82c710_device);
238         if (error)
239                 goto err_free_device;
240
241         serio_register_port(ct82c710_port);
242
243         printk(KERN_INFO "serio: C&T 82c710 mouse port at %#llx irq %d\n",
244                 (unsigned long long)CT82C710_DATA, CT82C710_IRQ);
245
246         return 0;
247
248  err_free_device:
249         platform_device_put(ct82c710_device);
250  err_unregister_driver:
251         platform_driver_unregister(&ct82c710_driver);
252         return error;
253 }
254
255 static void __exit ct82c710_exit(void)
256 {
257         platform_device_unregister(ct82c710_device);
258         platform_driver_unregister(&ct82c710_driver);
259 }
260
261 module_init(ct82c710_init);
262 module_exit(ct82c710_exit);