pixcir_i2c_ts.c 15 KB

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