ps2-gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * GPIO based serio bus driver for bit banging the PS/2 protocol
  3. *
  4. * Author: Danilo Krummrich <danilokrummrich@dk-develop.de>
  5. *
  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.
  9. */
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/module.h>
  13. #include <linux/serio.h>
  14. #include <linux/slab.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/completion.h>
  18. #include <linux/mutex.h>
  19. #include <linux/preempt.h>
  20. #include <linux/property.h>
  21. #include <linux/of.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/delay.h>
  24. #define DRIVER_NAME "ps2-gpio"
  25. #define PS2_MODE_RX 0
  26. #define PS2_MODE_TX 1
  27. #define PS2_START_BIT 0
  28. #define PS2_DATA_BIT0 1
  29. #define PS2_DATA_BIT1 2
  30. #define PS2_DATA_BIT2 3
  31. #define PS2_DATA_BIT3 4
  32. #define PS2_DATA_BIT4 5
  33. #define PS2_DATA_BIT5 6
  34. #define PS2_DATA_BIT6 7
  35. #define PS2_DATA_BIT7 8
  36. #define PS2_PARITY_BIT 9
  37. #define PS2_STOP_BIT 10
  38. #define PS2_TX_TIMEOUT 11
  39. #define PS2_ACK_BIT 12
  40. #define PS2_DEV_RET_ACK 0xfa
  41. #define PS2_DEV_RET_NACK 0xfe
  42. #define PS2_CMD_RESEND 0xfe
  43. struct ps2_gpio_data {
  44. struct device *dev;
  45. struct serio *serio;
  46. unsigned char mode;
  47. struct gpio_desc *gpio_clk;
  48. struct gpio_desc *gpio_data;
  49. bool write_enable;
  50. int irq;
  51. unsigned char rx_cnt;
  52. unsigned char rx_byte;
  53. unsigned char tx_cnt;
  54. unsigned char tx_byte;
  55. struct completion tx_done;
  56. struct mutex tx_mutex;
  57. struct delayed_work tx_work;
  58. };
  59. static int ps2_gpio_open(struct serio *serio)
  60. {
  61. struct ps2_gpio_data *drvdata = serio->port_data;
  62. enable_irq(drvdata->irq);
  63. return 0;
  64. }
  65. static void ps2_gpio_close(struct serio *serio)
  66. {
  67. struct ps2_gpio_data *drvdata = serio->port_data;
  68. disable_irq(drvdata->irq);
  69. }
  70. static int __ps2_gpio_write(struct serio *serio, unsigned char val)
  71. {
  72. struct ps2_gpio_data *drvdata = serio->port_data;
  73. disable_irq_nosync(drvdata->irq);
  74. gpiod_direction_output(drvdata->gpio_clk, 0);
  75. drvdata->mode = PS2_MODE_TX;
  76. drvdata->tx_byte = val;
  77. schedule_delayed_work(&drvdata->tx_work, usecs_to_jiffies(200));
  78. return 0;
  79. }
  80. static int ps2_gpio_write(struct serio *serio, unsigned char val)
  81. {
  82. struct ps2_gpio_data *drvdata = serio->port_data;
  83. int ret = 0;
  84. if (in_task()) {
  85. mutex_lock(&drvdata->tx_mutex);
  86. __ps2_gpio_write(serio, val);
  87. if (!wait_for_completion_timeout(&drvdata->tx_done,
  88. msecs_to_jiffies(10000)))
  89. ret = SERIO_TIMEOUT;
  90. mutex_unlock(&drvdata->tx_mutex);
  91. } else {
  92. __ps2_gpio_write(serio, val);
  93. }
  94. return ret;
  95. }
  96. static void ps2_gpio_tx_work_fn(struct work_struct *work)
  97. {
  98. struct delayed_work *dwork = to_delayed_work(work);
  99. struct ps2_gpio_data *drvdata = container_of(dwork,
  100. struct ps2_gpio_data,
  101. tx_work);
  102. enable_irq(drvdata->irq);
  103. gpiod_direction_output(drvdata->gpio_data, 0);
  104. gpiod_direction_input(drvdata->gpio_clk);
  105. }
  106. static irqreturn_t ps2_gpio_irq_rx(struct ps2_gpio_data *drvdata)
  107. {
  108. unsigned char byte, cnt;
  109. int data;
  110. int rxflags = 0;
  111. static unsigned long old_jiffies;
  112. byte = drvdata->rx_byte;
  113. cnt = drvdata->rx_cnt;
  114. if (old_jiffies == 0)
  115. old_jiffies = jiffies;
  116. if ((jiffies - old_jiffies) > usecs_to_jiffies(100)) {
  117. dev_err(drvdata->dev,
  118. "RX: timeout, probably we missed an interrupt\n");
  119. goto err;
  120. }
  121. old_jiffies = jiffies;
  122. data = gpiod_get_value(drvdata->gpio_data);
  123. if (unlikely(data < 0)) {
  124. dev_err(drvdata->dev, "RX: failed to get data gpio val: %d\n",
  125. data);
  126. goto err;
  127. }
  128. switch (cnt) {
  129. case PS2_START_BIT:
  130. /* start bit should be low */
  131. if (unlikely(data)) {
  132. dev_err(drvdata->dev, "RX: start bit should be low\n");
  133. goto err;
  134. }
  135. break;
  136. case PS2_DATA_BIT0:
  137. case PS2_DATA_BIT1:
  138. case PS2_DATA_BIT2:
  139. case PS2_DATA_BIT3:
  140. case PS2_DATA_BIT4:
  141. case PS2_DATA_BIT5:
  142. case PS2_DATA_BIT6:
  143. case PS2_DATA_BIT7:
  144. /* processing data bits */
  145. if (data)
  146. byte |= (data << (cnt - 1));
  147. break;
  148. case PS2_PARITY_BIT:
  149. /* check odd parity */
  150. if (!((hweight8(byte) & 1) ^ data)) {
  151. rxflags |= SERIO_PARITY;
  152. dev_warn(drvdata->dev, "RX: parity error\n");
  153. if (!drvdata->write_enable)
  154. goto err;
  155. }
  156. /* Do not send spurious ACK's and NACK's when write fn is
  157. * not provided.
  158. */
  159. if (!drvdata->write_enable) {
  160. if (byte == PS2_DEV_RET_NACK)
  161. goto err;
  162. else if (byte == PS2_DEV_RET_ACK)
  163. break;
  164. }
  165. /* Let's send the data without waiting for the stop bit to be
  166. * sent. It may happen that we miss the stop bit. When this
  167. * happens we have no way to recover from this, certainly
  168. * missing the parity bit would be recognized when processing
  169. * the stop bit. When missing both, data is lost.
  170. */
  171. serio_interrupt(drvdata->serio, byte, rxflags);
  172. dev_dbg(drvdata->dev, "RX: sending byte 0x%x\n", byte);
  173. break;
  174. case PS2_STOP_BIT:
  175. /* stop bit should be high */
  176. if (unlikely(!data)) {
  177. dev_err(drvdata->dev, "RX: stop bit should be high\n");
  178. goto err;
  179. }
  180. cnt = byte = 0;
  181. old_jiffies = 0;
  182. goto end; /* success */
  183. default:
  184. dev_err(drvdata->dev, "RX: got out of sync with the device\n");
  185. goto err;
  186. }
  187. cnt++;
  188. goto end; /* success */
  189. err:
  190. cnt = byte = 0;
  191. old_jiffies = 0;
  192. __ps2_gpio_write(drvdata->serio, PS2_CMD_RESEND);
  193. end:
  194. drvdata->rx_cnt = cnt;
  195. drvdata->rx_byte = byte;
  196. return IRQ_HANDLED;
  197. }
  198. static irqreturn_t ps2_gpio_irq_tx(struct ps2_gpio_data *drvdata)
  199. {
  200. unsigned char byte, cnt;
  201. int data;
  202. static unsigned long old_jiffies;
  203. cnt = drvdata->tx_cnt;
  204. byte = drvdata->tx_byte;
  205. if (old_jiffies == 0)
  206. old_jiffies = jiffies;
  207. if ((jiffies - old_jiffies) > usecs_to_jiffies(100)) {
  208. dev_err(drvdata->dev,
  209. "TX: timeout, probably we missed an interrupt\n");
  210. goto err;
  211. }
  212. old_jiffies = jiffies;
  213. switch (cnt) {
  214. case PS2_START_BIT:
  215. /* should never happen */
  216. dev_err(drvdata->dev,
  217. "TX: start bit should have been sent already\n");
  218. goto err;
  219. case PS2_DATA_BIT0:
  220. case PS2_DATA_BIT1:
  221. case PS2_DATA_BIT2:
  222. case PS2_DATA_BIT3:
  223. case PS2_DATA_BIT4:
  224. case PS2_DATA_BIT5:
  225. case PS2_DATA_BIT6:
  226. case PS2_DATA_BIT7:
  227. data = byte & BIT(cnt - 1);
  228. gpiod_set_value(drvdata->gpio_data, data);
  229. break;
  230. case PS2_PARITY_BIT:
  231. /* do odd parity */
  232. data = !(hweight8(byte) & 1);
  233. gpiod_set_value(drvdata->gpio_data, data);
  234. break;
  235. case PS2_STOP_BIT:
  236. /* release data line to generate stop bit */
  237. gpiod_direction_input(drvdata->gpio_data);
  238. break;
  239. case PS2_TX_TIMEOUT:
  240. /* Devices generate one extra clock pulse before sending the
  241. * acknowledgment.
  242. */
  243. break;
  244. case PS2_ACK_BIT:
  245. gpiod_direction_input(drvdata->gpio_data);
  246. data = gpiod_get_value(drvdata->gpio_data);
  247. if (data) {
  248. dev_warn(drvdata->dev, "TX: received NACK, retry\n");
  249. goto err;
  250. }
  251. drvdata->mode = PS2_MODE_RX;
  252. complete(&drvdata->tx_done);
  253. cnt = 1;
  254. old_jiffies = 0;
  255. goto end; /* success */
  256. default:
  257. /* Probably we missed the stop bit. Therefore we release data
  258. * line and try again.
  259. */
  260. gpiod_direction_input(drvdata->gpio_data);
  261. dev_err(drvdata->dev, "TX: got out of sync with the device\n");
  262. goto err;
  263. }
  264. cnt++;
  265. goto end; /* success */
  266. err:
  267. cnt = 1;
  268. old_jiffies = 0;
  269. gpiod_direction_input(drvdata->gpio_data);
  270. __ps2_gpio_write(drvdata->serio, drvdata->tx_byte);
  271. end:
  272. drvdata->tx_cnt = cnt;
  273. return IRQ_HANDLED;
  274. }
  275. static irqreturn_t ps2_gpio_irq(int irq, void *dev_id)
  276. {
  277. struct ps2_gpio_data *drvdata = dev_id;
  278. return drvdata->mode ? ps2_gpio_irq_tx(drvdata) :
  279. ps2_gpio_irq_rx(drvdata);
  280. }
  281. static int ps2_gpio_get_props(struct device *dev,
  282. struct ps2_gpio_data *drvdata)
  283. {
  284. drvdata->gpio_data = devm_gpiod_get(dev, "data", GPIOD_IN);
  285. if (IS_ERR(drvdata->gpio_data)) {
  286. dev_err(dev, "failed to request data gpio: %ld",
  287. PTR_ERR(drvdata->gpio_data));
  288. return PTR_ERR(drvdata->gpio_data);
  289. }
  290. drvdata->gpio_clk = devm_gpiod_get(dev, "clk", GPIOD_IN);
  291. if (IS_ERR(drvdata->gpio_clk)) {
  292. dev_err(dev, "failed to request clock gpio: %ld",
  293. PTR_ERR(drvdata->gpio_clk));
  294. return PTR_ERR(drvdata->gpio_clk);
  295. }
  296. drvdata->write_enable = device_property_read_bool(dev,
  297. "write-enable");
  298. return 0;
  299. }
  300. static int ps2_gpio_probe(struct platform_device *pdev)
  301. {
  302. struct ps2_gpio_data *drvdata;
  303. struct serio *serio;
  304. struct device *dev = &pdev->dev;
  305. int error;
  306. drvdata = devm_kzalloc(dev, sizeof(struct ps2_gpio_data), GFP_KERNEL);
  307. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  308. if (!drvdata || !serio) {
  309. error = -ENOMEM;
  310. goto err_free_serio;
  311. }
  312. error = ps2_gpio_get_props(dev, drvdata);
  313. if (error)
  314. goto err_free_serio;
  315. if (gpiod_cansleep(drvdata->gpio_data) ||
  316. gpiod_cansleep(drvdata->gpio_clk)) {
  317. dev_err(dev, "GPIO data or clk are connected via slow bus\n");
  318. error = -EINVAL;
  319. goto err_free_serio;
  320. }
  321. drvdata->irq = platform_get_irq(pdev, 0);
  322. if (drvdata->irq < 0) {
  323. dev_err(dev, "failed to get irq from platform resource: %d\n",
  324. drvdata->irq);
  325. error = drvdata->irq;
  326. goto err_free_serio;
  327. }
  328. error = devm_request_irq(dev, drvdata->irq, ps2_gpio_irq,
  329. IRQF_NO_THREAD, DRIVER_NAME, drvdata);
  330. if (error) {
  331. dev_err(dev, "failed to request irq %d: %d\n",
  332. drvdata->irq, error);
  333. goto err_free_serio;
  334. }
  335. /* Keep irq disabled until serio->open is called. */
  336. disable_irq(drvdata->irq);
  337. serio->id.type = SERIO_8042;
  338. serio->open = ps2_gpio_open;
  339. serio->close = ps2_gpio_close;
  340. /* Write can be enabled in platform/dt data, but possibly it will not
  341. * work because of the tough timings.
  342. */
  343. serio->write = drvdata->write_enable ? ps2_gpio_write : NULL;
  344. serio->port_data = drvdata;
  345. serio->dev.parent = dev;
  346. strlcpy(serio->name, dev_name(dev), sizeof(serio->name));
  347. strlcpy(serio->phys, dev_name(dev), sizeof(serio->phys));
  348. drvdata->serio = serio;
  349. drvdata->dev = dev;
  350. drvdata->mode = PS2_MODE_RX;
  351. /* Tx count always starts at 1, as the start bit is sent implicitly by
  352. * host-to-device communication initialization.
  353. */
  354. drvdata->tx_cnt = 1;
  355. INIT_DELAYED_WORK(&drvdata->tx_work, ps2_gpio_tx_work_fn);
  356. init_completion(&drvdata->tx_done);
  357. mutex_init(&drvdata->tx_mutex);
  358. serio_register_port(serio);
  359. platform_set_drvdata(pdev, drvdata);
  360. return 0; /* success */
  361. err_free_serio:
  362. kfree(serio);
  363. return error;
  364. }
  365. static int ps2_gpio_remove(struct platform_device *pdev)
  366. {
  367. struct ps2_gpio_data *drvdata = platform_get_drvdata(pdev);
  368. serio_unregister_port(drvdata->serio);
  369. return 0;
  370. }
  371. #if defined(CONFIG_OF)
  372. static const struct of_device_id ps2_gpio_match[] = {
  373. { .compatible = "ps2-gpio", },
  374. { },
  375. };
  376. MODULE_DEVICE_TABLE(of, ps2_gpio_match);
  377. #endif
  378. static struct platform_driver ps2_gpio_driver = {
  379. .probe = ps2_gpio_probe,
  380. .remove = ps2_gpio_remove,
  381. .driver = {
  382. .name = DRIVER_NAME,
  383. .of_match_table = of_match_ptr(ps2_gpio_match),
  384. },
  385. };
  386. module_platform_driver(ps2_gpio_driver);
  387. MODULE_AUTHOR("Danilo Krummrich <danilokrummrich@dk-develop.de>");
  388. MODULE_DESCRIPTION("GPIO PS2 driver");
  389. MODULE_LICENSE("GPL v2");