zforce_ts.c 22 KB

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