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