1 /* 2 * Atmel maXTouch Touchscreen driver 3 * 4 * Copyright (C) 2010 Samsung Electronics Co.Ltd 5 * Copyright (C) 2011-2014 Atmel Corporation 6 * Copyright (C) 2012 Google, Inc. 7 * 8 * Author: Joonyoung Shim <jy0922.shim@samsung.com> 9 * 10 * This program is free software; you can redistribute it and/or modify it 11 * under the terms of the GNU General Public License as published by the 12 * Free Software Foundation; either version 2 of the License, or (at your 13 * option) any later version. 14 * 15 */ 16 17 #include <linux/module.h> 18 #include <linux/init.h> 19 #include <linux/completion.h> 20 #include <linux/delay.h> 21 #include <linux/firmware.h> 22 #include <linux/i2c.h> 23 #include <linux/i2c/atmel_mxt_ts.h> 24 #include <linux/input/mt.h> 25 #include <linux/interrupt.h> 26 #include <linux/of.h> 27 #include <linux/slab.h> 28 29 /* Version */ 30 #define MXT_VER_20 20 31 #define MXT_VER_21 21 32 #define MXT_VER_22 22 33 34 /* Firmware files */ 35 #define MXT_FW_NAME "maxtouch.fw" 36 #define MXT_CFG_NAME "maxtouch.cfg" 37 #define MXT_CFG_MAGIC "OBP_RAW V1" 38 39 /* Registers */ 40 #define MXT_INFO 0x00 41 #define MXT_FAMILY_ID 0x00 42 #define MXT_VARIANT_ID 0x01 43 #define MXT_VERSION 0x02 44 #define MXT_BUILD 0x03 45 #define MXT_MATRIX_X_SIZE 0x04 46 #define MXT_MATRIX_Y_SIZE 0x05 47 #define MXT_OBJECT_NUM 0x06 48 #define MXT_OBJECT_START 0x07 49 50 #define MXT_OBJECT_SIZE 6 51 #define MXT_INFO_CHECKSUM_SIZE 3 52 #define MXT_MAX_BLOCK_WRITE 256 53 54 /* Object types */ 55 #define MXT_DEBUG_DIAGNOSTIC_T37 37 56 #define MXT_GEN_MESSAGE_T5 5 57 #define MXT_GEN_COMMAND_T6 6 58 #define MXT_GEN_POWER_T7 7 59 #define MXT_GEN_ACQUIRE_T8 8 60 #define MXT_GEN_DATASOURCE_T53 53 61 #define MXT_TOUCH_MULTI_T9 9 62 #define MXT_TOUCH_KEYARRAY_T15 15 63 #define MXT_TOUCH_PROXIMITY_T23 23 64 #define MXT_TOUCH_PROXKEY_T52 52 65 #define MXT_PROCI_GRIPFACE_T20 20 66 #define MXT_PROCG_NOISE_T22 22 67 #define MXT_PROCI_ONETOUCH_T24 24 68 #define MXT_PROCI_TWOTOUCH_T27 27 69 #define MXT_PROCI_GRIP_T40 40 70 #define MXT_PROCI_PALM_T41 41 71 #define MXT_PROCI_TOUCHSUPPRESSION_T42 42 72 #define MXT_PROCI_STYLUS_T47 47 73 #define MXT_PROCG_NOISESUPPRESSION_T48 48 74 #define MXT_SPT_COMMSCONFIG_T18 18 75 #define MXT_SPT_GPIOPWM_T19 19 76 #define MXT_SPT_SELFTEST_T25 25 77 #define MXT_SPT_CTECONFIG_T28 28 78 #define MXT_SPT_USERDATA_T38 38 79 #define MXT_SPT_DIGITIZER_T43 43 80 #define MXT_SPT_MESSAGECOUNT_T44 44 81 #define MXT_SPT_CTECONFIG_T46 46 82 83 /* MXT_GEN_MESSAGE_T5 object */ 84 #define MXT_RPTID_NOMSG 0xff 85 86 /* MXT_GEN_COMMAND_T6 field */ 87 #define MXT_COMMAND_RESET 0 88 #define MXT_COMMAND_BACKUPNV 1 89 #define MXT_COMMAND_CALIBRATE 2 90 #define MXT_COMMAND_REPORTALL 3 91 #define MXT_COMMAND_DIAGNOSTIC 5 92 93 /* Define for T6 status byte */ 94 #define MXT_T6_STATUS_RESET (1 << 7) 95 #define MXT_T6_STATUS_OFL (1 << 6) 96 #define MXT_T6_STATUS_SIGERR (1 << 5) 97 #define MXT_T6_STATUS_CAL (1 << 4) 98 #define MXT_T6_STATUS_CFGERR (1 << 3) 99 #define MXT_T6_STATUS_COMSERR (1 << 2) 100 101 /* MXT_GEN_POWER_T7 field */ 102 struct t7_config { 103 u8 idle; 104 u8 active; 105 } __packed; 106 107 #define MXT_POWER_CFG_RUN 0 108 #define MXT_POWER_CFG_DEEPSLEEP 1 109 110 /* MXT_GEN_ACQUIRE_T8 field */ 111 #define MXT_ACQUIRE_CHRGTIME 0 112 #define MXT_ACQUIRE_TCHDRIFT 2 113 #define MXT_ACQUIRE_DRIFTST 3 114 #define MXT_ACQUIRE_TCHAUTOCAL 4 115 #define MXT_ACQUIRE_SYNC 5 116 #define MXT_ACQUIRE_ATCHCALST 6 117 #define MXT_ACQUIRE_ATCHCALSTHR 7 118 119 /* MXT_TOUCH_MULTI_T9 field */ 120 #define MXT_T9_ORIENT 9 121 #define MXT_T9_RANGE 18 122 123 /* MXT_TOUCH_MULTI_T9 status */ 124 #define MXT_T9_UNGRIP (1 << 0) 125 #define MXT_T9_SUPPRESS (1 << 1) 126 #define MXT_T9_AMP (1 << 2) 127 #define MXT_T9_VECTOR (1 << 3) 128 #define MXT_T9_MOVE (1 << 4) 129 #define MXT_T9_RELEASE (1 << 5) 130 #define MXT_T9_PRESS (1 << 6) 131 #define MXT_T9_DETECT (1 << 7) 132 133 struct t9_range { 134 u16 x; 135 u16 y; 136 } __packed; 137 138 /* MXT_TOUCH_MULTI_T9 orient */ 139 #define MXT_T9_ORIENT_SWITCH (1 << 0) 140 141 /* MXT_PROCI_GRIPFACE_T20 field */ 142 #define MXT_GRIPFACE_CTRL 0 143 #define MXT_GRIPFACE_XLOGRIP 1 144 #define MXT_GRIPFACE_XHIGRIP 2 145 #define MXT_GRIPFACE_YLOGRIP 3 146 #define MXT_GRIPFACE_YHIGRIP 4 147 #define MXT_GRIPFACE_MAXTCHS 5 148 #define MXT_GRIPFACE_SZTHR1 7 149 #define MXT_GRIPFACE_SZTHR2 8 150 #define MXT_GRIPFACE_SHPTHR1 9 151 #define MXT_GRIPFACE_SHPTHR2 10 152 #define MXT_GRIPFACE_SUPEXTTO 11 153 154 /* MXT_PROCI_NOISE field */ 155 #define MXT_NOISE_CTRL 0 156 #define MXT_NOISE_OUTFLEN 1 157 #define MXT_NOISE_GCAFUL_LSB 3 158 #define MXT_NOISE_GCAFUL_MSB 4 159 #define MXT_NOISE_GCAFLL_LSB 5 160 #define MXT_NOISE_GCAFLL_MSB 6 161 #define MXT_NOISE_ACTVGCAFVALID 7 162 #define MXT_NOISE_NOISETHR 8 163 #define MXT_NOISE_FREQHOPSCALE 10 164 #define MXT_NOISE_FREQ0 11 165 #define MXT_NOISE_FREQ1 12 166 #define MXT_NOISE_FREQ2 13 167 #define MXT_NOISE_FREQ3 14 168 #define MXT_NOISE_FREQ4 15 169 #define MXT_NOISE_IDLEGCAFVALID 16 170 171 /* MXT_SPT_COMMSCONFIG_T18 */ 172 #define MXT_COMMS_CTRL 0 173 #define MXT_COMMS_CMD 1 174 175 /* MXT_SPT_CTECONFIG_T28 field */ 176 #define MXT_CTE_CTRL 0 177 #define MXT_CTE_CMD 1 178 #define MXT_CTE_MODE 2 179 #define MXT_CTE_IDLEGCAFDEPTH 3 180 #define MXT_CTE_ACTVGCAFDEPTH 4 181 #define MXT_CTE_VOLTAGE 5 182 183 #define MXT_VOLTAGE_DEFAULT 2700000 184 #define MXT_VOLTAGE_STEP 10000 185 186 /* Define for MXT_GEN_COMMAND_T6 */ 187 #define MXT_BOOT_VALUE 0xa5 188 #define MXT_RESET_VALUE 0x01 189 #define MXT_BACKUP_VALUE 0x55 190 191 /* Delay times */ 192 #define MXT_BACKUP_TIME 50 /* msec */ 193 #define MXT_RESET_TIME 200 /* msec */ 194 #define MXT_RESET_TIMEOUT 3000 /* msec */ 195 #define MXT_CRC_TIMEOUT 1000 /* msec */ 196 #define MXT_FW_RESET_TIME 3000 /* msec */ 197 #define MXT_FW_CHG_TIMEOUT 300 /* msec */ 198 199 /* Command to unlock bootloader */ 200 #define MXT_UNLOCK_CMD_MSB 0xaa 201 #define MXT_UNLOCK_CMD_LSB 0xdc 202 203 /* Bootloader mode status */ 204 #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */ 205 #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */ 206 #define MXT_FRAME_CRC_CHECK 0x02 207 #define MXT_FRAME_CRC_FAIL 0x03 208 #define MXT_FRAME_CRC_PASS 0x04 209 #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */ 210 #define MXT_BOOT_STATUS_MASK 0x3f 211 #define MXT_BOOT_EXTENDED_ID (1 << 5) 212 #define MXT_BOOT_ID_MASK 0x1f 213 214 /* Touchscreen absolute values */ 215 #define MXT_MAX_AREA 0xff 216 217 #define MXT_PIXELS_PER_MM 20 218 219 struct mxt_info { 220 u8 family_id; 221 u8 variant_id; 222 u8 version; 223 u8 build; 224 u8 matrix_xsize; 225 u8 matrix_ysize; 226 u8 object_num; 227 }; 228 229 struct mxt_object { 230 u8 type; 231 u16 start_address; 232 u8 size_minus_one; 233 u8 instances_minus_one; 234 u8 num_report_ids; 235 } __packed; 236 237 /* Each client has this additional data */ 238 struct mxt_data { 239 struct i2c_client *client; 240 struct input_dev *input_dev; 241 char phys[64]; /* device physical location */ 242 const struct mxt_platform_data *pdata; 243 struct mxt_object *object_table; 244 struct mxt_info info; 245 unsigned int irq; 246 unsigned int max_x; 247 unsigned int max_y; 248 bool in_bootloader; 249 u16 mem_size; 250 u8 max_reportid; 251 u32 config_crc; 252 u32 info_crc; 253 u8 bootloader_addr; 254 u8 *msg_buf; 255 u8 t6_status; 256 bool update_input; 257 u8 last_message_count; 258 u8 num_touchids; 259 struct t7_config t7_cfg; 260 261 /* Cached parameters from object table */ 262 u16 T5_address; 263 u8 T5_msg_size; 264 u8 T6_reportid; 265 u16 T6_address; 266 u16 T7_address; 267 u8 T9_reportid_min; 268 u8 T9_reportid_max; 269 u8 T19_reportid; 270 u16 T44_address; 271 272 /* for fw update in bootloader */ 273 struct completion bl_completion; 274 275 /* for reset handling */ 276 struct completion reset_completion; 277 278 /* for config update handling */ 279 struct completion crc_completion; 280 }; 281 282 static size_t mxt_obj_size(const struct mxt_object *obj) 283 { 284 return obj->size_minus_one + 1; 285 } 286 287 static size_t mxt_obj_instances(const struct mxt_object *obj) 288 { 289 return obj->instances_minus_one + 1; 290 } 291 292 static bool mxt_object_readable(unsigned int type) 293 { 294 switch (type) { 295 case MXT_GEN_COMMAND_T6: 296 case MXT_GEN_POWER_T7: 297 case MXT_GEN_ACQUIRE_T8: 298 case MXT_GEN_DATASOURCE_T53: 299 case MXT_TOUCH_MULTI_T9: 300 case MXT_TOUCH_KEYARRAY_T15: 301 case MXT_TOUCH_PROXIMITY_T23: 302 case MXT_TOUCH_PROXKEY_T52: 303 case MXT_PROCI_GRIPFACE_T20: 304 case MXT_PROCG_NOISE_T22: 305 case MXT_PROCI_ONETOUCH_T24: 306 case MXT_PROCI_TWOTOUCH_T27: 307 case MXT_PROCI_GRIP_T40: 308 case MXT_PROCI_PALM_T41: 309 case MXT_PROCI_TOUCHSUPPRESSION_T42: 310 case MXT_PROCI_STYLUS_T47: 311 case MXT_PROCG_NOISESUPPRESSION_T48: 312 case MXT_SPT_COMMSCONFIG_T18: 313 case MXT_SPT_GPIOPWM_T19: 314 case MXT_SPT_SELFTEST_T25: 315 case MXT_SPT_CTECONFIG_T28: 316 case MXT_SPT_USERDATA_T38: 317 case MXT_SPT_DIGITIZER_T43: 318 case MXT_SPT_CTECONFIG_T46: 319 return true; 320 default: 321 return false; 322 } 323 } 324 325 static void mxt_dump_message(struct mxt_data *data, u8 *message) 326 { 327 dev_dbg(&data->client->dev, "message: %*ph\n", 328 data->T5_msg_size, message); 329 } 330 331 static int mxt_wait_for_completion(struct mxt_data *data, 332 struct completion *comp, 333 unsigned int timeout_ms) 334 { 335 struct device *dev = &data->client->dev; 336 unsigned long timeout = msecs_to_jiffies(timeout_ms); 337 long ret; 338 339 ret = wait_for_completion_interruptible_timeout(comp, timeout); 340 if (ret < 0) { 341 return ret; 342 } else if (ret == 0) { 343 dev_err(dev, "Wait for completion timed out.\n"); 344 return -ETIMEDOUT; 345 } 346 return 0; 347 } 348 349 static int mxt_bootloader_read(struct mxt_data *data, 350 u8 *val, unsigned int count) 351 { 352 int ret; 353 struct i2c_msg msg; 354 355 msg.addr = data->bootloader_addr; 356 msg.flags = data->client->flags & I2C_M_TEN; 357 msg.flags |= I2C_M_RD; 358 msg.len = count; 359 msg.buf = val; 360 361 ret = i2c_transfer(data->client->adapter, &msg, 1); 362 if (ret == 1) { 363 ret = 0; 364 } else { 365 ret = ret < 0 ? ret : -EIO; 366 dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n", 367 __func__, ret); 368 } 369 370 return ret; 371 } 372 373 static int mxt_bootloader_write(struct mxt_data *data, 374 const u8 * const val, unsigned int count) 375 { 376 int ret; 377 struct i2c_msg msg; 378 379 msg.addr = data->bootloader_addr; 380 msg.flags = data->client->flags & I2C_M_TEN; 381 msg.len = count; 382 msg.buf = (u8 *)val; 383 384 ret = i2c_transfer(data->client->adapter, &msg, 1); 385 if (ret == 1) { 386 ret = 0; 387 } else { 388 ret = ret < 0 ? ret : -EIO; 389 dev_err(&data->client->dev, "%s: i2c send failed (%d)\n", 390 __func__, ret); 391 } 392 393 return ret; 394 } 395 396 static int mxt_lookup_bootloader_address(struct mxt_data *data, bool retry) 397 { 398 u8 appmode = data->client->addr; 399 u8 bootloader; 400 401 switch (appmode) { 402 case 0x4a: 403 case 0x4b: 404 /* Chips after 1664S use different scheme */ 405 if (retry || data->info.family_id >= 0xa2) { 406 bootloader = appmode - 0x24; 407 break; 408 } 409 /* Fall through for normal case */ 410 case 0x4c: 411 case 0x4d: 412 case 0x5a: 413 case 0x5b: 414 bootloader = appmode - 0x26; 415 break; 416 417 default: 418 dev_err(&data->client->dev, 419 "Appmode i2c address 0x%02x not found\n", 420 appmode); 421 return -EINVAL; 422 } 423 424 data->bootloader_addr = bootloader; 425 return 0; 426 } 427 428 static int mxt_probe_bootloader(struct mxt_data *data, bool alt_address) 429 { 430 struct device *dev = &data->client->dev; 431 int error; 432 u8 val; 433 bool crc_failure; 434 435 error = mxt_lookup_bootloader_address(data, alt_address); 436 if (error) 437 return error; 438 439 error = mxt_bootloader_read(data, &val, 1); 440 if (error) 441 return error; 442 443 /* Check app crc fail mode */ 444 crc_failure = (val & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL; 445 446 dev_err(dev, "Detected bootloader, status:%02X%s\n", 447 val, crc_failure ? ", APP_CRC_FAIL" : ""); 448 449 return 0; 450 } 451 452 static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val) 453 { 454 struct device *dev = &data->client->dev; 455 u8 buf[3]; 456 457 if (val & MXT_BOOT_EXTENDED_ID) { 458 if (mxt_bootloader_read(data, &buf[0], 3) != 0) { 459 dev_err(dev, "%s: i2c failure\n", __func__); 460 return val; 461 } 462 463 dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]); 464 465 return buf[0]; 466 } else { 467 dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK); 468 469 return val; 470 } 471 } 472 473 static int mxt_check_bootloader(struct mxt_data *data, unsigned int state, 474 bool wait) 475 { 476 struct device *dev = &data->client->dev; 477 u8 val; 478 int ret; 479 480 recheck: 481 if (wait) { 482 /* 483 * In application update mode, the interrupt 484 * line signals state transitions. We must wait for the 485 * CHG assertion before reading the status byte. 486 * Once the status byte has been read, the line is deasserted. 487 */ 488 ret = mxt_wait_for_completion(data, &data->bl_completion, 489 MXT_FW_CHG_TIMEOUT); 490 if (ret) { 491 /* 492 * TODO: handle -ERESTARTSYS better by terminating 493 * fw update process before returning to userspace 494 * by writing length 0x000 to device (iff we are in 495 * WAITING_FRAME_DATA state). 496 */ 497 dev_err(dev, "Update wait error %d\n", ret); 498 return ret; 499 } 500 } 501 502 ret = mxt_bootloader_read(data, &val, 1); 503 if (ret) 504 return ret; 505 506 if (state == MXT_WAITING_BOOTLOAD_CMD) 507 val = mxt_get_bootloader_version(data, val); 508 509 switch (state) { 510 case MXT_WAITING_BOOTLOAD_CMD: 511 case MXT_WAITING_FRAME_DATA: 512 case MXT_APP_CRC_FAIL: 513 val &= ~MXT_BOOT_STATUS_MASK; 514 break; 515 case MXT_FRAME_CRC_PASS: 516 if (val == MXT_FRAME_CRC_CHECK) { 517 goto recheck; 518 } else if (val == MXT_FRAME_CRC_FAIL) { 519 dev_err(dev, "Bootloader CRC fail\n"); 520 return -EINVAL; 521 } 522 break; 523 default: 524 return -EINVAL; 525 } 526 527 if (val != state) { 528 dev_err(dev, "Invalid bootloader state %02X != %02X\n", 529 val, state); 530 return -EINVAL; 531 } 532 533 return 0; 534 } 535 536 static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock) 537 { 538 int ret; 539 u8 buf[2]; 540 541 if (unlock) { 542 buf[0] = MXT_UNLOCK_CMD_LSB; 543 buf[1] = MXT_UNLOCK_CMD_MSB; 544 } else { 545 buf[0] = 0x01; 546 buf[1] = 0x01; 547 } 548 549 ret = mxt_bootloader_write(data, buf, 2); 550 if (ret) 551 return ret; 552 553 return 0; 554 } 555 556 static int __mxt_read_reg(struct i2c_client *client, 557 u16 reg, u16 len, void *val) 558 { 559 struct i2c_msg xfer[2]; 560 u8 buf[2]; 561 int ret; 562 563 buf[0] = reg & 0xff; 564 buf[1] = (reg >> 8) & 0xff; 565 566 /* Write register */ 567 xfer[0].addr = client->addr; 568 xfer[0].flags = 0; 569 xfer[0].len = 2; 570 xfer[0].buf = buf; 571 572 /* Read data */ 573 xfer[1].addr = client->addr; 574 xfer[1].flags = I2C_M_RD; 575 xfer[1].len = len; 576 xfer[1].buf = val; 577 578 ret = i2c_transfer(client->adapter, xfer, 2); 579 if (ret == 2) { 580 ret = 0; 581 } else { 582 if (ret >= 0) 583 ret = -EIO; 584 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n", 585 __func__, ret); 586 } 587 588 return ret; 589 } 590 591 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len, 592 const void *val) 593 { 594 u8 *buf; 595 size_t count; 596 int ret; 597 598 count = len + 2; 599 buf = kmalloc(count, GFP_KERNEL); 600 if (!buf) 601 return -ENOMEM; 602 603 buf[0] = reg & 0xff; 604 buf[1] = (reg >> 8) & 0xff; 605 memcpy(&buf[2], val, len); 606 607 ret = i2c_master_send(client, buf, count); 608 if (ret == count) { 609 ret = 0; 610 } else { 611 if (ret >= 0) 612 ret = -EIO; 613 dev_err(&client->dev, "%s: i2c send failed (%d)\n", 614 __func__, ret); 615 } 616 617 kfree(buf); 618 return ret; 619 } 620 621 static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val) 622 { 623 return __mxt_write_reg(client, reg, 1, &val); 624 } 625 626 static struct mxt_object * 627 mxt_get_object(struct mxt_data *data, u8 type) 628 { 629 struct mxt_object *object; 630 int i; 631 632 for (i = 0; i < data->info.object_num; i++) { 633 object = data->object_table + i; 634 if (object->type == type) 635 return object; 636 } 637 638 dev_warn(&data->client->dev, "Invalid object type T%u\n", type); 639 return NULL; 640 } 641 642 static void mxt_proc_t6_messages(struct mxt_data *data, u8 *msg) 643 { 644 struct device *dev = &data->client->dev; 645 u8 status = msg[1]; 646 u32 crc = msg[2] | (msg[3] << 8) | (msg[4] << 16); 647 648 complete(&data->crc_completion); 649 650 if (crc != data->config_crc) { 651 data->config_crc = crc; 652 dev_dbg(dev, "T6 Config Checksum: 0x%06X\n", crc); 653 } 654 655 /* Detect reset */ 656 if (status & MXT_T6_STATUS_RESET) 657 complete(&data->reset_completion); 658 659 /* Output debug if status has changed */ 660 if (status != data->t6_status) 661 dev_dbg(dev, "T6 Status 0x%02X%s%s%s%s%s%s%s\n", 662 status, 663 status == 0 ? " OK" : "", 664 status & MXT_T6_STATUS_RESET ? " RESET" : "", 665 status & MXT_T6_STATUS_OFL ? " OFL" : "", 666 status & MXT_T6_STATUS_SIGERR ? " SIGERR" : "", 667 status & MXT_T6_STATUS_CAL ? " CAL" : "", 668 status & MXT_T6_STATUS_CFGERR ? " CFGERR" : "", 669 status & MXT_T6_STATUS_COMSERR ? " COMSERR" : ""); 670 671 /* Save current status */ 672 data->t6_status = status; 673 } 674 675 static void mxt_input_button(struct mxt_data *data, u8 *message) 676 { 677 struct input_dev *input = data->input_dev; 678 const struct mxt_platform_data *pdata = data->pdata; 679 bool button; 680 int i; 681 682 /* Active-low switch */ 683 for (i = 0; i < pdata->t19_num_keys; i++) { 684 if (pdata->t19_keymap[i] == KEY_RESERVED) 685 continue; 686 button = !(message[1] & (1 << i)); 687 input_report_key(input, pdata->t19_keymap[i], button); 688 } 689 } 690 691 static void mxt_input_sync(struct mxt_data *data) 692 { 693 input_mt_report_pointer_emulation(data->input_dev, 694 data->pdata->t19_num_keys); 695 input_sync(data->input_dev); 696 } 697 698 static void mxt_proc_t9_message(struct mxt_data *data, u8 *message) 699 { 700 struct device *dev = &data->client->dev; 701 struct input_dev *input_dev = data->input_dev; 702 int id; 703 u8 status; 704 int x; 705 int y; 706 int area; 707 int amplitude; 708 709 id = message[0] - data->T9_reportid_min; 710 status = message[1]; 711 x = (message[2] << 4) | ((message[4] >> 4) & 0xf); 712 y = (message[3] << 4) | ((message[4] & 0xf)); 713 714 /* Handle 10/12 bit switching */ 715 if (data->max_x < 1024) 716 x >>= 2; 717 if (data->max_y < 1024) 718 y >>= 2; 719 720 area = message[5]; 721 amplitude = message[6]; 722 723 dev_dbg(dev, 724 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n", 725 id, 726 (status & MXT_T9_DETECT) ? 'D' : '.', 727 (status & MXT_T9_PRESS) ? 'P' : '.', 728 (status & MXT_T9_RELEASE) ? 'R' : '.', 729 (status & MXT_T9_MOVE) ? 'M' : '.', 730 (status & MXT_T9_VECTOR) ? 'V' : '.', 731 (status & MXT_T9_AMP) ? 'A' : '.', 732 (status & MXT_T9_SUPPRESS) ? 'S' : '.', 733 (status & MXT_T9_UNGRIP) ? 'U' : '.', 734 x, y, area, amplitude); 735 736 input_mt_slot(input_dev, id); 737 738 if (status & MXT_T9_DETECT) { 739 /* 740 * Multiple bits may be set if the host is slow to read 741 * the status messages, indicating all the events that 742 * have happened. 743 */ 744 if (status & MXT_T9_RELEASE) { 745 input_mt_report_slot_state(input_dev, 746 MT_TOOL_FINGER, 0); 747 mxt_input_sync(data); 748 } 749 750 /* Touch active */ 751 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1); 752 input_report_abs(input_dev, ABS_MT_POSITION_X, x); 753 input_report_abs(input_dev, ABS_MT_POSITION_Y, y); 754 input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude); 755 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area); 756 } else { 757 /* Touch no longer active, close out slot */ 758 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0); 759 } 760 761 data->update_input = true; 762 } 763 764 static int mxt_proc_message(struct mxt_data *data, u8 *message) 765 { 766 u8 report_id = message[0]; 767 768 if (report_id == MXT_RPTID_NOMSG) 769 return 0; 770 771 if (report_id == data->T6_reportid) { 772 mxt_proc_t6_messages(data, message); 773 } else if (!data->input_dev) { 774 /* 775 * Do not report events if input device 776 * is not yet registered. 777 */ 778 mxt_dump_message(data, message); 779 } else if (report_id >= data->T9_reportid_min 780 && report_id <= data->T9_reportid_max) { 781 mxt_proc_t9_message(data, message); 782 } else if (report_id == data->T19_reportid) { 783 mxt_input_button(data, message); 784 data->update_input = true; 785 } else { 786 mxt_dump_message(data, message); 787 } 788 789 return 1; 790 } 791 792 static int mxt_read_and_process_messages(struct mxt_data *data, u8 count) 793 { 794 struct device *dev = &data->client->dev; 795 int ret; 796 int i; 797 u8 num_valid = 0; 798 799 /* Safety check for msg_buf */ 800 if (count > data->max_reportid) 801 return -EINVAL; 802 803 /* Process remaining messages if necessary */ 804 ret = __mxt_read_reg(data->client, data->T5_address, 805 data->T5_msg_size * count, data->msg_buf); 806 if (ret) { 807 dev_err(dev, "Failed to read %u messages (%d)\n", count, ret); 808 return ret; 809 } 810 811 for (i = 0; i < count; i++) { 812 ret = mxt_proc_message(data, 813 data->msg_buf + data->T5_msg_size * i); 814 815 if (ret == 1) 816 num_valid++; 817 } 818 819 /* return number of messages read */ 820 return num_valid; 821 } 822 823 static irqreturn_t mxt_process_messages_t44(struct mxt_data *data) 824 { 825 struct device *dev = &data->client->dev; 826 int ret; 827 u8 count, num_left; 828 829 /* Read T44 and T5 together */ 830 ret = __mxt_read_reg(data->client, data->T44_address, 831 data->T5_msg_size + 1, data->msg_buf); 832 if (ret) { 833 dev_err(dev, "Failed to read T44 and T5 (%d)\n", ret); 834 return IRQ_NONE; 835 } 836 837 count = data->msg_buf[0]; 838 839 if (count == 0) { 840 /* 841 * This condition is caused by the CHG line being configured 842 * in Mode 0. It results in unnecessary I2C operations but it 843 * is benign. 844 */ 845 dev_dbg(dev, "Interrupt triggered but zero messages\n"); 846 return IRQ_NONE; 847 } else if (count > data->max_reportid) { 848 dev_err(dev, "T44 count %d exceeded max report id\n", count); 849 count = data->max_reportid; 850 } 851 852 /* Process first message */ 853 ret = mxt_proc_message(data, data->msg_buf + 1); 854 if (ret < 0) { 855 dev_warn(dev, "Unexpected invalid message\n"); 856 return IRQ_NONE; 857 } 858 859 num_left = count - 1; 860 861 /* Process remaining messages if necessary */ 862 if (num_left) { 863 ret = mxt_read_and_process_messages(data, num_left); 864 if (ret < 0) 865 goto end; 866 else if (ret != num_left) 867 dev_warn(dev, "Unexpected invalid message\n"); 868 } 869 870 end: 871 if (data->update_input) { 872 mxt_input_sync(data); 873 data->update_input = false; 874 } 875 876 return IRQ_HANDLED; 877 } 878 879 static int mxt_process_messages_until_invalid(struct mxt_data *data) 880 { 881 struct device *dev = &data->client->dev; 882 int count, read; 883 u8 tries = 2; 884 885 count = data->max_reportid; 886 887 /* Read messages until we force an invalid */ 888 do { 889 read = mxt_read_and_process_messages(data, count); 890 if (read < count) 891 return 0; 892 } while (--tries); 893 894 if (data->update_input) { 895 mxt_input_sync(data); 896 data->update_input = false; 897 } 898 899 dev_err(dev, "CHG pin isn't cleared\n"); 900 return -EBUSY; 901 } 902 903 static irqreturn_t mxt_process_messages(struct mxt_data *data) 904 { 905 int total_handled, num_handled; 906 u8 count = data->last_message_count; 907 908 if (count < 1 || count > data->max_reportid) 909 count = 1; 910 911 /* include final invalid message */ 912 total_handled = mxt_read_and_process_messages(data, count + 1); 913 if (total_handled < 0) 914 return IRQ_NONE; 915 /* if there were invalid messages, then we are done */ 916 else if (total_handled <= count) 917 goto update_count; 918 919 /* keep reading two msgs until one is invalid or reportid limit */ 920 do { 921 num_handled = mxt_read_and_process_messages(data, 2); 922 if (num_handled < 0) 923 return IRQ_NONE; 924 925 total_handled += num_handled; 926 927 if (num_handled < 2) 928 break; 929 } while (total_handled < data->num_touchids); 930 931 update_count: 932 data->last_message_count = total_handled; 933 934 if (data->update_input) { 935 mxt_input_sync(data); 936 data->update_input = false; 937 } 938 939 return IRQ_HANDLED; 940 } 941 942 static irqreturn_t mxt_interrupt(int irq, void *dev_id) 943 { 944 struct mxt_data *data = dev_id; 945 946 if (data->in_bootloader) { 947 /* bootloader state transition completion */ 948 complete(&data->bl_completion); 949 return IRQ_HANDLED; 950 } 951 952 if (!data->object_table) 953 return IRQ_HANDLED; 954 955 if (data->T44_address) { 956 return mxt_process_messages_t44(data); 957 } else { 958 return mxt_process_messages(data); 959 } 960 } 961 962 static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset, 963 u8 value, bool wait) 964 { 965 u16 reg; 966 u8 command_register; 967 int timeout_counter = 0; 968 int ret; 969 970 reg = data->T6_address + cmd_offset; 971 972 ret = mxt_write_reg(data->client, reg, value); 973 if (ret) 974 return ret; 975 976 if (!wait) 977 return 0; 978 979 do { 980 msleep(20); 981 ret = __mxt_read_reg(data->client, reg, 1, &command_register); 982 if (ret) 983 return ret; 984 } while (command_register != 0 && timeout_counter++ <= 100); 985 986 if (timeout_counter > 100) { 987 dev_err(&data->client->dev, "Command failed!\n"); 988 return -EIO; 989 } 990 991 return 0; 992 } 993 994 static int mxt_soft_reset(struct mxt_data *data) 995 { 996 struct device *dev = &data->client->dev; 997 int ret = 0; 998 999 dev_info(dev, "Resetting chip\n"); 1000 1001 reinit_completion(&data->reset_completion); 1002 1003 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false); 1004 if (ret) 1005 return ret; 1006 1007 ret = mxt_wait_for_completion(data, &data->reset_completion, 1008 MXT_RESET_TIMEOUT); 1009 if (ret) 1010 return ret; 1011 1012 return 0; 1013 } 1014 1015 static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value) 1016 { 1017 /* 1018 * On failure, CRC is set to 0 and config will always be 1019 * downloaded. 1020 */ 1021 data->config_crc = 0; 1022 reinit_completion(&data->crc_completion); 1023 1024 mxt_t6_command(data, cmd, value, true); 1025 1026 /* 1027 * Wait for crc message. On failure, CRC is set to 0 and config will 1028 * always be downloaded. 1029 */ 1030 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT); 1031 } 1032 1033 static void mxt_calc_crc24(u32 *crc, u8 firstbyte, u8 secondbyte) 1034 { 1035 static const unsigned int crcpoly = 0x80001B; 1036 u32 result; 1037 u32 data_word; 1038 1039 data_word = (secondbyte << 8) | firstbyte; 1040 result = ((*crc << 1) ^ data_word); 1041 1042 if (result & 0x1000000) 1043 result ^= crcpoly; 1044 1045 *crc = result; 1046 } 1047 1048 static u32 mxt_calculate_crc(u8 *base, off_t start_off, off_t end_off) 1049 { 1050 u32 crc = 0; 1051 u8 *ptr = base + start_off; 1052 u8 *last_val = base + end_off - 1; 1053 1054 if (end_off < start_off) 1055 return -EINVAL; 1056 1057 while (ptr < last_val) { 1058 mxt_calc_crc24(&crc, *ptr, *(ptr + 1)); 1059 ptr += 2; 1060 } 1061 1062 /* if len is odd, fill the last byte with 0 */ 1063 if (ptr == last_val) 1064 mxt_calc_crc24(&crc, *ptr, 0); 1065 1066 /* Mask to 24-bit */ 1067 crc &= 0x00FFFFFF; 1068 1069 return crc; 1070 } 1071 1072 static int mxt_prepare_cfg_mem(struct mxt_data *data, 1073 const struct firmware *cfg, 1074 unsigned int data_pos, 1075 unsigned int cfg_start_ofs, 1076 u8 *config_mem, 1077 size_t config_mem_size) 1078 { 1079 struct device *dev = &data->client->dev; 1080 struct mxt_object *object; 1081 unsigned int type, instance, size, byte_offset; 1082 int offset; 1083 int ret; 1084 int i; 1085 u16 reg; 1086 u8 val; 1087 1088 while (data_pos < cfg->size) { 1089 /* Read type, instance, length */ 1090 ret = sscanf(cfg->data + data_pos, "%x %x %x%n", 1091 &type, &instance, &size, &offset); 1092 if (ret == 0) { 1093 /* EOF */ 1094 break; 1095 } else if (ret != 3) { 1096 dev_err(dev, "Bad format: failed to parse object\n"); 1097 return -EINVAL; 1098 } 1099 data_pos += offset; 1100 1101 object = mxt_get_object(data, type); 1102 if (!object) { 1103 /* Skip object */ 1104 for (i = 0; i < size; i++) { 1105 ret = sscanf(cfg->data + data_pos, "%hhx%n", 1106 &val, &offset); 1107 if (ret != 1) { 1108 dev_err(dev, "Bad format in T%d at %d\n", 1109 type, i); 1110 return -EINVAL; 1111 } 1112 data_pos += offset; 1113 } 1114 continue; 1115 } 1116 1117 if (size > mxt_obj_size(object)) { 1118 /* 1119 * Either we are in fallback mode due to wrong 1120 * config or config from a later fw version, 1121 * or the file is corrupt or hand-edited. 1122 */ 1123 dev_warn(dev, "Discarding %zu byte(s) in T%u\n", 1124 size - mxt_obj_size(object), type); 1125 } else if (mxt_obj_size(object) > size) { 1126 /* 1127 * If firmware is upgraded, new bytes may be added to 1128 * end of objects. It is generally forward compatible 1129 * to zero these bytes - previous behaviour will be 1130 * retained. However this does invalidate the CRC and 1131 * will force fallback mode until the configuration is 1132 * updated. We warn here but do nothing else - the 1133 * malloc has zeroed the entire configuration. 1134 */ 1135 dev_warn(dev, "Zeroing %zu byte(s) in T%d\n", 1136 mxt_obj_size(object) - size, type); 1137 } 1138 1139 if (instance >= mxt_obj_instances(object)) { 1140 dev_err(dev, "Object instances exceeded!\n"); 1141 return -EINVAL; 1142 } 1143 1144 reg = object->start_address + mxt_obj_size(object) * instance; 1145 1146 for (i = 0; i < size; i++) { 1147 ret = sscanf(cfg->data + data_pos, "%hhx%n", 1148 &val, 1149 &offset); 1150 if (ret != 1) { 1151 dev_err(dev, "Bad format in T%d at %d\n", 1152 type, i); 1153 return -EINVAL; 1154 } 1155 data_pos += offset; 1156 1157 if (i > mxt_obj_size(object)) 1158 continue; 1159 1160 byte_offset = reg + i - cfg_start_ofs; 1161 1162 if (byte_offset >= 0 && byte_offset < config_mem_size) { 1163 *(config_mem + byte_offset) = val; 1164 } else { 1165 dev_err(dev, "Bad object: reg:%d, T%d, ofs=%d\n", 1166 reg, object->type, byte_offset); 1167 return -EINVAL; 1168 } 1169 } 1170 } 1171 1172 return 0; 1173 } 1174 1175 static int mxt_upload_cfg_mem(struct mxt_data *data, unsigned int cfg_start, 1176 u8 *config_mem, size_t config_mem_size) 1177 { 1178 unsigned int byte_offset = 0; 1179 int error; 1180 1181 /* Write configuration as blocks */ 1182 while (byte_offset < config_mem_size) { 1183 unsigned int size = config_mem_size - byte_offset; 1184 1185 if (size > MXT_MAX_BLOCK_WRITE) 1186 size = MXT_MAX_BLOCK_WRITE; 1187 1188 error = __mxt_write_reg(data->client, 1189 cfg_start + byte_offset, 1190 size, config_mem + byte_offset); 1191 if (error) { 1192 dev_err(&data->client->dev, 1193 "Config write error, ret=%d\n", error); 1194 return error; 1195 } 1196 1197 byte_offset += size; 1198 } 1199 1200 return 0; 1201 } 1202 1203 /* 1204 * mxt_update_cfg - download configuration to chip 1205 * 1206 * Atmel Raw Config File Format 1207 * 1208 * The first four lines of the raw config file contain: 1209 * 1) Version 1210 * 2) Chip ID Information (first 7 bytes of device memory) 1211 * 3) Chip Information Block 24-bit CRC Checksum 1212 * 4) Chip Configuration 24-bit CRC Checksum 1213 * 1214 * The rest of the file consists of one line per object instance: 1215 * <TYPE> <INSTANCE> <SIZE> <CONTENTS> 1216 * 1217 * <TYPE> - 2-byte object type as hex 1218 * <INSTANCE> - 2-byte object instance number as hex 1219 * <SIZE> - 2-byte object size as hex 1220 * <CONTENTS> - array of <SIZE> 1-byte hex values 1221 */ 1222 static int mxt_update_cfg(struct mxt_data *data, const struct firmware *cfg) 1223 { 1224 struct device *dev = &data->client->dev; 1225 struct mxt_info cfg_info; 1226 int ret; 1227 int offset; 1228 int data_pos; 1229 int i; 1230 int cfg_start_ofs; 1231 u32 info_crc, config_crc, calculated_crc; 1232 u8 *config_mem; 1233 size_t config_mem_size; 1234 1235 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1); 1236 1237 if (strncmp(cfg->data, MXT_CFG_MAGIC, strlen(MXT_CFG_MAGIC))) { 1238 dev_err(dev, "Unrecognised config file\n"); 1239 return -EINVAL; 1240 } 1241 1242 data_pos = strlen(MXT_CFG_MAGIC); 1243 1244 /* Load information block and check */ 1245 for (i = 0; i < sizeof(struct mxt_info); i++) { 1246 ret = sscanf(cfg->data + data_pos, "%hhx%n", 1247 (unsigned char *)&cfg_info + i, 1248 &offset); 1249 if (ret != 1) { 1250 dev_err(dev, "Bad format\n"); 1251 return -EINVAL; 1252 } 1253 1254 data_pos += offset; 1255 } 1256 1257 if (cfg_info.family_id != data->info.family_id) { 1258 dev_err(dev, "Family ID mismatch!\n"); 1259 return -EINVAL; 1260 } 1261 1262 if (cfg_info.variant_id != data->info.variant_id) { 1263 dev_err(dev, "Variant ID mismatch!\n"); 1264 return -EINVAL; 1265 } 1266 1267 /* Read CRCs */ 1268 ret = sscanf(cfg->data + data_pos, "%x%n", &info_crc, &offset); 1269 if (ret != 1) { 1270 dev_err(dev, "Bad format: failed to parse Info CRC\n"); 1271 return -EINVAL; 1272 } 1273 data_pos += offset; 1274 1275 ret = sscanf(cfg->data + data_pos, "%x%n", &config_crc, &offset); 1276 if (ret != 1) { 1277 dev_err(dev, "Bad format: failed to parse Config CRC\n"); 1278 return -EINVAL; 1279 } 1280 data_pos += offset; 1281 1282 /* 1283 * The Info Block CRC is calculated over mxt_info and the object 1284 * table. If it does not match then we are trying to load the 1285 * configuration from a different chip or firmware version, so 1286 * the configuration CRC is invalid anyway. 1287 */ 1288 if (info_crc == data->info_crc) { 1289 if (config_crc == 0 || data->config_crc == 0) { 1290 dev_info(dev, "CRC zero, attempting to apply config\n"); 1291 } else if (config_crc == data->config_crc) { 1292 dev_dbg(dev, "Config CRC 0x%06X: OK\n", 1293 data->config_crc); 1294 return 0; 1295 } else { 1296 dev_info(dev, "Config CRC 0x%06X: does not match file 0x%06X\n", 1297 data->config_crc, config_crc); 1298 } 1299 } else { 1300 dev_warn(dev, 1301 "Warning: Info CRC error - device=0x%06X file=0x%06X\n", 1302 data->info_crc, info_crc); 1303 } 1304 1305 /* Malloc memory to store configuration */ 1306 cfg_start_ofs = MXT_OBJECT_START + 1307 data->info.object_num * sizeof(struct mxt_object) + 1308 MXT_INFO_CHECKSUM_SIZE; 1309 config_mem_size = data->mem_size - cfg_start_ofs; 1310 config_mem = kzalloc(config_mem_size, GFP_KERNEL); 1311 if (!config_mem) { 1312 dev_err(dev, "Failed to allocate memory\n"); 1313 return -ENOMEM; 1314 } 1315 1316 ret = mxt_prepare_cfg_mem(data, cfg, data_pos, cfg_start_ofs, 1317 config_mem, config_mem_size); 1318 if (ret) 1319 goto release_mem; 1320 1321 /* Calculate crc of the received configs (not the raw config file) */ 1322 if (data->T7_address < cfg_start_ofs) { 1323 dev_err(dev, "Bad T7 address, T7addr = %x, config offset %x\n", 1324 data->T7_address, cfg_start_ofs); 1325 ret = 0; 1326 goto release_mem; 1327 } 1328 1329 calculated_crc = mxt_calculate_crc(config_mem, 1330 data->T7_address - cfg_start_ofs, 1331 config_mem_size); 1332 1333 if (config_crc > 0 && config_crc != calculated_crc) 1334 dev_warn(dev, "Config CRC error, calculated=%06X, file=%06X\n", 1335 calculated_crc, config_crc); 1336 1337 ret = mxt_upload_cfg_mem(data, cfg_start_ofs, 1338 config_mem, config_mem_size); 1339 if (ret) 1340 goto release_mem; 1341 1342 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE); 1343 1344 ret = mxt_soft_reset(data); 1345 if (ret) 1346 goto release_mem; 1347 1348 dev_info(dev, "Config successfully updated\n"); 1349 1350 release_mem: 1351 kfree(config_mem); 1352 return ret; 1353 } 1354 1355 static int mxt_acquire_irq(struct mxt_data *data) 1356 { 1357 int error; 1358 1359 enable_irq(data->irq); 1360 1361 error = mxt_process_messages_until_invalid(data); 1362 if (error) 1363 return error; 1364 1365 return 0; 1366 } 1367 1368 static int mxt_get_info(struct mxt_data *data) 1369 { 1370 struct i2c_client *client = data->client; 1371 struct mxt_info *info = &data->info; 1372 int error; 1373 1374 /* Read 7-byte info block starting at address 0 */ 1375 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info); 1376 if (error) 1377 return error; 1378 1379 return 0; 1380 } 1381 1382 static void mxt_free_input_device(struct mxt_data *data) 1383 { 1384 if (data->input_dev) { 1385 input_unregister_device(data->input_dev); 1386 data->input_dev = NULL; 1387 } 1388 } 1389 1390 static void mxt_free_object_table(struct mxt_data *data) 1391 { 1392 kfree(data->object_table); 1393 data->object_table = NULL; 1394 kfree(data->msg_buf); 1395 data->msg_buf = NULL; 1396 data->T5_address = 0; 1397 data->T5_msg_size = 0; 1398 data->T6_reportid = 0; 1399 data->T7_address = 0; 1400 data->T9_reportid_min = 0; 1401 data->T9_reportid_max = 0; 1402 data->T19_reportid = 0; 1403 data->T44_address = 0; 1404 data->max_reportid = 0; 1405 } 1406 1407 static int mxt_get_object_table(struct mxt_data *data) 1408 { 1409 struct i2c_client *client = data->client; 1410 size_t table_size; 1411 struct mxt_object *object_table; 1412 int error; 1413 int i; 1414 u8 reportid; 1415 u16 end_address; 1416 1417 table_size = data->info.object_num * sizeof(struct mxt_object); 1418 object_table = kzalloc(table_size, GFP_KERNEL); 1419 if (!object_table) { 1420 dev_err(&data->client->dev, "Failed to allocate memory\n"); 1421 return -ENOMEM; 1422 } 1423 1424 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size, 1425 object_table); 1426 if (error) { 1427 kfree(object_table); 1428 return error; 1429 } 1430 1431 /* Valid Report IDs start counting from 1 */ 1432 reportid = 1; 1433 data->mem_size = 0; 1434 for (i = 0; i < data->info.object_num; i++) { 1435 struct mxt_object *object = object_table + i; 1436 u8 min_id, max_id; 1437 1438 le16_to_cpus(&object->start_address); 1439 1440 if (object->num_report_ids) { 1441 min_id = reportid; 1442 reportid += object->num_report_ids * 1443 mxt_obj_instances(object); 1444 max_id = reportid - 1; 1445 } else { 1446 min_id = 0; 1447 max_id = 0; 1448 } 1449 1450 dev_dbg(&data->client->dev, 1451 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n", 1452 object->type, object->start_address, 1453 mxt_obj_size(object), mxt_obj_instances(object), 1454 min_id, max_id); 1455 1456 switch (object->type) { 1457 case MXT_GEN_MESSAGE_T5: 1458 if (data->info.family_id == 0x80 && 1459 data->info.version < 0x20) { 1460 /* 1461 * On mXT224 firmware versions prior to V2.0 1462 * read and discard unused CRC byte otherwise 1463 * DMA reads are misaligned. 1464 */ 1465 data->T5_msg_size = mxt_obj_size(object); 1466 } else { 1467 /* CRC not enabled, so skip last byte */ 1468 data->T5_msg_size = mxt_obj_size(object) - 1; 1469 } 1470 data->T5_address = object->start_address; 1471 break; 1472 case MXT_GEN_COMMAND_T6: 1473 data->T6_reportid = min_id; 1474 data->T6_address = object->start_address; 1475 break; 1476 case MXT_GEN_POWER_T7: 1477 data->T7_address = object->start_address; 1478 break; 1479 case MXT_TOUCH_MULTI_T9: 1480 data->T9_reportid_min = min_id; 1481 data->T9_reportid_max = max_id; 1482 data->num_touchids = object->num_report_ids 1483 * mxt_obj_instances(object); 1484 break; 1485 case MXT_SPT_MESSAGECOUNT_T44: 1486 data->T44_address = object->start_address; 1487 break; 1488 case MXT_SPT_GPIOPWM_T19: 1489 data->T19_reportid = min_id; 1490 break; 1491 } 1492 1493 end_address = object->start_address 1494 + mxt_obj_size(object) * mxt_obj_instances(object) - 1; 1495 1496 if (end_address >= data->mem_size) 1497 data->mem_size = end_address + 1; 1498 } 1499 1500 /* Store maximum reportid */ 1501 data->max_reportid = reportid; 1502 1503 /* If T44 exists, T5 position has to be directly after */ 1504 if (data->T44_address && (data->T5_address != data->T44_address + 1)) { 1505 dev_err(&client->dev, "Invalid T44 position\n"); 1506 error = -EINVAL; 1507 goto free_object_table; 1508 } 1509 1510 data->msg_buf = kcalloc(data->max_reportid, 1511 data->T5_msg_size, GFP_KERNEL); 1512 if (!data->msg_buf) { 1513 dev_err(&client->dev, "Failed to allocate message buffer\n"); 1514 error = -ENOMEM; 1515 goto free_object_table; 1516 } 1517 1518 data->object_table = object_table; 1519 1520 return 0; 1521 1522 free_object_table: 1523 mxt_free_object_table(data); 1524 return error; 1525 } 1526 1527 static int mxt_read_t9_resolution(struct mxt_data *data) 1528 { 1529 struct i2c_client *client = data->client; 1530 int error; 1531 struct t9_range range; 1532 unsigned char orient; 1533 struct mxt_object *object; 1534 1535 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9); 1536 if (!object) 1537 return -EINVAL; 1538 1539 error = __mxt_read_reg(client, 1540 object->start_address + MXT_T9_RANGE, 1541 sizeof(range), &range); 1542 if (error) 1543 return error; 1544 1545 le16_to_cpus(&range.x); 1546 le16_to_cpus(&range.y); 1547 1548 error = __mxt_read_reg(client, 1549 object->start_address + MXT_T9_ORIENT, 1550 1, &orient); 1551 if (error) 1552 return error; 1553 1554 /* Handle default values */ 1555 if (range.x == 0) 1556 range.x = 1023; 1557 1558 if (range.y == 0) 1559 range.y = 1023; 1560 1561 if (orient & MXT_T9_ORIENT_SWITCH) { 1562 data->max_x = range.y; 1563 data->max_y = range.x; 1564 } else { 1565 data->max_x = range.x; 1566 data->max_y = range.y; 1567 } 1568 1569 dev_dbg(&client->dev, 1570 "Touchscreen size X%uY%u\n", data->max_x, data->max_y); 1571 1572 return 0; 1573 } 1574 1575 static int mxt_input_open(struct input_dev *dev); 1576 static void mxt_input_close(struct input_dev *dev); 1577 1578 static int mxt_initialize_t9_input_device(struct mxt_data *data) 1579 { 1580 struct device *dev = &data->client->dev; 1581 const struct mxt_platform_data *pdata = data->pdata; 1582 struct input_dev *input_dev; 1583 int error; 1584 unsigned int num_mt_slots; 1585 unsigned int mt_flags = 0; 1586 int i; 1587 1588 error = mxt_read_t9_resolution(data); 1589 if (error) 1590 dev_warn(dev, "Failed to initialize T9 resolution\n"); 1591 1592 input_dev = input_allocate_device(); 1593 if (!input_dev) { 1594 dev_err(dev, "Failed to allocate memory\n"); 1595 return -ENOMEM; 1596 } 1597 1598 input_dev->name = "Atmel maXTouch Touchscreen"; 1599 input_dev->phys = data->phys; 1600 input_dev->id.bustype = BUS_I2C; 1601 input_dev->dev.parent = dev; 1602 input_dev->open = mxt_input_open; 1603 input_dev->close = mxt_input_close; 1604 1605 __set_bit(EV_ABS, input_dev->evbit); 1606 __set_bit(EV_KEY, input_dev->evbit); 1607 __set_bit(BTN_TOUCH, input_dev->keybit); 1608 1609 if (pdata->t19_num_keys) { 1610 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit); 1611 1612 for (i = 0; i < pdata->t19_num_keys; i++) 1613 if (pdata->t19_keymap[i] != KEY_RESERVED) 1614 input_set_capability(input_dev, EV_KEY, 1615 pdata->t19_keymap[i]); 1616 1617 mt_flags |= INPUT_MT_POINTER; 1618 1619 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM); 1620 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM); 1621 input_abs_set_res(input_dev, ABS_MT_POSITION_X, 1622 MXT_PIXELS_PER_MM); 1623 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, 1624 MXT_PIXELS_PER_MM); 1625 1626 input_dev->name = "Atmel maXTouch Touchpad"; 1627 } 1628 1629 /* For single touch */ 1630 input_set_abs_params(input_dev, ABS_X, 1631 0, data->max_x, 0, 0); 1632 input_set_abs_params(input_dev, ABS_Y, 1633 0, data->max_y, 0, 0); 1634 input_set_abs_params(input_dev, ABS_PRESSURE, 1635 0, 255, 0, 0); 1636 1637 /* For multi touch */ 1638 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1; 1639 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags); 1640 if (error) { 1641 dev_err(dev, "Error %d initialising slots\n", error); 1642 goto err_free_mem; 1643 } 1644 1645 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 1646 0, MXT_MAX_AREA, 0, 0); 1647 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 1648 0, data->max_x, 0, 0); 1649 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 1650 0, data->max_y, 0, 0); 1651 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 1652 0, 255, 0, 0); 1653 1654 input_set_drvdata(input_dev, data); 1655 1656 error = input_register_device(input_dev); 1657 if (error) { 1658 dev_err(dev, "Error %d registering input device\n", error); 1659 goto err_free_mem; 1660 } 1661 1662 data->input_dev = input_dev; 1663 1664 return 0; 1665 1666 err_free_mem: 1667 input_free_device(input_dev); 1668 return error; 1669 } 1670 1671 static int mxt_configure_objects(struct mxt_data *data, 1672 const struct firmware *cfg); 1673 1674 static void mxt_config_cb(const struct firmware *cfg, void *ctx) 1675 { 1676 mxt_configure_objects(ctx, cfg); 1677 release_firmware(cfg); 1678 } 1679 1680 static int mxt_initialize(struct mxt_data *data) 1681 { 1682 struct i2c_client *client = data->client; 1683 int recovery_attempts = 0; 1684 int error; 1685 1686 while (1) { 1687 error = mxt_get_info(data); 1688 if (!error) 1689 break; 1690 1691 /* Check bootloader state */ 1692 error = mxt_probe_bootloader(data, false); 1693 if (error) { 1694 dev_info(&client->dev, "Trying alternate bootloader address\n"); 1695 error = mxt_probe_bootloader(data, true); 1696 if (error) { 1697 /* Chip is not in appmode or bootloader mode */ 1698 return error; 1699 } 1700 } 1701 1702 /* OK, we are in bootloader, see if we can recover */ 1703 if (++recovery_attempts > 1) { 1704 dev_err(&client->dev, "Could not recover from bootloader mode\n"); 1705 /* 1706 * We can reflash from this state, so do not 1707 * abort initialization. 1708 */ 1709 data->in_bootloader = true; 1710 return 0; 1711 } 1712 1713 /* Attempt to exit bootloader into app mode */ 1714 mxt_send_bootloader_cmd(data, false); 1715 msleep(MXT_FW_RESET_TIME); 1716 } 1717 1718 /* Get object table information */ 1719 error = mxt_get_object_table(data); 1720 if (error) { 1721 dev_err(&client->dev, "Error %d reading object table\n", error); 1722 return error; 1723 } 1724 1725 error = mxt_acquire_irq(data); 1726 if (error) 1727 goto err_free_object_table; 1728 1729 error = request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME, 1730 &client->dev, GFP_KERNEL, data, 1731 mxt_config_cb); 1732 if (error) { 1733 dev_err(&client->dev, "Failed to invoke firmware loader: %d\n", 1734 error); 1735 goto err_free_object_table; 1736 } 1737 1738 return 0; 1739 1740 err_free_object_table: 1741 mxt_free_object_table(data); 1742 return error; 1743 } 1744 1745 static int mxt_set_t7_power_cfg(struct mxt_data *data, u8 sleep) 1746 { 1747 struct device *dev = &data->client->dev; 1748 int error; 1749 struct t7_config *new_config; 1750 struct t7_config deepsleep = { .active = 0, .idle = 0 }; 1751 1752 if (sleep == MXT_POWER_CFG_DEEPSLEEP) 1753 new_config = &deepsleep; 1754 else 1755 new_config = &data->t7_cfg; 1756 1757 error = __mxt_write_reg(data->client, data->T7_address, 1758 sizeof(data->t7_cfg), new_config); 1759 if (error) 1760 return error; 1761 1762 dev_dbg(dev, "Set T7 ACTV:%d IDLE:%d\n", 1763 new_config->active, new_config->idle); 1764 1765 return 0; 1766 } 1767 1768 static int mxt_init_t7_power_cfg(struct mxt_data *data) 1769 { 1770 struct device *dev = &data->client->dev; 1771 int error; 1772 bool retry = false; 1773 1774 recheck: 1775 error = __mxt_read_reg(data->client, data->T7_address, 1776 sizeof(data->t7_cfg), &data->t7_cfg); 1777 if (error) 1778 return error; 1779 1780 if (data->t7_cfg.active == 0 || data->t7_cfg.idle == 0) { 1781 if (!retry) { 1782 dev_dbg(dev, "T7 cfg zero, resetting\n"); 1783 mxt_soft_reset(data); 1784 retry = true; 1785 goto recheck; 1786 } else { 1787 dev_dbg(dev, "T7 cfg zero after reset, overriding\n"); 1788 data->t7_cfg.active = 20; 1789 data->t7_cfg.idle = 100; 1790 return mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN); 1791 } 1792 } 1793 1794 dev_dbg(dev, "Initialized power cfg: ACTV %d, IDLE %d\n", 1795 data->t7_cfg.active, data->t7_cfg.idle); 1796 return 0; 1797 } 1798 1799 static int mxt_configure_objects(struct mxt_data *data, 1800 const struct firmware *cfg) 1801 { 1802 struct device *dev = &data->client->dev; 1803 struct mxt_info *info = &data->info; 1804 int error; 1805 1806 if (cfg) { 1807 error = mxt_update_cfg(data, cfg); 1808 if (error) 1809 dev_warn(dev, "Error %d updating config\n", error); 1810 } 1811 1812 error = mxt_init_t7_power_cfg(data); 1813 if (error) { 1814 dev_err(dev, "Failed to initialize power cfg\n"); 1815 return error; 1816 } 1817 1818 error = mxt_initialize_t9_input_device(data); 1819 if (error) 1820 return error; 1821 1822 dev_info(dev, 1823 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n", 1824 info->family_id, info->variant_id, info->version >> 4, 1825 info->version & 0xf, info->build, info->object_num); 1826 1827 return 0; 1828 } 1829 1830 /* Firmware Version is returned as Major.Minor.Build */ 1831 static ssize_t mxt_fw_version_show(struct device *dev, 1832 struct device_attribute *attr, char *buf) 1833 { 1834 struct mxt_data *data = dev_get_drvdata(dev); 1835 struct mxt_info *info = &data->info; 1836 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n", 1837 info->version >> 4, info->version & 0xf, info->build); 1838 } 1839 1840 /* Hardware Version is returned as FamilyID.VariantID */ 1841 static ssize_t mxt_hw_version_show(struct device *dev, 1842 struct device_attribute *attr, char *buf) 1843 { 1844 struct mxt_data *data = dev_get_drvdata(dev); 1845 struct mxt_info *info = &data->info; 1846 return scnprintf(buf, PAGE_SIZE, "%u.%u\n", 1847 info->family_id, info->variant_id); 1848 } 1849 1850 static ssize_t mxt_show_instance(char *buf, int count, 1851 struct mxt_object *object, int instance, 1852 const u8 *val) 1853 { 1854 int i; 1855 1856 if (mxt_obj_instances(object) > 1) 1857 count += scnprintf(buf + count, PAGE_SIZE - count, 1858 "Instance %u\n", instance); 1859 1860 for (i = 0; i < mxt_obj_size(object); i++) 1861 count += scnprintf(buf + count, PAGE_SIZE - count, 1862 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]); 1863 count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); 1864 1865 return count; 1866 } 1867 1868 static ssize_t mxt_object_show(struct device *dev, 1869 struct device_attribute *attr, char *buf) 1870 { 1871 struct mxt_data *data = dev_get_drvdata(dev); 1872 struct mxt_object *object; 1873 int count = 0; 1874 int i, j; 1875 int error; 1876 u8 *obuf; 1877 1878 /* Pre-allocate buffer large enough to hold max sized object. */ 1879 obuf = kmalloc(256, GFP_KERNEL); 1880 if (!obuf) 1881 return -ENOMEM; 1882 1883 error = 0; 1884 for (i = 0; i < data->info.object_num; i++) { 1885 object = data->object_table + i; 1886 1887 if (!mxt_object_readable(object->type)) 1888 continue; 1889 1890 count += scnprintf(buf + count, PAGE_SIZE - count, 1891 "T%u:\n", object->type); 1892 1893 for (j = 0; j < mxt_obj_instances(object); j++) { 1894 u16 size = mxt_obj_size(object); 1895 u16 addr = object->start_address + j * size; 1896 1897 error = __mxt_read_reg(data->client, addr, size, obuf); 1898 if (error) 1899 goto done; 1900 1901 count = mxt_show_instance(buf, count, object, j, obuf); 1902 } 1903 } 1904 1905 done: 1906 kfree(obuf); 1907 return error ?: count; 1908 } 1909 1910 static int mxt_check_firmware_format(struct device *dev, 1911 const struct firmware *fw) 1912 { 1913 unsigned int pos = 0; 1914 char c; 1915 1916 while (pos < fw->size) { 1917 c = *(fw->data + pos); 1918 1919 if (c < '0' || (c > '9' && c < 'A') || c > 'F') 1920 return 0; 1921 1922 pos++; 1923 } 1924 1925 /* 1926 * To convert file try: 1927 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw 1928 */ 1929 dev_err(dev, "Aborting: firmware file must be in binary format\n"); 1930 1931 return -EINVAL; 1932 } 1933 1934 static int mxt_load_fw(struct device *dev, const char *fn) 1935 { 1936 struct mxt_data *data = dev_get_drvdata(dev); 1937 const struct firmware *fw = NULL; 1938 unsigned int frame_size; 1939 unsigned int pos = 0; 1940 unsigned int retry = 0; 1941 unsigned int frame = 0; 1942 int ret; 1943 1944 ret = request_firmware(&fw, fn, dev); 1945 if (ret) { 1946 dev_err(dev, "Unable to open firmware %s\n", fn); 1947 return ret; 1948 } 1949 1950 /* Check for incorrect enc file */ 1951 ret = mxt_check_firmware_format(dev, fw); 1952 if (ret) 1953 goto release_firmware; 1954 1955 if (!data->in_bootloader) { 1956 /* Change to the bootloader mode */ 1957 data->in_bootloader = true; 1958 1959 ret = mxt_t6_command(data, MXT_COMMAND_RESET, 1960 MXT_BOOT_VALUE, false); 1961 if (ret) 1962 goto release_firmware; 1963 1964 msleep(MXT_RESET_TIME); 1965 1966 /* Do not need to scan since we know family ID */ 1967 ret = mxt_lookup_bootloader_address(data, 0); 1968 if (ret) 1969 goto release_firmware; 1970 1971 mxt_free_input_device(data); 1972 mxt_free_object_table(data); 1973 } else { 1974 enable_irq(data->irq); 1975 } 1976 1977 reinit_completion(&data->bl_completion); 1978 1979 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD, false); 1980 if (ret) { 1981 /* Bootloader may still be unlocked from previous attempt */ 1982 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, false); 1983 if (ret) 1984 goto disable_irq; 1985 } else { 1986 dev_info(dev, "Unlocking bootloader\n"); 1987 1988 /* Unlock bootloader */ 1989 ret = mxt_send_bootloader_cmd(data, true); 1990 if (ret) 1991 goto disable_irq; 1992 } 1993 1994 while (pos < fw->size) { 1995 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true); 1996 if (ret) 1997 goto disable_irq; 1998 1999 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1)); 2000 2001 /* Take account of CRC bytes */ 2002 frame_size += 2; 2003 2004 /* Write one frame to device */ 2005 ret = mxt_bootloader_write(data, fw->data + pos, frame_size); 2006 if (ret) 2007 goto disable_irq; 2008 2009 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true); 2010 if (ret) { 2011 retry++; 2012 2013 /* Back off by 20ms per retry */ 2014 msleep(retry * 20); 2015 2016 if (retry > 20) { 2017 dev_err(dev, "Retry count exceeded\n"); 2018 goto disable_irq; 2019 } 2020 } else { 2021 retry = 0; 2022 pos += frame_size; 2023 frame++; 2024 } 2025 2026 if (frame % 50 == 0) 2027 dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n", 2028 frame, pos, fw->size); 2029 } 2030 2031 /* Wait for flash. */ 2032 ret = mxt_wait_for_completion(data, &data->bl_completion, 2033 MXT_FW_RESET_TIME); 2034 if (ret) 2035 goto disable_irq; 2036 2037 dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos); 2038 2039 /* 2040 * Wait for device to reset. Some bootloader versions do not assert 2041 * the CHG line after bootloading has finished, so ignore potential 2042 * errors. 2043 */ 2044 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME); 2045 2046 data->in_bootloader = false; 2047 2048 disable_irq: 2049 disable_irq(data->irq); 2050 release_firmware: 2051 release_firmware(fw); 2052 return ret; 2053 } 2054 2055 static ssize_t mxt_update_fw_store(struct device *dev, 2056 struct device_attribute *attr, 2057 const char *buf, size_t count) 2058 { 2059 struct mxt_data *data = dev_get_drvdata(dev); 2060 int error; 2061 2062 error = mxt_load_fw(dev, MXT_FW_NAME); 2063 if (error) { 2064 dev_err(dev, "The firmware update failed(%d)\n", error); 2065 count = error; 2066 } else { 2067 dev_info(dev, "The firmware update succeeded\n"); 2068 2069 error = mxt_initialize(data); 2070 if (error) 2071 return error; 2072 } 2073 2074 return count; 2075 } 2076 2077 static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL); 2078 static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL); 2079 static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL); 2080 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store); 2081 2082 static struct attribute *mxt_attrs[] = { 2083 &dev_attr_fw_version.attr, 2084 &dev_attr_hw_version.attr, 2085 &dev_attr_object.attr, 2086 &dev_attr_update_fw.attr, 2087 NULL 2088 }; 2089 2090 static const struct attribute_group mxt_attr_group = { 2091 .attrs = mxt_attrs, 2092 }; 2093 2094 static void mxt_start(struct mxt_data *data) 2095 { 2096 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN); 2097 2098 /* Recalibrate since chip has been in deep sleep */ 2099 mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false); 2100 } 2101 2102 static void mxt_stop(struct mxt_data *data) 2103 { 2104 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_DEEPSLEEP); 2105 } 2106 2107 static int mxt_input_open(struct input_dev *dev) 2108 { 2109 struct mxt_data *data = input_get_drvdata(dev); 2110 2111 mxt_start(data); 2112 2113 return 0; 2114 } 2115 2116 static void mxt_input_close(struct input_dev *dev) 2117 { 2118 struct mxt_data *data = input_get_drvdata(dev); 2119 2120 mxt_stop(data); 2121 } 2122 2123 #ifdef CONFIG_OF 2124 static struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client) 2125 { 2126 struct mxt_platform_data *pdata; 2127 u32 *keymap; 2128 u32 keycode; 2129 int proplen, i, ret; 2130 2131 if (!client->dev.of_node) 2132 return ERR_PTR(-ENODEV); 2133 2134 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); 2135 if (!pdata) 2136 return ERR_PTR(-ENOMEM); 2137 2138 if (of_find_property(client->dev.of_node, "linux,gpio-keymap", 2139 &proplen)) { 2140 pdata->t19_num_keys = proplen / sizeof(u32); 2141 2142 keymap = devm_kzalloc(&client->dev, 2143 pdata->t19_num_keys * sizeof(keymap[0]), 2144 GFP_KERNEL); 2145 if (!keymap) 2146 return ERR_PTR(-ENOMEM); 2147 2148 for (i = 0; i < pdata->t19_num_keys; i++) { 2149 ret = of_property_read_u32_index(client->dev.of_node, 2150 "linux,gpio-keymap", i, &keycode); 2151 if (ret) 2152 keycode = KEY_RESERVED; 2153 2154 keymap[i] = keycode; 2155 } 2156 2157 pdata->t19_keymap = keymap; 2158 } 2159 2160 return pdata; 2161 } 2162 #else 2163 static struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client) 2164 { 2165 dev_dbg(&client->dev, "No platform data specified\n"); 2166 return ERR_PTR(-EINVAL); 2167 } 2168 #endif 2169 2170 static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id) 2171 { 2172 struct mxt_data *data; 2173 const struct mxt_platform_data *pdata; 2174 int error; 2175 2176 pdata = dev_get_platdata(&client->dev); 2177 if (!pdata) { 2178 pdata = mxt_parse_dt(client); 2179 if (IS_ERR(pdata)) 2180 return PTR_ERR(pdata); 2181 } 2182 2183 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL); 2184 if (!data) { 2185 dev_err(&client->dev, "Failed to allocate memory\n"); 2186 return -ENOMEM; 2187 } 2188 2189 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0", 2190 client->adapter->nr, client->addr); 2191 2192 data->client = client; 2193 data->pdata = pdata; 2194 data->irq = client->irq; 2195 i2c_set_clientdata(client, data); 2196 2197 init_completion(&data->bl_completion); 2198 init_completion(&data->reset_completion); 2199 init_completion(&data->crc_completion); 2200 2201 error = request_threaded_irq(client->irq, NULL, mxt_interrupt, 2202 pdata->irqflags | IRQF_ONESHOT, 2203 client->name, data); 2204 if (error) { 2205 dev_err(&client->dev, "Failed to register interrupt\n"); 2206 goto err_free_mem; 2207 } 2208 2209 disable_irq(client->irq); 2210 2211 error = mxt_initialize(data); 2212 if (error) 2213 goto err_free_irq; 2214 2215 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group); 2216 if (error) { 2217 dev_err(&client->dev, "Failure %d creating sysfs group\n", 2218 error); 2219 goto err_free_object; 2220 } 2221 2222 return 0; 2223 2224 err_free_object: 2225 mxt_free_input_device(data); 2226 mxt_free_object_table(data); 2227 err_free_irq: 2228 free_irq(client->irq, data); 2229 err_free_mem: 2230 kfree(data); 2231 return error; 2232 } 2233 2234 static int mxt_remove(struct i2c_client *client) 2235 { 2236 struct mxt_data *data = i2c_get_clientdata(client); 2237 2238 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group); 2239 free_irq(data->irq, data); 2240 mxt_free_input_device(data); 2241 mxt_free_object_table(data); 2242 kfree(data); 2243 2244 return 0; 2245 } 2246 2247 #ifdef CONFIG_PM_SLEEP 2248 static int mxt_suspend(struct device *dev) 2249 { 2250 struct i2c_client *client = to_i2c_client(dev); 2251 struct mxt_data *data = i2c_get_clientdata(client); 2252 struct input_dev *input_dev = data->input_dev; 2253 2254 mutex_lock(&input_dev->mutex); 2255 2256 if (input_dev->users) 2257 mxt_stop(data); 2258 2259 mutex_unlock(&input_dev->mutex); 2260 2261 return 0; 2262 } 2263 2264 static int mxt_resume(struct device *dev) 2265 { 2266 struct i2c_client *client = to_i2c_client(dev); 2267 struct mxt_data *data = i2c_get_clientdata(client); 2268 struct input_dev *input_dev = data->input_dev; 2269 2270 mutex_lock(&input_dev->mutex); 2271 2272 if (input_dev->users) 2273 mxt_start(data); 2274 2275 mutex_unlock(&input_dev->mutex); 2276 2277 return 0; 2278 } 2279 #endif 2280 2281 static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume); 2282 2283 static const struct of_device_id mxt_of_match[] = { 2284 { .compatible = "atmel,maxtouch", }, 2285 {}, 2286 }; 2287 MODULE_DEVICE_TABLE(of, mxt_of_match); 2288 2289 static const struct i2c_device_id mxt_id[] = { 2290 { "qt602240_ts", 0 }, 2291 { "atmel_mxt_ts", 0 }, 2292 { "atmel_mxt_tp", 0 }, 2293 { "mXT224", 0 }, 2294 { } 2295 }; 2296 MODULE_DEVICE_TABLE(i2c, mxt_id); 2297 2298 static struct i2c_driver mxt_driver = { 2299 .driver = { 2300 .name = "atmel_mxt_ts", 2301 .owner = THIS_MODULE, 2302 .of_match_table = of_match_ptr(mxt_of_match), 2303 .pm = &mxt_pm_ops, 2304 }, 2305 .probe = mxt_probe, 2306 .remove = mxt_remove, 2307 .id_table = mxt_id, 2308 }; 2309 2310 module_i2c_driver(mxt_driver); 2311 2312 /* Module information */ 2313 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>"); 2314 MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver"); 2315 MODULE_LICENSE("GPL"); 2316