goodix.c 23 KB

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