1 From ff3d8063eed907bed728a14e1519dc659036315a Mon Sep 17 00:00:00 2001
2 From: Fugang Duan <fugang.duan@nxp.com>
3 Date: Wed, 11 Sep 2019 16:36:48 +0800
4 Subject: [PATCH] MLK-17133-02 tty: serial: lpuart: add runtime pm support
6 Add runtime pm support to manage lpuart clock and its power domain
7 to save power in system idle and system suspend stages.
9 Signed-off-by: Fugang Duan <fugang.duan@nxp.com>
10 Reviewed-by: Robin Gong <yibin.gong@nxp.com>
12 drivers/tty/serial/fsl_lpuart.c | 80 ++++++++++++++++++++++++++++++++++++-----
13 1 file changed, 72 insertions(+), 8 deletions(-)
15 --- a/drivers/tty/serial/fsl_lpuart.c
16 +++ b/drivers/tty/serial/fsl_lpuart.c
19 /* Rx DMA timeout in ms, which is used to calculate Rx ring buffer size */
20 #define DMA_RX_TIMEOUT (10)
21 +#define UART_AUTOSUSPEND_TIMEOUT 3000
23 #define DRIVER_NAME "fsl-lpuart"
24 #define DEV_NAME "ttyLP"
25 @@ -846,6 +847,20 @@ static void lpuart32_start_tx(struct uar
30 +lpuart_uart_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
33 + case UART_PM_STATE_OFF:
34 + pm_runtime_mark_last_busy(port->dev);
35 + pm_runtime_put_autosuspend(port->dev);
38 + pm_runtime_get_sync(port->dev);
43 /* return TIOCSER_TEMT when transmitter is not busy */
44 static unsigned int lpuart_tx_empty(struct uart_port *port)
46 @@ -2259,6 +2274,7 @@ static const struct uart_ops lpuart_pops
47 .break_ctl = lpuart_break_ctl,
48 .startup = lpuart_startup,
49 .shutdown = lpuart_shutdown,
50 + .pm = lpuart_uart_pm,
51 .set_termios = lpuart_set_termios,
53 .request_port = lpuart_request_port,
54 @@ -2283,6 +2299,7 @@ static const struct uart_ops lpuart32_po
55 .break_ctl = lpuart32_break_ctl,
56 .startup = lpuart32_startup,
57 .shutdown = lpuart32_shutdown,
58 + .pm = lpuart_uart_pm,
59 .set_termios = lpuart32_set_termios,
61 .request_port = lpuart_request_port,
62 @@ -2742,6 +2759,11 @@ static int lpuart_probe(struct platform_
64 goto failed_irq_request;
66 + pm_runtime_use_autosuspend(&pdev->dev);
67 + pm_runtime_set_autosuspend_delay(&pdev->dev, UART_AUTOSUSPEND_TIMEOUT);
68 + pm_runtime_set_active(&pdev->dev);
69 + pm_runtime_enable(&pdev->dev);
71 ret = uart_add_one_port(&lpuart_reg, &sport->port);
73 goto failed_attach_port;
74 @@ -2776,6 +2798,9 @@ static int lpuart_probe(struct platform_
76 uart_remove_one_port(&lpuart_reg, &sport->port);
78 + pm_runtime_disable(&pdev->dev);
79 + pm_runtime_set_suspended(&pdev->dev);
80 + pm_runtime_dont_use_autosuspend(&pdev->dev);
82 lpuart_disable_clks(sport);
84 @@ -2802,15 +2827,41 @@ static int lpuart_remove(struct platform
85 if (sport->dma_rx_chan)
86 dma_release_channel(sport->dma_rx_chan);
88 + pm_runtime_disable(&pdev->dev);
89 + pm_runtime_set_suspended(&pdev->dev);
90 + pm_runtime_dont_use_autosuspend(&pdev->dev);
94 #ifdef CONFIG_PM_SLEEP
95 +static int lpuart_runtime_suspend(struct device *dev)
97 + struct platform_device *pdev = to_platform_device(dev);
98 + struct lpuart_port *sport = platform_get_drvdata(pdev);
100 + lpuart_disable_clks(sport);
105 +static int lpuart_runtime_resume(struct device *dev)
107 + struct platform_device *pdev = to_platform_device(dev);
108 + struct lpuart_port *sport = platform_get_drvdata(pdev);
110 + return lpuart_enable_clks(sport);
113 static int lpuart_suspend(struct device *dev)
115 struct lpuart_port *sport = dev_get_drvdata(dev);
120 + ret = clk_prepare_enable(sport->ipg_clk);
124 if (lpuart_is_32(sport)) {
125 /* disable Rx/Tx and interrupts */
126 @@ -2824,10 +2875,14 @@ static int lpuart_suspend(struct device
127 writeb(temp, sport->port.membase + UARTCR2);
130 + clk_disable_unprepare(sport->ipg_clk);
132 uart_suspend_port(&lpuart_reg, &sport->port);
134 /* uart_suspend_port() might set wakeup flag */
135 irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
136 + if (sport->port.suspended && !irq_wake)
139 if (sport->lpuart_dma_rx_use) {
141 @@ -2858,9 +2913,6 @@ static int lpuart_suspend(struct device
142 dmaengine_terminate_all(sport->dma_tx_chan);
145 - if (sport->port.suspended && !irq_wake)
146 - lpuart_disable_clks(sport);
151 @@ -2868,9 +2920,11 @@ static int lpuart_resume(struct device *
153 struct lpuart_port *sport = dev_get_drvdata(dev);
154 bool irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
157 - if (sport->port.suspended && !irq_wake)
158 - lpuart_enable_clks(sport);
159 + ret = clk_prepare_enable(sport->ipg_clk);
163 if (lpuart_is_32(sport))
164 lpuart32_setup_watermark_enable(sport);
165 @@ -2891,13 +2945,23 @@ static int lpuart_resume(struct device *
166 if (lpuart_is_32(sport))
167 lpuart32_configure(sport);
169 + clk_disable_unprepare(sport->ipg_clk);
171 uart_resume_port(&lpuart_reg, &sport->port);
176 +static const struct dev_pm_ops lpuart_pm_ops = {
177 + SET_RUNTIME_PM_OPS(lpuart_runtime_suspend,
178 + lpuart_runtime_resume, NULL)
179 + SET_SYSTEM_SLEEP_PM_OPS(lpuart_suspend, lpuart_resume)
181 +#define SERIAL_LPUART_PM_OPS (&lpuart_pm_ops)
183 -static SIMPLE_DEV_PM_OPS(lpuart_pm_ops, lpuart_suspend, lpuart_resume);
184 +#else /* !CONFIG_PM_SLEEP */
186 +#define SERIAL_LPUART_PM_OPS NULL
189 static struct platform_driver lpuart_driver = {
190 .probe = lpuart_probe,
191 @@ -2905,7 +2969,7 @@ static struct platform_driver lpuart_dri
193 .name = "fsl-lpuart",
194 .of_match_table = lpuart_dt_ids,
195 - .pm = &lpuart_pm_ops,
196 + .pm = SERIAL_LPUART_PM_OPS,