Skip to content

Fix Incorrect State of Charge for RK817

Chris Morgan linux-pm linux-rockchip mfd 4 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 Latest7 patches 0 repliesNew
  2. v37 patches 1 replySuperseded
  3. v24 patches 2 repliesSuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v15 patches7 repliesSuperseded

Changes in v4 from the cover letter

- A few bugs pointed out by Sashiko bot were fixed.
- Corrected spelling of coulomb in new function for resetting counter, after Sashiko pointed out I made the same mistake I just fixed in a prior series. Also corrected variable name to the right units.
- Updated rk817_bat_voltage_uv() function to check for regmap_bulk_read success. If it fails, we return the error and then check later for voltage values less than 0 and treat them appropriately.
- Change logic so an SoC of 0 is handled correctly when looking up values from the OCV table while selecting the boot SoC.

Patches in v4 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/7power: supply: rk817-charger: correct spelling of coulombdrivers/power/supply/rk817_charger.c+10 -100 replies
2/7power: supply: rk817-charger: Fix NVRAM SoC valuedrivers/power/supply/rk817_charger.c+3 -30 replies
3/7power: supply: rk817-charger: simplify re-initializing coulomb counterdrivers/power/supply/rk817_charger.c+23 -210 replies
4/7power: supply: rk817-charger: Resolve boot state of charge from coulomb counterdrivers/power/supply/rk817_charger.c include/linux/mfd/rk808.h+211 -530 replies
5/7power: supply: rk817-charger: Expose boot voltage and OCV valuesdrivers/power/supply/rk817_charger.c+10 -00 replies
6/7power: supply: rk817-charger: Clear SYS_CAN_SD reg by defaultdrivers/power/supply/rk817_charger.c include/linux/mfd/rk808.h+10 -00 replies
7/7power: supply: rk817-charger: Clear full charge cal on resumedrivers/power/supply/rk817_charger.c+3 -00 replies

Reviews trailers per patch

6 of 7 patches have a Reviewed-by or Acked-by.

PatchLee Jones
1/7
2/7A-b
3/7A-b
4/7A-b
5/7A-b
6/7A-b
7/7A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v4 thread

Cover letter [PATCH V4 0/7] Fix Incorrect State of Charge for RK817

From: Chris Morgan <[email protected]>

Users of the popular firmware RockNIX had identified a few bugs with accurately displaying the correct state of charge for RK817 battery devices. With the help of one user analyzing the code with Claude they were able to identify a few potential bugs along with potential solutions which I was then able to verify. These bugs are as follows:

1) Incorrectly clamping a value with a max of 10000 instead of 100000. This bug caused the state of charge to sometimes read as 10% instead of 100% as it should have.

2) Using the boot_voltage value to calibrate the battery in scenarios where it could not be trusted. The boot_voltage register populates immediately at power-on, and the driver would check if this value was older than 30 minutes before using it. Unfortunately when combined with the default behavior of the bootloader to turn the device off when plugged in (requiring a user to explicitly power the device on) the boot_voltage and boot_timer values were wrong. The boot voltage would not account for any voltage added while the device was off, and the boot_counter that tracks the time the device was off would be reset. To fix this we changed the behavior to (outside of first-boot scenarios) use a value saved to NVRAM, the value from the coulomb counter directly, and an OCV lookup from the live battery voltage, and then using those values provide an initial "guess" of the state of charge and recalibrate with the OCV voltage once it is available.

Included in this series is another fix that would cause the battery to drain while the system was shut down. This patch replicates the Rockchip BSP driver to clear a shutdown register. Unlike the BSP there is no behavior to bypass this since no boards have yet been observed using the SYS_CAN_SD bypass on the BSP kernel.

Lastly, it has come to my attention that I misspelled "coulomb" throughout the driver, I have included a patch to fix that.

Changes Since V3:
- A few bugs pointed out by Sashiko bot were fixed.
- Corrected spelling of coulomb in new function for resetting counter, after Sashiko pointed out I made the same mistake I just fixed in a prior series. Also corrected variable name to the right units.
- Updated rk817_bat_voltage_uv() function to check for regmap_bulk_read success. If it fails, we return the error and then check later for voltage values less than 0 and treat them appropriately.
- Change logic so an SoC of 0 is handled correctly when looking up values from the OCV table while selecting the boot SoC.

Changes Since V2:
- Added a patch to correct the spelling of "coulomb".
- Split off creating a new coulomb counter function into another patch.
- Split off exposing the boot voltage and OCV values into another patch.
- The code should be identical for series v3 as it was for series v2, all that changed was the spelling of coulomb and splitting one patch into 3. I'm keeping the ack from v2 since the code has not changed.

Changes Since V1:
- Dropped the sleep fixes, as I would like to test them a bit more extensively first.
- Dropped the devicetree parameter, as since no boards have *yet* needed to alter the default behavior there's no need for it.
- Changed the behavior of how we select the initial SOC and made it its own function to ease in code readibility. This new method was tested with various boot scenarios. The average error was less than 5%, and in a worst case scenario where the device was discharged to 0%, then left to charge at the bootloader the device over-reported its state of charge by about 10%. When this occurred the device reported its SOC as 100% but that it was charging, which was corrected by the new OCV logic that kicked in once charging completed.
- Scrapped the more complicated sleep fix in favor of simply telling the battery to recalibrate at full after suspend, in case of a counting anomaly.

Chris Morgan (7):
power: supply: rk817-charger: correct spelling of coulomb
power: supply: rk817-charger: Fix NVRAM SoC value
power: supply: rk817-charger: simplify re-initializing coulomb counter
power: supply: rk817-charger: Resolve boot state of charge from
coulomb counter
power: supply: rk817-charger: Expose boot voltage and OCV values
power: supply: rk817-charger: Clear SYS_CAN_SD reg by default
power: supply: rk817-charger: Clear full charge cal on resume

 drivers/power/supply/rk817_charger.c | 350 ++++++++++++++++++++-------
 include/linux/mfd/rk808.h            |   3 +
 2 files changed, 268 insertions(+), 85 deletions(-)
Signature · 1 line
2.53.0