Commit | Line | Data |
---|---|---|
6fa3eb70 S |
1 | /***************************************************************************** |
2 | * | |
3 | * Filename: | |
4 | * --------- | |
5 | * linear_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 | * HISTORY | |
24 | * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!! | |
25 | *------------------------------------------------------------------------------ | |
26 | *------------------------------------------------------------------------------ | |
27 | * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!! | |
28 | *============================================================================ | |
29 | ****************************************************************************/ | |
30 | #include <linux/kernel.h> | |
31 | #include <mach/battery_common.h> | |
32 | #include <mach/charging.h> | |
33 | #include "cust_charging.h" | |
34 | #include <mach/mt_boot.h> | |
35 | #include <linux/delay.h> | |
36 | #include <mach/battery_meter.h> | |
37 | #include <linux/mutex.h> | |
38 | #include <linux/wakelock.h> | |
39 | ||
6fa3eb70 S |
40 | /* ============================================================ // */ |
41 | /* define */ | |
42 | /* ============================================================ // */ | |
43 | /* cut off to full */ | |
44 | #define POST_CHARGING_TIME 30 * 60 /* 30mins */ | |
45 | #define CV_CHECK_DELAT_FOR_BANDGAP 80 /* 80mV */ | |
46 | #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT) | |
47 | #define BJT_LIMIT 1200000 /* 1.2W */ | |
48 | #ifndef TA_START_VCHR_TUNUNG_VOLTAG | |
49 | #define TA_START_VCHR_TUNUNG_VOLTAGE 3700 /* for isink blink issue */ | |
50 | #define TA_CHARGING_CURRENT CHARGE_CURRENT_1500_00_MA | |
51 | #endif /* TA_START_VCHR_TUNUNG_VOLTAG */ | |
52 | #endif /* MTK_PUMP_EXPRESS_SUPPORT */ | |
53 | ||
54 | #define FULL_CHECK_TIMES 6 | |
55 | ||
56 | /* ============================================================ // */ | |
57 | /* global variable */ | |
58 | /* ============================================================ // */ | |
59 | kal_uint32 g_bcct_flag = 0; | |
60 | CHR_CURRENT_ENUM g_temp_CC_value = CHARGE_CURRENT_0_00_MA; | |
61 | kal_uint32 g_usb_state = USB_UNCONFIGURED; | |
62 | kal_uint32 charging_full_current = CHARGING_FULL_CURRENT; /* mA */ | |
63 | kal_uint32 v_cc2topoff_threshold = V_CC2TOPOFF_THRES; | |
4b9e9796 S |
64 | CHR_CURRENT_ENUM ulc_cv_charging_current = AC_CHARGER_CURRENT; |
65 | kal_bool ulc_cv_charging_current_flag = KAL_FALSE; | |
6fa3eb70 S |
66 | static bool usb_unlimited=false; |
67 | ||
68 | /* [PLATFORM]-Add-BEGIN by TCTSZ.leo.guo, 04/15/2015, modify ntc temperature function */ | |
69 | #ifdef MTK_BATTERY_PROTECT_FEATURE | |
70 | kal_bool high_temp_stop_charge = KAL_FALSE; | |
71 | #endif | |
72 | /* [PLATFORM]-Add-END by TCTSZ.leo.guo */ | |
73 | ||
74 | /* ///////////////////////////////////////////////////////////////////////////////////////// */ | |
75 | /* // JEITA */ | |
76 | /* ///////////////////////////////////////////////////////////////////////////////////////// */ | |
77 | #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT) | |
78 | int g_jeita_recharging_voltage = JEITA_RECHARGE_VOLTAGE; | |
79 | int g_temp_status = TEMP_POS_10_TO_POS_45; | |
80 | kal_bool temp_error_recovery_chr_flag = KAL_TRUE; | |
81 | #endif | |
82 | ||
83 | /* ///////////////////////////////////////////////////////////////////////////////////////// */ | |
84 | /* // PUMP EXPRESS */ | |
85 | /* ///////////////////////////////////////////////////////////////////////////////////////// */ | |
86 | #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT) | |
87 | struct wake_lock TA_charger_suspend_lock; | |
88 | CHR_CURRENT_ENUM ta_charging_current = TA_CHARGING_CURRENT; | |
89 | int ta_current_level = 5000; | |
90 | int ta_pre_vbat = 0; | |
91 | kal_bool ta_check_chr_type = KAL_TRUE; | |
92 | kal_bool ta_check_ta_control = KAL_FALSE; | |
93 | kal_bool ta_vchr_tuning = KAL_FALSE; | |
94 | kal_bool first_vchr_det = KAL_TRUE; | |
95 | kal_bool ta_cable_out_occur = KAL_FALSE; | |
96 | kal_bool is_ta_connect = KAL_FALSE; | |
97 | #endif | |
98 | ||
99 | ||
100 | /* ============================================================ // */ | |
101 | /* function prototype */ | |
102 | /* ============================================================ // */ | |
103 | ||
104 | ||
105 | /* ============================================================ // */ | |
106 | /* extern variable */ | |
107 | /* ============================================================ // */ | |
108 | extern int g_platform_boot_mode; | |
109 | ||
110 | /* ============================================================ // */ | |
111 | /* extern function */ | |
112 | /* ============================================================ // */ | |
113 | ||
114 | ||
115 | /* ============================================================ // */ | |
116 | void BATTERY_SetUSBState(int usb_state_value) | |
117 | { | |
118 | #if defined(CONFIG_POWER_EXT) | |
119 | battery_xlog_printk(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_xlog_printk(BAT_LOG_CRTI, | |
123 | "[BATTERY] BAT_SetUSBState Fail! Restore to default value\r\n"); | |
124 | usb_state_value = USB_UNCONFIGURED; | |
125 | } else { | |
126 | battery_xlog_printk(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 | ||
133 | /* EXPORT_SYMBOL(BATTERY_SetUSBState); */ | |
134 | ||
135 | ||
136 | //EXPORT_SYMBOL(BATTERY_SetUSBState); | |
137 | ||
138 | ||
139 | kal_uint32 get_charging_setting_current() | |
140 | { | |
141 | return g_temp_CC_value; | |
142 | } | |
143 | ||
144 | ||
145 | #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT) | |
146 | ||
147 | static DEFINE_MUTEX(ta_mutex); | |
148 | ||
149 | ||
150 | static void mtk_ta_decrease(void) | |
151 | { | |
152 | kal_bool ta_current_pattern = KAL_FALSE; // FALSE = decrease | |
153 | ||
154 | //if(BMT_status.charger_exist == KAL_TRUE) | |
155 | if(ta_cable_out_occur == KAL_FALSE) { | |
156 | battery_charging_control(CHARGING_CMD_SET_TA_CURRENT_PATTERN,&ta_current_pattern); | |
157 | ta_current_level -= 200; | |
158 | } else { | |
159 | ta_check_chr_type = KAL_TRUE; | |
160 | //is_ta_connect = KAL_FALSE; | |
161 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_decrease() Cable out \n"); | |
162 | } | |
163 | } | |
164 | ||
165 | static void mtk_ta_increase(void) | |
166 | { | |
167 | kal_bool ta_current_pattern = KAL_TRUE; // TRUE = increase | |
168 | ||
169 | //if(BMT_status.charger_exist == KAL_TRUE) | |
170 | if(ta_cable_out_occur == KAL_FALSE) { | |
171 | battery_charging_control(CHARGING_CMD_SET_TA_CURRENT_PATTERN,&ta_current_pattern); | |
172 | ta_current_level += 200; | |
173 | } else { | |
174 | ta_check_chr_type = KAL_TRUE; | |
175 | //is_ta_connect = KAL_FALSE; | |
176 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_increase() Cable out \n"); | |
177 | } | |
178 | } | |
179 | ||
180 | static void mtk_ta_reset_vchr(void) | |
181 | { | |
182 | CHR_CURRENT_ENUM chr_current = CHARGE_CURRENT_70_00_MA; | |
183 | ||
184 | battery_charging_control(CHARGING_CMD_SET_CURRENT,&chr_current); | |
185 | msleep(250); // reset Vchr to 5V | |
186 | ||
187 | ta_current_level = 5000; | |
188 | ||
189 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_reset_vchr(): reset Vchr to 5V \n"); | |
190 | } | |
191 | ||
192 | static void mtk_ta_init(void) | |
193 | { | |
194 | ta_current_level = 5000; | |
195 | is_ta_connect = KAL_FALSE; | |
196 | ta_pre_vbat = 0; | |
197 | ta_vchr_tuning = KAL_FALSE; | |
198 | ta_check_ta_control = KAL_FALSE; | |
199 | ta_cable_out_occur = KAL_FALSE; | |
200 | ||
201 | battery_charging_control(CHARGING_CMD_INIT,NULL); | |
202 | ||
203 | } | |
204 | ||
205 | static void mtk_ta_detector(void) | |
206 | { | |
207 | int real_v_chrA; | |
208 | int real_v_chrB; | |
209 | kal_bool retransmit = KAL_TRUE; | |
210 | kal_uint32 retransmit_count=0; | |
211 | ||
212 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_detector() start\n"); | |
213 | do { | |
214 | real_v_chrA = battery_meter_get_charger_voltage(); | |
215 | mtk_ta_decrease(); | |
216 | mtk_ta_decrease(); | |
217 | real_v_chrB = battery_meter_get_charger_voltage(); | |
218 | ||
219 | if(real_v_chrA - real_v_chrB >= 300) { /* 0.3V */ | |
220 | retransmit = KAL_FALSE; | |
221 | is_ta_connect = KAL_TRUE; | |
222 | } else { | |
223 | retransmit_count++; | |
224 | ||
225 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_detector(): retransmit_count =%d, chrA=%d, chrB=%d\n", | |
226 | retransmit_count, real_v_chrA, real_v_chrB); | |
227 | ||
228 | mtk_ta_reset_vchr(); | |
229 | } | |
230 | ||
231 | if((retransmit_count == 3) || (BMT_status.charger_exist == KAL_FALSE)) { | |
232 | retransmit = KAL_FALSE; | |
233 | is_ta_connect = KAL_FALSE; | |
234 | } | |
235 | ||
236 | } while((retransmit == KAL_TRUE) && (ta_cable_out_occur == KAL_FALSE)); | |
237 | ||
238 | ||
239 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_detector() ta_current_level=%d, real_v_chrA=%d, real_v_chrB=%d, is_ta_connect=%d\n", | |
240 | ta_current_level, real_v_chrA, real_v_chrB,is_ta_connect); | |
241 | ||
242 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_detector() end, retry_count=%d, ta_cable_out_occur=%d \n",retransmit_count,ta_cable_out_occur); | |
243 | } | |
244 | ||
245 | static void mtk_tuning_voltage(int curr_level, int target_level) | |
246 | { | |
247 | int is_increase = 0; | |
248 | int exec_level = 0; | |
249 | CHR_CURRENT_ENUM chr_current = CHARGE_CURRENT_70_00_MA; | |
250 | ||
251 | /* if(BMT_status.charger_exist == KAL_TRUE) */ | |
252 | if(ta_cable_out_occur == KAL_FALSE) { | |
253 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_tuning_voltage() start\n"); | |
254 | ||
255 | if(curr_level >= target_level) { | |
256 | exec_level = (curr_level-target_level)/200; | |
257 | is_increase = 0; | |
258 | } else { | |
259 | exec_level = (target_level-curr_level)/200; | |
260 | is_increase = 1; | |
261 | } | |
262 | ||
263 | if(exec_level == 0) { /* curr_level == target_level */ | |
264 | battery_charging_control(CHARGING_CMD_SET_CURRENT,&chr_current); | |
265 | msleep(50); // for VChr reading to check error occur or not | |
266 | } | |
267 | ||
268 | ||
269 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_tuning_voltage() before : ta_current_level=%d, real_v_chr=%d, is_ta_connect=%d, is_increase=%d, exec_level=%d\n", | |
270 | ta_current_level, battery_meter_get_charger_voltage(), is_ta_connect, is_increase, exec_level); | |
271 | ||
272 | while((exec_level > 0) && (ta_cable_out_occur == KAL_FALSE)) { | |
273 | if(is_increase == 1) | |
274 | mtk_ta_increase(); | |
275 | else | |
276 | mtk_ta_decrease(); | |
277 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_tuning_voltage() after ta_current_level=%d, real_v_chr=%d, is_ta_connect=%d, is_increase=%d, exec_level=%d\n", | |
278 | ta_current_level, battery_meter_get_charger_voltage(), is_ta_connect, is_increase, exec_level); | |
279 | ||
280 | exec_level--; | |
281 | } | |
282 | ||
283 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_tuning_voltage() end\n"); | |
284 | } else { | |
285 | ta_check_chr_type = KAL_TRUE; | |
286 | //is_ta_connect = KAL_FALSE; | |
287 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_tuning_voltage(), Cable Out\n"); | |
288 | } | |
289 | } | |
290 | ||
291 | static void select_v_chr_candidate(int curr_vbat, int ta_v_chr_candidate[]) | |
292 | { | |
293 | battery_xlog_printk(BAT_LOG_CRTI, "select_v_chr_candidate() start\n"); | |
294 | ||
295 | if(curr_vbat > 4200) ta_v_chr_candidate[0]=4600; | |
296 | else if(curr_vbat > 4000) ta_v_chr_candidate[0]=4400; | |
297 | else if(curr_vbat > 3800) ta_v_chr_candidate[0]=4200; | |
298 | else if(curr_vbat > 3600) ta_v_chr_candidate[0]=4000; | |
299 | else ta_v_chr_candidate[0]=3800; | |
300 | ||
301 | ta_v_chr_candidate[1]=ta_v_chr_candidate[0]+200; | |
302 | ta_v_chr_candidate[2]=ta_v_chr_candidate[0]+400; | |
303 | ta_v_chr_candidate[3]=ta_v_chr_candidate[0]+600; | |
304 | ||
305 | battery_xlog_printk(BAT_LOG_CRTI, "select_v_chr_candidate() vbat=%d, candidate=%d,%d,%d\n", | |
306 | curr_vbat, ta_v_chr_candidate[1], ta_v_chr_candidate[2], ta_v_chr_candidate[3]); | |
307 | ||
308 | battery_xlog_printk(BAT_LOG_CRTI, "select_v_chr_candidate() end\n"); | |
309 | } | |
310 | ||
311 | ||
312 | static void mtk_ta_vchr_select(int i,int ta_v_chr_candidate[], int ta_charging_current_candidate[], int *max_charging_current, int *max_charging_current_i) | |
313 | { | |
314 | int current_vchr; | |
315 | kal_bool retransmit = KAL_TRUE; | |
316 | kal_uint32 retransmit_count=0; | |
317 | ||
318 | current_vchr = battery_meter_get_charger_voltage(); | |
319 | if(ta_current_level != 5000 && current_vchr >= 4900) { /* pattern error before, so reset vchr to 5V */ | |
320 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_vchr_select() : curr_VChr=%d, ta_current_level=%d\n",current_vchr,ta_current_level); | |
321 | ||
322 | mtk_ta_reset_vchr(); | |
323 | } | |
324 | ||
325 | do { | |
326 | mtk_tuning_voltage(ta_current_level, ta_v_chr_candidate[i]); | |
327 | ||
328 | current_vchr = battery_meter_get_charger_voltage(); | |
329 | if((abs(current_vchr - ta_current_level) > 300) && (ta_cable_out_occur == KAL_FALSE)) { /* variation > 0.3V, error occur */ | |
330 | retransmit_count++; | |
331 | ||
332 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_vchr_select(): retransmit_count =%d, cur_chr=%d, ta_current_level=%d\n", | |
333 | retransmit_count, current_vchr, ta_current_level); | |
334 | ||
335 | mtk_ta_reset_vchr(); | |
336 | } else { | |
337 | retransmit = KAL_FALSE; | |
338 | } | |
339 | ||
340 | if((retransmit_count == 2) || (ta_cable_out_occur == KAL_TRUE)) { | |
341 | retransmit = KAL_FALSE; | |
342 | } | |
343 | ||
344 | } while((retransmit == KAL_TRUE) && (ta_cable_out_occur == KAL_FALSE)); | |
345 | ||
346 | battery_charging_control(CHARGING_CMD_SET_CURRENT,&ta_charging_current); //1.5A | |
347 | ||
348 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_vchr_select() : use 1.5A for select max current\n"); | |
349 | msleep(900); // over 800ms to avoid interference pattern | |
350 | ||
351 | ta_charging_current_candidate[i] = battery_meter_get_charging_current_imm(); | |
352 | ||
353 | /* we hope to choose the less VChr if the current difference between 2 step is not large, so we add weighting for different VChr step */ | |
354 | if(i == 1) | |
355 | ta_charging_current_candidate[i] += 100; // weighting, plus 120mA for Vbat+0.4V | |
356 | else if(i == 2) | |
357 | ta_charging_current_candidate[i] += 50; // weighting, plug 60mA for Vbat+0.6V | |
358 | ||
359 | if(ta_charging_current_candidate[i] > *max_charging_current) { | |
360 | *max_charging_current = ta_charging_current_candidate[i]; | |
361 | *max_charging_current_i = i; | |
362 | } | |
363 | } | |
364 | ||
365 | ||
366 | static void mtk_ta_BJT_check(void) | |
367 | { | |
368 | int curr_vbat = 0; | |
369 | int curr_current = 0; | |
370 | int vchr = 0; | |
371 | int watt = 0; | |
372 | ||
373 | vchr = battery_meter_get_charger_voltage(); | |
374 | curr_vbat = battery_meter_get_battery_voltage(KAL_TRUE); | |
375 | curr_current = battery_meter_get_charging_current_imm(); | |
376 | ||
377 | watt = ((vchr - curr_vbat)*curr_current); | |
378 | if(watt > BJT_LIMIT) //1.2W | |
379 | is_ta_connect = KAL_FALSE; | |
380 | ||
381 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_BJT_check() vchr=%d, vbat=%d, current=%d, Watt=%d, ta_current_level=%d\n", | |
382 | vchr,curr_vbat,curr_current,watt, ta_current_level); | |
383 | } | |
384 | ||
385 | static void battery_pump_express_charger_check(void) | |
386 | { | |
387 | if (ta_check_chr_type == KAL_TRUE && BMT_status.charger_type == STANDARD_CHARGER) { | |
388 | mutex_lock(&ta_mutex); | |
389 | wake_lock(&TA_charger_suspend_lock); | |
390 | ||
391 | mtk_ta_reset_vchr(); | |
392 | mtk_ta_init(); | |
393 | mtk_ta_detector(); | |
394 | ||
395 | first_vchr_det = KAL_TRUE; | |
396 | ||
397 | if(ta_cable_out_occur == KAL_FALSE) { | |
398 | ta_check_chr_type = KAL_FALSE; | |
399 | } else { | |
400 | /* need to re-check if the charger plug out during ta detector */ | |
401 | ta_check_chr_type = KAL_TRUE; | |
402 | } | |
403 | ||
404 | wake_unlock(&TA_charger_suspend_lock); | |
405 | mutex_unlock(&ta_mutex); | |
406 | } | |
407 | ||
408 | } | |
409 | ||
410 | static void battery_pump_express_algorithm_start(void) | |
411 | { | |
412 | int ta_v_chr_candidate[4]={0,0,0,0}; | |
413 | int ta_charging_current_candidate[4]={0,0,0,0}; | |
414 | int max_charging_current = 0; | |
415 | int max_charging_current_i = 0; | |
416 | int curr_vbat = 0; | |
417 | int i = 0; | |
418 | int ta_cv_vchr; | |
419 | #ifdef HIGH_BATTERY_VOLTAGE_SUPPORT | |
420 | kal_uint32 cv_voltage = 4350; | |
421 | #else | |
422 | kal_uint32 cv_voltage = 4200; | |
423 | #endif | |
424 | ||
425 | mutex_lock(&ta_mutex); | |
426 | wake_lock(&TA_charger_suspend_lock); | |
427 | ||
428 | if(is_ta_connect == KAL_TRUE) { | |
429 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_algorithm() start\n"); | |
430 | ||
431 | curr_vbat = battery_meter_get_battery_voltage(KAL_TRUE); | |
432 | if( ((curr_vbat-ta_pre_vbat)>100) && (curr_vbat < (cv_voltage - (CV_CHECK_DELAT_FOR_BANDGAP+20))) && (curr_vbat > TA_START_VCHR_TUNUNG_VOLTAGE) ) { /*cv -0.12V && to avoid screen flash( VBAT less than 3.7V) */ | |
433 | ta_pre_vbat = curr_vbat; | |
434 | ||
435 | select_v_chr_candidate(curr_vbat, ta_v_chr_candidate); | |
436 | ||
437 | if(first_vchr_det == KAL_TRUE) { | |
438 | for(i=3 ; i>=1 ; i--) { /* measure VBAT+0.8V, VBAT+0.6V then VBAT+0.4V */ | |
439 | if(ta_cable_out_occur == KAL_FALSE) | |
440 | mtk_ta_vchr_select(i,ta_v_chr_candidate,ta_charging_current_candidate,&max_charging_current,&max_charging_current_i); | |
441 | } | |
442 | ||
443 | first_vchr_det = KAL_FALSE; | |
444 | } else { | |
445 | for(i=1 ; i<=3 ; i++) { /* measure VBAT+0.4V,VBAT+0.6V then VBAT+0.8V */ | |
446 | if(ta_cable_out_occur == KAL_FALSE) | |
447 | mtk_ta_vchr_select(i,ta_v_chr_candidate,ta_charging_current_candidate,&max_charging_current,&max_charging_current_i); | |
448 | } | |
449 | } | |
450 | ||
451 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_algorithm() candidate=%d,%d,%d,%d ; i=%d,%d,%d,%d ; max_charging_current_i=%d\n", | |
452 | ta_v_chr_candidate[0], ta_v_chr_candidate[1], ta_v_chr_candidate[2], ta_v_chr_candidate[3], | |
453 | ta_charging_current_candidate[0], ta_charging_current_candidate[1], ta_charging_current_candidate[2],ta_charging_current_candidate[3], | |
454 | max_charging_current_i | |
455 | ); | |
456 | ||
457 | mtk_tuning_voltage(ta_current_level, ta_v_chr_candidate[max_charging_current_i]); | |
458 | ||
459 | ta_vchr_tuning = KAL_TRUE; | |
460 | ta_check_ta_control = KAL_TRUE; | |
461 | } else if(curr_vbat >= (cv_voltage - (CV_CHECK_DELAT_FOR_BANDGAP+20))) { | |
462 | if(cv_voltage == 4200) | |
463 | ta_cv_vchr = 4800; | |
464 | else // cv 4.35V | |
465 | ta_cv_vchr = 5000; | |
466 | ||
467 | if(ta_current_level != ta_cv_vchr) { | |
468 | mtk_tuning_voltage(ta_current_level, ta_cv_vchr); | |
469 | } | |
470 | ||
471 | ta_vchr_tuning = KAL_TRUE; | |
472 | ta_check_ta_control = KAL_FALSE; | |
473 | ||
474 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_algorithm(),curr_vbat > cv_voltage, ta_current_level=%d, cv_voltage=%d, ta_cv_vchr=%d,",ta_current_level,cv_voltage,ta_cv_vchr); | |
475 | } | |
476 | ||
477 | /* --for normal charging */ | |
478 | if((is_ta_connect == KAL_TRUE) && (curr_vbat > TA_START_VCHR_TUNUNG_VOLTAGE) &&(ta_check_ta_control == KAL_TRUE)) { /* to avoid screen flash( VBAT less than 3.7V) */ | |
479 | battery_charging_control(CHARGING_CMD_SET_CURRENT,&ta_charging_current); /* 1.5A */ | |
480 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_algorithm() : detect TA, use 1.5A for normal charging, curr_vbat=%d, ta_pre_vbat=%d, ta_current_level=%d\n", | |
481 | curr_vbat, ta_pre_vbat, ta_current_level); | |
482 | //msleep(1500); | |
483 | } | |
484 | //------------------------ | |
485 | ||
486 | mtk_ta_BJT_check(); | |
487 | battery_xlog_printk(BAT_LOG_CRTI, "mtk_ta_algorithm() end\n"); | |
488 | } else { | |
489 | battery_xlog_printk(BAT_LOG_CRTI, "It's not a TA charger, bypass TA algorithm\n"); | |
490 | } | |
491 | ||
492 | wake_unlock(&TA_charger_suspend_lock); | |
493 | mutex_unlock(&ta_mutex); | |
494 | } | |
495 | ||
496 | #endif | |
497 | ||
498 | #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT) | |
499 | ||
500 | static BATTERY_VOLTAGE_ENUM select_jeita_cv(void) | |
501 | { | |
502 | BATTERY_VOLTAGE_ENUM cv_voltage; | |
503 | ||
504 | if (g_temp_status == TEMP_ABOVE_POS_60) { | |
505 | cv_voltage = JEITA_TEMP_ABOVE_POS_60_CV_VOLTAGE; | |
506 | } else if (g_temp_status == TEMP_POS_45_TO_POS_60) { | |
507 | cv_voltage = JEITA_TEMP_POS_45_TO_POS_60_CV_VOLTAGE; | |
508 | } else if (g_temp_status == TEMP_POS_10_TO_POS_45) { | |
509 | #ifdef HIGH_BATTERY_VOLTAGE_SUPPORT | |
510 | cv_voltage = BATTERY_VOLT_04_350000_V; | |
511 | #else | |
512 | cv_voltage = JEITA_TEMP_POS_10_TO_POS_45_CV_VOLTAGE; | |
513 | #endif | |
514 | } else if (g_temp_status == TEMP_POS_0_TO_POS_10) { | |
515 | cv_voltage = JEITA_TEMP_POS_0_TO_POS_10_CV_VOLTAGE; | |
516 | } else if (g_temp_status == TEMP_NEG_10_TO_POS_0) { | |
517 | cv_voltage = JEITA_TEMP_NEG_10_TO_POS_0_CV_VOLTAGE; | |
518 | } else if (g_temp_status == TEMP_BELOW_NEG_10) { | |
519 | cv_voltage = JEITA_TEMP_BELOW_NEG_10_CV_VOLTAGE; | |
520 | } else { | |
521 | cv_voltage = BATTERY_VOLT_04_200000_V; | |
522 | } | |
523 | ||
524 | return cv_voltage; | |
525 | } | |
526 | ||
527 | PMU_STATUS do_jeita_state_machine(void) | |
528 | { | |
529 | int previous_g_temp_status; | |
530 | BATTERY_VOLTAGE_ENUM cv_voltage; | |
531 | ||
532 | previous_g_temp_status = g_temp_status; | |
533 | /* JEITA battery temp Standard */ | |
534 | if (BMT_status.temperature >= TEMP_POS_60_THRESHOLD) { | |
535 | battery_xlog_printk(BAT_LOG_CRTI, | |
536 | "[BATTERY] Battery Over high Temperature(%d) !!\n\r", | |
537 | TEMP_POS_60_THRESHOLD); | |
538 | g_temp_status = TEMP_ABOVE_POS_60; | |
539 | return PMU_STATUS_FAIL; | |
540 | } else if (BMT_status.temperature > TEMP_POS_45_THRESHOLD) { | |
541 | if ((g_temp_status == TEMP_ABOVE_POS_60) | |
542 | && (BMT_status.temperature >= TEMP_POS_60_THRES_MINUS_X_DEGREE)) { | |
543 | battery_xlog_printk(BAT_LOG_CRTI, | |
544 | "[BATTERY] Battery Temperature between %d and %d,not allow charging yet!!\n\r", | |
545 | TEMP_POS_60_THRES_MINUS_X_DEGREE, | |
546 | TEMP_POS_60_THRESHOLD); | |
547 | return PMU_STATUS_FAIL; | |
548 | } else { | |
549 | battery_xlog_printk(BAT_LOG_CRTI, | |
550 | "[BATTERY] Battery Temperature between %d and %d !!\n\r", | |
551 | TEMP_POS_45_THRESHOLD, TEMP_POS_60_THRESHOLD); | |
552 | g_temp_status = TEMP_POS_45_TO_POS_60; | |
553 | g_jeita_recharging_voltage = JEITA_TEMP_POS_45_TO_POS_60_RECHARGE_VOLTAGE; | |
554 | v_cc2topoff_threshold = JEITA_TEMP_POS_45_TO_POS_60_CC2TOPOFF_THRESHOLD; | |
555 | charging_full_current = CHARGING_FULL_CURRENT; | |
556 | } | |
557 | } else if (BMT_status.temperature >= TEMP_POS_10_THRESHOLD) { | |
558 | if (((g_temp_status == TEMP_POS_45_TO_POS_60) | |
559 | && (BMT_status.temperature >= TEMP_POS_45_THRES_MINUS_X_DEGREE)) | |
560 | || ((g_temp_status == TEMP_POS_0_TO_POS_10) | |
561 | && (BMT_status.temperature <= TEMP_POS_10_THRES_PLUS_X_DEGREE))) { | |
562 | battery_xlog_printk(BAT_LOG_CRTI, | |
563 | "[BATTERY] Battery Temperature not recovery to normal temperature charging mode yet!!\n\r"); | |
564 | } else { | |
565 | battery_xlog_printk(BAT_LOG_CRTI, | |
566 | "[BATTERY] Battery Normal Temperature between %d and %d !!\n\r", | |
567 | TEMP_POS_10_THRESHOLD, TEMP_POS_45_THRESHOLD); | |
568 | ||
569 | g_temp_status = TEMP_POS_10_TO_POS_45; | |
570 | #ifdef HIGH_BATTERY_VOLTAGE_SUPPORT | |
571 | g_jeita_recharging_voltage = 4200; | |
572 | #else | |
573 | g_jeita_recharging_voltage = JEITA_TEMP_POS_10_TO_POS_45_RECHARGE_VOLTAGE; | |
574 | #endif | |
575 | v_cc2topoff_threshold = JEITA_TEMP_POS_10_TO_POS_45_CC2TOPOFF_THRESHOLD; | |
576 | charging_full_current = CHARGING_FULL_CURRENT; | |
577 | } | |
578 | } else if (BMT_status.temperature >= TEMP_POS_0_THRESHOLD) { | |
579 | if ((g_temp_status == TEMP_NEG_10_TO_POS_0 || g_temp_status == TEMP_BELOW_NEG_10) | |
580 | && (BMT_status.temperature <= TEMP_POS_0_THRES_PLUS_X_DEGREE)) { | |
581 | if (g_temp_status == TEMP_NEG_10_TO_POS_0) { | |
582 | battery_xlog_printk(BAT_LOG_CRTI, | |
583 | "[BATTERY] Battery Temperature between %d and %d !!\n\r", | |
584 | TEMP_POS_0_THRES_PLUS_X_DEGREE, | |
585 | TEMP_POS_10_THRESHOLD); | |
586 | } | |
587 | if (g_temp_status == TEMP_BELOW_NEG_10) { | |
588 | battery_xlog_printk(BAT_LOG_CRTI, | |
589 | "[BATTERY] Battery Temperature between %d and %d,not allow charging yet!!\n\r", | |
590 | TEMP_POS_0_THRESHOLD, | |
591 | TEMP_POS_0_THRES_PLUS_X_DEGREE); | |
592 | return PMU_STATUS_FAIL; | |
593 | } | |
594 | } else { | |
595 | battery_xlog_printk(BAT_LOG_CRTI, | |
596 | "[BATTERY] Battery Temperature between %d and %d !!\n\r", | |
597 | TEMP_POS_0_THRESHOLD, TEMP_POS_10_THRESHOLD); | |
598 | g_temp_status = TEMP_POS_0_TO_POS_10; | |
599 | g_jeita_recharging_voltage = JEITA_TEMP_POS_0_TO_POS_10_RECHARGE_VOLTAGE; | |
600 | v_cc2topoff_threshold = JEITA_TEMP_POS_0_TO_POS_10_CC2TOPOFF_THRESHOLD; | |
601 | charging_full_current = CHARGING_FULL_CURRENT; | |
602 | } | |
603 | } else if (BMT_status.temperature >= TEMP_NEG_10_THRESHOLD) { | |
604 | if ((g_temp_status == TEMP_BELOW_NEG_10) | |
605 | && (BMT_status.temperature <= TEMP_NEG_10_THRES_PLUS_X_DEGREE)) { | |
606 | battery_xlog_printk(BAT_LOG_CRTI, | |
607 | "[BATTERY] Battery Temperature between %d and %d,not allow charging yet!!\n\r", | |
608 | TEMP_NEG_10_THRESHOLD, TEMP_NEG_10_THRES_PLUS_X_DEGREE); | |
609 | return PMU_STATUS_FAIL; | |
610 | } else { | |
611 | battery_xlog_printk(BAT_LOG_CRTI, | |
612 | "[BATTERY] Battery Temperature between %d and %d !!\n\r", | |
613 | TEMP_NEG_10_THRESHOLD, TEMP_POS_0_THRESHOLD); | |
614 | g_temp_status = TEMP_NEG_10_TO_POS_0; | |
615 | g_jeita_recharging_voltage = JEITA_TEMP_NEG_10_TO_POS_0_RECHARGE_VOLTAGE; | |
616 | v_cc2topoff_threshold = JEITA_TEMP_NEG_10_TO_POS_0_CC2TOPOFF_THRESHOLD; | |
617 | charging_full_current = JEITA_NEG_10_TO_POS_0_FULL_CURRENT; | |
618 | } | |
619 | } else { | |
620 | battery_xlog_printk(BAT_LOG_CRTI, | |
621 | "[BATTERY] Battery below low Temperature(%d) !!\n\r", | |
622 | TEMP_NEG_10_THRESHOLD); | |
623 | g_temp_status = TEMP_BELOW_NEG_10; | |
624 | return PMU_STATUS_FAIL; | |
625 | } | |
626 | ||
627 | /* set CV after temperature changed */ | |
628 | if (g_temp_status != previous_g_temp_status) { | |
629 | cv_voltage = select_jeita_cv(); | |
630 | battery_charging_control(CHARGING_CMD_SET_CV_VOLTAGE, &cv_voltage); | |
631 | } | |
632 | ||
633 | return PMU_STATUS_OK; | |
634 | } | |
635 | ||
636 | ||
637 | static void set_jeita_charging_current(void) | |
638 | { | |
639 | #ifdef CONFIG_USB_IF | |
640 | if (BMT_status.charger_type == STANDARD_HOST) | |
641 | return; | |
642 | #endif | |
643 | ||
644 | if (g_temp_status == TEMP_NEG_10_TO_POS_0) { | |
645 | g_temp_CC_value = CHARGE_CURRENT_200_00_MA; /* for low temp */ | |
646 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] JEITA set charging current : %d\r\n", | |
647 | g_temp_CC_value); | |
648 | } | |
649 | } | |
650 | ||
651 | #endif | |
652 | ||
653 | bool get_usb_current_unlimited(void) | |
654 | { | |
655 | if (BMT_status.charger_type == STANDARD_HOST || BMT_status.charger_type == CHARGING_HOST) | |
656 | return usb_unlimited; | |
657 | else | |
658 | return false; | |
659 | } | |
660 | ||
661 | void set_usb_current_unlimited(bool enable) | |
662 | { | |
663 | usb_unlimited = enable; | |
664 | } | |
665 | ||
666 | void select_charging_curret_bcct(void) | |
667 | { | |
668 | /* done on set_bat_charging_current_limit */ | |
669 | } | |
670 | ||
671 | ||
672 | kal_uint32 set_bat_charging_current_limit(int current_limit) | |
673 | { | |
674 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] set_bat_charging_current_limit (%d)\r\n", | |
675 | current_limit); | |
676 | ||
677 | if (current_limit != -1) { | |
678 | g_bcct_flag = 1; | |
679 | ||
680 | if (current_limit < 70) | |
681 | g_temp_CC_value = CHARGE_CURRENT_0_00_MA; | |
682 | else if (current_limit < 200) | |
683 | g_temp_CC_value = CHARGE_CURRENT_70_00_MA; | |
684 | else if (current_limit < 300) | |
685 | g_temp_CC_value = CHARGE_CURRENT_200_00_MA; | |
686 | else if (current_limit < 400) | |
687 | g_temp_CC_value = CHARGE_CURRENT_300_00_MA; | |
688 | else if (current_limit < 450) | |
689 | g_temp_CC_value = CHARGE_CURRENT_400_00_MA; | |
690 | else if (current_limit < 550) | |
691 | g_temp_CC_value = CHARGE_CURRENT_450_00_MA; | |
692 | else if (current_limit < 650) | |
693 | g_temp_CC_value = CHARGE_CURRENT_550_00_MA; | |
694 | else if (current_limit < 700) | |
695 | g_temp_CC_value = CHARGE_CURRENT_650_00_MA; | |
696 | else if (current_limit < 800) | |
697 | g_temp_CC_value = CHARGE_CURRENT_700_00_MA; | |
698 | else if (current_limit < 900) | |
699 | g_temp_CC_value = CHARGE_CURRENT_800_00_MA; | |
700 | else if (current_limit < 1000) | |
701 | g_temp_CC_value = CHARGE_CURRENT_900_00_MA; | |
702 | else if (current_limit < 1100) | |
703 | g_temp_CC_value = CHARGE_CURRENT_1000_00_MA; | |
704 | else if (current_limit < 1200) | |
705 | g_temp_CC_value = CHARGE_CURRENT_1100_00_MA; | |
706 | else if (current_limit < 1300) | |
707 | g_temp_CC_value = CHARGE_CURRENT_1200_00_MA; | |
708 | else if (current_limit < 1400) | |
709 | g_temp_CC_value = CHARGE_CURRENT_1300_00_MA; | |
710 | else if (current_limit < 1500) | |
711 | g_temp_CC_value = CHARGE_CURRENT_1400_00_MA; | |
712 | else if (current_limit < 1600) | |
713 | g_temp_CC_value = CHARGE_CURRENT_1500_00_MA; | |
714 | else if (current_limit == 1600) | |
715 | g_temp_CC_value = CHARGE_CURRENT_1600_00_MA; | |
716 | else | |
717 | g_temp_CC_value = CHARGE_CURRENT_450_00_MA; | |
718 | } else { | |
719 | /* change to default current setting */ | |
720 | g_bcct_flag = 0; | |
721 | } | |
722 | ||
723 | wake_up_bat(); | |
724 | ||
725 | return g_bcct_flag; | |
726 | } | |
727 | ||
728 | void set_bat_sw_cv_charging_current_limit(int current_limit) | |
729 | { | |
730 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] set_bat_sw_cv_charging_current_limit (%d)\r\n", current_limit); | |
731 | ||
732 | if(current_limit <= CHARGE_CURRENT_70_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_0_00_MA; | |
733 | else if(current_limit <= CHARGE_CURRENT_200_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_70_00_MA; | |
734 | else if(current_limit <= CHARGE_CURRENT_300_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_200_00_MA; | |
735 | else if(current_limit <= CHARGE_CURRENT_400_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_300_00_MA; | |
736 | else if(current_limit <= CHARGE_CURRENT_450_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_400_00_MA; | |
737 | else if(current_limit <= CHARGE_CURRENT_550_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_450_00_MA; | |
738 | else if(current_limit <= CHARGE_CURRENT_650_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_550_00_MA; | |
739 | else if(current_limit <= CHARGE_CURRENT_700_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_650_00_MA; | |
740 | else if(current_limit <= CHARGE_CURRENT_800_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_700_00_MA; | |
741 | else if(current_limit <= CHARGE_CURRENT_900_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_800_00_MA; | |
742 | else if(current_limit <= CHARGE_CURRENT_1000_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_900_00_MA; | |
743 | else if(current_limit <= CHARGE_CURRENT_1100_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_1000_00_MA; | |
744 | else if(current_limit <= CHARGE_CURRENT_1200_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_1100_00_MA; | |
745 | else if(current_limit <= CHARGE_CURRENT_1300_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_1200_00_MA; | |
746 | else if(current_limit <= CHARGE_CURRENT_1400_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_1300_00_MA; | |
747 | else if(current_limit <= CHARGE_CURRENT_1500_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_1400_00_MA; | |
748 | else if(current_limit <= CHARGE_CURRENT_1600_00_MA) ulc_cv_charging_current=CHARGE_CURRENT_1500_00_MA; | |
749 | else ulc_cv_charging_current=CHARGE_CURRENT_450_00_MA; | |
750 | } | |
751 | ||
752 | void select_charging_curret(void) | |
753 | { | |
754 | if (g_ftm_battery_flag) { | |
755 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] FTM charging : %d\r\n", | |
756 | charging_level_data[0]); | |
757 | g_temp_CC_value = charging_level_data[0]; | |
758 | } else { | |
759 | if (BMT_status.charger_type == STANDARD_HOST) { | |
760 | #ifdef CONFIG_USB_IF | |
761 | { | |
762 | if (g_usb_state == USB_SUSPEND) { | |
763 | g_temp_CC_value = USB_CHARGER_CURRENT_SUSPEND; | |
764 | } else if (g_usb_state == USB_UNCONFIGURED) { | |
765 | g_temp_CC_value = USB_CHARGER_CURRENT_UNCONFIGURED; | |
766 | } else if (g_usb_state == USB_CONFIGURED) { | |
767 | g_temp_CC_value = USB_CHARGER_CURRENT_CONFIGURED; | |
768 | } else { | |
769 | g_temp_CC_value = USB_CHARGER_CURRENT_UNCONFIGURED; | |
770 | } | |
771 | ||
772 | battery_xlog_printk(BAT_LOG_CRTI, | |
773 | "[BATTERY] STANDARD_HOST CC mode charging : %d on %d state\r\n", | |
774 | g_temp_CC_value, g_usb_state); | |
775 | } | |
776 | #else | |
777 | { | |
778 | g_temp_CC_value = USB_CHARGER_CURRENT; | |
779 | } | |
780 | #endif | |
781 | } else if (BMT_status.charger_type == NONSTANDARD_CHARGER) { | |
782 | g_temp_CC_value = NON_STD_AC_CHARGER_CURRENT; | |
783 | } else if (BMT_status.charger_type == STANDARD_CHARGER) { | |
784 | g_temp_CC_value = AC_CHARGER_CURRENT; | |
785 | #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT) | |
786 | if(is_ta_connect == KAL_TRUE && ta_vchr_tuning == KAL_TRUE) | |
787 | g_temp_CC_value = CHARGE_CURRENT_1500_00_MA; | |
788 | #endif | |
789 | } else if (BMT_status.charger_type == CHARGING_HOST) { | |
790 | g_temp_CC_value = CHARGING_HOST_CHARGER_CURRENT; | |
791 | } else if (BMT_status.charger_type == APPLE_2_1A_CHARGER) { | |
792 | g_temp_CC_value = APPLE_2_1A_CHARGER_CURRENT; | |
793 | } else if (BMT_status.charger_type == APPLE_1_0A_CHARGER) { | |
794 | g_temp_CC_value = APPLE_1_0A_CHARGER_CURRENT; | |
795 | } else if (BMT_status.charger_type == APPLE_0_5A_CHARGER) { | |
796 | g_temp_CC_value = APPLE_0_5A_CHARGER_CURRENT; | |
797 | } else { | |
798 | g_temp_CC_value = CHARGE_CURRENT_70_00_MA; | |
799 | } | |
800 | ||
801 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] Default CC mode charging : %d\r\n", | |
802 | g_temp_CC_value); | |
803 | ||
804 | #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT) | |
805 | set_jeita_charging_current(); | |
806 | #endif | |
807 | } | |
808 | } | |
809 | ||
810 | ||
811 | ||
812 | ||
813 | static kal_uint32 charging_full_check(void) | |
814 | { | |
815 | kal_uint32 status = KAL_FALSE; | |
816 | ||
817 | #if defined(POST_TIME_ENABLE) | |
818 | static kal_uint32 post_charging_time = 0; | |
819 | ||
820 | if (post_charging_time >= POST_CHARGING_TIME) { | |
821 | status = KAL_TRUE; | |
822 | post_charging_time = 0; | |
823 | ||
824 | battery_xlog_printk(BAT_LOG_CRTI, | |
825 | "[BATTERY] Battery real full and disable charging on %d mA\n", | |
826 | BMT_status.ICharging); | |
827 | } else if (post_charging_time > 0) { | |
828 | post_charging_time += BAT_TASK_PERIOD; | |
829 | battery_xlog_printk(BAT_LOG_CRTI, | |
830 | "[BATTERY] post_charging_time=%d,POST_CHARGING_TIME=%d\n", | |
831 | post_charging_time, POST_CHARGING_TIME); | |
832 | } else if ((BMT_status.TOPOFF_charging_time > 60) | |
833 | && (BMT_status.ICharging <= charging_full_current)) { | |
834 | post_charging_time = BAT_TASK_PERIOD; | |
835 | battery_xlog_printk(BAT_LOG_CRTI, | |
836 | "[BATTERY] Enter Post charge, post_charging_time=%d,POST_CHARGING_TIME=%d\n", | |
837 | post_charging_time, POST_CHARGING_TIME); | |
838 | } else { | |
839 | post_charging_time = 0; | |
840 | } | |
841 | #else | |
842 | static kal_uint8 full_check_count = 0; | |
843 | ||
844 | if (BMT_status.ICharging <= charging_full_current) { | |
845 | full_check_count++; | |
846 | if (full_check_count >= FULL_CHECK_TIMES) { | |
847 | /* [PLATFORM]-Add-BEGIN by TCTSZ.leo.guo, 04/15/2015, modify ntc temperature function */ | |
848 | #ifdef MTK_BATTERY_PROTECT_FEATURE | |
849 | if (BMT_status.temperature < MAX_LIMIT_CHARGE_TEMPERATURE) { | |
850 | status = KAL_TRUE; | |
851 | } | |
852 | else { | |
853 | high_temp_stop_charge = KAL_TRUE; | |
854 | } | |
855 | #else | |
856 | status = KAL_TRUE; | |
857 | #endif | |
858 | /* [PLATFORM]-Add-END by TCTSZ.leo.guo */ | |
859 | full_check_count = 0; | |
860 | battery_xlog_printk(BAT_LOG_CRTI, | |
861 | "[BATTERY] Battery full and disable charging on %d mA\n", | |
862 | BMT_status.ICharging); | |
863 | } | |
864 | } else { | |
865 | full_check_count = 0; | |
866 | } | |
867 | #endif | |
868 | ||
869 | return status; | |
870 | } | |
871 | ||
872 | ||
873 | static void charging_current_calibration(void) | |
874 | { | |
875 | kal_int32 bat_isense_offset; | |
876 | #if 0 | |
877 | kal_int32 bat_vol = battery_meter_get_battery_voltage(); | |
878 | kal_int32 Vsense = battery_meter_get_VSense(); | |
879 | ||
880 | bat_isense_offset = bat_vol - Vsense; | |
881 | ||
882 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] bat_vol=%d, Vsense=%d, offset=%d \r\n", | |
883 | bat_vol, Vsense, bat_isense_offset); | |
884 | #else | |
885 | bat_isense_offset = 0; | |
886 | #endif | |
887 | ||
888 | battery_meter_sync(bat_isense_offset); | |
889 | } | |
890 | ||
891 | static void pchr_sw_cv_charing_current_check(void) | |
892 | { | |
893 | kal_bool charging_enable = KAL_TRUE; | |
894 | kal_uint32 csdac_full_flag = KAL_TRUE; | |
895 | ||
896 | battery_charging_control(CHARGING_CMD_SET_CURRENT,&ulc_cv_charging_current); | |
897 | battery_charging_control(CHARGING_CMD_ENABLE,&charging_enable); | |
898 | ||
899 | msleep(192); | |
900 | ||
901 | battery_charging_control(CHARGING_CMD_GET_CSDAC_FALL_FLAG,&csdac_full_flag); | |
902 | ||
903 | if(csdac_full_flag == KAL_TRUE) { | |
904 | ulc_cv_charging_current = battery_meter_get_charging_current() * 100; /* get immedeate charging current and align to enum value */ | |
905 | } | |
906 | ||
907 | while(csdac_full_flag == KAL_TRUE && ulc_cv_charging_current !=CHARGE_CURRENT_0_00_MA) { | |
908 | set_bat_sw_cv_charging_current_limit(ulc_cv_charging_current); | |
909 | battery_charging_control(CHARGING_CMD_SET_CURRENT,&ulc_cv_charging_current); | |
910 | ulc_cv_charging_current_flag = KAL_TRUE; | |
911 | ||
912 | msleep(192); /* large than 512 code x 0.25ms */ | |
913 | ||
914 | battery_charging_control(CHARGING_CMD_GET_CSDAC_FALL_FLAG,&csdac_full_flag); | |
915 | ||
916 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] Sw CV set charging current, csdac_full_flag=%d, current=%d !\n",csdac_full_flag,ulc_cv_charging_current); | |
917 | } | |
918 | ||
919 | if(ulc_cv_charging_current == CHARGE_CURRENT_0_00_MA) | |
920 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] Sw CV set charging current Error!\n"); | |
921 | } | |
922 | ||
923 | static void pchr_turn_on_charging(void) | |
924 | { | |
925 | #if !defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT) | |
926 | BATTERY_VOLTAGE_ENUM cv_voltage; | |
927 | #endif | |
928 | kal_uint32 charging_enable = KAL_TRUE; | |
929 | ||
930 | battery_xlog_printk(BAT_LOG_FULL, "[BATTERY] pchr_turn_on_charging()!\r\n"); | |
931 | ||
932 | if (BMT_status.bat_charging_state == CHR_ERROR) { | |
933 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] Charger Error, turn OFF charging !\n"); | |
934 | ||
935 | charging_enable = KAL_FALSE; | |
936 | } else if ((g_platform_boot_mode == META_BOOT) || (g_platform_boot_mode == ADVMETA_BOOT)) { | |
937 | battery_xlog_printk(BAT_LOG_CRTI, | |
938 | "[BATTERY] In meta or advanced meta mode, disable charging.\n"); | |
939 | charging_enable = KAL_FALSE; | |
940 | } else { | |
941 | /*HW initialization */ | |
942 | battery_xlog_printk(BAT_LOG_FULL, "charging_hw_init\n"); | |
943 | battery_charging_control(CHARGING_CMD_INIT, NULL); | |
944 | ||
945 | #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT) | |
946 | battery_pump_express_algorithm_start(); | |
947 | #endif | |
948 | ||
949 | /* Set Charging Current */ | |
950 | if (get_usb_current_unlimited()) { | |
951 | g_temp_CC_value = AC_CHARGER_CURRENT; | |
952 | battery_xlog_printk(BAT_LOG_FULL, "USB_CURRENT_UNLIMITED, use AC_CHARGER_CURRENT\n" ); | |
953 | } else { | |
954 | if (g_bcct_flag == 1) { | |
955 | battery_xlog_printk(BAT_LOG_FULL, | |
956 | "[BATTERY] select_charging_curret_bcct !\n"); | |
957 | select_charging_curret_bcct(); | |
958 | } else { | |
959 | battery_xlog_printk(BAT_LOG_FULL, "[BATTERY] select_charging_current !\n"); | |
960 | select_charging_curret(); | |
961 | } | |
962 | } | |
963 | ||
964 | if (g_temp_CC_value == CHARGE_CURRENT_0_00_MA) { | |
965 | charging_enable = KAL_FALSE; | |
966 | battery_xlog_printk(BAT_LOG_CRTI, | |
967 | "[BATTERY] charging current is set 0mA, turn off charging !\r\n"); | |
968 | } else { | |
969 | #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT) | |
970 | if(ta_check_ta_control == KAL_FALSE) | |
971 | #endif | |
972 | { | |
973 | if(ulc_cv_charging_current_flag == KAL_TRUE) | |
974 | battery_charging_control(CHARGING_CMD_SET_CURRENT,&ulc_cv_charging_current); | |
975 | else | |
976 | battery_charging_control(CHARGING_CMD_SET_CURRENT,&g_temp_CC_value); | |
977 | } | |
978 | ||
979 | /* Set CV */ | |
980 | #if !defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT) | |
981 | #ifdef HIGH_BATTERY_VOLTAGE_SUPPORT | |
982 | cv_voltage = BATTERY_VOLT_04_350000_V; | |
983 | #else | |
984 | cv_voltage = BATTERY_VOLT_04_200000_V; | |
985 | #endif | |
986 | /* [PLATFORM]-Add-BEGIN by TCTSZ.leo.guo, 04/15/2015, modify ntc temperature function */ | |
987 | #ifdef MTK_BATTERY_PROTECT_FEATURE | |
988 | /*Battery temperature more than 45 degree or less than 55 degree, try to limit max voltage*/ | |
989 | if((BMT_status.temperature >= MAX_LIMIT_CHARGE_TEMPERATURE) && (BMT_status.temperature <= MAX_CHARGE_TEMPERATURE) ) | |
990 | { | |
991 | cv_voltage = BATTERY_VOLT_04_100000_V; | |
992 | battery_xlog_printk(BAT_LOG_CRTI, | |
993 | "[BATTERY] temperature more than 45 degree or less than 55 degree, try to limit max voltage !\r\n"); | |
994 | } | |
995 | #endif | |
996 | /* [PLATFORM]-Add-END by TCTSZ.leo.guo */ | |
997 | battery_charging_control(CHARGING_CMD_SET_CV_VOLTAGE, &cv_voltage); | |
998 | #endif | |
999 | } | |
1000 | } | |
1001 | ||
1002 | /* enable/disable charging */ | |
1003 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] pchr_turn_on_charging(), enable =%d \r\n", | |
1004 | charging_enable); | |
1005 | battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable); | |
1006 | ||
1007 | ||
1008 | } | |
1009 | ||
1010 | ||
1011 | PMU_STATUS BAT_PreChargeModeAction(void) | |
1012 | { | |
1013 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] Pre-CC mode charge, timer=%d on %d !!\n\r", | |
1014 | BMT_status.PRE_charging_time, BMT_status.total_charging_time); | |
1015 | ||
1016 | BMT_status.PRE_charging_time += BAT_TASK_PERIOD; | |
1017 | BMT_status.CC_charging_time = 0; | |
1018 | BMT_status.TOPOFF_charging_time = 0; | |
1019 | BMT_status.total_charging_time += BAT_TASK_PERIOD; | |
1020 | ||
1021 | select_charging_curret(); | |
1022 | ulc_cv_charging_current = g_temp_CC_value; | |
1023 | ulc_cv_charging_current_flag = KAL_FALSE; | |
1024 | ||
1025 | if (BMT_status.UI_SOC == 100) { | |
1026 | BMT_status.bat_charging_state = CHR_BATFULL; | |
1027 | BMT_status.bat_full = KAL_TRUE; | |
1028 | g_charging_full_reset_bat_meter = KAL_TRUE; | |
1029 | } else if (BMT_status.bat_vol > V_PRE2CC_THRES) { | |
1030 | BMT_status.bat_charging_state = CHR_CC; | |
1031 | } | |
1032 | ||
1033 | #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT)//defined(MTK_LINEAR_CHARGER_NO_DISCHARGE) | |
1034 | // no disable charging | |
1035 | #else | |
1036 | { | |
1037 | kal_bool charging_enable = KAL_FALSE; | |
6fa3eb70 S |
1038 | |
1039 | /*Charging 9s and discharging 1s : start */ | |
1040 | battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable); | |
1041 | msleep(1000); | |
6fa3eb70 S |
1042 | } |
1043 | #endif | |
1044 | ||
1045 | charging_current_calibration(); | |
1046 | pchr_turn_on_charging(); | |
4b9e9796 | 1047 | |
6fa3eb70 S |
1048 | return PMU_STATUS_OK; |
1049 | } | |
1050 | ||
1051 | ||
1052 | PMU_STATUS BAT_ConstantCurrentModeAction(void) | |
1053 | { | |
1054 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] CC mode charge, timer=%d on %d !!\n\r", | |
1055 | BMT_status.CC_charging_time, BMT_status.total_charging_time); | |
1056 | ||
1057 | BMT_status.PRE_charging_time = 0; | |
1058 | BMT_status.CC_charging_time += BAT_TASK_PERIOD; | |
1059 | BMT_status.TOPOFF_charging_time = 0; | |
1060 | BMT_status.total_charging_time += BAT_TASK_PERIOD; | |
1061 | ||
1062 | ulc_cv_charging_current_flag = KAL_FALSE; | |
1063 | ulc_cv_charging_current = g_temp_CC_value; | |
1064 | ||
1065 | if (BMT_status.bat_vol > v_cc2topoff_threshold) { | |
1066 | BMT_status.bat_charging_state = CHR_TOP_OFF; | |
1067 | } | |
1068 | ||
1069 | #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT)//defined(MTK_LINEAR_CHARGER_NO_DISCHARGE) | |
1070 | // no disable charging#else | |
1071 | #else | |
1072 | { | |
6fa3eb70 | 1073 | kal_bool charging_enable = KAL_FALSE; |
6fa3eb70 S |
1074 | |
1075 | /* Charging 9s and discharging 1s : start */ | |
1076 | battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable); | |
1077 | msleep(1000); | |
6fa3eb70 S |
1078 | } |
1079 | #endif | |
1080 | ||
1081 | charging_current_calibration(); | |
1082 | ||
1083 | pchr_turn_on_charging(); | |
4b9e9796 | 1084 | |
6fa3eb70 S |
1085 | return PMU_STATUS_OK; |
1086 | } | |
1087 | ||
1088 | ||
1089 | PMU_STATUS BAT_TopOffModeAction(void) | |
1090 | { | |
1091 | kal_uint32 charging_enable = KAL_FALSE; | |
1092 | #ifdef HIGH_BATTERY_VOLTAGE_SUPPORT | |
1093 | kal_uint32 cv_voltage = 4350; | |
1094 | #else | |
1095 | kal_uint32 cv_voltage = 4200; | |
1096 | #endif | |
1097 | ||
1098 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] Top Off mode charge, timer=%d on %d !!\n\r", | |
1099 | BMT_status.TOPOFF_charging_time, BMT_status.total_charging_time); | |
1100 | ||
1101 | BMT_status.PRE_charging_time = 0; | |
1102 | BMT_status.CC_charging_time = 0; | |
1103 | BMT_status.TOPOFF_charging_time += BAT_TASK_PERIOD; | |
1104 | BMT_status.total_charging_time += BAT_TASK_PERIOD; | |
1105 | ||
1106 | ||
1107 | if(BMT_status.bat_vol > (cv_voltage-CV_CHECK_DELAT_FOR_BANDGAP)) { /* CV - 0.08V */ | |
1108 | pchr_sw_cv_charing_current_check(); | |
1109 | } | |
1110 | pchr_turn_on_charging(); | |
1111 | ||
1112 | if ((BMT_status.TOPOFF_charging_time >= MAX_CV_CHARGING_TIME) | |
1113 | || (charging_full_check() == KAL_TRUE)) { | |
1114 | BMT_status.bat_charging_state = CHR_BATFULL; | |
1115 | BMT_status.bat_full = KAL_TRUE; | |
1116 | g_charging_full_reset_bat_meter = KAL_TRUE; | |
1117 | ||
1118 | /* Disable charging */ | |
1119 | battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable); | |
1120 | } | |
1121 | ||
1122 | return PMU_STATUS_OK; | |
1123 | } | |
1124 | ||
1125 | ||
1126 | PMU_STATUS BAT_BatteryFullAction(void) | |
1127 | { | |
1128 | kal_uint32 charging_enable = KAL_FALSE; | |
1129 | ||
1130 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] Battery full !!\n\r"); | |
1131 | ||
1132 | BMT_status.bat_full = KAL_TRUE; | |
1133 | BMT_status.total_charging_time = 0; | |
1134 | BMT_status.PRE_charging_time = 0; | |
1135 | BMT_status.CC_charging_time = 0; | |
1136 | BMT_status.TOPOFF_charging_time = 0; | |
1137 | BMT_status.POSTFULL_charging_time = 0; | |
1138 | BMT_status.bat_in_recharging_state = KAL_FALSE; | |
1139 | ||
1140 | ||
1141 | #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT) | |
1142 | if (BMT_status.bat_vol < g_jeita_recharging_voltage) | |
1143 | #else | |
1144 | if (BMT_status.bat_vol < RECHARGING_VOLTAGE) | |
1145 | #endif | |
1146 | { | |
1147 | battery_xlog_printk(BAT_LOG_CRTI, | |
1148 | "[BATTERY] Battery Enter Re-charging!! , vbat=(%d)\n\r", | |
1149 | BMT_status.bat_vol); | |
1150 | ||
1151 | BMT_status.bat_in_recharging_state = KAL_TRUE; | |
1152 | BMT_status.bat_charging_state = CHR_CC; | |
1153 | ulc_cv_charging_current = g_temp_CC_value; | |
1154 | ulc_cv_charging_current_flag = KAL_FALSE; | |
1155 | } | |
1156 | ||
1157 | /* Disable charging */ | |
1158 | battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable); | |
1159 | ||
1160 | return PMU_STATUS_OK; | |
1161 | } | |
1162 | ||
1163 | ||
1164 | PMU_STATUS BAT_BatteryHoldAction(void) | |
1165 | { | |
1166 | kal_uint32 charging_enable; | |
1167 | ||
1168 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] Hold mode !!\n\r"); | |
1169 | ||
1170 | if (BMT_status.bat_vol < TALKING_RECHARGE_VOLTAGE || g_call_state == CALL_IDLE) { | |
1171 | BMT_status.bat_charging_state = CHR_CC; | |
1172 | battery_xlog_printk(BAT_LOG_CRTI, | |
1173 | "[BATTERY] Exit Hold mode and Enter CC mode !!\n\r"); | |
1174 | } | |
1175 | ||
1176 | /* Disable charger */ | |
1177 | charging_enable = KAL_FALSE; | |
1178 | battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable); | |
1179 | ||
1180 | return PMU_STATUS_OK; | |
1181 | } | |
1182 | ||
1183 | ||
1184 | PMU_STATUS BAT_BatteryStatusFailAction(void) | |
1185 | { | |
1186 | kal_uint32 charging_enable; | |
1187 | ||
1188 | battery_xlog_printk(BAT_LOG_CRTI, "[BATTERY] BAD Battery status... Charging Stop !!\n\r"); | |
1189 | ||
1190 | #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT) | |
1191 | if ((g_temp_status == TEMP_ABOVE_POS_60) || (g_temp_status == TEMP_BELOW_NEG_10)) { | |
1192 | temp_error_recovery_chr_flag = KAL_FALSE; | |
1193 | } | |
1194 | if ((temp_error_recovery_chr_flag == KAL_FALSE) && (g_temp_status != TEMP_ABOVE_POS_60) | |
1195 | && (g_temp_status != TEMP_BELOW_NEG_10)) { | |
1196 | temp_error_recovery_chr_flag = KAL_TRUE; | |
1197 | BMT_status.bat_charging_state = CHR_PRE; | |
1198 | } | |
1199 | #endif | |
1200 | ||
1201 | BMT_status.total_charging_time = 0; | |
1202 | BMT_status.PRE_charging_time = 0; | |
1203 | BMT_status.CC_charging_time = 0; | |
1204 | BMT_status.TOPOFF_charging_time = 0; | |
1205 | BMT_status.POSTFULL_charging_time = 0; | |
1206 | ||
1207 | /* Disable charger */ | |
1208 | charging_enable = KAL_FALSE; | |
1209 | battery_charging_control(CHARGING_CMD_ENABLE, &charging_enable); | |
1210 | ||
1211 | return PMU_STATUS_OK; | |
1212 | } | |
1213 | ||
1214 | ||
1215 | void mt_battery_charging_algorithm(void) | |
1216 | { | |
1217 | #if defined(CONFIG_MTK_PUMP_EXPRESS_SUPPORT) | |
1218 | battery_pump_express_charger_check(); | |
1219 | #endif | |
1220 | switch (BMT_status.bat_charging_state) { | |
1221 | case CHR_PRE: | |
1222 | BAT_PreChargeModeAction(); | |
1223 | break; | |
1224 | ||
1225 | case CHR_CC: | |
1226 | BAT_ConstantCurrentModeAction(); | |
1227 | break; | |
1228 | ||
1229 | case CHR_TOP_OFF: | |
1230 | BAT_TopOffModeAction(); | |
1231 | break; | |
1232 | ||
1233 | case CHR_BATFULL: | |
1234 | BAT_BatteryFullAction(); | |
1235 | break; | |
1236 | ||
1237 | case CHR_HOLD: | |
1238 | BAT_BatteryHoldAction(); | |
1239 | break; | |
1240 | ||
1241 | case CHR_ERROR: | |
1242 | BAT_BatteryStatusFailAction(); | |
1243 | break; | |
1244 | } | |
1245 | ||
1246 | } |