msm: kgsl: Fix context reference counting

Get rid of kgsl_find_context. Use instead kgsl_context_get that does
correct RCU read locking around the itr_find and increases the
reference count on the context before returning it.  This eliminates
the chance that a context will be destroyed while somebody is still
using it.  Of course increased use of kgsl_context_get is accompanied
by kgsl_context_put in all the right places.

Change-Id: Ic0dedbad73d497fd9b451aefad8e5b28d33b829d
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Use a read/lock for the context idr

Everybody loves a rcu but in this case we are dangerously mixing rcus and
atomic operations.  Add a read/write lock to explicitly protect the idr.
Also fix a few spots where the idr was used without protection.

Change-Id: Ic0dedbad517a9f89134cbcf7af29c8bf0f034708
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: convert cffdump stub functions to inlines

Since cffdump is not often enabled in Kconfig, it is helpful
to at least catch compiler errors and warnings when it is off.

Change-Id: Ic0dedbad41b0faac6e0a2514e694f8e369c61f7f
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: use the same logic in adreno_idle and adreno_isidle

These two functions need to agree on the meaning of "idle",
so that calling adreno_isidle() right after an adreno_idle()
will always return true.

Change-Id: I7cddf73773186c3ec8b56c111affacac3b07fcc7
Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>
msm: kgsl: use kgsl_mmu_device_setstate() whenever the gpu is idle

We really don't want new GPU commands or events to be generated
just to manage the iommu while we are idle.

Change-Id: I2e8740bee8c25c93bddc51a90a3370d151aaf558
Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>
msm: kgsl: embed kgsl_context struct in adreno_context struct

Having a separate allocated struct for the device specific context
makes ownership unclear, which could lead to reference counting
problems or invalid pointers. Also, duplicate members were
starting to appear in adreno_context because there wasn't a safe
way to reach the kgsl_context from some parts of the adreno code.
This can now be done via container_of().

This change alters the lifecycle of the context->id, which is
now freed when the context reference count hits zero rather
than in kgsl_context_detach().

It also changes the context creation and destruction sequence.
The device specific code must allocate a structure containing
a struct kgsl_context and passes a pointer it to kgsl_init_context()
before doing any device specific initialization. There is also a
separate drawctxt_detach() callback for doing device specific
cleanup. This is separate from freeing memory, which is done
by the drawctxt_destroy() callback.

Change-Id: I7d238476a3bfec98fd8dbc28971cf3187a81dac2
Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>
msm: kgsl: Take a reference count on the active adreno draw context

Take a reference count on the currently active draw context to keep
it from going away while we are maintaining a pointer to it in the
adreno device.

Change-Id: Ic0dedbade8c09ecacf822e9a3c5fbaf6e017ec0c
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: vidc: Handle FBD's with ARF flag

For VP8 some frames are used only for decoding and not for display.
FW sends FBD with DROP_FRAME flag set for this frames.These are queued
to driver by setting DROP_FRAME flag. Clients can decide how to handle
these FBD's.

Change-Id: I278637871e4fdeef8b4089b5e04596de88a543ad
Signed-off-by: Praneeth Paladugu <ppaladug@codeaurora.org>
msm: vidc: Add support to process EBD flags

FW returns UNSUPPORTED_INPUT flags in EBD message if
the bitstream is not supported by Venus FW. This needs
to be handled by driver properly.

Change-Id: Ib5048f6edb69511c2f4ab6cbf481e11302de00fb
Signed-off-by: Praneeth Paladugu <ppaladug@codeaurora.org>
msm_serial_hs: Add ioctl support for UART Clock on/off request

This change adds support for ioctl implementation in HSUART
driver. UART Client application (e.g. Bluetooth) can make
request for UART Clock on/off using the ioctl mechanism.
UART Clock status also can be queried using this mechanism

CRs-Fixed: 496245
Change-Id: I9531cf2f9eda487082b9719341c5ad84c15ad5f7
Signed-off-by: Saket Saurabh <ssaurabh@codeaurora.org>
msm_serial_hs: Vote for clocks during tty close

While BT operation is in progress, it's possible for
client functions to explicitly turn off the
UART clocks without notifying TTY layer.  Then when
TTY layer tries to access UART, L2 error happened because
of un-clocked registers. Since there is no clear way
to notify clients to turn on clocks before TTY access
UART driver will implicilty turn on the clocks

CRs-Fixed: 505225
Change-Id: Ia9958658465a6c0e5af66cd932e8e694cc12b78c
Signed-off-by: Naveen Kaje <nkaje@codeaurora.org>
msm:camera: Fix the crash observed in msm_post_event

When an error happens in wait_event_interruptible_timeout()
in msm_post_event(), a crash happens because msm_dequeue() is not executed.
Crash seems to be caused by -ERESTARTSYS, hence retry waiting on command
if rc is -ERESTARTSYS.

Change-Id: Ie44e03f63ce6262a65e2b9bb695eaa5a76d73579
Signed-off-by: Lakshmi Narayana Kalavala <lkalaval@codeaurora.org>
msm8974: mhl_sii8334: init chip on discovery

MHL driver initializes the chip in probe and enables
irq. These operations are postponed until they're
required. This prevents any interrupts from
being triggered even when there is no real
communication from hardware.

Change-Id: Ifa5c66cfe2fb732d2f8e34b7473ab9ae07937dc7
Signed-off-by: Manoj Rao <manojraj@codeaurora.org>
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>
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>
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>
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>
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>
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>
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>