xref: /linux/drivers/input/serio/rpckbd.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Copyright (c) 2000-2001 Vojtech Pavlik
41da177e4SLinus Torvalds  *  Copyright (c) 2002 Russell King
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Acorn RiscPC PS/2 keyboard controller driver for Linux/ARM
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/interrupt.h>
131da177e4SLinus Torvalds #include <linux/serio.h>
141da177e4SLinus Torvalds #include <linux/err.h>
15d052d1beSRussell King #include <linux/platform_device.h>
1699730225SRussell King #include <linux/io.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
181da177e4SLinus Torvalds 
19a09e64fbSRussell King #include <mach/hardware.h>
201da177e4SLinus Torvalds #include <asm/hardware/iomd.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik, Russell King");
231da177e4SLinus Torvalds MODULE_DESCRIPTION("Acorn RiscPC PS/2 keyboard controller driver");
241da177e4SLinus Torvalds MODULE_LICENSE("GPL");
25d7b5247bSKay Sievers MODULE_ALIAS("platform:kart");
261da177e4SLinus Torvalds 
278c6d9d0aSRussell King struct rpckbd_data {
288c6d9d0aSRussell King 	int tx_irq;
298c6d9d0aSRussell King 	int rx_irq;
308c6d9d0aSRussell King };
318c6d9d0aSRussell King 
rpckbd_write(struct serio * port,unsigned char val)321da177e4SLinus Torvalds static int rpckbd_write(struct serio *port, unsigned char val)
331da177e4SLinus Torvalds {
341da177e4SLinus Torvalds 	while (!(iomd_readb(IOMD_KCTRL) & (1 << 7)))
351da177e4SLinus Torvalds 		cpu_relax();
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds 	iomd_writeb(val, IOMD_KARTTX);
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds 	return 0;
401da177e4SLinus Torvalds }
411da177e4SLinus Torvalds 
rpckbd_rx(int irq,void * dev_id)427d12e780SDavid Howells static irqreturn_t rpckbd_rx(int irq, void *dev_id)
431da177e4SLinus Torvalds {
441da177e4SLinus Torvalds 	struct serio *port = dev_id;
451da177e4SLinus Torvalds 	unsigned int byte;
461da177e4SLinus Torvalds 	int handled = IRQ_NONE;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	while (iomd_readb(IOMD_KCTRL) & (1 << 5)) {
491da177e4SLinus Torvalds 		byte = iomd_readb(IOMD_KARTRX);
501da177e4SLinus Torvalds 
517d12e780SDavid Howells 		serio_interrupt(port, byte, 0);
521da177e4SLinus Torvalds 		handled = IRQ_HANDLED;
531da177e4SLinus Torvalds 	}
541da177e4SLinus Torvalds 	return handled;
551da177e4SLinus Torvalds }
561da177e4SLinus Torvalds 
rpckbd_tx(int irq,void * dev_id)577d12e780SDavid Howells static irqreturn_t rpckbd_tx(int irq, void *dev_id)
581da177e4SLinus Torvalds {
591da177e4SLinus Torvalds 	return IRQ_HANDLED;
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds 
rpckbd_open(struct serio * port)621da177e4SLinus Torvalds static int rpckbd_open(struct serio *port)
631da177e4SLinus Torvalds {
648c6d9d0aSRussell King 	struct rpckbd_data *rpckbd = port->port_data;
658c6d9d0aSRussell King 
661da177e4SLinus Torvalds 	/* Reset the keyboard state machine. */
671da177e4SLinus Torvalds 	iomd_writeb(0, IOMD_KCTRL);
681da177e4SLinus Torvalds 	iomd_writeb(8, IOMD_KCTRL);
691da177e4SLinus Torvalds 	iomd_readb(IOMD_KARTRX);
701da177e4SLinus Torvalds 
718c6d9d0aSRussell King 	if (request_irq(rpckbd->rx_irq, rpckbd_rx, 0, "rpckbd", port) != 0) {
721da177e4SLinus Torvalds 		printk(KERN_ERR "rpckbd.c: Could not allocate keyboard receive IRQ\n");
731da177e4SLinus Torvalds 		return -EBUSY;
741da177e4SLinus Torvalds 	}
751da177e4SLinus Torvalds 
768c6d9d0aSRussell King 	if (request_irq(rpckbd->tx_irq, rpckbd_tx, 0, "rpckbd", port) != 0) {
771da177e4SLinus Torvalds 		printk(KERN_ERR "rpckbd.c: Could not allocate keyboard transmit IRQ\n");
788c6d9d0aSRussell King 		free_irq(rpckbd->rx_irq, port);
791da177e4SLinus Torvalds 		return -EBUSY;
801da177e4SLinus Torvalds 	}
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	return 0;
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
rpckbd_close(struct serio * port)851da177e4SLinus Torvalds static void rpckbd_close(struct serio *port)
861da177e4SLinus Torvalds {
878c6d9d0aSRussell King 	struct rpckbd_data *rpckbd = port->port_data;
888c6d9d0aSRussell King 
898c6d9d0aSRussell King 	free_irq(rpckbd->rx_irq, port);
908c6d9d0aSRussell King 	free_irq(rpckbd->tx_irq, port);
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /*
941da177e4SLinus Torvalds  * Allocate and initialize serio structure for subsequent registration
951da177e4SLinus Torvalds  * with serio core.
961da177e4SLinus Torvalds  */
rpckbd_probe(struct platform_device * dev)975298cc4cSBill Pemberton static int rpckbd_probe(struct platform_device *dev)
981da177e4SLinus Torvalds {
998c6d9d0aSRussell King 	struct rpckbd_data *rpckbd;
1001da177e4SLinus Torvalds 	struct serio *serio;
1018c6d9d0aSRussell King 	int tx_irq, rx_irq;
1028c6d9d0aSRussell King 
1038c6d9d0aSRussell King 	rx_irq = platform_get_irq(dev, 0);
10454116d44SRuan Jinjie 	if (rx_irq < 0)
10554116d44SRuan Jinjie 		return rx_irq;
1068c6d9d0aSRussell King 
1078c6d9d0aSRussell King 	tx_irq = platform_get_irq(dev, 1);
10854116d44SRuan Jinjie 	if (tx_irq < 0)
10954116d44SRuan Jinjie 		return tx_irq;
1101da177e4SLinus Torvalds 
111*06b449d7SErick Archer 	serio = kzalloc(sizeof(*serio), GFP_KERNEL);
1128c6d9d0aSRussell King 	rpckbd = kzalloc(sizeof(*rpckbd), GFP_KERNEL);
1138c6d9d0aSRussell King 	if (!serio || !rpckbd) {
1148c6d9d0aSRussell King 		kfree(rpckbd);
1158c6d9d0aSRussell King 		kfree(serio);
1161da177e4SLinus Torvalds 		return -ENOMEM;
1178c6d9d0aSRussell King 	}
1188c6d9d0aSRussell King 
1198c6d9d0aSRussell King 	rpckbd->rx_irq = rx_irq;
1208c6d9d0aSRussell King 	rpckbd->tx_irq = tx_irq;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	serio->id.type		= SERIO_8042;
1231da177e4SLinus Torvalds 	serio->write		= rpckbd_write;
1241da177e4SLinus Torvalds 	serio->open		= rpckbd_open;
1251da177e4SLinus Torvalds 	serio->close		= rpckbd_close;
1263ae5eaecSRussell King 	serio->dev.parent	= &dev->dev;
1278c6d9d0aSRussell King 	serio->port_data	= rpckbd;
128a9f08ad7SWolfram Sang 	strscpy(serio->name, "RiscPC PS/2 kbd port", sizeof(serio->name));
129a9f08ad7SWolfram Sang 	strscpy(serio->phys, "rpckbd/serio0", sizeof(serio->phys));
1301da177e4SLinus Torvalds 
1313ae5eaecSRussell King 	platform_set_drvdata(dev, serio);
1321da177e4SLinus Torvalds 	serio_register_port(serio);
1331da177e4SLinus Torvalds 	return 0;
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
rpckbd_remove(struct platform_device * dev)136d4904adbSUwe Kleine-König static void rpckbd_remove(struct platform_device *dev)
1371da177e4SLinus Torvalds {
1383ae5eaecSRussell King 	struct serio *serio = platform_get_drvdata(dev);
1398c6d9d0aSRussell King 	struct rpckbd_data *rpckbd = serio->port_data;
1408c6d9d0aSRussell King 
1411da177e4SLinus Torvalds 	serio_unregister_port(serio);
1428c6d9d0aSRussell King 	kfree(rpckbd);
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1453ae5eaecSRussell King static struct platform_driver rpckbd_driver = {
1461da177e4SLinus Torvalds 	.probe		= rpckbd_probe,
147d4904adbSUwe Kleine-König 	.remove_new	= rpckbd_remove,
1483ae5eaecSRussell King 	.driver		= {
1493ae5eaecSRussell King 		.name	= "kart",
1503ae5eaecSRussell King 	},
1511da177e4SLinus Torvalds };
15224d2469aSJJ Ding module_platform_driver(rpckbd_driver);
153