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