From patchwork Wed Feb 26 08:53:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Changqi Hu X-Patchwork-Id: 214244 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, MIME_BASE64_TEXT, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, UNPARSEABLE_RELAY, UNWANTED_LANGUAGE_BODY, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD719C4BA0B for ; Wed, 26 Feb 2020 08:54:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7512A20714 for ; Wed, 26 Feb 2020 08:54:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b="c40go33f" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726132AbgBZIyE (ORCPT ); Wed, 26 Feb 2020 03:54:04 -0500 Received: from mailgw02.mediatek.com ([210.61.82.184]:3943 "EHLO mailgw02.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1725872AbgBZIyD (ORCPT ); Wed, 26 Feb 2020 03:54:03 -0500 X-UUID: aa1ea91aa72b4f579898336d1bfe5998-20200226 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=6C9TgGDdkO5F5jXOiRjOlhQAyTOnDDs9QSKwWISWmG8=; b=c40go33fgEGK6WYjvd7AFM6CSmdG2pv7RFZOoKENJ9pd/G+RjsWm2XsWNdZuF0eX39KQ4t2mweXoD61fci2yYY7BgCgKMW6uYpEVKw4IvPlkKR+GMZWI/xzAVrGhkSJ2co/aTzKuuN0gKQCKiFTMB+NJt1HsQrZYXx69QyvNj8o=; X-UUID: aa1ea91aa72b4f579898336d1bfe5998-20200226 Received: from mtkexhb01.mediatek.inc [(172.21.101.102)] by mailgw02.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.10 Build 0809 with TLS) with ESMTP id 251042541; Wed, 26 Feb 2020 16:53:59 +0800 Received: from mtkcas09.mediatek.inc (172.21.101.178) by mtkmbs07n2.mediatek.inc (172.21.101.141) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Wed, 26 Feb 2020 16:53:08 +0800 Received: from localhost.localdomain (10.17.3.153) by mtkcas09.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1395.4 via Frontend Transport; Wed, 26 Feb 2020 16:53:45 +0800 From: Changqi Hu To: Greg Kroah-Hartman , Jiri Slaby , Matthias Brugger CC: Vinod Koul , Nicolas Boichat , Frank Wunderlich , Claire Chang , , , , , , Yingjoe Chen , Eddie Huang , Changqi Hu Subject: [PATCH v4] serial: 8250-mtk: modify mtk uart power and clock management Date: Wed, 26 Feb 2020 16:53:45 +0800 Message-ID: <1582707225-26815-1-git-send-email-changqi.hu@mediatek.com> X-Mailer: git-send-email 1.8.1.1.dirty MIME-Version: 1.0 X-MTK: N Sender: linux-serial-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org MTK uart design no need to control uart clock, so we just control bus clock in runtime function. Add uart clock used count to avoid repeatedly switching the clock. Signed-off-by: Changqi Hu --- Changes in v4: Modify commit-message Changes in v3: Merge patch v1 and v2 together. Changes in v2: Enable uart bus clock when probe and resume base on v1 patch, but miss v1 patch itself. drivers/tty/serial/8250/8250_mtk.c | 50 ++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 18 deletions(-) -- 2.6.4 diff --git a/drivers/tty/serial/8250/8250_mtk.c b/drivers/tty/serial/8250/8250_mtk.c index 4d067f5..f839380 100644 --- a/drivers/tty/serial/8250/8250_mtk.c +++ b/drivers/tty/serial/8250/8250_mtk.c @@ -32,6 +32,7 @@ #define MTK_UART_RXTRI_AD 0x14 /* RX Trigger address */ #define MTK_UART_FRACDIV_L 0x15 /* Fractional divider LSB address */ #define MTK_UART_FRACDIV_M 0x16 /* Fractional divider MSB address */ +#define MTK_UART_DEBUG0 0x18 #define MTK_UART_IER_XOFFI 0x20 /* Enable XOFF character interrupt */ #define MTK_UART_IER_RTSI 0x40 /* Enable RTS Modem status interrupt */ #define MTK_UART_IER_CTSI 0x80 /* Enable CTS Modem status interrupt */ @@ -388,9 +389,18 @@ mtk8250_set_termios(struct uart_port *port, struct ktermios *termios, static int __maybe_unused mtk8250_runtime_suspend(struct device *dev) { struct mtk8250_data *data = dev_get_drvdata(dev); + struct uart_8250_port *up = serial8250_get_port(data->line); - clk_disable_unprepare(data->uart_clk); - clk_disable_unprepare(data->bus_clk); + /* wait until UART in idle status */ + while + (serial_in(up, MTK_UART_DEBUG0)); + + if (data->clk_count == 0U) { + dev_dbg(dev, "%s clock count is 0\n", __func__); + } else { + clk_disable_unprepare(data->bus_clk); + data->clk_count--; + } return 0; } @@ -400,16 +410,16 @@ static int __maybe_unused mtk8250_runtime_resume(struct device *dev) struct mtk8250_data *data = dev_get_drvdata(dev); int err; - err = clk_prepare_enable(data->uart_clk); - if (err) { - dev_warn(dev, "Can't enable clock\n"); - return err; - } - - err = clk_prepare_enable(data->bus_clk); - if (err) { - dev_warn(dev, "Can't enable bus clock\n"); - return err; + if (data->clk_count > 0U) { + dev_dbg(dev, "%s clock count is %d\n", __func__, + data->clk_count); + } else { + err = clk_prepare_enable(data->bus_clk); + if (err) { + dev_warn(dev, "Can't enable bus clock\n"); + return err; + } + data->clk_count++; } return 0; @@ -419,12 +429,14 @@ static void mtk8250_do_pm(struct uart_port *port, unsigned int state, unsigned int old) { if (!state) - pm_runtime_get_sync(port->dev); + if (!mtk8250_runtime_resume(port->dev)) + pm_runtime_get_sync(port->dev); serial8250_do_pm(port, state, old); if (state) - pm_runtime_put_sync_suspend(port->dev); + if (!pm_runtime_put_sync_suspend(port->dev)) + mtk8250_runtime_suspend(port->dev); } #ifdef CONFIG_SERIAL_8250_DMA @@ -501,6 +513,8 @@ static int mtk8250_probe(struct platform_device *pdev) if (!data) return -ENOMEM; + data->clk_count = 0; + if (pdev->dev.of_node) { err = mtk8250_probe_of(pdev, &uart.port, data); if (err) @@ -533,6 +547,7 @@ static int mtk8250_probe(struct platform_device *pdev) platform_set_drvdata(pdev, data); + pm_runtime_enable(&pdev->dev); err = mtk8250_runtime_resume(&pdev->dev); if (err) return err; @@ -541,9 +556,6 @@ static int mtk8250_probe(struct platform_device *pdev) if (data->line < 0) return data->line; - pm_runtime_set_active(&pdev->dev); - pm_runtime_enable(&pdev->dev); - data->rx_wakeup_irq = platform_get_irq_optional(pdev, 1); return 0; @@ -556,11 +568,13 @@ static int mtk8250_remove(struct platform_device *pdev) pm_runtime_get_sync(&pdev->dev); serial8250_unregister_port(data->line); - mtk8250_runtime_suspend(&pdev->dev); pm_runtime_disable(&pdev->dev); pm_runtime_put_noidle(&pdev->dev); + if (!pm_runtime_status_suspended(&pdev->dev)) + mtk8250_runtime_suspend(&pdev->dev); + return 0; }