1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * cyttsp4_core.c 4 * Cypress TrueTouch(TM) Standard Product V4 Core driver module. 5 * For use with Cypress Txx4xx parts. 6 * Supported parts include: 7 * TMA4XX 8 * TMA1036 9 * 10 * Copyright (C) 2012 Cypress Semiconductor 11 * 12 * Contact Cypress Semiconductor at www.cypress.com <ttdrivers@cypress.com> 13 */ 14 15 #include "cyttsp4_core.h" 16 #include <linux/delay.h> 17 #include <linux/gpio.h> 18 #include <linux/input/mt.h> 19 #include <linux/interrupt.h> 20 #include <linux/pm_runtime.h> 21 #include <linux/sched.h> 22 #include <linux/slab.h> 23 24 /* Timeout in ms. */ 25 #define CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT 500 26 #define CY_CORE_SLEEP_REQUEST_EXCLUSIVE_TIMEOUT 5000 27 #define CY_CORE_MODE_CHANGE_TIMEOUT 1000 28 #define CY_CORE_RESET_AND_WAIT_TIMEOUT 500 29 #define CY_CORE_WAKEUP_TIMEOUT 500 30 31 #define CY_CORE_STARTUP_RETRY_COUNT 3 32 33 static const u8 ldr_exit[] = { 34 0xFF, 0x01, 0x3B, 0x00, 0x00, 0x4F, 0x6D, 0x17 35 }; 36 37 static const u8 ldr_err_app[] = { 38 0x01, 0x02, 0x00, 0x00, 0x55, 0xDD, 0x17 39 }; 40 41 static inline size_t merge_bytes(u8 high, u8 low) 42 { 43 return (high << 8) + low; 44 } 45 46 #ifdef VERBOSE_DEBUG 47 static void cyttsp4_pr_buf(struct device *dev, u8 *pr_buf, u8 *dptr, int size, 48 const char *data_name) 49 { 50 int i, k; 51 const char fmt[] = "%02X "; 52 int max; 53 54 if (!size) 55 return; 56 57 max = (CY_MAX_PRBUF_SIZE - 1) - sizeof(CY_PR_TRUNCATED); 58 59 pr_buf[0] = 0; 60 for (i = k = 0; i < size && k < max; i++, k += 3) 61 scnprintf(pr_buf + k, CY_MAX_PRBUF_SIZE, fmt, dptr[i]); 62 63 dev_vdbg(dev, "%s: %s[0..%d]=%s%s\n", __func__, data_name, size - 1, 64 pr_buf, size <= max ? "" : CY_PR_TRUNCATED); 65 } 66 #else 67 #define cyttsp4_pr_buf(dev, pr_buf, dptr, size, data_name) do { } while (0) 68 #endif 69 70 static int cyttsp4_load_status_regs(struct cyttsp4 *cd) 71 { 72 struct cyttsp4_sysinfo *si = &cd->sysinfo; 73 struct device *dev = cd->dev; 74 int rc; 75 76 rc = cyttsp4_adap_read(cd, CY_REG_BASE, si->si_ofs.mode_size, 77 si->xy_mode); 78 if (rc < 0) 79 dev_err(dev, "%s: fail read mode regs r=%d\n", 80 __func__, rc); 81 else 82 cyttsp4_pr_buf(dev, cd->pr_buf, si->xy_mode, 83 si->si_ofs.mode_size, "xy_mode"); 84 85 return rc; 86 } 87 88 static int cyttsp4_handshake(struct cyttsp4 *cd, u8 mode) 89 { 90 u8 cmd = mode ^ CY_HST_TOGGLE; 91 int rc; 92 93 /* 94 * Mode change issued, handshaking now will cause endless mode change 95 * requests, for sync mode modechange will do same with handshake 96 * */ 97 if (mode & CY_HST_MODE_CHANGE) 98 return 0; 99 100 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(cmd), &cmd); 101 if (rc < 0) 102 dev_err(cd->dev, "%s: bus write fail on handshake (ret=%d)\n", 103 __func__, rc); 104 105 return rc; 106 } 107 108 static int cyttsp4_hw_soft_reset(struct cyttsp4 *cd) 109 { 110 u8 cmd = CY_HST_RESET; 111 int rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(cmd), &cmd); 112 if (rc < 0) { 113 dev_err(cd->dev, "%s: FAILED to execute SOFT reset\n", 114 __func__); 115 return rc; 116 } 117 return 0; 118 } 119 120 static int cyttsp4_hw_hard_reset(struct cyttsp4 *cd) 121 { 122 if (cd->cpdata->xres) { 123 cd->cpdata->xres(cd->cpdata, cd->dev); 124 dev_dbg(cd->dev, "%s: execute HARD reset\n", __func__); 125 return 0; 126 } 127 dev_err(cd->dev, "%s: FAILED to execute HARD reset\n", __func__); 128 return -ENOSYS; 129 } 130 131 static int cyttsp4_hw_reset(struct cyttsp4 *cd) 132 { 133 int rc = cyttsp4_hw_hard_reset(cd); 134 if (rc == -ENOSYS) 135 rc = cyttsp4_hw_soft_reset(cd); 136 return rc; 137 } 138 139 /* 140 * Gets number of bits for a touch filed as parameter, 141 * sets maximum value for field which is used as bit mask 142 * and returns number of bytes required for that field 143 */ 144 static int cyttsp4_bits_2_bytes(unsigned int nbits, size_t *max) 145 { 146 *max = 1UL << nbits; 147 return (nbits + 7) / 8; 148 } 149 150 static int cyttsp4_si_data_offsets(struct cyttsp4 *cd) 151 { 152 struct cyttsp4_sysinfo *si = &cd->sysinfo; 153 int rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(si->si_data), 154 &si->si_data); 155 if (rc < 0) { 156 dev_err(cd->dev, "%s: fail read sysinfo data offsets r=%d\n", 157 __func__, rc); 158 return rc; 159 } 160 161 /* Print sysinfo data offsets */ 162 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)&si->si_data, 163 sizeof(si->si_data), "sysinfo_data_offsets"); 164 165 /* convert sysinfo data offset bytes into integers */ 166 167 si->si_ofs.map_sz = merge_bytes(si->si_data.map_szh, 168 si->si_data.map_szl); 169 si->si_ofs.map_sz = merge_bytes(si->si_data.map_szh, 170 si->si_data.map_szl); 171 si->si_ofs.cydata_ofs = merge_bytes(si->si_data.cydata_ofsh, 172 si->si_data.cydata_ofsl); 173 si->si_ofs.test_ofs = merge_bytes(si->si_data.test_ofsh, 174 si->si_data.test_ofsl); 175 si->si_ofs.pcfg_ofs = merge_bytes(si->si_data.pcfg_ofsh, 176 si->si_data.pcfg_ofsl); 177 si->si_ofs.opcfg_ofs = merge_bytes(si->si_data.opcfg_ofsh, 178 si->si_data.opcfg_ofsl); 179 si->si_ofs.ddata_ofs = merge_bytes(si->si_data.ddata_ofsh, 180 si->si_data.ddata_ofsl); 181 si->si_ofs.mdata_ofs = merge_bytes(si->si_data.mdata_ofsh, 182 si->si_data.mdata_ofsl); 183 return rc; 184 } 185 186 static int cyttsp4_si_get_cydata(struct cyttsp4 *cd) 187 { 188 struct cyttsp4_sysinfo *si = &cd->sysinfo; 189 int read_offset; 190 int mfgid_sz, calc_mfgid_sz; 191 void *p; 192 int rc; 193 194 if (si->si_ofs.test_ofs <= si->si_ofs.cydata_ofs) { 195 dev_err(cd->dev, 196 "%s: invalid offset test_ofs: %zu, cydata_ofs: %zu\n", 197 __func__, si->si_ofs.test_ofs, si->si_ofs.cydata_ofs); 198 return -EINVAL; 199 } 200 201 si->si_ofs.cydata_size = si->si_ofs.test_ofs - si->si_ofs.cydata_ofs; 202 dev_dbg(cd->dev, "%s: cydata size: %zd\n", __func__, 203 si->si_ofs.cydata_size); 204 205 p = krealloc(si->si_ptrs.cydata, si->si_ofs.cydata_size, GFP_KERNEL); 206 if (p == NULL) { 207 dev_err(cd->dev, "%s: failed to allocate cydata memory\n", 208 __func__); 209 return -ENOMEM; 210 } 211 si->si_ptrs.cydata = p; 212 213 read_offset = si->si_ofs.cydata_ofs; 214 215 /* Read the CYDA registers up to MFGID field */ 216 rc = cyttsp4_adap_read(cd, read_offset, 217 offsetof(struct cyttsp4_cydata, mfgid_sz) 218 + sizeof(si->si_ptrs.cydata->mfgid_sz), 219 si->si_ptrs.cydata); 220 if (rc < 0) { 221 dev_err(cd->dev, "%s: fail read cydata r=%d\n", 222 __func__, rc); 223 return rc; 224 } 225 226 /* Check MFGID size */ 227 mfgid_sz = si->si_ptrs.cydata->mfgid_sz; 228 calc_mfgid_sz = si->si_ofs.cydata_size - sizeof(struct cyttsp4_cydata); 229 if (mfgid_sz != calc_mfgid_sz) { 230 dev_err(cd->dev, "%s: mismatch in MFGID size, reported:%d calculated:%d\n", 231 __func__, mfgid_sz, calc_mfgid_sz); 232 return -EINVAL; 233 } 234 235 read_offset += offsetof(struct cyttsp4_cydata, mfgid_sz) 236 + sizeof(si->si_ptrs.cydata->mfgid_sz); 237 238 /* Read the CYDA registers for MFGID field */ 239 rc = cyttsp4_adap_read(cd, read_offset, si->si_ptrs.cydata->mfgid_sz, 240 si->si_ptrs.cydata->mfg_id); 241 if (rc < 0) { 242 dev_err(cd->dev, "%s: fail read cydata r=%d\n", 243 __func__, rc); 244 return rc; 245 } 246 247 read_offset += si->si_ptrs.cydata->mfgid_sz; 248 249 /* Read the rest of the CYDA registers */ 250 rc = cyttsp4_adap_read(cd, read_offset, 251 sizeof(struct cyttsp4_cydata) 252 - offsetof(struct cyttsp4_cydata, cyito_idh), 253 &si->si_ptrs.cydata->cyito_idh); 254 if (rc < 0) { 255 dev_err(cd->dev, "%s: fail read cydata r=%d\n", 256 __func__, rc); 257 return rc; 258 } 259 260 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.cydata, 261 si->si_ofs.cydata_size, "sysinfo_cydata"); 262 return rc; 263 } 264 265 static int cyttsp4_si_get_test_data(struct cyttsp4 *cd) 266 { 267 struct cyttsp4_sysinfo *si = &cd->sysinfo; 268 void *p; 269 int rc; 270 271 if (si->si_ofs.pcfg_ofs <= si->si_ofs.test_ofs) { 272 dev_err(cd->dev, 273 "%s: invalid offset pcfg_ofs: %zu, test_ofs: %zu\n", 274 __func__, si->si_ofs.pcfg_ofs, si->si_ofs.test_ofs); 275 return -EINVAL; 276 } 277 278 si->si_ofs.test_size = si->si_ofs.pcfg_ofs - si->si_ofs.test_ofs; 279 280 p = krealloc(si->si_ptrs.test, si->si_ofs.test_size, GFP_KERNEL); 281 if (p == NULL) { 282 dev_err(cd->dev, "%s: failed to allocate test memory\n", 283 __func__); 284 return -ENOMEM; 285 } 286 si->si_ptrs.test = p; 287 288 rc = cyttsp4_adap_read(cd, si->si_ofs.test_ofs, si->si_ofs.test_size, 289 si->si_ptrs.test); 290 if (rc < 0) { 291 dev_err(cd->dev, "%s: fail read test data r=%d\n", 292 __func__, rc); 293 return rc; 294 } 295 296 cyttsp4_pr_buf(cd->dev, cd->pr_buf, 297 (u8 *)si->si_ptrs.test, si->si_ofs.test_size, 298 "sysinfo_test_data"); 299 if (si->si_ptrs.test->post_codel & 300 CY_POST_CODEL_WDG_RST) 301 dev_info(cd->dev, "%s: %s codel=%02X\n", 302 __func__, "Reset was a WATCHDOG RESET", 303 si->si_ptrs.test->post_codel); 304 305 if (!(si->si_ptrs.test->post_codel & 306 CY_POST_CODEL_CFG_DATA_CRC_FAIL)) 307 dev_info(cd->dev, "%s: %s codel=%02X\n", __func__, 308 "Config Data CRC FAIL", 309 si->si_ptrs.test->post_codel); 310 311 if (!(si->si_ptrs.test->post_codel & 312 CY_POST_CODEL_PANEL_TEST_FAIL)) 313 dev_info(cd->dev, "%s: %s codel=%02X\n", 314 __func__, "PANEL TEST FAIL", 315 si->si_ptrs.test->post_codel); 316 317 dev_info(cd->dev, "%s: SCANNING is %s codel=%02X\n", 318 __func__, si->si_ptrs.test->post_codel & 0x08 ? 319 "ENABLED" : "DISABLED", 320 si->si_ptrs.test->post_codel); 321 return rc; 322 } 323 324 static int cyttsp4_si_get_pcfg_data(struct cyttsp4 *cd) 325 { 326 struct cyttsp4_sysinfo *si = &cd->sysinfo; 327 void *p; 328 int rc; 329 330 if (si->si_ofs.opcfg_ofs <= si->si_ofs.pcfg_ofs) { 331 dev_err(cd->dev, 332 "%s: invalid offset opcfg_ofs: %zu, pcfg_ofs: %zu\n", 333 __func__, si->si_ofs.opcfg_ofs, si->si_ofs.pcfg_ofs); 334 return -EINVAL; 335 } 336 337 si->si_ofs.pcfg_size = si->si_ofs.opcfg_ofs - si->si_ofs.pcfg_ofs; 338 339 p = krealloc(si->si_ptrs.pcfg, si->si_ofs.pcfg_size, GFP_KERNEL); 340 if (p == NULL) { 341 dev_err(cd->dev, "%s: failed to allocate pcfg memory\n", 342 __func__); 343 return -ENOMEM; 344 } 345 si->si_ptrs.pcfg = p; 346 347 rc = cyttsp4_adap_read(cd, si->si_ofs.pcfg_ofs, si->si_ofs.pcfg_size, 348 si->si_ptrs.pcfg); 349 if (rc < 0) { 350 dev_err(cd->dev, "%s: fail read pcfg data r=%d\n", 351 __func__, rc); 352 return rc; 353 } 354 355 si->si_ofs.max_x = merge_bytes((si->si_ptrs.pcfg->res_xh 356 & CY_PCFG_RESOLUTION_X_MASK), si->si_ptrs.pcfg->res_xl); 357 si->si_ofs.x_origin = !!(si->si_ptrs.pcfg->res_xh 358 & CY_PCFG_ORIGIN_X_MASK); 359 si->si_ofs.max_y = merge_bytes((si->si_ptrs.pcfg->res_yh 360 & CY_PCFG_RESOLUTION_Y_MASK), si->si_ptrs.pcfg->res_yl); 361 si->si_ofs.y_origin = !!(si->si_ptrs.pcfg->res_yh 362 & CY_PCFG_ORIGIN_Y_MASK); 363 si->si_ofs.max_p = merge_bytes(si->si_ptrs.pcfg->max_zh, 364 si->si_ptrs.pcfg->max_zl); 365 366 cyttsp4_pr_buf(cd->dev, cd->pr_buf, 367 (u8 *)si->si_ptrs.pcfg, 368 si->si_ofs.pcfg_size, "sysinfo_pcfg_data"); 369 return rc; 370 } 371 372 static int cyttsp4_si_get_opcfg_data(struct cyttsp4 *cd) 373 { 374 struct cyttsp4_sysinfo *si = &cd->sysinfo; 375 struct cyttsp4_tch_abs_params *tch; 376 struct cyttsp4_tch_rec_params *tch_old, *tch_new; 377 enum cyttsp4_tch_abs abs; 378 int i; 379 void *p; 380 int rc; 381 382 if (si->si_ofs.ddata_ofs <= si->si_ofs.opcfg_ofs) { 383 dev_err(cd->dev, 384 "%s: invalid offset ddata_ofs: %zu, opcfg_ofs: %zu\n", 385 __func__, si->si_ofs.ddata_ofs, si->si_ofs.opcfg_ofs); 386 return -EINVAL; 387 } 388 389 si->si_ofs.opcfg_size = si->si_ofs.ddata_ofs - si->si_ofs.opcfg_ofs; 390 391 p = krealloc(si->si_ptrs.opcfg, si->si_ofs.opcfg_size, GFP_KERNEL); 392 if (p == NULL) { 393 dev_err(cd->dev, "%s: failed to allocate opcfg memory\n", 394 __func__); 395 return -ENOMEM; 396 } 397 si->si_ptrs.opcfg = p; 398 399 rc = cyttsp4_adap_read(cd, si->si_ofs.opcfg_ofs, si->si_ofs.opcfg_size, 400 si->si_ptrs.opcfg); 401 if (rc < 0) { 402 dev_err(cd->dev, "%s: fail read opcfg data r=%d\n", 403 __func__, rc); 404 return rc; 405 } 406 si->si_ofs.cmd_ofs = si->si_ptrs.opcfg->cmd_ofs; 407 si->si_ofs.rep_ofs = si->si_ptrs.opcfg->rep_ofs; 408 si->si_ofs.rep_sz = (si->si_ptrs.opcfg->rep_szh * 256) + 409 si->si_ptrs.opcfg->rep_szl; 410 si->si_ofs.num_btns = si->si_ptrs.opcfg->num_btns; 411 si->si_ofs.num_btn_regs = (si->si_ofs.num_btns + 412 CY_NUM_BTN_PER_REG - 1) / CY_NUM_BTN_PER_REG; 413 si->si_ofs.tt_stat_ofs = si->si_ptrs.opcfg->tt_stat_ofs; 414 si->si_ofs.obj_cfg0 = si->si_ptrs.opcfg->obj_cfg0; 415 si->si_ofs.max_tchs = si->si_ptrs.opcfg->max_tchs & 416 CY_BYTE_OFS_MASK; 417 si->si_ofs.tch_rec_size = si->si_ptrs.opcfg->tch_rec_size & 418 CY_BYTE_OFS_MASK; 419 420 /* Get the old touch fields */ 421 for (abs = CY_TCH_X; abs < CY_NUM_TCH_FIELDS; abs++) { 422 tch = &si->si_ofs.tch_abs[abs]; 423 tch_old = &si->si_ptrs.opcfg->tch_rec_old[abs]; 424 425 tch->ofs = tch_old->loc & CY_BYTE_OFS_MASK; 426 tch->size = cyttsp4_bits_2_bytes(tch_old->size, 427 &tch->max); 428 tch->bofs = (tch_old->loc & CY_BOFS_MASK) >> CY_BOFS_SHIFT; 429 } 430 431 /* button fields */ 432 si->si_ofs.btn_rec_size = si->si_ptrs.opcfg->btn_rec_size; 433 si->si_ofs.btn_diff_ofs = si->si_ptrs.opcfg->btn_diff_ofs; 434 si->si_ofs.btn_diff_size = si->si_ptrs.opcfg->btn_diff_size; 435 436 if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) { 437 /* Get the extended touch fields */ 438 for (i = 0; i < CY_NUM_EXT_TCH_FIELDS; abs++, i++) { 439 tch = &si->si_ofs.tch_abs[abs]; 440 tch_new = &si->si_ptrs.opcfg->tch_rec_new[i]; 441 442 tch->ofs = tch_new->loc & CY_BYTE_OFS_MASK; 443 tch->size = cyttsp4_bits_2_bytes(tch_new->size, 444 &tch->max); 445 tch->bofs = (tch_new->loc & CY_BOFS_MASK) >> CY_BOFS_SHIFT; 446 } 447 } 448 449 for (abs = 0; abs < CY_TCH_NUM_ABS; abs++) { 450 dev_dbg(cd->dev, "%s: tch_rec_%s\n", __func__, 451 cyttsp4_tch_abs_string[abs]); 452 dev_dbg(cd->dev, "%s: ofs =%2zd\n", __func__, 453 si->si_ofs.tch_abs[abs].ofs); 454 dev_dbg(cd->dev, "%s: siz =%2zd\n", __func__, 455 si->si_ofs.tch_abs[abs].size); 456 dev_dbg(cd->dev, "%s: max =%2zd\n", __func__, 457 si->si_ofs.tch_abs[abs].max); 458 dev_dbg(cd->dev, "%s: bofs=%2zd\n", __func__, 459 si->si_ofs.tch_abs[abs].bofs); 460 } 461 462 si->si_ofs.mode_size = si->si_ofs.tt_stat_ofs + 1; 463 si->si_ofs.data_size = si->si_ofs.max_tchs * 464 si->si_ptrs.opcfg->tch_rec_size; 465 466 cyttsp4_pr_buf(cd->dev, cd->pr_buf, (u8 *)si->si_ptrs.opcfg, 467 si->si_ofs.opcfg_size, "sysinfo_opcfg_data"); 468 469 return 0; 470 } 471 472 static int cyttsp4_si_get_ddata(struct cyttsp4 *cd) 473 { 474 struct cyttsp4_sysinfo *si = &cd->sysinfo; 475 void *p; 476 int rc; 477 478 si->si_ofs.ddata_size = si->si_ofs.mdata_ofs - si->si_ofs.ddata_ofs; 479 480 p = krealloc(si->si_ptrs.ddata, si->si_ofs.ddata_size, GFP_KERNEL); 481 if (p == NULL) { 482 dev_err(cd->dev, "%s: fail alloc ddata memory\n", __func__); 483 return -ENOMEM; 484 } 485 si->si_ptrs.ddata = p; 486 487 rc = cyttsp4_adap_read(cd, si->si_ofs.ddata_ofs, si->si_ofs.ddata_size, 488 si->si_ptrs.ddata); 489 if (rc < 0) 490 dev_err(cd->dev, "%s: fail read ddata data r=%d\n", 491 __func__, rc); 492 else 493 cyttsp4_pr_buf(cd->dev, cd->pr_buf, 494 (u8 *)si->si_ptrs.ddata, 495 si->si_ofs.ddata_size, "sysinfo_ddata"); 496 return rc; 497 } 498 499 static int cyttsp4_si_get_mdata(struct cyttsp4 *cd) 500 { 501 struct cyttsp4_sysinfo *si = &cd->sysinfo; 502 void *p; 503 int rc; 504 505 si->si_ofs.mdata_size = si->si_ofs.map_sz - si->si_ofs.mdata_ofs; 506 507 p = krealloc(si->si_ptrs.mdata, si->si_ofs.mdata_size, GFP_KERNEL); 508 if (p == NULL) { 509 dev_err(cd->dev, "%s: fail alloc mdata memory\n", __func__); 510 return -ENOMEM; 511 } 512 si->si_ptrs.mdata = p; 513 514 rc = cyttsp4_adap_read(cd, si->si_ofs.mdata_ofs, si->si_ofs.mdata_size, 515 si->si_ptrs.mdata); 516 if (rc < 0) 517 dev_err(cd->dev, "%s: fail read mdata data r=%d\n", 518 __func__, rc); 519 else 520 cyttsp4_pr_buf(cd->dev, cd->pr_buf, 521 (u8 *)si->si_ptrs.mdata, 522 si->si_ofs.mdata_size, "sysinfo_mdata"); 523 return rc; 524 } 525 526 static int cyttsp4_si_get_btn_data(struct cyttsp4 *cd) 527 { 528 struct cyttsp4_sysinfo *si = &cd->sysinfo; 529 int btn; 530 int num_defined_keys; 531 u16 *key_table; 532 void *p; 533 int rc = 0; 534 535 if (si->si_ofs.num_btns) { 536 si->si_ofs.btn_keys_size = si->si_ofs.num_btns * 537 sizeof(struct cyttsp4_btn); 538 539 p = krealloc(si->btn, si->si_ofs.btn_keys_size, 540 GFP_KERNEL|__GFP_ZERO); 541 if (p == NULL) { 542 dev_err(cd->dev, "%s: %s\n", __func__, 543 "fail alloc btn_keys memory"); 544 return -ENOMEM; 545 } 546 si->btn = p; 547 548 if (cd->cpdata->sett[CY_IC_GRPNUM_BTN_KEYS] == NULL) 549 num_defined_keys = 0; 550 else if (cd->cpdata->sett[CY_IC_GRPNUM_BTN_KEYS]->data == NULL) 551 num_defined_keys = 0; 552 else 553 num_defined_keys = cd->cpdata->sett 554 [CY_IC_GRPNUM_BTN_KEYS]->size; 555 556 for (btn = 0; btn < si->si_ofs.num_btns && 557 btn < num_defined_keys; btn++) { 558 key_table = (u16 *)cd->cpdata->sett 559 [CY_IC_GRPNUM_BTN_KEYS]->data; 560 si->btn[btn].key_code = key_table[btn]; 561 si->btn[btn].state = CY_BTN_RELEASED; 562 si->btn[btn].enabled = true; 563 } 564 for (; btn < si->si_ofs.num_btns; btn++) { 565 si->btn[btn].key_code = KEY_RESERVED; 566 si->btn[btn].state = CY_BTN_RELEASED; 567 si->btn[btn].enabled = true; 568 } 569 570 return rc; 571 } 572 573 si->si_ofs.btn_keys_size = 0; 574 kfree(si->btn); 575 si->btn = NULL; 576 return rc; 577 } 578 579 static int cyttsp4_si_get_op_data_ptrs(struct cyttsp4 *cd) 580 { 581 struct cyttsp4_sysinfo *si = &cd->sysinfo; 582 void *p; 583 584 p = krealloc(si->xy_mode, si->si_ofs.mode_size, GFP_KERNEL|__GFP_ZERO); 585 if (p == NULL) 586 return -ENOMEM; 587 si->xy_mode = p; 588 589 p = krealloc(si->xy_data, si->si_ofs.data_size, GFP_KERNEL|__GFP_ZERO); 590 if (p == NULL) 591 return -ENOMEM; 592 si->xy_data = p; 593 594 p = krealloc(si->btn_rec_data, 595 si->si_ofs.btn_rec_size * si->si_ofs.num_btns, 596 GFP_KERNEL|__GFP_ZERO); 597 if (p == NULL) 598 return -ENOMEM; 599 si->btn_rec_data = p; 600 601 return 0; 602 } 603 604 static void cyttsp4_si_put_log_data(struct cyttsp4 *cd) 605 { 606 struct cyttsp4_sysinfo *si = &cd->sysinfo; 607 dev_dbg(cd->dev, "%s: cydata_ofs =%4zd siz=%4zd\n", __func__, 608 si->si_ofs.cydata_ofs, si->si_ofs.cydata_size); 609 dev_dbg(cd->dev, "%s: test_ofs =%4zd siz=%4zd\n", __func__, 610 si->si_ofs.test_ofs, si->si_ofs.test_size); 611 dev_dbg(cd->dev, "%s: pcfg_ofs =%4zd siz=%4zd\n", __func__, 612 si->si_ofs.pcfg_ofs, si->si_ofs.pcfg_size); 613 dev_dbg(cd->dev, "%s: opcfg_ofs =%4zd siz=%4zd\n", __func__, 614 si->si_ofs.opcfg_ofs, si->si_ofs.opcfg_size); 615 dev_dbg(cd->dev, "%s: ddata_ofs =%4zd siz=%4zd\n", __func__, 616 si->si_ofs.ddata_ofs, si->si_ofs.ddata_size); 617 dev_dbg(cd->dev, "%s: mdata_ofs =%4zd siz=%4zd\n", __func__, 618 si->si_ofs.mdata_ofs, si->si_ofs.mdata_size); 619 620 dev_dbg(cd->dev, "%s: cmd_ofs =%4zd\n", __func__, 621 si->si_ofs.cmd_ofs); 622 dev_dbg(cd->dev, "%s: rep_ofs =%4zd\n", __func__, 623 si->si_ofs.rep_ofs); 624 dev_dbg(cd->dev, "%s: rep_sz =%4zd\n", __func__, 625 si->si_ofs.rep_sz); 626 dev_dbg(cd->dev, "%s: num_btns =%4zd\n", __func__, 627 si->si_ofs.num_btns); 628 dev_dbg(cd->dev, "%s: num_btn_regs =%4zd\n", __func__, 629 si->si_ofs.num_btn_regs); 630 dev_dbg(cd->dev, "%s: tt_stat_ofs =%4zd\n", __func__, 631 si->si_ofs.tt_stat_ofs); 632 dev_dbg(cd->dev, "%s: tch_rec_size =%4zd\n", __func__, 633 si->si_ofs.tch_rec_size); 634 dev_dbg(cd->dev, "%s: max_tchs =%4zd\n", __func__, 635 si->si_ofs.max_tchs); 636 dev_dbg(cd->dev, "%s: mode_size =%4zd\n", __func__, 637 si->si_ofs.mode_size); 638 dev_dbg(cd->dev, "%s: data_size =%4zd\n", __func__, 639 si->si_ofs.data_size); 640 dev_dbg(cd->dev, "%s: map_sz =%4zd\n", __func__, 641 si->si_ofs.map_sz); 642 643 dev_dbg(cd->dev, "%s: btn_rec_size =%2zd\n", __func__, 644 si->si_ofs.btn_rec_size); 645 dev_dbg(cd->dev, "%s: btn_diff_ofs =%2zd\n", __func__, 646 si->si_ofs.btn_diff_ofs); 647 dev_dbg(cd->dev, "%s: btn_diff_size =%2zd\n", __func__, 648 si->si_ofs.btn_diff_size); 649 650 dev_dbg(cd->dev, "%s: max_x = 0x%04zX (%zd)\n", __func__, 651 si->si_ofs.max_x, si->si_ofs.max_x); 652 dev_dbg(cd->dev, "%s: x_origin = %zd (%s)\n", __func__, 653 si->si_ofs.x_origin, 654 si->si_ofs.x_origin == CY_NORMAL_ORIGIN ? 655 "left corner" : "right corner"); 656 dev_dbg(cd->dev, "%s: max_y = 0x%04zX (%zd)\n", __func__, 657 si->si_ofs.max_y, si->si_ofs.max_y); 658 dev_dbg(cd->dev, "%s: y_origin = %zd (%s)\n", __func__, 659 si->si_ofs.y_origin, 660 si->si_ofs.y_origin == CY_NORMAL_ORIGIN ? 661 "upper corner" : "lower corner"); 662 dev_dbg(cd->dev, "%s: max_p = 0x%04zX (%zd)\n", __func__, 663 si->si_ofs.max_p, si->si_ofs.max_p); 664 665 dev_dbg(cd->dev, "%s: xy_mode=%p xy_data=%p\n", __func__, 666 si->xy_mode, si->xy_data); 667 } 668 669 static int cyttsp4_get_sysinfo_regs(struct cyttsp4 *cd) 670 { 671 struct cyttsp4_sysinfo *si = &cd->sysinfo; 672 int rc; 673 674 rc = cyttsp4_si_data_offsets(cd); 675 if (rc < 0) 676 return rc; 677 678 rc = cyttsp4_si_get_cydata(cd); 679 if (rc < 0) 680 return rc; 681 682 rc = cyttsp4_si_get_test_data(cd); 683 if (rc < 0) 684 return rc; 685 686 rc = cyttsp4_si_get_pcfg_data(cd); 687 if (rc < 0) 688 return rc; 689 690 rc = cyttsp4_si_get_opcfg_data(cd); 691 if (rc < 0) 692 return rc; 693 694 rc = cyttsp4_si_get_ddata(cd); 695 if (rc < 0) 696 return rc; 697 698 rc = cyttsp4_si_get_mdata(cd); 699 if (rc < 0) 700 return rc; 701 702 rc = cyttsp4_si_get_btn_data(cd); 703 if (rc < 0) 704 return rc; 705 706 rc = cyttsp4_si_get_op_data_ptrs(cd); 707 if (rc < 0) { 708 dev_err(cd->dev, "%s: failed to get_op_data\n", 709 __func__); 710 return rc; 711 } 712 713 cyttsp4_si_put_log_data(cd); 714 715 /* provide flow control handshake */ 716 rc = cyttsp4_handshake(cd, si->si_data.hst_mode); 717 if (rc < 0) 718 dev_err(cd->dev, "%s: handshake fail on sysinfo reg\n", 719 __func__); 720 721 si->ready = true; 722 return rc; 723 } 724 725 static void cyttsp4_queue_startup_(struct cyttsp4 *cd) 726 { 727 if (cd->startup_state == STARTUP_NONE) { 728 cd->startup_state = STARTUP_QUEUED; 729 schedule_work(&cd->startup_work); 730 dev_dbg(cd->dev, "%s: cyttsp4_startup queued\n", __func__); 731 } else { 732 dev_dbg(cd->dev, "%s: startup_state = %d\n", __func__, 733 cd->startup_state); 734 } 735 } 736 737 static void cyttsp4_report_slot_liftoff(struct cyttsp4_mt_data *md, 738 int max_slots) 739 { 740 int t; 741 742 if (md->num_prv_tch == 0) 743 return; 744 745 for (t = 0; t < max_slots; t++) { 746 input_mt_slot(md->input, t); 747 input_mt_report_slot_inactive(md->input); 748 } 749 } 750 751 static void cyttsp4_lift_all(struct cyttsp4_mt_data *md) 752 { 753 if (!md->si) 754 return; 755 756 if (md->num_prv_tch != 0) { 757 cyttsp4_report_slot_liftoff(md, 758 md->si->si_ofs.tch_abs[CY_TCH_T].max); 759 input_sync(md->input); 760 md->num_prv_tch = 0; 761 } 762 } 763 764 static void cyttsp4_get_touch_axis(struct cyttsp4_mt_data *md, 765 int *axis, int size, int max, u8 *xy_data, int bofs) 766 { 767 int nbyte; 768 int next; 769 770 for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) { 771 dev_vdbg(&md->input->dev, 772 "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p" 773 " xy_data[%d]=%02X(%d) bofs=%d\n", 774 __func__, *axis, *axis, size, max, xy_data, next, 775 xy_data[next], xy_data[next], bofs); 776 *axis = (*axis * 256) + (xy_data[next] >> bofs); 777 next++; 778 } 779 780 *axis &= max - 1; 781 782 dev_vdbg(&md->input->dev, 783 "%s: *axis=%02X(%d) size=%d max=%08X xy_data=%p" 784 " xy_data[%d]=%02X(%d)\n", 785 __func__, *axis, *axis, size, max, xy_data, next, 786 xy_data[next], xy_data[next]); 787 } 788 789 static void cyttsp4_get_touch(struct cyttsp4_mt_data *md, 790 struct cyttsp4_touch *touch, u8 *xy_data) 791 { 792 struct device *dev = &md->input->dev; 793 struct cyttsp4_sysinfo *si = md->si; 794 enum cyttsp4_tch_abs abs; 795 bool flipped; 796 797 for (abs = CY_TCH_X; abs < CY_TCH_NUM_ABS; abs++) { 798 cyttsp4_get_touch_axis(md, &touch->abs[abs], 799 si->si_ofs.tch_abs[abs].size, 800 si->si_ofs.tch_abs[abs].max, 801 xy_data + si->si_ofs.tch_abs[abs].ofs, 802 si->si_ofs.tch_abs[abs].bofs); 803 dev_vdbg(dev, "%s: get %s=%04X(%d)\n", __func__, 804 cyttsp4_tch_abs_string[abs], 805 touch->abs[abs], touch->abs[abs]); 806 } 807 808 if (md->pdata->flags & CY_FLAG_FLIP) { 809 swap(touch->abs[CY_TCH_X], touch->abs[CY_TCH_Y]); 810 flipped = true; 811 } else 812 flipped = false; 813 814 if (md->pdata->flags & CY_FLAG_INV_X) { 815 if (flipped) 816 touch->abs[CY_TCH_X] = md->si->si_ofs.max_y - 817 touch->abs[CY_TCH_X]; 818 else 819 touch->abs[CY_TCH_X] = md->si->si_ofs.max_x - 820 touch->abs[CY_TCH_X]; 821 } 822 if (md->pdata->flags & CY_FLAG_INV_Y) { 823 if (flipped) 824 touch->abs[CY_TCH_Y] = md->si->si_ofs.max_x - 825 touch->abs[CY_TCH_Y]; 826 else 827 touch->abs[CY_TCH_Y] = md->si->si_ofs.max_y - 828 touch->abs[CY_TCH_Y]; 829 } 830 831 dev_vdbg(dev, "%s: flip=%s inv-x=%s inv-y=%s x=%04X(%d) y=%04X(%d)\n", 832 __func__, flipped ? "true" : "false", 833 md->pdata->flags & CY_FLAG_INV_X ? "true" : "false", 834 md->pdata->flags & CY_FLAG_INV_Y ? "true" : "false", 835 touch->abs[CY_TCH_X], touch->abs[CY_TCH_X], 836 touch->abs[CY_TCH_Y], touch->abs[CY_TCH_Y]); 837 } 838 839 static void cyttsp4_final_sync(struct input_dev *input, int max_slots, int *ids) 840 { 841 int t; 842 843 for (t = 0; t < max_slots; t++) { 844 if (ids[t]) 845 continue; 846 input_mt_slot(input, t); 847 input_mt_report_slot_inactive(input); 848 } 849 850 input_sync(input); 851 } 852 853 static void cyttsp4_get_mt_touches(struct cyttsp4_mt_data *md, int num_cur_tch) 854 { 855 struct device *dev = &md->input->dev; 856 struct cyttsp4_sysinfo *si = md->si; 857 struct cyttsp4_touch tch; 858 int sig; 859 int i, j, t = 0; 860 int ids[max(CY_TMA1036_MAX_TCH, CY_TMA4XX_MAX_TCH)]; 861 862 memset(ids, 0, si->si_ofs.tch_abs[CY_TCH_T].max * sizeof(int)); 863 for (i = 0; i < num_cur_tch; i++) { 864 cyttsp4_get_touch(md, &tch, si->xy_data + 865 (i * si->si_ofs.tch_rec_size)); 866 if ((tch.abs[CY_TCH_T] < md->pdata->frmwrk->abs 867 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MIN_OST]) || 868 (tch.abs[CY_TCH_T] > md->pdata->frmwrk->abs 869 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MAX_OST])) { 870 dev_err(dev, "%s: tch=%d -> bad trk_id=%d max_id=%d\n", 871 __func__, i, tch.abs[CY_TCH_T], 872 md->pdata->frmwrk->abs[(CY_ABS_ID_OST * 873 CY_NUM_ABS_SET) + CY_MAX_OST]); 874 continue; 875 } 876 877 /* use 0 based track id's */ 878 sig = md->pdata->frmwrk->abs 879 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + 0]; 880 if (sig != CY_IGNORE_VALUE) { 881 t = tch.abs[CY_TCH_T] - md->pdata->frmwrk->abs 882 [(CY_ABS_ID_OST * CY_NUM_ABS_SET) + CY_MIN_OST]; 883 if (tch.abs[CY_TCH_E] == CY_EV_LIFTOFF) { 884 dev_dbg(dev, "%s: t=%d e=%d lift-off\n", 885 __func__, t, tch.abs[CY_TCH_E]); 886 goto cyttsp4_get_mt_touches_pr_tch; 887 } 888 input_mt_slot(md->input, t); 889 input_mt_report_slot_state(md->input, MT_TOOL_FINGER, 890 true); 891 ids[t] = true; 892 } 893 894 /* all devices: position and pressure fields */ 895 for (j = 0; j <= CY_ABS_W_OST; j++) { 896 sig = md->pdata->frmwrk->abs[((CY_ABS_X_OST + j) * 897 CY_NUM_ABS_SET) + 0]; 898 if (sig != CY_IGNORE_VALUE) 899 input_report_abs(md->input, sig, 900 tch.abs[CY_TCH_X + j]); 901 } 902 if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) { 903 /* 904 * TMA400 size and orientation fields: 905 * if pressure is non-zero and major touch 906 * signal is zero, then set major and minor touch 907 * signals to minimum non-zero value 908 */ 909 if (tch.abs[CY_TCH_P] > 0 && tch.abs[CY_TCH_MAJ] == 0) 910 tch.abs[CY_TCH_MAJ] = tch.abs[CY_TCH_MIN] = 1; 911 912 /* Get the extended touch fields */ 913 for (j = 0; j < CY_NUM_EXT_TCH_FIELDS; j++) { 914 sig = md->pdata->frmwrk->abs 915 [((CY_ABS_MAJ_OST + j) * 916 CY_NUM_ABS_SET) + 0]; 917 if (sig != CY_IGNORE_VALUE) 918 input_report_abs(md->input, sig, 919 tch.abs[CY_TCH_MAJ + j]); 920 } 921 } 922 923 cyttsp4_get_mt_touches_pr_tch: 924 if (si->si_ofs.tch_rec_size > CY_TMA1036_TCH_REC_SIZE) 925 dev_dbg(dev, 926 "%s: t=%d x=%d y=%d z=%d M=%d m=%d o=%d e=%d\n", 927 __func__, t, 928 tch.abs[CY_TCH_X], 929 tch.abs[CY_TCH_Y], 930 tch.abs[CY_TCH_P], 931 tch.abs[CY_TCH_MAJ], 932 tch.abs[CY_TCH_MIN], 933 tch.abs[CY_TCH_OR], 934 tch.abs[CY_TCH_E]); 935 else 936 dev_dbg(dev, 937 "%s: t=%d x=%d y=%d z=%d e=%d\n", __func__, 938 t, 939 tch.abs[CY_TCH_X], 940 tch.abs[CY_TCH_Y], 941 tch.abs[CY_TCH_P], 942 tch.abs[CY_TCH_E]); 943 } 944 945 cyttsp4_final_sync(md->input, si->si_ofs.tch_abs[CY_TCH_T].max, ids); 946 947 md->num_prv_tch = num_cur_tch; 948 949 return; 950 } 951 952 /* read xy_data for all current touches */ 953 static int cyttsp4_xy_worker(struct cyttsp4 *cd) 954 { 955 struct cyttsp4_mt_data *md = &cd->md; 956 struct device *dev = &md->input->dev; 957 struct cyttsp4_sysinfo *si = md->si; 958 u8 num_cur_tch; 959 u8 hst_mode; 960 u8 rep_len; 961 u8 rep_stat; 962 u8 tt_stat; 963 int rc = 0; 964 965 /* 966 * Get event data from cyttsp4 device. 967 * The event data includes all data 968 * for all active touches. 969 * Event data also includes button data 970 */ 971 /* 972 * Use 2 reads: 973 * 1st read to get mode + button bytes + touch count (core) 974 * 2nd read (optional) to get touch 1 - touch n data 975 */ 976 hst_mode = si->xy_mode[CY_REG_BASE]; 977 rep_len = si->xy_mode[si->si_ofs.rep_ofs]; 978 rep_stat = si->xy_mode[si->si_ofs.rep_ofs + 1]; 979 tt_stat = si->xy_mode[si->si_ofs.tt_stat_ofs]; 980 dev_vdbg(dev, "%s: %s%02X %s%d %s%02X %s%02X\n", __func__, 981 "hst_mode=", hst_mode, "rep_len=", rep_len, 982 "rep_stat=", rep_stat, "tt_stat=", tt_stat); 983 984 num_cur_tch = GET_NUM_TOUCHES(tt_stat); 985 dev_vdbg(dev, "%s: num_cur_tch=%d\n", __func__, num_cur_tch); 986 987 if (rep_len == 0 && num_cur_tch > 0) { 988 dev_err(dev, "%s: report length error rep_len=%d num_tch=%d\n", 989 __func__, rep_len, num_cur_tch); 990 goto cyttsp4_xy_worker_exit; 991 } 992 993 /* read touches */ 994 if (num_cur_tch > 0) { 995 rc = cyttsp4_adap_read(cd, si->si_ofs.tt_stat_ofs + 1, 996 num_cur_tch * si->si_ofs.tch_rec_size, 997 si->xy_data); 998 if (rc < 0) { 999 dev_err(dev, "%s: read fail on touch regs r=%d\n", 1000 __func__, rc); 1001 goto cyttsp4_xy_worker_exit; 1002 } 1003 } 1004 1005 /* print xy data */ 1006 cyttsp4_pr_buf(dev, cd->pr_buf, si->xy_data, num_cur_tch * 1007 si->si_ofs.tch_rec_size, "xy_data"); 1008 1009 /* check any error conditions */ 1010 if (IS_BAD_PKT(rep_stat)) { 1011 dev_dbg(dev, "%s: Invalid buffer detected\n", __func__); 1012 rc = 0; 1013 goto cyttsp4_xy_worker_exit; 1014 } 1015 1016 if (IS_LARGE_AREA(tt_stat)) 1017 dev_dbg(dev, "%s: Large area detected\n", __func__); 1018 1019 if (num_cur_tch > si->si_ofs.max_tchs) { 1020 dev_err(dev, "%s: too many tch; set to max tch (n=%d c=%zd)\n", 1021 __func__, num_cur_tch, si->si_ofs.max_tchs); 1022 num_cur_tch = si->si_ofs.max_tchs; 1023 } 1024 1025 /* extract xy_data for all currently reported touches */ 1026 dev_vdbg(dev, "%s: extract data num_cur_tch=%d\n", __func__, 1027 num_cur_tch); 1028 if (num_cur_tch) 1029 cyttsp4_get_mt_touches(md, num_cur_tch); 1030 else 1031 cyttsp4_lift_all(md); 1032 1033 rc = 0; 1034 1035 cyttsp4_xy_worker_exit: 1036 return rc; 1037 } 1038 1039 static int cyttsp4_mt_attention(struct cyttsp4 *cd) 1040 { 1041 struct device *dev = cd->dev; 1042 struct cyttsp4_mt_data *md = &cd->md; 1043 int rc = 0; 1044 1045 if (!md->si) 1046 return 0; 1047 1048 mutex_lock(&md->report_lock); 1049 if (!md->is_suspended) { 1050 /* core handles handshake */ 1051 rc = cyttsp4_xy_worker(cd); 1052 } else { 1053 dev_vdbg(dev, "%s: Ignoring report while suspended\n", 1054 __func__); 1055 } 1056 mutex_unlock(&md->report_lock); 1057 if (rc < 0) 1058 dev_err(dev, "%s: xy_worker error r=%d\n", __func__, rc); 1059 1060 return rc; 1061 } 1062 1063 static irqreturn_t cyttsp4_irq(int irq, void *handle) 1064 { 1065 struct cyttsp4 *cd = handle; 1066 struct device *dev = cd->dev; 1067 enum cyttsp4_mode cur_mode; 1068 u8 cmd_ofs = cd->sysinfo.si_ofs.cmd_ofs; 1069 u8 mode[3]; 1070 int rc; 1071 1072 /* 1073 * Check whether this IRQ should be ignored (external) 1074 * This should be the very first thing to check since 1075 * ignore_irq may be set for a very short period of time 1076 */ 1077 if (atomic_read(&cd->ignore_irq)) { 1078 dev_vdbg(dev, "%s: Ignoring IRQ\n", __func__); 1079 return IRQ_HANDLED; 1080 } 1081 1082 dev_dbg(dev, "%s int:0x%x\n", __func__, cd->int_status); 1083 1084 mutex_lock(&cd->system_lock); 1085 1086 /* Just to debug */ 1087 if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING) 1088 dev_vdbg(dev, "%s: Received IRQ while in sleep\n", __func__); 1089 1090 rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), mode); 1091 if (rc) { 1092 dev_err(cd->dev, "%s: Fail read adapter r=%d\n", __func__, rc); 1093 goto cyttsp4_irq_exit; 1094 } 1095 dev_vdbg(dev, "%s mode[0-2]:0x%X 0x%X 0x%X\n", __func__, 1096 mode[0], mode[1], mode[2]); 1097 1098 if (IS_BOOTLOADER(mode[0], mode[1])) { 1099 cur_mode = CY_MODE_BOOTLOADER; 1100 dev_vdbg(dev, "%s: bl running\n", __func__); 1101 if (cd->mode == CY_MODE_BOOTLOADER) { 1102 /* Signal bootloader heartbeat heard */ 1103 wake_up(&cd->wait_q); 1104 goto cyttsp4_irq_exit; 1105 } 1106 1107 /* switch to bootloader */ 1108 dev_dbg(dev, "%s: restart switch to bl m=%d -> m=%d\n", 1109 __func__, cd->mode, cur_mode); 1110 1111 /* catch operation->bl glitch */ 1112 if (cd->mode != CY_MODE_UNKNOWN) { 1113 /* Incase startup_state do not let startup_() */ 1114 cd->mode = CY_MODE_UNKNOWN; 1115 cyttsp4_queue_startup_(cd); 1116 goto cyttsp4_irq_exit; 1117 } 1118 1119 /* 1120 * do not wake thread on this switch since 1121 * it is possible to get an early heartbeat 1122 * prior to performing the reset 1123 */ 1124 cd->mode = cur_mode; 1125 1126 goto cyttsp4_irq_exit; 1127 } 1128 1129 switch (mode[0] & CY_HST_MODE) { 1130 case CY_HST_OPERATE: 1131 cur_mode = CY_MODE_OPERATIONAL; 1132 dev_vdbg(dev, "%s: operational\n", __func__); 1133 break; 1134 case CY_HST_CAT: 1135 cur_mode = CY_MODE_CAT; 1136 dev_vdbg(dev, "%s: CaT\n", __func__); 1137 break; 1138 case CY_HST_SYSINFO: 1139 cur_mode = CY_MODE_SYSINFO; 1140 dev_vdbg(dev, "%s: sysinfo\n", __func__); 1141 break; 1142 default: 1143 cur_mode = CY_MODE_UNKNOWN; 1144 dev_err(dev, "%s: unknown HST mode 0x%02X\n", __func__, 1145 mode[0]); 1146 break; 1147 } 1148 1149 /* Check whether this IRQ should be ignored (internal) */ 1150 if (cd->int_status & CY_INT_IGNORE) { 1151 dev_vdbg(dev, "%s: Ignoring IRQ\n", __func__); 1152 goto cyttsp4_irq_exit; 1153 } 1154 1155 /* Check for wake up interrupt */ 1156 if (cd->int_status & CY_INT_AWAKE) { 1157 cd->int_status &= ~CY_INT_AWAKE; 1158 wake_up(&cd->wait_q); 1159 dev_vdbg(dev, "%s: Received wake up interrupt\n", __func__); 1160 goto cyttsp4_irq_handshake; 1161 } 1162 1163 /* Expecting mode change interrupt */ 1164 if ((cd->int_status & CY_INT_MODE_CHANGE) 1165 && (mode[0] & CY_HST_MODE_CHANGE) == 0) { 1166 cd->int_status &= ~CY_INT_MODE_CHANGE; 1167 dev_dbg(dev, "%s: finish mode switch m=%d -> m=%d\n", 1168 __func__, cd->mode, cur_mode); 1169 cd->mode = cur_mode; 1170 wake_up(&cd->wait_q); 1171 goto cyttsp4_irq_handshake; 1172 } 1173 1174 /* compare current core mode to current device mode */ 1175 dev_vdbg(dev, "%s: cd->mode=%d cur_mode=%d\n", 1176 __func__, cd->mode, cur_mode); 1177 if ((mode[0] & CY_HST_MODE_CHANGE) == 0 && cd->mode != cur_mode) { 1178 /* Unexpected mode change occurred */ 1179 dev_err(dev, "%s %d->%d 0x%x\n", __func__, cd->mode, 1180 cur_mode, cd->int_status); 1181 dev_dbg(dev, "%s: Unexpected mode change, startup\n", 1182 __func__); 1183 cyttsp4_queue_startup_(cd); 1184 goto cyttsp4_irq_exit; 1185 } 1186 1187 /* Expecting command complete interrupt */ 1188 dev_vdbg(dev, "%s: command byte:0x%x\n", __func__, mode[cmd_ofs]); 1189 if ((cd->int_status & CY_INT_EXEC_CMD) 1190 && mode[cmd_ofs] & CY_CMD_COMPLETE) { 1191 cd->int_status &= ~CY_INT_EXEC_CMD; 1192 dev_vdbg(dev, "%s: Received command complete interrupt\n", 1193 __func__); 1194 wake_up(&cd->wait_q); 1195 /* 1196 * It is possible to receive a single interrupt for 1197 * command complete and touch/button status report. 1198 * Continue processing for a possible status report. 1199 */ 1200 } 1201 1202 /* This should be status report, read status regs */ 1203 if (cd->mode == CY_MODE_OPERATIONAL) { 1204 dev_vdbg(dev, "%s: Read status registers\n", __func__); 1205 rc = cyttsp4_load_status_regs(cd); 1206 if (rc < 0) 1207 dev_err(dev, "%s: fail read mode regs r=%d\n", 1208 __func__, rc); 1209 } 1210 1211 cyttsp4_mt_attention(cd); 1212 1213 cyttsp4_irq_handshake: 1214 /* handshake the event */ 1215 dev_vdbg(dev, "%s: Handshake mode=0x%02X r=%d\n", 1216 __func__, mode[0], rc); 1217 rc = cyttsp4_handshake(cd, mode[0]); 1218 if (rc < 0) 1219 dev_err(dev, "%s: Fail handshake mode=0x%02X r=%d\n", 1220 __func__, mode[0], rc); 1221 1222 /* 1223 * a non-zero udelay period is required for using 1224 * IRQF_TRIGGER_LOW in order to delay until the 1225 * device completes isr deassert 1226 */ 1227 udelay(cd->cpdata->level_irq_udelay); 1228 1229 cyttsp4_irq_exit: 1230 mutex_unlock(&cd->system_lock); 1231 return IRQ_HANDLED; 1232 } 1233 1234 static void cyttsp4_start_wd_timer(struct cyttsp4 *cd) 1235 { 1236 if (!CY_WATCHDOG_TIMEOUT) 1237 return; 1238 1239 mod_timer(&cd->watchdog_timer, jiffies + 1240 msecs_to_jiffies(CY_WATCHDOG_TIMEOUT)); 1241 } 1242 1243 static void cyttsp4_stop_wd_timer(struct cyttsp4 *cd) 1244 { 1245 if (!CY_WATCHDOG_TIMEOUT) 1246 return; 1247 1248 /* 1249 * Ensure we wait until the watchdog timer 1250 * running on a different CPU finishes 1251 */ 1252 del_timer_sync(&cd->watchdog_timer); 1253 cancel_work_sync(&cd->watchdog_work); 1254 del_timer_sync(&cd->watchdog_timer); 1255 } 1256 1257 static void cyttsp4_watchdog_timer(struct timer_list *t) 1258 { 1259 struct cyttsp4 *cd = from_timer(cd, t, watchdog_timer); 1260 1261 dev_vdbg(cd->dev, "%s: Watchdog timer triggered\n", __func__); 1262 1263 schedule_work(&cd->watchdog_work); 1264 1265 return; 1266 } 1267 1268 static int cyttsp4_request_exclusive(struct cyttsp4 *cd, void *ownptr, 1269 int timeout_ms) 1270 { 1271 int t = msecs_to_jiffies(timeout_ms); 1272 bool with_timeout = (timeout_ms != 0); 1273 1274 mutex_lock(&cd->system_lock); 1275 if (!cd->exclusive_dev && cd->exclusive_waits == 0) { 1276 cd->exclusive_dev = ownptr; 1277 goto exit; 1278 } 1279 1280 cd->exclusive_waits++; 1281 wait: 1282 mutex_unlock(&cd->system_lock); 1283 if (with_timeout) { 1284 t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t); 1285 if (IS_TMO(t)) { 1286 dev_err(cd->dev, "%s: tmo waiting exclusive access\n", 1287 __func__); 1288 mutex_lock(&cd->system_lock); 1289 cd->exclusive_waits--; 1290 mutex_unlock(&cd->system_lock); 1291 return -ETIME; 1292 } 1293 } else { 1294 wait_event(cd->wait_q, !cd->exclusive_dev); 1295 } 1296 mutex_lock(&cd->system_lock); 1297 if (cd->exclusive_dev) 1298 goto wait; 1299 cd->exclusive_dev = ownptr; 1300 cd->exclusive_waits--; 1301 exit: 1302 mutex_unlock(&cd->system_lock); 1303 1304 return 0; 1305 } 1306 1307 /* 1308 * returns error if was not owned 1309 */ 1310 static int cyttsp4_release_exclusive(struct cyttsp4 *cd, void *ownptr) 1311 { 1312 mutex_lock(&cd->system_lock); 1313 if (cd->exclusive_dev != ownptr) { 1314 mutex_unlock(&cd->system_lock); 1315 return -EINVAL; 1316 } 1317 1318 dev_vdbg(cd->dev, "%s: exclusive_dev %p freed\n", 1319 __func__, cd->exclusive_dev); 1320 cd->exclusive_dev = NULL; 1321 wake_up(&cd->wait_q); 1322 mutex_unlock(&cd->system_lock); 1323 return 0; 1324 } 1325 1326 static int cyttsp4_wait_bl_heartbeat(struct cyttsp4 *cd) 1327 { 1328 long t; 1329 int rc = 0; 1330 1331 /* wait heartbeat */ 1332 dev_vdbg(cd->dev, "%s: wait heartbeat...\n", __func__); 1333 t = wait_event_timeout(cd->wait_q, cd->mode == CY_MODE_BOOTLOADER, 1334 msecs_to_jiffies(CY_CORE_RESET_AND_WAIT_TIMEOUT)); 1335 if (IS_TMO(t)) { 1336 dev_err(cd->dev, "%s: tmo waiting bl heartbeat cd->mode=%d\n", 1337 __func__, cd->mode); 1338 rc = -ETIME; 1339 } 1340 1341 return rc; 1342 } 1343 1344 static int cyttsp4_wait_sysinfo_mode(struct cyttsp4 *cd) 1345 { 1346 long t; 1347 1348 dev_vdbg(cd->dev, "%s: wait sysinfo...\n", __func__); 1349 1350 t = wait_event_timeout(cd->wait_q, cd->mode == CY_MODE_SYSINFO, 1351 msecs_to_jiffies(CY_CORE_MODE_CHANGE_TIMEOUT)); 1352 if (IS_TMO(t)) { 1353 dev_err(cd->dev, "%s: tmo waiting exit bl cd->mode=%d\n", 1354 __func__, cd->mode); 1355 mutex_lock(&cd->system_lock); 1356 cd->int_status &= ~CY_INT_MODE_CHANGE; 1357 mutex_unlock(&cd->system_lock); 1358 return -ETIME; 1359 } 1360 1361 return 0; 1362 } 1363 1364 static int cyttsp4_reset_and_wait(struct cyttsp4 *cd) 1365 { 1366 int rc; 1367 1368 /* reset hardware */ 1369 mutex_lock(&cd->system_lock); 1370 dev_dbg(cd->dev, "%s: reset hw...\n", __func__); 1371 rc = cyttsp4_hw_reset(cd); 1372 cd->mode = CY_MODE_UNKNOWN; 1373 mutex_unlock(&cd->system_lock); 1374 if (rc < 0) { 1375 dev_err(cd->dev, "%s:Fail hw reset r=%d\n", __func__, rc); 1376 return rc; 1377 } 1378 1379 return cyttsp4_wait_bl_heartbeat(cd); 1380 } 1381 1382 /* 1383 * returns err if refused or timeout; block until mode change complete 1384 * bit is set (mode change interrupt) 1385 */ 1386 static int cyttsp4_set_mode(struct cyttsp4 *cd, int new_mode) 1387 { 1388 u8 new_dev_mode; 1389 u8 mode; 1390 long t; 1391 int rc; 1392 1393 switch (new_mode) { 1394 case CY_MODE_OPERATIONAL: 1395 new_dev_mode = CY_HST_OPERATE; 1396 break; 1397 case CY_MODE_SYSINFO: 1398 new_dev_mode = CY_HST_SYSINFO; 1399 break; 1400 case CY_MODE_CAT: 1401 new_dev_mode = CY_HST_CAT; 1402 break; 1403 default: 1404 dev_err(cd->dev, "%s: invalid mode: %02X(%d)\n", 1405 __func__, new_mode, new_mode); 1406 return -EINVAL; 1407 } 1408 1409 /* change mode */ 1410 dev_dbg(cd->dev, "%s: %s=%p new_dev_mode=%02X new_mode=%d\n", 1411 __func__, "have exclusive", cd->exclusive_dev, 1412 new_dev_mode, new_mode); 1413 1414 mutex_lock(&cd->system_lock); 1415 rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode); 1416 if (rc < 0) { 1417 mutex_unlock(&cd->system_lock); 1418 dev_err(cd->dev, "%s: Fail read mode r=%d\n", 1419 __func__, rc); 1420 goto exit; 1421 } 1422 1423 /* Clear device mode bits and set to new mode */ 1424 mode &= ~CY_HST_MODE; 1425 mode |= new_dev_mode | CY_HST_MODE_CHANGE; 1426 1427 cd->int_status |= CY_INT_MODE_CHANGE; 1428 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(mode), &mode); 1429 mutex_unlock(&cd->system_lock); 1430 if (rc < 0) { 1431 dev_err(cd->dev, "%s: Fail write mode change r=%d\n", 1432 __func__, rc); 1433 goto exit; 1434 } 1435 1436 /* wait for mode change done interrupt */ 1437 t = wait_event_timeout(cd->wait_q, 1438 (cd->int_status & CY_INT_MODE_CHANGE) == 0, 1439 msecs_to_jiffies(CY_CORE_MODE_CHANGE_TIMEOUT)); 1440 dev_dbg(cd->dev, "%s: back from wait t=%ld cd->mode=%d\n", 1441 __func__, t, cd->mode); 1442 1443 if (IS_TMO(t)) { 1444 dev_err(cd->dev, "%s: %s\n", __func__, 1445 "tmo waiting mode change"); 1446 mutex_lock(&cd->system_lock); 1447 cd->int_status &= ~CY_INT_MODE_CHANGE; 1448 mutex_unlock(&cd->system_lock); 1449 rc = -EINVAL; 1450 } 1451 1452 exit: 1453 return rc; 1454 } 1455 1456 static void cyttsp4_watchdog_work(struct work_struct *work) 1457 { 1458 struct cyttsp4 *cd = 1459 container_of(work, struct cyttsp4, watchdog_work); 1460 u8 *mode; 1461 int retval; 1462 1463 mutex_lock(&cd->system_lock); 1464 retval = cyttsp4_load_status_regs(cd); 1465 if (retval < 0) { 1466 dev_err(cd->dev, 1467 "%s: failed to access device in watchdog timer r=%d\n", 1468 __func__, retval); 1469 cyttsp4_queue_startup_(cd); 1470 goto cyttsp4_timer_watchdog_exit_error; 1471 } 1472 mode = &cd->sysinfo.xy_mode[CY_REG_BASE]; 1473 if (IS_BOOTLOADER(mode[0], mode[1])) { 1474 dev_err(cd->dev, 1475 "%s: device found in bootloader mode when operational mode\n", 1476 __func__); 1477 cyttsp4_queue_startup_(cd); 1478 goto cyttsp4_timer_watchdog_exit_error; 1479 } 1480 1481 cyttsp4_start_wd_timer(cd); 1482 cyttsp4_timer_watchdog_exit_error: 1483 mutex_unlock(&cd->system_lock); 1484 return; 1485 } 1486 1487 static int cyttsp4_core_sleep_(struct cyttsp4 *cd) 1488 { 1489 enum cyttsp4_sleep_state ss = SS_SLEEP_ON; 1490 enum cyttsp4_int_state int_status = CY_INT_IGNORE; 1491 int rc = 0; 1492 u8 mode[2]; 1493 1494 /* Already in sleep mode? */ 1495 mutex_lock(&cd->system_lock); 1496 if (cd->sleep_state == SS_SLEEP_ON) { 1497 mutex_unlock(&cd->system_lock); 1498 return 0; 1499 } 1500 cd->sleep_state = SS_SLEEPING; 1501 mutex_unlock(&cd->system_lock); 1502 1503 cyttsp4_stop_wd_timer(cd); 1504 1505 /* Wait until currently running IRQ handler exits and disable IRQ */ 1506 disable_irq(cd->irq); 1507 1508 dev_vdbg(cd->dev, "%s: write DEEP SLEEP...\n", __func__); 1509 mutex_lock(&cd->system_lock); 1510 rc = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode); 1511 if (rc) { 1512 mutex_unlock(&cd->system_lock); 1513 dev_err(cd->dev, "%s: Fail read adapter r=%d\n", __func__, rc); 1514 goto error; 1515 } 1516 1517 if (IS_BOOTLOADER(mode[0], mode[1])) { 1518 mutex_unlock(&cd->system_lock); 1519 dev_err(cd->dev, "%s: Device in BOOTLOADER mode.\n", __func__); 1520 rc = -EINVAL; 1521 goto error; 1522 } 1523 1524 mode[0] |= CY_HST_SLEEP; 1525 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(mode[0]), &mode[0]); 1526 mutex_unlock(&cd->system_lock); 1527 if (rc) { 1528 dev_err(cd->dev, "%s: Fail write adapter r=%d\n", __func__, rc); 1529 goto error; 1530 } 1531 dev_vdbg(cd->dev, "%s: write DEEP SLEEP succeeded\n", __func__); 1532 1533 if (cd->cpdata->power) { 1534 dev_dbg(cd->dev, "%s: Power down HW\n", __func__); 1535 rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, &cd->ignore_irq); 1536 } else { 1537 dev_dbg(cd->dev, "%s: No power function\n", __func__); 1538 rc = 0; 1539 } 1540 if (rc < 0) { 1541 dev_err(cd->dev, "%s: HW Power down fails r=%d\n", 1542 __func__, rc); 1543 goto error; 1544 } 1545 1546 /* Give time to FW to sleep */ 1547 msleep(50); 1548 1549 goto exit; 1550 1551 error: 1552 ss = SS_SLEEP_OFF; 1553 int_status = CY_INT_NONE; 1554 cyttsp4_start_wd_timer(cd); 1555 1556 exit: 1557 mutex_lock(&cd->system_lock); 1558 cd->sleep_state = ss; 1559 cd->int_status |= int_status; 1560 mutex_unlock(&cd->system_lock); 1561 enable_irq(cd->irq); 1562 return rc; 1563 } 1564 1565 static int cyttsp4_startup_(struct cyttsp4 *cd) 1566 { 1567 int retry = CY_CORE_STARTUP_RETRY_COUNT; 1568 int rc; 1569 1570 cyttsp4_stop_wd_timer(cd); 1571 1572 reset: 1573 if (retry != CY_CORE_STARTUP_RETRY_COUNT) 1574 dev_dbg(cd->dev, "%s: Retry %d\n", __func__, 1575 CY_CORE_STARTUP_RETRY_COUNT - retry); 1576 1577 /* reset hardware and wait for heartbeat */ 1578 rc = cyttsp4_reset_and_wait(cd); 1579 if (rc < 0) { 1580 dev_err(cd->dev, "%s: Error on h/w reset r=%d\n", __func__, rc); 1581 if (retry--) 1582 goto reset; 1583 goto exit; 1584 } 1585 1586 /* exit bl into sysinfo mode */ 1587 dev_vdbg(cd->dev, "%s: write exit ldr...\n", __func__); 1588 mutex_lock(&cd->system_lock); 1589 cd->int_status &= ~CY_INT_IGNORE; 1590 cd->int_status |= CY_INT_MODE_CHANGE; 1591 1592 rc = cyttsp4_adap_write(cd, CY_REG_BASE, sizeof(ldr_exit), 1593 (u8 *)ldr_exit); 1594 mutex_unlock(&cd->system_lock); 1595 if (rc < 0) { 1596 dev_err(cd->dev, "%s: Fail write r=%d\n", __func__, rc); 1597 if (retry--) 1598 goto reset; 1599 goto exit; 1600 } 1601 1602 rc = cyttsp4_wait_sysinfo_mode(cd); 1603 if (rc < 0) { 1604 u8 buf[sizeof(ldr_err_app)]; 1605 int rc1; 1606 1607 /* Check for invalid/corrupted touch application */ 1608 rc1 = cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(ldr_err_app), 1609 buf); 1610 if (rc1) { 1611 dev_err(cd->dev, "%s: Fail read r=%d\n", __func__, rc1); 1612 } else if (!memcmp(buf, ldr_err_app, sizeof(ldr_err_app))) { 1613 dev_err(cd->dev, "%s: Error launching touch application\n", 1614 __func__); 1615 mutex_lock(&cd->system_lock); 1616 cd->invalid_touch_app = true; 1617 mutex_unlock(&cd->system_lock); 1618 goto exit_no_wd; 1619 } 1620 1621 if (retry--) 1622 goto reset; 1623 goto exit; 1624 } 1625 1626 mutex_lock(&cd->system_lock); 1627 cd->invalid_touch_app = false; 1628 mutex_unlock(&cd->system_lock); 1629 1630 /* read sysinfo data */ 1631 dev_vdbg(cd->dev, "%s: get sysinfo regs..\n", __func__); 1632 rc = cyttsp4_get_sysinfo_regs(cd); 1633 if (rc < 0) { 1634 dev_err(cd->dev, "%s: failed to get sysinfo regs rc=%d\n", 1635 __func__, rc); 1636 if (retry--) 1637 goto reset; 1638 goto exit; 1639 } 1640 1641 rc = cyttsp4_set_mode(cd, CY_MODE_OPERATIONAL); 1642 if (rc < 0) { 1643 dev_err(cd->dev, "%s: failed to set mode to operational rc=%d\n", 1644 __func__, rc); 1645 if (retry--) 1646 goto reset; 1647 goto exit; 1648 } 1649 1650 cyttsp4_lift_all(&cd->md); 1651 1652 /* restore to sleep if was suspended */ 1653 mutex_lock(&cd->system_lock); 1654 if (cd->sleep_state == SS_SLEEP_ON) { 1655 cd->sleep_state = SS_SLEEP_OFF; 1656 mutex_unlock(&cd->system_lock); 1657 cyttsp4_core_sleep_(cd); 1658 goto exit_no_wd; 1659 } 1660 mutex_unlock(&cd->system_lock); 1661 1662 exit: 1663 cyttsp4_start_wd_timer(cd); 1664 exit_no_wd: 1665 return rc; 1666 } 1667 1668 static int cyttsp4_startup(struct cyttsp4 *cd) 1669 { 1670 int rc; 1671 1672 mutex_lock(&cd->system_lock); 1673 cd->startup_state = STARTUP_RUNNING; 1674 mutex_unlock(&cd->system_lock); 1675 1676 rc = cyttsp4_request_exclusive(cd, cd->dev, 1677 CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT); 1678 if (rc < 0) { 1679 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n", 1680 __func__, cd->exclusive_dev, cd->dev); 1681 goto exit; 1682 } 1683 1684 rc = cyttsp4_startup_(cd); 1685 1686 if (cyttsp4_release_exclusive(cd, cd->dev) < 0) 1687 /* Don't return fail code, mode is already changed. */ 1688 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__); 1689 else 1690 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__); 1691 1692 exit: 1693 mutex_lock(&cd->system_lock); 1694 cd->startup_state = STARTUP_NONE; 1695 mutex_unlock(&cd->system_lock); 1696 1697 /* Wake the waiters for end of startup */ 1698 wake_up(&cd->wait_q); 1699 1700 return rc; 1701 } 1702 1703 static void cyttsp4_startup_work_function(struct work_struct *work) 1704 { 1705 struct cyttsp4 *cd = container_of(work, struct cyttsp4, startup_work); 1706 int rc; 1707 1708 rc = cyttsp4_startup(cd); 1709 if (rc < 0) 1710 dev_err(cd->dev, "%s: Fail queued startup r=%d\n", 1711 __func__, rc); 1712 } 1713 1714 static void cyttsp4_free_si_ptrs(struct cyttsp4 *cd) 1715 { 1716 struct cyttsp4_sysinfo *si = &cd->sysinfo; 1717 1718 if (!si) 1719 return; 1720 1721 kfree(si->si_ptrs.cydata); 1722 kfree(si->si_ptrs.test); 1723 kfree(si->si_ptrs.pcfg); 1724 kfree(si->si_ptrs.opcfg); 1725 kfree(si->si_ptrs.ddata); 1726 kfree(si->si_ptrs.mdata); 1727 kfree(si->btn); 1728 kfree(si->xy_mode); 1729 kfree(si->xy_data); 1730 kfree(si->btn_rec_data); 1731 } 1732 1733 #ifdef CONFIG_PM 1734 static int cyttsp4_core_sleep(struct cyttsp4 *cd) 1735 { 1736 int rc; 1737 1738 rc = cyttsp4_request_exclusive(cd, cd->dev, 1739 CY_CORE_SLEEP_REQUEST_EXCLUSIVE_TIMEOUT); 1740 if (rc < 0) { 1741 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n", 1742 __func__, cd->exclusive_dev, cd->dev); 1743 return 0; 1744 } 1745 1746 rc = cyttsp4_core_sleep_(cd); 1747 1748 if (cyttsp4_release_exclusive(cd, cd->dev) < 0) 1749 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__); 1750 else 1751 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__); 1752 1753 return rc; 1754 } 1755 1756 static int cyttsp4_core_wake_(struct cyttsp4 *cd) 1757 { 1758 struct device *dev = cd->dev; 1759 int rc; 1760 u8 mode; 1761 int t; 1762 1763 /* Already woken? */ 1764 mutex_lock(&cd->system_lock); 1765 if (cd->sleep_state == SS_SLEEP_OFF) { 1766 mutex_unlock(&cd->system_lock); 1767 return 0; 1768 } 1769 cd->int_status &= ~CY_INT_IGNORE; 1770 cd->int_status |= CY_INT_AWAKE; 1771 cd->sleep_state = SS_WAKING; 1772 1773 if (cd->cpdata->power) { 1774 dev_dbg(dev, "%s: Power up HW\n", __func__); 1775 rc = cd->cpdata->power(cd->cpdata, 1, dev, &cd->ignore_irq); 1776 } else { 1777 dev_dbg(dev, "%s: No power function\n", __func__); 1778 rc = -ENOSYS; 1779 } 1780 if (rc < 0) { 1781 dev_err(dev, "%s: HW Power up fails r=%d\n", 1782 __func__, rc); 1783 1784 /* Initiate a read transaction to wake up */ 1785 cyttsp4_adap_read(cd, CY_REG_BASE, sizeof(mode), &mode); 1786 } else 1787 dev_vdbg(cd->dev, "%s: HW power up succeeds\n", 1788 __func__); 1789 mutex_unlock(&cd->system_lock); 1790 1791 t = wait_event_timeout(cd->wait_q, 1792 (cd->int_status & CY_INT_AWAKE) == 0, 1793 msecs_to_jiffies(CY_CORE_WAKEUP_TIMEOUT)); 1794 if (IS_TMO(t)) { 1795 dev_err(dev, "%s: TMO waiting for wakeup\n", __func__); 1796 mutex_lock(&cd->system_lock); 1797 cd->int_status &= ~CY_INT_AWAKE; 1798 /* Try starting up */ 1799 cyttsp4_queue_startup_(cd); 1800 mutex_unlock(&cd->system_lock); 1801 } 1802 1803 mutex_lock(&cd->system_lock); 1804 cd->sleep_state = SS_SLEEP_OFF; 1805 mutex_unlock(&cd->system_lock); 1806 1807 cyttsp4_start_wd_timer(cd); 1808 1809 return 0; 1810 } 1811 1812 static int cyttsp4_core_wake(struct cyttsp4 *cd) 1813 { 1814 int rc; 1815 1816 rc = cyttsp4_request_exclusive(cd, cd->dev, 1817 CY_CORE_REQUEST_EXCLUSIVE_TIMEOUT); 1818 if (rc < 0) { 1819 dev_err(cd->dev, "%s: fail get exclusive ex=%p own=%p\n", 1820 __func__, cd->exclusive_dev, cd->dev); 1821 return 0; 1822 } 1823 1824 rc = cyttsp4_core_wake_(cd); 1825 1826 if (cyttsp4_release_exclusive(cd, cd->dev) < 0) 1827 dev_err(cd->dev, "%s: fail to release exclusive\n", __func__); 1828 else 1829 dev_vdbg(cd->dev, "%s: pass release exclusive\n", __func__); 1830 1831 return rc; 1832 } 1833 1834 static int cyttsp4_core_suspend(struct device *dev) 1835 { 1836 struct cyttsp4 *cd = dev_get_drvdata(dev); 1837 struct cyttsp4_mt_data *md = &cd->md; 1838 int rc; 1839 1840 md->is_suspended = true; 1841 1842 rc = cyttsp4_core_sleep(cd); 1843 if (rc < 0) { 1844 dev_err(dev, "%s: Error on sleep\n", __func__); 1845 return -EAGAIN; 1846 } 1847 return 0; 1848 } 1849 1850 static int cyttsp4_core_resume(struct device *dev) 1851 { 1852 struct cyttsp4 *cd = dev_get_drvdata(dev); 1853 struct cyttsp4_mt_data *md = &cd->md; 1854 int rc; 1855 1856 md->is_suspended = false; 1857 1858 rc = cyttsp4_core_wake(cd); 1859 if (rc < 0) { 1860 dev_err(dev, "%s: Error on wake\n", __func__); 1861 return -EAGAIN; 1862 } 1863 1864 return 0; 1865 } 1866 #endif 1867 1868 const struct dev_pm_ops cyttsp4_pm_ops = { 1869 SET_SYSTEM_SLEEP_PM_OPS(cyttsp4_core_suspend, cyttsp4_core_resume) 1870 SET_RUNTIME_PM_OPS(cyttsp4_core_suspend, cyttsp4_core_resume, NULL) 1871 }; 1872 EXPORT_SYMBOL_GPL(cyttsp4_pm_ops); 1873 1874 static int cyttsp4_mt_open(struct input_dev *input) 1875 { 1876 pm_runtime_get(input->dev.parent); 1877 return 0; 1878 } 1879 1880 static void cyttsp4_mt_close(struct input_dev *input) 1881 { 1882 struct cyttsp4_mt_data *md = input_get_drvdata(input); 1883 mutex_lock(&md->report_lock); 1884 if (!md->is_suspended) 1885 pm_runtime_put(input->dev.parent); 1886 mutex_unlock(&md->report_lock); 1887 } 1888 1889 1890 static int cyttsp4_setup_input_device(struct cyttsp4 *cd) 1891 { 1892 struct device *dev = cd->dev; 1893 struct cyttsp4_mt_data *md = &cd->md; 1894 int signal = CY_IGNORE_VALUE; 1895 int max_x, max_y, max_p, min, max; 1896 int max_x_tmp, max_y_tmp; 1897 int i; 1898 int rc; 1899 1900 dev_vdbg(dev, "%s: Initialize event signals\n", __func__); 1901 __set_bit(EV_ABS, md->input->evbit); 1902 __set_bit(EV_REL, md->input->evbit); 1903 __set_bit(EV_KEY, md->input->evbit); 1904 1905 max_x_tmp = md->si->si_ofs.max_x; 1906 max_y_tmp = md->si->si_ofs.max_y; 1907 1908 /* get maximum values from the sysinfo data */ 1909 if (md->pdata->flags & CY_FLAG_FLIP) { 1910 max_x = max_y_tmp - 1; 1911 max_y = max_x_tmp - 1; 1912 } else { 1913 max_x = max_x_tmp - 1; 1914 max_y = max_y_tmp - 1; 1915 } 1916 max_p = md->si->si_ofs.max_p; 1917 1918 /* set event signal capabilities */ 1919 for (i = 0; i < (md->pdata->frmwrk->size / CY_NUM_ABS_SET); i++) { 1920 signal = md->pdata->frmwrk->abs 1921 [(i * CY_NUM_ABS_SET) + CY_SIGNAL_OST]; 1922 if (signal != CY_IGNORE_VALUE) { 1923 __set_bit(signal, md->input->absbit); 1924 min = md->pdata->frmwrk->abs 1925 [(i * CY_NUM_ABS_SET) + CY_MIN_OST]; 1926 max = md->pdata->frmwrk->abs 1927 [(i * CY_NUM_ABS_SET) + CY_MAX_OST]; 1928 if (i == CY_ABS_ID_OST) { 1929 /* shift track ids down to start at 0 */ 1930 max = max - min; 1931 min = min - min; 1932 } else if (i == CY_ABS_X_OST) 1933 max = max_x; 1934 else if (i == CY_ABS_Y_OST) 1935 max = max_y; 1936 else if (i == CY_ABS_P_OST) 1937 max = max_p; 1938 input_set_abs_params(md->input, signal, min, max, 1939 md->pdata->frmwrk->abs 1940 [(i * CY_NUM_ABS_SET) + CY_FUZZ_OST], 1941 md->pdata->frmwrk->abs 1942 [(i * CY_NUM_ABS_SET) + CY_FLAT_OST]); 1943 dev_dbg(dev, "%s: register signal=%02X min=%d max=%d\n", 1944 __func__, signal, min, max); 1945 if ((i == CY_ABS_ID_OST) && 1946 (md->si->si_ofs.tch_rec_size < 1947 CY_TMA4XX_TCH_REC_SIZE)) 1948 break; 1949 } 1950 } 1951 1952 input_mt_init_slots(md->input, md->si->si_ofs.tch_abs[CY_TCH_T].max, 1953 INPUT_MT_DIRECT); 1954 rc = input_register_device(md->input); 1955 if (rc < 0) 1956 dev_err(dev, "%s: Error, failed register input device r=%d\n", 1957 __func__, rc); 1958 return rc; 1959 } 1960 1961 static int cyttsp4_mt_probe(struct cyttsp4 *cd) 1962 { 1963 struct device *dev = cd->dev; 1964 struct cyttsp4_mt_data *md = &cd->md; 1965 struct cyttsp4_mt_platform_data *pdata = cd->pdata->mt_pdata; 1966 int rc = 0; 1967 1968 mutex_init(&md->report_lock); 1969 md->pdata = pdata; 1970 /* Create the input device and register it. */ 1971 dev_vdbg(dev, "%s: Create the input device and register it\n", 1972 __func__); 1973 md->input = input_allocate_device(); 1974 if (md->input == NULL) { 1975 dev_err(dev, "%s: Error, failed to allocate input device\n", 1976 __func__); 1977 rc = -ENOSYS; 1978 goto error_alloc_failed; 1979 } 1980 1981 md->input->name = pdata->inp_dev_name; 1982 scnprintf(md->phys, sizeof(md->phys)-1, "%s", dev_name(dev)); 1983 md->input->phys = md->phys; 1984 md->input->id.bustype = cd->bus_ops->bustype; 1985 md->input->dev.parent = dev; 1986 md->input->open = cyttsp4_mt_open; 1987 md->input->close = cyttsp4_mt_close; 1988 input_set_drvdata(md->input, md); 1989 1990 /* get sysinfo */ 1991 md->si = &cd->sysinfo; 1992 1993 rc = cyttsp4_setup_input_device(cd); 1994 if (rc) 1995 goto error_init_input; 1996 1997 return 0; 1998 1999 error_init_input: 2000 input_free_device(md->input); 2001 error_alloc_failed: 2002 dev_err(dev, "%s failed.\n", __func__); 2003 return rc; 2004 } 2005 2006 struct cyttsp4 *cyttsp4_probe(const struct cyttsp4_bus_ops *ops, 2007 struct device *dev, u16 irq, size_t xfer_buf_size) 2008 { 2009 struct cyttsp4 *cd; 2010 struct cyttsp4_platform_data *pdata = dev_get_platdata(dev); 2011 unsigned long irq_flags; 2012 int rc = 0; 2013 2014 if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) { 2015 dev_err(dev, "%s: Missing platform data\n", __func__); 2016 rc = -ENODEV; 2017 goto error_no_pdata; 2018 } 2019 2020 cd = kzalloc(sizeof(*cd), GFP_KERNEL); 2021 if (!cd) { 2022 dev_err(dev, "%s: Error, kzalloc\n", __func__); 2023 rc = -ENOMEM; 2024 goto error_alloc_data; 2025 } 2026 2027 cd->xfer_buf = kzalloc(xfer_buf_size, GFP_KERNEL); 2028 if (!cd->xfer_buf) { 2029 dev_err(dev, "%s: Error, kzalloc\n", __func__); 2030 rc = -ENOMEM; 2031 goto error_free_cd; 2032 } 2033 2034 /* Initialize device info */ 2035 cd->dev = dev; 2036 cd->pdata = pdata; 2037 cd->cpdata = pdata->core_pdata; 2038 cd->bus_ops = ops; 2039 2040 /* Initialize mutexes and spinlocks */ 2041 mutex_init(&cd->system_lock); 2042 mutex_init(&cd->adap_lock); 2043 2044 /* Initialize wait queue */ 2045 init_waitqueue_head(&cd->wait_q); 2046 2047 /* Initialize works */ 2048 INIT_WORK(&cd->startup_work, cyttsp4_startup_work_function); 2049 INIT_WORK(&cd->watchdog_work, cyttsp4_watchdog_work); 2050 2051 /* Initialize IRQ */ 2052 cd->irq = gpio_to_irq(cd->cpdata->irq_gpio); 2053 if (cd->irq < 0) { 2054 rc = -EINVAL; 2055 goto error_free_xfer; 2056 } 2057 2058 dev_set_drvdata(dev, cd); 2059 2060 /* Call platform init function */ 2061 if (cd->cpdata->init) { 2062 dev_dbg(cd->dev, "%s: Init HW\n", __func__); 2063 rc = cd->cpdata->init(cd->cpdata, 1, cd->dev); 2064 } else { 2065 dev_dbg(cd->dev, "%s: No HW INIT function\n", __func__); 2066 rc = 0; 2067 } 2068 if (rc < 0) 2069 dev_err(cd->dev, "%s: HW Init fail r=%d\n", __func__, rc); 2070 2071 dev_dbg(dev, "%s: initialize threaded irq=%d\n", __func__, cd->irq); 2072 if (cd->cpdata->level_irq_udelay > 0) 2073 /* use level triggered interrupts */ 2074 irq_flags = IRQF_TRIGGER_LOW | IRQF_ONESHOT; 2075 else 2076 /* use edge triggered interrupts */ 2077 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT; 2078 2079 rc = request_threaded_irq(cd->irq, NULL, cyttsp4_irq, irq_flags, 2080 dev_name(dev), cd); 2081 if (rc < 0) { 2082 dev_err(dev, "%s: Error, could not request irq\n", __func__); 2083 goto error_request_irq; 2084 } 2085 2086 /* Setup watchdog timer */ 2087 timer_setup(&cd->watchdog_timer, cyttsp4_watchdog_timer, 0); 2088 2089 /* 2090 * call startup directly to ensure that the device 2091 * is tested before leaving the probe 2092 */ 2093 rc = cyttsp4_startup(cd); 2094 2095 /* Do not fail probe if startup fails but the device is detected */ 2096 if (rc < 0 && cd->mode == CY_MODE_UNKNOWN) { 2097 dev_err(cd->dev, "%s: Fail initial startup r=%d\n", 2098 __func__, rc); 2099 goto error_startup; 2100 } 2101 2102 rc = cyttsp4_mt_probe(cd); 2103 if (rc < 0) { 2104 dev_err(dev, "%s: Error, fail mt probe\n", __func__); 2105 goto error_startup; 2106 } 2107 2108 pm_runtime_enable(dev); 2109 2110 return cd; 2111 2112 error_startup: 2113 cancel_work_sync(&cd->startup_work); 2114 cyttsp4_stop_wd_timer(cd); 2115 pm_runtime_disable(dev); 2116 cyttsp4_free_si_ptrs(cd); 2117 free_irq(cd->irq, cd); 2118 error_request_irq: 2119 if (cd->cpdata->init) 2120 cd->cpdata->init(cd->cpdata, 0, dev); 2121 error_free_xfer: 2122 kfree(cd->xfer_buf); 2123 error_free_cd: 2124 kfree(cd); 2125 error_alloc_data: 2126 error_no_pdata: 2127 dev_err(dev, "%s failed.\n", __func__); 2128 return ERR_PTR(rc); 2129 } 2130 EXPORT_SYMBOL_GPL(cyttsp4_probe); 2131 2132 static void cyttsp4_mt_release(struct cyttsp4_mt_data *md) 2133 { 2134 input_unregister_device(md->input); 2135 input_set_drvdata(md->input, NULL); 2136 } 2137 2138 int cyttsp4_remove(struct cyttsp4 *cd) 2139 { 2140 struct device *dev = cd->dev; 2141 2142 cyttsp4_mt_release(&cd->md); 2143 2144 /* 2145 * Suspend the device before freeing the startup_work and stopping 2146 * the watchdog since sleep function restarts watchdog on failure 2147 */ 2148 pm_runtime_suspend(dev); 2149 pm_runtime_disable(dev); 2150 2151 cancel_work_sync(&cd->startup_work); 2152 2153 cyttsp4_stop_wd_timer(cd); 2154 2155 free_irq(cd->irq, cd); 2156 if (cd->cpdata->init) 2157 cd->cpdata->init(cd->cpdata, 0, dev); 2158 cyttsp4_free_si_ptrs(cd); 2159 kfree(cd); 2160 return 0; 2161 } 2162 EXPORT_SYMBOL_GPL(cyttsp4_remove); 2163 2164 MODULE_LICENSE("GPL"); 2165 MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen core driver"); 2166 MODULE_AUTHOR("Cypress"); 2167