focaltech_5x06.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. /*
  2. * drivers/input/touchscreen/ft5x0x_ts.c
  3. *
  4. * FocalTech ft5x0x TouchScreen driver.
  5. *
  6. * Copyright (c) 2010 Focal tech Ltd.
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * VERSION DATE AUTHOR
  18. * 1.0 2010-01-05 WenFS
  19. *
  20. * note: only support mulititouch Wenfs 2010-10-01
  21. */
  22. #include <linux/i2c.h>
  23. #include <linux/delay.h>
  24. #include <linux/input.h>
  25. #include <linux/earlysuspend.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/slab.h>
  28. #include <linux/device.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/workqueue.h>
  31. #include <mach/gpio.h>
  32. #include <mach/gpio_data.h>
  33. #include <linux/ft5x06_ts.h>
  34. static int ft5x0x_write_reg(u8 addr, u8 para);
  35. static int ft5x0x_read_reg(u8 addr, u8 *pdata);
  36. unsigned char fts_ctpm_get_upg_ver(void);
  37. static unsigned char ft5x0x_read_fw_info(const char cmd);
  38. static struct i2c_client *this_client;
  39. static struct ts_platform_data *focaltechPdata2;
  40. static int ft5x0x_printk_enable_flag=0;
  41. typedef enum
  42. {
  43. ERR_OK,
  44. ERR_MODE,
  45. ERR_READID,
  46. ERR_ERASE,
  47. ERR_STATUS,
  48. ERR_ECC,
  49. ERR_DL_ERASE_FAIL,
  50. ERR_DL_PROGRAM_FAIL,
  51. ERR_DL_VERIFY_FAIL,
  52. ERR_REV_FILE_FAIL,
  53. ERR_IN_UPGRADE,
  54. ERR_INIT_STATE
  55. }E_UPGRADE_ERR_TYPE;
  56. static char upgrade_result = ERR_INIT_STATE;
  57. typedef unsigned char FTS_BYTE; //8 bit
  58. typedef unsigned short FTS_WORD; //16 bit
  59. typedef unsigned int FTS_DWRD; //16 bit
  60. typedef unsigned char FTS_BOOL; //8 bit
  61. #define FTS_NULL 0x0
  62. #define FTS_TRUE 0x01
  63. #define FTS_FALSE 0x0
  64. #define I2C_CTPM_ADDRESS 0x70
  65. E_UPGRADE_ERR_TYPE fts_ctpm_fw_upgrade(FTS_BYTE* pbt_buf, FTS_DWRD dw_lenth);
  66. #define FT5X0X_EVENT_MAX 5
  67. //#define CONFIG_TOUCH_PANEL_KEY
  68. #ifdef CONFIG_TOUCH_PANEL_KEY
  69. #define TOUCH_SCREEN_RELEASE_DELAY (100 * 1000000)//unit usec
  70. #define TAP_KEY_RELEASE_DELAY (100 * 1000000)
  71. #define TAP_KEY_TIME 10
  72. enum {
  73. NO_TOUCH,
  74. TOUCH_KEY_PRE,
  75. TAP_KEY,
  76. TOUCH_KEY,
  77. TOUCH_SCREEN,
  78. TOUCH_SCREEN_RELEASE,
  79. };
  80. #endif
  81. struct ts_event {
  82. u8 id;
  83. s16 x;
  84. s16 y;
  85. s16 z;
  86. s16 w;
  87. };
  88. struct ft5x0x_ts_data {
  89. struct input_dev *input_dev;
  90. struct ts_event event[FT5X0X_EVENT_MAX];
  91. u8 event_num;
  92. struct work_struct pen_event_work;
  93. struct workqueue_struct *ts_workqueue;
  94. struct early_suspend early_suspend;
  95. #ifdef CONFIG_TOUCH_PANEL_KEY
  96. u8 touch_state;
  97. short key;
  98. struct hrtimer timer;
  99. struct ts_event first_event;
  100. int offset;
  101. int touch_count;
  102. #endif
  103. };
  104. #define ft5x0x_dbg(fmt, args...) { if(ft5x0x_printk_enable_flag) \
  105. printk("[ft5x0x]: " fmt, ## args); }
  106. //#define AC_DETECT_IN_TOUCH_DRIVER
  107. #ifdef AC_DETECT_IN_TOUCH_DRIVER
  108. static int ac_detect_flag_current=0;
  109. static int ac_detect_flag_old=3;
  110. static void Ac_Detect_In_Touch_Driver(void)
  111. {
  112. unsigned char ver;
  113. int ac_detect_flag_temp=0;
  114. if (!focaltechPdata2->Ac_is_connect) return;
  115. ac_detect_flag_temp=focaltechPdata2->Ac_is_connect();
  116. ac_detect_flag_current=ac_detect_flag_temp;
  117. if(ac_detect_flag_current!=ac_detect_flag_old)
  118. {
  119. if(1==focaltechPdata2->Ac_is_connect())
  120. ft5x0x_write_reg(0xb2,0x1);
  121. else
  122. ft5x0x_write_reg(0xb2,0x0);
  123. ac_detect_flag_old=ac_detect_flag_current;
  124. }
  125. if(1==ft5x0x_printk_enable_flag)
  126. {
  127. ft5x0x_read_reg(0xb2, &ver);
  128. printk("reg 0xb2=%d\n",ver);
  129. }
  130. }
  131. #endif
  132. static ssize_t ft5x0x_read(struct device *dev, struct device_attribute *attr, char *buf)
  133. {
  134. char *tmp_buf = NULL, *pre_tmp = NULL;
  135. int i = 0,count = 0;
  136. if (!strcmp(attr->attr.name, "ft5x0xPrintFlag")) {
  137. memcpy(buf, &ft5x0x_printk_enable_flag,sizeof(ft5x0x_printk_enable_flag));
  138. printk("buf[0]=%d, buf[1]=%d\n", buf[0], buf[1]);
  139. return sizeof(ft5x0x_printk_enable_flag);
  140. }
  141. else if (!strcmp(attr->attr.name, "effect")) {
  142. if (pre_tmp == NULL) pre_tmp = (FTS_BYTE *)kmalloc(20*sizeof(FTS_BYTE), GFP_KERNEL);
  143. if (pre_tmp == NULL) {
  144. printk("Insufficient memory in upgrade!\n");
  145. return -ERR_REV_FILE_FAIL;
  146. }
  147. tmp_buf = pre_tmp;
  148. *tmp_buf++ = ft5x0x_read_fw_info(FT5X0X_REG_PERIODACTIVE);
  149. *tmp_buf++ = ft5x0x_read_fw_info(FT5X0X_REG_FIRMID);
  150. *tmp_buf++ = ft5x0x_read_fw_info(FT5X0X_REG_CIPHER);
  151. *tmp_buf++ = ft5x0x_read_fw_info(FT5X0X_REG_FT5201ID);
  152. count = tmp_buf-pre_tmp;
  153. for (i=0; i<count; i++) {
  154. tmp_buf--;
  155. snprintf(buf++,sizeof(FTS_BYTE)+1,"%x",*tmp_buf/16);
  156. snprintf(buf++,sizeof(FTS_BYTE)+1,"%x",*tmp_buf%16);
  157. }
  158. if (pre_tmp != NULL) {
  159. kfree(pre_tmp);
  160. pre_tmp = NULL;
  161. }
  162. return count*2;
  163. }
  164. else if (!strcmp(attr->attr.name, "upgrade")) {
  165. *buf = upgrade_result+'0';
  166. printk("upgrade_result = %d.\n", upgrade_result);
  167. return sizeof(upgrade_result);
  168. }
  169. return 0;
  170. }
  171. static FTS_BYTE char_to_hex(const char prec, const char nextc)
  172. {
  173. FTS_BYTE sum = 0;
  174. if ((prec>='0') && (prec<='9')) sum = prec - '0';
  175. else if ((prec>='a') && (prec<='f')) sum = prec - 'a' + 10;
  176. if ((nextc>='0') && (nextc<='9')) sum = sum*16 + nextc - '0';
  177. else if ((nextc>='a') && (nextc<='f')) sum = sum*16 + nextc - 'a' + 10;
  178. return sum;
  179. }
  180. static ssize_t ft5x0x_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  181. {
  182. int ret = 0, i = 0, send_len = 0;
  183. static size_t length;
  184. E_UPGRADE_ERR_TYPE err = ERR_REV_FILE_FAIL;
  185. static FTS_BYTE *to_buf = NULL, *pre_to_buf = NULL, *free_to_buf = NULL;
  186. if (!strcmp(attr->attr.name, "ft5x0xPrintFlag")) {
  187. printk("buf[0]=%d, buf[1]=%d\n", buf[0], buf[1]);
  188. if (buf[0] == '0') ft5x0x_printk_enable_flag = 0;
  189. if (buf[0] == '1') ft5x0x_printk_enable_flag = 1;
  190. if (buf[0] == '2') {
  191. ft5x0x_printk_enable_flag=2;
  192. if(focaltechPdata2->reset){
  193. focaltechPdata2->reset(0);
  194. msleep(15);
  195. }
  196. if(focaltechPdata2->power){
  197. focaltechPdata2->power(0);
  198. msleep(50);
  199. focaltechPdata2->power(1);
  200. msleep(200);
  201. }
  202. }
  203. if (buf[0] == '3') {
  204. u8 data;
  205. ft5x0x_write_reg(0xa5, 0x03);
  206. printk("set reg[0xa5] = 0x03\n");
  207. msleep(20);
  208. ft5x0x_read_reg(0xa5, &data);
  209. printk("read back: reg[0xa5] = %d\n", data);
  210. }
  211. }
  212. else if (!strcmp(attr->attr.name, "effect")) {
  213. printk("buf[0]=%d, buf[1]=%d\n", buf[0], buf[1]);
  214. }
  215. else if (!strcmp(attr->attr.name, "upgrade")) {
  216. if (length == 0) {
  217. if (free_to_buf == NULL) free_to_buf = (FTS_BYTE *)kmalloc((128*1024-20)*sizeof(FTS_BYTE), GFP_KERNEL);
  218. printk("start free_to_buf = 0x%x.\n", free_to_buf);
  219. if (free_to_buf == NULL) {
  220. printk("Insufficient memory in upgrade!\n");
  221. length = 0;
  222. upgrade_result = ERR_REV_FILE_FAIL;
  223. return -ERR_REV_FILE_FAIL;
  224. }
  225. pre_to_buf = free_to_buf;
  226. to_buf = pre_to_buf;
  227. upgrade_result = ERR_IN_UPGRADE;
  228. }
  229. memcpy(to_buf, buf, count);
  230. to_buf += count;
  231. length += count;
  232. // printk("upgrade data count = %d.\n", count);
  233. // printk("upgrade data length = %d.\n", length);
  234. if (length > (128*1024-25)) {
  235. upgrade_result = ERR_REV_FILE_FAIL;
  236. printk("upgrade_result = %d.\n", ERR_REV_FILE_FAIL);
  237. printk("end free_to_buf = 0x%x.\n", free_to_buf);
  238. length = 0;
  239. if (free_to_buf != NULL) {
  240. kfree(free_to_buf);
  241. free_to_buf = NULL;
  242. }
  243. return ERR_REV_FILE_FAIL;
  244. }
  245. if ((to_buf != NULL) && (*(to_buf-1)=='d') && (*(to_buf-2)=='n') && (*(to_buf-3)=='e')) {
  246. for (i=0; i<length-3;) {
  247. if ((*pre_to_buf=='0') && (*(pre_to_buf+1)=='x')) {
  248. if ((*(pre_to_buf+3)==',') && (*(pre_to_buf+4)==' ')) free_to_buf[send_len++] = char_to_hex('0', *(pre_to_buf+2));
  249. else free_to_buf[send_len++] = char_to_hex(*(pre_to_buf+2), *(pre_to_buf+3));
  250. pre_to_buf += 5;
  251. i += 5;
  252. }
  253. else {
  254. pre_to_buf++;
  255. i++;
  256. }
  257. }
  258. if (send_len < 100)
  259. return ERR_REV_FILE_FAIL;
  260. for (i=0; i<10; i++) {
  261. printk("free_to_buf[%d] = 0x%x.\n", i, free_to_buf[i]);
  262. }
  263. for (i=send_len-1; i>=send_len-10; i--) {
  264. printk("free_to_buf[%d] = 0x%x.\n", i, free_to_buf[i]);
  265. }
  266. for (i=0; i<5; i++) {
  267. err = fts_ctpm_fw_upgrade(free_to_buf, send_len);
  268. if (err == 0) break;
  269. }
  270. upgrade_result = err;
  271. printk("upgrade status is %d.\n", err);
  272. printk("upgrade data send_len = %d.\n", send_len);
  273. printk("end free_to_buf = 0x%x.\n", free_to_buf);
  274. if (free_to_buf != NULL) {
  275. kfree(free_to_buf);
  276. free_to_buf = NULL;
  277. }
  278. length = 0;
  279. }
  280. }
  281. return count;
  282. }
  283. static DEVICE_ATTR(ft5x0xPrintFlag, S_IRWXUGO, ft5x0x_read, ft5x0x_write);
  284. static DEVICE_ATTR(effect, S_IRUGO|S_IWUGO, ft5x0x_read, ft5x0x_write);
  285. static DEVICE_ATTR(upgrade, S_IRUGO|S_IWUGO, ft5x0x_read, ft5x0x_write);
  286. static struct attribute *ft5x0x_attr[] = {
  287. &dev_attr_ft5x0xPrintFlag.attr,
  288. &dev_attr_effect.attr,
  289. &dev_attr_upgrade.attr,
  290. NULL
  291. };
  292. static struct attribute_group ft5x0x_attr_group = {
  293. .name = NULL,
  294. .attrs = ft5x0x_attr,
  295. };
  296. /***********************************************************************************************
  297. Name : ft5x0x_i2c_rxdata
  298. Input : *rxdata
  299. *length
  300. Output : ret
  301. function :
  302. ***********************************************************************************************/
  303. static int ft5x0x_i2c_rxdata(char *rxdata, int length)
  304. {
  305. int ret;
  306. struct i2c_msg msgs[] = {
  307. {
  308. .addr = this_client->addr,
  309. .flags = 0,
  310. .len = 1,
  311. .buf = rxdata,
  312. },
  313. {
  314. .addr = this_client->addr,
  315. .flags = I2C_M_RD,
  316. .len = length,
  317. .buf = rxdata,
  318. },
  319. };
  320. //msleep(1);
  321. ret = i2c_transfer(this_client->adapter, msgs, 2);
  322. if (ret < 0)
  323. pr_err("msg %s i2c read error: %d\n", __func__, ret);
  324. return ret;
  325. }
  326. /***********************************************************************************************
  327. Name :
  328. Input :
  329. Output :
  330. function :
  331. ***********************************************************************************************/
  332. static int ft5x0x_i2c_txdata(char *txdata, int length)
  333. {
  334. int ret;
  335. struct i2c_msg msg[] = {
  336. {
  337. .addr = this_client->addr,
  338. .flags = 0,
  339. .len = length,
  340. .buf = txdata,
  341. },
  342. };
  343. //msleep(1);
  344. ret = i2c_transfer(this_client->adapter, msg, 1);
  345. if (ret < 0)
  346. pr_err("%s i2c write error: %d\n", __func__, ret);
  347. return ret;
  348. }
  349. /***********************************************************************************************
  350. Name : ft5x0x_write_reg
  351. Input : addr -- address
  352. para -- parameter
  353. Output :
  354. function : write register of ft5x0x
  355. ***********************************************************************************************/
  356. static int ft5x0x_write_reg(u8 addr, u8 para)
  357. {
  358. u8 buf[3];
  359. int ret = -1;
  360. buf[0] = addr;
  361. buf[1] = para;
  362. ret = ft5x0x_i2c_txdata(buf, 2);
  363. if (ret < 0) {
  364. pr_err("write reg failed! %#x ret: %d", buf[0], ret);
  365. return -1;
  366. }
  367. return 0;
  368. }
  369. /***********************************************************************************************
  370. Name : ft5x0x_read_reg
  371. Input : addr
  372. pdata
  373. Output :
  374. function : read register of ft5x0x
  375. ***********************************************************************************************/
  376. static int ft5x0x_read_reg(u8 addr, u8 *pdata)
  377. {
  378. int ret;
  379. u8 buf[2] = {0};
  380. buf[0] = addr;
  381. struct i2c_msg msgs[] = {
  382. {
  383. .addr = this_client->addr,
  384. .flags = 0,
  385. .len = 1,
  386. .buf = buf,
  387. },
  388. {
  389. .addr = this_client->addr,
  390. .flags = I2C_M_RD,
  391. .len = 1,
  392. .buf = buf,
  393. },
  394. };
  395. //msleep(1);
  396. ret = i2c_transfer(this_client->adapter, msgs, 2);
  397. if (ret < 0)
  398. pr_err("msg %s i2c read error: %d\n", __func__, ret);
  399. *pdata = buf[0];
  400. return ret;
  401. }
  402. /***********************************************************************************************
  403. Name : ft5x0x_read_fw_ver
  404. Input : void
  405. Output : firmware version
  406. function : read TP firmware version
  407. ***********************************************************************************************/
  408. static unsigned char ft5x0x_read_fw_ver(void)
  409. {
  410. unsigned char ver;
  411. ft5x0x_read_reg(FT5X0X_REG_FIRMID, &ver);
  412. return(ver);
  413. }
  414. static unsigned char ft5x0x_read_fw_info(const char cmd)
  415. {
  416. unsigned char ver;
  417. ft5x0x_read_reg(cmd, &ver);
  418. return(ver);
  419. }
  420. void delay_qt_ms(unsigned long w_ms)
  421. {
  422. unsigned long i;
  423. unsigned long j;
  424. for (i = 0; i < w_ms; i++)
  425. {
  426. for (j = 0; j < 1000; j++)
  427. {
  428. udelay(1);
  429. }
  430. }
  431. }
  432. /*
  433. [function]:
  434. callback: read data from ctpm by i2c interface,implemented by special user;
  435. [parameters]:
  436. bt_ctpm_addr[in] :the address of the ctpm;
  437. pbt_buf[out] :data buffer;
  438. dw_lenth[in] :the length of the data buffer;
  439. [return]:
  440. FTS_TRUE :success;
  441. FTS_FALSE :fail;
  442. */
  443. FTS_BOOL i2c_read_interface(FTS_BYTE bt_ctpm_addr, FTS_BYTE* pbt_buf, FTS_DWRD dw_lenth)
  444. {
  445. int ret;
  446. ret=i2c_master_recv(this_client, pbt_buf, dw_lenth);
  447. if(ret<=0)
  448. {
  449. printk("[TSP]i2c_read_interface error\n");
  450. return FTS_FALSE;
  451. }
  452. return FTS_TRUE;
  453. }
  454. /*
  455. [function]:
  456. callback: write data to ctpm by i2c interface,implemented by special user;
  457. [parameters]:
  458. bt_ctpm_addr[in] :the address of the ctpm;
  459. pbt_buf[in] :data buffer;
  460. dw_lenth[in] :the length of the data buffer;
  461. [return]:
  462. FTS_TRUE :success;
  463. FTS_FALSE :fail;
  464. */
  465. FTS_BOOL i2c_write_interface(FTS_BYTE bt_ctpm_addr, FTS_BYTE* pbt_buf, FTS_DWRD dw_lenth)
  466. {
  467. int ret;
  468. ret=i2c_master_send(this_client, pbt_buf, dw_lenth);
  469. if(ret<=0)
  470. {
  471. printk("[TSP]i2c_write_interface error line = %d, ret = %d\n", __LINE__, ret);
  472. return FTS_FALSE;
  473. }
  474. return FTS_TRUE;
  475. }
  476. /*
  477. [function]:
  478. send a command to ctpm.
  479. [parameters]:
  480. btcmd[in] :command code;
  481. btPara1[in] :parameter 1;
  482. btPara2[in] :parameter 2;
  483. btPara3[in] :parameter 3;
  484. num[in] :the valid input parameter numbers, if only command code needed and no parameters followed,then the num is 1;
  485. [return]:
  486. FTS_TRUE :success;
  487. FTS_FALSE :io fail;
  488. */
  489. FTS_BOOL cmd_write(FTS_BYTE btcmd,FTS_BYTE btPara1,FTS_BYTE btPara2,FTS_BYTE btPara3,FTS_BYTE num)
  490. {
  491. FTS_BYTE write_cmd[4] = {0};
  492. write_cmd[0] = btcmd;
  493. write_cmd[1] = btPara1;
  494. write_cmd[2] = btPara2;
  495. write_cmd[3] = btPara3;
  496. return i2c_write_interface(I2C_CTPM_ADDRESS, write_cmd, num);
  497. }
  498. /*
  499. [function]:
  500. write data to ctpm , the destination address is 0.
  501. [parameters]:
  502. pbt_buf[in] :point to data buffer;
  503. bt_len[in] :the data numbers;
  504. [return]:
  505. FTS_TRUE :success;
  506. FTS_FALSE :io fail;
  507. */
  508. FTS_BOOL byte_write(FTS_BYTE* pbt_buf, FTS_DWRD dw_len)
  509. {
  510. return i2c_write_interface(I2C_CTPM_ADDRESS, pbt_buf, dw_len);
  511. }
  512. /*
  513. [function]:
  514. read out data from ctpm,the destination address is 0.
  515. [parameters]:
  516. pbt_buf[out] :point to data buffer;
  517. bt_len[in] :the data numbers;
  518. [return]:
  519. FTS_TRUE :success;
  520. FTS_FALSE :io fail;
  521. */
  522. FTS_BOOL byte_read(FTS_BYTE* pbt_buf, FTS_BYTE bt_len)
  523. {
  524. return i2c_read_interface(I2C_CTPM_ADDRESS, pbt_buf, bt_len);
  525. }
  526. /*
  527. [function]:
  528. burn the FW to ctpm.
  529. [parameters]:(ref. SPEC)
  530. pbt_buf[in] :point to Head+FW ;
  531. dw_lenth[in]:the length of the FW + 6(the Head length);
  532. bt_ecc[in] :the ECC of the FW
  533. [return]:
  534. ERR_OK :no error;
  535. ERR_MODE :fail to switch to UPDATE mode;
  536. ERR_READID :read id fail;
  537. ERR_ERASE :erase chip fail;
  538. ERR_STATUS :status error;
  539. ERR_ECC :ecc error.
  540. */
  541. #define FTS_PACKET_LENGTH 128
  542. int fts_ctpm_auto_clb(void)
  543. {
  544. unsigned char uc_temp;
  545. unsigned char i ;
  546. printk("[FTS] start auto CLB.\n");
  547. msleep(200);
  548. ft5x0x_write_reg(0, 0x40);
  549. delay_qt_ms(100); //make sure already enter factory mode
  550. ft5x0x_write_reg(2, 0x4); //write command to start calibration
  551. delay_qt_ms(300);
  552. for(i=0;i<100;i++)
  553. {
  554. ft5x0x_read_reg(0,&uc_temp);
  555. if ( ((uc_temp&0x70)>>4) == 0x0) //return to normal mode, calibration finish
  556. {
  557. break;
  558. }
  559. delay_qt_ms(200);
  560. printk("[FTS] waiting calibration %d\n",i);
  561. }
  562. printk("[FTS] calibration OK.\n");
  563. msleep(300);
  564. ft5x0x_write_reg(0, 0x40); //goto factory mode
  565. delay_qt_ms(100); //make sure already enter factory mode
  566. ft5x0x_write_reg(2, 0x5); //store CLB result
  567. delay_qt_ms(300);
  568. ft5x0x_write_reg(0, 0x0); //return to normal mode
  569. msleep(300);
  570. printk("[FTS] store CLB result OK.\n");
  571. return 0;
  572. }
  573. E_UPGRADE_ERR_TYPE fts_ctpm_fw_upgrade(FTS_BYTE* pbt_buf, FTS_DWRD dw_lenth)
  574. {
  575. FTS_BYTE reg_val[2] = {0}, read_id = 0;
  576. FTS_DWRD i = 0;
  577. FTS_DWRD packet_number;
  578. FTS_DWRD j;
  579. FTS_DWRD temp;
  580. FTS_DWRD lenght;
  581. FTS_BYTE packet_buf[FTS_PACKET_LENGTH + 6];
  582. FTS_BYTE auc_i2c_write_buf[10];
  583. FTS_BYTE bt_ecc;
  584. int i_ret;
  585. if (focaltechPdata2->chip_id == 0) {
  586. focaltechPdata2->step12_delay = 30;
  587. focaltechPdata2->step23_delay = 0;
  588. read_id = 0x3;
  589. }
  590. else {
  591. focaltechPdata2->step12_delay = 10;
  592. focaltechPdata2->step23_delay = 100;
  593. read_id = focaltechPdata2->chip_id;
  594. }
  595. /*********Step 1:Reset CTPM *****/
  596. /*write 0xaa to register 0xfc*/
  597. ft5x0x_write_reg(0xfc,0xaa);
  598. delay_qt_ms(50);
  599. /*write 0x55 to register 0xfc*/
  600. ft5x0x_write_reg(0xfc,0x55);
  601. printk("[TSP] Step 1: Reset CTPM test\n");
  602. delay_qt_ms(focaltechPdata2->step12_delay);
  603. /*********Step 2:Enter upgrade mode *****/
  604. auc_i2c_write_buf[0] = 0x55;
  605. auc_i2c_write_buf[1] = 0xaa;
  606. do
  607. {
  608. i ++;
  609. i_ret = ft5x0x_i2c_txdata(auc_i2c_write_buf, 2);
  610. delay_qt_ms(5);
  611. }while(i_ret <= 0 && i < 5 );
  612. delay_qt_ms(focaltechPdata2->step23_delay);
  613. /*********Step 3:check READ-ID***********************/
  614. // delay_qt_ms(5);
  615. cmd_write(0x90,0x00,0x00,0x00,4);
  616. byte_read(reg_val,2);
  617. if (reg_val[0] == 0x79 && reg_val[1] == read_id)
  618. {
  619. printk("[TSP] Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",reg_val[0],reg_val[1]);
  620. }
  621. else
  622. {
  623. for(i=0;i<10;i++)
  624. {
  625. byte_read(reg_val,2);
  626. if (reg_val[0] == 0x79 && reg_val[1] == read_id)
  627. {
  628. printk("[TSP] Step 3: succes = 0x%x",i);
  629. break;
  630. }
  631. else
  632. {
  633. printk("[TSP] Step 3: error = 0x%x",i);
  634. }
  635. }
  636. if(i==10)
  637. {
  638. printk("[TSP] Step 3: return error = 0x%x",i);
  639. return ERR_READID;
  640. }
  641. //i_is_new_protocol = 1;
  642. }
  643. cmd_write(0xcd,0x0,0x00,0x00,1);
  644. byte_read(reg_val,1);
  645. printk("[FTS] bootloader version = 0x%x\n", reg_val[0]);
  646. cmd_write(0x61,0x00,0x00,0x00,1);
  647. delay_qt_ms(1500);
  648. cmd_write(0x63,0x00,0x00,0x00,1); //erase panel parameter area
  649. delay_qt_ms(100);
  650. printk("[TSP] Step 4: erase. \n");
  651. /*********Step 5:write firmware(FW) to ctpm flash*********/
  652. bt_ecc = 0;
  653. printk("[TSP] Step 5: start upgrade. \n");
  654. dw_lenth = dw_lenth - 8;
  655. packet_number = (dw_lenth) / FTS_PACKET_LENGTH;
  656. packet_buf[0] = 0xbf;
  657. packet_buf[1] = 0x00;
  658. for (j=0;j<packet_number;j++)
  659. {
  660. temp = j * FTS_PACKET_LENGTH;
  661. packet_buf[2] = (FTS_BYTE)(temp>>8);
  662. packet_buf[3] = (FTS_BYTE)temp;
  663. lenght = FTS_PACKET_LENGTH;
  664. packet_buf[4] = (FTS_BYTE)(lenght>>8);
  665. packet_buf[5] = (FTS_BYTE)lenght;
  666. for (i=0;i<FTS_PACKET_LENGTH;i++)
  667. {
  668. packet_buf[6+i] = pbt_buf[j*FTS_PACKET_LENGTH + i];
  669. bt_ecc ^= packet_buf[6+i];
  670. }
  671. byte_write(&packet_buf[0],FTS_PACKET_LENGTH + 6);
  672. delay_qt_ms(FTS_PACKET_LENGTH/6 + 1);
  673. if ((j * FTS_PACKET_LENGTH % 1024) == 0)
  674. {
  675. printk("[TSP] upgrade the 0x%x th byte.\n", ((unsigned int)j) * FTS_PACKET_LENGTH);
  676. }
  677. }
  678. if ((dw_lenth) % FTS_PACKET_LENGTH > 0)
  679. {
  680. temp = packet_number * FTS_PACKET_LENGTH;
  681. packet_buf[2] = (FTS_BYTE)(temp>>8);
  682. packet_buf[3] = (FTS_BYTE)temp;
  683. temp = (dw_lenth) % FTS_PACKET_LENGTH;
  684. packet_buf[4] = (FTS_BYTE)(temp>>8);
  685. packet_buf[5] = (FTS_BYTE)temp;
  686. for (i=0;i<temp;i++)
  687. {
  688. packet_buf[6+i] = pbt_buf[ packet_number*FTS_PACKET_LENGTH + i];
  689. bt_ecc ^= packet_buf[6+i];
  690. }
  691. byte_write(&packet_buf[0],temp+6);
  692. delay_qt_ms(20);
  693. }
  694. //send the last six byte
  695. for (i = 0; i<6; i++)
  696. {
  697. temp = 0x6ffa + i;
  698. packet_buf[2] = (FTS_BYTE)(temp>>8);
  699. packet_buf[3] = (FTS_BYTE)temp;
  700. temp =1;
  701. packet_buf[4] = (FTS_BYTE)(temp>>8);
  702. packet_buf[5] = (FTS_BYTE)temp;
  703. packet_buf[6] = pbt_buf[ dw_lenth + i];
  704. bt_ecc ^= packet_buf[6];
  705. byte_write(&packet_buf[0],7);
  706. delay_qt_ms(20);
  707. }
  708. /*********Step 6: read out checksum***********************/
  709. /*send the opration head*/
  710. cmd_write(0xcc,0x00,0x00,0x00,1);
  711. byte_read(reg_val,1);
  712. printk("[TSP] Step 6: ecc read 0x%x, new firmware 0x%x. \n", reg_val[0], bt_ecc);
  713. if(reg_val[0] != bt_ecc)
  714. {
  715. printk("[TSP] Step 6: ecc error and return \n");
  716. return ERR_ECC;
  717. }
  718. /*********Step 7: reset the new FW***********************/
  719. cmd_write(0x07,0x00,0x00,0x00,1);
  720. printk("[TSP] Step 7 \n");
  721. delay_qt_ms(300);
  722. fts_ctpm_auto_clb();
  723. return ERR_OK;
  724. }
  725. #ifdef CONFIG_FOCALTECH_TOUCHSCREEN_CODE_UPG
  726. static unsigned char CTPM_FW[]=
  727. {
  728. #include "ft_app.ft"
  729. };
  730. unsigned char fts_ctpm_get_upg_ver(void)
  731. {
  732. unsigned int ui_sz;
  733. ui_sz = sizeof(CTPM_FW);
  734. if (ui_sz > 2)
  735. {
  736. return CTPM_FW[ui_sz - 2];
  737. }
  738. else
  739. {
  740. //TBD, error handling?
  741. return 0xff; //default value
  742. }
  743. }
  744. int fts_ctpm_fw_upgrade_with_i_file(void)
  745. {
  746. FTS_BYTE* pbt_buf = FTS_NULL;
  747. int i_ret;
  748. unsigned char uc_host_fm_ver;
  749. unsigned char uc_tp_fm_ver;
  750. //=========FW upgrade========================*/
  751. pbt_buf = CTPM_FW;
  752. /*call the upgrade function*/
  753. uc_tp_fm_ver = ft5x0x_read_fw_ver();
  754. uc_host_fm_ver = fts_ctpm_get_upg_ver();
  755. if ( uc_tp_fm_ver == 0xa6 || //the firmware in touch panel maybe corrupted
  756. uc_tp_fm_ver < uc_host_fm_ver //the firmware in host flash is new, need upgrade
  757. )
  758. { printk("[FTS] upgrade start.\n");
  759. i_ret = fts_ctpm_fw_upgrade(pbt_buf,sizeof(CTPM_FW));
  760. }
  761. else{
  762. printk("[FTS] do not upgrade.\n");
  763. }
  764. if (i_ret != 0)
  765. {
  766. //error handling ...
  767. //TBD
  768. }
  769. return i_ret;
  770. }
  771. #endif
  772. #ifdef CONFIG_TOUCH_PANEL_KEY
  773. static int is_tp_key(struct tp_key *tp_key, int key_num, int x, int y)
  774. {
  775. int i;
  776. if (tp_key && key_num) {
  777. for (i=0; i<key_num; i++) {
  778. if ((x > tp_key->x1) && (x < tp_key->x2)
  779. && (y > tp_key->y1) && (y < tp_key->y2)) {
  780. return tp_key->key;
  781. }
  782. tp_key++;
  783. }
  784. }
  785. return 0;
  786. }
  787. static enum hrtimer_restart ft5x0x_timer(struct hrtimer *timer)
  788. {
  789. struct ft5x0x_ts_data *data = container_of(timer, struct ft5x0x_ts_data, timer);
  790. if (data->touch_state == TOUCH_SCREEN) {
  791. input_report_key(data->input_dev, BTN_TOUCH, 0);
  792. input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, 0);
  793. input_sync(data->input_dev);
  794. ft5x0x_dbg("touch screen up(2)!\n");
  795. }
  796. else if (data->touch_state == TAP_KEY) {
  797. input_report_key(data->input_dev, data->key, 1);
  798. input_report_key(data->input_dev, data->key, 0);
  799. input_sync(data->input_dev);
  800. ft5x0x_dbg("touch key(%d) down(short)\n", data->key);
  801. ft5x0x_dbg("touch key(%d) up(short)\n", data->key);
  802. }
  803. data->touch_state = NO_TOUCH;
  804. return HRTIMER_NORESTART;
  805. };
  806. #endif
  807. /*
  808. * return event number.
  809. */
  810. static int ft5x0x_get_event(struct ts_event *event)
  811. {
  812. u8 buf[32] = {0};
  813. int ret, i;
  814. ret = ft5x0x_i2c_rxdata(buf, 31);
  815. if (ret < 0) {
  816. printk("%s read_data i2c_rxdata failed: %d\n", __func__, ret);
  817. return ret;
  818. }
  819. ret = (buf[2] & 0x07);
  820. if (ret > FT5X0X_EVENT_MAX) ret = FT5X0X_EVENT_MAX;
  821. for (i=0; i<ret; i++) {
  822. event->id = i;
  823. event->x = (s16)(buf[3+i*6] & 0x0F)<<8 | (s16)buf[4+i*6];
  824. event->y = (s16)(buf[5+i*6] & 0x0F)<<8 | (s16)buf[6+i*6];
  825. event->z = 200;
  826. event->w = 1;
  827. if (focaltechPdata2->swap_xy)
  828. swap(event->x, event->y);
  829. if (focaltechPdata2->xpol)
  830. event->x = focaltechPdata2->screen_max_x - event->x;
  831. if (focaltechPdata2->ypol)
  832. event->y = focaltechPdata2->screen_max_y - event->y;
  833. if (event->x == 0) event->x = 1;
  834. if (event->y == 0) event->y = 1;
  835. event++;
  836. }
  837. return ret;
  838. }
  839. static void ft5x0x_report_mt_event(struct input_dev *input, struct ts_event *event, int event_num)
  840. {
  841. int i;
  842. for (i=0; i<event_num; i++) {
  843. // input_report_abs(input, ABS_MT_TOUCH_ID, event->id);
  844. input_report_abs(input, ABS_MT_POSITION_X, event->x);
  845. input_report_abs(input, ABS_MT_POSITION_Y, event->y);
  846. input_report_abs(input, ABS_MT_TOUCH_MAJOR, event->z);
  847. input_report_abs(input, ABS_MT_WIDTH_MAJOR, event->w);
  848. input_mt_sync(input);
  849. ft5x0x_dbg("point_%d: %d, %d\n",event->id, event->x,event->y);
  850. event++;
  851. }
  852. input_sync(input);
  853. }
  854. #define enter_touch_screen_state() {\
  855. data->first_event = data->event[0];\
  856. data->offset = 0;\
  857. data->touch_count = 0;\
  858. input_report_key(data->input_dev, BTN_TOUCH, 1);\
  859. data->touch_state = TOUCH_SCREEN;\
  860. }
  861. #define enter_key_pre_state() {\
  862. data->key = key;\
  863. data->touch_count = 0;\
  864. data->touch_state = TOUCH_KEY_PRE;\
  865. }
  866. /***********************************************************************************************
  867. Name :
  868. Input :
  869. Output :
  870. function :
  871. ***********************************************************************************************/
  872. static void ft5x0x_ts_pen_irq_work(struct work_struct *work)
  873. {
  874. struct ft5x0x_ts_data *data = i2c_get_clientdata(this_client);
  875. struct ts_event *event = &data->event[0];
  876. int event_num = 0;
  877. event_num = ft5x0x_get_event(event);
  878. if (event_num < 0) {
  879. enable_irq(this_client->irq);
  880. return;
  881. }
  882. #ifdef AC_DETECT_IN_TOUCH_DRIVER
  883. Ac_Detect_In_Touch_Driver();
  884. #endif
  885. #ifdef CONFIG_TOUCH_PANEL_KEY
  886. int key = 0;
  887. if (event_num == 1) {
  888. key = is_tp_key(focaltechPdata2->tp_key, focaltechPdata2->tp_key_num, event->x, event->y);
  889. if (key)
  890. ft5x0x_dbg("key pos: %d, %d\n", event->x,event->y);
  891. }
  892. switch (data->touch_state) {
  893. case NO_TOUCH:
  894. if(key) {
  895. ft5x0x_dbg("touch key(%d) down 0\n", key);
  896. enter_key_pre_state();
  897. }
  898. else if (event_num) {
  899. ft5x0x_dbg("touch screen down\n");
  900. ft5x0x_report_mt_event(data->input_dev, event, event_num);
  901. enter_touch_screen_state();
  902. }
  903. break;
  904. case TOUCH_KEY_PRE:
  905. if (key) {
  906. data->key = key;
  907. if (++data->touch_count > TAP_KEY_TIME) {
  908. ft5x0x_dbg("touch key(%d) down\n", key);
  909. input_report_key(data->input_dev, key, 1);
  910. input_sync(data->input_dev);
  911. data->touch_state = TOUCH_KEY;
  912. }
  913. }
  914. else if(event_num) {
  915. ft5x0x_report_mt_event(data->input_dev, event, event_num);
  916. enter_touch_screen_state();
  917. }
  918. else {
  919. hrtimer_start(&data->timer, ktime_set(0, TAP_KEY_RELEASE_DELAY), HRTIMER_MODE_REL);
  920. data->touch_state = TAP_KEY;
  921. }
  922. break;
  923. case TAP_KEY:
  924. if (key) {
  925. hrtimer_cancel(&data->timer);
  926. input_report_key(data->input_dev, data->key, 1);
  927. input_report_key(data->input_dev, data->key, 0);
  928. input_sync(data->input_dev);
  929. ft5x0x_dbg("touch key(%d) down(tap)\n", data->key);
  930. ft5x0x_dbg("touch key(%d) up(tap)\n", data->key);
  931. enter_key_pre_state();
  932. }
  933. else if (event_num) {
  934. hrtimer_cancel(&data->timer);
  935. ft5x0x_dbg("ignore the tap key!\n");
  936. ft5x0x_report_mt_event(data->input_dev, event, event_num);
  937. enter_touch_screen_state();
  938. }
  939. break;
  940. case TOUCH_KEY:
  941. if (!event_num) {
  942. input_report_key(data->input_dev, data->key, 0);
  943. input_sync(data->input_dev);
  944. ft5x0x_dbg("touch key(%d) up\n", data->key);
  945. data->touch_state = NO_TOUCH;
  946. }
  947. break;
  948. case TOUCH_SCREEN:
  949. if (!event_num) {
  950. if ((data->offset < 10) && (data->touch_count > 1)) {
  951. input_report_key(data->input_dev, BTN_TOUCH, 0);
  952. input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, 0);
  953. input_sync(data->input_dev);
  954. ft5x0x_dbg("touch screen up!\n");
  955. data->touch_state = NO_TOUCH;
  956. }
  957. else {
  958. #if 0
  959. hrtimer_start(&data->timer, ktime_set(0, TP_KEY_GUARANTEE_TIME_AFTER_TOUCH), HRTIMER_MODE_REL);//honghaoyu
  960. #else
  961. input_report_key(data->input_dev, BTN_TOUCH, 0);
  962. input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, 0);
  963. input_sync(data->input_dev);
  964. data->touch_state = NO_TOUCH;
  965. #endif
  966. }
  967. }
  968. else {
  969. hrtimer_cancel(&data->timer);
  970. data->touch_count++;
  971. if (!key) {
  972. int offset;
  973. ft5x0x_report_mt_event(data->input_dev, event, event_num);
  974. offset = abs(event->x - data->first_event.x);
  975. offset += abs(event->y - data->first_event.y);
  976. if (offset > data->offset) data->offset = offset;
  977. }
  978. }
  979. break;
  980. default:
  981. break;
  982. }
  983. #else
  984. if (event_num)
  985. {
  986. input_report_key(data->input_dev, BTN_TOUCH, 1);
  987. ft5x0x_report_mt_event(data->input_dev, event, event_num);
  988. }
  989. else {
  990. input_report_key(data->input_dev, BTN_TOUCH, 0);
  991. input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, 0);
  992. input_sync(data->input_dev);
  993. }
  994. #endif
  995. enable_irq(this_client->irq);
  996. }
  997. /***********************************************************************************************
  998. Name :
  999. Input :
  1000. Output :
  1001. function :
  1002. ***********************************************************************************************/
  1003. static irqreturn_t ft5x0x_ts_interrupt(int irq, void *dev_id)
  1004. {
  1005. static int irq_count = 0;
  1006. ft5x0x_dbg("irq count: %d\n", irq_count++);
  1007. struct ft5x0x_ts_data *ft5x0x_ts = dev_id;
  1008. disable_irq_nosync(this_client->irq);
  1009. if (!work_pending(&ft5x0x_ts->pen_event_work)) {
  1010. queue_work(ft5x0x_ts->ts_workqueue, &ft5x0x_ts->pen_event_work);
  1011. }
  1012. return IRQ_HANDLED;
  1013. }
  1014. #ifdef CONFIG_HAS_EARLYSUSPEND
  1015. /***********************************************************************************************
  1016. Name :
  1017. Input :
  1018. Output :
  1019. function :
  1020. ***********************************************************************************************/
  1021. static void ft5x0x_ts_suspend(struct early_suspend *handler)
  1022. {
  1023. // struct ft5x0x_ts_data *ts;
  1024. // ts = container_of(handler, struct ft5x0x_ts_data, early_suspend);
  1025. printk("==ft5x0x_ts_suspend=\n");
  1026. if(focaltechPdata2->power)
  1027. focaltechPdata2->power(0);
  1028. else {
  1029. u8 data;
  1030. ft5x0x_write_reg(0xa5, 0x03);
  1031. printk("set reg[0xa5] = 0x03\n");
  1032. msleep(20);
  1033. ft5x0x_read_reg(0xa5, &data);
  1034. printk("read back: reg[0xa5] = %d\n", data);
  1035. }
  1036. if(focaltechPdata2->enable)
  1037. focaltechPdata2->enable(0);
  1038. if (focaltechPdata2->key_led_ctrl)
  1039. focaltechPdata2->key_led_ctrl(0);
  1040. }
  1041. /***********************************************************************************************
  1042. Name :
  1043. Input :
  1044. Output :
  1045. function :
  1046. ***********************************************************************************************/
  1047. static void ft5x0x_ts_resume(struct early_suspend *handler)
  1048. {
  1049. printk("==ft5x0x_ts_resume=\n");
  1050. if(focaltechPdata2->reset){
  1051. focaltechPdata2->reset(0);
  1052. msleep(15);
  1053. }
  1054. if(focaltechPdata2->power) {
  1055. focaltechPdata2->power(0);
  1056. msleep(50);
  1057. focaltechPdata2->power(1);
  1058. msleep(200);
  1059. }
  1060. if(focaltechPdata2->enable)
  1061. focaltechPdata2->enable(1);
  1062. if (focaltechPdata2->key_led_ctrl)
  1063. focaltechPdata2->key_led_ctrl(1);
  1064. }
  1065. #endif //CONFIG_HAS_EARLYSUSPEND
  1066. /***********************************************************************************************
  1067. Name :
  1068. Input :
  1069. Output :
  1070. function :
  1071. ***********************************************************************************************/
  1072. static int
  1073. ft5x0x_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1074. {
  1075. struct ft5x0x_ts_data *ft5x0x_ts;
  1076. struct input_dev *input_dev;
  1077. int err = 0;
  1078. unsigned char uc_reg_value;
  1079. struct ts_platform_data *pdata = client->dev.platform_data;
  1080. if (!pdata) {
  1081. printk("%s: no platform data\n", __FUNCTION__);
  1082. err = -ENODEV;
  1083. goto exit_check_functionality_failed;
  1084. }
  1085. focaltechPdata2 = client->dev.platform_data;
  1086. if(pdata->enable) {
  1087. pdata->enable(1);
  1088. mdelay(5);
  1089. }
  1090. if(pdata->power){
  1091. pdata->power(0);
  1092. mdelay(5);
  1093. pdata->power(1);
  1094. mdelay(300);
  1095. }
  1096. printk("==ft5x0x_ts_probe=\n");
  1097. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1098. err = -ENODEV;
  1099. goto exit_check_functionality_failed;
  1100. }
  1101. //client->irq = client->dev.platform_data->irq;
  1102. client->irq =pdata->irq;
  1103. printk("==kzalloc=\n");
  1104. ft5x0x_ts = kzalloc(sizeof(*ft5x0x_ts), GFP_KERNEL);
  1105. if (!ft5x0x_ts) {
  1106. err = -ENOMEM;
  1107. goto exit_alloc_data_failed;
  1108. }
  1109. printk("==kzalloc success=\n");
  1110. this_client = client;
  1111. i2c_set_clientdata(client, ft5x0x_ts);
  1112. INIT_WORK(&ft5x0x_ts->pen_event_work, ft5x0x_ts_pen_irq_work);
  1113. ft5x0x_ts->ts_workqueue = create_singlethread_workqueue(dev_name(&client->dev));
  1114. if (!ft5x0x_ts->ts_workqueue) {
  1115. err = -ESRCH;
  1116. goto exit_create_singlethread;
  1117. }
  1118. // __gpio_as_irq_fall_edge(pdata->intr); //
  1119. printk("==enable Irq=\n");
  1120. if (pdata->init_irq) {
  1121. pdata->init_irq();
  1122. }
  1123. printk("==enable Irq success=\n");
  1124. disable_irq_nosync(this_client->irq);
  1125. // disable_irq(IRQ_EINT(6));
  1126. printk("==input_allocate_device=\n");
  1127. input_dev = input_allocate_device();
  1128. if (!input_dev) {
  1129. err = -ENOMEM;
  1130. dev_err(&client->dev, "failed to allocate input device\n");
  1131. goto exit_input_dev_alloc_failed;
  1132. }
  1133. ft5x0x_ts->input_dev = input_dev;
  1134. #ifdef CONFIG_TOUCH_PANEL_KEY
  1135. ft5x0x_ts->touch_state = NO_TOUCH;
  1136. ft5x0x_ts->key = 0;
  1137. if (pdata->tp_key && pdata->tp_key_num) {
  1138. int i;
  1139. for (i=0; i<pdata->tp_key_num; i++) {
  1140. set_bit(pdata->tp_key[i].key, input_dev->keybit);
  1141. printk("tp key (%d)registered\n", pdata->tp_key[i].key);
  1142. }
  1143. set_bit(EV_SYN, input_dev->evbit);
  1144. hrtimer_init(&ft5x0x_ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1145. ft5x0x_ts->timer.function = ft5x0x_timer;
  1146. }
  1147. #endif
  1148. set_bit(BTN_TOUCH, input_dev->keybit);
  1149. set_bit(ABS_MT_TOUCH_MAJOR, input_dev->absbit);
  1150. set_bit(ABS_MT_POSITION_X, input_dev->absbit);
  1151. set_bit(ABS_MT_POSITION_Y, input_dev->absbit);
  1152. set_bit(ABS_MT_WIDTH_MAJOR, input_dev->absbit);
  1153. input_set_abs_params(input_dev,
  1154. ABS_MT_POSITION_X, 0, pdata->screen_max_x, 0, 0);
  1155. input_set_abs_params(input_dev,
  1156. ABS_MT_POSITION_Y, 0, pdata->screen_max_y, 0, 0);
  1157. input_set_abs_params(input_dev,
  1158. ABS_MT_TOUCH_MAJOR, 0, PRESS_MAX, 0, 0);
  1159. input_set_abs_params(input_dev,
  1160. ABS_MT_WIDTH_MAJOR, 0, 200, 0, 0);
  1161. set_bit(EV_ABS, input_dev->evbit);
  1162. set_bit(EV_KEY, input_dev->evbit);
  1163. input_dev->name = FT5X0X_NAME; //dev_name(&client->dev)
  1164. err = input_register_device(input_dev);
  1165. if (err) {
  1166. dev_err(&client->dev,
  1167. "ft5x0x_ts_probe: failed to register input device: %s\n",
  1168. dev_name(&client->dev));
  1169. goto exit_input_register_device_failed;
  1170. }
  1171. #ifdef CONFIG_HAS_EARLYSUSPEND
  1172. printk("==register_early_suspend =\n");
  1173. ft5x0x_ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  1174. ft5x0x_ts->early_suspend.suspend = ft5x0x_ts_suspend;
  1175. ft5x0x_ts->early_suspend.resume = ft5x0x_ts_resume;
  1176. register_early_suspend(&ft5x0x_ts->early_suspend);
  1177. #endif
  1178. uc_reg_value = ft5x0x_read_fw_ver();
  1179. printk("[FST] Firmware version = 0x%x\n", uc_reg_value);
  1180. #ifdef CONFIG_FOCALTECH_TOUCHSCREEN_CODE_UPG
  1181. msleep(50);
  1182. //get some register information
  1183. uc_reg_value = ft5x0x_read_fw_ver();
  1184. printk("[FST] Firmware old version = 0x%x\n", uc_reg_value);
  1185. fts_ctpm_fw_upgrade_with_i_file();
  1186. mdelay(200);
  1187. uc_reg_value = ft5x0x_read_fw_ver();
  1188. printk("[FST] Firmware new version after update = 0x%x\n", uc_reg_value);
  1189. #endif
  1190. err = request_irq(client->irq, ft5x0x_ts_interrupt, IRQF_DISABLED, "ft5x0x_ts", ft5x0x_ts);
  1191. // err = request_irq(IRQ_EINT(6), ft5x0x_ts_interrupt, IRQF_TRIGGER_FALLING, "ft5x0x_ts", ft5x0x_ts);
  1192. if (err < 0) {
  1193. dev_err(&client->dev, "ft5x0x_probe: request irq failed\n");
  1194. goto exit_irq_request_failed;
  1195. }
  1196. //wake the CTPM
  1197. // __gpio_as_output(GPIO_FT5X0X_WAKE);
  1198. // __gpio_clear_pin(GPIO_FT5X0X_WAKE); //set wake = 0,base on system
  1199. // msleep(100);
  1200. // __gpio_set_pin(GPIO_FT5X0X_WAKE); //set wake = 1,base on system
  1201. // msleep(100);
  1202. // ft5x0x_set_reg(0x88, 0x05); //5, 6,7,8
  1203. // ft5x0x_set_reg(0x80, 30);
  1204. // msleep(50);
  1205. // enable_irq(this_client->irq);
  1206. // enable_irq(IRQ_EINT(6));
  1207. err = sysfs_create_group(&client->dev.kobj, &ft5x0x_attr_group);
  1208. if (focaltechPdata2->key_led_ctrl)
  1209. focaltechPdata2->key_led_ctrl(1);
  1210. printk("==probe over =\n");
  1211. return 0;
  1212. exit_input_register_device_failed:
  1213. input_free_device(input_dev);
  1214. exit_input_dev_alloc_failed:
  1215. free_irq(client->irq, ft5x0x_ts);
  1216. // free_irq(IRQ_EINT(6), ft5x0x_ts);
  1217. exit_irq_request_failed:
  1218. cancel_work_sync(&ft5x0x_ts->pen_event_work);
  1219. destroy_workqueue(ft5x0x_ts->ts_workqueue);
  1220. exit_create_singlethread:
  1221. printk("==singlethread error =\n");
  1222. i2c_set_clientdata(client, NULL);
  1223. kfree(ft5x0x_ts);
  1224. exit_alloc_data_failed:
  1225. exit_check_functionality_failed:
  1226. return err;
  1227. }
  1228. /***********************************************************************************************
  1229. Name :
  1230. Input :
  1231. Output :
  1232. function :
  1233. ***********************************************************************************************/
  1234. static int __devexit ft5x0x_ts_remove(struct i2c_client *client)
  1235. {
  1236. printk("==ft5x0x_ts_remove=\n");
  1237. struct ft5x0x_ts_data *ft5x0x_ts = i2c_get_clientdata(client);
  1238. unregister_early_suspend(&ft5x0x_ts->early_suspend);
  1239. free_irq(client->irq, ft5x0x_ts);
  1240. // free_irq(IRQ_EINT(6), ft5x0x_ts);
  1241. input_unregister_device(ft5x0x_ts->input_dev);
  1242. kfree(ft5x0x_ts);
  1243. cancel_work_sync(&ft5x0x_ts->pen_event_work);
  1244. destroy_workqueue(ft5x0x_ts->ts_workqueue);
  1245. i2c_set_clientdata(client, NULL);
  1246. return 0;
  1247. }
  1248. static const struct i2c_device_id ft5x0x_ts_id[] = {
  1249. { FT5X0X_NAME, 0 },{ }
  1250. };
  1251. MODULE_DEVICE_TABLE(i2c, ft5x0x_ts_id);
  1252. static struct i2c_driver ft5x0x_ts_driver = {
  1253. .probe = ft5x0x_ts_probe,
  1254. .remove = __devexit_p(ft5x0x_ts_remove),
  1255. .id_table = ft5x0x_ts_id,
  1256. .driver = {
  1257. .name = FT5X0X_NAME,
  1258. .owner = THIS_MODULE,
  1259. },
  1260. };
  1261. /***********************************************************************************************
  1262. Name :
  1263. Input :
  1264. Output :
  1265. function :
  1266. ***********************************************************************************************/
  1267. static int __init ft5x0x_ts_init(void)
  1268. {
  1269. int ret;
  1270. printk("==ft5x0x_ts_init==\n");
  1271. ret = i2c_add_driver(&ft5x0x_ts_driver);
  1272. printk("ret=%d\n",ret);
  1273. return ret;
  1274. // return i2c_add_driver(&ft5x0x_ts_driver);
  1275. }
  1276. /***********************************************************************************************
  1277. Name :
  1278. Input :
  1279. Output :
  1280. function :
  1281. ***********************************************************************************************/
  1282. static void __exit ft5x0x_ts_exit(void)
  1283. {
  1284. printk("==ft5x0x_ts_exit==\n");
  1285. i2c_del_driver(&ft5x0x_ts_driver);
  1286. }
  1287. module_init(ft5x0x_ts_init);
  1288. module_exit(ft5x0x_ts_exit);
  1289. MODULE_AUTHOR("<wenfs@Focaltech-systems.com>");
  1290. MODULE_DESCRIPTION("FocalTech ft5x0x TouchScreen driver");
  1291. MODULE_LICENSE("GPL");