pt_i2c.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * pt_i2c.c
  3. * Parade TrueTouch(TM) Standard Product I2C 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/i2c.h>
  31. #include <linux/version.h>
  32. #define PT_I2C_DATA_SIZE (2 * 256)
  33. #define PT_I2C_NAME "pt_i2c_adapter"
  34. /*******************************************************************************
  35. * FUNCTION: pt_i2c_read_default
  36. *
  37. * SUMMARY: Read a certain number of bytes from the I2C bus
  38. *
  39. * PARAMETERS:
  40. * *dev - pointer to Device structure
  41. * *buf - pointer to buffer where the data read will be stored
  42. * size - size to be read
  43. ******************************************************************************/
  44. static int pt_i2c_read_default(struct device *dev, void *buf, int size)
  45. {
  46. struct i2c_client *client = to_i2c_client(dev);
  47. int rc;
  48. int read_size = size;
  49. if (!buf || !size || size > PT_I2C_DATA_SIZE)
  50. return -EINVAL;
  51. rc = i2c_master_recv(client, buf, read_size);
  52. return (rc < 0) ? rc : rc != read_size ? -EIO : 0;
  53. }
  54. /*******************************************************************************
  55. * FUNCTION: pt_i2c_read_default_nosize
  56. *
  57. * SUMMARY: Read from the I2C bus in two transactions first reading the HID
  58. * packet size (2 bytes) followed by reading the rest of the packet based
  59. * on the size initially read.
  60. * NOTE: The empty buffer 'size' was redefined in PIP version 1.7.
  61. *
  62. * PARAMETERS:
  63. * *dev - pointer to Device structure
  64. * *buf - pointer to buffer where the data read will be stored
  65. * max - max size that can be read
  66. ******************************************************************************/
  67. static int pt_i2c_read_default_nosize(struct device *dev, u8 *buf, u32 max)
  68. {
  69. struct i2c_client *client = to_i2c_client(dev);
  70. struct i2c_msg msgs[2];
  71. u8 msg_count = 1;
  72. int rc;
  73. u32 size;
  74. if (!buf)
  75. return -EINVAL;
  76. msgs[0].addr = client->addr;
  77. msgs[0].flags = (client->flags & I2C_M_TEN) | I2C_M_RD;
  78. msgs[0].len = 2;
  79. msgs[0].buf = buf;
  80. rc = i2c_transfer(client->adapter, msgs, msg_count);
  81. if (rc < 0 || rc != msg_count)
  82. return (rc < 0) ? rc : -EIO;
  83. size = get_unaligned_le16(&buf[0]);
  84. if (!size || size == 2 || size >= PT_PIP_1P7_EMPTY_BUF)
  85. /*
  86. * Before PIP 1.7, empty buffer is 0x0002;
  87. * From PIP 1.7, empty buffer is 0xFFXX
  88. */
  89. return 0;
  90. if (size > max)
  91. return -EINVAL;
  92. rc = i2c_master_recv(client, buf, size);
  93. return (rc < 0) ? rc : rc != (int)size ? -EIO : 0;
  94. }
  95. /*******************************************************************************
  96. * FUNCTION: pt_i2c_write_read_specific
  97. *
  98. * SUMMARY: Write the contents of write_buf to the I2C device and then read
  99. * the response using pt_i2c_read_default_nosize()
  100. *
  101. * PARAMETERS:
  102. * *dev - pointer to Device structure
  103. * write_len - length of data buffer write_buf
  104. * *write_buf - pointer to buffer to write
  105. * *read_buf - pointer to buffer to read response into
  106. ******************************************************************************/
  107. static int pt_i2c_write_read_specific(struct device *dev, u16 write_len,
  108. u8 *write_buf, u8 *read_buf)
  109. {
  110. struct i2c_client *client = to_i2c_client(dev);
  111. struct i2c_msg msgs[2];
  112. u8 msg_count = 1;
  113. int rc;
  114. /* Ensure no packet larger than what the PIP spec allows */
  115. if (write_len > PT_MAX_PIP2_MSG_SIZE)
  116. return -EINVAL;
  117. if (!write_buf || !write_len) {
  118. if (!write_buf)
  119. pt_debug(dev, DL_ERROR,
  120. "%s write_buf is NULL", __func__);
  121. if (!write_len)
  122. pt_debug(dev, DL_ERROR,
  123. "%s write_len is NULL", __func__);
  124. return -EINVAL;
  125. }
  126. msgs[0].addr = client->addr;
  127. msgs[0].flags = client->flags & I2C_M_TEN;
  128. msgs[0].len = write_len;
  129. msgs[0].buf = write_buf;
  130. rc = i2c_transfer(client->adapter, msgs, msg_count);
  131. if (rc < 0 || rc != msg_count)
  132. return (rc < 0) ? rc : -EIO;
  133. rc = 0;
  134. if (read_buf) {
  135. rc = pt_i2c_read_default_nosize(dev, read_buf,
  136. PT_I2C_DATA_SIZE);
  137. }
  138. return rc;
  139. }
  140. static struct pt_bus_ops pt_i2c_bus_ops = {
  141. .bustype = BUS_I2C,
  142. .read_default = pt_i2c_read_default,
  143. .read_default_nosize = pt_i2c_read_default_nosize,
  144. .write_read_specific = pt_i2c_write_read_specific,
  145. };
  146. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  147. static const struct of_device_id pt_i2c_of_match[] = {
  148. { .compatible = "parade,pt_i2c_adapter", },
  149. { }
  150. };
  151. MODULE_DEVICE_TABLE(of, pt_i2c_of_match);
  152. #endif
  153. /*******************************************************************************
  154. * FUNCTION: pt_i2c_probe
  155. *
  156. * SUMMARY: Probe functon for the I2C module
  157. *
  158. * PARAMETERS:
  159. * *client - pointer to i2c client structure
  160. * *i2c_id - pointer to i2c device structure
  161. ******************************************************************************/
  162. static int pt_i2c_probe(struct i2c_client *client,
  163. const struct i2c_device_id *i2c_id)
  164. {
  165. struct device *dev = &client->dev;
  166. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  167. const struct of_device_id *match;
  168. #endif
  169. int rc;
  170. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  171. pt_debug(dev, DL_ERROR, "I2C functionality not Supported\n");
  172. return -EIO;
  173. }
  174. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  175. match = of_match_device(of_match_ptr(pt_i2c_of_match), dev);
  176. if (match) {
  177. rc = pt_devtree_create_and_get_pdata(dev);
  178. if (rc < 0)
  179. return rc;
  180. }
  181. #endif
  182. rc = pt_probe(&pt_i2c_bus_ops, &client->dev, client->irq,
  183. PT_I2C_DATA_SIZE);
  184. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  185. if (rc && match)
  186. pt_devtree_clean_pdata(dev);
  187. #endif
  188. return rc;
  189. }
  190. /*******************************************************************************
  191. * FUNCTION: pt_i2c_remove
  192. *
  193. * SUMMARY: Remove functon for the I2C module
  194. *
  195. * PARAMETERS:
  196. * *client - pointer to i2c client structure
  197. ******************************************************************************/
  198. static int pt_i2c_remove(struct i2c_client *client)
  199. {
  200. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  201. const struct of_device_id *match;
  202. #endif
  203. struct device *dev = &client->dev;
  204. struct pt_core_data *cd = i2c_get_clientdata(client);
  205. pt_release(cd);
  206. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  207. match = of_match_device(of_match_ptr(pt_i2c_of_match), dev);
  208. if (match)
  209. pt_devtree_clean_pdata(dev);
  210. #endif
  211. return 0;
  212. }
  213. /*******************************************************************************
  214. * FUNCTION: pt_i2c_shutdown
  215. *
  216. * SUMMARY: Shutdown functon for the I2C module
  217. *
  218. * PARAMETERS:
  219. * *client - pointer to i2c client structure
  220. ******************************************************************************/
  221. void pt_i2c_shutdown(struct i2c_client *client)
  222. {
  223. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  224. const struct of_device_id *match;
  225. #endif
  226. struct device *dev = &client->dev;
  227. struct pt_core_data *cd = i2c_get_clientdata(client);
  228. pt_release(cd);
  229. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  230. match = of_match_device(of_match_ptr(pt_i2c_of_match), dev);
  231. if (match)
  232. pt_devtree_clean_pdata(dev);
  233. #endif
  234. }
  235. static const struct i2c_device_id pt_i2c_id[] = {
  236. { PT_I2C_NAME, 0, },
  237. { }
  238. };
  239. MODULE_DEVICE_TABLE(i2c, pt_i2c_id);
  240. static struct i2c_driver pt_i2c_driver = {
  241. .driver = {
  242. .name = PT_I2C_NAME,
  243. .owner = THIS_MODULE,
  244. .pm = &pt_pm_ops,
  245. #ifdef CONFIG_TOUCHSCREEN_PARADE_DEVICETREE_SUPPORT
  246. .of_match_table = pt_i2c_of_match,
  247. #endif
  248. },
  249. .probe = pt_i2c_probe,
  250. .remove = pt_i2c_remove,
  251. .shutdown = pt_i2c_shutdown,
  252. .id_table = pt_i2c_id,
  253. };
  254. #if (KERNEL_VERSION(3, 3, 0) <= LINUX_VERSION_CODE)
  255. module_i2c_driver(pt_i2c_driver);
  256. #else
  257. /*******************************************************************************
  258. * FUNCTION: pt_i2c_init
  259. *
  260. * SUMMARY: Initialize function to register i2c module to kernel.
  261. *
  262. * RETURN:
  263. * 0 = success
  264. * !0 = failure
  265. ******************************************************************************/
  266. static int __init pt_i2c_init(void)
  267. {
  268. int rc = i2c_add_driver(&pt_i2c_driver);
  269. pr_info("%s: Parade TTDL I2C Driver (Build %s) rc=%d\n",
  270. __func__, PT_DRIVER_VERSION, rc);
  271. return rc;
  272. }
  273. module_init(pt_i2c_init);
  274. /*******************************************************************************
  275. * FUNCTION: pt_i2c_exit
  276. *
  277. * SUMMARY: Exit function to unregister i2c module from kernel.
  278. *
  279. ******************************************************************************/
  280. static void __exit pt_i2c_exit(void)
  281. {
  282. i2c_del_driver(&pt_i2c_driver);
  283. }
  284. module_exit(pt_i2c_exit);
  285. #endif
  286. MODULE_LICENSE("GPL");
  287. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product I2C driver");
  288. MODULE_AUTHOR("Parade Technologies <[email protected]>");