aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVignesh R <vigneshr@ti.com>2016-08-17 08:13:00 -0400
committerJonathan Cameron <jic23@kernel.org>2016-08-21 14:47:58 -0400
commit90c43ec6997a892448f1f86180a515f59cafd8a3 (patch)
treebd0c0b2d354ee2e7dcdac528c03b137d581a2381
parentca64d4bc80a88845f7e1e266dbff798f928bcc06 (diff)
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: 7ca6740cd1cd4 ("mfd: input: iio: ti_amm335x: Rework TSC/ADC synchronization") Signed-off-by: Vignesh R <vigneshr@ti.com> Cc: <Stable@vger.kernel.org> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
-rw-r--r--drivers/iio/adc/ti_am335x_adc.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/drivers/iio/adc/ti_am335x_adc.c b/drivers/iio/adc/ti_am335x_adc.c
index 8a368756881b..bed9977a1863 100644
--- a/drivers/iio/adc/ti_am335x_adc.c
+++ b/drivers/iio/adc/ti_am335x_adc.c
@@ -32,6 +32,7 @@
32 32
33struct tiadc_device { 33struct tiadc_device {
34 struct ti_tscadc_dev *mfd_tscadc; 34 struct ti_tscadc_dev *mfd_tscadc;
35 struct mutex fifo1_lock; /* to protect fifo access */
35 int channels; 36 int channels;
36 u8 channel_line[8]; 37 u8 channel_line[8];
37 u8 channel_step[8]; 38 u8 channel_step[8];
@@ -359,6 +360,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,
359 int *val, int *val2, long mask) 360 int *val, int *val2, long mask)
360{ 361{
361 struct tiadc_device *adc_dev = iio_priv(indio_dev); 362 struct tiadc_device *adc_dev = iio_priv(indio_dev);
363 int ret = IIO_VAL_INT;
362 int i, map_val; 364 int i, map_val;
363 unsigned int fifo1count, read, stepid; 365 unsigned int fifo1count, read, stepid;
364 bool found = false; 366 bool found = false;
@@ -372,6 +374,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,
372 if (!step_en) 374 if (!step_en)
373 return -EINVAL; 375 return -EINVAL;
374 376
377 mutex_lock(&adc_dev->fifo1_lock);
375 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); 378 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
376 while (fifo1count--) 379 while (fifo1count--)
377 tiadc_readl(adc_dev, REG_FIFO1); 380 tiadc_readl(adc_dev, REG_FIFO1);
@@ -388,7 +391,8 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,
388 391
389 if (time_after(jiffies, timeout)) { 392 if (time_after(jiffies, timeout)) {
390 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc); 393 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
391 return -EAGAIN; 394 ret = -EAGAIN;
395 goto err_unlock;
392 } 396 }
393 } 397 }
394 map_val = adc_dev->channel_step[chan->scan_index]; 398 map_val = adc_dev->channel_step[chan->scan_index];
@@ -414,8 +418,11 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,
414 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc); 418 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
415 419
416 if (found == false) 420 if (found == false)
417 return -EBUSY; 421 ret = -EBUSY;
418 return IIO_VAL_INT; 422
423err_unlock:
424 mutex_unlock(&adc_dev->fifo1_lock);
425 return ret;
419} 426}
420 427
421static const struct iio_info tiadc_info = { 428static const struct iio_info tiadc_info = {
@@ -483,6 +490,7 @@ static int tiadc_probe(struct platform_device *pdev)
483 490
484 tiadc_step_config(indio_dev); 491 tiadc_step_config(indio_dev);
485 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD); 492 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
493 mutex_init(&adc_dev->fifo1_lock);
486 494
487 err = tiadc_channel_init(indio_dev, adc_dev->channels); 495 err = tiadc_channel_init(indio_dev, adc_dev->channels);
488 if (err < 0) 496 if (err < 0)