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