xref: /linux/drivers/input/serio/ct82c710.c (revision 858259cf7d1c443c836a2022b78cb281f0a9b95e)
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/config.h>
35 #include <linux/init.h>
36 #include <linux/interrupt.h>
37 #include <linux/serio.h>
38 #include <linux/errno.h>
39 #include <linux/err.h>
40 #include <linux/platform_device.h>
41 
42 #include <asm/io.h>
43 
44 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
45 MODULE_DESCRIPTION("82C710 C&T mouse port chip driver");
46 MODULE_LICENSE("GPL");
47 
48 /*
49  * ct82c710 interface
50  */
51 
52 #define CT82C710_DEV_IDLE     0x01		/* Device Idle */
53 #define CT82C710_RX_FULL      0x02		/* Device Char received */
54 #define CT82C710_TX_IDLE      0x04		/* Device XMIT Idle */
55 #define CT82C710_RESET        0x08		/* Device Reset */
56 #define CT82C710_INTS_ON      0x10		/* Device Interrupt On */
57 #define CT82C710_ERROR_FLAG   0x20		/* Device Error */
58 #define CT82C710_CLEAR        0x40		/* Device Clear */
59 #define CT82C710_ENABLE       0x80		/* Device Enable */
60 
61 #define CT82C710_IRQ          12
62 
63 #define CT82C710_DATA         ct82c710_iores.start
64 #define CT82C710_STATUS       (ct82c710_iores.start + 1)
65 
66 static struct serio *ct82c710_port;
67 static struct platform_device *ct82c710_device;
68 static struct resource ct82c710_iores;
69 
70 /*
71  * Interrupt handler for the 82C710 mouse port. A character
72  * is waiting in the 82C710.
73  */
74 
75 static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id, struct pt_regs * regs)
76 {
77 	return serio_interrupt(ct82c710_port, inb(CT82C710_DATA), 0, regs);
78 }
79 
80 /*
81  * Wait for device to send output char and flush any input char.
82  */
83 
84 static int ct82c170_wait(void)
85 {
86 	int timeout = 60000;
87 
88 	while ((inb(CT82C710_STATUS) & (CT82C710_RX_FULL | CT82C710_TX_IDLE | CT82C710_DEV_IDLE))
89 		       != (CT82C710_DEV_IDLE | CT82C710_TX_IDLE) && timeout) {
90 
91 		if (inb_p(CT82C710_STATUS) & CT82C710_RX_FULL) inb_p(CT82C710_DATA);
92 
93 		udelay(1);
94 		timeout--;
95 	}
96 
97 	return !timeout;
98 }
99 
100 static void ct82c710_close(struct serio *serio)
101 {
102 	if (ct82c170_wait())
103 		printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
104 
105 	outb_p(inb_p(CT82C710_STATUS) & ~(CT82C710_ENABLE | CT82C710_INTS_ON), CT82C710_STATUS);
106 
107 	if (ct82c170_wait())
108 		printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
109 
110 	free_irq(CT82C710_IRQ, NULL);
111 }
112 
113 static int ct82c710_open(struct serio *serio)
114 {
115 	unsigned char status;
116 
117 	if (request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL))
118 		return -1;
119 
120 	status = inb_p(CT82C710_STATUS);
121 
122 	status |= (CT82C710_ENABLE | CT82C710_RESET);
123 	outb_p(status, CT82C710_STATUS);
124 
125 	status &= ~(CT82C710_RESET);
126 	outb_p(status, CT82C710_STATUS);
127 
128 	status |= CT82C710_INTS_ON;
129 	outb_p(status, CT82C710_STATUS);	/* Enable interrupts */
130 
131 	while (ct82c170_wait()) {
132 		printk(KERN_ERR "ct82c710: Device busy in open()\n");
133 		status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON);
134 		outb_p(status, CT82C710_STATUS);
135 		free_irq(CT82C710_IRQ, NULL);
136 		return -1;
137 	}
138 
139 	return 0;
140 }
141 
142 /*
143  * Write to the 82C710 mouse device.
144  */
145 
146 static int ct82c710_write(struct serio *port, unsigned char c)
147 {
148 	if (ct82c170_wait()) return -1;
149 	outb_p(c, CT82C710_DATA);
150 	return 0;
151 }
152 
153 /*
154  * See if we can find a 82C710 device. Read mouse address.
155  */
156 
157 static int __init ct82c710_probe(void)
158 {
159 	outb_p(0x55, 0x2fa);				/* Any value except 9, ff or 36 */
160 	outb_p(0xaa, 0x3fa);				/* Inverse of 55 */
161 	outb_p(0x36, 0x3fa);				/* Address the chip */
162 	outb_p(0xe4, 0x3fa);				/* 390/4; 390 = config address */
163 	outb_p(0x1b, 0x2fa);				/* Inverse of e4 */
164 	outb_p(0x0f, 0x390);				/* Write index */
165 	if (inb_p(0x391) != 0xe4)			/* Config address found? */
166 		return -1;				/* No: no 82C710 here */
167 
168 	outb_p(0x0d, 0x390);				/* Write index */
169 	ct82c710_iores.start = inb_p(0x391) << 2;	/* Get mouse I/O address */
170 	ct82c710_iores.end = ct82c710_iores.start + 1;
171 	ct82c710_iores.flags = IORESOURCE_IO;
172 	outb_p(0x0f, 0x390);
173 	outb_p(0x0f, 0x391);				/* Close config mode */
174 
175 	return 0;
176 }
177 
178 static struct serio * __init ct82c710_allocate_port(void)
179 {
180 	struct serio *serio;
181 
182 	serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
183 	if (serio) {
184 		memset(serio, 0, sizeof(struct serio));
185 		serio->id.type = SERIO_8042;
186 		serio->open = ct82c710_open;
187 		serio->close = ct82c710_close;
188 		serio->write = ct82c710_write;
189 		serio->dev.parent = &ct82c710_device->dev;
190 		strlcpy(serio->name, "C&T 82c710 mouse port", sizeof(serio->name));
191 		snprintf(serio->phys, sizeof(serio->phys), "isa%04lx/serio0", CT82C710_DATA);
192 	}
193 
194 	return serio;
195 }
196 
197 static int __init ct82c710_init(void)
198 {
199 	if (ct82c710_probe())
200 		return -ENODEV;
201 
202 	ct82c710_device = platform_device_register_simple("ct82c710", -1, &ct82c710_iores, 1);
203 	if (IS_ERR(ct82c710_device))
204 		return PTR_ERR(ct82c710_device);
205 
206 	if (!(ct82c710_port = ct82c710_allocate_port())) {
207 		platform_device_unregister(ct82c710_device);
208 		return -ENOMEM;
209 	}
210 
211 	serio_register_port(ct82c710_port);
212 
213 	printk(KERN_INFO "serio: C&T 82c710 mouse port at %#lx irq %d\n",
214 		CT82C710_DATA, CT82C710_IRQ);
215 
216 	return 0;
217 }
218 
219 static void __exit ct82c710_exit(void)
220 {
221 	serio_unregister_port(ct82c710_port);
222 	platform_device_unregister(ct82c710_device);
223 }
224 
225 module_init(ct82c710_init);
226 module_exit(ct82c710_exit);
227