K97 项目相关修改
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / ft5306_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/input.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
26 #include <linux/platform_device.h>
27 #include <linux/spi/spi.h>
28 #include <linux/slab.h>
29 #include <linux/fcntl.h>
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/timer.h>
33 #include <linux/jiffies.h>
34 #include <linux/miscdevice.h>
35 #include <linux/types.h>
36 #include <linux/io.h>
37 #include <linux/delay.h>
38 #include <linux/ioport.h>
39 #include <linux/input-polldev.h>
40 #include <linux/i2c.h>
41 #include <linux/workqueue.h>
42 #ifdef CONFIG_ANDROID_POWER
43 #include <linux/android_power.h>
44 #endif
45 #include <mach/hardware.h>
46 #include <asm/setup.h>
47 #include <asm/mach-types.h>
48 #include <asm/mach/arch.h>
49 #include <asm/mach/map.h>
50 #include <asm/mach/flash.h>
51 #include <asm/hardware/gic.h>
52 #include <mach/iomux.h>
53 #include <mach/gpio.h>
54 #include <mach/irqs.h>
55 #include <mach/rk29_iomap.h>
56 #include <mach/board.h>
57 #include <mach/rk29_nand.h>
58 #include <mach/rk29_camera.h>                          /* ddl@rock-chips.com : camera support */
59 #include <media/soc_camera.h>                               /* ddl@rock-chips.com : camera support */
60 #include <mach/vpu_mem.h>
61 #include <mach/sram.h>
62 #ifdef CONFIG_HAS_EARLYSUSPEND
63 #include <linux/earlysuspend.h>
64 #endif
65 #include <linux/input/mt.h>
66
67
68 #if 0
69 #define FTprintk(x...) printk(x)
70 #else
71 #define FTprintk(x...) do{} while(0)
72 #endif
73
74 #define CONFIG_FT5X0X_MULTITOUCH  1
75 #define MAX_POINT                 5
76 #define FT5306_IIC_SPEED          400*1000    //300*1000
77 #define TOUCH_RESET_PIN           RK29_PIN6_PC3
78 #define FT5X0X_REG_THRES          0x80         /* Thresshold, the threshold be low, the sensitivy will be high */
79 #define FT5X0X_REG_REPORT_RATE    0x88         /* **************report rate, in unit of 10Hz **************/
80 #define FT5X0X_REG_PMODE          0xA5         /* Power Consume Mode 0 -- active, 1 -- monitor, 3 -- sleep */    
81 #define FT5X0X_REG_FIRMID         0xA6         /* ***************firmware version **********************/
82 #define FT5X0X_REG_NOISE_MODE     0xb2         /* to enable or disable power noise, 1 -- enable, 0 -- disable */
83 #define SCREEN_MAX_X              480
84 #define SCREEN_MAX_Y              800
85 #define PRESS_MAX                 255
86 #define FT5X0X_NAME                   "ft5x0x_ts"//"synaptics_i2c_rmi"//"synaptics-rmi-ts"// 
87 #define TOUCH_MAJOR_MAX           200
88 #define WIDTH_MAJOR_MAX           200
89 //FT5X0X_REG_PMODE
90 #define PMODE_ACTIVE              0x00
91 #define PMODE_MONITOR             0x01
92 #define PMODE_STANDBY             0x02
93 #define PMODE_HIBERNATE           0x03
94
95
96 struct ts_event {
97         u16     x1;
98         u16     y1;
99         u16     x2;
100         u16     y2;
101         u16     x3;
102         u16     y3;
103         u16     x4;
104         u16     y4;
105         u16     x5;
106         u16     y5;
107         u16     pressure;
108     s16  touch_ID1;
109         s16  touch_ID2;
110     s16  touch_ID3;
111     s16  touch_ID4;
112         s16  touch_ID5;
113         u8   touch_point;
114         u8   status;
115 };
116
117 struct tp_event {
118         u16     x;
119         u16     y;
120     s16 id;
121         u16     pressure;
122         u8  touch_point;
123         u8  flag;
124 };
125
126 struct ft5x0x_ts_data {
127         struct i2c_client *client;
128         struct input_dev        *input_dev;
129         int    irq;
130         int     (*platform_sleep)(void);
131     int     (*platform_wakeup)(void);
132         struct ts_event         event;
133         struct work_struct      pen_event_work;
134         struct workqueue_struct *ts_workqueue;
135 #ifdef CONFIG_HAS_EARLYSUSPEND
136         struct early_suspend ft5306_early_suspend;
137 #endif
138 };
139 static struct i2c_client *this_client;
140
141 /***********************************************************************/
142
143 #define    FTS_PACKET_LENGTH        128
144
145
146 static u8 CTPM_FW[]=
147 {
148 #include "ft_app_5306.i"
149 };
150
151 typedef enum
152 {
153     ERR_OK,
154     ERR_MODE,
155     ERR_READID,
156     ERR_ERASE,
157     ERR_STATUS,
158     ERR_ECC,
159     ERR_DL_ERASE_FAIL,
160     ERR_DL_PROGRAM_FAIL,
161     ERR_DL_VERIFY_FAIL
162 }E_UPGRADE_ERR_TYPE;
163
164 /***********************************************************************/
165
166 /***********************************************************************
167     [function]: 
168                            callback:                send data to ctpm by i2c interface;
169     [parameters]:
170                             txdata[in]:              data buffer which is used to send data;
171                             length[in]:              the length of the data buffer;
172     [return]:
173                             FTS_TRUE:              success;
174                             FTS_FALSE:             fail;
175 ************************************************************************/
176 static int fts_i2c_txdata(u8 *txdata, int length)
177 {
178         int ret;
179
180         struct i2c_msg msg;
181
182       msg.addr = this_client->addr;
183       msg.flags = 0;
184       msg.len = length;
185       msg.buf = txdata;
186       msg.scl_rate = FT5306_IIC_SPEED;
187         ret = i2c_transfer(this_client->adapter, &msg, 1);
188         if (ret < 0)
189                 pr_err("%s i2c write error: %d\n", __func__, ret);
190
191         return ret;
192 }
193
194 /***********************************************************************
195     [function]: 
196                            callback:               write data to ctpm by i2c interface;
197     [parameters]:
198                             buffer[in]:             data buffer;
199                             length[in]:            the length of the data buffer;
200     [return]:
201                             FTS_TRUE:            success;
202                             FTS_FALSE:           fail;
203 ************************************************************************/
204 static bool  i2c_write_interface(u8* pbt_buf, int dw_lenth)
205 {
206     int ret;
207     ret=i2c_master_send(this_client, pbt_buf, dw_lenth);
208     if(ret<=0)
209     {
210         FTprintk("[TSP]i2c_write_interface error line = %d, ret = %d\n", __LINE__, ret);
211         return false;
212     }
213
214     return true;
215 }
216
217 /***********************************************************************
218     [function]: 
219                            callback:                read register value ftom ctpm by i2c interface;
220     [parameters]:
221                         reg_name[in]:         the register which you want to write;
222                             tx_buf[in]:              buffer which is contained of the writing value;
223     [return]:
224                             FTS_TRUE:              success;
225                             FTS_FALSE:             fail;
226 ************************************************************************/
227 static bool fts_register_write(u8 reg_name, u8* tx_buf)
228 {
229         u8 write_cmd[2] = {0};
230
231         write_cmd[0] = reg_name;
232         write_cmd[1] = *tx_buf;
233
234         /*call the write callback function*/
235         return i2c_write_interface(write_cmd, 2);
236 }
237
238 /***********************************************************************
239 [function]: 
240                       callback:         send a command to ctpm.
241 [parameters]:
242                           btcmd[in]:       command code;
243                           btPara1[in]:     parameter 1;    
244                           btPara2[in]:     parameter 2;    
245                           btPara3[in]:     parameter 3;    
246                       num[in]:         the valid input parameter numbers, 
247                                            if only command code needed and no 
248                                            parameters followed,then the num is 1;    
249 [return]:
250                           FTS_TRUE:      success;
251                           FTS_FALSE:     io fail;
252 ************************************************************************/
253 static bool cmd_write(u8 btcmd,u8 btPara1,u8 btPara2,u8 btPara3,u8 num)
254 {
255     u8 write_cmd[4] = {0};
256
257     write_cmd[0] = btcmd;
258     write_cmd[1] = btPara1;
259     write_cmd[2] = btPara2;
260     write_cmd[3] = btPara3;
261     return i2c_write_interface(write_cmd, num);
262 }
263
264 /***********************************************************************
265     [function]: 
266                            callback:              read data from ctpm by i2c interface;
267     [parameters]:
268                             buffer[in]:            data buffer;
269                             length[in]:           the length of the data buffer;
270     [return]:
271                             FTS_TRUE:            success;
272                             FTS_FALSE:           fail;
273 ************************************************************************/
274 static bool i2c_read_interface(u8* pbt_buf, int dw_lenth)
275 {
276     int ret;
277     
278     ret=i2c_master_recv(this_client, pbt_buf, dw_lenth);
279
280     if(ret<=0)
281     {
282         FTprintk("[TSP]i2c_read_interface error\n");
283         return false;
284     }
285   
286     return true;
287 }
288
289
290 /***********************************************************************
291 [function]: 
292                       callback:         read a byte data  from ctpm;
293 [parameters]:
294                           buffer[in]:       read buffer;
295                           length[in]:      the size of read data;    
296 [return]:
297                           FTS_TRUE:      success;
298                           FTS_FALSE:     io fail;
299 ************************************************************************/
300 static bool byte_read(u8* buffer, int length)
301 {
302     return i2c_read_interface(buffer, length);
303 }
304
305 /***********************************************************************
306 [function]: 
307                       callback:         write a byte data  to ctpm;
308 [parameters]:
309                           buffer[in]:       write buffer;
310                           length[in]:      the size of write data;    
311 [return]:
312                           FTS_TRUE:      success;
313                           FTS_FALSE:     io fail;
314 ************************************************************************/
315 static bool byte_write(u8* buffer, int length)
316 {
317     
318     return i2c_write_interface(buffer, length);
319 }
320
321 /***********************************************************************
322     [function]: 
323                            callback:                 read register value ftom ctpm by i2c interface;
324     [parameters]:
325                         reg_name[in]:         the register which you want to read;
326                             rx_buf[in]:              data buffer which is used to store register value;
327                             rx_length[in]:          the length of the data buffer;
328     [return]:
329                             FTS_TRUE:              success;
330                             FTS_FALSE:             fail;
331 ************************************************************************/
332 static bool fts_register_read(u8 reg_name, u8* rx_buf, int rx_length)
333 {
334         u8 read_cmd[2]= {0};
335         u8 cmd_len      = 0;
336
337         read_cmd[0] = reg_name;
338         cmd_len = 1;    
339
340         /*send register addr*/
341         if(!i2c_write_interface(&read_cmd[0], cmd_len))
342         {
343                 return false;
344         }
345
346         /*call the read callback function to get the register value*/           
347         if(!i2c_read_interface(rx_buf, rx_length))
348         {
349                 return false;
350         }
351         return true;
352 }
353
354
355
356 /***********************************************************************
357 [function]: 
358                         callback:          burn the FW to ctpm.
359 [parameters]:
360                             pbt_buf[in]:     point to Head+FW ;
361                             dw_lenth[in]:   the length of the FW + 6(the Head length);    
362 [return]:
363                             ERR_OK:          no error;
364                             ERR_MODE:      fail to switch to UPDATE mode;
365                             ERR_READID:   read id fail;
366                             ERR_ERASE:     erase chip fail;
367                             ERR_STATUS:   status error;
368                             ERR_ECC:        ecc error.
369 ************************************************************************/
370 E_UPGRADE_ERR_TYPE  fts_ctpm_fw_upgrade(u8* pbt_buf, int dw_lenth)
371 {
372     u8  cmd,reg_val[2] = {0};
373         u8  buffer[2] = {0};
374     u8  packet_buf[FTS_PACKET_LENGTH + 6];
375     u8  auc_i2c_write_buf[10];
376     u8  bt_ecc;
377         
378     int  j,temp,lenght,i_ret,packet_number, i = 0;
379     int  i_is_new_protocol = 0;
380         
381
382     /******write 0xaa to register 0xfc******/
383     cmd=0xaa;
384     fts_register_write(0xfc,&cmd);
385     mdelay(50);
386         
387      /******write 0x55 to register 0xfc******/
388     cmd=0x55;
389     fts_register_write(0xfc,&cmd);
390     FTprintk("[TSP] Step 1: Reset CTPM test\n");
391    
392     mdelay(10);   
393
394
395     /*******Step 2:Enter upgrade mode ****/
396     FTprintk("\n[TSP] Step 2:enter new update mode\n");
397     auc_i2c_write_buf[0] = 0x55;
398     auc_i2c_write_buf[1] = 0xaa;
399     do
400     {
401         i ++;
402         i_ret = fts_i2c_txdata(auc_i2c_write_buf, 2);
403         mdelay(5);
404     }while(i_ret <= 0 && i < 10 );
405
406     if (i > 1)
407     {
408         i_is_new_protocol = 1;
409     }
410
411     /********Step 3:check READ-ID********/        
412     cmd_write(0x90,0x00,0x00,0x00,4);
413     byte_read(reg_val,2);
414     if (reg_val[0] == 0x79 && reg_val[1] == 0x3)
415     {
416         FTprintk("[TSP] Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",reg_val[0],reg_val[1]);
417     }
418     else
419     {
420         return ERR_READID;
421         //i_is_new_protocol = 1;
422     }
423     
424
425      /*********Step 4:erase app**********/
426     if (i_is_new_protocol)
427     {
428         cmd_write(0x61,0x00,0x00,0x00,1);
429     }
430     else
431     {
432         cmd_write(0x60,0x00,0x00,0x00,1);
433     }
434     mdelay(1500);
435     FTprintk("[TSP] Step 4: erase. \n");
436
437
438
439     /*Step 5:write firmware(FW) to ctpm flash*/
440     bt_ecc = 0;
441     FTprintk("[TSP] Step 5: start upgrade. \n");
442     dw_lenth = dw_lenth - 8;
443     packet_number = (dw_lenth) / FTS_PACKET_LENGTH;
444     packet_buf[0] = 0xbf;
445     packet_buf[1] = 0x00;
446         FTprintk("[TSP]  packet_number = %d\n",packet_number);
447     for (j=0;j<packet_number;j++)
448     {
449         temp = j * FTS_PACKET_LENGTH;
450         packet_buf[2] = (u8)(temp>>8);
451         packet_buf[3] = (u8)temp;
452         lenght = FTS_PACKET_LENGTH;
453         packet_buf[4] = (u8)(lenght>>8);
454         packet_buf[5] = (u8)lenght;
455
456         for (i=0;i<FTS_PACKET_LENGTH;i++)
457         {
458             packet_buf[6+i] = pbt_buf[j*FTS_PACKET_LENGTH + i]; 
459             bt_ecc ^= packet_buf[6+i];
460         }
461         
462         byte_write(&packet_buf[0],FTS_PACKET_LENGTH + 6);
463         mdelay(FTS_PACKET_LENGTH/6 + 1);
464         if ((j * FTS_PACKET_LENGTH % 1024) == 0)
465         {
466               FTprintk("[TSP] upgrade the 0x%x th byte.\n", ((unsigned int)j) * FTS_PACKET_LENGTH);
467         }
468     }
469
470     if ((dw_lenth) % FTS_PACKET_LENGTH > 0)
471     {
472         temp = packet_number * FTS_PACKET_LENGTH;
473         packet_buf[2] = (u8)(temp>>8);
474         packet_buf[3] = (u8)temp;
475
476         temp = (dw_lenth) % FTS_PACKET_LENGTH;
477         packet_buf[4] = (u8)(temp>>8);
478         packet_buf[5] = (u8)temp;
479
480         for (i=0;i<temp;i++)
481         {
482             packet_buf[6+i] = pbt_buf[ packet_number*FTS_PACKET_LENGTH + i]; 
483             bt_ecc ^= packet_buf[6+i];
484         }
485
486         byte_write(&packet_buf[0],temp+6);    
487         mdelay(20);
488     }
489
490     /***********send the last six byte**********/
491     for (i = 0; i<6; i++)
492     {
493         temp = 0x6ffa + i;
494         packet_buf[2] = (u8)(temp>>8);
495         packet_buf[3] = (u8)temp;
496         temp =1;
497         packet_buf[4] = (u8)(temp>>8);
498         packet_buf[5] = (u8)temp;
499         packet_buf[6] = pbt_buf[ dw_lenth + i]; 
500         bt_ecc ^= packet_buf[6];
501
502         byte_write(&packet_buf[0],7);  
503         mdelay(20);
504     }
505
506     /********send the opration head************/
507     cmd_write(0xcc,0x00,0x00,0x00,1);
508     byte_read(reg_val,1);
509     FTprintk("[TSP] Step 6:  ecc read 0x%x, new firmware 0x%x. \n", reg_val[0], bt_ecc);
510     if(reg_val[0] != bt_ecc)
511     {
512         return ERR_ECC;
513     }
514
515     /*******Step 7: reset the new FW**********/
516     cmd_write(0x07,0x00,0x00,0x00,1);
517         mdelay(100);//100ms     
518         fts_register_read(0xfc, buffer, 1);     
519         if (buffer[0] == 1)
520         {
521         cmd=4;
522         fts_register_write(0xfc, &cmd);
523         mdelay(2500);//2500ms   
524          do     
525          {      
526          fts_register_read(0xfc, buffer, 1);    
527          mdelay(100);//100ms    
528          }while (buffer[0] != 1);                       
529         }
530     return ERR_OK;
531 }
532
533
534 /***********************************************************************/
535
536 int fts_ctpm_fw_upgrade_with_i_file(void)
537 {
538    u8*     pbt_buf = 0;
539    int i_ret;
540     
541    pbt_buf = CTPM_FW;
542    i_ret =  fts_ctpm_fw_upgrade(pbt_buf,sizeof(CTPM_FW));
543    
544    return i_ret;
545 }
546
547 /***********************************************************************/
548
549 unsigned char fts_ctpm_get_upg_ver(void)
550 {
551     unsigned int ui_sz;
552         
553     ui_sz = sizeof(CTPM_FW);
554     if (ui_sz > 2)
555     {
556         return CTPM_FW[ui_sz - 2];
557     }
558     else
559         return 0xff; 
560  
561 }
562
563 /*read the it7260 register ,used i2c bus*/
564 static int ft5306_read_regs(struct i2c_client *client, u8 reg, u8 *buf, unsigned len)
565 {
566         int ret; 
567         ret = i2c_master_reg8_recv(client, reg, buf, len, FT5306_IIC_SPEED);
568         return ret; 
569 }
570
571 /* set the it7260 registe,used i2c bus*/
572 static int ft5306_set_regs(struct i2c_client *client, u8 reg, u8 *buf, unsigned short len)
573 {
574         int ret; 
575         ret = i2c_master_reg8_send(client, reg, buf, (int)len, FT5306_IIC_SPEED);
576         return ret;
577 }
578
579 static void ft5306_queue_work(struct work_struct *work)
580 {
581         struct ft5x0x_ts_data *data = container_of(work, struct ft5x0x_ts_data, pen_event_work);
582         struct tp_event event;
583         u8 start_reg=0x0;
584         u8 buf[32] = {0};
585         int ret,i,offset,points;
586         static u8 points_last_flag[MAX_POINT]={0};
587         struct tp_event  current_events[MAX_POINT];
588                 
589 #if CONFIG_FT5X0X_MULTITOUCH
590         ret = ft5306_read_regs(data->client,start_reg, buf, 6*MAX_POINT+1);
591 #else
592         ret = ft5306_read_regs(data->client,start_reg, buf, 7);
593 #endif
594         if (ret < 0) {
595                 dev_err(&data->client->dev, "ft5306_read_regs fail:%d!\n",ret);
596                 enable_irq(data->irq);
597                 return;
598         }
599 #if 0
600         for (i=0; i<32; i++) {
601                 FTprintk("buf[%d] = 0x%x \n", i, buf[i]);
602         }
603 #endif
604         
605         points = buf[2] & 0x07;
606         //dev_info(&data->client->dev, "ft5306_read_and_report_data points = %d\n",points);
607         if (points == 0) {
608 #if   CONFIG_FT5X0X_MULTITOUCH
609                 //input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, 0);
610                 //input_report_abs(data->input_dev, ABS_MT_WIDTH_MAJOR, 0);
611                 
612                 for(i=0;i<MAX_POINT;i++)
613                 {
614                         if(points_last_flag[i]!=0)
615                         {
616                                 FTprintk("Point UP event.id=%d\n",i);
617                                 input_mt_slot(data->input_dev, i);
618                                 input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER, false);                                     
619                         }
620                 }
621
622                 memset(points_last_flag, 0, sizeof(points_last_flag));
623                 //input_mt_sync(data->input_dev);
624 #else
625                 input_report_abs(data->input_dev, ABS_PRESSURE, 0);
626                 input_report_key(data->input_dev, BTN_TOUCH, 0);
627 #endif
628                 input_sync(data->input_dev);
629                 enable_irq(data->irq);
630                 return; 
631         }
632         memset(&event, 0, sizeof(struct tp_event));
633 #if CONFIG_FT5X0X_MULTITOUCH
634   memset(current_events, 0, sizeof(current_events));
635   
636         for(i=0;i<points;i++){
637                 offset = i*6+3;
638                 event.x = (((s16)(buf[offset+0] & 0x0F))<<8) | ((s16)buf[offset+1]);
639                 event.y = (((s16)(buf[offset+2] & 0x0F))<<8) | ((s16)buf[offset+3]);
640                 event.id = (s16)(buf[offset+2] & 0xF0)>>4;
641                 event.flag = ((buf[offset+0] & 0xc0) >> 6);
642                 event.pressure = 200;
643                 FTprintk("x=%d, y=%d event.id=%d event.flag=%d\n",event.x,event.y,event.id,event.flag);
644                 if(event.x<=SCREEN_MAX_X && event.y<=SCREEN_MAX_Y+60){
645                         //dev_info(&data->client->dev, 
646                         //      "ft5306 multiple report event[%d]:x = %d,y = %d,id = %d,flag = %d,pressure = %d\n",
647                         //      i,event.x,event.y,event.id,event.flag,event.pressure);
648                 if(event.flag)
649                         memcpy(&current_events[event.id], &event, sizeof(event));
650                         //points_current[event.id] = event.flag;                        
651                 }
652         }
653         
654         for(i=0;i<MAX_POINT;i++)
655         {
656                   if((current_events[i].flag == 0) && (points_last_flag[i] != 0))
657                         {
658                 FTprintk("Point UP event.id=%d\n",i);
659                                         input_mt_slot(data->input_dev, i);
660                                         input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER, false);                                             
661                         }
662                         else  if(current_events[i].flag)        
663                         {       
664                                 FTprintk("Point DN event.id=%d\n",i);
665                                         input_mt_slot(data->input_dev, i);
666                                         input_mt_report_slot_state(data->input_dev, MT_TOOL_FINGER, true);
667                                         input_report_abs(data->input_dev, ABS_MT_TOUCH_MAJOR, 1);
668                                         //input_report_abs(data->input_dev, ABS_MT_PRESSURE, event.pressure);
669                                         input_report_abs(data->input_dev, ABS_MT_POSITION_X,  current_events[i].x);
670                                         input_report_abs(data->input_dev, ABS_MT_POSITION_Y,  current_events[i].y);                                                                     
671                         }               
672                         points_last_flag[i] =   current_events[i].flag;
673         }
674 #else
675         event.x = (s16)(buf[3] & 0x0F)<<8 | (s16)buf[4];
676         event.y = (s16)(buf[5] & 0x0F)<<8 | (s16)buf[6];
677         event.pressure =200;
678         input_report_abs(data->input_dev, ABS_X, event.x);
679         input_report_abs(data->input_dev, ABS_Y, event.y);
680         //input_report_abs(data->input_dev, ABS_PRESSURE, event.pressure);
681         input_report_key(data->input_dev, BTN_TOUCH, 1);
682         
683         
684         //dev_info(&data->client->dev, "ft5306 single report event:x = %d,y = %d\n",event.x,event.y);
685 #endif
686         //dev_info(&data->client->dev, "ft5306 sync\n",event.x,event.y);
687         input_sync(data->input_dev);
688         enable_irq(data->irq);
689         return;
690 }
691
692 static irqreturn_t ft5306_interrupt(int irq, void *dev_id)
693 {
694         struct ft5x0x_ts_data *ft5x0x_ts = dev_id;
695         FTprintk("[TSP]  ft5306_interrupt\n");
696         disable_irq_nosync(ft5x0x_ts->irq);
697         if (!work_pending(&ft5x0x_ts->pen_event_work)) 
698                 queue_work(ft5x0x_ts->ts_workqueue, &ft5x0x_ts->pen_event_work);
699         return IRQ_HANDLED;
700 }
701 #ifdef CONFIG_HAS_EARLYSUSPEND
702
703 static int ft5306_suspend(struct early_suspend *h)
704 {
705         struct ft5x0x_ts_data *ft5x0x_ts;
706         char buf_w[1] = {3};
707         int err;
708     ft5x0x_ts = container_of(h, struct ft5x0x_ts_data, ft5306_early_suspend);
709         FTprintk("TSP ft5306_suspend\n");
710         //if (ft5x0x_ts->platform_sleep){ 
711         //      ft5x0x_ts->platform_sleep();
712         //}
713                 err = ft5306_set_regs(this_client,0xA5,buf_w,1);
714         if (err>0)
715                 printk("ft5306_set_regs OK!!\n");
716         disable_irq(ft5x0x_ts->irq);
717         return 0;
718 }
719
720
721 static int ft5306_resume(struct early_suspend *h)
722 {
723         struct ft5x0x_ts_data *ft5x0x_ts;       
724         ft5x0x_ts = container_of(h, struct ft5x0x_ts_data, ft5306_early_suspend);
725         FTprintk("TSP ft5306_resume\n");
726         enable_irq(ft5x0x_ts->irq);
727         //if (ft5x0x_ts->platform_wakeup)                              
728                 //ft5x0x_ts->platform_wakeup();
729         gpio_set_value(TOUCH_RESET_PIN,GPIO_LOW);
730         msleep(10);
731         gpio_set_value(TOUCH_RESET_PIN,GPIO_HIGH);
732         return 0;
733 }
734 #endif
735
736 static int __devexit ft5306_remove(struct i2c_client *client)
737 {
738         struct ft5x0x_ts_data *ft5x0x_ts = i2c_get_clientdata(client);
739
740         free_irq(ft5x0x_ts->irq, ft5x0x_ts);
741         input_unregister_device(ft5x0x_ts->input_dev);
742         kfree(ft5x0x_ts);
743         cancel_work_sync(&ft5x0x_ts->pen_event_work);
744         destroy_workqueue(ft5x0x_ts->ts_workqueue);
745         i2c_set_clientdata(client, NULL);
746 #ifdef CONFIG_HAS_EARLYSUSPEND
747     unregister_early_suspend(&ft5x0x_ts->ft5306_early_suspend);
748 #endif 
749     this_client = NULL;
750         return 0;
751 }
752
753 static int  ft5306_probe(struct i2c_client *client ,const struct i2c_device_id *id)
754 {
755         struct ft5x0x_ts_data *ft5x0x_ts;
756         struct input_dev *input_dev;
757         struct ft5406_platform_data *pdata = client->dev.platform_data;
758         int err = 0;
759         int ret = 0;
760         int retry = 0;
761         u8 buf_w[1];
762         u8 buf_r[1];
763         const u8 buf_test[1] = {0};
764     unsigned char reg_value;
765     unsigned char reg_version;
766
767         dev_info(&client->dev, "ft5306_ts_probe!\n");
768         if (!pdata) {
769                 dev_err(&client->dev, "platform data is required!\n");
770                 return -EINVAL;
771         }
772
773         if (pdata->init_platform_hw)                              
774                 pdata->init_platform_hw();
775         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)){
776                 dev_err(&client->dev, "Must have I2C_FUNC_I2C.\n");
777                 return -ENODEV;
778         }
779         
780         ft5x0x_ts = kzalloc(sizeof(*ft5x0x_ts), GFP_KERNEL);
781         ft5x0x_ts->platform_wakeup = pdata->platform_wakeup;
782         ft5x0x_ts->platform_sleep = pdata->platform_sleep;      
783         if (!ft5x0x_ts) {
784                 return -ENOMEM;
785         }
786
787         while(retry < 5)
788         {
789                 ret=ft5306_set_regs(client,FT5X0X_REG_PMODE, buf_test,1);
790                 if(ret > 0)break;
791                 retry++;
792         }
793         if(ret <= 0)
794         {
795                 FTprintk("FT5306 I2C TEST ERROR!\n");
796                 err = -ENODEV;
797                 goto exit_i2c_test_fail;
798         }
799         
800         input_dev = input_allocate_device();
801         if (!input_dev) {
802                 err = -ENOMEM;
803                 FTprintk("failed to allocate input device\n");
804                 goto exit_input_dev_alloc_failed;
805         }
806         ft5x0x_ts->client = this_client = client;
807         ft5x0x_ts->irq = client->irq;
808         ft5x0x_ts->input_dev = input_dev;
809
810   #if   CONFIG_FT5X0X_MULTITOUCH
811         __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
812         __set_bit(EV_ABS, input_dev->evbit);    
813
814         input_mt_init_slots(input_dev, MAX_POINT);
815         input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);      
816         input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, SCREEN_MAX_X, 0, 0);
817         input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, SCREEN_MAX_Y, 0, 0);
818         //input_set_abs_params(input_dev, ABS_MT_TRACKING_ID, 0, MAX_POINT, 0, 0);
819         //input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
820         
821 #else
822         set_bit(ABS_X, input_dev->absbit);
823         set_bit(ABS_Y, input_dev->absbit);
824         set_bit(ABS_PRESSURE, input_dev->absbit);
825         set_bit(BTN_TOUCH, input_dev->keybit);
826         input_set_abs_params(input_dev, ABS_X, 0, SCREEN_MAX_X, 0, 0);
827         input_set_abs_params(input_dev, ABS_Y, 0, SCREEN_MAX_Y, 0, 0);
828         //input_set_abs_params(input_dev, ABS_PRESSURE, 0, PRESS_MAX, 0 , 0);
829 #endif
830
831         //input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); 
832         //input_dev->keybit[BIT_WORD(BTN_START)] = BIT_MASK(BTN_START);
833         //set_bit(EV_ABS, input_dev->evbit);
834         //set_bit(EV_KEY, input_dev->evbit);
835         
836         input_dev->name         = "ft5x0x_ts-touchscreen";              //dev_name(&client->dev)
837         err = input_register_device(input_dev);
838         if (err) {
839                 FTprintk("ft5306_ts_probe: failed to register input device: \n");
840                 goto exit_input_register_device_failed;
841         }
842
843         if (!ft5x0x_ts->irq) {
844                 err = -ENODEV;
845                 dev_err(&ft5x0x_ts->client->dev, "no IRQ?\n");
846                 goto exit_no_irq_fail;
847         }else{
848                 ft5x0x_ts->irq = gpio_to_irq(ft5x0x_ts->irq);
849         }
850
851         INIT_WORK(&ft5x0x_ts->pen_event_work, ft5306_queue_work);
852         ft5x0x_ts->ts_workqueue = create_singlethread_workqueue("ft5x0x_ts");
853         if (!ft5x0x_ts->ts_workqueue) {
854                 err = -ESRCH;
855                 goto exit_create_singlethread;
856         }
857
858         /***wait CTP to bootup normally***/
859         msleep(200); 
860 #if 1   //write firmware 
861         fts_register_read(FT5X0X_REG_FIRMID, &reg_version,1);
862         FTprintk("[TSP] firmware version = 0x%2x\n", reg_version);
863         if (fts_ctpm_get_upg_ver() != reg_version)  
864         {
865           FTprintk("[TSP] start upgrade new verison 0x%2x\n", fts_ctpm_get_upg_ver());
866           msleep(200);
867           err =  fts_ctpm_fw_upgrade_with_i_file();
868           if (err == 0)
869           {
870                   FTprintk("[TSP] ugrade successfuly.\n");
871                   msleep(300);
872                   fts_register_read(FT5X0X_REG_FIRMID, &reg_value,1);
873                   FTprintk("FTS_DBG from old version 0x%2x to new version = 0x%2x\n", reg_version, reg_value);
874           }
875           else
876           {
877                   FTprintk("[TSP]  ugrade fail err=%d, line = %d.\n",err, __LINE__);
878           }
879           msleep(4000);
880         }
881 #endif
882         ret = request_irq(ft5x0x_ts->irq, ft5306_interrupt, IRQF_TRIGGER_FALLING, client->dev.driver->name, ft5x0x_ts);
883         if (ret < 0) {
884                 dev_err(&client->dev, "irq %d busy?\n", ft5x0x_ts->irq);
885                 goto exit_irq_request_fail;
886         }
887         i2c_set_clientdata(client, ft5x0x_ts);
888 #ifdef CONFIG_HAS_EARLYSUSPEND
889         ft5x0x_ts->ft5306_early_suspend.suspend =ft5306_suspend;
890         ft5x0x_ts->ft5306_early_suspend.resume =ft5306_resume;
891         ft5x0x_ts->ft5306_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;;
892         register_early_suspend(&ft5x0x_ts->ft5306_early_suspend);
893 #endif
894         buf_w[0] = 6;
895         err = ft5306_set_regs(client,0x88,buf_w,1);
896         buf_r[0] = 0;
897         err = ft5306_read_regs(client,0x88,buf_r,1);
898         FTprintk("read buf[0x88] = %d\n", buf_r[0]);
899     return 0;
900
901         i2c_set_clientdata(client, NULL);
902         free_irq(ft5x0x_ts->irq,ft5x0x_ts);
903 exit_irq_request_fail:
904         cancel_work_sync(&ft5x0x_ts->pen_event_work);
905         destroy_workqueue(ft5x0x_ts->ts_workqueue);
906 exit_create_singlethread:
907 exit_no_irq_fail:
908         input_unregister_device(input_dev);
909 exit_input_register_device_failed:
910         input_free_device(input_dev);
911 exit_input_dev_alloc_failed:
912 exit_i2c_test_fail:
913         if (pdata->exit_platform_hw)                              
914                 pdata->exit_platform_hw();
915         kfree(ft5x0x_ts);
916         return err;
917 }
918
919
920
921 static struct i2c_device_id ft5306_idtable[] = {
922         { FT5X0X_NAME, 0 },
923         { }
924 };
925
926 MODULE_DEVICE_TABLE(i2c, ft5306_idtable);
927
928 static struct i2c_driver ft5306_driver  = {
929         .driver = {
930                 .owner  = THIS_MODULE,
931                 .name   = FT5X0X_NAME
932         },
933         .id_table       = ft5306_idtable,
934         .probe      = ft5306_probe,
935     .suspend    = ft5306_suspend,
936         .resume     = ft5306_resume,
937         .remove         = __devexit_p(ft5306_remove),
938 };
939
940 static int __init ft5306_ts_init(void)
941 {
942         return i2c_add_driver(&ft5306_driver);
943 }
944
945 static void __exit ft5306_ts_exit(void)
946 {
947         FTprintk("Touchscreen driver of ft5306 exited.\n");
948         i2c_del_driver(&ft5306_driver);
949 }
950
951
952 /***********************************************************************/
953
954 module_init(ft5306_ts_init);
955 module_exit(ft5306_ts_exit);
956
957 MODULE_AUTHOR("<wenfs@Focaltech-systems.com>");
958 MODULE_DESCRIPTION("FocalTech ft5x0x TouchScreen driver");
959