Skip to content

Commit ccf300c

Browse files
Guilherme Diasjic23
authored andcommitted
iio: gyro: adxrs290: Use guard(mutex) in lieu of manual lock+unlock
Use guard(mutex)() to automatically release the lock on scope exit, simplifying the error handling path and removing the need for explicit unlock and goto-based cleanup. Signed-off-by: Guilherme Dias <guilhermeabreu200105@usp.br> Co-developed-by: João Paulo Menezes Linaris <jplinaris@usp.br> Signed-off-by: João Paulo Menezes Linaris <jplinaris@usp.br> Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com> Reviewed-by: Maxwell Doose <m32285159@gmail.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent c49965a commit ccf300c

1 file changed

Lines changed: 31 additions & 44 deletions

File tree

drivers/iio/gyro/adxrs290.c

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <linux/bitfield.h>
1010
#include <linux/bitops.h>
11+
#include <linux/cleanup.h>
1112
#include <linux/delay.h>
1213
#include <linux/device.h>
1314
#include <linux/kernel.h>
@@ -115,65 +116,53 @@ static const int adxrs290_hpf_3db_freq_hz_table[][2] = {
115116
static int adxrs290_get_rate_data(struct iio_dev *indio_dev, const u8 cmd, int *val)
116117
{
117118
struct adxrs290_state *st = iio_priv(indio_dev);
118-
int ret = 0;
119119
int temp;
120120

121-
mutex_lock(&st->lock);
121+
guard(mutex)(&st->lock);
122+
122123
temp = spi_w8r16(st->spi, cmd);
123-
if (temp < 0) {
124-
ret = temp;
125-
goto err_unlock;
126-
}
124+
if (temp < 0)
125+
return temp;
127126

128127
*val = sign_extend32(temp, 15);
129128

130-
err_unlock:
131-
mutex_unlock(&st->lock);
132-
return ret;
129+
return 0;
133130
}
134131

135132
static int adxrs290_get_temp_data(struct iio_dev *indio_dev, int *val)
136133
{
137134
const u8 cmd = ADXRS290_READ_REG(ADXRS290_REG_TEMP0);
138135
struct adxrs290_state *st = iio_priv(indio_dev);
139-
int ret = 0;
140136
int temp;
141137

142-
mutex_lock(&st->lock);
138+
guard(mutex)(&st->lock);
139+
143140
temp = spi_w8r16(st->spi, cmd);
144-
if (temp < 0) {
145-
ret = temp;
146-
goto err_unlock;
147-
}
141+
if (temp < 0)
142+
return temp;
148143

149144
/* extract lower 12 bits temperature reading */
150145
*val = sign_extend32(temp, 11);
151146

152-
err_unlock:
153-
mutex_unlock(&st->lock);
154-
return ret;
147+
return 0;
155148
}
156149

157150
static int adxrs290_get_3db_freq(struct iio_dev *indio_dev, u8 *val, u8 *val2)
158151
{
159152
const u8 cmd = ADXRS290_READ_REG(ADXRS290_REG_FILTER);
160153
struct adxrs290_state *st = iio_priv(indio_dev);
161-
int ret = 0;
162154
short temp;
163155

164-
mutex_lock(&st->lock);
156+
guard(mutex)(&st->lock);
157+
165158
temp = spi_w8r8(st->spi, cmd);
166-
if (temp < 0) {
167-
ret = temp;
168-
goto err_unlock;
169-
}
159+
if (temp < 0)
160+
return temp;
170161

171162
*val = FIELD_GET(ADXRS290_LPF_MASK, temp);
172163
*val2 = FIELD_GET(ADXRS290_HPF_MASK, temp);
173164

174-
err_unlock:
175-
mutex_unlock(&st->lock);
176-
return ret;
165+
return 0;
177166
}
178167

179168
static int adxrs290_spi_write_reg(struct spi_device *spi, const u8 reg,
@@ -220,11 +209,11 @@ static int adxrs290_set_mode(struct iio_dev *indio_dev, enum adxrs290_mode mode)
220209
if (st->mode == mode)
221210
return 0;
222211

223-
mutex_lock(&st->lock);
212+
guard(mutex)(&st->lock);
224213

225214
ret = spi_w8r8(st->spi, ADXRS290_READ_REG(ADXRS290_REG_POWER_CTL));
226215
if (ret < 0)
227-
goto out_unlock;
216+
return ret;
228217

229218
val = ret;
230219

@@ -236,21 +225,18 @@ static int adxrs290_set_mode(struct iio_dev *indio_dev, enum adxrs290_mode mode)
236225
val |= ADXRS290_MEASUREMENT;
237226
break;
238227
default:
239-
ret = -EINVAL;
240-
goto out_unlock;
228+
return -EINVAL;
241229
}
242230

243231
ret = adxrs290_spi_write_reg(st->spi, ADXRS290_REG_POWER_CTL, val);
244232
if (ret < 0) {
245233
dev_err(&st->spi->dev, "unable to set mode: %d\n", ret);
246-
goto out_unlock;
234+
return ret;
247235
}
248236

249237
/* update cached mode */
250238
st->mode = mode;
251239

252-
out_unlock:
253-
mutex_unlock(&st->lock);
254240
return ret;
255241
}
256242

@@ -506,19 +492,20 @@ static irqreturn_t adxrs290_trigger_handler(int irq, void *p)
506492
u8 tx = ADXRS290_READ_REG(ADXRS290_REG_DATAX0);
507493
int ret;
508494

509-
mutex_lock(&st->lock);
495+
do {
496+
guard(mutex)(&st->lock);
510497

511-
/* exercise a bulk data capture starting from reg DATAX0... */
512-
ret = spi_write_then_read(st->spi, &tx, sizeof(tx), st->buffer.channels,
513-
sizeof(st->buffer.channels));
514-
if (ret < 0)
515-
goto out_unlock_notify;
498+
/* exercise a bulk data capture starting from reg DATAX0... */
499+
ret = spi_write_then_read(st->spi, &tx, sizeof(tx),
500+
st->buffer.channels,
501+
sizeof(st->buffer.channels));
502+
if (ret < 0)
503+
break;
516504

517-
iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
518-
pf->timestamp);
505+
iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
506+
pf->timestamp);
507+
} while (0);
519508

520-
out_unlock_notify:
521-
mutex_unlock(&st->lock);
522509
iio_trigger_notify_done(indio_dev->trig);
523510

524511
return IRQ_HANDLED;

0 commit comments

Comments
 (0)