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