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>
23 #define DRIVER_NAME "ps2-gpio"
28 #define PS2_START_BIT 0
29 #define PS2_DATA_BIT0 1
30 #define PS2_DATA_BIT1 2
31 #define PS2_DATA_BIT2 3
32 #define PS2_DATA_BIT3 4
33 #define PS2_DATA_BIT4 5
34 #define PS2_DATA_BIT5 6
35 #define PS2_DATA_BIT6 7
36 #define PS2_DATA_BIT7 8
37 #define PS2_PARITY_BIT 9
38 #define PS2_STOP_BIT 10
39 #define PS2_TX_TIMEOUT 11
40 #define PS2_ACK_BIT 12
42 #define PS2_DEV_RET_ACK 0xfa
43 #define PS2_DEV_RET_NACK 0xfe
45 #define PS2_CMD_RESEND 0xfe
47 struct ps2_gpio_data
{
51 struct gpio_desc
*gpio_clk
;
52 struct gpio_desc
*gpio_data
;
56 unsigned char rx_byte
;
58 unsigned char tx_byte
;
59 struct completion tx_done
;
60 struct mutex tx_mutex
;
61 struct delayed_work tx_work
;
64 static int ps2_gpio_open(struct serio
*serio
)
66 struct ps2_gpio_data
*drvdata
= serio
->port_data
;
68 enable_irq(drvdata
->irq
);
72 static void ps2_gpio_close(struct serio
*serio
)
74 struct ps2_gpio_data
*drvdata
= serio
->port_data
;
76 flush_delayed_work(&drvdata
->tx_work
);
77 disable_irq(drvdata
->irq
);
80 static int __ps2_gpio_write(struct serio
*serio
, unsigned char val
)
82 struct ps2_gpio_data
*drvdata
= serio
->port_data
;
84 disable_irq_nosync(drvdata
->irq
);
85 gpiod_direction_output(drvdata
->gpio_clk
, 0);
87 drvdata
->mode
= PS2_MODE_TX
;
88 drvdata
->tx_byte
= val
;
90 schedule_delayed_work(&drvdata
->tx_work
, usecs_to_jiffies(200));
95 static int ps2_gpio_write(struct serio
*serio
, unsigned char val
)
97 struct ps2_gpio_data
*drvdata
= serio
->port_data
;
101 mutex_lock(&drvdata
->tx_mutex
);
102 __ps2_gpio_write(serio
, val
);
103 if (!wait_for_completion_timeout(&drvdata
->tx_done
,
104 msecs_to_jiffies(10000)))
106 mutex_unlock(&drvdata
->tx_mutex
);
108 __ps2_gpio_write(serio
, val
);
114 static void ps2_gpio_tx_work_fn(struct work_struct
*work
)
116 struct delayed_work
*dwork
= to_delayed_work(work
);
117 struct ps2_gpio_data
*drvdata
= container_of(dwork
,
118 struct ps2_gpio_data
,
121 enable_irq(drvdata
->irq
);
122 gpiod_direction_output(drvdata
->gpio_data
, 0);
123 gpiod_direction_input(drvdata
->gpio_clk
);
126 static irqreturn_t
ps2_gpio_irq_rx(struct ps2_gpio_data
*drvdata
)
128 unsigned char byte
, cnt
;
131 static unsigned long old_jiffies
;
133 byte
= drvdata
->rx_byte
;
134 cnt
= drvdata
->rx_cnt
;
136 if (old_jiffies
== 0)
137 old_jiffies
= jiffies
;
139 if ((jiffies
- old_jiffies
) > usecs_to_jiffies(100)) {
140 dev_err(drvdata
->dev
,
141 "RX: timeout, probably we missed an interrupt\n");
144 old_jiffies
= jiffies
;
146 data
= gpiod_get_value(drvdata
->gpio_data
);
147 if (unlikely(data
< 0)) {
148 dev_err(drvdata
->dev
, "RX: failed to get data gpio val: %d\n",
155 /* start bit should be low */
156 if (unlikely(data
)) {
157 dev_err(drvdata
->dev
, "RX: start bit should be low\n");
169 /* processing data bits */
171 byte
|= (data
<< (cnt
- 1));
174 /* check odd parity */
175 if (!((hweight8(byte
) & 1) ^ data
)) {
176 rxflags
|= SERIO_PARITY
;
177 dev_warn(drvdata
->dev
, "RX: parity error\n");
178 if (!drvdata
->write_enable
)
182 /* Do not send spurious ACK's and NACK's when write fn is
185 if (!drvdata
->write_enable
) {
186 if (byte
== PS2_DEV_RET_NACK
)
188 else if (byte
== PS2_DEV_RET_ACK
)
192 /* Let's send the data without waiting for the stop bit to be
193 * sent. It may happen that we miss the stop bit. When this
194 * happens we have no way to recover from this, certainly
195 * missing the parity bit would be recognized when processing
196 * the stop bit. When missing both, data is lost.
198 serio_interrupt(drvdata
->serio
, byte
, rxflags
);
199 dev_dbg(drvdata
->dev
, "RX: sending byte 0x%x\n", byte
);
202 /* stop bit should be high */
203 if (unlikely(!data
)) {
204 dev_err(drvdata
->dev
, "RX: stop bit should be high\n");
209 goto end
; /* success */
211 dev_err(drvdata
->dev
, "RX: got out of sync with the device\n");
216 goto end
; /* success */
221 __ps2_gpio_write(drvdata
->serio
, PS2_CMD_RESEND
);
223 drvdata
->rx_cnt
= cnt
;
224 drvdata
->rx_byte
= byte
;
228 static irqreturn_t
ps2_gpio_irq_tx(struct ps2_gpio_data
*drvdata
)
230 unsigned char byte
, cnt
;
232 static unsigned long old_jiffies
;
234 cnt
= drvdata
->tx_cnt
;
235 byte
= drvdata
->tx_byte
;
237 if (old_jiffies
== 0)
238 old_jiffies
= jiffies
;
240 if ((jiffies
- old_jiffies
) > usecs_to_jiffies(100)) {
241 dev_err(drvdata
->dev
,
242 "TX: timeout, probably we missed an interrupt\n");
245 old_jiffies
= jiffies
;
249 /* should never happen */
250 dev_err(drvdata
->dev
,
251 "TX: start bit should have been sent already\n");
261 data
= byte
& BIT(cnt
- 1);
262 gpiod_set_value(drvdata
->gpio_data
, data
);
266 data
= !(hweight8(byte
) & 1);
267 gpiod_set_value(drvdata
->gpio_data
, data
);
270 /* release data line to generate stop bit */
271 gpiod_direction_input(drvdata
->gpio_data
);
274 /* Devices generate one extra clock pulse before sending the
279 gpiod_direction_input(drvdata
->gpio_data
);
280 data
= gpiod_get_value(drvdata
->gpio_data
);
282 dev_warn(drvdata
->dev
, "TX: received NACK, retry\n");
286 drvdata
->mode
= PS2_MODE_RX
;
287 complete(&drvdata
->tx_done
);
291 goto end
; /* success */
293 /* Probably we missed the stop bit. Therefore we release data
294 * line and try again.
296 gpiod_direction_input(drvdata
->gpio_data
);
297 dev_err(drvdata
->dev
, "TX: got out of sync with the device\n");
302 goto end
; /* success */
307 gpiod_direction_input(drvdata
->gpio_data
);
308 __ps2_gpio_write(drvdata
->serio
, drvdata
->tx_byte
);
310 drvdata
->tx_cnt
= cnt
;
314 static irqreturn_t
ps2_gpio_irq(int irq
, void *dev_id
)
316 struct ps2_gpio_data
*drvdata
= dev_id
;
318 return drvdata
->mode
? ps2_gpio_irq_tx(drvdata
) :
319 ps2_gpio_irq_rx(drvdata
);
322 static int ps2_gpio_get_props(struct device
*dev
,
323 struct ps2_gpio_data
*drvdata
)
325 drvdata
->gpio_data
= devm_gpiod_get(dev
, "data", GPIOD_IN
);
326 if (IS_ERR(drvdata
->gpio_data
)) {
327 dev_err(dev
, "failed to request data gpio: %ld",
328 PTR_ERR(drvdata
->gpio_data
));
329 return PTR_ERR(drvdata
->gpio_data
);
332 drvdata
->gpio_clk
= devm_gpiod_get(dev
, "clk", GPIOD_IN
);
333 if (IS_ERR(drvdata
->gpio_clk
)) {
334 dev_err(dev
, "failed to request clock gpio: %ld",
335 PTR_ERR(drvdata
->gpio_clk
));
336 return PTR_ERR(drvdata
->gpio_clk
);
339 drvdata
->write_enable
= device_property_read_bool(dev
,
345 static int ps2_gpio_probe(struct platform_device
*pdev
)
347 struct ps2_gpio_data
*drvdata
;
349 struct device
*dev
= &pdev
->dev
;
352 drvdata
= devm_kzalloc(dev
, sizeof(struct ps2_gpio_data
), GFP_KERNEL
);
353 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
354 if (!drvdata
|| !serio
) {
359 error
= ps2_gpio_get_props(dev
, drvdata
);
363 if (gpiod_cansleep(drvdata
->gpio_data
) ||
364 gpiod_cansleep(drvdata
->gpio_clk
)) {
365 dev_err(dev
, "GPIO data or clk are connected via slow bus\n");
370 drvdata
->irq
= platform_get_irq(pdev
, 0);
371 if (drvdata
->irq
< 0) {
372 error
= drvdata
->irq
;
376 error
= devm_request_irq(dev
, drvdata
->irq
, ps2_gpio_irq
,
377 IRQF_NO_THREAD
, DRIVER_NAME
, drvdata
);
379 dev_err(dev
, "failed to request irq %d: %d\n",
380 drvdata
->irq
, error
);
384 /* Keep irq disabled until serio->open is called. */
385 disable_irq(drvdata
->irq
);
387 serio
->id
.type
= SERIO_8042
;
388 serio
->open
= ps2_gpio_open
;
389 serio
->close
= ps2_gpio_close
;
390 /* Write can be enabled in platform/dt data, but possibly it will not
391 * work because of the tough timings.
393 serio
->write
= drvdata
->write_enable
? ps2_gpio_write
: NULL
;
394 serio
->port_data
= drvdata
;
395 serio
->dev
.parent
= dev
;
396 strlcpy(serio
->name
, dev_name(dev
), sizeof(serio
->name
));
397 strlcpy(serio
->phys
, dev_name(dev
), sizeof(serio
->phys
));
399 drvdata
->serio
= serio
;
401 drvdata
->mode
= PS2_MODE_RX
;
403 /* Tx count always starts at 1, as the start bit is sent implicitly by
404 * host-to-device communication initialization.
408 INIT_DELAYED_WORK(&drvdata
->tx_work
, ps2_gpio_tx_work_fn
);
409 init_completion(&drvdata
->tx_done
);
410 mutex_init(&drvdata
->tx_mutex
);
412 serio_register_port(serio
);
413 platform_set_drvdata(pdev
, drvdata
);
415 return 0; /* success */
422 static int ps2_gpio_remove(struct platform_device
*pdev
)
424 struct ps2_gpio_data
*drvdata
= platform_get_drvdata(pdev
);
426 serio_unregister_port(drvdata
->serio
);
430 #if defined(CONFIG_OF)
431 static const struct of_device_id ps2_gpio_match
[] = {
432 { .compatible
= "ps2-gpio", },
435 MODULE_DEVICE_TABLE(of
, ps2_gpio_match
);
438 static struct platform_driver ps2_gpio_driver
= {
439 .probe
= ps2_gpio_probe
,
440 .remove
= ps2_gpio_remove
,
443 .of_match_table
= of_match_ptr(ps2_gpio_match
),
446 module_platform_driver(ps2_gpio_driver
);
448 MODULE_AUTHOR("Danilo Krummrich <danilokrummrich@dk-develop.de>");
449 MODULE_DESCRIPTION("GPIO PS2 driver");
450 MODULE_LICENSE("GPL v2");