[PATCH] iio: imu: lsm6dsx: Add shutdown callback support for I3C interface

Manikanta Guntupalli posted 1 patch 2 months, 2 weeks ago
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      |  1 +
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c |  3 ++-
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_i3c.c  | 14 ++++++++++++++
3 files changed, 17 insertions(+), 1 deletion(-)
[PATCH] iio: imu: lsm6dsx: Add shutdown callback support for I3C interface
Posted by Manikanta Guntupalli 2 months, 2 weeks ago
Add a shutdown handler for the ST LSM6DSx I3C driver to perform a hardware
reset during system shutdown. This ensures the sensor is placed in a
well-defined reset state, preventing issues during subsequent reboots,
such as kexec, where the device may fail to respond correctly during
enumeration.

To support this, the previously static st_lsm6dsx_reset_device() function
is now exported via EXPORT_SYMBOL_NS() under the IIO_LSM6DSX namespace,
allowing it to be invoked from the I3C-specific driver.

Signed-off-by: Manikanta Guntupalli <manikanta.guntupalli@amd.com>
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      |  1 +
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c |  3 ++-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_i3c.c  | 14 ++++++++++++++
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
index c225b246c8a5..42c0dcfbad49 100644
--- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
+++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
@@ -466,6 +466,7 @@ extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
 
 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
 		     struct regmap *regmap);
+int st_lsm6dsx_reset_device(struct st_lsm6dsx_hw *hw);
 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
 				 bool enable);
 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
index c65ad49829e7..929b30985d41 100644
--- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
+++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
@@ -2267,7 +2267,7 @@ static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw *hw)
 	return 0;
 }
 
-static int st_lsm6dsx_reset_device(struct st_lsm6dsx_hw *hw)
+int st_lsm6dsx_reset_device(struct st_lsm6dsx_hw *hw)
 {
 	const struct st_lsm6dsx_reg *reg;
 	int err;
@@ -2302,6 +2302,7 @@ static int st_lsm6dsx_reset_device(struct st_lsm6dsx_hw *hw)
 
 	return 0;
 }
+EXPORT_SYMBOL_NS(st_lsm6dsx_reset_device, "IIO_LSM6DSX");
 
 static int st_lsm6dsx_init_device(struct st_lsm6dsx_hw *hw)
 {
diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_i3c.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_i3c.c
index cb5c5d7e1f3d..f3d9cdd5a743 100644
--- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_i3c.c
+++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_i3c.c
@@ -41,10 +41,24 @@ static int st_lsm6dsx_i3c_probe(struct i3c_device *i3cdev)
 	return st_lsm6dsx_probe(dev, 0, (uintptr_t)id->data, regmap);
 }
 
+static void st_lsm6dsx_i3c_shutdown(struct device *dev)
+{
+	struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
+
+	/*
+	 * Perform device reset to ensure the sensor is in a known
+	 * good state for subsequent re-initialization or power cycles.
+	 * This addresses issues where the sensor might not enumerate
+	 * correctly after a warm reboot (e.g., kexec).
+	 */
+	st_lsm6dsx_reset_device(hw);
+}
+
 static struct i3c_driver st_lsm6dsx_driver = {
 	.driver = {
 		.name = "st_lsm6dsx_i3c",
 		.pm = pm_sleep_ptr(&st_lsm6dsx_pm_ops),
+		.shutdown = st_lsm6dsx_i3c_shutdown,
 	},
 	.probe = st_lsm6dsx_i3c_probe,
 	.id_table = st_lsm6dsx_i3c_ids,
-- 
2.34.1
Re: [PATCH] iio: imu: lsm6dsx: Add shutdown callback support for I3C interface
Posted by Andy Shevchenko 2 months, 2 weeks ago
On Mon, Jul 21, 2025 at 04:37:41PM +0530, Manikanta Guntupalli wrote:
> Add a shutdown handler for the ST LSM6DSx I3C driver to perform a hardware
> reset during system shutdown. This ensures the sensor is placed in a
> well-defined reset state, preventing issues during subsequent reboots,
> such as kexec, where the device may fail to respond correctly during
> enumeration.

Do you imply that tons of device drivers missing this? I don't think we have
even 5% of the drivers implementing the feature.

> To support this, the previously static st_lsm6dsx_reset_device() function
> is now exported via EXPORT_SYMBOL_NS() under the IIO_LSM6DSX namespace,
> allowing it to be invoked from the I3C-specific driver.

Why system suspend callback can't do this?

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH] iio: imu: lsm6dsx: Add shutdown callback support for I3C interface
Posted by David Lechner 2 months, 2 weeks ago
On 7/21/25 6:38 AM, Andy Shevchenko wrote:
> On Mon, Jul 21, 2025 at 04:37:41PM +0530, Manikanta Guntupalli wrote:
>> Add a shutdown handler for the ST LSM6DSx I3C driver to perform a hardware
>> reset during system shutdown. This ensures the sensor is placed in a
>> well-defined reset state, preventing issues during subsequent reboots,
>> such as kexec, where the device may fail to respond correctly during
>> enumeration.
> 
> Do you imply that tons of device drivers missing this? I don't think we have
> even 5% of the drivers implementing the feature.
> 
In the IIO drivers I've worked on, we always do reset in the probe()
function. The shutdown() function might not run, e.g. if the board
loses power, so it doesn't fix 100% of the cases.
Re: [PATCH] iio: imu: lsm6dsx: Add shutdown callback support for I3C interface
Posted by Andy Shevchenko 2 months, 2 weeks ago
On Mon, Jul 21, 2025 at 02:38:42PM +0300, Andy Shevchenko wrote:
> On Mon, Jul 21, 2025 at 04:37:41PM +0530, Manikanta Guntupalli wrote:
> > Add a shutdown handler for the ST LSM6DSx I3C driver to perform a hardware
> > reset during system shutdown. This ensures the sensor is placed in a
> > well-defined reset state, preventing issues during subsequent reboots,
> > such as kexec, where the device may fail to respond correctly during
> > enumeration.
> 
> Do you imply that tons of device drivers missing this? I don't think we have
> even 5% of the drivers implementing the feature.
> 
> > To support this, the previously static st_lsm6dsx_reset_device() function
> > is now exported via EXPORT_SYMBOL_NS() under the IIO_LSM6DSX namespace,
> > allowing it to be invoked from the I3C-specific driver.
> 
> Why system suspend callback can't do this?

Ah, and why only I3C is important? Doesn't I2C or SPI also broken in this sense?

-- 
With Best Regards,
Andy Shevchenko