st1232.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * ST1232 Touchscreen Controller Driver
  3. *
  4. * Copyright (C) 2010 Renesas Solutions Corp.
  5. * Tony SIM <chinyeow.sim.xt@renesas.com>
  6. *
  7. * Using code from:
  8. * - android.git.kernel.org: projects/kernel/common.git: synaptics_i2c_rmi.c
  9. * Copyright (C) 2007 Google, Inc.
  10. *
  11. * This software is licensed under the terms of the GNU General Public
  12. * License version 2, as published by the Free Software Foundation, and
  13. * may be copied, distributed, and modified under those terms.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/delay.h>
  21. #include <linux/i2c.h>
  22. #include <linux/input.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/module.h>
  25. #include <linux/pm_qos.h>
  26. #include <linux/slab.h>
  27. #include <linux/types.h>
  28. #define ST1232_TS_NAME "st1232-ts"
  29. #define MIN_X 0x00
  30. #define MIN_Y 0x00
  31. #define MAX_X 0x31f /* (800 - 1) */
  32. #define MAX_Y 0x1df /* (480 - 1) */
  33. #define MAX_AREA 0xff
  34. #define MAX_FINGERS 2
  35. struct st1232_ts_finger {
  36. u16 x;
  37. u16 y;
  38. u8 t;
  39. bool is_valid;
  40. };
  41. struct st1232_ts_data {
  42. struct i2c_client *client;
  43. struct input_dev *input_dev;
  44. struct st1232_ts_finger finger[MAX_FINGERS];
  45. struct dev_pm_qos_request low_latency_req;
  46. };
  47. static int st1232_ts_read_data(struct st1232_ts_data *ts)
  48. {
  49. struct st1232_ts_finger *finger = ts->finger;
  50. struct i2c_client *client = ts->client;
  51. struct i2c_msg msg[2];
  52. int error;
  53. u8 start_reg;
  54. u8 buf[10];
  55. /* read touchscreen data from ST1232 */
  56. msg[0].addr = client->addr;
  57. msg[0].flags = 0;
  58. msg[0].len = 1;
  59. msg[0].buf = &start_reg;
  60. start_reg = 0x10;
  61. msg[1].addr = ts->client->addr;
  62. msg[1].flags = I2C_M_RD;
  63. msg[1].len = sizeof(buf);
  64. msg[1].buf = buf;
  65. error = i2c_transfer(client->adapter, msg, 2);
  66. if (error < 0)
  67. return error;
  68. /* get "valid" bits */
  69. finger[0].is_valid = buf[2] >> 7;
  70. finger[1].is_valid = buf[5] >> 7;
  71. /* get xy coordinate */
  72. if (finger[0].is_valid) {
  73. finger[0].x = ((buf[2] & 0x0070) << 4) | buf[3];
  74. finger[0].y = ((buf[2] & 0x0007) << 8) | buf[4];
  75. finger[0].t = buf[8];
  76. }
  77. if (finger[1].is_valid) {
  78. finger[1].x = ((buf[5] & 0x0070) << 4) | buf[6];
  79. finger[1].y = ((buf[5] & 0x0007) << 8) | buf[7];
  80. finger[1].t = buf[9];
  81. }
  82. return 0;
  83. }
  84. static irqreturn_t st1232_ts_irq_handler(int irq, void *dev_id)
  85. {
  86. struct st1232_ts_data *ts = dev_id;
  87. struct st1232_ts_finger *finger = ts->finger;
  88. struct input_dev *input_dev = ts->input_dev;
  89. int count = 0;
  90. int i, ret;
  91. ret = st1232_ts_read_data(ts);
  92. if (ret < 0)
  93. goto end;
  94. /* multi touch protocol */
  95. for (i = 0; i < MAX_FINGERS; i++) {
  96. if (!finger[i].is_valid)
  97. continue;
  98. input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, finger[i].t);
  99. input_report_abs(input_dev, ABS_MT_POSITION_X, finger[i].x);
  100. input_report_abs(input_dev, ABS_MT_POSITION_Y, finger[i].y);
  101. input_mt_sync(input_dev);
  102. count++;
  103. }
  104. /* SYN_MT_REPORT only if no contact */
  105. if (!count) {
  106. input_mt_sync(input_dev);
  107. if (ts->low_latency_req.dev) {
  108. dev_pm_qos_remove_request(&ts->low_latency_req);
  109. ts->low_latency_req.dev = NULL;
  110. }
  111. } else if (!ts->low_latency_req.dev) {
  112. /* First contact, request 100 us latency. */
  113. dev_pm_qos_add_ancestor_request(&ts->client->dev,
  114. &ts->low_latency_req, 100);
  115. }
  116. /* SYN_REPORT */
  117. input_sync(input_dev);
  118. end:
  119. return IRQ_HANDLED;
  120. }
  121. static int __devinit st1232_ts_probe(struct i2c_client *client,
  122. const struct i2c_device_id *id)
  123. {
  124. struct st1232_ts_data *ts;
  125. struct input_dev *input_dev;
  126. int error;
  127. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  128. dev_err(&client->dev, "need I2C_FUNC_I2C\n");
  129. return -EIO;
  130. }
  131. if (!client->irq) {
  132. dev_err(&client->dev, "no IRQ?\n");
  133. return -EINVAL;
  134. }
  135. ts = kzalloc(sizeof(struct st1232_ts_data), GFP_KERNEL);
  136. input_dev = input_allocate_device();
  137. if (!ts || !input_dev) {
  138. error = -ENOMEM;
  139. goto err_free_mem;
  140. }
  141. ts->client = client;
  142. ts->input_dev = input_dev;
  143. input_dev->name = "st1232-touchscreen";
  144. input_dev->id.bustype = BUS_I2C;
  145. input_dev->dev.parent = &client->dev;
  146. __set_bit(EV_SYN, input_dev->evbit);
  147. __set_bit(EV_KEY, input_dev->evbit);
  148. __set_bit(EV_ABS, input_dev->evbit);
  149. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, MAX_AREA, 0, 0);
  150. input_set_abs_params(input_dev, ABS_MT_POSITION_X, MIN_X, MAX_X, 0, 0);
  151. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, MIN_Y, MAX_Y, 0, 0);
  152. error = request_threaded_irq(client->irq, NULL, st1232_ts_irq_handler,
  153. IRQF_ONESHOT, client->name, ts);
  154. if (error) {
  155. dev_err(&client->dev, "Failed to register interrupt\n");
  156. goto err_free_mem;
  157. }
  158. error = input_register_device(ts->input_dev);
  159. if (error) {
  160. dev_err(&client->dev, "Unable to register %s input device\n",
  161. input_dev->name);
  162. goto err_free_irq;
  163. }
  164. i2c_set_clientdata(client, ts);
  165. device_init_wakeup(&client->dev, 1);
  166. return 0;
  167. err_free_irq:
  168. free_irq(client->irq, ts);
  169. err_free_mem:
  170. input_free_device(input_dev);
  171. kfree(ts);
  172. return error;
  173. }
  174. static int __devexit st1232_ts_remove(struct i2c_client *client)
  175. {
  176. struct st1232_ts_data *ts = i2c_get_clientdata(client);
  177. device_init_wakeup(&client->dev, 0);
  178. free_irq(client->irq, ts);
  179. input_unregister_device(ts->input_dev);
  180. kfree(ts);
  181. return 0;
  182. }
  183. #ifdef CONFIG_PM
  184. static int st1232_ts_suspend(struct device *dev)
  185. {
  186. struct i2c_client *client = to_i2c_client(dev);
  187. if (device_may_wakeup(&client->dev))
  188. enable_irq_wake(client->irq);
  189. else
  190. disable_irq(client->irq);
  191. return 0;
  192. }
  193. static int st1232_ts_resume(struct device *dev)
  194. {
  195. struct i2c_client *client = to_i2c_client(dev);
  196. if (device_may_wakeup(&client->dev))
  197. disable_irq_wake(client->irq);
  198. else
  199. enable_irq(client->irq);
  200. return 0;
  201. }
  202. static const struct dev_pm_ops st1232_ts_pm_ops = {
  203. .suspend = st1232_ts_suspend,
  204. .resume = st1232_ts_resume,
  205. };
  206. #endif
  207. static const struct i2c_device_id st1232_ts_id[] = {
  208. { ST1232_TS_NAME, 0 },
  209. { }
  210. };
  211. MODULE_DEVICE_TABLE(i2c, st1232_ts_id);
  212. static struct i2c_driver st1232_ts_driver = {
  213. .probe = st1232_ts_probe,
  214. .remove = __devexit_p(st1232_ts_remove),
  215. .id_table = st1232_ts_id,
  216. .driver = {
  217. .name = ST1232_TS_NAME,
  218. .owner = THIS_MODULE,
  219. #ifdef CONFIG_PM
  220. .pm = &st1232_ts_pm_ops,
  221. #endif
  222. },
  223. };
  224. module_i2c_driver(st1232_ts_driver);
  225. MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>");
  226. MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver");
  227. MODULE_LICENSE("GPL");