xref: /openbmc/linux/drivers/input/serio/ioc3kbd.c (revision 3cf3cdea)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * SGI IOC3 PS/2 controller driver for linux
4  *
5  * Copyright (C) 2019 Thomas Bogendoerfer <tbogendoerfer@suse.de>
6  *
7  * Based on code Copyright (C) 2005 Stanislaw Skowronek <skylark@unaligned.org>
8  *               Copyright (C) 2009 Johannes Dickgreber <tanzy@gmx.de>
9  */
10 
11 #include <linux/delay.h>
12 #include <linux/init.h>
13 #include <linux/io.h>
14 #include <linux/serio.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 
18 #include <asm/sn/ioc3.h>
19 
20 struct ioc3kbd_data {
21 	struct ioc3_serioregs __iomem *regs;
22 	struct serio *kbd, *aux;
23 	bool kbd_exists, aux_exists;
24 	int irq;
25 };
26 
27 static int ioc3kbd_wait(struct ioc3_serioregs __iomem *regs, u32 mask)
28 {
29 	unsigned long timeout = 0;
30 
31 	while ((readl(&regs->km_csr) & mask) && (timeout < 250)) {
32 		udelay(50);
33 		timeout++;
34 	}
35 	return (timeout >= 250) ? -ETIMEDOUT : 0;
36 }
37 
38 static int ioc3kbd_write(struct serio *dev, u8 val)
39 {
40 	struct ioc3kbd_data *d = dev->port_data;
41 	int ret;
42 
43 	ret = ioc3kbd_wait(d->regs, KM_CSR_K_WRT_PEND);
44 	if (ret)
45 		return ret;
46 
47 	writel(val, &d->regs->k_wd);
48 
49 	return 0;
50 }
51 
52 static int ioc3kbd_start(struct serio *dev)
53 {
54 	struct ioc3kbd_data *d = dev->port_data;
55 
56 	d->kbd_exists = true;
57 	return 0;
58 }
59 
60 static void ioc3kbd_stop(struct serio *dev)
61 {
62 	struct ioc3kbd_data *d = dev->port_data;
63 
64 	d->kbd_exists = false;
65 }
66 
67 static int ioc3aux_write(struct serio *dev, u8 val)
68 {
69 	struct ioc3kbd_data *d = dev->port_data;
70 	int ret;
71 
72 	ret = ioc3kbd_wait(d->regs, KM_CSR_M_WRT_PEND);
73 	if (ret)
74 		return ret;
75 
76 	writel(val, &d->regs->m_wd);
77 
78 	return 0;
79 }
80 
81 static int ioc3aux_start(struct serio *dev)
82 {
83 	struct ioc3kbd_data *d = dev->port_data;
84 
85 	d->aux_exists = true;
86 	return 0;
87 }
88 
89 static void ioc3aux_stop(struct serio *dev)
90 {
91 	struct ioc3kbd_data *d = dev->port_data;
92 
93 	d->aux_exists = false;
94 }
95 
96 static void ioc3kbd_process_data(struct serio *dev, u32 data)
97 {
98 	if (data & KM_RD_VALID_0)
99 		serio_interrupt(dev, (data >> KM_RD_DATA_0_SHIFT) & 0xff, 0);
100 	if (data & KM_RD_VALID_1)
101 		serio_interrupt(dev, (data >> KM_RD_DATA_1_SHIFT) & 0xff, 0);
102 	if (data & KM_RD_VALID_2)
103 		serio_interrupt(dev, (data >> KM_RD_DATA_2_SHIFT) & 0xff, 0);
104 }
105 
106 static irqreturn_t ioc3kbd_intr(int itq, void *dev_id)
107 {
108 	struct ioc3kbd_data *d = dev_id;
109 	u32 data_k, data_m;
110 
111 	data_k = readl(&d->regs->k_rd);
112 	if (d->kbd_exists)
113 		ioc3kbd_process_data(d->kbd, data_k);
114 
115 	data_m = readl(&d->regs->m_rd);
116 	if (d->aux_exists)
117 		ioc3kbd_process_data(d->aux, data_m);
118 
119 	return IRQ_HANDLED;
120 }
121 
122 static int ioc3kbd_probe(struct platform_device *pdev)
123 {
124 	struct ioc3_serioregs __iomem *regs;
125 	struct device *dev = &pdev->dev;
126 	struct ioc3kbd_data *d;
127 	struct serio *sk, *sa;
128 	int irq, ret;
129 
130 	regs = devm_platform_ioremap_resource(pdev, 0);
131 	if (IS_ERR(regs))
132 		return PTR_ERR(regs);
133 
134 	irq = platform_get_irq(pdev, 0);
135 	if (irq < 0)
136 		return -ENXIO;
137 
138 	d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
139 	if (!d)
140 		return -ENOMEM;
141 
142 	sk = kzalloc(sizeof(*sk), GFP_KERNEL);
143 	if (!sk)
144 		return -ENOMEM;
145 
146 	sa = kzalloc(sizeof(*sa), GFP_KERNEL);
147 	if (!sa) {
148 		kfree(sk);
149 		return -ENOMEM;
150 	}
151 
152 	sk->id.type = SERIO_8042;
153 	sk->write = ioc3kbd_write;
154 	sk->start = ioc3kbd_start;
155 	sk->stop = ioc3kbd_stop;
156 	snprintf(sk->name, sizeof(sk->name), "IOC3 keyboard %d", pdev->id);
157 	snprintf(sk->phys, sizeof(sk->phys), "ioc3/serio%dkbd", pdev->id);
158 	sk->port_data = d;
159 	sk->dev.parent = dev;
160 
161 	sa->id.type = SERIO_8042;
162 	sa->write = ioc3aux_write;
163 	sa->start = ioc3aux_start;
164 	sa->stop = ioc3aux_stop;
165 	snprintf(sa->name, sizeof(sa->name), "IOC3 auxiliary %d", pdev->id);
166 	snprintf(sa->phys, sizeof(sa->phys), "ioc3/serio%daux", pdev->id);
167 	sa->port_data = d;
168 	sa->dev.parent = dev;
169 
170 	d->regs = regs;
171 	d->kbd = sk;
172 	d->aux = sa;
173 	d->irq = irq;
174 
175 	platform_set_drvdata(pdev, d);
176 	serio_register_port(d->kbd);
177 	serio_register_port(d->aux);
178 
179 	ret = request_irq(irq, ioc3kbd_intr, IRQF_SHARED, "ioc3-kbd", d);
180 	if (ret) {
181 		dev_err(dev, "could not request IRQ %d\n", irq);
182 		serio_unregister_port(d->kbd);
183 		serio_unregister_port(d->aux);
184 		return ret;
185 	}
186 
187 	/* enable ports */
188 	writel(KM_CSR_K_CLAMP_3 | KM_CSR_M_CLAMP_3, &regs->km_csr);
189 
190 	return 0;
191 }
192 
193 static int ioc3kbd_remove(struct platform_device *pdev)
194 {
195 	struct ioc3kbd_data *d = platform_get_drvdata(pdev);
196 
197 	free_irq(d->irq, d);
198 
199 	serio_unregister_port(d->kbd);
200 	serio_unregister_port(d->aux);
201 
202 	return 0;
203 }
204 
205 static struct platform_driver ioc3kbd_driver = {
206 	.probe          = ioc3kbd_probe,
207 	.remove         = ioc3kbd_remove,
208 	.driver = {
209 		.name = "ioc3-kbd",
210 	},
211 };
212 module_platform_driver(ioc3kbd_driver);
213 
214 MODULE_AUTHOR("Thomas Bogendoerfer <tbogendoerfer@suse.de>");
215 MODULE_DESCRIPTION("SGI IOC3 serio driver");
216 MODULE_LICENSE("GPL");
217