Commit 1fa220ec authored by Jonathan Cameron's avatar Jonathan Cameron

iio: adc: ad4130: Use automatic cleanup of locks and direct mode.

Reduces boilerplate and allows for simpler to follow direct returns.
Reviewed-by: default avatarNuno Sa <nuno.a@analog.com>
Link: https://lore.kernel.org/r/20240128150537.44592-10-jic23@kernel.orgSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent fb1f1971
...@@ -887,9 +887,9 @@ static int ad4130_set_filter_mode(struct iio_dev *indio_dev, ...@@ -887,9 +887,9 @@ static int ad4130_set_filter_mode(struct iio_dev *indio_dev,
unsigned int old_fs; unsigned int old_fs;
int ret = 0; int ret = 0;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
if (setup_info->filter_mode == val) if (setup_info->filter_mode == val)
goto out; return 0;
old_fs = setup_info->fs; old_fs = setup_info->fs;
old_filter_mode = setup_info->filter_mode; old_filter_mode = setup_info->filter_mode;
...@@ -911,12 +911,10 @@ static int ad4130_set_filter_mode(struct iio_dev *indio_dev, ...@@ -911,12 +911,10 @@ static int ad4130_set_filter_mode(struct iio_dev *indio_dev,
if (ret) { if (ret) {
setup_info->fs = old_fs; setup_info->fs = old_fs;
setup_info->filter_mode = old_filter_mode; setup_info->filter_mode = old_filter_mode;
return ret;
} }
out: return 0;
mutex_unlock(&st->lock);
return ret;
} }
static int ad4130_get_filter_mode(struct iio_dev *indio_dev, static int ad4130_get_filter_mode(struct iio_dev *indio_dev,
...@@ -927,9 +925,8 @@ static int ad4130_get_filter_mode(struct iio_dev *indio_dev, ...@@ -927,9 +925,8 @@ static int ad4130_get_filter_mode(struct iio_dev *indio_dev,
struct ad4130_setup_info *setup_info = &st->chans_info[channel].setup; struct ad4130_setup_info *setup_info = &st->chans_info[channel].setup;
enum ad4130_filter_mode filter_mode; enum ad4130_filter_mode filter_mode;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
filter_mode = setup_info->filter_mode; filter_mode = setup_info->filter_mode;
mutex_unlock(&st->lock);
return filter_mode; return filter_mode;
} }
...@@ -971,7 +968,7 @@ static int ad4130_set_channel_pga(struct ad4130_state *st, unsigned int channel, ...@@ -971,7 +968,7 @@ static int ad4130_set_channel_pga(struct ad4130_state *st, unsigned int channel,
struct ad4130_chan_info *chan_info = &st->chans_info[channel]; struct ad4130_chan_info *chan_info = &st->chans_info[channel];
struct ad4130_setup_info *setup_info = &chan_info->setup; struct ad4130_setup_info *setup_info = &chan_info->setup;
unsigned int pga, old_pga; unsigned int pga, old_pga;
int ret = 0; int ret;
for (pga = 0; pga < AD4130_MAX_PGA; pga++) for (pga = 0; pga < AD4130_MAX_PGA; pga++)
if (val == st->scale_tbls[setup_info->ref_sel][pga][0] && if (val == st->scale_tbls[setup_info->ref_sel][pga][0] &&
...@@ -981,21 +978,20 @@ static int ad4130_set_channel_pga(struct ad4130_state *st, unsigned int channel, ...@@ -981,21 +978,20 @@ static int ad4130_set_channel_pga(struct ad4130_state *st, unsigned int channel,
if (pga == AD4130_MAX_PGA) if (pga == AD4130_MAX_PGA)
return -EINVAL; return -EINVAL;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
if (pga == setup_info->pga) if (pga == setup_info->pga)
goto out; return 0;
old_pga = setup_info->pga; old_pga = setup_info->pga;
setup_info->pga = pga; setup_info->pga = pga;
ret = ad4130_write_channel_setup(st, channel, false); ret = ad4130_write_channel_setup(st, channel, false);
if (ret) if (ret) {
setup_info->pga = old_pga; setup_info->pga = old_pga;
out:
mutex_unlock(&st->lock);
return ret; return ret;
}
return 0;
} }
static int ad4130_set_channel_freq(struct ad4130_state *st, static int ad4130_set_channel_freq(struct ad4130_state *st,
...@@ -1004,26 +1000,25 @@ static int ad4130_set_channel_freq(struct ad4130_state *st, ...@@ -1004,26 +1000,25 @@ static int ad4130_set_channel_freq(struct ad4130_state *st,
struct ad4130_chan_info *chan_info = &st->chans_info[channel]; struct ad4130_chan_info *chan_info = &st->chans_info[channel];
struct ad4130_setup_info *setup_info = &chan_info->setup; struct ad4130_setup_info *setup_info = &chan_info->setup;
unsigned int fs, old_fs; unsigned int fs, old_fs;
int ret = 0; int ret;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
old_fs = setup_info->fs; old_fs = setup_info->fs;
ad4130_freq_to_fs(setup_info->filter_mode, val, val2, &fs); ad4130_freq_to_fs(setup_info->filter_mode, val, val2, &fs);
if (fs == setup_info->fs) if (fs == setup_info->fs)
goto out; return 0;
setup_info->fs = fs; setup_info->fs = fs;
ret = ad4130_write_channel_setup(st, channel, false); ret = ad4130_write_channel_setup(st, channel, false);
if (ret) if (ret) {
setup_info->fs = old_fs; setup_info->fs = old_fs;
out:
mutex_unlock(&st->lock);
return ret; return ret;
}
return 0;
} }
static int _ad4130_read_sample(struct iio_dev *indio_dev, unsigned int channel, static int _ad4130_read_sample(struct iio_dev *indio_dev, unsigned int channel,
...@@ -1065,20 +1060,13 @@ static int _ad4130_read_sample(struct iio_dev *indio_dev, unsigned int channel, ...@@ -1065,20 +1060,13 @@ static int _ad4130_read_sample(struct iio_dev *indio_dev, unsigned int channel,
static int ad4130_read_sample(struct iio_dev *indio_dev, unsigned int channel, static int ad4130_read_sample(struct iio_dev *indio_dev, unsigned int channel,
int *val) int *val)
{ {
iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
struct ad4130_state *st = iio_priv(indio_dev); struct ad4130_state *st = iio_priv(indio_dev);
int ret;
ret = iio_device_claim_direct_mode(indio_dev);
if (ret)
return ret;
mutex_lock(&st->lock);
ret = _ad4130_read_sample(indio_dev, channel, val);
mutex_unlock(&st->lock);
iio_device_release_direct_mode(indio_dev); guard(mutex)(&st->lock);
return _ad4130_read_sample(indio_dev, channel, val);
return ret; }
unreachable();
} }
static int ad4130_read_raw(struct iio_dev *indio_dev, static int ad4130_read_raw(struct iio_dev *indio_dev,
...@@ -1092,24 +1080,24 @@ static int ad4130_read_raw(struct iio_dev *indio_dev, ...@@ -1092,24 +1080,24 @@ static int ad4130_read_raw(struct iio_dev *indio_dev,
switch (info) { switch (info) {
case IIO_CHAN_INFO_RAW: case IIO_CHAN_INFO_RAW:
return ad4130_read_sample(indio_dev, channel, val); return ad4130_read_sample(indio_dev, channel, val);
case IIO_CHAN_INFO_SCALE: case IIO_CHAN_INFO_SCALE: {
mutex_lock(&st->lock); guard(mutex)(&st->lock);
*val = st->scale_tbls[setup_info->ref_sel][setup_info->pga][0]; *val = st->scale_tbls[setup_info->ref_sel][setup_info->pga][0];
*val2 = st->scale_tbls[setup_info->ref_sel][setup_info->pga][1]; *val2 = st->scale_tbls[setup_info->ref_sel][setup_info->pga][1];
mutex_unlock(&st->lock);
return IIO_VAL_INT_PLUS_NANO; return IIO_VAL_INT_PLUS_NANO;
}
case IIO_CHAN_INFO_OFFSET: case IIO_CHAN_INFO_OFFSET:
*val = st->bipolar ? -BIT(chan->scan_type.realbits - 1) : 0; *val = st->bipolar ? -BIT(chan->scan_type.realbits - 1) : 0;
return IIO_VAL_INT; return IIO_VAL_INT;
case IIO_CHAN_INFO_SAMP_FREQ: case IIO_CHAN_INFO_SAMP_FREQ: {
mutex_lock(&st->lock); guard(mutex)(&st->lock);
ad4130_fs_to_freq(setup_info->filter_mode, setup_info->fs, ad4130_fs_to_freq(setup_info->filter_mode, setup_info->fs,
val, val2); val, val2);
mutex_unlock(&st->lock);
return IIO_VAL_INT_PLUS_NANO; return IIO_VAL_INT_PLUS_NANO;
}
default: default:
return -EINVAL; return -EINVAL;
} }
...@@ -1134,9 +1122,9 @@ static int ad4130_read_avail(struct iio_dev *indio_dev, ...@@ -1134,9 +1122,9 @@ static int ad4130_read_avail(struct iio_dev *indio_dev,
return IIO_AVAIL_LIST; return IIO_AVAIL_LIST;
case IIO_CHAN_INFO_SAMP_FREQ: case IIO_CHAN_INFO_SAMP_FREQ:
mutex_lock(&st->lock); scoped_guard(mutex, &st->lock) {
filter_config = &ad4130_filter_configs[setup_info->filter_mode]; filter_config = &ad4130_filter_configs[setup_info->filter_mode];
mutex_unlock(&st->lock); }
*vals = (int *)filter_config->samp_freq_avail; *vals = (int *)filter_config->samp_freq_avail;
*length = filter_config->samp_freq_avail_len * 2; *length = filter_config->samp_freq_avail_len * 2;
...@@ -1197,21 +1185,18 @@ static int ad4130_update_scan_mode(struct iio_dev *indio_dev, ...@@ -1197,21 +1185,18 @@ static int ad4130_update_scan_mode(struct iio_dev *indio_dev,
unsigned int val = 0; unsigned int val = 0;
int ret; int ret;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
for_each_set_bit(channel, scan_mask, indio_dev->num_channels) { for_each_set_bit(channel, scan_mask, indio_dev->num_channels) {
ret = ad4130_set_channel_enable(st, channel, true); ret = ad4130_set_channel_enable(st, channel, true);
if (ret) if (ret)
goto out; return ret;
val++; val++;
} }
st->num_enabled_channels = val; st->num_enabled_channels = val;
out:
mutex_unlock(&st->lock);
return 0; return 0;
} }
...@@ -1232,22 +1217,19 @@ static int ad4130_set_fifo_watermark(struct iio_dev *indio_dev, unsigned int val ...@@ -1232,22 +1217,19 @@ static int ad4130_set_fifo_watermark(struct iio_dev *indio_dev, unsigned int val
*/ */
eff = rounddown(AD4130_FIFO_SIZE, st->num_enabled_channels); eff = rounddown(AD4130_FIFO_SIZE, st->num_enabled_channels);
mutex_lock(&st->lock); guard(mutex)(&st->lock);
ret = regmap_update_bits(st->regmap, AD4130_FIFO_CONTROL_REG, ret = regmap_update_bits(st->regmap, AD4130_FIFO_CONTROL_REG,
AD4130_FIFO_CONTROL_WM_MASK, AD4130_FIFO_CONTROL_WM_MASK,
FIELD_PREP(AD4130_FIFO_CONTROL_WM_MASK, FIELD_PREP(AD4130_FIFO_CONTROL_WM_MASK,
ad4130_watermark_reg_val(eff))); ad4130_watermark_reg_val(eff)));
if (ret) if (ret)
goto out; return ret;
st->effective_watermark = eff; st->effective_watermark = eff;
st->watermark = val; st->watermark = val;
out: return 0;
mutex_unlock(&st->lock);
return ret;
} }
static const struct iio_info ad4130_info = { static const struct iio_info ad4130_info = {
...@@ -1265,26 +1247,21 @@ static int ad4130_buffer_postenable(struct iio_dev *indio_dev) ...@@ -1265,26 +1247,21 @@ static int ad4130_buffer_postenable(struct iio_dev *indio_dev)
struct ad4130_state *st = iio_priv(indio_dev); struct ad4130_state *st = iio_priv(indio_dev);
int ret; int ret;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
ret = ad4130_set_watermark_interrupt_en(st, true); ret = ad4130_set_watermark_interrupt_en(st, true);
if (ret) if (ret)
goto out; return ret;
ret = irq_set_irq_type(st->spi->irq, st->inv_irq_trigger); ret = irq_set_irq_type(st->spi->irq, st->inv_irq_trigger);
if (ret) if (ret)
goto out; return ret;
ret = ad4130_set_fifo_mode(st, AD4130_FIFO_MODE_WM); ret = ad4130_set_fifo_mode(st, AD4130_FIFO_MODE_WM);
if (ret) if (ret)
goto out;
ret = ad4130_set_mode(st, AD4130_MODE_CONTINUOUS);
out:
mutex_unlock(&st->lock);
return ret; return ret;
return ad4130_set_mode(st, AD4130_MODE_CONTINUOUS);
} }
static int ad4130_buffer_predisable(struct iio_dev *indio_dev) static int ad4130_buffer_predisable(struct iio_dev *indio_dev)
...@@ -1293,23 +1270,23 @@ static int ad4130_buffer_predisable(struct iio_dev *indio_dev) ...@@ -1293,23 +1270,23 @@ static int ad4130_buffer_predisable(struct iio_dev *indio_dev)
unsigned int i; unsigned int i;
int ret; int ret;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
ret = ad4130_set_mode(st, AD4130_MODE_IDLE); ret = ad4130_set_mode(st, AD4130_MODE_IDLE);
if (ret) if (ret)
goto out; return ret;
ret = irq_set_irq_type(st->spi->irq, st->irq_trigger); ret = irq_set_irq_type(st->spi->irq, st->irq_trigger);
if (ret) if (ret)
goto out; return ret;
ret = ad4130_set_fifo_mode(st, AD4130_FIFO_MODE_DISABLED); ret = ad4130_set_fifo_mode(st, AD4130_FIFO_MODE_DISABLED);
if (ret) if (ret)
goto out; return ret;
ret = ad4130_set_watermark_interrupt_en(st, false); ret = ad4130_set_watermark_interrupt_en(st, false);
if (ret) if (ret)
goto out; return ret;
/* /*
* update_scan_mode() is not called in the disable path, disable all * update_scan_mode() is not called in the disable path, disable all
...@@ -1318,13 +1295,10 @@ static int ad4130_buffer_predisable(struct iio_dev *indio_dev) ...@@ -1318,13 +1295,10 @@ static int ad4130_buffer_predisable(struct iio_dev *indio_dev)
for (i = 0; i < indio_dev->num_channels; i++) { for (i = 0; i < indio_dev->num_channels; i++) {
ret = ad4130_set_channel_enable(st, i, false); ret = ad4130_set_channel_enable(st, i, false);
if (ret) if (ret)
goto out; return ret;
} }
out: return 0;
mutex_unlock(&st->lock);
return ret;
} }
static const struct iio_buffer_setup_ops ad4130_buffer_ops = { static const struct iio_buffer_setup_ops ad4130_buffer_ops = {
...@@ -1338,9 +1312,8 @@ static ssize_t hwfifo_watermark_show(struct device *dev, ...@@ -1338,9 +1312,8 @@ static ssize_t hwfifo_watermark_show(struct device *dev,
struct ad4130_state *st = iio_priv(dev_to_iio_dev(dev)); struct ad4130_state *st = iio_priv(dev_to_iio_dev(dev));
unsigned int val; unsigned int val;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
val = st->watermark; val = st->watermark;
mutex_unlock(&st->lock);
return sysfs_emit(buf, "%d\n", val); return sysfs_emit(buf, "%d\n", val);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment