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