hl7005.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2017. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. *
  31. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #include <platform/mt_typedefs.h>
  36. #include <platform/mt_reg_base.h>
  37. #include <platform/errno.h>
  38. #include <platform/mt_i2c.h>
  39. #include <platform/mt_pmic.h>
  40. #include <platform/hl7005.h>
  41. #include <platform/mtk_charger_intf.h>
  42. #include <printf.h>
  43. #if !defined(CONFIG_POWER_EXT)
  44. #include <platform/upmu_common.h>
  45. #endif
  46. const unsigned int VBAT_CVTH[] = {
  47. 3500000, 3520000, 3540000, 3560000,
  48. 3580000, 3600000, 3620000, 3640000,
  49. 3660000, 3680000, 3700000, 3720000,
  50. 3740000, 3760000, 3780000, 3800000,
  51. 3820000, 3840000, 3860000, 3880000,
  52. 3900000, 3920000, 3940000, 3960000,
  53. 3980000, 4000000, 4020000, 4040000,
  54. 4060000, 4080000, 4100000, 4120000,
  55. 4140000, 4160000, 4180000, 4200000,
  56. 4220000, 4240000, 4260000, 4280000,
  57. 4300000, 4320000, 4340000, 4360000,
  58. 4380000, 4400000, 4420000, 4440000
  59. };
  60. const unsigned int CSTH[] = {
  61. 55000, 65000, 75000, 85000,
  62. 95000, 105000, 115000, 125000
  63. };
  64. /*hl7005 REG00 IINLIM[5:0]*/
  65. const unsigned int INPUT_CSTH[] = {
  66. 10000, 50000, 80000, 500000
  67. };
  68. /* hl7005 REG0A BOOST_LIM[2:0], mA */
  69. const unsigned int BOOST_CURRENT_LIMIT[] = {
  70. 500, 750, 1200, 1400, 1650, 1875, 2150,
  71. };
  72. #define HL7005_SLAVE_ADDR_WRITE 0xD4
  73. unsigned int charging_value_to_parameter(const unsigned int *parameter, const unsigned int array_size,
  74. const unsigned int val)
  75. {
  76. if (val < array_size)
  77. return parameter[val];
  78. dprintf(CRITICAL, "Can't find the parameter\n");
  79. return parameter[0];
  80. }
  81. unsigned int charging_parameter_to_value(const unsigned int *parameter, const unsigned int array_size,
  82. const unsigned int val)
  83. {
  84. unsigned int i;
  85. dprintf(CRITICAL, "array_size = %d\n", array_size);
  86. for (i = 0; i < array_size; i++) {
  87. if (val == *(parameter + i))
  88. return i;
  89. }
  90. dprintf(CRITICAL, "NO register value match\n");
  91. /* TODO: ASSERT(0); // not find the value */
  92. return 0;
  93. }
  94. static unsigned int bmt_find_closest_level(const unsigned int *pList, unsigned int number,
  95. unsigned int level)
  96. {
  97. unsigned int i;
  98. unsigned int max_value_in_last_element;
  99. if (pList[0] < pList[1])
  100. max_value_in_last_element = 1;
  101. else
  102. max_value_in_last_element = 0;
  103. if (max_value_in_last_element == 1) {
  104. for (i = (number - 1); i != 0; i--) { /* max value in the last element */
  105. if (pList[i] <= level) {
  106. dprintf(CRITICAL, "zzf_%d<=%d, i=%d\n", pList[i], level, i);
  107. return pList[i];
  108. }
  109. }
  110. dprintf(CRITICAL, "Can't find closest level\n");
  111. return pList[0];
  112. /* return 000; */
  113. } else {
  114. for (i = 0; i < number; i++) { /* max value in the first element */
  115. if (pList[i] <= level)
  116. return pList[i];
  117. }
  118. dprintf(CRITICAL, "Can't find closest level\n");
  119. return pList[number - 1];
  120. /* return 000; */
  121. }
  122. }
  123. unsigned char hl7005_reg[HL7005_REG_NUM] = { 0 };
  124. #define HL7005_I2C_ID I2C1
  125. static struct mt_i2c_t hl7005_i2c;
  126. u32 hl7005_write_byte(u8 addr, u8 value)
  127. {
  128. u32 ret_code = I2C_OK;
  129. u8 write_data[2];
  130. u16 len;
  131. write_data[0]= addr;
  132. write_data[1] = value;
  133. hl7005_i2c.id = HL7005_I2C_ID;
  134. /* Since i2c will left shift 1 bit, we need to set hl7005 I2C address to >>1 */
  135. hl7005_i2c.addr = (HL7005_SLAVE_ADDR_WRITE >> 1);
  136. hl7005_i2c.mode = ST_MODE;
  137. hl7005_i2c.speed = 100;
  138. len = 2;
  139. ret_code = i2c_write(&hl7005_i2c, write_data, len);
  140. //dprintf(CRITICAL, "%s: i2c_write: ret_code: %d\n", __func__, ret_code);
  141. return ret_code;
  142. }
  143. u32 hl7005_read_byte(u8 addr, u8 *dataBuffer)
  144. {
  145. u32 ret_code = I2C_OK;
  146. u16 len;
  147. *dataBuffer = addr;
  148. hl7005_i2c.id = HL7005_I2C_ID;
  149. /* Since i2c will left shift 1 bit, we need to set hl7005 I2C address to >>1 */
  150. hl7005_i2c.addr = (HL7005_SLAVE_ADDR_WRITE >> 1);
  151. hl7005_i2c.mode = ST_MODE;
  152. hl7005_i2c.speed = 100;
  153. len = 1;
  154. ret_code = i2c_write_read(&hl7005_i2c, dataBuffer, len, len);
  155. //dprintf(CRITICAL, "%s: i2c_read: ret_code: %d\n", __func__, ret_code);
  156. return ret_code;
  157. }
  158. unsigned int hl7005_read_interface(unsigned char reg_num, unsigned char *val, unsigned char MASK,
  159. unsigned char SHIFT)
  160. {
  161. unsigned char hl7005_reg = 0;
  162. unsigned int ret = 0;
  163. ret = hl7005_read_byte(reg_num, &hl7005_reg);
  164. dprintf(CRITICAL, "[hl7005_read_interface] Reg[%x]=0x%x\n", reg_num, hl7005_reg);
  165. hl7005_reg &= (MASK << SHIFT);
  166. *val = (hl7005_reg >> SHIFT);
  167. dprintf(CRITICAL, "[hl7005_read_interface] val=0x%x\n", *val);
  168. return ret;
  169. }
  170. unsigned int hl7005_config_interface(unsigned char reg_num, unsigned char val, unsigned char MASK,
  171. unsigned char SHIFT)
  172. {
  173. unsigned char hl7005_reg = 0;
  174. unsigned char hl7005_reg_ori = 0;
  175. unsigned int ret = 0;
  176. ret = hl7005_read_byte(reg_num, &hl7005_reg);
  177. hl7005_reg_ori = hl7005_reg;
  178. hl7005_reg &= ~(MASK << SHIFT);
  179. hl7005_reg |= (val << SHIFT);
  180. ret = hl7005_write_byte(reg_num, hl7005_reg);
  181. dprintf(CRITICAL, "[hl7005_config_interface] write Reg[%x]=0x%x from 0x%x\n", reg_num,
  182. hl7005_reg, hl7005_reg_ori);
  183. /* Check */
  184. /* hl7005_read_byte(reg_num, &hl7005_reg); */
  185. /* printk("[hl7005_config_interface] Check Reg[%x]=0x%x\n", reg_num, hl7005_reg); */
  186. return ret;
  187. }
  188. /* write one register directly */
  189. unsigned int hl7005_reg_config_interface(unsigned char reg_num, unsigned char val)
  190. {
  191. unsigned char hl7005_reg = val;
  192. return hl7005_write_byte(reg_num, hl7005_reg);
  193. }
  194. void hl7005_set_tmr_rst(unsigned int val)
  195. {
  196. hl7005_config_interface((unsigned char)(HL7005_CON0),
  197. (unsigned char)(val),
  198. (unsigned char)(CON0_TMR_RST_MASK),
  199. (unsigned char)(CON0_TMR_RST_SHIFT)
  200. );
  201. }
  202. unsigned int hl7005_get_otg_status(void)
  203. {
  204. unsigned char val = 0;
  205. hl7005_read_interface((unsigned char)(HL7005_CON0),
  206. (unsigned char *)(&val),
  207. (unsigned char)(CON0_OTG_MASK),
  208. (unsigned char)(CON0_OTG_SHIFT)
  209. );
  210. return val;
  211. }
  212. void hl7005_set_en_stat(unsigned int val)
  213. {
  214. hl7005_config_interface((unsigned char)(HL7005_CON0),
  215. (unsigned char)(val),
  216. (unsigned char)(CON0_EN_STAT_MASK),
  217. (unsigned char)(CON0_EN_STAT_SHIFT)
  218. );
  219. }
  220. unsigned int hl7005_get_chip_status(void)
  221. {
  222. unsigned char val = 0;
  223. hl7005_read_interface((unsigned char)(HL7005_CON0),
  224. (unsigned char *)(&val),
  225. (unsigned char)(CON0_STAT_MASK),
  226. (unsigned char)(CON0_STAT_SHIFT)
  227. );
  228. return val;
  229. }
  230. unsigned int hl7005_get_boost_status(void)
  231. {
  232. unsigned char val = 0;
  233. hl7005_read_interface((unsigned char)(HL7005_CON0),
  234. (unsigned char *)(&val),
  235. (unsigned char)(CON0_BOOST_MASK),
  236. (unsigned char)(CON0_BOOST_SHIFT)
  237. );
  238. return val;
  239. }
  240. unsigned int hl7005_get_fault_status(void)
  241. {
  242. unsigned char val = 0;
  243. hl7005_read_interface((unsigned char)(HL7005_CON0),
  244. (unsigned char *)(&val),
  245. (unsigned char)(CON0_FAULT_MASK),
  246. (unsigned char)(CON0_FAULT_SHIFT)
  247. );
  248. return val;
  249. }
  250. void hl7005_set_input_charging_current(unsigned int val)
  251. {
  252. hl7005_config_interface((unsigned char)(HL7005_CON1),
  253. (unsigned char)(val),
  254. (unsigned char)(CON1_LIN_LIMIT_MASK),
  255. (unsigned char)(CON1_LIN_LIMIT_SHIFT)
  256. );
  257. }
  258. unsigned int hl7005_get_input_charging_current(void)
  259. {
  260. unsigned char val = 0;
  261. hl7005_read_interface((unsigned char)(HL7005_CON1),
  262. (unsigned char *)(&val),
  263. (unsigned char)(CON1_LIN_LIMIT_MASK),
  264. (unsigned char)(CON1_LIN_LIMIT_SHIFT)
  265. );
  266. return val;
  267. }
  268. void hl7005_set_v_low(unsigned int val)
  269. {
  270. hl7005_config_interface((unsigned char)(HL7005_CON1),
  271. (unsigned char)(val),
  272. (unsigned char)(CON1_LOW_V_MASK),
  273. (unsigned char)(CON1_LOW_V_SHIFT)
  274. );
  275. }
  276. void hl7005_set_te(unsigned int val)
  277. {
  278. hl7005_config_interface((unsigned char)(HL7005_CON1),
  279. (unsigned char)(val),
  280. (unsigned char)(CON1_TE_MASK),
  281. (unsigned char)(CON1_TE_SHIFT)
  282. );
  283. }
  284. void hl7005_set_ce(unsigned int val)
  285. {
  286. hl7005_config_interface((unsigned char)(HL7005_CON1),
  287. (unsigned char)(val),
  288. (unsigned char)(CON1_CE_MASK),
  289. (unsigned char)(CON1_CE_SHIFT)
  290. );
  291. }
  292. void hl7005_set_hz_mode(unsigned int val)
  293. {
  294. hl7005_config_interface((unsigned char)(HL7005_CON1),
  295. (unsigned char)(val),
  296. (unsigned char)(CON1_HZ_MODE_MASK),
  297. (unsigned char)(CON1_HZ_MODE_SHIFT)
  298. );
  299. }
  300. void hl7005_set_opa_mode(unsigned int val)
  301. {
  302. hl7005_config_interface((unsigned char)(HL7005_CON1),
  303. (unsigned char)(val),
  304. (unsigned char)(CON1_OPA_MODE_MASK),
  305. (unsigned char)(CON1_OPA_MODE_SHIFT)
  306. );
  307. }
  308. void hl7005_set_oreg(unsigned int val)
  309. {
  310. hl7005_config_interface((unsigned char)(HL7005_CON2),
  311. (unsigned char)(val),
  312. (unsigned char)(CON2_OREG_MASK),
  313. (unsigned char)(CON2_OREG_SHIFT)
  314. );
  315. }
  316. void hl7005_set_otg_pl(unsigned int val)
  317. {
  318. hl7005_config_interface((unsigned char)(HL7005_CON2),
  319. (unsigned char)(val),
  320. (unsigned char)(CON2_OTG_PL_MASK),
  321. (unsigned char)(CON2_OTG_PL_SHIFT)
  322. );
  323. }
  324. void hl7005_set_otg_en(unsigned int val)
  325. {
  326. hl7005_config_interface((unsigned char)(HL7005_CON2),
  327. (unsigned char)(val),
  328. (unsigned char)(CON2_OTG_EN_MASK),
  329. (unsigned char)(CON2_OTG_EN_SHIFT)
  330. );
  331. }
  332. unsigned int hl7005_get_vender_code(void)
  333. {
  334. unsigned char val = 0;
  335. hl7005_read_interface((unsigned char)(HL7005_CON3),
  336. (unsigned char *)(&val),
  337. (unsigned char)(CON3_VENDER_CODE_MASK),
  338. (unsigned char)(CON3_VENDER_CODE_SHIFT)
  339. );
  340. return val;
  341. }
  342. unsigned int hl7005_get_pn(void)
  343. {
  344. unsigned char val = 0;
  345. hl7005_read_interface((unsigned char)(HL7005_CON3),
  346. (unsigned char *)(&val),
  347. (unsigned char)(CON3_PIN_MASK),
  348. (unsigned char)(CON3_PIN_SHIFT)
  349. );
  350. return val;
  351. }
  352. unsigned int hl7005_get_revision(void)
  353. {
  354. unsigned char val = 0;
  355. hl7005_read_interface((unsigned char)(HL7005_CON3),
  356. (unsigned char *)(&val),
  357. (unsigned char)(CON3_REVISION_MASK),
  358. (unsigned char)(CON3_REVISION_SHIFT)
  359. );
  360. return val;
  361. }
  362. void hl7005_set_reset(unsigned int val)
  363. {
  364. hl7005_config_interface((unsigned char)(HL7005_CON4),
  365. (unsigned char)(val),
  366. (unsigned char)(CON4_RESET_MASK),
  367. (unsigned char)(CON4_RESET_SHIFT)
  368. );
  369. }
  370. void hl7005_set_iocharge(unsigned int val)
  371. {
  372. hl7005_config_interface((unsigned char)(HL7005_CON4),
  373. (unsigned char)(val),
  374. (unsigned char)(CON4_I_CHR_MASK),
  375. (unsigned char)(CON4_I_CHR_SHIFT)
  376. );
  377. }
  378. void hl7005_set_iterm(unsigned int val)
  379. {
  380. hl7005_config_interface((unsigned char)(HL7005_CON4),
  381. (unsigned char)(val),
  382. (unsigned char)(CON4_I_TERM_MASK),
  383. (unsigned char)(CON4_I_TERM_SHIFT)
  384. );
  385. }
  386. void hl7005_set_dis_vreg(unsigned int val)
  387. {
  388. hl7005_config_interface((unsigned char)(HL7005_CON5),
  389. (unsigned char)(val),
  390. (unsigned char)(CON5_DIS_VREG_MASK),
  391. (unsigned char)(CON5_DIS_VREG_SHIFT)
  392. );
  393. }
  394. void hl7005_set_io_level(unsigned int val)
  395. {
  396. hl7005_config_interface((unsigned char)(HL7005_CON5),
  397. (unsigned char)(val),
  398. (unsigned char)(CON5_IO_LEVEL_MASK),
  399. (unsigned char)(CON5_IO_LEVEL_SHIFT)
  400. );
  401. }
  402. unsigned int hl7005_get_sp_status(void)
  403. {
  404. unsigned char val = 0;
  405. hl7005_read_interface((unsigned char)(HL7005_CON5),
  406. (unsigned char *)(&val),
  407. (unsigned char)(CON5_SP_STATUS_MASK),
  408. (unsigned char)(CON5_SP_STATUS_SHIFT)
  409. );
  410. return val;
  411. }
  412. unsigned int hl7005_get_en_level(void)
  413. {
  414. unsigned char val = 0;
  415. hl7005_read_interface((unsigned char)(HL7005_CON5),
  416. (unsigned char *)(&val),
  417. (unsigned char)(CON5_EN_LEVEL_MASK),
  418. (unsigned char)(CON5_EN_LEVEL_SHIFT)
  419. );
  420. return val;
  421. }
  422. void hl7005_set_vsp(unsigned int val)
  423. {
  424. hl7005_config_interface((unsigned char)(HL7005_CON5),
  425. (unsigned char)(val),
  426. (unsigned char)(CON5_VSP_MASK),
  427. (unsigned char)(CON5_VSP_SHIFT)
  428. );
  429. }
  430. void hl7005_set_i_safe(unsigned int val)
  431. {
  432. hl7005_config_interface((unsigned char)(HL7005_CON6),
  433. (unsigned char)(val),
  434. (unsigned char)(CON6_ISAFE_MASK),
  435. (unsigned char)(CON6_ISAFE_SHIFT)
  436. );
  437. }
  438. void hl7005_set_v_safe(unsigned int val)
  439. {
  440. hl7005_config_interface((unsigned char)(HL7005_CON6),
  441. (unsigned char)(val),
  442. (unsigned char)(CON6_VSAFE_MASK),
  443. (unsigned char)(CON6_VSAFE_SHIFT)
  444. );
  445. }
  446. void hl7005_dump_register()
  447. {
  448. int i;
  449. for (i = 0;i < HL7005_REG_NUM;i++) {
  450. hl7005_read_byte(i, &hl7005_reg[i]);
  451. dprintf(CRITICAL, "[0x%x]=0x%x ", i, hl7005_reg[i]);
  452. }
  453. }
  454. int hl7005_enable_charging(bool en)
  455. {
  456. unsigned int status = 0;
  457. if (en) {
  458. hl7005_set_ce(0);
  459. hl7005_set_hz_mode(0);
  460. hl7005_set_opa_mode(0);
  461. } else {
  462. hl7005_set_ce(1);
  463. }
  464. return status;
  465. }
  466. static int hl7005_set_cv_voltage(u32 cv)
  467. {
  468. int status = 0;
  469. unsigned short int array_size;
  470. unsigned int set_cv_voltage;
  471. unsigned short int register_value;
  472. /*static kal_int16 pre_register_value; */
  473. array_size = ARRAY_SIZE(VBAT_CVTH);
  474. /*pre_register_value = -1; */
  475. set_cv_voltage = bmt_find_closest_level(VBAT_CVTH, array_size, cv);
  476. register_value =
  477. charging_parameter_to_value(VBAT_CVTH, array_size, set_cv_voltage);
  478. dprintf(CRITICAL, "charging_set_cv_voltage register_value=0x%x %d %d\n",
  479. register_value, cv, set_cv_voltage);
  480. hl7005_set_oreg(register_value);
  481. return status;
  482. }
  483. static int hl7005_get_current(u32 *ichg)
  484. {
  485. int status = 0;
  486. unsigned int array_size;
  487. unsigned char reg_value;
  488. array_size = ARRAY_SIZE(CSTH);
  489. hl7005_read_interface(0x1, &reg_value, 0x3, 0x6); /* IINLIM */
  490. *ichg = charging_value_to_parameter(CSTH, array_size, reg_value);
  491. return status;
  492. }
  493. static int hl7005_set_current(u32 current_value)
  494. {
  495. unsigned int status = 0;
  496. unsigned int set_chr_current;
  497. unsigned int array_size;
  498. unsigned int register_value;
  499. if (current_value <= 35000) {
  500. hl7005_set_io_level(1);
  501. } else {
  502. hl7005_set_io_level(0);
  503. array_size = ARRAY_SIZE(CSTH);
  504. set_chr_current = bmt_find_closest_level(CSTH, array_size, current_value);
  505. register_value = charging_parameter_to_value(CSTH, array_size, set_chr_current);
  506. hl7005_set_iocharge(register_value);
  507. }
  508. return status;
  509. }
  510. static int hl7005_get_input_current(u32 *aicr)
  511. {
  512. unsigned int status = 0;
  513. unsigned int array_size;
  514. unsigned int register_value;
  515. array_size = ARRAY_SIZE(INPUT_CSTH);
  516. register_value = hl7005_get_input_charging_current();
  517. *aicr = charging_parameter_to_value(INPUT_CSTH, array_size, register_value);
  518. return status;
  519. }
  520. static int hl7005_set_input_current(u32 current_value)
  521. {
  522. unsigned int status = 0;
  523. unsigned int set_chr_current;
  524. unsigned int array_size;
  525. unsigned int register_value;
  526. if (current_value > 50000) {
  527. register_value = 0x3;
  528. } else {
  529. array_size = ARRAY_SIZE(INPUT_CSTH);
  530. set_chr_current = bmt_find_closest_level(INPUT_CSTH, array_size, current_value);
  531. register_value =
  532. charging_parameter_to_value(INPUT_CSTH, array_size, set_chr_current);
  533. }
  534. hl7005_set_input_charging_current(register_value);
  535. return status;
  536. }
  537. static int hl7005_get_charging_status(bool *is_done)
  538. {
  539. unsigned int status = 0;
  540. unsigned int ret_val;
  541. ret_val = hl7005_get_chip_status();
  542. if (ret_val == 0x2)
  543. *is_done = true;
  544. else
  545. *is_done = false;
  546. return status;
  547. }
  548. static int hl7005_reset_watch_dog_timer()
  549. {
  550. hl7005_set_tmr_rst(1);
  551. return 0;
  552. }
  553. void hl7005_charging_enable(void)
  554. {
  555. hl7005_reg_config_interface(0x00,0xC0);
  556. hl7005_reg_config_interface(0x01,0x78);
  557. //hl7005_reg_config_interface(0x02,0x8e);
  558. hl7005_reg_config_interface(0x05,0x04);
  559. hl7005_reg_config_interface(0x04,0x1A); //146mA
  560. }
  561. void hl7005_hw_init(void)
  562. {
  563. #if defined(HIGH_BATTERY_VOLTAGE_SUPPORT)
  564. dprintf(CRITICAL, "[hl7005_hw_init] (0x06,0x77)\n");
  565. hl7005_reg_config_interface(0x06,0x77); // set ISAFE and HW CV point (4.34)
  566. hl7005_reg_config_interface(0x02,0xaa); // 4.34
  567. #else
  568. dprintf(CRITICAL, "[hl7005_hw_init] (0x06,0x70)\n");
  569. hl7005_reg_config_interface(0x06,0x70); // set ISAFE
  570. hl7005_reg_config_interface(0x02,0x8e); // 4.2
  571. #endif
  572. }