guitar_update.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /*
  2. *
  3. * Copyright (C) 2011 Goodix, Inc.
  4. *
  5. * Author: Scott
  6. * Date: 2012.01.05
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/delay.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <mach/gpio.h>
  14. //#include <plat/gpio-cfg.h>
  15. #include <linux/irq.h>
  16. #include <linux/fs.h>
  17. #include <linux/string.h>
  18. #include <asm/uaccess.h>
  19. #include <linux/mm.h>
  20. #include <linux/init.h>
  21. #include <linux/kthread.h>
  22. #include <linux/slab.h>
  23. #include "gt82x.h"
  24. //#ifdef AUTO_UPDATE_GUITAR
  25. //****************************升级模块参数******************************************
  26. #define SEARCH_FILE_TIMES 100
  27. #define UPDATE_FILE_PATH_2 "/data/goodix/_goodix_update_.bin"
  28. #define UPDATE_FILE_PATH_1 "/sdcard/goodix/_goodix_update_.bin"
  29. #define PACK_SIZE 64 //update file package size
  30. #define BIT_NVRAM_STROE 0
  31. #define BIT_NVRAM_RECALL 1
  32. #define BIT_NVRAM_LOCK 2
  33. #define REG_NVRCS_H 0X12
  34. #define REG_NVRCS_L 0X01
  35. #define READ_FW_MSG_ADDR_H 0x0F
  36. #define READ_FW_MSG_ADDR_L 0x7C
  37. #define UPDATE_FW_MSG_ADDR_H 0x40
  38. #define UPDATE_FW_MSG_ADDR_L 0x50
  39. #define READ_MSK_VER_ADDR_H 0xC0
  40. #define READ_MSK_VER_ADDR_L 0x09
  41. #define FW_HEAD_LENGTH 30
  42. #define FILE_HEAD_LENGTH 100
  43. #define IGNORE_LENGTH 100
  44. #define FW_MSG_LENGTH 7
  45. #define UPDATE_DATA_LENGTH 5000
  46. static int guitar_update_proc(void*);
  47. static u8 get_ic_fw_msg(struct goodix_ts_data *);
  48. static int guitar_update_mode(struct goodix_ts_data *);
  49. extern int goodix_init_panel(struct goodix_ts_data *, u8);
  50. #pragma pack(1)
  51. typedef struct
  52. {
  53. u8 type; //产品类型//
  54. u16 version; //FW版本号//
  55. u8 msk_ver[4]; //MASK版本//
  56. u8 st_addr[2]; //烧录的起始地址//
  57. u16 lenth; //FW长度//
  58. u8 chk_sum[3];
  59. u8 force_update[6];//强制升级标志,为"GOODIX"则强制升级//
  60. }st_fw_head;
  61. #pragma pack()
  62. typedef struct
  63. {
  64. u8 force_update;
  65. u8 fw_flag;
  66. loff_t gt_loc;
  67. struct file *file;
  68. st_fw_head ic_fw_msg;
  69. mm_segment_t old_fs;
  70. }st_update_msg;
  71. st_update_msg update_msg;
  72. //******************************************************************************
  73. static int i2c_read_bytes(struct i2c_client *client, uint8_t *buf, int len)
  74. {
  75. struct i2c_msg msgs[2];
  76. int ret=-1;
  77. //发送写地址
  78. msgs[0].flags=!I2C_M_RD; //写消息
  79. msgs[0].addr=client->addr;
  80. msgs[0].len=2;
  81. msgs[0].buf=&buf[0];
  82. //接收数据
  83. msgs[1].flags=I2C_M_RD;//读消息
  84. msgs[1].addr=client->addr;
  85. msgs[1].len=len-2;
  86. msgs[1].buf=&buf[2];
  87. ret=i2c_transfer(client->adapter,msgs, 2);
  88. return ret;
  89. }
  90. /*******************************************************
  91. 功能:
  92. 向从机写数据
  93. 参数:
  94. client: i2c设备,包含设备地址
  95. buf[0]~buf[1]: 首字节为写地址
  96. buf[2]~buf[len]:数据缓冲区
  97. len: 数据长度
  98. return:
  99. 执行消息数
  100. *******************************************************/
  101. /*Function as i2c_master_send */
  102. static int i2c_write_bytes(struct i2c_client *client,uint8_t *data,int len)
  103. {
  104. struct i2c_msg msg;
  105. int ret=-1;
  106. //发送设备地址
  107. msg.flags=!I2C_M_RD;//写消息
  108. msg.addr=client->addr;
  109. msg.len=len;
  110. msg.buf=data;
  111. ret=i2c_transfer(client->adapter,&msg, 1);
  112. return ret;
  113. }
  114. /*******************************************************
  115. 功能:
  116. 发送后缀命令
  117. ts: client私有数据结构体
  118. return:
  119. 执行结果码,0表示正常执行
  120. *******************************************************/
  121. static int i2c_end_cmd(struct goodix_ts_data *ts)
  122. {
  123. int ret;
  124. u8 end_cmd_data[2]={0x80, 0x00};
  125. ret=i2c_write_bytes(ts->client,end_cmd_data,2);
  126. return ret;//*/
  127. }
  128. static u8 is_equal( u8 *src , u8 *dst , int len )
  129. {
  130. int i;
  131. for( i = 0 ; i < len ; i++ )
  132. {
  133. if (src[i] != dst[i])
  134. {
  135. return false;
  136. }
  137. }
  138. return true;
  139. }
  140. static u8 get_ic_msg(struct goodix_ts_data *ts, u16 addr, u8* msg, s32 len)
  141. {
  142. s32 i = 0;
  143. msg[0] = addr >> 8 & 0xff;
  144. msg[1] = addr & 0xff;
  145. for (i = 0; i < 5; i++)
  146. {
  147. if (i2c_read_bytes(ts->client, msg, ADDR_LENGTH + len) > 0)
  148. {
  149. break;
  150. }
  151. }
  152. i2c_end_cmd(ts);
  153. if (i >= 5)
  154. {
  155. DEBUG_UPDATE("Read data from 0x%02x%02x failed!\n", msg[0], msg[1]);
  156. return fail;
  157. }
  158. return success;
  159. }
  160. static u8 clear_mix_flag(struct goodix_ts_data *ts)
  161. {
  162. s32 i = 0;
  163. u8 buf[3];
  164. buf[0] = 0x14;
  165. buf[1] = 0x00;
  166. buf[2] = 0x80;
  167. for (i = 0; i < 5; i++)
  168. {
  169. if (i2c_write_bytes(ts->client, buf, 3) > 0)
  170. {
  171. break;
  172. }
  173. }
  174. i2c_end_cmd(ts);
  175. if (i >= 5)
  176. {
  177. DEBUG_UPDATE("Clear mix flag failed!\n");
  178. return fail;
  179. }
  180. return success;
  181. }
  182. static u8 get_ic_fw_msg(struct goodix_ts_data *ts)
  183. {
  184. s32 ret = 0;
  185. s32 i = 0;
  186. u8 buf[32];
  187. if (fail == clear_mix_flag(ts))
  188. {
  189. return fail;
  190. }
  191. //Get the mask version in rom of IC
  192. if (fail == get_ic_msg(ts, READ_MSK_VER_ADDR_H << 8 | READ_MSK_VER_ADDR_L, buf, 4))
  193. {
  194. DEBUG_UPDATE("Read mask version failed!\n");
  195. return fail;
  196. }
  197. memcpy(update_msg.ic_fw_msg.msk_ver, &buf[ADDR_LENGTH], 4);
  198. DEBUG_UPDATE("IC The mask version in rom is %c%c%c%c.\n",
  199. update_msg.ic_fw_msg.msk_ver[0],update_msg.ic_fw_msg.msk_ver[1],
  200. update_msg.ic_fw_msg.msk_ver[2],update_msg.ic_fw_msg.msk_ver[3]);
  201. #if 1
  202. //Get the firmware msg in IC, include firmware version and checksum flag
  203. for (i = 0; i < 2; i++)
  204. {
  205. if (fail == get_ic_msg(ts, READ_FW_MSG_ADDR_H<< 8 | READ_FW_MSG_ADDR_L, buf, 4))
  206. {
  207. DEBUG_UPDATE("Get firmware msg in IC error.\n");
  208. return fail;
  209. }
  210. update_msg.force_update = buf[ADDR_LENGTH];
  211. if (i == 0 && update_msg.force_update == 0xAA)
  212. {
  213. DEBUG_UPDATE("The check sum in ic is error.\n");
  214. DEBUG_UPDATE("IC will be reset.\n");
  215. DEBUG_UPDATE("If the check sum is still error,\n ");
  216. DEBUG_UPDATE("The IC will be updated by force.\n");
  217. guitar_reset(ts, 10);
  218. continue;
  219. //msleep(100);
  220. }
  221. break;
  222. }
  223. //ic_fw_msg.type = buf[ADDR_LENGTH + 1];
  224. update_msg.ic_fw_msg.version = buf[ADDR_LENGTH + 2] << 8 | buf[ADDR_LENGTH + 3];
  225. DEBUG_UPDATE("IC VID:0x%x\n", (int)update_msg.ic_fw_msg.version);
  226. DEBUG_UPDATE("IC force update:%x\n", update_msg.force_update);
  227. #endif
  228. //Cuts the frequency
  229. buf[0] = 0x15;
  230. buf[1] = 0x22;
  231. buf[2] = 0x18;
  232. ret = i2c_write_bytes(ts->client, buf, 3);
  233. if (ret <= 0)
  234. {
  235. return fail;
  236. }
  237. i2c_end_cmd(ts);
  238. //Get the pid at 0x4011 in nvram
  239. if (fail == get_ic_msg(ts, 0x4011, buf, 1))
  240. {
  241. DEBUG_UPDATE("Read pid failed!\n");
  242. return fail;
  243. }
  244. update_msg.ic_fw_msg.type = buf[ADDR_LENGTH];
  245. DEBUG_UPDATE("IC PID:%x\n", update_msg.ic_fw_msg.type);
  246. // guitar_reset(ts, 10);
  247. return success;
  248. }
  249. /*
  250. * Steps of reset guitar
  251. *1. INT脚输出低,延时5ms
  252. *2. RESET脚拉低100ms,转输入悬浮态
  253. *3. I2C寻址GUITAR
  254. *4. 延时100ms读取0xff(3、4轮询80次,直至成功)
  255. *5. Oxff等于0x55则返回成功,否则失败
  256. */
  257. static int guitar_update_mode( struct goodix_ts_data *ts )
  258. {
  259. int ret = 1;
  260. u8 retry;
  261. unsigned char inbuf[3] = {0,0xff,0};
  262. // step 1
  263. guitar_enter_update_mode(ts);
  264. msleep(5);
  265. //step 2
  266. guitar_reset(ts, 100);
  267. for(retry=0;retry < 80; retry++)
  268. {
  269. //step 3
  270. ret =i2c_write_bytes(ts->client, inbuf, 0); //Test I2C connection.
  271. if (ret > 0)
  272. {
  273. DEBUG_UPDATE("<Set update mode>I2C is OK!\n");
  274. //step 4
  275. msleep(100);
  276. ret =i2c_read_bytes(ts->client, inbuf, 3);
  277. if (ret > 0)
  278. {
  279. DEBUG_UPDATE("The value of 0x00ff is 0x%02x\n", inbuf[2]);
  280. //step 5
  281. if(inbuf[2] == 0x55)
  282. {
  283. return success;
  284. }
  285. }
  286. }
  287. msleep(10);
  288. }
  289. DEBUG_UPDATE(KERN_INFO"Detect address %0X\n", ts->client->addr);
  290. return fail;
  291. }
  292. u8 load_update_file(struct goodix_ts_data *ts, st_fw_head* fw_head, u8* data, u8* path)
  293. {
  294. u8 mask_num = 0;
  295. int ret = 0;
  296. int i = 0;
  297. u8 buf[FW_HEAD_LENGTH];
  298. if (path)
  299. {
  300. update_msg.file = filp_open(path, O_RDWR, 0666);
  301. if (IS_ERR(update_msg.file))
  302. {
  303. DEBUG_UPDATE("Open update file(%s) error!\n", path);
  304. return fail;
  305. }
  306. }
  307. else
  308. {
  309. //Begin to search update file
  310. for (i = 0; i < SEARCH_FILE_TIMES; i++)
  311. {
  312. update_msg.file = filp_open(UPDATE_FILE_PATH_1, O_RDWR, 0666);
  313. if (IS_ERR(update_msg.file))
  314. {
  315. update_msg.file = filp_open(UPDATE_FILE_PATH_2, O_RDWR, 0666);//O_RDWR
  316. if (IS_ERR(update_msg.file))
  317. {
  318. DEBUG_UPDATE("%3d:Searching file...\n", i);
  319. msleep(3000);
  320. continue;
  321. }
  322. else
  323. {
  324. break;
  325. }
  326. }
  327. else
  328. {
  329. break;
  330. }
  331. }
  332. if (i >= 100)
  333. {
  334. DEBUG_UPDATE("Can't find update file.\n");
  335. return fail;
  336. }
  337. DEBUG_UPDATE("Find the update file.\n");
  338. }
  339. update_msg.old_fs = get_fs();
  340. set_fs(KERNEL_DS);
  341. update_msg.file->f_pos = IGNORE_LENGTH;
  342. //Make sure the file is the right file.(By compare the "Guitar" flag)
  343. ret = update_msg.file->f_op->read(update_msg.file, (char*)&buf, 6, &update_msg.file->f_pos);
  344. if (ret < 0)
  345. {
  346. DEBUG_UPDATE("Read \"Guitar\" flag error.\n");
  347. goto load_failed;
  348. }
  349. if (false == is_equal(buf, "Guitar", 6))
  350. {
  351. DEBUG_UPDATE("The flag is %s.Not equal!\n"
  352. "The update file is incorrect!\n", buf);
  353. goto load_failed;
  354. }
  355. DEBUG_UPDATE("The file flag is :%s.\n", buf);
  356. //Get the total number of masks
  357. update_msg.file->f_pos++; //ignore one byte.
  358. ret = update_msg.file->f_op->read(update_msg.file, &mask_num, 1, &update_msg.file->f_pos);
  359. if (ret < 0)
  360. {
  361. DEBUG_UPDATE("Didn't get the mask number from the file.\n");
  362. goto load_failed;
  363. }
  364. DEBUG_UPDATE("FILE The total number of masks is:%d.\n", mask_num);
  365. update_msg.file->f_pos = FILE_HEAD_LENGTH + IGNORE_LENGTH;
  366. //Get the correct nvram data
  367. //The correct conditions:
  368. //1. the product id is the same
  369. //2. the mask id is the same
  370. //3. the nvram version in update file is greater than the nvram version in ic
  371. //or force update flag is marked or the check sum in ic is wrong
  372. update_msg.gt_loc = -1;
  373. for ( i = 0; i < mask_num; i++)
  374. {
  375. ret = update_msg.file->f_op->read(update_msg.file, (char*)buf, FW_HEAD_LENGTH, &update_msg.file->f_pos);
  376. if (ret < 0)
  377. {
  378. DEBUG_UPDATE("Read update file head error.\n");
  379. goto load_failed;
  380. }
  381. memcpy(fw_head, buf, sizeof(st_fw_head));
  382. fw_head->version = buf[1] << 8 | buf[2];
  383. fw_head->lenth = buf[9] << 8 | buf[10];
  384. DEBUG_UPDATE("No.%d firmware\n", i);
  385. DEBUG_UPDATE("FILE PID:%x\n", fw_head->type);
  386. DEBUG_UPDATE("FILE VID:0x%x\n", fw_head->version);
  387. DEBUG_UPDATE("FILE mask version:%c%c%c%c.\n", fw_head->msk_ver[0],
  388. fw_head->msk_ver[1],fw_head->msk_ver[2],fw_head->msk_ver[3]);
  389. DEBUG_UPDATE("FILE start address:0x%02x%02x.\n", fw_head->st_addr[0], fw_head->st_addr[1]);
  390. DEBUG_UPDATE("FILE length:%d\n", (int)fw_head->lenth);
  391. DEBUG_UPDATE("FILE force update flag:%s\n", fw_head->force_update);
  392. DEBUG_UPDATE("FILE chksum:0x%02x%02x%02x\n", fw_head->chk_sum[0],
  393. fw_head->chk_sum[1], fw_head->chk_sum[2]);
  394. //First two conditions
  395. if (is_equal(fw_head->msk_ver, update_msg.ic_fw_msg.msk_ver, sizeof(update_msg.ic_fw_msg.msk_ver))
  396. && update_msg.ic_fw_msg.type == fw_head->type)
  397. {
  398. DEBUG_UPDATE("Get the same mask version and same pid.\n");
  399. //The third condition
  400. if (fw_head->version > update_msg.ic_fw_msg.version
  401. || is_equal(fw_head->force_update, "GOODIX", 6)
  402. || update_msg.force_update == 0xAA)
  403. {
  404. // DEBUG_UPDATE("FILE read position:%d\n", file->f_pos);
  405. // file->f_pos = FW_HEAD_LENGTH + FILE_HEAD_LENGTH + IGNORE_LENGTH;
  406. if (is_equal(fw_head->force_update, "GOODIX", 6))
  407. {
  408. update_msg.gt_loc = update_msg.file->f_pos - FW_HEAD_LENGTH + sizeof(st_fw_head) - sizeof(fw_head->force_update);
  409. }
  410. ret = update_msg.file->f_op->read(update_msg.file, (char*)data, fw_head->lenth, &update_msg.file->f_pos);
  411. if (ret <= 0)
  412. {
  413. DEBUG_UPDATE("Read firmware data in file error.\n");
  414. goto load_failed;
  415. }
  416. // DEBUG_ARRAY(data, 512);
  417. // set_fs(ts->old_fs);
  418. // filp_close(ts->file, NULL);
  419. DEBUG_UPDATE("Load data from file successfully.\n");
  420. return success;
  421. }
  422. DEBUG_UPDATE("Don't meet the third condition.\n");
  423. goto load_failed;
  424. }
  425. update_msg.file->f_pos += UPDATE_DATA_LENGTH;
  426. }
  427. load_failed:
  428. set_fs(update_msg.old_fs);
  429. filp_close(update_msg.file, NULL);
  430. return fail;
  431. }
  432. static u8 guitar_nvram_store( struct goodix_ts_data *ts )
  433. {
  434. int ret;
  435. int i;
  436. u8 inbuf[3] = {REG_NVRCS_H,REG_NVRCS_L, 0x18};
  437. ret = i2c_read_bytes(ts->client, inbuf, 3);
  438. if ( ret < 0 )
  439. {
  440. return fail;
  441. }
  442. if ((inbuf[2] & BIT_NVRAM_LOCK ) == BIT_NVRAM_LOCK)
  443. {
  444. return fail;
  445. }
  446. inbuf[2] = 0x18;
  447. inbuf[2] |= (1<<BIT_NVRAM_STROE); //store command
  448. for ( i = 0 ; i < 300 ; i++ )
  449. {
  450. ret = i2c_write_bytes( ts->client, inbuf, 3 );
  451. if ( ret > 0 )
  452. return success;
  453. }
  454. return fail;
  455. }
  456. static u8 guitar_nvram_recall( struct goodix_ts_data *ts )
  457. {
  458. int ret;
  459. u8 inbuf[3] = {REG_NVRCS_H,REG_NVRCS_L,0};
  460. ret = i2c_read_bytes( ts->client, inbuf, 3 );
  461. if ( ret < 0 )
  462. {
  463. return fail;
  464. }
  465. if ( ( inbuf[2]&BIT_NVRAM_LOCK) == BIT_NVRAM_LOCK )
  466. {
  467. return fail;
  468. }
  469. inbuf[2] = ( 1 << BIT_NVRAM_RECALL ); //recall command
  470. ret = i2c_write_bytes( ts->client , inbuf, 3);
  471. if (ret <= 0)
  472. {
  473. return fail;
  474. }
  475. return success;
  476. }
  477. static u8 guitar_update_nvram(struct goodix_ts_data *ts, st_fw_head* fw_head, u8 *nvram)
  478. {
  479. int length = 0;
  480. int ret = 0;
  481. int write_bytes = 0;
  482. int retry = 0;
  483. int i = 0;
  484. int comp = 0;
  485. u16 st_addr = 0;
  486. u8 w_buf[PACK_SIZE + ADDR_LENGTH];
  487. u8 r_buf[PACK_SIZE + ADDR_LENGTH];
  488. if (fw_head->lenth > PACK_SIZE)
  489. {
  490. write_bytes = PACK_SIZE;
  491. }
  492. else
  493. {
  494. write_bytes = fw_head->lenth;
  495. }
  496. clear_mix_flag(ts);
  497. st_addr = (fw_head->st_addr[0] << 8) | (fw_head->st_addr[1]&0xff);
  498. memcpy(&w_buf[2], &nvram[length], write_bytes);
  499. DEBUG_UPDATE("Total length:%d\n", (int)fw_head->lenth);
  500. while(length < fw_head->lenth)
  501. {
  502. w_buf[0] = st_addr >> 8;
  503. w_buf[1] = st_addr & 0xff;
  504. DEBUG_UPDATE("Write address:0x%02x%02x\tlength:%d\n", w_buf[0], w_buf[1], write_bytes);
  505. ret = i2c_write_bytes(ts->client, w_buf, ADDR_LENGTH + write_bytes);
  506. if (ret <= 0)
  507. {
  508. if (retry++ > 10)
  509. {
  510. DEBUG_UPDATE("Write the same address 10 times.Give up!\n");
  511. return fail;
  512. }
  513. DEBUG_UPDATE("Write error![guitar_update_nvram]\n");
  514. continue;
  515. }
  516. else
  517. {
  518. // DEBUG_UPDATE("w_buf:\n");
  519. // DEBUG_ARRAY(w_buf, ADDR_LENGTH + write_bytes);
  520. /* r_buf[0] = 0x14;
  521. r_buf[1] = 0x00;
  522. r_buf[2] = 0x80;
  523. i2c_write_bytes(ts->client, r_buf, 3);
  524. r_buf[0] = 0x14;
  525. r_buf[1] = 0x00;
  526. i2c_read_bytes(ts->client, r_buf, 3);
  527. DEBUG_UPDATE("I2CCS:0x%x\n", r_buf[2]);//*/
  528. r_buf[0] = w_buf[0];
  529. r_buf[1] = w_buf[1];
  530. for (i = 0; i < 10; i++)
  531. {
  532. ret = i2c_read_bytes(ts->client, r_buf, ADDR_LENGTH + write_bytes);
  533. if (ret <= 0)
  534. {
  535. continue;
  536. }
  537. break;
  538. }
  539. if (i >= 10)
  540. {
  541. DEBUG_UPDATE("Read error! Can't check the nvram data.\n");
  542. return fail;
  543. }
  544. // DEBUG_UPDATE("r_buf:\n");
  545. // DEBUG_ARRAY(r_buf, ADDR_LENGTH + write_bytes);
  546. #if 0
  547. if (fail == guitar_nvram_store(ts))
  548. {
  549. DEBUG_UPDATE("Store nvram failed.\n");
  550. //continue;
  551. }
  552. return fail;
  553. #endif
  554. if (false == is_equal(r_buf, w_buf, ADDR_LENGTH + write_bytes))
  555. {
  556. if (comp ++ > 10)
  557. {
  558. DEBUG_UPDATE("Compare error!\n");
  559. return fail;
  560. }
  561. DEBUG_UPDATE("Updating nvram: Not equal!\n");
  562. DEBUG_UPDATE("r_buf:\n");
  563. DEBUG_ARRAY(r_buf, ADDR_LENGTH + write_bytes);
  564. DEBUG_UPDATE("w_buf:\n");
  565. // DEBUG_ARRAY(w_buf, ADDR_LENGTH + write_bytes);
  566. continue;
  567. //return fail;
  568. }
  569. }
  570. comp = 0;
  571. retry = 0;
  572. length += PACK_SIZE;
  573. st_addr += PACK_SIZE;
  574. if ((length + PACK_SIZE) > fw_head->lenth)
  575. {
  576. write_bytes = fw_head->lenth - length;
  577. }
  578. memcpy(&w_buf[2], &nvram[length], write_bytes);
  579. }
  580. return success;
  581. }
  582. static u8 guitar_update_firmware(struct goodix_ts_data *ts, st_fw_head* fw_head, u8 *nvram)
  583. {
  584. int retry;
  585. int ret;
  586. u32 status = 0;
  587. u8 buf[32];
  588. //Cuts the frequency
  589. buf[0] = 0x15;
  590. buf[1] = 0x22;
  591. buf[2] = 0x18;
  592. ret = i2c_write_bytes(ts->client, buf, 3);
  593. if (ret <= 0)
  594. {
  595. return fail;
  596. }
  597. get_ic_msg(ts, 0x1522, buf, 1);
  598. DEBUG_UPDATE("IC OSC_CAL:0x%02x.\n", buf[2]);
  599. for (retry = 0; retry < 10; retry++)
  600. {
  601. //Write the 1st part (pid and vid)
  602. /* if (!(status & 0x01))
  603. {
  604. buf[0] = UPDATE_FW_MSG_ADDR_H;
  605. buf[1] = UPDATE_FW_MSG_ADDR_L;
  606. buf[2] = fw_head->type;
  607. buf[3] = fw_head->version >> 8;
  608. buf[4] = fw_head->version & 0xff;
  609. ret = i2c_write_bytes(ts->client, buf, 5);
  610. if (ret <= 0)
  611. {
  612. continue;
  613. }
  614. else
  615. {
  616. DEBUG_UPDATE("Update pid and vid successfully!\n");
  617. status |= 0x01;
  618. msleep(1);
  619. }
  620. }
  621. */
  622. //Write the 2nd part (nvram)
  623. if (!(status & 0x02))
  624. {
  625. if (fail == guitar_update_nvram(ts, fw_head, nvram))
  626. {
  627. continue;
  628. }
  629. else
  630. {
  631. DEBUG_UPDATE("Update nvram successfully!\n");
  632. status |= 0x02;
  633. msleep(1);
  634. }
  635. }
  636. //Write the 3rd part (check sum)
  637. if (1)
  638. {
  639. buf[0] = 0x4f;
  640. buf[1] = 0xf3;
  641. memcpy(&buf[2], fw_head->chk_sum, sizeof(fw_head->chk_sum));
  642. ret = i2c_write_bytes(ts->client, buf, 5);
  643. if (ret <= 0)
  644. {
  645. continue;
  646. }
  647. else
  648. {
  649. DEBUG_UPDATE("Update check sum successfully!\n");
  650. break;
  651. }
  652. }
  653. }
  654. if (retry >= 10)
  655. {
  656. return fail;
  657. }
  658. else
  659. {
  660. for (retry = 0; retry < 10; retry++)
  661. {
  662. buf[0] = 0x00;
  663. buf[1] = 0xff;
  664. buf[2] = 0x44;
  665. ret = i2c_write_bytes(ts->client, buf, 3);
  666. if (ret > 0)
  667. {
  668. break;
  669. }
  670. }
  671. if (retry >= 10)
  672. {
  673. DEBUG_UPDATE("Write address at 0x00ff error!\n");
  674. return fail;
  675. }
  676. msleep(10);
  677. }
  678. for (retry = 0; retry < 30; retry++)
  679. {
  680. msleep(1);
  681. if (fail == get_ic_msg(ts, 0x00ff, buf, 1))
  682. {
  683. DEBUG_UPDATE("Read address at 0x00ff error!\t retry:%d\n", retry);
  684. continue;
  685. }
  686. if (0xcc == buf[ADDR_LENGTH])
  687. {
  688. return success;
  689. }
  690. else
  691. {
  692. DEBUG_UPDATE("The value of 0x00ff: 0x%02x!\t retry:%d\n", buf[ADDR_LENGTH], retry);
  693. continue;
  694. }
  695. }
  696. DEBUG_UPDATE("The value of 0x00ff error.\n");
  697. return fail;
  698. }
  699. static int guitar_update_proc(void *v_ts)
  700. {
  701. s32 ret;
  702. u32 retry = 100;
  703. u32 i = 0;
  704. struct goodix_ts_data* ts = NULL;
  705. u8* data = NULL;
  706. u8* ic_nvram = NULL;
  707. st_fw_head fw_head;
  708. u8 buf[32];
  709. ts = (struct goodix_ts_data*)v_ts;
  710. data = kzalloc(UPDATE_DATA_LENGTH, GFP_KERNEL);
  711. if (NULL == data)
  712. {
  713. DEBUG_UPDATE("data failed apply for memory.\n");
  714. return fail;
  715. }
  716. ic_nvram = kzalloc(UPDATE_DATA_LENGTH, GFP_KERNEL);
  717. if (NULL == ic_nvram)
  718. {
  719. DEBUG_UPDATE("ic_nvram failed apply for memory.\n");
  720. goto app_mem_failed;
  721. }
  722. DEBUG_UPDATE("Apply for memory successfully.memory size: %d.\n", UPDATE_DATA_LENGTH);
  723. msleep(1000);
  724. DEBUG_UPDATE("Updating...\n");
  725. if (fail == load_update_file(ts, &fw_head, &data[2], NULL))
  726. {
  727. DEBUG_UPDATE("Load file data failed!\n");
  728. goto load_failed;
  729. }
  730. DEBUG_UPDATE("Load file data successfully!\n");
  731. if(!ts->irq_is_disable)
  732. {
  733. disable_irq(ts->client->irq);
  734. }
  735. ts->irq_is_disable = 2;
  736. for (i = 0; i < 5; i++)
  737. {
  738. if (fail == guitar_update_mode(ts))
  739. {
  740. DEBUG_UPDATE("Next try![Enter update mode]\n");
  741. continue;
  742. }
  743. else
  744. {
  745. DEBUG_UPDATE("Set update mode successfully.\n");
  746. break;
  747. }
  748. }
  749. if (i >= 5)
  750. {
  751. DEBUG_UPDATE("Set update mode failed.\n");
  752. return fail;
  753. }
  754. retry = 0;
  755. while(retry++ < 5)
  756. {
  757. if (fail == guitar_update_firmware(ts, &fw_head, &data[2]))
  758. {
  759. DEBUG_UPDATE("Update firmware failed.\n");
  760. continue;
  761. }
  762. DEBUG_UPDATE("Update firmware successfully.\n");
  763. //while(1) // simulation store operation failed
  764. if (fail == guitar_nvram_store(ts))
  765. {
  766. DEBUG_UPDATE("Store nvram failed.\n");
  767. continue;
  768. }
  769. msleep(100);
  770. if (fail == get_ic_msg(ts, 0x1201, buf, 1))
  771. {
  772. DEBUG_UPDATE("Read NVRCS failed.(Store)\n");
  773. continue;
  774. }
  775. if (buf[ADDR_LENGTH] & 0x01)
  776. {
  777. DEBUG_UPDATE("Check NVRCS(0x%02x) failed.(Store)\n", buf[ADDR_LENGTH]);
  778. continue;
  779. }
  780. DEBUG_UPDATE("Store nvram successfully.\n");
  781. if (fail == guitar_nvram_recall(ts))
  782. {
  783. DEBUG_UPDATE("Recall nvram failed.\n");
  784. continue;
  785. }
  786. msleep(5);
  787. if (fail == get_ic_msg(ts, 0x1201, buf, 1))
  788. {
  789. DEBUG_UPDATE("Read NVRCS failed.(Recall)\n");
  790. continue;
  791. }
  792. if (buf[ADDR_LENGTH] & 0x02)
  793. {
  794. DEBUG_UPDATE("Check NVRCS(0x%02x) failed.(Recall)\n", buf[ADDR_LENGTH]);
  795. continue;
  796. }
  797. DEBUG_UPDATE("Recall nvram successfully.\n");
  798. ic_nvram[0] = fw_head.st_addr[0];
  799. ic_nvram[1] = fw_head.st_addr[1];
  800. for ( i = 0; i < 10; i++)
  801. {
  802. ret = i2c_read_bytes(ts->client, ic_nvram, ADDR_LENGTH + fw_head.lenth);
  803. if (ret <= 0)
  804. {
  805. continue;
  806. }
  807. break;
  808. }
  809. if (i >= 10)
  810. {
  811. DEBUG_UPDATE("Read nvram failed!\n");
  812. continue;
  813. }
  814. DEBUG_UPDATE("Read nvram successfully!\n");
  815. if (false == is_equal(&data[2], &ic_nvram[2], fw_head.lenth))
  816. {
  817. DEBUG_UPDATE("Nvram not equal!\n");
  818. continue;
  819. }
  820. DEBUG_UPDATE("Check nvram by byte successfully!\n");
  821. if (update_msg.gt_loc > 0)
  822. {
  823. DEBUG_UPDATE("Location:%d, Ret:%d.\n", (s32)update_msg.gt_loc, (s32)ret);
  824. memset(buf, 0, sizeof(buf));
  825. ret = update_msg.file->f_op->write(update_msg.file, buf, 6, &update_msg.gt_loc);
  826. if (ret < 0)
  827. {
  828. DEBUG_UPDATE("Didn't clear the focre update flag in file.\n");
  829. }
  830. else
  831. {
  832. DEBUG_UPDATE("Clear the focre update flag in file.Location:%d, Ret:%d.\n", (s32)update_msg.gt_loc, (s32)ret);
  833. }
  834. }
  835. DEBUG_UPDATE("Update successfully!\n");
  836. break;
  837. }
  838. set_fs(update_msg.old_fs);
  839. filp_close(update_msg.file, NULL);
  840. guitar_leave_update_mode(ts);
  841. DEBUG_UPDATE("Leave update mode!\n");
  842. //Reset guitar
  843. DEBUG_UPDATE("Reset IC and send config!\n");
  844. guitar_reset(ts, 10);
  845. for (i = 0; i < 3; i++)
  846. {
  847. if (fail == goodix_init_panel(ts, 1))
  848. {
  849. msleep(10);
  850. continue;
  851. }
  852. break;
  853. }
  854. if (i >= 3)
  855. {
  856. DEBUG_UPDATE("Send config data failed.\n");
  857. }
  858. msleep(10);
  859. ts->irq_is_disable = 0;
  860. enable_irq(ts->client->irq);
  861. load_failed:
  862. kfree(ic_nvram);
  863. app_mem_failed:
  864. kfree(data);
  865. if (retry < 5)
  866. {
  867. return success;
  868. }
  869. DEBUG_UPDATE("Update failed!\n");
  870. return fail;
  871. }
  872. s32 init_update_proc(struct goodix_ts_data *ts)
  873. {
  874. u8 flag = 0;
  875. struct task_struct *thread = NULL;
  876. s32 retry = 0;
  877. DEBUG_MSG("Ready to run update thread.\n");
  878. update_msg.fw_flag = get_ic_fw_msg(ts);
  879. if (fail == update_msg.fw_flag)
  880. {
  881. DEBUG_UPDATE("Try get ic msg in update mode.\n");
  882. for (retry = 0; retry < 5; retry++)
  883. {
  884. if (success == guitar_update_mode(ts))
  885. {
  886. break;
  887. }
  888. }
  889. if (retry >= 5)
  890. {
  891. update_msg.fw_flag = fail;
  892. }
  893. else
  894. {
  895. DEBUG_UPDATE("Get ic msg in update mode.\n");
  896. update_msg.fw_flag = get_ic_fw_msg(ts);
  897. update_msg.ic_fw_msg.version = 0xfff0;
  898. if (update_msg.force_update == 0xAA)
  899. {
  900. flag = 0xff;
  901. }
  902. }
  903. guitar_leave_update_mode(ts);
  904. }
  905. else
  906. {
  907. guitar_reset(ts, 10);
  908. }
  909. if (success == update_msg.fw_flag)
  910. {
  911. update_msg.gt_loc = -1;
  912. thread = kthread_run(guitar_update_proc, (void*)ts, "guitar_update");
  913. if (IS_ERR(thread))
  914. {
  915. dev_err(&ts->client->dev, " failed to create update thread\n");
  916. }
  917. if (0xff == flag)
  918. {
  919. return 0xff;
  920. }
  921. }
  922. return success;
  923. }
  924. //#endif //endif AUTO_UPDATE_GUITAR
  925. //******************************End of firmware update surpport*******************************