goodix.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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. for (i = 0; i < touch_num; i++)
  235. goodix_ts_report_touch(ts,
  236. &point_data[1 + GOODIX_CONTACT_SIZE * i]);
  237. input_mt_sync_frame(ts->input_dev);
  238. input_sync(ts->input_dev);
  239. }
  240. /**
  241. * goodix_ts_irq_handler - The IRQ handler
  242. *
  243. * @irq: interrupt number.
  244. * @dev_id: private data pointer.
  245. */
  246. static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
  247. {
  248. struct goodix_ts_data *ts = dev_id;
  249. goodix_process_events(ts);
  250. if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0)
  251. dev_err(&ts->client->dev, "I2C write end_cmd error\n");
  252. return IRQ_HANDLED;
  253. }
  254. static void goodix_free_irq(struct goodix_ts_data *ts)
  255. {
  256. devm_free_irq(&ts->client->dev, ts->client->irq, ts);
  257. }
  258. static int goodix_request_irq(struct goodix_ts_data *ts)
  259. {
  260. return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
  261. NULL, goodix_ts_irq_handler,
  262. ts->irq_flags, ts->client->name, ts);
  263. }
  264. /**
  265. * goodix_check_cfg - Checks if config fw is valid
  266. *
  267. * @ts: goodix_ts_data pointer
  268. * @cfg: firmware config data
  269. */
  270. static int goodix_check_cfg(struct goodix_ts_data *ts,
  271. const struct firmware *cfg)
  272. {
  273. int i, raw_cfg_len;
  274. u8 check_sum = 0;
  275. if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) {
  276. dev_err(&ts->client->dev,
  277. "The length of the config fw is not correct");
  278. return -EINVAL;
  279. }
  280. raw_cfg_len = cfg->size - 2;
  281. for (i = 0; i < raw_cfg_len; i++)
  282. check_sum += cfg->data[i];
  283. check_sum = (~check_sum) + 1;
  284. if (check_sum != cfg->data[raw_cfg_len]) {
  285. dev_err(&ts->client->dev,
  286. "The checksum of the config fw is not correct");
  287. return -EINVAL;
  288. }
  289. if (cfg->data[raw_cfg_len + 1] != 1) {
  290. dev_err(&ts->client->dev,
  291. "Config fw must have Config_Fresh register set");
  292. return -EINVAL;
  293. }
  294. return 0;
  295. }
  296. /**
  297. * goodix_send_cfg - Write fw config to device
  298. *
  299. * @ts: goodix_ts_data pointer
  300. * @cfg: config firmware to write to device
  301. */
  302. static int goodix_send_cfg(struct goodix_ts_data *ts,
  303. const struct firmware *cfg)
  304. {
  305. int error;
  306. error = goodix_check_cfg(ts, cfg);
  307. if (error)
  308. return error;
  309. error = goodix_i2c_write(ts->client, GOODIX_REG_CONFIG_DATA, cfg->data,
  310. cfg->size);
  311. if (error) {
  312. dev_err(&ts->client->dev, "Failed to write config data: %d",
  313. error);
  314. return error;
  315. }
  316. dev_dbg(&ts->client->dev, "Config sent successfully.");
  317. /* Let the firmware reconfigure itself, so sleep for 10ms */
  318. usleep_range(10000, 11000);
  319. return 0;
  320. }
  321. static int goodix_int_sync(struct goodix_ts_data *ts)
  322. {
  323. int error;
  324. error = gpiod_direction_output(ts->gpiod_int, 0);
  325. if (error)
  326. return error;
  327. msleep(50); /* T5: 50ms */
  328. error = gpiod_direction_input(ts->gpiod_int);
  329. if (error)
  330. return error;
  331. return 0;
  332. }
  333. /**
  334. * goodix_reset - Reset device during power on
  335. *
  336. * @ts: goodix_ts_data pointer
  337. */
  338. static int goodix_reset(struct goodix_ts_data *ts)
  339. {
  340. int error;
  341. /* begin select I2C slave addr */
  342. error = gpiod_direction_output(ts->gpiod_rst, 0);
  343. if (error)
  344. return error;
  345. msleep(20); /* T2: > 10ms */
  346. /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
  347. error = gpiod_direction_output(ts->gpiod_int, ts->client->addr == 0x14);
  348. if (error)
  349. return error;
  350. usleep_range(100, 2000); /* T3: > 100us */
  351. error = gpiod_direction_output(ts->gpiod_rst, 1);
  352. if (error)
  353. return error;
  354. usleep_range(6000, 10000); /* T4: > 5ms */
  355. /* end select I2C slave addr */
  356. error = gpiod_direction_input(ts->gpiod_rst);
  357. if (error)
  358. return error;
  359. error = goodix_int_sync(ts);
  360. if (error)
  361. return error;
  362. return 0;
  363. }
  364. /**
  365. * goodix_get_gpio_config - Get GPIO config from ACPI/DT
  366. *
  367. * @ts: goodix_ts_data pointer
  368. */
  369. static int goodix_get_gpio_config(struct goodix_ts_data *ts)
  370. {
  371. int error;
  372. struct device *dev;
  373. struct gpio_desc *gpiod;
  374. if (!ts->client)
  375. return -EINVAL;
  376. dev = &ts->client->dev;
  377. /* Get the interrupt GPIO pin number */
  378. gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
  379. if (IS_ERR(gpiod)) {
  380. error = PTR_ERR(gpiod);
  381. if (error != -EPROBE_DEFER)
  382. dev_dbg(dev, "Failed to get %s GPIO: %d\n",
  383. GOODIX_GPIO_INT_NAME, error);
  384. return error;
  385. }
  386. ts->gpiod_int = gpiod;
  387. /* Get the reset line GPIO pin number */
  388. gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN);
  389. if (IS_ERR(gpiod)) {
  390. error = PTR_ERR(gpiod);
  391. if (error != -EPROBE_DEFER)
  392. dev_dbg(dev, "Failed to get %s GPIO: %d\n",
  393. GOODIX_GPIO_RST_NAME, error);
  394. return error;
  395. }
  396. ts->gpiod_rst = gpiod;
  397. return 0;
  398. }
  399. /**
  400. * goodix_read_config - Read the embedded configuration of the panel
  401. *
  402. * @ts: our goodix_ts_data pointer
  403. *
  404. * Must be called during probe
  405. */
  406. static void goodix_read_config(struct goodix_ts_data *ts)
  407. {
  408. u8 config[GOODIX_CONFIG_MAX_LENGTH];
  409. int error;
  410. error = goodix_i2c_read(ts->client, GOODIX_REG_CONFIG_DATA,
  411. config, ts->cfg_len);
  412. if (error) {
  413. dev_warn(&ts->client->dev,
  414. "Error reading config (%d), using defaults\n",
  415. error);
  416. ts->abs_x_max = GOODIX_MAX_WIDTH;
  417. ts->abs_y_max = GOODIX_MAX_HEIGHT;
  418. if (ts->swapped_x_y)
  419. swap(ts->abs_x_max, ts->abs_y_max);
  420. ts->int_trigger_type = GOODIX_INT_TRIGGER;
  421. ts->max_touch_num = GOODIX_MAX_CONTACTS;
  422. return;
  423. }
  424. ts->abs_x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
  425. ts->abs_y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
  426. if (ts->swapped_x_y)
  427. swap(ts->abs_x_max, ts->abs_y_max);
  428. ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
  429. ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
  430. if (!ts->abs_x_max || !ts->abs_y_max || !ts->max_touch_num) {
  431. dev_err(&ts->client->dev,
  432. "Invalid config, using defaults\n");
  433. ts->abs_x_max = GOODIX_MAX_WIDTH;
  434. ts->abs_y_max = GOODIX_MAX_HEIGHT;
  435. if (ts->swapped_x_y)
  436. swap(ts->abs_x_max, ts->abs_y_max);
  437. ts->max_touch_num = GOODIX_MAX_CONTACTS;
  438. }
  439. if (dmi_check_system(rotated_screen)) {
  440. ts->inverted_x = true;
  441. ts->inverted_y = true;
  442. dev_dbg(&ts->client->dev,
  443. "Applying '180 degrees rotated screen' quirk\n");
  444. }
  445. }
  446. /**
  447. * goodix_read_version - Read goodix touchscreen version
  448. *
  449. * @ts: our goodix_ts_data pointer
  450. */
  451. static int goodix_read_version(struct goodix_ts_data *ts)
  452. {
  453. int error;
  454. u8 buf[6];
  455. char id_str[5];
  456. error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
  457. if (error) {
  458. dev_err(&ts->client->dev, "read version failed: %d\n", error);
  459. return error;
  460. }
  461. memcpy(id_str, buf, 4);
  462. id_str[4] = 0;
  463. if (kstrtou16(id_str, 10, &ts->id))
  464. ts->id = 0x1001;
  465. ts->version = get_unaligned_le16(&buf[4]);
  466. dev_info(&ts->client->dev, "ID %d, version: %04x\n", ts->id,
  467. ts->version);
  468. return 0;
  469. }
  470. /**
  471. * goodix_i2c_test - I2C test function to check if the device answers.
  472. *
  473. * @client: the i2c client
  474. */
  475. static int goodix_i2c_test(struct i2c_client *client)
  476. {
  477. int retry = 0;
  478. int error;
  479. u8 test;
  480. while (retry++ < 2) {
  481. error = goodix_i2c_read(client, GOODIX_REG_CONFIG_DATA,
  482. &test, 1);
  483. if (!error)
  484. return 0;
  485. dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
  486. retry, error);
  487. msleep(20);
  488. }
  489. return error;
  490. }
  491. /**
  492. * goodix_request_input_dev - Allocate, populate and register the input device
  493. *
  494. * @ts: our goodix_ts_data pointer
  495. *
  496. * Must be called during probe
  497. */
  498. static int goodix_request_input_dev(struct goodix_ts_data *ts)
  499. {
  500. int error;
  501. ts->input_dev = devm_input_allocate_device(&ts->client->dev);
  502. if (!ts->input_dev) {
  503. dev_err(&ts->client->dev, "Failed to allocate input device.");
  504. return -ENOMEM;
  505. }
  506. input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
  507. 0, ts->abs_x_max, 0, 0);
  508. input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
  509. 0, ts->abs_y_max, 0, 0);
  510. input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
  511. input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  512. input_mt_init_slots(ts->input_dev, ts->max_touch_num,
  513. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  514. ts->input_dev->name = "Goodix Capacitive TouchScreen";
  515. ts->input_dev->phys = "input/ts";
  516. ts->input_dev->id.bustype = BUS_I2C;
  517. ts->input_dev->id.vendor = 0x0416;
  518. ts->input_dev->id.product = ts->id;
  519. ts->input_dev->id.version = ts->version;
  520. error = input_register_device(ts->input_dev);
  521. if (error) {
  522. dev_err(&ts->client->dev,
  523. "Failed to register input device: %d", error);
  524. return error;
  525. }
  526. return 0;
  527. }
  528. /**
  529. * goodix_configure_dev - Finish device initialization
  530. *
  531. * @ts: our goodix_ts_data pointer
  532. *
  533. * Must be called from probe to finish initialization of the device.
  534. * Contains the common initialization code for both devices that
  535. * declare gpio pins and devices that do not. It is either called
  536. * directly from probe or from request_firmware_wait callback.
  537. */
  538. static int goodix_configure_dev(struct goodix_ts_data *ts)
  539. {
  540. int error;
  541. ts->swapped_x_y = device_property_read_bool(&ts->client->dev,
  542. "touchscreen-swapped-x-y");
  543. ts->inverted_x = device_property_read_bool(&ts->client->dev,
  544. "touchscreen-inverted-x");
  545. ts->inverted_y = device_property_read_bool(&ts->client->dev,
  546. "touchscreen-inverted-y");
  547. goodix_read_config(ts);
  548. error = goodix_request_input_dev(ts);
  549. if (error)
  550. return error;
  551. ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
  552. error = goodix_request_irq(ts);
  553. if (error) {
  554. dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
  555. return error;
  556. }
  557. return 0;
  558. }
  559. /**
  560. * goodix_config_cb - Callback to finish device init
  561. *
  562. * @ts: our goodix_ts_data pointer
  563. *
  564. * request_firmware_wait callback that finishes
  565. * initialization of the device.
  566. */
  567. static void goodix_config_cb(const struct firmware *cfg, void *ctx)
  568. {
  569. struct goodix_ts_data *ts = ctx;
  570. int error;
  571. if (cfg) {
  572. /* send device configuration to the firmware */
  573. error = goodix_send_cfg(ts, cfg);
  574. if (error)
  575. goto err_release_cfg;
  576. }
  577. goodix_configure_dev(ts);
  578. err_release_cfg:
  579. release_firmware(cfg);
  580. complete_all(&ts->firmware_loading_complete);
  581. }
  582. static int goodix_ts_probe(struct i2c_client *client,
  583. const struct i2c_device_id *id)
  584. {
  585. struct goodix_ts_data *ts;
  586. int error;
  587. dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
  588. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  589. dev_err(&client->dev, "I2C check functionality failed.\n");
  590. return -ENXIO;
  591. }
  592. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  593. if (!ts)
  594. return -ENOMEM;
  595. ts->client = client;
  596. i2c_set_clientdata(client, ts);
  597. init_completion(&ts->firmware_loading_complete);
  598. error = goodix_get_gpio_config(ts);
  599. if (error)
  600. return error;
  601. if (ts->gpiod_int && ts->gpiod_rst) {
  602. /* reset the controller */
  603. error = goodix_reset(ts);
  604. if (error) {
  605. dev_err(&client->dev, "Controller reset failed.\n");
  606. return error;
  607. }
  608. }
  609. error = goodix_i2c_test(client);
  610. if (error) {
  611. dev_err(&client->dev, "I2C communication failure: %d\n", error);
  612. return error;
  613. }
  614. error = goodix_read_version(ts);
  615. if (error) {
  616. dev_err(&client->dev, "Read version failed.\n");
  617. return error;
  618. }
  619. ts->cfg_len = goodix_get_cfg_len(ts->id);
  620. if (ts->gpiod_int && ts->gpiod_rst) {
  621. /* update device config */
  622. ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL,
  623. "/*(DEBLOBBED)*/", ts->id);
  624. if (!ts->cfg_name)
  625. return -ENOMEM;
  626. error = reject_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
  627. &client->dev, GFP_KERNEL, ts,
  628. goodix_config_cb);
  629. if (error) {
  630. dev_err(&client->dev,
  631. "Failed to invoke firmware loader: %d\n",
  632. error);
  633. return error;
  634. }
  635. return 0;
  636. } else {
  637. error = goodix_configure_dev(ts);
  638. if (error)
  639. return error;
  640. }
  641. return 0;
  642. }
  643. static int goodix_ts_remove(struct i2c_client *client)
  644. {
  645. struct goodix_ts_data *ts = i2c_get_clientdata(client);
  646. if (ts->gpiod_int && ts->gpiod_rst)
  647. wait_for_completion(&ts->firmware_loading_complete);
  648. return 0;
  649. }
  650. static int __maybe_unused goodix_suspend(struct device *dev)
  651. {
  652. struct i2c_client *client = to_i2c_client(dev);
  653. struct goodix_ts_data *ts = i2c_get_clientdata(client);
  654. int error;
  655. /* We need gpio pins to suspend/resume */
  656. if (!ts->gpiod_int || !ts->gpiod_rst) {
  657. disable_irq(client->irq);
  658. return 0;
  659. }
  660. wait_for_completion(&ts->firmware_loading_complete);
  661. /* Free IRQ as IRQ pin is used as output in the suspend sequence */
  662. goodix_free_irq(ts);
  663. /* Output LOW on the INT pin for 5 ms */
  664. error = gpiod_direction_output(ts->gpiod_int, 0);
  665. if (error) {
  666. goodix_request_irq(ts);
  667. return error;
  668. }
  669. usleep_range(5000, 6000);
  670. error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
  671. GOODIX_CMD_SCREEN_OFF);
  672. if (error) {
  673. dev_err(&ts->client->dev, "Screen off command failed\n");
  674. gpiod_direction_input(ts->gpiod_int);
  675. goodix_request_irq(ts);
  676. return -EAGAIN;
  677. }
  678. /*
  679. * The datasheet specifies that the interval between sending screen-off
  680. * command and wake-up should be longer than 58 ms. To avoid waking up
  681. * sooner, delay 58ms here.
  682. */
  683. msleep(58);
  684. return 0;
  685. }
  686. static int __maybe_unused goodix_resume(struct device *dev)
  687. {
  688. struct i2c_client *client = to_i2c_client(dev);
  689. struct goodix_ts_data *ts = i2c_get_clientdata(client);
  690. int error;
  691. if (!ts->gpiod_int || !ts->gpiod_rst) {
  692. enable_irq(client->irq);
  693. return 0;
  694. }
  695. /*
  696. * Exit sleep mode by outputting HIGH level to INT pin
  697. * for 2ms~5ms.
  698. */
  699. error = gpiod_direction_output(ts->gpiod_int, 1);
  700. if (error)
  701. return error;
  702. usleep_range(2000, 5000);
  703. error = goodix_int_sync(ts);
  704. if (error)
  705. return error;
  706. error = goodix_request_irq(ts);
  707. if (error)
  708. return error;
  709. return 0;
  710. }
  711. static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
  712. static const struct i2c_device_id goodix_ts_id[] = {
  713. { "GDIX1001:00", 0 },
  714. { }
  715. };
  716. MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
  717. #ifdef CONFIG_ACPI
  718. static const struct acpi_device_id goodix_acpi_match[] = {
  719. { "GDIX1001", 0 },
  720. { "GDIX1002", 0 },
  721. { }
  722. };
  723. MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
  724. #endif
  725. #ifdef CONFIG_OF
  726. static const struct of_device_id goodix_of_match[] = {
  727. { .compatible = "goodix,gt911" },
  728. { .compatible = "goodix,gt9110" },
  729. { .compatible = "goodix,gt912" },
  730. { .compatible = "goodix,gt927" },
  731. { .compatible = "goodix,gt9271" },
  732. { .compatible = "goodix,gt928" },
  733. { .compatible = "goodix,gt967" },
  734. { }
  735. };
  736. MODULE_DEVICE_TABLE(of, goodix_of_match);
  737. #endif
  738. static struct i2c_driver goodix_ts_driver = {
  739. .probe = goodix_ts_probe,
  740. .remove = goodix_ts_remove,
  741. .id_table = goodix_ts_id,
  742. .driver = {
  743. .name = "Goodix-TS",
  744. .acpi_match_table = ACPI_PTR(goodix_acpi_match),
  745. .of_match_table = of_match_ptr(goodix_of_match),
  746. .pm = &goodix_pm_ops,
  747. },
  748. };
  749. module_i2c_driver(goodix_ts_driver);
  750. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  751. MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
  752. MODULE_DESCRIPTION("Goodix touchscreen driver");
  753. MODULE_LICENSE("GPL v2");