synaptics_tcm_i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Synaptics TCM touchscreen driver
  3. *
  4. * Copyright (C) 2017-2019 Synaptics Incorporated. All rights reserved.
  5. *
  6. * Copyright (C) 2017-2019 Scott Lin <[email protected]>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * INFORMATION CONTAINED IN THIS DOCUMENT IS PROVIDED "AS-IS," AND SYNAPTICS
  19. * EXPRESSLY DISCLAIMS ALL EXPRESS AND IMPLIED WARRANTIES, INCLUDING ANY
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
  21. * AND ANY WARRANTIES OF NON-INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHTS.
  22. * IN NO EVENT SHALL SYNAPTICS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, PUNITIVE, OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR IN CONNECTION
  24. * WITH THE USE OF THE INFORMATION CONTAINED IN THIS DOCUMENT, HOWEVER CAUSED
  25. * AND BASED ON ANY THEORY OF LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  26. * NEGLIGENCE OR OTHER TORTIOUS ACTION, AND EVEN IF SYNAPTICS WAS ADVISED OF
  27. * THE POSSIBILITY OF SUCH DAMAGE. IF A TRIBUNAL OF COMPETENT JURISDICTION DOES
  28. * NOT PERMIT THE DISCLAIMER OF DIRECT DAMAGES OR ANY OTHER DAMAGES, SYNAPTICS'
  29. * TOTAL CUMULATIVE LIABILITY TO ANY PARTY SHALL NOT EXCEED ONE HUNDRED U.S.
  30. * DOLLARS.
  31. */
  32. #include <linux/i2c.h>
  33. #include <linux/of_gpio.h>
  34. #include <linux/version.h>
  35. #include "synaptics_tcm_core.h"
  36. #include "linux/moduleparam.h"
  37. #define XFER_ATTEMPTS 10
  38. static unsigned char *buf;
  39. static unsigned int buf_size;
  40. static struct syna_tcm_bus_io bus_io;
  41. static struct syna_tcm_hw_interface hw_if;
  42. static struct platform_device *syna_tcm_i2c_device;
  43. static struct drm_panel *active_tcm_panel;
  44. struct drm_panel *tcm_get_panel(void)
  45. {
  46. return active_tcm_panel;
  47. }
  48. EXPORT_SYMBOL(tcm_get_panel);
  49. #ifdef CONFIG_OF
  50. static int parse_dt(struct device *dev, struct syna_tcm_board_data *bdata)
  51. {
  52. int retval;
  53. struct device_node *np = dev->of_node;
  54. retval = of_get_named_gpio_flags(np,
  55. "synaptics,irq-gpio", 0,
  56. (enum of_gpio_flags *)&bdata->irq_flags);
  57. if (!gpio_is_valid(retval)) {
  58. if (retval != -EPROBE_DEFER)
  59. dev_err(dev, "Error getting irq_gpio\n");
  60. return retval;
  61. }
  62. bdata->irq_gpio = retval;
  63. of_property_read_u32(np, "synaptics,irq-on-state",
  64. &bdata->irq_on_state);
  65. of_property_read_string(np, "synaptics,pwr-reg-name",
  66. &bdata->pwr_reg_name);
  67. of_property_read_string(np, "synaptics,bus-reg-name",
  68. &bdata->bus_reg_name);
  69. of_property_read_string(np, "synaptics,firmware-name",
  70. &bdata->fw_name);
  71. bdata->power_gpio = of_get_named_gpio_flags(np,
  72. "synaptics,power-gpio", 0, NULL);
  73. retval = of_property_read_u32(np, "synaptics,power-on-state",
  74. &bdata->power_on_state);
  75. if (retval < 0) {
  76. LOGD(dev, "Failed to read synaptics,power-on-state\n");
  77. bdata->power_on_state = 0;
  78. }
  79. retval = of_property_read_u32(np, "synaptics,power-delay-ms",
  80. &bdata->power_delay_ms);
  81. if (retval < 0) {
  82. LOGE(dev, "Failed to read synaptics,power-delay-ms\n");
  83. return retval;
  84. }
  85. retval = of_get_named_gpio_flags(np,
  86. "synaptics,reset-gpio", 0, NULL);
  87. if (!gpio_is_valid(retval)) {
  88. if (retval != -EPROBE_DEFER)
  89. dev_err(dev, "Error getting reset gpio\n");
  90. return retval;
  91. }
  92. bdata->reset_gpio = retval;
  93. retval = of_property_read_u32(np, "synaptics,reset-on-state",
  94. &bdata->reset_on_state);
  95. if (retval < 0) {
  96. LOGE(dev, "Failed to read synaptics,reset-on-state\n");
  97. return retval;
  98. }
  99. retval = of_property_read_u32(np, "synaptics,reset-active-ms",
  100. &bdata->reset_active_ms);
  101. if (retval < 0) {
  102. LOGE(dev, "Failed to read synaptics,reset-active-ms\n");
  103. return retval;
  104. }
  105. retval = of_property_read_u32(np, "synaptics,reset-delay-ms",
  106. &bdata->reset_delay_ms);
  107. if (retval < 0) {
  108. LOGE(dev, "Unable to read synaptics,reset-delay-ms\n");
  109. return retval;
  110. }
  111. bdata->x_flip = of_property_read_bool(np, "synaptics,x-flip");
  112. bdata->y_flip = of_property_read_bool(np, "synaptics,y-flip");
  113. bdata->swap_axes = of_property_read_bool(np, "synaptics,swap-axes");
  114. retval = of_property_read_u32(np, "synaptics,ubl-i2c-addr",
  115. &bdata->ubl_i2c_addr);
  116. if (retval < 0) {
  117. LOGE(dev, "Unable to read synaptics,ubl-i2c-addr\n");
  118. return retval;
  119. }
  120. bdata->extend_report = of_property_read_bool(np,
  121. "synaptics,extend_report");
  122. return 0;
  123. }
  124. #endif
  125. static int syna_tcm_i2c_alloc_mem(struct syna_tcm_hcd *tcm_hcd,
  126. unsigned int size)
  127. {
  128. struct i2c_client *i2c = to_i2c_client(tcm_hcd->pdev->dev.parent);
  129. if (size > buf_size) {
  130. if (buf_size)
  131. kfree(buf);
  132. buf = kmalloc(size, GFP_KERNEL);
  133. if (!buf) {
  134. LOGE(&i2c->dev,
  135. "Failed to allocate memory for buf\n");
  136. buf_size = 0;
  137. return -ENOMEM;
  138. }
  139. buf_size = size;
  140. }
  141. return 0;
  142. }
  143. static int syna_tcm_i2c_rmi_read(struct syna_tcm_hcd *tcm_hcd,
  144. unsigned short addr, unsigned char *data, unsigned int length)
  145. {
  146. int retval = 0;
  147. unsigned char address;
  148. unsigned int attempt;
  149. struct i2c_msg msg[2];
  150. struct i2c_client *i2c = to_i2c_client(tcm_hcd->pdev->dev.parent);
  151. const struct syna_tcm_board_data *bdata = tcm_hcd->hw_if->bdata;
  152. mutex_lock(&tcm_hcd->io_ctrl_mutex);
  153. address = (unsigned char)addr;
  154. msg[0].addr = bdata->ubl_i2c_addr;
  155. msg[0].flags = 0;
  156. msg[0].len = 1;
  157. msg[0].buf = &address;
  158. msg[1].addr = bdata->ubl_i2c_addr;
  159. msg[1].flags = I2C_M_RD;
  160. msg[1].len = length;
  161. msg[1].buf = data;
  162. for (attempt = 0; attempt < XFER_ATTEMPTS; attempt++) {
  163. if (i2c_transfer(i2c->adapter, msg, 2) == 2) {
  164. retval = length;
  165. goto exit;
  166. }
  167. LOGD(&i2c->dev, "Transfer attempt %d times\n", attempt + 1);
  168. if (attempt + 1 == XFER_ATTEMPTS) {
  169. LOGE(&i2c->dev, "Transfer failed\n");
  170. retval = -EIO;
  171. goto exit;
  172. }
  173. msleep(20);
  174. }
  175. exit:
  176. mutex_unlock(&tcm_hcd->io_ctrl_mutex);
  177. return retval;
  178. }
  179. static int syna_tcm_i2c_rmi_write(struct syna_tcm_hcd *tcm_hcd,
  180. unsigned short addr, unsigned char *data, unsigned int length)
  181. {
  182. int retval;
  183. unsigned int attempt;
  184. unsigned int byte_count;
  185. struct i2c_msg msg;
  186. struct i2c_client *i2c = to_i2c_client(tcm_hcd->pdev->dev.parent);
  187. const struct syna_tcm_board_data *bdata = tcm_hcd->hw_if->bdata;
  188. mutex_lock(&tcm_hcd->io_ctrl_mutex);
  189. byte_count = length + 1;
  190. retval = syna_tcm_i2c_alloc_mem(tcm_hcd, byte_count);
  191. if (retval < 0) {
  192. LOGE(&i2c->dev,
  193. "Failed to allocate memory\n");
  194. goto exit;
  195. }
  196. buf[0] = (unsigned char)addr;
  197. retval = secure_memcpy(&buf[1],
  198. buf_size - 1,
  199. data,
  200. length,
  201. length);
  202. if (retval < 0) {
  203. LOGE(&i2c->dev,
  204. "Failed to copy write data\n");
  205. goto exit;
  206. }
  207. msg.addr = bdata->ubl_i2c_addr;
  208. msg.flags = 0;
  209. msg.len = byte_count;
  210. msg.buf = buf;
  211. for (attempt = 0; attempt < XFER_ATTEMPTS; attempt++) {
  212. if (i2c_transfer(i2c->adapter, &msg, 1) == 1) {
  213. retval = length;
  214. goto exit;
  215. }
  216. LOGD(&i2c->dev, "Transfer attempt %d times\n", attempt + 1);
  217. if (attempt + 1 == XFER_ATTEMPTS) {
  218. LOGE(&i2c->dev, "Transfer failed\n");
  219. retval = -EIO;
  220. goto exit;
  221. }
  222. msleep(20);
  223. }
  224. exit:
  225. mutex_unlock(&tcm_hcd->io_ctrl_mutex);
  226. return retval;
  227. }
  228. static int syna_tcm_i2c_read(struct syna_tcm_hcd *tcm_hcd, unsigned char *data,
  229. unsigned int length)
  230. {
  231. int retval = 0;
  232. unsigned int attempt;
  233. struct i2c_msg msg;
  234. struct i2c_client *i2c = to_i2c_client(tcm_hcd->pdev->dev.parent);
  235. mutex_lock(&tcm_hcd->io_ctrl_mutex);
  236. msg.addr = i2c->addr;
  237. msg.flags = I2C_M_RD;
  238. msg.len = length;
  239. msg.buf = data;
  240. for (attempt = 0; attempt < XFER_ATTEMPTS; attempt++) {
  241. if (i2c_transfer(i2c->adapter, &msg, 1) == 1) {
  242. retval = length;
  243. goto exit;
  244. }
  245. LOGD(&i2c->dev, "Transfer attempt %d times\n", attempt + 1);
  246. if (attempt + 1 == XFER_ATTEMPTS) {
  247. LOGE(&i2c->dev, "Transfer failed\n");
  248. retval = -EIO;
  249. goto exit;
  250. }
  251. msleep(20);
  252. }
  253. exit:
  254. mutex_unlock(&tcm_hcd->io_ctrl_mutex);
  255. return retval;
  256. }
  257. static int syna_tcm_i2c_write(struct syna_tcm_hcd *tcm_hcd, unsigned char *data,
  258. unsigned int length)
  259. {
  260. int retval = 0;
  261. unsigned int attempt;
  262. struct i2c_msg msg;
  263. struct i2c_client *i2c = to_i2c_client(tcm_hcd->pdev->dev.parent);
  264. mutex_lock(&tcm_hcd->io_ctrl_mutex);
  265. msg.addr = i2c->addr;
  266. msg.flags = 0;
  267. msg.len = length;
  268. msg.buf = data;
  269. for (attempt = 0; attempt < XFER_ATTEMPTS; attempt++) {
  270. if (i2c_transfer(i2c->adapter, &msg, 1) == 1) {
  271. retval = length;
  272. goto exit;
  273. }
  274. LOGD(&i2c->dev, "Transfer attempt %d times\n", attempt + 1);
  275. if (attempt + 1 == XFER_ATTEMPTS) {
  276. LOGE(&i2c->dev, "Transfer failed\n");
  277. retval = -EIO;
  278. goto exit;
  279. }
  280. msleep(20);
  281. }
  282. exit:
  283. mutex_unlock(&tcm_hcd->io_ctrl_mutex);
  284. return retval;
  285. }
  286. static int syna_tcm_check_dt(struct device_node *np)
  287. {
  288. int i;
  289. int count;
  290. struct device_node *node;
  291. struct drm_panel *panel;
  292. count = of_count_phandle_with_args(np, "panel", NULL);
  293. if (count <= 0)
  294. return 0;
  295. for (i = 0; i < count; i++) {
  296. node = of_parse_phandle(np, "panel", i);
  297. panel = of_drm_find_panel(node);
  298. of_node_put(node);
  299. if (!IS_ERR(panel)) {
  300. active_tcm_panel = panel;
  301. return 0;
  302. }
  303. }
  304. return PTR_ERR(panel);
  305. }
  306. static int syna_tcm_check_default_tp(struct device_node *dt, const char *prop)
  307. {
  308. const char *active_tp;
  309. const char *compatible;
  310. char *start;
  311. int ret;
  312. ret = of_property_read_string(dt->parent, prop, &active_tp);
  313. if (ret) {
  314. pr_err(" %s:fail to read %s %d\n", __func__, prop, ret);
  315. return -ENODEV;
  316. }
  317. ret = of_property_read_string(dt, "compatible", &compatible);
  318. if (ret < 0) {
  319. pr_err(" %s:fail to read %s %d\n", __func__, "compatible", ret);
  320. return -ENODEV;
  321. }
  322. start = strnstr(active_tp, compatible, strlen(active_tp));
  323. if (start == NULL) {
  324. pr_err(" %s:no match compatible, %s, %s\n",
  325. __func__, compatible, active_tp);
  326. ret = -ENODEV;
  327. }
  328. return ret;
  329. }
  330. static int syna_tcm_i2c_probe(struct i2c_client *i2c,
  331. const struct i2c_device_id *dev_id)
  332. {
  333. int retval;
  334. struct device_node *dt = i2c->dev.of_node;
  335. retval = syna_tcm_check_dt(dt);
  336. if (retval == -EPROBE_DEFER)
  337. return retval;
  338. if (retval) {
  339. if (!syna_tcm_check_default_tp(dt, "qcom,i2c-touch-active"))
  340. retval = -EPROBE_DEFER;
  341. else
  342. retval = -ENODEV;
  343. return retval;
  344. }
  345. syna_tcm_i2c_device = platform_device_alloc(PLATFORM_DRIVER_NAME, 0);
  346. if (!syna_tcm_i2c_device) {
  347. LOGE(&i2c->dev,
  348. "Failed to allocate platform device\n");
  349. return -ENOMEM;
  350. }
  351. #ifdef CONFIG_OF
  352. hw_if.bdata = devm_kzalloc(&i2c->dev, sizeof(*hw_if.bdata), GFP_KERNEL);
  353. if (!hw_if.bdata) {
  354. LOGE(&i2c->dev,
  355. "Failed to allocate memory for board data\n");
  356. return -ENOMEM;
  357. }
  358. retval = parse_dt(&i2c->dev, hw_if.bdata);
  359. if (retval < 0) {
  360. LOGE(&i2c->dev, "Failed to parse dt\n");
  361. return retval;
  362. }
  363. #else
  364. hw_if.bdata = i2c->dev.platform_data;
  365. #endif
  366. bus_io.type = BUS_I2C;
  367. bus_io.read = syna_tcm_i2c_read;
  368. bus_io.write = syna_tcm_i2c_write;
  369. bus_io.rmi_read = syna_tcm_i2c_rmi_read;
  370. bus_io.rmi_write = syna_tcm_i2c_rmi_write;
  371. hw_if.bus_io = &bus_io;
  372. syna_tcm_i2c_device->dev.parent = &i2c->dev;
  373. syna_tcm_i2c_device->dev.platform_data = &hw_if;
  374. retval = platform_device_add(syna_tcm_i2c_device);
  375. if (retval < 0) {
  376. LOGE(&i2c->dev,
  377. "Failed to add platform device\n");
  378. return retval;
  379. }
  380. return 0;
  381. }
  382. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0))
  383. static void syna_tcm_i2c_remove(struct i2c_client *i2c)
  384. {
  385. syna_tcm_i2c_device->dev.platform_data = NULL;
  386. platform_device_unregister(syna_tcm_i2c_device);
  387. }
  388. #else
  389. static int syna_tcm_i2c_remove(struct i2c_client *i2c)
  390. {
  391. syna_tcm_i2c_device->dev.platform_data = NULL;
  392. platform_device_unregister(syna_tcm_i2c_device);
  393. return 0;
  394. }
  395. #endif
  396. static const struct i2c_device_id syna_tcm_id_table[] = {
  397. {I2C_MODULE_NAME, 0},
  398. {},
  399. };
  400. MODULE_DEVICE_TABLE(i2c, syna_tcm_id_table);
  401. #ifdef CONFIG_OF
  402. static const struct of_device_id syna_tcm_of_match_table[] = {
  403. {
  404. .compatible = "synaptics,tcm-i2c",
  405. },
  406. {},
  407. };
  408. MODULE_DEVICE_TABLE(of, syna_tcm_of_match_table);
  409. #else
  410. #define syna_tcm_of_match_table NULL
  411. #endif
  412. static struct i2c_driver syna_tcm_i2c_driver = {
  413. .driver = {
  414. .name = I2C_MODULE_NAME,
  415. .owner = THIS_MODULE,
  416. .of_match_table = syna_tcm_of_match_table,
  417. },
  418. .probe = syna_tcm_i2c_probe,
  419. .remove = syna_tcm_i2c_remove,
  420. .id_table = syna_tcm_id_table,
  421. };
  422. int syna_tcm_bus_init(void)
  423. {
  424. return i2c_add_driver(&syna_tcm_i2c_driver);
  425. }
  426. EXPORT_SYMBOL(syna_tcm_bus_init);
  427. void syna_tcm_bus_exit(void)
  428. {
  429. kfree(buf);
  430. i2c_del_driver(&syna_tcm_i2c_driver);
  431. }
  432. EXPORT_SYMBOL(syna_tcm_bus_exit);
  433. MODULE_AUTHOR("Synaptics, Inc.");
  434. MODULE_DESCRIPTION("Synaptics TCM I2C Bus Module");
  435. MODULE_LICENSE("GPL v2");