goodix_tool.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /* drivers/input/touchscreen/goodix_tool.c
  2. *
  3. * 2010 - 2012 Goodix Technology.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be a reference
  11. * to you, when you are integrating the GOODiX's CTP IC into your system,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * Version:1.2
  17. * V1.0:2012/05/01,create file.
  18. * V1.2:2012/10/17,reset_guitar etc.
  19. *
  20. */
  21. #include "tpd.h"
  22. #include <linux/interrupt.h>
  23. #include <linux/i2c.h>
  24. #include <linux/sched.h>
  25. #include <linux/kthread.h>
  26. #include <linux/rtpm_prio.h>
  27. #include <linux/wait.h>
  28. #include <linux/time.h>
  29. #include <linux/delay.h>
  30. #include <linux/uaccess.h>
  31. #include "tpd_custom_gt9xx.h"
  32. #include <linux/device.h>
  33. #include <linux/proc_fs.h> /*proc */
  34. #pragma pack(1)
  35. struct st_cmd_head {
  36. u8 wr; /* write read flag£¬0:R 1:W 2:PID 3: */
  37. u8 flag; /* 0:no need flag/int 1: need flag 2:need int */
  38. u8 flag_addr[2]; /* flag address */
  39. u8 flag_val; /* flag val */
  40. u8 flag_relation; /* flag_val:flag 0:not equal 1:equal 2:> 3:< */
  41. u16 circle; /* polling cycle */
  42. u8 times; /* plling times */
  43. u8 retry; /* I2C retry times */
  44. u16 delay; /* delay before read or after write */
  45. u16 data_len; /* data length */
  46. u8 addr_len; /* address length */
  47. u8 addr[2]; /* address */
  48. u8 res[3]; /* reserved */
  49. u8 *data; /* data pointer */
  50. };
  51. #pragma pack()
  52. struct st_cmd_head cmd_head;
  53. #define UPDATE_FUNCTIONS
  54. #define DATA_LENGTH_UINT 512
  55. #define CMD_HEAD_LENGTH (sizeof(struct st_cmd_head) - sizeof(u8 *))
  56. #define GOODIX_ENTRY_NAME "goodix_tool"
  57. static char procname[20] = { 0 };
  58. static struct i2c_client *gt_client;
  59. #if 0
  60. static struct proc_dir_entry *goodix_proc_entry;
  61. #endif
  62. static s32 goodix_tool_write(struct file *filp, const char __user *buff,
  63. unsigned long len, void *data);
  64. static s32 goodix_tool_read(char *page, char **start, off_t off, int count, int *eof, void *data);
  65. static s32 (*tool_i2c_read)(u8 *, u16);
  66. static s32 (*tool_i2c_write)(u8 *, u16);
  67. s32 DATA_LENGTH = 0;
  68. s8 IC_TYPE[16] = "GT9XX";
  69. static void tool_set_proc_name(char *procname)
  70. {
  71. sprintf(procname, "gmnode");
  72. }
  73. static ssize_t goodix_tool_upper_read(struct file *file, char __user *buffer,
  74. size_t count, loff_t *ppos)
  75. {
  76. return goodix_tool_read(buffer, NULL, 0, count, NULL, ppos);
  77. }
  78. static ssize_t goodix_tool_upper_write(struct file *file,
  79. const char __user *buffer, size_t count, loff_t *ppos)
  80. {
  81. return goodix_tool_write(file, buffer, count, ppos);
  82. }
  83. static const struct file_operations gt_tool_fops = {
  84. .write = goodix_tool_upper_write,
  85. .read = goodix_tool_upper_read
  86. };
  87. static s32 tool_i2c_read_no_extra(u8 *buf, u16 len)
  88. {
  89. s32 ret = -1;
  90. ret = gtp_i2c_read(gt_client, buf, len + GTP_ADDR_LENGTH);
  91. return ret;
  92. }
  93. static s32 tool_i2c_write_no_extra(u8 *buf, u16 len)
  94. {
  95. s32 ret = -1;
  96. ret = gtp_i2c_write(gt_client, buf, len);
  97. return ret;
  98. }
  99. static s32 tool_i2c_read_with_extra(u8 *buf, u16 len)
  100. {
  101. s32 ret = -1;
  102. u8 pre[2] = { 0x0f, 0xff };
  103. u8 end[2] = { 0x80, 0x00 };
  104. tool_i2c_write_no_extra(pre, 2);
  105. ret = tool_i2c_read_no_extra(buf, len);
  106. tool_i2c_write_no_extra(end, 2);
  107. return ret;
  108. }
  109. static s32 tool_i2c_write_with_extra(u8 *buf, u16 len)
  110. {
  111. s32 ret = -1;
  112. u8 pre[2] = { 0x0f, 0xff };
  113. u8 end[2] = { 0x80, 0x00 };
  114. tool_i2c_write_no_extra(pre, 2);
  115. ret = tool_i2c_write_no_extra(buf, len);
  116. tool_i2c_write_no_extra(end, 2);
  117. return ret;
  118. }
  119. static void register_i2c_func(void)
  120. {
  121. /* if (!strncmp(IC_TYPE, "GT818", 5) || !strncmp(IC_TYPE, "GT816", 5) */
  122. /* || !strncmp(IC_TYPE, "GT811", 5) || !strncmp(IC_TYPE, "GT818F", 6) */
  123. /* || !strncmp(IC_TYPE, "GT827", 5) || !strncmp(IC_TYPE,"GT828", 5) */
  124. /* || !strncmp(IC_TYPE, "GT813", 5)) */
  125. if (strncmp(IC_TYPE, "GT8110", 6) && strncmp(IC_TYPE, "GT8105", 6)
  126. && strncmp(IC_TYPE, "GT801", 5) && strncmp(IC_TYPE, "GT800", 5)
  127. && strncmp(IC_TYPE, "GT801PLUS", 9) && strncmp(IC_TYPE, "GT811", 5)
  128. && strncmp(IC_TYPE, "GTxxx", 5) && strncmp(IC_TYPE, "GT9XX", 5)) {
  129. tool_i2c_read = tool_i2c_read_with_extra;
  130. tool_i2c_write = tool_i2c_write_with_extra;
  131. GTP_DEBUG("I2C function: with pre and end cmd!");
  132. } else {
  133. tool_i2c_read = tool_i2c_read_no_extra;
  134. tool_i2c_write = tool_i2c_write_no_extra;
  135. GTP_INFO("I2C function: without pre and end cmd!");
  136. }
  137. }
  138. static void unregister_i2c_func(void)
  139. {
  140. tool_i2c_read = NULL;
  141. tool_i2c_write = NULL;
  142. GTP_INFO("I2C function: unregister i2c transfer function!");
  143. }
  144. s32 init_wr_node(struct i2c_client *client)
  145. {
  146. s32 i;
  147. gt_client = i2c_client_point;
  148. memset(&cmd_head, 0, sizeof(cmd_head));
  149. cmd_head.data = NULL;
  150. i = 5;
  151. while ((!cmd_head.data) && i) {
  152. cmd_head.data = kzalloc(i * DATA_LENGTH_UINT, GFP_KERNEL);
  153. if (NULL != cmd_head.data)
  154. break;
  155. i--;
  156. }
  157. if (i) {
  158. DATA_LENGTH = i * DATA_LENGTH_UINT + GTP_ADDR_LENGTH;
  159. GTP_INFO("Applied memory size:%d.", DATA_LENGTH);
  160. } else {
  161. GTP_ERROR("Apply for memory failed.");
  162. return FAIL;
  163. }
  164. cmd_head.addr_len = 2;
  165. cmd_head.retry = 5;
  166. register_i2c_func();
  167. tool_set_proc_name(procname);
  168. if (proc_create(procname, 0660, NULL, &gt_tool_fops) == NULL) {
  169. GTP_ERROR("create_proc_entry %s failed", procname);
  170. return -1;
  171. }
  172. return SUCCESS;
  173. }
  174. void uninit_wr_node(void)
  175. {
  176. kfree(cmd_head.data);
  177. cmd_head.data = NULL;
  178. unregister_i2c_func();
  179. remove_proc_entry(procname, NULL);
  180. }
  181. static u8 relation(u8 src, u8 dst, u8 rlt)
  182. {
  183. u8 ret = 0;
  184. switch (rlt) {
  185. case 0:
  186. ret = (src != dst) ? true : false;
  187. break;
  188. case 1:
  189. ret = (src == dst) ? true : false;
  190. GTP_DEBUG("equal:src:0x%02x dst:0x%02x ret:%d.", src, dst, (s32) ret);
  191. break;
  192. case 2:
  193. ret = (src > dst) ? true : false;
  194. break;
  195. case 3:
  196. ret = (src < dst) ? true : false;
  197. break;
  198. case 4:
  199. ret = (src & dst) ? true : false;
  200. break;
  201. case 5:
  202. ret = (!(src | dst)) ? true : false;
  203. break;
  204. default:
  205. ret = false;
  206. break;
  207. }
  208. return ret;
  209. }
  210. /*******************************************************
  211. Function:
  212. Comfirm function.
  213. Input:
  214. None.
  215. Output:
  216. Return write length.
  217. ********************************************************/
  218. static u8 comfirm(void)
  219. {
  220. s32 i = 0;
  221. u8 buf[32];
  222. /* memcpy(&buf[GTP_ADDR_LENGTH - cmd_head.addr_len], &cmd_head.flag_addr, cmd_head.addr_len); */
  223. /* memcpy(buf, &cmd_head.flag_addr, cmd_head.addr_len);//Modified by Scott, 2012-02-17 */
  224. memcpy(buf, cmd_head.flag_addr, cmd_head.addr_len);
  225. for (i = 0; i < cmd_head.times; i++) {
  226. if (tool_i2c_read(buf, 1) <= 0) {
  227. GTP_ERROR("Read flag data failed!");
  228. return FAIL;
  229. }
  230. if (true ==
  231. relation(buf[GTP_ADDR_LENGTH], cmd_head.flag_val, cmd_head.flag_relation)) {
  232. GTP_DEBUG("value at flag addr:0x%02x.", buf[GTP_ADDR_LENGTH]);
  233. GTP_DEBUG("flag value:0x%02x.", cmd_head.flag_val);
  234. break;
  235. }
  236. msleep(cmd_head.circle);
  237. }
  238. if (i >= cmd_head.times) {
  239. GTP_ERROR("Didn't get the flag to continue!");
  240. return FAIL;
  241. }
  242. return SUCCESS;
  243. }
  244. /*******************************************************
  245. Function:
  246. Goodix tool write function.
  247. Input:
  248. standard proc write function param.
  249. Output:
  250. Return write length.
  251. ********************************************************/
  252. static s32 goodix_tool_write(struct file *filp, const char __user *buff,
  253. unsigned long len, void *data)
  254. {
  255. u64 ret = 0;
  256. GTP_DEBUG_FUNC();
  257. GTP_DEBUG_ARRAY((u8 *) buff, len);
  258. ret = copy_from_user(&cmd_head, buff, CMD_HEAD_LENGTH);
  259. if (ret)
  260. GTP_ERROR("copy_from_user failed.");
  261. GTP_DEBUG("wr :0x%02x.", cmd_head.wr);
  262. GTP_DEBUG("flag:0x%02x.", cmd_head.flag);
  263. GTP_DEBUG("flag addr:0x%02x%02x.", cmd_head.flag_addr[0], cmd_head.flag_addr[1]);
  264. GTP_DEBUG("flag val:0x%02x.", cmd_head.flag_val);
  265. GTP_DEBUG("flag rel:0x%02x.", cmd_head.flag_relation);
  266. GTP_DEBUG("circle :%d.", (s32) cmd_head.circle);
  267. GTP_DEBUG("times :%d.", (s32) cmd_head.times);
  268. GTP_DEBUG("retry :%d.", (s32) cmd_head.retry);
  269. GTP_DEBUG("delay :%d.", (s32) cmd_head.delay);
  270. GTP_DEBUG("data len:%d.", (s32) cmd_head.data_len);
  271. GTP_DEBUG("addr len:%d.", (s32) cmd_head.addr_len);
  272. GTP_DEBUG("addr:0x%02x%02x.", cmd_head.addr[0], cmd_head.addr[1]);
  273. GTP_DEBUG("len:%d.", (s32) len);
  274. GTP_DEBUG("buf[20]:0x%02x.", buff[CMD_HEAD_LENGTH]);
  275. if (1 == cmd_head.wr) {
  276. /* copy_from_user(&cmd_head.data[cmd_head.addr_len], &buff[CMD_HEAD_LENGTH], cmd_head.data_len); */
  277. ret =
  278. copy_from_user(&cmd_head.data[GTP_ADDR_LENGTH],
  279. &buff[CMD_HEAD_LENGTH], cmd_head.data_len);
  280. if (ret)
  281. GTP_ERROR("copy_from_user failed.");
  282. memcpy(&cmd_head.data[GTP_ADDR_LENGTH - cmd_head.addr_len],
  283. cmd_head.addr, cmd_head.addr_len);
  284. GTP_DEBUG_ARRAY(cmd_head.data, cmd_head.data_len + cmd_head.addr_len);
  285. GTP_DEBUG_ARRAY((u8 *) &buff[CMD_HEAD_LENGTH], cmd_head.data_len);
  286. if (1 == cmd_head.flag) {
  287. if (FAIL == comfirm()) {
  288. GTP_ERROR("[WRITE]Comfirm fail!");
  289. return FAIL;
  290. }
  291. } else if (2 == cmd_head.flag) {
  292. /* Need interrupt! */
  293. }
  294. if (tool_i2c_write
  295. (&cmd_head.data[GTP_ADDR_LENGTH - cmd_head.addr_len],
  296. cmd_head.data_len + cmd_head.addr_len) <= 0) {
  297. GTP_ERROR("[WRITE]Write data failed!");
  298. return FAIL;
  299. }
  300. GTP_DEBUG_ARRAY(&cmd_head.data[GTP_ADDR_LENGTH - cmd_head.addr_len],
  301. cmd_head.data_len + cmd_head.addr_len);
  302. if (cmd_head.delay)
  303. msleep(cmd_head.delay);
  304. return cmd_head.data_len + CMD_HEAD_LENGTH;
  305. } else if (3 == cmd_head.wr) {
  306. memcpy(IC_TYPE, cmd_head.data, cmd_head.data_len);
  307. register_i2c_func();
  308. return cmd_head.data_len + CMD_HEAD_LENGTH;
  309. } else if (5 == cmd_head.wr) {
  310. /* memcpy(IC_TYPE, cmd_head.data, cmd_head.data_len); */
  311. return cmd_head.data_len + CMD_HEAD_LENGTH;
  312. } else if (7 == cmd_head.wr) {
  313. /* mt_eint_mask(CUST_EINT_TOUCH_PANEL_NUM); */
  314. disable_irq(touch_irq);
  315. #if GTP_ESD_PROTECT
  316. gtp_esd_switch(i2c_client_point, SWITCH_OFF);
  317. #endif
  318. return CMD_HEAD_LENGTH;
  319. } else if (9 == cmd_head.wr) {
  320. /* mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM); */
  321. enable_irq(touch_irq);
  322. #if GTP_ESD_PROTECT
  323. gtp_esd_switch(i2c_client_point, SWITCH_ON);
  324. #endif
  325. return CMD_HEAD_LENGTH;
  326. } else if (17 == cmd_head.wr) {
  327. ret =
  328. copy_from_user(&cmd_head.data[GTP_ADDR_LENGTH],
  329. &buff[CMD_HEAD_LENGTH], cmd_head.data_len);
  330. if (ret)
  331. GTP_DEBUG("copy_from_user failed.");
  332. if (cmd_head.data[GTP_ADDR_LENGTH]) {
  333. GTP_DEBUG("gtp enter rawdiff.");
  334. gtp_rawdiff_mode = true;
  335. } else {
  336. gtp_rawdiff_mode = false;
  337. GTP_DEBUG("gtp leave rawdiff.");
  338. }
  339. return CMD_HEAD_LENGTH;
  340. }
  341. #ifdef UPDATE_FUNCTIONS
  342. else if (11 == cmd_head.wr) { /* Enter update mode! */
  343. if (FAIL == gup_enter_update_mode(gt_client))
  344. return FAIL;
  345. } else if (13 == cmd_head.wr) {
  346. gup_leave_update_mode();
  347. } else if (15 == cmd_head.wr) {
  348. show_len = 0;
  349. total_len = 0;
  350. if ((cmd_head.data == NULL)
  351. || (cmd_head.data_len >= DATA_LENGTH)
  352. || (cmd_head.data_len >= (len - CMD_HEAD_LENGTH))) {
  353. GTP_ERROR("copy_from_user data out of range.");
  354. return -EINVAL;
  355. }
  356. memset(cmd_head.data, 0, cmd_head.data_len + 1);
  357. memcpy(cmd_head.data, &buff[CMD_HEAD_LENGTH], cmd_head.data_len);
  358. GTP_DEBUG("update firmware, filename: %s", cmd_head.data);
  359. if (FAIL == gup_update_proc((void *)cmd_head.data))
  360. return FAIL;
  361. }
  362. #endif
  363. return CMD_HEAD_LENGTH;
  364. }
  365. /*******************************************************
  366. Function:
  367. Goodix tool read function.
  368. Input:
  369. standard proc read function param.
  370. Output:
  371. Return read length.
  372. ********************************************************/
  373. static s32 goodix_tool_read(char *page, char **start, off_t off, int count, int *eof, void *data)
  374. {
  375. GTP_DEBUG_FUNC();
  376. if (cmd_head.wr % 2) {
  377. return FAIL;
  378. } else if (!cmd_head.wr) {
  379. u16 len = 0;
  380. s16 data_len = 0;
  381. u16 loc = 0;
  382. if (1 == cmd_head.flag) {
  383. if (FAIL == comfirm()) {
  384. GTP_ERROR("[READ]Comfirm fail!");
  385. return FAIL;
  386. }
  387. } else if (2 == cmd_head.flag) {
  388. /* Need interrupt! */
  389. }
  390. memcpy(cmd_head.data, cmd_head.addr, cmd_head.addr_len);
  391. GTP_DEBUG("[CMD HEAD DATA] ADDR:0x%02x%02x.", cmd_head.data[0], cmd_head.data[1]);
  392. GTP_DEBUG("[CMD HEAD ADDR] ADDR:0x%02x%02x.", cmd_head.addr[0], cmd_head.addr[1]);
  393. if (cmd_head.delay)
  394. msleep(cmd_head.delay);
  395. data_len = cmd_head.data_len;
  396. while (data_len > 0) {
  397. if (data_len > DATA_LENGTH)
  398. len = DATA_LENGTH;
  399. else
  400. len = data_len;
  401. data_len -= DATA_LENGTH;
  402. if (tool_i2c_read(cmd_head.data, len) <= 0) {
  403. GTP_ERROR("[READ]Read data failed!");
  404. return FAIL;
  405. }
  406. memcpy(&page[loc], &cmd_head.data[GTP_ADDR_LENGTH], len);
  407. loc += len;
  408. GTP_DEBUG_ARRAY(&cmd_head.data[GTP_ADDR_LENGTH], len);
  409. GTP_DEBUG_ARRAY(page, len);
  410. }
  411. } else if (2 == cmd_head.wr) {
  412. /* memcpy(page, "gt8", cmd_head.data_len); */
  413. /* memcpy(page, "GT818", 5); */
  414. /* page[5] = 0; */
  415. GTP_DEBUG("Return ic type:%s len:%d.", page, (s32) cmd_head.data_len);
  416. return cmd_head.data_len;
  417. /* return sizeof(IC_TYPE_NAME); */
  418. } else if (4 == cmd_head.wr) {
  419. page[0] = show_len >> 8;
  420. page[1] = show_len & 0xff;
  421. page[2] = total_len >> 8;
  422. page[3] = total_len & 0xff;
  423. return cmd_head.data_len;
  424. } else if (6 == cmd_head.wr) {
  425. /* Read error code! */
  426. } else if (8 == cmd_head.wr) {
  427. /* memcpy(page, GTP_DRIVER_VERSION, strlen(GTP_DRIVER_VERSION)); */
  428. s32 tmp_len;
  429. tmp_len = strlen(GTP_DRIVER_VERSION);
  430. memcpy(page, GTP_DRIVER_VERSION, tmp_len);
  431. page[tmp_len] = 0;
  432. }
  433. return cmd_head.data_len;
  434. }