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