Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  linux/drivers/input/serio/sa1111ps2.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (C) 2002 Russell King
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/serio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/hardware/sa1111.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define PS2CR		0x0000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define PS2STAT		0x0004
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define PS2DATA		0x0008
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define PS2CLKDIV	0x000c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define PS2PRECNT	0x0010
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define PS2CR_ENA	0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define PS2CR_FKD	0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define PS2CR_FKC	0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define PS2STAT_STP	0x0100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define PS2STAT_TXE	0x0080
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define PS2STAT_TXB	0x0040
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define PS2STAT_RXF	0x0020
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define PS2STAT_RXB	0x0010
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define PS2STAT_ENA	0x0008
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define PS2STAT_RXP	0x0004
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define PS2STAT_KBD	0x0002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define PS2STAT_KBC	0x0001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) struct ps2if {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct serio		*io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct sa1111_dev	*dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	void __iomem		*base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	int			rx_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int			tx_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	unsigned int		open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	spinlock_t		lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	unsigned int		head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	unsigned int		tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	unsigned char		buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * Read all bytes waiting in the PS2 port.  There should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * at the most one, but we loop for safety.  If there was a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * framing error, we have to manually clear the status.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static irqreturn_t ps2_rxint(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct ps2if *ps2if = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	unsigned int scancode, flag, status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	status = readl_relaxed(ps2if->base + PS2STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	while (status & PS2STAT_RXF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		if (status & PS2STAT_STP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			writel_relaxed(PS2STAT_STP, ps2if->base + PS2STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		flag = (status & PS2STAT_STP ? SERIO_FRAME : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		       (status & PS2STAT_RXP ? 0 : SERIO_PARITY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		scancode = readl_relaxed(ps2if->base + PS2DATA) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		if (hweight8(scancode) & 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			flag ^= SERIO_PARITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		serio_interrupt(ps2if->io, scancode, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		status = readl_relaxed(ps2if->base + PS2STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)         }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)         return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * Completion of ps2 write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static irqreturn_t ps2_txint(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct ps2if *ps2if = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	unsigned int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	spin_lock(&ps2if->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	status = readl_relaxed(ps2if->base + PS2STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (ps2if->head == ps2if->tail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		disable_irq_nosync(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		/* done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	} else if (status & PS2STAT_TXE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		writel_relaxed(ps2if->buf[ps2if->tail], ps2if->base + PS2DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		ps2if->tail = (ps2if->tail + 1) & (sizeof(ps2if->buf) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	spin_unlock(&ps2if->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * Write a byte to the PS2 port.  We have to wait for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  * port to indicate that the transmitter is empty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static int ps2_write(struct serio *io, unsigned char val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct ps2if *ps2if = io->port_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	unsigned int head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	spin_lock_irqsave(&ps2if->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	 * If the TX register is empty, we can go straight out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (readl_relaxed(ps2if->base + PS2STAT) & PS2STAT_TXE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		writel_relaxed(val, ps2if->base + PS2DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		if (ps2if->head == ps2if->tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			enable_irq(ps2if->tx_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		head = (ps2if->head + 1) & (sizeof(ps2if->buf) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		if (head != ps2if->tail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			ps2if->buf[ps2if->head] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			ps2if->head = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	spin_unlock_irqrestore(&ps2if->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static int ps2_open(struct serio *io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	struct ps2if *ps2if = io->port_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	ret = sa1111_enable_device(ps2if->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	ret = request_irq(ps2if->rx_irq, ps2_rxint, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			  SA1111_DRIVER_NAME(ps2if->dev), ps2if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			ps2if->rx_irq, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		sa1111_disable_device(ps2if->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	ret = request_irq(ps2if->tx_irq, ps2_txint, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			  SA1111_DRIVER_NAME(ps2if->dev), ps2if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		printk(KERN_ERR "sa1111ps2: could not allocate IRQ%d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			ps2if->tx_irq, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		free_irq(ps2if->rx_irq, ps2if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		sa1111_disable_device(ps2if->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	ps2if->open = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	enable_irq_wake(ps2if->rx_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	writel_relaxed(PS2CR_ENA, ps2if->base + PS2CR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static void ps2_close(struct serio *io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	struct ps2if *ps2if = io->port_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	writel_relaxed(0, ps2if->base + PS2CR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	disable_irq_wake(ps2if->rx_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	ps2if->open = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	free_irq(ps2if->tx_irq, ps2if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	free_irq(ps2if->rx_irq, ps2if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	sa1111_disable_device(ps2if->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  * Clear the input buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static void ps2_clear_input(struct ps2if *ps2if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	int maxread = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	while (maxread--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		if ((readl_relaxed(ps2if->base + PS2DATA) & 0xff) == 0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static unsigned int ps2_test_one(struct ps2if *ps2if,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 					   unsigned int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	writel_relaxed(PS2CR_ENA | mask, ps2if->base + PS2CR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	udelay(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	val = readl_relaxed(ps2if->base + PS2STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return val & (PS2STAT_KBC | PS2STAT_KBD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)  * Test the keyboard interface.  We basically check to make sure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  * we can drive each line to the keyboard independently of each other.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static int ps2_test(struct ps2if *ps2if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	unsigned int stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	stat = ps2_test_one(ps2if, PS2CR_FKC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (stat != PS2STAT_KBD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		printk("PS/2 interface test failed[1]: %02x\n", stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	stat = ps2_test_one(ps2if, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (stat != (PS2STAT_KBC | PS2STAT_KBD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		printk("PS/2 interface test failed[2]: %02x\n", stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	stat = ps2_test_one(ps2if, PS2CR_FKD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (stat != PS2STAT_KBC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		printk("PS/2 interface test failed[3]: %02x\n", stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	writel_relaxed(0, ps2if->base + PS2CR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)  * Add one device to this driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static int ps2_probe(struct sa1111_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	struct ps2if *ps2if;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct serio *serio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	ps2if = kzalloc(sizeof(struct ps2if), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (!ps2if || !serio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	serio->id.type		= SERIO_8042;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	serio->write		= ps2_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	serio->open		= ps2_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	serio->close		= ps2_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	strlcpy(serio->name, dev_name(&dev->dev), sizeof(serio->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	serio->port_data	= ps2if;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	serio->dev.parent	= &dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	ps2if->io		= serio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	ps2if->dev		= dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	sa1111_set_drvdata(dev, ps2if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	spin_lock_init(&ps2if->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	ps2if->rx_irq = sa1111_get_irq(dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (ps2if->rx_irq <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		ret = ps2if->rx_irq ? : -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	ps2if->tx_irq = sa1111_get_irq(dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (ps2if->tx_irq <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		ret = ps2if->tx_irq ? : -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	 * Request the physical region for this PS2 port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (!request_mem_region(dev->res.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 				dev->res.end - dev->res.start + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 				SA1111_DRIVER_NAME(dev))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	 * Our parent device has already mapped the region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	ps2if->base = dev->mapbase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	sa1111_enable_device(ps2if->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	/* Incoming clock is 8MHz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	writel_relaxed(0, ps2if->base + PS2CLKDIV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	writel_relaxed(127, ps2if->base + PS2PRECNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	 * Flush any pending input.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	ps2_clear_input(ps2if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	 * Test the keyboard interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	ret = ps2_test(ps2if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	 * Flush any pending input.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	ps2_clear_input(ps2if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	sa1111_disable_device(ps2if->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	serio_register_port(ps2if->io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	sa1111_disable_device(ps2if->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	release_mem_region(dev->res.start, resource_size(&dev->res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)  free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	sa1111_set_drvdata(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	kfree(ps2if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	kfree(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  * Remove one device from this driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static int ps2_remove(struct sa1111_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	struct ps2if *ps2if = sa1111_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	serio_unregister_port(ps2if->io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	release_mem_region(dev->res.start, resource_size(&dev->res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	sa1111_set_drvdata(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	kfree(ps2if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)  * Our device driver structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static struct sa1111_driver ps2_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	.drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		.name	= "sa1111-ps2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		.owner	= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	.devid		= SA1111_DEVID_PS2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	.probe		= ps2_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	.remove		= ps2_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static int __init ps2_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	return sa1111_driver_register(&ps2_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static void __exit ps2_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	sa1111_driver_unregister(&ps2_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) module_init(ps2_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) module_exit(ps2_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) MODULE_DESCRIPTION("SA1111 PS2 controller driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) MODULE_LICENSE("GPL");