goodix_brl_hw.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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. ret = cd->hw_ops->reset(cd, GOODIX_NORMAL_RESET_DELAY_MS);
  258. if (ret) {
  259. ts_err("failed reset tp");
  260. return ret;
  261. }
  262. #endif
  263. return ret;
  264. }
  265. #define GOODIX_GESTURE_CMD_BA 0x12
  266. #define GOODIX_GESTURE_CMD 0xA6
  267. int brl_gesture(struct goodix_ts_core *cd, int gesture_type)
  268. {
  269. struct goodix_ts_cmd cmd;
  270. if (cd->bus->ic_type == IC_TYPE_BERLIN_A)
  271. cmd.cmd = GOODIX_GESTURE_CMD_BA;
  272. else
  273. cmd.cmd = GOODIX_GESTURE_CMD;
  274. cmd.len = 5;
  275. cmd.data[0] = gesture_type;
  276. if (cd->hw_ops->send_cmd(cd, &cmd))
  277. ts_err("failed send gesture cmd");
  278. return 0;
  279. }
  280. static int brl_reset(struct goodix_ts_core *cd, int delay)
  281. {
  282. ts_info("chip_reset");
  283. gpio_direction_output(cd->board_data.reset_gpio, 0);
  284. usleep_range(2000, 2100);
  285. gpio_direction_output(cd->board_data.reset_gpio, 1);
  286. if (delay < 20)
  287. usleep_range(delay * 1000, delay * 1000 + 100);
  288. else
  289. msleep(delay);
  290. return brl_select_spi_mode(cd);
  291. }
  292. static int brl_irq_enbale(struct goodix_ts_core *cd, bool enable)
  293. {
  294. if (enable && !atomic_cmpxchg(&cd->irq_enabled, 0, 1)) {
  295. enable_irq(cd->irq);
  296. ts_debug("Irq enabled");
  297. return 0;
  298. }
  299. if (!enable && atomic_cmpxchg(&cd->irq_enabled, 1, 0)) {
  300. disable_irq(cd->irq);
  301. ts_debug("Irq disabled");
  302. return 0;
  303. }
  304. ts_info("warnning: irq deepth inbalance!");
  305. return 0;
  306. }
  307. static int brl_read(struct goodix_ts_core *cd, unsigned int addr,
  308. unsigned char *data, unsigned int len)
  309. {
  310. struct goodix_bus_interface *bus = cd->bus;
  311. return bus->read(bus->dev, addr, data, len);
  312. }
  313. static int brl_write(struct goodix_ts_core *cd, unsigned int addr,
  314. unsigned char *data, unsigned int len)
  315. {
  316. struct goodix_bus_interface *bus = cd->bus;
  317. return bus->write(bus->dev, addr, data, len);
  318. }
  319. /* command ack info */
  320. #define CMD_ACK_IDLE 0x01
  321. #define CMD_ACK_BUSY 0x02
  322. #define CMD_ACK_BUFFER_OVERFLOW 0x03
  323. #define CMD_ACK_CHECKSUM_ERROR 0x04
  324. #define CMD_ACK_OK 0x80
  325. #define GOODIX_CMD_RETRY 6
  326. static int brl_send_cmd(struct goodix_ts_core *cd,
  327. struct goodix_ts_cmd *cmd)
  328. {
  329. int ret, retry, i;
  330. struct goodix_ts_cmd cmd_ack;
  331. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  332. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  333. cmd->state = 0;
  334. cmd->ack = 0;
  335. goodix_append_checksum(&(cmd->buf[2]), cmd->len - 2,
  336. CHECKSUM_MODE_U8_LE);
  337. ts_debug("cmd data %*ph", cmd->len, &(cmd->buf[2]));
  338. retry = 0;
  339. while (retry++ < GOODIX_CMD_RETRY) {
  340. ret = hw_ops->write(cd, misc->cmd_addr,
  341. cmd->buf, sizeof(*cmd));
  342. if (ret < 0) {
  343. ts_err("failed write command");
  344. return ret;
  345. }
  346. for (i = 0; i < GOODIX_CMD_RETRY; i++) {
  347. /* check command result */
  348. ret = hw_ops->read(cd, misc->cmd_addr,
  349. cmd_ack.buf, sizeof(cmd_ack));
  350. if (ret < 0) {
  351. ts_err("failed read command ack, %d", ret);
  352. return ret;
  353. }
  354. ts_debug("cmd ack data %*ph",
  355. (int)sizeof(cmd_ack), cmd_ack.buf);
  356. if (cmd_ack.ack == CMD_ACK_OK) {
  357. msleep(40); // wait for cmd response
  358. return 0;
  359. }
  360. if (cmd_ack.ack == CMD_ACK_BUSY ||
  361. cmd_ack.ack == 0x00) {
  362. usleep_range(1000, 1100);
  363. continue;
  364. }
  365. if (cmd_ack.ack == CMD_ACK_BUFFER_OVERFLOW)
  366. usleep_range(10000, 11000);
  367. usleep_range(1000, 1100);
  368. break;
  369. }
  370. }
  371. ts_err("failed get valid cmd ack");
  372. return -EINVAL;
  373. }
  374. #pragma pack(1)
  375. struct goodix_config_head {
  376. union {
  377. struct {
  378. u8 panel_name[8];
  379. u8 fw_pid[8];
  380. u8 fw_vid[4];
  381. u8 project_name[8];
  382. u8 file_ver[2];
  383. u32 cfg_id;
  384. u8 cfg_ver;
  385. u8 cfg_time[8];
  386. u8 reserved[15];
  387. u8 flag;
  388. u16 cfg_len;
  389. u8 cfg_num;
  390. u16 checksum;
  391. };
  392. u8 buf[64];
  393. };
  394. };
  395. #pragma pack()
  396. #define CONFIG_CND_LEN 4
  397. #define CONFIG_CMD_START 0x04
  398. #define CONFIG_CMD_WRITE 0x05
  399. #define CONFIG_CMD_EXIT 0x06
  400. #define CONFIG_CMD_READ_START 0x07
  401. #define CONFIG_CMD_READ_EXIT 0x08
  402. #define CONFIG_CMD_STATUS_PASS 0x80
  403. #define CONFIG_CMD_WAIT_RETRY 20
  404. static int wait_cmd_status(struct goodix_ts_core *cd,
  405. u8 target_status, int retry)
  406. {
  407. struct goodix_ts_cmd cmd_ack;
  408. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  409. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  410. int i, ret;
  411. for (i = 0; i < retry; i++) {
  412. ret = hw_ops->read(cd, misc->cmd_addr, cmd_ack.buf,
  413. sizeof(cmd_ack));
  414. if (!ret && cmd_ack.state == target_status) {
  415. ts_debug("status check pass");
  416. return 0;
  417. }
  418. ts_debug("cmd buf %*ph", (int)sizeof(cmd_ack), cmd_ack.buf);
  419. msleep(20);
  420. }
  421. ts_err("cmd status not ready, retry %d, ack 0x%x, status 0x%x, ret %d",
  422. i, cmd_ack.ack, cmd_ack.state, ret);
  423. return -EINVAL;
  424. }
  425. static int send_cfg_cmd(struct goodix_ts_core *cd,
  426. struct goodix_ts_cmd *cfg_cmd)
  427. {
  428. int ret;
  429. ret = cd->hw_ops->send_cmd(cd, cfg_cmd);
  430. if (ret) {
  431. ts_err("failed write cfg prepare cmd %d", ret);
  432. return ret;
  433. }
  434. ret = wait_cmd_status(cd, CONFIG_CMD_STATUS_PASS,
  435. CONFIG_CMD_WAIT_RETRY);
  436. if (ret) {
  437. ts_err("failed wait for fw ready for config, %d", ret);
  438. return ret;
  439. }
  440. return 0;
  441. }
  442. static int brl_send_config(struct goodix_ts_core *cd, u8 *cfg, int len)
  443. {
  444. int ret;
  445. u8 *tmp_buf;
  446. struct goodix_ts_cmd cfg_cmd;
  447. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  448. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  449. 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. }
  454. tmp_buf = kzalloc(len, GFP_KERNEL);
  455. if (!tmp_buf)
  456. return -ENOMEM;
  457. cfg_cmd.len = CONFIG_CND_LEN;
  458. cfg_cmd.cmd = CONFIG_CMD_START;
  459. ret = send_cfg_cmd(cd, &cfg_cmd);
  460. if (ret) {
  461. ts_err("failed write cfg prepare cmd %d", ret);
  462. goto exit;
  463. }
  464. ts_debug("try send config to 0x%x, len %d", misc->fw_buffer_addr, len);
  465. ret = hw_ops->write(cd, misc->fw_buffer_addr, cfg, len);
  466. if (ret) {
  467. ts_err("failed write config data, %d", ret);
  468. goto exit;
  469. }
  470. ret = hw_ops->read(cd, misc->fw_buffer_addr, tmp_buf, len);
  471. if (ret) {
  472. ts_err("failed read back config data");
  473. goto exit;
  474. }
  475. if (memcmp(cfg, tmp_buf, len)) {
  476. ts_err("config data read back compare file");
  477. ret = -EINVAL;
  478. goto exit;
  479. }
  480. /* notify fw for recive config */
  481. memset(cfg_cmd.buf, 0, sizeof(cfg_cmd));
  482. cfg_cmd.len = CONFIG_CND_LEN;
  483. cfg_cmd.cmd = CONFIG_CMD_WRITE;
  484. ret = send_cfg_cmd(cd, &cfg_cmd);
  485. if (ret)
  486. ts_err("failed send config data ready cmd %d", ret);
  487. exit:
  488. memset(cfg_cmd.buf, 0, sizeof(cfg_cmd));
  489. cfg_cmd.len = CONFIG_CND_LEN;
  490. cfg_cmd.cmd = CONFIG_CMD_EXIT;
  491. if (send_cfg_cmd(cd, &cfg_cmd)) {
  492. ts_err("failed send config write end command");
  493. ret = -EINVAL;
  494. }
  495. if (!ret) {
  496. ts_info("success send config");
  497. msleep(100);
  498. }
  499. kfree(tmp_buf);
  500. return ret;
  501. }
  502. /*
  503. * return: return config length on succes, other wise return < 0
  504. **/
  505. static int brl_read_config(struct goodix_ts_core *cd, u8 *cfg, int size)
  506. {
  507. int ret;
  508. struct goodix_ts_cmd cfg_cmd;
  509. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  510. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  511. struct goodix_config_head cfg_head;
  512. if (!cfg)
  513. return -EINVAL;
  514. cfg_cmd.len = CONFIG_CND_LEN;
  515. cfg_cmd.cmd = CONFIG_CMD_READ_START;
  516. ret = send_cfg_cmd(cd, &cfg_cmd);
  517. if (ret) {
  518. ts_err("failed send config read prepare command");
  519. return ret;
  520. }
  521. ret = hw_ops->read(cd, misc->fw_buffer_addr,
  522. cfg_head.buf, sizeof(cfg_head));
  523. if (ret) {
  524. ts_err("failed read config head %d", ret);
  525. goto exit;
  526. }
  527. if (checksum_cmp(cfg_head.buf, sizeof(cfg_head), CHECKSUM_MODE_U8_LE)) {
  528. ts_err("config head checksum error");
  529. ret = -EINVAL;
  530. goto exit;
  531. }
  532. cfg_head.cfg_len = le16_to_cpu(cfg_head.cfg_len);
  533. if (cfg_head.cfg_len > misc->fw_buffer_max_len ||
  534. cfg_head.cfg_len > size) {
  535. ts_err("cfg len exceed buffer size %d > %d", cfg_head.cfg_len,
  536. misc->fw_buffer_max_len);
  537. ret = -EINVAL;
  538. goto exit;
  539. }
  540. memcpy(cfg, cfg_head.buf, sizeof(cfg_head));
  541. ret = hw_ops->read(cd, misc->fw_buffer_addr + sizeof(cfg_head),
  542. cfg + sizeof(cfg_head), cfg_head.cfg_len);
  543. if (ret) {
  544. ts_err("failed read cfg pack, %d", ret);
  545. goto exit;
  546. }
  547. ts_info("config len %d", cfg_head.cfg_len);
  548. if (checksum_cmp(cfg + sizeof(cfg_head),
  549. cfg_head.cfg_len, CHECKSUM_MODE_U16_LE)) {
  550. ts_err("config body checksum error");
  551. ret = -EINVAL;
  552. goto exit;
  553. }
  554. ts_info("success read config data: len %zu",
  555. cfg_head.cfg_len + sizeof(cfg_head));
  556. exit:
  557. memset(cfg_cmd.buf, 0, sizeof(cfg_cmd));
  558. cfg_cmd.len = CONFIG_CND_LEN;
  559. cfg_cmd.cmd = CONFIG_CMD_READ_EXIT;
  560. if (send_cfg_cmd(cd, &cfg_cmd)) {
  561. ts_err("failed send config read finish command");
  562. ret = -EINVAL;
  563. }
  564. if (ret)
  565. return -EINVAL;
  566. return cfg_head.cfg_len + sizeof(cfg_head);
  567. }
  568. /*
  569. * return: 0 for no error.
  570. * GOODIX_EBUS when encounter a bus error
  571. * GOODIX_ECHECKSUM version checksum error
  572. * GOODIX_EVERSION patch ID compare failed,
  573. * in this case the sensorID is valid.
  574. */
  575. static int brl_read_version(struct goodix_ts_core *cd,
  576. struct goodix_fw_version *version)
  577. {
  578. int ret, i;
  579. u32 fw_addr;
  580. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  581. u8 buf[sizeof(struct goodix_fw_version)] = {0};
  582. u8 temp_pid[8] = {0};
  583. if (cd->bus->ic_type == IC_TYPE_BERLIN_A)
  584. fw_addr = FW_VERSION_INFO_ADDR_BRA;
  585. else
  586. fw_addr = FW_VERSION_INFO_ADDR;
  587. for (i = 0; i < 2; i++) {
  588. ret = hw_ops->read(cd, fw_addr, buf, sizeof(buf));
  589. if (ret) {
  590. ts_info("read fw version: %d, retry %d", ret, i);
  591. ret = -GOODIX_EBUS;
  592. usleep_range(5000, 5100);
  593. continue;
  594. }
  595. if (!checksum_cmp(buf, sizeof(buf), CHECKSUM_MODE_U8_LE))
  596. break;
  597. ts_info("invalid fw version: checksum error!");
  598. ts_info("fw version:%*ph", (int)sizeof(buf), buf);
  599. ret = -GOODIX_ECHECKSUM;
  600. usleep_range(10000, 11000);
  601. }
  602. if (ret) {
  603. ts_err("failed get valied fw version");
  604. return ret;
  605. }
  606. memcpy(version, buf, sizeof(*version));
  607. memcpy(temp_pid, version->rom_pid, sizeof(version->rom_pid));
  608. ts_info("rom_pid:%s", temp_pid);
  609. ts_info("rom_vid:%*ph", (int)sizeof(version->rom_vid),
  610. version->rom_vid);
  611. ts_info("pid:%s", version->patch_pid);
  612. ts_info("vid:%*ph", (int)sizeof(version->patch_vid),
  613. version->patch_vid);
  614. ts_info("sensor_id:%d", version->sensor_id);
  615. return 0;
  616. }
  617. #define LE16_TO_CPU(x) (x = le16_to_cpu(x))
  618. #define LE32_TO_CPU(x) (x = le32_to_cpu(x))
  619. static int convert_ic_info(struct goodix_ic_info *info, const u8 *data)
  620. {
  621. int i;
  622. struct goodix_ic_info_version *version = &info->version;
  623. struct goodix_ic_info_feature *feature = &info->feature;
  624. struct goodix_ic_info_param *parm = &info->parm;
  625. struct goodix_ic_info_misc *misc = &info->misc;
  626. info->length = le16_to_cpup((__le16 *)data);
  627. data += 2;
  628. memcpy(version, data, sizeof(*version));
  629. version->config_id = le32_to_cpu(version->config_id);
  630. data += sizeof(struct goodix_ic_info_version);
  631. memcpy(feature, data, sizeof(*feature));
  632. feature->freqhop_feature =
  633. le16_to_cpu(feature->freqhop_feature);
  634. feature->calibration_feature =
  635. le16_to_cpu(feature->calibration_feature);
  636. feature->gesture_feature =
  637. le16_to_cpu(feature->gesture_feature);
  638. feature->side_touch_feature =
  639. le16_to_cpu(feature->side_touch_feature);
  640. feature->stylus_feature =
  641. le16_to_cpu(feature->stylus_feature);
  642. data += sizeof(struct goodix_ic_info_feature);
  643. parm->drv_num = *(data++);
  644. parm->sen_num = *(data++);
  645. parm->button_num = *(data++);
  646. parm->force_num = *(data++);
  647. parm->active_scan_rate_num = *(data++);
  648. if (parm->active_scan_rate_num > MAX_SCAN_RATE_NUM) {
  649. ts_err("invalid scan rate num %d > %d",
  650. parm->active_scan_rate_num, MAX_SCAN_RATE_NUM);
  651. return -EINVAL;
  652. }
  653. for (i = 0; i < parm->active_scan_rate_num; i++)
  654. parm->active_scan_rate[i] =
  655. le16_to_cpup((__le16 *)(data + i * 2));
  656. data += parm->active_scan_rate_num * 2;
  657. parm->mutual_freq_num = *(data++);
  658. if (parm->mutual_freq_num > MAX_SCAN_FREQ_NUM) {
  659. ts_err("invalid mntual freq num %d > %d",
  660. parm->mutual_freq_num, MAX_SCAN_FREQ_NUM);
  661. return -EINVAL;
  662. }
  663. for (i = 0; i < parm->mutual_freq_num; i++)
  664. parm->mutual_freq[i] =
  665. le16_to_cpup((__le16 *)(data + i * 2));
  666. data += parm->mutual_freq_num * 2;
  667. parm->self_tx_freq_num = *(data++);
  668. if (parm->self_tx_freq_num > MAX_SCAN_FREQ_NUM) {
  669. ts_err("invalid tx freq num %d > %d",
  670. parm->self_tx_freq_num, MAX_SCAN_FREQ_NUM);
  671. return -EINVAL;
  672. }
  673. for (i = 0; i < parm->self_tx_freq_num; i++)
  674. parm->self_tx_freq[i] =
  675. le16_to_cpup((__le16 *)(data + i * 2));
  676. data += parm->self_tx_freq_num * 2;
  677. parm->self_rx_freq_num = *(data++);
  678. if (parm->self_rx_freq_num > MAX_SCAN_FREQ_NUM) {
  679. ts_err("invalid rx freq num %d > %d",
  680. parm->self_rx_freq_num, MAX_SCAN_FREQ_NUM);
  681. return -EINVAL;
  682. }
  683. for (i = 0; i < parm->self_rx_freq_num; i++)
  684. parm->self_rx_freq[i] =
  685. le16_to_cpup((__le16 *)(data + i * 2));
  686. data += parm->self_rx_freq_num * 2;
  687. parm->stylus_freq_num = *(data++);
  688. if (parm->stylus_freq_num > MAX_FREQ_NUM_STYLUS) {
  689. ts_err("invalid stylus freq num %d > %d",
  690. parm->stylus_freq_num, MAX_FREQ_NUM_STYLUS);
  691. return -EINVAL;
  692. }
  693. for (i = 0; i < parm->stylus_freq_num; i++)
  694. parm->stylus_freq[i] =
  695. le16_to_cpup((__le16 *)(data + i * 2));
  696. data += parm->stylus_freq_num * 2;
  697. memcpy(misc, data, sizeof(*misc));
  698. misc->cmd_addr = le32_to_cpu(misc->cmd_addr);
  699. misc->cmd_max_len = le16_to_cpu(misc->cmd_max_len);
  700. misc->cmd_reply_addr = le32_to_cpu(misc->cmd_reply_addr);
  701. misc->cmd_reply_len = le16_to_cpu(misc->cmd_reply_len);
  702. misc->fw_state_addr = le32_to_cpu(misc->fw_state_addr);
  703. misc->fw_state_len = le16_to_cpu(misc->fw_state_len);
  704. misc->fw_buffer_addr = le32_to_cpu(misc->fw_buffer_addr);
  705. misc->fw_buffer_max_len = le16_to_cpu(misc->fw_buffer_max_len);
  706. misc->frame_data_addr = le32_to_cpu(misc->frame_data_addr);
  707. misc->frame_data_head_len = le16_to_cpu(misc->frame_data_head_len);
  708. misc->fw_attr_len = le16_to_cpu(misc->fw_attr_len);
  709. misc->fw_log_len = le16_to_cpu(misc->fw_log_len);
  710. misc->stylus_struct_len = le16_to_cpu(misc->stylus_struct_len);
  711. misc->mutual_struct_len = le16_to_cpu(misc->mutual_struct_len);
  712. misc->self_struct_len = le16_to_cpu(misc->self_struct_len);
  713. misc->noise_struct_len = le16_to_cpu(misc->noise_struct_len);
  714. misc->touch_data_addr = le32_to_cpu(misc->touch_data_addr);
  715. misc->touch_data_head_len = le16_to_cpu(misc->touch_data_head_len);
  716. misc->point_struct_len = le16_to_cpu(misc->point_struct_len);
  717. LE32_TO_CPU(misc->mutual_rawdata_addr);
  718. LE32_TO_CPU(misc->mutual_diffdata_addr);
  719. LE32_TO_CPU(misc->mutual_refdata_addr);
  720. LE32_TO_CPU(misc->self_rawdata_addr);
  721. LE32_TO_CPU(misc->self_diffdata_addr);
  722. LE32_TO_CPU(misc->self_refdata_addr);
  723. LE32_TO_CPU(misc->iq_rawdata_addr);
  724. LE32_TO_CPU(misc->iq_refdata_addr);
  725. LE32_TO_CPU(misc->im_rawdata_addr);
  726. LE16_TO_CPU(misc->im_readata_len);
  727. LE32_TO_CPU(misc->noise_rawdata_addr);
  728. LE16_TO_CPU(misc->noise_rawdata_len);
  729. LE32_TO_CPU(misc->stylus_rawdata_addr);
  730. LE16_TO_CPU(misc->stylus_rawdata_len);
  731. LE32_TO_CPU(misc->noise_data_addr);
  732. LE32_TO_CPU(misc->esd_addr);
  733. return 0;
  734. }
  735. static void print_ic_info(struct goodix_ic_info *ic_info)
  736. {
  737. struct goodix_ic_info_version *version = &ic_info->version;
  738. struct goodix_ic_info_feature *feature = &ic_info->feature;
  739. struct goodix_ic_info_param *parm = &ic_info->parm;
  740. struct goodix_ic_info_misc *misc = &ic_info->misc;
  741. ts_info("ic_info_length: %d",
  742. ic_info->length);
  743. ts_info("info_customer_id: 0x%01X",
  744. version->info_customer_id);
  745. ts_info("info_version_id: 0x%01X",
  746. version->info_version_id);
  747. ts_info("ic_die_id: 0x%01X",
  748. version->ic_die_id);
  749. ts_info("ic_version_id: 0x%01X",
  750. version->ic_version_id);
  751. ts_info("config_id: 0x%4X",
  752. version->config_id);
  753. ts_info("config_version: 0x%01X",
  754. version->config_version);
  755. ts_info("frame_data_customer_id: 0x%01X",
  756. version->frame_data_customer_id);
  757. ts_info("frame_data_version_id: 0x%01X",
  758. version->frame_data_version_id);
  759. ts_info("touch_data_customer_id: 0x%01X",
  760. version->touch_data_customer_id);
  761. ts_info("touch_data_version_id: 0x%01X",
  762. version->touch_data_version_id);
  763. ts_info("freqhop_feature: 0x%04X",
  764. feature->freqhop_feature);
  765. ts_info("calibration_feature: 0x%04X",
  766. feature->calibration_feature);
  767. ts_info("gesture_feature: 0x%04X",
  768. feature->gesture_feature);
  769. ts_info("side_touch_feature: 0x%04X",
  770. feature->side_touch_feature);
  771. ts_info("stylus_feature: 0x%04X",
  772. feature->stylus_feature);
  773. ts_info("Drv*Sen,Button,Force num: %d x %d, %d, %d",
  774. parm->drv_num, parm->sen_num,
  775. parm->button_num, parm->force_num);
  776. ts_info("Cmd: 0x%04X, %d",
  777. misc->cmd_addr, misc->cmd_max_len);
  778. ts_info("Cmd-Reply: 0x%04X, %d",
  779. misc->cmd_reply_addr, misc->cmd_reply_len);
  780. ts_info("FW-State: 0x%04X, %d",
  781. misc->fw_state_addr, misc->fw_state_len);
  782. ts_info("FW-Buffer: 0x%04X, %d",
  783. misc->fw_buffer_addr, misc->fw_buffer_max_len);
  784. ts_info("Touch-Data: 0x%04X, %d",
  785. misc->touch_data_addr, misc->touch_data_head_len);
  786. ts_info("point_struct_len: %d",
  787. misc->point_struct_len);
  788. ts_info("mutual_rawdata_addr: 0x%04X",
  789. misc->mutual_rawdata_addr);
  790. ts_info("mutual_diffdata_addr: 0x%04X",
  791. misc->mutual_diffdata_addr);
  792. ts_info("self_rawdata_addr: 0x%04X",
  793. misc->self_rawdata_addr);
  794. ts_info("self_diffdata_addr: 0x%04X",
  795. misc->self_diffdata_addr);
  796. ts_info("stylus_rawdata_addr: 0x%04X, %d",
  797. misc->stylus_rawdata_addr, misc->stylus_rawdata_len);
  798. ts_info("esd_addr: 0x%04X",
  799. misc->esd_addr);
  800. }
  801. static int brl_get_ic_info(struct goodix_ts_core *cd,
  802. struct goodix_ic_info *ic_info)
  803. {
  804. int ret, i;
  805. u16 length = 0;
  806. u32 ic_addr;
  807. u8 afe_data[GOODIX_IC_INFO_MAX_LEN] = {0};
  808. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  809. if (cd->bus->ic_type == IC_TYPE_BERLIN_A)
  810. ic_addr = GOODIX_IC_INFO_ADDR_BRA;
  811. else
  812. ic_addr = GOODIX_IC_INFO_ADDR;
  813. for (i = 0; i < GOODIX_RETRY_3; i++) {
  814. ret = hw_ops->read(cd, ic_addr,
  815. (u8 *)&length, sizeof(length));
  816. if (ret) {
  817. ts_info("failed get ic info length, %d", ret);
  818. usleep_range(5000, 5100);
  819. continue;
  820. }
  821. length = le16_to_cpu(length);
  822. if (length >= GOODIX_IC_INFO_MAX_LEN) {
  823. ts_info("invalid ic info length %d, retry %d",
  824. length, i);
  825. continue;
  826. }
  827. ret = hw_ops->read(cd, ic_addr, afe_data, length);
  828. if (ret) {
  829. ts_info("failed get ic info data, %d", ret);
  830. usleep_range(5000, 5100);
  831. continue;
  832. }
  833. /* judge whether the data is valid */
  834. if (is_risk_data((const uint8_t *)afe_data, length)) {
  835. ts_info("fw info data invalid");
  836. usleep_range(5000, 5100);
  837. continue;
  838. }
  839. if (checksum_cmp((const uint8_t *)afe_data,
  840. length, CHECKSUM_MODE_U8_LE)) {
  841. ts_info("fw info checksum error!");
  842. usleep_range(5000, 5100);
  843. continue;
  844. }
  845. break;
  846. }
  847. if (i == GOODIX_RETRY_3) {
  848. ts_err("failed get ic info");
  849. return -EINVAL;
  850. }
  851. ret = convert_ic_info(ic_info, afe_data);
  852. if (ret) {
  853. ts_err("convert ic info encounter error");
  854. return ret;
  855. }
  856. print_ic_info(ic_info);
  857. /* check some key info */
  858. if (!ic_info->misc.cmd_addr || !ic_info->misc.fw_buffer_addr ||
  859. !ic_info->misc.touch_data_addr) {
  860. ts_err("cmd_addr fw_buf_addr and touch_data_addr is null");
  861. return -EINVAL;
  862. }
  863. return 0;
  864. }
  865. #define GOODIX_ESD_TICK_WRITE_DATA 0xAA
  866. static int brl_esd_check(struct goodix_ts_core *cd)
  867. {
  868. int ret;
  869. u32 esd_addr;
  870. u8 esd_value;
  871. if (!cd->ic_info.misc.esd_addr)
  872. return 0;
  873. esd_addr = cd->ic_info.misc.esd_addr;
  874. ret = cd->hw_ops->read(cd, esd_addr, &esd_value, 1);
  875. if (ret) {
  876. ts_err("failed get esd value, %d", ret);
  877. return ret;
  878. }
  879. if (esd_value == GOODIX_ESD_TICK_WRITE_DATA) {
  880. ts_err("esd check failed, 0x%x", esd_value);
  881. return -EINVAL;
  882. }
  883. esd_value = GOODIX_ESD_TICK_WRITE_DATA;
  884. ret = cd->hw_ops->write(cd, esd_addr, &esd_value, 1);
  885. if (ret) {
  886. ts_err("failed refrash esd value");
  887. return ret;
  888. }
  889. return 0;
  890. }
  891. #define IRQ_EVENT_HEAD_LEN 8
  892. #define BYTES_PER_POINT 8
  893. #define COOR_DATA_CHECKSUM_SIZE 2
  894. #define GOODIX_TOUCH_EVENT 0x80
  895. #define GOODIX_REQUEST_EVENT 0x40
  896. #define GOODIX_GESTURE_EVENT 0x20
  897. #define POINT_TYPE_STYLUS_HOVER 0x01
  898. #define POINT_TYPE_STYLUS 0x03
  899. static void goodix_parse_finger(struct goodix_touch_data *touch_data,
  900. u8 *buf, int touch_num)
  901. {
  902. unsigned int id = 0, x = 0, y = 0, w = 0;
  903. u8 *coor_data;
  904. int i;
  905. coor_data = &buf[IRQ_EVENT_HEAD_LEN];
  906. for (i = 0; i < touch_num; i++) {
  907. id = (coor_data[0] >> 4) & 0x0F;
  908. if (id >= GOODIX_MAX_TOUCH) {
  909. ts_info("invalid finger id =%d", id);
  910. touch_data->touch_num = 0;
  911. return;
  912. }
  913. x = le16_to_cpup((__le16 *)(coor_data + 2));
  914. y = le16_to_cpup((__le16 *)(coor_data + 4));
  915. w = le16_to_cpup((__le16 *)(coor_data + 6));
  916. touch_data->coords[id].status = TS_TOUCH;
  917. touch_data->coords[id].x = x;
  918. touch_data->coords[id].y = y;
  919. touch_data->coords[id].w = w;
  920. coor_data += BYTES_PER_POINT;
  921. }
  922. touch_data->touch_num = touch_num;
  923. }
  924. static unsigned int goodix_pen_btn_code[] = {BTN_STYLUS, BTN_STYLUS2};
  925. static void goodix_parse_pen(struct goodix_pen_data *pen_data,
  926. u8 *buf, int touch_num)
  927. {
  928. unsigned int id = 0;
  929. u8 cur_key_map = 0;
  930. u8 *coor_data;
  931. int16_t x_angle, y_angle;
  932. int i;
  933. pen_data->coords.tool_type = BTN_TOOL_PEN;
  934. if (touch_num) {
  935. pen_data->coords.status = TS_TOUCH;
  936. coor_data = &buf[IRQ_EVENT_HEAD_LEN];
  937. id = (coor_data[0] >> 4) & 0x0F;
  938. pen_data->coords.x = le16_to_cpup((__le16 *)(coor_data + 2));
  939. pen_data->coords.y = le16_to_cpup((__le16 *)(coor_data + 4));
  940. pen_data->coords.p = le16_to_cpup((__le16 *)(coor_data + 6));
  941. x_angle = le16_to_cpup((__le16 *)(coor_data + 8));
  942. y_angle = le16_to_cpup((__le16 *)(coor_data + 10));
  943. pen_data->coords.tilt_x = x_angle / 100;
  944. pen_data->coords.tilt_y = y_angle / 100;
  945. } else {
  946. pen_data->coords.status = TS_RELEASE;
  947. }
  948. cur_key_map = (buf[3] & 0x0F) >> 1;
  949. for (i = 0; i < GOODIX_MAX_PEN_KEY; i++) {
  950. pen_data->keys[i].code = goodix_pen_btn_code[i];
  951. if (!(cur_key_map & (1 << i)))
  952. continue;
  953. pen_data->keys[i].status = TS_TOUCH;
  954. }
  955. }
  956. static int goodix_touch_handler(struct goodix_ts_core *cd,
  957. struct goodix_ts_event *ts_event,
  958. u8 *pre_buf, u32 pre_buf_len)
  959. {
  960. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  961. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  962. struct goodix_touch_data *touch_data = &ts_event->touch_data;
  963. struct goodix_pen_data *pen_data = &ts_event->pen_data;
  964. static u8 buffer[IRQ_EVENT_HEAD_LEN +
  965. BYTES_PER_POINT * GOODIX_MAX_TOUCH + 2];
  966. u8 touch_num = 0;
  967. int ret = 0;
  968. u8 point_type = 0;
  969. static u8 pre_finger_num;
  970. static u8 pre_pen_num;
  971. /* clean event buffer */
  972. memset(ts_event, 0, sizeof(*ts_event));
  973. /* copy pre-data to buffer */
  974. memcpy(buffer, pre_buf, pre_buf_len);
  975. touch_num = buffer[2] & 0x0F;
  976. if (touch_num > GOODIX_MAX_TOUCH) {
  977. ts_debug("invalid touch num %d", touch_num);
  978. return -EINVAL;
  979. }
  980. if (unlikely(touch_num > 2)) {
  981. ret = hw_ops->read(cd,
  982. misc->touch_data_addr + pre_buf_len,
  983. &buffer[pre_buf_len],
  984. (touch_num - 2) * BYTES_PER_POINT);
  985. if (ret) {
  986. ts_debug("failed get touch data");
  987. return ret;
  988. }
  989. }
  990. /* read done */
  991. hw_ops->after_event_handler(cd);
  992. if (touch_num > 0) {
  993. point_type = buffer[IRQ_EVENT_HEAD_LEN] & 0x0F;
  994. if (point_type == POINT_TYPE_STYLUS ||
  995. point_type == POINT_TYPE_STYLUS_HOVER) {
  996. ret = checksum_cmp(&buffer[IRQ_EVENT_HEAD_LEN],
  997. BYTES_PER_POINT * 2 + 2,
  998. CHECKSUM_MODE_U8_LE);
  999. if (ret) {
  1000. ts_debug("touch data checksum error");
  1001. ts_debug("data:%*ph", BYTES_PER_POINT * 2 + 2,
  1002. &buffer[IRQ_EVENT_HEAD_LEN]);
  1003. return -EINVAL;
  1004. }
  1005. } else {
  1006. ret = checksum_cmp(&buffer[IRQ_EVENT_HEAD_LEN],
  1007. touch_num * BYTES_PER_POINT + 2,
  1008. CHECKSUM_MODE_U8_LE);
  1009. if (ret) {
  1010. ts_debug("touch data checksum error");
  1011. ts_debug("data:%*ph",
  1012. touch_num * BYTES_PER_POINT + 2,
  1013. &buffer[IRQ_EVENT_HEAD_LEN]);
  1014. return -EINVAL;
  1015. }
  1016. }
  1017. }
  1018. if (touch_num > 0 && (point_type == POINT_TYPE_STYLUS
  1019. || point_type == POINT_TYPE_STYLUS_HOVER)) {
  1020. /* stylus info */
  1021. if (pre_finger_num) {
  1022. ts_event->event_type = EVENT_TOUCH;
  1023. goodix_parse_finger(touch_data, buffer, 0);
  1024. pre_finger_num = 0;
  1025. } else {
  1026. pre_pen_num = 1;
  1027. ts_event->event_type = EVENT_PEN;
  1028. goodix_parse_pen(pen_data, buffer, touch_num);
  1029. }
  1030. } else {
  1031. /* finger info */
  1032. if (pre_pen_num) {
  1033. ts_event->event_type = EVENT_PEN;
  1034. goodix_parse_pen(pen_data, buffer, 0);
  1035. pre_pen_num = 0;
  1036. } else {
  1037. ts_event->event_type = EVENT_TOUCH;
  1038. goodix_parse_finger(touch_data, buffer, touch_num);
  1039. pre_finger_num = touch_num;
  1040. }
  1041. }
  1042. /* process custom info */
  1043. if (buffer[3] & 0x01)
  1044. ts_debug("TODO add custom info process function");
  1045. return 0;
  1046. }
  1047. static int brl_event_handler(struct goodix_ts_core *cd,
  1048. struct goodix_ts_event *ts_event)
  1049. {
  1050. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  1051. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  1052. int pre_read_len;
  1053. u8 pre_buf[32];
  1054. u8 event_status;
  1055. int ret;
  1056. pre_read_len = IRQ_EVENT_HEAD_LEN +
  1057. BYTES_PER_POINT * 2 + COOR_DATA_CHECKSUM_SIZE;
  1058. ret = hw_ops->read(cd, misc->touch_data_addr,
  1059. pre_buf, pre_read_len);
  1060. if (ret) {
  1061. ts_debug("failed get event head data");
  1062. return ret;
  1063. }
  1064. if (pre_buf[0] == 0x00) {
  1065. ts_debug("invalid touch head");
  1066. return -EINVAL;
  1067. }
  1068. if (checksum_cmp(pre_buf, IRQ_EVENT_HEAD_LEN, CHECKSUM_MODE_U8_LE)) {
  1069. ts_debug("touch head checksum err[%*ph]",
  1070. IRQ_EVENT_HEAD_LEN, pre_buf);
  1071. return -EINVAL;
  1072. }
  1073. event_status = pre_buf[0];
  1074. if (event_status & GOODIX_TOUCH_EVENT)
  1075. return goodix_touch_handler(cd, ts_event,
  1076. pre_buf, pre_read_len);
  1077. if (event_status & GOODIX_REQUEST_EVENT) {
  1078. ts_event->event_type = EVENT_REQUEST;
  1079. if (pre_buf[2] == BRL_REQUEST_CODE_CONFIG)
  1080. ts_event->request_code = REQUEST_TYPE_CONFIG;
  1081. else if (pre_buf[2] == BRL_REQUEST_CODE_RESET)
  1082. ts_event->request_code = REQUEST_TYPE_RESET;
  1083. else
  1084. ts_debug("unsupported request code 0x%x", pre_buf[2]);
  1085. }
  1086. if (event_status & GOODIX_GESTURE_EVENT) {
  1087. ts_event->event_type = EVENT_GESTURE;
  1088. ts_event->gesture_type = pre_buf[4];
  1089. memcpy(ts_event->gesture_data, &pre_buf[8],
  1090. GOODIX_GESTURE_DATA_LEN);
  1091. }
  1092. /* read done */
  1093. hw_ops->after_event_handler(cd);
  1094. return 0;
  1095. }
  1096. static int brl_after_event_handler(struct goodix_ts_core *cd)
  1097. {
  1098. struct goodix_ts_hw_ops *hw_ops = cd->hw_ops;
  1099. struct goodix_ic_info_misc *misc = &cd->ic_info.misc;
  1100. u8 sync_clean = 0;
  1101. if (cd->tools_ctrl_sync)
  1102. return 0;
  1103. return hw_ops->write(cd, misc->touch_data_addr,
  1104. &sync_clean, 1);
  1105. }
  1106. static int brld_get_framedata(struct goodix_ts_core *cd,
  1107. struct ts_rawdata_info *info)
  1108. {
  1109. int ret;
  1110. unsigned char val;
  1111. int retry = 20;
  1112. struct frame_head *frame_head;
  1113. unsigned char frame_buf[GOODIX_MAX_FRAMEDATA_LEN];
  1114. unsigned char *cur_ptr;
  1115. unsigned int flag_addr = cd->ic_info.misc.frame_data_addr;
  1116. /* clean touch event flag */
  1117. val = 0;
  1118. ret = brl_write(cd, flag_addr, &val, 1);
  1119. if (ret < 0) {
  1120. ts_err("clean touch event failed, exit!");
  1121. return ret;
  1122. }
  1123. while (retry--) {
  1124. usleep_range(2000, 2100);
  1125. ret = brl_read(cd, flag_addr, &val, 1);
  1126. if (!ret && (val & GOODIX_TOUCH_EVENT))
  1127. break;
  1128. }
  1129. if (retry < 0) {
  1130. ts_err("framedata is not ready val:0x%02x, exit!", val);
  1131. return -EINVAL;
  1132. }
  1133. ret = brl_read(cd, flag_addr, frame_buf, GOODIX_MAX_FRAMEDATA_LEN);
  1134. if (ret < 0) {
  1135. ts_err("read frame data failed");
  1136. return ret;
  1137. }
  1138. if (checksum_cmp(frame_buf, cd->ic_info.misc.frame_data_head_len,
  1139. CHECKSUM_MODE_U8_LE)) {
  1140. ts_err("frame head checksum error");
  1141. return -EINVAL;
  1142. }
  1143. frame_head = (struct frame_head *)frame_buf;
  1144. if (checksum_cmp(frame_buf, frame_head->cur_frame_len,
  1145. CHECKSUM_MODE_U16_LE)) {
  1146. ts_err("frame body checksum error");
  1147. return -EINVAL;
  1148. }
  1149. cur_ptr = frame_buf;
  1150. cur_ptr += cd->ic_info.misc.frame_data_head_len;
  1151. cur_ptr += cd->ic_info.misc.fw_attr_len;
  1152. cur_ptr += cd->ic_info.misc.fw_log_len;
  1153. memcpy((u8 *)(info->buff + info->used_size), cur_ptr + 8,
  1154. cd->ic_info.misc.mutual_struct_len - 8);
  1155. return 0;
  1156. }
  1157. static int brld_get_cap_data(struct goodix_ts_core *cd,
  1158. struct ts_rawdata_info *info)
  1159. {
  1160. struct goodix_ts_cmd temp_cmd;
  1161. int tx = cd->ic_info.parm.drv_num;
  1162. int rx = cd->ic_info.parm.sen_num;
  1163. int size = tx * rx;
  1164. int ret;
  1165. /* disable irq & close esd */
  1166. brl_irq_enbale(cd, false);
  1167. goodix_ts_blocking_notify(NOTIFY_ESD_OFF, NULL);
  1168. info->buff[0] = rx;
  1169. info->buff[1] = tx;
  1170. info->used_size = 2;
  1171. temp_cmd.cmd = 0x90;
  1172. temp_cmd.data[0] = 0x81;
  1173. temp_cmd.len = 5;
  1174. ret = brl_send_cmd(cd, &temp_cmd);
  1175. if (ret < 0) {
  1176. ts_err("report rawdata failed, exit!");
  1177. goto exit;
  1178. }
  1179. ret = brld_get_framedata(cd, info);
  1180. if (ret < 0) {
  1181. ts_err("brld get rawdata failed");
  1182. goto exit;
  1183. }
  1184. goodix_rotate_abcd2cbad(tx, rx, &info->buff[info->used_size]);
  1185. info->used_size += size;
  1186. temp_cmd.cmd = 0x90;
  1187. temp_cmd.data[0] = 0x82;
  1188. temp_cmd.len = 5;
  1189. ret = brl_send_cmd(cd, &temp_cmd);
  1190. if (ret < 0) {
  1191. ts_err("report diffdata failed, exit!");
  1192. goto exit;
  1193. }
  1194. ret = brld_get_framedata(cd, info);
  1195. if (ret < 0) {
  1196. ts_err("brld get diffdata failed");
  1197. goto exit;
  1198. }
  1199. goodix_rotate_abcd2cbad(tx, rx, &info->buff[info->used_size]);
  1200. info->used_size += size;
  1201. exit:
  1202. temp_cmd.cmd = 0x90;
  1203. temp_cmd.data[0] = 0;
  1204. temp_cmd.len = 5;
  1205. brl_send_cmd(cd, &temp_cmd);
  1206. /* enable irq & esd */
  1207. brl_irq_enbale(cd, true);
  1208. goodix_ts_blocking_notify(NOTIFY_ESD_ON, NULL);
  1209. return ret;
  1210. }
  1211. #define GOODIX_CMD_RAWDATA 2
  1212. #define GOODIX_CMD_COORD 0
  1213. static int brl_get_capacitance_data(struct goodix_ts_core *cd,
  1214. struct ts_rawdata_info *info)
  1215. {
  1216. int ret;
  1217. int retry = 20;
  1218. struct goodix_ts_cmd temp_cmd;
  1219. u32 flag_addr = cd->ic_info.misc.touch_data_addr;
  1220. u32 raw_addr = cd->ic_info.misc.mutual_rawdata_addr;
  1221. u32 diff_addr = cd->ic_info.misc.mutual_diffdata_addr;
  1222. int tx = cd->ic_info.parm.drv_num;
  1223. int rx = cd->ic_info.parm.sen_num;
  1224. int size = tx * rx;
  1225. u8 val;
  1226. if (!info) {
  1227. ts_err("input null ptr");
  1228. return -EIO;
  1229. }
  1230. if (cd->bus->ic_type == IC_TYPE_BERLIN_D)
  1231. return brld_get_cap_data(cd, info);
  1232. /* disable irq & close esd */
  1233. brl_irq_enbale(cd, false);
  1234. goodix_ts_blocking_notify(NOTIFY_ESD_OFF, NULL);
  1235. /* switch rawdata mode */
  1236. temp_cmd.cmd = GOODIX_CMD_RAWDATA;
  1237. temp_cmd.len = 4;
  1238. ret = brl_send_cmd(cd, &temp_cmd);
  1239. if (ret < 0) {
  1240. ts_err("switch rawdata mode failed, exit!");
  1241. goto exit;
  1242. }
  1243. /* clean touch event flag */
  1244. val = 0;
  1245. ret = brl_write(cd, flag_addr, &val, 1);
  1246. if (ret < 0) {
  1247. ts_err("clean touch event failed, exit!");
  1248. goto exit;
  1249. }
  1250. while (retry--) {
  1251. usleep_range(5000, 5100);
  1252. ret = brl_read(cd, flag_addr, &val, 1);
  1253. if (!ret && (val & GOODIX_TOUCH_EVENT))
  1254. break;
  1255. }
  1256. if (retry < 0) {
  1257. ts_err("rawdata is not ready val:0x%02x, exit!", val);
  1258. goto exit;
  1259. }
  1260. /* obtain rawdata & diff_rawdata */
  1261. info->buff[0] = rx;
  1262. info->buff[1] = tx;
  1263. info->used_size = 2;
  1264. ret = brl_read(cd, raw_addr, (u8 *)&info->buff[info->used_size],
  1265. size * sizeof(s16));
  1266. if (ret < 0) {
  1267. ts_err("obtian raw_data failed, exit!");
  1268. goto exit;
  1269. }
  1270. goodix_rotate_abcd2cbad(tx, rx, &info->buff[info->used_size]);
  1271. info->used_size += size;
  1272. ret = brl_read(cd, diff_addr, (u8 *)&info->buff[info->used_size],
  1273. size * sizeof(s16));
  1274. if (ret < 0) {
  1275. ts_err("obtian diff_data failed, exit!");
  1276. goto exit;
  1277. }
  1278. goodix_rotate_abcd2cbad(tx, rx, &info->buff[info->used_size]);
  1279. info->used_size += size;
  1280. exit:
  1281. /* switch coor mode */
  1282. temp_cmd.cmd = GOODIX_CMD_COORD;
  1283. temp_cmd.len = 4;
  1284. brl_send_cmd(cd, &temp_cmd);
  1285. /* clean touch event flag */
  1286. val = 0;
  1287. brl_write(cd, flag_addr, &val, 1);
  1288. /* enable irq & esd */
  1289. brl_irq_enbale(cd, true);
  1290. goodix_ts_blocking_notify(NOTIFY_ESD_ON, NULL);
  1291. return ret;
  1292. }
  1293. static struct goodix_ts_hw_ops brl_hw_ops = {
  1294. .power_on = brl_power_on,
  1295. .resume = brl_resume,
  1296. .suspend = brl_suspend,
  1297. .gesture = brl_gesture,
  1298. .reset = brl_reset,
  1299. .irq_enable = brl_irq_enbale,
  1300. .read = brl_read,
  1301. .write = brl_write,
  1302. .send_cmd = brl_send_cmd,
  1303. .send_config = brl_send_config,
  1304. .read_config = brl_read_config,
  1305. .read_version = brl_read_version,
  1306. .get_ic_info = brl_get_ic_info,
  1307. .esd_check = brl_esd_check,
  1308. .event_handler = brl_event_handler,
  1309. .after_event_handler = brl_after_event_handler,
  1310. .get_capacitance_data = brl_get_capacitance_data,
  1311. };
  1312. struct goodix_ts_hw_ops *goodix_get_hw_ops(void)
  1313. {
  1314. return &brl_hw_ops;
  1315. }