st1232.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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/slab.h>
  26. #include <linux/types.h>
  27. #define ST1232_TS_NAME "st1232-ts"
  28. #define MIN_X 0x00
  29. #define MIN_Y 0x00
  30. #define MAX_X 0x31f /* (800 - 1) */
  31. #define MAX_Y 0x1df /* (480 - 1) */
  32. #define MAX_AREA 0xff
  33. #define MAX_FINGERS 2
  34. struct st1232_ts_finger {
  35. u16 x;
  36. u16 y;
  37. u8 t;
  38. bool is_valid;
  39. };
  40. struct st1232_ts_data {
  41. struct i2c_client *client;
  42. struct input_dev *input_dev;
  43. struct st1232_ts_finger finger[MAX_FINGERS];
  44. };
  45. static int st1232_ts_read_data(struct st1232_ts_data *ts)
  46. {
  47. struct st1232_ts_finger *finger = ts->finger;
  48. struct i2c_client *client = ts->client;
  49. struct i2c_msg msg[2];
  50. int error;
  51. u8 start_reg;
  52. u8 buf[10];
  53. /* read touchscreen data from ST1232 */
  54. msg[0].addr = client->addr;
  55. msg[0].flags = 0;
  56. msg[0].len = 1;
  57. msg[0].buf = &start_reg;
  58. start_reg = 0x10;
  59. msg[1].addr = ts->client->addr;
  60. msg[1].flags = I2C_M_RD;
  61. msg[1].len = sizeof(buf);
  62. msg[1].buf = buf;
  63. error = i2c_transfer(client->adapter, msg, 2);
  64. if (error < 0)
  65. return error;
  66. /* get "valid" bits */
  67. finger[0].is_valid = buf[2] >> 7;
  68. finger[1].is_valid = buf[5] >> 7;
  69. /* get xy coordinate */
  70. if (finger[0].is_valid) {
  71. finger[0].x = ((buf[2] & 0x0070) << 4) | buf[3];
  72. finger[0].y = ((buf[2] & 0x0007) << 8) | buf[4];
  73. finger[0].t = buf[8];
  74. }
  75. if (finger[1].is_valid) {
  76. finger[1].x = ((buf[5] & 0x0070) << 4) | buf[6];
  77. finger[1].y = ((buf[5] & 0x0007) << 8) | buf[7];
  78. finger[1].t = buf[9];
  79. }
  80. return 0;
  81. }
  82. static irqreturn_t st1232_ts_irq_handler(int irq, void *dev_id)
  83. {
  84. struct st1232_ts_data *ts = dev_id;
  85. struct st1232_ts_finger *finger = ts->finger;
  86. struct input_dev *input_dev = ts->input_dev;
  87. int count = 0;
  88. int i, ret;
  89. ret = st1232_ts_read_data(ts);
  90. if (ret < 0)
  91. goto end;
  92. /* multi touch protocol */
  93. for (i = 0; i < MAX_FINGERS; i++) {
  94. if (!finger[i].is_valid)
  95. continue;
  96. input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, finger[i].t);
  97. input_report_abs(input_dev, ABS_MT_POSITION_X, finger[i].x);
  98. input_report_abs(input_dev, ABS_MT_POSITION_Y, finger[i].y);
  99. input_mt_sync(input_dev);
  100. count++;
  101. }
  102. /* SYN_MT_REPORT only if no contact */
  103. if (!count)
  104. input_mt_sync(input_dev);
  105. /* SYN_REPORT */
  106. input_sync(input_dev);
  107. end:
  108. return IRQ_HANDLED;
  109. }
  110. static int __devinit st1232_ts_probe(struct i2c_client *client,
  111. const struct i2c_device_id *id)
  112. {
  113. struct st1232_ts_data *ts;
  114. struct input_dev *input_dev;
  115. int error;
  116. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  117. dev_err(&client->dev, "need I2C_FUNC_I2C\n");
  118. return -EIO;
  119. }
  120. if (!client->irq) {
  121. dev_err(&client->dev, "no IRQ?\n");
  122. return -EINVAL;
  123. }
  124. ts = kzalloc(sizeof(struct st1232_ts_data), GFP_KERNEL);
  125. input_dev = input_allocate_device();
  126. if (!ts || !input_dev) {
  127. error = -ENOMEM;
  128. goto err_free_mem;
  129. }
  130. ts->client = client;
  131. ts->input_dev = input_dev;
  132. input_dev->name = "st1232-touchscreen";
  133. input_dev->id.bustype = BUS_I2C;
  134. input_dev->dev.parent = &client->dev;
  135. __set_bit(EV_SYN, input_dev->evbit);
  136. __set_bit(EV_KEY, input_dev->evbit);
  137. __set_bit(EV_ABS, input_dev->evbit);
  138. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, MAX_AREA, 0, 0);
  139. input_set_abs_params(input_dev, ABS_MT_POSITION_X, MIN_X, MAX_X, 0, 0);
  140. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, MIN_Y, MAX_Y, 0, 0);
  141. error = request_threaded_irq(client->irq, NULL, st1232_ts_irq_handler,
  142. IRQF_ONESHOT, client->name, ts);
  143. if (error) {
  144. dev_err(&client->dev, "Failed to register interrupt\n");
  145. goto err_free_mem;
  146. }
  147. error = input_register_device(ts->input_dev);
  148. if (error) {
  149. dev_err(&client->dev, "Unable to register %s input device\n",
  150. input_dev->name);
  151. goto err_free_irq;
  152. }
  153. i2c_set_clientdata(client, ts);
  154. device_init_wakeup(&client->dev, 1);
  155. return 0;
  156. err_free_irq:
  157. free_irq(client->irq, ts);
  158. err_free_mem:
  159. input_free_device(input_dev);
  160. kfree(ts);
  161. return error;
  162. }
  163. static int __devexit st1232_ts_remove(struct i2c_client *client)
  164. {
  165. struct st1232_ts_data *ts = i2c_get_clientdata(client);
  166. device_init_wakeup(&client->dev, 0);
  167. free_irq(client->irq, ts);
  168. input_unregister_device(ts->input_dev);
  169. kfree(ts);
  170. return 0;
  171. }
  172. #ifdef CONFIG_PM
  173. static int st1232_ts_suspend(struct device *dev)
  174. {
  175. struct i2c_client *client = to_i2c_client(dev);
  176. if (device_may_wakeup(&client->dev))
  177. enable_irq_wake(client->irq);
  178. else
  179. disable_irq(client->irq);
  180. return 0;
  181. }
  182. static int st1232_ts_resume(struct device *dev)
  183. {
  184. struct i2c_client *client = to_i2c_client(dev);
  185. if (device_may_wakeup(&client->dev))
  186. disable_irq_wake(client->irq);
  187. else
  188. enable_irq(client->irq);
  189. return 0;
  190. }
  191. static const struct dev_pm_ops st1232_ts_pm_ops = {
  192. .suspend = st1232_ts_suspend,
  193. .resume = st1232_ts_resume,
  194. };
  195. #endif
  196. static const struct i2c_device_id st1232_ts_id[] = {
  197. { ST1232_TS_NAME, 0 },
  198. { }
  199. };
  200. MODULE_DEVICE_TABLE(i2c, st1232_ts_id);
  201. static struct i2c_driver st1232_ts_driver = {
  202. .probe = st1232_ts_probe,
  203. .remove = __devexit_p(st1232_ts_remove),
  204. .id_table = st1232_ts_id,
  205. .driver = {
  206. .name = ST1232_TS_NAME,
  207. .owner = THIS_MODULE,
  208. #ifdef CONFIG_PM
  209. .pm = &st1232_ts_pm_ops,
  210. #endif
  211. },
  212. };
  213. static int __init st1232_ts_init(void)
  214. {
  215. return i2c_add_driver(&st1232_ts_driver);
  216. }
  217. module_init(st1232_ts_init);
  218. static void __exit st1232_ts_exit(void)
  219. {
  220. i2c_del_driver(&st1232_ts_driver);
  221. }
  222. module_exit(st1232_ts_exit);
  223. MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>");
  224. MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver");
  225. MODULE_LICENSE("GPL");