power: supply: rk817: Fix low SOC bugs
authorChris Morgan <macromorgan@hotmail.com>
Fri, 7 Apr 2023 16:18:27 +0000 (11:18 -0500)
committerSebastian Reichel <sre@kernel.org>
Fri, 7 Apr 2023 22:30:05 +0000 (00:30 +0200)
commitbaba1315a74d12772d4940a05d58dc03e6ec0635
treee612d4d334842977f159446ffbd455e50323eec1
parent528bd42615f4bb564613cc76dead90d7fbae76ba
power: supply: rk817: Fix low SOC bugs

When the SOC approaches zero, an integer overflows in the columb
counter causing the driver to react poorly. This makes the driver
think it's at (above) the fully charged capacity when in fact it's
zero. It would then write this full capacity to NVRAM which would be
used on boot if the device remained off for less than 5 hours and
not plugged in.

This can be fixed and guarded against by doing the following:
 - Changing the type of tmp in rk817_read_or_set_full_charge_on_boot()
   to be an int instead of a u32. That way we can account for negative
   numbers.
 - Guard against negative values for the full charge on boot by setting
   the charge to 0 if the system charge reports less than 0.
 - Catch scenarios where the battery voltage is below the design
   minimum voltage and set the system SOC to 0 at that time and update
   the columb counter with a charge level of 0.
 - Change the off time value from 5 hours to 30 minutes before we
   recalculate the current capacity based on the OCV tables.

These changes allow the driver to operate better at low voltage/low
capacity conditions.

Fixes: 3268a4d9b0b8 ("power: supply: rk817: Fix unsigned comparison with less than zero")
Fixes: 11cb8da0189b ("power: supply: Add charger driver for Rockchip RK817")
Signed-off-by: Chris Morgan <macromorgan@hotmail.com>
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
drivers/power/supply/rk817_charger.c