Skip to contentSkip to conversation

runtime PM support for I2C adapter devices

PATCH New Mika Westerberg 3 participants 10 messages Sep 30, 2013 – Oct 5, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 runtime PM support for I2C adapter devices

Hi,

This is third revision of the patch. The previous version can be found here:

	http://lwn.net/Articles/566234/

It was pointed out that the previous solution was not good for the existing devices for several reasons:
- The I2C adapter is powered on before client ->probe() is called. This causes problems with some devices.
- The I2C adapter is kept powered on if any of its children are active.

However, for ACPI enumerated devices we need to have parent child relationship so that the runtime PM core can power on the adapter device if any of its children become active.

In this version we still enable runtime PM for the I2C adapter device but only for devices that are enumerated from ACPI. The existing drivers should continue to work as they do today.

I combined patches [1/9] and [2/9] from the previous version and added checks for ACPI_HANDLE(). I hope Aaron and Lv are OK with this.

I didn't include changes to the SPI bus (the $subject is changed again to reflect that) because we don't need to change the existing drivers anymore. If this approach is accepted we can do the same for the SPI bus as well.

Mika Westerberg (1):
i2c: enable runtime PM for I2C adapter devices enumerated from ACPI

 drivers/i2c/i2c-core.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)
Signature · 1 line
1.8.4.rc3
#1 lore raw

PATCH v3 1/1 i2c: enable runtime PM for I2C adapter devices enumerated from ACPI

The ACPI specification requires the parent device to be powered on before any of its children. It can be only powered off when all the children are already off.

Currently whenever there is no I2C traffic going on, the I2C controller driver can put the device into low power state transparently to its children (the I2C client devices). This violates the ACPI specification because now the parent device is in lower power state than its children.

In order to keep ACPI happy we enable runtime PM for the I2C adapter device if we find out that the I2C controller was in fact an ACPI device. In addition to that we attach the I2C client devices to the ACPI power domain and make sure that they are powered on when the driver ->probe() is called.

Non-ACPI devices are not affected by this change.

This patch is based on the work by Aaron Lu and Lv Zheng.

---
 drivers/i2c/i2c-core.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)
drivers/i2c/i2c-core.c +46 -1
index 29d3f04..fa861ad 100644
@@ -236,6 +236,27 @@ int i2c_recover_bus(struct i2c_adapter *adap)
 	return adap->bus_recovery_info->recover_bus(adap);
 }
 
+static void acpi_i2c_device_pm_get(struct i2c_client *client)
+{
+	struct i2c_adapter *adap = client->adapter;
+
+	/* Make sure the adapter is active */
+	if (ACPI_HANDLE(adap->dev.parent))
+		pm_runtime_get_sync(&adap->dev);
+	if (ACPI_HANDLE(&client->dev))
+		acpi_dev_pm_attach(&client->dev, true);
+}
+
+static void acpi_i2c_device_pm_put(struct i2c_client *client, bool detach)
+{
+	struct i2c_adapter *adap = client->adapter;
+
+	if (ACPI_HANDLE(&client->dev) && detach)
+		acpi_dev_pm_detach(&client->dev, true);
+	if (ACPI_HANDLE(adap->dev.parent))
+		pm_runtime_put(&adap->dev);
+}
+
 static int i2c_device_probe(struct device *dev)
 {
 	struct i2c_client	*client = i2c_verify_client(dev);
@@ -254,11 +275,15 @@ static int i2c_device_probe(struct device *dev)
 					client->flags & I2C_CLIENT_WAKE);
 	dev_dbg(dev, "probe\n");
 
+	acpi_i2c_device_pm_get(client);
+
 	status = driver->probe(client, i2c_match_id(driver->id_table, client));
 	if (status) {
 		client->driver = NULL;
 		i2c_set_clientdata(client, NULL);
 	}
+
+	acpi_i2c_device_pm_put(client, !!status);
 	return status;
 }
 
@@ -271,6 +296,8 @@ static int i2c_device_remove(struct device *dev)
 	if (!client || !dev->driver)
 		return 0;
 
+	acpi_i2c_device_pm_get(client);
+
 	driver = to_i2c_driver(dev->driver);
 	if (driver->remove) {
 		dev_dbg(dev, "remove\n");
@@ -283,6 +310,8 @@ static int i2c_device_remove(struct device *dev)
 		client->driver = NULL;
 		i2c_set_clientdata(client, NULL);
 	}
+
+	acpi_i2c_device_pm_put(client, true);
 	return status;
 }
 
@@ -294,8 +323,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) {
+		acpi_i2c_device_pm_get(client);
 		driver->shutdown(client);
+		acpi_i2c_device_pm_put(client, false);
+	}
 }
 
 #ifdef CONFIG_PM_SLEEP
@@ -1263,6 +1295,16 @@ exit_recovery:
 	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
 	mutex_unlock(&core_lock);
 
+	/*
+	 * For ACPI enumerated controllers we must make sure that the
+	 * controller is powered on before its children. Runtime PM handles
+	 * this for us once we have enabled it for the adapter device.
+	 */
+	if (ACPI_HANDLE(adap->dev.parent)) {
+		pm_runtime_no_callbacks(&adap->dev);
+		pm_runtime_enable(&adap->dev);
+	}
+
 	return 0;
 
 out_list:
@@ -1427,6 +1469,9 @@ void i2c_del_adapter(struct i2c_adapter *adap)
 		return;
 	}
 
+	if (ACPI_HANDLE(adap->dev.parent))
+		pm_runtime_disable(&adap->dev);
+
 	/* Tell drivers about this removal */
 	mutex_lock(&core_lock);
 	bus_for_each_drv(&i2c_bus_type, NULL, adap,
Signature · 1 line
1.8.4.rc3
#2 lore raw

Re: [PATCH v3] i2c: enable runtime PM for I2C adapter devices enumerated from ACPI

Quoted 30 lines · Mika Westerberg

The ACPI specification requires the parent device to be powered on before any of its children. It can be only powered off when all the children are already off.

Currently whenever there is no I2C traffic going on, the I2C controller driver can put the device into low power state transparently to its children (the I2C client devices). This violates the ACPI specification because now the parent device is in lower power state than its children.

In order to keep ACPI happy we enable runtime PM for the I2C adapter device if we find out that the I2C controller was in fact an ACPI device. In addition to that we attach the I2C client devices to the ACPI power domain and make sure that they are powered on when the driver ->probe() is called.

Non-ACPI devices are not affected by this change.

This patch is based on the work by Aaron Lu and Lv Zheng.

---
 drivers/i2c/i2c-core.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)
drivers/i2c/i2c-core.c +0 -0
index 29d3f04..fa861ad 100644
@@ -236,6 +236,27 @@ int i2c_recover_bus(struct i2c_adapter *adap)
 	return adap->bus_recovery_info->recover_bus(adap);
 }

drivers/i2c/i2c-core.c int i2c_recover_bus(struct i2c_adapter *adap) · line 247

Quoted 10 lines
drivers/i2c/i2c-core.c +9 -0
 
+static void acpi_i2c_device_pm_get(struct i2c_client *client)
+{
+	struct i2c_adapter *adap = client->adapter;
+
+	/* Make sure the adapter is active */
+	if (ACPI_HANDLE(adap->dev.parent))
+		pm_runtime_get_sync(&adap->dev);
+	if (ACPI_HANDLE(&client->dev))
+		acpi_dev_pm_attach(&client->dev, true);

It would be sufficient to do

	if (ACPI_HANDLE(&client->dev)) {
		pm_runtime_get_sync(&adap->dev);
		acpi_dev_pm_attach(&client->dev, true);
	}

here (and below), because I don't think the client with an ACPI handle and the parent without one is extremely unlikely (to the point of non-existence actually ;-)). And even if something like that happens, then we only enable runtime PM for the adapter if the parent has an ACPI handle, so it still should be OK.

Apart from this the patch looks good to me.

Quoted 81 lines
drivers/i2c/i2c-core.c +34 -1
+}
+
+static void acpi_i2c_device_pm_put(struct i2c_client *client, bool detach)
+{
+	struct i2c_adapter *adap = client->adapter;
+
+	if (ACPI_HANDLE(&client->dev) && detach)
+		acpi_dev_pm_detach(&client->dev, true);
+	if (ACPI_HANDLE(adap->dev.parent))
+		pm_runtime_put(&adap->dev);
+}
+
 static int i2c_device_probe(struct device *dev)
 {
 	struct i2c_client	*client = i2c_verify_client(dev);
@@ -254,11 +275,15 @@ static int i2c_device_probe(struct device *dev)
 					client->flags & I2C_CLIENT_WAKE);
 	dev_dbg(dev, "probe\n");
 
+	acpi_i2c_device_pm_get(client);
+
 	status = driver->probe(client, i2c_match_id(driver->id_table, client));
 	if (status) {
 		client->driver = NULL;
 		i2c_set_clientdata(client, NULL);
 	}
+
+	acpi_i2c_device_pm_put(client, !!status);
 	return status;
 }
 
@@ -271,6 +296,8 @@ static int i2c_device_remove(struct device *dev)
 	if (!client || !dev->driver)
 		return 0;
 
+	acpi_i2c_device_pm_get(client);
+
 	driver = to_i2c_driver(dev->driver);
 	if (driver->remove) {
 		dev_dbg(dev, "remove\n");
@@ -283,6 +310,8 @@ static int i2c_device_remove(struct device *dev)
 		client->driver = NULL;
 		i2c_set_clientdata(client, NULL);
 	}
+
+	acpi_i2c_device_pm_put(client, true);
 	return status;
 }
 
@@ -294,8 +323,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) {
+		acpi_i2c_device_pm_get(client);
 		driver->shutdown(client);
+		acpi_i2c_device_pm_put(client, false);
+	}
 }
 
 #ifdef CONFIG_PM_SLEEP
@@ -1263,6 +1295,16 @@ exit_recovery:
 	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
 	mutex_unlock(&core_lock);
 
+	/*
+	 * For ACPI enumerated controllers we must make sure that the
+	 * controller is powered on before its children. Runtime PM handles
+	 * this for us once we have enabled it for the adapter device.
+	 */
+	if (ACPI_HANDLE(adap->dev.parent)) {
+		pm_runtime_no_callbacks(&adap->dev);
+		pm_runtime_enable(&adap->dev);
+	}
+
 	return 0;
 
 out_list:

drivers/i2c/i2c-core.c void i2c_del_adapter(struct i2c_adapter *adap) · line 1477

Quoted 9 lines
drivers/i2c/i2c-core.c +3 -0
@@ -1427,6 +1469,9 @@ void i2c_del_adapter(struct i2c_adapter *adap)
 		return;
 	}
 
+	if (ACPI_HANDLE(adap->dev.parent))
+		pm_runtime_disable(&adap->dev);
+
 	/* Tell drivers about this removal */
 	mutex_lock(&core_lock);
 	bus_for_each_drv(&i2c_bus_type, NULL, adap,

Thanks!

Signature · 2 lines
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
#3 lore raw

Re: [PATCH v3] i2c: enable runtime PM for I2C adapter devices enumerated from ACPI

Quoted 44 lines · Rafael J. Wysocki
Quoted 30 lines · Mika Westerberg

The ACPI specification requires the parent device to be powered on before any of its children. It can be only powered off when all the children are already off.

Currently whenever there is no I2C traffic going on, the I2C controller driver can put the device into low power state transparently to its children (the I2C client devices). This violates the ACPI specification because now the parent device is in lower power state than its children.

In order to keep ACPI happy we enable runtime PM for the I2C adapter device if we find out that the I2C controller was in fact an ACPI device. In addition to that we attach the I2C client devices to the ACPI power domain and make sure that they are powered on when the driver ->probe() is called.

Non-ACPI devices are not affected by this change.

This patch is based on the work by Aaron Lu and Lv Zheng.

---
 drivers/i2c/i2c-core.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)
drivers/i2c/i2c-core.c +0 -0
index 29d3f04..fa861ad 100644
@@ -236,6 +236,27 @@ int i2c_recover_bus(struct i2c_adapter *adap)
 	return adap->bus_recovery_info->recover_bus(adap);
 }

drivers/i2c/i2c-core.c int i2c_recover_bus(struct i2c_adapter *adap) · line 247

Quoted 10 lines
drivers/i2c/i2c-core.c +9 -0
 
+static void acpi_i2c_device_pm_get(struct i2c_client *client)
+{
+	struct i2c_adapter *adap = client->adapter;
+
+	/* Make sure the adapter is active */
+	if (ACPI_HANDLE(adap->dev.parent))
+		pm_runtime_get_sync(&adap->dev);
+	if (ACPI_HANDLE(&client->dev))
+		acpi_dev_pm_attach(&client->dev, true);

It would be sufficient to do

Quoted 6 lines
	if (ACPI_HANDLE(&client->dev)) {
		pm_runtime_get_sync(&adap->dev);
		acpi_dev_pm_attach(&client->dev, true);
	}

here (and below), because I don't think the client with an ACPI handle and the

s/I don't think/I think/

Quoted 101 lines

parent without one is extremely unlikely (to the point of non-existence actually ;-)). And even if something like that happens, then we only enable runtime PM for the adapter if the parent has an ACPI handle, so it still should be OK.

Apart from this the patch looks good to me.

Quoted 81 lines
drivers/i2c/i2c-core.c +34 -1
+}
+
+static void acpi_i2c_device_pm_put(struct i2c_client *client, bool detach)
+{
+	struct i2c_adapter *adap = client->adapter;
+
+	if (ACPI_HANDLE(&client->dev) && detach)
+		acpi_dev_pm_detach(&client->dev, true);
+	if (ACPI_HANDLE(adap->dev.parent))
+		pm_runtime_put(&adap->dev);
+}
+
 static int i2c_device_probe(struct device *dev)
 {
 	struct i2c_client	*client = i2c_verify_client(dev);
@@ -254,11 +275,15 @@ static int i2c_device_probe(struct device *dev)
 					client->flags & I2C_CLIENT_WAKE);
 	dev_dbg(dev, "probe\n");
 
+	acpi_i2c_device_pm_get(client);
+
 	status = driver->probe(client, i2c_match_id(driver->id_table, client));
 	if (status) {
 		client->driver = NULL;
 		i2c_set_clientdata(client, NULL);
 	}
+
+	acpi_i2c_device_pm_put(client, !!status);
 	return status;
 }
 
@@ -271,6 +296,8 @@ static int i2c_device_remove(struct device *dev)
 	if (!client || !dev->driver)
 		return 0;
 
+	acpi_i2c_device_pm_get(client);
+
 	driver = to_i2c_driver(dev->driver);
 	if (driver->remove) {
 		dev_dbg(dev, "remove\n");
@@ -283,6 +310,8 @@ static int i2c_device_remove(struct device *dev)
 		client->driver = NULL;
 		i2c_set_clientdata(client, NULL);
 	}
+
+	acpi_i2c_device_pm_put(client, true);
 	return status;
 }
 
@@ -294,8 +323,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) {
+		acpi_i2c_device_pm_get(client);
 		driver->shutdown(client);
+		acpi_i2c_device_pm_put(client, false);
+	}
 }
 
 #ifdef CONFIG_PM_SLEEP
@@ -1263,6 +1295,16 @@ exit_recovery:
 	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
 	mutex_unlock(&core_lock);
 
+	/*
+	 * For ACPI enumerated controllers we must make sure that the
+	 * controller is powered on before its children. Runtime PM handles
+	 * this for us once we have enabled it for the adapter device.
+	 */
+	if (ACPI_HANDLE(adap->dev.parent)) {
+		pm_runtime_no_callbacks(&adap->dev);
+		pm_runtime_enable(&adap->dev);
+	}
+
 	return 0;
 
 out_list:

drivers/i2c/i2c-core.c void i2c_del_adapter(struct i2c_adapter *adap) · line 1477

Quoted 9 lines
drivers/i2c/i2c-core.c +3 -0
@@ -1427,6 +1469,9 @@ void i2c_del_adapter(struct i2c_adapter *adap)
 		return;
 	}
 
+	if (ACPI_HANDLE(adap->dev.parent))
+		pm_runtime_disable(&adap->dev);
+
 	/* Tell drivers about this removal */
 	mutex_lock(&core_lock);
 	bus_for_each_drv(&i2c_bus_type, NULL, adap,

Thanks!

Signature · 2 lines
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
#4 lore raw

Re: [PATCH v3] i2c: enable runtime PM for I2C adapter devices enumerated from ACPI

In reply to Rafael J. Wysocki
Quoted 17 lines · Rafael J. Wysocki

drivers/i2c/i2c-core.c int i2c_recover_bus(struct i2c_adapter *adap) · line 247

Quoted 9 lines
drivers/i2c/i2c-core.c +9 -0
+static void acpi_i2c_device_pm_get(struct i2c_client *client)
+{
+	struct i2c_adapter *adap = client->adapter;
+
+	/* Make sure the adapter is active */
+	if (ACPI_HANDLE(adap->dev.parent))
+		pm_runtime_get_sync(&adap->dev);
+	if (ACPI_HANDLE(&client->dev))
+		acpi_dev_pm_attach(&client->dev, true);

It would be sufficient to do

	if (ACPI_HANDLE(&client->dev)) {
		pm_runtime_get_sync(&adap->dev);
		acpi_dev_pm_attach(&client->dev, true);
	}
Quoted 5 lines

here (and below), because I don't think the client with an ACPI handle and the parent without one is extremely unlikely (to the point of non-existence actually ;-)). And even if something like that happens, then we only enable runtime PM for the adapter if the parent has an ACPI handle, so it still should be OK.

OK, I'll change that in the next revision.

Quoted 1 line

Apart from this the patch looks good to me.

Thanks!

#5 lore raw

Re: [PATCH v3] i2c: enable runtime PM for I2C adapter devices enumerated from ACPI

In reply to Mika Westerberg

drivers/i2c/i2c-core.c int i2c_recover_bus(struct i2c_adapter *adap) · line 247

Quoted 9 lines · Mika Westerberg
drivers/i2c/i2c-core.c +9 -0
+static void acpi_i2c_device_pm_get(struct i2c_client *client)
+{
+	struct i2c_adapter *adap = client->adapter;
+
+	/* Make sure the adapter is active */
+	if (ACPI_HANDLE(adap->dev.parent))
+		pm_runtime_get_sync(&adap->dev);
+	if (ACPI_HANDLE(&client->dev))
+		acpi_dev_pm_attach(&client->dev, true);

There's a bug here as we call acpi_dev_pm_attach() several times. It doesn't do any harm because acpi_dev_pm_attach() checks if the device is already attached to the ACPI domain or not.

I'll fix this in the next revision as well.

Quoted 1 line

+}

#6 lore raw

PATCH v4 1/1 i2c: enable runtime PM for I2C adapter devices enumerated from ACPI

In reply to Mika Westerberg

The ACPI specification requires the parent device to be powered on before any of its children. It can be only powered off when all the children are already off.

Currently whenever there is no I2C traffic going on, the I2C controller driver can put the device into low power state transparently to its children (the I2C client devices). This violates the ACPI specification because now the parent device is in lower power state than its children.

In order to keep ACPI happy we enable runtime PM for the I2C adapter device if we find out that the I2C controller was in fact an ACPI device. In addition to that we attach the I2C client devices to the ACPI power domain and make sure that they are powered on when the driver ->probe() is called.

Non-ACPI devices are not affected by this change.

This patch is based on the work by Aaron Lu and Lv Zheng.

---
Changes to v3:
* Check only client ACPI_HANDLE().
* Fixed bug where we attach the device to ACPI power domain multiple times.
* Functions are renamed to _get_attach/_put_attach to make it clear that we do attach/detach as well.

 drivers/i2c/i2c-core.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)
drivers/i2c/i2c-core.c +46 -1
index 29d3f04..e289b1d 100644
@@ -236,6 +236,27 @@ int i2c_recover_bus(struct i2c_adapter *adap)
 	return adap->bus_recovery_info->recover_bus(adap);
 }
 
+static void acpi_i2c_device_pm_get_attach(struct i2c_client *client,
+					  bool attach)
+{
+	if (ACPI_HANDLE(&client->dev)) {
+		/* Make sure the adapter is active */
+		pm_runtime_get_sync(&client->adapter->dev);
+		if (attach)
+			acpi_dev_pm_attach(&client->dev, true);
+	}
+}
+
+static void acpi_i2c_device_pm_put_detach(struct i2c_client *client,
+					  bool detach)
+{
+	if (ACPI_HANDLE(&client->dev)) {
+		if (detach)
+			acpi_dev_pm_detach(&client->dev, true);
+		pm_runtime_put(&client->adapter->dev);
+	}
+}
+
 static int i2c_device_probe(struct device *dev)
 {
 	struct i2c_client	*client = i2c_verify_client(dev);
@@ -254,11 +275,15 @@ static int i2c_device_probe(struct device *dev)
 					client->flags & I2C_CLIENT_WAKE);
 	dev_dbg(dev, "probe\n");
 
+	acpi_i2c_device_pm_get_attach(client, true);
+
 	status = driver->probe(client, i2c_match_id(driver->id_table, client));
 	if (status) {
 		client->driver = NULL;
 		i2c_set_clientdata(client, NULL);
 	}
+
+	acpi_i2c_device_pm_put_detach(client, !!status);
 	return status;
 }
 
@@ -271,6 +296,8 @@ static int i2c_device_remove(struct device *dev)
 	if (!client || !dev->driver)
 		return 0;
 
+	acpi_i2c_device_pm_get_attach(client, false);
+
 	driver = to_i2c_driver(dev->driver);
 	if (driver->remove) {
 		dev_dbg(dev, "remove\n");
@@ -283,6 +310,8 @@ static int i2c_device_remove(struct device *dev)
 		client->driver = NULL;
 		i2c_set_clientdata(client, NULL);
 	}
+
+	acpi_i2c_device_pm_put_detach(client, true);
 	return status;
 }
 
@@ -294,8 +323,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) {
+		acpi_i2c_device_pm_get_attach(client, false);
 		driver->shutdown(client);
+		acpi_i2c_device_pm_put_detach(client, false);
+	}
 }
 
 #ifdef CONFIG_PM_SLEEP
@@ -1263,6 +1295,16 @@ exit_recovery:
 	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
 	mutex_unlock(&core_lock);
 
+	/*
+	 * For ACPI enumerated controllers we must make sure that the
+	 * controller is powered on before its children. Runtime PM handles
+	 * this for us once we have enabled it for the adapter device.
+	 */
+	if (ACPI_HANDLE(adap->dev.parent)) {
+		pm_runtime_no_callbacks(&adap->dev);
+		pm_runtime_enable(&adap->dev);
+	}
+
 	return 0;
 
 out_list:
@@ -1427,6 +1469,9 @@ void i2c_del_adapter(struct i2c_adapter *adap)
 		return;
 	}
 
+	if (ACPI_HANDLE(adap->dev.parent))
+		pm_runtime_disable(&adap->dev);
+
 	/* Tell drivers about this removal */
 	mutex_lock(&core_lock);
 	bus_for_each_drv(&i2c_bus_type, NULL, adap,
Signature · 1 line
1.8.4.rc3
Acked-by on v4 1/1
#7 lore raw

Re: [PATCH v4] i2c: enable runtime PM for I2C adapter devices enumerated from ACPI

Quoted 14 lines · Mika Westerberg

The ACPI specification requires the parent device to be powered on before any of its children. It can be only powered off when all the children are already off.

Currently whenever there is no I2C traffic going on, the I2C controller driver can put the device into low power state transparently to its children (the I2C client devices). This violates the ACPI specification because now the parent device is in lower power state than its children.

In order to keep ACPI happy we enable runtime PM for the I2C adapter device if we find out that the I2C controller was in fact an ACPI device. In addition to that we attach the I2C client devices to the ACPI power domain and make sure that they are powered on when the driver ->probe() is called.

Quoted 5 lines

Non-ACPI devices are not affected by this change.

This patch is based on the work by Aaron Lu and Lv Zheng.

Quoted 118 lines

---
Changes to v3:
* Check only client ACPI_HANDLE().
* Fixed bug where we attach the device to ACPI power domain multiple times.
* Functions are renamed to _get_attach/_put_attach to make it clear that we do attach/detach as well.

 drivers/i2c/i2c-core.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)
drivers/i2c/i2c-core.c +46 -1
index 29d3f04..e289b1d 100644
@@ -236,6 +236,27 @@ int i2c_recover_bus(struct i2c_adapter *adap)
 	return adap->bus_recovery_info->recover_bus(adap);
 }
 
+static void acpi_i2c_device_pm_get_attach(struct i2c_client *client,
+					  bool attach)
+{
+	if (ACPI_HANDLE(&client->dev)) {
+		/* Make sure the adapter is active */
+		pm_runtime_get_sync(&client->adapter->dev);
+		if (attach)
+			acpi_dev_pm_attach(&client->dev, true);
+	}
+}
+
+static void acpi_i2c_device_pm_put_detach(struct i2c_client *client,
+					  bool detach)
+{
+	if (ACPI_HANDLE(&client->dev)) {
+		if (detach)
+			acpi_dev_pm_detach(&client->dev, true);
+		pm_runtime_put(&client->adapter->dev);
+	}
+}
+
 static int i2c_device_probe(struct device *dev)
 {
 	struct i2c_client	*client = i2c_verify_client(dev);
@@ -254,11 +275,15 @@ static int i2c_device_probe(struct device *dev)
 					client->flags & I2C_CLIENT_WAKE);
 	dev_dbg(dev, "probe\n");
 
+	acpi_i2c_device_pm_get_attach(client, true);
+
 	status = driver->probe(client, i2c_match_id(driver->id_table, client));
 	if (status) {
 		client->driver = NULL;
 		i2c_set_clientdata(client, NULL);
 	}
+
+	acpi_i2c_device_pm_put_detach(client, !!status);
 	return status;
 }
 
@@ -271,6 +296,8 @@ static int i2c_device_remove(struct device *dev)
 	if (!client || !dev->driver)
 		return 0;
 
+	acpi_i2c_device_pm_get_attach(client, false);
+
 	driver = to_i2c_driver(dev->driver);
 	if (driver->remove) {
 		dev_dbg(dev, "remove\n");
@@ -283,6 +310,8 @@ static int i2c_device_remove(struct device *dev)
 		client->driver = NULL;
 		i2c_set_clientdata(client, NULL);
 	}
+
+	acpi_i2c_device_pm_put_detach(client, true);
 	return status;
 }
 
@@ -294,8 +323,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) {
+		acpi_i2c_device_pm_get_attach(client, false);
 		driver->shutdown(client);
+		acpi_i2c_device_pm_put_detach(client, false);
+	}
 }
 
 #ifdef CONFIG_PM_SLEEP
@@ -1263,6 +1295,16 @@ exit_recovery:
 	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
 	mutex_unlock(&core_lock);
 
+	/*
+	 * For ACPI enumerated controllers we must make sure that the
+	 * controller is powered on before its children. Runtime PM handles
+	 * this for us once we have enabled it for the adapter device.
+	 */
+	if (ACPI_HANDLE(adap->dev.parent)) {
+		pm_runtime_no_callbacks(&adap->dev);
+		pm_runtime_enable(&adap->dev);
+	}
+
 	return 0;
 
 out_list:
@@ -1427,6 +1469,9 @@ void i2c_del_adapter(struct i2c_adapter *adap)
 		return;
 	}
 
+	if (ACPI_HANDLE(adap->dev.parent))
+		pm_runtime_disable(&adap->dev);
+
 	/* Tell drivers about this removal */
 	mutex_lock(&core_lock);
 	bus_for_each_drv(&i2c_bus_type, NULL, adap,
Signature · 2 lines
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
#8 lore raw

Re: [PATCH v4] i2c: enable runtime PM for I2C adapter devices enumerated from ACPI

In reply to Mika Westerberg
Quoted 9 lines · Mika Westerberg

The ACPI specification requires the parent device to be powered on before any of its children. It can be only powered off when all the children are already off.

Currently whenever there is no I2C traffic going on, the I2C controller driver can put the device into low power state transparently to its children (the I2C client devices). This violates the ACPI specification because now the parent device is in lower power state than its children.

Quoted 4 lines

In order to keep ACPI happy we enable runtime PM for the I2C adapter device if we find out that the I2C controller was in fact an ACPI device. In addition to that we attach the I2C client devices to the ACPI power domain and make sure that they are powered on when the driver ->probe() is called.

It looks like Windows actually powers the I2C controller off independently of the I2C client power state. We should probably do the same in Linux even though it is not following what the ACPI spec says (but makes sense with serial buses like I2C and SPI).

Wolfram, please don't apply this patch - we are going to do one more iteration but this time we only attach devices to the ACPI power domain and leave runtime PM alone.

#9 lore raw

Re: [PATCH v4] i2c: enable runtime PM for I2C adapter devices enumerated from ACPI

Quoted 4 lines · Mika Westerberg

It looks like Windows actually powers the I2C controller off independently of the I2C client power state. We should probably do the same in Linux even though it is not following what the ACPI spec says (but makes sense with serial buses like I2C and SPI).

Sounds like it's probably worth going to the effort of getting the spec amended...