Skip to content

Commit e132fc6

Browse files
batcheusre
authored andcommitted
power: supply: charger-manager: Make decisions focussed on battery status
cm_monitor(), where charging management starts, checks various charging condition sequentially to decide next charging operation. However, as it follows sequential process, cascaded if statements, it does some jobs which have already done in the previous stage. This results in a delay in decision making. Moreover, starting point of charging is spread all around which makes maintain code and debugging difficult. Both of the problems mentioned above become clean if it manages battery charging focusing on battery status not following sequential condition checking. Now, cm_monitor() moves battery state diagram and does the optimal operation for current state. As a result, it reduces whole monitoring time almost in half. Signed-off-by: Jonghwa Lee <jonghwa3.lee@samsung.com> Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com> Signed-off-by: Jonathan Bakker <xc-racer2@live.ca> Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
1 parent dfc6382 commit e132fc6

2 files changed

Lines changed: 64 additions & 120 deletions

File tree

‎drivers/power/supply/charger-manager.c‎

Lines changed: 61 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,19 @@ static bool is_full_charged(struct charger_manager *cm)
271271
if (!fuel_gauge)
272272
return false;
273273

274+
/* Full, if it's over the fullbatt voltage */
275+
if (desc->fullbatt_uV > 0) {
276+
ret = get_batt_uV(cm, &uV);
277+
if (!ret) {
278+
/* Battery is already full, checks voltage drop. */
279+
if (cm->battery_status == POWER_SUPPLY_STATUS_FULL
280+
&& desc->fullbatt_vchkdrop_uV)
281+
uV += desc->fullbatt_vchkdrop_uV;
282+
if (uV >= desc->fullbatt_uV)
283+
return true;
284+
}
285+
}
286+
274287
if (desc->fullbatt_full_capacity > 0) {
275288
val.intval = 0;
276289

@@ -283,15 +296,6 @@ static bool is_full_charged(struct charger_manager *cm)
283296
}
284297
}
285298

286-
/* Full, if it's over the fullbatt voltage */
287-
if (desc->fullbatt_uV > 0) {
288-
ret = get_batt_uV(cm, &uV);
289-
if (!ret && uV >= desc->fullbatt_uV) {
290-
is_full = true;
291-
goto out;
292-
}
293-
}
294-
295299
/* Full, if the capacity is more than fullbatt_soc */
296300
if (desc->fullbatt_soc > 0) {
297301
val.intval = 0;
@@ -406,66 +410,12 @@ static int try_charger_enable(struct charger_manager *cm, bool enable)
406410
}
407411
}
408412

409-
if (!err) {
413+
if (!err)
410414
cm->charger_enabled = enable;
411-
power_supply_changed(cm->charger_psy);
412-
}
413415

414416
return err;
415417
}
416418

417-
/**
418-
* try_charger_restart - Restart charging.
419-
* @cm: the Charger Manager representing the battery.
420-
*
421-
* Restart charging by turning off and on the charger.
422-
*/
423-
static int try_charger_restart(struct charger_manager *cm)
424-
{
425-
int err;
426-
427-
if (cm->emergency_stop)
428-
return -EAGAIN;
429-
430-
err = try_charger_enable(cm, false);
431-
if (err)
432-
return err;
433-
434-
return try_charger_enable(cm, true);
435-
}
436-
437-
/**
438-
* fullbatt_vchk - Check voltage drop some times after "FULL" event.
439-
*
440-
* If a user has designated "fullbatt_vchkdrop_uV" values with
441-
* charger_desc, Charger Manager checks voltage drop after the battery
442-
* "FULL" event. It checks whether the voltage has dropped more than
443-
* fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
444-
*/
445-
static void fullbatt_vchk(struct charger_manager *cm)
446-
{
447-
struct charger_desc *desc = cm->desc;
448-
int batt_uV, err, diff;
449-
450-
if (!desc->fullbatt_vchkdrop_uV)
451-
return;
452-
453-
err = get_batt_uV(cm, &batt_uV);
454-
if (err) {
455-
dev_err(cm->dev, "%s: get_batt_uV error(%d)\n", __func__, err);
456-
return;
457-
}
458-
459-
diff = desc->fullbatt_uV - batt_uV;
460-
if (diff < 0)
461-
return;
462-
463-
dev_info(cm->dev, "VBATT dropped %duV after full-batt\n", diff);
464-
465-
if (diff > desc->fullbatt_vchkdrop_uV)
466-
try_charger_restart(cm);
467-
}
468-
469419
/**
470420
* check_charging_duration - Monitor charging/discharging duration
471421
* @cm: the Charger Manager representing the battery.
@@ -493,17 +443,14 @@ static int check_charging_duration(struct charger_manager *cm)
493443
if (duration > desc->charging_max_duration_ms) {
494444
dev_info(cm->dev, "Charging duration exceed %ums\n",
495445
desc->charging_max_duration_ms);
496-
try_charger_enable(cm, false);
497446
ret = true;
498447
}
499-
} else if (is_ext_pwr_online(cm) && !cm->charger_enabled) {
448+
} else if (cm->battery_status == POWER_SUPPLY_STATUS_NOT_CHARGING) {
500449
duration = curr - cm->charging_end_time;
501450

502-
if (duration > desc->discharging_max_duration_ms &&
503-
is_ext_pwr_online(cm)) {
451+
if (duration > desc->charging_max_duration_ms) {
504452
dev_info(cm->dev, "Discharging duration exceed %ums\n",
505453
desc->discharging_max_duration_ms);
506-
try_charger_enable(cm, true);
507454
ret = true;
508455
}
509456
}
@@ -585,9 +532,46 @@ static int cm_check_thermal_status(struct charger_manager *cm)
585532
else
586533
ret = CM_BATT_OK;
587534

535+
cm->emergency_stop = ret;
536+
588537
return ret;
589538
}
590539

540+
/**
541+
* cm_get_target_status - Check current status and get next target status.
542+
* @cm: the Charger Manager representing the battery.
543+
*/
544+
static int cm_get_target_status(struct charger_manager *cm)
545+
{
546+
if (!is_ext_pwr_online(cm))
547+
return POWER_SUPPLY_STATUS_DISCHARGING;
548+
549+
if (cm_check_thermal_status(cm)) {
550+
/* Check if discharging duration exeeds limit. */
551+
if (check_charging_duration(cm))
552+
goto charging_ok;
553+
return POWER_SUPPLY_STATUS_NOT_CHARGING;
554+
}
555+
556+
switch (cm->battery_status) {
557+
case POWER_SUPPLY_STATUS_CHARGING:
558+
/* Check if charging duration exeeds limit. */
559+
if (check_charging_duration(cm))
560+
return POWER_SUPPLY_STATUS_FULL;
561+
fallthrough;
562+
case POWER_SUPPLY_STATUS_FULL:
563+
if (is_full_charged(cm))
564+
return POWER_SUPPLY_STATUS_FULL;
565+
fallthrough;
566+
default:
567+
break;
568+
}
569+
570+
charging_ok:
571+
/* Charging is allowed. */
572+
return POWER_SUPPLY_STATUS_CHARGING;
573+
}
574+
591575
/**
592576
* _cm_monitor - Monitor the temperature and return true for exceptions.
593577
* @cm: the Charger Manager representing the battery.
@@ -597,56 +581,18 @@ static int cm_check_thermal_status(struct charger_manager *cm)
597581
*/
598582
static bool _cm_monitor(struct charger_manager *cm)
599583
{
600-
int temp_alrt;
584+
int target;
601585

602-
temp_alrt = cm_check_thermal_status(cm);
586+
target = cm_get_target_status(cm);
603587

604-
/* It has been stopped already */
605-
if (temp_alrt && cm->emergency_stop)
606-
return false;
607-
608-
/*
609-
* Check temperature whether overheat or cold.
610-
* If temperature is out of range normal state, stop charging.
611-
*/
612-
if (temp_alrt) {
613-
cm->emergency_stop = temp_alrt;
614-
try_charger_enable(cm, false);
615-
616-
/*
617-
* Check whole charging duration and discharging duration
618-
* after full-batt.
619-
*/
620-
} else if (!cm->emergency_stop && check_charging_duration(cm)) {
621-
dev_dbg(cm->dev,
622-
"Charging/Discharging duration is out of range\n");
623-
/*
624-
* Check dropped voltage of battery. If battery voltage is more
625-
* dropped than fullbatt_vchkdrop_uV after fully charged state,
626-
* charger-manager have to recharge battery.
627-
*/
628-
} else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
629-
!cm->charger_enabled) {
630-
fullbatt_vchk(cm);
588+
try_charger_enable(cm, (target == POWER_SUPPLY_STATUS_CHARGING));
631589

632-
/*
633-
* Check whether fully charged state to protect overcharge
634-
* if charger-manager is charging for battery.
635-
*/
636-
} else if (!cm->emergency_stop && is_full_charged(cm) &&
637-
cm->charger_enabled) {
638-
dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
639-
try_charger_enable(cm, false);
640-
641-
fullbatt_vchk(cm);
642-
} else {
643-
cm->emergency_stop = 0;
644-
if (is_ext_pwr_online(cm)) {
645-
try_charger_enable(cm, true);
646-
}
590+
if (cm->battery_status != target) {
591+
cm->battery_status = target;
592+
power_supply_changed(cm->charger_psy);
647593
}
648594

649-
return true;
595+
return (cm->battery_status == POWER_SUPPLY_STATUS_NOT_CHARGING);
650596
}
651597

652598
/**
@@ -751,12 +697,7 @@ static int charger_get_property(struct power_supply *psy,
751697

752698
switch (psp) {
753699
case POWER_SUPPLY_PROP_STATUS:
754-
if (is_charging(cm))
755-
val->intval = POWER_SUPPLY_STATUS_CHARGING;
756-
else if (is_ext_pwr_online(cm))
757-
val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
758-
else
759-
val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
700+
val->intval = cm->battery_status;
760701
break;
761702
case POWER_SUPPLY_PROP_HEALTH:
762703
if (cm->emergency_stop > 0)

‎include/linux/power/charger-manager.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ struct charger_desc {
213213
* saved status of battery before entering suspend-to-RAM
214214
* @charging_start_time: saved start time of enabling charging
215215
* @charging_end_time: saved end time of disabling charging
216+
* @battery_status: Current battery status
216217
*/
217218
struct charger_manager {
218219
struct list_head entry;
@@ -232,6 +233,8 @@ struct charger_manager {
232233

233234
u64 charging_start_time;
234235
u64 charging_end_time;
236+
237+
int battery_status;
235238
};
236239

237240
#endif /* _CHARGER_MANAGER_H */

0 commit comments

Comments
 (0)