switch_charging.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. /*****************************************************************************
  2. *
  3. * Filename:
  4. * ---------
  5. * switch_charging.c
  6. *
  7. * Project:
  8. * --------
  9. * ALPS_Software
  10. *
  11. * Description:
  12. * ------------
  13. * This file implements the interface between BMT and ADC scheduler.
  14. *
  15. * Author:
  16. * -------
  17. * Oscar Liu
  18. *
  19. *============================================================================
  20. * Revision: 1.0
  21. * Modtime: 11 Aug 2005 10:28:16
  22. * Log: //mtkvs01/vmdata/Maui_sw/archives/mcu/hal/peripheral/inc/bmt_chr_setting.h-arc
  23. *
  24. * 03 05 2015 wy.chuang
  25. * [ALPS01921641] [L1_merge] for PMIC and charging
  26. * .
  27. * HISTORY
  28. * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  29. *------------------------------------------------------------------------------
  30. *------------------------------------------------------------------------------
  31. * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  32. *============================================================================
  33. ****************************************************************************/
  34. #include <linux/types.h>
  35. #include <linux/kernel.h>
  36. #include <mt-plat/battery_meter.h>
  37. #include <mt-plat/battery_common.h>
  38. #include <mt-plat/charging.h>
  39. #include <mach/mt_charging.h>
  40. #include <mt-plat/mt_boot.h>
  41. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  42. #include <linux/mutex.h>
  43. #include <linux/wakelock.h>
  44. #include <linux/delay.h>
  45. #if !defined(TA_AC_CHARGING_CURRENT)
  46. #include <mach/mt_pe.h>
  47. #endif
  48. #endif
  49. #ifdef CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT
  50. #include <mach/diso.h>
  51. #endif
  52. /* ============================================================ // */
  53. /* define */
  54. /* ============================================================ // */
  55. /* cut off to full */
  56. #define POST_CHARGING_TIME (30*60) /* 30mins */
  57. #define FULL_CHECK_TIMES 6
  58. /* ============================================================ // */
  59. /* global variable */
  60. /* ============================================================ // */
  61. unsigned int g_bcct_flag = 0;
  62. unsigned int g_bcct_value = 0;
  63. #ifdef CONFIG_MTK_SWITCH_INPUT_OUTPUT_CURRENT_SUPPORT
  64. /*input-output curent distinction*/
  65. unsigned int g_bcct_input_flag = 0;
  66. unsigned int g_bcct_input_value = 0;
  67. #endif
  68. unsigned int g_full_check_count = 0;
  69. CHR_CURRENT_ENUM g_temp_CC_value = CHARGE_CURRENT_0_00_MA;
  70. CHR_CURRENT_ENUM g_temp_input_CC_value = CHARGE_CURRENT_0_00_MA;
  71. unsigned int g_usb_state = USB_UNCONFIGURED;
  72. static bool usb_unlimited;
  73. #if defined(CONFIG_MTK_HAFG_20)
  74. #ifdef HIGH_BATTERY_VOLTAGE_SUPPORT
  75. BATTERY_VOLTAGE_ENUM g_cv_voltage = BATTERY_VOLT_04_340000_V;
  76. #else
  77. BATTERY_VOLTAGE_ENUM g_cv_voltage = BATTERY_VOLT_04_200000_V;
  78. #endif
  79. unsigned int get_cv_voltage(void)
  80. {
  81. return g_cv_voltage;
  82. }
  83. #endif
  84. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  85. /* // PUMP EXPRESS */
  86. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  87. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  88. struct wake_lock TA_charger_suspend_lock;
  89. kal_bool ta_check_chr_type = KAL_TRUE;
  90. kal_bool ta_cable_out_occur = KAL_FALSE;
  91. kal_bool is_ta_connect = KAL_FALSE;
  92. kal_bool ta_vchr_tuning = KAL_TRUE;
  93. #if defined(PUMPEX_PLUS_RECHG)
  94. kal_bool pep_det_rechg = KAL_FALSE;
  95. #endif
  96. int ta_v_chr_org = 0;
  97. #endif
  98. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  99. /* // JEITA */
  100. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  101. #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  102. int g_temp_status = TEMP_POS_10_TO_POS_45;
  103. kal_bool temp_error_recovery_chr_flag = KAL_TRUE;
  104. #endif
  105. /* ============================================================ // */
  106. /* function prototype */
  107. /* ============================================================ // */
  108. /* ============================================================ // */
  109. /* extern variable */
  110. /* ============================================================ // */
  111. /*extern int g_platform_boot_mode; moved to battery_common.h*/
  112. /* ============================================================ // */
  113. /* extern function */
  114. /* ============================================================ // */
  115. /* ============================================================ // */
  116. void BATTERY_SetUSBState(int usb_state_value)
  117. {
  118. #if defined(CONFIG_POWER_EXT)
  119. battery_log(BAT_LOG_CRTI, "[BATTERY_SetUSBState] in FPGA/EVB, no service\r\n");
  120. #else
  121. if ((usb_state_value < USB_SUSPEND) || ((usb_state_value > USB_CONFIGURED))) {
  122. battery_log(BAT_LOG_CRTI,
  123. "[BATTERY] BAT_SetUSBState Fail! Restore to default value\r\n");
  124. usb_state_value = USB_UNCONFIGURED;
  125. } else {
  126. battery_log(BAT_LOG_CRTI, "[BATTERY] BAT_SetUSBState Success! Set %d\r\n",
  127. usb_state_value);
  128. g_usb_state = usb_state_value;
  129. }
  130. #endif
  131. }
  132. unsigned int get_charging_setting_current(void)
  133. {
  134. return g_temp_CC_value;
  135. }
  136. #if defined(CONFIG_MTK_DYNAMIC_BAT_CV_SUPPORT)
  137. static unsigned int get_constant_voltage(void)
  138. {
  139. unsigned int cv;
  140. #ifdef CONFIG_MTK_BIF_SUPPORT
  141. unsigned int vbat_bif;
  142. unsigned int vbat_auxadc;
  143. unsigned int vbat, bif_ok;
  144. int i;
  145. #endif
  146. /*unit:mV defined in cust_charging.h */
  147. cv = V_CC2TOPOFF_THRES;
  148. #ifdef CONFIG_MTK_BIF_SUPPORT
  149. /*Use BIF API to get vbat_core to adjust cv */
  150. i = 0;
  151. do {
  152. battery_charging_control(CHARGING_CMD_GET_BIF_VBAT, &vbat_bif);
  153. vbat_auxadc = battery_meter_get_battery_voltage(KAL_TRUE);
  154. if (vbat_bif < vbat_auxadc && vbat_bif != 0) {
  155. vbat = vbat_bif;
  156. bif_ok = 1;
  157. battery_log(BAT_LOG_CRTI, "[BIF]using vbat_bif=%d\n with dV=%dmV", vbat,
  158. (vbat_bif - vbat_auxadc));
  159. } else {
  160. vbat = vbat_auxadc;
  161. if (i < 5)
  162. i++;
  163. else {
  164. battery_log(BAT_LOG_CRTI,
  165. "[BIF]using vbat_auxadc=%d, check vbat_bif=%d\n", vbat,
  166. vbat_bif);
  167. bif_ok = 0;
  168. break;
  169. }
  170. }
  171. } while (vbat_bif > vbat_auxadc || vbat_bif == 0);
  172. /*CV adjustment according to the obtained vbat */
  173. if (bif_ok == 1) {
  174. #ifdef HIGH_BATTERY_VOLTAGE_SUPPORT
  175. int vbat1 = 4250;
  176. int vbat2 = 4300;
  177. int cv1 = 4450;
  178. #else
  179. int vbat1 = 4100;
  180. int vbat2 = 4150;
  181. int cv1 = 4350;
  182. #endif
  183. if (vbat >= 3400 && vbat < vbat1)
  184. cv = 4608;
  185. else if (vbat >= vbat1 && vbat < vbat2)
  186. cv = cv1;
  187. else
  188. cv = V_CC2TOPOFF_THRES;
  189. battery_log(BAT_LOG_FULL, "[BIF]dynamic CV=%dmV\n", cv);
  190. }
  191. #endif
  192. return cv;
  193. }
  194. static void switch_charger_set_vindpm(unsigned int chr_v)
  195. {
  196. /*unsigned int delta_v = 0; */
  197. unsigned int vindpm = 0;
  198. /*chr_v = battery_meter_get_charger_voltage();*/
  199. /*delta_v = chr_v - ta_v_chr_org; */
  200. if (chr_v > 11000)
  201. vindpm = SWITCH_CHR_VINDPM_12V;
  202. else if (chr_v > 8000)
  203. vindpm = SWITCH_CHR_VINDPM_9V;
  204. else if (chr_v > 6000)
  205. vindpm = SWITCH_CHR_VINDPM_7V;
  206. else
  207. vindpm = SWITCH_CHR_VINDPM_5V;
  208. battery_charging_control(CHARGING_CMD_SET_VINDPM, &vindpm);
  209. battery_log(BAT_LOG_CRTI,
  210. "[PE+] switch charger set VINDPM=%dmV with charger volatge=%dmV\n",
  211. vindpm * 100 + 2600, chr_v);
  212. }
  213. #endif
  214. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  215. static DEFINE_MUTEX(ta_mutex);
  216. static void set_ta_charging_current(void)
  217. {
  218. int real_v_chrA = 0;
  219. real_v_chrA = battery_meter_get_charger_voltage();
  220. #if defined(TA_AC_12V_INPUT_CURRENT)
  221. if ((real_v_chrA - ta_v_chr_org) > 6000) {
  222. g_temp_input_CC_value = TA_AC_12V_INPUT_CURRENT; /* CHARGE_CURRENT_2000_00_MA */
  223. g_temp_CC_value = batt_cust_data.ta_ac_charging_current;
  224. } else
  225. #endif
  226. if ((real_v_chrA - ta_v_chr_org) > 3000) {
  227. g_temp_input_CC_value = batt_cust_data.ta_ac_9v_input_current; /* TA = 9V */
  228. g_temp_CC_value = batt_cust_data.ta_ac_charging_current;
  229. } else if ((real_v_chrA - ta_v_chr_org) > 1000) {
  230. g_temp_input_CC_value = batt_cust_data.ta_ac_7v_input_current; /* TA = 7V */
  231. g_temp_CC_value = batt_cust_data.ta_ac_charging_current;
  232. }
  233. battery_log(BAT_LOG_CRTI, "[PE+]set Ichg=%dmA with Iinlim=%dmA, chrA=%d, chrB=%d\n",
  234. g_temp_CC_value / 100, g_temp_input_CC_value / 100, ta_v_chr_org, real_v_chrA);
  235. }
  236. static void mtk_ta_reset_vchr(void)
  237. {
  238. CHR_CURRENT_ENUM chr_current = CHARGE_CURRENT_70_00_MA;
  239. battery_charging_control(CHARGING_CMD_SET_INPUT_CURRENT, &chr_current);
  240. msleep(250); /* reset Vchr to 5V */
  241. battery_log(BAT_LOG_CRTI, "[PE+]mtk_ta_reset_vchr(): reset Vchr to 5V\n");
  242. }
  243. static void mtk_ta_increase(void)
  244. {
  245. kal_bool ta_current_pattern = KAL_TRUE; /* TRUE = increase */
  246. if (ta_cable_out_occur == KAL_FALSE) {
  247. battery_charging_control(CHARGING_CMD_SET_TA_CURRENT_PATTERN, &ta_current_pattern);
  248. } else {
  249. ta_check_chr_type = KAL_TRUE;
  250. battery_log(BAT_LOG_CRTI, "[PE+]mtk_ta_increase() Cable out\n");
  251. }
  252. }
  253. static kal_bool mtk_ta_retry_increase(void)
  254. {
  255. int real_v_chrA;
  256. int real_v_chrB;
  257. kal_bool retransmit = KAL_TRUE;
  258. unsigned int retransmit_count = 0;
  259. do {
  260. real_v_chrA = battery_meter_get_charger_voltage();
  261. mtk_ta_increase(); /* increase TA voltage to 7V */
  262. real_v_chrB = battery_meter_get_charger_voltage();
  263. if (real_v_chrB - real_v_chrA >= 1000) /* 1.0V */
  264. retransmit = KAL_FALSE;
  265. else {
  266. retransmit_count++;
  267. battery_log(BAT_LOG_CRTI,
  268. "[PE+]Communicated with adapter:retransmit_count =%d, chrA=%d, chrB=%d\n",
  269. retransmit_count, real_v_chrA, real_v_chrB);
  270. }
  271. if ((retransmit_count == 3) || (BMT_status.charger_exist == KAL_FALSE))
  272. retransmit = KAL_FALSE;
  273. } while ((retransmit == KAL_TRUE) && (ta_cable_out_occur == KAL_FALSE));
  274. battery_log(BAT_LOG_CRTI,
  275. "[PE+]Finished communicating with adapter real_v_chrA=%d, real_v_chrB=%d, retry=%d\n",
  276. real_v_chrA, real_v_chrB, retransmit_count);
  277. if (retransmit_count == 3)
  278. return KAL_FALSE;
  279. return KAL_TRUE;
  280. }
  281. static void mtk_ta_detector(void)
  282. {
  283. int real_v_chrB = 0;
  284. #if defined(CONFIG_MTK_BQ25896_SUPPORT)
  285. /*unsigned int chr_ovp;*/
  286. unsigned int vindpm;
  287. unsigned int hw_ovp_en;
  288. /*Need to disable CHR_VCDT_HV before pump to 7V */
  289. /*chr_ovp = TA_AC_7V_INPUT_OVER_VOLTAGE;
  290. battery_charging_control(CHARGING_CMD_SET_HV_THRESHOLD, &chr_ovp);*/
  291. hw_ovp_en = 0;
  292. battery_charging_control(CHARGING_CMD_SET_VBUS_OVP_EN, &hw_ovp_en);
  293. batt_cust_data.v_charger_max = 15000;
  294. #endif
  295. battery_log(BAT_LOG_CRTI, "[PE+]starting PE+ adapter detection\n");
  296. ta_v_chr_org = battery_meter_get_charger_voltage();
  297. mtk_ta_retry_increase();
  298. real_v_chrB = battery_meter_get_charger_voltage();
  299. if (real_v_chrB - ta_v_chr_org >= 1000)
  300. is_ta_connect = KAL_TRUE;
  301. else {
  302. is_ta_connect = KAL_FALSE;
  303. #if defined(CONFIG_MTK_BQ25896_SUPPORT)
  304. /*chr_ovp = TA_AC_5V_INPUT_OVER_VOLTAGE;
  305. battery_charging_control(CHARGING_CMD_SET_HV_THRESHOLD, &chr_ovp);*/
  306. hw_ovp_en = 1;
  307. battery_charging_control(CHARGING_CMD_SET_VBUS_OVP_EN, &hw_ovp_en);
  308. batt_cust_data.v_charger_max = V_CHARGER_MAX;
  309. /*Set BQ25896 VINDPM to 4.6V for vbus = 5V */
  310. vindpm = SWITCH_CHR_VINDPM_5V;
  311. battery_charging_control(CHARGING_CMD_SET_VINDPM, &vindpm);
  312. #endif
  313. }
  314. battery_log(BAT_LOG_CRTI, "[PE+]End of PE+ adapter detection, is_ta_connect=%d\n",
  315. is_ta_connect);
  316. }
  317. static void mtk_ta_init(void)
  318. {
  319. is_ta_connect = KAL_FALSE;
  320. ta_cable_out_occur = KAL_FALSE;
  321. if (batt_cust_data.ta_9v_support || batt_cust_data.ta_12v_support)
  322. ta_vchr_tuning = KAL_FALSE;
  323. battery_charging_control(CHARGING_CMD_INIT, NULL);
  324. }
  325. static void battery_pump_express_charger_check(void)
  326. {
  327. if (KAL_TRUE == ta_check_chr_type &&
  328. STANDARD_CHARGER == BMT_status.charger_type &&
  329. BMT_status.SOC >= batt_cust_data.ta_start_battery_soc &&
  330. BMT_status.SOC < batt_cust_data.ta_stop_battery_soc) {
  331. battery_log(BAT_LOG_CRTI, "[PE+]Starting PE Adaptor detection\n");
  332. mutex_lock(&ta_mutex);
  333. wake_lock(&TA_charger_suspend_lock);
  334. mtk_ta_reset_vchr();
  335. mtk_ta_init();
  336. mtk_ta_detector();
  337. /* need to re-check if the charger plug out during ta detector */
  338. if (KAL_TRUE == ta_cable_out_occur)
  339. ta_check_chr_type = KAL_TRUE;
  340. else
  341. ta_check_chr_type = KAL_FALSE;
  342. #if defined(PUMPEX_PLUS_RECHG)
  343. /*PE detection disable in the event of recharge after 1st PE detection is finished */
  344. pep_det_rechg = KAL_FALSE;
  345. #endif
  346. wake_unlock(&TA_charger_suspend_lock);
  347. mutex_unlock(&ta_mutex);
  348. } else {
  349. battery_log(BAT_LOG_CRTI,
  350. "[PE+]Stop battery_pump_express_charger_check, SOC=%d, ta_check_chr_type = %d, charger_type = %d\n",
  351. BMT_status.SOC, ta_check_chr_type, BMT_status.charger_type);
  352. }
  353. }
  354. static void battery_pump_express_algorithm_start(void)
  355. {
  356. signed int charger_vol;
  357. unsigned int charging_enable = KAL_FALSE;
  358. #if defined(CONFIG_MTK_DYNAMIC_BAT_CV_SUPPORT)
  359. unsigned int cv;
  360. unsigned int vbat;
  361. #endif
  362. #if defined(TA_12V_SUPPORT)
  363. kal_bool pumped_volt;
  364. unsigned int chr_ovp_en;
  365. battery_log(BAT_LOG_CRTI, "[PE+][battery_pump_express_algorithm_start]start PEP...");
  366. #endif
  367. mutex_lock(&ta_mutex);
  368. wake_lock(&TA_charger_suspend_lock);
  369. if (KAL_TRUE == is_ta_connect) {
  370. /* check cable impedance */
  371. charger_vol = battery_meter_get_charger_voltage();
  372. batt_cust_data.v_charger_max = 15000;
  373. #if defined(CONFIG_MTK_DYNAMIC_BAT_CV_SUPPORT)
  374. cv = get_constant_voltage();
  375. vbat = battery_meter_get_battery_voltage(KAL_TRUE);
  376. #endif
  377. if (KAL_FALSE == ta_vchr_tuning) {
  378. #ifndef TA_12V_SUPPORT
  379. mtk_ta_retry_increase();
  380. charger_vol = battery_meter_get_charger_voltage();
  381. #else
  382. /*1. Set CHR_HV to a higher level than 9V */
  383. /*chr_ovp = TA_AC_9V_INPUT_OVER_VOLTAGE;
  384. battery_charging_control(CHARGING_CMD_SET_HV_THRESHOLD, &chr_ovp);*/
  385. /* increase TA voltage to 9V */
  386. pumped_volt = mtk_ta_retry_increase();
  387. if (pumped_volt == KAL_FALSE) {
  388. battery_log(BAT_LOG_CRTI,
  389. "[PE+]adaptor failed to output 9V, Please check adaptor");
  390. /*1. Set BQ25896 VINDPM back to 6.3V for Vbus = 7V */
  391. /* vindpm = SWITCH_CHR_VINDPM_7V; */
  392. /* battery_charging_control(CHARGING_CMD_SET_VINDPM, &vindpm); */
  393. /*2. Set CHR_HV back to a higher level than 7V */
  394. /*chr_ovp = TA_AC_7V_INPUT_OVER_VOLTAGE;
  395. battery_charging_control(CHARGING_CMD_SET_HV_THRESHOLD, &chr_ovp);*/
  396. }
  397. #endif
  398. #if defined(TA_12V_SUPPORT)
  399. else {
  400. /*1. disable PMIC VBUS OVP : VCDT */
  401. chr_ovp_en = 0;
  402. battery_charging_control(CHARGING_CMD_SET_VBUS_OVP_EN, &chr_ovp_en);
  403. batt_cust_data.v_charger_max = 15000;
  404. /*2. Set BQ25896 VINDPM to 10.5V for vbus = 12V */
  405. /* vindpm = SWITCH_CHR_VINDPM_12V; */
  406. /* battery_charging_control(CHARGING_CMD_SET_VINDPM, &vindpm); */
  407. /*ready to pump up to 12V */
  408. pumped_volt = mtk_ta_retry_increase(); /* increase TA voltage to 12V */
  409. if (pumped_volt == KAL_FALSE) {
  410. /*1. Enable PMIC VBUS OVP : VCDT */
  411. chr_ovp_en = 0;
  412. battery_charging_control(CHARGING_CMD_SET_VBUS_OVP_EN,
  413. &chr_ovp_en);
  414. /*2. Reset BQ25896 VINDPM back to 8.1V */
  415. /* vindpm = SWITCH_CHR_VINDPM_9V; */
  416. /* battery_charging_control(CHARGING_CMD_SET_VINDPM, &vindpm); */
  417. battery_log(BAT_LOG_CRTI,
  418. "[PE+]adaptor failed to output 12V, Please check adaptor.");
  419. } else
  420. battery_log(BAT_LOG_FULL,
  421. "[PE+]adaptor successed to output 12V.");
  422. charger_vol = battery_meter_get_charger_voltage();
  423. }
  424. #endif
  425. ta_vchr_tuning = KAL_TRUE;
  426. } else if (BMT_status.SOC > batt_cust_data.ta_stop_battery_soc) {
  427. /* disable charging, avoid Iterm issue */
  428. battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable);
  429. mtk_ta_reset_vchr(); /* decrease TA voltage to 5V */
  430. charger_vol = battery_meter_get_charger_voltage();
  431. if (abs(charger_vol - ta_v_chr_org) <= 1000) /* 1.0V */
  432. is_ta_connect = KAL_FALSE;
  433. battery_log(BAT_LOG_CRTI,
  434. "[PE+]Stop battery_pump_express_algorithm, SOC=%d is_ta_connect =%d, TA_STOP_BATTERY_SOC: %d\n",
  435. BMT_status.SOC, is_ta_connect, batt_cust_data.ta_stop_battery_soc);
  436. }
  437. #if defined(CONFIG_MTK_DYNAMIC_BAT_CV_SUPPORT)
  438. /*For BQ25896, voltage is used to check if PE+ should be tuned off.*/
  439. else if (vbat >= cv) {
  440. /*CV point reached, disable PE+ */
  441. battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable);
  442. mtk_ta_reset_vchr(); /* decrease TA voltage to 5V */
  443. charger_vol = battery_meter_get_charger_voltage();
  444. if (abs(charger_vol - ta_v_chr_org) <= 1000) /* 1.0V */
  445. is_ta_connect = KAL_FALSE;
  446. /*1. Recover CHR_OVP status */
  447. /*chr_ovp = TA_AC_5V_INPUT_OVER_VOLTAGE;
  448. battery_charging_control(CHARGING_CMD_SET_HV_THRESHOLD, &chr_ovp);*/
  449. chr_ovp_en = 1;
  450. battery_charging_control(CHARGING_CMD_SET_VBUS_OVP_EN, &chr_ovp_en);
  451. /*2. Recover SW_OVP status */
  452. batt_cust_data.v_charger_max = V_CHARGER_MAX;
  453. battery_log(BAT_LOG_CRTI,
  454. "[PE+]CV=%d reached. Stopping PE+, is_ta_connect =%d\n", cv,
  455. is_ta_connect);
  456. } else if (0) {
  457. /*check charger voltage status if vbus is dropped*/
  458. if (abs(charger_vol - ta_v_chr_org) < 1000 && BMT_status.bat_charging_state == CHR_CC) {
  459. ta_check_chr_type = KAL_TRUE;
  460. battery_log(BAT_LOG_CRTI, "[PE+] abnormal TA chager voltage, rechecking PE+ adapter\n");
  461. }
  462. }
  463. /*Set VINDPM after Vbus voltage is set or reset */
  464. switch_charger_set_vindpm(charger_vol);
  465. #endif
  466. battery_log(BAT_LOG_CRTI,
  467. "[PE+]check cable impedance, VA(%d) VB(%d) delta(%d).\n",
  468. ta_v_chr_org, charger_vol, charger_vol - ta_v_chr_org);
  469. battery_log(BAT_LOG_CRTI, "[PE+]mtk_ta_algorithm() end\n");
  470. } else {
  471. battery_log(BAT_LOG_CRTI, "[PE+]Not a TA charger, bypass TA algorithm\n");
  472. #if defined(TA_12V_SUPPORT)
  473. batt_cust_data.v_charger_max = V_CHARGER_MAX;
  474. chr_ovp_en = 1;
  475. battery_charging_control(CHARGING_CMD_SET_VBUS_OVP_EN, &chr_ovp_en);
  476. #endif
  477. }
  478. wake_unlock(&TA_charger_suspend_lock);
  479. mutex_unlock(&ta_mutex);
  480. }
  481. #endif
  482. #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  483. static BATTERY_VOLTAGE_ENUM select_jeita_cv(void)
  484. {
  485. BATTERY_VOLTAGE_ENUM cv_voltage;
  486. if (g_temp_status == TEMP_ABOVE_POS_60) {
  487. cv_voltage = JEITA_TEMP_ABOVE_POS_60_CV_VOLTAGE;
  488. } else if (g_temp_status == TEMP_POS_45_TO_POS_60) {
  489. cv_voltage = JEITA_TEMP_POS_45_TO_POS_60_CV_VOLTAGE;
  490. } else if (g_temp_status == TEMP_POS_10_TO_POS_45) {
  491. if (batt_cust_data.high_battery_voltage_support)
  492. cv_voltage = BATTERY_VOLT_04_340000_V;
  493. else
  494. cv_voltage = JEITA_TEMP_POS_10_TO_POS_45_CV_VOLTAGE;
  495. } else if (g_temp_status == TEMP_POS_0_TO_POS_10) {
  496. cv_voltage = JEITA_TEMP_POS_0_TO_POS_10_CV_VOLTAGE;
  497. } else if (g_temp_status == TEMP_NEG_10_TO_POS_0) {
  498. cv_voltage = JEITA_TEMP_NEG_10_TO_POS_0_CV_VOLTAGE;
  499. } else if (g_temp_status == TEMP_BELOW_NEG_10) {
  500. cv_voltage = JEITA_TEMP_BELOW_NEG_10_CV_VOLTAGE;
  501. } else {
  502. cv_voltage = BATTERY_VOLT_04_200000_V;
  503. }
  504. return cv_voltage;
  505. }
  506. PMU_STATUS do_jeita_state_machine(void)
  507. {
  508. BATTERY_VOLTAGE_ENUM cv_voltage;
  509. PMU_STATUS jeita_status = PMU_STATUS_OK;
  510. /* JEITA battery temp Standard */
  511. if (BMT_status.temperature >= TEMP_POS_60_THRESHOLD) {
  512. battery_log(BAT_LOG_CRTI,
  513. "[BATTERY] Battery Over high Temperature(%d) !!\n\r",
  514. TEMP_POS_60_THRESHOLD);
  515. g_temp_status = TEMP_ABOVE_POS_60;
  516. return PMU_STATUS_FAIL;
  517. } else if (BMT_status.temperature > TEMP_POS_45_THRESHOLD) { /* control 45c to normal behavior */
  518. if ((g_temp_status == TEMP_ABOVE_POS_60)
  519. && (BMT_status.temperature >= TEMP_POS_60_THRES_MINUS_X_DEGREE)) {
  520. battery_log(BAT_LOG_CRTI,
  521. "[BATTERY] Battery Temperature between %d and %d,not allow charging yet!!\n\r",
  522. TEMP_POS_60_THRES_MINUS_X_DEGREE, TEMP_POS_60_THRESHOLD);
  523. jeita_status = PMU_STATUS_FAIL;
  524. } else {
  525. battery_log(BAT_LOG_CRTI,
  526. "[BATTERY] Battery Temperature between %d and %d !!\n\r",
  527. TEMP_POS_45_THRESHOLD, TEMP_POS_60_THRESHOLD);
  528. g_temp_status = TEMP_POS_45_TO_POS_60;
  529. }
  530. } else if (BMT_status.temperature >= TEMP_POS_10_THRESHOLD) {
  531. if (((g_temp_status == TEMP_POS_45_TO_POS_60)
  532. && (BMT_status.temperature >= TEMP_POS_45_THRES_MINUS_X_DEGREE))
  533. || ((g_temp_status == TEMP_POS_0_TO_POS_10)
  534. && (BMT_status.temperature <= TEMP_POS_10_THRES_PLUS_X_DEGREE))) {
  535. battery_log(BAT_LOG_CRTI,
  536. "[BATTERY] Battery Temperature not recovery to normal temperature charging mode yet!!\n\r");
  537. } else {
  538. battery_log(BAT_LOG_CRTI,
  539. "[BATTERY] Battery Normal Temperature between %d and %d !!\n\r",
  540. TEMP_POS_10_THRESHOLD, TEMP_POS_45_THRESHOLD);
  541. g_temp_status = TEMP_POS_10_TO_POS_45;
  542. }
  543. } else if (BMT_status.temperature >= TEMP_POS_0_THRESHOLD) {
  544. if ((g_temp_status == TEMP_NEG_10_TO_POS_0 || g_temp_status == TEMP_BELOW_NEG_10)
  545. && (BMT_status.temperature <= TEMP_POS_0_THRES_PLUS_X_DEGREE)) {
  546. if (g_temp_status == TEMP_NEG_10_TO_POS_0) {
  547. battery_log(BAT_LOG_CRTI,
  548. "[BATTERY] Battery Temperature between %d and %d !!\n\r",
  549. TEMP_POS_0_THRES_PLUS_X_DEGREE, TEMP_POS_10_THRESHOLD);
  550. }
  551. if (g_temp_status == TEMP_BELOW_NEG_10) {
  552. battery_log(BAT_LOG_CRTI,
  553. "[BATTERY] Battery Temperature between %d and %d,not allow charging yet!!\n\r",
  554. TEMP_POS_0_THRESHOLD, TEMP_POS_0_THRES_PLUS_X_DEGREE);
  555. return PMU_STATUS_FAIL;
  556. }
  557. } else {
  558. battery_log(BAT_LOG_CRTI,
  559. "[BATTERY] Battery Temperature between %d and %d !!\n\r",
  560. TEMP_POS_0_THRESHOLD, TEMP_POS_10_THRESHOLD);
  561. g_temp_status = TEMP_POS_0_TO_POS_10;
  562. }
  563. } else if (BMT_status.temperature >= TEMP_NEG_10_THRESHOLD) {
  564. if ((g_temp_status == TEMP_BELOW_NEG_10)
  565. && (BMT_status.temperature <= TEMP_NEG_10_THRES_PLUS_X_DEGREE)) {
  566. battery_log(BAT_LOG_CRTI,
  567. "[BATTERY] Battery Temperature between %d and %d,not allow charging yet!!\n\r",
  568. TEMP_NEG_10_THRESHOLD, TEMP_NEG_10_THRES_PLUS_X_DEGREE);
  569. jeita_status = PMU_STATUS_FAIL;
  570. } else {
  571. battery_log(BAT_LOG_CRTI,
  572. "[BATTERY] Battery Temperature between %d and %d !!\n\r",
  573. TEMP_NEG_10_THRESHOLD, TEMP_POS_0_THRESHOLD);
  574. g_temp_status = TEMP_NEG_10_TO_POS_0;
  575. }
  576. } else {
  577. battery_log(BAT_LOG_CRTI,
  578. "[BATTERY] Battery below low Temperature(%d) !!\n\r",
  579. TEMP_NEG_10_THRESHOLD);
  580. g_temp_status = TEMP_BELOW_NEG_10;
  581. jeita_status = PMU_STATUS_FAIL;
  582. }
  583. /* set CV after temperature changed */
  584. cv_voltage = select_jeita_cv();
  585. battery_charging_control(CHARGING_CMD_SET_CV_VOLTAGE, &cv_voltage);
  586. #if defined(CONFIG_MTK_HAFG_20)
  587. g_cv_voltage = cv_voltage;
  588. #endif
  589. return jeita_status;
  590. }
  591. static void set_jeita_charging_current(void)
  592. {
  593. #ifdef CONFIG_USB_IF
  594. if (BMT_status.charger_type == STANDARD_HOST)
  595. return;
  596. #endif
  597. if (g_temp_status == TEMP_NEG_10_TO_POS_0) {
  598. g_temp_CC_value = CHARGE_CURRENT_350_00_MA;
  599. g_temp_input_CC_value = CHARGE_CURRENT_500_00_MA;
  600. battery_log(BAT_LOG_CRTI, "[BATTERY] JEITA set charging current : %d\r\n",
  601. g_temp_CC_value);
  602. }
  603. }
  604. #endif
  605. bool get_usb_current_unlimited(void)
  606. {
  607. if (BMT_status.charger_type == STANDARD_HOST || BMT_status.charger_type == CHARGING_HOST)
  608. return usb_unlimited;
  609. return false;
  610. }
  611. void set_usb_current_unlimited(bool enable)
  612. {
  613. unsigned int en;
  614. usb_unlimited = enable;
  615. if (enable == true)
  616. en = 1;
  617. else
  618. en = 0;
  619. battery_charging_control(CHARGING_CMD_ENABLE_SAFETY_TIMER, &en);
  620. }
  621. void select_charging_current_bcct(void)
  622. {
  623. /*BQ25896 is the first switch chrager separating input and charge current
  624. * any switch charger can use this compile option which may be generalized
  625. * to be CONFIG_SWITCH_INPUT_OUTPUT_CURRENT_SUPPORT
  626. */
  627. #ifndef CONFIG_MTK_SWITCH_INPUT_OUTPUT_CURRENT_SUPPORT
  628. if ((BMT_status.charger_type == STANDARD_HOST) ||
  629. (BMT_status.charger_type == NONSTANDARD_CHARGER)) {
  630. if (g_bcct_value < 100)
  631. g_temp_input_CC_value = CHARGE_CURRENT_0_00_MA;
  632. else if (g_bcct_value < 500)
  633. g_temp_input_CC_value = CHARGE_CURRENT_100_00_MA;
  634. else if (g_bcct_value < 800)
  635. g_temp_input_CC_value = CHARGE_CURRENT_500_00_MA;
  636. else if (g_bcct_value == 800)
  637. g_temp_input_CC_value = CHARGE_CURRENT_800_00_MA;
  638. else
  639. g_temp_input_CC_value = CHARGE_CURRENT_500_00_MA;
  640. } else if ((BMT_status.charger_type == STANDARD_CHARGER) ||
  641. (BMT_status.charger_type == CHARGING_HOST)) {
  642. g_temp_input_CC_value = CHARGE_CURRENT_MAX;
  643. /* --------------------------------------------------- */
  644. /* set IOCHARGE */
  645. if (g_bcct_value < 550)
  646. g_temp_CC_value = CHARGE_CURRENT_0_00_MA;
  647. else if (g_bcct_value < 650)
  648. g_temp_CC_value = CHARGE_CURRENT_550_00_MA;
  649. else if (g_bcct_value < 750)
  650. g_temp_CC_value = CHARGE_CURRENT_650_00_MA;
  651. else if (g_bcct_value < 850)
  652. g_temp_CC_value = CHARGE_CURRENT_750_00_MA;
  653. else if (g_bcct_value < 950)
  654. g_temp_CC_value = CHARGE_CURRENT_850_00_MA;
  655. else if (g_bcct_value < 1050)
  656. g_temp_CC_value = CHARGE_CURRENT_950_00_MA;
  657. else if (g_bcct_value < 1150)
  658. g_temp_CC_value = CHARGE_CURRENT_1050_00_MA;
  659. else if (g_bcct_value < 1250)
  660. g_temp_CC_value = CHARGE_CURRENT_1150_00_MA;
  661. else if (g_bcct_value == 1250)
  662. g_temp_CC_value = CHARGE_CURRENT_1250_00_MA;
  663. else
  664. g_temp_CC_value = CHARGE_CURRENT_650_00_MA;
  665. /* --------------------------------------------------- */
  666. } else {
  667. g_temp_input_CC_value = CHARGE_CURRENT_500_00_MA;
  668. }
  669. #else
  670. if (g_bcct_flag == 1)
  671. g_temp_CC_value = g_bcct_value * 100;
  672. if (g_bcct_input_flag == 1)
  673. g_temp_input_CC_value = g_bcct_input_value * 100;
  674. /*
  675. if ((BMT_status.charger_type == STANDARD_CHARGER) ||
  676. (BMT_status.charger_type == CHARGING_HOST)) {
  677. if (g_bcct_value < 550)
  678. g_temp_CC_value = CHARGE_CURRENT_550_00_MA;
  679. else
  680. g_temp_CC_value = g_bcct_value * 100;
  681. if (g_bcct_value < 650)
  682. g_temp_CC_value = CHARGE_CURRENT_550_00_MA;
  683. else if (g_bcct_value < 750)
  684. g_temp_CC_value = CHARGE_CURRENT_650_00_MA;
  685. else if (g_bcct_value < 850)
  686. g_temp_CC_value = CHARGE_CURRENT_750_00_MA;
  687. else if (g_bcct_value < 950)
  688. g_temp_CC_value = CHARGE_CURRENT_850_00_MA;
  689. else if (g_bcct_value < 1050)
  690. g_temp_CC_value = CHARGE_CURRENT_950_00_MA;
  691. else if (g_bcct_value < 1150)
  692. g_temp_CC_value = CHARGE_CURRENT_1050_00_MA;
  693. else if (g_bcct_value < 1250)
  694. g_temp_CC_value = CHARGE_CURRENT_1150_00_MA;
  695. else if (g_bcct_value == 1250)
  696. g_temp_CC_value = CHARGE_CURRENT_1250_00_MA;
  697. else if (g_bcct_value == 1350)
  698. g_temp_CC_value = CHARGE_CURRENT_1350_00_MA;
  699. else if (g_bcct_value == 1450)
  700. g_temp_CC_value = CHARGE_CURRENT_1450_00_MA;
  701. else if (g_bcct_value == 1550)
  702. g_temp_CC_value = CHARGE_CURRENT_1550_00_MA;
  703. else if (g_bcct_value == 1650)
  704. g_temp_CC_value = CHARGE_CURRENT_1650_00_MA;
  705. else if (g_bcct_value == 1750)
  706. g_temp_CC_value = CHARGE_CURRENT_1750_00_MA;
  707. else if (g_bcct_value == 1850)
  708. g_temp_CC_value = CHARGE_CURRENT_1850_00_MA;
  709. else if (g_bcct_value == 1950)
  710. g_temp_CC_value = CHARGE_CURRENT_1950_00_MA;
  711. else if (g_bcct_value == 2050)
  712. g_temp_CC_value = CHARGE_CURRENT_2050_00_MA;
  713. else if (g_bcct_value == 2150)
  714. g_temp_CC_value = CHARGE_CURRENT_2150_00_MA;
  715. else if (g_bcct_value == 2250)
  716. g_temp_CC_value = CHARGE_CURRENT_2250_00_MA;
  717. else if (g_bcct_value == 2350)
  718. g_temp_CC_value = CHARGE_CURRENT_2350_00_MA;
  719. else if (g_bcct_value == 2450)
  720. g_temp_CC_value = CHARGE_CURRENT_2450_00_MA;
  721. else if (g_bcct_value == 2550)
  722. g_temp_CC_value = CHARGE_CURRENT_2550_00_MA;
  723. else if (g_bcct_value == 2650)
  724. g_temp_CC_value = CHARGE_CURRENT_2650_00_MA;
  725. else if (g_bcct_value == 2750)
  726. g_temp_CC_value = CHARGE_CURRENT_2750_00_MA;
  727. else if (g_bcct_value == 2850)
  728. g_temp_CC_value = CHARGE_CURRENT_2850_00_MA;
  729. else if (g_bcct_value == 2950)
  730. g_temp_CC_value = CHARGE_CURRENT_2950_00_MA;
  731. else
  732. g_temp_CC_value = CHARGE_CURRENT_650_00_MA;
  733. } else {
  734. g_temp_CC_value = CHARGE_CURRENT_500_00_MA;
  735. }
  736. */
  737. #endif
  738. }
  739. /*BQ25896 is the first switch chrager separating input and charge current
  740. */
  741. unsigned int set_chr_input_current_limit(int current_limit)
  742. {
  743. #ifdef CONFIG_MTK_SWITCH_INPUT_OUTPUT_CURRENT_SUPPORT
  744. if (current_limit != -1) {
  745. g_bcct_input_flag = 1;
  746. if ((BMT_status.charger_type == STANDARD_HOST) ||
  747. (BMT_status.charger_type == NONSTANDARD_CHARGER)) {
  748. g_temp_input_CC_value = CHARGE_CURRENT_500_00_MA;
  749. g_bcct_input_value = CHARGE_CURRENT_500_00_MA / 100;
  750. } else if ((BMT_status.charger_type == STANDARD_CHARGER) ||
  751. (BMT_status.charger_type == CHARGING_HOST)) {
  752. if (current_limit < 650) {
  753. g_temp_input_CC_value = CHARGE_CURRENT_650_00_MA;
  754. g_bcct_input_value = CHARGE_CURRENT_650_00_MA / 100;
  755. } else {
  756. g_temp_input_CC_value = current_limit * 100;
  757. g_bcct_input_value = current_limit;
  758. }
  759. } else {
  760. g_temp_input_CC_value = CHARGE_CURRENT_500_00_MA;
  761. g_bcct_input_value = CHARGE_CURRENT_500_00_MA / 100;
  762. }
  763. battery_log(BAT_LOG_CRTI, "[BATTERY] set_chr_input_current_limit (%d)\r\n",
  764. current_limit);
  765. } else {
  766. /* change to default current setting */
  767. g_bcct_input_flag = 0;
  768. }
  769. /* wake_up_bat(); */
  770. /*pchr_turn_on_charging();*/
  771. /* this function must be followed by set_bat_charging_current_limit()*/
  772. return g_bcct_input_flag;
  773. #else
  774. battery_log(BAT_LOG_CRTI, "[BATTERY] set_chr_input_current_limit _NOT_ supported\n");
  775. return 0;
  776. #endif
  777. }
  778. static void pchr_turn_on_charging(void);
  779. unsigned int set_bat_charging_current_limit(int current_limit)
  780. {
  781. battery_log(BAT_LOG_CRTI, "[BATTERY] set_bat_charging_current_limit (%d)\r\n",
  782. current_limit);
  783. if (current_limit != -1) {
  784. g_bcct_flag = 1;
  785. g_bcct_value = current_limit;
  786. #ifdef CONFIG_MTK_THERMAL_TEST_SUPPORT
  787. g_temp_CC_value = current_limit * 100;
  788. #else
  789. if (current_limit < 70)
  790. g_temp_CC_value = CHARGE_CURRENT_0_00_MA;
  791. else if (current_limit < 200)
  792. g_temp_CC_value = CHARGE_CURRENT_70_00_MA;
  793. else if (current_limit < 300)
  794. g_temp_CC_value = CHARGE_CURRENT_200_00_MA;
  795. else if (current_limit < 400)
  796. g_temp_CC_value = CHARGE_CURRENT_300_00_MA;
  797. else if (current_limit < 450)
  798. g_temp_CC_value = CHARGE_CURRENT_400_00_MA;
  799. else if (current_limit < 550)
  800. g_temp_CC_value = CHARGE_CURRENT_450_00_MA;
  801. else if (current_limit < 650)
  802. g_temp_CC_value = CHARGE_CURRENT_550_00_MA;
  803. else if (current_limit < 700)
  804. g_temp_CC_value = CHARGE_CURRENT_650_00_MA;
  805. else if (current_limit < 800)
  806. g_temp_CC_value = CHARGE_CURRENT_700_00_MA;
  807. else if (current_limit < 900)
  808. g_temp_CC_value = CHARGE_CURRENT_800_00_MA;
  809. else if (current_limit < 1000)
  810. g_temp_CC_value = CHARGE_CURRENT_900_00_MA;
  811. else if (current_limit < 1100)
  812. g_temp_CC_value = CHARGE_CURRENT_1000_00_MA;
  813. else if (current_limit < 1200)
  814. g_temp_CC_value = CHARGE_CURRENT_1100_00_MA;
  815. else if (current_limit < 1300)
  816. g_temp_CC_value = CHARGE_CURRENT_1200_00_MA;
  817. else if (current_limit < 1400)
  818. g_temp_CC_value = CHARGE_CURRENT_1300_00_MA;
  819. else if (current_limit < 1500)
  820. g_temp_CC_value = CHARGE_CURRENT_1400_00_MA;
  821. else if (current_limit < 1600)
  822. g_temp_CC_value = CHARGE_CURRENT_1500_00_MA;
  823. else if (current_limit == 1600)
  824. g_temp_CC_value = CHARGE_CURRENT_1600_00_MA;
  825. else
  826. g_temp_CC_value = CHARGE_CURRENT_450_00_MA;
  827. #endif
  828. } else {
  829. /* change to default current setting */
  830. g_bcct_flag = 0;
  831. }
  832. /* wake_up_bat(); */
  833. pchr_turn_on_charging();
  834. return g_bcct_flag;
  835. }
  836. void select_charging_current(void)
  837. {
  838. if (g_ftm_battery_flag) {
  839. battery_log(BAT_LOG_CRTI, "[BATTERY] FTM charging : %d\r\n",
  840. charging_level_data[0]);
  841. g_temp_CC_value = charging_level_data[0];
  842. if (g_temp_CC_value == CHARGE_CURRENT_450_00_MA) {
  843. g_temp_input_CC_value = CHARGE_CURRENT_500_00_MA;
  844. } else {
  845. g_temp_input_CC_value = CHARGE_CURRENT_MAX;
  846. g_temp_CC_value = batt_cust_data.ac_charger_current;
  847. battery_log(BAT_LOG_CRTI, "[BATTERY] set_ac_current \r\n");
  848. }
  849. } else {
  850. if (BMT_status.charger_type == STANDARD_HOST) {
  851. #ifdef CONFIG_USB_IF
  852. {
  853. g_temp_input_CC_value = CHARGE_CURRENT_MAX;
  854. if (g_usb_state == USB_SUSPEND)
  855. g_temp_CC_value = USB_CHARGER_CURRENT_SUSPEND;
  856. else if (g_usb_state == USB_UNCONFIGURED)
  857. g_temp_CC_value = batt_cust_data.usb_charger_current_unconfigured;
  858. else if (g_usb_state == USB_CONFIGURED)
  859. g_temp_CC_value = batt_cust_data.usb_charger_current_configured;
  860. else
  861. g_temp_CC_value = batt_cust_data.usb_charger_current_unconfigured;
  862. battery_log(BAT_LOG_CRTI,
  863. "[BATTERY] STANDARD_HOST CC mode charging : %d on %d state\r\n",
  864. g_temp_CC_value, g_usb_state);
  865. }
  866. #else
  867. {
  868. g_temp_input_CC_value = batt_cust_data.usb_charger_current;
  869. g_temp_CC_value = batt_cust_data.usb_charger_current;
  870. }
  871. #endif
  872. } else if (BMT_status.charger_type == NONSTANDARD_CHARGER) {
  873. g_temp_input_CC_value = batt_cust_data.non_std_ac_charger_current;
  874. g_temp_CC_value = batt_cust_data.non_std_ac_charger_current;
  875. } else if (BMT_status.charger_type == STANDARD_CHARGER) {
  876. if (batt_cust_data.ac_charger_input_current != 0)
  877. g_temp_input_CC_value = batt_cust_data.ac_charger_input_current;
  878. else
  879. g_temp_input_CC_value = batt_cust_data.ac_charger_current;
  880. g_temp_CC_value = batt_cust_data.ac_charger_current;
  881. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  882. if (is_ta_connect == KAL_TRUE)
  883. set_ta_charging_current();
  884. #endif
  885. } else if (BMT_status.charger_type == CHARGING_HOST) {
  886. g_temp_input_CC_value = batt_cust_data.charging_host_charger_current;
  887. g_temp_CC_value = batt_cust_data.charging_host_charger_current;
  888. } else if (BMT_status.charger_type == APPLE_2_1A_CHARGER) {
  889. g_temp_input_CC_value = batt_cust_data.apple_2_1a_charger_current;
  890. g_temp_CC_value = batt_cust_data.apple_2_1a_charger_current;
  891. } else if (BMT_status.charger_type == APPLE_1_0A_CHARGER) {
  892. g_temp_input_CC_value = batt_cust_data.apple_1_0a_charger_current;
  893. g_temp_CC_value = batt_cust_data.apple_1_0a_charger_current;
  894. } else if (BMT_status.charger_type == APPLE_0_5A_CHARGER) {
  895. g_temp_input_CC_value = batt_cust_data.apple_0_5a_charger_current;
  896. g_temp_CC_value = batt_cust_data.apple_0_5a_charger_current;
  897. } else {
  898. g_temp_input_CC_value = CHARGE_CURRENT_500_00_MA;
  899. g_temp_CC_value = CHARGE_CURRENT_500_00_MA;
  900. }
  901. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  902. if (DISO_data.diso_state.cur_vdc_state == DISO_ONLINE) {
  903. g_temp_input_CC_value = batt_cust_data.ac_charger_current;
  904. g_temp_CC_value = batt_cust_data.ac_charger_current;
  905. }
  906. #endif
  907. #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  908. set_jeita_charging_current();
  909. #endif
  910. }
  911. }
  912. static unsigned int charging_full_check(void)
  913. {
  914. unsigned int status;
  915. battery_charging_control(CHARGING_CMD_GET_CHARGING_STATUS, &status);
  916. if (status == KAL_TRUE) {
  917. g_full_check_count++;
  918. if (g_full_check_count >= FULL_CHECK_TIMES)
  919. return KAL_TRUE;
  920. else
  921. return KAL_FALSE;
  922. } /*else {*/
  923. g_full_check_count = 0;
  924. return status;
  925. /*}*/
  926. }
  927. static void pchr_turn_on_charging(void)
  928. {
  929. #if !defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  930. BATTERY_VOLTAGE_ENUM cv_voltage;
  931. #endif
  932. unsigned int charging_enable = KAL_TRUE;
  933. #if defined(CONFIG_MTK_DUAL_INPUT_CHARGER_SUPPORT)
  934. if (KAL_TRUE == BMT_status.charger_exist)
  935. charging_enable = KAL_TRUE;
  936. else
  937. charging_enable = KAL_FALSE;
  938. #endif
  939. if (BMT_status.bat_charging_state == CHR_ERROR) {
  940. battery_log(BAT_LOG_CRTI, "[BATTERY] Charger Error, turn OFF charging !\n");
  941. charging_enable = KAL_FALSE;
  942. } else if ((g_platform_boot_mode == META_BOOT) || (g_platform_boot_mode == ADVMETA_BOOT)) {
  943. battery_log(BAT_LOG_CRTI,
  944. "[BATTERY] In meta or advanced meta mode, disable charging.\n");
  945. charging_enable = KAL_FALSE;
  946. } else {
  947. /*HW initialization */
  948. battery_charging_control(CHARGING_CMD_INIT, NULL);
  949. battery_log(BAT_LOG_FULL, "charging_hw_init\n");
  950. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  951. battery_pump_express_algorithm_start();
  952. #endif
  953. /* Set Charging Current */
  954. if (get_usb_current_unlimited()) {
  955. if (batt_cust_data.ac_charger_input_current != 0)
  956. g_temp_input_CC_value = batt_cust_data.ac_charger_input_current;
  957. else
  958. g_temp_input_CC_value = batt_cust_data.ac_charger_current;
  959. g_temp_CC_value = batt_cust_data.ac_charger_current;
  960. battery_log(BAT_LOG_FULL,
  961. "USB_CURRENT_UNLIMITED, use batt_cust_data.ac_charger_current\n");
  962. #ifndef CONFIG_MTK_SWITCH_INPUT_OUTPUT_CURRENT_SUPPORT
  963. } else if (g_bcct_flag == 1) {
  964. select_charging_current_bcct();
  965. battery_log(BAT_LOG_FULL, "[BATTERY] select_charging_current_bcct !\n");
  966. } else {
  967. select_charging_current();
  968. battery_log(BAT_LOG_FULL, "[BATTERY] select_charging_current !\n");
  969. }
  970. #else
  971. } else if (g_bcct_flag == 1 || g_bcct_input_flag == 1) {
  972. select_charging_current();
  973. select_charging_current_bcct();
  974. battery_log(BAT_LOG_FULL, "[BATTERY] select_charging_curret_bcct !\n");
  975. } else {
  976. select_charging_current();
  977. battery_log(BAT_LOG_FULL, "[BATTERY] select_charging_curret !\n");
  978. }
  979. #endif
  980. battery_log(BAT_LOG_CRTI,
  981. "[BATTERY] Default CC mode charging : %d, input current = %d\r\n",
  982. g_temp_CC_value, g_temp_input_CC_value);
  983. if (g_temp_CC_value == CHARGE_CURRENT_0_00_MA
  984. || g_temp_input_CC_value == CHARGE_CURRENT_0_00_MA) {
  985. charging_enable = KAL_FALSE;
  986. battery_log(BAT_LOG_CRTI,
  987. "[BATTERY] charging current is set 0mA, turn off charging !\r\n");
  988. } else {
  989. battery_charging_control(CHARGING_CMD_SET_INPUT_CURRENT,
  990. &g_temp_input_CC_value);
  991. battery_charging_control(CHARGING_CMD_SET_CURRENT, &g_temp_CC_value);
  992. /*Set CV Voltage */
  993. #if !defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  994. if (batt_cust_data.high_battery_voltage_support)
  995. cv_voltage = BATTERY_VOLT_04_340000_V;
  996. else
  997. cv_voltage = BATTERY_VOLT_04_200000_V;
  998. #ifdef CONFIG_MTK_DYNAMIC_BAT_CV_SUPPORT
  999. cv_voltage = get_constant_voltage() * 1000;
  1000. battery_log(BAT_LOG_CRTI, "[BATTERY][BIF] Setting CV to %d\n", cv_voltage / 1000);
  1001. #endif
  1002. battery_charging_control(CHARGING_CMD_SET_CV_VOLTAGE, &cv_voltage);
  1003. #if defined(CONFIG_MTK_HAFG_20)
  1004. g_cv_voltage = cv_voltage;
  1005. #endif
  1006. #endif
  1007. }
  1008. }
  1009. /* enable/disable charging */
  1010. battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable);
  1011. battery_log(BAT_LOG_FULL, "[BATTERY] pchr_turn_on_charging(), enable =%d !\r\n",
  1012. charging_enable);
  1013. }
  1014. PMU_STATUS BAT_PreChargeModeAction(void)
  1015. {
  1016. battery_log(BAT_LOG_CRTI, "[BATTERY] Pre-CC mode charge, timer=%d on %d !!\n\r",
  1017. BMT_status.PRE_charging_time, BMT_status.total_charging_time);
  1018. BMT_status.PRE_charging_time += BAT_TASK_PERIOD;
  1019. BMT_status.CC_charging_time = 0;
  1020. BMT_status.TOPOFF_charging_time = 0;
  1021. BMT_status.total_charging_time += BAT_TASK_PERIOD;
  1022. /* Enable charger */
  1023. pchr_turn_on_charging();
  1024. #if defined(CONFIG_MTK_HAFG_20)
  1025. if (BMT_status.UI_SOC2 == 100 && charging_full_check()) {
  1026. #else
  1027. if (BMT_status.UI_SOC == 100) {
  1028. #endif
  1029. BMT_status.bat_charging_state = CHR_BATFULL;
  1030. BMT_status.bat_full = KAL_TRUE;
  1031. g_charging_full_reset_bat_meter = KAL_TRUE;
  1032. } else if (BMT_status.bat_vol > V_PRE2CC_THRES) {
  1033. BMT_status.bat_charging_state = CHR_CC;
  1034. }
  1035. return PMU_STATUS_OK;
  1036. }
  1037. PMU_STATUS BAT_ConstantCurrentModeAction(void)
  1038. {
  1039. battery_log(BAT_LOG_FULL, "[BATTERY] CC mode charge, timer=%d on %d !!\n\r",
  1040. BMT_status.CC_charging_time, BMT_status.total_charging_time);
  1041. BMT_status.PRE_charging_time = 0;
  1042. BMT_status.CC_charging_time += BAT_TASK_PERIOD;
  1043. BMT_status.TOPOFF_charging_time = 0;
  1044. BMT_status.total_charging_time += BAT_TASK_PERIOD;
  1045. /* Enable charger */
  1046. pchr_turn_on_charging();
  1047. if (charging_full_check() == KAL_TRUE) {
  1048. BMT_status.bat_charging_state = CHR_BATFULL;
  1049. BMT_status.bat_full = KAL_TRUE;
  1050. g_charging_full_reset_bat_meter = KAL_TRUE;
  1051. }
  1052. return PMU_STATUS_OK;
  1053. }
  1054. PMU_STATUS BAT_BatteryFullAction(void)
  1055. {
  1056. battery_log(BAT_LOG_CRTI, "[BATTERY] Battery full !!\n\r");
  1057. BMT_status.bat_full = KAL_TRUE;
  1058. BMT_status.total_charging_time = 0;
  1059. BMT_status.PRE_charging_time = 0;
  1060. BMT_status.CC_charging_time = 0;
  1061. BMT_status.TOPOFF_charging_time = 0;
  1062. BMT_status.POSTFULL_charging_time = 0;
  1063. BMT_status.bat_in_recharging_state = KAL_FALSE;
  1064. if (charging_full_check() == KAL_FALSE) {
  1065. battery_log(BAT_LOG_CRTI, "[BATTERY] Battery Re-charging !!\n\r");
  1066. BMT_status.bat_in_recharging_state = KAL_TRUE;
  1067. BMT_status.bat_charging_state = CHR_CC;
  1068. #ifndef CONFIG_MTK_HAFG_20
  1069. battery_meter_reset();
  1070. #endif
  1071. #if defined(PUMPEX_PLUS_RECHG) && defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  1072. /*enable PE detection only once when recharge is needed */
  1073. pep_det_rechg = KAL_TRUE;
  1074. #endif
  1075. }
  1076. return PMU_STATUS_OK;
  1077. }
  1078. PMU_STATUS BAT_BatteryHoldAction(void)
  1079. {
  1080. unsigned int charging_enable;
  1081. battery_log(BAT_LOG_CRTI, "[BATTERY] Hold mode !!\n\r");
  1082. if (BMT_status.bat_vol < TALKING_RECHARGE_VOLTAGE || g_call_state == CALL_IDLE) {
  1083. BMT_status.bat_charging_state = CHR_CC;
  1084. battery_log(BAT_LOG_CRTI, "[BATTERY] Exit Hold mode and Enter CC mode !!\n\r");
  1085. }
  1086. /* Disable charger */
  1087. charging_enable = KAL_FALSE;
  1088. battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable);
  1089. return PMU_STATUS_OK;
  1090. }
  1091. PMU_STATUS BAT_BatteryStatusFailAction(void)
  1092. {
  1093. unsigned int charging_enable;
  1094. battery_log(BAT_LOG_CRTI, "[BATTERY] BAD Battery status... Charging Stop !!\n\r");
  1095. #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  1096. if ((g_temp_status == TEMP_ABOVE_POS_60) || (g_temp_status == TEMP_BELOW_NEG_10))
  1097. temp_error_recovery_chr_flag = KAL_FALSE;
  1098. if ((temp_error_recovery_chr_flag == KAL_FALSE) && (g_temp_status != TEMP_ABOVE_POS_60)
  1099. && (g_temp_status != TEMP_BELOW_NEG_10)) {
  1100. temp_error_recovery_chr_flag = KAL_TRUE;
  1101. BMT_status.bat_charging_state = CHR_PRE;
  1102. }
  1103. #endif
  1104. BMT_status.total_charging_time = 0;
  1105. BMT_status.PRE_charging_time = 0;
  1106. BMT_status.CC_charging_time = 0;
  1107. BMT_status.TOPOFF_charging_time = 0;
  1108. BMT_status.POSTFULL_charging_time = 0;
  1109. /* Disable charger */
  1110. charging_enable = KAL_FALSE;
  1111. battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable);
  1112. return PMU_STATUS_OK;
  1113. }
  1114. void mt_battery_charging_algorithm(void)
  1115. {
  1116. battery_charging_control(CHARGING_CMD_RESET_WATCH_DOG_TIMER, NULL);
  1117. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  1118. #if defined(PUMPEX_PLUS_RECHG)
  1119. if (BMT_status.bat_in_recharging_state == KAL_TRUE && pep_det_rechg == KAL_TRUE)
  1120. ta_check_chr_type = KAL_TRUE;
  1121. #endif
  1122. battery_pump_express_charger_check();
  1123. #endif
  1124. switch (BMT_status.bat_charging_state) {
  1125. case CHR_PRE:
  1126. BAT_PreChargeModeAction();
  1127. break;
  1128. case CHR_CC:
  1129. BAT_ConstantCurrentModeAction();
  1130. break;
  1131. case CHR_BATFULL:
  1132. BAT_BatteryFullAction();
  1133. break;
  1134. case CHR_HOLD:
  1135. BAT_BatteryHoldAction();
  1136. break;
  1137. case CHR_ERROR:
  1138. BAT_BatteryStatusFailAction();
  1139. break;
  1140. }
  1141. battery_charging_control(CHARGING_CMD_DUMP_REGISTER, NULL);
  1142. }