goodix.c 23 KB

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