Skip to contentSkip to conversation

i2c: Hook up runtime PM support

PATCH In review Mark Brown 4 participants 8 messages Feb 5, 2010 – Feb 15, 2010 4 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

Mark Brown

author
#0 lore raw

PATCH 1/1 i2c: Hook up runtime PM support

Allow I2C drivers to make use of the runtime PM framework by adding bus implementations of the runtime PM operations. These simply immediately suspend when the device is idle. The runtime PM framework provides drivers with off the shelf refcounts for enables and sysfs control for managing runtime suspend from userspace so is useful even without meaningful input from the bus.

---

Changed to allow drivers to use the idle callback to veto suspend.

 drivers/i2c/i2c-core.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)
drivers/i2c/i2c-core.c +50 -0
index 10be7b5..4131698 100644
@@ -34,6 +34,7 @@
 #include <linux/hardirq.h>
 #include <linux/irqflags.h>
 #include <linux/rwsem.h>
+#include <linux/pm_runtime.h>
 #include <asm/uaccess.h>
 
 #include "i2c-core.h"
@@ -184,6 +185,52 @@ static int i2c_device_pm_resume(struct device *dev)
 #define i2c_device_pm_resume	NULL
 #endif
 
+#ifdef CONFIG_PM_RUNTIME
+static int i2c_device_runtime_suspend(struct device *dev)
+{
+	const struct dev_pm_ops *pm;
+
+	if (!dev->driver)
+		return 0;
+	pm = dev->driver->pm;
+	if (!pm || !pm->runtime_suspend)
+		return 0;
+	return pm->runtime_suspend(dev);
+}
+
+static int i2c_device_runtime_resume(struct device *dev)
+{
+	const struct dev_pm_ops *pm;
+
+	if (!dev->driver)
+		return 0;
+	pm = dev->driver->pm;
+	if (!pm || !pm->runtime_resume)
+		return 0;
+	return pm->runtime_resume(dev);
+}
+
+static int i2c_device_runtime_idle(struct device *dev)
+{
+	const struct dev_pm_ops *pm = NULL;
+	int ret;
+
+	if (dev->driver)
+		pm = dev->driver->pm;
+	if (pm && pm->runtime_idle) {
+		ret = pm->runtime_idle(dev);
+		if (ret)
+			return ret;
+	}
+
+	return pm_runtime_suspend(dev);
+}
+#else
+#define i2c_device_runtime_suspend	NULL
+#define i2c_device_runtime_resume	NULL
+#define i2c_device_runtime_idle		NULL
+#endif
+
 static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
 {
 	struct i2c_client *client = i2c_verify_client(dev);
@@ -251,6 +298,9 @@ static const struct attribute_group *i2c_dev_attr_groups[] = {
 static const struct dev_pm_ops i2c_device_pm_ops = {
 	.suspend = i2c_device_pm_suspend,
 	.resume = i2c_device_pm_resume,
+	.runtime_suspend = i2c_device_runtime_suspend,
+	.runtime_resume = i2c_device_runtime_resume,
+	.runtime_idle = i2c_device_runtime_idle,
 };
 
 struct bus_type i2c_bus_type = {
Signature · 1 line
1.6.6

A message that is not in the archive (sent off-list or not yet received): 1265373011-12874-1-git-send-email-broonie-yzvPICuk2AATkU/[email protected]

#2 lore raw
Quoted 8 lines · Mark Brown

Allow I2C drivers to make use of the runtime PM framework by adding bus implementations of the runtime PM operations. These simply immediately suspend when the device is idle. The runtime PM framework provides drivers with off the shelf refcounts for enables and sysfs control for managing runtime suspend from userspace so is useful even without meaningful input from the bus.

Quoted 83 lines

---

Changed to allow drivers to use the idle callback to veto suspend.

 drivers/i2c/i2c-core.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)
drivers/i2c/i2c-core.c +50 -0
index 10be7b5..4131698 100644
@@ -34,6 +34,7 @@
 #include <linux/hardirq.h>
 #include <linux/irqflags.h>
 #include <linux/rwsem.h>
+#include <linux/pm_runtime.h>
 #include <asm/uaccess.h>
 
 #include "i2c-core.h"
@@ -184,6 +185,52 @@ static int i2c_device_pm_resume(struct device *dev)
 #define i2c_device_pm_resume	NULL
 #endif
 
+#ifdef CONFIG_PM_RUNTIME
+static int i2c_device_runtime_suspend(struct device *dev)
+{
+	const struct dev_pm_ops *pm;
+
+	if (!dev->driver)
+		return 0;
+	pm = dev->driver->pm;
+	if (!pm || !pm->runtime_suspend)
+		return 0;
+	return pm->runtime_suspend(dev);
+}
+
+static int i2c_device_runtime_resume(struct device *dev)
+{
+	const struct dev_pm_ops *pm;
+
+	if (!dev->driver)
+		return 0;
+	pm = dev->driver->pm;
+	if (!pm || !pm->runtime_resume)
+		return 0;
+	return pm->runtime_resume(dev);
+}
+
+static int i2c_device_runtime_idle(struct device *dev)
+{
+	const struct dev_pm_ops *pm = NULL;
+	int ret;
+
+	if (dev->driver)
+		pm = dev->driver->pm;
+	if (pm && pm->runtime_idle) {
+		ret = pm->runtime_idle(dev);
+		if (ret)
+			return ret;
+	}
+
+	return pm_runtime_suspend(dev);
+}
+#else
+#define i2c_device_runtime_suspend	NULL
+#define i2c_device_runtime_resume	NULL
+#define i2c_device_runtime_idle		NULL
+#endif
+
 static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
 {
 	struct i2c_client *client = i2c_verify_client(dev);
@@ -251,6 +298,9 @@ static const struct attribute_group *i2c_dev_attr_groups[] = {
 static const struct dev_pm_ops i2c_device_pm_ops = {
 	.suspend = i2c_device_pm_suspend,
 	.resume = i2c_device_pm_resume,
+	.runtime_suspend = i2c_device_runtime_suspend,
+	.runtime_resume = i2c_device_runtime_resume,
+	.runtime_idle = i2c_device_runtime_idle,
 };
 
 struct bus_type i2c_bus_type = {
#3 lore raw
In reply to a message not in the archive

Hi Mark,

Quoted 81 lines · Mark Brown

Allow I2C drivers to make use of the runtime PM framework by adding bus implementations of the runtime PM operations. These simply immediately suspend when the device is idle. The runtime PM framework provides drivers with off the shelf refcounts for enables and sysfs control for managing runtime suspend from userspace so is useful even without meaningful input from the bus.

---

Changed to allow drivers to use the idle callback to veto suspend.

 drivers/i2c/i2c-core.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)
drivers/i2c/i2c-core.c +47 -0
index 10be7b5..4131698 100644
@@ -34,6 +34,7 @@
 #include <linux/hardirq.h>
 #include <linux/irqflags.h>
 #include <linux/rwsem.h>
+#include <linux/pm_runtime.h>
 #include <asm/uaccess.h>
 
 #include "i2c-core.h"
@@ -184,6 +185,52 @@ static int i2c_device_pm_resume(struct device *dev)
 #define i2c_device_pm_resume	NULL
 #endif
 
+#ifdef CONFIG_PM_RUNTIME
+static int i2c_device_runtime_suspend(struct device *dev)
+{
+	const struct dev_pm_ops *pm;
+
+	if (!dev->driver)
+		return 0;
+	pm = dev->driver->pm;
+	if (!pm || !pm->runtime_suspend)
+		return 0;
+	return pm->runtime_suspend(dev);
+}
+
+static int i2c_device_runtime_resume(struct device *dev)
+{
+	const struct dev_pm_ops *pm;
+
+	if (!dev->driver)
+		return 0;
+	pm = dev->driver->pm;
+	if (!pm || !pm->runtime_resume)
+		return 0;
+	return pm->runtime_resume(dev);
+}
+
+static int i2c_device_runtime_idle(struct device *dev)
+{
+	const struct dev_pm_ops *pm = NULL;
+	int ret;
+
+	if (dev->driver)
+		pm = dev->driver->pm;
+	if (pm && pm->runtime_idle) {
+		ret = pm->runtime_idle(dev);
+		if (ret)
+			return ret;
+	}
+
+	return pm_runtime_suspend(dev);
+}
+#else
+#define i2c_device_runtime_suspend	NULL
+#define i2c_device_runtime_resume	NULL
+#define i2c_device_runtime_idle		NULL
+#endif
+
 static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
 {
 	struct i2c_client *client = i2c_verify_client(dev);

drivers/i2c/i2c-core.c static const struct attribute_group *i2c_dev_attr_groups[] = { · line 306

Quoted 9 lines
drivers/i2c/i2c-core.c +3 -0
@@ -251,6 +298,9 @@ static const struct attribute_group *i2c_dev_attr_groups[] = {
 static const struct dev_pm_ops i2c_device_pm_ops = {
 	.suspend = i2c_device_pm_suspend,
 	.resume = i2c_device_pm_resume,
+	.runtime_suspend = i2c_device_runtime_suspend,
+	.runtime_resume = i2c_device_runtime_resume,
+	.runtime_idle = i2c_device_runtime_idle,
 };
 
 struct bus_type i2c_bus_type = {

Applied, thanks. I am a little surprised to see that these functions have nothing i2c-specific, so I am wondering why we have to duplicate them in every bus type... Shouldn't the functions above be part of drivers/base/power/runtime.c and exported so that all bus types that want them can reuse them?

Signature · 1 line
Jean Delvare

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#5 lore raw
Quoted 5 lines · Jean Delvare

Applied, thanks. I am a little surprised to see that these functions have nothing i2c-specific, so I am wondering why we have to duplicate them in every bus type... Shouldn't the functions above be part of drivers/base/power/runtime.c and exported so that all bus types that want them can reuse them?

I tend to agree - in fact I'd been a little surprised the default for buses that don't provide anything was to refuse to do runtime PM (though I can see the transition issues when a bus does want to go and do its own thing).

My actual plan here was to implement this for a couple of buses and then present a patch factoring out the common code.

A message that is not in the archive (sent off-list or not yet received): 20100215183058.GA24590-HF5t3jzXg/6ND3a5+9QAFujbO/[email protected]

#7 lore raw
Quoted 7 lines · Mark Brown
Quoted 5 lines · Jean Delvare

Applied, thanks. I am a little surprised to see that these functions have nothing i2c-specific, so I am wondering why we have to duplicate them in every bus type... Shouldn't the functions above be part of drivers/base/power/runtime.c and exported so that all bus types that want them can reuse them?

In fact this is the first bus type that doesn't need anything specific in these routines so fat.

Quoted 7 lines

I tend to agree - in fact I'd been a little surprised the default for buses that don't provide anything was to refuse to do runtime PM (though I can see the transition issues when a bus does want to go and do its own thing).

My actual plan here was to implement this for a couple of buses and then present a patch factoring out the common code.

That's a good approach IMO.

Rafael

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw
Quoted 17 lines · Rafael J. Wysocki
Quoted 7 lines · Mark Brown
Quoted 5 lines · Jean Delvare

Applied, thanks. I am a little surprised to see that these functions have nothing i2c-specific, so I am wondering why we have to duplicate them in every bus type... Shouldn't the functions above be part of drivers/base/power/runtime.c and exported so that all bus types that want them can reuse them?

In fact this is the first bus type that doesn't need anything specific in these routines so fat.

Quoted 5 lines

I tend to agree - in fact I'd been a little surprised the default for buses that don't provide anything was to refuse to do runtime PM (though I can see the transition issues when a bus does want to go and do its own thing).

Quoted 4 lines
Quoted 2 lines

My actual plan here was to implement this for a couple of buses and then present a patch factoring out the common code.

That's a good approach IMO.

OK, fine with me.

Signature · 1 line
Jean Delvare
author
#10 lore raw
In reply to a message not in the archive
Quoted 11 lines · Rafael J. Wysocki
Quoted 7 lines · Mark Brown
Quoted 5 lines · Jean Delvare

Applied, thanks. I am a little surprised to see that these functions have nothing i2c-specific, so I am wondering why we have to duplicate them in every bus type... Shouldn't the functions above be part of drivers/base/power/runtime.c and exported so that all bus types that want them can reuse them?

In fact this is the first bus type that doesn't need anything specific in these routines so fat.

Not really - the platform bus is the same, it's just that some platforms are also able to do some neat stuff with the information they glean via runtime PM. The platform bus is actually a bit of a problem here since it gets used both for on-SoC devices where that sort of stuff is possible and also for things like MFDs and random memory mapped things on the system, meaning that drivers can't take advantage of runtime PM unless someone goes through and does something per-SoC which is a bit inconvnient.

There's other buses like SPI that are in the same boat as I2C - they don't really have a cohesive bus level power management structure, or the power management is fully handled by with noop methods on the bus and holding the parent open until all the children are idle.

Acked-by on v1 1/1
#11 lore raw
In reply to Mark Brown
Quoted 8 lines · Mark Brown

Allow I2C drivers to make use of the runtime PM framework by adding bus implementations of the runtime PM operations. These simply immediately suspend when the device is idle. The runtime PM framework provides drivers with off the shelf refcounts for enables and sysfs control for managing runtime suspend from userspace so is useful even without meaningful input from the bus.

Quoted 83 lines

---

Changed to allow drivers to use the idle callback to veto suspend.

 drivers/i2c/i2c-core.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)
drivers/i2c/i2c-core.c +50 -0
index 10be7b5..4131698 100644
@@ -34,6 +34,7 @@
 #include <linux/hardirq.h>
 #include <linux/irqflags.h>
 #include <linux/rwsem.h>
+#include <linux/pm_runtime.h>
 #include <asm/uaccess.h>
 
 #include "i2c-core.h"
@@ -184,6 +185,52 @@ static int i2c_device_pm_resume(struct device *dev)
 #define i2c_device_pm_resume	NULL
 #endif
 
+#ifdef CONFIG_PM_RUNTIME
+static int i2c_device_runtime_suspend(struct device *dev)
+{
+	const struct dev_pm_ops *pm;
+
+	if (!dev->driver)
+		return 0;
+	pm = dev->driver->pm;
+	if (!pm || !pm->runtime_suspend)
+		return 0;
+	return pm->runtime_suspend(dev);
+}
+
+static int i2c_device_runtime_resume(struct device *dev)
+{
+	const struct dev_pm_ops *pm;
+
+	if (!dev->driver)
+		return 0;
+	pm = dev->driver->pm;
+	if (!pm || !pm->runtime_resume)
+		return 0;
+	return pm->runtime_resume(dev);
+}
+
+static int i2c_device_runtime_idle(struct device *dev)
+{
+	const struct dev_pm_ops *pm = NULL;
+	int ret;
+
+	if (dev->driver)
+		pm = dev->driver->pm;
+	if (pm && pm->runtime_idle) {
+		ret = pm->runtime_idle(dev);
+		if (ret)
+			return ret;
+	}
+
+	return pm_runtime_suspend(dev);
+}
+#else
+#define i2c_device_runtime_suspend	NULL
+#define i2c_device_runtime_resume	NULL
+#define i2c_device_runtime_idle		NULL
+#endif
+
 static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
 {
 	struct i2c_client *client = i2c_verify_client(dev);
@@ -251,6 +298,9 @@ static const struct attribute_group *i2c_dev_attr_groups[] = {
 static const struct dev_pm_ops i2c_device_pm_ops = {
 	.suspend = i2c_device_pm_suspend,
 	.resume = i2c_device_pm_resume,
+	.runtime_suspend = i2c_device_runtime_suspend,
+	.runtime_resume = i2c_device_runtime_resume,
+	.runtime_idle = i2c_device_runtime_idle,
 };
 
 struct bus_type i2c_bus_type = {