From patchwork Fri Jun 17 10:16:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pramod Gurav X-Patchwork-Id: 70301 Delivered-To: patch@linaro.org Received: by 10.140.28.4 with SMTP id 4csp201434qgy; Fri, 17 Jun 2016 03:17:31 -0700 (PDT) X-Received: by 10.66.149.99 with SMTP id tz3mr1660790pab.128.1466158636489; Fri, 17 Jun 2016 03:17:16 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f15si11784240pfk.58.2016.06.17.03.17.16; Fri, 17 Jun 2016 03:17:16 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-serial-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-serial-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-serial-owner@vger.kernel.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933436AbcFQKRP (ORCPT + 2 others); Fri, 17 Jun 2016 06:17:15 -0400 Received: from mail-pf0-f175.google.com ([209.85.192.175]:34549 "EHLO mail-pf0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752094AbcFQKRM (ORCPT ); Fri, 17 Jun 2016 06:17:12 -0400 Received: by mail-pf0-f175.google.com with SMTP id h14so14468872pfe.1 for ; Fri, 17 Jun 2016 03:17:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=0a0YStnugUXYiS5HwxnENmdPb3RPUL7vSbcjPXAq0iE=; b=ky3fAR/xSddJkl36LlmO2AtSIuC6zX+Qxta18y8UYSis1mkG3lL/WBDFU6xuJbkoga 4h34/QGNZIYHJsbyt4LYRF9RdUlB+4P0MFWQooYQBU7jCe3PmWZEG7/ynS8jdJDJl2hD HSHo8yJTIQMM9+wabMcRnHkllbroFGsuGSsys= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=0a0YStnugUXYiS5HwxnENmdPb3RPUL7vSbcjPXAq0iE=; b=YvnAiv6pKR+UD1d888XijBYU7AE/TJZdY2BQ0oK+OSoHoOZMiINk+aNcpEfvmTyV7r aVUTiWIoIsJlTohDtRIdshn35ExH+2HVWF8FbhKJ2/cwY6wOUFpfKXM8BY5qA9gm3P41 mCnLyH3ZEZy75Z5pXMTCWjOAwIebhYTWtyO9pwmV0vznY9HF/Itxhoo6Xh+zuSmm5d3q 5ki4jMjByPalQxqyi1szHqt/w8aTFQ70w3fGMdBguDazn8XtEpvBZVjgznwIezzTrKf7 mT8mli8DOufSGu83s0O/BgAN7T1jSnF7KeHssEg9lLW4PE7soMriG5flG3wxZ3n/04Qo BLAg== X-Gm-Message-State: ALyK8tJeMJpMUJO+qJ6iiGz3oKQIIDyvVTLwZ/ZizJ3kZCLp42d5nRe2imIt4Blcj89967Gr X-Received: by 10.98.103.66 with SMTP id b63mr1604167pfc.141.1466158631511; Fri, 17 Jun 2016 03:17:11 -0700 (PDT) Received: from blr-ubuntu-56.ap.qualcomm.com ([202.46.23.61]) by smtp.gmail.com with ESMTPSA id ut6sm67189952pac.37.2016.06.17.03.17.08 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 17 Jun 2016 03:17:11 -0700 (PDT) From: Pramod Gurav To: linux-arm-msm@vger.kernel.org, linux-serial@vger.kernel.org Cc: gregkh@linuxfoundation.org, jslaby@suse.com, andy.gross@linaro.org, david.brown@linaro.org, ulf.hansson@linaro.org, linux-kernel@vger.kernel.org, Pramod Gurav Subject: [PATCH] tty: serial: msm: Add runtime PM and system sleep support Date: Fri, 17 Jun 2016 15:46:58 +0530 Message-Id: <1466158618-6702-1-git-send-email-pramod.gurav@linaro.org> X-Mailer: git-send-email 1.8.2.1 Sender: linux-serial-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org Add runtime pm and suspend/resume callback support to serial msm driver so that clock resources are managed runtime to save power. Signed-off-by: Pramod Gurav --- drivers/tty/serial/msm_serial.c | 183 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 168 insertions(+), 15 deletions(-) -- 1.8.2.1 -- To unsubscribe from this list: send the line "unsubscribe linux-serial" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/tty/serial/msm_serial.c b/drivers/tty/serial/msm_serial.c index b7d80bd..6b5776a 100644 --- a/drivers/tty/serial/msm_serial.c +++ b/drivers/tty/serial/msm_serial.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -234,8 +235,12 @@ static void msm_stop_tx(struct uart_port *port) { struct msm_port *msm_port = UART_TO_MSM(port); + if (pm_runtime_get_sync(port->dev) < 0) + return; msm_port->imr &= ~UART_IMR_TXLEV; msm_write(port, msm_port->imr, UART_IMR); + pm_runtime_mark_last_busy(port->dev); + pm_runtime_put_autosuspend(port->dev); } static void msm_start_tx(struct uart_port *port) @@ -247,8 +252,12 @@ static void msm_start_tx(struct uart_port *port) if (dma->count) return; + if (pm_runtime_get_sync(port->dev) < 0) + return; msm_port->imr |= UART_IMR_TXLEV; msm_write(port, msm_port->imr, UART_IMR); + pm_runtime_mark_last_busy(port->dev); + pm_runtime_put_autosuspend(port->dev); } static void msm_reset_dm_count(struct uart_port *port, int count) @@ -270,6 +279,8 @@ static void msm_complete_tx_dma(void *args) unsigned int count; u32 val; + if (pm_runtime_get_sync(port->dev) < 0) + return; spin_lock_irqsave(&port->lock, flags); /* Already stopped */ @@ -306,6 +317,8 @@ static void msm_complete_tx_dma(void *args) msm_handle_tx(port); done: spin_unlock_irqrestore(&port->lock, flags); + pm_runtime_mark_last_busy(port->dev); + pm_runtime_put_autosuspend(port->dev); } static int msm_handle_tx_dma(struct msm_port *msm_port, unsigned int count) @@ -378,6 +391,8 @@ static void msm_complete_rx_dma(void *args) unsigned long flags; u32 val; + if (pm_runtime_get_sync(port->dev) < 0) + return; spin_lock_irqsave(&port->lock, flags); /* Already stopped */ @@ -433,6 +448,8 @@ done: if (count) tty_flip_buffer_push(tport); + pm_runtime_mark_last_busy(port->dev); + pm_runtime_put_autosuspend(port->dev); } static void msm_start_rx_dma(struct msm_port *msm_port) @@ -507,19 +524,28 @@ static void msm_stop_rx(struct uart_port *port) struct msm_port *msm_port = UART_TO_MSM(port); struct msm_dma *dma = &msm_port->rx_dma; + if (pm_runtime_get_sync(port->dev) < 0) + return; msm_port->imr &= ~(UART_IMR_RXLEV | UART_IMR_RXSTALE); msm_write(port, msm_port->imr, UART_IMR); if (dma->chan) msm_stop_dma(port, dma); + pm_runtime_mark_last_busy(port->dev); + pm_runtime_put_autosuspend(port->dev); } static void msm_enable_ms(struct uart_port *port) { struct msm_port *msm_port = UART_TO_MSM(port); + if (pm_runtime_get_sync(port->dev) < 0) + return; + msm_port->imr |= UART_IMR_DELTA_CTS; msm_write(port, msm_port->imr, UART_IMR); + pm_runtime_mark_last_busy(port->dev); + pm_runtime_put_autosuspend(port->dev); } static void msm_handle_rx_dm(struct uart_port *port, unsigned int misr) @@ -766,6 +792,8 @@ static irqreturn_t msm_uart_irq(int irq, void *dev_id) unsigned int misr; u32 val; + if (pm_runtime_get_sync(port->dev) < 0) + return IRQ_NONE; spin_lock_irqsave(&port->lock, flags); misr = msm_read(port, UART_MISR); msm_write(port, 0, UART_IMR); /* disable interrupt */ @@ -799,13 +827,25 @@ static irqreturn_t msm_uart_irq(int irq, void *dev_id) msm_write(port, msm_port->imr, UART_IMR); /* restore interrupt */ spin_unlock_irqrestore(&port->lock, flags); + pm_runtime_mark_last_busy(port->dev); + pm_runtime_put_autosuspend(port->dev); return IRQ_HANDLED; } static unsigned int msm_tx_empty(struct uart_port *port) { - return (msm_read(port, UART_SR) & UART_SR_TX_EMPTY) ? TIOCSER_TEMT : 0; + int ret; + + ret = pm_runtime_get_sync(port->dev); + if (ret < 0) + return ret; + + ret = msm_read(port, UART_SR) & UART_SR_TX_EMPTY ? TIOCSER_TEMT : 0; + pm_runtime_mark_last_busy(port->dev); + pm_runtime_put_autosuspend(port->dev); + + return ret; } static unsigned int msm_get_mctrl(struct uart_port *port) @@ -834,6 +874,8 @@ static void msm_set_mctrl(struct uart_port *port, unsigned int mctrl) { unsigned int mr; + if (pm_runtime_get_sync(port->dev) < 0) + return; mr = msm_read(port, UART_MR1); if (!(mctrl & TIOCM_RTS)) { @@ -844,14 +886,20 @@ static void msm_set_mctrl(struct uart_port *port, unsigned int mctrl) mr |= UART_MR1_RX_RDY_CTL; msm_write(port, mr, UART_MR1); } + pm_runtime_mark_last_busy(port->dev); + pm_runtime_put_autosuspend(port->dev); } static void msm_break_ctl(struct uart_port *port, int break_ctl) { + if (pm_runtime_get_sync(port->dev) < 0) + return; if (break_ctl) msm_write(port, UART_CR_CMD_START_BREAK, UART_CR); else msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR); + pm_runtime_mark_last_busy(port->dev); + pm_runtime_put_autosuspend(port->dev); } struct msm_baud_map { @@ -992,15 +1040,6 @@ static int msm_set_baud_rate(struct uart_port *port, unsigned int baud, return baud; } -static void msm_init_clock(struct uart_port *port) -{ - struct msm_port *msm_port = UART_TO_MSM(port); - - clk_prepare_enable(msm_port->clk); - clk_prepare_enable(msm_port->pclk); - msm_serial_set_mnd_regs(port); -} - static int msm_startup(struct uart_port *port) { struct msm_port *msm_port = UART_TO_MSM(port); @@ -1015,7 +1054,22 @@ static int msm_startup(struct uart_port *port) if (unlikely(ret)) return ret; - msm_init_clock(port); + /* + * UART clk must be kept enabled to + * avoid losing received character + */ + ret = clk_prepare_enable(msm_port->clk); + if (ret) + return ret; + ret = clk_prepare(msm_port->pclk); + if (ret) + return ret; + + ret = pm_runtime_get_sync(port->dev); + if (ret < 0) + goto err; + + msm_serial_set_mnd_regs(port); if (likely(port->fifosize > 12)) rfr_level = port->fifosize - 12; @@ -1041,19 +1095,34 @@ static int msm_startup(struct uart_port *port) msm_request_rx_dma(msm_port, msm_port->uart.mapbase); } + pm_runtime_mark_last_busy(port->dev); + pm_runtime_put_autosuspend(port->dev); + return 0; + +err: + clk_unprepare(msm_port->pclk); + clk_disable_unprepare(msm_port->clk); + free_irq(port->irq, port); + return ret; } static void msm_shutdown(struct uart_port *port) { struct msm_port *msm_port = UART_TO_MSM(port); + if (pm_runtime_get_sync(port->dev) < 0) + return; + msm_port->imr = 0; msm_write(port, 0, UART_IMR); /* disable interrupts */ if (msm_port->is_uartdm) msm_release_dma(msm_port); + pm_runtime_mark_last_busy(port->dev); + pm_runtime_put_autosuspend(port->dev); + clk_unprepare(msm_port->pclk); clk_disable_unprepare(msm_port->clk); free_irq(port->irq, port); @@ -1067,6 +1136,8 @@ static void msm_set_termios(struct uart_port *port, struct ktermios *termios, unsigned long flags; unsigned int baud, mr; + if (pm_runtime_get_sync(port->dev) < 0) + return; spin_lock_irqsave(&port->lock, flags); if (dma->chan) /* Terminate if any */ @@ -1140,6 +1211,8 @@ static void msm_set_termios(struct uart_port *port, struct ktermios *termios, msm_start_rx_dma(msm_port); spin_unlock_irqrestore(&port->lock, flags); + pm_runtime_mark_last_busy(port->dev); + pm_runtime_put_autosuspend(port->dev); } static const char *msm_type(struct uart_port *port) @@ -1220,12 +1293,26 @@ static void msm_power(struct uart_port *port, unsigned int state, switch (state) { case 0: - clk_prepare_enable(msm_port->clk); - clk_prepare_enable(msm_port->pclk); + /* + * UART clk must be kept enabled to + * avoid losing received character + */ + if (clk_prepare_enable(msm_port->clk)) + return; + if (clk_prepare(msm_port->pclk)) { + clk_disable_unprepare(msm_port->clk); + return; + } + if (pm_runtime_get_sync(port->dev) < 0) { + clk_unprepare(msm_port->pclk); + clk_disable_unprepare(msm_port->clk); + return; + } break; case 3: + pm_runtime_put(port->dev); + clk_unprepare(msm_port->pclk); clk_disable_unprepare(msm_port->clk); - clk_disable_unprepare(msm_port->pclk); break; default: pr_err("msm_serial: Unknown PM state %d\n", state); @@ -1465,7 +1552,11 @@ static void msm_console_write(struct console *co, const char *s, port = msm_get_port_from_line(co->index); msm_port = UART_TO_MSM(port); + if (pm_runtime_get_sync(port->dev) < 0) + return; __msm_console_write(port, s, count, msm_port->is_uartdm); + pm_runtime_mark_last_busy(port->dev); + pm_runtime_put_autosuspend(port->dev); } static int __init msm_console_setup(struct console *co, char *options) @@ -1484,7 +1575,7 @@ static int __init msm_console_setup(struct console *co, char *options) if (unlikely(!port->membase)) return -ENXIO; - msm_init_clock(port); + msm_serial_set_mnd_regs(port); if (options) uart_parse_options(options, &baud, &parity, &bits, &flow); @@ -1627,6 +1718,12 @@ static int msm_serial_probe(struct platform_device *pdev) platform_set_drvdata(pdev, port); + pm_runtime_use_autosuspend(&pdev->dev); + pm_runtime_set_autosuspend_delay(&pdev->dev, 500); + pm_runtime_irq_safe(&pdev->dev); + pm_runtime_enable(&pdev->dev); + pm_runtime_set_suspended(&pdev->dev); + return uart_add_one_port(&msm_uart_driver, port); } @@ -1635,6 +1732,7 @@ static int msm_serial_remove(struct platform_device *pdev) struct uart_port *port = platform_get_drvdata(pdev); uart_remove_one_port(&msm_uart_driver, port); + pm_runtime_disable(&pdev->dev); return 0; } @@ -1645,12 +1743,67 @@ static const struct of_device_id msm_match_table[] = { {} }; +#ifdef CONFIG_PM +static int msm_serial_runtime_suspend(struct device *dev) +{ + struct uart_port *port = dev_get_drvdata(dev); + struct msm_port *msm_port = UART_TO_MSM(port); + + if (msm_port->is_uartdm) + clk_disable(msm_port->pclk); + + return 0; +} + +static int msm_serial_runtime_resume(struct device *dev) +{ + struct uart_port *port = dev_get_drvdata(dev); + struct msm_port *msm_port = UART_TO_MSM(port); + int ret; + + if (msm_port->is_uartdm) { + ret = clk_enable(msm_port->pclk); + if (ret) + return ret; + } + + return 0; +} +#endif + +#ifdef CONFIG_PM_SLEEP +static int msm_serial_suspend(struct device *dev) +{ + struct uart_port *port = dev_get_drvdata(dev); + + uart_suspend_port(&msm_uart_driver, port); + + return 0; +} + +static int msm_serial_resume(struct device *dev) +{ + struct uart_port *port = dev_get_drvdata(dev); + + uart_resume_port(&msm_uart_driver, port); + + return 0; +} +#endif + +static const struct dev_pm_ops msm_serial_pm_ops = { + SET_SYSTEM_SLEEP_PM_OPS(msm_serial_suspend, msm_serial_resume) + SET_RUNTIME_PM_OPS(msm_serial_runtime_suspend, + msm_serial_runtime_resume, NULL) +}; + static struct platform_driver msm_platform_driver = { .remove = msm_serial_remove, .probe = msm_serial_probe, .driver = { .name = "msm_serial", .of_match_table = msm_match_table, + .pm = &msm_serial_pm_ops, }, };