drv_interface.c 13 KB

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