[PATCH] iio: imu: inv_mpu6050: use guard(mutex) and scoped_guard
Aline Ayumi Nakazawa
aline.nakazawa em ime.usp.br
Qua Abr 29 20:40:19 -03 2026
From: Aline Nakazawa <aline.nakazawa em ime.usp.br>
Replace manual mutex_lock/unlock calls with guard() and scoped_guard()
macros.
Signed-off-by: Aline Nakazawa <aline.nakazawa em ime.usp.br>
---
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c | 193 ++++++++++-----------
1 file changed, 88 insertions(+), 105 deletions(-)
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
index 5796896d54cd..74caf2a95154 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
@@ -20,6 +20,7 @@
#include <linux/pm.h>
#include <linux/pm_runtime.h>
#include <linux/property.h>
+#include <linux/cleanup.h>
#include <linux/iio/common/inv_sensors_timestamp.h>
#include <linux/iio/iio.h>
@@ -756,25 +757,23 @@ inv_mpu6050_read_raw(struct iio_dev *indio_dev,
case IIO_CHAN_INFO_RAW:
if (!iio_device_claim_direct(indio_dev))
return -EBUSY;
- mutex_lock(&st->lock);
- ret = inv_mpu6050_read_channel_data(indio_dev, chan, val);
- mutex_unlock(&st->lock);
+ scoped_guard(mutex, &st->lock) {
+ ret = inv_mpu6050_read_channel_data(indio_dev, chan, val);
+ }
iio_device_release_direct(indio_dev);
return ret;
case IIO_CHAN_INFO_SCALE:
switch (chan->type) {
case IIO_ANGL_VEL:
- mutex_lock(&st->lock);
+ guard(mutex)(&st->lock);
*val = 0;
*val2 = gyro_scale_6050[st->chip_config.fsr];
- mutex_unlock(&st->lock);
return IIO_VAL_INT_PLUS_NANO;
case IIO_ACCEL:
- mutex_lock(&st->lock);
+ guard(mutex)(&st->lock);
*val = 0;
*val2 = accel_scale[st->chip_config.accl_fs];
- mutex_unlock(&st->lock);
return IIO_VAL_INT_PLUS_MICRO;
case IIO_TEMP:
@@ -797,18 +796,13 @@ inv_mpu6050_read_raw(struct iio_dev *indio_dev,
case IIO_CHAN_INFO_CALIBBIAS:
switch (chan->type) {
case IIO_ANGL_VEL:
- mutex_lock(&st->lock);
- ret = inv_mpu6050_sensor_show(st, st->reg->gyro_offset,
+ guard(mutex)(&st->lock);
+ return inv_mpu6050_sensor_show(st, st->reg->gyro_offset,
chan->channel2, val);
- mutex_unlock(&st->lock);
- return ret;
case IIO_ACCEL:
- mutex_lock(&st->lock);
- ret = inv_mpu6050_sensor_show(st, st->reg->accl_offset,
+ guard(mutex)(&st->lock);
+ return inv_mpu6050_sensor_show(st, st->reg->accl_offset,
chan->channel2, val);
- mutex_unlock(&st->lock);
- return ret;
-
default:
return -EINVAL;
}
@@ -896,50 +890,49 @@ static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
if (!iio_device_claim_direct(indio_dev))
return -EBUSY;
- mutex_lock(&st->lock);
- result = pm_runtime_resume_and_get(pdev);
- if (result)
- goto error_write_raw_unlock;
-
- switch (mask) {
- case IIO_CHAN_INFO_SCALE:
- switch (chan->type) {
- case IIO_ANGL_VEL:
- result = inv_mpu6050_write_gyro_scale(st, val, val2);
- break;
- case IIO_ACCEL:
- result = inv_mpu6050_write_accel_scale(st, val, val2);
- break;
- default:
- result = -EINVAL;
+ scoped_guard(mutex, &st->lock) {
+ result = pm_runtime_resume_and_get(pdev);
+ if (result)
break;
- }
- break;
- case IIO_CHAN_INFO_CALIBBIAS:
- switch (chan->type) {
- case IIO_ANGL_VEL:
- result = inv_mpu6050_sensor_set(st,
- st->reg->gyro_offset,
- chan->channel2, val);
+
+ switch (mask) {
+ case IIO_CHAN_INFO_SCALE:
+ switch (chan->type) {
+ case IIO_ANGL_VEL:
+ result = inv_mpu6050_write_gyro_scale(st, val, val2);
+ break;
+ case IIO_ACCEL:
+ result = inv_mpu6050_write_accel_scale(st, val, val2);
+ break;
+ default:
+ result = -EINVAL;
+ break;
+ }
break;
- case IIO_ACCEL:
- result = inv_mpu6050_sensor_set(st,
- st->reg->accl_offset,
- chan->channel2, val);
+ case IIO_CHAN_INFO_CALIBBIAS:
+ switch (chan->type) {
+ case IIO_ANGL_VEL:
+ result = inv_mpu6050_sensor_set(st,
+ st->reg->gyro_offset,
+ chan->channel2, val);
+ break;
+ case IIO_ACCEL:
+ result = inv_mpu6050_sensor_set(st,
+ st->reg->accl_offset,
+ chan->channel2, val);
+ break;
+ default:
+ result = -EINVAL;
+ break;
+ }
break;
default:
result = -EINVAL;
break;
}
- break;
- default:
- result = -EINVAL;
- break;
- }
- pm_runtime_put_autosuspend(pdev);
-error_write_raw_unlock:
- mutex_unlock(&st->lock);
+ pm_runtime_put_autosuspend(pdev);
+ }
iio_device_release_direct(indio_dev);
return result;
@@ -1315,49 +1308,46 @@ inv_mpu6050_fifo_rate_store(struct device *dev, struct device_attribute *attr,
fifo_rate = INV_MPU6050_DIVIDER_TO_FIFO_RATE(d);
fifo_period = NSEC_PER_SEC / fifo_rate;
- mutex_lock(&st->lock);
- if (d == st->chip_config.divider) {
- result = 0;
- goto fifo_rate_fail_unlock;
- }
-
- fifo_on = st->chip_config.accl_fifo_enable ||
- st->chip_config.gyro_fifo_enable ||
- st->chip_config.magn_fifo_enable;
- result = inv_sensors_timestamp_update_odr(&st->timestamp, fifo_period, fifo_on);
- if (result)
- goto fifo_rate_fail_unlock;
+ scoped_guard(mutex, &st->lock) {
+ if (d == st->chip_config.divider)
+ return 0;
- result = pm_runtime_resume_and_get(pdev);
- if (result)
- goto fifo_rate_fail_unlock;
+ fifo_on = st->chip_config.accl_fifo_enable ||
+ st->chip_config.gyro_fifo_enable ||
+ st->chip_config.magn_fifo_enable;
+ result = inv_sensors_timestamp_update_odr(&st->timestamp, fifo_period, fifo_on);
+ if (result)
+ return result;
- result = regmap_write(st->map, st->reg->sample_rate_div, d);
- if (result)
- goto fifo_rate_fail_power_off;
- st->chip_config.divider = d;
+ result = pm_runtime_resume_and_get(pdev);
+ if (result)
+ return result;
- result = inv_mpu6050_set_lpf(st, fifo_rate);
- if (result)
- goto fifo_rate_fail_power_off;
+ result = regmap_write(st->map, st->reg->sample_rate_div, d);
+ if (!result) {
+ st->chip_config.divider = d;
- /* update rate for magn, noop if not present in chip */
- result = inv_mpu_magn_set_rate(st, fifo_rate);
- if (result)
- goto fifo_rate_fail_power_off;
+ result = inv_mpu6050_set_lpf(st, fifo_rate);
+ if (!result) {
+ /* update rate for magn, noop if not present in chip */
+ result = inv_mpu_magn_set_rate(st, fifo_rate);
+ }
- /* update wom threshold since roc is dependent on sampling frequency */
- result = inv_mpu6050_set_wom_threshold(st, st->chip_config.roc_threshold,
- INV_MPU6050_FREQ_DIVIDER(st));
- if (result)
- goto fifo_rate_fail_power_off;
+ if (!result) {
+ /*
+ * update wom threshold since roc is dependent on
+ * sampling frequency
+ */
+ result = inv_mpu6050_set_wom_threshold(st,
+ st->chip_config.roc_threshold,
+ INV_MPU6050_FREQ_DIVIDER(st));
+ }
+ }
-fifo_rate_fail_power_off:
- pm_runtime_put_autosuspend(pdev);
-fifo_rate_fail_unlock:
- mutex_unlock(&st->lock);
- if (result)
- return result;
+ pm_runtime_put_autosuspend(pdev);
+ if (result)
+ return result;
+ }
return count;
}
@@ -1372,9 +1362,9 @@ inv_fifo_rate_show(struct device *dev, struct device_attribute *attr,
struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
unsigned fifo_rate;
- mutex_lock(&st->lock);
- fifo_rate = INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
- mutex_unlock(&st->lock);
+ scoped_guard(mutex, &st->lock) {
+ fifo_rate = INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
+ }
return sysfs_emit(buf, "%u\n", fifo_rate);
}
@@ -1718,16 +1708,12 @@ static int inv_mpu6050_reg_access(struct iio_dev *indio_dev,
unsigned int *readval)
{
struct inv_mpu6050_state *st = iio_priv(indio_dev);
- int ret;
- mutex_lock(&st->lock);
+ guard(mutex)(&st->lock);
if (readval)
- ret = regmap_read(st->map, reg, readval);
- else
- ret = regmap_write(st->map, reg, writeval);
- mutex_unlock(&st->lock);
+ return regmap_read(st->map, reg, readval);
- return ret;
+ return regmap_write(st->map, reg, writeval);
}
static const struct iio_info mpu_info = {
@@ -2227,23 +2213,20 @@ static int inv_mpu_runtime_suspend(struct device *dev)
unsigned int sensors;
int ret;
- mutex_lock(&st->lock);
+ guard(mutex)(&st->lock);
sensors = INV_MPU6050_SENSOR_ACCL | INV_MPU6050_SENSOR_GYRO |
INV_MPU6050_SENSOR_TEMP | INV_MPU6050_SENSOR_MAGN |
INV_MPU6050_SENSOR_WOM;
ret = inv_mpu6050_switch_engine(st, false, sensors);
if (ret)
- goto out_unlock;
+ return ret;
ret = inv_mpu6050_set_power_itg(st, false);
if (ret)
- goto out_unlock;
+ return ret;
inv_mpu_core_disable_regulator_vddio(st);
-
-out_unlock:
- mutex_unlock(&st->lock);
return ret;
}
--
2.51.0
Mais detalhes sobre a lista de discussão kernel