goodix.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  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. /* 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. /* Some devices with gpio_int_idx 0 list a third unused GPIO */
  746. if ((ts->gpio_count == 2 || ts->gpio_count == 3) && ts->gpio_int_idx == 0) {
  747. ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
  748. gpio_mapping = acpi_goodix_int_first_gpios;
  749. } else if (ts->gpio_count == 2 && ts->gpio_int_idx == 1) {
  750. ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
  751. gpio_mapping = acpi_goodix_int_last_gpios;
  752. } else if (ts->gpio_count == 1 && ts->gpio_int_idx == -1 &&
  753. acpi_has_method(ACPI_HANDLE(dev), "INTI") &&
  754. acpi_has_method(ACPI_HANDLE(dev), "INTO")) {
  755. dev_info(dev, "Using ACPI INTI and INTO methods for IRQ pin access\n");
  756. ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_METHOD;
  757. gpio_mapping = acpi_goodix_reset_only_gpios;
  758. } else if (soc_intel_is_byt() && ts->gpio_count == 2 && ts->gpio_int_idx == -1) {
  759. dev_info(dev, "No ACPI GpioInt resource, assuming that the GPIO order is reset, int\n");
  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 == 0) {
  763. /*
  764. * On newer devices there is only 1 GpioInt resource and _PS0
  765. * does the whole reset sequence for us.
  766. */
  767. acpi_device_fix_up_power(ACPI_COMPANION(dev));
  768. /*
  769. * Before the _PS0 call the int GPIO may have been in output
  770. * mode and the call should have put the int GPIO in input mode,
  771. * but the GPIO subsys cached state may still think it is
  772. * in output mode, causing gpiochip_lock_as_irq() failure.
  773. *
  774. * Add a mapping for the int GPIO to make the
  775. * gpiod_int = gpiod_get(..., GPIOD_IN) call succeed,
  776. * which will explicitly set the direction to input.
  777. */
  778. ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
  779. gpio_mapping = acpi_goodix_int_first_gpios;
  780. } else {
  781. dev_warn(dev, "Unexpected ACPI resources: gpio_count %d, gpio_int_idx %d\n",
  782. ts->gpio_count, ts->gpio_int_idx);
  783. /*
  784. * On some devices _PS0 does a reset for us and
  785. * sometimes this is necessary for things to work.
  786. */
  787. acpi_device_fix_up_power(ACPI_COMPANION(dev));
  788. return -EINVAL;
  789. }
  790. /*
  791. * Normally we put the reset pin in input / high-impedance mode to save
  792. * power. But some x86/ACPI boards don't have a pull-up, so for the ACPI
  793. * case, leave the pin as is. This results in the pin not being touched
  794. * at all on x86/ACPI boards, except when needed for error-recover.
  795. */
  796. ts->gpiod_rst_flags = GPIOD_ASIS;
  797. return devm_acpi_dev_add_driver_gpios(dev, gpio_mapping);
  798. }
  799. #else
  800. static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
  801. {
  802. return -EINVAL;
  803. }
  804. #endif /* CONFIG_X86 && CONFIG_ACPI */
  805. /**
  806. * goodix_get_gpio_config - Get GPIO config from ACPI/DT
  807. *
  808. * @ts: goodix_ts_data pointer
  809. */
  810. static int goodix_get_gpio_config(struct goodix_ts_data *ts)
  811. {
  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. return dev_err_probe(dev, PTR_ERR(ts->avdd28), "Failed to get AVDD28 regulator\n");
  826. ts->vddio = devm_regulator_get(dev, "VDDIO");
  827. if (IS_ERR(ts->vddio))
  828. return dev_err_probe(dev, PTR_ERR(ts->vddio), "Failed to get VDDIO regulator\n");
  829. retry_get_irq_gpio:
  830. /* Get the interrupt GPIO pin number */
  831. gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
  832. if (IS_ERR(gpiod))
  833. return dev_err_probe(dev, PTR_ERR(gpiod), "Failed to get %s GPIO\n",
  834. GOODIX_GPIO_INT_NAME);
  835. if (!gpiod && has_acpi_companion(dev) && !added_acpi_mappings) {
  836. added_acpi_mappings = true;
  837. if (goodix_add_acpi_gpio_mappings(ts) == 0)
  838. goto retry_get_irq_gpio;
  839. }
  840. ts->gpiod_int = gpiod;
  841. /* Get the reset line GPIO pin number */
  842. gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, ts->gpiod_rst_flags);
  843. if (IS_ERR(gpiod))
  844. return dev_err_probe(dev, PTR_ERR(gpiod), "Failed to get %s GPIO\n",
  845. GOODIX_GPIO_RST_NAME);
  846. ts->gpiod_rst = gpiod;
  847. switch (ts->irq_pin_access_method) {
  848. case IRQ_PIN_ACCESS_ACPI_GPIO:
  849. /*
  850. * We end up here if goodix_add_acpi_gpio_mappings() has
  851. * called devm_acpi_dev_add_driver_gpios() because the ACPI
  852. * tables did not contain name to index mappings.
  853. * Check that we successfully got both GPIOs after we've
  854. * added our own acpi_gpio_mapping and if we did not get both
  855. * GPIOs reset irq_pin_access_method to IRQ_PIN_ACCESS_NONE.
  856. */
  857. if (!ts->gpiod_int || !ts->gpiod_rst)
  858. ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
  859. break;
  860. case IRQ_PIN_ACCESS_ACPI_METHOD:
  861. if (!ts->gpiod_rst)
  862. ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
  863. break;
  864. default:
  865. if (ts->gpiod_int && ts->gpiod_rst) {
  866. ts->reset_controller_at_probe = true;
  867. ts->load_cfg_from_disk = true;
  868. ts->irq_pin_access_method = IRQ_PIN_ACCESS_GPIO;
  869. }
  870. }
  871. return 0;
  872. }
  873. /**
  874. * goodix_read_config - Read the embedded configuration of the panel
  875. *
  876. * @ts: our goodix_ts_data pointer
  877. *
  878. * Must be called during probe
  879. */
  880. static void goodix_read_config(struct goodix_ts_data *ts)
  881. {
  882. int x_max, y_max;
  883. int error;
  884. /*
  885. * On controllers where we need to upload the firmware
  886. * (controllers without flash) ts->config already has the config
  887. * at this point and the controller itself does not have it yet!
  888. */
  889. if (!ts->firmware_name) {
  890. error = goodix_i2c_read(ts->client, ts->chip->config_addr,
  891. ts->config, ts->chip->config_len);
  892. if (error) {
  893. ts->int_trigger_type = GOODIX_INT_TRIGGER;
  894. ts->max_touch_num = GOODIX_MAX_CONTACTS;
  895. return;
  896. }
  897. }
  898. ts->int_trigger_type = ts->config[TRIGGER_LOC] & 0x03;
  899. ts->max_touch_num = ts->config[MAX_CONTACTS_LOC] & 0x0f;
  900. x_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC]);
  901. y_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC + 2]);
  902. if (x_max && y_max) {
  903. input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
  904. input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
  905. }
  906. ts->chip->calc_config_checksum(ts);
  907. }
  908. /**
  909. * goodix_read_version - Read goodix touchscreen version
  910. *
  911. * @ts: our goodix_ts_data pointer
  912. */
  913. static int goodix_read_version(struct goodix_ts_data *ts)
  914. {
  915. int error;
  916. u8 buf[6];
  917. char id_str[GOODIX_ID_MAX_LEN + 1];
  918. error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
  919. if (error)
  920. return error;
  921. memcpy(id_str, buf, GOODIX_ID_MAX_LEN);
  922. id_str[GOODIX_ID_MAX_LEN] = 0;
  923. strscpy(ts->id, id_str, GOODIX_ID_MAX_LEN + 1);
  924. ts->version = get_unaligned_le16(&buf[4]);
  925. dev_info(&ts->client->dev, "ID %s, version: %04x\n", ts->id,
  926. ts->version);
  927. return 0;
  928. }
  929. /**
  930. * goodix_i2c_test - I2C test function to check if the device answers.
  931. *
  932. * @client: the i2c client
  933. */
  934. static int goodix_i2c_test(struct i2c_client *client)
  935. {
  936. int retry = 0;
  937. int error;
  938. u8 test;
  939. while (retry++ < 2) {
  940. error = goodix_i2c_read(client, GOODIX_REG_ID, &test, 1);
  941. if (!error)
  942. return 0;
  943. msleep(20);
  944. }
  945. return error;
  946. }
  947. /**
  948. * goodix_configure_dev - Finish device initialization
  949. *
  950. * @ts: our goodix_ts_data pointer
  951. *
  952. * Must be called from probe to finish initialization of the device.
  953. * Contains the common initialization code for both devices that
  954. * declare gpio pins and devices that do not. It is either called
  955. * directly from probe or from request_firmware_wait callback.
  956. */
  957. static int goodix_configure_dev(struct goodix_ts_data *ts)
  958. {
  959. int error;
  960. int i;
  961. ts->int_trigger_type = GOODIX_INT_TRIGGER;
  962. ts->max_touch_num = GOODIX_MAX_CONTACTS;
  963. ts->input_dev = devm_input_allocate_device(&ts->client->dev);
  964. if (!ts->input_dev) {
  965. dev_err(&ts->client->dev, "Failed to allocate input device.");
  966. return -ENOMEM;
  967. }
  968. ts->input_dev->name = "Goodix Capacitive TouchScreen";
  969. ts->input_dev->phys = "input/ts";
  970. ts->input_dev->id.bustype = BUS_I2C;
  971. ts->input_dev->id.vendor = 0x0416;
  972. if (kstrtou16(ts->id, 10, &ts->input_dev->id.product))
  973. ts->input_dev->id.product = 0x1001;
  974. ts->input_dev->id.version = ts->version;
  975. ts->input_dev->keycode = ts->keymap;
  976. ts->input_dev->keycodesize = sizeof(ts->keymap[0]);
  977. ts->input_dev->keycodemax = GOODIX_MAX_KEYS;
  978. /* Capacitive Windows/Home button on some devices */
  979. for (i = 0; i < GOODIX_MAX_KEYS; ++i) {
  980. if (i == 0)
  981. ts->keymap[i] = KEY_LEFTMETA;
  982. else
  983. ts->keymap[i] = KEY_F1 + (i - 1);
  984. input_set_capability(ts->input_dev, EV_KEY, ts->keymap[i]);
  985. }
  986. input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
  987. input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
  988. input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
  989. input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  990. retry_read_config:
  991. /* Read configuration and apply touchscreen parameters */
  992. goodix_read_config(ts);
  993. /* Try overriding touchscreen parameters via device properties */
  994. touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
  995. if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
  996. if (!ts->reset_controller_at_probe &&
  997. ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) {
  998. dev_info(&ts->client->dev, "Config not set, resetting controller\n");
  999. /* Retry after a controller reset */
  1000. ts->reset_controller_at_probe = true;
  1001. error = goodix_reset(ts);
  1002. if (error)
  1003. return error;
  1004. goto retry_read_config;
  1005. }
  1006. dev_err(&ts->client->dev,
  1007. "Invalid config (%d, %d, %d), using defaults\n",
  1008. ts->prop.max_x, ts->prop.max_y, ts->max_touch_num);
  1009. ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
  1010. ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
  1011. ts->max_touch_num = GOODIX_MAX_CONTACTS;
  1012. input_abs_set_max(ts->input_dev,
  1013. ABS_MT_POSITION_X, ts->prop.max_x);
  1014. input_abs_set_max(ts->input_dev,
  1015. ABS_MT_POSITION_Y, ts->prop.max_y);
  1016. }
  1017. if (dmi_check_system(nine_bytes_report)) {
  1018. ts->contact_size = 9;
  1019. dev_dbg(&ts->client->dev,
  1020. "Non-standard 9-bytes report format quirk\n");
  1021. }
  1022. if (dmi_check_system(inverted_x_screen)) {
  1023. ts->prop.invert_x = true;
  1024. dev_dbg(&ts->client->dev,
  1025. "Applying 'inverted x screen' quirk\n");
  1026. }
  1027. error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
  1028. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  1029. if (error) {
  1030. dev_err(&ts->client->dev,
  1031. "Failed to initialize MT slots: %d", error);
  1032. return error;
  1033. }
  1034. error = input_register_device(ts->input_dev);
  1035. if (error) {
  1036. dev_err(&ts->client->dev,
  1037. "Failed to register input device: %d", error);
  1038. return error;
  1039. }
  1040. /*
  1041. * Create the input_pen device before goodix_request_irq() calls
  1042. * devm_request_threaded_irq() so that the devm framework frees
  1043. * it after disabling the irq.
  1044. * Unfortunately there is no way to detect if the touchscreen has pen
  1045. * support, so registering the dev is delayed till the first pen event.
  1046. */
  1047. error = goodix_create_pen_input(ts);
  1048. if (error)
  1049. return error;
  1050. ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
  1051. error = goodix_request_irq(ts);
  1052. if (error) {
  1053. dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
  1054. return error;
  1055. }
  1056. return 0;
  1057. }
  1058. /**
  1059. * goodix_config_cb - Callback to finish device init
  1060. *
  1061. * @cfg: firmware config
  1062. * @ctx: our goodix_ts_data pointer
  1063. *
  1064. * request_firmware_wait callback that finishes
  1065. * initialization of the device.
  1066. */
  1067. static void goodix_config_cb(const struct firmware *cfg, void *ctx)
  1068. {
  1069. struct goodix_ts_data *ts = ctx;
  1070. int error;
  1071. if (ts->firmware_name) {
  1072. if (!cfg)
  1073. goto err_release_cfg;
  1074. error = goodix_check_cfg(ts, cfg->data, cfg->size);
  1075. if (error)
  1076. goto err_release_cfg;
  1077. memcpy(ts->config, cfg->data, cfg->size);
  1078. } else if (cfg) {
  1079. /* send device configuration to the firmware */
  1080. error = goodix_send_cfg(ts, cfg->data, cfg->size);
  1081. if (error)
  1082. goto err_release_cfg;
  1083. }
  1084. goodix_configure_dev(ts);
  1085. err_release_cfg:
  1086. release_firmware(cfg);
  1087. complete_all(&ts->firmware_loading_complete);
  1088. }
  1089. static void goodix_disable_regulators(void *arg)
  1090. {
  1091. struct goodix_ts_data *ts = arg;
  1092. regulator_disable(ts->vddio);
  1093. regulator_disable(ts->avdd28);
  1094. }
  1095. static int goodix_ts_probe(struct i2c_client *client)
  1096. {
  1097. struct goodix_ts_data *ts;
  1098. const char *cfg_name;
  1099. int error;
  1100. dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
  1101. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1102. dev_err(&client->dev, "I2C check functionality failed.\n");
  1103. return -ENXIO;
  1104. }
  1105. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  1106. if (!ts)
  1107. return -ENOMEM;
  1108. ts->client = client;
  1109. i2c_set_clientdata(client, ts);
  1110. init_completion(&ts->firmware_loading_complete);
  1111. ts->contact_size = GOODIX_CONTACT_SIZE;
  1112. error = goodix_get_gpio_config(ts);
  1113. if (error)
  1114. return error;
  1115. /* power up the controller */
  1116. error = regulator_enable(ts->avdd28);
  1117. if (error) {
  1118. dev_err(&client->dev,
  1119. "Failed to enable AVDD28 regulator: %d\n",
  1120. error);
  1121. return error;
  1122. }
  1123. error = regulator_enable(ts->vddio);
  1124. if (error) {
  1125. dev_err(&client->dev,
  1126. "Failed to enable VDDIO regulator: %d\n",
  1127. error);
  1128. regulator_disable(ts->avdd28);
  1129. return error;
  1130. }
  1131. error = devm_add_action_or_reset(&client->dev,
  1132. goodix_disable_regulators, ts);
  1133. if (error)
  1134. return error;
  1135. reset:
  1136. if (ts->reset_controller_at_probe) {
  1137. /* reset the controller */
  1138. error = goodix_reset(ts);
  1139. if (error)
  1140. return error;
  1141. }
  1142. error = goodix_i2c_test(client);
  1143. if (error) {
  1144. if (!ts->reset_controller_at_probe &&
  1145. ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) {
  1146. /* Retry after a controller reset */
  1147. ts->reset_controller_at_probe = true;
  1148. goto reset;
  1149. }
  1150. dev_err(&client->dev, "I2C communication failure: %d\n", error);
  1151. return error;
  1152. }
  1153. error = goodix_firmware_check(ts);
  1154. if (error)
  1155. return error;
  1156. error = goodix_read_version(ts);
  1157. if (error)
  1158. return error;
  1159. ts->chip = goodix_get_chip_data(ts->id);
  1160. if (ts->load_cfg_from_disk) {
  1161. /* update device config */
  1162. error = device_property_read_string(&client->dev,
  1163. "goodix,config-name",
  1164. &cfg_name);
  1165. if (!error)
  1166. snprintf(ts->cfg_name, sizeof(ts->cfg_name),
  1167. "goodix/%s", cfg_name);
  1168. else
  1169. snprintf(ts->cfg_name, sizeof(ts->cfg_name),
  1170. "goodix_%s_cfg.bin", ts->id);
  1171. error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
  1172. &client->dev, GFP_KERNEL, ts,
  1173. goodix_config_cb);
  1174. if (error) {
  1175. dev_err(&client->dev,
  1176. "Failed to invoke firmware loader: %d\n",
  1177. error);
  1178. return error;
  1179. }
  1180. return 0;
  1181. } else {
  1182. error = goodix_configure_dev(ts);
  1183. if (error)
  1184. return error;
  1185. }
  1186. return 0;
  1187. }
  1188. static void goodix_ts_remove(struct i2c_client *client)
  1189. {
  1190. struct goodix_ts_data *ts = i2c_get_clientdata(client);
  1191. if (ts->load_cfg_from_disk)
  1192. wait_for_completion(&ts->firmware_loading_complete);
  1193. }
  1194. static int goodix_suspend(struct device *dev)
  1195. {
  1196. struct i2c_client *client = to_i2c_client(dev);
  1197. struct goodix_ts_data *ts = i2c_get_clientdata(client);
  1198. int error;
  1199. if (ts->load_cfg_from_disk)
  1200. wait_for_completion(&ts->firmware_loading_complete);
  1201. /* We need gpio pins to suspend/resume */
  1202. if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
  1203. disable_irq(client->irq);
  1204. return 0;
  1205. }
  1206. /* Free IRQ as IRQ pin is used as output in the suspend sequence */
  1207. goodix_free_irq(ts);
  1208. /* Save reference (calibration) info if necessary */
  1209. goodix_save_bak_ref(ts);
  1210. /* Output LOW on the INT pin for 5 ms */
  1211. error = goodix_irq_direction_output(ts, 0);
  1212. if (error) {
  1213. goodix_request_irq(ts);
  1214. return error;
  1215. }
  1216. usleep_range(5000, 6000);
  1217. error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
  1218. GOODIX_CMD_SCREEN_OFF);
  1219. if (error) {
  1220. goodix_irq_direction_input(ts);
  1221. goodix_request_irq(ts);
  1222. return -EAGAIN;
  1223. }
  1224. /*
  1225. * The datasheet specifies that the interval between sending screen-off
  1226. * command and wake-up should be longer than 58 ms. To avoid waking up
  1227. * sooner, delay 58ms here.
  1228. */
  1229. msleep(58);
  1230. return 0;
  1231. }
  1232. static int goodix_resume(struct device *dev)
  1233. {
  1234. struct i2c_client *client = to_i2c_client(dev);
  1235. struct goodix_ts_data *ts = i2c_get_clientdata(client);
  1236. u8 config_ver;
  1237. int error;
  1238. if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
  1239. enable_irq(client->irq);
  1240. return 0;
  1241. }
  1242. /*
  1243. * Exit sleep mode by outputting HIGH level to INT pin
  1244. * for 2ms~5ms.
  1245. */
  1246. error = goodix_irq_direction_output(ts, 1);
  1247. if (error)
  1248. return error;
  1249. usleep_range(2000, 5000);
  1250. error = goodix_int_sync(ts);
  1251. if (error)
  1252. return error;
  1253. error = goodix_i2c_read(ts->client, ts->chip->config_addr,
  1254. &config_ver, 1);
  1255. if (!error && config_ver != ts->config[0])
  1256. dev_info(dev, "Config version mismatch %d != %d, resetting controller\n",
  1257. config_ver, ts->config[0]);
  1258. if (error != 0 || config_ver != ts->config[0]) {
  1259. error = goodix_reset(ts);
  1260. if (error)
  1261. return error;
  1262. error = goodix_send_cfg(ts, ts->config, ts->chip->config_len);
  1263. if (error)
  1264. return error;
  1265. }
  1266. error = goodix_request_irq(ts);
  1267. if (error)
  1268. return error;
  1269. return 0;
  1270. }
  1271. static DEFINE_SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
  1272. static const struct i2c_device_id goodix_ts_id[] = {
  1273. { "GDIX1001:00" },
  1274. { }
  1275. };
  1276. MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
  1277. #ifdef CONFIG_ACPI
  1278. static const struct acpi_device_id goodix_acpi_match[] = {
  1279. { "GDIX1001", 0 },
  1280. { "GDIX1002", 0 },
  1281. { "GDX9110", 0 },
  1282. { }
  1283. };
  1284. MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
  1285. #endif
  1286. #ifdef CONFIG_OF
  1287. static const struct of_device_id goodix_of_match[] = {
  1288. { .compatible = "goodix,gt1151" },
  1289. { .compatible = "goodix,gt1158" },
  1290. { .compatible = "goodix,gt5663" },
  1291. { .compatible = "goodix,gt5688" },
  1292. { .compatible = "goodix,gt911" },
  1293. { .compatible = "goodix,gt9110" },
  1294. { .compatible = "goodix,gt912" },
  1295. { .compatible = "goodix,gt9147" },
  1296. { .compatible = "goodix,gt917s" },
  1297. { .compatible = "goodix,gt927" },
  1298. { .compatible = "goodix,gt9271" },
  1299. { .compatible = "goodix,gt928" },
  1300. { .compatible = "goodix,gt9286" },
  1301. { .compatible = "goodix,gt967" },
  1302. { }
  1303. };
  1304. MODULE_DEVICE_TABLE(of, goodix_of_match);
  1305. #endif
  1306. static struct i2c_driver goodix_ts_driver = {
  1307. .probe = goodix_ts_probe,
  1308. .remove = goodix_ts_remove,
  1309. .id_table = goodix_ts_id,
  1310. .driver = {
  1311. .name = "Goodix-TS",
  1312. .acpi_match_table = ACPI_PTR(goodix_acpi_match),
  1313. .of_match_table = of_match_ptr(goodix_of_match),
  1314. .pm = pm_sleep_ptr(&goodix_pm_ops),
  1315. },
  1316. };
  1317. module_i2c_driver(goodix_ts_driver);
  1318. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  1319. MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
  1320. MODULE_DESCRIPTION("Goodix touchscreen driver");
  1321. MODULE_LICENSE("GPL v2");