Skip to content

Commit 661facb

Browse files
sknseanjic23
authored andcommitted
iio: imu: inv_icm42600: use guard() to release mutexes
Replace explicit mutex_lock() and mutex_unlock() with the guard() macro for cleaner and safer mutex handling. Signed-off-by: Sean Nyekjaer <sean@geanix.com> Reviewed-by: David Lechner <dlechner@baylibre.com> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent ee8fc40 commit 661facb

4 files changed

Lines changed: 56 additions & 96 deletions

File tree

drivers/iio/imu/inv_icm42600/inv_icm42600_accel.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -561,11 +561,10 @@ static int inv_icm42600_accel_write_scale(struct iio_dev *indio_dev,
561561
conf.fs = idx / 2;
562562

563563
pm_runtime_get_sync(dev);
564-
mutex_lock(&st->lock);
565564

566-
ret = inv_icm42600_set_accel_conf(st, &conf, NULL);
565+
scoped_guard(mutex, &st->lock)
566+
ret = inv_icm42600_set_accel_conf(st, &conf, NULL);
567567

568-
mutex_unlock(&st->lock);
569568
pm_runtime_put_autosuspend(dev);
570569

571570
return ret;
@@ -986,16 +985,11 @@ static int inv_icm42600_accel_hwfifo_set_watermark(struct iio_dev *indio_dev,
986985
unsigned int val)
987986
{
988987
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
989-
int ret;
990988

991-
mutex_lock(&st->lock);
989+
guard(mutex)(&st->lock);
992990

993991
st->fifo.watermark.accel = val;
994-
ret = inv_icm42600_buffer_update_watermark(st);
995-
996-
mutex_unlock(&st->lock);
997-
998-
return ret;
992+
return inv_icm42600_buffer_update_watermark(st);
999993
}
1000994

1001995
static int inv_icm42600_accel_hwfifo_flush(struct iio_dev *indio_dev,
@@ -1007,15 +1001,13 @@ static int inv_icm42600_accel_hwfifo_flush(struct iio_dev *indio_dev,
10071001
if (count == 0)
10081002
return 0;
10091003

1010-
mutex_lock(&st->lock);
1004+
guard(mutex)(&st->lock);
10111005

10121006
ret = inv_icm42600_buffer_hwfifo_flush(st, count);
1013-
if (!ret)
1014-
ret = st->fifo.nb.accel;
1015-
1016-
mutex_unlock(&st->lock);
1007+
if (ret)
1008+
return ret;
10171009

1018-
return ret;
1010+
return st->fifo.nb.accel;
10191011
}
10201012

10211013
static int inv_icm42600_accel_read_event_config(struct iio_dev *indio_dev,

drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,8 @@ static int inv_icm42600_buffer_preenable(struct iio_dev *indio_dev)
283283

284284
pm_runtime_get_sync(dev);
285285

286-
mutex_lock(&st->lock);
286+
guard(mutex)(&st->lock);
287287
inv_sensors_timestamp_reset(ts);
288-
mutex_unlock(&st->lock);
289288

290289
return 0;
291290
}
@@ -299,82 +298,74 @@ static int inv_icm42600_buffer_postenable(struct iio_dev *indio_dev)
299298
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
300299
int ret;
301300

302-
mutex_lock(&st->lock);
301+
guard(mutex)(&st->lock);
303302

304-
/* exit if FIFO is already on */
305303
if (st->fifo.on) {
306-
ret = 0;
307-
goto out_on;
304+
st->fifo.on++;
305+
return 0;
308306
}
309307

310308
/* set FIFO threshold interrupt */
311309
ret = regmap_set_bits(st->map, INV_ICM42600_REG_INT_SOURCE0,
312310
INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN);
313311
if (ret)
314-
goto out_unlock;
312+
return ret;
315313

316314
/* flush FIFO data */
317315
ret = regmap_write(st->map, INV_ICM42600_REG_SIGNAL_PATH_RESET,
318316
INV_ICM42600_SIGNAL_PATH_RESET_FIFO_FLUSH);
319317
if (ret)
320-
goto out_unlock;
318+
return ret;
321319

322320
/* set FIFO in streaming mode */
323321
ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
324322
INV_ICM42600_FIFO_CONFIG_STREAM);
325323
if (ret)
326-
goto out_unlock;
324+
return ret;
327325

328326
/* workaround: first read of FIFO count after reset is always 0 */
329327
ret = regmap_bulk_read(st->map, INV_ICM42600_REG_FIFO_COUNT, st->buffer, 2);
330328
if (ret)
331-
goto out_unlock;
329+
return ret;
332330

333-
out_on:
334-
/* increase FIFO on counter */
335331
st->fifo.on++;
336-
out_unlock:
337-
mutex_unlock(&st->lock);
338-
return ret;
332+
333+
return 0;
339334
}
340335

341336
static int inv_icm42600_buffer_predisable(struct iio_dev *indio_dev)
342337
{
343338
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
344339
int ret;
345340

346-
mutex_lock(&st->lock);
341+
guard(mutex)(&st->lock);
347342

348-
/* exit if there are several sensors using the FIFO */
349343
if (st->fifo.on > 1) {
350-
ret = 0;
351-
goto out_off;
344+
st->fifo.on--;
345+
return 0;
352346
}
353347

354348
/* set FIFO in bypass mode */
355349
ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
356350
INV_ICM42600_FIFO_CONFIG_BYPASS);
357351
if (ret)
358-
goto out_unlock;
352+
return ret;
359353

360354
/* flush FIFO data */
361355
ret = regmap_write(st->map, INV_ICM42600_REG_SIGNAL_PATH_RESET,
362356
INV_ICM42600_SIGNAL_PATH_RESET_FIFO_FLUSH);
363357
if (ret)
364-
goto out_unlock;
358+
return ret;
365359

366360
/* disable FIFO threshold interrupt */
367361
ret = regmap_clear_bits(st->map, INV_ICM42600_REG_INT_SOURCE0,
368362
INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN);
369363
if (ret)
370-
goto out_unlock;
364+
return ret;
371365

372-
out_off:
373-
/* decrease FIFO on counter */
374366
st->fifo.on--;
375-
out_unlock:
376-
mutex_unlock(&st->lock);
377-
return ret;
367+
368+
return 0;
378369
}
379370

380371
static int inv_icm42600_buffer_postdisable(struct iio_dev *indio_dev)

drivers/iio/imu/inv_icm42600/inv_icm42600_core.c

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -439,18 +439,13 @@ int inv_icm42600_debugfs_reg(struct iio_dev *indio_dev, unsigned int reg,
439439
unsigned int writeval, unsigned int *readval)
440440
{
441441
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
442-
int ret;
443442

444-
mutex_lock(&st->lock);
443+
guard(mutex)(&st->lock);
445444

446445
if (readval)
447-
ret = regmap_read(st->map, reg, readval);
448-
else
449-
ret = regmap_write(st->map, reg, writeval);
450-
451-
mutex_unlock(&st->lock);
446+
return regmap_read(st->map, reg, readval);
452447

453-
return ret;
448+
return regmap_write(st->map, reg, writeval);
454449
}
455450

456451
static int inv_icm42600_set_conf(struct inv_icm42600_state *st,
@@ -820,22 +815,22 @@ static int inv_icm42600_suspend(struct device *dev)
820815
struct device *accel_dev;
821816
bool wakeup;
822817
int accel_conf;
823-
int ret = 0;
818+
int ret;
824819

825-
mutex_lock(&st->lock);
820+
guard(mutex)(&st->lock);
826821

827822
st->suspended.gyro = st->conf.gyro.mode;
828823
st->suspended.accel = st->conf.accel.mode;
829824
st->suspended.temp = st->conf.temp_en;
830825
if (pm_runtime_suspended(dev))
831-
goto out_unlock;
826+
return 0;
832827

833828
/* disable FIFO data streaming */
834829
if (st->fifo.on) {
835830
ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
836831
INV_ICM42600_FIFO_CONFIG_BYPASS);
837832
if (ret)
838-
goto out_unlock;
833+
return ret;
839834
}
840835

841836
/* keep chip on and wake-up capable if APEX and wakeup on */
@@ -851,23 +846,21 @@ static int inv_icm42600_suspend(struct device *dev)
851846
if (st->apex.wom.enable) {
852847
ret = inv_icm42600_disable_wom(st);
853848
if (ret)
854-
goto out_unlock;
849+
return ret;
855850
}
856851
accel_conf = INV_ICM42600_SENSOR_MODE_OFF;
857852
}
858853

859854
ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF,
860855
accel_conf, false, NULL);
861856
if (ret)
862-
goto out_unlock;
857+
return ret;
863858

864859
/* disable vddio regulator if chip is sleeping */
865860
if (!wakeup)
866861
regulator_disable(st->vddio_supply);
867862

868-
out_unlock:
869-
mutex_unlock(&st->lock);
870-
return ret;
863+
return 0;
871864
}
872865

873866
/*
@@ -881,12 +874,12 @@ static int inv_icm42600_resume(struct device *dev)
881874
struct inv_icm42600_sensor_state *accel_st = iio_priv(st->indio_accel);
882875
struct device *accel_dev;
883876
bool wakeup;
884-
int ret = 0;
877+
int ret;
885878

886-
mutex_lock(&st->lock);
879+
guard(mutex)(&st->lock);
887880

888881
if (pm_runtime_suspended(dev))
889-
goto out_unlock;
882+
return 0;
890883

891884
/* check wakeup capability */
892885
accel_dev = &st->indio_accel->dev;
@@ -898,21 +891,21 @@ static int inv_icm42600_resume(struct device *dev)
898891
} else {
899892
ret = inv_icm42600_enable_regulator_vddio(st);
900893
if (ret)
901-
goto out_unlock;
894+
return ret;
902895
}
903896

904897
/* restore sensors state */
905898
ret = inv_icm42600_set_pwr_mgmt0(st, st->suspended.gyro,
906899
st->suspended.accel,
907900
st->suspended.temp, NULL);
908901
if (ret)
909-
goto out_unlock;
902+
return ret;
910903

911904
/* restore APEX features if disabled */
912905
if (!wakeup && st->apex.wom.enable) {
913906
ret = inv_icm42600_enable_wom(st);
914907
if (ret)
915-
goto out_unlock;
908+
return ret;
916909
}
917910

918911
/* restore FIFO data streaming */
@@ -923,9 +916,7 @@ static int inv_icm42600_resume(struct device *dev)
923916
INV_ICM42600_FIFO_CONFIG_STREAM);
924917
}
925918

926-
out_unlock:
927-
mutex_unlock(&st->lock);
928-
return ret;
919+
return 0;
929920
}
930921

931922
/* Runtime suspend will turn off sensors that are enabled by iio devices. */
@@ -934,34 +925,28 @@ static int inv_icm42600_runtime_suspend(struct device *dev)
934925
struct inv_icm42600_state *st = dev_get_drvdata(dev);
935926
int ret;
936927

937-
mutex_lock(&st->lock);
928+
guard(mutex)(&st->lock);
938929

939930
/* disable all sensors */
940931
ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF,
941932
INV_ICM42600_SENSOR_MODE_OFF, false,
942933
NULL);
943934
if (ret)
944-
goto error_unlock;
935+
return ret;
945936

946937
regulator_disable(st->vddio_supply);
947938

948-
error_unlock:
949-
mutex_unlock(&st->lock);
950-
return ret;
939+
return 0;
951940
}
952941

953942
/* Sensors are enabled by iio devices, no need to turn them back on here. */
954943
static int inv_icm42600_runtime_resume(struct device *dev)
955944
{
956945
struct inv_icm42600_state *st = dev_get_drvdata(dev);
957-
int ret;
958946

959-
mutex_lock(&st->lock);
960-
961-
ret = inv_icm42600_enable_regulator_vddio(st);
947+
guard(mutex)(&st->lock);
962948

963-
mutex_unlock(&st->lock);
964-
return ret;
949+
return inv_icm42600_enable_regulator_vddio(st);
965950
}
966951

967952
EXPORT_NS_GPL_DEV_PM_OPS(inv_icm42600_pm_ops, IIO_ICM42600) = {

drivers/iio/imu/inv_icm42600/inv_icm42600_gyro.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,10 @@ static int inv_icm42600_gyro_write_scale(struct iio_dev *indio_dev,
277277
conf.fs = idx / 2;
278278

279279
pm_runtime_get_sync(dev);
280-
mutex_lock(&st->lock);
281280

282-
ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
281+
scoped_guard(mutex, &st->lock)
282+
ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
283283

284-
mutex_unlock(&st->lock);
285284
pm_runtime_put_autosuspend(dev);
286285

287286
return ret;
@@ -688,16 +687,11 @@ static int inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev *indio_dev,
688687
unsigned int val)
689688
{
690689
struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
691-
int ret;
692690

693-
mutex_lock(&st->lock);
691+
guard(mutex)(&st->lock);
694692

695693
st->fifo.watermark.gyro = val;
696-
ret = inv_icm42600_buffer_update_watermark(st);
697-
698-
mutex_unlock(&st->lock);
699-
700-
return ret;
694+
return inv_icm42600_buffer_update_watermark(st);
701695
}
702696

703697
static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev,
@@ -709,15 +703,13 @@ static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev,
709703
if (count == 0)
710704
return 0;
711705

712-
mutex_lock(&st->lock);
706+
guard(mutex)(&st->lock);
713707

714708
ret = inv_icm42600_buffer_hwfifo_flush(st, count);
715-
if (!ret)
716-
ret = st->fifo.nb.gyro;
717-
718-
mutex_unlock(&st->lock);
709+
if (ret)
710+
return ret;
719711

720-
return ret;
712+
return st->fifo.nb.gyro;
721713
}
722714

723715
static const struct iio_info inv_icm42600_gyro_info = {

0 commit comments

Comments
 (0)