rk fb: extend output mutex lock in win config
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / ft5506_wgj.c
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  *
18  *    note: only support mulititouch    Wenfs 2010-10-01
19  */
20
21 #include <linux/irq.h>
22 #include <asm/mach/irq.h>
23 #include <linux/slab.h>
24 #include <linux/i2c.h>
25 #include <linux/input.h>
26 //#include "ft5x0x_i2c_ts.h"
27 #include "ft5506_wgj.h"
28 #include <linux/interrupt.h>
29 #include <linux/delay.h>
30 #include <linux/platform_device.h>
31 #include <linux/async.h>
32 #include <mach/gpio.h>
33 #include <mach/board.h>
34 #include <linux/earlysuspend.h>
35 #include <linux/input/mt.h>
36
37
38 //#include "ft5x06_ex_fun.h"
39 /*
40 enum touchstate {
41         TOUCH_UP = 0, TOUCH_DOWN = 1,
42 };
43 #define TOUCH_NUMBER 10
44
45 static int touch_state[TOUCH_NUMBER] = {TOUCH_UP,TOUCH_UP};
46 */
47
48 struct FTS_TS_DATA_T {
49         struct  i2c_client *client;
50     struct input_dev    *input_dev;
51     struct FTS_TS_EVENT_T        event;
52     struct work_struct     pen_event_work;
53     struct workqueue_struct *ts_workqueue;
54     struct      early_suspend early_suspend;
55 };
56
57 /* -------------- global variable definition -----------*/
58 static struct i2c_client *this_client;
59 static REPORT_FINGER_INFO_T _st_finger_infos[CFG_MAX_POINT_NUM];
60 //static unsigned int _sui_irq_num= IRQ_EINT(6);
61 static int _si_touch_num = 0; 
62
63
64
65 int tsp_keycodes[CFG_NUMOFKEYS] ={
66
67         KEY_MENU,
68         KEY_HOME,
69         KEY_BACK,
70         KEY_SEARCH
71 };
72
73 char *tsp_keyname[CFG_NUMOFKEYS] ={
74
75         "Menu",
76         "Home",
77         "Back",
78         "Search"
79 };
80
81 static bool tsp_keystatus[CFG_NUMOFKEYS];
82
83
84 #ifdef CONFIG_HAS_EARLYSUSPEND
85 static void ft5x0x_ts_early_suspend(struct early_suspend *h);
86 static void ft5x0x_ts_late_resume(struct early_suspend *h);
87 #endif
88
89
90 /***********************************************************************
91   [function]: 
92 callback:              read data from ctpm by i2c interface;
93 [parameters]:
94 buffer[in]:            data buffer;
95 length[in]:           the length of the data buffer;
96 [return]:
97 FTS_TRUE:            success;
98 FTS_FALSE:           fail;
99  ************************************************************************/
100 static bool i2c_read_interface(u8* pbt_buf, int dw_lenth)
101 {
102         int ret;
103
104         ret=i2c_master_recv(this_client, pbt_buf, dw_lenth);
105
106         if(ret<=0)
107         {
108                 printk("[TSP]i2c_read_interface error\n");
109                 return FTS_FALSE;
110         }
111
112         return FTS_TRUE;
113 }
114
115
116
117 /***********************************************************************
118   [function]: 
119 callback:               write data to ctpm by i2c interface;
120 [parameters]:
121 buffer[in]:             data buffer;
122 length[in]:            the length of the data buffer;
123 [return]:
124 FTS_TRUE:            success;
125 FTS_FALSE:           fail;
126  ************************************************************************/
127 static bool  i2c_write_interface(u8* pbt_buf, int dw_lenth)
128 {
129         int ret;
130         ret=i2c_master_send(this_client, pbt_buf, dw_lenth);
131         if(ret<=0)
132         {
133                 printk("[TSP]i2c_write_interface error line = %d, ret = %d\n", __LINE__, ret);
134                 return FTS_FALSE;
135         }
136
137         return FTS_TRUE;
138 }
139
140
141
142 /***********************************************************************
143   [function]: 
144 callback:                 read register value ftom ctpm by i2c interface;
145 [parameters]:
146 reg_name[in]:         the register which you want to read;
147 rx_buf[in]:              data buffer which is used to store register value;
148 rx_length[in]:          the length of the data buffer;
149 [return]:
150 FTS_TRUE:              success;
151 FTS_FALSE:             fail;
152  ************************************************************************/
153 static bool fts_register_read(u8 reg_name, u8* rx_buf, int rx_length)
154 {
155         u8 read_cmd[2]= {0};
156         u8 cmd_len      = 0;
157
158         read_cmd[0] = reg_name;
159         cmd_len = 1;    
160
161         /*send register addr*/
162         if(!i2c_write_interface(&read_cmd[0], cmd_len))
163         {
164                 return FTS_FALSE;
165         }
166
167         /*call the read callback function to get the register value*/           
168         if(!i2c_read_interface(rx_buf, rx_length))
169         {
170                 return FTS_FALSE;
171         }
172         return FTS_TRUE;
173 }
174
175
176
177
178 /***********************************************************************
179   [function]: 
180 callback:                read register value ftom ctpm by i2c interface;
181 [parameters]:
182 reg_name[in]:         the register which you want to write;
183 tx_buf[in]:              buffer which is contained of the writing value;
184 [return]:
185 FTS_TRUE:              success;
186 FTS_FALSE:             fail;
187  ************************************************************************/
188 static bool fts_register_write(u8 reg_name, u8* tx_buf)
189 {
190         u8 write_cmd[2] = {0};
191
192         write_cmd[0] = reg_name;
193         write_cmd[1] = *tx_buf;
194
195         /*call the write callback function*/
196         return i2c_write_interface(write_cmd, 2);
197 }
198
199
200
201
202 /***********************************************************************
203   [function]: 
204 callback:        report to the input system that the finger is put up;
205 [parameters]:
206 null;
207 [return]:
208 null;
209  ************************************************************************/
210 static void fts_ts_release(void)
211 {
212         struct FTS_TS_DATA_T *data = i2c_get_clientdata(this_client);
213         int i;
214         int i_need_sync = 0;
215         for ( i= 0; i<CFG_MAX_POINT_NUM; ++i )
216         {
217                 if ( _st_finger_infos[i].u2_pressure == -1 )
218                         continue;
219
220                 input_mt_slot(data->input_dev, i);
221                 input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER, false);
222
223 /*
224                 input_report_abs(data->input_dev, ABS_MT_POSITION_X, _st_finger_infos[i].i2_x);
225                 input_report_abs(data->input_dev, ABS_MT_POSITION_Y, _st_finger_infos[i].i2_y);
226                 input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, _st_finger_infos[i].u2_pressure);
227                 input_report_abs(data->input_dev, ABS_MT_TRACKING_ID, _st_finger_infos[i].ui2_id);
228                 input_mt_sync(data->input_dev);
229 */
230                 i_need_sync = 1;
231
232                 if ( _st_finger_infos[i].u2_pressure == 0 )
233                         _st_finger_infos[i].u2_pressure= -1;
234         }
235
236         if (i_need_sync)
237         {
238                 printk("+++++++enter fts_ts_release++++++++\n");
239                 input_sync(data->input_dev);
240         }
241
242         _si_touch_num = 0;
243 }
244
245
246
247
248
249
250 /***********************************************************************
251   [function]: 
252 callback:                 read touch  data ftom ctpm by i2c interface;
253 [parameters]:
254 rxdata[in]:              data buffer which is used to store touch data;
255 length[in]:              the length of the data buffer;
256 [return]:
257 FTS_TRUE:              success;
258 FTS_FALSE:             fail;
259  ************************************************************************/
260 static int fts_i2c_rxdata(u8 *rxdata, int length)
261 {
262         int ret;
263         struct i2c_msg msg;
264
265
266         msg.addr = this_client->addr;
267         msg.flags = 0;
268         msg.len = 1;
269         msg.buf = rxdata;
270         msg.scl_rate = FT5X0X_I2C_SPEED;
271         
272         ret = i2c_transfer(this_client->adapter, &msg, 1);
273
274         if(ret == 0){
275                 pr_err("msg %s line:%d i2c write error: %d\n", __func__, __LINE__,ret);
276                 return -EBUSY;
277         }else if(ret < 0){
278                 pr_err("msg %s line:%d i2c write error: %d\n", __func__, __LINE__,ret);
279                 return ret;
280         }
281         
282         msg.addr = this_client->addr;
283         msg.flags = I2C_M_RD;
284         msg.len = length;
285         msg.buf = rxdata;
286         msg.scl_rate = FT5X0X_I2C_SPEED;
287         ret = i2c_transfer(this_client->adapter, &msg, 1);
288         
289         if(ret == 0){
290                 pr_err("msg %s line:%d i2c write error: %d\n", __func__, __LINE__,ret);
291                 return -EBUSY;
292         }else if(ret < 0){
293                 pr_err("msg %s line:%d i2c write error: %d\n", __func__, __LINE__,ret);
294                 return ret;
295         }
296
297         return ret;
298 }
299
300
301
302
303
304 /***********************************************************************
305   [function]: 
306 callback:                send data to ctpm by i2c interface;
307 [parameters]:
308 txdata[in]:              data buffer which is used to send data;
309 length[in]:              the length of the data buffer;
310 [return]:
311 FTS_TRUE:              success;
312 FTS_FALSE:             fail;
313  ************************************************************************/
314 static int fts_i2c_txdata(u8 *txdata, int length)
315 {
316         int ret;
317
318         struct i2c_msg msg;
319
320         msg.addr = this_client->addr;
321         msg.flags = 0;
322         msg.len = length;
323         msg.buf = txdata;
324         msg.scl_rate = FT5X0X_I2C_SPEED;
325         
326         ret = i2c_transfer(this_client->adapter, &msg, 1);
327         
328         if(ret == 0){
329                 pr_err("msg %s line:%d i2c write error: %d\n", __func__, __LINE__,ret);
330                 return -EBUSY;
331         }else if(ret < 0){
332                 pr_err("msg %s line:%d i2c write error: %d\n", __func__, __LINE__,ret);
333                 return ret;
334         }
335
336         return ret;
337 }
338
339
340 static int ft5x0x_set_reg(u8 addr, u8 para)
341 {
342     u8 buf[3];
343     int ret = -1;
344     buf[0] = addr;
345     buf[1] = para;
346     ret = fts_i2c_txdata(buf, 2);
347     if (ret < 0) {
348         pr_err("write reg failed! %#x ret: %d", buf[0], ret);
349         return -1;
350     }
351     return 0;
352 }
353
354
355 /***********************************************************************
356   [function]: 
357 callback:            gather the finger information and calculate the X,Y
358 coordinate then report them to the input system;
359 [parameters]:
360 null;
361 [return]:
362 null;
363  ************************************************************************/
364 int fts_read_data(void)
365 {
366         struct FTS_TS_DATA_T *data = i2c_get_clientdata(this_client);
367         u8 buf[64] = {0};
368 //      static int key_id=0x80;
369
370         int id,i_count,ret = -1;
371         int touch_point_num = 0, touch_event, x, y;
372         int tmp, swap ,sync_flag = 0;
373 /*
374         buf[0] = 2;//ADDR
375         ret = fts_i2c_rxdata(buf, 1);
376         if (ret > 0) 
377                 touch_point_num = buf[0]&0xf;
378         else
379                 printk(KERN_ERR "get fingers failed!\n");
380 */
381   touch_point_num = 10;
382
383         if(touch_point_num > CFG_MAX_POINT_NUM)
384                 printk("[Warning],+++++Touch number[%d] readed is larger than max point number+++++++++++++\n",touch_point_num);
385         
386         i_count = 0;
387
388         if(touch_point_num != 0)
389         {
390                 buf[0] = 3;//ADDR
391                 ret=fts_i2c_rxdata(buf, 6*touch_point_num);
392                 if(ret >= 0)
393                 {
394                         do
395                         {
396                                 id = buf[2+i_count*6]>>4;
397 //                              printk("Touch ID readed is id=%d\n",id);
398 //                              if(id <0 || id>CFG_MAX_POINT_NUM)
399 //                                      printk("[ERROR] Touch ID readed is illegal!! id=%d\n",id);
400                                 
401                                 touch_event = buf[i_count*6]>>6; 
402                                 x =((buf[i_count*6]& 0x0f)<<8) |buf[i_count*6+1];
403                                 y =( (buf[i_count*6+2]& 0x0f)<<8) | buf[i_count*6+3];
404                                                                 
405                                 //adjust value
406                                 //x =  768 - x;
407                                 //y = 1024 - y; 
408 #if 0
409                                 {//swap
410                                                 swap = x;
411                                                 x = y;
412                                                 y = swap;
413                                 }
414 #endif
415  //     y = (768-y);
416                                 if (touch_event == 0) //down
417                                 {
418                                         _st_finger_infos[i_count].u2_pressure= 1;//pressure;
419                                         _st_finger_infos[i_count].i2_x= (int16_t)x;
420                                         _st_finger_infos[i_count].i2_y= (int16_t)y;
421                                 }
422                                 else if (touch_event == 1) //up event
423                                 {
424                                         _st_finger_infos[i_count].u2_pressure= 0;
425                                 }
426                                 else if (touch_event == 2) //move
427                                 {
428                                         _st_finger_infos[i_count].u2_pressure= 1;//pressure;
429                                         _st_finger_infos[i_count].i2_x= (int16_t)x;
430                                         _st_finger_infos[i_count].i2_y= (int16_t)y;
431                                 }
432                                 else    /*bad event, ignore*/
433                                 {
434                                         //printk("Bad event, ignore!!!\n");
435                                         i_count++;
436                                         continue;  
437                                 }
438                                 
439                                 if(_st_finger_infos[i_count].u2_pressure == 1)//down
440                                 {
441                                         input_mt_slot(data->input_dev, id);
442                                         input_report_abs(data->input_dev, ABS_MT_TRACKING_ID, id);
443                                         input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER, true);                                      
444                                         input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, 1);
445                                         input_report_abs(data->input_dev, ABS_MT_POSITION_X,  _st_finger_infos[i_count].i2_x);
446                                         input_report_abs(data->input_dev, ABS_MT_POSITION_Y,  _st_finger_infos[i_count].i2_y);
447 //                                      printk("ABS_MT_POSITION_X == %d, ABS_MT_POSITION_Y == %d\n",_st_finger_infos[i_count].i2_x,_st_finger_infos[i_count].i2_y);
448                                                                                 
449                                         sync_flag = 1;
450                                 }
451                                 
452                                 else if((_st_finger_infos[i_count].u2_pressure == 0)||(touch_event == 1))//up
453                                 {
454                                         input_mt_slot(data->input_dev, id);
455                                         input_report_abs(data->input_dev, ABS_MT_TRACKING_ID, -1);
456                                         input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER, false);
457 //                                input_sync(data->input_dev);
458                                         sync_flag = 1;
459                                 }
460                                 
461
462                                 if(_st_finger_infos[i_count].u2_pressure == 0 )
463                                 {
464                                         _st_finger_infos[i_count].u2_pressure= -1;
465                                 }                               
466 //                              input_sync(data->input_dev);
467                                 
468
469                                 i_count++;
470                         }while(i_count < touch_point_num);
471                         
472                         if(sync_flag)
473                                 input_sync(data->input_dev);
474 //                              input_mt_sync(data->input_dev);
475
476                 }
477                 else
478                 {
479                         printk("[LAIBAO] ERROR: in %s, line %d, ret = %d\n",
480                                         __FUNCTION__, __LINE__, ret);
481                 }
482         }
483
484         //If touch number is zero then release touch.
485         if(touch_point_num == 0 )
486         {
487                 fts_ts_release();
488         }
489         
490         return 0;
491 }
492
493
494
495 static void fts_work_func(struct work_struct *work)
496 {
497         //disable_irq(this_client->irq);
498         fts_read_data();    
499         enable_irq(this_client->irq);
500 }
501
502
503
504
505 static irqreturn_t fts_ts_irq(int irq, void *dev_id)
506 {
507         struct FTS_TS_DATA_T *ft5x0x_ts = dev_id;
508 //      printk(KERN_ALERT "fts_tp_irq\n");
509         if (!work_pending(&ft5x0x_ts->pen_event_work)) {
510                 disable_irq_nosync(this_client->irq);
511                 queue_work(ft5x0x_ts->ts_workqueue, &ft5x0x_ts->pen_event_work);
512         }
513
514         return IRQ_HANDLED;
515 }
516
517
518
519 /***********************************************************************
520   [function]: 
521 callback:         send a command to ctpm.
522 [parameters]:
523 btcmd[in]:       command code;
524 btPara1[in]:     parameter 1;    
525 btPara2[in]:     parameter 2;    
526 btPara3[in]:     parameter 3;    
527 num[in]:         the valid input parameter numbers, 
528 if only command code needed and no 
529 parameters followed,then the num is 1;    
530 [return]:
531 FTS_TRUE:      success;
532 FTS_FALSE:     io fail;
533  ************************************************************************/
534 static bool cmd_write(u8 btcmd,u8 btPara1,u8 btPara2,u8 btPara3,u8 num)
535 {
536         u8 write_cmd[4] = {0};
537
538         write_cmd[0] = btcmd;
539         write_cmd[1] = btPara1;
540         write_cmd[2] = btPara2;
541         write_cmd[3] = btPara3;
542         return i2c_write_interface(write_cmd, num);
543 }
544
545
546
547
548 /***********************************************************************
549   [function]: 
550 callback:         write a byte data  to ctpm;
551 [parameters]:
552 buffer[in]:       write buffer;
553 length[in]:      the size of write data;    
554 [return]:
555 FTS_TRUE:      success;
556 FTS_FALSE:     io fail;
557  ************************************************************************/
558 static bool byte_write(u8* buffer, int length)
559 {
560
561         return i2c_write_interface(buffer, length);
562 }
563
564
565
566
567 /***********************************************************************
568   [function]: 
569 callback:         read a byte data  from ctpm;
570 [parameters]:
571 buffer[in]:       read buffer;
572 length[in]:      the size of read data;    
573 [return]:
574 FTS_TRUE:      success;
575 FTS_FALSE:     io fail;
576  ************************************************************************/
577 static bool byte_read(u8* buffer, int length)
578 {
579         return i2c_read_interface(buffer, length);
580 }
581
582
583
584
585
586 #define    FTS_PACKET_LENGTH        128
587
588 static unsigned char CTPM_FW[]=
589 {
590         //#include "ft_app.i"
591         #include "ft5506_app.f"
592 };
593
594
595
596
597 /***********************************************************************
598   [function]: 
599 callback:          burn the FW to ctpm.
600 [parameters]:
601 pbt_buf[in]:     point to Head+FW ;
602 dw_lenth[in]:   the length of the FW + 6(the Head length);    
603 [return]:
604 ERR_OK:          no error;
605 ERR_MODE:      fail to switch to UPDATE mode;
606 ERR_READID:   read id fail;
607 ERR_ERASE:     erase chip fail;
608 ERR_STATUS:   status error;
609 ERR_ECC:        ecc error.
610  ************************************************************************/
611 E_UPGRADE_ERR_TYPE  fts_ctpm_fw_upgrade(u8* pbt_buf, int dw_lenth)
612 {
613         u8  cmd,reg_val[2] = {0};
614         u8  packet_buf[FTS_PACKET_LENGTH + 6];
615         u8  auc_i2c_write_buf[10];
616         u8  bt_ecc;
617
618         int  j,temp,lenght,i_ret,packet_number, i = 0;
619         int  i_is_new_protocol = 0;
620
621
622         /******write 0xaa to register 0xfc******/
623         cmd=0xaa;
624         fts_register_write(0xfc,&cmd);
625         mdelay(50);
626
627         /******write 0x55 to register 0xfc******/
628         cmd=0x55;
629         fts_register_write(0xfc,&cmd);
630         printk("[TSP] Step 1: Reset CTPM test\n");
631
632         mdelay(30);
633
634
635         /*******Step 2:Enter upgrade mode ****/
636         printk("\n[TSP] Step 2:enter new update mode\n");
637         auc_i2c_write_buf[0] = 0x55;
638         auc_i2c_write_buf[1] = 0xaa;
639         do
640         {
641                 i ++;
642                 i_ret = fts_i2c_txdata(auc_i2c_write_buf, 2);
643                 mdelay(5);
644         }while(i_ret <= 0 && i < 10 );
645
646         if (i > 1)
647         {
648                 i_is_new_protocol = 1;
649         }
650   
651   mdelay(100);
652         /********Step 3:check READ-ID********/        
653         cmd_write(0x90,0x00,0x00,0x00,4);
654         msleep(500);
655         byte_read(reg_val,2);
656         
657         printk("=============CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",reg_val[0],reg_val[1]);
658         
659         //if (reg_val[0] == 0x79 && reg_val[1] == 0x3)
660         if (reg_val[0] == 0x79 && reg_val[1] == 0x6)
661         {
662                 printk("[TSP] Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",reg_val[0],reg_val[1]);
663         }
664         else
665         {
666                 return ERR_READID;
667                 //i_is_new_protocol = 1;
668         }
669
670
671         /*********Step 4:erase app**********/
672         if (i_is_new_protocol)
673         {
674                 cmd_write(0x61,0x00,0x00,0x00,1);
675         }
676         else
677         {
678                 cmd_write(0x60,0x00,0x00,0x00,1);
679         }
680         mdelay(2000);
681         printk("[TSP] Step 4: erase. \n");
682
683
684
685         /*Step 5:write firmware(FW) to ctpm flash*/
686         bt_ecc = 0;
687         printk("[TSP] Step 5: start upgrade. \n");
688         dw_lenth = dw_lenth - 8;
689         packet_number = (dw_lenth) / FTS_PACKET_LENGTH;
690         packet_buf[0] = 0xbf;
691         packet_buf[1] = 0x00;
692         for (j=0;j<packet_number;j++)
693         {
694                 temp = j * FTS_PACKET_LENGTH;
695                 packet_buf[2] = (FTS_BYTE)(temp>>8);
696                 packet_buf[3] = (FTS_BYTE)temp;
697                 lenght = FTS_PACKET_LENGTH;
698                 packet_buf[4] = (FTS_BYTE)(lenght>>8);
699                 packet_buf[5] = (FTS_BYTE)lenght;
700
701                 for (i=0;i<FTS_PACKET_LENGTH;i++)
702                 {
703                         packet_buf[6+i] = pbt_buf[j*FTS_PACKET_LENGTH + i]; 
704                         bt_ecc ^= packet_buf[6+i];
705                 }
706
707                 byte_write(&packet_buf[0],FTS_PACKET_LENGTH + 6);
708                 mdelay(FTS_PACKET_LENGTH/6 + 1);
709                 if ((j * FTS_PACKET_LENGTH % 1024) == 0)
710                 {
711                         printk("[TSP] upgrade the 0x%x th byte.\n", ((unsigned int)j) * FTS_PACKET_LENGTH);
712                 }
713         }
714
715         if ((dw_lenth) % FTS_PACKET_LENGTH > 0)
716         {
717                 temp = packet_number * FTS_PACKET_LENGTH;
718                 packet_buf[2] = (FTS_BYTE)(temp>>8);
719                 packet_buf[3] = (FTS_BYTE)temp;
720
721                 temp = (dw_lenth) % FTS_PACKET_LENGTH;
722                 packet_buf[4] = (FTS_BYTE)(temp>>8);
723                 packet_buf[5] = (FTS_BYTE)temp;
724
725                 for (i=0;i<temp;i++)
726                 {
727                         packet_buf[6+i] = pbt_buf[ packet_number*FTS_PACKET_LENGTH + i]; 
728                         bt_ecc ^= packet_buf[6+i];
729                 }
730
731                 byte_write(&packet_buf[0],temp+6);    
732                 mdelay(20);
733         }
734
735         /***********send the last six byte**********/
736         for (i = 0; i<6; i++)
737         {
738                 temp = 0x6ffa + i;
739                 packet_buf[2] = (FTS_BYTE)(temp>>8);
740                 packet_buf[3] = (FTS_BYTE)temp;
741                 temp =1;
742                 packet_buf[4] = (FTS_BYTE)(temp>>8);
743                 packet_buf[5] = (FTS_BYTE)temp;
744                 packet_buf[6] = pbt_buf[ dw_lenth + i]; 
745                 bt_ecc ^= packet_buf[6];
746
747                 byte_write(&packet_buf[0],7);  
748                 mdelay(20);
749         }
750
751         /********send the opration head************/
752         cmd_write(0xcc,0x00,0x00,0x00,1);
753         byte_read(reg_val,1);
754         printk("[TSP] Step 6:  ecc read 0x%x, new firmware 0x%x. \n", reg_val[0], bt_ecc);
755         if(reg_val[0] != bt_ecc)
756         {
757                 return ERR_ECC;
758         }
759
760         /*******Step 7: reset the new FW**********/
761         cmd_write(0x07,0x00,0x00,0x00,1);
762
763         return ERR_OK;
764 }
765
766
767
768
769 int fts_ctpm_fw_upgrade_with_i_file(void)
770 {
771         u8*     pbt_buf = FTS_NULL;
772         int i_ret;
773
774         pbt_buf = CTPM_FW;
775         i_ret =  fts_ctpm_fw_upgrade(pbt_buf,sizeof(CTPM_FW));
776
777         return i_ret;
778 }
779
780 unsigned char fts_ctpm_get_upg_ver(void)
781 {
782         unsigned int ui_sz;
783
784         ui_sz = sizeof(CTPM_FW);
785         if (ui_sz > 2)
786         {
787                 return CTPM_FW[ui_sz - 2];
788         }
789         else
790                 return 0xff; 
791
792 }
793
794 void ft5x0x_ts_set_standby(struct i2c_client *client, int enable)
795 {
796 /* 
797     struct laibao_platform_data *mach_info = client->dev.platform_data;
798         unsigned pwr_pin = mach_info->pwr_pin;
799         unsigned pwr_on_value = mach_info->pwr_on_value;
800         unsigned reset_pin = mach_info->reset_pin;
801         unsigned reset_value = mach_info->reset_value;
802
803     printk("%s : %s, enable = %d\n", __FILE__, __FUNCTION__,enable);
804     if(pwr_pin != INVALID_GPIO)
805     {
806         gpio_direction_output(pwr_pin, 0);
807         gpio_set_value(pwr_pin, enable ? pwr_on_value : !pwr_on_value);                         
808     }
809     if(reset_pin != INVALID_GPIO)
810     {
811         gpio_direction_output(reset_pin, enable ? reset_value : !reset_value);
812         gpio_set_value(reset_pin, enable ? reset_value : !reset_value);                         
813 */
814 }
815
816 #ifdef CONFIG_HAS_EARLYSUSPEND
817
818 static void ft5x0x_ts_early_suspend(struct early_suspend *h)
819 {
820         int ret;
821         struct FTS_TS_DATA_T *data = i2c_get_clientdata(this_client);
822   printk("enter ft5x0x_ts_early_suspend\n");
823         if(this_client->irq)
824                 disable_irq_nosync(this_client->irq);   
825                 ret = cancel_work_sync(&data->pen_event_work);  
826                 if (ret && this_client->irq) // if work was pending disable-count is now 2 
827                 enable_irq(this_client->irq);
828 //              ft5x0x_set_reg(FT5X0X_REG_PMODE, PMODE_HIBERNATE);
829 //      disable_irq(this_client->irq);  
830 //      ft5x0x_ts_set_standby(this_client,0);   
831         return;
832 }
833
834 /*
835 static void ft5x0x_ts_early_suspend(struct early_suspend *handler)
836 {
837         int ret;
838         struct ft5x0x_ts_dev *ts;
839         ts =  container_of(handler, struct ft5x0x_ts_dev, early_suspend);
840         key_led_ctrl(0);
841         if(ts->irq)
842         disable_irq_nosync(ts->irq);
843         ret = cancel_work_sync(&ts->pen_event_work);
844         if (ret && ts->irq) // if work was pending disable-count is now 2 
845                 enable_irq(ts->irq);
846         // ==set mode ==, 
847     ft5x0x_set_reg(FT5X0X_REG_PMODE, PMODE_HIBERNATE);
848 }
849 */
850
851 static void ft5x0x_ts_late_resume(struct early_suspend *h)
852 {
853         struct FTS_TS_DATA_T *data = i2c_get_clientdata(this_client);
854         //ft5x0x_ts_set_standby(this_client,1);
855         input_sync(data->input_dev);
856         
857         gpio_direction_output(RK30_PIN4_PD0, 0);
858         msleep(5);
859         gpio_set_value(RK30_PIN4_PD0,GPIO_HIGH);
860         msleep(20);
861         if(this_client->irq)
862         enable_irq(this_client->irq);
863   printk("ft5x0x_ts_late_resume finish\n");
864         return ;
865 }
866
867 /*
868 static void ft5x0x_ts_resume(struct early_suspend *handler)
869 {
870         struct ft5x0x_ts_dev *ts;
871         ts =  container_of(handler, struct ft5x0x_ts_dev, early_suspend);
872         struct ts_event *event = &ts->event;
873         // wake the mode
874         int i =0;
875     for(i=0; i<5; i++) 
876     {
877                 input_mt_slot(ts->input_dev, i);
878                 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, false);
879     }
880     input_sync(ts->input_dev);
881         event->point[i].status == 0;
882         key_led_ctrl(0);
883         gpio_direction_output(RK29_PIN6_PC3, 0);
884         // gpio_set_value(RK29_PIN6_PC3,GPIO_LOW);
885         msleep(5);
886         gpio_set_value(RK29_PIN6_PC3,GPIO_HIGH);
887         msleep(20);
888 #if USE_POINT
889         down_table      = 0;
890         up_table        = ~0;
891 #endif
892         if(ts->irq)
893                 enable_irq(ts->irq);
894 }
895 */
896
897 #else
898 #define ft5x0x_ts_early_suspend       NULL
899 #define ft5x0x_ts_late_resume        NULL
900 #endif
901
902
903 static int fts_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
904 {
905         struct FTS_TS_DATA_T *ft5x0x_ts;
906         struct input_dev *input_dev;
907         int err = 0;
908         int _sui_irq_num;
909         unsigned char reg_value;
910         unsigned char reg_version;
911         int i;
912
913         struct ft5506_platform_data *pdata = client->dev.platform_data;
914
915         printk("[LAIBAO] file(%s), function (%s), --probe start\n", __FILE__, __FUNCTION__);
916         
917         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
918                 err = -ENODEV;
919                 goto exit_check_functionality_failed;
920         }
921
922         if (pdata->init_platform_hw) {
923                 err = pdata->init_platform_hw();
924                 if (err < 0) {
925                         printk("[LAIBAO] init_platform_hw failed\n");
926                         goto exit_init_platform_hw_failed;
927                 }
928         }
929         
930         msleep(200);
931         
932         this_client = client;
933         err = i2c_master_reg8_recv(this_client, FT5X0X_REG_FIRMID, &reg_version, 1, 200*1000);
934         if (err < 0) {
935                 printk("[LAIBAO] Device not found\n");
936                 goto exit_device_not_found;
937         }
938                 
939         client->irq = gpio_to_irq(client->irq);
940         _sui_irq_num = client->irq;
941         
942         ft5x0x_ts = kzalloc(sizeof(*ft5x0x_ts), GFP_KERNEL);
943         if (!ft5x0x_ts)    {
944                 err = -ENOMEM;
945                 goto exit_alloc_data_failed;
946         }
947
948         //this_client = client;
949         ft5x0x_ts->client = client;
950         i2c_set_clientdata(client, ft5x0x_ts);
951
952         INIT_WORK(&ft5x0x_ts->pen_event_work, fts_work_func);
953
954         ft5x0x_ts->ts_workqueue = create_singlethread_workqueue(dev_name(&client->dev));
955         if (!ft5x0x_ts->ts_workqueue) {
956                 err = -ESRCH;
957                 goto exit_create_singlethread;
958         }
959         
960         /***wait CTP to bootup normally***/
961         //msleep(200); 
962
963         //fts_register_read(FT5X0X_REG_FIRMID, &reg_version,1);
964         i2c_master_reg8_recv(this_client, FT5X0X_REG_FIRMID, &reg_version, 1, 200*1000);
965         printk("[LAIBAO] firmware version = 0x%2x\n", reg_version);
966         //fts_register_read(FT5X0X_REG_REPORT_RATE, &reg_value,1);
967         i2c_master_reg8_recv(this_client, FT5X0X_REG_REPORT_RATE, &reg_value, 1, 200*1000);
968         printk("[LAIBAO] firmware report rate = %dHz\n", reg_value*10);
969         //fts_register_read(FT5X0X_REG_THRES, &reg_value,1);
970         i2c_master_reg8_recv(this_client, FT5X0X_REG_THRES, &reg_value, 1, 200*1000);
971         printk("[LAIBAO] firmware threshold = %d\n", reg_value * 4);
972         //fts_register_read(FT5X0X_REG_NOISE_MODE, &reg_value,1);
973         i2c_master_reg8_recv(this_client, FT5X0X_REG_NOISE_MODE, &reg_value, 1, 200*1000);
974         printk("[LAIBAO] nosie mode = 0x%2x\n", reg_value);
975
976 #if 1
977         if (fts_ctpm_get_upg_ver() != reg_version)  
978         {
979                 printk("[LAIBAO] start upgrade new verison 0x%2x\n", fts_ctpm_get_upg_ver());
980                 msleep(400);
981                 err =  fts_ctpm_fw_upgrade_with_i_file();
982                 if (err == 0)
983                 {
984                         printk("[LAIBAO] ugrade successfuly.\n");
985                         msleep(500);
986                         fts_register_read(FT5X0X_REG_FIRMID, &reg_value,1);
987                         printk("FTS_DBG from old version 0x%2x to new version = 0x%2x\n", reg_version, reg_value);
988                 }
989                 else
990                 {
991                         printk("[LAIBAO]  ugrade fail err=%d, line = %d.\n",
992                                         err, __LINE__);
993                 }
994                 msleep(4000);
995         }
996 #endif
997
998
999
1000         
1001         
1002
1003         printk("[LAIBAO]=========================_sui_irq_num = %d   ================\n",_sui_irq_num);
1004         printk("[LAIBAO]=========================client->dev.driver->name = %s  ================\n",client->dev.driver->name);
1005         err = request_irq(_sui_irq_num, fts_ts_irq, GPIOEdgelFalling, client->dev.driver->name, ft5x0x_ts);
1006
1007         if (err < 0) {
1008                 dev_err(&client->dev, "[LAIBAO] ft5x0x_probe: request irq failed\n");
1009                 printk("[LAIBAO]=========================err = %d   ================\n",err);   
1010                 goto exit_irq_request_failed;
1011         }
1012         disable_irq(_sui_irq_num);
1013         
1014         
1015         input_dev = input_allocate_device();
1016         if (!input_dev) {
1017                 err = -ENOMEM;
1018                 dev_err(&client->dev, "[LAIBAO]failed to allocate input device\n");
1019                 goto exit_input_dev_alloc_failed;
1020         }
1021
1022         ft5x0x_ts->input_dev = input_dev;
1023
1024         /***setup coordinate area******/
1025         //set_bit(EV_ABS, input_dev->evbit);
1026         //set_bit(ABS_MT_TOUCH_MAJOR, input_dev->absbit);
1027         //set_bit(ABS_MT_POSITION_X, input_dev->absbit);
1028         //set_bit(ABS_MT_POSITION_Y, input_dev->absbit);
1029         //set_bit(ABS_MT_WIDTH_MAJOR, input_dev->absbit);
1030         
1031 //      input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1032
1033         __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
1034         __set_bit(EV_ABS, input_dev->evbit);
1035
1036         /****** for multi-touch *******/
1037         for (i=0; i<CFG_MAX_POINT_NUM; i++)   
1038                 _st_finger_infos[i].u2_pressure = -1;
1039
1040         input_set_abs_params(input_dev,
1041                         ABS_MT_POSITION_X, 0, SCREEN_MAX_X + SCREEN_BOUNDARY_ADJUST_VALUE, 0, 0);
1042         input_set_abs_params(input_dev,
1043                         ABS_MT_POSITION_Y, 0, SCREEN_MAX_Y + SCREEN_BOUNDARY_ADJUST_VALUE, 0, 0);
1044 //      input_set_abs_params(input_dev,
1045 //                      ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1046         //input_set_abs_params(input_dev,
1047         //              ABS_MT_TRACKING_ID, 0, 30, 0, 0);
1048 //      input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
1049
1050 //      input_set_abs_params(input_dev,
1051 //                           ABS_MT_TRACKING_ID, 0, CFG_MAX_POINT_NUM, 0, 0);
1052
1053
1054         input_mt_init_slots(input_dev, CFG_MAX_POINT_NUM);
1055         input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1056         
1057         /*****setup key code area******/
1058         //set_bit(EV_SYN, input_dev->evbit);
1059         //set_bit(EV_KEY, input_dev->evbit);
1060         //set_bit(BTN_TOUCH, input_dev->keybit);
1061         //input_dev->keycode = tsp_keycodes;
1062         //for(i = 0; i < CFG_NUMOFKEYS; i++)
1063         //{
1064         //      input_set_capability(input_dev, EV_KEY, ((int*)input_dev->keycode)[i]);
1065         //      tsp_keystatus[i] = KEY_RELEASE;
1066         //}
1067
1068         input_dev->name        = FT5X0X_NAME;
1069         input_dev->id.bustype = BUS_I2C;
1070         input_dev->id.vendor = 0xdead;
1071         input_dev->id.product = 0xbeef;
1072         input_dev->id.version = 10427;
1073
1074         err = input_register_device(input_dev);
1075         if (err) {
1076                 dev_err(&client->dev,
1077                                 "fts_ts_probe: failed to register input device: %s\n",
1078                                 dev_name(&client->dev));
1079                 goto exit_input_register_device_failed;
1080         }
1081
1082
1083 #ifdef CONFIG_HAS_EARLYSUSPEND
1084     ft5x0x_ts->early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB + 1;
1085     ft5x0x_ts->early_suspend.suspend = ft5x0x_ts_early_suspend;
1086     ft5x0x_ts->early_suspend.resume = ft5x0x_ts_late_resume;
1087     register_early_suspend(&ft5x0x_ts->early_suspend);
1088 #endif
1089
1090
1091         enable_irq(_sui_irq_num);    
1092         printk("[LAIBAO] file(%s), function (%s), -- end\n", __FILE__, __FUNCTION__);
1093         return 0;
1094
1095 exit_input_register_device_failed:
1096         input_free_device(input_dev);
1097 exit_input_dev_alloc_failed:
1098         free_irq(_sui_irq_num, ft5x0x_ts);
1099 exit_irq_request_failed:
1100         cancel_work_sync(&ft5x0x_ts->pen_event_work);
1101         destroy_workqueue(ft5x0x_ts->ts_workqueue);
1102 exit_create_singlethread:
1103         i2c_set_clientdata(client, NULL);
1104         kfree(ft5x0x_ts);
1105 exit_alloc_data_failed:
1106 exit_device_not_found:
1107         //if (pdata->exit_platform_hw)
1108         //      pdata->exit_platform_hw();
1109
1110 exit_init_platform_hw_failed:
1111 exit_check_functionality_failed:
1112         return err;
1113 }
1114
1115
1116
1117 static int __devexit fts_ts_remove(struct i2c_client *client)
1118 {
1119         struct FTS_TS_DATA_T *ft5x0x_ts;
1120         int _sui_irq_num=client->irq;
1121
1122         ft5x0x_ts = (struct FTS_TS_DATA_T *)i2c_get_clientdata(client);
1123         free_irq(_sui_irq_num, ft5x0x_ts);
1124         input_unregister_device(ft5x0x_ts->input_dev);
1125         kfree(ft5x0x_ts);
1126         cancel_work_sync(&ft5x0x_ts->pen_event_work);
1127         destroy_workqueue(ft5x0x_ts->ts_workqueue);
1128         i2c_set_clientdata(client, NULL);
1129
1130         return 0;
1131 }
1132
1133 static const struct i2c_device_id ft5x0x_ts_id[] = {
1134         {FT5X0X_NAME, 0},
1135         {}
1136 };
1137
1138
1139 MODULE_DEVICE_TABLE(i2c, ft5x0x_ts_id);
1140
1141 static struct i2c_driver fts_ts_driver = {
1142         .probe  = fts_ts_probe,
1143         .remove = fts_ts_remove,//devexit_p(fts_ts_remove),
1144         .id_table = ft5x0x_ts_id,
1145         .driver = {
1146                 .name = FT5X0X_NAME,
1147         },
1148 };
1149
1150 static void __init fts_ts_initasync(void *unused, async_cookie_t cookie)
1151 {
1152         i2c_add_driver(&fts_ts_driver);
1153 }
1154
1155 static int __init fts_ts_init(void)
1156 {
1157         async_schedule(fts_ts_initasync, NULL);
1158         return 0;
1159 }
1160
1161 static void __exit fts_ts_exit(void)
1162 {
1163         i2c_del_driver(&fts_ts_driver);
1164 }
1165
1166 module_init(fts_ts_init);
1167 module_exit(fts_ts_exit);
1168
1169 MODULE_AUTHOR("<duxx@Focaltech-systems.com>");
1170 MODULE_DESCRIPTION("FocalTech ft5x0x TouchScreen driver");
1171 MODULE_LICENSE("GPL");
1172