From patchwork Mon Sep 9 13:34:38 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mika Westerberg X-Patchwork-Id: 2861171 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 45E48BF43F for ; Mon, 9 Sep 2013 13:36:33 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 869022013A for ; Mon, 9 Sep 2013 13:36:28 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2B16220119 for ; Mon, 9 Sep 2013 13:36:27 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VJ1d3-0000BR-Rz; Mon, 09 Sep 2013 13:35:46 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1VJ1co-0006A4-V4; Mon, 09 Sep 2013 13:35:30 +0000 Received: from mga14.intel.com ([143.182.124.37]) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1VJ1cl-000687-CE for linux-arm-kernel@lists.infradead.org; Mon, 09 Sep 2013 13:35:28 +0000 Received: from azsmga001.ch.intel.com ([10.2.17.19]) by azsmga102.ch.intel.com with ESMTP; 09 Sep 2013 06:34:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.90,866,1371106800"; d="scan'208";a="357858167" Received: from blue.fi.intel.com ([10.237.72.156]) by azsmga001.ch.intel.com with ESMTP; 09 Sep 2013 06:34:40 -0700 Received: by blue.fi.intel.com (Postfix, from userid 1004) id E18EFE0093; Mon, 9 Sep 2013 16:34:39 +0300 (EEST) From: Mika Westerberg To: linux-i2c@vger.kernel.org Subject: [PATCH RESEND 1/2] i2c: prepare runtime PM support for I2C client devices Date: Mon, 9 Sep 2013 16:34:38 +0300 Message-Id: <1378733679-19500-2-git-send-email-mika.westerberg@linux.intel.com> X-Mailer: git-send-email 1.8.4.rc3 In-Reply-To: <1378733679-19500-1-git-send-email-mika.westerberg@linux.intel.com> References: <1378733679-19500-1-git-send-email-mika.westerberg@linux.intel.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130909_093527_685402_2CBB743C X-CRM114-Status: GOOD ( 23.76 ) X-Spam-Score: -7.8 (-------) Cc: Aaron Lu , Wolfram Sang , "Rafael J. Wysocki" , linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, Lv Zheng , Mika Westerberg , linux-arm-kernel@lists.infradead.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-5.1 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, 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 From: Aaron Lu This patch adds runtime PM support for the I2C bus in a similar way that has been done for PCI bus already. This means that the I2C bus core prepares runtime PM for a client device just before a driver is about to be bound to it. Devices that are not bound to any driver are not prepared for runtime PM. In order to take advantage of this runtime PM support, the client device driver needs drop the device runtime PM reference count by calling pm_runtime_put() in its ->probe() callback and possibly implement rest of the runtime PM callbacks. However, this does not yet make runtime PM happen for the device, it has to be explicitly allowed from userspace per each I2C client device. The reason for this is that things like HID over I2C might not work as smoothly when runtime PM is active. So we leave it to the user to balance between performance and power efficiency. User can allow runtime PM for the client device by running: # echo auto > /sys/bus/i2c/devices//power/control and it can be forbidden again by: # echo on > /sys/bus/i2c/devices//power/control Status of the device can be monitored by reading files under the device power directory. If the driver doesn't support runtime PM (like most of the existing I2C client drivers), the device in question is regarded as being runtime PM active and powered on. The patch adds also runtime PM support for the adapter device because it is needed to be able to runtime power manage the I2C controller device. The adapter device is handled along with the I2C controller device (it uses pm_runtime_no_callbacks()). Signed-off-by: Aaron Lu Signed-off-by: Mika Westerberg Acked-by: Rafael J. Wysocki --- drivers/i2c/i2c-core.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index 3d44292..8fad5ac 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c @@ -254,11 +254,34 @@ static int i2c_device_probe(struct device *dev) client->flags & I2C_CLIENT_WAKE); dev_dbg(dev, "probe\n"); + /* Make sure the adapter is active */ + pm_runtime_get_sync(&client->adapter->dev); + + /* + * Enable runtime PM for the client device. If the client wants to + * participate on runtime PM it should call pm_runtime_put() in its + * probe() callback. + * + * User still needs to allow the PM runtime before it can actually + * happen. + */ + pm_runtime_forbid(&client->dev); + pm_runtime_get_noresume(&client->dev); + pm_runtime_set_active(&client->dev); + pm_runtime_enable(&client->dev); + status = driver->probe(client, i2c_match_id(driver->id_table, client)); if (status) { client->driver = NULL; i2c_set_clientdata(client, NULL); + + pm_runtime_disable(&client->dev); + pm_runtime_set_suspended(&client->dev); + pm_runtime_put_noidle(&client->dev); } + + pm_runtime_put(&client->adapter->dev); + return status; } @@ -271,6 +294,8 @@ static int i2c_device_remove(struct device *dev) if (!client || !dev->driver) return 0; + pm_runtime_get_sync(&client->adapter->dev); + driver = to_i2c_driver(dev->driver); if (driver->remove) { dev_dbg(dev, "remove\n"); @@ -283,6 +308,13 @@ static int i2c_device_remove(struct device *dev) client->driver = NULL; i2c_set_clientdata(client, NULL); } + + /* Undo the runtime PM done in i2c_probe() */ + pm_runtime_disable(&client->dev); + pm_runtime_set_suspended(&client->dev); + pm_runtime_put_noidle(&client->dev); + + pm_runtime_put(&client->adapter->dev); return status; } @@ -294,8 +326,11 @@ static void i2c_device_shutdown(struct device *dev) if (!client || !dev->driver) return; driver = to_i2c_driver(dev->driver); - if (driver->shutdown) + if (driver->shutdown) { + pm_runtime_get_sync(&client->adapter->dev); driver->shutdown(client); + pm_runtime_put(&client->adapter->dev); + } } #ifdef CONFIG_PM_SLEEP @@ -1263,6 +1298,15 @@ exit_recovery: bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter); mutex_unlock(&core_lock); + /* + * Make sure the adapter runtime PM follows the parent device (the + * host controller) so that we can suspend it once there aren't any + * active clients anymore. + */ + pm_runtime_set_active(&adap->dev); + pm_runtime_no_callbacks(&adap->dev); + pm_runtime_enable(&adap->dev); + return 0; out_list: @@ -1427,6 +1471,8 @@ void i2c_del_adapter(struct i2c_adapter *adap) return; } + pm_runtime_disable(&adap->dev); + /* Tell drivers about this removal */ mutex_lock(&core_lock); bus_for_each_drv(&i2c_bus_type, NULL, adap,