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