diff mbox series

[12/16] iio: adc: max1027: Introduce an end of conversion helper

Message ID 20210818111139.330636-13-miquel.raynal@bootlin.com (mailing list archive)
State Changes Requested
Headers show
Series Bring software triggers support to MAX1027-like ADCs | expand

Commit Message

Miquel Raynal Aug. 18, 2021, 11:11 a.m. UTC
For now this helper only waits for the maximum duration of a conversion,
but it will soon be improved to properly handle the end of conversion
interrupt.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/iio/adc/max1027.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

Comments

Nuno Sa Aug. 20, 2021, 7:28 a.m. UTC | #1
> -----Original Message-----
> From: Miquel Raynal <miquel.raynal@bootlin.com>
> Sent: Wednesday, August 18, 2021 1:12 PM
> To: Jonathan Cameron <jic23@kernel.org>; Lars-Peter Clausen
> <lars@metafoo.de>
> Cc: Thomas Petazzoni <thomas.petazzoni@bootlin.com>; linux-
> iio@vger.kernel.org; linux-kernel@vger.kernel.org; Miquel Raynal
> <miquel.raynal@bootlin.com>
> Subject: [PATCH 12/16] iio: adc: max1027: Introduce an end of
> conversion helper
> 
> [External]
> 
> For now this helper only waits for the maximum duration of a
> conversion,
> but it will soon be improved to properly handle the end of conversion
> interrupt.
> 
> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> ---
>  drivers/iio/adc/max1027.c | 23 +++++++++++++++++++++--
>  1 file changed, 21 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c
> index afc3ce69f7ea..2d6485591761 100644
> --- a/drivers/iio/adc/max1027.c
> +++ b/drivers/iio/adc/max1027.c
> @@ -60,6 +60,9 @@
>  #define MAX1027_NAVG_32   (0x03 << 2)
>  #define MAX1027_AVG_EN    BIT(4)
> 
> +/* Device can achieve 300ksps so we assume a 3.33us conversion
> delay */
> +#define MAX1027_CONVERSION_UDELAY 4
> +
>  enum max1027_id {
>  	max1027,
>  	max1029,
> @@ -236,6 +239,20 @@ struct max1027_state {
>  	u8				reg ____cacheline_aligned;
>  };
> 
> +static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);
> +
> +static int max1027_wait_eoc(struct iio_dev *indio_dev)
> +{
> +	unsigned int conversion_time =
> MAX1027_CONVERSION_UDELAY;
> +
> +	if (indio_dev->active_scan_mask)
> +		conversion_time *= hweight32(*indio_dev-
> >active_scan_mask);
> +
> +	usleep_range(conversion_time, conversion_time * 2);
> +
> +	return 0;
> +}
> +
>  /* Scan from 0 to the highest requested channel */
>  static int max1027_configure_chans_to_scan(struct iio_dev
> *indio_dev)
>  {
> @@ -310,9 +327,11 @@ static int max1027_read_single_value(struct
> iio_dev *indio_dev,
>  	/*
>  	 * For an unknown reason, when we use the mode "10" (write
>  	 * conversion register), the interrupt doesn't occur every time.
> -	 * So we just wait 1 ms.
> +	 * So we just wait the maximum conversion time and deliver
> the value.
>  	 */
> -	mdelay(1);
> +	ret = max1027_wait_eoc(indio_dev);
> +	if (ret)
> +		return ret;

I'm a bit confused here... Why are we looking at the active_scan_mask?
Aren't we preventing this for running concurrently with buffering?

- Nuno Sá
> 
>  	/* Read result */
>  	ret = spi_read(st->spi, st->buffer, (chan->type == IIO_TEMP) ?
> 4 : 2);
> --
> 2.27.0
Jonathan Cameron Aug. 30, 2021, 10:34 a.m. UTC | #2
On Wed, 18 Aug 2021 13:11:35 +0200
Miquel Raynal <miquel.raynal@bootlin.com> wrote:

> For now this helper only waits for the maximum duration of a conversion,
> but it will soon be improved to properly handle the end of conversion
> interrupt.
> 
> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> ---
>  drivers/iio/adc/max1027.c | 23 +++++++++++++++++++++--
>  1 file changed, 21 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c
> index afc3ce69f7ea..2d6485591761 100644
> --- a/drivers/iio/adc/max1027.c
> +++ b/drivers/iio/adc/max1027.c
> @@ -60,6 +60,9 @@
>  #define MAX1027_NAVG_32   (0x03 << 2)
>  #define MAX1027_AVG_EN    BIT(4)
>  
> +/* Device can achieve 300ksps so we assume a 3.33us conversion delay */
> +#define MAX1027_CONVERSION_UDELAY 4
> +
>  enum max1027_id {
>  	max1027,
>  	max1029,
> @@ -236,6 +239,20 @@ struct max1027_state {
>  	u8				reg ____cacheline_aligned;
>  };
>  
> +static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);
Why here?  This had me confused when I couldn't find it being introduced
in the later patch that uses it.  More comments in that patch.

> +
> +static int max1027_wait_eoc(struct iio_dev *indio_dev)
> +{
> +	unsigned int conversion_time = MAX1027_CONVERSION_UDELAY;
> +
> +	if (indio_dev->active_scan_mask)
> +		conversion_time *= hweight32(*indio_dev->active_scan_mask);
> +
> +	usleep_range(conversion_time, conversion_time * 2);
> +
> +	return 0;
> +}
> +
>  /* Scan from 0 to the highest requested channel */
>  static int max1027_configure_chans_to_scan(struct iio_dev *indio_dev)
>  {
> @@ -310,9 +327,11 @@ static int max1027_read_single_value(struct iio_dev *indio_dev,
>  	/*
>  	 * For an unknown reason, when we use the mode "10" (write
>  	 * conversion register), the interrupt doesn't occur every time.
> -	 * So we just wait 1 ms.
> +	 * So we just wait the maximum conversion time and deliver the value.
>  	 */
> -	mdelay(1);
> +	ret = max1027_wait_eoc(indio_dev);
> +	if (ret)
> +		return ret;
>  
>  	/* Read result */
>  	ret = spi_read(st->spi, st->buffer, (chan->type == IIO_TEMP) ? 4 : 2);
Miquel Raynal Sept. 2, 2021, 9:26 a.m. UTC | #3
Hi Nuno,

"Sa, Nuno" <Nuno.Sa@analog.com> wrote on Fri, 20 Aug 2021 07:28:17
+0000:

> > -----Original Message-----
> > From: Miquel Raynal <miquel.raynal@bootlin.com>
> > Sent: Wednesday, August 18, 2021 1:12 PM
> > To: Jonathan Cameron <jic23@kernel.org>; Lars-Peter Clausen
> > <lars@metafoo.de>
> > Cc: Thomas Petazzoni <thomas.petazzoni@bootlin.com>; linux-
> > iio@vger.kernel.org; linux-kernel@vger.kernel.org; Miquel Raynal
> > <miquel.raynal@bootlin.com>
> > Subject: [PATCH 12/16] iio: adc: max1027: Introduce an end of
> > conversion helper
> > 
> > [External]
> > 
> > For now this helper only waits for the maximum duration of a
> > conversion,
> > but it will soon be improved to properly handle the end of conversion
> > interrupt.
> > 
> > Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> > ---
> >  drivers/iio/adc/max1027.c | 23 +++++++++++++++++++++--
> >  1 file changed, 21 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c
> > index afc3ce69f7ea..2d6485591761 100644
> > --- a/drivers/iio/adc/max1027.c
> > +++ b/drivers/iio/adc/max1027.c
> > @@ -60,6 +60,9 @@
> >  #define MAX1027_NAVG_32   (0x03 << 2)
> >  #define MAX1027_AVG_EN    BIT(4)
> > 
> > +/* Device can achieve 300ksps so we assume a 3.33us conversion
> > delay */
> > +#define MAX1027_CONVERSION_UDELAY 4
> > +
> >  enum max1027_id {
> >  	max1027,
> >  	max1029,
> > @@ -236,6 +239,20 @@ struct max1027_state {
> >  	u8				reg ____cacheline_aligned;
> >  };
> > 
> > +static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);
> > +
> > +static int max1027_wait_eoc(struct iio_dev *indio_dev)
> > +{
> > +	unsigned int conversion_time =
> > MAX1027_CONVERSION_UDELAY;
> > +
> > +	if (indio_dev->active_scan_mask)
> > +		conversion_time *= hweight32(*indio_dev-  
> > >active_scan_mask);  
> > +
> > +	usleep_range(conversion_time, conversion_time * 2);
> > +
> > +	return 0;
> > +}
> > +
> >  /* Scan from 0 to the highest requested channel */
> >  static int max1027_configure_chans_to_scan(struct iio_dev
> > *indio_dev)
> >  {
> > @@ -310,9 +327,11 @@ static int max1027_read_single_value(struct
> > iio_dev *indio_dev,
> >  	/*
> >  	 * For an unknown reason, when we use the mode "10" (write
> >  	 * conversion register), the interrupt doesn't occur every time.
> > -	 * So we just wait 1 ms.
> > +	 * So we just wait the maximum conversion time and deliver
> > the value.
> >  	 */
> > -	mdelay(1);
> > +	ret = max1027_wait_eoc(indio_dev);
> > +	if (ret)
> > +		return ret;  
> 
> I'm a bit confused here... Why are we looking at the active_scan_mask?
> Aren't we preventing this for running concurrently with buffering?

Sorry for the confusion, at this stage the "if (active_masks)" is
useless, I moved it later in the series when we actually start to use
this EOC helper for the buffered reads as well.

Thanks,
Miquèl
diff mbox series

Patch

diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c
index afc3ce69f7ea..2d6485591761 100644
--- a/drivers/iio/adc/max1027.c
+++ b/drivers/iio/adc/max1027.c
@@ -60,6 +60,9 @@ 
 #define MAX1027_NAVG_32   (0x03 << 2)
 #define MAX1027_AVG_EN    BIT(4)
 
+/* Device can achieve 300ksps so we assume a 3.33us conversion delay */
+#define MAX1027_CONVERSION_UDELAY 4
+
 enum max1027_id {
 	max1027,
 	max1029,
@@ -236,6 +239,20 @@  struct max1027_state {
 	u8				reg ____cacheline_aligned;
 };
 
+static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);
+
+static int max1027_wait_eoc(struct iio_dev *indio_dev)
+{
+	unsigned int conversion_time = MAX1027_CONVERSION_UDELAY;
+
+	if (indio_dev->active_scan_mask)
+		conversion_time *= hweight32(*indio_dev->active_scan_mask);
+
+	usleep_range(conversion_time, conversion_time * 2);
+
+	return 0;
+}
+
 /* Scan from 0 to the highest requested channel */
 static int max1027_configure_chans_to_scan(struct iio_dev *indio_dev)
 {
@@ -310,9 +327,11 @@  static int max1027_read_single_value(struct iio_dev *indio_dev,
 	/*
 	 * For an unknown reason, when we use the mode "10" (write
 	 * conversion register), the interrupt doesn't occur every time.
-	 * So we just wait 1 ms.
+	 * So we just wait the maximum conversion time and deliver the value.
 	 */
-	mdelay(1);
+	ret = max1027_wait_eoc(indio_dev);
+	if (ret)
+		return ret;
 
 	/* Read result */
 	ret = spi_read(st->spi, st->buffer, (chan->type == IIO_TEMP) ? 4 : 2);