ftsIO.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * FTS Capacitive touch screen controller (FingerTipS)
  4. *
  5. * Copyright (C) 2016-2019, STMicroelectronics Limited.
  6. * Authors: AMG(Analog Mems Group) <[email protected]>
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. *
  23. **************************************************************************
  24. ** STMicroelectronics **
  25. **************************************************************************
  26. ** [email protected] **
  27. **************************************************************************
  28. * *
  29. * I2C/SPI Communication **
  30. * *
  31. **************************************************************************
  32. **************************************************************************
  33. *
  34. */
  35. #include <linux/init.h>
  36. #include <linux/errno.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/kernel.h>
  39. #include <linux/module.h>
  40. #include <linux/slab.h>
  41. #include <linux/string.h>
  42. #include <stdarg.h>
  43. #include <linux/input.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/serio.h>
  46. #include <linux/init.h>
  47. #include <linux/pm.h>
  48. #include <linux/delay.h>
  49. #include <linux/ctype.h>
  50. #include <linux/gpio.h>
  51. #include <linux/i2c.h>
  52. #include <linux/i2c-dev.h>
  53. #include <linux/fs.h>
  54. #include <linux/uaccess.h>
  55. #include <linux/power_supply.h>
  56. #include <linux/firmware.h>
  57. #include <linux/regulator/consumer.h>
  58. #include <linux/of_gpio.h>
  59. #include <linux/i2c.h>
  60. #include <linux/i2c-dev.h>
  61. #include <linux/spi/spidev.h>
  62. #include <linux/timekeeping.h>
  63. #include "ftsSoftware.h"
  64. #include "ftsCrossCompile.h"
  65. #include "ftsError.h"
  66. #include "ftsHardware.h"
  67. #include "ftsIO.h"
  68. #include "ftsTool.h"
  69. #include "../fts.h"
  70. static char tag[8] = "[ FTS ]\0";
  71. static struct i2c_client *client;
  72. static u16 I2CSAD;
  73. int openChannel(struct i2c_client *clt)
  74. {
  75. client = clt;
  76. I2CSAD = clt->addr;
  77. logError(0, "%s %s: SAD: %02X\n", tag, __func__, I2CSAD);
  78. return OK;
  79. }
  80. struct device *getDev()
  81. {
  82. if (client != NULL)
  83. return &(client->dev);
  84. else
  85. return NULL;
  86. }
  87. struct i2c_client *getClient()
  88. {
  89. if (client != NULL)
  90. return client;
  91. else
  92. return NULL;
  93. }
  94. int fts_readCmd(u8 *cmd, int cmdLength, u8 *outBuf, int byteToRead)
  95. {
  96. int ret = -1;
  97. int retry = 0;
  98. struct i2c_msg I2CMsg[2];
  99. struct fts_ts_info *info = i2c_get_clientdata(client);
  100. uint8_t *buf = info->i2c_data;
  101. /*
  102. * Reassign memory for i2c_data in case length is greater than 32 bytes
  103. */
  104. if (info->i2c_data_len < cmdLength + byteToRead + 1) {
  105. kfree(info->i2c_data);
  106. info->i2c_data = kmalloc(cmdLength + byteToRead + 1,
  107. GFP_KERNEL);
  108. if (!info->i2c_data) {
  109. info->i2c_data_len = 0;
  110. return -ENOMEM;
  111. }
  112. info->i2c_data_len = cmdLength + byteToRead + 1;
  113. buf = info->i2c_data;
  114. }
  115. //write msg
  116. I2CMsg[0].addr = (__u16)I2CSAD;
  117. I2CMsg[0].flags = (__u16)0;
  118. I2CMsg[0].len = (__u16)cmdLength;
  119. I2CMsg[0].buf = buf;
  120. //read msg
  121. I2CMsg[1].addr = (__u16)I2CSAD;
  122. I2CMsg[1].flags = I2C_M_RD;
  123. I2CMsg[1].len = byteToRead;
  124. I2CMsg[1].buf = buf + cmdLength;
  125. memcpy(buf, cmd, cmdLength);
  126. if (client == NULL)
  127. return ERROR_I2C_O;
  128. while (retry < I2C_RETRY && ret < OK) {
  129. ret = i2c_transfer(client->adapter, I2CMsg, 2);
  130. retry++;
  131. if (ret < OK)
  132. msleep(I2C_WAIT_BEFORE_RETRY);
  133. }
  134. if (ret < 0) {
  135. logError(1, "%s %s: ERROR %02X\n",
  136. tag, __func__, ERROR_I2C_R);
  137. return ERROR_I2C_R;
  138. }
  139. memcpy(outBuf, buf + cmdLength, byteToRead);
  140. return OK;
  141. }
  142. int fts_writeCmd(u8 *cmd, int cmdLength)
  143. {
  144. int ret = -1;
  145. int retry = 0;
  146. struct i2c_msg I2CMsg[1];
  147. struct fts_ts_info *info = i2c_get_clientdata(client);
  148. uint8_t *buf = info->i2c_data;
  149. /*
  150. * Reassign memory for i2c_data in case length is greater than 32 bytes
  151. */
  152. if (info->i2c_data_len < cmdLength + 1) {
  153. kfree(info->i2c_data);
  154. info->i2c_data = kmalloc(cmdLength + 1, GFP_KERNEL);
  155. if (!info->i2c_data) {
  156. info->i2c_data_len = 0;
  157. return -ENOMEM;
  158. }
  159. info->i2c_data_len = cmdLength + 1;
  160. buf = info->i2c_data;
  161. }
  162. I2CMsg[0].addr = (__u16)I2CSAD;
  163. I2CMsg[0].flags = (__u16)0;
  164. I2CMsg[0].len = (__u16)cmdLength;
  165. I2CMsg[0].buf = buf;
  166. memcpy(buf, cmd, cmdLength);
  167. if (client == NULL)
  168. return ERROR_I2C_O;
  169. while (retry < I2C_RETRY && ret < OK) {
  170. ret = i2c_transfer(client->adapter, I2CMsg, 1);
  171. retry++;
  172. if (ret < OK)
  173. msleep(I2C_WAIT_BEFORE_RETRY);
  174. //logError(1,"%s fts_writeCmd: attempt %d\n", tag, retry);
  175. }
  176. if (ret < 0) {
  177. logError(1, "%s %s: ERROR %02X\n", tag, __func__, ERROR_I2C_W);
  178. return ERROR_I2C_W;
  179. }
  180. return OK;
  181. }
  182. int fts_writeFwCmd(u8 *cmd, int cmdLength)
  183. {
  184. int ret = -1;
  185. int ret2 = -1;
  186. int retry = 0;
  187. struct i2c_msg I2CMsg[1];
  188. struct fts_ts_info *info = i2c_get_clientdata(client);
  189. uint8_t *buf = info->i2c_data;
  190. /*
  191. * Reassign memory for i2c_data in case length is greater than 32 bytes
  192. */
  193. if (info->i2c_data_len < cmdLength + 1) {
  194. kfree(info->i2c_data);
  195. info->i2c_data = kmalloc(cmdLength + 1, GFP_KERNEL);
  196. if (!info->i2c_data) {
  197. info->i2c_data_len = 0;
  198. return -ENOMEM;
  199. }
  200. info->i2c_data_len = cmdLength + 1;
  201. buf = info->i2c_data;
  202. }
  203. I2CMsg[0].addr = (__u16)I2CSAD;
  204. I2CMsg[0].flags = (__u16)0;
  205. I2CMsg[0].len = (__u16)cmdLength;
  206. I2CMsg[0].buf = buf;
  207. memcpy(buf, cmd, cmdLength);
  208. if (client == NULL)
  209. return ERROR_I2C_O;
  210. while (retry < I2C_RETRY && (ret < OK || ret2 < OK)) {
  211. ret = i2c_transfer(client->adapter, I2CMsg, 1);
  212. retry++;
  213. if (ret >= 0)
  214. ret2 = checkEcho(cmd, cmdLength);
  215. if (ret < OK || ret2 < OK)
  216. msleep(I2C_WAIT_BEFORE_RETRY);
  217. //logError(1,"%s fts_writeCmd: attempt %d\n", tag, retry);
  218. }
  219. if (ret < 0) {
  220. logError(1, "%s %s: ERROR %02X\n",
  221. tag, __func__, ERROR_I2C_W);
  222. return ERROR_I2C_W;
  223. }
  224. if (ret2 < OK) {
  225. logError(1, "%s %s: check echo ERROR %02X\n",
  226. tag, __func__, ret2);
  227. return (ret | ERROR_I2C_W);
  228. }
  229. return OK;
  230. }
  231. int writeReadCmd(u8 *writeCmd1, int writeCmdLength, u8 *readCmd1,
  232. int readCmdLength, u8 *outBuf, int byteToRead)
  233. {
  234. int ret = -1;
  235. int retry = 0;
  236. struct i2c_msg I2CMsg[3];
  237. struct fts_ts_info *info = i2c_get_clientdata(client);
  238. uint8_t *buf = info->i2c_data;
  239. uint8_t wr_len = writeCmdLength + readCmdLength + byteToRead;
  240. /*
  241. * Reassign memory for i2c_data in case length is greater than 32 bytes
  242. */
  243. if (info->i2c_data_len < wr_len + 1) {
  244. kfree(info->i2c_data);
  245. info->i2c_data = kmalloc(wr_len + 1, GFP_KERNEL);
  246. if (!info->i2c_data) {
  247. info->i2c_data_len = 0;
  248. return -ENOMEM;
  249. }
  250. info->i2c_data_len = wr_len + 1;
  251. buf = info->i2c_data;
  252. }
  253. //write msg
  254. I2CMsg[0].addr = (__u16)I2CSAD;
  255. I2CMsg[0].flags = (__u16)0;
  256. I2CMsg[0].len = (__u16)writeCmdLength;
  257. I2CMsg[0].buf = buf;
  258. //write msg
  259. I2CMsg[1].addr = (__u16)I2CSAD;
  260. I2CMsg[1].flags = (__u16)0;
  261. I2CMsg[1].len = (__u16)readCmdLength;
  262. I2CMsg[1].buf = buf + writeCmdLength;
  263. //read msg
  264. I2CMsg[2].addr = (__u16)I2CSAD;
  265. I2CMsg[2].flags = I2C_M_RD;
  266. I2CMsg[2].len = byteToRead;
  267. I2CMsg[2].buf = buf + writeCmdLength + readCmdLength;
  268. memcpy(buf, writeCmd1, writeCmdLength);
  269. memcpy(buf + writeCmdLength, readCmd1, readCmdLength);
  270. if (client == NULL)
  271. return ERROR_I2C_O;
  272. while (retry < I2C_RETRY && ret < OK) {
  273. ret = i2c_transfer(client->adapter, I2CMsg, 3);
  274. retry++;
  275. if (ret < OK)
  276. msleep(I2C_WAIT_BEFORE_RETRY);
  277. }
  278. if (ret < 0) {
  279. logError(1, "%s %s: ERROR %02X\n",
  280. tag, __func__, ERROR_I2C_WR);
  281. return ERROR_I2C_WR;
  282. }
  283. memcpy(outBuf, buf + writeCmdLength + readCmdLength, byteToRead);
  284. return OK;
  285. }
  286. int readCmdU16(u8 cmd, u16 address, u8 *outBuf, int byteToRead,
  287. int hasDummyByte)
  288. {
  289. int remaining = byteToRead;
  290. int toRead = 0;
  291. u8 rCmd[3] = { cmd, 0x00, 0x00 };
  292. u8 *buff = (u8 *)kmalloc_array(READ_CHUNK + 1, sizeof(u8), GFP_KERNEL);
  293. if (buff == NULL) {
  294. logError(1, "%s %s: ERROR %02X\n", tag, __func__, ERROR_ALLOC);
  295. return ERROR_ALLOC;
  296. }
  297. while (remaining > 0) {
  298. if (remaining >= READ_CHUNK) {
  299. toRead = READ_CHUNK;
  300. remaining -= READ_CHUNK;
  301. } else {
  302. toRead = remaining;
  303. remaining = 0;
  304. }
  305. rCmd[1] = (u8)((address & 0xFF00) >> 8);
  306. rCmd[2] = (u8)(address & 0xFF);
  307. if (hasDummyByte) {
  308. if (fts_readCmd(rCmd, 3, buff, toRead + 1) < 0) {
  309. logError(1, "%s %s: ERROR %02X\n",
  310. tag, __func__, ERROR_I2C_R);
  311. kfree(buff);
  312. return ERROR_I2C_R;
  313. }
  314. memcpy(outBuf, buff + 1, toRead);
  315. } else {
  316. if (fts_readCmd(rCmd, 3, buff, toRead) < 0)
  317. return ERROR_I2C_R;
  318. memcpy(outBuf, buff, toRead);
  319. }
  320. address += toRead;
  321. outBuf += toRead;
  322. }
  323. kfree(buff);
  324. return OK;
  325. }
  326. int writeCmdU16(u8 WriteCmd, u16 address, u8 *dataToWrite, int byteToWrite)
  327. {
  328. int remaining = byteToWrite;
  329. int toWrite = 0;
  330. u8 *buff = (u8 *)kmalloc_array(WRITE_CHUNK + 3, sizeof(u8), GFP_KERNEL);
  331. if (buff == NULL) {
  332. logError(1, "%s %s: ERROR %02X\n", tag, __func__, ERROR_ALLOC);
  333. return ERROR_ALLOC;
  334. }
  335. buff[0] = WriteCmd;
  336. while (remaining > 0) {
  337. if (remaining >= WRITE_CHUNK) {
  338. toWrite = WRITE_CHUNK;
  339. remaining -= WRITE_CHUNK;
  340. } else {
  341. toWrite = remaining;
  342. remaining = 0;
  343. }
  344. buff[1] = (u8)((address & 0xFF00) >> 8);
  345. buff[2] = (u8)(address & 0xFF);
  346. memcpy(buff + 3, dataToWrite, toWrite);
  347. if (fts_writeCmd(buff, 3 + toWrite) < 0) {
  348. logError(1, "%s %s: ERROR %02\n",
  349. tag, __func__, ERROR_I2C_W);
  350. kfree(buff);
  351. return ERROR_I2C_W;
  352. }
  353. address += toWrite;
  354. dataToWrite += toWrite;
  355. }
  356. kfree(buff);
  357. return OK;
  358. }
  359. int writeCmdU32(u8 writeCmd1, u8 writeCmd2, u32 address, u8 *dataToWrite,
  360. int byteToWrite)
  361. {
  362. int remaining = byteToWrite;
  363. int toWrite = 0;
  364. int ret;
  365. u8 buff1[3] = { writeCmd1, 0x00, 0x00 };
  366. u8 *buff2 = (u8 *)kmalloc_array(WRITE_CHUNK + 3,
  367. sizeof(u8), GFP_KERNEL);
  368. if (buff2 == NULL) {
  369. logError(1, "%s %s: ERROR %02X\n",
  370. tag, __func__, ERROR_ALLOC);
  371. return ERROR_ALLOC;
  372. }
  373. buff2[0] = writeCmd2;
  374. while (remaining > 0) {
  375. if (remaining >= WRITE_CHUNK) {
  376. toWrite = WRITE_CHUNK;
  377. remaining -= WRITE_CHUNK;
  378. } else {
  379. toWrite = remaining;
  380. remaining = 0;
  381. }
  382. buff1[1] = (u8)((address & 0xFF000000) >> 24);
  383. buff1[2] = (u8)((address & 0x00FF0000) >> 16);
  384. buff2[1] = (u8)((address & 0x0000FF00) >> 8);
  385. buff2[2] = (u8)(address & 0xFF);
  386. memcpy(buff2 + 3, dataToWrite, toWrite);
  387. if (fts_writeCmd(buff1, 3) < 0) {
  388. logError(1, "%s %s: ERROR %02X\n",
  389. tag, __func__, ERROR_I2C_W);
  390. ret = ERROR_I2C_W;
  391. goto END;
  392. }
  393. if (fts_writeCmd(buff2, 3 + toWrite) < 0) {
  394. logError(1, "%s %s: ERROR %02X\n",
  395. tag, __func__, ERROR_I2C_W);
  396. ret = ERROR_I2C_W;
  397. goto END;
  398. }
  399. address += toWrite;
  400. dataToWrite += toWrite;
  401. }
  402. ret = OK;
  403. END:
  404. kfree(buff2);
  405. return ret;
  406. }
  407. int writeReadCmdU32(u8 wCmd, u8 rCmd, u32 address, u8 *outBuf,
  408. int byteToRead, int hasDummyByte)
  409. {
  410. int remaining = byteToRead;
  411. int toRead = 0;
  412. u8 reaCmd[3];
  413. u8 wriCmd[3];
  414. u8 *buff = (u8 *)kmalloc_array(READ_CHUNK + 1, sizeof(u8), GFP_KERNEL);
  415. if (buff == NULL) {
  416. logError(1, "%s writereadCmd32: ERROR %02X\n",
  417. tag, ERROR_ALLOC);
  418. return ERROR_ALLOC;
  419. }
  420. reaCmd[0] = rCmd;
  421. wriCmd[0] = wCmd;
  422. while (remaining > 0) {
  423. if (remaining >= READ_CHUNK) {
  424. toRead = READ_CHUNK;
  425. remaining -= READ_CHUNK;
  426. } else {
  427. toRead = remaining;
  428. remaining = 0;
  429. }
  430. wriCmd[1] = (u8)((address & 0xFF000000) >> 24);
  431. wriCmd[2] = (u8)((address & 0x00FF0000) >> 16);
  432. reaCmd[1] = (u8)((address & 0x0000FF00) >> 8);
  433. reaCmd[2] = (u8)(address & 0x000000FF);
  434. if (hasDummyByte) {
  435. if (writeReadCmd(wriCmd, 3, reaCmd, 3,
  436. buff, toRead + 1) < 0) {
  437. logError(1, "%s writeCmdU32: ERROR %02X\n",
  438. tag, ERROR_I2C_WR);
  439. kfree(buff);
  440. return ERROR_I2C_WR;
  441. }
  442. memcpy(outBuf, buff + 1, toRead);
  443. } else {
  444. if (writeReadCmd(wriCmd, 3, reaCmd,
  445. 3, buff, toRead) < 0)
  446. return ERROR_I2C_WR;
  447. memcpy(outBuf, buff, toRead);
  448. }
  449. address += toRead;
  450. outBuf += toRead;
  451. }
  452. kfree(buff);
  453. return OK;
  454. }