Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp80781pxu; Tue, 6 Oct 2020 00:55:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxI9ADJ6Seo316Qx2iZvdsIJdzIF74odl5RRXDIQVb48svHFEaMrJBur0703CYcvwS84HDt X-Received: by 2002:aa7:c2d9:: with SMTP id m25mr4106795edp.226.1601970957613; Tue, 06 Oct 2020 00:55:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601970957; cv=none; d=google.com; s=arc-20160816; b=Wt7+XaieJP6ePRPk56zi6i5MVajsVMb1t0Fo+knJU+GSkKdcj/j9t0orn2yxQos/YH y204OIBczT6km2LUiRWh44PNoFyK8Yi9mrearx7vHO7Ki+jH+Vm5mt7pmvW/tYDPAQlz vN0DYImZyqRalFZVh++OvNcBgdLsxXwhv7iF/+/GOrRnxlBzVcY9ipujP9pQ7fysF2Mh GqHaK5kP1MpUAHUcN7y2QFbwrcBICnViTSXdFvinITEiISbViQ84IeooI6Nl6icaQtcS ONaLeborSywK2zKUDtGzKcx/1wv7P9LqL+e7GjEnadVi5/x3SE+FcPp4AGuXxpVNJZb3 u0Zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=pwe2F6gUJFWdG9JMF5LSPTO+AiLU9oqcMF4ps8kWBqM=; b=xrarMLYtGsMZD1tcSSJpKj8cwZLn74C3N1IeFHrbY2Amce+LhYuMEivaaZJ0G7voCx uUQVcN20mObxR6Zp5+BM0xU3GAUzoQBQv6dH5DsfhY28Y0pLGQ+ZVThKyiJFM+uTLVaC awiE2GTbcA5VlcM4mAartuvV2AaLThIkqM3q8diwFRJtayD2ysjRiGmphahCjuA+VOMf TEstrCrkBiuUd1MlLWSNnzU0/wAU6Z7W38IxPjCTMrNTUU18wHZH8BgqzRP14onH40C9 rELgzikKOHG++vWCeZJZ0z7qyxUTyAJXmcDk9MMyK2LTydH42ugAoWXGfakZbUYmb2tM kZig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HSOc001O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qt7si1494575ejb.97.2020.10.06.00.55.35; Tue, 06 Oct 2020 00:55:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HSOc001O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727284AbgJFHya (ORCPT + 99 others); Tue, 6 Oct 2020 03:54:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727236AbgJFHyZ (ORCPT ); Tue, 6 Oct 2020 03:54:25 -0400 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B054C0613D3 for ; Tue, 6 Oct 2020 00:54:23 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id n14so8549198pff.6 for ; Tue, 06 Oct 2020 00:54:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pwe2F6gUJFWdG9JMF5LSPTO+AiLU9oqcMF4ps8kWBqM=; b=HSOc001Oe2i/fhLGKNTj8bqlOtrGfl3jWJ7rrl8V02OJCaDhyRSAtLtc8wYE6cqx6U Ebx9Rnj9IXqAqb1u1MLKzNVR4zM9sYBIm/kXvpen2l+z/zXrPosfCbWeH1Eg9LdfZvSO m+vb1NsMrfvwPM5oGtKxfI6eu6j+ZDm6uukfc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pwe2F6gUJFWdG9JMF5LSPTO+AiLU9oqcMF4ps8kWBqM=; b=o/uD/RhhYToqoR5dz9ZyFFtvAqn+kvEojQMzGwmgMSnOR4isJ48UU/XrTN7b7PwrxV 1CwQ1WYZvsh1brOAWtyhrzF7a4Wmz3G3+3Opl6IGw3EovMo3jyXePGeNHRzZIo4lqPh8 1zibAcegkVbnNptrSPew1WNW+dGf93cTXeL1rSqRKCBv30kpoVE3RHrOUEj5TVV6iXvA 9K1v9vOcmEZFaIR7xlN43AlWKS2JOb9mPePBd8JKSNHETsuAInJ1GleEgbtiHT4z5EvA 4+a3yuQe1+zxEDhUqVLGT+l/6uB6CGVwixLzRyu9bmvk6VkF81yEuk+3MG7aU4QnNcRS 6TKA== X-Gm-Message-State: AOAM532d0h/bV31tIMeMHGeJlwGscMeDmlZLLbzd1vLLYaSKgentMenJ oe8zo5L+9Y06DHVukgYvnZ8beQ== X-Received: by 2002:aa7:81d5:0:b029:142:2501:39fa with SMTP id c21-20020aa781d50000b0290142250139famr3333605pfn.73.1601970863160; Tue, 06 Oct 2020 00:54:23 -0700 (PDT) Received: from ikjn-p920.tpe.corp.google.com ([2401:fa00:1:10:f693:9fff:fef4:a8fc]) by smtp.gmail.com with ESMTPSA id r6sm2545147pfq.11.2020.10.06.00.54.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Oct 2020 00:54:22 -0700 (PDT) From: Ikjoon Jang To: Rob Herring , Mark Brown , devicetree@vger.kernel.org, linux-spi@vger.kernel.org, linux-mtd@lists.infradead.org Cc: Ikjoon Jang , Matthias Brugger , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mediatek@lists.infradead.org Subject: [PATCH v5 4/4] spi: spi-mtk-nor: Add power management support Date: Tue, 6 Oct 2020 15:54:05 +0800 Message-Id: <20201006155010.v5.4.I68983b582d949a91866163bab588ff3c2a0d0275@changeid> X-Mailer: git-send-email 2.28.0.806.g8561365e88-goog In-Reply-To: <20201006075405.11658-1-ikjn@chromium.org> References: <20201006075405.11658-1-ikjn@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds dev_pm_ops to mtk-nor to support suspend/resume, auto suspend delay is set to -1 by default. Accessing registers are only permitted after its clock is enabled to deal with unknown state of operating clk at probe time. Signed-off-by: Ikjoon Jang --- drivers/spi/spi-mtk-nor.c | 98 ++++++++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 22 deletions(-) diff --git a/drivers/spi/spi-mtk-nor.c b/drivers/spi/spi-mtk-nor.c index e46d5c93d742..b97f26a60cbe 100644 --- a/drivers/spi/spi-mtk-nor.c +++ b/drivers/spi/spi-mtk-nor.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -690,22 +691,15 @@ static int mtk_nor_enable_clk(struct mtk_nor *sp) return 0; } -static int mtk_nor_init(struct mtk_nor *sp) +static void mtk_nor_init(struct mtk_nor *sp) { - int ret; - - ret = mtk_nor_enable_clk(sp); - if (ret) - return ret; - - sp->spi_freq = clk_get_rate(sp->spi_clk); + writel(0, sp->base + MTK_NOR_REG_IRQ_EN); + writel(MTK_NOR_IRQ_MASK, sp->base + MTK_NOR_REG_IRQ_STAT); writel(MTK_NOR_ENABLE_SF_CMD, sp->base + MTK_NOR_REG_WP); mtk_nor_rmw(sp, MTK_NOR_REG_CFG2, MTK_NOR_WR_CUSTOM_OP_EN, 0); mtk_nor_rmw(sp, MTK_NOR_REG_CFG3, MTK_NOR_DISABLE_WREN | MTK_NOR_DISABLE_SR_POLL, 0); - - return ret; } static irqreturn_t mtk_nor_irq_handler(int irq, void *data) @@ -788,6 +782,7 @@ static int mtk_nor_probe(struct platform_device *pdev) ctlr->num_chipselect = 1; ctlr->setup = mtk_nor_setup; ctlr->transfer_one_message = mtk_nor_transfer_one_message; + ctlr->auto_runtime_pm = true; dev_set_drvdata(&pdev->dev, ctlr); @@ -811,12 +806,19 @@ static int mtk_nor_probe(struct platform_device *pdev) return -ENOMEM; } + ret = mtk_nor_enable_clk(sp); + if (ret < 0) + return ret; + + sp->spi_freq = clk_get_rate(sp->spi_clk); + + mtk_nor_init(sp); + irq = platform_get_irq_optional(pdev, 0); + if (irq < 0) { dev_warn(sp->dev, "IRQ not available."); } else { - writel(MTK_NOR_IRQ_MASK, base + MTK_NOR_REG_IRQ_STAT); - writel(0, base + MTK_NOR_REG_IRQ_EN); ret = devm_request_irq(sp->dev, irq, mtk_nor_irq_handler, 0, pdev->name, sp); if (ret < 0) { @@ -827,34 +829,86 @@ static int mtk_nor_probe(struct platform_device *pdev) } } - ret = mtk_nor_init(sp); - if (ret < 0) { - kfree(ctlr); - return ret; - } + pm_runtime_set_autosuspend_delay(&pdev->dev, -1); + pm_runtime_use_autosuspend(&pdev->dev); + pm_runtime_set_active(&pdev->dev); + pm_runtime_enable(&pdev->dev); + pm_runtime_get_noresume(&pdev->dev); + + ret = devm_spi_register_controller(&pdev->dev, ctlr); + if (ret < 0) + goto err_probe; + + pm_runtime_mark_last_busy(&pdev->dev); + pm_runtime_put_autosuspend(&pdev->dev); dev_info(&pdev->dev, "spi frequency: %d Hz\n", sp->spi_freq); - return devm_spi_register_controller(&pdev->dev, ctlr); + return 0; + +err_probe: + pm_runtime_disable(&pdev->dev); + pm_runtime_set_suspended(&pdev->dev); + pm_runtime_dont_use_autosuspend(&pdev->dev); + + mtk_nor_disable_clk(sp); + + return ret; } static int mtk_nor_remove(struct platform_device *pdev) { - struct spi_controller *ctlr; - struct mtk_nor *sp; + struct spi_controller *ctlr = dev_get_drvdata(&pdev->dev); + struct mtk_nor *sp = spi_controller_get_devdata(ctlr); - ctlr = dev_get_drvdata(&pdev->dev); - sp = spi_controller_get_devdata(ctlr); + pm_runtime_disable(&pdev->dev); + pm_runtime_set_suspended(&pdev->dev); + pm_runtime_dont_use_autosuspend(&pdev->dev); + + mtk_nor_disable_clk(sp); + + return 0; +} + +static int __maybe_unused mtk_nor_runtime_suspend(struct device *dev) +{ + struct spi_controller *ctlr = dev_get_drvdata(dev); + struct mtk_nor *sp = spi_controller_get_devdata(ctlr); mtk_nor_disable_clk(sp); return 0; } +static int __maybe_unused mtk_nor_runtime_resume(struct device *dev) +{ + struct spi_controller *ctlr = dev_get_drvdata(dev); + struct mtk_nor *sp = spi_controller_get_devdata(ctlr); + + return mtk_nor_enable_clk(sp); +} + +static int __maybe_unused mtk_nor_suspend(struct device *dev) +{ + return pm_runtime_force_suspend(dev); +} + +static int __maybe_unused mtk_nor_resume(struct device *dev) +{ + return pm_runtime_force_resume(dev); +} + +static const struct dev_pm_ops mtk_nor_pm_ops = { + SET_RUNTIME_PM_OPS(mtk_nor_runtime_suspend, + mtk_nor_runtime_resume, NULL) + SET_SYSTEM_SLEEP_PM_OPS(mtk_nor_suspend, mtk_nor_resume) +}; + static struct platform_driver mtk_nor_driver = { .driver = { .name = DRIVER_NAME, .of_match_table = mtk_nor_match, + .pm = &mtk_nor_pm_ops, }, .probe = mtk_nor_probe, .remove = mtk_nor_remove, -- 2.28.0.806.g8561365e88-goog