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 disable_irq(drvdata
->irq
);
82 static int __ps2_gpio_write(struct serio
*serio
, unsigned char val
)
84 struct ps2_gpio_data
*drvdata
= serio
->port_data
;
86 disable_irq_nosync(drvdata
->irq
);
87 gpiod_direction_output(drvdata
->gpio_clk
, 0);
89 drvdata
->mode
= PS2_MODE_TX
;
90 drvdata
->tx_byte
= val
;
92 schedule_delayed_work(&drvdata
->tx_work
, usecs_to_jiffies(200));
97 static int ps2_gpio_write(struct serio
*serio
, unsigned char val
)
99 struct ps2_gpio_data
*drvdata
= serio
->port_data
;
103 mutex_lock(&drvdata
->tx_mutex
);
104 __ps2_gpio_write(serio
, val
);
105 if (!wait_for_completion_timeout(&drvdata
->tx_done
,
106 msecs_to_jiffies(10000)))
108 mutex_unlock(&drvdata
->tx_mutex
);
110 __ps2_gpio_write(serio
, val
);
116 static void ps2_gpio_tx_work_fn(struct work_struct
*work
)
118 struct delayed_work
*dwork
= to_delayed_work(work
);
119 struct ps2_gpio_data
*drvdata
= container_of(dwork
,
120 struct ps2_gpio_data
,
123 enable_irq(drvdata
->irq
);
124 gpiod_direction_output(drvdata
->gpio_data
, 0);
125 gpiod_direction_input(drvdata
->gpio_clk
);
128 static irqreturn_t
ps2_gpio_irq_rx(struct ps2_gpio_data
*drvdata
)
130 unsigned char byte
, cnt
;
133 static unsigned long old_jiffies
;
135 byte
= drvdata
->rx_byte
;
136 cnt
= drvdata
->rx_cnt
;
138 if (old_jiffies
== 0)
139 old_jiffies
= jiffies
;
141 if ((jiffies
- old_jiffies
) > usecs_to_jiffies(100)) {
142 dev_err(drvdata
->dev
,
143 "RX: timeout, probably we missed an interrupt\n");
146 old_jiffies
= jiffies
;
148 data
= gpiod_get_value(drvdata
->gpio_data
);
149 if (unlikely(data
< 0)) {
150 dev_err(drvdata
->dev
, "RX: failed to get data gpio val: %d\n",
157 /* start bit should be low */
158 if (unlikely(data
)) {
159 dev_err(drvdata
->dev
, "RX: start bit should be low\n");
171 /* processing data bits */
173 byte
|= (data
<< (cnt
- 1));
176 /* check odd parity */
177 if (!((hweight8(byte
) & 1) ^ data
)) {
178 rxflags
|= SERIO_PARITY
;
179 dev_warn(drvdata
->dev
, "RX: parity error\n");
180 if (!drvdata
->write_enable
)
184 /* Do not send spurious ACK's and NACK's when write fn is
187 if (!drvdata
->write_enable
) {
188 if (byte
== PS2_DEV_RET_NACK
)
190 else if (byte
== PS2_DEV_RET_ACK
)
194 /* Let's send the data without waiting for the stop bit to be
195 * sent. It may happen that we miss the stop bit. When this
196 * happens we have no way to recover from this, certainly
197 * missing the parity bit would be recognized when processing
198 * the stop bit. When missing both, data is lost.
200 serio_interrupt(drvdata
->serio
, byte
, rxflags
);
201 dev_dbg(drvdata
->dev
, "RX: sending byte 0x%x\n", byte
);
204 /* stop bit should be high */
205 if (unlikely(!data
)) {
206 dev_err(drvdata
->dev
, "RX: stop bit should be high\n");
211 goto end
; /* success */
213 dev_err(drvdata
->dev
, "RX: got out of sync with the device\n");
218 goto end
; /* success */
223 __ps2_gpio_write(drvdata
->serio
, PS2_CMD_RESEND
);
225 drvdata
->rx_cnt
= cnt
;
226 drvdata
->rx_byte
= byte
;
230 static irqreturn_t
ps2_gpio_irq_tx(struct ps2_gpio_data
*drvdata
)
232 unsigned char byte
, cnt
;
234 static unsigned long old_jiffies
;
236 cnt
= drvdata
->tx_cnt
;
237 byte
= drvdata
->tx_byte
;
239 if (old_jiffies
== 0)
240 old_jiffies
= jiffies
;
242 if ((jiffies
- old_jiffies
) > usecs_to_jiffies(100)) {
243 dev_err(drvdata
->dev
,
244 "TX: timeout, probably we missed an interrupt\n");
247 old_jiffies
= jiffies
;
251 /* should never happen */
252 dev_err(drvdata
->dev
,
253 "TX: start bit should have been sent already\n");
263 data
= byte
& BIT(cnt
- 1);
264 gpiod_set_value(drvdata
->gpio_data
, data
);
268 data
= !(hweight8(byte
) & 1);
269 gpiod_set_value(drvdata
->gpio_data
, data
);
272 /* release data line to generate stop bit */
273 gpiod_direction_input(drvdata
->gpio_data
);
276 /* Devices generate one extra clock pulse before sending the
281 gpiod_direction_input(drvdata
->gpio_data
);
282 data
= gpiod_get_value(drvdata
->gpio_data
);
284 dev_warn(drvdata
->dev
, "TX: received NACK, retry\n");
288 drvdata
->mode
= PS2_MODE_RX
;
289 complete(&drvdata
->tx_done
);
293 goto end
; /* success */
295 /* Probably we missed the stop bit. Therefore we release data
296 * line and try again.
298 gpiod_direction_input(drvdata
->gpio_data
);
299 dev_err(drvdata
->dev
, "TX: got out of sync with the device\n");
304 goto end
; /* success */
309 gpiod_direction_input(drvdata
->gpio_data
);
310 __ps2_gpio_write(drvdata
->serio
, drvdata
->tx_byte
);
312 drvdata
->tx_cnt
= cnt
;
316 static irqreturn_t
ps2_gpio_irq(int irq
, void *dev_id
)
318 struct ps2_gpio_data
*drvdata
= dev_id
;
320 return drvdata
->mode
? ps2_gpio_irq_tx(drvdata
) :
321 ps2_gpio_irq_rx(drvdata
);
324 static int ps2_gpio_get_props(struct device
*dev
,
325 struct ps2_gpio_data
*drvdata
)
327 drvdata
->gpio_data
= devm_gpiod_get(dev
, "data", GPIOD_IN
);
328 if (IS_ERR(drvdata
->gpio_data
)) {
329 dev_err(dev
, "failed to request data gpio: %ld",
330 PTR_ERR(drvdata
->gpio_data
));
331 return PTR_ERR(drvdata
->gpio_data
);
334 drvdata
->gpio_clk
= devm_gpiod_get(dev
, "clk", GPIOD_IN
);
335 if (IS_ERR(drvdata
->gpio_clk
)) {
336 dev_err(dev
, "failed to request clock gpio: %ld",
337 PTR_ERR(drvdata
->gpio_clk
));
338 return PTR_ERR(drvdata
->gpio_clk
);
341 drvdata
->write_enable
= device_property_read_bool(dev
,
347 static int ps2_gpio_probe(struct platform_device
*pdev
)
349 struct ps2_gpio_data
*drvdata
;
351 struct device
*dev
= &pdev
->dev
;
354 drvdata
= devm_kzalloc(dev
, sizeof(struct ps2_gpio_data
), GFP_KERNEL
);
355 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
356 if (!drvdata
|| !serio
) {
361 error
= ps2_gpio_get_props(dev
, drvdata
);
365 if (gpiod_cansleep(drvdata
->gpio_data
) ||
366 gpiod_cansleep(drvdata
->gpio_clk
)) {
367 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");