0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <linux/delay.h>
0018 #include <linux/i2c.h>
0019 #include <linux/input.h>
0020 #include <linux/input/mt.h>
0021 #include <linux/interrupt.h>
0022 #include <linux/module.h>
0023 #include <linux/mutex.h>
0024 #include <linux/regulator/consumer.h>
0025 #include <linux/slab.h>
0026 #include <linux/uaccess.h>
0027 #include <linux/pm_runtime.h>
0028 #include <linux/acpi.h>
0029 #include <linux/of.h>
0030 #include "cyapa.h"
0031
0032
0033 #define CYAPA_ADAPTER_FUNC_NONE 0
0034 #define CYAPA_ADAPTER_FUNC_I2C 1
0035 #define CYAPA_ADAPTER_FUNC_SMBUS 2
0036 #define CYAPA_ADAPTER_FUNC_BOTH 3
0037
0038 #define CYAPA_FW_NAME "cyapa.bin"
0039
0040 const char product_id[] = "CYTRA";
0041
0042 static int cyapa_reinitialize(struct cyapa *cyapa);
0043
0044 bool cyapa_is_pip_bl_mode(struct cyapa *cyapa)
0045 {
0046 if (cyapa->gen == CYAPA_GEN6 && cyapa->state == CYAPA_STATE_GEN6_BL)
0047 return true;
0048
0049 if (cyapa->gen == CYAPA_GEN5 && cyapa->state == CYAPA_STATE_GEN5_BL)
0050 return true;
0051
0052 return false;
0053 }
0054
0055 bool cyapa_is_pip_app_mode(struct cyapa *cyapa)
0056 {
0057 if (cyapa->gen == CYAPA_GEN6 && cyapa->state == CYAPA_STATE_GEN6_APP)
0058 return true;
0059
0060 if (cyapa->gen == CYAPA_GEN5 && cyapa->state == CYAPA_STATE_GEN5_APP)
0061 return true;
0062
0063 return false;
0064 }
0065
0066 static bool cyapa_is_bootloader_mode(struct cyapa *cyapa)
0067 {
0068 if (cyapa_is_pip_bl_mode(cyapa))
0069 return true;
0070
0071 if (cyapa->gen == CYAPA_GEN3 &&
0072 cyapa->state >= CYAPA_STATE_BL_BUSY &&
0073 cyapa->state <= CYAPA_STATE_BL_ACTIVE)
0074 return true;
0075
0076 return false;
0077 }
0078
0079 static inline bool cyapa_is_operational_mode(struct cyapa *cyapa)
0080 {
0081 if (cyapa_is_pip_app_mode(cyapa))
0082 return true;
0083
0084 if (cyapa->gen == CYAPA_GEN3 && cyapa->state == CYAPA_STATE_OP)
0085 return true;
0086
0087 return false;
0088 }
0089
0090
0091 static ssize_t cyapa_i2c_read(struct cyapa *cyapa, u8 reg, size_t len,
0092 u8 *values)
0093 {
0094 struct i2c_client *client = cyapa->client;
0095 struct i2c_msg msgs[] = {
0096 {
0097 .addr = client->addr,
0098 .flags = 0,
0099 .len = 1,
0100 .buf = ®,
0101 },
0102 {
0103 .addr = client->addr,
0104 .flags = I2C_M_RD,
0105 .len = len,
0106 .buf = values,
0107 },
0108 };
0109 int ret;
0110
0111 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
0112
0113 if (ret != ARRAY_SIZE(msgs))
0114 return ret < 0 ? ret : -EIO;
0115
0116 return 0;
0117 }
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128 static int cyapa_i2c_write(struct cyapa *cyapa, u8 reg,
0129 size_t len, const void *values)
0130 {
0131 struct i2c_client *client = cyapa->client;
0132 char buf[32];
0133 int ret;
0134
0135 if (len > sizeof(buf) - 1)
0136 return -ENOMEM;
0137
0138 buf[0] = reg;
0139 memcpy(&buf[1], values, len);
0140
0141 ret = i2c_master_send(client, buf, len + 1);
0142 if (ret != len + 1)
0143 return ret < 0 ? ret : -EIO;
0144
0145 return 0;
0146 }
0147
0148 static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
0149 {
0150 u8 ret = CYAPA_ADAPTER_FUNC_NONE;
0151
0152 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
0153 ret |= CYAPA_ADAPTER_FUNC_I2C;
0154 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
0155 I2C_FUNC_SMBUS_BLOCK_DATA |
0156 I2C_FUNC_SMBUS_I2C_BLOCK))
0157 ret |= CYAPA_ADAPTER_FUNC_SMBUS;
0158 return ret;
0159 }
0160
0161
0162
0163
0164 static int cyapa_get_state(struct cyapa *cyapa)
0165 {
0166 u8 status[BL_STATUS_SIZE];
0167 u8 cmd[32];
0168
0169 bool even_addr = ((cyapa->client->addr & 0x0001) == 0);
0170 bool smbus = false;
0171 int retries = 2;
0172 int error;
0173
0174 cyapa->state = CYAPA_STATE_NO_DEVICE;
0175
0176
0177
0178
0179
0180
0181
0182 error = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE,
0183 status);
0184
0185
0186
0187
0188
0189
0190 if (cyapa->smbus && (error == -ETIMEDOUT || error == -ENXIO)) {
0191 if (!even_addr)
0192 error = cyapa_read_block(cyapa,
0193 CYAPA_CMD_BL_STATUS, status);
0194 smbus = true;
0195 }
0196
0197 if (error != BL_STATUS_SIZE)
0198 goto error;
0199
0200
0201
0202
0203 do {
0204 cyapa->status[REG_OP_STATUS] = status[REG_OP_STATUS];
0205 cyapa->status[REG_BL_STATUS] = status[REG_BL_STATUS];
0206 cyapa->status[REG_BL_ERROR] = status[REG_BL_ERROR];
0207
0208 if (cyapa->gen == CYAPA_GEN_UNKNOWN ||
0209 cyapa->gen == CYAPA_GEN3) {
0210 error = cyapa_gen3_ops.state_parse(cyapa,
0211 status, BL_STATUS_SIZE);
0212 if (!error)
0213 goto out_detected;
0214 }
0215 if (cyapa->gen == CYAPA_GEN_UNKNOWN ||
0216 cyapa->gen == CYAPA_GEN6 ||
0217 cyapa->gen == CYAPA_GEN5) {
0218 error = cyapa_pip_state_parse(cyapa,
0219 status, BL_STATUS_SIZE);
0220 if (!error)
0221 goto out_detected;
0222 }
0223
0224 if ((cyapa->gen == CYAPA_GEN_UNKNOWN ||
0225 cyapa->gen == CYAPA_GEN5) &&
0226 !smbus && even_addr) {
0227 error = cyapa_gen5_ops.state_parse(cyapa,
0228 status, BL_STATUS_SIZE);
0229 if (!error)
0230 goto out_detected;
0231 }
0232
0233
0234
0235
0236
0237 if (!smbus) {
0238 cmd[0] = 0x00;
0239 cmd[1] = 0x00;
0240 error = cyapa_i2c_write(cyapa, 0, 2, cmd);
0241 if (error)
0242 goto error;
0243
0244 msleep(50);
0245
0246 error = cyapa_i2c_read(cyapa, BL_HEAD_OFFSET,
0247 BL_STATUS_SIZE, status);
0248 if (error)
0249 goto error;
0250 }
0251 } while (--retries > 0 && !smbus);
0252
0253 goto error;
0254
0255 out_detected:
0256 if (cyapa->state <= CYAPA_STATE_BL_BUSY)
0257 return -EAGAIN;
0258 return 0;
0259
0260 error:
0261 return (error < 0) ? error : -EAGAIN;
0262 }
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279 int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
0280 {
0281 int error;
0282 int tries = timeout / 100;
0283
0284 do {
0285 error = cyapa_get_state(cyapa);
0286 if (!error && cyapa->state > CYAPA_STATE_BL_BUSY)
0287 return 0;
0288
0289 msleep(100);
0290 } while (tries--);
0291
0292 return (error == -EAGAIN || error == -ETIMEDOUT) ? -ETIMEDOUT : error;
0293 }
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311 static int cyapa_check_is_operational(struct cyapa *cyapa)
0312 {
0313 int error;
0314
0315 error = cyapa_poll_state(cyapa, 4000);
0316 if (error)
0317 return error;
0318
0319 switch (cyapa->gen) {
0320 case CYAPA_GEN6:
0321 cyapa->ops = &cyapa_gen6_ops;
0322 break;
0323 case CYAPA_GEN5:
0324 cyapa->ops = &cyapa_gen5_ops;
0325 break;
0326 case CYAPA_GEN3:
0327 cyapa->ops = &cyapa_gen3_ops;
0328 break;
0329 default:
0330 return -ENODEV;
0331 }
0332
0333 error = cyapa->ops->operational_check(cyapa);
0334 if (!error && cyapa_is_operational_mode(cyapa))
0335 cyapa->operational = true;
0336 else
0337 cyapa->operational = false;
0338
0339 return error;
0340 }
0341
0342
0343
0344
0345
0346
0347
0348 static int cyapa_detect(struct cyapa *cyapa)
0349 {
0350 struct device *dev = &cyapa->client->dev;
0351 int error;
0352
0353 error = cyapa_check_is_operational(cyapa);
0354 if (error) {
0355 if (error != -ETIMEDOUT && error != -ENODEV &&
0356 cyapa_is_bootloader_mode(cyapa)) {
0357 dev_warn(dev, "device detected but not operational\n");
0358 return 0;
0359 }
0360
0361 dev_err(dev, "no device detected: %d\n", error);
0362 return error;
0363 }
0364
0365 return 0;
0366 }
0367
0368 static int cyapa_open(struct input_dev *input)
0369 {
0370 struct cyapa *cyapa = input_get_drvdata(input);
0371 struct i2c_client *client = cyapa->client;
0372 struct device *dev = &client->dev;
0373 int error;
0374
0375 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
0376 if (error)
0377 return error;
0378
0379 if (cyapa->operational) {
0380
0381
0382
0383
0384
0385 error = cyapa->ops->set_power_mode(cyapa,
0386 PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
0387 if (error) {
0388 dev_warn(dev, "set active power failed: %d\n", error);
0389 goto out;
0390 }
0391 } else {
0392 error = cyapa_reinitialize(cyapa);
0393 if (error || !cyapa->operational) {
0394 error = error ? error : -EAGAIN;
0395 goto out;
0396 }
0397 }
0398
0399 enable_irq(client->irq);
0400 if (!pm_runtime_enabled(dev)) {
0401 pm_runtime_set_active(dev);
0402 pm_runtime_enable(dev);
0403 }
0404
0405 pm_runtime_get_sync(dev);
0406 pm_runtime_mark_last_busy(dev);
0407 pm_runtime_put_sync_autosuspend(dev);
0408 out:
0409 mutex_unlock(&cyapa->state_sync_lock);
0410 return error;
0411 }
0412
0413 static void cyapa_close(struct input_dev *input)
0414 {
0415 struct cyapa *cyapa = input_get_drvdata(input);
0416 struct i2c_client *client = cyapa->client;
0417 struct device *dev = &cyapa->client->dev;
0418
0419 mutex_lock(&cyapa->state_sync_lock);
0420
0421 disable_irq(client->irq);
0422 if (pm_runtime_enabled(dev))
0423 pm_runtime_disable(dev);
0424 pm_runtime_set_suspended(dev);
0425
0426 if (cyapa->operational)
0427 cyapa->ops->set_power_mode(cyapa,
0428 PWR_MODE_OFF, 0, CYAPA_PM_DEACTIVE);
0429
0430 mutex_unlock(&cyapa->state_sync_lock);
0431 }
0432
0433 static int cyapa_create_input_dev(struct cyapa *cyapa)
0434 {
0435 struct device *dev = &cyapa->client->dev;
0436 struct input_dev *input;
0437 int error;
0438
0439 if (!cyapa->physical_size_x || !cyapa->physical_size_y)
0440 return -EINVAL;
0441
0442 input = devm_input_allocate_device(dev);
0443 if (!input) {
0444 dev_err(dev, "failed to allocate memory for input device.\n");
0445 return -ENOMEM;
0446 }
0447
0448 input->name = CYAPA_NAME;
0449 input->phys = cyapa->phys;
0450 input->id.bustype = BUS_I2C;
0451 input->id.version = 1;
0452 input->id.product = 0;
0453 input->dev.parent = &cyapa->client->dev;
0454
0455 input->open = cyapa_open;
0456 input->close = cyapa_close;
0457
0458 input_set_drvdata(input, cyapa);
0459
0460 __set_bit(EV_ABS, input->evbit);
0461
0462
0463 input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0,
0464 0);
0465 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0,
0466 0);
0467 input_set_abs_params(input, ABS_MT_PRESSURE, 0, cyapa->max_z, 0, 0);
0468 if (cyapa->gen > CYAPA_GEN3) {
0469 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
0470 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482 input_set_abs_params(input, ABS_MT_ORIENTATION,
0483 -127, 127, 0, 0);
0484 }
0485 if (cyapa->gen >= CYAPA_GEN5) {
0486 input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
0487 input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 255, 0, 0);
0488 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);
0489 }
0490
0491 input_abs_set_res(input, ABS_MT_POSITION_X,
0492 cyapa->max_abs_x / cyapa->physical_size_x);
0493 input_abs_set_res(input, ABS_MT_POSITION_Y,
0494 cyapa->max_abs_y / cyapa->physical_size_y);
0495
0496 if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
0497 __set_bit(BTN_LEFT, input->keybit);
0498 if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
0499 __set_bit(BTN_MIDDLE, input->keybit);
0500 if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
0501 __set_bit(BTN_RIGHT, input->keybit);
0502
0503 if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK)
0504 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
0505
0506
0507 error = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
0508 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
0509 if (error) {
0510 dev_err(dev, "failed to initialize MT slots: %d\n", error);
0511 return error;
0512 }
0513
0514
0515 error = input_register_device(input);
0516 if (error) {
0517 dev_err(dev, "failed to register input device: %d\n", error);
0518 return error;
0519 }
0520
0521 cyapa->input = input;
0522 return 0;
0523 }
0524
0525 static void cyapa_enable_irq_for_cmd(struct cyapa *cyapa)
0526 {
0527 struct input_dev *input = cyapa->input;
0528
0529 if (!input || !input_device_enabled(input)) {
0530
0531
0532
0533
0534
0535
0536 if (!input || cyapa->operational)
0537 cyapa->ops->set_power_mode(cyapa,
0538 PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
0539
0540 if (cyapa->gen >= CYAPA_GEN5)
0541 enable_irq(cyapa->client->irq);
0542 }
0543 }
0544
0545 static void cyapa_disable_irq_for_cmd(struct cyapa *cyapa)
0546 {
0547 struct input_dev *input = cyapa->input;
0548
0549 if (!input || !input_device_enabled(input)) {
0550 if (cyapa->gen >= CYAPA_GEN5)
0551 disable_irq(cyapa->client->irq);
0552 if (!input || cyapa->operational)
0553 cyapa->ops->set_power_mode(cyapa,
0554 PWR_MODE_OFF, 0, CYAPA_PM_ACTIVE);
0555 }
0556 }
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572 u8 cyapa_sleep_time_to_pwr_cmd(u16 sleep_time)
0573 {
0574 u16 encoded_time;
0575
0576 sleep_time = clamp_val(sleep_time, 20, 1000);
0577 encoded_time = sleep_time < 100 ? sleep_time / 10 : sleep_time / 20 + 5;
0578 return (encoded_time << 2) & PWR_MODE_MASK;
0579 }
0580
0581 u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode)
0582 {
0583 u8 encoded_time = pwr_mode >> 2;
0584
0585 return (encoded_time < 10) ? encoded_time * 10
0586 : (encoded_time - 5) * 20;
0587 }
0588
0589
0590 static int cyapa_initialize(struct cyapa *cyapa)
0591 {
0592 int error = 0;
0593
0594 cyapa->state = CYAPA_STATE_NO_DEVICE;
0595 cyapa->gen = CYAPA_GEN_UNKNOWN;
0596 mutex_init(&cyapa->state_sync_lock);
0597
0598
0599
0600
0601
0602 cyapa->suspend_power_mode = PWR_MODE_SLEEP;
0603 cyapa->suspend_sleep_time =
0604 cyapa_pwr_cmd_to_sleep_time(cyapa->suspend_power_mode);
0605
0606
0607 error = cyapa_gen3_ops.initialize(cyapa);
0608 if (!error)
0609 error = cyapa_gen5_ops.initialize(cyapa);
0610 if (!error)
0611 error = cyapa_gen6_ops.initialize(cyapa);
0612 if (error)
0613 return error;
0614
0615 error = cyapa_detect(cyapa);
0616 if (error)
0617 return error;
0618
0619
0620 if (cyapa->operational)
0621 cyapa->ops->set_power_mode(cyapa,
0622 PWR_MODE_OFF, 0, CYAPA_PM_ACTIVE);
0623
0624 return 0;
0625 }
0626
0627 static int cyapa_reinitialize(struct cyapa *cyapa)
0628 {
0629 struct device *dev = &cyapa->client->dev;
0630 struct input_dev *input = cyapa->input;
0631 int error;
0632
0633 if (pm_runtime_enabled(dev))
0634 pm_runtime_disable(dev);
0635
0636
0637 if (cyapa->operational)
0638 cyapa->ops->set_power_mode(cyapa,
0639 PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_ACTIVE);
0640
0641 error = cyapa_detect(cyapa);
0642 if (error)
0643 goto out;
0644
0645 if (!input && cyapa->operational) {
0646 error = cyapa_create_input_dev(cyapa);
0647 if (error) {
0648 dev_err(dev, "create input_dev instance failed: %d\n",
0649 error);
0650 goto out;
0651 }
0652 }
0653
0654 out:
0655 if (!input || !input_device_enabled(input)) {
0656
0657 if (cyapa->operational)
0658 cyapa->ops->set_power_mode(cyapa,
0659 PWR_MODE_OFF, 0, CYAPA_PM_DEACTIVE);
0660 } else if (!error && cyapa->operational) {
0661
0662
0663
0664
0665 pm_runtime_set_active(dev);
0666 pm_runtime_enable(dev);
0667
0668 pm_runtime_get_sync(dev);
0669 pm_runtime_mark_last_busy(dev);
0670 pm_runtime_put_sync_autosuspend(dev);
0671 }
0672
0673 return error;
0674 }
0675
0676 static irqreturn_t cyapa_irq(int irq, void *dev_id)
0677 {
0678 struct cyapa *cyapa = dev_id;
0679 struct device *dev = &cyapa->client->dev;
0680 int error;
0681
0682 if (device_may_wakeup(dev))
0683 pm_wakeup_event(dev, 0);
0684
0685
0686 if (cyapa->ops->irq_cmd_handler(cyapa)) {
0687
0688
0689
0690 if (!cyapa->input) {
0691
0692
0693
0694
0695 cyapa->ops->sort_empty_output_data(cyapa,
0696 NULL, NULL, NULL);
0697 goto out;
0698 }
0699
0700 if (cyapa->operational) {
0701 error = cyapa->ops->irq_handler(cyapa);
0702
0703
0704
0705
0706
0707
0708
0709
0710
0711
0712 pm_runtime_get_sync(dev);
0713 pm_runtime_mark_last_busy(dev);
0714 pm_runtime_put_sync_autosuspend(dev);
0715 }
0716
0717 if (!cyapa->operational || error) {
0718 if (!mutex_trylock(&cyapa->state_sync_lock)) {
0719 cyapa->ops->sort_empty_output_data(cyapa,
0720 NULL, NULL, NULL);
0721 goto out;
0722 }
0723 cyapa_reinitialize(cyapa);
0724 mutex_unlock(&cyapa->state_sync_lock);
0725 }
0726 }
0727
0728 out:
0729 return IRQ_HANDLED;
0730 }
0731
0732
0733
0734
0735
0736
0737 #ifdef CONFIG_PM_SLEEP
0738 static ssize_t cyapa_show_suspend_scanrate(struct device *dev,
0739 struct device_attribute *attr,
0740 char *buf)
0741 {
0742 struct cyapa *cyapa = dev_get_drvdata(dev);
0743 u8 pwr_cmd;
0744 u16 sleep_time;
0745 int len;
0746 int error;
0747
0748 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
0749 if (error)
0750 return error;
0751
0752 pwr_cmd = cyapa->suspend_power_mode;
0753 sleep_time = cyapa->suspend_sleep_time;
0754
0755 mutex_unlock(&cyapa->state_sync_lock);
0756
0757 switch (pwr_cmd) {
0758 case PWR_MODE_BTN_ONLY:
0759 len = scnprintf(buf, PAGE_SIZE, "%s\n", BTN_ONLY_MODE_NAME);
0760 break;
0761
0762 case PWR_MODE_OFF:
0763 len = scnprintf(buf, PAGE_SIZE, "%s\n", OFF_MODE_NAME);
0764 break;
0765
0766 default:
0767 len = scnprintf(buf, PAGE_SIZE, "%u\n",
0768 cyapa->gen == CYAPA_GEN3 ?
0769 cyapa_pwr_cmd_to_sleep_time(pwr_cmd) :
0770 sleep_time);
0771 break;
0772 }
0773
0774 return len;
0775 }
0776
0777 static ssize_t cyapa_update_suspend_scanrate(struct device *dev,
0778 struct device_attribute *attr,
0779 const char *buf, size_t count)
0780 {
0781 struct cyapa *cyapa = dev_get_drvdata(dev);
0782 u16 sleep_time;
0783 int error;
0784
0785 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
0786 if (error)
0787 return error;
0788
0789 if (sysfs_streq(buf, BTN_ONLY_MODE_NAME)) {
0790 cyapa->suspend_power_mode = PWR_MODE_BTN_ONLY;
0791 } else if (sysfs_streq(buf, OFF_MODE_NAME)) {
0792 cyapa->suspend_power_mode = PWR_MODE_OFF;
0793 } else if (!kstrtou16(buf, 10, &sleep_time)) {
0794 cyapa->suspend_sleep_time = min_t(u16, sleep_time, 1000);
0795 cyapa->suspend_power_mode =
0796 cyapa_sleep_time_to_pwr_cmd(cyapa->suspend_sleep_time);
0797 } else {
0798 count = -EINVAL;
0799 }
0800
0801 mutex_unlock(&cyapa->state_sync_lock);
0802
0803 return count;
0804 }
0805
0806 static DEVICE_ATTR(suspend_scanrate_ms, S_IRUGO|S_IWUSR,
0807 cyapa_show_suspend_scanrate,
0808 cyapa_update_suspend_scanrate);
0809
0810 static struct attribute *cyapa_power_wakeup_entries[] = {
0811 &dev_attr_suspend_scanrate_ms.attr,
0812 NULL,
0813 };
0814
0815 static const struct attribute_group cyapa_power_wakeup_group = {
0816 .name = power_group_name,
0817 .attrs = cyapa_power_wakeup_entries,
0818 };
0819
0820 static void cyapa_remove_power_wakeup_group(void *data)
0821 {
0822 struct cyapa *cyapa = data;
0823
0824 sysfs_unmerge_group(&cyapa->client->dev.kobj,
0825 &cyapa_power_wakeup_group);
0826 }
0827
0828 static int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
0829 {
0830 struct i2c_client *client = cyapa->client;
0831 struct device *dev = &client->dev;
0832 int error;
0833
0834 if (device_can_wakeup(dev)) {
0835 error = sysfs_merge_group(&dev->kobj,
0836 &cyapa_power_wakeup_group);
0837 if (error) {
0838 dev_err(dev, "failed to add power wakeup group: %d\n",
0839 error);
0840 return error;
0841 }
0842
0843 error = devm_add_action_or_reset(dev,
0844 cyapa_remove_power_wakeup_group, cyapa);
0845 if (error) {
0846 dev_err(dev, "failed to add power cleanup action: %d\n",
0847 error);
0848 return error;
0849 }
0850 }
0851
0852 return 0;
0853 }
0854 #else
0855 static inline int cyapa_prepare_wakeup_controls(struct cyapa *cyapa)
0856 {
0857 return 0;
0858 }
0859 #endif
0860
0861 #ifdef CONFIG_PM
0862 static ssize_t cyapa_show_rt_suspend_scanrate(struct device *dev,
0863 struct device_attribute *attr,
0864 char *buf)
0865 {
0866 struct cyapa *cyapa = dev_get_drvdata(dev);
0867 u8 pwr_cmd;
0868 u16 sleep_time;
0869 int error;
0870
0871 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
0872 if (error)
0873 return error;
0874
0875 pwr_cmd = cyapa->runtime_suspend_power_mode;
0876 sleep_time = cyapa->runtime_suspend_sleep_time;
0877
0878 mutex_unlock(&cyapa->state_sync_lock);
0879
0880 return scnprintf(buf, PAGE_SIZE, "%u\n",
0881 cyapa->gen == CYAPA_GEN3 ?
0882 cyapa_pwr_cmd_to_sleep_time(pwr_cmd) :
0883 sleep_time);
0884 }
0885
0886 static ssize_t cyapa_update_rt_suspend_scanrate(struct device *dev,
0887 struct device_attribute *attr,
0888 const char *buf, size_t count)
0889 {
0890 struct cyapa *cyapa = dev_get_drvdata(dev);
0891 u16 time;
0892 int error;
0893
0894 if (buf == NULL || count == 0 || kstrtou16(buf, 10, &time)) {
0895 dev_err(dev, "invalid runtime suspend scanrate ms parameter\n");
0896 return -EINVAL;
0897 }
0898
0899
0900
0901
0902
0903
0904 pm_runtime_get_sync(dev);
0905
0906 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
0907 if (error)
0908 return error;
0909
0910 cyapa->runtime_suspend_sleep_time = min_t(u16, time, 1000);
0911 cyapa->runtime_suspend_power_mode =
0912 cyapa_sleep_time_to_pwr_cmd(cyapa->runtime_suspend_sleep_time);
0913
0914 mutex_unlock(&cyapa->state_sync_lock);
0915
0916 pm_runtime_put_sync_autosuspend(dev);
0917
0918 return count;
0919 }
0920
0921 static DEVICE_ATTR(runtime_suspend_scanrate_ms, S_IRUGO|S_IWUSR,
0922 cyapa_show_rt_suspend_scanrate,
0923 cyapa_update_rt_suspend_scanrate);
0924
0925 static struct attribute *cyapa_power_runtime_entries[] = {
0926 &dev_attr_runtime_suspend_scanrate_ms.attr,
0927 NULL,
0928 };
0929
0930 static const struct attribute_group cyapa_power_runtime_group = {
0931 .name = power_group_name,
0932 .attrs = cyapa_power_runtime_entries,
0933 };
0934
0935 static void cyapa_remove_power_runtime_group(void *data)
0936 {
0937 struct cyapa *cyapa = data;
0938
0939 sysfs_unmerge_group(&cyapa->client->dev.kobj,
0940 &cyapa_power_runtime_group);
0941 }
0942
0943 static int cyapa_start_runtime(struct cyapa *cyapa)
0944 {
0945 struct device *dev = &cyapa->client->dev;
0946 int error;
0947
0948 cyapa->runtime_suspend_power_mode = PWR_MODE_IDLE;
0949 cyapa->runtime_suspend_sleep_time =
0950 cyapa_pwr_cmd_to_sleep_time(cyapa->runtime_suspend_power_mode);
0951
0952 error = sysfs_merge_group(&dev->kobj, &cyapa_power_runtime_group);
0953 if (error) {
0954 dev_err(dev,
0955 "failed to create power runtime group: %d\n", error);
0956 return error;
0957 }
0958
0959 error = devm_add_action_or_reset(dev, cyapa_remove_power_runtime_group,
0960 cyapa);
0961 if (error) {
0962 dev_err(dev,
0963 "failed to add power runtime cleanup action: %d\n",
0964 error);
0965 return error;
0966 }
0967
0968
0969 pm_runtime_set_suspended(dev);
0970 pm_runtime_use_autosuspend(dev);
0971 pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_DELAY);
0972
0973 return 0;
0974 }
0975 #else
0976 static inline int cyapa_start_runtime(struct cyapa *cyapa)
0977 {
0978 return 0;
0979 }
0980 #endif
0981
0982 static ssize_t cyapa_show_fm_ver(struct device *dev,
0983 struct device_attribute *attr, char *buf)
0984 {
0985 int error;
0986 struct cyapa *cyapa = dev_get_drvdata(dev);
0987
0988 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
0989 if (error)
0990 return error;
0991 error = scnprintf(buf, PAGE_SIZE, "%d.%d\n", cyapa->fw_maj_ver,
0992 cyapa->fw_min_ver);
0993 mutex_unlock(&cyapa->state_sync_lock);
0994 return error;
0995 }
0996
0997 static ssize_t cyapa_show_product_id(struct device *dev,
0998 struct device_attribute *attr, char *buf)
0999 {
1000 struct cyapa *cyapa = dev_get_drvdata(dev);
1001 int size;
1002 int error;
1003
1004 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1005 if (error)
1006 return error;
1007 size = scnprintf(buf, PAGE_SIZE, "%s\n", cyapa->product_id);
1008 mutex_unlock(&cyapa->state_sync_lock);
1009 return size;
1010 }
1011
1012 static int cyapa_firmware(struct cyapa *cyapa, const char *fw_name)
1013 {
1014 struct device *dev = &cyapa->client->dev;
1015 const struct firmware *fw;
1016 int error;
1017
1018 error = request_firmware(&fw, fw_name, dev);
1019 if (error) {
1020 dev_err(dev, "Could not load firmware from %s: %d\n",
1021 fw_name, error);
1022 return error;
1023 }
1024
1025 error = cyapa->ops->check_fw(cyapa, fw);
1026 if (error) {
1027 dev_err(dev, "Invalid CYAPA firmware image: %s\n",
1028 fw_name);
1029 goto done;
1030 }
1031
1032
1033
1034
1035
1036
1037 pm_runtime_get_sync(dev);
1038
1039
1040 cyapa_enable_irq_for_cmd(cyapa);
1041
1042 error = cyapa->ops->bl_enter(cyapa);
1043 if (error) {
1044 dev_err(dev, "bl_enter failed, %d\n", error);
1045 goto err_detect;
1046 }
1047
1048 error = cyapa->ops->bl_activate(cyapa);
1049 if (error) {
1050 dev_err(dev, "bl_activate failed, %d\n", error);
1051 goto err_detect;
1052 }
1053
1054 error = cyapa->ops->bl_initiate(cyapa, fw);
1055 if (error) {
1056 dev_err(dev, "bl_initiate failed, %d\n", error);
1057 goto err_detect;
1058 }
1059
1060 error = cyapa->ops->update_fw(cyapa, fw);
1061 if (error) {
1062 dev_err(dev, "update_fw failed, %d\n", error);
1063 goto err_detect;
1064 }
1065
1066 err_detect:
1067 cyapa_disable_irq_for_cmd(cyapa);
1068 pm_runtime_put_noidle(dev);
1069
1070 done:
1071 release_firmware(fw);
1072 return error;
1073 }
1074
1075 static ssize_t cyapa_update_fw_store(struct device *dev,
1076 struct device_attribute *attr,
1077 const char *buf, size_t count)
1078 {
1079 struct cyapa *cyapa = dev_get_drvdata(dev);
1080 char fw_name[NAME_MAX];
1081 int ret, error;
1082
1083 if (count >= NAME_MAX) {
1084 dev_err(dev, "File name too long\n");
1085 return -EINVAL;
1086 }
1087
1088 memcpy(fw_name, buf, count);
1089 if (fw_name[count - 1] == '\n')
1090 fw_name[count - 1] = '\0';
1091 else
1092 fw_name[count] = '\0';
1093
1094 if (cyapa->input) {
1095
1096
1097
1098
1099
1100 input_unregister_device(cyapa->input);
1101 cyapa->input = NULL;
1102 }
1103
1104 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1105 if (error) {
1106
1107
1108
1109
1110 cyapa_reinitialize(cyapa);
1111 return error;
1112 }
1113
1114 error = cyapa_firmware(cyapa, fw_name);
1115 if (error)
1116 dev_err(dev, "firmware update failed: %d\n", error);
1117 else
1118 dev_dbg(dev, "firmware update successfully done.\n");
1119
1120
1121
1122
1123
1124 ret = cyapa_reinitialize(cyapa);
1125 if (ret) {
1126 dev_err(dev, "failed to re-detect after updated: %d\n", ret);
1127 error = error ? error : ret;
1128 }
1129
1130 mutex_unlock(&cyapa->state_sync_lock);
1131
1132 return error ? error : count;
1133 }
1134
1135 static ssize_t cyapa_calibrate_store(struct device *dev,
1136 struct device_attribute *attr,
1137 const char *buf, size_t count)
1138 {
1139 struct cyapa *cyapa = dev_get_drvdata(dev);
1140 int error;
1141
1142 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1143 if (error)
1144 return error;
1145
1146 if (cyapa->operational) {
1147 cyapa_enable_irq_for_cmd(cyapa);
1148 error = cyapa->ops->calibrate_store(dev, attr, buf, count);
1149 cyapa_disable_irq_for_cmd(cyapa);
1150 } else {
1151 error = -EBUSY;
1152 }
1153
1154 mutex_unlock(&cyapa->state_sync_lock);
1155 return error < 0 ? error : count;
1156 }
1157
1158 static ssize_t cyapa_show_baseline(struct device *dev,
1159 struct device_attribute *attr, char *buf)
1160 {
1161 struct cyapa *cyapa = dev_get_drvdata(dev);
1162 ssize_t error;
1163
1164 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1165 if (error)
1166 return error;
1167
1168 if (cyapa->operational) {
1169 cyapa_enable_irq_for_cmd(cyapa);
1170 error = cyapa->ops->show_baseline(dev, attr, buf);
1171 cyapa_disable_irq_for_cmd(cyapa);
1172 } else {
1173 error = -EBUSY;
1174 }
1175
1176 mutex_unlock(&cyapa->state_sync_lock);
1177 return error;
1178 }
1179
1180 static char *cyapa_state_to_string(struct cyapa *cyapa)
1181 {
1182 switch (cyapa->state) {
1183 case CYAPA_STATE_BL_BUSY:
1184 return "bootloader busy";
1185 case CYAPA_STATE_BL_IDLE:
1186 return "bootloader idle";
1187 case CYAPA_STATE_BL_ACTIVE:
1188 return "bootloader active";
1189 case CYAPA_STATE_GEN5_BL:
1190 case CYAPA_STATE_GEN6_BL:
1191 return "bootloader";
1192 case CYAPA_STATE_OP:
1193 case CYAPA_STATE_GEN5_APP:
1194 case CYAPA_STATE_GEN6_APP:
1195 return "operational";
1196 default:
1197 return "invalid mode";
1198 }
1199 }
1200
1201 static ssize_t cyapa_show_mode(struct device *dev,
1202 struct device_attribute *attr, char *buf)
1203 {
1204 struct cyapa *cyapa = dev_get_drvdata(dev);
1205 int size;
1206 int error;
1207
1208 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1209 if (error)
1210 return error;
1211
1212 size = scnprintf(buf, PAGE_SIZE, "gen%d %s\n",
1213 cyapa->gen, cyapa_state_to_string(cyapa));
1214
1215 mutex_unlock(&cyapa->state_sync_lock);
1216 return size;
1217 }
1218
1219 static DEVICE_ATTR(firmware_version, S_IRUGO, cyapa_show_fm_ver, NULL);
1220 static DEVICE_ATTR(product_id, S_IRUGO, cyapa_show_product_id, NULL);
1221 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, cyapa_update_fw_store);
1222 static DEVICE_ATTR(baseline, S_IRUGO, cyapa_show_baseline, NULL);
1223 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, cyapa_calibrate_store);
1224 static DEVICE_ATTR(mode, S_IRUGO, cyapa_show_mode, NULL);
1225
1226 static struct attribute *cyapa_sysfs_entries[] = {
1227 &dev_attr_firmware_version.attr,
1228 &dev_attr_product_id.attr,
1229 &dev_attr_update_fw.attr,
1230 &dev_attr_baseline.attr,
1231 &dev_attr_calibrate.attr,
1232 &dev_attr_mode.attr,
1233 NULL,
1234 };
1235
1236 static const struct attribute_group cyapa_sysfs_group = {
1237 .attrs = cyapa_sysfs_entries,
1238 };
1239
1240 static void cyapa_disable_regulator(void *data)
1241 {
1242 struct cyapa *cyapa = data;
1243
1244 regulator_disable(cyapa->vcc);
1245 }
1246
1247 static int cyapa_probe(struct i2c_client *client,
1248 const struct i2c_device_id *dev_id)
1249 {
1250 struct device *dev = &client->dev;
1251 struct cyapa *cyapa;
1252 u8 adapter_func;
1253 union i2c_smbus_data dummy;
1254 int error;
1255
1256 adapter_func = cyapa_check_adapter_functionality(client);
1257 if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
1258 dev_err(dev, "not a supported I2C/SMBus adapter\n");
1259 return -EIO;
1260 }
1261
1262
1263 if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1264 I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0)
1265 return -ENODEV;
1266
1267 cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
1268 if (!cyapa)
1269 return -ENOMEM;
1270
1271
1272 if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
1273 cyapa->smbus = true;
1274
1275 cyapa->client = client;
1276 i2c_set_clientdata(client, cyapa);
1277 sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
1278 client->addr);
1279
1280 cyapa->vcc = devm_regulator_get(dev, "vcc");
1281 if (IS_ERR(cyapa->vcc)) {
1282 error = PTR_ERR(cyapa->vcc);
1283 dev_err(dev, "failed to get vcc regulator: %d\n", error);
1284 return error;
1285 }
1286
1287 error = regulator_enable(cyapa->vcc);
1288 if (error) {
1289 dev_err(dev, "failed to enable regulator: %d\n", error);
1290 return error;
1291 }
1292
1293 error = devm_add_action_or_reset(dev, cyapa_disable_regulator, cyapa);
1294 if (error) {
1295 dev_err(dev, "failed to add disable regulator action: %d\n",
1296 error);
1297 return error;
1298 }
1299
1300 error = cyapa_initialize(cyapa);
1301 if (error) {
1302 dev_err(dev, "failed to detect and initialize tp device.\n");
1303 return error;
1304 }
1305
1306 error = devm_device_add_group(dev, &cyapa_sysfs_group);
1307 if (error) {
1308 dev_err(dev, "failed to create sysfs entries: %d\n", error);
1309 return error;
1310 }
1311
1312 error = cyapa_prepare_wakeup_controls(cyapa);
1313 if (error) {
1314 dev_err(dev, "failed to prepare wakeup controls: %d\n", error);
1315 return error;
1316 }
1317
1318 error = cyapa_start_runtime(cyapa);
1319 if (error) {
1320 dev_err(dev, "failed to start pm_runtime: %d\n", error);
1321 return error;
1322 }
1323
1324 error = devm_request_threaded_irq(dev, client->irq,
1325 NULL, cyapa_irq,
1326 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1327 "cyapa", cyapa);
1328 if (error) {
1329 dev_err(dev, "failed to request threaded irq: %d\n", error);
1330 return error;
1331 }
1332
1333
1334 disable_irq(client->irq);
1335
1336
1337
1338
1339
1340
1341 if (cyapa->operational) {
1342 error = cyapa_create_input_dev(cyapa);
1343 if (error) {
1344 dev_err(dev, "create input_dev instance failed: %d\n",
1345 error);
1346 return error;
1347 }
1348 }
1349
1350 return 0;
1351 }
1352
1353 static int __maybe_unused cyapa_suspend(struct device *dev)
1354 {
1355 struct i2c_client *client = to_i2c_client(dev);
1356 struct cyapa *cyapa = i2c_get_clientdata(client);
1357 u8 power_mode;
1358 int error;
1359
1360 error = mutex_lock_interruptible(&cyapa->state_sync_lock);
1361 if (error)
1362 return error;
1363
1364
1365
1366
1367
1368
1369 if (pm_runtime_enabled(dev))
1370 pm_runtime_disable(dev);
1371 disable_irq(client->irq);
1372
1373
1374
1375
1376
1377 if (cyapa->operational) {
1378 power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode
1379 : PWR_MODE_OFF;
1380 error = cyapa->ops->set_power_mode(cyapa, power_mode,
1381 cyapa->suspend_sleep_time, CYAPA_PM_SUSPEND);
1382 if (error)
1383 dev_err(dev, "suspend set power mode failed: %d\n",
1384 error);
1385 }
1386
1387
1388
1389
1390
1391 if (cyapa->dev_pwr_mode != PWR_MODE_OFF)
1392 cyapa->ops->set_proximity(cyapa, false);
1393
1394 if (device_may_wakeup(dev))
1395 cyapa->irq_wake = (enable_irq_wake(client->irq) == 0);
1396
1397 mutex_unlock(&cyapa->state_sync_lock);
1398 return 0;
1399 }
1400
1401 static int __maybe_unused cyapa_resume(struct device *dev)
1402 {
1403 struct i2c_client *client = to_i2c_client(dev);
1404 struct cyapa *cyapa = i2c_get_clientdata(client);
1405 int error;
1406
1407 mutex_lock(&cyapa->state_sync_lock);
1408
1409 if (device_may_wakeup(dev) && cyapa->irq_wake) {
1410 disable_irq_wake(client->irq);
1411 cyapa->irq_wake = false;
1412 }
1413
1414
1415
1416
1417
1418 error = cyapa_reinitialize(cyapa);
1419 if (error)
1420 dev_warn(dev, "failed to reinitialize TP device: %d\n", error);
1421
1422 enable_irq(client->irq);
1423
1424 mutex_unlock(&cyapa->state_sync_lock);
1425 return 0;
1426 }
1427
1428 static int __maybe_unused cyapa_runtime_suspend(struct device *dev)
1429 {
1430 struct cyapa *cyapa = dev_get_drvdata(dev);
1431 int error;
1432
1433 error = cyapa->ops->set_power_mode(cyapa,
1434 cyapa->runtime_suspend_power_mode,
1435 cyapa->runtime_suspend_sleep_time,
1436 CYAPA_PM_RUNTIME_SUSPEND);
1437 if (error)
1438 dev_warn(dev, "runtime suspend failed: %d\n", error);
1439
1440 return 0;
1441 }
1442
1443 static int __maybe_unused cyapa_runtime_resume(struct device *dev)
1444 {
1445 struct cyapa *cyapa = dev_get_drvdata(dev);
1446 int error;
1447
1448 error = cyapa->ops->set_power_mode(cyapa,
1449 PWR_MODE_FULL_ACTIVE, 0, CYAPA_PM_RUNTIME_RESUME);
1450 if (error)
1451 dev_warn(dev, "runtime resume failed: %d\n", error);
1452
1453 return 0;
1454 }
1455
1456 static const struct dev_pm_ops cyapa_pm_ops = {
1457 SET_SYSTEM_SLEEP_PM_OPS(cyapa_suspend, cyapa_resume)
1458 SET_RUNTIME_PM_OPS(cyapa_runtime_suspend, cyapa_runtime_resume, NULL)
1459 };
1460
1461 static const struct i2c_device_id cyapa_id_table[] = {
1462 { "cyapa", 0 },
1463 { },
1464 };
1465 MODULE_DEVICE_TABLE(i2c, cyapa_id_table);
1466
1467 #ifdef CONFIG_ACPI
1468 static const struct acpi_device_id cyapa_acpi_id[] = {
1469 { "CYAP0000", 0 },
1470 { "CYAP0001", 0 },
1471 { "CYAP0002", 0 },
1472 { }
1473 };
1474 MODULE_DEVICE_TABLE(acpi, cyapa_acpi_id);
1475 #endif
1476
1477 #ifdef CONFIG_OF
1478 static const struct of_device_id cyapa_of_match[] = {
1479 { .compatible = "cypress,cyapa" },
1480 { }
1481 };
1482 MODULE_DEVICE_TABLE(of, cyapa_of_match);
1483 #endif
1484
1485 static struct i2c_driver cyapa_driver = {
1486 .driver = {
1487 .name = "cyapa",
1488 .pm = &cyapa_pm_ops,
1489 .acpi_match_table = ACPI_PTR(cyapa_acpi_id),
1490 .of_match_table = of_match_ptr(cyapa_of_match),
1491 },
1492
1493 .probe = cyapa_probe,
1494 .id_table = cyapa_id_table,
1495 };
1496
1497 module_i2c_driver(cyapa_driver);
1498
1499 MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver");
1500 MODULE_AUTHOR("Dudley Du <dudl@cypress.com>");
1501 MODULE_LICENSE("GPL");