USB: msm72k_udc: Check if the ep is not disabled before queuing it

Queuing a request on a disabled endpoint during composition switch leads
to prime failure. Hence return -EINVAL if a request is queued on a disabled
endpoint.

Also, in f_mtp, block queuing a request in OUT ep in receive_file_work, if
the device state is STATE_OFFLINE.

CRs-Fixed: 378207
Change-Id: Ic319c854a2149c5bc7b7f8ee8af3e921f609f018
Signed-off-by: Rajkumar Raghupathy <raghup@codeaurora.org>
Revert "Bluetooth: Fix RFCOMM session reference counting issue"

This reverts commit b1d63550199fdefec5a4a41cb0efd69c94a52477.

Change-Id: Ieca5ef0383b1177a5624e300dca2cf83ef14eb09
Signed-off-by: Srivalli Oguri <oguri@codeaurora.org>
ASoC: msm: qdsp6: Fixed the issue unmap command is sent incorrectly

When session CLOSE command is sent right before session RUN command
is acknowledged, callback function can mistakenly think that
the next received acknowledgement is for CLOSE command instead of
RUN command. This triggers driver to send memory unmap command to
the Q6 while it is still processing the CLOSE command. Eventually,
this leads to an invalid memory access and causes Q6 crash.

CRs-Fixed: 377281
Signed-off-by: Jay Wang <jaywang@codeaurora.org>
(cherry picked from commit 0668d106027ab1f2608171db970ecd1edf634d65)

Conflicts:

	sound/soc/msm/qdsp6/q6asm.c

Change-Id: Ieeadf5bc48060fd0462d23f5e0ca023dec43e09c
Signed-off-by: Srivalli Oguri <oguri@codeaurora.org>
WLAN: WCN1314: Fixed kernel panic during unloading WLAN driver

Declaring completion variables, card_rem_event_var and shutdown_event_var
as global variables, because we observed these completion variables,
getting freed while wait on these variables.

(cherry picked from commit b968a7c0a3489cb09845dd53507d3df794bbca6d)

Change-Id: Ifcae4001e6bbd02f7564d62f156ec32be5472604
CRs-Fixed: 393414
Signed-off-by: c_gowri <deepthi@codeaurora.org>
msm: gpio-v2: Dont re-enable the interrupt and clear the status

If a gpio interrupt is already enabled, do not re-enable it again
and do not clear the interrupt status.
Before this patch:
If an edge interrupt is marked as wakeup, the gpio sys suspend will
clear the status and enable it.
On Resume - the gpio sys resume would clear the intr_status of the wakeup
irq.
Thus, if this particular wakeup irq triggered and woke us up, we lose its
intr_status before calling the gpio summary handler.
Now, when the summary handler is finally called, the irq handler for the
wakeup interrupt is never called.

With this change:
1) Not clearing the intr_status on sys-resume, ensures that it is marked as
pending in the gpio layer.
2) Not re-enabling it again, makes sure that when the irq framework unmasks
all irqs on resume, it does not clear the intr_status either.

Change-Id: I7289522c8c9c97c6f4210433814013160678d130
CRs-Fixed: 400562
Signed-off-by: Rohit Vaswani <rvaswani@codeaurora.org>
msm: audio: qdsp6v2: Fix WMV stream resume issue

- Suspend phone while WMV clip playback is going on.
Resume it, clip fails to play.
- At resume of WMV clip, Pause is issued initially
to driver. But there is a bug in pause API which is
not returning appropriate result. OMX component when
starts playback, it now issues resume to driver and
driver is setting DSP to run state even before ENC_DEC
params are set.
- Fix pause code to return error value if driver is not
enabled. This will avoid driver in sending CMD_RUN
before ENC_DEC command.

(cherry picked from commit 92ad28cd56803b451ae4bde72bfd20021c769649)

Change-Id: If2814bab09d47fd4c99cc1e917be8f573bed0b50
CRs-Fixed: 394756
Signed-off-by: Laxminath Kasam <lkasam@codeaurora.org>
msm: bam_dmux: handle system wide low memory conditions

When A2 turns on, queue_rx() is first run from the SMSM thread, and
attempts to allocate 32 2kB-buffers with GFP_KERNEL for DMA transactions.
If the system is low on memory, the allocation could put the thread to
sleep while the memory allocator scavenges for enough memory to satisfy
the request.  This is not desired as it stalls a critical system thread
that is communicating with a real-time system.

Instead, use GFP_NOWAIT which will return immediately if no memory is
available for the allocation request so that bam_dmux may handle the
situation gracefully and possibly reschedule the allocations for a later
time.

CRs-Fixed: 406946
Change-Id: Ibff8c2d75c56b41c8d947a49e5a477fea7b31a36
Signed-off-by: Jeffrey Hugo <jhugo@codeaurora.org>
msm: camera: Change timeout values for msm_server_proc_ctrl.

Allow userspace to set timeout values for msm_server_proc_ctrl.

(cherry picked from commit 5bd0660a216dfdf934e4d91b519aa04cc186c9dc)

Change-Id: Ic513d2c3707f0ed81a571d7bf7430846e947092c
Signed-off-by: Deepak Kurien <dkurien@codeaurora.org>
Signed-off-by: Jignesh Mehta <jigneshm@codeaurora.org>
msm: camera: synchronize vfe start/stop operations.

In monkey runs due to race condition vfe_start gets triggered
before vfe_stop. Add synchoronization mechanism to avoid this.

CRs-fixed: 375444
Signed-off-by: Azam Sadiq Pasha Kapatrala Syed <akapatra@codeaurora.org>
(cherry picked from commit eeca82f2028f888f4895c6530879446cb3a2693f)

Change-Id: Ie7397e5ad8e1467cd47b822ca2c47e15ccbf03c8
Signed-off-by: Srivalli Oguri <oguri@codeaurora.org>
Bluetooth: Fix for supervision timeout must be 6X connection interval

Added a new function to verify whether the LE Connection Update
parameters are valid instaed of verifying all the LE Connection
parameters since only update parameters are set in the Connection
Update Request.

(cherry picked from commit 95319af0612d58788279748f586cc57221c3443e)

Change-Id: I48b74375efe3429cc463db10746a2ec368719cb5
CRs-Fixed: 387146
Signed-off-by: Archana Ramachandran <archanar@codeaurora.org>
msm-camera: Add Double Padding Support in Gemini

Add support to handle the padding
on both sides of the input buffers
to the Gemini driver to support
rotation in ZSL mode

Change-Id: I4bc0ef5a3d0f551021afbb93fd57acdcc83875b4
Signed-off-by: Ashwini Rao <ashwinik@codeaurora.org>
msm: bam_dmux: Extend locking around SPS transfer call

When a new receive packet is created in queue_rx(), it is added to the
receive packet queue and also queued with the SPS driver.  Once the
packet is filled, then BAM DMUX calls sps_get_iovec() to get the filled
receive buffer.  BAM DMUX then pulls the receive packet info off of the
RX queue and compares the DMA address from the sps_get_iovec() call to
the expected DMA address.  In some cases, they do not match resulting in
BAM DMUX processing the wrong RX buffer.

Going back to the queue_rx() processing, the packet is added to the
receive queue with a mutex locked, but then the mutex is unlocked before
the call to sps_transfer_one() which allows a race condition to exist if
multiple threads call queue_rx().

During normal operation, queue_rx() is only called by the
single-threaded receive workqueue, but during the call to
reconnect_to_bam(), a race condition is expected between the call to
rx_siwtch_to_interrupt_mode() and the final call to queue_rx() if a
packet comes in and is processed before the queue_rx() call is
completed.

This change extends the receive pool mutex locking around
sps_transfer_one() call which resolves the issue.

(cherry picked from commit 00cf869cf08158aae15e568d687603512f99dfd6)

Change-Id: I9bcc089f3ce484cff7298a254382f5f22371c702
CRs-Fixed: 377609
Signed-off-by: Eric Holmberg <eholmber@codeaurora.org>
Bluetooth: hidp: safely acquire hci connection

Claim device lock to safely enumerate hci connection list and bump
hci connection proxy device ref count simultaneously.

This patch incorporates David Herrmann's fix to prevent adding an
HID device when the hci connection no longer exists.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Gustavo F. Padovan <padovan@profusion.mobi>
Signed-off-by: Sumit Bajpai <sbajpai@codeaurora.org>
(cherry picked from commit e0a15cea91da20b047f30244f06de238008e0685)

Conflicts:

	net/bluetooth/hidp/core.c

Change-Id: I0c9de44f8f5f909d330f97f5a0a9776eacb46f8a
CRs-fixed: 390876
Signed-off-by: Srivalli Oguri <oguri@codeaurora.org>
Bluetooth: Rename hidp_find_connection()

hidp_get_connection() makes more sense because we hold a reference to the
connection inside this function.

Signed-off-by: Gustavo F. Padovan <padovan@profusion.mobi>
Signed-off-by: Sumit Bajpai <sbajpai@codeaurora.org>
(cherry picked from commit c7e4ebda4ab85605b3b2aba66411989d3dafdb7d)

Change-Id: I3442281bf2142f781d8a371914a632bda2b21947
CRs-fixed: 390876
Signed-off-by: Srivalli Oguri <oguri@codeaurora.org>
Bluetooth: Fix hidp_get_connection()

This functions needs crtl_sock and intr_sock to be set first.

Signed-off-by: Gustavo F. Padovan <padovan@profusion.mobi>
Signed-off-by: Sumit Bajpai <sbajpai@codeaurora.org>
(cherry picked from commit be96be79a7fdd31825666ae645107da25a78bd12)

Change-Id: Ic2ecefe3eba8dfe80574755662909be3e136821a
CRs-fixed: 390876
Signed-off-by: Srivalli Oguri <oguri@codeaurora.org>
msm:vidc: Fix buffer pool index

While searching for the available buffer entry
in the buffer pool the search should begin from
index 1 as zeroth entry is reserved.

CRs-fixed: 366361
Signed-off-by: Mohan Kumar Gubbihalli Lachma Naik <mgubbi@codeaurora.org>
(cherry picked from commit 8c7e367a5ccd1c78c728657a14f08d6956b72e50)

Change-Id: I754fac98ce14f28cede1cb590c62784ad303a645
Signed-off-by: Srivalli Oguri <oguri@codeaurora.org>
msm_serial_hs_hwreg.h: Correcting the even parity and odd parity bits

Currently parity bits are defined to be used with UART_DM_MR2
register with PARITY_MODE Bit as below :
EVEN_PARTIY = 1 and ODD_PARITY = 2
With these values, UART functionality doesn't work after enabling parity.
Hence adding correct used PARITY_MODE bits value as
EVEN_PARITY = 2 and ODD_PARITY = 1
after confirming with UART hardware programming guide.

(cherry picked from commit fd40e5f6313fbc74e8897927be1004faa5d9df08)

Change-Id: I90fa8384d434abd42dc46c917d39e8de1f540993
CRs-Fixed: 410377
Signed-off-by: Saket Saurabh <ssaurabh@codeaurora.org>
power: pm8921-charger: Squashed updates

msm: 8960: Make connector resistance value consistent

The BMS and charger platform data do not reflect
consistent connector resistance.

Fix this by using the rconn_mohm value from the
charger platform data which was the measured value, rather
than the BMS value which was picked for debug purposes.

Change-Id: I50c8a3bc672cc1d62e75d04707bb90342946b17c
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

mfd: pm8xxx: provide api to read restart reason

The BMS driver needs to know the restart reason of the pmic. Add
an api in the pm8xxx design to provide the restart reason.

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

msm: bms: fix data for palladium battery

The battery data is incorrect and is causing SOC jumps. The SOC is often
inaccurate and would jump from e.g. 40 to 35 in a few seconds due to a bad
battery profile.

Fix the values to use an accurate battery profile that reflects the
battery resistance and voltage versus percent values which help alleviate
the jumps.

Change-Id: I66a9d6eec497829512b001431dde757fed115651
Signed-off-by: Xiaozhe Shi <xiaozhes@codeaurora.org>

power: pm8921-charger: remove OV UV irq

Enabling the OverVoltage (OV) and UnderVoltage (UV) interrupts is
causing an interrupt storm. These interrupts are not used by the
charger driver.

Remove them from being registered and enabled.

CRs-fixed: 397078
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 8a2a5f509a80b5188cd7379ad66c085344c25f77)

Conflicts:

	drivers/power/pm8921-charger.c

Change-Id: I622d0d7fd35b223c9d7eaedd6f1931dc7cb41e95
Signed-off-by: Srivalli Oguri <oguri@codeaurora.org>

power: pm8921-charger: Fix liquid 8960 charging LEDs report

LiQUID orange charging LED should be immediately on/off
after external charger start/stop charging (i.e. DC insert/remove).
The green charging LED is on after End-Of-Charging detected.
The blue LED should be used for message notification
and not for dc-online report.

Change-Id: Id53829072ab213c3e594a539c57372bc9db3d340
Signed-off-by: Amir Samuelov <amirs@codeaurora.org>

power: pm8921-bms: fine tune the uuc scaling algorithm

For every soc calculations there are three unusable charge (uuc)
values.
uuc_uah_itest:
	The uuc value in micro amp hour (uah) based on the max current
	(itest). Typically this value is very high and changes as the
	battery resistance changes
uuc_uah_iavg:
	The uuc value based on the average current (iavg). Typically
	this is lower than uuc_itest
uuc_reported:
	The reported uuc based on state of charge (SOC) of the battery
	which chooses a stepsize value and approaches towards
	uuc_uah_itest or uuc_uah_iavg based on whether the device is
	being charged.

The expectation is that the reported uuc increase as the battery looses SOC
and decrease as SOC approaches full condition.

To help us coming up with a satisfactory algorithm the following
parameters are introduced.
uuc_multiplier:
	The multiplier used in stepsize calculation
uuc_iavg_div:
	The divisor to use in calculation of stepsize when there
	is a significant jump in average current from the
	previous attempt
uuc_min_step_size:
	The minimum stepsize to use if the calculated stepsize is very less

Also, in order to ignore the high bootup time load a timer is introduced
which makes the algorithm ignore iavg values upon bootup.

The algorithm also uses the previous uuc based on iavg in
"uuc_uah_iavg_prev". This aids in checking if there was a significant
increase in average current from the previous time uuc was calculated.
The algorithm keeps track of the previous reported uuc in last_uuc_uah.

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

power: pm8921-charger: change BOOT_DONE configuration

Set the BOOT_TIMER_EN bit when writing the
BOOT_DONE configuration. This is required to make
sure the boot timer will be enabled during the
next phone boot.

Change-Id: I0c43de3a0c285979f7bccd42e464ca0d2fbbe42a
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power: pm8921-charger: add UVD threshold API

PM8917 has a new register which allows configuring
the falling USB under voltage detection (UVD) threshold
from 3.85V to 4.35V in 100 mV steps.

Add an API that allows to set this value per default and
also only do this for PM8917 chips.

Change-Id: I9b4259b43f148762ac72af40bdc79b895d9b4fc2
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power: pm8921-charger: add reverse boost fix for dc_in

Reverse boosting is also seen on the DC charging path
and may cause charger removal detection problems.

Fix this by changing the reverse boost detection to only
check the active charging path. Only the active charging
path will see reverse boosting voltages because the other
path's OVP will automatically be opened (off) by the
hardware.

Change-Id: I23a48cd49d912e2bb2651159ec91d0ccfa7f5e0d
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power: pm8921-charger: add dc_unplug_check option

Add a configuration option to enable or disable
the reverse boost fix for the DC charging path.

Some board designs do not utilize the DC_IN path
on the PMIC, however they do use the presence
detection of DC_IN to enable the auto switching
and interrupt features. In these particular configurations
the PMIC does not control the DC OVP FETs.
Hence add an option to enable or disable the DC path workaround
and leave the workaround disabled by default.

Change-Id: I1e55bbfe37c7cc6f62581af11280a565ed648c06
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power: power_supply_core: add power_supply_set_supply_type API

Allow other drivers to change the supply type of a given power
supply. This is required when a USB driver determines the exact
USB connection type and needs to notify the owner of the usb power
supply struct.

Change-Id: I99d377628a296704e1dcd170251ab9ceadc653ec
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power: pm8921-bms: dont use override mode if batfet is open

If the battery fet is open, the current sourced or sinked from the
battery is very low. There is a high chance that an open circuit
voltage(OCV) happens. Putting the BMS in override mode resets the timer
whose expiry causes an OCV.

Avoid putting the BMS in override mode if battery fet is open.

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

power: pm8921-bms: remove reading rbatt from bms

Since battery resistance is found to be changing w.r.t. state of charge,
temperature and time since a load change happened, it is best if rbatt
is estimated using learnt tables.

Remove all the code that attempts to read battery resistance from BMS.

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

power: pm8921-bms: fix rounding down state of charge

The code is rounding down state of charge value in the final step of the
calculation. Instead use a division that returns the closest integer.

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

power: pm8xxx-ccadc: missing calibration at bootup

There is a bug in the sequence between when the calibration work is
scheduled and the global chip pointer is initialized. If work is
scheduled before the chip pointer is initialized it refuses to
calibrate.

Initialize the chip pointer before scheduling calibration work.

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

power: pm8921-bms: periodic hkadc calibration

Introduce periodic hkadc calibration again. A bug was introduced when
periodic ccadc calibration code was moved to the ccadc driver - hkadc
calibration got dropped.

Reintroduce periodic hkadc calibration.

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

power: pm8921-bms: use direct charger api instead of power supply api

The power supply api reports usb as missing if a usb wall charger
is plugged in - this is expected as a usb wall charger should be
treated as a dc charger - not a usb charger.

The bms needs to know if anything is connected to the usb port, it
does not care whether it is a usb wall charger or pc usb charger.

Use a direct api exposed by the charger that reports the USBIN_VALID
bit.

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

power: pm8921-bms: use shutdown_soc to force power on soc

Experiments have shown that shutdown_soc is very accurate. However
upon a restart, the battery may be unsettled, leading to a significantly
different power on soc.

Update the code to force the value of shutdown soc on the power on
soc. To do this simply update the power on OCV (open circuit voltage)
value.

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

power: pm8921-bms: remove Dynamic Unusable Charge algorithm

The dynamic UUC algorithm changes the UUC as load, state of charge and
temperature changes. This uncontrolled behaviour causes unacceptable
jumps in state of charge numbers.

Replace the dynamic UUC algorithm with a simple average current based
UUC. The average current is calculated by remembering the load for last
few (16) samples. Also to maintain a reasonable UUC while charging, a
load of 300mA is assumed.

Note that the first time UUC is calculated we don't have load samples
and in that case the instantaneous current is used.

Since we now don't change the UUC with respect to max possible load
(itest), the usage of this value is removed.  Also instead of failure
voltage we introduce cutoff voltage which represents the loaded voltage
by which the battery should be reported 0%.

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

power: pm8921-bms: increase soc slowly while charging

It could happen that the calculated soc was higher than the reported
soc even when a charger is not plugged in. This usually happens when
the battery temperature increases or the average current changes causing
unusable charge to go low. The current code never reports this high soc
unless a charger is plugged in.

However at the instant when charger is plugged in, the driver reports
that high calculated soc. This jump in soc in an instant is
unacceptable.

Slowly increase the reported soc so that it approaches the calculated
soc while charging. Basically we are playing catchup between reported
soc and calculated soc. The catch up time is based on how big the
difference between calculated soc and reported soc is - we allow
60 seconds per percent delta.

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

power: pm8921-charger: increase hearbeat if battery is low

When the battery percent charge goes low we want to increase the
frequency of state of charge updates. This helps the correction
algorithm ample opportunities to correct the state of charge and
prevent a low voltage shutdown.

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

power: pm8921-bms: adjust rbatt and ocv both for shutdown soc

When ocv (open circuit voltage of the battery) is adjusted to match a
specific soc, rbatt (the battery resistance) changes too. This is because
rbatt is dependent on the remaining charge which changes as the ocv
changes.

The issue we observed with this changing rbatt was the unusable charge
changes which causes the soc to move away from the soc the algorithm
attempts to match.

To get around this issue adjust both the rbatt and ocv when we want to
match a soc for a specific soc.

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

power: pm8921-bms: use background work to calculate soc

The soc (state of charge) calculation algorithms and the associated
adjusting algorithms were coded assuming that the soc will be queried
at distant intervals (more than 20 seconds).

However this assumption is bad and soc can be queried at sub second
intervals. These soc queries in quick succession causes the adjusting
algorithm to over correct the battery voltage.

Separate the soc calculation from the soc reporting - calculate soc
at specific intervals, while the reporting uses the values resulting
from the latest calculation. This way the adjusting algorithms run
at specified intervals and are not perturbed by the sub second querying
for soc.

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

power: pm8921-bms: iterate rbatt algorithm

When ocv (open circuit voltage of the battery) is adjusted to match a
specific soc, rbatt (the battery resistance) changes too. This is
because rbatt is dependent on the remaining charge which changes as
the ocv changes.

The issue we observed with this changing rbatt was the unusable charge
changes which causes the soc to move away from the soc the algorithm
attempts to match.

To get around this issue adjust both the rbatt and ocv in a iterative
fashion until rbatt value stabilizes.

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

power: pm8921-bms: dont adjust at high battery capacity

The adjusting algorithm was designed to prevent uvlo event towards
low state of charge situations. It works by decreasing ocv so that the
a lower soc is reported.

When the battery is fully charged, we fake an ocv event, causing the
ocv to jump to 100% and cc to 0. But after a few milliseconds when
charging stops, the adjusting algorithm reduces the ocv which
causes the SOC to jump from 100% to 99%.

This jump from 100 to 99 is incorrect. Moreover we have seen quick
decreases at high soc range because of the adjusting algorithm.
To fix that and the 100 to 99% jumps simply don't run the adjusting
algorithm when the battery soc is above 90%.

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

power: pm8921-bms: use shutdown soc if it is within limits

When a strong battery is removed, it was seen that the battery voltage
lines on the phone take about five to six seconds to go below 2.1volts
where the pmic resets all the battery backed registers.

If a new battery is plugged in within this time the driver will force
the shutdown soc on this battery which is incorrect.

Compare the shutdown soc with the calculated soc and if they are
different than a configurable limit, simply discard the shutdown soc
and use the calculated soc.

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

power: pm8921-bms: change power on ocv if soc < 0

When a phone boots up with a weak battery the ocv (open circuit voltage)
is so low that we end up in  a -ve soc.

If a charger is plugged in, it takes a while for that -ve soc to reach
0. Until then the reported soc is returned 0. (note the driver
never reports a -ve soc). This creates an impression that it takes very
long to go from 0% to 1%.

Change this by bumping up the pon ocv a bit such that soc is reported
exact 0.

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

power: pm8921-bms: provide option to ignore shutdown soc

Few platforms do not want to use the shutdown soc. Provide a option to
selectively disable it.

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

power: pm8921-bms: use rtc to get average current

The wall time does keep track of time when the device
is suspended. This causes the average current values to be high.

Use rtc clock instead of the monotonic clock api. Since the rtc
clock has only one second resolution the driver rejects calculating
average current if it is invoked in less than 15 seconds. In such
cases it uses the previously calculated average current.

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

power: pm8921-bms: backup average current along with shutdown soc

When a phone shuts down because of 0% State of charge (soc) while it is
under heavy load, the unusable charge value is very high. The unusable
charge value is based on load.

When the device restarts, that load has significantly decreased
causing uuc value to reduce. This in turn increases the soc. In certain
cases the increase is so large that it falls beyond the shutdown soc
limit checks and a higher soc value is reported. That is unacceptable.

This problem can be addressed if we can remember the load used to
calculate uuc between restarts.  Use the lower nibble of register 0x105
to store the average current. Since we want finer resolution towards higher
current let the first step be 500mA and then each increment be 50mA steps.

Also since we are saving the range of load between restarts fill up all
the load samples in uuc calculation with this value. This emulates a
low pass filter on current changes and increases/decreases uuc
gradually.

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

power: pm8921-bms: limit voltage correction

The "adjust_soc" algorithm where we change the open circuit voltage
(ocv) in steps so that the state of charge (soc) starts approaching
the estimated soc, causes nonlinearity in the soc curves if an incorrect
resistance value is fed to the algorithm.

As battery ages and temperature changes, it is hard to estimate the
exact battery resistance.

So to fix the nonlinearity, limit the amount by which the ocv is
changed. Make it proportional to the current, i.e. change ocv by
small amounts in light load and let it change by proportionally
large amounts in heavy load situations.

Also, make the point where the soc is adjusted configurable via platform
data instead of forcing it to 25%.

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

power: pm8921-bms: increase/decrease UUC 1% at a time

We see huge jumps in Unusable Charge (UUC) as temperature, average
load or state of charge (SOC) changes, which results in huge jumps
in the reported capacity.

Increase/Decrease UUC slowly (limit 1%) each time SOC is requested.
This results in linearity improvements.

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

power: pm8921-bms: use termination battery resistance

Unusable Charge(UUC) is based on battery resistance and average load. Since
battery resistance is based on temperature and state of charge, UUC
is essentially based on temperature, state of charge and the load.

The purpose of UUC is to prevent the battery from falling below the
cutoff voltage. In the current implementation, since UUC is dependent
on SOC, it is small when SOC is high and gets bigger as SOC approaches
zero. This increase however is not linear which causes huge jumps in
SOC.

Remove the dependence of UUC on SOC. Simply use the resistance value
which would have been used when SOC = 0.

To find this resistance value, loop from SOC = 0 towards 100 and at each
step calculate/interpolate open circuit voltage, resistance and the
corresponding unusable voltage. The interpolated resistance value where
unusable voltage equals open circuit voltage is the termination
resistance. Use this resistance value for calculation of UUC. Note that
this resistance value is not dependent on the current SOC and will be
constant as long as the load and temperature remain the same.

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

power: pm8921-charger: round up vddmax adjustments

It was observed that the battery was charged at voltage less than the
max voltage. This was because the vdd max algorithm rounded down the
voltage.

Use voltage rounding up instead.

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

power: pm8921-bms: charging curve adjustments

It was observed that while charging the reported soc sometimes reaches
100% before end of charging happens. At other times it does not reach
100% at end of charge and a abrupt jump to 100% soc happens.

Fix this by linearly increasing soc based on battery charge current
after constant voltage phase is reached. Constant voltage phase is
reached when battery voltage reaches the max value.

Also once constant voltage phase is reached and the voltage or charge
current decreases keep reporting the earlier soc. This could be because
of a transient system load.

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

power: pm8921-bms: change OCV when scaling soc for charging

A drop in state of charge (soc) was observed right after End Of Charge
(EOC) happens. This was because calculated soc was still low, while the
scaling algorithm increased the SOC to 100%. There was a situation
where SOC was calculated right when charging finished but charger did not
send the EOC to bms - the charging adjustments algorithm does not scale
since it sees charging is stopped and falls back to the calculated soc
which was lower than the scaled soc.

Fix this by changing the calculated OCV(open circuit voltage) of the
battery so that the calculated soc is same as the scaled soc.

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

power: pm8921-bms: remove forcing shutdown soc

Forcing to return shutdown soc for the first 30 seconds was added so
that we show consistent soc right after bootup.

However with the recent changes to the algorithm viz. using termination
resistance for UUC, restricting UUC changes at most 1% and limiting the
OCV corrections, this forcing is not required. The soc stays within
limits of the shutdown soc.

Remove this forcing of shutdown soc.

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

power: pm8921-bms: fix 0% soc reported when started with a charger

When the device is started with a charger inserted, it reports 0% soc.
This is because a variable "soc_at_cv" is not initialized correctly in
the probe function.

Fix this by initializing it appropriately to -EINVAL.

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

power: pm8921-charger: reject vbus calls after checking vbat

Only reject VBUS requests after also checking the VBAT line
to ensure there is actually not other power source present.

Change-Id: Ie846e1d0450e62cddb69bd64a654214215fe2b2b
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power: pm8921-bms: fix fake battery emulation

With the recent split of functions to calculate and report state of
charge, the faking battery feature broke, it remained in the calculate
function.

Move the fake battery feature in the reporting function, where it really
belongs.

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

power: pm8921-charger: remove old workarounds

PMIC8921 revision 1.0 and 1.1 are no longer supported by software.

Remove the applicable workarounds from the driver.

Change-Id: I8c99fcbbf2f7229728236ae1059f4c9cb8dbb065
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power: pm8921-bms: use consistent temperature units

There is bug in the code where interpolate_pc is passed temperature in
DegC at one call site vs deciDegCelcius at other.

Use degC consistently.

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

power: pm8921-bms: report higher soc even when called within a second

The current code puts a restriction in the charger scaling algorithm
that state of charge (soc) should be called after a second. If it is
called earlier, simply report the previous soc.

There are user space applications that call soc every few milliseconds.
The soc appears stuck for such applications.

Remove that restriction.

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

power: pm8921-charger: PMIC version check for workarounds

Since the revision registers across different PMIC chips
may match up some workarounds may be applied to chips
that do not require them and cause issues.

Fix this by adding version checks to these workarounds
to make sure that the workarounds are applied to the correct
PMIC version.

Change-Id: I8af031570466a88656c89ee7764f78c5ec14d247
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power: pm8921-bms: use restart reason for usb detection

When the charger is not initialized it returns -EINVAL when asked
whether usb charger is plugged in. Use the restart reason to check
if we started with a charger insertion.

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

power: pm8921-bms: update power supply class if soc changes

There is a hearbeat loop that queries the state of charge
(soc) every few seconds. If the soc changes multiple times in
that period, we miss updating the userspace of those changes.

The userspace will notice a big step in soc, which is undesirable.
Update the userspace when the calculated SOC changes.

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

power: pm8921-charger: report DC online for some platforms

Some hardware platforms can supply VPH_PWR without battery
or charger being present.

To avoid shutting down due to no connected power sources
from the perspective of software set a flag that forces
the DC supply to be online for this type of platform.

Change-Id: I325ea00d2103da229bfb7a854ebb7f7fd556c16b
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

msm: 8960/8064/8930: add has_dc_supply property

All CDP platforms are powered by an external DC
supply. Hence set the has_dc_supply flag to
allow the charger driver to handle it accordingly.

Change-Id: Iacdbccbe164823d91ea72cce9d33a0ab5491a45f
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power: pm8921-charger: adjust vdd max earlier

The charger hardware decides Constant Current VS Constant Voltage based
on the VPH_PWR voltage and not on battery voltage (vbatt) as it should.

The problem is, becuase of IR drop between VPH_PWR and VBATT,  VPH_PWR
reaches the voltage programmed in vdd_max earlier while the VBATT stays
lower causing the charger hardware switch to constant voltage mode
prematurely.

To fix this issue adjust the vdd_max early.

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

power: pm8921-charger: workaround for active charger path

The active path bits in PBL_ACCESS1 register don't reflect the actual
status until some delay. This delay varies from few millisecond to
three seconds.

The unplug detection loop relies on these bits reflecting the correct
status for proper operation. Due to this bug the unplug worker wont
run when a charger is plugged in.

Update the code such that if it sees nothing set in the active path,
it still reschedules the unplug detection worker.

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

power: pm8921 and ltc4088-charger: add pm_power_set_property

Add pm_power_set_property_usb API to support the power
supply framework APIs which allow changing the type of
a given power supply.

This is required to avoid the use of PMIC specific APIs
in the USB driver and encourage the use of the power
supply framework.

Change-Id: Ifba2bd1248e8bb47a0700dec4d321d32db4f2066
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power_supply: add BMS for a new power supply type

Add POWER_SUPPLY_TYPE_BMS for the hardware chip or module which
monitors battery for state of charge and other properties.

BMS will register itself as a separate power supply of type BMS.
The charger will register the battery power supply and pull BMS data
from the BMS power supply if it exists.

Change-Id: I071f0d958a4b2b6af5f51460d201f6970aed595f
Signed-off-by: Xiaozhe Shi <xiaozhes@codeaurora.org>

power: pm8921-charger: Add power supply property

Support POWER_SUPPLY_PROP_CURRENT_MAX
power supply property so that
application can get the max current consumption number.

Change-Id: I0fc1e116c0f5a5648f99df967bdbb41fe5a07cbc
Signed-off-by: Binqiang Qiu <bqiu@codeaurora.org>

power: pm8921-charger: fix TTRKL_MAX_MASK

The PM8921_CHG_TTRKL_MAX_MASK should range from
bits 0 to 5.

Fix this by changing the mask to 0x3F.

Change-Id: I8e3e9068e7b9a3bfd24c07a00bb3953180ce0741
CRs-Fixed: 394246
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power: pm8921-charger: disable AICL when charging is disabled

If charging is disabled the VCHG line will see a drop in input
voltage regulation and the VIN_ACTIVE bit will be set.

To avoid incorrectly changing the USB input current limit disable the
adjustments made by the AICL algorithms if charging is disabled.

CRs-Fixed: 380478
Change-Id: I2bf31a23199f942a9ac843b9940a0f7377256238
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power: pm8921-charger: add PM8038 buck clock control address

The CHG_BUCK_CLOCK_CTRL register address changed in PM8038.

Add this address and make sure the correct buck clock frequency
is set in the hardware initialization.

Change-Id: I96b47c8603a291c41c5769fab9483d83fefb6084
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power: pm8921-charger: enable unplug_check for PC USB

For some platforms disabling the input voltage
regulation is not sufficent to prevent unplug events
from going unnoticed.

Fix this by making sure the unplug_check_worker
is also running when a PC USB connection with
a current limit of 500 mA is set.

Change-Id: I46b890ccd8da6c38056a4c0937ba954e904609b0
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power: pm8921-charger: Control CHG_USB_SUSPEND enabling boost

Toggle the CHG_USB_SUSPEND bit when utilizing the VBUS boost.
This bit controls ensures that no current is being drawn from
a USB device and the charger runs off the battery.

This makes sure that when pm8921_disable_source_current is called
the correct setting is written to the suspend bit depending
on the disable flag.

CRs-Fixed: 393498
Change-Id: I2acfae5aec19165a2196c50c31569c999cb090db
Signed-off-by: David Keitel <dkeitel@codeaurora.org>

power: pm8921-charger: enable DCIN_VALID_IRQ as wake irq

When the device is in suspend the DCIN_VALID_IRQ is not
used as a wake up interrupt.

Fix this by configuring the DCIN_VALID_IRQ as a wake up
interrupt.

CRs-Fixed: 389524
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
(cherry picked from commit 8d7b8f7264da504cc10b7d987a9ab6c62dafc9c0)

Conflicts:

	drivers/power/pm8921-charger.c

Change-Id: I56ad75068cadb658554f86ab2ddda9b008d1eb54
Signed-off-by: Srivalli Oguri <oguri@codeaurora.org>

power: pm8921-charger: Start unplug check worker for DC

It could happen that the device is powered up with a dc charger.
We should start the unplug check worker when the device starts with
dc charger or else we run the risk of not detecting the dc charger
removal.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 90e49c590fccdae53933f7a8fa62a7f0d65c4a0f)

Change-Id: Iaa9e6c56c5766addbf26f1a5052c6ee923ae1013
Signed-off-by: Srivalli Oguri <oguri@codeaurora.org>

power: pm8921-bms: reintroduce hkadc calibration during probe

The hkadc calibration process takes only one sample. This should be
a quick procedure to be done during probe. Without calibration the
voltage readings are skewed which affect the accuracy of BMS.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 067b0f024744f04b25766437fbd13718c71e76c7)

Change-Id: I1862c7df17f7aa9fbf339ddf9f47718b54e725f2
Signed-off-by: Srivalli Oguri <oguri@codeaurora.org>
fat: fix build warning

This fixes a compile warning (unititialized variable) in
the fat filesystem code.

Signed-off-by: Jonas Aberg <jonas.aberg@stericsson.com>
Signed-off-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
netfilter: xt_LOG: don't use xchg() for simple assignment

At least on ia64 the (bogus) use of xchg() here results in the compiler
warning about an unused expression result. As only an assignment is
intended here, convert it to such.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
Acked-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>