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