gt1x_generic.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  1. /* drivers/input/touchscreen/gt1x_generic.c
  2. *
  3. * 2010 - 2014 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.0
  17. * Revision RecordL:
  18. * V1.0: first release. 2014/09/28.
  19. *
  20. */
  21. #include <linux/input.h>
  22. #include "include/gt1x_tpd_common.h"
  23. #include "gt1x_config.h"
  24. #ifdef CONFIG_GTP_PROXIMITY
  25. #include <linux/hwmsensor.h>
  26. #include <linux/hwmsen_dev.h>
  27. #include <linux/sensors_io.h>
  28. #endif
  29. #ifdef CONFIG_GTP_ICS_SLOT_REPORT
  30. #include <linux/input/mt.h>
  31. #endif
  32. /*******************GLOBAL VARIABLE*********************/
  33. struct i2c_client *gt1x_i2c_client = NULL;
  34. static struct workqueue_struct *gt1x_workqueue;
  35. u8 gt1x_config[GTP_CONFIG_MAX_LENGTH] = { 0 };
  36. u32 gt1x_cfg_length = GTP_CONFIG_MAX_LENGTH;
  37. bool check_flag = false;
  38. CHIP_TYPE_T gt1x_chip_type = CHIP_TYPE_GT1X;
  39. struct gt1x_version_info gt1x_version = {
  40. .product_id = {0},
  41. .patch_id = 0,
  42. .mask_id = 0,
  43. .sensor_id = 0,
  44. .match_opt = 0
  45. };
  46. #if defined(CONFIG_GTP_WITH_STYLUS) && defined(CONFIG_GTP_HAVE_STYLUS_KEY)
  47. const u16 gt1x_stylus_key_array[] = GTP_STYLUS_KEY_TAB;
  48. #endif
  49. u8 gt1x_clk_buf[6];
  50. u8 gt1x_clk_retries = 0;
  51. u8 gt1x_ref_retries = 0;
  52. u8 gt1x_driver_num = 0;
  53. u8 gt1x_sensor_num = 0;
  54. u8 gt1x_int_type = 0;
  55. u8 gt1x_wakeup_level = 0;
  56. u32 gt1x_abs_x_max = 0;
  57. u32 gt1x_abs_y_max = 0;
  58. u8 gt1x_rawdiff_mode = 0;
  59. u8 gt1x_init_failed = 0;
  60. u8 is_resetting = 0;
  61. static ssize_t gt1x_debug_read_proc(struct file *, char __user *, size_t, loff_t *);
  62. static ssize_t gt1x_debug_write_proc(struct file *, const char __user *, size_t, loff_t *);
  63. static struct proc_dir_entry *gt1x_debug_proc_entry;
  64. static const struct file_operations gt1x_debug_fops = {
  65. .owner = THIS_MODULE,
  66. .read = gt1x_debug_read_proc,
  67. .write = gt1x_debug_write_proc,
  68. };
  69. s32 gt1x_init_debug_node(void)
  70. {
  71. gt1x_debug_proc_entry = proc_create(GT1X_DEBUG_PROC_FILE, 0660, NULL, &gt1x_debug_fops);
  72. if (gt1x_debug_proc_entry == NULL) {
  73. GTP_ERROR("create_proc_entry %s FAILED!", GT1X_DEBUG_PROC_FILE);
  74. return -1;
  75. }
  76. GTP_ERROR("create_proc_entry %s SUCCESS.", GT1X_DEBUG_PROC_FILE);
  77. return 0;
  78. }
  79. void gt1x_deinit_debug_node(void)
  80. {
  81. if (gt1x_debug_proc_entry != NULL)
  82. remove_proc_entry(GT1X_DEBUG_PROC_FILE, NULL);
  83. }
  84. static ssize_t gt1x_debug_read_proc(struct file *file, char __user *page, size_t size, loff_t *ppos)
  85. {
  86. char *ptr = page;
  87. char temp_data[GTP_CONFIG_MAX_LENGTH] = { 0 };
  88. int i;
  89. ssize_t ret;
  90. if (*ppos)
  91. return 0;
  92. ptr += sprintf(ptr, "==== GT1X default config setting in driver====\n");
  93. for (i = 0; i < GTP_CONFIG_MAX_LENGTH; i++) {
  94. ptr += sprintf(ptr, "0x%02X,", gt1x_config[i]);
  95. if (i % 10 == 9)
  96. ptr += sprintf(ptr, "\n");
  97. }
  98. ptr += sprintf(ptr, "\n");
  99. ptr += sprintf(ptr, "==== GT1X config read from chip====\n");
  100. i = gt1x_i2c_read(GTP_REG_CONFIG_DATA, temp_data, GTP_CONFIG_MAX_LENGTH);
  101. GTP_INFO("I2C TRANSFER: %d", i);
  102. for (i = 0; i < GTP_CONFIG_MAX_LENGTH; i++) {
  103. ptr += sprintf(ptr, "0x%02X,", temp_data[i]);
  104. if (i % 10 == 9)
  105. ptr += sprintf(ptr, "\n");
  106. }
  107. /* Touch PID & VID */
  108. ptr += sprintf(ptr, "\n");
  109. ptr += sprintf(ptr, "==== GT1X Version Info ====\n");
  110. gt1x_i2c_read(GTP_REG_VERSION, temp_data, 12);
  111. ptr += sprintf(ptr, "ProductID: GT%c%c%c%c\n", temp_data[0], temp_data[1], temp_data[2], temp_data[3]);
  112. ptr += sprintf(ptr, "PatchID: %02X%02X\n", temp_data[4], temp_data[5]);
  113. ptr += sprintf(ptr, "MaskID: %02X%02X\n", temp_data[7], temp_data[8]);
  114. ptr += sprintf(ptr, "SensorID: %02X\n", temp_data[10] & 0x0F);
  115. ptr += sprintf(ptr, "Driver Num: %02d. Sensor Num: %02d\n", gt1x_driver_num, gt1x_sensor_num);
  116. *ppos += ptr - page;
  117. ret = ptr - page;
  118. return ret;
  119. }
  120. static ssize_t gt1x_debug_write_proc(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  121. {
  122. s32 ret = 0;
  123. u8 buf[GTP_CONFIG_MAX_LENGTH] = { 0 };
  124. char mode_str[50] = { 0 };
  125. int mode;
  126. int cfg_len;
  127. char arg1[50] = { 0 };
  128. u8 temp_config[GTP_CONFIG_MAX_LENGTH] = { 0 };
  129. GTP_DEBUG("write count %ld\n", (unsigned long)count);
  130. if (count > GTP_CONFIG_MAX_LENGTH) {
  131. GTP_ERROR("Too much data, buffer size: %d, data:%ld", GTP_CONFIG_MAX_LENGTH, (unsigned long)count);
  132. return -EFAULT;
  133. }
  134. if (copy_from_user(buf, buffer, count)) {
  135. GTP_ERROR("copy from user fail!");
  136. return -EFAULT;
  137. }
  138. /*send config*/
  139. if (count == gt1x_cfg_length) {
  140. memcpy(gt1x_config, buf, count);
  141. ret = gt1x_send_cfg(gt1x_config, gt1x_cfg_length);
  142. if (ret < 0) {
  143. GTP_ERROR("send gt1x_config failed.");
  144. return -EFAULT;
  145. }
  146. gt1x_abs_x_max = (gt1x_config[RESOLUTION_LOC + 1] << 8) + gt1x_config[RESOLUTION_LOC];
  147. gt1x_abs_y_max = (gt1x_config[RESOLUTION_LOC + 3] << 8) + gt1x_config[RESOLUTION_LOC + 2];
  148. return count;
  149. }
  150. ret = sscanf(buf, "%49s %d", (char *)&mode_str, &mode);
  151. if (ret < 0) {
  152. GTP_ERROR("Sscanf buf ERROR1");
  153. return ret;
  154. }
  155. /*force clear gt1x_config*/
  156. if (strcmp(mode_str, "clear_config") == 0) {
  157. GTP_INFO("Force clear gt1x_config");
  158. gt1x_send_cmd(GTP_CMD_CLEAR_CFG, 0);
  159. return count;
  160. }
  161. if (strcmp(mode_str, "init") == 0) {
  162. GTP_INFO("Init panel");
  163. gt1x_init_panel();
  164. return count;
  165. }
  166. if (strcmp(mode_str, "chip") == 0) {
  167. GTP_INFO("Get chip type:");
  168. gt1x_get_chip_type();
  169. return count;
  170. }
  171. if (strcmp(mode_str, "int") == 0) {
  172. if (mode == 0) {
  173. GTP_INFO("Disable irq.");
  174. gt1x_irq_disable();
  175. } else {
  176. GTP_INFO("Enable irq.");
  177. gt1x_irq_enable();
  178. }
  179. return count;
  180. }
  181. if (strcmp(mode_str, "poweron") == 0) {
  182. gt1x_power_switch(1);
  183. return count;
  184. }
  185. if (strcmp(mode_str, "poweroff") == 0) {
  186. gt1x_power_switch(0);
  187. return count;
  188. }
  189. if (strcmp(mode_str, "version") == 0) {
  190. gt1x_read_version(NULL);
  191. return count;
  192. }
  193. if (strcmp(mode_str, "reset") == 0) {
  194. gt1x_reset_guitar();
  195. return count;
  196. }
  197. #ifdef CONFIG_GTP_CHARGER_SWITCH
  198. if (strcmp(mode_str, "charger") == 0) {
  199. gt1x_charger_config(mode);
  200. return count;
  201. }
  202. #endif
  203. ret = sscanf(buf, "%49s %49s", (char *)&mode_str, (char *)&arg1);
  204. if (ret < 0) {
  205. GTP_ERROR("Sscanf buf ERROR2");
  206. return ret;
  207. }
  208. if (strcmp(mode_str, "update") == 0) {
  209. gt1x_update_firmware(arg1);
  210. return count;
  211. }
  212. if (strcmp(mode_str, "sendconfig") == 0) {
  213. cfg_len = gt1x_parse_config(arg1, temp_config);
  214. if (cfg_len < 0)
  215. return -1;
  216. gt1x_send_cfg(gt1x_config, gt1x_cfg_length);
  217. return count;
  218. }
  219. return gt1x_debug_proc(buf, count);
  220. }
  221. static u8 ascii2hex(u8 a)
  222. {
  223. s8 value = 0;
  224. if (a >= '0' && a <= '9')
  225. value = a - '0';
  226. else if (a >= 'A' && a <= 'F')
  227. value = a - 'A' + 0x0A;
  228. else if (a >= 'a' && a <= 'f')
  229. value = a - 'a' + 0x0A;
  230. else
  231. value = 0xff;
  232. return value;
  233. }
  234. int gt1x_parse_config(char *filename, u8 *config)
  235. {
  236. mm_segment_t old_fs;
  237. struct file *fp = NULL;
  238. u8 *buf;
  239. int i;
  240. int len;
  241. int cur_len = -1;
  242. u8 high, low;
  243. old_fs = get_fs();
  244. set_fs(KERNEL_DS);
  245. fp = filp_open(filename, O_RDONLY, 0);
  246. if (IS_ERR(fp)) {
  247. GTP_ERROR("Open config file error!(file: %s)", filename);
  248. goto parse_cfg_fail1;
  249. }
  250. len = fp->f_op->llseek(fp, 0, SEEK_END);
  251. if (len > GTP_CONFIG_MAX_LENGTH * 6 || len < GTP_CONFIG_MAX_LENGTH) {
  252. GTP_ERROR("Config is invalid!(length: %d)", len);
  253. goto parse_cfg_fail2;
  254. }
  255. buf = kzalloc(len, GFP_KERNEL);
  256. if (buf == NULL) {
  257. GTP_ERROR("Allocate memory failed!(size: %d)", len);
  258. goto parse_cfg_fail2;
  259. }
  260. fp->f_op->llseek(fp, 0, SEEK_SET);
  261. if (fp->f_op->read(fp, (char *)buf, len, &fp->f_pos) != len)
  262. GTP_ERROR("Read %d bytes from file failed!", len);
  263. GTP_INFO("Parse config file: %s (%d bytes)", filename, len);
  264. for (i = 0, cur_len = 0; i < len && cur_len < GTP_CONFIG_MAX_LENGTH;) {
  265. if (buf[i] == ' ' || buf[i] == '\r' || buf[i] == '\n' || buf[i] == ',') {
  266. i++;
  267. continue;
  268. }
  269. if (buf[i] == '0' && (buf[i + 1] == 'x' || buf[i + 1] == 'X')) {
  270. high = ascii2hex(buf[i + 2]);
  271. low = ascii2hex(buf[i + 3]);
  272. if (high != 0xFF && low != 0xFF) {
  273. config[cur_len++] = (high << 4) + low;
  274. i += 4;
  275. continue;
  276. }
  277. }
  278. GTP_ERROR("Illegal config file!");
  279. cur_len = -1;
  280. break;
  281. }
  282. if (cur_len < GTP_CONFIG_MIN_LENGTH || config[cur_len - 1] != 0x01) {
  283. cur_len = -1;
  284. } else {
  285. for (i = 0; i < cur_len; i++) {
  286. if (i % 10 == 0)
  287. GTP_INFO("\n<<GTP-DBG>>:");
  288. GTP_INFO("0x%02x,", config[i]);
  289. }
  290. GTP_INFO("\n");
  291. }
  292. kfree(buf);
  293. parse_cfg_fail2:
  294. filp_close(fp, NULL);
  295. parse_cfg_fail1:
  296. set_fs(old_fs);
  297. return cur_len;
  298. }
  299. s32 _do_i2c_read(struct i2c_msg *msgs, u16 addr, u8 *buffer, s32 len)
  300. {
  301. s32 ret = -1;
  302. s32 pos = 0;
  303. s32 data_length = len;
  304. s32 transfer_length = 0;
  305. u8 *data = NULL;
  306. u16 address = addr;
  307. data =
  308. kmalloc(IIC_MAX_TRANSFER_SIZE <
  309. (len + GTP_ADDR_LENGTH) ? IIC_MAX_TRANSFER_SIZE : (len + GTP_ADDR_LENGTH), GFP_KERNEL);
  310. if (data == NULL)
  311. return ERROR_MEM;
  312. msgs[1].buf = data;
  313. while (pos != data_length) {
  314. if ((data_length - pos) > IIC_MAX_TRANSFER_SIZE)
  315. transfer_length = IIC_MAX_TRANSFER_SIZE;
  316. else
  317. transfer_length = data_length - pos;
  318. msgs[0].buf[0] = (address >> 8) & 0xFF;
  319. msgs[0].buf[1] = address & 0xFF;
  320. msgs[1].len = transfer_length;
  321. ret = i2c_transfer(gt1x_i2c_client->adapter, msgs, 2);
  322. if (ret != 2) {
  323. GTP_INFO("I2c Transfer error! (%d)", ret);
  324. kfree(data);
  325. return ERROR_IIC;
  326. }
  327. memcpy(&buffer[pos], msgs[1].buf, transfer_length);
  328. pos += transfer_length;
  329. address += transfer_length;
  330. }
  331. kfree(data);
  332. return 0;
  333. }
  334. s32 _do_i2c_write(struct i2c_msg *msg, u16 addr, u8 *buffer, s32 len)
  335. {
  336. s32 ret = -1;
  337. s32 pos = 0;
  338. s32 data_length = len;
  339. s32 transfer_length = 0;
  340. u8 *data = NULL;
  341. u16 address = addr;
  342. data =
  343. kmalloc(IIC_MAX_TRANSFER_SIZE <
  344. (len + GTP_ADDR_LENGTH) ? IIC_MAX_TRANSFER_SIZE : (len + GTP_ADDR_LENGTH), GFP_KERNEL);
  345. if (data == NULL)
  346. return ERROR_MEM;
  347. msg->buf = data;
  348. while (pos != data_length) {
  349. if ((data_length - pos) > (IIC_MAX_TRANSFER_SIZE - GTP_ADDR_LENGTH))
  350. transfer_length = IIC_MAX_TRANSFER_SIZE - GTP_ADDR_LENGTH;
  351. else
  352. transfer_length = data_length - pos;
  353. msg->buf[0] = (address >> 8) & 0xFF;
  354. msg->buf[1] = address & 0xFF;
  355. msg->len = transfer_length + GTP_ADDR_LENGTH;
  356. memcpy(&msg->buf[GTP_ADDR_LENGTH], &buffer[pos], transfer_length);
  357. ret = i2c_transfer(gt1x_i2c_client->adapter, msg, 1);
  358. if (ret != 1) {
  359. GTP_INFO("I2c Transfer error! (%d)", ret);
  360. kfree(data);
  361. return ERROR_IIC;
  362. }
  363. pos += transfer_length;
  364. address += transfer_length;
  365. }
  366. kfree(data);
  367. return 0;
  368. }
  369. s32 gt1x_i2c_test(void)
  370. {
  371. u8 retry = 0;
  372. s32 ret = -1;
  373. u32 hw_info = 0;
  374. GTP_DEBUG_FUNC();
  375. while (retry++ < 5) {
  376. ret = gt1x_i2c_read(GTP_REG_HW_INFO, (u8 *) &hw_info, sizeof(hw_info));
  377. if (!ret) {
  378. GTP_INFO("GTP_REG_HW_INFO : %08X", hw_info);
  379. return ret;
  380. }
  381. msleep(20);
  382. GTP_ERROR("GTP_REG_HW_INFO : %08X", hw_info);
  383. GTP_ERROR("GTP i2c test failed time %d.", retry);
  384. }
  385. return ERROR_RETRY;
  386. }
  387. s32 gt1x_i2c_read_dbl_check(u16 addr, u8 *buffer, s32 len)
  388. {
  389. u8 buf[16] = { 0 };
  390. u8 confirm_buf[16] = { 0 };
  391. if (len > 16) {
  392. GTP_ERROR("i2c_read_dbl_check length %d is too long, do not beyond %d", len, (int)(sizeof(buf)));
  393. return ERROR;
  394. }
  395. memset(buf, 0xAA, 16);
  396. gt1x_i2c_read(addr, buf, len);
  397. msleep(20);
  398. memset(confirm_buf, 0, 16);
  399. gt1x_i2c_read(addr, confirm_buf, len);
  400. if (!memcmp(buf, confirm_buf, len)) {
  401. memcpy(buffer, confirm_buf, len);
  402. return 0;
  403. }
  404. GTP_ERROR("i2c read 0x%04X, %d bytes, double check failed!", addr, len);
  405. return ERROR;
  406. }
  407. /**
  408. * gt1x_get_info - Get information from ic, such as resolution and
  409. * int trigger type
  410. * Return <0: i2c failed, 0: i2c ok
  411. */
  412. s32 gt1x_get_info(void)
  413. {
  414. u8 opr_buf[4] = { 0 };
  415. s32 ret = 0;
  416. ret = gt1x_i2c_read(GTP_REG_CONFIG_DATA + 1, opr_buf, 4);
  417. if (ret < 0)
  418. return ret;
  419. gt1x_abs_x_max = (opr_buf[1] << 8) + opr_buf[0];
  420. gt1x_abs_y_max = (opr_buf[3] << 8) + opr_buf[2];
  421. ret = gt1x_i2c_read(GTP_REG_CONFIG_DATA + 6, opr_buf, 1);
  422. if (ret < 0)
  423. return ret;
  424. gt1x_int_type = opr_buf[0] & 0x03;
  425. GTP_INFO("X_MAX = %d, Y_MAX = %d, TRIGGER = 0x%02x", gt1x_abs_x_max, gt1x_abs_y_max, gt1x_int_type);
  426. return 0;
  427. }
  428. /**
  429. * gt1x_send_cfg - Send gt1x_config Function.
  430. * @config: pointer of the configuration array.
  431. * @cfg_len: length of configuration array.
  432. * Return 0--success,non-0--fail.
  433. */
  434. s32 gt1x_send_cfg(u8 *config, int cfg_len)
  435. {
  436. #ifdef CONFIG_GTP_DRIVER_SEND_CFG
  437. int i;
  438. s32 ret = 0;
  439. s32 retry = 0;
  440. u16 checksum = 0;
  441. GTP_DEBUG("Driver Send Config, length: %d", cfg_len);
  442. for (i = 0; i < cfg_len - 3; i += 2)
  443. checksum += (config[i] << 8) + config[i + 1];
  444. if (!checksum) {
  445. GTP_ERROR("Invalid config, all of the bytes is zero!");
  446. return -1;
  447. }
  448. checksum = 0 - checksum;
  449. GTP_DEBUG("Config checksum: 0x%04X", checksum);
  450. config[cfg_len - 3] = (checksum >> 8) & 0xFF;
  451. config[cfg_len - 2] = checksum & 0xFF;
  452. config[cfg_len - 1] = 0x01;
  453. while (retry++ < 5) {
  454. ret = gt1x_i2c_write(GTP_REG_CONFIG_DATA, config, cfg_len);
  455. if (!ret) {
  456. msleep(200); /* must 200ms, wait for storing config into flash. */
  457. GTP_DEBUG("Send config successfully!");
  458. return 0;
  459. }
  460. }
  461. GTP_ERROR("Send config failed!");
  462. return ret;
  463. #endif
  464. return 0;
  465. }
  466. s32 gt1x_init_panel(void)
  467. {
  468. s32 ret = 0;
  469. u8 cfg_len = 0;
  470. #ifdef CONFIG_GTP_DRIVER_SEND_CFG
  471. u8 sensor_id = 0;
  472. const u8 cfg_grp0[] = GTP_CFG_GROUP0;
  473. const u8 cfg_grp1[] = GTP_CFG_GROUP1;
  474. const u8 cfg_grp2[] = GTP_CFG_GROUP2;
  475. const u8 cfg_grp3[] = GTP_CFG_GROUP3;
  476. const u8 cfg_grp4[] = GTP_CFG_GROUP4;
  477. const u8 cfg_grp5[] = GTP_CFG_GROUP5;
  478. const u8 *cfgs[] = {
  479. cfg_grp0, cfg_grp1, cfg_grp2,
  480. cfg_grp3, cfg_grp4, cfg_grp5
  481. };
  482. u8 cfg_lens[] = {
  483. CFG_GROUP_LEN(cfg_grp0),
  484. CFG_GROUP_LEN(cfg_grp1),
  485. CFG_GROUP_LEN(cfg_grp2),
  486. CFG_GROUP_LEN(cfg_grp3),
  487. CFG_GROUP_LEN(cfg_grp4),
  488. CFG_GROUP_LEN(cfg_grp5)
  489. };
  490. #ifdef CONFIG_GTP_CHARGER_SWITCH
  491. const u8 cfg_grp0_charger[] = GTP_CFG_GROUP0_CHARGER;
  492. const u8 cfg_grp1_charger[] = GTP_CFG_GROUP1_CHARGER;
  493. const u8 cfg_grp2_charger[] = GTP_CFG_GROUP2_CHARGER;
  494. const u8 cfg_grp3_charger[] = GTP_CFG_GROUP3_CHARGER;
  495. const u8 cfg_grp4_charger[] = GTP_CFG_GROUP4_CHARGER;
  496. const u8 cfg_grp5_charger[] = GTP_CFG_GROUP5_CHARGER;
  497. const u8 *cfgs_charger[] = {
  498. cfg_grp0_charger, cfg_grp1_charger, cfg_grp2_charger,
  499. cfg_grp3_charger, cfg_grp4_charger, cfg_grp5_charger
  500. };
  501. u8 cfg_lens_charger[] = {
  502. CFG_GROUP_LEN(cfg_grp0_charger),
  503. CFG_GROUP_LEN(cfg_grp1_charger),
  504. CFG_GROUP_LEN(cfg_grp2_charger),
  505. CFG_GROUP_LEN(cfg_grp3_charger),
  506. CFG_GROUP_LEN(cfg_grp4_charger),
  507. CFG_GROUP_LEN(cfg_grp5_charger)
  508. };
  509. #endif /* end CONFIG_GTP_CHARGER_SWITCH */
  510. GTP_DEBUG("Config Groups Length: %d, %d, %d, %d, %d, %d", cfg_lens[0], cfg_lens[1], cfg_lens[2], cfg_lens[3],
  511. cfg_lens[4], cfg_lens[5]);
  512. sensor_id = gt1x_version.sensor_id;
  513. if (sensor_id >= 6 || cfg_lens[sensor_id] < GTP_CONFIG_MIN_LENGTH
  514. || cfg_lens[sensor_id] > GTP_CONFIG_MAX_LENGTH) {
  515. sensor_id = 0;
  516. }
  517. cfg_len = cfg_lens[sensor_id];
  518. GTP_INFO("CTP_CONFIG_GROUP%d used, gt1x_config length: %d", sensor_id, cfg_len);
  519. if (cfg_len < GTP_CONFIG_MIN_LENGTH || cfg_len > GTP_CONFIG_MAX_LENGTH) {
  520. GTP_ERROR
  521. ("CTP_CONFIG_GROUP%d is INVALID CONFIG GROUP! NO Config Sent! ;"
  522. "You need to check you header file CFG_GROUP section!",
  523. sensor_id + 1);
  524. return -1;
  525. }
  526. memset(gt1x_config, 0, sizeof(gt1x_config));
  527. memcpy(gt1x_config, cfgs[sensor_id], cfg_len);
  528. /* clear the flag, avoid failure when send the_config of driver. */
  529. gt1x_config[0] &= 0x7F;
  530. #ifdef CONFIG_GTP_CUSTOM_CFG
  531. gt1x_config[RESOLUTION_LOC] = (u8) tpd_dts_data.tpd_resolution[0];
  532. gt1x_config[RESOLUTION_LOC + 1] = (u8) (tpd_dts_data.tpd_resolution[0] >> 8);
  533. gt1x_config[RESOLUTION_LOC + 2] = (u8) tpd_dts_data.tpd_resolution[1];
  534. gt1x_config[RESOLUTION_LOC + 3] = (u8) (tpd_dts_data.tpd_resolution[1] >> 8);
  535. GTP_INFO("Res: %d * %d, trigger: %d", tpd_dts_data.tpd_resolution[0],
  536. tpd_dts_data.tpd_resolution[1], GTP_INT_TRIGGER);
  537. if (GTP_INT_TRIGGER == 0) { /* RISING */
  538. gt1x_config[TRIGGER_LOC] &= 0xfe;
  539. } else if (GTP_INT_TRIGGER == 1) { /* FALLING */
  540. gt1x_config[TRIGGER_LOC] |= 0x01;
  541. }
  542. #endif /* END CONFIG_GTP_CUSTOM_CFG */
  543. #ifdef CONFIG_GTP_CHARGER_SWITCH
  544. GTP_DEBUG("Charger Config Groups Length: %d, %d, %d, %d, %d, %d", cfg_lens_charger[0],
  545. cfg_lens_charger[1], cfg_lens_charger[2], cfg_lens_charger[3], cfg_lens_charger[4],
  546. cfg_lens_charger[5]);
  547. memset(gt1x_config_charger, 0, sizeof(gt1x_config_charger));
  548. if (cfg_lens_charger[sensor_id] == cfg_len)
  549. memcpy(gt1x_config_charger, cfgs_charger[sensor_id], cfg_len);
  550. /* clear the flag, avoid failure when send the config of driver. */
  551. gt1x_config_charger[0] &= 0x7F;
  552. #ifdef CONFIG_GTP_CUSTOM_CFG
  553. gt1x_config_charger[RESOLUTION_LOC] = (u8) tpd_dts_data.tpd_resolution[0];
  554. gt1x_config_charger[RESOLUTION_LOC + 1] = (u8) (tpd_dts_data.tpd_resolution[0] >> 8);
  555. gt1x_config_charger[RESOLUTION_LOC + 2] = (u8) tpd_dts_data.tpd_resolution[1];
  556. gt1x_config_charger[RESOLUTION_LOC + 3] = (u8) (tpd_dts_data.tpd_resolution[1] >> 8);
  557. if (GTP_INT_TRIGGER == 0) { /* RISING */
  558. gt1x_config_charger[TRIGGER_LOC] &= 0xfe;
  559. } else if (GTP_INT_TRIGGER == 1) { /* FALLING */
  560. gt1x_config_charger[TRIGGER_LOC] |= 0x01;
  561. }
  562. #endif /* END CONFIG_GTP_CUSTOM_CFG */
  563. if (cfg_lens_charger[sensor_id] != cfg_len)
  564. memset(gt1x_config_charger, 0, sizeof(gt1x_config_charger));
  565. #endif /* END CONFIG_GTP_CHARGER_SWITCH */
  566. #else /* DRIVER NOT SEND CONFIG */
  567. cfg_len = GTP_CONFIG_MAX_LENGTH;
  568. ret = gt1x_i2c_read(GTP_REG_CONFIG_DATA, gt1x_config, cfg_len);
  569. if (ret < 0)
  570. return ret;
  571. #endif /* END CONFIG_GTP_DRIVER_SEND_CFG */
  572. GTP_DEBUG_FUNC();
  573. /* match resolution when gt1x_abs_x_max & gt1x_abs_y_max have been set already */
  574. if ((gt1x_abs_x_max == 0) && (gt1x_abs_y_max == 0)) {
  575. gt1x_abs_x_max = (gt1x_config[RESOLUTION_LOC + 1] << 8) + gt1x_config[RESOLUTION_LOC];
  576. gt1x_abs_y_max = (gt1x_config[RESOLUTION_LOC + 3] << 8) + gt1x_config[RESOLUTION_LOC + 2];
  577. gt1x_int_type = (gt1x_config[TRIGGER_LOC]) & 0x03;
  578. gt1x_wakeup_level = !(gt1x_config[MODULE_SWITCH3_LOC] & 0x20);
  579. } else {
  580. gt1x_config[RESOLUTION_LOC] = (u8) gt1x_abs_x_max;
  581. gt1x_config[RESOLUTION_LOC + 1] = (u8) (gt1x_abs_x_max >> 8);
  582. gt1x_config[RESOLUTION_LOC + 2] = (u8) gt1x_abs_y_max;
  583. gt1x_config[RESOLUTION_LOC + 3] = (u8) (gt1x_abs_y_max >> 8);
  584. set_reg_bit(gt1x_config[MODULE_SWITCH3_LOC], 5, !gt1x_wakeup_level);
  585. gt1x_config[TRIGGER_LOC] = (gt1x_config[TRIGGER_LOC] & 0xFC) | gt1x_int_type;
  586. #ifdef CONFIG_GTP_CHARGER_SWITCH
  587. gt1x_config_charger[RESOLUTION_LOC] = (u8) gt1x_abs_x_max;
  588. gt1x_config_charger[RESOLUTION_LOC + 1] = (u8) (gt1x_abs_x_max >> 8);
  589. gt1x_config_charger[RESOLUTION_LOC + 2] = (u8) gt1x_abs_y_max;
  590. gt1x_config_charger[RESOLUTION_LOC + 3] = (u8) (gt1x_abs_y_max >> 8);
  591. set_reg_bit(gt1x_config[MODULE_SWITCH3_LOC], 5, !gt1x_wakeup_level);
  592. gt1x_config[TRIGGER_LOC] = (gt1x_config[TRIGGER_LOC] & 0xFC) | gt1x_int_type;
  593. #endif
  594. }
  595. GTP_INFO("X_MAX=%d,Y_MAX=%d,TRIGGER=0x%02x,WAKEUP_LEVEL=%d", gt1x_abs_x_max, gt1x_abs_y_max, gt1x_int_type,
  596. gt1x_wakeup_level);
  597. gt1x_cfg_length = cfg_len;
  598. ret = gt1x_send_cfg(gt1x_config, gt1x_cfg_length);
  599. return ret;
  600. }
  601. void gt1x_select_addr(void)
  602. {
  603. GTP_GPIO_OUTPUT(GTP_RST_PORT, 0);
  604. msleep(20);
  605. GTP_GPIO_OUTPUT(GTP_INT_PORT, gt1x_i2c_client->addr == 0x14);
  606. msleep(20);
  607. GTP_GPIO_OUTPUT(GTP_RST_PORT, 1);
  608. }
  609. s32 gt1x_reset_guitar(void)
  610. {
  611. s32 ret = 0;
  612. GTP_INFO("GTP RESET!\n");
  613. /* select i2c address */
  614. gt1x_select_addr();
  615. msleep(20); /*must >= 6ms*/
  616. /* int synchronization */
  617. if (CHIP_TYPE_GT2X == gt1x_chip_type) {
  618. /* for GT2X */
  619. } else {
  620. GTP_GPIO_OUTPUT(GTP_INT_PORT, 0);
  621. msleep(50);
  622. GTP_GPIO_AS_INT(GTP_INT_PORT);
  623. }
  624. #ifdef CONFIG_GTP_ESD_PROTECT
  625. ret = gt1x_init_ext_watchdog();
  626. #else
  627. ret = gt1x_i2c_test();
  628. #endif
  629. return ret;
  630. }
  631. /**
  632. * gt1x_read_version - Read gt1x version info.
  633. * @ver_info: address to store version info
  634. * Return 0-succeed.
  635. */
  636. s32 gt1x_read_version(struct gt1x_version_info *ver_info)
  637. {
  638. s32 ret = -1;
  639. u8 buf[12] = { 0 };
  640. u32 mask_id = 0;
  641. u32 patch_id = 0;
  642. u8 product_id[5] = { 0 };
  643. u8 sensor_id = 0;
  644. u8 match_opt = 0;
  645. int i, retry = 3;
  646. u8 checksum = 0;
  647. GTP_DEBUG_FUNC();
  648. while (retry--) {
  649. ret = gt1x_i2c_read_dbl_check(GTP_REG_VERSION, buf, sizeof(buf));
  650. if (!ret) {
  651. checksum = 0;
  652. for (i = 0; i < sizeof(buf); i++)
  653. checksum += buf[i];
  654. if (checksum == 0 && /* first 3 bytes must be number or char */
  655. /*sensor id == 0xFF, retry */
  656. IS_NUM_OR_CHAR(buf[0]) && IS_NUM_OR_CHAR(buf[1]) && IS_NUM_OR_CHAR(buf[2]) && buf[10] != 0xFF) {
  657. break;
  658. }
  659. GTP_ERROR("GTP read version failed!(checksum error)");
  660. } else {
  661. GTP_ERROR("GTP read version failed!");
  662. }
  663. GTP_DEBUG("GTP reread version : %d", retry);
  664. msleep(100);
  665. }
  666. if (retry <= 0)
  667. return -1;
  668. mask_id = (u32) ((buf[7] << 16) | (buf[8] << 8) | buf[9]);
  669. patch_id = (u32) ((buf[4] << 16) | (buf[5] << 8) | buf[6]);
  670. memcpy(product_id, buf, 4);
  671. sensor_id = buf[10] & 0x0F;
  672. match_opt = (buf[10] >> 4) & 0x0F;
  673. GTP_INFO("IC VERSION: GT%s_%04X(Patch)_%04X(Mask)_%02X(SensorID)", product_id, patch_id >> 8, mask_id >> 8,
  674. sensor_id);
  675. if (ver_info != NULL) {
  676. ver_info->mask_id = mask_id;
  677. ver_info->patch_id = patch_id;
  678. memcpy(ver_info->product_id, product_id, 5);
  679. ver_info->sensor_id = sensor_id;
  680. ver_info->match_opt = match_opt;
  681. }
  682. return 0;
  683. }
  684. /**
  685. * gt1x_get_chip_type - get chip type .
  686. *
  687. * different chip synchronize in different way,
  688. */
  689. s32 gt1x_get_chip_type(void)
  690. {
  691. u8 opr_buf[4] = { 0x00 };
  692. u8 gt1x_data[] = { 0x02, 0x08, 0x90, 0x00 };
  693. u8 gt9l_data[] = { 0x01, 0x10, 0x90, 0x00 };
  694. s32 ret = -1;
  695. /* chip type already exist */
  696. if (gt1x_chip_type != CHIP_TYPE_NONE)
  697. return 0;
  698. /* read hardware */
  699. ret = gt1x_i2c_read_dbl_check(GTP_REG_HW_INFO, opr_buf, sizeof(opr_buf));
  700. if (ret) {
  701. GTP_ERROR("I2c communication error.");
  702. return -1;
  703. }
  704. /* find chip type */
  705. if (!memcmp(opr_buf, gt1x_data, sizeof(gt1x_data)))
  706. gt1x_chip_type = CHIP_TYPE_GT1X;
  707. else if (!memcmp(opr_buf, gt9l_data, sizeof(gt9l_data)))
  708. gt1x_chip_type = CHIP_TYPE_GT2X;
  709. if (gt1x_chip_type != CHIP_TYPE_NONE) {
  710. GTP_INFO("Chip Type: %s", (gt1x_chip_type == CHIP_TYPE_GT1X) ? "GT1X" : "GT2X");
  711. return 0;
  712. } else {
  713. return -1;
  714. }
  715. }
  716. /**
  717. * gt1x_enter_sleep - Eter sleep function.
  718. *
  719. * Returns 0--success,non-0--fail.
  720. */
  721. s32 gt1x_enter_sleep(void)
  722. {
  723. s32 ret = ERROR;
  724. if (CHIP_TYPE_GT2X == gt1x_chip_type) {
  725. /*Store bak ref*/
  726. /*ret = gt1x_bak_ref_proc(GTP_BAK_REF_STORE);*/
  727. if (ret)
  728. GTP_ERROR("[gt1x_enter_sleep]Store bak ref failed.");
  729. }
  730. #ifdef CONFIG_GTP_POWER_CTRL_SLEEP
  731. gt1x_power_switch(SWITCH_OFF);
  732. GTP_INFO("GTP enter sleep by poweroff!");
  733. return 0;
  734. #else
  735. {
  736. s32 retry = 0;
  737. if (gt1x_wakeup_level == 1) { /* high level wakeup */
  738. GTP_GPIO_OUTPUT(GTP_INT_PORT, 0);
  739. }
  740. msleep(20);
  741. while (retry++ < 5) {
  742. if (!gt1x_send_cmd(GTP_CMD_SLEEP, 0)) {
  743. GTP_INFO("GTP enter sleep!");
  744. return 0;
  745. }
  746. msleep(20);
  747. }
  748. GTP_ERROR("GTP send sleep cmd failed.");
  749. return -1;
  750. }
  751. #endif
  752. }
  753. /**
  754. * gt1x_wakeup_sleep - wakeup from sleep mode Function.
  755. *
  756. * Return: 0--success,non-0--fail.
  757. */
  758. s32 gt1x_wakeup_sleep(void)
  759. {
  760. #ifndef CONFIG_GTP_POWER_CTRL_SLEEP
  761. u8 retry = 0;
  762. s32 ret = -1;
  763. #endif
  764. GTP_DEBUG("GTP wakeup begin.");
  765. #ifdef CONFIG_GTP_POWER_CTRL_SLEEP /* power manager unit control the procedure */
  766. gt1x_power_reset();
  767. GTP_INFO("Ic wakeup by poweron");
  768. return 0;
  769. #else /* gesture wakeup & int port wakeup */
  770. while (retry++ < 2) {
  771. #ifdef CONFIG_GTP_GESTURE_WAKEUP
  772. if (gesture_enabled) {
  773. if (DOZE_WAKEUP != gesture_doze_status)
  774. GTP_INFO("Powerkey wakeup.");
  775. else
  776. GTP_INFO("Gesture wakeup.");
  777. gesture_doze_status = DOZE_DISABLED;
  778. ret = gt1x_reset_guitar();
  779. if (!ret)
  780. break;
  781. } else
  782. #endif
  783. {
  784. /* wake up through int port */
  785. GTP_GPIO_OUTPUT(GTP_INT_PORT, gt1x_wakeup_level);
  786. msleep(20);
  787. if (CHIP_TYPE_GT2X == gt1x_chip_type) {
  788. /* for GT2X */
  789. } else {
  790. /* Synchronize int IO */
  791. GTP_GPIO_OUTPUT(GTP_INT_PORT, 0);
  792. msleep(50);
  793. GTP_GPIO_AS_INT(GTP_INT_PORT);
  794. }
  795. /* test i2c */
  796. ret = gt1x_i2c_test();
  797. if (!ret) {
  798. /* i2c test succeed, init externl watchdog */
  799. #ifdef CONFIG_GTP_ESD_PROTECT
  800. ret = gt1x_init_ext_watchdog();
  801. if (!ret)
  802. break;
  803. #else
  804. break;
  805. #endif
  806. }
  807. }
  808. }
  809. if (ret) { /* wakeup failed , try waking up by resetting */
  810. while (retry--) {
  811. ret = gt1x_reset_guitar();
  812. if (!ret)
  813. break;
  814. }
  815. }
  816. if (ret) {
  817. GTP_ERROR("GTP wakeup sleep failed.");
  818. return -1;
  819. }
  820. GTP_INFO("GTP wakeup sleep.");
  821. return 0;
  822. #endif /* END CONFIG_GTP_POWER_CTRL_SLEEP */
  823. }
  824. /**
  825. * gt1x_send_cmd - seng cmd
  826. * must write data & checksum first
  827. * byte content
  828. * 0 cmd
  829. * 1 data
  830. * 2 checksum
  831. * Returns 0 - succeed,non-0 - failed
  832. */
  833. s32 gt1x_send_cmd(u8 cmd, u8 data)
  834. {
  835. s32 ret;
  836. static DEFINE_MUTEX(cmd_mutex);
  837. u8 buffer[3] = { cmd, data, 0 };
  838. mutex_lock(&cmd_mutex);
  839. buffer[2] = (u8) ((0 - cmd - data) & 0xFF);
  840. ret = gt1x_i2c_write(GTP_REG_CMD + 1, &buffer[1], 2);
  841. ret |= gt1x_i2c_write(GTP_REG_CMD, &buffer[0], 1);
  842. msleep(50);
  843. mutex_unlock(&cmd_mutex);
  844. return ret;
  845. }
  846. void gt1x_power_reset(void)
  847. {
  848. s32 i = 0;
  849. s32 ret = 0;
  850. if (is_resetting)
  851. return;
  852. GTP_INFO("force_reset_guitar");
  853. is_resetting = 1;
  854. gt1x_irq_disable();
  855. gt1x_power_switch(SWITCH_OFF);
  856. msleep(30);
  857. gt1x_power_switch(SWITCH_ON);
  858. msleep(30);
  859. for (i = 0; i < 5; i++) {
  860. ret = gt1x_reset_guitar();
  861. if (ret < 0)
  862. continue;
  863. ret = gt1x_send_cfg(gt1x_config, gt1x_cfg_length);
  864. if (ret < 0) {
  865. msleep(500);
  866. continue;
  867. }
  868. break;
  869. }
  870. gt1x_irq_enable();
  871. is_resetting = 0;
  872. }
  873. s32 gt1x_request_event_handler(void)
  874. {
  875. s32 ret = -1;
  876. u8 rqst_data = 0;
  877. ret = gt1x_i2c_read(GTP_REG_RQST, &rqst_data, 1);
  878. if (ret) {
  879. GTP_ERROR("I2C transfer error. errno:%d", ret);
  880. return -1;
  881. }
  882. GTP_DEBUG("Request state:0x%02x.", rqst_data);
  883. switch (rqst_data & 0x0F) {
  884. case GTP_RQST_CONFIG:
  885. GTP_INFO("Request Config.");
  886. ret = gt1x_send_cfg(gt1x_config, gt1x_cfg_length);
  887. if (ret) {
  888. GTP_ERROR("Send gt1x_config error.");
  889. } else {
  890. GTP_INFO("Send gt1x_config success.");
  891. rqst_data = GTP_RQST_RESPONDED;
  892. gt1x_i2c_write(GTP_REG_RQST, &rqst_data, 1);
  893. }
  894. break;
  895. case GTP_RQST_RESET:
  896. GTP_INFO("Request Reset.");
  897. gt1x_reset_guitar();
  898. rqst_data = GTP_RQST_RESPONDED;
  899. gt1x_i2c_write(GTP_REG_RQST, &rqst_data, 1);
  900. break;
  901. case GTP_RQST_BAK_REF:
  902. GTP_INFO("Request Ref.");
  903. break;
  904. case GTP_RQST_MAIN_CLOCK:
  905. GTP_INFO("Request main clock.");
  906. break;
  907. #ifdef CONFIG_GTP_HOTKNOT
  908. case GTP_RQST_HOTKNOT_CODE:
  909. GTP_INFO("Request HotKnot Code.");
  910. break;
  911. #endif
  912. default:
  913. break;
  914. }
  915. return 0;
  916. }
  917. /**
  918. * gt1x_touch_event_handler - handle touch event
  919. * (pen event, key event, finger touch envent)
  920. * @data:
  921. * Return <0: failed, 0: succeed
  922. */
  923. s32 gt1x_touch_event_handler(u8 *data, struct input_dev *dev, struct input_dev *pen_dev)
  924. {
  925. u8 touch_data[1 + 8 * GTP_MAX_TOUCH + 2] = { 0 };
  926. u8 touch_num = 0;
  927. u16 cur_event = 0;
  928. static u16 pre_event;
  929. static u16 pre_index;
  930. u8 key_value = 0;
  931. u8 *coor_data = NULL;
  932. s32 input_x = 0;
  933. s32 input_y = 0;
  934. s32 input_w = 0;
  935. s32 id = 0;
  936. s32 i = 0;
  937. s32 ret = -1;
  938. GTP_DEBUG_FUNC();
  939. touch_num = data[0] & 0x0f;
  940. if (touch_num > GTP_MAX_TOUCH) {
  941. GTP_ERROR("Illegal finger number = %d!", touch_num);
  942. return ERROR_VALUE;
  943. }
  944. memcpy(touch_data, data, 11);
  945. /* read the remaining coor data */
  946. if (touch_num > 1) {
  947. ret = gt1x_i2c_read((GTP_READ_COOR_ADDR + 11), &touch_data[11], 1 + 8 * touch_num + 2 - 11);
  948. if (ret) {
  949. GTP_ERROR("Read coordinate i2c error.");
  950. return ret;
  951. }
  952. }
  953. /* checksum */
  954. /*
  955. * cur_event , pre_event bit defination
  956. * bit4 bit3 bit2 bit1 bit0
  957. * hover stylus_key stylus key touch
  958. *
  959. */
  960. key_value = touch_data[1 + 8 * touch_num];
  961. /* check current event */
  962. if ((touch_data[0] & 0x10) && key_value) {
  963. #ifdef CONFIG_GTP_HAVE_STYLUS_KEY
  964. /* get current key states */
  965. if (key_value & 0xF0)
  966. SET_BIT(cur_event, BIT_STYLUS_KEY);
  967. else if (key_value & 0x0F)
  968. SET_BIT(cur_event, BIT_TOUCH_KEY);
  969. #endif
  970. if (tpd_dts_data.use_tpd_button) {
  971. /* get current key states */
  972. if (key_value & 0xF0)
  973. SET_BIT(cur_event, BIT_STYLUS_KEY);
  974. else if (key_value & 0x0F)
  975. SET_BIT(cur_event, BIT_TOUCH_KEY);
  976. }
  977. }
  978. #ifdef CONFIG_GTP_WITH_STYLUS
  979. else if (touch_data[1] & 0x80)
  980. SET_BIT(cur_event, BIT_STYLUS);
  981. #endif
  982. else if (touch_num)
  983. SET_BIT(cur_event, BIT_TOUCH);
  984. /* handle current event and pre-event */
  985. #ifdef CONFIG_GTP_HAVE_STYLUS_KEY
  986. if (CHK_BIT(cur_event, BIT_STYLUS_KEY) || CHK_BIT(pre_event, BIT_STYLUS_KEY)) {
  987. /*
  988. * 0x10 -- stylus key0 down
  989. * 0x20 -- stylus key1 down
  990. * 0x40 -- stylus key0 & stylus key1 both down
  991. */
  992. u8 temp = (key_value & 0x40) ? 0x30 : key_value;
  993. for (i = 4; i < 6; i++)
  994. input_report_key(pen_dev, gt1x_stylus_key_array[i - 4], temp & (0x01 << i));
  995. GTP_DEBUG("Stulus key event.");
  996. }
  997. #endif
  998. #ifdef CONFIG_GTP_WITH_STYLUS
  999. if (CHK_BIT(cur_event, BIT_STYLUS)) {
  1000. coor_data = &touch_data[1];
  1001. id = coor_data[0] & 0x7F;
  1002. input_x = coor_data[1] | (coor_data[2] << 8);
  1003. input_y = coor_data[3] | (coor_data[4] << 8);
  1004. input_w = coor_data[5] | (coor_data[6] << 8);
  1005. input_x = GTP_WARP_X(gt1x_abs_x_max, input_x);
  1006. input_y = GTP_WARP_Y(gt1x_abs_y_max, input_y);
  1007. GTP_DEBUG("Pen touch DOWN.");
  1008. gt1x_pen_down(input_x, input_y, input_w, 0);
  1009. } else if (CHK_BIT(pre_event, BIT_STYLUS)) {
  1010. GTP_DEBUG("Pen touch UP.");
  1011. gt1x_pen_up(0);
  1012. }
  1013. #endif
  1014. if (tpd_dts_data.use_tpd_button) {
  1015. if (CHK_BIT(cur_event, BIT_TOUCH_KEY) || CHK_BIT(pre_event, BIT_TOUCH_KEY)) {
  1016. for (i = 0; i < tpd_dts_data.tpd_key_num; i++)
  1017. input_report_key(dev, tpd_dts_data.tpd_key_local[i], key_value & (0x01 << i));
  1018. if (CHK_BIT(cur_event, BIT_TOUCH_KEY))
  1019. GTP_DEBUG("Key Down.");
  1020. else
  1021. GTP_DEBUG("Key Up.");
  1022. }
  1023. }
  1024. /* finger touch event*/
  1025. if (CHK_BIT(cur_event, BIT_TOUCH)) {
  1026. u8 report_num = 0;
  1027. coor_data = &touch_data[1];
  1028. id = coor_data[0] & 0x0F;
  1029. for (i = 0; i < GTP_MAX_TOUCH; i++) {
  1030. if (i == id) {
  1031. input_x = coor_data[1] | (coor_data[2] << 8);
  1032. input_y = coor_data[3] | (coor_data[4] << 8);
  1033. input_w = coor_data[5] | (coor_data[6] << 8);
  1034. input_x = GTP_WARP_X(gt1x_abs_x_max, input_x);
  1035. input_y = GTP_WARP_Y(gt1x_abs_y_max, input_y);
  1036. GTP_DEBUG("(%d)(%d, %d)[%d]", id, input_x, input_y, input_w);
  1037. gt1x_touch_down(input_x, input_y, input_w, i);
  1038. if (report_num++ < touch_num) {
  1039. coor_data += 8;
  1040. id = coor_data[0] & 0x0F;
  1041. }
  1042. pre_index |= 0x01 << i;
  1043. } else if (pre_index & (0x01 << i)) {
  1044. #ifdef CONFIG_GTP_ICS_SLOT_REPORT
  1045. gt1x_touch_up(i);
  1046. #endif
  1047. pre_index &= ~(0x01 << i);
  1048. }
  1049. }
  1050. } else if (CHK_BIT(pre_event, BIT_TOUCH)) {
  1051. #ifdef CONFIG_GTP_ICS_SLOT_REPORT
  1052. int cycles = pre_index < 3 ? 3 : GTP_MAX_TOUCH;
  1053. for (i = 0; i < cycles; i++) {
  1054. if (pre_index >> i & 0x01)
  1055. gt1x_touch_up(i);
  1056. }
  1057. #else
  1058. gt1x_touch_up(0);
  1059. #endif
  1060. GTP_DEBUG("Released Touch.");
  1061. pre_index = 0;
  1062. }
  1063. /* input sync report */
  1064. if (CHK_BIT(cur_event, BIT_STYLUS_KEY | BIT_STYLUS)
  1065. || CHK_BIT(pre_event, BIT_STYLUS_KEY | BIT_STYLUS)) {
  1066. input_sync(pen_dev);
  1067. }
  1068. if (CHK_BIT(cur_event, BIT_TOUCH_KEY | BIT_TOUCH)
  1069. || CHK_BIT(pre_event, BIT_TOUCH_KEY | BIT_TOUCH)) {
  1070. input_sync(dev);
  1071. }
  1072. if (!pre_event && !cur_event)
  1073. GTP_DEBUG("Additional Int Pulse.");
  1074. else
  1075. pre_event = cur_event;
  1076. return 0;
  1077. }
  1078. #ifdef CONFIG_GTP_WITH_STYLUS
  1079. struct input_dev *pen_dev;
  1080. void gt1x_pen_init(void)
  1081. {
  1082. s32 ret = 0;
  1083. pen_dev = input_allocate_device();
  1084. if (pen_dev == NULL) {
  1085. GTP_ERROR("Failed to allocate input device for pen/stylus.");
  1086. return;
  1087. }
  1088. pen_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  1089. pen_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  1090. set_bit(BTN_TOOL_PEN, pen_dev->keybit);
  1091. set_bit(INPUT_PROP_DIRECT, pen_dev->propbit);
  1092. #ifdef CONFIG_GTP_HAVE_STYLUS_KEY
  1093. input_set_capability(pen_dev, EV_KEY, BTN_STYLUS);
  1094. input_set_capability(pen_dev, EV_KEY, BTN_STYLUS2);
  1095. #endif
  1096. input_set_abs_params(pen_dev, ABS_MT_POSITION_X, 0, gt1x_abs_x_max, 0, 0);
  1097. input_set_abs_params(pen_dev, ABS_MT_POSITION_Y, 0, gt1x_abs_y_max, 0, 0);
  1098. input_set_abs_params(pen_dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
  1099. input_set_abs_params(pen_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  1100. input_set_abs_params(pen_dev, ABS_MT_TRACKING_ID, 0, 255, 0, 0);
  1101. pen_dev->name = "goodix-pen";
  1102. pen_dev->phys = "input/ts";
  1103. pen_dev->id.bustype = BUS_I2C;
  1104. ret = input_register_device(pen_dev);
  1105. if (ret) {
  1106. GTP_ERROR("Register %s input device failed", pen_dev->name);
  1107. return;
  1108. }
  1109. }
  1110. void gt1x_pen_down(s32 x, s32 y, s32 size, s32 id)
  1111. {
  1112. input_report_key(pen_dev, BTN_TOOL_PEN, 1);
  1113. #ifdef CONFIG_GTP_CHANGE_X2Y
  1114. GTP_SWAP(x, y);
  1115. #endif
  1116. #ifdef CONFIG_GTP_ICS_SLOT_REPORT
  1117. input_mt_slot(pen_dev, id);
  1118. input_report_abs(pen_dev, ABS_MT_PRESSURE, size);
  1119. input_report_abs(pen_dev, ABS_MT_TOUCH_MAJOR, size);
  1120. input_report_abs(pen_dev, ABS_MT_TRACKING_ID, id);
  1121. input_report_abs(pen_dev, ABS_MT_POSITION_X, x);
  1122. input_report_abs(pen_dev, ABS_MT_POSITION_Y, y);
  1123. #else
  1124. input_report_key(pen_dev, BTN_TOUCH, 1);
  1125. if ((!size) && (!id)) {
  1126. /* for virtual button */
  1127. input_report_abs(pen_dev, ABS_MT_PRESSURE, 100);
  1128. input_report_abs(pen_dev, ABS_MT_TOUCH_MAJOR, 100);
  1129. } else {
  1130. input_report_abs(pen_dev, ABS_MT_PRESSURE, size);
  1131. input_report_abs(pen_dev, ABS_MT_TOUCH_MAJOR, size);
  1132. input_report_abs(pen_dev, ABS_MT_TRACKING_ID, id);
  1133. }
  1134. input_report_abs(pen_dev, ABS_MT_POSITION_X, x);
  1135. input_report_abs(pen_dev, ABS_MT_POSITION_Y, y);
  1136. input_mt_sync(pen_dev);
  1137. #endif
  1138. }
  1139. void gt1x_pen_up(s32 id)
  1140. {
  1141. input_report_key(pen_dev, BTN_TOOL_PEN, 0);
  1142. #ifdef CONFIG_GTP_ICS_SLOT_REPORT
  1143. input_mt_slot(pen_dev, id);
  1144. input_report_abs(pen_dev, ABS_MT_TRACKING_ID, -1);
  1145. #else
  1146. input_report_key(pen_dev, BTN_TOUCH, 0);
  1147. input_mt_sync(pen_dev);
  1148. #endif
  1149. }
  1150. #endif
  1151. /**
  1152. * PROXIMITY
  1153. */
  1154. #ifdef CONFIG_GTP_PROXIMITY
  1155. #define GTP_REG_PROXIMITY_VALID 0x814E
  1156. #define GTP_REG_PROXIMITY_ENABLE 0x8049
  1157. u8 gt1x_proximity_flag = 0;
  1158. u8 gt1x_proximity_detect = 1; /*0-->close ; 1--> far away*/
  1159. static struct hwmsen_object obj_ps;
  1160. s32 gt1x_ps_operate(void *self, u32 command, void *buff_in, s32 size_in, void *buff_out, s32 size_out, s32 *actualout)
  1161. {
  1162. s32 err = 0;
  1163. s32 value;
  1164. hwm_sensor_data *sensor_data;
  1165. GTP_INFO("psensor operator cmd:%d", command);
  1166. switch (command) {
  1167. case SENSOR_DELAY:
  1168. if ((buff_in == NULL) || (size_in < sizeof(int))) {
  1169. GTP_ERROR("Set delay parameter error!");
  1170. err = -EINVAL;
  1171. }
  1172. break;
  1173. case SENSOR_ENABLE:
  1174. if ((buff_in == NULL) || (size_in < sizeof(int))) {
  1175. GTP_ERROR("Enable sensor parameter error!");
  1176. err = -EINVAL;
  1177. } else {
  1178. value = *(int *)buff_in;
  1179. err = gt1x_enable_ps(value);
  1180. }
  1181. break;
  1182. case SENSOR_GET_DATA:
  1183. if ((buff_out == NULL) || (size_out < sizeof(hwm_sensor_data))) {
  1184. GTP_ERROR("Get sensor data parameter error!");
  1185. err = -EINVAL;
  1186. } else {
  1187. sensor_data = (hwm_sensor_data *) buff_out;
  1188. sensor_data->values[0] = gt1x_get_ps_value();
  1189. sensor_data->value_divide = 1;
  1190. sensor_data->status = SENSOR_STATUS_ACCURACY_MEDIUM;
  1191. }
  1192. break;
  1193. default:
  1194. GTP_ERROR("proxmy sensor operate function no this parameter %d!\n", command);
  1195. err = -1;
  1196. break;
  1197. }
  1198. return err;
  1199. }
  1200. void gt1x_ps_init(void)
  1201. {
  1202. s32 err = 0;
  1203. /*obj_ps.self = cm3623_obj;*/
  1204. obj_ps.polling = 0; /*0--interrupt mode;1--polling mode;*/
  1205. obj_ps.sensor_operate = gt1x_ps_operate;
  1206. err = hwmsen_attach(ID_PROXIMITY, &obj_ps);
  1207. if (err)
  1208. GTP_ERROR("hwmsen attach fail, return:%d.", err);
  1209. }
  1210. void gt1x_report_ps(u8 state)
  1211. {
  1212. s32 ret = -1;
  1213. hwm_sensor_data sensor_data;
  1214. /*get raw data*/
  1215. GTP_DEBUG("P-sensor state:%s", state ? "AWAY" : "NEAR");
  1216. /*map and store data to hwm_sensor_data*/
  1217. sensor_data.values[0] = state;
  1218. sensor_data.value_divide = 1;
  1219. sensor_data.status = SENSOR_STATUS_ACCURACY_MEDIUM;
  1220. /*report to the up-layer*/
  1221. ret = hwmsen_get_interrupt_data(ID_PROXIMITY, &sensor_data);
  1222. if (ret)
  1223. GTP_ERROR("Call hwmsen_get_interrupt_data fail = %d\n", ret);
  1224. }
  1225. static s32 gt1x_get_ps_value(void)
  1226. {
  1227. return gt1x_proximity_detect;
  1228. }
  1229. static s32 gt1x_enable_ps(s32 enable)
  1230. {
  1231. u8 state;
  1232. s32 ret = -1;
  1233. GTP_INFO("TPD proximity function to be %s.", enable ? "on" : "off");
  1234. state = enable ? 1 : 0;
  1235. ret = gt1x_i2c_write(GTP_REG_PROXIMITY_ENABLE, &state, 1);
  1236. if (ret)
  1237. GTP_ERROR("TPD %s proximity cmd failed.", state ? "enable" : "disable");
  1238. if (enable) {
  1239. if (!ret) {
  1240. gt1x_proximity_flag = 1;
  1241. gt1x_proximity_detect = 1;
  1242. }
  1243. } else {
  1244. gt1x_proximity_flag = 0;
  1245. }
  1246. GTP_INFO("TPD proximity function %s %s.", state ? "enable" : "disable", ret ? "fail" : "success");
  1247. return ret;
  1248. }
  1249. int gt1x_prox_event_handler(u8 *data)
  1250. {
  1251. u8 proximity_status = 0;
  1252. if (gt1x_proximity_flag) {
  1253. GTP_DEBUG("REG INDEX[0x814E]:0x%02X\n", data[0]);
  1254. proximity_status = (data[0] & 0x60) ? 0 : 1;
  1255. if (proximity_status != gt1x_proximity_detect) {
  1256. gt1x_report_ps(proximity_status);
  1257. gt1x_proximity_detect = proximity_status;
  1258. }
  1259. if (proximity_status == 0)
  1260. return 1;
  1261. else
  1262. return 0;
  1263. }
  1264. return -1;
  1265. }
  1266. #endif /*CONFIG_GTP_PROXIMITY */
  1267. /**
  1268. * ESD PROTECT
  1269. */
  1270. #ifdef CONFIG_GTP_ESD_PROTECT
  1271. static int esd_work_cycle = 200;
  1272. static struct delayed_work esd_check_work;
  1273. static int esd_running;
  1274. struct mutex esd_lock;
  1275. static void gt1x_esd_check_func(struct work_struct *);
  1276. void gt1x_init_esd_protect(void)
  1277. {
  1278. esd_work_cycle = 2 * HZ; /*HZ: clock ticks in 1 second generated by system*/
  1279. GTP_DEBUG("Clock ticks for an esd cycle: %d", esd_work_cycle);
  1280. INIT_DELAYED_WORK(&esd_check_work, gt1x_esd_check_func);
  1281. mutex_init(&esd_lock);
  1282. }
  1283. void gt1x_deinit_esd_protect(void)
  1284. {
  1285. gt1x_esd_switch(SWITCH_OFF);
  1286. }
  1287. s32 gt1x_init_ext_watchdog(void)
  1288. {
  1289. s32 ret;
  1290. u8 value = 0xAA;
  1291. GTP_DEBUG("Init external watchdog.");
  1292. ret = gt1x_send_cmd(GTP_CMD_ESD, 0);
  1293. ret |= gt1x_i2c_write(GTP_REG_ESD_CHECK, &value, 1);
  1294. return ret;
  1295. }
  1296. void gt1x_esd_switch(s32 on)
  1297. {
  1298. mutex_lock(&esd_lock);
  1299. if (SWITCH_ON == on) { /* switch on esd check */
  1300. if (!esd_running) {
  1301. esd_running = 1;
  1302. GTP_INFO("Esd protector started!");
  1303. queue_delayed_work(gt1x_workqueue, &esd_check_work, esd_work_cycle);
  1304. }
  1305. } else { /* switch off esd check */
  1306. if (esd_running) {
  1307. esd_running = 0;
  1308. GTP_INFO("Esd protector stopped!");
  1309. cancel_delayed_work(&esd_check_work);
  1310. }
  1311. }
  1312. mutex_unlock(&esd_lock);
  1313. }
  1314. static void gt1x_esd_check_func(struct work_struct *work)
  1315. {
  1316. s32 i = 0;
  1317. s32 ret = -1;
  1318. u8 esd_buf[4] = { 0 };
  1319. if (!esd_running) {
  1320. GTP_INFO("Esd protector suspended!");
  1321. return;
  1322. }
  1323. for (i = 0; i < 3; i++) {
  1324. ret = gt1x_i2c_read(GTP_REG_CMD, esd_buf, 4);
  1325. GTP_DEBUG("[Esd]0x8040 = 0x%02X, 0x8043 = 0x%02X", esd_buf[0], esd_buf[3]);
  1326. if (!ret && esd_buf[0] != 0xAA && esd_buf[3] == 0xAA)
  1327. break;
  1328. msleep(50);
  1329. }
  1330. if (i < 3) {
  1331. /* IC works normally, Write 0x8040 0xAA, feed the watchdog */
  1332. gt1x_send_cmd(GTP_CMD_ESD, 0);
  1333. } else {
  1334. if (esd_running) {
  1335. GTP_INFO("IC works abnormally! Process reset guitar.");
  1336. memset(esd_buf, 0x01, sizeof(esd_buf));
  1337. gt1x_i2c_write(0x4226, esd_buf, sizeof(esd_buf));
  1338. msleep(50);
  1339. gt1x_power_reset();
  1340. } else {
  1341. GTP_INFO("Esd protector suspended, no need reset!");
  1342. }
  1343. }
  1344. mutex_lock(&esd_lock);
  1345. if (esd_running)
  1346. queue_delayed_work(gt1x_workqueue, &esd_check_work, esd_work_cycle);
  1347. else
  1348. GTP_INFO("Esd protector suspended!");
  1349. mutex_unlock(&esd_lock);
  1350. }
  1351. #endif
  1352. /**
  1353. * CHARGER SWITCH
  1354. */
  1355. #ifdef CONFIG_GTP_CHARGER_SWITCH
  1356. u8 gt1x_config_charger[GTP_CONFIG_MAX_LENGTH] = { 0 };
  1357. static struct delayed_work charger_switch_work;
  1358. static int charger_work_cycle = 200;
  1359. static spinlock_t charger_lock;
  1360. static int charger_running;
  1361. static void gt1x_charger_work_func(struct work_struct *);
  1362. void gt1x_init_charger(void)
  1363. {
  1364. charger_work_cycle = 2 * HZ; /*HZ: clock ticks in 1 second generated by system*/
  1365. GTP_DEBUG("Clock ticks for an charger cycle: %d", charger_work_cycle);
  1366. INIT_DELAYED_WORK(&charger_switch_work, gt1x_charger_work_func);
  1367. spin_lock_init(&charger_lock);
  1368. }
  1369. /**
  1370. * gt1x_charger_switch - switch states of charging work thread
  1371. *
  1372. * @on: SWITCH_ON - start work thread, SWITCH_OFF: stop .
  1373. *
  1374. */
  1375. void gt1x_charger_switch(s32 on)
  1376. {
  1377. spin_lock(&charger_lock);
  1378. if (SWITCH_ON == on) {
  1379. if (!charger_running) {
  1380. charger_running = 1;
  1381. spin_unlock(&charger_lock);
  1382. GTP_INFO("Charger checker started!");
  1383. queue_delayed_work(gt1x_workqueue, &charger_switch_work, charger_work_cycle);
  1384. } else {
  1385. spin_unlock(&charger_lock);
  1386. }
  1387. } else {
  1388. if (charger_running) {
  1389. charger_running = 0;
  1390. spin_unlock(&charger_lock);
  1391. cancel_delayed_work(&charger_switch_work);
  1392. GTP_INFO("Charger checker stopped!");
  1393. } else {
  1394. spin_unlock(&charger_lock);
  1395. }
  1396. }
  1397. }
  1398. /**
  1399. * gt1x_charger_config - check and update charging status configuration
  1400. * @dir_update
  1401. * 0: check before send charging status configuration
  1402. * 1: directly send charging status configuration
  1403. *
  1404. */
  1405. void gt1x_charger_config(s32 dir_update)
  1406. {
  1407. static u8 chr_pluggedin;
  1408. if (gt1x_get_charger_status()) {
  1409. if (!chr_pluggedin || dir_update) {
  1410. GTP_INFO("Charger Plugin.");
  1411. if (gt1x_send_cfg(gt1x_config_charger, gt1x_cfg_length))
  1412. GTP_ERROR("Send config for Charger Plugin failed!");
  1413. if (gt1x_send_cmd(GTP_CMD_CHARGER_ON, 0))
  1414. GTP_ERROR("Update status for Charger Plugin failed!");
  1415. chr_pluggedin = 1;
  1416. }
  1417. } else {
  1418. if (chr_pluggedin || dir_update) {
  1419. GTP_INFO("Charger Plugout.");
  1420. if (gt1x_send_cfg(gt1x_config, gt1x_cfg_length))
  1421. GTP_INFO("Send config for Charger Plugout failed!");
  1422. if (gt1x_send_cmd(GTP_CMD_CHARGER_OFF, 0))
  1423. GTP_ERROR("Update status for Charger Plugout failed!");
  1424. chr_pluggedin = 0;
  1425. }
  1426. }
  1427. }
  1428. static void gt1x_charger_work_func(struct work_struct *work)
  1429. {
  1430. if (!charger_running) {
  1431. GTP_INFO("Charger checker suspended!");
  1432. return;
  1433. }
  1434. gt1x_charger_config(0);
  1435. GTP_DEBUG("Charger check done!");
  1436. if (charger_running)
  1437. queue_delayed_work(gt1x_workqueue, &charger_switch_work, charger_work_cycle);
  1438. }
  1439. #endif
  1440. s32 gt1x_init(void)
  1441. {
  1442. s32 ret = -1;
  1443. s32 retry = 0;
  1444. u8 reg_val[1];
  1445. gt1x_power_switch(SWITCH_ON);
  1446. /* select i2c address */
  1447. gt1x_select_addr();
  1448. msleep(20);
  1449. while (retry++ < 5) {
  1450. gt1x_init_failed = 0;
  1451. /* get chip type */
  1452. ret = gt1x_get_chip_type();
  1453. if (ret != 0) {
  1454. GTP_ERROR("GTP get chip type failed!");
  1455. continue;
  1456. }
  1457. /* reset ic */
  1458. ret = gt1x_reset_guitar();
  1459. if (ret != 0) {
  1460. GTP_ERROR("GTP reset guitar failed!");
  1461. continue;
  1462. } else {
  1463. tpd_load_status = 1;
  1464. check_flag = true;
  1465. wake_up_interruptible(&init_waiter);
  1466. }
  1467. ret = gt1x_i2c_read_dbl_check(0x41E4, reg_val, 1);
  1468. if (ret != 0) {
  1469. continue;
  1470. } else if (reg_val[0] != 0xBE) {
  1471. GTP_ERROR("Check 0x41E4 failed.");
  1472. gt1x_init_failed = 1;
  1473. break;
  1474. }
  1475. /* read version information */
  1476. ret = gt1x_read_version(&gt1x_version);
  1477. if (ret != 0) {
  1478. GTP_ERROR("GTP get verision failed!");
  1479. gt1x_init_failed = 1;
  1480. continue;
  1481. }
  1482. /* init and send configs */
  1483. ret = gt1x_init_panel();
  1484. if (ret != 0) {
  1485. GTP_ERROR("GTP init panel failed.");
  1486. continue;
  1487. } else {
  1488. break;
  1489. }
  1490. }
  1491. /* if the initialization fails, set default setting */
  1492. ret |= gt1x_init_failed;
  1493. if (ret) {
  1494. GTP_INFO("Init failed, use default setting");
  1495. gt1x_abs_x_max = tpd_dts_data.tpd_resolution[0];
  1496. gt1x_abs_y_max = tpd_dts_data.tpd_resolution[1];
  1497. gt1x_int_type = GTP_INT_TRIGGER;
  1498. gt1x_wakeup_level = GTP_WAKEUP_LEVEL;
  1499. }
  1500. gt1x_workqueue = create_singlethread_workqueue("gt1x_workthread");
  1501. if (gt1x_workqueue == NULL)
  1502. GTP_ERROR("create workqueue failed!");
  1503. /* init auxiliary node and functions */
  1504. gt1x_init_debug_node();
  1505. #ifdef CONFIG_GTP_CREATE_WR_NODE
  1506. gt1x_init_tool_node();
  1507. #endif
  1508. #if defined(CONFIG_GTP_GESTURE_WAKEUP) || defined(CONFIG_GTP_HOTKNOT)
  1509. gt1x_init_node();
  1510. #endif
  1511. #ifdef CONFIG_GTP_PROXIMITY
  1512. gt1x_ps_init();
  1513. #endif
  1514. #ifdef CONFIG_GTP_CHARGER_SWITCH
  1515. gt1x_charger_config(1);
  1516. gt1x_init_charger();
  1517. gt1x_charger_switch(SWITCH_ON);
  1518. #endif
  1519. #ifdef CONFIG_GTP_WITH_STYLUS
  1520. gt1x_pen_init();
  1521. #endif
  1522. return ret;
  1523. }
  1524. void gt1x_deinit(void)
  1525. {
  1526. #ifdef CONFIG_GTP_CREATE_WR_NODE
  1527. gt1x_deinit_tool_node();
  1528. #endif
  1529. #ifdef CONFIG_GTP_ESD_PROTECT
  1530. gt1x_deinit_esd_protect();
  1531. #endif
  1532. #ifdef CONFIG_GTP_CHARGER_SWITCH
  1533. gt1x_charger_switch(SWITCH_OFF);
  1534. #endif
  1535. if (gt1x_workqueue)
  1536. destroy_workqueue(gt1x_workqueue);
  1537. }