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