Skip to content

Commit

Permalink
drivers: battery: revert back to stock Oreo battery charger
Browse files Browse the repository at this point in the history
* Remove custom store mode as its causing issues ill explain in the next commit
* Add prevent Swelling and battery health checking support
Signed-off-by: ananjaser1211 <[email protected]>
  • Loading branch information
ananjaser1211 committed Jun 22, 2020
1 parent 1aeb50e commit 559cc5d
Show file tree
Hide file tree
Showing 7 changed files with 988 additions and 55 deletions.
5 changes: 5 additions & 0 deletions drivers/battery/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ config BATTERY_CISD
Say Y to include support for cisd
cisd means cell internal short detection

config PREVENT_SWELLING_BATTERY
bool "support for psb"
help
Say Y to include support for psb

config UPDATE_BATTERY_DATA
bool "support for updating battery data"
default n
Expand Down
1 change: 1 addition & 0 deletions drivers/battery/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ obj-$(CONFIG_STEP_CHARGING) += sec_step_charging.o
obj-$(CONFIG_BATTERY_CISD) += sec_cisd.o
obj-$(CONFIG_UPDATE_BATTERY_DATA) += sec_battery_data.o
obj-$(CONFIG_BATTERY_NOTIFIER) += battery_notifier.o
obj-$(CONFIG_PREVENT_SWELLING_BATTERY) += sec_psb.o

obj-$(CONFIG_FUELGAUGE_MAX17042) += max17042_fuelgauge.o sec_fuelgauge.o
obj-$(CONFIG_FUELGAUGE_MAX17048) += max17048_fuelgauge.o sec_fuelgauge.o
Expand Down
200 changes: 148 additions & 52 deletions drivers/battery/sec_battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
static int wl_polling = 10;
module_param(wl_polling, int, 0644);

#if defined(CONFIG_PREVENT_SWELLING_BATTERY)
#include <linux/battery/sec_psb.h>
#endif

enum {
P9220_VOUT_0V = 0,
P9220_VOUT_5V,
Expand All @@ -39,14 +43,6 @@ enum {
#define SEC_INPUT_VOLTAGE_5V 5
#define SEC_INPUT_VOLTAGE_9V 9

static unsigned int STORE_MODE_CHARGING_MAX = 90;
static unsigned int STORE_MODE_CHARGING_MIN = 20;

module_param_named(store_mode_max, STORE_MODE_CHARGING_MAX, uint, S_IWUSR | S_IRUGO);
module_param_named(store_mode_min, STORE_MODE_CHARGING_MIN, uint, S_IWUSR | S_IRUGO);

const char *charger_chip_name;

bool sleep_mode = false;

static struct device_attribute sec_battery_attrs[] = {
Expand Down Expand Up @@ -602,9 +598,7 @@ static int sec_bat_set_charging_current(struct sec_battery_info *battery)
return 0;
}

static int sec_bat_set_charge(
struct sec_battery_info *battery,
int chg_mode)
int sec_bat_set_charge(struct sec_battery_info *battery, int chg_mode)
{
union power_supply_propval val;
ktime_t current_time;
Expand Down Expand Up @@ -656,17 +650,16 @@ static int sec_bat_set_charge(
return 0;
}

static void sec_bat_set_misc_event(struct sec_battery_info *battery,
const int misc_event_type, bool do_clear) {
void sec_bat_set_misc_event(struct sec_battery_info *battery,
unsigned int misc_event_val, unsigned int misc_event_mask)
{
unsigned int temp = battery->misc_event;

mutex_lock(&battery->misclock);
pr_info("%s: %s misc event(now=0x%x, value=0x%x)\n",
__func__, ((do_clear) ? "clear" : "set"), battery->misc_event, misc_event_type);
if (do_clear) {
battery->misc_event &= ~misc_event_type;
} else {
battery->misc_event |= misc_event_type;
}
battery->misc_event &= ~misc_event_mask;
battery->misc_event |= misc_event_val;
pr_info("%s: misc event before(0x%x), after(0x%x)\n",
__func__, temp, battery->misc_event);
mutex_unlock(&battery->misclock);

if (battery->prev_misc_event != battery->misc_event) {
Expand Down Expand Up @@ -979,8 +972,8 @@ static bool sec_bat_get_cable_type(
return ret;
}

static void sec_bat_set_charging_status(struct sec_battery_info *battery,
int status) {
void sec_bat_set_charging_status(struct sec_battery_info *battery, int status)
{
union power_supply_propval value;
switch (status) {
case POWER_SUPPLY_STATUS_CHARGING:
Expand Down Expand Up @@ -1722,6 +1715,38 @@ static void sec_bat_aging_check(struct sec_battery_info *battery)
}
#endif

static void sec_bat_check_battery_health(struct sec_battery_info *battery)
{
union power_supply_propval value;
battery_health_condition state;
int i, battery_health;

/* check to support ASoC and Cycle */
psy_do_property(battery->pdata->fuelgauge_name, get,
POWER_SUPPLY_PROP_ENERGY_FULL, value);
#if !defined(CONFIG_BATTERY_AGE_FORECAST)
value.intval = -1;
#endif
if (value.intval <= 0 || battery->pdata->health_condition == NULL) {
pr_err("%s: does not support cycle or asoc or health_condition\n", __func__);
return;
}
/* Checking Cycle and ASoC */
state.cycle = state.asoc = BATTERY_HEALTH_BAD;
for (i = BATTERY_HEALTH_MAX - 1; i >= 0; i--) {
if (battery->pdata->health_condition[i].cycle >= (battery->batt_cycle % 10000))
state.cycle = i + BATTERY_HEALTH_GOOD;
if (battery->pdata->health_condition[i].asoc <= battery->batt_asoc)
state.asoc = i + BATTERY_HEALTH_GOOD;
}
battery_health = max(state.cycle, state.asoc);
pr_info("%s: update battery_health(%d), (%d - %d)\n",
__func__, battery_health, state.cycle, state.asoc);
/* Update battery health */
sec_bat_set_misc_event(battery,
(battery_health << BATTERY_HEALTH_SHIFT), BATT_MISC_EVENT_BATTERY_HEALTH);
}

static bool sec_bat_temperature_check(
struct sec_battery_info *battery)
{
Expand Down Expand Up @@ -2712,6 +2737,9 @@ static void sec_bat_do_fullcharged(
#if defined(CONFIG_BATTERY_AGE_FORECAST)
sec_bat_aging_check(battery);
#endif
#if defined(CONFIG_PREVENT_SWELLING_BATTERY)
sec_bat_start_psb(battery);
#endif

value.intval = POWER_SUPPLY_STATUS_FULL;
psy_do_property(battery->pdata->fuelgauge_name, set,
Expand Down Expand Up @@ -3800,6 +3828,11 @@ static void sec_bat_monitor_work(
if (!battery->charging_block && battery->status != POWER_SUPPLY_STATUS_DISCHARGING)
sec_bat_calculate_safety_time(battery);

#if defined(CONFIG_PREVENT_SWELLING_BATTERY)
sec_bat_update_psb_level(battery);
sec_bat_check_psb(battery);
#endif

dev_info(battery->dev,
"%s: Status(%s), mode(%s), Health(%s), Cable(%d), level(%d%%), slate_mode(%d)"
#if defined(CONFIG_AFC_CHARGER_MODE)
Expand Down Expand Up @@ -4113,11 +4146,14 @@ static void sec_bat_cable_work(struct work_struct *work)

if (battery->cable_type == POWER_SUPPLY_TYPE_OTG ||
battery->cable_type == POWER_SUPPLY_TYPE_POWER_SHARING) {
if (sec_bat_set_charge(battery, SEC_BAT_CHG_MODE_CHARGING_OFF))
goto end_of_cable_work;
sec_bat_set_charge(battery, SEC_BAT_CHG_MODE_CHARGING_OFF);
goto end_of_cable_work;
} else if (!keep_charging_state) {
if (sec_bat_set_charge(battery, SEC_BAT_CHG_MODE_CHARGING))
goto end_of_cable_work;
#if defined(CONFIG_PREVENT_SWELLING_BATTERY)
sec_bat_check_full_state(battery);
#else
sec_bat_set_charge(battery, SEC_BAT_CHG_MODE_CHARGING);
#endif
}

#if defined(CONFIG_CALC_TIME_TO_FULL)
Expand Down Expand Up @@ -5130,6 +5166,15 @@ ssize_t sec_bat_store_attrs(
break;
case FG_CAPACITY:
break;
case FG_ASOC:
if (sscanf(buf, "%d\n", &x) == 1) {
if (x >= 0 && x <= 100) {
battery->batt_asoc = x;
sec_bat_check_battery_health(battery);
}
ret = count;
}
break;
case AUTH:
break;
case CHG_CURRENT_ADC:
Expand Down Expand Up @@ -5215,14 +5260,24 @@ ssize_t sec_bat_store_attrs(
break;
case STORE_MODE:
if (sscanf(buf, "%d\n", &x) == 1) {
battery->store_mode = x ? true : false;
ret = count;
if (battery->store_mode) {
union power_supply_propval value;
value.intval = battery->store_mode;
psy_do_property(battery->pdata->charger_name, set,
POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, value);
#if !defined(CONFIG_SEC_FACTORY)
if (x) {
if (!battery->store_mode) {
battery->pdata->wpc_high_temp -= 30;
battery->pdata->wpc_high_temp_recovery -= 30;
}
battery->store_mode |= STORE_MODE_LDU_RDU;
if(battery->capacity <= 5) {
battery->ignore_store_mode = true;
} else {
if(battery->cable_type == POWER_SUPPLY_TYPE_HV_MAINS || \
battery->cable_type == POWER_SUPPLY_TYPE_HV_MAINS_12V ||
battery->cable_type == POWER_SUPPLY_TYPE_HV_ERR)
sec_bat_set_charging_current(battery);
}
}
#endif
ret = count;
}
break;
case UPDATE:
Expand Down Expand Up @@ -5541,6 +5596,7 @@ ssize_t sec_bat_store_attrs(
if (prev_battery_cycle < 0) {
sec_bat_aging_check(battery);
}
sec_bat_check_battery_health(battery);
}
ret = count;
}
Expand Down Expand Up @@ -6169,7 +6225,13 @@ static int sec_bat_get_property(struct power_supply *psy,
return 0;
}
}

#if defined(CONFIG_STORE_MODE)
if (battery->store_mode && !lpcharge &&
battery->cable_type != POWER_SUPPLY_TYPE_BATTERY &&
battery->status == POWER_SUPPLY_STATUS_DISCHARGING) {
val->intval = POWER_SUPPLY_STATUS_CHARGING;
} else
#endif
val->intval = battery->status;
}
break;
Expand Down Expand Up @@ -6743,7 +6805,9 @@ static int batt_handle_notification(struct notifier_block *nb,
#endif
block_water_event &= (battery->muic_cable_type != ATTACHED_DEV_UNDEFINED_CHARGING_MUIC) &&
(battery->muic_cable_type != ATTACHED_DEV_UNDEFINED_RANGE_MUIC);
sec_bat_set_misc_event(battery, BATT_MISC_EVENT_UNDEFINED_RANGE_TYPE, block_water_event);
sec_bat_set_misc_event(battery,
(block_water_event ? 0 : BATT_MISC_EVENT_UNDEFINED_RANGE_TYPE),
BATT_MISC_EVENT_UNDEFINED_RANGE_TYPE);

#ifdef CONFIG_CCIC_NOTIFIER
/* If PD cable is already attached, return this function */
Expand Down Expand Up @@ -7908,23 +7972,47 @@ static int sec_bat_parse_dt(struct device *dev,
kfree(battery->pdata->age_data);
battery->pdata->age_data = NULL;
battery->pdata->num_age_step = 0;
}
pr_err("%s num_age_step : %d\n", __func__, battery->pdata->num_age_step);
for (len = 0; len < battery->pdata->num_age_step; ++len) {
pr_err("[%d/%d]cycle:%d, float:%d, full_v:%d, recharge_v:%d, soc:%d\n",
len, battery->pdata->num_age_step-1,
battery->pdata->age_data[len].cycle,
battery->pdata->age_data[len].float_voltage,
battery->pdata->age_data[len].full_condition_vcell,
battery->pdata->age_data[len].recharge_condition_vcell,
battery->pdata->age_data[len].full_condition_soc);
} else {
pr_err("%s num_age_step : %d\n", __func__, battery->pdata->num_age_step);
for (len = 0; len < battery->pdata->num_age_step; ++len) {
pr_err("[%d/%d]cycle:%d, float:%d, full_v:%d, recharge_v:%d, soc:%d\n",
len, battery->pdata->num_age_step-1,
battery->pdata->age_data[len].cycle,
battery->pdata->age_data[len].float_voltage,
battery->pdata->age_data[len].full_condition_vcell,
battery->pdata->age_data[len].recharge_condition_vcell,
battery->pdata->age_data[len].full_condition_soc);
}
}
} else {
battery->pdata->num_age_step = 0;
pr_err("%s there is not age_data\n", __func__);
}
#endif

p = of_get_property(np, "battery,health_condition", &len);
if (p || (len / sizeof(battery_health_condition)) == BATTERY_HEALTH_MAX) {
battery->pdata->health_condition = kzalloc(len, GFP_KERNEL);
ret = of_property_read_u32_array(np, "battery,health_condition",
(u32 *)battery->pdata->health_condition, len/sizeof(u32));
if (ret) {
pr_err("%s failed to read battery->pdata->health_condition: %d\n",
__func__, ret);
kfree(battery->pdata->health_condition);
battery->pdata->health_condition = NULL;
} else {
for (i = 0; i < BATTERY_HEALTH_MAX; i++) {
pr_err("%s: [BATTERY_HEALTH] %d: Cycle(~ %d), ASoC(~ %d)\n",
__func__, i,
battery->pdata->health_condition[i].cycle,
battery->pdata->health_condition[i].asoc);
}
}
} else {
battery->pdata->health_condition = NULL;
pr_err("%s there is not health_condition, len(%d)\n", __func__, len);
}

ret = of_property_read_u32(np, "battery,siop_event_check_type",
&pdata->siop_event_check_type);
ret = of_property_read_u32(np, "battery,siop_call_cc_current",
Expand Down Expand Up @@ -8166,14 +8254,8 @@ static int sec_battery_probe(struct platform_device *pdev)
battery->cable_type = POWER_SUPPLY_TYPE_BATTERY;
battery->test_mode = 0;
battery->factory_mode = false;
#if defined(CONFIG_STORE_MODE)
battery->store_mode = false;
value.intval = battery->store_mode;
psy_do_property(battery->pdata->charger_name, set,
POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, value);
#else
battery->store_mode = STORE_MODE_NONE;
#endif
battery->ignore_store_mode = false;
battery->slate_mode = false;
battery->is_hc_usb = false;

Expand All @@ -8187,6 +8269,7 @@ static int sec_battery_probe(struct platform_device *pdev)
battery->batt_cycle = -1;
battery->pdata->age_step = 0;
#endif
battery->batt_asoc = 100;

battery->wpc_temp_mode = false;
battery->health_change = false;
Expand Down Expand Up @@ -8346,6 +8429,10 @@ static int sec_battery_probe(struct platform_device *pdev)
goto err_req_irq;
}

#if defined(CONFIG_PREVENT_SWELLING_BATTERY)
sec_bat_init_psb(battery);
#endif

value.intval = POWER_SUPPLY_TYPE_MAINS;
psy_do_property(battery->pdata->charger_name, get,
POWER_SUPPLY_PROP_CURRENT_AVG, value);
Expand Down Expand Up @@ -8375,6 +8462,15 @@ static int sec_battery_probe(struct platform_device *pdev)
psy_do_property(battery->pdata->wireless_charger_name, set,
POWER_SUPPLY_PROP_CHARGE_TYPE, value);

#if defined(CONFIG_STORE_MODE) && !defined(CONFIG_SEC_FACTORY)
battery->store_mode |= STORE_MODE_LDU_RDU;
if (battery->capacity <= 5)
battery->ignore_store_mode = true;

battery->pdata->wpc_high_temp -= 30;
battery->pdata->wpc_high_temp_recovery -= 30;
#endif

#if defined(CONFIG_MUIC_NOTIFIER)
muic_notifier_register(&battery->batt_nb,
batt_handle_notification,
Expand Down
Loading

0 comments on commit 559cc5d

Please sign in to comment.