xref: /OK3568_Linux_fs/kernel/drivers/input/touchscreen/elan/elan_ts.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * ELAN HID-I2C TouchScreen driver.
3  *
4  * Copyright (C) 2014 Elan Microelectronics Corporation.
5  * Chuming Zhang <chuming.zhang@elanic.com.cn>
6  *
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17 */
18 
19 #include <linux/module.h>
20 #include <linux/input.h>
21 #include <linux/input/mt.h>
22 #include <linux/interrupt.h>
23 #include <linux/of.h>
24 #include <linux/kthread.h>
25 #include <linux/proc_fs.h>
26 #include <linux/of_gpio.h>
27 #include <linux/regulator/consumer.h>
28 //#include <linux/wakelock.h>
29 #include <linux/pm_wakeup.h>
30 #include <linux/string.h>
31 #include <linux/delay.h>
32 #include "elan_ts.h"
33 
34 
35 
36 
37 #if defined(CONFIG_FB)
38 static int fb_notifier_callback(struct notifier_block *self,unsigned long event, void *data);
39 #elif defined(CONFIG_HAS_EARLYSUSPEND)
40 static void elan_ts_early_suspend(struct early_suspend *h);
41 static void elan_ts_late_resume(struct early_suspend *h);
42 #endif
43 
44 //define private data
45 struct elan_ts_data *private_ts;
46 unsigned long delay = HZ;
47 
48 /************************key event define**************************/
49 static const int key_value[] = {KEY_MENU, KEY_HOMEPAGE, KEY_BACK};
50 
elan_ts_hw_reset(struct ts_chip_hw_info * hw_info)51 void elan_ts_hw_reset(struct ts_chip_hw_info *hw_info)
52 {
53 	gpio_set_value(hw_info->rst_gpio, 1);
54 	msleep(10);
55 	gpio_set_value(hw_info->rst_gpio, 0);
56 	msleep(100);
57 	gpio_set_value(hw_info->rst_gpio, 1);
58         printk("[elan] elan_ts_hw_reset()\n");
59 }
60 
elan_switch_irq(struct elan_ts_data * ts,int on)61 void elan_switch_irq(struct elan_ts_data *ts, int on)
62 {
63 //	struct elan_ts_data *ts = private_ts;
64 
65 	dev_err(&ts->client->dev,
66 		"[elan] %s enter, irq = %d, on = %d, irq_lock_flag=%d\n",
67 		__func__, ts->hw_info.irq_num, on, ts->irq_lock_flag);
68 	mutex_lock(&ts->irq_mutex);
69 	if (on) {
70 		if(ts->irq_lock_flag == 1) {
71 			enable_irq(ts->hw_info.irq_num);
72 			ts->irq_lock_flag = 0;
73 		}
74 	} else {
75 		if(ts->irq_lock_flag == 0) {
76 			disable_irq(ts->hw_info.irq_num);
77 			ts->irq_lock_flag = 1;
78 		}
79 	}
80 	mutex_unlock(&ts->irq_mutex);
81 }
82 
elan_poll_int(void)83 static int elan_poll_int(void)
84 {
85 	int status = 0, retry = 50;//20;
86 
87 	do {
88 		status = gpio_get_value(private_ts->hw_info.intr_gpio);
89 		if (status == 0)
90 			break;
91 		retry--;
92 		msleep(10);//msleep(20);
93 	} while (status == 1 && retry > 0);
94 
95 	if (status > 0)
96 		dev_info(&private_ts->client->dev,
97 				"%s: poll interrupt status %s\n",\
98 				__func__, status == 1 ? "high" : "low");
99 
100 	return status == 0 ? 0 : -ETIMEDOUT;
101 }
102 
elan_i2c_send(const uint8_t * buf,int count)103 static int elan_i2c_send(const uint8_t *buf, int count)
104 {
105 	int ret = -1;
106 	int retries = 0;
107 	struct i2c_client *client = private_ts->client;
108 	struct i2c_adapter *adap = client->adapter;
109 	struct i2c_msg msg;
110 
111 	msg.addr = client->addr;
112 	msg.flags = client->flags & I2C_M_TEN;
113 	msg.len = count;
114 	msg.buf = (char *)buf;
115 
116 	while(retries < 5)
117 	{
118 		ret = i2c_transfer(adap, &msg, 1);
119 		if (ret == 1)break;
120 			retries++;
121 	}
122 
123 	/*
124 	 * If everything went ok (i.e. 1 msg transmitted), return #bytes
125 	 * transmitted, else error code.
126 	 */
127 	return (ret == 1) ? count : ret;
128 }
129 
elan_i2c_recv(uint8_t * buf,int count)130 static int elan_i2c_recv(uint8_t *buf, int count)
131 {
132 	int ret = -1;
133 	int retries = 0;
134 	struct i2c_client *client = private_ts->client;
135 	struct i2c_adapter *adap = client->adapter;
136 	struct i2c_msg msg;
137 
138 	msg.addr = client->addr;
139 	msg.flags = client->flags & I2C_M_TEN;
140 	msg.flags |= I2C_M_RD;
141 	msg.len = count;
142 	msg.buf = buf;
143 
144 	while(retries < 5)
145 	{
146 		ret = i2c_transfer(adap, &msg, 1);
147 		if(ret == 1)break;
148 			retries++;
149 	}
150 	/*
151 	 * If everything went ok (i.e. 1 msg received), return #bytes received,
152 	 * else error code.
153 	 */
154 
155 	return (ret == 1) ? count : ret;
156 }
157 
158 struct elan_i2c_operation elan_ops = {
159 	.send	=	elan_i2c_send,
160 	.recv	=	elan_i2c_recv,
161 	.poll	=	elan_poll_int,
162 };
163 
elan_ic_status(struct i2c_client * client)164 int elan_ic_status(struct i2c_client *client)
165 {
166 	uint8_t checkstatus[HID_CMD_LEN] = {0x04, 0x00, 0x23, 0x00, 0x03, 0x18};
167 	uint8_t buf[HID_RECV_LEN] = {0x00};
168 	int err = 0;
169 	struct elan_ts_data *ts = i2c_get_clientdata(client);
170 	int retry = 3;
171 
172 	/*HID protocol after reset need delay 300ms*/
173 	msleep(100);
174 
175 RETRY:
176 	err = ts->ops->send(checkstatus,sizeof(checkstatus));
177 	if (err != sizeof(checkstatus)) {
178 		dev_err(&client->dev,
179 				"[elan] ERROR: Send get hid hello cmd fail!len=%d\n", err);
180 		if((retry--)> 0)
181 			goto RETRY;
182 		return -1;
183 	}
184 
185 	err = ts->ops->poll();//elan_poll_int();
186 	if (err) {
187 		dev_err(&client->dev,
188 				"[elan] ERROR: %s INT status high",__func__);
189 
190 		if((retry--) > 0)
191 			goto RETRY;
192 		return err;
193 	}
194 
195 	err = ts->ops->recv(buf, sizeof(buf));
196 	if (err != sizeof(buf)) {
197 		dev_err(&client->dev,
198 				"[elan] ERROR:%s Read Hello Data error\n", __func__);
199 		if((retry--) > 0)
200 			goto RETRY;
201 		return -1;
202 	}
203 
204 	dev_err(&client->dev, "[elan] FW Mode = 0x%2x\n",buf[4]);
205 	if ( HID_FW_NORMAL_MODE == buf[4]) {
206 		return COMPARE_UPGRADE;
207 	} else if (HID_FW_RECOVERY_MODE == buf[4]) {
208 		if (buf[6] != buf[7])
209 			ts->fw_info.fw_bcl  = buf[7];
210 		else
211 			ts->fw_info.fw_bcl  = buf[4];
212 		return FORCED_UPGRADE;
213 	}else
214 		return UNKNOW_TYPE;
215 }
216 
get_normal_hello(struct i2c_client * client)217 static int get_normal_hello(struct i2c_client *client)
218 {
219 	int err = 0;
220 	uint8_t buf[8] = { 0 };
221 	uint8_t normal_hello[4] = {NORMAL_FW_NORMAL_MODE,NORMAL_FW_NORMAL_MODE,NORMAL_FW_NORMAL_MODE,NORMAL_FW_NORMAL_MODE};
222 	uint8_t recovery_hello[4] = {NORMAL_FW_NORMAL_MODE,NORMAL_FW_NORMAL_MODE,NORMAL_FW_RECOVERY_MODE,NORMAL_FW_RECOVERY_MODE};
223 	struct elan_ts_data *ts = i2c_get_clientdata(client);
224 
225 	err = ts->ops->poll();//elan_poll_int();
226 	if (err) {
227 		dev_err(&client->dev,
228 				"[elan] ERROR: %s INT status high",__func__);
229 		return -1;
230 	}
231 
232 	err = ts->ops->recv(buf, sizeof(buf));
233 	if (err != sizeof(buf)) {
234 		dev_err(&client->dev,
235 				"[elan] ERROR:%s Read Hello Data error\n", __func__);
236 		return -1;
237 	}
238 
239 	if( memcmp(buf,normal_hello,sizeof(normal_hello)) == 0) {
240 		dev_info(&client->dev, "[elan] hello packet check success!!\n");
241 		return COMPARE_UPGRADE;
242 	}
243 	else if( memcmp(buf,recovery_hello,sizeof(recovery_hello)) == 0) {
244 		dev_info(&client->dev, "[elan] hello packet check faile!!\n");
245 		return FORCED_UPGRADE;
246 	} else {
247 		dev_info(&client->dev, "[elan] recive hello packet error!!\n");
248 		return UNKNOW_TYPE;
249 	}
250 }
251 
elan__hello_packet_handler(struct i2c_client * client,int chip_type)252 int elan__hello_packet_handler(struct i2c_client *client, int chip_type)
253 {
254 	int ret = 0;
255 	struct elan_ts_data *ts = i2c_get_clientdata(client);
256 
257 	dev_err(&client->dev, "[elan] chip_type =%d\n", chip_type);
258 	if (HID_TYPE_PROTOCOL == chip_type) {
259 		ret = elan_ic_status(client);
260 	} else if (chip_type == NORMAL_TYPE_PROTOCOL) {
261 		ret = get_normal_hello(client);
262 	}
263 
264 	ts->recover = ret;
265 	dev_err(&client->dev, "[elan] ts->recover =%d\n", ts->recover);
266 	return ret;
267 }
268 
elan_ts_get_data(struct i2c_client * client,const uint8_t * wbuf,size_t wsize,uint8_t * rbuf,size_t rsize)269 static int elan_ts_get_data(struct i2c_client *client, const uint8_t *wbuf,
270 		size_t wsize, uint8_t *rbuf, size_t rsize)
271 {
272 	int err = 0;
273 	struct elan_ts_data *ts = i2c_get_clientdata(client);
274 
275 	if (!wbuf || !rbuf)
276 		return -EINVAL;
277 
278 	err = ts->ops->send(wbuf, wsize);
279 	if(err != wsize) {
280 		dev_err(&client->dev, "[elan] %s send cmd faile\n",__func__);
281 		err = -1;
282 		return err;
283 	}
284 
285 	err = ts->ops->poll();//elan_poll_int();
286 	if (err !=  0) {
287 		dev_err(&client->dev, "[elan] %s Int status hight\n",__func__);
288 		return err;
289 	}
290 
291 	err = ts->ops->recv(rbuf,rsize);
292 	if (err != rsize) {
293 		dev_err(&client->dev, "[elan] %s cmd respone error\n",__func__);
294 		err = -1;
295 		return err;
296 	} else {
297 		if (ts->chip_type == HID_TYPE_PROTOCOL) {
298 			if ((CMD_S_PKT == rbuf[4])|| (REG_S_PKT == rbuf[4]))
299 				return 0;
300 			else
301 				return -EINVAL;
302 		} else {
303 			if ((CMD_S_PKT == rbuf[0]) || (REG_S_PKT == rbuf[0]))
304 				return 0;
305 			else
306 				return -EINVAL;
307 		}
308 	}
309 }
310 
hid_fw_packet_handler(struct i2c_client * client)311 static int hid_fw_packet_handler(struct i2c_client *client)
312 {
313 	const int pen_osr = 260; /*Ntring=256, Warcon: 260*/
314 	const uint8_t cmd_ver[HID_CMD_LEN]		=   {0x04,0x00,0x23,0x00,0x03,0x00,0x04,0x53,0x00,0x00,0x01};
315 	const uint8_t cmd_id[HID_CMD_LEN]		=   {0x04,0x00,0x23,0x00,0x03,0x00,0x04,0x53,0xf0,0x00,0x01};
316 	const uint8_t cmd_bc[HID_CMD_LEN]		=   {0x04,0x00,0x23,0x00,0x03,0x00,0x04,0x53,0x10,0x00,0x01};
317 	const uint8_t cmd_osr[HID_CMD_LEN]		=	{0x04,0x00,0x23,0x00,0x03,0x00,0x04,0x53,0xD6,0x00,0x01};
318 	const uint8_t cmd_test_ver[HID_CMD_LEN] =	{0x04,0x00,0x23,0x00,0x03,0x00,0x04,0x53,0xe0,0x00,0x01};
319 	const uint8_t cmd_whck_ver[HID_CMD_LEN] =	{0x04,0x00,0x23,0x00,0x03,0x00,0x04,0x53,0xd2,0x00,0x01};
320 	const uint8_t cmd_res[HID_CMD_LEN]      =	{0x04,0x00,0x23,0x00,0x03,0x00,0x04,0x5B,0x00,0x00,0x00,0x00,0x00};
321 	uint8_t rbuf[HID_RECV_LEN] = {0};
322 	int err = 0;
323 	int major, minor;
324 	struct elan_ts_data *ts = i2c_get_clientdata(client);
325 	struct elan_fw_info *fw_info = &ts->fw_info;
326 
327 	/*fw version*/
328 	err = elan_ts_get_data(client, cmd_ver, sizeof(cmd_ver), rbuf, sizeof(rbuf));
329 	if ( err ) {
330 		dev_err(&client->dev, "[elan] %s get fw version failed\n",__func__);
331 		return err;
332 	}
333 	major = ((rbuf[5] & 0x0f) << 4) | ((rbuf[6] & 0xf0) >> 4);
334 	minor = ((rbuf[6] & 0x0f) << 4) | ((rbuf[7] & 0xf0) >> 4);
335 	fw_info->fw_ver = major << 8 | minor;
336 
337 
338 	/*fw id*/
339 	err = elan_ts_get_data(client, cmd_id, sizeof(cmd_id),rbuf,sizeof(rbuf));
340 	if ( err ) {
341 		dev_err(&client->dev, "[elan] %s get fw id failed\n",__func__);
342 		return err;
343 	}
344 	major = ((rbuf[5] & 0x0f) << 4) | ((rbuf[6] & 0xf0) >> 4);
345 	minor = ((rbuf[6] & 0x0f) << 4) | ((rbuf[7] & 0xf0) >> 4);
346 	fw_info->fw_id = major << 8 | minor;
347 
348 	/*get bootcode version*/
349 	err = elan_ts_get_data(client, cmd_bc, sizeof(cmd_bc),rbuf,sizeof(rbuf));
350 	if ( err ) {
351 		dev_err(&client->dev, "[elan] %s get bootcode version failed\n",__func__);
352 		return err;
353 	}
354 	major = ((rbuf[5] & 0x0f) << 4) | ((rbuf[6] & 0xf0) >> 4);
355 	minor = ((rbuf[6] & 0x0f) << 4) | ((rbuf[7] & 0xf0) >> 4);
356 	fw_info->fw_bcv = major << 8 | minor;
357 	fw_info->fw_bcl = minor;
358 
359 	/*get finger osr*/
360 	err = elan_ts_get_data(client, cmd_osr, sizeof(cmd_osr),rbuf,sizeof(rbuf));
361 	if ( err ) {
362 		dev_err(&client->dev, "[elan] %s get finger osr failed\n",__func__);
363 		return err;
364 	}
365 	fw_info->finger_osr = rbuf[7];
366 
367 	/*get trace num*/
368 	err = elan_ts_get_data(client, cmd_res, sizeof(cmd_res),rbuf,sizeof(rbuf));
369 	if ( err ) {
370 		dev_err(&client->dev, "[elan] %s get finger osr failed\n",__func__);
371 		return err;
372 	}
373 
374 	// for ic rx = buf[6], tx = buf[7], rx > tx
375 	fw_info->rx = rbuf[6];
376 	fw_info->tx = rbuf[7];
377 	/*finger resolution*/
378 	fw_info->finger_xres = (rbuf[6] * 2 - 1) * fw_info->finger_osr;
379 	fw_info->finger_yres = (rbuf[7] - 1) * fw_info->finger_osr;
380 
381 	/*pen resolution*/
382 	fw_info->pen_xres = (rbuf[6] * 2 - 1) * pen_osr;
383 	fw_info->pen_yres = (rbuf[7] - 1) * pen_osr;
384 
385 	/*get test ver*/
386 	err = elan_ts_get_data(client, cmd_test_ver, sizeof(cmd_test_ver),rbuf,sizeof(rbuf));
387 	if ( err ) {
388 		dev_err(&client->dev, "[elan] %s get test ver failed\n",__func__);
389 		return err;
390 	}
391 	major = ((rbuf[5] & 0x0f) << 4) | ((rbuf[6] & 0xf0) >> 4);
392 	minor = ((rbuf[6] & 0x0f) << 4) | ((rbuf[7] & 0xf0) >> 4);
393 	fw_info->testsolversion  = major << 8 | minor;
394 	fw_info->testversion = major;
395 	fw_info->solutionversion = minor;
396 
397 	/*get whck ver*/
398 	err = elan_ts_get_data(client, cmd_whck_ver, sizeof(cmd_whck_ver),rbuf,sizeof(rbuf));
399 	if ( err ) {
400 		dev_err(&client->dev, "[elan] %s get test ver failed\n",__func__);
401 		return err;
402 	}
403 	major = ((rbuf[5] & 0x0f) << 4) | ((rbuf[6] & 0xf0) >> 4);
404 	minor = ((rbuf[6] & 0x0f) << 4) | ((rbuf[7] & 0xf0) >> 4);
405 	fw_info->whck_ver = major << 8 | minor;
406 
407 	dev_info(&client->dev,
408 			"[elan] %s fw version:0x%4.4x\n",
409 			__func__,fw_info->fw_ver);
410 	dev_info(&client->dev,
411 			"[elan] %s fw id:0x%4.4x\n",
412 			__func__,fw_info->fw_id);
413 	dev_info(&client->dev,
414 			"[elan] %s bootcode version:0x%4.4x: low byte 0x%2.2x\n",
415 			__func__,fw_info->fw_bcv,fw_info->fw_bcl);
416 	dev_info(&client->dev,
417 			"[elan] %s fw_info->rx, fw_info->tx: %d:%d\n",
418 			__func__,fw_info->rx, fw_info->tx);
419 	dev_info(&client->dev,
420 			"[elan] %s finger x/y resolution:0x%4.4x/0x%4.4x\n",
421 			__func__,fw_info->finger_xres,fw_info->finger_yres);
422 	dev_info(&client->dev,
423 			"[elan] %s pen x/y resolution:0x%4.4x/0x%4.4x\n",
424 			__func__,fw_info->pen_xres,fw_info->pen_yres);
425 	dev_info(&client->dev,
426 			"[elan] %s  testsolversion:testversion :0x%4.4x/0x%4.4x\n",
427 			__func__,fw_info->testsolversion,fw_info->testversion);
428 	dev_info(&client->dev,
429 			"[elan] %s solutionversion:whck_ver : 0x%4.4x/0x%4.4x\n",
430 			__func__,fw_info->solutionversion,fw_info->whck_ver);
431 
432 	return err;
433 }
434 
normal_fw_packet_handler(struct i2c_client * client)435 static int normal_fw_packet_handler(struct i2c_client *client)
436 {
437 	int err = 0;
438 	struct elan_ts_data *ts = i2c_get_clientdata(client);
439 	int major, minor;
440 	struct elan_fw_info *fw_info = &ts->fw_info;
441 
442 	const uint8_t cmd_ver[]	=	{0x53, 0x00, 0x00, 0x01};
443 	const uint8_t cmd_id[]	=	{0x53, 0xf0, 0x00, 0x01};
444 	const uint8_t cmd_bc[]	=	{0x53, 0x10, 0x00, 0x01};
445 
446 #ifndef TWO_LAYER
447 	const uint8_t cmd_x[]   =	{0x53, 0x60, 0x00, 0x00};
448 	const uint8_t cmd_y[]   =	{0x53, 0x63, 0x00, 0x00};
449 	uint8_t rbuf[4] = {0x00};
450 #else
451 	const uint8_t cmd_info[] = {0x5B, 0x00, 0x00, 0x00, 0x00, 0x00};
452 	uint8_t rbuf[17] = {0x00};
453 #endif
454 
455 	/*fw version*/
456 	err = elan_ts_get_data(client, cmd_ver, sizeof(cmd_ver),rbuf,sizeof(rbuf));
457 	if ( err ) {
458 		dev_err(&client->dev, "[elan] %s get fw version failed\n",__func__);
459 		return err;
460 	}
461 	major = ((rbuf[1] & 0x0f) << 4) | ((rbuf[2] & 0xf0) >> 4);
462 	minor = ((rbuf[2] & 0x0f) << 4) | ((rbuf[3] & 0xf0) >> 4);
463 	fw_info->fw_ver = major << 8 | minor;
464 
465 	/*fw id*/
466 	err = elan_ts_get_data(client, cmd_id, sizeof(cmd_id),rbuf,sizeof(rbuf));
467 	if ( err ) {
468 		dev_err(&client->dev, "[elan] %s get fw id failed\n",__func__);
469 		return err;
470 	}
471 	major = ((rbuf[1] & 0x0f) << 4) | ((rbuf[2] & 0xf0) >> 4);
472 	minor = ((rbuf[2] & 0x0f) << 4) | ((rbuf[3] & 0xf0) >> 4);
473 	fw_info->fw_id = major << 8 | minor;
474 
475 	/*get boocode version*/
476 	err = elan_ts_get_data(client, cmd_bc, sizeof(cmd_bc),rbuf,sizeof(rbuf));
477 	if ( err ) {
478 		dev_err(&client->dev, "[elan] %s get bootcode version failed\n",__func__);
479 		return err;
480 	}
481 	major = ((rbuf[1] & 0x0f) << 4) | ((rbuf[2] & 0xf0) >> 4);
482 	minor = ((rbuf[2] & 0x0f) << 4) | ((rbuf[3] & 0xf0) >> 4);
483 	fw_info->fw_bcv = major << 8 | minor;
484 
485 #ifndef TWO_LAYER
486 	err = elan_ts_get_data(client, cmd_x, sizeof(cmd_x),rbuf,sizeof(rbuf));
487 	if ( err ) {
488 		dev_err(&client->dev, "[elan] %s get finger xresolution failed\n",__func__);
489 		return err;
490 	}
491 	minor = ((rbuf[2])) | ((rbuf[3] & 0xf0) << 4);
492 	fw_info->finger_xres = minor;
493 
494 	err = elan_ts_get_data(client, cmd_y, sizeof(cmd_y),rbuf,sizeof(rbuf));
495 	if ( err ) {
496 		dev_err(&client->dev, "[elan] %s get finger yresolution failed\n",__func__);
497 		return err;
498 	}
499 	minor = ((rbuf[2])) | ((rbuf[3] & 0xf0) << 4);
500 	fw_info->finger_yres = minor;
501 #else
502 	err = elan_ts_get_data(client, cmd_info, sizeof(cmd_info),rbuf,sizeof(rbuf));
503 	if ( err ) {
504 		dev_err(&client->dev, "[elan] %s get two layer x/y resolution failed\n",__func__);
505 		return err;
506 	}
507 	fw_info->finger_xres = (rbuf[2]+rbuf[6] - 1) * 64;
508 	fw_info->finger_yres = (rbuf[3]+rbuf[7] - 1) * 64;
509 #endif
510 
511 	dev_info(&client->dev,
512 			"[elan] %s fw version:0x%4.4x\n",
513 			__func__,fw_info->fw_ver);
514 	dev_info(&client->dev,
515 			"[elan] %s fw id:0x%4.4x\n",
516 			__func__,fw_info->fw_id);
517 	dev_info(&client->dev,
518 			"[elan] %s bootcode version:0x%4.4x\n",
519 			__func__,fw_info->fw_bcv);
520 	dev_info(&client->dev,
521 			"[elan] %s finger x/y resolution:0x%4.4x/0x%4.4x\n",
522 			__func__,fw_info->finger_xres,fw_info->finger_yres);
523 
524 	return err;
525 }
526 
elan__fw_packet_handler(struct i2c_client * client)527 int elan__fw_packet_handler(struct i2c_client *client)
528 {
529 	struct elan_ts_data *ts = i2c_get_clientdata(client);
530 	int ret = 0;
531 
532 	dev_err(&client->dev, "[elan] fw packet handler chip_type %d\n",ts->chip_type);
533 	if (ts->chip_type == HID_TYPE_PROTOCOL) {
534 		ret = hid_fw_packet_handler(client);
535 		if ( ret ) {
536 			dev_err(&client->dev,
537 					"[elan] %s HID get fw msg failed\n",__func__);
538 		}
539 	} else if (ts->chip_type == NORMAL_TYPE_PROTOCOL) {
540 		ret = normal_fw_packet_handler(client);
541 		if ( ret ) {
542 			dev_err(&client->dev,
543 					"[elan] %s Normal get fw msg failed\n",__func__);
544 		}
545 	}
546 	return ret;
547 }
548 
elan_ts_recv_data(struct elan_ts_data * ts,uint8_t * buf)549 static int elan_ts_recv_data(struct elan_ts_data *ts, uint8_t *buf)
550 {
551 	int rc = 0;
552 	uint8_t rbuf[HID_RECV_LEN]={0};
553 	struct elan_report_struct *report = &ts->report;
554 	int i = 0;
555 
556 	rc = ts->ops->recv(rbuf, sizeof(rbuf));
557 	if ( rc < 0) {
558 		dev_err(&ts->client->dev, "[elan] recv report data error [%d] !!\n",rc);
559 		return -1;
560 	}
561 
562 	if (ts->chip_type == HID_TYPE_PROTOCOL) {
563 		if (rbuf[2] == HID_FID) {
564 			report->finger.fvalid_num		= rbuf[62];
565 			report->finger.fbutton_value	= rbuf[63];
566 			memcpy(buf,rbuf,67);
567 			if (rbuf[62] > 5) {
568 				rc = ts->ops->recv(rbuf, sizeof(rbuf));
569 				if (rc != sizeof(rbuf)) {
570 					dev_err(&ts->client->dev, "[elan] recv second report data error!!\n");
571 					return -1;
572 				}
573 				report->finger.fbuf_valid_size = 67*2;
574 				memcpy(buf+58,rbuf+3,67-3);
575 
576 			} else {
577 				report->finger.fbuf_valid_size	= 67;
578 			}
579 			report->finger.fid				=  HID_FID;
580 		    report->finger.fsupport_num		= 10;
581 			report->finger.freport_idx		= 3;
582 			report->finger.fshift_byte		= 11;
583 			report->tool_type				= ELAN_FINGER;
584 		} else if (rbuf[2] == HID_PID) {
585 			report->stylus.pid					= HID_PID;
586 			report->stylus.pbuf_valid_size		= rbuf[0];
587 			report->stylus.pbutton_value		= 0;//rbuf[13];
588 			report->stylus.preport_idx			= 3;
589 			report->stylus.tip_status			= (rbuf[3] & 0x33) >> 1;
590 			report->stylus.inrange_status		= rbuf[3] & 0x02;
591 			report->stylus.key					= rbuf[3] >> 1;
592 //			report->stylus.eraser				= rbuf[3] >> 1;
593 //			report->stylus.inver				= rbuf[3] >> 1;
594 //			report->stylus.barrel				= rbuf[3] >> 1;
595 			report->stylus.barrel_tip			= rbuf[3];
596 			report->tool_type					= ELAN_PEN;
597 			memcpy(buf,rbuf,report->stylus.pbuf_valid_size);
598 		}
599 
600 	} else if (ts->chip_type == NORMAL_TYPE_PROTOCOL) {
601 		if (rbuf[0] == NOR2_FID) {
602 			report->finger.fid				= NOR2_FID;
603 			report->finger.fbuf_valid_size	= NOR2_SIZE;
604 			report->finger.fsupport_num		= 2;
605 			report->finger.fvalid_num		= rbuf[7] & 0x03;
606 			report->finger.freport_idx		= 1;
607 			report->finger.fbits			= rbuf[7] & 0x03;
608 		} else if(rbuf[0] == NOR5_FID) {
609 			report->finger.fid				= NOR5_FID;
610 			report->finger.fbuf_valid_size	= NOR5_SIZE;
611 			report->finger.fsupport_num		= 5;
612 			report->finger.fvalid_num		= rbuf[1] & 0x07;
613 			report->finger.freport_idx		= 2;
614 			report->finger.fbits			= rbuf[1] >> 3;
615 		} else if (rbuf[0] == NOR10_FID) {
616 			report->finger.fid				= NOR10_FID;
617 			report->finger.fbuf_valid_size	= NOR10_SIZE;
618 			report->finger.fsupport_num		= 10;
619 			report->finger.fvalid_num		= rbuf[2] & 0x0f;
620 			report->finger.freport_idx		= 3;
621 			report->finger.fbits			= ((rbuf[2] & 0x30)<<4) | (rbuf[1]);
622 		}
623 		report->finger.fbutton_value		= rbuf[report->finger.fbuf_valid_size - 1];
624 		report->finger.fshift_byte			= 3;
625 		report->tool_type					= ELAN_FINGER;
626 		memcpy(buf,rbuf, report->finger.fbuf_valid_size);
627 	}
628 
629 	if ( report->tool_type == ELAN_PEN) {
630 		for(i = 0; i < report->stylus.pbuf_valid_size/8 + 1; i++) {
631 			print_log(ts->level,"%02x %02x %02x %02x %02x %02x %02x %02x\n",\
632 					buf[i*8+0],buf[i*8+1],buf[i*8+2],buf[i*8+3],\
633 					buf[i*8+4],buf[i*8+5],buf[i*8+6],buf[i*8+7]);
634 		}
635 	} else {
636 		for(i = 0; i < report->finger.fbuf_valid_size/8 + 1; i++) {
637 			print_log(ts->level,"%02x %02x %02x %02x %02x %02x %02x %02x\n",\
638 					buf[i*8+0],buf[i*8+1],buf[i*8+2],buf[i*8+3],\
639 					buf[i*8+4],buf[i*8+5],buf[i*8+6],buf[i*8+7]);
640 		}
641 	}
642 
643 	return rc;
644 }
645 
elan_ts_fparse_xy(uint8_t * data,uint16_t * x,uint16_t * y,const int type)646 static inline int elan_ts_fparse_xy(uint8_t *data, uint16_t *x, uint16_t *y, const int type)
647 {
648 	*x = *y  = 0;
649 
650 	if (type == HID_FID) {
651 		*x = (data[6]);
652 		*x <<= 8;
653 		*x |= data[5];
654 
655 		*y = (data[10]);
656 		*y <<= 8;
657 		*y |= data[9];
658 	} else {
659 		*x = (data[0] & 0xf0);
660 		*x <<= 4;
661 		*x |= data[1];
662 		*y = (data[0] & 0x0f);
663 		*y <<= 8;
664 		*y |= data[2];
665 	}
666 
667 	return 0;
668 
669 }
670 
elan_ts_pparse_xy(uint8_t * data,uint16_t * x,uint16_t * y,uint16_t * p)671 static inline int elan_ts_pparse_xy(uint8_t *data, uint16_t *x, uint16_t *y, uint16_t *p)
672 {
673 	*x = *y = *p = 0;
674 
675 	*x = data[5];
676 	*x <<= 8;
677 	*x |= data[4];
678 
679 	*y = data[7];
680 	*y <<= 8;
681 	*y |= data[6];
682 
683 	*p = data[9];
684 	*p <<= 8;
685 	*p |= data[8];
686 
687 
688 
689 	return 0;
690 }
691 
elants_a_report(struct elan_ts_data * ts,uint8_t * buf)692 static void  elants_a_report(struct elan_ts_data *ts, uint8_t *buf)
693 {
694 	struct elan_report_struct *report = &ts->report;
695 	struct elan_finger_struct finger = report->finger;
696 	struct elan_stylus_struct stylus = report->stylus;
697 	int fbits = finger.fbits;
698 	int reportid = 0;
699 	int valid_num =  finger.fvalid_num;
700 	uint16_t x = 0, y = 0, p = 0;
701 	int fbit = 0;
702 	static int pkey = 0;
703 
704 	if (report->tool_type == ELAN_FINGER) {
705 		for (reportid = 0; reportid < finger.fvalid_num; reportid++ ) {
706 			if (finger.fid ==  HID_FID) {  /*hid over i2c protocol*/
707 
708 				fbits = (buf[finger.freport_idx] & 0x03);
709 				if (fbits) {
710 					fbit  = (((buf[finger.freport_idx] & 0xfc) >> 2) - 1);
711 					elan_ts_fparse_xy(&buf[finger.freport_idx], &y, &x, finger.fid);
712 
713 					x = 800 * x / 2112;
714 					y = 1200 * y / 3392;
715 
716 					input_report_key(ts->finger_idev, BTN_TOUCH, 1);
717 					input_report_key(ts->finger_idev, BTN_TOOL_FINGER, true);
718 					input_report_abs(ts->finger_idev, ABS_MT_POSITION_X, x);
719 					input_report_abs(ts->finger_idev, ABS_MT_POSITION_Y, y);
720 					input_report_abs(ts->finger_idev, ABS_MT_TRACKING_ID, fbit);
721 					input_mt_sync(ts->finger_idev);
722 				} else
723 					valid_num --;
724 				finger.freport_idx  += 11;
725 
726 			} else { /*normal i2c protocol*/
727 
728 				if (fbits & 0x01) {
729 
730 					elan_ts_fparse_xy(&buf[finger.freport_idx], &y, &x, finger.fid);
731 					input_report_key(ts->finger_idev, BTN_TOUCH, 1);
732 					input_report_key(ts->finger_idev, BTN_TOOL_FINGER, true);
733 					input_report_abs(ts->finger_idev, ABS_MT_POSITION_X, x);
734 					input_report_abs(ts->finger_idev, ABS_MT_POSITION_Y, y);
735 					input_report_abs(ts->finger_idev, ABS_MT_TRACKING_ID, reportid);
736 					input_mt_sync(ts->finger_idev);
737 
738 				}else
739 					valid_num--;
740 				fbits = fbits >> 1;
741 				finger.freport_idx  += 3;
742 			}
743 
744 		}
745 
746 		if (!valid_num) {
747 			input_report_key(ts->finger_idev, BTN_TOUCH, 0);
748 			input_report_key(ts->finger_idev, BTN_TOOL_FINGER, false);
749 			input_mt_sync(ts->finger_idev);
750 		}
751 
752 		input_sync(ts->finger_idev);
753 	} else if (report->tool_type == ELAN_PEN) {
754 		print_log(ts->level,"[elan] stylus.key %d, pkey %d\n",stylus.key,pkey);
755 		if (stylus.key > 0 || !pkey) {
756 			switch(stylus.key) {
757 				case 2:
758 					pkey =  BTN_STYLUS;
759 					input_report_key(ts->pen_idev, pkey , 1);
760 					input_sync(ts->pen_idev);
761 					break;
762 				case 3:
763 					pkey =  BTN_STYLUS2;
764 					input_report_key(ts->pen_idev, pkey , 1);
765 					input_sync(ts->pen_idev);
766 					break;
767 				case 4:
768 					pkey = BTN_STYLUS;
769 					input_report_key(ts->pen_idev, pkey , 1);
770 					input_sync(ts->pen_idev);
771 					break;
772 				case 8:
773 					pkey = BTN_STYLUS2;
774 					input_report_key(ts->pen_idev, pkey , 1);
775 					input_sync(ts->pen_idev);
776 					break;
777 				default:
778 					input_report_key(ts->pen_idev, pkey , 0);
779 					pkey = 0;
780 					input_sync(ts->pen_idev);
781 					break;
782 			}
783 		}
784 
785 		print_log(ts->level, "[elan] stylus.tip_status  %d, stylus.inrange_status %d\n",\
786 				stylus.tip_status,stylus.inrange_status);
787 		if (stylus.inrange_status) {
788 			elan_ts_pparse_xy(&buf[0],&y,&x,&p);
789 
790 			x = 800  * x / 8580;
791 			y = 1200 * y / 13780;
792 
793 			input_report_abs(ts->pen_idev, ABS_PRESSURE, p);
794 			input_report_abs(ts->pen_idev, ABS_X, x);
795 			input_report_abs(ts->pen_idev, ABS_Y, y);
796 			dev_info(&ts->client->dev, "[elan] X:Y:P ====%d:%d:%d\n",x,y,p);
797 		}
798 
799 		input_report_key(ts->pen_idev, BTN_TOUCH, stylus.tip_status);
800 		input_report_key(ts->pen_idev, BTN_TOOL_PEN, stylus.tip_status);
801 		input_sync(ts->pen_idev);
802 	}
803 }
804 
elants_slot_report(struct elan_ts_data * ts,uint8_t * buf)805 static void elants_slot_report(struct elan_ts_data *ts, uint8_t *buf)
806 {
807 
808 	struct elan_report_struct *report = &ts->report;
809 	struct elan_finger_struct finger = report->finger;
810 	struct elan_stylus_struct stylus = report->stylus;
811 	uint16_t x = 0, y = 0, p = 0;
812 	int16_t x_tilt_raw = 0, y_tilt_raw = 0;
813 	int8_t x_tilt = 0, y_tilt = 0;
814 	int fbits = finger.fbits;
815 	int fprebits = 0;
816 	int fbits_tmp = 0;
817 	int active = 0;
818 	int id = 0, reportid = 0;
819 	int num = finger.fvalid_num;
820 	static int pkey = 0;
821 
822 	if (finger.fid > HID_PID)
823 		fbits_tmp	= fbits;
824 
825 	if (report->tool_type == ELAN_FINGER) {
826 
827 
828 		if (finger.fid ==  HID_FID) {  /*hid over i2c protocol*/
829 			for (reportid = 0; reportid < finger.fvalid_num; reportid++ ) {
830 				active = (buf[finger.freport_idx] & 0x03);
831 				/*id = (((buf[finger.freport_idx] & 0xfc) >> 2) -1);*/
832 				id = (((buf[finger.freport_idx] & 0xfc) >> 2));
833 				elan_ts_fparse_xy(&buf[finger.freport_idx], &x, &y, finger.fid); //lcm x :y = 720 : 1280 tp x: y = 1296:720
834 				x = ts->hw_info.screen_x * x / ts->fw_info.finger_xres;
835 				y = ts->hw_info.screen_y * y / ts->fw_info.finger_yres;
836 				//x = ts->hw_info.screen_x - x;
837 				//y = ts->hw_info.screen_y - y;
838 
839 				if (active) { /*finger contact*/
840 					input_mt_slot(ts->finger_idev, id);
841 					//input_report_abs(ts->finger_idev, ABS_MT_PRESSURE, 100);
842 					input_report_abs(ts->finger_idev, ABS_MT_TOUCH_MAJOR, 100);
843 					input_report_abs(ts->finger_idev, ABS_MT_POSITION_X, x);
844 					input_report_abs(ts->finger_idev, ABS_MT_POSITION_Y, y);
845 					input_mt_report_slot_state(ts->finger_idev, MT_TOOL_FINGER, true);
846 					input_report_key(ts->finger_idev, BTN_TOUCH, 1);
847 					//dev_info(&ts->client->dev, "[elan] finger X:Y ====%d:%d\n",x,y);
848 				} else {     /*finger leave*/
849 					input_mt_slot(ts->finger_idev, id);
850 					input_mt_report_slot_state(ts->finger_idev, MT_TOOL_FINGER, false);
851 					//input_report_key(ts->finger_idev, BTN_TOUCH, 0);
852 					num--;
853 				}
854 
855 				finger.freport_idx  += 11;
856 			}
857 			//input_mt_sync(ts->finger_idev);
858 			if(num == 0)
859 				input_report_key(ts->finger_idev, BTN_TOUCH, 0);
860 
861 			 input_sync(ts->finger_idev);
862 
863 		} else { /*notmal i2c protocol*/
864 
865 
866 			if (fbits || fprebits) {
867 				for (reportid = 0; reportid < finger.fvalid_num; reportid++ ) {
868 					if(fbits&0x01){
869 						elan_ts_fparse_xy(&buf[finger.freport_idx], &y, &x, finger.fid);
870 						input_mt_slot(ts->finger_idev, reportid);
871 						input_mt_report_slot_state(ts->finger_idev, MT_TOOL_FINGER, true);
872 						input_report_abs(ts->finger_idev, ABS_MT_POSITION_X, x);
873 						input_report_abs(ts->finger_idev, ABS_MT_POSITION_Y, y);
874 					}  else if(fprebits&0x01){
875 						input_mt_slot(ts->finger_idev, id);
876 						input_mt_report_slot_state(ts->finger_idev, MT_TOOL_FINGER, false);
877 					}
878 					finger.freport_idx  += 3;
879 				}
880 			}
881 			fprebits = fbits_tmp;
882 			input_sync(ts->finger_idev);
883 		}
884 	} else if (report->tool_type == ELAN_PEN) {
885 		if (stylus.key > 0 || !pkey) {
886 			switch(stylus.key) {
887 				case 2:
888 					pkey = BTN_TOOL_RUBBER;
889 					input_report_key(ts->pen_idev, pkey , 1);
890 					input_sync(ts->pen_idev);
891 					break;
892 				case 3:
893 					pkey = BTN_TOOL_RUBBER;
894 					input_report_key(ts->pen_idev, pkey , 1);
895 					input_sync(ts->pen_idev);
896 					break;
897 				case 4:
898 					pkey = BTN_STYLUS;
899 					input_report_key(ts->pen_idev, pkey , 1);
900 					input_sync(ts->pen_idev);
901 					break;
902 				case 8:
903 					pkey = BTN_STYLUS2;
904 					input_report_key(ts->pen_idev, pkey , 1);
905 					input_sync(ts->pen_idev);
906 					break;
907 				default:
908 					input_report_key(ts->pen_idev, pkey , 0);
909 					pkey = 0;
910 					input_sync(ts->pen_idev);
911 					break;
912 			}
913 		}
914 
915 		print_log(ts->level, "[elan] stylus.inrange_status = %d, stylus.barrel_tip = %d", \
916 				stylus.inrange_status,stylus.barrel_tip);
917 		if(stylus.inrange_status) {
918 			 elan_ts_pparse_xy(&buf[0],&x,&y,&p);
919 			 x = ts->hw_info.screen_x * x / ts->fw_info.pen_xres;
920 			 y = ts->hw_info.screen_y * y / ts->fw_info.pen_yres;
921 
922 			 //x = ts->hw_info.screen_x - x;
923 			 //y = ts->hw_info.screen_y - y;
924 
925 			x_tilt_raw = (int16_t)((buf[12] << 8) | buf[11]);
926 			y_tilt_raw = (int16_t)((buf[14] << 8) | buf[13]);
927              		x_tilt = (int8_t)(x_tilt_raw / 100);
928              		y_tilt = (int8_t)(y_tilt_raw / 100);
929 
930 
931 			 input_mt_slot(ts->pen_idev, 0);
932 			 input_mt_report_slot_state(ts->pen_idev, MT_TOOL_PEN, true);
933 			 input_report_key(ts->pen_idev, BTN_TOUCH, 1);
934 			 input_report_abs(ts->pen_idev, ABS_MT_TOOL_TYPE, MT_TOOL_PEN);
935 			 input_report_abs(ts->pen_idev, ABS_MT_PRESSURE, p);
936 			 input_report_abs(ts->pen_idev, ABS_MT_POSITION_X, x);
937 			 input_report_abs(ts->pen_idev, ABS_MT_POSITION_Y, y);
938 			 input_report_abs(ts->pen_idev, ABS_TILT_X, x_tilt);
939 			 input_report_abs(ts->pen_idev, ABS_TILT_Y, y_tilt);
940 			 print_log(ts->level, "[elan] pen X:Y:P:TX:TY ====%d:%d:%d:%d:%d\n",
941 				   x, y, p, x_tilt, y_tilt);
942 		} else {
943 			input_mt_slot(ts->pen_idev, 0);
944 			input_mt_report_slot_state(ts->pen_idev, MT_TOOL_PEN, false);
945 			input_report_key(ts->pen_idev, BTN_TOUCH, 0);
946 			dev_info(&ts->client->dev, "[elan] pen relese!!!!");
947 		}
948 
949 		input_sync(ts->pen_idev);
950 	}
951 
952 }
953 
report_mbutton(struct input_dev * idev,int button_value)954 static int report_mbutton(struct input_dev *idev, int button_value)
955 {
956 	static int key;
957 
958 	switch(button_value) {
959 		case 0x01:
960 			key = KEY_BACK;
961 			input_report_key(idev, key, 1);
962 			break;
963 
964 		case 0x02:
965 			key = KEY_HOMEPAGE;
966 			input_report_key(idev, key, 1);
967 
968 			break;
969 		case 0x03:
970 		case 0x04:
971 			key = KEY_BACK;
972 			input_report_key(idev, key, 1);
973 			break;
974 		default:
975 			if (key != 0) {
976 				input_report_key(idev, key, 0);
977 				key = 0;
978 			}
979 			break;
980 	}
981 	input_sync(idev);
982 	return key;
983 }
984 
985 
986 
elan_ts_hid_report(struct elan_ts_data * ts,uint8_t * buf)987 static void elan_ts_hid_report(struct elan_ts_data *ts, uint8_t *buf)
988 {
989 	struct elan_report_struct *report = &ts->report;
990 	int button = report->finger.fbutton_value;
991 	int pbutton = report->stylus.pbutton_value;
992 	static int prekey = 0;
993 
994 	/*for hid protocol finger contat mutual button or pen contact mutual button*/
995 	/*button priority is higher than other*/
996 	if ((button != 0 && button != 0xFF) || (pbutton != 0 && pbutton != 0xFF)|| (prekey != 0)) {
997 		if (ts->report.tool_type == ELAN_FINGER) {
998 			prekey = report_mbutton(ts->finger_idev,button);
999 			return;
1000 		} else {
1001 			prekey = report_mbutton(ts->pen_idev,pbutton);
1002 			return;
1003 		}
1004 	}
1005 
1006 	if (ts->report_type == PROTOCOL_TYPE_B) {
1007 		elants_slot_report(ts,buf);
1008 	} else {
1009 		elants_a_report(ts,buf);
1010 	}
1011 }
1012 
elan_ts_normal_report(struct elan_ts_data * ts,uint8_t * buf)1013 static void elan_ts_normal_report(struct elan_ts_data *ts, uint8_t *buf)
1014 {
1015 	struct elan_report_struct *report = &ts->report;
1016 	int button = report->finger.fbutton_value;
1017 	static int prekey = 0;
1018 
1019 	if ((button != 0 && button != 0xFF) || (prekey != 0)) {
1020 		prekey = report_mbutton(ts->finger_idev,button);
1021 		return;
1022 	}
1023 
1024 	if (ts->report_type == PROTOCOL_TYPE_B) {
1025 		elants_slot_report(ts,buf);
1026 		return;
1027 	} else {
1028 		elants_a_report(ts,buf);
1029 		return;
1030 	}
1031 
1032 }
1033 
elan_ts_report_data(struct elan_ts_data * ts,uint8_t * buf)1034 static void elan_ts_report_data(struct elan_ts_data *ts, uint8_t *buf)
1035 {
1036 	switch (ts->chip_type) {
1037 		case HID_TYPE_PROTOCOL:
1038 
1039 			elan_ts_hid_report(ts, buf);
1040 			break;
1041 		case NORMAL_TYPE_PROTOCOL:
1042 
1043 			elan_ts_normal_report(ts,buf);
1044 			break;
1045 		default:
1046 			dev_err(&ts->client->dev,
1047 					"[elan] unknow type 0x%2x:0x%2x:0x%2x:0x%2x",\
1048 					buf[0],buf[1],buf[2],buf[3]);
1049 			break;
1050 	}
1051 }
1052 
elan_ts_work_func(struct work_struct * work)1053 static void  elan_ts_work_func(struct work_struct *work)
1054 {
1055 	struct elan_ts_data *ts =
1056 		container_of(work, struct elan_ts_data, ts_work);
1057 	uint8_t buf[HID_REPORT_MAX_LEN] = {0x00};
1058 	int rc = 0;
1059 
1060 	if(gpio_get_value(ts->hw_info.intr_gpio)) {
1061 		dev_err(&ts->client->dev,"[elan]interrupt jitter\n.");
1062 		return;
1063 	}
1064 
1065 	memset(&ts->report, 0, sizeof(ts->report));
1066 
1067 	rc = elan_ts_recv_data(ts,buf);
1068 	if (rc < 0) {
1069 		dev_err(&ts->client->dev,"[elan]recv data error\n.");
1070 		return;
1071 	}
1072 
1073 	elan_ts_report_data(ts,buf);
1074 	return;
1075 }
1076 
1077 
elan_ts_irq_handler(int irq,void * dev_id)1078 static irqreturn_t elan_ts_irq_handler(int irq, void *dev_id)
1079 {
1080 	struct elan_ts_data *ts = (struct elan_ts_data*)dev_id;
1081 
1082 	if (ts->user_handle_irq) {
1083 		wake_up_interruptible(&ts->elan_userqueue);
1084 		ts->int_val = 0;
1085 		return IRQ_HANDLED;
1086 	} else {
1087 		queue_work(ts->elan_wq,&ts->ts_work);
1088 		return IRQ_HANDLED;
1089 	}
1090 }
1091 
elan_request_pen_input_dev(struct elan_ts_data * ts)1092 static int elan_request_pen_input_dev(struct elan_ts_data *ts)
1093 {
1094 	int err = 0;
1095 
1096 	ts->pen_idev = input_allocate_device();
1097 	if (ts->pen_idev == NULL) {
1098 		err = -ENOMEM;
1099 		dev_err(&ts->client->dev,
1100 				"[elan error] Failed to allocate pen device\n");
1101 		return err;
1102 	}
1103 
1104 	if (ts->report_type == PROTOCOL_TYPE_B) {
1105 		//input_mt_init_slots(ts->pen_idev, 10);
1106 		input_mt_init_slots(ts->pen_idev, FINGERS_NUM,INPUT_MT_DIRECT);
1107 		input_set_abs_params(ts->pen_idev, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
1108 		input_set_abs_params(ts->pen_idev, ABS_MT_POSITION_X, 0,  ts->hw_info.screen_x, 0, 0);
1109 		input_set_abs_params(ts->pen_idev, ABS_MT_POSITION_Y, 0,  ts->hw_info.screen_y, 0, 0);
1110 		input_set_abs_params(ts->pen_idev, ABS_MT_PRESSURE, 0, 4096, 0, 0);
1111 		input_set_abs_params(ts->pen_idev, ABS_TILT_X, -90, 90, 0, 0);
1112 		input_set_abs_params(ts->pen_idev, ABS_TILT_Y, -90, 90, 0, 0);
1113 	} else {
1114 		__set_bit(BTN_TOOL_PEN, ts->pen_idev->keybit);
1115 		__set_bit(BTN_TOUCH, ts->pen_idev->keybit);
1116 		ts->pen_idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
1117 		input_set_abs_params(ts->pen_idev, ABS_X, 0,  ts->hw_info.screen_x, 0, 0);
1118 		input_set_abs_params(ts->pen_idev, ABS_Y, 0,  ts->hw_info.screen_y, 0, 0);
1119 		input_set_abs_params(ts->pen_idev, ABS_PRESSURE, 0, 4096, 0, 0);
1120 
1121 		input_set_abs_params(ts->pen_idev, ABS_TILT_X, -90, 90, 0, 0);
1122 		input_set_abs_params(ts->pen_idev, ABS_TILT_Y, -90, 90, 0, 0);
1123 	}
1124 
1125 	ts->pen_idev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) | BIT_MASK(EV_SYN);
1126 
1127 	__set_bit(KEY_BACK, ts->pen_idev->keybit);
1128 	__set_bit(BTN_TOOL_RUBBER, ts->pen_idev->keybit);
1129 	__set_bit(BTN_STYLUS, ts->pen_idev->keybit);
1130 	__set_bit(BTN_STYLUS2, ts->pen_idev->keybit);
1131 	__set_bit(INPUT_PROP_DIRECT, ts->pen_idev->propbit);
1132 	__set_bit(BTN_TOUCH, ts->pen_idev->keybit);
1133 	input_set_abs_params(ts->pen_idev, ABS_MT_TRACKING_ID, 0, 10, 0, 0);
1134 
1135 	ts->pen_idev->name = "elan_pen";
1136 	ts->pen_idev->phys = "input/ts";
1137 	ts->pen_idev->id.bustype = BUS_I2C;
1138 
1139 	err = input_register_device(ts->pen_idev);
1140 	if (err) {
1141 		input_free_device(ts->pen_idev);
1142 		dev_err(&ts->client->dev,
1143 				"unable to register pen input device: %d\n", err);
1144 		return err;
1145 	}
1146 
1147 	return err;
1148 }
1149 
elan_request_finger_input_dev(struct elan_ts_data * ts)1150 static int elan_request_finger_input_dev(struct elan_ts_data *ts)
1151 {
1152 	int err = 0;
1153 	int i = 0;
1154 
1155 	ts->finger_idev = input_allocate_device();
1156 	if (ts->finger_idev == NULL) {
1157 		err = -ENOMEM;
1158 		dev_err(&ts->client->dev,
1159 				"[elan] Failed to allocate input device\n");
1160 		return err;
1161 	}
1162 
1163 	ts->finger_idev->evbit[0] = BIT(EV_KEY)|BIT_MASK(EV_REP);
1164 
1165 	/*key setting*/
1166 	for (i = 0; i < ARRAY_SIZE(key_value); i++)
1167 		__set_bit(key_value[i], ts->finger_idev->keybit);
1168 
1169 
1170 	ts->finger_idev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1171 
1172 	__set_bit(INPUT_PROP_DIRECT, ts->finger_idev->propbit);
1173 
1174 	if (ts->report_type == PROTOCOL_TYPE_B) {
1175 		//input_mt_init_slots(ts->finger_idev, 10);
1176 		input_mt_init_slots(ts->finger_idev, FINGERS_NUM,INPUT_MT_DIRECT);
1177 		input_set_abs_params(ts->finger_idev, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
1178 	} else {
1179 		__set_bit(BTN_TOOL_FINGER, ts->finger_idev->keybit);
1180 	}
1181 
1182 	dev_info(&ts->client->dev,
1183 			"[elan] %s: x resolution: %d, y resolution: %d\n",
1184 			__func__, ts->fw_info.finger_xres, ts->fw_info.finger_yres);
1185 
1186 	ts->finger_idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1187 	input_set_abs_params(ts->finger_idev, ABS_MT_POSITION_X, 0, ts->hw_info.screen_x, 0, 0);
1188 	input_set_abs_params(ts->finger_idev, ABS_MT_POSITION_Y, 0, ts->hw_info.screen_y, 0, 0);
1189 //	input_set_abs_params(ts->finger_idev, ABS_MT_PRESSURE, 0, 255, 0, 0);
1190 //	input_set_abs_params(ts->finger_idev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1191 	input_set_abs_params(ts->finger_idev, ABS_MT_TRACKING_ID, 0, 255, 0, 0);
1192 
1193 	ts->finger_idev->name = ELAN_TS_NAME;
1194 	ts->finger_idev->phys = "input/ts";
1195 	ts->finger_idev->id.bustype = BUS_I2C;
1196 	ts->finger_idev->id.vendor = 0x0001;
1197 	ts->finger_idev->id.product = 0x0002;
1198 	ts->finger_idev->id.version = 0x0003;
1199 
1200 	err = input_register_device(ts->finger_idev);
1201 	if (err) {
1202 		input_free_device(ts->finger_idev);
1203 		dev_err(&ts->client->dev,
1204 				"[elan]%s: unable to register %s input device\n",
1205 				__func__, ts->finger_idev->name);
1206 		return err;
1207 	}
1208 
1209 	return 0;
1210 }
1211 
elan_ts_register_interrupt(struct elan_ts_data * ts)1212 static int elan_ts_register_interrupt(struct elan_ts_data *ts)
1213 {
1214 	int err = 0;
1215 
1216 	err = request_threaded_irq(ts->hw_info.irq_num,
1217 			NULL, elan_ts_irq_handler, IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1218 			ELAN_TS_NAME, ts);
1219 
1220 	if (err < 0)
1221 		dev_err(&ts->client->dev,
1222 				"[elan] %s: request_irq %d failed,err = %d\n",
1223 				__func__, ts->client->irq, err);
1224 
1225 	return err;
1226 }
1227 
1228 
elan_ic_init_work(struct work_struct * work)1229 static void elan_ic_init_work(struct work_struct *work)
1230 {
1231 	int rc = 0;
1232 	int retry_cnt = 0;
1233 	struct elan_ts_data *ts = private_ts;
1234 	struct i2c_client *client = ts->client;
1235 
1236 	/*Get FW MSG: ID,VERSION,X_RES,Y_RES,etc*/
1237 	if (ts->recover == COMPARE_UPGRADE) {
1238 		for (retry_cnt = 0; retry_cnt < 3; retry_cnt++) {
1239 			rc = elan__fw_packet_handler(client);
1240 			if (rc < 0)
1241 				dev_err(&client->dev,
1242 						"[elan]%s, fw_packet_handler fail, rc = %d\n",
1243 						__func__, rc);
1244 			else
1245 				break;
1246 		}
1247 		if (retry_cnt >= 3) {
1248 			dev_err(&client->dev,
1249 					"[elan]%s, fw_packet_handler failed,retry out, rc = %d\n",
1250 					__func__,rc);
1251 			return;
1252 		}
1253 	} else {
1254 			dev_err(&client->dev,
1255 					"[elan]%s, fw into recovery mode force update, rc = %d\n", __func__,rc);
1256 	}
1257 
1258 #ifdef IAP_PORTION
1259 
1260 	dev_err(&ts->client->dev, "[elan]Start IAP Flow!!!\n");
1261 	ts->power_lock = 1; //skip resume / suspend flow
1262 	elan_check_update_flage(ts);
1263 
1264 #endif
1265 
1266 	/*finget and pen input event register*/
1267 	rc = elan_request_pen_input_dev(ts);
1268 	if ( rc ) {
1269 		dev_err(&ts->client->dev,
1270 				"[elan]: %s pen input event request failed.\n",
1271 				__func__);
1272 		goto exit_pen_input_dev_failed;
1273 
1274 	}
1275 
1276 	rc = elan_request_finger_input_dev(ts);
1277 	if ( rc ) {
1278 		dev_err(&ts->client->dev,
1279 				"[elan]: %s finger input event request failed %d.\n",
1280 				__func__, rc);
1281 		goto exit_finger_input_dev_failed;
1282 	}
1283 
1284 	mutex_lock(&ts->irq_mutex);
1285 	ts->irq_lock_flag = 0;
1286 	mutex_unlock(&ts->irq_mutex);
1287 
1288 	/*elan irq resgister*/
1289 	rc = elan_ts_register_interrupt(private_ts);
1290 	if ( rc ) {
1291 		dev_err(&private_ts->client->dev,
1292 				"[elan]: %s elan_ts_register_interrupt failed %d\n",
1293 				__func__, rc);
1294 		goto exit_register_interrupt_failed;
1295 	}
1296 
1297 #ifdef IAP_PORTION
1298 	ts->power_lock = 0;
1299 #endif
1300 
1301 	return;
1302 
1303 exit_register_interrupt_failed:
1304 	input_unregister_device(ts->finger_idev);
1305 exit_finger_input_dev_failed:
1306 	input_unregister_device(ts->pen_idev);
1307 exit_pen_input_dev_failed:
1308 	return;
1309 }
1310 
1311 
elan_ts_setup(struct elan_ts_data * ts)1312 static int elan_ts_setup(struct elan_ts_data *ts)
1313 {
1314 	int err = 0;
1315 
1316 	dev_err(&ts->client->dev, "[elan] setup hw reset\n");
1317 	/*HW RESET TP and delay 200ms*/
1318 	elan_ts_hw_reset(&ts->hw_info);
1319 	msleep(500);
1320 
1321 	err = elan__hello_packet_handler(ts->client, ts->chip_type);
1322 	if ( err < 0 ) {
1323 		dev_err(&ts->client->dev,
1324 				"[elan error] %s, hello_packet_handler fail,err= %d\n",
1325 				__func__,err);
1326 		return err;
1327 	} else {
1328 		dev_err(&ts->client->dev,
1329 				"[elan] %s,ic status = %s",
1330 				__func__, err == FORCED_UPGRADE ? "recovery":"normal");
1331 	}
1332 
1333 	return err;
1334 }
1335 
elan_iap_open(struct inode * inode,struct file * filp)1336 static int elan_iap_open(struct inode *inode, struct file *filp)
1337 {
1338 	struct elan_ts_data *ts = container_of(((struct miscdevice*)filp->private_data), struct elan_ts_data, firmware);
1339 
1340 	dev_dbg(&ts->client->dev,"%s enter\n", __func__);
1341 
1342 	filp->private_data = ts;
1343 //	ts->int_val = 1;
1344 //	ts->user_handle_irq = 1;
1345 	return 0;
1346 }
1347 
elan_iap_release(struct inode * inode,struct file * filp)1348 static int elan_iap_release(struct inode *inode, struct file *filp)
1349 {
1350 	dev_info(&private_ts->client->dev,"%s enter", __func__);
1351 
1352 	filp->private_data = NULL;
1353 	//private_ts->user_handle_irq = 0;
1354 	//private_ts->int_val = 0;
1355 	return 0;
1356 }
1357 
elan_iap_write(struct file * filp,const char * buff,size_t count,loff_t * offp)1358 static ssize_t elan_iap_write(struct file *filp, const char *buff, size_t count, loff_t *offp)
1359 {
1360 	int ret;
1361 	char *tmp;
1362 	struct elan_ts_data *ts = (struct elan_ts_data *)filp->private_data;
1363 	struct i2c_client *client= ts->client;
1364 
1365 	dev_info(&client->dev,"%s enter", __func__);
1366 	if (count > 8192){
1367 		count = 8192;
1368 	}
1369 
1370 	tmp = kmalloc(count, GFP_KERNEL);
1371 	if (tmp == NULL){
1372 		return -ENOMEM;
1373 	}
1374 
1375 	if (copy_from_user(tmp, buff, count)) {
1376 		return -EFAULT;
1377 	}
1378 
1379 	ret = elan_i2c_send(tmp, count);
1380 	if (ret != count){
1381 		dev_err(&client->dev, "[elan]elan elan_i2c_send fail, ret=%d \n", ret);
1382 	}
1383 
1384 	kfree(tmp);
1385 
1386 	return ret;
1387 }
1388 
elan_iap_read(struct file * filp,char * buff,size_t count,loff_t * offp)1389 static ssize_t elan_iap_read(struct file *filp, char *buff, size_t count, loff_t *offp)
1390 {
1391 	char *tmp;
1392 	int ret;
1393 	long rc;
1394 	struct elan_ts_data *ts = (struct elan_ts_data *)filp->private_data;
1395 	struct i2c_client *client = ts->client;
1396 
1397 	dev_info(&client->dev, "%s enter", __func__);
1398 
1399 	if (count > 8192){
1400 		count = 8192;
1401 	}
1402 
1403 	tmp = kmalloc(count, GFP_KERNEL);
1404 	if (tmp == NULL){
1405 		return -ENOMEM;
1406 	}
1407 
1408 	if (ts->user_handle_irq == 1) {
1409 		wait_event_interruptible(ts->elan_userqueue, ts->int_val == 0);
1410 	}
1411 
1412 	ret = elan_i2c_recv(tmp, count);
1413 	if (ret != count){
1414 		dev_err(&client->dev, "[elan error]elan elan_i2c_recv fail, ret=%d \n", ret);
1415 	}
1416 
1417 	if (ret == count){
1418 		rc = copy_to_user(buff, tmp, count);
1419 	}
1420 
1421 	if (ts->user_handle_irq == 1) {
1422 		ts->int_val = 1;
1423 	}
1424 
1425 	kfree(tmp);
1426 	return ret;
1427 }
1428 
elan_iap_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1429 static long elan_iap_ioctl( struct file *filp, unsigned int cmd, unsigned long arg)
1430 {
1431 	int __user *ip = (int __user *)arg;
1432 	struct elan_ts_data *ts = (struct elan_ts_data *)filp->private_data;
1433 	struct i2c_client *client =  ts->client;
1434 
1435 	dev_info(&client->dev, "%s enter cmd value %x\n", __func__,cmd);
1436 
1437 	switch (cmd) {
1438 	case IOCTL_I2C_SLAVE:
1439 		dev_info(&client->dev, "pre addr is %X\n",  client->addr);
1440 		client->addr = (int __user)arg;
1441 		dev_info(&client->dev, "new addr is %X\n",  client->addr);
1442 		break;
1443 	case IOCTL_RESET:
1444 		elan_ts_hw_reset(&ts->hw_info);
1445 		break;
1446 	case IOCTL_IAP_MODE_LOCK:
1447 		if(private_ts->power_lock == 0){
1448 			private_ts->power_lock = 1;
1449 			elan_switch_irq(ts,0);
1450 		}
1451 		break;
1452         case IOCTL_IAP_MODE_UNLOCK:
1453 		if(private_ts->power_lock == 1){
1454 			private_ts->power_lock = 0;
1455 			elan_switch_irq(ts,1);
1456 		}
1457 		break;
1458 	case IOCTL_CHECK_RECOVERY_MODE:
1459 		return private_ts->recover;
1460 		break;
1461 	case IOCTL_ROUGH_CALIBRATE:
1462 		return elan_ts_calibrate(ts->client);
1463 	case IOCTL_I2C_INT:
1464 		put_user(gpio_get_value(ts->hw_info.intr_gpio), ip);
1465 		break;
1466 		case IOCTL_USER_HANDLE_IRQ:
1467 			ts->user_handle_irq = 1;
1468 			break;
1469 		case IOCTL_KERN_HANDLE_IRQ:
1470 			ts->user_handle_irq = 0;
1471 	default:
1472 		break;
1473 	}
1474 	return 0;
1475 }
1476 
elan_iap_poll(struct file * filp,struct poll_table_struct * wait)1477 static unsigned int elan_iap_poll(struct file *filp, struct poll_table_struct *wait)
1478 {
1479 	int mask = 0;
1480 	struct elan_ts_data *ts = (struct elan_ts_data *)filp->private_data;
1481 	dev_info(&ts->client->dev, "[elan] polling int_val = %d\n", ts->int_val);
1482 
1483 	poll_wait(filp,&ts->elan_userqueue, wait);
1484 	if (ts->int_val == 0)
1485 		mask |= POLLIN|POLLRDNORM;
1486 	else if(ts->int_val == 1)
1487 		mask |= POLLOUT|POLLWRNORM;
1488 
1489 	return mask;
1490 }
1491 
1492 struct file_operations elan_touch_fops = {
1493 	.open			= elan_iap_open,
1494 	.write			= elan_iap_write,
1495 	.read			= elan_iap_read,
1496 	.release		= elan_iap_release,
1497 	.unlocked_ioctl		= elan_iap_ioctl,
1498 	.compat_ioctl		= elan_iap_ioctl,
1499 	.poll			= elan_iap_poll,
1500 };
1501 
1502 
elan_touch_node_init(struct elan_ts_data * ts)1503 static void elan_touch_node_init(struct elan_ts_data *ts)
1504 {
1505 
1506 	elan_sysfs_attri_file(ts);
1507 
1508 	/*creat dev/elan-iap node for fw operation*/
1509 	ts->firmware.minor = MISC_DYNAMIC_MINOR;
1510 	ts->firmware.name = "elan-iap";
1511 	ts->firmware.fops = &elan_touch_fops;
1512 	ts->firmware.mode = S_IFREG|S_IRWXUGO;
1513 
1514 	if (misc_register(&ts->firmware) < 0)
1515 		dev_err(&ts->client->dev, "misc_register failed!!\n");
1516 
1517 	ts->p = proc_create("elan-iap", 0664, NULL, (const struct proc_ops *)&elan_touch_fops);
1518 	if (ts->p == NULL)
1519 		dev_err(&ts->client->dev, "[elan error] proc_create failed!!\n");
1520 	else
1521 		dev_info(&ts->client->dev, "proc_create ok!!\n");
1522 
1523 	return;
1524 }
1525 
elan_touch_node_deinit(struct elan_ts_data * ts)1526 static void elan_touch_node_deinit(struct elan_ts_data *ts)
1527 {
1528 	elan_sysfs_attri_file_remove(ts);
1529 	misc_deregister(&ts->firmware);
1530 	remove_proc_entry("elan-iap", NULL);
1531 }
1532 /*******************************************************
1533 Function:
1534    	Power on  Funtion.
1535 Input:
1536     ts: elan_ts_data struct.
1537     on: bool, true:on, flase:off
1538 Output:
1539     Executive outcomes.
1540     0: succeed. otherwise: failed
1541 *******************************************************/
1542 #if 1
elan_ts_power_on(struct elan_ts_data * ts,bool on)1543 static int elan_ts_power_on(struct elan_ts_data *ts, bool on)
1544 {
1545 	int ret = 0;
1546 
1547 	if (!on)
1548 		goto power_off;
1549 
1550 	ret = regulator_enable(ts->vdd);
1551 	if (ret) {
1552 		dev_err(&ts->client->dev,
1553 				"Regulator vdd enable failed ret = %d\n",ret);
1554 		return ret;
1555 	}
1556 #if 0
1557 	ret = regulator_enable(ts->vcc_i2c);
1558 	if (ret) {
1559 		dev_err(&ts->client->dev,
1560 				"Regulator vcc_i2c enable failed ret = %d\n",ret);
1561 		regulator_disable(ts->vdd);
1562 	}
1563 #endif
1564 	return ret;
1565 
1566 power_off:
1567 	ret = regulator_disable(ts->vdd);
1568 	if (ret) {
1569 		dev_err(&ts->client->dev,
1570 				"Regulator vdd disable failed ret = %d\n",ret);
1571 		return ret;
1572 	}
1573 #if 0
1574 	ret = regulator_disable(ts->vcc_i2c);
1575 	if (ret) {
1576 		dev_err(&ts->client->dev,
1577 				"Regulator vcc_i2c disable failed ret = %d\n", ret);
1578 		ret = regulator_enable(ts->vdd);
1579 		if (ret)
1580 			dev_err(&ts->client->dev,
1581 					"Regulator vdd enable failed ret = %d\n", ret);
1582 	}
1583 #endif
1584 	return ret;
1585 }
1586 
elan_power_initial(struct elan_ts_data * ts)1587 static int elan_power_initial(struct elan_ts_data *ts)
1588 {
1589 	int ret = 0;
1590 
1591 	ts->vdd = regulator_get(&ts->client->dev, "vdd");
1592 	if (IS_ERR(ts->vdd)) {
1593 		ret = PTR_ERR(ts->vdd);
1594 		dev_err(&ts->client->dev,
1595 			"Regulator get failed vdd rc=%d\n", ret);
1596 		return ret;
1597 	}
1598 
1599 #if 0
1600 	if (regulator_count_voltages(ts->vdd) > 0) {
1601 		ret = regulator_set_voltage(ts->vdd,ELAN_VTG_MIN_UV,
1602 				ELAN_VTG_MAX_UV);
1603 		if (ret) {
1604 			dev_err(&ts->client->dev,
1605 				"Regulator set_vtg failed vdd rc=%d\n", ret);
1606 			goto reg_vdd_put;
1607 		}
1608 	}
1609 
1610 	ts->vcc_i2c = regulator_get(&ts->client->dev, "vcc_i2c");
1611 	if (IS_ERR(ts->vcc_i2c)) {
1612 		ret = PTR_ERR(ts->vcc_i2c);
1613 		dev_err(&ts->client->dev,
1614 			"Regulator get failed vcc_i2c rc=%d\n", ret);
1615 		goto reg_vdd_set_vtg;
1616 	}
1617 
1618 	if (regulator_count_voltages(ts->vcc_i2c) > 0) {
1619 		ret = regulator_set_voltage(ts->vcc_i2c, ELAN_I2C_VTG_MIN_UV,
1620 				ELAN_I2C_VTG_MAX_UV);
1621 		if (ret) {
1622 			dev_err(&ts->client->dev,
1623 				"Regulator set_vtg failed vcc_i2c rc=%d\n", ret);
1624 			goto reg_vcc_i2c_put;
1625 		}
1626 	}
1627 #endif
1628 	return ret;
1629 
1630 #if 0
1631 reg_vcc_i2c_put:
1632 	regulator_put(ts->vcc_i2c);
1633 reg_vdd_set_vtg:
1634 	if (regulator_count_voltages(ts->vdd) > 0)
1635 		regulator_set_voltage(ts->vdd, 0, ELAN_VTG_MAX_UV);
1636 
1637 
1638 reg_vdd_put:
1639 	regulator_put(ts->vdd);
1640 #endif
1641 	return ret;
1642 }
1643 
elan_ts_set_power(struct elan_ts_data * ts,bool on)1644 static int elan_ts_set_power(struct elan_ts_data *ts, bool on)
1645 {
1646 	int ret = 0;
1647 
1648 	if(!on) {
1649 		ret = on;
1650 		goto pwr_deinit;
1651 	}
1652 
1653 	/*initial power*/
1654 	ret = elan_power_initial(ts);
1655 	if(ret)
1656 		goto elan_power_init_failed;
1657 
1658 	/*power on*/
1659 	ret = elan_ts_power_on(ts,on);
1660 	if(ret)
1661 		goto elan_power_on_failed;
1662 
1663 	return ret;
1664 
1665 elan_power_on_failed:
1666 	regulator_put(ts->vdd);
1667 	regulator_put(ts->vcc_i2c);
1668 elan_power_init_failed:
1669 pwr_deinit:
1670 	return ret;
1671 }
1672 #endif
1673 /*******************************************************
1674 Function:
1675    	Initial gpio Funtion
1676 Input:
1677     hw_info: ts_chip_hw_info struct
1678 Output:
1679     Executive outcomes.
1680     0: succeed. otherwise: failed
1681 *******************************************************/
1682 
elan_ts_gpio_initial(struct ts_chip_hw_info * hw_info)1683 static int elan_ts_gpio_initial(struct ts_chip_hw_info *hw_info)
1684 {
1685 	int ret = 0;
1686 
1687 	printk("[elan] request reset gpio\n");
1688 	ret = gpio_request(hw_info->rst_gpio, "tp_reset");
1689 	if (ret < 0) {
1690 		pr_err("%s: request rst_gpio pin failed\n", __func__);
1691 		goto free_rst_gpio;
1692 	}
1693 
1694 	gpio_direction_output(hw_info->rst_gpio, 1);
1695 
1696 	printk("[elan] request interrupt gpio\n");
1697 	/*set int pin input*/
1698 	ret = gpio_request(hw_info->intr_gpio, "tp_irq");
1699 	if (ret < 0) {
1700 		pr_err("%s: request intr_gpio pin failed\n", __func__);
1701 		goto free_irq_gpio;
1702 	}
1703 	gpio_direction_input(hw_info->intr_gpio);
1704 
1705 	hw_info->irq_num = gpio_to_irq(hw_info->intr_gpio);
1706 
1707 
1708 
1709 	return ret;
1710 
1711 free_irq_gpio:
1712 	if (gpio_is_valid(hw_info->intr_gpio))
1713 		gpio_free(hw_info->intr_gpio);
1714 free_rst_gpio:
1715 	if (gpio_is_valid(hw_info->rst_gpio))
1716 		gpio_free(hw_info->rst_gpio);
1717 	return ret;
1718 }
1719 
1720 /*******************************************************
1721 Function:
1722    	Get dts gpio num
1723 Input:
1724     dev: device struct.
1725     hw_info: ts_chip_hw_info struct
1726 Output:
1727     Executive outcomes.
1728     0: succeed. otherwise: failed
1729 *******************************************************/
1730 #ifdef CONFIG_OF
elan_parse_dt(struct device * dev,struct ts_chip_hw_info * chip_hw_info)1731 static int elan_parse_dt(struct device *dev,
1732 			struct ts_chip_hw_info *chip_hw_info)
1733 {
1734 	int ret = 0;
1735 	u32 data = 0;
1736 	//struct device_node *node = NULL;
1737 	struct elan_ts_data *ts =
1738 		container_of(chip_hw_info, struct elan_ts_data, hw_info);
1739 	//u32 lcm_coordinate[2] = {0};
1740 	struct device_node *np = dev->of_node;
1741 /*
1742 	node = of_find_compatible_node(NULL, NULL, "elan,ektf");
1743 	if(node){
1744 		dev_err(&ts->client->dev,"[elan]of_find_compatible_node of : %s\n", "elan,ektf");
1745 		return -ENODEV;
1746 	}
1747 */
1748 	/*get irq gpio from dts*/
1749 	chip_hw_info->intr_gpio = of_get_named_gpio_flags(np,
1750 		"elan,irq-gpio", 0, NULL);
1751 	if (!gpio_is_valid(chip_hw_info->intr_gpio)) {
1752 		dev_err(&ts->client->dev, "[elan] hw_info->intr_gpio invalid\n");
1753 		ret =  -EINVAL;
1754 		goto request_intr_gpio_failed;
1755 	}
1756 
1757 	/*get reset gpio from dts*/
1758 	chip_hw_info->rst_gpio = of_get_named_gpio_flags(np,
1759 		"elan,rst-gpio", 0, NULL);
1760 	if (!gpio_is_valid(chip_hw_info->rst_gpio)) {
1761 		dev_err(&ts->client->dev, "[elan] hw_info->rst_gpio invalid\n");
1762 		ret = -EINVAL;
1763 		goto request_rst_gpio_failed;
1764 	}
1765 
1766 	/*get ic communicate protocol*/
1767 	ret = of_property_read_u32(np, "chip_type", &data);
1768 	if (ret == 0) {
1769 		ts->chip_type = data;
1770 		dev_info(&ts->client->dev,"[elan]:chip protocol_type=%s", ts->chip_type == 1 ? "HID IIC":"NORMAL IIC" );
1771 	} else {
1772 		ret = -EINVAL;
1773 		goto read_chip_type_failed;
1774 	}
1775 
1776 	/*get report protocol */
1777 	ret = of_property_read_u32(np, "report_type", &data);
1778 	if (ret == 0) {
1779 		ts->report_type = data;
1780 		dev_info(&ts->client->dev,"[elan]:report protocol_type=%s", ts->report_type == 1?"B protocol":"A protocol");
1781 	} else {
1782 		ret = -EINVAL;//hw_info->rst_gpio;
1783 		goto read_report_type_failed;
1784 	}
1785 	ts->hw_info.screen_x = 2160; //1728; //2160;
1786 	ts->hw_info.screen_y = 1440; //2368; //1440
1787 #if 0
1788 	/*get lcm coordinate*/
1789 	ret = of_property_read_u32_array(np, "lcm_resolution", lcm_coordinate,sizeof(lcm_coordinate));
1790 	if (ret == 0) {
1791 		ts->hw_info.screen_x = lcm_coordinate[0];
1792 		ts->hw_info.screen_y = lcm_coordinate[1];
1793 		dev_info(&ts->client->dev,"[elan]:LCM RESOLUTION X:Y=%d:%d,", ts->hw_info.screen_x, ts->hw_info.screen_y);
1794 	} else {
1795 		ret = -EINVAL;//hw_info->rst_gpio;
1796 		goto read_lcm_res_failed;
1797 	}
1798 #endif
1799 	return ret;
1800 
1801 //read_lcm_res_failed:
1802 read_report_type_failed:
1803 read_chip_type_failed:
1804 	if (gpio_is_valid(chip_hw_info->rst_gpio))
1805 		gpio_free(chip_hw_info->rst_gpio);
1806 request_rst_gpio_failed:
1807 	if (gpio_is_valid(chip_hw_info->intr_gpio))
1808 		gpio_free(chip_hw_info->intr_gpio);
1809 request_intr_gpio_failed:
1810 	return ret;
1811 }
1812 
1813 #endif
1814 
1815 /*******************************************************
1816 Function:
1817    	Get platform data Funtion
1818 Input:
1819     ts: elan_ts_data struct.
1820 Output:
1821     Executive outcomes.
1822     0: succeed. otherwise: failed
1823 *******************************************************/
1824 
elan_ts_hw_initial(struct elan_ts_data * ts)1825 static int elan_ts_hw_initial(struct elan_ts_data *ts)
1826 {
1827 	int ret = 0;
1828 	struct i2c_client *client = ts->client;
1829 	struct ts_chip_hw_info *hw_info;
1830 
1831 	hw_info = &(ts->hw_info);
1832 #if 0
1833 	hw_info = devm_kzalloc(&client->dev,sizeof(struct ts_chip_hw_info), GFP_KERNEL);
1834 	if (!hw_info) {
1835 		dev_err(&client->dev,
1836 				"ETP Failed to allocate memory for hw_info\n");
1837 		return -ENOMEM;
1838 	}
1839 	else {
1840         hw_info = client->dev.platform_data;
1841 		ts->chip_type = 1; /*1:HID IIC, 0: NORMAL IIC*/
1842 		ts->report_type = 1; /*1:B protocol, 0:A protocol*/
1843   	}
1844 #endif
1845 
1846 
1847 #ifdef CONFIG_OF
1848 	if (client->dev.of_node) {
1849         ret = elan_parse_dt(&client->dev, hw_info);
1850         if (ret)
1851             return ret;
1852   	}
1853 #endif
1854 
1855 
1856 	ts->fw_store_type = FROM_SYS_ETC_FIRMWARE; //define get fw solution
1857 	ts->user_handle_irq = 0;
1858 	//ts->irq_lock_flag = 0;
1859 
1860 	//ts->hw_info = *hw_info;
1861 	ret = elan_ts_gpio_initial(&ts->hw_info);
1862 	if (ret)
1863 	dev_err(&client->dev, "gpio initial failed ret = %d\n",ret);
1864 
1865 	dev_err(&client->dev, "[elan] rst = %d, int = %d, irq=%d\n",hw_info->rst_gpio, hw_info->intr_gpio,hw_info->irq_num);
1866 	dev_err(&client->dev, "[elan] lcm_x = %d, lcm_y = %d\n",hw_info->screen_x, hw_info->screen_y);
1867 
1868 	return ret;
1869 }
1870 
elan_ts_hw_deinit(struct elan_ts_data * ts)1871 static void elan_ts_hw_deinit(struct elan_ts_data *ts)
1872 {
1873 
1874 	regulator_put(ts->vdd);
1875 	regulator_put(ts->vcc_i2c);
1876 	if (gpio_is_valid(ts->hw_info.intr_gpio))
1877 		gpio_free(ts->hw_info.intr_gpio);
1878 
1879 	if (gpio_is_valid(ts->hw_info.rst_gpio))
1880 		gpio_free(ts->hw_info.rst_gpio);
1881 }
1882 
1883 /*******************************************************
1884 Function:
1885     I2c probe.
1886 Input:
1887     client: i2c device struct.
1888     id: device id.
1889 Output:
1890     Executive outcomes.
1891     0: succeed.
1892 *******************************************************/
elan_ts_probe(struct i2c_client * client,const struct i2c_device_id * id)1893 static int elan_ts_probe(struct i2c_client *client,
1894                const struct i2c_device_id *id)
1895 {
1896 //#define SM_BUS
1897 	int err;
1898 #ifdef SM_BUS
1899 	union i2c_smbus_data dummy;
1900 #endif
1901 	struct elan_ts_data *ts;
1902 	int retry = 0;
1903 
1904        	printk("elan %s() %d\n", __func__, __LINE__);
1905 	/*check i2c bus support fuction*/
1906 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1907 		dev_err(&client->dev,
1908 			"i2c check functionality error\n");
1909 			return -ENXIO;
1910 	}
1911 
1912 	/*kzalloc struct elan_ts_data memory */
1913 	ts = devm_kzalloc(&client->dev, sizeof(struct elan_ts_data), GFP_KERNEL);
1914 	if (!ts) {
1915 		dev_err(&client->dev,
1916 			"%s: allocate elan_ts_data failed", __func__);
1917 		return -ENOMEM;
1918 	}
1919 
1920 	printk("elan %s() %d\n", __func__, __LINE__);
1921 
1922 	ts->client = client;
1923 	i2c_set_clientdata(client, ts);
1924 	private_ts = ts;
1925 
1926 
1927 	/*get hw info and initial*/
1928 	err = elan_ts_hw_initial(ts);
1929 	if(err) {
1930 		dev_err(&client->dev, "%s hw initial failed\n",__func__);
1931 		goto free_client_data;
1932 	}
1933 
1934 	printk("elan %s() %d\n", __func__, __LINE__);
1935 	/*set power & power on*/
1936 #if 1
1937 	err = elan_ts_set_power(ts,1);
1938 	if (err) {
1939 		dev_err(&client->dev, "%s power seting  failed\n",__func__);
1940 		goto free_io_port;
1941 	}
1942 	msleep(100);
1943 #endif
1944 
1945 	printk("elan %s() %d\n", __func__, __LINE__);
1946 	/*check elan ic in bus or not*/
1947 #ifdef SM_BUS
1948 	if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1949 		I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
1950 		dev_err(&client->dev, "nothing at this address 0x%x\n", client->addr);
1951 		goto free_power_set;
1952 	}
1953 #endif
1954 
1955 	/*elan ic transfer initial*/
1956 	ts->ops = &elan_ops;
1957 
1958 
1959 	printk("elan %s() %d\n", __func__, __LINE__);
1960 	/*check elan ic status*/
1961 	err = elan_ts_setup(ts);
1962 	if (err < 0) {
1963 		dev_err(&client->dev, "%s ic initial failed\n",__func__);
1964 		goto err_no_elan_chip;
1965 	}
1966 
1967 	/*check rek */
1968 	if(COMPARE_UPGRADE == ts->recover) {
1969 		for (retry = 0; retry < 3; retry++) {
1970 			err = elan_ts_check_calibrate(ts->client); /*ic reponse rek count,count != 0xff? "ok":"failed" */
1971 			if (err) {
1972 				dev_err(&ts->client->dev, "[elan] check rek failed, retry=%d\n",retry);
1973 				err = elan_ts_calibrate(ts->client);
1974 				if (err) {
1975 					dev_err(&ts->client->dev, "[elan]calibrate failed, retry=%d\n",retry);
1976 				} else
1977 					break;
1978 			} else
1979 				break;
1980 		}
1981 	}
1982 
1983 	/*creat dev node & sysfs node for fw operatrion*/
1984 	elan_touch_node_init(ts);
1985 
1986 
1987 	printk("elan %s() %d\n", __func__, __LINE__);
1988 	/*get fw infomation, register input dev, register interrupt*/
1989 	INIT_DELAYED_WORK(&ts->init_work, elan_ic_init_work);
1990 	ts->init_elan_ic_wq = create_singlethread_workqueue("init_elan_ic_wq");
1991 	if (IS_ERR(ts->init_elan_ic_wq)) {
1992 		err = PTR_ERR(ts->init_elan_ic_wq);
1993 		goto err_ic_init_failed;
1994 	}
1995 	queue_delayed_work(ts->init_elan_ic_wq, &ts->init_work, delay);
1996 
1997 
1998 	printk("elan %s() %d\n", __func__, __LINE__);
1999 	/*report work thread*/
2000 	ts->elan_wq = create_singlethread_workqueue("elan_wq");
2001 	if (IS_ERR(ts->elan_wq)) {
2002 		err = PTR_ERR(ts->elan_wq);
2003 		dev_err(&client->dev,
2004 				"[elan error] failed to create kernel thread: %d\n",
2005 				err);
2006 		goto err_create_workqueue_failed;
2007 	}
2008 	INIT_WORK(&ts->ts_work, elan_ts_work_func);
2009 
2010 	/*set print log level*/
2011 	ts->level = TP_DEBUG;
2012 
2013 	/*initial wait queue for userspace*/
2014 	init_waitqueue_head(&ts->elan_userqueue);
2015 	/*lcm callback resume and suspend*/
2016 #if defined(CONFIG_FB)
2017 	ts->fb_notif.notifier_call = fb_notifier_callback;
2018 	err = fb_register_client(&ts->fb_notif);
2019 	if (err)
2020 		dev_err(&client->dev,"[FB]Unable to register fb_notifier: %d", err);
2021 #elif defined(CONFIG_HAS_EARLYSUSPEND)
2022 	ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
2023 	ts->early_suspend.suspend = elan_ts_early_suspend;
2024 	ts->early_suspend.resume = elan_ts_late_resume;
2025 	register_early_suspend(&ts->early_suspend);
2026 #endif
2027 
2028 	printk("elan %s() %d probe success!\n", __func__, __LINE__);
2029 	return err;
2030 
2031 err_create_workqueue_failed:
2032 	destroy_workqueue(ts->elan_wq);
2033 err_ic_init_failed:
2034 	destroy_workqueue(ts->init_elan_ic_wq);
2035 err_no_elan_chip:
2036 #ifdef SM_BUS
2037 free_power_set:
2038 #endif
2039 	regulator_put(ts->vdd);
2040 	regulator_put(ts->vcc_i2c);
2041 #if 1
2042 free_io_port:
2043 	if (gpio_is_valid(ts->hw_info.intr_gpio))
2044 		gpio_free(ts->hw_info.intr_gpio);
2045 
2046 	if (gpio_is_valid(ts->hw_info.rst_gpio))
2047 		gpio_free(ts->hw_info.rst_gpio);
2048 #endif
2049 free_client_data:
2050 	i2c_set_clientdata(client,NULL);
2051 
2052 	return err;
2053 }
2054 
2055 
2056 /*******************************************************
2057 Function:
2058     Elan touchscreen driver release function.
2059 Input:
2060     client: i2c device struct.
2061 Output:
2062     Executive outcomes. 0---succeed.
2063 *******************************************************/
2064 
elan_ts_remove(struct i2c_client * client)2065 static int elan_ts_remove(struct i2c_client *client)
2066 {
2067 	struct elan_ts_data *ts = i2c_get_clientdata(client);
2068 
2069 	elan_ts_hw_deinit(ts);
2070 	elan_touch_node_deinit(ts);
2071 
2072 	input_unregister_device(ts->finger_idev);
2073 	input_unregister_device(ts->pen_idev);
2074 	free_irq(ts->hw_info.irq_num,(void *)elan_ts_irq_handler);
2075 
2076 	if (!IS_ERR(ts->init_elan_ic_wq)) {
2077 		destroy_workqueue(ts->init_elan_ic_wq);
2078 	}
2079 
2080 	if (!IS_ERR(ts->elan_wq)) {
2081 		destroy_workqueue(ts->elan_wq);
2082 	}
2083 #if defined(CONFIG_FB)
2084 	fb_unregister_client(&ts->fb_notif);
2085 #endif
2086 
2087 #ifdef CONFIG_HAS_EARLYSUSPEND
2088 	unregister_early_suspend(&ts->early_suspend);
2089 #endif
2090 	i2c_set_clientdata(client,NULL);
2091 	return 0;
2092 }
2093 
2094 
elan_release_point(void)2095 static void elan_release_point(void)
2096 {
2097 	struct input_dev *fidev;
2098 	struct input_dev *pidev;
2099 	int i = 0;
2100 
2101 	if (private_ts->finger_idev && private_ts->pen_idev) {
2102 		fidev = private_ts->finger_idev;
2103 		pidev = private_ts->pen_idev;
2104 
2105 	if (private_ts->report_type == PROTOCOL_TYPE_B) {
2106 		for (i = 0; i < 10; i++) {
2107 			input_mt_slot(fidev, i);
2108 			input_mt_report_slot_state(fidev, MT_TOOL_FINGER, 0);
2109 		}
2110 
2111 		if (private_ts->report.tool_type == ELAN_PEN) {
2112 			 input_mt_slot(pidev, 0);
2113 			 input_mt_report_slot_state(pidev, MT_TOOL_PEN, false);
2114 		}
2115 
2116 	} else {
2117 		input_mt_sync(fidev);
2118 		input_report_key(fidev, BTN_TOUCH, 0);
2119 
2120 		if (private_ts->report.tool_type == ELAN_PEN) {
2121 			input_mt_sync(pidev);
2122 			input_report_key(pidev, BTN_TOUCH, 0);
2123 		}
2124 	}
2125 	input_sync(fidev);
2126 
2127 	if (private_ts->report.tool_type == ELAN_PEN) {
2128 		input_sync(pidev);
2129 		}
2130 	} else {
2131 		dev_err(&private_ts->client->dev, "Noting done\n");
2132 	}
2133 	return;
2134 }
2135 
elan_ts_set_power_state(struct i2c_client * client,int state)2136 static int elan_ts_set_power_state(struct i2c_client *client, int state)
2137 {
2138 	int err = 0;
2139 	struct elan_ts_data *ts = i2c_get_clientdata(client);
2140 	/*send ic sleep/wake up  command*/
2141 	uint8_t hid_cmd[HID_CMD_LEN] = {0x04, 0x00, 0x23, 0x00, 0x03, 0x00, 0x04, CMD_W_PKT, 0x50, 0x00, 0x01};
2142 	uint8_t cmd[4] = {CMD_W_PKT, 0x50, 0x00, 0x01};
2143 
2144 	if (ts->chip_type == HID_TYPE_PROTOCOL) {
2145 		hid_cmd[8] |= (state << 3);
2146 		err = ts->ops->send(hid_cmd, sizeof(hid_cmd));
2147 		if (err != sizeof(hid_cmd)) {
2148 			err = -EINVAL;
2149 			goto err_set_power_state;
2150 		}
2151 	} else {
2152 		cmd[1] |= (state << 3);
2153 		err = ts->ops->send(cmd,sizeof(cmd));
2154 		if (err != sizeof(cmd)) {
2155 			err = -EINVAL;
2156 			goto err_set_power_state;
2157 		}
2158 	}
2159 
2160 	print_log(ts->level, "[elan] set power stats success\n");
2161 	return 0;
2162 
2163 err_set_power_state:
2164 	return err;
2165 }
2166 
2167 
elan_ts_suspend(struct device * dev)2168 static int elan_ts_suspend(struct device *dev)
2169 {
2170 	struct i2c_client *client = to_i2c_client(dev);
2171 	struct elan_ts_data *ts = i2c_get_clientdata(client);
2172 	int err = 0;
2173 	int retry = RETRY_TIMES;
2174 
2175 	//if do fw upgrade, don't sleep
2176 	if (ts->power_lock == 0) {
2177 		dev_err(&client->dev, "[elan] %s suspend flow \n", __func__);
2178 		elan_switch_irq(ts, 0);
2179 try_set_power:  // if system would not power off, must do this and check
2180 		err = elan_ts_set_power_state(ts->client, PWR_STATE_DEEP_SLEEP);
2181 		if (err) {
2182 			dev_err(&client->dev, "[elan] set power stats failed err = %d\n", err);
2183 			if ( (retry --) > 0)
2184 				goto try_set_power;
2185 		}
2186 
2187 		/*release finger*/
2188 		elan_release_point();
2189 
2190 		/*power off*/
2191 		elan_ts_power_on(ts,false);
2192 	} else {
2193 		dev_err(&client->dev, "[elsn] %s Nothing Done!!\n",__func__);
2194 	}
2195 
2196 	return 0;
2197 }
2198 
2199 
elan_ts_resume(struct device * dev)2200 static int elan_ts_resume(struct device *dev)
2201 {
2202 	struct i2c_client *client = to_i2c_client(dev);
2203 	struct elan_ts_data *ts = i2c_get_clientdata(client);
2204 	int err = 0;
2205 	int retry = RETRY_TIMES;
2206 
2207 
2208 
2209 
2210 	/*
2211 	** enable irq, set ic status, reset ic
2212 	**/
2213 	if (ts->power_lock == 0) {
2214 		dev_err(&client->dev, "[elan] reset gpio to resum tp\n");
2215 		/*device power on*/
2216 		elan_ts_power_on(ts,true);
2217 
2218 		/*delay for ic initial*/
2219 		msleep(100);
2220 
2221 reset_power_state:
2222 		err = elan_ts_set_power_state(ts->client, PWR_STATE_NORMAL);
2223 		if (err) {
2224 			dev_err(&client->dev, "[elan]%s set power stata failed!!\n",__func__);
2225 			if ((retry--) > 0)
2226 				goto reset_power_state;
2227 			else
2228 				elan_ts_hw_reset(&ts->hw_info);
2229 		}
2230 		/*release point*/
2231 		elan_release_point();
2232 		elan_switch_irq(ts, 1);
2233 	} else {
2234 		dev_err(&client->dev, "[elsn] %s Nothing Done!!\n",__func__);
2235 	}
2236 
2237 
2238 
2239 	return 0;
2240 }
2241 
2242 /*******************************************************
2243 Function:
2244    fb_notifier_callback function.
2245 Input:
2246     self: 	notifier_block struct.
2247     event: 	unsigned long.
2248     data: 	void
2249 Output:
2250     0.
2251 *******************************************************/
2252 #if defined(CONFIG_FB)
fb_notifier_callback(struct notifier_block * self,unsigned long event,void * data)2253 static int fb_notifier_callback(struct notifier_block *self,
2254                  unsigned long event, void *data)
2255 {
2256 	struct fb_event *evdata = data;
2257 	int *blank;
2258 	struct elan_ts_data *ts =
2259 	container_of(self, struct elan_ts_data, fb_notif);
2260 
2261 	if (evdata && evdata->data && event == FB_EVENT_BLANK &&
2262 	    ts && ts->client) {
2263 		blank = evdata->data;
2264 		if (*blank == FB_BLANK_UNBLANK)
2265             		elan_ts_resume(&ts->client->dev);
2266         	else if (*blank == FB_BLANK_POWERDOWN)
2267 			elan_ts_suspend(&ts->client->dev);
2268 	}
2269 
2270 	return 0;
2271 }
2272 
2273 #elif defined(CONFIG_HAS_EARLYSUSPEND)
2274 /*******************************************************
2275 Function:
2276     Early suspend function.
2277 Input:
2278     h: early_suspend struct.
2279 Output:
2280     None.
2281 *******************************************************/
elan_ts_early_suspend(struct early_suspend * h)2282 static void elan_ts_early_suspend(struct early_suspend *h)
2283 {
2284 	struct elan_ts_data *ts;
2285 	ts = container_of(h, struct elan_ts_data, early_suspend);
2286 	elan_ts_suspend(&ts->client->dev);
2287 }
2288 
2289 /*******************************************************
2290 Function:
2291     Late resume function.
2292 Input:
2293     h: early_suspend struct.
2294 Output:
2295     None.
2296 *******************************************************/
2297 
elan_ts_late_resume(struct early_suspend * h)2298 static void elan_ts_late_resume(struct early_suspend *h)
2299 {
2300 	struct elan_ts_data *ts;
2301 	ts = container_of(h, struct elan_ts_data, early_suspend);
2302 	elan_ts_resume(&ts->client->dev);
2303 }
2304 #endif/* !CONFIG_HAS_EARLYSUSPEND && !CONFIG_FB*/
2305 
2306 #ifdef CONFIG_PM
2307 static const struct dev_pm_ops elan_ts_dev_pm_ops = {
2308 #if (!defined(CONFIG_FB) && !defined(CONFIG_HAS_EARLYSUSPEND))
2309 	.suspend = elan_ts_suspend,
2310 	.resume = elan_ts_resume,
2311 #endif
2312 };
2313 #endif
2314 
2315 static const struct i2c_device_id elan_ts_id[] = {
2316 	{ ELAN_TS_NAME, 0 },
2317 	{ }
2318 };
2319 
2320 #ifdef CONFIG_OF
2321 static const struct of_device_id elan_of_match[] = {
2322 	{.compatible = "elan,ektf"},
2323 	{},
2324 };
2325 MODULE_DEVICE_TABLE(of, elan_of_match);
2326 #endif
2327 
2328 static struct i2c_driver elan_ts_driver = {
2329 	.probe		= elan_ts_probe,
2330 	.remove		= elan_ts_remove,
2331 	.id_table	= elan_ts_id,
2332 	.driver		= {
2333 		.name	= ELAN_TS_NAME,
2334 #ifdef CONFIG_OF
2335 		.of_match_table = elan_of_match,
2336 #endif
2337 #ifdef CONFIG_PM
2338 		.pm = &elan_ts_dev_pm_ops,
2339 #endif
2340 	},
2341 };
2342 
elan_ts_init(void)2343 static int __init elan_ts_init(void)
2344 {
2345 	int ret = 0;
2346 
2347 	ret = i2c_add_driver(&elan_ts_driver);
2348 	return ret;
2349 }
2350 
elan_ts_exit(void)2351 static void __exit elan_ts_exit(void)
2352 {
2353 	i2c_del_driver(&elan_ts_driver);
2354 	return;
2355 }
2356 
2357 module_init(elan_ts_init);
2358 module_exit(elan_ts_exit);
2359 MODULE_DESCRIPTION("ELAN HID-I2C and I2C Touchscreen Driver");
2360 MODULE_AUTHOR("Minger Zhang <chuming.zhang@elanic.com.cn>");
2361 MODULE_LICENSE("GPL v2");
2362 MODULE_IMPORT_NS(VFS_internal_I_am_really_a_filesystem_and_am_NOT_a_driver);
2363