Commit c991bf9b authored by Alexandru Ardelean's avatar Alexandru Ardelean Committed by Jonathan Cameron

iio: dac: ad7303: replace mlock with own lock

This change replaces indio_dev's mlock with the driver's own lock. The lock
is mostly needed to protect state when changing the `dac_cache` info.
The lock has been extended to `ad7303_read_raw()`, to make sure that the
cache is updated if an SPI-write is already in progress.
Signed-off-by: default avatarAlexandru Ardelean <alexandru.ardelean@analog.com>
Signed-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent af8dc7bf
...@@ -41,6 +41,7 @@ struct ad7303_state { ...@@ -41,6 +41,7 @@ struct ad7303_state {
struct regulator *vdd_reg; struct regulator *vdd_reg;
struct regulator *vref_reg; struct regulator *vref_reg;
struct mutex lock;
/* /*
* DMA (thus cache coherency maintenance) requires the * DMA (thus cache coherency maintenance) requires the
* transfer buffers to live in their own cache lines. * transfer buffers to live in their own cache lines.
...@@ -79,7 +80,7 @@ static ssize_t ad7303_write_dac_powerdown(struct iio_dev *indio_dev, ...@@ -79,7 +80,7 @@ static ssize_t ad7303_write_dac_powerdown(struct iio_dev *indio_dev,
if (ret) if (ret)
return ret; return ret;
mutex_lock(&indio_dev->mlock); mutex_lock(&st->lock);
if (pwr_down) if (pwr_down)
st->config |= AD7303_CFG_POWER_DOWN(chan->channel); st->config |= AD7303_CFG_POWER_DOWN(chan->channel);
...@@ -90,7 +91,7 @@ static ssize_t ad7303_write_dac_powerdown(struct iio_dev *indio_dev, ...@@ -90,7 +91,7 @@ static ssize_t ad7303_write_dac_powerdown(struct iio_dev *indio_dev,
* mode, so just write one of the DAC channels again */ * mode, so just write one of the DAC channels again */
ad7303_write(st, chan->channel, st->dac_cache[chan->channel]); ad7303_write(st, chan->channel, st->dac_cache[chan->channel]);
mutex_unlock(&indio_dev->mlock); mutex_unlock(&st->lock);
return len; return len;
} }
...@@ -116,7 +117,9 @@ static int ad7303_read_raw(struct iio_dev *indio_dev, ...@@ -116,7 +117,9 @@ static int ad7303_read_raw(struct iio_dev *indio_dev,
switch (info) { switch (info) {
case IIO_CHAN_INFO_RAW: case IIO_CHAN_INFO_RAW:
mutex_lock(&st->lock);
*val = st->dac_cache[chan->channel]; *val = st->dac_cache[chan->channel];
mutex_unlock(&st->lock);
return IIO_VAL_INT; return IIO_VAL_INT;
case IIO_CHAN_INFO_SCALE: case IIO_CHAN_INFO_SCALE:
vref_uv = ad7303_get_vref(st, chan); vref_uv = ad7303_get_vref(st, chan);
...@@ -144,11 +147,11 @@ static int ad7303_write_raw(struct iio_dev *indio_dev, ...@@ -144,11 +147,11 @@ static int ad7303_write_raw(struct iio_dev *indio_dev,
if (val >= (1 << chan->scan_type.realbits) || val < 0) if (val >= (1 << chan->scan_type.realbits) || val < 0)
return -EINVAL; return -EINVAL;
mutex_lock(&indio_dev->mlock); mutex_lock(&st->lock);
ret = ad7303_write(st, chan->address, val); ret = ad7303_write(st, chan->address, val);
if (ret == 0) if (ret == 0)
st->dac_cache[chan->channel] = val; st->dac_cache[chan->channel] = val;
mutex_unlock(&indio_dev->mlock); mutex_unlock(&st->lock);
break; break;
default: default:
ret = -EINVAL; ret = -EINVAL;
...@@ -211,6 +214,8 @@ static int ad7303_probe(struct spi_device *spi) ...@@ -211,6 +214,8 @@ static int ad7303_probe(struct spi_device *spi)
st->spi = spi; st->spi = spi;
mutex_init(&st->lock);
st->vdd_reg = devm_regulator_get(&spi->dev, "Vdd"); st->vdd_reg = devm_regulator_get(&spi->dev, "Vdd");
if (IS_ERR(st->vdd_reg)) if (IS_ERR(st->vdd_reg))
return PTR_ERR(st->vdd_reg); return PTR_ERR(st->vdd_reg);
......
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