linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lucas Stach <l.stach@pengutronix.de>
To: Wolfram Sang <wsa@the-dreams.de>
Cc: linux-i2c@vger.kernel.org, kernel@pengutronix.de,
	patchwork-lst@pengutronix.de, Lukas Rusak <lorusak@gmail.com>,
	Chris Healy <cphealy@gmail.com>
Subject: [PATCH 2/3] i2c: add runtime PM handling to core
Date: Fri, 17 Nov 2017 19:05:38 +0100	[thread overview]
Message-ID: <20171117180539.4984-3-l.stach@pengutronix.de> (raw)
In-Reply-To: <20171117180539.4984-1-l.stach@pengutronix.de>

This allows to simplify the drivers if they opt to use the runtime PM
provided by the core. Even more importantly it allows to move the
runtime PM handling out of the i2c bus lock. As runtime PM may need to
take other locks itself (i.e. when manipulating clocks), holding the
bus lock may lead to deadlocks, due to different lock ordering.

Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
---
 drivers/i2c/i2c-core-base.c | 18 ++++++++++++++++--
 include/linux/i2c.h         |  1 +
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index 56e46581b84b..3c971711171c 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -1931,11 +1931,19 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 		}
 #endif
 
+		if (adap->auto_rpm) {
+			ret = pm_runtime_get_sync(adap->dev.parent);
+			if (ret < 0)
+				return ret;
+		}
+
 		if (in_atomic() || irqs_disabled()) {
 			ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
-			if (!ret)
+			if (!ret) {
 				/* I2C activity is ongoing. */
-				return -EAGAIN;
+				ret = -EAGAIN;
+				goto out_rpm;
+			}
 		} else {
 			i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
 		}
@@ -1943,6 +1951,12 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 		ret = __i2c_transfer(adap, msgs, num);
 		i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
 
+out_rpm:
+		if (adap->auto_rpm) {
+			pm_runtime_mark_last_busy(adap->dev.parent);
+			pm_runtime_put_autosuspend(adap->dev.parent);
+		}
+
 		return ret;
 	} else {
 		dev_dbg(&adap->dev, "I2C level transfers not supported\n");
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index d501d3956f13..6bc3d1ed554e 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -585,6 +585,7 @@ struct i2c_adapter {
 	int nr;
 	char name[48];
 	struct completion dev_released;
+	bool auto_rpm;			/* runtime PM managed by core */
 
 	struct mutex userspace_clients_lock;
 	struct list_head userspace_clients;
-- 
2.11.0

  parent reply	other threads:[~2017-11-17 18:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-17 18:05 [PATCH 0/3] imx i2c vs clk framework deadlock fix Lucas Stach
2017-11-17 18:05 ` [PATCH 1/3] i2c: imx: use clk notifier for rate changes Lucas Stach
2017-11-17 18:05 ` Lucas Stach [this message]
2017-11-17 18:05 ` [PATCH 3/3] i2c: imx: use runtime PM provided by core Lucas Stach

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171117180539.4984-3-l.stach@pengutronix.de \
    --to=l.stach@pengutronix.de \
    --cc=cphealy@gmail.com \
    --cc=kernel@pengutronix.de \
    --cc=linux-i2c@vger.kernel.org \
    --cc=lorusak@gmail.com \
    --cc=patchwork-lst@pengutronix.de \
    --cc=wsa@the-dreams.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).