2 * GPIO based serio bus driver for bit banging the PS/2 protocol
4 * Author: Danilo Krummrich <danilokrummrich@dk-develop.de>
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.
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>
23 #include <linux/jiffies.h>
24 #include <linux/delay.h>
26 #define DRIVER_NAME "ps2-gpio"
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
45 #define PS2_DEV_RET_ACK 0xfa
46 #define PS2_DEV_RET_NACK 0xfe
48 #define PS2_CMD_RESEND 0xfe
50 struct ps2_gpio_data {
54 struct gpio_desc *gpio_clk;
55 struct gpio_desc *gpio_data;
59 unsigned char rx_byte;
61 unsigned char tx_byte;
62 struct completion tx_done;
63 struct mutex tx_mutex;
64 struct delayed_work tx_work;
67 static int ps2_gpio_open(struct serio *serio)
69 struct ps2_gpio_data *drvdata = serio->port_data;
71 enable_irq(drvdata->irq);
75 static void ps2_gpio_close(struct serio *serio)
77 struct ps2_gpio_data *drvdata = serio->port_data;
79 flush_delayed_work(&drvdata->tx_work);
80 disable_irq(drvdata->irq);
83 static int __ps2_gpio_write(struct serio *serio, unsigned char val)
85 struct ps2_gpio_data *drvdata = serio->port_data;
87 disable_irq_nosync(drvdata->irq);
88 gpiod_direction_output(drvdata->gpio_clk, 0);
90 drvdata->mode = PS2_MODE_TX;
91 drvdata->tx_byte = val;
93 schedule_delayed_work(&drvdata->tx_work, usecs_to_jiffies(200));
98 static int ps2_gpio_write(struct serio *serio, unsigned char val)
100 struct ps2_gpio_data *drvdata = serio->port_data;
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)))
109 mutex_unlock(&drvdata->tx_mutex);
111 __ps2_gpio_write(serio, val);
117 static void ps2_gpio_tx_work_fn(struct work_struct *work)
119 struct delayed_work *dwork = to_delayed_work(work);
120 struct ps2_gpio_data *drvdata = container_of(dwork,
121 struct ps2_gpio_data,
124 enable_irq(drvdata->irq);
125 gpiod_direction_output(drvdata->gpio_data, 0);
126 gpiod_direction_input(drvdata->gpio_clk);
129 static irqreturn_t ps2_gpio_irq_rx(struct ps2_gpio_data *drvdata)
131 unsigned char byte, cnt;
134 static unsigned long old_jiffies;
136 byte = drvdata->rx_byte;
137 cnt = drvdata->rx_cnt;
139 if (old_jiffies == 0)
140 old_jiffies = jiffies;
142 if ((jiffies - old_jiffies) > usecs_to_jiffies(100)) {
143 dev_err(drvdata->dev,
144 "RX: timeout, probably we missed an interrupt\n");
147 old_jiffies = jiffies;
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",
158 /* start bit should be low */
159 if (unlikely(data)) {
160 dev_err(drvdata->dev, "RX: start bit should be low\n");
172 /* processing data bits */
174 byte |= (data << (cnt - 1));
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)
185 /* Do not send spurious ACK's and NACK's when write fn is
188 if (!drvdata->write_enable) {
189 if (byte == PS2_DEV_RET_NACK)
191 else if (byte == PS2_DEV_RET_ACK)
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.
201 serio_interrupt(drvdata->serio, byte, rxflags);
202 dev_dbg(drvdata->dev, "RX: sending byte 0x%x\n", byte);
205 /* stop bit should be high */
206 if (unlikely(!data)) {
207 dev_err(drvdata->dev, "RX: stop bit should be high\n");
212 goto end; /* success */
214 dev_err(drvdata->dev, "RX: got out of sync with the device\n");
219 goto end; /* success */
224 __ps2_gpio_write(drvdata->serio, PS2_CMD_RESEND);
226 drvdata->rx_cnt = cnt;
227 drvdata->rx_byte = byte;
231 static irqreturn_t ps2_gpio_irq_tx(struct ps2_gpio_data *drvdata)
233 unsigned char byte, cnt;
235 static unsigned long old_jiffies;
237 cnt = drvdata->tx_cnt;
238 byte = drvdata->tx_byte;
240 if (old_jiffies == 0)
241 old_jiffies = jiffies;
243 if ((jiffies - old_jiffies) > usecs_to_jiffies(100)) {
244 dev_err(drvdata->dev,
245 "TX: timeout, probably we missed an interrupt\n");
248 old_jiffies = jiffies;
252 /* should never happen */
253 dev_err(drvdata->dev,
254 "TX: start bit should have been sent already\n");
264 data = byte & BIT(cnt - 1);
265 gpiod_set_value(drvdata->gpio_data, data);
269 data = !(hweight8(byte) & 1);
270 gpiod_set_value(drvdata->gpio_data, data);
273 /* release data line to generate stop bit */
274 gpiod_direction_input(drvdata->gpio_data);
277 /* Devices generate one extra clock pulse before sending the
282 gpiod_direction_input(drvdata->gpio_data);
283 data = gpiod_get_value(drvdata->gpio_data);
285 dev_warn(drvdata->dev, "TX: received NACK, retry\n");
289 drvdata->mode = PS2_MODE_RX;
290 complete(&drvdata->tx_done);
294 goto end; /* success */
296 /* Probably we missed the stop bit. Therefore we release data
297 * line and try again.
299 gpiod_direction_input(drvdata->gpio_data);
300 dev_err(drvdata->dev, "TX: got out of sync with the device\n");
305 goto end; /* success */
310 gpiod_direction_input(drvdata->gpio_data);
311 __ps2_gpio_write(drvdata->serio, drvdata->tx_byte);
313 drvdata->tx_cnt = cnt;
317 static irqreturn_t ps2_gpio_irq(int irq, void *dev_id)
319 struct ps2_gpio_data *drvdata = dev_id;
321 return drvdata->mode ? ps2_gpio_irq_tx(drvdata) :
322 ps2_gpio_irq_rx(drvdata);
325 static int ps2_gpio_get_props(struct device *dev,
326 struct ps2_gpio_data *drvdata)
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);
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);
342 drvdata->write_enable = device_property_read_bool(dev,
348 static int ps2_gpio_probe(struct platform_device *pdev)
350 struct ps2_gpio_data *drvdata;
352 struct device *dev = &pdev->dev;
355 drvdata = devm_kzalloc(dev, sizeof(struct ps2_gpio_data), GFP_KERNEL);
356 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
357 if (!drvdata || !serio) {
362 error = ps2_gpio_get_props(dev, drvdata);
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");
372 drvdata->irq = platform_get_irq(pdev, 0);
373 if (drvdata->irq < 0) {
374 dev_err(dev, "failed to get irq from platform resource: %d\n",
376 error = drvdata->irq;
380 error = devm_request_irq(dev, drvdata->irq, ps2_gpio_irq,
381 IRQF_NO_THREAD, DRIVER_NAME, drvdata);
383 dev_err(dev, "failed to request irq %d: %d\n",
384 drvdata->irq, error);
388 /* Keep irq disabled until serio->open is called. */
389 disable_irq(drvdata->irq);
391 serio->id.type = SERIO_8042;
392 serio->open = ps2_gpio_open;
393 serio->close = ps2_gpio_close;
394 /* Write can be enabled in platform/dt data, but possibly it will not
395 * work because of the tough timings.
397 serio->write = drvdata->write_enable ? ps2_gpio_write : NULL;
398 serio->port_data = drvdata;
399 serio->dev.parent = dev;
400 strlcpy(serio->name, dev_name(dev), sizeof(serio->name));
401 strlcpy(serio->phys, dev_name(dev), sizeof(serio->phys));
403 drvdata->serio = serio;
405 drvdata->mode = PS2_MODE_RX;
407 /* Tx count always starts at 1, as the start bit is sent implicitly by
408 * host-to-device communication initialization.
412 INIT_DELAYED_WORK(&drvdata->tx_work, ps2_gpio_tx_work_fn);
413 init_completion(&drvdata->tx_done);
414 mutex_init(&drvdata->tx_mutex);
416 serio_register_port(serio);
417 platform_set_drvdata(pdev, drvdata);
419 return 0; /* success */
426 static int ps2_gpio_remove(struct platform_device *pdev)
428 struct ps2_gpio_data *drvdata = platform_get_drvdata(pdev);
430 serio_unregister_port(drvdata->serio);
434 #if defined(CONFIG_OF)
435 static const struct of_device_id ps2_gpio_match[] = {
436 { .compatible = "ps2-gpio", },
439 MODULE_DEVICE_TABLE(of, ps2_gpio_match);
442 static struct platform_driver ps2_gpio_driver = {
443 .probe = ps2_gpio_probe,
444 .remove = ps2_gpio_remove,
447 .of_match_table = of_match_ptr(ps2_gpio_match),
450 module_platform_driver(ps2_gpio_driver);
452 MODULE_AUTHOR("Danilo Krummrich <danilokrummrich@dk-develop.de>");
453 MODULE_DESCRIPTION("GPIO PS2 driver");
454 MODULE_LICENSE("GPL v2");