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