1 /* 2 * Copyright (C) 2012-2013 MundoReader S.L. 3 * Author: Heiko Stuebner <heiko@sntech.de> 4 * 5 * based in parts on Nook zforce driver 6 * 7 * Copyright (C) 2010 Barnes & Noble, Inc. 8 * Author: Pieter Truter<ptruter@intrinsyc.com> 9 * 10 * This software is licensed under the terms of the GNU General Public 11 * License version 2, as published by the Free Software Foundation, and 12 * may be copied, distributed, and modified under those terms. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 */ 19 20 #include <linux/module.h> 21 #include <linux/hrtimer.h> 22 #include <linux/slab.h> 23 #include <linux/input.h> 24 #include <linux/interrupt.h> 25 #include <linux/i2c.h> 26 #include <linux/delay.h> 27 #include <linux/gpio.h> 28 #include <linux/device.h> 29 #include <linux/sysfs.h> 30 #include <linux/input/mt.h> 31 #include <linux/platform_data/zforce_ts.h> 32 #include <linux/regulator/consumer.h> 33 #include <linux/of.h> 34 #include <linux/of_gpio.h> 35 36 #define WAIT_TIMEOUT msecs_to_jiffies(1000) 37 38 #define FRAME_START 0xee 39 #define FRAME_MAXSIZE 257 40 41 /* Offsets of the different parts of the payload the controller sends */ 42 #define PAYLOAD_HEADER 0 43 #define PAYLOAD_LENGTH 1 44 #define PAYLOAD_BODY 2 45 46 /* Response offsets */ 47 #define RESPONSE_ID 0 48 #define RESPONSE_DATA 1 49 50 /* Commands */ 51 #define COMMAND_DEACTIVATE 0x00 52 #define COMMAND_INITIALIZE 0x01 53 #define COMMAND_RESOLUTION 0x02 54 #define COMMAND_SETCONFIG 0x03 55 #define COMMAND_DATAREQUEST 0x04 56 #define COMMAND_SCANFREQ 0x08 57 #define COMMAND_STATUS 0X1e 58 59 /* 60 * Responses the controller sends as a result of 61 * command requests 62 */ 63 #define RESPONSE_DEACTIVATE 0x00 64 #define RESPONSE_INITIALIZE 0x01 65 #define RESPONSE_RESOLUTION 0x02 66 #define RESPONSE_SETCONFIG 0x03 67 #define RESPONSE_SCANFREQ 0x08 68 #define RESPONSE_STATUS 0X1e 69 70 /* 71 * Notifications are sent by the touch controller without 72 * being requested by the driver and include for example 73 * touch indications 74 */ 75 #define NOTIFICATION_TOUCH 0x04 76 #define NOTIFICATION_BOOTCOMPLETE 0x07 77 #define NOTIFICATION_OVERRUN 0x25 78 #define NOTIFICATION_PROXIMITY 0x26 79 #define NOTIFICATION_INVALID_COMMAND 0xfe 80 81 #define ZFORCE_REPORT_POINTS 2 82 #define ZFORCE_MAX_AREA 0xff 83 84 #define STATE_DOWN 0 85 #define STATE_MOVE 1 86 #define STATE_UP 2 87 88 #define SETCONFIG_DUALTOUCH (1 << 0) 89 90 struct zforce_point { 91 int coord_x; 92 int coord_y; 93 int state; 94 int id; 95 int area_major; 96 int area_minor; 97 int orientation; 98 int pressure; 99 int prblty; 100 }; 101 102 /* 103 * @client the i2c_client 104 * @input the input device 105 * @suspending in the process of going to suspend (don't emit wakeup 106 * events for commands executed to suspend the device) 107 * @suspended device suspended 108 * @access_mutex serialize i2c-access, to keep multipart reads together 109 * @command_done completion to wait for the command result 110 * @command_mutex serialize commands sent to the ic 111 * @command_waiting the id of the command that is currently waiting 112 * for a result 113 * @command_result returned result of the command 114 */ 115 struct zforce_ts { 116 struct i2c_client *client; 117 struct input_dev *input; 118 const struct zforce_ts_platdata *pdata; 119 char phys[32]; 120 121 struct regulator *reg_vdd; 122 123 bool suspending; 124 bool suspended; 125 bool boot_complete; 126 127 /* Firmware version information */ 128 u16 version_major; 129 u16 version_minor; 130 u16 version_build; 131 u16 version_rev; 132 133 struct mutex access_mutex; 134 135 struct completion command_done; 136 struct mutex command_mutex; 137 int command_waiting; 138 int command_result; 139 }; 140 141 static int zforce_command(struct zforce_ts *ts, u8 cmd) 142 { 143 struct i2c_client *client = ts->client; 144 char buf[3]; 145 int ret; 146 147 dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd); 148 149 buf[0] = FRAME_START; 150 buf[1] = 1; /* data size, command only */ 151 buf[2] = cmd; 152 153 mutex_lock(&ts->access_mutex); 154 ret = i2c_master_send(client, &buf[0], ARRAY_SIZE(buf)); 155 mutex_unlock(&ts->access_mutex); 156 if (ret < 0) { 157 dev_err(&client->dev, "i2c send data request error: %d\n", ret); 158 return ret; 159 } 160 161 return 0; 162 } 163 164 static int zforce_send_wait(struct zforce_ts *ts, const char *buf, int len) 165 { 166 struct i2c_client *client = ts->client; 167 int ret; 168 169 ret = mutex_trylock(&ts->command_mutex); 170 if (!ret) { 171 dev_err(&client->dev, "already waiting for a command\n"); 172 return -EBUSY; 173 } 174 175 dev_dbg(&client->dev, "sending %d bytes for command 0x%x\n", 176 buf[1], buf[2]); 177 178 ts->command_waiting = buf[2]; 179 180 mutex_lock(&ts->access_mutex); 181 ret = i2c_master_send(client, buf, len); 182 mutex_unlock(&ts->access_mutex); 183 if (ret < 0) { 184 dev_err(&client->dev, "i2c send data request error: %d\n", ret); 185 goto unlock; 186 } 187 188 dev_dbg(&client->dev, "waiting for result for command 0x%x\n", buf[2]); 189 190 if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0) { 191 ret = -ETIME; 192 goto unlock; 193 } 194 195 ret = ts->command_result; 196 197 unlock: 198 mutex_unlock(&ts->command_mutex); 199 return ret; 200 } 201 202 static int zforce_command_wait(struct zforce_ts *ts, u8 cmd) 203 { 204 struct i2c_client *client = ts->client; 205 char buf[3]; 206 int ret; 207 208 dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd); 209 210 buf[0] = FRAME_START; 211 buf[1] = 1; /* data size, command only */ 212 buf[2] = cmd; 213 214 ret = zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf)); 215 if (ret < 0) { 216 dev_err(&client->dev, "i2c send data request error: %d\n", ret); 217 return ret; 218 } 219 220 return 0; 221 } 222 223 static int zforce_resolution(struct zforce_ts *ts, u16 x, u16 y) 224 { 225 struct i2c_client *client = ts->client; 226 char buf[7] = { FRAME_START, 5, COMMAND_RESOLUTION, 227 (x & 0xff), ((x >> 8) & 0xff), 228 (y & 0xff), ((y >> 8) & 0xff) }; 229 230 dev_dbg(&client->dev, "set resolution to (%d,%d)\n", x, y); 231 232 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf)); 233 } 234 235 static int zforce_scan_frequency(struct zforce_ts *ts, u16 idle, u16 finger, 236 u16 stylus) 237 { 238 struct i2c_client *client = ts->client; 239 char buf[9] = { FRAME_START, 7, COMMAND_SCANFREQ, 240 (idle & 0xff), ((idle >> 8) & 0xff), 241 (finger & 0xff), ((finger >> 8) & 0xff), 242 (stylus & 0xff), ((stylus >> 8) & 0xff) }; 243 244 dev_dbg(&client->dev, 245 "set scan frequency to (idle: %d, finger: %d, stylus: %d)\n", 246 idle, finger, stylus); 247 248 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf)); 249 } 250 251 static int zforce_setconfig(struct zforce_ts *ts, char b1) 252 { 253 struct i2c_client *client = ts->client; 254 char buf[7] = { FRAME_START, 5, COMMAND_SETCONFIG, 255 b1, 0, 0, 0 }; 256 257 dev_dbg(&client->dev, "set config to (%d)\n", b1); 258 259 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf)); 260 } 261 262 static int zforce_start(struct zforce_ts *ts) 263 { 264 struct i2c_client *client = ts->client; 265 const struct zforce_ts_platdata *pdata = ts->pdata; 266 int ret; 267 268 dev_dbg(&client->dev, "starting device\n"); 269 270 ret = zforce_command_wait(ts, COMMAND_INITIALIZE); 271 if (ret) { 272 dev_err(&client->dev, "Unable to initialize, %d\n", ret); 273 return ret; 274 } 275 276 ret = zforce_resolution(ts, pdata->x_max, pdata->y_max); 277 if (ret) { 278 dev_err(&client->dev, "Unable to set resolution, %d\n", ret); 279 goto error; 280 } 281 282 ret = zforce_scan_frequency(ts, 10, 50, 50); 283 if (ret) { 284 dev_err(&client->dev, "Unable to set scan frequency, %d\n", 285 ret); 286 goto error; 287 } 288 289 ret = zforce_setconfig(ts, SETCONFIG_DUALTOUCH); 290 if (ret) { 291 dev_err(&client->dev, "Unable to set config\n"); 292 goto error; 293 } 294 295 /* start sending touch events */ 296 ret = zforce_command(ts, COMMAND_DATAREQUEST); 297 if (ret) { 298 dev_err(&client->dev, "Unable to request data\n"); 299 goto error; 300 } 301 302 /* 303 * Per NN, initial cal. take max. of 200msec. 304 * Allow time to complete this calibration 305 */ 306 msleep(200); 307 308 return 0; 309 310 error: 311 zforce_command_wait(ts, COMMAND_DEACTIVATE); 312 return ret; 313 } 314 315 static int zforce_stop(struct zforce_ts *ts) 316 { 317 struct i2c_client *client = ts->client; 318 int ret; 319 320 dev_dbg(&client->dev, "stopping device\n"); 321 322 /* Deactivates touch sensing and puts the device into sleep. */ 323 ret = zforce_command_wait(ts, COMMAND_DEACTIVATE); 324 if (ret != 0) { 325 dev_err(&client->dev, "could not deactivate device, %d\n", 326 ret); 327 return ret; 328 } 329 330 return 0; 331 } 332 333 static int zforce_touch_event(struct zforce_ts *ts, u8 *payload) 334 { 335 struct i2c_client *client = ts->client; 336 const struct zforce_ts_platdata *pdata = ts->pdata; 337 struct zforce_point point; 338 int count, i, num = 0; 339 340 count = payload[0]; 341 if (count > ZFORCE_REPORT_POINTS) { 342 dev_warn(&client->dev, 343 "too many coordinates %d, expected max %d\n", 344 count, ZFORCE_REPORT_POINTS); 345 count = ZFORCE_REPORT_POINTS; 346 } 347 348 for (i = 0; i < count; i++) { 349 point.coord_x = 350 payload[9 * i + 2] << 8 | payload[9 * i + 1]; 351 point.coord_y = 352 payload[9 * i + 4] << 8 | payload[9 * i + 3]; 353 354 if (point.coord_x > pdata->x_max || 355 point.coord_y > pdata->y_max) { 356 dev_warn(&client->dev, "coordinates (%d,%d) invalid\n", 357 point.coord_x, point.coord_y); 358 point.coord_x = point.coord_y = 0; 359 } 360 361 point.state = payload[9 * i + 5] & 0x03; 362 point.id = (payload[9 * i + 5] & 0xfc) >> 2; 363 364 /* determine touch major, minor and orientation */ 365 point.area_major = max(payload[9 * i + 6], 366 payload[9 * i + 7]); 367 point.area_minor = min(payload[9 * i + 6], 368 payload[9 * i + 7]); 369 point.orientation = payload[9 * i + 6] > payload[9 * i + 7]; 370 371 point.pressure = payload[9 * i + 8]; 372 point.prblty = payload[9 * i + 9]; 373 374 dev_dbg(&client->dev, 375 "point %d/%d: state %d, id %d, pressure %d, prblty %d, x %d, y %d, amajor %d, aminor %d, ori %d\n", 376 i, count, point.state, point.id, 377 point.pressure, point.prblty, 378 point.coord_x, point.coord_y, 379 point.area_major, point.area_minor, 380 point.orientation); 381 382 /* the zforce id starts with "1", so needs to be decreased */ 383 input_mt_slot(ts->input, point.id - 1); 384 385 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, 386 point.state != STATE_UP); 387 388 if (point.state != STATE_UP) { 389 input_report_abs(ts->input, ABS_MT_POSITION_X, 390 point.coord_x); 391 input_report_abs(ts->input, ABS_MT_POSITION_Y, 392 point.coord_y); 393 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, 394 point.area_major); 395 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, 396 point.area_minor); 397 input_report_abs(ts->input, ABS_MT_ORIENTATION, 398 point.orientation); 399 num++; 400 } 401 } 402 403 input_mt_sync_frame(ts->input); 404 405 input_mt_report_finger_count(ts->input, num); 406 407 input_sync(ts->input); 408 409 return 0; 410 } 411 412 static int zforce_read_packet(struct zforce_ts *ts, u8 *buf) 413 { 414 struct i2c_client *client = ts->client; 415 int ret; 416 417 mutex_lock(&ts->access_mutex); 418 419 /* read 2 byte message header */ 420 ret = i2c_master_recv(client, buf, 2); 421 if (ret < 0) { 422 dev_err(&client->dev, "error reading header: %d\n", ret); 423 goto unlock; 424 } 425 426 if (buf[PAYLOAD_HEADER] != FRAME_START) { 427 dev_err(&client->dev, "invalid frame start: %d\n", buf[0]); 428 ret = -EIO; 429 goto unlock; 430 } 431 432 if (buf[PAYLOAD_LENGTH] == 0) { 433 dev_err(&client->dev, "invalid payload length: %d\n", 434 buf[PAYLOAD_LENGTH]); 435 ret = -EIO; 436 goto unlock; 437 } 438 439 /* read the message */ 440 ret = i2c_master_recv(client, &buf[PAYLOAD_BODY], buf[PAYLOAD_LENGTH]); 441 if (ret < 0) { 442 dev_err(&client->dev, "error reading payload: %d\n", ret); 443 goto unlock; 444 } 445 446 dev_dbg(&client->dev, "read %d bytes for response command 0x%x\n", 447 buf[PAYLOAD_LENGTH], buf[PAYLOAD_BODY]); 448 449 unlock: 450 mutex_unlock(&ts->access_mutex); 451 return ret; 452 } 453 454 static void zforce_complete(struct zforce_ts *ts, int cmd, int result) 455 { 456 struct i2c_client *client = ts->client; 457 458 if (ts->command_waiting == cmd) { 459 dev_dbg(&client->dev, "completing command 0x%x\n", cmd); 460 ts->command_result = result; 461 complete(&ts->command_done); 462 } else { 463 dev_dbg(&client->dev, "command %d not for us\n", cmd); 464 } 465 } 466 467 static irqreturn_t zforce_irq(int irq, void *dev_id) 468 { 469 struct zforce_ts *ts = dev_id; 470 struct i2c_client *client = ts->client; 471 472 if (ts->suspended && device_may_wakeup(&client->dev)) 473 pm_wakeup_event(&client->dev, 500); 474 475 return IRQ_WAKE_THREAD; 476 } 477 478 static irqreturn_t zforce_irq_thread(int irq, void *dev_id) 479 { 480 struct zforce_ts *ts = dev_id; 481 struct i2c_client *client = ts->client; 482 const struct zforce_ts_platdata *pdata = ts->pdata; 483 int ret; 484 u8 payload_buffer[FRAME_MAXSIZE]; 485 u8 *payload; 486 487 /* 488 * When still suspended, return. 489 * Due to the level-interrupt we will get re-triggered later. 490 */ 491 if (ts->suspended) { 492 msleep(20); 493 return IRQ_HANDLED; 494 } 495 496 dev_dbg(&client->dev, "handling interrupt\n"); 497 498 /* Don't emit wakeup events from commands run by zforce_suspend */ 499 if (!ts->suspending && device_may_wakeup(&client->dev)) 500 pm_stay_awake(&client->dev); 501 502 while (!gpio_get_value(pdata->gpio_int)) { 503 ret = zforce_read_packet(ts, payload_buffer); 504 if (ret < 0) { 505 dev_err(&client->dev, 506 "could not read packet, ret: %d\n", ret); 507 break; 508 } 509 510 payload = &payload_buffer[PAYLOAD_BODY]; 511 512 switch (payload[RESPONSE_ID]) { 513 case NOTIFICATION_TOUCH: 514 /* 515 * Always report touch-events received while 516 * suspending, when being a wakeup source 517 */ 518 if (ts->suspending && device_may_wakeup(&client->dev)) 519 pm_wakeup_event(&client->dev, 500); 520 zforce_touch_event(ts, &payload[RESPONSE_DATA]); 521 break; 522 523 case NOTIFICATION_BOOTCOMPLETE: 524 ts->boot_complete = payload[RESPONSE_DATA]; 525 zforce_complete(ts, payload[RESPONSE_ID], 0); 526 break; 527 528 case RESPONSE_INITIALIZE: 529 case RESPONSE_DEACTIVATE: 530 case RESPONSE_SETCONFIG: 531 case RESPONSE_RESOLUTION: 532 case RESPONSE_SCANFREQ: 533 zforce_complete(ts, payload[RESPONSE_ID], 534 payload[RESPONSE_DATA]); 535 break; 536 537 case RESPONSE_STATUS: 538 /* 539 * Version Payload Results 540 * [2:major] [2:minor] [2:build] [2:rev] 541 */ 542 ts->version_major = (payload[RESPONSE_DATA + 1] << 8) | 543 payload[RESPONSE_DATA]; 544 ts->version_minor = (payload[RESPONSE_DATA + 3] << 8) | 545 payload[RESPONSE_DATA + 2]; 546 ts->version_build = (payload[RESPONSE_DATA + 5] << 8) | 547 payload[RESPONSE_DATA + 4]; 548 ts->version_rev = (payload[RESPONSE_DATA + 7] << 8) | 549 payload[RESPONSE_DATA + 6]; 550 dev_dbg(&ts->client->dev, 551 "Firmware Version %04x:%04x %04x:%04x\n", 552 ts->version_major, ts->version_minor, 553 ts->version_build, ts->version_rev); 554 555 zforce_complete(ts, payload[RESPONSE_ID], 0); 556 break; 557 558 case NOTIFICATION_INVALID_COMMAND: 559 dev_err(&ts->client->dev, "invalid command: 0x%x\n", 560 payload[RESPONSE_DATA]); 561 break; 562 563 default: 564 dev_err(&ts->client->dev, 565 "unrecognized response id: 0x%x\n", 566 payload[RESPONSE_ID]); 567 break; 568 } 569 } 570 571 if (!ts->suspending && device_may_wakeup(&client->dev)) 572 pm_relax(&client->dev); 573 574 dev_dbg(&client->dev, "finished interrupt\n"); 575 576 return IRQ_HANDLED; 577 } 578 579 static int zforce_input_open(struct input_dev *dev) 580 { 581 struct zforce_ts *ts = input_get_drvdata(dev); 582 int ret; 583 584 ret = zforce_start(ts); 585 if (ret) 586 return ret; 587 588 return 0; 589 } 590 591 static void zforce_input_close(struct input_dev *dev) 592 { 593 struct zforce_ts *ts = input_get_drvdata(dev); 594 struct i2c_client *client = ts->client; 595 int ret; 596 597 ret = zforce_stop(ts); 598 if (ret) 599 dev_warn(&client->dev, "stopping zforce failed\n"); 600 601 return; 602 } 603 604 static int __maybe_unused zforce_suspend(struct device *dev) 605 { 606 struct i2c_client *client = to_i2c_client(dev); 607 struct zforce_ts *ts = i2c_get_clientdata(client); 608 struct input_dev *input = ts->input; 609 int ret = 0; 610 611 mutex_lock(&input->mutex); 612 ts->suspending = true; 613 614 /* 615 * When configured as a wakeup source device should always wake 616 * the system, therefore start device if necessary. 617 */ 618 if (device_may_wakeup(&client->dev)) { 619 dev_dbg(&client->dev, "suspend while being a wakeup source\n"); 620 621 /* Need to start device, if not open, to be a wakeup source. */ 622 if (!input->users) { 623 ret = zforce_start(ts); 624 if (ret) 625 goto unlock; 626 } 627 628 enable_irq_wake(client->irq); 629 } else if (input->users) { 630 dev_dbg(&client->dev, 631 "suspend without being a wakeup source\n"); 632 633 ret = zforce_stop(ts); 634 if (ret) 635 goto unlock; 636 637 disable_irq(client->irq); 638 } 639 640 ts->suspended = true; 641 642 unlock: 643 ts->suspending = false; 644 mutex_unlock(&input->mutex); 645 646 return ret; 647 } 648 649 static int __maybe_unused zforce_resume(struct device *dev) 650 { 651 struct i2c_client *client = to_i2c_client(dev); 652 struct zforce_ts *ts = i2c_get_clientdata(client); 653 struct input_dev *input = ts->input; 654 int ret = 0; 655 656 mutex_lock(&input->mutex); 657 658 ts->suspended = false; 659 660 if (device_may_wakeup(&client->dev)) { 661 dev_dbg(&client->dev, "resume from being a wakeup source\n"); 662 663 disable_irq_wake(client->irq); 664 665 /* need to stop device if it was not open on suspend */ 666 if (!input->users) { 667 ret = zforce_stop(ts); 668 if (ret) 669 goto unlock; 670 } 671 } else if (input->users) { 672 dev_dbg(&client->dev, "resume without being a wakeup source\n"); 673 674 enable_irq(client->irq); 675 676 ret = zforce_start(ts); 677 if (ret < 0) 678 goto unlock; 679 } 680 681 unlock: 682 mutex_unlock(&input->mutex); 683 684 return ret; 685 } 686 687 static SIMPLE_DEV_PM_OPS(zforce_pm_ops, zforce_suspend, zforce_resume); 688 689 static void zforce_reset(void *data) 690 { 691 struct zforce_ts *ts = data; 692 693 gpio_set_value(ts->pdata->gpio_rst, 0); 694 695 udelay(10); 696 697 if (!IS_ERR(ts->reg_vdd)) 698 regulator_disable(ts->reg_vdd); 699 } 700 701 static struct zforce_ts_platdata *zforce_parse_dt(struct device *dev) 702 { 703 struct zforce_ts_platdata *pdata; 704 struct device_node *np = dev->of_node; 705 706 if (!np) 707 return ERR_PTR(-ENOENT); 708 709 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 710 if (!pdata) { 711 dev_err(dev, "failed to allocate platform data\n"); 712 return ERR_PTR(-ENOMEM); 713 } 714 715 pdata->gpio_int = of_get_gpio(np, 0); 716 if (!gpio_is_valid(pdata->gpio_int)) { 717 dev_err(dev, "failed to get interrupt gpio\n"); 718 return ERR_PTR(-EINVAL); 719 } 720 721 pdata->gpio_rst = of_get_gpio(np, 1); 722 if (!gpio_is_valid(pdata->gpio_rst)) { 723 dev_err(dev, "failed to get reset gpio\n"); 724 return ERR_PTR(-EINVAL); 725 } 726 727 if (of_property_read_u32(np, "x-size", &pdata->x_max)) { 728 dev_err(dev, "failed to get x-size property\n"); 729 return ERR_PTR(-EINVAL); 730 } 731 732 if (of_property_read_u32(np, "y-size", &pdata->y_max)) { 733 dev_err(dev, "failed to get y-size property\n"); 734 return ERR_PTR(-EINVAL); 735 } 736 737 return pdata; 738 } 739 740 static int zforce_probe(struct i2c_client *client, 741 const struct i2c_device_id *id) 742 { 743 const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev); 744 struct zforce_ts *ts; 745 struct input_dev *input_dev; 746 int ret; 747 748 if (!pdata) { 749 pdata = zforce_parse_dt(&client->dev); 750 if (IS_ERR(pdata)) 751 return PTR_ERR(pdata); 752 } 753 754 ts = devm_kzalloc(&client->dev, sizeof(struct zforce_ts), GFP_KERNEL); 755 if (!ts) 756 return -ENOMEM; 757 758 ret = devm_gpio_request_one(&client->dev, pdata->gpio_int, GPIOF_IN, 759 "zforce_ts_int"); 760 if (ret) { 761 dev_err(&client->dev, "request of gpio %d failed, %d\n", 762 pdata->gpio_int, ret); 763 return ret; 764 } 765 766 ret = devm_gpio_request_one(&client->dev, pdata->gpio_rst, 767 GPIOF_OUT_INIT_LOW, "zforce_ts_rst"); 768 if (ret) { 769 dev_err(&client->dev, "request of gpio %d failed, %d\n", 770 pdata->gpio_rst, ret); 771 return ret; 772 } 773 774 ts->reg_vdd = devm_regulator_get_optional(&client->dev, "vdd"); 775 if (IS_ERR(ts->reg_vdd)) { 776 ret = PTR_ERR(ts->reg_vdd); 777 if (ret == -EPROBE_DEFER) 778 return ret; 779 } else { 780 ret = regulator_enable(ts->reg_vdd); 781 if (ret) 782 return ret; 783 784 /* 785 * according to datasheet add 100us grace time after regular 786 * regulator enable delay. 787 */ 788 udelay(100); 789 } 790 791 ret = devm_add_action(&client->dev, zforce_reset, ts); 792 if (ret) { 793 dev_err(&client->dev, "failed to register reset action, %d\n", 794 ret); 795 796 /* hereafter the regulator will be disabled by the action */ 797 if (!IS_ERR(ts->reg_vdd)) 798 regulator_disable(ts->reg_vdd); 799 800 return ret; 801 } 802 803 snprintf(ts->phys, sizeof(ts->phys), 804 "%s/input0", dev_name(&client->dev)); 805 806 input_dev = devm_input_allocate_device(&client->dev); 807 if (!input_dev) { 808 dev_err(&client->dev, "could not allocate input device\n"); 809 return -ENOMEM; 810 } 811 812 mutex_init(&ts->access_mutex); 813 mutex_init(&ts->command_mutex); 814 815 ts->pdata = pdata; 816 ts->client = client; 817 ts->input = input_dev; 818 819 input_dev->name = "Neonode zForce touchscreen"; 820 input_dev->phys = ts->phys; 821 input_dev->id.bustype = BUS_I2C; 822 823 input_dev->open = zforce_input_open; 824 input_dev->close = zforce_input_close; 825 826 __set_bit(EV_KEY, input_dev->evbit); 827 __set_bit(EV_SYN, input_dev->evbit); 828 __set_bit(EV_ABS, input_dev->evbit); 829 830 /* For multi touch */ 831 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, 832 pdata->x_max, 0, 0); 833 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, 834 pdata->y_max, 0, 0); 835 836 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 837 ZFORCE_MAX_AREA, 0, 0); 838 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0, 839 ZFORCE_MAX_AREA, 0, 0); 840 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0); 841 input_mt_init_slots(input_dev, ZFORCE_REPORT_POINTS, INPUT_MT_DIRECT); 842 843 input_set_drvdata(ts->input, ts); 844 845 init_completion(&ts->command_done); 846 847 /* 848 * The zforce pulls the interrupt low when it has data ready. 849 * After it is triggered the isr thread runs until all the available 850 * packets have been read and the interrupt is high again. 851 * Therefore we can trigger the interrupt anytime it is low and do 852 * not need to limit it to the interrupt edge. 853 */ 854 ret = devm_request_threaded_irq(&client->dev, client->irq, 855 zforce_irq, zforce_irq_thread, 856 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 857 input_dev->name, ts); 858 if (ret) { 859 dev_err(&client->dev, "irq %d request failed\n", client->irq); 860 return ret; 861 } 862 863 i2c_set_clientdata(client, ts); 864 865 /* let the controller boot */ 866 gpio_set_value(pdata->gpio_rst, 1); 867 868 ts->command_waiting = NOTIFICATION_BOOTCOMPLETE; 869 if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0) 870 dev_warn(&client->dev, "bootcomplete timed out\n"); 871 872 /* need to start device to get version information */ 873 ret = zforce_command_wait(ts, COMMAND_INITIALIZE); 874 if (ret) { 875 dev_err(&client->dev, "unable to initialize, %d\n", ret); 876 return ret; 877 } 878 879 /* this gets the firmware version among other information */ 880 ret = zforce_command_wait(ts, COMMAND_STATUS); 881 if (ret < 0) { 882 dev_err(&client->dev, "couldn't get status, %d\n", ret); 883 zforce_stop(ts); 884 return ret; 885 } 886 887 /* stop device and put it into sleep until it is opened */ 888 ret = zforce_stop(ts); 889 if (ret < 0) 890 return ret; 891 892 device_set_wakeup_capable(&client->dev, true); 893 894 ret = input_register_device(input_dev); 895 if (ret) { 896 dev_err(&client->dev, "could not register input device, %d\n", 897 ret); 898 return ret; 899 } 900 901 return 0; 902 } 903 904 static struct i2c_device_id zforce_idtable[] = { 905 { "zforce-ts", 0 }, 906 { } 907 }; 908 MODULE_DEVICE_TABLE(i2c, zforce_idtable); 909 910 #ifdef CONFIG_OF 911 static const struct of_device_id zforce_dt_idtable[] = { 912 { .compatible = "neonode,zforce" }, 913 {}, 914 }; 915 MODULE_DEVICE_TABLE(of, zforce_dt_idtable); 916 #endif 917 918 static struct i2c_driver zforce_driver = { 919 .driver = { 920 .owner = THIS_MODULE, 921 .name = "zforce-ts", 922 .pm = &zforce_pm_ops, 923 .of_match_table = of_match_ptr(zforce_dt_idtable), 924 }, 925 .probe = zforce_probe, 926 .id_table = zforce_idtable, 927 }; 928 929 module_i2c_driver(zforce_driver); 930 931 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>"); 932 MODULE_DESCRIPTION("zForce TouchScreen Driver"); 933 MODULE_LICENSE("GPL"); 934