msm: smsm: Fix race condition in SMSM notification

The SMSM snapshot logic takes a snapshot of the current SMSM state,
places it into a FIFO, increments a wakelock count, and then signals a
worker to process the state changes.  The worker processes the state
changes and then decrements the wakelock count.

In the previous case, the code would do the following:

Interrupt handler:
 1) insert data into FIFO
 2) increment wakelock count

Worker:
 a) verifies >= 1 snapshots are in FIFO
 b) processes snapshot
 c) decrements wakelock reference count

If the interrupt handler is interrupted between steps 1 and 2, then the
function flow would be 1abc2.  In this case, step c would fail because
the wakelock count would be zero and the wakelock would remain at 1 and
locked even though the FIFO would be empty.  This results in the
wakelock not getting released.

To resolve this, the order of 2 (now 1') and 1 (now 2') was swapped
which always ensures 1' will always occur before abc which results in
the proper ordering 1'2'abc.

CRS-Fixed: 349099
Change-Id: I388b4c2f76838f59337e94f83b65fa06e81bb875
Signed-off-by: Eric Holmberg <eholmber@codeaurora.org>
(cherry picked from commit 96b55f6ec25d5812a0492fc39b6b44a6141795d2)
(cherry picked from commit 8c17b69afa7f2b7eb10df635f984b15d08c71da5)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
diag: Edit event mask responses

This change rectifies incorrect handling of event mask requests.
PC tools send in requests like x60, x82 for diag events traffic.
x60 requires all event states to be toggled and x82 command
requires present status in its reponse.

(cherry picked from commit a51f2592900dbc00b9cc66a668ef09b6a6eb3ccb)

Change-Id: I0368e13394b08fb9657f8dfef2594b4279a0595b
Signed-off-by: Shalabh Jain <shalabhj@codeaurora.org>
(cherry picked from commit 0a6f0dd4a9698e8740df6fe5bce97f4b62b901b1)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
video: msm: wfd: Delete from free_queue before freeing memory

VSG has a stale pointer to deallocated memory that is accessed, causing
a page fault.

Change-Id: I036293950795f601c0592c39b6da20d3c0cbb77f
CRs-Fixed: 348977
Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
(cherry picked from commit c73f497b1ce2d267c1b9b5b272f625b406ab36b2)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
msm: lpass-8960: Avoid write to lpass wakeup register.

LPASS SSR driver has a write to lpass wakeup register as part of
lpass shutdown process. But wakeup register falls under secure
space which is not accessible by HLOS. To avoid this, the write
has been moved to NMI call now. Hence remove this write to lpass
wake register from ssr driver

CRs-Fixed: 340872
Change-Id: I30f2eae105eeea2ebb543eb57b508c30e4b4b3d0
Signed-off-by: Sriranjan Srikantam <cssrika@codeaurora.org>
(cherry picked from commit 51a43afaaa39543a02f8234e973b462276e64043)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
ASoC: wcd9310: Add plug detection through mechanical switch

Use mechanical switch on the phone jack to detect headset/headphone
insertion and removal.  Mechanical switch is beneficial to avoid fake
button press and high impedance microphone headset detection.

CRs-fixed: 341402
Change-Id: I9eab7285617b9116671e9facb39a878a8ddb464e
Signed-off-by: Joonwoo Park <joonwoop@codeaurora.org>
(cherry picked from commit d7cf2e931cea51c7ff0a40e6b930edf99b320d8e)
(cherry picked from commit 803e1dd6602e789eb1e0da240dc734160f330d39)
ASoC: msm8960: properly protect external mclk control function

Both audio path and MBHC accessory detection logic need master clock
enabled along with bandgap and clock block of CODEC. Clock control
is managed by the machine driver. Calls to clock control function from
audio and MBHC detection paths can be nested. As a result, reference
counter of master clock is incremen/decrement out of order and cause
master clock not enabled when audio path is enabled. Without master clock
CODEC will not consume data and this leads to SLIMBUS overflow error.

Change-Id: Ic29b9740a28256251a8fb2aaf773082562a8fe40
Signed-off-by: Joonwoo Park <joonwoop@codeaurora.org>
(cherry picked from commit 28f49c84acb4e691b630b26c37ab6d206109482f)
(cherry picked from commit 62bceb0052e50013ed6d2e580c125a89493b289a)
cpufreq: Separate speed target revalidate time and initial set time

Allow speed drop after min_sample_time elapses from last time
the current speed was last re-validated as appropriate for
current load / input boost.

Allow speed bump after min_sample_time (or above_hispeed_delay)
elapses from the time the current speed was originally set.

Change-Id: Ic25687a7a53d25e6544c30c47d7ab6f27a47bee8
Signed-off-by: Todd Poynor <toddpoynor@google.com>
power: pm8921-bms: accumulate last_charge_increase

The current code always overwrites last_charger_increase when end
of charging happens. Update the code to add the current percent
increase.

Change-Id: I645554b65de627d26ab01399a6e25a9f5a014fe5
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
(cherry picked from commit 56a5059813b8d95757685cfc5ebec188564408b2)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
power: pm8921-bms: fix reporting  bms_end_cc_uah = 0

There is a bug in the driver where the code to fake full battery
causes coulumb counter based charge (bms_end_cc_uah) to be
reported 0mAh. The reason is once the battery is fully charged a 100% OCV
(open circuit voltage) is faked and columb counter value is offset
such that the coulumb reads start from zero. This faking is cleaned up
until a real OCV happens.

Fix the code such that bms_end_cc_uah is updated before the columb counter
is made to read 0.

Change-Id: I14ce494182ac899205fa41ad815a030d573ab793
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
(cherry picked from commit 29f82c8b66f0c70977c7bb101aff1dfecd6b33a3)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
power: bms: add desay calibration data

Add the calibration data and battery ID information for the
Desay 5200 mAh battery which is used in LiQUID platform
devices.

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

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
msm: 8960/8064: robust battery detection

To ensure proper battery id dectection on LiQUID and MTP
use machine_is_msm8960/8064_mtp/cdp to determine which
calibration data to use for battery. If platform is
unknown fall back on regular battery id detection.

Conflicts:

	arch/arm/mach-msm/board-8064-pmic.c
	arch/arm/mach-msm/board-8930-pmic.c
Change-Id: I2f5629572d1e5fcac0a2feb114947b91a96f3bf0
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
(cherry picked from commit 08e614cd89252e9bb83ffbc9bc5fd5db2c539170)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
power: bms: split battery data for different battery

Use different files for different battery profiles. This makes updating
the profiles easy as more data is collected.

Change-Id: Iafa369f4629f4e797ea55f6f81df08c54b57ad1d
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
Signed-off-by: David Keitel <dkeitel@codeaurora.org>
(cherry picked from commit b0353eb58bdab4d7ee67320be500313261f7b626)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
pm8xxx-adc: fix missing interrupts

There exists situations when an adc conversion is requested very
early in the resume sequence. These requests fail to complete
because of missing interrupts and eventually cause lockups.

When an interrupt is enabled during system resume the kernel rejects
enabling these interrupts. This keeps the interrupt disabled and causes
missed interrupt. Get around this problem by not needing to enable the
interrupt. IOW, do not call disable_irq and enable_irq, only request
the irq which leaves it in enabled state.

There is a bug in the pmic where if an interrupt is masked/unmasked
it clears the pending status. This causes a nonwakeup interrupt to
be missed as it is unmasked while resuming. Mostly this is okay but
if a adc request is made early in the suspend, it causes it to miss
interrupts. Make the adc interrupt a wakeup interrupt.

The driver initializes its completion only at probe time. This may cause
incorrect end of conversions with out of sync (spurious) interrupts.
Update the code to init a completion just before issuing a conversion.

Another issue was observed where the interrupt's unmask_irq was delayed,
causing another cpu to initiate the next conversion as soon as complete was
called. This lead to a situation where the interrupt for the next request
was triggered and the unmask happened later. Call complete within a
workqueue which guarantees that unmask would have completed and
it is safe to start the next conversion.

CRs-Fixed: 350768
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit f5285a2ed7918b941ac260e078c64c534a1d0e8d)

Change-Id: I09398655caed85a8a1b3af1cd5128b8dd2470c8c
Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
dm-crypt: fix possible mempool deadlock

This patch fixes a possible deadlock in mempool in dm-crypt.

Currently, dm-crypt reserves a mempool of MIN_BIO_PAGES reserved
pages. It allocates first MIN_BIO_PAGES with non-failing allocation
(the allocation cannot fail and waits until the mempool is refilled).
Further pages are allocated with different gfp flags that allow
failing.

Because allocations may be done in parallel, this code can deadlock.
Example:
There are two processes, each tries to allocate MIN_BIO_PAGES and
the processes run simultaneously.
It may end up in a situation where each process allocates
(MIN_BIO_PAGES / 2) pages. The mempool is exhausted. Each process
waits for more pages to be freed to the mempool, which never happens.

To avoid this deadlock scenario, this patch changes the code so that
only the first page is allocated with non-failing gfp mask. Allocation
of further pages may fail.

(cherry picked from commit 71b41c32bc4dc0be468435fc62e9285ee862e805)

Change-Id: I56b934a16f3056b494bdea732676debde47219c1
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Signed-off-by: Shashank Mittal <mittals@codeaurora.org>
(cherry picked from commit dca62667dfdee13709698769bb5af4a58ce5fc83)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
dm-crypt: add missing error handling

There were cases where error code would be used only if we finish
processing the last sector. If there were other encryption operations
in flight, error would be ignored and bio would be returned with
success as if no error happened.

This bug is present in kcryptd_crypt_write_convert,
kcryptd_crypt_read_convert and kcryptd_async_done.

This patch fixes it, setting io->error to -EIO always when an
error happens.

(cherry picked from commit 8bfc015229efd9f2e2eb18f03176d1a9d058e3a4)

Change-Id: I384100803d46630343e4777f807e062cf686dfd7
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Signed-off-by: Shashank Mittal <mittals@codeaurora.org>
(cherry picked from commit 7f7f17e8c5d0cd1aa7cc8f2b33e1d9afda1c9c45)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
dm-crypt: Rename pending

There are two structures that have "pending" field.
This patch renames it to "cc_pending" and "io_pending" to reduce confusion
and ease searching the code.

(cherry picked from commit 815bc97ac977a0cbccbd141f373a730374cd7882)

Change-Id: I831085b44d8910a14379e8140349bbd5747db4eb
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Signed-off-by: Shashank Mittal <mittals@codeaurora.org>
(cherry picked from commit 6a82453e13fc7f65cd706649cf37e1cb06dd1874)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
dm-crypt: move static cipher data out of per-cpu structure

Cipher information is static, so it does not have to be in a per-cpu
structure.

This is a preparation for further patches that remove per-cpu structure
altogether.

(cherry picked from commit fbd06f976a4f69bde837f7307c6ac62d0e8f8704)

Change-Id: Id717d197a894e5c05eb9d2b4c9f56982899ccdc2
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Signed-off-by: Shashank Mittal <mittals@codeaurora.org>
(cherry picked from commit d3c9a8845f3086b9adf4579180654f5a1b147ddb)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
dm-crypt: remove per-cpu structure

Remove per-cpu structure and make it per-convert_context instead.
This allows moving requests between different cpus.

CRs-Fixed: 339113

(cherry picked from commit 30c273a77c02ed9db52bd51027c812ebf513771e)

Change-Id: Icbd465144e17b51710901b1cca88577e7bcfc9c7
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Signed-off-by: Shashank Mittal <mittals@codeaurora.org>
(cherry picked from commit b463d1e1f24833d3b667f2f36f86e43adbaae88c)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
msm: camera: Fix msm_camera_i2c_poll function call

Change-Id: Ie99e61811332054b2b7e4b0f3a05a932e988729c
Signed-off-by: Rajakumar Govindaram <rajakuma@codeaurora.org>
CRs-Fixed: 348564
(cherry picked from commit e14cb2fd294c6dc2a3a825255bfafe2ce4fe4386)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
power: pm8921-charger: fix 10mv error on vdd_max

It was observed that the charging voltage is off by 10mV. The root
cause was the 10mV bit in the vdd_max register adds 10mV to the voltage
instead of reducing the voltage by 10mV.

The current driver thinks it subtracts 10mV. Fix this.

Change-Id: I37949113cb4c25642c873ce0cf39ac9fdeb276e1
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 08e7e0a792f32189bd0dadddc880065da9bdab44)
(cherry picked from commit ad83bbf9c99ae01e37ce37be76f8442629be65e7)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>