Commit fb1f1971 authored by Jonathan Cameron's avatar Jonathan Cameron

iio: proximity: sx9310: Use automated cleanup for locks and IIO mode claiming.

This simplifies error handling paths and generallly removes a bunch
of boilerplate.
Reviewed-by: default avatarNuno Sa <nuno.a@analog.com>
Link: https://lore.kernel.org/r/20240128150537.44592-9-jic23@kernel.orgSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent d62f68c1
...@@ -337,28 +337,19 @@ static int sx9310_read_raw(struct iio_dev *indio_dev, ...@@ -337,28 +337,19 @@ static int sx9310_read_raw(struct iio_dev *indio_dev,
int *val2, long mask) int *val2, long mask)
{ {
struct sx_common_data *data = iio_priv(indio_dev); struct sx_common_data *data = iio_priv(indio_dev);
int ret;
if (chan->type != IIO_PROXIMITY) if (chan->type != IIO_PROXIMITY)
return -EINVAL; return -EINVAL;
switch (mask) { switch (mask) {
case IIO_CHAN_INFO_RAW: case IIO_CHAN_INFO_RAW:
ret = iio_device_claim_direct_mode(indio_dev); iio_device_claim_direct_scoped(return -EBUSY, indio_dev)
if (ret) return sx_common_read_proximity(data, chan, val);
return ret; unreachable();
ret = sx_common_read_proximity(data, chan, val);
iio_device_release_direct_mode(indio_dev);
return ret;
case IIO_CHAN_INFO_HARDWAREGAIN: case IIO_CHAN_INFO_HARDWAREGAIN:
ret = iio_device_claim_direct_mode(indio_dev); iio_device_claim_direct_scoped(return -EBUSY, indio_dev)
if (ret) return sx9310_read_gain(data, chan, val);
return ret; unreachable();
ret = sx9310_read_gain(data, chan, val);
iio_device_release_direct_mode(indio_dev);
return ret;
case IIO_CHAN_INFO_SAMP_FREQ: case IIO_CHAN_INFO_SAMP_FREQ:
return sx9310_read_samp_freq(data, val, val2); return sx9310_read_samp_freq(data, val, val2);
default: default:
...@@ -546,12 +537,10 @@ static int sx9310_write_thresh(struct sx_common_data *data, ...@@ -546,12 +537,10 @@ static int sx9310_write_thresh(struct sx_common_data *data,
return -EINVAL; return -EINVAL;
regval = FIELD_PREP(SX9310_REG_PROX_CTRL8_9_PTHRESH_MASK, regval); regval = FIELD_PREP(SX9310_REG_PROX_CTRL8_9_PTHRESH_MASK, regval);
mutex_lock(&data->mutex);
ret = regmap_update_bits(data->regmap, reg,
SX9310_REG_PROX_CTRL8_9_PTHRESH_MASK, regval);
mutex_unlock(&data->mutex);
return ret; guard(mutex)(&data->mutex);
return regmap_update_bits(data->regmap, reg,
SX9310_REG_PROX_CTRL8_9_PTHRESH_MASK, regval);
} }
static int sx9310_write_hysteresis(struct sx_common_data *data, static int sx9310_write_hysteresis(struct sx_common_data *data,
...@@ -576,17 +565,14 @@ static int sx9310_write_hysteresis(struct sx_common_data *data, ...@@ -576,17 +565,14 @@ static int sx9310_write_hysteresis(struct sx_common_data *data,
return -EINVAL; return -EINVAL;
hyst = FIELD_PREP(SX9310_REG_PROX_CTRL10_HYST_MASK, hyst); hyst = FIELD_PREP(SX9310_REG_PROX_CTRL10_HYST_MASK, hyst);
mutex_lock(&data->mutex);
ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL10,
SX9310_REG_PROX_CTRL10_HYST_MASK, hyst);
mutex_unlock(&data->mutex);
return ret; guard(mutex)(&data->mutex);
return regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL10,
SX9310_REG_PROX_CTRL10_HYST_MASK, hyst);
} }
static int sx9310_write_far_debounce(struct sx_common_data *data, int val) static int sx9310_write_far_debounce(struct sx_common_data *data, int val)
{ {
int ret;
unsigned int regval; unsigned int regval;
if (val > 0) if (val > 0)
...@@ -596,18 +582,14 @@ static int sx9310_write_far_debounce(struct sx_common_data *data, int val) ...@@ -596,18 +582,14 @@ static int sx9310_write_far_debounce(struct sx_common_data *data, int val)
regval = FIELD_PREP(SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_MASK, val); regval = FIELD_PREP(SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_MASK, val);
mutex_lock(&data->mutex); guard(mutex)(&data->mutex);
ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL10, return regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL10,
SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_MASK, SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_MASK,
regval); regval);
mutex_unlock(&data->mutex);
return ret;
} }
static int sx9310_write_close_debounce(struct sx_common_data *data, int val) static int sx9310_write_close_debounce(struct sx_common_data *data, int val)
{ {
int ret;
unsigned int regval; unsigned int regval;
if (val > 0) if (val > 0)
...@@ -617,13 +599,10 @@ static int sx9310_write_close_debounce(struct sx_common_data *data, int val) ...@@ -617,13 +599,10 @@ static int sx9310_write_close_debounce(struct sx_common_data *data, int val)
regval = FIELD_PREP(SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_MASK, val); regval = FIELD_PREP(SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_MASK, val);
mutex_lock(&data->mutex); guard(mutex)(&data->mutex);
ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL10, return regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL10,
SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_MASK, SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_MASK,
regval); regval);
mutex_unlock(&data->mutex);
return ret;
} }
static int sx9310_write_event_val(struct iio_dev *indio_dev, static int sx9310_write_event_val(struct iio_dev *indio_dev,
...@@ -658,7 +637,7 @@ static int sx9310_write_event_val(struct iio_dev *indio_dev, ...@@ -658,7 +637,7 @@ static int sx9310_write_event_val(struct iio_dev *indio_dev,
static int sx9310_set_samp_freq(struct sx_common_data *data, int val, int val2) static int sx9310_set_samp_freq(struct sx_common_data *data, int val, int val2)
{ {
int i, ret; int i;
for (i = 0; i < ARRAY_SIZE(sx9310_samp_freq_table); i++) for (i = 0; i < ARRAY_SIZE(sx9310_samp_freq_table); i++)
if (val == sx9310_samp_freq_table[i].val && if (val == sx9310_samp_freq_table[i].val &&
...@@ -668,23 +647,17 @@ static int sx9310_set_samp_freq(struct sx_common_data *data, int val, int val2) ...@@ -668,23 +647,17 @@ static int sx9310_set_samp_freq(struct sx_common_data *data, int val, int val2)
if (i == ARRAY_SIZE(sx9310_samp_freq_table)) if (i == ARRAY_SIZE(sx9310_samp_freq_table))
return -EINVAL; return -EINVAL;
mutex_lock(&data->mutex); guard(mutex)(&data->mutex);
return regmap_update_bits(
ret = regmap_update_bits(
data->regmap, SX9310_REG_PROX_CTRL0, data->regmap, SX9310_REG_PROX_CTRL0,
SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK,
FIELD_PREP(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, i)); FIELD_PREP(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, i));
mutex_unlock(&data->mutex);
return ret;
} }
static int sx9310_write_gain(struct sx_common_data *data, static int sx9310_write_gain(struct sx_common_data *data,
const struct iio_chan_spec *chan, int val) const struct iio_chan_spec *chan, int val)
{ {
unsigned int gain, mask; unsigned int gain, mask;
int ret;
gain = ilog2(val); gain = ilog2(val);
...@@ -703,12 +676,9 @@ static int sx9310_write_gain(struct sx_common_data *data, ...@@ -703,12 +676,9 @@ static int sx9310_write_gain(struct sx_common_data *data,
return -EINVAL; return -EINVAL;
} }
mutex_lock(&data->mutex); guard(mutex)(&data->mutex);
ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL3, mask, return regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL3, mask,
gain); gain);
mutex_unlock(&data->mutex);
return ret;
} }
static int sx9310_write_raw(struct iio_dev *indio_dev, static int sx9310_write_raw(struct iio_dev *indio_dev,
...@@ -969,22 +939,18 @@ static int sx9310_suspend(struct device *dev) ...@@ -969,22 +939,18 @@ static int sx9310_suspend(struct device *dev)
disable_irq_nosync(data->client->irq); disable_irq_nosync(data->client->irq);
mutex_lock(&data->mutex); guard(mutex)(&data->mutex);
ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0,
&data->suspend_ctrl); &data->suspend_ctrl);
if (ret) if (ret)
goto out; return ret;
ctrl0 = data->suspend_ctrl & ~SX9310_REG_PROX_CTRL0_SENSOREN_MASK; ctrl0 = data->suspend_ctrl & ~SX9310_REG_PROX_CTRL0_SENSOREN_MASK;
ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0); ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
if (ret) if (ret)
goto out; return ret;
ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 0);
out: return regmap_write(data->regmap, SX9310_REG_PAUSE, 0);
mutex_unlock(&data->mutex);
return ret;
} }
static int sx9310_resume(struct device *dev) static int sx9310_resume(struct device *dev)
...@@ -992,18 +958,16 @@ static int sx9310_resume(struct device *dev) ...@@ -992,18 +958,16 @@ static int sx9310_resume(struct device *dev)
struct sx_common_data *data = iio_priv(dev_get_drvdata(dev)); struct sx_common_data *data = iio_priv(dev_get_drvdata(dev));
int ret; int ret;
mutex_lock(&data->mutex); scoped_guard(mutex, &data->mutex) {
ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 1); ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 1);
if (ret) if (ret)
goto out; return ret;
ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
data->suspend_ctrl);
out: ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
mutex_unlock(&data->mutex); data->suspend_ctrl);
if (ret) if (ret)
return ret; return ret;
}
enable_irq(data->client->irq); enable_irq(data->client->irq);
return 0; return 0;
......
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