xref: /linux/drivers/input/serio/ct82c710.c (revision a9f08ad7adb3d2f90e11efbb40a1246ef95b0c04)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Copyright (c) 1999-2001 Vojtech Pavlik
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds /*
71da177e4SLinus Torvalds  *  82C710 C&T mouse port chip driver for Linux
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds /*
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/delay.h>
141da177e4SLinus Torvalds #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/ioport.h>
161da177e4SLinus Torvalds #include <linux/init.h>
171da177e4SLinus Torvalds #include <linux/interrupt.h>
181da177e4SLinus Torvalds #include <linux/serio.h>
191da177e4SLinus Torvalds #include <linux/errno.h>
201da177e4SLinus Torvalds #include <linux/err.h>
21d052d1beSRussell King #include <linux/platform_device.h>
225a0e3ad6STejun Heo #include <linux/slab.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <asm/io.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
271da177e4SLinus Torvalds MODULE_DESCRIPTION("82C710 C&T mouse port chip driver");
281da177e4SLinus Torvalds MODULE_LICENSE("GPL");
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds /*
311da177e4SLinus Torvalds  * ct82c710 interface
321da177e4SLinus Torvalds  */
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #define CT82C710_DEV_IDLE     0x01		/* Device Idle */
351da177e4SLinus Torvalds #define CT82C710_RX_FULL      0x02		/* Device Char received */
361da177e4SLinus Torvalds #define CT82C710_TX_IDLE      0x04		/* Device XMIT Idle */
371da177e4SLinus Torvalds #define CT82C710_RESET        0x08		/* Device Reset */
381da177e4SLinus Torvalds #define CT82C710_INTS_ON      0x10		/* Device Interrupt On */
391da177e4SLinus Torvalds #define CT82C710_ERROR_FLAG   0x20		/* Device Error */
401da177e4SLinus Torvalds #define CT82C710_CLEAR        0x40		/* Device Clear */
411da177e4SLinus Torvalds #define CT82C710_ENABLE       0x80		/* Device Enable */
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #define CT82C710_IRQ          12
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #define CT82C710_DATA         ct82c710_iores.start
461da177e4SLinus Torvalds #define CT82C710_STATUS       (ct82c710_iores.start + 1)
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds static struct serio *ct82c710_port;
491da177e4SLinus Torvalds static struct platform_device *ct82c710_device;
501da177e4SLinus Torvalds static struct resource ct82c710_iores;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  * Interrupt handler for the 82C710 mouse port. A character
541da177e4SLinus Torvalds  * is waiting in the 82C710.
551da177e4SLinus Torvalds  */
561da177e4SLinus Torvalds 
577d12e780SDavid Howells static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id)
581da177e4SLinus Torvalds {
597d12e780SDavid Howells 	return serio_interrupt(ct82c710_port, inb(CT82C710_DATA), 0);
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds /*
631da177e4SLinus Torvalds  * Wait for device to send output char and flush any input char.
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds static int ct82c170_wait(void)
671da177e4SLinus Torvalds {
681da177e4SLinus Torvalds 	int timeout = 60000;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 	while ((inb(CT82C710_STATUS) & (CT82C710_RX_FULL | CT82C710_TX_IDLE | CT82C710_DEV_IDLE))
711da177e4SLinus Torvalds 		       != (CT82C710_DEV_IDLE | CT82C710_TX_IDLE) && timeout) {
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 		if (inb_p(CT82C710_STATUS) & CT82C710_RX_FULL) inb_p(CT82C710_DATA);
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 		udelay(1);
761da177e4SLinus Torvalds 		timeout--;
771da177e4SLinus Torvalds 	}
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	return !timeout;
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds static void ct82c710_close(struct serio *serio)
831da177e4SLinus Torvalds {
841da177e4SLinus Torvalds 	if (ct82c170_wait())
851da177e4SLinus Torvalds 		printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	outb_p(inb_p(CT82C710_STATUS) & ~(CT82C710_ENABLE | CT82C710_INTS_ON), CT82C710_STATUS);
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	if (ct82c170_wait())
901da177e4SLinus Torvalds 		printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	free_irq(CT82C710_IRQ, NULL);
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds static int ct82c710_open(struct serio *serio)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds 	unsigned char status;
98b0f05aadSDavidlohr Bueso 	int err;
991da177e4SLinus Torvalds 
100b0f05aadSDavidlohr Bueso 	err = request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL);
101b0f05aadSDavidlohr Bueso 	if (err)
102b0f05aadSDavidlohr Bueso 		return err;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	status = inb_p(CT82C710_STATUS);
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	status |= (CT82C710_ENABLE | CT82C710_RESET);
1071da177e4SLinus Torvalds 	outb_p(status, CT82C710_STATUS);
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	status &= ~(CT82C710_RESET);
1101da177e4SLinus Torvalds 	outb_p(status, CT82C710_STATUS);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	status |= CT82C710_INTS_ON;
1131da177e4SLinus Torvalds 	outb_p(status, CT82C710_STATUS);	/* Enable interrupts */
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	while (ct82c170_wait()) {
1161da177e4SLinus Torvalds 		printk(KERN_ERR "ct82c710: Device busy in open()\n");
1171da177e4SLinus Torvalds 		status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON);
1181da177e4SLinus Torvalds 		outb_p(status, CT82C710_STATUS);
1191da177e4SLinus Torvalds 		free_irq(CT82C710_IRQ, NULL);
120b0f05aadSDavidlohr Bueso 		return -EBUSY;
1211da177e4SLinus Torvalds 	}
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	return 0;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /*
1271da177e4SLinus Torvalds  * Write to the 82C710 mouse device.
1281da177e4SLinus Torvalds  */
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds static int ct82c710_write(struct serio *port, unsigned char c)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	if (ct82c170_wait()) return -1;
1331da177e4SLinus Torvalds 	outb_p(c, CT82C710_DATA);
1341da177e4SLinus Torvalds 	return 0;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /*
1381da177e4SLinus Torvalds  * See if we can find a 82C710 device. Read mouse address.
1391da177e4SLinus Torvalds  */
1401da177e4SLinus Torvalds 
141916d83cfSDmitry Torokhov static int __init ct82c710_detect(void)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	outb_p(0x55, 0x2fa);				/* Any value except 9, ff or 36 */
1441da177e4SLinus Torvalds 	outb_p(0xaa, 0x3fa);				/* Inverse of 55 */
1451da177e4SLinus Torvalds 	outb_p(0x36, 0x3fa);				/* Address the chip */
1461da177e4SLinus Torvalds 	outb_p(0xe4, 0x3fa);				/* 390/4; 390 = config address */
1471da177e4SLinus Torvalds 	outb_p(0x1b, 0x2fa);				/* Inverse of e4 */
1481da177e4SLinus Torvalds 	outb_p(0x0f, 0x390);				/* Write index */
1491da177e4SLinus Torvalds 	if (inb_p(0x391) != 0xe4)			/* Config address found? */
150916d83cfSDmitry Torokhov 		return -ENODEV;				/* No: no 82C710 here */
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	outb_p(0x0d, 0x390);				/* Write index */
1531da177e4SLinus Torvalds 	ct82c710_iores.start = inb_p(0x391) << 2;	/* Get mouse I/O address */
1541da177e4SLinus Torvalds 	ct82c710_iores.end = ct82c710_iores.start + 1;
1551da177e4SLinus Torvalds 	ct82c710_iores.flags = IORESOURCE_IO;
1561da177e4SLinus Torvalds 	outb_p(0x0f, 0x390);
1571da177e4SLinus Torvalds 	outb_p(0x0f, 0x391);				/* Close config mode */
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	return 0;
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
1625298cc4cSBill Pemberton static int ct82c710_probe(struct platform_device *dev)
1631da177e4SLinus Torvalds {
164916d83cfSDmitry Torokhov 	ct82c710_port = kzalloc(sizeof(struct serio), GFP_KERNEL);
165916d83cfSDmitry Torokhov 	if (!ct82c710_port)
166916d83cfSDmitry Torokhov 		return -ENOMEM;
1671da177e4SLinus Torvalds 
168916d83cfSDmitry Torokhov 	ct82c710_port->id.type = SERIO_8042;
169916d83cfSDmitry Torokhov 	ct82c710_port->dev.parent = &dev->dev;
170916d83cfSDmitry Torokhov 	ct82c710_port->open = ct82c710_open;
171916d83cfSDmitry Torokhov 	ct82c710_port->close = ct82c710_close;
172916d83cfSDmitry Torokhov 	ct82c710_port->write = ct82c710_write;
173*a9f08ad7SWolfram Sang 	strscpy(ct82c710_port->name, "C&T 82c710 mouse port",
174916d83cfSDmitry Torokhov 		sizeof(ct82c710_port->name));
175916d83cfSDmitry Torokhov 	snprintf(ct82c710_port->phys, sizeof(ct82c710_port->phys),
176e29419ffSGreg Kroah-Hartman 		 "isa%16llx/serio0", (unsigned long long)CT82C710_DATA);
177916d83cfSDmitry Torokhov 
178916d83cfSDmitry Torokhov 	serio_register_port(ct82c710_port);
179916d83cfSDmitry Torokhov 
180f09830abSAxel Lin 	printk(KERN_INFO "serio: C&T 82c710 mouse port at %#llx irq %d\n",
181f09830abSAxel Lin 		(unsigned long long)CT82C710_DATA, CT82C710_IRQ);
182f09830abSAxel Lin 
183916d83cfSDmitry Torokhov 	return 0;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
186e2619cf7SBill Pemberton static int ct82c710_remove(struct platform_device *dev)
187916d83cfSDmitry Torokhov {
188916d83cfSDmitry Torokhov 	serio_unregister_port(ct82c710_port);
189916d83cfSDmitry Torokhov 
190916d83cfSDmitry Torokhov 	return 0;
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
193916d83cfSDmitry Torokhov static struct platform_driver ct82c710_driver = {
194916d83cfSDmitry Torokhov 	.driver		= {
195916d83cfSDmitry Torokhov 		.name	= "ct82c710",
196916d83cfSDmitry Torokhov 	},
197916d83cfSDmitry Torokhov 	.probe		= ct82c710_probe,
1981cb0aa88SBill Pemberton 	.remove		= ct82c710_remove,
199916d83cfSDmitry Torokhov };
200916d83cfSDmitry Torokhov 
201916d83cfSDmitry Torokhov 
2021da177e4SLinus Torvalds static int __init ct82c710_init(void)
2031da177e4SLinus Torvalds {
204916d83cfSDmitry Torokhov 	int error;
2051da177e4SLinus Torvalds 
206916d83cfSDmitry Torokhov 	error = ct82c710_detect();
207916d83cfSDmitry Torokhov 	if (error)
208916d83cfSDmitry Torokhov 		return error;
2091da177e4SLinus Torvalds 
210916d83cfSDmitry Torokhov 	error = platform_driver_register(&ct82c710_driver);
211916d83cfSDmitry Torokhov 	if (error)
212916d83cfSDmitry Torokhov 		return error;
213916d83cfSDmitry Torokhov 
214916d83cfSDmitry Torokhov 	ct82c710_device = platform_device_alloc("ct82c710", -1);
215916d83cfSDmitry Torokhov 	if (!ct82c710_device) {
216916d83cfSDmitry Torokhov 		error = -ENOMEM;
217916d83cfSDmitry Torokhov 		goto err_unregister_driver;
2181da177e4SLinus Torvalds 	}
2191da177e4SLinus Torvalds 
220916d83cfSDmitry Torokhov 	error = platform_device_add_resources(ct82c710_device, &ct82c710_iores, 1);
221916d83cfSDmitry Torokhov 	if (error)
222916d83cfSDmitry Torokhov 		goto err_free_device;
223916d83cfSDmitry Torokhov 
224916d83cfSDmitry Torokhov 	error = platform_device_add(ct82c710_device);
225916d83cfSDmitry Torokhov 	if (error)
226916d83cfSDmitry Torokhov 		goto err_free_device;
227916d83cfSDmitry Torokhov 
2281da177e4SLinus Torvalds 	return 0;
229916d83cfSDmitry Torokhov 
230916d83cfSDmitry Torokhov  err_free_device:
231916d83cfSDmitry Torokhov 	platform_device_put(ct82c710_device);
232916d83cfSDmitry Torokhov  err_unregister_driver:
233916d83cfSDmitry Torokhov 	platform_driver_unregister(&ct82c710_driver);
234916d83cfSDmitry Torokhov 	return error;
2351da177e4SLinus Torvalds }
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds static void __exit ct82c710_exit(void)
2381da177e4SLinus Torvalds {
2391da177e4SLinus Torvalds 	platform_device_unregister(ct82c710_device);
240916d83cfSDmitry Torokhov 	platform_driver_unregister(&ct82c710_driver);
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds module_init(ct82c710_init);
2441da177e4SLinus Torvalds module_exit(ct82c710_exit);
245