st1232.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/gpio.h>
  22. #include <linux/i2c.h>
  23. #include <linux/input.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/pm_qos.h>
  29. #include <linux/slab.h>
  30. #include <linux/types.h>
  31. #include <linux/platform_data/st1232_pdata.h>
  32. #define ST1232_TS_NAME "st1232-ts"
  33. #define MIN_X 0x00
  34. #define MIN_Y 0x00
  35. #define MAX_X 0x31f /* (800 - 1) */
  36. #define MAX_Y 0x1df /* (480 - 1) */
  37. #define MAX_AREA 0xff
  38. #define MAX_FINGERS 2
  39. struct st1232_ts_finger {
  40. u16 x;
  41. u16 y;
  42. u8 t;
  43. bool is_valid;
  44. };
  45. struct st1232_ts_data {
  46. struct i2c_client *client;
  47. struct input_dev *input_dev;
  48. struct st1232_ts_finger finger[MAX_FINGERS];
  49. struct dev_pm_qos_request low_latency_req;
  50. int reset_gpio;
  51. };
  52. static int st1232_ts_read_data(struct st1232_ts_data *ts)
  53. {
  54. struct st1232_ts_finger *finger = ts->finger;
  55. struct i2c_client *client = ts->client;
  56. struct i2c_msg msg[2];
  57. int error;
  58. u8 start_reg;
  59. u8 buf[10];
  60. /* read touchscreen data from ST1232 */
  61. msg[0].addr = client->addr;
  62. msg[0].flags = 0;
  63. msg[0].len = 1;
  64. msg[0].buf = &start_reg;
  65. start_reg = 0x10;
  66. msg[1].addr = ts->client->addr;
  67. msg[1].flags = I2C_M_RD;
  68. msg[1].len = sizeof(buf);
  69. msg[1].buf = buf;
  70. error = i2c_transfer(client->adapter, msg, 2);
  71. if (error < 0)
  72. return error;
  73. /* get "valid" bits */
  74. finger[0].is_valid = buf[2] >> 7;
  75. finger[1].is_valid = buf[5] >> 7;
  76. /* get xy coordinate */
  77. if (finger[0].is_valid) {
  78. finger[0].x = ((buf[2] & 0x0070) << 4) | buf[3];
  79. finger[0].y = ((buf[2] & 0x0007) << 8) | buf[4];
  80. finger[0].t = buf[8];
  81. }
  82. if (finger[1].is_valid) {
  83. finger[1].x = ((buf[5] & 0x0070) << 4) | buf[6];
  84. finger[1].y = ((buf[5] & 0x0007) << 8) | buf[7];
  85. finger[1].t = buf[9];
  86. }
  87. return 0;
  88. }
  89. static irqreturn_t st1232_ts_irq_handler(int irq, void *dev_id)
  90. {
  91. struct st1232_ts_data *ts = dev_id;
  92. struct st1232_ts_finger *finger = ts->finger;
  93. struct input_dev *input_dev = ts->input_dev;
  94. int count = 0;
  95. int i, ret;
  96. ret = st1232_ts_read_data(ts);
  97. if (ret < 0)
  98. goto end;
  99. /* multi touch protocol */
  100. for (i = 0; i < MAX_FINGERS; i++) {
  101. if (!finger[i].is_valid)
  102. continue;
  103. input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, finger[i].t);
  104. input_report_abs(input_dev, ABS_MT_POSITION_X, finger[i].x);
  105. input_report_abs(input_dev, ABS_MT_POSITION_Y, finger[i].y);
  106. input_mt_sync(input_dev);
  107. count++;
  108. }
  109. /* SYN_MT_REPORT only if no contact */
  110. if (!count) {
  111. input_mt_sync(input_dev);
  112. if (ts->low_latency_req.dev) {
  113. dev_pm_qos_remove_request(&ts->low_latency_req);
  114. ts->low_latency_req.dev = NULL;
  115. }
  116. } else if (!ts->low_latency_req.dev) {
  117. /* First contact, request 100 us latency. */
  118. dev_pm_qos_add_ancestor_request(&ts->client->dev,
  119. &ts->low_latency_req, 100);
  120. }
  121. /* SYN_REPORT */
  122. input_sync(input_dev);
  123. end:
  124. return IRQ_HANDLED;
  125. }
  126. static void st1232_ts_power(struct st1232_ts_data *ts, bool poweron)
  127. {
  128. if (gpio_is_valid(ts->reset_gpio))
  129. gpio_direction_output(ts->reset_gpio, poweron);
  130. }
  131. static int st1232_ts_probe(struct i2c_client *client,
  132. const struct i2c_device_id *id)
  133. {
  134. struct st1232_ts_data *ts;
  135. struct st1232_pdata *pdata = client->dev.platform_data;
  136. struct input_dev *input_dev;
  137. int error;
  138. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  139. dev_err(&client->dev, "need I2C_FUNC_I2C\n");
  140. return -EIO;
  141. }
  142. if (!client->irq) {
  143. dev_err(&client->dev, "no IRQ?\n");
  144. return -EINVAL;
  145. }
  146. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  147. if (!ts)
  148. return -ENOMEM;
  149. input_dev = devm_input_allocate_device(&client->dev);
  150. if (!input_dev)
  151. return -ENOMEM;
  152. ts->client = client;
  153. ts->input_dev = input_dev;
  154. if (pdata)
  155. ts->reset_gpio = pdata->reset_gpio;
  156. else if (client->dev.of_node)
  157. ts->reset_gpio = of_get_gpio(client->dev.of_node, 0);
  158. else
  159. ts->reset_gpio = -ENODEV;
  160. if (gpio_is_valid(ts->reset_gpio)) {
  161. error = devm_gpio_request(&client->dev, ts->reset_gpio, NULL);
  162. if (error) {
  163. dev_err(&client->dev,
  164. "Unable to request GPIO pin %d.\n",
  165. ts->reset_gpio);
  166. return error;
  167. }
  168. }
  169. st1232_ts_power(ts, true);
  170. input_dev->name = "st1232-touchscreen";
  171. input_dev->id.bustype = BUS_I2C;
  172. input_dev->dev.parent = &client->dev;
  173. __set_bit(EV_SYN, input_dev->evbit);
  174. __set_bit(EV_KEY, input_dev->evbit);
  175. __set_bit(EV_ABS, input_dev->evbit);
  176. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, MAX_AREA, 0, 0);
  177. input_set_abs_params(input_dev, ABS_MT_POSITION_X, MIN_X, MAX_X, 0, 0);
  178. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, MIN_Y, MAX_Y, 0, 0);
  179. error = devm_request_threaded_irq(&client->dev, client->irq,
  180. NULL, st1232_ts_irq_handler,
  181. IRQF_ONESHOT,
  182. client->name, ts);
  183. if (error) {
  184. dev_err(&client->dev, "Failed to register interrupt\n");
  185. return error;
  186. }
  187. error = input_register_device(ts->input_dev);
  188. if (error) {
  189. dev_err(&client->dev, "Unable to register %s input device\n",
  190. input_dev->name);
  191. return error;
  192. }
  193. i2c_set_clientdata(client, ts);
  194. device_init_wakeup(&client->dev, 1);
  195. return 0;
  196. }
  197. static int st1232_ts_remove(struct i2c_client *client)
  198. {
  199. struct st1232_ts_data *ts = i2c_get_clientdata(client);
  200. device_init_wakeup(&client->dev, 0);
  201. st1232_ts_power(ts, false);
  202. return 0;
  203. }
  204. #ifdef CONFIG_PM_SLEEP
  205. static int st1232_ts_suspend(struct device *dev)
  206. {
  207. struct i2c_client *client = to_i2c_client(dev);
  208. struct st1232_ts_data *ts = i2c_get_clientdata(client);
  209. if (device_may_wakeup(&client->dev)) {
  210. enable_irq_wake(client->irq);
  211. } else {
  212. disable_irq(client->irq);
  213. st1232_ts_power(ts, false);
  214. }
  215. return 0;
  216. }
  217. static int st1232_ts_resume(struct device *dev)
  218. {
  219. struct i2c_client *client = to_i2c_client(dev);
  220. struct st1232_ts_data *ts = i2c_get_clientdata(client);
  221. if (device_may_wakeup(&client->dev)) {
  222. disable_irq_wake(client->irq);
  223. } else {
  224. st1232_ts_power(ts, true);
  225. enable_irq(client->irq);
  226. }
  227. return 0;
  228. }
  229. #endif
  230. static SIMPLE_DEV_PM_OPS(st1232_ts_pm_ops,
  231. st1232_ts_suspend, st1232_ts_resume);
  232. static const struct i2c_device_id st1232_ts_id[] = {
  233. { ST1232_TS_NAME, 0 },
  234. { }
  235. };
  236. MODULE_DEVICE_TABLE(i2c, st1232_ts_id);
  237. #ifdef CONFIG_OF
  238. static const struct of_device_id st1232_ts_dt_ids[] = {
  239. { .compatible = "sitronix,st1232", },
  240. { }
  241. };
  242. MODULE_DEVICE_TABLE(of, st1232_ts_dt_ids);
  243. #endif
  244. static struct i2c_driver st1232_ts_driver = {
  245. .probe = st1232_ts_probe,
  246. .remove = st1232_ts_remove,
  247. .id_table = st1232_ts_id,
  248. .driver = {
  249. .name = ST1232_TS_NAME,
  250. .owner = THIS_MODULE,
  251. .of_match_table = of_match_ptr(st1232_ts_dt_ids),
  252. .pm = &st1232_ts_pm_ops,
  253. },
  254. };
  255. module_i2c_driver(st1232_ts_driver);
  256. MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>");
  257. MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver");
  258. MODULE_LICENSE("GPL");