pixcir_i2c_ts.c 14 KB

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