ADC: Fix bugs related to oneshot read
Subsystems (2)
cover ADC: Fix bugs related to oneshot read
This series fixes a couple of bugs related to simultaneous read of multiple ADC channels in oneshot mode. Since, all channels share FIFO1, it has to be protected using a mutex, implemented by patch 1. Patch 2 increases the timeout waiting for a ADC sample.
Vignesh R (2):
iio: adc: ti_am335x_adc: Protect FIFO1 from concurrent access
iio: adc: ti_am335x_adc: Increase timeout value waiting for ADC sample
drivers/iio/adc/ti_am335x_adc.c | 16 ++++++++++++---- include/linux/mfd/ti_am335x_tscadc.h | 8 ++++---- 2 files changed, 16 insertions(+), 8 deletions(-)
Signature · 1 line
2.9.2
PATCH 1/2 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.
--- drivers/iio/adc/ti_am335x_adc.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/iio/adc/ti_am335x_adc.c +11 -3
@@ -32,6 +32,7 @@struct tiadc_device {struct ti_tscadc_dev *mfd_tscadc; +struct mutex fifo1_lock;/* to protect fifo access */ int channels;u8 channel_line[8 ];u8 channel_step[8 ];
@@ -359,6 +360,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev, int *val, int *val2, long mask) { struct tiadc_device *adc_dev = iio_priv(indio_dev); +int ret =IIO_VAL_INT ;int i, map_val;unsigned int fifo1count, read, stepid;bool found = false;
@@ -372,6 +374,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,if (!step_en)return -EINVAL ; +mutex_lock (&adc_dev->fifo1_lock); fifo1count =tiadc_readl (adc_dev,REG_FIFO1CNT );while (fifo1count--)tiadc_readl (adc_dev,REG_FIFO1 );
@@ -388,7 +391,8 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,if (time_after (jiffies, timeout)) {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];
@@ -414,8 +418,11 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,am335x_tsc_se_adc_done (adc_dev->mfd_tscadc);if (found == false) -return -EBUSY ; -return IIO_VAL_INT ; + ret = -EBUSY ; + +err_unlock: +mutex_unlock (&adc_dev->fifo1_lock); +return ret; }static const struct iio_info tiadc_info = {
@@ -483,6 +490,7 @@ static int tiadc_probe(struct platform_device *pdev)tiadc_step_config (indio_dev);tiadc_writel (adc_dev,REG_FIFO1THR ,FIFO1_THRESHOLD ); +mutex_init (&adc_dev->fifo1_lock); err =tiadc_channel_init (indio_dev, adc_dev->channels);if (err <0 )
Signature · 1 line
2.9.2
Re: [PATCH 1/2] iio: adc: ti_am335x_adc: Protect FIFO1 from concurrent access
Quoted 21 lines · Vignesh R
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.
Ifam335x_tsc_se_set_once()is called again fromtiadc_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. Sincetiadc_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.Signed-off-by Vignesh R <[email protected]>--- drivers/iio/adc/ti_am335x_adc.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)drivers/iio/adc/ti_am335x_adc.c +0
-0
drivers/iio/adc/ti_am335x_adc.c line 35
Quoted 4 lines
drivers/iio/adc/ti_am335x_adc.c +1
-0@@ -32,6 +32,7 @@struct tiadc_device {struct ti_tscadc_dev *mfd_tscadc; +struct mutex fifo1_lock;/* to protect fifo access */
Are there more than one FIFOs, or even possible? Just wondering the number indexing here..
Quoted 58 lines
drivers/iio/adc/ti_am335x_adc.c +10
-4int channels;u8 channel_line[8 ];u8 channel_step[8 ];@@ -359,6 +360,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev, int *val, int *val2, long mask) { struct tiadc_device *adc_dev = iio_priv(indio_dev); +int ret =IIO_VAL_INT ;int i, map_val;unsigned int fifo1count, read, stepid;bool found = false;@@ -372,6 +374,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,if (!step_en)return -EINVAL ; +mutex_lock (&adc_dev->fifo1_lock); fifo1count =tiadc_readl (adc_dev,REG_FIFO1CNT );while (fifo1count--)tiadc_readl (adc_dev,REG_FIFO1 );@@ -388,7 +391,8 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,if (time_after (jiffies, timeout)) {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];@@ -414,8 +418,11 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,am335x_tsc_se_adc_done (adc_dev->mfd_tscadc);if (found == false) -return -EBUSY ; -return IIO_VAL_INT ; + ret = -EBUSY ; + +err_unlock: +mutex_unlock (&adc_dev->fifo1_lock); +return ret; }static const struct iio_info tiadc_info = {@@ -483,6 +490,7 @@ static int tiadc_probe(struct platform_device *pdev)tiadc_step_config (indio_dev);tiadc_writel (adc_dev,REG_FIFO1THR ,FIFO1_THRESHOLD ); +mutex_init (&adc_dev->fifo1_lock); err =tiadc_channel_init (indio_dev, adc_dev->channels);if (err <0 ) --2.9.2
--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 1/2] iio: adc: ti_am335x_adc: Protect FIFO1 from concurrent access
Quoted 23 lines · Matt Ranostay
Quoted 22 lines · Vignesh R
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.
Ifam335x_tsc_se_set_once()is called again fromtiadc_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. Sincetiadc_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.Signed-off-by Vignesh R <[email protected]>--- drivers/iio/adc/ti_am335x_adc.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)drivers/iio/adc/ti_am335x_adc.c +0
-0@@ -32,6 +32,7 @@
Quoted 6 lines
drivers/iio/adc/ti_am335x_adc.c line 35
Quoted 3 lines
drivers/iio/adc/ti_am335x_adc.c +1
-0struct tiadc_device {struct ti_tscadc_dev *mfd_tscadc; +struct mutex fifo1_lock;/* to protect fifo access */ Are there more than one FIFOs, or even possible? Just wondering the number indexing here..
ADC IP as such as two FIFOs: FIFO0 and FIFO1. FIFO0 is dedicated for touchscreen channels and FIFO1 is used for ADC channels. Its not possible to use FIFO0 for ADC, but to match h/w register naming, I used above name for mutex.
Signature · 2 lines
Regards Vignesh
Jonathan Cameron
maintainerRe: [PATCH 1/2] iio: adc: ti_am335x_adc: Protect FIFO1 from concurrent access
In reply to Vignesh RQuoted 11 lines · Vignesh R
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.
Ifam335x_tsc_se_set_once()is called again fromtiadc_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. Sincetiadc_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.
Quoted 1 line
Signed-off-by Vignesh R <[email protected]>
Hi,
Thanks for the patch.
As this is clearly a fix for a long standing issue, I'd like to send it for stable inclusion. Would you mind doing a bit of detective work to added a Fixes tag to say which original patch introduced the issue?
I'm going to start pushing back on this in general as it's helpful to the stable maintainers if we provide this info whenever possible.
Thanks,
Jonathan
Quoted 65 lines
--- drivers/iio/adc/ti_am335x_adc.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)drivers/iio/adc/ti_am335x_adc.c +11
-3@@ -32,6 +32,7 @@struct tiadc_device {struct ti_tscadc_dev *mfd_tscadc; +struct mutex fifo1_lock;/* to protect fifo access */ int channels;u8 channel_line[8 ];u8 channel_step[8 ];@@ -359,6 +360,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev, int *val, int *val2, long mask) { struct tiadc_device *adc_dev = iio_priv(indio_dev); +int ret =IIO_VAL_INT ;int i, map_val;unsigned int fifo1count, read, stepid;bool found = false;@@ -372,6 +374,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,if (!step_en)return -EINVAL ; +mutex_lock (&adc_dev->fifo1_lock); fifo1count =tiadc_readl (adc_dev,REG_FIFO1CNT );while (fifo1count--)tiadc_readl (adc_dev,REG_FIFO1 );@@ -388,7 +391,8 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,if (time_after (jiffies, timeout)) {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];@@ -414,8 +418,11 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,am335x_tsc_se_adc_done (adc_dev->mfd_tscadc);if (found == false) -return -EBUSY ; -return IIO_VAL_INT ; + ret = -EBUSY ; + +err_unlock: +mutex_unlock (&adc_dev->fifo1_lock); +return ret; }static const struct iio_info tiadc_info = {@@ -483,6 +490,7 @@ static int tiadc_probe(struct platform_device *pdev)tiadc_step_config (indio_dev);tiadc_writel (adc_dev,REG_FIFO1THR ,FIFO1_THRESHOLD ); +mutex_init (&adc_dev->fifo1_lock); err =tiadc_channel_init (indio_dev, adc_dev->channels);if (err <0 )
Re: [PATCH 1/2] iio: adc: ti_am335x_adc: Protect FIFO1 from concurrent access
Quoted 12 lines · Jonathan Cameron
Quoted 11 lines · Vignesh R
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.
Ifam335x_tsc_se_set_once()is called again fromtiadc_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. Sincetiadc_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.
Quoted 9 lines
Quoted 1 line
Signed-off-by Vignesh R <[email protected]>Hi,
Thanks for the patch.
As this is clearly a fix for a long standing issue, I'd like to send it for stable inclusion. Would you mind doing a bit of detective work to added a Fixes tag to say which original patch introduced the issue?
Looks to be due to the commit 7ca6740cd1cd4 ("mfd: input: iio:ti_amm335x: Rework TSC/ADC synchronization")
Will send v2 with fixes tag. Thanks!
Signature · 2 lines
Regards Vignesh
PATCH 2/2 iio: adc: ti_am335x_adc: Increase timeout value waiting for ADC sample
In reply to Vignesh RNow that open delay and sample delay for each channel is configurable via DT, the default IDLE_TIMEOUT value is not enough as this is calculated based on hardcoded macros. This results in driver returning EBUSY sometimes. Fix this by increasing the timeout
value based on maximum value possible to open delay and sample delays for each channel.
--- drivers/iio/adc/ti_am335x_adc.c | 2 +- include/linux/mfd/ti_am335x_tscadc.h | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/iio/adc/ti_am335x_adc.c +1 -1
@@ -381,7 +381,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,am335x_tsc_se_set_once (adc_dev->mfd_tscadc, step_en); - timeout = jiffies +usecs_to_jiffies + timeout = jiffies +msecs_to_jiffies (IDLE_TIMEOUT * adc_dev->channels);/* Wait for Fifo threshold interrupt */ while (1 ) {
include/linux/mfd/ti_am335x_tscadc.h +4 -4
@@ -138,16 +138,16 @@/* * time in us for processing a single channel, calculated as follows: * -* num cycles = open delay + (sample delay + conv time) * averaging +* max num cycles = open delay + (sample delay + conv time) * averaging * -* num cycles: 152 + (1 + 13) * 16 = 376 +* max num cycles: 262143 + (255 + 13) * 16 = 266431 * * clock frequency: 26MHz / 8 = 3.25MHz * clock period: 1 / 3.25MHz = 308ns * -* processing time: 376 * 308ns = 116us +* max processing time: 266431 * 308ns = 83ms(approx) */ -#define IDLE_TIMEOUT 116/* microsec */ +#define IDLE_TIMEOUT 83/* milliseconds */ #define TSCADC_CELLS 2
Signature · 1 line
2.9.2
Re: [PATCH 2/2] iio: adc: ti_am335x_adc: Increase timeout value waiting for ADC sample
Quoted 11 lines · Vignesh R
Now that open delay and sample delay for each channel is configurable via DT, the default
IDLE_TIMEOUTvalue is not enough as this is calculated based on hardcoded macros. This results in driver returning EBUSY sometimes. Fix this by increasing the timeout
value based on maximum value possible to open delay and sample delays for each channel.Signed-off-by Vignesh R <[email protected]>--- drivers/iio/adc/ti_am335x_adc.c | 2 +- include/linux/mfd/ti_am335x_tscadc.h | 8 ++++----
Quoted 40 lines
2 files changed, 5 insertions(+), 5 deletions(-)drivers/iio/adc/ti_am335x_adc.c +1
-1@@ -381,7 +381,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,am335x_tsc_se_set_once (adc_dev->mfd_tscadc, step_en); - timeout = jiffies +usecs_to_jiffies + timeout = jiffies +msecs_to_jiffies (IDLE_TIMEOUT * adc_dev->channels);/* Wait for Fifo threshold interrupt */ while (1 ) {include/linux/mfd/ti_am335x_tscadc.h +4
-4@@ -138,16 +138,16 @@/* * time in us for processing a single channel, calculated as follows: * -* num cycles = open delay + (sample delay + conv time) * averaging +* max num cycles = open delay + (sample delay + conv time) * averaging * -* num cycles: 152 + (1 + 13) * 16 = 376 +* max num cycles: 262143 + (255 + 13) * 16 = 266431 * * clock frequency: 26MHz / 8 = 3.25MHz * clock period: 1 / 3.25MHz = 308ns * -* processing time: 376 * 308ns = 116us +* max processing time: 266431 * 308ns = 83ms(approx) */ -#define IDLE_TIMEOUT 116/* microsec */ +#define IDLE_TIMEOUT 83/* milliseconds */ #define TSCADC_CELLS 2
Signature · 4 lines
Lee Jones Linaro STMicroelectronics Landing Team Lead Linaro.org │ Open source software for ARM SoCs Follow Linaro: Facebook | Twitter | Blog
Jonathan Cameron
maintainerRe: [PATCH 2/2] iio: adc: ti_am335x_adc: Increase timeout value waiting for ADC sample
In reply to Vignesh RQuoted 8 lines · Vignesh R
Now that open delay and sample delay for each channel is configurable via DT, the default
IDLE_TIMEOUTvalue is not enough as this is calculated based on hardcoded macros. This results in driver returning EBUSY sometimes. Fix this by increasing the timeout
value based on maximum value possible to open delay and sample delays for each channel.Signed-off-by Vignesh R <[email protected]>
As with patch 1 could you provide a fixes tag as well highlighting the patch that introduced the issue. I'm thinking you are referring to:
iio: adc: ti_am335x_adc: make sample delay, open delay, averaging DT parameters 5dc11e810676ec4a5acb4423ccd33314bf74f4e54
Quoted 44 lines
--- drivers/iio/adc/ti_am335x_adc.c | 2 +- include/linux/mfd/ti_am335x_tscadc.h | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-)drivers/iio/adc/ti_am335x_adc.c +1
-1@@ -381,7 +381,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,am335x_tsc_se_set_once (adc_dev->mfd_tscadc, step_en); - timeout = jiffies +usecs_to_jiffies + timeout = jiffies +msecs_to_jiffies (IDLE_TIMEOUT * adc_dev->channels);/* Wait for Fifo threshold interrupt */ while (1 ) {include/linux/mfd/ti_am335x_tscadc.h +4
-4@@ -138,16 +138,16 @@/* * time in us for processing a single channel, calculated as follows: * -* num cycles = open delay + (sample delay + conv time) * averaging +* max num cycles = open delay + (sample delay + conv time) * averaging * -* num cycles: 152 + (1 + 13) * 16 = 376 +* max num cycles: 262143 + (255 + 13) * 16 = 266431 * * clock frequency: 26MHz / 8 = 3.25MHz * clock period: 1 / 3.25MHz = 308ns * -* processing time: 376 * 308ns = 116us +* max processing time: 266431 * 308ns = 83ms(approx) */ -#define IDLE_TIMEOUT 116/* microsec */ +#define IDLE_TIMEOUT 83/* milliseconds */ #define TSCADC_CELLS 2
Re: [PATCH 2/2] iio: adc: ti_am335x_adc: Increase timeout value waiting for ADC sample
Quoted 8 lines · Jonathan Cameron
Quoted 7 lines · Vignesh R
Now that open delay and sample delay for each channel is configurable via DT, the default
IDLE_TIMEOUTvalue is not enough as this is calculated based on hardcoded macros. This results in driver returning EBUSY sometimes. Fix this by increasing the timeout
value based on maximum value possible to open delay and sample delays for each channel.
Quoted 6 lines
Quoted 1 line
Signed-off-by Vignesh R <[email protected]>As with patch 1 could you provide a fixes tag as well highlighting the patch that introduced the issue. I'm thinking you are referring to:
iio: adc:
ti_am335x_adc: make sample delay, open delay, averaging DT parameters5dc11e810676ec4a5acb4423ccd33314bf74f4e54
Yes, will send v2 with fixes tag pointing to above commit. Thanks!
Signature · 2 lines
Regards Vignesh