ps2-gpio.c 11 KB

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