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