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