gt9xx_update.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  1. /* drivers/input/touchscreen/gt813_827_828_update.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.6
  17. * Revision Record:
  18. * V1.0: first release. by Andrew, 2012/08/27.
  19. * V1.2: modify gt9110p pid map, by Andrew, 2012/10/15
  20. * V1.4:
  21. * 1. modify gup_enter_update_mode,
  22. * 2. rewrite i2c read/write func
  23. * 3. check update file checksum
  24. * by Andrew, 2012/12/12
  25. * v1.6:
  26. * 1. delete GTP_FW_DOWNLOAD related things.
  27. * 2. add GTP_DEF_FW_UPDATE switch to update fw by gtp_default_fw in *.h directly
  28. * by Meta, 2013/04/18
  29. */
  30. #include "tpd.h"
  31. #include <linux/interrupt.h>
  32. #include <linux/i2c.h>
  33. #include <linux/sched.h>
  34. #include <linux/kthread.h>
  35. #include <linux/rtpm_prio.h>
  36. #include <linux/wait.h>
  37. #include <linux/time.h>
  38. #include <linux/delay.h>
  39. #include <linux/namei.h>
  40. #include <linux/mount.h>
  41. #include <linux/uaccess.h>
  42. #include "tpd_custom_gt9xx.h"
  43. #if GTP_COMPATIBLE_MODE
  44. unsigned char gtp_default_FW_fl[] = {
  45. #include "gt9xx_firmware.h"
  46. };
  47. #endif
  48. #define GUP_REG_HW_INFO 0x4220
  49. #define GUP_REG_FW_MSG 0x41E4
  50. #define GUP_REG_PID_VID 0x8140
  51. #define GUP_SEARCH_FILE_TIMES 50
  52. #define UPDATE_FILE_PATH_1 "/data/_goodix_update_.bin"
  53. #define UPDATE_FILE_PATH_2 "/sdcard/_goodix_update_.bin"
  54. #define CONFIG_FILE_PATH_1 "/data/_goodix_config_.cfg"
  55. #define CONFIG_FILE_PATH_2 "/sdcard/_goodix_config_.cfg"
  56. #define FW_HEAD_LENGTH 14
  57. #define FW_DOWNLOAD_LENGTH 0x4000
  58. #define FW_SECTION_LENGTH 0x2000
  59. #define FW_DSP_ISP_LENGTH 0x1000
  60. #define FW_DSP_LENGTH 0x1000
  61. #define FW_BOOT_LENGTH 0x800
  62. #define PACK_SIZE 256
  63. #define MAX_FRAME_CHECK_TIME 5
  64. #define _bRW_MISCTL__SRAM_BANK 0x4048
  65. #define _bRW_MISCTL__MEM_CD_EN 0x4049
  66. #define _bRW_MISCTL__CACHE_EN 0x404B
  67. #define _bRW_MISCTL__TMR0_EN 0x40B0
  68. #define _rRW_MISCTL__SWRST_B0_ 0x4180
  69. #define _bWO_MISCTL__CPU_SWRST_PULSE 0x4184
  70. #define _rRW_MISCTL__BOOTCTL_B0_ 0x4190
  71. #define _rRW_MISCTL__BOOT_OPT_B0_ 0x4218
  72. #define _rRW_MISCTL__BOOT_CTL_ 0x5094
  73. #define FAIL 0
  74. #define SUCCESS 1
  75. #pragma pack(1)
  76. struct st_fw_head {
  77. u8 hw_info[4]; /* hardware info// */
  78. u8 pid[8]; /* product id // */
  79. u16 vid; /* version id // */
  80. };
  81. #pragma pack()
  82. struct st_update_msg {
  83. u8 force_update;
  84. u8 fw_flag;
  85. struct file *file;
  86. struct file *cfg_file;
  87. struct st_fw_head ic_fw_msg;
  88. mm_segment_t old_fs;
  89. };
  90. struct st_update_msg update_msg;
  91. u16 show_len;
  92. u16 total_len;
  93. u8 searching_file = 0;
  94. s32 file_ready = 0;
  95. static s32 gup_i2c_read(struct i2c_client *client, u8 *buf, s32 len)
  96. {
  97. s32 ret = -1;
  98. u16 addr = (buf[0] << 8) + buf[1];
  99. ret = i2c_read_bytes(client, addr, &buf[2], len - 2);
  100. if (!ret)
  101. return 2;
  102. return ret;
  103. }
  104. static s32 gup_i2c_write(struct i2c_client *client, u8 *buf, s32 len)
  105. {
  106. s32 ret = -1;
  107. u16 addr = (buf[0] << 8) + buf[1];
  108. ret = i2c_write_bytes(client, addr, &buf[2], len - 2);
  109. if (!ret)
  110. return 1;
  111. return ret;
  112. }
  113. static u8 gup_get_ic_msg(struct i2c_client *client, u16 addr, u8 *msg, s32 len)
  114. {
  115. s32 i = 0;
  116. msg[0] = (addr >> 8) & 0xff;
  117. msg[1] = addr & 0xff;
  118. for (i = 0; i < 5; i++) {
  119. if (gup_i2c_read(client, msg, GTP_ADDR_LENGTH + len) > 0)
  120. break;
  121. }
  122. if (i >= 5) {
  123. GTP_ERROR("Read data from 0x%02x%02x failed!", msg[0], msg[1]);
  124. return FAIL;
  125. }
  126. return SUCCESS;
  127. }
  128. static u8 gup_set_ic_msg(struct i2c_client *client, u16 addr, u8 val)
  129. {
  130. s32 i = 0;
  131. u8 msg[3];
  132. msg[0] = (addr >> 8) & 0xff;
  133. msg[1] = addr & 0xff;
  134. msg[2] = val;
  135. for (i = 0; i < 5; i++) {
  136. if (gup_i2c_write(client, msg, GTP_ADDR_LENGTH + 1) > 0)
  137. break;
  138. }
  139. if (i >= 5) {
  140. GTP_ERROR("Set data to 0x%02x%02x failed!", msg[0], msg[1]);
  141. return FAIL;
  142. }
  143. return SUCCESS;
  144. }
  145. static u8 gup_get_ic_fw_msg(struct i2c_client *client)
  146. {
  147. s32 ret = -1;
  148. u8 retry = 0;
  149. u8 buf[16];
  150. u8 i;
  151. /* step1:get hardware info */
  152. ret = gtp_i2c_read_dbl_check(client, GUP_REG_HW_INFO, &buf[GTP_ADDR_LENGTH], 4);
  153. if (FAIL == ret) {
  154. GTP_ERROR("[get_ic_fw_msg]get hw_info failed,exit");
  155. return FAIL;
  156. }
  157. /* buf[2~5]: 00 06 90 00 */
  158. /* hw_info: 00 90 06 00 */
  159. for (i = 0; i < 4; i++)
  160. update_msg.ic_fw_msg.hw_info[i] = buf[GTP_ADDR_LENGTH + 3 - i];
  161. GTP_INFO("IC Hardware info:%02x%02x%02x%02x",
  162. update_msg.ic_fw_msg.hw_info[0],
  163. update_msg.ic_fw_msg.hw_info[1],
  164. update_msg.ic_fw_msg.hw_info[2], update_msg.ic_fw_msg.hw_info[3]);
  165. /* step2:get firmware message */
  166. for (retry = 0; retry < 2; retry++) {
  167. ret = gup_get_ic_msg(client, GUP_REG_FW_MSG, buf, 1);
  168. if (FAIL == ret) {
  169. GTP_ERROR("Read firmware message fail.");
  170. return ret;
  171. }
  172. update_msg.force_update = buf[GTP_ADDR_LENGTH];
  173. if ((0xBE != update_msg.force_update) && (!retry)) {
  174. GTP_INFO("The check sum in ic is error.");
  175. GTP_INFO("The IC will be updated by force.");
  176. continue;
  177. }
  178. break;
  179. }
  180. GTP_INFO("IC force update flag:0x%x", update_msg.force_update);
  181. /* step3:get pid & vid */
  182. ret = gtp_i2c_read_dbl_check(client, GUP_REG_PID_VID, &buf[GTP_ADDR_LENGTH], 6);
  183. if (FAIL == ret) {
  184. GTP_ERROR("[get_ic_fw_msg]get pid & vid failed,exit");
  185. return FAIL;
  186. }
  187. memset(update_msg.ic_fw_msg.pid, 0, sizeof(update_msg.ic_fw_msg.pid));
  188. memcpy(update_msg.ic_fw_msg.pid, &buf[GTP_ADDR_LENGTH], 4);
  189. /* GT9XX PID MAPPING */
  190. /*|-----FLASH-----RAM-----|
  191. |------918------918-----|
  192. |------968------968-----|
  193. |------913------913-----|
  194. |------913P-----913P----|
  195. |------927------927-----|
  196. |------927P-----927P----|
  197. |------9110-----9110----|
  198. |------9110P----9111----| */
  199. if (update_msg.ic_fw_msg.pid[0] != 0) {
  200. if (!memcmp(update_msg.ic_fw_msg.pid, "9111", 4)) {
  201. GTP_INFO("IC Mapping Product id:%s", update_msg.ic_fw_msg.pid);
  202. memcpy(update_msg.ic_fw_msg.pid, "9110P", 5);
  203. }
  204. }
  205. update_msg.ic_fw_msg.vid = buf[GTP_ADDR_LENGTH + 4] + (buf[GTP_ADDR_LENGTH + 5] << 8);
  206. return SUCCESS;
  207. }
  208. s32 gup_enter_update_mode(struct i2c_client *client)
  209. {
  210. s32 ret = -1;
  211. s32 retry = 0;
  212. u8 rd_buf[3];
  213. /* step1:RST output low last at least 2ms */
  214. tpd_gpio_output(GTP_RST_PORT, 0);
  215. msleep(20);
  216. /* step2:select I2C slave addr,INT:0--0xBA;1--0x28. */
  217. tpd_gpio_output(GTP_INT_PORT, (client->addr == 0x14));
  218. msleep(20);
  219. /* step3:RST output high reset guitar */
  220. tpd_gpio_output(GTP_RST_PORT, 1);
  221. /* 20121211 modify start */
  222. msleep(20);
  223. while (retry++ < 200) {
  224. /* step4:Hold ss51 & dsp */
  225. ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x0C);
  226. if (ret <= 0) {
  227. GTP_DEBUG("Hold ss51 & dsp I2C error,retry:%d", retry);
  228. continue;
  229. }
  230. /* step5:Confirm hold */
  231. ret = gup_get_ic_msg(client, _rRW_MISCTL__SWRST_B0_, rd_buf, 1);
  232. if (ret <= 0) {
  233. GTP_DEBUG("Hold ss51 & dsp I2C error,retry:%d", retry);
  234. continue;
  235. }
  236. if (0x0C == rd_buf[GTP_ADDR_LENGTH]) {
  237. GTP_DEBUG("Hold ss51 & dsp confirm SUCCESS");
  238. break;
  239. }
  240. GTP_DEBUG("Hold ss51 & dsp confirm 0x4180 failed,value:%d",
  241. rd_buf[GTP_ADDR_LENGTH]);
  242. }
  243. if (retry >= 200) {
  244. GTP_ERROR("Enter update Hold ss51 failed.");
  245. return FAIL;
  246. }
  247. /* step6:DSP_CK and DSP_ALU_CK PowerOn */
  248. ret = gup_set_ic_msg(client, 0x4010, 0x00);
  249. /* 20121211 modify end */
  250. return ret;
  251. }
  252. void gup_leave_update_mode(void)
  253. {
  254. tpd_gpio_as_int(GTP_INT_PORT);
  255. GTP_DEBUG("[leave_update_mode]reset chip.");
  256. gtp_reset_guitar(i2c_client_point, 20);
  257. }
  258. static u8 gup_enter_update_judge(struct st_fw_head *fw_head)
  259. {
  260. u16 u16_tmp;
  261. s32 i = 0;
  262. u8 pid_cmp_len = 0;
  263. /* Get the correct nvram data */
  264. /* The correct conditions: */
  265. /* 1. the hardware info is the same */
  266. /* 2. the product id is the same */
  267. /* 3. the firmware version in update file is greater than the firmware version in ic */
  268. /* or the check sum in ic is wrong */
  269. u16_tmp = fw_head->vid;
  270. fw_head->vid = (u16) (u16_tmp >> 8) + (u16) (u16_tmp << 8);
  271. GTP_INFO("FILE HARDWARE INFO:%02x%02x%02x%02x", fw_head->hw_info[0],
  272. fw_head->hw_info[1], fw_head->hw_info[2], fw_head->hw_info[3]);
  273. GTP_INFO("FILE PID:%s", fw_head->pid);
  274. GTP_INFO("FILE VID:%04x", fw_head->vid);
  275. GTP_INFO("IC HARDWARE INFO:%02x%02x%02x%02x",
  276. update_msg.ic_fw_msg.hw_info[0],
  277. update_msg.ic_fw_msg.hw_info[1],
  278. update_msg.ic_fw_msg.hw_info[2], update_msg.ic_fw_msg.hw_info[3]);
  279. GTP_INFO("IC PID:%s", update_msg.ic_fw_msg.pid);
  280. GTP_INFO("IC VID:%04x", update_msg.ic_fw_msg.vid);
  281. /* First two conditions */
  282. if (!memcmp
  283. (fw_head->hw_info, update_msg.ic_fw_msg.hw_info,
  284. sizeof(update_msg.ic_fw_msg.hw_info))) {
  285. GTP_INFO("Get the same hardware info.");
  286. if (update_msg.force_update != 0xBE) {
  287. GTP_INFO("FW chksum error,need enter update.");
  288. return SUCCESS;
  289. }
  290. /* 20130523 start */
  291. if (strlen(update_msg.ic_fw_msg.pid) < 3) {
  292. GTP_INFO("Illegal IC pid, need enter update");
  293. return SUCCESS;
  294. }
  295. for (i = 0; i < 3; i++) {
  296. if ((update_msg.ic_fw_msg.pid[i] < 0x30)
  297. || (update_msg.ic_fw_msg.pid[i] > 0x39)) {
  298. GTP_INFO("Illegal IC pid, out of bound, need enter update");
  299. return SUCCESS;
  300. }
  301. }
  302. /* 20130523 end */
  303. pid_cmp_len = strlen(fw_head->pid);
  304. if (pid_cmp_len < strlen(update_msg.ic_fw_msg.pid))
  305. pid_cmp_len = strlen(update_msg.ic_fw_msg.pid);
  306. if ((!memcmp(fw_head->pid, update_msg.ic_fw_msg.pid, pid_cmp_len))
  307. || (!memcmp(update_msg.ic_fw_msg.pid, "91XX", 4))
  308. || (!memcmp(fw_head->pid, "91XX", 4))) {
  309. if (!memcmp(fw_head->pid, "91XX", 4))
  310. GTP_DEBUG("Force none same pid update mode.");
  311. else
  312. GTP_DEBUG("Get the same pid.");
  313. /* The third condition */
  314. if (fw_head->vid > update_msg.ic_fw_msg.vid) {
  315. GTP_INFO("Need enter update.");
  316. return SUCCESS;
  317. }
  318. GTP_INFO("Don't meet the third condition.");
  319. GTP_ERROR("File VID <= IC VID, update aborted!");
  320. } else {
  321. GTP_ERROR("File PID != IC PID, update aborted!");
  322. }
  323. } else {
  324. GTP_ERROR("Different Hardware, update aborted!");
  325. }
  326. return FAIL;
  327. }
  328. static s32 gup_update_config(struct i2c_client *client, char *cfg_path)
  329. {
  330. struct file *cfg_filp;
  331. s32 ret = 0;
  332. s32 i = 0;
  333. u32 cfg = 0;
  334. u8 chksum = 0;
  335. u8 gt_config[228] = { 0 };
  336. char *sconfig = NULL;
  337. u16 file_len = 0;
  338. cfg_filp = filp_open(cfg_path, O_RDONLY, 0);
  339. if (IS_ERR(cfg_filp)) {
  340. GTP_ERROR("Failed to open %s to config update", cfg_path);
  341. return FAIL;
  342. }
  343. cfg_filp->f_op->llseek(cfg_filp, 0, SEEK_SET);
  344. file_len = cfg_filp->f_op->llseek(cfg_filp, 0, SEEK_END);
  345. GTP_DEBUG("file len: %d", file_len);
  346. if (file_len < (cfg_len * 3)) {
  347. GTP_ERROR("invalid config file, config update aborted!");
  348. filp_close(cfg_filp, NULL);
  349. return FAIL;
  350. }
  351. sconfig = kmalloc(sizeof(char) * (file_len + 1), GFP_KERNEL);
  352. memset(sconfig, 0, sizeof(char) * (file_len + 1));
  353. cfg_filp->f_op->llseek(cfg_filp, 0, SEEK_SET);
  354. ret = cfg_filp->f_op->read(cfg_filp, sconfig, file_len, &cfg_filp->f_pos);
  355. filp_close(cfg_filp, NULL);
  356. if (ret < 0) {
  357. GTP_ERROR("failed to read config file");
  358. kfree(sconfig);
  359. return FAIL;
  360. }
  361. GTP_DEBUG("sconfig: %s", sconfig);
  362. /* clear whitespace */
  363. for (ret = 0, i = 0; ret < file_len; ++ret) {
  364. if ((sconfig[ret] == ' ') ||
  365. (sconfig[ret] == '\n') ||
  366. (sconfig[ret] == '\r') || (sconfig[ret] == '\t') || (sconfig[ret] == '\\')
  367. ) {
  368. continue;
  369. }
  370. sconfig[i++] = sconfig[ret];
  371. }
  372. GTP_DEBUG("After cleanup: %d", i);
  373. file_len = i;
  374. ret = sscanf(sconfig, "0x%02X", &cfg);
  375. gt_config[0] = (u8) cfg;
  376. for (ret = 4, i = 1; ret < file_len; ret += 5) {
  377. ret = sscanf(sconfig + ret, ",0x%02X", &cfg);
  378. gt_config[i] = (u8) cfg;
  379. ++i;
  380. }
  381. kfree(sconfig);
  382. GTP_DEBUG("config len: %d", i);
  383. GTP_DEBUG_ARRAY(gt_config, i);
  384. /* check checksum */
  385. for (ret = 0; ret < (i - 2); ++ret)
  386. chksum += gt_config[ret];
  387. chksum = (~chksum) + 1;
  388. if (chksum != gt_config[i - 2]) {
  389. GTP_ERROR("Wrong checksum in %s, config update aborted!", cfg_path);
  390. return FAIL;
  391. }
  392. /* check update flag */
  393. if (gt_config[i - 1] != 0x01) {
  394. GTP_ERROR("Unsetted config update flag, config update aborted!");
  395. return FAIL;
  396. }
  397. ret = i2c_write_bytes(client, GTP_REG_CONFIG_DATA, gt_config, i);
  398. if (ret < 0) {
  399. GTP_ERROR("write config data failed, config update failed!");
  400. return FAIL;
  401. }
  402. GTP_INFO("Config update successfully!");
  403. msleep(500); /* ic store config info into flash */
  404. return SUCCESS;
  405. }
  406. u8 gup_check_fs_mounted(char *path_name)
  407. {
  408. struct path root_path;
  409. struct path path;
  410. int err;
  411. err = kern_path("/", LOOKUP_FOLLOW, &root_path);
  412. if (err)
  413. return FAIL;
  414. err = kern_path(path_name, LOOKUP_FOLLOW, &path);
  415. if (err)
  416. return FAIL;
  417. if (path.mnt->mnt_sb == root_path.mnt->mnt_sb) {
  418. /* -- not mounted */
  419. path_put(&root_path);
  420. path_put(&path);
  421. return FAIL;
  422. }
  423. path_put(&root_path);
  424. path_put(&path);
  425. return SUCCESS;
  426. /*
  427. struct file *pfile;
  428. pfile = filp_open(GTP_BAK_REF_PATH, O_RDONLY | O_CREAT, 0);
  429. if (IS_ERR(pfile))
  430. {
  431. return FAIL;
  432. }
  433. else
  434. {
  435. filp_close(pfile, NULL);
  436. return SUCCESS;
  437. }
  438. */
  439. }
  440. static u8 gup_check_update_file(struct i2c_client *client, struct st_fw_head *fw_head, u8 *path)
  441. {
  442. s32 ret = 0;
  443. s32 i = 0;
  444. s32 fw_checksum = 0;
  445. u8 buf[FW_HEAD_LENGTH];
  446. if (path) {
  447. update_msg.file = filp_open(path, O_RDONLY, 0644);
  448. if (IS_ERR(update_msg.file)) {
  449. GTP_ERROR("Open update file(%s) error!", path);
  450. return FAIL;
  451. }
  452. file_ready = 1;
  453. } else {
  454. for (i = 0; i < GUP_SEARCH_FILE_TIMES; i++) {
  455. msleep(500);
  456. GTP_DEBUG("Wati for FS /data mounted %d", i);
  457. if (gup_check_fs_mounted("/data") == SUCCESS) {
  458. GTP_DEBUG("/data mounted ~!!!!");
  459. break;
  460. }
  461. }
  462. /* Begin to search update file */
  463. searching_file = 1;
  464. for (i = 0; i < GUP_SEARCH_FILE_TIMES; i++) {
  465. #if GTP_DEF_FW_UPDATE
  466. file_ready = 0;
  467. break;
  468. #endif
  469. if (0 == searching_file) {
  470. GTP_INFO("Force terminate searching file auto update.");
  471. return FAIL;
  472. }
  473. update_msg.file = filp_open(UPDATE_FILE_PATH_1, O_RDONLY, 0);
  474. if (!IS_ERR(update_msg.file)) {
  475. GTP_DEBUG("%s opened, size: %d bytes!",
  476. UPDATE_FILE_PATH_1,
  477. (int)update_msg.file->f_dentry->d_inode->i_size);
  478. if (update_msg.file->f_dentry->d_inode->i_size == 0) {
  479. filp_close(update_msg.file, NULL);
  480. } else {
  481. GTP_DEBUG("Fw Update File: %s ready!", UPDATE_FILE_PATH_1);
  482. file_ready = 1;
  483. break;
  484. }
  485. }
  486. update_msg.file = filp_open(UPDATE_FILE_PATH_2, O_RDONLY, 0);
  487. if (!IS_ERR(update_msg.file)) {
  488. GTP_DEBUG("%s opened, size: %d bytes!",
  489. UPDATE_FILE_PATH_2,
  490. (int)update_msg.file->f_dentry->d_inode->i_size);
  491. if (update_msg.file->f_dentry->d_inode->i_size == 0) {
  492. filp_close(update_msg.file, NULL);
  493. } else {
  494. GTP_DEBUG("Fw Update File: %s ready!", UPDATE_FILE_PATH_2);
  495. file_ready = 1;
  496. break;
  497. }
  498. }
  499. msleep(2000);
  500. }
  501. searching_file = 0;
  502. update_msg.cfg_file = filp_open(CONFIG_FILE_PATH_1, O_RDONLY, 0);
  503. if (IS_ERR(update_msg.cfg_file)) {
  504. update_msg.cfg_file = filp_open(CONFIG_FILE_PATH_2, O_RDONLY, 0);
  505. if (IS_ERR(update_msg.cfg_file)) {
  506. GTP_DEBUG("config update file unavailable");
  507. } else {
  508. GTP_INFO("Config Update file: %s", CONFIG_FILE_PATH_2);
  509. filp_close(update_msg.cfg_file, NULL);
  510. gup_update_config(client, CONFIG_FILE_PATH_2);
  511. }
  512. } else {
  513. GTP_INFO("Config Update file: %s", CONFIG_FILE_PATH_1);
  514. filp_close(update_msg.cfg_file, NULL);
  515. gup_update_config(client, CONFIG_FILE_PATH_1);
  516. }
  517. }
  518. if (file_ready == 0) {
  519. #if GTP_DEF_FW_UPDATE
  520. GTP_INFO("Update by default firmware array");
  521. if (sizeof(gtp_default_FW) <
  522. (FW_HEAD_LENGTH + FW_SECTION_LENGTH * 4 +
  523. FW_DSP_ISP_LENGTH + FW_DSP_LENGTH + FW_BOOT_LENGTH)) {
  524. GTP_INFO("[check_update_file]default firmware array is INVALID!");
  525. return FAIL;
  526. }
  527. memcpy(buf, &gtp_default_FW[0], FW_HEAD_LENGTH);
  528. memcpy(fw_head, buf, FW_HEAD_LENGTH);
  529. /* check firmware legality */
  530. fw_checksum = 0;
  531. for (i = 0;
  532. i <
  533. FW_SECTION_LENGTH * 4 + FW_DSP_ISP_LENGTH + FW_DSP_LENGTH +
  534. FW_BOOT_LENGTH; i += 2) {
  535. u16 temp;
  536. memcpy(buf, &gtp_default_FW[FW_HEAD_LENGTH + i], 2);
  537. temp = (buf[0] << 8) + buf[1];
  538. fw_checksum += temp;
  539. }
  540. GTP_DEBUG("firmware checksum:%x", fw_checksum & 0xFFFF);
  541. if (fw_checksum & 0xFFFF) {
  542. GTP_ERROR("Illegal firmware file.");
  543. return FAIL;
  544. }
  545. return SUCCESS;
  546. #else
  547. return FAIL;
  548. #endif
  549. }
  550. update_msg.old_fs = get_fs();
  551. set_fs(KERNEL_DS);
  552. update_msg.file->f_op->llseek(update_msg.file, 0, SEEK_SET);
  553. /* update_msg.file->f_pos = 0; */
  554. ret =
  555. update_msg.file->f_op->read(update_msg.file, (char *)buf,
  556. FW_HEAD_LENGTH, &update_msg.file->f_pos);
  557. if (ret < 0) {
  558. GTP_ERROR("Read firmware head in update file error.");
  559. goto load_failed;
  560. }
  561. memcpy(fw_head, buf, FW_HEAD_LENGTH);
  562. /* check firmware legality */
  563. fw_checksum = 0;
  564. for (i = 0;
  565. i <
  566. FW_SECTION_LENGTH * 4 + FW_DSP_ISP_LENGTH + FW_DSP_LENGTH + FW_BOOT_LENGTH; i += 2) {
  567. u16 temp;
  568. ret =
  569. update_msg.file->f_op->read(update_msg.file, (char *)buf, 2,
  570. &update_msg.file->f_pos);
  571. if (ret < 0) {
  572. GTP_ERROR("Read firmware file error.");
  573. goto load_failed;
  574. }
  575. /* GTP_DEBUG("BUF[0]:%x", buf[0]); */
  576. temp = (buf[0] << 8) + buf[1];
  577. fw_checksum += temp;
  578. }
  579. GTP_DEBUG("firmware checksum:%x", fw_checksum & 0xFFFF);
  580. if (fw_checksum & 0xFFFF) {
  581. GTP_ERROR("Illegal firmware file.");
  582. goto load_failed;
  583. }
  584. return SUCCESS;
  585. load_failed:
  586. set_fs(update_msg.old_fs);
  587. if (update_msg.file && !IS_ERR(update_msg.file))
  588. filp_close(update_msg.file, NULL);
  589. return FAIL;
  590. }
  591. static u8 gup_burn_proc(struct i2c_client *client, u8 *burn_buf, u16 start_addr, u16 total_length)
  592. {
  593. s32 ret = 0;
  594. u16 burn_addr = start_addr;
  595. u16 frame_length = 0;
  596. u16 burn_length = 0;
  597. u8 wr_buf[PACK_SIZE + GTP_ADDR_LENGTH];
  598. u8 rd_buf[PACK_SIZE + GTP_ADDR_LENGTH];
  599. u8 retry = 0;
  600. GTP_DEBUG("Begin burn %dk data to addr 0x%x", (total_length / 1024), start_addr);
  601. while (burn_length < total_length) {
  602. GTP_DEBUG("B/T:%04d/%04d", burn_length, total_length);
  603. frame_length =
  604. ((total_length - burn_length) >
  605. PACK_SIZE) ? PACK_SIZE : (total_length - burn_length);
  606. wr_buf[0] = (u8) (burn_addr >> 8);
  607. rd_buf[0] = wr_buf[0];
  608. wr_buf[1] = (u8) burn_addr;
  609. rd_buf[1] = wr_buf[1];
  610. memcpy(&wr_buf[GTP_ADDR_LENGTH], &burn_buf[burn_length], frame_length);
  611. for (retry = 0; retry < MAX_FRAME_CHECK_TIME; retry++) {
  612. ret = gup_i2c_write(client, wr_buf, GTP_ADDR_LENGTH + frame_length);
  613. if (ret <= 0) {
  614. GTP_ERROR("Write frame data i2c error.");
  615. continue;
  616. }
  617. ret = gup_i2c_read(client, rd_buf, GTP_ADDR_LENGTH + frame_length);
  618. if (ret <= 0) {
  619. GTP_ERROR("Read back frame data i2c error.");
  620. continue;
  621. }
  622. if (memcmp
  623. (&wr_buf[GTP_ADDR_LENGTH], &rd_buf[GTP_ADDR_LENGTH], frame_length)) {
  624. GTP_ERROR("Check frame data fail,not equal.");
  625. GTP_DEBUG("write array:");
  626. GTP_DEBUG_ARRAY(&wr_buf[GTP_ADDR_LENGTH], frame_length);
  627. GTP_DEBUG("read array:");
  628. GTP_DEBUG_ARRAY(&rd_buf[GTP_ADDR_LENGTH], frame_length);
  629. continue;
  630. } else {
  631. /* GTP_DEBUG("Check frame data success."); */
  632. break;
  633. }
  634. }
  635. if (retry > MAX_FRAME_CHECK_TIME) {
  636. GTP_ERROR("Burn frame data time out,exit.");
  637. return FAIL;
  638. }
  639. burn_length += frame_length;
  640. burn_addr += frame_length;
  641. }
  642. return SUCCESS;
  643. }
  644. static u8 gup_load_section_file(u8 *buf, u16 offset, u16 length)
  645. {
  646. #if (GTP_AUTO_UPDATE && GTP_DEF_FW_UPDATE)
  647. if (0 == file_ready) {
  648. memcpy(buf, &gtp_default_FW[FW_HEAD_LENGTH + offset], length);
  649. return SUCCESS;
  650. }
  651. #endif
  652. s32 ret = 0;
  653. if (update_msg.file == NULL) {
  654. GTP_ERROR("cannot find update file,load section file fail.");
  655. return FAIL;
  656. }
  657. update_msg.file->f_pos = FW_HEAD_LENGTH + offset;
  658. ret =
  659. update_msg.file->f_op->read(update_msg.file, (char *)buf,
  660. length, &update_msg.file->f_pos);
  661. if (ret < 0) {
  662. GTP_ERROR("Read update file fail.");
  663. return FAIL;
  664. }
  665. return SUCCESS;
  666. }
  667. static u8 gup_recall_check(struct i2c_client *client, u8 *chk_src,
  668. u16 start_rd_addr, u16 chk_length)
  669. {
  670. u8 rd_buf[PACK_SIZE + GTP_ADDR_LENGTH];
  671. s32 ret = 0;
  672. u16 recall_addr = start_rd_addr;
  673. u16 recall_length = 0;
  674. u16 frame_length = 0;
  675. while (recall_length < chk_length) {
  676. frame_length =
  677. ((chk_length - recall_length) >
  678. PACK_SIZE) ? PACK_SIZE : (chk_length - recall_length);
  679. ret = gup_get_ic_msg(client, recall_addr, rd_buf, frame_length);
  680. if (ret <= 0) {
  681. GTP_ERROR("recall i2c error,exit");
  682. return FAIL;
  683. }
  684. if (memcmp(&rd_buf[GTP_ADDR_LENGTH], &chk_src[recall_length], frame_length)) {
  685. GTP_ERROR("Recall frame data fail,not equal.");
  686. GTP_DEBUG("chk_src array:");
  687. GTP_DEBUG_ARRAY(&chk_src[recall_length], frame_length);
  688. GTP_DEBUG("recall array:");
  689. GTP_DEBUG_ARRAY(&rd_buf[GTP_ADDR_LENGTH], frame_length);
  690. return FAIL;
  691. }
  692. recall_length += frame_length;
  693. recall_addr += frame_length;
  694. }
  695. GTP_DEBUG("Recall check %dk firmware success.", (chk_length / 1024));
  696. return SUCCESS;
  697. }
  698. static u8 gup_burn_fw_section(struct i2c_client *client, u8 *fw_section,
  699. u16 start_addr, u8 bank_cmd)
  700. {
  701. s32 ret = 0;
  702. u8 rd_buf[5];
  703. /* step1:hold ss51 & dsp */
  704. ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x0C);
  705. if (ret <= 0) {
  706. GTP_ERROR("[burn_fw_section]hold ss51 & dsp fail.");
  707. return FAIL;
  708. }
  709. /* step2:set scramble */
  710. ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_OPT_B0_, 0x00);
  711. if (ret <= 0) {
  712. GTP_ERROR("[burn_fw_section]set scramble fail.");
  713. return FAIL;
  714. }
  715. /* step3:select bank */
  716. ret = gup_set_ic_msg(client, _bRW_MISCTL__SRAM_BANK, (bank_cmd >> 4) & 0x0F);
  717. if (ret <= 0) {
  718. GTP_ERROR("[burn_fw_section]select bank %d fail.", (bank_cmd >> 4) & 0x0F);
  719. return FAIL;
  720. }
  721. /* step4:enable accessing code */
  722. ret = gup_set_ic_msg(client, _bRW_MISCTL__MEM_CD_EN, 0x01);
  723. if (ret <= 0) {
  724. GTP_ERROR("[burn_fw_section]enable accessing code fail.");
  725. return FAIL;
  726. }
  727. /* step5:burn 8k fw section */
  728. ret = gup_burn_proc(client, fw_section, start_addr, FW_SECTION_LENGTH);
  729. if (FAIL == ret) {
  730. GTP_ERROR("[burn_fw_section]burn fw_section fail.");
  731. return FAIL;
  732. }
  733. /* step6:hold ss51 & release dsp */
  734. ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x04);
  735. if (ret <= 0) {
  736. GTP_ERROR("[burn_fw_section]hold ss51 & release dsp fail.");
  737. return FAIL;
  738. }
  739. /* must delay */
  740. msleep(20);
  741. /* step7:send burn cmd to move data to flash from sram */
  742. ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, bank_cmd & 0x0f);
  743. if (ret <= 0) {
  744. GTP_ERROR("[burn_fw_section]send burn cmd fail.");
  745. return FAIL;
  746. }
  747. GTP_DEBUG("[burn_fw_section]Wait for the burn is complete......");
  748. do {
  749. ret = gup_get_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, rd_buf, 1);
  750. if (ret <= 0) {
  751. GTP_ERROR("[burn_fw_section]Get burn state fail");
  752. return FAIL;
  753. }
  754. msleep(20);
  755. /* GTP_DEBUG("[burn_fw_section]Get burn state:%d.", rd_buf[GTP_ADDR_LENGTH]); */
  756. } while (rd_buf[GTP_ADDR_LENGTH]);
  757. /* step8:select bank */
  758. ret = gup_set_ic_msg(client, _bRW_MISCTL__SRAM_BANK, (bank_cmd >> 4) & 0x0F);
  759. if (ret <= 0) {
  760. GTP_ERROR("[burn_fw_section]select bank %d fail.", (bank_cmd >> 4) & 0x0F);
  761. return FAIL;
  762. }
  763. /* step9:enable accessing code */
  764. ret = gup_set_ic_msg(client, _bRW_MISCTL__MEM_CD_EN, 0x01);
  765. if (ret <= 0) {
  766. GTP_ERROR("[burn_fw_section]enable accessing code fail.");
  767. return FAIL;
  768. }
  769. /* step10:recall 8k fw section */
  770. ret = gup_recall_check(client, fw_section, start_addr, FW_SECTION_LENGTH);
  771. if (FAIL == ret) {
  772. GTP_ERROR("[burn_fw_section]recall check 8k firmware fail.");
  773. return FAIL;
  774. }
  775. /* step11:disable accessing code */
  776. ret = gup_set_ic_msg(client, _bRW_MISCTL__MEM_CD_EN, 0x00);
  777. if (ret <= 0) {
  778. GTP_ERROR("[burn_fw_section]disable accessing code fail.");
  779. return FAIL;
  780. }
  781. return SUCCESS;
  782. }
  783. static u8 gup_burn_dsp_isp(struct i2c_client *client)
  784. {
  785. s32 ret = 0;
  786. u8 *fw_dsp_isp = NULL;
  787. u8 retry = 0;
  788. GTP_INFO("[burn_dsp_isp]Begin burn dsp isp---->>");
  789. /* step1:alloc memory */
  790. GTP_DEBUG("[burn_dsp_isp]step1:alloc memory");
  791. while (retry++ < 5) {
  792. fw_dsp_isp = kzalloc(FW_DSP_ISP_LENGTH, GFP_KERNEL);
  793. if (fw_dsp_isp == NULL) {
  794. continue;
  795. } else {
  796. GTP_INFO("[burn_dsp_isp]Alloc %dk byte memory success.",
  797. (FW_DSP_ISP_LENGTH / 1024));
  798. break;
  799. }
  800. }
  801. if (retry >= 5) {
  802. GTP_ERROR("[burn_dsp_isp]Alloc memory fail,exit.");
  803. return FAIL;
  804. }
  805. /* step2:load dsp isp file data */
  806. GTP_DEBUG("[burn_dsp_isp]step2:load dsp isp file data");
  807. ret =
  808. gup_load_section_file(fw_dsp_isp,
  809. (4 * FW_SECTION_LENGTH + FW_DSP_LENGTH +
  810. FW_BOOT_LENGTH), FW_DSP_ISP_LENGTH);
  811. if (FAIL == ret) {
  812. GTP_ERROR("[burn_dsp_isp]load firmware dsp_isp fail.");
  813. goto exit_burn_dsp_isp;
  814. }
  815. /* step3:disable wdt,clear cache enable */
  816. GTP_DEBUG("[burn_dsp_isp]step3:disable wdt,clear cache enable");
  817. ret = gup_set_ic_msg(client, _bRW_MISCTL__TMR0_EN, 0x00);
  818. if (ret <= 0) {
  819. GTP_ERROR("[burn_dsp_isp]disable wdt fail.");
  820. ret = FAIL;
  821. goto exit_burn_dsp_isp;
  822. }
  823. ret = gup_set_ic_msg(client, _bRW_MISCTL__CACHE_EN, 0x00);
  824. if (ret <= 0) {
  825. GTP_ERROR("[burn_dsp_isp]clear cache enable fail.");
  826. ret = FAIL;
  827. goto exit_burn_dsp_isp;
  828. }
  829. /* step4:hold ss51 & dsp */
  830. GTP_DEBUG("[burn_dsp_isp]step4:hold ss51 & dsp");
  831. ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x0C);
  832. if (ret <= 0) {
  833. GTP_ERROR("[burn_dsp_isp]hold ss51 & dsp fail.");
  834. ret = FAIL;
  835. goto exit_burn_dsp_isp;
  836. }
  837. /* step5:set boot from sram */
  838. GTP_DEBUG("[burn_dsp_isp]step5:set boot from sram");
  839. ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOTCTL_B0_, 0x02);
  840. if (ret <= 0) {
  841. GTP_ERROR("[burn_dsp_isp]set boot from sram fail.");
  842. ret = FAIL;
  843. goto exit_burn_dsp_isp;
  844. }
  845. /* step6:software reboot */
  846. GTP_DEBUG("[burn_dsp_isp]step6:software reboot");
  847. ret = gup_set_ic_msg(client, _bWO_MISCTL__CPU_SWRST_PULSE, 0x01);
  848. if (ret <= 0) {
  849. GTP_ERROR("[burn_dsp_isp]software reboot fail.");
  850. ret = FAIL;
  851. goto exit_burn_dsp_isp;
  852. }
  853. /* step7:select bank2 */
  854. GTP_DEBUG("[burn_dsp_isp]step7:select bank2");
  855. ret = gup_set_ic_msg(client, _bRW_MISCTL__SRAM_BANK, 0x02);
  856. if (ret <= 0) {
  857. GTP_ERROR("[burn_dsp_isp]select bank2 fail.");
  858. ret = FAIL;
  859. goto exit_burn_dsp_isp;
  860. }
  861. /* step8:enable accessing code */
  862. GTP_DEBUG("[burn_dsp_isp]step8:enable accessing code");
  863. ret = gup_set_ic_msg(client, _bRW_MISCTL__MEM_CD_EN, 0x01);
  864. if (ret <= 0) {
  865. GTP_ERROR("[burn_dsp_isp]enable accessing code fail.");
  866. ret = FAIL;
  867. goto exit_burn_dsp_isp;
  868. }
  869. /* step9:burn 4k dsp_isp */
  870. GTP_DEBUG("[burn_dsp_isp]step9:burn 4k dsp_isp");
  871. ret = gup_burn_proc(client, fw_dsp_isp, 0xC000, FW_DSP_ISP_LENGTH);
  872. if (FAIL == ret) {
  873. GTP_ERROR("[burn_dsp_isp]burn dsp_isp fail.");
  874. goto exit_burn_dsp_isp;
  875. }
  876. /* step10:set scramble */
  877. GTP_DEBUG("[burn_dsp_isp]step10:set scramble");
  878. ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_OPT_B0_, 0x00);
  879. if (ret <= 0) {
  880. GTP_ERROR("[burn_dsp_isp]set scramble fail.");
  881. ret = FAIL;
  882. goto exit_burn_dsp_isp;
  883. }
  884. ret = SUCCESS;
  885. exit_burn_dsp_isp:
  886. kfree(fw_dsp_isp);
  887. return ret;
  888. }
  889. static u8 gup_burn_fw_ss51(struct i2c_client *client)
  890. {
  891. u8 *fw_ss51 = NULL;
  892. u8 retry = 0;
  893. s32 ret = 0;
  894. GTP_INFO("[burn_fw_ss51]Begin burn ss51 firmware---->>");
  895. /* step1:alloc memory */
  896. GTP_DEBUG("[burn_fw_ss51]step1:alloc memory");
  897. while (retry++ < 5) {
  898. fw_ss51 = kzalloc(FW_SECTION_LENGTH, GFP_KERNEL);
  899. if (fw_ss51 == NULL) {
  900. continue;
  901. } else {
  902. GTP_INFO("[burn_fw_ss51]Alloc %dk byte memory success.",
  903. (FW_SECTION_LENGTH / 1024));
  904. break;
  905. }
  906. }
  907. if (retry >= 5) {
  908. GTP_ERROR("[burn_fw_ss51]Alloc memory fail,exit.");
  909. return FAIL;
  910. }
  911. /* step2:load ss51 firmware section 1 file data */
  912. GTP_DEBUG("[burn_fw_ss51]step2:load ss51 firmware section 1 file data");
  913. ret = gup_load_section_file(fw_ss51, 0, FW_SECTION_LENGTH);
  914. if (FAIL == ret) {
  915. GTP_ERROR("[burn_fw_ss51]load ss51 firmware section 1 fail.");
  916. goto exit_burn_fw_ss51;
  917. }
  918. /* step3:clear control flag */
  919. GTP_DEBUG("[burn_fw_ss51]step3:clear control flag");
  920. ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, 0x00);
  921. if (ret <= 0) {
  922. GTP_ERROR("[burn_fw_ss51]clear control flag fail.");
  923. ret = FAIL;
  924. goto exit_burn_fw_ss51;
  925. }
  926. /* step4:burn ss51 firmware section 1 */
  927. GTP_DEBUG("[burn_fw_ss51]step4:burn ss51 firmware section 1");
  928. ret = gup_burn_fw_section(client, fw_ss51, 0xC000, 0x01);
  929. if (FAIL == ret) {
  930. GTP_ERROR("[burn_fw_ss51]burn ss51 firmware section 1 fail.");
  931. goto exit_burn_fw_ss51;
  932. }
  933. /* step5:load ss51 firmware section 2 file data */
  934. GTP_DEBUG("[burn_fw_ss51]step5:load ss51 firmware section 2 file data");
  935. ret = gup_load_section_file(fw_ss51, FW_SECTION_LENGTH, FW_SECTION_LENGTH);
  936. if (FAIL == ret) {
  937. GTP_ERROR("[burn_fw_ss51]load ss51 firmware section 2 fail.");
  938. goto exit_burn_fw_ss51;
  939. }
  940. /* step6:burn ss51 firmware section 2 */
  941. GTP_DEBUG("[burn_fw_ss51]step6:burn ss51 firmware section 2");
  942. ret = gup_burn_fw_section(client, fw_ss51, 0xE000, 0x02);
  943. if (FAIL == ret) {
  944. GTP_ERROR("[burn_fw_ss51]burn ss51 firmware section 2 fail.");
  945. goto exit_burn_fw_ss51;
  946. }
  947. /* step7:load ss51 firmware section 3 file data */
  948. GTP_DEBUG("[burn_fw_ss51]step7:load ss51 firmware section 3 file data");
  949. ret = gup_load_section_file(fw_ss51, 2 * FW_SECTION_LENGTH, FW_SECTION_LENGTH);
  950. if (FAIL == ret) {
  951. GTP_ERROR("[burn_fw_ss51]load ss51 firmware section 3 fail.");
  952. goto exit_burn_fw_ss51;
  953. }
  954. /* step8:burn ss51 firmware section 3 */
  955. GTP_DEBUG("[burn_fw_ss51]step8:burn ss51 firmware section 3");
  956. ret = gup_burn_fw_section(client, fw_ss51, 0xC000, 0x13);
  957. if (FAIL == ret) {
  958. GTP_ERROR("[burn_fw_ss51]burn ss51 firmware section 3 fail.");
  959. goto exit_burn_fw_ss51;
  960. }
  961. /* step9:load ss51 firmware section 4 file data */
  962. GTP_DEBUG("[burn_fw_ss51]step9:load ss51 firmware section 4 file data");
  963. ret = gup_load_section_file(fw_ss51, 3 * FW_SECTION_LENGTH, FW_SECTION_LENGTH);
  964. if (FAIL == ret) {
  965. GTP_ERROR("[burn_fw_ss51]load ss51 firmware section 4 fail.");
  966. goto exit_burn_fw_ss51;
  967. }
  968. /* step10:burn ss51 firmware section 4 */
  969. GTP_DEBUG("[burn_fw_ss51]step10:burn ss51 firmware section 4");
  970. ret = gup_burn_fw_section(client, fw_ss51, 0xE000, 0x14);
  971. if (FAIL == ret) {
  972. GTP_ERROR("[burn_fw_ss51]burn ss51 firmware section 4 fail.");
  973. goto exit_burn_fw_ss51;
  974. }
  975. ret = SUCCESS;
  976. exit_burn_fw_ss51:
  977. kfree(fw_ss51);
  978. return ret;
  979. }
  980. static u8 gup_burn_fw_dsp(struct i2c_client *client)
  981. {
  982. s32 ret = 0;
  983. u8 *fw_dsp = NULL;
  984. u8 retry = 0;
  985. u8 rd_buf[5];
  986. GTP_INFO("[burn_fw_dsp]Begin burn dsp firmware---->>");
  987. /* step1:alloc memory */
  988. GTP_DEBUG("[burn_fw_dsp]step1:alloc memory");
  989. while (retry++ < 5) {
  990. fw_dsp = kzalloc(FW_DSP_LENGTH, GFP_KERNEL);
  991. if (fw_dsp == NULL) {
  992. continue;
  993. } else {
  994. GTP_INFO("[burn_fw_dsp]Alloc %dk byte memory success.",
  995. (FW_SECTION_LENGTH / 1024));
  996. break;
  997. }
  998. }
  999. if (retry >= 5) {
  1000. GTP_ERROR("[burn_fw_dsp]Alloc memory fail,exit.");
  1001. return FAIL;
  1002. }
  1003. /* step2:load firmware dsp */
  1004. GTP_DEBUG("[burn_fw_dsp]step2:load firmware dsp");
  1005. ret = gup_load_section_file(fw_dsp, 4 * FW_SECTION_LENGTH, FW_DSP_LENGTH);
  1006. if (FAIL == ret) {
  1007. GTP_ERROR("[burn_fw_dsp]load firmware dsp fail.");
  1008. goto exit_burn_fw_dsp;
  1009. }
  1010. /* step3:select bank3 */
  1011. GTP_DEBUG("[burn_fw_dsp]step3:select bank3");
  1012. ret = gup_set_ic_msg(client, _bRW_MISCTL__SRAM_BANK, 0x03);
  1013. if (ret <= 0) {
  1014. GTP_ERROR("[burn_fw_dsp]select bank3 fail.");
  1015. ret = FAIL;
  1016. goto exit_burn_fw_dsp;
  1017. }
  1018. /* step4:hold ss51 & dsp */
  1019. GTP_DEBUG("[burn_fw_dsp]step4:hold ss51 & dsp");
  1020. ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x0C);
  1021. if (ret <= 0) {
  1022. GTP_ERROR("[burn_fw_dsp]hold ss51 & dsp fail.");
  1023. ret = FAIL;
  1024. goto exit_burn_fw_dsp;
  1025. }
  1026. /* step5:set scramble */
  1027. GTP_DEBUG("[burn_fw_dsp]step5:set scramble");
  1028. ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_OPT_B0_, 0x00);
  1029. if (ret <= 0) {
  1030. GTP_ERROR("[burn_fw_dsp]set scramble fail.");
  1031. ret = FAIL;
  1032. goto exit_burn_fw_dsp;
  1033. }
  1034. /* step6:release ss51 & dsp */
  1035. GTP_DEBUG("[burn_fw_dsp]step6:release ss51 & dsp");
  1036. ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x04); /* 20121212 */
  1037. if (ret <= 0) {
  1038. GTP_ERROR("[burn_fw_dsp]release ss51 & dsp fail.");
  1039. ret = FAIL;
  1040. goto exit_burn_fw_dsp;
  1041. }
  1042. /* must delay */
  1043. msleep(20);
  1044. /* step7:burn 4k dsp firmware */
  1045. GTP_DEBUG("[burn_fw_dsp]step7:burn 4k dsp firmware");
  1046. ret = gup_burn_proc(client, fw_dsp, 0x9000, FW_DSP_LENGTH);
  1047. if (FAIL == ret) {
  1048. GTP_ERROR("[burn_fw_dsp]burn fw_section fail.");
  1049. goto exit_burn_fw_dsp;
  1050. }
  1051. /* step8:send burn cmd to move data to flash from sram */
  1052. GTP_DEBUG("[burn_fw_dsp]step8:send burn cmd to move data to flash from sram");
  1053. ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, 0x05);
  1054. if (ret <= 0) {
  1055. GTP_ERROR("[burn_fw_dsp]send burn cmd fail.");
  1056. goto exit_burn_fw_dsp;
  1057. }
  1058. GTP_DEBUG("[burn_fw_dsp]Wait for the burn is complete......");
  1059. do {
  1060. ret = gup_get_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, rd_buf, 1);
  1061. if (ret <= 0) {
  1062. GTP_ERROR("[burn_fw_dsp]Get burn state fail");
  1063. goto exit_burn_fw_dsp;
  1064. }
  1065. msleep(20);
  1066. /* GTP_DEBUG("[burn_fw_dsp]Get burn state:%d.", rd_buf[GTP_ADDR_LENGTH]); */
  1067. } while (rd_buf[GTP_ADDR_LENGTH]);
  1068. /* step9:recall check 4k dsp firmware */
  1069. GTP_DEBUG("[burn_fw_dsp]step9:recall check 4k dsp firmware");
  1070. ret = gup_recall_check(client, fw_dsp, 0x9000, FW_DSP_LENGTH);
  1071. if (FAIL == ret) {
  1072. GTP_ERROR("[burn_fw_dsp]recall check 4k dsp firmware fail.");
  1073. goto exit_burn_fw_dsp;
  1074. }
  1075. ret = SUCCESS;
  1076. exit_burn_fw_dsp:
  1077. kfree(fw_dsp);
  1078. return ret;
  1079. }
  1080. static u8 gup_burn_fw_boot(struct i2c_client *client)
  1081. {
  1082. s32 ret = 0;
  1083. u8 *fw_boot = NULL;
  1084. u8 retry = 0;
  1085. u8 rd_buf[5];
  1086. GTP_INFO("[burn_fw_boot]Begin burn bootloader firmware---->>");
  1087. /* step1:Alloc memory */
  1088. GTP_DEBUG("[burn_fw_boot]step1:Alloc memory");
  1089. while (retry++ < 5) {
  1090. fw_boot = kzalloc(FW_BOOT_LENGTH, GFP_KERNEL);
  1091. if (fw_boot == NULL) {
  1092. continue;
  1093. } else {
  1094. GTP_INFO("[burn_fw_boot]Alloc %dk byte memory success.",
  1095. (FW_BOOT_LENGTH / 1024));
  1096. break;
  1097. }
  1098. }
  1099. if (retry >= 5) {
  1100. GTP_ERROR("[burn_fw_boot]Alloc memory fail,exit.");
  1101. return FAIL;
  1102. }
  1103. /* step2:load firmware bootloader */
  1104. GTP_DEBUG("[burn_fw_boot]step2:load firmware bootloader");
  1105. ret =
  1106. gup_load_section_file(fw_boot, (4 * FW_SECTION_LENGTH + FW_DSP_LENGTH), FW_BOOT_LENGTH);
  1107. if (FAIL == ret) {
  1108. GTP_ERROR("[burn_fw_boot]load firmware dsp fail.");
  1109. goto exit_burn_fw_boot;
  1110. }
  1111. /* step3:hold ss51 & dsp */
  1112. GTP_DEBUG("[burn_fw_boot]step3:hold ss51 & dsp");
  1113. ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x0C);
  1114. if (ret <= 0) {
  1115. GTP_ERROR("[burn_fw_boot]hold ss51 & dsp fail.");
  1116. ret = FAIL;
  1117. goto exit_burn_fw_boot;
  1118. }
  1119. /* step4:set scramble */
  1120. GTP_DEBUG("[burn_fw_boot]step4:set scramble");
  1121. ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_OPT_B0_, 0x00);
  1122. if (ret <= 0) {
  1123. GTP_ERROR("[burn_fw_boot]set scramble fail.");
  1124. ret = FAIL;
  1125. goto exit_burn_fw_boot;
  1126. }
  1127. /* step5:release ss51 & dsp */
  1128. GTP_DEBUG("[burn_fw_boot]step5:release ss51 & dsp");
  1129. ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x04); /* 20121212 */
  1130. if (ret <= 0) {
  1131. GTP_ERROR("[burn_fw_boot]release ss51 & dsp fail.");
  1132. ret = FAIL;
  1133. goto exit_burn_fw_boot;
  1134. }
  1135. /* must delay */
  1136. msleep(20);
  1137. /* step6:burn 2k bootloader firmware */
  1138. GTP_DEBUG("[burn_fw_boot]step6:burn 2k bootloader firmware");
  1139. ret = gup_burn_proc(client, fw_boot, 0x9000, FW_BOOT_LENGTH);
  1140. if (FAIL == ret) {
  1141. GTP_ERROR("[burn_fw_boot]burn fw_section fail.");
  1142. goto exit_burn_fw_boot;
  1143. }
  1144. /* step7:send burn cmd to move data to flash from sram */
  1145. GTP_DEBUG("[burn_fw_boot]step7:send burn cmd to move data to flash from sram");
  1146. ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, 0x06);
  1147. if (ret <= 0) {
  1148. GTP_ERROR("[burn_fw_boot]send burn cmd fail.");
  1149. goto exit_burn_fw_boot;
  1150. }
  1151. GTP_DEBUG("[burn_fw_boot]Wait for the burn is complete......");
  1152. do {
  1153. ret = gup_get_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, rd_buf, 1);
  1154. if (ret <= 0) {
  1155. GTP_ERROR("[burn_fw_boot]Get burn state fail");
  1156. goto exit_burn_fw_boot;
  1157. }
  1158. msleep(20);
  1159. /* GTP_DEBUG("[burn_fw_boot]Get burn state:%d.", rd_buf[GTP_ADDR_LENGTH]); */
  1160. } while (rd_buf[GTP_ADDR_LENGTH]);
  1161. /* step8:recall check 2k bootloader firmware */
  1162. GTP_DEBUG("[burn_fw_boot]step8:recall check 2k bootloader firmware");
  1163. ret = gup_recall_check(client, fw_boot, 0x9000, FW_BOOT_LENGTH);
  1164. if (FAIL == ret) {
  1165. GTP_ERROR("[burn_fw_boot]recall check 4k dsp firmware fail.");
  1166. goto exit_burn_fw_boot;
  1167. }
  1168. /* step9:enable download DSP code */
  1169. GTP_DEBUG("[burn_fw_boot]step9:enable download DSP code ");
  1170. ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, 0x99);
  1171. if (ret <= 0) {
  1172. GTP_ERROR("[burn_fw_boot]enable download DSP code fail.");
  1173. ret = FAIL;
  1174. goto exit_burn_fw_boot;
  1175. }
  1176. /* step10:release ss51 & hold dsp */
  1177. GTP_DEBUG("[burn_fw_boot]step10:release ss51 & hold dsp");
  1178. ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x08);
  1179. if (ret <= 0) {
  1180. GTP_ERROR("[burn_fw_boot]release ss51 & hold dsp fail.");
  1181. ret = FAIL;
  1182. goto exit_burn_fw_boot;
  1183. }
  1184. ret = SUCCESS;
  1185. exit_burn_fw_boot:
  1186. kfree(fw_boot);
  1187. return ret;
  1188. }
  1189. s32 gup_update_proc(void *dir)
  1190. {
  1191. s32 ret = 0;
  1192. u8 retry = 0;
  1193. struct st_fw_head fw_head;
  1194. GTP_INFO("[update_proc]Begin update ......");
  1195. if (dir == NULL)
  1196. msleep(3000); /* wait main thread to be completed */
  1197. if (1 == searching_file) {
  1198. searching_file = 0; /* exit .bin update file searching */
  1199. GTP_INFO("Exiting searching file for auto update...");
  1200. while ((show_len != 200) && (show_len != 100))
  1201. msleep(100);
  1202. }
  1203. show_len = 1;
  1204. total_len = 100;
  1205. #if GTP_COMPATIBLE_MODE
  1206. if (CHIP_TYPE_GT9F == gtp_chip_type)
  1207. return gup_fw_download_proc(dir, GTP_FL_FW_BURN);
  1208. #endif
  1209. ret = gup_check_update_file(i2c_client_point, &fw_head, (u8 *) dir); /* 20121212 */
  1210. if (FAIL == ret) {
  1211. GTP_ERROR("[update_proc]check update file fail.");
  1212. goto file_fail;
  1213. }
  1214. /* gtp_reset_guitar(i2c_client_point, 20); */
  1215. ret = gup_get_ic_fw_msg(i2c_client_point);
  1216. if (FAIL == ret) {
  1217. GTP_ERROR("[update_proc]get ic message fail.");
  1218. goto file_fail;
  1219. }
  1220. ret = gup_enter_update_judge(&fw_head); /* 20121212 */
  1221. if (FAIL == ret) {
  1222. GTP_ERROR("[update_proc]Check *.bin file fail.");
  1223. goto file_fail;
  1224. }
  1225. /* mt_eint_mask(CUST_EINT_TOUCH_PANEL_NUM); */
  1226. disable_irq(touch_irq);
  1227. #if GTP_ESD_PROTECT
  1228. gtp_esd_switch(i2c_client_point, SWITCH_OFF);
  1229. #endif
  1230. ret = gup_enter_update_mode(i2c_client_point);
  1231. if (FAIL == ret) {
  1232. GTP_ERROR("[update_proc]enter update mode fail.");
  1233. goto update_fail;
  1234. }
  1235. while (retry++ < 5) {
  1236. show_len = 10;
  1237. total_len = 100;
  1238. ret = gup_burn_dsp_isp(i2c_client_point);
  1239. if (FAIL == ret) {
  1240. GTP_ERROR("[update_proc]burn dsp isp fail.");
  1241. continue;
  1242. }
  1243. show_len += 10;
  1244. ret = gup_burn_fw_ss51(i2c_client_point);
  1245. if (FAIL == ret) {
  1246. GTP_ERROR("[update_proc]burn ss51 firmware fail.");
  1247. continue;
  1248. }
  1249. show_len += 40;
  1250. ret = gup_burn_fw_dsp(i2c_client_point);
  1251. if (FAIL == ret) {
  1252. GTP_ERROR("[update_proc]burn dsp firmware fail.");
  1253. continue;
  1254. }
  1255. show_len += 20;
  1256. ret = gup_burn_fw_boot(i2c_client_point);
  1257. if (FAIL == ret) {
  1258. GTP_ERROR("[update_proc]burn bootloader firmware fail.");
  1259. continue;
  1260. }
  1261. show_len += 10;
  1262. GTP_INFO("[update_proc]UPDATE SUCCESS.");
  1263. break;
  1264. }
  1265. if (retry >= 5) {
  1266. GTP_ERROR("[update_proc]retry timeout,UPDATE FAIL.");
  1267. goto update_fail;
  1268. }
  1269. /* GTP_DEBUG("[update_proc]reset chip."); */
  1270. /* gtp_reset_guitar(i2c_client_point, 20); */
  1271. /* msleep(100); */
  1272. show_len = 100;
  1273. total_len = 100;
  1274. GTP_DEBUG("[update_proc]leave update mode.");
  1275. gup_leave_update_mode();
  1276. GTP_DEBUG("[update_proc]send config.");
  1277. ret = gtp_send_cfg(i2c_client_point);
  1278. if (ret < 0)
  1279. GTP_ERROR("[update_proc]send config fail.");
  1280. if (update_msg.file && !IS_ERR(update_msg.file))
  1281. filp_close(update_msg.file, NULL);
  1282. /* mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM); */
  1283. enable_irq(touch_irq);
  1284. #if GTP_ESD_PROTECT
  1285. gtp_esd_switch(i2c_client_point, SWITCH_ON);
  1286. #endif
  1287. return SUCCESS;
  1288. update_fail:
  1289. GTP_DEBUG("[update_proc]leave update mode.");
  1290. gup_leave_update_mode();
  1291. GTP_DEBUG("[update_proc]send config.");
  1292. ret = gtp_send_cfg(i2c_client_point);
  1293. if (ret < 0)
  1294. GTP_ERROR("[update_proc]send config fail.");
  1295. /* mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM); */
  1296. enable_irq(touch_irq);
  1297. #if GTP_ESD_PROTECT
  1298. gtp_esd_switch(i2c_client_point, SWITCH_ON);
  1299. #endif
  1300. file_fail:
  1301. if (update_msg.file && !IS_ERR(update_msg.file))
  1302. filp_close(update_msg.file, NULL);
  1303. show_len = 200;
  1304. total_len = 100;
  1305. return FAIL;
  1306. }
  1307. u8 gup_init_update_proc(struct i2c_client *client)
  1308. {
  1309. struct task_struct *thread = NULL;
  1310. GTP_INFO("Ready to run update thread");
  1311. #if GTP_COMPATIBLE_MODE
  1312. if (CHIP_TYPE_GT9F == gtp_chip_type) {
  1313. thread = kthread_run(gup_update_proc, "update", "fl_auto_update");
  1314. } else
  1315. #endif
  1316. {
  1317. thread = kthread_run(gup_update_proc, (void *)NULL, "guitar_update");
  1318. }
  1319. if (IS_ERR(thread)) {
  1320. GTP_ERROR("Failed to create update thread.\n");
  1321. return -1;
  1322. }
  1323. return 0;
  1324. }
  1325. /* ******************* For GT9XXF Start ********************/
  1326. #define FL_UPDATE_PATH "/data/_fl_update_.bin"
  1327. #define FL_UPDATE_PATH_SD "/sdcard/_fl_update_.bin"
  1328. #define GUP_FW_CHK_SIZE 256
  1329. #define MAX_CHECK_TIMES 128 /* max: 2 * (16 * 1024) / 256 = 128 */
  1330. /* for clk cal */
  1331. #define PULSE_LENGTH (200)
  1332. #define INIT_CLK_DAC (50)
  1333. #define MAX_CLK_DAC (120)
  1334. #define CLK_AVG_TIME (1)
  1335. #define MILLION 1000000
  1336. #define _wRW_MISCTL__RG_DMY 0x4282
  1337. #define _bRW_MISCTL__RG_OSC_CALIB 0x4268
  1338. #define _fRW_MISCTL__GIO0 0x41e9
  1339. #define _fRW_MISCTL__GIO1 0x41ed
  1340. #define _fRW_MISCTL__GIO2 0x41f1
  1341. #define _fRW_MISCTL__GIO3 0x41f5
  1342. #define _fRW_MISCTL__GIO4 0x41f9
  1343. #define _fRW_MISCTL__GIO5 0x41fd
  1344. #define _fRW_MISCTL__GIO6 0x4201
  1345. #define _fRW_MISCTL__GIO7 0x4205
  1346. #define _fRW_MISCTL__GIO8 0x4209
  1347. #define _fRW_MISCTL__GIO9 0x420d
  1348. #define _fRW_MISCTL__MEA 0x41a0
  1349. #define _bRW_MISCTL__MEA_MODE 0x41a1
  1350. #define _wRW_MISCTL__MEA_MAX_NUM 0x41a4
  1351. #define _dRO_MISCTL__MEA_VAL 0x41b0
  1352. #define _bRW_MISCTL__MEA_SRCSEL 0x41a3
  1353. #define _bRO_MISCTL__MEA_RDY 0x41a8
  1354. #define _rRW_MISCTL__ANA_RXADC_B0_ 0x4250
  1355. #define _bRW_MISCTL__RG_LDO_A18_PWD 0x426f
  1356. #define _bRW_MISCTL__RG_BG_PWD 0x426a
  1357. #define _bRW_MISCTL__RG_CLKGEN_PWD 0x4269
  1358. #define _fRW_MISCTL__RG_RXADC_PWD 0x426a
  1359. #define _bRW_MISCTL__OSC_CK_SEL 0x4030
  1360. #define _rRW_MISCTL_RG_DMY83 0x4283
  1361. #define _rRW_MISCTL__GIO1CTL_B2_ 0x41ee
  1362. #define _rRW_MISCTL__GIO1CTL_B1_ 0x41ed
  1363. #if GTP_COMPATIBLE_MODE
  1364. static u8 gup_check_and_repair(struct i2c_client *client, s32 chk_start_addr,
  1365. u8 *target_fw, u32 chk_total_length);
  1366. s32 gup_hold_ss51_dsp(struct i2c_client *client)
  1367. {
  1368. s32 ret = -1;
  1369. s32 retry = 0;
  1370. u8 rd_buf[3];
  1371. while (retry++ < 200) {
  1372. /* step4:Hold ss51 & dsp */
  1373. ret = gup_set_ic_msg(client, _rRW_MISCTL__SWRST_B0_, 0x0C);
  1374. if (ret <= 0) {
  1375. GTP_DEBUG("Hold ss51 & dsp I2C error,retry:%d", retry);
  1376. continue;
  1377. }
  1378. /* step5:Confirm hold */
  1379. ret = gup_get_ic_msg(client, _rRW_MISCTL__SWRST_B0_, rd_buf, 1);
  1380. if (ret <= 0) {
  1381. GTP_DEBUG("Hold ss51 & dsp I2C error,retry:%d", retry);
  1382. continue;
  1383. }
  1384. if (0x0C == rd_buf[GTP_ADDR_LENGTH]) {
  1385. GTP_DEBUG("[enter_update_mode]Hold ss51 & dsp confirm SUCCESS");
  1386. break;
  1387. }
  1388. GTP_DEBUG("Hold ss51 & dsp confirm 0x4180 failed,value:%d",
  1389. rd_buf[GTP_ADDR_LENGTH]);
  1390. }
  1391. if (retry >= 200) {
  1392. GTP_ERROR("Enter update Hold ss51 failed.");
  1393. return FAIL;
  1394. }
  1395. /* DSP_CK and DSP_ALU_CK PowerOn */
  1396. ret = gup_set_ic_msg(client, 0x4010, 0x00);
  1397. if (ret <= 0) {
  1398. GTP_ERROR("[enter_update_mode]DSP_CK and DSP_ALU_CK PowerOn fail.");
  1399. return FAIL;
  1400. }
  1401. /* disable wdt */
  1402. ret = gup_set_ic_msg(client, _bRW_MISCTL__TMR0_EN, 0x00);
  1403. if (ret <= 0) {
  1404. GTP_ERROR("[enter_update_mode]disable wdt fail.");
  1405. return FAIL;
  1406. }
  1407. /* clear cache enable */
  1408. ret = gup_set_ic_msg(client, _bRW_MISCTL__CACHE_EN, 0x00);
  1409. if (ret <= 0) {
  1410. GTP_ERROR("[enter_update_mode]clear cache enable fail.");
  1411. return FAIL;
  1412. }
  1413. /* set boot from sram */
  1414. ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOTCTL_B0_, 0x02);
  1415. if (ret <= 0) {
  1416. GTP_ERROR("[enter_update_mode]set boot from sram fail.");
  1417. return FAIL;
  1418. }
  1419. /* software reboot */
  1420. ret = gup_set_ic_msg(client, _bWO_MISCTL__CPU_SWRST_PULSE, 0x01);
  1421. if (ret <= 0) {
  1422. GTP_ERROR("[enter_update_mode]software reboot fail.");
  1423. return FAIL;
  1424. }
  1425. return SUCCESS;
  1426. }
  1427. s32 gup_enter_update_mode_fl(struct i2c_client *client)
  1428. {
  1429. s32 ret = -1;
  1430. /* s32 retry = 0; */
  1431. /* u8 rd_buf[3]; */
  1432. /* step1:RST output low last at least 2ms */
  1433. tpd_gpio_output(GTP_RST_PORT, 0);
  1434. msleep(20);
  1435. /* step2:select I2C slave addr,INT:0--0xBA;1--0x28. */
  1436. tpd_gpio_output(GTP_INT_PORT, (client->addr == 0x14));
  1437. msleep(20);
  1438. /* step3:RST output high reset guitar */
  1439. tpd_gpio_output(GTP_RST_PORT, 1);
  1440. msleep(20);
  1441. /* select addr & hold ss51_dsp */
  1442. ret = gup_hold_ss51_dsp(client);
  1443. if (ret <= 0) {
  1444. GTP_ERROR("[enter_update_mode]hold ss51 & dsp failed.");
  1445. return FAIL;
  1446. }
  1447. /* clear control flag */
  1448. ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_CTL_, 0x00);
  1449. if (ret <= 0) {
  1450. GTP_ERROR("[enter_update_mode]clear control flag fail.");
  1451. return FAIL;
  1452. }
  1453. /* set scramble */
  1454. ret = gup_set_ic_msg(client, _rRW_MISCTL__BOOT_OPT_B0_, 0x00);
  1455. if (ret <= 0) {
  1456. GTP_ERROR("[enter_update_mode]set scramble fail.");
  1457. return FAIL;
  1458. }
  1459. /* enable accessing code */
  1460. ret = gup_set_ic_msg(client, _bRW_MISCTL__MEM_CD_EN, 0x01);
  1461. if (ret <= 0) {
  1462. GTP_ERROR("[enter_update_mode]enable accessing code fail.");
  1463. return FAIL;
  1464. }
  1465. return SUCCESS;
  1466. }
  1467. static s32 gup_prepare_fl_fw(char *path, struct st_fw_head *fw_head)
  1468. {
  1469. s32 i = 0;
  1470. s32 ret = 0;
  1471. s32 timeout = 0;
  1472. if (!memcmp(path, "update", 6)) {
  1473. GTP_INFO("Search for Flashless firmware file to update");
  1474. searching_file = 1;
  1475. for (i = 0; i < GUP_SEARCH_FILE_TIMES; ++i) {
  1476. if (0 == searching_file) {
  1477. GTP_INFO("Force terminate searching file auto update.");
  1478. return FAIL;
  1479. }
  1480. update_msg.file = filp_open(FL_UPDATE_PATH, O_RDONLY, 0);
  1481. if (IS_ERR(update_msg.file)) {
  1482. update_msg.file = filp_open(FL_UPDATE_PATH_SD, O_RDONLY, 0);
  1483. if (IS_ERR(update_msg.file)) {
  1484. msleep(2000);
  1485. continue;
  1486. } else {
  1487. path = FL_UPDATE_PATH_SD;
  1488. break;
  1489. }
  1490. } else {
  1491. path = FL_UPDATE_PATH;
  1492. break;
  1493. }
  1494. }
  1495. searching_file = 0;
  1496. if (i >= 50) {
  1497. GTP_ERROR("Search timeout, update aborted");
  1498. return FAIL;
  1499. }
  1500. if (!IS_ERR(update_msg.file))
  1501. filp_close(update_msg.file, NULL);
  1502. while (rqst_processing && (timeout++ < 15)) {
  1503. GTP_INFO("wait for request process completed!");
  1504. msleep(1000);
  1505. }
  1506. }
  1507. GTP_INFO("Firmware update file path: %s", path);
  1508. update_msg.file = filp_open(path, O_RDONLY, 0);
  1509. if (IS_ERR(update_msg.file)) {
  1510. GTP_ERROR("Open update file(%s) error!", path);
  1511. return FAIL;
  1512. }
  1513. ret = gup_get_ic_fw_msg(i2c_client_point);
  1514. if (FAIL == ret) {
  1515. GTP_ERROR("failed to get ic firmware info");
  1516. filp_close(update_msg.file, NULL);
  1517. return FAIL;
  1518. }
  1519. update_msg.old_fs = get_fs();
  1520. set_fs(KERNEL_DS);
  1521. update_msg.file->f_op->llseek(update_msg.file, 0, SEEK_SET);
  1522. update_msg.file->f_op->read(update_msg.file, (char *)fw_head,
  1523. FW_HEAD_LENGTH, &update_msg.file->f_pos);
  1524. ret = gup_enter_update_judge(fw_head);
  1525. if (FAIL == ret) {
  1526. set_fs(update_msg.old_fs);
  1527. if (update_msg.file && !IS_ERR(update_msg.file))
  1528. filp_close(update_msg.file, NULL);
  1529. return FAIL;
  1530. }
  1531. update_msg.file->f_op->llseek(update_msg.file, 0, SEEK_SET);
  1532. /* copy fw file to gtp_default_FW_fl array */
  1533. ret = update_msg.file->f_op->read(update_msg.file,
  1534. (char *)gtp_default_FW_fl,
  1535. FW_HEAD_LENGTH +
  1536. 2 * FW_DOWNLOAD_LENGTH +
  1537. FW_DSP_LENGTH, &update_msg.file->f_pos);
  1538. if (ret < 0) {
  1539. GTP_ERROR("failed to read firmware data from %s, err-code: %d", path, ret);
  1540. ret = FAIL;
  1541. } else {
  1542. ret = SUCCESS;
  1543. }
  1544. set_fs(update_msg.old_fs);
  1545. if (!IS_ERR(update_msg.file))
  1546. filp_close(update_msg.file, NULL);
  1547. return ret;
  1548. }
  1549. static u8 gup_check_update_file_fl(struct i2c_client *client,
  1550. struct st_fw_head *fw_head, char *path)
  1551. {
  1552. s32 i = 0;
  1553. s32 fw_checksum = 0;
  1554. s32 ret = 0;
  1555. if (NULL != path) {
  1556. ret = gup_prepare_fl_fw(path, fw_head);
  1557. if (ret == FAIL)
  1558. return FAIL;
  1559. } else {
  1560. memcpy(fw_head, gtp_default_FW_fl, FW_HEAD_LENGTH);
  1561. GTP_INFO("FILE HARDWARE INFO:%02x%02x%02x%02x",
  1562. fw_head->hw_info[0], fw_head->hw_info[1],
  1563. fw_head->hw_info[2], fw_head->hw_info[3]);
  1564. GTP_INFO("FILE PID:%s", fw_head->pid);
  1565. fw_head->vid = ((fw_head->vid & 0xFF00) >> 8) + ((fw_head->vid & 0x00FF) << 8);
  1566. GTP_INFO("FILE VID:%04x", fw_head->vid);
  1567. }
  1568. /* check firmware legality */
  1569. fw_checksum = 0;
  1570. for (i = FW_HEAD_LENGTH; i < FW_HEAD_LENGTH + FW_SECTION_LENGTH * 4 + FW_DSP_LENGTH; i += 2) {
  1571. u16 temp;
  1572. /* GTP_DEBUG("BUF[0]:%x", buf[0]); */
  1573. temp = (gtp_default_FW_fl[i] << 8) + gtp_default_FW_fl[i + 1];
  1574. fw_checksum += temp;
  1575. }
  1576. GTP_DEBUG("firmware checksum:%x", fw_checksum & 0xFFFF);
  1577. if (fw_checksum & 0xFFFF) {
  1578. GTP_ERROR("Illegal firmware file.");
  1579. return FAIL;
  1580. }
  1581. return SUCCESS;
  1582. }
  1583. static u8 gup_download_fw_ss51(struct i2c_client *client, u8 dwn_mode)
  1584. {
  1585. s32 ret = 0;
  1586. if (GTP_FL_FW_BURN == dwn_mode)
  1587. GTP_INFO("[download_fw_ss51]Begin download ss51 firmware---->>");
  1588. else
  1589. GTP_INFO("[download_fw_ss51]Begin check ss51 firmware----->>");
  1590. /* step1:download FW section 1 */
  1591. GTP_DEBUG("[download_fw_ss51]step1:download FW section 1");
  1592. ret = gup_set_ic_msg(i2c_client_point, _bRW_MISCTL__SRAM_BANK, 0x00);
  1593. if (ret <= 0) {
  1594. GTP_ERROR("[download_fw_ss51]select bank0 fail.");
  1595. ret = FAIL;
  1596. goto exit_download_fw_ss51;
  1597. }
  1598. if (GTP_FL_FW_BURN == dwn_mode) {
  1599. ret =
  1600. i2c_write_bytes(client, 0xC000, &gtp_default_FW_fl[FW_HEAD_LENGTH],
  1601. FW_DOWNLOAD_LENGTH);
  1602. if (ret == -1) {
  1603. GTP_ERROR("[download_fw_ss51]download FW section 1 fail.");
  1604. ret = FAIL;
  1605. goto exit_download_fw_ss51;
  1606. }
  1607. }
  1608. ret = gup_check_and_repair(i2c_client_point,
  1609. 0xC000, &gtp_default_FW_fl[FW_HEAD_LENGTH], FW_DOWNLOAD_LENGTH);
  1610. if (FAIL == ret) {
  1611. GTP_ERROR("[download_fw_ss51]Checked FW section 1 fail.");
  1612. goto exit_download_fw_ss51;
  1613. }
  1614. /* step2:download FW section 2 */
  1615. GTP_DEBUG("[download_fw_ss51]step2:download FW section 1");
  1616. ret = gup_set_ic_msg(i2c_client_point, _bRW_MISCTL__SRAM_BANK, 0x01);
  1617. if (ret <= 0) {
  1618. GTP_ERROR("[download_fw_ss51]select bank1 fail.");
  1619. ret = FAIL;
  1620. goto exit_download_fw_ss51;
  1621. }
  1622. if (GTP_FL_FW_BURN == dwn_mode) {
  1623. ret =
  1624. i2c_write_bytes(client, 0xC000,
  1625. &gtp_default_FW_fl[FW_HEAD_LENGTH +
  1626. FW_DOWNLOAD_LENGTH], FW_DOWNLOAD_LENGTH);
  1627. if (ret == -1) {
  1628. GTP_ERROR("[download_fw_ss51]download FW section 2 fail.");
  1629. ret = FAIL;
  1630. goto exit_download_fw_ss51;
  1631. }
  1632. }
  1633. ret = gup_check_and_repair(i2c_client_point,
  1634. 0xC000,
  1635. &gtp_default_FW_fl[FW_HEAD_LENGTH +
  1636. FW_DOWNLOAD_LENGTH], FW_DOWNLOAD_LENGTH);
  1637. if (FAIL == ret) {
  1638. GTP_ERROR("[download_fw_ss51]Checked FW section 2 fail.");
  1639. goto exit_download_fw_ss51;
  1640. }
  1641. ret = SUCCESS;
  1642. exit_download_fw_ss51:
  1643. return ret;
  1644. }
  1645. #if (!GTP_SUPPORT_I2C_DMA)
  1646. static s32 i2c_auto_read(struct i2c_client *client, u8 *rxbuf, int len)
  1647. {
  1648. u8 retry;
  1649. u16 left = len;
  1650. u16 offset = 0;
  1651. struct i2c_msg msg = {
  1652. .addr = ((client->addr & I2C_MASK_FLAG) | (I2C_ENEXT_FLAG)),
  1653. /*.addr = ((client->addr & I2C_MASK_FLAG) | (I2C_PUSHPULL_FLAG)),*/
  1654. .flags = I2C_M_RD,
  1655. .timing = I2C_MASTER_CLOCK
  1656. };
  1657. if (NULL == rxbuf)
  1658. return -1;
  1659. while (left > 0) {
  1660. msg.buf = &rxbuf[offset];
  1661. if (left > MAX_TRANSACTION_LENGTH) {
  1662. msg.len = MAX_TRANSACTION_LENGTH;
  1663. left -= MAX_TRANSACTION_LENGTH;
  1664. offset += MAX_TRANSACTION_LENGTH;
  1665. } else {
  1666. msg.len = left;
  1667. left = 0;
  1668. }
  1669. retry = 0;
  1670. while (i2c_transfer(client->adapter, &msg, 1) != 1) {
  1671. retry++;
  1672. if (retry == 20) {
  1673. GTP_ERROR("I2C read 0x%X length=%d failed\n", offset, len);
  1674. return -1;
  1675. }
  1676. }
  1677. }
  1678. return 0;
  1679. }
  1680. #endif
  1681. static u8 gup_check_and_repair(struct i2c_client *client, s32 chk_start_addr,
  1682. u8 *target_fw, u32 chk_total_length)
  1683. {
  1684. s32 ret = 0;
  1685. u32 checked_len = 0;
  1686. u8 checked_times = 0;
  1687. u32 chk_addr = 0;
  1688. u8 chk_buf[GUP_FW_CHK_SIZE];
  1689. u32 rd_size = 0;
  1690. u8 flag_err = 0;
  1691. s32 i = 0;
  1692. chk_addr = chk_start_addr;
  1693. while ((checked_times < MAX_CHECK_TIMES)
  1694. && (checked_len < chk_total_length)) {
  1695. rd_size = chk_total_length - checked_len;
  1696. if (rd_size >= GUP_FW_CHK_SIZE)
  1697. rd_size = GUP_FW_CHK_SIZE;
  1698. #if GTP_SUPPORT_I2C_DMA
  1699. ret = i2c_read_bytes(client, chk_addr, chk_buf, rd_size);
  1700. #else
  1701. if (!i)
  1702. ret = i2c_read_bytes(client, chk_addr, chk_buf, rd_size);
  1703. else
  1704. ret = i2c_auto_read(client, chk_buf, rd_size);
  1705. #endif
  1706. if (-1 == ret) {
  1707. GTP_ERROR("Read chk ram fw i2c error, client addr:0x%x", client->addr);
  1708. checked_times++;
  1709. continue;
  1710. }
  1711. for (i = 0; i < rd_size; i++) {
  1712. if (chk_buf[i] != target_fw[i]) {
  1713. GTP_ERROR("Ram pos[0x%04x] checked failed,rewrite.", chk_addr + i);
  1714. i2c_write_bytes(client, chk_addr + i, &target_fw[i], rd_size - i);
  1715. flag_err = 1;
  1716. i = 0;
  1717. break;
  1718. }
  1719. }
  1720. if (!flag_err) {
  1721. GTP_DEBUG("Ram pos[0x%04X] check pass!", chk_addr);
  1722. checked_len += rd_size;
  1723. target_fw += rd_size;
  1724. chk_addr += rd_size;
  1725. } else {
  1726. flag_err = 0;
  1727. checked_times++;
  1728. }
  1729. }
  1730. if (checked_times >= MAX_CHECK_TIMES) {
  1731. GTP_ERROR("Ram data check failed.");
  1732. return FAIL;
  1733. }
  1734. return SUCCESS;
  1735. }
  1736. static u8 gup_download_fw_dsp(struct i2c_client *client, u8 dwn_mode)
  1737. {
  1738. s32 ret = 0;
  1739. if (GTP_FL_FW_BURN == dwn_mode)
  1740. GTP_INFO("[download_fw_dsp]Begin download dsp fw---->>");
  1741. else
  1742. GTP_INFO("[download_fw_dsp]Begin check dsp fw---->>");
  1743. /* step1:select bank2 */
  1744. GTP_DEBUG("[download_fw_dsp]step1:select bank2");
  1745. ret = gup_set_ic_msg(client, _bRW_MISCTL__SRAM_BANK, 0x02);
  1746. if (ret <= 0) {
  1747. GTP_ERROR("[download_fw_dsp]select bank2 fail.");
  1748. ret = FAIL;
  1749. goto exit_download_fw_dsp;
  1750. }
  1751. if (GTP_FL_FW_BURN == dwn_mode) {
  1752. ret = i2c_write_bytes(client, 0xC000,
  1753. &gtp_default_FW_fl[FW_HEAD_LENGTH + 2 * FW_DOWNLOAD_LENGTH],
  1754. FW_DSP_LENGTH);
  1755. if (ret == -1) {
  1756. GTP_ERROR("[download_fw_dsp]download FW dsp fail.");
  1757. ret = FAIL;
  1758. goto exit_download_fw_dsp;
  1759. }
  1760. }
  1761. ret = gup_check_and_repair(client,
  1762. 0xC000,
  1763. &gtp_default_FW_fl[FW_HEAD_LENGTH +
  1764. 2 * FW_DOWNLOAD_LENGTH], FW_DSP_LENGTH);
  1765. if (FAIL == ret) {
  1766. GTP_ERROR("[download_fw_dsp]Checked FW dsp fail.");
  1767. goto exit_download_fw_dsp;
  1768. }
  1769. ret = SUCCESS;
  1770. exit_download_fw_dsp:
  1771. return ret;
  1772. }
  1773. s32 gup_fw_download_proc(void *dir, u8 dwn_mode)
  1774. {
  1775. s32 ret = 0;
  1776. u8 retry = 0;
  1777. struct st_fw_head fw_head;
  1778. if (GTP_FL_FW_BURN == dwn_mode)
  1779. GTP_INFO("[fw_download_proc]Begin fw download ......");
  1780. else
  1781. GTP_INFO("[fw_download_proc]Begin fw check ......");
  1782. show_len = 0;
  1783. total_len = 100;
  1784. ret = gup_check_update_file_fl(i2c_client_point, &fw_head, (char *)dir);
  1785. show_len = 10;
  1786. if (FAIL == ret) {
  1787. GTP_ERROR("[fw_download_proc]check update file fail.");
  1788. goto file_fail;
  1789. }
  1790. if (!memcmp(fw_head.pid, "950", 3)) {
  1791. is_950 = 1;
  1792. GTP_DEBUG("GT9XXF ic type: gt950");
  1793. } else {
  1794. is_950 = 0;
  1795. GTP_DEBUG("GT9XXF ic type: others");
  1796. }
  1797. tpd_halt = 1;
  1798. /* mt_eint_mask(CUST_EINT_TOUCH_PANEL_NUM); */
  1799. disable_irq(touch_irq);
  1800. ret = gup_enter_update_mode_fl(i2c_client_point);
  1801. show_len = 20;
  1802. if (FAIL == ret) {
  1803. GTP_ERROR("[fw_download_proc]enter update mode fail.");
  1804. goto download_fail;
  1805. }
  1806. while (retry++ < 5) {
  1807. ret = gup_download_fw_ss51(i2c_client_point, dwn_mode);
  1808. show_len = 60;
  1809. if (FAIL == ret) {
  1810. GTP_ERROR("[fw_download_proc]burn ss51 firmware fail.");
  1811. continue;
  1812. }
  1813. ret = gup_download_fw_dsp(i2c_client_point, dwn_mode);
  1814. show_len = 80;
  1815. if (FAIL == ret) {
  1816. GTP_ERROR("[fw_download_proc]burn dsp firmware fail.");
  1817. continue;
  1818. }
  1819. GTP_INFO("[fw_download_proc]UPDATE SUCCESS.");
  1820. break;
  1821. }
  1822. if (retry >= 5) {
  1823. GTP_ERROR("[fw_download_proc]retry timeout,UPDATE FAIL.");
  1824. goto download_fail;
  1825. }
  1826. if (NULL != dir) {
  1827. gtp_fw_startup(i2c_client_point);
  1828. tpd_halt = 0;
  1829. #if GTP_ESD_PROTECT
  1830. gtp_esd_switch(i2c_client_point, SWITCH_ON);
  1831. #endif
  1832. }
  1833. show_len = 100;
  1834. tpd_halt = 0;
  1835. /* mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM); */
  1836. enable_irq(touch_irq);
  1837. return SUCCESS;
  1838. download_fail:
  1839. if (NULL != dir) {
  1840. gtp_fw_startup(i2c_client_point);
  1841. tpd_halt = 0;
  1842. #if GTP_ESD_PROTECT
  1843. gtp_esd_switch(i2c_client_point, SWITCH_ON);
  1844. #endif
  1845. }
  1846. file_fail:
  1847. show_len = 200;
  1848. tpd_halt = 0;
  1849. /* mt_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM); */
  1850. enable_irq(touch_irq);
  1851. return FAIL;
  1852. }
  1853. static void gup_bit_write(s32 addr, s32 bit, s32 val)
  1854. {
  1855. u8 buf;
  1856. i2c_read_bytes(i2c_client_point, addr, &buf, 1);
  1857. buf = (buf & (~((u8) 1 << bit))) | ((u8) val << bit);
  1858. i2c_write_bytes(i2c_client_point, addr, &buf, 1);
  1859. }
  1860. static void gup_clk_count_init(s32 bCh, s32 bCNT)
  1861. {
  1862. u8 buf;
  1863. /* _fRW_MISCTL__MEA_EN = 0; //Frequency measure enable */
  1864. gup_bit_write(_fRW_MISCTL__MEA, 0, 0);
  1865. /* _fRW_MISCTL__MEA_CLR = 1; //Frequency measure clear */
  1866. gup_bit_write(_fRW_MISCTL__MEA, 1, 1);
  1867. /* _bRW_MISCTL__MEA_MODE = 0; //Pulse mode */
  1868. buf = 0;
  1869. i2c_write_bytes(i2c_client_point, _bRW_MISCTL__MEA_MODE, &buf, 1);
  1870. /* _bRW_MISCTL__MEA_SRCSEL = 8 + bCh; //From GIO1 */
  1871. buf = 8 + bCh;
  1872. i2c_write_bytes(i2c_client_point, _bRW_MISCTL__MEA_SRCSEL, &buf, 1);
  1873. /* _wRW_MISCTL__MEA_MAX_NUM = bCNT; //Set the Measure Counts = 1 */
  1874. buf = bCNT;
  1875. i2c_write_bytes(i2c_client_point, _wRW_MISCTL__MEA_MAX_NUM, &buf, 1);
  1876. /* _fRW_MISCTL__MEA_CLR = 0; //Frequency measure not clear */
  1877. gup_bit_write(_fRW_MISCTL__MEA, 1, 0);
  1878. /* _fRW_MISCTL__MEA_EN = 1; */
  1879. gup_bit_write(_fRW_MISCTL__MEA, 0, 1);
  1880. }
  1881. static u32 gup_clk_count_get(void)
  1882. {
  1883. s32 ready = 0;
  1884. s32 temp;
  1885. s8 buf[4];
  1886. while ((ready == 0)) { /* Wait for measurement complete */
  1887. i2c_read_bytes(i2c_client_point, _bRO_MISCTL__MEA_RDY, buf, 1);
  1888. ready = buf[0];
  1889. }
  1890. udelay(50);
  1891. /* _fRW_MISCTL__MEA_EN = 0; */
  1892. gup_bit_write(_fRW_MISCTL__MEA, 0, 0);
  1893. i2c_read_bytes(i2c_client_point, _dRO_MISCTL__MEA_VAL, buf, 4);
  1894. GTP_INFO("Clk_count 0: %2X", buf[0]);
  1895. GTP_INFO("Clk_count 1: %2X", buf[1]);
  1896. GTP_INFO("Clk_count 2: %2X", buf[2]);
  1897. GTP_INFO("Clk_count 3: %2X", buf[3]);
  1898. temp = (s32) buf[0] + ((s32) buf[1] << 8) + ((s32) buf[2] << 16) + ((s32) buf[3] << 24);
  1899. GTP_INFO("Clk_count : %d", temp);
  1900. return temp;
  1901. }
  1902. u8 gup_clk_dac_setting(int dac)
  1903. {
  1904. s8 buf1, buf2;
  1905. i2c_read_bytes(i2c_client_point, _wRW_MISCTL__RG_DMY, &buf1, 1);
  1906. i2c_read_bytes(i2c_client_point, _bRW_MISCTL__RG_OSC_CALIB, &buf2, 1);
  1907. buf1 = (buf1 & 0xFFCF) | ((dac & 0x03) << 4);
  1908. buf2 = (dac >> 2) & 0x3f;
  1909. i2c_write_bytes(i2c_client_point, _wRW_MISCTL__RG_DMY, &buf1, 1);
  1910. i2c_write_bytes(i2c_client_point, _bRW_MISCTL__RG_OSC_CALIB, &buf2, 1);
  1911. return 0;
  1912. }
  1913. static u8 gup_clk_calibration_pin_select(s32 bCh)
  1914. {
  1915. s32 i2c_addr;
  1916. switch (bCh) {
  1917. case 0:
  1918. i2c_addr = _fRW_MISCTL__GIO0;
  1919. break;
  1920. case 1:
  1921. i2c_addr = _fRW_MISCTL__GIO1;
  1922. break;
  1923. case 2:
  1924. i2c_addr = _fRW_MISCTL__GIO2;
  1925. break;
  1926. case 3:
  1927. i2c_addr = _fRW_MISCTL__GIO3;
  1928. break;
  1929. case 4:
  1930. i2c_addr = _fRW_MISCTL__GIO4;
  1931. break;
  1932. case 5:
  1933. i2c_addr = _fRW_MISCTL__GIO5;
  1934. break;
  1935. case 6:
  1936. i2c_addr = _fRW_MISCTL__GIO6;
  1937. break;
  1938. case 7:
  1939. i2c_addr = _fRW_MISCTL__GIO7;
  1940. break;
  1941. case 8:
  1942. i2c_addr = _fRW_MISCTL__GIO8;
  1943. break;
  1944. case 9:
  1945. i2c_addr = _fRW_MISCTL__GIO9;
  1946. break;
  1947. }
  1948. gup_bit_write(i2c_addr, 1, 0);
  1949. return 0;
  1950. }
  1951. void gup_output_pulse(int t)
  1952. {
  1953. unsigned long flags;
  1954. /* s32 i; */
  1955. tpd_gpio_output(GTP_INT_PORT, 0);
  1956. udelay(10);
  1957. local_irq_save(flags);
  1958. tpd_gpio_output(GTP_INT_PORT, 1);
  1959. udelay(50);
  1960. tpd_gpio_output(GTP_INT_PORT, 0);
  1961. udelay(t - 50);
  1962. tpd_gpio_output(GTP_INT_PORT, 1);
  1963. local_irq_restore(flags);
  1964. udelay(20);
  1965. tpd_gpio_output(GTP_INT_PORT, 0);
  1966. }
  1967. static void gup_sys_clk_init(void)
  1968. {
  1969. u8 buf;
  1970. /* _fRW_MISCTL__RG_RXADC_CKMUX = 0; */
  1971. gup_bit_write(_rRW_MISCTL__ANA_RXADC_B0_, 5, 0);
  1972. /* _bRW_MISCTL__RG_LDO_A18_PWD = 0; //DrvMISCTL_A18_PowerON */
  1973. buf = 0;
  1974. i2c_write_bytes(i2c_client_point, _bRW_MISCTL__RG_LDO_A18_PWD, &buf, 1);
  1975. /* _bRW_MISCTL__RG_BG_PWD = 0; //DrvMISCTL_BG_PowerON */
  1976. buf = 0;
  1977. i2c_write_bytes(i2c_client_point, _bRW_MISCTL__RG_BG_PWD, &buf, 1);
  1978. /* _bRW_MISCTL__RG_CLKGEN_PWD = 0; //DrvMISCTL_CLKGEN_PowerON */
  1979. buf = 0;
  1980. i2c_write_bytes(i2c_client_point, _bRW_MISCTL__RG_CLKGEN_PWD, &buf, 1);
  1981. /* _fRW_MISCTL__RG_RXADC_PWD = 0; //DrvMISCTL_RX_ADC_PowerON */
  1982. gup_bit_write(_rRW_MISCTL__ANA_RXADC_B0_, 0, 0);
  1983. /* _fRW_MISCTL__RG_RXADC_REF_PWD = 0; //DrvMISCTL_RX_ADCREF_PowerON */
  1984. gup_bit_write(_rRW_MISCTL__ANA_RXADC_B0_, 1, 0);
  1985. /* gup_clk_dac_setting(60); */
  1986. /* _bRW_MISCTL__OSC_CK_SEL = 1;; */
  1987. buf = 1;
  1988. i2c_write_bytes(i2c_client_point, _bRW_MISCTL__OSC_CK_SEL, &buf, 1);
  1989. }
  1990. u8 gup_clk_calibration(void)
  1991. {
  1992. /* u8 buf; */
  1993. /* u8 trigger; */
  1994. s32 i;
  1995. struct timeval start, end;
  1996. s32 count;
  1997. s32 count_ref;
  1998. s32 sec;
  1999. s32 usec;
  2000. s32 ret = 0;
  2001. /* unsigned long flags; */
  2002. /* buf = 0x0C; // hold ss51 and dsp */
  2003. /* i2c_write_bytes(i2c_client_point, _rRW_MISCTL__SWRST_B0_, &buf, 1); */
  2004. ret = gup_hold_ss51_dsp(i2c_client_point);
  2005. if (ret <= 0) {
  2006. GTP_ERROR("[gup_clk_calibration]hold ss51 & dsp failed.");
  2007. return FAIL;
  2008. }
  2009. /* _fRW_MISCTL__CLK_BIAS = 0; //disable clock bias */
  2010. gup_bit_write(_rRW_MISCTL_RG_DMY83, 7, 0);
  2011. /* _fRW_MISCTL__GIO1_PU = 0; //set TOUCH INT PIN MODE as input */
  2012. gup_bit_write(_rRW_MISCTL__GIO1CTL_B2_, 0, 0);
  2013. /* _fRW_MISCTL__GIO1_OE = 0; //set TOUCH INT PIN MODE as input */
  2014. gup_bit_write(_rRW_MISCTL__GIO1CTL_B1_, 1, 0);
  2015. /* buf = 0x00; */
  2016. /* i2c_write_bytes(i2c_client_point, _rRW_MISCTL__SWRST_B0_, &buf, 1); */
  2017. /* msleep(1000); */
  2018. GTP_INFO("CLK calibration GO");
  2019. gup_sys_clk_init();
  2020. gup_clk_calibration_pin_select(1); /* use GIO1 to do the calibration */
  2021. tpd_gpio_output(GTP_INT_PORT, 0);
  2022. for (i = INIT_CLK_DAC; i < MAX_CLK_DAC; i++) {
  2023. GTP_INFO("CLK calibration DAC %d", i);
  2024. gup_clk_dac_setting(i);
  2025. gup_clk_count_init(1, CLK_AVG_TIME);
  2026. #if 0
  2027. gup_output_pulse(PULSE_LENGTH);
  2028. count = gup_clk_count_get();
  2029. if (count > PULSE_LENGTH * 60) { /* 60= 60Mhz * 1us */
  2030. break;
  2031. }
  2032. #else
  2033. tpd_gpio_output(GTP_INT_PORT, 0);
  2034. /* local_irq_save(flags); */
  2035. do_gettimeofday(&start);
  2036. tpd_gpio_output(GTP_INT_PORT, 1);
  2037. /* local_irq_restore(flags); */
  2038. msleep(20);
  2039. tpd_gpio_output(GTP_INT_PORT, 0);
  2040. msleep(20);
  2041. /* local_irq_save(flags); */
  2042. do_gettimeofday(&end);
  2043. tpd_gpio_output(GTP_INT_PORT, 1);
  2044. /* local_irq_restore(flags); */
  2045. count = gup_clk_count_get();
  2046. udelay(20);
  2047. tpd_gpio_output(GTP_INT_PORT, 0);
  2048. usec = end.tv_usec - start.tv_usec;
  2049. sec = end.tv_sec - start.tv_sec;
  2050. count_ref = 60 * (usec + sec * MILLION);
  2051. GTP_DEBUG("== time %d, %d, %d", sec, usec, count_ref);
  2052. if (count > count_ref) {
  2053. GTP_DEBUG("== count_diff %d", count - count_ref);
  2054. break;
  2055. }
  2056. #endif
  2057. }
  2058. /* clk_dac = i; */
  2059. /* gtp_reset_guitar(i2c_client_point, 20); */
  2060. #if 0 /* for debug */
  2061. /* -- output clk to GPIO 4 */
  2062. buf = 0x00;
  2063. i2c_write_bytes(i2c_client_point, 0x41FA, &buf, 1);
  2064. buf = 0x00;
  2065. i2c_write_bytes(i2c_client_point, 0x4104, &buf, 1);
  2066. buf = 0x00;
  2067. i2c_write_bytes(i2c_client_point, 0x4105, &buf, 1);
  2068. buf = 0x00;
  2069. i2c_write_bytes(i2c_client_point, 0x4106, &buf, 1);
  2070. buf = 0x01;
  2071. i2c_write_bytes(i2c_client_point, 0x4107, &buf, 1);
  2072. buf = 0x06;
  2073. i2c_write_bytes(i2c_client_point, 0x41F8, &buf, 1);
  2074. buf = 0x02;
  2075. i2c_write_bytes(i2c_client_point, 0x41F9, &buf, 1);
  2076. #endif
  2077. tpd_gpio_as_int(GTP_INT_PORT);
  2078. return i;
  2079. }
  2080. #endif
  2081. /* *************** For GT9XXF End ***********************/