Commit 2a721dfc authored by Anton Vorontsov's avatar Anton Vorontsov

apm_power: calculate to_full/to_empty time using energy

This patch adds ability to calculate time using energy.
Needed to calculate time_to_full for PMU batteries.
Signed-off-by: default avatarAnton Vorontsov <cbou@mail.ru>
parent d385376f
...@@ -87,41 +87,63 @@ static void find_main_battery(void) ...@@ -87,41 +87,63 @@ static void find_main_battery(void)
} }
} }
static int calculate_time(int status) static int calculate_time(int status, int using_charge)
{ {
union power_supply_propval charge_full, charge_empty; union power_supply_propval full;
union power_supply_propval charge, I; union power_supply_propval empty;
union power_supply_propval cur;
union power_supply_propval I;
enum power_supply_property full_prop;
enum power_supply_property full_design_prop;
enum power_supply_property empty_prop;
enum power_supply_property empty_design_prop;
enum power_supply_property cur_avg_prop;
enum power_supply_property cur_now_prop;
if (MPSY_PROP(CHARGE_FULL, &charge_full)) { if (MPSY_PROP(CURRENT_AVG, &I)) {
/* if battery can't report this property, use design value */ /* if battery can't report average value, use momentary */
if (MPSY_PROP(CHARGE_FULL_DESIGN, &charge_full)) if (MPSY_PROP(CURRENT_NOW, &I))
return -1; return -1;
} }
if (MPSY_PROP(CHARGE_EMPTY, &charge_empty)) { if (using_charge) {
/* if battery can't report this property, use design value */ full_prop = POWER_SUPPLY_PROP_CHARGE_FULL;
if (MPSY_PROP(CHARGE_EMPTY_DESIGN, &charge_empty)) full_design_prop = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
charge_empty.intval = 0; empty_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
empty_design_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
cur_avg_prop = POWER_SUPPLY_PROP_CHARGE_AVG;
cur_now_prop = POWER_SUPPLY_PROP_CHARGE_NOW;
} else {
full_prop = POWER_SUPPLY_PROP_ENERGY_FULL;
full_design_prop = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
empty_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY;
empty_design_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
cur_avg_prop = POWER_SUPPLY_PROP_ENERGY_AVG;
cur_now_prop = POWER_SUPPLY_PROP_ENERGY_NOW;
} }
if (MPSY_PROP(CHARGE_AVG, &charge)) { if (_MPSY_PROP(full_prop, &full)) {
/* if battery can't report average value, use momentary */ /* if battery can't report this property, use design value */
if (MPSY_PROP(CHARGE_NOW, &charge)) if (_MPSY_PROP(full_design_prop, &full))
return -1; return -1;
} }
if (MPSY_PROP(CURRENT_AVG, &I)) { if (_MPSY_PROP(empty_prop, &empty)) {
/* if battery can't report this property, use design value */
if (_MPSY_PROP(empty_design_prop, &empty))
empty.intval = 0;
}
if (_MPSY_PROP(cur_avg_prop, &cur)) {
/* if battery can't report average value, use momentary */ /* if battery can't report average value, use momentary */
if (MPSY_PROP(CURRENT_NOW, &I)) if (_MPSY_PROP(cur_now_prop, &cur))
return -1; return -1;
} }
if (status == POWER_SUPPLY_STATUS_CHARGING) if (status == POWER_SUPPLY_STATUS_CHARGING)
return ((charge.intval - charge_full.intval) * 60L) / return ((cur.intval - full.intval) * 60L) / I.intval;
I.intval;
else else
return -((charge.intval - charge_empty.intval) * 60L) / return -((cur.intval - empty.intval) * 60L) / I.intval;
I.intval;
} }
static int calculate_capacity(int using_charge) static int calculate_capacity(int using_charge)
...@@ -238,16 +260,22 @@ static void apm_battery_apm_get_power_status(struct apm_power_info *info) ...@@ -238,16 +260,22 @@ static void apm_battery_apm_get_power_status(struct apm_power_info *info)
if (status.intval == POWER_SUPPLY_STATUS_CHARGING) { if (status.intval == POWER_SUPPLY_STATUS_CHARGING) {
if (!MPSY_PROP(TIME_TO_FULL_AVG, &time_to_full) || if (!MPSY_PROP(TIME_TO_FULL_AVG, &time_to_full) ||
!MPSY_PROP(TIME_TO_FULL_NOW, &time_to_full)) !MPSY_PROP(TIME_TO_FULL_NOW, &time_to_full)) {
info->time = time_to_full.intval / 60; info->time = time_to_full.intval / 60;
else } else {
info->time = calculate_time(status.intval); info->time = calculate_time(status.intval, 0);
if (info->time == -1)
info->time = calculate_time(status.intval, 1);
}
} else { } else {
if (!MPSY_PROP(TIME_TO_EMPTY_AVG, &time_to_empty) || if (!MPSY_PROP(TIME_TO_EMPTY_AVG, &time_to_empty) ||
!MPSY_PROP(TIME_TO_EMPTY_NOW, &time_to_empty)) !MPSY_PROP(TIME_TO_EMPTY_NOW, &time_to_empty)) {
info->time = time_to_empty.intval / 60; info->time = time_to_empty.intval / 60;
else } else {
info->time = calculate_time(status.intval); info->time = calculate_time(status.intval, 0);
if (info->time == -1)
info->time = calculate_time(status.intval, 1);
}
} }
up(&power_supply_class->sem); up(&power_supply_class->sem);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment