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 / gslx680.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 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/hrtimer.h>
15 #include <linux/i2c.h>
16 #include <linux/input.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/platform_device.h>
20 #include <linux/async.h>
21 #include <linux/gpio.h>
22 #include <asm/irq.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
25 #include <linux/proc_fs.h>
26 #include <linux/input/mt.h>
27 //#include "rockchip_gslX680_rk3168.h"
28 #include "tp_suspend.h"
29 #include "gslx680.h"
30 #include <linux/of_gpio.h>
31 #include <linux/wakelock.h>
32
33 #define GSL_DEBUG
34
35 /*
36 struct fw_data
37 {
38         u32 offset : 8;
39         u32 : 0;
40         u32 val;
41 };
42 */
43
44 #define RK_GEAR_TOUCH
45 #define REPORT_DATA_ANDROID_4_0
46 #define HAVE_TOUCH_KEY
47 //#define SLEEP_CLEAR_POINT
48
49 //#define FILTER_POINT
50
51 #ifdef FILTER_POINT
52 #define FILTER_MAX      9       //6
53 #endif
54
55 #define GSLX680_I2C_NAME        "gslX680"
56 #define GSLX680_I2C_ADDR        0x40
57
58 //#define IRQ_PORT      RK2928_PIN1_PB0//RK30_PIN1_PB7
59 //#define WAKE_PORT     RK30_PIN0_PA1//RK30_PIN0_PB6
60
61 #define GSL_DATA_REG            0x80
62 #define GSL_STATUS_REG          0xe0
63 #define GSL_PAGE_REG            0xf0
64
65 #define TPD_PROC_DEBUG
66 #ifdef TPD_PROC_DEBUG
67 #include <linux/proc_fs.h>
68 #include <asm/uaccess.h>
69 #include <linux/seq_file.h>
70 //static struct proc_dir_entry *gsl_config_proc = NULL;
71 #define GSL_CONFIG_PROC_FILE "gsl_config"
72 #define CONFIG_LEN 31
73 static char gsl_read[CONFIG_LEN];
74 static u8 gsl_data_proc[8] = { 0 };
75 static u8 gsl_proc_flag = 0;
76 static struct i2c_client *i2c_client = NULL;
77 #endif
78 #define GSL_MONITOR
79 #define PRESS_MAX               255
80 #define MAX_FINGERS             10
81 #define MAX_CONTACTS            10
82 #define DMA_TRANS_LEN           0x20
83 #ifdef GSL_MONITOR
84
85 #ifdef RK_GEAR_TOUCH
86 static int g_istouch=0;
87 #endif
88
89 static struct workqueue_struct *gsl_monitor_workqueue = NULL;
90 static u8 int_1st[4] = { 0 };
91 static u8 int_2nd[4] = { 0 };
92 //static char dac_counter = 0;
93 static char b0_counter = 0;
94 static char bc_counter = 0;
95 static char i2c_lock_flag = 0;
96 #endif
97
98 #define WRITE_I2C_SPEED (350*1000)
99 #define I2C_SPEED       (200*1000)
100 #define CLOSE_TP_POWER   0
101 //add by yuandan
102 //#define HAVE_CLICK_TIMER
103
104 #ifdef HAVE_CLICK_TIMER
105
106 static struct workqueue_struct *gsl_timer_workqueue = NULL;
107 bool send_key = false;
108 struct semaphore my_sem;
109 #endif
110
111 #ifdef HAVE_TOUCH_KEY
112 static u16 key = 0;
113 static int key_state_flag = 0;
114 struct key_data {
115         u16 key;
116         u16 x_min;
117         u16 x_max;
118         u16 y_min;
119         u16 y_max;
120 };
121
122 const u16 key_array[] = {
123         KEY_LEFT,
124         KEY_RIGHT,
125         KEY_UP,
126         KEY_DOWN,
127         KEY_ENTER,
128 };
129
130 #define MAX_KEY_NUM     (sizeof(key_array)/sizeof(key_array[0]))
131 //add by yuandan
132 int key_x[512];
133 int key_y[512];
134 int key_count = 0;
135 int key_repeat;
136 struct key_data gsl_key_data[MAX_KEY_NUM] = {
137         {KEY_BACK, 550, 650, 1400, 1600},
138         {KEY_HOMEPAGE, 350, 450, 1400, 1600},
139         {KEY_MENU, 150, 250, 1400, 1600},
140         {KEY_SEARCH, 2048, 2048, 2048, 2048},
141 };
142
143 #endif
144
145 struct gsl_ts_data {
146         u8 x_index;
147         u8 y_index;
148         u8 z_index;
149         u8 id_index;
150         u8 touch_index;
151         u8 data_reg;
152         u8 status_reg;
153         u8 data_size;
154         u8 touch_bytes;
155         u8 update_data;
156         u8 touch_meta_data;
157         u8 finger_size;
158 };
159
160 static struct gsl_ts_data devices[] = {
161         {
162          .x_index = 6,
163          .y_index = 4,
164          .z_index = 5,
165          .id_index = 7,
166          .data_reg = GSL_DATA_REG,
167          .status_reg = GSL_STATUS_REG,
168          .update_data = 0x4,
169          .touch_bytes = 4,
170          .touch_meta_data = 4,
171          .finger_size = 70,
172          },
173 };
174
175 struct gsl_ts {
176         struct i2c_client *client;
177         struct input_dev *input;
178         struct work_struct work;
179         struct workqueue_struct *wq;
180         struct gsl_ts_data *dd;
181         u8 *touch_data;
182         u8 device_id;
183         int irq;
184         int rst;
185         struct delayed_work gsl_monitor_work;
186 #if defined(CONFIG_HAS_EARLYSUSPEND)
187         struct early_suspend early_suspend;
188 #endif
189
190 #if defined (HAVE_CLICK_TIMER)
191         struct work_struct click_work;
192 #endif
193
194         struct tp_device tp;
195         struct pinctrl *pinctrl;
196         struct pinctrl_state *pins_default;
197         struct pinctrl_state *pins_sleep;
198         struct pinctrl_state *pins_inactive;
199 };
200
201 #ifdef GSL_DEBUG
202 #define print_info(fmt, args...)        printk(fmt, ##args);
203 #else
204 #define print_info(fmt, args...)
205 #endif
206
207 static u32 id_sign[MAX_CONTACTS + 1] = { 0 };
208 static u8 id_state_flag[MAX_CONTACTS + 1] = { 0 };
209 static u8 id_state_old_flag[MAX_CONTACTS + 1] = { 0 };
210 static u16 x_old[MAX_CONTACTS + 1] = { 0 };
211 static u16 y_old[MAX_CONTACTS + 1] = { 0 };
212 static u16 x_new = 0;
213 static u16 y_new = 0;
214
215 int gslx680_set_pinctrl_state(struct gsl_ts *ts, struct pinctrl_state *state)
216 {
217         int ret = 0;
218
219         if (!IS_ERR(state)) {
220                 ret = pinctrl_select_state(ts->pinctrl, state);
221                 if (ret)
222                         printk("could not set pins \n");
223         }
224
225         return ret;
226 }
227
228 static int gslX680_init(struct gsl_ts *ts)
229 {
230         struct device_node *np = ts->client->dev.of_node;
231         int err = 0;
232         int ret = 0;
233
234         ts->irq = of_get_named_gpio_flags(np, "touch-gpio", 0, NULL);
235         ts->rst = of_get_named_gpio_flags(np, "reset-gpio", 0, NULL);
236
237         //msleep(20);
238 #if 0   //#if defined (CONFIG_BOARD_ZM71C)||defined (CONFIG_BOARD_ZM72CP) ||
239         defined(CONFIG_BOARD_ZM726C) || defined(CONFIG_BOARD_ZM726CE)
240             if (gpio_request(ts->rst, NULL) != 0) {
241                 gpio_free(ts->rst);
242                 printk("gslX680_init gpio_request error\n");
243                 return -EIO;
244         }
245 #endif
246
247         /* pinctrl */
248         ts->pinctrl = devm_pinctrl_get(&ts->client->dev);
249         if (IS_ERR(ts->pinctrl)) {
250                 ret = PTR_ERR(ts->pinctrl);
251                 //goto out;
252         }
253
254         ts->pins_default =
255             pinctrl_lookup_state(ts->pinctrl, PINCTRL_STATE_DEFAULT);
256         //if (IS_ERR(ts->pins_default))
257         //      dev_err(&client->dev, "could not get default pinstate\n");
258
259         ts->pins_sleep = pinctrl_lookup_state(ts->pinctrl, PINCTRL_STATE_SLEEP);
260         //if (IS_ERR(ts->pins_sleep))
261         //      dev_err(&client->dev, "could not get sleep pinstate\n");
262
263         ts->pins_inactive = pinctrl_lookup_state(ts->pinctrl, "inactive");
264         //if (IS_ERR(ts->pins_inactive))
265         //      dev_err(&client->dev, "could not get inactive pinstate\n");
266
267         err = gpio_request(ts->rst, "tp reset");
268         if (err) {
269                 printk("gslx680 reset gpio request failed.\n");
270                 return -1;
271         }
272
273         gslx680_set_pinctrl_state(ts, ts->pins_default);
274         gpio_direction_output(ts->rst, 1);
275         gpio_set_value(ts->rst, 1);
276
277         return 0;
278 }
279
280 static int gslX680_shutdown_low(struct gsl_ts *ts)
281 {
282         printk("gsl  gslX680_shutdown_low\n");
283         gpio_direction_output(ts->rst, 0);
284         gpio_set_value(ts->rst, 0);
285
286         return 0;
287 }
288
289 static int gslX680_shutdown_high(struct gsl_ts *ts)
290 {
291         printk("gsl  gslX680_shutdown_high\n");
292         gpio_direction_output(ts->rst, 1);
293         gpio_set_value(ts->rst, 1);
294
295         return 0;
296 }
297
298 static inline u16 join_bytes(u8 a, u8 b)
299 {
300         u16 ab = 0;
301
302         ab = ab | a;
303         ab = ab << 8 | b;
304
305         return ab;
306 }
307
308 /*
309 static u32 gsl_read_interface(struct i2c_client *client,
310         u8 reg, u8 *buf, u32 num)
311 {
312         struct i2c_msg xfer_msg[2];
313
314         xfer_msg[0].addr = client->addr;
315         xfer_msg[0].len = 1;
316         xfer_msg[0].flags = client->flags & I2C_M_TEN;
317         xfer_msg[0].buf = &reg;
318         xfer_msg[0].scl_rate=300*1000;
319
320         xfer_msg[1].addr = client->addr;
321         xfer_msg[1].len = num;
322         xfer_msg[1].flags |= I2C_M_RD;
323         xfer_msg[1].buf = buf;
324         xfer_msg[1].scl_rate=300*1000;
325
326         if (reg < 0x80) {
327                 i2c_transfer(client->adapter, xfer_msg, ARRAY_SIZE(xfer_msg));
328                 msleep(5);
329         }
330
331         return i2c_transfer(client->adapter, xfer_msg, ARRAY_SIZE(xfer_msg)) \
332                 == ARRAY_SIZE(xfer_msg) ? 0 : -EFAULT;
333 }
334  */
335
336 static u32 gsl_write_interface(struct i2c_client *client,
337                                const u8 reg, u8 *buf, u32 num)
338 {
339         struct i2c_msg xfer_msg[1];
340
341         buf[0] = reg;
342
343         xfer_msg[0].addr = client->addr;
344         xfer_msg[0].len = num + 1;
345         xfer_msg[0].flags = client->flags & I2C_M_TEN;
346         xfer_msg[0].buf = buf;
347         //xfer_msg[0].scl_rate = 100 * 1000;
348
349         return i2c_transfer(client->adapter, xfer_msg, 1) == 1 ? 0 : -EFAULT;
350 }
351
352 static int gsl_ts_write(struct i2c_client *client,
353                         u8 addr, u8 *pdata, int datalen)
354 {
355         int ret = 0;
356         u8 tmp_buf[128];
357         unsigned int bytelen = 0;
358
359         if (datalen > 125) {
360                 printk("%s too big datalen = %d!\n", __func__, datalen);
361                 return -1;
362         }
363
364         tmp_buf[0] = addr;
365         bytelen++;
366
367         if (datalen != 0 && pdata != NULL) {
368                 memcpy(&tmp_buf[bytelen], pdata, datalen);
369                 bytelen += datalen;
370         }
371
372         ret = i2c_master_send(client, tmp_buf, bytelen);
373         return ret;
374 }
375
376 static int gsl_ts_read(struct i2c_client *client, u8 addr,
377                        u8 *pdata, unsigned int datalen)
378 {
379         int ret = 0;
380
381         if (datalen > 126) {
382                 printk("%s too big datalen = %d!\n", __func__, datalen);
383                 return -1;
384         }
385
386         ret = gsl_ts_write(client, addr, NULL, 0);
387         if (ret < 0) {
388                 printk("%s set data address fail!\n", __func__);
389                 return ret;
390         }
391
392         return i2c_master_recv(client, pdata, datalen);
393 }
394
395 static __inline__ void fw2buf(u8 *buf, const u32 *fw)
396 {
397         u32 *u32_buf = (int *)buf;
398         *u32_buf = *fw;
399 }
400
401 static void gsl_load_fw(struct i2c_client *client)
402 {
403         u8 buf[DMA_TRANS_LEN * 4 + 1] = { 0 };
404         u8 send_flag = 1;
405         u8 *cur = buf + 1;
406         u32 source_line = 0;
407         u32 source_len;
408         //u8 read_buf[4] = {0};
409         struct fw_data const *ptr_fw;
410
411         ptr_fw = GSLX680_FW;
412         source_len = ARRAY_SIZE(GSLX680_FW);
413
414         for (source_line = 0; source_line < source_len; source_line++) {
415                 /* init page trans, set the page val */
416                 if (GSL_PAGE_REG == ptr_fw[source_line].offset) {
417                         fw2buf(cur, &ptr_fw[source_line].val);
418                         gsl_write_interface(client, GSL_PAGE_REG, buf, 4);
419                         send_flag = 1;
420                 } else {
421                         if (1 ==
422                             send_flag % (DMA_TRANS_LEN <
423                                          0x20 ? DMA_TRANS_LEN : 0x20))
424                                 buf[0] = (u8) ptr_fw[source_line].offset;
425
426                         fw2buf(cur, &ptr_fw[source_line].val);
427                         cur += 4;
428
429                         if (0 ==
430                             send_flag % (DMA_TRANS_LEN <
431                                          0x20 ? DMA_TRANS_LEN : 0x20)) {
432                                 gsl_write_interface(client, buf[0], buf,
433                                                     cur - buf - 1);
434                                 cur = buf + 1;
435                         }
436
437                         send_flag++;
438                 }
439         }
440 }
441
442 static int test_i2c(struct i2c_client *client)
443 {
444         u8 read_buf = 0;
445         u8 write_buf = 0x12;
446         int ret, rc = 1;
447
448         ret = gsl_ts_read(client, 0xf0, &read_buf, sizeof(read_buf));
449         if (ret < 0)
450                 rc--;
451         else
452                 printk("gsl I read reg 0xf0 is %x\n", read_buf);
453
454         msleep(2);
455         ret = gsl_ts_write(client, 0xf0, &write_buf, sizeof(write_buf));
456         if (ret >= 0)
457                 printk("gsl I write reg 0xf0 0x12\n");
458
459         msleep(2);
460         ret = gsl_ts_read(client, 0xf0, &read_buf, sizeof(read_buf));
461         if (ret < 0)
462                 rc--;
463         else
464                 printk("gsl I read reg 0xf0 is 0x%x\n", read_buf);
465
466         return rc;
467 }
468 static void startup_chip(struct i2c_client *client)
469 {
470         u8 tmp = 0x00;
471
472         printk("gsl  startup_chip\n");
473
474 #ifdef GSL_NOID_VERSION
475         gsl_DataInit(gsl_config_data_id);
476 #endif
477         gsl_ts_write(client, 0xe0, &tmp, 1);
478         mdelay(10);
479 }
480
481 static void reset_chip(struct i2c_client *client)
482 {
483         u8 tmp = 0x88;
484         u8 buf[4] = { 0x00 };
485
486         printk("gsl  reset_chip\n");
487
488         gsl_ts_write(client, 0xe0, &tmp, sizeof(tmp));
489         mdelay(20);
490         tmp = 0x04;
491         gsl_ts_write(client, 0xe4, &tmp, sizeof(tmp));
492         mdelay(10);
493         gsl_ts_write(client, 0xbc, buf, sizeof(buf));
494         mdelay(10);
495 }
496
497 static void clr_reg(struct i2c_client *client)
498 {
499         u8 write_buf[4] = { 0 };
500
501         write_buf[0] = 0x88;
502         gsl_ts_write(client, 0xe0, &write_buf[0], 1);
503         mdelay(20);
504         write_buf[0] = 0x03;
505         gsl_ts_write(client, 0x80, &write_buf[0], 1);
506         mdelay(5);
507         write_buf[0] = 0x04;
508         gsl_ts_write(client, 0xe4, &write_buf[0], 1);
509         mdelay(5);
510         write_buf[0] = 0x00;
511         gsl_ts_write(client, 0xe0, &write_buf[0], 1);
512         mdelay(20);
513 }
514
515 static void init_chip(struct i2c_client *client, struct gsl_ts *ts)
516 {
517         int rc;
518
519         printk("gsl  init_chip\n");
520
521         gslX680_shutdown_low(ts);
522         mdelay(20);
523         gslX680_shutdown_high(ts);
524         mdelay(20);
525         rc = test_i2c(client);
526         if (rc < 0) {
527                 printk("gslX680 test_i2c error\n");
528                 return;
529         }
530         clr_reg(client);
531         reset_chip(client);
532         gsl_load_fw(client);
533         startup_chip(client);
534         reset_chip(client);
535         startup_chip(client);
536 }
537
538 static void check_mem_data(struct i2c_client *client, struct gsl_ts *ts)
539 {
540         u8 read_buf[4] = { 0 };
541
542         mdelay(30);
543         gsl_ts_read(client, 0xb0, read_buf, sizeof(read_buf));
544         if (read_buf[3] != 0x5a || read_buf[2] != 0x5a ||
545                 read_buf[1] != 0x5a || read_buf[0] != 0x5a) {
546                 init_chip(client, ts);
547         }
548 }
549
550 #ifdef TPD_PROC_DEBUG
551 static int char_to_int(char ch)
552 {
553         if (ch >= '0' && ch <= '9')
554                 return (ch - '0');
555         else
556                 return (ch - 'a' + 10);
557 }
558
559 static int gsl_config_read_proc(struct seq_file *m, void *v)
560 {
561         //char *ptr = page;
562         char temp_data[5] = { 0 };
563         unsigned int tmp = 0;
564         //unsigned int *ptr_fw;
565
566         if ('v' == gsl_read[0] && 's' == gsl_read[1]) {
567 #ifdef GSL_NOID_VERSION
568                 tmp = gsl_version_id();
569 #else
570                 tmp = 0x20121215;
571 #endif
572                 seq_printf(m, "version:%x\n", tmp);
573         } else if ('r' == gsl_read[0] && 'e' == gsl_read[1]) {
574                 if ('i' == gsl_read[3]) {
575 #ifdef GSL_NOID_VERSION
576                         /*      tmp=(gsl_data_proc[5]<<8) | gsl_data_proc[4];
577                            seq_printf(m,"gsl_config_data_id[%d] = ",tmp);
578                            if(tmp>=0&&tmp<gsl_cfg_table[gsl_cfg_index].data_size)
579                            seq_printf(m,"%d\n",gsl_cfg_table[gsl_cfg_index].data_id[tmp]); */
580
581                         tmp = (gsl_data_proc[5] << 8) | gsl_data_proc[4];
582                         seq_printf(m, "gsl_config_data_id[%d] = ", tmp);
583                         if (tmp >= 0 && tmp < 512)
584                                 seq_printf(m, "%d\n", gsl_config_data_id[tmp]);
585 #endif
586                 } else {
587                         i2c_smbus_write_i2c_block_data(i2c_client, 0xf0, 4,
588                                                        &gsl_data_proc[4]);
589                         if (gsl_data_proc[0] < 0x80)
590                                 i2c_smbus_read_i2c_block_data(i2c_client,
591                                                               gsl_data_proc[0],
592                                                               4, temp_data);
593                         i2c_smbus_read_i2c_block_data(i2c_client,
594                                                       gsl_data_proc[0], 4,
595                                                       temp_data);
596
597                         seq_printf(m, "offset : {0x%02x,0x", gsl_data_proc[0]);
598                         seq_printf(m, "%02x", temp_data[3]);
599                         seq_printf(m, "%02x", temp_data[2]);
600                         seq_printf(m, "%02x", temp_data[1]);
601                         seq_printf(m, "%02x};\n", temp_data[0]);
602                 }
603         }
604         return 0;
605 }
606 static ssize_t gsl_config_write_proc(struct file *file, const char *buffer,
607                                  size_t count, loff_t *data)
608 {
609         u8 buf[8] = { 0 };
610         char temp_buf[CONFIG_LEN];
611         char *path_buf;
612         int tmp = 0;
613         int tmp1 = 0;
614
615         print_info("[tp-gsl][%s] \n", __func__);
616         if (count > 512) {
617                 //print_info("size not match [%d:%d]\n", CONFIG_LEN, count);
618                 return -EFAULT;
619         }
620         path_buf = kzalloc(count, GFP_KERNEL);
621         if (!path_buf) {
622                 printk("alloc path_buf memory error \n");
623         }
624         if (copy_from_user(path_buf, buffer, count)) {
625                 print_info("copy from user fail\n");
626                 goto exit_write_proc_out;
627         }
628         memcpy(temp_buf, path_buf, (count < CONFIG_LEN ? count : CONFIG_LEN));
629         print_info("[tp-gsl][%s][%s]\n", __func__, temp_buf);
630
631         buf[3] = char_to_int(temp_buf[14]) << 4 | char_to_int(temp_buf[15]);
632         buf[2] = char_to_int(temp_buf[16]) << 4 | char_to_int(temp_buf[17]);
633         buf[1] = char_to_int(temp_buf[18]) << 4 | char_to_int(temp_buf[19]);
634         buf[0] = char_to_int(temp_buf[20]) << 4 | char_to_int(temp_buf[21]);
635
636         buf[7] = char_to_int(temp_buf[5]) << 4 | char_to_int(temp_buf[6]);
637         buf[6] = char_to_int(temp_buf[7]) << 4 | char_to_int(temp_buf[8]);
638         buf[5] = char_to_int(temp_buf[9]) << 4 | char_to_int(temp_buf[10]);
639         buf[4] = char_to_int(temp_buf[11]) << 4 | char_to_int(temp_buf[12]);
640         if ('v' == temp_buf[0] && 's' == temp_buf[1]) {
641                 //version //vs
642                 memcpy(gsl_read, temp_buf, 4);
643                 printk("gsl version\n");
644         } else if ('s' == temp_buf[0] && 't' == temp_buf[1]) {
645                 //start //st
646                 gsl_proc_flag = 1;
647                 reset_chip(i2c_client);
648         } else if ('e' == temp_buf[0] && 'n' == temp_buf[1]) {
649                 //end //en
650                 mdelay(20);
651                 reset_chip(i2c_client);
652                 startup_chip(i2c_client);
653                 gsl_proc_flag = 0;
654         } else if ('r' == temp_buf[0] && 'e' == temp_buf[1]) {
655                 //read buf //
656                 memcpy(gsl_read, temp_buf, 4);
657                 memcpy(gsl_data_proc, buf, 8);
658         } else if ('w' == temp_buf[0] && 'r' == temp_buf[1]) {
659                 //write buf
660                 i2c_smbus_write_i2c_block_data(i2c_client, buf[4], 4, buf);
661         }
662 #ifdef GSL_NOID_VERSION
663         else if ('i' == temp_buf[0] && 'd' == temp_buf[1]) {
664                 //write id config //
665                 tmp1 = (buf[7] << 24) | (buf[6] << 16) | (buf[5] << 8) | buf[4];
666                 tmp = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
667
668                 if (tmp1 >= 0 && tmp1 < 512) {
669                         gsl_config_data_id[tmp1] = tmp;
670                 }
671         }
672 #endif
673         exit_write_proc_out:
674         kfree(path_buf);
675         return count;
676 }
677
678 static int gsl_server_list_open(struct inode *inode, struct file *file)
679 {
680         return single_open(file, gsl_config_read_proc, NULL);
681 }
682 static const struct file_operations gsl_seq_fops = {
683         .open = gsl_server_list_open,
684         .read = seq_read,
685         .release = single_release,
686         .write = gsl_config_write_proc,
687         .owner = THIS_MODULE,
688 };
689
690 #endif
691
692 #ifdef FILTER_POINT
693 static void filter_point(u16 x, u16 y, u8 id)
694 {
695         u16 x_err = 0;
696         u16 y_err = 0;
697         u16 filter_step_x = 0, filter_step_y = 0;
698
699         id_sign[id] = id_sign[id] + 1;
700         if (id_sign[id] == 1) {
701                 x_old[id] = x;
702                 y_old[id] = y;
703         }
704
705         x_err = x > x_old[id] ? (x - x_old[id]) : (x_old[id] - x);
706         y_err = y > y_old[id] ? (y - y_old[id]) : (y_old[id] - y);
707
708         if ((x_err > FILTER_MAX && y_err > FILTER_MAX / 3) ||
709                 (x_err > FILTER_MAX / 3 && y_err > FILTER_MAX)) {
710                 filter_step_x = x_err;
711                 filter_step_y = y_err;
712         } else {
713                 if (x_err > FILTER_MAX)
714                         filter_step_x = x_err;
715                 if (y_err > FILTER_MAX)
716                         filter_step_y = y_err;
717         }
718
719         if (x_err <= 2 * FILTER_MAX && y_err <= 2 * FILTER_MAX) {
720                 filter_step_x >>= 2;
721                 filter_step_y >>= 2;
722         } else if (x_err <= 3 * FILTER_MAX && y_err <= 3 * FILTER_MAX) {
723                 filter_step_x >>= 1;
724                 filter_step_y >>= 1;
725         } else if (x_err <= 4 * FILTER_MAX && y_err <= 4 * FILTER_MAX) {
726                 filter_step_x = filter_step_x * 3 / 4;
727                 filter_step_y = filter_step_y * 3 / 4;
728         }
729
730         x_new =
731             x >
732             x_old[id] ? (x_old[id] + filter_step_x) : (x_old[id] -
733                                                        filter_step_x);
734         y_new =
735             y >
736             y_old[id] ? (y_old[id] + filter_step_y) : (y_old[id] -
737                                                        filter_step_y);
738
739         x_old[id] = x_new;
740         y_old[id] = y_new;
741 }
742 #else
743 static void record_point(u16 x, u16 y, u8 id)
744 {
745         u16 x_err = 0;
746         u16 y_err = 0;
747
748         id_sign[id] = id_sign[id] + 1;
749
750         if (id_sign[id] == 1) {
751                 x_old[id] = x;
752                 y_old[id] = y;
753         }
754
755         x = (x_old[id] + x) / 2;
756         y = (y_old[id] + y) / 2;
757
758         if (x > x_old[id]) {
759                 x_err = x - x_old[id];
760         } else {
761                 x_err = x_old[id] - x;
762         }
763
764         if (y > y_old[id]) {
765                 y_err = y - y_old[id];
766         } else {
767                 y_err = y_old[id] - y;
768         }
769
770         if ((x_err > 3 && y_err > 1) || (x_err > 1 && y_err > 3)) {
771                 x_new = x;
772                 x_old[id] = x;
773                 y_new = y;
774                 y_old[id] = y;
775         } else {
776                 if (x_err > 3) {
777                         x_new = x;
778                         x_old[id] = x;
779                 } else {
780                         x_new = x_old[id];
781                 }
782
783                 if (y_err > 3) {
784                         y_new = y;
785                         y_old[id] = y;
786                 } else {
787                         y_new = y_old[id];
788                 }
789         }
790
791         if (id_sign[id] == 1) {
792                 x_new = x_old[id];
793                 y_new = y_old[id];
794         }
795 }
796 #endif
797
798
799 #ifdef SLEEP_CLEAR_POINT
800 #ifdef HAVE_TOUCH_KEY
801 static void report_key(struct gsl_ts *ts, u16 x, u16 y)
802 {
803         u16 i = 0;
804
805         for (i = 0; i < MAX_KEY_NUM; i++) {
806                 if ((gsl_key_data[i].x_min < x)
807                     && (x < gsl_key_data[i].x_max)
808                     && (gsl_key_data[i].y_min < y)
809                     && (y < gsl_key_data[i].y_max)) {
810                         key = gsl_key_data[i].key;
811                         input_report_key(ts->input, key, 1);
812                         input_sync(ts->input);
813                         key_state_flag = 1;
814                         break;
815                 }
816         }
817 }
818 #endif
819 #endif
820
821 #ifdef RK_GEAR_TOUCH
822 static void report_data(struct gsl_ts *ts, u16 x, u16 y, u8 pressure, u8 id)
823 {
824 #ifdef RK_GEAR_TOUCH
825         int delt_x,delt_y;
826         static int old_x=0, old_y=0;
827 #endif
828         //#ifndef SWAP_XY
829         //      swap(x, y);
830         //#endif
831         //printk("#####id=%d,x=%d,y=%d######\n",id,x,y);
832
833         if (x > SCREEN_MAX_X || y > SCREEN_MAX_Y) {
834 #ifdef HAVE_TOUCH_KEY
835                 //report_key(ts, x, y);
836                 //printk("#####report_key x=%d,y=%d######\n",x,y);
837 #endif
838                 return;
839         }
840
841         /*
842            input_mt_slot(ts->input_dev, id);
843            input_report_abs(ts->input_dev, ABS_MT_TRACKING_ID, id);
844            input_report_abs(ts->input_dev, ABS_MT_POSITION_X, x);
845            input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, y);
846            input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, w);
847            input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
848          */
849 #ifdef RK_GEAR_TOUCH
850         if (g_istouch == 0){
851                 g_istouch = 1;
852                 input_event(ts->input, EV_MSC, MSC_SCAN, 0x90001);
853                 input_report_key(ts->input, 0x110, 1);
854                 input_sync(ts->input);
855         }
856         delt_x = (int)x - old_x;
857         delt_y = (int)y - old_y;
858         delt_x /= 10;
859         delt_y /= 10;
860         input_report_rel(ts->input, REL_Y, -delt_x);
861     input_report_rel(ts->input, REL_X, -delt_y);
862         input_sync(ts->input);
863         old_x = x;
864         old_y = y;
865         return;
866 #endif
867
868 #ifdef REPORT_DATA_ANDROID_4_0
869         //printk("#####REPORT_DATA_ANDROID_4_0######\n");
870         input_mt_slot(ts->input, id);
871         //input_report_abs(ts->input, ABS_MT_TRACKING_ID, id);
872         input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 1);
873         input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, pressure);
874 #ifdef X_POL
875         input_report_abs(ts->input, ABS_MT_POSITION_X, SCREEN_MAX_X - x);
876 #else
877         input_report_abs(ts->input, ABS_MT_POSITION_X, x);
878 #endif
879 #ifdef Y_POL
880         input_report_abs(ts->input, ABS_MT_POSITION_Y, (SCREEN_MAX_Y - y));
881 #else
882         input_report_abs(ts->input, ABS_MT_POSITION_Y, (y));
883 #endif
884         input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 1);
885 #else
886         //printk("#####nonono REPORT_DATA_ANDROID_4_0######\n");
887         input_report_abs(ts->input, ABS_MT_TRACKING_ID, id);
888         input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, pressure);
889         input_report_abs(ts->input, ABS_MT_POSITION_X, x);
890         input_report_abs(ts->input, ABS_MT_POSITION_Y, y);
891         input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 1);
892         input_mt_sync(ts->input);
893 #endif
894 }
895 #endif
896
897 static void gslX680_ts_worker(struct work_struct *work)
898 {
899         int rc, i;
900         u8 id, touches;
901         u16 x, y;
902
903 #ifdef GSL_NOID_VERSION
904         u32 tmp1;
905         u8 buf[4] = { 0 };
906         struct gsl_touch_info cinfo;
907 #endif
908
909         struct gsl_ts *ts = container_of(work, struct gsl_ts, work);
910
911 #ifdef TPD_PROC_DEBUG
912         if (gsl_proc_flag == 1)
913                 goto schedule;
914 #endif
915
916 #ifdef GSL_MONITOR
917         if (i2c_lock_flag != 0)
918                 goto i2c_lock_schedule;
919         else
920                 i2c_lock_flag = 1;
921 #endif
922
923         rc = gsl_ts_read(ts->client, 0x80, ts->touch_data, ts->dd->data_size);
924         if (rc < 0) {
925                 dev_err(&ts->client->dev, "read failed\n");
926                 goto schedule;
927         }
928
929         touches = ts->touch_data[ts->dd->touch_index];
930         //print_info("-----touches: %d -----\n", touches);
931 #ifdef GSL_NOID_VERSION
932
933         cinfo.finger_num = touches;
934         //print_info("tp-gsl  finger_num = %d\n",cinfo.finger_num);
935         for (i = 0; i < (touches < MAX_CONTACTS ? touches : MAX_CONTACTS); i++) {
936                 cinfo.x[i] =
937                     join_bytes((ts->
938                                 touch_data[ts->dd->x_index + 4 * i + 1] & 0xf),
939                                ts->touch_data[ts->dd->x_index + 4 * i]);
940                 cinfo.y[i] =
941                     join_bytes(ts->touch_data[ts->dd->y_index + 4 * i + 1],
942                                ts->touch_data[ts->dd->y_index + 4 * i]);
943                 cinfo.id[i] =
944                     ((ts->touch_data[ts->dd->x_index + 4 * i + 1] & 0xf0) >> 4);
945                 /*print_info("tp-gsl  before: x[%d] = %d, y[%d] = %d,
946                  id[%d] = %d \n",i,cinfo.x[i],i,cinfo.y[i],i,cinfo.id[i]);*/
947         }
948         cinfo.finger_num = (ts->touch_data[3] << 24) | (ts->touch_data[2] << 16)
949             | (ts->touch_data[1] << 8) | (ts->touch_data[0]);
950         gsl_alg_id_main(&cinfo);
951         tmp1 = gsl_mask_tiaoping();
952         //print_info("[tp-gsl] tmp1 = %x\n", tmp1);
953         if (tmp1 > 0 && tmp1 < 0xffffffff) {
954                 buf[0] = 0xa;
955                 buf[1] = 0;
956                 buf[2] = 0;
957                 buf[3] = 0;
958                 gsl_ts_write(ts->client, 0xf0, buf, 4);
959                 buf[0] = (u8) (tmp1 & 0xff);
960                 buf[1] = (u8) ((tmp1 >> 8) & 0xff);
961                 buf[2] = (u8) ((tmp1 >> 16) & 0xff);
962                 buf[3] = (u8) ((tmp1 >> 24) & 0xff);
963                 print_info("tmp1=%08x,buf[0]=%02x,buf[1]=%02x,buf[2]=%02x, \
964                         buf[3]=%02x\n", tmp1, buf[0], buf[1], buf[2], buf[3]);
965                 gsl_ts_write(ts->client, 0x8, buf, 4);
966         }
967         touches = cinfo.finger_num;
968 #endif
969
970         for (i = 1; i <= MAX_CONTACTS; i++) {
971                 if (touches == 0)
972                         id_sign[i] = 0;
973                 id_state_flag[i] = 0;
974         }
975         for (i = 0; i < (touches > MAX_FINGERS ? MAX_FINGERS : touches); i++) {
976 #ifdef GSL_NOID_VERSION
977                 id = cinfo.id[i];
978                 x = cinfo.x[i];
979                 y = cinfo.y[i];
980 #else
981                 x = join_bytes((ts->
982                                 touch_data[ts->dd->x_index + 4 * i + 1] & 0xf),
983                                ts->touch_data[ts->dd->x_index + 4 * i]);
984                 y = join_bytes(ts->touch_data[ts->dd->y_index + 4 * i + 1],
985                                ts->touch_data[ts->dd->y_index + 4 * i]);
986                 id = ts->touch_data[ts->dd->id_index + 4 * i] >> 4;
987 #endif
988
989                 if (1 <= id && id <= MAX_CONTACTS) {
990 #ifdef FILTER_POINT
991                         filter_point(x, y, id);
992 #else
993                         record_point(x, y, id);
994 #endif
995 #ifdef RK_GEAR_TOUCH
996                         report_data(ts, x_new, y_new, 10, id);
997 #endif
998                         if (key_count <= 512) {
999                                 key_x[key_count] = x_new;
1000                                 key_y[key_count] = y_new;
1001                                 key_count++;
1002                                 /*printk("test in key store in here,
1003                                 x_new is %d , y_new is %d ,
1004                                 key_count is %d \n", x_new ,y_new,key_count);*/
1005                         }
1006                         id_state_flag[id] = 1;
1007                 }
1008         }
1009         for (i = 1; i <= MAX_CONTACTS; i++) {
1010                 if ((0 == touches)
1011                     || ((0 != id_state_old_flag[i])
1012                         && (0 == id_state_flag[i]))) {
1013 #ifdef RK_GEAR_TOUCH
1014                         if (g_istouch == 1){
1015                                 g_istouch = 0;
1016                                 input_event(ts->input, EV_MSC, MSC_SCAN, 0x90001);
1017                                 input_report_key(ts->input, 0x110, 0);
1018                                 input_sync(ts->input);
1019                         }
1020                         g_istouch = 0;
1021 #endif
1022 #ifdef REPORT_DATA_ANDROID_4_0
1023                         input_mt_slot(ts->input, i);
1024                         //input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
1025                         input_mt_report_slot_state(ts->input, MT_TOOL_FINGER,
1026                                                    false);
1027 #endif
1028                         id_sign[i] = 0;
1029                 }
1030                 id_state_old_flag[i] = id_state_flag[i];
1031         }
1032
1033         if (0 == touches) {
1034 #ifdef REPORT_DATA_ANDROID_4_0
1035 #ifndef RK_GEAR_TOUCH
1036                 //input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, 0);
1037                 //input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 0);
1038                 //input_mt_sync(ts->input);
1039
1040                 int temp_x = 0;
1041                 int temp_y = 0;
1042                 temp_x =
1043                     (((key_x[key_count - 1] - key_x[0]) >
1044                       0) ? (key_x[key_count - 1] - key_x[0])
1045                      : (key_x[0] - key_x[key_count - 1]));
1046                 temp_y =
1047                     (((key_y[key_count - 1] - key_y[0]) >
1048                       0) ? (key_y[key_count - 1] - key_y[0])
1049                      : (key_y[0] - key_y[key_count - 1]));
1050                 if (key_count <= 512) {
1051                         if (temp_x > temp_y) {
1052                                 if ((key_x[key_count - 1] - key_x[0]) > 100) {
1053                                         printk(" send up key \n");
1054                                         input_report_key(ts->input,
1055                                                          key_array[2], 1);
1056                                         input_sync(ts->input);
1057                                         input_report_key(ts->input,
1058                                                          key_array[2], 0);
1059                                         input_sync(ts->input);
1060                                 } else if ((key_x[0] - key_x[key_count - 1]) >
1061                                            100) {
1062                                         printk(" send down key \n");
1063                                         input_report_key(ts->input,
1064                                                          key_array[3], 1);
1065                                         input_sync(ts->input);
1066                                         input_report_key(ts->input,
1067                                                          key_array[3], 0);
1068                                         input_sync(ts->input);
1069                                 }
1070                         } else if (temp_x <= temp_y) {
1071                                 if ((key_y[key_count - 1] - key_y[0]) > 100) {
1072                                         printk(" send left key \n");
1073                                         input_report_key(ts->input,
1074                                                          key_array[0], 1);
1075                                         input_sync(ts->input);
1076                                         input_report_key(ts->input,
1077                                                          key_array[0], 0);
1078                                         input_sync(ts->input);
1079                                 } else if ((key_y[0] - key_y[key_count - 1]) >
1080                                            100) {
1081                                         printk(" send right key \n");
1082                                         input_report_key(ts->input,
1083                                                          key_array[1], 1);
1084                                         input_sync(ts->input);
1085                                         input_report_key(ts->input,
1086                                                          key_array[1], 0);
1087                                         input_sync(ts->input);
1088                                 }
1089                         }
1090                         /*printk(" key_x[key_count -1],  key_x[0],
1091                         key_y[key_count -1], key_y[0] is %d ,%d , %d , %d\n",
1092                         key_x[key_count -1], key_x[0], key_y[key_count -1],
1093                         key_y[0]);*/
1094                         if ((key_x[key_count - 1] - key_x[0] < 50)
1095                             && (key_x[key_count - 1] - key_x[0] >= -50)
1096                             && (key_y[key_count - 1] - key_y[0] < 50)
1097                             && (key_y[key_count - 1] - key_y[0] >= -50)
1098                             && (key_x[0] != 0) && (key_y[0] != 0)) {
1099                                 //queue_work(gsl_timer_workqueue,&ts->click_work);
1100                                 //printk(" send enter2 key by yuandan \n");
1101                                 //if(send_key)
1102                                 //      {
1103                                 printk(" send enter key \n");
1104                                 input_report_key(ts->input, key_array[4], 1);
1105                                 input_sync(ts->input);
1106                                 input_report_key(ts->input, key_array[4], 0);
1107                                 input_sync(ts->input);
1108                                 //      }else
1109                                 //              {
1110                                 //down(&my_sem);
1111                                 //                      send_key = true;
1112                                 //up(&my_sem);
1113                                 //              }
1114                         }
1115                 } else if (key_count > 512) {
1116                         if (temp_x > temp_y) {
1117                                 if ((key_x[511] - key_x[0]) > 100) {
1118                                         printk(" send up key \n");
1119                                         input_report_key(ts->input,
1120                                                          key_array[2], 1);
1121                                         input_sync(ts->input);
1122                                         input_report_key(ts->input,
1123                                                          key_array[2], 0);
1124                                         input_sync(ts->input);
1125                                 } else if ((key_x[0] - key_x[511]) > 100) {
1126                                         printk(" send down key \n");
1127                                         input_report_key(ts->input,
1128                                                          key_array[3], 1);
1129                                         input_sync(ts->input);
1130                                         input_report_key(ts->input,
1131                                                          key_array[3], 0);
1132                                         input_sync(ts->input);
1133                                 }
1134                         } else if (temp_x <= temp_y) {
1135
1136                                 if ((key_y[511] - key_y[0]) > 100) {
1137                                         printk(" send left key \n");
1138                                         input_report_key(ts->input,
1139                                                          key_array[0], 1);
1140                                         input_sync(ts->input);
1141                                         input_report_key(ts->input,
1142                                                          key_array[0], 0);
1143                                         input_sync(ts->input);
1144                                 } else if ((key_y[0] - key_y[511]) > 100) {
1145                                         printk(" send right key \n");
1146                                         input_report_key(ts->input,
1147                                                          key_array[1], 1);
1148                                         input_sync(ts->input);
1149                                         input_report_key(ts->input,
1150                                                          key_array[1], 0);
1151                                         input_sync(ts->input);
1152                                 }
1153                         }
1154                 }
1155                 memset(key_y, 0, sizeof(int) * 512);
1156                 memset(key_x, 0, sizeof(int) * 512);
1157                 key_count = 0;
1158 #endif
1159 #endif
1160 #ifdef HAVE_TOUCH_KEY
1161                 if (key_state_flag) {
1162                         input_report_key(ts->input, key, 0);
1163                         input_sync(ts->input);
1164                         key_state_flag = 0;
1165                 }
1166 #endif
1167
1168         }
1169
1170         input_sync(ts->input);
1171
1172       schedule:
1173 #ifdef GSL_MONITOR
1174         i2c_lock_flag = 0;
1175       i2c_lock_schedule:
1176 #endif
1177         enable_irq(ts->irq);
1178
1179 }
1180
1181 #ifdef HAVE_CLICK_TIMER
1182
1183 static void click_timer_worker(struct work_struct *work)
1184 {
1185         while (true) {
1186                 mdelay(500);
1187                 //down(&my_sem);
1188                 send_key = false;
1189                 //up(&my_sem);
1190         }
1191 }
1192
1193 #endif
1194
1195 #ifdef GSL_MONITOR
1196 static void gsl_monitor_worker(struct work_struct *work)
1197 {
1198         //u8 write_buf[4] = {0};
1199         u8 read_buf[4] = { 0 };
1200         char init_chip_flag = 0;
1201
1202         //print_info("gsl_monitor_worker\n");
1203         struct gsl_ts *ts =
1204             container_of(work, struct gsl_ts, gsl_monitor_work.work);
1205         if (i2c_lock_flag != 0) {
1206                 i2c_lock_flag = 1;
1207         }
1208         //goto queue_monitor_work;
1209         else
1210                 i2c_lock_flag = 1;
1211
1212         //gsl_ts_read(ts->client, 0x80, read_buf, 4);
1213         /*printk("======read 0x80: %x %x %x %x ======tony0geshu\n",
1214         read_buf[3], read_buf[2], read_buf[1], read_buf[0]);*/
1215
1216         gsl_ts_read(ts->client, 0xb0, read_buf, 4);
1217         if (read_buf[3] != 0x5a || read_buf[2] != 0x5a || read_buf[1] != 0x5a
1218             || read_buf[0] != 0x5a)
1219                 b0_counter++;
1220         else
1221                 b0_counter = 0;
1222
1223         if (b0_counter > 1) {
1224                 /*printk("======read 0xb0: %x %x %x %x ======\n",
1225                 read_buf[3], read_buf[2], read_buf[1], read_buf[0]);*/
1226                 init_chip_flag = 1;
1227                 b0_counter = 0;
1228         }
1229
1230         gsl_ts_read(ts->client, 0xb4, read_buf, 4);
1231         int_2nd[3] = int_1st[3];
1232         int_2nd[2] = int_1st[2];
1233         int_2nd[1] = int_1st[1];
1234         int_2nd[0] = int_1st[0];
1235         int_1st[3] = read_buf[3];
1236         int_1st[2] = read_buf[2];
1237         int_1st[1] = read_buf[1];
1238         int_1st[0] = read_buf[0];
1239
1240         /*printk("int_1st: %x %x %x %x , int_2nd: %x %x %x %x\n",
1241         int_1st[3], int_1st[2], int_1st[1], int_1st[0],
1242         int_2nd[3], int_2nd[2],int_2nd[1],int_2nd[0]);*/
1243
1244         if (int_1st[3] == int_2nd[3] && int_1st[2] == int_2nd[2]
1245             && int_1st[1] == int_2nd[1] && int_1st[0] == int_2nd[0]) {
1246                 /*printk("int_1st: %x %x %x %x , int_2nd: %x %x %x %x\n",
1247                 int_1st[3], int_1st[2], int_1st[1], int_1st[0],
1248                 int_2nd[3], int_2nd[2],int_2nd[1],int_2nd[0]);*/
1249                 init_chip_flag = 1;
1250                 //goto queue_monitor_init_chip;
1251         }
1252
1253         gsl_ts_read(ts->client, 0xbc, read_buf, 4);
1254         if (read_buf[3] != 0 || read_buf[2] != 0 || read_buf[1] != 0
1255             || read_buf[0] != 0)
1256                 bc_counter++;
1257         else
1258                 bc_counter = 0;
1259         if (bc_counter > 1) {
1260                 /*printk("======read 0xbc: %x %x %x %x======\n",
1261                 read_buf[3], read_buf[2], read_buf[1], read_buf[0]);*/
1262                 init_chip_flag = 1;
1263                 bc_counter = 0;
1264         }
1265
1266         /*
1267            write_buf[3] = 0x01;
1268            write_buf[2] = 0xfe;
1269            write_buf[1] = 0x10;
1270            write_buf[0] = 0x00;
1271            gsl_ts_write(ts->client, 0xf0, write_buf, 4);
1272            gsl_ts_read(ts->client, 0x10, read_buf, 4);
1273            gsl_ts_read(ts->client, 0x10, read_buf, 4);
1274
1275            if(read_buf[3] < 10
1276                 && read_buf[2] < 10
1277                 && read_buf[1] < 10
1278                 && read_buf[0] < 10)
1279            dac_counter ++;
1280            else
1281            dac_counter = 0;
1282
1283            if(dac_counter > 1)
1284            {
1285            printk("read DAC1_0: %x %x %x %x\n",
1286                 read_buf[3], read_buf[2], read_buf[1], read_buf[0]);
1287            init_chip_flag = 1;
1288            dac_counter = 0;
1289            }
1290          */
1291         //queue_monitor_init_chip:
1292         if (init_chip_flag)
1293                 init_chip(ts->client, ts);
1294
1295         i2c_lock_flag = 0;
1296
1297         //queue_monitor_work:
1298         //queue_delayed_work(gsl_monitor_workqueue, &ts->gsl_monitor_work, 100);
1299 }
1300 #endif
1301
1302 static irqreturn_t gsl_ts_irq(int irq, void *dev_id)
1303 {
1304         ///struct gsl_ts *ts = dev_id;
1305         struct gsl_ts *ts = (struct gsl_ts *)dev_id;
1306         //print_info("========gslX680 Interrupt=========\n");
1307
1308         disable_irq_nosync(ts->irq);
1309
1310         if (!work_pending(&ts->work)) {
1311                 queue_work(ts->wq, &ts->work);
1312         }
1313
1314         return IRQ_HANDLED;
1315
1316 }
1317
1318 static int gslX680_ts_init(struct i2c_client *client, struct gsl_ts *ts)
1319 {
1320         struct input_dev *input_device;
1321         int rc = 0;
1322         int i = 0;
1323
1324         printk("[GSLX680] Enter %s\n", __func__);
1325
1326         ts->dd = &devices[ts->device_id];
1327
1328         if (ts->device_id == 0) {
1329                 ts->dd->data_size =
1330                     MAX_FINGERS * ts->dd->touch_bytes + ts->dd->touch_meta_data;
1331                 ts->dd->touch_index = 0;
1332         }
1333
1334         ts->touch_data =
1335             devm_kzalloc(&client->dev, ts->dd->data_size, GFP_KERNEL);
1336         if (!ts->touch_data) {
1337                 pr_err("%s: Unable to allocate memory\n", __func__);
1338                 return -ENOMEM;
1339         }
1340
1341         input_device = devm_input_allocate_device(&ts->client->dev);
1342         if (!input_device) {
1343                 rc = -ENOMEM;
1344                 goto init_err_ret;
1345         }
1346
1347         ts->input = input_device;
1348         input_device->name = GSLX680_I2C_NAME;
1349         input_device->id.bustype = BUS_I2C;
1350         input_device->dev.parent = &client->dev;
1351         input_set_drvdata(input_device, ts);
1352
1353 #ifdef REPORT_DATA_ANDROID_4_0
1354         __set_bit(EV_ABS, input_device->evbit);
1355         __set_bit(EV_KEY, input_device->evbit);
1356         __set_bit(EV_REP, input_device->evbit);
1357         __set_bit(EV_SYN, input_device->evbit);
1358         __set_bit(INPUT_PROP_DIRECT, input_device->propbit);
1359         __set_bit(MT_TOOL_FINGER, input_device->keybit);
1360         input_mt_init_slots(input_device, (MAX_CONTACTS + 1), 0);
1361 #else
1362         input_set_abs_params(input_device, ABS_MT_TRACKING_ID, 0,
1363                              (MAX_CONTACTS + 1), 0, 0);
1364         set_bit(EV_ABS, input_device->evbit);
1365         set_bit(EV_KEY, input_device->evbit);
1366         __set_bit(INPUT_PROP_DIRECT, input_device->propbit);
1367         input_device->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1368 #endif
1369
1370 #ifdef HAVE_TOUCH_KEY
1371         input_device->evbit[0] = BIT_MASK(EV_KEY);
1372         /*input_device->evbit[0] = BIT_MASK(EV_SYN)
1373                 | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);*/
1374         for (i = 0; i < MAX_KEY_NUM; i++)
1375                 set_bit(key_array[i], input_device->keybit);
1376 #endif
1377
1378 #ifdef RK_GEAR_TOUCH
1379         set_bit(EV_REL, input_device->evbit);
1380         input_set_capability(input_device, EV_REL, REL_X);
1381         input_set_capability(input_device, EV_REL, REL_Y);
1382         input_set_capability(input_device, EV_MSC, MSC_SCAN);
1383         input_set_capability(input_device, EV_KEY, 0x110);
1384 #endif
1385
1386         set_bit(ABS_MT_POSITION_X, input_device->absbit);
1387         set_bit(ABS_MT_POSITION_Y, input_device->absbit);
1388         set_bit(ABS_MT_TOUCH_MAJOR, input_device->absbit);
1389         set_bit(ABS_MT_WIDTH_MAJOR, input_device->absbit);
1390
1391         input_set_abs_params(input_device, ABS_MT_POSITION_X, 0, SCREEN_MAX_X,
1392                              0, 0);
1393         input_set_abs_params(input_device, ABS_MT_POSITION_Y, 0, SCREEN_MAX_Y,
1394                              0, 0);
1395         input_set_abs_params(input_device, ABS_MT_TOUCH_MAJOR, 0, PRESS_MAX, 0,
1396                              0);
1397         input_set_abs_params(input_device, ABS_MT_WIDTH_MAJOR, 0, 200, 0, 0);
1398
1399         //client->irq = IRQ_PORT;
1400         //ts->irq = client->irq;
1401
1402         ts->wq = create_singlethread_workqueue("kworkqueue_ts");
1403         if (!ts->wq) {
1404                 dev_err(&client->dev, "gsl Could not create workqueue\n");
1405                 goto init_err_ret;
1406         }
1407         flush_workqueue(ts->wq);
1408
1409         INIT_WORK(&ts->work, gslX680_ts_worker);
1410
1411         rc = input_register_device(input_device);
1412         if (rc)
1413                 goto error_unreg_device;
1414
1415         return 0;
1416
1417       error_unreg_device:
1418         destroy_workqueue(ts->wq);
1419       init_err_ret:
1420         return rc;
1421 }
1422
1423 #if 0
1424 static int gsl_ts_suspend(struct i2c_client *dev, pm_message_t mesg)
1425 {
1426 #if 0
1427         struct gsl_ts *ts = dev_get_drvdata(dev);
1428
1429         printk("I'am in gsl_ts_suspend() start\n");
1430
1431 #ifdef GSL_MONITOR
1432         printk("gsl_ts_suspend () : cancel gsl_monitor_work\n");
1433         cancel_delayed_work_sync(&ts->gsl_monitor_work);
1434 #endif
1435
1436 #ifdef HAVE_CLICK_TIMER
1437         //cancel_work_sync(&ts->click_work);
1438 #endif
1439         disable_irq_nosync(ts->irq);
1440
1441         gslX680_shutdown_low(ts);
1442
1443 #ifdef SLEEP_CLEAR_POINT
1444         mdelay(10);
1445 #ifdef REPORT_DATA_ANDROID_4_0
1446         for (i = 1; i <= MAX_CONTACTS; i++) {
1447                 input_mt_slot(ts->input, i);
1448                 input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
1449                 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
1450         }
1451 #else
1452         input_mt_sync(ts->input);
1453 #endif
1454         input_sync(ts->input);
1455         mdelay(10);
1456         report_data(ts, 1, 1, 10, 1);
1457         input_sync(ts->input);
1458 #endif
1459
1460 #endif
1461         return 0;
1462 }
1463 #endif
1464
1465 #if 0
1466 static int gsl_ts_resume(struct i2c_client *dev)
1467 {
1468 #if 0
1469         struct gsl_ts *ts = dev_get_drvdata(dev);
1470
1471         printk("I'am in gsl_ts_resume() start\n");
1472
1473         gslX680_shutdown_high(ts);
1474         msleep(20);
1475         reset_chip(ts->client);
1476         startup_chip(ts->client);
1477         check_mem_data(ts->client, ts);
1478
1479 #ifdef SLEEP_CLEAR_POINT
1480 #ifdef REPORT_DATA_ANDROID_4_0
1481         for (i = 1; i <= MAX_CONTACTS; i++) {
1482                 input_mt_slot(ts->input, i);
1483                 input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
1484                 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
1485         }
1486 #else
1487         input_mt_sync(ts->input);
1488 #endif
1489         input_sync(ts->input);
1490 #endif
1491 #ifdef GSL_MONITOR
1492         printk("gsl_ts_resume () : queue gsl_monitor_work\n");
1493         queue_delayed_work(gsl_monitor_workqueue, &ts->gsl_monitor_work, 300);
1494 #endif
1495
1496 #ifdef HAVE_CLICK_TIMER
1497         //queue_work(gsl_timer_workqueue,&ts->click_work);
1498 #endif
1499
1500         disable_irq_nosync(ts->irq);
1501         enable_irq(ts->irq);
1502 #endif
1503
1504         return 0;
1505 }
1506 #endif 
1507
1508 static int gsl_ts_early_suspend(struct tp_device *tp_d)
1509 {
1510         struct gsl_ts *ts = container_of(tp_d, struct gsl_ts, tp);
1511         printk("[GSLX680] Enter %s\n", __func__);
1512         //gsl_ts_suspend(&ts->client->dev);
1513 #ifdef GSL_MONITOR
1514         printk("gsl_ts_suspend () : cancel gsl_monitor_work\n");
1515         cancel_delayed_work_sync(&ts->gsl_monitor_work);
1516 #endif
1517
1518         disable_irq_nosync(ts->irq);
1519
1520 #ifdef SLEEP_CLEAR_POINT
1521         msleep(10);
1522 #ifdef REPORT_DATA_ANDROID_4_0
1523         for (i = 1; i <= MAX_CONTACTS; i++) {
1524                 input_mt_slot(ts->input, i);
1525                 input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
1526                 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
1527         }
1528 #else
1529         input_mt_sync(ts->input);
1530 #endif
1531         input_sync(ts->input);
1532         msleep(10);
1533         report_data(ts, 1, 1, 10, 1);
1534         input_sync(ts->input);
1535 #endif
1536         gslX680_shutdown_low(ts);
1537         return 0;
1538 }
1539
1540 static int gsl_ts_late_resume(struct tp_device *tp_d)
1541 {
1542         struct gsl_ts *ts = container_of(tp_d, struct gsl_ts, tp);
1543         printk("[GSLX680] Enter %s\n", __func__);
1544         //gsl_ts_resume(&ts->client->dev);
1545
1546         printk("I'am in gsl_ts_resume() start\n");
1547
1548         gslX680_shutdown_high(ts);
1549         msleep(20);
1550         reset_chip(ts->client);
1551         startup_chip(ts->client);
1552         check_mem_data(ts->client, ts);
1553
1554 #ifdef SLEEP_CLEAR_POINT
1555 #ifdef REPORT_DATA_ANDROID_4_0
1556         for (i = 1; i <= MAX_CONTACTS; i++) {
1557                 input_mt_slot(ts->input, i);
1558                 input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
1559                 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
1560         }
1561 #else
1562         input_mt_sync(ts->input);
1563 #endif
1564         input_sync(ts->input);
1565 #endif
1566 #ifdef GSL_MONITOR
1567         printk("gsl_ts_resume () : queue gsl_monitor_work\n");
1568         queue_delayed_work(gsl_monitor_workqueue, &ts->gsl_monitor_work, 300);
1569 #endif
1570         disable_irq_nosync(ts->irq);
1571         enable_irq(ts->irq);
1572
1573         return 0;
1574 }
1575
1576 #ifdef CONFIG_HAS_EARLYSUSPEND
1577
1578 static void gsl_ts_early_suspend(struct early_suspend *h)
1579 {
1580         struct gsl_ts *ts = container_of(h, struct gsl_ts, early_suspend);
1581         printk("[GSLX680] Enter %s\n", __func__);
1582         //gsl_ts_suspend(&ts->client->dev);
1583 #ifdef GSL_MONITOR
1584         printk("gsl_ts_suspend () : cancel gsl_monitor_work\n");
1585         cancel_delayed_work_sync(&ts->gsl_monitor_work);
1586 #endif
1587
1588         disable_irq_nosync(ts->irq);
1589
1590 #ifdef SLEEP_CLEAR_POINT
1591         msleep(10);
1592 #ifdef REPORT_DATA_ANDROID_4_0
1593         for (i = 1; i <= MAX_CONTACTS; i++) {
1594                 input_mt_slot(ts->input, i);
1595                 input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
1596                 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
1597         }
1598 #else
1599         input_mt_sync(ts->input);
1600 #endif
1601         input_sync(ts->input);
1602         msleep(10);
1603         report_data(ts, 1, 1, 10, 1);
1604         input_sync(ts->input);
1605 #endif
1606         gslX680_shutdown_low(ts);
1607         return 0;
1608 }
1609
1610 static void gsl_ts_late_resume(struct early_suspend *h)
1611 {
1612         struct gsl_ts *ts = container_of(h, struct gsl_ts, early_suspend);
1613         printk("[GSLX680] Enter %s\n", __func__);
1614         //gsl_ts_resume(&ts->client->dev);
1615         int i;
1616
1617         printk("I'am in gsl_ts_resume() start\n");
1618
1619         gslX680_shutdown_high(ts);
1620         msleep(20);
1621         reset_chip(ts->client);
1622         startup_chip(ts->client);
1623         check_mem_data(ts->client, ts);
1624
1625 #ifdef SLEEP_CLEAR_POINT
1626 #ifdef REPORT_DATA_ANDROID_4_0
1627         for (i = 1; i <= MAX_CONTACTS; i++) {
1628                 input_mt_slot(ts->input, i);
1629                 input_report_abs(ts->input, ABS_MT_TRACKING_ID, -1);
1630                 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, false);
1631         }
1632 #else
1633         input_mt_sync(ts->input);
1634 #endif
1635         input_sync(ts->input);
1636 #endif
1637 #ifdef GSL_MONITOR
1638         printk("gsl_ts_resume () : queue gsl_monitor_work\n");
1639         queue_delayed_work(gsl_monitor_workqueue, &ts->gsl_monitor_work, 300);
1640 #endif
1641         disable_irq_nosync(ts->irq);
1642         enable_irq(ts->irq);
1643 }
1644 #endif
1645
1646 //static struct wake_lock touch_wakelock;
1647
1648 static int gsl_ts_probe(struct i2c_client *client,
1649                         const struct i2c_device_id *id)
1650 {
1651         struct gsl_ts *ts;
1652         int rc;
1653
1654         printk("GSLX680 Enter %s\n", __func__);
1655         //wake_lock_init(&touch_wakelock, WAKE_LOCK_SUSPEND, "touch");
1656         //wake_lock(&touch_wakelock); //system do not enter deep sleep
1657         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1658                 dev_err(&client->dev, "gsl I2C functionality not supported\n");
1659                 return -ENODEV;
1660         }
1661
1662         ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1663         if (!ts)
1664                 return -ENOMEM;
1665
1666         ts->tp.tp_suspend = gsl_ts_early_suspend;
1667         ts->tp.tp_resume = gsl_ts_late_resume;
1668         tp_register_fb(&ts->tp);
1669
1670         ts->client = client;
1671         i2c_set_clientdata(client, ts);
1672         //ts->device_id = id->driver_data;
1673
1674         gslX680_init(ts);
1675         rc = gslX680_ts_init(client, ts);
1676         if (rc < 0) {
1677                 dev_err(&client->dev, "gsl GSLX680 init failed\n");
1678                 goto porbe_err_ret;
1679         }
1680         //#ifdef GSLX680_COMPATIBLE
1681         //      judge_chip_type(client);
1682         //#endif
1683         //printk("#####################  probe [2]chip_type=%c .\n",chip_type);
1684         init_chip(ts->client, ts);
1685         check_mem_data(ts->client, ts);
1686
1687         client->irq = gpio_to_irq(ts->irq);
1688         rc = request_irq(client->irq, gsl_ts_irq, IRQF_TRIGGER_RISING,
1689                          client->name, ts);
1690         if (rc < 0) {
1691                 printk("gsl_probe: request irq failed\n");
1692                 goto porbe_err_ret;
1693         }
1694
1695         /* create debug attribute */
1696         //rc = device_create_file(&ts->input->dev, &dev_attr_debug_enable);
1697
1698 #ifdef CONFIG_HAS_EARLYSUSPEND
1699
1700         ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
1701         //ts->early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB + 1;
1702         ts->early_suspend.suspend = gsl_ts_early_suspend;
1703         ts->early_suspend.resume = gsl_ts_late_resume;
1704         register_early_suspend(&ts->early_suspend);
1705 #endif
1706
1707 #ifdef GSL_MONITOR
1708
1709         INIT_DELAYED_WORK(&ts->gsl_monitor_work, gsl_monitor_worker);
1710         gsl_monitor_workqueue =
1711             create_singlethread_workqueue("gsl_monitor_workqueue");
1712         queue_delayed_work(gsl_monitor_workqueue, &ts->gsl_monitor_work, 1000);
1713 #endif
1714
1715 #ifdef HAVE_CLICK_TIMER
1716         sema_init(&my_sem, 1);
1717         INIT_WORK(&ts->click_work, click_timer_worker);
1718         gsl_timer_workqueue = create_singlethread_workqueue("click_timer");
1719         queue_work(gsl_timer_workqueue, &ts->click_work);
1720 #endif
1721
1722 #ifdef TPD_PROC_DEBUG
1723 #if 0
1724         gsl_config_proc = create_proc_entry(GSL_CONFIG_PROC_FILE, 0666, NULL);
1725         printk("[tp-gsl] [%s] gsl_config_proc = %x \n", __func__,
1726                gsl_config_proc);
1727         if (gsl_config_proc == NULL) {
1728                 print_info("create_proc_entry %s failed\n",
1729                            GSL_CONFIG_PROC_FILE);
1730         } else {
1731                 gsl_config_proc->read_proc = gsl_config_read_proc;
1732                 gsl_config_proc->write_proc = gsl_config_write_proc;
1733         }
1734 #else
1735         i2c_client = client;
1736         proc_create(GSL_CONFIG_PROC_FILE, 0666, NULL, &gsl_seq_fops);
1737 #endif
1738         gsl_proc_flag = 0;
1739 #endif
1740         //disable_irq_nosync(->irq);
1741         printk("[GSLX680] End %s\n", __func__);
1742
1743         return 0;
1744
1745       porbe_err_ret:
1746         return rc;
1747 }
1748
1749 static int gsl_ts_remove(struct i2c_client *client)
1750 {
1751         struct gsl_ts *ts = i2c_get_clientdata(client);
1752
1753 #ifdef CONFIG_HAS_EARLYSUSPEND
1754         unregister_early_suspend(&ts->early_suspend);
1755 #endif
1756
1757 #ifdef GSL_MONITOR
1758         cancel_delayed_work_sync(&ts->gsl_monitor_work);
1759         destroy_workqueue(gsl_monitor_workqueue);
1760 #endif
1761
1762 #ifdef HAVE_CLICK_TIMER
1763         cancel_work_sync(&ts->click_work);
1764         destroy_workqueue(gsl_timer_workqueue);
1765 #endif
1766
1767         device_init_wakeup(&client->dev, 0);
1768         cancel_work_sync(&ts->work);
1769         free_irq(ts->irq, ts);
1770         destroy_workqueue(ts->wq);
1771         //device_remove_file(&ts->input->dev, &dev_attr_debug_enable);
1772
1773         return 0;
1774 }
1775
1776 static struct of_device_id gsl_ts_ids[] = {
1777         {.compatible = "gslX680"},
1778         {}
1779 };
1780
1781 static const struct i2c_device_id gsl_ts_id[] = {
1782         {GSLX680_I2C_NAME, 0},
1783         {}
1784 };
1785
1786 MODULE_DEVICE_TABLE(i2c, gsl_ts_id);
1787
1788 static struct i2c_driver gsl_ts_driver = {
1789         .driver = {
1790                    .name = GSLX680_I2C_NAME,
1791                    .owner = THIS_MODULE,
1792                    .of_match_table = of_match_ptr(gsl_ts_ids),
1793                    },
1794 #if 0 //ndef CONFIG_HAS_EARLYSUSPEND
1795         .suspend = gsl_ts_suspend,
1796         .resume = gsl_ts_resume,
1797 #endif
1798         .probe = gsl_ts_probe,
1799         .remove = gsl_ts_remove,
1800         .id_table = gsl_ts_id,
1801 };
1802
1803 static int __init gsl_ts_init(void)
1804 {
1805         int ret;
1806         ret = i2c_add_driver(&gsl_ts_driver);
1807         return ret;
1808 }
1809 static void __exit gsl_ts_exit(void)
1810 {
1811         i2c_del_driver(&gsl_ts_driver);
1812         return;
1813 }
1814
1815 module_init(gsl_ts_init);
1816 module_exit(gsl_ts_exit);
1817
1818 MODULE_LICENSE("GPL");
1819 MODULE_DESCRIPTION("GSLX680 touchscreen controller driver");
1820 MODULE_AUTHOR("Guan Yuwei, guanyuwei@basewin.com");
1821 MODULE_ALIAS("platform:gsl_ts");