st1232.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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,
  120. DEV_PM_QOS_RESUME_LATENCY, 100);
  121. }
  122. /* SYN_REPORT */
  123. input_sync(input_dev);
  124. end:
  125. return IRQ_HANDLED;
  126. }
  127. static void st1232_ts_power(struct st1232_ts_data *ts, bool poweron)
  128. {
  129. if (gpio_is_valid(ts->reset_gpio))
  130. gpio_direction_output(ts->reset_gpio, poweron);
  131. }
  132. static int st1232_ts_probe(struct i2c_client *client,
  133. const struct i2c_device_id *id)
  134. {
  135. struct st1232_ts_data *ts;
  136. struct st1232_pdata *pdata = dev_get_platdata(&client->dev);
  137. struct input_dev *input_dev;
  138. int error;
  139. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  140. dev_err(&client->dev, "need I2C_FUNC_I2C\n");
  141. return -EIO;
  142. }
  143. if (!client->irq) {
  144. dev_err(&client->dev, "no IRQ?\n");
  145. return -EINVAL;
  146. }
  147. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  148. if (!ts)
  149. return -ENOMEM;
  150. input_dev = devm_input_allocate_device(&client->dev);
  151. if (!input_dev)
  152. return -ENOMEM;
  153. ts->client = client;
  154. ts->input_dev = input_dev;
  155. if (pdata)
  156. ts->reset_gpio = pdata->reset_gpio;
  157. else if (client->dev.of_node)
  158. ts->reset_gpio = of_get_gpio(client->dev.of_node, 0);
  159. else
  160. ts->reset_gpio = -ENODEV;
  161. if (gpio_is_valid(ts->reset_gpio)) {
  162. error = devm_gpio_request(&client->dev, ts->reset_gpio, NULL);
  163. if (error) {
  164. dev_err(&client->dev,
  165. "Unable to request GPIO pin %d.\n",
  166. ts->reset_gpio);
  167. return error;
  168. }
  169. }
  170. st1232_ts_power(ts, true);
  171. input_dev->name = "st1232-touchscreen";
  172. input_dev->id.bustype = BUS_I2C;
  173. input_dev->dev.parent = &client->dev;
  174. __set_bit(EV_SYN, input_dev->evbit);
  175. __set_bit(EV_KEY, input_dev->evbit);
  176. __set_bit(EV_ABS, input_dev->evbit);
  177. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, MAX_AREA, 0, 0);
  178. input_set_abs_params(input_dev, ABS_MT_POSITION_X, MIN_X, MAX_X, 0, 0);
  179. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, MIN_Y, MAX_Y, 0, 0);
  180. error = devm_request_threaded_irq(&client->dev, client->irq,
  181. NULL, st1232_ts_irq_handler,
  182. IRQF_ONESHOT,
  183. client->name, ts);
  184. if (error) {
  185. dev_err(&client->dev, "Failed to register interrupt\n");
  186. return error;
  187. }
  188. error = input_register_device(ts->input_dev);
  189. if (error) {
  190. dev_err(&client->dev, "Unable to register %s input device\n",
  191. input_dev->name);
  192. return error;
  193. }
  194. i2c_set_clientdata(client, ts);
  195. device_init_wakeup(&client->dev, 1);
  196. return 0;
  197. }
  198. static int st1232_ts_remove(struct i2c_client *client)
  199. {
  200. struct st1232_ts_data *ts = i2c_get_clientdata(client);
  201. device_init_wakeup(&client->dev, 0);
  202. st1232_ts_power(ts, false);
  203. return 0;
  204. }
  205. static int __maybe_unused 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 __maybe_unused 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. 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. .of_match_table = of_match_ptr(st1232_ts_dt_ids),
  250. .pm = &st1232_ts_pm_ops,
  251. },
  252. };
  253. module_i2c_driver(st1232_ts_driver);
  254. MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>");
  255. MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver");
  256. MODULE_LICENSE("GPL");