1 // SPDX-License-Identifier: GPL-2.0 2 // Melfas MMS114/MMS152 touchscreen device driver 3 // 4 // Copyright (c) 2012 Samsung Electronics Co., Ltd. 5 // Author: Joonyoung Shim <jy0922.shim@samsung.com> 6 7 #include <linux/module.h> 8 #include <linux/delay.h> 9 #include <linux/of.h> 10 #include <linux/of_device.h> 11 #include <linux/i2c.h> 12 #include <linux/input/mt.h> 13 #include <linux/input/touchscreen.h> 14 #include <linux/interrupt.h> 15 #include <linux/regulator/consumer.h> 16 #include <linux/slab.h> 17 18 /* Write only registers */ 19 #define MMS114_MODE_CONTROL 0x01 20 #define MMS114_OPERATION_MODE_MASK 0xE 21 #define MMS114_ACTIVE BIT(1) 22 23 #define MMS114_XY_RESOLUTION_H 0x02 24 #define MMS114_X_RESOLUTION 0x03 25 #define MMS114_Y_RESOLUTION 0x04 26 #define MMS114_CONTACT_THRESHOLD 0x05 27 #define MMS114_MOVING_THRESHOLD 0x06 28 29 /* Read only registers */ 30 #define MMS114_PACKET_SIZE 0x0F 31 #define MMS114_INFORMATION 0x10 32 #define MMS114_TSP_REV 0xF0 33 34 #define MMS152_FW_REV 0xE1 35 #define MMS152_COMPAT_GROUP 0xF2 36 37 /* Minimum delay time is 50us between stop and start signal of i2c */ 38 #define MMS114_I2C_DELAY 50 39 40 /* 200ms needs after power on */ 41 #define MMS114_POWERON_DELAY 200 42 43 /* Touchscreen absolute values */ 44 #define MMS114_MAX_AREA 0xff 45 46 #define MMS114_MAX_TOUCH 10 47 #define MMS114_PACKET_NUM 8 48 49 /* Touch type */ 50 #define MMS114_TYPE_NONE 0 51 #define MMS114_TYPE_TOUCHSCREEN 1 52 #define MMS114_TYPE_TOUCHKEY 2 53 54 enum mms_type { 55 TYPE_MMS114 = 114, 56 TYPE_MMS152 = 152, 57 TYPE_MMS345L = 345, 58 }; 59 60 struct mms114_data { 61 struct i2c_client *client; 62 struct input_dev *input_dev; 63 struct regulator *core_reg; 64 struct regulator *io_reg; 65 struct touchscreen_properties props; 66 enum mms_type type; 67 unsigned int contact_threshold; 68 unsigned int moving_threshold; 69 70 /* Use cache data for mode control register(write only) */ 71 u8 cache_mode_control; 72 }; 73 74 struct mms114_touch { 75 u8 id:4, reserved_bit4:1, type:2, pressed:1; 76 u8 x_hi:4, y_hi:4; 77 u8 x_lo; 78 u8 y_lo; 79 u8 width; 80 u8 strength; 81 u8 reserved[2]; 82 } __packed; 83 84 static int __mms114_read_reg(struct mms114_data *data, unsigned int reg, 85 unsigned int len, u8 *val) 86 { 87 struct i2c_client *client = data->client; 88 struct i2c_msg xfer[2]; 89 u8 buf = reg & 0xff; 90 int error; 91 92 if (reg <= MMS114_MODE_CONTROL && reg + len > MMS114_MODE_CONTROL) 93 BUG(); 94 95 /* Write register */ 96 xfer[0].addr = client->addr; 97 xfer[0].flags = client->flags & I2C_M_TEN; 98 xfer[0].len = 1; 99 xfer[0].buf = &buf; 100 101 /* Read data */ 102 xfer[1].addr = client->addr; 103 xfer[1].flags = (client->flags & I2C_M_TEN) | I2C_M_RD; 104 xfer[1].len = len; 105 xfer[1].buf = val; 106 107 error = i2c_transfer(client->adapter, xfer, 2); 108 if (error != 2) { 109 dev_err(&client->dev, 110 "%s: i2c transfer failed (%d)\n", __func__, error); 111 return error < 0 ? error : -EIO; 112 } 113 udelay(MMS114_I2C_DELAY); 114 115 return 0; 116 } 117 118 static int mms114_read_reg(struct mms114_data *data, unsigned int reg) 119 { 120 u8 val; 121 int error; 122 123 if (reg == MMS114_MODE_CONTROL) 124 return data->cache_mode_control; 125 126 error = __mms114_read_reg(data, reg, 1, &val); 127 return error < 0 ? error : val; 128 } 129 130 static int mms114_write_reg(struct mms114_data *data, unsigned int reg, 131 unsigned int val) 132 { 133 struct i2c_client *client = data->client; 134 u8 buf[2]; 135 int error; 136 137 buf[0] = reg & 0xff; 138 buf[1] = val & 0xff; 139 140 error = i2c_master_send(client, buf, 2); 141 if (error != 2) { 142 dev_err(&client->dev, 143 "%s: i2c send failed (%d)\n", __func__, error); 144 return error < 0 ? error : -EIO; 145 } 146 udelay(MMS114_I2C_DELAY); 147 148 if (reg == MMS114_MODE_CONTROL) 149 data->cache_mode_control = val; 150 151 return 0; 152 } 153 154 static void mms114_process_mt(struct mms114_data *data, struct mms114_touch *touch) 155 { 156 struct i2c_client *client = data->client; 157 struct input_dev *input_dev = data->input_dev; 158 unsigned int id; 159 unsigned int x; 160 unsigned int y; 161 162 if (touch->id > MMS114_MAX_TOUCH) { 163 dev_err(&client->dev, "Wrong touch id (%d)\n", touch->id); 164 return; 165 } 166 167 if (touch->type != MMS114_TYPE_TOUCHSCREEN) { 168 dev_err(&client->dev, "Wrong touch type (%d)\n", touch->type); 169 return; 170 } 171 172 id = touch->id - 1; 173 x = touch->x_lo | touch->x_hi << 8; 174 y = touch->y_lo | touch->y_hi << 8; 175 176 dev_dbg(&client->dev, 177 "id: %d, type: %d, pressed: %d, x: %d, y: %d, width: %d, strength: %d\n", 178 id, touch->type, touch->pressed, 179 x, y, touch->width, touch->strength); 180 181 input_mt_slot(input_dev, id); 182 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, touch->pressed); 183 184 if (touch->pressed) { 185 touchscreen_report_pos(input_dev, &data->props, x, y, true); 186 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, touch->width); 187 input_report_abs(input_dev, ABS_MT_PRESSURE, touch->strength); 188 } 189 } 190 191 static irqreturn_t mms114_interrupt(int irq, void *dev_id) 192 { 193 struct mms114_data *data = dev_id; 194 struct input_dev *input_dev = data->input_dev; 195 struct mms114_touch touch[MMS114_MAX_TOUCH]; 196 int packet_size; 197 int touch_size; 198 int index; 199 int error; 200 201 mutex_lock(&input_dev->mutex); 202 if (!input_dev->users) { 203 mutex_unlock(&input_dev->mutex); 204 goto out; 205 } 206 mutex_unlock(&input_dev->mutex); 207 208 packet_size = mms114_read_reg(data, MMS114_PACKET_SIZE); 209 if (packet_size <= 0) 210 goto out; 211 212 touch_size = packet_size / MMS114_PACKET_NUM; 213 214 error = __mms114_read_reg(data, MMS114_INFORMATION, packet_size, 215 (u8 *)touch); 216 if (error < 0) 217 goto out; 218 219 for (index = 0; index < touch_size; index++) 220 mms114_process_mt(data, touch + index); 221 222 input_mt_report_pointer_emulation(data->input_dev, true); 223 input_sync(data->input_dev); 224 225 out: 226 return IRQ_HANDLED; 227 } 228 229 static int mms114_set_active(struct mms114_data *data, bool active) 230 { 231 int val; 232 233 val = mms114_read_reg(data, MMS114_MODE_CONTROL); 234 if (val < 0) 235 return val; 236 237 val &= ~MMS114_OPERATION_MODE_MASK; 238 239 /* If active is false, sleep mode */ 240 if (active) 241 val |= MMS114_ACTIVE; 242 243 return mms114_write_reg(data, MMS114_MODE_CONTROL, val); 244 } 245 246 static int mms114_get_version(struct mms114_data *data) 247 { 248 struct device *dev = &data->client->dev; 249 u8 buf[6]; 250 int group; 251 int error; 252 253 switch (data->type) { 254 case TYPE_MMS345L: 255 error = __mms114_read_reg(data, MMS152_FW_REV, 3, buf); 256 if (error) 257 return error; 258 259 dev_info(dev, "TSP FW Rev: bootloader 0x%x / core 0x%x / config 0x%x\n", 260 buf[0], buf[1], buf[2]); 261 break; 262 263 case TYPE_MMS152: 264 error = __mms114_read_reg(data, MMS152_FW_REV, 3, buf); 265 if (error) 266 return error; 267 268 group = i2c_smbus_read_byte_data(data->client, 269 MMS152_COMPAT_GROUP); 270 if (group < 0) 271 return group; 272 273 dev_info(dev, "TSP FW Rev: bootloader 0x%x / core 0x%x / config 0x%x, Compat group: %c\n", 274 buf[0], buf[1], buf[2], group); 275 break; 276 277 case TYPE_MMS114: 278 error = __mms114_read_reg(data, MMS114_TSP_REV, 6, buf); 279 if (error) 280 return error; 281 282 dev_info(dev, "TSP Rev: 0x%x, HW Rev: 0x%x, Firmware Ver: 0x%x\n", 283 buf[0], buf[1], buf[3]); 284 break; 285 } 286 287 return 0; 288 } 289 290 static int mms114_setup_regs(struct mms114_data *data) 291 { 292 const struct touchscreen_properties *props = &data->props; 293 int val; 294 int error; 295 296 error = mms114_get_version(data); 297 if (error < 0) 298 return error; 299 300 /* Only MMS114 has configuration and power on registers */ 301 if (data->type != TYPE_MMS114) 302 return 0; 303 304 error = mms114_set_active(data, true); 305 if (error < 0) 306 return error; 307 308 val = (props->max_x >> 8) & 0xf; 309 val |= ((props->max_y >> 8) & 0xf) << 4; 310 error = mms114_write_reg(data, MMS114_XY_RESOLUTION_H, val); 311 if (error < 0) 312 return error; 313 314 val = props->max_x & 0xff; 315 error = mms114_write_reg(data, MMS114_X_RESOLUTION, val); 316 if (error < 0) 317 return error; 318 319 val = props->max_x & 0xff; 320 error = mms114_write_reg(data, MMS114_Y_RESOLUTION, val); 321 if (error < 0) 322 return error; 323 324 if (data->contact_threshold) { 325 error = mms114_write_reg(data, MMS114_CONTACT_THRESHOLD, 326 data->contact_threshold); 327 if (error < 0) 328 return error; 329 } 330 331 if (data->moving_threshold) { 332 error = mms114_write_reg(data, MMS114_MOVING_THRESHOLD, 333 data->moving_threshold); 334 if (error < 0) 335 return error; 336 } 337 338 return 0; 339 } 340 341 static int mms114_start(struct mms114_data *data) 342 { 343 struct i2c_client *client = data->client; 344 int error; 345 346 error = regulator_enable(data->core_reg); 347 if (error) { 348 dev_err(&client->dev, "Failed to enable avdd: %d\n", error); 349 return error; 350 } 351 352 error = regulator_enable(data->io_reg); 353 if (error) { 354 dev_err(&client->dev, "Failed to enable vdd: %d\n", error); 355 regulator_disable(data->core_reg); 356 return error; 357 } 358 359 msleep(MMS114_POWERON_DELAY); 360 361 error = mms114_setup_regs(data); 362 if (error < 0) { 363 regulator_disable(data->io_reg); 364 regulator_disable(data->core_reg); 365 return error; 366 } 367 368 enable_irq(client->irq); 369 370 return 0; 371 } 372 373 static void mms114_stop(struct mms114_data *data) 374 { 375 struct i2c_client *client = data->client; 376 int error; 377 378 disable_irq(client->irq); 379 380 error = regulator_disable(data->io_reg); 381 if (error) 382 dev_warn(&client->dev, "Failed to disable vdd: %d\n", error); 383 384 error = regulator_disable(data->core_reg); 385 if (error) 386 dev_warn(&client->dev, "Failed to disable avdd: %d\n", error); 387 } 388 389 static int mms114_input_open(struct input_dev *dev) 390 { 391 struct mms114_data *data = input_get_drvdata(dev); 392 393 return mms114_start(data); 394 } 395 396 static void mms114_input_close(struct input_dev *dev) 397 { 398 struct mms114_data *data = input_get_drvdata(dev); 399 400 mms114_stop(data); 401 } 402 403 static int mms114_parse_legacy_bindings(struct mms114_data *data) 404 { 405 struct device *dev = &data->client->dev; 406 struct touchscreen_properties *props = &data->props; 407 408 if (device_property_read_u32(dev, "x-size", &props->max_x)) { 409 dev_dbg(dev, "failed to get legacy x-size property\n"); 410 return -EINVAL; 411 } 412 413 if (device_property_read_u32(dev, "y-size", &props->max_y)) { 414 dev_dbg(dev, "failed to get legacy y-size property\n"); 415 return -EINVAL; 416 } 417 418 device_property_read_u32(dev, "contact-threshold", 419 &data->contact_threshold); 420 device_property_read_u32(dev, "moving-threshold", 421 &data->moving_threshold); 422 423 if (device_property_read_bool(dev, "x-invert")) 424 props->invert_x = true; 425 if (device_property_read_bool(dev, "y-invert")) 426 props->invert_y = true; 427 428 props->swap_x_y = false; 429 430 return 0; 431 } 432 433 static int mms114_probe(struct i2c_client *client, 434 const struct i2c_device_id *id) 435 { 436 struct mms114_data *data; 437 struct input_dev *input_dev; 438 const void *match_data; 439 int error; 440 441 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 442 dev_err(&client->dev, "Not supported I2C adapter\n"); 443 return -ENODEV; 444 } 445 446 data = devm_kzalloc(&client->dev, sizeof(struct mms114_data), 447 GFP_KERNEL); 448 input_dev = devm_input_allocate_device(&client->dev); 449 if (!data || !input_dev) { 450 dev_err(&client->dev, "Failed to allocate memory\n"); 451 return -ENOMEM; 452 } 453 454 data->client = client; 455 data->input_dev = input_dev; 456 457 match_data = device_get_match_data(&client->dev); 458 if (!match_data) 459 return -EINVAL; 460 461 data->type = (enum mms_type)match_data; 462 463 input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X); 464 input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y); 465 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 255, 0, 0); 466 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 467 0, MMS114_MAX_AREA, 0, 0); 468 469 touchscreen_parse_properties(input_dev, true, &data->props); 470 if (!data->props.max_x || !data->props.max_y) { 471 dev_dbg(&client->dev, 472 "missing X/Y size properties, trying legacy bindings\n"); 473 error = mms114_parse_legacy_bindings(data); 474 if (error) 475 return error; 476 477 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 478 0, data->props.max_x, 0, 0); 479 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 480 0, data->props.max_y, 0, 0); 481 } 482 483 if (data->type == TYPE_MMS114) { 484 /* 485 * The firmware handles movement and pressure fuzz, so 486 * don't duplicate that in software. 487 */ 488 data->moving_threshold = input_abs_get_fuzz(input_dev, 489 ABS_MT_POSITION_X); 490 data->contact_threshold = input_abs_get_fuzz(input_dev, 491 ABS_MT_PRESSURE); 492 input_abs_set_fuzz(input_dev, ABS_MT_POSITION_X, 0); 493 input_abs_set_fuzz(input_dev, ABS_MT_POSITION_Y, 0); 494 input_abs_set_fuzz(input_dev, ABS_MT_PRESSURE, 0); 495 } 496 497 input_dev->name = devm_kasprintf(&client->dev, GFP_KERNEL, 498 "MELFAS MMS%d Touchscreen", 499 data->type); 500 if (!input_dev->name) 501 return -ENOMEM; 502 503 input_dev->id.bustype = BUS_I2C; 504 input_dev->dev.parent = &client->dev; 505 input_dev->open = mms114_input_open; 506 input_dev->close = mms114_input_close; 507 508 error = input_mt_init_slots(input_dev, MMS114_MAX_TOUCH, 509 INPUT_MT_DIRECT); 510 if (error) 511 return error; 512 513 input_set_drvdata(input_dev, data); 514 i2c_set_clientdata(client, data); 515 516 data->core_reg = devm_regulator_get(&client->dev, "avdd"); 517 if (IS_ERR(data->core_reg)) { 518 error = PTR_ERR(data->core_reg); 519 dev_err(&client->dev, 520 "Unable to get the Core regulator (%d)\n", error); 521 return error; 522 } 523 524 data->io_reg = devm_regulator_get(&client->dev, "vdd"); 525 if (IS_ERR(data->io_reg)) { 526 error = PTR_ERR(data->io_reg); 527 dev_err(&client->dev, 528 "Unable to get the IO regulator (%d)\n", error); 529 return error; 530 } 531 532 error = devm_request_threaded_irq(&client->dev, client->irq, 533 NULL, mms114_interrupt, IRQF_ONESHOT, 534 dev_name(&client->dev), data); 535 if (error) { 536 dev_err(&client->dev, "Failed to register interrupt\n"); 537 return error; 538 } 539 disable_irq(client->irq); 540 541 error = input_register_device(data->input_dev); 542 if (error) { 543 dev_err(&client->dev, "Failed to register input device\n"); 544 return error; 545 } 546 547 return 0; 548 } 549 550 static int __maybe_unused mms114_suspend(struct device *dev) 551 { 552 struct i2c_client *client = to_i2c_client(dev); 553 struct mms114_data *data = i2c_get_clientdata(client); 554 struct input_dev *input_dev = data->input_dev; 555 int id; 556 557 /* Release all touch */ 558 for (id = 0; id < MMS114_MAX_TOUCH; id++) { 559 input_mt_slot(input_dev, id); 560 input_mt_report_slot_inactive(input_dev); 561 } 562 563 input_mt_report_pointer_emulation(input_dev, true); 564 input_sync(input_dev); 565 566 mutex_lock(&input_dev->mutex); 567 if (input_dev->users) 568 mms114_stop(data); 569 mutex_unlock(&input_dev->mutex); 570 571 return 0; 572 } 573 574 static int __maybe_unused mms114_resume(struct device *dev) 575 { 576 struct i2c_client *client = to_i2c_client(dev); 577 struct mms114_data *data = i2c_get_clientdata(client); 578 struct input_dev *input_dev = data->input_dev; 579 int error; 580 581 mutex_lock(&input_dev->mutex); 582 if (input_dev->users) { 583 error = mms114_start(data); 584 if (error < 0) { 585 mutex_unlock(&input_dev->mutex); 586 return error; 587 } 588 } 589 mutex_unlock(&input_dev->mutex); 590 591 return 0; 592 } 593 594 static SIMPLE_DEV_PM_OPS(mms114_pm_ops, mms114_suspend, mms114_resume); 595 596 static const struct i2c_device_id mms114_id[] = { 597 { "mms114", 0 }, 598 { } 599 }; 600 MODULE_DEVICE_TABLE(i2c, mms114_id); 601 602 #ifdef CONFIG_OF 603 static const struct of_device_id mms114_dt_match[] = { 604 { 605 .compatible = "melfas,mms114", 606 .data = (void *)TYPE_MMS114, 607 }, { 608 .compatible = "melfas,mms152", 609 .data = (void *)TYPE_MMS152, 610 }, { 611 .compatible = "melfas,mms345l", 612 .data = (void *)TYPE_MMS345L, 613 }, 614 { } 615 }; 616 MODULE_DEVICE_TABLE(of, mms114_dt_match); 617 #endif 618 619 static struct i2c_driver mms114_driver = { 620 .driver = { 621 .name = "mms114", 622 .pm = &mms114_pm_ops, 623 .of_match_table = of_match_ptr(mms114_dt_match), 624 }, 625 .probe = mms114_probe, 626 .id_table = mms114_id, 627 }; 628 629 module_i2c_driver(mms114_driver); 630 631 /* Module information */ 632 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>"); 633 MODULE_DESCRIPTION("MELFAS mms114 Touchscreen driver"); 634 MODULE_LICENSE("GPL v2"); 635