msm_serial_hs: Squashed set of commits from CodeAurora

msm_serial_hs: Fix the issue with device unregister/register

UART device can be removed/added dynamically based on the
requirement. Currently removing UART device using
platform_unregister_device(), device is crashing with NULL pointer
dereference error for debugfs entry node. Also add probe function
as __devinit instead of __init section.

Change-Id: I058c9093d79d90eb32fb34835cb4ebb1ef4fa704
Signed-off-by: Mayank Rana <mrana@codeaurora.org>

msm_serial_hs: Release wakelock in case of failure case

msm_hs_init_clk() function is not releasing acquire wakelock
in case of uartclk enable is failing. Hence handle this case
and also correct the error handling in msm_hs_startup function
to release the acquire resources if any error condition encounter.

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

msm_serial_hs: Cleanup regarding usage of clock enable/disable apis

Currently driver uses clock apis (clock_enable()/clock_disable()) before
accessing any UART Core register. Hence remove all usage of clock apis
which is just increasing the refcount of uart clock instead of turning
on/off the uart lock.

Change-Id: Idf39108e40b23eb7f17d55470247b339a7850a5f
Signed-off-by: Mayank Rana <mrana@codeaurora.org>

msm_serial_hs: Use new clock framework apis

Clock framework provides clk_prepare/clk_unprepare (callable from
non-atomic context only) and clk_enable/clk_disable (callable from
atomic/non-atomic context) APIs. Start using these new APIs.

To use these new Clock APIs below changes are done in driver.
1. wakeup irq is converted into wakeup threaded irq.
2. New mutex lock (clk_mutex) is introduced to guard the clock
off/on request and making sure that there is no race condition
exist between these two asynchronous calls.
3. current UART Tx/Rx activities and clock status is checked in
msm_hs_isr() function by calling msm_hs_check_clock_off_locked()
function. Based on the return value of it, clock off timer is
started. Now same functionality is achieved by clock_off_work and
it is used instead of explicit function call.

With these changes, HSUART_DM driver imposes below restrictions on
usage of exported Clock On/Off APIs.
1. Client driver shouldn't call msm_hs_request_clk_on() API from
atomic context. It must be called from non-atomic context.
2. Client driver can call msm_hs_requeset_clk_off() API from
both atomic / non-atomic context.

Change-Id: I75512f488bb53f4f9a08879a1d473706861f5cd4
Signed-off-by: Mayank Rana <mrana@codeaurora.org>

msm_serial_hs: Fix section mismatch

WARNING: drivers/tty/serial/built-in.o(.devinit.text+0x284):
Section mismatch in reference from the function msm_hs_probe() to
the function .init.text:msm_serial_debugfs_init()
The function __devinit msm_hs_probe() references
a function __init msm_serial_debugfs_init().
If msm_serial_debugfs_init is only used by msm_hs_probe then
annotate msm_serial_debugfs_init with a matching annotation.

Change-Id: I2d1aa030e598423cde3f3634ae6e4e35d74b29ed
Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>

msm: dma: Add graceful argument to msm_dmov_flush

The graceful argument allows clients to specify whether they want
to flush now, or wait until the end of the current scatter gather or
box mode descriptor. This also helps us unify msm_dmov_flush and
msm_dmov_stop_cmd.

Change-Id: Ifdfe5da804e79343782cc2999ad3624d2afbec4d
Signed-off-by: Jeff Ohlstein <johlstei@codeaurora.org>

msm: dma: unify flush and stop_cmd interfaces

Previously there were two interfaces for flushing a channel, the
difference between them being rather ill defined. Both would end up
with the same result if used correctly, and they only varied in error
conditions.

Change-Id: If8dcf1d1b7fc2e7417508b67ca3ceeffe77fa658
Signed-off-by: Jeff Ohlstein <johlstei@codeaurora.org>

msm_serial_hs: Enable clocks for register writes in probe

The msm_serial_hs driver performs register writes without enabling
either the core or iface clocks. As a result, these writes do not
go through. Fix this.

Change-Id: Ia1a215818fa09802d0ea6e4d06db0b4842dabd27
Signed-off-by: Matt Wagantall <mattw@codeaurora.org>

msm_serial_hs: Add flush state machine for UART Tx Transfer

Handle below error cases with UART Tx transfer:
1. Application doing I/O to UART port and is being terminated abnormally.
2. Used ADM Channel for UART TX transfer stalls.

In both above conditions, queued Tx command to ADM is not flushed. Hence
restarting of application shows list corruption for queueing next UART
Tx command to ADM. This change fixes it by having state machine for UART
Tx, identifying such condition and flushing the queue Tx command before
closing the UART device.

Change-Id: I4e567a539d890dffed50b5837dde58d5cd62d641
Signed-off-by: Mayank Rana <mrana@codeaurora.org>

msm_serial_hs: Don't destroy mutex from msm_hs_shutdown()

mutex is used uninitialized while unloading N_TTY line discipline
and installing SMUX Line discipline as msm_hs_shutdown() of HSUART
driver is called which is destroying this used mutex. Hence while
SMUX Line discipline is used, and clock off API is called, mutex
slowpath warning is seen. Destroying of mutex shouldn't be done from
msm_hs_shutdown() as initizalition and destroying of the mutex
is done from probe() and remove() of HSUART driver. Hence don't
destroy mutex from msm_hs_shutdown() to fix the issue.

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

msm_serial_hs: Call clk_set_rate() from non-atomic context

By default, HSUART Core CLK frequency is set to 7.37MHz. With this
CLK Frequency, it is not possible to get higher baud rate of 3.2 or
4 Mbps. Hence to set higher baud rate, driver calls clk_set_rate()
to set HSUART Core CLK frequency as ( required baud_rate * 16 ).
clk_set_rate() shouldn't be call from atomic context otherwise below
BUG is seen.

BUG: sleeping function called from invalid context at
mutex.c:85 in_atomic(): 1, irqs_disabled(): 128, pid: 1, name: init
[<c00151c4>] (unwind_backtrace+0x0/0x12c) from
		[<c07aebbc>] (mutex_lock+0x18/0x3c)
[<c07aebbc>] (mutex_lock+0x18/0x3c) from
		[<c0021258>] (clk_set_rate+0x2c/0x178)
[<c0021258>] (clk_set_rate+0x2c/0x178) from
		[<c0364a34>] (msm_hs_set_termios+0x820/0xb9c)
[<c0364a34>] (msm_hs_set_termios+0x820/0xb9c)
		from [<c0362170>] (uart_change_speed+0x90/0x94)
[<c0362170>] (uart_change_speed+0x90/0x94)
		from [<c03626c0>] (uart_startup+0xc8/0x1a0)
[<c03626c0>] (uart_startup+0xc8/0x1a0)
		from [<c0363534>] (uart_open+0xe4/0x13c)
[<c0363534>] (uart_open+0xe4/0x13c)
		from [<c0343d64>] (tty_open+0x360/0x4ec)
[<c0343d64>] (tty_open+0x360/0x4ec)
		from [<c013787c>] (chrdev_open+0x114/0x134)
[<c013787c>] (chrdev_open+0x114/0x134)
		from [<c0132844>] (__dentry_open+0x190/0x29c)
[<c0132844>] (__dentry_open+0x190/0x29c)
		from [<c0132a00>] (nameidata_to_filp+0x50/0x5c)
[<c0132a00>] (nameidata_to_filp+0x50/0x5c)
		from [<c0141b08>] (do_last+0x7ec/0x928)
[<c0141b08>] (do_last+0x7ec/0x928)
		from [<c0141d08>] (path_openat+0xc4/0x390)
[<c0141d08>] (path_openat+0xc4/0x390)
		from [<c01420b4>] (do_filp_open+0x30/0x7c)
[<c01420b4>] (do_filp_open+0x30/0x7c)
		from [<c0132518>] (do_sys_open+0xd8/0x174)
[<c0132518>] (do_sys_open+0xd8/0x174)
		from [<c000e380>] (ret_fast_syscall+0x0/0x30)

Hence use mutex to protect msm_set_termios() allowing clk_set_rate()
to be called from non-atomic context.

Change-Id: I92073aaf0c80a9a432c47d42286d9a130cf5172c
Signed-off-by: Mayank Rana <mrana@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/ttyHS<userid>
instead of /dev/ttyHS<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

CRs-Fixed: 386736
Change-Id: I50db8a40d4b7758adafc05240fe27af3a95fab28
Signed-off-by: Saket Saurabh <ssaurabh@codeaurora.org>

msm_serial_hs : handle uart_flush_buffer

Serial core allocates circular buffer for uart tx transfer.
Circular buffer head and tail is updated in serial core and
circular buffer tail is updated in uart driver.While uart
transfer is happening, uart client bluetooth hci_ldisc,calls
uart_flush_buffer in serial core and sets circular buffer
head and tail to zero.As uart driver does not know when
uart_flush_buffer is called in serial core by uart client,it
updates circular buffer tail for the previous ADM Tx completion.
This leads to queueing Tx command to ADM although no data is
there in circular buffer.Because of this,uart client is not
functional.

Hence adding msm_hs_flush_buffer api which notifies to uart driver
that uart_flush_buffer is called in serial core by uart client and
set tty_flush_receive flag to true.In uart interrupt handler ,do
not update circular buffer tail if uart_flush_buffer is already
called in serial core by uart client else update circular buffer
tail on ADM Tx completion.

CRs-Fixed: 419054
Change-Id: Ide13960bd5f9c4c374cadfab2a9860e928ef2e1c
Signed-off-by: Saket Saurabh <ssaurabh@codeaurora.org>

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.

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

Conflicts:
	drivers/tty/serial/msm_serial_hs.c
msm_serial_hs: Set the driver data in the probe correctly

 * Set the UART driver data correctly so we can access it later to
   control the clocks externally.

Change-Id: Ie0691c5a11cb4e98c2c3cb5cc2d052b7776ebcab