goodix_tool.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 <asm/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, unsigned long len,
  63. 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. char *months[12] = { "Jan", "Feb", "Mar", "Apr", "May",
  72. "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  73. };
  74. char date[20] = { 0 };
  75. char month[4] = { 0 };
  76. int i = 0, n_month = 1, n_day = 0, n_year = 0;
  77. /*sprintf(date, "%s", __DATE__);*/
  78. /* GTP_DEBUG("compile date: %s", date); */
  79. if (sscanf(date, "%s %d %d", month, &n_day, &n_year) == -1)
  80. return;
  81. for (i = 0; i < 12; ++i) {
  82. if (!memcmp(months[i], month, 3)) {
  83. n_month = i + 1;
  84. break;
  85. }
  86. }
  87. sprintf(procname, "gmnode%04d%02d%02d", n_year, n_month, n_day);
  88. /* GTP_DEBUG("procname = %s", procname); */
  89. }
  90. static ssize_t goodix_tool_upper_read(struct file *file, char __user *buffer,
  91. size_t count, loff_t *ppos)
  92. {
  93. return goodix_tool_read(buffer, NULL, 0, count, NULL, ppos);
  94. }
  95. static ssize_t goodix_tool_upper_write(struct file *file, const char __user *buffer,
  96. size_t count, loff_t *ppos)
  97. {
  98. return goodix_tool_write(file, buffer, count, ppos);
  99. }
  100. static const struct file_operations gt_tool_fops = {
  101. .write = goodix_tool_upper_write,
  102. .read = goodix_tool_upper_read
  103. };
  104. static s32 tool_i2c_read_no_extra(u8 *buf, u16 len)
  105. {
  106. s32 ret = -1;
  107. ret = gtp_i2c_read(gt_client, buf, len + GTP_ADDR_LENGTH);
  108. return ret;
  109. }
  110. static s32 tool_i2c_write_no_extra(u8 *buf, u16 len)
  111. {
  112. s32 ret = -1;
  113. ret = gtp_i2c_write(gt_client, buf, len);
  114. return ret;
  115. }
  116. static s32 tool_i2c_read_with_extra(u8 *buf, u16 len)
  117. {
  118. s32 ret = -1;
  119. u8 pre[2] = { 0x0f, 0xff };
  120. u8 end[2] = { 0x80, 0x00 };
  121. tool_i2c_write_no_extra(pre, 2);
  122. ret = tool_i2c_read_no_extra(buf, len);
  123. tool_i2c_write_no_extra(end, 2);
  124. return ret;
  125. }
  126. static s32 tool_i2c_write_with_extra(u8 *buf, u16 len)
  127. {
  128. s32 ret = -1;
  129. u8 pre[2] = { 0x0f, 0xff };
  130. u8 end[2] = { 0x80, 0x00 };
  131. tool_i2c_write_no_extra(pre, 2);
  132. ret = tool_i2c_write_no_extra(buf, len);
  133. tool_i2c_write_no_extra(end, 2);
  134. return ret;
  135. }
  136. static void register_i2c_func(void)
  137. {
  138. /* if (!strncmp(IC_TYPE, "GT818", 5) || !strncmp(IC_TYPE, "GT816", 5) */
  139. /* || !strncmp(IC_TYPE, "GT811", 5) || !strncmp(IC_TYPE, "GT818F", 6) */
  140. /* || !strncmp(IC_TYPE, "GT827", 5) || !strncmp(IC_TYPE,"GT828", 5) */
  141. /* || !strncmp(IC_TYPE, "GT813", 5)) */
  142. if (strncmp(IC_TYPE, "GT8110", 6) && strncmp(IC_TYPE, "GT8105", 6)
  143. && strncmp(IC_TYPE, "GT801", 5) && strncmp(IC_TYPE, "GT800", 5)
  144. && strncmp(IC_TYPE, "GT801PLUS", 9) && strncmp(IC_TYPE, "GT811", 5)
  145. && strncmp(IC_TYPE, "GTxxx", 5) && strncmp(IC_TYPE, "GT9XX", 5)) {
  146. tool_i2c_read = tool_i2c_read_with_extra;
  147. tool_i2c_write = tool_i2c_write_with_extra;
  148. GTP_DEBUG("I2C function: with pre and end cmd!");
  149. } else {
  150. tool_i2c_read = tool_i2c_read_no_extra;
  151. tool_i2c_write = tool_i2c_write_no_extra;
  152. GTP_INFO("I2C function: without pre and end cmd!");
  153. }
  154. }
  155. static void unregister_i2c_func(void)
  156. {
  157. tool_i2c_read = NULL;
  158. tool_i2c_write = NULL;
  159. GTP_INFO("I2C function: unregister i2c transfer function!");
  160. }
  161. s32 init_wr_node(struct i2c_client *client)
  162. {
  163. s32 i;
  164. gt_client = i2c_client_point;
  165. memset(&cmd_head, 0, sizeof(cmd_head));
  166. cmd_head.data = NULL;
  167. i = 5;
  168. while ((!cmd_head.data) && i) {
  169. cmd_head.data = kzalloc(i * DATA_LENGTH_UINT, GFP_KERNEL);
  170. if (NULL != cmd_head.data)
  171. break;
  172. i--;
  173. }
  174. if (i) {
  175. DATA_LENGTH = i * DATA_LENGTH_UINT + GTP_ADDR_LENGTH;
  176. GTP_INFO("Applied memory size:%d.", DATA_LENGTH);
  177. } else {
  178. GTP_ERROR("Apply for memory failed.");
  179. return FAIL;
  180. }
  181. cmd_head.addr_len = 2;
  182. cmd_head.retry = 5;
  183. register_i2c_func();
  184. tool_set_proc_name(procname);
  185. #if 0 /* fix 3.10 */
  186. goodix_proc_entry = create_proc_entry(gtp_tool_entry, 0664, NULL);
  187. if (goodix_proc_entry == NULL) {
  188. GTP_ERROR("Couldn't create proc entry!");
  189. return FAIL;
  190. }
  191. GTP_INFO("Create proc entry success!");
  192. goodix_proc_entry->write_proc = goodix_tool_write;
  193. goodix_proc_entry->read_proc = goodix_tool_read;
  194. #else
  195. if (proc_create(procname, 0660, NULL, &gt_tool_fops) == NULL) {
  196. GTP_ERROR("create_proc_entry %s failed", procname);
  197. return -1;
  198. }
  199. #endif
  200. return SUCCESS;
  201. }
  202. void uninit_wr_node(void)
  203. {
  204. kfree(cmd_head.data);
  205. cmd_head.data = NULL;
  206. unregister_i2c_func();
  207. remove_proc_entry(procname, NULL);
  208. }
  209. static u8 relation(u8 src, u8 dst, u8 rlt)
  210. {
  211. u8 ret = 0;
  212. switch (rlt) {
  213. case 0:
  214. ret = (src != dst) ? true : false;
  215. break;
  216. case 1:
  217. ret = (src == dst) ? true : false;
  218. GTP_DEBUG("equal:src:0x%02x dst:0x%02x ret:%d.", src, dst, (s32) ret);
  219. break;
  220. case 2:
  221. ret = (src > dst) ? true : false;
  222. break;
  223. case 3:
  224. ret = (src < dst) ? true : false;
  225. break;
  226. case 4:
  227. ret = (src & dst) ? true : false;
  228. break;
  229. case 5:
  230. ret = (!(src | dst)) ? true : false;
  231. break;
  232. default:
  233. ret = false;
  234. break;
  235. }
  236. return ret;
  237. }
  238. /*******************************************************
  239. Function:
  240. Comfirm function.
  241. Input:
  242. None.
  243. Output:
  244. Return write length.
  245. ********************************************************/
  246. static u8 comfirm(void)
  247. {
  248. s32 i = 0;
  249. u8 buf[32];
  250. /* memcpy(&buf[GTP_ADDR_LENGTH - cmd_head.addr_len], &cmd_head.flag_addr, cmd_head.addr_len); */
  251. /* memcpy(buf, &cmd_head.flag_addr, cmd_head.addr_len);//Modified by Scott, 2012-02-17 */
  252. memcpy(buf, cmd_head.flag_addr, cmd_head.addr_len);
  253. for (i = 0; i < cmd_head.times; i++) {
  254. if (tool_i2c_read(buf, 1) <= 0) {
  255. GTP_ERROR("Read flag data failed!");
  256. return FAIL;
  257. }
  258. if (true ==
  259. relation(buf[GTP_ADDR_LENGTH], cmd_head.flag_val, cmd_head.flag_relation)) {
  260. GTP_DEBUG("value at flag addr:0x%02x.", buf[GTP_ADDR_LENGTH]);
  261. GTP_DEBUG("flag value:0x%02x.", cmd_head.flag_val);
  262. break;
  263. }
  264. msleep(cmd_head.circle);
  265. }
  266. if (i >= cmd_head.times) {
  267. GTP_ERROR("Didn't get the flag to continue!");
  268. return FAIL;
  269. }
  270. return SUCCESS;
  271. }
  272. /*******************************************************
  273. Function:
  274. Goodix tool write function.
  275. Input:
  276. standard proc write function param.
  277. Output:
  278. Return write length.
  279. ********************************************************/
  280. static s32 goodix_tool_write(struct file *filp, const char __user *buff, unsigned long len,
  281. void *data)
  282. {
  283. u64 ret = 0;
  284. GTP_DEBUG_FUNC();
  285. GTP_DEBUG_ARRAY((u8 *) buff, len);
  286. ret = copy_from_user(&cmd_head, buff, CMD_HEAD_LENGTH);
  287. if (ret)
  288. GTP_ERROR("copy_from_user failed.");
  289. GTP_DEBUG("wr :0x%02x.", cmd_head.wr);
  290. GTP_DEBUG("flag:0x%02x.", cmd_head.flag);
  291. GTP_DEBUG("flag addr:0x%02x%02x.", cmd_head.flag_addr[0], cmd_head.flag_addr[1]);
  292. GTP_DEBUG("flag val:0x%02x.", cmd_head.flag_val);
  293. GTP_DEBUG("flag rel:0x%02x.", cmd_head.flag_relation);
  294. GTP_DEBUG("circle :%d.", (s32) cmd_head.circle);
  295. GTP_DEBUG("times :%d.", (s32) cmd_head.times);
  296. GTP_DEBUG("retry :%d.", (s32) cmd_head.retry);
  297. GTP_DEBUG("delay :%d.", (s32) cmd_head.delay);
  298. GTP_DEBUG("data len:%d.", (s32) cmd_head.data_len);
  299. GTP_DEBUG("addr len:%d.", (s32) cmd_head.addr_len);
  300. GTP_DEBUG("addr:0x%02x%02x.", cmd_head.addr[0], cmd_head.addr[1]);
  301. GTP_DEBUG("len:%d.", (s32) len);
  302. GTP_DEBUG("buf[20]:0x%02x.", buff[CMD_HEAD_LENGTH]);
  303. if (1 == cmd_head.wr) {
  304. /* copy_from_user(&cmd_head.data[cmd_head.addr_len], &buff[CMD_HEAD_LENGTH], cmd_head.data_len); */
  305. ret =
  306. copy_from_user(&cmd_head.data[GTP_ADDR_LENGTH], &buff[CMD_HEAD_LENGTH],
  307. cmd_head.data_len);
  308. if (ret)
  309. GTP_ERROR("copy_from_user failed.");
  310. memcpy(&cmd_head.data[GTP_ADDR_LENGTH - cmd_head.addr_len], cmd_head.addr,
  311. cmd_head.addr_len);
  312. GTP_DEBUG_ARRAY(cmd_head.data, cmd_head.data_len + cmd_head.addr_len);
  313. GTP_DEBUG_ARRAY((u8 *) &buff[CMD_HEAD_LENGTH], cmd_head.data_len);
  314. if (1 == cmd_head.flag) {
  315. if (FAIL == comfirm()) {
  316. GTP_ERROR("[WRITE]Comfirm fail!");
  317. return FAIL;
  318. }
  319. } else if (2 == cmd_head.flag) {
  320. /* Need interrupt! */
  321. }
  322. if (tool_i2c_write(&cmd_head.data[GTP_ADDR_LENGTH - cmd_head.addr_len],
  323. cmd_head.data_len + cmd_head.addr_len) <= 0) {
  324. GTP_ERROR("[WRITE]Write data failed!");
  325. return FAIL;
  326. }
  327. GTP_DEBUG_ARRAY(&cmd_head.data[GTP_ADDR_LENGTH - cmd_head.addr_len],
  328. cmd_head.data_len + cmd_head.addr_len);
  329. if (cmd_head.delay)
  330. msleep(cmd_head.delay);
  331. return cmd_head.data_len + CMD_HEAD_LENGTH;
  332. } else if (3 == cmd_head.wr) { /* Write ic type */
  333. memcpy(IC_TYPE, cmd_head.data, cmd_head.data_len);
  334. register_i2c_func();
  335. return cmd_head.data_len + CMD_HEAD_LENGTH;
  336. } else if (5 == cmd_head.wr) {
  337. /* memcpy(IC_TYPE, cmd_head.data, cmd_head.data_len); */
  338. return cmd_head.data_len + CMD_HEAD_LENGTH;
  339. } else if (7 == cmd_head.wr) { /* disable irq! */
  340. disable_irq(touch_irq);
  341. #if GTP_ESD_PROTECT
  342. gtp_esd_switch(i2c_client_point, SWITCH_OFF);
  343. #endif
  344. return CMD_HEAD_LENGTH;
  345. } else if (9 == cmd_head.wr) { /* enable irq! */
  346. enable_irq(touch_irq);
  347. #if GTP_ESD_PROTECT
  348. gtp_esd_switch(i2c_client_point, SWITCH_ON);
  349. #endif
  350. return CMD_HEAD_LENGTH;
  351. } else if (17 == cmd_head.wr) {
  352. /* struct goodix_ts_data *ts = i2c_get_clientdata(gt_client); */
  353. ret =
  354. copy_from_user(&cmd_head.data[GTP_ADDR_LENGTH], &buff[CMD_HEAD_LENGTH],
  355. cmd_head.data_len);
  356. if (ret)
  357. GTP_DEBUG("copy_from_user failed.");
  358. if (cmd_head.data[GTP_ADDR_LENGTH]) {
  359. GTP_DEBUG("gtp enter rawdiff.");
  360. gtp_rawdiff_mode = true;
  361. } else {
  362. gtp_rawdiff_mode = false;
  363. GTP_DEBUG("gtp leave rawdiff.");
  364. }
  365. return CMD_HEAD_LENGTH;
  366. }
  367. #ifdef UPDATE_FUNCTIONS
  368. else if (11 == cmd_head.wr) { /* Enter update mode! */
  369. if (FAIL == gup_enter_update_mode(gt_client))
  370. return FAIL;
  371. } else if (13 == cmd_head.wr) { /* Leave update mode! */
  372. gup_leave_update_mode();
  373. } else if (15 == cmd_head.wr) { /* Update firmware! */
  374. show_len = 0;
  375. total_len = 0;
  376. if ((cmd_head.data == NULL)
  377. || (cmd_head.data_len >= DATA_LENGTH)
  378. || (cmd_head.data_len >= (len - CMD_HEAD_LENGTH))) {
  379. GTP_ERROR("copy_from_user data out of range.");
  380. return -EINVAL;
  381. }
  382. memset(cmd_head.data, 0, cmd_head.data_len + 1);
  383. memcpy(cmd_head.data, &buff[CMD_HEAD_LENGTH], cmd_head.data_len);
  384. GTP_DEBUG("update firmware, filename: %s", cmd_head.data);
  385. if (FAIL == gup_update_proc((void *)cmd_head.data))
  386. return FAIL;
  387. }
  388. #endif
  389. return CMD_HEAD_LENGTH;
  390. }
  391. /*******************************************************
  392. Function:
  393. Goodix tool read function.
  394. Input:
  395. standard proc read function param.
  396. Output:
  397. Return read length.
  398. ********************************************************/
  399. static s32 goodix_tool_read(char *page, char **start, off_t off, int count, int *eof, void *data)
  400. {
  401. GTP_DEBUG_FUNC();
  402. if (cmd_head.wr % 2) {
  403. return FAIL;
  404. } else if (!cmd_head.wr) {
  405. u16 len = 0;
  406. s16 data_len = 0;
  407. u16 loc = 0;
  408. if (1 == cmd_head.flag) {
  409. if (FAIL == comfirm()) {
  410. GTP_ERROR("[READ]Comfirm fail!");
  411. return FAIL;
  412. }
  413. } else if (2 == cmd_head.flag) {
  414. /* Need interrupt! */
  415. }
  416. memcpy(cmd_head.data, cmd_head.addr, cmd_head.addr_len);
  417. GTP_DEBUG("[CMD HEAD DATA] ADDR:0x%02x%02x.", cmd_head.data[0], cmd_head.data[1]);
  418. GTP_DEBUG("[CMD HEAD ADDR] ADDR:0x%02x%02x.", cmd_head.addr[0], cmd_head.addr[1]);
  419. if (cmd_head.delay)
  420. msleep(cmd_head.delay);
  421. data_len = cmd_head.data_len;
  422. while (data_len > 0) {
  423. if (data_len > DATA_LENGTH)
  424. len = DATA_LENGTH;
  425. else
  426. len = data_len;
  427. data_len -= DATA_LENGTH;
  428. if (tool_i2c_read(cmd_head.data, len) <= 0) {
  429. GTP_ERROR("[READ]Read data failed!");
  430. return FAIL;
  431. }
  432. memcpy(&page[loc], &cmd_head.data[GTP_ADDR_LENGTH], len);
  433. loc += len;
  434. GTP_DEBUG_ARRAY(&cmd_head.data[GTP_ADDR_LENGTH], len);
  435. GTP_DEBUG_ARRAY(page, len);
  436. }
  437. } else if (2 == cmd_head.wr) {
  438. /* memcpy(page, "gt8", cmd_head.data_len); */
  439. /* memcpy(page, "GT818", 5); */
  440. /* page[5] = 0; */
  441. GTP_DEBUG("Return ic type:%s len:%d.", page, (s32) cmd_head.data_len);
  442. return cmd_head.data_len;
  443. /* return sizeof(IC_TYPE_NAME); */
  444. } else if (4 == cmd_head.wr) {
  445. page[0] = show_len >> 8;
  446. page[1] = show_len & 0xff;
  447. page[2] = total_len >> 8;
  448. page[3] = total_len & 0xff;
  449. return cmd_head.data_len;
  450. } else if (6 == cmd_head.wr) {
  451. /* Read error code! */
  452. } else if (8 == cmd_head.wr) { /* Read driver version */
  453. /* memcpy(page, GTP_DRIVER_VERSION, strlen(GTP_DRIVER_VERSION)); */
  454. s32 tmp_len;
  455. tmp_len = strlen(GTP_DRIVER_VERSION);
  456. memcpy(page, GTP_DRIVER_VERSION, tmp_len);
  457. page[tmp_len] = 0;
  458. }
  459. return cmd_head.data_len;
  460. }