goodix.c 41 KB

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