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