xref: /linux/drivers/input/mouse/elan_i2c_core.c (revision 2ba9268dd603d23e17643437b2246acb6844953b)
1 /*
2  * Elan I2C/SMBus Touchpad driver
3  *
4  * Copyright (c) 2013 ELAN Microelectronics Corp.
5  *
6  * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
7  * Version: 1.5.6
8  *
9  * Based on cyapa driver:
10  * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
11  * copyright (c) 2011-2012 Google, Inc.
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License version 2 as published
15  * by the Free Software Foundation.
16  *
17  * Trademarks are the property of their respective owners.
18  */
19 
20 #include <linux/acpi.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/firmware.h>
24 #include <linux/i2c.h>
25 #include <linux/init.h>
26 #include <linux/input/mt.h>
27 #include <linux/interrupt.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/input.h>
33 #include <linux/uaccess.h>
34 #include <linux/jiffies.h>
35 #include <linux/completion.h>
36 #include <linux/of.h>
37 #include <linux/regulator/consumer.h>
38 #include <asm/unaligned.h>
39 
40 #include "elan_i2c.h"
41 
42 #define DRIVER_NAME		"elan_i2c"
43 #define ELAN_DRIVER_VERSION	"1.5.6"
44 #define ETP_PRESSURE_OFFSET	25
45 #define ETP_MAX_PRESSURE	255
46 #define ETP_FWIDTH_REDUCE	90
47 #define ETP_FINGER_WIDTH	15
48 #define ETP_RETRY_COUNT		3
49 
50 #define ETP_MAX_FINGERS		5
51 #define ETP_FINGER_DATA_LEN	5
52 #define ETP_REPORT_ID		0x5D
53 #define ETP_REPORT_ID_OFFSET	2
54 #define ETP_TOUCH_INFO_OFFSET	3
55 #define ETP_FINGER_DATA_OFFSET	4
56 #define ETP_MAX_REPORT_LEN	34
57 
58 /* The main device structure */
59 struct elan_tp_data {
60 	struct i2c_client	*client;
61 	struct input_dev	*input;
62 	struct regulator	*vcc;
63 
64 	const struct elan_transport_ops *ops;
65 
66 	/* for fw update */
67 	struct completion	fw_completion;
68 	bool			in_fw_update;
69 
70 	struct mutex		sysfs_mutex;
71 
72 	unsigned int		max_x;
73 	unsigned int		max_y;
74 	unsigned int		width_x;
75 	unsigned int		width_y;
76 	unsigned int		x_res;
77 	unsigned int		y_res;
78 
79 	u8			product_id;
80 	u8			fw_version;
81 	u8			sm_version;
82 	u8			iap_version;
83 	u16			fw_checksum;
84 
85 	u8			mode;
86 
87 	bool			irq_wake;
88 
89 	u8			min_baseline;
90 	u8			max_baseline;
91 	bool			baseline_ready;
92 };
93 
94 static int elan_enable_power(struct elan_tp_data *data)
95 {
96 	int repeat = ETP_RETRY_COUNT;
97 	int error;
98 
99 	error = regulator_enable(data->vcc);
100 	if (error) {
101 		dev_err(&data->client->dev,
102 			"Failed to enable regulator: %d\n", error);
103 		return error;
104 	}
105 
106 	do {
107 		error = data->ops->power_control(data->client, true);
108 		if (error >= 0)
109 			return 0;
110 
111 		msleep(30);
112 	} while (--repeat > 0);
113 
114 	return error;
115 }
116 
117 static int elan_disable_power(struct elan_tp_data *data)
118 {
119 	int repeat = ETP_RETRY_COUNT;
120 	int error;
121 
122 	do {
123 		error = data->ops->power_control(data->client, false);
124 		if (!error) {
125 			error = regulator_disable(data->vcc);
126 			if (error) {
127 				dev_err(&data->client->dev,
128 					"Failed to disable regulator: %d\n",
129 					error);
130 				/* Attempt to power the chip back up */
131 				data->ops->power_control(data->client, true);
132 				break;
133 			}
134 
135 			return 0;
136 		}
137 
138 		msleep(30);
139 	} while (--repeat > 0);
140 
141 	return error;
142 }
143 
144 static int elan_sleep(struct elan_tp_data *data)
145 {
146 	int repeat = ETP_RETRY_COUNT;
147 	int error;
148 
149 	do {
150 		error = data->ops->sleep_control(data->client, true);
151 		if (!error)
152 			return 0;
153 
154 		msleep(30);
155 	} while (--repeat > 0);
156 
157 	return error;
158 }
159 
160 static int __elan_initialize(struct elan_tp_data *data)
161 {
162 	struct i2c_client *client = data->client;
163 	int error;
164 
165 	error = data->ops->initialize(client);
166 	if (error) {
167 		dev_err(&client->dev, "device initialize failed: %d\n", error);
168 		return error;
169 	}
170 
171 	data->mode |= ETP_ENABLE_ABS;
172 	error = data->ops->set_mode(client, data->mode);
173 	if (error) {
174 		dev_err(&client->dev,
175 			"failed to switch to absolute mode: %d\n", error);
176 		return error;
177 	}
178 
179 	error = data->ops->sleep_control(client, false);
180 	if (error) {
181 		dev_err(&client->dev,
182 			"failed to wake device up: %d\n", error);
183 		return error;
184 	}
185 
186 	return 0;
187 }
188 
189 static int elan_initialize(struct elan_tp_data *data)
190 {
191 	int repeat = ETP_RETRY_COUNT;
192 	int error;
193 
194 	do {
195 		error = __elan_initialize(data);
196 		if (!error)
197 			return 0;
198 
199 		repeat--;
200 		msleep(30);
201 	} while (--repeat > 0);
202 
203 	return error;
204 }
205 
206 static int elan_query_device_info(struct elan_tp_data *data)
207 {
208 	int error;
209 
210 	error = data->ops->get_product_id(data->client, &data->product_id);
211 	if (error)
212 		return error;
213 
214 	error = data->ops->get_version(data->client, false, &data->fw_version);
215 	if (error)
216 		return error;
217 
218 	error = data->ops->get_checksum(data->client, false,
219 					&data->fw_checksum);
220 	if (error)
221 		return error;
222 
223 	error = data->ops->get_sm_version(data->client, &data->sm_version);
224 	if (error)
225 		return error;
226 
227 	error = data->ops->get_version(data->client, true, &data->iap_version);
228 	if (error)
229 		return error;
230 
231 	return 0;
232 }
233 
234 static unsigned int elan_convert_resolution(u8 val)
235 {
236 	/*
237 	 * (value from firmware) * 10 + 790 = dpi
238 	 *
239 	 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
240 	 * point).
241 	 */
242 
243 	return ((int)(char)val * 10 + 790) * 10 / 254;
244 }
245 
246 static int elan_query_device_parameters(struct elan_tp_data *data)
247 {
248 	unsigned int x_traces, y_traces;
249 	u8 hw_x_res, hw_y_res;
250 	int error;
251 
252 	error = data->ops->get_max(data->client, &data->max_x, &data->max_y);
253 	if (error)
254 		return error;
255 
256 	error = data->ops->get_num_traces(data->client, &x_traces, &y_traces);
257 	if (error)
258 		return error;
259 
260 	data->width_x = data->max_x / x_traces;
261 	data->width_y = data->max_y / y_traces;
262 
263 	error = data->ops->get_resolution(data->client, &hw_x_res, &hw_y_res);
264 	if (error)
265 		return error;
266 
267 	data->x_res = elan_convert_resolution(hw_x_res);
268 	data->y_res = elan_convert_resolution(hw_y_res);
269 
270 	return 0;
271 }
272 
273 /*
274  **********************************************************
275  * IAP firmware updater related routines
276  **********************************************************
277  */
278 static int elan_write_fw_block(struct elan_tp_data *data,
279 			       const u8 *page, u16 checksum, int idx)
280 {
281 	int retry = ETP_RETRY_COUNT;
282 	int error;
283 
284 	do {
285 		error = data->ops->write_fw_block(data->client,
286 						  page, checksum, idx);
287 		if (!error)
288 			return 0;
289 
290 		dev_dbg(&data->client->dev,
291 			"IAP retrying page %d (error: %d)\n", idx, error);
292 	} while (--retry > 0);
293 
294 	return error;
295 }
296 
297 static int __elan_update_firmware(struct elan_tp_data *data,
298 				  const struct firmware *fw)
299 {
300 	struct i2c_client *client = data->client;
301 	struct device *dev = &client->dev;
302 	int i, j;
303 	int error;
304 	u16 iap_start_addr;
305 	u16 boot_page_count;
306 	u16 sw_checksum = 0, fw_checksum = 0;
307 
308 	error = data->ops->prepare_fw_update(client);
309 	if (error)
310 		return error;
311 
312 	iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]);
313 
314 	boot_page_count = (iap_start_addr * 2) / ETP_FW_PAGE_SIZE;
315 	for (i = boot_page_count; i < ETP_FW_VAILDPAGE_COUNT; i++) {
316 		u16 checksum = 0;
317 		const u8 *page = &fw->data[i * ETP_FW_PAGE_SIZE];
318 
319 		for (j = 0; j < ETP_FW_PAGE_SIZE; j += 2)
320 			checksum += ((page[j + 1] << 8) | page[j]);
321 
322 		error = elan_write_fw_block(data, page, checksum, i);
323 		if (error) {
324 			dev_err(dev, "write page %d fail: %d\n", i, error);
325 			return error;
326 		}
327 
328 		sw_checksum += checksum;
329 	}
330 
331 	/* Wait WDT reset and power on reset */
332 	msleep(600);
333 
334 	error = data->ops->finish_fw_update(client, &data->fw_completion);
335 	if (error)
336 		return error;
337 
338 	error = data->ops->get_checksum(client, true, &fw_checksum);
339 	if (error)
340 		return error;
341 
342 	if (sw_checksum != fw_checksum) {
343 		dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n",
344 			sw_checksum, fw_checksum);
345 		return -EIO;
346 	}
347 
348 	return 0;
349 }
350 
351 static int elan_update_firmware(struct elan_tp_data *data,
352 				const struct firmware *fw)
353 {
354 	struct i2c_client *client = data->client;
355 	int retval;
356 
357 	dev_dbg(&client->dev, "Starting firmware update....\n");
358 
359 	disable_irq(client->irq);
360 	data->in_fw_update = true;
361 
362 	retval = __elan_update_firmware(data, fw);
363 	if (retval) {
364 		dev_err(&client->dev, "firmware update failed: %d\n", retval);
365 		data->ops->iap_reset(client);
366 	} else {
367 		/* Reinitialize TP after fw is updated */
368 		elan_initialize(data);
369 		elan_query_device_info(data);
370 	}
371 
372 	data->in_fw_update = false;
373 	enable_irq(client->irq);
374 
375 	return retval;
376 }
377 
378 /*
379  *******************************************************************
380  * SYSFS attributes
381  *******************************************************************
382  */
383 static ssize_t elan_sysfs_read_fw_checksum(struct device *dev,
384 					   struct device_attribute *attr,
385 					   char *buf)
386 {
387 	struct i2c_client *client = to_i2c_client(dev);
388 	struct elan_tp_data *data = i2c_get_clientdata(client);
389 
390 	return sprintf(buf, "0x%04x\n", data->fw_checksum);
391 }
392 
393 static ssize_t elan_sysfs_read_product_id(struct device *dev,
394 					 struct device_attribute *attr,
395 					 char *buf)
396 {
397 	struct i2c_client *client = to_i2c_client(dev);
398 	struct elan_tp_data *data = i2c_get_clientdata(client);
399 
400 	return sprintf(buf, "%d.0\n", data->product_id);
401 }
402 
403 static ssize_t elan_sysfs_read_fw_ver(struct device *dev,
404 				      struct device_attribute *attr,
405 				      char *buf)
406 {
407 	struct i2c_client *client = to_i2c_client(dev);
408 	struct elan_tp_data *data = i2c_get_clientdata(client);
409 
410 	return sprintf(buf, "%d.0\n", data->fw_version);
411 }
412 
413 static ssize_t elan_sysfs_read_sm_ver(struct device *dev,
414 				      struct device_attribute *attr,
415 				      char *buf)
416 {
417 	struct i2c_client *client = to_i2c_client(dev);
418 	struct elan_tp_data *data = i2c_get_clientdata(client);
419 
420 	return sprintf(buf, "%d.0\n", data->sm_version);
421 }
422 
423 static ssize_t elan_sysfs_read_iap_ver(struct device *dev,
424 				       struct device_attribute *attr,
425 				       char *buf)
426 {
427 	struct i2c_client *client = to_i2c_client(dev);
428 	struct elan_tp_data *data = i2c_get_clientdata(client);
429 
430 	return sprintf(buf, "%d.0\n", data->iap_version);
431 }
432 
433 static ssize_t elan_sysfs_update_fw(struct device *dev,
434 				    struct device_attribute *attr,
435 				    const char *buf, size_t count)
436 {
437 	struct elan_tp_data *data = dev_get_drvdata(dev);
438 	const struct firmware *fw;
439 	int error;
440 	const u8 *fw_signature;
441 	static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
442 
443 	error = request_firmware(&fw, ETP_FW_NAME, dev);
444 	if (error) {
445 		dev_err(dev, "cannot load firmware %s: %d\n",
446 			ETP_FW_NAME, error);
447 		return error;
448 	}
449 
450 	/* Firmware file must match signature data */
451 	fw_signature = &fw->data[ETP_FW_SIGNATURE_ADDRESS];
452 	if (memcmp(fw_signature, signature, sizeof(signature)) != 0) {
453 		dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n",
454 			(int)sizeof(signature), signature,
455 			(int)sizeof(signature), fw_signature);
456 		error = -EBADF;
457 		goto out_release_fw;
458 	}
459 
460 	error = mutex_lock_interruptible(&data->sysfs_mutex);
461 	if (error)
462 		goto out_release_fw;
463 
464 	error = elan_update_firmware(data, fw);
465 
466 	mutex_unlock(&data->sysfs_mutex);
467 
468 out_release_fw:
469 	release_firmware(fw);
470 	return error ?: count;
471 }
472 
473 static ssize_t calibrate_store(struct device *dev,
474 			       struct device_attribute *attr,
475 			       const char *buf, size_t count)
476 {
477 	struct i2c_client *client = to_i2c_client(dev);
478 	struct elan_tp_data *data = i2c_get_clientdata(client);
479 	int tries = 20;
480 	int retval;
481 	int error;
482 	u8 val[3];
483 
484 	retval = mutex_lock_interruptible(&data->sysfs_mutex);
485 	if (retval)
486 		return retval;
487 
488 	disable_irq(client->irq);
489 
490 	data->mode |= ETP_ENABLE_CALIBRATE;
491 	retval = data->ops->set_mode(client, data->mode);
492 	if (retval) {
493 		dev_err(dev, "failed to enable calibration mode: %d\n",
494 			retval);
495 		goto out;
496 	}
497 
498 	retval = data->ops->calibrate(client);
499 	if (retval) {
500 		dev_err(dev, "failed to start calibration: %d\n",
501 			retval);
502 		goto out_disable_calibrate;
503 	}
504 
505 	val[0] = 0xff;
506 	do {
507 		/* Wait 250ms before checking if calibration has completed. */
508 		msleep(250);
509 
510 		retval = data->ops->calibrate_result(client, val);
511 		if (retval)
512 			dev_err(dev, "failed to check calibration result: %d\n",
513 				retval);
514 		else if (val[0] == 0)
515 			break; /* calibration done */
516 
517 	} while (--tries);
518 
519 	if (tries == 0) {
520 		dev_err(dev, "failed to calibrate. Timeout.\n");
521 		retval = -ETIMEDOUT;
522 	}
523 
524 out_disable_calibrate:
525 	data->mode &= ~ETP_ENABLE_CALIBRATE;
526 	error = data->ops->set_mode(data->client, data->mode);
527 	if (error) {
528 		dev_err(dev, "failed to disable calibration mode: %d\n",
529 			error);
530 		if (!retval)
531 			retval = error;
532 	}
533 out:
534 	enable_irq(client->irq);
535 	mutex_unlock(&data->sysfs_mutex);
536 	return retval ?: count;
537 }
538 
539 static ssize_t elan_sysfs_read_mode(struct device *dev,
540 				    struct device_attribute *attr,
541 				    char *buf)
542 {
543 	struct i2c_client *client = to_i2c_client(dev);
544 	struct elan_tp_data *data = i2c_get_clientdata(client);
545 	int error;
546 	enum tp_mode mode;
547 
548 	error = mutex_lock_interruptible(&data->sysfs_mutex);
549 	if (error)
550 		return error;
551 
552 	error = data->ops->iap_get_mode(data->client, &mode);
553 
554 	mutex_unlock(&data->sysfs_mutex);
555 
556 	if (error)
557 		return error;
558 
559 	return sprintf(buf, "%d\n", (int)mode);
560 }
561 
562 static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL);
563 static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL);
564 static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL);
565 static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL);
566 static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL);
567 static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL);
568 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw);
569 
570 static DEVICE_ATTR_WO(calibrate);
571 
572 static struct attribute *elan_sysfs_entries[] = {
573 	&dev_attr_product_id.attr,
574 	&dev_attr_firmware_version.attr,
575 	&dev_attr_sample_version.attr,
576 	&dev_attr_iap_version.attr,
577 	&dev_attr_fw_checksum.attr,
578 	&dev_attr_calibrate.attr,
579 	&dev_attr_mode.attr,
580 	&dev_attr_update_fw.attr,
581 	NULL,
582 };
583 
584 static const struct attribute_group elan_sysfs_group = {
585 	.attrs = elan_sysfs_entries,
586 };
587 
588 static ssize_t acquire_store(struct device *dev, struct device_attribute *attr,
589 			     const char *buf, size_t count)
590 {
591 	struct i2c_client *client = to_i2c_client(dev);
592 	struct elan_tp_data *data = i2c_get_clientdata(client);
593 	int error;
594 	int retval;
595 
596 	retval = mutex_lock_interruptible(&data->sysfs_mutex);
597 	if (retval)
598 		return retval;
599 
600 	disable_irq(client->irq);
601 
602 	data->baseline_ready = false;
603 
604 	data->mode |= ETP_ENABLE_CALIBRATE;
605 	retval = data->ops->set_mode(data->client, data->mode);
606 	if (retval) {
607 		dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n",
608 			retval);
609 		goto out;
610 	}
611 
612 	msleep(250);
613 
614 	retval = data->ops->get_baseline_data(data->client, true,
615 					      &data->max_baseline);
616 	if (retval) {
617 		dev_err(dev, "Failed to read max baseline form device: %d\n",
618 			retval);
619 		goto out_disable_calibrate;
620 	}
621 
622 	retval = data->ops->get_baseline_data(data->client, false,
623 					      &data->min_baseline);
624 	if (retval) {
625 		dev_err(dev, "Failed to read min baseline form device: %d\n",
626 			retval);
627 		goto out_disable_calibrate;
628 	}
629 
630 	data->baseline_ready = true;
631 
632 out_disable_calibrate:
633 	data->mode &= ~ETP_ENABLE_CALIBRATE;
634 	error = data->ops->set_mode(data->client, data->mode);
635 	if (error) {
636 		dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n",
637 			error);
638 		if (!retval)
639 			retval = error;
640 	}
641 out:
642 	enable_irq(client->irq);
643 	mutex_unlock(&data->sysfs_mutex);
644 	return retval ?: count;
645 }
646 
647 static ssize_t min_show(struct device *dev,
648 			struct device_attribute *attr, char *buf)
649 {
650 	struct i2c_client *client = to_i2c_client(dev);
651 	struct elan_tp_data *data = i2c_get_clientdata(client);
652 	int retval;
653 
654 	retval = mutex_lock_interruptible(&data->sysfs_mutex);
655 	if (retval)
656 		return retval;
657 
658 	if (!data->baseline_ready) {
659 		retval = -ENODATA;
660 		goto out;
661 	}
662 
663 	retval = snprintf(buf, PAGE_SIZE, "%d", data->min_baseline);
664 
665 out:
666 	mutex_unlock(&data->sysfs_mutex);
667 	return retval;
668 }
669 
670 static ssize_t max_show(struct device *dev,
671 			struct device_attribute *attr, char *buf)
672 {
673 	struct i2c_client *client = to_i2c_client(dev);
674 	struct elan_tp_data *data = i2c_get_clientdata(client);
675 	int retval;
676 
677 	retval = mutex_lock_interruptible(&data->sysfs_mutex);
678 	if (retval)
679 		return retval;
680 
681 	if (!data->baseline_ready) {
682 		retval = -ENODATA;
683 		goto out;
684 	}
685 
686 	retval = snprintf(buf, PAGE_SIZE, "%d", data->max_baseline);
687 
688 out:
689 	mutex_unlock(&data->sysfs_mutex);
690 	return retval;
691 }
692 
693 
694 static DEVICE_ATTR_WO(acquire);
695 static DEVICE_ATTR_RO(min);
696 static DEVICE_ATTR_RO(max);
697 
698 static struct attribute *elan_baseline_sysfs_entries[] = {
699 	&dev_attr_acquire.attr,
700 	&dev_attr_min.attr,
701 	&dev_attr_max.attr,
702 	NULL,
703 };
704 
705 static const struct attribute_group elan_baseline_sysfs_group = {
706 	.name = "baseline",
707 	.attrs = elan_baseline_sysfs_entries,
708 };
709 
710 static const struct attribute_group *elan_sysfs_groups[] = {
711 	&elan_sysfs_group,
712 	&elan_baseline_sysfs_group,
713 	NULL
714 };
715 
716 /*
717  ******************************************************************
718  * Elan isr functions
719  ******************************************************************
720  */
721 static void elan_report_contact(struct elan_tp_data *data,
722 				int contact_num, bool contact_valid,
723 				u8 *finger_data)
724 {
725 	struct input_dev *input = data->input;
726 	unsigned int pos_x, pos_y;
727 	unsigned int pressure, mk_x, mk_y;
728 	unsigned int area_x, area_y, major, minor, new_pressure;
729 
730 
731 	if (contact_valid) {
732 		pos_x = ((finger_data[0] & 0xf0) << 4) |
733 						finger_data[1];
734 		pos_y = ((finger_data[0] & 0x0f) << 8) |
735 						finger_data[2];
736 		mk_x = (finger_data[3] & 0x0f);
737 		mk_y = (finger_data[3] >> 4);
738 		pressure = finger_data[4];
739 
740 		if (pos_x > data->max_x || pos_y > data->max_y) {
741 			dev_dbg(input->dev.parent,
742 				"[%d] x=%d y=%d over max (%d, %d)",
743 				contact_num, pos_x, pos_y,
744 				data->max_x, data->max_y);
745 			return;
746 		}
747 
748 		/*
749 		 * To avoid treating large finger as palm, let's reduce the
750 		 * width x and y per trace.
751 		 */
752 		area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE);
753 		area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE);
754 
755 		major = max(area_x, area_y);
756 		minor = min(area_x, area_y);
757 
758 		new_pressure = pressure + ETP_PRESSURE_OFFSET;
759 		if (new_pressure > ETP_MAX_PRESSURE)
760 			new_pressure = ETP_MAX_PRESSURE;
761 
762 		input_mt_slot(input, contact_num);
763 		input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
764 		input_report_abs(input, ABS_MT_POSITION_X, pos_x);
765 		input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y);
766 		input_report_abs(input, ABS_MT_PRESSURE, new_pressure);
767 		input_report_abs(input, ABS_TOOL_WIDTH, mk_x);
768 		input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
769 		input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
770 	} else {
771 		input_mt_slot(input, contact_num);
772 		input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
773 	}
774 }
775 
776 static void elan_report_absolute(struct elan_tp_data *data, u8 *packet)
777 {
778 	struct input_dev *input = data->input;
779 	u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET];
780 	int i;
781 	u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET];
782 	bool contact_valid;
783 
784 	for (i = 0; i < ETP_MAX_FINGERS; i++) {
785 		contact_valid = tp_info & (1U << (3 + i));
786 		elan_report_contact(data, i, contact_valid, finger_data);
787 
788 		if (contact_valid)
789 			finger_data += ETP_FINGER_DATA_LEN;
790 	}
791 
792 	input_report_key(input, BTN_LEFT, tp_info & 0x01);
793 	input_mt_report_pointer_emulation(input, true);
794 	input_sync(input);
795 }
796 
797 static irqreturn_t elan_isr(int irq, void *dev_id)
798 {
799 	struct elan_tp_data *data = dev_id;
800 	struct device *dev = &data->client->dev;
801 	int error;
802 	u8 report[ETP_MAX_REPORT_LEN];
803 
804 	/*
805 	 * When device is connected to i2c bus, when all IAP page writes
806 	 * complete, the driver will receive interrupt and must read
807 	 * 0000 to confirm that IAP is finished.
808 	*/
809 	if (data->in_fw_update) {
810 		complete(&data->fw_completion);
811 		goto out;
812 	}
813 
814 	error = data->ops->get_report(data->client, report);
815 	if (error)
816 		goto out;
817 
818 	if (report[ETP_REPORT_ID_OFFSET] != ETP_REPORT_ID)
819 		dev_err(dev, "invalid report id data (%x)\n",
820 			report[ETP_REPORT_ID_OFFSET]);
821 	else
822 		elan_report_absolute(data, report);
823 
824 out:
825 	return IRQ_HANDLED;
826 }
827 
828 /*
829  ******************************************************************
830  * Elan initialization functions
831  ******************************************************************
832  */
833 static int elan_setup_input_device(struct elan_tp_data *data)
834 {
835 	struct device *dev = &data->client->dev;
836 	struct input_dev *input;
837 	unsigned int max_width = max(data->width_x, data->width_y);
838 	unsigned int min_width = min(data->width_x, data->width_y);
839 	int error;
840 
841 	input = devm_input_allocate_device(dev);
842 	if (!input)
843 		return -ENOMEM;
844 
845 	input->name = "Elan Touchpad";
846 	input->id.bustype = BUS_I2C;
847 	input_set_drvdata(input, data);
848 
849 	error = input_mt_init_slots(input, ETP_MAX_FINGERS,
850 				    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
851 	if (error) {
852 		dev_err(dev, "failed to initialize MT slots: %d\n", error);
853 		return error;
854 	}
855 
856 	__set_bit(EV_ABS, input->evbit);
857 	__set_bit(INPUT_PROP_POINTER, input->propbit);
858 	__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
859 	__set_bit(BTN_LEFT, input->keybit);
860 
861 	/* Set up ST parameters */
862 	input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0);
863 	input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0);
864 	input_abs_set_res(input, ABS_X, data->x_res);
865 	input_abs_set_res(input, ABS_Y, data->y_res);
866 	input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0);
867 	input_set_abs_params(input, ABS_TOOL_WIDTH, 0, ETP_FINGER_WIDTH, 0, 0);
868 
869 	/* And MT parameters */
870 	input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0);
871 	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0);
872 	input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res);
873 	input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res);
874 	input_set_abs_params(input, ABS_MT_PRESSURE, 0,
875 			     ETP_MAX_PRESSURE, 0, 0);
876 	input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
877 			     ETP_FINGER_WIDTH * max_width, 0, 0);
878 	input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0,
879 			     ETP_FINGER_WIDTH * min_width, 0, 0);
880 
881 	data->input = input;
882 
883 	return 0;
884 }
885 
886 static void elan_disable_regulator(void *_data)
887 {
888 	struct elan_tp_data *data = _data;
889 
890 	regulator_disable(data->vcc);
891 }
892 
893 static void elan_remove_sysfs_groups(void *_data)
894 {
895 	struct elan_tp_data *data = _data;
896 
897 	sysfs_remove_groups(&data->client->dev.kobj, elan_sysfs_groups);
898 }
899 
900 static int elan_probe(struct i2c_client *client,
901 		      const struct i2c_device_id *dev_id)
902 {
903 	const struct elan_transport_ops *transport_ops;
904 	struct device *dev = &client->dev;
905 	struct elan_tp_data *data;
906 	unsigned long irqflags;
907 	int error;
908 
909 	if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) &&
910 	    i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
911 		transport_ops = &elan_i2c_ops;
912 	} else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) &&
913 		   i2c_check_functionality(client->adapter,
914 					   I2C_FUNC_SMBUS_BYTE_DATA |
915 						I2C_FUNC_SMBUS_BLOCK_DATA |
916 						I2C_FUNC_SMBUS_I2C_BLOCK)) {
917 		transport_ops = &elan_smbus_ops;
918 	} else {
919 		dev_err(dev, "not a supported I2C/SMBus adapter\n");
920 		return -EIO;
921 	}
922 
923 	data = devm_kzalloc(&client->dev, sizeof(struct elan_tp_data),
924 			    GFP_KERNEL);
925 	if (!data)
926 		return -ENOMEM;
927 
928 	i2c_set_clientdata(client, data);
929 
930 	data->ops = transport_ops;
931 	data->client = client;
932 	init_completion(&data->fw_completion);
933 	mutex_init(&data->sysfs_mutex);
934 
935 	data->vcc = devm_regulator_get(&client->dev, "vcc");
936 	if (IS_ERR(data->vcc)) {
937 		error = PTR_ERR(data->vcc);
938 		if (error != -EPROBE_DEFER)
939 			dev_err(&client->dev,
940 				"Failed to get 'vcc' regulator: %d\n",
941 				error);
942 		return error;
943 	}
944 
945 	error = regulator_enable(data->vcc);
946 	if (error) {
947 		dev_err(&client->dev,
948 			"Failed to enable regulator: %d\n", error);
949 		return error;
950 	}
951 
952 	error = devm_add_action(&client->dev,
953 				elan_disable_regulator, data);
954 	if (error) {
955 		regulator_disable(data->vcc);
956 		dev_err(&client->dev,
957 			"Failed to add disable regulator action: %d\n",
958 			error);
959 		return error;
960 	}
961 
962 	/* Initialize the touchpad. */
963 	error = elan_initialize(data);
964 	if (error)
965 		return error;
966 
967 	error = elan_query_device_info(data);
968 	if (error)
969 		return error;
970 
971 	error = elan_query_device_parameters(data);
972 	if (error)
973 		return error;
974 
975 	dev_dbg(&client->dev,
976 		"Elan Touchpad Information:\n"
977 		"    Module product ID:  0x%04x\n"
978 		"    Firmware Version:  0x%04x\n"
979 		"    Sample Version:  0x%04x\n"
980 		"    IAP Version:  0x%04x\n"
981 		"    Max ABS X,Y:   %d,%d\n"
982 		"    Width X,Y:   %d,%d\n"
983 		"    Resolution X,Y:   %d,%d (dots/mm)\n",
984 		data->product_id,
985 		data->fw_version,
986 		data->sm_version,
987 		data->iap_version,
988 		data->max_x, data->max_y,
989 		data->width_x, data->width_y,
990 		data->x_res, data->y_res);
991 
992 	/* Set up input device properties based on queried parameters. */
993 	error = elan_setup_input_device(data);
994 	if (error)
995 		return error;
996 
997 	/*
998 	 * Systems using device tree should set up interrupt via DTS,
999 	 * the rest will use the default falling edge interrupts.
1000 	 */
1001 	irqflags = client->dev.of_node ? 0 : IRQF_TRIGGER_FALLING;
1002 
1003 	error = devm_request_threaded_irq(&client->dev, client->irq,
1004 					  NULL, elan_isr,
1005 					  irqflags | IRQF_ONESHOT,
1006 					  client->name, data);
1007 	if (error) {
1008 		dev_err(&client->dev, "cannot register irq=%d\n", client->irq);
1009 		return error;
1010 	}
1011 
1012 	error = sysfs_create_groups(&client->dev.kobj, elan_sysfs_groups);
1013 	if (error) {
1014 		dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
1015 			error);
1016 		return error;
1017 	}
1018 
1019 	error = devm_add_action(&client->dev,
1020 				elan_remove_sysfs_groups, data);
1021 	if (error) {
1022 		elan_remove_sysfs_groups(data);
1023 		dev_err(&client->dev,
1024 			"Failed to add sysfs cleanup action: %d\n",
1025 			error);
1026 		return error;
1027 	}
1028 
1029 	error = input_register_device(data->input);
1030 	if (error) {
1031 		dev_err(&client->dev, "failed to register input device: %d\n",
1032 			error);
1033 		return error;
1034 	}
1035 
1036 	/*
1037 	 * Systems using device tree should set up wakeup via DTS,
1038 	 * the rest will configure device as wakeup source by default.
1039 	 */
1040 	if (!client->dev.of_node)
1041 		device_init_wakeup(&client->dev, true);
1042 
1043 	return 0;
1044 }
1045 
1046 static int __maybe_unused elan_suspend(struct device *dev)
1047 {
1048 	struct i2c_client *client = to_i2c_client(dev);
1049 	struct elan_tp_data *data = i2c_get_clientdata(client);
1050 	int ret;
1051 
1052 	/*
1053 	 * We are taking the mutex to make sure sysfs operations are
1054 	 * complete before we attempt to bring the device into low[er]
1055 	 * power mode.
1056 	 */
1057 	ret = mutex_lock_interruptible(&data->sysfs_mutex);
1058 	if (ret)
1059 		return ret;
1060 
1061 	disable_irq(client->irq);
1062 
1063 	if (device_may_wakeup(dev)) {
1064 		ret = elan_sleep(data);
1065 		/* Enable wake from IRQ */
1066 		data->irq_wake = (enable_irq_wake(client->irq) == 0);
1067 	} else {
1068 		ret = elan_disable_power(data);
1069 	}
1070 
1071 	mutex_unlock(&data->sysfs_mutex);
1072 	return ret;
1073 }
1074 
1075 static int __maybe_unused elan_resume(struct device *dev)
1076 {
1077 	struct i2c_client *client = to_i2c_client(dev);
1078 	struct elan_tp_data *data = i2c_get_clientdata(client);
1079 	int error;
1080 
1081 	if (device_may_wakeup(dev) && data->irq_wake) {
1082 		disable_irq_wake(client->irq);
1083 		data->irq_wake = false;
1084 	}
1085 
1086 	error = elan_enable_power(data);
1087 	if (error)
1088 		dev_err(dev, "power up when resuming failed: %d\n", error);
1089 
1090 	error = elan_initialize(data);
1091 	if (error)
1092 		dev_err(dev, "initialize when resuming failed: %d\n", error);
1093 
1094 	enable_irq(data->client->irq);
1095 
1096 	return 0;
1097 }
1098 
1099 static SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume);
1100 
1101 static const struct i2c_device_id elan_id[] = {
1102 	{ DRIVER_NAME, 0 },
1103 	{ },
1104 };
1105 MODULE_DEVICE_TABLE(i2c, elan_id);
1106 
1107 #ifdef CONFIG_ACPI
1108 static const struct acpi_device_id elan_acpi_id[] = {
1109 	{ "ELAN0000", 0 },
1110 	{ }
1111 };
1112 MODULE_DEVICE_TABLE(acpi, elan_acpi_id);
1113 #endif
1114 
1115 #ifdef CONFIG_OF
1116 static const struct of_device_id elan_of_match[] = {
1117 	{ .compatible = "elan,ekth3000" },
1118 	{ /* sentinel */ }
1119 };
1120 MODULE_DEVICE_TABLE(of, elan_of_match);
1121 #endif
1122 
1123 static struct i2c_driver elan_driver = {
1124 	.driver = {
1125 		.name	= DRIVER_NAME,
1126 		.owner	= THIS_MODULE,
1127 		.pm	= &elan_pm_ops,
1128 		.acpi_match_table = ACPI_PTR(elan_acpi_id),
1129 		.of_match_table = of_match_ptr(elan_of_match),
1130 	},
1131 	.probe		= elan_probe,
1132 	.id_table	= elan_id,
1133 };
1134 
1135 module_i2c_driver(elan_driver);
1136 
1137 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1138 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1139 MODULE_LICENSE("GPL");
1140 MODULE_VERSION(ELAN_DRIVER_VERSION);
1141