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