Commit eabc6b08 authored by Jonathan Cameron's avatar Jonathan Cameron

iio: accel: adxl367: Use automated cleanup for locks and iio direct mode.

Switching to the iio_device_claim_direct_scoped() for state
and to guard() based unlocking of mutexes simplifies error handling
by allowing direct returns when an error is encountered.
Reviewed-by: default avatarNuno Sa <nuno.a@analog.com>
Link: https://lore.kernel.org/r/20240128150537.44592-4-jic23@kernel.orgSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 366328e5
...@@ -339,22 +339,17 @@ static int adxl367_set_act_threshold(struct adxl367_state *st, ...@@ -339,22 +339,17 @@ static int adxl367_set_act_threshold(struct adxl367_state *st,
{ {
int ret; int ret;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
ret = adxl367_set_measure_en(st, false); ret = adxl367_set_measure_en(st, false);
if (ret) if (ret)
goto out; return ret;
ret = _adxl367_set_act_threshold(st, act, threshold); ret = _adxl367_set_act_threshold(st, act, threshold);
if (ret) if (ret)
goto out;
ret = adxl367_set_measure_en(st, true);
out:
mutex_unlock(&st->lock);
return ret; return ret;
return adxl367_set_measure_en(st, true);
} }
static int adxl367_set_act_proc_mode(struct adxl367_state *st, static int adxl367_set_act_proc_mode(struct adxl367_state *st,
...@@ -482,25 +477,22 @@ static int adxl367_set_fifo_watermark(struct adxl367_state *st, ...@@ -482,25 +477,22 @@ static int adxl367_set_fifo_watermark(struct adxl367_state *st,
static int adxl367_set_range(struct iio_dev *indio_dev, static int adxl367_set_range(struct iio_dev *indio_dev,
enum adxl367_range range) enum adxl367_range range)
{ {
iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
struct adxl367_state *st = iio_priv(indio_dev); struct adxl367_state *st = iio_priv(indio_dev);
int ret; int ret;
ret = iio_device_claim_direct_mode(indio_dev); guard(mutex)(&st->lock);
if (ret)
return ret;
mutex_lock(&st->lock);
ret = adxl367_set_measure_en(st, false); ret = adxl367_set_measure_en(st, false);
if (ret) if (ret)
goto out; return ret;
ret = regmap_update_bits(st->regmap, ADXL367_REG_FILTER_CTL, ret = regmap_update_bits(st->regmap, ADXL367_REG_FILTER_CTL,
ADXL367_FILTER_CTL_RANGE_MASK, ADXL367_FILTER_CTL_RANGE_MASK,
FIELD_PREP(ADXL367_FILTER_CTL_RANGE_MASK, FIELD_PREP(ADXL367_FILTER_CTL_RANGE_MASK,
range)); range));
if (ret) if (ret)
goto out; return ret;
adxl367_scale_act_thresholds(st, st->range, range); adxl367_scale_act_thresholds(st, st->range, range);
...@@ -508,25 +500,22 @@ static int adxl367_set_range(struct iio_dev *indio_dev, ...@@ -508,25 +500,22 @@ static int adxl367_set_range(struct iio_dev *indio_dev,
ret = _adxl367_set_act_threshold(st, ADXL367_ACTIVITY, ret = _adxl367_set_act_threshold(st, ADXL367_ACTIVITY,
st->act_threshold); st->act_threshold);
if (ret) if (ret)
goto out; return ret;
ret = _adxl367_set_act_threshold(st, ADXL367_INACTIVITY, ret = _adxl367_set_act_threshold(st, ADXL367_INACTIVITY,
st->inact_threshold); st->inact_threshold);
if (ret) if (ret)
goto out; return ret;
ret = adxl367_set_measure_en(st, true); ret = adxl367_set_measure_en(st, true);
if (ret) if (ret)
goto out; return ret;
st->range = range; st->range = range;
out: return 0;
mutex_unlock(&st->lock); }
unreachable();
iio_device_release_direct_mode(indio_dev);
return ret;
} }
static int adxl367_time_ms_to_samples(struct adxl367_state *st, unsigned int ms) static int adxl367_time_ms_to_samples(struct adxl367_state *st, unsigned int ms)
...@@ -587,11 +576,11 @@ static int adxl367_set_act_time_ms(struct adxl367_state *st, ...@@ -587,11 +576,11 @@ static int adxl367_set_act_time_ms(struct adxl367_state *st,
{ {
int ret; int ret;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
ret = adxl367_set_measure_en(st, false); ret = adxl367_set_measure_en(st, false);
if (ret) if (ret)
goto out; return ret;
if (act == ADXL367_ACTIVITY) if (act == ADXL367_ACTIVITY)
ret = _adxl367_set_act_time_ms(st, ms); ret = _adxl367_set_act_time_ms(st, ms);
...@@ -599,14 +588,9 @@ static int adxl367_set_act_time_ms(struct adxl367_state *st, ...@@ -599,14 +588,9 @@ static int adxl367_set_act_time_ms(struct adxl367_state *st,
ret = _adxl367_set_inact_time_ms(st, ms); ret = _adxl367_set_inact_time_ms(st, ms);
if (ret) if (ret)
goto out;
ret = adxl367_set_measure_en(st, true);
out:
mutex_unlock(&st->lock);
return ret; return ret;
return adxl367_set_measure_en(st, true);
} }
static int _adxl367_set_odr(struct adxl367_state *st, enum adxl367_odr odr) static int _adxl367_set_odr(struct adxl367_state *st, enum adxl367_odr odr)
...@@ -636,31 +620,23 @@ static int _adxl367_set_odr(struct adxl367_state *st, enum adxl367_odr odr) ...@@ -636,31 +620,23 @@ static int _adxl367_set_odr(struct adxl367_state *st, enum adxl367_odr odr)
static int adxl367_set_odr(struct iio_dev *indio_dev, enum adxl367_odr odr) static int adxl367_set_odr(struct iio_dev *indio_dev, enum adxl367_odr odr)
{ {
struct adxl367_state *st = iio_priv(indio_dev); iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
struct adxl367_state *st = iio_priv(indio_dev);;
int ret; int ret;
ret = iio_device_claim_direct_mode(indio_dev); guard(mutex)(&st->lock);
if (ret)
return ret;
mutex_lock(&st->lock);
ret = adxl367_set_measure_en(st, false); ret = adxl367_set_measure_en(st, false);
if (ret) if (ret)
goto out; return ret;
ret = _adxl367_set_odr(st, odr); ret = _adxl367_set_odr(st, odr);
if (ret) if (ret)
goto out;
ret = adxl367_set_measure_en(st, true);
out:
mutex_unlock(&st->lock);
iio_device_release_direct_mode(indio_dev);
return ret; return ret;
return adxl367_set_measure_en(st, true);
}
unreachable();
} }
static int adxl367_set_temp_adc_en(struct adxl367_state *st, unsigned int reg, static int adxl367_set_temp_adc_en(struct adxl367_state *st, unsigned int reg,
...@@ -749,36 +725,32 @@ static int adxl367_read_sample(struct iio_dev *indio_dev, ...@@ -749,36 +725,32 @@ static int adxl367_read_sample(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan, struct iio_chan_spec const *chan,
int *val) int *val)
{ {
iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
struct adxl367_state *st = iio_priv(indio_dev); struct adxl367_state *st = iio_priv(indio_dev);
u16 sample; u16 sample;
int ret; int ret;
ret = iio_device_claim_direct_mode(indio_dev); guard(mutex)(&st->lock);
if (ret)
return ret;
mutex_lock(&st->lock);
ret = adxl367_set_temp_adc_reg_en(st, chan->address, true); ret = adxl367_set_temp_adc_reg_en(st, chan->address, true);
if (ret) if (ret)
goto out; return ret;
ret = regmap_bulk_read(st->regmap, chan->address, &st->sample_buf, ret = regmap_bulk_read(st->regmap, chan->address, &st->sample_buf,
sizeof(st->sample_buf)); sizeof(st->sample_buf));
if (ret) if (ret)
goto out; return ret;
sample = FIELD_GET(ADXL367_DATA_MASK, be16_to_cpu(st->sample_buf)); sample = FIELD_GET(ADXL367_DATA_MASK, be16_to_cpu(st->sample_buf));
*val = sign_extend32(sample, chan->scan_type.realbits - 1); *val = sign_extend32(sample, chan->scan_type.realbits - 1);
ret = adxl367_set_temp_adc_reg_en(st, chan->address, false); ret = adxl367_set_temp_adc_reg_en(st, chan->address, false);
if (ret)
return ret;
out: return IIO_VAL_INT;
mutex_unlock(&st->lock); }
unreachable();
iio_device_release_direct_mode(indio_dev);
return ret ?: IIO_VAL_INT;
} }
static int adxl367_get_status(struct adxl367_state *st, u8 *status, static int adxl367_get_status(struct adxl367_state *st, u8 *status,
...@@ -886,12 +858,12 @@ static int adxl367_read_raw(struct iio_dev *indio_dev, ...@@ -886,12 +858,12 @@ static int adxl367_read_raw(struct iio_dev *indio_dev,
return adxl367_read_sample(indio_dev, chan, val); return adxl367_read_sample(indio_dev, chan, val);
case IIO_CHAN_INFO_SCALE: case IIO_CHAN_INFO_SCALE:
switch (chan->type) { switch (chan->type) {
case IIO_ACCEL: case IIO_ACCEL: {
mutex_lock(&st->lock); guard(mutex)(&st->lock);
*val = adxl367_range_scale_tbl[st->range][0]; *val = adxl367_range_scale_tbl[st->range][0];
*val2 = adxl367_range_scale_tbl[st->range][1]; *val2 = adxl367_range_scale_tbl[st->range][1];
mutex_unlock(&st->lock);
return IIO_VAL_INT_PLUS_NANO; return IIO_VAL_INT_PLUS_NANO;
}
case IIO_TEMP: case IIO_TEMP:
*val = 1000; *val = 1000;
*val2 = ADXL367_TEMP_PER_C; *val2 = ADXL367_TEMP_PER_C;
...@@ -914,12 +886,12 @@ static int adxl367_read_raw(struct iio_dev *indio_dev, ...@@ -914,12 +886,12 @@ static int adxl367_read_raw(struct iio_dev *indio_dev,
default: default:
return -EINVAL; return -EINVAL;
} }
case IIO_CHAN_INFO_SAMP_FREQ: case IIO_CHAN_INFO_SAMP_FREQ: {
mutex_lock(&st->lock); guard(mutex)(&st->lock);
*val = adxl367_samp_freq_tbl[st->odr][0]; *val = adxl367_samp_freq_tbl[st->odr][0];
*val2 = adxl367_samp_freq_tbl[st->odr][1]; *val2 = adxl367_samp_freq_tbl[st->odr][1];
mutex_unlock(&st->lock);
return IIO_VAL_INT_PLUS_MICRO; return IIO_VAL_INT_PLUS_MICRO;
}
default: default:
return -EINVAL; return -EINVAL;
} }
...@@ -1004,18 +976,15 @@ static int adxl367_read_event_value(struct iio_dev *indio_dev, ...@@ -1004,18 +976,15 @@ static int adxl367_read_event_value(struct iio_dev *indio_dev,
{ {
struct adxl367_state *st = iio_priv(indio_dev); struct adxl367_state *st = iio_priv(indio_dev);
guard(mutex)(&st->lock);
switch (info) { switch (info) {
case IIO_EV_INFO_VALUE: { case IIO_EV_INFO_VALUE: {
switch (dir) { switch (dir) {
case IIO_EV_DIR_RISING: case IIO_EV_DIR_RISING:
mutex_lock(&st->lock);
*val = st->act_threshold; *val = st->act_threshold;
mutex_unlock(&st->lock);
return IIO_VAL_INT; return IIO_VAL_INT;
case IIO_EV_DIR_FALLING: case IIO_EV_DIR_FALLING:
mutex_lock(&st->lock);
*val = st->inact_threshold; *val = st->inact_threshold;
mutex_unlock(&st->lock);
return IIO_VAL_INT; return IIO_VAL_INT;
default: default:
return -EINVAL; return -EINVAL;
...@@ -1024,15 +993,11 @@ static int adxl367_read_event_value(struct iio_dev *indio_dev, ...@@ -1024,15 +993,11 @@ static int adxl367_read_event_value(struct iio_dev *indio_dev,
case IIO_EV_INFO_PERIOD: case IIO_EV_INFO_PERIOD:
switch (dir) { switch (dir) {
case IIO_EV_DIR_RISING: case IIO_EV_DIR_RISING:
mutex_lock(&st->lock);
*val = st->act_time_ms; *val = st->act_time_ms;
mutex_unlock(&st->lock);
*val2 = 1000; *val2 = 1000;
return IIO_VAL_FRACTIONAL; return IIO_VAL_FRACTIONAL;
case IIO_EV_DIR_FALLING: case IIO_EV_DIR_FALLING:
mutex_lock(&st->lock);
*val = st->inact_time_ms; *val = st->inact_time_ms;
mutex_unlock(&st->lock);
*val2 = 1000; *val2 = 1000;
return IIO_VAL_FRACTIONAL; return IIO_VAL_FRACTIONAL;
default: default:
...@@ -1110,9 +1075,7 @@ static int adxl367_write_event_config(struct iio_dev *indio_dev, ...@@ -1110,9 +1075,7 @@ static int adxl367_write_event_config(struct iio_dev *indio_dev,
enum iio_event_direction dir, enum iio_event_direction dir,
int state) int state)
{ {
struct adxl367_state *st = iio_priv(indio_dev);
enum adxl367_activity_type act; enum adxl367_activity_type act;
int ret;
switch (dir) { switch (dir) {
case IIO_EV_DIR_RISING: case IIO_EV_DIR_RISING:
...@@ -1125,33 +1088,28 @@ static int adxl367_write_event_config(struct iio_dev *indio_dev, ...@@ -1125,33 +1088,28 @@ static int adxl367_write_event_config(struct iio_dev *indio_dev,
return -EINVAL; return -EINVAL;
} }
ret = iio_device_claim_direct_mode(indio_dev); iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
if (ret) struct adxl367_state *st = iio_priv(indio_dev);
return ret; int ret;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
ret = adxl367_set_measure_en(st, false); ret = adxl367_set_measure_en(st, false);
if (ret) if (ret)
goto out; return ret;
ret = adxl367_set_act_interrupt_en(st, act, state); ret = adxl367_set_act_interrupt_en(st, act, state);
if (ret) if (ret)
goto out; return ret;
ret = adxl367_set_act_en(st, act, state ? ADCL367_ACT_REF_ENABLED ret = adxl367_set_act_en(st, act, state ? ADCL367_ACT_REF_ENABLED
: ADXL367_ACT_DISABLED); : ADXL367_ACT_DISABLED);
if (ret) if (ret)
goto out;
ret = adxl367_set_measure_en(st, true);
out:
mutex_unlock(&st->lock);
iio_device_release_direct_mode(indio_dev);
return ret; return ret;
return adxl367_set_measure_en(st, true);
}
unreachable();
} }
static ssize_t adxl367_get_fifo_enabled(struct device *dev, static ssize_t adxl367_get_fifo_enabled(struct device *dev,
...@@ -1176,9 +1134,8 @@ static ssize_t adxl367_get_fifo_watermark(struct device *dev, ...@@ -1176,9 +1134,8 @@ static ssize_t adxl367_get_fifo_watermark(struct device *dev,
struct adxl367_state *st = iio_priv(dev_to_iio_dev(dev)); struct adxl367_state *st = iio_priv(dev_to_iio_dev(dev));
unsigned int fifo_watermark; unsigned int fifo_watermark;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
fifo_watermark = st->fifo_watermark; fifo_watermark = st->fifo_watermark;
mutex_unlock(&st->lock);
return sysfs_emit(buf, "%d\n", fifo_watermark); return sysfs_emit(buf, "%d\n", fifo_watermark);
} }
...@@ -1207,22 +1164,17 @@ static int adxl367_set_watermark(struct iio_dev *indio_dev, unsigned int val) ...@@ -1207,22 +1164,17 @@ static int adxl367_set_watermark(struct iio_dev *indio_dev, unsigned int val)
if (val > ADXL367_FIFO_MAX_WATERMARK) if (val > ADXL367_FIFO_MAX_WATERMARK)
return -EINVAL; return -EINVAL;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
ret = adxl367_set_measure_en(st, false); ret = adxl367_set_measure_en(st, false);
if (ret) if (ret)
goto out; return ret;
ret = adxl367_set_fifo_watermark(st, val); ret = adxl367_set_fifo_watermark(st, val);
if (ret) if (ret)
goto out;
ret = adxl367_set_measure_en(st, true);
out:
mutex_unlock(&st->lock);
return ret; return ret;
return adxl367_set_measure_en(st, true);
} }
static bool adxl367_find_mask_fifo_format(const unsigned long *scan_mask, static bool adxl367_find_mask_fifo_format(const unsigned long *scan_mask,
...@@ -1253,27 +1205,24 @@ static int adxl367_update_scan_mode(struct iio_dev *indio_dev, ...@@ -1253,27 +1205,24 @@ static int adxl367_update_scan_mode(struct iio_dev *indio_dev,
if (!adxl367_find_mask_fifo_format(active_scan_mask, &fifo_format)) if (!adxl367_find_mask_fifo_format(active_scan_mask, &fifo_format))
return -EINVAL; return -EINVAL;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
ret = adxl367_set_measure_en(st, false); ret = adxl367_set_measure_en(st, false);
if (ret) if (ret)
goto out; return ret;
ret = adxl367_set_fifo_format(st, fifo_format); ret = adxl367_set_fifo_format(st, fifo_format);
if (ret) if (ret)
goto out; return ret;
ret = adxl367_set_measure_en(st, true); ret = adxl367_set_measure_en(st, true);
if (ret) if (ret)
goto out; return ret;
st->fifo_set_size = bitmap_weight(active_scan_mask, st->fifo_set_size = bitmap_weight(active_scan_mask,
indio_dev->masklength); indio_dev->masklength);
out: return 0;
mutex_unlock(&st->lock);
return ret;
} }
static int adxl367_buffer_postenable(struct iio_dev *indio_dev) static int adxl367_buffer_postenable(struct iio_dev *indio_dev)
...@@ -1281,31 +1230,26 @@ static int adxl367_buffer_postenable(struct iio_dev *indio_dev) ...@@ -1281,31 +1230,26 @@ static int adxl367_buffer_postenable(struct iio_dev *indio_dev)
struct adxl367_state *st = iio_priv(indio_dev); struct adxl367_state *st = iio_priv(indio_dev);
int ret; int ret;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
ret = adxl367_set_temp_adc_mask_en(st, indio_dev->active_scan_mask, ret = adxl367_set_temp_adc_mask_en(st, indio_dev->active_scan_mask,
true); true);
if (ret) if (ret)
goto out; return ret;
ret = adxl367_set_measure_en(st, false); ret = adxl367_set_measure_en(st, false);
if (ret) if (ret)
goto out; return ret;
ret = adxl367_set_fifo_watermark_interrupt_en(st, true); ret = adxl367_set_fifo_watermark_interrupt_en(st, true);
if (ret) if (ret)
goto out; return ret;
ret = adxl367_set_fifo_mode(st, ADXL367_FIFO_MODE_STREAM); ret = adxl367_set_fifo_mode(st, ADXL367_FIFO_MODE_STREAM);
if (ret) if (ret)
goto out;
ret = adxl367_set_measure_en(st, true);
out:
mutex_unlock(&st->lock);
return ret; return ret;
return adxl367_set_measure_en(st, true);
} }
static int adxl367_buffer_predisable(struct iio_dev *indio_dev) static int adxl367_buffer_predisable(struct iio_dev *indio_dev)
...@@ -1313,31 +1257,26 @@ static int adxl367_buffer_predisable(struct iio_dev *indio_dev) ...@@ -1313,31 +1257,26 @@ static int adxl367_buffer_predisable(struct iio_dev *indio_dev)
struct adxl367_state *st = iio_priv(indio_dev); struct adxl367_state *st = iio_priv(indio_dev);
int ret; int ret;
mutex_lock(&st->lock); guard(mutex)(&st->lock);
ret = adxl367_set_measure_en(st, false); ret = adxl367_set_measure_en(st, false);
if (ret) if (ret)
goto out; return ret;
ret = adxl367_set_fifo_mode(st, ADXL367_FIFO_MODE_DISABLED); ret = adxl367_set_fifo_mode(st, ADXL367_FIFO_MODE_DISABLED);
if (ret) if (ret)
goto out; return ret;
ret = adxl367_set_fifo_watermark_interrupt_en(st, false); ret = adxl367_set_fifo_watermark_interrupt_en(st, false);
if (ret) if (ret)
goto out; return ret;
ret = adxl367_set_measure_en(st, true); ret = adxl367_set_measure_en(st, true);
if (ret) if (ret)
goto out; return ret;
ret = adxl367_set_temp_adc_mask_en(st, indio_dev->active_scan_mask, return adxl367_set_temp_adc_mask_en(st, indio_dev->active_scan_mask,
false); false);
out:
mutex_unlock(&st->lock);
return ret;
} }
static const struct iio_buffer_setup_ops adxl367_buffer_ops = { static const struct iio_buffer_setup_ops adxl367_buffer_ops = {
......
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