zforce_ts.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012-2013 MundoReader S.L.
  4. * Author: Heiko Stuebner <heiko@sntech.de>
  5. *
  6. * based in parts on Nook zforce driver
  7. *
  8. * Copyright (C) 2010 Barnes & Noble, Inc.
  9. * Author: Pieter Truter<ptruter@intrinsyc.com>
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/i2c.h>
  15. #include <linux/input.h>
  16. #include <linux/input/mt.h>
  17. #include <linux/input/touchscreen.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/property.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/slab.h>
  24. #include <linux/unaligned.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. * @command_done completion to wait for the command result
  87. * @command_waiting the id of the command that is currently waiting
  88. * for a result
  89. * @command_result returned result of the command
  90. */
  91. struct zforce_ts {
  92. struct i2c_client *client;
  93. struct input_dev *input;
  94. struct touchscreen_properties prop;
  95. char phys[32];
  96. struct gpio_desc *gpio_int;
  97. struct gpio_desc *gpio_rst;
  98. bool suspending;
  99. bool suspended;
  100. bool boot_complete;
  101. /* Firmware version information */
  102. u16 version_major;
  103. u16 version_minor;
  104. u16 version_build;
  105. u16 version_rev;
  106. struct completion command_done;
  107. int command_waiting;
  108. int command_result;
  109. };
  110. static int zforce_command(struct zforce_ts *ts, u8 cmd)
  111. {
  112. struct i2c_client *client = ts->client;
  113. char buf[3];
  114. int ret;
  115. dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
  116. buf[0] = FRAME_START;
  117. buf[1] = 1; /* data size, command only */
  118. buf[2] = cmd;
  119. ret = i2c_master_send(client, &buf[0], ARRAY_SIZE(buf));
  120. if (ret < 0) {
  121. dev_err(&client->dev, "i2c send data request error: %d\n", ret);
  122. return ret;
  123. }
  124. return 0;
  125. }
  126. static int zforce_send_wait(struct zforce_ts *ts, const char *buf, int len)
  127. {
  128. struct i2c_client *client = ts->client;
  129. int ret;
  130. dev_dbg(&client->dev, "sending %d bytes for command 0x%x\n",
  131. buf[1], buf[2]);
  132. ts->command_waiting = buf[2];
  133. ret = i2c_master_send(client, buf, len);
  134. if (ret < 0) {
  135. dev_err(&client->dev, "i2c send data request error: %d\n", ret);
  136. return ret;
  137. }
  138. dev_dbg(&client->dev, "waiting for result for command 0x%x\n", buf[2]);
  139. if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0)
  140. return -ETIME;
  141. ret = ts->command_result;
  142. return 0;
  143. }
  144. static int zforce_command_wait(struct zforce_ts *ts, u8 cmd)
  145. {
  146. struct i2c_client *client = ts->client;
  147. char buf[3];
  148. int error;
  149. dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
  150. buf[0] = FRAME_START;
  151. buf[1] = 1; /* data size, command only */
  152. buf[2] = cmd;
  153. error = zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
  154. if (error) {
  155. dev_err(&client->dev, "i2c send data request error: %d\n",
  156. error);
  157. return error;
  158. }
  159. return 0;
  160. }
  161. static int zforce_resolution(struct zforce_ts *ts, u16 x, u16 y)
  162. {
  163. struct i2c_client *client = ts->client;
  164. char buf[7] = { FRAME_START, 5, COMMAND_RESOLUTION,
  165. (x & 0xff), ((x >> 8) & 0xff),
  166. (y & 0xff), ((y >> 8) & 0xff) };
  167. dev_dbg(&client->dev, "set resolution to (%d,%d)\n", x, y);
  168. return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
  169. }
  170. static int zforce_scan_frequency(struct zforce_ts *ts, u16 idle, u16 finger,
  171. u16 stylus)
  172. {
  173. struct i2c_client *client = ts->client;
  174. char buf[9] = { FRAME_START, 7, COMMAND_SCANFREQ,
  175. (idle & 0xff), ((idle >> 8) & 0xff),
  176. (finger & 0xff), ((finger >> 8) & 0xff),
  177. (stylus & 0xff), ((stylus >> 8) & 0xff) };
  178. dev_dbg(&client->dev,
  179. "set scan frequency to (idle: %d, finger: %d, stylus: %d)\n",
  180. idle, finger, stylus);
  181. return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
  182. }
  183. static int zforce_setconfig(struct zforce_ts *ts, char b1)
  184. {
  185. struct i2c_client *client = ts->client;
  186. char buf[7] = { FRAME_START, 5, COMMAND_SETCONFIG,
  187. b1, 0, 0, 0 };
  188. dev_dbg(&client->dev, "set config to (%d)\n", b1);
  189. return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
  190. }
  191. static int zforce_start(struct zforce_ts *ts)
  192. {
  193. struct i2c_client *client = ts->client;
  194. int error;
  195. dev_dbg(&client->dev, "starting device\n");
  196. error = zforce_command_wait(ts, COMMAND_INITIALIZE);
  197. if (error) {
  198. dev_err(&client->dev, "Unable to initialize, %d\n", error);
  199. return error;
  200. }
  201. error = zforce_resolution(ts, ts->prop.max_x, ts->prop.max_y);
  202. if (error) {
  203. dev_err(&client->dev, "Unable to set resolution, %d\n", error);
  204. goto err_deactivate;
  205. }
  206. error = zforce_scan_frequency(ts, 10, 50, 50);
  207. if (error) {
  208. dev_err(&client->dev, "Unable to set scan frequency, %d\n",
  209. error);
  210. goto err_deactivate;
  211. }
  212. error = zforce_setconfig(ts, SETCONFIG_DUALTOUCH);
  213. if (error) {
  214. dev_err(&client->dev, "Unable to set config\n");
  215. goto err_deactivate;
  216. }
  217. /* start sending touch events */
  218. error = zforce_command(ts, COMMAND_DATAREQUEST);
  219. if (error) {
  220. dev_err(&client->dev, "Unable to request data\n");
  221. goto err_deactivate;
  222. }
  223. /*
  224. * Per NN, initial cal. take max. of 200msec.
  225. * Allow time to complete this calibration
  226. */
  227. msleep(200);
  228. return 0;
  229. err_deactivate:
  230. zforce_command_wait(ts, COMMAND_DEACTIVATE);
  231. return error;
  232. }
  233. static int zforce_stop(struct zforce_ts *ts)
  234. {
  235. struct i2c_client *client = ts->client;
  236. int error;
  237. dev_dbg(&client->dev, "stopping device\n");
  238. /* Deactivates touch sensing and puts the device into sleep. */
  239. error = zforce_command_wait(ts, COMMAND_DEACTIVATE);
  240. if (error) {
  241. dev_err(&client->dev, "could not deactivate device, %d\n",
  242. error);
  243. return error;
  244. }
  245. return 0;
  246. }
  247. static int zforce_touch_event(struct zforce_ts *ts, u8 *payload)
  248. {
  249. struct i2c_client *client = ts->client;
  250. struct zforce_point point;
  251. int count, i, num = 0;
  252. u8 *p;
  253. count = payload[0];
  254. if (count > ZFORCE_REPORT_POINTS) {
  255. dev_warn(&client->dev,
  256. "too many coordinates %d, expected max %d\n",
  257. count, ZFORCE_REPORT_POINTS);
  258. count = ZFORCE_REPORT_POINTS;
  259. }
  260. for (i = 0; i < count; i++) {
  261. p = &payload[i * 9 + 1];
  262. point.coord_x = get_unaligned_le16(&p[0]);
  263. point.coord_y = get_unaligned_le16(&p[2]);
  264. if (point.coord_x > ts->prop.max_x ||
  265. point.coord_y > ts->prop.max_y) {
  266. dev_warn(&client->dev, "coordinates (%d,%d) invalid\n",
  267. point.coord_x, point.coord_y);
  268. point.coord_x = point.coord_y = 0;
  269. }
  270. point.state = p[4] & 0x0f;
  271. point.id = (p[4] & 0xf0) >> 4;
  272. /* determine touch major, minor and orientation */
  273. point.area_major = max(p[5], p[6]);
  274. point.area_minor = min(p[5], p[6]);
  275. point.orientation = p[5] > p[6];
  276. point.pressure = p[7];
  277. point.prblty = p[8];
  278. dev_dbg(&client->dev,
  279. "point %d/%d: state %d, id %d, pressure %d, prblty %d, x %d, y %d, amajor %d, aminor %d, ori %d\n",
  280. i, count, point.state, point.id,
  281. point.pressure, point.prblty,
  282. point.coord_x, point.coord_y,
  283. point.area_major, point.area_minor,
  284. point.orientation);
  285. /* the zforce id starts with "1", so needs to be decreased */
  286. input_mt_slot(ts->input, point.id - 1);
  287. if (input_mt_report_slot_state(ts->input, MT_TOOL_FINGER,
  288. point.state != STATE_UP)) {
  289. touchscreen_report_pos(ts->input, &ts->prop,
  290. point.coord_x, point.coord_y,
  291. true);
  292. input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR,
  293. point.area_major);
  294. input_report_abs(ts->input, ABS_MT_TOUCH_MINOR,
  295. point.area_minor);
  296. input_report_abs(ts->input, ABS_MT_ORIENTATION,
  297. point.orientation);
  298. num++;
  299. }
  300. }
  301. input_mt_sync_frame(ts->input);
  302. input_mt_report_finger_count(ts->input, num);
  303. input_sync(ts->input);
  304. return 0;
  305. }
  306. static int zforce_read_packet(struct zforce_ts *ts, u8 *buf)
  307. {
  308. struct i2c_client *client = ts->client;
  309. int ret;
  310. /* read 2 byte message header */
  311. ret = i2c_master_recv(client, buf, 2);
  312. if (ret < 0) {
  313. dev_err(&client->dev, "error reading header: %d\n", ret);
  314. return ret;
  315. }
  316. if (buf[PAYLOAD_HEADER] != FRAME_START) {
  317. dev_err(&client->dev, "invalid frame start: %d\n", buf[0]);
  318. return -EIO;
  319. }
  320. if (buf[PAYLOAD_LENGTH] == 0) {
  321. dev_err(&client->dev, "invalid payload length: %d\n",
  322. buf[PAYLOAD_LENGTH]);
  323. return -EIO;
  324. }
  325. /* read the message */
  326. ret = i2c_master_recv(client, &buf[PAYLOAD_BODY], buf[PAYLOAD_LENGTH]);
  327. if (ret < 0) {
  328. dev_err(&client->dev, "error reading payload: %d\n", ret);
  329. return ret;
  330. }
  331. dev_dbg(&client->dev, "read %d bytes for response command 0x%x\n",
  332. buf[PAYLOAD_LENGTH], buf[PAYLOAD_BODY]);
  333. return 0;
  334. }
  335. static void zforce_complete(struct zforce_ts *ts, int cmd, int result)
  336. {
  337. struct i2c_client *client = ts->client;
  338. if (ts->command_waiting == cmd) {
  339. dev_dbg(&client->dev, "completing command 0x%x\n", cmd);
  340. ts->command_result = result;
  341. complete(&ts->command_done);
  342. } else {
  343. dev_dbg(&client->dev, "command %d not for us\n", cmd);
  344. }
  345. }
  346. static irqreturn_t zforce_irq(int irq, void *dev_id)
  347. {
  348. struct zforce_ts *ts = dev_id;
  349. struct i2c_client *client = ts->client;
  350. if (ts->suspended && device_may_wakeup(&client->dev))
  351. pm_wakeup_event(&client->dev, 500);
  352. return IRQ_WAKE_THREAD;
  353. }
  354. static irqreturn_t zforce_irq_thread(int irq, void *dev_id)
  355. {
  356. struct zforce_ts *ts = dev_id;
  357. struct i2c_client *client = ts->client;
  358. int error;
  359. u8 payload_buffer[FRAME_MAXSIZE];
  360. u8 *payload;
  361. bool suspending;
  362. /*
  363. * When still suspended, return.
  364. * Due to the level-interrupt we will get re-triggered later.
  365. */
  366. if (ts->suspended) {
  367. msleep(20);
  368. return IRQ_HANDLED;
  369. }
  370. dev_dbg(&client->dev, "handling interrupt\n");
  371. /* Don't emit wakeup events from commands run by zforce_suspend */
  372. suspending = READ_ONCE(ts->suspending);
  373. if (!suspending && device_may_wakeup(&client->dev))
  374. pm_stay_awake(&client->dev);
  375. /*
  376. * Run at least once and exit the loop if
  377. * - the optional interrupt GPIO isn't specified
  378. * (there is only one packet read per ISR invocation, then)
  379. * or
  380. * - the GPIO isn't active any more
  381. * (packet read until the level GPIO indicates that there is
  382. * no IRQ any more)
  383. */
  384. do {
  385. error = zforce_read_packet(ts, payload_buffer);
  386. if (error) {
  387. dev_err(&client->dev,
  388. "could not read packet, ret: %d\n", error);
  389. break;
  390. }
  391. payload = &payload_buffer[PAYLOAD_BODY];
  392. switch (payload[RESPONSE_ID]) {
  393. case NOTIFICATION_TOUCH:
  394. /*
  395. * Always report touch-events received while
  396. * suspending, when being a wakeup source
  397. */
  398. if (suspending && device_may_wakeup(&client->dev))
  399. pm_wakeup_event(&client->dev, 500);
  400. zforce_touch_event(ts, &payload[RESPONSE_DATA]);
  401. break;
  402. case NOTIFICATION_BOOTCOMPLETE:
  403. ts->boot_complete = payload[RESPONSE_DATA];
  404. zforce_complete(ts, payload[RESPONSE_ID], 0);
  405. break;
  406. case RESPONSE_INITIALIZE:
  407. case RESPONSE_DEACTIVATE:
  408. case RESPONSE_SETCONFIG:
  409. case RESPONSE_RESOLUTION:
  410. case RESPONSE_SCANFREQ:
  411. zforce_complete(ts, payload[RESPONSE_ID],
  412. payload[RESPONSE_DATA]);
  413. break;
  414. case RESPONSE_STATUS:
  415. /*
  416. * Version Payload Results
  417. * [2:major] [2:minor] [2:build] [2:rev]
  418. */
  419. ts->version_major =
  420. get_unaligned_le16(&payload[RESPONSE_DATA]);
  421. ts->version_minor =
  422. get_unaligned_le16(&payload[RESPONSE_DATA + 2]);
  423. ts->version_build =
  424. get_unaligned_le16(&payload[RESPONSE_DATA + 4]);
  425. ts->version_rev =
  426. get_unaligned_le16(&payload[RESPONSE_DATA + 6]);
  427. dev_dbg(&ts->client->dev,
  428. "Firmware Version %04x:%04x %04x:%04x\n",
  429. ts->version_major, ts->version_minor,
  430. ts->version_build, ts->version_rev);
  431. zforce_complete(ts, payload[RESPONSE_ID], 0);
  432. break;
  433. case NOTIFICATION_INVALID_COMMAND:
  434. dev_err(&ts->client->dev, "invalid command: 0x%x\n",
  435. payload[RESPONSE_DATA]);
  436. break;
  437. default:
  438. dev_err(&ts->client->dev,
  439. "unrecognized response id: 0x%x\n",
  440. payload[RESPONSE_ID]);
  441. break;
  442. }
  443. } while (gpiod_get_value_cansleep(ts->gpio_int));
  444. if (!suspending && device_may_wakeup(&client->dev))
  445. pm_relax(&client->dev);
  446. dev_dbg(&client->dev, "finished interrupt\n");
  447. return IRQ_HANDLED;
  448. }
  449. static int zforce_input_open(struct input_dev *dev)
  450. {
  451. struct zforce_ts *ts = input_get_drvdata(dev);
  452. return zforce_start(ts);
  453. }
  454. static void zforce_input_close(struct input_dev *dev)
  455. {
  456. struct zforce_ts *ts = input_get_drvdata(dev);
  457. struct i2c_client *client = ts->client;
  458. int error;
  459. error = zforce_stop(ts);
  460. if (error)
  461. dev_warn(&client->dev, "stopping zforce failed\n");
  462. }
  463. static int __zforce_suspend(struct zforce_ts *ts)
  464. {
  465. struct i2c_client *client = ts->client;
  466. struct input_dev *input = ts->input;
  467. int error;
  468. guard(mutex)(&input->mutex);
  469. /*
  470. * When configured as a wakeup source device should always wake
  471. * the system, therefore start device if necessary.
  472. */
  473. if (device_may_wakeup(&client->dev)) {
  474. dev_dbg(&client->dev, "suspend while being a wakeup source\n");
  475. /* Need to start device, if not open, to be a wakeup source. */
  476. if (!input_device_enabled(input)) {
  477. error = zforce_start(ts);
  478. if (error)
  479. return error;
  480. }
  481. enable_irq_wake(client->irq);
  482. } else if (input_device_enabled(input)) {
  483. dev_dbg(&client->dev,
  484. "suspend without being a wakeup source\n");
  485. error = zforce_stop(ts);
  486. if (error)
  487. return error;
  488. disable_irq(client->irq);
  489. }
  490. ts->suspended = true;
  491. return 0;
  492. }
  493. static int zforce_suspend(struct device *dev)
  494. {
  495. struct i2c_client *client = to_i2c_client(dev);
  496. struct zforce_ts *ts = i2c_get_clientdata(client);
  497. int ret;
  498. WRITE_ONCE(ts->suspending, true);
  499. smp_mb();
  500. ret = __zforce_suspend(ts);
  501. smp_mb();
  502. WRITE_ONCE(ts->suspending, false);
  503. return ret;
  504. }
  505. static int zforce_resume(struct device *dev)
  506. {
  507. struct i2c_client *client = to_i2c_client(dev);
  508. struct zforce_ts *ts = i2c_get_clientdata(client);
  509. struct input_dev *input = ts->input;
  510. int error;
  511. guard(mutex)(&input->mutex);
  512. ts->suspended = false;
  513. if (device_may_wakeup(&client->dev)) {
  514. dev_dbg(&client->dev, "resume from being a wakeup source\n");
  515. disable_irq_wake(client->irq);
  516. /* need to stop device if it was not open on suspend */
  517. if (!input_device_enabled(input)) {
  518. error = zforce_stop(ts);
  519. if (error)
  520. return error;
  521. }
  522. } else if (input_device_enabled(input)) {
  523. dev_dbg(&client->dev, "resume without being a wakeup source\n");
  524. enable_irq(client->irq);
  525. error = zforce_start(ts);
  526. if (error)
  527. return error;
  528. }
  529. return 0;
  530. }
  531. static DEFINE_SIMPLE_DEV_PM_OPS(zforce_pm_ops, zforce_suspend, zforce_resume);
  532. static void zforce_reset(void *data)
  533. {
  534. struct zforce_ts *ts = data;
  535. gpiod_set_value_cansleep(ts->gpio_rst, 1);
  536. udelay(10);
  537. }
  538. static void zforce_ts_parse_legacy_properties(struct zforce_ts *ts)
  539. {
  540. u32 x_max = 0;
  541. u32 y_max = 0;
  542. device_property_read_u32(&ts->client->dev, "x-size", &x_max);
  543. input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, x_max, 0, 0);
  544. device_property_read_u32(&ts->client->dev, "y-size", &y_max);
  545. input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, y_max, 0, 0);
  546. }
  547. static int zforce_probe(struct i2c_client *client)
  548. {
  549. struct zforce_ts *ts;
  550. struct input_dev *input_dev;
  551. int error;
  552. ts = devm_kzalloc(&client->dev, sizeof(struct zforce_ts), GFP_KERNEL);
  553. if (!ts)
  554. return -ENOMEM;
  555. ts->gpio_rst = devm_gpiod_get_optional(&client->dev, "reset",
  556. GPIOD_OUT_HIGH);
  557. error = PTR_ERR_OR_ZERO(ts->gpio_rst);
  558. if (error)
  559. return dev_err_probe(&client->dev, error,
  560. "failed to request reset GPIO\n");
  561. if (ts->gpio_rst) {
  562. ts->gpio_int = devm_gpiod_get_optional(&client->dev, "irq",
  563. GPIOD_IN);
  564. error = PTR_ERR_OR_ZERO(ts->gpio_int);
  565. if (error)
  566. return dev_err_probe(&client->dev, error,
  567. "failed to request interrupt GPIO\n");
  568. } else {
  569. /*
  570. * Deprecated GPIO handling for compatibility
  571. * with legacy binding.
  572. */
  573. /* INT GPIO */
  574. ts->gpio_int = devm_gpiod_get_index(&client->dev, NULL, 0,
  575. GPIOD_IN);
  576. error = PTR_ERR_OR_ZERO(ts->gpio_int);
  577. if (error)
  578. return dev_err_probe(&client->dev, error,
  579. "failed to request interrupt GPIO\n");
  580. /* RST GPIO */
  581. ts->gpio_rst = devm_gpiod_get_index(&client->dev, NULL, 1,
  582. GPIOD_OUT_HIGH);
  583. error = PTR_ERR_OR_ZERO(ts->gpio_rst);
  584. if (error)
  585. return dev_err_probe(&client->dev, error,
  586. "failed to request reset GPIO\n");
  587. }
  588. error = devm_regulator_get_enable(&client->dev, "vdd");
  589. if (error)
  590. return dev_err_probe(&client->dev, error,
  591. "failed to request vdd supply\n");
  592. /*
  593. * According to datasheet add 100us grace time after regular
  594. * regulator enable delay.
  595. */
  596. usleep_range(100, 200);
  597. error = devm_add_action_or_reset(&client->dev, zforce_reset, ts);
  598. if (error)
  599. return dev_err_probe(&client->dev, error,
  600. "failed to register reset action\n");
  601. snprintf(ts->phys, sizeof(ts->phys),
  602. "%s/input0", dev_name(&client->dev));
  603. input_dev = devm_input_allocate_device(&client->dev);
  604. if (!input_dev)
  605. return -ENOMEM;
  606. ts->client = client;
  607. ts->input = input_dev;
  608. input_dev->name = "Neonode zForce touchscreen";
  609. input_dev->phys = ts->phys;
  610. input_dev->id.bustype = BUS_I2C;
  611. input_dev->open = zforce_input_open;
  612. input_dev->close = zforce_input_close;
  613. zforce_ts_parse_legacy_properties(ts);
  614. touchscreen_parse_properties(input_dev, true, &ts->prop);
  615. if (ts->prop.max_x == 0 || ts->prop.max_y == 0)
  616. return dev_err_probe(&client->dev, -EINVAL, "no size specified");
  617. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
  618. ZFORCE_MAX_AREA, 0, 0);
  619. input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0,
  620. ZFORCE_MAX_AREA, 0, 0);
  621. input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
  622. error = input_mt_init_slots(input_dev, ZFORCE_REPORT_POINTS,
  623. INPUT_MT_DIRECT);
  624. if (error)
  625. return error;
  626. input_set_drvdata(ts->input, ts);
  627. init_completion(&ts->command_done);
  628. /*
  629. * The zforce pulls the interrupt low when it has data ready.
  630. * After it is triggered the isr thread runs until all the available
  631. * packets have been read and the interrupt is high again.
  632. * Therefore we can trigger the interrupt anytime it is low and do
  633. * not need to limit it to the interrupt edge.
  634. */
  635. error = devm_request_threaded_irq(&client->dev, client->irq,
  636. zforce_irq, zforce_irq_thread,
  637. IRQF_ONESHOT, input_dev->name, ts);
  638. if (error)
  639. return dev_err_probe(&client->dev, error,
  640. "irq %d request failed\n", client->irq);
  641. i2c_set_clientdata(client, ts);
  642. /* let the controller boot */
  643. gpiod_set_value_cansleep(ts->gpio_rst, 0);
  644. ts->command_waiting = NOTIFICATION_BOOTCOMPLETE;
  645. if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0)
  646. dev_warn(&client->dev, "bootcomplete timed out\n");
  647. /* need to start device to get version information */
  648. error = zforce_command_wait(ts, COMMAND_INITIALIZE);
  649. if (error)
  650. return dev_err_probe(&client->dev, error, "unable to initialize\n");
  651. /* this gets the firmware version among other information */
  652. error = zforce_command_wait(ts, COMMAND_STATUS);
  653. if (error) {
  654. dev_err_probe(&client->dev, error, "couldn't get status\n");
  655. zforce_stop(ts);
  656. return error;
  657. }
  658. /* stop device and put it into sleep until it is opened */
  659. error = zforce_stop(ts);
  660. if (error)
  661. return error;
  662. device_set_wakeup_capable(&client->dev, true);
  663. error = input_register_device(input_dev);
  664. if (error)
  665. return dev_err_probe(&client->dev, error,
  666. "could not register input device\n");
  667. return 0;
  668. }
  669. static const struct i2c_device_id zforce_idtable[] = {
  670. { "zforce-ts" },
  671. { }
  672. };
  673. MODULE_DEVICE_TABLE(i2c, zforce_idtable);
  674. #ifdef CONFIG_OF
  675. static const struct of_device_id zforce_dt_idtable[] = {
  676. { .compatible = "neonode,zforce" },
  677. {},
  678. };
  679. MODULE_DEVICE_TABLE(of, zforce_dt_idtable);
  680. #endif
  681. static struct i2c_driver zforce_driver = {
  682. .driver = {
  683. .name = "zforce-ts",
  684. .pm = pm_sleep_ptr(&zforce_pm_ops),
  685. .of_match_table = of_match_ptr(zforce_dt_idtable),
  686. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  687. },
  688. .probe = zforce_probe,
  689. .id_table = zforce_idtable,
  690. };
  691. module_i2c_driver(zforce_driver);
  692. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  693. MODULE_DESCRIPTION("zForce TouchScreen Driver");
  694. MODULE_LICENSE("GPL");