power: pm8921-bms: limit the number of tries for ocv calculation

When we want to force a certain percent charge we calculate the
expect percent charge (pc) from the expected RC (remaining charge
same as ocv based charge). We reverse lookup the ocv (open circuit
voltage) based on the pc. We double make sure that a forward lookup
matches the reverse lookup and if it does not within a percent limit
we repeatedly tweak the ocv by 5mV to get it to match.

This tweaking is unbounded and based on the battery profile could
result in a infinite loop. Limit the number of times we tweak the ocv
to prevent a possible watchdog bark.

CRs-Fixed: 406197
Change-Id: Iea874027a9e2f7ce326c8398f255e393fa7e4529
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
power: pm8921-charger: Change usb trim value for 1A on PM8917

Adjust a trim code for PM8917 for the 1A setting.

Change-Id: I07c346475fb4a294bc8eb8da8c2048cc23394e20
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
power: pm8921-charger: change ENERGY_FULL to CHARGE_FULL

Currently the reported ENERGY_FULL property reports
a value in nAh which does not comply with the power
supply class.

Fix this by changing the property to CHARGE_FULL and
changing the reported units to uAh.

Change-Id: Ib1161d3d8c4a9378eabf44f9de1eae9556d82643
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
power: pm8921-bms: fix charging accuracy

The current driver assumes 100mA as the charge termination current.
Use the value configured from the board file.

Also the driver uses the battery voltage at the ADC to determine CC
to CV point. Although it works, it is more appropriate to calculate
battery terminal voltage and base CC to CV point on it.

CRs-Fixed: 420670
Change-Id: Ib046fd7149d71fc62c8f2df7a9747d6b54e95138
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
power: pm8921-charger: speed up AICL

An issue is observed where it takes 3 to 4 seconds for the AICL
algorithm to settle on a certain USB current. This should be as
fast as possible.

Fix this by circumventing the delay of the unplug check worker if the
USB input current limit has increased.

Also remove the rounding of jiffies to the nearest second
when unplug check worker is called. This way we guarantee
that unplug check worker (and AICL) runs within the timing
requirements.

Change-Id: I546ff57d76e50ad86eb5853782fdcc1f348c9f0e
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
power: pm8921-charger: support temperature less configurations

There could be configurations where the voltage that appears on
the batt_therm line is out of specification and makes the battery
temperature comparators (btc) useless i.e. the  comparators could
trigger when the battery is not really cold or hot.

Add software support to monitor battery temperature. While charging
start a worker which polls the temperature every few (configurable) ms.
If the temperature is hot or cold override btc to force hot or cold
which in turn feeds in to the charging FSM and disables charging.
When the temperature comes back to normal override btc to force normal
temperature. Note that when the temperature goes out of range while
charging the end of charge worker still runs, we can run the btc worker
piggy backed on that wakelock.

Change-Id: I1293ecc2974bae4ad530ecb775cd19072e413c72
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
power: pm8xxx-ccadc: Disable the interrupt before registering

There is possibility of a race if the IRQ triggers immediately
after requesting. Fix this by disabling the IRQ before it
registers.

Change-Id: I8db5a202a2e63ca0a3e355a6083d426acb7342cb
Signed-off-by: Anirudh Ghayal <aghayal@codeaurora.org>
power: pm8921-charger: PM8917 USB trimming fix

PM8917 has slightly different trimming setup than
other PMIC platforms and requires a 7th bit.

Fix this by reading another scratch register to obtain
this value.

CRs-Fixed: 428106
Change-Id: I71482f9a4430a8bbcd02c954a415ff3fab2a0c8f
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
power: pm8921-charger: add pm_chg_write wrapper

A complex workaround needs to make changes in the charger
hardware block during every write to charger registers.

Fix this by adding a pm_chg_write wrapper function and
replacing all pm8xxx_writeb function calls with this
function.

Change-Id: I0c5656798444764b52f4d9d8889f831d674a1610
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
power: pm8921-bms: change invalid soc logging to debug

Currently, when unusable charge is calculated to be above full charge,
a warning is logged. Also, if SoC is calculated to be below 0, a error
is logged.

Change both of these to debug as to not confuse people debugging the
kernel when BMS is having accuracy issues.

Change-Id: I93b830d42e7d44435c790832c11d5d38861015e0
Signed-off-by: Xiaozhe Shi <xiaozhes@codeaurora.org>
power: pm8921-bms: Add error handling logic

Handle return code in reading vsense average so that
the parent function pm8921_bms_get_battery_current does not
report invalid battery current. While at it, also fix the
variable initialization.

CRs-Fixed: 389636
Change-Id: If24985f47cc9fcd6c2eb84a73165878559008120
Signed-off-by: Anirudh Ghayal <aghayal@codeaurora.org>
power: pm8921-charger: smbc uvlo lockup workaround

During UVLO events the charger hardware may lock up
and hence the charger poweron trigger will not work.

Add a workaround which ensures that
low power mode (LPM) is always enabled while the device
is running off the battery. This forces the
charger to run off the 32kHz clock which prevents
a lockup during a UVLO event.

However as a caveat charger register writes do not
work during LPM and hence during every write that happens
the software makes sure that LPM bit is disabled.
Note that this has no effect while a charger is connected
because the hardware automatically switches over to the
19.2 MHz clock which is needed for the registers writes.

While in LPM mode the bat_gone debouncer is bypassed while
a charger is not connected and re-enabled when a charger
is connected to avoid a debounce delay due to the clock
switching.

Note that all previous clock switching logic
including the battery alarm that was used to force on
the 19.2Mhz clock has been removed as part of this change.

Change-Id: I39b908451f4b678ed63b872ffc46590e2c728b2c
CRs-Fixed: 390327
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
power: pm8921-charger: remove the use of vbat_ov irq

It was believed that the vbat_ov irq triggers when the battery voltage
goes above vdd_max. That interpretation is wrong, the vbat_ov irq
triggers when the battery goes above vbatdet threshold.

Remove all the useage and requesting of this interrupt.

Change-Id: If7e5622067c77c4c8d64ee466d1c62e4dcb0e53f
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
power: pm8921-charger: charging time expiry fixes

When the charger timer expires charger h/w raises an interrupt and sets
the charger failed bit. Once the charger failed bit is set to 0 by s/w
the charger hardware resumes charging.

The current charging driver resets the charge failed bit periodically.
This is not safe. Update the code to reset failed bit only when the
charger failed interrupt happens and only if a configurable parameter
is enabled.

CRs-Fixed: 428877
Change-Id: If58279329f6c76978223369a2d08d47a7b1e3579
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
power: pm8921-bms: recalculate soc immediately upon wakeup

Under certain conditions, sleep current may be very high, and the BMS
may have counted a lot of current. However, SoC is only recalculated
once every 20 seconds of kernel time. If SoC has been recalculated just
before sleep, there may be almost 20 seconds before the SoC is updated.
The user will see SoC make a big jump a few seconds after waking up.

Fix this by recalculating SoC immediately after resume in the BMS so that
SoC is immediately refreshed upon wakeup.

Change-Id: I358909c38b0771fe0283386841b6123ddb2931a6
Signed-off-by: Xiaozhe Shi <xiaozhes@codeaurora.org>
power: pm8921-bms: account for sleep time in ADC calibration

Currently, the system calibrates the CCADC and HKADC every 10 minutes of
kernel time. However, this does not count sleep time, which could make
calibration much more infrequent.

Fix this by saving the time of the last calibration for both ADCs and
immediately calibrate upon resume if the elapsed time is longer than the
calibration period.

Change-Id: I34f51c6202cbaa805c496360e8dbd391890decc6
Signed-off-by: Xiaozhe Shi <xiaozhes@codeaurora.org>
power: pm8921-charger: set USB OVP time to maximum for PM8917

The USB OVP debounce time play a significant role when a
watchdog reset occurs on PM8917. If the default value of 4
ms is set this can lead the SMBC to mistakenly enter restart_pon
and reset all regulators.

Fix this by increasing the USB OVP debounce time to the maximum
setting of 15ms.

CRs-Fixed: 434656
Change-Id: Iaab82ea254444bba233cd9ebad93d38426154ca4
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
power: pm8921-charger: Cancel/schedule work across suspend/resume

There is a possibility that the work can get scheduled after the
driver suspend. Avoid this by canceling the work in suspend and
re-scheduling it on resume.

CRs-Fixed: 433758
Change-Id: Ic282fff7167a8224ec1a1e0f7c247a9c8d73f390
Signed-off-by: Anirudh Ghayal <aghayal@codeaurora.org>
power: pm8921-bms: remember the temperature when ocv was taken

The code looks up the percent charge based on ocv at the
current temperature. The ocv could have been recorded earlier
under different temperature conditions. Looking it up at the
current temperature is wrong.

Hence remember the temperature when the ocv was first seen. And
use that to lookup the ocv values.

Change-Id: Icdc707a9438adae5c8bb2d9b97af95e65c4bc2a1
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
power: pm8921-bms: hold a wakelock while calculating capacity

It was observed that the system goes to suspend when the SOC calculation
is half way through. Prevent it from going to suspend when SOC
calculation is in progress.

Change-Id: I26c4e1fb971f22ec721e1dddcce6b301f5db6608
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>