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