Commit fba730dd authored by Jonathan Cameron's avatar Jonathan Cameron

iio: proximity: sx9360: 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-7-jic23@kernel.orgSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 3d329bcd
...@@ -322,25 +322,16 @@ static int sx9360_read_raw(struct iio_dev *indio_dev, ...@@ -322,25 +322,16 @@ static int sx9360_read_raw(struct iio_dev *indio_dev,
int *val, int *val2, long mask) int *val, int *val2, long mask)
{ {
struct sx_common_data *data = iio_priv(indio_dev); struct sx_common_data *data = iio_priv(indio_dev);
int ret;
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 sx9360_read_gain(data, chan, val);
return ret; unreachable();
ret = sx9360_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 sx9360_read_samp_freq(data, val, val2); return sx9360_read_samp_freq(data, val, val2);
default: default:
...@@ -387,19 +378,15 @@ static int sx9360_read_avail(struct iio_dev *indio_dev, ...@@ -387,19 +378,15 @@ static int sx9360_read_avail(struct iio_dev *indio_dev,
static int sx9360_set_samp_freq(struct sx_common_data *data, static int sx9360_set_samp_freq(struct sx_common_data *data,
int val, int val2) int val, int val2)
{ {
int ret, reg; int reg;
__be16 buf; __be16 buf;
reg = val * 8192 / SX9360_FOSC_HZ + val2 * 8192 / (SX9360_FOSC_MHZ); reg = val * 8192 / SX9360_FOSC_HZ + val2 * 8192 / (SX9360_FOSC_MHZ);
buf = cpu_to_be16(reg); buf = cpu_to_be16(reg);
mutex_lock(&data->mutex); guard(mutex)(&data->mutex);
ret = regmap_bulk_write(data->regmap, SX9360_REG_GNRL_CTRL1, &buf, return regmap_bulk_write(data->regmap, SX9360_REG_GNRL_CTRL1, &buf,
sizeof(buf)); sizeof(buf));
mutex_unlock(&data->mutex);
return ret;
} }
static int sx9360_read_thresh(struct sx_common_data *data, int *val) static int sx9360_read_thresh(struct sx_common_data *data, int *val)
...@@ -510,7 +497,6 @@ static int sx9360_read_event_val(struct iio_dev *indio_dev, ...@@ -510,7 +497,6 @@ static int sx9360_read_event_val(struct iio_dev *indio_dev,
static int sx9360_write_thresh(struct sx_common_data *data, int _val) static int sx9360_write_thresh(struct sx_common_data *data, int _val)
{ {
unsigned int val = _val; unsigned int val = _val;
int ret;
if (val >= 1) if (val >= 1)
val = int_sqrt(2 * val); val = int_sqrt(2 * val);
...@@ -518,11 +504,8 @@ static int sx9360_write_thresh(struct sx_common_data *data, int _val) ...@@ -518,11 +504,8 @@ static int sx9360_write_thresh(struct sx_common_data *data, int _val)
if (val > 0xff) if (val > 0xff)
return -EINVAL; return -EINVAL;
mutex_lock(&data->mutex); guard(mutex)(&data->mutex);
ret = regmap_write(data->regmap, SX9360_REG_PROX_CTRL5, val); return regmap_write(data->regmap, SX9360_REG_PROX_CTRL5, val);
mutex_unlock(&data->mutex);
return ret;
} }
static int sx9360_write_hysteresis(struct sx_common_data *data, int _val) static int sx9360_write_hysteresis(struct sx_common_data *data, int _val)
...@@ -546,18 +529,14 @@ static int sx9360_write_hysteresis(struct sx_common_data *data, int _val) ...@@ -546,18 +529,14 @@ static int sx9360_write_hysteresis(struct sx_common_data *data, int _val)
return -EINVAL; return -EINVAL;
hyst = FIELD_PREP(SX9360_REG_PROX_CTRL4_HYST_MASK, hyst); hyst = FIELD_PREP(SX9360_REG_PROX_CTRL4_HYST_MASK, hyst);
mutex_lock(&data->mutex); guard(mutex)(&data->mutex);
ret = regmap_update_bits(data->regmap, SX9360_REG_PROX_CTRL4, return regmap_update_bits(data->regmap, SX9360_REG_PROX_CTRL4,
SX9360_REG_PROX_CTRL4_HYST_MASK, hyst); SX9360_REG_PROX_CTRL4_HYST_MASK, hyst);
mutex_unlock(&data->mutex);
return ret;
} }
static int sx9360_write_far_debounce(struct sx_common_data *data, int _val) static int sx9360_write_far_debounce(struct sx_common_data *data, int _val)
{ {
unsigned int regval, val = _val; unsigned int regval, val = _val;
int ret;
if (val > 0) if (val > 0)
val = ilog2(val); val = ilog2(val);
...@@ -566,19 +545,15 @@ static int sx9360_write_far_debounce(struct sx_common_data *data, int _val) ...@@ -566,19 +545,15 @@ static int sx9360_write_far_debounce(struct sx_common_data *data, int _val)
regval = FIELD_PREP(SX9360_REG_PROX_CTRL4_FAR_DEBOUNCE_MASK, val); regval = FIELD_PREP(SX9360_REG_PROX_CTRL4_FAR_DEBOUNCE_MASK, val);
mutex_lock(&data->mutex); guard(mutex)(&data->mutex);
ret = regmap_update_bits(data->regmap, SX9360_REG_PROX_CTRL4, return regmap_update_bits(data->regmap, SX9360_REG_PROX_CTRL4,
SX9360_REG_PROX_CTRL4_FAR_DEBOUNCE_MASK, SX9360_REG_PROX_CTRL4_FAR_DEBOUNCE_MASK,
regval); regval);
mutex_unlock(&data->mutex);
return ret;
} }
static int sx9360_write_close_debounce(struct sx_common_data *data, int _val) static int sx9360_write_close_debounce(struct sx_common_data *data, int _val)
{ {
unsigned int regval, val = _val; unsigned int regval, val = _val;
int ret;
if (val > 0) if (val > 0)
val = ilog2(val); val = ilog2(val);
...@@ -587,13 +562,10 @@ static int sx9360_write_close_debounce(struct sx_common_data *data, int _val) ...@@ -587,13 +562,10 @@ static int sx9360_write_close_debounce(struct sx_common_data *data, int _val)
regval = FIELD_PREP(SX9360_REG_PROX_CTRL4_CLOSE_DEBOUNCE_MASK, val); regval = FIELD_PREP(SX9360_REG_PROX_CTRL4_CLOSE_DEBOUNCE_MASK, val);
mutex_lock(&data->mutex); guard(mutex)(&data->mutex);
ret = regmap_update_bits(data->regmap, SX9360_REG_PROX_CTRL4, return regmap_update_bits(data->regmap, SX9360_REG_PROX_CTRL4,
SX9360_REG_PROX_CTRL4_CLOSE_DEBOUNCE_MASK, SX9360_REG_PROX_CTRL4_CLOSE_DEBOUNCE_MASK,
regval); regval);
mutex_unlock(&data->mutex);
return ret;
} }
static int sx9360_write_event_val(struct iio_dev *indio_dev, static int sx9360_write_event_val(struct iio_dev *indio_dev,
...@@ -630,19 +602,15 @@ static int sx9360_write_gain(struct sx_common_data *data, ...@@ -630,19 +602,15 @@ static int sx9360_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, reg; unsigned int gain, reg;
int ret;
gain = ilog2(val); gain = ilog2(val);
reg = SX9360_REG_PROX_CTRL0_PHR + chan->channel; reg = SX9360_REG_PROX_CTRL0_PHR + chan->channel;
gain = FIELD_PREP(SX9360_REG_PROX_CTRL0_GAIN_MASK, gain); gain = FIELD_PREP(SX9360_REG_PROX_CTRL0_GAIN_MASK, gain);
mutex_lock(&data->mutex); guard(mutex)(&data->mutex);
ret = regmap_update_bits(data->regmap, reg, return regmap_update_bits(data->regmap, reg,
SX9360_REG_PROX_CTRL0_GAIN_MASK, SX9360_REG_PROX_CTRL0_GAIN_MASK,
gain); gain);
mutex_unlock(&data->mutex);
return ret;
} }
static int sx9360_write_raw(struct iio_dev *indio_dev, static int sx9360_write_raw(struct iio_dev *indio_dev,
...@@ -827,36 +795,31 @@ static int sx9360_suspend(struct device *dev) ...@@ -827,36 +795,31 @@ static int sx9360_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, SX9360_REG_GNRL_CTRL0, &regval); ret = regmap_read(data->regmap, SX9360_REG_GNRL_CTRL0, &regval);
if (ret < 0)
return ret;
data->suspend_ctrl = data->suspend_ctrl =
FIELD_GET(SX9360_REG_GNRL_CTRL0_PHEN_MASK, regval); FIELD_GET(SX9360_REG_GNRL_CTRL0_PHEN_MASK, regval);
if (ret < 0)
goto out;
/* Disable all phases, send the device to sleep. */ /* Disable all phases, send the device to sleep. */
ret = regmap_write(data->regmap, SX9360_REG_GNRL_CTRL0, 0); return regmap_write(data->regmap, SX9360_REG_GNRL_CTRL0, 0);
out:
mutex_unlock(&data->mutex);
return ret;
} }
static int sx9360_resume(struct device *dev) static int sx9360_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;
mutex_lock(&data->mutex); scoped_guard(mutex, &data->mutex) {
ret = regmap_update_bits(data->regmap, SX9360_REG_GNRL_CTRL0, int ret = regmap_update_bits(data->regmap,
SX9360_REG_GNRL_CTRL0,
SX9360_REG_GNRL_CTRL0_PHEN_MASK, SX9360_REG_GNRL_CTRL0_PHEN_MASK,
data->suspend_ctrl); data->suspend_ctrl);
mutex_unlock(&data->mutex);
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