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