focaltech_i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. *
  3. * FocalTech TouchScreen driver.
  4. *
  5. * Copyright (c) 2012-2019, FocalTech Systems, Ltd., all rights reserved.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. /************************************************************************
  18. *
  19. * File Name: focaltech_i2c.c
  20. *
  21. * Author: Focaltech Driver Team
  22. *
  23. * Created: 2016-08-04
  24. *
  25. * Abstract: i2c communication with TP
  26. *
  27. * Version: v1.0
  28. *
  29. * Revision History:
  30. *
  31. ************************************************************************/
  32. /*****************************************************************************
  33. * Included header files
  34. *****************************************************************************/
  35. #include "focaltech_core.h"
  36. #include <linux/pm_runtime.h>
  37. /*****************************************************************************
  38. * Private constant and macro definitions using #define
  39. *****************************************************************************/
  40. #define I2C_RETRY_NUMBER 3
  41. #define I2C_BUF_LENGTH 256
  42. /*****************************************************************************
  43. * Private enumerations, structures and unions using typedef
  44. *****************************************************************************/
  45. /*****************************************************************************
  46. * Static variables
  47. *****************************************************************************/
  48. static struct fts_ts_data *ts_data;
  49. /*****************************************************************************
  50. * Global variable or extern global variabls/functions
  51. *****************************************************************************/
  52. /*****************************************************************************
  53. * Static function prototypes
  54. *****************************************************************************/
  55. /*****************************************************************************
  56. * functions body
  57. *****************************************************************************/
  58. static int fts_i2c_read(u8 *cmd, u32 cmdlen, u8 *data, u32 datalen)
  59. {
  60. int ret = 0;
  61. int i = 0;
  62. struct i2c_msg msg_list[2];
  63. struct i2c_msg *msg = NULL;
  64. int msg_num = 0;
  65. /* must have data when read */
  66. if (!ts_data || !ts_data->client || !data || !datalen
  67. || (datalen >= I2C_BUF_LENGTH) || (cmdlen >= I2C_BUF_LENGTH)) {
  68. FTS_ERROR("fts_data/client/cmdlen(%d)/data/datalen(%d) is invalid",
  69. cmdlen, datalen);
  70. return -EINVAL;
  71. }
  72. mutex_lock(&ts_data->bus_lock);
  73. memset(&msg_list[0], 0, sizeof(struct i2c_msg));
  74. memset(&msg_list[1], 0, sizeof(struct i2c_msg));
  75. memcpy(ts_data->bus_tx_buf, cmd, cmdlen);
  76. msg_list[0].addr = ts_data->client->addr;
  77. msg_list[0].flags = 0;
  78. msg_list[0].len = cmdlen;
  79. msg_list[0].buf = ts_data->bus_tx_buf;
  80. msg_list[1].addr = ts_data->client->addr;
  81. msg_list[1].flags = I2C_M_RD;
  82. msg_list[1].len = datalen;
  83. msg_list[1].buf = ts_data->bus_rx_buf;
  84. if (cmd && cmdlen) {
  85. msg = &msg_list[0];
  86. msg_num = 2;
  87. } else {
  88. msg = &msg_list[1];
  89. msg_num = 1;
  90. }
  91. for (i = 0; i < I2C_RETRY_NUMBER; i++) {
  92. ret = i2c_transfer(ts_data->client->adapter, msg, msg_num);
  93. if (ret < 0) {
  94. #ifdef CONFIG_FTS_TRUSTED_TOUCH
  95. #ifdef CONFIG_ARCH_QTI_VM
  96. if (atomic_read(&ts_data->trusted_touch_enabled) &&
  97. ret == -ECONNRESET) {
  98. pr_err("failed i2c read reacquiring session\n");
  99. pm_runtime_put_sync(
  100. ts_data->client->adapter->dev.parent);
  101. pm_runtime_get_sync(
  102. ts_data->client->adapter->dev.parent);
  103. }
  104. #endif
  105. #endif
  106. FTS_ERROR("i2c_transfer(read) fail,ret:%d", ret);
  107. } else {
  108. memcpy(data, ts_data->bus_rx_buf, datalen);
  109. break;
  110. }
  111. }
  112. if (ret < 0) {
  113. #ifdef CONFIG_FTS_TRUSTED_TOUCH
  114. #ifdef CONFIG_ARCH_QTI_VM
  115. pr_err("initiating abort due to i2c xfer failure\n");
  116. fts_ts_trusted_touch_tvm_i2c_failure_report(ts_data);
  117. #endif
  118. #endif
  119. }
  120. mutex_unlock(&ts_data->bus_lock);
  121. return ret;
  122. }
  123. static int fts_i2c_write(u8 *writebuf, u32 writelen)
  124. {
  125. int ret = 0;
  126. int i = 0;
  127. struct i2c_msg msgs;
  128. if (!ts_data || !ts_data->client || !writebuf || !writelen
  129. || (writelen >= I2C_BUF_LENGTH)) {
  130. FTS_ERROR("fts_data/client/data/datalen(%d) is invalid", writelen);
  131. return -EINVAL;
  132. }
  133. mutex_lock(&ts_data->bus_lock);
  134. memset(&msgs, 0, sizeof(struct i2c_msg));
  135. memcpy(ts_data->bus_tx_buf, writebuf, writelen);
  136. msgs.addr = ts_data->client->addr;
  137. msgs.flags = 0;
  138. msgs.len = writelen;
  139. msgs.buf = ts_data->bus_tx_buf;
  140. for (i = 0; i < I2C_RETRY_NUMBER; i++) {
  141. ret = i2c_transfer(ts_data->client->adapter, &msgs, 1);
  142. if (ret < 0) {
  143. #ifdef CONFIG_FTS_TRUSTED_TOUCH
  144. #ifdef CONFIG_ARCH_QTI_VM
  145. if (atomic_read(&ts_data->trusted_touch_enabled) &&
  146. ret == -ECONNRESET){
  147. pr_err("failed i2c write reacquiring session\n");
  148. pm_runtime_put_sync(
  149. ts_data->client->adapter->dev.parent);
  150. pm_runtime_get_sync(
  151. ts_data->client->adapter->dev.parent);
  152. }
  153. #endif
  154. #endif
  155. FTS_ERROR("i2c_transfer(write) fail,ret:%d", ret);
  156. } else {
  157. break;
  158. }
  159. }
  160. if (ret < 0) {
  161. #ifdef CONFIG_FTS_TRUSTED_TOUCH
  162. #ifdef CONFIG_ARCH_QTI_VM
  163. pr_err("initiating abort due to i2c xfer failure\n");
  164. fts_ts_trusted_touch_tvm_i2c_failure_report(ts_data);
  165. #endif
  166. #endif
  167. }
  168. mutex_unlock(&ts_data->bus_lock);
  169. return ret;
  170. }
  171. static int fts_i2c_init(struct fts_ts_data *ts_data)
  172. {
  173. FTS_FUNC_ENTER();
  174. ts_data->bus_tx_buf = kzalloc(I2C_BUF_LENGTH, GFP_KERNEL);
  175. if (ts_data->bus_tx_buf == NULL) {
  176. FTS_ERROR("failed to allocate memory for bus_tx_buf");
  177. return -ENOMEM;
  178. }
  179. ts_data->bus_rx_buf = kzalloc(I2C_BUF_LENGTH, GFP_KERNEL);
  180. if (ts_data->bus_rx_buf == NULL) {
  181. FTS_ERROR("failed to allocate memory for bus_rx_buf");
  182. return -ENOMEM;
  183. }
  184. FTS_FUNC_EXIT();
  185. return 0;
  186. }
  187. static int fts_i2c_exit(struct fts_ts_data *ts_data)
  188. {
  189. FTS_FUNC_ENTER();
  190. if (ts_data && ts_data->bus_tx_buf) {
  191. kfree(ts_data->bus_tx_buf);
  192. ts_data->bus_tx_buf = NULL;
  193. }
  194. if (ts_data && ts_data->bus_rx_buf) {
  195. kfree(ts_data->bus_rx_buf);
  196. ts_data->bus_rx_buf = NULL;
  197. }
  198. FTS_FUNC_EXIT();
  199. return 0;
  200. }
  201. /*****************************************************************************
  202. * Private constant and macro definitions using #define
  203. ****************************************************************************/
  204. #define SPI_RETRY_NUMBER 3
  205. #define CS_HIGH_DELAY 150 /* unit: us */
  206. #define SPI_BUF_LENGTH 256
  207. #define DATA_CRC_EN 0x20
  208. #define WRITE_CMD 0x00
  209. #define READ_CMD (0x80 | DATA_CRC_EN)
  210. #define SPI_DUMMY_BYTE 3
  211. #define SPI_HEADER_LENGTH 6 /*CRC*/
  212. /*****************************************************************************
  213. * functions body
  214. ****************************************************************************/
  215. /* spi interface */
  216. static int fts_spi_transfer(u8 *tx_buf, u8 *rx_buf, u32 len)
  217. {
  218. int ret = 0;
  219. struct spi_device *spi = fts_data->spi;
  220. struct spi_message msg;
  221. struct spi_transfer xfer = {
  222. .tx_buf = tx_buf,
  223. .rx_buf = rx_buf,
  224. .len = len,
  225. };
  226. spi_message_init(&msg);
  227. spi_message_add_tail(&xfer, &msg);
  228. ret = spi_sync(spi, &msg);
  229. if (ret) {
  230. FTS_ERROR("spi_sync fail,ret:%d", ret);
  231. return ret;
  232. }
  233. return ret;
  234. }
  235. static void crckermit(u8 *data, u32 len, u16 *crc_out)
  236. {
  237. u32 i = 0;
  238. u32 j = 0;
  239. u16 crc = 0xFFFF;
  240. for (i = 0; i < len; i++) {
  241. crc ^= data[i];
  242. for (j = 0; j < 8; j++) {
  243. if (crc & 0x01)
  244. crc = (crc >> 1) ^ 0x8408;
  245. else
  246. crc = (crc >> 1);
  247. }
  248. }
  249. *crc_out = crc;
  250. }
  251. static int rdata_check(u8 *rdata, u32 rlen)
  252. {
  253. u16 crc_calc = 0;
  254. u16 crc_read = 0;
  255. crckermit(rdata, rlen - 2, &crc_calc);
  256. crc_read = (u16)(rdata[rlen - 1] << 8) + rdata[rlen - 2];
  257. if (crc_calc != crc_read)
  258. return -EIO;
  259. return 0;
  260. }
  261. static int fts_spi_write(u8 *writebuf, u32 writelen)
  262. {
  263. int ret = 0;
  264. int i = 0;
  265. struct fts_ts_data *ts_data = fts_data;
  266. u8 *txbuf = NULL;
  267. u8 *rxbuf = NULL;
  268. u32 txlen = 0;
  269. u32 txlen_need = writelen + SPI_HEADER_LENGTH + ts_data->dummy_byte;
  270. u32 datalen = writelen - 1;
  271. if (!writebuf || !writelen) {
  272. FTS_ERROR("writebuf/len is invalid");
  273. return -EINVAL;
  274. }
  275. mutex_lock(&ts_data->bus_lock);
  276. if (txlen_need > SPI_BUF_LENGTH) {
  277. txbuf = kzalloc(txlen_need, GFP_KERNEL);
  278. if (txbuf == NULL) {
  279. FTS_ERROR("txbuf malloc fail");
  280. ret = -ENOMEM;
  281. goto err_write;
  282. }
  283. rxbuf = kzalloc(txlen_need, GFP_KERNEL);
  284. if (rxbuf == NULL) {
  285. FTS_ERROR("rxbuf malloc fail");
  286. ret = -ENOMEM;
  287. goto err_write;
  288. }
  289. } else {
  290. txbuf = ts_data->bus_tx_buf;
  291. rxbuf = ts_data->bus_rx_buf;
  292. memset(txbuf, 0x0, SPI_BUF_LENGTH);
  293. memset(rxbuf, 0x0, SPI_BUF_LENGTH);
  294. }
  295. txbuf[txlen++] = writebuf[0];
  296. txbuf[txlen++] = WRITE_CMD;
  297. txbuf[txlen++] = (datalen >> 8) & 0xFF;
  298. txbuf[txlen++] = datalen & 0xFF;
  299. if (datalen > 0) {
  300. txlen = txlen + SPI_DUMMY_BYTE;
  301. memcpy(&txbuf[txlen], &writebuf[1], datalen);
  302. txlen = txlen + datalen;
  303. }
  304. for (i = 0; i < SPI_RETRY_NUMBER; i++) {
  305. ret = fts_spi_transfer(txbuf, rxbuf, txlen);
  306. if ((ret == 0) && ((rxbuf[3] & 0xA0) == 0))
  307. break;
  308. FTS_DEBUG("data write(addr:%x),status:%x,retry:%d,ret:%d",
  309. writebuf[0], rxbuf[3], i, ret);
  310. ret = -EIO;
  311. udelay(CS_HIGH_DELAY);
  312. }
  313. if (ret < 0) {
  314. FTS_ERROR("data write(addr:%x) fail,status:%x,ret:%d",
  315. writebuf[0], rxbuf[3], ret);
  316. }
  317. err_write:
  318. if (txlen_need > SPI_BUF_LENGTH) {
  319. kfree(txbuf);
  320. kfree(rxbuf);
  321. }
  322. udelay(CS_HIGH_DELAY);
  323. mutex_unlock(&ts_data->bus_lock);
  324. return ret;
  325. }
  326. static int fts_spi_read(u8 *cmd, u32 cmdlen, u8 *data, u32 datalen)
  327. {
  328. int ret = 0;
  329. int i = 0;
  330. u8 *txbuf = NULL;
  331. u8 *rxbuf = NULL;
  332. u32 txlen = 0;
  333. u32 txlen_need = datalen + SPI_HEADER_LENGTH + ts_data->dummy_byte;
  334. u8 ctrl = READ_CMD;
  335. u32 dp = 0;
  336. if (!cmd || !cmdlen || !data || !datalen) {
  337. FTS_ERROR("cmd/cmdlen/data/datalen is invalid");
  338. return -EINVAL;
  339. }
  340. mutex_lock(&ts_data->bus_lock);
  341. if (txlen_need > SPI_BUF_LENGTH) {
  342. txbuf = kzalloc(txlen_need, GFP_KERNEL);
  343. if (txbuf == NULL) {
  344. FTS_ERROR("txbuf malloc fail");
  345. ret = -ENOMEM;
  346. goto err_read;
  347. }
  348. rxbuf = kzalloc(txlen_need, GFP_KERNEL);
  349. if (rxbuf == NULL) {
  350. FTS_ERROR("rxbuf malloc fail");
  351. ret = -ENOMEM;
  352. goto err_read;
  353. }
  354. } else {
  355. txbuf = ts_data->bus_tx_buf;
  356. rxbuf = ts_data->bus_rx_buf;
  357. memset(txbuf, 0x0, SPI_BUF_LENGTH);
  358. memset(rxbuf, 0x0, SPI_BUF_LENGTH);
  359. }
  360. txbuf[txlen++] = cmd[0];
  361. txbuf[txlen++] = ctrl;
  362. txbuf[txlen++] = (datalen >> 8) & 0xFF;
  363. txbuf[txlen++] = datalen & 0xFF;
  364. dp = txlen + SPI_DUMMY_BYTE;
  365. txlen = dp + datalen;
  366. if (ctrl & DATA_CRC_EN)
  367. txlen = txlen + 2;
  368. for (i = 0; i < SPI_RETRY_NUMBER; i++) {
  369. ret = fts_spi_transfer(txbuf, rxbuf, txlen);
  370. if ((ret == 0) && ((rxbuf[3] & 0xA0) == 0)) {
  371. memcpy(data, &rxbuf[dp], datalen);
  372. /* crc check */
  373. if (ctrl & DATA_CRC_EN) {
  374. ret = rdata_check(&rxbuf[dp], txlen - dp);
  375. if (ret < 0) {
  376. FTS_DEBUG("data read(addr:%x) crc abnormal,retry:%d",
  377. cmd[0], i);
  378. udelay(CS_HIGH_DELAY);
  379. continue;
  380. }
  381. }
  382. break;
  383. }
  384. FTS_DEBUG("data read(addr:%x) status:%x,retry:%d,ret:%d",
  385. cmd[0], rxbuf[3], i, ret);
  386. ret = -EIO;
  387. udelay(CS_HIGH_DELAY);
  388. }
  389. if (ret < 0) {
  390. FTS_ERROR("data read(addr:%x) %s,status:%x,ret:%d", cmd[0],
  391. (i >= SPI_RETRY_NUMBER) ? "crc abnormal" : "fail",
  392. rxbuf[3], ret);
  393. }
  394. err_read:
  395. if (txlen_need > SPI_BUF_LENGTH) {
  396. kfree(txbuf);
  397. kfree(rxbuf);
  398. }
  399. udelay(CS_HIGH_DELAY);
  400. mutex_unlock(&ts_data->bus_lock);
  401. return ret;
  402. }
  403. static int fts_spi_init(struct fts_ts_data *ts_data)
  404. {
  405. FTS_FUNC_ENTER();
  406. ts_data->bus_tx_buf = kzalloc(SPI_BUF_LENGTH, GFP_KERNEL);
  407. if (ts_data->bus_tx_buf == NULL) {
  408. FTS_ERROR("failed to allocate memory for bus_tx_buf");
  409. return -ENOMEM;
  410. }
  411. ts_data->bus_rx_buf = kzalloc(SPI_BUF_LENGTH, GFP_KERNEL);
  412. if (ts_data->bus_rx_buf == NULL) {
  413. FTS_ERROR("failed to allocate memory for bus_rx_buf");
  414. return -ENOMEM;
  415. }
  416. ts_data->dummy_byte = SPI_DUMMY_BYTE;
  417. FTS_FUNC_EXIT();
  418. return 0;
  419. }
  420. static int fts_spi_exit(struct fts_ts_data *ts_data)
  421. {
  422. FTS_FUNC_ENTER();
  423. if (ts_data && ts_data->bus_tx_buf) {
  424. kfree(ts_data->bus_tx_buf);
  425. ts_data->bus_tx_buf = NULL;
  426. }
  427. if (ts_data && ts_data->bus_rx_buf) {
  428. kfree(ts_data->bus_rx_buf);
  429. ts_data->bus_rx_buf = NULL;
  430. }
  431. FTS_FUNC_EXIT();
  432. return 0;
  433. }
  434. int fts_read(u8 *cmd, u32 cmdlen, u8 *data, u32 datalen)
  435. {
  436. int ret = 0;
  437. if (ts_data->bus_type == BUS_TYPE_I2C)
  438. ret = fts_i2c_read(cmd, cmdlen, data, datalen);
  439. else
  440. ret = fts_spi_read(cmd, cmdlen, data, datalen);
  441. return ret;
  442. }
  443. int fts_write(u8 *writebuf, u32 writelen)
  444. {
  445. int ret = 0;
  446. if (ts_data->bus_type == BUS_TYPE_I2C)
  447. ret = fts_i2c_write(writebuf, writelen);
  448. else
  449. ret = fts_spi_write(writebuf, writelen);
  450. return ret;
  451. }
  452. int fts_read_reg(u8 addr, u8 *value)
  453. {
  454. return fts_read(&addr, 1, value, 1);
  455. }
  456. int fts_write_reg(u8 addr, u8 value)
  457. {
  458. u8 buf[2] = { 0 };
  459. buf[0] = addr;
  460. buf[1] = value;
  461. return fts_write(buf, sizeof(buf));
  462. }
  463. int fts_bus_init(struct fts_ts_data *_ts_data)
  464. {
  465. ts_data = _ts_data;
  466. if (ts_data->bus_type == BUS_TYPE_I2C)
  467. return fts_i2c_init(ts_data);
  468. return fts_spi_init(ts_data);
  469. }
  470. int fts_bus_exit(struct fts_ts_data *ts_data)
  471. {
  472. if (ts_data->bus_type == BUS_TYPE_I2C)
  473. return fts_i2c_exit(ts_data);
  474. return fts_spi_exit(ts_data);
  475. }