/drivers/input/touchscreen/st1232.c

http://github.com/mirrors/linux · C · 340 lines · 266 code · 60 blank · 14 comment · 29 complexity · e3f4f89029acdb5922395d7303de2622 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ST1232 Touchscreen Controller Driver
  4. *
  5. * Copyright (C) 2010 Renesas Solutions Corp.
  6. * Tony SIM <chinyeow.sim.xt@renesas.com>
  7. *
  8. * Using code from:
  9. * - android.git.kernel.org: projects/kernel/common.git: synaptics_i2c_rmi.c
  10. * Copyright (C) 2007 Google, Inc.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/i2c.h>
  15. #include <linux/input.h>
  16. #include <linux/input/mt.h>
  17. #include <linux/input/touchscreen.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/pm_qos.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #define ST1232_TS_NAME "st1232-ts"
  25. #define ST1633_TS_NAME "st1633-ts"
  26. #define ST_TS_MAX_FINGERS 10
  27. struct st_chip_info {
  28. bool have_z;
  29. u16 max_x;
  30. u16 max_y;
  31. u16 max_area;
  32. u16 max_fingers;
  33. u8 start_reg;
  34. };
  35. struct st1232_ts_data {
  36. struct i2c_client *client;
  37. struct input_dev *input_dev;
  38. struct touchscreen_properties prop;
  39. struct dev_pm_qos_request low_latency_req;
  40. struct gpio_desc *reset_gpio;
  41. const struct st_chip_info *chip_info;
  42. int read_buf_len;
  43. u8 *read_buf;
  44. };
  45. static int st1232_ts_read_data(struct st1232_ts_data *ts)
  46. {
  47. struct i2c_client *client = ts->client;
  48. u8 start_reg = ts->chip_info->start_reg;
  49. struct i2c_msg msg[] = {
  50. {
  51. .addr = client->addr,
  52. .len = sizeof(start_reg),
  53. .buf = &start_reg,
  54. },
  55. {
  56. .addr = client->addr,
  57. .flags = I2C_M_RD | I2C_M_DMA_SAFE,
  58. .len = ts->read_buf_len,
  59. .buf = ts->read_buf,
  60. }
  61. };
  62. int ret;
  63. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  64. if (ret != ARRAY_SIZE(msg))
  65. return ret < 0 ? ret : -EIO;
  66. return 0;
  67. }
  68. static int st1232_ts_parse_and_report(struct st1232_ts_data *ts)
  69. {
  70. struct input_dev *input = ts->input_dev;
  71. struct input_mt_pos pos[ST_TS_MAX_FINGERS];
  72. u8 z[ST_TS_MAX_FINGERS];
  73. int slots[ST_TS_MAX_FINGERS];
  74. int n_contacts = 0;
  75. int i;
  76. for (i = 0; i < ts->chip_info->max_fingers; i++) {
  77. u8 *buf = &ts->read_buf[i * 4];
  78. if (buf[0] & BIT(7)) {
  79. unsigned int x = ((buf[0] & 0x70) << 4) | buf[1];
  80. unsigned int y = ((buf[0] & 0x07) << 8) | buf[2];
  81. touchscreen_set_mt_pos(&pos[n_contacts],
  82. &ts->prop, x, y);
  83. /* st1232 includes a z-axis / touch strength */
  84. if (ts->chip_info->have_z)
  85. z[n_contacts] = ts->read_buf[i + 6];
  86. n_contacts++;
  87. }
  88. }
  89. input_mt_assign_slots(input, slots, pos, n_contacts, 0);
  90. for (i = 0; i < n_contacts; i++) {
  91. input_mt_slot(input, slots[i]);
  92. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  93. input_report_abs(input, ABS_MT_POSITION_X, pos[i].x);
  94. input_report_abs(input, ABS_MT_POSITION_Y, pos[i].y);
  95. if (ts->chip_info->have_z)
  96. input_report_abs(input, ABS_MT_TOUCH_MAJOR, z[i]);
  97. }
  98. input_mt_sync_frame(input);
  99. input_sync(input);
  100. return n_contacts;
  101. }
  102. static irqreturn_t st1232_ts_irq_handler(int irq, void *dev_id)
  103. {
  104. struct st1232_ts_data *ts = dev_id;
  105. int count;
  106. int error;
  107. error = st1232_ts_read_data(ts);
  108. if (error)
  109. goto out;
  110. count = st1232_ts_parse_and_report(ts);
  111. if (!count) {
  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. out:
  123. return IRQ_HANDLED;
  124. }
  125. static void st1232_ts_power(struct st1232_ts_data *ts, bool poweron)
  126. {
  127. if (ts->reset_gpio)
  128. gpiod_set_value_cansleep(ts->reset_gpio, !poweron);
  129. }
  130. static void st1232_ts_power_off(void *data)
  131. {
  132. st1232_ts_power(data, false);
  133. }
  134. static const struct st_chip_info st1232_chip_info = {
  135. .have_z = true,
  136. .max_x = 0x31f, /* 800 - 1 */
  137. .max_y = 0x1df, /* 480 -1 */
  138. .max_area = 0xff,
  139. .max_fingers = 2,
  140. .start_reg = 0x12,
  141. };
  142. static const struct st_chip_info st1633_chip_info = {
  143. .have_z = false,
  144. .max_x = 0x13f, /* 320 - 1 */
  145. .max_y = 0x1df, /* 480 -1 */
  146. .max_area = 0x00,
  147. .max_fingers = 5,
  148. .start_reg = 0x12,
  149. };
  150. static int st1232_ts_probe(struct i2c_client *client,
  151. const struct i2c_device_id *id)
  152. {
  153. const struct st_chip_info *match;
  154. struct st1232_ts_data *ts;
  155. struct input_dev *input_dev;
  156. int error;
  157. match = device_get_match_data(&client->dev);
  158. if (!match && id)
  159. match = (const void *)id->driver_data;
  160. if (!match) {
  161. dev_err(&client->dev, "unknown device model\n");
  162. return -ENODEV;
  163. }
  164. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  165. dev_err(&client->dev, "need I2C_FUNC_I2C\n");
  166. return -EIO;
  167. }
  168. if (!client->irq) {
  169. dev_err(&client->dev, "no IRQ?\n");
  170. return -EINVAL;
  171. }
  172. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  173. if (!ts)
  174. return -ENOMEM;
  175. ts->chip_info = match;
  176. /* allocate a buffer according to the number of registers to read */
  177. ts->read_buf_len = ts->chip_info->max_fingers * 4;
  178. ts->read_buf = devm_kzalloc(&client->dev, ts->read_buf_len, GFP_KERNEL);
  179. if (!ts->read_buf)
  180. return -ENOMEM;
  181. input_dev = devm_input_allocate_device(&client->dev);
  182. if (!input_dev)
  183. return -ENOMEM;
  184. ts->client = client;
  185. ts->input_dev = input_dev;
  186. ts->reset_gpio = devm_gpiod_get_optional(&client->dev, NULL,
  187. GPIOD_OUT_HIGH);
  188. if (IS_ERR(ts->reset_gpio)) {
  189. error = PTR_ERR(ts->reset_gpio);
  190. dev_err(&client->dev, "Unable to request GPIO pin: %d.\n",
  191. error);
  192. return error;
  193. }
  194. st1232_ts_power(ts, true);
  195. error = devm_add_action_or_reset(&client->dev, st1232_ts_power_off, ts);
  196. if (error) {
  197. dev_err(&client->dev,
  198. "Failed to install power off action: %d\n", error);
  199. return error;
  200. }
  201. input_dev->name = "st1232-touchscreen";
  202. input_dev->id.bustype = BUS_I2C;
  203. if (ts->chip_info->have_z)
  204. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
  205. ts->chip_info->max_area, 0, 0);
  206. input_set_abs_params(input_dev, ABS_MT_POSITION_X,
  207. 0, ts->chip_info->max_x, 0, 0);
  208. input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
  209. 0, ts->chip_info->max_y, 0, 0);
  210. touchscreen_parse_properties(input_dev, true, &ts->prop);
  211. error = input_mt_init_slots(input_dev, ts->chip_info->max_fingers,
  212. INPUT_MT_DIRECT | INPUT_MT_TRACK |
  213. INPUT_MT_DROP_UNUSED);
  214. if (error) {
  215. dev_err(&client->dev, "failed to initialize MT slots\n");
  216. return error;
  217. }
  218. error = devm_request_threaded_irq(&client->dev, client->irq,
  219. NULL, st1232_ts_irq_handler,
  220. IRQF_ONESHOT,
  221. client->name, ts);
  222. if (error) {
  223. dev_err(&client->dev, "Failed to register interrupt\n");
  224. return error;
  225. }
  226. error = input_register_device(ts->input_dev);
  227. if (error) {
  228. dev_err(&client->dev, "Unable to register %s input device\n",
  229. input_dev->name);
  230. return error;
  231. }
  232. i2c_set_clientdata(client, ts);
  233. return 0;
  234. }
  235. static int __maybe_unused st1232_ts_suspend(struct device *dev)
  236. {
  237. struct i2c_client *client = to_i2c_client(dev);
  238. struct st1232_ts_data *ts = i2c_get_clientdata(client);
  239. disable_irq(client->irq);
  240. if (!device_may_wakeup(&client->dev))
  241. st1232_ts_power(ts, false);
  242. return 0;
  243. }
  244. static int __maybe_unused st1232_ts_resume(struct device *dev)
  245. {
  246. struct i2c_client *client = to_i2c_client(dev);
  247. struct st1232_ts_data *ts = i2c_get_clientdata(client);
  248. if (!device_may_wakeup(&client->dev))
  249. st1232_ts_power(ts, true);
  250. enable_irq(client->irq);
  251. return 0;
  252. }
  253. static SIMPLE_DEV_PM_OPS(st1232_ts_pm_ops,
  254. st1232_ts_suspend, st1232_ts_resume);
  255. static const struct i2c_device_id st1232_ts_id[] = {
  256. { ST1232_TS_NAME, (unsigned long)&st1232_chip_info },
  257. { ST1633_TS_NAME, (unsigned long)&st1633_chip_info },
  258. { }
  259. };
  260. MODULE_DEVICE_TABLE(i2c, st1232_ts_id);
  261. static const struct of_device_id st1232_ts_dt_ids[] = {
  262. { .compatible = "sitronix,st1232", .data = &st1232_chip_info },
  263. { .compatible = "sitronix,st1633", .data = &st1633_chip_info },
  264. { }
  265. };
  266. MODULE_DEVICE_TABLE(of, st1232_ts_dt_ids);
  267. static struct i2c_driver st1232_ts_driver = {
  268. .probe = st1232_ts_probe,
  269. .id_table = st1232_ts_id,
  270. .driver = {
  271. .name = ST1232_TS_NAME,
  272. .of_match_table = st1232_ts_dt_ids,
  273. .pm = &st1232_ts_pm_ops,
  274. },
  275. };
  276. module_i2c_driver(st1232_ts_driver);
  277. MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>");
  278. MODULE_AUTHOR("Martin Kepplinger <martin.kepplinger@ginzinger.com>");
  279. MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver");
  280. MODULE_LICENSE("GPL v2");