ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / gslx680_pad.c
1 /*
2  * drivers/input/touchscreen/gslX680.c
3  *
4  * Copyright (c) 2012 Shanghai Basewin
5  *      Guan Yuwei<guanyuwei@basewin.com>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  */
11
12
13 #include <linux/module.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/hrtimer.h>
17 #include <linux/i2c.h>
18 #include <linux/input.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/platform_device.h>
22 #include <linux/async.h>
23 #include <linux/irq.h>
24 #include <linux/workqueue.h>
25 #include <linux/proc_fs.h>
26 #include <linux/input/mt.h>
27
28 #include <linux/version.h>
29 #include <linux/slab.h>
30 #include <linux/of_gpio.h>
31 #include "tp_suspend.h"
32
33 #include "gslx680_pad.h"
34 #include <linux/wakelock.h>
35 //#define GSL_DEBUG
36 #define REPORT_DATA_ANDROID_4_0
37 #define SLEEP_CLEAR_POINT
38 //#define FILTER_POINT
39 #ifdef FILTER_POINT
40 #define FILTER_MAX      9
41 #endif
42
43 #define GSLX680_I2C_NAME        "gslX680-pad"
44 #define GSLX680_I2C_ADDR        0x40
45
46 int g_wake_pin=0;
47 int g_irq_pin=0;
48
49 #define GSL_DATA_REG            0x80
50 #define GSL_STATUS_REG          0xe0
51 #define GSL_PAGE_REG            0xf0
52
53 #define TPD_PROC_DEBUG
54 #ifdef TPD_PROC_DEBUG
55 #include <linux/proc_fs.h>
56 #include <asm/uaccess.h>
57 #include <linux/seq_file.h>  //lzk
58 //static struct proc_dir_entry *gsl_config_proc = NULL;
59 #define GSL_CONFIG_PROC_FILE "gsl_config"
60 #define CONFIG_LEN 31
61 static char gsl_read[CONFIG_LEN];
62 static u8 gsl_data_proc[8] = {0};
63 static u8 gsl_proc_flag = 0;
64
65 #endif
66
67 #define PRESS_MAX                       255
68 #define MAX_FINGERS             10
69 #define MAX_CONTACTS            10
70 #define DMA_TRANS_LEN           0x20
71 static struct i2c_client *gsl_client = NULL;
72
73 struct gsl_ts_data {
74     u8 x_index;
75     u8 y_index;
76     u8 z_index;
77     u8 id_index;
78     u8 touch_index;
79     u8 data_reg;
80     u8 status_reg;
81     u8 data_size;
82     u8 touch_bytes;
83     u8 update_data;
84     u8 touch_meta_data;
85     u8 finger_size;
86 };
87
88 static struct gsl_ts_data devices[] = {
89     {
90         .x_index = 6,
91         .y_index = 4,
92         .z_index = 5,
93         .id_index = 7,
94         .data_reg = GSL_DATA_REG,
95         .status_reg = GSL_STATUS_REG,
96         .update_data = 0x4,
97         .touch_bytes = 4,
98         .touch_meta_data = 4,
99         .finger_size = 70,
100     },
101 };
102
103 struct gsl_ts {
104     struct i2c_client *client;
105     struct input_dev *input;
106     struct work_struct work;
107     struct workqueue_struct *wq;
108     struct gsl_ts_data *dd;
109     u8 *touch_data;
110     u8 device_id;
111     int irq;
112     int irq_pin;
113     int wake_pin;
114     struct  tp_device  tp;
115     int screen_max_x;
116     int screen_max_y;
117 };
118
119 #ifdef GSL_DEBUG
120 #define print_info(fmt, args...)   \
121     do{                              \
122         printk(fmt, ##args);     \
123     }while(0)
124 #else
125 #define print_info(fmt, args...)
126 #endif
127
128
129 static u32 id_sign[MAX_CONTACTS+1] = {0};
130 static u8 id_state_flag[MAX_CONTACTS+1] = {0};
131 static u8 id_state_old_flag[MAX_CONTACTS+1] = {0};
132 static u16 x_old[MAX_CONTACTS+1] = {0};
133 static u16 y_old[MAX_CONTACTS+1] = {0};
134 static u16 x_new = 0;
135 static u16 y_new = 0;
136
137
138
139 static int gslX680_init(void)
140 {
141     gpio_set_value(g_wake_pin,1);
142     gpio_set_value(g_irq_pin,1);
143     return 0;
144 }
145
146 static int gslX680_shutdown_low(void)
147 {
148     if(g_wake_pin !=0)
149     {
150         gpio_direction_output(g_wake_pin, 0);
151         gpio_set_value(g_wake_pin,0);
152     }
153     return 0;
154 }
155
156 static int gslX680_shutdown_high(void)
157 {
158     if(g_wake_pin !=0)
159     {
160         gpio_direction_output(g_wake_pin, 0);
161         gpio_set_value(g_wake_pin,1);
162     }
163     return 0;
164 }
165
166 static inline u16 join_bytes(u8 a, u8 b)
167 {
168     u16 ab = 0;
169     ab = ab | a;
170     ab = ab << 8 | b;
171     return ab;
172 }
173
174 static u32 gsl_write_interface(struct i2c_client *client, const u8 reg, u8 *buf, u32 num)
175 {
176     struct i2c_msg xfer_msg[1];
177
178     buf[0] = reg;
179
180     xfer_msg[0].addr = client->addr;
181     xfer_msg[0].len = num + 1;
182     xfer_msg[0].flags = client->flags & I2C_M_TEN;
183     xfer_msg[0].buf = buf;
184     //xfer_msg[0].scl_rate = 400*1000; //RK3066 RK2926 I2C±¨´íʱ´ò¿ªÕâ¸ö
185
186     return i2c_transfer(client->adapter, xfer_msg, 1) == 1 ? 0 : -EFAULT;
187 }
188
189 static int gsl_ts_write(struct i2c_client *client, u8 addr, u8 *pdata, int datalen)
190 {
191     int ret = 0;
192     u8 tmp_buf[128];
193     unsigned int bytelen = 0;
194     if (datalen > 125)
195     {
196         dev_err(&client->dev,"%s too big datalen = %d!\n", __func__, datalen);
197         return -1;
198     }
199
200     tmp_buf[0] = addr;
201     bytelen++;
202
203     if (datalen != 0 && pdata != NULL)
204     {
205         memcpy(&tmp_buf[bytelen], pdata, datalen);
206         bytelen += datalen;
207     }
208
209     ret = i2c_master_send(client, tmp_buf, bytelen);
210     return ret;
211 }
212
213 static int gsl_ts_read(struct i2c_client *client, u8 addr, u8 *pdata, unsigned int datalen)
214 {
215     int ret = 0;
216
217     if (datalen > 126)
218     {
219         dev_err(&client->dev, "%s too big datalen = %d!\n", __func__, datalen);
220         return -1;
221     }
222
223     ret = gsl_ts_write(client, addr, NULL, 0);
224     if (ret < 0)
225     {
226         dev_err(&client->dev, "%s set data address fail!\n", __func__);
227         return ret;
228     }
229
230     return i2c_master_recv(client, pdata, datalen);
231 }
232
233 static __inline__ void fw2buf(u8 *buf, const u32 *fw)
234 {
235     u32 *u32_buf = (int *)buf;
236     *u32_buf = *fw;
237 }
238
239 static void gsl_load_fw(struct i2c_client *client)
240 {
241     u8 buf[DMA_TRANS_LEN*4 + 1] = {0};
242     u8 send_flag = 1;
243     u8 *cur = buf + 1;
244     u32 source_line = 0;
245     u32 source_len;
246     struct fw_data *ptr_fw;
247
248     ptr_fw = GSLX680_FW;
249     source_len = ARRAY_SIZE(GSLX680_FW);
250
251     for (source_line = 0; source_line < source_len; source_line++)
252     {
253         /* init page trans, set the page val */
254         if (GSL_PAGE_REG == ptr_fw[source_line].offset)
255         {
256             fw2buf(cur, &ptr_fw[source_line].val);
257             gsl_write_interface(client, GSL_PAGE_REG, buf, 4);
258             //i2c_smbus_write_i2c_block_data(client, GSL_PAGE_REG,4, buf);
259             send_flag = 1;
260         }
261         else
262         {
263             if (1 == send_flag % (DMA_TRANS_LEN < 0x20 ? DMA_TRANS_LEN : 0x20))
264                 buf[0] = (u8)ptr_fw[source_line].offset;
265
266             fw2buf(cur, &ptr_fw[source_line].val);
267             cur += 4;
268
269             if (0 == send_flag % (DMA_TRANS_LEN < 0x20 ? DMA_TRANS_LEN : 0x20))
270             {
271                 gsl_write_interface(client, buf[0], buf, cur - buf - 1);
272                 //i2c_smbus_write_i2c_block_data(client, buf[0], cur - buf - 1, buf);
273                 cur = buf + 1;
274             }
275
276             send_flag++;
277         }
278     }
279 }
280
281 static int test_i2c(struct i2c_client *client)
282 {
283     u8 read_buf = 0;
284     u8 write_buf = 0x12;
285     int ret, rc = 1;
286
287     ret = gsl_ts_read( client, 0xf0, &read_buf, sizeof(read_buf) );
288     if  (ret  < 0)
289         rc --;
290     else
291         dev_info(&client->dev, "I read reg 0xf0 is %x\n", read_buf);
292
293     mdelay(2);
294     ret = gsl_ts_write(client, 0xf0, &write_buf, sizeof(write_buf));
295     if(ret  >=  0 )
296         dev_info(&client->dev, "I write reg 0xf0 0x12\n");
297
298     mdelay(2);
299     ret = gsl_ts_read( client, 0xf0, &read_buf, sizeof(read_buf) );
300     if(ret <  0 )
301         rc --;
302     else
303         dev_info(&client->dev, "I read reg 0xf0 is 0x%x\n", read_buf);
304
305     return rc;
306 }
307
308
309 static void startup_chip(struct i2c_client *client)
310 {
311     u8 tmp = 0x00;
312
313 #ifdef GSL_NOID_VERSION
314     gsl_DataInit(gsl_config_data_id);
315 #endif
316     gsl_ts_write(client, 0xe0, &tmp, 1);
317 }
318
319 static void reset_chip(struct i2c_client *client)
320 {
321     u8 tmp = 0x88;
322     u8 buf[4] = {0x00};
323
324     gsl_ts_write(client, 0xe0, &tmp, sizeof(tmp));
325     mdelay(5);
326     tmp = 0x04;
327     gsl_ts_write(client, 0xe4, &tmp, sizeof(tmp));
328     mdelay(5);
329     gsl_ts_write(client, 0xbc, buf, sizeof(buf));
330 }
331
332 static void clr_reg(struct i2c_client *client)
333 {
334     u8 write_buf[4]     = {0};
335
336     write_buf[0] = 0x88;
337     gsl_ts_write(client, 0xe0, &write_buf[0], 1);
338     msleep(20);
339     write_buf[0] = 0x03;
340     gsl_ts_write(client, 0x80, &write_buf[0], 1);
341     msleep(5);
342     write_buf[0] = 0x04;
343     gsl_ts_write(client, 0xe4, &write_buf[0], 1);
344     msleep(5);
345     write_buf[0] = 0x00;
346     gsl_ts_write(client, 0xe0, &write_buf[0], 1);
347     msleep(20);
348 }
349
350 static void init_chip(struct i2c_client *client)
351 {
352     int rc;
353     gslX680_shutdown_low();
354     mdelay(20);
355     gslX680_shutdown_high();
356     gpio_set_value(g_irq_pin,1);
357     msleep(20);
358     rc = test_i2c(client);
359     if(rc < 0)
360     {
361         dev_err(&client->dev, "------gslX680 test_i2c error------\n");
362         return;
363     }
364         clr_reg(client);
365         reset_chip(client);
366         gsl_load_fw(client);
367         startup_chip(client);
368         reset_chip(client);
369         startup_chip(client);
370 }
371
372 static void check_mem_data(struct i2c_client *client)
373 {
374     u8 read_buf[4]  = {0};
375
376     gsl_ts_read(client,0xb0, read_buf, sizeof(read_buf));
377
378     if (read_buf[3] != 0x5a || read_buf[2] != 0x5a || read_buf[1] != 0x5a || read_buf[0] != 0x5a)
379     {
380         dev_info(&client->dev, "#########check mem read 0xb0 = %x %x %x %x #########\n", read_buf[3], read_buf[2], read_buf[1], read_buf[0]);
381         init_chip(client);
382     }
383 }
384
385
386 #ifdef TPD_PROC_DEBUG
387 static int char_to_int(char ch)
388 {
389     if(ch>='0' && ch<='9')
390         return (ch-'0');
391     else
392         return (ch-'a'+10);
393 }
394
395
396 static int gsl_read_interface(struct i2c_client *client, u8 reg, u8 *buf, u32 num)
397 {
398
399     int err = 0;
400     u8 temp = reg;
401     //mutex_lock(&gsl_i2c_lock);
402     if(temp < 0x80)
403     {
404         temp = (temp+8)&0x5c;
405         i2c_master_send(client,&temp,1);
406         err = i2c_master_recv(client,&buf[0],4);
407
408         temp = reg;
409         i2c_master_send(client,&temp,1);
410         err = i2c_master_recv(client,&buf[0],4);
411     }
412     i2c_master_send(client,&reg,1);
413     err = i2c_master_recv(client,&buf[0],num);
414     //mutex_unlock(&gsl_i2c_lock);
415     return err;
416 }
417
418 static int gsl_config_read_proc(struct seq_file *m,void *v)
419 {
420     //char *ptr = page;
421     char temp_data[5] = {0};
422     unsigned int tmp=0;
423
424     if('v'==gsl_read[0]&&'s'==gsl_read[1])
425     {
426 #ifdef GSL_NOID_VERSION
427         tmp=gsl_version_id();
428 #else
429         tmp=0x20121215;
430 #endif
431         seq_printf(m,"version:%x\n",tmp);
432     }
433     else if('r'==gsl_read[0]&&'e'==gsl_read[1])
434     {
435         if('i'==gsl_read[3])
436         {
437 #ifdef GSL_NOID_VERSION
438             tmp=(gsl_data_proc[5]<<8) | gsl_data_proc[4];
439 #endif
440         }
441         else
442         {
443             gsl_ts_write(gsl_client,0xf0,&gsl_data_proc[4],4);
444             gsl_read_interface(gsl_client,gsl_data_proc[0],temp_data,4);
445             seq_printf(m,"offset : {0x%02x,0x",gsl_data_proc[0]);
446             seq_printf(m,"%02x",temp_data[3]);
447             seq_printf(m,"%02x",temp_data[2]);
448             seq_printf(m,"%02x",temp_data[1]);
449             seq_printf(m,"%02x};\n",temp_data[0]);
450         }
451     }
452     return 0;
453 }
454
455 ssize_t gsl_config_write_proc(struct file *file, const char *buffer, size_t count, loff_t *data)
456 {
457     u8 buf[8] = {0};
458     char temp_buf[CONFIG_LEN];
459     char *path_buf;
460     int tmp = 0;
461     int tmp1 = 0;
462     print_info("[tp-gsl][%s] \n",__func__);
463     if(count > 512)
464     {
465         pr_err("size not match [%d:%ld]\n", CONFIG_LEN, count);
466         return -EFAULT;
467     }
468     path_buf=kzalloc(count,GFP_KERNEL);
469     if(!path_buf)
470     {
471         pr_err("alloc path_buf memory error \n");
472     }
473     if(copy_from_user(path_buf, buffer, count))
474     {
475         pr_err("copy from user fail\n");
476         goto exit_write_proc_out;
477     }
478     memcpy(temp_buf,path_buf,(count<CONFIG_LEN?count:CONFIG_LEN));
479
480     buf[3]=char_to_int(temp_buf[14])<<4 | char_to_int(temp_buf[15]);
481     buf[2]=char_to_int(temp_buf[16])<<4 | char_to_int(temp_buf[17]);
482     buf[1]=char_to_int(temp_buf[18])<<4 | char_to_int(temp_buf[19]);
483     buf[0]=char_to_int(temp_buf[20])<<4 | char_to_int(temp_buf[21]);
484
485     buf[7]=char_to_int(temp_buf[5])<<4 | char_to_int(temp_buf[6]);
486     buf[6]=char_to_int(temp_buf[7])<<4 | char_to_int(temp_buf[8]);
487     buf[5]=char_to_int(temp_buf[9])<<4 | char_to_int(temp_buf[10]);
488     buf[4]=char_to_int(temp_buf[11])<<4 | char_to_int(temp_buf[12]);
489     if('v'==temp_buf[0]&& 's'==temp_buf[1])//version //vs
490     {
491         memcpy(gsl_read,temp_buf,4);
492     }
493     else if('s'==temp_buf[0]&& 't'==temp_buf[1])//start //st
494     {
495         gsl_proc_flag = 1;
496         reset_chip(gsl_client);
497     }
498     else if('e'==temp_buf[0]&&'n'==temp_buf[1])//end //en
499     {
500         msleep(20);
501         reset_chip(gsl_client);
502         startup_chip(gsl_client);
503         gsl_proc_flag = 0;
504     }
505     else if('r'==temp_buf[0]&&'e'==temp_buf[1])//read buf //
506     {
507         memcpy(gsl_read,temp_buf,4);
508         memcpy(gsl_data_proc,buf,8);
509     }
510     else if('w'==temp_buf[0]&&'r'==temp_buf[1])//write buf
511     {
512         gsl_ts_write(gsl_client,buf[4],buf,4);
513     }
514 #ifdef GSL_NOID_VERSION
515     else if('i'==temp_buf[0]&&'d'==temp_buf[1])//write id config //
516     {
517         tmp1=(buf[7]<<24)|(buf[6]<<16)|(buf[5]<<8)|buf[4];
518         tmp=(buf[3]<<24)|(buf[2]<<16)|(buf[1]<<8)|buf[0];
519         if(tmp1>=0 && tmp1<ARRAY_SIZE(gsl_config_data_id))
520         {
521             gsl_config_data_id[tmp1] = tmp;
522         }
523     }
524 #endif
525 exit_write_proc_out:
526     kfree(path_buf);
527     return count;
528 }
529
530 static int gsl_server_list_open(struct inode *inode,struct file *file)
531 {
532     return single_open(file,gsl_config_read_proc,NULL);
533 }
534 static const struct file_operations gsl_seq_fops = {
535     .open = gsl_server_list_open,
536     .read = seq_read,
537     .release = single_release,
538     .write = gsl_config_write_proc,
539     .owner = THIS_MODULE,
540 };
541 #endif
542
543 #ifdef FILTER_POINT
544 static void filter_point(u16 x, u16 y , u8 id)
545 {
546     u16 x_err =0;
547     u16 y_err =0;
548     u16 filter_step_x = 0, filter_step_y = 0;
549
550     id_sign[id] = id_sign[id] + 1;
551     if(id_sign[id] == 1)
552     {
553         x_old[id] = x;
554         y_old[id] = y;
555     }
556
557     x_err = x > x_old[id] ? (x -x_old[id]) : (x_old[id] - x);
558     y_err = y > y_old[id] ? (y -y_old[id]) : (y_old[id] - y);
559
560     if( (x_err > FILTER_MAX && y_err > FILTER_MAX/3) || (x_err > FILTER_MAX/3 && y_err > FILTER_MAX) )
561     {
562         filter_step_x = x_err;
563         filter_step_y = y_err;
564     }
565     else
566     {
567         if(x_err > FILTER_MAX)
568             filter_step_x = x_err;
569         if(y_err> FILTER_MAX)
570             filter_step_y = y_err;
571     }
572
573     if(x_err <= 2*FILTER_MAX && y_err <= 2*FILTER_MAX)
574     {
575         filter_step_x >>= 2;
576         filter_step_y >>= 2;
577     }
578     else if(x_err <= 3*FILTER_MAX && y_err <= 3*FILTER_MAX)
579     {
580         filter_step_x >>= 1;
581         filter_step_y >>= 1;
582     }
583     else if(x_err <= 4*FILTER_MAX && y_err <= 4*FILTER_MAX)
584     {
585         filter_step_x = filter_step_x*3/4;
586         filter_step_y = filter_step_y*3/4;
587     }
588
589     x_new = x > x_old[id] ? (x_old[id] + filter_step_x) : (x_old[id] - filter_step_x);
590     y_new = y > y_old[id] ? (y_old[id] + filter_step_y) : (y_old[id] - filter_step_y);
591
592     x_old[id] = x_new;
593     y_old[id] = y_new;
594 }
595 #else
596 static void record_point(u16 x, u16 y , u8 id)
597 {
598     u16 x_err =0;
599     u16 y_err =0;
600
601     id_sign[id]=id_sign[id]+1;
602
603     if(id_sign[id]==1){
604         x_old[id]=x;
605         y_old[id]=y;
606     }
607
608     x = (x_old[id] + x)/2;
609     y = (y_old[id] + y)/2;
610
611     if(x>x_old[id]){
612         x_err=x -x_old[id];
613     }
614     else{
615         x_err=x_old[id]-x;
616     }
617
618     if(y>y_old[id]){
619         y_err=y -y_old[id];
620     }
621     else{
622         y_err=y_old[id]-y;
623     }
624
625     if( (x_err > 3 && y_err > 1) || (x_err > 1 && y_err > 3) ){
626         x_new = x;     x_old[id] = x;
627         y_new = y;     y_old[id] = y;
628     }
629     else{
630         if(x_err > 3){
631             x_new = x;     x_old[id] = x;
632         }
633         else
634             x_new = x_old[id];
635         if(y_err> 3){
636             y_new = y;     y_old[id] = y;
637         }
638         else
639             y_new = y_old[id];
640     }
641
642     if(id_sign[id]==1){
643         x_new= x_old[id];
644         y_new= y_old[id];
645     }
646
647 }
648 #endif
649
650 static void report_data(struct gsl_ts *ts, u16 x, u16 y, u8 pressure, u8 id)
651 {
652     if(revert_xy)
653         swap(x, y);
654
655     if(x > ts->screen_max_x || y > ts->screen_max_y)
656     {
657         return;
658     }
659
660     if(revert_x)
661         x = ts->screen_max_x-x-1;
662     if(revert_y)
663         y = ts->screen_max_y-y-1;
664 #ifdef REPORT_DATA_ANDROID_4_0
665     input_mt_slot(ts->input, id);
666     input_report_abs(ts->input, ABS_MT_TRACKING_ID, id);
667     input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, pressure);
668     input_report_abs(ts->input, ABS_MT_POSITION_X, x);
669     input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
670     input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 1);
671 #else
672     input_report_abs(ts->input, ABS_MT_TRACKING_ID, id);
673     input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, pressure);
674     input_report_abs(ts->input, ABS_MT_POSITION_X,x);
675     input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
676     input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 1);
677     input_mt_sync(ts->input);
678 #endif
679 }
680
681 static void gslX680_ts_worker(struct work_struct *work)
682 {
683     int rc, i;
684     u8 id, touches;
685     u16 x, y;
686 #ifdef GSL_NOID_VERSION
687     unsigned int tmp1;
688     u8 buf[4] = {0};
689     struct gsl_touch_info cinfo = {{0}};
690 #endif
691     struct gsl_ts *ts = container_of(work, struct gsl_ts,work);
692
693 #ifdef TPD_PROC_DEBUG
694     if(gsl_proc_flag == 1)
695         goto schedule;
696 #endif
697
698     rc = gsl_ts_read(ts->client, 0x80, ts->touch_data, ts->dd->data_size);
699     if (rc < 0)
700     {
701         dev_err(&ts->client->dev, "read failed\n");
702         reset_chip(ts->client);
703         startup_chip(ts->client);
704         goto schedule;
705     }
706
707     touches = ts->touch_data[ts->dd->touch_index];
708 #ifdef GSL_NOID_VERSION
709     cinfo.finger_num = touches;
710     for(i = 0; i < (touches < MAX_CONTACTS ? touches : MAX_CONTACTS); i ++)
711     {
712         cinfo.x[i] = join_bytes( ( ts->touch_data[ts->dd->x_index  + 4 * i + 1] & 0xf),
713                 ts->touch_data[ts->dd->x_index + 4 * i]);
714         cinfo.y[i] = join_bytes(ts->touch_data[ts->dd->y_index + 4 * i + 1],
715                 ts->touch_data[ts->dd->y_index + 4 * i ]);
716         cinfo.id[i] = ((ts->touch_data[ts->dd->x_index  + 4 * i + 1] & 0xf0)>>4);
717     }
718     cinfo.finger_num=(ts->touch_data[3]<<24)|(ts->touch_data[2]<<16)
719         |(ts->touch_data[1]<<8)|(ts->touch_data[0]);
720     gsl_alg_id_main(&cinfo);
721     tmp1=gsl_mask_tiaoping();
722     if(tmp1>0&&tmp1<0xffffffff)
723     {
724         buf[0]=0xa;buf[1]=0;buf[2]=0;buf[3]=0;
725         gsl_ts_write(ts->client,0xf0,buf,4);
726         buf[0]=(u8)(tmp1 & 0xff);
727         buf[1]=(u8)((tmp1>>8) & 0xff);
728         buf[2]=(u8)((tmp1>>16) & 0xff);
729         buf[3]=(u8)((tmp1>>24) & 0xff);
730         gsl_ts_write(ts->client,0x8,buf,4);
731     }
732     touches = cinfo.finger_num;
733 #endif
734
735     for(i = 1; i <= MAX_CONTACTS; i ++)
736     {
737         if(touches == 0)
738             id_sign[i] = 0;
739         id_state_flag[i] = 0;
740     }
741     for(i= 0;i < (touches > MAX_FINGERS ? MAX_FINGERS : touches);i ++)
742     {
743 #ifdef GSL_NOID_VERSION
744         {
745             id = cinfo.id[i];
746             x =  cinfo.x[i];
747             y =  cinfo.y[i];
748         }
749 #else
750         {
751             x = join_bytes( ( ts->touch_data[ts->dd->x_index  + 4 * i + 1] & 0xf),
752                     ts->touch_data[ts->dd->x_index + 4 * i]);
753             y = join_bytes(ts->touch_data[ts->dd->y_index + 4 * i + 1],
754                     ts->touch_data[ts->dd->y_index + 4 * i ]);
755             id = ts->touch_data[ts->dd->id_index + 4 * i] >> 4;
756         }
757 #endif
758
759         if(1 <=id && id <= MAX_CONTACTS)
760         {
761 #ifdef FILTER_POINT
762             filter_point(x, y ,id);
763 #else
764             record_point(x, y , id);
765 #endif
766             report_data(ts, x_new, y_new, 10, id);
767             id_state_flag[id] = 1;
768         }
769     }
770     for(i = 1; i <= MAX_CONTACTS; i ++)
771     {
772         if( (0 == touches) || ((0 != id_state_old_flag[i]) && (0 == id_state_flag[i])) )
773         {
774 #ifdef REPORT_DATA_ANDROID_4_0
775             input_mt_slot(ts->input, i);
776             input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
777             input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
778 #endif
779             id_sign[i]=0;
780         }
781         id_state_old_flag[i] = id_state_flag[i];
782     }
783     if(0 == touches)
784     {
785 #ifndef REPORT_DATA_ANDROID_4_0
786         input_mt_sync(ts->input);
787 #endif
788     }
789     input_sync(ts->input);
790
791 schedule:
792     enable_irq(ts->irq);
793
794 }
795
796 static irqreturn_t gsl_ts_irq(int irq, void *dev_id)
797 {
798     struct gsl_ts *ts = dev_id;
799
800     disable_irq_nosync(ts->irq);
801
802     if (!work_pending(&ts->work))
803     {
804         queue_work(ts->wq, &ts->work);
805     }
806
807     return IRQ_HANDLED;
808
809 }
810
811 static int gslX680_ts_init(struct i2c_client *client, struct gsl_ts *ts)
812 {
813     struct input_dev *input_device;
814     int rc = 0;
815
816     ts->dd = &devices[ts->device_id];
817
818     if (ts->device_id == 0) {
819         ts->dd->data_size = MAX_FINGERS * ts->dd->touch_bytes + ts->dd->touch_meta_data;
820         ts->dd->touch_index = 0;
821     }
822
823     ts->touch_data = kzalloc(ts->dd->data_size, GFP_KERNEL);
824     if (!ts->touch_data) {
825         pr_err("%s: Unable to allocate memory\n", __func__);
826         return -ENOMEM;
827     }
828
829     input_device = input_allocate_device();
830     if (!input_device) {
831         rc = -ENOMEM;
832         goto error_alloc_dev;
833     }
834
835     ts->input = input_device;
836     input_device->name = GSLX680_I2C_NAME;
837     input_device->id.bustype = BUS_I2C;
838     input_device->dev.parent = &client->dev;
839     input_set_drvdata(input_device, ts);
840
841 #ifdef REPORT_DATA_ANDROID_4_0
842     __set_bit(EV_ABS, input_device->evbit);
843     __set_bit(EV_KEY, input_device->evbit);
844     __set_bit(EV_REP, input_device->evbit);
845     __set_bit(INPUT_PROP_DIRECT, input_device->propbit);
846     input_mt_init_slots(input_device, (MAX_CONTACTS+1),0);
847 #else
848     input_set_abs_params(input_device,ABS_MT_TRACKING_ID, 0, (MAX_CONTACTS+1), 0, 0);
849     set_bit(EV_ABS, input_device->evbit);
850     set_bit(EV_KEY, input_device->evbit);
851     __set_bit(INPUT_PROP_DIRECT, input_device->propbit);
852     input_device->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
853 #endif
854
855     set_bit(ABS_MT_POSITION_X, input_device->absbit);
856     set_bit(ABS_MT_POSITION_Y, input_device->absbit);
857     set_bit(ABS_MT_TOUCH_MAJOR, input_device->absbit);
858     set_bit(ABS_MT_WIDTH_MAJOR, input_device->absbit);
859
860     input_set_abs_params(input_device,ABS_MT_POSITION_X, 0, ts->screen_max_x, 0, 0);
861     input_set_abs_params(input_device,ABS_MT_POSITION_Y, 0, ts->screen_max_y, 0, 0);
862     input_set_abs_params(input_device,ABS_MT_TOUCH_MAJOR, 0, PRESS_MAX, 0, 0);
863     input_set_abs_params(input_device,ABS_MT_WIDTH_MAJOR, 0, 200, 0, 0);
864
865     ts->wq = create_singlethread_workqueue("kworkqueue_ts");
866     if (!ts->wq) {
867         dev_err(&client->dev, "Could not create workqueue\n");
868         goto error_wq_create;
869     }
870     flush_workqueue(ts->wq);
871     INIT_WORK(&ts->work, gslX680_ts_worker);
872     rc = input_register_device(input_device);
873     if (rc)
874         goto error_unreg_device;
875     return 0;
876
877 error_unreg_device:
878     destroy_workqueue(ts->wq);
879 error_wq_create:
880     input_free_device(input_device);
881 error_alloc_dev:
882     kfree(ts->touch_data);
883     return rc;
884 }
885
886 static int gsl_ts_suspend(struct device *dev)
887 {
888     struct gsl_ts *ts = dev_get_drvdata(dev);
889 #ifdef REPORT_DATA_ANDROID_4_0
890     int i;
891 #endif
892
893     disable_irq_nosync(ts->irq);
894     gslX680_shutdown_low();
895
896 #ifdef SLEEP_CLEAR_POINT
897     msleep(10);
898 #ifdef REPORT_DATA_ANDROID_4_0
899     for(i = 1; i <= MAX_CONTACTS ;i ++)
900     {
901         input_mt_slot(ts->input, i);
902         input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
903         input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
904     }
905 #else
906     input_mt_sync(ts->input);
907 #endif
908     input_sync(ts->input);
909     msleep(10);
910     report_data(ts, 1, 1, 10, 1);
911     input_sync(ts->input);
912 #endif
913
914     return 0;
915 }
916
917 static int gsl_ts_resume(struct device *dev)
918 {
919     struct gsl_ts *ts = dev_get_drvdata(dev);
920 #ifdef REPORT_DATA_ANDROID_4_0
921     int i;
922 #endif
923     gslX680_shutdown_high();
924     msleep(20);
925     reset_chip(ts->client);
926     startup_chip(ts->client);
927     check_mem_data(ts->client);
928     check_mem_data(ts->client);
929
930 #ifdef SLEEP_CLEAR_POINT
931 #ifdef REPORT_DATA_ANDROID_4_0
932     for(i =1;i<=MAX_CONTACTS;i++)
933     {
934         input_mt_slot(ts->input, i);
935         input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
936         input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
937     }
938 #else
939     input_mt_sync(ts->input);
940 #endif
941     input_sync(ts->input);
942 #endif
943    enable_irq(ts->irq);
944     return 0;
945 }
946
947 static int  gsl_ts_probe(struct i2c_client *client,
948         const struct i2c_device_id *id)
949 {
950     struct device_node *np = client->dev.of_node;
951     enum of_gpio_flags wake_flags, irq_flags;
952     struct gsl_ts *ts;
953     int rc;
954
955     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
956         dev_err(&client->dev, "I2C functionality not supported\n");
957         return -ENODEV;
958     }
959
960     ts = kzalloc(sizeof(*ts), GFP_KERNEL);
961     if (!ts)
962         return -ENOMEM;
963
964     ts->client = client;
965     i2c_set_clientdata(client, ts);
966     ts->device_id = id->driver_data;
967
968
969     of_property_read_u32(np,"screen_max_x",&(ts->screen_max_x));
970     of_property_read_u32(np,"screen_max_y",&(ts->screen_max_y));
971
972     dev_info(&ts->client->dev, "[tp-gsl] screen_max_x =[%d] \n",ts->screen_max_x);
973     dev_info(&ts->client->dev, "[tp-gsl] screen_max_y =[%d] \n",ts->screen_max_y);
974
975     ts->irq_pin=of_get_named_gpio_flags(np, "touch-gpio", 0, &irq_flags);
976     ts->wake_pin=of_get_named_gpio_flags(np, "reset-gpio", 0, &wake_flags);
977
978     if (gpio_is_valid(ts->wake_pin)) {
979         rc = devm_gpio_request_one(&ts->client->dev, ts->wake_pin, (wake_flags & OF_GPIO_ACTIVE_LOW) ? GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH, "gslX680 wake pin");
980         if (rc != 0) {
981             dev_err(&ts->client->dev, "gslX680 wake pin error\n");
982             return -EIO;
983         }
984         g_wake_pin = ts->wake_pin;
985     } else {
986         dev_info(&ts->client->dev, "wake pin invalid\n");
987     }
988     if (gpio_is_valid(ts->irq_pin)) {
989         rc = devm_gpio_request_one(&ts->client->dev, ts->irq_pin, (irq_flags & OF_GPIO_ACTIVE_LOW) ? GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH, "gslX680 irq pin");
990         if (rc != 0) {
991             dev_err(&ts->client->dev, "gslX680 irq pin error\n");
992             return -EIO;
993         }
994         g_irq_pin = ts->irq_pin;
995     } else {
996         dev_info(&ts->client->dev, "irq pin invalid\n");
997     }
998
999     gslX680_init();
1000     rc = gslX680_ts_init(client, ts);
1001     if (rc < 0) {
1002         dev_err(&client->dev, "GSLX680 init failed\n");
1003         goto error_mutex_destroy;
1004     }
1005
1006     gsl_client = client;
1007     init_chip(ts->client);
1008     check_mem_data(ts->client);
1009
1010     ts->irq=gpio_to_irq(ts->irq_pin);           //If not defined in client
1011     if (ts->irq)
1012     {
1013         rc = devm_request_threaded_irq(&client->dev, ts->irq, NULL, gsl_ts_irq, irq_flags | IRQF_ONESHOT, client->name, ts);
1014         if (rc != 0) {
1015             dev_err(&client->dev, "Cannot allocate ts INT!ERRNO:%d\n", rc);
1016             goto error_req_irq_fail;
1017         }
1018         disable_irq(ts->irq);
1019     }
1020     else
1021     {
1022         dev_err(&client->dev, "gsl x680 irq req fail\n");
1023         goto error_req_irq_fail;
1024     }
1025
1026     /* create debug attribute */
1027 #ifdef TPD_PROC_DEBUG
1028     proc_create(GSL_CONFIG_PROC_FILE,0666,NULL,&gsl_seq_fops);
1029     gsl_proc_flag = 0;
1030 #endif
1031
1032     gpio_set_value(ts->irq_pin, 0);
1033     enable_irq(ts->irq);
1034
1035     return 0;
1036
1037     //exit_set_irq_mode:
1038 error_req_irq_fail:
1039     free_irq(ts->irq, ts);
1040
1041 error_mutex_destroy:
1042     input_free_device(ts->input);
1043     kfree(ts);
1044     return rc;
1045 }
1046
1047 static int gsl_ts_remove(struct i2c_client *client)
1048 {
1049     struct gsl_ts *ts = i2c_get_clientdata(client);
1050
1051     device_init_wakeup(&client->dev, 0);
1052     cancel_work_sync(&ts->work);
1053     free_irq(ts->irq, ts);
1054     destroy_workqueue(ts->wq);
1055     input_unregister_device(ts->input);
1056
1057     kfree(ts->touch_data);
1058     kfree(ts);
1059
1060     return 0;
1061 }
1062
1063 static struct of_device_id gsl_ts_ids[] = {
1064     {.compatible = "gslX680-pad"},
1065     {}
1066 };
1067
1068 static const struct i2c_device_id gsl_ts_id[] = {
1069     {GSLX680_I2C_NAME, 0},
1070     {}
1071 };
1072 MODULE_DEVICE_TABLE(i2c, gsl_ts_id);
1073
1074 static const struct dev_pm_ops gsl_pm_ops = {
1075         .suspend    = gsl_ts_suspend,
1076         .resume     = gsl_ts_resume,
1077 };
1078
1079 static struct i2c_driver gsl_ts_driver = {
1080     .driver = {
1081         .name = GSLX680_I2C_NAME,
1082         .owner = THIS_MODULE,
1083         .of_match_table = of_match_ptr(gsl_ts_ids),
1084         .pm = &gsl_pm_ops,
1085     },
1086     .probe              = gsl_ts_probe,
1087     .remove             = gsl_ts_remove,
1088     .id_table   = gsl_ts_id,
1089 };
1090
1091 static int __init gsl_ts_init(void)
1092 {
1093     int ret;
1094     ret = i2c_add_driver(&gsl_ts_driver);
1095     return ret;
1096 }
1097 static void __exit gsl_ts_exit(void)
1098 {
1099     i2c_del_driver(&gsl_ts_driver);
1100     return;
1101 }
1102
1103 module_init(gsl_ts_init);
1104 module_exit(gsl_ts_exit);
1105
1106 MODULE_LICENSE("GPL");
1107 MODULE_DESCRIPTION("GSLX680 touchscreen controller driver");
1108 MODULE_AUTHOR("Guan Yuwei, guanyuwei@basewin.com");
1109 MODULE_ALIAS("platform:gsl_ts");