zforce_ts.c 23 KB

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