1 /* 2 * MELFAS MIP4 Touchscreen 3 * 4 * Copyright (C) 2016 MELFAS Inc. 5 * 6 * Author : Sangwon Jee <jeesw@melfas.com> 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * as published by the Free Software Foundation; either version 2 11 * of the License, or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 */ 18 19 #include <linux/acpi.h> 20 #include <linux/delay.h> 21 #include <linux/firmware.h> 22 #include <linux/gpio/consumer.h> 23 #include <linux/i2c.h> 24 #include <linux/input.h> 25 #include <linux/input/mt.h> 26 #include <linux/interrupt.h> 27 #include <linux/module.h> 28 #include <linux/of.h> 29 #include <linux/slab.h> 30 #include <asm/unaligned.h> 31 32 #define MIP4_DEVICE_NAME "mip4_ts" 33 34 /***************************************************************** 35 * Protocol 36 * Version : MIP 4.0 Rev 4.6 37 *****************************************************************/ 38 39 /* Address */ 40 #define MIP4_R0_BOOT 0x00 41 #define MIP4_R1_BOOT_MODE 0x01 42 #define MIP4_R1_BOOT_BUF_ADDR 0x10 43 #define MIP4_R1_BOOT_STATUS 0x20 44 #define MIP4_R1_BOOT_CMD 0x30 45 #define MIP4_R1_BOOT_TARGET_ADDR 0x40 46 #define MIP4_R1_BOOT_SIZE 0x44 47 48 #define MIP4_R0_INFO 0x01 49 #define MIP4_R1_INFO_PRODUCT_NAME 0x00 50 #define MIP4_R1_INFO_RESOLUTION_X 0x10 51 #define MIP4_R1_INFO_RESOLUTION_Y 0x12 52 #define MIP4_R1_INFO_NODE_NUM_X 0x14 53 #define MIP4_R1_INFO_NODE_NUM_Y 0x15 54 #define MIP4_R1_INFO_KEY_NUM 0x16 55 #define MIP4_R1_INFO_PRESSURE_NUM 0x17 56 #define MIP4_R1_INFO_LENGTH_X 0x18 57 #define MIP4_R1_INFO_LENGTH_Y 0x1A 58 #define MIP4_R1_INFO_PPM_X 0x1C 59 #define MIP4_R1_INFO_PPM_Y 0x1D 60 #define MIP4_R1_INFO_VERSION_BOOT 0x20 61 #define MIP4_R1_INFO_VERSION_CORE 0x22 62 #define MIP4_R1_INFO_VERSION_APP 0x24 63 #define MIP4_R1_INFO_VERSION_PARAM 0x26 64 #define MIP4_R1_INFO_SECT_BOOT_START 0x30 65 #define MIP4_R1_INFO_SECT_BOOT_END 0x31 66 #define MIP4_R1_INFO_SECT_CORE_START 0x32 67 #define MIP4_R1_INFO_SECT_CORE_END 0x33 68 #define MIP4_R1_INFO_SECT_APP_START 0x34 69 #define MIP4_R1_INFO_SECT_APP_END 0x35 70 #define MIP4_R1_INFO_SECT_PARAM_START 0x36 71 #define MIP4_R1_INFO_SECT_PARAM_END 0x37 72 #define MIP4_R1_INFO_BUILD_DATE 0x40 73 #define MIP4_R1_INFO_BUILD_TIME 0x44 74 #define MIP4_R1_INFO_CHECKSUM_PRECALC 0x48 75 #define MIP4_R1_INFO_CHECKSUM_REALTIME 0x4A 76 #define MIP4_R1_INFO_PROTOCOL_NAME 0x50 77 #define MIP4_R1_INFO_PROTOCOL_VERSION 0x58 78 #define MIP4_R1_INFO_IC_ID 0x70 79 #define MIP4_R1_INFO_IC_NAME 0x71 80 #define MIP4_R1_INFO_IC_VENDOR_ID 0x75 81 #define MIP4_R1_INFO_IC_HW_CATEGORY 0x77 82 #define MIP4_R1_INFO_CONTACT_THD_SCR 0x78 83 #define MIP4_R1_INFO_CONTACT_THD_KEY 0x7A 84 85 #define MIP4_R0_EVENT 0x02 86 #define MIP4_R1_EVENT_SUPPORTED_FUNC 0x00 87 #define MIP4_R1_EVENT_FORMAT 0x04 88 #define MIP4_R1_EVENT_SIZE 0x06 89 #define MIP4_R1_EVENT_PACKET_INFO 0x10 90 #define MIP4_R1_EVENT_PACKET_DATA 0x11 91 92 #define MIP4_R0_CTRL 0x06 93 #define MIP4_R1_CTRL_READY_STATUS 0x00 94 #define MIP4_R1_CTRL_EVENT_READY 0x01 95 #define MIP4_R1_CTRL_MODE 0x10 96 #define MIP4_R1_CTRL_EVENT_TRIGGER_TYPE 0x11 97 #define MIP4_R1_CTRL_RECALIBRATE 0x12 98 #define MIP4_R1_CTRL_POWER_STATE 0x13 99 #define MIP4_R1_CTRL_GESTURE_TYPE 0x14 100 #define MIP4_R1_CTRL_DISABLE_ESD_ALERT 0x18 101 #define MIP4_R1_CTRL_CHARGER_MODE 0x19 102 #define MIP4_R1_CTRL_HIGH_SENS_MODE 0x1A 103 #define MIP4_R1_CTRL_WINDOW_MODE 0x1B 104 #define MIP4_R1_CTRL_PALM_REJECTION 0x1C 105 #define MIP4_R1_CTRL_EDGE_CORRECTION 0x1D 106 #define MIP4_R1_CTRL_ENTER_GLOVE_MODE 0x1E 107 #define MIP4_R1_CTRL_I2C_ON_LPM 0x1F 108 #define MIP4_R1_CTRL_GESTURE_DEBUG 0x20 109 #define MIP4_R1_CTRL_PALM_EVENT 0x22 110 #define MIP4_R1_CTRL_PROXIMITY_SENSING 0x23 111 112 /* Value */ 113 #define MIP4_BOOT_MODE_BOOT 0x01 114 #define MIP4_BOOT_MODE_APP 0x02 115 116 #define MIP4_BOOT_STATUS_BUSY 0x05 117 #define MIP4_BOOT_STATUS_ERROR 0x0E 118 #define MIP4_BOOT_STATUS_DONE 0xA0 119 120 #define MIP4_BOOT_CMD_MASS_ERASE 0x15 121 #define MIP4_BOOT_CMD_PROGRAM 0x54 122 #define MIP4_BOOT_CMD_ERASE 0x8F 123 #define MIP4_BOOT_CMD_WRITE 0xA5 124 #define MIP4_BOOT_CMD_READ 0xC2 125 126 #define MIP4_EVENT_INPUT_TYPE_KEY 0 127 #define MIP4_EVENT_INPUT_TYPE_SCREEN 1 128 #define MIP4_EVENT_INPUT_TYPE_PROXIMITY 2 129 130 #define I2C_RETRY_COUNT 3 /* 2~ */ 131 132 #define MIP4_BUF_SIZE 128 133 #define MIP4_MAX_FINGERS 10 134 #define MIP4_MAX_KEYS 4 135 136 #define MIP4_TOUCH_MAJOR_MIN 0 137 #define MIP4_TOUCH_MAJOR_MAX 255 138 #define MIP4_TOUCH_MINOR_MIN 0 139 #define MIP4_TOUCH_MINOR_MAX 255 140 #define MIP4_PRESSURE_MIN 0 141 #define MIP4_PRESSURE_MAX 255 142 143 #define MIP4_FW_NAME "melfas_mip4.fw" 144 #define MIP4_FW_UPDATE_DEBUG 0 /* 0 (default) or 1 */ 145 146 struct mip4_fw_version { 147 u16 boot; 148 u16 core; 149 u16 app; 150 u16 param; 151 }; 152 153 struct mip4_ts { 154 struct i2c_client *client; 155 struct input_dev *input; 156 struct gpio_desc *gpio_ce; 157 158 char phys[32]; 159 char product_name[16]; 160 char ic_name[4]; 161 162 unsigned int max_x; 163 unsigned int max_y; 164 u8 node_x; 165 u8 node_y; 166 u8 node_key; 167 unsigned int ppm_x; 168 unsigned int ppm_y; 169 170 struct mip4_fw_version fw_version; 171 172 unsigned int event_size; 173 unsigned int event_format; 174 175 unsigned int key_num; 176 unsigned short key_code[MIP4_MAX_KEYS]; 177 178 bool wake_irq_enabled; 179 180 u8 buf[MIP4_BUF_SIZE]; 181 }; 182 183 static int mip4_i2c_xfer(struct mip4_ts *ts, 184 char *write_buf, unsigned int write_len, 185 char *read_buf, unsigned int read_len) 186 { 187 struct i2c_msg msg[] = { 188 { 189 .addr = ts->client->addr, 190 .flags = 0, 191 .buf = write_buf, 192 .len = write_len, 193 }, { 194 .addr = ts->client->addr, 195 .flags = I2C_M_RD, 196 .buf = read_buf, 197 .len = read_len, 198 }, 199 }; 200 int retry = I2C_RETRY_COUNT; 201 int res; 202 int error; 203 204 do { 205 res = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg)); 206 if (res == ARRAY_SIZE(msg)) 207 return 0; 208 209 error = res < 0 ? res : -EIO; 210 dev_err(&ts->client->dev, 211 "%s - i2c_transfer failed: %d (%d)\n", 212 __func__, error, res); 213 } while (--retry); 214 215 return error; 216 } 217 218 static void mip4_parse_fw_version(const u8 *buf, struct mip4_fw_version *v) 219 { 220 v->boot = get_unaligned_le16(buf + 0); 221 v->core = get_unaligned_le16(buf + 2); 222 v->app = get_unaligned_le16(buf + 4); 223 v->param = get_unaligned_le16(buf + 6); 224 } 225 226 /* 227 * Read chip firmware version 228 */ 229 static int mip4_get_fw_version(struct mip4_ts *ts) 230 { 231 u8 cmd[] = { MIP4_R0_INFO, MIP4_R1_INFO_VERSION_BOOT }; 232 u8 buf[sizeof(ts->fw_version)]; 233 int error; 234 235 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, sizeof(buf)); 236 if (error) { 237 memset(&ts->fw_version, 0xff, sizeof(ts->fw_version)); 238 return error; 239 } 240 241 mip4_parse_fw_version(buf, &ts->fw_version); 242 243 return 0; 244 } 245 246 /* 247 * Fetch device characteristics 248 */ 249 static int mip4_query_device(struct mip4_ts *ts) 250 { 251 int error; 252 u8 cmd[2]; 253 u8 buf[14]; 254 255 /* Product name */ 256 cmd[0] = MIP4_R0_INFO; 257 cmd[1] = MIP4_R1_INFO_PRODUCT_NAME; 258 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), 259 ts->product_name, sizeof(ts->product_name)); 260 if (error) 261 dev_warn(&ts->client->dev, 262 "Failed to retrieve product name: %d\n", error); 263 else 264 dev_dbg(&ts->client->dev, "product name: %.*s\n", 265 (int)sizeof(ts->product_name), ts->product_name); 266 267 /* IC name */ 268 cmd[0] = MIP4_R0_INFO; 269 cmd[1] = MIP4_R1_INFO_IC_NAME; 270 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), 271 ts->ic_name, sizeof(ts->ic_name)); 272 if (error) 273 dev_warn(&ts->client->dev, 274 "Failed to retrieve IC name: %d\n", error); 275 else 276 dev_dbg(&ts->client->dev, "IC name: %.*s\n", 277 (int)sizeof(ts->ic_name), ts->ic_name); 278 279 /* Firmware version */ 280 error = mip4_get_fw_version(ts); 281 if (error) 282 dev_warn(&ts->client->dev, 283 "Failed to retrieve FW version: %d\n", error); 284 else 285 dev_dbg(&ts->client->dev, "F/W Version: %04X %04X %04X %04X\n", 286 ts->fw_version.boot, ts->fw_version.core, 287 ts->fw_version.app, ts->fw_version.param); 288 289 /* Resolution */ 290 cmd[0] = MIP4_R0_INFO; 291 cmd[1] = MIP4_R1_INFO_RESOLUTION_X; 292 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 14); 293 if (error) { 294 dev_warn(&ts->client->dev, 295 "Failed to retrieve touchscreen parameters: %d\n", 296 error); 297 } else { 298 ts->max_x = get_unaligned_le16(&buf[0]); 299 ts->max_y = get_unaligned_le16(&buf[2]); 300 dev_dbg(&ts->client->dev, "max_x: %d, max_y: %d\n", 301 ts->max_x, ts->max_y); 302 303 ts->node_x = buf[4]; 304 ts->node_y = buf[5]; 305 ts->node_key = buf[6]; 306 dev_dbg(&ts->client->dev, 307 "node_x: %d, node_y: %d, node_key: %d\n", 308 ts->node_x, ts->node_y, ts->node_key); 309 310 ts->ppm_x = buf[12]; 311 ts->ppm_y = buf[13]; 312 dev_dbg(&ts->client->dev, "ppm_x: %d, ppm_y: %d\n", 313 ts->ppm_x, ts->ppm_y); 314 315 /* Key ts */ 316 if (ts->node_key > 0) 317 ts->key_num = ts->node_key; 318 } 319 320 /* Protocol */ 321 cmd[0] = MIP4_R0_EVENT; 322 cmd[1] = MIP4_R1_EVENT_SUPPORTED_FUNC; 323 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), buf, 7); 324 if (error) { 325 dev_warn(&ts->client->dev, 326 "Failed to retrieve device type: %d\n", error); 327 ts->event_format = 0xff; 328 } else { 329 ts->event_format = get_unaligned_le16(&buf[4]); 330 ts->event_size = buf[6]; 331 dev_dbg(&ts->client->dev, "event_format: %d, event_size: %d\n", 332 ts->event_format, ts->event_size); 333 334 if (ts->event_format == 2 || ts->event_format > 3) 335 dev_warn(&ts->client->dev, 336 "Unknown event format %d\n", ts->event_format); 337 } 338 339 return 0; 340 } 341 342 static int mip4_power_on(struct mip4_ts *ts) 343 { 344 if (ts->gpio_ce) { 345 gpiod_set_value_cansleep(ts->gpio_ce, 1); 346 347 /* Booting delay : 200~300ms */ 348 usleep_range(200 * 1000, 300 * 1000); 349 } 350 351 return 0; 352 } 353 354 static void mip4_power_off(struct mip4_ts *ts) 355 { 356 if (ts->gpio_ce) 357 gpiod_set_value_cansleep(ts->gpio_ce, 0); 358 } 359 360 /* 361 * Clear touch input event status 362 */ 363 static void mip4_clear_input(struct mip4_ts *ts) 364 { 365 int i; 366 367 /* Screen */ 368 for (i = 0; i < MIP4_MAX_FINGERS; i++) { 369 input_mt_slot(ts->input, i); 370 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0); 371 } 372 373 /* Keys */ 374 for (i = 0; i < ts->key_num; i++) 375 input_report_key(ts->input, ts->key_code[i], 0); 376 377 input_sync(ts->input); 378 } 379 380 static int mip4_enable(struct mip4_ts *ts) 381 { 382 int error; 383 384 error = mip4_power_on(ts); 385 if (error) 386 return error; 387 388 enable_irq(ts->client->irq); 389 390 return 0; 391 } 392 393 static void mip4_disable(struct mip4_ts *ts) 394 { 395 disable_irq(ts->client->irq); 396 397 mip4_power_off(ts); 398 399 mip4_clear_input(ts); 400 } 401 402 /***************************************************************** 403 * Input handling 404 *****************************************************************/ 405 406 static void mip4_report_keys(struct mip4_ts *ts, u8 *packet) 407 { 408 u8 key; 409 bool down; 410 411 switch (ts->event_format) { 412 case 0: 413 case 1: 414 key = packet[0] & 0x0F; 415 down = packet[0] & 0x80; 416 break; 417 418 case 3: 419 default: 420 key = packet[0] & 0x0F; 421 down = packet[1] & 0x01; 422 break; 423 } 424 425 /* Report key event */ 426 if (key >= 1 && key <= ts->key_num) { 427 unsigned short keycode = ts->key_code[key - 1]; 428 429 dev_dbg(&ts->client->dev, 430 "Key - ID: %d, keycode: %d, state: %d\n", 431 key, keycode, down); 432 433 input_event(ts->input, EV_MSC, MSC_SCAN, keycode); 434 input_report_key(ts->input, keycode, down); 435 436 } else { 437 dev_err(&ts->client->dev, "Unknown key: %d\n", key); 438 } 439 } 440 441 static void mip4_report_touch(struct mip4_ts *ts, u8 *packet) 442 { 443 int id; 444 bool hover; 445 bool palm; 446 bool state; 447 u16 x, y; 448 u8 pressure_stage = 0; 449 u8 pressure; 450 u8 size; 451 u8 touch_major; 452 u8 touch_minor; 453 454 switch (ts->event_format) { 455 case 0: 456 case 1: 457 /* Touch only */ 458 state = packet[0] & BIT(7); 459 hover = packet[0] & BIT(5); 460 palm = packet[0] & BIT(4); 461 id = (packet[0] & 0x0F) - 1; 462 x = ((packet[1] & 0x0F) << 8) | packet[2]; 463 y = (((packet[1] >> 4) & 0x0F) << 8) | 464 packet[3]; 465 pressure = packet[4]; 466 size = packet[5]; 467 if (ts->event_format == 0) { 468 touch_major = packet[5]; 469 touch_minor = packet[5]; 470 } else { 471 touch_major = packet[6]; 472 touch_minor = packet[7]; 473 } 474 break; 475 476 case 3: 477 default: 478 /* Touch + Force(Pressure) */ 479 id = (packet[0] & 0x0F) - 1; 480 hover = packet[1] & BIT(2); 481 palm = packet[1] & BIT(1); 482 state = packet[1] & BIT(0); 483 x = ((packet[2] & 0x0F) << 8) | packet[3]; 484 y = (((packet[2] >> 4) & 0x0F) << 8) | 485 packet[4]; 486 size = packet[6]; 487 pressure_stage = (packet[7] & 0xF0) >> 4; 488 pressure = ((packet[7] & 0x0F) << 8) | 489 packet[8]; 490 touch_major = packet[9]; 491 touch_minor = packet[10]; 492 break; 493 } 494 495 dev_dbg(&ts->client->dev, 496 "Screen - Slot: %d State: %d X: %04d Y: %04d Z: %d\n", 497 id, state, x, y, pressure); 498 499 if (unlikely(id < 0 || id >= MIP4_MAX_FINGERS)) { 500 dev_err(&ts->client->dev, "Screen - invalid slot ID: %d\n", id); 501 } else if (state) { 502 /* Press or Move event */ 503 input_mt_slot(ts->input, id); 504 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true); 505 input_report_abs(ts->input, ABS_MT_POSITION_X, x); 506 input_report_abs(ts->input, ABS_MT_POSITION_Y, y); 507 input_report_abs(ts->input, ABS_MT_PRESSURE, pressure); 508 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, touch_major); 509 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, touch_minor); 510 } else { 511 /* Release event */ 512 input_mt_slot(ts->input, id); 513 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 0); 514 } 515 516 input_mt_sync_frame(ts->input); 517 } 518 519 static int mip4_handle_packet(struct mip4_ts *ts, u8 *packet) 520 { 521 u8 type; 522 523 switch (ts->event_format) { 524 case 0: 525 case 1: 526 type = (packet[0] & 0x40) >> 6; 527 break; 528 529 case 3: 530 type = (packet[0] & 0xF0) >> 4; 531 break; 532 533 default: 534 /* Should not happen unless we have corrupted firmware */ 535 return -EINVAL; 536 } 537 538 dev_dbg(&ts->client->dev, "Type: %d\n", type); 539 540 /* Report input event */ 541 switch (type) { 542 case MIP4_EVENT_INPUT_TYPE_KEY: 543 mip4_report_keys(ts, packet); 544 break; 545 546 case MIP4_EVENT_INPUT_TYPE_SCREEN: 547 mip4_report_touch(ts, packet); 548 break; 549 550 default: 551 dev_err(&ts->client->dev, "Unknown event type: %d\n", type); 552 break; 553 } 554 555 return 0; 556 } 557 558 static irqreturn_t mip4_interrupt(int irq, void *dev_id) 559 { 560 struct mip4_ts *ts = dev_id; 561 struct i2c_client *client = ts->client; 562 unsigned int i; 563 int error; 564 u8 cmd[2]; 565 u8 size; 566 bool alert; 567 568 /* Read packet info */ 569 cmd[0] = MIP4_R0_EVENT; 570 cmd[1] = MIP4_R1_EVENT_PACKET_INFO; 571 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, 1); 572 if (error) { 573 dev_err(&client->dev, 574 "Failed to read packet info: %d\n", error); 575 goto out; 576 } 577 578 size = ts->buf[0] & 0x7F; 579 alert = ts->buf[0] & BIT(7); 580 dev_dbg(&client->dev, "packet size: %d, alert: %d\n", size, alert); 581 582 /* Check size */ 583 if (!size) { 584 dev_err(&client->dev, "Empty packet\n"); 585 goto out; 586 } 587 588 /* Read packet data */ 589 cmd[0] = MIP4_R0_EVENT; 590 cmd[1] = MIP4_R1_EVENT_PACKET_DATA; 591 error = mip4_i2c_xfer(ts, cmd, sizeof(cmd), ts->buf, size); 592 if (error) { 593 dev_err(&client->dev, 594 "Failed to read packet data: %d\n", error); 595 goto out; 596 } 597 598 if (alert) { 599 dev_dbg(&client->dev, "Alert: %d\n", ts->buf[0]); 600 } else { 601 for (i = 0; i < size; i += ts->event_size) { 602 error = mip4_handle_packet(ts, &ts->buf[i]); 603 if (error) 604 break; 605 } 606 607 input_sync(ts->input); 608 } 609 610 out: 611 return IRQ_HANDLED; 612 } 613 614 static int mip4_input_open(struct input_dev *dev) 615 { 616 struct mip4_ts *ts = input_get_drvdata(dev); 617 618 return mip4_enable(ts); 619 } 620 621 static void mip4_input_close(struct input_dev *dev) 622 { 623 struct mip4_ts *ts = input_get_drvdata(dev); 624 625 mip4_disable(ts); 626 } 627 628 /***************************************************************** 629 * Firmware update 630 *****************************************************************/ 631 632 /* Firmware Info */ 633 #define MIP4_BL_PAGE_SIZE 512 /* 512 */ 634 #define MIP4_BL_PACKET_SIZE 512 /* 512, 256, 128, 64, ... */ 635 636 /* 637 * Firmware binary tail info 638 */ 639 640 struct mip4_bin_tail { 641 u8 tail_mark[4]; 642 u8 chip_name[4]; 643 644 __le32 bin_start_addr; 645 __le32 bin_length; 646 647 __le16 ver_boot; 648 __le16 ver_core; 649 __le16 ver_app; 650 __le16 ver_param; 651 652 u8 boot_start; 653 u8 boot_end; 654 u8 core_start; 655 u8 core_end; 656 u8 app_start; 657 u8 app_end; 658 u8 param_start; 659 u8 param_end; 660 661 u8 checksum_type; 662 u8 hw_category; 663 664 __le16 param_id; 665 __le32 param_length; 666 __le32 build_date; 667 __le32 build_time; 668 669 __le32 reserved1; 670 __le32 reserved2; 671 __le16 reserved3; 672 __le16 tail_size; 673 __le32 crc; 674 } __packed; 675 676 #define MIP4_BIN_TAIL_MARK "MBT\001" 677 #define MIP4_BIN_TAIL_SIZE (sizeof(struct mip4_bin_tail)) 678 679 /* 680 * Bootloader - Read status 681 */ 682 static int mip4_bl_read_status(struct mip4_ts *ts) 683 { 684 u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_STATUS }; 685 u8 result; 686 struct i2c_msg msg[] = { 687 { 688 .addr = ts->client->addr, 689 .flags = 0, 690 .buf = cmd, 691 .len = sizeof(cmd), 692 }, { 693 .addr = ts->client->addr, 694 .flags = I2C_M_RD, 695 .buf = &result, 696 .len = sizeof(result), 697 }, 698 }; 699 int ret; 700 int error; 701 int retry = 1000; 702 703 do { 704 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg)); 705 if (ret != ARRAY_SIZE(msg)) { 706 error = ret < 0 ? ret : -EIO; 707 dev_err(&ts->client->dev, 708 "Failed to read bootloader status: %d\n", 709 error); 710 return error; 711 } 712 713 switch (result) { 714 case MIP4_BOOT_STATUS_DONE: 715 dev_dbg(&ts->client->dev, "%s - done\n", __func__); 716 return 0; 717 718 case MIP4_BOOT_STATUS_ERROR: 719 dev_err(&ts->client->dev, "Bootloader failure\n"); 720 return -EIO; 721 722 case MIP4_BOOT_STATUS_BUSY: 723 dev_dbg(&ts->client->dev, "%s - Busy\n", __func__); 724 error = -EBUSY; 725 break; 726 727 default: 728 dev_err(&ts->client->dev, 729 "Unexpected bootloader status: %#02x\n", 730 result); 731 error = -EINVAL; 732 break; 733 } 734 735 usleep_range(1000, 2000); 736 } while (--retry); 737 738 return error; 739 } 740 741 /* 742 * Bootloader - Change mode 743 */ 744 static int mip4_bl_change_mode(struct mip4_ts *ts, u8 mode) 745 { 746 u8 mode_chg_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE, mode }; 747 u8 mode_read_cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_MODE }; 748 u8 result; 749 struct i2c_msg msg[] = { 750 { 751 .addr = ts->client->addr, 752 .flags = 0, 753 .buf = mode_read_cmd, 754 .len = sizeof(mode_read_cmd), 755 }, { 756 .addr = ts->client->addr, 757 .flags = I2C_M_RD, 758 .buf = &result, 759 .len = sizeof(result), 760 }, 761 }; 762 int retry = 10; 763 int ret; 764 int error; 765 766 do { 767 /* Send mode change command */ 768 ret = i2c_master_send(ts->client, 769 mode_chg_cmd, sizeof(mode_chg_cmd)); 770 if (ret != sizeof(mode_chg_cmd)) { 771 error = ret < 0 ? ret : -EIO; 772 dev_err(&ts->client->dev, 773 "Failed to send %d mode change: %d (%d)\n", 774 mode, error, ret); 775 return error; 776 } 777 778 dev_dbg(&ts->client->dev, 779 "Sent mode change request (mode: %d)\n", mode); 780 781 /* Wait */ 782 msleep(1000); 783 784 /* Verify target mode */ 785 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg)); 786 if (ret != ARRAY_SIZE(msg)) { 787 error = ret < 0 ? ret : -EIO; 788 dev_err(&ts->client->dev, 789 "Failed to read device mode: %d\n", error); 790 return error; 791 } 792 793 dev_dbg(&ts->client->dev, 794 "Current device mode: %d, want: %d\n", result, mode); 795 796 if (result == mode) 797 return 0; 798 799 } while (--retry); 800 801 return -EIO; 802 } 803 804 /* 805 * Bootloader - Start bootloader mode 806 */ 807 static int mip4_bl_enter(struct mip4_ts *ts) 808 { 809 return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_BOOT); 810 } 811 812 /* 813 * Bootloader - Exit bootloader mode 814 */ 815 static int mip4_bl_exit(struct mip4_ts *ts) 816 { 817 return mip4_bl_change_mode(ts, MIP4_BOOT_MODE_APP); 818 } 819 820 static int mip4_bl_get_address(struct mip4_ts *ts, u16 *buf_addr) 821 { 822 u8 cmd[] = { MIP4_R0_BOOT, MIP4_R1_BOOT_BUF_ADDR }; 823 u8 result[sizeof(u16)]; 824 struct i2c_msg msg[] = { 825 { 826 .addr = ts->client->addr, 827 .flags = 0, 828 .buf = cmd, 829 .len = sizeof(cmd), 830 }, { 831 .addr = ts->client->addr, 832 .flags = I2C_M_RD, 833 .buf = result, 834 .len = sizeof(result), 835 }, 836 }; 837 int ret; 838 int error; 839 840 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg)); 841 if (ret != ARRAY_SIZE(msg)) { 842 error = ret < 0 ? ret : -EIO; 843 dev_err(&ts->client->dev, 844 "Failed to retrieve bootloader buffer address: %d\n", 845 error); 846 return error; 847 } 848 849 *buf_addr = get_unaligned_le16(result); 850 dev_dbg(&ts->client->dev, 851 "Bootloader buffer address %#04x\n", *buf_addr); 852 853 return 0; 854 } 855 856 static int mip4_bl_program_page(struct mip4_ts *ts, int offset, 857 const u8 *data, int length, u16 buf_addr) 858 { 859 u8 cmd[6]; 860 u8 *data_buf; 861 u16 buf_offset; 862 int ret; 863 int error; 864 865 dev_dbg(&ts->client->dev, "Writing page @%#06x (%d)\n", 866 offset, length); 867 868 if (length > MIP4_BL_PAGE_SIZE || length % MIP4_BL_PACKET_SIZE) { 869 dev_err(&ts->client->dev, 870 "Invalid page length: %d\n", length); 871 return -EINVAL; 872 } 873 874 data_buf = kmalloc(2 + MIP4_BL_PACKET_SIZE, GFP_KERNEL); 875 if (!data_buf) 876 return -ENOMEM; 877 878 /* Addr */ 879 cmd[0] = MIP4_R0_BOOT; 880 cmd[1] = MIP4_R1_BOOT_TARGET_ADDR; 881 put_unaligned_le32(offset, &cmd[2]); 882 ret = i2c_master_send(ts->client, cmd, 6); 883 if (ret != 6) { 884 error = ret < 0 ? ret : -EIO; 885 dev_err(&ts->client->dev, 886 "Failed to send write page address: %d\n", error); 887 goto out; 888 } 889 890 /* Size */ 891 cmd[0] = MIP4_R0_BOOT; 892 cmd[1] = MIP4_R1_BOOT_SIZE; 893 put_unaligned_le32(length, &cmd[2]); 894 ret = i2c_master_send(ts->client, cmd, 6); 895 if (ret != 6) { 896 error = ret < 0 ? ret : -EIO; 897 dev_err(&ts->client->dev, 898 "Failed to send write page size: %d\n", error); 899 goto out; 900 } 901 902 /* Data */ 903 for (buf_offset = 0; 904 buf_offset < length; 905 buf_offset += MIP4_BL_PACKET_SIZE) { 906 dev_dbg(&ts->client->dev, 907 "writing chunk at %#04x (size %d)\n", 908 buf_offset, MIP4_BL_PACKET_SIZE); 909 put_unaligned_be16(buf_addr + buf_offset, data_buf); 910 memcpy(&data_buf[2], &data[buf_offset], MIP4_BL_PACKET_SIZE); 911 ret = i2c_master_send(ts->client, 912 data_buf, 2 + MIP4_BL_PACKET_SIZE); 913 if (ret != 2 + MIP4_BL_PACKET_SIZE) { 914 error = ret < 0 ? ret : -EIO; 915 dev_err(&ts->client->dev, 916 "Failed to read chunk at %#04x (size %d): %d\n", 917 buf_offset, MIP4_BL_PACKET_SIZE, error); 918 goto out; 919 } 920 } 921 922 /* Command */ 923 cmd[0] = MIP4_R0_BOOT; 924 cmd[1] = MIP4_R1_BOOT_CMD; 925 cmd[2] = MIP4_BOOT_CMD_PROGRAM; 926 ret = i2c_master_send(ts->client, cmd, 3); 927 if (ret != 3) { 928 error = ret < 0 ? ret : -EIO; 929 dev_err(&ts->client->dev, 930 "Failed to send 'write' command: %d\n", error); 931 goto out; 932 } 933 934 /* Status */ 935 error = mip4_bl_read_status(ts); 936 937 out: 938 kfree(data_buf); 939 return error ? error : 0; 940 } 941 942 static int mip4_bl_verify_page(struct mip4_ts *ts, int offset, 943 const u8 *data, int length, int buf_addr) 944 { 945 u8 cmd[8]; 946 u8 *read_buf; 947 int buf_offset; 948 struct i2c_msg msg[] = { 949 { 950 .addr = ts->client->addr, 951 .flags = 0, 952 .buf = cmd, 953 .len = 2, 954 }, { 955 .addr = ts->client->addr, 956 .flags = I2C_M_RD, 957 .len = MIP4_BL_PACKET_SIZE, 958 }, 959 }; 960 int ret; 961 int error; 962 963 dev_dbg(&ts->client->dev, "Validating page @%#06x (%d)\n", 964 offset, length); 965 966 /* Addr */ 967 cmd[0] = MIP4_R0_BOOT; 968 cmd[1] = MIP4_R1_BOOT_TARGET_ADDR; 969 put_unaligned_le32(offset, &cmd[2]); 970 ret = i2c_master_send(ts->client, cmd, 6); 971 if (ret != 6) { 972 error = ret < 0 ? ret : -EIO; 973 dev_err(&ts->client->dev, 974 "Failed to send read page address: %d\n", error); 975 return error; 976 } 977 978 /* Size */ 979 cmd[0] = MIP4_R0_BOOT; 980 cmd[1] = MIP4_R1_BOOT_SIZE; 981 put_unaligned_le32(length, &cmd[2]); 982 ret = i2c_master_send(ts->client, cmd, 6); 983 if (ret != 6) { 984 error = ret < 0 ? ret : -EIO; 985 dev_err(&ts->client->dev, 986 "Failed to send read page size: %d\n", error); 987 return error; 988 } 989 990 /* Command */ 991 cmd[0] = MIP4_R0_BOOT; 992 cmd[1] = MIP4_R1_BOOT_CMD; 993 cmd[2] = MIP4_BOOT_CMD_READ; 994 ret = i2c_master_send(ts->client, cmd, 3); 995 if (ret != 3) { 996 error = ret < 0 ? ret : -EIO; 997 dev_err(&ts->client->dev, 998 "Failed to send 'read' command: %d\n", error); 999 return error; 1000 } 1001 1002 /* Status */ 1003 error = mip4_bl_read_status(ts); 1004 if (error) 1005 return error; 1006 1007 /* Read */ 1008 msg[1].buf = read_buf = kmalloc(MIP4_BL_PACKET_SIZE, GFP_KERNEL); 1009 if (!read_buf) 1010 return -ENOMEM; 1011 1012 for (buf_offset = 0; 1013 buf_offset < length; 1014 buf_offset += MIP4_BL_PACKET_SIZE) { 1015 dev_dbg(&ts->client->dev, 1016 "reading chunk at %#04x (size %d)\n", 1017 buf_offset, MIP4_BL_PACKET_SIZE); 1018 put_unaligned_be16(buf_addr + buf_offset, cmd); 1019 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg)); 1020 if (ret != ARRAY_SIZE(msg)) { 1021 error = ret < 0 ? ret : -EIO; 1022 dev_err(&ts->client->dev, 1023 "Failed to read chunk at %#04x (size %d): %d\n", 1024 buf_offset, MIP4_BL_PACKET_SIZE, error); 1025 break; 1026 } 1027 1028 if (memcmp(&data[buf_offset], read_buf, MIP4_BL_PACKET_SIZE)) { 1029 dev_err(&ts->client->dev, 1030 "Failed to validate chunk at %#04x (size %d)\n", 1031 buf_offset, MIP4_BL_PACKET_SIZE); 1032 #if MIP4_FW_UPDATE_DEBUG 1033 print_hex_dump(KERN_DEBUG, 1034 MIP4_DEVICE_NAME " F/W File: ", 1035 DUMP_PREFIX_OFFSET, 16, 1, 1036 data + offset, MIP4_BL_PACKET_SIZE, 1037 false); 1038 print_hex_dump(KERN_DEBUG, 1039 MIP4_DEVICE_NAME " F/W Chip: ", 1040 DUMP_PREFIX_OFFSET, 16, 1, 1041 read_buf, MIP4_BL_PAGE_SIZE, false); 1042 #endif 1043 error = -EINVAL; 1044 break; 1045 } 1046 } 1047 1048 kfree(read_buf); 1049 return error ? error : 0; 1050 } 1051 1052 /* 1053 * Flash chip firmware 1054 */ 1055 static int mip4_flash_fw(struct mip4_ts *ts, 1056 const u8 *fw_data, u32 fw_size, u32 fw_offset) 1057 { 1058 struct i2c_client *client = ts->client; 1059 int offset; 1060 u16 buf_addr; 1061 int error, error2; 1062 1063 /* Enter bootloader mode */ 1064 dev_dbg(&client->dev, "Entering bootloader mode\n"); 1065 1066 error = mip4_bl_enter(ts); 1067 if (error) { 1068 dev_err(&client->dev, 1069 "Failed to enter bootloader mode: %d\n", 1070 error); 1071 return error; 1072 } 1073 1074 /* Read info */ 1075 error = mip4_bl_get_address(ts, &buf_addr); 1076 if (error) 1077 goto exit_bl; 1078 1079 /* Program & Verify */ 1080 dev_dbg(&client->dev, 1081 "Program & Verify, page size: %d, packet size: %d\n", 1082 MIP4_BL_PAGE_SIZE, MIP4_BL_PACKET_SIZE); 1083 1084 for (offset = fw_offset; 1085 offset < fw_offset + fw_size; 1086 offset += MIP4_BL_PAGE_SIZE) { 1087 /* Program */ 1088 error = mip4_bl_program_page(ts, offset, fw_data + offset, 1089 MIP4_BL_PAGE_SIZE, buf_addr); 1090 if (error) 1091 break; 1092 1093 /* Verify */ 1094 error = mip4_bl_verify_page(ts, offset, fw_data + offset, 1095 MIP4_BL_PAGE_SIZE, buf_addr); 1096 if (error) 1097 break; 1098 } 1099 1100 exit_bl: 1101 /* Exit bootloader mode */ 1102 dev_dbg(&client->dev, "Exiting bootloader mode\n"); 1103 1104 error2 = mip4_bl_exit(ts); 1105 if (error2) { 1106 dev_err(&client->dev, 1107 "Failed to exit bootloader mode: %d\n", error2); 1108 if (!error) 1109 error = error2; 1110 } 1111 1112 /* Reset chip */ 1113 mip4_power_off(ts); 1114 mip4_power_on(ts); 1115 1116 mip4_query_device(ts); 1117 1118 /* Refresh device parameters */ 1119 input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0); 1120 input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0); 1121 input_set_abs_params(ts->input, ABS_X, 0, ts->max_x, 0, 0); 1122 input_set_abs_params(ts->input, ABS_Y, 0, ts->max_y, 0, 0); 1123 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x); 1124 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y); 1125 input_abs_set_res(ts->input, ABS_X, ts->ppm_x); 1126 input_abs_set_res(ts->input, ABS_Y, ts->ppm_y); 1127 1128 return error ? error : 0; 1129 } 1130 1131 static int mip4_parse_firmware(struct mip4_ts *ts, const struct firmware *fw, 1132 u32 *fw_offset_start, u32 *fw_size, 1133 const struct mip4_bin_tail **pfw_info) 1134 { 1135 const struct mip4_bin_tail *fw_info; 1136 struct mip4_fw_version fw_version; 1137 u16 tail_size; 1138 1139 if (fw->size < MIP4_BIN_TAIL_SIZE) { 1140 dev_err(&ts->client->dev, 1141 "Invalid firmware, size mismatch (tail %zd vs %zd)\n", 1142 MIP4_BIN_TAIL_SIZE, fw->size); 1143 return -EINVAL; 1144 } 1145 1146 fw_info = (const void *)&fw->data[fw->size - MIP4_BIN_TAIL_SIZE]; 1147 1148 #if MIP4_FW_UPDATE_DEBUG 1149 print_hex_dump(KERN_ERR, MIP4_DEVICE_NAME " Bin Info: ", 1150 DUMP_PREFIX_OFFSET, 16, 1, *fw_info, tail_size, false); 1151 #endif 1152 1153 tail_size = get_unaligned_le16(&fw_info->tail_size); 1154 if (tail_size != MIP4_BIN_TAIL_SIZE) { 1155 dev_err(&ts->client->dev, 1156 "wrong tail size: %d (expected %zd)\n", 1157 tail_size, MIP4_BIN_TAIL_SIZE); 1158 return -EINVAL; 1159 } 1160 1161 /* Check bin format */ 1162 if (memcmp(fw_info->tail_mark, MIP4_BIN_TAIL_MARK, 1163 sizeof(fw_info->tail_mark))) { 1164 dev_err(&ts->client->dev, 1165 "unable to locate tail marker (%*ph vs %*ph)\n", 1166 (int)sizeof(fw_info->tail_mark), fw_info->tail_mark, 1167 (int)sizeof(fw_info->tail_mark), MIP4_BIN_TAIL_MARK); 1168 return -EINVAL; 1169 } 1170 1171 *fw_offset_start = get_unaligned_le32(&fw_info->bin_start_addr); 1172 *fw_size = get_unaligned_le32(&fw_info->bin_length); 1173 1174 dev_dbg(&ts->client->dev, 1175 "F/W Data offset: %#08x, size: %d\n", 1176 *fw_offset_start, *fw_size); 1177 1178 if (*fw_size % MIP4_BL_PAGE_SIZE) { 1179 dev_err(&ts->client->dev, 1180 "encoded fw length %d is not multiple of pages (%d)\n", 1181 *fw_size, MIP4_BL_PAGE_SIZE); 1182 return -EINVAL; 1183 } 1184 1185 if (fw->size != *fw_offset_start + *fw_size) { 1186 dev_err(&ts->client->dev, 1187 "Wrong firmware size, expected %d bytes, got %zd\n", 1188 *fw_offset_start + *fw_size, fw->size); 1189 return -EINVAL; 1190 } 1191 1192 mip4_parse_fw_version((const u8 *)&fw_info->ver_boot, &fw_version); 1193 1194 dev_dbg(&ts->client->dev, 1195 "F/W file version %04X %04X %04X %04X\n", 1196 fw_version.boot, fw_version.core, 1197 fw_version.app, fw_version.param); 1198 1199 dev_dbg(&ts->client->dev, "F/W chip version: %04X %04X %04X %04X\n", 1200 ts->fw_version.boot, ts->fw_version.core, 1201 ts->fw_version.app, ts->fw_version.param); 1202 1203 /* Check F/W type */ 1204 if (fw_version.boot != 0xEEEE && fw_version.boot != 0xFFFF && 1205 fw_version.core == 0xEEEE && 1206 fw_version.app == 0xEEEE && 1207 fw_version.param == 0xEEEE) { 1208 dev_dbg(&ts->client->dev, "F/W type: Bootloader\n"); 1209 } else if (fw_version.boot == 0xEEEE && 1210 fw_version.core != 0xEEEE && fw_version.core != 0xFFFF && 1211 fw_version.app != 0xEEEE && fw_version.app != 0xFFFF && 1212 fw_version.param != 0xEEEE && fw_version.param != 0xFFFF) { 1213 dev_dbg(&ts->client->dev, "F/W type: Main\n"); 1214 } else { 1215 dev_err(&ts->client->dev, "Wrong firmware type\n"); 1216 return -EINVAL; 1217 } 1218 1219 return 0; 1220 } 1221 1222 static int mip4_execute_fw_update(struct mip4_ts *ts, const struct firmware *fw) 1223 { 1224 const struct mip4_bin_tail *fw_info; 1225 u32 fw_start_offset; 1226 u32 fw_size; 1227 int retires = 3; 1228 int error; 1229 1230 error = mip4_parse_firmware(ts, fw, 1231 &fw_start_offset, &fw_size, &fw_info); 1232 if (error) 1233 return error; 1234 1235 if (ts->input->users) { 1236 disable_irq(ts->client->irq); 1237 } else { 1238 error = mip4_power_on(ts); 1239 if (error) 1240 return error; 1241 } 1242 1243 /* Update firmware */ 1244 do { 1245 error = mip4_flash_fw(ts, fw->data, fw_size, fw_start_offset); 1246 if (!error) 1247 break; 1248 } while (--retires); 1249 1250 if (error) 1251 dev_err(&ts->client->dev, 1252 "Failed to flash firmware: %d\n", error); 1253 1254 /* Enable IRQ */ 1255 if (ts->input->users) 1256 enable_irq(ts->client->irq); 1257 else 1258 mip4_power_off(ts); 1259 1260 return error ? error : 0; 1261 } 1262 1263 static ssize_t mip4_sysfs_fw_update(struct device *dev, 1264 struct device_attribute *attr, 1265 const char *buf, size_t count) 1266 { 1267 struct i2c_client *client = to_i2c_client(dev); 1268 struct mip4_ts *ts = i2c_get_clientdata(client); 1269 const struct firmware *fw; 1270 int error; 1271 1272 error = request_firmware(&fw, MIP4_FW_NAME, dev); 1273 if (error) { 1274 dev_err(&ts->client->dev, 1275 "Failed to retrieve firmware %s: %d\n", 1276 MIP4_FW_NAME, error); 1277 return error; 1278 } 1279 1280 /* 1281 * Take input mutex to prevent racing with itself and also with 1282 * userspace opening and closing the device and also suspend/resume 1283 * transitions. 1284 */ 1285 mutex_lock(&ts->input->mutex); 1286 1287 error = mip4_execute_fw_update(ts, fw); 1288 1289 mutex_unlock(&ts->input->mutex); 1290 1291 release_firmware(fw); 1292 1293 if (error) { 1294 dev_err(&ts->client->dev, 1295 "Firmware update failed: %d\n", error); 1296 return error; 1297 } 1298 1299 return count; 1300 } 1301 1302 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mip4_sysfs_fw_update); 1303 1304 static ssize_t mip4_sysfs_read_fw_version(struct device *dev, 1305 struct device_attribute *attr, 1306 char *buf) 1307 { 1308 struct i2c_client *client = to_i2c_client(dev); 1309 struct mip4_ts *ts = i2c_get_clientdata(client); 1310 size_t count; 1311 1312 /* Take lock to prevent racing with firmware update */ 1313 mutex_lock(&ts->input->mutex); 1314 1315 count = snprintf(buf, PAGE_SIZE, "%04X %04X %04X %04X\n", 1316 ts->fw_version.boot, ts->fw_version.core, 1317 ts->fw_version.app, ts->fw_version.param); 1318 1319 mutex_unlock(&ts->input->mutex); 1320 1321 return count; 1322 } 1323 1324 static DEVICE_ATTR(fw_version, S_IRUGO, mip4_sysfs_read_fw_version, NULL); 1325 1326 static ssize_t mip4_sysfs_read_hw_version(struct device *dev, 1327 struct device_attribute *attr, 1328 char *buf) 1329 { 1330 struct i2c_client *client = to_i2c_client(dev); 1331 struct mip4_ts *ts = i2c_get_clientdata(client); 1332 size_t count; 1333 1334 /* Take lock to prevent racing with firmware update */ 1335 mutex_lock(&ts->input->mutex); 1336 1337 /* 1338 * product_name shows the name or version of the hardware 1339 * paired with current firmware in the chip. 1340 */ 1341 count = snprintf(buf, PAGE_SIZE, "%.*s\n", 1342 (int)sizeof(ts->product_name), ts->product_name); 1343 1344 mutex_unlock(&ts->input->mutex); 1345 1346 return count; 1347 } 1348 1349 static DEVICE_ATTR(hw_version, S_IRUGO, mip4_sysfs_read_hw_version, NULL); 1350 1351 static ssize_t mip4_sysfs_read_ic_name(struct device *dev, 1352 struct device_attribute *attr, 1353 char *buf) 1354 { 1355 struct i2c_client *client = to_i2c_client(dev); 1356 struct mip4_ts *ts = i2c_get_clientdata(client); 1357 size_t count; 1358 1359 mutex_lock(&ts->input->mutex); 1360 1361 count = snprintf(buf, PAGE_SIZE, "%.*s\n", 1362 (int)sizeof(ts->ic_name), ts->ic_name); 1363 1364 mutex_unlock(&ts->input->mutex); 1365 1366 return count; 1367 } 1368 1369 static DEVICE_ATTR(ic_name, S_IRUGO, mip4_sysfs_read_ic_name, NULL); 1370 1371 static struct attribute *mip4_attrs[] = { 1372 &dev_attr_fw_version.attr, 1373 &dev_attr_hw_version.attr, 1374 &dev_attr_ic_name.attr, 1375 &dev_attr_update_fw.attr, 1376 NULL, 1377 }; 1378 1379 static const struct attribute_group mip4_attr_group = { 1380 .attrs = mip4_attrs, 1381 }; 1382 1383 static void mip4_sysfs_remove(void *_data) 1384 { 1385 struct mip4_ts *ts = _data; 1386 1387 sysfs_remove_group(&ts->client->dev.kobj, &mip4_attr_group); 1388 } 1389 1390 static int mip4_probe(struct i2c_client *client, const struct i2c_device_id *id) 1391 { 1392 struct mip4_ts *ts; 1393 struct input_dev *input; 1394 int error; 1395 1396 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 1397 dev_err(&client->dev, "Not supported I2C adapter\n"); 1398 return -ENXIO; 1399 } 1400 1401 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); 1402 if (!ts) 1403 return -ENOMEM; 1404 1405 input = devm_input_allocate_device(&client->dev); 1406 if (!input) 1407 return -ENOMEM; 1408 1409 ts->client = client; 1410 ts->input = input; 1411 1412 snprintf(ts->phys, sizeof(ts->phys), 1413 "%s/input0", dev_name(&client->dev)); 1414 1415 ts->gpio_ce = devm_gpiod_get_optional(&client->dev, 1416 "ce", GPIOD_OUT_LOW); 1417 if (IS_ERR(ts->gpio_ce)) { 1418 error = PTR_ERR(ts->gpio_ce); 1419 if (error != EPROBE_DEFER) 1420 dev_err(&client->dev, 1421 "Failed to get gpio: %d\n", error); 1422 return error; 1423 } 1424 1425 error = mip4_power_on(ts); 1426 if (error) 1427 return error; 1428 error = mip4_query_device(ts); 1429 mip4_power_off(ts); 1430 if (error) 1431 return error; 1432 1433 input->name = "MELFAS MIP4 Touchscreen"; 1434 input->phys = ts->phys; 1435 1436 input->id.bustype = BUS_I2C; 1437 input->id.vendor = 0x13c5; 1438 1439 input->open = mip4_input_open; 1440 input->close = mip4_input_close; 1441 1442 input_set_drvdata(input, ts); 1443 1444 input->keycode = ts->key_code; 1445 input->keycodesize = sizeof(*ts->key_code); 1446 input->keycodemax = ts->key_num; 1447 1448 input_set_abs_params(input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0); 1449 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0); 1450 input_set_abs_params(input, ABS_MT_PRESSURE, 1451 MIP4_PRESSURE_MIN, MIP4_PRESSURE_MAX, 0, 0); 1452 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 1453 MIP4_TOUCH_MAJOR_MIN, MIP4_TOUCH_MAJOR_MAX, 0, 0); 1454 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 1455 MIP4_TOUCH_MINOR_MIN, MIP4_TOUCH_MINOR_MAX, 0, 0); 1456 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->ppm_x); 1457 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->ppm_y); 1458 1459 error = input_mt_init_slots(input, MIP4_MAX_FINGERS, INPUT_MT_DIRECT); 1460 if (error) 1461 return error; 1462 1463 i2c_set_clientdata(client, ts); 1464 1465 error = devm_request_threaded_irq(&client->dev, client->irq, 1466 NULL, mip4_interrupt, 1467 IRQF_ONESHOT, MIP4_DEVICE_NAME, ts); 1468 if (error) { 1469 dev_err(&client->dev, 1470 "Failed to request interrupt %d: %d\n", 1471 client->irq, error); 1472 return error; 1473 } 1474 1475 disable_irq(client->irq); 1476 1477 error = input_register_device(input); 1478 if (error) { 1479 dev_err(&client->dev, 1480 "Failed to register input device: %d\n", error); 1481 return error; 1482 } 1483 1484 error = sysfs_create_group(&client->dev.kobj, &mip4_attr_group); 1485 if (error) { 1486 dev_err(&client->dev, 1487 "Failed to create sysfs attribute group: %d\n", error); 1488 return error; 1489 } 1490 1491 error = devm_add_action(&client->dev, mip4_sysfs_remove, ts); 1492 if (error) { 1493 mip4_sysfs_remove(ts); 1494 dev_err(&client->dev, 1495 "Failed to install sysfs remoce action: %d\n", error); 1496 return error; 1497 } 1498 1499 return 0; 1500 } 1501 1502 static int __maybe_unused mip4_suspend(struct device *dev) 1503 { 1504 struct i2c_client *client = to_i2c_client(dev); 1505 struct mip4_ts *ts = i2c_get_clientdata(client); 1506 struct input_dev *input = ts->input; 1507 1508 mutex_lock(&input->mutex); 1509 1510 if (device_may_wakeup(dev)) 1511 ts->wake_irq_enabled = enable_irq_wake(client->irq) == 0; 1512 else if (input->users) 1513 mip4_disable(ts); 1514 1515 mutex_unlock(&input->mutex); 1516 1517 return 0; 1518 } 1519 1520 static int __maybe_unused mip4_resume(struct device *dev) 1521 { 1522 struct i2c_client *client = to_i2c_client(dev); 1523 struct mip4_ts *ts = i2c_get_clientdata(client); 1524 struct input_dev *input = ts->input; 1525 1526 mutex_lock(&input->mutex); 1527 1528 if (ts->wake_irq_enabled) 1529 disable_irq_wake(client->irq); 1530 else if (input->users) 1531 mip4_enable(ts); 1532 1533 mutex_unlock(&input->mutex); 1534 1535 return 0; 1536 } 1537 1538 static SIMPLE_DEV_PM_OPS(mip4_pm_ops, mip4_suspend, mip4_resume); 1539 1540 #ifdef CONFIG_OF 1541 static const struct of_device_id mip4_of_match[] = { 1542 { .compatible = "melfas,"MIP4_DEVICE_NAME, }, 1543 { }, 1544 }; 1545 MODULE_DEVICE_TABLE(of, mip4_of_match); 1546 #endif 1547 1548 #ifdef CONFIG_ACPI 1549 static const struct acpi_device_id mip4_acpi_match[] = { 1550 { "MLFS0000", 0}, 1551 { }, 1552 }; 1553 MODULE_DEVICE_TABLE(acpi, mip4_acpi_match); 1554 #endif 1555 1556 static const struct i2c_device_id mip4_i2c_ids[] = { 1557 { MIP4_DEVICE_NAME, 0 }, 1558 { }, 1559 }; 1560 MODULE_DEVICE_TABLE(i2c, mip4_i2c_ids); 1561 1562 static struct i2c_driver mip4_driver = { 1563 .id_table = mip4_i2c_ids, 1564 .probe = mip4_probe, 1565 .driver = { 1566 .name = MIP4_DEVICE_NAME, 1567 .of_match_table = of_match_ptr(mip4_of_match), 1568 .acpi_match_table = ACPI_PTR(mip4_acpi_match), 1569 .pm = &mip4_pm_ops, 1570 }, 1571 }; 1572 module_i2c_driver(mip4_driver); 1573 1574 MODULE_DESCRIPTION("MELFAS MIP4 Touchscreen"); 1575 MODULE_VERSION("2016.09.28"); 1576 MODULE_AUTHOR("Sangwon Jee <jeesw@melfas.com>"); 1577 MODULE_LICENSE("GPL"); 1578