Skip to contentSkip to conversation

iio: fix bug with triggers not resuming after sleep

PATCH In review Denis Benato 2 participants 4 messages Aug 7, 2024 – Aug 10, 2024

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

v3 cover iio: fix bug with triggers not resuming after sleep

When a device enters an idle state (for example ASUS RC71L with s2idle) and an iio driver has a trigger attached such as iio-trig-hrtimer, after resuming the device the trigger is not triggering data acquisition.

This patch series solves the problem reliably and is well tested after many cycles and many reboots.

Changelog:
- V2: patch 2:
+ Simplify code
+ Remove unneeded code protections around SET_RUNTIME_PM_OPS()
+ pm_ptr() to let the compiler drop bmi323_core_pm_ops if !CONFIG_PM

- V3: patch 1:
+ Add pf->irq=0 that was accidentally left out

Previous patches obsoleted:
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/

Denis Benato (2):
iio: trigger: allow devices to suspend/resume theirs associated
trigger
iio: bmi323: suspend and resume triggering on relevant pm operations

 drivers/iio/imu/bmi323/bmi323.h      |  1 +
 drivers/iio/imu/bmi323/bmi323_core.c | 23 +++++++++++++++++++++++
 drivers/iio/imu/bmi323/bmi323_i2c.c  |  1 +
 drivers/iio/imu/bmi323/bmi323_spi.c  |  1 +
 drivers/iio/industrialio-trigger.c   | 27 +++++++++++++++++++++++++++
 include/linux/iio/iio.h              | 17 +++++++++++++++++
 6 files changed, 70 insertions(+)
Signature · 1 line
2.46.0
#1 lore raw

PATCH v3 1/2 iio: trigger: allow devices to suspend/resume theirs associated trigger

When a machine enters a sleep state while a trigger is associated to an iio device that trigger is not resumed after exiting the sleep state:
provide iio device drivers a way to suspend and resume
the associated trigger to solve the aforementioned bug.

Each iio driver supporting external triggers is expected to call iio_device_suspend_triggering before suspending,
and iio_device_resume_triggering upon resuming.

---
 drivers/iio/industrialio-trigger.c | 27 +++++++++++++++++++++++++++
 include/linux/iio/iio.h            | 17 +++++++++++++++++
 2 files changed, 44 insertions(+)
drivers/iio/industrialio-trigger.c +27 -0
index 2e84776f4fbd..54416a384232 100644
@@ -347,6 +347,7 @@ int iio_trigger_detach_poll_func(struct iio_trigger *trig,
 	iio_trigger_put_irq(trig, pf->irq);
 	free_irq(pf->irq, pf);
 	module_put(iio_dev_opaque->driver_module);
+	pf->irq = 0;
 
 	return ret;
 }
@@ -770,3 +771,29 @@ void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
 	if (indio_dev->trig)
 		iio_trigger_put(indio_dev->trig);
 }
+
+int iio_device_suspend_triggering(struct iio_dev *indio_dev)
+{
+	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
+
+	guard(mutex)(&iio_dev_opaque->mlock);
+
+	if ((indio_dev->pollfunc) && (indio_dev->pollfunc->irq > 0))
+		disable_irq(indio_dev->pollfunc->irq);
+
+	return 0;
+}
+EXPORT_SYMBOL(iio_device_suspend_triggering);
+
+int iio_device_resume_triggering(struct iio_dev *indio_dev)
+{
+	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
+
+	guard(mutex)(&iio_dev_opaque->mlock);
+
+	if ((indio_dev->pollfunc) && (indio_dev->pollfunc->irq > 0))
+		enable_irq(indio_dev->pollfunc->irq);
+
+	return 0;
+}
+EXPORT_SYMBOL(iio_device_resume_triggering);
include/linux/iio/iio.h +17 -0
index f6c0499853bb..c66c784d31e4 100644
@@ -810,6 +810,23 @@ static inline struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
 }
 #endif
 
+/**
+ * iio_device_suspend_triggering() - suspend trigger attached to an iio_dev
+ * @indio_dev: iio_dev associated with the device that will have triggers suspended
+ *
+ * Return 0 if successful, negative otherwise
+ **/
+int iio_device_suspend_triggering(struct iio_dev *indio_dev);
+
+/**
+ * iio_device_resume_triggering() - resume trigger attached to an iio_dev
+ *	that was previously suspended with iio_device_suspend_triggering()
+ * @indio_dev: iio_dev associated with the device that will have triggers resumed
+ *
+ * Return 0 if successful, negative otherwise
+ **/
+int iio_device_resume_triggering(struct iio_dev *indio_dev);
+
 #ifdef CONFIG_ACPI
 bool iio_read_acpi_mount_matrix(struct device *dev,
 				struct iio_mount_matrix *orientation,
Signature · 1 line
2.46.0
#2 lore raw

PATCH v3 2/2 iio: bmi323: suspend and resume triggering on relevant pm operations

In reply to Denis Benato

Prevent triggers from stop working after the device has entered sleep:
use iio_device_suspend_triggering and iio_device_resume_triggering helpers.

---
 drivers/iio/imu/bmi323/bmi323.h      |  1 +
 drivers/iio/imu/bmi323/bmi323_core.c | 23 +++++++++++++++++++++++
 drivers/iio/imu/bmi323/bmi323_i2c.c  |  1 +
 drivers/iio/imu/bmi323/bmi323_spi.c  |  1 +
 4 files changed, 26 insertions(+)
drivers/iio/imu/bmi323/bmi323.h +1 -0
index dff126d41658..209bccb1f335 100644
@@ -205,5 +205,6 @@
 struct device;
 int bmi323_core_probe(struct device *dev);
 extern const struct regmap_config bmi323_regmap_config;
+extern const struct dev_pm_ops bmi323_core_pm_ops;
 
 #endif
drivers/iio/imu/bmi323/bmi323_core.c +23 -0
index d708d1fe3e42..4b2b211a3e88 100644
@@ -2121,6 +2121,29 @@ int bmi323_core_probe(struct device *dev)
 }
 EXPORT_SYMBOL_NS_GPL(bmi323_core_probe, IIO_BMI323);
 
+#if defined(CONFIG_PM)
+static int bmi323_core_runtime_suspend(struct device *dev)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+
+	return iio_device_suspend_triggering(indio_dev);
+}
+
+static int bmi323_core_runtime_resume(struct device *dev)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+
+	return iio_device_resume_triggering(indio_dev);
+}
+
+#endif
+
+const struct dev_pm_ops bmi323_core_pm_ops = {
+	SET_RUNTIME_PM_OPS(bmi323_core_runtime_suspend,
+			   bmi323_core_runtime_resume, NULL)
+};
+EXPORT_SYMBOL_NS_GPL(bmi323_core_pm_ops, IIO_BMI323);
+
 MODULE_DESCRIPTION("Bosch BMI323 IMU driver");
 MODULE_AUTHOR("Jagath Jog J <[email protected]>");
 MODULE_LICENSE("GPL");
drivers/iio/imu/bmi323/bmi323_i2c.c +1 -0
index 3298e027c97e..0ba5d69d8329 100644
@@ -128,6 +128,7 @@ MODULE_DEVICE_TABLE(of, bmi323_of_i2c_match);
 static struct i2c_driver bmi323_i2c_driver = {
 	.driver = {
 		.name = "bmi323",
+		.pm = pm_ptr(&bmi323_core_pm_ops),
 		.of_match_table = bmi323_of_i2c_match,
 		.acpi_match_table = bmi323_acpi_match,
 	},
drivers/iio/imu/bmi323/bmi323_spi.c +1 -0
index 571e1cd7ce72..9de3ade78d71 100644
@@ -79,6 +79,7 @@ MODULE_DEVICE_TABLE(of, bmi323_of_spi_match);
 static struct spi_driver bmi323_spi_driver = {
 	.driver = {
 		.name = "bmi323",
+		.pm = pm_ptr(&bmi323_core_pm_ops),
 		.of_match_table = bmi323_of_spi_match,
 	},
 	.probe = bmi323_spi_probe,
Signature · 1 line
2.46.0
maintainer
#3 lore raw
In reply to Denis Benato
Quoted 9 lines · On Wed, 7 Aug 2024 20:56:17 +0200 Denis Benato <[email protected]>…

When a device enters an idle state (for example ASUS RC71L with s2idle) and an iio driver has a trigger attached such as iio-trig-hrtimer, after resuming the device the trigger is not triggering data acquisition.

This patch series solves the problem reliably and is well tested after many cycles and many reboots.

Quoted 8 lines

Changelog:
- V2: patch 2:
+ Simplify code
+ Remove unneeded code protections around SET_RUNTIME_PM_OPS()
+ pm_ptr() to let the compiler drop bmi323_core_pm_ops if !CONFIG_PM

- V3: patch 1:
+ Add pf->irq=0 that was accidentally left out

Dropped v2 and picked up v3.

Thanks,

Jonathan

Quoted 18 lines

Previous patches obsoleted:
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/

Denis Benato (2):
iio: trigger: allow devices to suspend/resume theirs associated
trigger
iio: bmi323: suspend and resume triggering on relevant pm operations

 drivers/iio/imu/bmi323/bmi323.h      |  1 +
 drivers/iio/imu/bmi323/bmi323_core.c | 23 +++++++++++++++++++++++
 drivers/iio/imu/bmi323/bmi323_i2c.c  |  1 +
 drivers/iio/imu/bmi323/bmi323_spi.c  |  1 +
 drivers/iio/industrialio-trigger.c   | 27 +++++++++++++++++++++++++++
 include/linux/iio/iio.h              | 17 +++++++++++++++++
 6 files changed, 70 insertions(+)