ncp1854.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. #include <platform/mt_typedefs.h>
  2. #include <platform/mt_reg_base.h>
  3. #include <platform/mt_i2c.h>
  4. #include <platform/mt_gpio.h>
  5. #include <platform/mt_pmic.h>
  6. #include <platform/ncp1854.h>
  7. #include <printf.h>
  8. #if !defined(CONFIG_POWER_EXT)
  9. #include <platform/upmu_common.h>
  10. #endif
  11. int g_ncp1854_log_en=0;
  12. /**********************************************************
  13. *
  14. * [I2C Slave Setting]
  15. *
  16. *********************************************************/
  17. #define NCP1854_SLAVE_ADDR_WRITE 0x6C
  18. #define NCP1854_SLAVE_ADDR_Read 0x6D
  19. /**********************************************************
  20. *
  21. * [Global Variable]
  22. *
  23. *********************************************************/
  24. #define NCP1854_REG_NUM 18
  25. #ifndef NCP1854_BUSNUM
  26. #define NCP1854_BUSNUM 1
  27. #endif
  28. #define PRECC_BATVOL 2800 //preCC 2.8V
  29. /* #ifndef GPIO_SWCHARGER_EN_PIN
  30. #define GPIO_SWCHARGER_EN_PIN 65
  31. #endif */
  32. kal_uint8 ncp1854_reg[NCP1854_REG_NUM] = {0};
  33. /**********************************************************
  34. *
  35. * [I2C Function For Read/Write ncp1854]
  36. *
  37. *********************************************************/
  38. #ifndef NCP1854_I2C_ID
  39. #define NCP1854_I2C_ID I2C0
  40. #endif
  41. static struct mt_i2c_t ncp1854_i2c;
  42. kal_uint32 ncp1854_write_byte(kal_uint8 addr, kal_uint8 value)
  43. {
  44. kal_uint32 ret_code = I2C_OK;
  45. kal_uint8 write_data[2];
  46. kal_uint16 len;
  47. write_data[0]= addr;
  48. write_data[1] = value;
  49. ncp1854_i2c.id = NCP1854_I2C_ID;
  50. /* Since i2c will left shift 1 bit, we need to set NCP1854 I2C address to >>1 */
  51. ncp1854_i2c.addr = (NCP1854_SLAVE_ADDR_WRITE >> 1);
  52. ncp1854_i2c.mode = ST_MODE;
  53. ncp1854_i2c.speed = 100;
  54. len = 2;
  55. ret_code = i2c_write(&ncp1854_i2c, write_data, len);
  56. if (I2C_OK != ret_code)
  57. dprintf(INFO, "%s: i2c_write: ret_code: %d\n", __func__, ret_code);
  58. return ret_code;
  59. }
  60. kal_uint32 ncp1854_read_byte (kal_uint8 addr, kal_uint8 *dataBuffer)
  61. {
  62. kal_uint32 ret_code = I2C_OK;
  63. kal_uint16 len;
  64. *dataBuffer = addr;
  65. ncp1854_i2c.id = NCP1854_I2C_ID;
  66. /* Since i2c will left shift 1 bit, we need to set NCP1854 I2C address to >>1 */
  67. ncp1854_i2c.addr = (NCP1854_SLAVE_ADDR_Read >> 1);
  68. ncp1854_i2c.mode = ST_MODE;
  69. ncp1854_i2c.speed = 100;
  70. len = 1;
  71. ret_code = i2c_write_read(&ncp1854_i2c, dataBuffer, len, len);
  72. if (I2C_OK != ret_code)
  73. dprintf(INFO, "%s: i2c_read: ret_code: %d\n", __func__, ret_code);
  74. return ret_code;
  75. }
  76. /**********************************************************
  77. *
  78. * [Read / Write Function]
  79. *
  80. *********************************************************/
  81. kal_uint32 ncp1854_read_interface (kal_uint8 RegNum, kal_uint8 *val, kal_uint8 MASK, kal_uint8 SHIFT)
  82. {
  83. kal_uint8 ncp1854_reg = 0;
  84. int ret = 0;
  85. dprintf(INFO, "--------------------------------------------------\n");
  86. ret = ncp1854_read_byte(RegNum, &ncp1854_reg);
  87. dprintf(INFO, "[ncp1854_read_interface] Reg[%x]=0x%x\n", RegNum, ncp1854_reg);
  88. ncp1854_reg &= (MASK << SHIFT);
  89. *val = (ncp1854_reg >> SHIFT);
  90. dprintf(INFO, "[ncp1854_read_interface] Val=0x%x\n", *val);
  91. return ret;
  92. }
  93. kal_uint32 ncp1854_config_interface (kal_uint8 RegNum, kal_uint8 val, kal_uint8 MASK, kal_uint8 SHIFT)
  94. {
  95. kal_uint8 ncp1854_reg = 0;
  96. int ret = 0;
  97. dprintf(INFO, "--------------------------------------------------\n");
  98. ret = ncp1854_read_byte(RegNum, &ncp1854_reg);
  99. //dprintf(INFO, "[ncp1854_config_interface] Reg[%x]=0x%x\n", RegNum, ncp1854_reg);
  100. ncp1854_reg &= ~(MASK << SHIFT);
  101. ncp1854_reg |= (val << SHIFT);
  102. if (RegNum == NCP1854_CON1 && val == 1 && MASK ==CON1_REG_RST_MASK && SHIFT == CON1_REG_RST_SHIFT) {
  103. // RESET bit
  104. } else if (RegNum == NCP1854_CON1) {
  105. ncp1854_reg &= ~0x80; //RESET bit read returs 1, so clear it
  106. }
  107. ret = ncp1854_write_byte(RegNum, ncp1854_reg);
  108. //dprintf(INFO, "[ncp18546_config_interface] Write Reg[%x]=0x%x\n", RegNum, ncp1854_reg);
  109. // Check
  110. //ncp1854_read_byte(RegNum, &ncp1854_reg);
  111. //dprintf(INFO, "[ncp1854_config_interface] Check Reg[%x]=0x%x\n", RegNum, ncp1854_reg);
  112. return ret;
  113. }
  114. /**********************************************************
  115. *
  116. * [ncp1854 Function]
  117. *
  118. *********************************************************/
  119. //CON0
  120. kal_uint32 ncp1854_get_chip_status(void)
  121. {
  122. kal_uint32 ret=0;
  123. kal_uint8 val=0;
  124. ret=ncp1854_read_interface((kal_uint8)(NCP1854_CON0),
  125. (&val),
  126. (kal_uint8)(CON0_STATE_MASK),
  127. (kal_uint8)(CON0_STATE_SHIFT)
  128. );
  129. return val;
  130. }
  131. kal_uint32 ncp1854_get_batfet(void)
  132. {
  133. kal_uint32 ret=0;
  134. kal_uint8 val=0;
  135. ret=ncp1854_read_interface((kal_uint8)(NCP1854_CON0),
  136. (&val),
  137. (kal_uint8)(CON0_BATFET_MASK),
  138. (kal_uint8)(CON0_BATFET_SHIFT)
  139. );
  140. return val;
  141. }
  142. //CON1
  143. void ncp1854_set_reset(kal_uint32 val)
  144. {
  145. kal_uint32 ret=0;
  146. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON1),
  147. (kal_uint8)(val),
  148. (kal_uint8)(CON1_REG_RST_MASK),
  149. (kal_uint8)(CON1_REG_RST_SHIFT)
  150. );
  151. }
  152. void ncp1854_set_chg_en(kal_uint32 val)
  153. {
  154. kal_uint32 ret=0;
  155. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON1),
  156. (kal_uint8)(val),
  157. (kal_uint8)(CON1_CHG_EN_MASK),
  158. (kal_uint8)(CON1_CHG_EN_SHIFT)
  159. );
  160. }
  161. void ncp1854_set_otg_en(kal_uint32 val)
  162. {
  163. kal_uint32 ret=0;
  164. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON1),
  165. (kal_uint8)(val),
  166. (kal_uint8)(CON1_OTG_EN_MASK),
  167. (kal_uint8)(CON1_OTG_EN_SHIFT)
  168. );
  169. return val;
  170. }
  171. void ncp1854_set_ntc_en(kal_uint32 val)
  172. {
  173. kal_uint32 ret=0;
  174. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON1),
  175. (kal_uint8)(val),
  176. (kal_uint8)(CON1_NTC_EN_MASK),
  177. (kal_uint8)(CON1_NTC_EN_SHIFT)
  178. );
  179. }
  180. void ncp1854_set_tj_warn_opt(kal_uint32 val)
  181. {
  182. kal_uint32 ret=0;
  183. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON1),
  184. (kal_uint8)(val),
  185. (kal_uint8)(CON1_TJ_WARN_OPT_MASK),
  186. (kal_uint8)(CON1_TJ_WARN_OPT_SHIFT)
  187. );
  188. return val;
  189. }
  190. void ncp1854_set_jeita_opt(kal_uint32 val)
  191. {
  192. kal_uint32 ret=0;
  193. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON1),
  194. (kal_uint8)(val),
  195. (kal_uint8)(CON1_JEITA_OPT_MASK),
  196. (kal_uint8)(CON1_JEITA_OPT_SHIFT)
  197. );
  198. return val;
  199. }
  200. void ncp1854_set_tchg_rst(kal_uint32 val)
  201. {
  202. kal_uint32 ret=0;
  203. ret=ncp1854_config_interface( (kal_uint8)(NCP1854_CON1),
  204. (kal_uint8)(val),
  205. (kal_uint8)(CON1_TCHG_RST_MASK),
  206. (kal_uint8)(CON1_TCHG_RST_SHIFT)
  207. );
  208. }
  209. void ncp1854_set_int_mask(kal_uint32 val)
  210. {
  211. kal_uint32 ret=0;
  212. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON1),
  213. (kal_uint8)(val),
  214. (kal_uint8)(CON1_INT_MASK_MASK),
  215. (kal_uint8)(CON1_INT_MASK_SHIFT)
  216. );
  217. return val;
  218. }
  219. //CON2
  220. void ncp1854_set_wdto_dis(kal_uint32 val)
  221. {
  222. kal_uint32 ret=0;
  223. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON2),
  224. (kal_uint8)(val),
  225. (kal_uint8)(CON2_WDTO_DIS_MASK),
  226. (kal_uint8)(CON2_WDTO_DIS_SHIFT)
  227. );
  228. }
  229. void ncp1854_set_chgto_dis(kal_uint32 val)
  230. {
  231. kal_uint32 ret=0;
  232. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON2),
  233. (kal_uint8)(val),
  234. (kal_uint8)(CON2_CHGTO_DIS_MASK),
  235. (kal_uint8)(CON2_CHGTO_DIS_SHIFT)
  236. );
  237. }
  238. void ncp1854_set_pwr_path(kal_uint32 val)
  239. {
  240. kal_uint32 ret=0;
  241. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON2),
  242. (kal_uint8)(val),
  243. (kal_uint8)(CON2_PWR_PATH_MASK),
  244. (kal_uint8)(CON2_PWR_PATH_SHIFT)
  245. );
  246. }
  247. void ncp1854_set_trans_en(kal_uint32 val)
  248. {
  249. kal_uint32 ret=0;
  250. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON2),
  251. (kal_uint8)(val),
  252. (kal_uint8)(CON2_TRANS_EN_MASK),
  253. (kal_uint8)(CON2_TRANS_EN_SHIFT)
  254. );
  255. }
  256. void ncp1854_set_factory_mode(kal_uint32 val)
  257. {
  258. kal_uint32 ret=0;
  259. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON2),
  260. (kal_uint8)(val),
  261. (kal_uint8)(CON2_FCTRY_MOD_MASK),
  262. (kal_uint8)(CON2_FCTRY_MOD_SHIFT)
  263. );
  264. }
  265. void ncp1854_set_iinset_pin_en(kal_uint32 val)
  266. {
  267. kal_uint32 ret=0;
  268. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON2),
  269. (kal_uint8)(val),
  270. (kal_uint8)(CON2_IINSET_PIN_EN_MASK),
  271. (kal_uint8)(CON2_IINSET_PIN_EN_SHIFT)
  272. );
  273. }
  274. void ncp1854_set_iinlim_en(kal_uint32 val)
  275. {
  276. kal_uint32 ret=0;
  277. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON2),
  278. (kal_uint8)(val),
  279. (kal_uint8)(CON2_IINLIM_EN_MASK),
  280. (kal_uint8)(CON2_IINLIM_EN_SHIFT)
  281. );
  282. }
  283. void ncp1854_set_aicl_en(kal_uint32 val)
  284. {
  285. kal_uint32 ret=0;
  286. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON2),
  287. (kal_uint8)(val),
  288. (kal_uint8)(CON2_AICL_EN_MASK),
  289. (kal_uint8)(CON2_AICL_EN_SHIFT)
  290. );
  291. }
  292. //CON8
  293. kal_uint32 ncp1854_get_vfet_ok(void)
  294. {
  295. kal_uint32 ret=0;
  296. kal_uint8 val=0;
  297. ret=ncp1854_read_interface((kal_uint8)(NCP1854_CON8),
  298. (&val),
  299. (kal_uint8)(CON8_VFET_OK_MASK),
  300. (kal_uint8)(CON8_VFET_OK_SHIFT)
  301. );
  302. return val;
  303. }
  304. //CON14
  305. void ncp1854_set_ctrl_vbat(kal_uint32 val)
  306. {
  307. kal_uint32 ret=0;
  308. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON14),
  309. (kal_uint8)(val),
  310. (kal_uint8)(CON14_CTRL_VBAT_MASK),
  311. (kal_uint8)(CON14_CTRL_VBAT_SHIFT)
  312. );
  313. }
  314. //CON15
  315. void ncp1854_set_ichg_high(kal_uint32 val)
  316. {
  317. kal_uint32 ret=0;
  318. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON15),
  319. (kal_uint8)(val),
  320. (kal_uint8)(CON15_ICHG_HIGH_MASK),
  321. (kal_uint8)(CON15_ICHG_HIGH_SHIFT)
  322. );
  323. }
  324. void ncp1854_set_ieoc(kal_uint32 val)
  325. {
  326. kal_uint32 ret=0;
  327. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON15),
  328. (kal_uint8)(val),
  329. (kal_uint8)(CON15_IEOC_MASK),
  330. (kal_uint8)(CON15_IEOC_SHIFT)
  331. );
  332. }
  333. void ncp1854_set_ichg(kal_uint32 val)
  334. {
  335. kal_uint32 ret=0;
  336. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON15),
  337. (kal_uint8)(val),
  338. (kal_uint8)(CON15_ICHG_MASK),
  339. (kal_uint8)(CON15_ICHG_SHIFT)
  340. );
  341. }
  342. //CON16
  343. void ncp1854_set_iweak(kal_uint32 val)
  344. {
  345. kal_uint32 ret=0;
  346. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON16),
  347. (kal_uint8)(val),
  348. (kal_uint8)(CON16_IWEAK_MASK),
  349. (kal_uint8)(CON16_IWEAK_SHIFT)
  350. );
  351. }
  352. void ncp1854_set_ctrl_vfet(kal_uint32 val)
  353. {
  354. kal_uint32 ret=0;
  355. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON16),
  356. (kal_uint8)(val),
  357. (kal_uint8)(CON16_CTRL_VFET_MASK),
  358. (kal_uint8)(CON16_CTRL_VFET_SHIFT)
  359. );
  360. }
  361. void ncp1854_set_iinlim(kal_uint32 val)
  362. {
  363. kal_uint32 ret=0;
  364. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON16),
  365. (kal_uint8)(val),
  366. (kal_uint8)(CON16_IINLIM_MASK),
  367. (kal_uint8)(CON16_IINLIM_SHIFT)
  368. );
  369. }
  370. //CON17
  371. void ncp1854_set_iinlim_ta(kal_uint32 val)
  372. {
  373. kal_uint32 ret=0;
  374. ret=ncp1854_config_interface((kal_uint8)(NCP1854_CON17),
  375. (kal_uint8)(val),
  376. (kal_uint8)(CON17_IINLIM_TA_MASK),
  377. (kal_uint8)(CON17_IINLIM_TA_SHIFT)
  378. );
  379. }
  380. /**********************************************************
  381. *
  382. * [Internal Function]
  383. *
  384. *********************************************************/
  385. void ncp1854_dump_register(void)
  386. {
  387. int i=0;
  388. for (i=0; i<NCP1854_REG_NUM; i++) {
  389. if ((i == 3) || (i == 4) || (i == 5) || (i == 6)) //do not dump read clear status register
  390. continue;
  391. if ((i == 10) || (i == 11) || (i == 12) || (i == 13)) //do not dump interrupt mask bit register
  392. continue;
  393. ncp1854_read_byte(i, &ncp1854_reg[i]);
  394. dprintf(CRITICAL, "[ncp1854_dump_register] Reg[0x%X]=0x%X\n", i, ncp1854_reg[i]);
  395. }
  396. }
  397. void ncp1854_hw_init()
  398. {
  399. dprintf(CRITICAL, "[ncp1854] ChargerHwInit_ncp1854\n" );
  400. kal_uint32 ncp1854_status;
  401. ncp1854_status = ncp1854_get_chip_status();
  402. ncp1854_set_otg_en(0x0);
  403. ncp1854_set_trans_en(0);
  404. ncp1854_set_tj_warn_opt(0x1);
  405. // ncp1854_set_int_mask(0x0); //disable all interrupt
  406. ncp1854_set_int_mask(0x1); //enable all interrupt for boost mode status monitor
  407. // ncp1854_set_tchg_rst(0x1); //reset charge timer
  408. #ifdef NCP1854_PWR_PATH
  409. ncp1854_set_pwr_path(0x1);
  410. #else
  411. ncp1854_set_pwr_path(0x0);
  412. #endif
  413. if ((ncp1854_status == 0x8) || (ncp1854_status == 0x9) || (ncp1854_status == 0xA)) //WEAK WAIT, WEAK SAFE, WEAK CHARGE
  414. ncp1854_set_ctrl_vbat(0x1C); //VCHG = 4.0V
  415. ncp1854_set_ieoc(0x4); // terminate current = 200mA for ICS optimized suspend power
  416. ncp1854_set_iweak(0x3); //weak charge current = 300mA
  417. ncp1854_set_ctrl_vfet(0x3); // VFET = 3.4V
  418. }
  419. static CHARGER_TYPE g_chr_type_num = CHARGER_UNKNOWN;
  420. int hw_charging_get_charger_type(void);
  421. extern int g_std_ac_large_current_en;
  422. void ncp1854_charging_enable(kal_uint32 bEnable)
  423. {
  424. int temp_CC_value = 0;
  425. kal_int32 bat_val = 0;
  426. if (CHARGER_UNKNOWN == g_chr_type_num && KAL_TRUE == upmu_is_chr_det()) {
  427. hw_charging_get_charger_type();
  428. dprintf(CRITICAL, "[BATTERY:ncp1854] charger type: %d\n", g_chr_type_num);
  429. }
  430. bat_val = get_i_sense_volt(1);
  431. if (g_chr_type_num == STANDARD_CHARGER) {
  432. if (g_std_ac_large_current_en==1) {
  433. temp_CC_value = 2000;
  434. ncp1854_set_iinlim_ta(0xF); //IN current limit at 2A
  435. ncp1854_set_iinlim_en(0x1); //IN current limit enable
  436. if (bat_val < PRECC_BATVOL) {
  437. ncp1854_set_ichg_high(0x0); //ICHG
  438. ncp1854_set_ichg(0x1); //Pre-Charging Current Limit at 500mA
  439. } else {
  440. ncp1854_set_ichg_high(0); //ICHG
  441. ncp1854_set_ichg(0xF); //Fast Charging Current Limit at 1.9A
  442. }
  443. } else {
  444. temp_CC_value = 1000;
  445. ncp1854_set_iinlim_ta(0xA); //IN current limit at 1.5A
  446. ncp1854_set_iinlim_en(0x1); //IN current limit enable
  447. if (bat_val < PRECC_BATVOL) {
  448. ncp1854_set_ichg_high(0x0); //ICHG
  449. ncp1854_set_ichg(0x1); //Pre-Charging Current Limit at 500mA
  450. } else {
  451. ncp1854_set_ichg_high(0); //ICHG
  452. ncp1854_set_ichg(0x6); //Fast Charging Current Limit at 1A
  453. }
  454. }
  455. } else if (g_chr_type_num == STANDARD_HOST \
  456. || g_chr_type_num == CHARGING_HOST \
  457. || g_chr_type_num == NONSTANDARD_CHARGER) {
  458. temp_CC_value = 500;
  459. ncp1854_set_iinlim_ta(0); //IINLIM
  460. ncp1854_set_iinlim(0x1); //IN current limit at 500mA
  461. ncp1854_set_iinlim_en(0x1); //IN current limit enable
  462. ncp1854_set_ichg_high(0x0); //ICHG
  463. ncp1854_set_ichg(0x1); //Pre-Charging Current Limit at 500mA
  464. } else {
  465. temp_CC_value = 500;
  466. ncp1854_set_iinlim_ta(0); //IINLIM
  467. ncp1854_set_iinlim(0x1); //IN current limit at 500mA
  468. ncp1854_set_iinlim_en(0x1); //IN current limit enable
  469. ncp1854_set_ichg_high(0x0); //ICHG
  470. ncp1854_set_ichg(0x1); //Pre-Charging Current Limit at 500mA
  471. }
  472. if (KAL_TRUE == bEnable)
  473. ncp1854_set_chg_en(0x1); // charger enable
  474. else
  475. ncp1854_set_chg_en(0); // charger disable
  476. ncp1854_set_tchg_rst(0x1);
  477. dprintf(INFO, "[BATTERY:ncp1854] ncp1854_set_ac_current(), CC value(%dmA) \r\n", temp_CC_value);
  478. dprintf(INFO, "[BATTERY:ncp1854] charger enable/disable %d !\r\n", bEnable);
  479. }
  480. #if defined(CONFIG_POWER_EXT)
  481. int hw_charging_get_charger_type(void)
  482. {
  483. g_chr_type_num = STANDARD_HOST;
  484. return STANDARD_HOST;
  485. }
  486. #else
  487. extern void Charger_Detect_Init(void);
  488. extern void Charger_Detect_Release(void);
  489. extern void mdelay (unsigned long msec);
  490. static void hw_bc11_dump_register(void)
  491. {
  492. dprintf(INFO, "Reg[0x%x]=0x%x,Reg[0x%x]=0x%x\n",
  493. MT6325_CHR_CON20, upmu_get_reg_value(MT6325_CHR_CON20),
  494. MT6325_CHR_CON21, upmu_get_reg_value(MT6325_CHR_CON21)
  495. );
  496. }
  497. static void hw_bc11_init(void)
  498. {
  499. mdelay(300);
  500. Charger_Detect_Init();
  501. //RG_bc11_BIAS_EN=1
  502. mt6325_upmu_set_rg_bc11_bias_en(0x1);
  503. //RG_bc11_VSRC_EN[1:0]=00
  504. mt6325_upmu_set_rg_bc11_vsrc_en(0x0);
  505. //RG_bc11_VREF_VTH = [1:0]=00
  506. mt6325_upmu_set_rg_bc11_vref_vth(0x0);
  507. //RG_bc11_CMP_EN[1.0] = 00
  508. mt6325_upmu_set_rg_bc11_cmp_en(0x0);
  509. //RG_bc11_IPU_EN[1.0] = 00
  510. mt6325_upmu_set_rg_bc11_ipu_en(0x0);
  511. //RG_bc11_IPD_EN[1.0] = 00
  512. mt6325_upmu_set_rg_bc11_ipd_en(0x0);
  513. //bc11_RST=1
  514. mt6325_upmu_set_rg_bc11_rst(0x1);
  515. //bc11_BB_CTRL=1
  516. mt6325_upmu_set_rg_bc11_bb_ctrl(0x1);
  517. mdelay(50);
  518. if (g_ncp1854_log_en>1) {
  519. dprintf(INFO, "hw_bc11_init() \r\n");
  520. hw_bc11_dump_register();
  521. }
  522. }
  523. static U32 hw_bc11_DCD(void)
  524. {
  525. U32 wChargerAvail = 0;
  526. //RG_bc11_IPU_EN[1.0] = 10
  527. mt6325_upmu_set_rg_bc11_ipu_en(0x2);
  528. //RG_bc11_IPD_EN[1.0] = 01
  529. mt6325_upmu_set_rg_bc11_ipd_en(0x1);
  530. //RG_bc11_VREF_VTH = [1:0]=01
  531. mt6325_upmu_set_rg_bc11_vref_vth(0x1);
  532. //RG_bc11_CMP_EN[1.0] = 10
  533. mt6325_upmu_set_rg_bc11_cmp_en(0x2);
  534. mdelay(80);
  535. wChargerAvail = mt6325_upmu_get_rgs_bc11_cmp_out();
  536. if (g_ncp1854_log_en>1) {
  537. dprintf(INFO, "hw_bc11_DCD() \r\n");
  538. hw_bc11_dump_register();
  539. }
  540. //RG_bc11_IPU_EN[1.0] = 00
  541. mt6325_upmu_set_rg_bc11_ipu_en(0x0);
  542. //RG_bc11_IPD_EN[1.0] = 00
  543. mt6325_upmu_set_rg_bc11_ipd_en(0x0);
  544. //RG_bc11_CMP_EN[1.0] = 00
  545. mt6325_upmu_set_rg_bc11_cmp_en(0x0);
  546. //RG_bc11_VREF_VTH = [1:0]=00
  547. mt6325_upmu_set_rg_bc11_vref_vth(0x0);
  548. return wChargerAvail;
  549. }
  550. static U32 hw_bc11_stepA1(void)
  551. {
  552. U32 wChargerAvail = 0;
  553. //RG_bc11_IPD_EN[1.0] = 01
  554. mt6325_upmu_set_rg_bc11_ipd_en(0x1);
  555. //RG_bc11_VREF_VTH = [1:0]=00
  556. mt6325_upmu_set_rg_bc11_vref_vth(0x0);
  557. //RG_bc11_CMP_EN[1.0] = 01
  558. mt6325_upmu_set_rg_bc11_cmp_en(0x1);
  559. mdelay(80);
  560. wChargerAvail = mt6325_upmu_get_rgs_bc11_cmp_out();
  561. if (g_ncp1854_log_en>1) {
  562. dprintf(INFO, "hw_bc11_stepA1() \r\n");
  563. hw_bc11_dump_register();
  564. }
  565. //RG_bc11_IPD_EN[1.0] = 00
  566. mt6325_upmu_set_rg_bc11_ipd_en(0x0);
  567. //RG_bc11_CMP_EN[1.0] = 00
  568. mt6325_upmu_set_rg_bc11_cmp_en(0x0);
  569. return wChargerAvail;
  570. }
  571. static U32 hw_bc11_stepA2(void)
  572. {
  573. U32 wChargerAvail = 0;
  574. //RG_bc11_VSRC_EN[1.0] = 10
  575. mt6325_upmu_set_rg_bc11_vsrc_en(0x2);
  576. //RG_bc11_IPD_EN[1:0] = 01
  577. mt6325_upmu_set_rg_bc11_ipd_en(0x1);
  578. //RG_bc11_VREF_VTH = [1:0]=00
  579. mt6325_upmu_set_rg_bc11_vref_vth(0x0);
  580. //RG_bc11_CMP_EN[1.0] = 01
  581. mt6325_upmu_set_rg_bc11_cmp_en(0x1);
  582. mdelay(80);
  583. wChargerAvail = mt6325_upmu_get_rgs_bc11_cmp_out();
  584. if (g_ncp1854_log_en>1) {
  585. dprintf(INFO, "hw_bc11_stepA2() \r\n");
  586. hw_bc11_dump_register();
  587. }
  588. //RG_bc11_VSRC_EN[1:0]=00
  589. mt6325_upmu_set_rg_bc11_vsrc_en(0x0);
  590. //RG_bc11_IPD_EN[1.0] = 00
  591. mt6325_upmu_set_rg_bc11_ipd_en(0x0);
  592. //RG_bc11_CMP_EN[1.0] = 00
  593. mt6325_upmu_set_rg_bc11_cmp_en(0x0);
  594. return wChargerAvail;
  595. }
  596. static U32 hw_bc11_stepB2(void)
  597. {
  598. U32 wChargerAvail = 0;
  599. //RG_bc11_IPU_EN[1:0]=10
  600. mt6325_upmu_set_rg_bc11_ipu_en(0x2);
  601. //RG_bc11_VREF_VTH = [1:0]=01
  602. mt6325_upmu_set_rg_bc11_vref_vth(0x1);
  603. //RG_bc11_CMP_EN[1.0] = 01
  604. mt6325_upmu_set_rg_bc11_cmp_en(0x1);
  605. mdelay(80);
  606. wChargerAvail = mt6325_upmu_get_rgs_bc11_cmp_out();
  607. if (g_ncp1854_log_en>1) {
  608. dprintf(INFO, "hw_bc11_stepB2() \r\n");
  609. hw_bc11_dump_register();
  610. }
  611. //RG_bc11_IPU_EN[1.0] = 00
  612. mt6325_upmu_set_rg_bc11_ipu_en(0x0);
  613. //RG_bc11_CMP_EN[1.0] = 00
  614. mt6325_upmu_set_rg_bc11_cmp_en(0x0);
  615. //RG_bc11_VREF_VTH = [1:0]=00
  616. mt6325_upmu_set_rg_bc11_vref_vth(0x0);
  617. return wChargerAvail;
  618. }
  619. static void hw_bc11_done(void)
  620. {
  621. //RG_bc11_VSRC_EN[1:0]=00
  622. mt6325_upmu_set_rg_bc11_vsrc_en(0x0);
  623. //RG_bc11_VREF_VTH = [1:0]=0
  624. mt6325_upmu_set_rg_bc11_vref_vth(0x0);
  625. //RG_bc11_CMP_EN[1.0] = 00
  626. mt6325_upmu_set_rg_bc11_cmp_en(0x0);
  627. //RG_bc11_IPU_EN[1.0] = 00
  628. mt6325_upmu_set_rg_bc11_ipu_en(0x0);
  629. //RG_bc11_IPD_EN[1.0] = 00
  630. mt6325_upmu_set_rg_bc11_ipd_en(0x0);
  631. //RG_bc11_BIAS_EN=0
  632. mt6325_upmu_set_rg_bc11_bias_en(0x0);
  633. Charger_Detect_Release();
  634. if (g_ncp1854_log_en>1) {
  635. dprintf(INFO, "hw_bc11_done() \r\n");
  636. hw_bc11_dump_register();
  637. }
  638. }
  639. int hw_charging_get_charger_type(void)
  640. {
  641. if (CHARGER_UNKNOWN != g_chr_type_num)
  642. return g_chr_type_num;
  643. /********* Step initial ***************/
  644. hw_bc11_init();
  645. /********* Step DCD ***************/
  646. if (1 == hw_bc11_DCD()) {
  647. /********* Step A1 ***************/
  648. if (1 == hw_bc11_stepA1()) {
  649. g_chr_type_num = APPLE_2_1A_CHARGER;
  650. dprintf(INFO, "step A1 : Apple 2.1A CHARGER!\r\n");
  651. } else {
  652. g_chr_type_num = NONSTANDARD_CHARGER;
  653. dprintf(INFO, "step A1 : Non STANDARD CHARGER!\r\n");
  654. }
  655. } else {
  656. /********* Step A2 ***************/
  657. if (1 == hw_bc11_stepA2()) {
  658. /********* Step B2 ***************/
  659. if (1 == hw_bc11_stepB2()) {
  660. g_chr_type_num = STANDARD_CHARGER;
  661. dprintf(INFO, "step B2 : STANDARD CHARGER!\r\n");
  662. } else {
  663. g_chr_type_num = CHARGING_HOST;
  664. dprintf(INFO, "step B2 : Charging Host!\r\n");
  665. }
  666. } else {
  667. g_chr_type_num = STANDARD_HOST;
  668. dprintf(INFO, "step A2 : Standard USB Host!\r\n");
  669. }
  670. }
  671. /********* Finally setting *******************************/
  672. hw_bc11_done();
  673. return g_chr_type_num;
  674. }
  675. #endif