goodix.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Goodix Touchscreens
  4. *
  5. * Copyright (c) 2014 Red Hat Inc.
  6. * Copyright (c) 2015 K. Merker <merker@debian.org>
  7. *
  8. * This code is based on gt9xx.c authored by andrew@goodix.com:
  9. *
  10. * 2010 - 2012 Goodix Technology.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/dmi.h>
  14. #include <linux/firmware.h>
  15. #include <linux/module.h>
  16. #include <linux/delay.h>
  17. #include <linux/irq.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/platform_data/x86/soc.h>
  20. #include <linux/slab.h>
  21. #include <linux/acpi.h>
  22. #include <linux/of.h>
  23. #include <linux/unaligned.h>
  24. #include "goodix.h"
  25. #define GOODIX_GPIO_INT_NAME "irq"
  26. #define GOODIX_GPIO_RST_NAME "reset"
  27. #define GOODIX_MAX_HEIGHT 4096
  28. #define GOODIX_MAX_WIDTH 4096
  29. #define GOODIX_INT_TRIGGER 1
  30. #define GOODIX_CONTACT_SIZE 8
  31. #define GOODIX_MAX_CONTACT_SIZE 9
  32. #define GOODIX_MAX_CONTACTS 10
  33. #define GOODIX_CONFIG_MIN_LENGTH 186
  34. #define GOODIX_CONFIG_911_LENGTH 186
  35. #define GOODIX_CONFIG_967_LENGTH 228
  36. #define GOODIX_CONFIG_GT9X_LENGTH 240
  37. #define GOODIX_BUFFER_STATUS_READY BIT(7)
  38. #define GOODIX_HAVE_KEY BIT(4)
  39. #define GOODIX_BUFFER_STATUS_TIMEOUT 20
  40. #define RESOLUTION_LOC 1
  41. #define MAX_CONTACTS_LOC 5
  42. #define TRIGGER_LOC 6
  43. #define GOODIX_POLL_INTERVAL_MS 17 /* 17ms = 60fps */
  44. /* Our special handling for GPIO accesses through ACPI is x86 specific */
  45. #if defined CONFIG_X86 && defined CONFIG_ACPI
  46. #define ACPI_GPIO_SUPPORT
  47. #endif
  48. struct goodix_chip_id {
  49. const char *id;
  50. const struct goodix_chip_data *data;
  51. };
  52. static int goodix_check_cfg_8(struct goodix_ts_data *ts,
  53. const u8 *cfg, int len);
  54. static int goodix_check_cfg_16(struct goodix_ts_data *ts,
  55. const u8 *cfg, int len);
  56. static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts);
  57. static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts);
  58. static const struct goodix_chip_data gt1x_chip_data = {
  59. .config_addr = GOODIX_GT1X_REG_CONFIG_DATA,
  60. .config_len = GOODIX_CONFIG_GT9X_LENGTH,
  61. .check_config = goodix_check_cfg_16,
  62. .calc_config_checksum = goodix_calc_cfg_checksum_16,
  63. };
  64. static const struct goodix_chip_data gt911_chip_data = {
  65. .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
  66. .config_len = GOODIX_CONFIG_911_LENGTH,
  67. .check_config = goodix_check_cfg_8,
  68. .calc_config_checksum = goodix_calc_cfg_checksum_8,
  69. };
  70. static const struct goodix_chip_data gt967_chip_data = {
  71. .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
  72. .config_len = GOODIX_CONFIG_967_LENGTH,
  73. .check_config = goodix_check_cfg_8,
  74. .calc_config_checksum = goodix_calc_cfg_checksum_8,
  75. };
  76. static const struct goodix_chip_data gt9x_chip_data = {
  77. .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
  78. .config_len = GOODIX_CONFIG_GT9X_LENGTH,
  79. .check_config = goodix_check_cfg_8,
  80. .calc_config_checksum = goodix_calc_cfg_checksum_8,
  81. };
  82. static const struct goodix_chip_id goodix_chip_ids[] = {
  83. { .id = "1151", .data = &gt1x_chip_data },
  84. { .id = "1158", .data = &gt1x_chip_data },
  85. { .id = "5663", .data = &gt1x_chip_data },
  86. { .id = "5688", .data = &gt1x_chip_data },
  87. { .id = "917S", .data = &gt1x_chip_data },
  88. { .id = "9286", .data = &gt1x_chip_data },
  89. { .id = "911", .data = &gt911_chip_data },
  90. { .id = "9271", .data = &gt911_chip_data },
  91. { .id = "9110", .data = &gt911_chip_data },
  92. { .id = "9111", .data = &gt911_chip_data },
  93. { .id = "927", .data = &gt911_chip_data },
  94. { .id = "928", .data = &gt911_chip_data },
  95. { .id = "912", .data = &gt967_chip_data },
  96. { .id = "9147", .data = &gt967_chip_data },
  97. { .id = "967", .data = &gt967_chip_data },
  98. { }
  99. };
  100. static const unsigned long goodix_irq_flags[] = {
  101. IRQ_TYPE_EDGE_RISING,
  102. IRQ_TYPE_EDGE_FALLING,
  103. IRQ_TYPE_LEVEL_LOW,
  104. IRQ_TYPE_LEVEL_HIGH,
  105. };
  106. static const struct dmi_system_id nine_bytes_report[] = {
  107. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  108. {
  109. /* Lenovo Yoga Book X90F / X90L */
  110. .matches = {
  111. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
  112. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "CHERRYVIEW D1 PLATFORM"),
  113. DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "YETI-11"),
  114. }
  115. },
  116. {
  117. /* Lenovo Yoga Book X91F / X91L */
  118. .matches = {
  119. /* Non exact match to match F + L versions */
  120. DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X91"),
  121. }
  122. },
  123. #endif
  124. {}
  125. };
  126. /*
  127. * Those tablets have their x coordinate inverted
  128. */
  129. static const struct dmi_system_id inverted_x_screen[] = {
  130. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  131. {
  132. .ident = "Cube I15-TC",
  133. .matches = {
  134. DMI_MATCH(DMI_SYS_VENDOR, "Cube"),
  135. DMI_MATCH(DMI_PRODUCT_NAME, "I15-TC")
  136. },
  137. },
  138. #endif
  139. {}
  140. };
  141. /**
  142. * goodix_i2c_read - read data from a register of the i2c slave device.
  143. *
  144. * @client: i2c device.
  145. * @reg: the register to read from.
  146. * @buf: raw write data buffer.
  147. * @len: length of the buffer to write
  148. */
  149. int goodix_i2c_read(struct i2c_client *client, u16 reg, u8 *buf, int len)
  150. {
  151. struct i2c_msg msgs[2];
  152. __be16 wbuf = cpu_to_be16(reg);
  153. int ret;
  154. msgs[0].flags = 0;
  155. msgs[0].addr = client->addr;
  156. msgs[0].len = 2;
  157. msgs[0].buf = (u8 *)&wbuf;
  158. msgs[1].flags = I2C_M_RD;
  159. msgs[1].addr = client->addr;
  160. msgs[1].len = len;
  161. msgs[1].buf = buf;
  162. ret = i2c_transfer(client->adapter, msgs, 2);
  163. if (ret >= 0)
  164. ret = (ret == ARRAY_SIZE(msgs) ? 0 : -EIO);
  165. if (ret)
  166. dev_err(&client->dev, "Error reading %d bytes from 0x%04x: %d\n",
  167. len, reg, ret);
  168. return ret;
  169. }
  170. /**
  171. * goodix_i2c_write - write data to a register of the i2c slave device.
  172. *
  173. * @client: i2c device.
  174. * @reg: the register to write to.
  175. * @buf: raw data buffer to write.
  176. * @len: length of the buffer to write
  177. */
  178. int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf, int len)
  179. {
  180. u8 *addr_buf;
  181. struct i2c_msg msg;
  182. int ret;
  183. addr_buf = kmalloc(len + 2, GFP_KERNEL);
  184. if (!addr_buf)
  185. return -ENOMEM;
  186. addr_buf[0] = reg >> 8;
  187. addr_buf[1] = reg & 0xFF;
  188. memcpy(&addr_buf[2], buf, len);
  189. msg.flags = 0;
  190. msg.addr = client->addr;
  191. msg.buf = addr_buf;
  192. msg.len = len + 2;
  193. ret = i2c_transfer(client->adapter, &msg, 1);
  194. if (ret >= 0)
  195. ret = (ret == 1 ? 0 : -EIO);
  196. kfree(addr_buf);
  197. if (ret)
  198. dev_err(&client->dev, "Error writing %d bytes to 0x%04x: %d\n",
  199. len, reg, ret);
  200. return ret;
  201. }
  202. 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(const char *id)
  207. {
  208. unsigned int i;
  209. for (i = 0; goodix_chip_ids[i].id; i++) {
  210. if (!strcmp(goodix_chip_ids[i].id, id))
  211. return goodix_chip_ids[i].data;
  212. }
  213. return &gt9x_chip_data;
  214. }
  215. static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
  216. {
  217. unsigned long max_timeout;
  218. int touch_num;
  219. int error;
  220. u16 addr = GOODIX_READ_COOR_ADDR;
  221. /*
  222. * We are going to read 1-byte header,
  223. * ts->contact_size * max(1, touch_num) bytes of coordinates
  224. * and 1-byte footer which contains the touch-key code.
  225. */
  226. const int header_contact_keycode_size = 1 + ts->contact_size + 1;
  227. /*
  228. * The 'buffer status' bit, which indicates that the data is valid, is
  229. * not set as soon as the interrupt is raised, but slightly after.
  230. * This takes around 10 ms to happen, so we poll for 20 ms.
  231. */
  232. max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT);
  233. do {
  234. error = goodix_i2c_read(ts->client, addr, data,
  235. header_contact_keycode_size);
  236. if (error)
  237. return error;
  238. if (data[0] & GOODIX_BUFFER_STATUS_READY) {
  239. touch_num = data[0] & 0x0f;
  240. if (touch_num > ts->max_touch_num)
  241. return -EPROTO;
  242. if (touch_num > 1) {
  243. addr += header_contact_keycode_size;
  244. data += header_contact_keycode_size;
  245. error = goodix_i2c_read(ts->client,
  246. addr, data,
  247. ts->contact_size *
  248. (touch_num - 1));
  249. if (error)
  250. return error;
  251. }
  252. return touch_num;
  253. }
  254. if (data[0] == 0 && ts->firmware_name) {
  255. if (goodix_handle_fw_request(ts))
  256. return 0;
  257. }
  258. usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
  259. } while (time_before(jiffies, max_timeout));
  260. /*
  261. * The Goodix panel will send spurious interrupts after a
  262. * 'finger up' event, which will always cause a timeout.
  263. */
  264. return -ENOMSG;
  265. }
  266. static int goodix_create_pen_input(struct goodix_ts_data *ts)
  267. {
  268. struct device *dev = &ts->client->dev;
  269. struct input_dev *input;
  270. input = devm_input_allocate_device(dev);
  271. if (!input)
  272. return -ENOMEM;
  273. input_copy_abs(input, ABS_X, ts->input_dev, ABS_MT_POSITION_X);
  274. input_copy_abs(input, ABS_Y, ts->input_dev, ABS_MT_POSITION_Y);
  275. /*
  276. * The resolution of these touchscreens is about 10 units/mm, the actual
  277. * resolution does not matter much since we set INPUT_PROP_DIRECT.
  278. * Userspace wants something here though, so just set it to 10 units/mm.
  279. */
  280. input_abs_set_res(input, ABS_X, 10);
  281. input_abs_set_res(input, ABS_Y, 10);
  282. input_set_abs_params(input, ABS_PRESSURE, 0, 255, 0, 0);
  283. input_set_capability(input, EV_KEY, BTN_TOUCH);
  284. input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
  285. input_set_capability(input, EV_KEY, BTN_STYLUS);
  286. input_set_capability(input, EV_KEY, BTN_STYLUS2);
  287. __set_bit(INPUT_PROP_DIRECT, input->propbit);
  288. input->name = "Goodix Active Pen";
  289. input->phys = "input/pen";
  290. input->id.bustype = BUS_I2C;
  291. input->id.vendor = 0x0416;
  292. if (kstrtou16(ts->id, 10, &input->id.product))
  293. input->id.product = 0x1001;
  294. input->id.version = ts->version;
  295. ts->input_pen = input;
  296. return 0;
  297. }
  298. static void goodix_ts_report_pen_down(struct goodix_ts_data *ts, u8 *data)
  299. {
  300. int input_x, input_y, input_w, error;
  301. u8 key_value;
  302. if (!ts->pen_input_registered) {
  303. error = input_register_device(ts->input_pen);
  304. ts->pen_input_registered = (error == 0) ? 1 : error;
  305. }
  306. if (ts->pen_input_registered < 0)
  307. return;
  308. if (ts->contact_size == 9) {
  309. input_x = get_unaligned_le16(&data[4]);
  310. input_y = get_unaligned_le16(&data[6]);
  311. input_w = get_unaligned_le16(&data[8]);
  312. } else {
  313. input_x = get_unaligned_le16(&data[2]);
  314. input_y = get_unaligned_le16(&data[4]);
  315. input_w = get_unaligned_le16(&data[6]);
  316. }
  317. touchscreen_report_pos(ts->input_pen, &ts->prop, input_x, input_y, false);
  318. input_report_abs(ts->input_pen, ABS_PRESSURE, input_w);
  319. input_report_key(ts->input_pen, BTN_TOUCH, 1);
  320. input_report_key(ts->input_pen, BTN_TOOL_PEN, 1);
  321. if (data[0] & GOODIX_HAVE_KEY) {
  322. key_value = data[1 + ts->contact_size];
  323. input_report_key(ts->input_pen, BTN_STYLUS, key_value & 0x10);
  324. input_report_key(ts->input_pen, BTN_STYLUS2, key_value & 0x20);
  325. } else {
  326. input_report_key(ts->input_pen, BTN_STYLUS, 0);
  327. input_report_key(ts->input_pen, BTN_STYLUS2, 0);
  328. }
  329. input_sync(ts->input_pen);
  330. }
  331. static void goodix_ts_report_pen_up(struct goodix_ts_data *ts)
  332. {
  333. if (!ts->input_pen)
  334. return;
  335. input_report_key(ts->input_pen, BTN_TOUCH, 0);
  336. input_report_key(ts->input_pen, BTN_TOOL_PEN, 0);
  337. input_report_key(ts->input_pen, BTN_STYLUS, 0);
  338. input_report_key(ts->input_pen, BTN_STYLUS2, 0);
  339. input_sync(ts->input_pen);
  340. }
  341. static void goodix_ts_report_touch_8b(struct goodix_ts_data *ts, u8 *coor_data)
  342. {
  343. int id = coor_data[0] & 0x0F;
  344. int input_x = get_unaligned_le16(&coor_data[1]);
  345. int input_y = get_unaligned_le16(&coor_data[3]);
  346. int input_w = get_unaligned_le16(&coor_data[5]);
  347. input_mt_slot(ts->input_dev, id);
  348. input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
  349. touchscreen_report_pos(ts->input_dev, &ts->prop,
  350. input_x, input_y, true);
  351. input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
  352. input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
  353. }
  354. static void goodix_ts_report_touch_9b(struct goodix_ts_data *ts, u8 *coor_data)
  355. {
  356. int id = coor_data[1] & 0x0F;
  357. int input_x = get_unaligned_le16(&coor_data[3]);
  358. int input_y = get_unaligned_le16(&coor_data[5]);
  359. int input_w = get_unaligned_le16(&coor_data[7]);
  360. input_mt_slot(ts->input_dev, id);
  361. input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
  362. touchscreen_report_pos(ts->input_dev, &ts->prop,
  363. input_x, input_y, true);
  364. input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
  365. input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
  366. }
  367. static void goodix_ts_release_keys(struct goodix_ts_data *ts)
  368. {
  369. int i;
  370. for (i = 0; i < GOODIX_MAX_KEYS; i++)
  371. input_report_key(ts->input_dev, ts->keymap[i], 0);
  372. }
  373. static void goodix_ts_report_key(struct goodix_ts_data *ts, u8 *data)
  374. {
  375. int touch_num;
  376. u8 key_value;
  377. int i;
  378. if (data[0] & GOODIX_HAVE_KEY) {
  379. touch_num = data[0] & 0x0f;
  380. key_value = data[1 + ts->contact_size * touch_num];
  381. for (i = 0; i < GOODIX_MAX_KEYS; i++)
  382. if (key_value & BIT(i))
  383. input_report_key(ts->input_dev,
  384. ts->keymap[i], 1);
  385. } else {
  386. goodix_ts_release_keys(ts);
  387. }
  388. }
  389. /**
  390. * goodix_process_events - Process incoming events
  391. *
  392. * @ts: our goodix_ts_data pointer
  393. *
  394. * Called when the IRQ is triggered. Read the current device state, and push
  395. * the input events to the user space.
  396. */
  397. static void goodix_process_events(struct goodix_ts_data *ts)
  398. {
  399. u8 point_data[2 + GOODIX_MAX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
  400. int touch_num;
  401. int i;
  402. touch_num = goodix_ts_read_input_report(ts, point_data);
  403. if (touch_num < 0)
  404. return;
  405. /* The pen being down is always reported as a single touch */
  406. if (touch_num == 1 && (point_data[1] & 0x80)) {
  407. goodix_ts_report_pen_down(ts, point_data);
  408. goodix_ts_release_keys(ts);
  409. goto sync; /* Release any previously registered touches */
  410. } else {
  411. goodix_ts_report_pen_up(ts);
  412. }
  413. goodix_ts_report_key(ts, point_data);
  414. for (i = 0; i < touch_num; i++)
  415. if (ts->contact_size == 9)
  416. goodix_ts_report_touch_9b(ts,
  417. &point_data[1 + ts->contact_size * i]);
  418. else
  419. goodix_ts_report_touch_8b(ts,
  420. &point_data[1 + ts->contact_size * i]);
  421. sync:
  422. input_mt_sync_frame(ts->input_dev);
  423. input_sync(ts->input_dev);
  424. }
  425. static void goodix_ts_work_i2c_poll(struct input_dev *input)
  426. {
  427. struct goodix_ts_data *ts = input_get_drvdata(input);
  428. goodix_process_events(ts);
  429. goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0);
  430. }
  431. /**
  432. * goodix_ts_irq_handler - The IRQ handler
  433. *
  434. * @irq: interrupt number.
  435. * @dev_id: private data pointer.
  436. */
  437. static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
  438. {
  439. struct goodix_ts_data *ts = dev_id;
  440. goodix_process_events(ts);
  441. goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0);
  442. return IRQ_HANDLED;
  443. }
  444. static void goodix_enable_irq(struct goodix_ts_data *ts)
  445. {
  446. if (ts->client->irq)
  447. enable_irq(ts->client->irq);
  448. }
  449. static void goodix_disable_irq(struct goodix_ts_data *ts)
  450. {
  451. if (ts->client->irq)
  452. disable_irq(ts->client->irq);
  453. }
  454. static void goodix_free_irq(struct goodix_ts_data *ts)
  455. {
  456. if (ts->client->irq)
  457. devm_free_irq(&ts->client->dev, ts->client->irq, ts);
  458. }
  459. static int goodix_request_irq(struct goodix_ts_data *ts)
  460. {
  461. if (!ts->client->irq)
  462. return 0;
  463. return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
  464. NULL, goodix_ts_irq_handler,
  465. ts->irq_flags, ts->client->name, ts);
  466. }
  467. static int goodix_check_cfg_8(struct goodix_ts_data *ts, const u8 *cfg, int len)
  468. {
  469. int i, raw_cfg_len = len - 2;
  470. u8 check_sum = 0;
  471. for (i = 0; i < raw_cfg_len; i++)
  472. check_sum += cfg[i];
  473. check_sum = (~check_sum) + 1;
  474. if (check_sum != cfg[raw_cfg_len]) {
  475. dev_err(&ts->client->dev,
  476. "The checksum of the config fw is not correct");
  477. return -EINVAL;
  478. }
  479. if (cfg[raw_cfg_len + 1] != 1) {
  480. dev_err(&ts->client->dev,
  481. "Config fw must have Config_Fresh register set");
  482. return -EINVAL;
  483. }
  484. return 0;
  485. }
  486. static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts)
  487. {
  488. int i, raw_cfg_len = ts->chip->config_len - 2;
  489. u8 check_sum = 0;
  490. for (i = 0; i < raw_cfg_len; i++)
  491. check_sum += ts->config[i];
  492. check_sum = (~check_sum) + 1;
  493. ts->config[raw_cfg_len] = check_sum;
  494. ts->config[raw_cfg_len + 1] = 1; /* Set "config_fresh" bit */
  495. }
  496. static int goodix_check_cfg_16(struct goodix_ts_data *ts, const u8 *cfg,
  497. int len)
  498. {
  499. int i, raw_cfg_len = len - 3;
  500. u16 check_sum = 0;
  501. for (i = 0; i < raw_cfg_len; i += 2)
  502. check_sum += get_unaligned_be16(&cfg[i]);
  503. check_sum = (~check_sum) + 1;
  504. if (check_sum != get_unaligned_be16(&cfg[raw_cfg_len])) {
  505. dev_err(&ts->client->dev,
  506. "The checksum of the config fw is not correct");
  507. return -EINVAL;
  508. }
  509. if (cfg[raw_cfg_len + 2] != 1) {
  510. dev_err(&ts->client->dev,
  511. "Config fw must have Config_Fresh register set");
  512. return -EINVAL;
  513. }
  514. return 0;
  515. }
  516. static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts)
  517. {
  518. int i, raw_cfg_len = ts->chip->config_len - 3;
  519. u16 check_sum = 0;
  520. for (i = 0; i < raw_cfg_len; i += 2)
  521. check_sum += get_unaligned_be16(&ts->config[i]);
  522. check_sum = (~check_sum) + 1;
  523. put_unaligned_be16(check_sum, &ts->config[raw_cfg_len]);
  524. ts->config[raw_cfg_len + 2] = 1; /* Set "config_fresh" bit */
  525. }
  526. /**
  527. * goodix_check_cfg - Checks if config fw is valid
  528. *
  529. * @ts: goodix_ts_data pointer
  530. * @cfg: firmware config data
  531. * @len: config data length
  532. */
  533. static int goodix_check_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
  534. {
  535. if (len < GOODIX_CONFIG_MIN_LENGTH ||
  536. len > GOODIX_CONFIG_MAX_LENGTH) {
  537. dev_err(&ts->client->dev,
  538. "The length of the config fw is not correct");
  539. return -EINVAL;
  540. }
  541. return ts->chip->check_config(ts, cfg, len);
  542. }
  543. /**
  544. * goodix_send_cfg - Write fw config to device
  545. *
  546. * @ts: goodix_ts_data pointer
  547. * @cfg: config firmware to write to device
  548. * @len: config data length
  549. */
  550. int goodix_send_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
  551. {
  552. int error;
  553. error = goodix_check_cfg(ts, cfg, len);
  554. if (error)
  555. return error;
  556. error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg, len);
  557. if (error)
  558. return error;
  559. dev_dbg(&ts->client->dev, "Config sent successfully.");
  560. /* Let the firmware reconfigure itself, so sleep for 10ms */
  561. usleep_range(10000, 11000);
  562. return 0;
  563. }
  564. #ifdef ACPI_GPIO_SUPPORT
  565. static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
  566. {
  567. acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
  568. acpi_status status;
  569. status = acpi_evaluate_object(handle, "INTI", NULL, NULL);
  570. return ACPI_SUCCESS(status) ? 0 : -EIO;
  571. }
  572. static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
  573. {
  574. acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
  575. acpi_status status;
  576. status = acpi_execute_simple_method(handle, "INTO", value);
  577. return ACPI_SUCCESS(status) ? 0 : -EIO;
  578. }
  579. #else
  580. static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
  581. {
  582. dev_err(&ts->client->dev,
  583. "%s called on device without ACPI support\n", __func__);
  584. return -EINVAL;
  585. }
  586. static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
  587. {
  588. dev_err(&ts->client->dev,
  589. "%s called on device without ACPI support\n", __func__);
  590. return -EINVAL;
  591. }
  592. #endif
  593. static int goodix_irq_direction_output(struct goodix_ts_data *ts, int value)
  594. {
  595. switch (ts->irq_pin_access_method) {
  596. case IRQ_PIN_ACCESS_NONE:
  597. dev_err(&ts->client->dev,
  598. "%s called without an irq_pin_access_method set\n",
  599. __func__);
  600. return -EINVAL;
  601. case IRQ_PIN_ACCESS_GPIO:
  602. return gpiod_direction_output(ts->gpiod_int, value);
  603. case IRQ_PIN_ACCESS_ACPI_GPIO:
  604. /*
  605. * The IRQ pin triggers on a falling edge, so its gets marked
  606. * as active-low, use output_raw to avoid the value inversion.
  607. */
  608. return gpiod_direction_output_raw(ts->gpiod_int, value);
  609. case IRQ_PIN_ACCESS_ACPI_METHOD:
  610. return goodix_pin_acpi_output_method(ts, value);
  611. }
  612. return -EINVAL; /* Never reached */
  613. }
  614. static int goodix_irq_direction_input(struct goodix_ts_data *ts)
  615. {
  616. switch (ts->irq_pin_access_method) {
  617. case IRQ_PIN_ACCESS_NONE:
  618. dev_err(&ts->client->dev,
  619. "%s called without an irq_pin_access_method set\n",
  620. __func__);
  621. return -EINVAL;
  622. case IRQ_PIN_ACCESS_GPIO:
  623. return gpiod_direction_input(ts->gpiod_int);
  624. case IRQ_PIN_ACCESS_ACPI_GPIO:
  625. return gpiod_direction_input(ts->gpiod_int);
  626. case IRQ_PIN_ACCESS_ACPI_METHOD:
  627. return goodix_pin_acpi_direction_input(ts);
  628. }
  629. return -EINVAL; /* Never reached */
  630. }
  631. int goodix_int_sync(struct goodix_ts_data *ts)
  632. {
  633. int error;
  634. error = goodix_irq_direction_output(ts, 0);
  635. if (error)
  636. goto error;
  637. msleep(50); /* T5: 50ms */
  638. error = goodix_irq_direction_input(ts);
  639. if (error)
  640. goto error;
  641. return 0;
  642. error:
  643. dev_err(&ts->client->dev, "Controller irq sync failed.\n");
  644. return error;
  645. }
  646. /**
  647. * goodix_reset_no_int_sync - Reset device, leaving interrupt line in output mode
  648. *
  649. * @ts: goodix_ts_data pointer
  650. */
  651. int goodix_reset_no_int_sync(struct goodix_ts_data *ts)
  652. {
  653. int error;
  654. /* begin select I2C slave addr */
  655. error = gpiod_direction_output(ts->gpiod_rst, 0);
  656. if (error)
  657. goto error;
  658. msleep(20); /* T2: > 10ms */
  659. /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
  660. error = goodix_irq_direction_output(ts, ts->client->addr == 0x14);
  661. if (error)
  662. goto error;
  663. usleep_range(100, 2000); /* T3: > 100us */
  664. error = gpiod_direction_output(ts->gpiod_rst, 1);
  665. if (error)
  666. goto error;
  667. usleep_range(6000, 10000); /* T4: > 5ms */
  668. return 0;
  669. error:
  670. dev_err(&ts->client->dev, "Controller reset failed.\n");
  671. return error;
  672. }
  673. /**
  674. * goodix_reset - Reset device during power on
  675. *
  676. * @ts: goodix_ts_data pointer
  677. */
  678. static int goodix_reset(struct goodix_ts_data *ts)
  679. {
  680. int error;
  681. error = goodix_reset_no_int_sync(ts);
  682. if (error)
  683. return error;
  684. return goodix_int_sync(ts);
  685. }
  686. #ifdef ACPI_GPIO_SUPPORT
  687. static const struct acpi_gpio_params first_gpio = { 0, 0, false };
  688. static const struct acpi_gpio_params second_gpio = { 1, 0, false };
  689. static const struct acpi_gpio_mapping acpi_goodix_int_first_gpios[] = {
  690. { GOODIX_GPIO_INT_NAME "-gpios", &first_gpio, 1 },
  691. { GOODIX_GPIO_RST_NAME "-gpios", &second_gpio, 1 },
  692. { },
  693. };
  694. static const struct acpi_gpio_mapping acpi_goodix_int_last_gpios[] = {
  695. { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
  696. { GOODIX_GPIO_INT_NAME "-gpios", &second_gpio, 1 },
  697. { },
  698. };
  699. static const struct acpi_gpio_mapping acpi_goodix_reset_only_gpios[] = {
  700. { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
  701. { },
  702. };
  703. static int goodix_resource(struct acpi_resource *ares, void *data)
  704. {
  705. struct goodix_ts_data *ts = data;
  706. struct device *dev = &ts->client->dev;
  707. struct acpi_resource_gpio *gpio;
  708. if (acpi_gpio_get_irq_resource(ares, &gpio)) {
  709. if (ts->gpio_int_idx == -1) {
  710. ts->gpio_int_idx = ts->gpio_count;
  711. } else {
  712. dev_err(dev, "More then one GpioInt resource, ignoring ACPI GPIO resources\n");
  713. ts->gpio_int_idx = -2;
  714. }
  715. ts->gpio_count++;
  716. } else if (acpi_gpio_get_io_resource(ares, &gpio))
  717. ts->gpio_count++;
  718. return 0;
  719. }
  720. /*
  721. * This function gets called in case we fail to get the irq GPIO directly
  722. * because the ACPI tables lack GPIO-name to APCI _CRS index mappings
  723. * (no _DSD UUID daffd814-6eba-4d8c-8a91-bc9bbf4aa301 data).
  724. * In that case we add our own mapping and then goodix_get_gpio_config()
  725. * retries to get the GPIOs based on the added mapping.
  726. */
  727. static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
  728. {
  729. const struct acpi_gpio_mapping *gpio_mapping = NULL;
  730. struct device *dev = &ts->client->dev;
  731. LIST_HEAD(resources);
  732. int irq, ret;
  733. ts->gpio_count = 0;
  734. ts->gpio_int_idx = -1;
  735. ret = acpi_dev_get_resources(ACPI_COMPANION(dev), &resources,
  736. goodix_resource, ts);
  737. if (ret < 0) {
  738. dev_err(dev, "Error getting ACPI resources: %d\n", ret);
  739. return ret;
  740. }
  741. acpi_dev_free_resource_list(&resources);
  742. /*
  743. * CHT devices should have a GpioInt + a regular GPIO ACPI resource.
  744. * Some CHT devices have a bug (where the also is bogus Interrupt
  745. * resource copied from a previous BYT based generation). i2c-core-acpi
  746. * will use the non-working Interrupt resource, fix this up.
  747. */
  748. if (soc_intel_is_cht() && ts->gpio_count == 2 && ts->gpio_int_idx != -1) {
  749. irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
  750. if (irq > 0 && irq != ts->client->irq) {
  751. dev_warn(dev, "Overriding IRQ %d -> %d\n", ts->client->irq, irq);
  752. ts->client->irq = irq;
  753. }
  754. }
  755. /* Some devices with gpio_int_idx 0 list a third unused GPIO */
  756. if ((ts->gpio_count == 2 || ts->gpio_count == 3) && ts->gpio_int_idx == 0) {
  757. ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
  758. gpio_mapping = acpi_goodix_int_first_gpios;
  759. } else if (ts->gpio_count == 2 && ts->gpio_int_idx == 1) {
  760. ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
  761. gpio_mapping = acpi_goodix_int_last_gpios;
  762. } else if (ts->gpio_count == 1 && ts->gpio_int_idx == -1 &&
  763. acpi_has_method(ACPI_HANDLE(dev), "INTI") &&
  764. acpi_has_method(ACPI_HANDLE(dev), "INTO")) {
  765. dev_info(dev, "Using ACPI INTI and INTO methods for IRQ pin access\n");
  766. ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_METHOD;
  767. gpio_mapping = acpi_goodix_reset_only_gpios;
  768. } else if (soc_intel_is_byt() && ts->gpio_count == 2 && ts->gpio_int_idx == -1) {
  769. dev_info(dev, "No ACPI GpioInt resource, assuming that the GPIO order is reset, int\n");
  770. ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
  771. gpio_mapping = acpi_goodix_int_last_gpios;
  772. } else if (ts->gpio_count == 1 && ts->gpio_int_idx == 0) {
  773. /*
  774. * On newer devices there is only 1 GpioInt resource and _PS0
  775. * does the whole reset sequence for us.
  776. */
  777. acpi_device_fix_up_power(ACPI_COMPANION(dev));
  778. /*
  779. * Before the _PS0 call the int GPIO may have been in output
  780. * mode and the call should have put the int GPIO in input mode,
  781. * but the GPIO subsys cached state may still think it is
  782. * in output mode, causing gpiochip_lock_as_irq() failure.
  783. *
  784. * Add a mapping for the int GPIO to make the
  785. * gpiod_int = gpiod_get(..., GPIOD_IN) call succeed,
  786. * which will explicitly set the direction to input.
  787. */
  788. ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
  789. gpio_mapping = acpi_goodix_int_first_gpios;
  790. } else {
  791. dev_warn(dev, "Unexpected ACPI resources: gpio_count %d, gpio_int_idx %d\n",
  792. ts->gpio_count, ts->gpio_int_idx);
  793. /*
  794. * On some devices _PS0 does a reset for us and
  795. * sometimes this is necessary for things to work.
  796. */
  797. acpi_device_fix_up_power(ACPI_COMPANION(dev));
  798. return -EINVAL;
  799. }
  800. return devm_acpi_dev_add_driver_gpios(dev, gpio_mapping);
  801. }
  802. #else
  803. static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
  804. {
  805. return -EINVAL;
  806. }
  807. #endif /* CONFIG_X86 && CONFIG_ACPI */
  808. /**
  809. * goodix_get_gpio_config - Get GPIO config from ACPI/DT
  810. *
  811. * @ts: goodix_ts_data pointer
  812. */
  813. static int goodix_get_gpio_config(struct goodix_ts_data *ts)
  814. {
  815. struct device *dev;
  816. struct gpio_desc *gpiod;
  817. bool added_acpi_mappings = false;
  818. if (!ts->client)
  819. return -EINVAL;
  820. dev = &ts->client->dev;
  821. ts->avdd28 = devm_regulator_get(dev, "AVDD28");
  822. if (IS_ERR(ts->avdd28))
  823. return dev_err_probe(dev, PTR_ERR(ts->avdd28), "Failed to get AVDD28 regulator\n");
  824. ts->vddio = devm_regulator_get(dev, "VDDIO");
  825. if (IS_ERR(ts->vddio))
  826. return dev_err_probe(dev, PTR_ERR(ts->vddio), "Failed to get VDDIO regulator\n");
  827. retry_get_irq_gpio:
  828. /* Get the interrupt GPIO pin number */
  829. gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
  830. if (IS_ERR(gpiod))
  831. return dev_err_probe(dev, PTR_ERR(gpiod), "Failed to get %s GPIO\n",
  832. GOODIX_GPIO_INT_NAME);
  833. if (!gpiod && has_acpi_companion(dev) && !added_acpi_mappings) {
  834. added_acpi_mappings = true;
  835. if (goodix_add_acpi_gpio_mappings(ts) == 0)
  836. goto retry_get_irq_gpio;
  837. }
  838. ts->gpiod_int = gpiod;
  839. /* Get the reset line GPIO pin number */
  840. gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_ASIS);
  841. if (IS_ERR(gpiod))
  842. return dev_err_probe(dev, PTR_ERR(gpiod), "Failed to get %s GPIO\n",
  843. GOODIX_GPIO_RST_NAME);
  844. ts->gpiod_rst = gpiod;
  845. switch (ts->irq_pin_access_method) {
  846. case IRQ_PIN_ACCESS_ACPI_GPIO:
  847. /*
  848. * We end up here if goodix_add_acpi_gpio_mappings() has
  849. * called devm_acpi_dev_add_driver_gpios() because the ACPI
  850. * tables did not contain name to index mappings.
  851. * Check that we successfully got both GPIOs after we've
  852. * added our own acpi_gpio_mapping and if we did not get both
  853. * GPIOs reset irq_pin_access_method to IRQ_PIN_ACCESS_NONE.
  854. */
  855. if (!ts->gpiod_int || !ts->gpiod_rst)
  856. ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
  857. break;
  858. case IRQ_PIN_ACCESS_ACPI_METHOD:
  859. if (!ts->gpiod_rst)
  860. ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
  861. break;
  862. default:
  863. if (ts->gpiod_int && ts->gpiod_rst) {
  864. ts->reset_controller_at_probe = true;
  865. ts->load_cfg_from_disk = true;
  866. ts->irq_pin_access_method = IRQ_PIN_ACCESS_GPIO;
  867. }
  868. }
  869. return 0;
  870. }
  871. /**
  872. * goodix_read_config - Read the embedded configuration of the panel
  873. *
  874. * @ts: our goodix_ts_data pointer
  875. *
  876. * Must be called during probe
  877. */
  878. static void goodix_read_config(struct goodix_ts_data *ts)
  879. {
  880. int x_max, y_max;
  881. int error;
  882. /*
  883. * On controllers where we need to upload the firmware
  884. * (controllers without flash) ts->config already has the config
  885. * at this point and the controller itself does not have it yet!
  886. */
  887. if (!ts->firmware_name) {
  888. error = goodix_i2c_read(ts->client, ts->chip->config_addr,
  889. ts->config, ts->chip->config_len);
  890. if (error) {
  891. ts->int_trigger_type = GOODIX_INT_TRIGGER;
  892. ts->max_touch_num = GOODIX_MAX_CONTACTS;
  893. return;
  894. }
  895. }
  896. ts->int_trigger_type = ts->config[TRIGGER_LOC] & 0x03;
  897. ts->max_touch_num = ts->config[MAX_CONTACTS_LOC] & 0x0f;
  898. x_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC]);
  899. y_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC + 2]);
  900. if (x_max && y_max) {
  901. input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
  902. input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
  903. }
  904. ts->chip->calc_config_checksum(ts);
  905. }
  906. /**
  907. * goodix_read_version - Read goodix touchscreen version
  908. *
  909. * @ts: our goodix_ts_data pointer
  910. */
  911. static int goodix_read_version(struct goodix_ts_data *ts)
  912. {
  913. int error;
  914. u8 buf[6];
  915. char id_str[GOODIX_ID_MAX_LEN + 1];
  916. error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
  917. if (error)
  918. return error;
  919. memcpy(id_str, buf, GOODIX_ID_MAX_LEN);
  920. id_str[GOODIX_ID_MAX_LEN] = 0;
  921. strscpy(ts->id, id_str, GOODIX_ID_MAX_LEN + 1);
  922. ts->version = get_unaligned_le16(&buf[4]);
  923. dev_info(&ts->client->dev, "ID %s, version: %04x\n", ts->id,
  924. ts->version);
  925. return 0;
  926. }
  927. /**
  928. * goodix_i2c_test - I2C test function to check if the device answers.
  929. *
  930. * @client: the i2c client
  931. */
  932. static int goodix_i2c_test(struct i2c_client *client)
  933. {
  934. int retry = 0;
  935. int error;
  936. u8 test;
  937. while (retry++ < 2) {
  938. error = goodix_i2c_read(client, GOODIX_REG_ID, &test, 1);
  939. if (!error)
  940. return 0;
  941. msleep(20);
  942. }
  943. return error;
  944. }
  945. /**
  946. * goodix_configure_dev - Finish device initialization
  947. *
  948. * @ts: our goodix_ts_data pointer
  949. *
  950. * Must be called from probe to finish initialization of the device.
  951. * Contains the common initialization code for both devices that
  952. * declare gpio pins and devices that do not. It is either called
  953. * directly from probe or from request_firmware_wait callback.
  954. */
  955. static int goodix_configure_dev(struct goodix_ts_data *ts)
  956. {
  957. int error;
  958. int i;
  959. ts->int_trigger_type = GOODIX_INT_TRIGGER;
  960. ts->max_touch_num = GOODIX_MAX_CONTACTS;
  961. ts->input_dev = devm_input_allocate_device(&ts->client->dev);
  962. if (!ts->input_dev) {
  963. dev_err(&ts->client->dev, "Failed to allocate input device.");
  964. return -ENOMEM;
  965. }
  966. ts->input_dev->name = "Goodix Capacitive TouchScreen";
  967. ts->input_dev->phys = "input/ts";
  968. ts->input_dev->id.bustype = BUS_I2C;
  969. ts->input_dev->id.vendor = 0x0416;
  970. if (kstrtou16(ts->id, 10, &ts->input_dev->id.product))
  971. ts->input_dev->id.product = 0x1001;
  972. ts->input_dev->id.version = ts->version;
  973. ts->input_dev->keycode = ts->keymap;
  974. ts->input_dev->keycodesize = sizeof(ts->keymap[0]);
  975. ts->input_dev->keycodemax = GOODIX_MAX_KEYS;
  976. /* Capacitive Windows/Home button on some devices */
  977. for (i = 0; i < GOODIX_MAX_KEYS; ++i) {
  978. if (i == 0)
  979. ts->keymap[i] = KEY_LEFTMETA;
  980. else
  981. ts->keymap[i] = KEY_F1 + (i - 1);
  982. input_set_capability(ts->input_dev, EV_KEY, ts->keymap[i]);
  983. }
  984. input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
  985. input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
  986. input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
  987. input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  988. retry_read_config:
  989. /* Read configuration and apply touchscreen parameters */
  990. goodix_read_config(ts);
  991. /* Try overriding touchscreen parameters via device properties */
  992. touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
  993. if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
  994. if (!ts->reset_controller_at_probe &&
  995. ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) {
  996. dev_info(&ts->client->dev, "Config not set, resetting controller\n");
  997. /* Retry after a controller reset */
  998. ts->reset_controller_at_probe = true;
  999. error = goodix_reset(ts);
  1000. if (error)
  1001. return error;
  1002. goto retry_read_config;
  1003. }
  1004. dev_err(&ts->client->dev,
  1005. "Invalid config (%d, %d, %d), using defaults\n",
  1006. ts->prop.max_x, ts->prop.max_y, ts->max_touch_num);
  1007. ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
  1008. ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
  1009. ts->max_touch_num = GOODIX_MAX_CONTACTS;
  1010. input_abs_set_max(ts->input_dev,
  1011. ABS_MT_POSITION_X, ts->prop.max_x);
  1012. input_abs_set_max(ts->input_dev,
  1013. ABS_MT_POSITION_Y, ts->prop.max_y);
  1014. }
  1015. if (dmi_check_system(nine_bytes_report)) {
  1016. ts->contact_size = 9;
  1017. dev_dbg(&ts->client->dev,
  1018. "Non-standard 9-bytes report format quirk\n");
  1019. }
  1020. if (dmi_check_system(inverted_x_screen)) {
  1021. ts->prop.invert_x = true;
  1022. dev_dbg(&ts->client->dev,
  1023. "Applying 'inverted x screen' quirk\n");
  1024. }
  1025. error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
  1026. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  1027. if (error) {
  1028. dev_err(&ts->client->dev,
  1029. "Failed to initialize MT slots: %d", error);
  1030. return error;
  1031. }
  1032. input_set_drvdata(ts->input_dev, ts);
  1033. if (!ts->client->irq) {
  1034. error = input_setup_polling(ts->input_dev, goodix_ts_work_i2c_poll);
  1035. if (error) {
  1036. dev_err(&ts->client->dev,
  1037. "could not set up polling mode, %d\n", error);
  1038. return error;
  1039. }
  1040. input_set_poll_interval(ts->input_dev, GOODIX_POLL_INTERVAL_MS);
  1041. }
  1042. error = input_register_device(ts->input_dev);
  1043. if (error) {
  1044. dev_err(&ts->client->dev,
  1045. "Failed to register input device: %d", error);
  1046. return error;
  1047. }
  1048. /*
  1049. * Create the input_pen device before goodix_request_irq() calls
  1050. * devm_request_threaded_irq() so that the devm framework frees
  1051. * it after disabling the irq.
  1052. * Unfortunately there is no way to detect if the touchscreen has pen
  1053. * support, so registering the dev is delayed till the first pen event.
  1054. */
  1055. error = goodix_create_pen_input(ts);
  1056. if (error)
  1057. return error;
  1058. ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
  1059. error = goodix_request_irq(ts);
  1060. if (error) {
  1061. dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
  1062. return error;
  1063. }
  1064. return 0;
  1065. }
  1066. /**
  1067. * goodix_config_cb - Callback to finish device init
  1068. *
  1069. * @cfg: firmware config
  1070. * @ctx: our goodix_ts_data pointer
  1071. *
  1072. * request_firmware_wait callback that finishes
  1073. * initialization of the device.
  1074. */
  1075. static void goodix_config_cb(const struct firmware *cfg, void *ctx)
  1076. {
  1077. struct goodix_ts_data *ts = ctx;
  1078. int error;
  1079. if (ts->firmware_name) {
  1080. if (!cfg)
  1081. goto err_release_cfg;
  1082. error = goodix_check_cfg(ts, cfg->data, cfg->size);
  1083. if (error)
  1084. goto err_release_cfg;
  1085. memcpy(ts->config, cfg->data, cfg->size);
  1086. } else if (cfg) {
  1087. /* send device configuration to the firmware */
  1088. error = goodix_send_cfg(ts, cfg->data, cfg->size);
  1089. if (error)
  1090. goto err_release_cfg;
  1091. }
  1092. goodix_configure_dev(ts);
  1093. err_release_cfg:
  1094. release_firmware(cfg);
  1095. complete_all(&ts->firmware_loading_complete);
  1096. }
  1097. static void goodix_disable_regulators(void *arg)
  1098. {
  1099. struct goodix_ts_data *ts = arg;
  1100. regulator_disable(ts->vddio);
  1101. regulator_disable(ts->avdd28);
  1102. }
  1103. static int goodix_ts_probe(struct i2c_client *client)
  1104. {
  1105. struct goodix_ts_data *ts;
  1106. const char *cfg_name;
  1107. int error;
  1108. dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
  1109. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1110. dev_err(&client->dev, "I2C check functionality failed.\n");
  1111. return -ENXIO;
  1112. }
  1113. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  1114. if (!ts)
  1115. return -ENOMEM;
  1116. ts->client = client;
  1117. i2c_set_clientdata(client, ts);
  1118. init_completion(&ts->firmware_loading_complete);
  1119. ts->contact_size = GOODIX_CONTACT_SIZE;
  1120. error = goodix_get_gpio_config(ts);
  1121. if (error)
  1122. return error;
  1123. /* power up the controller */
  1124. error = regulator_enable(ts->avdd28);
  1125. if (error) {
  1126. dev_err(&client->dev,
  1127. "Failed to enable AVDD28 regulator: %d\n",
  1128. error);
  1129. return error;
  1130. }
  1131. error = regulator_enable(ts->vddio);
  1132. if (error) {
  1133. dev_err(&client->dev,
  1134. "Failed to enable VDDIO regulator: %d\n",
  1135. error);
  1136. regulator_disable(ts->avdd28);
  1137. return error;
  1138. }
  1139. error = devm_add_action_or_reset(&client->dev,
  1140. goodix_disable_regulators, ts);
  1141. if (error)
  1142. return error;
  1143. reset:
  1144. if (ts->reset_controller_at_probe) {
  1145. /* reset the controller */
  1146. error = goodix_reset(ts);
  1147. if (error)
  1148. return error;
  1149. }
  1150. error = goodix_i2c_test(client);
  1151. if (error) {
  1152. if (!ts->reset_controller_at_probe &&
  1153. ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) {
  1154. /* Retry after a controller reset */
  1155. ts->reset_controller_at_probe = true;
  1156. goto reset;
  1157. }
  1158. dev_err(&client->dev, "I2C communication failure: %d\n", error);
  1159. return error;
  1160. }
  1161. error = goodix_firmware_check(ts);
  1162. if (error)
  1163. return error;
  1164. error = goodix_read_version(ts);
  1165. if (error)
  1166. return error;
  1167. ts->chip = goodix_get_chip_data(ts->id);
  1168. if (ts->load_cfg_from_disk) {
  1169. /* update device config */
  1170. error = device_property_read_string(&client->dev,
  1171. "goodix,config-name",
  1172. &cfg_name);
  1173. if (!error)
  1174. snprintf(ts->cfg_name, sizeof(ts->cfg_name),
  1175. "goodix/%s", cfg_name);
  1176. else
  1177. snprintf(ts->cfg_name, sizeof(ts->cfg_name),
  1178. "goodix_%s_cfg.bin", ts->id);
  1179. error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
  1180. &client->dev, GFP_KERNEL, ts,
  1181. goodix_config_cb);
  1182. if (error) {
  1183. dev_err(&client->dev,
  1184. "Failed to invoke firmware loader: %d\n",
  1185. error);
  1186. return error;
  1187. }
  1188. return 0;
  1189. } else {
  1190. error = goodix_configure_dev(ts);
  1191. if (error)
  1192. return error;
  1193. }
  1194. return 0;
  1195. }
  1196. static void goodix_ts_remove(struct i2c_client *client)
  1197. {
  1198. struct goodix_ts_data *ts = i2c_get_clientdata(client);
  1199. if (ts->load_cfg_from_disk)
  1200. wait_for_completion(&ts->firmware_loading_complete);
  1201. }
  1202. static int goodix_suspend(struct device *dev)
  1203. {
  1204. struct i2c_client *client = to_i2c_client(dev);
  1205. struct goodix_ts_data *ts = i2c_get_clientdata(client);
  1206. int error;
  1207. if (ts->load_cfg_from_disk)
  1208. wait_for_completion(&ts->firmware_loading_complete);
  1209. /* We need gpio pins to suspend/resume */
  1210. if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
  1211. goodix_disable_irq(ts);
  1212. return 0;
  1213. }
  1214. /* Free IRQ as IRQ pin is used as output in the suspend sequence */
  1215. goodix_free_irq(ts);
  1216. /* Save reference (calibration) info if necessary */
  1217. goodix_save_bak_ref(ts);
  1218. /* Output LOW on the INT pin for 5 ms */
  1219. error = goodix_irq_direction_output(ts, 0);
  1220. if (error) {
  1221. goodix_request_irq(ts);
  1222. return error;
  1223. }
  1224. usleep_range(5000, 6000);
  1225. error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
  1226. GOODIX_CMD_SCREEN_OFF);
  1227. if (error) {
  1228. goodix_irq_direction_input(ts);
  1229. goodix_request_irq(ts);
  1230. return -EAGAIN;
  1231. }
  1232. /*
  1233. * The datasheet specifies that the interval between sending screen-off
  1234. * command and wake-up should be longer than 58 ms. To avoid waking up
  1235. * sooner, delay 58ms here.
  1236. */
  1237. msleep(58);
  1238. return 0;
  1239. }
  1240. static int goodix_resume(struct device *dev)
  1241. {
  1242. struct i2c_client *client = to_i2c_client(dev);
  1243. struct goodix_ts_data *ts = i2c_get_clientdata(client);
  1244. u8 config_ver;
  1245. int error;
  1246. if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
  1247. goodix_enable_irq(ts);
  1248. return 0;
  1249. }
  1250. /*
  1251. * Exit sleep mode by outputting HIGH level to INT pin
  1252. * for 2ms~5ms.
  1253. */
  1254. error = goodix_irq_direction_output(ts, 1);
  1255. if (error)
  1256. return error;
  1257. usleep_range(2000, 5000);
  1258. error = goodix_int_sync(ts);
  1259. if (error)
  1260. return error;
  1261. error = goodix_i2c_read(ts->client, ts->chip->config_addr,
  1262. &config_ver, 1);
  1263. if (!error && config_ver != ts->config[0])
  1264. dev_info(dev, "Config version mismatch %d != %d, resetting controller\n",
  1265. config_ver, ts->config[0]);
  1266. if (error != 0 || config_ver != ts->config[0]) {
  1267. error = goodix_reset(ts);
  1268. if (error)
  1269. return error;
  1270. error = goodix_send_cfg(ts, ts->config, ts->chip->config_len);
  1271. if (error)
  1272. return error;
  1273. }
  1274. error = goodix_request_irq(ts);
  1275. if (error)
  1276. return error;
  1277. return 0;
  1278. }
  1279. static DEFINE_SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
  1280. static const struct i2c_device_id goodix_ts_id[] = {
  1281. { "GDIX1001:00" },
  1282. { }
  1283. };
  1284. MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
  1285. #ifdef CONFIG_ACPI
  1286. static const struct acpi_device_id goodix_acpi_match[] = {
  1287. { "GDIX1001", 0 },
  1288. { "GDIX1002", 0 },
  1289. { "GDIX1003", 0 },
  1290. { "GDX9110", 0 },
  1291. { }
  1292. };
  1293. MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
  1294. #endif
  1295. #ifdef CONFIG_OF
  1296. static const struct of_device_id goodix_of_match[] = {
  1297. { .compatible = "goodix,gt1151" },
  1298. { .compatible = "goodix,gt1158" },
  1299. { .compatible = "goodix,gt5663" },
  1300. { .compatible = "goodix,gt5688" },
  1301. { .compatible = "goodix,gt911" },
  1302. { .compatible = "goodix,gt9110" },
  1303. { .compatible = "goodix,gt912" },
  1304. { .compatible = "goodix,gt9147" },
  1305. { .compatible = "goodix,gt917s" },
  1306. { .compatible = "goodix,gt927" },
  1307. { .compatible = "goodix,gt9271" },
  1308. { .compatible = "goodix,gt928" },
  1309. { .compatible = "goodix,gt9286" },
  1310. { .compatible = "goodix,gt967" },
  1311. { }
  1312. };
  1313. MODULE_DEVICE_TABLE(of, goodix_of_match);
  1314. #endif
  1315. static struct i2c_driver goodix_ts_driver = {
  1316. .probe = goodix_ts_probe,
  1317. .remove = goodix_ts_remove,
  1318. .id_table = goodix_ts_id,
  1319. .driver = {
  1320. .name = "Goodix-TS",
  1321. .acpi_match_table = ACPI_PTR(goodix_acpi_match),
  1322. .of_match_table = of_match_ptr(goodix_of_match),
  1323. .pm = pm_sleep_ptr(&goodix_pm_ops),
  1324. },
  1325. };
  1326. module_i2c_driver(goodix_ts_driver);
  1327. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  1328. MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
  1329. MODULE_DESCRIPTION("Goodix touchscreen driver");
  1330. MODULE_LICENSE("GPL v2");