xref: /linux/drivers/input/serio/ct82c710.c (revision 916d83cfe5da1cda454d8b0ae233f06b58bd7f91)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * $Id: ct82c710.c,v 1.11 2001/09/25 10:12:07 vojtech Exp $
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (c) 1999-2001 Vojtech Pavlik
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  *  82C710 C&T mouse port chip driver for Linux
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /*
121da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
131da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
141da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
151da177e4SLinus Torvalds  * (at your option) any later version.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
181da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
191da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
201da177e4SLinus Torvalds  * GNU General Public License for more details.
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
231da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
241da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  * Should you need to contact me, the author, you can do so either by
271da177e4SLinus Torvalds  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
281da177e4SLinus Torvalds  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <linux/delay.h>
321da177e4SLinus Torvalds #include <linux/module.h>
331da177e4SLinus Torvalds #include <linux/ioport.h>
341da177e4SLinus Torvalds #include <linux/config.h>
351da177e4SLinus Torvalds #include <linux/init.h>
361da177e4SLinus Torvalds #include <linux/interrupt.h>
371da177e4SLinus Torvalds #include <linux/serio.h>
381da177e4SLinus Torvalds #include <linux/errno.h>
391da177e4SLinus Torvalds #include <linux/err.h>
40d052d1beSRussell King #include <linux/platform_device.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <asm/io.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
451da177e4SLinus Torvalds MODULE_DESCRIPTION("82C710 C&T mouse port chip driver");
461da177e4SLinus Torvalds MODULE_LICENSE("GPL");
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /*
491da177e4SLinus Torvalds  * ct82c710 interface
501da177e4SLinus Torvalds  */
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #define CT82C710_DEV_IDLE     0x01		/* Device Idle */
531da177e4SLinus Torvalds #define CT82C710_RX_FULL      0x02		/* Device Char received */
541da177e4SLinus Torvalds #define CT82C710_TX_IDLE      0x04		/* Device XMIT Idle */
551da177e4SLinus Torvalds #define CT82C710_RESET        0x08		/* Device Reset */
561da177e4SLinus Torvalds #define CT82C710_INTS_ON      0x10		/* Device Interrupt On */
571da177e4SLinus Torvalds #define CT82C710_ERROR_FLAG   0x20		/* Device Error */
581da177e4SLinus Torvalds #define CT82C710_CLEAR        0x40		/* Device Clear */
591da177e4SLinus Torvalds #define CT82C710_ENABLE       0x80		/* Device Enable */
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #define CT82C710_IRQ          12
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #define CT82C710_DATA         ct82c710_iores.start
641da177e4SLinus Torvalds #define CT82C710_STATUS       (ct82c710_iores.start + 1)
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds static struct serio *ct82c710_port;
671da177e4SLinus Torvalds static struct platform_device *ct82c710_device;
681da177e4SLinus Torvalds static struct resource ct82c710_iores;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /*
711da177e4SLinus Torvalds  * Interrupt handler for the 82C710 mouse port. A character
721da177e4SLinus Torvalds  * is waiting in the 82C710.
731da177e4SLinus Torvalds  */
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id, struct pt_regs * regs)
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds 	return serio_interrupt(ct82c710_port, inb(CT82C710_DATA), 0, regs);
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds /*
811da177e4SLinus Torvalds  * Wait for device to send output char and flush any input char.
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds static int ct82c170_wait(void)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	int timeout = 60000;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	while ((inb(CT82C710_STATUS) & (CT82C710_RX_FULL | CT82C710_TX_IDLE | CT82C710_DEV_IDLE))
891da177e4SLinus Torvalds 		       != (CT82C710_DEV_IDLE | CT82C710_TX_IDLE) && timeout) {
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 		if (inb_p(CT82C710_STATUS) & CT82C710_RX_FULL) inb_p(CT82C710_DATA);
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 		udelay(1);
941da177e4SLinus Torvalds 		timeout--;
951da177e4SLinus Torvalds 	}
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	return !timeout;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static void ct82c710_close(struct serio *serio)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	if (ct82c170_wait())
1031da177e4SLinus Torvalds 		printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	outb_p(inb_p(CT82C710_STATUS) & ~(CT82C710_ENABLE | CT82C710_INTS_ON), CT82C710_STATUS);
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	if (ct82c170_wait())
1081da177e4SLinus Torvalds 		printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	free_irq(CT82C710_IRQ, NULL);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds static int ct82c710_open(struct serio *serio)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds 	unsigned char status;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	if (request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL))
1181da177e4SLinus Torvalds 		return -1;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	status = inb_p(CT82C710_STATUS);
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	status |= (CT82C710_ENABLE | CT82C710_RESET);
1231da177e4SLinus Torvalds 	outb_p(status, CT82C710_STATUS);
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	status &= ~(CT82C710_RESET);
1261da177e4SLinus Torvalds 	outb_p(status, CT82C710_STATUS);
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	status |= CT82C710_INTS_ON;
1291da177e4SLinus Torvalds 	outb_p(status, CT82C710_STATUS);	/* Enable interrupts */
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	while (ct82c170_wait()) {
1321da177e4SLinus Torvalds 		printk(KERN_ERR "ct82c710: Device busy in open()\n");
1331da177e4SLinus Torvalds 		status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON);
1341da177e4SLinus Torvalds 		outb_p(status, CT82C710_STATUS);
1351da177e4SLinus Torvalds 		free_irq(CT82C710_IRQ, NULL);
1361da177e4SLinus Torvalds 		return -1;
1371da177e4SLinus Torvalds 	}
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	return 0;
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds /*
1431da177e4SLinus Torvalds  * Write to the 82C710 mouse device.
1441da177e4SLinus Torvalds  */
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds static int ct82c710_write(struct serio *port, unsigned char c)
1471da177e4SLinus Torvalds {
1481da177e4SLinus Torvalds 	if (ct82c170_wait()) return -1;
1491da177e4SLinus Torvalds 	outb_p(c, CT82C710_DATA);
1501da177e4SLinus Torvalds 	return 0;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds /*
1541da177e4SLinus Torvalds  * See if we can find a 82C710 device. Read mouse address.
1551da177e4SLinus Torvalds  */
1561da177e4SLinus Torvalds 
157*916d83cfSDmitry Torokhov static int __init ct82c710_detect(void)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	outb_p(0x55, 0x2fa);				/* Any value except 9, ff or 36 */
1601da177e4SLinus Torvalds 	outb_p(0xaa, 0x3fa);				/* Inverse of 55 */
1611da177e4SLinus Torvalds 	outb_p(0x36, 0x3fa);				/* Address the chip */
1621da177e4SLinus Torvalds 	outb_p(0xe4, 0x3fa);				/* 390/4; 390 = config address */
1631da177e4SLinus Torvalds 	outb_p(0x1b, 0x2fa);				/* Inverse of e4 */
1641da177e4SLinus Torvalds 	outb_p(0x0f, 0x390);				/* Write index */
1651da177e4SLinus Torvalds 	if (inb_p(0x391) != 0xe4)			/* Config address found? */
166*916d83cfSDmitry Torokhov 		return -ENODEV;				/* No: no 82C710 here */
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	outb_p(0x0d, 0x390);				/* Write index */
1691da177e4SLinus Torvalds 	ct82c710_iores.start = inb_p(0x391) << 2;	/* Get mouse I/O address */
1701da177e4SLinus Torvalds 	ct82c710_iores.end = ct82c710_iores.start + 1;
1711da177e4SLinus Torvalds 	ct82c710_iores.flags = IORESOURCE_IO;
1721da177e4SLinus Torvalds 	outb_p(0x0f, 0x390);
1731da177e4SLinus Torvalds 	outb_p(0x0f, 0x391);				/* Close config mode */
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	return 0;
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
178*916d83cfSDmitry Torokhov static int __devinit ct82c710_probe(struct platform_device *dev)
1791da177e4SLinus Torvalds {
180*916d83cfSDmitry Torokhov 	ct82c710_port = kzalloc(sizeof(struct serio), GFP_KERNEL);
181*916d83cfSDmitry Torokhov 	if (!ct82c710_port)
182*916d83cfSDmitry Torokhov 		return -ENOMEM;
1831da177e4SLinus Torvalds 
184*916d83cfSDmitry Torokhov 	ct82c710_port->id.type = SERIO_8042;
185*916d83cfSDmitry Torokhov 	ct82c710_port->dev.parent = &dev->dev;
186*916d83cfSDmitry Torokhov 	ct82c710_port->open = ct82c710_open;
187*916d83cfSDmitry Torokhov 	ct82c710_port->close = ct82c710_close;
188*916d83cfSDmitry Torokhov 	ct82c710_port->write = ct82c710_write;
189*916d83cfSDmitry Torokhov 	strlcpy(ct82c710_port->name, "C&T 82c710 mouse port",
190*916d83cfSDmitry Torokhov 		sizeof(ct82c710_port->name));
191*916d83cfSDmitry Torokhov 	snprintf(ct82c710_port->phys, sizeof(ct82c710_port->phys),
192*916d83cfSDmitry Torokhov 		 "isa%04lx/serio0", CT82C710_DATA);
193*916d83cfSDmitry Torokhov 
194*916d83cfSDmitry Torokhov 	serio_register_port(ct82c710_port);
195*916d83cfSDmitry Torokhov 
196*916d83cfSDmitry Torokhov 	return 0;
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
199*916d83cfSDmitry Torokhov static int __devexit ct82c710_remove(struct platform_device *dev)
200*916d83cfSDmitry Torokhov {
201*916d83cfSDmitry Torokhov 	serio_unregister_port(ct82c710_port);
202*916d83cfSDmitry Torokhov 
203*916d83cfSDmitry Torokhov 	return 0;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
206*916d83cfSDmitry Torokhov static struct platform_driver ct82c710_driver = {
207*916d83cfSDmitry Torokhov 	.driver		= {
208*916d83cfSDmitry Torokhov 		.name	= "ct82c710",
209*916d83cfSDmitry Torokhov 		.owner	= THIS_MODULE,
210*916d83cfSDmitry Torokhov 	},
211*916d83cfSDmitry Torokhov 	.probe		= ct82c710_probe,
212*916d83cfSDmitry Torokhov 	.remove		= __devexit_p(ct82c710_remove),
213*916d83cfSDmitry Torokhov };
214*916d83cfSDmitry Torokhov 
215*916d83cfSDmitry Torokhov 
2161da177e4SLinus Torvalds static int __init ct82c710_init(void)
2171da177e4SLinus Torvalds {
218*916d83cfSDmitry Torokhov 	int error;
2191da177e4SLinus Torvalds 
220*916d83cfSDmitry Torokhov 	error = ct82c710_detect();
221*916d83cfSDmitry Torokhov 	if (error)
222*916d83cfSDmitry Torokhov 		return error;
2231da177e4SLinus Torvalds 
224*916d83cfSDmitry Torokhov 	error = platform_driver_register(&ct82c710_driver);
225*916d83cfSDmitry Torokhov 	if (error)
226*916d83cfSDmitry Torokhov 		return error;
227*916d83cfSDmitry Torokhov 
228*916d83cfSDmitry Torokhov 	ct82c710_device = platform_device_alloc("ct82c710", -1);
229*916d83cfSDmitry Torokhov 	if (!ct82c710_device) {
230*916d83cfSDmitry Torokhov 		error = -ENOMEM;
231*916d83cfSDmitry Torokhov 		goto err_unregister_driver;
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 
234*916d83cfSDmitry Torokhov 	error = platform_device_add_resources(ct82c710_device, &ct82c710_iores, 1);
235*916d83cfSDmitry Torokhov 	if (error)
236*916d83cfSDmitry Torokhov 		goto err_free_device;
237*916d83cfSDmitry Torokhov 
238*916d83cfSDmitry Torokhov 	error = platform_device_add(ct82c710_device);
239*916d83cfSDmitry Torokhov 	if (error)
240*916d83cfSDmitry Torokhov 		goto err_free_device;
241*916d83cfSDmitry Torokhov 
2421da177e4SLinus Torvalds 	serio_register_port(ct82c710_port);
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	printk(KERN_INFO "serio: C&T 82c710 mouse port at %#lx irq %d\n",
2451da177e4SLinus Torvalds 		CT82C710_DATA, CT82C710_IRQ);
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	return 0;
248*916d83cfSDmitry Torokhov 
249*916d83cfSDmitry Torokhov  err_free_device:
250*916d83cfSDmitry Torokhov 	platform_device_put(ct82c710_device);
251*916d83cfSDmitry Torokhov  err_unregister_driver:
252*916d83cfSDmitry Torokhov 	platform_driver_unregister(&ct82c710_driver);
253*916d83cfSDmitry Torokhov 	return error;
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds static void __exit ct82c710_exit(void)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	platform_device_unregister(ct82c710_device);
259*916d83cfSDmitry Torokhov 	platform_driver_unregister(&ct82c710_driver);
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds module_init(ct82c710_init);
2631da177e4SLinus Torvalds module_exit(ct82c710_exit);
264