ftsFrame.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. * FTS functions for getting frames *
  30. * *
  31. **************************************************************************
  32. **************************************************************************
  33. */
  34. #include <linux/init.h>
  35. #include <linux/errno.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/kernel.h>
  38. #include <linux/module.h>
  39. #include <linux/slab.h>
  40. #include <linux/string.h>
  41. #include <stdarg.h>
  42. #include <linux/input.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/serio.h>
  45. #include <linux/time.h>
  46. #include <linux/pm.h>
  47. #include <linux/delay.h>
  48. #include <linux/ctype.h>
  49. #include <linux/gpio.h>
  50. #include <linux/i2c.h>
  51. #include <linux/i2c-dev.h>
  52. #include <linux/fs.h>
  53. #include <linux/uaccess.h>
  54. #include <linux/power_supply.h>
  55. #include <linux/firmware.h>
  56. #include <linux/regulator/consumer.h>
  57. #include <linux/of_gpio.h>
  58. //#include <linux/sec_sysfs.h>
  59. #include "ftsCrossCompile.h"
  60. #include "ftsCompensation.h"
  61. #include "ftsError.h"
  62. #include "ftsFrame.h"
  63. #include "ftsHardware.h"
  64. #include "ftsIO.h"
  65. #include "ftsSoftware.h"
  66. #include "ftsTool.h"
  67. #include "ftsTime.h"
  68. #include "../fts.h"
  69. static char tag[8] = "[ FTS ]\0";
  70. static int sense_len, force_len;
  71. int getOffsetFrame(u16 address, u16 *offset)
  72. {
  73. u8 data[2];
  74. u8 cmd = { FTS_CMD_FRAMEBUFFER_R };
  75. char *temp = NULL;
  76. if (readCmdU16(cmd, address, data, OFFSET_LENGTH,
  77. DUMMY_FRAMEBUFFER) < 0) {
  78. logError(1, "%s %S: ERROR %02X\n", tag, __func__, ERROR_I2C_R);
  79. return ERROR_I2C_R;
  80. }
  81. u8ToU16(data, offset);
  82. temp = printHex("Offest = ", data, OFFSET_LENGTH);
  83. if (temp != NULL)
  84. logError(0, "%s %s", tag, temp);
  85. kfree(temp);
  86. return OK;
  87. }
  88. int getChannelsLength(void)
  89. {
  90. int ret;
  91. u8 *data = (u8 *)kmalloc_array(2, sizeof(u8), GFP_KERNEL);
  92. if (data == NULL) {
  93. logError(1, "%s %s: ERROR %02X\n",
  94. tag, __func__, ERROR_ALLOC);
  95. return ERROR_ALLOC;
  96. }
  97. ret = readB2(ADDR_SENSE_LEN, data, 2);
  98. if (ret < OK) {
  99. logError(1, "%s %s: ERROR %02X\n",
  100. tag, __func__, ERROR_READ_B2);
  101. kfree(data);
  102. return (ret|ERROR_READ_B2);
  103. }
  104. sense_len = (int)data[0];
  105. force_len = (int)data[1];
  106. logError(0, "%s Force_len = %d Sense_Len = %d\n",
  107. tag, force_len, sense_len);
  108. kfree(data);
  109. return OK;
  110. }
  111. int getFrameData(u16 address, int size, short **frame)
  112. {
  113. int i, j, ret;
  114. u8 *data = (u8 *)kmalloc_array(size, sizeof(u8), GFP_KERNEL);
  115. if (data == NULL) {
  116. logError(1, "%s %s: ERROR %02X\n",
  117. tag, __func__, ERROR_ALLOC);
  118. return ERROR_ALLOC;
  119. }
  120. ret = readCmdU16(FTS_CMD_FRAMEBUFFER_R, address,
  121. data, size, DUMMY_FRAMEBUFFER);
  122. if (ret < OK) {
  123. logError(1, "%s %s: ERROR %02X\n",
  124. tag, __func__, ERROR_I2C_R);
  125. kfree(data);
  126. return ERROR_I2C_R;
  127. }
  128. j = 0;
  129. for (i = 0; i < size; i += 2) {
  130. (*frame)[j] = (short)((data[i + 1] << 8) + data[i]);
  131. j++;
  132. }
  133. kfree(data);
  134. return OK;
  135. }
  136. int getMSFrame(u16 type, struct MutualSenseFrame *frame, int keep_first_row)
  137. {
  138. u16 offset;
  139. int ret;
  140. if (getSenseLen() == 0 || getForceLen() == 0) {
  141. ret = getChannelsLength();
  142. if (ret < OK) {
  143. logError(1, "%s %s: ERROR %02X\n",
  144. tag, __func__, ERROR_CH_LEN);
  145. return (ret|ERROR_CH_LEN);
  146. }
  147. }
  148. ret = getOffsetFrame(type, &offset);
  149. if (ret < OK) {
  150. logError(1, "%s %s: ERROR %02X\n",
  151. tag, __func__, ERROR_GET_OFFSET);
  152. return (ret | ERROR_GET_OFFSET);
  153. }
  154. switch (type) {
  155. case ADDR_RAW_TOUCH:
  156. case ADDR_FILTER_TOUCH:
  157. case ADDR_NORM_TOUCH:
  158. case ADDR_CALIB_TOUCH:
  159. if (keep_first_row == 1) {
  160. frame->node_data_size = ((force_len + 1) * sense_len);
  161. frame->header.force_node = force_len + 1;
  162. } else {
  163. frame->node_data_size = ((force_len) * sense_len);
  164. offset += (sense_len * BYTES_PER_NODE);
  165. frame->header.force_node = force_len;
  166. }
  167. frame->header.sense_node = sense_len;
  168. break;
  169. case ADDR_NORM_MS_KEY:
  170. case ADDR_RAW_MS_KEY:
  171. frame->header.force_node = 1;
  172. frame->header.sense_node = ftsInfo.u8_msKeyLen;
  173. frame->node_data_size = ftsInfo.u8_msKeyLen;
  174. break;
  175. default:
  176. logError(1, "%s %s: ERROR % 02X\n",
  177. tag, __func__, ERROR_OP_NOT_ALLOW);
  178. return ERROR_OP_NOT_ALLOW;
  179. }
  180. frame->node_data = (short *)kmalloc_array(frame->node_data_size,
  181. sizeof(short), GFP_KERNEL);
  182. if (frame->node_data == NULL) {
  183. logError(1, "%s %s: ERROR %02X\n",
  184. tag, __func__, ERROR_ALLOC);
  185. return ERROR_ALLOC;
  186. }
  187. ret = getFrameData(offset,
  188. frame->node_data_size * BYTES_PER_NODE,
  189. &(frame->node_data));
  190. if (ret < OK) {
  191. logError(1, "%s %s: ERROR %02X\n",
  192. tag, __func__, ERROR_GET_FRAME_DATA);
  193. kfree(frame->node_data);
  194. return (ret | ERROR_GET_FRAME_DATA);
  195. }
  196. // if you want to access one node i,j,
  197. //you should compute the offset like:
  198. //offset = i * columns + j => frame[i, j]
  199. logError(0, "%s Frame acquired!\n", tag);
  200. //return the number of data put inside frame
  201. return frame->node_data_size;
  202. }
  203. int getSenseLen(void)
  204. {
  205. int ret;
  206. if (sense_len != 0)
  207. return sense_len;
  208. if (ftsInfo.u8_scrSenseLen != 0) {
  209. sense_len = ftsInfo.u8_scrSenseLen;
  210. } else {
  211. ret = getChannelsLength();
  212. if (ret < OK)
  213. return ret;
  214. }
  215. return sense_len;
  216. }
  217. int getForceLen(void)
  218. {
  219. int ret;
  220. if (force_len != 0)
  221. return force_len;
  222. if (ftsInfo.u8_scrForceLen != 0) {
  223. force_len = ftsInfo.u8_scrForceLen;
  224. } else {
  225. ret = getChannelsLength();
  226. if (ret < OK)
  227. return ret;
  228. }
  229. return force_len;
  230. }
  231. int requestFrame(u16 type)
  232. {
  233. int retry = 0;
  234. int ret;
  235. u16 answer;
  236. char *temp = NULL;
  237. int event_to_search[1];
  238. u8 readEvent[FIFO_EVENT_SIZE];
  239. u8 cmd[3] = { FTS_CMD_REQU_FRAME_DATA, 0x00, 0x00};
  240. // B7 is the command for asking frame data
  241. event_to_search[0] = (int)EVENTID_FRAME_DATA_READ;
  242. u16ToU8(type, &cmd[1]);
  243. while (retry < FRAME_DATA_READ_RETRY) {
  244. temp = printHex("Command = ", cmd, 3);
  245. if (temp != NULL)
  246. logError(0, "%s %s", tag, temp);
  247. kfree(temp);
  248. //send the request to the chip to load in memory the Frame Data
  249. ret = fts_writeFwCmd(cmd, 3);
  250. if (ret < OK) {
  251. logError(1, "%s %s: ERROR %02X\n",
  252. tag, __func__, ERROR_I2C_W);
  253. return ERROR_I2C_W;
  254. }
  255. ret = pollForEvent(event_to_search,
  256. 1,
  257. readEvent,
  258. TIMEOUT_REQU_COMP_DATA);
  259. if (ret < OK) {
  260. logError(0, "%s Event did not Found at %d attemp!\n",
  261. tag, retry + 1);
  262. retry += 1;
  263. } else {
  264. retry = 0;
  265. break;
  266. }
  267. }
  268. if (retry == FRAME_DATA_READ_RETRY) {
  269. logError(1, "%s %s: ERROR %02X\n",
  270. tag, __func__, ERROR_TIMEOUT);
  271. return ERROR_TIMEOUT;
  272. }
  273. u8ToU16_le(&readEvent[1], &answer);
  274. if (answer == type)
  275. return OK;
  276. logError(1, "%s The event found has a different type of ", tag);
  277. logError(1, "Frame data:%02X\n", ERROR_DIFF_COMP_TYPE);
  278. return ERROR_DIFF_COMP_TYPE;
  279. }
  280. int readFrameDataHeader(u16 type, struct DataHeader *header)
  281. {
  282. u16 offset = ADDR_FRAMEBUFFER_DATA;
  283. u16 answer;
  284. u8 data[FRAME_DATA_HEADER];
  285. if (readCmdU16(FTS_CMD_FRAMEBUFFER_R, offset, data,
  286. FRAME_DATA_HEADER, DUMMY_FRAMEBUFFER) < 0) {
  287. logError(1, "%s %s: ERROR %02X\n",
  288. tag, __func__, ERROR_I2C_R);
  289. return ERROR_I2C_R;
  290. }
  291. logError(0, "%s Read Data Header done!\n", tag);
  292. if (data[0] != FRAME_HEADER_SIGNATURE) {
  293. logError(1, "%s %s %02X Wrong Header Signature !%02X != %02X\n",
  294. tag, __func__, ERROR_WRONG_COMP_SIGN, data[0],
  295. HEADER_SIGNATURE);
  296. return ERROR_WRONG_COMP_SIGN;
  297. }
  298. u8ToU16_le(&data[1], &answer);
  299. if (answer != type) {
  300. logError(1, "%s %s: ERROR %02X\n",
  301. tag, __func__, ERROR_DIFF_COMP_TYPE);
  302. return ERROR_DIFF_COMP_TYPE;
  303. }
  304. logError(0, "%s Type of Frame data OK!\n", tag);
  305. header->type = type;
  306. header->force_node = (int)data[4];
  307. header->sense_node = (int)data[5];
  308. return OK;
  309. }
  310. int getMSFrame2(u16 type, struct MutualSenseFrame *frame)
  311. {
  312. u16 offset = ADDR_FRAMEBUFFER_DATA+FRAME_DATA_HEADER;
  313. int size, ret;
  314. frame->node_data = NULL;
  315. if (!(type == MS_TOUCH_ACTIVE || type == MS_TOUCH_LOW_POWER
  316. || type == MS_TOUCH_ULTRA_LOW_POWER
  317. || type == MS_KEY)) {
  318. logError(1, "%s %s:Choose a MS type of frame data ERROR %02X\n",
  319. tag, __func__, ERROR_OP_NOT_ALLOW);
  320. return ERROR_OP_NOT_ALLOW;
  321. }
  322. ret = requestFrame(type);
  323. if (ret < 0) {
  324. logError(1, "%s readMutualSenseCompensation:ERROR %02X\n",
  325. tag, ERROR_REQU_COMP_DATA);
  326. return (ret | ERROR_REQU_COMP_DATA);
  327. }
  328. ret = readFrameDataHeader(type, &(frame->header));
  329. if (ret < 0) {
  330. logError(1, "%s readMutualSenseCompensationData:ERROR %02X\n",
  331. tag, ERROR_COMP_DATA_HEADER);
  332. return (ret | ERROR_COMP_DATA_HEADER);
  333. }
  334. switch (type) {
  335. case MS_TOUCH_ACTIVE:
  336. case MS_TOUCH_LOW_POWER:
  337. case MS_TOUCH_ULTRA_LOW_POWER:
  338. size = frame->header.force_node * frame->header.sense_node;
  339. break;
  340. case MS_KEY:
  341. //or use directly the number in the ftsChip
  342. if (frame->header.force_node > frame->header.sense_node)
  343. size = frame->header.force_node;
  344. else
  345. size = frame->header.sense_node;
  346. frame->header.force_node = 1;
  347. frame->header.sense_node = size;
  348. break;
  349. default:
  350. logError(1, "%s %s: ERROR % 02X\n",
  351. tag, __func__, ERROR_OP_NOT_ALLOW);
  352. return ERROR_OP_NOT_ALLOW;
  353. }
  354. frame->node_data = (short *)kmalloc_array(size,
  355. sizeof(short), GFP_KERNEL);
  356. if (frame->node_data == NULL) {
  357. logError(1, "%s %s: ERROR %02X\n", tag, __func__, ERROR_ALLOC);
  358. return ERROR_ALLOC;
  359. }
  360. ret = getFrameData(offset, size*BYTES_PER_NODE, &(frame->node_data));
  361. if (ret < OK) {
  362. logError(1, "%s %s: ERROR %02X\n",
  363. tag, __func__, ERROR_GET_FRAME_DATA);
  364. kfree(frame->node_data);
  365. return (ret | ERROR_GET_FRAME_DATA);
  366. }
  367. // if you want to access one node i,j,
  368. //you should compute the offset like:
  369. //offset = i * columns + j = > frame[i, j]
  370. logError(0, "%s Frame acquired!\n", tag);
  371. frame->node_data_size = size;
  372. return size;//return the number of data put inside frame
  373. }
  374. int getSSFrame2(u16 type, struct SelfSenseFrame *frame)
  375. {
  376. u16 offset = ADDR_FRAMEBUFFER_DATA + FRAME_DATA_HEADER;
  377. int size, ret;
  378. short *temp = NULL;
  379. frame->force_data = NULL;
  380. frame->sense_data = NULL;
  381. if (!(type == SS_TOUCH || type == SS_KEY || type == SS_HOVER
  382. || type == SS_PROXIMITY)) {
  383. logError(1, "%s %s:Choose a SS type of frame data ERROR %02X\n",
  384. tag, __func__, ERROR_OP_NOT_ALLOW);
  385. return ERROR_OP_NOT_ALLOW;
  386. }
  387. ret = requestFrame(type);
  388. if (ret < 0) {
  389. logError(1, "%s %s: ERROR %02X\n",
  390. tag, __func__, ERROR_REQU_COMP_DATA);
  391. return (ret | ERROR_REQU_COMP_DATA);
  392. }
  393. ret = readFrameDataHeader(type, &(frame->header));
  394. if (ret < 0) {
  395. logError(1, "%s %s: ERROR %02X\n",
  396. tag, __func__, ERROR_COMP_DATA_HEADER);
  397. return (ret | ERROR_COMP_DATA_HEADER);
  398. }
  399. switch (type) {
  400. case SS_TOUCH:
  401. case SS_HOVER:
  402. case SS_PROXIMITY:
  403. size = frame->header.force_node + frame->header.sense_node;
  404. break;
  405. default:
  406. logError(1, "%s %s: ERROR % 02X\n",
  407. tag, __func__, ERROR_OP_NOT_ALLOW);
  408. return ERROR_OP_NOT_ALLOW;
  409. }
  410. temp = (short *)kmalloc_array(size, sizeof(short), GFP_KERNEL);
  411. if (temp == NULL) {
  412. logError(1, "%s %s: temp ERROR %02X\n",
  413. tag, __func__, ERROR_ALLOC);
  414. return ERROR_ALLOC;
  415. }
  416. ret = getFrameData(offset, size*BYTES_PER_NODE, &temp);
  417. if (ret < OK) {
  418. logError(1, "%s %s: ERROR %02X\n",
  419. tag, __func__, ERROR_GET_FRAME_DATA);
  420. kfree(temp);
  421. return (ret | ERROR_GET_FRAME_DATA);
  422. }
  423. frame->force_data = (short *)kmalloc_array(frame->header.force_node,
  424. sizeof(short), GFP_KERNEL);
  425. if (frame->force_data == NULL) {
  426. logError(1, "%s %s: frame->force_data ERROR %02X\n",
  427. tag, __func__, ERROR_ALLOC);
  428. kfree(temp);
  429. return ERROR_ALLOC;
  430. }
  431. memcpy(frame->force_data, temp,
  432. frame->header.force_node * sizeof(short));
  433. frame->sense_data = (short *)kmalloc_array(frame->header.sense_node,
  434. sizeof(short), GFP_KERNEL);
  435. if (frame->sense_data == NULL) {
  436. logError(1, "%s %s: frame->sense_data ERROR %02X\n",
  437. tag, __func__, ERROR_ALLOC);
  438. kfree(temp);
  439. kfree(frame->force_data);
  440. return ERROR_ALLOC;
  441. }
  442. memcpy(frame->sense_data, &temp[frame->header.force_node],
  443. frame->header.sense_node * sizeof(short));
  444. logError(0, "%s Frame acquired!\n", tag);
  445. kfree(temp);
  446. return size; //return the number of data put inside frame
  447. }