msm_serial_hs: Don't configure gpios into UART Mode always

UARTDM device is used as interface to connect external devices through
GPIOs. Currently those GPIOs are configured in active mode once driver's
probe() is called and configured in suspend mode once its remove() is
called. Probe() is called once only and remove is not called until UARTDM
platform device is removed. With this current implementation, although
external device (ex. Bluetooth SoC) is not operational mode, used UARTDM
GPIOs are configured in active mode which is causing more leakage current
and more power consumption.

Hence configure those UARTDM GPIOs in active mode only when used UARTDM
device is opened and under used. Configure UARTDM GPIOs back to suspend
mode when UARTDM device is closed and under not used.

Change-Id: I1a7be076fcd3b72034e9bc06ff89922cbd32a7f3
CRs-Fixed: 422217
Signed-off-by: Mayank Rana <mrana@codeaurora.org>
Signed-off-by: Gopal Goberu <ggober@codeaurora.org>
msm : Add support to get user-defined TTY device for uart port

Currently UART port is enumerating with platform device ID. It is not
necessary that TTY<ID> is same as available UART device ID. Provide
optional support which allows to override used platform device ID with
uart port by userid to get TTY based Uart device as /dev/ttyMSM<userid>
instead of /dev/ttyMSM<ID>.This support helps third party application/test
framework to use the same TTY uart device name across different platform
without changing in those applications as it is not necessary that on
different platform used UART device would be having same TTY<ID> name.
The userid value should be from 0 to 256

Signed-off-by: Saket Saurabh <ssaurabh@codeaurora.org>
(cherry picked from commit eb4ad84b4588aa4a80989f89bb46b3c1874335c1)

Change-Id: If89dcc087372e2e406debbdf12ff19a45e6db33f
Signed-off-by: Gopal Goberu <ggober@codeaurora.org>
msm_serial_hs: consider flush buffer request only when DMA is in progress

TTY Core uses flush_buffer to clear out any output data before closing
port, whereas UART application can always request to do flush output data
before starting data transfer using tty supported ioctl().
receive_tty_flush is used to make decision on completion of any DMA Tx
data transfer. Using same approach when there is no DMA Tx transfer is
happening, it is breaking next DMA Tx transfer functionality. Hence
update receive_tty_flush only when there is any DMA TX transfer active.

CRs-Fixed: 438057
Signed-off-by: Mayank Rana <mrana@codeaurora.org>
(cherry picked from commit 53069734628ebce341cf1ef778c91dbcbbcf67db)

Change-Id: I8e5cc99588f4e5dd148ef1fd58b5c2867078fbda
Signed-off-by: Akhila Musunuri <makhila@codeaurora.org>

Conflicts:

	drivers/tty/serial/msm_serial_hs.c
msm_serial: Don't configure UART IRQ as wakeup interrupt

Currently UART IRQ is being configured as wakeup interrupt once
UART port is being opened and not unconfigured on closing the port.
It is not required to configure UART IRQ as wakeup interrupt as
wakeup functionality is only supported through UART RX GPIO IRQ line
if it is required and enabled for the use case. Enabling UART IRQ as
wakeup interrupt consumes more power on TCXO state.

CRs-Fixed: 452191
Change-Id: I25f86316fdbd356547e0b6921bde23aff3a56355
Signed-off-by: Saket Saurabh <ssaurabh@codeaurora.org>
msm_serial_hs: Wait for discard flush completion for UART Rx channel

Completion of requested flush command with ADM driver is having more
latency then previously. Hence now it is required to wait for discard
flush complete if there is more events expected with UART driver which
would go out of sync without it. Below are 2 instances where it is must
to wait for discard flush completion requested on UART Rx channel.

1. Changing Baud Rate of UART
UART application can send baud rate change request based on its requirement
of communication with connected device on remote uart. Serial core also
does set by default baud rate when application is opening the uart port. As
Rx command is queued always with ADM driver from UART driver, for above
events it is required to flush the same after setting the baud rate. Not
waiting for completion of Rx flush would allow application to send command
on Tx or any other ioctl which would reach to connected device but response
may not be received as Rx flush completion is not received to queue next Rx
command to ADM. Hence with this there are chances that received data with
UART wil be lost when Rx flush completion is not received in-time. Hence
wait for Rx flush completion from set_termios() with timeout as 300 jiffie.
Rx flush completion time is non-deterministic as it depends on number of
commands queued to ADM driver from ADM client drivers.

2. While going for UART clock off
UART clock off has multiple state machines and before going into last
state it makes sure that there are no pending data in UART Tx and Rx FIFO.
Although there would be one Rx command queued which is flushed before
moving to last state and going ahead with UART clock off. If UART clock is
turned off before Rx flush request is being executed and ADM tried to flush
Rx command, ADM encounters data bus error on UART Rx Channel. Hence with
waiting for Rx flush completion, and then doing UART clock off on receving
the same would resolve ADM data bus error issue on UART Rx channel.

CRs-Fixed: 457769
Change-Id: I9344277224a2dc3f28140a1f42aa3feed5411b97
Signed-off-by: Mayank Rana <mrana@codeaurora.org>

Conflicts:

	drivers/tty/serial/msm_serial_hs.c
msm_serial_hs: Fix Tx path issues during uart shutdown.

UART queues tx transfer request to ADM and upon getting completion
callback from ADM, schedules tx tasklet in which it enables the
Tx Ready Interrupt and waits for it. On Tx ready interrupt tx
dma_in_flight is set to zero indicating no more tx data is pending.

This would cause corner case where if uart shutdown function checks
dma_in_flight flag between dmov callback and Tx Ready interrupt,
then this may cause uart shutdown function queuing dmov flush for
the completed Tx command and with this dmov flush callback never
be called. To fix this issue, move the dma_in_flight flag reset from
Tx Ready interrupt to dmov callback.

To avoid issues if any uart clients submitting any Tx requests after
uart shutdown, add is_shutdown flag and check before tx command
submission.

CRs-Fixed: 476745
Change-Id: I3304833857caa7cc7a7f1eb82d57dd036bc8054d
Signed-off-by: Saket Saurabh <ssaurabh@codeaurora.org>
Signed-off-by: Prasad Sodagudi <psodagud@codeaurora.org>
(cherry picked from commit 3ee602f9b3d3415e59a771d9a77ffce02588f819)

Signed-off-by: Goberu Gopal <ggober@codeaurora.org>
msm_serial_hs: Fix UART Rx dmov command stall issues.

Resetting Rx and Tx HW state machine while Rx dmov command queued
with ADM, caused dmov stall issues. Hence moving the Rx and Tx reset
to after dmov_flush. In order to do this, change the Rx path to not
enqueue dmov request as a part of dmov flush when flush is
FLUSH_IGNORE.

Removing the unnecessary UART Rx and Tx HW Reset in
msm_hs_set_bps_locked().

Require to add few clock cycles after Rx and Tx HW reset, hence
adding 10us delay as safe side. In addition to this, combining
both Rx and Tx reset in single register write.

CRs-Fixed: 481244
Change-Id: Ibed1d9bb9a36e5a174fa20bc97207e2ceb2dcf01
Signed-off-by: Saket Saurabh <ssaurabh@codeaurora.org>
(cherry picked from commit cbdb6508bb021815a277aadf194b504cf2b77086)

Signed-off-by: Goberu Gopal <ggober@codeaurora.org>
msm_serial_hs: Fix synchronization between ADM and UART Cores.

With current UART Core programming sequence, it is seen that ADM
and UART Core are going out of sync in few usecases with regression.
It is fixed by using Force RxStale feature of UART Core from
set_termios() and sending discard flush to ADM which would terminate
currently queued UART RX CMD to ADM. Add termios_in_progress check
to make decision about what kind flush to send on receiving RxStale
interrupt. It makes set_termios() more reboust and also keeps ADM
and UART Core in-sync.

Add dma_in_flight to keep check of UART Rx CMD status and BUG_ON if
UART driver is trying to queue one more UART RX CMD to ADM as UART
driver re-uses same UART RX CMD.

This change also adds check of currently set uartclk rate and next
required uartclk rate for new baud rate. If both are same it would
not call clk_set_rate() to set same uartclk rate again.

UART baud rate change request (i.e. executing set_termios()) can come
asynchronously and time to program ADM hardware with queued CMD by ADM
driver is non-deterministic. It is required by UART driver to confirm
that queued RX CMD is programmed with ADM hardware before going with
requested baud rate change operation. Hence this change wait in
set_termios() for ADM driver to program queued RX CMD before going ahead
with baud rate. Otherwise ADM may encounter data bus error with UART RX
channel which is unrecoverable.

Change-Id: I678e91420b33a49ce8b6a5a29c6309f9ff6f5c4a
Signed-off-by: Mayank Rana <mrana@codeaurora.org>
(cherry picked from commit a5d3538c26f76fd6a3512b6b6c56090a0e7d2915)

Signed-off-by: Goberu Gopal <ggober@codeaurora.org>
msm_serial_hs: Donot register UART device as PM runtime active.

Currently UART device is being registered as PM runtime active
in probe() and set as suspended in shutdown(). With this UART
driver PM runtime suspend is being called when system suspend
is invoked and while resuming from the same which invokes UART
driver LPM functionality. It causes UART communication to stop
if both UART Tx and RX FIFO are empty.

This change donot register UART device as PM runtime active
device as UART driver doesn't use PM runtime APIs for its LPM
functionality.

Change-Id: I33d0c95e4227fdb1b7d6d514498e161e6e4df465
Signed-off-by: Mayank Rana <mrana@codeaurora.org>
(cherry picked from commit 09e0cf01f8559ddbde2bdca3595bd5bd64907e35)

Signed-off-by: Goberu Gopal <ggober@codeaurora.org>
msm_serial_hs: Disable UART RxStale before Force RxStale in set_termios

There is possible race for handling normal RxStale event and Force
Rxstale event with UART Core, when Force RxStale is issued from
set_termios(). Hence on baud rate change request disable RxStale event
mechanism, and invoke Force RxStale. With RxStale event mechanism
disabled, Force RxStale still generates interrupt.
Fix possible race condition if before completion of previous UART RX CMD
flush, set_termios() request is received. With this case it is quite
possible that rx_tlet() queues one UART RX CMD with ADM driver and also
set_termios() queues one more UART RX CMD which would results with
queueing same UART RX CMD twice with ADM driver. With this device would
crash as the ADM driver will not be able to de-reference the second UART
RX CMD work. Hence add check in set_termios with rx.flush value as
FLUSH_DATA_READY which confirms that flush is initiated but it is not
completed and wait for rx.flush to become FLUSH_NONE which would happen
only once flush is completed and new UART RX CMD is being queued with ADM
driver.
Reduce waiting time for discard flush completion as it is already waited
for queued UART Rx CMD to be programmed with ADM hardware. Hence change
wait time out from 300 jiffies to 50 mseconds.
Add an API to get important UART core registers values which can be called
from error condition. Use the same when discard flush timeout happens with
set_termios(), when rx.dma_in_flight is not set with set_termios() and when
multiple UART RX CMD queueing condition is seen in msm_hs_start_rx_locked()
API.

CRs-Fixed: 486260
Change-Id: If3e7ee1db8d884a8d8dfa994aa87b4aafe7723fa
Signed-off-by: Mayank Rana <mrana@codeaurora.org>
msm_serial_hs: Improve UART Shutdown path

For uart port close operation, serial core calls msm_hs_stop_rx_locked()
which sends discard flush request to ADM driver after changing rx.flush
state machine from FLUSH_NONE to FLUSH_STOP value. Next serial core
calls msm_hs_shutdown()and it waits for discard flush completion with
rx.flush state machine to be set as FLUSH_SHUTDOWN to complete uart
port close operation. With this approach, transfer termination is not
known by UART Core which may still allow UART core to make CRCI request
with ADM Core if new data is being received from remote uart. Hence
invoke Force RxStale from msm_hs_stop_rx_locked() which informs UART
core to terminate current Rx transfer and do not make any new CRCI
request with ADM core for any new incoming data. On receiving RxStale
interrupt, it sends discard flush request to ADM driver to terminate
transfer with ADM hardware.
This new programming sequence is required to get both UART and ADM
hardwares in-sync for termination of transfer.

CRs-Fixed: 475230
Change-Id: I802a456294d4fd2fa51a5f4e918a4f4f22bc5ee3
Signed-off-by: Mayank Rana <mrana@codeaurora.org>
msm_serial_hs: Fixing issue during UART shutdown

During UART port close in msm_hs_shutdown(), flag is_shutdown is
set as true and then UART interrupt is disabled and UART irq line
is freed. This is causing corner case in which even if uart shutdown
is in progress, UART interrupt can come as the UART interrupt is not
disabled and UART irq line not freed at this point. As flag
is_shutdown is set as true in msm_hs_shutdown() and then if UART
interrupt comes, it is hitting BUG_ON in UART Interrupt handler
msm_hs_isr(). To fix this issue, disable the UART irq and free
the UART irq line at the beginning of the msm_hs_shutdown() and
then set the flag is_shutdown.

In msm_hs_shutdown, disabling UART Interrupt and setting flag
is_shutdown is done with uart spinlock acquired. There is a
possibility that on other core, msm_hs_isr() might need to wait
for the same uart spinlock and later when msm_hs_isr() is executed
BUG_ON is hit. As by this time all UART Interrupt is disabled,
hence removing the BUG_ON and instead keep prints to notify the
same.

CRs-Fixed: 487945
Change-Id: I21fb032139fd59793169f97a90c4644bae562159
Signed-off-by: Saket Saurabh <ssaurabh@codeaurora.org>
base: genlock: Correct the lock file reference count.

genlock_get_fd_handle API creates a new FD and attaches
it to lock file and shares this FD to second process.
Now, two file descriptor are pointing to same file.
If one FD is closed, lock file will be closed. This
changes add a extra reference to lock file as new FD
has been attached. Taking this reference will make sure
that file doesn't get close. This refrence will go when
client will call close on this FD.

Change-Id: I3524bed5ededd89d43ca6d8b85df11c85c03c1e3
Signed-off-by: Deepak Kumar <dkumar@codeaurora.org>
msm-camera: increase wait for config status

wait for config status for 2 iterations before sending to upperlayers
in case of ERESTARTSYS error.

Change-Id: I938eee5316382a18524fbd7c7c3f1c6715d581dc
Signed-off-by: Katta Santhisindhu <kattas@codeaurora.org>
msm: camera: Fixing multiple stability issues:

    1. Add state for power up.
    2. Adding state check for sensor power up and down.
    3. Fix kernal crash due to NULL mctl_cmd.
    4. Fix for actuator crash.
    5. Add NULL pointer check for regulator.

Change-Id: I33d0ac14ff933872a94e32b25ecb50da56ff7b02
Signed-off-by: Krupal Divvela<kdivvela@codeaurora.org>
msm: camera: Fix NULL pointer dereference while CSIC config

Fix NULL pointer dereference from CSIC config.

CRs-Fixed: 416473

Change-Id: I7d76742de16eb714af357957b53fb8e0ca7d4bb3
Signed-off-by: Lokesh Kumar Aakulu <lkumar@codeaurora.org>
Signed-off-by: Raju P.L.S.S.S.N <rplsssn@codeaurora.org>
msm: camera: Remove duplicate video device list initialization.

When a video device is registered through video_register_device,
the fh_lock and fh_list variables are already initialized. So we
dont need to explicitly initialize them during v4l2 event queue
setup. Doing this overwrites the fh_list in some cases like
mctl pp node, where each instance can have its own eventHandle but
linked to the mctl pp video node. This can cause video device list
corruption in some usecases like HDR. Remove this extra
initialization for v4l2 nodes which register their video devices.
For native nodes using v4l2 event handles, add the initialization
before setting up the v4l2 event queue.
Move the function setup_v4l2_event_queue and
destroy_v4l2_event_queue into msm_cam_server.c for better code
organization.

CRs-fixed: 443659

Change-Id: Iad6a4108f1c970fdf5a64300b9a3194bc48b8a8b
Signed-off-by: Aditya Jonnalagadda <ajonnala@codeaurora.org>