st1232.c 7.3 KB

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