st1232.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #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,
  119. DEV_PM_QOS_RESUME_LATENCY, 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 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. ts->reset_gpio = of_get_gpio(client->dev.of_node, 0);
  154. if (gpio_is_valid(ts->reset_gpio)) {
  155. error = devm_gpio_request(&client->dev, ts->reset_gpio, NULL);
  156. if (error) {
  157. dev_err(&client->dev,
  158. "Unable to request GPIO pin %d.\n",
  159. ts->reset_gpio);
  160. return error;
  161. }
  162. }
  163. st1232_ts_power(ts, true);
  164. input_dev->name = "st1232-touchscreen";
  165. input_dev->id.bustype = BUS_I2C;
  166. input_dev->dev.parent = &client->dev;
  167. __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
  168. __set_bit(EV_SYN, input_dev->evbit);
  169. __set_bit(EV_KEY, input_dev->evbit);
  170. __set_bit(EV_ABS, input_dev->evbit);
  171. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, MAX_AREA, 0, 0);
  172. input_set_abs_params(input_dev, ABS_MT_POSITION_X, MIN_X, MAX_X, 0, 0);
  173. input_set_abs_params(input_dev, ABS_MT_POSITION_Y, MIN_Y, MAX_Y, 0, 0);
  174. error = devm_request_threaded_irq(&client->dev, client->irq,
  175. NULL, st1232_ts_irq_handler,
  176. IRQF_ONESHOT,
  177. client->name, ts);
  178. if (error) {
  179. dev_err(&client->dev, "Failed to register interrupt\n");
  180. return error;
  181. }
  182. error = input_register_device(ts->input_dev);
  183. if (error) {
  184. dev_err(&client->dev, "Unable to register %s input device\n",
  185. input_dev->name);
  186. return error;
  187. }
  188. i2c_set_clientdata(client, ts);
  189. device_init_wakeup(&client->dev, 1);
  190. return 0;
  191. }
  192. static int st1232_ts_remove(struct i2c_client *client)
  193. {
  194. struct st1232_ts_data *ts = i2c_get_clientdata(client);
  195. st1232_ts_power(ts, false);
  196. return 0;
  197. }
  198. static int __maybe_unused st1232_ts_suspend(struct device *dev)
  199. {
  200. struct i2c_client *client = to_i2c_client(dev);
  201. struct st1232_ts_data *ts = i2c_get_clientdata(client);
  202. if (device_may_wakeup(&client->dev)) {
  203. enable_irq_wake(client->irq);
  204. } else {
  205. disable_irq(client->irq);
  206. st1232_ts_power(ts, false);
  207. }
  208. return 0;
  209. }
  210. static int __maybe_unused st1232_ts_resume(struct device *dev)
  211. {
  212. struct i2c_client *client = to_i2c_client(dev);
  213. struct st1232_ts_data *ts = i2c_get_clientdata(client);
  214. if (device_may_wakeup(&client->dev)) {
  215. disable_irq_wake(client->irq);
  216. } else {
  217. st1232_ts_power(ts, true);
  218. enable_irq(client->irq);
  219. }
  220. return 0;
  221. }
  222. static SIMPLE_DEV_PM_OPS(st1232_ts_pm_ops,
  223. st1232_ts_suspend, st1232_ts_resume);
  224. static const struct i2c_device_id st1232_ts_id[] = {
  225. { ST1232_TS_NAME, 0 },
  226. { }
  227. };
  228. MODULE_DEVICE_TABLE(i2c, st1232_ts_id);
  229. static const struct of_device_id st1232_ts_dt_ids[] = {
  230. { .compatible = "sitronix,st1232", },
  231. { }
  232. };
  233. MODULE_DEVICE_TABLE(of, st1232_ts_dt_ids);
  234. static struct i2c_driver st1232_ts_driver = {
  235. .probe = st1232_ts_probe,
  236. .remove = st1232_ts_remove,
  237. .id_table = st1232_ts_id,
  238. .driver = {
  239. .name = ST1232_TS_NAME,
  240. .of_match_table = st1232_ts_dt_ids,
  241. .pm = &st1232_ts_pm_ops,
  242. },
  243. };
  244. module_i2c_driver(st1232_ts_driver);
  245. MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>");
  246. MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver");
  247. MODULE_LICENSE("GPL");