drv_interface.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* drv_interface.c
  2. *
  3. * Raydium TouchScreen driver.
  4. *
  5. * Copyright (c) 2021 Raydium tech Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/timer.h>
  19. #include <linux/delay.h>
  20. #include <linux/unistd.h>
  21. #include <linux/string.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/version.h>
  24. #include "chip_raydium/ic_drv_global.h"
  25. #include "chip_raydium/ic_drv_interface.h"
  26. #include "drv_interface.h"
  27. #include "raydium_selftest.h"
  28. #include "raydium_driver.h"
  29. #include "chip_raydium/f303_ic_control.h"
  30. struct timeval timer;
  31. unsigned char g_u8_m_buf[2][128];
  32. unsigned char g_u8_ini_flash[0x400];
  33. struct raydium_ts_data *ts;
  34. unsigned char g_u8_mute_i2c_err_log;
  35. STATUS i2c_burst_read_pda2(unsigned char u8_addr, unsigned short u16ReadLen, unsigned char *p_u8_output_buf)
  36. {
  37. return ERROR;
  38. }
  39. STATUS i2c_burst_write_pda2(unsigned char u8_addr, unsigned char bWriteLen, unsigned char *bValue)
  40. {
  41. return ERROR;
  42. }
  43. unsigned char spi_write_pda(unsigned int u32_addr, unsigned char u8_write_len, unsigned char *bValue, unsigned char u8_trans_mode)
  44. {
  45. return ERROR;
  46. }
  47. unsigned char spi_read_pda(unsigned int u32_addr, unsigned char u8_read_len, unsigned char *p_u8_output_buf)
  48. {
  49. return ERROR;
  50. }
  51. STATUS burn_fw_3x(void)
  52. {
  53. g_u32_wearable_test_result &= ~(WEARABLE_FT_TEST_RESULT_CMD_NG | WEARABLE_FT_TEST_RESULT_BURN_FW_NG);
  54. return SUCCESS;
  55. }
  56. unsigned char fw_upgrade(unsigned char type)
  57. {
  58. int i32_ret = ERROR;
  59. i32_ret = raydium_burn_fw(g_raydium_ts->client);
  60. if (i32_ret < 0)
  61. pr_err("[touch]FW update fail:%d\n", i32_ret);
  62. return i32_ret;
  63. }
  64. unsigned char read_flash_data(unsigned int u32_addr, unsigned short u16_lenth)
  65. {
  66. unsigned int u32_data_offset;
  67. if (g_u16_dev_id == DEVICE_ID_2X) {
  68. u32_data_offset = u32_addr - 0x800;
  69. if (u32_addr < 0x8000) {
  70. if (u32_data_offset >= 0x8000 && u32_addr < F302_DONGLE_FLASH_INI_ADDR) {
  71. u32_data_offset = u32_data_offset - 0x6200;
  72. memcpy(g_u8_data_buf, g_rad_para_image + u32_data_offset, u16_lenth);
  73. } else if (u32_addr >= F302_DONGLE_FLASH_INI_ADDR) {
  74. u32_data_offset = u32_addr - F302_DONGLE_FLASH_INI_ADDR;
  75. memcpy(g_u8_data_buf, g_u8_ini_flash + u32_data_offset, u16_lenth);
  76. } else
  77. memcpy(g_u8_data_buf, g_rad_fw_image + u32_data_offset, u16_lenth);
  78. } else {
  79. u32_data_offset -= 0x8000;
  80. if (u32_data_offset >= 0x6200 && u32_addr < F302_DONGLE_FLASH_INI_ADDR) {
  81. u32_data_offset = u32_data_offset - 0x6200;
  82. memcpy(g_u8_data_buf, g_rad_testpara_image + u32_data_offset, u16_lenth);
  83. } else if (u32_addr >= F302_DONGLE_FLASH_INI_ADDR) {
  84. u32_data_offset = u32_addr - F302_DONGLE_FLASH_INI_ADDR;
  85. memcpy(g_u8_data_buf, g_u8_ini_flash + u32_data_offset, u16_lenth);
  86. } else {
  87. memcpy(g_u8_data_buf, g_rad_testfw_image + u32_data_offset, u16_lenth);
  88. }
  89. }
  90. return TRUE;
  91. } else if (g_u16_dev_id == DEVICE_ID_3X) {
  92. u32_data_offset = u32_addr - 0x800;
  93. if (u32_addr < 0x7800) {
  94. if (u32_data_offset >= 0x7300 && u32_addr < F303_DONGLE_FLASH_INI_ADDR) {
  95. u32_data_offset = u32_data_offset - 0x7300;
  96. memcpy(g_u8_data_buf, g_rad_para_image + u32_data_offset, u16_lenth);
  97. } else
  98. memcpy(g_u8_data_buf, g_rad_fw_image + u32_data_offset, u16_lenth);
  99. } else {
  100. u32_data_offset -= 0x7800;
  101. if (u32_data_offset >= 0x7300 && u32_addr < F303_DONGLE_FLASH_INI_ADDR) {
  102. u32_data_offset = u32_data_offset - 0x7300;
  103. memcpy(g_u8_data_buf, g_rad_testpara_image + u32_data_offset, u16_lenth);
  104. } else if (u32_addr >= F303_DONGLE_FLASH_INI_ADDR) {
  105. u32_data_offset = u32_addr - F303_DONGLE_FLASH_INI_ADDR;
  106. pr_info("ini addr 0x%x offset %d\r\n", u32_addr, u32_data_offset);
  107. memcpy(g_u8_data_buf, g_u8_ini_flash + u32_data_offset, u16_lenth);
  108. } else {
  109. memcpy(g_u8_data_buf, g_rad_testfw_image + u32_data_offset, u16_lenth);
  110. }
  111. }
  112. return TRUE;
  113. }
  114. return FALSE;
  115. }
  116. unsigned int get_system_time(void)
  117. {
  118. /* unsigned int u32_timer;
  119. * do_gettimeofday(&timer);
  120. * u32_timer = (timer.tv_sec % 1000) * 1000 + (timer.tv_usec / 1000);
  121. * return u32_timer;
  122. */
  123. #if (KERNEL_VERSION(5, 0, 0) <= LINUX_VERSION_CODE)
  124. struct timespec ts;
  125. getnstimeofday(&ts);
  126. return (ts.tv_sec*1000 + ts.tv_nsec/1000000);
  127. #else
  128. struct timeval tv;
  129. do_gettimeofday(&tv);
  130. return (tv.tv_sec*1000 + tv.tv_usec/1000);
  131. #endif
  132. }
  133. unsigned char gpio_touch_int_pin_state_access(void)
  134. {
  135. return gpio_get_value(ts->irq_gpio);
  136. }
  137. unsigned char gpio_touch_int_access(unsigned char u8_is_clear_flag)
  138. {
  139. unsigned char u8_flag = g_u8_raydium_flag;
  140. if (u8_is_clear_flag && u8_flag)
  141. g_u8_raydium_flag &= ~INT_FLAG;
  142. return u8_flag;
  143. }
  144. unsigned char sysfs_burn_cc_bl(void)
  145. {
  146. unsigned char ret = ERROR;
  147. DEBUGOUT("%s\r\n", __func__);
  148. ret = raydium_burn_comp(ts->client);
  149. return ret;
  150. }
  151. unsigned char raydium_upgrade_test_fw_2x(unsigned long ul_fw_addr)
  152. {
  153. int ret = ERROR;
  154. unsigned char u8_retry = 2;
  155. unsigned int u32_read;
  156. unsigned int u32_write;
  157. RETRY:
  158. gpio_touch_reset_pin_control(0);
  159. delay_ms(10);
  160. gpio_touch_reset_pin_control(1);
  161. delay_ms(2);
  162. u32_write = 0x00000030;
  163. handle_ic_write(0x50000918, 4, (unsigned char *)&u32_write, g_u8_drv_interface, I2C_WORD_MODE);
  164. if (raydium_load_test_fw(ts->client) == SUCCESS) {
  165. ret = SUCCESS;
  166. DEBUGOUT("### Raydium Load test FW SUCCESS ###\n");
  167. }
  168. handle_ic_read(0x6A04, 4, (unsigned char *)&u32_read, g_u8_drv_interface, I2C_WORD_MODE);
  169. if (u32_read != g_st_test_para_resv.u32_test_fw_version) {
  170. DEBUGOUT("Read FW version NG=0x%x:0x%x!!\r\n", u32_read, g_st_test_para_resv.u32_test_fw_version);
  171. goto ERROR_EXIT;
  172. }
  173. return ret;
  174. ERROR_EXIT:
  175. if (u8_retry) {
  176. u8_retry--;
  177. goto RETRY;
  178. }
  179. return ERROR;
  180. }
  181. unsigned char raydium_upgrade_test_fw_3x(unsigned long ul_fw_addr)
  182. {
  183. int ret = ERROR;
  184. unsigned char u8_retry = 2;
  185. unsigned int u32_read;
  186. RETRY:
  187. if (raydium_load_test_fw(ts->client) == SUCCESS) {
  188. ret = SUCCESS;
  189. DEBUGOUT("### Raydium Load test FW SUCCESS ###\n");
  190. }
  191. handle_ic_read(0x7B04, 4, (unsigned char *)&u32_read, g_u8_drv_interface, I2C_WORD_MODE);
  192. if (u32_read != g_st_test_para_resv.u32_test_fw_version) {
  193. DEBUGOUT("Read FW version NG=0x%x:0x%x!!\r\n", u32_read, g_st_test_para_resv.u32_test_fw_version);
  194. goto ERROR_EXIT;
  195. }
  196. return ret;
  197. ERROR_EXIT:
  198. if (u8_retry) {
  199. u8_retry--;
  200. goto RETRY;
  201. }
  202. return ERROR;
  203. }
  204. void gpio_touch_reset_pin_control(unsigned char u8_high)
  205. {
  206. if (u8_high)
  207. gpio_set_value(ts->rst_gpio, 1);
  208. else
  209. gpio_set_value(ts->rst_gpio, 0);
  210. return;
  211. }
  212. void gpio_touch_hw_reset(void)
  213. {
  214. gpio_touch_reset_pin_control(0);
  215. delay_ms(10);
  216. gpio_touch_reset_pin_control(1);
  217. }
  218. void set_raydium_ts_data(struct raydium_ts_data *ts_old)
  219. {
  220. ts = ts_old;
  221. }
  222. /******************************************************************************
  223. **Function name:handle_ic_read
  224. **
  225. **Descriptions:handle read data from ic
  226. **
  227. **parameters:u32_addr,Address
  228. ** u8_read_len,Read datalength
  229. ** p_u8_output_buf,Data buffer
  230. ** u8_interface,SPI or I2C
  231. ** u8_trans_modePDA2_MODE, PDA_WORD_MODE, PDA_BYTE_MODE, MCU_MODE
  232. **
  233. **Returned value:ERROR,SUCCESS
  234. **
  235. ******************************************************************************
  236. */
  237. unsigned char handle_ic_read(
  238. unsigned int u32_addr,
  239. unsigned short u8_read_len,
  240. unsigned char *p_u8_output_buf,
  241. unsigned char u8_interface,
  242. unsigned char u8_trans_mode)
  243. {
  244. if (u8_trans_mode == I2C_PDA2_MODE) {
  245. /*PDA2 MODE */
  246. if (raydium_i2c_pda2_read(g_raydium_ts->client, (unsigned char)u32_addr, p_u8_output_buf, u8_read_len) == ERROR) {
  247. DEBUGOUT("%s\r\n", __func__);
  248. return ERROR;
  249. }
  250. } else {
  251. /*PDA MODE*/
  252. if ((u8_trans_mode == I2C_WORD_MODE) && (u32_addr & 0x00000003)) {
  253. DEBUGOUT("[HRW] Handle Read Word ADDR Not Word Align!!\r\n");
  254. return ERROR;
  255. }
  256. if (u8_interface == SPI_INTERFACE) {
  257. if (spi_read_pda(u32_addr, u8_read_len, p_u8_output_buf) == ERROR) {
  258. DEBUGOUT("%s\r\n", __func__);
  259. return ERROR;
  260. }
  261. } else {
  262. if (g_u16_dev_id == DEVICE_ID_3X) {
  263. if ((u8_interface & I2C_PDA_MODE) != 0) {
  264. if (raydium_i2c_pda_read(g_raydium_ts->client, u32_addr, p_u8_output_buf, u8_read_len) == ERROR) {
  265. DEBUGOUT("%s\r\n", __func__);
  266. return ERROR;
  267. }
  268. } else {
  269. if (raydium_i2c_read_pda_via_pda2(g_raydium_ts->client, u32_addr, p_u8_output_buf, u8_read_len) == ERROR) {
  270. DEBUGOUT("%s\r\n", __func__);
  271. return ERROR;
  272. }
  273. }
  274. } else {
  275. if (raydium_i2c_pda_read(g_raydium_ts->client, u32_addr, p_u8_output_buf, u8_read_len) == ERROR) {
  276. DEBUGOUT("%s\r\n", __func__);
  277. return ERROR;
  278. }
  279. }
  280. }
  281. }
  282. return SUCCESS;
  283. }
  284. /******************************************************************************
  285. ** Function name:handle_ic_write
  286. **
  287. ** Descriptions:handle write data to ic
  288. **
  289. ** parameters:u32_addr,Address
  290. ** u8_write_len,datalength
  291. ** bValue,Data
  292. ** u8_interface,SPI or I2C
  293. ** u8_trans_modePDA2_MODE, PDA_WORD_MODE, PDA_BYTE_MODE, MCU_MODE
  294. **
  295. ** Returned value:ERROR,SUCCESS
  296. **
  297. ******************************************************************************/
  298. unsigned char handle_ic_write(
  299. unsigned int u32_addr,
  300. unsigned char u8_write_len,
  301. unsigned char *bValue,
  302. unsigned char u8_interface,
  303. unsigned char u8_trans_mode)
  304. {
  305. if (u8_trans_mode == I2C_PDA2_MODE) {
  306. /*PDA2 MODE*/
  307. if (raydium_i2c_pda2_write(g_raydium_ts->client, (unsigned char)u32_addr, bValue, u8_write_len) == ERROR) {
  308. DEBUGOUT("%s\r\n", __func__);
  309. return ERROR;
  310. }
  311. } else {
  312. /*PDA MODE*/
  313. if ((u8_trans_mode == I2C_WORD_MODE) && (u32_addr & 0x00000003)) {
  314. DEBUGOUT("[I2CRW] Handle Write Word ADDR Not Word Align!!\r\n");
  315. return ERROR;
  316. }
  317. if (u8_interface == SPI_INTERFACE) {
  318. switch (u8_trans_mode) {
  319. case I2C_BYTE_MODE:
  320. u8_trans_mode = SPI_BYTE_MODE;
  321. break;
  322. case I2C_WORD_MODE:
  323. u8_trans_mode = SPI_WORD_MODE;
  324. break;
  325. /* case I2C_MCU_MODE:
  326. *
  327. * u8_trans_mode = SPI_MCU_MODE;
  328. *
  329. * break;
  330. */
  331. case SPI_BYTE_MODE:
  332. case SPI_WORD_MODE:
  333. break;
  334. default:
  335. DEBUGOUT("%s%d\r\n", __func__, u8_trans_mode);
  336. return ERROR;
  337. }
  338. if (spi_write_pda(u32_addr, u8_write_len, bValue, u8_trans_mode) == ERROR) {
  339. DEBUGOUT("%s\r\n", __func__);
  340. return ERROR;
  341. }
  342. } else {
  343. if (g_u16_dev_id == DEVICE_ID_3X) {
  344. if ((u8_interface & I2C_PDA_MODE) != 0) {
  345. if (raydium_i2c_pda_write(g_raydium_ts->client, u32_addr, bValue, u8_write_len) == ERROR) {
  346. DEBUGOUT("%s\r\n", __func__);
  347. return ERROR;
  348. }
  349. } else {
  350. if (raydium_i2c_write_pda_via_pda2(g_raydium_ts->client, u32_addr, bValue, u8_write_len) == ERROR) {
  351. DEBUGOUT("%s\r\n", __func__);
  352. return ERROR;
  353. }
  354. }
  355. } else {
  356. if (raydium_i2c_pda_write(g_raydium_ts->client, u32_addr, bValue, u8_write_len) == ERROR) {
  357. DEBUGOUT("%s\r\n", __func__);
  358. return ERROR;
  359. }
  360. }
  361. }
  362. }
  363. return SUCCESS;
  364. }
  365. /******************************************************************************
  366. ** Function name:handle_display_write
  367. **
  368. ** Descriptions:handle write data to display
  369. **
  370. ** parameters:p_u8_data,Data
  371. ** u16DataLength,datalength
  372. **
  373. **
  374. ** Returned value:ERROR,SUCCESS
  375. **
  376. ******************************************************************************/
  377. unsigned char handle_display_write(
  378. unsigned char *p_u8_data,
  379. unsigned short u16DataLength)
  380. {
  381. if (WriteDriverByTouchMode(p_u8_data, u16DataLength) == ERROR) {
  382. DEBUGOUT("[HDW] WriteDriverByTouchMode NG!\r\n");
  383. return ERROR;
  384. }
  385. return SUCCESS;
  386. }