Commit 15aacc98 authored by Nuno Sa's avatar Nuno Sa Committed by Jonathan Cameron

iio: adis: add helpers for locking

Add some helpers to lock and unlock the device. As this is such a simple
change, we update all the users that were using the lock already in this
patch.
Signed-off-by: default avatarNuno Sa <nuno.sa@analog.com>
Link: https://lore.kernel.org/r/20210218114039.216091-5-nuno.sa@analog.comSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 3b15e6a5
...@@ -505,7 +505,6 @@ static int adis16400_write_raw(struct iio_dev *indio_dev, ...@@ -505,7 +505,6 @@ static int adis16400_write_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan, int val, int val2, long info) struct iio_chan_spec const *chan, int val, int val2, long info)
{ {
struct adis16400_state *st = iio_priv(indio_dev); struct adis16400_state *st = iio_priv(indio_dev);
struct mutex *slock = &st->adis.state_lock;
int ret, sps; int ret, sps;
switch (info) { switch (info) {
...@@ -518,18 +517,18 @@ static int adis16400_write_raw(struct iio_dev *indio_dev, ...@@ -518,18 +517,18 @@ static int adis16400_write_raw(struct iio_dev *indio_dev,
* Need to cache values so we can update if the frequency * Need to cache values so we can update if the frequency
* changes. * changes.
*/ */
mutex_lock(slock); adis_dev_lock(&st->adis);
st->filt_int = val; st->filt_int = val;
/* Work out update to current value */ /* Work out update to current value */
sps = st->variant->get_freq(st); sps = st->variant->get_freq(st);
if (sps < 0) { if (sps < 0) {
mutex_unlock(slock); adis_dev_unlock(&st->adis);
return sps; return sps;
} }
ret = __adis16400_set_filter(indio_dev, sps, ret = __adis16400_set_filter(indio_dev, sps,
val * 1000 + val2 / 1000); val * 1000 + val2 / 1000);
mutex_unlock(slock); adis_dev_unlock(&st->adis);
return ret; return ret;
case IIO_CHAN_INFO_SAMP_FREQ: case IIO_CHAN_INFO_SAMP_FREQ:
sps = val * 1000 + val2 / 1000; sps = val * 1000 + val2 / 1000;
...@@ -537,9 +536,9 @@ static int adis16400_write_raw(struct iio_dev *indio_dev, ...@@ -537,9 +536,9 @@ static int adis16400_write_raw(struct iio_dev *indio_dev,
if (sps <= 0) if (sps <= 0)
return -EINVAL; return -EINVAL;
mutex_lock(slock); adis_dev_lock(&st->adis);
ret = st->variant->set_freq(st, sps); ret = st->variant->set_freq(st, sps);
mutex_unlock(slock); adis_dev_unlock(&st->adis);
return ret; return ret;
default: default:
return -EINVAL; return -EINVAL;
...@@ -550,7 +549,6 @@ static int adis16400_read_raw(struct iio_dev *indio_dev, ...@@ -550,7 +549,6 @@ static int adis16400_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan, int *val, int *val2, long info) struct iio_chan_spec const *chan, int *val, int *val2, long info)
{ {
struct adis16400_state *st = iio_priv(indio_dev); struct adis16400_state *st = iio_priv(indio_dev);
struct mutex *slock = &st->adis.state_lock;
int16_t val16; int16_t val16;
int ret; int ret;
...@@ -606,17 +604,17 @@ static int adis16400_read_raw(struct iio_dev *indio_dev, ...@@ -606,17 +604,17 @@ static int adis16400_read_raw(struct iio_dev *indio_dev,
*val = st->variant->temp_offset; *val = st->variant->temp_offset;
return IIO_VAL_INT; return IIO_VAL_INT;
case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
mutex_lock(slock); adis_dev_lock(&st->adis);
/* Need both the number of taps and the sampling frequency */ /* Need both the number of taps and the sampling frequency */
ret = __adis_read_reg_16(&st->adis, ret = __adis_read_reg_16(&st->adis,
ADIS16400_SENS_AVG, ADIS16400_SENS_AVG,
&val16); &val16);
if (ret) { if (ret) {
mutex_unlock(slock); adis_dev_unlock(&st->adis);
return ret; return ret;
} }
ret = st->variant->get_freq(st); ret = st->variant->get_freq(st);
mutex_unlock(slock); adis_dev_unlock(&st->adis);
if (ret) if (ret)
return ret; return ret;
ret /= adis16400_3db_divisors[val16 & 0x07]; ret /= adis16400_3db_divisors[val16 & 0x07];
...@@ -624,9 +622,9 @@ static int adis16400_read_raw(struct iio_dev *indio_dev, ...@@ -624,9 +622,9 @@ static int adis16400_read_raw(struct iio_dev *indio_dev,
*val2 = (ret % 1000) * 1000; *val2 = (ret % 1000) * 1000;
return IIO_VAL_INT_PLUS_MICRO; return IIO_VAL_INT_PLUS_MICRO;
case IIO_CHAN_INFO_SAMP_FREQ: case IIO_CHAN_INFO_SAMP_FREQ:
mutex_lock(slock); adis_dev_lock(&st->adis);
ret = st->variant->get_freq(st); ret = st->variant->get_freq(st);
mutex_unlock(slock); adis_dev_unlock(&st->adis);
if (ret) if (ret)
return ret; return ret;
*val = ret / 1000; *val = ret / 1000;
......
...@@ -263,7 +263,7 @@ static int adis16475_get_freq(struct adis16475 *st, u32 *freq) ...@@ -263,7 +263,7 @@ static int adis16475_get_freq(struct adis16475 *st, u32 *freq)
u16 dec; u16 dec;
u32 sample_rate = st->clk_freq; u32 sample_rate = st->clk_freq;
mutex_lock(&st->adis.state_lock); adis_dev_lock(&st->adis);
if (st->sync_mode == ADIS16475_SYNC_SCALED) { if (st->sync_mode == ADIS16475_SYNC_SCALED) {
u16 sync_scale; u16 sync_scale;
...@@ -279,13 +279,13 @@ static int adis16475_get_freq(struct adis16475 *st, u32 *freq) ...@@ -279,13 +279,13 @@ static int adis16475_get_freq(struct adis16475 *st, u32 *freq)
if (ret) if (ret)
goto error; goto error;
mutex_unlock(&st->adis.state_lock); adis_dev_unlock(&st->adis);
*freq = DIV_ROUND_CLOSEST(sample_rate, dec + 1); *freq = DIV_ROUND_CLOSEST(sample_rate, dec + 1);
return 0; return 0;
error: error:
mutex_unlock(&st->adis.state_lock); adis_dev_unlock(&st->adis);
return ret; return ret;
} }
...@@ -298,7 +298,7 @@ static int adis16475_set_freq(struct adis16475 *st, const u32 freq) ...@@ -298,7 +298,7 @@ static int adis16475_set_freq(struct adis16475 *st, const u32 freq)
if (!freq) if (!freq)
return -EINVAL; return -EINVAL;
mutex_lock(&st->adis.state_lock); adis_dev_lock(&st->adis);
/* /*
* When using sync scaled mode, the input clock needs to be scaled so that we have * When using sync scaled mode, the input clock needs to be scaled so that we have
* an IMU sample rate between (optimally) 1900 and 2100. After this, we can use the * an IMU sample rate between (optimally) 1900 and 2100. After this, we can use the
...@@ -366,7 +366,7 @@ static int adis16475_set_freq(struct adis16475 *st, const u32 freq) ...@@ -366,7 +366,7 @@ static int adis16475_set_freq(struct adis16475 *st, const u32 freq)
return 0; return 0;
error: error:
mutex_unlock(&st->adis.state_lock); adis_dev_unlock(&st->adis);
return ret; return ret;
} }
......
...@@ -329,7 +329,7 @@ static int adis16480_set_freq(struct iio_dev *indio_dev, int val, int val2) ...@@ -329,7 +329,7 @@ static int adis16480_set_freq(struct iio_dev *indio_dev, int val, int val2)
if (t == 0) if (t == 0)
return -EINVAL; return -EINVAL;
mutex_lock(&st->adis.state_lock); adis_dev_lock(&st->adis);
/* /*
* When using PPS mode, the input clock needs to be scaled so that we have an IMU * When using PPS mode, the input clock needs to be scaled so that we have an IMU
* sample rate between (optimally) 4000 and 4250. After this, we can use the * sample rate between (optimally) 4000 and 4250. After this, we can use the
...@@ -386,7 +386,7 @@ static int adis16480_set_freq(struct iio_dev *indio_dev, int val, int val2) ...@@ -386,7 +386,7 @@ static int adis16480_set_freq(struct iio_dev *indio_dev, int val, int val2)
ret = __adis_write_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, t); ret = __adis_write_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, t);
error: error:
mutex_unlock(&st->adis.state_lock); adis_dev_unlock(&st->adis);
return ret; return ret;
} }
...@@ -397,7 +397,7 @@ static int adis16480_get_freq(struct iio_dev *indio_dev, int *val, int *val2) ...@@ -397,7 +397,7 @@ static int adis16480_get_freq(struct iio_dev *indio_dev, int *val, int *val2)
int ret; int ret;
unsigned int freq, sample_rate = st->clk_freq; unsigned int freq, sample_rate = st->clk_freq;
mutex_lock(&st->adis.state_lock); adis_dev_lock(&st->adis);
if (st->clk_mode == ADIS16480_CLK_PPS) { if (st->clk_mode == ADIS16480_CLK_PPS) {
u16 sync_scale; u16 sync_scale;
...@@ -413,7 +413,7 @@ static int adis16480_get_freq(struct iio_dev *indio_dev, int *val, int *val2) ...@@ -413,7 +413,7 @@ static int adis16480_get_freq(struct iio_dev *indio_dev, int *val, int *val2)
if (ret) if (ret)
goto error; goto error;
mutex_unlock(&st->adis.state_lock); adis_dev_unlock(&st->adis);
freq = DIV_ROUND_CLOSEST(sample_rate, (t + 1)); freq = DIV_ROUND_CLOSEST(sample_rate, (t + 1));
...@@ -422,7 +422,7 @@ static int adis16480_get_freq(struct iio_dev *indio_dev, int *val, int *val2) ...@@ -422,7 +422,7 @@ static int adis16480_get_freq(struct iio_dev *indio_dev, int *val, int *val2)
return IIO_VAL_INT_PLUS_MICRO; return IIO_VAL_INT_PLUS_MICRO;
error: error:
mutex_unlock(&st->adis.state_lock); adis_dev_unlock(&st->adis);
return ret; return ret;
} }
...@@ -598,7 +598,6 @@ static int adis16480_set_filter_freq(struct iio_dev *indio_dev, ...@@ -598,7 +598,6 @@ static int adis16480_set_filter_freq(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan, unsigned int freq) const struct iio_chan_spec *chan, unsigned int freq)
{ {
struct adis16480 *st = iio_priv(indio_dev); struct adis16480 *st = iio_priv(indio_dev);
struct mutex *slock = &st->adis.state_lock;
unsigned int enable_mask, offset, reg; unsigned int enable_mask, offset, reg;
unsigned int diff, best_diff; unsigned int diff, best_diff;
unsigned int i, best_freq; unsigned int i, best_freq;
...@@ -609,7 +608,7 @@ static int adis16480_set_filter_freq(struct iio_dev *indio_dev, ...@@ -609,7 +608,7 @@ static int adis16480_set_filter_freq(struct iio_dev *indio_dev,
offset = ad16480_filter_data[chan->scan_index][1]; offset = ad16480_filter_data[chan->scan_index][1];
enable_mask = BIT(offset + 2); enable_mask = BIT(offset + 2);
mutex_lock(slock); adis_dev_lock(&st->adis);
ret = __adis_read_reg_16(&st->adis, reg, &val); ret = __adis_read_reg_16(&st->adis, reg, &val);
if (ret) if (ret)
...@@ -637,7 +636,7 @@ static int adis16480_set_filter_freq(struct iio_dev *indio_dev, ...@@ -637,7 +636,7 @@ static int adis16480_set_filter_freq(struct iio_dev *indio_dev,
ret = __adis_write_reg_16(&st->adis, reg, val); ret = __adis_write_reg_16(&st->adis, reg, val);
out_unlock: out_unlock:
mutex_unlock(slock); adis_dev_unlock(&st->adis);
return ret; return ret;
} }
......
...@@ -428,6 +428,16 @@ static inline int adis_initial_startup(struct adis *adis) ...@@ -428,6 +428,16 @@ static inline int adis_initial_startup(struct adis *adis)
return ret; return ret;
} }
static inline void adis_dev_lock(struct adis *adis)
{
mutex_lock(&adis->state_lock);
}
static inline void adis_dev_unlock(struct adis *adis)
{
mutex_unlock(&adis->state_lock);
}
int adis_single_conversion(struct iio_dev *indio_dev, int adis_single_conversion(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan, unsigned int error_mask, const struct iio_chan_spec *chan, unsigned int error_mask,
int *val); int *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