ps2-gpio.c 10 KB

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