From 07daaad308ae62530bd6ab6acee7578e9308d483 Mon Sep 17 00:00:00 2001 From: Yilun Lin Date: Wed, 21 Nov 2018 09:57:10 +0800 Subject: battery/max17055: Update batt_param as a whole. Clearing batt_param everytime when calling battery_get_params may cause AP getting weird battery information through host_command due to preemption. We should update the batt_param as a whole. BUG=b:119322063, b:73347743, b:117061273 TEST=on kukui, remove batt, connect pd, see battery output in console > batt Status: 0x0040 DCHG Param flags:00000000 Temp: 0x0000 = 0.0 K (-273.1 C) V: 0x0000 = 0 mV V-desired: 0x0000 = 0 mV I: 0x0000 = 0 mA I-desired: 0x0000 = 0 mA Charging: Not Allowed Charge: 0 % Manuf: Device: Chem: Serial: 0xffffffff V-design: 0x0f14 = 3860 mV Mode: (unsupported) Abs charge:(unsupported) Remaining: 6355 mAh Cap-full: 6910 mAh (6771 mAh with 98 % compensation) Display: 0.0 % Design: 6910 mAh Time-full: 102h:23 Empty: 102h:23 TEST=on connect batt, connect pd, see battery output in console > batt Status: 0x0000 Param flags:00000003 Temp: 0x0bae = 299.0 K (25.9 C) V: 0x0ef4 = 3828 mV V-desired: 0x1130 = 4400 mV I: 0x0067 = 103 mA(CHG) I-desired: 0x07d0 = 2000 mA Charging: Allowed Charge: 49 % Manuf: Device: Chem: Serial: 0xffffffff V-design: 0x0f14 = 3860 mV Mode: (unsupported) Abs charge:(unsupported) Remaining: 3440 mAh Cap-full: 6910 mAh (6771 mAh with 98 % compensation) Display: 0.0 % Design: 6910 mAh Time-full: 0h:0 Empty: 102h:23 BRANCH=None Change-Id: I752a3842e6ca54dc8cad98a1099387e0088cc48d Signed-off-by: Yilun Lin Reviewed-on: https://chromium-review.googlesource.com/1345551 Commit-Ready: ChromeOS CL Exonerator Bot Tested-by: Yilun Lin Reviewed-by: Philip Chen Reviewed-by: Daisuke Nojiri --- driver/battery/max17055.c | 61 +++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/driver/battery/max17055.c b/driver/battery/max17055.c index 9cdd107d20..1b5679fe37 100644 --- a/driver/battery/max17055.c +++ b/driver/battery/max17055.c @@ -263,64 +263,67 @@ enum battery_present battery_is_present(void) void battery_get_params(struct batt_params *batt) { int reg = 0; + struct batt_params batt_new = {0}; - /* Reset params */ - memset(batt, 0, sizeof(struct batt_params)); /* * Assuming the battery is responsive as long as * max17055 finds battery is present. */ - batt->is_present = battery_is_present(); + batt_new.is_present = battery_is_present(); - if (batt->is_present == BP_YES) - batt->flags |= BATT_FLAG_RESPONSIVE; - else if (batt->is_present == BP_NO) + if (batt_new.is_present == BP_YES) + batt_new.flags |= BATT_FLAG_RESPONSIVE; + else if (batt_new.is_present == BP_NO) /* Battery is not present, gauge won't report useful info. */ - return; + goto batt_out; if (max17055_read(REG_TEMPERATURE, ®)) - batt->flags |= BATT_FLAG_BAD_TEMPERATURE; + batt_new.flags |= BATT_FLAG_BAD_TEMPERATURE; - batt->temperature = TEMPERATURE_CONV((int16_t)reg); + batt_new.temperature = TEMPERATURE_CONV((int16_t)reg); if (max17055_read(REG_STATE_OF_CHARGE, ®) && fake_state_of_charge < 0) - batt->flags |= BATT_FLAG_BAD_STATE_OF_CHARGE; + batt_new.flags |= BATT_FLAG_BAD_STATE_OF_CHARGE; - batt->state_of_charge = fake_state_of_charge >= 0 ? + batt_new.state_of_charge = fake_state_of_charge >= 0 ? fake_state_of_charge : PERCENTAGE_CONV(reg); if (max17055_read(REG_VOLTAGE, ®)) - batt->flags |= BATT_FLAG_BAD_VOLTAGE; + batt_new.flags |= BATT_FLAG_BAD_VOLTAGE; - batt->voltage = VOLTAGE_CONV(reg); + batt_new.voltage = VOLTAGE_CONV(reg); if (max17055_read(REG_CURRENT, ®)) - batt->flags |= BATT_FLAG_BAD_CURRENT; + batt_new.flags |= BATT_FLAG_BAD_CURRENT; - batt->current = CURRENT_CONV((int16_t)reg); + batt_new.current = CURRENT_CONV((int16_t)reg); - batt->desired_voltage = battery_get_info()->voltage_max; - batt->desired_current = BATTERY_DESIRED_CHARGING_CURRENT; + batt_new.desired_voltage = battery_get_info()->voltage_max; + batt_new.desired_current = BATTERY_DESIRED_CHARGING_CURRENT; - if (battery_remaining_capacity(&batt->remaining_capacity)) - batt->flags |= BATT_FLAG_BAD_REMAINING_CAPACITY; + if (battery_remaining_capacity(&batt_new.remaining_capacity)) + batt_new.flags |= BATT_FLAG_BAD_REMAINING_CAPACITY; - if (battery_full_charge_capacity(&batt->full_capacity)) - batt->flags |= BATT_FLAG_BAD_FULL_CAPACITY; + if (battery_full_charge_capacity(&batt_new.full_capacity)) + batt_new.flags |= BATT_FLAG_BAD_FULL_CAPACITY; /* * Charging allowed if both desired voltage and current are nonzero * and battery isn't full (and we read them all correctly). */ - if (!(batt->flags & BATT_FLAG_BAD_STATE_OF_CHARGE) && - batt->desired_voltage && - batt->desired_current && - batt->state_of_charge < BATTERY_LEVEL_FULL) - batt->flags |= BATT_FLAG_WANT_CHARGE; - - if (battery_status(&batt->status)) - batt->flags |= BATT_FLAG_BAD_STATUS; + if (!(batt_new.flags & BATT_FLAG_BAD_STATE_OF_CHARGE) && + batt_new.desired_voltage && + batt_new.desired_current && + batt_new.state_of_charge < BATTERY_LEVEL_FULL) + batt_new.flags |= BATT_FLAG_WANT_CHARGE; + + if (battery_status(&batt_new.status)) + batt_new.flags |= BATT_FLAG_BAD_STATUS; + +batt_out: + /* Update visible battery parameters */ + memcpy(batt, &batt_new, sizeof(*batt)); } #ifdef CONFIG_CMD_PWR_AVG -- cgit v1.2.1