power: qpnp-bms: Add FCC learning support

Full charge capacity (FCC) of the battery changes as it ages.
Failing to update the FCC will lead to SOC errors. Add the
following changes to support FCC learning

1. Add configurable parameters to
	- enable/disable fcc learning
	- minimum soc to start FCC learning
	- minimum ocv (pc) to start FCC learning
	- minimum cycles to update fcc vs temp. table

2. New FCC is calculated using the cc count-
	FCC_NEW = (cc_end - cc_start) / (soc2 - soc1)
	cc_end = CC count when charing ends
	cc_start = CC count when charging starts
	soc1, soc2 = starting and ending SOC_rbatt value

3. Use the BMS data registers (0xB4 to 0xBF) to backup
   the generated FCC, temperature, charge cycles and
   charge increase. These backed-up values are restored
   during boot.

While at it, add the logic to readjust the FCC after a
new FCC is generated.

Change-Id: I604e4031061af2b94a90e2a3a9bc7b0b4f0144f3
Signed-off-by: Anirudh Ghayal <aghayal@codeaurora.org>
(cherry picked from commit 7d80d74e8d424ff2455bf88214857099d7243d60)
power: qpnp-bms: Expose the learnt FCC values to userspace

The learnt FCC values need to be backed-up and restored
across reboot. Expose the FCC parameters to the userspace
via sysfs entries to facilitate this. Use sysfs_notify
to indicate a newly generated FCC value.

While at it, add CYCLE_COUNT property to expose the current
charge cycle.

Change-Id: I0fc62e0a3eea88cc082bec3ce4d1bc302be9ace1
Signed-off-by: Anirudh Ghayal <aghayal@codeaurora.org>
(cherry picked from commit 3cc17a5dc64123061afb385e15aa872bc27c27ab)
power: qpnp-bms: Avoid deadlock in reset_vbat_monitoring

Avoid using mutex in reset_vbat_monitoring() as this function is
already called holding the same mutex.

Change-Id: I18f361d78ee3a53f69e57265faa513987961f32b
Signed-off-by: Anirudh Ghayal <aghayal@codeaurora.org>
(cherry picked from commit 0144b8b04925d219edd58e11f5031711214e4a58)
power: qpnp-bms: Improve SoC response time while charging

Currently, there are a number of issues that can cause SoC reported
while charging to lag behind the true calculated SoC.

First, if the charger driver probes before the BMS and starts
charging before the BMS driver probes, the BMS will not know that
the battery is charging. Fix this by checking the charging status at
BMS driver probe time.

Second, In a previous patch, the check for SoC changes was moved in
the calculate_state_of_charge function. This can cause SoC to not be
updated and propagated into userspace at times. Fix this by checking
for SoC changes to an earlier spot in the function.

Third, while charging, the SoC can change faster than once per 20
seconds. Change this limit to 1 SoC per 5 seconds.

Change-Id: I25575b2f3e7170f69a902fd7cc731fc6f06dc06d
Signed-off-by: Xiaozhe Shi <xiaozhes@codeaurora.org>
(cherry picked from commit f96aa1c8d86e89a4bddea8103a1491713f5d5dec)
power: qpnp-bms: Clear fcc_count if battery removal is detected

The last_fcc_update_count tracks the updated location of the
FCC data array. Reset this variable if battery removal is
detected.

While at it, avoid FCC updates from userspace after the intial
set of FCC data tuple. Such a condition can occur if userspace
reboots/restarts.

Change-Id: I7a0eb70f73241874dab6b4c6b39795d18509f0cd
Signed-off-by: Anirudh Ghayal <aghayal@codeaurora.org>
(cherry picked from commit 40bdcd06d8d56677ad9305602d77499fb3c0a5c9)
power_supply: Add CHARGE_COUNTER_SHADOW property to power supply

Add the "CHARGE_COUNTER_SHADOW" property to the power supply
framework that signifies a shadow charge counter of the power supply.
This should be a shadowed version of the charge counter that is not
cleared automatically.
The units for this property should be in microamp-hours.

Change-Id: Ia9504a3c36af305e42d0503cf791594449249fa0
Signed-off-by: Xiaozhe Shi <xiaozhes@codeaurora.org>
(cherry picked from commit 4090f4f77cc988d437e3d32a6a4a64749c9df794)
power: qpnp-bms: export shadow coulomb counter value

Export the shadow coulomb counter register value to userspace in
order to assist in recording power consumption.

Change-Id: I47ac60ce3d17e96f9cef418f5f346ba278d09521
Signed-off-by: Xiaozhe Shi <xiaozhes@codeaurora.org>
(cherry picked from commit df24c359f217b7252ed4f504c74c89f1d708aee3)
power: qpnp-bms: improve coulomb counter shadow performance

Improve the shadow coulomb counter performance by keeping a software
shadow of it and periodically adding error compensated readings.

Change-Id: Ie549b7d0eef362f7473621de78f536173dcff09d
Signed-off-by: Xiaozhe Shi <xiaozhes@codeaurora.org>
(cherry picked from commit 4182488a8f08746beb8fb66ccb68103ac9070558)
power: qpnp-bms: wake up when SoC changes while battery is full

Currently, the apps processor can be asleep when SoC drops below
the charging resume limit. Change this by setting the good_ocv and
cc_thr interrupts to be wakeable, and configuring the coulomb
counter threshold to be when SoC would change.

Change-Id: Ibefaf82570f875ca0ceabf0d2f693ba3e14ef595
Signed-off-by: Xiaozhe Shi <xiaozhes@codeaurora.org>
(cherry picked from commit 32abbd1a277c25c81ab418e3308e1c2326f889da)
power: qpnp-bms: use a separate flag to indicate registration

The current code uses the name of the bms power supply to indicate that
it is successfully registered. There is a time window where the name is
set but bms power supply is not registered. Interrupts executing during
this time window will call power supply apis on the bms power supply
since they see the name member initialized. These api calls on a non
registered power supply causes errors, most notably

[] (__queue_work+0x2b8/0x42c) from [] (queue_work_on+0x3c/0x44)
[] (queue_work_on+0x3c/0x44) from [] (queue_work+0x24/0x58)
[] (queue_work+0x24/0x58) from [] (btm_notify_vbat+0xb0/0x534)
[] (btm_notify_vbat+0xb0/0x534) from [] (notify_adc_tm_fn+0x98/0x154)
[] (notify_adc_tm_fn+0x98/0x154) from [] (process_one_work+0x12c/0x4b4)
[] (process_one_work+0x12c/0x4b4) from [] (worker_thread+0x160/0x3d4)
[] (worker_thread+0x160/0x3d4) from [] (kthread+0x84/0x90)
[] (kthread+0x84/0x90) from [] (kernel_thread_exit+0x0/0x8)

Update the code to not use name member and create a separate boolean to
indicate bms power supply is successfully registered.

Change-Id: I09c1d848096f4bd5de4ee4be1e0edeb100bf6113
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 267092b9eaf75fdac1b632044824cc49a5daf59e)
power: qpnp-bms: Support POWER_SUPPLY_PROP_CHARGE_FULL property

This property reports the real(current) full charge capacity (FCC)
in uAh.

Change-Id: I2ebf1333a53723c4c42800b073ce464efc148e19
Signed-off-by: Anirudh Ghayal <aghayal@codeaurora.org>
(cherry picked from commit e0155640c4a4c52ec42a8fddac2cf88d5d5657e5)
power: qpnp-bms: request interrupts at the end of probe

The current driver requests interrupts before it initializes other
data structures. This creates NULL pointer exceptions if an interrupt
triggers before the data structures are initialized.

Move requesting interrupts towards the end of the probe function, after
data structures are initialized.

CRs-Fixed: 506597
Change-Id: I94a524a0a1b8a5fa9372e414bcf36d8448fc1e4a
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 672c57f2f0906c6cbf4eb95bd7e772c9cba4ea43)
arm/dt: 8974: Configure FCC learning parameters

Set the Full Charge Capacity (FCC) learning parameters
and enable FCC learning.

Change-Id: I7119eb60886509b8c3a40cfea8e7907386c5088c
Signed-off-by: Anirudh Ghayal <aghayal@codeaurora.org>
(cherry picked from commit 5dbe631bd1f4b869cbcd8bda7a443712c5c74720)
power: qpnp-bms: Store FCC learnt data in BMS registers

BMS module supports 16 (8-bit) battery backed-up registers.
Use these registers to store the learnt FCC data. Prior to
this change, the generated FCC data was expected to be
maintained by userspace.

Every new FCC value is first interpolated to a default
temperature (this eliminates the use of storing the
temperature at which the FCC is generated). The FCC value
(in mAh) is mapped to a 8-bit value and stored accordingly.
Same encoding mechanism is used to store the charge-cycles.

Change-Id: I1f5870afbccb306cc2e54b1d539f8446c10540f6
Signed-off-by: Anirudh Ghayal <aghayal@codeaurora.org>
(cherry picked from commit 21abfcaa00fe24df052802160c864cc54c59969a)
power: qpnp-bms: Remove the FCC sysfs files and module parameters

The FCC data is now stored in the battery backed-up
registers in the BMS. It is not required to expose
them to userspace.

Change-Id: Id88f7ba13317a2f15ff93d9409106d84443e1c33
Signed-off-by: Anirudh Ghayal <aghayal@codeaurora.org>
(cherry picked from commit 6de5df9d146680df639fa8d0ba91161c861f8a01)
power: qpnp-charger: enable vbatdet-lo-irq falling edge

Enable the falling edge interrupt for VBATDET_LO to
wake up when the battery level falls below the configured
vbatdet voltage.

Change-Id: I266e55e242dc133125359dd5fed5c46d389d54f4
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
(cherry picked from commit 48e7f902e6a373f07d068eda77afa7c7307144bb)
power: qpnp-charger: move vadc_is_ready()

To avoid any late -EPROBE_DEFER probe returns
add logic to check for vadc at the beginning
of probe to allow the deferral to happen early
during probe.

CRs-Fixed: 470329
Change-Id: I86b15409993036d397ddd8061612ef2d65a0e4dd
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
(cherry picked from commit 420498e92b8f299f61d7a092fb43ee01a0b7f4fb)
power: qpnp-charger: enable IR drop compensation

Change from using VPH_PWR sense to VBAT_SNS to
regulate VBAT. This should allow the charger
to correctly regulate vbat at 4.2V when
end of charge is approaching.

Change-Id: Ic1daa6197f5309a8b5b326a321f2b8a30e58bf27
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
(cherry picked from commit 9d70506b4c72f4c58c37b4a06c46381a02b333db)
power: qpnp-charger: remove revision check for temperature reads

The revision register is not sufficient enough to distinguish
whether the ADC is available or not.

Fix this by removing this check to allow the ADC driver to
handle whether the temperature reading is available or not.

CRs-Fixed: 500673
Change-Id: I4ec2fbfb3ef5f70e8461ae4467492157f319540e
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
(cherry picked from commit 879becd27329e28acbfcaf05238d375506d9925c)
power: qpnp-charger: only monitor bat_therm when bat_if present

If the battery interface peripheral is disabled, JEITA
notifications are not required.

Fix this by checking if the bat_if base address is
initialized before requesting vadc notifications.

Change-Id: I8b2b3132771879fce07c64d55b6fe614e20ce790
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
(cherry picked from commit e858d1ec5004561a30691e8bb6ed4e471c07b36c)