input: touchscreen: add touch screen of gslx680 for rk3399-firefly-edp
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / ft5306_ts_wpx2.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/irqs.h>
53 #include <mach/board.h>
54 #include <mach/gpio.h>
55 #include <mach/sram.h>
56 #ifdef CONFIG_HAS_EARLYSUSPEND
57 #include <linux/earlysuspend.h>
58 #endif
59 #include <linux/input/mt.h>
60
61
62 #if 0
63 #define fts_dbg(dev, format, arg...)            \
64         dev_printk(KERN_INFO , dev , format , ## arg)
65 #else
66 #define fts_dbg(dev, format, arg...)
67 #endif
68
69 #define FTS_NAME                "ft5x0x_ts"
70 #define FTS_I2C_RATE            (400*1000)
71 #define MAX_POINT               5
72 #define SCREEN_MAX_Y            800
73 #define SCREEN_MAX_X            480
74 #define PRESS_MAX               255
75
76 #define FTS_REPORT_RATE         0x0c //0x06 // report_rate = (FTS_REPORT_RATE)*(10Hz)
77 #define FTS_VALID_THRES         0x0a // valid_tresshold = 0x0a * 4
78
79 #define FTS_REG_THRES           0x80         /* Thresshold, the threshold be low, the sensitivy will be high */
80 #define FTS_REG_REPORT_RATE     0x88         /* **************report rate, in unit of 10Hz **************/
81 #define FTS_REG_PMODE           0xA5         /* Power Consume Mode 0 -- active, 1 -- monitor, 3 -- sleep */    
82 #define FS_REG_FIRMID           0xA6         /* ***************firmware version **********************/
83 #define FS_REG_NOISE_MODE       0xb2         /* to enable or disable power noise, 1 -- enable, 0 -- disable */
84
85 //FT5X0X_REG_PMODE
86 enum {
87         FTS_PMODE_ACTIVE  = 0x00,
88         FTS_PMODE_MONITOR,
89         FTS_PMODE_STANDBY,
90         FTS_PMODE_HIBERNATE,
91 };
92
93 struct fts_event{
94         u16 x;
95         u16 y;
96         u16 pressure;
97         s16 id;
98         u8 flag;
99 };
100 struct fts_data {
101         int irq;
102         u8 touch_point;
103         u16 down_count[MAX_POINT];
104         u8 flags[MAX_POINT];
105         struct workqueue_struct *freezable_work; 
106         struct delayed_work work;
107
108         struct i2c_client *client;
109         struct device *dev;
110         struct input_dev *input_dev;
111         int (*platform_sleep)(void);
112         int (*platform_wakeup)(void);
113         int (*platform_init_hw)(void);
114         void (*platform_deinit_hw)(void);
115 #ifdef CONFIG_HAS_EARLYSUSPEND
116         struct early_suspend suspend;
117 #endif
118 };
119
120 static int fts_i2c_read(struct fts_data *ts, const char reg, char *buf, int len)
121 {
122         int ret = 0;
123
124         ret = i2c_master_reg8_recv(ts->client, reg, buf, len, FTS_I2C_RATE);
125
126         if(ret < 0)
127                 fts_dbg(ts->dev, "%s error, reg: 0x%x\n", __func__, reg);
128
129         ret = (ret < 0)?ret:0;
130
131         return ret;
132 }
133 static int fts_i2c_write(struct fts_data *ts, const char reg, const char *buf, int len)
134 {
135         int ret  = 0;
136
137         ret = i2c_master_reg8_send(ts->client, reg, buf, len , FTS_I2C_RATE);
138         
139         if(ret < 0)
140                 fts_dbg(ts->dev, "%s error, reg: 0x%x\n", __func__, reg);
141
142         ret = (ret < 0)?ret:0;
143
144         return ret;
145 }
146 #define    FTS_PACKET_LENGTH        128
147 static u8 CTPM_FW[]=
148 {
149 #include "ft_app_5306.i"
150 };
151
152 typedef enum
153 {
154         ERR_OK,
155         ERR_MODE,
156         ERR_READID,
157         ERR_ERASE,
158         ERR_STATUS,
159         ERR_ECC,
160         ERR_DL_ERASE_FAIL,
161         ERR_DL_PROGRAM_FAIL,
162         ERR_DL_VERIFY_FAIL,
163         ERR_RESET_FM
164 }E_UPGRADE_ERR_TYPE;
165
166 static int fts_ctpm_read(struct fts_data *ts, char *buf, int len)
167 {
168         int ret  = 0;
169
170         ret = i2c_master_normal_recv(ts->client,  buf, len , FTS_I2C_RATE);
171         
172         if(ret < 0)
173                 fts_dbg(ts->dev, "%s error\n", __func__);
174
175         ret = (ret < 0)?ret:0;
176
177         return ret;
178 }
179 static int fts_ctpm_write(struct fts_data *ts, char *buf, int len)
180 {
181         int ret  = 0;
182
183         ret = i2c_master_normal_send(ts->client,  buf, len , FTS_I2C_RATE);
184         
185         if(ret < 0)
186                 fts_dbg(ts->dev, "%s error\n", __func__);
187
188         ret = (ret < 0)?ret:0;
189
190         return ret;
191 }
192
193 E_UPGRADE_ERR_TYPE  fts_ctpm_fw_upgrade(struct fts_data *ts, u8* pbt_buf, int dw_lenth)
194 {
195         char val[4];
196         int retry = 10, ret = 0, tmp;
197         int  i, j, i_is_new_protocol = 0, packet_number;
198         u8  packet_buf[FTS_PACKET_LENGTH + 6];
199         u8  bt_ecc = 0;
200
201         val[0] = 0xaa;
202         fts_i2c_write(ts, 0xfc, val, 1);
203         mdelay(50);
204         val[0] = 0x55;
205         fts_i2c_write(ts, 0xfc, val, 1);
206         fts_dbg(ts->dev, "[TSP] Step 1: Reset CTPM\n");
207         mdelay(10);
208         fts_dbg(ts->dev, "[TSP] Step 2:enter new update mode\n");
209
210         val[0] = 0x55;
211         val[1] = 0xaa;
212         do{
213                 ret = fts_ctpm_write(ts, val, 2);
214         }while(retry-- && (ret < 0));
215
216         if (retry > 0)
217                 i_is_new_protocol = 1;
218
219         val[0] = 0x90;
220         val[1] = 0x00;
221         val[2] = 0x00;
222         val[3] = 0x00;
223         fts_ctpm_write(ts, val, 4);
224
225         fts_ctpm_read(ts, val, 2);
226         fts_dbg(ts->dev, "[TSP] Step 3: CTPM ID,ID1 = 0x%x,ID2 = 0x%x\n",val[0],val[1]);
227         if(val[0] != 0x79 || val[1] != 0x03)
228                 return -ERR_READID;
229         
230         if(i_is_new_protocol)
231                 val[0] = 0x61;
232         else
233                 val[0] = 0x60;
234         
235         val[1] = 0x00;
236         val[2] = 0x00;
237         val[3] = 0x00;
238         fts_ctpm_write(ts, val, 1);
239
240         mdelay(1500);
241         fts_dbg(ts->dev,"[TSP] Step 4: erase. \n");
242
243         dw_lenth = dw_lenth - 8;
244         packet_number = (dw_lenth) / FTS_PACKET_LENGTH;
245         fts_dbg(ts->dev,"[TSP] Step 5: start upgrade, packet_number = %d\n", packet_number);
246
247         packet_buf[0] = 0xbf;
248         packet_buf[1] = 0x00;
249
250         for(i = 0; i < packet_number; i++){
251                 tmp = i * FTS_PACKET_LENGTH;
252                 packet_buf[2] = (u8)(tmp >> 8);
253                 packet_buf[3] = (u8)tmp ;
254                 tmp = FTS_PACKET_LENGTH;
255                 packet_buf[4] = (u8)(tmp >> 8);
256                 packet_buf[5] = (u8)(tmp);
257
258                 for(j = 0; j < FTS_PACKET_LENGTH; j++){
259                         packet_buf[6+j] = pbt_buf[i*FTS_PACKET_LENGTH + j];
260                         bt_ecc ^= packet_buf[6+j];
261                 }
262                 fts_ctpm_write(ts, packet_buf, FTS_PACKET_LENGTH + 6);
263                 mdelay(FTS_PACKET_LENGTH/6 + 1);
264
265                 if ((i * FTS_PACKET_LENGTH % 1024) == 0)
266                 {
267                         fts_dbg(ts->dev, "[TSP] upgrade the 0x%x th byte.\n", ((unsigned int)i) * FTS_PACKET_LENGTH);
268                 }
269         }
270         
271         if ((dw_lenth) % FTS_PACKET_LENGTH > 0)
272         {
273                 tmp = packet_number * FTS_PACKET_LENGTH;
274                 packet_buf[2] = (u8)(tmp>>8);
275                 packet_buf[3] = (u8)tmp;
276                 
277                 tmp = (dw_lenth) % FTS_PACKET_LENGTH;
278                 packet_buf[4] = (u8)(tmp>>8);
279                 packet_buf[5] = (u8)tmp;
280
281                 for (i=0;i<tmp; i++)
282                 {
283                         packet_buf[6+i] = pbt_buf[packet_number*FTS_PACKET_LENGTH + i]; 
284                         bt_ecc ^= packet_buf[6+i];
285                 }
286
287                 fts_ctpm_write(ts, packet_buf, tmp + 6);    
288                 mdelay(20);
289         }
290
291         for (i = 0; i<6; i++)
292         {
293                 tmp = 0x6ffa + i;
294                 packet_buf[2] = (u8)(tmp>>8);
295                 packet_buf[3] = (u8)tmp;
296                 packet_buf[4] = (u8)(1>>8);
297                 packet_buf[5] = (u8)1;
298                 packet_buf[6] = pbt_buf[dw_lenth + i]; 
299                 bt_ecc ^= packet_buf[6];
300
301                 fts_ctpm_write(ts, packet_buf,7);  
302                 mdelay(20);
303         }
304
305         val[0] = 0xcc;
306         val[1] = 0x00;
307         val[2] = 0x00;
308         val[3] = 0x00;
309         fts_ctpm_write(ts, val, 1);
310
311         fts_ctpm_read(ts, val, 1);
312         fts_dbg(ts->dev,"[TSP] Step 6:  ecc read 0x%x, new firmware 0x%x. \n", val[0], bt_ecc);
313
314         if(val[0] != bt_ecc)
315                 return -ERR_ECC;
316
317         val[0] = 0x07;
318         val[1] = 0x00;
319         val[2] = 0x00;
320         val[3] = 0x00;
321         fts_ctpm_write(ts, val, 1);
322         
323         mdelay(100);//100ms
324         fts_dbg(ts->dev, "[TSP] Step 1: Reset new FM\n");
325         fts_i2c_read(ts, 0xfc, val, 1);
326         if(val[0] == 0x01){
327                 val[0] = 0x04;
328                 fts_i2c_write(ts, 0xfc, val, 1);
329                 mdelay(2500);//2500ms
330                 do{
331                         fts_i2c_read(ts, 0xfc, val, 1);
332                         mdelay(100);//100ms
333                 }while(retry-- && val[0] != 1);
334
335                 if(retry <= 0)
336                         return -ERR_RESET_FM;
337         }
338         
339         fts_dbg(ts->dev, "[TSP] %s ok\n", __func__);    
340         return ERR_OK;
341 }
342 int fts_ctpm_fw_upgrade_with_i_file(struct fts_data *ts)
343 {
344         u8* pbt_buf = 0;
345         int ret;
346     
347         pbt_buf = CTPM_FW;
348         ret =  fts_ctpm_fw_upgrade(ts, pbt_buf, sizeof(CTPM_FW));
349    
350         return ret;
351 }
352 unsigned char fts_ctpm_get_upg_ver(void)
353 {
354         unsigned int ui_sz;
355         
356         ui_sz = sizeof(CTPM_FW);
357         if (ui_sz > 2)
358         {
359                 return CTPM_FW[ui_sz - 2];
360         }
361         else
362                 return 0xff; 
363  
364 }
365 static int fts_update_config(struct fts_data *ts)
366 {
367         char old_ver, new_ver;
368         int ret = 0;
369
370         ret = fts_i2c_read(ts, FS_REG_FIRMID, &old_ver, 1);
371         if(ret < 0){
372                 dev_err(ts->dev, "%s: i2c read version error\n", __func__);
373                 return ret;
374         }
375
376         if(fts_ctpm_get_upg_ver() != old_ver){
377                 msleep(200);
378                 ret =  fts_ctpm_fw_upgrade_with_i_file(ts);
379                 if(ret < 0){
380                         dev_err(ts->dev, "%s: failed to ugrade\n", __func__);
381                         return ret;
382                 }
383                 msleep(200);
384                 fts_i2c_read(ts, FS_REG_FIRMID, &new_ver, 1);
385                 dev_info(ts->dev, "Update from old version[0x%2x] to new version[0x%2x]\n", old_ver, new_ver);
386                 msleep(4000);
387         }
388         return 0;
389 }
390
391 static void fts_work(struct work_struct *work)
392 {
393         int ret = 0;
394         char buf[2];
395         struct fts_data *ts = container_of(work, struct fts_data, work.work); 
396
397         buf[0] = FTS_REPORT_RATE;
398         ret = fts_i2c_write(ts, FTS_REG_REPORT_RATE, &buf[0], 1);
399         if(ret == 0)
400                 ret = fts_i2c_read(ts, FTS_REG_REPORT_RATE, &buf[0], 1);
401         if(ret < 0){
402                 dev_err(ts->dev, "%s: i2c(r/w) error, reg: 0x%x\n", __func__, FTS_REG_REPORT_RATE);
403                 goto again;
404         }
405         
406         buf[1] = FTS_VALID_THRES;
407         ret = fts_i2c_write(ts, FTS_REG_THRES, &buf[1], 1);
408         if(ret == 0)
409                 ret = fts_i2c_read(ts, FTS_REG_THRES, &buf[1], 1);
410         if(ret < 0){
411                 dev_err(ts->dev, "%s: i2c(r/w), reg: 0x%x\n", __func__, FTS_REG_THRES);
412                 goto again;
413         }
414         if(buf[0] == FTS_REPORT_RATE && buf[1] == FTS_VALID_THRES){
415                 dev_info(ts->dev, "%s: report rate: %dHz, valide thres: %d\n", __func__, buf[0]*10, buf[1]*4);
416                 return;
417         }
418
419 again:
420         queue_delayed_work(ts->freezable_work, &ts->work, msecs_to_jiffies(1000));
421         return;
422
423 }
424
425 static void fts_report_value(struct fts_data *ts)
426 {
427         char buf[6*MAX_POINT+1], reg;
428         struct fts_event events[MAX_POINT], ev;
429         int ret = 0, i, off;
430
431         reg = 0x00;
432         ret = fts_i2c_read(ts, reg, buf, 6*MAX_POINT+1);
433
434         if (ret < 0) {
435                 dev_err(ts->dev, "fts_i2c_write error:%d!\n",ret);
436                 return;
437         }
438         memset(events, 0, sizeof(struct fts_event) * MAX_POINT);
439         ts->touch_point = buf[2]&0x07;
440
441         if(ts->touch_point == 0){
442                 for(i = 0; i < MAX_POINT; i++){
443                         if(ts->flags[i] != 0){
444                                 fts_dbg(ts->dev, "Point UP: id: %d, down_count: %d\n",i, ts->down_count[i]);
445                                 ts->flags[i] = 0;
446                                 ts->down_count[i] = 0;
447                                 input_mt_slot(ts->input_dev, i);
448                                 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, false);
449                         }
450                 }
451                 input_sync(ts->input_dev);
452                 return;
453         }
454
455         for(i = 0; i < ts->touch_point; i++){
456                 off = i*6+3;
457                 ev.id = (s16)(buf[off+2] & 0xF0)>>4;
458                 if(unlikely((ev.id >= MAX_POINT) || (ev.id < 0))){
459                         dev_err(ts->dev, "read touch_id[%d] error\n", ev.id);
460                         return;
461                 }
462                 ev.x = ((buf[off+0] & 0x0F)<<8) | buf[off+1];
463                 ev.y = ((buf[off+2] & 0x0F)<<8) | buf[off+3];
464                 if(unlikely(ev.x > SCREEN_MAX_X || ev.y > (SCREEN_MAX_Y + 60))){
465                         dev_err(ts->dev, "read pos[x:%d, y:%d] error\n", ev.x, ev.y);
466                         continue;
467                 }
468                 ev.flag = (buf[off+0] & 0xc0) >> 6;
469                 if(ev.flag){
470                         events[ev.id].x = ev.x;
471                         events[ev.id].y = ev.y;
472                         events[ev.id].flag = ev.flag;
473                         events[ev.id].pressure = 200;
474                 }
475                 fts_dbg(ts->dev, "get event: id: %d, x: %d, y: %d, flag:%d\n",ev.id, ev.x, ev.y, ev.flag);
476         }
477         for(i = 0; i < MAX_POINT; i++){
478                 if((events[i].flag == 0) && (ts->flags[i] != 0)){
479                         fts_dbg(ts->dev, "Point UP: id: %d, x: %d, y: %d, down_count: %d\n",
480                                 i, events[i].x, events[i].y, ts->down_count[i]);
481                         ts->down_count[i] = 0;
482                         input_mt_slot(ts->input_dev, i);
483                         input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, false);
484                 }else if(events[i].flag != 0){
485                         fts_dbg(ts->dev, "Point DOWN: id: %d, x: %d, y: %d\n",i, events[i].x, events[i].y);
486                         ts->down_count[i]++;
487                         input_mt_slot(ts->input_dev, i);
488                         input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
489                         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 1);
490                         //input_report_abs(data->input_dev, ABS_MT_PRESSURE, event.pressure);
491                         input_report_abs(ts->input_dev, ABS_MT_POSITION_X,  events[i].x);
492                         input_report_abs(ts->input_dev, ABS_MT_POSITION_Y,  events[i].y);
493                 }
494                 ts->flags[i] = events[i].flag;
495         }
496         input_sync(ts->input_dev);
497         return;
498 }
499
500 static irqreturn_t fts_interrupt(int irq, void *dev_id)
501 {
502         struct fts_data *ts = dev_id;
503         
504         disable_irq_nosync(ts->irq);
505         fts_report_value(ts);
506         enable_irq(ts->irq);
507         return IRQ_HANDLED;
508 }
509 #ifdef CONFIG_HAS_EARLYSUSPEND
510 void fts_early_suspend(struct early_suspend *h)
511 {
512         struct fts_data *ts = container_of(h, struct fts_data, suspend);
513
514         char buf = FTS_PMODE_HIBERNATE;
515         int ret = 0;
516         
517         disable_irq(ts->irq);
518         cancel_delayed_work(&ts->work);
519         ret = fts_i2c_write(ts, FTS_REG_PMODE, &buf, 1);
520
521         if(ret < 0)
522                 dev_err(ts->dev, "%s: i2c_write error, reg = 0x%x\n", __func__, FTS_REG_PMODE);
523         if(ts->platform_sleep)
524                 ts->platform_sleep();
525         return;
526 }
527 void fts_early_resume(struct early_suspend *h)
528 {
529         struct fts_data *ts = container_of(h, struct fts_data, suspend);
530         if(ts->platform_wakeup)
531                 ts->platform_wakeup();
532
533         queue_delayed_work(ts->freezable_work, &ts->work, msecs_to_jiffies(1000));
534         
535         enable_irq(ts->irq);
536         return;
537 }
538 #endif
539 static struct i2c_device_id fts_idtable[] = {
540         { FTS_NAME, 0 },
541         { }
542 };
543
544 static int  fts_probe(struct i2c_client *client ,const struct i2c_device_id *id)
545 {
546         struct fts_data *ts;
547         struct ft5x0x_platform_data *pdata = client->dev.platform_data;
548
549         char buf[2];
550         int retrys = 5, ret = 0;
551
552         if (!pdata) {
553                 dev_err(&client->dev, "no platform data\n");
554                 return -EINVAL;
555         }
556
557         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)){
558                 dev_err(&client->dev, "Must have I2C_FUNC_I2C.\n");
559                 return -ENODEV;
560         }
561         
562         if(!client->irq){
563                 dev_err(&client->dev, "no irq.\n");
564                 return -ENODEV;
565         }
566         
567         ts = kzalloc(sizeof(struct fts_data), GFP_KERNEL);      
568         if (!ts){
569                 dev_err(&client->dev, "No memory for fts\n");
570                 return -ENOMEM;
571         }
572         ts->client = client;
573         ts->dev = &client->dev;
574         ts->irq = gpio_to_irq(client->irq);
575         ts->platform_wakeup = pdata->ft5x0x_platform_wakeup;
576         ts->platform_sleep = pdata->ft5x0x_platform_sleep;
577         ts->platform_init_hw = pdata->init_platform_hw;
578         ts->platform_deinit_hw = pdata->exit_platform_hw;
579
580         i2c_set_clientdata(client, ts);
581
582         if (pdata->init_platform_hw)                              
583                 pdata->init_platform_hw();
584
585         buf[0] = FTS_PMODE_MONITOR;
586         while(retrys--){
587                 ret = fts_i2c_write(ts, FTS_REG_PMODE, buf, 1);
588                 if(ret == 0)
589                         break;
590         }
591         
592         if(ret < 0){
593                 dev_err(ts->dev, "fts_i2c_write error, reg: 0x%x\n", FTS_REG_PMODE);
594                 goto err_i2c_write;
595         }
596         
597         ts->input_dev = input_allocate_device();
598         if (!ts->input_dev) {
599                 ret = -ENOMEM;
600                 dev_err(ts->dev, "failed to allocate input device\n");
601                 goto err_input_allocate_device;
602         }
603
604         __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
605         __set_bit(EV_ABS, ts->input_dev->evbit);        
606
607         input_mt_init_slots(ts->input_dev, MAX_POINT);
608         input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);  
609         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, SCREEN_MAX_X, 0, 0);
610         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, SCREEN_MAX_Y, 0, 0);
611
612         ts->input_dev->name = "ft5x0x_ts-touchscreen";          //dev_name(&client->dev)
613         ret = input_register_device(ts->input_dev);
614         if (ret) {
615                 dev_err(ts->dev,"failed to register input device: \n");
616                 goto err_input_register_device;
617         }
618
619         ret = fts_update_config(ts);
620         if(ret < 0){
621                 dev_err(ts->dev, "failed to fts_update_config\n");
622                 goto err_fts_update_config;
623         }
624
625 #ifdef CONFIG_HAS_EARLYSUSPEND
626         ts->suspend.suspend =fts_early_suspend;
627         ts->suspend.resume =fts_early_resume;
628         ts->suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;;
629         register_early_suspend(&ts->suspend);
630 #endif
631
632         ts->freezable_work = system_freezable_wq;
633         INIT_DELAYED_WORK(&ts->work, fts_work);
634
635         buf[0] = FTS_REPORT_RATE;
636         ret = fts_i2c_write(ts, FTS_REG_REPORT_RATE, &buf[0], 1);
637         if(ret == 0)
638                 ret = fts_i2c_read(ts, FTS_REG_REPORT_RATE, &buf[0], 1);
639         if(ret < 0){
640                 dev_err(&client->dev, "fts_i2c_read error, reg: 0x%x\n", FTS_REG_REPORT_RATE);
641                 goto err_request_irq;
642         }
643         
644         buf[1] = FTS_VALID_THRES;
645         ret = fts_i2c_write(ts, FTS_REG_THRES, &buf[1], 1);
646         if(ret == 0)
647                 ret = fts_i2c_read(ts, FTS_REG_THRES, &buf[1], 1);
648         if(ret < 0){
649                 dev_err(&client->dev, "fts_i2c_read error, reg: 0x%x\n", FTS_REG_THRES);
650                 goto err_request_irq;
651         }
652         ret = request_threaded_irq(ts->irq, NULL, fts_interrupt, IRQF_TRIGGER_FALLING, client->dev.driver->name, ts);
653         if (ret < 0) {
654                 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
655                 goto err_request_irq;
656         }
657         
658         dev_info(ts->dev, "%s ok, i2c addr: 0x%x, report rate: %dHz, active thres: %d\n", 
659                 __func__, ts->client->addr, buf[0]*10, buf[1]*4);
660         
661         return 0;
662         
663 err_request_irq:
664 #ifdef CONFIG_HAS_EARLYSUSPEND
665         unregister_early_suspend(&ts->suspend);
666 #endif  
667 err_fts_update_config:  
668         input_unregister_device(ts->input_dev);
669 err_input_register_device:
670         input_free_device(ts->input_dev);
671 err_input_allocate_device:
672 err_i2c_write:
673         i2c_set_clientdata(client, NULL);
674         kfree(ts);
675         if (pdata->exit_platform_hw)                              
676                 pdata->exit_platform_hw();
677         return ret;
678         
679 }
680 static int __devexit fts_remove(struct i2c_client *client)
681 {
682         struct fts_data *ts = i2c_get_clientdata(client);
683
684         free_irq(ts->irq,ts);
685 #ifdef CONFIG_HAS_EARLYSUSPEND
686         unregister_early_suspend(&ts->suspend);
687 #endif 
688         input_unregister_device(ts->input_dev);
689         input_free_device(ts->input_dev);
690         i2c_set_clientdata(client, NULL);
691         kfree(ts);
692         if(ts->platform_deinit_hw)
693                 ts->platform_deinit_hw();
694         return 0;
695 }
696 MODULE_DEVICE_TABLE(i2c, fts_idtable);
697
698 static struct i2c_driver fts_driver  = {
699         .driver = {
700                 .owner  = THIS_MODULE,
701                 .name   = FTS_NAME
702         },
703         .id_table       = fts_idtable,
704         .probe          = fts_probe,
705         .remove         = __devexit_p(fts_remove),
706 };
707
708 static int __init fts_ts_init(void)
709 {
710         return i2c_add_driver(&fts_driver);
711 }
712
713 static void __exit fts_ts_exit(void)
714 {
715         i2c_del_driver(&fts_driver);
716 }
717
718 module_init(fts_ts_init);
719 module_exit(fts_ts_exit);
720
721 MODULE_AUTHOR("<kfx@rock-chips.com>");
722 MODULE_DESCRIPTION("FocalTech ft5x0x TouchScreen driver");
723