pixcir_i2c_ts.c 15 KB

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