power: pm8921-bms: adjust soc

It was observed that the soc(state of charge) reported by the bms was off.
This lead to the phone shutting off because the battery voltage got very
low (UVLO). The expected behaviour is that as battery gets depleted the soc
is reported as 0 by the driver and the system executes a clean shutdown.

Avoid this situation by estimating the instantaneous SOC and adjusting the
reported SOC. The reported SOC is such that it approaches the estimated
soc.

The algorithm to calcualte soc est is as follows
1. Get the simultaneous vbatt and ibatt readings.
2. Get the rbatt of the battery
3. estimated open circuit voltage ocv_est = vbatt + (rbatt * ibatt)
4. calculate rc_est = calcualte_pc(ocv_est) * fcc;
5. soc_est = (rc_est - uuc)/ (fcc - uuc)

The soc est indicates what would be the current soc of the battery solely
based on instantaneous ocv (open circuit voltage) of the battery.

Note that soc_est is subject to fluctuations as the load on the battery
changes - it needs to be weighted accordingly. Let "n" inversely define how
quickly soc approaches the soc_est i.e. a high value in n indicates the
approach is slow and a low value of n indicates we approach the fast. This
implies that the value of n should be smaller as soc_est goes to 0.

Choose a value of n based on where soc and soc_est
are. Experiments showed that

n = min(200, max(1, soc + soc_est + last_soc_est))

yields expected adjusted results. last_soc_est is the soc_est the last time
soc request was made.

Once n is calculated, the last_ocv_uv is adjusted such that soc moves 1/n
steps closer to soc_est. This new soc value "soc_new" is the adjusted
value reported to the userspace.

CRs-Fixed: 349817
Change-Id: I807f051fc1e63b40740e0e0c3a50857ad958bcb6
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
1 file changed