sis_i2c.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Touch Screen driver for SiS 9200 family I2C Touch panels
  4. *
  5. * Copyright (C) 2015 SiS, Inc.
  6. * Copyright (C) 2016 Nextfour Group
  7. */
  8. #include <linux/crc-itu-t.h>
  9. #include <linux/delay.h>
  10. #include <linux/i2c.h>
  11. #include <linux/input.h>
  12. #include <linux/input/mt.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <asm/unaligned.h>
  18. #define SIS_I2C_NAME "sis_i2c_ts"
  19. /*
  20. * The I2C packet format:
  21. * le16 byte count
  22. * u8 Report ID
  23. * <contact data - variable length>
  24. * u8 Number of contacts
  25. * le16 Scan Time (optional)
  26. * le16 CRC
  27. *
  28. * One touch point information consists of 6+ bytes, the order is:
  29. * u8 contact state
  30. * u8 finger id
  31. * le16 x axis
  32. * le16 y axis
  33. * u8 contact width (optional)
  34. * u8 contact height (optional)
  35. * u8 pressure (optional)
  36. *
  37. * Maximum amount of data transmitted in one shot is 64 bytes, if controller
  38. * needs to report more contacts than fit in one packet it will send true
  39. * number of contacts in first packet and 0 as number of contacts in second
  40. * packet.
  41. */
  42. #define SIS_MAX_PACKET_SIZE 64
  43. #define SIS_PKT_LEN_OFFSET 0
  44. #define SIS_PKT_REPORT_OFFSET 2 /* Report ID/type */
  45. #define SIS_PKT_CONTACT_OFFSET 3 /* First contact */
  46. #define SIS_SCAN_TIME_LEN 2
  47. /* Supported report types */
  48. #define SIS_ALL_IN_ONE_PACKAGE 0x10
  49. #define SIS_PKT_IS_TOUCH(x) (((x) & 0x0f) == 0x01)
  50. #define SIS_PKT_IS_HIDI2C(x) (((x) & 0x0f) == 0x06)
  51. /* Contact properties within report */
  52. #define SIS_PKT_HAS_AREA(x) ((x) & BIT(4))
  53. #define SIS_PKT_HAS_PRESSURE(x) ((x) & BIT(5))
  54. #define SIS_PKT_HAS_SCANTIME(x) ((x) & BIT(6))
  55. /* Contact size */
  56. #define SIS_BASE_LEN_PER_CONTACT 6
  57. #define SIS_AREA_LEN_PER_CONTACT 2
  58. #define SIS_PRESSURE_LEN_PER_CONTACT 1
  59. /* Offsets within contact data */
  60. #define SIS_CONTACT_STATUS_OFFSET 0
  61. #define SIS_CONTACT_ID_OFFSET 1 /* Contact ID */
  62. #define SIS_CONTACT_X_OFFSET 2
  63. #define SIS_CONTACT_Y_OFFSET 4
  64. #define SIS_CONTACT_WIDTH_OFFSET 6
  65. #define SIS_CONTACT_HEIGHT_OFFSET 7
  66. #define SIS_CONTACT_PRESSURE_OFFSET(id) (SIS_PKT_HAS_AREA(id) ? 8 : 6)
  67. /* Individual contact state */
  68. #define SIS_STATUS_UP 0x0
  69. #define SIS_STATUS_DOWN 0x3
  70. /* Touchscreen parameters */
  71. #define SIS_MAX_FINGERS 10
  72. #define SIS_MAX_X 4095
  73. #define SIS_MAX_Y 4095
  74. #define SIS_MAX_PRESSURE 255
  75. /* Resolution diagonal */
  76. #define SIS_AREA_LENGTH_LONGER 5792
  77. /*((SIS_MAX_X^2) + (SIS_MAX_Y^2))^0.5*/
  78. #define SIS_AREA_LENGTH_SHORT 5792
  79. #define SIS_AREA_UNIT (5792 / 32)
  80. struct sis_ts_data {
  81. struct i2c_client *client;
  82. struct input_dev *input;
  83. struct gpio_desc *attn_gpio;
  84. struct gpio_desc *reset_gpio;
  85. u8 packet[SIS_MAX_PACKET_SIZE];
  86. };
  87. static int sis_read_packet(struct i2c_client *client, u8 *buf,
  88. unsigned int *num_contacts,
  89. unsigned int *contact_size)
  90. {
  91. int count_idx;
  92. int ret;
  93. u16 len;
  94. u16 crc, pkg_crc;
  95. u8 report_id;
  96. ret = i2c_master_recv(client, buf, SIS_MAX_PACKET_SIZE);
  97. if (ret <= 0)
  98. return -EIO;
  99. len = get_unaligned_le16(&buf[SIS_PKT_LEN_OFFSET]);
  100. if (len > SIS_MAX_PACKET_SIZE) {
  101. dev_err(&client->dev,
  102. "%s: invalid packet length (%d vs %d)\n",
  103. __func__, len, SIS_MAX_PACKET_SIZE);
  104. return -E2BIG;
  105. }
  106. if (len < 10)
  107. return -EINVAL;
  108. report_id = buf[SIS_PKT_REPORT_OFFSET];
  109. count_idx = len - 1;
  110. *contact_size = SIS_BASE_LEN_PER_CONTACT;
  111. if (report_id != SIS_ALL_IN_ONE_PACKAGE) {
  112. if (SIS_PKT_IS_TOUCH(report_id)) {
  113. /*
  114. * Calculate CRC ignoring packet length
  115. * in the beginning and CRC transmitted
  116. * at the end of the packet.
  117. */
  118. crc = crc_itu_t(0, buf + 2, len - 2 - 2);
  119. pkg_crc = get_unaligned_le16(&buf[len - 2]);
  120. if (crc != pkg_crc) {
  121. dev_err(&client->dev,
  122. "%s: CRC Error (%d vs %d)\n",
  123. __func__, crc, pkg_crc);
  124. return -EINVAL;
  125. }
  126. count_idx -= 2;
  127. } else if (!SIS_PKT_IS_HIDI2C(report_id)) {
  128. dev_err(&client->dev,
  129. "%s: invalid packet ID %#02x\n",
  130. __func__, report_id);
  131. return -EINVAL;
  132. }
  133. if (SIS_PKT_HAS_SCANTIME(report_id))
  134. count_idx -= SIS_SCAN_TIME_LEN;
  135. if (SIS_PKT_HAS_AREA(report_id))
  136. *contact_size += SIS_AREA_LEN_PER_CONTACT;
  137. if (SIS_PKT_HAS_PRESSURE(report_id))
  138. *contact_size += SIS_PRESSURE_LEN_PER_CONTACT;
  139. }
  140. *num_contacts = buf[count_idx];
  141. return 0;
  142. }
  143. static int sis_ts_report_contact(struct sis_ts_data *ts, const u8 *data, u8 id)
  144. {
  145. struct input_dev *input = ts->input;
  146. int slot;
  147. u8 status = data[SIS_CONTACT_STATUS_OFFSET];
  148. u8 pressure;
  149. u8 height, width;
  150. u16 x, y;
  151. if (status != SIS_STATUS_DOWN && status != SIS_STATUS_UP) {
  152. dev_err(&ts->client->dev, "Unexpected touch status: %#02x\n",
  153. data[SIS_CONTACT_STATUS_OFFSET]);
  154. return -EINVAL;
  155. }
  156. slot = input_mt_get_slot_by_key(input, data[SIS_CONTACT_ID_OFFSET]);
  157. if (slot < 0)
  158. return -ENOENT;
  159. input_mt_slot(input, slot);
  160. input_mt_report_slot_state(input, MT_TOOL_FINGER,
  161. status == SIS_STATUS_DOWN);
  162. if (status == SIS_STATUS_DOWN) {
  163. pressure = height = width = 1;
  164. if (id != SIS_ALL_IN_ONE_PACKAGE) {
  165. if (SIS_PKT_HAS_AREA(id)) {
  166. width = data[SIS_CONTACT_WIDTH_OFFSET];
  167. height = data[SIS_CONTACT_HEIGHT_OFFSET];
  168. }
  169. if (SIS_PKT_HAS_PRESSURE(id))
  170. pressure =
  171. data[SIS_CONTACT_PRESSURE_OFFSET(id)];
  172. }
  173. x = get_unaligned_le16(&data[SIS_CONTACT_X_OFFSET]);
  174. y = get_unaligned_le16(&data[SIS_CONTACT_Y_OFFSET]);
  175. input_report_abs(input, ABS_MT_TOUCH_MAJOR,
  176. width * SIS_AREA_UNIT);
  177. input_report_abs(input, ABS_MT_TOUCH_MINOR,
  178. height * SIS_AREA_UNIT);
  179. input_report_abs(input, ABS_MT_PRESSURE, pressure);
  180. input_report_abs(input, ABS_MT_POSITION_X, x);
  181. input_report_abs(input, ABS_MT_POSITION_Y, y);
  182. }
  183. return 0;
  184. }
  185. static void sis_ts_handle_packet(struct sis_ts_data *ts)
  186. {
  187. const u8 *contact;
  188. unsigned int num_to_report = 0;
  189. unsigned int num_contacts;
  190. unsigned int num_reported;
  191. unsigned int contact_size;
  192. int error;
  193. u8 report_id;
  194. do {
  195. error = sis_read_packet(ts->client, ts->packet,
  196. &num_contacts, &contact_size);
  197. if (error)
  198. break;
  199. if (num_to_report == 0) {
  200. num_to_report = num_contacts;
  201. } else if (num_contacts != 0) {
  202. dev_err(&ts->client->dev,
  203. "%s: nonzero (%d) point count in tail packet\n",
  204. __func__, num_contacts);
  205. break;
  206. }
  207. report_id = ts->packet[SIS_PKT_REPORT_OFFSET];
  208. contact = &ts->packet[SIS_PKT_CONTACT_OFFSET];
  209. num_reported = 0;
  210. while (num_to_report > 0) {
  211. error = sis_ts_report_contact(ts, contact, report_id);
  212. if (error)
  213. break;
  214. contact += contact_size;
  215. num_to_report--;
  216. num_reported++;
  217. if (report_id != SIS_ALL_IN_ONE_PACKAGE &&
  218. num_reported >= 5) {
  219. /*
  220. * The remainder of contacts is sent
  221. * in the 2nd packet.
  222. */
  223. break;
  224. }
  225. }
  226. } while (num_to_report > 0);
  227. input_mt_sync_frame(ts->input);
  228. input_sync(ts->input);
  229. }
  230. static irqreturn_t sis_ts_irq_handler(int irq, void *dev_id)
  231. {
  232. struct sis_ts_data *ts = dev_id;
  233. do {
  234. sis_ts_handle_packet(ts);
  235. } while (ts->attn_gpio && gpiod_get_value_cansleep(ts->attn_gpio));
  236. return IRQ_HANDLED;
  237. }
  238. static void sis_ts_reset(struct sis_ts_data *ts)
  239. {
  240. if (ts->reset_gpio) {
  241. /* Get out of reset */
  242. usleep_range(1000, 2000);
  243. gpiod_set_value(ts->reset_gpio, 1);
  244. usleep_range(1000, 2000);
  245. gpiod_set_value(ts->reset_gpio, 0);
  246. msleep(100);
  247. }
  248. }
  249. static int sis_ts_probe(struct i2c_client *client,
  250. const struct i2c_device_id *id)
  251. {
  252. struct sis_ts_data *ts;
  253. struct input_dev *input;
  254. int error;
  255. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  256. if (!ts)
  257. return -ENOMEM;
  258. ts->client = client;
  259. ts->attn_gpio = devm_gpiod_get_optional(&client->dev,
  260. "attn", GPIOD_IN);
  261. if (IS_ERR(ts->attn_gpio)) {
  262. error = PTR_ERR(ts->attn_gpio);
  263. if (error != -EPROBE_DEFER)
  264. dev_err(&client->dev,
  265. "Failed to get attention GPIO: %d\n", error);
  266. return error;
  267. }
  268. ts->reset_gpio = devm_gpiod_get_optional(&client->dev,
  269. "reset", GPIOD_OUT_LOW);
  270. if (IS_ERR(ts->reset_gpio)) {
  271. error = PTR_ERR(ts->reset_gpio);
  272. if (error != -EPROBE_DEFER)
  273. dev_err(&client->dev,
  274. "Failed to get reset GPIO: %d\n", error);
  275. return error;
  276. }
  277. sis_ts_reset(ts);
  278. ts->input = input = devm_input_allocate_device(&client->dev);
  279. if (!input) {
  280. dev_err(&client->dev, "Failed to allocate input device\n");
  281. return -ENOMEM;
  282. }
  283. input->name = "SiS Touchscreen";
  284. input->id.bustype = BUS_I2C;
  285. input_set_abs_params(input, ABS_MT_POSITION_X, 0, SIS_MAX_X, 0, 0);
  286. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, SIS_MAX_Y, 0, 0);
  287. input_set_abs_params(input, ABS_MT_PRESSURE, 0, SIS_MAX_PRESSURE, 0, 0);
  288. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
  289. 0, SIS_AREA_LENGTH_LONGER, 0, 0);
  290. input_set_abs_params(input, ABS_MT_TOUCH_MINOR,
  291. 0, SIS_AREA_LENGTH_SHORT, 0, 0);
  292. error = input_mt_init_slots(input, SIS_MAX_FINGERS, INPUT_MT_DIRECT);
  293. if (error) {
  294. dev_err(&client->dev,
  295. "Failed to initialize MT slots: %d\n", error);
  296. return error;
  297. }
  298. error = devm_request_threaded_irq(&client->dev, client->irq,
  299. NULL, sis_ts_irq_handler,
  300. IRQF_ONESHOT,
  301. client->name, ts);
  302. if (error) {
  303. dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
  304. return error;
  305. }
  306. error = input_register_device(ts->input);
  307. if (error) {
  308. dev_err(&client->dev,
  309. "Failed to register input device: %d\n", error);
  310. return error;
  311. }
  312. return 0;
  313. }
  314. #ifdef CONFIG_OF
  315. static const struct of_device_id sis_ts_dt_ids[] = {
  316. { .compatible = "sis,9200-ts" },
  317. { /* sentinel */ }
  318. };
  319. MODULE_DEVICE_TABLE(of, sis_ts_dt_ids);
  320. #endif
  321. static const struct i2c_device_id sis_ts_id[] = {
  322. { SIS_I2C_NAME, 0 },
  323. { "9200-ts", 0 },
  324. { /* sentinel */ }
  325. };
  326. MODULE_DEVICE_TABLE(i2c, sis_ts_id);
  327. static struct i2c_driver sis_ts_driver = {
  328. .driver = {
  329. .name = SIS_I2C_NAME,
  330. .of_match_table = of_match_ptr(sis_ts_dt_ids),
  331. },
  332. .probe = sis_ts_probe,
  333. .id_table = sis_ts_id,
  334. };
  335. module_i2c_driver(sis_ts_driver);
  336. MODULE_DESCRIPTION("SiS 9200 Family Touchscreen Driver");
  337. MODULE_LICENSE("GPL v2");
  338. MODULE_AUTHOR("Mika Penttilä <[email protected]>");