0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <linux/delay.h>
0017 #include <linux/input.h>
0018 #include <linux/input/mt.h>
0019 #include <linux/input/touchscreen.h>
0020 #include <linux/gpio.h>
0021 #include <linux/interrupt.h>
0022 #include <linux/slab.h>
0023 #include <linux/property.h>
0024 #include <linux/gpio/consumer.h>
0025 #include <linux/regulator/consumer.h>
0026
0027 #include "cyttsp_core.h"
0028
0029
0030 #define CY_NUM_BL_KEYS 8
0031
0032
0033 #define GET_NUM_TOUCHES(x) ((x) & 0x0F)
0034 #define IS_LARGE_AREA(x) (((x) & 0x10) >> 4)
0035 #define IS_BAD_PKT(x) ((x) & 0x20)
0036 #define IS_VALID_APP(x) ((x) & 0x01)
0037 #define IS_OPERATIONAL_ERR(x) ((x) & 0x3F)
0038 #define GET_HSTMODE(reg) (((reg) & 0x70) >> 4)
0039 #define GET_BOOTLOADERMODE(reg) (((reg) & 0x10) >> 4)
0040
0041 #define CY_REG_BASE 0x00
0042 #define CY_REG_ACT_DIST 0x1E
0043 #define CY_REG_ACT_INTRVL 0x1D
0044 #define CY_REG_TCH_TMOUT (CY_REG_ACT_INTRVL + 1)
0045 #define CY_REG_LP_INTRVL (CY_REG_TCH_TMOUT + 1)
0046 #define CY_MAXZ 255
0047 #define CY_DELAY_DFLT 20
0048 #define CY_DELAY_MAX 500
0049
0050 #define CY_ACT_DIST_DFLT 0xF8
0051 #define CY_ACT_DIST_MASK 0x0F
0052
0053 #define CY_ACT_INTRVL_DFLT 0x00
0054
0055 #define CY_LP_INTRVL_DFLT 0x0A
0056
0057 #define CY_TCH_TMOUT_DFLT 0xFF
0058 #define CY_HNDSHK_BIT 0x80
0059
0060 #define CY_OPERATE_MODE 0x00
0061 #define CY_SYSINFO_MODE 0x10
0062
0063 #define CY_SOFT_RESET_MODE 0x01
0064 #define CY_DEEP_SLEEP_MODE 0x02
0065 #define CY_LOW_POWER_MODE 0x04
0066
0067
0068 #define CY_MAX_FINGER 4
0069 #define CY_MAX_ID 16
0070
0071 static const u8 bl_command[] = {
0072 0x00,
0073 0xFF,
0074 0xA5,
0075 0, 1, 2, 3, 4, 5, 6, 7
0076 };
0077
0078 static int ttsp_read_block_data(struct cyttsp *ts, u8 command,
0079 u8 length, void *buf)
0080 {
0081 int error;
0082 int tries;
0083
0084 for (tries = 0; tries < CY_NUM_RETRY; tries++) {
0085 error = ts->bus_ops->read(ts->dev, ts->xfer_buf, command,
0086 length, buf);
0087 if (!error)
0088 return 0;
0089
0090 msleep(CY_DELAY_DFLT);
0091 }
0092
0093 return -EIO;
0094 }
0095
0096 static int ttsp_write_block_data(struct cyttsp *ts, u8 command,
0097 u8 length, void *buf)
0098 {
0099 int error;
0100 int tries;
0101
0102 for (tries = 0; tries < CY_NUM_RETRY; tries++) {
0103 error = ts->bus_ops->write(ts->dev, ts->xfer_buf, command,
0104 length, buf);
0105 if (!error)
0106 return 0;
0107
0108 msleep(CY_DELAY_DFLT);
0109 }
0110
0111 return -EIO;
0112 }
0113
0114 static int ttsp_send_command(struct cyttsp *ts, u8 cmd)
0115 {
0116 return ttsp_write_block_data(ts, CY_REG_BASE, sizeof(cmd), &cmd);
0117 }
0118
0119 static int cyttsp_handshake(struct cyttsp *ts)
0120 {
0121 if (ts->use_hndshk)
0122 return ttsp_send_command(ts,
0123 ts->xy_data.hst_mode ^ CY_HNDSHK_BIT);
0124
0125 return 0;
0126 }
0127
0128 static int cyttsp_load_bl_regs(struct cyttsp *ts)
0129 {
0130 memset(&ts->bl_data, 0, sizeof(ts->bl_data));
0131 ts->bl_data.bl_status = 0x10;
0132
0133 return ttsp_read_block_data(ts, CY_REG_BASE,
0134 sizeof(ts->bl_data), &ts->bl_data);
0135 }
0136
0137 static int cyttsp_exit_bl_mode(struct cyttsp *ts)
0138 {
0139 int error;
0140 u8 bl_cmd[sizeof(bl_command)];
0141
0142 memcpy(bl_cmd, bl_command, sizeof(bl_command));
0143 if (ts->bl_keys)
0144 memcpy(&bl_cmd[sizeof(bl_command) - CY_NUM_BL_KEYS],
0145 ts->bl_keys, CY_NUM_BL_KEYS);
0146
0147 error = ttsp_write_block_data(ts, CY_REG_BASE,
0148 sizeof(bl_cmd), bl_cmd);
0149 if (error)
0150 return error;
0151
0152
0153 msleep(CY_DELAY_DFLT);
0154
0155 error = cyttsp_load_bl_regs(ts);
0156 if (error)
0157 return error;
0158
0159 if (GET_BOOTLOADERMODE(ts->bl_data.bl_status))
0160 return -EIO;
0161
0162 return 0;
0163 }
0164
0165 static int cyttsp_set_operational_mode(struct cyttsp *ts)
0166 {
0167 int error;
0168
0169 error = ttsp_send_command(ts, CY_OPERATE_MODE);
0170 if (error)
0171 return error;
0172
0173
0174 error = ttsp_read_block_data(ts, CY_REG_BASE,
0175 sizeof(ts->xy_data), &ts->xy_data);
0176 if (error)
0177 return error;
0178
0179 error = cyttsp_handshake(ts);
0180 if (error)
0181 return error;
0182
0183 return ts->xy_data.act_dist == CY_ACT_DIST_DFLT ? -EIO : 0;
0184 }
0185
0186 static int cyttsp_set_sysinfo_mode(struct cyttsp *ts)
0187 {
0188 int error;
0189
0190 memset(&ts->sysinfo_data, 0, sizeof(ts->sysinfo_data));
0191
0192
0193 error = ttsp_send_command(ts, CY_SYSINFO_MODE);
0194 if (error)
0195 return error;
0196
0197
0198 msleep(CY_DELAY_DFLT);
0199 error = ttsp_read_block_data(ts, CY_REG_BASE, sizeof(ts->sysinfo_data),
0200 &ts->sysinfo_data);
0201 if (error)
0202 return error;
0203
0204 error = cyttsp_handshake(ts);
0205 if (error)
0206 return error;
0207
0208 if (!ts->sysinfo_data.tts_verh && !ts->sysinfo_data.tts_verl)
0209 return -EIO;
0210
0211 return 0;
0212 }
0213
0214 static int cyttsp_set_sysinfo_regs(struct cyttsp *ts)
0215 {
0216 int retval = 0;
0217
0218 if (ts->act_intrvl != CY_ACT_INTRVL_DFLT ||
0219 ts->tch_tmout != CY_TCH_TMOUT_DFLT ||
0220 ts->lp_intrvl != CY_LP_INTRVL_DFLT) {
0221
0222 u8 intrvl_ray[] = {
0223 ts->act_intrvl,
0224 ts->tch_tmout,
0225 ts->lp_intrvl
0226 };
0227
0228
0229 retval = ttsp_write_block_data(ts, CY_REG_ACT_INTRVL,
0230 sizeof(intrvl_ray), intrvl_ray);
0231 msleep(CY_DELAY_DFLT);
0232 }
0233
0234 return retval;
0235 }
0236
0237 static void cyttsp_hard_reset(struct cyttsp *ts)
0238 {
0239 if (ts->reset_gpio) {
0240
0241
0242
0243
0244
0245
0246 gpiod_set_value_cansleep(ts->reset_gpio, 1);
0247 usleep_range(1000, 2000);
0248 gpiod_set_value_cansleep(ts->reset_gpio, 0);
0249 usleep_range(5000, 6000);
0250 }
0251 }
0252
0253 static int cyttsp_soft_reset(struct cyttsp *ts)
0254 {
0255 int retval;
0256
0257
0258 reinit_completion(&ts->bl_ready);
0259 ts->state = CY_BL_STATE;
0260
0261 enable_irq(ts->irq);
0262
0263 retval = ttsp_send_command(ts, CY_SOFT_RESET_MODE);
0264 if (retval) {
0265 dev_err(ts->dev, "failed to send soft reset\n");
0266 goto out;
0267 }
0268
0269 if (!wait_for_completion_timeout(&ts->bl_ready,
0270 msecs_to_jiffies(CY_DELAY_DFLT * CY_DELAY_MAX))) {
0271 dev_err(ts->dev, "timeout waiting for soft reset\n");
0272 retval = -EIO;
0273 }
0274
0275 out:
0276 ts->state = CY_IDLE_STATE;
0277 disable_irq(ts->irq);
0278 return retval;
0279 }
0280
0281 static int cyttsp_act_dist_setup(struct cyttsp *ts)
0282 {
0283 u8 act_dist_setup = ts->act_dist;
0284
0285
0286 return ttsp_write_block_data(ts, CY_REG_ACT_DIST,
0287 sizeof(act_dist_setup), &act_dist_setup);
0288 }
0289
0290 static void cyttsp_extract_track_ids(struct cyttsp_xydata *xy_data, int *ids)
0291 {
0292 ids[0] = xy_data->touch12_id >> 4;
0293 ids[1] = xy_data->touch12_id & 0xF;
0294 ids[2] = xy_data->touch34_id >> 4;
0295 ids[3] = xy_data->touch34_id & 0xF;
0296 }
0297
0298 static const struct cyttsp_tch *cyttsp_get_tch(struct cyttsp_xydata *xy_data,
0299 int idx)
0300 {
0301 switch (idx) {
0302 case 0:
0303 return &xy_data->tch1;
0304 case 1:
0305 return &xy_data->tch2;
0306 case 2:
0307 return &xy_data->tch3;
0308 case 3:
0309 return &xy_data->tch4;
0310 default:
0311 return NULL;
0312 }
0313 }
0314
0315 static void cyttsp_report_tchdata(struct cyttsp *ts)
0316 {
0317 struct cyttsp_xydata *xy_data = &ts->xy_data;
0318 struct input_dev *input = ts->input;
0319 int num_tch = GET_NUM_TOUCHES(xy_data->tt_stat);
0320 const struct cyttsp_tch *tch;
0321 int ids[CY_MAX_ID];
0322 int i;
0323 DECLARE_BITMAP(used, CY_MAX_ID);
0324
0325 if (IS_LARGE_AREA(xy_data->tt_stat) == 1) {
0326
0327 num_tch = 0;
0328 dev_dbg(ts->dev, "%s: Large area detected\n", __func__);
0329 } else if (num_tch > CY_MAX_FINGER) {
0330
0331 num_tch = 0;
0332 dev_dbg(ts->dev, "%s: Num touch error detected\n", __func__);
0333 } else if (IS_BAD_PKT(xy_data->tt_mode)) {
0334
0335 num_tch = 0;
0336 dev_dbg(ts->dev, "%s: Invalid buffer detected\n", __func__);
0337 }
0338
0339 cyttsp_extract_track_ids(xy_data, ids);
0340
0341 bitmap_zero(used, CY_MAX_ID);
0342
0343 for (i = 0; i < num_tch; i++) {
0344 tch = cyttsp_get_tch(xy_data, i);
0345
0346 input_mt_slot(input, ids[i]);
0347 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
0348 input_report_abs(input, ABS_MT_POSITION_X, be16_to_cpu(tch->x));
0349 input_report_abs(input, ABS_MT_POSITION_Y, be16_to_cpu(tch->y));
0350 input_report_abs(input, ABS_MT_TOUCH_MAJOR, tch->z);
0351
0352 __set_bit(ids[i], used);
0353 }
0354
0355 for (i = 0; i < CY_MAX_ID; i++) {
0356 if (test_bit(i, used))
0357 continue;
0358
0359 input_mt_slot(input, i);
0360 input_mt_report_slot_inactive(input);
0361 }
0362
0363 input_sync(input);
0364 }
0365
0366 static irqreturn_t cyttsp_irq(int irq, void *handle)
0367 {
0368 struct cyttsp *ts = handle;
0369 int error;
0370
0371 if (unlikely(ts->state == CY_BL_STATE)) {
0372 complete(&ts->bl_ready);
0373 goto out;
0374 }
0375
0376
0377 error = ttsp_read_block_data(ts, CY_REG_BASE,
0378 sizeof(struct cyttsp_xydata), &ts->xy_data);
0379 if (error)
0380 goto out;
0381
0382
0383 error = cyttsp_handshake(ts);
0384 if (error)
0385 goto out;
0386
0387 if (unlikely(ts->state == CY_IDLE_STATE))
0388 goto out;
0389
0390 if (GET_BOOTLOADERMODE(ts->xy_data.tt_mode)) {
0391
0392
0393
0394
0395 error = cyttsp_exit_bl_mode(ts);
0396 if (error) {
0397 dev_err(ts->dev,
0398 "Could not return to operational mode, err: %d\n",
0399 error);
0400 ts->state = CY_IDLE_STATE;
0401 }
0402 } else {
0403 cyttsp_report_tchdata(ts);
0404 }
0405
0406 out:
0407 return IRQ_HANDLED;
0408 }
0409
0410 static int cyttsp_power_on(struct cyttsp *ts)
0411 {
0412 int error;
0413
0414 error = cyttsp_soft_reset(ts);
0415 if (error)
0416 return error;
0417
0418 error = cyttsp_load_bl_regs(ts);
0419 if (error)
0420 return error;
0421
0422 if (GET_BOOTLOADERMODE(ts->bl_data.bl_status) &&
0423 IS_VALID_APP(ts->bl_data.bl_status)) {
0424 error = cyttsp_exit_bl_mode(ts);
0425 if (error) {
0426 dev_err(ts->dev, "failed to exit bootloader mode\n");
0427 return error;
0428 }
0429 }
0430
0431 if (GET_HSTMODE(ts->bl_data.bl_file) != CY_OPERATE_MODE ||
0432 IS_OPERATIONAL_ERR(ts->bl_data.bl_status)) {
0433 return -ENODEV;
0434 }
0435
0436 error = cyttsp_set_sysinfo_mode(ts);
0437 if (error)
0438 return error;
0439
0440 error = cyttsp_set_sysinfo_regs(ts);
0441 if (error)
0442 return error;
0443
0444 error = cyttsp_set_operational_mode(ts);
0445 if (error)
0446 return error;
0447
0448
0449 error = cyttsp_act_dist_setup(ts);
0450 if (error)
0451 return error;
0452
0453 ts->state = CY_ACTIVE_STATE;
0454
0455 return 0;
0456 }
0457
0458 static int cyttsp_enable(struct cyttsp *ts)
0459 {
0460 int error;
0461
0462
0463
0464
0465
0466
0467
0468 error = ttsp_read_block_data(ts, CY_REG_BASE,
0469 sizeof(ts->xy_data), &ts->xy_data);
0470 if (error)
0471 return error;
0472
0473 if (GET_HSTMODE(ts->xy_data.hst_mode))
0474 return -EIO;
0475
0476 enable_irq(ts->irq);
0477
0478 return 0;
0479 }
0480
0481 static int cyttsp_disable(struct cyttsp *ts)
0482 {
0483 int error;
0484
0485 error = ttsp_send_command(ts, CY_LOW_POWER_MODE);
0486 if (error)
0487 return error;
0488
0489 disable_irq(ts->irq);
0490
0491 return 0;
0492 }
0493
0494 static int __maybe_unused cyttsp_suspend(struct device *dev)
0495 {
0496 struct cyttsp *ts = dev_get_drvdata(dev);
0497 int retval = 0;
0498
0499 mutex_lock(&ts->input->mutex);
0500
0501 if (input_device_enabled(ts->input)) {
0502 retval = cyttsp_disable(ts);
0503 if (retval == 0)
0504 ts->suspended = true;
0505 }
0506
0507 mutex_unlock(&ts->input->mutex);
0508
0509 return retval;
0510 }
0511
0512 static int __maybe_unused cyttsp_resume(struct device *dev)
0513 {
0514 struct cyttsp *ts = dev_get_drvdata(dev);
0515
0516 mutex_lock(&ts->input->mutex);
0517
0518 if (input_device_enabled(ts->input))
0519 cyttsp_enable(ts);
0520
0521 ts->suspended = false;
0522
0523 mutex_unlock(&ts->input->mutex);
0524
0525 return 0;
0526 }
0527
0528 SIMPLE_DEV_PM_OPS(cyttsp_pm_ops, cyttsp_suspend, cyttsp_resume);
0529 EXPORT_SYMBOL_GPL(cyttsp_pm_ops);
0530
0531 static int cyttsp_open(struct input_dev *dev)
0532 {
0533 struct cyttsp *ts = input_get_drvdata(dev);
0534 int retval = 0;
0535
0536 if (!ts->suspended)
0537 retval = cyttsp_enable(ts);
0538
0539 return retval;
0540 }
0541
0542 static void cyttsp_close(struct input_dev *dev)
0543 {
0544 struct cyttsp *ts = input_get_drvdata(dev);
0545
0546 if (!ts->suspended)
0547 cyttsp_disable(ts);
0548 }
0549
0550 static int cyttsp_parse_properties(struct cyttsp *ts)
0551 {
0552 struct device *dev = ts->dev;
0553 u32 dt_value;
0554 int ret;
0555
0556 ts->bl_keys = devm_kzalloc(dev, CY_NUM_BL_KEYS, GFP_KERNEL);
0557 if (!ts->bl_keys)
0558 return -ENOMEM;
0559
0560
0561 ts->use_hndshk = false;
0562 ts->act_dist = CY_ACT_DIST_DFLT;
0563 ts->act_intrvl = CY_ACT_INTRVL_DFLT;
0564 ts->tch_tmout = CY_TCH_TMOUT_DFLT;
0565 ts->lp_intrvl = CY_LP_INTRVL_DFLT;
0566
0567 ret = device_property_read_u8_array(dev, "bootloader-key",
0568 ts->bl_keys, CY_NUM_BL_KEYS);
0569 if (ret) {
0570 dev_err(dev,
0571 "bootloader-key property could not be retrieved\n");
0572 return ret;
0573 }
0574
0575 ts->use_hndshk = device_property_present(dev, "use-handshake");
0576
0577 if (!device_property_read_u32(dev, "active-distance", &dt_value)) {
0578 if (dt_value > 15) {
0579 dev_err(dev, "active-distance (%u) must be [0-15]\n",
0580 dt_value);
0581 return -EINVAL;
0582 }
0583 ts->act_dist &= ~CY_ACT_DIST_MASK;
0584 ts->act_dist |= dt_value;
0585 }
0586
0587 if (!device_property_read_u32(dev, "active-interval-ms", &dt_value)) {
0588 if (dt_value > 255) {
0589 dev_err(dev, "active-interval-ms (%u) must be [0-255]\n",
0590 dt_value);
0591 return -EINVAL;
0592 }
0593 ts->act_intrvl = dt_value;
0594 }
0595
0596 if (!device_property_read_u32(dev, "lowpower-interval-ms", &dt_value)) {
0597 if (dt_value > 2550) {
0598 dev_err(dev, "lowpower-interval-ms (%u) must be [0-2550]\n",
0599 dt_value);
0600 return -EINVAL;
0601 }
0602
0603 ts->lp_intrvl = dt_value / 10;
0604 }
0605
0606 if (!device_property_read_u32(dev, "touch-timeout-ms", &dt_value)) {
0607 if (dt_value > 2550) {
0608 dev_err(dev, "touch-timeout-ms (%u) must be [0-2550]\n",
0609 dt_value);
0610 return -EINVAL;
0611 }
0612
0613 ts->tch_tmout = dt_value / 10;
0614 }
0615
0616 return 0;
0617 }
0618
0619 static void cyttsp_disable_regulators(void *_ts)
0620 {
0621 struct cyttsp *ts = _ts;
0622
0623 regulator_bulk_disable(ARRAY_SIZE(ts->regulators),
0624 ts->regulators);
0625 }
0626
0627 struct cyttsp *cyttsp_probe(const struct cyttsp_bus_ops *bus_ops,
0628 struct device *dev, int irq, size_t xfer_buf_size)
0629 {
0630 struct cyttsp *ts;
0631 struct input_dev *input_dev;
0632 int error;
0633
0634 ts = devm_kzalloc(dev, sizeof(*ts) + xfer_buf_size, GFP_KERNEL);
0635 if (!ts)
0636 return ERR_PTR(-ENOMEM);
0637
0638 input_dev = devm_input_allocate_device(dev);
0639 if (!input_dev)
0640 return ERR_PTR(-ENOMEM);
0641
0642 ts->dev = dev;
0643 ts->input = input_dev;
0644 ts->bus_ops = bus_ops;
0645 ts->irq = irq;
0646
0647
0648
0649
0650
0651 ts->regulators[0].supply = "vcpin";
0652 ts->regulators[1].supply = "vdd";
0653 error = devm_regulator_bulk_get(dev, ARRAY_SIZE(ts->regulators),
0654 ts->regulators);
0655 if (error) {
0656 dev_err(dev, "Failed to get regulators: %d\n", error);
0657 return ERR_PTR(error);
0658 }
0659
0660 error = regulator_bulk_enable(ARRAY_SIZE(ts->regulators),
0661 ts->regulators);
0662 if (error) {
0663 dev_err(dev, "Cannot enable regulators: %d\n", error);
0664 return ERR_PTR(error);
0665 }
0666
0667 error = devm_add_action_or_reset(dev, cyttsp_disable_regulators, ts);
0668 if (error) {
0669 dev_err(dev, "failed to install chip disable handler\n");
0670 return ERR_PTR(error);
0671 }
0672
0673 ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
0674 if (IS_ERR(ts->reset_gpio)) {
0675 error = PTR_ERR(ts->reset_gpio);
0676 dev_err(dev, "Failed to request reset gpio, error %d\n", error);
0677 return ERR_PTR(error);
0678 }
0679
0680 error = cyttsp_parse_properties(ts);
0681 if (error)
0682 return ERR_PTR(error);
0683
0684 init_completion(&ts->bl_ready);
0685
0686 input_dev->name = "Cypress TTSP TouchScreen";
0687 input_dev->id.bustype = bus_ops->bustype;
0688 input_dev->dev.parent = ts->dev;
0689
0690 input_dev->open = cyttsp_open;
0691 input_dev->close = cyttsp_close;
0692
0693 input_set_drvdata(input_dev, ts);
0694
0695 input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
0696 input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
0697
0698 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
0699
0700 touchscreen_parse_properties(input_dev, true, NULL);
0701
0702 error = input_mt_init_slots(input_dev, CY_MAX_ID, INPUT_MT_DIRECT);
0703 if (error) {
0704 dev_err(dev, "Unable to init MT slots.\n");
0705 return ERR_PTR(error);
0706 }
0707
0708 error = devm_request_threaded_irq(dev, ts->irq, NULL, cyttsp_irq,
0709 IRQF_ONESHOT | IRQF_NO_AUTOEN,
0710 "cyttsp", ts);
0711 if (error) {
0712 dev_err(ts->dev, "failed to request IRQ %d, err: %d\n",
0713 ts->irq, error);
0714 return ERR_PTR(error);
0715 }
0716
0717 cyttsp_hard_reset(ts);
0718
0719 error = cyttsp_power_on(ts);
0720 if (error)
0721 return ERR_PTR(error);
0722
0723 error = input_register_device(input_dev);
0724 if (error) {
0725 dev_err(ts->dev, "failed to register input device: %d\n",
0726 error);
0727 return ERR_PTR(error);
0728 }
0729
0730 return ts;
0731 }
0732 EXPORT_SYMBOL_GPL(cyttsp_probe);
0733
0734 MODULE_LICENSE("GPL");
0735 MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen driver core");
0736 MODULE_AUTHOR("Cypress");