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