From patchwork Thu Feb 13 14:09:04 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulf Hansson X-Patchwork-Id: 3645461 Return-Path: X-Original-To: patchwork-linux-mmc@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 3C9D79F38B for ; Thu, 13 Feb 2014 14:09:25 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 18390201ED for ; Thu, 13 Feb 2014 14:09:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E4D38201F2 for ; Thu, 13 Feb 2014 14:09:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751551AbaBMOJT (ORCPT ); Thu, 13 Feb 2014 09:09:19 -0500 Received: from mail-we0-f178.google.com ([74.125.82.178]:57784 "EHLO mail-we0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751514AbaBMOJR (ORCPT ); Thu, 13 Feb 2014 09:09:17 -0500 Received: by mail-we0-f178.google.com with SMTP id q59so7432469wes.37 for ; Thu, 13 Feb 2014 06:09:16 -0800 (PST) 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:in-reply-to :references; bh=NzHHfoNjJXE9HGZMYg2cZ49rnzmc56bi1If+TGyC7WI=; b=cXhE8pL82xjTZq8nX2hJ8IXiqMOZuae9RDkW2q2pctRmBK1si8gExQdhFG1fLaAdKw z0QbZLt18CrN5k4U9ixUaj8Y8JAsRa/ILRvdwsHQjrNpSkqaj1596mbDHH8jlLubf+rH ID9kki1YODWlHZ+hY5GVB7/nC1Ny3luO7WkKcT9DS5OEqcy7rmH79Pyoo6oHDpt9j3LA avgoO3vJEWescNcOHkkLyV1xP2QxI7UdxpuRHCfB+X7nR7KbHoC3fUqijckA4bSXuSL/ mTe1um8r2OwYT4K71BQAepccbmZ9wmGukbuLHena4KpPmRF/EQahc8/M4NK1CsPYlEw9 hN1g== X-Gm-Message-State: ALoCoQkTjpGv59uxexYMU6EsU5cWzIRn1DJ5q1VaxHCLdUc+yV8cvFbXJ+ZiuHqKaZhYfuNQPryb X-Received: by 10.194.234.106 with SMTP id ud10mr1641515wjc.0.1392300556437; Thu, 13 Feb 2014 06:09:16 -0800 (PST) Received: from localhost.localdomain ([85.235.11.236]) by mx.google.com with ESMTPSA id q15sm4823177wjw.18.2014.02.13.06.09.14 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 13 Feb 2014 06:09:15 -0800 (PST) From: Ulf Hansson To: Russell King , linux-arm-kernel@lists.infradead.org Cc: Alessandro Rubini , Linus Walleij , Wolfram Sang , Chris Ball , Mark Brown , linux-kernel@vger.kernel.org, linux-i2c@vger.kernel.org, linux-spi@vger.kernel.org, linux-mmc@vger.kernel.org, Ulf Hansson Subject: [PATCH V2 14/17] i2c: nomadik: Fixup deployment of runtime PM Date: Thu, 13 Feb 2014 15:09:04 +0100 Message-Id: <1392300547-23365-3-git-send-email-ulf.hansson@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1392300547-23365-1-git-send-email-ulf.hansson@linaro.org> References: <1392300547-23365-1-git-send-email-ulf.hansson@linaro.org> Sender: linux-mmc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00,KHOP_BIG_TO_CC, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Since the runtime PM state is expected to be active according to the amba bus, we must align our behaviour while probing to it. Moreover, this is needed to be able to have the driver fully functional without depending on CONFIG_RUNTIME_PM. Since the device is active while a successful probe has been completed, the reference counting for the clock will be screwed up and never reach zero. We resolve this by implementing runtime PM callbacks and let them handle the resources accordingly, including the clock. Cc: Alessandro Rubini Cc: Linus Walleij Cc: Wolfram Sang Signed-off-by: Ulf Hansson --- Changes in v2: Rebased on top of latest i2c-nomadik branch. Squashed with PATCH 13/17 from previous version. Add error handling to align to previous behavior. --- drivers/i2c/busses/i2c-nomadik.c | 77 +++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 26 deletions(-) diff --git a/drivers/i2c/busses/i2c-nomadik.c b/drivers/i2c/busses/i2c-nomadik.c index d800d0f..01b78df 100644 --- a/drivers/i2c/busses/i2c-nomadik.c +++ b/drivers/i2c/busses/i2c-nomadik.c @@ -666,7 +666,7 @@ static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags) static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num_msgs) { - int status; + int status = 0; int i; struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap); int j; @@ -675,19 +675,6 @@ static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap, pm_runtime_get_sync(&dev->adev->dev); - status = clk_prepare_enable(dev->clk); - if (status) { - dev_err(&dev->adev->dev, "can't prepare_enable clock\n"); - goto out_clk; - } - - /* Optionaly enable pins to be muxed in and configured */ - pinctrl_pm_select_default_state(&dev->adev->dev); - - status = init_hw(dev); - if (status) - goto out; - /* Attempt three times to send the message queue */ for (j = 0; j < 3; j++) { /* setup the i2c controller */ @@ -708,12 +695,6 @@ static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap, break; } -out: - clk_disable_unprepare(dev->clk); -out_clk: - /* Optionally let pins go into idle state */ - pinctrl_pm_select_idle_state(&dev->adev->dev); - pm_runtime_put_sync(&dev->adev->dev); dev->busy = false; @@ -930,6 +911,41 @@ static int nmk_i2c_resume(struct device *dev) #define nmk_i2c_resume NULL #endif +#if CONFIG_PM +static int nmk_i2c_runtime_suspend(struct device *dev) +{ + struct amba_device *adev = to_amba_device(dev); + struct nmk_i2c_dev *nmk_i2c = amba_get_drvdata(adev); + + clk_disable_unprepare(nmk_i2c->clk); + pinctrl_pm_select_idle_state(dev); + return 0; +} + +static int nmk_i2c_runtime_resume(struct device *dev) +{ + struct amba_device *adev = to_amba_device(dev); + struct nmk_i2c_dev *nmk_i2c = amba_get_drvdata(adev); + int ret; + + ret = clk_prepare_enable(nmk_i2c->clk); + if (ret) { + dev_err(dev, "can't prepare_enable clock\n"); + return ret; + } + + pinctrl_pm_select_default_state(dev); + + ret = init_hw(nmk_i2c); + if (ret) { + clk_disable_unprepare(nmk_i2c->clk); + pinctrl_pm_select_idle_state(dev); + } + + return ret; +} +#endif + /* * We use noirq so that we suspend late and resume before the wakeup interrupt * to ensure that we do the !pm_runtime_suspended() check in resume before @@ -938,6 +954,9 @@ static int nmk_i2c_resume(struct device *dev) static const struct dev_pm_ops nmk_i2c_pm = { .suspend_noirq = nmk_i2c_suspend, .resume_noirq = nmk_i2c_resume, + SET_PM_RUNTIME_PM_OPS(nmk_i2c_runtime_suspend, + nmk_i2c_runtime_resume, + NULL) }; static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap) @@ -1006,11 +1025,6 @@ static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id) amba_set_drvdata(adev, dev); - /* Select default pin state */ - pinctrl_pm_select_default_state(&adev->dev); - /* If possible, let's go to idle until the first transfer */ - pinctrl_pm_select_idle_state(&adev->dev); - dev->virtbase = devm_ioremap(&adev->dev, adev->res.start, resource_size(&adev->res)); if (!dev->virtbase) { @@ -1035,6 +1049,14 @@ static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id) goto err_no_mem; } + ret = clk_prepare_enable(dev->clk); + if (ret) { + dev_err(&adev->dev, "can't prepare_enable clock\n"); + goto err_no_mem; + } + + init_hw(dev); + adap = &dev->adap; adap->dev.of_node = np; adap->dev.parent = &adev->dev; @@ -1054,13 +1076,15 @@ static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id) ret = i2c_add_adapter(adap); if (ret) { dev_err(&adev->dev, "failed to add adapter\n"); - goto err_no_mem; + goto err_no_adap; } pm_runtime_put(&adev->dev); return 0; + err_no_adap: + clk_disable_unprepare(dev->clk); err_no_mem: return ret; @@ -1077,6 +1101,7 @@ static int nmk_i2c_remove(struct amba_device *adev) clear_all_interrupts(dev); /* disable the controller */ i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE); + clk_disable_unprepare(dev->clk); if (res) release_mem_region(res->start, resource_size(res));