goodix.c 24 KB

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