linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Joy Chakraborty <joychakr@google.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	 Pavel Machek <pavel@ucw.cz>, Len Brown <len.brown@intel.com>,
	 Linus Walleij <linus.walleij@linaro.org>
Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
	 linux-gpio@vger.kernel.org, manugautam@google.com,
	aniketmaurya@google.com,  Joy Chakraborty <joychakr@google.com>
Subject: [RFC PATCH] PM: runtime: Apply pinctrl settings if defined
Date: Fri, 10 Nov 2023 10:20:54 +0000	[thread overview]
Message-ID: <20231110102054.1393570-1-joychakr@google.com> (raw)

Apply pinctrl state from  runtime framework device state transtion.

Pinctrl states if defined in DT are bookmarked in device structures
but they need to be explicitly applied from device driver callbacks
which is boiler plate code and also not present in many drivers.

If there is a specific order of setting pinctrl state with other driver
actions then the device driver can choose to do it from its pm callbacks,
in such a case this call will be a no-op from the pinctrl core framework
since the desired pinctrl state would already be set.

We could also add a Kconfig knob to enable/disable this, but I do not
see a need to.

Signed-off-by: Joy Chakraborty <joychakr@google.com>
---
 drivers/base/pinctrl.c          | 33 +++++++++++++++++++++++++++++++++
 drivers/base/power/runtime.c    |  9 ++++++++-
 include/linux/pinctrl/devinfo.h | 11 +++++++++++
 3 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/drivers/base/pinctrl.c b/drivers/base/pinctrl.c
index c22864458511..24bb6dba8cb7 100644
--- a/drivers/base/pinctrl.c
+++ b/drivers/base/pinctrl.c
@@ -103,3 +103,36 @@ int pinctrl_bind_pins(struct device *dev)
 
 	return 0;
 }
+
+#ifdef CONFIG_PM
+/**
+ * pinctrl_state_try_default() - Try setting default state if available
+ * @dev The device to set pinctrl state
+ */
+inline void pinctrl_state_try_default(struct device *dev)
+{
+	if (dev->pins && !IS_ERR(dev->pins->default_state))
+		pinctrl_select_state(dev->pins->p, dev->pins->default_state);
+}
+
+/**
+ * pinctrl_state_try_idle() - Try setting idle state if available
+ * @dev The device to set pinctrl state
+ */
+inline void pinctrl_state_try_idle(struct device *dev)
+{
+	if (dev->pins && !IS_ERR(dev->pins->idle_state))
+		pinctrl_select_state(dev->pins->p, dev->pins->idle_state);
+}
+
+/**
+ * pinctrl_state_try_sleep() - Try setting sleep state if available
+ * @dev The device to set pinctrl state
+ */
+inline void pinctrl_state_try_sleep(struct device *dev)
+{
+	if (dev->pins && !IS_ERR(dev->pins->sleep_state))
+		pinctrl_select_state(dev->pins->p, dev->pins->sleep_state);
+}
+#endif
+
diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c
index 4545669cb973..a07efa446330 100644
--- a/drivers/base/power/runtime.c
+++ b/drivers/base/power/runtime.c
@@ -9,6 +9,7 @@
 #include <linux/ktime.h>
 #include <linux/hrtimer.h>
 #include <linux/export.h>
+#include <linux/pinctrl/devinfo.h>
 #include <linux/pm_runtime.h>
 #include <linux/pm_wakeirq.h>
 #include <trace/events/rpm.h>
@@ -499,7 +500,7 @@ static int rpm_idle(struct device *dev, int rpmflags)
 
 	/* If no callback assume success. */
 	if (!callback || dev->power.no_callbacks)
-		goto out;
+		goto no_callback;
 
 	/* Carry out an asynchronous or a synchronous idle notification. */
 	if (rpmflags & RPM_ASYNC) {
@@ -529,6 +530,8 @@ static int rpm_idle(struct device *dev, int rpmflags)
 	dev->power.idle_notification = false;
 	wake_up_all(&dev->power.wait_queue);
 
+ no_callback:
+	pinctrl_state_try_idle(dev);
  out:
 	trace_rpm_return_int(dev, _THIS_IP_, retval);
 	return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO);
@@ -676,6 +679,7 @@ static int rpm_suspend(struct device *dev, int rpmflags)
  no_callback:
 	__update_runtime_status(dev, RPM_SUSPENDED);
 	pm_runtime_deactivate_timer(dev);
+	pinctrl_state_try_sleep(dev);
 
 	if (dev->parent) {
 		parent = dev->parent;
@@ -918,6 +922,7 @@ static int rpm_resume(struct device *dev, int rpmflags)
  no_callback:
 		__update_runtime_status(dev, RPM_ACTIVE);
 		pm_runtime_mark_last_busy(dev);
+		pinctrl_state_try_default(dev);
 		if (parent)
 			atomic_inc(&parent->power.child_count);
 	}
@@ -1899,6 +1904,7 @@ int pm_runtime_force_suspend(struct device *dev)
 		__update_runtime_status(dev, RPM_SUSPENDED);
 		dev->power.needs_force_resume = 1;
 	}
+	pinctrl_state_try_sleep(dev);
 
 	return 0;
 
@@ -1946,6 +1952,7 @@ int pm_runtime_force_resume(struct device *dev)
 	}
 
 	pm_runtime_mark_last_busy(dev);
+	pinctrl_state_try_default(dev);
 out:
 	dev->power.needs_force_resume = 0;
 	pm_runtime_enable(dev);
diff --git a/include/linux/pinctrl/devinfo.h b/include/linux/pinctrl/devinfo.h
index bb6653af4f92..c3a63e96dd37 100644
--- a/include/linux/pinctrl/devinfo.h
+++ b/include/linux/pinctrl/devinfo.h
@@ -54,6 +54,12 @@ static inline struct pinctrl *dev_pinctrl(struct device *dev)
 	return dev->pins->p;
 }
 
+#ifdef CONFIG_PM
+extern inline void pinctrl_state_try_default(struct device *dev);
+extern inline void pinctrl_state_try_idle(struct device *dev);
+extern inline void pinctrl_state_try_sleep(struct device *dev);
+#endif /* CONFIG_PM */
+
 #else
 
 /* Stubs if we're not using pinctrl */
@@ -73,5 +79,10 @@ static inline struct pinctrl *dev_pinctrl(struct device *dev)
 	return NULL;
 }
 
+#ifdef CONFIG_PM
+static inline void pinctrl_state_try_default(struct device *dev) {}
+static inline void pinctrl_state_try_idle(struct device *dev) {}
+static inline void pinctrl_state_try_sleep(struct device *dev) {}
+#endif /* CONFIG_PM */
 #endif /* CONFIG_PINCTRL */
 #endif /* PINCTRL_DEVINFO_H */
-- 
2.42.0.869.gea05f2083d-goog



             reply	other threads:[~2023-11-10 17:47 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-10 10:20 Joy Chakraborty [this message]
2023-11-14 13:01 ` [RFC PATCH] PM: runtime: Apply pinctrl settings if defined Linus Walleij
2023-11-15  1:51   ` andy.shevchenko
2023-11-16 15:34     ` Joy Chakraborty
2023-11-16 20:00       ` Linus Walleij
2023-12-01  6:08         ` Joy Chakraborty

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=20231110102054.1393570-1-joychakr@google.com \
    --to=joychakr@google.com \
    --cc=aniketmaurya@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=len.brown@intel.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=manugautam@google.com \
    --cc=pavel@ucw.cz \
    --cc=rafael@kernel.org \
    /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).