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 * Copyright (C) 2016 Zodiac Inflight Innovations 8 * 9 * Author: Joonyoung Shim <jy0922.shim@samsung.com> 10 * 11 * This program is free software; you can redistribute it and/or modify it 12 * under the terms of the GNU General Public License as published by the 13 * Free Software Foundation; either version 2 of the License, or (at your 14 * option) any later version. 15 * 16 */ 17 18 #include <linux/acpi.h> 19 #include <linux/dmi.h> 20 #include <linux/module.h> 21 #include <linux/init.h> 22 #include <linux/completion.h> 23 #include <linux/delay.h> 24 #include <linux/firmware.h> 25 #include <linux/i2c.h> 26 #include <linux/platform_data/atmel_mxt_ts.h> 27 #include <linux/input/mt.h> 28 #include <linux/interrupt.h> 29 #include <linux/of.h> 30 #include <linux/slab.h> 31 #include <linux/gpio/consumer.h> 32 #include <asm/unaligned.h> 33 #include <media/v4l2-device.h> 34 #include <media/v4l2-ioctl.h> 35 #include <media/videobuf2-v4l2.h> 36 #include <media/videobuf2-vmalloc.h> 37 38 /* Firmware files */ 39 #define MXT_FW_NAME "maxtouch.fw" 40 #define MXT_CFG_NAME "maxtouch.cfg" 41 #define MXT_CFG_MAGIC "OBP_RAW V1" 42 43 /* Registers */ 44 #define MXT_OBJECT_START 0x07 45 #define MXT_OBJECT_SIZE 6 46 #define MXT_INFO_CHECKSUM_SIZE 3 47 #define MXT_MAX_BLOCK_WRITE 256 48 49 /* Object types */ 50 #define MXT_DEBUG_DIAGNOSTIC_T37 37 51 #define MXT_GEN_MESSAGE_T5 5 52 #define MXT_GEN_COMMAND_T6 6 53 #define MXT_GEN_POWER_T7 7 54 #define MXT_GEN_ACQUIRE_T8 8 55 #define MXT_GEN_DATASOURCE_T53 53 56 #define MXT_TOUCH_MULTI_T9 9 57 #define MXT_TOUCH_KEYARRAY_T15 15 58 #define MXT_TOUCH_PROXIMITY_T23 23 59 #define MXT_TOUCH_PROXKEY_T52 52 60 #define MXT_PROCI_GRIPFACE_T20 20 61 #define MXT_PROCG_NOISE_T22 22 62 #define MXT_PROCI_ONETOUCH_T24 24 63 #define MXT_PROCI_TWOTOUCH_T27 27 64 #define MXT_PROCI_GRIP_T40 40 65 #define MXT_PROCI_PALM_T41 41 66 #define MXT_PROCI_TOUCHSUPPRESSION_T42 42 67 #define MXT_PROCI_STYLUS_T47 47 68 #define MXT_PROCG_NOISESUPPRESSION_T48 48 69 #define MXT_SPT_COMMSCONFIG_T18 18 70 #define MXT_SPT_GPIOPWM_T19 19 71 #define MXT_SPT_SELFTEST_T25 25 72 #define MXT_SPT_CTECONFIG_T28 28 73 #define MXT_SPT_USERDATA_T38 38 74 #define MXT_SPT_DIGITIZER_T43 43 75 #define MXT_SPT_MESSAGECOUNT_T44 44 76 #define MXT_SPT_CTECONFIG_T46 46 77 #define MXT_TOUCH_MULTITOUCHSCREEN_T100 100 78 79 /* MXT_GEN_MESSAGE_T5 object */ 80 #define MXT_RPTID_NOMSG 0xff 81 82 /* MXT_GEN_COMMAND_T6 field */ 83 #define MXT_COMMAND_RESET 0 84 #define MXT_COMMAND_BACKUPNV 1 85 #define MXT_COMMAND_CALIBRATE 2 86 #define MXT_COMMAND_REPORTALL 3 87 #define MXT_COMMAND_DIAGNOSTIC 5 88 89 /* Define for T6 status byte */ 90 #define MXT_T6_STATUS_RESET (1 << 7) 91 #define MXT_T6_STATUS_OFL (1 << 6) 92 #define MXT_T6_STATUS_SIGERR (1 << 5) 93 #define MXT_T6_STATUS_CAL (1 << 4) 94 #define MXT_T6_STATUS_CFGERR (1 << 3) 95 #define MXT_T6_STATUS_COMSERR (1 << 2) 96 97 /* MXT_GEN_POWER_T7 field */ 98 struct t7_config { 99 u8 idle; 100 u8 active; 101 } __packed; 102 103 #define MXT_POWER_CFG_RUN 0 104 #define MXT_POWER_CFG_DEEPSLEEP 1 105 106 /* MXT_TOUCH_MULTI_T9 field */ 107 #define MXT_T9_CTRL 0 108 #define MXT_T9_XSIZE 3 109 #define MXT_T9_YSIZE 4 110 #define MXT_T9_ORIENT 9 111 #define MXT_T9_RANGE 18 112 113 /* MXT_TOUCH_MULTI_T9 status */ 114 #define MXT_T9_UNGRIP (1 << 0) 115 #define MXT_T9_SUPPRESS (1 << 1) 116 #define MXT_T9_AMP (1 << 2) 117 #define MXT_T9_VECTOR (1 << 3) 118 #define MXT_T9_MOVE (1 << 4) 119 #define MXT_T9_RELEASE (1 << 5) 120 #define MXT_T9_PRESS (1 << 6) 121 #define MXT_T9_DETECT (1 << 7) 122 123 struct t9_range { 124 __le16 x; 125 __le16 y; 126 } __packed; 127 128 /* MXT_TOUCH_MULTI_T9 orient */ 129 #define MXT_T9_ORIENT_SWITCH (1 << 0) 130 #define MXT_T9_ORIENT_INVERTX (1 << 1) 131 #define MXT_T9_ORIENT_INVERTY (1 << 2) 132 133 /* MXT_SPT_COMMSCONFIG_T18 */ 134 #define MXT_COMMS_CTRL 0 135 #define MXT_COMMS_CMD 1 136 137 /* MXT_DEBUG_DIAGNOSTIC_T37 */ 138 #define MXT_DIAGNOSTIC_PAGEUP 0x01 139 #define MXT_DIAGNOSTIC_DELTAS 0x10 140 #define MXT_DIAGNOSTIC_REFS 0x11 141 #define MXT_DIAGNOSTIC_SIZE 128 142 143 #define MXT_FAMILY_1386 160 144 #define MXT1386_COLUMNS 3 145 #define MXT1386_PAGES_PER_COLUMN 8 146 147 struct t37_debug { 148 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37 149 u8 mode; 150 u8 page; 151 u8 data[MXT_DIAGNOSTIC_SIZE]; 152 #endif 153 }; 154 155 /* Define for MXT_GEN_COMMAND_T6 */ 156 #define MXT_BOOT_VALUE 0xa5 157 #define MXT_RESET_VALUE 0x01 158 #define MXT_BACKUP_VALUE 0x55 159 160 /* T100 Multiple Touch Touchscreen */ 161 #define MXT_T100_CTRL 0 162 #define MXT_T100_CFG1 1 163 #define MXT_T100_TCHAUX 3 164 #define MXT_T100_XSIZE 9 165 #define MXT_T100_XRANGE 13 166 #define MXT_T100_YSIZE 20 167 #define MXT_T100_YRANGE 24 168 169 #define MXT_T100_CFG_SWITCHXY BIT(5) 170 #define MXT_T100_CFG_INVERTY BIT(6) 171 #define MXT_T100_CFG_INVERTX BIT(7) 172 173 #define MXT_T100_TCHAUX_VECT BIT(0) 174 #define MXT_T100_TCHAUX_AMPL BIT(1) 175 #define MXT_T100_TCHAUX_AREA BIT(2) 176 177 #define MXT_T100_DETECT BIT(7) 178 #define MXT_T100_TYPE_MASK 0x70 179 180 enum t100_type { 181 MXT_T100_TYPE_FINGER = 1, 182 MXT_T100_TYPE_PASSIVE_STYLUS = 2, 183 MXT_T100_TYPE_HOVERING_FINGER = 4, 184 MXT_T100_TYPE_GLOVE = 5, 185 MXT_T100_TYPE_LARGE_TOUCH = 6, 186 }; 187 188 #define MXT_DISTANCE_ACTIVE_TOUCH 0 189 #define MXT_DISTANCE_HOVERING 1 190 191 #define MXT_TOUCH_MAJOR_DEFAULT 1 192 #define MXT_PRESSURE_DEFAULT 1 193 194 /* Delay times */ 195 #define MXT_BACKUP_TIME 50 /* msec */ 196 #define MXT_RESET_TIME 200 /* msec */ 197 #define MXT_RESET_TIMEOUT 3000 /* msec */ 198 #define MXT_CRC_TIMEOUT 1000 /* msec */ 199 #define MXT_FW_RESET_TIME 3000 /* msec */ 200 #define MXT_FW_CHG_TIMEOUT 300 /* msec */ 201 202 /* Command to unlock bootloader */ 203 #define MXT_UNLOCK_CMD_MSB 0xaa 204 #define MXT_UNLOCK_CMD_LSB 0xdc 205 206 /* Bootloader mode status */ 207 #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */ 208 #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */ 209 #define MXT_FRAME_CRC_CHECK 0x02 210 #define MXT_FRAME_CRC_FAIL 0x03 211 #define MXT_FRAME_CRC_PASS 0x04 212 #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */ 213 #define MXT_BOOT_STATUS_MASK 0x3f 214 #define MXT_BOOT_EXTENDED_ID (1 << 5) 215 #define MXT_BOOT_ID_MASK 0x1f 216 217 /* Touchscreen absolute values */ 218 #define MXT_MAX_AREA 0xff 219 220 #define MXT_PIXELS_PER_MM 20 221 222 struct mxt_info { 223 u8 family_id; 224 u8 variant_id; 225 u8 version; 226 u8 build; 227 u8 matrix_xsize; 228 u8 matrix_ysize; 229 u8 object_num; 230 }; 231 232 struct mxt_object { 233 u8 type; 234 u16 start_address; 235 u8 size_minus_one; 236 u8 instances_minus_one; 237 u8 num_report_ids; 238 } __packed; 239 240 struct mxt_dbg { 241 u16 t37_address; 242 u16 diag_cmd_address; 243 struct t37_debug *t37_buf; 244 unsigned int t37_pages; 245 unsigned int t37_nodes; 246 247 struct v4l2_device v4l2; 248 struct v4l2_pix_format format; 249 struct video_device vdev; 250 struct vb2_queue queue; 251 struct mutex lock; 252 int input; 253 }; 254 255 enum v4l_dbg_inputs { 256 MXT_V4L_INPUT_DELTAS, 257 MXT_V4L_INPUT_REFS, 258 MXT_V4L_INPUT_MAX, 259 }; 260 261 static const struct v4l2_file_operations mxt_video_fops = { 262 .owner = THIS_MODULE, 263 .open = v4l2_fh_open, 264 .release = vb2_fop_release, 265 .unlocked_ioctl = video_ioctl2, 266 .read = vb2_fop_read, 267 .mmap = vb2_fop_mmap, 268 .poll = vb2_fop_poll, 269 }; 270 271 /* Each client has this additional data */ 272 struct mxt_data { 273 struct i2c_client *client; 274 struct input_dev *input_dev; 275 char phys[64]; /* device physical location */ 276 const struct mxt_platform_data *pdata; 277 struct mxt_object *object_table; 278 struct mxt_info info; 279 unsigned int irq; 280 unsigned int max_x; 281 unsigned int max_y; 282 bool invertx; 283 bool inverty; 284 bool xy_switch; 285 u8 xsize; 286 u8 ysize; 287 bool in_bootloader; 288 u16 mem_size; 289 u8 t100_aux_ampl; 290 u8 t100_aux_area; 291 u8 t100_aux_vect; 292 u8 max_reportid; 293 u32 config_crc; 294 u32 info_crc; 295 u8 bootloader_addr; 296 u8 *msg_buf; 297 u8 t6_status; 298 bool update_input; 299 u8 last_message_count; 300 u8 num_touchids; 301 u8 multitouch; 302 struct t7_config t7_cfg; 303 struct mxt_dbg dbg; 304 struct gpio_desc *reset_gpio; 305 306 /* Cached parameters from object table */ 307 u16 T5_address; 308 u8 T5_msg_size; 309 u8 T6_reportid; 310 u16 T6_address; 311 u16 T7_address; 312 u8 T9_reportid_min; 313 u8 T9_reportid_max; 314 u8 T19_reportid; 315 u16 T44_address; 316 u8 T100_reportid_min; 317 u8 T100_reportid_max; 318 319 /* for fw update in bootloader */ 320 struct completion bl_completion; 321 322 /* for reset handling */ 323 struct completion reset_completion; 324 325 /* for config update handling */ 326 struct completion crc_completion; 327 }; 328 329 struct mxt_vb2_buffer { 330 struct vb2_buffer vb; 331 struct list_head list; 332 }; 333 334 static size_t mxt_obj_size(const struct mxt_object *obj) 335 { 336 return obj->size_minus_one + 1; 337 } 338 339 static size_t mxt_obj_instances(const struct mxt_object *obj) 340 { 341 return obj->instances_minus_one + 1; 342 } 343 344 static bool mxt_object_readable(unsigned int type) 345 { 346 switch (type) { 347 case MXT_GEN_COMMAND_T6: 348 case MXT_GEN_POWER_T7: 349 case MXT_GEN_ACQUIRE_T8: 350 case MXT_GEN_DATASOURCE_T53: 351 case MXT_TOUCH_MULTI_T9: 352 case MXT_TOUCH_KEYARRAY_T15: 353 case MXT_TOUCH_PROXIMITY_T23: 354 case MXT_TOUCH_PROXKEY_T52: 355 case MXT_TOUCH_MULTITOUCHSCREEN_T100: 356 case MXT_PROCI_GRIPFACE_T20: 357 case MXT_PROCG_NOISE_T22: 358 case MXT_PROCI_ONETOUCH_T24: 359 case MXT_PROCI_TWOTOUCH_T27: 360 case MXT_PROCI_GRIP_T40: 361 case MXT_PROCI_PALM_T41: 362 case MXT_PROCI_TOUCHSUPPRESSION_T42: 363 case MXT_PROCI_STYLUS_T47: 364 case MXT_PROCG_NOISESUPPRESSION_T48: 365 case MXT_SPT_COMMSCONFIG_T18: 366 case MXT_SPT_GPIOPWM_T19: 367 case MXT_SPT_SELFTEST_T25: 368 case MXT_SPT_CTECONFIG_T28: 369 case MXT_SPT_USERDATA_T38: 370 case MXT_SPT_DIGITIZER_T43: 371 case MXT_SPT_CTECONFIG_T46: 372 return true; 373 default: 374 return false; 375 } 376 } 377 378 static void mxt_dump_message(struct mxt_data *data, u8 *message) 379 { 380 dev_dbg(&data->client->dev, "message: %*ph\n", 381 data->T5_msg_size, message); 382 } 383 384 static int mxt_wait_for_completion(struct mxt_data *data, 385 struct completion *comp, 386 unsigned int timeout_ms) 387 { 388 struct device *dev = &data->client->dev; 389 unsigned long timeout = msecs_to_jiffies(timeout_ms); 390 long ret; 391 392 ret = wait_for_completion_interruptible_timeout(comp, timeout); 393 if (ret < 0) { 394 return ret; 395 } else if (ret == 0) { 396 dev_err(dev, "Wait for completion timed out.\n"); 397 return -ETIMEDOUT; 398 } 399 return 0; 400 } 401 402 static int mxt_bootloader_read(struct mxt_data *data, 403 u8 *val, unsigned int count) 404 { 405 int ret; 406 struct i2c_msg msg; 407 408 msg.addr = data->bootloader_addr; 409 msg.flags = data->client->flags & I2C_M_TEN; 410 msg.flags |= I2C_M_RD; 411 msg.len = count; 412 msg.buf = val; 413 414 ret = i2c_transfer(data->client->adapter, &msg, 1); 415 if (ret == 1) { 416 ret = 0; 417 } else { 418 ret = ret < 0 ? ret : -EIO; 419 dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n", 420 __func__, ret); 421 } 422 423 return ret; 424 } 425 426 static int mxt_bootloader_write(struct mxt_data *data, 427 const u8 * const val, unsigned int count) 428 { 429 int ret; 430 struct i2c_msg msg; 431 432 msg.addr = data->bootloader_addr; 433 msg.flags = data->client->flags & I2C_M_TEN; 434 msg.len = count; 435 msg.buf = (u8 *)val; 436 437 ret = i2c_transfer(data->client->adapter, &msg, 1); 438 if (ret == 1) { 439 ret = 0; 440 } else { 441 ret = ret < 0 ? ret : -EIO; 442 dev_err(&data->client->dev, "%s: i2c send failed (%d)\n", 443 __func__, ret); 444 } 445 446 return ret; 447 } 448 449 static int mxt_lookup_bootloader_address(struct mxt_data *data, bool retry) 450 { 451 u8 appmode = data->client->addr; 452 u8 bootloader; 453 454 switch (appmode) { 455 case 0x4a: 456 case 0x4b: 457 /* Chips after 1664S use different scheme */ 458 if (retry || data->info.family_id >= 0xa2) { 459 bootloader = appmode - 0x24; 460 break; 461 } 462 /* Fall through for normal case */ 463 case 0x4c: 464 case 0x4d: 465 case 0x5a: 466 case 0x5b: 467 bootloader = appmode - 0x26; 468 break; 469 470 default: 471 dev_err(&data->client->dev, 472 "Appmode i2c address 0x%02x not found\n", 473 appmode); 474 return -EINVAL; 475 } 476 477 data->bootloader_addr = bootloader; 478 return 0; 479 } 480 481 static int mxt_probe_bootloader(struct mxt_data *data, bool alt_address) 482 { 483 struct device *dev = &data->client->dev; 484 int error; 485 u8 val; 486 bool crc_failure; 487 488 error = mxt_lookup_bootloader_address(data, alt_address); 489 if (error) 490 return error; 491 492 error = mxt_bootloader_read(data, &val, 1); 493 if (error) 494 return error; 495 496 /* Check app crc fail mode */ 497 crc_failure = (val & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL; 498 499 dev_err(dev, "Detected bootloader, status:%02X%s\n", 500 val, crc_failure ? ", APP_CRC_FAIL" : ""); 501 502 return 0; 503 } 504 505 static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val) 506 { 507 struct device *dev = &data->client->dev; 508 u8 buf[3]; 509 510 if (val & MXT_BOOT_EXTENDED_ID) { 511 if (mxt_bootloader_read(data, &buf[0], 3) != 0) { 512 dev_err(dev, "%s: i2c failure\n", __func__); 513 return val; 514 } 515 516 dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]); 517 518 return buf[0]; 519 } else { 520 dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK); 521 522 return val; 523 } 524 } 525 526 static int mxt_check_bootloader(struct mxt_data *data, unsigned int state, 527 bool wait) 528 { 529 struct device *dev = &data->client->dev; 530 u8 val; 531 int ret; 532 533 recheck: 534 if (wait) { 535 /* 536 * In application update mode, the interrupt 537 * line signals state transitions. We must wait for the 538 * CHG assertion before reading the status byte. 539 * Once the status byte has been read, the line is deasserted. 540 */ 541 ret = mxt_wait_for_completion(data, &data->bl_completion, 542 MXT_FW_CHG_TIMEOUT); 543 if (ret) { 544 /* 545 * TODO: handle -ERESTARTSYS better by terminating 546 * fw update process before returning to userspace 547 * by writing length 0x000 to device (iff we are in 548 * WAITING_FRAME_DATA state). 549 */ 550 dev_err(dev, "Update wait error %d\n", ret); 551 return ret; 552 } 553 } 554 555 ret = mxt_bootloader_read(data, &val, 1); 556 if (ret) 557 return ret; 558 559 if (state == MXT_WAITING_BOOTLOAD_CMD) 560 val = mxt_get_bootloader_version(data, val); 561 562 switch (state) { 563 case MXT_WAITING_BOOTLOAD_CMD: 564 case MXT_WAITING_FRAME_DATA: 565 case MXT_APP_CRC_FAIL: 566 val &= ~MXT_BOOT_STATUS_MASK; 567 break; 568 case MXT_FRAME_CRC_PASS: 569 if (val == MXT_FRAME_CRC_CHECK) { 570 goto recheck; 571 } else if (val == MXT_FRAME_CRC_FAIL) { 572 dev_err(dev, "Bootloader CRC fail\n"); 573 return -EINVAL; 574 } 575 break; 576 default: 577 return -EINVAL; 578 } 579 580 if (val != state) { 581 dev_err(dev, "Invalid bootloader state %02X != %02X\n", 582 val, state); 583 return -EINVAL; 584 } 585 586 return 0; 587 } 588 589 static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock) 590 { 591 int ret; 592 u8 buf[2]; 593 594 if (unlock) { 595 buf[0] = MXT_UNLOCK_CMD_LSB; 596 buf[1] = MXT_UNLOCK_CMD_MSB; 597 } else { 598 buf[0] = 0x01; 599 buf[1] = 0x01; 600 } 601 602 ret = mxt_bootloader_write(data, buf, 2); 603 if (ret) 604 return ret; 605 606 return 0; 607 } 608 609 static int __mxt_read_reg(struct i2c_client *client, 610 u16 reg, u16 len, void *val) 611 { 612 struct i2c_msg xfer[2]; 613 u8 buf[2]; 614 int ret; 615 616 buf[0] = reg & 0xff; 617 buf[1] = (reg >> 8) & 0xff; 618 619 /* Write register */ 620 xfer[0].addr = client->addr; 621 xfer[0].flags = 0; 622 xfer[0].len = 2; 623 xfer[0].buf = buf; 624 625 /* Read data */ 626 xfer[1].addr = client->addr; 627 xfer[1].flags = I2C_M_RD; 628 xfer[1].len = len; 629 xfer[1].buf = val; 630 631 ret = i2c_transfer(client->adapter, xfer, 2); 632 if (ret == 2) { 633 ret = 0; 634 } else { 635 if (ret >= 0) 636 ret = -EIO; 637 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n", 638 __func__, ret); 639 } 640 641 return ret; 642 } 643 644 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len, 645 const void *val) 646 { 647 u8 *buf; 648 size_t count; 649 int ret; 650 651 count = len + 2; 652 buf = kmalloc(count, GFP_KERNEL); 653 if (!buf) 654 return -ENOMEM; 655 656 buf[0] = reg & 0xff; 657 buf[1] = (reg >> 8) & 0xff; 658 memcpy(&buf[2], val, len); 659 660 ret = i2c_master_send(client, buf, count); 661 if (ret == count) { 662 ret = 0; 663 } else { 664 if (ret >= 0) 665 ret = -EIO; 666 dev_err(&client->dev, "%s: i2c send failed (%d)\n", 667 __func__, ret); 668 } 669 670 kfree(buf); 671 return ret; 672 } 673 674 static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val) 675 { 676 return __mxt_write_reg(client, reg, 1, &val); 677 } 678 679 static struct mxt_object * 680 mxt_get_object(struct mxt_data *data, u8 type) 681 { 682 struct mxt_object *object; 683 int i; 684 685 for (i = 0; i < data->info.object_num; i++) { 686 object = data->object_table + i; 687 if (object->type == type) 688 return object; 689 } 690 691 dev_warn(&data->client->dev, "Invalid object type T%u\n", type); 692 return NULL; 693 } 694 695 static void mxt_proc_t6_messages(struct mxt_data *data, u8 *msg) 696 { 697 struct device *dev = &data->client->dev; 698 u8 status = msg[1]; 699 u32 crc = msg[2] | (msg[3] << 8) | (msg[4] << 16); 700 701 complete(&data->crc_completion); 702 703 if (crc != data->config_crc) { 704 data->config_crc = crc; 705 dev_dbg(dev, "T6 Config Checksum: 0x%06X\n", crc); 706 } 707 708 /* Detect reset */ 709 if (status & MXT_T6_STATUS_RESET) 710 complete(&data->reset_completion); 711 712 /* Output debug if status has changed */ 713 if (status != data->t6_status) 714 dev_dbg(dev, "T6 Status 0x%02X%s%s%s%s%s%s%s\n", 715 status, 716 status == 0 ? " OK" : "", 717 status & MXT_T6_STATUS_RESET ? " RESET" : "", 718 status & MXT_T6_STATUS_OFL ? " OFL" : "", 719 status & MXT_T6_STATUS_SIGERR ? " SIGERR" : "", 720 status & MXT_T6_STATUS_CAL ? " CAL" : "", 721 status & MXT_T6_STATUS_CFGERR ? " CFGERR" : "", 722 status & MXT_T6_STATUS_COMSERR ? " COMSERR" : ""); 723 724 /* Save current status */ 725 data->t6_status = status; 726 } 727 728 static int mxt_write_object(struct mxt_data *data, 729 u8 type, u8 offset, u8 val) 730 { 731 struct mxt_object *object; 732 u16 reg; 733 734 object = mxt_get_object(data, type); 735 if (!object || offset >= mxt_obj_size(object)) 736 return -EINVAL; 737 738 reg = object->start_address; 739 return mxt_write_reg(data->client, reg + offset, val); 740 } 741 742 static void mxt_input_button(struct mxt_data *data, u8 *message) 743 { 744 struct input_dev *input = data->input_dev; 745 const struct mxt_platform_data *pdata = data->pdata; 746 int i; 747 748 for (i = 0; i < pdata->t19_num_keys; i++) { 749 if (pdata->t19_keymap[i] == KEY_RESERVED) 750 continue; 751 752 /* Active-low switch */ 753 input_report_key(input, pdata->t19_keymap[i], 754 !(message[1] & BIT(i))); 755 } 756 } 757 758 static void mxt_input_sync(struct mxt_data *data) 759 { 760 input_mt_report_pointer_emulation(data->input_dev, 761 data->pdata->t19_num_keys); 762 input_sync(data->input_dev); 763 } 764 765 static void mxt_proc_t9_message(struct mxt_data *data, u8 *message) 766 { 767 struct device *dev = &data->client->dev; 768 struct input_dev *input_dev = data->input_dev; 769 int id; 770 u8 status; 771 int x; 772 int y; 773 int area; 774 int amplitude; 775 776 id = message[0] - data->T9_reportid_min; 777 status = message[1]; 778 x = (message[2] << 4) | ((message[4] >> 4) & 0xf); 779 y = (message[3] << 4) | ((message[4] & 0xf)); 780 781 /* Handle 10/12 bit switching */ 782 if (data->max_x < 1024) 783 x >>= 2; 784 if (data->max_y < 1024) 785 y >>= 2; 786 787 area = message[5]; 788 amplitude = message[6]; 789 790 dev_dbg(dev, 791 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n", 792 id, 793 (status & MXT_T9_DETECT) ? 'D' : '.', 794 (status & MXT_T9_PRESS) ? 'P' : '.', 795 (status & MXT_T9_RELEASE) ? 'R' : '.', 796 (status & MXT_T9_MOVE) ? 'M' : '.', 797 (status & MXT_T9_VECTOR) ? 'V' : '.', 798 (status & MXT_T9_AMP) ? 'A' : '.', 799 (status & MXT_T9_SUPPRESS) ? 'S' : '.', 800 (status & MXT_T9_UNGRIP) ? 'U' : '.', 801 x, y, area, amplitude); 802 803 input_mt_slot(input_dev, id); 804 805 if (status & MXT_T9_DETECT) { 806 /* 807 * Multiple bits may be set if the host is slow to read 808 * the status messages, indicating all the events that 809 * have happened. 810 */ 811 if (status & MXT_T9_RELEASE) { 812 input_mt_report_slot_state(input_dev, 813 MT_TOOL_FINGER, 0); 814 mxt_input_sync(data); 815 } 816 817 /* Touch active */ 818 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1); 819 input_report_abs(input_dev, ABS_MT_POSITION_X, x); 820 input_report_abs(input_dev, ABS_MT_POSITION_Y, y); 821 input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude); 822 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area); 823 } else { 824 /* Touch no longer active, close out slot */ 825 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0); 826 } 827 828 data->update_input = true; 829 } 830 831 static void mxt_proc_t100_message(struct mxt_data *data, u8 *message) 832 { 833 struct device *dev = &data->client->dev; 834 struct input_dev *input_dev = data->input_dev; 835 int id; 836 u8 status; 837 u8 type = 0; 838 u16 x; 839 u16 y; 840 int distance = 0; 841 int tool = 0; 842 u8 major = 0; 843 u8 pressure = 0; 844 u8 orientation = 0; 845 846 id = message[0] - data->T100_reportid_min - 2; 847 848 /* ignore SCRSTATUS events */ 849 if (id < 0) 850 return; 851 852 status = message[1]; 853 x = get_unaligned_le16(&message[2]); 854 y = get_unaligned_le16(&message[4]); 855 856 if (status & MXT_T100_DETECT) { 857 type = (status & MXT_T100_TYPE_MASK) >> 4; 858 859 switch (type) { 860 case MXT_T100_TYPE_HOVERING_FINGER: 861 tool = MT_TOOL_FINGER; 862 distance = MXT_DISTANCE_HOVERING; 863 864 if (data->t100_aux_vect) 865 orientation = message[data->t100_aux_vect]; 866 867 break; 868 869 case MXT_T100_TYPE_FINGER: 870 case MXT_T100_TYPE_GLOVE: 871 tool = MT_TOOL_FINGER; 872 distance = MXT_DISTANCE_ACTIVE_TOUCH; 873 874 if (data->t100_aux_area) 875 major = message[data->t100_aux_area]; 876 877 if (data->t100_aux_ampl) 878 pressure = message[data->t100_aux_ampl]; 879 880 if (data->t100_aux_vect) 881 orientation = message[data->t100_aux_vect]; 882 883 break; 884 885 case MXT_T100_TYPE_PASSIVE_STYLUS: 886 tool = MT_TOOL_PEN; 887 888 /* 889 * Passive stylus is reported with size zero so 890 * hardcode. 891 */ 892 major = MXT_TOUCH_MAJOR_DEFAULT; 893 894 if (data->t100_aux_ampl) 895 pressure = message[data->t100_aux_ampl]; 896 897 break; 898 899 case MXT_T100_TYPE_LARGE_TOUCH: 900 /* Ignore suppressed touch */ 901 break; 902 903 default: 904 dev_dbg(dev, "Unexpected T100 type\n"); 905 return; 906 } 907 } 908 909 /* 910 * Values reported should be non-zero if tool is touching the 911 * device 912 */ 913 if (!pressure && type != MXT_T100_TYPE_HOVERING_FINGER) 914 pressure = MXT_PRESSURE_DEFAULT; 915 916 input_mt_slot(input_dev, id); 917 918 if (status & MXT_T100_DETECT) { 919 dev_dbg(dev, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n", 920 id, type, x, y, major, pressure, orientation); 921 922 input_mt_report_slot_state(input_dev, tool, 1); 923 input_report_abs(input_dev, ABS_MT_POSITION_X, x); 924 input_report_abs(input_dev, ABS_MT_POSITION_Y, y); 925 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, major); 926 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure); 927 input_report_abs(input_dev, ABS_MT_DISTANCE, distance); 928 input_report_abs(input_dev, ABS_MT_ORIENTATION, orientation); 929 } else { 930 dev_dbg(dev, "[%u] release\n", id); 931 932 /* close out slot */ 933 input_mt_report_slot_state(input_dev, 0, 0); 934 } 935 936 data->update_input = true; 937 } 938 939 static int mxt_proc_message(struct mxt_data *data, u8 *message) 940 { 941 u8 report_id = message[0]; 942 943 if (report_id == MXT_RPTID_NOMSG) 944 return 0; 945 946 if (report_id == data->T6_reportid) { 947 mxt_proc_t6_messages(data, message); 948 } else if (!data->input_dev) { 949 /* 950 * Do not report events if input device 951 * is not yet registered. 952 */ 953 mxt_dump_message(data, message); 954 } else if (report_id >= data->T9_reportid_min && 955 report_id <= data->T9_reportid_max) { 956 mxt_proc_t9_message(data, message); 957 } else if (report_id >= data->T100_reportid_min && 958 report_id <= data->T100_reportid_max) { 959 mxt_proc_t100_message(data, message); 960 } else if (report_id == data->T19_reportid) { 961 mxt_input_button(data, message); 962 data->update_input = true; 963 } else { 964 mxt_dump_message(data, message); 965 } 966 967 return 1; 968 } 969 970 static int mxt_read_and_process_messages(struct mxt_data *data, u8 count) 971 { 972 struct device *dev = &data->client->dev; 973 int ret; 974 int i; 975 u8 num_valid = 0; 976 977 /* Safety check for msg_buf */ 978 if (count > data->max_reportid) 979 return -EINVAL; 980 981 /* Process remaining messages if necessary */ 982 ret = __mxt_read_reg(data->client, data->T5_address, 983 data->T5_msg_size * count, data->msg_buf); 984 if (ret) { 985 dev_err(dev, "Failed to read %u messages (%d)\n", count, ret); 986 return ret; 987 } 988 989 for (i = 0; i < count; i++) { 990 ret = mxt_proc_message(data, 991 data->msg_buf + data->T5_msg_size * i); 992 993 if (ret == 1) 994 num_valid++; 995 } 996 997 /* return number of messages read */ 998 return num_valid; 999 } 1000 1001 static irqreturn_t mxt_process_messages_t44(struct mxt_data *data) 1002 { 1003 struct device *dev = &data->client->dev; 1004 int ret; 1005 u8 count, num_left; 1006 1007 /* Read T44 and T5 together */ 1008 ret = __mxt_read_reg(data->client, data->T44_address, 1009 data->T5_msg_size + 1, data->msg_buf); 1010 if (ret) { 1011 dev_err(dev, "Failed to read T44 and T5 (%d)\n", ret); 1012 return IRQ_NONE; 1013 } 1014 1015 count = data->msg_buf[0]; 1016 1017 /* 1018 * This condition may be caused by the CHG line being configured in 1019 * Mode 0. It results in unnecessary I2C operations but it is benign. 1020 */ 1021 if (count == 0) 1022 return IRQ_NONE; 1023 1024 if (count > data->max_reportid) { 1025 dev_warn(dev, "T44 count %d exceeded max report id\n", count); 1026 count = data->max_reportid; 1027 } 1028 1029 /* Process first message */ 1030 ret = mxt_proc_message(data, data->msg_buf + 1); 1031 if (ret < 0) { 1032 dev_warn(dev, "Unexpected invalid message\n"); 1033 return IRQ_NONE; 1034 } 1035 1036 num_left = count - 1; 1037 1038 /* Process remaining messages if necessary */ 1039 if (num_left) { 1040 ret = mxt_read_and_process_messages(data, num_left); 1041 if (ret < 0) 1042 goto end; 1043 else if (ret != num_left) 1044 dev_warn(dev, "Unexpected invalid message\n"); 1045 } 1046 1047 end: 1048 if (data->update_input) { 1049 mxt_input_sync(data); 1050 data->update_input = false; 1051 } 1052 1053 return IRQ_HANDLED; 1054 } 1055 1056 static int mxt_process_messages_until_invalid(struct mxt_data *data) 1057 { 1058 struct device *dev = &data->client->dev; 1059 int count, read; 1060 u8 tries = 2; 1061 1062 count = data->max_reportid; 1063 1064 /* Read messages until we force an invalid */ 1065 do { 1066 read = mxt_read_and_process_messages(data, count); 1067 if (read < count) 1068 return 0; 1069 } while (--tries); 1070 1071 if (data->update_input) { 1072 mxt_input_sync(data); 1073 data->update_input = false; 1074 } 1075 1076 dev_err(dev, "CHG pin isn't cleared\n"); 1077 return -EBUSY; 1078 } 1079 1080 static irqreturn_t mxt_process_messages(struct mxt_data *data) 1081 { 1082 int total_handled, num_handled; 1083 u8 count = data->last_message_count; 1084 1085 if (count < 1 || count > data->max_reportid) 1086 count = 1; 1087 1088 /* include final invalid message */ 1089 total_handled = mxt_read_and_process_messages(data, count + 1); 1090 if (total_handled < 0) 1091 return IRQ_NONE; 1092 /* if there were invalid messages, then we are done */ 1093 else if (total_handled <= count) 1094 goto update_count; 1095 1096 /* keep reading two msgs until one is invalid or reportid limit */ 1097 do { 1098 num_handled = mxt_read_and_process_messages(data, 2); 1099 if (num_handled < 0) 1100 return IRQ_NONE; 1101 1102 total_handled += num_handled; 1103 1104 if (num_handled < 2) 1105 break; 1106 } while (total_handled < data->num_touchids); 1107 1108 update_count: 1109 data->last_message_count = total_handled; 1110 1111 if (data->update_input) { 1112 mxt_input_sync(data); 1113 data->update_input = false; 1114 } 1115 1116 return IRQ_HANDLED; 1117 } 1118 1119 static irqreturn_t mxt_interrupt(int irq, void *dev_id) 1120 { 1121 struct mxt_data *data = dev_id; 1122 1123 if (data->in_bootloader) { 1124 /* bootloader state transition completion */ 1125 complete(&data->bl_completion); 1126 return IRQ_HANDLED; 1127 } 1128 1129 if (!data->object_table) 1130 return IRQ_HANDLED; 1131 1132 if (data->T44_address) { 1133 return mxt_process_messages_t44(data); 1134 } else { 1135 return mxt_process_messages(data); 1136 } 1137 } 1138 1139 static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset, 1140 u8 value, bool wait) 1141 { 1142 u16 reg; 1143 u8 command_register; 1144 int timeout_counter = 0; 1145 int ret; 1146 1147 reg = data->T6_address + cmd_offset; 1148 1149 ret = mxt_write_reg(data->client, reg, value); 1150 if (ret) 1151 return ret; 1152 1153 if (!wait) 1154 return 0; 1155 1156 do { 1157 msleep(20); 1158 ret = __mxt_read_reg(data->client, reg, 1, &command_register); 1159 if (ret) 1160 return ret; 1161 } while (command_register != 0 && timeout_counter++ <= 100); 1162 1163 if (timeout_counter > 100) { 1164 dev_err(&data->client->dev, "Command failed!\n"); 1165 return -EIO; 1166 } 1167 1168 return 0; 1169 } 1170 1171 static int mxt_acquire_irq(struct mxt_data *data) 1172 { 1173 int error; 1174 1175 enable_irq(data->irq); 1176 1177 error = mxt_process_messages_until_invalid(data); 1178 if (error) 1179 return error; 1180 1181 return 0; 1182 } 1183 1184 static int mxt_soft_reset(struct mxt_data *data) 1185 { 1186 struct device *dev = &data->client->dev; 1187 int ret = 0; 1188 1189 dev_info(dev, "Resetting device\n"); 1190 1191 disable_irq(data->irq); 1192 1193 reinit_completion(&data->reset_completion); 1194 1195 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false); 1196 if (ret) 1197 return ret; 1198 1199 /* Ignore CHG line for 100ms after reset */ 1200 msleep(100); 1201 1202 mxt_acquire_irq(data); 1203 1204 ret = mxt_wait_for_completion(data, &data->reset_completion, 1205 MXT_RESET_TIMEOUT); 1206 if (ret) 1207 return ret; 1208 1209 return 0; 1210 } 1211 1212 static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value) 1213 { 1214 /* 1215 * On failure, CRC is set to 0 and config will always be 1216 * downloaded. 1217 */ 1218 data->config_crc = 0; 1219 reinit_completion(&data->crc_completion); 1220 1221 mxt_t6_command(data, cmd, value, true); 1222 1223 /* 1224 * Wait for crc message. On failure, CRC is set to 0 and config will 1225 * always be downloaded. 1226 */ 1227 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT); 1228 } 1229 1230 static void mxt_calc_crc24(u32 *crc, u8 firstbyte, u8 secondbyte) 1231 { 1232 static const unsigned int crcpoly = 0x80001B; 1233 u32 result; 1234 u32 data_word; 1235 1236 data_word = (secondbyte << 8) | firstbyte; 1237 result = ((*crc << 1) ^ data_word); 1238 1239 if (result & 0x1000000) 1240 result ^= crcpoly; 1241 1242 *crc = result; 1243 } 1244 1245 static u32 mxt_calculate_crc(u8 *base, off_t start_off, off_t end_off) 1246 { 1247 u32 crc = 0; 1248 u8 *ptr = base + start_off; 1249 u8 *last_val = base + end_off - 1; 1250 1251 if (end_off < start_off) 1252 return -EINVAL; 1253 1254 while (ptr < last_val) { 1255 mxt_calc_crc24(&crc, *ptr, *(ptr + 1)); 1256 ptr += 2; 1257 } 1258 1259 /* if len is odd, fill the last byte with 0 */ 1260 if (ptr == last_val) 1261 mxt_calc_crc24(&crc, *ptr, 0); 1262 1263 /* Mask to 24-bit */ 1264 crc &= 0x00FFFFFF; 1265 1266 return crc; 1267 } 1268 1269 static int mxt_prepare_cfg_mem(struct mxt_data *data, 1270 const struct firmware *cfg, 1271 unsigned int data_pos, 1272 unsigned int cfg_start_ofs, 1273 u8 *config_mem, 1274 size_t config_mem_size) 1275 { 1276 struct device *dev = &data->client->dev; 1277 struct mxt_object *object; 1278 unsigned int type, instance, size, byte_offset; 1279 int offset; 1280 int ret; 1281 int i; 1282 u16 reg; 1283 u8 val; 1284 1285 while (data_pos < cfg->size) { 1286 /* Read type, instance, length */ 1287 ret = sscanf(cfg->data + data_pos, "%x %x %x%n", 1288 &type, &instance, &size, &offset); 1289 if (ret == 0) { 1290 /* EOF */ 1291 break; 1292 } else if (ret != 3) { 1293 dev_err(dev, "Bad format: failed to parse object\n"); 1294 return -EINVAL; 1295 } 1296 data_pos += offset; 1297 1298 object = mxt_get_object(data, type); 1299 if (!object) { 1300 /* Skip object */ 1301 for (i = 0; i < size; i++) { 1302 ret = sscanf(cfg->data + data_pos, "%hhx%n", 1303 &val, &offset); 1304 if (ret != 1) { 1305 dev_err(dev, "Bad format in T%d at %d\n", 1306 type, i); 1307 return -EINVAL; 1308 } 1309 data_pos += offset; 1310 } 1311 continue; 1312 } 1313 1314 if (size > mxt_obj_size(object)) { 1315 /* 1316 * Either we are in fallback mode due to wrong 1317 * config or config from a later fw version, 1318 * or the file is corrupt or hand-edited. 1319 */ 1320 dev_warn(dev, "Discarding %zu byte(s) in T%u\n", 1321 size - mxt_obj_size(object), type); 1322 } else if (mxt_obj_size(object) > size) { 1323 /* 1324 * If firmware is upgraded, new bytes may be added to 1325 * end of objects. It is generally forward compatible 1326 * to zero these bytes - previous behaviour will be 1327 * retained. However this does invalidate the CRC and 1328 * will force fallback mode until the configuration is 1329 * updated. We warn here but do nothing else - the 1330 * malloc has zeroed the entire configuration. 1331 */ 1332 dev_warn(dev, "Zeroing %zu byte(s) in T%d\n", 1333 mxt_obj_size(object) - size, type); 1334 } 1335 1336 if (instance >= mxt_obj_instances(object)) { 1337 dev_err(dev, "Object instances exceeded!\n"); 1338 return -EINVAL; 1339 } 1340 1341 reg = object->start_address + mxt_obj_size(object) * instance; 1342 1343 for (i = 0; i < size; i++) { 1344 ret = sscanf(cfg->data + data_pos, "%hhx%n", 1345 &val, 1346 &offset); 1347 if (ret != 1) { 1348 dev_err(dev, "Bad format in T%d at %d\n", 1349 type, i); 1350 return -EINVAL; 1351 } 1352 data_pos += offset; 1353 1354 if (i > mxt_obj_size(object)) 1355 continue; 1356 1357 byte_offset = reg + i - cfg_start_ofs; 1358 1359 if (byte_offset >= 0 && byte_offset < config_mem_size) { 1360 *(config_mem + byte_offset) = val; 1361 } else { 1362 dev_err(dev, "Bad object: reg:%d, T%d, ofs=%d\n", 1363 reg, object->type, byte_offset); 1364 return -EINVAL; 1365 } 1366 } 1367 } 1368 1369 return 0; 1370 } 1371 1372 static int mxt_upload_cfg_mem(struct mxt_data *data, unsigned int cfg_start, 1373 u8 *config_mem, size_t config_mem_size) 1374 { 1375 unsigned int byte_offset = 0; 1376 int error; 1377 1378 /* Write configuration as blocks */ 1379 while (byte_offset < config_mem_size) { 1380 unsigned int size = config_mem_size - byte_offset; 1381 1382 if (size > MXT_MAX_BLOCK_WRITE) 1383 size = MXT_MAX_BLOCK_WRITE; 1384 1385 error = __mxt_write_reg(data->client, 1386 cfg_start + byte_offset, 1387 size, config_mem + byte_offset); 1388 if (error) { 1389 dev_err(&data->client->dev, 1390 "Config write error, ret=%d\n", error); 1391 return error; 1392 } 1393 1394 byte_offset += size; 1395 } 1396 1397 return 0; 1398 } 1399 1400 static int mxt_init_t7_power_cfg(struct mxt_data *data); 1401 1402 /* 1403 * mxt_update_cfg - download configuration to chip 1404 * 1405 * Atmel Raw Config File Format 1406 * 1407 * The first four lines of the raw config file contain: 1408 * 1) Version 1409 * 2) Chip ID Information (first 7 bytes of device memory) 1410 * 3) Chip Information Block 24-bit CRC Checksum 1411 * 4) Chip Configuration 24-bit CRC Checksum 1412 * 1413 * The rest of the file consists of one line per object instance: 1414 * <TYPE> <INSTANCE> <SIZE> <CONTENTS> 1415 * 1416 * <TYPE> - 2-byte object type as hex 1417 * <INSTANCE> - 2-byte object instance number as hex 1418 * <SIZE> - 2-byte object size as hex 1419 * <CONTENTS> - array of <SIZE> 1-byte hex values 1420 */ 1421 static int mxt_update_cfg(struct mxt_data *data, const struct firmware *cfg) 1422 { 1423 struct device *dev = &data->client->dev; 1424 struct mxt_info cfg_info; 1425 int ret; 1426 int offset; 1427 int data_pos; 1428 int i; 1429 int cfg_start_ofs; 1430 u32 info_crc, config_crc, calculated_crc; 1431 u8 *config_mem; 1432 size_t config_mem_size; 1433 1434 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1); 1435 1436 if (strncmp(cfg->data, MXT_CFG_MAGIC, strlen(MXT_CFG_MAGIC))) { 1437 dev_err(dev, "Unrecognised config file\n"); 1438 return -EINVAL; 1439 } 1440 1441 data_pos = strlen(MXT_CFG_MAGIC); 1442 1443 /* Load information block and check */ 1444 for (i = 0; i < sizeof(struct mxt_info); i++) { 1445 ret = sscanf(cfg->data + data_pos, "%hhx%n", 1446 (unsigned char *)&cfg_info + i, 1447 &offset); 1448 if (ret != 1) { 1449 dev_err(dev, "Bad format\n"); 1450 return -EINVAL; 1451 } 1452 1453 data_pos += offset; 1454 } 1455 1456 if (cfg_info.family_id != data->info.family_id) { 1457 dev_err(dev, "Family ID mismatch!\n"); 1458 return -EINVAL; 1459 } 1460 1461 if (cfg_info.variant_id != data->info.variant_id) { 1462 dev_err(dev, "Variant ID mismatch!\n"); 1463 return -EINVAL; 1464 } 1465 1466 /* Read CRCs */ 1467 ret = sscanf(cfg->data + data_pos, "%x%n", &info_crc, &offset); 1468 if (ret != 1) { 1469 dev_err(dev, "Bad format: failed to parse Info CRC\n"); 1470 return -EINVAL; 1471 } 1472 data_pos += offset; 1473 1474 ret = sscanf(cfg->data + data_pos, "%x%n", &config_crc, &offset); 1475 if (ret != 1) { 1476 dev_err(dev, "Bad format: failed to parse Config CRC\n"); 1477 return -EINVAL; 1478 } 1479 data_pos += offset; 1480 1481 /* 1482 * The Info Block CRC is calculated over mxt_info and the object 1483 * table. If it does not match then we are trying to load the 1484 * configuration from a different chip or firmware version, so 1485 * the configuration CRC is invalid anyway. 1486 */ 1487 if (info_crc == data->info_crc) { 1488 if (config_crc == 0 || data->config_crc == 0) { 1489 dev_info(dev, "CRC zero, attempting to apply config\n"); 1490 } else if (config_crc == data->config_crc) { 1491 dev_dbg(dev, "Config CRC 0x%06X: OK\n", 1492 data->config_crc); 1493 return 0; 1494 } else { 1495 dev_info(dev, "Config CRC 0x%06X: does not match file 0x%06X\n", 1496 data->config_crc, config_crc); 1497 } 1498 } else { 1499 dev_warn(dev, 1500 "Warning: Info CRC error - device=0x%06X file=0x%06X\n", 1501 data->info_crc, info_crc); 1502 } 1503 1504 /* Malloc memory to store configuration */ 1505 cfg_start_ofs = MXT_OBJECT_START + 1506 data->info.object_num * sizeof(struct mxt_object) + 1507 MXT_INFO_CHECKSUM_SIZE; 1508 config_mem_size = data->mem_size - cfg_start_ofs; 1509 config_mem = kzalloc(config_mem_size, GFP_KERNEL); 1510 if (!config_mem) { 1511 dev_err(dev, "Failed to allocate memory\n"); 1512 return -ENOMEM; 1513 } 1514 1515 ret = mxt_prepare_cfg_mem(data, cfg, data_pos, cfg_start_ofs, 1516 config_mem, config_mem_size); 1517 if (ret) 1518 goto release_mem; 1519 1520 /* Calculate crc of the received configs (not the raw config file) */ 1521 if (data->T7_address < cfg_start_ofs) { 1522 dev_err(dev, "Bad T7 address, T7addr = %x, config offset %x\n", 1523 data->T7_address, cfg_start_ofs); 1524 ret = 0; 1525 goto release_mem; 1526 } 1527 1528 calculated_crc = mxt_calculate_crc(config_mem, 1529 data->T7_address - cfg_start_ofs, 1530 config_mem_size); 1531 1532 if (config_crc > 0 && config_crc != calculated_crc) 1533 dev_warn(dev, "Config CRC error, calculated=%06X, file=%06X\n", 1534 calculated_crc, config_crc); 1535 1536 ret = mxt_upload_cfg_mem(data, cfg_start_ofs, 1537 config_mem, config_mem_size); 1538 if (ret) 1539 goto release_mem; 1540 1541 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE); 1542 1543 ret = mxt_soft_reset(data); 1544 if (ret) 1545 goto release_mem; 1546 1547 dev_info(dev, "Config successfully updated\n"); 1548 1549 /* T7 config may have changed */ 1550 mxt_init_t7_power_cfg(data); 1551 1552 release_mem: 1553 kfree(config_mem); 1554 return ret; 1555 } 1556 1557 static int mxt_get_info(struct mxt_data *data) 1558 { 1559 struct i2c_client *client = data->client; 1560 struct mxt_info *info = &data->info; 1561 int error; 1562 1563 /* Read 7-byte info block starting at address 0 */ 1564 error = __mxt_read_reg(client, 0, sizeof(*info), info); 1565 if (error) 1566 return error; 1567 1568 return 0; 1569 } 1570 1571 static void mxt_free_input_device(struct mxt_data *data) 1572 { 1573 if (data->input_dev) { 1574 input_unregister_device(data->input_dev); 1575 data->input_dev = NULL; 1576 } 1577 } 1578 1579 static void mxt_free_object_table(struct mxt_data *data) 1580 { 1581 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37 1582 video_unregister_device(&data->dbg.vdev); 1583 v4l2_device_unregister(&data->dbg.v4l2); 1584 #endif 1585 1586 kfree(data->object_table); 1587 data->object_table = NULL; 1588 kfree(data->msg_buf); 1589 data->msg_buf = NULL; 1590 data->T5_address = 0; 1591 data->T5_msg_size = 0; 1592 data->T6_reportid = 0; 1593 data->T7_address = 0; 1594 data->T9_reportid_min = 0; 1595 data->T9_reportid_max = 0; 1596 data->T19_reportid = 0; 1597 data->T44_address = 0; 1598 data->T100_reportid_min = 0; 1599 data->T100_reportid_max = 0; 1600 data->max_reportid = 0; 1601 } 1602 1603 static int mxt_get_object_table(struct mxt_data *data) 1604 { 1605 struct i2c_client *client = data->client; 1606 size_t table_size; 1607 struct mxt_object *object_table; 1608 int error; 1609 int i; 1610 u8 reportid; 1611 u16 end_address; 1612 1613 table_size = data->info.object_num * sizeof(struct mxt_object); 1614 object_table = kzalloc(table_size, GFP_KERNEL); 1615 if (!object_table) { 1616 dev_err(&data->client->dev, "Failed to allocate memory\n"); 1617 return -ENOMEM; 1618 } 1619 1620 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size, 1621 object_table); 1622 if (error) { 1623 kfree(object_table); 1624 return error; 1625 } 1626 1627 /* Valid Report IDs start counting from 1 */ 1628 reportid = 1; 1629 data->mem_size = 0; 1630 for (i = 0; i < data->info.object_num; i++) { 1631 struct mxt_object *object = object_table + i; 1632 u8 min_id, max_id; 1633 1634 le16_to_cpus(&object->start_address); 1635 1636 if (object->num_report_ids) { 1637 min_id = reportid; 1638 reportid += object->num_report_ids * 1639 mxt_obj_instances(object); 1640 max_id = reportid - 1; 1641 } else { 1642 min_id = 0; 1643 max_id = 0; 1644 } 1645 1646 dev_dbg(&data->client->dev, 1647 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n", 1648 object->type, object->start_address, 1649 mxt_obj_size(object), mxt_obj_instances(object), 1650 min_id, max_id); 1651 1652 switch (object->type) { 1653 case MXT_GEN_MESSAGE_T5: 1654 if (data->info.family_id == 0x80 && 1655 data->info.version < 0x20) { 1656 /* 1657 * On mXT224 firmware versions prior to V2.0 1658 * read and discard unused CRC byte otherwise 1659 * DMA reads are misaligned. 1660 */ 1661 data->T5_msg_size = mxt_obj_size(object); 1662 } else { 1663 /* CRC not enabled, so skip last byte */ 1664 data->T5_msg_size = mxt_obj_size(object) - 1; 1665 } 1666 data->T5_address = object->start_address; 1667 break; 1668 case MXT_GEN_COMMAND_T6: 1669 data->T6_reportid = min_id; 1670 data->T6_address = object->start_address; 1671 break; 1672 case MXT_GEN_POWER_T7: 1673 data->T7_address = object->start_address; 1674 break; 1675 case MXT_TOUCH_MULTI_T9: 1676 data->multitouch = MXT_TOUCH_MULTI_T9; 1677 data->T9_reportid_min = min_id; 1678 data->T9_reportid_max = max_id; 1679 data->num_touchids = object->num_report_ids 1680 * mxt_obj_instances(object); 1681 break; 1682 case MXT_SPT_MESSAGECOUNT_T44: 1683 data->T44_address = object->start_address; 1684 break; 1685 case MXT_SPT_GPIOPWM_T19: 1686 data->T19_reportid = min_id; 1687 break; 1688 case MXT_TOUCH_MULTITOUCHSCREEN_T100: 1689 data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100; 1690 data->T100_reportid_min = min_id; 1691 data->T100_reportid_max = max_id; 1692 /* first two report IDs reserved */ 1693 data->num_touchids = object->num_report_ids - 2; 1694 break; 1695 } 1696 1697 end_address = object->start_address 1698 + mxt_obj_size(object) * mxt_obj_instances(object) - 1; 1699 1700 if (end_address >= data->mem_size) 1701 data->mem_size = end_address + 1; 1702 } 1703 1704 /* Store maximum reportid */ 1705 data->max_reportid = reportid; 1706 1707 /* If T44 exists, T5 position has to be directly after */ 1708 if (data->T44_address && (data->T5_address != data->T44_address + 1)) { 1709 dev_err(&client->dev, "Invalid T44 position\n"); 1710 error = -EINVAL; 1711 goto free_object_table; 1712 } 1713 1714 data->msg_buf = kcalloc(data->max_reportid, 1715 data->T5_msg_size, GFP_KERNEL); 1716 if (!data->msg_buf) { 1717 dev_err(&client->dev, "Failed to allocate message buffer\n"); 1718 error = -ENOMEM; 1719 goto free_object_table; 1720 } 1721 1722 data->object_table = object_table; 1723 1724 return 0; 1725 1726 free_object_table: 1727 mxt_free_object_table(data); 1728 return error; 1729 } 1730 1731 static int mxt_read_t9_resolution(struct mxt_data *data) 1732 { 1733 struct i2c_client *client = data->client; 1734 int error; 1735 struct t9_range range; 1736 unsigned char orient; 1737 struct mxt_object *object; 1738 1739 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9); 1740 if (!object) 1741 return -EINVAL; 1742 1743 error = __mxt_read_reg(client, 1744 object->start_address + MXT_T9_XSIZE, 1745 sizeof(data->xsize), &data->xsize); 1746 if (error) 1747 return error; 1748 1749 error = __mxt_read_reg(client, 1750 object->start_address + MXT_T9_YSIZE, 1751 sizeof(data->ysize), &data->ysize); 1752 if (error) 1753 return error; 1754 1755 error = __mxt_read_reg(client, 1756 object->start_address + MXT_T9_RANGE, 1757 sizeof(range), &range); 1758 if (error) 1759 return error; 1760 1761 data->max_x = get_unaligned_le16(&range.x); 1762 data->max_y = get_unaligned_le16(&range.y); 1763 1764 error = __mxt_read_reg(client, 1765 object->start_address + MXT_T9_ORIENT, 1766 1, &orient); 1767 if (error) 1768 return error; 1769 1770 data->xy_switch = orient & MXT_T9_ORIENT_SWITCH; 1771 data->invertx = orient & MXT_T9_ORIENT_INVERTX; 1772 data->inverty = orient & MXT_T9_ORIENT_INVERTY; 1773 1774 return 0; 1775 } 1776 1777 static int mxt_read_t100_config(struct mxt_data *data) 1778 { 1779 struct i2c_client *client = data->client; 1780 int error; 1781 struct mxt_object *object; 1782 u16 range_x, range_y; 1783 u8 cfg, tchaux; 1784 u8 aux; 1785 1786 object = mxt_get_object(data, MXT_TOUCH_MULTITOUCHSCREEN_T100); 1787 if (!object) 1788 return -EINVAL; 1789 1790 /* read touchscreen dimensions */ 1791 error = __mxt_read_reg(client, 1792 object->start_address + MXT_T100_XRANGE, 1793 sizeof(range_x), &range_x); 1794 if (error) 1795 return error; 1796 1797 data->max_x = get_unaligned_le16(&range_x); 1798 1799 error = __mxt_read_reg(client, 1800 object->start_address + MXT_T100_YRANGE, 1801 sizeof(range_y), &range_y); 1802 if (error) 1803 return error; 1804 1805 data->max_y = get_unaligned_le16(&range_y); 1806 1807 error = __mxt_read_reg(client, 1808 object->start_address + MXT_T100_XSIZE, 1809 sizeof(data->xsize), &data->xsize); 1810 if (error) 1811 return error; 1812 1813 error = __mxt_read_reg(client, 1814 object->start_address + MXT_T100_YSIZE, 1815 sizeof(data->ysize), &data->ysize); 1816 if (error) 1817 return error; 1818 1819 /* read orientation config */ 1820 error = __mxt_read_reg(client, 1821 object->start_address + MXT_T100_CFG1, 1822 1, &cfg); 1823 if (error) 1824 return error; 1825 1826 data->xy_switch = cfg & MXT_T100_CFG_SWITCHXY; 1827 data->invertx = cfg & MXT_T100_CFG_INVERTX; 1828 data->inverty = cfg & MXT_T100_CFG_INVERTY; 1829 1830 /* allocate aux bytes */ 1831 error = __mxt_read_reg(client, 1832 object->start_address + MXT_T100_TCHAUX, 1833 1, &tchaux); 1834 if (error) 1835 return error; 1836 1837 aux = 6; 1838 1839 if (tchaux & MXT_T100_TCHAUX_VECT) 1840 data->t100_aux_vect = aux++; 1841 1842 if (tchaux & MXT_T100_TCHAUX_AMPL) 1843 data->t100_aux_ampl = aux++; 1844 1845 if (tchaux & MXT_T100_TCHAUX_AREA) 1846 data->t100_aux_area = aux++; 1847 1848 dev_dbg(&client->dev, 1849 "T100 aux mappings vect:%u ampl:%u area:%u\n", 1850 data->t100_aux_vect, data->t100_aux_ampl, data->t100_aux_area); 1851 1852 return 0; 1853 } 1854 1855 static int mxt_input_open(struct input_dev *dev); 1856 static void mxt_input_close(struct input_dev *dev); 1857 1858 static void mxt_set_up_as_touchpad(struct input_dev *input_dev, 1859 struct mxt_data *data) 1860 { 1861 const struct mxt_platform_data *pdata = data->pdata; 1862 int i; 1863 1864 input_dev->name = "Atmel maXTouch Touchpad"; 1865 1866 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit); 1867 1868 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM); 1869 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM); 1870 input_abs_set_res(input_dev, ABS_MT_POSITION_X, 1871 MXT_PIXELS_PER_MM); 1872 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, 1873 MXT_PIXELS_PER_MM); 1874 1875 for (i = 0; i < pdata->t19_num_keys; i++) 1876 if (pdata->t19_keymap[i] != KEY_RESERVED) 1877 input_set_capability(input_dev, EV_KEY, 1878 pdata->t19_keymap[i]); 1879 } 1880 1881 static int mxt_initialize_input_device(struct mxt_data *data) 1882 { 1883 const struct mxt_platform_data *pdata = data->pdata; 1884 struct device *dev = &data->client->dev; 1885 struct input_dev *input_dev; 1886 int error; 1887 unsigned int num_mt_slots; 1888 unsigned int mt_flags = 0; 1889 1890 switch (data->multitouch) { 1891 case MXT_TOUCH_MULTI_T9: 1892 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1; 1893 error = mxt_read_t9_resolution(data); 1894 if (error) 1895 dev_warn(dev, "Failed to initialize T9 resolution\n"); 1896 break; 1897 1898 case MXT_TOUCH_MULTITOUCHSCREEN_T100: 1899 num_mt_slots = data->num_touchids; 1900 error = mxt_read_t100_config(data); 1901 if (error) 1902 dev_warn(dev, "Failed to read T100 config\n"); 1903 break; 1904 1905 default: 1906 dev_err(dev, "Invalid multitouch object\n"); 1907 return -EINVAL; 1908 } 1909 1910 /* Handle default values and orientation switch */ 1911 if (data->max_x == 0) 1912 data->max_x = 1023; 1913 1914 if (data->max_y == 0) 1915 data->max_y = 1023; 1916 1917 if (data->xy_switch) 1918 swap(data->max_x, data->max_y); 1919 1920 dev_info(dev, "Touchscreen size X%uY%u\n", data->max_x, data->max_y); 1921 1922 /* Register input device */ 1923 input_dev = input_allocate_device(); 1924 if (!input_dev) { 1925 dev_err(dev, "Failed to allocate memory\n"); 1926 return -ENOMEM; 1927 } 1928 1929 input_dev->name = "Atmel maXTouch Touchscreen"; 1930 input_dev->phys = data->phys; 1931 input_dev->id.bustype = BUS_I2C; 1932 input_dev->dev.parent = dev; 1933 input_dev->open = mxt_input_open; 1934 input_dev->close = mxt_input_close; 1935 1936 input_set_capability(input_dev, EV_KEY, BTN_TOUCH); 1937 1938 /* For single touch */ 1939 input_set_abs_params(input_dev, ABS_X, 0, data->max_x, 0, 0); 1940 input_set_abs_params(input_dev, ABS_Y, 0, data->max_y, 0, 0); 1941 1942 if (data->multitouch == MXT_TOUCH_MULTI_T9 || 1943 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && 1944 data->t100_aux_ampl)) { 1945 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0); 1946 } 1947 1948 /* If device has buttons we assume it is a touchpad */ 1949 if (pdata->t19_num_keys) { 1950 mxt_set_up_as_touchpad(input_dev, data); 1951 mt_flags |= INPUT_MT_POINTER; 1952 } else { 1953 mt_flags |= INPUT_MT_DIRECT; 1954 } 1955 1956 /* For multi touch */ 1957 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags); 1958 if (error) { 1959 dev_err(dev, "Error %d initialising slots\n", error); 1960 goto err_free_mem; 1961 } 1962 1963 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100) { 1964 input_set_abs_params(input_dev, ABS_MT_TOOL_TYPE, 1965 0, MT_TOOL_MAX, 0, 0); 1966 input_set_abs_params(input_dev, ABS_MT_DISTANCE, 1967 MXT_DISTANCE_ACTIVE_TOUCH, 1968 MXT_DISTANCE_HOVERING, 1969 0, 0); 1970 } 1971 1972 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 1973 0, data->max_x, 0, 0); 1974 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 1975 0, data->max_y, 0, 0); 1976 1977 if (data->multitouch == MXT_TOUCH_MULTI_T9 || 1978 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && 1979 data->t100_aux_area)) { 1980 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 1981 0, MXT_MAX_AREA, 0, 0); 1982 } 1983 1984 if (data->multitouch == MXT_TOUCH_MULTI_T9 || 1985 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && 1986 data->t100_aux_ampl)) { 1987 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 1988 0, 255, 0, 0); 1989 } 1990 1991 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && 1992 data->t100_aux_vect) { 1993 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 1994 0, 255, 0, 0); 1995 } 1996 1997 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && 1998 data->t100_aux_ampl) { 1999 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 2000 0, 255, 0, 0); 2001 } 2002 2003 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 && 2004 data->t100_aux_vect) { 2005 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 2006 0, 255, 0, 0); 2007 } 2008 2009 input_set_drvdata(input_dev, data); 2010 2011 error = input_register_device(input_dev); 2012 if (error) { 2013 dev_err(dev, "Error %d registering input device\n", error); 2014 goto err_free_mem; 2015 } 2016 2017 data->input_dev = input_dev; 2018 2019 return 0; 2020 2021 err_free_mem: 2022 input_free_device(input_dev); 2023 return error; 2024 } 2025 2026 static int mxt_configure_objects(struct mxt_data *data, 2027 const struct firmware *cfg); 2028 2029 static void mxt_config_cb(const struct firmware *cfg, void *ctx) 2030 { 2031 mxt_configure_objects(ctx, cfg); 2032 release_firmware(cfg); 2033 } 2034 2035 static int mxt_initialize(struct mxt_data *data) 2036 { 2037 struct i2c_client *client = data->client; 2038 int recovery_attempts = 0; 2039 int error; 2040 2041 while (1) { 2042 error = mxt_get_info(data); 2043 if (!error) 2044 break; 2045 2046 /* Check bootloader state */ 2047 error = mxt_probe_bootloader(data, false); 2048 if (error) { 2049 dev_info(&client->dev, "Trying alternate bootloader address\n"); 2050 error = mxt_probe_bootloader(data, true); 2051 if (error) { 2052 /* Chip is not in appmode or bootloader mode */ 2053 return error; 2054 } 2055 } 2056 2057 /* OK, we are in bootloader, see if we can recover */ 2058 if (++recovery_attempts > 1) { 2059 dev_err(&client->dev, "Could not recover from bootloader mode\n"); 2060 /* 2061 * We can reflash from this state, so do not 2062 * abort initialization. 2063 */ 2064 data->in_bootloader = true; 2065 return 0; 2066 } 2067 2068 /* Attempt to exit bootloader into app mode */ 2069 mxt_send_bootloader_cmd(data, false); 2070 msleep(MXT_FW_RESET_TIME); 2071 } 2072 2073 /* Get object table information */ 2074 error = mxt_get_object_table(data); 2075 if (error) { 2076 dev_err(&client->dev, "Error %d reading object table\n", error); 2077 return error; 2078 } 2079 2080 error = mxt_acquire_irq(data); 2081 if (error) 2082 goto err_free_object_table; 2083 2084 error = request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME, 2085 &client->dev, GFP_KERNEL, data, 2086 mxt_config_cb); 2087 if (error) { 2088 dev_err(&client->dev, "Failed to invoke firmware loader: %d\n", 2089 error); 2090 goto err_free_object_table; 2091 } 2092 2093 return 0; 2094 2095 err_free_object_table: 2096 mxt_free_object_table(data); 2097 return error; 2098 } 2099 2100 static int mxt_set_t7_power_cfg(struct mxt_data *data, u8 sleep) 2101 { 2102 struct device *dev = &data->client->dev; 2103 int error; 2104 struct t7_config *new_config; 2105 struct t7_config deepsleep = { .active = 0, .idle = 0 }; 2106 2107 if (sleep == MXT_POWER_CFG_DEEPSLEEP) 2108 new_config = &deepsleep; 2109 else 2110 new_config = &data->t7_cfg; 2111 2112 error = __mxt_write_reg(data->client, data->T7_address, 2113 sizeof(data->t7_cfg), new_config); 2114 if (error) 2115 return error; 2116 2117 dev_dbg(dev, "Set T7 ACTV:%d IDLE:%d\n", 2118 new_config->active, new_config->idle); 2119 2120 return 0; 2121 } 2122 2123 static int mxt_init_t7_power_cfg(struct mxt_data *data) 2124 { 2125 struct device *dev = &data->client->dev; 2126 int error; 2127 bool retry = false; 2128 2129 recheck: 2130 error = __mxt_read_reg(data->client, data->T7_address, 2131 sizeof(data->t7_cfg), &data->t7_cfg); 2132 if (error) 2133 return error; 2134 2135 if (data->t7_cfg.active == 0 || data->t7_cfg.idle == 0) { 2136 if (!retry) { 2137 dev_dbg(dev, "T7 cfg zero, resetting\n"); 2138 mxt_soft_reset(data); 2139 retry = true; 2140 goto recheck; 2141 } else { 2142 dev_dbg(dev, "T7 cfg zero after reset, overriding\n"); 2143 data->t7_cfg.active = 20; 2144 data->t7_cfg.idle = 100; 2145 return mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN); 2146 } 2147 } 2148 2149 dev_dbg(dev, "Initialized power cfg: ACTV %d, IDLE %d\n", 2150 data->t7_cfg.active, data->t7_cfg.idle); 2151 return 0; 2152 } 2153 2154 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37 2155 static u16 mxt_get_debug_value(struct mxt_data *data, unsigned int x, 2156 unsigned int y) 2157 { 2158 struct mxt_info *info = &data->info; 2159 struct mxt_dbg *dbg = &data->dbg; 2160 unsigned int ofs, page; 2161 unsigned int col = 0; 2162 unsigned int col_width; 2163 2164 if (info->family_id == MXT_FAMILY_1386) { 2165 col_width = info->matrix_ysize / MXT1386_COLUMNS; 2166 col = y / col_width; 2167 y = y % col_width; 2168 } else { 2169 col_width = info->matrix_ysize; 2170 } 2171 2172 ofs = (y + (x * col_width)) * sizeof(u16); 2173 page = ofs / MXT_DIAGNOSTIC_SIZE; 2174 ofs %= MXT_DIAGNOSTIC_SIZE; 2175 2176 if (info->family_id == MXT_FAMILY_1386) 2177 page += col * MXT1386_PAGES_PER_COLUMN; 2178 2179 return get_unaligned_le16(&dbg->t37_buf[page].data[ofs]); 2180 } 2181 2182 static int mxt_convert_debug_pages(struct mxt_data *data, u16 *outbuf) 2183 { 2184 struct mxt_dbg *dbg = &data->dbg; 2185 unsigned int x = 0; 2186 unsigned int y = 0; 2187 unsigned int i, rx, ry; 2188 2189 for (i = 0; i < dbg->t37_nodes; i++) { 2190 /* Handle orientation */ 2191 rx = data->xy_switch ? y : x; 2192 ry = data->xy_switch ? x : y; 2193 rx = data->invertx ? (data->xsize - 1 - rx) : rx; 2194 ry = data->inverty ? (data->ysize - 1 - ry) : ry; 2195 2196 outbuf[i] = mxt_get_debug_value(data, rx, ry); 2197 2198 /* Next value */ 2199 if (++x >= (data->xy_switch ? data->ysize : data->xsize)) { 2200 x = 0; 2201 y++; 2202 } 2203 } 2204 2205 return 0; 2206 } 2207 2208 static int mxt_read_diagnostic_debug(struct mxt_data *data, u8 mode, 2209 u16 *outbuf) 2210 { 2211 struct mxt_dbg *dbg = &data->dbg; 2212 int retries = 0; 2213 int page; 2214 int ret; 2215 u8 cmd = mode; 2216 struct t37_debug *p; 2217 u8 cmd_poll; 2218 2219 for (page = 0; page < dbg->t37_pages; page++) { 2220 p = dbg->t37_buf + page; 2221 2222 ret = mxt_write_reg(data->client, dbg->diag_cmd_address, 2223 cmd); 2224 if (ret) 2225 return ret; 2226 2227 retries = 0; 2228 msleep(20); 2229 wait_cmd: 2230 /* Read back command byte */ 2231 ret = __mxt_read_reg(data->client, dbg->diag_cmd_address, 2232 sizeof(cmd_poll), &cmd_poll); 2233 if (ret) 2234 return ret; 2235 2236 /* Field is cleared once the command has been processed */ 2237 if (cmd_poll) { 2238 if (retries++ > 100) 2239 return -EINVAL; 2240 2241 msleep(20); 2242 goto wait_cmd; 2243 } 2244 2245 /* Read T37 page */ 2246 ret = __mxt_read_reg(data->client, dbg->t37_address, 2247 sizeof(struct t37_debug), p); 2248 if (ret) 2249 return ret; 2250 2251 if (p->mode != mode || p->page != page) { 2252 dev_err(&data->client->dev, "T37 page mismatch\n"); 2253 return -EINVAL; 2254 } 2255 2256 dev_dbg(&data->client->dev, "%s page:%d retries:%d\n", 2257 __func__, page, retries); 2258 2259 /* For remaining pages, write PAGEUP rather than mode */ 2260 cmd = MXT_DIAGNOSTIC_PAGEUP; 2261 } 2262 2263 return mxt_convert_debug_pages(data, outbuf); 2264 } 2265 2266 static int mxt_queue_setup(struct vb2_queue *q, 2267 unsigned int *nbuffers, unsigned int *nplanes, 2268 unsigned int sizes[], struct device *alloc_devs[]) 2269 { 2270 struct mxt_data *data = q->drv_priv; 2271 size_t size = data->dbg.t37_nodes * sizeof(u16); 2272 2273 if (*nplanes) 2274 return sizes[0] < size ? -EINVAL : 0; 2275 2276 *nplanes = 1; 2277 sizes[0] = size; 2278 2279 return 0; 2280 } 2281 2282 static void mxt_buffer_queue(struct vb2_buffer *vb) 2283 { 2284 struct mxt_data *data = vb2_get_drv_priv(vb->vb2_queue); 2285 u16 *ptr; 2286 int ret; 2287 u8 mode; 2288 2289 ptr = vb2_plane_vaddr(vb, 0); 2290 if (!ptr) { 2291 dev_err(&data->client->dev, "Error acquiring frame ptr\n"); 2292 goto fault; 2293 } 2294 2295 switch (data->dbg.input) { 2296 case MXT_V4L_INPUT_DELTAS: 2297 default: 2298 mode = MXT_DIAGNOSTIC_DELTAS; 2299 break; 2300 2301 case MXT_V4L_INPUT_REFS: 2302 mode = MXT_DIAGNOSTIC_REFS; 2303 break; 2304 } 2305 2306 ret = mxt_read_diagnostic_debug(data, mode, ptr); 2307 if (ret) 2308 goto fault; 2309 2310 vb2_set_plane_payload(vb, 0, data->dbg.t37_nodes * sizeof(u16)); 2311 vb2_buffer_done(vb, VB2_BUF_STATE_DONE); 2312 return; 2313 2314 fault: 2315 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); 2316 } 2317 2318 /* V4L2 structures */ 2319 static const struct vb2_ops mxt_queue_ops = { 2320 .queue_setup = mxt_queue_setup, 2321 .buf_queue = mxt_buffer_queue, 2322 .wait_prepare = vb2_ops_wait_prepare, 2323 .wait_finish = vb2_ops_wait_finish, 2324 }; 2325 2326 static const struct vb2_queue mxt_queue = { 2327 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, 2328 .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ, 2329 .buf_struct_size = sizeof(struct mxt_vb2_buffer), 2330 .ops = &mxt_queue_ops, 2331 .mem_ops = &vb2_vmalloc_memops, 2332 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC, 2333 .min_buffers_needed = 1, 2334 }; 2335 2336 static int mxt_vidioc_querycap(struct file *file, void *priv, 2337 struct v4l2_capability *cap) 2338 { 2339 struct mxt_data *data = video_drvdata(file); 2340 2341 strlcpy(cap->driver, "atmel_mxt_ts", sizeof(cap->driver)); 2342 strlcpy(cap->card, "atmel_mxt_ts touch", sizeof(cap->card)); 2343 snprintf(cap->bus_info, sizeof(cap->bus_info), 2344 "I2C:%s", dev_name(&data->client->dev)); 2345 return 0; 2346 } 2347 2348 static int mxt_vidioc_enum_input(struct file *file, void *priv, 2349 struct v4l2_input *i) 2350 { 2351 if (i->index >= MXT_V4L_INPUT_MAX) 2352 return -EINVAL; 2353 2354 i->type = V4L2_INPUT_TYPE_TOUCH; 2355 2356 switch (i->index) { 2357 case MXT_V4L_INPUT_REFS: 2358 strlcpy(i->name, "Mutual Capacitance References", 2359 sizeof(i->name)); 2360 break; 2361 case MXT_V4L_INPUT_DELTAS: 2362 strlcpy(i->name, "Mutual Capacitance Deltas", sizeof(i->name)); 2363 break; 2364 } 2365 2366 return 0; 2367 } 2368 2369 static int mxt_set_input(struct mxt_data *data, unsigned int i) 2370 { 2371 struct v4l2_pix_format *f = &data->dbg.format; 2372 2373 if (i >= MXT_V4L_INPUT_MAX) 2374 return -EINVAL; 2375 2376 if (i == MXT_V4L_INPUT_DELTAS) 2377 f->pixelformat = V4L2_TCH_FMT_DELTA_TD16; 2378 else 2379 f->pixelformat = V4L2_TCH_FMT_TU16; 2380 2381 f->width = data->xy_switch ? data->ysize : data->xsize; 2382 f->height = data->xy_switch ? data->xsize : data->ysize; 2383 f->field = V4L2_FIELD_NONE; 2384 f->colorspace = V4L2_COLORSPACE_RAW; 2385 f->bytesperline = f->width * sizeof(u16); 2386 f->sizeimage = f->width * f->height * sizeof(u16); 2387 2388 data->dbg.input = i; 2389 2390 return 0; 2391 } 2392 2393 static int mxt_vidioc_s_input(struct file *file, void *priv, unsigned int i) 2394 { 2395 return mxt_set_input(video_drvdata(file), i); 2396 } 2397 2398 static int mxt_vidioc_g_input(struct file *file, void *priv, unsigned int *i) 2399 { 2400 struct mxt_data *data = video_drvdata(file); 2401 2402 *i = data->dbg.input; 2403 2404 return 0; 2405 } 2406 2407 static int mxt_vidioc_fmt(struct file *file, void *priv, struct v4l2_format *f) 2408 { 2409 struct mxt_data *data = video_drvdata(file); 2410 2411 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 2412 f->fmt.pix = data->dbg.format; 2413 2414 return 0; 2415 } 2416 2417 static int mxt_vidioc_enum_fmt(struct file *file, void *priv, 2418 struct v4l2_fmtdesc *fmt) 2419 { 2420 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 2421 return -EINVAL; 2422 2423 switch (fmt->index) { 2424 case 0: 2425 fmt->pixelformat = V4L2_TCH_FMT_TU16; 2426 break; 2427 2428 case 1: 2429 fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD16; 2430 break; 2431 2432 default: 2433 return -EINVAL; 2434 } 2435 2436 return 0; 2437 } 2438 2439 static int mxt_vidioc_g_parm(struct file *file, void *fh, 2440 struct v4l2_streamparm *a) 2441 { 2442 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 2443 return -EINVAL; 2444 2445 a->parm.capture.readbuffers = 1; 2446 a->parm.capture.timeperframe.numerator = 1; 2447 a->parm.capture.timeperframe.denominator = 10; 2448 return 0; 2449 } 2450 2451 static const struct v4l2_ioctl_ops mxt_video_ioctl_ops = { 2452 .vidioc_querycap = mxt_vidioc_querycap, 2453 2454 .vidioc_enum_fmt_vid_cap = mxt_vidioc_enum_fmt, 2455 .vidioc_s_fmt_vid_cap = mxt_vidioc_fmt, 2456 .vidioc_g_fmt_vid_cap = mxt_vidioc_fmt, 2457 .vidioc_try_fmt_vid_cap = mxt_vidioc_fmt, 2458 .vidioc_g_parm = mxt_vidioc_g_parm, 2459 2460 .vidioc_enum_input = mxt_vidioc_enum_input, 2461 .vidioc_g_input = mxt_vidioc_g_input, 2462 .vidioc_s_input = mxt_vidioc_s_input, 2463 2464 .vidioc_reqbufs = vb2_ioctl_reqbufs, 2465 .vidioc_create_bufs = vb2_ioctl_create_bufs, 2466 .vidioc_querybuf = vb2_ioctl_querybuf, 2467 .vidioc_qbuf = vb2_ioctl_qbuf, 2468 .vidioc_dqbuf = vb2_ioctl_dqbuf, 2469 .vidioc_expbuf = vb2_ioctl_expbuf, 2470 2471 .vidioc_streamon = vb2_ioctl_streamon, 2472 .vidioc_streamoff = vb2_ioctl_streamoff, 2473 }; 2474 2475 static const struct video_device mxt_video_device = { 2476 .name = "Atmel maxTouch", 2477 .fops = &mxt_video_fops, 2478 .ioctl_ops = &mxt_video_ioctl_ops, 2479 .release = video_device_release_empty, 2480 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH | 2481 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING, 2482 }; 2483 2484 static void mxt_debug_init(struct mxt_data *data) 2485 { 2486 struct mxt_info *info = &data->info; 2487 struct mxt_dbg *dbg = &data->dbg; 2488 struct mxt_object *object; 2489 int error; 2490 2491 object = mxt_get_object(data, MXT_GEN_COMMAND_T6); 2492 if (!object) 2493 goto error; 2494 2495 dbg->diag_cmd_address = object->start_address + MXT_COMMAND_DIAGNOSTIC; 2496 2497 object = mxt_get_object(data, MXT_DEBUG_DIAGNOSTIC_T37); 2498 if (!object) 2499 goto error; 2500 2501 if (mxt_obj_size(object) != sizeof(struct t37_debug)) { 2502 dev_warn(&data->client->dev, "Bad T37 size"); 2503 goto error; 2504 } 2505 2506 dbg->t37_address = object->start_address; 2507 2508 /* Calculate size of data and allocate buffer */ 2509 dbg->t37_nodes = data->xsize * data->ysize; 2510 2511 if (info->family_id == MXT_FAMILY_1386) 2512 dbg->t37_pages = MXT1386_COLUMNS * MXT1386_PAGES_PER_COLUMN; 2513 else 2514 dbg->t37_pages = DIV_ROUND_UP(data->xsize * 2515 info->matrix_ysize * 2516 sizeof(u16), 2517 sizeof(dbg->t37_buf->data)); 2518 2519 dbg->t37_buf = devm_kmalloc_array(&data->client->dev, dbg->t37_pages, 2520 sizeof(struct t37_debug), GFP_KERNEL); 2521 if (!dbg->t37_buf) 2522 goto error; 2523 2524 /* init channel to zero */ 2525 mxt_set_input(data, 0); 2526 2527 /* register video device */ 2528 snprintf(dbg->v4l2.name, sizeof(dbg->v4l2.name), "%s", "atmel_mxt_ts"); 2529 error = v4l2_device_register(&data->client->dev, &dbg->v4l2); 2530 if (error) 2531 goto error; 2532 2533 /* initialize the queue */ 2534 mutex_init(&dbg->lock); 2535 dbg->queue = mxt_queue; 2536 dbg->queue.drv_priv = data; 2537 dbg->queue.lock = &dbg->lock; 2538 dbg->queue.dev = &data->client->dev; 2539 2540 error = vb2_queue_init(&dbg->queue); 2541 if (error) 2542 goto error_unreg_v4l2; 2543 2544 dbg->vdev = mxt_video_device; 2545 dbg->vdev.v4l2_dev = &dbg->v4l2; 2546 dbg->vdev.lock = &dbg->lock; 2547 dbg->vdev.vfl_dir = VFL_DIR_RX; 2548 dbg->vdev.queue = &dbg->queue; 2549 video_set_drvdata(&dbg->vdev, data); 2550 2551 error = video_register_device(&dbg->vdev, VFL_TYPE_TOUCH, -1); 2552 if (error) 2553 goto error_unreg_v4l2; 2554 2555 return; 2556 2557 error_unreg_v4l2: 2558 v4l2_device_unregister(&dbg->v4l2); 2559 error: 2560 dev_warn(&data->client->dev, "Error initializing T37\n"); 2561 } 2562 #else 2563 static void mxt_debug_init(struct mxt_data *data) 2564 { 2565 } 2566 #endif 2567 2568 static int mxt_configure_objects(struct mxt_data *data, 2569 const struct firmware *cfg) 2570 { 2571 struct device *dev = &data->client->dev; 2572 struct mxt_info *info = &data->info; 2573 int error; 2574 2575 error = mxt_init_t7_power_cfg(data); 2576 if (error) { 2577 dev_err(dev, "Failed to initialize power cfg\n"); 2578 return error; 2579 } 2580 2581 if (cfg) { 2582 error = mxt_update_cfg(data, cfg); 2583 if (error) 2584 dev_warn(dev, "Error %d updating config\n", error); 2585 } 2586 2587 if (data->multitouch) { 2588 error = mxt_initialize_input_device(data); 2589 if (error) 2590 return error; 2591 } else { 2592 dev_warn(dev, "No touch object detected\n"); 2593 } 2594 2595 mxt_debug_init(data); 2596 2597 dev_info(dev, 2598 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n", 2599 info->family_id, info->variant_id, info->version >> 4, 2600 info->version & 0xf, info->build, info->object_num); 2601 2602 return 0; 2603 } 2604 2605 /* Firmware Version is returned as Major.Minor.Build */ 2606 static ssize_t mxt_fw_version_show(struct device *dev, 2607 struct device_attribute *attr, char *buf) 2608 { 2609 struct mxt_data *data = dev_get_drvdata(dev); 2610 struct mxt_info *info = &data->info; 2611 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n", 2612 info->version >> 4, info->version & 0xf, info->build); 2613 } 2614 2615 /* Hardware Version is returned as FamilyID.VariantID */ 2616 static ssize_t mxt_hw_version_show(struct device *dev, 2617 struct device_attribute *attr, char *buf) 2618 { 2619 struct mxt_data *data = dev_get_drvdata(dev); 2620 struct mxt_info *info = &data->info; 2621 return scnprintf(buf, PAGE_SIZE, "%u.%u\n", 2622 info->family_id, info->variant_id); 2623 } 2624 2625 static ssize_t mxt_show_instance(char *buf, int count, 2626 struct mxt_object *object, int instance, 2627 const u8 *val) 2628 { 2629 int i; 2630 2631 if (mxt_obj_instances(object) > 1) 2632 count += scnprintf(buf + count, PAGE_SIZE - count, 2633 "Instance %u\n", instance); 2634 2635 for (i = 0; i < mxt_obj_size(object); i++) 2636 count += scnprintf(buf + count, PAGE_SIZE - count, 2637 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]); 2638 count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); 2639 2640 return count; 2641 } 2642 2643 static ssize_t mxt_object_show(struct device *dev, 2644 struct device_attribute *attr, char *buf) 2645 { 2646 struct mxt_data *data = dev_get_drvdata(dev); 2647 struct mxt_object *object; 2648 int count = 0; 2649 int i, j; 2650 int error; 2651 u8 *obuf; 2652 2653 /* Pre-allocate buffer large enough to hold max sized object. */ 2654 obuf = kmalloc(256, GFP_KERNEL); 2655 if (!obuf) 2656 return -ENOMEM; 2657 2658 error = 0; 2659 for (i = 0; i < data->info.object_num; i++) { 2660 object = data->object_table + i; 2661 2662 if (!mxt_object_readable(object->type)) 2663 continue; 2664 2665 count += scnprintf(buf + count, PAGE_SIZE - count, 2666 "T%u:\n", object->type); 2667 2668 for (j = 0; j < mxt_obj_instances(object); j++) { 2669 u16 size = mxt_obj_size(object); 2670 u16 addr = object->start_address + j * size; 2671 2672 error = __mxt_read_reg(data->client, addr, size, obuf); 2673 if (error) 2674 goto done; 2675 2676 count = mxt_show_instance(buf, count, object, j, obuf); 2677 } 2678 } 2679 2680 done: 2681 kfree(obuf); 2682 return error ?: count; 2683 } 2684 2685 static int mxt_check_firmware_format(struct device *dev, 2686 const struct firmware *fw) 2687 { 2688 unsigned int pos = 0; 2689 char c; 2690 2691 while (pos < fw->size) { 2692 c = *(fw->data + pos); 2693 2694 if (c < '0' || (c > '9' && c < 'A') || c > 'F') 2695 return 0; 2696 2697 pos++; 2698 } 2699 2700 /* 2701 * To convert file try: 2702 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw 2703 */ 2704 dev_err(dev, "Aborting: firmware file must be in binary format\n"); 2705 2706 return -EINVAL; 2707 } 2708 2709 static int mxt_load_fw(struct device *dev, const char *fn) 2710 { 2711 struct mxt_data *data = dev_get_drvdata(dev); 2712 const struct firmware *fw = NULL; 2713 unsigned int frame_size; 2714 unsigned int pos = 0; 2715 unsigned int retry = 0; 2716 unsigned int frame = 0; 2717 int ret; 2718 2719 ret = request_firmware(&fw, fn, dev); 2720 if (ret) { 2721 dev_err(dev, "Unable to open firmware %s\n", fn); 2722 return ret; 2723 } 2724 2725 /* Check for incorrect enc file */ 2726 ret = mxt_check_firmware_format(dev, fw); 2727 if (ret) 2728 goto release_firmware; 2729 2730 if (!data->in_bootloader) { 2731 /* Change to the bootloader mode */ 2732 data->in_bootloader = true; 2733 2734 ret = mxt_t6_command(data, MXT_COMMAND_RESET, 2735 MXT_BOOT_VALUE, false); 2736 if (ret) 2737 goto release_firmware; 2738 2739 msleep(MXT_RESET_TIME); 2740 2741 /* Do not need to scan since we know family ID */ 2742 ret = mxt_lookup_bootloader_address(data, 0); 2743 if (ret) 2744 goto release_firmware; 2745 2746 mxt_free_input_device(data); 2747 mxt_free_object_table(data); 2748 } else { 2749 enable_irq(data->irq); 2750 } 2751 2752 reinit_completion(&data->bl_completion); 2753 2754 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD, false); 2755 if (ret) { 2756 /* Bootloader may still be unlocked from previous attempt */ 2757 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, false); 2758 if (ret) 2759 goto disable_irq; 2760 } else { 2761 dev_info(dev, "Unlocking bootloader\n"); 2762 2763 /* Unlock bootloader */ 2764 ret = mxt_send_bootloader_cmd(data, true); 2765 if (ret) 2766 goto disable_irq; 2767 } 2768 2769 while (pos < fw->size) { 2770 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true); 2771 if (ret) 2772 goto disable_irq; 2773 2774 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1)); 2775 2776 /* Take account of CRC bytes */ 2777 frame_size += 2; 2778 2779 /* Write one frame to device */ 2780 ret = mxt_bootloader_write(data, fw->data + pos, frame_size); 2781 if (ret) 2782 goto disable_irq; 2783 2784 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true); 2785 if (ret) { 2786 retry++; 2787 2788 /* Back off by 20ms per retry */ 2789 msleep(retry * 20); 2790 2791 if (retry > 20) { 2792 dev_err(dev, "Retry count exceeded\n"); 2793 goto disable_irq; 2794 } 2795 } else { 2796 retry = 0; 2797 pos += frame_size; 2798 frame++; 2799 } 2800 2801 if (frame % 50 == 0) 2802 dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n", 2803 frame, pos, fw->size); 2804 } 2805 2806 /* Wait for flash. */ 2807 ret = mxt_wait_for_completion(data, &data->bl_completion, 2808 MXT_FW_RESET_TIME); 2809 if (ret) 2810 goto disable_irq; 2811 2812 dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos); 2813 2814 /* 2815 * Wait for device to reset. Some bootloader versions do not assert 2816 * the CHG line after bootloading has finished, so ignore potential 2817 * errors. 2818 */ 2819 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME); 2820 2821 data->in_bootloader = false; 2822 2823 disable_irq: 2824 disable_irq(data->irq); 2825 release_firmware: 2826 release_firmware(fw); 2827 return ret; 2828 } 2829 2830 static ssize_t mxt_update_fw_store(struct device *dev, 2831 struct device_attribute *attr, 2832 const char *buf, size_t count) 2833 { 2834 struct mxt_data *data = dev_get_drvdata(dev); 2835 int error; 2836 2837 error = mxt_load_fw(dev, MXT_FW_NAME); 2838 if (error) { 2839 dev_err(dev, "The firmware update failed(%d)\n", error); 2840 count = error; 2841 } else { 2842 dev_info(dev, "The firmware update succeeded\n"); 2843 2844 error = mxt_initialize(data); 2845 if (error) 2846 return error; 2847 } 2848 2849 return count; 2850 } 2851 2852 static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL); 2853 static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL); 2854 static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL); 2855 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store); 2856 2857 static struct attribute *mxt_attrs[] = { 2858 &dev_attr_fw_version.attr, 2859 &dev_attr_hw_version.attr, 2860 &dev_attr_object.attr, 2861 &dev_attr_update_fw.attr, 2862 NULL 2863 }; 2864 2865 static const struct attribute_group mxt_attr_group = { 2866 .attrs = mxt_attrs, 2867 }; 2868 2869 static void mxt_start(struct mxt_data *data) 2870 { 2871 switch (data->pdata->suspend_mode) { 2872 case MXT_SUSPEND_T9_CTRL: 2873 mxt_soft_reset(data); 2874 2875 /* Touch enable */ 2876 /* 0x83 = SCANEN | RPTEN | ENABLE */ 2877 mxt_write_object(data, 2878 MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0x83); 2879 break; 2880 2881 case MXT_SUSPEND_DEEP_SLEEP: 2882 default: 2883 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN); 2884 2885 /* Recalibrate since chip has been in deep sleep */ 2886 mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false); 2887 break; 2888 } 2889 2890 } 2891 2892 static void mxt_stop(struct mxt_data *data) 2893 { 2894 switch (data->pdata->suspend_mode) { 2895 case MXT_SUSPEND_T9_CTRL: 2896 /* Touch disable */ 2897 mxt_write_object(data, 2898 MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0); 2899 break; 2900 2901 case MXT_SUSPEND_DEEP_SLEEP: 2902 default: 2903 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_DEEPSLEEP); 2904 break; 2905 } 2906 } 2907 2908 static int mxt_input_open(struct input_dev *dev) 2909 { 2910 struct mxt_data *data = input_get_drvdata(dev); 2911 2912 mxt_start(data); 2913 2914 return 0; 2915 } 2916 2917 static void mxt_input_close(struct input_dev *dev) 2918 { 2919 struct mxt_data *data = input_get_drvdata(dev); 2920 2921 mxt_stop(data); 2922 } 2923 2924 #ifdef CONFIG_OF 2925 static const struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client) 2926 { 2927 struct mxt_platform_data *pdata; 2928 struct device_node *np = client->dev.of_node; 2929 u32 *keymap; 2930 int proplen, ret; 2931 2932 if (!np) 2933 return ERR_PTR(-ENOENT); 2934 2935 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); 2936 if (!pdata) 2937 return ERR_PTR(-ENOMEM); 2938 2939 if (of_find_property(np, "linux,gpio-keymap", &proplen)) { 2940 pdata->t19_num_keys = proplen / sizeof(u32); 2941 2942 keymap = devm_kzalloc(&client->dev, 2943 pdata->t19_num_keys * sizeof(keymap[0]), 2944 GFP_KERNEL); 2945 if (!keymap) 2946 return ERR_PTR(-ENOMEM); 2947 2948 ret = of_property_read_u32_array(np, "linux,gpio-keymap", 2949 keymap, pdata->t19_num_keys); 2950 if (ret) 2951 dev_warn(&client->dev, 2952 "Couldn't read linux,gpio-keymap: %d\n", ret); 2953 2954 pdata->t19_keymap = keymap; 2955 } 2956 2957 pdata->suspend_mode = MXT_SUSPEND_DEEP_SLEEP; 2958 2959 return pdata; 2960 } 2961 #else 2962 static const struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client) 2963 { 2964 return ERR_PTR(-ENOENT); 2965 } 2966 #endif 2967 2968 #ifdef CONFIG_ACPI 2969 2970 struct mxt_acpi_platform_data { 2971 const char *hid; 2972 struct mxt_platform_data pdata; 2973 }; 2974 2975 static unsigned int samus_touchpad_buttons[] = { 2976 KEY_RESERVED, 2977 KEY_RESERVED, 2978 KEY_RESERVED, 2979 BTN_LEFT 2980 }; 2981 2982 static struct mxt_acpi_platform_data samus_platform_data[] = { 2983 { 2984 /* Touchpad */ 2985 .hid = "ATML0000", 2986 .pdata = { 2987 .t19_num_keys = ARRAY_SIZE(samus_touchpad_buttons), 2988 .t19_keymap = samus_touchpad_buttons, 2989 }, 2990 }, 2991 { 2992 /* Touchscreen */ 2993 .hid = "ATML0001", 2994 }, 2995 { } 2996 }; 2997 2998 static unsigned int chromebook_tp_buttons[] = { 2999 KEY_RESERVED, 3000 KEY_RESERVED, 3001 KEY_RESERVED, 3002 KEY_RESERVED, 3003 KEY_RESERVED, 3004 BTN_LEFT 3005 }; 3006 3007 static struct mxt_acpi_platform_data chromebook_platform_data[] = { 3008 { 3009 /* Touchpad */ 3010 .hid = "ATML0000", 3011 .pdata = { 3012 .t19_num_keys = ARRAY_SIZE(chromebook_tp_buttons), 3013 .t19_keymap = chromebook_tp_buttons, 3014 }, 3015 }, 3016 { 3017 /* Touchscreen */ 3018 .hid = "ATML0001", 3019 }, 3020 { } 3021 }; 3022 3023 static const struct dmi_system_id mxt_dmi_table[] = { 3024 { 3025 /* 2015 Google Pixel */ 3026 .ident = "Chromebook Pixel 2", 3027 .matches = { 3028 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"), 3029 DMI_MATCH(DMI_PRODUCT_NAME, "Samus"), 3030 }, 3031 .driver_data = samus_platform_data, 3032 }, 3033 { 3034 /* Other Google Chromebooks */ 3035 .ident = "Chromebook", 3036 .matches = { 3037 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"), 3038 }, 3039 .driver_data = chromebook_platform_data, 3040 }, 3041 { } 3042 }; 3043 3044 static const struct mxt_platform_data *mxt_parse_acpi(struct i2c_client *client) 3045 { 3046 struct acpi_device *adev; 3047 const struct dmi_system_id *system_id; 3048 const struct mxt_acpi_platform_data *acpi_pdata; 3049 3050 /* 3051 * Ignore ACPI devices representing bootloader mode. 3052 * 3053 * This is a bit of a hack: Google Chromebook BIOS creates ACPI 3054 * devices for both application and bootloader modes, but we are 3055 * interested in application mode only (if device is in bootloader 3056 * mode we'll end up switching into application anyway). So far 3057 * application mode addresses were all above 0x40, so we'll use it 3058 * as a threshold. 3059 */ 3060 if (client->addr < 0x40) 3061 return ERR_PTR(-ENXIO); 3062 3063 adev = ACPI_COMPANION(&client->dev); 3064 if (!adev) 3065 return ERR_PTR(-ENOENT); 3066 3067 system_id = dmi_first_match(mxt_dmi_table); 3068 if (!system_id) 3069 return ERR_PTR(-ENOENT); 3070 3071 acpi_pdata = system_id->driver_data; 3072 if (!acpi_pdata) 3073 return ERR_PTR(-ENOENT); 3074 3075 while (acpi_pdata->hid) { 3076 if (!strcmp(acpi_device_hid(adev), acpi_pdata->hid)) 3077 return &acpi_pdata->pdata; 3078 3079 acpi_pdata++; 3080 } 3081 3082 return ERR_PTR(-ENOENT); 3083 } 3084 #else 3085 static const struct mxt_platform_data *mxt_parse_acpi(struct i2c_client *client) 3086 { 3087 return ERR_PTR(-ENOENT); 3088 } 3089 #endif 3090 3091 static const struct mxt_platform_data * 3092 mxt_get_platform_data(struct i2c_client *client) 3093 { 3094 const struct mxt_platform_data *pdata; 3095 3096 pdata = dev_get_platdata(&client->dev); 3097 if (pdata) 3098 return pdata; 3099 3100 pdata = mxt_parse_dt(client); 3101 if (!IS_ERR(pdata) || PTR_ERR(pdata) != -ENOENT) 3102 return pdata; 3103 3104 pdata = mxt_parse_acpi(client); 3105 if (!IS_ERR(pdata) || PTR_ERR(pdata) != -ENOENT) 3106 return pdata; 3107 3108 dev_err(&client->dev, "No platform data specified\n"); 3109 return ERR_PTR(-EINVAL); 3110 } 3111 3112 static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id) 3113 { 3114 struct mxt_data *data; 3115 const struct mxt_platform_data *pdata; 3116 int error; 3117 3118 pdata = mxt_get_platform_data(client); 3119 if (IS_ERR(pdata)) 3120 return PTR_ERR(pdata); 3121 3122 data = devm_kzalloc(&client->dev, sizeof(struct mxt_data), GFP_KERNEL); 3123 if (!data) 3124 return -ENOMEM; 3125 3126 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0", 3127 client->adapter->nr, client->addr); 3128 3129 data->client = client; 3130 data->pdata = pdata; 3131 data->irq = client->irq; 3132 i2c_set_clientdata(client, data); 3133 3134 init_completion(&data->bl_completion); 3135 init_completion(&data->reset_completion); 3136 init_completion(&data->crc_completion); 3137 3138 data->reset_gpio = devm_gpiod_get_optional(&client->dev, 3139 "reset", GPIOD_OUT_LOW); 3140 if (IS_ERR(data->reset_gpio)) { 3141 error = PTR_ERR(data->reset_gpio); 3142 dev_err(&client->dev, "Failed to get reset gpio: %d\n", error); 3143 return error; 3144 } 3145 3146 error = devm_request_threaded_irq(&client->dev, client->irq, 3147 NULL, mxt_interrupt, 3148 pdata->irqflags | IRQF_ONESHOT, 3149 client->name, data); 3150 if (error) { 3151 dev_err(&client->dev, "Failed to register interrupt\n"); 3152 return error; 3153 } 3154 3155 if (data->reset_gpio) { 3156 data->in_bootloader = true; 3157 msleep(MXT_RESET_TIME); 3158 reinit_completion(&data->bl_completion); 3159 gpiod_set_value(data->reset_gpio, 1); 3160 error = mxt_wait_for_completion(data, &data->bl_completion, 3161 MXT_RESET_TIMEOUT); 3162 if (error) 3163 return error; 3164 data->in_bootloader = false; 3165 } 3166 3167 disable_irq(client->irq); 3168 3169 error = mxt_initialize(data); 3170 if (error) 3171 return error; 3172 3173 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group); 3174 if (error) { 3175 dev_err(&client->dev, "Failure %d creating sysfs group\n", 3176 error); 3177 goto err_free_object; 3178 } 3179 3180 return 0; 3181 3182 err_free_object: 3183 mxt_free_input_device(data); 3184 mxt_free_object_table(data); 3185 return error; 3186 } 3187 3188 static int mxt_remove(struct i2c_client *client) 3189 { 3190 struct mxt_data *data = i2c_get_clientdata(client); 3191 3192 disable_irq(data->irq); 3193 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group); 3194 mxt_free_input_device(data); 3195 mxt_free_object_table(data); 3196 3197 return 0; 3198 } 3199 3200 static int __maybe_unused mxt_suspend(struct device *dev) 3201 { 3202 struct i2c_client *client = to_i2c_client(dev); 3203 struct mxt_data *data = i2c_get_clientdata(client); 3204 struct input_dev *input_dev = data->input_dev; 3205 3206 if (!input_dev) 3207 return 0; 3208 3209 mutex_lock(&input_dev->mutex); 3210 3211 if (input_dev->users) 3212 mxt_stop(data); 3213 3214 mutex_unlock(&input_dev->mutex); 3215 3216 return 0; 3217 } 3218 3219 static int __maybe_unused mxt_resume(struct device *dev) 3220 { 3221 struct i2c_client *client = to_i2c_client(dev); 3222 struct mxt_data *data = i2c_get_clientdata(client); 3223 struct input_dev *input_dev = data->input_dev; 3224 3225 if (!input_dev) 3226 return 0; 3227 3228 mutex_lock(&input_dev->mutex); 3229 3230 if (input_dev->users) 3231 mxt_start(data); 3232 3233 mutex_unlock(&input_dev->mutex); 3234 3235 return 0; 3236 } 3237 3238 static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume); 3239 3240 static const struct of_device_id mxt_of_match[] = { 3241 { .compatible = "atmel,maxtouch", }, 3242 {}, 3243 }; 3244 MODULE_DEVICE_TABLE(of, mxt_of_match); 3245 3246 #ifdef CONFIG_ACPI 3247 static const struct acpi_device_id mxt_acpi_id[] = { 3248 { "ATML0000", 0 }, /* Touchpad */ 3249 { "ATML0001", 0 }, /* Touchscreen */ 3250 { } 3251 }; 3252 MODULE_DEVICE_TABLE(acpi, mxt_acpi_id); 3253 #endif 3254 3255 static const struct i2c_device_id mxt_id[] = { 3256 { "qt602240_ts", 0 }, 3257 { "atmel_mxt_ts", 0 }, 3258 { "atmel_mxt_tp", 0 }, 3259 { "maxtouch", 0 }, 3260 { "mXT224", 0 }, 3261 { } 3262 }; 3263 MODULE_DEVICE_TABLE(i2c, mxt_id); 3264 3265 static struct i2c_driver mxt_driver = { 3266 .driver = { 3267 .name = "atmel_mxt_ts", 3268 .of_match_table = of_match_ptr(mxt_of_match), 3269 .acpi_match_table = ACPI_PTR(mxt_acpi_id), 3270 .pm = &mxt_pm_ops, 3271 }, 3272 .probe = mxt_probe, 3273 .remove = mxt_remove, 3274 .id_table = mxt_id, 3275 }; 3276 3277 module_i2c_driver(mxt_driver); 3278 3279 /* Module information */ 3280 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>"); 3281 MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver"); 3282 MODULE_LICENSE("GPL"); 3283