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