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