Merge tag 'v4.4-rc6'
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / ft5x0x.c
1 #include <linux/module.h>
2 #include <linux/delay.h>
3 #include <linux/earlysuspend.h>
4 #include <linux/hrtimer.h>
5 #include <linux/i2c.h>
6 #include <linux/input.h>
7 #include <linux/interrupt.h>
8 #include <linux/io.h>
9 #include <linux/platform_device.h>
10 #include <linux/async.h>
11 #include <mach/gpio.h>
12 #include <mach/iomux.h>
13 #include <linux/irq.h>
14 #include <mach/board.h>
15 #include <linux/kthread.h>
16 #include <linux/slab.h>
17 #include <linux/input/mt.h>
18 #include <linux/regulator/rk29-pwm-regulator.h>
19 #include <linux/regulator/machine.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/regulator/machine.h>
22
23 //#define FT5X0X_DEBUG
24 #ifdef FT5X0X_DEBUG
25 #define DBG(fmt, args...)       printk("*** " fmt, ## args)
26 #else
27 #define DBG(fmt, args...)       do{}while(0)
28 #endif
29
30 #define EV_MENU                                 KEY_MENU
31
32 #define I2C_SPEED 200*1000
33 #define MAX_POINT  10
34
35 #define SCREEN_MAX_X 1920
36 #define SCREEN_MAX_Y 1200
37
38 //#define TP_UPGRADE 1
39
40 #define PRESS_MAX 200
41
42 #define VID_OF          0x51    //OuFei
43 #define VID_MD          0x53    //MuDong
44 #define VID_BYD         0x59
45 #define VID_BM          0x5D    //BaoMing
46 #define VID_YJ          0x80
47 #define VID_DSW         0x8C    //DingShengWei
48 #define VID_YM          0x94    //0xC0
49 static unsigned char g_vid;
50
51 #ifdef CONFIG_HAS_EARLYSUSPEND
52 static struct early_suspend ft5x0x_early_suspend;
53 #endif
54
55 #ifndef TOUCH_EN_LEVEL
56 #define TOUCH_EN_LEVEL GPIO_HIGH
57 #endif
58
59 static int  ft5x0x_probe(struct i2c_client *client, const struct i2c_device_id *id);
60
61 struct ts_event {
62     u16    flag;
63     u16    x;
64     u16    y;
65     u16    pressure;
66     u16    w;
67 };
68 static struct ts_event ts_point[MAX_POINT];
69
70 struct ft5x0x_data
71 {
72         struct i2c_client *client;
73         struct input_dev        *input_dev;
74         int             reset_gpio;
75         int             touch_en_gpio;
76         int             last_point_num;
77         struct work_struct      pen_event_work;
78         struct workqueue_struct *ts_workqueue;
79 };
80
81 struct i2c_client *g_client;
82
83
84 #define FT5X0X_REG_FIRMID         0xA6         /* ***************firmware version **********************/
85
86 /***********************************************************************
87     [function]: 
88                            callback:               write data to ctpm by i2c interface;
89     [parameters]:
90                             buffer[in]:             data buffer;
91                             length[in]:            the length of the data buffer;
92     [return]:
93                             FTS_TRUE:            success;
94                             FTS_FALSE:           fail;
95 ************************************************************************/
96 static bool  i2c_write_interface(u8* pbt_buf, int dw_lenth)
97 {
98     int ret;
99     ret=i2c_master_send(g_client, pbt_buf, dw_lenth);
100     if(ret<=0)
101     {
102         printk("[TSP]i2c_write_interface error line = %d, ret = %d\n", __LINE__, ret);
103         return false;
104     }
105
106     return true;
107 }
108
109 #ifdef TP_UPGRADE
110
111 #if 0
112 #define FTprintk(x...) printk(x)
113 #else
114 #define FTprintk(x...) do{} while(0)
115 #endif
116
117 //#define CONFIG_FT5X0X_MULTITOUCH  1
118 #define FT5306_IIC_SPEED          250*1000    //300*1000
119 #define FT5X0X_REG_THRES          0x80         /* Thresshold, the threshold be low, the sensitivy will be high */
120 #define FT5X0X_REG_REPORT_RATE    0x88         /* **************report rate, in unit of 10Hz **************/
121 #define FT5X0X_REG_PMODE          0xA5         /* Power Consume Mode 0 -- active, 1 -- monitor, 3 -- sleep */    
122 //#define FT5X0X_REG_FIRMID         0xA6         /* ***************firmware version **********************/
123 #define FT5X0X_REG_NOISE_MODE     0xb2         /* to enable or disable power noise, 1 -- enable, 0 -- disable */
124
125 //FT5X0X_REG_PMODE
126 #define PMODE_ACTIVE              0x00
127 #define PMODE_MONITOR             0x01
128 #define PMODE_STANDBY             0x02
129 #define PMODE_HIBERNATE           0x03
130
131
132 struct tp_event {
133         u16     x;
134         u16     y;
135     s16 id;
136         u16     pressure;
137         u8  touch_point;
138         u8  flag;
139 };
140
141 struct ft5x0x_ts_data {
142         struct i2c_client *client;
143         struct input_dev        *input_dev;
144         int    irq;
145         int     (*platform_sleep)(void);
146     int     (*platform_wakeup)(void);
147         struct ts_event         event;
148         struct work_struct      pen_event_work;
149         struct workqueue_struct *ts_workqueue;
150 #ifdef CONFIG_HAS_EARLYSUSPEND
151         struct early_suspend ft5306_early_suspend;
152 #endif
153 };
154
155 /***********************************************************************/
156
157 #define    FTS_PACKET_LENGTH        128
158
159
160 static u8 CTPM_FW[]=
161 {
162 //#include "7022620120726B.txt"
163 //#include "101050-01A-ft_app.i"
164 //#include "ft_app_u30gt2.i"
165 #include "20130305-u30gt2-ft5506_app.i"
166 };
167
168 typedef enum
169 {
170     ERR_OK,
171     ERR_MODE,
172     ERR_READID,
173     ERR_ERASE,
174     ERR_STATUS,
175     ERR_ECC,
176     ERR_DL_ERASE_FAIL,
177     ERR_DL_PROGRAM_FAIL,
178     ERR_DL_VERIFY_FAIL
179 }E_UPGRADE_ERR_TYPE;
180
181 /***********************************************************************/
182
183 /***********************************************************************
184     [function]: 
185                            callback:                send data to ctpm by i2c interface;
186     [parameters]:
187                             txdata[in]:              data buffer which is used to send data;
188                             length[in]:              the length of the data buffer;
189     [return]:
190                             FTS_TRUE:              success;
191                             FTS_FALSE:             fail;
192 ************************************************************************/
193 static int fts_i2c_txdata(u8 *txdata, int length)
194 {
195         int ret;
196
197         struct i2c_msg msg;
198
199       msg.addr = g_client->addr;
200       msg.flags = 0;
201       msg.len = length;
202       msg.buf = txdata;
203       msg.scl_rate = FT5306_IIC_SPEED;
204         ret = i2c_transfer(g_client->adapter, &msg, 1);
205         if (ret < 0)
206                 pr_err("%s i2c write error: %d\n", __func__, ret);
207
208         return ret;
209 }
210
211
212 /***********************************************************************
213     [function]: 
214                            callback:                read register value ftom ctpm by i2c interface;
215     [parameters]:
216                         reg_name[in]:         the register which you want to write;
217                             tx_buf[in]:              buffer which is contained of the writing value;
218     [return]:
219                             FTS_TRUE:              success;
220                             FTS_FALSE:             fail;
221 ************************************************************************/
222 static bool fts_register_write(u8 reg_name, u8* tx_buf)
223 {
224         u8 write_cmd[2] = {0};
225
226         write_cmd[0] = reg_name;
227         write_cmd[1] = *tx_buf;
228
229         /*call the write callback function*/
230         return i2c_write_interface(write_cmd, 2);
231 }
232
233 /***********************************************************************
234 [function]: 
235                       callback:         send a command to ctpm.
236 [parameters]:
237                           btcmd[in]:       command code;
238                           btPara1[in]:     parameter 1;    
239                           btPara2[in]:     parameter 2;    
240                           btPara3[in]:     parameter 3;    
241                       num[in]:         the valid input parameter numbers, 
242                                            if only command code needed and no 
243                                            parameters followed,then the num is 1;    
244 [return]:
245                           FTS_TRUE:      success;
246                           FTS_FALSE:     io fail;
247 ************************************************************************/
248 static bool cmd_write(u8 btcmd,u8 btPara1,u8 btPara2,u8 btPara3,u8 num)
249 {
250     u8 write_cmd[4] = {0};
251
252     write_cmd[0] = btcmd;
253     write_cmd[1] = btPara1;
254     write_cmd[2] = btPara2;
255     write_cmd[3] = btPara3;
256     return i2c_write_interface(write_cmd, num);
257 }
258
259 /***********************************************************************
260     [function]: 
261                            callback:              read data from ctpm by i2c interface;
262     [parameters]:
263                             buffer[in]:            data buffer;
264                             length[in]:           the length of the data buffer;
265     [return]:
266                             FTS_TRUE:            success;
267                             FTS_FALSE:           fail;
268 ************************************************************************/
269 static bool i2c_read_interface(u8* pbt_buf, int dw_lenth)
270 {
271     int ret;
272     
273     ret=i2c_master_recv(g_client, pbt_buf, dw_lenth);
274
275     if(ret<=0)
276     {
277         FTprintk("[TSP]i2c_read_interface error\n");
278         return false;
279     }
280   
281     return true;
282 }
283
284
285 /***********************************************************************
286 [function]: 
287                       callback:         read a byte data  from ctpm;
288 [parameters]:
289                           buffer[in]:       read buffer;
290                           length[in]:      the size of read data;    
291 [return]:
292                           FTS_TRUE:      success;
293                           FTS_FALSE:     io fail;
294 ************************************************************************/
295 static bool byte_read(u8* buffer, int length)
296 {
297     return i2c_read_interface(buffer, length);
298 }
299
300 /***********************************************************************
301 [function]: 
302                       callback:         write a byte data  to ctpm;
303 [parameters]:
304                           buffer[in]:       write buffer;
305                           length[in]:      the size of write data;    
306 [return]:
307                           FTS_TRUE:      success;
308                           FTS_FALSE:     io fail;
309 ************************************************************************/
310 static bool byte_write(u8* buffer, int length)
311 {
312     
313     return i2c_write_interface(buffer, length);
314 }
315
316 /***********************************************************************
317     [function]: 
318                            callback:                 read register value ftom ctpm by i2c interface;
319     [parameters]:
320                         reg_name[in]:         the register which you want to read;
321                             rx_buf[in]:              data buffer which is used to store register value;
322                             rx_length[in]:          the length of the data buffer;
323     [return]:
324                             FTS_TRUE:              success;
325                             FTS_FALSE:             fail;
326 ************************************************************************/
327 static bool fts_register_read(u8 reg_name, u8* rx_buf, int rx_length)
328 {
329         u8 read_cmd[2]= {0};
330         u8 cmd_len      = 0;
331
332         read_cmd[0] = reg_name;
333         cmd_len = 1;    
334
335         /*send register addr*/
336         if(!i2c_write_interface(&read_cmd[0], cmd_len))
337         {
338                 return false;
339         }
340
341         /*call the read callback function to get the register value*/           
342         if(!i2c_read_interface(rx_buf, rx_length))
343         {
344                 return false;
345         }
346         return true;
347 }
348
349
350
351 /***********************************************************************
352 [function]: 
353                         callback:          burn the FW to ctpm.
354 [parameters]:
355                             pbt_buf[in]:     point to Head+FW ;
356                             dw_lenth[in]:   the length of the FW + 6(the Head length);    
357 [return]:
358                             ERR_OK:          no error;
359                             ERR_MODE:      fail to switch to UPDATE mode;
360                             ERR_READID:   read id fail;
361                             ERR_ERASE:     erase chip fail;
362                             ERR_STATUS:   status error;
363                             ERR_ECC:        ecc error.
364 ************************************************************************/
365 E_UPGRADE_ERR_TYPE  fts_ctpm_fw_upgrade(u8* pbt_buf, int dw_lenth)
366 {
367     u8  cmd,reg_val[2] = {0};
368     u8  buffer[2] = {0};
369     u8  packet_buf[FTS_PACKET_LENGTH + 6];
370     u8  auc_i2c_write_buf[10];
371     u8  bt_ecc;
372         
373     int  j,temp,lenght,i_ret,packet_number, i = 0;
374     int  i_is_new_protocol = 0;
375         
376
377     /******write 0xaa to register 0xfc******/
378     cmd=0xaa;
379     fts_register_write(0xfc,&cmd);
380     mdelay(50);
381         
382     /******write 0x55 to register 0xfc******/
383     cmd=0x55;
384     fts_register_write(0xfc,&cmd);
385     FTprintk("[TSP] Step 1: Reset CTPM test\n");
386    
387     mdelay(30);   
388
389
390     /******write 0xaa to register 0xfc******/
391     cmd=0xaa;
392     fts_register_write(0xfc,&cmd);
393     mdelay(50);
394         
395     /******write 0x55 to register 0xfc******/
396     cmd=0x55;
397     fts_register_write(0xfc,&cmd);
398     FTprintk("[TSP] Step 1: Reset CTPM test\n");
399    
400     mdelay(30);   
401
402     /******write 0xaa to register 0xfc******/
403     cmd=0xaa;
404     fts_register_write(0xfc,&cmd);
405     mdelay(50);
406         
407     /******write 0x55 to register 0xfc******/
408     cmd=0x55;
409     fts_register_write(0xfc,&cmd);
410     FTprintk("[TSP] Step 1: Reset CTPM test\n");
411    
412     mdelay(30);   
413
414
415
416
417     /*******Step 2:Enter upgrade mode ****/
418     FTprintk("\n[TSP] Step 2:enter new update mode\n");
419     auc_i2c_write_buf[0] = 0x55;
420     auc_i2c_write_buf[1] = 0xaa;
421     printk("xhc-----------!!!!!!!!!\n");
422     do
423     {
424         i ++;
425         i_ret = fts_i2c_txdata(auc_i2c_write_buf, 2);
426         mdelay(50);
427     }while(i_ret <= 0 && i < 10 );
428
429     if (i > 1)
430     {
431         i_is_new_protocol = 1;
432     }
433
434     /********Step 3:check READ-ID********/        
435     mdelay(100);
436     cmd_write(0x90,0x00,0x00,0x00,4);
437     byte_read(reg_val,2);
438     printk("################## id0=0x%0x, id1=0x%0x\n", reg_val[0],reg_val[1]);
439     if (reg_val[0] == 0x79 && reg_val[1] == 0x6)
440     {
441         FTprintk("[TSP] Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",reg_val[0],reg_val[1]);
442     }
443     else
444     {
445         return ERR_READID;
446         //i_is_new_protocol = 1;
447     }
448     
449
450      /*********Step 4:erase app**********/
451     if (i_is_new_protocol)
452     {
453         cmd_write(0x61,0x00,0x00,0x00,1);
454     }
455     else
456     {
457         cmd_write(0x60,0x00,0x00,0x00,1);
458     }
459     mdelay(1500);
460     FTprintk("[TSP] Step 4: erase. \n");
461
462
463
464     /*Step 5:write firmware(FW) to ctpm flash*/
465     bt_ecc = 0;
466     FTprintk("[TSP] Step 5: start upgrade. \n");
467     dw_lenth = dw_lenth - 8;
468     packet_number = (dw_lenth) / FTS_PACKET_LENGTH;
469     packet_buf[0] = 0xbf;
470     packet_buf[1] = 0x00;
471         FTprintk("[TSP]  packet_number = %d\n",packet_number);
472     for (j=0;j<packet_number;j++)
473     {
474         temp = j * FTS_PACKET_LENGTH;
475         packet_buf[2] = (u8)(temp>>8);
476         packet_buf[3] = (u8)temp;
477         lenght = FTS_PACKET_LENGTH;
478         packet_buf[4] = (u8)(lenght>>8);
479         packet_buf[5] = (u8)lenght;
480
481         for (i=0;i<FTS_PACKET_LENGTH;i++)
482         {
483             packet_buf[6+i] = pbt_buf[j*FTS_PACKET_LENGTH + i]; 
484             bt_ecc ^= packet_buf[6+i];
485         }
486         
487         byte_write(&packet_buf[0],FTS_PACKET_LENGTH + 6);
488         mdelay(FTS_PACKET_LENGTH/6 + 1);
489         if ((j * FTS_PACKET_LENGTH % 1024) == 0)
490         {
491               FTprintk("[TSP] upgrade the 0x%x th byte.\n", ((unsigned int)j) * FTS_PACKET_LENGTH);
492         }
493     }
494
495     if ((dw_lenth) % FTS_PACKET_LENGTH > 0)
496     {
497         temp = packet_number * FTS_PACKET_LENGTH;
498         packet_buf[2] = (u8)(temp>>8);
499         packet_buf[3] = (u8)temp;
500
501         temp = (dw_lenth) % FTS_PACKET_LENGTH;
502         packet_buf[4] = (u8)(temp>>8);
503         packet_buf[5] = (u8)temp;
504
505         for (i=0;i<temp;i++)
506         {
507             packet_buf[6+i] = pbt_buf[ packet_number*FTS_PACKET_LENGTH + i]; 
508             bt_ecc ^= packet_buf[6+i];
509         }
510
511         byte_write(&packet_buf[0],temp+6);    
512         mdelay(20);
513     }
514
515     /***********send the last six byte**********/
516     for (i = 0; i<6; i++)
517     {
518         temp = 0x6ffa + i;
519         packet_buf[2] = (u8)(temp>>8);
520         packet_buf[3] = (u8)temp;
521         temp =1;
522         packet_buf[4] = (u8)(temp>>8);
523         packet_buf[5] = (u8)temp;
524         packet_buf[6] = pbt_buf[ dw_lenth + i]; 
525         bt_ecc ^= packet_buf[6];
526
527         byte_write(&packet_buf[0],7);  
528         mdelay(20);
529     }
530
531     /********send the opration head************/
532     cmd_write(0xcc,0x00,0x00,0x00,1);
533     byte_read(reg_val,1);
534     FTprintk("[TSP] Step 6:  ecc read 0x%x, new firmware 0x%x. \n", reg_val[0], bt_ecc);
535     if(reg_val[0] != bt_ecc)
536     {
537         return ERR_ECC;
538     }
539
540     /*******Step 7: reset the new FW**********/
541     cmd_write(0x07,0x00,0x00,0x00,1);
542         mdelay(100);//100ms     
543         fts_register_read(0xfc, buffer, 1);     
544         if (buffer[0] == 1)
545         {
546         cmd=4;
547         fts_register_write(0xfc, &cmd);
548         mdelay(2500);//2500ms   
549          do     
550          {      
551          fts_register_read(0xfc, buffer, 1);    
552          mdelay(100);//100ms    
553          }while (buffer[0] != 1);                       
554         }
555     return ERR_OK;
556 }
557
558 int fts_ctpm_auto_clb(void)
559 {
560     unsigned char uc_temp;
561     unsigned char i ;
562     u8 reg_val;
563
564     printk("[FTS] start auto CLB.\n");
565     msleep(200);
566     reg_val = 0x40;
567     fts_register_write(0x0, &reg_val);
568     mdelay(100);
569     reg_val = 0x4;
570     fts_register_write(0x02, &reg_val);
571     mdelay(300);
572     
573     for(i=0;i<100;i++)
574     {
575         fts_register_read(0x0, &reg_val, 1);
576         if ( ((reg_val&0x70)>>4) == 0x0)  //return to normal mode, calibration finish
577         {
578             break;
579         }
580         mdelay(200);
581         printk("[FTS] waiting calibration %d\n",i);
582         
583     }
584     printk("[FTS] calibration OK.\n");
585     
586     msleep(300);
587     reg_val = 0x40;
588     fts_register_write(0x0, &reg_val);  //goto factory mode
589     mdelay(100);   //make sure already enter factory mode
590     reg_val = 0x05;
591     fts_register_write(0x2, &reg_val);  //store CLB result
592     mdelay(300);
593     reg_val = 0x0;
594     fts_register_write(0x0, &reg_val); //return to normal mode 
595     msleep(300);
596     printk("[FTS] store CLB result OK.\n");
597     return 0;
598 }
599
600 /***********************************************************************/
601
602 int fts_ctpm_fw_upgrade_with_i_file(void)
603 {
604    u8*     pbt_buf = 0;
605    int i_ret;
606     
607    pbt_buf = CTPM_FW;
608    i_ret =  fts_ctpm_fw_upgrade(pbt_buf,sizeof(CTPM_FW));
609
610         if (i_ret != 0) {
611                 printk("[FTS] upgrade failed i_ret = %d.\n", i_ret);
612         } else {
613                 fts_ctpm_auto_clb();  //start auto CLB
614                 printk("[FTS] upgrade successfully....\n");
615         }
616    
617    return i_ret;
618 }
619
620 /***********************************************************************/
621
622 unsigned char fts_ctpm_get_upg_ver(void)
623 {
624     unsigned int ui_sz;
625         
626     ui_sz = sizeof(CTPM_FW);
627     if (ui_sz > 2)
628     {
629         return CTPM_FW[ui_sz - 2];
630     }
631     else
632         return 0xff; 
633  
634 }
635
636 #endif
637
638
639 /***********************************************************************************************
640 Name    :       ft5x0x_i2c_rxdata 
641
642 Input   :       *rxdata
643                      *length
644
645 Output  :       ret
646
647 function        :       
648
649 ***********************************************************************************************/
650 int ft5x0x_i2c_Read(char * writebuf, int writelen, char *readbuf, int readlen)
651 {
652         int ret;
653
654         if(writelen > 0)
655         {
656                 struct i2c_msg msgs[] = {
657                         {
658                                 .addr   = g_client->addr,
659                                 .flags  = 0,
660                                 .len    = writelen,
661                                 .buf    = writebuf,
662                                 .scl_rate = I2C_SPEED,
663                         },
664                         {
665                                 .addr   = g_client->addr,
666                                 .flags  = I2C_M_RD,
667                                 .len    = readlen,
668                                 .buf    = readbuf,
669                                 .scl_rate = I2C_SPEED,
670                         },
671                 };
672                 ret = i2c_transfer(g_client->adapter, msgs, 2);
673                 if (ret < 0)
674                         DBG("msg %s i2c read error: %d\n", __func__, ret);
675         }
676         else
677         {
678                 struct i2c_msg msgs[] = {
679                         {
680                                 .addr   = g_client->addr,
681                                 .flags  = I2C_M_RD,
682                                 .len    = readlen,
683                                 .buf    = readbuf,
684                                 .scl_rate = I2C_SPEED,
685                         },
686                 };
687                 ret = i2c_transfer(g_client->adapter, msgs, 1);
688                 if (ret < 0)
689                         DBG("msg %s i2c read error: %d\n", __func__, ret);
690         }
691         return ret;
692 }EXPORT_SYMBOL(ft5x0x_i2c_Read);
693 /***********************************************************************************************
694 Name    :        ft5x0x_i2c_Write
695
696 Input   :       
697                      
698
699 Output  :0-write success        
700                 other-error code        
701 function        :       write data by i2c 
702
703 ***********************************************************************************************/
704 int ft5x0x_i2c_Write(char *writebuf, int writelen)
705 {
706         int ret;
707
708         struct i2c_msg msg[] = {
709                 {
710                         .addr   = g_client->addr,
711                         .flags  = 0,
712                         .len    = writelen,
713                         .buf    = writebuf,
714                         .scl_rate = I2C_SPEED,
715                 },
716         };
717
718         ret = i2c_transfer(g_client->adapter, msg, 1);
719         if (ret < 0)
720                 DBG("%s i2c write error: %d\n", __func__, ret);
721
722         return ret;
723 }EXPORT_SYMBOL(ft5x0x_i2c_Write);
724
725 int ft5x0x_rx_data(struct i2c_client *client, char *rxData, int length)
726 {
727         int ret = 0;
728         char reg = rxData[0];
729         ret = i2c_master_reg8_recv(client, reg, rxData, length, I2C_SPEED);
730         return (ret > 0)? 0 : ret;
731 }
732
733 static int ft5x0x_tx_data(struct i2c_client *client, char *txData, int length)
734 {
735         int ret = 0;
736         char reg = txData[0];
737         ret = i2c_master_reg8_send(client, reg, &txData[1], length-1, I2C_SPEED);
738         return (ret > 0)? 0 : ret;
739 }
740
741 char ft5x0x_read_reg(struct i2c_client *client, int addr)
742 {
743         char tmp;
744         int ret = 0;
745
746         tmp = addr;
747         ret = ft5x0x_rx_data(client, &tmp, 1);
748         if (ret < 0) {
749                 return ret;
750         }
751         return tmp;
752 }
753
754 int ft5x0x_write_reg(struct i2c_client *client,int addr,int value)
755 {
756         char buffer[3];
757         int ret = 0;
758
759         buffer[0] = addr;
760         buffer[1] = value;
761         ret = ft5x0x_tx_data(client, &buffer[0], 2);
762         return ret;
763 }
764
765 static void ft5x0x_power_en(struct ft5x0x_data *tsdata, int on)
766 {
767 #if defined (TOUCH_POWER_PIN)
768         if (on) {
769                 gpio_direction_output(tsdata->touch_en_gpio, TOUCH_EN_LEVEL);
770                 gpio_set_value(tsdata->touch_en_gpio, TOUCH_EN_LEVEL);
771                 mdelay(10);
772         } else {
773                 gpio_direction_output(tsdata->touch_en_gpio, !TOUCH_EN_LEVEL);
774                 gpio_set_value(tsdata->touch_en_gpio, !TOUCH_EN_LEVEL);
775                 mdelay(10);
776         }
777 #endif
778        if( tsdata->reset_gpio <= 0 ){
779                struct regulator *vcc_tp;
780                vcc_tp = regulator_get(NULL, "vaux33");   // vcc28_cif
781                if (vcc_tp == NULL || IS_ERR(vcc_tp) ){
782                     printk(">>>> get cif vaux33 ldo failed!\n");
783                     return;
784                 }
785
786                  if( on ){ //turn on
787                         regulator_set_voltage(vcc_tp, 3300000, 3300000);
788                           regulator_enable(vcc_tp);
789                           printk(" %s set  vpll vcc28_cif=%dmV end\n", __func__, regulator_get_voltage(vcc_tp));
790                           regulator_put(vcc_tp);
791                  }else{   //turn off
792                         while(regulator_is_enabled(vcc_tp)>0){
793                                        regulator_disable(vcc_tp);
794                           }
795                           printk(" %s regulator disable vcc tp \n",__func__);
796                           regulator_put(vcc_tp);
797                  }
798          }
799
800
801 }
802
803 static void ft5x0x_chip_reset(struct ft5x0x_data *tsdata)
804 {
805     gpio_direction_output(tsdata->reset_gpio, 0);
806     gpio_set_value(tsdata->reset_gpio, 1);
807         mdelay(20);
808     gpio_set_value(tsdata->reset_gpio, 0);
809         mdelay(20);
810     gpio_set_value(tsdata->reset_gpio, 1);
811 }
812
813
814
815 static int ft_cmd_write(unsigned char btcmd, unsigned char btPara1, unsigned char btPara2,
816                 unsigned char btPara3, int num)
817 {
818     unsigned char write_cmd[4] = {0};
819
820     write_cmd[0] = btcmd;
821     write_cmd[1] = btPara1;
822     write_cmd[2] = btPara2;
823     write_cmd[3] = btPara3;
824     // printk("&write_cmd=0x%08x, &write_cmd[0]=0x%08x\n", &write_cmd, &write_cmd[0]);
825     return i2c_write_interface(write_cmd, num);
826 }
827
828
829 static int ft5x0x_chip_init(struct i2c_client * client)
830 {
831         int ret = 0;
832         int w_value;
833         char r_value;
834         int err = -1;
835         int reg;
836         int i = 0, flag = 1;
837         struct ft5x0x_data *tsdata = i2c_get_clientdata(client);
838
839        if( tsdata->reset_gpio > 0 ){
840                 gpio_free(tsdata->reset_gpio);
841                 err = gpio_request(tsdata->reset_gpio, "ft5x0x rst");
842                 if (err) {
843                         printk( "failed to request ft5x0x reset GPIO%d\n", tsdata->reset_gpio);
844                         goto exit_alloc_gpio_rst_failed;
845                 }
846        }
847         
848 #if defined (TOUCH_POWER_PIN)
849 #if defined (TOUCH_POWER_MUX_NAME)
850     rk29_mux_api_set(TOUCH_POWER_MUX_NAME, TOUCH_POWER_MUX_MODE_GPIO);
851 #endif
852         gpio_free(tsdata->touch_en_gpio);
853         err = gpio_request(tsdata->touch_en_gpio, "ft5x0x power enable");
854         if (err) {
855                 DBG( "failed to request ft5x0x power enable GPIO%d\n", tsdata->touch_en_gpio);
856                 goto exit_alloc_gpio_power_failed;
857         }
858 #endif
859
860         ft5x0x_power_en(tsdata, 0);
861         mdelay(100);
862         ft5x0x_chip_reset(tsdata);
863         ft5x0x_power_en(tsdata, 1);
864         mdelay(500);
865         ft_cmd_write(0x07,0x00,0x00,0x00,1);
866         mdelay(10);
867
868 #if 1
869         while (1) {
870                 reg = 0x88;
871                 w_value = 7; 
872                 ret = ft5x0x_write_reg(client, reg, w_value);    /* adjust frequency 70Hz */
873                 if (ret < 0) {
874                         printk(KERN_ERR "ft5x0x i2c txdata failed\n");
875                         //goto out;
876                 }
877
878                 r_value = ft5x0x_read_reg(client, reg);
879                 if (ret < 0) {
880                         printk(KERN_ERR "ft5x0x i2c rxdata failed\n");
881                         //goto out;
882                 }
883                 printk("r_value = %d\n, i = %d, flag = %d", r_value, i, flag);
884                 i++;
885
886                 if (w_value != r_value) {
887                         ret = -1;
888                         flag = 0;
889                         if (i > 10) { /* test 5 count */
890                                 break;
891                         }
892                 } else {
893                         ret = 0;
894                         break;
895                 }
896         }
897
898       if( ret == -1)
899           return ret;
900
901 #endif
902         ret = ft5x0x_read_reg(client, 0xA8);//read touchpad ID for adjust touchkey place
903         if (ret < 0) {
904                 printk(KERN_ERR "ft5x0x i2c rxdata failed\n");
905                 //goto out;
906         }
907         printk("ft5406 g_vid = 0x%X\n", ret);
908         g_vid = ret;
909
910         return ret;
911
912 #if defined (TOUCH_POWER_PIN)
913 exit_alloc_gpio_power_failed:
914         gpio_free(tsdata->touch_en_gpio);
915 #endif
916 exit_alloc_gpio_rst_failed:
917     gpio_free(tsdata->reset_gpio);
918         printk("%s error\n",__FUNCTION__);
919         return err;
920 }
921
922 static void key_led_ctrl(int on)
923 {
924 #ifdef TOUCH_KEY_LED
925         gpio_set_value(TOUCH_KEY_LED, on);
926 #endif
927 }
928
929 //static int g_screen_key=0;
930
931 static int ft5x0x_process_points(struct ft5x0x_data *data)
932 {
933         struct i2c_client *client = data->client;
934         u8 start_reg = 0x0;
935         u8 buf[64] = {0};
936         int ret = -1;
937         int status = 0, id, x, y, touch_num;
938         int offset, i;
939         // int static w, p;
940         // int back_press = 0, search_press=0, menu_press=0, home_press=0;
941         int points;
942
943         start_reg = 0;
944         buf[0] = start_reg;
945
946         //printk("ft5406 g_vid = 0x%X\n", g_vid);
947         if (MAX_POINT == 5) {
948                 ret = ft5x0x_rx_data(client, buf, 31);
949         } else {
950                 ret = ft5x0x_rx_data(client, buf, 62);
951         }
952
953     if (ret < 0) {
954                 printk("%s read_data i2c_rxdata failed: %d\n", __func__, ret);
955                 return ret;
956         }
957
958
959         if (MAX_POINT == 5) {
960                 touch_num = buf[2] & 0x07;
961         } else {
962                 touch_num = buf[2] & 0x0f;
963         }
964
965         if (touch_num == 0) {
966                 for (i = 0; i < MAX_POINT; i++) {
967                         input_mt_slot(data->input_dev, i);
968                         input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER, false);
969                 }
970
971                 input_sync(data->input_dev);
972                 DBG("release all points!!!!!!\n");
973                 return 0;
974         }
975
976         points = touch_num;
977         if (data->last_point_num > touch_num) {
978                 touch_num = data->last_point_num;
979         }
980         data->last_point_num = points;
981
982         offset = 0;
983     for (i = 0; i < touch_num; i++) {        
984                 id = buf[5 + offset] >> 4;
985         status = buf[3  + offset] >> 6;
986         x = (s16)(buf[3 + offset] & 0x0F) << 8 | (s16)buf[4 + offset];
987         y = (s16)(buf[5 + offset] & 0x0F) << 8 | (s16)buf[6 + offset];
988
989         //p = buf[7 + offset];
990         //w = buf[8 + offset];
991
992         offset += 6;
993                 
994         //printk("%d-%d(%d,%d)%d-%d\n", id, status, x, y, p, w);
995                 DBG("TOUCH_NO=%d: ID=%d,(X=%d,Y=%d), status=%d, pressure=%d, w=%d\n", i, id, x, y, status, 0, 0);
996
997                 if (x < (SCREEN_MAX_X + 10)) {
998                         if (status == 1) {
999                                 input_mt_slot(data->input_dev, id);
1000                                 input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER, false);
1001                         } else {
1002                                 input_mt_slot(data->input_dev, id);
1003                                 input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER, true);
1004                                 input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, 200);
1005                                 input_report_abs(data->input_dev, ABS_MT_POSITION_X, x);
1006                                 input_report_abs(data->input_dev, ABS_MT_POSITION_Y, y);
1007                         }
1008                 } else {
1009                 }
1010         }
1011
1012         input_sync(data->input_dev);
1013
1014         return 0;
1015 }
1016
1017 static void  ft5x0x_delaywork_func(struct work_struct *work)
1018 {
1019         struct ft5x0x_data *ft5x0x = container_of(work, struct ft5x0x_data, pen_event_work);
1020         struct i2c_client *client = ft5x0x->client;
1021
1022         ft5x0x_process_points(ft5x0x);
1023         enable_irq(client->irq);                
1024 }
1025
1026 static irqreturn_t ft5x0x_interrupt(int irq, void *handle)
1027 {
1028         struct ft5x0x_data *ft5x0x_ts = handle;
1029
1030         //printk("Enter:%s %d\n",__FUNCTION__,__LINE__);
1031         disable_irq_nosync(irq);
1032         //if (!work_pending(&ft5x0x_ts->pen_event_work)) {
1033                 queue_work(ft5x0x_ts->ts_workqueue, &ft5x0x_ts->pen_event_work);
1034         //}
1035         return IRQ_HANDLED;
1036 }
1037
1038
1039 static int ft5x0x_remove(struct i2c_client *client)
1040 {
1041         struct ft5x0x_data *ft5x0x = i2c_get_clientdata(client);
1042         
1043     input_unregister_device(ft5x0x->input_dev);
1044     input_free_device(ft5x0x->input_dev);
1045     free_irq(client->irq, ft5x0x);
1046     kfree(ft5x0x); 
1047 #ifdef CONFIG_HAS_EARLYSUSPEND
1048     unregister_early_suspend(&ft5x0x_early_suspend);
1049 #endif      
1050         return 0;
1051 }
1052
1053 #ifdef CONFIG_HAS_EARLYSUSPEND
1054 static void ft5x0x_suspend(struct early_suspend *h)
1055 {
1056         int err;
1057         int w_value;
1058         int reg;
1059         struct ft5x0x_data *ft5x0x = i2c_get_clientdata(g_client);
1060
1061         printk("==ft5x0x_ts_suspend=\n");
1062         key_led_ctrl(0);
1063
1064 #if 1
1065         w_value = 3;
1066         reg = 0xa5;
1067         err = ft5x0x_write_reg(g_client, reg, w_value);   /* enter sleep mode */
1068         if (err < 0) {
1069                 printk("ft5x0x enter sleep mode failed\n");
1070         }
1071 #endif
1072         disable_irq(g_client->irq);     
1073         if( ft5x0x->reset_gpio <= 0 ){
1074             ft5x0x_power_en(ft5x0x, 0);
1075         }
1076 }
1077
1078 static void ft5x0x_resume(struct early_suspend *h)
1079 {
1080         struct ft5x0x_data *ft5x0x = i2c_get_clientdata(g_client);
1081
1082         key_led_ctrl(0);
1083
1084         printk("==ft5x0x_ts_resume=\n");
1085         if( ft5x0x->reset_gpio > 0 ){
1086             ft5x0x_chip_reset(ft5x0x);
1087         }else{
1088             ft5x0x_power_en(ft5x0x, 1);
1089         }
1090
1091         mdelay(100);
1092
1093         enable_irq(g_client->irq);              
1094 }
1095 #else
1096 static int ft5x0x_suspend(struct i2c_client *client, pm_message_t mesg)
1097 {
1098         return 0;
1099 }
1100 static int ft5x0x_resume(struct i2c_client *client)
1101 {
1102         return 0;
1103 }
1104 #endif
1105
1106 static const struct i2c_device_id ft5x0x_id[] = {
1107                 {"ft5x0x_ts", 0},
1108                 { }
1109 };
1110
1111 MODULE_DEVICE_TABLE(i2c, ft5x0x_id);
1112
1113 static struct i2c_driver ft5x0x_driver = {
1114         .driver = {
1115                 .name = "ft5x0x_ts",
1116             },
1117         .id_table       = ft5x0x_id,
1118         .probe          = ft5x0x_probe,
1119         .remove         = __devexit_p(ft5x0x_remove),
1120 #ifndef CONFIG_HAS_EARLYSUSPEND 
1121         .suspend = &ft5x0x_suspend,
1122         .resume = &ft5x0x_resume,
1123 #endif  
1124 };
1125
1126 static int ft5x0x_client_init(struct i2c_client *client)
1127 {
1128         struct ft5x0x_data *tsdata = i2c_get_clientdata(client);
1129         int ret = 0;
1130
1131         DBG("gpio_to_irq(%d) is %d\n", client->irq, gpio_to_irq(client->irq));
1132         if ( !gpio_is_valid(client->irq)) {
1133                 DBG("+++++++++++gpio_is_invalid\n");
1134                 return -EINVAL;
1135         }
1136
1137         gpio_free(client->irq);
1138         ret = gpio_request(client->irq, "ft5x0x_int");
1139         if (ret) {
1140                 DBG( "failed to request ft5x0x GPIO%d\n", gpio_to_irq(client->irq));
1141                 return ret;
1142         }
1143
1144     ret = gpio_direction_input(client->irq);
1145     if (ret) {
1146         DBG("failed to set ft5x0x gpio input\n");
1147                 return ret;
1148     }
1149
1150         gpio_pull_updown(client->irq, GPIOPullUp);
1151         client->irq = gpio_to_irq(client->irq);
1152         //ft5x0x->irq = client->irq;
1153         ret = request_irq(client->irq, ft5x0x_interrupt, IRQF_TRIGGER_FALLING, client->dev.driver->name, tsdata);
1154         DBG("request irq is %d,ret is 0x%x\n", client->irq, ret);
1155         if (ret ) {
1156                 DBG(KERN_ERR "ft5x0x_client_init: request irq failed,ret is %d\n", ret);
1157         return ret;
1158         }
1159         disable_irq(client->irq);
1160
1161         return 0;
1162 }
1163
1164 static int  ft5x0x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1165 {
1166         struct ft5x0x_data *ft5x0x_ts;
1167         struct ts_hw_data *pdata = client->dev.platform_data;
1168         int err = 0;
1169         // int i;
1170         
1171         unsigned char reg_version;
1172 #ifdef TP_UPGRADE
1173         unsigned char reg_value;
1174 #endif
1175
1176         printk("%s enter\n",__FUNCTION__);
1177         ft5x0x_ts = kzalloc(sizeof(struct ft5x0x_data), GFP_KERNEL);
1178         if (!ft5x0x_ts) {
1179                 DBG("[ft5x0x]:alloc data failed.\n");
1180                 err = -ENOMEM;
1181                 goto exit_alloc_data_failed;
1182         }
1183     
1184     memset(ts_point, 0x0, sizeof(struct ts_event) * MAX_POINT);
1185
1186         g_client = client;
1187         ft5x0x_ts->client = client;
1188         ft5x0x_ts->last_point_num = 0;
1189         ft5x0x_ts->reset_gpio = pdata->reset_gpio;
1190         ft5x0x_ts->touch_en_gpio = pdata->touch_en_gpio;
1191         i2c_set_clientdata(client, ft5x0x_ts);
1192
1193         err = ft5x0x_chip_init(client);
1194         if (err < 0) {
1195                 printk(KERN_ERR
1196                        "ft5x0x_probe: ft5x0x chip init failed\n");
1197                 goto exit_request_gpio_irq_failed;
1198         }
1199
1200         err = ft5x0x_client_init(client);
1201         if (err < 0) {
1202                 printk(KERN_ERR
1203                        "ft5x0x_probe: ft5x0x_client_init failed\n");
1204                 goto exit_request_gpio_irq_failed;
1205         }
1206                 
1207         ft5x0x_ts->input_dev = input_allocate_device();
1208         if (!ft5x0x_ts->input_dev) {
1209                 err = -ENOMEM;
1210                 printk(KERN_ERR
1211                        "ft5x0x_probe: Failed to allocate input device\n");
1212                 goto exit_input_allocate_device_failed;
1213         }
1214
1215         ft5x0x_ts->input_dev->name = "ft5x0x-ts";
1216         ft5x0x_ts->input_dev->dev.parent = &client->dev;
1217
1218         err = input_register_device(ft5x0x_ts->input_dev);
1219         if (err < 0) {
1220                 printk(KERN_ERR
1221                        "ft5x0x_probe: Unable to register input device: %s\n",
1222                        ft5x0x_ts->input_dev->name);
1223                 goto exit_input_register_device_failed;
1224         }
1225
1226         INIT_WORK(&ft5x0x_ts->pen_event_work, ft5x0x_delaywork_func);
1227         ft5x0x_ts->ts_workqueue = create_singlethread_workqueue("ft5x0x_ts");
1228         if (!ft5x0x_ts->ts_workqueue) {
1229                 err = -ESRCH;
1230                 goto exit_request_gpio_irq_failed;
1231         }
1232
1233
1234         __set_bit(EV_SYN, ft5x0x_ts->input_dev->evbit);
1235         __set_bit(EV_KEY, ft5x0x_ts->input_dev->evbit);
1236         __set_bit(EV_ABS, ft5x0x_ts->input_dev->evbit);
1237         __set_bit(INPUT_PROP_DIRECT, ft5x0x_ts->input_dev->propbit);
1238
1239         input_mt_init_slots(ft5x0x_ts->input_dev, MAX_POINT);
1240         input_set_abs_params(ft5x0x_ts->input_dev, ABS_MT_POSITION_X, 0, SCREEN_MAX_X, 0, 0);
1241         input_set_abs_params(ft5x0x_ts->input_dev, ABS_MT_POSITION_Y, 0, SCREEN_MAX_Y, 0, 0);
1242         input_set_abs_params(ft5x0x_ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, PRESS_MAX, 0, 0);
1243
1244         /***wait CTP to bootup normally***/
1245         msleep(200); 
1246         reg_version = FT5X0X_REG_FIRMID;        
1247         ft5x0x_rx_data(client, &reg_version,1);
1248         printk("------------------cdy == [TSP] firmware tpversion = 0x%2x-------------------------\n", reg_version);
1249         
1250         
1251 #ifdef TP_UPGRADE       //write firmware 
1252         fts_register_read(FT5X0X_REG_FIRMID, &reg_version,1);
1253         FTprintk("[TSP] firmware version = 0x%2x\n", reg_version);
1254         if (fts_ctpm_get_upg_ver() != reg_version)  
1255         {
1256           FTprintk("[TSP] start upgrade new verison 0x%2x\n", fts_ctpm_get_upg_ver());
1257           msleep(200);
1258           err =  fts_ctpm_fw_upgrade_with_i_file();
1259           if (err == 0)
1260           {
1261                   FTprintk("[TSP] ugrade successfuly.\n");
1262                   msleep(300);
1263                   fts_register_read(FT5X0X_REG_FIRMID, &reg_value,1);
1264                   FTprintk("FTS_DBG from old version 0x%2x to new version = 0x%2x\n", reg_version, reg_value);
1265           }
1266           else
1267           {
1268                   FTprintk("[TSP]  ugrade fail err=%d, line = %d.\n",err, __LINE__);
1269           }
1270           msleep(4000);
1271         }
1272 #endif
1273
1274
1275 #ifdef CONFIG_HAS_EARLYSUSPEND
1276     ft5x0x_early_suspend.suspend = ft5x0x_suspend;
1277     ft5x0x_early_suspend.resume = ft5x0x_resume;
1278     ft5x0x_early_suspend.level = 0x2;
1279     register_early_suspend(&ft5x0x_early_suspend);
1280 #endif
1281
1282
1283
1284         enable_irq(client->irq);
1285         return 0;
1286
1287 exit_input_register_device_failed:
1288         input_free_device(ft5x0x_ts->input_dev);
1289 exit_input_allocate_device_failed:
1290     free_irq(client->irq, ft5x0x_ts);
1291 exit_request_gpio_irq_failed:
1292         kfree(ft5x0x_ts);       
1293 #if defined (TOUCH_POWER_PIN)
1294 exit_alloc_gpio_power_failed:
1295         gpio_free(ft5x0x_ts->touch_en_gpio);
1296 #endif
1297 #if 0
1298 exit_alloc_gpio_rst_failed:
1299     gpio_free(ft5x0x_ts->reset_gpio);
1300 #endif
1301 exit_alloc_data_failed:
1302         printk("%s error\n",__FUNCTION__);
1303         return err;
1304 }
1305
1306 static void __init ft5x0x_init_async(void *unused, async_cookie_t cookie)
1307 {
1308         i2c_add_driver(&ft5x0x_driver);
1309 }
1310
1311 static int __init ft5x0x_mod_init(void)
1312 {
1313         printk("ft5x0x module init\n");
1314         async_schedule(ft5x0x_init_async, NULL);
1315         return 0;
1316 }
1317
1318 static void __exit ft5x0x_mod_exit(void)
1319 {
1320         i2c_del_driver(&ft5x0x_driver);
1321 }
1322
1323 module_init(ft5x0x_mod_init);
1324 module_exit(ft5x0x_mod_exit);
1325
1326 MODULE_DESCRIPTION("ft5406 touch driver");
1327 MODULE_AUTHOR("zqqu<zqqu@yifangdigital.com>");
1328 MODULE_LICENSE("GPL");
1329