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 #include <linux/delay.h>
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/ioport.h>
131da177e4SLinus Torvalds #include <linux/init.h>
141da177e4SLinus Torvalds #include <linux/interrupt.h>
151da177e4SLinus Torvalds #include <linux/serio.h>
161da177e4SLinus Torvalds #include <linux/errno.h>
171da177e4SLinus Torvalds #include <linux/err.h>
18d052d1beSRussell King #include <linux/platform_device.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
201da177e4SLinus Torvalds
211da177e4SLinus Torvalds #include <asm/io.h>
221da177e4SLinus Torvalds
231da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
241da177e4SLinus Torvalds MODULE_DESCRIPTION("82C710 C&T mouse port chip driver");
251da177e4SLinus Torvalds MODULE_LICENSE("GPL");
261da177e4SLinus Torvalds
271da177e4SLinus Torvalds /*
281da177e4SLinus Torvalds * ct82c710 interface
291da177e4SLinus Torvalds */
301da177e4SLinus Torvalds
311da177e4SLinus Torvalds #define CT82C710_DEV_IDLE 0x01 /* Device Idle */
321da177e4SLinus Torvalds #define CT82C710_RX_FULL 0x02 /* Device Char received */
331da177e4SLinus Torvalds #define CT82C710_TX_IDLE 0x04 /* Device XMIT Idle */
341da177e4SLinus Torvalds #define CT82C710_RESET 0x08 /* Device Reset */
351da177e4SLinus Torvalds #define CT82C710_INTS_ON 0x10 /* Device Interrupt On */
361da177e4SLinus Torvalds #define CT82C710_ERROR_FLAG 0x20 /* Device Error */
371da177e4SLinus Torvalds #define CT82C710_CLEAR 0x40 /* Device Clear */
381da177e4SLinus Torvalds #define CT82C710_ENABLE 0x80 /* Device Enable */
391da177e4SLinus Torvalds
401da177e4SLinus Torvalds #define CT82C710_IRQ 12
411da177e4SLinus Torvalds
421da177e4SLinus Torvalds #define CT82C710_DATA ct82c710_iores.start
431da177e4SLinus Torvalds #define CT82C710_STATUS (ct82c710_iores.start + 1)
441da177e4SLinus Torvalds
451da177e4SLinus Torvalds static struct serio *ct82c710_port;
461da177e4SLinus Torvalds static struct platform_device *ct82c710_device;
471da177e4SLinus Torvalds static struct resource ct82c710_iores;
481da177e4SLinus Torvalds
491da177e4SLinus Torvalds /*
501da177e4SLinus Torvalds * Interrupt handler for the 82C710 mouse port. A character
511da177e4SLinus Torvalds * is waiting in the 82C710.
521da177e4SLinus Torvalds */
531da177e4SLinus Torvalds
ct82c710_interrupt(int cpl,void * dev_id)547d12e780SDavid Howells static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id)
551da177e4SLinus Torvalds {
567d12e780SDavid Howells return serio_interrupt(ct82c710_port, inb(CT82C710_DATA), 0);
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds
591da177e4SLinus Torvalds /*
601da177e4SLinus Torvalds * Wait for device to send output char and flush any input char.
611da177e4SLinus Torvalds */
621da177e4SLinus Torvalds
ct82c170_wait(void)631da177e4SLinus Torvalds static int ct82c170_wait(void)
641da177e4SLinus Torvalds {
651da177e4SLinus Torvalds int timeout = 60000;
661da177e4SLinus Torvalds
671da177e4SLinus Torvalds while ((inb(CT82C710_STATUS) & (CT82C710_RX_FULL | CT82C710_TX_IDLE | CT82C710_DEV_IDLE))
681da177e4SLinus Torvalds != (CT82C710_DEV_IDLE | CT82C710_TX_IDLE) && timeout) {
691da177e4SLinus Torvalds
701da177e4SLinus Torvalds if (inb_p(CT82C710_STATUS) & CT82C710_RX_FULL) inb_p(CT82C710_DATA);
711da177e4SLinus Torvalds
721da177e4SLinus Torvalds udelay(1);
731da177e4SLinus Torvalds timeout--;
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds
761da177e4SLinus Torvalds return !timeout;
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds
ct82c710_close(struct serio * serio)791da177e4SLinus Torvalds static void ct82c710_close(struct serio *serio)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds if (ct82c170_wait())
821da177e4SLinus Torvalds printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
831da177e4SLinus Torvalds
841da177e4SLinus Torvalds outb_p(inb_p(CT82C710_STATUS) & ~(CT82C710_ENABLE | CT82C710_INTS_ON), CT82C710_STATUS);
851da177e4SLinus Torvalds
861da177e4SLinus Torvalds if (ct82c170_wait())
871da177e4SLinus Torvalds printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
881da177e4SLinus Torvalds
891da177e4SLinus Torvalds free_irq(CT82C710_IRQ, NULL);
901da177e4SLinus Torvalds }
911da177e4SLinus Torvalds
ct82c710_open(struct serio * serio)921da177e4SLinus Torvalds static int ct82c710_open(struct serio *serio)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds unsigned char status;
95b0f05aadSDavidlohr Bueso int err;
961da177e4SLinus Torvalds
97b0f05aadSDavidlohr Bueso err = request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL);
98b0f05aadSDavidlohr Bueso if (err)
99b0f05aadSDavidlohr Bueso return err;
1001da177e4SLinus Torvalds
1011da177e4SLinus Torvalds status = inb_p(CT82C710_STATUS);
1021da177e4SLinus Torvalds
1031da177e4SLinus Torvalds status |= (CT82C710_ENABLE | CT82C710_RESET);
1041da177e4SLinus Torvalds outb_p(status, CT82C710_STATUS);
1051da177e4SLinus Torvalds
1061da177e4SLinus Torvalds status &= ~(CT82C710_RESET);
1071da177e4SLinus Torvalds outb_p(status, CT82C710_STATUS);
1081da177e4SLinus Torvalds
1091da177e4SLinus Torvalds status |= CT82C710_INTS_ON;
1101da177e4SLinus Torvalds outb_p(status, CT82C710_STATUS); /* Enable interrupts */
1111da177e4SLinus Torvalds
1121da177e4SLinus Torvalds while (ct82c170_wait()) {
1131da177e4SLinus Torvalds printk(KERN_ERR "ct82c710: Device busy in open()\n");
1141da177e4SLinus Torvalds status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON);
1151da177e4SLinus Torvalds outb_p(status, CT82C710_STATUS);
1161da177e4SLinus Torvalds free_irq(CT82C710_IRQ, NULL);
117b0f05aadSDavidlohr Bueso return -EBUSY;
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds
1201da177e4SLinus Torvalds return 0;
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds
1231da177e4SLinus Torvalds /*
1241da177e4SLinus Torvalds * Write to the 82C710 mouse device.
1251da177e4SLinus Torvalds */
1261da177e4SLinus Torvalds
ct82c710_write(struct serio * port,unsigned char c)1271da177e4SLinus Torvalds static int ct82c710_write(struct serio *port, unsigned char c)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds if (ct82c170_wait()) return -1;
1301da177e4SLinus Torvalds outb_p(c, CT82C710_DATA);
1311da177e4SLinus Torvalds return 0;
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds
1341da177e4SLinus Torvalds /*
1351da177e4SLinus Torvalds * See if we can find a 82C710 device. Read mouse address.
1361da177e4SLinus Torvalds */
1371da177e4SLinus Torvalds
ct82c710_detect(void)138916d83cfSDmitry Torokhov static int __init ct82c710_detect(void)
1391da177e4SLinus Torvalds {
1401da177e4SLinus Torvalds outb_p(0x55, 0x2fa); /* Any value except 9, ff or 36 */
1411da177e4SLinus Torvalds outb_p(0xaa, 0x3fa); /* Inverse of 55 */
1421da177e4SLinus Torvalds outb_p(0x36, 0x3fa); /* Address the chip */
1431da177e4SLinus Torvalds outb_p(0xe4, 0x3fa); /* 390/4; 390 = config address */
1441da177e4SLinus Torvalds outb_p(0x1b, 0x2fa); /* Inverse of e4 */
1451da177e4SLinus Torvalds outb_p(0x0f, 0x390); /* Write index */
1461da177e4SLinus Torvalds if (inb_p(0x391) != 0xe4) /* Config address found? */
147916d83cfSDmitry Torokhov return -ENODEV; /* No: no 82C710 here */
1481da177e4SLinus Torvalds
1491da177e4SLinus Torvalds outb_p(0x0d, 0x390); /* Write index */
1501da177e4SLinus Torvalds ct82c710_iores.start = inb_p(0x391) << 2; /* Get mouse I/O address */
1511da177e4SLinus Torvalds ct82c710_iores.end = ct82c710_iores.start + 1;
1521da177e4SLinus Torvalds ct82c710_iores.flags = IORESOURCE_IO;
1531da177e4SLinus Torvalds outb_p(0x0f, 0x390);
1541da177e4SLinus Torvalds outb_p(0x0f, 0x391); /* Close config mode */
1551da177e4SLinus Torvalds
1561da177e4SLinus Torvalds return 0;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds
ct82c710_probe(struct platform_device * dev)1595298cc4cSBill Pemberton static int ct82c710_probe(struct platform_device *dev)
1601da177e4SLinus Torvalds {
161*06b449d7SErick Archer ct82c710_port = kzalloc(sizeof(*ct82c710_port), GFP_KERNEL);
162916d83cfSDmitry Torokhov if (!ct82c710_port)
163916d83cfSDmitry Torokhov return -ENOMEM;
1641da177e4SLinus Torvalds
165916d83cfSDmitry Torokhov ct82c710_port->id.type = SERIO_8042;
166916d83cfSDmitry Torokhov ct82c710_port->dev.parent = &dev->dev;
167916d83cfSDmitry Torokhov ct82c710_port->open = ct82c710_open;
168916d83cfSDmitry Torokhov ct82c710_port->close = ct82c710_close;
169916d83cfSDmitry Torokhov ct82c710_port->write = ct82c710_write;
170a9f08ad7SWolfram Sang strscpy(ct82c710_port->name, "C&T 82c710 mouse port",
171916d83cfSDmitry Torokhov sizeof(ct82c710_port->name));
172916d83cfSDmitry Torokhov snprintf(ct82c710_port->phys, sizeof(ct82c710_port->phys),
173e29419ffSGreg Kroah-Hartman "isa%16llx/serio0", (unsigned long long)CT82C710_DATA);
174916d83cfSDmitry Torokhov
175916d83cfSDmitry Torokhov serio_register_port(ct82c710_port);
176916d83cfSDmitry Torokhov
177f09830abSAxel Lin printk(KERN_INFO "serio: C&T 82c710 mouse port at %#llx irq %d\n",
178f09830abSAxel Lin (unsigned long long)CT82C710_DATA, CT82C710_IRQ);
179f09830abSAxel Lin
180916d83cfSDmitry Torokhov return 0;
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds
ct82c710_remove(struct platform_device * dev)183508366deSUwe Kleine-König static void ct82c710_remove(struct platform_device *dev)
184916d83cfSDmitry Torokhov {
185916d83cfSDmitry Torokhov serio_unregister_port(ct82c710_port);
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds
188916d83cfSDmitry Torokhov static struct platform_driver ct82c710_driver = {
189916d83cfSDmitry Torokhov .driver = {
190916d83cfSDmitry Torokhov .name = "ct82c710",
191916d83cfSDmitry Torokhov },
192916d83cfSDmitry Torokhov .probe = ct82c710_probe,
193508366deSUwe Kleine-König .remove_new = ct82c710_remove,
194916d83cfSDmitry Torokhov };
195916d83cfSDmitry Torokhov
196916d83cfSDmitry Torokhov
ct82c710_init(void)1971da177e4SLinus Torvalds static int __init ct82c710_init(void)
1981da177e4SLinus Torvalds {
199916d83cfSDmitry Torokhov int error;
2001da177e4SLinus Torvalds
201916d83cfSDmitry Torokhov error = ct82c710_detect();
202916d83cfSDmitry Torokhov if (error)
203916d83cfSDmitry Torokhov return error;
2041da177e4SLinus Torvalds
205916d83cfSDmitry Torokhov error = platform_driver_register(&ct82c710_driver);
206916d83cfSDmitry Torokhov if (error)
207916d83cfSDmitry Torokhov return error;
208916d83cfSDmitry Torokhov
209916d83cfSDmitry Torokhov ct82c710_device = platform_device_alloc("ct82c710", -1);
210916d83cfSDmitry Torokhov if (!ct82c710_device) {
211916d83cfSDmitry Torokhov error = -ENOMEM;
212916d83cfSDmitry Torokhov goto err_unregister_driver;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds
215916d83cfSDmitry Torokhov error = platform_device_add_resources(ct82c710_device, &ct82c710_iores, 1);
216916d83cfSDmitry Torokhov if (error)
217916d83cfSDmitry Torokhov goto err_free_device;
218916d83cfSDmitry Torokhov
219916d83cfSDmitry Torokhov error = platform_device_add(ct82c710_device);
220916d83cfSDmitry Torokhov if (error)
221916d83cfSDmitry Torokhov goto err_free_device;
222916d83cfSDmitry Torokhov
2231da177e4SLinus Torvalds return 0;
224916d83cfSDmitry Torokhov
225916d83cfSDmitry Torokhov err_free_device:
226916d83cfSDmitry Torokhov platform_device_put(ct82c710_device);
227916d83cfSDmitry Torokhov err_unregister_driver:
228916d83cfSDmitry Torokhov platform_driver_unregister(&ct82c710_driver);
229916d83cfSDmitry Torokhov return error;
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds
ct82c710_exit(void)2321da177e4SLinus Torvalds static void __exit ct82c710_exit(void)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds platform_device_unregister(ct82c710_device);
235916d83cfSDmitry Torokhov platform_driver_unregister(&ct82c710_driver);
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds
2381da177e4SLinus Torvalds module_init(ct82c710_init);
2391da177e4SLinus Torvalds module_exit(ct82c710_exit);
240