Skip to content

Commit 0e6a627

Browse files
aldocontelkjic23
authored andcommitted
iio: tcs3472: convert remaining locking to guard(mutex)
Convert several functions to use guard(mutex)() This avoids manual unlock calls on each return path, drops the goto in tcs3472_write_event(), and removes 'ret' variables only needed to return after the unlock. While the conversion is in progress, take the opportunity to make a few small cleanups that guard() enables: - In tcs3472_read_event_config(), replace '!!(...)' with '(...) ? 1 : 0' for readability. - In tcs3472_write_event_config(), tcs3472_powerdown() and tcs3472_resume() use an early return on the I2C write failure path. No functional change. Signed-off-by: Aldo Conte <aldocontelk@gmail.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 6283163 commit 0e6a627

1 file changed

Lines changed: 32 additions & 44 deletions

File tree

drivers/iio/light/tcs3472.c

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* TODO: wait time
1414
*/
1515

16+
#include <linux/cleanup.h>
1617
#include <linux/delay.h>
1718
#include <linux/i2c.h>
1819
#include <linux/module.h>
@@ -220,32 +221,24 @@ static int tcs3472_read_event(struct iio_dev *indio_dev,
220221
int *val2)
221222
{
222223
struct tcs3472_data *data = iio_priv(indio_dev);
223-
int ret;
224224
unsigned int period;
225225

226-
mutex_lock(&data->lock);
226+
guard(mutex)(&data->lock);
227227

228228
switch (info) {
229229
case IIO_EV_INFO_VALUE:
230230
*val = (dir == IIO_EV_DIR_RISING) ?
231231
data->high_thresh : data->low_thresh;
232-
ret = IIO_VAL_INT;
233-
break;
232+
return IIO_VAL_INT;
234233
case IIO_EV_INFO_PERIOD:
235234
period = (256 - data->atime) * 2400 *
236235
tcs3472_intr_pers[data->apers];
237236
*val = period / USEC_PER_SEC;
238237
*val2 = period % USEC_PER_SEC;
239-
ret = IIO_VAL_INT_PLUS_MICRO;
240-
break;
238+
return IIO_VAL_INT_PLUS_MICRO;
241239
default:
242-
ret = -EINVAL;
243-
break;
240+
return -EINVAL;
244241
}
245-
246-
mutex_unlock(&data->lock);
247-
248-
return ret;
249242
}
250243

251244
static int tcs3472_write_event(struct iio_dev *indio_dev,
@@ -259,7 +252,8 @@ static int tcs3472_write_event(struct iio_dev *indio_dev,
259252
int period;
260253
int i;
261254

262-
mutex_lock(&data->lock);
255+
guard(mutex)(&data->lock);
256+
263257
switch (info) {
264258
case IIO_EV_INFO_VALUE:
265259
switch (dir) {
@@ -270,18 +264,18 @@ static int tcs3472_write_event(struct iio_dev *indio_dev,
270264
command = TCS3472_AILT;
271265
break;
272266
default:
273-
ret = -EINVAL;
274-
goto error;
267+
return -EINVAL;
275268
}
276269
ret = i2c_smbus_write_word_data(data->client, command, val);
277270
if (ret)
278-
goto error;
271+
return ret;
279272

280273
if (dir == IIO_EV_DIR_RISING)
281274
data->high_thresh = val;
282275
else
283276
data->low_thresh = val;
284-
break;
277+
278+
return 0;
285279
case IIO_EV_INFO_PERIOD:
286280
period = val * USEC_PER_SEC + val2;
287281
for (i = 1; i < ARRAY_SIZE(tcs3472_intr_pers) - 1; i++) {
@@ -291,32 +285,25 @@ static int tcs3472_write_event(struct iio_dev *indio_dev,
291285
}
292286
ret = i2c_smbus_write_byte_data(data->client, TCS3472_PERS, i);
293287
if (ret)
294-
goto error;
288+
return ret;
295289

296290
data->apers = i;
297-
break;
291+
292+
return 0;
298293
default:
299-
ret = -EINVAL;
300-
break;
294+
return -EINVAL;
301295
}
302-
error:
303-
mutex_unlock(&data->lock);
304-
305-
return ret;
306296
}
307297

308298
static int tcs3472_read_event_config(struct iio_dev *indio_dev,
309299
const struct iio_chan_spec *chan, enum iio_event_type type,
310300
enum iio_event_direction dir)
311301
{
312302
struct tcs3472_data *data = iio_priv(indio_dev);
313-
int ret;
314303

315-
mutex_lock(&data->lock);
316-
ret = !!(data->enable & TCS3472_ENABLE_AIEN);
317-
mutex_unlock(&data->lock);
304+
guard(mutex)(&data->lock);
318305

319-
return ret;
306+
return (data->enable & TCS3472_ENABLE_AIEN) ? 1 : 0;
320307
}
321308

322309
static int tcs3472_write_event_config(struct iio_dev *indio_dev,
@@ -327,7 +314,7 @@ static int tcs3472_write_event_config(struct iio_dev *indio_dev,
327314
int ret = 0;
328315
u8 enable_old;
329316

330-
mutex_lock(&data->lock);
317+
guard(mutex)(&data->lock);
331318

332319
enable_old = data->enable;
333320

@@ -339,12 +326,13 @@ static int tcs3472_write_event_config(struct iio_dev *indio_dev,
339326
if (enable_old != data->enable) {
340327
ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE,
341328
data->enable);
342-
if (ret)
329+
if (ret) {
343330
data->enable = enable_old;
331+
return ret;
332+
}
344333
}
345-
mutex_unlock(&data->lock);
346334

347-
return ret;
335+
return 0;
348336
}
349337

350338
static irqreturn_t tcs3472_event_handler(int irq, void *priv)
@@ -445,16 +433,16 @@ static int tcs3472_powerdown(struct tcs3472_data *data)
445433
int ret;
446434
u8 enable_mask = TCS3472_ENABLE_AEN | TCS3472_ENABLE_PON;
447435

448-
mutex_lock(&data->lock);
436+
guard(mutex)(&data->lock);
449437

450438
ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE,
451439
data->enable & ~enable_mask);
452-
if (!ret)
453-
data->enable &= ~enable_mask;
440+
if (ret)
441+
return ret;
454442

455-
mutex_unlock(&data->lock);
443+
data->enable &= ~enable_mask;
456444

457-
return ret;
445+
return 0;
458446
}
459447

460448
static int tcs3472_probe(struct i2c_client *client)
@@ -583,16 +571,16 @@ static int tcs3472_resume(struct device *dev)
583571
int ret;
584572
u8 enable_mask = TCS3472_ENABLE_AEN | TCS3472_ENABLE_PON;
585573

586-
mutex_lock(&data->lock);
574+
guard(mutex)(&data->lock);
587575

588576
ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE,
589577
data->enable | enable_mask);
590-
if (!ret)
591-
data->enable |= enable_mask;
578+
if (ret)
579+
return ret;
592580

593-
mutex_unlock(&data->lock);
581+
data->enable |= enable_mask;
594582

595-
return ret;
583+
return 0;
596584
}
597585

598586
static DEFINE_SIMPLE_DEV_PM_OPS(tcs3472_pm_ops, tcs3472_suspend,

0 commit comments

Comments
 (0)