Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2012-2013 MundoReader S.L.
0004  * Author: Heiko Stuebner <heiko@sntech.de>
0005  *
0006  * based in parts on Nook zforce driver
0007  *
0008  * Copyright (C) 2010 Barnes & Noble, Inc.
0009  * Author: Pieter Truter<ptruter@intrinsyc.com>
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/hrtimer.h>
0014 #include <linux/slab.h>
0015 #include <linux/input.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/i2c.h>
0018 #include <linux/delay.h>
0019 #include <linux/gpio/consumer.h>
0020 #include <linux/device.h>
0021 #include <linux/sysfs.h>
0022 #include <linux/input/mt.h>
0023 #include <linux/platform_data/zforce_ts.h>
0024 #include <linux/regulator/consumer.h>
0025 #include <linux/of.h>
0026 
0027 #define WAIT_TIMEOUT        msecs_to_jiffies(1000)
0028 
0029 #define FRAME_START     0xee
0030 #define FRAME_MAXSIZE       257
0031 
0032 /* Offsets of the different parts of the payload the controller sends */
0033 #define PAYLOAD_HEADER      0
0034 #define PAYLOAD_LENGTH      1
0035 #define PAYLOAD_BODY        2
0036 
0037 /* Response offsets */
0038 #define RESPONSE_ID     0
0039 #define RESPONSE_DATA       1
0040 
0041 /* Commands */
0042 #define COMMAND_DEACTIVATE  0x00
0043 #define COMMAND_INITIALIZE  0x01
0044 #define COMMAND_RESOLUTION  0x02
0045 #define COMMAND_SETCONFIG   0x03
0046 #define COMMAND_DATAREQUEST 0x04
0047 #define COMMAND_SCANFREQ    0x08
0048 #define COMMAND_STATUS      0X1e
0049 
0050 /*
0051  * Responses the controller sends as a result of
0052  * command requests
0053  */
0054 #define RESPONSE_DEACTIVATE 0x00
0055 #define RESPONSE_INITIALIZE 0x01
0056 #define RESPONSE_RESOLUTION 0x02
0057 #define RESPONSE_SETCONFIG  0x03
0058 #define RESPONSE_SCANFREQ   0x08
0059 #define RESPONSE_STATUS     0X1e
0060 
0061 /*
0062  * Notifications are sent by the touch controller without
0063  * being requested by the driver and include for example
0064  * touch indications
0065  */
0066 #define NOTIFICATION_TOUCH      0x04
0067 #define NOTIFICATION_BOOTCOMPLETE   0x07
0068 #define NOTIFICATION_OVERRUN        0x25
0069 #define NOTIFICATION_PROXIMITY      0x26
0070 #define NOTIFICATION_INVALID_COMMAND    0xfe
0071 
0072 #define ZFORCE_REPORT_POINTS        2
0073 #define ZFORCE_MAX_AREA         0xff
0074 
0075 #define STATE_DOWN          0
0076 #define STATE_MOVE          1
0077 #define STATE_UP            2
0078 
0079 #define SETCONFIG_DUALTOUCH     (1 << 0)
0080 
0081 struct zforce_point {
0082     int coord_x;
0083     int coord_y;
0084     int state;
0085     int id;
0086     int area_major;
0087     int area_minor;
0088     int orientation;
0089     int pressure;
0090     int prblty;
0091 };
0092 
0093 /*
0094  * @client      the i2c_client
0095  * @input       the input device
0096  * @suspending      in the process of going to suspend (don't emit wakeup
0097  *          events for commands executed to suspend the device)
0098  * @suspended       device suspended
0099  * @access_mutex    serialize i2c-access, to keep multipart reads together
0100  * @command_done    completion to wait for the command result
0101  * @command_mutex   serialize commands sent to the ic
0102  * @command_waiting the id of the command that is currently waiting
0103  *          for a result
0104  * @command_result  returned result of the command
0105  */
0106 struct zforce_ts {
0107     struct i2c_client   *client;
0108     struct input_dev    *input;
0109     const struct zforce_ts_platdata *pdata;
0110     char            phys[32];
0111 
0112     struct regulator    *reg_vdd;
0113 
0114     struct gpio_desc    *gpio_int;
0115     struct gpio_desc    *gpio_rst;
0116 
0117     bool            suspending;
0118     bool            suspended;
0119     bool            boot_complete;
0120 
0121     /* Firmware version information */
0122     u16         version_major;
0123     u16         version_minor;
0124     u16         version_build;
0125     u16         version_rev;
0126 
0127     struct mutex        access_mutex;
0128 
0129     struct completion   command_done;
0130     struct mutex        command_mutex;
0131     int         command_waiting;
0132     int         command_result;
0133 };
0134 
0135 static int zforce_command(struct zforce_ts *ts, u8 cmd)
0136 {
0137     struct i2c_client *client = ts->client;
0138     char buf[3];
0139     int ret;
0140 
0141     dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
0142 
0143     buf[0] = FRAME_START;
0144     buf[1] = 1; /* data size, command only */
0145     buf[2] = cmd;
0146 
0147     mutex_lock(&ts->access_mutex);
0148     ret = i2c_master_send(client, &buf[0], ARRAY_SIZE(buf));
0149     mutex_unlock(&ts->access_mutex);
0150     if (ret < 0) {
0151         dev_err(&client->dev, "i2c send data request error: %d\n", ret);
0152         return ret;
0153     }
0154 
0155     return 0;
0156 }
0157 
0158 static void zforce_reset_assert(struct zforce_ts *ts)
0159 {
0160     gpiod_set_value_cansleep(ts->gpio_rst, 1);
0161 }
0162 
0163 static void zforce_reset_deassert(struct zforce_ts *ts)
0164 {
0165     gpiod_set_value_cansleep(ts->gpio_rst, 0);
0166 }
0167 
0168 static int zforce_send_wait(struct zforce_ts *ts, const char *buf, int len)
0169 {
0170     struct i2c_client *client = ts->client;
0171     int ret;
0172 
0173     ret = mutex_trylock(&ts->command_mutex);
0174     if (!ret) {
0175         dev_err(&client->dev, "already waiting for a command\n");
0176         return -EBUSY;
0177     }
0178 
0179     dev_dbg(&client->dev, "sending %d bytes for command 0x%x\n",
0180         buf[1], buf[2]);
0181 
0182     ts->command_waiting = buf[2];
0183 
0184     mutex_lock(&ts->access_mutex);
0185     ret = i2c_master_send(client, buf, len);
0186     mutex_unlock(&ts->access_mutex);
0187     if (ret < 0) {
0188         dev_err(&client->dev, "i2c send data request error: %d\n", ret);
0189         goto unlock;
0190     }
0191 
0192     dev_dbg(&client->dev, "waiting for result for command 0x%x\n", buf[2]);
0193 
0194     if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0) {
0195         ret = -ETIME;
0196         goto unlock;
0197     }
0198 
0199     ret = ts->command_result;
0200 
0201 unlock:
0202     mutex_unlock(&ts->command_mutex);
0203     return ret;
0204 }
0205 
0206 static int zforce_command_wait(struct zforce_ts *ts, u8 cmd)
0207 {
0208     struct i2c_client *client = ts->client;
0209     char buf[3];
0210     int ret;
0211 
0212     dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
0213 
0214     buf[0] = FRAME_START;
0215     buf[1] = 1; /* data size, command only */
0216     buf[2] = cmd;
0217 
0218     ret = zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
0219     if (ret < 0) {
0220         dev_err(&client->dev, "i2c send data request error: %d\n", ret);
0221         return ret;
0222     }
0223 
0224     return 0;
0225 }
0226 
0227 static int zforce_resolution(struct zforce_ts *ts, u16 x, u16 y)
0228 {
0229     struct i2c_client *client = ts->client;
0230     char buf[7] = { FRAME_START, 5, COMMAND_RESOLUTION,
0231             (x & 0xff), ((x >> 8) & 0xff),
0232             (y & 0xff), ((y >> 8) & 0xff) };
0233 
0234     dev_dbg(&client->dev, "set resolution to (%d,%d)\n", x, y);
0235 
0236     return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
0237 }
0238 
0239 static int zforce_scan_frequency(struct zforce_ts *ts, u16 idle, u16 finger,
0240                  u16 stylus)
0241 {
0242     struct i2c_client *client = ts->client;
0243     char buf[9] = { FRAME_START, 7, COMMAND_SCANFREQ,
0244             (idle & 0xff), ((idle >> 8) & 0xff),
0245             (finger & 0xff), ((finger >> 8) & 0xff),
0246             (stylus & 0xff), ((stylus >> 8) & 0xff) };
0247 
0248     dev_dbg(&client->dev,
0249         "set scan frequency to (idle: %d, finger: %d, stylus: %d)\n",
0250         idle, finger, stylus);
0251 
0252     return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
0253 }
0254 
0255 static int zforce_setconfig(struct zforce_ts *ts, char b1)
0256 {
0257     struct i2c_client *client = ts->client;
0258     char buf[7] = { FRAME_START, 5, COMMAND_SETCONFIG,
0259             b1, 0, 0, 0 };
0260 
0261     dev_dbg(&client->dev, "set config to (%d)\n", b1);
0262 
0263     return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
0264 }
0265 
0266 static int zforce_start(struct zforce_ts *ts)
0267 {
0268     struct i2c_client *client = ts->client;
0269     const struct zforce_ts_platdata *pdata = ts->pdata;
0270     int ret;
0271 
0272     dev_dbg(&client->dev, "starting device\n");
0273 
0274     ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
0275     if (ret) {
0276         dev_err(&client->dev, "Unable to initialize, %d\n", ret);
0277         return ret;
0278     }
0279 
0280     ret = zforce_resolution(ts, pdata->x_max, pdata->y_max);
0281     if (ret) {
0282         dev_err(&client->dev, "Unable to set resolution, %d\n", ret);
0283         goto error;
0284     }
0285 
0286     ret = zforce_scan_frequency(ts, 10, 50, 50);
0287     if (ret) {
0288         dev_err(&client->dev, "Unable to set scan frequency, %d\n",
0289             ret);
0290         goto error;
0291     }
0292 
0293     ret = zforce_setconfig(ts, SETCONFIG_DUALTOUCH);
0294     if (ret) {
0295         dev_err(&client->dev, "Unable to set config\n");
0296         goto error;
0297     }
0298 
0299     /* start sending touch events */
0300     ret = zforce_command(ts, COMMAND_DATAREQUEST);
0301     if (ret) {
0302         dev_err(&client->dev, "Unable to request data\n");
0303         goto error;
0304     }
0305 
0306     /*
0307      * Per NN, initial cal. take max. of 200msec.
0308      * Allow time to complete this calibration
0309      */
0310     msleep(200);
0311 
0312     return 0;
0313 
0314 error:
0315     zforce_command_wait(ts, COMMAND_DEACTIVATE);
0316     return ret;
0317 }
0318 
0319 static int zforce_stop(struct zforce_ts *ts)
0320 {
0321     struct i2c_client *client = ts->client;
0322     int ret;
0323 
0324     dev_dbg(&client->dev, "stopping device\n");
0325 
0326     /* Deactivates touch sensing and puts the device into sleep. */
0327     ret = zforce_command_wait(ts, COMMAND_DEACTIVATE);
0328     if (ret != 0) {
0329         dev_err(&client->dev, "could not deactivate device, %d\n",
0330             ret);
0331         return ret;
0332     }
0333 
0334     return 0;
0335 }
0336 
0337 static int zforce_touch_event(struct zforce_ts *ts, u8 *payload)
0338 {
0339     struct i2c_client *client = ts->client;
0340     const struct zforce_ts_platdata *pdata = ts->pdata;
0341     struct zforce_point point;
0342     int count, i, num = 0;
0343 
0344     count = payload[0];
0345     if (count > ZFORCE_REPORT_POINTS) {
0346         dev_warn(&client->dev,
0347              "too many coordinates %d, expected max %d\n",
0348              count, ZFORCE_REPORT_POINTS);
0349         count = ZFORCE_REPORT_POINTS;
0350     }
0351 
0352     for (i = 0; i < count; i++) {
0353         point.coord_x =
0354             payload[9 * i + 2] << 8 | payload[9 * i + 1];
0355         point.coord_y =
0356             payload[9 * i + 4] << 8 | payload[9 * i + 3];
0357 
0358         if (point.coord_x > pdata->x_max ||
0359             point.coord_y > pdata->y_max) {
0360             dev_warn(&client->dev, "coordinates (%d,%d) invalid\n",
0361                 point.coord_x, point.coord_y);
0362             point.coord_x = point.coord_y = 0;
0363         }
0364 
0365         point.state = payload[9 * i + 5] & 0x0f;
0366         point.id = (payload[9 * i + 5] & 0xf0) >> 4;
0367 
0368         /* determine touch major, minor and orientation */
0369         point.area_major = max(payload[9 * i + 6],
0370                       payload[9 * i + 7]);
0371         point.area_minor = min(payload[9 * i + 6],
0372                       payload[9 * i + 7]);
0373         point.orientation = payload[9 * i + 6] > payload[9 * i + 7];
0374 
0375         point.pressure = payload[9 * i + 8];
0376         point.prblty = payload[9 * i + 9];
0377 
0378         dev_dbg(&client->dev,
0379             "point %d/%d: state %d, id %d, pressure %d, prblty %d, x %d, y %d, amajor %d, aminor %d, ori %d\n",
0380             i, count, point.state, point.id,
0381             point.pressure, point.prblty,
0382             point.coord_x, point.coord_y,
0383             point.area_major, point.area_minor,
0384             point.orientation);
0385 
0386         /* the zforce id starts with "1", so needs to be decreased */
0387         input_mt_slot(ts->input, point.id - 1);
0388 
0389         input_mt_report_slot_state(ts->input, MT_TOOL_FINGER,
0390                         point.state != STATE_UP);
0391 
0392         if (point.state != STATE_UP) {
0393             input_report_abs(ts->input, ABS_MT_POSITION_X,
0394                      point.coord_x);
0395             input_report_abs(ts->input, ABS_MT_POSITION_Y,
0396                      point.coord_y);
0397             input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR,
0398                      point.area_major);
0399             input_report_abs(ts->input, ABS_MT_TOUCH_MINOR,
0400                      point.area_minor);
0401             input_report_abs(ts->input, ABS_MT_ORIENTATION,
0402                      point.orientation);
0403             num++;
0404         }
0405     }
0406 
0407     input_mt_sync_frame(ts->input);
0408 
0409     input_mt_report_finger_count(ts->input, num);
0410 
0411     input_sync(ts->input);
0412 
0413     return 0;
0414 }
0415 
0416 static int zforce_read_packet(struct zforce_ts *ts, u8 *buf)
0417 {
0418     struct i2c_client *client = ts->client;
0419     int ret;
0420 
0421     mutex_lock(&ts->access_mutex);
0422 
0423     /* read 2 byte message header */
0424     ret = i2c_master_recv(client, buf, 2);
0425     if (ret < 0) {
0426         dev_err(&client->dev, "error reading header: %d\n", ret);
0427         goto unlock;
0428     }
0429 
0430     if (buf[PAYLOAD_HEADER] != FRAME_START) {
0431         dev_err(&client->dev, "invalid frame start: %d\n", buf[0]);
0432         ret = -EIO;
0433         goto unlock;
0434     }
0435 
0436     if (buf[PAYLOAD_LENGTH] == 0) {
0437         dev_err(&client->dev, "invalid payload length: %d\n",
0438             buf[PAYLOAD_LENGTH]);
0439         ret = -EIO;
0440         goto unlock;
0441     }
0442 
0443     /* read the message */
0444     ret = i2c_master_recv(client, &buf[PAYLOAD_BODY], buf[PAYLOAD_LENGTH]);
0445     if (ret < 0) {
0446         dev_err(&client->dev, "error reading payload: %d\n", ret);
0447         goto unlock;
0448     }
0449 
0450     dev_dbg(&client->dev, "read %d bytes for response command 0x%x\n",
0451         buf[PAYLOAD_LENGTH], buf[PAYLOAD_BODY]);
0452 
0453 unlock:
0454     mutex_unlock(&ts->access_mutex);
0455     return ret;
0456 }
0457 
0458 static void zforce_complete(struct zforce_ts *ts, int cmd, int result)
0459 {
0460     struct i2c_client *client = ts->client;
0461 
0462     if (ts->command_waiting == cmd) {
0463         dev_dbg(&client->dev, "completing command 0x%x\n", cmd);
0464         ts->command_result = result;
0465         complete(&ts->command_done);
0466     } else {
0467         dev_dbg(&client->dev, "command %d not for us\n", cmd);
0468     }
0469 }
0470 
0471 static irqreturn_t zforce_irq(int irq, void *dev_id)
0472 {
0473     struct zforce_ts *ts = dev_id;
0474     struct i2c_client *client = ts->client;
0475 
0476     if (ts->suspended && device_may_wakeup(&client->dev))
0477         pm_wakeup_event(&client->dev, 500);
0478 
0479     return IRQ_WAKE_THREAD;
0480 }
0481 
0482 static irqreturn_t zforce_irq_thread(int irq, void *dev_id)
0483 {
0484     struct zforce_ts *ts = dev_id;
0485     struct i2c_client *client = ts->client;
0486     int ret;
0487     u8 payload_buffer[FRAME_MAXSIZE];
0488     u8 *payload;
0489 
0490     /*
0491      * When still suspended, return.
0492      * Due to the level-interrupt we will get re-triggered later.
0493      */
0494     if (ts->suspended) {
0495         msleep(20);
0496         return IRQ_HANDLED;
0497     }
0498 
0499     dev_dbg(&client->dev, "handling interrupt\n");
0500 
0501     /* Don't emit wakeup events from commands run by zforce_suspend */
0502     if (!ts->suspending && device_may_wakeup(&client->dev))
0503         pm_stay_awake(&client->dev);
0504 
0505     /*
0506      * Run at least once and exit the loop if
0507      * - the optional interrupt GPIO isn't specified
0508      *   (there is only one packet read per ISR invocation, then)
0509      * or
0510      * - the GPIO isn't active any more
0511      *   (packet read until the level GPIO indicates that there is
0512      *    no IRQ any more)
0513      */
0514     do {
0515         ret = zforce_read_packet(ts, payload_buffer);
0516         if (ret < 0) {
0517             dev_err(&client->dev,
0518                 "could not read packet, ret: %d\n", ret);
0519             break;
0520         }
0521 
0522         payload =  &payload_buffer[PAYLOAD_BODY];
0523 
0524         switch (payload[RESPONSE_ID]) {
0525         case NOTIFICATION_TOUCH:
0526             /*
0527              * Always report touch-events received while
0528              * suspending, when being a wakeup source
0529              */
0530             if (ts->suspending && device_may_wakeup(&client->dev))
0531                 pm_wakeup_event(&client->dev, 500);
0532             zforce_touch_event(ts, &payload[RESPONSE_DATA]);
0533             break;
0534 
0535         case NOTIFICATION_BOOTCOMPLETE:
0536             ts->boot_complete = payload[RESPONSE_DATA];
0537             zforce_complete(ts, payload[RESPONSE_ID], 0);
0538             break;
0539 
0540         case RESPONSE_INITIALIZE:
0541         case RESPONSE_DEACTIVATE:
0542         case RESPONSE_SETCONFIG:
0543         case RESPONSE_RESOLUTION:
0544         case RESPONSE_SCANFREQ:
0545             zforce_complete(ts, payload[RESPONSE_ID],
0546                     payload[RESPONSE_DATA]);
0547             break;
0548 
0549         case RESPONSE_STATUS:
0550             /*
0551              * Version Payload Results
0552              * [2:major] [2:minor] [2:build] [2:rev]
0553              */
0554             ts->version_major = (payload[RESPONSE_DATA + 1] << 8) |
0555                         payload[RESPONSE_DATA];
0556             ts->version_minor = (payload[RESPONSE_DATA + 3] << 8) |
0557                         payload[RESPONSE_DATA + 2];
0558             ts->version_build = (payload[RESPONSE_DATA + 5] << 8) |
0559                         payload[RESPONSE_DATA + 4];
0560             ts->version_rev   = (payload[RESPONSE_DATA + 7] << 8) |
0561                         payload[RESPONSE_DATA + 6];
0562             dev_dbg(&ts->client->dev,
0563                 "Firmware Version %04x:%04x %04x:%04x\n",
0564                 ts->version_major, ts->version_minor,
0565                 ts->version_build, ts->version_rev);
0566 
0567             zforce_complete(ts, payload[RESPONSE_ID], 0);
0568             break;
0569 
0570         case NOTIFICATION_INVALID_COMMAND:
0571             dev_err(&ts->client->dev, "invalid command: 0x%x\n",
0572                 payload[RESPONSE_DATA]);
0573             break;
0574 
0575         default:
0576             dev_err(&ts->client->dev,
0577                 "unrecognized response id: 0x%x\n",
0578                 payload[RESPONSE_ID]);
0579             break;
0580         }
0581     } while (gpiod_get_value_cansleep(ts->gpio_int));
0582 
0583     if (!ts->suspending && device_may_wakeup(&client->dev))
0584         pm_relax(&client->dev);
0585 
0586     dev_dbg(&client->dev, "finished interrupt\n");
0587 
0588     return IRQ_HANDLED;
0589 }
0590 
0591 static int zforce_input_open(struct input_dev *dev)
0592 {
0593     struct zforce_ts *ts = input_get_drvdata(dev);
0594 
0595     return zforce_start(ts);
0596 }
0597 
0598 static void zforce_input_close(struct input_dev *dev)
0599 {
0600     struct zforce_ts *ts = input_get_drvdata(dev);
0601     struct i2c_client *client = ts->client;
0602     int ret;
0603 
0604     ret = zforce_stop(ts);
0605     if (ret)
0606         dev_warn(&client->dev, "stopping zforce failed\n");
0607 
0608     return;
0609 }
0610 
0611 static int __maybe_unused zforce_suspend(struct device *dev)
0612 {
0613     struct i2c_client *client = to_i2c_client(dev);
0614     struct zforce_ts *ts = i2c_get_clientdata(client);
0615     struct input_dev *input = ts->input;
0616     int ret = 0;
0617 
0618     mutex_lock(&input->mutex);
0619     ts->suspending = true;
0620 
0621     /*
0622      * When configured as a wakeup source device should always wake
0623      * the system, therefore start device if necessary.
0624      */
0625     if (device_may_wakeup(&client->dev)) {
0626         dev_dbg(&client->dev, "suspend while being a wakeup source\n");
0627 
0628         /* Need to start device, if not open, to be a wakeup source. */
0629         if (!input_device_enabled(input)) {
0630             ret = zforce_start(ts);
0631             if (ret)
0632                 goto unlock;
0633         }
0634 
0635         enable_irq_wake(client->irq);
0636     } else if (input_device_enabled(input)) {
0637         dev_dbg(&client->dev,
0638             "suspend without being a wakeup source\n");
0639 
0640         ret = zforce_stop(ts);
0641         if (ret)
0642             goto unlock;
0643 
0644         disable_irq(client->irq);
0645     }
0646 
0647     ts->suspended = true;
0648 
0649 unlock:
0650     ts->suspending = false;
0651     mutex_unlock(&input->mutex);
0652 
0653     return ret;
0654 }
0655 
0656 static int __maybe_unused zforce_resume(struct device *dev)
0657 {
0658     struct i2c_client *client = to_i2c_client(dev);
0659     struct zforce_ts *ts = i2c_get_clientdata(client);
0660     struct input_dev *input = ts->input;
0661     int ret = 0;
0662 
0663     mutex_lock(&input->mutex);
0664 
0665     ts->suspended = false;
0666 
0667     if (device_may_wakeup(&client->dev)) {
0668         dev_dbg(&client->dev, "resume from being a wakeup source\n");
0669 
0670         disable_irq_wake(client->irq);
0671 
0672         /* need to stop device if it was not open on suspend */
0673         if (!input_device_enabled(input)) {
0674             ret = zforce_stop(ts);
0675             if (ret)
0676                 goto unlock;
0677         }
0678     } else if (input_device_enabled(input)) {
0679         dev_dbg(&client->dev, "resume without being a wakeup source\n");
0680 
0681         enable_irq(client->irq);
0682 
0683         ret = zforce_start(ts);
0684         if (ret < 0)
0685             goto unlock;
0686     }
0687 
0688 unlock:
0689     mutex_unlock(&input->mutex);
0690 
0691     return ret;
0692 }
0693 
0694 static SIMPLE_DEV_PM_OPS(zforce_pm_ops, zforce_suspend, zforce_resume);
0695 
0696 static void zforce_reset(void *data)
0697 {
0698     struct zforce_ts *ts = data;
0699 
0700     zforce_reset_assert(ts);
0701 
0702     udelay(10);
0703 
0704     if (!IS_ERR(ts->reg_vdd))
0705         regulator_disable(ts->reg_vdd);
0706 }
0707 
0708 static struct zforce_ts_platdata *zforce_parse_dt(struct device *dev)
0709 {
0710     struct zforce_ts_platdata *pdata;
0711     struct device_node *np = dev->of_node;
0712 
0713     if (!np)
0714         return ERR_PTR(-ENOENT);
0715 
0716     pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
0717     if (!pdata) {
0718         dev_err(dev, "failed to allocate platform data\n");
0719         return ERR_PTR(-ENOMEM);
0720     }
0721 
0722     if (of_property_read_u32(np, "x-size", &pdata->x_max)) {
0723         dev_err(dev, "failed to get x-size property\n");
0724         return ERR_PTR(-EINVAL);
0725     }
0726 
0727     if (of_property_read_u32(np, "y-size", &pdata->y_max)) {
0728         dev_err(dev, "failed to get y-size property\n");
0729         return ERR_PTR(-EINVAL);
0730     }
0731 
0732     return pdata;
0733 }
0734 
0735 static int zforce_probe(struct i2c_client *client,
0736             const struct i2c_device_id *id)
0737 {
0738     const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
0739     struct zforce_ts *ts;
0740     struct input_dev *input_dev;
0741     int ret;
0742 
0743     if (!pdata) {
0744         pdata = zforce_parse_dt(&client->dev);
0745         if (IS_ERR(pdata))
0746             return PTR_ERR(pdata);
0747     }
0748 
0749     ts = devm_kzalloc(&client->dev, sizeof(struct zforce_ts), GFP_KERNEL);
0750     if (!ts)
0751         return -ENOMEM;
0752 
0753     ts->gpio_rst = devm_gpiod_get_optional(&client->dev, "reset",
0754                            GPIOD_OUT_HIGH);
0755     if (IS_ERR(ts->gpio_rst)) {
0756         ret = PTR_ERR(ts->gpio_rst);
0757         dev_err(&client->dev,
0758             "failed to request reset GPIO: %d\n", ret);
0759         return ret;
0760     }
0761 
0762     if (ts->gpio_rst) {
0763         ts->gpio_int = devm_gpiod_get_optional(&client->dev, "irq",
0764                                GPIOD_IN);
0765         if (IS_ERR(ts->gpio_int)) {
0766             ret = PTR_ERR(ts->gpio_int);
0767             dev_err(&client->dev,
0768                 "failed to request interrupt GPIO: %d\n", ret);
0769             return ret;
0770         }
0771     } else {
0772         /*
0773          * Deprecated GPIO handling for compatibility
0774          * with legacy binding.
0775          */
0776 
0777         /* INT GPIO */
0778         ts->gpio_int = devm_gpiod_get_index(&client->dev, NULL, 0,
0779                             GPIOD_IN);
0780         if (IS_ERR(ts->gpio_int)) {
0781             ret = PTR_ERR(ts->gpio_int);
0782             dev_err(&client->dev,
0783                 "failed to request interrupt GPIO: %d\n", ret);
0784             return ret;
0785         }
0786 
0787         /* RST GPIO */
0788         ts->gpio_rst = devm_gpiod_get_index(&client->dev, NULL, 1,
0789                         GPIOD_OUT_HIGH);
0790         if (IS_ERR(ts->gpio_rst)) {
0791             ret = PTR_ERR(ts->gpio_rst);
0792             dev_err(&client->dev,
0793                 "failed to request reset GPIO: %d\n", ret);
0794             return ret;
0795         }
0796     }
0797 
0798     ts->reg_vdd = devm_regulator_get_optional(&client->dev, "vdd");
0799     if (IS_ERR(ts->reg_vdd)) {
0800         ret = PTR_ERR(ts->reg_vdd);
0801         if (ret == -EPROBE_DEFER)
0802             return ret;
0803     } else {
0804         ret = regulator_enable(ts->reg_vdd);
0805         if (ret)
0806             return ret;
0807 
0808         /*
0809          * according to datasheet add 100us grace time after regular
0810          * regulator enable delay.
0811          */
0812         udelay(100);
0813     }
0814 
0815     ret = devm_add_action(&client->dev, zforce_reset, ts);
0816     if (ret) {
0817         dev_err(&client->dev, "failed to register reset action, %d\n",
0818             ret);
0819 
0820         /* hereafter the regulator will be disabled by the action */
0821         if (!IS_ERR(ts->reg_vdd))
0822             regulator_disable(ts->reg_vdd);
0823 
0824         return ret;
0825     }
0826 
0827     snprintf(ts->phys, sizeof(ts->phys),
0828          "%s/input0", dev_name(&client->dev));
0829 
0830     input_dev = devm_input_allocate_device(&client->dev);
0831     if (!input_dev) {
0832         dev_err(&client->dev, "could not allocate input device\n");
0833         return -ENOMEM;
0834     }
0835 
0836     mutex_init(&ts->access_mutex);
0837     mutex_init(&ts->command_mutex);
0838 
0839     ts->pdata = pdata;
0840     ts->client = client;
0841     ts->input = input_dev;
0842 
0843     input_dev->name = "Neonode zForce touchscreen";
0844     input_dev->phys = ts->phys;
0845     input_dev->id.bustype = BUS_I2C;
0846 
0847     input_dev->open = zforce_input_open;
0848     input_dev->close = zforce_input_close;
0849 
0850     __set_bit(EV_KEY, input_dev->evbit);
0851     __set_bit(EV_SYN, input_dev->evbit);
0852     __set_bit(EV_ABS, input_dev->evbit);
0853 
0854     /* For multi touch */
0855     input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
0856                  pdata->x_max, 0, 0);
0857     input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
0858                  pdata->y_max, 0, 0);
0859 
0860     input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
0861                  ZFORCE_MAX_AREA, 0, 0);
0862     input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0,
0863                  ZFORCE_MAX_AREA, 0, 0);
0864     input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
0865     input_mt_init_slots(input_dev, ZFORCE_REPORT_POINTS, INPUT_MT_DIRECT);
0866 
0867     input_set_drvdata(ts->input, ts);
0868 
0869     init_completion(&ts->command_done);
0870 
0871     /*
0872      * The zforce pulls the interrupt low when it has data ready.
0873      * After it is triggered the isr thread runs until all the available
0874      * packets have been read and the interrupt is high again.
0875      * Therefore we can trigger the interrupt anytime it is low and do
0876      * not need to limit it to the interrupt edge.
0877      */
0878     ret = devm_request_threaded_irq(&client->dev, client->irq,
0879                     zforce_irq, zforce_irq_thread,
0880                     IRQF_TRIGGER_LOW | IRQF_ONESHOT,
0881                     input_dev->name, ts);
0882     if (ret) {
0883         dev_err(&client->dev, "irq %d request failed\n", client->irq);
0884         return ret;
0885     }
0886 
0887     i2c_set_clientdata(client, ts);
0888 
0889     /* let the controller boot */
0890     zforce_reset_deassert(ts);
0891 
0892     ts->command_waiting = NOTIFICATION_BOOTCOMPLETE;
0893     if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0)
0894         dev_warn(&client->dev, "bootcomplete timed out\n");
0895 
0896     /* need to start device to get version information */
0897     ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
0898     if (ret) {
0899         dev_err(&client->dev, "unable to initialize, %d\n", ret);
0900         return ret;
0901     }
0902 
0903     /* this gets the firmware version among other information */
0904     ret = zforce_command_wait(ts, COMMAND_STATUS);
0905     if (ret < 0) {
0906         dev_err(&client->dev, "couldn't get status, %d\n", ret);
0907         zforce_stop(ts);
0908         return ret;
0909     }
0910 
0911     /* stop device and put it into sleep until it is opened */
0912     ret = zforce_stop(ts);
0913     if (ret < 0)
0914         return ret;
0915 
0916     device_set_wakeup_capable(&client->dev, true);
0917 
0918     ret = input_register_device(input_dev);
0919     if (ret) {
0920         dev_err(&client->dev, "could not register input device, %d\n",
0921             ret);
0922         return ret;
0923     }
0924 
0925     return 0;
0926 }
0927 
0928 static struct i2c_device_id zforce_idtable[] = {
0929     { "zforce-ts", 0 },
0930     { }
0931 };
0932 MODULE_DEVICE_TABLE(i2c, zforce_idtable);
0933 
0934 #ifdef CONFIG_OF
0935 static const struct of_device_id zforce_dt_idtable[] = {
0936     { .compatible = "neonode,zforce" },
0937     {},
0938 };
0939 MODULE_DEVICE_TABLE(of, zforce_dt_idtable);
0940 #endif
0941 
0942 static struct i2c_driver zforce_driver = {
0943     .driver = {
0944         .name   = "zforce-ts",
0945         .pm = &zforce_pm_ops,
0946         .of_match_table = of_match_ptr(zforce_dt_idtable),
0947     },
0948     .probe      = zforce_probe,
0949     .id_table   = zforce_idtable,
0950 };
0951 
0952 module_i2c_driver(zforce_driver);
0953 
0954 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
0955 MODULE_DESCRIPTION("zForce TouchScreen Driver");
0956 MODULE_LICENSE("GPL");