zforce_ts.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012-2013 MundoReader S.L.
  4. * Author: Heiko Stuebner <[email protected]>
  5. *
  6. * based in parts on Nook zforce driver
  7. *
  8. * Copyright (C) 2010 Barnes & Noble, Inc.
  9. * Author: Pieter Truter<[email protected]>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/hrtimer.h>
  13. #include <linux/slab.h>
  14. #include <linux/input.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/i2c.h>
  17. #include <linux/delay.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/device.h>
  20. #include <linux/sysfs.h>
  21. #include <linux/input/mt.h>
  22. #include <linux/platform_data/zforce_ts.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/of.h>
  25. #define WAIT_TIMEOUT msecs_to_jiffies(1000)
  26. #define FRAME_START 0xee
  27. #define FRAME_MAXSIZE 257
  28. /* Offsets of the different parts of the payload the controller sends */
  29. #define PAYLOAD_HEADER 0
  30. #define PAYLOAD_LENGTH 1
  31. #define PAYLOAD_BODY 2
  32. /* Response offsets */
  33. #define RESPONSE_ID 0
  34. #define RESPONSE_DATA 1
  35. /* Commands */
  36. #define COMMAND_DEACTIVATE 0x00
  37. #define COMMAND_INITIALIZE 0x01
  38. #define COMMAND_RESOLUTION 0x02
  39. #define COMMAND_SETCONFIG 0x03
  40. #define COMMAND_DATAREQUEST 0x04
  41. #define COMMAND_SCANFREQ 0x08
  42. #define COMMAND_STATUS 0X1e
  43. /*
  44. * Responses the controller sends as a result of
  45. * command requests
  46. */
  47. #define RESPONSE_DEACTIVATE 0x00
  48. #define RESPONSE_INITIALIZE 0x01
  49. #define RESPONSE_RESOLUTION 0x02
  50. #define RESPONSE_SETCONFIG 0x03
  51. #define RESPONSE_SCANFREQ 0x08
  52. #define RESPONSE_STATUS 0X1e
  53. /*
  54. * Notifications are sent by the touch controller without
  55. * being requested by the driver and include for example
  56. * touch indications
  57. */
  58. #define NOTIFICATION_TOUCH 0x04
  59. #define NOTIFICATION_BOOTCOMPLETE 0x07
  60. #define NOTIFICATION_OVERRUN 0x25
  61. #define NOTIFICATION_PROXIMITY 0x26
  62. #define NOTIFICATION_INVALID_COMMAND 0xfe
  63. #define ZFORCE_REPORT_POINTS 2
  64. #define ZFORCE_MAX_AREA 0xff
  65. #define STATE_DOWN 0
  66. #define STATE_MOVE 1
  67. #define STATE_UP 2
  68. #define SETCONFIG_DUALTOUCH (1 << 0)
  69. struct zforce_point {
  70. int coord_x;
  71. int coord_y;
  72. int state;
  73. int id;
  74. int area_major;
  75. int area_minor;
  76. int orientation;
  77. int pressure;
  78. int prblty;
  79. };
  80. /*
  81. * @client the i2c_client
  82. * @input the input device
  83. * @suspending in the process of going to suspend (don't emit wakeup
  84. * events for commands executed to suspend the device)
  85. * @suspended device suspended
  86. * @access_mutex serialize i2c-access, to keep multipart reads together
  87. * @command_done completion to wait for the command result
  88. * @command_mutex serialize commands sent to the ic
  89. * @command_waiting the id of the command that is currently waiting
  90. * for a result
  91. * @command_result returned result of the command
  92. */
  93. struct zforce_ts {
  94. struct i2c_client *client;
  95. struct input_dev *input;
  96. const struct zforce_ts_platdata *pdata;
  97. char phys[32];
  98. struct regulator *reg_vdd;
  99. struct gpio_desc *gpio_int;
  100. struct gpio_desc *gpio_rst;
  101. bool suspending;
  102. bool suspended;
  103. bool boot_complete;
  104. /* Firmware version information */
  105. u16 version_major;
  106. u16 version_minor;
  107. u16 version_build;
  108. u16 version_rev;
  109. struct mutex access_mutex;
  110. struct completion command_done;
  111. struct mutex command_mutex;
  112. int command_waiting;
  113. int command_result;
  114. };
  115. static int zforce_command(struct zforce_ts *ts, u8 cmd)
  116. {
  117. struct i2c_client *client = ts->client;
  118. char buf[3];
  119. int ret;
  120. dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
  121. buf[0] = FRAME_START;
  122. buf[1] = 1; /* data size, command only */
  123. buf[2] = cmd;
  124. mutex_lock(&ts->access_mutex);
  125. ret = i2c_master_send(client, &buf[0], ARRAY_SIZE(buf));
  126. mutex_unlock(&ts->access_mutex);
  127. if (ret < 0) {
  128. dev_err(&client->dev, "i2c send data request error: %d\n", ret);
  129. return ret;
  130. }
  131. return 0;
  132. }
  133. static void zforce_reset_assert(struct zforce_ts *ts)
  134. {
  135. gpiod_set_value_cansleep(ts->gpio_rst, 1);
  136. }
  137. static void zforce_reset_deassert(struct zforce_ts *ts)
  138. {
  139. gpiod_set_value_cansleep(ts->gpio_rst, 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] & 0x0f;
  298. point.id = (payload[9 * i + 5] & 0xf0) >> 4;
  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. int ret;
  395. u8 payload_buffer[FRAME_MAXSIZE];
  396. u8 *payload;
  397. /*
  398. * When still suspended, return.
  399. * Due to the level-interrupt we will get re-triggered later.
  400. */
  401. if (ts->suspended) {
  402. msleep(20);
  403. return IRQ_HANDLED;
  404. }
  405. dev_dbg(&client->dev, "handling interrupt\n");
  406. /* Don't emit wakeup events from commands run by zforce_suspend */
  407. if (!ts->suspending && device_may_wakeup(&client->dev))
  408. pm_stay_awake(&client->dev);
  409. /*
  410. * Run at least once and exit the loop if
  411. * - the optional interrupt GPIO isn't specified
  412. * (there is only one packet read per ISR invocation, then)
  413. * or
  414. * - the GPIO isn't active any more
  415. * (packet read until the level GPIO indicates that there is
  416. * no IRQ any more)
  417. */
  418. do {
  419. ret = zforce_read_packet(ts, payload_buffer);
  420. if (ret < 0) {
  421. dev_err(&client->dev,
  422. "could not read packet, ret: %d\n", ret);
  423. break;
  424. }
  425. payload = &payload_buffer[PAYLOAD_BODY];
  426. switch (payload[RESPONSE_ID]) {
  427. case NOTIFICATION_TOUCH:
  428. /*
  429. * Always report touch-events received while
  430. * suspending, when being a wakeup source
  431. */
  432. if (ts->suspending && device_may_wakeup(&client->dev))
  433. pm_wakeup_event(&client->dev, 500);
  434. zforce_touch_event(ts, &payload[RESPONSE_DATA]);
  435. break;
  436. case NOTIFICATION_BOOTCOMPLETE:
  437. ts->boot_complete = payload[RESPONSE_DATA];
  438. zforce_complete(ts, payload[RESPONSE_ID], 0);
  439. break;
  440. case RESPONSE_INITIALIZE:
  441. case RESPONSE_DEACTIVATE:
  442. case RESPONSE_SETCONFIG:
  443. case RESPONSE_RESOLUTION:
  444. case RESPONSE_SCANFREQ:
  445. zforce_complete(ts, payload[RESPONSE_ID],
  446. payload[RESPONSE_DATA]);
  447. break;
  448. case RESPONSE_STATUS:
  449. /*
  450. * Version Payload Results
  451. * [2:major] [2:minor] [2:build] [2:rev]
  452. */
  453. ts->version_major = (payload[RESPONSE_DATA + 1] << 8) |
  454. payload[RESPONSE_DATA];
  455. ts->version_minor = (payload[RESPONSE_DATA + 3] << 8) |
  456. payload[RESPONSE_DATA + 2];
  457. ts->version_build = (payload[RESPONSE_DATA + 5] << 8) |
  458. payload[RESPONSE_DATA + 4];
  459. ts->version_rev = (payload[RESPONSE_DATA + 7] << 8) |
  460. payload[RESPONSE_DATA + 6];
  461. dev_dbg(&ts->client->dev,
  462. "Firmware Version %04x:%04x %04x:%04x\n",
  463. ts->version_major, ts->version_minor,
  464. ts->version_build, ts->version_rev);
  465. zforce_complete(ts, payload[RESPONSE_ID], 0);
  466. break;
  467. case NOTIFICATION_INVALID_COMMAND:
  468. dev_err(&ts->client->dev, "invalid command: 0x%x\n",
  469. payload[RESPONSE_DATA]);
  470. break;
  471. default:
  472. dev_err(&ts->client->dev,
  473. "unrecognized response id: 0x%x\n",
  474. payload[RESPONSE_ID]);
  475. break;
  476. }
  477. } while (gpiod_get_value_cansleep(ts->gpio_int));
  478. if (!ts->suspending && device_may_wakeup(&client->dev))
  479. pm_relax(&client->dev);
  480. dev_dbg(&client->dev, "finished interrupt\n");
  481. return IRQ_HANDLED;
  482. }
  483. static int zforce_input_open(struct input_dev *dev)
  484. {
  485. struct zforce_ts *ts = input_get_drvdata(dev);
  486. return zforce_start(ts);
  487. }
  488. static void zforce_input_close(struct input_dev *dev)
  489. {
  490. struct zforce_ts *ts = input_get_drvdata(dev);
  491. struct i2c_client *client = ts->client;
  492. int ret;
  493. ret = zforce_stop(ts);
  494. if (ret)
  495. dev_warn(&client->dev, "stopping zforce failed\n");
  496. return;
  497. }
  498. static int __maybe_unused zforce_suspend(struct device *dev)
  499. {
  500. struct i2c_client *client = to_i2c_client(dev);
  501. struct zforce_ts *ts = i2c_get_clientdata(client);
  502. struct input_dev *input = ts->input;
  503. int ret = 0;
  504. mutex_lock(&input->mutex);
  505. ts->suspending = true;
  506. /*
  507. * When configured as a wakeup source device should always wake
  508. * the system, therefore start device if necessary.
  509. */
  510. if (device_may_wakeup(&client->dev)) {
  511. dev_dbg(&client->dev, "suspend while being a wakeup source\n");
  512. /* Need to start device, if not open, to be a wakeup source. */
  513. if (!input_device_enabled(input)) {
  514. ret = zforce_start(ts);
  515. if (ret)
  516. goto unlock;
  517. }
  518. enable_irq_wake(client->irq);
  519. } else if (input_device_enabled(input)) {
  520. dev_dbg(&client->dev,
  521. "suspend without being a wakeup source\n");
  522. ret = zforce_stop(ts);
  523. if (ret)
  524. goto unlock;
  525. disable_irq(client->irq);
  526. }
  527. ts->suspended = true;
  528. unlock:
  529. ts->suspending = false;
  530. mutex_unlock(&input->mutex);
  531. return ret;
  532. }
  533. static int __maybe_unused zforce_resume(struct device *dev)
  534. {
  535. struct i2c_client *client = to_i2c_client(dev);
  536. struct zforce_ts *ts = i2c_get_clientdata(client);
  537. struct input_dev *input = ts->input;
  538. int ret = 0;
  539. mutex_lock(&input->mutex);
  540. ts->suspended = false;
  541. if (device_may_wakeup(&client->dev)) {
  542. dev_dbg(&client->dev, "resume from being a wakeup source\n");
  543. disable_irq_wake(client->irq);
  544. /* need to stop device if it was not open on suspend */
  545. if (!input_device_enabled(input)) {
  546. ret = zforce_stop(ts);
  547. if (ret)
  548. goto unlock;
  549. }
  550. } else if (input_device_enabled(input)) {
  551. dev_dbg(&client->dev, "resume without being a wakeup source\n");
  552. enable_irq(client->irq);
  553. ret = zforce_start(ts);
  554. if (ret < 0)
  555. goto unlock;
  556. }
  557. unlock:
  558. mutex_unlock(&input->mutex);
  559. return ret;
  560. }
  561. static SIMPLE_DEV_PM_OPS(zforce_pm_ops, zforce_suspend, zforce_resume);
  562. static void zforce_reset(void *data)
  563. {
  564. struct zforce_ts *ts = data;
  565. zforce_reset_assert(ts);
  566. udelay(10);
  567. if (!IS_ERR(ts->reg_vdd))
  568. regulator_disable(ts->reg_vdd);
  569. }
  570. static struct zforce_ts_platdata *zforce_parse_dt(struct device *dev)
  571. {
  572. struct zforce_ts_platdata *pdata;
  573. struct device_node *np = dev->of_node;
  574. if (!np)
  575. return ERR_PTR(-ENOENT);
  576. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  577. if (!pdata) {
  578. dev_err(dev, "failed to allocate platform data\n");
  579. return ERR_PTR(-ENOMEM);
  580. }
  581. if (of_property_read_u32(np, "x-size", &pdata->x_max)) {
  582. dev_err(dev, "failed to get x-size property\n");
  583. return ERR_PTR(-EINVAL);
  584. }
  585. if (of_property_read_u32(np, "y-size", &pdata->y_max)) {
  586. dev_err(dev, "failed to get y-size property\n");
  587. return ERR_PTR(-EINVAL);
  588. }
  589. return pdata;
  590. }
  591. static int zforce_probe(struct i2c_client *client,
  592. const struct i2c_device_id *id)
  593. {
  594. const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
  595. struct zforce_ts *ts;
  596. struct input_dev *input_dev;
  597. int ret;
  598. if (!pdata) {
  599. pdata = zforce_parse_dt(&client->dev);
  600. if (IS_ERR(pdata))
  601. return PTR_ERR(pdata);
  602. }
  603. ts = devm_kzalloc(&client->dev, sizeof(struct zforce_ts), GFP_KERNEL);
  604. if (!ts)
  605. return -ENOMEM;
  606. ts->gpio_rst = devm_gpiod_get_optional(&client->dev, "reset",
  607. GPIOD_OUT_HIGH);
  608. if (IS_ERR(ts->gpio_rst)) {
  609. ret = PTR_ERR(ts->gpio_rst);
  610. dev_err(&client->dev,
  611. "failed to request reset GPIO: %d\n", ret);
  612. return ret;
  613. }
  614. if (ts->gpio_rst) {
  615. ts->gpio_int = devm_gpiod_get_optional(&client->dev, "irq",
  616. GPIOD_IN);
  617. if (IS_ERR(ts->gpio_int)) {
  618. ret = PTR_ERR(ts->gpio_int);
  619. dev_err(&client->dev,
  620. "failed to request interrupt GPIO: %d\n", ret);
  621. return ret;
  622. }
  623. } else {
  624. /*
  625. * Deprecated GPIO handling for compatibility
  626. * with legacy binding.
  627. */
  628. /* INT GPIO */
  629. ts->gpio_int = devm_gpiod_get_index(&client->dev, NULL, 0,
  630. GPIOD_IN);
  631. if (IS_ERR(ts->gpio_int)) {
  632. ret = PTR_ERR(ts->gpio_int);
  633. dev_err(&client->dev,
  634. "failed to request interrupt GPIO: %d\n", ret);
  635. return ret;
  636. }
  637. /* RST GPIO */
  638. ts->gpio_rst = devm_gpiod_get_index(&client->dev, NULL, 1,
  639. GPIOD_OUT_HIGH);
  640. if (IS_ERR(ts->gpio_rst)) {
  641. ret = PTR_ERR(ts->gpio_rst);
  642. dev_err(&client->dev,
  643. "failed to request reset GPIO: %d\n", ret);
  644. return ret;
  645. }
  646. }
  647. ts->reg_vdd = devm_regulator_get_optional(&client->dev, "vdd");
  648. if (IS_ERR(ts->reg_vdd)) {
  649. ret = PTR_ERR(ts->reg_vdd);
  650. if (ret == -EPROBE_DEFER)
  651. return ret;
  652. } else {
  653. ret = regulator_enable(ts->reg_vdd);
  654. if (ret)
  655. return ret;
  656. /*
  657. * according to datasheet add 100us grace time after regular
  658. * regulator enable delay.
  659. */
  660. udelay(100);
  661. }
  662. ret = devm_add_action(&client->dev, zforce_reset, ts);
  663. if (ret) {
  664. dev_err(&client->dev, "failed to register reset action, %d\n",
  665. ret);
  666. /* hereafter the regulator will be disabled by the action */
  667. if (!IS_ERR(ts->reg_vdd))
  668. regulator_disable(ts->reg_vdd);
  669. return ret;
  670. }
  671. snprintf(ts->phys, sizeof(ts->phys),
  672. "%s/input0", dev_name(&client->dev));
  673. input_dev = devm_input_allocate_device(&client->dev);
  674. if (!input_dev) {
  675. dev_err(&client->dev, "could not allocate input device\n");
  676. return -ENOMEM;
  677. }
  678. mutex_init(&ts->access_mutex);
  679. mutex_init(&ts->command_mutex);
  680. ts->pdata = pdata;
  681. ts->client = client;
  682. ts->input = input_dev;
  683. input_dev->name = "Neonode zForce touchscreen";
  684. input_dev->phys = ts->phys;
  685. input_dev->id.bustype = BUS_I2C;
  686. input_dev->open = zforce_input_open;
  687. input_dev->close = zforce_input_close;
  688. __set_bit(EV_KEY, input_dev->evbit);
  689. __set_bit(EV_SYN, input_dev->evbit);
  690. __set_bit(EV_ABS, input_dev->evbit);
  691. /* For multi touch */
  692. input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
  693. pdata->x_max, 0, 0);
  694. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
  695. pdata->y_max, 0, 0);
  696. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
  697. ZFORCE_MAX_AREA, 0, 0);
  698. input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0,
  699. ZFORCE_MAX_AREA, 0, 0);
  700. input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
  701. input_mt_init_slots(input_dev, ZFORCE_REPORT_POINTS, INPUT_MT_DIRECT);
  702. input_set_drvdata(ts->input, ts);
  703. init_completion(&ts->command_done);
  704. /*
  705. * The zforce pulls the interrupt low when it has data ready.
  706. * After it is triggered the isr thread runs until all the available
  707. * packets have been read and the interrupt is high again.
  708. * Therefore we can trigger the interrupt anytime it is low and do
  709. * not need to limit it to the interrupt edge.
  710. */
  711. ret = devm_request_threaded_irq(&client->dev, client->irq,
  712. zforce_irq, zforce_irq_thread,
  713. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  714. input_dev->name, ts);
  715. if (ret) {
  716. dev_err(&client->dev, "irq %d request failed\n", client->irq);
  717. return ret;
  718. }
  719. i2c_set_clientdata(client, ts);
  720. /* let the controller boot */
  721. zforce_reset_deassert(ts);
  722. ts->command_waiting = NOTIFICATION_BOOTCOMPLETE;
  723. if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0)
  724. dev_warn(&client->dev, "bootcomplete timed out\n");
  725. /* need to start device to get version information */
  726. ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
  727. if (ret) {
  728. dev_err(&client->dev, "unable to initialize, %d\n", ret);
  729. return ret;
  730. }
  731. /* this gets the firmware version among other information */
  732. ret = zforce_command_wait(ts, COMMAND_STATUS);
  733. if (ret < 0) {
  734. dev_err(&client->dev, "couldn't get status, %d\n", ret);
  735. zforce_stop(ts);
  736. return ret;
  737. }
  738. /* stop device and put it into sleep until it is opened */
  739. ret = zforce_stop(ts);
  740. if (ret < 0)
  741. return ret;
  742. device_set_wakeup_capable(&client->dev, true);
  743. ret = input_register_device(input_dev);
  744. if (ret) {
  745. dev_err(&client->dev, "could not register input device, %d\n",
  746. ret);
  747. return ret;
  748. }
  749. return 0;
  750. }
  751. static struct i2c_device_id zforce_idtable[] = {
  752. { "zforce-ts", 0 },
  753. { }
  754. };
  755. MODULE_DEVICE_TABLE(i2c, zforce_idtable);
  756. #ifdef CONFIG_OF
  757. static const struct of_device_id zforce_dt_idtable[] = {
  758. { .compatible = "neonode,zforce" },
  759. {},
  760. };
  761. MODULE_DEVICE_TABLE(of, zforce_dt_idtable);
  762. #endif
  763. static struct i2c_driver zforce_driver = {
  764. .driver = {
  765. .name = "zforce-ts",
  766. .pm = &zforce_pm_ops,
  767. .of_match_table = of_match_ptr(zforce_dt_idtable),
  768. },
  769. .probe = zforce_probe,
  770. .id_table = zforce_idtable,
  771. };
  772. module_i2c_driver(zforce_driver);
  773. MODULE_AUTHOR("Heiko Stuebner <[email protected]>");
  774. MODULE_DESCRIPTION("zForce TouchScreen Driver");
  775. MODULE_LICENSE("GPL");