Skip to content

Commit

Permalink
ayufan: cw2015: approximate current now to have semi-accurate time to…
Browse files Browse the repository at this point in the history
… empty reading
  • Loading branch information
ayufan committed Jun 28, 2019
1 parent 5f6f9db commit 1f28c84
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion drivers/power/cw2015_battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,11 @@ static void cw_bat_work(struct work_struct *work)
msecs_to_jiffies(cw_bat->monitor_sec));
}

static bool cw_battery_valid_time_to_empty(struct cw_battery *cw_bat)
{
return cw_bat->time_to_empty > 0 && cw_bat->time_to_empty < 0x1FFF;
}

static int cw_battery_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
Expand Down Expand Up @@ -637,7 +642,11 @@ static int cw_battery_get_property(struct power_supply *psy,
break;

case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
val->intval = cw_bat->time_to_empty;
if (cw_battery_valid_time_to_empty(cw_bat)) {
val->intval = cw_bat->time_to_empty;
} else {
val->intval = 0;
}
if (cw_bat->bat_mode == MODE_VIRTUAL)
val->intval = VIRTUAL_TIME2EMPTY;
break;
Expand All @@ -658,6 +667,22 @@ static int cw_battery_get_property(struct power_supply *psy,
val->intval = VIRTUAL_TEMPERATURE;
break;

case POWER_SUPPLY_PROP_CURRENT_NOW:
if (cw_battery_valid_time_to_empty(cw_bat)) {
// calculate remaining capacity
val->intval = cw_bat->plat_data.design_capacity * 1000;
val->intval = val->intval * cw_bat->capacity / 100;

// estimate current based on time to empty (in minutes)
val->intval = 60 * val->intval / cw_bat->time_to_empty;
} else {
val->intval = 0;
}

if (cw_bat->bat_mode == MODE_VIRTUAL)
val->intval = VIRTUAL_CURRENT;
break;

default:
break;
}
Expand All @@ -675,6 +700,7 @@ static enum power_supply_property cw_battery_properties[] = {
POWER_SUPPLY_PROP_CHARGE_COUNTER,
POWER_SUPPLY_PROP_CHARGE_FULL,
POWER_SUPPLY_PROP_TEMP,
POWER_SUPPLY_PROP_CURRENT_NOW,
};

static const struct power_supply_desc cw2015_bat_desc = {
Expand Down

0 comments on commit 1f28c84

Please sign in to comment.