2022-06-20 15:53:23

by Olivier MOYSAN

[permalink] [raw]
Subject: [PATCH 0/2] iio: adc: stm32-adc: add checks on adc state

On ADC startup a calibration is launched.
A prerequisite for ADC calibration is to have ADC in disabled state.
After kernel boot, the ADC state may be unknown as the ADC may have been left enabled by boot stage.

Call ADC disable routine systematically before launching calibration.
Adapt ADC disable routine to perform disabling sequence according to current ADC state.

Olivier Moysan (2):
iio: adc: stm32-adc: make safe adc disable
iio: adc: stm32-adc: disable adc before calibration

drivers/iio/adc/stm32-adc.c | 6 ++++++
1 file changed, 6 insertions(+)

--
2.25.1


2022-06-20 15:53:24

by Olivier MOYSAN

[permalink] [raw]
Subject: [PATCH 1/2] iio: adc: stm32-adc: make safe adc disable

From ADC reference manual the software is allowed to write the
control bit ADDIS of the ADC_CR register only if the ADC is enabled.
Return immediately from stm32h7_adc_disable() if ADC is already disabled.

Signed-off-by: Olivier Moysan <[email protected]>
---
drivers/iio/adc/stm32-adc.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c
index a68ecbda6480..80e333f65ddd 100644
--- a/drivers/iio/adc/stm32-adc.c
+++ b/drivers/iio/adc/stm32-adc.c
@@ -876,6 +876,9 @@ static void stm32h7_adc_disable(struct iio_dev *indio_dev)
int ret;
u32 val;

+ if (!(stm32_adc_readl(adc, STM32H7_ADC_CR) & STM32H7_ADEN))
+ return;
+
/* Disable ADC and wait until it's effectively disabled */
stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADDIS);
ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,
--
2.25.1

2022-06-20 16:25:13

by Olivier MOYSAN

[permalink] [raw]
Subject: [PATCH 2/2] iio: adc: stm32-adc: disable adc before calibration

The calibration is launched from prepare callback.
The ADC state when entering this function may be unknown as the
ADC may have been left enabled by boot stage.
One prerequisite for ADC calibration is to have ADC in disabled state.
If the calibration is started when ADC is still enabled, the behavior is
unpredictable, and the calibration may fail with a timeout error.

Force ADC to disabled state in stm32h7_adc_selfcalib().
ADC enabling is ensured by stm32h7_adc_enable() call,
before leaving prepare callback.

Signed-off-by: Olivier Moysan <[email protected]>
---
drivers/iio/adc/stm32-adc.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c
index 80e333f65ddd..3985fe972892 100644
--- a/drivers/iio/adc/stm32-adc.c
+++ b/drivers/iio/adc/stm32-adc.c
@@ -1019,6 +1019,9 @@ static int stm32h7_adc_selfcalib(struct iio_dev *indio_dev)
if (adc->cal.calibrated)
return true;

+ /* ADC must be disabled for calibration */
+ stm32h7_adc_disable(indio_dev);
+
/*
* Select calibration mode:
* - Offset calibration for single ended inputs
--
2.25.1

2022-06-21 16:34:27

by Fabrice Gasnier

[permalink] [raw]
Subject: Re: [PATCH 2/2] iio: adc: stm32-adc: disable adc before calibration

On 6/20/22 17:47, Olivier Moysan wrote:
> The calibration is launched from prepare callback.
> The ADC state when entering this function may be unknown as the
> ADC may have been left enabled by boot stage.
> One prerequisite for ADC calibration is to have ADC in disabled state.
> If the calibration is started when ADC is still enabled, the behavior is
> unpredictable, and the calibration may fail with a timeout error.
>
> Force ADC to disabled state in stm32h7_adc_selfcalib().
> ADC enabling is ensured by stm32h7_adc_enable() call,
> before leaving prepare callback.
>
> Signed-off-by: Olivier Moysan <[email protected]>

Hi Olivier,

You can add my:
Reviewed-by: Fabrice Gasnier <[email protected]>

Thanks,
Fabrice

> ---
> drivers/iio/adc/stm32-adc.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c
> index 80e333f65ddd..3985fe972892 100644
> --- a/drivers/iio/adc/stm32-adc.c
> +++ b/drivers/iio/adc/stm32-adc.c
> @@ -1019,6 +1019,9 @@ static int stm32h7_adc_selfcalib(struct iio_dev *indio_dev)
> if (adc->cal.calibrated)
> return true;
>
> + /* ADC must be disabled for calibration */
> + stm32h7_adc_disable(indio_dev);
> +
> /*
> * Select calibration mode:
> * - Offset calibration for single ended inputs

2022-06-21 16:39:11

by Fabrice Gasnier

[permalink] [raw]
Subject: Re: [PATCH 1/2] iio: adc: stm32-adc: make safe adc disable

On 6/20/22 17:47, Olivier Moysan wrote:
> From ADC reference manual the software is allowed to write the
> control bit ADDIS of the ADC_CR register only if the ADC is enabled.
> Return immediately from stm32h7_adc_disable() if ADC is already disabled.
>
> Signed-off-by: Olivier Moysan <[email protected]>

Hi Olivier,

You can add my:
Reviewed-by: Fabrice Gasnier <[email protected]>

Thanks,
Fabrice

> ---
> drivers/iio/adc/stm32-adc.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c
> index a68ecbda6480..80e333f65ddd 100644
> --- a/drivers/iio/adc/stm32-adc.c
> +++ b/drivers/iio/adc/stm32-adc.c
> @@ -876,6 +876,9 @@ static void stm32h7_adc_disable(struct iio_dev *indio_dev)
> int ret;
> u32 val;
>
> + if (!(stm32_adc_readl(adc, STM32H7_ADC_CR) & STM32H7_ADEN))
> + return;
> +
> /* Disable ADC and wait until it's effectively disabled */
> stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADDIS);
> ret = stm32_adc_readl_poll_timeout(STM32H7_ADC_CR, val,

2022-06-25 13:23:19

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH 0/2] iio: adc: stm32-adc: add checks on adc state

On Mon, 20 Jun 2022 17:47:36 +0200
Olivier Moysan <[email protected]> wrote:

> On ADC startup a calibration is launched.
> A prerequisite for ADC calibration is to have ADC in disabled state.
> After kernel boot, the ADC state may be unknown as the ADC may have been left enabled by boot stage.
>
> Call ADC disable routine systematically before launching calibration.
> Adapt ADC disable routine to perform disabling sequence according to current ADC state.
>
> Olivier Moysan (2):
> iio: adc: stm32-adc: make safe adc disable
> iio: adc: stm32-adc: disable adc before calibration
>
> drivers/iio/adc/stm32-adc.c | 6 ++++++
> 1 file changed, 6 insertions(+)
>

Series applied to the togreg branch and pushed out as testing
for all the normal reasons.

Thanks,

Jonathan