xref: /linux/drivers/input/serio/ct82c710.c (revision d052d1beff706920e82c5d55006b08e256b5df09)
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>
40*d052d1beSRussell 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 
1571da177e4SLinus Torvalds static int __init ct82c710_probe(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? */
1661da177e4SLinus Torvalds 		return -1;				/* 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 
1781da177e4SLinus Torvalds static struct serio * __init ct82c710_allocate_port(void)
1791da177e4SLinus Torvalds {
1801da177e4SLinus Torvalds 	struct serio *serio;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
1831da177e4SLinus Torvalds 	if (serio) {
1841da177e4SLinus Torvalds 		memset(serio, 0, sizeof(struct serio));
1851da177e4SLinus Torvalds 		serio->id.type = SERIO_8042;
1861da177e4SLinus Torvalds 		serio->open = ct82c710_open;
1871da177e4SLinus Torvalds 		serio->close = ct82c710_close;
1881da177e4SLinus Torvalds 		serio->write = ct82c710_write;
1891da177e4SLinus Torvalds 		serio->dev.parent = &ct82c710_device->dev;
1901da177e4SLinus Torvalds 		strlcpy(serio->name, "C&T 82c710 mouse port", sizeof(serio->name));
1911da177e4SLinus Torvalds 		snprintf(serio->phys, sizeof(serio->phys), "isa%04lx/serio0", CT82C710_DATA);
1921da177e4SLinus Torvalds 	}
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	return serio;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds static int __init ct82c710_init(void)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	if (ct82c710_probe())
2001da177e4SLinus Torvalds 		return -ENODEV;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	ct82c710_device = platform_device_register_simple("ct82c710", -1, &ct82c710_iores, 1);
2031da177e4SLinus Torvalds 	if (IS_ERR(ct82c710_device))
2041da177e4SLinus Torvalds 		return PTR_ERR(ct82c710_device);
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	if (!(ct82c710_port = ct82c710_allocate_port())) {
2071da177e4SLinus Torvalds 		platform_device_unregister(ct82c710_device);
2081da177e4SLinus Torvalds 		return -ENOMEM;
2091da177e4SLinus Torvalds 	}
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	serio_register_port(ct82c710_port);
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	printk(KERN_INFO "serio: C&T 82c710 mouse port at %#lx irq %d\n",
2141da177e4SLinus Torvalds 		CT82C710_DATA, CT82C710_IRQ);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	return 0;
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds static void __exit ct82c710_exit(void)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	serio_unregister_port(ct82c710_port);
2221da177e4SLinus Torvalds 	platform_device_unregister(ct82c710_device);
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds module_init(ct82c710_init);
2261da177e4SLinus Torvalds module_exit(ct82c710_exit);
227