zet6223.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (C) 2016, Jelle van der Waa <jelle@vdwaa.nl>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/i2c.h>
  16. #include <linux/input.h>
  17. #include <linux/input/mt.h>
  18. #include <linux/input/touchscreen.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <asm/unaligned.h>
  23. #define ZET6223_MAX_FINGERS 16
  24. #define ZET6223_MAX_PKT_SIZE (3 + 4 * ZET6223_MAX_FINGERS)
  25. #define ZET6223_CMD_INFO 0xB2
  26. #define ZET6223_CMD_INFO_LENGTH 17
  27. #define ZET6223_VALID_PACKET 0x3c
  28. #define ZET6223_POWER_ON_DELAY_MSEC 30
  29. struct zet6223_ts {
  30. struct i2c_client *client;
  31. struct input_dev *input;
  32. struct regulator *vcc;
  33. struct regulator *vio;
  34. struct touchscreen_properties prop;
  35. struct regulator_bulk_data supplies[2];
  36. u16 max_x;
  37. u16 max_y;
  38. u8 fingernum;
  39. };
  40. static int zet6223_start(struct input_dev *dev)
  41. {
  42. struct zet6223_ts *ts = input_get_drvdata(dev);
  43. enable_irq(ts->client->irq);
  44. return 0;
  45. }
  46. static void zet6223_stop(struct input_dev *dev)
  47. {
  48. struct zet6223_ts *ts = input_get_drvdata(dev);
  49. disable_irq(ts->client->irq);
  50. }
  51. static irqreturn_t zet6223_irq(int irq, void *dev_id)
  52. {
  53. struct zet6223_ts *ts = dev_id;
  54. u16 finger_bits;
  55. /*
  56. * First 3 bytes are an identifier, two bytes of finger data.
  57. * X, Y data per finger is 4 bytes.
  58. */
  59. u8 bufsize = 3 + 4 * ts->fingernum;
  60. u8 buf[ZET6223_MAX_PKT_SIZE];
  61. int i;
  62. int ret;
  63. int error;
  64. ret = i2c_master_recv(ts->client, buf, bufsize);
  65. if (ret != bufsize) {
  66. error = ret < 0 ? ret : -EIO;
  67. dev_err_ratelimited(&ts->client->dev,
  68. "Error reading input data: %d\n", error);
  69. return IRQ_HANDLED;
  70. }
  71. if (buf[0] != ZET6223_VALID_PACKET)
  72. return IRQ_HANDLED;
  73. finger_bits = get_unaligned_be16(buf + 1);
  74. for (i = 0; i < ts->fingernum; i++) {
  75. if (!(finger_bits & BIT(15 - i)))
  76. continue;
  77. input_mt_slot(ts->input, i);
  78. input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
  79. input_event(ts->input, EV_ABS, ABS_MT_POSITION_X,
  80. ((buf[i + 3] >> 4) << 8) + buf[i + 4]);
  81. input_event(ts->input, EV_ABS, ABS_MT_POSITION_Y,
  82. ((buf[i + 3] & 0xF) << 8) + buf[i + 5]);
  83. }
  84. input_mt_sync_frame(ts->input);
  85. input_sync(ts->input);
  86. return IRQ_HANDLED;
  87. }
  88. static void zet6223_power_off(void *_ts)
  89. {
  90. struct zet6223_ts *ts = _ts;
  91. regulator_bulk_disable(ARRAY_SIZE(ts->supplies), ts->supplies);
  92. }
  93. static int zet6223_power_on(struct zet6223_ts *ts)
  94. {
  95. struct device *dev = &ts->client->dev;
  96. int error;
  97. ts->supplies[0].supply = "vio";
  98. ts->supplies[1].supply = "vcc";
  99. error = devm_regulator_bulk_get(dev, ARRAY_SIZE(ts->supplies),
  100. ts->supplies);
  101. if (error)
  102. return error;
  103. error = regulator_bulk_enable(ARRAY_SIZE(ts->supplies), ts->supplies);
  104. if (error)
  105. return error;
  106. msleep(ZET6223_POWER_ON_DELAY_MSEC);
  107. error = devm_add_action_or_reset(dev, zet6223_power_off, ts);
  108. if (error) {
  109. dev_err(dev, "failed to install poweroff action: %d\n", error);
  110. return error;
  111. }
  112. return 0;
  113. }
  114. static int zet6223_query_device(struct zet6223_ts *ts)
  115. {
  116. u8 buf[ZET6223_CMD_INFO_LENGTH];
  117. u8 cmd = ZET6223_CMD_INFO;
  118. int ret;
  119. int error;
  120. ret = i2c_master_send(ts->client, &cmd, sizeof(cmd));
  121. if (ret != sizeof(cmd)) {
  122. error = ret < 0 ? ret : -EIO;
  123. dev_err(&ts->client->dev,
  124. "touchpanel info cmd failed: %d\n", error);
  125. return error;
  126. }
  127. ret = i2c_master_recv(ts->client, buf, sizeof(buf));
  128. if (ret != sizeof(buf)) {
  129. error = ret < 0 ? ret : -EIO;
  130. dev_err(&ts->client->dev,
  131. "failed to retrieve touchpanel info: %d\n", error);
  132. return error;
  133. }
  134. ts->fingernum = buf[15] & 0x7F;
  135. if (ts->fingernum > ZET6223_MAX_FINGERS) {
  136. dev_warn(&ts->client->dev,
  137. "touchpanel reports %d fingers, limiting to %d\n",
  138. ts->fingernum, ZET6223_MAX_FINGERS);
  139. ts->fingernum = ZET6223_MAX_FINGERS;
  140. }
  141. ts->max_x = get_unaligned_le16(&buf[8]);
  142. ts->max_y = get_unaligned_le16(&buf[10]);
  143. return 0;
  144. }
  145. static int zet6223_probe(struct i2c_client *client,
  146. const struct i2c_device_id *id)
  147. {
  148. struct device *dev = &client->dev;
  149. struct zet6223_ts *ts;
  150. struct input_dev *input;
  151. int error;
  152. if (!client->irq) {
  153. dev_err(dev, "no irq specified\n");
  154. return -EINVAL;
  155. }
  156. ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
  157. if (!ts)
  158. return -ENOMEM;
  159. ts->client = client;
  160. error = zet6223_power_on(ts);
  161. if (error)
  162. return error;
  163. error = zet6223_query_device(ts);
  164. if (error)
  165. return error;
  166. ts->input = input = devm_input_allocate_device(dev);
  167. if (!input)
  168. return -ENOMEM;
  169. input_set_drvdata(input, ts);
  170. input->name = client->name;
  171. input->id.bustype = BUS_I2C;
  172. input->open = zet6223_start;
  173. input->close = zet6223_stop;
  174. input_set_abs_params(input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
  175. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
  176. touchscreen_parse_properties(input, true, &ts->prop);
  177. error = input_mt_init_slots(input, ts->fingernum,
  178. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  179. if (error)
  180. return error;
  181. error = devm_request_threaded_irq(dev, client->irq, NULL, zet6223_irq,
  182. IRQF_ONESHOT, client->name, ts);
  183. if (error) {
  184. dev_err(dev, "failed to request irq %d: %d\n",
  185. client->irq, error);
  186. return error;
  187. }
  188. zet6223_stop(input);
  189. error = input_register_device(input);
  190. if (error)
  191. return error;
  192. return 0;
  193. }
  194. static const struct of_device_id zet6223_of_match[] = {
  195. { .compatible = "zeitec,zet6223" },
  196. { }
  197. };
  198. MODULE_DEVICE_TABLE(of, zet6223_of_match);
  199. static const struct i2c_device_id zet6223_id[] = {
  200. { "zet6223", 0},
  201. { }
  202. };
  203. MODULE_DEVICE_TABLE(i2c, zet6223_id);
  204. static struct i2c_driver zet6223_driver = {
  205. .driver = {
  206. .name = "zet6223",
  207. .of_match_table = zet6223_of_match,
  208. },
  209. .probe = zet6223_probe,
  210. .id_table = zet6223_id
  211. };
  212. module_i2c_driver(zet6223_driver);
  213. MODULE_AUTHOR("Jelle van der Waa <jelle@vdwaa.nl>");
  214. MODULE_DESCRIPTION("ZEITEC zet622x I2C touchscreen driver");
  215. MODULE_LICENSE("GPL");