zforce_ts.c 21 KB

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