pt_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * pt_spi.c
  3. * Parade TrueTouch(TM) Standard Product SPI Module.
  4. * For use with Parade touchscreen controllers.
  5. * Supported parts include:
  6. * TMA5XX
  7. * TMA448
  8. * TMA445A
  9. * TT21XXX
  10. * TT31XXX
  11. * TT4XXXX
  12. * TT7XXX
  13. * TC3XXX
  14. *
  15. * Copyright (C) 2015-2020 Parade Technologies
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2, and only version 2, as published by the
  20. * Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * Contact Parade Technologies at www.paradetech.com <[email protected]>
  28. */
  29. #include "pt_regs.h"
  30. #include <linux/spi/spi.h>
  31. #include <linux/version.h>
  32. /* TC3315 - DUT Address (0x24 & 0x07) << 1 = 0x08 for write and 0x09 for read */
  33. #define PT_SPI_WR_OP 0x08 /* r/~w */
  34. #define PT_SPI_RD_OP 0x09
  35. #define PT_SPI_BITS_PER_WORD 8
  36. #define PT_SPI_SYNC_ACK 0x62
  37. #define PT_SPI_CMD_BYTES 0
  38. #define PT_SPI_DATA_SIZE (2 * 256)
  39. #define PT_SPI_DATA_BUF_SIZE (PT_SPI_CMD_BYTES + PT_SPI_DATA_SIZE)
  40. #define PT_SPI_OP_SIZE (1)
  41. #define PT_SPI_DUMMY_READ (1)
  42. #define PT_SPI_BUFFER_SIZE \
  43. (PT_MAX_PIP2_MSG_SIZE + PT_SPI_OP_SIZE + PT_SPI_DUMMY_READ)
  44. static u8 *tmp_rbuf;
  45. static u8 *tmp_wbuf;
  46. DEFINE_MUTEX(pt_spi_bus_lock);
  47. /*******************************************************************************
  48. * FUNCTION: pt_spi_xfer
  49. *
  50. * SUMMARY: Read or write date for SPI device.
  51. *
  52. * RETURN:
  53. * 0 = success
  54. * !0 = failure
  55. *
  56. * PARAMETERS:
  57. * *dev - pointer to device structure
  58. * op - flag to write or read data
  59. * *buf - pointer to data buffer
  60. * length - data length
  61. ******************************************************************************/
  62. static int pt_spi_xfer(struct device *dev, u8 op, u8 *buf, int length)
  63. {
  64. struct spi_device *spi = to_spi_device(dev);
  65. struct spi_message msg;
  66. struct spi_transfer xfer;
  67. int rc;
  68. memset(&xfer, 0, sizeof(xfer));
  69. spi_message_init(&msg);
  70. switch (op) {
  71. case PT_SPI_RD_OP:
  72. /* Clear tmp_wbuf with additional OP Code and dummy byte */
  73. memset(tmp_wbuf, 0, length + 2);
  74. tmp_wbuf[0] = op;
  75. /* Total read/write = Read length + Op code + dummy byte */
  76. xfer.tx_buf = tmp_wbuf;
  77. xfer.rx_buf = tmp_rbuf;
  78. xfer.len = length + 2;
  79. break;
  80. case PT_SPI_WR_OP:
  81. memcpy(&tmp_wbuf[1], buf, length);
  82. tmp_wbuf[0] = op;
  83. /* Write length + size of Op code */
  84. xfer.tx_buf = tmp_wbuf;
  85. xfer.len = length + 1;
  86. break;
  87. default:
  88. rc = -EIO;
  89. goto exit;
  90. }
  91. spi_message_add_tail(&xfer, &msg);
  92. rc = spi_sync(spi, &msg);
  93. /* On reads copy only the data content back into the passed in buf */
  94. if (op == PT_SPI_RD_OP)
  95. memcpy(buf, &tmp_rbuf[2], length);
  96. exit:
  97. if (rc < 0)
  98. pt_debug(dev, DL_ERROR, "%s: spi_sync() error %d\n",
  99. __func__, rc);
  100. #if 0 /* TODO TC3315 - need to verify the ACK byte */
  101. if (tmp_rbuf[0] != PT_SPI_SYNC_ACK) {
  102. pt_debug(dev, DL_ERROR, "%s: r_header = 0x%02X\n", __func__,
  103. r_header[0]);
  104. return -EIO;
  105. }
  106. #endif
  107. return rc;
  108. }
  109. /*******************************************************************************
  110. * FUNCTION: pt_spi_read_default
  111. *
  112. * SUMMARY: Read a certain number of bytes from the SPI bus
  113. * NOTE: For TC3315 every response includes a "dummy" prefix byte that
  114. * needs to be stipped off before returning buf.
  115. *
  116. * RETURN:
  117. * 0 = success
  118. * !0 = failure
  119. *
  120. * PARAMETERS:
  121. * *dev - pointer to Device structure
  122. * *buf - pointer to buffer where the data read will be stored
  123. * size - size to be read
  124. ******************************************************************************/
  125. static int pt_spi_read_default(struct device *dev, void *buf, int size)
  126. {
  127. int rc = 0;
  128. if (!buf || !size || size > PT_MAX_PIP2_MSG_SIZE)
  129. return -EINVAL;
  130. mutex_lock(&pt_spi_bus_lock);
  131. rc = pt_spi_xfer(dev, PT_SPI_RD_OP, buf, size);
  132. mutex_unlock(&pt_spi_bus_lock);
  133. return rc;
  134. }
  135. /*******************************************************************************
  136. * FUNCTION: pt_spi_read_default_nosize
  137. *
  138. * SUMMARY: Read from the SPI bus in two transactions first reading the HID
  139. * packet size (2 bytes) followed by reading the rest of the packet based
  140. * on the size initially read.
  141. * NOTE: The empty buffer 'size' was redefined in PIP version 1.7.
  142. * NOTE: For TC3315 every response includes a "dummy" prefix byte that
  143. * needs to be stipped off before returning buf.
  144. *
  145. * RETURN:
  146. * 0 = success
  147. * !0 = failure
  148. *
  149. * PARAMETERS:
  150. * *dev - pointer to Device structure
  151. * *buf - pointer to buffer where the data read will be stored
  152. * max - max size that can be read
  153. ******************************************************************************/
  154. static int pt_spi_read_default_nosize(struct device *dev, u8 *buf, u32 max)
  155. {
  156. u32 size;
  157. int rc = 0;
  158. if (!buf)
  159. return 0;
  160. mutex_lock(&pt_spi_bus_lock);
  161. /* Separate transaction to retrieve only the length to read */
  162. rc = pt_spi_xfer(dev, PT_SPI_RD_OP, buf, 2);
  163. if (rc < 0) {
  164. pt_debug(dev, DL_ERROR, "%s: SPI transfer error rc = %d\n",
  165. __func__, rc);
  166. goto exit;
  167. }
  168. size = get_unaligned_le16(&buf[0]);
  169. if (!size || size == 2 || size >= PT_PIP_1P7_EMPTY_BUF)
  170. goto exit;
  171. if (size > max || size > PT_MAX_PIP2_MSG_SIZE) {
  172. pt_debug(dev, DL_ERROR, "%s: Invalid size %d !\n", __func__,
  173. size);
  174. rc = -EINVAL;
  175. goto exit;
  176. }
  177. rc = pt_spi_xfer(dev, PT_SPI_RD_OP, buf, size);
  178. if (rc)
  179. pt_debug(dev, DL_ERROR, "%s: SPI transfer error rc = %d\n",
  180. __func__, rc);
  181. exit:
  182. mutex_unlock(&pt_spi_bus_lock);
  183. return rc;
  184. }
  185. /*******************************************************************************
  186. * FUNCTION: pt_spi_write_read_specific
  187. *
  188. * SUMMARY: Write the contents of write_buf to the SPI device and then read
  189. * the response using pt_spi_read_default_nosize()
  190. *
  191. * RETURN:
  192. * 0 = success
  193. * !0 = failure
  194. *
  195. * PARAMETERS:
  196. * *dev - pointer to Device structure
  197. * write_len - length of data buffer write_buf
  198. * *write_buf - pointer to buffer to write
  199. * *read_buf - pointer to buffer to read response into
  200. ******************************************************************************/
  201. static int pt_spi_write_read_specific(struct device *dev, u16 write_len,
  202. u8 *write_buf, u8 *read_buf)
  203. {
  204. int rc = 0;
  205. /* Ensure no packet larger than what the PIP spec allows */
  206. if (write_len > PT_MAX_PIP2_MSG_SIZE)
  207. return -EINVAL;
  208. if (!write_buf || !write_len) {
  209. if (!write_buf)
  210. pt_debug(dev, DL_ERROR,
  211. "%s write_buf is NULL", __func__);
  212. if (!write_len)
  213. pt_debug(dev, DL_ERROR,
  214. "%s write_len is NULL", __func__);
  215. return -EINVAL;
  216. }
  217. mutex_lock(&pt_spi_bus_lock);
  218. rc = pt_spi_xfer(dev, PT_SPI_WR_OP, write_buf, write_len);
  219. if (rc < 0)
  220. goto error;
  221. mutex_unlock(&pt_spi_bus_lock);
  222. if (read_buf)
  223. rc = pt_spi_read_default_nosize(dev, read_buf,
  224. PT_SPI_DATA_SIZE);
  225. return rc;
  226. error:
  227. mutex_unlock(&pt_spi_bus_lock);
  228. return rc;
  229. }
  230. static struct pt_bus_ops pt_spi_bus_ops = {
  231. .bustype = BUS_SPI,
  232. .read_default = pt_spi_read_default,
  233. .read_default_nosize = pt_spi_read_default_nosize,
  234. .write_read_specific = pt_spi_write_read_specific,
  235. };
  236. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  237. static const struct of_device_id pt_spi_of_match[] = {
  238. { .compatible = "parade,pt_spi_adapter", },
  239. { }
  240. };
  241. MODULE_DEVICE_TABLE(of, pt_spi_of_match);
  242. #endif
  243. /*******************************************************************************
  244. * FUNCTION: pt_spi_probe
  245. *
  246. * SUMMARY: Probe functon for the SPI module
  247. *
  248. * RETURN:
  249. * 0 = success
  250. * !0 = failure
  251. *
  252. * PARAMETERS:
  253. * *spi - pointer to spi device structure
  254. ******************************************************************************/
  255. static int pt_spi_probe(struct spi_device *spi)
  256. {
  257. struct device *dev = &spi->dev;
  258. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  259. const struct of_device_id *match;
  260. #endif
  261. int rc;
  262. /* Set up SPI*/
  263. spi->bits_per_word = PT_SPI_BITS_PER_WORD;
  264. spi->mode = SPI_MODE_0;
  265. rc = spi_setup(spi);
  266. if (rc < 0) {
  267. pt_debug(dev, DL_ERROR, "%s: SPI setup error %d\n",
  268. __func__, rc);
  269. return rc;
  270. }
  271. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  272. match = of_match_device(of_match_ptr(pt_spi_of_match), dev);
  273. if (match) {
  274. rc = pt_devtree_create_and_get_pdata(dev);
  275. if (rc < 0)
  276. return rc;
  277. }
  278. #endif
  279. /* Add 2 to the length for the 'OP Code' & 'Dummy' prefix bytes */
  280. tmp_wbuf = kzalloc(PT_SPI_BUFFER_SIZE, GFP_KERNEL);
  281. tmp_rbuf = kzalloc(PT_SPI_BUFFER_SIZE, GFP_KERNEL);
  282. if (!tmp_wbuf || !tmp_rbuf)
  283. return -ENOMEM;
  284. rc = pt_probe(&pt_spi_bus_ops, &spi->dev, spi->irq,
  285. PT_SPI_DATA_BUF_SIZE);
  286. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  287. if (rc && match)
  288. pt_devtree_clean_pdata(dev);
  289. #endif
  290. return rc;
  291. }
  292. /*******************************************************************************
  293. * FUNCTION: pt_spi_remove
  294. *
  295. * SUMMARY: Remove functon for the SPI module
  296. *
  297. * RETURN:
  298. * 0 = success
  299. * !0 = failure
  300. *
  301. * PARAMETERS:
  302. * *spi - pointer to spi device structure
  303. ******************************************************************************/
  304. static int pt_spi_remove(struct spi_device *spi)
  305. {
  306. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  307. const struct of_device_id *match;
  308. #endif
  309. struct device *dev = &spi->dev;
  310. struct pt_core_data *cd = dev_get_drvdata(dev);
  311. kfree(tmp_rbuf);
  312. kfree(tmp_wbuf);
  313. pt_release(cd);
  314. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  315. match = of_match_device(of_match_ptr(pt_spi_of_match), dev);
  316. if (match)
  317. pt_devtree_clean_pdata(dev);
  318. #endif
  319. return 0;
  320. }
  321. static const struct spi_device_id pt_spi_id[] = {
  322. { PT_SPI_NAME, 0 },
  323. { }
  324. };
  325. MODULE_DEVICE_TABLE(spi, pt_spi_id);
  326. static struct spi_driver pt_spi_driver = {
  327. .driver = {
  328. .name = PT_SPI_NAME,
  329. .bus = &spi_bus_type,
  330. .owner = THIS_MODULE,
  331. .pm = &pt_pm_ops,
  332. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  333. .of_match_table = pt_spi_of_match,
  334. #endif
  335. },
  336. .probe = pt_spi_probe,
  337. .remove = (pt_spi_remove),
  338. .id_table = pt_spi_id,
  339. };
  340. #if (KERNEL_VERSION(3, 3, 0) <= LINUX_VERSION_CODE)
  341. module_spi_driver(pt_spi_driver);
  342. #else
  343. /*******************************************************************************
  344. * FUNCTION: pt_spi_init
  345. *
  346. * SUMMARY: Initialize function to register spi module to kernel.
  347. *
  348. * RETURN:
  349. * 0 = success
  350. * !0 = failure
  351. ******************************************************************************/
  352. static int __init pt_spi_init(void)
  353. {
  354. int err = spi_register_driver(&pt_spi_driver);
  355. pr_info("%s: Parade TTDL SPI Driver (Build %s) rc=%d\n",
  356. __func__, PT_DRIVER_VERSION, err);
  357. return err;
  358. }
  359. module_init(pt_spi_init);
  360. /*******************************************************************************
  361. * FUNCTION: pt_spi_exit
  362. *
  363. * SUMMARY: Exit function to unregister spi module from kernel.
  364. *
  365. ******************************************************************************/
  366. static void __exit pt_spi_exit(void)
  367. {
  368. spi_unregister_driver(&pt_spi_driver);
  369. }
  370. module_exit(pt_spi_exit);
  371. #endif
  372. MODULE_LICENSE("GPL");
  373. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product SPI Driver");
  374. MODULE_AUTHOR("Parade Technologies <[email protected]>");