1/*
2 * GPIO based serio bus driver for bit banging the PS/2 protocol
3 *
4 * Author: Danilo Krummrich <danilokrummrich@dk-develop.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/gpio/consumer.h>
12#include <linux/interrupt.h>
13#include <linux/module.h>
14#include <linux/serio.h>
15#include <linux/slab.h>
16#include <linux/platform_device.h>
17#include <linux/workqueue.h>
18#include <linux/completion.h>
19#include <linux/mutex.h>
20#include <linux/preempt.h>
21#include <linux/property.h>
22#include <linux/of.h>
23#include <linux/jiffies.h>
24#include <linux/delay.h>
25
26#define DRIVER_NAME "ps2-gpio"
27
28#define PS2_MODE_RX 0
29#define PS2_MODE_TX 1
30
31#define PS2_START_BIT 0
32#define PS2_DATA_BIT0 1
33#define PS2_DATA_BIT1 2
34#define PS2_DATA_BIT2 3
35#define PS2_DATA_BIT3 4
36#define PS2_DATA_BIT4 5
37#define PS2_DATA_BIT5 6
38#define PS2_DATA_BIT6 7
39#define PS2_DATA_BIT7 8
40#define PS2_PARITY_BIT 9
41#define PS2_STOP_BIT 10
42#define PS2_TX_TIMEOUT 11
43#define PS2_ACK_BIT 12
44
45#define PS2_DEV_RET_ACK 0xfa
46#define PS2_DEV_RET_NACK 0xfe
47
48#define PS2_CMD_RESEND 0xfe
49
50struct ps2_gpio_data {
51 struct device *dev;
52 struct serio *serio;
53 unsigned char mode;
54 struct gpio_desc *gpio_clk;
55 struct gpio_desc *gpio_data;
56 bool write_enable;
57 int irq;
58 unsigned char rx_cnt;
59 unsigned char rx_byte;
60 unsigned char tx_cnt;
61 unsigned char tx_byte;
62 struct completion tx_done;
63 struct mutex tx_mutex;
64 struct delayed_work tx_work;
65};
66
67static int ps2_gpio_open(struct serio *serio)
68{
69 struct ps2_gpio_data *drvdata = serio->port_data;
70
71 enable_irq(drvdata->irq);
72 return 0;
73}
74
75static void ps2_gpio_close(struct serio *serio)
76{
77 struct ps2_gpio_data *drvdata = serio->port_data;
78
79 flush_delayed_work(&drvdata->tx_work);
80 disable_irq(drvdata->irq);
81}
82
83static int __ps2_gpio_write(struct serio *serio, unsigned char val)
84{
85 struct ps2_gpio_data *drvdata = serio->port_data;
86
87 disable_irq_nosync(drvdata->irq);
88 gpiod_direction_output(drvdata->gpio_clk, 0);
89
90 drvdata->mode = PS2_MODE_TX;
91 drvdata->tx_byte = val;
92
93 schedule_delayed_work(&drvdata->tx_work, usecs_to_jiffies(200));
94
95 return 0;
96}
97
98static int ps2_gpio_write(struct serio *serio, unsigned char val)
99{
100 struct ps2_gpio_data *drvdata = serio->port_data;
101 int ret = 0;
102
103 if (in_task()) {
104 mutex_lock(&drvdata->tx_mutex);
105 __ps2_gpio_write(serio, val);
106 if (!wait_for_completion_timeout(&drvdata->tx_done,
107 msecs_to_jiffies(10000)))
108 ret = SERIO_TIMEOUT;
109 mutex_unlock(&drvdata->tx_mutex);
110 } else {
111 __ps2_gpio_write(serio, val);
112 }
113
114 return ret;
115}
116
117static void ps2_gpio_tx_work_fn(struct work_struct *work)
118{
119 struct delayed_work *dwork = to_delayed_work(work);
120 struct ps2_gpio_data *drvdata = container_of(dwork,
121 struct ps2_gpio_data,
122 tx_work);
123
124 enable_irq(drvdata->irq);
125 gpiod_direction_output(drvdata->gpio_data, 0);
126 gpiod_direction_input(drvdata->gpio_clk);
127}
128
129static irqreturn_t ps2_gpio_irq_rx(struct ps2_gpio_data *drvdata)
130{
131 unsigned char byte, cnt;
132 int data;
133 int rxflags = 0;
134 static unsigned long old_jiffies;
135
136 byte = drvdata->rx_byte;
137 cnt = drvdata->rx_cnt;
138
139 if (old_jiffies == 0)
140 old_jiffies = jiffies;
141
142 if ((jiffies - old_jiffies) > usecs_to_jiffies(100)) {
143 dev_err(drvdata->dev,
144 "RX: timeout, probably we missed an interrupt\n");
145 goto err;
146 }
147 old_jiffies = jiffies;
148
149 data = gpiod_get_value(drvdata->gpio_data);
150 if (unlikely(data < 0)) {
151 dev_err(drvdata->dev, "RX: failed to get data gpio val: %d\n",
152 data);
153 goto err;
154 }
155
156 switch (cnt) {
157 case PS2_START_BIT:
158 /* start bit should be low */
159 if (unlikely(data)) {
160 dev_err(drvdata->dev, "RX: start bit should be low\n");
161 goto err;
162 }
163 break;
164 case PS2_DATA_BIT0:
165 case PS2_DATA_BIT1:
166 case PS2_DATA_BIT2:
167 case PS2_DATA_BIT3:
168 case PS2_DATA_BIT4:
169 case PS2_DATA_BIT5:
170 case PS2_DATA_BIT6:
171 case PS2_DATA_BIT7:
172 /* processing data bits */
173 if (data)
174 byte |= (data << (cnt - 1));
175 break;
176 case PS2_PARITY_BIT:
177 /* check odd parity */
178 if (!((hweight8(byte) & 1) ^ data)) {
179 rxflags |= SERIO_PARITY;
180 dev_warn(drvdata->dev, "RX: parity error\n");
181 if (!drvdata->write_enable)
182 goto err;
183 }
184
185 /* Do not send spurious ACK's and NACK's when write fn is
186 * not provided.
187 */
188 if (!drvdata->write_enable) {
189 if (byte == PS2_DEV_RET_NACK)
190 goto err;
191 else if (byte == PS2_DEV_RET_ACK)
192 break;
193 }
194
195 /* Let's send the data without waiting for the stop bit to be
196 * sent. It may happen that we miss the stop bit. When this
197 * happens we have no way to recover from this, certainly
198 * missing the parity bit would be recognized when processing
199 * the stop bit. When missing both, data is lost.
200 */
201 serio_interrupt(drvdata->serio, byte, rxflags);
202 dev_dbg(drvdata->dev, "RX: sending byte 0x%x\n", byte);
203 break;
204 case PS2_STOP_BIT:
205 /* stop bit should be high */
206 if (unlikely(!data)) {
207 dev_err(drvdata->dev, "RX: stop bit should be high\n");
208 goto err;
209 }
210 cnt = byte = 0;
211 old_jiffies = 0;
212 goto end; /* success */
213 default:
214 dev_err(drvdata->dev, "RX: got out of sync with the device\n");
215 goto err;
216 }
217
218 cnt++;
219 goto end; /* success */
220
221err:
222 cnt = byte = 0;
223 old_jiffies = 0;
224 __ps2_gpio_write(drvdata->serio, PS2_CMD_RESEND);
225end:
226 drvdata->rx_cnt = cnt;
227 drvdata->rx_byte = byte;
228 return IRQ_HANDLED;
229}
230
231static irqreturn_t ps2_gpio_irq_tx(struct ps2_gpio_data *drvdata)
232{
233 unsigned char byte, cnt;
234 int data;
235 static unsigned long old_jiffies;
236
237 cnt = drvdata->tx_cnt;
238 byte = drvdata->tx_byte;
239
240 if (old_jiffies == 0)
241 old_jiffies = jiffies;
242
243 if ((jiffies - old_jiffies) > usecs_to_jiffies(100)) {
244 dev_err(drvdata->dev,
245 "TX: timeout, probably we missed an interrupt\n");
246 goto err;
247 }
248 old_jiffies = jiffies;
249
250 switch (cnt) {
251 case PS2_START_BIT:
252 /* should never happen */
253 dev_err(drvdata->dev,
254 "TX: start bit should have been sent already\n");
255 goto err;
256 case PS2_DATA_BIT0:
257 case PS2_DATA_BIT1:
258 case PS2_DATA_BIT2:
259 case PS2_DATA_BIT3:
260 case PS2_DATA_BIT4:
261 case PS2_DATA_BIT5:
262 case PS2_DATA_BIT6:
263 case PS2_DATA_BIT7:
264 data = byte & BIT(cnt - 1);
265 gpiod_set_value(drvdata->gpio_data, data);
266 break;
267 case PS2_PARITY_BIT:
268 /* do odd parity */
269 data = !(hweight8(byte) & 1);
270 gpiod_set_value(drvdata->gpio_data, data);
271 break;
272 case PS2_STOP_BIT:
273 /* release data line to generate stop bit */
274 gpiod_direction_input(drvdata->gpio_data);
275 break;
276 case PS2_TX_TIMEOUT:
277 /* Devices generate one extra clock pulse before sending the
278 * acknowledgment.
279 */
280 break;
281 case PS2_ACK_BIT:
282 gpiod_direction_input(drvdata->gpio_data);
283 data = gpiod_get_value(drvdata->gpio_data);
284 if (data) {
285 dev_warn(drvdata->dev, "TX: received NACK, retry\n");
286 goto err;
287 }
288
289 drvdata->mode = PS2_MODE_RX;
290 complete(&drvdata->tx_done);
291
292 cnt = 1;
293 old_jiffies = 0;
294 goto end; /* success */
295 default:
296 /* Probably we missed the stop bit. Therefore we release data
297 * line and try again.
298 */
299 gpiod_direction_input(drvdata->gpio_data);
300 dev_err(drvdata->dev, "TX: got out of sync with the device\n");
301 goto err;
302 }
303
304 cnt++;
305 goto end; /* success */
306
307err:
308 cnt = 1;
309 old_jiffies = 0;
310 gpiod_direction_input(drvdata->gpio_data);
311 __ps2_gpio_write(drvdata->serio, drvdata->tx_byte);
312end:
313 drvdata->tx_cnt = cnt;
314 return IRQ_HANDLED;
315}
316
317static irqreturn_t ps2_gpio_irq(int irq, void *dev_id)
318{
319 struct ps2_gpio_data *drvdata = dev_id;
320
321 return drvdata->mode ? ps2_gpio_irq_tx(drvdata) :
322 ps2_gpio_irq_rx(drvdata);
323}
324
325static int ps2_gpio_get_props(struct device *dev,
326 struct ps2_gpio_data *drvdata)
327{
328 drvdata->gpio_data = devm_gpiod_get(dev, "data", GPIOD_IN);
329 if (IS_ERR(drvdata->gpio_data)) {
330 dev_err(dev, "failed to request data gpio: %ld",
331 PTR_ERR(drvdata->gpio_data));
332 return PTR_ERR(drvdata->gpio_data);
333 }
334
335 drvdata->gpio_clk = devm_gpiod_get(dev, "clk", GPIOD_IN);
336 if (IS_ERR(drvdata->gpio_clk)) {
337 dev_err(dev, "failed to request clock gpio: %ld",
338 PTR_ERR(drvdata->gpio_clk));
339 return PTR_ERR(drvdata->gpio_clk);
340 }
341
342 drvdata->write_enable = device_property_read_bool(dev,
343 "write-enable");
344
345 return 0;
346}
347
348static int ps2_gpio_probe(struct platform_device *pdev)
349{
350 struct ps2_gpio_data *drvdata;
351 struct serio *serio;
352 struct device *dev = &pdev->dev;
353 int error;
354
355 drvdata = devm_kzalloc(dev, sizeof(struct ps2_gpio_data), GFP_KERNEL);
356 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
357 if (!drvdata || !serio) {
358 error = -ENOMEM;
359 goto err_free_serio;
360 }
361
362 error = ps2_gpio_get_props(dev, drvdata);
363 if (error)
364 goto err_free_serio;
365
366 if (gpiod_cansleep(drvdata->gpio_data) ||
367 gpiod_cansleep(drvdata->gpio_clk)) {
368 dev_err(dev, "GPIO data or clk are connected via slow bus\n");
369 error = -EINVAL;
370 goto err_free_serio;
371 }
372
373 drvdata->irq = platform_get_irq(pdev, 0);
374 if (drvdata->irq < 0) {
375 dev_err(dev, "failed to get irq from platform resource: %d\n",
376 drvdata->irq);
377 error = drvdata->irq;
378 goto err_free_serio;
379 }
380
381 error = devm_request_irq(dev, drvdata->irq, ps2_gpio_irq,
382 IRQF_NO_THREAD, DRIVER_NAME, drvdata);
383 if (error) {
384 dev_err(dev, "failed to request irq %d: %d\n",
385 drvdata->irq, error);
386 goto err_free_serio;
387 }
388
389 /* Keep irq disabled until serio->open is called. */
390 disable_irq(drvdata->irq);
391
392 serio->id.type = SERIO_8042;
393 serio->open = ps2_gpio_open;
394 serio->close = ps2_gpio_close;
395 /* Write can be enabled in platform/dt data, but possibly it will not
396 * work because of the tough timings.
397 */
398 serio->write = drvdata->write_enable ? ps2_gpio_write : NULL;
399 serio->port_data = drvdata;
400 serio->dev.parent = dev;
401 strlcpy(serio->name, dev_name(dev), sizeof(serio->name));
402 strlcpy(serio->phys, dev_name(dev), sizeof(serio->phys));
403
404 drvdata->serio = serio;
405 drvdata->dev = dev;
406 drvdata->mode = PS2_MODE_RX;
407
408 /* Tx count always starts at 1, as the start bit is sent implicitly by
409 * host-to-device communication initialization.
410 */
411 drvdata->tx_cnt = 1;
412
413 INIT_DELAYED_WORK(&drvdata->tx_work, ps2_gpio_tx_work_fn);
414 init_completion(&drvdata->tx_done);
415 mutex_init(&drvdata->tx_mutex);
416
417 serio_register_port(serio);
418 platform_set_drvdata(pdev, drvdata);
419
420 return 0; /* success */
421
422err_free_serio:
423 kfree(serio);
424 return error;
425}
426
427static int ps2_gpio_remove(struct platform_device *pdev)
428{
429 struct ps2_gpio_data *drvdata = platform_get_drvdata(pdev);
430
431 serio_unregister_port(drvdata->serio);
432 return 0;
433}
434
435#if defined(CONFIG_OF)
436static const struct of_device_id ps2_gpio_match[] = {
437 { .compatible = "ps2-gpio", },
438 { },
439};
440MODULE_DEVICE_TABLE(of, ps2_gpio_match);
441#endif
442
443static struct platform_driver ps2_gpio_driver = {
444 .probe = ps2_gpio_probe,
445 .remove = ps2_gpio_remove,
446 .driver = {
447 .name = DRIVER_NAME,
448 .of_match_table = of_match_ptr(ps2_gpio_match),
449 },
450};
451module_platform_driver(ps2_gpio_driver);
452
453MODULE_AUTHOR("Danilo Krummrich <danilokrummrich@dk-develop.de>");
454MODULE_DESCRIPTION("GPIO PS2 driver");
455MODULE_LICENSE("GPL v2");
456