1 // SPDX-License-Identifier: GPL-2.0 2 // Samsung S6SY761 Touchscreen device driver 3 // 4 // Copyright (c) 2017 Samsung Electronics Co., Ltd. 5 // Copyright (c) 2017 Andi Shyti <andi@etezian.org> 6 7 #include <asm/unaligned.h> 8 #include <linux/delay.h> 9 #include <linux/i2c.h> 10 #include <linux/input/mt.h> 11 #include <linux/input/touchscreen.h> 12 #include <linux/interrupt.h> 13 #include <linux/irq.h> 14 #include <linux/module.h> 15 #include <linux/pm_runtime.h> 16 #include <linux/regulator/consumer.h> 17 18 /* commands */ 19 #define S6SY761_SENSE_ON 0x10 20 #define S6SY761_SENSE_OFF 0x11 21 #define S6SY761_TOUCH_FUNCTION 0x30 /* R/W for get/set */ 22 #define S6SY761_FIRMWARE_INTEGRITY 0x21 23 #define S6SY761_PANEL_INFO 0x23 24 #define S6SY761_DEVICE_ID 0x52 25 #define S6SY761_BOOT_STATUS 0x55 26 #define S6SY761_READ_ONE_EVENT 0x60 27 #define S6SY761_READ_ALL_EVENT 0x61 28 #define S6SY761_CLEAR_EVENT_STACK 0x62 29 #define S6SY761_APPLICATION_MODE 0xe4 30 31 /* events */ 32 #define S6SY761_EVENT_INFO 0x02 33 #define S6SY761_EVENT_VENDOR_INFO 0x07 34 35 /* info */ 36 #define S6SY761_INFO_BOOT_COMPLETE 0x00 37 38 /* firmware status */ 39 #define S6SY761_FW_OK 0x80 40 41 /* 42 * the functionalities are put as a reference 43 * as in the device I am using none of them 44 * works therefore not used in this driver yet. 45 */ 46 /* touchscreen functionalities */ 47 #define S6SY761_MASK_TOUCH BIT(0) 48 #define S6SY761_MASK_HOVER BIT(1) 49 #define S6SY761_MASK_COVER BIT(2) 50 #define S6SY761_MASK_GLOVE BIT(3) 51 #define S6SY761_MASK_STYLUS BIT(4) 52 #define S6SY761_MASK_PALM BIT(5) 53 #define S6SY761_MASK_WET BIT(6) 54 #define S6SY761_MASK_PROXIMITY BIT(7) 55 56 /* boot status (BS) */ 57 #define S6SY761_BS_BOOT_LOADER 0x10 58 #define S6SY761_BS_APPLICATION 0x20 59 60 /* event id */ 61 #define S6SY761_EVENT_ID_COORDINATE 0x00 62 #define S6SY761_EVENT_ID_STATUS 0x01 63 64 /* event register masks */ 65 #define S6SY761_MASK_TOUCH_STATE 0xc0 /* byte 0 */ 66 #define S6SY761_MASK_TID 0x3c 67 #define S6SY761_MASK_EID 0x03 68 #define S6SY761_MASK_X 0xf0 /* byte 3 */ 69 #define S6SY761_MASK_Y 0x0f 70 #define S6SY761_MASK_Z 0x3f /* byte 6 */ 71 #define S6SY761_MASK_LEFT_EVENTS 0x3f /* byte 7 */ 72 #define S6SY761_MASK_TOUCH_TYPE 0xc0 /* MSB in byte 6, LSB in byte 7 */ 73 74 /* event touch state values */ 75 #define S6SY761_TS_NONE 0x00 76 #define S6SY761_TS_PRESS 0x01 77 #define S6SY761_TS_MOVE 0x02 78 #define S6SY761_TS_RELEASE 0x03 79 80 /* application modes */ 81 #define S6SY761_APP_NORMAL 0x0 82 #define S6SY761_APP_LOW_POWER 0x1 83 #define S6SY761_APP_TEST 0x2 84 #define S6SY761_APP_FLASH 0x3 85 #define S6SY761_APP_SLEEP 0x4 86 87 #define S6SY761_EVENT_SIZE 8 88 #define S6SY761_EVENT_COUNT 32 89 #define S6SY761_DEVID_SIZE 3 90 #define S6SY761_PANEL_ID_SIZE 11 91 #define S6SY761_TS_STATUS_SIZE 5 92 #define S6SY761_MAX_FINGERS 10 93 94 #define S6SY761_DEV_NAME "s6sy761" 95 96 enum s6sy761_regulators { 97 S6SY761_REGULATOR_VDD, 98 S6SY761_REGULATOR_AVDD, 99 }; 100 101 struct s6sy761_data { 102 struct i2c_client *client; 103 struct regulator_bulk_data regulators[2]; 104 struct input_dev *input; 105 struct touchscreen_properties prop; 106 107 u8 data[S6SY761_EVENT_SIZE * S6SY761_EVENT_COUNT]; 108 109 u16 devid; 110 u8 tx_channel; 111 }; 112 113 /* 114 * We can't simply use i2c_smbus_read_i2c_block_data because we 115 * need to read more than 255 bytes 116 */ 117 static int s6sy761_read_events(struct s6sy761_data *sdata, u16 n_events) 118 { 119 u8 cmd = S6SY761_READ_ALL_EVENT; 120 struct i2c_msg msgs[2] = { 121 { 122 .addr = sdata->client->addr, 123 .len = 1, 124 .buf = &cmd, 125 }, 126 { 127 .addr = sdata->client->addr, 128 .flags = I2C_M_RD, 129 .len = (n_events * S6SY761_EVENT_SIZE), 130 .buf = sdata->data + S6SY761_EVENT_SIZE, 131 }, 132 }; 133 int ret; 134 135 ret = i2c_transfer(sdata->client->adapter, msgs, ARRAY_SIZE(msgs)); 136 if (ret < 0) 137 return ret; 138 139 return ret == ARRAY_SIZE(msgs) ? 0 : -EIO; 140 } 141 142 static void s6sy761_report_coordinates(struct s6sy761_data *sdata, 143 u8 *event, u8 tid) 144 { 145 u8 major = event[4]; 146 u8 minor = event[5]; 147 u8 z = event[6] & S6SY761_MASK_Z; 148 u16 x = (event[1] << 3) | ((event[3] & S6SY761_MASK_X) >> 4); 149 u16 y = (event[2] << 3) | (event[3] & S6SY761_MASK_Y); 150 151 input_mt_slot(sdata->input, tid); 152 153 input_mt_report_slot_state(sdata->input, MT_TOOL_FINGER, true); 154 input_report_abs(sdata->input, ABS_MT_POSITION_X, x); 155 input_report_abs(sdata->input, ABS_MT_POSITION_Y, y); 156 input_report_abs(sdata->input, ABS_MT_TOUCH_MAJOR, major); 157 input_report_abs(sdata->input, ABS_MT_TOUCH_MINOR, minor); 158 input_report_abs(sdata->input, ABS_MT_PRESSURE, z); 159 160 input_sync(sdata->input); 161 } 162 163 static void s6sy761_report_release(struct s6sy761_data *sdata, 164 u8 *event, u8 tid) 165 { 166 input_mt_slot(sdata->input, tid); 167 input_mt_report_slot_state(sdata->input, MT_TOOL_FINGER, false); 168 169 input_sync(sdata->input); 170 } 171 172 static void s6sy761_handle_coordinates(struct s6sy761_data *sdata, u8 *event) 173 { 174 u8 tid; 175 u8 touch_state; 176 177 if (unlikely(!(event[0] & S6SY761_MASK_TID))) 178 return; 179 180 tid = ((event[0] & S6SY761_MASK_TID) >> 2) - 1; 181 touch_state = (event[0] & S6SY761_MASK_TOUCH_STATE) >> 6; 182 183 switch (touch_state) { 184 185 case S6SY761_TS_NONE: 186 break; 187 case S6SY761_TS_RELEASE: 188 s6sy761_report_release(sdata, event, tid); 189 break; 190 case S6SY761_TS_PRESS: 191 case S6SY761_TS_MOVE: 192 s6sy761_report_coordinates(sdata, event, tid); 193 break; 194 } 195 } 196 197 static void s6sy761_handle_events(struct s6sy761_data *sdata, u8 n_events) 198 { 199 int i; 200 201 for (i = 0; i < n_events; i++) { 202 u8 *event = &sdata->data[i * S6SY761_EVENT_SIZE]; 203 u8 event_id = event[0] & S6SY761_MASK_EID; 204 205 if (!event[0]) 206 return; 207 208 switch (event_id) { 209 210 case S6SY761_EVENT_ID_COORDINATE: 211 s6sy761_handle_coordinates(sdata, event); 212 break; 213 214 case S6SY761_EVENT_ID_STATUS: 215 break; 216 217 default: 218 break; 219 } 220 } 221 } 222 223 static irqreturn_t s6sy761_irq_handler(int irq, void *dev) 224 { 225 struct s6sy761_data *sdata = dev; 226 int ret; 227 u8 n_events; 228 229 ret = i2c_smbus_read_i2c_block_data(sdata->client, 230 S6SY761_READ_ONE_EVENT, 231 S6SY761_EVENT_SIZE, 232 sdata->data); 233 if (ret < 0) { 234 dev_err(&sdata->client->dev, "failed to read events\n"); 235 return IRQ_HANDLED; 236 } 237 238 if (!sdata->data[0]) 239 return IRQ_HANDLED; 240 241 n_events = sdata->data[7] & S6SY761_MASK_LEFT_EVENTS; 242 if (unlikely(n_events > S6SY761_EVENT_COUNT - 1)) 243 return IRQ_HANDLED; 244 245 if (n_events) { 246 ret = s6sy761_read_events(sdata, n_events); 247 if (ret < 0) { 248 dev_err(&sdata->client->dev, "failed to read events\n"); 249 return IRQ_HANDLED; 250 } 251 } 252 253 s6sy761_handle_events(sdata, n_events + 1); 254 255 return IRQ_HANDLED; 256 } 257 258 static int s6sy761_input_open(struct input_dev *dev) 259 { 260 struct s6sy761_data *sdata = input_get_drvdata(dev); 261 262 return i2c_smbus_write_byte(sdata->client, S6SY761_SENSE_ON); 263 } 264 265 static void s6sy761_input_close(struct input_dev *dev) 266 { 267 struct s6sy761_data *sdata = input_get_drvdata(dev); 268 int ret; 269 270 ret = i2c_smbus_write_byte(sdata->client, S6SY761_SENSE_OFF); 271 if (ret) 272 dev_err(&sdata->client->dev, "failed to turn off sensing\n"); 273 } 274 275 static ssize_t s6sy761_sysfs_devid(struct device *dev, 276 struct device_attribute *attr, char *buf) 277 { 278 struct s6sy761_data *sdata = dev_get_drvdata(dev); 279 280 return sprintf(buf, "%#x\n", sdata->devid); 281 } 282 283 static DEVICE_ATTR(devid, 0444, s6sy761_sysfs_devid, NULL); 284 285 static struct attribute *s6sy761_sysfs_attrs[] = { 286 &dev_attr_devid.attr, 287 NULL 288 }; 289 290 static struct attribute_group s6sy761_attribute_group = { 291 .attrs = s6sy761_sysfs_attrs 292 }; 293 294 static int s6sy761_power_on(struct s6sy761_data *sdata) 295 { 296 u8 buffer[S6SY761_EVENT_SIZE]; 297 u8 event; 298 int ret; 299 300 ret = regulator_bulk_enable(ARRAY_SIZE(sdata->regulators), 301 sdata->regulators); 302 if (ret) 303 return ret; 304 305 msleep(140); 306 307 /* double check whether the touch is functional */ 308 ret = i2c_smbus_read_i2c_block_data(sdata->client, 309 S6SY761_READ_ONE_EVENT, 310 S6SY761_EVENT_SIZE, 311 buffer); 312 if (ret < 0) 313 return ret; 314 315 event = (buffer[0] >> 2) & 0xf; 316 317 if ((event != S6SY761_EVENT_INFO && 318 event != S6SY761_EVENT_VENDOR_INFO) || 319 buffer[1] != S6SY761_INFO_BOOT_COMPLETE) { 320 return -ENODEV; 321 } 322 323 ret = i2c_smbus_read_byte_data(sdata->client, S6SY761_BOOT_STATUS); 324 if (ret < 0) 325 return ret; 326 327 /* for some reasons the device might be stuck in the bootloader */ 328 if (ret != S6SY761_BS_APPLICATION) 329 return -ENODEV; 330 331 /* enable touch functionality */ 332 ret = i2c_smbus_write_word_data(sdata->client, 333 S6SY761_TOUCH_FUNCTION, 334 S6SY761_MASK_TOUCH); 335 if (ret) 336 return ret; 337 338 return 0; 339 } 340 341 static int s6sy761_hw_init(struct s6sy761_data *sdata, 342 unsigned int *max_x, unsigned int *max_y) 343 { 344 u8 buffer[S6SY761_PANEL_ID_SIZE]; /* larger read size */ 345 int ret; 346 347 ret = s6sy761_power_on(sdata); 348 if (ret) 349 return ret; 350 351 ret = i2c_smbus_read_i2c_block_data(sdata->client, 352 S6SY761_DEVICE_ID, 353 S6SY761_DEVID_SIZE, 354 buffer); 355 if (ret < 0) 356 return ret; 357 358 sdata->devid = get_unaligned_be16(buffer + 1); 359 360 ret = i2c_smbus_read_i2c_block_data(sdata->client, 361 S6SY761_PANEL_INFO, 362 S6SY761_PANEL_ID_SIZE, 363 buffer); 364 if (ret < 0) 365 return ret; 366 367 *max_x = get_unaligned_be16(buffer); 368 *max_y = get_unaligned_be16(buffer + 2); 369 370 /* if no tx channels defined, at least keep one */ 371 sdata->tx_channel = max_t(u8, buffer[8], 1); 372 373 ret = i2c_smbus_read_byte_data(sdata->client, 374 S6SY761_FIRMWARE_INTEGRITY); 375 if (ret < 0) 376 return ret; 377 else if (ret != S6SY761_FW_OK) 378 return -ENODEV; 379 380 return 0; 381 } 382 383 static void s6sy761_power_off(void *data) 384 { 385 struct s6sy761_data *sdata = data; 386 387 disable_irq(sdata->client->irq); 388 regulator_bulk_disable(ARRAY_SIZE(sdata->regulators), 389 sdata->regulators); 390 } 391 392 static int s6sy761_probe(struct i2c_client *client, 393 const struct i2c_device_id *id) 394 { 395 struct s6sy761_data *sdata; 396 unsigned int max_x, max_y; 397 int err; 398 399 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C | 400 I2C_FUNC_SMBUS_BYTE_DATA | 401 I2C_FUNC_SMBUS_I2C_BLOCK)) 402 return -ENODEV; 403 404 sdata = devm_kzalloc(&client->dev, sizeof(*sdata), GFP_KERNEL); 405 if (!sdata) 406 return -ENOMEM; 407 408 i2c_set_clientdata(client, sdata); 409 sdata->client = client; 410 411 sdata->regulators[S6SY761_REGULATOR_VDD].supply = "vdd"; 412 sdata->regulators[S6SY761_REGULATOR_AVDD].supply = "avdd"; 413 err = devm_regulator_bulk_get(&client->dev, 414 ARRAY_SIZE(sdata->regulators), 415 sdata->regulators); 416 if (err) 417 return err; 418 419 err = devm_add_action_or_reset(&client->dev, s6sy761_power_off, sdata); 420 if (err) 421 return err; 422 423 err = s6sy761_hw_init(sdata, &max_x, &max_y); 424 if (err) 425 return err; 426 427 sdata->input = devm_input_allocate_device(&client->dev); 428 if (!sdata->input) 429 return -ENOMEM; 430 431 sdata->input->name = S6SY761_DEV_NAME; 432 sdata->input->id.bustype = BUS_I2C; 433 sdata->input->open = s6sy761_input_open; 434 sdata->input->close = s6sy761_input_close; 435 436 input_set_abs_params(sdata->input, ABS_MT_POSITION_X, 0, max_x, 0, 0); 437 input_set_abs_params(sdata->input, ABS_MT_POSITION_Y, 0, max_y, 0, 0); 438 input_set_abs_params(sdata->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); 439 input_set_abs_params(sdata->input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0); 440 input_set_abs_params(sdata->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); 441 input_set_abs_params(sdata->input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0); 442 input_set_abs_params(sdata->input, ABS_MT_PRESSURE, 0, 255, 0, 0); 443 444 touchscreen_parse_properties(sdata->input, true, &sdata->prop); 445 446 if (!input_abs_get_max(sdata->input, ABS_X) || 447 !input_abs_get_max(sdata->input, ABS_Y)) { 448 dev_warn(&client->dev, "the axis have not been set\n"); 449 } 450 451 err = input_mt_init_slots(sdata->input, sdata->tx_channel, 452 INPUT_MT_DIRECT); 453 if (err) 454 return err; 455 456 input_set_drvdata(sdata->input, sdata); 457 458 err = input_register_device(sdata->input); 459 if (err) 460 return err; 461 462 err = devm_request_threaded_irq(&client->dev, client->irq, NULL, 463 s6sy761_irq_handler, 464 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 465 "s6sy761_irq", sdata); 466 if (err) 467 return err; 468 469 err = devm_device_add_group(&client->dev, &s6sy761_attribute_group); 470 if (err) 471 return err; 472 473 pm_runtime_enable(&client->dev); 474 475 return 0; 476 } 477 478 static int s6sy761_remove(struct i2c_client *client) 479 { 480 pm_runtime_disable(&client->dev); 481 482 return 0; 483 } 484 485 static int __maybe_unused s6sy761_runtime_suspend(struct device *dev) 486 { 487 struct s6sy761_data *sdata = dev_get_drvdata(dev); 488 489 return i2c_smbus_write_byte_data(sdata->client, 490 S6SY761_APPLICATION_MODE, S6SY761_APP_SLEEP); 491 } 492 493 static int __maybe_unused s6sy761_runtime_resume(struct device *dev) 494 { 495 struct s6sy761_data *sdata = dev_get_drvdata(dev); 496 497 return i2c_smbus_write_byte_data(sdata->client, 498 S6SY761_APPLICATION_MODE, S6SY761_APP_NORMAL); 499 } 500 501 static int __maybe_unused s6sy761_suspend(struct device *dev) 502 { 503 struct s6sy761_data *sdata = dev_get_drvdata(dev); 504 505 s6sy761_power_off(sdata); 506 507 return 0; 508 } 509 510 static int __maybe_unused s6sy761_resume(struct device *dev) 511 { 512 struct s6sy761_data *sdata = dev_get_drvdata(dev); 513 514 enable_irq(sdata->client->irq); 515 516 return s6sy761_power_on(sdata); 517 } 518 519 static const struct dev_pm_ops s6sy761_pm_ops = { 520 SET_SYSTEM_SLEEP_PM_OPS(s6sy761_suspend, s6sy761_resume) 521 SET_RUNTIME_PM_OPS(s6sy761_runtime_suspend, 522 s6sy761_runtime_resume, NULL) 523 }; 524 525 #ifdef CONFIG_OF 526 static const struct of_device_id s6sy761_of_match[] = { 527 { .compatible = "samsung,s6sy761", }, 528 { }, 529 }; 530 MODULE_DEVICE_TABLE(of, s6sy761_of_match); 531 #endif 532 533 static const struct i2c_device_id s6sy761_id[] = { 534 { "s6sy761", 0 }, 535 { }, 536 }; 537 MODULE_DEVICE_TABLE(i2c, s6sy761_id); 538 539 static struct i2c_driver s6sy761_driver = { 540 .driver = { 541 .name = S6SY761_DEV_NAME, 542 .of_match_table = of_match_ptr(s6sy761_of_match), 543 .pm = &s6sy761_pm_ops, 544 }, 545 .probe = s6sy761_probe, 546 .remove = s6sy761_remove, 547 .id_table = s6sy761_id, 548 }; 549 550 module_i2c_driver(s6sy761_driver); 551 552 MODULE_AUTHOR("Andi Shyti <andi.shyti@samsung.com>"); 553 MODULE_DESCRIPTION("Samsung S6SY761 Touch Screen"); 554 MODULE_LICENSE("GPL v2"); 555