1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Core Source for: 4 * Cypress TrueTouch(TM) Standard Product (TTSP) touchscreen drivers. 5 * For use with Cypress Txx3xx parts. 6 * Supported parts include: 7 * CY8CTST341 8 * CY8CTMA340 9 * 10 * Copyright (C) 2009, 2010, 2011 Cypress Semiconductor, Inc. 11 * Copyright (C) 2012 Javier Martinez Canillas <javier@dowhile0.org> 12 * 13 * Contact Cypress Semiconductor at www.cypress.com <kev@cypress.com> 14 */ 15 16 #include <linux/delay.h> 17 #include <linux/input.h> 18 #include <linux/input/mt.h> 19 #include <linux/input/touchscreen.h> 20 #include <linux/gpio.h> 21 #include <linux/interrupt.h> 22 #include <linux/slab.h> 23 #include <linux/property.h> 24 #include <linux/gpio/consumer.h> 25 26 #include "cyttsp_core.h" 27 28 /* Bootloader number of command keys */ 29 #define CY_NUM_BL_KEYS 8 30 31 /* helpers */ 32 #define GET_NUM_TOUCHES(x) ((x) & 0x0F) 33 #define IS_LARGE_AREA(x) (((x) & 0x10) >> 4) 34 #define IS_BAD_PKT(x) ((x) & 0x20) 35 #define IS_VALID_APP(x) ((x) & 0x01) 36 #define IS_OPERATIONAL_ERR(x) ((x) & 0x3F) 37 #define GET_HSTMODE(reg) (((reg) & 0x70) >> 4) 38 #define GET_BOOTLOADERMODE(reg) (((reg) & 0x10) >> 4) 39 40 #define CY_REG_BASE 0x00 41 #define CY_REG_ACT_DIST 0x1E 42 #define CY_REG_ACT_INTRVL 0x1D 43 #define CY_REG_TCH_TMOUT (CY_REG_ACT_INTRVL + 1) 44 #define CY_REG_LP_INTRVL (CY_REG_TCH_TMOUT + 1) 45 #define CY_MAXZ 255 46 #define CY_DELAY_DFLT 20 /* ms */ 47 #define CY_DELAY_MAX 500 48 #define CY_ACT_DIST_DFLT 0xF8 49 #define CY_ACT_DIST_MASK 0x0F 50 #define CY_HNDSHK_BIT 0x80 51 /* device mode bits */ 52 #define CY_OPERATE_MODE 0x00 53 #define CY_SYSINFO_MODE 0x10 54 /* power mode select bits */ 55 #define CY_SOFT_RESET_MODE 0x01 /* return to Bootloader mode */ 56 #define CY_DEEP_SLEEP_MODE 0x02 57 #define CY_LOW_POWER_MODE 0x04 58 59 /* Slots management */ 60 #define CY_MAX_FINGER 4 61 #define CY_MAX_ID 16 62 63 static const u8 bl_command[] = { 64 0x00, /* file offset */ 65 0xFF, /* command */ 66 0xA5, /* exit bootloader command */ 67 0, 1, 2, 3, 4, 5, 6, 7 /* default keys */ 68 }; 69 70 static int ttsp_read_block_data(struct cyttsp *ts, u8 command, 71 u8 length, void *buf) 72 { 73 int error; 74 int tries; 75 76 for (tries = 0; tries < CY_NUM_RETRY; tries++) { 77 error = ts->bus_ops->read(ts->dev, ts->xfer_buf, command, 78 length, buf); 79 if (!error) 80 return 0; 81 82 msleep(CY_DELAY_DFLT); 83 } 84 85 return -EIO; 86 } 87 88 static int ttsp_write_block_data(struct cyttsp *ts, u8 command, 89 u8 length, void *buf) 90 { 91 int error; 92 int tries; 93 94 for (tries = 0; tries < CY_NUM_RETRY; tries++) { 95 error = ts->bus_ops->write(ts->dev, ts->xfer_buf, command, 96 length, buf); 97 if (!error) 98 return 0; 99 100 msleep(CY_DELAY_DFLT); 101 } 102 103 return -EIO; 104 } 105 106 static int ttsp_send_command(struct cyttsp *ts, u8 cmd) 107 { 108 return ttsp_write_block_data(ts, CY_REG_BASE, sizeof(cmd), &cmd); 109 } 110 111 static int cyttsp_handshake(struct cyttsp *ts) 112 { 113 if (ts->use_hndshk) 114 return ttsp_send_command(ts, 115 ts->xy_data.hst_mode ^ CY_HNDSHK_BIT); 116 117 return 0; 118 } 119 120 static int cyttsp_load_bl_regs(struct cyttsp *ts) 121 { 122 memset(&ts->bl_data, 0, sizeof(ts->bl_data)); 123 ts->bl_data.bl_status = 0x10; 124 125 return ttsp_read_block_data(ts, CY_REG_BASE, 126 sizeof(ts->bl_data), &ts->bl_data); 127 } 128 129 static int cyttsp_exit_bl_mode(struct cyttsp *ts) 130 { 131 int error; 132 u8 bl_cmd[sizeof(bl_command)]; 133 134 memcpy(bl_cmd, bl_command, sizeof(bl_command)); 135 if (ts->bl_keys) 136 memcpy(&bl_cmd[sizeof(bl_command) - CY_NUM_BL_KEYS], 137 ts->bl_keys, CY_NUM_BL_KEYS); 138 139 error = ttsp_write_block_data(ts, CY_REG_BASE, 140 sizeof(bl_cmd), bl_cmd); 141 if (error) 142 return error; 143 144 /* wait for TTSP Device to complete the operation */ 145 msleep(CY_DELAY_DFLT); 146 147 error = cyttsp_load_bl_regs(ts); 148 if (error) 149 return error; 150 151 if (GET_BOOTLOADERMODE(ts->bl_data.bl_status)) 152 return -EIO; 153 154 return 0; 155 } 156 157 static int cyttsp_set_operational_mode(struct cyttsp *ts) 158 { 159 int error; 160 161 error = ttsp_send_command(ts, CY_OPERATE_MODE); 162 if (error) 163 return error; 164 165 /* wait for TTSP Device to complete switch to Operational mode */ 166 error = ttsp_read_block_data(ts, CY_REG_BASE, 167 sizeof(ts->xy_data), &ts->xy_data); 168 if (error) 169 return error; 170 171 error = cyttsp_handshake(ts); 172 if (error) 173 return error; 174 175 return ts->xy_data.act_dist == CY_ACT_DIST_DFLT ? -EIO : 0; 176 } 177 178 static int cyttsp_set_sysinfo_mode(struct cyttsp *ts) 179 { 180 int error; 181 182 memset(&ts->sysinfo_data, 0, sizeof(ts->sysinfo_data)); 183 184 /* switch to sysinfo mode */ 185 error = ttsp_send_command(ts, CY_SYSINFO_MODE); 186 if (error) 187 return error; 188 189 /* read sysinfo registers */ 190 msleep(CY_DELAY_DFLT); 191 error = ttsp_read_block_data(ts, CY_REG_BASE, sizeof(ts->sysinfo_data), 192 &ts->sysinfo_data); 193 if (error) 194 return error; 195 196 error = cyttsp_handshake(ts); 197 if (error) 198 return error; 199 200 if (!ts->sysinfo_data.tts_verh && !ts->sysinfo_data.tts_verl) 201 return -EIO; 202 203 return 0; 204 } 205 206 static int cyttsp_set_sysinfo_regs(struct cyttsp *ts) 207 { 208 int retval = 0; 209 210 if (ts->act_intrvl != CY_ACT_INTRVL_DFLT || 211 ts->tch_tmout != CY_TCH_TMOUT_DFLT || 212 ts->lp_intrvl != CY_LP_INTRVL_DFLT) { 213 214 u8 intrvl_ray[] = { 215 ts->act_intrvl, 216 ts->tch_tmout, 217 ts->lp_intrvl 218 }; 219 220 /* set intrvl registers */ 221 retval = ttsp_write_block_data(ts, CY_REG_ACT_INTRVL, 222 sizeof(intrvl_ray), intrvl_ray); 223 msleep(CY_DELAY_DFLT); 224 } 225 226 return retval; 227 } 228 229 static void cyttsp_hard_reset(struct cyttsp *ts) 230 { 231 if (ts->reset_gpio) { 232 /* 233 * According to the CY8CTMA340 datasheet page 21, the external 234 * reset pulse width should be >= 1 ms. The datasheet does not 235 * specify how long we have to wait after reset but a vendor 236 * tree specifies 5 ms here. 237 */ 238 gpiod_set_value_cansleep(ts->reset_gpio, 1); 239 usleep_range(1000, 2000); 240 gpiod_set_value_cansleep(ts->reset_gpio, 0); 241 usleep_range(5000, 6000); 242 } 243 } 244 245 static int cyttsp_soft_reset(struct cyttsp *ts) 246 { 247 int retval; 248 249 /* wait for interrupt to set ready completion */ 250 reinit_completion(&ts->bl_ready); 251 ts->state = CY_BL_STATE; 252 253 enable_irq(ts->irq); 254 255 retval = ttsp_send_command(ts, CY_SOFT_RESET_MODE); 256 if (retval) { 257 dev_err(ts->dev, "failed to send soft reset\n"); 258 goto out; 259 } 260 261 if (!wait_for_completion_timeout(&ts->bl_ready, 262 msecs_to_jiffies(CY_DELAY_DFLT * CY_DELAY_MAX))) { 263 dev_err(ts->dev, "timeout waiting for soft reset\n"); 264 retval = -EIO; 265 } 266 267 out: 268 ts->state = CY_IDLE_STATE; 269 disable_irq(ts->irq); 270 return retval; 271 } 272 273 static int cyttsp_act_dist_setup(struct cyttsp *ts) 274 { 275 u8 act_dist_setup = ts->act_dist; 276 277 /* Init gesture; active distance setup */ 278 return ttsp_write_block_data(ts, CY_REG_ACT_DIST, 279 sizeof(act_dist_setup), &act_dist_setup); 280 } 281 282 static void cyttsp_extract_track_ids(struct cyttsp_xydata *xy_data, int *ids) 283 { 284 ids[0] = xy_data->touch12_id >> 4; 285 ids[1] = xy_data->touch12_id & 0xF; 286 ids[2] = xy_data->touch34_id >> 4; 287 ids[3] = xy_data->touch34_id & 0xF; 288 } 289 290 static const struct cyttsp_tch *cyttsp_get_tch(struct cyttsp_xydata *xy_data, 291 int idx) 292 { 293 switch (idx) { 294 case 0: 295 return &xy_data->tch1; 296 case 1: 297 return &xy_data->tch2; 298 case 2: 299 return &xy_data->tch3; 300 case 3: 301 return &xy_data->tch4; 302 default: 303 return NULL; 304 } 305 } 306 307 static void cyttsp_report_tchdata(struct cyttsp *ts) 308 { 309 struct cyttsp_xydata *xy_data = &ts->xy_data; 310 struct input_dev *input = ts->input; 311 int num_tch = GET_NUM_TOUCHES(xy_data->tt_stat); 312 const struct cyttsp_tch *tch; 313 int ids[CY_MAX_ID]; 314 int i; 315 DECLARE_BITMAP(used, CY_MAX_ID); 316 317 if (IS_LARGE_AREA(xy_data->tt_stat) == 1) { 318 /* terminate all active tracks */ 319 num_tch = 0; 320 dev_dbg(ts->dev, "%s: Large area detected\n", __func__); 321 } else if (num_tch > CY_MAX_FINGER) { 322 /* terminate all active tracks */ 323 num_tch = 0; 324 dev_dbg(ts->dev, "%s: Num touch error detected\n", __func__); 325 } else if (IS_BAD_PKT(xy_data->tt_mode)) { 326 /* terminate all active tracks */ 327 num_tch = 0; 328 dev_dbg(ts->dev, "%s: Invalid buffer detected\n", __func__); 329 } 330 331 cyttsp_extract_track_ids(xy_data, ids); 332 333 bitmap_zero(used, CY_MAX_ID); 334 335 for (i = 0; i < num_tch; i++) { 336 tch = cyttsp_get_tch(xy_data, i); 337 338 input_mt_slot(input, ids[i]); 339 input_mt_report_slot_state(input, MT_TOOL_FINGER, true); 340 input_report_abs(input, ABS_MT_POSITION_X, be16_to_cpu(tch->x)); 341 input_report_abs(input, ABS_MT_POSITION_Y, be16_to_cpu(tch->y)); 342 input_report_abs(input, ABS_MT_TOUCH_MAJOR, tch->z); 343 344 __set_bit(ids[i], used); 345 } 346 347 for (i = 0; i < CY_MAX_ID; i++) { 348 if (test_bit(i, used)) 349 continue; 350 351 input_mt_slot(input, i); 352 input_mt_report_slot_inactive(input); 353 } 354 355 input_sync(input); 356 } 357 358 static irqreturn_t cyttsp_irq(int irq, void *handle) 359 { 360 struct cyttsp *ts = handle; 361 int error; 362 363 if (unlikely(ts->state == CY_BL_STATE)) { 364 complete(&ts->bl_ready); 365 goto out; 366 } 367 368 /* Get touch data from CYTTSP device */ 369 error = ttsp_read_block_data(ts, CY_REG_BASE, 370 sizeof(struct cyttsp_xydata), &ts->xy_data); 371 if (error) 372 goto out; 373 374 /* provide flow control handshake */ 375 error = cyttsp_handshake(ts); 376 if (error) 377 goto out; 378 379 if (unlikely(ts->state == CY_IDLE_STATE)) 380 goto out; 381 382 if (GET_BOOTLOADERMODE(ts->xy_data.tt_mode)) { 383 /* 384 * TTSP device has reset back to bootloader mode. 385 * Restore to operational mode. 386 */ 387 error = cyttsp_exit_bl_mode(ts); 388 if (error) { 389 dev_err(ts->dev, 390 "Could not return to operational mode, err: %d\n", 391 error); 392 ts->state = CY_IDLE_STATE; 393 } 394 } else { 395 cyttsp_report_tchdata(ts); 396 } 397 398 out: 399 return IRQ_HANDLED; 400 } 401 402 static int cyttsp_power_on(struct cyttsp *ts) 403 { 404 int error; 405 406 error = cyttsp_soft_reset(ts); 407 if (error) 408 return error; 409 410 error = cyttsp_load_bl_regs(ts); 411 if (error) 412 return error; 413 414 if (GET_BOOTLOADERMODE(ts->bl_data.bl_status) && 415 IS_VALID_APP(ts->bl_data.bl_status)) { 416 error = cyttsp_exit_bl_mode(ts); 417 if (error) { 418 dev_err(ts->dev, "failed to exit bootloader mode\n"); 419 return error; 420 } 421 } 422 423 if (GET_HSTMODE(ts->bl_data.bl_file) != CY_OPERATE_MODE || 424 IS_OPERATIONAL_ERR(ts->bl_data.bl_status)) { 425 return -ENODEV; 426 } 427 428 error = cyttsp_set_sysinfo_mode(ts); 429 if (error) 430 return error; 431 432 error = cyttsp_set_sysinfo_regs(ts); 433 if (error) 434 return error; 435 436 error = cyttsp_set_operational_mode(ts); 437 if (error) 438 return error; 439 440 /* init active distance */ 441 error = cyttsp_act_dist_setup(ts); 442 if (error) 443 return error; 444 445 ts->state = CY_ACTIVE_STATE; 446 447 return 0; 448 } 449 450 static int cyttsp_enable(struct cyttsp *ts) 451 { 452 int error; 453 454 /* 455 * The device firmware can wake on an I2C or SPI memory slave 456 * address match. So just reading a register is sufficient to 457 * wake up the device. The first read attempt will fail but it 458 * will wake it up making the second read attempt successful. 459 */ 460 error = ttsp_read_block_data(ts, CY_REG_BASE, 461 sizeof(ts->xy_data), &ts->xy_data); 462 if (error) 463 return error; 464 465 if (GET_HSTMODE(ts->xy_data.hst_mode)) 466 return -EIO; 467 468 enable_irq(ts->irq); 469 470 return 0; 471 } 472 473 static int cyttsp_disable(struct cyttsp *ts) 474 { 475 int error; 476 477 error = ttsp_send_command(ts, CY_LOW_POWER_MODE); 478 if (error) 479 return error; 480 481 disable_irq(ts->irq); 482 483 return 0; 484 } 485 486 static int __maybe_unused cyttsp_suspend(struct device *dev) 487 { 488 struct cyttsp *ts = dev_get_drvdata(dev); 489 int retval = 0; 490 491 mutex_lock(&ts->input->mutex); 492 493 if (input_device_enabled(ts->input)) { 494 retval = cyttsp_disable(ts); 495 if (retval == 0) 496 ts->suspended = true; 497 } 498 499 mutex_unlock(&ts->input->mutex); 500 501 return retval; 502 } 503 504 static int __maybe_unused cyttsp_resume(struct device *dev) 505 { 506 struct cyttsp *ts = dev_get_drvdata(dev); 507 508 mutex_lock(&ts->input->mutex); 509 510 if (input_device_enabled(ts->input)) 511 cyttsp_enable(ts); 512 513 ts->suspended = false; 514 515 mutex_unlock(&ts->input->mutex); 516 517 return 0; 518 } 519 520 SIMPLE_DEV_PM_OPS(cyttsp_pm_ops, cyttsp_suspend, cyttsp_resume); 521 EXPORT_SYMBOL_GPL(cyttsp_pm_ops); 522 523 static int cyttsp_open(struct input_dev *dev) 524 { 525 struct cyttsp *ts = input_get_drvdata(dev); 526 int retval = 0; 527 528 if (!ts->suspended) 529 retval = cyttsp_enable(ts); 530 531 return retval; 532 } 533 534 static void cyttsp_close(struct input_dev *dev) 535 { 536 struct cyttsp *ts = input_get_drvdata(dev); 537 538 if (!ts->suspended) 539 cyttsp_disable(ts); 540 } 541 542 static int cyttsp_parse_properties(struct cyttsp *ts) 543 { 544 struct device *dev = ts->dev; 545 u32 dt_value; 546 int ret; 547 548 ts->bl_keys = devm_kzalloc(dev, CY_NUM_BL_KEYS, GFP_KERNEL); 549 if (!ts->bl_keys) 550 return -ENOMEM; 551 552 /* Set some default values */ 553 ts->use_hndshk = false; 554 ts->act_dist = CY_ACT_DIST_DFLT; 555 ts->act_intrvl = CY_ACT_INTRVL_DFLT; 556 ts->tch_tmout = CY_TCH_TMOUT_DFLT; 557 ts->lp_intrvl = CY_LP_INTRVL_DFLT; 558 559 ret = device_property_read_u8_array(dev, "bootloader-key", 560 ts->bl_keys, CY_NUM_BL_KEYS); 561 if (ret) { 562 dev_err(dev, 563 "bootloader-key property could not be retrieved\n"); 564 return ret; 565 } 566 567 ts->use_hndshk = device_property_present(dev, "use-handshake"); 568 569 if (!device_property_read_u32(dev, "active-distance", &dt_value)) { 570 if (dt_value > 15) { 571 dev_err(dev, "active-distance (%u) must be [0-15]\n", 572 dt_value); 573 return -EINVAL; 574 } 575 ts->act_dist &= ~CY_ACT_DIST_MASK; 576 ts->act_dist |= dt_value; 577 } 578 579 if (!device_property_read_u32(dev, "active-interval-ms", &dt_value)) { 580 if (dt_value > 255) { 581 dev_err(dev, "active-interval-ms (%u) must be [0-255]\n", 582 dt_value); 583 return -EINVAL; 584 } 585 ts->act_intrvl = dt_value; 586 } 587 588 if (!device_property_read_u32(dev, "lowpower-interval-ms", &dt_value)) { 589 if (dt_value > 2550) { 590 dev_err(dev, "lowpower-interval-ms (%u) must be [0-2550]\n", 591 dt_value); 592 return -EINVAL; 593 } 594 /* Register value is expressed in 0.01s / bit */ 595 ts->lp_intrvl = dt_value / 10; 596 } 597 598 if (!device_property_read_u32(dev, "touch-timeout-ms", &dt_value)) { 599 if (dt_value > 2550) { 600 dev_err(dev, "touch-timeout-ms (%u) must be [0-2550]\n", 601 dt_value); 602 return -EINVAL; 603 } 604 /* Register value is expressed in 0.01s / bit */ 605 ts->tch_tmout = dt_value / 10; 606 } 607 608 return 0; 609 } 610 611 struct cyttsp *cyttsp_probe(const struct cyttsp_bus_ops *bus_ops, 612 struct device *dev, int irq, size_t xfer_buf_size) 613 { 614 struct cyttsp *ts; 615 struct input_dev *input_dev; 616 int error; 617 618 ts = devm_kzalloc(dev, sizeof(*ts) + xfer_buf_size, GFP_KERNEL); 619 if (!ts) 620 return ERR_PTR(-ENOMEM); 621 622 input_dev = devm_input_allocate_device(dev); 623 if (!input_dev) 624 return ERR_PTR(-ENOMEM); 625 626 ts->dev = dev; 627 ts->input = input_dev; 628 ts->bus_ops = bus_ops; 629 ts->irq = irq; 630 631 ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); 632 if (IS_ERR(ts->reset_gpio)) { 633 error = PTR_ERR(ts->reset_gpio); 634 dev_err(dev, "Failed to request reset gpio, error %d\n", error); 635 return ERR_PTR(error); 636 } 637 638 error = cyttsp_parse_properties(ts); 639 if (error) 640 return ERR_PTR(error); 641 642 init_completion(&ts->bl_ready); 643 644 input_dev->name = "Cypress TTSP TouchScreen"; 645 input_dev->id.bustype = bus_ops->bustype; 646 input_dev->dev.parent = ts->dev; 647 648 input_dev->open = cyttsp_open; 649 input_dev->close = cyttsp_close; 650 651 input_set_drvdata(input_dev, ts); 652 653 input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X); 654 input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y); 655 /* One byte for width 0..255 so this is the limit */ 656 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); 657 658 touchscreen_parse_properties(input_dev, true, NULL); 659 660 error = input_mt_init_slots(input_dev, CY_MAX_ID, INPUT_MT_DIRECT); 661 if (error) { 662 dev_err(dev, "Unable to init MT slots.\n"); 663 return ERR_PTR(error); 664 } 665 666 error = devm_request_threaded_irq(dev, ts->irq, NULL, cyttsp_irq, 667 IRQF_TRIGGER_FALLING | IRQF_ONESHOT | 668 IRQF_NO_AUTOEN, 669 "cyttsp", ts); 670 if (error) { 671 dev_err(ts->dev, "failed to request IRQ %d, err: %d\n", 672 ts->irq, error); 673 return ERR_PTR(error); 674 } 675 676 cyttsp_hard_reset(ts); 677 678 error = cyttsp_power_on(ts); 679 if (error) 680 return ERR_PTR(error); 681 682 error = input_register_device(input_dev); 683 if (error) { 684 dev_err(ts->dev, "failed to register input device: %d\n", 685 error); 686 return ERR_PTR(error); 687 } 688 689 return ts; 690 } 691 EXPORT_SYMBOL_GPL(cyttsp_probe); 692 693 MODULE_LICENSE("GPL"); 694 MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen driver core"); 695 MODULE_AUTHOR("Cypress"); 696