pixcir_i2c_ts.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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/pixcir_ts.h>
  26. #include <linux/gpio.h>
  27. struct pixcir_i2c_ts_data {
  28. struct i2c_client *client;
  29. struct input_dev *input;
  30. const struct pixcir_ts_platform_data *chip;
  31. bool running;
  32. };
  33. static void pixcir_ts_poscheck(struct pixcir_i2c_ts_data *data)
  34. {
  35. struct pixcir_i2c_ts_data *tsdata = data;
  36. u8 rdbuf[10], wrbuf[1] = { 0 };
  37. u8 touch;
  38. int ret;
  39. ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf));
  40. if (ret != sizeof(wrbuf)) {
  41. dev_err(&tsdata->client->dev,
  42. "%s: i2c_master_send failed(), ret=%d\n",
  43. __func__, ret);
  44. return;
  45. }
  46. ret = i2c_master_recv(tsdata->client, rdbuf, sizeof(rdbuf));
  47. if (ret != sizeof(rdbuf)) {
  48. dev_err(&tsdata->client->dev,
  49. "%s: i2c_master_recv failed(), ret=%d\n",
  50. __func__, ret);
  51. return;
  52. }
  53. touch = rdbuf[0];
  54. if (touch) {
  55. u16 posx1 = (rdbuf[3] << 8) | rdbuf[2];
  56. u16 posy1 = (rdbuf[5] << 8) | rdbuf[4];
  57. u16 posx2 = (rdbuf[7] << 8) | rdbuf[6];
  58. u16 posy2 = (rdbuf[9] << 8) | rdbuf[8];
  59. input_report_key(tsdata->input, BTN_TOUCH, 1);
  60. input_report_abs(tsdata->input, ABS_X, posx1);
  61. input_report_abs(tsdata->input, ABS_Y, posy1);
  62. input_report_abs(tsdata->input, ABS_MT_POSITION_X, posx1);
  63. input_report_abs(tsdata->input, ABS_MT_POSITION_Y, posy1);
  64. input_mt_sync(tsdata->input);
  65. if (touch == 2) {
  66. input_report_abs(tsdata->input,
  67. ABS_MT_POSITION_X, posx2);
  68. input_report_abs(tsdata->input,
  69. ABS_MT_POSITION_Y, posy2);
  70. input_mt_sync(tsdata->input);
  71. }
  72. } else {
  73. input_report_key(tsdata->input, BTN_TOUCH, 0);
  74. }
  75. input_sync(tsdata->input);
  76. }
  77. static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
  78. {
  79. struct pixcir_i2c_ts_data *tsdata = dev_id;
  80. const struct pixcir_ts_platform_data *pdata = tsdata->chip;
  81. while (tsdata->running) {
  82. pixcir_ts_poscheck(tsdata);
  83. if (gpio_get_value(pdata->gpio_attb))
  84. break;
  85. msleep(20);
  86. }
  87. return IRQ_HANDLED;
  88. }
  89. static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts,
  90. enum pixcir_power_mode mode)
  91. {
  92. struct device *dev = &ts->client->dev;
  93. int ret;
  94. ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE);
  95. if (ret < 0) {
  96. dev_err(dev, "%s: can't read reg 0x%x : %d\n",
  97. __func__, PIXCIR_REG_POWER_MODE, ret);
  98. return ret;
  99. }
  100. ret &= ~PIXCIR_POWER_MODE_MASK;
  101. ret |= mode;
  102. /* Always AUTO_IDLE */
  103. ret |= PIXCIR_POWER_ALLOW_IDLE;
  104. ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret);
  105. if (ret < 0) {
  106. dev_err(dev, "%s: can't write reg 0x%x : %d\n",
  107. __func__, PIXCIR_REG_POWER_MODE, ret);
  108. return ret;
  109. }
  110. return 0;
  111. }
  112. /*
  113. * Set the interrupt mode for the device i.e. ATTB line behaviour
  114. *
  115. * @polarity : 1 for active high, 0 for active low.
  116. */
  117. static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts,
  118. enum pixcir_int_mode mode, bool polarity)
  119. {
  120. struct device *dev = &ts->client->dev;
  121. int ret;
  122. ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
  123. if (ret < 0) {
  124. dev_err(dev, "%s: can't read reg 0x%x : %d\n",
  125. __func__, PIXCIR_REG_INT_MODE, ret);
  126. return ret;
  127. }
  128. ret &= ~PIXCIR_INT_MODE_MASK;
  129. ret |= mode;
  130. if (polarity)
  131. ret |= PIXCIR_INT_POL_HIGH;
  132. else
  133. ret &= ~PIXCIR_INT_POL_HIGH;
  134. ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
  135. if (ret < 0) {
  136. dev_err(dev, "%s: can't write reg 0x%x : %d\n",
  137. __func__, PIXCIR_REG_INT_MODE, ret);
  138. return ret;
  139. }
  140. return 0;
  141. }
  142. /*
  143. * Enable/disable interrupt generation
  144. */
  145. static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable)
  146. {
  147. struct device *dev = &ts->client->dev;
  148. int ret;
  149. ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
  150. if (ret < 0) {
  151. dev_err(dev, "%s: can't read reg 0x%x : %d\n",
  152. __func__, PIXCIR_REG_INT_MODE, ret);
  153. return ret;
  154. }
  155. if (enable)
  156. ret |= PIXCIR_INT_ENABLE;
  157. else
  158. ret &= ~PIXCIR_INT_ENABLE;
  159. ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
  160. if (ret < 0) {
  161. dev_err(dev, "%s: can't write reg 0x%x : %d\n",
  162. __func__, PIXCIR_REG_INT_MODE, ret);
  163. return ret;
  164. }
  165. return 0;
  166. }
  167. static int pixcir_start(struct pixcir_i2c_ts_data *ts)
  168. {
  169. struct device *dev = &ts->client->dev;
  170. int error;
  171. /* LEVEL_TOUCH interrupt with active low polarity */
  172. error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0);
  173. if (error) {
  174. dev_err(dev, "Failed to set interrupt mode: %d\n", error);
  175. return error;
  176. }
  177. ts->running = true;
  178. mb(); /* Update status before IRQ can fire */
  179. /* enable interrupt generation */
  180. error = pixcir_int_enable(ts, true);
  181. if (error) {
  182. dev_err(dev, "Failed to enable interrupt generation: %d\n",
  183. error);
  184. return error;
  185. }
  186. return 0;
  187. }
  188. static int pixcir_stop(struct pixcir_i2c_ts_data *ts)
  189. {
  190. int error;
  191. /* Disable interrupt generation */
  192. error = pixcir_int_enable(ts, false);
  193. if (error) {
  194. dev_err(&ts->client->dev,
  195. "Failed to disable interrupt generation: %d\n",
  196. error);
  197. return error;
  198. }
  199. /* Exit ISR if running, no more report parsing */
  200. ts->running = false;
  201. mb(); /* update status before we synchronize irq */
  202. /* Wait till running ISR is complete */
  203. synchronize_irq(ts->client->irq);
  204. return 0;
  205. }
  206. static int pixcir_input_open(struct input_dev *dev)
  207. {
  208. struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
  209. return pixcir_start(ts);
  210. }
  211. static void pixcir_input_close(struct input_dev *dev)
  212. {
  213. struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
  214. pixcir_stop(ts);
  215. }
  216. #ifdef CONFIG_PM_SLEEP
  217. static int pixcir_i2c_ts_suspend(struct device *dev)
  218. {
  219. struct i2c_client *client = to_i2c_client(dev);
  220. struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
  221. struct input_dev *input = ts->input;
  222. int ret = 0;
  223. mutex_lock(&input->mutex);
  224. if (device_may_wakeup(&client->dev)) {
  225. if (!input->users) {
  226. ret = pixcir_start(ts);
  227. if (ret) {
  228. dev_err(dev, "Failed to start\n");
  229. goto unlock;
  230. }
  231. }
  232. enable_irq_wake(client->irq);
  233. } else if (input->users) {
  234. ret = pixcir_stop(ts);
  235. }
  236. unlock:
  237. mutex_unlock(&input->mutex);
  238. return ret;
  239. }
  240. static int pixcir_i2c_ts_resume(struct device *dev)
  241. {
  242. struct i2c_client *client = to_i2c_client(dev);
  243. struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
  244. struct input_dev *input = ts->input;
  245. int ret = 0;
  246. mutex_lock(&input->mutex);
  247. if (device_may_wakeup(&client->dev)) {
  248. disable_irq_wake(client->irq);
  249. if (!input->users) {
  250. ret = pixcir_stop(ts);
  251. if (ret) {
  252. dev_err(dev, "Failed to stop\n");
  253. goto unlock;
  254. }
  255. }
  256. } else if (input->users) {
  257. ret = pixcir_start(ts);
  258. }
  259. unlock:
  260. mutex_unlock(&input->mutex);
  261. return ret;
  262. }
  263. #endif
  264. static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
  265. pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
  266. static int pixcir_i2c_ts_probe(struct i2c_client *client,
  267. const struct i2c_device_id *id)
  268. {
  269. const struct pixcir_ts_platform_data *pdata =
  270. dev_get_platdata(&client->dev);
  271. struct device *dev = &client->dev;
  272. struct pixcir_i2c_ts_data *tsdata;
  273. struct input_dev *input;
  274. int error;
  275. if (!pdata) {
  276. dev_err(&client->dev, "platform data not defined\n");
  277. return -EINVAL;
  278. }
  279. if (!gpio_is_valid(pdata->gpio_attb)) {
  280. dev_err(dev, "Invalid gpio_attb in pdata\n");
  281. return -EINVAL;
  282. }
  283. tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL);
  284. if (!tsdata)
  285. return -ENOMEM;
  286. input = devm_input_allocate_device(dev);
  287. if (!input) {
  288. dev_err(dev, "Failed to allocate input device\n");
  289. return -ENOMEM;
  290. }
  291. tsdata->client = client;
  292. tsdata->input = input;
  293. tsdata->chip = pdata;
  294. input->name = client->name;
  295. input->id.bustype = BUS_I2C;
  296. input->open = pixcir_input_open;
  297. input->close = pixcir_input_close;
  298. input->dev.parent = &client->dev;
  299. __set_bit(EV_KEY, input->evbit);
  300. __set_bit(EV_ABS, input->evbit);
  301. __set_bit(BTN_TOUCH, input->keybit);
  302. input_set_abs_params(input, ABS_X, 0, pdata->x_max, 0, 0);
  303. input_set_abs_params(input, ABS_Y, 0, pdata->y_max, 0, 0);
  304. input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0);
  305. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0);
  306. input_set_drvdata(input, tsdata);
  307. error = devm_gpio_request_one(dev, pdata->gpio_attb,
  308. GPIOF_DIR_IN, "pixcir_i2c_attb");
  309. if (error) {
  310. dev_err(dev, "Failed to request ATTB gpio\n");
  311. return error;
  312. }
  313. error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
  314. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  315. client->name, tsdata);
  316. if (error) {
  317. dev_err(dev, "failed to request irq %d\n", client->irq);
  318. return error;
  319. }
  320. /* Always be in IDLE mode to save power, device supports auto wake */
  321. error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE);
  322. if (error) {
  323. dev_err(dev, "Failed to set IDLE mode\n");
  324. return error;
  325. }
  326. /* Stop device till opened */
  327. error = pixcir_stop(tsdata);
  328. if (error)
  329. return error;
  330. error = input_register_device(input);
  331. if (error)
  332. return error;
  333. i2c_set_clientdata(client, tsdata);
  334. device_init_wakeup(&client->dev, 1);
  335. return 0;
  336. }
  337. static int pixcir_i2c_ts_remove(struct i2c_client *client)
  338. {
  339. device_init_wakeup(&client->dev, 0);
  340. return 0;
  341. }
  342. static const struct i2c_device_id pixcir_i2c_ts_id[] = {
  343. { "pixcir_ts", 0 },
  344. { }
  345. };
  346. MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
  347. static struct i2c_driver pixcir_i2c_ts_driver = {
  348. .driver = {
  349. .owner = THIS_MODULE,
  350. .name = "pixcir_ts",
  351. .pm = &pixcir_dev_pm_ops,
  352. },
  353. .probe = pixcir_i2c_ts_probe,
  354. .remove = pixcir_i2c_ts_remove,
  355. .id_table = pixcir_i2c_ts_id,
  356. };
  357. module_i2c_driver(pixcir_i2c_ts_driver);
  358. MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
  359. MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
  360. MODULE_LICENSE("GPL");