Merge branch 'linux-linaro-lsk-v4.4-android' of git://git.linaro.org/kernel/linux...
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / goodix_touch.c
1 /*---------------------------------------------------------------------------------------------------------
2  * driver/input/touchscreen/goodix_touch.c
3  *
4  * Copyright(c) 2010 Goodix Technology Corp. All rights reserved.      
5  * Author: Eltonny
6  * Date: 2010.11.11                                    
7  *                                                                                                         
8  *---------------------------------------------------------------------------------------------------------*/
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/time.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/earlysuspend.h>
15 #include <linux/hrtimer.h>
16 #include <linux/i2c.h>
17 #include <linux/input.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/platform_device.h>
21 //#include <mach/gpio.h>
22 //#include <plat/gpio-cfg.h>
23 //#include <plat/gpio-bank-l.h>
24 //#include <plat/gpio-bank-f.h>
25 #include <linux/irq.h>
26 #include <linux/gpio.h>
27 #include <mach/iomux.h>
28 #include <linux/goodix_touch.h>
29 #include <linux/goodix_queue.h>
30
31 #ifndef GUITAR_GT80X
32 #error The code does not match the hardware version.
33 #endif
34
35 static struct workqueue_struct *goodix_wq;
36
37 /********************************************
38 *       管理当前手指状态的伪队列,对当前手指根据时间顺序排序
39 *       适用于Guitar小屏           */
40 static struct point_queue  finger_list; //record the fingers list 
41 /*************************************************/
42
43 const char *rk29_ts_name = "Goodix TouchScreen of GT80X";
44 /*used by guitar_update module */
45 struct i2c_client * i2c_connect_client = NULL;
46 EXPORT_SYMBOL(i2c_connect_client);
47         
48 #ifdef CONFIG_HAS_EARLYSUSPEND
49 static void goodix_ts_early_suspend(struct early_suspend *h);
50 static void goodix_ts_late_resume(struct early_suspend *h);
51 #endif
52
53 /*read the gt80x register ,used i2c bus*/
54 static int gt80x_read_regs(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
55 {
56         int ret;
57         ret =i2c_master_reg8_recv(client, reg, buf, len, 200*1000);
58         if(ret < 0)
59                 dev_err(&client->dev,"i2c_read fail =%d\n",ret);
60         return ret;
61 }
62 /* set the gt80x registe,used i2c bus*/
63 static int gt80x_write_regs(struct i2c_client *client, u8 reg, u8 const buf[], unsigned short len)
64 {
65         int ret;
66         ret = i2c_master_reg8_send(client,reg, buf, len, 200*1000);
67         if (ret < 0) {
68                 dev_err(&client->dev,"i2c_write fail =%d\n",ret);
69     }
70         return ret;
71 }
72 #if 0
73 /*******************************************************        
74 功能:       
75         读取从机数据
76         每个读操作用两条i2c_msg组成,第1条消息用于发送从机地址,
77         第2条用于发送读取地址和取回数据;每条消息前发送起始信号
78 参数:
79         client: i2c设备,包含设备地址
80         buf[0]:       首字节为读取地址
81         buf[1]~buf[len]:数据缓冲区
82         len:  读取数据长度
83 return:
84         执行消息数
85 *********************************************************/
86 /*Function as i2c_master_send */
87 static int i2c_read_bytes(struct i2c_client *client, uint8_t *buf, int len)
88 {
89         struct i2c_msg msgs[2];
90         int ret=-1;
91         //发送写地址
92         msgs[0].flags=!I2C_M_RD;//写消息
93         msgs[0].addr=client->addr;
94         msgs[0].len=1;
95         msgs[0].buf=&buf[0];
96         //接收数据
97         msgs[1].flags=I2C_M_RD;//读消息
98         msgs[1].addr=client->addr;
99         msgs[1].len=len-1;
100         msgs[1].buf=&buf[1];
101         
102         ret=i2c_transfer(client->adapter,msgs,2);
103         return ret;
104 }
105
106 /*******************************************************        
107 功能:
108         向从机写数据
109 参数:
110         client: i2c设备,包含设备地址
111         buf[0]:       首字节为写地址
112         buf[1]~buf[len]:数据缓冲区
113         len:  数据长度    
114 return:
115         执行消息数
116 *******************************************************/
117 /*Function as i2c_master_send */
118 static int i2c_write_bytes(struct i2c_client *client,uint8_t *data,int len)
119 {
120         struct i2c_msg msg;
121         int ret=-1;
122         //发送设备地址
123         msg.flags=!I2C_M_RD;//写消息
124         msg.addr=client->addr;
125         msg.len=len;
126         msg.buf=data;           
127         
128         ret=i2c_transfer(client->adapter,&msg,1);
129         return ret;
130 }
131 #endif
132 /*******************************************************
133 功能:
134         Guitar初始化函数,用于发送配置信息,获取版本信息
135 参数:
136         ts:     client私有数据结构体
137 return:
138         执行结果码,0表示正常执行
139 *******************************************************/
140 static int goodix_init_panel(struct goodix_ts_data *ts)
141 {
142         int i, ret = -1;
143         u8 start_reg = 0x30;
144         u8 buf[53];
145         uint8_t config_info[53] = {
146                 0x13,0x05,0x04,0x28,0x02,0x14,0x14,0x10,0x50,0xBA,
147                 0x14,0x00,0x1E,0x00,0x01,0x23,0x45,0x67,0x89,0xAB,
148                 0xCD,0xE1,0x00,0x00,0x00,0x00,0x4D,0xC1,0x20,0x01,
149                 0x01,0x83,0x50,0x3C,0x1E,0xB4,0x00,0x33,0x2C,0x01,
150                 0xEC,0x3C,0x64,0x32,0x71,0x00,0x00,0x00,0x00,0x00,
151                 0x00,0x00,0x01
152         };
153
154         ret = gt80x_write_regs(ts->client, start_reg, config_info, 53);
155         if (ret < 0) {
156                 printk("download gt80x firmware err %d\n", ret);
157                 goto error_i2c_transfer;
158         }
159         msleep(10);
160 #if 0
161         ret = gt80x_read_regs(ts->client, start_reg, buf, 53);
162         if (ret < 0)
163         {
164                 printk("\n--%s--Read Register values error !!!\n",__FUNCTION__);
165         }
166                 
167         for (i = 0; i < 53; i++)
168         {
169                 if (buf[i] != config_info[i])
170                 {
171                         dev_err(&ts->client->dev,"may be i2c errorat reg_add[%d] = %#x current value = %#x expect value = %#x\n",
172                                          i, 0x30 + i, buf[i], config_info[i]);
173                         break;
174                 }
175         }
176 #endif
177         return 0;
178
179 error_i2c_transfer:
180         return ret;
181 }
182 /*读取GT80X的版本号并打印*/
183 static int  goodix_read_version(struct goodix_ts_data *ts)
184 {
185         int ret;
186         uint8_t reg1 = 0x69;
187         uint8_t reg2 = 0x6a;
188         //uint8_t version[2]={0x69,0xff};       //command of reading Guitar's version 
189         uint8_t version[1]={0xff};      //command of reading Guitar's version 
190         //uint8_t version_data[41];             //store touchscreen version infomation
191         uint8_t version_data[40];               //store touchscreen version infomation
192         memset(version_data, 0 , sizeof(version_data));
193         //version_data[0]=0x6A;
194         //ret=i2c_write_bytes(ts->client,version,2);
195         ret = gt80x_write_regs(ts->client, reg1, version, 1);
196         if (ret < 0) 
197                 goto error_i2c_version;
198         msleep(16);
199
200         //ret=i2c_read_bytes(ts->client,version_data, 40);
201         ret = gt80x_read_regs(ts->client, reg2, version_data, 40);
202         if (ret < 0) 
203                 goto error_i2c_version;
204         //dev_info(&ts->client->dev," Guitar Version: %s\n", &version_data[1]);
205         printk("%s: Guitar Version: %s\n", __func__, version_data);
206
207         version[0] = 0x00;                              //cancel the command
208         //i2c_write_bytes(ts->client, version, 2);
209         ret = gt80x_write_regs(ts->client, reg1, version, 1);
210         return 0;
211         
212 error_i2c_version:
213         return ret;
214 }
215
216 /*******************************************************        
217 功能:
218         触摸屏工作函数
219         由中断触发,接受1组坐标数据,校验后再分析输出
220 参数:
221         ts:     client私有数据结构体
222 return:
223         执行结果码,0表示正常执行
224 ********************************************************/
225 static void goodix_ts_work_func(struct work_struct *work)
226 {       
227         static uint8_t finger_bit = 0;  //last time fingers' state
228         uint8_t read_position = 0;
229         uint8_t point_data[35] = {0};
230         uint8_t finger = 0;                             //record which finger is changed
231         int ret = -1; 
232         int count = 0;
233         int check_sum = 0;
234         //unsigned char start_reg = 0x00;
235         struct goodix_ts_data *ts = container_of(work, struct goodix_ts_data, work);
236         struct goodix_i2c_rmi_platform_data *pdata = ts->client->dev.platform_data;
237
238 //#ifdef SHUTDOWN_PORT
239         if (pdata && pdata->shutdown_pin) {     
240                 //if (gpio_get_value(SHUTDOWN_PORT))
241                 if (gpio_get_value(pdata->shutdown_pin))
242                 {
243                         //printk(KERN_ALERT  "Guitar stop working.The data is invalid. \n");
244                         goto NO_ACTION;
245                 }
246         }
247 //#endif
248
249         //if i2c transfer is failed, let it restart less than 10 times
250         if (ts->retry > 9) {
251                 if(!ts->use_irq && (ts->timer.state != HRTIMER_STATE_INACTIVE))         
252                         hrtimer_cancel(&ts->timer);
253                 dev_info(&(ts->client->dev), "Because of transfer error, %s stop working.\n",rk29_ts_name);
254                 return ;
255         }
256         if(ts->bad_data) 
257                 msleep(16);
258
259         ret = gt80x_read_regs(ts->client, point_data[0], &point_data[1], 34);
260         if(ret <= 0)    
261         {
262                 //dev_err(&(ts->client->dev),"I2C transfer error. Number:%d\n ", ret);
263                 ts->bad_data = 1;
264                 ts->retry++;
265                 if(ts->power)
266                 {
267                         ts->power(ts, 0);
268                         ts->power(ts, 1);
269                 }
270                 else
271                 {
272                         goodix_init_panel(ts);
273                         msleep(500);
274                 }
275                 goto XFER_ERROR;
276         }       
277         ts->bad_data = 0; 
278         
279         //The bit indicate which fingers pressed down
280         switch (point_data[1] & 0x1f)
281         {
282                 case 0:
283                 case 1:
284                         for (count = 1; count < 8; count++)
285                                 check_sum += (int)point_data[count];
286                         if ((check_sum % 256) != point_data[8])
287                                 goto XFER_ERROR;
288                         break;
289                 case 2:
290                 case 3:
291                         for ( count = 1; count < 13; count++)
292                                 check_sum += (int)point_data[count];
293                         if ((check_sum % 256) != point_data[13])
294                                 goto XFER_ERROR;
295                         break;  
296                 default:                //(point_data[1]& 0x1f) > 3
297                         for (count = 1; count < 34; count++)
298                                 check_sum += (int)point_data[count];
299                         if ((check_sum % 256) != point_data[34])
300                                 goto XFER_ERROR;
301         }
302         
303         point_data[1] &= 0x1f;
304         finger = finger_bit ^ point_data[1];
305         if (finger == 0 && point_data[1] == 0)                  
306                 goto NO_ACTION;                                         //no fingers and no action
307         else if(finger == 0)                                                    //the same as last time
308                 goto BIT_NO_CHANGE;                                     
309         //check which point(s) DOWN or UP
310         for (count = 0; (finger != 0) && (count < MAX_FINGER_NUM);  count++)
311         {
312                 if ((finger & 0x01) == 1)               //current bit is 1, so NO.postion finger is change
313                 {                                                       
314                         if (((finger_bit >> count) & 0x01) ==1 )        //NO.postion finger is UP
315                                 set_up_point(&finger_list, count);
316                         else 
317                                 add_point(&finger_list, count);
318                 }
319                 finger >>= 1;
320         }
321
322 BIT_NO_CHANGE:
323         for(count = 0; count < finger_list.length; count++)
324         {       
325                 if(finger_list.pointer[count].state == FLAG_UP)
326                 {
327                         finger_list.pointer[count].x = finger_list.pointer[count].y = 0;
328                         finger_list.pointer[count].pressure = 0;
329                         continue;
330                 }
331                 
332                 if(finger_list.pointer[count].num < 3)
333                         read_position = finger_list.pointer[count].num*5 + 3;
334                 else if (finger_list.pointer[count].num == 4)
335                         read_position = 29;
336
337                 if(finger_list.pointer[count].num != 3)
338                 {
339                         finger_list.pointer[count].x = (unsigned int) (point_data[read_position]<<8) + (unsigned int)( point_data[read_position+1]);
340                         finger_list.pointer[count].y = (unsigned int)(point_data[read_position+2]<<8) + (unsigned int) (point_data[read_position+3]);
341                         finger_list.pointer[count].pressure = (unsigned int) (point_data[read_position+4]);
342                 }
343                 else 
344                 {
345                         finger_list.pointer[count].x = (unsigned int) (point_data[18]<<8) + (unsigned int)( point_data[25]);
346                         finger_list.pointer[count].y = (unsigned int)(point_data[26]<<8) + (unsigned int) (point_data[27]);
347                         finger_list.pointer[count].pressure = (unsigned int) (point_data[28]);
348                 }
349
350                 // 将触摸屏的坐标映射到LCD坐标上. 触摸屏短边为X轴,LCD坐标一般长边为X轴,可能需要调整原点位置
351                 finger_list.pointer[count].x = (TOUCH_MAX_WIDTH - finger_list.pointer[count].x)*SCREEN_MAX_WIDTH/TOUCH_MAX_WIDTH;//y
352                 finger_list.pointer[count].y =  finger_list.pointer[count].y*SCREEN_MAX_HEIGHT/TOUCH_MAX_HEIGHT ;                               //x
353                 gt80xy_swap(finger_list.pointer[count].x, finger_list.pointer[count].y); 
354
355                 //printk("%s: dx = %d dy = %d\n", __func__,
356                 //         finger_list.pointer[count].x, finger_list.pointer[count].y);
357
358         }
359
360 #ifndef GOODIX_MULTI_TOUCH      
361                 if(finger_list.pointer[0].state == FLAG_DOWN)
362                 {
363                         input_report_abs(ts->input_dev, ABS_X, finger_list.pointer[0].x);
364                         input_report_abs(ts->input_dev, ABS_Y, finger_list.pointer[0].y);       
365                 } 
366                 input_report_abs(ts->input_dev, ABS_PRESSURE, pressure[0]);
367                 input_report_key(ts->input_dev, BTN_TOUCH, finger_list.pointer[0].state);   
368 #else
369
370         /* ABS_MT_TOUCH_MAJOR is used as ABS_MT_PRESSURE in android. */
371         for(count = 0; count < (finger_list.length); count++)
372         {
373                 if(finger_list.pointer[count].state == FLAG_DOWN)
374                 {
375                         input_report_abs(ts->input_dev, ABS_MT_POSITION_X, finger_list.pointer[count].x);
376                         input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, finger_list.pointer[count].y);
377                 } 
378                 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, finger_list.pointer[count].pressure);
379                 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, finger_list.pointer[count].pressure);
380                 input_mt_sync(ts->input_dev);   
381         }
382         #if 0
383         read_position = finger_list.length-1;
384         if(finger_list.length > 0 && finger_list.pointer[read_position].state == FLAG_DOWN)
385         {
386                 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, finger_list.pointer[read_position].x);
387                 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, finger_list.pointer[read_position].y);
388         }       
389         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, finger_list.pointer[read_position].pressure);
390         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, finger_list.pointer[read_position].pressure);
391         input_mt_sync(ts->input_dev);
392         #endif  
393 #endif
394         input_sync(ts->input_dev);
395
396         del_point(&finger_list);
397         finger_bit=point_data[1];
398         
399 XFER_ERROR:     
400 NO_ACTION:
401         if(ts->use_irq)
402                 enable_irq(ts->client->irq);
403
404 }
405
406 /*******************************************************        
407 功能:
408         计时器响应函数
409         由计时器触发,调度触摸屏工作函数运行;之后重新计时
410 参数:
411         timer:函数关联的计时器        
412 return:
413         计时器工作模式,HRTIMER_NORESTART表示不需要自动重启
414 ********************************************************/
415 static enum hrtimer_restart goodix_ts_timer_func(struct hrtimer *timer)
416 {
417         struct goodix_ts_data *ts = container_of(timer, struct goodix_ts_data, timer);
418         queue_work(goodix_wq, &ts->work);
419         if(ts->timer.state != HRTIMER_STATE_INACTIVE)
420                 hrtimer_start(&ts->timer, ktime_set(0, 16000000), HRTIMER_MODE_REL);
421         return HRTIMER_NORESTART;
422 }
423
424 /*******************************************************        
425 功能:
426         中断响应函数
427         由中断触发,调度触摸屏处理函数运行
428 参数:
429         timer:函数关联的计时器        
430 return:
431         计时器工作模式,HRTIMER_NORESTART表示不需要自动重启
432 ********************************************************/
433 //#if defined(INT_PORT)
434 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
435 {
436         struct goodix_ts_data *ts = dev_id;
437         
438         disable_irq_nosync(ts->client->irq);
439         queue_work(goodix_wq, &ts->work);
440         
441         return IRQ_HANDLED;
442 }
443 //#endif
444
445 /*******************************************************        
446 功能:
447         GT80X的电源管理
448 参数:
449         on:设置GT80X运行模式,0为进入Sleep模式
450 return:
451         是否设置成功,小于0表示设置失败
452 ********************************************************/
453 //#if defined(SHUTDOWN_PORT)
454 static int goodix_ts_power(struct goodix_ts_data * ts, int on)
455 {
456         int ret = -1;
457         struct goodix_i2c_rmi_platform_data *pdata = NULL;
458
459         if(ts == NULL || (ts && !ts->use_shutdown))
460                 return -1;
461
462         pdata = ts->client->dev.platform_data;
463
464         switch(on) 
465         {
466                 case 0:
467                         gpio_set_value(pdata->shutdown_pin, 1);
468                         msleep(5);
469                         if(gpio_get_value(pdata->shutdown_pin)) //has been suspend
470                                 ret = 0;
471                         break;
472                 case 1:
473                         gpio_set_value(pdata->shutdown_pin, 0);
474                         msleep(5);
475                         if(gpio_get_value(pdata->shutdown_pin)) //has been suspend
476                                 ret = -1;
477                         else 
478                         {
479                                 ret = goodix_init_panel(ts);
480                 if(!ret)
481                                         msleep(500);
482                         }
483                         break;  
484                 //default:printk("Command ERROR.\n");
485         }
486         printk(on?"Set Guitar's Shutdown LOW\n":"Set Guitar's Shutdown HIGH\n");
487         return 0;
488 }
489 //#endif
490
491 /*******************************************************        
492 功能:
493         触摸屏探测函数
494         在注册驱动时调用(要求存在对应的client);
495         用于IO,中断等资源申请;设备注册;触摸屏初始化等工作
496 参数:
497         client:待驱动的设备结构体
498         id:设备ID
499 return:
500         执行结果码,0表示正常执行
501 ********************************************************/
502 static int goodix_ts_probe(struct i2c_client *client, const struct i2c_device_id *id)
503 {
504         struct goodix_ts_data *ts;
505         int ret = 0;
506         int retry=0;
507         int count=0;
508         u8 test_reg = 0x30;
509         u8 test_buf[] = {0x01};
510
511         struct goodix_i2c_rmi_platform_data *pdata;
512         pdata = client->dev.platform_data;
513
514         dev_dbg(&client->dev,"Install touchscreen driver for guitar.\n");
515
516         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 
517         {
518                 dev_err(&client->dev, "System need I2C function.\n");
519                 ret = -ENODEV;
520                 goto err_check_functionality_failed;
521         }
522         
523         ts = kzalloc(sizeof(*ts), GFP_KERNEL);
524         if (ts == NULL) {
525                 ret = -ENOMEM;
526                 goto err_alloc_data_failed;
527         }
528         //Check I2C function
529 //#ifdef SHUTDOWN_PORT  
530         if (pdata && pdata->shutdown_pin) {
531                 //ret = gpio_request(SHUTDOWN_PORT, "TS_SHUTDOWN");     //Request IO
532                 ret = gpio_request(pdata->shutdown_pin, "TS_SHUTDOWN"); //Request IO
533                 if (ret < 0) 
534                 {
535                         //printk(KERN_ALERT "Failed to request GPIO:%d, ERRNO:%d\n",(int)SHUTDOWN_PORT,ret);
536                         printk(KERN_ALERT "Failed to request GPIO:%d, ERRNO:%d\n",(int)pdata->shutdown_pin,ret);
537                         goto err_gpio_request;
538                 }       
539                 //gpio_direction_output(SHUTDOWN_PORT, 0);      //Touchscreen is waiting to wakeup
540                 //ret = gpio_get_value(SHUTDOWN_PORT);
541                 gpio_direction_output(pdata->shutdown_pin, 0);  //Touchscreen is waiting to wakeup
542                 ret = gpio_get_value(pdata->shutdown_pin);
543                 if (ret)
544                 {
545                         printk(KERN_ALERT  "Cannot set touchscreen to work.\n");
546                         goto err_i2c_failed;
547                 }
548         }
549 //#endif                
550         
551         i2c_connect_client = client;                            //used by Guitar Updating.
552         msleep(16);
553
554         for (retry = 0; retry < 5; retry++)
555         {
556                 ret = gt80x_write_regs(client, test_reg, test_buf, 1);  //Test i2c.
557                 if (ret > 0)
558                         break;
559         }
560         if (ret < 0)
561         {
562                 dev_err(&client->dev, "Warnning: I2C connection might be something wrong!\n");
563                 goto err_i2c_failed;
564         }
565
566 //#ifdef SHUTDOWN_PORT  
567         if (pdata && pdata->shutdown_pin) {
568                 ts->use_shutdown = 1;
569                 //gpio_set_value(SHUTDOWN_PORT, 1);
570                 gpio_set_value(pdata->shutdown_pin, 1);
571         }//suspend
572 //#endif        
573         
574         INIT_WORK(&ts->work, goodix_ts_work_func);
575         ts->client = client;
576         i2c_set_clientdata(client, ts);
577         //pdata = client->dev.platform_data;
578         
579         ts->input_dev = input_allocate_device();
580         if (ts->input_dev == NULL) {
581                 ret = -ENOMEM;
582                 dev_dbg(&client->dev,"Failed to allocate input device\n");
583                 goto err_input_dev_alloc_failed;
584         }
585
586         ts->input_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) ;
587 #ifndef GOODIX_MULTI_TOUCH      
588         ts->input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
589         ts->input_dev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y);
590         //      | BIT_MASK(ABS_MT_TOUCH_MAJOR)| BIT_MASK(ABS_MT_WIDTH_MAJOR)
591         //      BIT_MASK(ABS_MT_POSITION_X) |
592         //      BIT_MASK(ABS_MT_POSITION_Y);    // for android
593
594         input_set_abs_params(ts->input_dev, ABS_X, 0, SCREEN_MAX_HEIGHT, 0, 0);
595         input_set_abs_params(ts->input_dev, ABS_Y, 0, SCREEN_MAX_WIDTH, 0, 0);
596         input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 255, 0, 0);        
597 #else
598         input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
599         input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
600         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X, 0, SCREEN_MAX_HEIGHT, 0, 0);
601         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y, 0, SCREEN_MAX_WIDTH, 0, 0);      
602 #endif  
603
604         sprintf(ts->phys, "input/ts)");
605         ts->input_dev->name = rk29_ts_name;
606         ts->input_dev->phys = ts->phys;
607         ts->input_dev->id.bustype = BUS_I2C;
608         ts->input_dev->id.vendor = 0xDEAD;
609         ts->input_dev->id.product = 0xBEEF;
610         ts->input_dev->id.version = 10427;      
611
612         finger_list.length = 0;
613         ret = input_register_device(ts->input_dev);
614         if (ret) {
615                 dev_err(&client->dev,"Probe: Unable to register %s input device\n", ts->input_dev->name);
616                 goto err_input_register_device_failed;
617         }
618
619         ts->use_irq = 0;
620         ts->retry = 0;
621         ts->bad_data = 0;
622 //#if defined(INT_PORT) 
623         //client->irq=TS_INT;
624         //gpio_set_value(TS_INT, 0);            
625         
626         if (pdata && pdata->irq_pin) {
627                 client->irq = gpio_to_irq(client->irq);
628                 if (client->irq)
629                 {
630                         //ret = gpio_request(INT_PORT, "TS_INT");       //Request IO
631                         ret = gpio_request(pdata->irq_pin, "TS_INT");   //Request IO
632                         if (ret < 0) 
633                         {
634                                 //dev_err(&client->dev, "Failed to request GPIO:%d, ERRNO:%d\n",(int)INT_PORT,ret);
635                                 dev_err(&client->dev, "Failed to request GPIO:%d, ERRNO:%d\n", (int)pdata->irq_pin, ret);
636                                 goto err_int_request_failed;
637                         }
638                         //ret = s3c_gpio_cfgpin(INT_PORT, INT_CFG);     //Set IO port function
639                         ret  = request_irq(client->irq, goodix_ts_irq_handler ,  IRQ_TYPE_EDGE_RISING,
640                                 client->name, ts);
641                         if (ret != 0) {
642                                 dev_err(&client->dev,"Can't allocate touchscreen's interrupt!ERRNO:%d\n", ret);
643                                 //gpio_direction_input(INT_PORT);
644                                 //gpio_free(INT_PORT);
645                                 gpio_direction_input(pdata->irq_pin);
646                                 gpio_free(pdata->irq_pin);
647                                 goto err_int_request_failed;
648                         }
649                         else 
650                         {       
651                                 //disable_irq(TS_INT);
652                                 disable_irq(client->irq);
653                                 ts->use_irq = 1;
654                                 dev_dbg(&client->dev,"Reques EIRQ %d succesd on GPIO:%d\n",client->irq, pdata->irq_pin);
655                         }
656                 }
657         }
658 //#endif
659
660 err_int_request_failed: 
661         if (!ts->use_irq) 
662         {
663                 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
664                 ts->timer.function = goodix_ts_timer_func;
665                 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
666         }
667
668 //#ifdef SHUTDOWN_PORT          
669         if (pdata && pdata->shutdown_pin) {
670                 //gpio_set_value(SHUTDOWN_PORT, 0);
671                 gpio_set_value(pdata->shutdown_pin, 0);
672                 msleep(10);
673                 ts->power = goodix_ts_power;
674         }
675 //#endif        
676         for(count = 0; count < 3; count++)
677         {
678                 ret = goodix_init_panel(ts);    
679                 if(ret != 0)            //Initiall failed
680                         continue;
681                 else
682                 {
683                         if(ts->use_irq)
684                                 enable_irq(client->irq);
685                                 //enable_irq(TS_INT);
686                         break;
687                 }
688         }
689         if(ret != 0) {
690                 ts->bad_data = 1;
691                 goto err_init_godix_ts;
692         }
693         goodix_read_version(ts);
694         msleep(500);
695         
696 #ifdef CONFIG_HAS_EARLYSUSPEND
697         ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
698         ts->early_suspend.suspend = goodix_ts_early_suspend;
699         ts->early_suspend.resume = goodix_ts_late_resume;
700         register_early_suspend(&ts->early_suspend);
701 #endif
702         dev_info(&client->dev,"Start  %s in %s mode\n", 
703                 ts->input_dev->name, ts->use_irq ? "Interrupt" : "Polling");
704         return 0;
705
706 err_init_godix_ts:
707         if(ts->use_irq)
708         {
709                 //free_irq(TS_INT,ts);
710                 free_irq(client->irq,ts);
711 //#if defined(INT_PORT)         
712                 if (pdata && pdata->irq_pin)
713                         gpio_free(pdata->irq_pin);
714 //#endif
715         }
716
717 err_input_register_device_failed:
718         input_free_device(ts->input_dev);
719
720 err_input_dev_alloc_failed:
721         i2c_set_clientdata(client, NULL);
722 err_i2c_failed:
723 //#ifdef SHUTDOWN_PORT  
724         if (pdata && pdata->shutdown_pin) {
725                 //gpio_direction_input(SHUTDOWN_PORT);
726                 //gpio_free(SHUTDOWN_PORT);
727                 gpio_direction_input(pdata->shutdown_pin);
728                 gpio_free(pdata->shutdown_pin);
729         }
730 //#endif
731 err_gpio_request:
732         kfree(ts);
733 err_alloc_data_failed:
734 err_check_functionality_failed:
735         return ret;
736 }
737
738
739 /*******************************************************        
740 功能:
741         驱动资源释放
742 参数:
743         client:设备结构体
744 return:
745         执行结果码,0表示正常执行
746 ********************************************************/
747 static int goodix_ts_remove(struct i2c_client *client)
748 {
749         struct goodix_ts_data *ts = i2c_get_clientdata(client);
750         struct goodix_i2c_rmi_platform_data *pdata;
751         pdata = client->dev.platform_data;
752 #ifdef CONFIG_HAS_EARLYSUSPEND
753         unregister_early_suspend(&ts->early_suspend);
754 #endif
755         if (ts->use_irq)
756         {
757                 free_irq(client->irq, ts);
758 //#if defined(INT_PORT)         
759                 if (pdata && pdata->irq_pin)
760                         gpio_free(pdata->irq_pin);
761                         //gpio_free(INT_PORT);
762 //#endif
763         }       
764         else
765                 hrtimer_cancel(&ts->timer);
766
767 //#ifdef SHUTDOWN_PORT          
768         if (pdata && pdata->shutdown_pin) {
769                 if(ts->use_shutdown)
770                 {
771                         //gpio_direction_input(SHUTDOWN_PORT);
772                         //gpio_free(SHUTDOWN_PORT);
773                         gpio_direction_input(pdata->shutdown_pin);
774                         gpio_free(pdata->shutdown_pin);
775                 }
776         }
777 //#endif
778         dev_notice(&client->dev,"The driver is removing...\n");
779         i2c_set_clientdata(client, NULL);
780         input_unregister_device(ts->input_dev);
781         if(ts->input_dev)
782                 kfree(ts->input_dev);
783         kfree(ts);
784         return 0;
785 }
786
787 //停用设备
788 static int goodix_ts_suspend(struct i2c_client *client, pm_message_t mesg)
789 {
790         int ret;
791         struct goodix_ts_data *ts = i2c_get_clientdata(client);
792
793         if (ts->use_irq)
794                 disable_irq(client->irq);
795         else if(ts->timer.state)
796                 hrtimer_cancel(&ts->timer);
797         ret = cancel_work_sync(&ts->work);      
798         if(ret && ts->use_irq)
799                 enable_irq(client->irq);
800         if (ts->power) {
801                 ret = ts->power(ts,0);
802                 if (ret < 0)
803                         printk(KERN_ERR "%s power off failed\n", rk29_ts_name);
804         }
805         return 0;
806 }
807 //重新唤醒
808 static int goodix_ts_resume(struct i2c_client *client)
809 {
810         int ret;
811         struct goodix_ts_data *ts = i2c_get_clientdata(client);
812
813         if (ts->power) {
814                 ret = ts->power(ts, 1);
815                 if (ret < 0)
816                         printk(KERN_ERR "%s power on failed\n", rk29_ts_name);
817         }
818
819         if (ts->use_irq)
820                 enable_irq(client->irq);
821         else
822                 hrtimer_start(&ts->timer, ktime_set(1, 0), HRTIMER_MODE_REL);
823         return 0;
824 }
825
826 #ifdef CONFIG_HAS_EARLYSUSPEND
827 static void goodix_ts_early_suspend(struct early_suspend *h)
828 {
829         struct goodix_ts_data *ts;
830         ts = container_of(h, struct goodix_ts_data, early_suspend);
831         goodix_ts_suspend(ts->client, PMSG_SUSPEND);
832 }
833
834 static void goodix_ts_late_resume(struct early_suspend *h)
835 {
836         struct goodix_ts_data *ts;
837         ts = container_of(h, struct goodix_ts_data, early_suspend);
838         goodix_ts_resume(ts->client);
839 }
840 #endif
841
842 //可用于该驱动的 设备名—设备ID 列表
843 //only one client
844 static const struct i2c_device_id goodix_ts_id[] = {
845         { GOODIX_I2C_NAME, 0 },
846         { }
847 };
848
849 //设备驱动结构体
850 static struct i2c_driver goodix_ts_driver = {
851         .probe          = goodix_ts_probe,
852         .remove         = goodix_ts_remove,
853 #ifndef CONFIG_HAS_EARLYSUSPEND
854         .suspend        = goodix_ts_suspend,
855         .resume         = goodix_ts_resume,
856 #endif
857         .id_table       = goodix_ts_id,
858         .driver = {
859                 .name   = GOODIX_I2C_NAME,
860                 .owner = THIS_MODULE,
861         },
862 };
863
864 /*******************************************************        
865 功能:
866         驱动加载函数
867 return:
868         执行结果码,0表示正常执行
869 ********************************************************/
870 static int __devinit goodix_ts_init(void)
871 {
872         int ret;
873         //printk(KERN_DEBUG "%s is installing...\n", rk29_ts_name);
874         goodix_wq = create_workqueue("goodix_wq");
875         if (!goodix_wq) {
876                 printk(KERN_ALERT "Creat workqueue faiked\n");
877                 return -ENOMEM;
878                 
879         }
880         ret=i2c_add_driver(&goodix_ts_driver);
881         return ret; 
882 }
883
884 /*******************************************************        
885 功能:
886         驱动卸载函数
887 参数:
888         client:设备结构体
889 ********************************************************/
890 static void __exit goodix_ts_exit(void)
891 {
892         printk(KERN_DEBUG "%s is exiting...\n", rk29_ts_name);
893         i2c_del_driver(&goodix_ts_driver);
894         if (goodix_wq)
895                 destroy_workqueue(goodix_wq);
896 }
897
898 late_initcall(goodix_ts_init);
899 module_exit(goodix_ts_exit);
900
901 MODULE_DESCRIPTION("Goodix Touchscreen Driver");
902 MODULE_LICENSE("GPL");