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