pixcir_i2c_ts.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. struct pixcir_i2c_ts_data {
  27. struct i2c_client *client;
  28. struct input_dev *input;
  29. const struct pixcir_ts_platform_data *chip;
  30. bool exiting;
  31. };
  32. static void pixcir_ts_poscheck(struct pixcir_i2c_ts_data *data)
  33. {
  34. struct pixcir_i2c_ts_data *tsdata = data;
  35. u8 rdbuf[10], wrbuf[1] = { 0 };
  36. u8 touch;
  37. int ret;
  38. ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf));
  39. if (ret != sizeof(wrbuf)) {
  40. dev_err(&tsdata->client->dev,
  41. "%s: i2c_master_send failed(), ret=%d\n",
  42. __func__, ret);
  43. return;
  44. }
  45. ret = i2c_master_recv(tsdata->client, rdbuf, sizeof(rdbuf));
  46. if (ret != sizeof(rdbuf)) {
  47. dev_err(&tsdata->client->dev,
  48. "%s: i2c_master_recv failed(), ret=%d\n",
  49. __func__, ret);
  50. return;
  51. }
  52. touch = rdbuf[0];
  53. if (touch) {
  54. u16 posx1 = (rdbuf[3] << 8) | rdbuf[2];
  55. u16 posy1 = (rdbuf[5] << 8) | rdbuf[4];
  56. u16 posx2 = (rdbuf[7] << 8) | rdbuf[6];
  57. u16 posy2 = (rdbuf[9] << 8) | rdbuf[8];
  58. input_report_key(tsdata->input, BTN_TOUCH, 1);
  59. input_report_abs(tsdata->input, ABS_X, posx1);
  60. input_report_abs(tsdata->input, ABS_Y, posy1);
  61. input_report_abs(tsdata->input, ABS_MT_POSITION_X, posx1);
  62. input_report_abs(tsdata->input, ABS_MT_POSITION_Y, posy1);
  63. input_mt_sync(tsdata->input);
  64. if (touch == 2) {
  65. input_report_abs(tsdata->input,
  66. ABS_MT_POSITION_X, posx2);
  67. input_report_abs(tsdata->input,
  68. ABS_MT_POSITION_Y, posy2);
  69. input_mt_sync(tsdata->input);
  70. }
  71. } else {
  72. input_report_key(tsdata->input, BTN_TOUCH, 0);
  73. }
  74. input_sync(tsdata->input);
  75. }
  76. static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
  77. {
  78. struct pixcir_i2c_ts_data *tsdata = dev_id;
  79. while (!tsdata->exiting) {
  80. pixcir_ts_poscheck(tsdata);
  81. if (tsdata->chip->attb_read_val())
  82. break;
  83. msleep(20);
  84. }
  85. return IRQ_HANDLED;
  86. }
  87. #ifdef CONFIG_PM_SLEEP
  88. static int pixcir_i2c_ts_suspend(struct device *dev)
  89. {
  90. struct i2c_client *client = to_i2c_client(dev);
  91. if (device_may_wakeup(&client->dev))
  92. enable_irq_wake(client->irq);
  93. return 0;
  94. }
  95. static int pixcir_i2c_ts_resume(struct device *dev)
  96. {
  97. struct i2c_client *client = to_i2c_client(dev);
  98. if (device_may_wakeup(&client->dev))
  99. disable_irq_wake(client->irq);
  100. return 0;
  101. }
  102. #endif
  103. static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
  104. pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
  105. static int __devinit pixcir_i2c_ts_probe(struct i2c_client *client,
  106. const struct i2c_device_id *id)
  107. {
  108. const struct pixcir_ts_platform_data *pdata = client->dev.platform_data;
  109. struct pixcir_i2c_ts_data *tsdata;
  110. struct input_dev *input;
  111. int error;
  112. if (!pdata) {
  113. dev_err(&client->dev, "platform data not defined\n");
  114. return -EINVAL;
  115. }
  116. tsdata = kzalloc(sizeof(*tsdata), GFP_KERNEL);
  117. input = input_allocate_device();
  118. if (!tsdata || !input) {
  119. dev_err(&client->dev, "Failed to allocate driver data!\n");
  120. error = -ENOMEM;
  121. goto err_free_mem;
  122. }
  123. tsdata->client = client;
  124. tsdata->input = input;
  125. tsdata->chip = pdata;
  126. input->name = client->name;
  127. input->id.bustype = BUS_I2C;
  128. input->dev.parent = &client->dev;
  129. __set_bit(EV_KEY, input->evbit);
  130. __set_bit(EV_ABS, input->evbit);
  131. __set_bit(BTN_TOUCH, input->keybit);
  132. input_set_abs_params(input, ABS_X, 0, pdata->x_max, 0, 0);
  133. input_set_abs_params(input, ABS_Y, 0, pdata->y_max, 0, 0);
  134. input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0);
  135. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0);
  136. input_set_drvdata(input, tsdata);
  137. error = request_threaded_irq(client->irq, NULL, pixcir_ts_isr,
  138. IRQF_TRIGGER_FALLING,
  139. client->name, tsdata);
  140. if (error) {
  141. dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
  142. goto err_free_mem;
  143. }
  144. error = input_register_device(input);
  145. if (error)
  146. goto err_free_irq;
  147. i2c_set_clientdata(client, tsdata);
  148. device_init_wakeup(&client->dev, 1);
  149. return 0;
  150. err_free_irq:
  151. free_irq(client->irq, tsdata);
  152. err_free_mem:
  153. input_free_device(input);
  154. kfree(tsdata);
  155. return error;
  156. }
  157. static int __devexit pixcir_i2c_ts_remove(struct i2c_client *client)
  158. {
  159. struct pixcir_i2c_ts_data *tsdata = i2c_get_clientdata(client);
  160. device_init_wakeup(&client->dev, 0);
  161. tsdata->exiting = true;
  162. mb();
  163. free_irq(client->irq, tsdata);
  164. input_unregister_device(tsdata->input);
  165. kfree(tsdata);
  166. return 0;
  167. }
  168. static const struct i2c_device_id pixcir_i2c_ts_id[] = {
  169. { "pixcir_ts", 0 },
  170. { }
  171. };
  172. MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
  173. static struct i2c_driver pixcir_i2c_ts_driver = {
  174. .driver = {
  175. .owner = THIS_MODULE,
  176. .name = "pixcir_ts",
  177. .pm = &pixcir_dev_pm_ops,
  178. },
  179. .probe = pixcir_i2c_ts_probe,
  180. .remove = __devexit_p(pixcir_i2c_ts_remove),
  181. .id_table = pixcir_i2c_ts_id,
  182. };
  183. module_i2c_driver(pixcir_i2c_ts_driver);
  184. MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
  185. MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
  186. MODULE_LICENSE("GPL");