5
5
* Copyright 2017 Analog Devices Inc.
6
6
*/
7
7
#include <linux/bitfield.h>
8
+ #include <linux/cleanup.h>
8
9
#include <linux/clk.h>
9
10
#include <linux/delay.h>
10
11
#include <linux/device.h>
@@ -340,20 +341,12 @@ static int ad7768_reg_access(struct iio_dev *indio_dev,
340
341
unsigned int * readval )
341
342
{
342
343
struct ad7768_state * st = iio_priv (indio_dev );
343
- int ret ;
344
344
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 );
355
348
356
- return ret ;
349
+ return ad7768_spi_reg_write ( st , reg , writeval ) ;
357
350
}
358
351
359
352
static int ad7768_set_dig_fil (struct ad7768_state * st ,
@@ -381,32 +374,24 @@ static int ad7768_set_dig_fil(struct ad7768_state *st,
381
374
int ad7768_gpio_direction_input (struct gpio_chip * chip , unsigned int offset )
382
375
{
383
376
struct ad7768_state * st = gpiochip_get_data (chip );
384
- int ret ;
385
377
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 ,
388
380
AD7768_REG_GPIO_CONTROL ,
389
381
BIT (offset ),
390
382
AD7768_GPIO_INPUT (offset ));
391
- mutex_unlock (& st -> lock );
392
-
393
- return ret ;
394
383
}
395
384
396
385
int ad7768_gpio_direction_output (struct gpio_chip * chip ,
397
386
unsigned int offset , int value )
398
387
{
399
388
struct ad7768_state * st = gpiochip_get_data (chip );
400
- int ret ;
401
389
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 ,
404
392
AD7768_REG_GPIO_CONTROL ,
405
393
BIT (offset ),
406
394
AD7768_GPIO_OUTPUT (offset ));
407
- mutex_unlock (& st -> lock );
408
-
409
- return ret ;
410
395
}
411
396
412
397
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)
415
400
unsigned int val ;
416
401
int ret ;
417
402
418
- mutex_lock (& st -> lock );
403
+ guard ( mutex ) (& st -> lock );
419
404
ret = ad7768_spi_reg_read (st , AD7768_REG_GPIO_CONTROL , & val , 1 );
420
405
if (ret < 0 )
421
- goto gpio_get_err ;
406
+ return ret ;
422
407
423
408
if (val & BIT (offset ))
424
409
ret = ad7768_spi_reg_read (st , AD7768_REG_GPIO_WRITE , & val , 1 );
425
410
else
426
411
ret = ad7768_spi_reg_read (st , AD7768_REG_GPIO_READ , & val , 1 );
427
412
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 ;
434
414
435
- return ret ;
415
+ return !!( val & BIT ( offset )) ;
436
416
}
437
417
438
418
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)
441
421
unsigned int val ;
442
422
int ret ;
443
423
444
- mutex_lock (& st -> lock );
424
+ guard ( mutex ) (& st -> lock );
445
425
ret = ad7768_spi_reg_read (st , AD7768_REG_GPIO_CONTROL , & val , 1 );
446
426
if (ret < 0 )
447
- goto gpio_set_err ;
427
+ return ;
448
428
449
429
if (val & BIT (offset ))
450
430
ad7768_spi_reg_write_masked (st ,
451
431
AD7768_REG_GPIO_WRITE ,
452
432
BIT (offset ),
453
433
(value << offset ));
454
-
455
- gpio_set_err :
456
- mutex_unlock (& st -> lock );
457
434
}
458
435
459
436
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)
712
689
struct ad7768_state * st = iio_priv (indio_dev );
713
690
int ret ;
714
691
715
- mutex_lock (& st -> lock );
692
+ guard ( mutex ) (& st -> lock );
716
693
717
694
ret = spi_read (st -> spi , & st -> data .scan .chan , 3 );
718
695
if (ret < 0 )
@@ -723,7 +700,6 @@ static irqreturn_t ad7768_trigger_handler(int irq, void *p)
723
700
724
701
err_unlock :
725
702
iio_trigger_notify_done (indio_dev -> trig );
726
- mutex_unlock (& st -> lock );
727
703
728
704
return IRQ_HANDLED ;
729
705
}
0 commit comments