goodix_brl_hw.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. /*
  2. * Goodix Touchscreen Driver
  3. * Copyright (C) 2020 - 2021 Goodix, Inc.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be a reference
  12. * to you, when you are integrating the GOODiX's CTP IC into your system,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. */
  18. #include "goodix_ts_core.h"
  19. /* berlin_A SPI mode setting */
  20. #define GOODIX_SPI_MODE_REG 0xC900
  21. #define GOODIX_SPI_NORMAL_MODE_0 0x01
  22. /* berlin_A D12 setting */
  23. #define GOODIX_REG_CLK_STA0 0xD807
  24. #define GOODIX_CLK_STA0_ENABLE 0xFF
  25. #define GOODIX_REG_CLK_STA1 0xD806
  26. #define GOODIX_CLK_STA1_ENABLE 0x77
  27. #define GOODIX_REG_TRIM_D12 0xD006
  28. #define GOODIX_TRIM_D12_LEVEL 0x3C
  29. #define GOODIX_REG_RESET 0xD808
  30. #define GOODIX_RESET_EN 0xFA
  31. #define HOLD_CPU_REG_W 0x0002
  32. #define HOLD_CPU_REG_R 0x2000
  33. #define DEV_CONFIRM_VAL 0xAA
  34. #define BOOTOPTION_ADDR 0x10000
  35. #define FW_VERSION_INFO_ADDR_BRA 0x1000C
  36. #define FW_VERSION_INFO_ADDR 0x10014
  37. #define GOODIX_IC_INFO_MAX_LEN 1024
  38. #define GOODIX_IC_INFO_ADDR_BRA 0x10068
  39. #define GOODIX_IC_INFO_ADDR 0x10070
  40. enum brl_request_code {
  41. BRL_REQUEST_CODE_CONFIG = 0x01,
  42. BRL_REQUEST_CODE_REF_ERR = 0x02,
  43. BRL_REQUEST_CODE_RESET = 0x03,
  44. BRL_REQUEST_CODE_CLOCK = 0x04,
  45. };
  46. static int brl_select_spi_mode(struct goodix_ts_core *cd)
  47. {
  48. int ret;
  49. int i;
  50. u8 w_value = GOODIX_SPI_NORMAL_MODE_0;
  51. u8 r_value;
  52. if (cd->bus->bus_type == GOODIX_BUS_TYPE_I2C ||
  53. cd->bus->ic_type != IC_TYPE_BERLIN_A)
  54. return 0;
  55. for (i = 0; i < GOODIX_RETRY_5; i++) {
  56. cd->hw_ops->write(cd, GOODIX_SPI_MODE_REG,
  57. &w_value, 1);
  58. ret = cd->hw_ops->read(cd, GOODIX_SPI_MODE_REG,
  59. &r_value, 1);
  60. if (!ret && r_value == w_value)
  61. return 0;
  62. }
  63. ts_err("failed switch SPI mode, ret:%d r_value:%02x", ret, r_value);
  64. return -EINVAL;
  65. }
  66. static int brl_dev_confirm(struct goodix_ts_core *cd)
  67. {
  68. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  69. int ret = 0;
  70. int retry = GOODIX_RETRY_3;
  71. u8 tx_buf[8] = {0};
  72. u8 rx_buf[8] = {0};
  73. memset(tx_buf, DEV_CONFIRM_VAL, sizeof(tx_buf));
  74. while (retry--) {
  75. ret = hw_ops->write(cd, BOOTOPTION_ADDR,
  76. tx_buf, sizeof(tx_buf));
  77. if (ret < 0)
  78. return ret;
  79. ret = hw_ops->read(cd, BOOTOPTION_ADDR,
  80. rx_buf, sizeof(rx_buf));
  81. if (ret < 0)
  82. return ret;
  83. if (!memcmp(tx_buf, rx_buf, sizeof(tx_buf)))
  84. break;
  85. usleep_range(5000, 5100);
  86. }
  87. if (retry < 0) {
  88. ret = -EINVAL;
  89. ts_err("device confirm failed, rx_buf:%*ph", 8, rx_buf);
  90. }
  91. ts_info("device connected");
  92. return ret;
  93. }
  94. static int brl_reset_after(struct goodix_ts_core *cd)
  95. {
  96. u8 reg_val[2] = {0};
  97. u8 temp_buf[12] = {0};
  98. int ret;
  99. int retry;
  100. if (cd->bus->ic_type != IC_TYPE_BERLIN_A)
  101. return 0;
  102. ts_info("IN");
  103. /* select spi mode */
  104. ret = brl_select_spi_mode(cd);
  105. if (ret < 0)
  106. return ret;
  107. /* hold cpu */
  108. retry = GOODIX_RETRY_10;
  109. while (retry--) {
  110. reg_val[0] = 0x01;
  111. reg_val[1] = 0x00;
  112. ret = cd->hw_ops->write(cd, HOLD_CPU_REG_W, reg_val, 2);
  113. ret |= cd->hw_ops->read(cd, HOLD_CPU_REG_R, &temp_buf[0], 4);
  114. ret |= cd->hw_ops->read(cd, HOLD_CPU_REG_R, &temp_buf[4], 4);
  115. ret |= cd->hw_ops->read(cd, HOLD_CPU_REG_R, &temp_buf[8], 4);
  116. if (!ret && !memcmp(&temp_buf[0], &temp_buf[4], 4) &&
  117. !memcmp(&temp_buf[4], &temp_buf[8], 4) &&
  118. !memcmp(&temp_buf[0], &temp_buf[8], 4)) {
  119. break;
  120. }
  121. }
  122. if (retry < 0) {
  123. ts_err("failed to hold cpu, status:%*ph", 12, temp_buf);
  124. return -EINVAL;
  125. }
  126. /* enable sta0 clk */
  127. retry = GOODIX_RETRY_5;
  128. while (retry--) {
  129. reg_val[0] = GOODIX_CLK_STA0_ENABLE;
  130. ret = cd->hw_ops->write(cd, GOODIX_REG_CLK_STA0, reg_val, 1);
  131. ret |= cd->hw_ops->read(cd, GOODIX_REG_CLK_STA0, temp_buf, 1);
  132. if (!ret && temp_buf[0] == GOODIX_CLK_STA0_ENABLE)
  133. break;
  134. }
  135. if (retry < 0) {
  136. ts_err("failed to enable group0 clock, ret:%d status:%02x",
  137. ret, temp_buf[0]);
  138. return -EINVAL;
  139. }
  140. /* enable sta1 clk */
  141. retry = GOODIX_RETRY_5;
  142. while (retry--) {
  143. reg_val[0] = GOODIX_CLK_STA1_ENABLE;
  144. ret = cd->hw_ops->write(cd, GOODIX_REG_CLK_STA1, reg_val, 1);
  145. ret |= cd->hw_ops->read(cd, GOODIX_REG_CLK_STA1, temp_buf, 1);
  146. if (!ret && temp_buf[0] == GOODIX_CLK_STA1_ENABLE)
  147. break;
  148. }
  149. if (retry < 0) {
  150. ts_err("failed to enable group1 clock, ret:%d status:%02x",
  151. ret, temp_buf[0]);
  152. return -EINVAL;
  153. }
  154. /* set D12 level */
  155. retry = GOODIX_RETRY_5;
  156. while (retry--) {
  157. reg_val[0] = GOODIX_TRIM_D12_LEVEL;
  158. ret = cd->hw_ops->write(cd, GOODIX_REG_TRIM_D12, reg_val, 1);
  159. ret |= cd->hw_ops->read(cd, GOODIX_REG_TRIM_D12, temp_buf, 1);
  160. if (!ret && temp_buf[0] == GOODIX_TRIM_D12_LEVEL)
  161. break;
  162. }
  163. if (retry < 0) {
  164. ts_err("failed to set D12, ret:%d status:%02x",
  165. ret, temp_buf[0]);
  166. return -EINVAL;
  167. }
  168. usleep_range(5000, 5100);
  169. /* soft reset */
  170. reg_val[0] = GOODIX_RESET_EN;
  171. ret = cd->hw_ops->write(cd, GOODIX_REG_RESET, reg_val, 1);
  172. if (ret < 0)
  173. return ret;
  174. /* select spi mode */
  175. ret = brl_select_spi_mode(cd);
  176. if (ret < 0)
  177. return ret;
  178. ts_info("OUT");
  179. return 0;
  180. }
  181. static int brl_power_on(struct goodix_ts_core *cd, bool on)
  182. {
  183. int ret = 0;
  184. int iovdd_gpio = cd->board_data.iovdd_gpio;
  185. int avdd_gpio = cd->board_data.avdd_gpio;
  186. int reset_gpio = cd->board_data.reset_gpio;
  187. if (on) {
  188. if (iovdd_gpio > 0) {
  189. gpio_direction_output(iovdd_gpio, 1);
  190. } else if (cd->iovdd) {
  191. ret = regulator_enable(cd->iovdd);
  192. if (ret < 0) {
  193. ts_err("Failed to enable iovdd:%d", ret);
  194. goto power_off;
  195. }
  196. }
  197. usleep_range(3000, 3100);
  198. if (avdd_gpio > 0) {
  199. gpio_direction_output(avdd_gpio, 1);
  200. } else if (cd->avdd) {
  201. ret = regulator_enable(cd->avdd);
  202. if (ret < 0) {
  203. ts_err("Failed to enable avdd:%d", ret);
  204. goto power_off;
  205. }
  206. }
  207. usleep_range(15000, 15100);
  208. gpio_direction_output(reset_gpio, 1);
  209. usleep_range(4000, 4100);
  210. ret = brl_dev_confirm(cd);
  211. if (ret < 0)
  212. goto power_off;
  213. ret = brl_reset_after(cd);
  214. if (ret < 0)
  215. goto power_off;
  216. msleep(GOODIX_NORMAL_RESET_DELAY_MS);
  217. return 0;
  218. }
  219. power_off:
  220. gpio_direction_output(reset_gpio, 0);
  221. if (iovdd_gpio > 0)
  222. gpio_direction_output(iovdd_gpio, 0);
  223. else if (cd->iovdd)
  224. regulator_disable(cd->iovdd);
  225. if (avdd_gpio > 0)
  226. gpio_direction_output(avdd_gpio, 0);
  227. else if (cd->avdd)
  228. regulator_disable(cd->avdd);
  229. return ret;
  230. }
  231. #define GOODIX_SLEEP_CMD 0x84
  232. int brl_suspend(struct goodix_ts_core *cd)
  233. {
  234. #ifdef GOODIX_SUSPEND_GESTURE_ENABLE
  235. struct goodix_ts_cmd sleep_cmd;
  236. sleep_cmd.cmd = GOODIX_SLEEP_CMD;
  237. sleep_cmd.len = 4;
  238. if (cd->hw_ops->send_cmd(cd, &sleep_cmd))
  239. ts_err("failed send sleep cmd");
  240. #else
  241. if (cd->hw_ops->power_on(cd, 0))
  242. ts_err("failed power off");
  243. #endif
  244. return 0;
  245. }
  246. int brl_resume(struct goodix_ts_core *cd)
  247. {
  248. int ret = 0;
  249. #ifdef GOODIX_SUSPEND_GESTURE_ENABLE
  250. ret = cd->hw_ops->reset(cd, GOODIX_NORMAL_RESET_DELAY_MS);
  251. #else
  252. ret = cd->hw_ops->power_on(cd, 1);
  253. if (ret) {
  254. ts_err("failed power on");
  255. return ret;
  256. }
  257. #endif
  258. return ret;
  259. }
  260. #define GOODIX_GESTURE_CMD_BA 0x12
  261. #define GOODIX_GESTURE_CMD 0xA6
  262. int brl_gesture(struct goodix_ts_core *cd, int gesture_type)
  263. {
  264. struct goodix_ts_cmd cmd;
  265. if (cd->bus->ic_type == IC_TYPE_BERLIN_A)
  266. cmd.cmd = GOODIX_GESTURE_CMD_BA;
  267. else
  268. cmd.cmd = GOODIX_GESTURE_CMD;
  269. cmd.len = 5;
  270. cmd.data[0] = gesture_type;
  271. if (cd->hw_ops->send_cmd(cd, &cmd))
  272. ts_err("failed send gesture cmd");
  273. return 0;
  274. }
  275. static int brl_reset(struct goodix_ts_core *cd, int delay)
  276. {
  277. ts_info("chip_reset");
  278. gpio_direction_output(cd->board_data.reset_gpio, 0);
  279. usleep_range(2000, 2100);
  280. gpio_direction_output(cd->board_data.reset_gpio, 1);
  281. if (delay < 20)
  282. usleep_range(delay * 1000, delay * 1000 + 100);
  283. else
  284. msleep(delay);
  285. return brl_select_spi_mode(cd);
  286. }
  287. static int brl_irq_enbale(struct goodix_ts_core *cd, bool enable)
  288. {
  289. if (enable && !atomic_cmpxchg(&cd->irq_enabled, 0, 1)) {
  290. enable_irq(cd->irq);
  291. ts_debug("Irq enabled");
  292. return 0;
  293. }
  294. if (!enable && atomic_cmpxchg(&cd->irq_enabled, 1, 0)) {
  295. disable_irq(cd->irq);
  296. ts_debug("Irq disabled");
  297. return 0;
  298. }
  299. ts_info("warnning: irq deepth inbalance!");
  300. return 0;
  301. }
  302. static int brl_read(struct goodix_ts_core *cd, unsigned int addr,
  303. unsigned char *data, unsigned int len)
  304. {
  305. struct goodix_bus_interface *bus = cd->bus;
  306. return bus->read(bus->dev, addr, data, len);
  307. }
  308. static int brl_write(struct goodix_ts_core *cd, unsigned int addr,
  309. unsigned char *data, unsigned int len)
  310. {
  311. struct goodix_bus_interface *bus = cd->bus;
  312. return bus->write(bus->dev, addr, data, len);
  313. }
  314. /* command ack info */
  315. #define CMD_ACK_IDLE 0x01
  316. #define CMD_ACK_BUSY 0x02
  317. #define CMD_ACK_BUFFER_OVERFLOW 0x03
  318. #define CMD_ACK_CHECKSUM_ERROR 0x04
  319. #define CMD_ACK_OK 0x80
  320. #define GOODIX_CMD_RETRY 6
  321. static int brl_send_cmd(struct goodix_ts_core *cd,
  322. struct goodix_ts_cmd *cmd)
  323. {
  324. int ret, retry, i;
  325. struct goodix_ts_cmd cmd_ack;
  326. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  327. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  328. cmd->state = 0;
  329. cmd->ack = 0;
  330. goodix_append_checksum(&(cmd->buf[2]), cmd->len - 2,
  331. CHECKSUM_MODE_U8_LE);
  332. ts_debug("cmd data %*ph", cmd->len, &(cmd->buf[2]));
  333. retry = 0;
  334. while (retry++ < GOODIX_CMD_RETRY) {
  335. ret = hw_ops->write(cd, misc->cmd_addr,
  336. cmd->buf, sizeof(*cmd));
  337. if (ret < 0) {
  338. ts_err("failed write command");
  339. return ret;
  340. }
  341. for (i = 0; i < GOODIX_CMD_RETRY; i++) {
  342. /* check command result */
  343. ret = hw_ops->read(cd, misc->cmd_addr,
  344. cmd_ack.buf, sizeof(cmd_ack));
  345. if (ret < 0) {
  346. ts_err("failed read command ack, %d", ret);
  347. return ret;
  348. }
  349. ts_debug("cmd ack data %*ph",
  350. (int)sizeof(cmd_ack), cmd_ack.buf);
  351. if (cmd_ack.ack == CMD_ACK_OK) {
  352. msleep(40); // wait for cmd response
  353. return 0;
  354. }
  355. if (cmd_ack.ack == CMD_ACK_BUSY ||
  356. cmd_ack.ack == 0x00) {
  357. usleep_range(1000, 1100);
  358. continue;
  359. }
  360. if (cmd_ack.ack == CMD_ACK_BUFFER_OVERFLOW)
  361. usleep_range(10000, 11000);
  362. usleep_range(1000, 1100);
  363. break;
  364. }
  365. }
  366. ts_err("failed get valid cmd ack");
  367. return -EINVAL;
  368. }
  369. #pragma pack(1)
  370. struct goodix_config_head {
  371. union {
  372. struct {
  373. u8 panel_name[8];
  374. u8 fw_pid[8];
  375. u8 fw_vid[4];
  376. u8 project_name[8];
  377. u8 file_ver[2];
  378. u32 cfg_id;
  379. u8 cfg_ver;
  380. u8 cfg_time[8];
  381. u8 reserved[15];
  382. u8 flag;
  383. u16 cfg_len;
  384. u8 cfg_num;
  385. u16 checksum;
  386. };
  387. u8 buf[64];
  388. };
  389. };
  390. #pragma pack()
  391. #define CONFIG_CND_LEN 4
  392. #define CONFIG_CMD_START 0x04
  393. #define CONFIG_CMD_WRITE 0x05
  394. #define CONFIG_CMD_EXIT 0x06
  395. #define CONFIG_CMD_READ_START 0x07
  396. #define CONFIG_CMD_READ_EXIT 0x08
  397. #define CONFIG_CMD_STATUS_PASS 0x80
  398. #define CONFIG_CMD_WAIT_RETRY 20
  399. static int wait_cmd_status(struct goodix_ts_core *cd,
  400. u8 target_status, int retry)
  401. {
  402. struct goodix_ts_cmd cmd_ack;
  403. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  404. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  405. int i, ret;
  406. for (i = 0; i < retry; i++) {
  407. ret = hw_ops->read(cd, misc->cmd_addr, cmd_ack.buf,
  408. sizeof(cmd_ack));
  409. if (!ret && cmd_ack.state == target_status) {
  410. ts_debug("status check pass");
  411. return 0;
  412. }
  413. ts_debug("cmd buf %*ph", (int)sizeof(cmd_ack), cmd_ack.buf);
  414. msleep(20);
  415. }
  416. ts_err("cmd status not ready, retry %d, ack 0x%x, status 0x%x, ret %d",
  417. i, cmd_ack.ack, cmd_ack.state, ret);
  418. return -EINVAL;
  419. }
  420. static int send_cfg_cmd(struct goodix_ts_core *cd,
  421. struct goodix_ts_cmd *cfg_cmd)
  422. {
  423. int ret;
  424. ret = cd->hw_ops->send_cmd(cd, cfg_cmd);
  425. if (ret) {
  426. ts_err("failed write cfg prepare cmd %d", ret);
  427. return ret;
  428. }
  429. ret = wait_cmd_status(cd, CONFIG_CMD_STATUS_PASS,
  430. CONFIG_CMD_WAIT_RETRY);
  431. if (ret) {
  432. ts_err("failed wait for fw ready for config, %d", ret);
  433. return ret;
  434. }
  435. return 0;
  436. }
  437. static int brl_send_config(struct goodix_ts_core *cd, u8 *cfg, int len)
  438. {
  439. int ret;
  440. u8 *tmp_buf;
  441. u16 cfg_head_len = sizeof(struct goodix_config_head) / sizeof(u8);
  442. struct goodix_ts_cmd cfg_cmd;
  443. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  444. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  445. struct goodix_config_head *cfg_head = (struct goodix_config_head *)cfg;
  446. if (!cd || !cfg) {
  447. ts_err("input parameter is NULL");
  448. return -EINVAL;
  449. } else if (len > misc->fw_buffer_max_len) {
  450. ts_err("config len exceed limit %d > %d",
  451. len, misc->fw_buffer_max_len);
  452. return -EINVAL;
  453. } else if (len < cfg_head_len) {
  454. ts_err("config buffer size %d smaller than header size %d",
  455. len, cfg_head_len);
  456. return -EINVAL;
  457. } else if (len != cfg_head_len + cfg_head->cfg_len) {
  458. ts_err("config buffer size %d not equal to head %d + cfg_len %d",
  459. len, cfg_head_len, cfg_head->cfg_len);
  460. return -EINVAL;
  461. } else if (checksum_cmp(cfg, cfg_head_len, CHECKSUM_MODE_U8_LE)) {
  462. ts_err("config head checksum error");
  463. return -EINVAL;
  464. } else if (checksum_cmp(cfg + cfg_head_len, cfg_head->cfg_len, CHECKSUM_MODE_U16_LE)) {
  465. ts_err("config body checksum error");
  466. return -EINVAL;
  467. }
  468. tmp_buf = kzalloc(len, GFP_KERNEL);
  469. if (!tmp_buf)
  470. return -ENOMEM;
  471. cfg_cmd.len = CONFIG_CND_LEN;
  472. cfg_cmd.cmd = CONFIG_CMD_START;
  473. ret = send_cfg_cmd(cd, &cfg_cmd);
  474. if (ret) {
  475. ts_err("failed write cfg prepare cmd %d", ret);
  476. goto exit;
  477. }
  478. ts_debug("try send config to 0x%x, len %d", misc->fw_buffer_addr, len);
  479. ret = hw_ops->write(cd, misc->fw_buffer_addr, cfg, len);
  480. if (ret) {
  481. ts_err("failed write config data, %d", ret);
  482. goto exit;
  483. }
  484. ret = hw_ops->read(cd, misc->fw_buffer_addr, tmp_buf, len);
  485. if (ret) {
  486. ts_err("failed read back config data");
  487. goto exit;
  488. }
  489. if (memcmp(cfg, tmp_buf, len)) {
  490. ts_err("config data read back compare file");
  491. ret = -EINVAL;
  492. goto exit;
  493. }
  494. /* notify fw for recive config */
  495. memset(cfg_cmd.buf, 0, sizeof(cfg_cmd));
  496. cfg_cmd.len = CONFIG_CND_LEN;
  497. cfg_cmd.cmd = CONFIG_CMD_WRITE;
  498. ret = send_cfg_cmd(cd, &cfg_cmd);
  499. if (ret)
  500. ts_err("failed send config data ready cmd %d", ret);
  501. exit:
  502. memset(cfg_cmd.buf, 0, sizeof(cfg_cmd));
  503. cfg_cmd.len = CONFIG_CND_LEN;
  504. cfg_cmd.cmd = CONFIG_CMD_EXIT;
  505. if (send_cfg_cmd(cd, &cfg_cmd)) {
  506. ts_err("failed send config write end command");
  507. ret = -EINVAL;
  508. }
  509. if (!ret) {
  510. ts_info("success send config");
  511. msleep(100);
  512. }
  513. kfree(tmp_buf);
  514. return ret;
  515. }
  516. /*
  517. * return: return config length on succes, other wise return < 0
  518. **/
  519. static int brl_read_config(struct goodix_ts_core *cd, u8 *cfg, int size)
  520. {
  521. int ret;
  522. struct goodix_ts_cmd cfg_cmd;
  523. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  524. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  525. struct goodix_config_head cfg_head;
  526. if (!cfg || sizeof(cfg_head) > size)
  527. return -EINVAL;
  528. cfg_cmd.len = CONFIG_CND_LEN;
  529. cfg_cmd.cmd = CONFIG_CMD_READ_START;
  530. ret = send_cfg_cmd(cd, &cfg_cmd);
  531. if (ret) {
  532. ts_err("failed send config read prepare command");
  533. return ret;
  534. }
  535. ret = hw_ops->read(cd, misc->fw_buffer_addr,
  536. cfg_head.buf, sizeof(cfg_head));
  537. if (ret) {
  538. ts_err("failed read config head %d", ret);
  539. goto exit;
  540. }
  541. if (checksum_cmp(cfg_head.buf, sizeof(cfg_head), CHECKSUM_MODE_U8_LE)) {
  542. ts_err("config head checksum error");
  543. ret = -EINVAL;
  544. goto exit;
  545. }
  546. cfg_head.cfg_len = le16_to_cpu(cfg_head.cfg_len);
  547. if (cfg_head.cfg_len > misc->fw_buffer_max_len ||
  548. cfg_head.cfg_len > size) {
  549. ts_err("cfg len exceed buffer size %d > %d", cfg_head.cfg_len,
  550. misc->fw_buffer_max_len);
  551. ret = -EINVAL;
  552. goto exit;
  553. }
  554. memcpy(cfg, cfg_head.buf, sizeof(cfg_head));
  555. ret = hw_ops->read(cd, misc->fw_buffer_addr + sizeof(cfg_head),
  556. cfg + sizeof(cfg_head), cfg_head.cfg_len);
  557. if (ret) {
  558. ts_err("failed read cfg pack, %d", ret);
  559. goto exit;
  560. }
  561. ts_info("config len %d", cfg_head.cfg_len);
  562. if (checksum_cmp(cfg + sizeof(cfg_head),
  563. cfg_head.cfg_len, CHECKSUM_MODE_U16_LE)) {
  564. ts_err("config body checksum error");
  565. ret = -EINVAL;
  566. goto exit;
  567. }
  568. ts_info("success read config data: len %zu",
  569. cfg_head.cfg_len + sizeof(cfg_head));
  570. exit:
  571. memset(cfg_cmd.buf, 0, sizeof(cfg_cmd));
  572. cfg_cmd.len = CONFIG_CND_LEN;
  573. cfg_cmd.cmd = CONFIG_CMD_READ_EXIT;
  574. if (send_cfg_cmd(cd, &cfg_cmd)) {
  575. ts_err("failed send config read finish command");
  576. ret = -EINVAL;
  577. }
  578. if (ret)
  579. return -EINVAL;
  580. return cfg_head.cfg_len + sizeof(cfg_head);
  581. }
  582. /*
  583. * return: 0 for no error.
  584. * GOODIX_EBUS when encounter a bus error
  585. * GOODIX_ECHECKSUM version checksum error
  586. * GOODIX_EVERSION patch ID compare failed,
  587. * in this case the sensorID is valid.
  588. */
  589. static int brl_read_version(struct goodix_ts_core *cd,
  590. struct goodix_fw_version *version)
  591. {
  592. int ret, i;
  593. u32 fw_addr;
  594. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  595. u8 buf[sizeof(struct goodix_fw_version)] = {0};
  596. u8 temp_pid[8] = {0};
  597. if (cd->bus->ic_type == IC_TYPE_BERLIN_A)
  598. fw_addr = FW_VERSION_INFO_ADDR_BRA;
  599. else
  600. fw_addr = FW_VERSION_INFO_ADDR;
  601. for (i = 0; i < 2; i++) {
  602. ret = hw_ops->read(cd, fw_addr, buf, sizeof(buf));
  603. if (ret) {
  604. ts_info("read fw version: %d, retry %d", ret, i);
  605. ret = -GOODIX_EBUS;
  606. usleep_range(5000, 5100);
  607. continue;
  608. }
  609. if (!checksum_cmp(buf, sizeof(buf), CHECKSUM_MODE_U8_LE))
  610. break;
  611. ts_info("invalid fw version: checksum error!");
  612. ts_info("fw version:%*ph", (int)sizeof(buf), buf);
  613. ret = -GOODIX_ECHECKSUM;
  614. usleep_range(10000, 11000);
  615. }
  616. if (ret) {
  617. ts_err("failed get valied fw version");
  618. return ret;
  619. }
  620. memcpy(version, buf, sizeof(*version));
  621. memcpy(temp_pid, version->rom_pid, sizeof(version->rom_pid));
  622. ts_info("rom_pid:%s", temp_pid);
  623. ts_info("rom_vid:%*ph", (int)sizeof(version->rom_vid),
  624. version->rom_vid);
  625. ts_info("pid:%s", version->patch_pid);
  626. ts_info("vid:%*ph", (int)sizeof(version->patch_vid),
  627. version->patch_vid);
  628. ts_info("sensor_id:%d", version->sensor_id);
  629. return 0;
  630. }
  631. #define LE16_TO_CPU(x) (x = le16_to_cpu(x))
  632. #define LE32_TO_CPU(x) (x = le32_to_cpu(x))
  633. static int convert_ic_info(struct goodix_ic_info *info, const u8 *data)
  634. {
  635. int i;
  636. struct goodix_ic_info_version *version = &info->version;
  637. struct goodix_ic_info_feature *feature = &info->feature;
  638. struct goodix_ic_info_param *parm = &info->parm;
  639. struct goodix_ic_info_misc *misc = &info->misc;
  640. info->length = le16_to_cpup((__le16 *)data);
  641. data += 2;
  642. memcpy(version, data, sizeof(*version));
  643. version->config_id = le32_to_cpu(version->config_id);
  644. data += sizeof(struct goodix_ic_info_version);
  645. memcpy(feature, data, sizeof(*feature));
  646. feature->freqhop_feature =
  647. le16_to_cpu(feature->freqhop_feature);
  648. feature->calibration_feature =
  649. le16_to_cpu(feature->calibration_feature);
  650. feature->gesture_feature =
  651. le16_to_cpu(feature->gesture_feature);
  652. feature->side_touch_feature =
  653. le16_to_cpu(feature->side_touch_feature);
  654. feature->stylus_feature =
  655. le16_to_cpu(feature->stylus_feature);
  656. data += sizeof(struct goodix_ic_info_feature);
  657. parm->drv_num = *(data++);
  658. parm->sen_num = *(data++);
  659. parm->button_num = *(data++);
  660. parm->force_num = *(data++);
  661. parm->active_scan_rate_num = *(data++);
  662. if (parm->active_scan_rate_num > MAX_SCAN_RATE_NUM) {
  663. ts_err("invalid scan rate num %d > %d",
  664. parm->active_scan_rate_num, MAX_SCAN_RATE_NUM);
  665. return -EINVAL;
  666. }
  667. for (i = 0; i < parm->active_scan_rate_num; i++)
  668. parm->active_scan_rate[i] =
  669. le16_to_cpup((__le16 *)(data + i * 2));
  670. data += parm->active_scan_rate_num * 2;
  671. parm->mutual_freq_num = *(data++);
  672. if (parm->mutual_freq_num > MAX_SCAN_FREQ_NUM) {
  673. ts_err("invalid mntual freq num %d > %d",
  674. parm->mutual_freq_num, MAX_SCAN_FREQ_NUM);
  675. return -EINVAL;
  676. }
  677. for (i = 0; i < parm->mutual_freq_num; i++)
  678. parm->mutual_freq[i] =
  679. le16_to_cpup((__le16 *)(data + i * 2));
  680. data += parm->mutual_freq_num * 2;
  681. parm->self_tx_freq_num = *(data++);
  682. if (parm->self_tx_freq_num > MAX_SCAN_FREQ_NUM) {
  683. ts_err("invalid tx freq num %d > %d",
  684. parm->self_tx_freq_num, MAX_SCAN_FREQ_NUM);
  685. return -EINVAL;
  686. }
  687. for (i = 0; i < parm->self_tx_freq_num; i++)
  688. parm->self_tx_freq[i] =
  689. le16_to_cpup((__le16 *)(data + i * 2));
  690. data += parm->self_tx_freq_num * 2;
  691. parm->self_rx_freq_num = *(data++);
  692. if (parm->self_rx_freq_num > MAX_SCAN_FREQ_NUM) {
  693. ts_err("invalid rx freq num %d > %d",
  694. parm->self_rx_freq_num, MAX_SCAN_FREQ_NUM);
  695. return -EINVAL;
  696. }
  697. for (i = 0; i < parm->self_rx_freq_num; i++)
  698. parm->self_rx_freq[i] =
  699. le16_to_cpup((__le16 *)(data + i * 2));
  700. data += parm->self_rx_freq_num * 2;
  701. parm->stylus_freq_num = *(data++);
  702. if (parm->stylus_freq_num > MAX_FREQ_NUM_STYLUS) {
  703. ts_err("invalid stylus freq num %d > %d",
  704. parm->stylus_freq_num, MAX_FREQ_NUM_STYLUS);
  705. return -EINVAL;
  706. }
  707. for (i = 0; i < parm->stylus_freq_num; i++)
  708. parm->stylus_freq[i] =
  709. le16_to_cpup((__le16 *)(data + i * 2));
  710. data += parm->stylus_freq_num * 2;
  711. memcpy(misc, data, sizeof(*misc));
  712. misc->cmd_addr = le32_to_cpu(misc->cmd_addr);
  713. misc->cmd_max_len = le16_to_cpu(misc->cmd_max_len);
  714. misc->cmd_reply_addr = le32_to_cpu(misc->cmd_reply_addr);
  715. misc->cmd_reply_len = le16_to_cpu(misc->cmd_reply_len);
  716. misc->fw_state_addr = le32_to_cpu(misc->fw_state_addr);
  717. misc->fw_state_len = le16_to_cpu(misc->fw_state_len);
  718. misc->fw_buffer_addr = le32_to_cpu(misc->fw_buffer_addr);
  719. misc->fw_buffer_max_len = le16_to_cpu(misc->fw_buffer_max_len);
  720. misc->frame_data_addr = le32_to_cpu(misc->frame_data_addr);
  721. misc->frame_data_head_len = le16_to_cpu(misc->frame_data_head_len);
  722. misc->fw_attr_len = le16_to_cpu(misc->fw_attr_len);
  723. misc->fw_log_len = le16_to_cpu(misc->fw_log_len);
  724. misc->stylus_struct_len = le16_to_cpu(misc->stylus_struct_len);
  725. misc->mutual_struct_len = le16_to_cpu(misc->mutual_struct_len);
  726. misc->self_struct_len = le16_to_cpu(misc->self_struct_len);
  727. misc->noise_struct_len = le16_to_cpu(misc->noise_struct_len);
  728. misc->touch_data_addr = le32_to_cpu(misc->touch_data_addr);
  729. misc->touch_data_head_len = le16_to_cpu(misc->touch_data_head_len);
  730. misc->point_struct_len = le16_to_cpu(misc->point_struct_len);
  731. LE32_TO_CPU(misc->mutual_rawdata_addr);
  732. LE32_TO_CPU(misc->mutual_diffdata_addr);
  733. LE32_TO_CPU(misc->mutual_refdata_addr);
  734. LE32_TO_CPU(misc->self_rawdata_addr);
  735. LE32_TO_CPU(misc->self_diffdata_addr);
  736. LE32_TO_CPU(misc->self_refdata_addr);
  737. LE32_TO_CPU(misc->iq_rawdata_addr);
  738. LE32_TO_CPU(misc->iq_refdata_addr);
  739. LE32_TO_CPU(misc->im_rawdata_addr);
  740. LE16_TO_CPU(misc->im_readata_len);
  741. LE32_TO_CPU(misc->noise_rawdata_addr);
  742. LE16_TO_CPU(misc->noise_rawdata_len);
  743. LE32_TO_CPU(misc->stylus_rawdata_addr);
  744. LE16_TO_CPU(misc->stylus_rawdata_len);
  745. LE32_TO_CPU(misc->noise_data_addr);
  746. LE32_TO_CPU(misc->esd_addr);
  747. return 0;
  748. }
  749. static void print_ic_info(struct goodix_ic_info *ic_info)
  750. {
  751. struct goodix_ic_info_version *version = &ic_info->version;
  752. struct goodix_ic_info_feature *feature = &ic_info->feature;
  753. struct goodix_ic_info_param *parm = &ic_info->parm;
  754. struct goodix_ic_info_misc *misc = &ic_info->misc;
  755. ts_info("ic_info_length: %d",
  756. ic_info->length);
  757. ts_info("info_customer_id: 0x%01X",
  758. version->info_customer_id);
  759. ts_info("info_version_id: 0x%01X",
  760. version->info_version_id);
  761. ts_info("ic_die_id: 0x%01X",
  762. version->ic_die_id);
  763. ts_info("ic_version_id: 0x%01X",
  764. version->ic_version_id);
  765. ts_info("config_id: 0x%4X",
  766. version->config_id);
  767. ts_info("config_version: 0x%01X",
  768. version->config_version);
  769. ts_info("frame_data_customer_id: 0x%01X",
  770. version->frame_data_customer_id);
  771. ts_info("frame_data_version_id: 0x%01X",
  772. version->frame_data_version_id);
  773. ts_info("touch_data_customer_id: 0x%01X",
  774. version->touch_data_customer_id);
  775. ts_info("touch_data_version_id: 0x%01X",
  776. version->touch_data_version_id);
  777. ts_info("freqhop_feature: 0x%04X",
  778. feature->freqhop_feature);
  779. ts_info("calibration_feature: 0x%04X",
  780. feature->calibration_feature);
  781. ts_info("gesture_feature: 0x%04X",
  782. feature->gesture_feature);
  783. ts_info("side_touch_feature: 0x%04X",
  784. feature->side_touch_feature);
  785. ts_info("stylus_feature: 0x%04X",
  786. feature->stylus_feature);
  787. ts_info("Drv*Sen,Button,Force num: %d x %d, %d, %d",
  788. parm->drv_num, parm->sen_num,
  789. parm->button_num, parm->force_num);
  790. ts_info("Cmd: 0x%04X, %d",
  791. misc->cmd_addr, misc->cmd_max_len);
  792. ts_info("Cmd-Reply: 0x%04X, %d",
  793. misc->cmd_reply_addr, misc->cmd_reply_len);
  794. ts_info("FW-State: 0x%04X, %d",
  795. misc->fw_state_addr, misc->fw_state_len);
  796. ts_info("FW-Buffer: 0x%04X, %d",
  797. misc->fw_buffer_addr, misc->fw_buffer_max_len);
  798. ts_info("Touch-Data: 0x%04X, %d",
  799. misc->touch_data_addr, misc->touch_data_head_len);
  800. ts_info("point_struct_len: %d",
  801. misc->point_struct_len);
  802. ts_info("mutual_rawdata_addr: 0x%04X",
  803. misc->mutual_rawdata_addr);
  804. ts_info("mutual_diffdata_addr: 0x%04X",
  805. misc->mutual_diffdata_addr);
  806. ts_info("self_rawdata_addr: 0x%04X",
  807. misc->self_rawdata_addr);
  808. ts_info("self_diffdata_addr: 0x%04X",
  809. misc->self_diffdata_addr);
  810. ts_info("stylus_rawdata_addr: 0x%04X, %d",
  811. misc->stylus_rawdata_addr, misc->stylus_rawdata_len);
  812. ts_info("esd_addr: 0x%04X",
  813. misc->esd_addr);
  814. }
  815. static int brl_get_ic_info(struct goodix_ts_core *cd,
  816. struct goodix_ic_info *ic_info)
  817. {
  818. int ret, i;
  819. u16 length = 0;
  820. u32 ic_addr;
  821. u8 afe_data[GOODIX_IC_INFO_MAX_LEN] = {0};
  822. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  823. if (cd->bus->ic_type == IC_TYPE_BERLIN_A)
  824. ic_addr = GOODIX_IC_INFO_ADDR_BRA;
  825. else
  826. ic_addr = GOODIX_IC_INFO_ADDR;
  827. for (i = 0; i < GOODIX_RETRY_3; i++) {
  828. ret = hw_ops->read(cd, ic_addr,
  829. (u8 *)&length, sizeof(length));
  830. if (ret) {
  831. ts_info("failed get ic info length, %d", ret);
  832. usleep_range(5000, 5100);
  833. continue;
  834. }
  835. length = le16_to_cpu(length);
  836. if (length >= GOODIX_IC_INFO_MAX_LEN) {
  837. ts_info("invalid ic info length %d, retry %d",
  838. length, i);
  839. continue;
  840. }
  841. ret = hw_ops->read(cd, ic_addr, afe_data, length);
  842. if (ret) {
  843. ts_info("failed get ic info data, %d", ret);
  844. usleep_range(5000, 5100);
  845. continue;
  846. }
  847. /* judge whether the data is valid */
  848. if (is_risk_data((const uint8_t *)afe_data, length)) {
  849. ts_info("fw info data invalid");
  850. usleep_range(5000, 5100);
  851. continue;
  852. }
  853. if (checksum_cmp((const uint8_t *)afe_data,
  854. length, CHECKSUM_MODE_U8_LE)) {
  855. ts_info("fw info checksum error!");
  856. usleep_range(5000, 5100);
  857. continue;
  858. }
  859. break;
  860. }
  861. if (i == GOODIX_RETRY_3) {
  862. ts_err("failed get ic info");
  863. return -EINVAL;
  864. }
  865. ret = convert_ic_info(ic_info, afe_data);
  866. if (ret) {
  867. ts_err("convert ic info encounter error");
  868. return ret;
  869. }
  870. print_ic_info(ic_info);
  871. /* check some key info */
  872. if (!ic_info->misc.cmd_addr || !ic_info->misc.fw_buffer_addr ||
  873. !ic_info->misc.touch_data_addr) {
  874. ts_err("cmd_addr fw_buf_addr and touch_data_addr is null");
  875. return -EINVAL;
  876. }
  877. return 0;
  878. }
  879. #define GOODIX_ESD_TICK_WRITE_DATA 0xAA
  880. static int brl_esd_check(struct goodix_ts_core *cd)
  881. {
  882. int ret;
  883. u32 esd_addr;
  884. u8 esd_value;
  885. if (!cd->ic_info.misc.esd_addr)
  886. return 0;
  887. esd_addr = cd->ic_info.misc.esd_addr;
  888. ret = cd->hw_ops->read(cd, esd_addr, &esd_value, 1);
  889. if (ret) {
  890. ts_err("failed get esd value, %d", ret);
  891. return ret;
  892. }
  893. if (esd_value == GOODIX_ESD_TICK_WRITE_DATA) {
  894. ts_err("esd check failed, 0x%x", esd_value);
  895. return -EINVAL;
  896. }
  897. esd_value = GOODIX_ESD_TICK_WRITE_DATA;
  898. ret = cd->hw_ops->write(cd, esd_addr, &esd_value, 1);
  899. if (ret) {
  900. ts_err("failed refrash esd value");
  901. return ret;
  902. }
  903. return 0;
  904. }
  905. #define IRQ_EVENT_HEAD_LEN 8
  906. #define BYTES_PER_POINT 8
  907. #define COOR_DATA_CHECKSUM_SIZE 2
  908. #define GOODIX_TOUCH_EVENT 0x80
  909. #define GOODIX_REQUEST_EVENT 0x40
  910. #define GOODIX_GESTURE_EVENT 0x20
  911. #define POINT_TYPE_STYLUS_HOVER 0x01
  912. #define POINT_TYPE_STYLUS 0x03
  913. static void goodix_parse_finger(struct goodix_touch_data *touch_data,
  914. u8 *buf, int touch_num)
  915. {
  916. unsigned int id = 0, x = 0, y = 0, w = 0;
  917. u8 *coor_data;
  918. int i;
  919. coor_data = &buf[IRQ_EVENT_HEAD_LEN];
  920. for (i = 0; i < touch_num; i++) {
  921. id = (coor_data[0] >> 4) & 0x0F;
  922. if (id >= GOODIX_MAX_TOUCH) {
  923. ts_info("invalid finger id =%d", id);
  924. touch_data->touch_num = 0;
  925. return;
  926. }
  927. x = le16_to_cpup((__le16 *)(coor_data + 2));
  928. y = le16_to_cpup((__le16 *)(coor_data + 4));
  929. w = le16_to_cpup((__le16 *)(coor_data + 6));
  930. touch_data->coords[id].status = TS_TOUCH;
  931. touch_data->coords[id].x = x;
  932. touch_data->coords[id].y = y;
  933. touch_data->coords[id].w = w;
  934. coor_data += BYTES_PER_POINT;
  935. }
  936. touch_data->touch_num = touch_num;
  937. }
  938. static unsigned int goodix_pen_btn_code[] = {BTN_STYLUS, BTN_STYLUS2};
  939. static void goodix_parse_pen(struct goodix_pen_data *pen_data,
  940. u8 *buf, int touch_num)
  941. {
  942. unsigned int id = 0;
  943. u8 cur_key_map = 0;
  944. u8 *coor_data;
  945. int16_t x_angle, y_angle;
  946. int i;
  947. pen_data->coords.tool_type = BTN_TOOL_PEN;
  948. if (touch_num) {
  949. pen_data->coords.status = TS_TOUCH;
  950. coor_data = &buf[IRQ_EVENT_HEAD_LEN];
  951. id = (coor_data[0] >> 4) & 0x0F;
  952. pen_data->coords.x = le16_to_cpup((__le16 *)(coor_data + 2));
  953. pen_data->coords.y = le16_to_cpup((__le16 *)(coor_data + 4));
  954. pen_data->coords.p = le16_to_cpup((__le16 *)(coor_data + 6));
  955. x_angle = le16_to_cpup((__le16 *)(coor_data + 8));
  956. y_angle = le16_to_cpup((__le16 *)(coor_data + 10));
  957. pen_data->coords.tilt_x = x_angle / 100;
  958. pen_data->coords.tilt_y = y_angle / 100;
  959. } else {
  960. pen_data->coords.status = TS_RELEASE;
  961. }
  962. cur_key_map = (buf[3] & 0x0F) >> 1;
  963. for (i = 0; i < GOODIX_MAX_PEN_KEY; i++) {
  964. pen_data->keys[i].code = goodix_pen_btn_code[i];
  965. if (!(cur_key_map & (1 << i)))
  966. continue;
  967. pen_data->keys[i].status = TS_TOUCH;
  968. }
  969. }
  970. static int goodix_touch_handler(struct goodix_ts_core *cd,
  971. struct goodix_ts_event *ts_event,
  972. u8 *pre_buf, u32 pre_buf_len)
  973. {
  974. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  975. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  976. struct goodix_touch_data *touch_data = &ts_event->touch_data;
  977. struct goodix_pen_data *pen_data = &ts_event->pen_data;
  978. static u8 buffer[IRQ_EVENT_HEAD_LEN +
  979. BYTES_PER_POINT * GOODIX_MAX_TOUCH + 2];
  980. u8 touch_num = 0;
  981. int ret = 0;
  982. u8 point_type = 0;
  983. static u8 pre_finger_num;
  984. static u8 pre_pen_num;
  985. /* clean event buffer */
  986. memset(ts_event, 0, sizeof(*ts_event));
  987. /* copy pre-data to buffer */
  988. memcpy(buffer, pre_buf, pre_buf_len);
  989. touch_num = buffer[2] & 0x0F;
  990. if (touch_num > GOODIX_MAX_TOUCH) {
  991. ts_debug("invalid touch num %d", touch_num);
  992. return -EINVAL;
  993. }
  994. if (unlikely(touch_num > 2)) {
  995. ret = hw_ops->read(cd,
  996. misc->touch_data_addr + pre_buf_len,
  997. &buffer[pre_buf_len],
  998. (touch_num - 2) * BYTES_PER_POINT);
  999. if (ret) {
  1000. ts_debug("failed get touch data");
  1001. return ret;
  1002. }
  1003. }
  1004. /* read done */
  1005. hw_ops->after_event_handler(cd);
  1006. if (touch_num > 0) {
  1007. point_type = buffer[IRQ_EVENT_HEAD_LEN] & 0x0F;
  1008. if (point_type == POINT_TYPE_STYLUS ||
  1009. point_type == POINT_TYPE_STYLUS_HOVER) {
  1010. ret = checksum_cmp(&buffer[IRQ_EVENT_HEAD_LEN],
  1011. BYTES_PER_POINT * 2 + 2,
  1012. CHECKSUM_MODE_U8_LE);
  1013. if (ret) {
  1014. ts_debug("touch data checksum error");
  1015. ts_debug("data:%*ph", BYTES_PER_POINT * 2 + 2,
  1016. &buffer[IRQ_EVENT_HEAD_LEN]);
  1017. return -EINVAL;
  1018. }
  1019. } else {
  1020. ret = checksum_cmp(&buffer[IRQ_EVENT_HEAD_LEN],
  1021. touch_num * BYTES_PER_POINT + 2,
  1022. CHECKSUM_MODE_U8_LE);
  1023. if (ret) {
  1024. ts_debug("touch data checksum error");
  1025. ts_debug("data:%*ph",
  1026. touch_num * BYTES_PER_POINT + 2,
  1027. &buffer[IRQ_EVENT_HEAD_LEN]);
  1028. return -EINVAL;
  1029. }
  1030. }
  1031. }
  1032. if (touch_num > 0 && (point_type == POINT_TYPE_STYLUS
  1033. || point_type == POINT_TYPE_STYLUS_HOVER)) {
  1034. /* stylus info */
  1035. if (pre_finger_num) {
  1036. ts_event->event_type = EVENT_TOUCH;
  1037. goodix_parse_finger(touch_data, buffer, 0);
  1038. pre_finger_num = 0;
  1039. } else {
  1040. pre_pen_num = 1;
  1041. ts_event->event_type = EVENT_PEN;
  1042. goodix_parse_pen(pen_data, buffer, touch_num);
  1043. }
  1044. } else {
  1045. /* finger info */
  1046. if (pre_pen_num) {
  1047. ts_event->event_type = EVENT_PEN;
  1048. goodix_parse_pen(pen_data, buffer, 0);
  1049. pre_pen_num = 0;
  1050. } else {
  1051. ts_event->event_type = EVENT_TOUCH;
  1052. goodix_parse_finger(touch_data, buffer, touch_num);
  1053. pre_finger_num = touch_num;
  1054. }
  1055. }
  1056. /* process custom info */
  1057. if (buffer[3] & 0x01)
  1058. ts_debug("TODO add custom info process function");
  1059. return 0;
  1060. }
  1061. static int brl_event_handler(struct goodix_ts_core *cd,
  1062. struct goodix_ts_event *ts_event)
  1063. {
  1064. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  1065. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  1066. int pre_read_len;
  1067. u8 pre_buf[32];
  1068. u8 event_status;
  1069. int ret;
  1070. pre_read_len = IRQ_EVENT_HEAD_LEN +
  1071. BYTES_PER_POINT * 2 + COOR_DATA_CHECKSUM_SIZE;
  1072. ret = hw_ops->read(cd, misc->touch_data_addr,
  1073. pre_buf, pre_read_len);
  1074. if (ret) {
  1075. ts_debug("failed get event head data");
  1076. return ret;
  1077. }
  1078. if (pre_buf[0] == 0x00) {
  1079. ts_debug("invalid touch head");
  1080. return -EINVAL;
  1081. }
  1082. if (checksum_cmp(pre_buf, IRQ_EVENT_HEAD_LEN, CHECKSUM_MODE_U8_LE)) {
  1083. ts_debug("touch head checksum err[%*ph]",
  1084. IRQ_EVENT_HEAD_LEN, pre_buf);
  1085. return -EINVAL;
  1086. }
  1087. event_status = pre_buf[0];
  1088. if (event_status & GOODIX_TOUCH_EVENT)
  1089. return goodix_touch_handler(cd, ts_event,
  1090. pre_buf, pre_read_len);
  1091. if (event_status & GOODIX_REQUEST_EVENT) {
  1092. ts_event->event_type = EVENT_REQUEST;
  1093. if (pre_buf[2] == BRL_REQUEST_CODE_CONFIG)
  1094. ts_event->request_code = REQUEST_TYPE_CONFIG;
  1095. else if (pre_buf[2] == BRL_REQUEST_CODE_RESET)
  1096. ts_event->request_code = REQUEST_TYPE_RESET;
  1097. else
  1098. ts_debug("unsupported request code 0x%x", pre_buf[2]);
  1099. }
  1100. if (event_status & GOODIX_GESTURE_EVENT) {
  1101. ts_event->event_type = EVENT_GESTURE;
  1102. ts_event->gesture_type = pre_buf[4];
  1103. memcpy(ts_event->gesture_data, &pre_buf[8],
  1104. GOODIX_GESTURE_DATA_LEN);
  1105. }
  1106. /* read done */
  1107. hw_ops->after_event_handler(cd);
  1108. return 0;
  1109. }
  1110. static int brl_after_event_handler(struct goodix_ts_core *cd)
  1111. {
  1112. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  1113. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  1114. u8 sync_clean = 0;
  1115. if (cd->tools_ctrl_sync)
  1116. return 0;
  1117. return hw_ops->write(cd, misc->touch_data_addr,
  1118. &sync_clean, 1);
  1119. }
  1120. static int brld_get_framedata(struct goodix_ts_core *cd,
  1121. struct ts_rawdata_info *info)
  1122. {
  1123. int ret;
  1124. unsigned char val;
  1125. int retry = 20;
  1126. struct frame_head *frame_head;
  1127. unsigned char frame_buf[GOODIX_MAX_FRAMEDATA_LEN];
  1128. unsigned char *cur_ptr;
  1129. unsigned int flag_addr = cd->ic_info.misc.frame_data_addr;
  1130. /* clean touch event flag */
  1131. val = 0;
  1132. ret = brl_write(cd, flag_addr, &val, 1);
  1133. if (ret < 0) {
  1134. ts_err("clean touch event failed, exit!");
  1135. return ret;
  1136. }
  1137. while (retry--) {
  1138. usleep_range(2000, 2100);
  1139. ret = brl_read(cd, flag_addr, &val, 1);
  1140. if (!ret && (val & GOODIX_TOUCH_EVENT))
  1141. break;
  1142. }
  1143. if (retry < 0) {
  1144. ts_err("framedata is not ready val:0x%02x, exit!", val);
  1145. return -EINVAL;
  1146. }
  1147. ret = brl_read(cd, flag_addr, frame_buf, GOODIX_MAX_FRAMEDATA_LEN);
  1148. if (ret < 0) {
  1149. ts_err("read frame data failed");
  1150. return ret;
  1151. }
  1152. if (checksum_cmp(frame_buf, cd->ic_info.misc.frame_data_head_len,
  1153. CHECKSUM_MODE_U8_LE)) {
  1154. ts_err("frame head checksum error");
  1155. return -EINVAL;
  1156. }
  1157. frame_head = (struct frame_head *)frame_buf;
  1158. if (checksum_cmp(frame_buf, frame_head->cur_frame_len,
  1159. CHECKSUM_MODE_U16_LE)) {
  1160. ts_err("frame body checksum error");
  1161. return -EINVAL;
  1162. }
  1163. cur_ptr = frame_buf;
  1164. cur_ptr += cd->ic_info.misc.frame_data_head_len;
  1165. cur_ptr += cd->ic_info.misc.fw_attr_len;
  1166. cur_ptr += cd->ic_info.misc.fw_log_len;
  1167. memcpy((u8 *)(info->buff + info->used_size), cur_ptr + 8,
  1168. cd->ic_info.misc.mutual_struct_len - 8);
  1169. return 0;
  1170. }
  1171. static int brld_get_cap_data(struct goodix_ts_core *cd,
  1172. struct ts_rawdata_info *info)
  1173. {
  1174. struct goodix_ts_cmd temp_cmd;
  1175. int tx = cd->ic_info.parm.drv_num;
  1176. int rx = cd->ic_info.parm.sen_num;
  1177. int size = tx * rx;
  1178. int ret;
  1179. /* disable irq & close esd */
  1180. brl_irq_enbale(cd, false);
  1181. goodix_ts_blocking_notify(NOTIFY_ESD_OFF, NULL);
  1182. info->buff[0] = rx;
  1183. info->buff[1] = tx;
  1184. info->used_size = 2;
  1185. temp_cmd.cmd = 0x90;
  1186. temp_cmd.data[0] = 0x81;
  1187. temp_cmd.len = 5;
  1188. ret = brl_send_cmd(cd, &temp_cmd);
  1189. if (ret < 0) {
  1190. ts_err("report rawdata failed, exit!");
  1191. goto exit;
  1192. }
  1193. ret = brld_get_framedata(cd, info);
  1194. if (ret < 0) {
  1195. ts_err("brld get rawdata failed");
  1196. goto exit;
  1197. }
  1198. goodix_rotate_abcd2cbad(tx, rx, &info->buff[info->used_size]);
  1199. info->used_size += size;
  1200. temp_cmd.cmd = 0x90;
  1201. temp_cmd.data[0] = 0x82;
  1202. temp_cmd.len = 5;
  1203. ret = brl_send_cmd(cd, &temp_cmd);
  1204. if (ret < 0) {
  1205. ts_err("report diffdata failed, exit!");
  1206. goto exit;
  1207. }
  1208. ret = brld_get_framedata(cd, info);
  1209. if (ret < 0) {
  1210. ts_err("brld get diffdata failed");
  1211. goto exit;
  1212. }
  1213. goodix_rotate_abcd2cbad(tx, rx, &info->buff[info->used_size]);
  1214. info->used_size += size;
  1215. exit:
  1216. temp_cmd.cmd = 0x90;
  1217. temp_cmd.data[0] = 0;
  1218. temp_cmd.len = 5;
  1219. brl_send_cmd(cd, &temp_cmd);
  1220. /* enable irq & esd */
  1221. brl_irq_enbale(cd, true);
  1222. goodix_ts_blocking_notify(NOTIFY_ESD_ON, NULL);
  1223. return ret;
  1224. }
  1225. #define GOODIX_CMD_RAWDATA 2
  1226. #define GOODIX_CMD_COORD 0
  1227. static int brl_get_capacitance_data(struct goodix_ts_core *cd,
  1228. struct ts_rawdata_info *info)
  1229. {
  1230. int ret;
  1231. int retry = 20;
  1232. struct goodix_ts_cmd temp_cmd;
  1233. u32 flag_addr = cd->ic_info.misc.touch_data_addr;
  1234. u32 raw_addr = cd->ic_info.misc.mutual_rawdata_addr;
  1235. u32 diff_addr = cd->ic_info.misc.mutual_diffdata_addr;
  1236. int tx = cd->ic_info.parm.drv_num;
  1237. int rx = cd->ic_info.parm.sen_num;
  1238. int size = tx * rx;
  1239. u8 val;
  1240. if (!info) {
  1241. ts_err("input null ptr");
  1242. return -EIO;
  1243. }
  1244. if (cd->bus->ic_type == IC_TYPE_BERLIN_D)
  1245. return brld_get_cap_data(cd, info);
  1246. /* disable irq & close esd */
  1247. brl_irq_enbale(cd, false);
  1248. goodix_ts_blocking_notify(NOTIFY_ESD_OFF, NULL);
  1249. /* switch rawdata mode */
  1250. temp_cmd.cmd = GOODIX_CMD_RAWDATA;
  1251. temp_cmd.len = 4;
  1252. ret = brl_send_cmd(cd, &temp_cmd);
  1253. if (ret < 0) {
  1254. ts_err("switch rawdata mode failed, exit!");
  1255. goto exit;
  1256. }
  1257. /* clean touch event flag */
  1258. val = 0;
  1259. ret = brl_write(cd, flag_addr, &val, 1);
  1260. if (ret < 0) {
  1261. ts_err("clean touch event failed, exit!");
  1262. goto exit;
  1263. }
  1264. while (retry--) {
  1265. usleep_range(5000, 5100);
  1266. ret = brl_read(cd, flag_addr, &val, 1);
  1267. if (!ret && (val & GOODIX_TOUCH_EVENT))
  1268. break;
  1269. }
  1270. if (retry < 0) {
  1271. ts_err("rawdata is not ready val:0x%02x, exit!", val);
  1272. goto exit;
  1273. }
  1274. /* obtain rawdata & diff_rawdata */
  1275. info->buff[0] = rx;
  1276. info->buff[1] = tx;
  1277. info->used_size = 2;
  1278. ret = brl_read(cd, raw_addr, (u8 *)&info->buff[info->used_size],
  1279. size * sizeof(s16));
  1280. if (ret < 0) {
  1281. ts_err("obtian raw_data failed, exit!");
  1282. goto exit;
  1283. }
  1284. goodix_rotate_abcd2cbad(tx, rx, &info->buff[info->used_size]);
  1285. info->used_size += size;
  1286. ret = brl_read(cd, diff_addr, (u8 *)&info->buff[info->used_size],
  1287. size * sizeof(s16));
  1288. if (ret < 0) {
  1289. ts_err("obtian diff_data failed, exit!");
  1290. goto exit;
  1291. }
  1292. goodix_rotate_abcd2cbad(tx, rx, &info->buff[info->used_size]);
  1293. info->used_size += size;
  1294. exit:
  1295. /* switch coor mode */
  1296. temp_cmd.cmd = GOODIX_CMD_COORD;
  1297. temp_cmd.len = 4;
  1298. brl_send_cmd(cd, &temp_cmd);
  1299. /* clean touch event flag */
  1300. val = 0;
  1301. brl_write(cd, flag_addr, &val, 1);
  1302. /* enable irq & esd */
  1303. brl_irq_enbale(cd, true);
  1304. goodix_ts_blocking_notify(NOTIFY_ESD_ON, NULL);
  1305. return ret;
  1306. }
  1307. static struct goodix_ts_hw_ops brl_hw_ops = {
  1308. .power_on = brl_power_on,
  1309. .resume = brl_resume,
  1310. .suspend = brl_suspend,
  1311. .gesture = brl_gesture,
  1312. .reset = brl_reset,
  1313. .irq_enable = brl_irq_enbale,
  1314. .read = brl_read,
  1315. .write = brl_write,
  1316. .send_cmd = brl_send_cmd,
  1317. .send_config = brl_send_config,
  1318. .read_config = brl_read_config,
  1319. .read_version = brl_read_version,
  1320. .get_ic_info = brl_get_ic_info,
  1321. .esd_check = brl_esd_check,
  1322. .event_handler = brl_event_handler,
  1323. .after_event_handler = brl_after_event_handler,
  1324. .get_capacitance_data = brl_get_capacitance_data,
  1325. };
  1326. struct goodix_ts_hw_ops *goodix_get_hw_ops(void)
  1327. {
  1328. return &brl_hw_ops;
  1329. }