2 * Cypress APA trackpad with I2C interface
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>
9 * Copyright (C) 2011-2014 Cypress Semiconductor, Inc.
10 * Copyright (C) 2011-2012 Google, Inc.
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
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/slab.h>
25 #include <linux/uaccess.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/acpi.h>
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
36 #define CYAPA_FW_NAME "cyapa.bin"
38 const char product_id[] = "CYTRA";
40 static int cyapa_reinitialize(struct cyapa *cyapa);
42 static inline bool cyapa_is_bootloader_mode(struct cyapa *cyapa)
44 if (cyapa->gen == CYAPA_GEN5 && cyapa->state == CYAPA_STATE_GEN5_BL)
47 if (cyapa->gen == CYAPA_GEN3 &&
48 cyapa->state >= CYAPA_STATE_BL_BUSY &&
49 cyapa->state <= CYAPA_STATE_BL_ACTIVE)
55 static inline bool cyapa_is_operational_mode(struct cyapa *cyapa)
57 if (cyapa->gen == CYAPA_GEN5 && cyapa->state == CYAPA_STATE_GEN5_APP)
60 if (cyapa->gen == CYAPA_GEN3 && cyapa->state == CYAPA_STATE_OP)
66 /* Returns 0 on success, else negative errno on failure. */
67 static ssize_t cyapa_i2c_read(struct cyapa *cyapa, u8 reg, size_t len,
70 struct i2c_client *client = cyapa->client;
71 struct i2c_msg msgs[] = {
87 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
89 if (ret != ARRAY_SIZE(msgs))
90 return ret < 0 ? ret : -EIO;
96 * cyapa_i2c_write - Execute i2c block data write operation
97 * @cyapa: Handle to this driver
98 * @ret: Offset of the data to written in the register map
99 * @len: number of bytes to write
100 * @values: Data to be written
102 * Return negative errno code on error; return zero when success.
104 static int cyapa_i2c_write(struct cyapa *cyapa, u8 reg,
105 size_t len, const void *values)
107 struct i2c_client *client = cyapa->client;
111 if (len > sizeof(buf) - 1)
115 memcpy(&buf[1], values, len);
117 ret = i2c_master_send(client, buf, len + 1);
119 return ret < 0 ? ret : -EIO;
124 static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
126 u8 ret = CYAPA_ADAPTER_FUNC_NONE;
128 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
129 ret |= CYAPA_ADAPTER_FUNC_I2C;
130 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
131 I2C_FUNC_SMBUS_BLOCK_DATA |
132 I2C_FUNC_SMBUS_I2C_BLOCK))
133 ret |= CYAPA_ADAPTER_FUNC_SMBUS;
138 * Query device for its current operating state.
140 static int cyapa_get_state(struct cyapa *cyapa)
142 u8 status[BL_STATUS_SIZE];
144 /* The i2c address of gen4 and gen5 trackpad device must be even. */
145 bool even_addr = ((cyapa->client->addr & 0x0001) == 0);
150 cyapa->state = CYAPA_STATE_NO_DEVICE;
153 * Get trackpad status by reading 3 registers starting from 0.
154 * If the device is in the bootloader, this will be BL_HEAD.
155 * If the device is in operation mode, this will be the DATA regs.
158 error = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE,
162 * On smbus systems in OP mode, the i2c_reg_read will fail with
163 * -ETIMEDOUT. In this case, try again using the smbus equivalent
164 * command. This should return a BL_HEAD indicating CYAPA_STATE_OP.
166 if (cyapa->smbus && (error == -ETIMEDOUT || error == -ENXIO)) {
168 error = cyapa_read_block(cyapa,
169 CYAPA_CMD_BL_STATUS, status);
173 if (error != BL_STATUS_SIZE)
177 * Detect trackpad protocol based on characteristic registers and bits.
180 cyapa->status[REG_OP_STATUS] = status[REG_OP_STATUS];
181 cyapa->status[REG_BL_STATUS] = status[REG_BL_STATUS];
182 cyapa->status[REG_BL_ERROR] = status[REG_BL_ERROR];
184 if (cyapa->gen == CYAPA_GEN_UNKNOWN ||
185 cyapa->gen == CYAPA_GEN3) {
186 error = cyapa_gen3_ops.state_parse(cyapa,
187 status, BL_STATUS_SIZE);
191 if ((cyapa->gen == CYAPA_GEN_UNKNOWN ||
192 cyapa->gen == CYAPA_GEN5) &&
193 !smbus && even_addr) {
194 error = cyapa_gen5_ops.state_parse(cyapa,
195 status, BL_STATUS_SIZE);
201 * Write 0x00 0x00 to trackpad device to force update its
202 * status, then redo the detection again.
207 error = cyapa_i2c_write(cyapa, 0, 2, cmd);
213 error = cyapa_i2c_read(cyapa, BL_HEAD_OFFSET,
214 BL_STATUS_SIZE, status);
218 } while (--retries > 0 && !smbus);
223 if (cyapa->state <= CYAPA_STATE_BL_BUSY)
228 return (error < 0) ? error : -EAGAIN;
232 * Poll device for its status in a loop, waiting up to timeout for a response.
234 * When the device switches state, it usually takes ~300 ms.
235 * However, when running a new firmware image, the device must calibrate its
236 * sensors, which can take as long as 2 seconds.
238 * Note: The timeout has granularity of the polling rate, which is 100 ms.
241 * 0 when the device eventually responds with a valid non-busy state.
242 * -ETIMEDOUT if device never responds (too many -EAGAIN)
243 * -EAGAIN if bootload is busy, or unknown state.
246 int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
249 int tries = timeout / 100;
252 error = cyapa_get_state(cyapa);
253 if (!error && cyapa->state > CYAPA_STATE_BL_BUSY)
259 return (error == -EAGAIN || error == -ETIMEDOUT) ? -ETIMEDOUT : error;
263 * Check if device is operational.
265 * An operational device is responding, has exited bootloader, and has
266 * firmware supported by this driver.
270 * -EBUSY no device or in bootloader
271 * -EIO failure while reading from device
272 * -ETIMEDOUT timeout failure for bus idle or bus no response
273 * -EAGAIN device is still in bootloader
274 * if ->state = CYAPA_STATE_BL_IDLE, device has invalid firmware
275 * -EINVAL device is in operational mode, but not supported by this driver
276 * 0 device is supported
278 static int cyapa_check_is_operational(struct cyapa *cyapa)
282 error = cyapa_poll_state(cyapa, 4000);
286 switch (cyapa->gen) {
288 cyapa->ops = &cyapa_gen5_ops;
291 cyapa->ops = &cyapa_gen3_ops;
297 error = cyapa->ops->operational_check(cyapa);
298 if (!error && cyapa_is_operational_mode(cyapa))
299 cyapa->operational = true;
301 cyapa->operational = false;
308 * Returns 0 on device detected, negative errno on no device detected.
309 * And when the device is detected and opertaional, it will be reset to
310 * full power active mode automatically.
312 static int cyapa_detect(struct cyapa *cyapa)
314 struct device *dev = &cyapa->client->dev;
317 error = cyapa_check_is_operational(cyapa);
319 if (error != -ETIMEDOUT && error != -ENODEV &&
320 cyapa_is_bootloader_mode(cyapa)) {
321 dev_warn(dev, "device detected but not operational\n");
325 dev_err(dev, "no device detected: %d\n", error);
332 static int cyapa_open(struct input_dev *input)
334 struct cyapa *cyapa = input_get_drvdata(input);
335 struct i2c_client *client = cyapa->client;
338 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
342 if (cyapa->operational) {
344 * though failed to set active power mode,
345 * but still may be able to work in lower scan rate
346 * when in operational mode.
348 error = cyapa->ops->set_power_mode(cyapa,
349 PWR_MODE_FULL_ACTIVE, 0);
351 dev_warn(&client->dev,
352 "set active power failed: %d\n", error);
356 error = cyapa_reinitialize(cyapa);
357 if (error || !cyapa->operational) {
358 error = error ? error : -EAGAIN;
363 enable_irq(client->irq);
364 if (!pm_runtime_enabled(&client->dev)) {
365 pm_runtime_set_active(&client->dev);
366 pm_runtime_enable(&client->dev);
369 mutex_unlock(&cyapa->state_sync_lock);
373 static void cyapa_close(struct input_dev *input)
375 struct cyapa *cyapa = input_get_drvdata(input);
376 struct i2c_client *client = cyapa->client;
378 mutex_lock(&cyapa->state_sync_lock);
380 disable_irq(client->irq);
381 if (pm_runtime_enabled(&client->dev))
382 pm_runtime_disable(&client->dev);
383 pm_runtime_set_suspended(&client->dev);
385 if (cyapa->operational)
386 cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0);
388 mutex_unlock(&cyapa->state_sync_lock);
391 static int cyapa_create_input_dev(struct cyapa *cyapa)
393 struct device *dev = &cyapa->client->dev;
394 struct input_dev *input;
397 if (!cyapa->physical_size_x || !cyapa->physical_size_y)
400 input = devm_input_allocate_device(dev);
402 dev_err(dev, "failed to allocate memory for input device.\n");
406 input->name = CYAPA_NAME;
407 input->phys = cyapa->phys;
408 input->id.bustype = BUS_I2C;
409 input->id.version = 1;
410 input->id.product = 0; /* Means any product in eventcomm. */
411 input->dev.parent = &cyapa->client->dev;
413 input->open = cyapa_open;
414 input->close = cyapa_close;
416 input_set_drvdata(input, cyapa);
418 __set_bit(EV_ABS, input->evbit);
420 /* Finger position */
421 input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0,
423 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0,
425 input_set_abs_params(input, ABS_MT_PRESSURE, 0, cyapa->max_z, 0, 0);
426 if (cyapa->gen > CYAPA_GEN3) {
427 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
428 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
430 * Orientation is the angle between the vertical axis and
431 * the major axis of the contact ellipse.
432 * The range is -127 to 127.
433 * the positive direction is clockwise form the vertical axis.
434 * If the ellipse of contact degenerates into a circle,
435 * orientation is reported as 0.
437 * Also, for Gen5 trackpad the accurate of this orientation
438 * value is value + (-30 ~ 30).
440 input_set_abs_params(input, ABS_MT_ORIENTATION,
443 if (cyapa->gen >= CYAPA_GEN5) {
444 input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
445 input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 255, 0, 0);
448 input_abs_set_res(input, ABS_MT_POSITION_X,
449 cyapa->max_abs_x / cyapa->physical_size_x);
450 input_abs_set_res(input, ABS_MT_POSITION_Y,
451 cyapa->max_abs_y / cyapa->physical_size_y);
453 if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
454 __set_bit(BTN_LEFT, input->keybit);
455 if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
456 __set_bit(BTN_MIDDLE, input->keybit);
457 if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
458 __set_bit(BTN_RIGHT, input->keybit);
460 if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK)
461 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
463 /* Handle pointer emulation and unused slots in core */
464 error = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
465 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
467 dev_err(dev, "failed to initialize MT slots: %d\n", error);
471 /* Register the device in input subsystem */
472 error = input_register_device(input);
474 dev_err(dev, "failed to register input device: %d\n", error);
478 cyapa->input = input;
482 static void cyapa_enable_irq_for_cmd(struct cyapa *cyapa)
484 struct input_dev *input = cyapa->input;
486 if (!input || !input->users) {
488 * When input is NULL, TP must be in deep sleep mode.
489 * In this mode, later non-power I2C command will always failed
490 * if not bring it out of deep sleep mode firstly,
491 * so must command TP to active mode here.
493 if (!input || cyapa->operational)
494 cyapa->ops->set_power_mode(cyapa,
495 PWR_MODE_FULL_ACTIVE, 0);
496 /* Gen3 always using polling mode for command. */
497 if (cyapa->gen >= CYAPA_GEN5)
498 enable_irq(cyapa->client->irq);
502 static void cyapa_disable_irq_for_cmd(struct cyapa *cyapa)
504 struct input_dev *input = cyapa->input;
506 if (!input || !input->users) {
507 if (cyapa->gen >= CYAPA_GEN5)
508 disable_irq(cyapa->client->irq);
509 if (!input || cyapa->operational)
510 cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0);
515 * cyapa_sleep_time_to_pwr_cmd and cyapa_pwr_cmd_to_sleep_time
517 * These are helper functions that convert to and from integer idle
518 * times and register settings to write to the PowerMode register.
519 * The trackpad supports between 20ms to 1000ms scan intervals.
520 * The time will be increased in increments of 10ms from 20ms to 100ms.
521 * From 100ms to 1000ms, time will be increased in increments of 20ms.
523 * When Idle_Time < 100, the format to convert Idle_Time to Idle_Command is:
524 * Idle_Command = Idle Time / 10;
525 * When Idle_Time >= 100, the format to convert Idle_Time to Idle_Command is:
526 * Idle_Command = Idle Time / 20 + 5;
528 u8 cyapa_sleep_time_to_pwr_cmd(u16 sleep_time)
532 sleep_time = clamp_val(sleep_time, 20, 1000);
533 encoded_time = sleep_time < 100 ? sleep_time / 10 : sleep_time / 20 + 5;
534 return (encoded_time << 2) & PWR_MODE_MASK;
537 u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode)
539 u8 encoded_time = pwr_mode >> 2;
541 return (encoded_time < 10) ? encoded_time * 10
542 : (encoded_time - 5) * 20;
545 /* 0 on driver initialize and detected successfully, negative on failure. */
546 static int cyapa_initialize(struct cyapa *cyapa)
550 cyapa->state = CYAPA_STATE_NO_DEVICE;
551 cyapa->gen = CYAPA_GEN_UNKNOWN;
552 mutex_init(&cyapa->state_sync_lock);
555 * Set to hard code default, they will be updated with trackpad set
556 * default values after probe and initialized.
558 cyapa->suspend_power_mode = PWR_MODE_SLEEP;
559 cyapa->suspend_sleep_time =
560 cyapa_pwr_cmd_to_sleep_time(cyapa->suspend_power_mode);
562 /* ops.initialize() is aimed to prepare for module communications. */
563 error = cyapa_gen3_ops.initialize(cyapa);
565 error = cyapa_gen5_ops.initialize(cyapa);
569 error = cyapa_detect(cyapa);
573 /* Power down the device until we need it. */
574 if (cyapa->operational)
575 cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0);
580 static int cyapa_reinitialize(struct cyapa *cyapa)
582 struct device *dev = &cyapa->client->dev;
583 struct input_dev *input = cyapa->input;
586 if (pm_runtime_enabled(dev))
587 pm_runtime_disable(dev);
589 /* Avoid command failures when TP was in OFF state. */
590 if (cyapa->operational)
591 cyapa->ops->set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE, 0);
593 error = cyapa_detect(cyapa);
597 if (!input && cyapa->operational) {
598 error = cyapa_create_input_dev(cyapa);
600 dev_err(dev, "create input_dev instance failed: %d\n",
607 if (!input || !input->users) {
608 /* Reset to power OFF state to save power when no user open. */
609 if (cyapa->operational)
610 cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0);
611 } else if (!error && cyapa->operational) {
613 * Make sure only enable runtime PM when device is
614 * in operational mode and input->users > 0.
616 pm_runtime_set_active(dev);
617 pm_runtime_enable(dev);
623 static irqreturn_t cyapa_irq(int irq, void *dev_id)
625 struct cyapa *cyapa = dev_id;
626 struct device *dev = &cyapa->client->dev;
628 pm_runtime_get_sync(dev);
629 if (device_may_wakeup(dev))
630 pm_wakeup_event(dev, 0);
632 /* Interrupt event maybe cuased by host command to trackpad device. */
633 if (cyapa->ops->irq_cmd_handler(cyapa)) {
635 * Interrupt event maybe from trackpad device input reporting.
639 * Still in probling or in firware image
640 * udpating or reading.
642 cyapa->ops->sort_empty_output_data(cyapa,
647 if (!cyapa->operational || cyapa->ops->irq_handler(cyapa)) {
648 if (!mutex_trylock(&cyapa->state_sync_lock)) {
649 cyapa->ops->sort_empty_output_data(cyapa,
653 cyapa_reinitialize(cyapa);
654 mutex_unlock(&cyapa->state_sync_lock);
659 pm_runtime_mark_last_busy(dev);
660 pm_runtime_put_sync_autosuspend(dev);
665 **************************************************************
667 **************************************************************
669 #ifdef CONFIG_PM_SLEEP
670 static ssize_t cyapa_show_suspend_scanrate(struct device *dev,
671 struct device_attribute *attr,
674 struct cyapa *cyapa = dev_get_drvdata(dev);
675 u8 pwr_cmd = cyapa->suspend_power_mode;
680 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
684 pwr_cmd = cyapa->suspend_power_mode;
685 sleep_time = cyapa->suspend_sleep_time;
687 mutex_unlock(&cyapa->state_sync_lock);
690 case PWR_MODE_BTN_ONLY:
691 len = scnprintf(buf, PAGE_SIZE, "%s\n", BTN_ONLY_MODE_NAME);
695 len = scnprintf(buf, PAGE_SIZE, "%s\n", OFF_MODE_NAME);
699 len = scnprintf(buf, PAGE_SIZE, "%u\n",
700 cyapa->gen == CYAPA_GEN3 ?
701 cyapa_pwr_cmd_to_sleep_time(pwr_cmd) :
709 static ssize_t cyapa_update_suspend_scanrate(struct device *dev,
710 struct device_attribute *attr,
711 const char *buf, size_t count)
713 struct cyapa *cyapa = dev_get_drvdata(dev);
717 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
721 if (sysfs_streq(buf, BTN_ONLY_MODE_NAME)) {
722 cyapa->suspend_power_mode = PWR_MODE_BTN_ONLY;
723 } else if (sysfs_streq(buf, OFF_MODE_NAME)) {
724 cyapa->suspend_power_mode = PWR_MODE_OFF;
725 } else if (!kstrtou16(buf, 10, &sleep_time)) {
726 cyapa->suspend_sleep_time = min_t(u16, sleep_time, 1000);
727 cyapa->suspend_power_mode =
728 cyapa_sleep_time_to_pwr_cmd(cyapa->suspend_sleep_time);
733 mutex_unlock(&cyapa->state_sync_lock);
738 static DEVICE_ATTR(suspend_scanrate_ms, S_IRUGO|S_IWUSR,
739 cyapa_show_suspend_scanrate,
740 cyapa_update_suspend_scanrate);
742 static struct attribute *cyapa_power_wakeup_entries[] = {
743 &dev_attr_suspend_scanrate_ms.attr,
747 static const struct attribute_group cyapa_power_wakeup_group = {
748 .name = power_group_name,
749 .attrs = cyapa_power_wakeup_entries,
752 static void cyapa_remove_power_wakeup_group(void *data)
754 struct cyapa *cyapa = data;
756 sysfs_unmerge_group(&cyapa->client->dev.kobj,
757 &cyapa_power_wakeup_group);
760 static int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
762 struct i2c_client *client = cyapa->client;
763 struct device *dev = &client->dev;
766 if (device_can_wakeup(dev)) {
767 error = sysfs_merge_group(&client->dev.kobj,
768 &cyapa_power_wakeup_group);
770 dev_err(dev, "failed to add power wakeup group: %d\n",
775 error = devm_add_action(dev,
776 cyapa_remove_power_wakeup_group, cyapa);
778 cyapa_remove_power_wakeup_group(cyapa);
779 dev_err(dev, "failed to add power cleanup action: %d\n",
788 static inline int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
792 #endif /* CONFIG_PM_SLEEP */
795 static ssize_t cyapa_show_rt_suspend_scanrate(struct device *dev,
796 struct device_attribute *attr,
799 struct cyapa *cyapa = dev_get_drvdata(dev);
804 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
808 pwr_cmd = cyapa->runtime_suspend_power_mode;
809 sleep_time = cyapa->runtime_suspend_sleep_time;
811 mutex_unlock(&cyapa->state_sync_lock);
813 return scnprintf(buf, PAGE_SIZE, "%u\n",
814 cyapa->gen == CYAPA_GEN3 ?
815 cyapa_pwr_cmd_to_sleep_time(pwr_cmd) :
819 static ssize_t cyapa_update_rt_suspend_scanrate(struct device *dev,
820 struct device_attribute *attr,
821 const char *buf, size_t count)
823 struct cyapa *cyapa = dev_get_drvdata(dev);
827 if (buf == NULL || count == 0 || kstrtou16(buf, 10, &time)) {
828 dev_err(dev, "invalid runtime suspend scanrate ms parameter\n");
833 * When the suspend scanrate is changed, pm_runtime_get to resume
834 * a potentially suspended device, update to the new pwr_cmd
835 * and then pm_runtime_put to suspend into the new power mode.
837 pm_runtime_get_sync(dev);
839 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
843 cyapa->runtime_suspend_sleep_time = min_t(u16, time, 1000);
844 cyapa->runtime_suspend_power_mode =
845 cyapa_sleep_time_to_pwr_cmd(cyapa->runtime_suspend_sleep_time);
847 mutex_unlock(&cyapa->state_sync_lock);
849 pm_runtime_put_sync_autosuspend(dev);
854 static DEVICE_ATTR(runtime_suspend_scanrate_ms, S_IRUGO|S_IWUSR,
855 cyapa_show_rt_suspend_scanrate,
856 cyapa_update_rt_suspend_scanrate);
858 static struct attribute *cyapa_power_runtime_entries[] = {
859 &dev_attr_runtime_suspend_scanrate_ms.attr,
863 static const struct attribute_group cyapa_power_runtime_group = {
864 .name = power_group_name,
865 .attrs = cyapa_power_runtime_entries,
868 static void cyapa_remove_power_runtime_group(void *data)
870 struct cyapa *cyapa = data;
872 sysfs_unmerge_group(&cyapa->client->dev.kobj,
873 &cyapa_power_runtime_group);
876 static int cyapa_start_runtime(struct cyapa *cyapa)
878 struct device *dev = &cyapa->client->dev;
881 cyapa->runtime_suspend_power_mode = PWR_MODE_IDLE;
882 cyapa->runtime_suspend_sleep_time =
883 cyapa_pwr_cmd_to_sleep_time(cyapa->runtime_suspend_power_mode);
885 error = sysfs_merge_group(&dev->kobj, &cyapa_power_runtime_group);
888 "failed to create power runtime group: %d\n", error);
892 error = devm_add_action(dev, cyapa_remove_power_runtime_group, cyapa);
894 cyapa_remove_power_runtime_group(cyapa);
896 "failed to add power runtime cleanup action: %d\n",
901 /* runtime is enabled until device is operational and opened. */
902 pm_runtime_set_suspended(dev);
903 pm_runtime_use_autosuspend(dev);
904 pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_DELAY);
909 static inline int cyapa_start_runtime(struct cyapa *cyapa)
913 #endif /* CONFIG_PM */
915 static ssize_t cyapa_show_fm_ver(struct device *dev,
916 struct device_attribute *attr, char *buf)
919 struct cyapa *cyapa = dev_get_drvdata(dev);
921 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
924 error = scnprintf(buf, PAGE_SIZE, "%d.%d\n", cyapa->fw_maj_ver,
926 mutex_unlock(&cyapa->state_sync_lock);
930 static ssize_t cyapa_show_product_id(struct device *dev,
931 struct device_attribute *attr, char *buf)
933 struct cyapa *cyapa = dev_get_drvdata(dev);
937 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
940 size = scnprintf(buf, PAGE_SIZE, "%s\n", cyapa->product_id);
941 mutex_unlock(&cyapa->state_sync_lock);
945 static int cyapa_firmware(struct cyapa *cyapa, const char *fw_name)
947 struct device *dev = &cyapa->client->dev;
948 const struct firmware *fw;
951 error = request_firmware(&fw, fw_name, dev);
953 dev_err(dev, "Could not load firmware from %s: %d\n",
958 error = cyapa->ops->check_fw(cyapa, fw);
960 dev_err(dev, "Invalid CYAPA firmware image: %s\n",
966 * Resume the potentially suspended device because doing FW
967 * update on a device not in the FULL mode has a chance to
970 pm_runtime_get_sync(dev);
972 /* Require IRQ support for firmware update commands. */
973 cyapa_enable_irq_for_cmd(cyapa);
975 error = cyapa->ops->bl_enter(cyapa);
977 dev_err(dev, "bl_enter failed, %d\n", error);
981 error = cyapa->ops->bl_activate(cyapa);
983 dev_err(dev, "bl_activate failed, %d\n", error);
987 error = cyapa->ops->bl_initiate(cyapa, fw);
989 dev_err(dev, "bl_initiate failed, %d\n", error);
993 error = cyapa->ops->update_fw(cyapa, fw);
995 dev_err(dev, "update_fw failed, %d\n", error);
1000 cyapa_disable_irq_for_cmd(cyapa);
1001 pm_runtime_put_noidle(dev);
1004 release_firmware(fw);
1008 static ssize_t cyapa_update_fw_store(struct device *dev,
1009 struct device_attribute *attr,
1010 const char *buf, size_t count)
1012 struct cyapa *cyapa = dev_get_drvdata(dev);
1013 char fw_name[NAME_MAX];
1016 if (count >= NAME_MAX) {
1017 dev_err(dev, "File name too long\n");
1021 memcpy(fw_name, buf, count);
1022 if (fw_name[count - 1] == '\n')
1023 fw_name[count - 1] = '\0';
1025 fw_name[count] = '\0';
1029 * Force the input device to be registered after the firmware
1030 * image is updated, so if the corresponding parameters updated
1031 * in the new firmware image can taken effect immediately.
1033 input_unregister_device(cyapa->input);
1034 cyapa->input = NULL;
1037 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1040 * Whatever, do reinitialize to try to recover TP state to
1041 * previous state just as it entered fw update entrance.
1043 cyapa_reinitialize(cyapa);
1047 error = cyapa_firmware(cyapa, fw_name);
1049 dev_err(dev, "firmware update failed: %d\n", error);
1051 dev_dbg(dev, "firmware update successfully done.\n");
1054 * Redetect trackpad device states because firmware update process
1055 * will reset trackpad device into bootloader mode.
1057 ret = cyapa_reinitialize(cyapa);
1059 dev_err(dev, "failed to redetect after updated: %d\n", ret);
1060 error = error ? error : ret;
1063 mutex_unlock(&cyapa->state_sync_lock);
1065 return error ? error : count;
1068 static ssize_t cyapa_calibrate_store(struct device *dev,
1069 struct device_attribute *attr,
1070 const char *buf, size_t count)
1072 struct cyapa *cyapa = dev_get_drvdata(dev);
1075 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1079 if (cyapa->operational) {
1080 cyapa_enable_irq_for_cmd(cyapa);
1081 error = cyapa->ops->calibrate_store(dev, attr, buf, count);
1082 cyapa_disable_irq_for_cmd(cyapa);
1084 error = -EBUSY; /* Still running in bootloader mode. */
1087 mutex_unlock(&cyapa->state_sync_lock);
1088 return error < 0 ? error : count;
1091 static ssize_t cyapa_show_baseline(struct device *dev,
1092 struct device_attribute *attr, char *buf)
1094 struct cyapa *cyapa = dev_get_drvdata(dev);
1097 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1101 if (cyapa->operational) {
1102 cyapa_enable_irq_for_cmd(cyapa);
1103 error = cyapa->ops->show_baseline(dev, attr, buf);
1104 cyapa_disable_irq_for_cmd(cyapa);
1106 error = -EBUSY; /* Still running in bootloader mode. */
1109 mutex_unlock(&cyapa->state_sync_lock);
1113 static char *cyapa_state_to_string(struct cyapa *cyapa)
1115 switch (cyapa->state) {
1116 case CYAPA_STATE_BL_BUSY:
1117 return "bootloader busy";
1118 case CYAPA_STATE_BL_IDLE:
1119 return "bootloader idle";
1120 case CYAPA_STATE_BL_ACTIVE:
1121 return "bootloader active";
1122 case CYAPA_STATE_GEN5_BL:
1123 return "bootloader";
1124 case CYAPA_STATE_OP:
1125 case CYAPA_STATE_GEN5_APP:
1126 return "operational"; /* Normal valid state. */
1128 return "invalid mode";
1132 static ssize_t cyapa_show_mode(struct device *dev,
1133 struct device_attribute *attr, char *buf)
1135 struct cyapa *cyapa = dev_get_drvdata(dev);
1139 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1143 size = scnprintf(buf, PAGE_SIZE, "gen%d %s\n",
1144 cyapa->gen, cyapa_state_to_string(cyapa));
1146 mutex_unlock(&cyapa->state_sync_lock);
1150 static DEVICE_ATTR(firmware_version, S_IRUGO, cyapa_show_fm_ver, NULL);
1151 static DEVICE_ATTR(product_id, S_IRUGO, cyapa_show_product_id, NULL);
1152 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, cyapa_update_fw_store);
1153 static DEVICE_ATTR(baseline, S_IRUGO, cyapa_show_baseline, NULL);
1154 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, cyapa_calibrate_store);
1155 static DEVICE_ATTR(mode, S_IRUGO, cyapa_show_mode, NULL);
1157 static struct attribute *cyapa_sysfs_entries[] = {
1158 &dev_attr_firmware_version.attr,
1159 &dev_attr_product_id.attr,
1160 &dev_attr_update_fw.attr,
1161 &dev_attr_baseline.attr,
1162 &dev_attr_calibrate.attr,
1163 &dev_attr_mode.attr,
1167 static const struct attribute_group cyapa_sysfs_group = {
1168 .attrs = cyapa_sysfs_entries,
1171 static void cyapa_remove_sysfs_group(void *data)
1173 struct cyapa *cyapa = data;
1175 sysfs_remove_group(&cyapa->client->dev.kobj, &cyapa_sysfs_group);
1178 static int cyapa_probe(struct i2c_client *client,
1179 const struct i2c_device_id *dev_id)
1181 struct device *dev = &client->dev;
1182 struct cyapa *cyapa;
1184 union i2c_smbus_data dummy;
1187 adapter_func = cyapa_check_adapter_functionality(client);
1188 if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
1189 dev_err(dev, "not a supported I2C/SMBus adapter\n");
1193 /* Make sure there is something at this address */
1194 if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1195 I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0)
1198 cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
1202 /* i2c isn't supported, use smbus */
1203 if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
1204 cyapa->smbus = true;
1206 cyapa->client = client;
1207 i2c_set_clientdata(client, cyapa);
1208 sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
1211 error = cyapa_initialize(cyapa);
1213 dev_err(dev, "failed to detect and initialize tp device.\n");
1217 error = sysfs_create_group(&client->dev.kobj, &cyapa_sysfs_group);
1219 dev_err(dev, "failed to create sysfs entries: %d\n", error);
1223 error = devm_add_action(dev, cyapa_remove_sysfs_group, cyapa);
1225 cyapa_remove_sysfs_group(cyapa);
1226 dev_err(dev, "failed to add sysfs cleanup action: %d\n", error);
1230 error = cyapa_prepare_wakeup_controls(cyapa);
1232 dev_err(dev, "failed to prepare wakeup controls: %d\n", error);
1236 error = cyapa_start_runtime(cyapa);
1238 dev_err(dev, "failed to start pm_runtime: %d\n", error);
1242 error = devm_request_threaded_irq(dev, client->irq,
1244 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1247 dev_err(dev, "failed to request threaded irq: %d\n", error);
1251 /* Disable IRQ until the device is opened */
1252 disable_irq(client->irq);
1255 * Register the device in the input subsystem when it's operational.
1256 * Otherwise, keep in this driver, so it can be be recovered or updated
1257 * through the sysfs mode and update_fw interfaces by user or apps.
1259 if (cyapa->operational) {
1260 error = cyapa_create_input_dev(cyapa);
1262 dev_err(dev, "create input_dev instance failed: %d\n",
1271 static int __maybe_unused cyapa_suspend(struct device *dev)
1273 struct i2c_client *client = to_i2c_client(dev);
1274 struct cyapa *cyapa = i2c_get_clientdata(client);
1278 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1283 * Runtime PM is enable only when device is in operational mode and
1284 * users in use, so need check it before disable it to
1285 * avoid unbalance warning.
1287 if (pm_runtime_enabled(dev))
1288 pm_runtime_disable(dev);
1289 disable_irq(client->irq);
1292 * Set trackpad device to idle mode if wakeup is allowed,
1293 * otherwise turn off.
1295 if (cyapa->operational) {
1296 power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode
1298 error = cyapa->ops->set_power_mode(cyapa, power_mode,
1299 cyapa->suspend_sleep_time);
1301 dev_err(dev, "suspend set power mode failed: %d\n",
1305 if (device_may_wakeup(dev))
1306 cyapa->irq_wake = (enable_irq_wake(client->irq) == 0);
1308 mutex_unlock(&cyapa->state_sync_lock);
1312 static int __maybe_unused cyapa_resume(struct device *dev)
1314 struct i2c_client *client = to_i2c_client(dev);
1315 struct cyapa *cyapa = i2c_get_clientdata(client);
1318 mutex_lock(&cyapa->state_sync_lock);
1320 if (device_may_wakeup(dev) && cyapa->irq_wake) {
1321 disable_irq_wake(client->irq);
1322 cyapa->irq_wake = false;
1325 /* Update device states and runtime PM states. */
1326 error = cyapa_reinitialize(cyapa);
1328 dev_warn(dev, "failed to reinitialize TP device: %d\n", error);
1330 enable_irq(client->irq);
1332 mutex_unlock(&cyapa->state_sync_lock);
1336 static int __maybe_unused cyapa_runtime_suspend(struct device *dev)
1338 struct cyapa *cyapa = dev_get_drvdata(dev);
1341 error = cyapa->ops->set_power_mode(cyapa,
1342 cyapa->runtime_suspend_power_mode,
1343 cyapa->runtime_suspend_sleep_time);
1345 dev_warn(dev, "runtime suspend failed: %d\n", error);
1350 static int __maybe_unused cyapa_runtime_resume(struct device *dev)
1352 struct cyapa *cyapa = dev_get_drvdata(dev);
1355 error = cyapa->ops->set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE, 0);
1357 dev_warn(dev, "runtime resume failed: %d\n", error);
1362 static const struct dev_pm_ops cyapa_pm_ops = {
1363 SET_SYSTEM_SLEEP_PM_OPS(cyapa_suspend, cyapa_resume)
1364 SET_RUNTIME_PM_OPS(cyapa_runtime_suspend, cyapa_runtime_resume, NULL)
1367 static const struct i2c_device_id cyapa_id_table[] = {
1371 MODULE_DEVICE_TABLE(i2c, cyapa_id_table);
1374 static const struct acpi_device_id cyapa_acpi_id[] = {
1375 { "CYAP0000", 0 }, /* Gen3 trackpad with 0x67 I2C address. */
1376 { "CYAP0001", 0 }, /* Gen5 trackpad with 0x24 I2C address. */
1379 MODULE_DEVICE_TABLE(acpi, cyapa_acpi_id);
1382 static struct i2c_driver cyapa_driver = {
1385 .pm = &cyapa_pm_ops,
1386 .acpi_match_table = ACPI_PTR(cyapa_acpi_id),
1389 .probe = cyapa_probe,
1390 .id_table = cyapa_id_table,
1393 module_i2c_driver(cyapa_driver);
1395 MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver");
1396 MODULE_AUTHOR("Dudley Du <dudl@cypress.com>");
1397 MODULE_LICENSE("GPL");