goodix.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. * Driver for Goodix Touchscreens
  3. *
  4. * Copyright (c) 2014 Red Hat Inc.
  5. * Copyright (c) 2015 K. Merker <merker@debian.org>
  6. *
  7. * This code is based on gt9xx.c authored by andrew@goodix.com:
  8. *
  9. * 2010 - 2012 Goodix Technology.
  10. */
  11. /*
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the Free
  14. * Software Foundation; version 2 of the License.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/dmi.h>
  18. #include <linux/firmware.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/i2c.h>
  21. #include <linux/input.h>
  22. #include <linux/input/mt.h>
  23. #include <linux/module.h>
  24. #include <linux/delay.h>
  25. #include <linux/irq.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/slab.h>
  28. #include <linux/acpi.h>
  29. #include <linux/of.h>
  30. #include <asm/unaligned.h>
  31. struct goodix_ts_data {
  32. struct i2c_client *client;
  33. struct input_dev *input_dev;
  34. int abs_x_max;
  35. int abs_y_max;
  36. bool swapped_x_y;
  37. bool inverted_x;
  38. bool inverted_y;
  39. unsigned int max_touch_num;
  40. unsigned int int_trigger_type;
  41. int cfg_len;
  42. struct gpio_desc *gpiod_int;
  43. struct gpio_desc *gpiod_rst;
  44. u16 id;
  45. u16 version;
  46. const char *cfg_name;
  47. struct completion firmware_loading_complete;
  48. unsigned long irq_flags;
  49. };
  50. #define GOODIX_GPIO_INT_NAME "irq"
  51. #define GOODIX_GPIO_RST_NAME "reset"
  52. #define GOODIX_MAX_HEIGHT 4096
  53. #define GOODIX_MAX_WIDTH 4096
  54. #define GOODIX_INT_TRIGGER 1
  55. #define GOODIX_CONTACT_SIZE 8
  56. #define GOODIX_MAX_CONTACTS 10
  57. #define GOODIX_CONFIG_MAX_LENGTH 240
  58. #define GOODIX_CONFIG_911_LENGTH 186
  59. #define GOODIX_CONFIG_967_LENGTH 228
  60. /* Register defines */
  61. #define GOODIX_REG_COMMAND 0x8040
  62. #define GOODIX_CMD_SCREEN_OFF 0x05
  63. #define GOODIX_READ_COOR_ADDR 0x814E
  64. #define GOODIX_REG_CONFIG_DATA 0x8047
  65. #define GOODIX_REG_ID 0x8140
  66. #define RESOLUTION_LOC 1
  67. #define MAX_CONTACTS_LOC 5
  68. #define TRIGGER_LOC 6
  69. static const unsigned long goodix_irq_flags[] = {
  70. IRQ_TYPE_EDGE_RISING,
  71. IRQ_TYPE_EDGE_FALLING,
  72. IRQ_TYPE_LEVEL_LOW,
  73. IRQ_TYPE_LEVEL_HIGH,
  74. };
  75. /*
  76. * Those tablets have their coordinates origin at the bottom right
  77. * of the tablet, as if rotated 180 degrees
  78. */
  79. static const struct dmi_system_id rotated_screen[] = {
  80. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  81. {
  82. .ident = "WinBook TW100",
  83. .matches = {
  84. DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
  85. DMI_MATCH(DMI_PRODUCT_NAME, "TW100")
  86. }
  87. },
  88. {
  89. .ident = "WinBook TW700",
  90. .matches = {
  91. DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
  92. DMI_MATCH(DMI_PRODUCT_NAME, "TW700")
  93. },
  94. },
  95. #endif
  96. {}
  97. };
  98. /**
  99. * goodix_i2c_read - read data from a register of the i2c slave device.
  100. *
  101. * @client: i2c device.
  102. * @reg: the register to read from.
  103. * @buf: raw write data buffer.
  104. * @len: length of the buffer to write
  105. */
  106. static int goodix_i2c_read(struct i2c_client *client,
  107. u16 reg, u8 *buf, int len)
  108. {
  109. struct i2c_msg msgs[2];
  110. u16 wbuf = cpu_to_be16(reg);
  111. int ret;
  112. msgs[0].flags = 0;
  113. msgs[0].addr = client->addr;
  114. msgs[0].len = 2;
  115. msgs[0].buf = (u8 *)&wbuf;
  116. msgs[1].flags = I2C_M_RD;
  117. msgs[1].addr = client->addr;
  118. msgs[1].len = len;
  119. msgs[1].buf = buf;
  120. ret = i2c_transfer(client->adapter, msgs, 2);
  121. return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
  122. }
  123. /**
  124. * goodix_i2c_write - write data to a register of the i2c slave device.
  125. *
  126. * @client: i2c device.
  127. * @reg: the register to write to.
  128. * @buf: raw data buffer to write.
  129. * @len: length of the buffer to write
  130. */
  131. static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf,
  132. unsigned len)
  133. {
  134. u8 *addr_buf;
  135. struct i2c_msg msg;
  136. int ret;
  137. addr_buf = kmalloc(len + 2, GFP_KERNEL);
  138. if (!addr_buf)
  139. return -ENOMEM;
  140. addr_buf[0] = reg >> 8;
  141. addr_buf[1] = reg & 0xFF;
  142. memcpy(&addr_buf[2], buf, len);
  143. msg.flags = 0;
  144. msg.addr = client->addr;
  145. msg.buf = addr_buf;
  146. msg.len = len + 2;
  147. ret = i2c_transfer(client->adapter, &msg, 1);
  148. kfree(addr_buf);
  149. return ret < 0 ? ret : (ret != 1 ? -EIO : 0);
  150. }
  151. static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
  152. {
  153. return goodix_i2c_write(client, reg, &value, sizeof(value));
  154. }
  155. static int goodix_get_cfg_len(u16 id)
  156. {
  157. switch (id) {
  158. case 911:
  159. case 9271:
  160. case 9110:
  161. case 927:
  162. case 928:
  163. return GOODIX_CONFIG_911_LENGTH;
  164. case 912:
  165. case 967:
  166. return GOODIX_CONFIG_967_LENGTH;
  167. default:
  168. return GOODIX_CONFIG_MAX_LENGTH;
  169. }
  170. }
  171. static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
  172. {
  173. int touch_num;
  174. int error;
  175. error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR, data,
  176. GOODIX_CONTACT_SIZE + 1);
  177. if (error) {
  178. dev_err(&ts->client->dev, "I2C transfer error: %d\n", error);
  179. return error;
  180. }
  181. if (!(data[0] & 0x80))
  182. return -EAGAIN;
  183. touch_num = data[0] & 0x0f;
  184. if (touch_num > ts->max_touch_num)
  185. return -EPROTO;
  186. if (touch_num > 1) {
  187. data += 1 + GOODIX_CONTACT_SIZE;
  188. error = goodix_i2c_read(ts->client,
  189. GOODIX_READ_COOR_ADDR +
  190. 1 + GOODIX_CONTACT_SIZE,
  191. data,
  192. GOODIX_CONTACT_SIZE * (touch_num - 1));
  193. if (error)
  194. return error;
  195. }
  196. return touch_num;
  197. }
  198. static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
  199. {
  200. int id = coor_data[0] & 0x0F;
  201. int input_x = get_unaligned_le16(&coor_data[1]);
  202. int input_y = get_unaligned_le16(&coor_data[3]);
  203. int input_w = get_unaligned_le16(&coor_data[5]);
  204. /* Inversions have to happen before axis swapping */
  205. if (ts->inverted_x)
  206. input_x = ts->abs_x_max - input_x;
  207. if (ts->inverted_y)
  208. input_y = ts->abs_y_max - input_y;
  209. if (ts->swapped_x_y)
  210. swap(input_x, input_y);
  211. input_mt_slot(ts->input_dev, id);
  212. input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
  213. input_report_abs(ts->input_dev, ABS_MT_POSITION_X, input_x);
  214. input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, input_y);
  215. input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
  216. input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
  217. }
  218. /**
  219. * goodix_process_events - Process incoming events
  220. *
  221. * @ts: our goodix_ts_data pointer
  222. *
  223. * Called when the IRQ is triggered. Read the current device state, and push
  224. * the input events to the user space.
  225. */
  226. static void goodix_process_events(struct goodix_ts_data *ts)
  227. {
  228. u8 point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
  229. int touch_num;
  230. int i;
  231. touch_num = goodix_ts_read_input_report(ts, point_data);
  232. if (touch_num < 0)
  233. return;
  234. /*
  235. * Bit 4 of the first byte reports the status of the capacitive
  236. * Windows/Home button.
  237. */
  238. input_report_key(ts->input_dev, KEY_LEFTMETA, point_data[0] & BIT(4));
  239. for (i = 0; i < touch_num; i++)
  240. goodix_ts_report_touch(ts,
  241. &point_data[1 + GOODIX_CONTACT_SIZE * i]);
  242. input_mt_sync_frame(ts->input_dev);
  243. input_sync(ts->input_dev);
  244. }
  245. /**
  246. * goodix_ts_irq_handler - The IRQ handler
  247. *
  248. * @irq: interrupt number.
  249. * @dev_id: private data pointer.
  250. */
  251. static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
  252. {
  253. struct goodix_ts_data *ts = dev_id;
  254. goodix_process_events(ts);
  255. if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0)
  256. dev_err(&ts->client->dev, "I2C write end_cmd error\n");
  257. return IRQ_HANDLED;
  258. }
  259. static void goodix_free_irq(struct goodix_ts_data *ts)
  260. {
  261. devm_free_irq(&ts->client->dev, ts->client->irq, ts);
  262. }
  263. static int goodix_request_irq(struct goodix_ts_data *ts)
  264. {
  265. return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
  266. NULL, goodix_ts_irq_handler,
  267. ts->irq_flags, ts->client->name, ts);
  268. }
  269. /**
  270. * goodix_check_cfg - Checks if config fw is valid
  271. *
  272. * @ts: goodix_ts_data pointer
  273. * @cfg: firmware config data
  274. */
  275. static int goodix_check_cfg(struct goodix_ts_data *ts,
  276. const struct firmware *cfg)
  277. {
  278. int i, raw_cfg_len;
  279. u8 check_sum = 0;
  280. if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) {
  281. dev_err(&ts->client->dev,
  282. "The length of the config fw is not correct");
  283. return -EINVAL;
  284. }
  285. raw_cfg_len = cfg->size - 2;
  286. for (i = 0; i < raw_cfg_len; i++)
  287. check_sum += cfg->data[i];
  288. check_sum = (~check_sum) + 1;
  289. if (check_sum != cfg->data[raw_cfg_len]) {
  290. dev_err(&ts->client->dev,
  291. "The checksum of the config fw is not correct");
  292. return -EINVAL;
  293. }
  294. if (cfg->data[raw_cfg_len + 1] != 1) {
  295. dev_err(&ts->client->dev,
  296. "Config fw must have Config_Fresh register set");
  297. return -EINVAL;
  298. }
  299. return 0;
  300. }
  301. /**
  302. * goodix_send_cfg - Write fw config to device
  303. *
  304. * @ts: goodix_ts_data pointer
  305. * @cfg: config firmware to write to device
  306. */
  307. static int goodix_send_cfg(struct goodix_ts_data *ts,
  308. const struct firmware *cfg)
  309. {
  310. int error;
  311. error = goodix_check_cfg(ts, cfg);
  312. if (error)
  313. return error;
  314. error = goodix_i2c_write(ts->client, GOODIX_REG_CONFIG_DATA, cfg->data,
  315. cfg->size);
  316. if (error) {
  317. dev_err(&ts->client->dev, "Failed to write config data: %d",
  318. error);
  319. return error;
  320. }
  321. dev_dbg(&ts->client->dev, "Config sent successfully.");
  322. /* Let the firmware reconfigure itself, so sleep for 10ms */
  323. usleep_range(10000, 11000);
  324. return 0;
  325. }
  326. static int goodix_int_sync(struct goodix_ts_data *ts)
  327. {
  328. int error;
  329. error = gpiod_direction_output(ts->gpiod_int, 0);
  330. if (error)
  331. return error;
  332. msleep(50); /* T5: 50ms */
  333. error = gpiod_direction_input(ts->gpiod_int);
  334. if (error)
  335. return error;
  336. return 0;
  337. }
  338. /**
  339. * goodix_reset - Reset device during power on
  340. *
  341. * @ts: goodix_ts_data pointer
  342. */
  343. static int goodix_reset(struct goodix_ts_data *ts)
  344. {
  345. int error;
  346. /* begin select I2C slave addr */
  347. error = gpiod_direction_output(ts->gpiod_rst, 0);
  348. if (error)
  349. return error;
  350. msleep(20); /* T2: > 10ms */
  351. /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
  352. error = gpiod_direction_output(ts->gpiod_int, ts->client->addr == 0x14);
  353. if (error)
  354. return error;
  355. usleep_range(100, 2000); /* T3: > 100us */
  356. error = gpiod_direction_output(ts->gpiod_rst, 1);
  357. if (error)
  358. return error;
  359. usleep_range(6000, 10000); /* T4: > 5ms */
  360. /* end select I2C slave addr */
  361. error = gpiod_direction_input(ts->gpiod_rst);
  362. if (error)
  363. return error;
  364. error = goodix_int_sync(ts);
  365. if (error)
  366. return error;
  367. return 0;
  368. }
  369. /**
  370. * goodix_get_gpio_config - Get GPIO config from ACPI/DT
  371. *
  372. * @ts: goodix_ts_data pointer
  373. */
  374. static int goodix_get_gpio_config(struct goodix_ts_data *ts)
  375. {
  376. int error;
  377. struct device *dev;
  378. struct gpio_desc *gpiod;
  379. if (!ts->client)
  380. return -EINVAL;
  381. dev = &ts->client->dev;
  382. /* Get the interrupt GPIO pin number */
  383. gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
  384. if (IS_ERR(gpiod)) {
  385. error = PTR_ERR(gpiod);
  386. if (error != -EPROBE_DEFER)
  387. dev_dbg(dev, "Failed to get %s GPIO: %d\n",
  388. GOODIX_GPIO_INT_NAME, error);
  389. return error;
  390. }
  391. ts->gpiod_int = gpiod;
  392. /* Get the reset line GPIO pin number */
  393. gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN);
  394. if (IS_ERR(gpiod)) {
  395. error = PTR_ERR(gpiod);
  396. if (error != -EPROBE_DEFER)
  397. dev_dbg(dev, "Failed to get %s GPIO: %d\n",
  398. GOODIX_GPIO_RST_NAME, error);
  399. return error;
  400. }
  401. ts->gpiod_rst = gpiod;
  402. return 0;
  403. }
  404. /**
  405. * goodix_read_config - Read the embedded configuration of the panel
  406. *
  407. * @ts: our goodix_ts_data pointer
  408. *
  409. * Must be called during probe
  410. */
  411. static void goodix_read_config(struct goodix_ts_data *ts)
  412. {
  413. u8 config[GOODIX_CONFIG_MAX_LENGTH];
  414. int error;
  415. error = goodix_i2c_read(ts->client, GOODIX_REG_CONFIG_DATA,
  416. config, ts->cfg_len);
  417. if (error) {
  418. dev_warn(&ts->client->dev,
  419. "Error reading config (%d), using defaults\n",
  420. error);
  421. ts->abs_x_max = GOODIX_MAX_WIDTH;
  422. ts->abs_y_max = GOODIX_MAX_HEIGHT;
  423. if (ts->swapped_x_y)
  424. swap(ts->abs_x_max, ts->abs_y_max);
  425. ts->int_trigger_type = GOODIX_INT_TRIGGER;
  426. ts->max_touch_num = GOODIX_MAX_CONTACTS;
  427. return;
  428. }
  429. ts->abs_x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
  430. ts->abs_y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
  431. if (ts->swapped_x_y)
  432. swap(ts->abs_x_max, ts->abs_y_max);
  433. ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
  434. ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
  435. if (!ts->abs_x_max || !ts->abs_y_max || !ts->max_touch_num) {
  436. dev_err(&ts->client->dev,
  437. "Invalid config, using defaults\n");
  438. ts->abs_x_max = GOODIX_MAX_WIDTH;
  439. ts->abs_y_max = GOODIX_MAX_HEIGHT;
  440. if (ts->swapped_x_y)
  441. swap(ts->abs_x_max, ts->abs_y_max);
  442. ts->max_touch_num = GOODIX_MAX_CONTACTS;
  443. }
  444. if (dmi_check_system(rotated_screen)) {
  445. ts->inverted_x = true;
  446. ts->inverted_y = true;
  447. dev_dbg(&ts->client->dev,
  448. "Applying '180 degrees rotated screen' quirk\n");
  449. }
  450. }
  451. /**
  452. * goodix_read_version - Read goodix touchscreen version
  453. *
  454. * @ts: our goodix_ts_data pointer
  455. */
  456. static int goodix_read_version(struct goodix_ts_data *ts)
  457. {
  458. int error;
  459. u8 buf[6];
  460. char id_str[5];
  461. error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
  462. if (error) {
  463. dev_err(&ts->client->dev, "read version failed: %d\n", error);
  464. return error;
  465. }
  466. memcpy(id_str, buf, 4);
  467. id_str[4] = 0;
  468. if (kstrtou16(id_str, 10, &ts->id))
  469. ts->id = 0x1001;
  470. ts->version = get_unaligned_le16(&buf[4]);
  471. dev_info(&ts->client->dev, "ID %d, version: %04x\n", ts->id,
  472. ts->version);
  473. return 0;
  474. }
  475. /**
  476. * goodix_i2c_test - I2C test function to check if the device answers.
  477. *
  478. * @client: the i2c client
  479. */
  480. static int goodix_i2c_test(struct i2c_client *client)
  481. {
  482. int retry = 0;
  483. int error;
  484. u8 test;
  485. while (retry++ < 2) {
  486. error = goodix_i2c_read(client, GOODIX_REG_CONFIG_DATA,
  487. &test, 1);
  488. if (!error)
  489. return 0;
  490. dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
  491. retry, error);
  492. msleep(20);
  493. }
  494. return error;
  495. }
  496. /**
  497. * goodix_request_input_dev - Allocate, populate and register the input device
  498. *
  499. * @ts: our goodix_ts_data pointer
  500. *
  501. * Must be called during probe
  502. */
  503. static int goodix_request_input_dev(struct goodix_ts_data *ts)
  504. {
  505. int error;
  506. ts->input_dev = devm_input_allocate_device(&ts->client->dev);
  507. if (!ts->input_dev) {
  508. dev_err(&ts->client->dev, "Failed to allocate input device.");
  509. return -ENOMEM;
  510. }
  511. input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
  512. 0, ts->abs_x_max, 0, 0);
  513. input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
  514. 0, ts->abs_y_max, 0, 0);
  515. input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
  516. input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  517. input_mt_init_slots(ts->input_dev, ts->max_touch_num,
  518. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  519. ts->input_dev->name = "Goodix Capacitive TouchScreen";
  520. ts->input_dev->phys = "input/ts";
  521. ts->input_dev->id.bustype = BUS_I2C;
  522. ts->input_dev->id.vendor = 0x0416;
  523. ts->input_dev->id.product = ts->id;
  524. ts->input_dev->id.version = ts->version;
  525. /* Capacitive Windows/Home button on some devices */
  526. input_set_capability(ts->input_dev, EV_KEY, KEY_LEFTMETA);
  527. error = input_register_device(ts->input_dev);
  528. if (error) {
  529. dev_err(&ts->client->dev,
  530. "Failed to register input device: %d", error);
  531. return error;
  532. }
  533. return 0;
  534. }
  535. /**
  536. * goodix_configure_dev - Finish device initialization
  537. *
  538. * @ts: our goodix_ts_data pointer
  539. *
  540. * Must be called from probe to finish initialization of the device.
  541. * Contains the common initialization code for both devices that
  542. * declare gpio pins and devices that do not. It is either called
  543. * directly from probe or from request_firmware_wait callback.
  544. */
  545. static int goodix_configure_dev(struct goodix_ts_data *ts)
  546. {
  547. int error;
  548. ts->swapped_x_y = device_property_read_bool(&ts->client->dev,
  549. "touchscreen-swapped-x-y");
  550. ts->inverted_x = device_property_read_bool(&ts->client->dev,
  551. "touchscreen-inverted-x");
  552. ts->inverted_y = device_property_read_bool(&ts->client->dev,
  553. "touchscreen-inverted-y");
  554. goodix_read_config(ts);
  555. error = goodix_request_input_dev(ts);
  556. if (error)
  557. return error;
  558. ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
  559. error = goodix_request_irq(ts);
  560. if (error) {
  561. dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
  562. return error;
  563. }
  564. return 0;
  565. }
  566. /**
  567. * goodix_config_cb - Callback to finish device init
  568. *
  569. * @ts: our goodix_ts_data pointer
  570. *
  571. * request_firmware_wait callback that finishes
  572. * initialization of the device.
  573. */
  574. static void goodix_config_cb(const struct firmware *cfg, void *ctx)
  575. {
  576. struct goodix_ts_data *ts = ctx;
  577. int error;
  578. if (cfg) {
  579. /* send device configuration to the firmware */
  580. error = goodix_send_cfg(ts, cfg);
  581. if (error)
  582. goto err_release_cfg;
  583. }
  584. goodix_configure_dev(ts);
  585. err_release_cfg:
  586. release_firmware(cfg);
  587. complete_all(&ts->firmware_loading_complete);
  588. }
  589. static int goodix_ts_probe(struct i2c_client *client,
  590. const struct i2c_device_id *id)
  591. {
  592. struct goodix_ts_data *ts;
  593. int error;
  594. dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
  595. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  596. dev_err(&client->dev, "I2C check functionality failed.\n");
  597. return -ENXIO;
  598. }
  599. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  600. if (!ts)
  601. return -ENOMEM;
  602. ts->client = client;
  603. i2c_set_clientdata(client, ts);
  604. init_completion(&ts->firmware_loading_complete);
  605. error = goodix_get_gpio_config(ts);
  606. if (error)
  607. return error;
  608. if (ts->gpiod_int && ts->gpiod_rst) {
  609. /* reset the controller */
  610. error = goodix_reset(ts);
  611. if (error) {
  612. dev_err(&client->dev, "Controller reset failed.\n");
  613. return error;
  614. }
  615. }
  616. error = goodix_i2c_test(client);
  617. if (error) {
  618. dev_err(&client->dev, "I2C communication failure: %d\n", error);
  619. return error;
  620. }
  621. error = goodix_read_version(ts);
  622. if (error) {
  623. dev_err(&client->dev, "Read version failed.\n");
  624. return error;
  625. }
  626. ts->cfg_len = goodix_get_cfg_len(ts->id);
  627. if (ts->gpiod_int && ts->gpiod_rst) {
  628. /* update device config */
  629. ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL,
  630. "goodix_%d_cfg.bin", ts->id);
  631. if (!ts->cfg_name)
  632. return -ENOMEM;
  633. error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
  634. &client->dev, GFP_KERNEL, ts,
  635. goodix_config_cb);
  636. if (error) {
  637. dev_err(&client->dev,
  638. "Failed to invoke firmware loader: %d\n",
  639. error);
  640. return error;
  641. }
  642. return 0;
  643. } else {
  644. error = goodix_configure_dev(ts);
  645. if (error)
  646. return error;
  647. }
  648. return 0;
  649. }
  650. static int goodix_ts_remove(struct i2c_client *client)
  651. {
  652. struct goodix_ts_data *ts = i2c_get_clientdata(client);
  653. if (ts->gpiod_int && ts->gpiod_rst)
  654. wait_for_completion(&ts->firmware_loading_complete);
  655. return 0;
  656. }
  657. static int __maybe_unused goodix_suspend(struct device *dev)
  658. {
  659. struct i2c_client *client = to_i2c_client(dev);
  660. struct goodix_ts_data *ts = i2c_get_clientdata(client);
  661. int error;
  662. /* We need gpio pins to suspend/resume */
  663. if (!ts->gpiod_int || !ts->gpiod_rst)
  664. return 0;
  665. wait_for_completion(&ts->firmware_loading_complete);
  666. /* Free IRQ as IRQ pin is used as output in the suspend sequence */
  667. goodix_free_irq(ts);
  668. /* Output LOW on the INT pin for 5 ms */
  669. error = gpiod_direction_output(ts->gpiod_int, 0);
  670. if (error) {
  671. goodix_request_irq(ts);
  672. return error;
  673. }
  674. usleep_range(5000, 6000);
  675. error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
  676. GOODIX_CMD_SCREEN_OFF);
  677. if (error) {
  678. dev_err(&ts->client->dev, "Screen off command failed\n");
  679. gpiod_direction_input(ts->gpiod_int);
  680. goodix_request_irq(ts);
  681. return -EAGAIN;
  682. }
  683. /*
  684. * The datasheet specifies that the interval between sending screen-off
  685. * command and wake-up should be longer than 58 ms. To avoid waking up
  686. * sooner, delay 58ms here.
  687. */
  688. msleep(58);
  689. return 0;
  690. }
  691. static int __maybe_unused goodix_resume(struct device *dev)
  692. {
  693. struct i2c_client *client = to_i2c_client(dev);
  694. struct goodix_ts_data *ts = i2c_get_clientdata(client);
  695. int error;
  696. if (!ts->gpiod_int || !ts->gpiod_rst)
  697. return 0;
  698. /*
  699. * Exit sleep mode by outputting HIGH level to INT pin
  700. * for 2ms~5ms.
  701. */
  702. error = gpiod_direction_output(ts->gpiod_int, 1);
  703. if (error)
  704. return error;
  705. usleep_range(2000, 5000);
  706. error = goodix_int_sync(ts);
  707. if (error)
  708. return error;
  709. error = goodix_request_irq(ts);
  710. if (error)
  711. return error;
  712. return 0;
  713. }
  714. static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
  715. static const struct i2c_device_id goodix_ts_id[] = {
  716. { "GDIX1001:00", 0 },
  717. { }
  718. };
  719. MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
  720. #ifdef CONFIG_ACPI
  721. static const struct acpi_device_id goodix_acpi_match[] = {
  722. { "GDIX1001", 0 },
  723. { }
  724. };
  725. MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
  726. #endif
  727. #ifdef CONFIG_OF
  728. static const struct of_device_id goodix_of_match[] = {
  729. { .compatible = "goodix,gt911" },
  730. { .compatible = "goodix,gt9110" },
  731. { .compatible = "goodix,gt912" },
  732. { .compatible = "goodix,gt927" },
  733. { .compatible = "goodix,gt9271" },
  734. { .compatible = "goodix,gt928" },
  735. { .compatible = "goodix,gt967" },
  736. { }
  737. };
  738. MODULE_DEVICE_TABLE(of, goodix_of_match);
  739. #endif
  740. static struct i2c_driver goodix_ts_driver = {
  741. .probe = goodix_ts_probe,
  742. .remove = goodix_ts_remove,
  743. .id_table = goodix_ts_id,
  744. .driver = {
  745. .name = "Goodix-TS",
  746. .acpi_match_table = ACPI_PTR(goodix_acpi_match),
  747. .of_match_table = of_match_ptr(goodix_of_match),
  748. .pm = &goodix_pm_ops,
  749. },
  750. };
  751. module_i2c_driver(goodix_ts_driver);
  752. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  753. MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
  754. MODULE_DESCRIPTION("Goodix touchscreen driver");
  755. MODULE_LICENSE("GPL v2");