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