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