Commit 90c43ec6 authored by Vignesh R's avatar Vignesh R Committed by Jonathan Cameron

iio: adc: ti_am335x_adc: Protect FIFO1 from concurrent access

It is possible that two or more ADC channels can be simultaneously
requested for raw samples, in which case there can be race in access to
FIFO data resulting in loss of samples.
If am335x_tsc_se_set_once() is called again from tiadc_read_raw(), when
ADC is still acquired to sample one of the channels, the second process
might be put into uninterruptible sleep state. Fix these issues, by
protecting FIFO access and channel configurations with a mutex. Since
tiadc_read_raw() might take anywhere between few microseconds to few
milliseconds to finish execution (depending on averaging and delay
values supplied via DT), its better to use mutex instead of spinlock.

Fixes: 7ca6740c ("mfd: input: iio: ti_amm335x: Rework TSC/ADC synchronization")
Signed-off-by: default avatarVignesh R <vigneshr@ti.com>
Cc: <Stable@vger.kernel.org>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
parent ca64d4bc
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
struct tiadc_device { struct tiadc_device {
struct ti_tscadc_dev *mfd_tscadc; struct ti_tscadc_dev *mfd_tscadc;
struct mutex fifo1_lock; /* to protect fifo access */
int channels; int channels;
u8 channel_line[8]; u8 channel_line[8];
u8 channel_step[8]; u8 channel_step[8];
...@@ -359,6 +360,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev, ...@@ -359,6 +360,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,
int *val, int *val2, long mask) int *val, int *val2, long mask)
{ {
struct tiadc_device *adc_dev = iio_priv(indio_dev); struct tiadc_device *adc_dev = iio_priv(indio_dev);
int ret = IIO_VAL_INT;
int i, map_val; int i, map_val;
unsigned int fifo1count, read, stepid; unsigned int fifo1count, read, stepid;
bool found = false; bool found = false;
...@@ -372,6 +374,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev, ...@@ -372,6 +374,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,
if (!step_en) if (!step_en)
return -EINVAL; return -EINVAL;
mutex_lock(&adc_dev->fifo1_lock);
fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
while (fifo1count--) while (fifo1count--)
tiadc_readl(adc_dev, REG_FIFO1); tiadc_readl(adc_dev, REG_FIFO1);
...@@ -388,7 +391,8 @@ static int tiadc_read_raw(struct iio_dev *indio_dev, ...@@ -388,7 +391,8 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,
if (time_after(jiffies, timeout)) { if (time_after(jiffies, timeout)) {
am335x_tsc_se_adc_done(adc_dev->mfd_tscadc); am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
return -EAGAIN; ret = -EAGAIN;
goto err_unlock;
} }
} }
map_val = adc_dev->channel_step[chan->scan_index]; map_val = adc_dev->channel_step[chan->scan_index];
...@@ -414,8 +418,11 @@ static int tiadc_read_raw(struct iio_dev *indio_dev, ...@@ -414,8 +418,11 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,
am335x_tsc_se_adc_done(adc_dev->mfd_tscadc); am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
if (found == false) if (found == false)
return -EBUSY; ret = -EBUSY;
return IIO_VAL_INT;
err_unlock:
mutex_unlock(&adc_dev->fifo1_lock);
return ret;
} }
static const struct iio_info tiadc_info = { static const struct iio_info tiadc_info = {
...@@ -483,6 +490,7 @@ static int tiadc_probe(struct platform_device *pdev) ...@@ -483,6 +490,7 @@ static int tiadc_probe(struct platform_device *pdev)
tiadc_step_config(indio_dev); tiadc_step_config(indio_dev);
tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD); tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
mutex_init(&adc_dev->fifo1_lock);
err = tiadc_channel_init(indio_dev, adc_dev->channels); err = tiadc_channel_init(indio_dev, adc_dev->channels);
if (err < 0) if (err < 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