From 2570e879eb7ff35460de5895db9831d62cc5121b Mon Sep 17 00:00:00 2001 From: litong20 Date: Thu, 18 May 2023 13:45:26 +0800 Subject: [PATCH] save the value of ui_full to the A2 register The value of ui_full will be lost after reboot, so save the value of ui_full to the 0xA2 register. Change-Id: Ib183f9ee745e8e91207f5dd0763729845573ec04 Reviewed-on: https://gerrit.mot.com/2612156 SME-Granted: SME Approvals Granted SLTApproved: Slta Waiver Tested-by: Jira Key Reviewed-by: Wentao Xu Reviewed-by: Guobin Zhang Submit-Approved: Jira Key --- drivers/power/cw2217b_fg_mmi/Android.mk | 7 +++ drivers/power/cw2217b_fg_mmi/Kbuild | 4 ++ .../cw2217b_fg_mmi/cw2217B_fuel_gauge_base.c | 51 +++++++++++++++++-- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/drivers/power/cw2217b_fg_mmi/Android.mk b/drivers/power/cw2217b_fg_mmi/Android.mk index 5789c3877797..e65718be8956 100644 --- a/drivers/power/cw2217b_fg_mmi/Android.mk +++ b/drivers/power/cw2217b_fg_mmi/Android.mk @@ -5,4 +5,11 @@ include $(CLEAR_VARS) LOCAL_MODULE := cw2217b_fg_mmi.ko LOCAL_MODULE_TAGS := optional LOCAL_MODULE_PATH := $(KERNEL_MODULES_OUT) + +ifeq ($(DYNAMIC_UPDATE_UI_FULL),true) + KERNEL_CFLAGS += CONFIG_DYNAMIC_UPDATE_UI_FULL=y + KBUILD_OPTIONS += CONFIG_DYNAMIC_UPDATE_UI_FULL=y +endif +KBUILD_OPTIONS_GKI += GKI_OBJ_MODULE_DIR=gki + include $(DLKM_DIR)/AndroidKernelModule.mk diff --git a/drivers/power/cw2217b_fg_mmi/Kbuild b/drivers/power/cw2217b_fg_mmi/Kbuild index 6f917a2d5680..a84af03167d3 100644 --- a/drivers/power/cw2217b_fg_mmi/Kbuild +++ b/drivers/power/cw2217b_fg_mmi/Kbuild @@ -3,5 +3,9 @@ EXTRA_CFLAGS += -Wall EXTRA_CFLAGS += -I$(ANDROID_BUILD_TOP)/motorola/kernel/modules/include EXTRA_CFLAGS += -I$(ANDROID_BUILD_TOP)/motorola/kernel/drivers/power/cw2217b_fg_mmi +ifneq ($(filter m y,$(CONFIG_DYNAMIC_UPDATE_UI_FULL)),) + EXTRA_CFLAGS += -DDYNAMIC_UPDATE_UI_FULL +endif + cw2217b_fg_mmi-objs += cw2217B_fuel_gauge_base.o obj-m += cw2217b_fg_mmi.o diff --git a/drivers/power/cw2217b_fg_mmi/cw2217B_fuel_gauge_base.c b/drivers/power/cw2217b_fg_mmi/cw2217B_fuel_gauge_base.c index 043177f73bbe..574c18ba7243 100644 --- a/drivers/power/cw2217b_fg_mmi/cw2217B_fuel_gauge_base.c +++ b/drivers/power/cw2217b_fg_mmi/cw2217B_fuel_gauge_base.c @@ -342,7 +342,7 @@ static int cw_get_voltage(struct cw_battery *cw_bat) #define MAX_VAL( x, y ) ( ((x) > (y)) ? (x) : (y) ) #endif -#ifdef SUPPORT_DYNAMIC_UPDATE_UI_FULL +#ifdef DYNAMIC_UPDATE_UI_FULL #define TIMER_INTERVALS 1000 * 60 /* unit:ms */ static bool jiffies_timer_expire(bool clear_flag) { unsigned long cur_jiffies = jiffies; @@ -367,6 +367,42 @@ static bool jiffies_timer_expire(bool clear_flag) { } return ret; } + +static int cw_set_ui_full(struct cw_battery *cw_bat, int ui_full) +{ + int i, ret = 0; + int retry = 3; + unsigned char reg_val = ui_full; + + for (i = 0; i < retry; i++) { + ret = cw_write(cw_bat->client, REG_USER_CONF, ®_val); + if (ret < 0) { + msleep(CW_SLEEP_10MS); + cw_printk("failed to write ui_full to reg, retry = %d", i); + } else { + cw_printk("success save ui_full %d to reg", ui_full); + break; + } + } + return ret; +} + +static int cw_get_ui_full(struct cw_battery *cw_bat, int *ui_full) { + int i, ret = 0; + int retry = 3; + unsigned char reg_val; + + for (i = 0; i < retry; i++) { + ret = cw_read(cw_bat->client, REG_USER_CONF, ®_val); + if (ret >= 0 && reg_val <= CW_UI_FULL && reg_val >= cw_bat->ui_full) { + *ui_full = reg_val; + cw_printk("success read ui_full %d from reg", *ui_full); + break; + } + msleep(CW_SLEEP_10MS); + } + return ret; +} #endif static int cw_get_capacity(struct cw_battery *cw_bat) @@ -378,7 +414,7 @@ static int cw_get_capacity(struct cw_battery *cw_bat) int ui_soc; int remainder; int chr_st_now = 0;// 0 discharge, 1 charging; -#ifndef SUPPORT_DYNAMIC_UPDATE_UI_FULL +#ifndef DYNAMIC_UPDATE_UI_FULL int ui_100 = cw_bat->ui_full; #else static int ui_full_pre = 0; @@ -397,14 +433,17 @@ static int cw_get_capacity(struct cw_battery *cw_bat) soc_l = reg_val[1]; cw_bat->raw_soc = soc_h; -#ifndef SUPPORT_DYNAMIC_UPDATE_UI_FULL +#ifndef DYNAMIC_UPDATE_UI_FULL ui_soc = ((soc_h * 256 + soc_l) * 100)/ (ui_100 * 256); remainder = (((soc_h * 256 + soc_l) * 100 * 100) / (ui_100 * 256)) % 100; #else if (ui_full_pre == 0) { - ui_full_pre = (soc_h * 256 + soc_l) / 256; - if (ui_full_pre < cw_bat->ui_full) + ret = cw_get_ui_full(cw_bat, &ui_full_temp); + if (ret < 0 || ui_full_temp == 0) { ui_full_pre = cw_bat->ui_full; + } else { + ui_full_pre = ui_full_temp; + } cw_printk("CW2015[%d]: UI_FULL INIT is %d", __LINE__, ui_full_pre); } ui_full_temp = ui_full_pre; @@ -413,6 +452,7 @@ static int cw_get_capacity(struct cw_battery *cw_bat) if (ui_full_temp > cw_bat->ui_full) { if (jiffies_timer_expire(false)) { ui_full_temp -= 1; + cw_set_ui_full(cw_bat, ui_full_temp); cw_printk("CW2015[%d]: UI_FULL-- %d!!!!\n", __LINE__, ui_full_temp); } } @@ -422,6 +462,7 @@ static int cw_get_capacity(struct cw_battery *cw_bat) if ((soc_h * 256 + soc_l) / 256 > ui_full_temp) { cw_printk("CW2015[%d]: update UI_FULL to %d from %d !!!!\n", __LINE__, (soc_h * 256 + soc_l) / 256, ui_full_temp); ui_full_temp = (soc_h * 256 + soc_l) / 256; + cw_set_ui_full(cw_bat, ui_full_temp); } jiffies_timer_expire(true); }