zforce_ts.c 23 KB

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