1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Parade TrueTouch(TM) Standard Product V5 Module. 4 * 5 * Copyright (C) 2015 Parade Technologies 6 * Copyright (C) 2012-2015 Cypress Semiconductor 7 * Copyright (C) 2018 Bootlin 8 * 9 * Authors: Mylène Josserand <mylene.josserand@bootlin.com> 10 * Alistair Francis <alistair@alistair23.me> 11 */ 12 13 #include <linux/crc-itu-t.h> 14 #include <linux/delay.h> 15 #include <linux/device.h> 16 #include <linux/gpio/consumer.h> 17 #include <linux/input/mt.h> 18 #include <linux/input/touchscreen.h> 19 #include <linux/interrupt.h> 20 #include <linux/i2c.h> 21 #include <linux/module.h> 22 #include <linux/regmap.h> 23 #include <linux/unaligned.h> 24 25 #define CYTTSP5_NAME "cyttsp5" 26 #define CY_I2C_DATA_SIZE (2 * 256) 27 #define HID_VERSION 0x0100 28 #define CY_MAX_INPUT 512 29 #define CYTTSP5_PREALLOCATED_CMD_BUFFER 32 30 #define CY_BITS_PER_BTN 1 31 #define CY_NUM_BTN_EVENT_ID GENMASK(CY_BITS_PER_BTN - 1, 0) 32 33 #define MAX_AREA 255 34 #define HID_OUTPUT_BL_SOP 0x1 35 #define HID_OUTPUT_BL_EOP 0x17 36 #define HID_OUTPUT_BL_LAUNCH_APP 0x3B 37 #define HID_OUTPUT_BL_LAUNCH_APP_SIZE 11 38 #define HID_OUTPUT_GET_SYSINFO 0x2 39 #define HID_OUTPUT_GET_SYSINFO_SIZE 5 40 #define HID_OUTPUT_MAX_CMD_SIZE 12 41 42 #define HID_DESC_REG 0x1 43 #define HID_INPUT_REG 0x3 44 #define HID_OUTPUT_REG 0x4 45 #define HID_COMMAND_REG 0x5 46 47 #define REPORT_ID_TOUCH 0x1 48 #define REPORT_ID_BTN 0x3 49 #define REPORT_SIZE_5 5 50 #define REPORT_SIZE_8 8 51 #define REPORT_SIZE_16 16 52 53 /* Touch reports offsets */ 54 /* Header offsets */ 55 #define TOUCH_REPORT_DESC_HDR_CONTACTCOUNT 16 56 /* Record offsets */ 57 #define TOUCH_REPORT_DESC_CONTACTID 8 58 #define TOUCH_REPORT_DESC_X 16 59 #define TOUCH_REPORT_DESC_Y 32 60 #define TOUCH_REPORT_DESC_P 48 61 #define TOUCH_REPORT_DESC_MAJ 56 62 #define TOUCH_REPORT_DESC_MIN 64 63 64 /* HID */ 65 #define HID_TOUCH_REPORT_ID 0x1 66 #define HID_BTN_REPORT_ID 0x3 67 #define HID_APP_RESPONSE_REPORT_ID 0x1F 68 #define HID_APP_OUTPUT_REPORT_ID 0x2F 69 #define HID_BL_RESPONSE_REPORT_ID 0x30 70 #define HID_BL_OUTPUT_REPORT_ID 0x40 71 #define HID_RESPONSE_REPORT_ID 0xF0 72 73 #define HID_OUTPUT_RESPONSE_REPORT_OFFSET 2 74 #define HID_OUTPUT_RESPONSE_CMD_OFFSET 4 75 #define HID_OUTPUT_RESPONSE_CMD_MASK GENMASK(6, 0) 76 77 #define HID_SYSINFO_SENSING_OFFSET 33 78 #define HID_SYSINFO_BTN_OFFSET 48 79 #define HID_SYSINFO_BTN_MASK GENMASK(7, 0) 80 #define HID_SYSINFO_MAX_BTN 8 81 82 #define HID_CMD_SET_POWER 0x8 83 84 #define HID_POWER_ON 0x0 85 #define HID_POWER_SLEEP 0x1 86 87 #define CY_HID_OUTPUT_TIMEOUT_MS 200 88 #define CY_HID_OUTPUT_GET_SYSINFO_TIMEOUT_MS 3000 89 #define CY_HID_GET_HID_DESCRIPTOR_TIMEOUT_MS 4000 90 #define CY_HID_SET_POWER_TIMEOUT 500 91 92 /* maximum number of concurrent tracks */ 93 #define TOUCH_REPORT_SIZE 10 94 #define TOUCH_INPUT_HEADER_SIZE 7 95 #define BTN_REPORT_SIZE 9 96 #define BTN_INPUT_HEADER_SIZE 5 97 98 #define MAX_CY_TCH_T_IDS 32 99 100 /* All usage pages for Touch Report */ 101 #define TOUCH_REPORT_USAGE_PG_X 0x00010030 102 #define TOUCH_REPORT_USAGE_PG_Y 0x00010031 103 #define TOUCH_REPORT_USAGE_PG_P 0x000D0030 104 #define TOUCH_REPORT_USAGE_PG_CONTACTID 0x000D0051 105 #define TOUCH_REPORT_USAGE_PG_CONTACTCOUNT 0x000D0054 106 #define TOUCH_REPORT_USAGE_PG_MAJ 0xFF010062 107 #define TOUCH_REPORT_USAGE_PG_MIN 0xFF010063 108 #define TOUCH_COL_USAGE_PG 0x000D0022 109 110 #define SET_CMD_LOW(byte, bits) \ 111 ((byte) = (((byte) & 0xF0) | ((bits) & 0x0F))) 112 #define SET_CMD_HIGH(byte, bits)\ 113 ((byte) = (((byte) & 0x0F) | ((bits) & 0xF0))) 114 #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode) 115 #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4)) 116 #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id) 117 118 /* System Information interface definitions */ 119 struct cyttsp5_sensing_conf_data_dev { 120 u8 electrodes_x; 121 u8 electrodes_y; 122 __le16 len_x; 123 __le16 len_y; 124 __le16 res_x; 125 __le16 res_y; 126 __le16 max_z; 127 u8 origin_x; 128 u8 origin_y; 129 u8 panel_id; 130 u8 btn; 131 u8 scan_mode; 132 u8 max_num_of_tch_per_refresh_cycle; 133 } __packed; 134 135 struct cyttsp5_sensing_conf_data { 136 u16 res_x; 137 u16 res_y; 138 u16 max_z; 139 u16 len_x; 140 u16 len_y; 141 u8 origin_x; 142 u8 origin_y; 143 u8 max_tch; 144 }; 145 146 enum cyttsp5_tch_abs { /* for ordering within the extracted touch data array */ 147 CY_TCH_X, /* X */ 148 CY_TCH_Y, /* Y */ 149 CY_TCH_P, /* P (Z) */ 150 CY_TCH_T, /* TOUCH ID */ 151 CY_TCH_MAJ, /* TOUCH_MAJOR */ 152 CY_TCH_MIN, /* TOUCH_MINOR */ 153 CY_TCH_NUM_ABS 154 }; 155 156 struct cyttsp5_tch_abs_params { 157 size_t ofs; /* abs byte offset */ 158 size_t size; /* size in bits */ 159 size_t min; /* min value */ 160 size_t max; /* max value */ 161 size_t bofs; /* bit offset */ 162 }; 163 164 struct cyttsp5_touch { 165 int abs[CY_TCH_NUM_ABS]; 166 }; 167 168 struct cyttsp5_sysinfo { 169 struct cyttsp5_sensing_conf_data sensing_conf_data; 170 int num_btns; 171 struct cyttsp5_tch_abs_params tch_hdr; 172 struct cyttsp5_tch_abs_params tch_abs[CY_TCH_NUM_ABS]; 173 u32 key_code[HID_SYSINFO_MAX_BTN]; 174 }; 175 176 struct cyttsp5_hid_desc { 177 __le16 hid_desc_len; 178 u8 packet_id; 179 u8 reserved_byte; 180 __le16 bcd_version; 181 __le16 report_desc_len; 182 __le16 report_desc_register; 183 __le16 input_register; 184 __le16 max_input_len; 185 __le16 output_register; 186 __le16 max_output_len; 187 __le16 command_register; 188 __le16 data_register; 189 __le16 vendor_id; 190 __le16 product_id; 191 __le16 version_id; 192 u8 reserved[4]; 193 } __packed; 194 195 struct cyttsp5 { 196 struct device *dev; 197 struct completion cmd_done; 198 struct cyttsp5_sysinfo sysinfo; 199 struct cyttsp5_hid_desc hid_desc; 200 u8 cmd_buf[CYTTSP5_PREALLOCATED_CMD_BUFFER]; 201 u8 input_buf[CY_MAX_INPUT]; 202 u8 response_buf[CY_MAX_INPUT]; 203 struct gpio_desc *reset_gpio; 204 struct input_dev *input; 205 char phys[NAME_MAX]; 206 int num_prv_rec; 207 struct regmap *regmap; 208 struct touchscreen_properties prop; 209 struct regulator_bulk_data supplies[2]; 210 }; 211 212 /* 213 * For what is understood in the datasheet, the register does not 214 * matter. For consistency, use the Input Register address 215 * but it does mean anything to the device. The important data 216 * to send is the I2C address 217 */ 218 static int cyttsp5_read(struct cyttsp5 *ts, u8 *buf, u32 max) 219 { 220 int error; 221 u32 size; 222 u8 temp[2]; 223 224 /* Read the frame to retrieve the size */ 225 error = regmap_bulk_read(ts->regmap, HID_INPUT_REG, temp, sizeof(temp)); 226 if (error) 227 return error; 228 229 size = get_unaligned_le16(temp); 230 if (!size || size == 2) 231 return 0; 232 233 if (size > max) 234 return -EINVAL; 235 236 /* Get the real value */ 237 return regmap_bulk_read(ts->regmap, HID_INPUT_REG, buf, size); 238 } 239 240 static int cyttsp5_write(struct cyttsp5 *ts, unsigned int reg, u8 *data, 241 size_t size) 242 { 243 u8 cmd[HID_OUTPUT_MAX_CMD_SIZE]; 244 245 if (size + 1 > HID_OUTPUT_MAX_CMD_SIZE) 246 return -E2BIG; 247 248 /* High bytes of register address needed as first byte of cmd */ 249 cmd[0] = (reg >> 8) & 0xFF; 250 251 /* Copy the rest of the data */ 252 if (data) 253 memcpy(&cmd[1], data, size); 254 255 /* 256 * The hardware wants to receive a frame with the address register 257 * contained in the first two bytes. As the regmap_write function 258 * add the register adresse in the frame, we use the low byte as 259 * first frame byte for the address register and the first 260 * data byte is the high register + left of the cmd to send 261 */ 262 return regmap_bulk_write(ts->regmap, reg & 0xFF, cmd, size + 1); 263 } 264 265 static void cyttsp5_get_touch_axis(int *axis, int size, int max, u8 *xy_data, 266 int bofs) 267 { 268 int nbyte; 269 270 for (nbyte = 0, *axis = 0; nbyte < size; nbyte++) 271 *axis += ((xy_data[nbyte] >> bofs) << (nbyte * 8)); 272 273 *axis &= max - 1; 274 } 275 276 static void cyttsp5_get_touch_record(struct cyttsp5 *ts, 277 struct cyttsp5_touch *touch, u8 *xy_data) 278 { 279 struct cyttsp5_sysinfo *si = &ts->sysinfo; 280 enum cyttsp5_tch_abs abs; 281 282 for (abs = CY_TCH_X; abs < CY_TCH_NUM_ABS; abs++) 283 cyttsp5_get_touch_axis(&touch->abs[abs], 284 si->tch_abs[abs].size, 285 si->tch_abs[abs].max, 286 xy_data + si->tch_abs[abs].ofs, 287 si->tch_abs[abs].bofs); 288 } 289 290 static void cyttsp5_get_mt_touches(struct cyttsp5 *ts, 291 struct cyttsp5_touch *tch, int num_cur_tch) 292 { 293 struct cyttsp5_sysinfo *si = &ts->sysinfo; 294 int i, t = 0, offset = 0; 295 DECLARE_BITMAP(ids, MAX_CY_TCH_T_IDS); 296 u8 *tch_addr; 297 int tmp; 298 299 bitmap_zero(ids, MAX_CY_TCH_T_IDS); 300 memset(tch->abs, 0, sizeof(tch->abs)); 301 302 switch (ts->input_buf[2]) { 303 case HID_TOUCH_REPORT_ID: 304 offset = TOUCH_INPUT_HEADER_SIZE; 305 break; 306 case HID_BTN_REPORT_ID: 307 offset = BTN_INPUT_HEADER_SIZE; 308 break; 309 } 310 311 for (i = 0; i < num_cur_tch; i++) { 312 tch_addr = ts->input_buf + offset + (i * TOUCH_REPORT_SIZE); 313 cyttsp5_get_touch_record(ts, tch, tch_addr); 314 315 /* Convert MAJOR/MINOR from mm to resolution */ 316 tmp = tch->abs[CY_TCH_MAJ] * 100 * si->sensing_conf_data.res_x; 317 tch->abs[CY_TCH_MAJ] = tmp / si->sensing_conf_data.len_x; 318 tmp = tch->abs[CY_TCH_MIN] * 100 * si->sensing_conf_data.res_x; 319 tch->abs[CY_TCH_MIN] = tmp / si->sensing_conf_data.len_x; 320 321 t = tch->abs[CY_TCH_T]; 322 input_mt_slot(ts->input, t); 323 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true); 324 __set_bit(t, ids); 325 326 /* position and pressure fields */ 327 touchscreen_report_pos(ts->input, &ts->prop, 328 tch->abs[CY_TCH_X], tch->abs[CY_TCH_Y], 329 true); 330 input_report_abs(ts->input, ABS_MT_PRESSURE, 331 tch->abs[CY_TCH_P]); 332 333 /* Get the extended touch fields */ 334 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, 335 tch->abs[CY_TCH_MAJ]); 336 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, 337 tch->abs[CY_TCH_MIN]); 338 } 339 340 ts->num_prv_rec = num_cur_tch; 341 } 342 343 static int cyttsp5_mt_attention(struct device *dev) 344 { 345 struct cyttsp5 *ts = dev_get_drvdata(dev); 346 struct cyttsp5_sysinfo *si = &ts->sysinfo; 347 int max_tch = si->sensing_conf_data.max_tch; 348 struct cyttsp5_touch tch; 349 int num_cur_tch; 350 351 cyttsp5_get_touch_axis(&num_cur_tch, si->tch_hdr.size, 352 si->tch_hdr.max, 353 ts->input_buf + 3 + si->tch_hdr.ofs, 354 si->tch_hdr.bofs); 355 356 if (num_cur_tch > max_tch) { 357 dev_err(dev, "Num touch err detected (n=%d)\n", num_cur_tch); 358 num_cur_tch = max_tch; 359 } 360 361 if (num_cur_tch == 0 && ts->num_prv_rec == 0) 362 return 0; 363 364 /* extract xy_data for all currently reported touches */ 365 if (num_cur_tch) 366 cyttsp5_get_mt_touches(ts, &tch, num_cur_tch); 367 368 input_mt_sync_frame(ts->input); 369 input_sync(ts->input); 370 371 return 0; 372 } 373 374 static int cyttsp5_setup_input_device(struct device *dev) 375 { 376 struct cyttsp5 *ts = dev_get_drvdata(dev); 377 struct cyttsp5_sysinfo *si = &ts->sysinfo; 378 int max_x, max_y, max_p; 379 int max_x_tmp, max_y_tmp; 380 int error; 381 382 max_x_tmp = si->sensing_conf_data.res_x; 383 max_y_tmp = si->sensing_conf_data.res_y; 384 max_x = max_x_tmp - 1; 385 max_y = max_y_tmp - 1; 386 max_p = si->sensing_conf_data.max_z; 387 388 input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, max_x, 0, 0); 389 input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, max_y, 0, 0); 390 input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, max_p, 0, 0); 391 392 input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, MAX_AREA, 0, 0); 393 input_set_abs_params(ts->input, ABS_MT_TOUCH_MINOR, 0, MAX_AREA, 0, 0); 394 395 error = input_mt_init_slots(ts->input, si->tch_abs[CY_TCH_T].max, 396 INPUT_MT_DROP_UNUSED | INPUT_MT_DIRECT); 397 if (error) 398 return error; 399 400 error = input_register_device(ts->input); 401 if (error) { 402 dev_err(dev, "failed to register input device: %d\n", error); 403 return error; 404 } 405 406 return error; 407 } 408 409 static int cyttsp5_parse_dt_key_code(struct device *dev) 410 { 411 struct cyttsp5 *ts = dev_get_drvdata(dev); 412 struct cyttsp5_sysinfo *si = &ts->sysinfo; 413 414 if (!si->num_btns) 415 return 0; 416 417 /* Initialize the button to RESERVED */ 418 memset32(si->key_code, KEY_RESERVED, si->num_btns); 419 420 return device_property_read_u32_array(dev, "linux,keycodes", 421 si->key_code, si->num_btns); 422 } 423 424 static int cyttsp5_btn_attention(struct device *dev) 425 { 426 struct cyttsp5 *ts = dev_get_drvdata(dev); 427 struct cyttsp5_sysinfo *si = &ts->sysinfo; 428 int cur_btn, offset = 0; 429 int cur_btn_state; 430 431 switch (ts->input_buf[2]) { 432 case HID_TOUCH_REPORT_ID: 433 offset = TOUCH_INPUT_HEADER_SIZE; 434 break; 435 case HID_BTN_REPORT_ID: 436 offset = BTN_INPUT_HEADER_SIZE; 437 break; 438 } 439 440 if (ts->input_buf[2] != HID_BTN_REPORT_ID) 441 return 0; 442 443 /* extract button press/release touch information */ 444 for (cur_btn = 0; cur_btn < si->num_btns; cur_btn++) { 445 /* Get current button state */ 446 cur_btn_state = (ts->input_buf[offset] >> (cur_btn * CY_BITS_PER_BTN)) 447 & CY_NUM_BTN_EVENT_ID; 448 449 input_report_key(ts->input, si->key_code[cur_btn], 450 cur_btn_state); 451 input_sync(ts->input); 452 } 453 454 return 0; 455 } 456 457 static int cyttsp5_validate_cmd_response(struct cyttsp5 *ts, u8 code) 458 { 459 u16 size, crc; 460 u8 status, report_id; 461 int command_code; 462 463 size = get_unaligned_le16(&ts->response_buf[0]); 464 if (!size) 465 return 0; 466 467 report_id = ts->response_buf[HID_OUTPUT_RESPONSE_REPORT_OFFSET]; 468 469 switch (report_id) { 470 case HID_BL_RESPONSE_REPORT_ID: 471 if (ts->response_buf[4] != HID_OUTPUT_BL_SOP) { 472 dev_err(ts->dev, "HID output response, wrong SOP\n"); 473 return -EPROTO; 474 } 475 476 if (ts->response_buf[size - 1] != HID_OUTPUT_BL_EOP) { 477 dev_err(ts->dev, "HID output response, wrong EOP\n"); 478 return -EPROTO; 479 } 480 481 crc = crc_itu_t(0xFFFF, &ts->response_buf[4], size - 7); 482 if (get_unaligned_le16(&ts->response_buf[size - 3]) != crc) { 483 dev_err(ts->dev, 484 "HID output response, wrong CRC 0x%X\n", 485 crc); 486 return -EPROTO; 487 } 488 489 status = ts->response_buf[5]; 490 if (status) { 491 dev_err(ts->dev, "HID output response, ERROR:%d\n", 492 status); 493 return -EPROTO; 494 } 495 break; 496 497 case HID_APP_RESPONSE_REPORT_ID: 498 command_code = ts->response_buf[HID_OUTPUT_RESPONSE_CMD_OFFSET] 499 & HID_OUTPUT_RESPONSE_CMD_MASK; 500 if (command_code != code) { 501 dev_err(ts->dev, 502 "HID output response, wrong command_code:%X\n", 503 command_code); 504 return -EPROTO; 505 } 506 break; 507 } 508 509 return 0; 510 } 511 512 static void cyttsp5_si_get_btn_data(struct cyttsp5 *ts) 513 { 514 struct cyttsp5_sysinfo *si = &ts->sysinfo; 515 unsigned int btns = ts->response_buf[HID_SYSINFO_BTN_OFFSET] & 516 HID_SYSINFO_BTN_MASK; 517 518 si->num_btns = hweight8(btns); 519 } 520 521 static int cyttsp5_get_sysinfo_regs(struct cyttsp5 *ts) 522 { 523 struct cyttsp5_sensing_conf_data *scd = &ts->sysinfo.sensing_conf_data; 524 struct cyttsp5_sensing_conf_data_dev *scd_dev = 525 (struct cyttsp5_sensing_conf_data_dev *) 526 &ts->response_buf[HID_SYSINFO_SENSING_OFFSET]; 527 528 cyttsp5_si_get_btn_data(ts); 529 530 scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle; 531 scd->res_x = get_unaligned_le16(&scd_dev->res_x); 532 scd->res_y = get_unaligned_le16(&scd_dev->res_y); 533 scd->max_z = get_unaligned_le16(&scd_dev->max_z); 534 scd->len_x = get_unaligned_le16(&scd_dev->len_x); 535 scd->len_y = get_unaligned_le16(&scd_dev->len_y); 536 537 return 0; 538 } 539 540 static int cyttsp5_hid_output_get_sysinfo(struct cyttsp5 *ts) 541 { 542 int rc; 543 u8 cmd[HID_OUTPUT_GET_SYSINFO_SIZE]; 544 545 /* HI bytes of Output register address */ 546 put_unaligned_le16(HID_OUTPUT_GET_SYSINFO_SIZE, cmd); 547 cmd[2] = HID_APP_OUTPUT_REPORT_ID; 548 cmd[3] = 0x0; /* Reserved */ 549 cmd[4] = HID_OUTPUT_GET_SYSINFO; 550 551 rc = cyttsp5_write(ts, HID_OUTPUT_REG, cmd, 552 HID_OUTPUT_GET_SYSINFO_SIZE); 553 if (rc) { 554 dev_err(ts->dev, "Failed to write command %d", rc); 555 return rc; 556 } 557 558 rc = wait_for_completion_interruptible_timeout(&ts->cmd_done, 559 msecs_to_jiffies(CY_HID_OUTPUT_GET_SYSINFO_TIMEOUT_MS)); 560 if (rc <= 0) { 561 dev_err(ts->dev, "HID output cmd execution timed out\n"); 562 rc = -ETIMEDOUT; 563 return rc; 564 } 565 566 rc = cyttsp5_validate_cmd_response(ts, HID_OUTPUT_GET_SYSINFO); 567 if (rc) { 568 dev_err(ts->dev, "Validation of the response failed\n"); 569 return rc; 570 } 571 572 return cyttsp5_get_sysinfo_regs(ts); 573 } 574 575 static int cyttsp5_power_control(struct cyttsp5 *ts, bool on) 576 { 577 u8 state = on ? HID_POWER_ON : HID_POWER_SLEEP; 578 u8 cmd[2] = { 0 }; 579 int rc; 580 581 SET_CMD_REPORT_TYPE(cmd[0], 0); 582 SET_CMD_REPORT_ID(cmd[0], state); 583 SET_CMD_OPCODE(cmd[1], HID_CMD_SET_POWER); 584 585 rc = cyttsp5_write(ts, HID_COMMAND_REG, cmd, sizeof(cmd)); 586 if (rc) { 587 dev_err(ts->dev, "Failed to write power command %d", rc); 588 return rc; 589 } 590 591 rc = wait_for_completion_interruptible_timeout(&ts->cmd_done, 592 msecs_to_jiffies(CY_HID_SET_POWER_TIMEOUT)); 593 if (rc <= 0) { 594 dev_err(ts->dev, "HID power cmd execution timed out\n"); 595 return -ETIMEDOUT; 596 } 597 598 if (ts->response_buf[2] != HID_RESPONSE_REPORT_ID || 599 (ts->response_buf[3] & 0x03) != state || 600 (ts->response_buf[4] & 0x0f) != HID_CMD_SET_POWER) { 601 dev_err(ts->dev, "Validation of the %s response failed\n", 602 on ? "wakeup" : "sleep"); 603 return -EINVAL; 604 } 605 606 return 0; 607 } 608 609 static int cyttsp5_hid_output_bl_launch_app(struct cyttsp5 *ts) 610 { 611 int rc; 612 u8 cmd[HID_OUTPUT_BL_LAUNCH_APP_SIZE]; 613 u16 crc; 614 615 put_unaligned_le16(HID_OUTPUT_BL_LAUNCH_APP_SIZE, cmd); 616 cmd[2] = HID_BL_OUTPUT_REPORT_ID; 617 cmd[3] = 0x0; /* Reserved */ 618 cmd[4] = HID_OUTPUT_BL_SOP; 619 cmd[5] = HID_OUTPUT_BL_LAUNCH_APP; 620 put_unaligned_le16(0x00, &cmd[6]); 621 crc = crc_itu_t(0xFFFF, &cmd[4], 4); 622 put_unaligned_le16(crc, &cmd[8]); 623 cmd[10] = HID_OUTPUT_BL_EOP; 624 625 rc = cyttsp5_write(ts, HID_OUTPUT_REG, cmd, 626 HID_OUTPUT_BL_LAUNCH_APP_SIZE); 627 if (rc) { 628 dev_err(ts->dev, "Failed to write command %d", rc); 629 return rc; 630 } 631 632 rc = wait_for_completion_interruptible_timeout(&ts->cmd_done, 633 msecs_to_jiffies(CY_HID_OUTPUT_TIMEOUT_MS)); 634 if (rc <= 0) { 635 dev_err(ts->dev, "HID output cmd execution timed out\n"); 636 rc = -ETIMEDOUT; 637 return rc; 638 } 639 640 rc = cyttsp5_validate_cmd_response(ts, HID_OUTPUT_BL_LAUNCH_APP); 641 if (rc) { 642 dev_err(ts->dev, "Validation of the response failed\n"); 643 return rc; 644 } 645 646 return 0; 647 } 648 649 static int cyttsp5_get_hid_descriptor(struct cyttsp5 *ts, 650 struct cyttsp5_hid_desc *desc) 651 { 652 struct device *dev = ts->dev; 653 int rc; 654 655 rc = cyttsp5_write(ts, HID_DESC_REG, NULL, 0); 656 if (rc) { 657 dev_err(dev, "Failed to get HID descriptor, rc=%d\n", rc); 658 return rc; 659 } 660 661 rc = wait_for_completion_interruptible_timeout(&ts->cmd_done, 662 msecs_to_jiffies(CY_HID_GET_HID_DESCRIPTOR_TIMEOUT_MS)); 663 if (rc <= 0) { 664 dev_err(ts->dev, "HID get descriptor timed out\n"); 665 rc = -ETIMEDOUT; 666 return rc; 667 } 668 669 memcpy(desc, ts->response_buf, sizeof(*desc)); 670 671 /* Check HID descriptor length and version */ 672 if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) || 673 le16_to_cpu(desc->bcd_version) != HID_VERSION) { 674 dev_err(dev, "Unsupported HID version\n"); 675 return -ENODEV; 676 } 677 678 return 0; 679 } 680 681 static int fill_tch_abs(struct cyttsp5_tch_abs_params *tch_abs, int report_size, 682 int offset) 683 { 684 tch_abs->ofs = offset / 8; 685 tch_abs->size = report_size / 8; 686 if (report_size % 8) 687 tch_abs->size += 1; 688 tch_abs->min = 0; 689 tch_abs->max = 1 << report_size; 690 tch_abs->bofs = offset - (tch_abs->ofs << 3); 691 692 return 0; 693 } 694 695 static irqreturn_t cyttsp5_handle_irq(int irq, void *handle) 696 { 697 struct cyttsp5 *ts = handle; 698 int report_id; 699 int size; 700 int error; 701 702 error = cyttsp5_read(ts, ts->input_buf, CY_MAX_INPUT); 703 if (error) 704 return IRQ_HANDLED; 705 706 size = get_unaligned_le16(&ts->input_buf[0]); 707 if (size == 0) { 708 /* reset */ 709 report_id = 0; 710 size = 2; 711 } else { 712 report_id = ts->input_buf[2]; 713 } 714 715 switch (report_id) { 716 case HID_TOUCH_REPORT_ID: 717 cyttsp5_mt_attention(ts->dev); 718 break; 719 case HID_BTN_REPORT_ID: 720 cyttsp5_btn_attention(ts->dev); 721 break; 722 case HID_RESPONSE_REPORT_ID: 723 memcpy(ts->response_buf, ts->input_buf, size); 724 complete(&ts->cmd_done); 725 break; 726 default: 727 /* It is not an input but a command response */ 728 memcpy(ts->response_buf, ts->input_buf, size); 729 complete(&ts->cmd_done); 730 } 731 732 return IRQ_HANDLED; 733 } 734 735 static int cyttsp5_deassert_int(struct cyttsp5 *ts) 736 { 737 u16 size; 738 u8 buf[2]; 739 int error; 740 741 error = regmap_bulk_read(ts->regmap, HID_INPUT_REG, buf, sizeof(buf)); 742 if (error < 0) 743 return error; 744 745 size = get_unaligned_le16(&buf[0]); 746 if (size == 2 || size == 0) 747 return 0; 748 749 return -EINVAL; 750 } 751 752 static int cyttsp5_fill_all_touch(struct cyttsp5 *ts) 753 { 754 struct cyttsp5_sysinfo *si = &ts->sysinfo; 755 756 fill_tch_abs(&si->tch_abs[CY_TCH_X], REPORT_SIZE_16, 757 TOUCH_REPORT_DESC_X); 758 fill_tch_abs(&si->tch_abs[CY_TCH_Y], REPORT_SIZE_16, 759 TOUCH_REPORT_DESC_Y); 760 fill_tch_abs(&si->tch_abs[CY_TCH_P], REPORT_SIZE_8, 761 TOUCH_REPORT_DESC_P); 762 fill_tch_abs(&si->tch_abs[CY_TCH_T], REPORT_SIZE_5, 763 TOUCH_REPORT_DESC_CONTACTID); 764 fill_tch_abs(&si->tch_hdr, REPORT_SIZE_5, 765 TOUCH_REPORT_DESC_HDR_CONTACTCOUNT); 766 fill_tch_abs(&si->tch_abs[CY_TCH_MAJ], REPORT_SIZE_8, 767 TOUCH_REPORT_DESC_MAJ); 768 fill_tch_abs(&si->tch_abs[CY_TCH_MIN], REPORT_SIZE_8, 769 TOUCH_REPORT_DESC_MIN); 770 771 return 0; 772 } 773 774 static int cyttsp5_startup(struct cyttsp5 *ts) 775 { 776 int error; 777 778 error = cyttsp5_deassert_int(ts); 779 if (error) { 780 dev_err(ts->dev, "Error on deassert int r=%d\n", error); 781 return -ENODEV; 782 } 783 784 /* 785 * Launch the application as the device starts in bootloader mode 786 * because of a power-on-reset 787 */ 788 error = cyttsp5_hid_output_bl_launch_app(ts); 789 if (error < 0) { 790 dev_err(ts->dev, "Error on launch app r=%d\n", error); 791 return error; 792 } 793 794 error = cyttsp5_get_hid_descriptor(ts, &ts->hid_desc); 795 if (error < 0) { 796 dev_err(ts->dev, "Error on getting HID descriptor r=%d\n", error); 797 return error; 798 } 799 800 error = cyttsp5_fill_all_touch(ts); 801 if (error < 0) { 802 dev_err(ts->dev, "Error on report descriptor r=%d\n", error); 803 return error; 804 } 805 806 error = cyttsp5_hid_output_get_sysinfo(ts); 807 if (error) { 808 dev_err(ts->dev, "Error on getting sysinfo r=%d\n", error); 809 return error; 810 } 811 812 return error; 813 } 814 815 static void cyttsp5_cleanup(void *data) 816 { 817 struct cyttsp5 *ts = data; 818 819 regulator_bulk_disable(ARRAY_SIZE(ts->supplies), ts->supplies); 820 } 821 822 static int cyttsp5_probe(struct device *dev, struct regmap *regmap, int irq, 823 const char *name) 824 { 825 struct cyttsp5 *ts; 826 struct cyttsp5_sysinfo *si; 827 int error, i; 828 829 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL); 830 if (!ts) 831 return -ENOMEM; 832 833 /* Initialize device info */ 834 ts->regmap = regmap; 835 ts->dev = dev; 836 si = &ts->sysinfo; 837 dev_set_drvdata(dev, ts); 838 839 init_completion(&ts->cmd_done); 840 841 /* Power up the device */ 842 ts->supplies[0].supply = "vdd"; 843 ts->supplies[1].supply = "vddio"; 844 error = devm_regulator_bulk_get(dev, ARRAY_SIZE(ts->supplies), 845 ts->supplies); 846 if (error) { 847 dev_err(ts->dev, "Failed to get regulators, error %d\n", error); 848 return error; 849 } 850 851 error = devm_add_action_or_reset(dev, cyttsp5_cleanup, ts); 852 if (error) 853 return error; 854 855 error = regulator_bulk_enable(ARRAY_SIZE(ts->supplies), ts->supplies); 856 if (error) { 857 dev_err(ts->dev, "Failed to enable regulators, error %d\n", error); 858 return error; 859 } 860 861 ts->input = devm_input_allocate_device(dev); 862 if (!ts->input) { 863 dev_err(dev, "Error, failed to allocate input device\n"); 864 return -ENODEV; 865 } 866 867 ts->input->name = "cyttsp5"; 868 scnprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev)); 869 ts->input->phys = ts->phys; 870 input_set_drvdata(ts->input, ts); 871 872 /* Assert gpio to be in a reset state */ 873 ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 874 if (IS_ERR(ts->reset_gpio)) { 875 error = PTR_ERR(ts->reset_gpio); 876 dev_err(dev, "Failed to request reset gpio, error %d\n", error); 877 return error; 878 } 879 880 fsleep(10); /* Ensure long-enough reset pulse (minimum 10us). */ 881 882 gpiod_set_value_cansleep(ts->reset_gpio, 0); 883 884 /* Need a delay to have device up */ 885 msleep(20); 886 887 error = devm_request_threaded_irq(dev, irq, NULL, cyttsp5_handle_irq, 888 IRQF_ONESHOT, name, ts); 889 if (error) { 890 dev_err(dev, "unable to request IRQ\n"); 891 return error; 892 } 893 894 error = cyttsp5_startup(ts); 895 if (error) { 896 dev_err(ts->dev, "Fail initial startup r=%d\n", error); 897 return error; 898 } 899 900 error = cyttsp5_parse_dt_key_code(dev); 901 if (error < 0) { 902 dev_err(ts->dev, "Error while parsing dts %d\n", error); 903 return error; 904 } 905 906 touchscreen_parse_properties(ts->input, true, &ts->prop); 907 908 __set_bit(EV_KEY, ts->input->evbit); 909 for (i = 0; i < si->num_btns; i++) 910 __set_bit(si->key_code[i], ts->input->keybit); 911 912 return cyttsp5_setup_input_device(dev); 913 } 914 915 static int cyttsp5_i2c_probe(struct i2c_client *client) 916 { 917 struct regmap *regmap; 918 static const struct regmap_config config = { 919 .reg_bits = 8, 920 .val_bits = 8, 921 }; 922 923 regmap = devm_regmap_init_i2c(client, &config); 924 if (IS_ERR(regmap)) { 925 dev_err(&client->dev, "regmap allocation failed: %pe\n", 926 regmap); 927 return PTR_ERR(regmap); 928 } 929 930 return cyttsp5_probe(&client->dev, regmap, client->irq, client->name); 931 } 932 933 static const struct of_device_id cyttsp5_of_match[] = { 934 { .compatible = "cypress,tt21000", }, 935 { } 936 }; 937 MODULE_DEVICE_TABLE(of, cyttsp5_of_match); 938 939 static const struct i2c_device_id cyttsp5_i2c_id[] = { 940 { .name = CYTTSP5_NAME }, 941 { } 942 }; 943 MODULE_DEVICE_TABLE(i2c, cyttsp5_i2c_id); 944 945 static int __maybe_unused cyttsp5_suspend(struct device *dev) 946 { 947 struct cyttsp5 *ts = dev_get_drvdata(dev); 948 949 if (!device_may_wakeup(dev)) 950 cyttsp5_power_control(ts, false); 951 952 return 0; 953 } 954 955 static int __maybe_unused cyttsp5_resume(struct device *dev) 956 { 957 struct cyttsp5 *ts = dev_get_drvdata(dev); 958 959 if (!device_may_wakeup(dev)) 960 cyttsp5_power_control(ts, true); 961 962 return 0; 963 } 964 965 static SIMPLE_DEV_PM_OPS(cyttsp5_pm, cyttsp5_suspend, cyttsp5_resume); 966 967 static struct i2c_driver cyttsp5_i2c_driver = { 968 .driver = { 969 .name = CYTTSP5_NAME, 970 .of_match_table = cyttsp5_of_match, 971 .pm = &cyttsp5_pm, 972 }, 973 .probe = cyttsp5_i2c_probe, 974 .id_table = cyttsp5_i2c_id, 975 }; 976 module_i2c_driver(cyttsp5_i2c_driver); 977 978 MODULE_LICENSE("GPL"); 979 MODULE_DESCRIPTION("Touchscreen driver for Cypress TrueTouch Gen 5 Product"); 980 MODULE_AUTHOR("Mylène Josserand <mylene.josserand@bootlin.com>"); 981