xref: /linux/drivers/input/mouse/cyapa.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 /*
2  * Cypress APA trackpad with I2C interface
3  *
4  * Author: Dudley Du <dudl@cypress.com>
5  * Further cleanup and restructuring by:
6  *   Daniel Kurtz <djkurtz@chromium.org>
7  *   Benson Leung <bleung@chromium.org>
8  *
9  * Copyright (C) 2011-2015 Cypress Semiconductor, Inc.
10  * Copyright (C) 2011-2012 Google, Inc.
11  *
12  * This file is subject to the terms and conditions of the GNU General Public
13  * License.  See the file COPYING in the main directory of this archive for
14  * more details.
15  */
16 
17 #include <linux/delay.h>
18 #include <linux/i2c.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
26 #include <linux/uaccess.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/acpi.h>
29 #include <linux/of.h>
30 #include "cyapa.h"
31 
32 
33 #define CYAPA_ADAPTER_FUNC_NONE   0
34 #define CYAPA_ADAPTER_FUNC_I2C    1
35 #define CYAPA_ADAPTER_FUNC_SMBUS  2
36 #define CYAPA_ADAPTER_FUNC_BOTH   3
37 
38 #define CYAPA_FW_NAME		"cyapa.bin"
39 
40 const char product_id[] = "CYTRA";
41 
42 static int cyapa_reinitialize(struct cyapa *cyapa);
43 
44 bool cyapa_is_pip_bl_mode(struct cyapa *cyapa)
45 {
46 	if (cyapa->gen == CYAPA_GEN6 && cyapa->state == CYAPA_STATE_GEN6_BL)
47 		return true;
48 
49 	if (cyapa->gen == CYAPA_GEN5 && cyapa->state == CYAPA_STATE_GEN5_BL)
50 		return true;
51 
52 	return false;
53 }
54 
55 bool cyapa_is_pip_app_mode(struct cyapa *cyapa)
56 {
57 	if (cyapa->gen == CYAPA_GEN6 && cyapa->state == CYAPA_STATE_GEN6_APP)
58 		return true;
59 
60 	if (cyapa->gen == CYAPA_GEN5 && cyapa->state == CYAPA_STATE_GEN5_APP)
61 		return true;
62 
63 	return false;
64 }
65 
66 static bool cyapa_is_bootloader_mode(struct cyapa *cyapa)
67 {
68 	if (cyapa_is_pip_bl_mode(cyapa))
69 		return true;
70 
71 	if (cyapa->gen == CYAPA_GEN3 &&
72 		cyapa->state >= CYAPA_STATE_BL_BUSY &&
73 		cyapa->state <= CYAPA_STATE_BL_ACTIVE)
74 		return true;
75 
76 	return false;
77 }
78 
79 static inline bool cyapa_is_operational_mode(struct cyapa *cyapa)
80 {
81 	if (cyapa_is_pip_app_mode(cyapa))
82 		return true;
83 
84 	if (cyapa->gen == CYAPA_GEN3 && cyapa->state == CYAPA_STATE_OP)
85 		return true;
86 
87 	return false;
88 }
89 
90 /* Returns 0 on success, else negative errno on failure. */
91 static ssize_t cyapa_i2c_read(struct cyapa *cyapa, u8 reg, size_t len,
92 					u8 *values)
93 {
94 	struct i2c_client *client = cyapa->client;
95 	struct i2c_msg msgs[] = {
96 		{
97 			.addr = client->addr,
98 			.flags = 0,
99 			.len = 1,
100 			.buf = &reg,
101 		},
102 		{
103 			.addr = client->addr,
104 			.flags = I2C_M_RD,
105 			.len = len,
106 			.buf = values,
107 		},
108 	};
109 	int ret;
110 
111 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
112 
113 	if (ret != ARRAY_SIZE(msgs))
114 		return ret < 0 ? ret : -EIO;
115 
116 	return 0;
117 }
118 
119 /**
120  * cyapa_i2c_write - Execute i2c block data write operation
121  * @cyapa: Handle to this driver
122  * @reg: Offset of the data to written in the register map
123  * @len: number of bytes to write
124  * @values: Data to be written
125  *
126  * Return negative errno code on error; return zero when success.
127  */
128 static int cyapa_i2c_write(struct cyapa *cyapa, u8 reg,
129 					 size_t len, const void *values)
130 {
131 	struct i2c_client *client = cyapa->client;
132 	char buf[32];
133 	int ret;
134 
135 	if (len > sizeof(buf) - 1)
136 		return -ENOMEM;
137 
138 	buf[0] = reg;
139 	memcpy(&buf[1], values, len);
140 
141 	ret = i2c_master_send(client, buf, len + 1);
142 	if (ret != len + 1)
143 		return ret < 0 ? ret : -EIO;
144 
145 	return 0;
146 }
147 
148 static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
149 {
150 	u8 ret = CYAPA_ADAPTER_FUNC_NONE;
151 
152 	if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
153 		ret |= CYAPA_ADAPTER_FUNC_I2C;
154 	if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
155 				     I2C_FUNC_SMBUS_BLOCK_DATA |
156 				     I2C_FUNC_SMBUS_I2C_BLOCK))
157 		ret |= CYAPA_ADAPTER_FUNC_SMBUS;
158 	return ret;
159 }
160 
161 /*
162  * Query device for its current operating state.
163  */
164 static int cyapa_get_state(struct cyapa *cyapa)
165 {
166 	u8 status[BL_STATUS_SIZE];
167 	u8 cmd[32];
168 	/* The i2c address of gen4 and gen5 trackpad device must be even. */
169 	bool even_addr = ((cyapa->client->addr & 0x0001) == 0);
170 	bool smbus = false;
171 	int retries = 2;
172 	int error;
173 
174 	cyapa->state = CYAPA_STATE_NO_DEVICE;
175 
176 	/*
177 	 * Get trackpad status by reading 3 registers starting from 0.
178 	 * If the device is in the bootloader, this will be BL_HEAD.
179 	 * If the device is in operation mode, this will be the DATA regs.
180 	 *
181 	 */
182 	error = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE,
183 				       status);
184 
185 	/*
186 	 * On smbus systems in OP mode, the i2c_reg_read will fail with
187 	 * -ETIMEDOUT.  In this case, try again using the smbus equivalent
188 	 * command.  This should return a BL_HEAD indicating CYAPA_STATE_OP.
189 	 */
190 	if (cyapa->smbus && (error == -ETIMEDOUT || error == -ENXIO)) {
191 		if (!even_addr)
192 			error = cyapa_read_block(cyapa,
193 					CYAPA_CMD_BL_STATUS, status);
194 		smbus = true;
195 	}
196 
197 	if (error != BL_STATUS_SIZE)
198 		goto error;
199 
200 	/*
201 	 * Detect trackpad protocol based on characteristic registers and bits.
202 	 */
203 	do {
204 		cyapa->status[REG_OP_STATUS] = status[REG_OP_STATUS];
205 		cyapa->status[REG_BL_STATUS] = status[REG_BL_STATUS];
206 		cyapa->status[REG_BL_ERROR] = status[REG_BL_ERROR];
207 
208 		if (cyapa->gen == CYAPA_GEN_UNKNOWN ||
209 				cyapa->gen == CYAPA_GEN3) {
210 			error = cyapa_gen3_ops.state_parse(cyapa,
211 					status, BL_STATUS_SIZE);
212 			if (!error)
213 				goto out_detected;
214 		}
215 		if (cyapa->gen == CYAPA_GEN_UNKNOWN ||
216 				cyapa->gen == CYAPA_GEN6 ||
217 				cyapa->gen == CYAPA_GEN5) {
218 			error = cyapa_pip_state_parse(cyapa,
219 					status, BL_STATUS_SIZE);
220 			if (!error)
221 				goto out_detected;
222 		}
223 		/* For old Gen5 trackpads detecting. */
224 		if ((cyapa->gen == CYAPA_GEN_UNKNOWN ||
225 				cyapa->gen == CYAPA_GEN5) &&
226 			!smbus && even_addr) {
227 			error = cyapa_gen5_ops.state_parse(cyapa,
228 					status, BL_STATUS_SIZE);
229 			if (!error)
230 				goto out_detected;
231 		}
232 
233 		/*
234 		 * Write 0x00 0x00 to trackpad device to force update its
235 		 * status, then redo the detection again.
236 		 */
237 		if (!smbus) {
238 			cmd[0] = 0x00;
239 			cmd[1] = 0x00;
240 			error = cyapa_i2c_write(cyapa, 0, 2, cmd);
241 			if (error)
242 				goto error;
243 
244 			msleep(50);
245 
246 			error = cyapa_i2c_read(cyapa, BL_HEAD_OFFSET,
247 					BL_STATUS_SIZE, status);
248 			if (error)
249 				goto error;
250 		}
251 	} while (--retries > 0 && !smbus);
252 
253 	goto error;
254 
255 out_detected:
256 	if (cyapa->state <= CYAPA_STATE_BL_BUSY)
257 		return -EAGAIN;
258 	return 0;
259 
260 error:
261 	return (error < 0) ? error : -EAGAIN;
262 }
263 
264 /*
265  * Poll device for its status in a loop, waiting up to timeout for a response.
266  *
267  * When the device switches state, it usually takes ~300 ms.
268  * However, when running a new firmware image, the device must calibrate its
269  * sensors, which can take as long as 2 seconds.
270  *
271  * Note: The timeout has granularity of the polling rate, which is 100 ms.
272  *
273  * Returns:
274  *   0 when the device eventually responds with a valid non-busy state.
275  *   -ETIMEDOUT if device never responds (too many -EAGAIN)
276  *   -EAGAIN    if bootload is busy, or unknown state.
277  *   < 0        other errors
278  */
279 int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
280 {
281 	int error;
282 	int tries = timeout / 100;
283 
284 	do {
285 		error = cyapa_get_state(cyapa);
286 		if (!error && cyapa->state > CYAPA_STATE_BL_BUSY)
287 			return 0;
288 
289 		msleep(100);
290 	} while (tries--);
291 
292 	return (error == -EAGAIN || error == -ETIMEDOUT) ? -ETIMEDOUT : error;
293 }
294 
295 /*
296  * Check if device is operational.
297  *
298  * An operational device is responding, has exited bootloader, and has
299  * firmware supported by this driver.
300  *
301  * Returns:
302  *   -ENODEV no device
303  *   -EBUSY  no device or in bootloader
304  *   -EIO    failure while reading from device
305  *   -ETIMEDOUT timeout failure for bus idle or bus no response
306  *   -EAGAIN device is still in bootloader
307  *           if ->state = CYAPA_STATE_BL_IDLE, device has invalid firmware
308  *   -EINVAL device is in operational mode, but not supported by this driver
309  *   0       device is supported
310  */
311 static int cyapa_check_is_operational(struct cyapa *cyapa)
312 {
313 	int error;
314 
315 	error = cyapa_poll_state(cyapa, 4000);
316 	if (error)
317 		return error;
318 
319 	switch (cyapa->gen) {
320 	case CYAPA_GEN6:
321 		cyapa->ops = &cyapa_gen6_ops;
322 		break;
323 	case CYAPA_GEN5:
324 		cyapa->ops = &cyapa_gen5_ops;
325 		break;
326 	case CYAPA_GEN3:
327 		cyapa->ops = &cyapa_gen3_ops;
328 		break;
329 	default:
330 		return -ENODEV;
331 	}
332 
333 	error = cyapa->ops->operational_check(cyapa);
334 	if (!error && cyapa_is_operational_mode(cyapa))
335 		cyapa->operational = true;
336 	else
337 		cyapa->operational = false;
338 
339 	return error;
340 }
341 
342 
343 /*
344  * Returns 0 on device detected, negative errno on no device detected.
345  * And when the device is detected and operational, it will be reset to
346  * full power active mode automatically.
347  */
348 static int cyapa_detect(struct cyapa *cyapa)
349 {
350 	struct device *dev = &cyapa->client->dev;
351 	int error;
352 
353 	error = cyapa_check_is_operational(cyapa);
354 	if (error) {
355 		if (error != -ETIMEDOUT && error != -ENODEV &&
356 			cyapa_is_bootloader_mode(cyapa)) {
357 			dev_warn(dev, "device detected but not operational\n");
358 			return 0;
359 		}
360 
361 		dev_err(dev, "no device detected: %d\n", error);
362 		return error;
363 	}
364 
365 	return 0;
366 }
367 
368 static int cyapa_open(struct input_dev *input)
369 {
370 	struct cyapa *cyapa = input_get_drvdata(input);
371 	struct i2c_client *client = cyapa->client;
372 	struct device *dev = &client->dev;
373 	int error;
374 
375 	error = mutex_lock_interruptible(&cyapa->state_sync_lock);
376 	if (error)
377 		return error;
378 
379 	if (cyapa->operational) {
380 		/*
381 		 * though failed to set active power mode,
382 		 * but still may be able to work in lower scan rate
383 		 * when in operational mode.
384 		 */
385 		error = cyapa->ops->set_power_mode(cyapa,
386 				PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
387 		if (error) {
388 			dev_warn(dev, "set active power failed: %d\n", error);
389 			goto out;
390 		}
391 	} else {
392 		error = cyapa_reinitialize(cyapa);
393 		if (error || !cyapa->operational) {
394 			error = error ? error : -EAGAIN;
395 			goto out;
396 		}
397 	}
398 
399 	enable_irq(client->irq);
400 	if (!pm_runtime_enabled(dev)) {
401 		pm_runtime_set_active(dev);
402 		pm_runtime_enable(dev);
403 	}
404 
405 	pm_runtime_get_sync(dev);
406 	pm_runtime_put_sync_autosuspend(dev);
407 out:
408 	mutex_unlock(&cyapa->state_sync_lock);
409 	return error;
410 }
411 
412 static void cyapa_close(struct input_dev *input)
413 {
414 	struct cyapa *cyapa = input_get_drvdata(input);
415 	struct i2c_client *client = cyapa->client;
416 	struct device *dev = &cyapa->client->dev;
417 
418 	mutex_lock(&cyapa->state_sync_lock);
419 
420 	disable_irq(client->irq);
421 	if (pm_runtime_enabled(dev))
422 		pm_runtime_disable(dev);
423 	pm_runtime_set_suspended(dev);
424 
425 	if (cyapa->operational)
426 		cyapa->ops->set_power_mode(cyapa,
427 				PWR_MODE_OFF, 0, CYAPA_PM_DEACTIVE);
428 
429 	mutex_unlock(&cyapa->state_sync_lock);
430 }
431 
432 static int cyapa_create_input_dev(struct cyapa *cyapa)
433 {
434 	struct device *dev = &cyapa->client->dev;
435 	struct input_dev *input;
436 	int error;
437 
438 	if (!cyapa->physical_size_x || !cyapa->physical_size_y)
439 		return -EINVAL;
440 
441 	input = devm_input_allocate_device(dev);
442 	if (!input) {
443 		dev_err(dev, "failed to allocate memory for input device.\n");
444 		return -ENOMEM;
445 	}
446 
447 	input->name = CYAPA_NAME;
448 	input->phys = cyapa->phys;
449 	input->id.bustype = BUS_I2C;
450 	input->id.version = 1;
451 	input->id.product = 0;  /* Means any product in eventcomm. */
452 
453 	input->open = cyapa_open;
454 	input->close = cyapa_close;
455 
456 	input_set_drvdata(input, cyapa);
457 
458 	__set_bit(EV_ABS, input->evbit);
459 
460 	/* Finger position */
461 	input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0,
462 			     0);
463 	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0,
464 			     0);
465 	input_set_abs_params(input, ABS_MT_PRESSURE, 0, cyapa->max_z, 0, 0);
466 	if (cyapa->gen > CYAPA_GEN3) {
467 		input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
468 		input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
469 		/*
470 		 * Orientation is the angle between the vertical axis and
471 		 * the major axis of the contact ellipse.
472 		 * The range is -127 to 127.
473 		 * the positive direction is clockwise form the vertical axis.
474 		 * If the ellipse of contact degenerates into a circle,
475 		 * orientation is reported as 0.
476 		 *
477 		 * Also, for Gen5 trackpad the accurate of this orientation
478 		 * value is value + (-30 ~ 30).
479 		 */
480 		input_set_abs_params(input, ABS_MT_ORIENTATION,
481 				-127, 127, 0, 0);
482 	}
483 	if (cyapa->gen >= CYAPA_GEN5) {
484 		input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
485 		input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 255, 0, 0);
486 		input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);
487 	}
488 
489 	input_abs_set_res(input, ABS_MT_POSITION_X,
490 			  cyapa->max_abs_x / cyapa->physical_size_x);
491 	input_abs_set_res(input, ABS_MT_POSITION_Y,
492 			  cyapa->max_abs_y / cyapa->physical_size_y);
493 
494 	if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
495 		__set_bit(BTN_LEFT, input->keybit);
496 	if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
497 		__set_bit(BTN_MIDDLE, input->keybit);
498 	if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
499 		__set_bit(BTN_RIGHT, input->keybit);
500 
501 	if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK)
502 		__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
503 
504 	/* Handle pointer emulation and unused slots in core */
505 	error = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
506 				    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
507 	if (error) {
508 		dev_err(dev, "failed to initialize MT slots: %d\n", error);
509 		return error;
510 	}
511 
512 	/* Register the device in input subsystem */
513 	error = input_register_device(input);
514 	if (error) {
515 		dev_err(dev, "failed to register input device: %d\n", error);
516 		return error;
517 	}
518 
519 	cyapa->input = input;
520 	return 0;
521 }
522 
523 static void cyapa_enable_irq_for_cmd(struct cyapa *cyapa)
524 {
525 	struct input_dev *input = cyapa->input;
526 
527 	if (!input || !input_device_enabled(input)) {
528 		/*
529 		 * When input is NULL, TP must be in deep sleep mode.
530 		 * In this mode, later non-power I2C command will always failed
531 		 * if not bring it out of deep sleep mode firstly,
532 		 * so must command TP to active mode here.
533 		 */
534 		if (!input || cyapa->operational)
535 			cyapa->ops->set_power_mode(cyapa,
536 				PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
537 		/* Gen3 always using polling mode for command. */
538 		if (cyapa->gen >= CYAPA_GEN5)
539 			enable_irq(cyapa->client->irq);
540 	}
541 }
542 
543 static void cyapa_disable_irq_for_cmd(struct cyapa *cyapa)
544 {
545 	struct input_dev *input = cyapa->input;
546 
547 	if (!input || !input_device_enabled(input)) {
548 		if (cyapa->gen >= CYAPA_GEN5)
549 			disable_irq(cyapa->client->irq);
550 		if (!input || cyapa->operational)
551 			cyapa->ops->set_power_mode(cyapa,
552 					PWR_MODE_OFF, 0, CYAPA_PM_ACTIVE);
553 	}
554 }
555 
556 /*
557  * cyapa_sleep_time_to_pwr_cmd and cyapa_pwr_cmd_to_sleep_time
558  *
559  * These are helper functions that convert to and from integer idle
560  * times and register settings to write to the PowerMode register.
561  * The trackpad supports between 20ms to 1000ms scan intervals.
562  * The time will be increased in increments of 10ms from 20ms to 100ms.
563  * From 100ms to 1000ms, time will be increased in increments of 20ms.
564  *
565  * When Idle_Time < 100, the format to convert Idle_Time to Idle_Command is:
566  *   Idle_Command = Idle Time / 10;
567  * When Idle_Time >= 100, the format to convert Idle_Time to Idle_Command is:
568  *   Idle_Command = Idle Time / 20 + 5;
569  */
570 u8 cyapa_sleep_time_to_pwr_cmd(u16 sleep_time)
571 {
572 	u16 encoded_time;
573 
574 	sleep_time = clamp_val(sleep_time, 20, 1000);
575 	encoded_time = sleep_time < 100 ? sleep_time / 10 : sleep_time / 20 + 5;
576 	return (encoded_time << 2) & PWR_MODE_MASK;
577 }
578 
579 u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode)
580 {
581 	u8 encoded_time = pwr_mode >> 2;
582 
583 	return (encoded_time < 10) ? encoded_time * 10
584 				   : (encoded_time - 5) * 20;
585 }
586 
587 /* 0 on driver initialize and detected successfully, negative on failure. */
588 static int cyapa_initialize(struct cyapa *cyapa)
589 {
590 	int error = 0;
591 
592 	cyapa->state = CYAPA_STATE_NO_DEVICE;
593 	cyapa->gen = CYAPA_GEN_UNKNOWN;
594 	mutex_init(&cyapa->state_sync_lock);
595 
596 	/*
597 	 * Set to hard code default, they will be updated with trackpad set
598 	 * default values after probe and initialized.
599 	 */
600 	cyapa->suspend_power_mode = PWR_MODE_SLEEP;
601 	cyapa->suspend_sleep_time =
602 		cyapa_pwr_cmd_to_sleep_time(cyapa->suspend_power_mode);
603 
604 	/* ops.initialize() is aimed to prepare for module communications. */
605 	error = cyapa_gen3_ops.initialize(cyapa);
606 	if (!error)
607 		error = cyapa_gen5_ops.initialize(cyapa);
608 	if (!error)
609 		error = cyapa_gen6_ops.initialize(cyapa);
610 	if (error)
611 		return error;
612 
613 	error = cyapa_detect(cyapa);
614 	if (error)
615 		return error;
616 
617 	/* Power down the device until we need it. */
618 	if (cyapa->operational)
619 		cyapa->ops->set_power_mode(cyapa,
620 				PWR_MODE_OFF, 0, CYAPA_PM_ACTIVE);
621 
622 	return 0;
623 }
624 
625 static int cyapa_reinitialize(struct cyapa *cyapa)
626 {
627 	struct device *dev = &cyapa->client->dev;
628 	struct input_dev *input = cyapa->input;
629 	int error;
630 
631 	if (pm_runtime_enabled(dev))
632 		pm_runtime_disable(dev);
633 
634 	/* Avoid command failures when TP was in OFF state. */
635 	if (cyapa->operational)
636 		cyapa->ops->set_power_mode(cyapa,
637 				PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
638 
639 	error = cyapa_detect(cyapa);
640 	if (error)
641 		goto out;
642 
643 	if (!input && cyapa->operational) {
644 		error = cyapa_create_input_dev(cyapa);
645 		if (error) {
646 			dev_err(dev, "create input_dev instance failed: %d\n",
647 					error);
648 			goto out;
649 		}
650 	}
651 
652 out:
653 	if (!input || !input_device_enabled(input)) {
654 		/* Reset to power OFF state to save power when no user open. */
655 		if (cyapa->operational)
656 			cyapa->ops->set_power_mode(cyapa,
657 					PWR_MODE_OFF, 0, CYAPA_PM_DEACTIVE);
658 	} else if (!error && cyapa->operational) {
659 		/*
660 		 * Make sure only enable runtime PM when device is
661 		 * in operational mode and input->users > 0.
662 		 */
663 		pm_runtime_set_active(dev);
664 		pm_runtime_enable(dev);
665 
666 		pm_runtime_get_sync(dev);
667 		pm_runtime_put_sync_autosuspend(dev);
668 	}
669 
670 	return error;
671 }
672 
673 static irqreturn_t cyapa_irq(int irq, void *dev_id)
674 {
675 	struct cyapa *cyapa = dev_id;
676 	struct device *dev = &cyapa->client->dev;
677 	int error;
678 
679 	if (device_may_wakeup(dev))
680 		pm_wakeup_event(dev, 0);
681 
682 	/* Interrupt event can be caused by host command to trackpad device. */
683 	if (cyapa->ops->irq_cmd_handler(cyapa)) {
684 		/*
685 		 * Interrupt event maybe from trackpad device input reporting.
686 		 */
687 		if (!cyapa->input) {
688 			/*
689 			 * Still in probing or in firmware image
690 			 * updating or reading.
691 			 */
692 			cyapa->ops->sort_empty_output_data(cyapa,
693 					NULL, NULL, NULL);
694 			goto out;
695 		}
696 
697 		if (cyapa->operational) {
698 			error = cyapa->ops->irq_handler(cyapa);
699 
700 			/*
701 			 * Apply runtime power management to touch report event
702 			 * except the events caused by the command responses.
703 			 * Note:
704 			 * It will introduce about 20~40 ms additional delay
705 			 * time in receiving for first valid touch report data.
706 			 * The time is used to execute device runtime resume
707 			 * process.
708 			 */
709 			pm_runtime_get_sync(dev);
710 			pm_runtime_put_sync_autosuspend(dev);
711 		}
712 
713 		if (!cyapa->operational || error) {
714 			if (!mutex_trylock(&cyapa->state_sync_lock)) {
715 				cyapa->ops->sort_empty_output_data(cyapa,
716 					NULL, NULL, NULL);
717 				goto out;
718 			}
719 			cyapa_reinitialize(cyapa);
720 			mutex_unlock(&cyapa->state_sync_lock);
721 		}
722 	}
723 
724 out:
725 	return IRQ_HANDLED;
726 }
727 
728 /*
729  **************************************************************
730  * sysfs interface
731  **************************************************************
732 */
733 #ifdef CONFIG_PM_SLEEP
734 static ssize_t cyapa_show_suspend_scanrate(struct device *dev,
735 					   struct device_attribute *attr,
736 					   char *buf)
737 {
738 	struct cyapa *cyapa = dev_get_drvdata(dev);
739 	u8 pwr_cmd;
740 	u16 sleep_time;
741 	int len;
742 	int error;
743 
744 	error = mutex_lock_interruptible(&cyapa->state_sync_lock);
745 	if (error)
746 		return error;
747 
748 	pwr_cmd = cyapa->suspend_power_mode;
749 	sleep_time = cyapa->suspend_sleep_time;
750 
751 	mutex_unlock(&cyapa->state_sync_lock);
752 
753 	switch (pwr_cmd) {
754 	case PWR_MODE_BTN_ONLY:
755 		len = sysfs_emit(buf, "%s\n", BTN_ONLY_MODE_NAME);
756 		break;
757 
758 	case PWR_MODE_OFF:
759 		len = sysfs_emit(buf, "%s\n", OFF_MODE_NAME);
760 		break;
761 
762 	default:
763 		len = sysfs_emit(buf, "%u\n",
764 				 cyapa->gen == CYAPA_GEN3 ?
765 					cyapa_pwr_cmd_to_sleep_time(pwr_cmd) :
766 					sleep_time);
767 		break;
768 	}
769 
770 	return len;
771 }
772 
773 static ssize_t cyapa_update_suspend_scanrate(struct device *dev,
774 					     struct device_attribute *attr,
775 					     const char *buf, size_t count)
776 {
777 	struct cyapa *cyapa = dev_get_drvdata(dev);
778 	u16 sleep_time;
779 	int error;
780 
781 	error = mutex_lock_interruptible(&cyapa->state_sync_lock);
782 	if (error)
783 		return error;
784 
785 	if (sysfs_streq(buf, BTN_ONLY_MODE_NAME)) {
786 		cyapa->suspend_power_mode = PWR_MODE_BTN_ONLY;
787 	} else if (sysfs_streq(buf, OFF_MODE_NAME)) {
788 		cyapa->suspend_power_mode = PWR_MODE_OFF;
789 	} else if (!kstrtou16(buf, 10, &sleep_time)) {
790 		cyapa->suspend_sleep_time = min_t(u16, sleep_time, 1000);
791 		cyapa->suspend_power_mode =
792 			cyapa_sleep_time_to_pwr_cmd(cyapa->suspend_sleep_time);
793 	} else {
794 		count = -EINVAL;
795 	}
796 
797 	mutex_unlock(&cyapa->state_sync_lock);
798 
799 	return count;
800 }
801 
802 static DEVICE_ATTR(suspend_scanrate_ms, S_IRUGO|S_IWUSR,
803 		   cyapa_show_suspend_scanrate,
804 		   cyapa_update_suspend_scanrate);
805 
806 static struct attribute *cyapa_power_wakeup_entries[] = {
807 	&dev_attr_suspend_scanrate_ms.attr,
808 	NULL,
809 };
810 
811 static const struct attribute_group cyapa_power_wakeup_group = {
812 	.name = power_group_name,
813 	.attrs = cyapa_power_wakeup_entries,
814 };
815 
816 static void cyapa_remove_power_wakeup_group(void *data)
817 {
818 	struct cyapa *cyapa = data;
819 
820 	sysfs_unmerge_group(&cyapa->client->dev.kobj,
821 				&cyapa_power_wakeup_group);
822 }
823 
824 static int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
825 {
826 	struct i2c_client *client = cyapa->client;
827 	struct device *dev = &client->dev;
828 	int error;
829 
830 	if (device_can_wakeup(dev)) {
831 		error = sysfs_merge_group(&dev->kobj,
832 					  &cyapa_power_wakeup_group);
833 		if (error) {
834 			dev_err(dev, "failed to add power wakeup group: %d\n",
835 				error);
836 			return error;
837 		}
838 
839 		error = devm_add_action_or_reset(dev,
840 				cyapa_remove_power_wakeup_group, cyapa);
841 		if (error) {
842 			dev_err(dev, "failed to add power cleanup action: %d\n",
843 				error);
844 			return error;
845 		}
846 	}
847 
848 	return 0;
849 }
850 #else
851 static inline int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
852 {
853 	return 0;
854 }
855 #endif /* CONFIG_PM_SLEEP */
856 
857 #ifdef CONFIG_PM
858 static ssize_t cyapa_show_rt_suspend_scanrate(struct device *dev,
859 					      struct device_attribute *attr,
860 					      char *buf)
861 {
862 	struct cyapa *cyapa = dev_get_drvdata(dev);
863 	u8 pwr_cmd;
864 	u16 sleep_time;
865 	int error;
866 
867 	error = mutex_lock_interruptible(&cyapa->state_sync_lock);
868 	if (error)
869 		return error;
870 
871 	pwr_cmd = cyapa->runtime_suspend_power_mode;
872 	sleep_time = cyapa->runtime_suspend_sleep_time;
873 
874 	mutex_unlock(&cyapa->state_sync_lock);
875 
876 	return sysfs_emit(buf, "%u\n",
877 			  cyapa->gen == CYAPA_GEN3 ?
878 				cyapa_pwr_cmd_to_sleep_time(pwr_cmd) :
879 				sleep_time);
880 }
881 
882 static ssize_t cyapa_update_rt_suspend_scanrate(struct device *dev,
883 						struct device_attribute *attr,
884 						const char *buf, size_t count)
885 {
886 	struct cyapa *cyapa = dev_get_drvdata(dev);
887 	u16 time;
888 	int error;
889 
890 	if (buf == NULL || count == 0 || kstrtou16(buf, 10, &time)) {
891 		dev_err(dev, "invalid runtime suspend scanrate ms parameter\n");
892 		return -EINVAL;
893 	}
894 
895 	/*
896 	 * When the suspend scanrate is changed, pm_runtime_get to resume
897 	 * a potentially suspended device, update to the new pwr_cmd
898 	 * and then pm_runtime_put to suspend into the new power mode.
899 	 */
900 	pm_runtime_get_sync(dev);
901 
902 	error = mutex_lock_interruptible(&cyapa->state_sync_lock);
903 	if (error)
904 		return error;
905 
906 	cyapa->runtime_suspend_sleep_time = min_t(u16, time, 1000);
907 	cyapa->runtime_suspend_power_mode =
908 		cyapa_sleep_time_to_pwr_cmd(cyapa->runtime_suspend_sleep_time);
909 
910 	mutex_unlock(&cyapa->state_sync_lock);
911 
912 	pm_runtime_put_sync_autosuspend(dev);
913 
914 	return count;
915 }
916 
917 static DEVICE_ATTR(runtime_suspend_scanrate_ms, S_IRUGO|S_IWUSR,
918 		   cyapa_show_rt_suspend_scanrate,
919 		   cyapa_update_rt_suspend_scanrate);
920 
921 static struct attribute *cyapa_power_runtime_entries[] = {
922 	&dev_attr_runtime_suspend_scanrate_ms.attr,
923 	NULL,
924 };
925 
926 static const struct attribute_group cyapa_power_runtime_group = {
927 	.name = power_group_name,
928 	.attrs = cyapa_power_runtime_entries,
929 };
930 
931 static void cyapa_remove_power_runtime_group(void *data)
932 {
933 	struct cyapa *cyapa = data;
934 
935 	sysfs_unmerge_group(&cyapa->client->dev.kobj,
936 				&cyapa_power_runtime_group);
937 }
938 
939 static int cyapa_start_runtime(struct cyapa *cyapa)
940 {
941 	struct device *dev = &cyapa->client->dev;
942 	int error;
943 
944 	cyapa->runtime_suspend_power_mode = PWR_MODE_IDLE;
945 	cyapa->runtime_suspend_sleep_time =
946 		cyapa_pwr_cmd_to_sleep_time(cyapa->runtime_suspend_power_mode);
947 
948 	error = sysfs_merge_group(&dev->kobj, &cyapa_power_runtime_group);
949 	if (error) {
950 		dev_err(dev,
951 			"failed to create power runtime group: %d\n", error);
952 		return error;
953 	}
954 
955 	error = devm_add_action_or_reset(dev, cyapa_remove_power_runtime_group,
956 					 cyapa);
957 	if (error) {
958 		dev_err(dev,
959 			"failed to add power runtime cleanup action: %d\n",
960 			error);
961 		return error;
962 	}
963 
964 	/* runtime is enabled until device is operational and opened. */
965 	pm_runtime_set_suspended(dev);
966 	pm_runtime_use_autosuspend(dev);
967 	pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_DELAY);
968 
969 	return 0;
970 }
971 #else
972 static inline int cyapa_start_runtime(struct cyapa *cyapa)
973 {
974 	return 0;
975 }
976 #endif /* CONFIG_PM */
977 
978 static ssize_t cyapa_show_fm_ver(struct device *dev,
979 				 struct device_attribute *attr, char *buf)
980 {
981 	int error;
982 	struct cyapa *cyapa = dev_get_drvdata(dev);
983 
984 	error = mutex_lock_interruptible(&cyapa->state_sync_lock);
985 	if (error)
986 		return error;
987 	error = sysfs_emit(buf, "%d.%d\n",
988 			   cyapa->fw_maj_ver, cyapa->fw_min_ver);
989 	mutex_unlock(&cyapa->state_sync_lock);
990 	return error;
991 }
992 
993 static ssize_t cyapa_show_product_id(struct device *dev,
994 				     struct device_attribute *attr, char *buf)
995 {
996 	struct cyapa *cyapa = dev_get_drvdata(dev);
997 	int size;
998 	int error;
999 
1000 	error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1001 	if (error)
1002 		return error;
1003 	size = sysfs_emit(buf, "%s\n", cyapa->product_id);
1004 	mutex_unlock(&cyapa->state_sync_lock);
1005 	return size;
1006 }
1007 
1008 static int cyapa_firmware(struct cyapa *cyapa, const char *fw_name)
1009 {
1010 	struct device *dev = &cyapa->client->dev;
1011 	const struct firmware *fw;
1012 	int error;
1013 
1014 	error = request_firmware(&fw, fw_name, dev);
1015 	if (error) {
1016 		dev_err(dev, "Could not load firmware from %s: %d\n",
1017 			fw_name, error);
1018 		return error;
1019 	}
1020 
1021 	error = cyapa->ops->check_fw(cyapa, fw);
1022 	if (error) {
1023 		dev_err(dev, "Invalid CYAPA firmware image: %s\n",
1024 				fw_name);
1025 		goto done;
1026 	}
1027 
1028 	/*
1029 	 * Resume the potentially suspended device because doing FW
1030 	 * update on a device not in the FULL mode has a chance to
1031 	 * fail.
1032 	 */
1033 	pm_runtime_get_sync(dev);
1034 
1035 	/* Require IRQ support for firmware update commands. */
1036 	cyapa_enable_irq_for_cmd(cyapa);
1037 
1038 	error = cyapa->ops->bl_enter(cyapa);
1039 	if (error) {
1040 		dev_err(dev, "bl_enter failed, %d\n", error);
1041 		goto err_detect;
1042 	}
1043 
1044 	error = cyapa->ops->bl_activate(cyapa);
1045 	if (error) {
1046 		dev_err(dev, "bl_activate failed, %d\n", error);
1047 		goto err_detect;
1048 	}
1049 
1050 	error = cyapa->ops->bl_initiate(cyapa, fw);
1051 	if (error) {
1052 		dev_err(dev, "bl_initiate failed, %d\n", error);
1053 		goto err_detect;
1054 	}
1055 
1056 	error = cyapa->ops->update_fw(cyapa, fw);
1057 	if (error) {
1058 		dev_err(dev, "update_fw failed, %d\n", error);
1059 		goto err_detect;
1060 	}
1061 
1062 err_detect:
1063 	cyapa_disable_irq_for_cmd(cyapa);
1064 	pm_runtime_put_noidle(dev);
1065 
1066 done:
1067 	release_firmware(fw);
1068 	return error;
1069 }
1070 
1071 static ssize_t cyapa_update_fw_store(struct device *dev,
1072 				     struct device_attribute *attr,
1073 				     const char *buf, size_t count)
1074 {
1075 	struct cyapa *cyapa = dev_get_drvdata(dev);
1076 	char fw_name[NAME_MAX];
1077 	int ret, error;
1078 
1079 	if (!count || count >= NAME_MAX) {
1080 		dev_err(dev, "Bad file name size\n");
1081 		return -EINVAL;
1082 	}
1083 
1084 	memcpy(fw_name, buf, count);
1085 	if (fw_name[count - 1] == '\n')
1086 		fw_name[count - 1] = '\0';
1087 	else
1088 		fw_name[count] = '\0';
1089 
1090 	if (cyapa->input) {
1091 		/*
1092 		 * Force the input device to be registered after the firmware
1093 		 * image is updated, so if the corresponding parameters updated
1094 		 * in the new firmware image can taken effect immediately.
1095 		 */
1096 		input_unregister_device(cyapa->input);
1097 		cyapa->input = NULL;
1098 	}
1099 
1100 	error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1101 	if (error) {
1102 		/*
1103 		 * Whatever, do reinitialize to try to recover TP state to
1104 		 * previous state just as it entered fw update entrance.
1105 		 */
1106 		cyapa_reinitialize(cyapa);
1107 		return error;
1108 	}
1109 
1110 	error = cyapa_firmware(cyapa, fw_name);
1111 	if (error)
1112 		dev_err(dev, "firmware update failed: %d\n", error);
1113 	else
1114 		dev_dbg(dev, "firmware update successfully done.\n");
1115 
1116 	/*
1117 	 * Re-detect trackpad device states because firmware update process
1118 	 * will reset trackpad device into bootloader mode.
1119 	 */
1120 	ret = cyapa_reinitialize(cyapa);
1121 	if (ret) {
1122 		dev_err(dev, "failed to re-detect after updated: %d\n", ret);
1123 		error = error ? error : ret;
1124 	}
1125 
1126 	mutex_unlock(&cyapa->state_sync_lock);
1127 
1128 	return error ? error : count;
1129 }
1130 
1131 static ssize_t cyapa_calibrate_store(struct device *dev,
1132 				     struct device_attribute *attr,
1133 				     const char *buf, size_t count)
1134 {
1135 	struct cyapa *cyapa = dev_get_drvdata(dev);
1136 	int error;
1137 
1138 	error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1139 	if (error)
1140 		return error;
1141 
1142 	if (cyapa->operational) {
1143 		cyapa_enable_irq_for_cmd(cyapa);
1144 		error = cyapa->ops->calibrate_store(dev, attr, buf, count);
1145 		cyapa_disable_irq_for_cmd(cyapa);
1146 	} else {
1147 		error = -EBUSY;  /* Still running in bootloader mode. */
1148 	}
1149 
1150 	mutex_unlock(&cyapa->state_sync_lock);
1151 	return error < 0 ? error : count;
1152 }
1153 
1154 static ssize_t cyapa_show_baseline(struct device *dev,
1155 				   struct device_attribute *attr, char *buf)
1156 {
1157 	struct cyapa *cyapa = dev_get_drvdata(dev);
1158 	ssize_t error;
1159 
1160 	error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1161 	if (error)
1162 		return error;
1163 
1164 	if (cyapa->operational) {
1165 		cyapa_enable_irq_for_cmd(cyapa);
1166 		error = cyapa->ops->show_baseline(dev, attr, buf);
1167 		cyapa_disable_irq_for_cmd(cyapa);
1168 	} else {
1169 		error = -EBUSY;  /* Still running in bootloader mode. */
1170 	}
1171 
1172 	mutex_unlock(&cyapa->state_sync_lock);
1173 	return error;
1174 }
1175 
1176 static char *cyapa_state_to_string(struct cyapa *cyapa)
1177 {
1178 	switch (cyapa->state) {
1179 	case CYAPA_STATE_BL_BUSY:
1180 		return "bootloader busy";
1181 	case CYAPA_STATE_BL_IDLE:
1182 		return "bootloader idle";
1183 	case CYAPA_STATE_BL_ACTIVE:
1184 		return "bootloader active";
1185 	case CYAPA_STATE_GEN5_BL:
1186 	case CYAPA_STATE_GEN6_BL:
1187 		return "bootloader";
1188 	case CYAPA_STATE_OP:
1189 	case CYAPA_STATE_GEN5_APP:
1190 	case CYAPA_STATE_GEN6_APP:
1191 		return "operational";  /* Normal valid state. */
1192 	default:
1193 		return "invalid mode";
1194 	}
1195 }
1196 
1197 static ssize_t cyapa_show_mode(struct device *dev,
1198 				   struct device_attribute *attr, char *buf)
1199 {
1200 	struct cyapa *cyapa = dev_get_drvdata(dev);
1201 	int size;
1202 	int error;
1203 
1204 	error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1205 	if (error)
1206 		return error;
1207 
1208 	size = sysfs_emit(buf, "gen%d %s\n",
1209 			  cyapa->gen, cyapa_state_to_string(cyapa));
1210 
1211 	mutex_unlock(&cyapa->state_sync_lock);
1212 	return size;
1213 }
1214 
1215 static DEVICE_ATTR(firmware_version, S_IRUGO, cyapa_show_fm_ver, NULL);
1216 static DEVICE_ATTR(product_id, S_IRUGO, cyapa_show_product_id, NULL);
1217 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, cyapa_update_fw_store);
1218 static DEVICE_ATTR(baseline, S_IRUGO, cyapa_show_baseline, NULL);
1219 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, cyapa_calibrate_store);
1220 static DEVICE_ATTR(mode, S_IRUGO, cyapa_show_mode, NULL);
1221 
1222 static struct attribute *cyapa_attrs[] = {
1223 	&dev_attr_firmware_version.attr,
1224 	&dev_attr_product_id.attr,
1225 	&dev_attr_update_fw.attr,
1226 	&dev_attr_baseline.attr,
1227 	&dev_attr_calibrate.attr,
1228 	&dev_attr_mode.attr,
1229 	NULL,
1230 };
1231 ATTRIBUTE_GROUPS(cyapa);
1232 
1233 static void cyapa_disable_regulator(void *data)
1234 {
1235 	struct cyapa *cyapa = data;
1236 
1237 	regulator_disable(cyapa->vcc);
1238 }
1239 
1240 static int cyapa_probe(struct i2c_client *client)
1241 {
1242 	struct device *dev = &client->dev;
1243 	struct cyapa *cyapa;
1244 	u8 adapter_func;
1245 	union i2c_smbus_data dummy;
1246 	int error;
1247 
1248 	adapter_func = cyapa_check_adapter_functionality(client);
1249 	if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
1250 		dev_err(dev, "not a supported I2C/SMBus adapter\n");
1251 		return -EIO;
1252 	}
1253 
1254 	/* Make sure there is something at this address */
1255 	if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1256 			I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0)
1257 		return -ENODEV;
1258 
1259 	cyapa = devm_kzalloc_obj(dev, struct cyapa);
1260 	if (!cyapa)
1261 		return -ENOMEM;
1262 
1263 	/* i2c isn't supported, use smbus */
1264 	if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
1265 		cyapa->smbus = true;
1266 
1267 	cyapa->client = client;
1268 	i2c_set_clientdata(client, cyapa);
1269 	sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
1270 		client->addr);
1271 
1272 	cyapa->vcc = devm_regulator_get(dev, "vcc");
1273 	if (IS_ERR(cyapa->vcc)) {
1274 		error = PTR_ERR(cyapa->vcc);
1275 		dev_err(dev, "failed to get vcc regulator: %d\n", error);
1276 		return error;
1277 	}
1278 
1279 	error = regulator_enable(cyapa->vcc);
1280 	if (error) {
1281 		dev_err(dev, "failed to enable regulator: %d\n", error);
1282 		return error;
1283 	}
1284 
1285 	error = devm_add_action_or_reset(dev, cyapa_disable_regulator, cyapa);
1286 	if (error) {
1287 		dev_err(dev, "failed to add disable regulator action: %d\n",
1288 			error);
1289 		return error;
1290 	}
1291 
1292 	error = cyapa_initialize(cyapa);
1293 	if (error) {
1294 		dev_err(dev, "failed to detect and initialize tp device.\n");
1295 		return error;
1296 	}
1297 
1298 	error = cyapa_prepare_wakeup_controls(cyapa);
1299 	if (error) {
1300 		dev_err(dev, "failed to prepare wakeup controls: %d\n", error);
1301 		return error;
1302 	}
1303 
1304 	error = cyapa_start_runtime(cyapa);
1305 	if (error) {
1306 		dev_err(dev, "failed to start pm_runtime: %d\n", error);
1307 		return error;
1308 	}
1309 
1310 	error = devm_request_threaded_irq(dev, client->irq,
1311 					  NULL, cyapa_irq,
1312 					  IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1313 					  "cyapa", cyapa);
1314 	if (error) {
1315 		dev_err(dev, "failed to request threaded irq: %d\n", error);
1316 		return error;
1317 	}
1318 
1319 	/* Disable IRQ until the device is opened */
1320 	disable_irq(client->irq);
1321 
1322 	/*
1323 	 * Register the device in the input subsystem when it's operational.
1324 	 * Otherwise, keep in this driver, so it can be be recovered or updated
1325 	 * through the sysfs mode and update_fw interfaces by user or apps.
1326 	 */
1327 	if (cyapa->operational) {
1328 		error = cyapa_create_input_dev(cyapa);
1329 		if (error) {
1330 			dev_err(dev, "create input_dev instance failed: %d\n",
1331 					error);
1332 			return error;
1333 		}
1334 	}
1335 
1336 	return 0;
1337 }
1338 
1339 static int cyapa_suspend(struct device *dev)
1340 {
1341 	struct i2c_client *client = to_i2c_client(dev);
1342 	struct cyapa *cyapa = i2c_get_clientdata(client);
1343 	u8 power_mode;
1344 	int error;
1345 
1346 	error = mutex_lock_interruptible(&cyapa->input->mutex);
1347 	if (error)
1348 		return error;
1349 
1350 	error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1351 	if (error) {
1352 		mutex_unlock(&cyapa->input->mutex);
1353 		return error;
1354 	}
1355 
1356 	/*
1357 	 * Runtime PM is enable only when device is in operational mode and
1358 	 * users in use, so need check it before disable it to
1359 	 * avoid unbalance warning.
1360 	 */
1361 	if (pm_runtime_enabled(dev))
1362 		pm_runtime_disable(dev);
1363 	disable_irq(client->irq);
1364 
1365 	/*
1366 	 * Set trackpad device to idle mode if wakeup is allowed,
1367 	 * otherwise turn off.
1368 	 */
1369 	if (cyapa->operational) {
1370 		power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode
1371 						    : PWR_MODE_OFF;
1372 		error = cyapa->ops->set_power_mode(cyapa, power_mode,
1373 				cyapa->suspend_sleep_time, CYAPA_PM_SUSPEND);
1374 		if (error)
1375 			dev_err(dev, "suspend set power mode failed: %d\n",
1376 					error);
1377 	}
1378 
1379 	/*
1380 	 * Disable proximity interrupt when system idle, want true touch to
1381 	 * wake the system.
1382 	 */
1383 	if (cyapa->dev_pwr_mode != PWR_MODE_OFF)
1384 		cyapa->ops->set_proximity(cyapa, false);
1385 
1386 	if (device_may_wakeup(dev))
1387 		cyapa->irq_wake = (enable_irq_wake(client->irq) == 0);
1388 
1389 	mutex_unlock(&cyapa->state_sync_lock);
1390 	mutex_unlock(&cyapa->input->mutex);
1391 
1392 	return 0;
1393 }
1394 
1395 static int cyapa_resume(struct device *dev)
1396 {
1397 	struct i2c_client *client = to_i2c_client(dev);
1398 	struct cyapa *cyapa = i2c_get_clientdata(client);
1399 	int error;
1400 
1401 	mutex_lock(&cyapa->input->mutex);
1402 	mutex_lock(&cyapa->state_sync_lock);
1403 
1404 	if (device_may_wakeup(dev) && cyapa->irq_wake) {
1405 		disable_irq_wake(client->irq);
1406 		cyapa->irq_wake = false;
1407 	}
1408 
1409 	/*
1410 	 * Update device states and runtime PM states.
1411 	 * Re-Enable proximity interrupt after enter operational mode.
1412 	 */
1413 	error = cyapa_reinitialize(cyapa);
1414 	if (error)
1415 		dev_warn(dev, "failed to reinitialize TP device: %d\n", error);
1416 
1417 	enable_irq(client->irq);
1418 
1419 	mutex_unlock(&cyapa->state_sync_lock);
1420 	mutex_unlock(&cyapa->input->mutex);
1421 	return 0;
1422 }
1423 
1424 static int cyapa_runtime_suspend(struct device *dev)
1425 {
1426 	struct cyapa *cyapa = dev_get_drvdata(dev);
1427 	int error;
1428 
1429 	error = cyapa->ops->set_power_mode(cyapa,
1430 			cyapa->runtime_suspend_power_mode,
1431 			cyapa->runtime_suspend_sleep_time,
1432 			CYAPA_PM_RUNTIME_SUSPEND);
1433 	if (error)
1434 		dev_warn(dev, "runtime suspend failed: %d\n", error);
1435 
1436 	return 0;
1437 }
1438 
1439 static int cyapa_runtime_resume(struct device *dev)
1440 {
1441 	struct cyapa *cyapa = dev_get_drvdata(dev);
1442 	int error;
1443 
1444 	error = cyapa->ops->set_power_mode(cyapa,
1445 			PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_RUNTIME_RESUME);
1446 	if (error)
1447 		dev_warn(dev, "runtime resume failed: %d\n", error);
1448 
1449 	return 0;
1450 }
1451 
1452 static const struct dev_pm_ops cyapa_pm_ops = {
1453 	SYSTEM_SLEEP_PM_OPS(cyapa_suspend, cyapa_resume)
1454 	RUNTIME_PM_OPS(cyapa_runtime_suspend, cyapa_runtime_resume, NULL)
1455 };
1456 
1457 static const struct i2c_device_id cyapa_id_table[] = {
1458 	{ .name = "cyapa" },
1459 	{ }
1460 };
1461 MODULE_DEVICE_TABLE(i2c, cyapa_id_table);
1462 
1463 #ifdef CONFIG_ACPI
1464 static const struct acpi_device_id cyapa_acpi_id[] = {
1465 	{ .id = "CYAP0000" },  /* Gen3 trackpad with 0x67 I2C address. */
1466 	{ .id = "CYAP0001" },  /* Gen5 trackpad with 0x24 I2C address. */
1467 	{ .id = "CYAP0002" },  /* Gen6 trackpad with 0x24 I2C address. */
1468 	{ }
1469 };
1470 MODULE_DEVICE_TABLE(acpi, cyapa_acpi_id);
1471 #endif
1472 
1473 #ifdef CONFIG_OF
1474 static const struct of_device_id cyapa_of_match[] = {
1475 	{ .compatible = "cypress,cyapa" },
1476 	{ /* sentinel */ }
1477 };
1478 MODULE_DEVICE_TABLE(of, cyapa_of_match);
1479 #endif
1480 
1481 static struct i2c_driver cyapa_driver = {
1482 	.driver = {
1483 		.name = "cyapa",
1484 		.dev_groups = cyapa_groups,
1485 		.pm = pm_ptr(&cyapa_pm_ops),
1486 		.acpi_match_table = ACPI_PTR(cyapa_acpi_id),
1487 		.of_match_table = of_match_ptr(cyapa_of_match),
1488 	},
1489 
1490 	.probe = cyapa_probe,
1491 	.id_table = cyapa_id_table,
1492 };
1493 
1494 module_i2c_driver(cyapa_driver);
1495 
1496 MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver");
1497 MODULE_AUTHOR("Dudley Du <dudl@cypress.com>");
1498 MODULE_LICENSE("GPL");
1499