zforce_ts.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. if (zforce_setconfig(ts, SETCONFIG_DUALTOUCH)) {
  232. dev_err(&client->dev, "Unable to set config\n");
  233. goto error;
  234. }
  235. /* start sending touch events */
  236. ret = zforce_command(ts, COMMAND_DATAREQUEST);
  237. if (ret) {
  238. dev_err(&client->dev, "Unable to request data\n");
  239. goto error;
  240. }
  241. /*
  242. * Per NN, initial cal. take max. of 200msec.
  243. * Allow time to complete this calibration
  244. */
  245. msleep(200);
  246. return 0;
  247. error:
  248. zforce_command_wait(ts, COMMAND_DEACTIVATE);
  249. return ret;
  250. }
  251. static int zforce_stop(struct zforce_ts *ts)
  252. {
  253. struct i2c_client *client = ts->client;
  254. int ret;
  255. dev_dbg(&client->dev, "stopping device\n");
  256. /* Deactivates touch sensing and puts the device into sleep. */
  257. ret = zforce_command_wait(ts, COMMAND_DEACTIVATE);
  258. if (ret != 0) {
  259. dev_err(&client->dev, "could not deactivate device, %d\n",
  260. ret);
  261. return ret;
  262. }
  263. return 0;
  264. }
  265. static int zforce_touch_event(struct zforce_ts *ts, u8 *payload)
  266. {
  267. struct i2c_client *client = ts->client;
  268. const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
  269. struct zforce_point point;
  270. int count, i, num = 0;
  271. count = payload[0];
  272. if (count > ZFORCE_REPORT_POINTS) {
  273. dev_warn(&client->dev, "to many coordinates %d, expected max %d\n",
  274. count, ZFORCE_REPORT_POINTS);
  275. count = ZFORCE_REPORT_POINTS;
  276. }
  277. for (i = 0; i < count; i++) {
  278. point.coord_x =
  279. payload[9 * i + 2] << 8 | payload[9 * i + 1];
  280. point.coord_y =
  281. payload[9 * i + 4] << 8 | payload[9 * i + 3];
  282. if (point.coord_x > pdata->x_max ||
  283. point.coord_y > pdata->y_max) {
  284. dev_warn(&client->dev, "coordinates (%d,%d) invalid\n",
  285. point.coord_x, point.coord_y);
  286. point.coord_x = point.coord_y = 0;
  287. }
  288. point.state = payload[9 * i + 5] & 0x03;
  289. point.id = (payload[9 * i + 5] & 0xfc) >> 2;
  290. /* determine touch major, minor and orientation */
  291. point.area_major = max(payload[9 * i + 6],
  292. payload[9 * i + 7]);
  293. point.area_minor = min(payload[9 * i + 6],
  294. payload[9 * i + 7]);
  295. point.orientation = payload[9 * i + 6] > payload[9 * i + 7];
  296. point.pressure = payload[9 * i + 8];
  297. point.prblty = payload[9 * i + 9];
  298. dev_dbg(&client->dev,
  299. "point %d/%d: state %d, id %d, pressure %d, prblty %d, x %d, y %d, amajor %d, aminor %d, ori %d\n",
  300. i, count, point.state, point.id,
  301. point.pressure, point.prblty,
  302. point.coord_x, point.coord_y,
  303. point.area_major, point.area_minor,
  304. point.orientation);
  305. /* the zforce id starts with "1", so needs to be decreased */
  306. input_mt_slot(ts->input, point.id - 1);
  307. input_mt_report_slot_state(ts->input, MT_TOOL_FINGER,
  308. point.state != STATE_UP);
  309. if (point.state != STATE_UP) {
  310. input_report_abs(ts->input, ABS_MT_POSITION_X,
  311. point.coord_x);
  312. input_report_abs(ts->input, ABS_MT_POSITION_Y,
  313. point.coord_y);
  314. input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR,
  315. point.area_major);
  316. input_report_abs(ts->input, ABS_MT_TOUCH_MINOR,
  317. point.area_minor);
  318. input_report_abs(ts->input, ABS_MT_ORIENTATION,
  319. point.orientation);
  320. num++;
  321. }
  322. }
  323. input_mt_sync_frame(ts->input);
  324. input_mt_report_finger_count(ts->input, num);
  325. input_sync(ts->input);
  326. return 0;
  327. }
  328. static int zforce_read_packet(struct zforce_ts *ts, u8 *buf)
  329. {
  330. struct i2c_client *client = ts->client;
  331. int ret;
  332. mutex_lock(&ts->access_mutex);
  333. /* read 2 byte message header */
  334. ret = i2c_master_recv(client, buf, 2);
  335. if (ret < 0) {
  336. dev_err(&client->dev, "error reading header: %d\n", ret);
  337. goto unlock;
  338. }
  339. if (buf[PAYLOAD_HEADER] != FRAME_START) {
  340. dev_err(&client->dev, "invalid frame start: %d\n", buf[0]);
  341. ret = -EIO;
  342. goto unlock;
  343. }
  344. if (buf[PAYLOAD_LENGTH] <= 0 || buf[PAYLOAD_LENGTH] > 255) {
  345. dev_err(&client->dev, "invalid payload length: %d\n",
  346. buf[PAYLOAD_LENGTH]);
  347. ret = -EIO;
  348. goto unlock;
  349. }
  350. /* read the message */
  351. ret = i2c_master_recv(client, &buf[PAYLOAD_BODY], buf[PAYLOAD_LENGTH]);
  352. if (ret < 0) {
  353. dev_err(&client->dev, "error reading payload: %d\n", ret);
  354. goto unlock;
  355. }
  356. dev_dbg(&client->dev, "read %d bytes for response command 0x%x\n",
  357. buf[PAYLOAD_LENGTH], buf[PAYLOAD_BODY]);
  358. unlock:
  359. mutex_unlock(&ts->access_mutex);
  360. return ret;
  361. }
  362. static void zforce_complete(struct zforce_ts *ts, int cmd, int result)
  363. {
  364. struct i2c_client *client = ts->client;
  365. if (ts->command_waiting == cmd) {
  366. dev_dbg(&client->dev, "completing command 0x%x\n", cmd);
  367. ts->command_result = result;
  368. complete(&ts->command_done);
  369. } else {
  370. dev_dbg(&client->dev, "command %d not for us\n", cmd);
  371. }
  372. }
  373. static irqreturn_t zforce_interrupt(int irq, void *dev_id)
  374. {
  375. struct zforce_ts *ts = dev_id;
  376. struct i2c_client *client = ts->client;
  377. const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
  378. int ret;
  379. u8 payload_buffer[512];
  380. u8 *payload;
  381. /*
  382. * When suspended, emit a wakeup signal if necessary and return.
  383. * Due to the level-interrupt we will get re-triggered later.
  384. */
  385. if (ts->suspended) {
  386. if (device_may_wakeup(&client->dev))
  387. pm_wakeup_event(&client->dev, 500);
  388. msleep(20);
  389. return IRQ_HANDLED;
  390. }
  391. dev_dbg(&client->dev, "handling interrupt\n");
  392. /* Don't emit wakeup events from commands run by zforce_suspend */
  393. if (!ts->suspending && device_may_wakeup(&client->dev))
  394. pm_stay_awake(&client->dev);
  395. while (!gpio_get_value(pdata->gpio_int)) {
  396. ret = zforce_read_packet(ts, payload_buffer);
  397. if (ret < 0) {
  398. dev_err(&client->dev, "could not read packet, ret: %d\n",
  399. ret);
  400. break;
  401. }
  402. payload = &payload_buffer[PAYLOAD_BODY];
  403. switch (payload[RESPONSE_ID]) {
  404. case NOTIFICATION_TOUCH:
  405. /*
  406. * Always report touch-events received while
  407. * suspending, when being a wakeup source
  408. */
  409. if (ts->suspending && device_may_wakeup(&client->dev))
  410. pm_wakeup_event(&client->dev, 500);
  411. zforce_touch_event(ts, &payload[RESPONSE_DATA]);
  412. break;
  413. case NOTIFICATION_BOOTCOMPLETE:
  414. ts->boot_complete = payload[RESPONSE_DATA];
  415. zforce_complete(ts, payload[RESPONSE_ID], 0);
  416. break;
  417. case RESPONSE_INITIALIZE:
  418. case RESPONSE_DEACTIVATE:
  419. case RESPONSE_SETCONFIG:
  420. case RESPONSE_RESOLUTION:
  421. case RESPONSE_SCANFREQ:
  422. zforce_complete(ts, payload[RESPONSE_ID],
  423. payload[RESPONSE_DATA]);
  424. break;
  425. case RESPONSE_STATUS:
  426. /*
  427. * Version Payload Results
  428. * [2:major] [2:minor] [2:build] [2:rev]
  429. */
  430. ts->version_major = (payload[RESPONSE_DATA + 1] << 8) |
  431. payload[RESPONSE_DATA];
  432. ts->version_minor = (payload[RESPONSE_DATA + 3] << 8) |
  433. payload[RESPONSE_DATA + 2];
  434. ts->version_build = (payload[RESPONSE_DATA + 5] << 8) |
  435. payload[RESPONSE_DATA + 4];
  436. ts->version_rev = (payload[RESPONSE_DATA + 7] << 8) |
  437. payload[RESPONSE_DATA + 6];
  438. dev_dbg(&ts->client->dev, "Firmware Version %04x:%04x %04x:%04x\n",
  439. ts->version_major, ts->version_minor,
  440. ts->version_build, ts->version_rev);
  441. zforce_complete(ts, payload[RESPONSE_ID], 0);
  442. break;
  443. case NOTIFICATION_INVALID_COMMAND:
  444. dev_err(&ts->client->dev, "invalid command: 0x%x\n",
  445. payload[RESPONSE_DATA]);
  446. break;
  447. default:
  448. dev_err(&ts->client->dev, "unrecognized response id: 0x%x\n",
  449. payload[RESPONSE_ID]);
  450. break;
  451. }
  452. }
  453. if (!ts->suspending && device_may_wakeup(&client->dev))
  454. pm_relax(&client->dev);
  455. dev_dbg(&client->dev, "finished interrupt\n");
  456. return IRQ_HANDLED;
  457. }
  458. static int zforce_input_open(struct input_dev *dev)
  459. {
  460. struct zforce_ts *ts = input_get_drvdata(dev);
  461. int ret;
  462. ret = zforce_start(ts);
  463. if (ret)
  464. return ret;
  465. return 0;
  466. }
  467. static void zforce_input_close(struct input_dev *dev)
  468. {
  469. struct zforce_ts *ts = input_get_drvdata(dev);
  470. struct i2c_client *client = ts->client;
  471. int ret;
  472. ret = zforce_stop(ts);
  473. if (ret)
  474. dev_warn(&client->dev, "stopping zforce failed\n");
  475. return;
  476. }
  477. #ifdef CONFIG_PM_SLEEP
  478. static int zforce_suspend(struct device *dev)
  479. {
  480. struct i2c_client *client = to_i2c_client(dev);
  481. struct zforce_ts *ts = i2c_get_clientdata(client);
  482. struct input_dev *input = ts->input;
  483. int ret = 0;
  484. mutex_lock(&input->mutex);
  485. ts->suspending = true;
  486. /*
  487. * When configured as a wakeup source device should always wake
  488. * the system, therefore start device if necessary.
  489. */
  490. if (device_may_wakeup(&client->dev)) {
  491. dev_dbg(&client->dev, "suspend while being a wakeup source\n");
  492. /* Need to start device, if not open, to be a wakeup source. */
  493. if (!input->users) {
  494. ret = zforce_start(ts);
  495. if (ret)
  496. goto unlock;
  497. }
  498. enable_irq_wake(client->irq);
  499. } else if (input->users) {
  500. dev_dbg(&client->dev, "suspend without being a wakeup source\n");
  501. ret = zforce_stop(ts);
  502. if (ret)
  503. goto unlock;
  504. disable_irq(client->irq);
  505. }
  506. ts->suspended = true;
  507. unlock:
  508. ts->suspending = false;
  509. mutex_unlock(&input->mutex);
  510. return ret;
  511. }
  512. static int zforce_resume(struct device *dev)
  513. {
  514. struct i2c_client *client = to_i2c_client(dev);
  515. struct zforce_ts *ts = i2c_get_clientdata(client);
  516. struct input_dev *input = ts->input;
  517. int ret = 0;
  518. mutex_lock(&input->mutex);
  519. ts->suspended = false;
  520. if (device_may_wakeup(&client->dev)) {
  521. dev_dbg(&client->dev, "resume from being a wakeup source\n");
  522. disable_irq_wake(client->irq);
  523. /* need to stop device if it was not open on suspend */
  524. if (!input->users) {
  525. ret = zforce_stop(ts);
  526. if (ret)
  527. goto unlock;
  528. }
  529. } else if (input->users) {
  530. dev_dbg(&client->dev, "resume without being a wakeup source\n");
  531. enable_irq(client->irq);
  532. ret = zforce_start(ts);
  533. if (ret < 0)
  534. goto unlock;
  535. }
  536. unlock:
  537. mutex_unlock(&input->mutex);
  538. return ret;
  539. }
  540. #endif
  541. static SIMPLE_DEV_PM_OPS(zforce_pm_ops, zforce_suspend, zforce_resume);
  542. static void zforce_reset(void *data)
  543. {
  544. struct zforce_ts *ts = data;
  545. gpio_set_value(ts->pdata->gpio_rst, 0);
  546. }
  547. static int zforce_probe(struct i2c_client *client,
  548. const struct i2c_device_id *id)
  549. {
  550. const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
  551. struct zforce_ts *ts;
  552. struct input_dev *input_dev;
  553. int ret;
  554. if (!pdata)
  555. return -EINVAL;
  556. ts = devm_kzalloc(&client->dev, sizeof(struct zforce_ts), GFP_KERNEL);
  557. if (!ts)
  558. return -ENOMEM;
  559. ret = devm_gpio_request_one(&client->dev, pdata->gpio_int, GPIOF_IN,
  560. "zforce_ts_int");
  561. if (ret) {
  562. dev_err(&client->dev, "request of gpio %d failed, %d\n",
  563. pdata->gpio_int, ret);
  564. return ret;
  565. }
  566. ret = devm_gpio_request_one(&client->dev, pdata->gpio_rst,
  567. GPIOF_OUT_INIT_LOW, "zforce_ts_rst");
  568. if (ret) {
  569. dev_err(&client->dev, "request of gpio %d failed, %d\n",
  570. pdata->gpio_rst, ret);
  571. return ret;
  572. }
  573. ret = devm_add_action(&client->dev, zforce_reset, ts);
  574. if (ret) {
  575. dev_err(&client->dev, "failed to register reset action, %d\n",
  576. ret);
  577. return ret;
  578. }
  579. snprintf(ts->phys, sizeof(ts->phys),
  580. "%s/input0", dev_name(&client->dev));
  581. input_dev = devm_input_allocate_device(&client->dev);
  582. if (!input_dev) {
  583. dev_err(&client->dev, "could not allocate input device\n");
  584. return -ENOMEM;
  585. }
  586. mutex_init(&ts->access_mutex);
  587. mutex_init(&ts->command_mutex);
  588. ts->pdata = pdata;
  589. ts->client = client;
  590. ts->input = input_dev;
  591. input_dev->name = "Neonode zForce touchscreen";
  592. input_dev->phys = ts->phys;
  593. input_dev->id.bustype = BUS_I2C;
  594. input_dev->open = zforce_input_open;
  595. input_dev->close = zforce_input_close;
  596. __set_bit(EV_KEY, input_dev->evbit);
  597. __set_bit(EV_SYN, input_dev->evbit);
  598. __set_bit(EV_ABS, input_dev->evbit);
  599. /* For multi touch */
  600. input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
  601. pdata->x_max, 0, 0);
  602. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
  603. pdata->y_max, 0, 0);
  604. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
  605. ZFORCE_MAX_AREA, 0, 0);
  606. input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0,
  607. ZFORCE_MAX_AREA, 0, 0);
  608. input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
  609. input_mt_init_slots(input_dev, ZFORCE_REPORT_POINTS, INPUT_MT_DIRECT);
  610. input_set_drvdata(ts->input, ts);
  611. init_completion(&ts->command_done);
  612. /*
  613. * The zforce pulls the interrupt low when it has data ready.
  614. * After it is triggered the isr thread runs until all the available
  615. * packets have been read and the interrupt is high again.
  616. * Therefore we can trigger the interrupt anytime it is low and do
  617. * not need to limit it to the interrupt edge.
  618. */
  619. ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  620. zforce_interrupt,
  621. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  622. input_dev->name, ts);
  623. if (ret) {
  624. dev_err(&client->dev, "irq %d request failed\n", client->irq);
  625. return ret;
  626. }
  627. i2c_set_clientdata(client, ts);
  628. /* let the controller boot */
  629. gpio_set_value(pdata->gpio_rst, 1);
  630. ts->command_waiting = NOTIFICATION_BOOTCOMPLETE;
  631. if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0)
  632. dev_warn(&client->dev, "bootcomplete timed out\n");
  633. /* need to start device to get version information */
  634. ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
  635. if (ret) {
  636. dev_err(&client->dev, "unable to initialize, %d\n", ret);
  637. return ret;
  638. }
  639. /* this gets the firmware version among other informations */
  640. ret = zforce_command_wait(ts, COMMAND_STATUS);
  641. if (ret < 0) {
  642. dev_err(&client->dev, "couldn't get status, %d\n", ret);
  643. zforce_stop(ts);
  644. return ret;
  645. }
  646. /* stop device and put it into sleep until it is opened */
  647. ret = zforce_stop(ts);
  648. if (ret < 0)
  649. return ret;
  650. device_set_wakeup_capable(&client->dev, true);
  651. ret = input_register_device(input_dev);
  652. if (ret) {
  653. dev_err(&client->dev, "could not register input device, %d\n",
  654. ret);
  655. return ret;
  656. }
  657. return 0;
  658. }
  659. static struct i2c_device_id zforce_idtable[] = {
  660. { "zforce-ts", 0 },
  661. { }
  662. };
  663. MODULE_DEVICE_TABLE(i2c, zforce_idtable);
  664. static struct i2c_driver zforce_driver = {
  665. .driver = {
  666. .owner = THIS_MODULE,
  667. .name = "zforce-ts",
  668. .pm = &zforce_pm_ops,
  669. },
  670. .probe = zforce_probe,
  671. .id_table = zforce_idtable,
  672. };
  673. module_i2c_driver(zforce_driver);
  674. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  675. MODULE_DESCRIPTION("zForce TouchScreen Driver");
  676. MODULE_LICENSE("GPL");