[PATCH v2 3/4] iio: gyro: mpu3050: Move iio_device_register() to correct location

Ethan Tidmore posted 4 patches 1 month, 1 week ago
[PATCH v2 3/4] iio: gyro: mpu3050: Move iio_device_register() to correct location
Posted by Ethan Tidmore 1 month, 1 week ago
iio_device_register() should be at the end of the probe function to
prevent race conditions.

Place iio_device_register() at end of probe function and place
iio_device_unregister() accordingly.

Fixes: 3904b28efb2c7 ("iio: gyro: Add driver for the MPU-3050 gyroscope")
Suggested-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Ethan Tidmore <ethantidmore06@gmail.com>
---
 drivers/iio/gyro/mpu3050-core.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/drivers/iio/gyro/mpu3050-core.c b/drivers/iio/gyro/mpu3050-core.c
index b6e05afbe512..73dc7ea1f075 100644
--- a/drivers/iio/gyro/mpu3050-core.c
+++ b/drivers/iio/gyro/mpu3050-core.c
@@ -1218,12 +1218,6 @@ int mpu3050_common_probe(struct device *dev,
 		goto err_power_down;
 	}
 
-	ret = iio_device_register(indio_dev);
-	if (ret) {
-		dev_err(dev, "device register failed\n");
-		goto err_cleanup_buffer;
-	}
-
 	dev_set_drvdata(dev, indio_dev);
 
 	/* Check if we have an assigned IRQ to use as trigger */
@@ -1231,6 +1225,7 @@ int mpu3050_common_probe(struct device *dev,
 		ret = mpu3050_trigger_probe(indio_dev, irq);
 		if (ret)
 			dev_err(dev, "failed to register trigger\n");
+
 	}
 
 	/* Enable runtime PM */
@@ -1246,9 +1241,20 @@ int mpu3050_common_probe(struct device *dev,
 	pm_runtime_use_autosuspend(dev);
 	pm_runtime_put(dev);
 
+	ret = iio_device_register(indio_dev);
+	if (ret) {
+		dev_err(dev, "device register failed\n");
+		goto err_iio_device_register;
+	}
+
 	return 0;
 
-err_cleanup_buffer:
+err_iio_device_register:
+	pm_runtime_get_sync(dev);
+	pm_runtime_put_noidle(dev);
+	pm_runtime_disable(dev);
+	if (irq)
+		free_irq(mpu3050->irq, mpu3050->trig);
 	iio_triggered_buffer_cleanup(indio_dev);
 err_power_down:
 	mpu3050_power_down(mpu3050);
@@ -1261,13 +1267,13 @@ void mpu3050_common_remove(struct device *dev)
 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
 	struct mpu3050 *mpu3050 = iio_priv(indio_dev);
 
+	iio_device_unregister(indio_dev);
 	pm_runtime_get_sync(dev);
 	pm_runtime_put_noidle(dev);
 	pm_runtime_disable(dev);
 	iio_triggered_buffer_cleanup(indio_dev);
 	if (mpu3050->irq)
 		free_irq(mpu3050->irq, mpu3050->trig);
-	iio_device_unregister(indio_dev);
 	mpu3050_power_down(mpu3050);
 }
 
-- 
2.53.0
Re: [PATCH v2 3/4] iio: gyro: mpu3050: Move iio_device_register() to correct location
Posted by Andy Shevchenko 1 month, 1 week ago
On Mon, Feb 23, 2026 at 08:35:10PM -0600, Ethan Tidmore wrote:
> iio_device_register() should be at the end of the probe function to
> prevent race conditions.
> 
> Place iio_device_register() at end of probe function and place

at the end

> iio_device_unregister() accordingly.

...

> @@ -1231,6 +1225,7 @@ int mpu3050_common_probe(struct device *dev,
>  		ret = mpu3050_trigger_probe(indio_dev, irq);
>  		if (ret)
>  			dev_err(dev, "failed to register trigger\n");
> +
>  	}
>  
>  	/* Enable runtime PM */

Stray change.

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v2 3/4] iio: gyro: mpu3050: Move iio_device_register() to correct location
Posted by Linus Walleij 1 month, 1 week ago
On Tue, Feb 24, 2026 at 3:35 AM Ethan Tidmore <ethantidmore06@gmail.com> wrote:

> iio_device_register() should be at the end of the probe function to
> prevent race conditions.
>
> Place iio_device_register() at end of probe function and place
> iio_device_unregister() accordingly.
>
> Fixes: 3904b28efb2c7 ("iio: gyro: Add driver for the MPU-3050 gyroscope")
> Suggested-by: Jonathan Cameron <jic23@kernel.org>
> Signed-off-by: Ethan Tidmore <ethantidmore06@gmail.com>

Reviewed-by: Linus Walleij <linusw@kernel.org>

Yours,
Linus Walleij