xref: /linux/drivers/input/serio/ct82c710.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Copyright (c) 1999-2001 Vojtech Pavlik
31da177e4SLinus Torvalds  */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds /*
61da177e4SLinus Torvalds  *  82C710 C&T mouse port chip driver for Linux
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds /*
101da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
111da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
121da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
131da177e4SLinus Torvalds  * (at your option) any later version.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
161da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
171da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
181da177e4SLinus Torvalds  * GNU General Public License for more details.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
211da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
221da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * Should you need to contact me, the author, you can do so either by
251da177e4SLinus Torvalds  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
261da177e4SLinus Torvalds  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include <linux/delay.h>
301da177e4SLinus Torvalds #include <linux/module.h>
311da177e4SLinus Torvalds #include <linux/ioport.h>
321da177e4SLinus Torvalds #include <linux/init.h>
331da177e4SLinus Torvalds #include <linux/interrupt.h>
341da177e4SLinus Torvalds #include <linux/serio.h>
351da177e4SLinus Torvalds #include <linux/errno.h>
361da177e4SLinus Torvalds #include <linux/err.h>
37d052d1beSRussell King #include <linux/platform_device.h>
38*5a0e3ad6STejun Heo #include <linux/slab.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #include <asm/io.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
431da177e4SLinus Torvalds MODULE_DESCRIPTION("82C710 C&T mouse port chip driver");
441da177e4SLinus Torvalds MODULE_LICENSE("GPL");
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds /*
471da177e4SLinus Torvalds  * ct82c710 interface
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #define CT82C710_DEV_IDLE     0x01		/* Device Idle */
511da177e4SLinus Torvalds #define CT82C710_RX_FULL      0x02		/* Device Char received */
521da177e4SLinus Torvalds #define CT82C710_TX_IDLE      0x04		/* Device XMIT Idle */
531da177e4SLinus Torvalds #define CT82C710_RESET        0x08		/* Device Reset */
541da177e4SLinus Torvalds #define CT82C710_INTS_ON      0x10		/* Device Interrupt On */
551da177e4SLinus Torvalds #define CT82C710_ERROR_FLAG   0x20		/* Device Error */
561da177e4SLinus Torvalds #define CT82C710_CLEAR        0x40		/* Device Clear */
571da177e4SLinus Torvalds #define CT82C710_ENABLE       0x80		/* Device Enable */
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #define CT82C710_IRQ          12
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #define CT82C710_DATA         ct82c710_iores.start
621da177e4SLinus Torvalds #define CT82C710_STATUS       (ct82c710_iores.start + 1)
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds static struct serio *ct82c710_port;
651da177e4SLinus Torvalds static struct platform_device *ct82c710_device;
661da177e4SLinus Torvalds static struct resource ct82c710_iores;
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /*
691da177e4SLinus Torvalds  * Interrupt handler for the 82C710 mouse port. A character
701da177e4SLinus Torvalds  * is waiting in the 82C710.
711da177e4SLinus Torvalds  */
721da177e4SLinus Torvalds 
737d12e780SDavid Howells static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id)
741da177e4SLinus Torvalds {
757d12e780SDavid Howells 	return serio_interrupt(ct82c710_port, inb(CT82C710_DATA), 0);
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds /*
791da177e4SLinus Torvalds  * Wait for device to send output char and flush any input char.
801da177e4SLinus Torvalds  */
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds static int ct82c170_wait(void)
831da177e4SLinus Torvalds {
841da177e4SLinus Torvalds 	int timeout = 60000;
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	while ((inb(CT82C710_STATUS) & (CT82C710_RX_FULL | CT82C710_TX_IDLE | CT82C710_DEV_IDLE))
871da177e4SLinus Torvalds 		       != (CT82C710_DEV_IDLE | CT82C710_TX_IDLE) && timeout) {
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 		if (inb_p(CT82C710_STATUS) & CT82C710_RX_FULL) inb_p(CT82C710_DATA);
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 		udelay(1);
921da177e4SLinus Torvalds 		timeout--;
931da177e4SLinus Torvalds 	}
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	return !timeout;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds static void ct82c710_close(struct serio *serio)
991da177e4SLinus Torvalds {
1001da177e4SLinus Torvalds 	if (ct82c170_wait())
1011da177e4SLinus Torvalds 		printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	outb_p(inb_p(CT82C710_STATUS) & ~(CT82C710_ENABLE | CT82C710_INTS_ON), CT82C710_STATUS);
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	if (ct82c170_wait())
1061da177e4SLinus Torvalds 		printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	free_irq(CT82C710_IRQ, NULL);
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds static int ct82c710_open(struct serio *serio)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	unsigned char status;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	if (request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL))
1161da177e4SLinus Torvalds 		return -1;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	status = inb_p(CT82C710_STATUS);
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	status |= (CT82C710_ENABLE | CT82C710_RESET);
1211da177e4SLinus Torvalds 	outb_p(status, CT82C710_STATUS);
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	status &= ~(CT82C710_RESET);
1241da177e4SLinus Torvalds 	outb_p(status, CT82C710_STATUS);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	status |= CT82C710_INTS_ON;
1271da177e4SLinus Torvalds 	outb_p(status, CT82C710_STATUS);	/* Enable interrupts */
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	while (ct82c170_wait()) {
1301da177e4SLinus Torvalds 		printk(KERN_ERR "ct82c710: Device busy in open()\n");
1311da177e4SLinus Torvalds 		status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON);
1321da177e4SLinus Torvalds 		outb_p(status, CT82C710_STATUS);
1331da177e4SLinus Torvalds 		free_irq(CT82C710_IRQ, NULL);
1341da177e4SLinus Torvalds 		return -1;
1351da177e4SLinus Torvalds 	}
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	return 0;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds /*
1411da177e4SLinus Torvalds  * Write to the 82C710 mouse device.
1421da177e4SLinus Torvalds  */
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds static int ct82c710_write(struct serio *port, unsigned char c)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	if (ct82c170_wait()) return -1;
1471da177e4SLinus Torvalds 	outb_p(c, CT82C710_DATA);
1481da177e4SLinus Torvalds 	return 0;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds /*
1521da177e4SLinus Torvalds  * See if we can find a 82C710 device. Read mouse address.
1531da177e4SLinus Torvalds  */
1541da177e4SLinus Torvalds 
155916d83cfSDmitry Torokhov static int __init ct82c710_detect(void)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	outb_p(0x55, 0x2fa);				/* Any value except 9, ff or 36 */
1581da177e4SLinus Torvalds 	outb_p(0xaa, 0x3fa);				/* Inverse of 55 */
1591da177e4SLinus Torvalds 	outb_p(0x36, 0x3fa);				/* Address the chip */
1601da177e4SLinus Torvalds 	outb_p(0xe4, 0x3fa);				/* 390/4; 390 = config address */
1611da177e4SLinus Torvalds 	outb_p(0x1b, 0x2fa);				/* Inverse of e4 */
1621da177e4SLinus Torvalds 	outb_p(0x0f, 0x390);				/* Write index */
1631da177e4SLinus Torvalds 	if (inb_p(0x391) != 0xe4)			/* Config address found? */
164916d83cfSDmitry Torokhov 		return -ENODEV;				/* No: no 82C710 here */
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	outb_p(0x0d, 0x390);				/* Write index */
1671da177e4SLinus Torvalds 	ct82c710_iores.start = inb_p(0x391) << 2;	/* Get mouse I/O address */
1681da177e4SLinus Torvalds 	ct82c710_iores.end = ct82c710_iores.start + 1;
1691da177e4SLinus Torvalds 	ct82c710_iores.flags = IORESOURCE_IO;
1701da177e4SLinus Torvalds 	outb_p(0x0f, 0x390);
1711da177e4SLinus Torvalds 	outb_p(0x0f, 0x391);				/* Close config mode */
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	return 0;
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds 
176916d83cfSDmitry Torokhov static int __devinit ct82c710_probe(struct platform_device *dev)
1771da177e4SLinus Torvalds {
178916d83cfSDmitry Torokhov 	ct82c710_port = kzalloc(sizeof(struct serio), GFP_KERNEL);
179916d83cfSDmitry Torokhov 	if (!ct82c710_port)
180916d83cfSDmitry Torokhov 		return -ENOMEM;
1811da177e4SLinus Torvalds 
182916d83cfSDmitry Torokhov 	ct82c710_port->id.type = SERIO_8042;
183916d83cfSDmitry Torokhov 	ct82c710_port->dev.parent = &dev->dev;
184916d83cfSDmitry Torokhov 	ct82c710_port->open = ct82c710_open;
185916d83cfSDmitry Torokhov 	ct82c710_port->close = ct82c710_close;
186916d83cfSDmitry Torokhov 	ct82c710_port->write = ct82c710_write;
187916d83cfSDmitry Torokhov 	strlcpy(ct82c710_port->name, "C&T 82c710 mouse port",
188916d83cfSDmitry Torokhov 		sizeof(ct82c710_port->name));
189916d83cfSDmitry Torokhov 	snprintf(ct82c710_port->phys, sizeof(ct82c710_port->phys),
190e29419ffSGreg Kroah-Hartman 		 "isa%16llx/serio0", (unsigned long long)CT82C710_DATA);
191916d83cfSDmitry Torokhov 
192916d83cfSDmitry Torokhov 	serio_register_port(ct82c710_port);
193916d83cfSDmitry Torokhov 
194916d83cfSDmitry Torokhov 	return 0;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
197916d83cfSDmitry Torokhov static int __devexit ct82c710_remove(struct platform_device *dev)
198916d83cfSDmitry Torokhov {
199916d83cfSDmitry Torokhov 	serio_unregister_port(ct82c710_port);
200916d83cfSDmitry Torokhov 
201916d83cfSDmitry Torokhov 	return 0;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
204916d83cfSDmitry Torokhov static struct platform_driver ct82c710_driver = {
205916d83cfSDmitry Torokhov 	.driver		= {
206916d83cfSDmitry Torokhov 		.name	= "ct82c710",
207916d83cfSDmitry Torokhov 		.owner	= THIS_MODULE,
208916d83cfSDmitry Torokhov 	},
209916d83cfSDmitry Torokhov 	.probe		= ct82c710_probe,
210916d83cfSDmitry Torokhov 	.remove		= __devexit_p(ct82c710_remove),
211916d83cfSDmitry Torokhov };
212916d83cfSDmitry Torokhov 
213916d83cfSDmitry Torokhov 
2141da177e4SLinus Torvalds static int __init ct82c710_init(void)
2151da177e4SLinus Torvalds {
216916d83cfSDmitry Torokhov 	int error;
2171da177e4SLinus Torvalds 
218916d83cfSDmitry Torokhov 	error = ct82c710_detect();
219916d83cfSDmitry Torokhov 	if (error)
220916d83cfSDmitry Torokhov 		return error;
2211da177e4SLinus Torvalds 
222916d83cfSDmitry Torokhov 	error = platform_driver_register(&ct82c710_driver);
223916d83cfSDmitry Torokhov 	if (error)
224916d83cfSDmitry Torokhov 		return error;
225916d83cfSDmitry Torokhov 
226916d83cfSDmitry Torokhov 	ct82c710_device = platform_device_alloc("ct82c710", -1);
227916d83cfSDmitry Torokhov 	if (!ct82c710_device) {
228916d83cfSDmitry Torokhov 		error = -ENOMEM;
229916d83cfSDmitry Torokhov 		goto err_unregister_driver;
2301da177e4SLinus Torvalds 	}
2311da177e4SLinus Torvalds 
232916d83cfSDmitry Torokhov 	error = platform_device_add_resources(ct82c710_device, &ct82c710_iores, 1);
233916d83cfSDmitry Torokhov 	if (error)
234916d83cfSDmitry Torokhov 		goto err_free_device;
235916d83cfSDmitry Torokhov 
236916d83cfSDmitry Torokhov 	error = platform_device_add(ct82c710_device);
237916d83cfSDmitry Torokhov 	if (error)
238916d83cfSDmitry Torokhov 		goto err_free_device;
239916d83cfSDmitry Torokhov 
2401da177e4SLinus Torvalds 	serio_register_port(ct82c710_port);
2411da177e4SLinus Torvalds 
242e29419ffSGreg Kroah-Hartman 	printk(KERN_INFO "serio: C&T 82c710 mouse port at %#llx irq %d\n",
243e29419ffSGreg Kroah-Hartman 		(unsigned long long)CT82C710_DATA, CT82C710_IRQ);
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	return 0;
246916d83cfSDmitry Torokhov 
247916d83cfSDmitry Torokhov  err_free_device:
248916d83cfSDmitry Torokhov 	platform_device_put(ct82c710_device);
249916d83cfSDmitry Torokhov  err_unregister_driver:
250916d83cfSDmitry Torokhov 	platform_driver_unregister(&ct82c710_driver);
251916d83cfSDmitry Torokhov 	return error;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds static void __exit ct82c710_exit(void)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	platform_device_unregister(ct82c710_device);
257916d83cfSDmitry Torokhov 	platform_driver_unregister(&ct82c710_driver);
2581da177e4SLinus Torvalds }
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds module_init(ct82c710_init);
2611da177e4SLinus Torvalds module_exit(ct82c710_exit);
262