1 // SPDX-License-Identifier: GPL-2.0-only
3 * GPIO based serio bus driver for bit banging the PS/2 protocol
5 * Author: Danilo Krummrich <danilokrummrich@dk-develop.de>
8 #include <linux/gpio/consumer.h>
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/serio.h>
12 #include <linux/slab.h>
13 #include <linux/platform_device.h>
14 #include <linux/workqueue.h>
15 #include <linux/completion.h>
16 #include <linux/mutex.h>
17 #include <linux/preempt.h>
18 #include <linux/property.h>
20 #include <linux/jiffies.h>
21 #include <linux/delay.h>
22 #include <linux/timekeeping.h>
24 #define DRIVER_NAME "ps2-gpio"
29 #define PS2_START_BIT 0
30 #define PS2_DATA_BIT0 1
31 #define PS2_DATA_BIT1 2
32 #define PS2_DATA_BIT2 3
33 #define PS2_DATA_BIT3 4
34 #define PS2_DATA_BIT4 5
35 #define PS2_DATA_BIT5 6
36 #define PS2_DATA_BIT6 7
37 #define PS2_DATA_BIT7 8
38 #define PS2_PARITY_BIT 9
39 #define PS2_STOP_BIT 10
40 #define PS2_ACK_BIT 11
42 #define PS2_DEV_RET_ACK 0xfa
43 #define PS2_DEV_RET_NACK 0xfe
45 #define PS2_CMD_RESEND 0xfe
48 * The PS2 protocol specifies a clock frequency between 10kHz and 16.7kHz,
49 * therefore the maximal interrupt interval should be 100us and the minimum
50 * interrupt interval should be ~60us. Let's allow +/- 20us for frequency
51 * deviations and interrupt latency.
53 * The data line must be samples after ~30us to 50us after the falling edge,
54 * since the device updates the data line at the rising edge.
56 * ___ ______ ______ ______ ___
59 * \______/ \______/ \______/ \______/
61 * |-----------------| |--------|
62 * 60us/100us 30us/50us
64 #define PS2_CLK_FREQ_MIN_HZ 10000
65 #define PS2_CLK_FREQ_MAX_HZ 16700
66 #define PS2_CLK_MIN_INTERVAL_US ((1000 * 1000) / PS2_CLK_FREQ_MAX_HZ)
67 #define PS2_CLK_MAX_INTERVAL_US ((1000 * 1000) / PS2_CLK_FREQ_MIN_HZ)
68 #define PS2_IRQ_MIN_INTERVAL_US (PS2_CLK_MIN_INTERVAL_US - 20)
69 #define PS2_IRQ_MAX_INTERVAL_US (PS2_CLK_MAX_INTERVAL_US + 20)
71 struct ps2_gpio_data
{
75 struct gpio_desc
*gpio_clk
;
76 struct gpio_desc
*gpio_data
;
90 struct completion complete
;
92 struct delayed_work work
;
96 static int ps2_gpio_open(struct serio
*serio
)
98 struct ps2_gpio_data
*drvdata
= serio
->port_data
;
100 drvdata
->t_irq_last
= 0;
101 drvdata
->tx
.t_xfer_end
= 0;
103 enable_irq(drvdata
->irq
);
107 static void ps2_gpio_close(struct serio
*serio
)
109 struct ps2_gpio_data
*drvdata
= serio
->port_data
;
111 flush_delayed_work(&drvdata
->tx
.work
);
112 disable_irq(drvdata
->irq
);
115 static int __ps2_gpio_write(struct serio
*serio
, unsigned char val
)
117 struct ps2_gpio_data
*drvdata
= serio
->port_data
;
119 disable_irq_nosync(drvdata
->irq
);
120 gpiod_direction_output(drvdata
->gpio_clk
, 0);
122 drvdata
->mode
= PS2_MODE_TX
;
123 drvdata
->tx
.byte
= val
;
125 schedule_delayed_work(&drvdata
->tx
.work
, usecs_to_jiffies(200));
130 static int ps2_gpio_write(struct serio
*serio
, unsigned char val
)
132 struct ps2_gpio_data
*drvdata
= serio
->port_data
;
136 guard(mutex
)(&drvdata
->tx
.mutex
);
138 __ps2_gpio_write(serio
, val
);
139 if (!wait_for_completion_timeout(&drvdata
->tx
.complete
,
140 msecs_to_jiffies(10000)))
143 __ps2_gpio_write(serio
, val
);
149 static void ps2_gpio_tx_work_fn(struct work_struct
*work
)
151 struct delayed_work
*dwork
= to_delayed_work(work
);
152 struct ps2_gpio_data
*drvdata
= container_of(dwork
,
153 struct ps2_gpio_data
,
156 drvdata
->tx
.t_xfer_start
= ktime_get();
157 enable_irq(drvdata
->irq
);
158 gpiod_direction_output(drvdata
->gpio_data
, 0);
159 gpiod_direction_input(drvdata
->gpio_clk
);
162 static irqreturn_t
ps2_gpio_irq_rx(struct ps2_gpio_data
*drvdata
)
164 unsigned char byte
, cnt
;
169 byte
= drvdata
->rx
.byte
;
170 cnt
= drvdata
->rx
.cnt
;
172 drvdata
->t_irq_now
= ktime_get();
175 * We need to consider spurious interrupts happening right after
176 * a TX xfer finished.
178 us_delta
= ktime_us_delta(drvdata
->t_irq_now
, drvdata
->tx
.t_xfer_end
);
179 if (unlikely(us_delta
< PS2_IRQ_MIN_INTERVAL_US
))
182 us_delta
= ktime_us_delta(drvdata
->t_irq_now
, drvdata
->t_irq_last
);
183 if (us_delta
> PS2_IRQ_MAX_INTERVAL_US
&& cnt
) {
184 dev_err(drvdata
->dev
,
185 "RX: timeout, probably we missed an interrupt\n");
187 } else if (unlikely(us_delta
< PS2_IRQ_MIN_INTERVAL_US
)) {
188 /* Ignore spurious IRQs. */
191 drvdata
->t_irq_last
= drvdata
->t_irq_now
;
193 data
= gpiod_get_value(drvdata
->gpio_data
);
194 if (unlikely(data
< 0)) {
195 dev_err(drvdata
->dev
, "RX: failed to get data gpio val: %d\n",
202 /* start bit should be low */
203 if (unlikely(data
)) {
204 dev_err(drvdata
->dev
, "RX: start bit should be low\n");
216 /* processing data bits */
218 byte
|= (data
<< (cnt
- 1));
221 /* check odd parity */
222 if (!((hweight8(byte
) & 1) ^ data
)) {
223 rxflags
|= SERIO_PARITY
;
224 dev_warn(drvdata
->dev
, "RX: parity error\n");
225 if (!drvdata
->write_enable
)
230 /* stop bit should be high */
231 if (unlikely(!data
)) {
232 dev_err(drvdata
->dev
, "RX: stop bit should be high\n");
237 * Do not send spurious ACK's and NACK's when write fn is
240 if (!drvdata
->write_enable
) {
241 if (byte
== PS2_DEV_RET_NACK
)
243 else if (byte
== PS2_DEV_RET_ACK
)
247 serio_interrupt(drvdata
->serio
, byte
, rxflags
);
248 dev_dbg(drvdata
->dev
, "RX: sending byte 0x%x\n", byte
);
252 goto end
; /* success */
254 dev_err(drvdata
->dev
, "RX: got out of sync with the device\n");
259 goto end
; /* success */
263 __ps2_gpio_write(drvdata
->serio
, PS2_CMD_RESEND
);
265 drvdata
->rx
.cnt
= cnt
;
266 drvdata
->rx
.byte
= byte
;
270 static irqreturn_t
ps2_gpio_irq_tx(struct ps2_gpio_data
*drvdata
)
272 unsigned char byte
, cnt
;
276 cnt
= drvdata
->tx
.cnt
;
277 byte
= drvdata
->tx
.byte
;
279 drvdata
->t_irq_now
= ktime_get();
282 * There might be pending IRQs since we disabled IRQs in
283 * __ps2_gpio_write(). We can expect at least one clock period until
284 * the device generates the first falling edge after releasing the
287 us_delta
= ktime_us_delta(drvdata
->t_irq_now
,
288 drvdata
->tx
.t_xfer_start
);
289 if (unlikely(us_delta
< PS2_CLK_MIN_INTERVAL_US
))
292 us_delta
= ktime_us_delta(drvdata
->t_irq_now
, drvdata
->t_irq_last
);
293 if (us_delta
> PS2_IRQ_MAX_INTERVAL_US
&& cnt
> 1) {
294 dev_err(drvdata
->dev
,
295 "TX: timeout, probably we missed an interrupt\n");
297 } else if (unlikely(us_delta
< PS2_IRQ_MIN_INTERVAL_US
)) {
298 /* Ignore spurious IRQs. */
301 drvdata
->t_irq_last
= drvdata
->t_irq_now
;
305 /* should never happen */
306 dev_err(drvdata
->dev
,
307 "TX: start bit should have been sent already\n");
317 data
= byte
& BIT(cnt
- 1);
318 gpiod_set_value(drvdata
->gpio_data
, data
);
322 data
= !(hweight8(byte
) & 1);
323 gpiod_set_value(drvdata
->gpio_data
, data
);
326 /* release data line to generate stop bit */
327 gpiod_direction_input(drvdata
->gpio_data
);
330 data
= gpiod_get_value(drvdata
->gpio_data
);
332 dev_warn(drvdata
->dev
, "TX: received NACK, retry\n");
336 drvdata
->tx
.t_xfer_end
= ktime_get();
337 drvdata
->mode
= PS2_MODE_RX
;
338 complete(&drvdata
->tx
.complete
);
341 goto end
; /* success */
344 * Probably we missed the stop bit. Therefore we release data
345 * line and try again.
347 gpiod_direction_input(drvdata
->gpio_data
);
348 dev_err(drvdata
->dev
, "TX: got out of sync with the device\n");
353 goto end
; /* success */
357 gpiod_direction_input(drvdata
->gpio_data
);
358 __ps2_gpio_write(drvdata
->serio
, drvdata
->tx
.byte
);
360 drvdata
->tx
.cnt
= cnt
;
364 static irqreturn_t
ps2_gpio_irq(int irq
, void *dev_id
)
366 struct ps2_gpio_data
*drvdata
= dev_id
;
368 return drvdata
->mode
? ps2_gpio_irq_tx(drvdata
) :
369 ps2_gpio_irq_rx(drvdata
);
372 static int ps2_gpio_get_props(struct device
*dev
,
373 struct ps2_gpio_data
*drvdata
)
375 enum gpiod_flags gflags
;
377 /* Enforce open drain, since this is required by the PS/2 bus. */
378 gflags
= GPIOD_IN
| GPIOD_FLAGS_BIT_OPEN_DRAIN
;
380 drvdata
->gpio_data
= devm_gpiod_get(dev
, "data", gflags
);
381 if (IS_ERR(drvdata
->gpio_data
)) {
382 dev_err(dev
, "failed to request data gpio: %ld",
383 PTR_ERR(drvdata
->gpio_data
));
384 return PTR_ERR(drvdata
->gpio_data
);
387 drvdata
->gpio_clk
= devm_gpiod_get(dev
, "clk", gflags
);
388 if (IS_ERR(drvdata
->gpio_clk
)) {
389 dev_err(dev
, "failed to request clock gpio: %ld",
390 PTR_ERR(drvdata
->gpio_clk
));
391 return PTR_ERR(drvdata
->gpio_clk
);
394 drvdata
->write_enable
= device_property_read_bool(dev
,
400 static int ps2_gpio_probe(struct platform_device
*pdev
)
402 struct ps2_gpio_data
*drvdata
;
404 struct device
*dev
= &pdev
->dev
;
407 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
408 serio
= kzalloc(sizeof(*serio
), GFP_KERNEL
);
409 if (!drvdata
|| !serio
) {
414 error
= ps2_gpio_get_props(dev
, drvdata
);
418 if (gpiod_cansleep(drvdata
->gpio_data
) ||
419 gpiod_cansleep(drvdata
->gpio_clk
)) {
420 dev_err(dev
, "GPIO data or clk are connected via slow bus\n");
425 drvdata
->irq
= platform_get_irq(pdev
, 0);
426 if (drvdata
->irq
< 0) {
427 error
= drvdata
->irq
;
431 error
= devm_request_irq(dev
, drvdata
->irq
, ps2_gpio_irq
,
432 IRQF_NO_THREAD
| IRQF_NO_AUTOEN
, DRIVER_NAME
,
435 dev_err(dev
, "failed to request irq %d: %d\n",
436 drvdata
->irq
, error
);
440 serio
->id
.type
= SERIO_8042
;
441 serio
->open
= ps2_gpio_open
;
442 serio
->close
= ps2_gpio_close
;
444 * Write can be enabled in platform/dt data, but possibly it will not
445 * work because of the tough timings.
447 serio
->write
= drvdata
->write_enable
? ps2_gpio_write
: NULL
;
448 serio
->port_data
= drvdata
;
449 serio
->dev
.parent
= dev
;
450 strscpy(serio
->name
, dev_name(dev
), sizeof(serio
->name
));
451 strscpy(serio
->phys
, dev_name(dev
), sizeof(serio
->phys
));
453 drvdata
->serio
= serio
;
455 drvdata
->mode
= PS2_MODE_RX
;
458 * Tx count always starts at 1, as the start bit is sent implicitly by
459 * host-to-device communication initialization.
463 INIT_DELAYED_WORK(&drvdata
->tx
.work
, ps2_gpio_tx_work_fn
);
464 init_completion(&drvdata
->tx
.complete
);
465 mutex_init(&drvdata
->tx
.mutex
);
467 serio_register_port(serio
);
468 platform_set_drvdata(pdev
, drvdata
);
470 return 0; /* success */
477 static void ps2_gpio_remove(struct platform_device
*pdev
)
479 struct ps2_gpio_data
*drvdata
= platform_get_drvdata(pdev
);
481 serio_unregister_port(drvdata
->serio
);
484 #if defined(CONFIG_OF)
485 static const struct of_device_id ps2_gpio_match
[] = {
486 { .compatible
= "ps2-gpio", },
489 MODULE_DEVICE_TABLE(of
, ps2_gpio_match
);
492 static struct platform_driver ps2_gpio_driver
= {
493 .probe
= ps2_gpio_probe
,
494 .remove
= ps2_gpio_remove
,
497 .of_match_table
= of_match_ptr(ps2_gpio_match
),
500 module_platform_driver(ps2_gpio_driver
);
502 MODULE_AUTHOR("Danilo Krummrich <danilokrummrich@dk-develop.de>");
503 MODULE_DESCRIPTION("GPIO PS2 driver");
504 MODULE_LICENSE("GPL v2");