mirror of
https://github.com/torvalds/linux.git
synced 2026-05-24 15:12:13 +02:00
iio: adc: mcp3911: simplify code with guard macro
Use the guard(mutex) macro for handle mutex lock/unlocks. Signed-off-by: Marcus Folkesson <marcus.folkesson@gmail.com> Link: https://lore.kernel.org/r/20231206-mcp3911-guard-v4-1-30c3c5d4340f@gmail.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
This commit is contained in:
parent
9f4e9ffee9
commit
17819da62a
|
|
@ -7,6 +7,7 @@
|
|||
*/
|
||||
#include <linux/bitfield.h>
|
||||
#include <linux/bits.h>
|
||||
#include <linux/cleanup.h>
|
||||
#include <linux/clk.h>
|
||||
#include <linux/delay.h>
|
||||
#include <linux/err.h>
|
||||
|
|
@ -316,47 +317,37 @@ static int mcp3911_read_raw(struct iio_dev *indio_dev,
|
|||
int *val2, long mask)
|
||||
{
|
||||
struct mcp3911 *adc = iio_priv(indio_dev);
|
||||
int ret = -EINVAL;
|
||||
int ret;
|
||||
|
||||
mutex_lock(&adc->lock);
|
||||
guard(mutex)(&adc->lock);
|
||||
switch (mask) {
|
||||
case IIO_CHAN_INFO_RAW:
|
||||
ret = mcp3911_read(adc,
|
||||
MCP3911_CHANNEL(channel->channel), val, 3);
|
||||
if (ret)
|
||||
goto out;
|
||||
return ret;
|
||||
|
||||
*val = sign_extend32(*val, 23);
|
||||
|
||||
ret = IIO_VAL_INT;
|
||||
break;
|
||||
|
||||
return IIO_VAL_INT;
|
||||
case IIO_CHAN_INFO_OFFSET:
|
||||
|
||||
ret = adc->chip->get_offset(adc, channel->channel, val);
|
||||
if (ret)
|
||||
goto out;
|
||||
return ret;
|
||||
|
||||
ret = IIO_VAL_INT;
|
||||
break;
|
||||
return IIO_VAL_INT;
|
||||
case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
|
||||
ret = adc->chip->get_osr(adc, val);
|
||||
if (ret)
|
||||
goto out;
|
||||
|
||||
ret = IIO_VAL_INT;
|
||||
break;
|
||||
return ret;
|
||||
|
||||
return IIO_VAL_INT;
|
||||
case IIO_CHAN_INFO_SCALE:
|
||||
*val = mcp3911_scale_table[ilog2(adc->gain[channel->channel])][0];
|
||||
*val2 = mcp3911_scale_table[ilog2(adc->gain[channel->channel])][1];
|
||||
ret = IIO_VAL_INT_PLUS_NANO;
|
||||
break;
|
||||
return IIO_VAL_INT_PLUS_NANO;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
out:
|
||||
mutex_unlock(&adc->lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int mcp3911_write_raw(struct iio_dev *indio_dev,
|
||||
|
|
@ -364,9 +355,8 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev,
|
|||
int val2, long mask)
|
||||
{
|
||||
struct mcp3911 *adc = iio_priv(indio_dev);
|
||||
int ret = -EINVAL;
|
||||
|
||||
mutex_lock(&adc->lock);
|
||||
guard(mutex)(&adc->lock);
|
||||
switch (mask) {
|
||||
case IIO_CHAN_INFO_SCALE:
|
||||
for (int i = 0; i < MCP3911_NUM_SCALES; i++) {
|
||||
|
|
@ -374,32 +364,25 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev,
|
|||
val2 == mcp3911_scale_table[i][1]) {
|
||||
|
||||
adc->gain[channel->channel] = BIT(i);
|
||||
ret = adc->chip->set_scale(adc, channel->channel, i);
|
||||
return adc->chip->set_scale(adc, channel->channel, i);
|
||||
}
|
||||
}
|
||||
break;
|
||||
return -EINVAL;
|
||||
case IIO_CHAN_INFO_OFFSET:
|
||||
if (val2 != 0) {
|
||||
ret = -EINVAL;
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = adc->chip->set_offset(adc, channel->channel, val);
|
||||
break;
|
||||
if (val2 != 0)
|
||||
return -EINVAL;
|
||||
|
||||
return adc->chip->set_offset(adc, channel->channel, val);
|
||||
case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
|
||||
for (int i = 0; i < ARRAY_SIZE(mcp3911_osr_table); i++) {
|
||||
if (val == mcp3911_osr_table[i]) {
|
||||
ret = adc->chip->set_osr(adc, i);
|
||||
break;
|
||||
return adc->chip->set_osr(adc, i);
|
||||
}
|
||||
}
|
||||
break;
|
||||
return -EINVAL;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
out:
|
||||
mutex_unlock(&adc->lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int mcp3911_calc_scale_table(struct mcp3911 *adc)
|
||||
|
|
@ -532,7 +515,7 @@ static irqreturn_t mcp3911_trigger_handler(int irq, void *p)
|
|||
int i = 0;
|
||||
int ret;
|
||||
|
||||
mutex_lock(&adc->lock);
|
||||
guard(mutex)(&adc->lock);
|
||||
adc->tx_buf = MCP3911_REG_READ(MCP3911_CHANNEL(0), adc->dev_addr);
|
||||
ret = spi_sync_transfer(adc->spi, xfer, ARRAY_SIZE(xfer));
|
||||
if (ret < 0) {
|
||||
|
|
@ -549,7 +532,6 @@ static irqreturn_t mcp3911_trigger_handler(int irq, void *p)
|
|||
iio_push_to_buffers_with_timestamp(indio_dev, &adc->scan,
|
||||
iio_get_time_ns(indio_dev));
|
||||
out:
|
||||
mutex_unlock(&adc->lock);
|
||||
iio_trigger_notify_done(indio_dev->trig);
|
||||
|
||||
return IRQ_HANDLED;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user