pixcir_i2c_ts.c 15 KB

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