pixcir_i2c_ts.c 14 KB

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