goodix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Driver for Goodix Touchscreens
  3. *
  4. * Copyright (c) 2014 Red Hat Inc.
  5. *
  6. * This code is based on gt9xx.c authored by andrew@goodix.com:
  7. *
  8. * 2010 - 2012 Goodix Technology.
  9. */
  10. /*
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; version 2 of the License.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/dmi.h>
  17. #include <linux/i2c.h>
  18. #include <linux/input.h>
  19. #include <linux/input/mt.h>
  20. #include <linux/module.h>
  21. #include <linux/delay.h>
  22. #include <linux/irq.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/slab.h>
  25. #include <linux/acpi.h>
  26. #include <linux/of.h>
  27. #include <asm/unaligned.h>
  28. struct goodix_ts_data {
  29. struct i2c_client *client;
  30. struct input_dev *input_dev;
  31. int abs_x_max;
  32. int abs_y_max;
  33. unsigned int max_touch_num;
  34. unsigned int int_trigger_type;
  35. bool rotated_screen;
  36. };
  37. #define GOODIX_MAX_HEIGHT 4096
  38. #define GOODIX_MAX_WIDTH 4096
  39. #define GOODIX_INT_TRIGGER 1
  40. #define GOODIX_CONTACT_SIZE 8
  41. #define GOODIX_MAX_CONTACTS 10
  42. #define GOODIX_CONFIG_MAX_LENGTH 240
  43. /* Register defines */
  44. #define GOODIX_READ_COOR_ADDR 0x814E
  45. #define GOODIX_REG_CONFIG_DATA 0x8047
  46. #define GOODIX_REG_ID 0x8140
  47. #define RESOLUTION_LOC 1
  48. #define MAX_CONTACTS_LOC 5
  49. #define TRIGGER_LOC 6
  50. static const unsigned long goodix_irq_flags[] = {
  51. IRQ_TYPE_EDGE_RISING,
  52. IRQ_TYPE_EDGE_FALLING,
  53. IRQ_TYPE_LEVEL_LOW,
  54. IRQ_TYPE_LEVEL_HIGH,
  55. };
  56. /*
  57. * Those tablets have their coordinates origin at the bottom right
  58. * of the tablet, as if rotated 180 degrees
  59. */
  60. static const struct dmi_system_id rotated_screen[] = {
  61. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  62. {
  63. .ident = "WinBook TW100",
  64. .matches = {
  65. DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
  66. DMI_MATCH(DMI_PRODUCT_NAME, "TW100")
  67. }
  68. },
  69. {
  70. .ident = "WinBook TW700",
  71. .matches = {
  72. DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
  73. DMI_MATCH(DMI_PRODUCT_NAME, "TW700")
  74. },
  75. },
  76. #endif
  77. {}
  78. };
  79. /**
  80. * goodix_i2c_read - read data from a register of the i2c slave device.
  81. *
  82. * @client: i2c device.
  83. * @reg: the register to read from.
  84. * @buf: raw write data buffer.
  85. * @len: length of the buffer to write
  86. */
  87. static int goodix_i2c_read(struct i2c_client *client,
  88. u16 reg, u8 *buf, int len)
  89. {
  90. struct i2c_msg msgs[2];
  91. u16 wbuf = cpu_to_be16(reg);
  92. int ret;
  93. msgs[0].flags = 0;
  94. msgs[0].addr = client->addr;
  95. msgs[0].len = 2;
  96. msgs[0].buf = (u8 *)&wbuf;
  97. msgs[1].flags = I2C_M_RD;
  98. msgs[1].addr = client->addr;
  99. msgs[1].len = len;
  100. msgs[1].buf = buf;
  101. ret = i2c_transfer(client->adapter, msgs, 2);
  102. return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
  103. }
  104. static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
  105. {
  106. int touch_num;
  107. int error;
  108. error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR, data,
  109. GOODIX_CONTACT_SIZE + 1);
  110. if (error) {
  111. dev_err(&ts->client->dev, "I2C transfer error: %d\n", error);
  112. return error;
  113. }
  114. if (!(data[0] & 0x80))
  115. return -EAGAIN;
  116. touch_num = data[0] & 0x0f;
  117. if (touch_num > ts->max_touch_num)
  118. return -EPROTO;
  119. if (touch_num > 1) {
  120. data += 1 + GOODIX_CONTACT_SIZE;
  121. error = goodix_i2c_read(ts->client,
  122. GOODIX_READ_COOR_ADDR +
  123. 1 + GOODIX_CONTACT_SIZE,
  124. data,
  125. GOODIX_CONTACT_SIZE * (touch_num - 1));
  126. if (error)
  127. return error;
  128. }
  129. return touch_num;
  130. }
  131. static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
  132. {
  133. int id = coor_data[0] & 0x0F;
  134. int input_x = get_unaligned_le16(&coor_data[1]);
  135. int input_y = get_unaligned_le16(&coor_data[3]);
  136. int input_w = get_unaligned_le16(&coor_data[5]);
  137. if (ts->rotated_screen) {
  138. input_x = ts->abs_x_max - input_x;
  139. input_y = ts->abs_y_max - input_y;
  140. }
  141. input_mt_slot(ts->input_dev, id);
  142. input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
  143. input_report_abs(ts->input_dev, ABS_MT_POSITION_X, input_x);
  144. input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, input_y);
  145. input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
  146. input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
  147. }
  148. /**
  149. * goodix_process_events - Process incoming events
  150. *
  151. * @ts: our goodix_ts_data pointer
  152. *
  153. * Called when the IRQ is triggered. Read the current device state, and push
  154. * the input events to the user space.
  155. */
  156. static void goodix_process_events(struct goodix_ts_data *ts)
  157. {
  158. u8 point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
  159. int touch_num;
  160. int i;
  161. touch_num = goodix_ts_read_input_report(ts, point_data);
  162. if (touch_num < 0)
  163. return;
  164. for (i = 0; i < touch_num; i++)
  165. goodix_ts_report_touch(ts,
  166. &point_data[1 + GOODIX_CONTACT_SIZE * i]);
  167. input_mt_sync_frame(ts->input_dev);
  168. input_sync(ts->input_dev);
  169. }
  170. /**
  171. * goodix_ts_irq_handler - The IRQ handler
  172. *
  173. * @irq: interrupt number.
  174. * @dev_id: private data pointer.
  175. */
  176. static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
  177. {
  178. static const u8 end_cmd[] = {
  179. GOODIX_READ_COOR_ADDR >> 8,
  180. GOODIX_READ_COOR_ADDR & 0xff,
  181. 0
  182. };
  183. struct goodix_ts_data *ts = dev_id;
  184. goodix_process_events(ts);
  185. if (i2c_master_send(ts->client, end_cmd, sizeof(end_cmd)) < 0)
  186. dev_err(&ts->client->dev, "I2C write end_cmd error\n");
  187. return IRQ_HANDLED;
  188. }
  189. /**
  190. * goodix_read_config - Read the embedded configuration of the panel
  191. *
  192. * @ts: our goodix_ts_data pointer
  193. *
  194. * Must be called during probe
  195. */
  196. static void goodix_read_config(struct goodix_ts_data *ts)
  197. {
  198. u8 config[GOODIX_CONFIG_MAX_LENGTH];
  199. int error;
  200. error = goodix_i2c_read(ts->client, GOODIX_REG_CONFIG_DATA,
  201. config,
  202. GOODIX_CONFIG_MAX_LENGTH);
  203. if (error) {
  204. dev_warn(&ts->client->dev,
  205. "Error reading config (%d), using defaults\n",
  206. error);
  207. ts->abs_x_max = GOODIX_MAX_WIDTH;
  208. ts->abs_y_max = GOODIX_MAX_HEIGHT;
  209. ts->int_trigger_type = GOODIX_INT_TRIGGER;
  210. ts->max_touch_num = GOODIX_MAX_CONTACTS;
  211. return;
  212. }
  213. ts->abs_x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
  214. ts->abs_y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
  215. ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
  216. ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
  217. if (!ts->abs_x_max || !ts->abs_y_max || !ts->max_touch_num) {
  218. dev_err(&ts->client->dev,
  219. "Invalid config, using defaults\n");
  220. ts->abs_x_max = GOODIX_MAX_WIDTH;
  221. ts->abs_y_max = GOODIX_MAX_HEIGHT;
  222. ts->max_touch_num = GOODIX_MAX_CONTACTS;
  223. }
  224. ts->rotated_screen = dmi_check_system(rotated_screen);
  225. if (ts->rotated_screen)
  226. dev_dbg(&ts->client->dev,
  227. "Applying '180 degrees rotated screen' quirk\n");
  228. }
  229. /**
  230. * goodix_read_version - Read goodix touchscreen version
  231. *
  232. * @client: the i2c client
  233. * @version: output buffer containing the version on success
  234. * @id: output buffer containing the id on success
  235. */
  236. static int goodix_read_version(struct i2c_client *client, u16 *version, u16 *id)
  237. {
  238. int error;
  239. u8 buf[6];
  240. char id_str[5];
  241. error = goodix_i2c_read(client, GOODIX_REG_ID, buf, sizeof(buf));
  242. if (error) {
  243. dev_err(&client->dev, "read version failed: %d\n", error);
  244. return error;
  245. }
  246. memcpy(id_str, buf, 4);
  247. id_str[4] = 0;
  248. if (kstrtou16(id_str, 10, id))
  249. *id = 0x1001;
  250. *version = get_unaligned_le16(&buf[4]);
  251. dev_info(&client->dev, "ID %d, version: %04x\n", *id, *version);
  252. return 0;
  253. }
  254. /**
  255. * goodix_i2c_test - I2C test function to check if the device answers.
  256. *
  257. * @client: the i2c client
  258. */
  259. static int goodix_i2c_test(struct i2c_client *client)
  260. {
  261. int retry = 0;
  262. int error;
  263. u8 test;
  264. while (retry++ < 2) {
  265. error = goodix_i2c_read(client, GOODIX_REG_CONFIG_DATA,
  266. &test, 1);
  267. if (!error)
  268. return 0;
  269. dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
  270. retry, error);
  271. msleep(20);
  272. }
  273. return error;
  274. }
  275. /**
  276. * goodix_request_input_dev - Allocate, populate and register the input device
  277. *
  278. * @ts: our goodix_ts_data pointer
  279. * @version: device firmware version
  280. * @id: device ID
  281. *
  282. * Must be called during probe
  283. */
  284. static int goodix_request_input_dev(struct goodix_ts_data *ts, u16 version,
  285. u16 id)
  286. {
  287. int error;
  288. ts->input_dev = devm_input_allocate_device(&ts->client->dev);
  289. if (!ts->input_dev) {
  290. dev_err(&ts->client->dev, "Failed to allocate input device.");
  291. return -ENOMEM;
  292. }
  293. input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
  294. 0, ts->abs_x_max, 0, 0);
  295. input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
  296. 0, ts->abs_y_max, 0, 0);
  297. input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
  298. input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  299. input_mt_init_slots(ts->input_dev, ts->max_touch_num,
  300. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  301. ts->input_dev->name = "Goodix Capacitive TouchScreen";
  302. ts->input_dev->phys = "input/ts";
  303. ts->input_dev->id.bustype = BUS_I2C;
  304. ts->input_dev->id.vendor = 0x0416;
  305. ts->input_dev->id.product = id;
  306. ts->input_dev->id.version = version;
  307. error = input_register_device(ts->input_dev);
  308. if (error) {
  309. dev_err(&ts->client->dev,
  310. "Failed to register input device: %d", error);
  311. return error;
  312. }
  313. return 0;
  314. }
  315. static int goodix_ts_probe(struct i2c_client *client,
  316. const struct i2c_device_id *id)
  317. {
  318. struct goodix_ts_data *ts;
  319. unsigned long irq_flags;
  320. int error;
  321. u16 version_info, id_info;
  322. dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
  323. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  324. dev_err(&client->dev, "I2C check functionality failed.\n");
  325. return -ENXIO;
  326. }
  327. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  328. if (!ts)
  329. return -ENOMEM;
  330. ts->client = client;
  331. i2c_set_clientdata(client, ts);
  332. error = goodix_i2c_test(client);
  333. if (error) {
  334. dev_err(&client->dev, "I2C communication failure: %d\n", error);
  335. return error;
  336. }
  337. error = goodix_read_version(client, &version_info, &id_info);
  338. if (error) {
  339. dev_err(&client->dev, "Read version failed.\n");
  340. return error;
  341. }
  342. goodix_read_config(ts);
  343. error = goodix_request_input_dev(ts, version_info, id_info);
  344. if (error)
  345. return error;
  346. irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
  347. error = devm_request_threaded_irq(&ts->client->dev, client->irq,
  348. NULL, goodix_ts_irq_handler,
  349. irq_flags, client->name, ts);
  350. if (error) {
  351. dev_err(&client->dev, "request IRQ failed: %d\n", error);
  352. return error;
  353. }
  354. return 0;
  355. }
  356. static const struct i2c_device_id goodix_ts_id[] = {
  357. { "GDIX1001:00", 0 },
  358. { }
  359. };
  360. MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
  361. #ifdef CONFIG_ACPI
  362. static const struct acpi_device_id goodix_acpi_match[] = {
  363. { "GDIX1001", 0 },
  364. { "GDIX1002", 0 },
  365. { }
  366. };
  367. MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
  368. #endif
  369. #ifdef CONFIG_OF
  370. static const struct of_device_id goodix_of_match[] = {
  371. { .compatible = "goodix,gt911" },
  372. { .compatible = "goodix,gt9110" },
  373. { .compatible = "goodix,gt912" },
  374. { .compatible = "goodix,gt927" },
  375. { .compatible = "goodix,gt9271" },
  376. { .compatible = "goodix,gt928" },
  377. { .compatible = "goodix,gt967" },
  378. { }
  379. };
  380. MODULE_DEVICE_TABLE(of, goodix_of_match);
  381. #endif
  382. static struct i2c_driver goodix_ts_driver = {
  383. .probe = goodix_ts_probe,
  384. .id_table = goodix_ts_id,
  385. .driver = {
  386. .name = "Goodix-TS",
  387. .acpi_match_table = ACPI_PTR(goodix_acpi_match),
  388. .of_match_table = of_match_ptr(goodix_of_match),
  389. },
  390. };
  391. module_i2c_driver(goodix_ts_driver);
  392. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  393. MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
  394. MODULE_DESCRIPTION("Goodix touchscreen driver");
  395. MODULE_LICENSE("GPL v2");