Skip to content

Commit d2b3d46

Browse files
maxskiierjic23
authored andcommitted
iio: chemical: sps30: Replace manual locking with RAII locking
Replace manual mutex_lock() and mutex_unlock() calls with the much newer guard(mutex)() macro to enable RAII patterns, modernize the driver, and to increase readability. Move mutex locking into sps30_do_meas() and tune it up to use guard()(), as every caller takes the lock anyways. Signed-off-by: Maxwell Doose <m32285159@gmail.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 1ccae88 commit d2b3d46

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

drivers/iio/chemical/sps30.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
66
*/
77

8+
#include <linux/cleanup.h>
89
#include <linux/crc8.h>
910
#include <linux/delay.h>
1011
#include <linux/i2c.h>
@@ -69,6 +70,8 @@ static int sps30_do_meas(struct sps30_state *state, s32 *data, int size)
6970
{
7071
int i, ret;
7172

73+
guard(mutex)(&state->lock);
74+
7275
if (state->state == RESET) {
7376
ret = state->ops->start_meas(state);
7477
if (ret)
@@ -111,9 +114,7 @@ static irqreturn_t sps30_trigger_handler(int irq, void *p)
111114
aligned_s64 ts;
112115
} scan;
113116

114-
mutex_lock(&state->lock);
115117
ret = sps30_do_meas(state, scan.data, ARRAY_SIZE(scan.data));
116-
mutex_unlock(&state->lock);
117118
if (ret)
118119
goto err;
119120

@@ -136,7 +137,6 @@ static int sps30_read_raw(struct iio_dev *indio_dev,
136137
case IIO_CHAN_INFO_PROCESSED:
137138
switch (chan->type) {
138139
case IIO_MASSCONCENTRATION:
139-
mutex_lock(&state->lock);
140140
/* read up to the number of bytes actually needed */
141141
switch (chan->channel2) {
142142
case IIO_MOD_PM1:
@@ -152,7 +152,6 @@ static int sps30_read_raw(struct iio_dev *indio_dev,
152152
ret = sps30_do_meas(state, data, 4);
153153
break;
154154
}
155-
mutex_unlock(&state->lock);
156155
if (ret)
157156
return ret;
158157

@@ -197,9 +196,9 @@ static ssize_t start_cleaning_store(struct device *dev,
197196
if (kstrtoint(buf, 0, &val) || val != 1)
198197
return -EINVAL;
199198

200-
mutex_lock(&state->lock);
199+
guard(mutex)(&state->lock);
200+
201201
ret = state->ops->clean_fan(state);
202-
mutex_unlock(&state->lock);
203202
if (ret)
204203
return ret;
205204

@@ -215,9 +214,9 @@ static ssize_t cleaning_period_show(struct device *dev,
215214
__be32 val;
216215
int ret;
217216

218-
mutex_lock(&state->lock);
217+
guard(mutex)(&state->lock);
218+
219219
ret = state->ops->read_cleaning_period(state, &val);
220-
mutex_unlock(&state->lock);
221220
if (ret)
222221
return ret;
223222

@@ -238,12 +237,11 @@ static ssize_t cleaning_period_store(struct device *dev, struct device_attribute
238237
(val > SPS30_AUTO_CLEANING_PERIOD_MAX))
239238
return -EINVAL;
240239

241-
mutex_lock(&state->lock);
240+
guard(mutex)(&state->lock);
241+
242242
ret = state->ops->write_cleaning_period(state, cpu_to_be32(val));
243-
if (ret) {
244-
mutex_unlock(&state->lock);
243+
if (ret)
245244
return ret;
246-
}
247245

248246
msleep(20);
249247

@@ -256,8 +254,6 @@ static ssize_t cleaning_period_store(struct device *dev, struct device_attribute
256254
dev_warn(dev,
257255
"period changed but reads will return the old value\n");
258256

259-
mutex_unlock(&state->lock);
260-
261257
return len;
262258
}
263259

0 commit comments

Comments
 (0)