From e27d3d79e36adba672ea73301bb5c2cc223b1453 Mon Sep 17 00:00:00 2001 From: itsshashanksp <9945shashank@gmail.com> Date: Sun, 15 Sep 2024 01:07:44 +0530 Subject: [PATCH] power: supply: ti: Get capacity from battery power supply property * If current battery capacity if > 89% then it will enter to PD_PM_STATE_FC2_EXIT mode for LN8000 charger users to avoid disconnection issues. Inspired by commit https://github.com/fiqri19102002/android_kernel_xiaomi_sweet/commit/1003fcea6c130ef3ea07bc0adffb526a60e5513f. Signed-off-by: Shashank Patil <9945shashank@gmail.com> --- drivers/power/supply/ti/pd_policy_manager.c | 37 +++++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/drivers/power/supply/ti/pd_policy_manager.c b/drivers/power/supply/ti/pd_policy_manager.c index 266756770fce..16be3273c12a 100644 --- a/drivers/power/supply/ti/pd_policy_manager.c +++ b/drivers/power/supply/ti/pd_policy_manager.c @@ -46,6 +46,7 @@ //config monitor time (ms) #define PM_WORK_RUN_NORMAL_INTERVAL 500 +#define PM_WORK_RUN_QUICK_INTERVAL 200 #define PM_WORK_RUN_CRITICAL_INTERVAL 100 enum { @@ -145,6 +146,30 @@ static int pd_get_batt_current_thermal_level(struct usbpd_pm *pdpm, int *level) return rc; } +/* get capacity from battery power supply property */ +static int pd_get_batt_capacity(struct usbpd_pm *pdpm, int *capacity) +{ + union power_supply_propval pval = {0,}; + int rc = 0; + + usbpd_check_batt_psy(pdpm); + + if (!pdpm->sw_psy) + return -ENODEV; + + rc = power_supply_get_property(pdpm->sw_psy, + POWER_SUPPLY_PROP_CAPACITY, &pval); + if (rc < 0) { + pr_info("Couldn't get battery capacity:%d\n", rc); + return rc; + } + + pr_info("battery capacity is : %d\n", pval.intval); + + *capacity = pval.intval; + return rc; +} + /* determine whether to disable cp according to jeita status */ static bool pd_disable_cp_by_jeita_status(struct usbpd_pm *pdpm) { @@ -1170,6 +1195,7 @@ static int usbpd_pm_sm(struct usbpd_pm *pdpm) static bool recover; int effective_fcc_val = 0; int thermal_level = 0; + int capacity = 0; static int curr_fcc_lmt, curr_ibus_lmt, retry_count; static int request_fail_count = 0; @@ -1184,6 +1210,9 @@ static int usbpd_pm_sm(struct usbpd_pm *pdpm) pdpm->is_temp_out_fc2_range = pd_disable_cp_by_jeita_status(pdpm); pr_info("is_temp_out_fc2_range:%d\n", pdpm->is_temp_out_fc2_range); + if (ln8000_is_valid) + pd_get_batt_capacity(pdpm, &capacity); + effective_fcc_val = usbpd_get_effective_fcc_val(pdpm); if (effective_fcc_val > 0) { @@ -1197,8 +1226,8 @@ static int usbpd_pm_sm(struct usbpd_pm *pdpm) if (pdpm->cp.vbat_volt < pm_config.min_vbat_for_cp) { pr_info("batt_volt %d, waiting...\n", pdpm->cp.vbat_volt); - } else if (pdpm->cp.vbat_volt > pm_config.bat_volt_lp_lmt - 50) { - pr_info("batt_volt %d is too high for cp, charging with switch charger\n", + } else if (pdpm->cp.vbat_volt > pm_config.bat_volt_lp_lmt - 50 || (capacity > 89 && ln8000_is_valid)) { + pr_info("batt_volt %d or capacity is too high for cp, charging with switch charger\n", pdpm->cp.vbat_volt); usbpd_pm_move_state(pdpm, PD_PM_STATE_FC2_EXIT); if (pm_config.bat_volt_lp_lmt < BAT_VOLT_LOOP_LMT) @@ -1456,7 +1485,9 @@ static void usbpd_pm_workfunc(struct work_struct *work) __func__, pm_config.bat_volt_lp_lmt, pdpm->cp.vbat_volt); if (!usbpd_pm_sm(pdpm) && pdpm->pd_active) { - if (ln8000_is_valid) { + if (pdpm->state == PD_PM_STATE_FC2_ENTRY_2 && ln8000_is_valid) { + interval = PM_WORK_RUN_QUICK_INTERVAL; + } else if (ln8000_is_valid) { interval = PM_WORK_RUN_NORMAL_INTERVAL; } else { interval = PM_WORK_RUN_CRITICAL_INTERVAL;