Skip to content

Commit 1a5d9bd

Browse files
committed
iio: adc: ad7768-1: use guard(mutex) to simplify code
Use guard(mutex) from cleanup.h to remove most of the gotos and to make the code simpler and less likely to fail due to forgetting to unlock the resources. Signed-off-by: Jonathan Santos <Jonathan.Santos@analog.com>
1 parent a5547a7 commit 1a5d9bd

File tree

1 file changed

+16
-40
lines changed

1 file changed

+16
-40
lines changed

drivers/iio/adc/ad7768-1.c

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Copyright 2017 Analog Devices Inc.
66
*/
77
#include <linux/bitfield.h>
8+
#include <linux/cleanup.h>
89
#include <linux/clk.h>
910
#include <linux/delay.h>
1011
#include <linux/device.h>
@@ -340,20 +341,12 @@ static int ad7768_reg_access(struct iio_dev *indio_dev,
340341
unsigned int *readval)
341342
{
342343
struct ad7768_state *st = iio_priv(indio_dev);
343-
int ret;
344344

345-
mutex_lock(&st->lock);
346-
if (readval) {
347-
ret = ad7768_spi_reg_read(st, reg, readval, 1);
348-
if (ret < 0)
349-
goto err_unlock;
350-
} else {
351-
ret = ad7768_spi_reg_write(st, reg, writeval);
352-
}
353-
err_unlock:
354-
mutex_unlock(&st->lock);
345+
guard(mutex)(&st->lock);
346+
if (readval)
347+
return ad7768_spi_reg_read(st, reg, readval, 1);
355348

356-
return ret;
349+
return ad7768_spi_reg_write(st, reg, writeval);
357350
}
358351

359352
static int ad7768_set_dig_fil(struct ad7768_state *st,
@@ -381,32 +374,24 @@ static int ad7768_set_dig_fil(struct ad7768_state *st,
381374
int ad7768_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
382375
{
383376
struct ad7768_state *st = gpiochip_get_data(chip);
384-
int ret;
385377

386-
mutex_lock(&st->lock);
387-
ret = ad7768_spi_reg_write_masked(st,
378+
guard(mutex)(&st->lock);
379+
return ad7768_spi_reg_write_masked(st,
388380
AD7768_REG_GPIO_CONTROL,
389381
BIT(offset),
390382
AD7768_GPIO_INPUT(offset));
391-
mutex_unlock(&st->lock);
392-
393-
return ret;
394383
}
395384

396385
int ad7768_gpio_direction_output(struct gpio_chip *chip,
397386
unsigned int offset, int value)
398387
{
399388
struct ad7768_state *st = gpiochip_get_data(chip);
400-
int ret;
401389

402-
mutex_lock(&st->lock);
403-
ret = ad7768_spi_reg_write_masked(st,
390+
guard(mutex)(&st->lock);
391+
return ad7768_spi_reg_write_masked(st,
404392
AD7768_REG_GPIO_CONTROL,
405393
BIT(offset),
406394
AD7768_GPIO_OUTPUT(offset));
407-
mutex_unlock(&st->lock);
408-
409-
return ret;
410395
}
411396

412397
int ad7768_gpio_get(struct gpio_chip *chip, unsigned int offset)
@@ -415,24 +400,19 @@ int ad7768_gpio_get(struct gpio_chip *chip, unsigned int offset)
415400
unsigned int val;
416401
int ret;
417402

418-
mutex_lock(&st->lock);
403+
guard(mutex)(&st->lock);
419404
ret = ad7768_spi_reg_read(st, AD7768_REG_GPIO_CONTROL, &val, 1);
420405
if (ret < 0)
421-
goto gpio_get_err;
406+
return ret;
422407

423408
if (val & BIT(offset))
424409
ret = ad7768_spi_reg_read(st, AD7768_REG_GPIO_WRITE, &val, 1);
425410
else
426411
ret = ad7768_spi_reg_read(st, AD7768_REG_GPIO_READ, &val, 1);
427412
if (ret < 0)
428-
goto gpio_get_err;
429-
430-
ret = !!(val & BIT(offset));
431-
432-
gpio_get_err:
433-
mutex_unlock(&st->lock);
413+
return ret;
434414

435-
return ret;
415+
return !!(val & BIT(offset));
436416
}
437417

438418
void ad7768_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
@@ -441,19 +421,16 @@ void ad7768_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
441421
unsigned int val;
442422
int ret;
443423

444-
mutex_lock(&st->lock);
424+
guard(mutex)(&st->lock);
445425
ret = ad7768_spi_reg_read(st, AD7768_REG_GPIO_CONTROL, &val, 1);
446426
if (ret < 0)
447-
goto gpio_set_err;
427+
return;
448428

449429
if (val & BIT(offset))
450430
ad7768_spi_reg_write_masked(st,
451431
AD7768_REG_GPIO_WRITE,
452432
BIT(offset),
453433
(value << offset));
454-
455-
gpio_set_err:
456-
mutex_unlock(&st->lock);
457434
}
458435

459436
int ad7768_gpio_request(struct gpio_chip *chip, unsigned int offset)
@@ -712,7 +689,7 @@ static irqreturn_t ad7768_trigger_handler(int irq, void *p)
712689
struct ad7768_state *st = iio_priv(indio_dev);
713690
int ret;
714691

715-
mutex_lock(&st->lock);
692+
guard(mutex)(&st->lock);
716693

717694
ret = spi_read(st->spi, &st->data.scan.chan, 3);
718695
if (ret < 0)
@@ -723,7 +700,6 @@ static irqreturn_t ad7768_trigger_handler(int irq, void *p)
723700

724701
err_unlock:
725702
iio_trigger_notify_done(indio_dev->trig);
726-
mutex_unlock(&st->lock);
727703

728704
return IRQ_HANDLED;
729705
}

0 commit comments

Comments
 (0)