[RFC PATCH] PM: runtime: Apply pinctrl settings if defined

From: Joy Chakraborty
Date: Fri Nov 10 2023 - 12:58:17 EST


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@xxxxxxxxxx>
---
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