sis_i2c.c 10 KB

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