zforce_ts.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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_interrupt(int irq, void *dev_id)
  375. {
  376. struct zforce_ts *ts = dev_id;
  377. struct i2c_client *client = ts->client;
  378. const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
  379. int ret;
  380. u8 payload_buffer[512];
  381. u8 *payload;
  382. /*
  383. * When suspended, emit a wakeup signal if necessary and return.
  384. * Due to the level-interrupt we will get re-triggered later.
  385. */
  386. if (ts->suspended) {
  387. if (device_may_wakeup(&client->dev))
  388. pm_wakeup_event(&client->dev, 500);
  389. msleep(20);
  390. return IRQ_HANDLED;
  391. }
  392. dev_dbg(&client->dev, "handling interrupt\n");
  393. /* Don't emit wakeup events from commands run by zforce_suspend */
  394. if (!ts->suspending && device_may_wakeup(&client->dev))
  395. pm_stay_awake(&client->dev);
  396. while (!gpio_get_value(pdata->gpio_int)) {
  397. ret = zforce_read_packet(ts, payload_buffer);
  398. if (ret < 0) {
  399. dev_err(&client->dev, "could not read packet, ret: %d\n",
  400. ret);
  401. break;
  402. }
  403. payload = &payload_buffer[PAYLOAD_BODY];
  404. switch (payload[RESPONSE_ID]) {
  405. case NOTIFICATION_TOUCH:
  406. /*
  407. * Always report touch-events received while
  408. * suspending, when being a wakeup source
  409. */
  410. if (ts->suspending && device_may_wakeup(&client->dev))
  411. pm_wakeup_event(&client->dev, 500);
  412. zforce_touch_event(ts, &payload[RESPONSE_DATA]);
  413. break;
  414. case NOTIFICATION_BOOTCOMPLETE:
  415. ts->boot_complete = payload[RESPONSE_DATA];
  416. zforce_complete(ts, payload[RESPONSE_ID], 0);
  417. break;
  418. case RESPONSE_INITIALIZE:
  419. case RESPONSE_DEACTIVATE:
  420. case RESPONSE_SETCONFIG:
  421. case RESPONSE_RESOLUTION:
  422. case RESPONSE_SCANFREQ:
  423. zforce_complete(ts, payload[RESPONSE_ID],
  424. payload[RESPONSE_DATA]);
  425. break;
  426. case RESPONSE_STATUS:
  427. /*
  428. * Version Payload Results
  429. * [2:major] [2:minor] [2:build] [2:rev]
  430. */
  431. ts->version_major = (payload[RESPONSE_DATA + 1] << 8) |
  432. payload[RESPONSE_DATA];
  433. ts->version_minor = (payload[RESPONSE_DATA + 3] << 8) |
  434. payload[RESPONSE_DATA + 2];
  435. ts->version_build = (payload[RESPONSE_DATA + 5] << 8) |
  436. payload[RESPONSE_DATA + 4];
  437. ts->version_rev = (payload[RESPONSE_DATA + 7] << 8) |
  438. payload[RESPONSE_DATA + 6];
  439. dev_dbg(&ts->client->dev, "Firmware Version %04x:%04x %04x:%04x\n",
  440. ts->version_major, ts->version_minor,
  441. ts->version_build, ts->version_rev);
  442. zforce_complete(ts, payload[RESPONSE_ID], 0);
  443. break;
  444. case NOTIFICATION_INVALID_COMMAND:
  445. dev_err(&ts->client->dev, "invalid command: 0x%x\n",
  446. payload[RESPONSE_DATA]);
  447. break;
  448. default:
  449. dev_err(&ts->client->dev, "unrecognized response id: 0x%x\n",
  450. payload[RESPONSE_ID]);
  451. break;
  452. }
  453. }
  454. if (!ts->suspending && device_may_wakeup(&client->dev))
  455. pm_relax(&client->dev);
  456. dev_dbg(&client->dev, "finished interrupt\n");
  457. return IRQ_HANDLED;
  458. }
  459. static int zforce_input_open(struct input_dev *dev)
  460. {
  461. struct zforce_ts *ts = input_get_drvdata(dev);
  462. int ret;
  463. ret = zforce_start(ts);
  464. if (ret)
  465. return ret;
  466. return 0;
  467. }
  468. static void zforce_input_close(struct input_dev *dev)
  469. {
  470. struct zforce_ts *ts = input_get_drvdata(dev);
  471. struct i2c_client *client = ts->client;
  472. int ret;
  473. ret = zforce_stop(ts);
  474. if (ret)
  475. dev_warn(&client->dev, "stopping zforce failed\n");
  476. return;
  477. }
  478. #ifdef CONFIG_PM_SLEEP
  479. static int zforce_suspend(struct device *dev)
  480. {
  481. struct i2c_client *client = to_i2c_client(dev);
  482. struct zforce_ts *ts = i2c_get_clientdata(client);
  483. struct input_dev *input = ts->input;
  484. int ret = 0;
  485. mutex_lock(&input->mutex);
  486. ts->suspending = true;
  487. /*
  488. * When configured as a wakeup source device should always wake
  489. * the system, therefore start device if necessary.
  490. */
  491. if (device_may_wakeup(&client->dev)) {
  492. dev_dbg(&client->dev, "suspend while being a wakeup source\n");
  493. /* Need to start device, if not open, to be a wakeup source. */
  494. if (!input->users) {
  495. ret = zforce_start(ts);
  496. if (ret)
  497. goto unlock;
  498. }
  499. enable_irq_wake(client->irq);
  500. } else if (input->users) {
  501. dev_dbg(&client->dev, "suspend without being a wakeup source\n");
  502. ret = zforce_stop(ts);
  503. if (ret)
  504. goto unlock;
  505. disable_irq(client->irq);
  506. }
  507. ts->suspended = true;
  508. unlock:
  509. ts->suspending = false;
  510. mutex_unlock(&input->mutex);
  511. return ret;
  512. }
  513. static int zforce_resume(struct device *dev)
  514. {
  515. struct i2c_client *client = to_i2c_client(dev);
  516. struct zforce_ts *ts = i2c_get_clientdata(client);
  517. struct input_dev *input = ts->input;
  518. int ret = 0;
  519. mutex_lock(&input->mutex);
  520. ts->suspended = false;
  521. if (device_may_wakeup(&client->dev)) {
  522. dev_dbg(&client->dev, "resume from being a wakeup source\n");
  523. disable_irq_wake(client->irq);
  524. /* need to stop device if it was not open on suspend */
  525. if (!input->users) {
  526. ret = zforce_stop(ts);
  527. if (ret)
  528. goto unlock;
  529. }
  530. } else if (input->users) {
  531. dev_dbg(&client->dev, "resume without being a wakeup source\n");
  532. enable_irq(client->irq);
  533. ret = zforce_start(ts);
  534. if (ret < 0)
  535. goto unlock;
  536. }
  537. unlock:
  538. mutex_unlock(&input->mutex);
  539. return ret;
  540. }
  541. #endif
  542. static SIMPLE_DEV_PM_OPS(zforce_pm_ops, zforce_suspend, zforce_resume);
  543. static void zforce_reset(void *data)
  544. {
  545. struct zforce_ts *ts = data;
  546. gpio_set_value(ts->pdata->gpio_rst, 0);
  547. }
  548. static int zforce_probe(struct i2c_client *client,
  549. const struct i2c_device_id *id)
  550. {
  551. const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
  552. struct zforce_ts *ts;
  553. struct input_dev *input_dev;
  554. int ret;
  555. if (!pdata)
  556. return -EINVAL;
  557. ts = devm_kzalloc(&client->dev, sizeof(struct zforce_ts), GFP_KERNEL);
  558. if (!ts)
  559. return -ENOMEM;
  560. ret = devm_gpio_request_one(&client->dev, pdata->gpio_int, GPIOF_IN,
  561. "zforce_ts_int");
  562. if (ret) {
  563. dev_err(&client->dev, "request of gpio %d failed, %d\n",
  564. pdata->gpio_int, ret);
  565. return ret;
  566. }
  567. ret = devm_gpio_request_one(&client->dev, pdata->gpio_rst,
  568. GPIOF_OUT_INIT_LOW, "zforce_ts_rst");
  569. if (ret) {
  570. dev_err(&client->dev, "request of gpio %d failed, %d\n",
  571. pdata->gpio_rst, ret);
  572. return ret;
  573. }
  574. ret = devm_add_action(&client->dev, zforce_reset, ts);
  575. if (ret) {
  576. dev_err(&client->dev, "failed to register reset action, %d\n",
  577. ret);
  578. return ret;
  579. }
  580. snprintf(ts->phys, sizeof(ts->phys),
  581. "%s/input0", dev_name(&client->dev));
  582. input_dev = devm_input_allocate_device(&client->dev);
  583. if (!input_dev) {
  584. dev_err(&client->dev, "could not allocate input device\n");
  585. return -ENOMEM;
  586. }
  587. mutex_init(&ts->access_mutex);
  588. mutex_init(&ts->command_mutex);
  589. ts->pdata = pdata;
  590. ts->client = client;
  591. ts->input = input_dev;
  592. input_dev->name = "Neonode zForce touchscreen";
  593. input_dev->phys = ts->phys;
  594. input_dev->id.bustype = BUS_I2C;
  595. input_dev->open = zforce_input_open;
  596. input_dev->close = zforce_input_close;
  597. __set_bit(EV_KEY, input_dev->evbit);
  598. __set_bit(EV_SYN, input_dev->evbit);
  599. __set_bit(EV_ABS, input_dev->evbit);
  600. /* For multi touch */
  601. input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
  602. pdata->x_max, 0, 0);
  603. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
  604. pdata->y_max, 0, 0);
  605. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
  606. ZFORCE_MAX_AREA, 0, 0);
  607. input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0,
  608. ZFORCE_MAX_AREA, 0, 0);
  609. input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
  610. input_mt_init_slots(input_dev, ZFORCE_REPORT_POINTS, INPUT_MT_DIRECT);
  611. input_set_drvdata(ts->input, ts);
  612. init_completion(&ts->command_done);
  613. /*
  614. * The zforce pulls the interrupt low when it has data ready.
  615. * After it is triggered the isr thread runs until all the available
  616. * packets have been read and the interrupt is high again.
  617. * Therefore we can trigger the interrupt anytime it is low and do
  618. * not need to limit it to the interrupt edge.
  619. */
  620. ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  621. zforce_interrupt,
  622. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  623. input_dev->name, ts);
  624. if (ret) {
  625. dev_err(&client->dev, "irq %d request failed\n", client->irq);
  626. return ret;
  627. }
  628. i2c_set_clientdata(client, ts);
  629. /* let the controller boot */
  630. gpio_set_value(pdata->gpio_rst, 1);
  631. ts->command_waiting = NOTIFICATION_BOOTCOMPLETE;
  632. if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0)
  633. dev_warn(&client->dev, "bootcomplete timed out\n");
  634. /* need to start device to get version information */
  635. ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
  636. if (ret) {
  637. dev_err(&client->dev, "unable to initialize, %d\n", ret);
  638. return ret;
  639. }
  640. /* this gets the firmware version among other informations */
  641. ret = zforce_command_wait(ts, COMMAND_STATUS);
  642. if (ret < 0) {
  643. dev_err(&client->dev, "couldn't get status, %d\n", ret);
  644. zforce_stop(ts);
  645. return ret;
  646. }
  647. /* stop device and put it into sleep until it is opened */
  648. ret = zforce_stop(ts);
  649. if (ret < 0)
  650. return ret;
  651. device_set_wakeup_capable(&client->dev, true);
  652. ret = input_register_device(input_dev);
  653. if (ret) {
  654. dev_err(&client->dev, "could not register input device, %d\n",
  655. ret);
  656. return ret;
  657. }
  658. return 0;
  659. }
  660. static struct i2c_device_id zforce_idtable[] = {
  661. { "zforce-ts", 0 },
  662. { }
  663. };
  664. MODULE_DEVICE_TABLE(i2c, zforce_idtable);
  665. static struct i2c_driver zforce_driver = {
  666. .driver = {
  667. .owner = THIS_MODULE,
  668. .name = "zforce-ts",
  669. .pm = &zforce_pm_ops,
  670. },
  671. .probe = zforce_probe,
  672. .id_table = zforce_idtable,
  673. };
  674. module_i2c_driver(zforce_driver);
  675. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  676. MODULE_DESCRIPTION("zForce TouchScreen Driver");
  677. MODULE_LICENSE("GPL");