pixcir_i2c_ts.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Pixcir I2C touchscreen controllers.
  4. *
  5. * Copyright (C) 2010-2011 Pixcir, Inc.
  6. */
  7. #include <asm/unaligned.h>
  8. #include <linux/delay.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/i2c.h>
  11. #include <linux/input.h>
  12. #include <linux/input/mt.h>
  13. #include <linux/input/touchscreen.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/of_device.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #define PIXCIR_MAX_SLOTS 5 /* Max fingers supported by driver */
  19. /*
  20. * Register map
  21. */
  22. #define PIXCIR_REG_POWER_MODE 51
  23. #define PIXCIR_REG_INT_MODE 52
  24. /*
  25. * Power modes:
  26. * active: max scan speed
  27. * idle: lower scan speed with automatic transition to active on touch
  28. * halt: datasheet says sleep but this is more like halt as the chip
  29. * clocks are cut and it can only be brought out of this mode
  30. * using the RESET pin.
  31. */
  32. enum pixcir_power_mode {
  33. PIXCIR_POWER_ACTIVE,
  34. PIXCIR_POWER_IDLE,
  35. PIXCIR_POWER_HALT,
  36. };
  37. #define PIXCIR_POWER_MODE_MASK 0x03
  38. #define PIXCIR_POWER_ALLOW_IDLE (1UL << 2)
  39. /*
  40. * Interrupt modes:
  41. * periodical: interrupt is asserted periodicaly
  42. * diff coordinates: interrupt is asserted when coordinates change
  43. * level on touch: interrupt level asserted during touch
  44. * pulse on touch: interrupt pulse asserted during touch
  45. *
  46. */
  47. enum pixcir_int_mode {
  48. PIXCIR_INT_PERIODICAL,
  49. PIXCIR_INT_DIFF_COORD,
  50. PIXCIR_INT_LEVEL_TOUCH,
  51. PIXCIR_INT_PULSE_TOUCH,
  52. };
  53. #define PIXCIR_INT_MODE_MASK 0x03
  54. #define PIXCIR_INT_ENABLE (1UL << 3)
  55. #define PIXCIR_INT_POL_HIGH (1UL << 2)
  56. /**
  57. * struct pixcir_i2c_chip_data - chip related data
  58. * @max_fingers: Max number of fingers reported simultaneously by h/w
  59. * @has_hw_ids: Hardware supports finger tracking IDs
  60. *
  61. */
  62. struct pixcir_i2c_chip_data {
  63. u8 max_fingers;
  64. bool has_hw_ids;
  65. };
  66. struct pixcir_i2c_ts_data {
  67. struct i2c_client *client;
  68. struct input_dev *input;
  69. struct gpio_desc *gpio_attb;
  70. struct gpio_desc *gpio_reset;
  71. struct gpio_desc *gpio_enable;
  72. struct gpio_desc *gpio_wake;
  73. const struct pixcir_i2c_chip_data *chip;
  74. struct touchscreen_properties prop;
  75. bool running;
  76. };
  77. struct pixcir_report_data {
  78. int num_touches;
  79. struct input_mt_pos pos[PIXCIR_MAX_SLOTS];
  80. int ids[PIXCIR_MAX_SLOTS];
  81. };
  82. static void pixcir_ts_parse(struct pixcir_i2c_ts_data *tsdata,
  83. struct pixcir_report_data *report)
  84. {
  85. u8 rdbuf[2 + PIXCIR_MAX_SLOTS * 5];
  86. u8 wrbuf[1] = { 0 };
  87. u8 *bufptr;
  88. u8 touch;
  89. int ret, i;
  90. int readsize;
  91. const struct pixcir_i2c_chip_data *chip = tsdata->chip;
  92. memset(report, 0, sizeof(struct pixcir_report_data));
  93. i = chip->has_hw_ids ? 1 : 0;
  94. readsize = 2 + tsdata->chip->max_fingers * (4 + i);
  95. if (readsize > sizeof(rdbuf))
  96. readsize = sizeof(rdbuf);
  97. ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf));
  98. if (ret != sizeof(wrbuf)) {
  99. dev_err(&tsdata->client->dev,
  100. "%s: i2c_master_send failed(), ret=%d\n",
  101. __func__, ret);
  102. return;
  103. }
  104. ret = i2c_master_recv(tsdata->client, rdbuf, readsize);
  105. if (ret != readsize) {
  106. dev_err(&tsdata->client->dev,
  107. "%s: i2c_master_recv failed(), ret=%d\n",
  108. __func__, ret);
  109. return;
  110. }
  111. touch = rdbuf[0] & 0x7;
  112. if (touch > tsdata->chip->max_fingers)
  113. touch = tsdata->chip->max_fingers;
  114. report->num_touches = touch;
  115. bufptr = &rdbuf[2];
  116. for (i = 0; i < touch; i++) {
  117. touchscreen_set_mt_pos(&report->pos[i], &tsdata->prop,
  118. get_unaligned_le16(bufptr),
  119. get_unaligned_le16(bufptr + 2));
  120. if (chip->has_hw_ids) {
  121. report->ids[i] = bufptr[4];
  122. bufptr = bufptr + 5;
  123. } else {
  124. bufptr = bufptr + 4;
  125. }
  126. }
  127. }
  128. static void pixcir_ts_report(struct pixcir_i2c_ts_data *ts,
  129. struct pixcir_report_data *report)
  130. {
  131. int slots[PIXCIR_MAX_SLOTS];
  132. int n, i, slot;
  133. struct device *dev = &ts->client->dev;
  134. const struct pixcir_i2c_chip_data *chip = ts->chip;
  135. n = report->num_touches;
  136. if (n > PIXCIR_MAX_SLOTS)
  137. n = PIXCIR_MAX_SLOTS;
  138. if (!ts->chip->has_hw_ids)
  139. input_mt_assign_slots(ts->input, slots, report->pos, n, 0);
  140. for (i = 0; i < n; i++) {
  141. if (chip->has_hw_ids) {
  142. slot = input_mt_get_slot_by_key(ts->input,
  143. report->ids[i]);
  144. if (slot < 0) {
  145. dev_dbg(dev, "no free slot for id 0x%x\n",
  146. report->ids[i]);
  147. continue;
  148. }
  149. } else {
  150. slot = slots[i];
  151. }
  152. input_mt_slot(ts->input, slot);
  153. input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
  154. input_report_abs(ts->input, ABS_MT_POSITION_X,
  155. report->pos[i].x);
  156. input_report_abs(ts->input, ABS_MT_POSITION_Y,
  157. report->pos[i].y);
  158. dev_dbg(dev, "%d: slot %d, x %d, y %d\n",
  159. i, slot, report->pos[i].x, report->pos[i].y);
  160. }
  161. input_mt_sync_frame(ts->input);
  162. input_sync(ts->input);
  163. }
  164. static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
  165. {
  166. struct pixcir_i2c_ts_data *tsdata = dev_id;
  167. struct pixcir_report_data report;
  168. while (tsdata->running) {
  169. /* parse packet */
  170. pixcir_ts_parse(tsdata, &report);
  171. /* report it */
  172. pixcir_ts_report(tsdata, &report);
  173. if (gpiod_get_value_cansleep(tsdata->gpio_attb)) {
  174. if (report.num_touches) {
  175. /*
  176. * Last report with no finger up?
  177. * Do it now then.
  178. */
  179. input_mt_sync_frame(tsdata->input);
  180. input_sync(tsdata->input);
  181. }
  182. break;
  183. }
  184. msleep(20);
  185. }
  186. return IRQ_HANDLED;
  187. }
  188. static void pixcir_reset(struct pixcir_i2c_ts_data *tsdata)
  189. {
  190. if (!IS_ERR_OR_NULL(tsdata->gpio_reset)) {
  191. gpiod_set_value_cansleep(tsdata->gpio_reset, 1);
  192. ndelay(100); /* datasheet section 1.2.3 says 80ns min. */
  193. gpiod_set_value_cansleep(tsdata->gpio_reset, 0);
  194. /* wait for controller ready. 100ms guess. */
  195. msleep(100);
  196. }
  197. }
  198. static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts,
  199. enum pixcir_power_mode mode)
  200. {
  201. struct device *dev = &ts->client->dev;
  202. int ret;
  203. if (mode == PIXCIR_POWER_ACTIVE || mode == PIXCIR_POWER_IDLE) {
  204. if (ts->gpio_wake)
  205. gpiod_set_value_cansleep(ts->gpio_wake, 1);
  206. }
  207. ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE);
  208. if (ret < 0) {
  209. dev_err(dev, "%s: can't read reg %d : %d\n",
  210. __func__, PIXCIR_REG_POWER_MODE, ret);
  211. return ret;
  212. }
  213. ret &= ~PIXCIR_POWER_MODE_MASK;
  214. ret |= mode;
  215. /* Always AUTO_IDLE */
  216. ret |= PIXCIR_POWER_ALLOW_IDLE;
  217. ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret);
  218. if (ret < 0) {
  219. dev_err(dev, "%s: can't write reg %d : %d\n",
  220. __func__, PIXCIR_REG_POWER_MODE, ret);
  221. return ret;
  222. }
  223. if (mode == PIXCIR_POWER_HALT) {
  224. if (ts->gpio_wake)
  225. gpiod_set_value_cansleep(ts->gpio_wake, 0);
  226. }
  227. return 0;
  228. }
  229. /*
  230. * Set the interrupt mode for the device i.e. ATTB line behaviour
  231. *
  232. * @polarity : 1 for active high, 0 for active low.
  233. */
  234. static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts,
  235. enum pixcir_int_mode mode, bool polarity)
  236. {
  237. struct device *dev = &ts->client->dev;
  238. int ret;
  239. ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
  240. if (ret < 0) {
  241. dev_err(dev, "%s: can't read reg %d : %d\n",
  242. __func__, PIXCIR_REG_INT_MODE, ret);
  243. return ret;
  244. }
  245. ret &= ~PIXCIR_INT_MODE_MASK;
  246. ret |= mode;
  247. if (polarity)
  248. ret |= PIXCIR_INT_POL_HIGH;
  249. else
  250. ret &= ~PIXCIR_INT_POL_HIGH;
  251. ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
  252. if (ret < 0) {
  253. dev_err(dev, "%s: can't write reg %d : %d\n",
  254. __func__, PIXCIR_REG_INT_MODE, ret);
  255. return ret;
  256. }
  257. return 0;
  258. }
  259. /*
  260. * Enable/disable interrupt generation
  261. */
  262. static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable)
  263. {
  264. struct device *dev = &ts->client->dev;
  265. int ret;
  266. ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
  267. if (ret < 0) {
  268. dev_err(dev, "%s: can't read reg %d : %d\n",
  269. __func__, PIXCIR_REG_INT_MODE, ret);
  270. return ret;
  271. }
  272. if (enable)
  273. ret |= PIXCIR_INT_ENABLE;
  274. else
  275. ret &= ~PIXCIR_INT_ENABLE;
  276. ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
  277. if (ret < 0) {
  278. dev_err(dev, "%s: can't write reg %d : %d\n",
  279. __func__, PIXCIR_REG_INT_MODE, ret);
  280. return ret;
  281. }
  282. return 0;
  283. }
  284. static int pixcir_start(struct pixcir_i2c_ts_data *ts)
  285. {
  286. struct device *dev = &ts->client->dev;
  287. int error;
  288. if (ts->gpio_enable) {
  289. gpiod_set_value_cansleep(ts->gpio_enable, 1);
  290. msleep(100);
  291. }
  292. /* LEVEL_TOUCH interrupt with active low polarity */
  293. error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0);
  294. if (error) {
  295. dev_err(dev, "Failed to set interrupt mode: %d\n", error);
  296. return error;
  297. }
  298. ts->running = true;
  299. mb(); /* Update status before IRQ can fire */
  300. /* enable interrupt generation */
  301. error = pixcir_int_enable(ts, true);
  302. if (error) {
  303. dev_err(dev, "Failed to enable interrupt generation: %d\n",
  304. error);
  305. return error;
  306. }
  307. return 0;
  308. }
  309. static int pixcir_stop(struct pixcir_i2c_ts_data *ts)
  310. {
  311. int error;
  312. /* Disable interrupt generation */
  313. error = pixcir_int_enable(ts, false);
  314. if (error) {
  315. dev_err(&ts->client->dev,
  316. "Failed to disable interrupt generation: %d\n",
  317. error);
  318. return error;
  319. }
  320. /* Exit ISR if running, no more report parsing */
  321. ts->running = false;
  322. mb(); /* update status before we synchronize irq */
  323. /* Wait till running ISR is complete */
  324. synchronize_irq(ts->client->irq);
  325. if (ts->gpio_enable)
  326. gpiod_set_value_cansleep(ts->gpio_enable, 0);
  327. return 0;
  328. }
  329. static int pixcir_input_open(struct input_dev *dev)
  330. {
  331. struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
  332. return pixcir_start(ts);
  333. }
  334. static void pixcir_input_close(struct input_dev *dev)
  335. {
  336. struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
  337. pixcir_stop(ts);
  338. }
  339. static int __maybe_unused pixcir_i2c_ts_suspend(struct device *dev)
  340. {
  341. struct i2c_client *client = to_i2c_client(dev);
  342. struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
  343. struct input_dev *input = ts->input;
  344. int ret = 0;
  345. mutex_lock(&input->mutex);
  346. if (device_may_wakeup(&client->dev)) {
  347. if (!input_device_enabled(input)) {
  348. ret = pixcir_start(ts);
  349. if (ret) {
  350. dev_err(dev, "Failed to start\n");
  351. goto unlock;
  352. }
  353. }
  354. } else if (input_device_enabled(input)) {
  355. ret = pixcir_stop(ts);
  356. }
  357. unlock:
  358. mutex_unlock(&input->mutex);
  359. return ret;
  360. }
  361. static int __maybe_unused pixcir_i2c_ts_resume(struct device *dev)
  362. {
  363. struct i2c_client *client = to_i2c_client(dev);
  364. struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
  365. struct input_dev *input = ts->input;
  366. int ret = 0;
  367. mutex_lock(&input->mutex);
  368. if (device_may_wakeup(&client->dev)) {
  369. if (!input_device_enabled(input)) {
  370. ret = pixcir_stop(ts);
  371. if (ret) {
  372. dev_err(dev, "Failed to stop\n");
  373. goto unlock;
  374. }
  375. }
  376. } else if (input_device_enabled(input)) {
  377. ret = pixcir_start(ts);
  378. }
  379. unlock:
  380. mutex_unlock(&input->mutex);
  381. return ret;
  382. }
  383. static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
  384. pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
  385. static int pixcir_i2c_ts_probe(struct i2c_client *client,
  386. const struct i2c_device_id *id)
  387. {
  388. struct device *dev = &client->dev;
  389. struct pixcir_i2c_ts_data *tsdata;
  390. struct input_dev *input;
  391. int error;
  392. tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL);
  393. if (!tsdata)
  394. return -ENOMEM;
  395. tsdata->chip = device_get_match_data(dev);
  396. if (!tsdata->chip && id)
  397. tsdata->chip = (const void *)id->driver_data;
  398. if (!tsdata->chip) {
  399. dev_err(dev, "can't locate chip data\n");
  400. return -EINVAL;
  401. }
  402. input = devm_input_allocate_device(dev);
  403. if (!input) {
  404. dev_err(dev, "Failed to allocate input device\n");
  405. return -ENOMEM;
  406. }
  407. tsdata->client = client;
  408. tsdata->input = input;
  409. input->name = client->name;
  410. input->id.bustype = BUS_I2C;
  411. input->open = pixcir_input_open;
  412. input->close = pixcir_input_close;
  413. input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
  414. input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
  415. touchscreen_parse_properties(input, true, &tsdata->prop);
  416. if (!input_abs_get_max(input, ABS_MT_POSITION_X) ||
  417. !input_abs_get_max(input, ABS_MT_POSITION_Y)) {
  418. dev_err(dev, "Touchscreen size is not specified\n");
  419. return -EINVAL;
  420. }
  421. error = input_mt_init_slots(input, tsdata->chip->max_fingers,
  422. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  423. if (error) {
  424. dev_err(dev, "Error initializing Multi-Touch slots\n");
  425. return error;
  426. }
  427. input_set_drvdata(input, tsdata);
  428. tsdata->gpio_attb = devm_gpiod_get(dev, "attb", GPIOD_IN);
  429. if (IS_ERR(tsdata->gpio_attb)) {
  430. error = PTR_ERR(tsdata->gpio_attb);
  431. if (error != -EPROBE_DEFER)
  432. dev_err(dev, "Failed to request ATTB gpio: %d\n",
  433. error);
  434. return error;
  435. }
  436. tsdata->gpio_reset = devm_gpiod_get_optional(dev, "reset",
  437. GPIOD_OUT_LOW);
  438. if (IS_ERR(tsdata->gpio_reset)) {
  439. error = PTR_ERR(tsdata->gpio_reset);
  440. if (error != -EPROBE_DEFER)
  441. dev_err(dev, "Failed to request RESET gpio: %d\n",
  442. error);
  443. return error;
  444. }
  445. tsdata->gpio_wake = devm_gpiod_get_optional(dev, "wake",
  446. GPIOD_OUT_HIGH);
  447. if (IS_ERR(tsdata->gpio_wake)) {
  448. error = PTR_ERR(tsdata->gpio_wake);
  449. if (error != -EPROBE_DEFER)
  450. dev_err(dev, "Failed to get wake gpio: %d\n", error);
  451. return error;
  452. }
  453. tsdata->gpio_enable = devm_gpiod_get_optional(dev, "enable",
  454. GPIOD_OUT_HIGH);
  455. if (IS_ERR(tsdata->gpio_enable)) {
  456. error = PTR_ERR(tsdata->gpio_enable);
  457. if (error != -EPROBE_DEFER)
  458. dev_err(dev, "Failed to get enable gpio: %d\n", error);
  459. return error;
  460. }
  461. if (tsdata->gpio_enable)
  462. msleep(100);
  463. error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
  464. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  465. client->name, tsdata);
  466. if (error) {
  467. dev_err(dev, "failed to request irq %d\n", client->irq);
  468. return error;
  469. }
  470. pixcir_reset(tsdata);
  471. /* Always be in IDLE mode to save power, device supports auto wake */
  472. error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE);
  473. if (error) {
  474. dev_err(dev, "Failed to set IDLE mode\n");
  475. return error;
  476. }
  477. /* Stop device till opened */
  478. error = pixcir_stop(tsdata);
  479. if (error)
  480. return error;
  481. error = input_register_device(input);
  482. if (error)
  483. return error;
  484. i2c_set_clientdata(client, tsdata);
  485. return 0;
  486. }
  487. static const struct pixcir_i2c_chip_data pixcir_ts_data = {
  488. .max_fingers = 2,
  489. /* no hw id support */
  490. };
  491. static const struct pixcir_i2c_chip_data pixcir_tangoc_data = {
  492. .max_fingers = 5,
  493. .has_hw_ids = true,
  494. };
  495. static const struct i2c_device_id pixcir_i2c_ts_id[] = {
  496. { "pixcir_ts", (unsigned long) &pixcir_ts_data },
  497. { "pixcir_tangoc", (unsigned long) &pixcir_tangoc_data },
  498. { }
  499. };
  500. MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
  501. #ifdef CONFIG_OF
  502. static const struct of_device_id pixcir_of_match[] = {
  503. { .compatible = "pixcir,pixcir_ts", .data = &pixcir_ts_data },
  504. { .compatible = "pixcir,pixcir_tangoc", .data = &pixcir_tangoc_data },
  505. { }
  506. };
  507. MODULE_DEVICE_TABLE(of, pixcir_of_match);
  508. #endif
  509. static struct i2c_driver pixcir_i2c_ts_driver = {
  510. .driver = {
  511. .name = "pixcir_ts",
  512. .pm = &pixcir_dev_pm_ops,
  513. .of_match_table = of_match_ptr(pixcir_of_match),
  514. },
  515. .probe = pixcir_i2c_ts_probe,
  516. .id_table = pixcir_i2c_ts_id,
  517. };
  518. module_i2c_driver(pixcir_i2c_ts_driver);
  519. MODULE_AUTHOR("Jianchun Bian <[email protected]>, Dequan Meng <[email protected]>");
  520. MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
  521. MODULE_LICENSE("GPL");