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