1 // SPDX-License-Identifier: GPL-2.0 2 // STMicroelectronics FTS Touchscreen device driver 3 // 4 // Copyright (c) 2017 Samsung Electronics Co., Ltd. 5 // Copyright (c) 2017 Andi Shyti <andi@etezian.org> 6 7 #include <linux/delay.h> 8 #include <linux/i2c.h> 9 #include <linux/input/mt.h> 10 #include <linux/input/touchscreen.h> 11 #include <linux/interrupt.h> 12 #include <linux/irq.h> 13 #include <linux/leds.h> 14 #include <linux/module.h> 15 #include <linux/pm_runtime.h> 16 #include <linux/regulator/consumer.h> 17 18 /* I2C commands */ 19 #define STMFTS_READ_INFO 0x80 20 #define STMFTS_READ_STATUS 0x84 21 #define STMFTS_READ_ONE_EVENT 0x85 22 #define STMFTS_READ_ALL_EVENT 0x86 23 #define STMFTS_LATEST_EVENT 0x87 24 #define STMFTS_SLEEP_IN 0x90 25 #define STMFTS_SLEEP_OUT 0x91 26 #define STMFTS_MS_MT_SENSE_OFF 0x92 27 #define STMFTS_MS_MT_SENSE_ON 0x93 28 #define STMFTS_SS_HOVER_SENSE_OFF 0x94 29 #define STMFTS_SS_HOVER_SENSE_ON 0x95 30 #define STMFTS_MS_KEY_SENSE_OFF 0x9a 31 #define STMFTS_MS_KEY_SENSE_ON 0x9b 32 #define STMFTS_SYSTEM_RESET 0xa0 33 #define STMFTS_CLEAR_EVENT_STACK 0xa1 34 #define STMFTS_FULL_FORCE_CALIBRATION 0xa2 35 #define STMFTS_MS_CX_TUNING 0xa3 36 #define STMFTS_SS_CX_TUNING 0xa4 37 38 /* events */ 39 #define STMFTS_EV_NO_EVENT 0x00 40 #define STMFTS_EV_MULTI_TOUCH_DETECTED 0x02 41 #define STMFTS_EV_MULTI_TOUCH_ENTER 0x03 42 #define STMFTS_EV_MULTI_TOUCH_LEAVE 0x04 43 #define STMFTS_EV_MULTI_TOUCH_MOTION 0x05 44 #define STMFTS_EV_HOVER_ENTER 0x07 45 #define STMFTS_EV_HOVER_LEAVE 0x08 46 #define STMFTS_EV_HOVER_MOTION 0x09 47 #define STMFTS_EV_KEY_STATUS 0x0e 48 #define STMFTS_EV_ERROR 0x0f 49 #define STMFTS_EV_CONTROLLER_READY 0x10 50 #define STMFTS_EV_SLEEP_OUT_CONTROLLER_READY 0x11 51 #define STMFTS_EV_STATUS 0x16 52 #define STMFTS_EV_DEBUG 0xdb 53 54 /* multi touch related event masks */ 55 #define STMFTS_MASK_EVENT_ID 0x0f 56 #define STMFTS_MASK_TOUCH_ID 0xf0 57 #define STMFTS_MASK_LEFT_EVENT 0x0f 58 #define STMFTS_MASK_X_MSB 0x0f 59 #define STMFTS_MASK_Y_LSB 0xf0 60 61 /* key related event masks */ 62 #define STMFTS_MASK_KEY_NO_TOUCH 0x00 63 #define STMFTS_MASK_KEY_MENU 0x01 64 #define STMFTS_MASK_KEY_BACK 0x02 65 66 #define STMFTS_EVENT_SIZE 8 67 #define STMFTS_STACK_DEPTH 32 68 #define STMFTS_DATA_MAX_SIZE (STMFTS_EVENT_SIZE * STMFTS_STACK_DEPTH) 69 #define STMFTS_MAX_FINGERS 10 70 #define STMFTS_DEV_NAME "stmfts" 71 72 enum stmfts_regulators { 73 STMFTS_REGULATOR_VDD, 74 STMFTS_REGULATOR_AVDD, 75 }; 76 77 struct stmfts_data { 78 struct i2c_client *client; 79 struct input_dev *input; 80 struct led_classdev led_cdev; 81 struct mutex mutex; 82 83 struct touchscreen_properties prop; 84 85 struct regulator_bulk_data regulators[2]; 86 87 /* 88 * Presence of ledvdd will be used also to check 89 * whether the LED is supported. 90 */ 91 struct regulator *ledvdd; 92 93 u16 chip_id; 94 u8 chip_ver; 95 u16 fw_ver; 96 u8 config_id; 97 u8 config_ver; 98 99 u8 data[STMFTS_DATA_MAX_SIZE]; 100 101 struct completion cmd_done; 102 103 bool use_key; 104 bool led_status; 105 bool hover_enabled; 106 bool running; 107 }; 108 109 static int stmfts_brightness_set(struct led_classdev *led_cdev, 110 enum led_brightness value) 111 { 112 struct stmfts_data *sdata = container_of(led_cdev, 113 struct stmfts_data, led_cdev); 114 int err; 115 116 if (value != sdata->led_status && sdata->ledvdd) { 117 if (!value) { 118 regulator_disable(sdata->ledvdd); 119 } else { 120 err = regulator_enable(sdata->ledvdd); 121 if (err) { 122 dev_warn(&sdata->client->dev, 123 "failed to enable ledvdd regulator: %d\n", 124 err); 125 return err; 126 } 127 } 128 sdata->led_status = value; 129 } 130 131 return 0; 132 } 133 134 static enum led_brightness stmfts_brightness_get(struct led_classdev *led_cdev) 135 { 136 struct stmfts_data *sdata = container_of(led_cdev, 137 struct stmfts_data, led_cdev); 138 139 return !!regulator_is_enabled(sdata->ledvdd); 140 } 141 142 /* 143 * We can't simply use i2c_smbus_read_i2c_block_data because we 144 * need to read 256 bytes, which exceeds the 255-byte SMBus block limit. 145 */ 146 static int stmfts_read_events(struct stmfts_data *sdata) 147 { 148 u8 cmd = STMFTS_READ_ALL_EVENT; 149 struct i2c_msg msgs[2] = { 150 { 151 .addr = sdata->client->addr, 152 .len = 1, 153 .buf = &cmd, 154 }, 155 { 156 .addr = sdata->client->addr, 157 .flags = I2C_M_RD, 158 .len = STMFTS_DATA_MAX_SIZE, 159 .buf = sdata->data, 160 }, 161 }; 162 int ret; 163 164 ret = i2c_transfer(sdata->client->adapter, msgs, ARRAY_SIZE(msgs)); 165 if (ret < 0) 166 return ret; 167 168 return ret == ARRAY_SIZE(msgs) ? 0 : -EIO; 169 } 170 171 static void stmfts_report_contact_event(struct stmfts_data *sdata, 172 const u8 event[]) 173 { 174 u8 slot_id = (event[0] & STMFTS_MASK_TOUCH_ID) >> 4; 175 u16 x = event[1] | ((event[2] & STMFTS_MASK_X_MSB) << 8); 176 u16 y = (event[2] >> 4) | (event[3] << 4); 177 u8 maj = event[4]; 178 u8 min = event[5]; 179 u8 orientation = event[6]; 180 u8 area = event[7]; 181 182 input_mt_slot(sdata->input, slot_id); 183 184 input_mt_report_slot_state(sdata->input, MT_TOOL_FINGER, true); 185 input_report_abs(sdata->input, ABS_MT_POSITION_X, x); 186 input_report_abs(sdata->input, ABS_MT_POSITION_Y, y); 187 input_report_abs(sdata->input, ABS_MT_TOUCH_MAJOR, maj); 188 input_report_abs(sdata->input, ABS_MT_TOUCH_MINOR, min); 189 input_report_abs(sdata->input, ABS_MT_PRESSURE, area); 190 input_report_abs(sdata->input, ABS_MT_ORIENTATION, orientation); 191 192 input_sync(sdata->input); 193 } 194 195 static void stmfts_report_contact_release(struct stmfts_data *sdata, 196 const u8 event[]) 197 { 198 u8 slot_id = (event[0] & STMFTS_MASK_TOUCH_ID) >> 4; 199 200 input_mt_slot(sdata->input, slot_id); 201 input_mt_report_slot_inactive(sdata->input); 202 203 input_sync(sdata->input); 204 } 205 206 static void stmfts_report_hover_event(struct stmfts_data *sdata, 207 const u8 event[]) 208 { 209 u16 x = (event[2] << 4) | (event[4] >> 4); 210 u16 y = (event[3] << 4) | (event[4] & STMFTS_MASK_Y_LSB); 211 u8 z = event[5]; 212 213 input_report_abs(sdata->input, ABS_X, x); 214 input_report_abs(sdata->input, ABS_Y, y); 215 input_report_abs(sdata->input, ABS_DISTANCE, z); 216 217 input_sync(sdata->input); 218 } 219 220 static void stmfts_report_key_event(struct stmfts_data *sdata, const u8 event[]) 221 { 222 switch (event[2]) { 223 case 0: 224 input_report_key(sdata->input, KEY_BACK, 0); 225 input_report_key(sdata->input, KEY_MENU, 0); 226 break; 227 228 case STMFTS_MASK_KEY_BACK: 229 input_report_key(sdata->input, KEY_BACK, 1); 230 break; 231 232 case STMFTS_MASK_KEY_MENU: 233 input_report_key(sdata->input, KEY_MENU, 1); 234 break; 235 236 default: 237 dev_warn(&sdata->client->dev, 238 "unknown key event: %#02x\n", event[2]); 239 break; 240 } 241 242 input_sync(sdata->input); 243 } 244 245 static void stmfts_parse_events(struct stmfts_data *sdata) 246 { 247 int i; 248 249 for (i = 0; i < STMFTS_STACK_DEPTH; i++) { 250 u8 *event = &sdata->data[i * STMFTS_EVENT_SIZE]; 251 252 switch (event[0]) { 253 254 case STMFTS_EV_CONTROLLER_READY: 255 case STMFTS_EV_SLEEP_OUT_CONTROLLER_READY: 256 case STMFTS_EV_STATUS: 257 complete(&sdata->cmd_done); 258 fallthrough; 259 260 case STMFTS_EV_NO_EVENT: 261 case STMFTS_EV_DEBUG: 262 return; 263 } 264 265 switch (event[0] & STMFTS_MASK_EVENT_ID) { 266 267 case STMFTS_EV_MULTI_TOUCH_ENTER: 268 case STMFTS_EV_MULTI_TOUCH_MOTION: 269 stmfts_report_contact_event(sdata, event); 270 break; 271 272 case STMFTS_EV_MULTI_TOUCH_LEAVE: 273 stmfts_report_contact_release(sdata, event); 274 break; 275 276 case STMFTS_EV_HOVER_ENTER: 277 case STMFTS_EV_HOVER_LEAVE: 278 case STMFTS_EV_HOVER_MOTION: 279 stmfts_report_hover_event(sdata, event); 280 break; 281 282 case STMFTS_EV_KEY_STATUS: 283 stmfts_report_key_event(sdata, event); 284 break; 285 286 case STMFTS_EV_ERROR: 287 dev_warn(&sdata->client->dev, 288 "error code: 0x%x%x%x%x%x%x", 289 event[6], event[5], event[4], 290 event[3], event[2], event[1]); 291 break; 292 293 default: 294 dev_err(&sdata->client->dev, 295 "unknown event %#02x\n", event[0]); 296 } 297 } 298 } 299 300 static irqreturn_t stmfts_irq_handler(int irq, void *dev) 301 { 302 struct stmfts_data *sdata = dev; 303 int err; 304 305 guard(mutex)(&sdata->mutex); 306 307 err = stmfts_read_events(sdata); 308 if (unlikely(err)) 309 dev_err(&sdata->client->dev, 310 "failed to read events: %d\n", err); 311 else 312 stmfts_parse_events(sdata); 313 314 return IRQ_HANDLED; 315 } 316 317 static int stmfts_command(struct stmfts_data *sdata, const u8 cmd) 318 { 319 int err; 320 321 reinit_completion(&sdata->cmd_done); 322 323 err = i2c_smbus_write_byte(sdata->client, cmd); 324 if (err) 325 return err; 326 327 if (!wait_for_completion_timeout(&sdata->cmd_done, 328 msecs_to_jiffies(1000))) 329 return -ETIMEDOUT; 330 331 return 0; 332 } 333 334 static int stmfts_input_open(struct input_dev *dev) 335 { 336 struct stmfts_data *sdata = input_get_drvdata(dev); 337 int err; 338 339 err = pm_runtime_resume_and_get(&sdata->client->dev); 340 if (err) 341 return err; 342 343 err = i2c_smbus_write_byte(sdata->client, STMFTS_MS_MT_SENSE_ON); 344 if (err) { 345 pm_runtime_put_sync(&sdata->client->dev); 346 return err; 347 } 348 349 scoped_guard(mutex, &sdata->mutex) { 350 sdata->running = true; 351 352 if (sdata->hover_enabled) { 353 err = i2c_smbus_write_byte(sdata->client, 354 STMFTS_SS_HOVER_SENSE_ON); 355 if (err) 356 dev_warn(&sdata->client->dev, 357 "failed to enable hover\n"); 358 } 359 } 360 361 if (sdata->use_key) { 362 err = i2c_smbus_write_byte(sdata->client, 363 STMFTS_MS_KEY_SENSE_ON); 364 if (err) 365 /* I can still use only the touch screen */ 366 dev_warn(&sdata->client->dev, 367 "failed to enable touchkey\n"); 368 } 369 370 return 0; 371 } 372 373 static void stmfts_input_close(struct input_dev *dev) 374 { 375 struct stmfts_data *sdata = input_get_drvdata(dev); 376 int err; 377 378 err = i2c_smbus_write_byte(sdata->client, STMFTS_MS_MT_SENSE_OFF); 379 if (err) 380 dev_warn(&sdata->client->dev, 381 "failed to disable touchscreen: %d\n", err); 382 383 scoped_guard(mutex, &sdata->mutex) { 384 sdata->running = false; 385 386 if (sdata->hover_enabled) { 387 err = i2c_smbus_write_byte(sdata->client, 388 STMFTS_SS_HOVER_SENSE_OFF); 389 if (err) 390 dev_warn(&sdata->client->dev, 391 "failed to disable hover: %d\n", err); 392 } 393 } 394 395 if (sdata->use_key) { 396 err = i2c_smbus_write_byte(sdata->client, 397 STMFTS_MS_KEY_SENSE_OFF); 398 if (err) 399 dev_warn(&sdata->client->dev, 400 "failed to disable touchkey: %d\n", err); 401 } 402 403 pm_runtime_put_sync(&sdata->client->dev); 404 } 405 406 static ssize_t stmfts_sysfs_chip_id(struct device *dev, 407 struct device_attribute *attr, char *buf) 408 { 409 struct stmfts_data *sdata = dev_get_drvdata(dev); 410 411 return sysfs_emit(buf, "%#x\n", sdata->chip_id); 412 } 413 414 static ssize_t stmfts_sysfs_chip_version(struct device *dev, 415 struct device_attribute *attr, char *buf) 416 { 417 struct stmfts_data *sdata = dev_get_drvdata(dev); 418 419 return sysfs_emit(buf, "%u\n", sdata->chip_ver); 420 } 421 422 static ssize_t stmfts_sysfs_fw_ver(struct device *dev, 423 struct device_attribute *attr, char *buf) 424 { 425 struct stmfts_data *sdata = dev_get_drvdata(dev); 426 427 return sysfs_emit(buf, "%u\n", sdata->fw_ver); 428 } 429 430 static ssize_t stmfts_sysfs_config_id(struct device *dev, 431 struct device_attribute *attr, char *buf) 432 { 433 struct stmfts_data *sdata = dev_get_drvdata(dev); 434 435 return sysfs_emit(buf, "%#x\n", sdata->config_id); 436 } 437 438 static ssize_t stmfts_sysfs_config_version(struct device *dev, 439 struct device_attribute *attr, char *buf) 440 { 441 struct stmfts_data *sdata = dev_get_drvdata(dev); 442 443 return sysfs_emit(buf, "%u\n", sdata->config_ver); 444 } 445 446 static ssize_t stmfts_sysfs_read_status(struct device *dev, 447 struct device_attribute *attr, char *buf) 448 { 449 struct stmfts_data *sdata = dev_get_drvdata(dev); 450 u8 status[4]; 451 int err; 452 453 err = i2c_smbus_read_i2c_block_data(sdata->client, STMFTS_READ_STATUS, 454 sizeof(status), status); 455 if (err) 456 return err; 457 458 return sysfs_emit(buf, "%#02x\n", status[0]); 459 } 460 461 static ssize_t stmfts_sysfs_hover_enable_read(struct device *dev, 462 struct device_attribute *attr, char *buf) 463 { 464 struct stmfts_data *sdata = dev_get_drvdata(dev); 465 466 return sysfs_emit(buf, "%u\n", sdata->hover_enabled); 467 } 468 469 static ssize_t stmfts_sysfs_hover_enable_write(struct device *dev, 470 struct device_attribute *attr, 471 const char *buf, size_t len) 472 { 473 struct stmfts_data *sdata = dev_get_drvdata(dev); 474 unsigned long value; 475 bool hover; 476 int err; 477 478 if (kstrtoul(buf, 0, &value)) 479 return -EINVAL; 480 481 hover = !!value; 482 483 guard(mutex)(&sdata->mutex); 484 485 if (hover != sdata->hover_enabled) { 486 if (sdata->running) { 487 err = i2c_smbus_write_byte(sdata->client, 488 value ? STMFTS_SS_HOVER_SENSE_ON : 489 STMFTS_SS_HOVER_SENSE_OFF); 490 if (err) 491 return err; 492 } 493 494 sdata->hover_enabled = hover; 495 } 496 497 return len; 498 } 499 500 static DEVICE_ATTR(chip_id, 0444, stmfts_sysfs_chip_id, NULL); 501 static DEVICE_ATTR(chip_version, 0444, stmfts_sysfs_chip_version, NULL); 502 static DEVICE_ATTR(fw_ver, 0444, stmfts_sysfs_fw_ver, NULL); 503 static DEVICE_ATTR(config_id, 0444, stmfts_sysfs_config_id, NULL); 504 static DEVICE_ATTR(config_version, 0444, stmfts_sysfs_config_version, NULL); 505 static DEVICE_ATTR(status, 0444, stmfts_sysfs_read_status, NULL); 506 static DEVICE_ATTR(hover_enable, 0644, stmfts_sysfs_hover_enable_read, 507 stmfts_sysfs_hover_enable_write); 508 509 static struct attribute *stmfts_sysfs_attrs[] = { 510 &dev_attr_chip_id.attr, 511 &dev_attr_chip_version.attr, 512 &dev_attr_fw_ver.attr, 513 &dev_attr_config_id.attr, 514 &dev_attr_config_version.attr, 515 &dev_attr_status.attr, 516 &dev_attr_hover_enable.attr, 517 NULL 518 }; 519 ATTRIBUTE_GROUPS(stmfts_sysfs); 520 521 static int stmfts_power_on(struct stmfts_data *sdata) 522 { 523 int err; 524 u8 reg[8]; 525 526 err = regulator_bulk_enable(ARRAY_SIZE(sdata->regulators), 527 sdata->regulators); 528 if (err) 529 return err; 530 531 /* 532 * The datasheet does not specify the power on time, but considering 533 * that the reset time is < 10ms, I sleep 20ms to be sure 534 */ 535 msleep(20); 536 537 err = i2c_smbus_read_i2c_block_data(sdata->client, STMFTS_READ_INFO, 538 sizeof(reg), reg); 539 if (err < 0) 540 return err; 541 if (err != sizeof(reg)) 542 return -EIO; 543 544 sdata->chip_id = be16_to_cpup((__be16 *)®[6]); 545 sdata->chip_ver = reg[0]; 546 sdata->fw_ver = be16_to_cpup((__be16 *)®[2]); 547 sdata->config_id = reg[4]; 548 sdata->config_ver = reg[5]; 549 550 enable_irq(sdata->client->irq); 551 552 msleep(50); 553 554 err = stmfts_command(sdata, STMFTS_SYSTEM_RESET); 555 if (err) 556 return err; 557 558 err = stmfts_command(sdata, STMFTS_SLEEP_OUT); 559 if (err) 560 return err; 561 562 /* optional tuning */ 563 err = stmfts_command(sdata, STMFTS_MS_CX_TUNING); 564 if (err) 565 dev_warn(&sdata->client->dev, 566 "failed to perform mutual auto tune: %d\n", err); 567 568 /* optional tuning */ 569 err = stmfts_command(sdata, STMFTS_SS_CX_TUNING); 570 if (err) 571 dev_warn(&sdata->client->dev, 572 "failed to perform self auto tune: %d\n", err); 573 574 err = stmfts_command(sdata, STMFTS_FULL_FORCE_CALIBRATION); 575 if (err) 576 return err; 577 578 /* 579 * At this point no one is using the touchscreen 580 * and I don't really care about the return value 581 */ 582 (void) i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_IN); 583 584 return 0; 585 } 586 587 static void stmfts_power_off(void *data) 588 { 589 struct stmfts_data *sdata = data; 590 591 disable_irq(sdata->client->irq); 592 regulator_bulk_disable(ARRAY_SIZE(sdata->regulators), 593 sdata->regulators); 594 } 595 596 static int stmfts_enable_led(struct stmfts_data *sdata) 597 { 598 int err; 599 600 /* get the regulator for powering the leds on */ 601 sdata->ledvdd = devm_regulator_get(&sdata->client->dev, "ledvdd"); 602 if (IS_ERR(sdata->ledvdd)) 603 return PTR_ERR(sdata->ledvdd); 604 605 sdata->led_cdev.name = STMFTS_DEV_NAME; 606 sdata->led_cdev.max_brightness = LED_ON; 607 sdata->led_cdev.brightness = LED_OFF; 608 sdata->led_cdev.brightness_set_blocking = stmfts_brightness_set; 609 sdata->led_cdev.brightness_get = stmfts_brightness_get; 610 611 err = devm_led_classdev_register(&sdata->client->dev, &sdata->led_cdev); 612 if (err) { 613 devm_regulator_put(sdata->ledvdd); 614 return err; 615 } 616 617 return 0; 618 } 619 620 static int stmfts_probe(struct i2c_client *client) 621 { 622 int err; 623 struct stmfts_data *sdata; 624 625 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C | 626 I2C_FUNC_SMBUS_BYTE_DATA | 627 I2C_FUNC_SMBUS_I2C_BLOCK)) 628 return -ENODEV; 629 630 sdata = devm_kzalloc(&client->dev, sizeof(*sdata), GFP_KERNEL); 631 if (!sdata) 632 return -ENOMEM; 633 634 i2c_set_clientdata(client, sdata); 635 636 sdata->client = client; 637 mutex_init(&sdata->mutex); 638 init_completion(&sdata->cmd_done); 639 640 sdata->regulators[STMFTS_REGULATOR_VDD].supply = "vdd"; 641 sdata->regulators[STMFTS_REGULATOR_AVDD].supply = "avdd"; 642 err = devm_regulator_bulk_get(&client->dev, 643 ARRAY_SIZE(sdata->regulators), 644 sdata->regulators); 645 if (err) 646 return err; 647 648 sdata->input = devm_input_allocate_device(&client->dev); 649 if (!sdata->input) 650 return -ENOMEM; 651 652 sdata->input->name = STMFTS_DEV_NAME; 653 sdata->input->id.bustype = BUS_I2C; 654 sdata->input->open = stmfts_input_open; 655 sdata->input->close = stmfts_input_close; 656 657 input_set_capability(sdata->input, EV_ABS, ABS_MT_POSITION_X); 658 input_set_capability(sdata->input, EV_ABS, ABS_MT_POSITION_Y); 659 touchscreen_parse_properties(sdata->input, true, &sdata->prop); 660 661 input_set_abs_params(sdata->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); 662 input_set_abs_params(sdata->input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0); 663 input_set_abs_params(sdata->input, ABS_MT_ORIENTATION, 0, 255, 0, 0); 664 input_set_abs_params(sdata->input, ABS_MT_PRESSURE, 0, 255, 0, 0); 665 input_set_abs_params(sdata->input, ABS_DISTANCE, 0, 255, 0, 0); 666 667 sdata->use_key = device_property_read_bool(&client->dev, 668 "touch-key-connected"); 669 if (sdata->use_key) { 670 input_set_capability(sdata->input, EV_KEY, KEY_MENU); 671 input_set_capability(sdata->input, EV_KEY, KEY_BACK); 672 } 673 674 err = input_mt_init_slots(sdata->input, 675 STMFTS_MAX_FINGERS, INPUT_MT_DIRECT); 676 if (err) 677 return err; 678 679 input_set_drvdata(sdata->input, sdata); 680 681 /* 682 * stmfts_power_on expects interrupt to be disabled, but 683 * at this point the device is still off and I do not trust 684 * the status of the irq line that can generate some spurious 685 * interrupts. To be on the safe side it's better to not enable 686 * the interrupts during their request. 687 */ 688 err = devm_request_threaded_irq(&client->dev, client->irq, 689 NULL, stmfts_irq_handler, 690 IRQF_ONESHOT | IRQF_NO_AUTOEN, 691 "stmfts_irq", sdata); 692 if (err) 693 return err; 694 695 dev_dbg(&client->dev, "initializing ST-Microelectronics FTS...\n"); 696 697 err = stmfts_power_on(sdata); 698 if (err) 699 return err; 700 701 err = devm_add_action_or_reset(&client->dev, stmfts_power_off, sdata); 702 if (err) 703 return err; 704 705 err = input_register_device(sdata->input); 706 if (err) 707 return err; 708 709 if (sdata->use_key) { 710 err = stmfts_enable_led(sdata); 711 if (err) { 712 /* 713 * Even if the LEDs have failed to be initialized and 714 * used in the driver, I can still use the device even 715 * without LEDs. The ledvdd regulator pointer will be 716 * used as a flag. 717 */ 718 dev_warn(&client->dev, "unable to use touchkey leds\n"); 719 sdata->ledvdd = NULL; 720 } 721 } 722 723 pm_runtime_enable(&client->dev); 724 device_enable_async_suspend(&client->dev); 725 726 return 0; 727 } 728 729 static void stmfts_remove(struct i2c_client *client) 730 { 731 pm_runtime_disable(&client->dev); 732 } 733 734 static int stmfts_runtime_suspend(struct device *dev) 735 { 736 struct stmfts_data *sdata = dev_get_drvdata(dev); 737 int ret; 738 739 ret = i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_IN); 740 if (ret) 741 dev_warn(dev, "failed to suspend device: %d\n", ret); 742 743 return ret; 744 } 745 746 static int stmfts_runtime_resume(struct device *dev) 747 { 748 struct stmfts_data *sdata = dev_get_drvdata(dev); 749 int ret; 750 751 ret = i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_OUT); 752 if (ret) 753 dev_err(dev, "failed to resume device: %d\n", ret); 754 755 return ret; 756 } 757 758 static int stmfts_suspend(struct device *dev) 759 { 760 struct stmfts_data *sdata = dev_get_drvdata(dev); 761 762 stmfts_power_off(sdata); 763 764 return 0; 765 } 766 767 static int stmfts_resume(struct device *dev) 768 { 769 struct stmfts_data *sdata = dev_get_drvdata(dev); 770 771 return stmfts_power_on(sdata); 772 } 773 774 static const struct dev_pm_ops stmfts_pm_ops = { 775 SYSTEM_SLEEP_PM_OPS(stmfts_suspend, stmfts_resume) 776 RUNTIME_PM_OPS(stmfts_runtime_suspend, stmfts_runtime_resume, NULL) 777 }; 778 779 #ifdef CONFIG_OF 780 static const struct of_device_id stmfts_of_match[] = { 781 { .compatible = "st,stmfts", }, 782 { }, 783 }; 784 MODULE_DEVICE_TABLE(of, stmfts_of_match); 785 #endif 786 787 static const struct i2c_device_id stmfts_id[] = { 788 { "stmfts" }, 789 { } 790 }; 791 MODULE_DEVICE_TABLE(i2c, stmfts_id); 792 793 static struct i2c_driver stmfts_driver = { 794 .driver = { 795 .name = STMFTS_DEV_NAME, 796 .dev_groups = stmfts_sysfs_groups, 797 .of_match_table = of_match_ptr(stmfts_of_match), 798 .pm = pm_ptr(&stmfts_pm_ops), 799 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 800 }, 801 .probe = stmfts_probe, 802 .remove = stmfts_remove, 803 .id_table = stmfts_id, 804 }; 805 806 module_i2c_driver(stmfts_driver); 807 808 MODULE_AUTHOR("Andi Shyti <andi.shyti@samsung.com>"); 809 MODULE_DESCRIPTION("STMicroelectronics FTS Touch Screen"); 810 MODULE_LICENSE("GPL v2"); 811