goodix.c 10 KB

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