diff mbox series

[07/16] iio: adc: max1027: Create a helper to configure the trigger

Message ID 20210818111139.330636-8-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
There are two ways to physically trigger a conversion:
- A falling edge on the cnvst pin
- A write operation on the conversion register

Let's create a helper for this.

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

Comments

Nuno Sa Aug. 20, 2021, 7:16 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 07/16] iio: adc: max1027: Create a helper to configure
> the trigger
> 
> [External]
> 
> There are two ways to physically trigger a conversion:
> - A falling edge on the cnvst pin
> - A write operation on the conversion register
> 
> Let's create a helper for this.
> 
> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> ---
>  drivers/iio/adc/max1027.c | 52 ++++++++++++++++++++++++++------
> -------
>  1 file changed, 35 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c
> index a6ebc951c09a..59914fcfd320 100644
> --- a/drivers/iio/adc/max1027.c
> +++ b/drivers/iio/adc/max1027.c
> @@ -232,10 +232,37 @@ struct max1027_state {
>  	struct iio_trigger		*trig;
>  	__be16				*buffer;
>  	struct mutex			lock;
> -
> +	bool				cnvst_trigger;
>  	u8				reg ____cacheline_aligned;
>  };
> 
> +static int max1027_configure_trigger(struct iio_dev *indio_dev)
> +{
> +	struct max1027_state *st = iio_priv(indio_dev);
> +	int ret;
> +
> +	st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2;
> +
> +	/*
> +	 * Start acquisition on:
> +	 * MODE0: external hardware trigger wired to the cnvst input
> pin
> +	 * MODE2: conversion register write
> +	 */
> +	if (st->cnvst_trigger)
> +		st->reg |= MAX1027_CKS_MODE0;
> +	else
> +		st->reg |= MAX1027_CKS_MODE2;
> +
> +	ret = spi_write(st->spi, &st->reg, 1);
> +	if (ret < 0) {
> +		dev_err(&indio_dev->dev,
> +			"Failed to configure register (%d)\n", ret);
> +		return ret;
> +	}

I do not think the error message is so important here that we cannot
make this more simple and just do:

return spi_write(st->spi, &st->reg, 1);

Anyways, not that important so:

Reviewed-by: Nuno Sá <nuno.sa@analog.com>

> +	return 0;
> +}
> +
>  static int max1027_read_single_value(struct iio_dev *indio_dev,
>  				     struct iio_chan_spec const *chan,
>  				     int *val)
> @@ -248,14 +275,9 @@ static int max1027_read_single_value(struct
> iio_dev *indio_dev,
>  		return -EBUSY;
>  	}
> 
> -	/* Start acquisition on conversion register write */
> -	st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2 |
> MAX1027_CKS_MODE2;
> -	ret = spi_write(st->spi, &st->reg, 1);
> -	if (ret < 0) {
> -		dev_err(&indio_dev->dev,
> -			"Failed to configure setup register\n");
> +	ret = max1027_configure_trigger(indio_dev);
> +	if (ret)
>  		return ret;
> -	}
> 
>  	/* Configure conversion register with the requested chan */
>  	st->reg = MAX1027_CONV_REG | MAX1027_CHAN(chan-
> >channel) |
> @@ -363,12 +385,10 @@ static int
> max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
>  	if (bitmap_empty(indio_dev->active_scan_mask, indio_dev-
> >masklength))
>  		return -EINVAL;
> 
> +	st->cnvst_trigger = state;
>  	if (state) {
> -		/* Start acquisition on cnvst */
> -		st->reg = MAX1027_SETUP_REG |
> MAX1027_CKS_MODE0 |
> -			  MAX1027_REF_MODE2;
> -		ret = spi_write(st->spi, &st->reg, 1);
> -		if (ret < 0)
> +		ret = max1027_configure_trigger(indio_dev);
> +		if (ret)
>  			return ret;
> 
>  		/*
> @@ -382,10 +402,8 @@ static int
> max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
>  			return ret;
>  	} else {
>  		/* Start acquisition on conversion register write */
> -		st->reg = MAX1027_SETUP_REG |
> MAX1027_CKS_MODE2	|
> -			  MAX1027_REF_MODE2;
> -		ret = spi_write(st->spi, &st->reg, 1);
> -		if (ret < 0)
> +		ret = max1027_configure_trigger(indio_dev);
> +		if (ret)
>  			return ret;
>  	}
> 
> --
> 2.27.0
Jonathan Cameron Aug. 30, 2021, 10:16 a.m. UTC | #2
On Wed, 18 Aug 2021 13:11:30 +0200
Miquel Raynal <miquel.raynal@bootlin.com> wrote:

> There are two ways to physically trigger a conversion:
> - A falling edge on the cnvst pin
> - A write operation on the conversion register
> 
> Let's create a helper for this.
> 
> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Trivial comments inline.

Thanks,

Jonathan

> ---
>  drivers/iio/adc/max1027.c | 52 ++++++++++++++++++++++++++-------------
>  1 file changed, 35 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c
> index a6ebc951c09a..59914fcfd320 100644
> --- a/drivers/iio/adc/max1027.c
> +++ b/drivers/iio/adc/max1027.c
> @@ -232,10 +232,37 @@ struct max1027_state {
>  	struct iio_trigger		*trig;
>  	__be16				*buffer;
>  	struct mutex			lock;
> -

Avoid white space changes in a patch doing something else.
In this case, I'd imagine the reg had a blank line before it to more
strongly indicate the cacheline_aligned nature meaning there would be
a bit of padding inserted here.

> +	bool				cnvst_trigger;

I'd expect a cnvst_trigger named variable to be a pointer to the trigger.  Perhaps
call it use_cnvst_trigger or something like that?

>  	u8				reg ____cacheline_aligned;
>  };
>  
> +static int max1027_configure_trigger(struct iio_dev *indio_dev)
> +{
> +	struct max1027_state *st = iio_priv(indio_dev);
> +	int ret;
> +
> +	st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2;
> +
> +	/*
> +	 * Start acquisition on:
> +	 * MODE0: external hardware trigger wired to the cnvst input pin
> +	 * MODE2: conversion register write
> +	 */
> +	if (st->cnvst_trigger)
> +		st->reg |= MAX1027_CKS_MODE0;
> +	else
> +		st->reg |= MAX1027_CKS_MODE2;
> +
> +	ret = spi_write(st->spi, &st->reg, 1);
> +	if (ret < 0) {
> +		dev_err(&indio_dev->dev,
> +			"Failed to configure register (%d)\n", ret);
> +		return ret;
> +	}
> +
> +	return 0;
> +}
> +
>  static int max1027_read_single_value(struct iio_dev *indio_dev,
>  				     struct iio_chan_spec const *chan,
>  				     int *val)
> @@ -248,14 +275,9 @@ static int max1027_read_single_value(struct iio_dev *indio_dev,
>  		return -EBUSY;
>  	}
>  
> -	/* Start acquisition on conversion register write */
> -	st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2 | MAX1027_CKS_MODE2;
> -	ret = spi_write(st->spi, &st->reg, 1);
> -	if (ret < 0) {
> -		dev_err(&indio_dev->dev,
> -			"Failed to configure setup register\n");
> +	ret = max1027_configure_trigger(indio_dev);
> +	if (ret)
>  		return ret;
> -	}
>  
>  	/* Configure conversion register with the requested chan */
>  	st->reg = MAX1027_CONV_REG | MAX1027_CHAN(chan->channel) |
> @@ -363,12 +385,10 @@ static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
>  	if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
>  		return -EINVAL;
>  
> +	st->cnvst_trigger = state;
>  	if (state) {
> -		/* Start acquisition on cnvst */
> -		st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE0 |
> -			  MAX1027_REF_MODE2;
> -		ret = spi_write(st->spi, &st->reg, 1);
> -		if (ret < 0)
> +		ret = max1027_configure_trigger(indio_dev);
> +		if (ret)
>  			return ret;
>  
>  		/*
> @@ -382,10 +402,8 @@ static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
>  			return ret;
>  	} else {
>  		/* Start acquisition on conversion register write */
> -		st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE2	|
> -			  MAX1027_REF_MODE2;
> -		ret = spi_write(st->spi, &st->reg, 1);
> -		if (ret < 0)
> +		ret = max1027_configure_trigger(indio_dev);
> +		if (ret)
>  			return ret;
>  	}
>
diff mbox series

Patch

diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c
index a6ebc951c09a..59914fcfd320 100644
--- a/drivers/iio/adc/max1027.c
+++ b/drivers/iio/adc/max1027.c
@@ -232,10 +232,37 @@  struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
-
+	bool				cnvst_trigger;
 	u8				reg ____cacheline_aligned;
 };
 
+static int max1027_configure_trigger(struct iio_dev *indio_dev)
+{
+	struct max1027_state *st = iio_priv(indio_dev);
+	int ret;
+
+	st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2;
+
+	/*
+	 * Start acquisition on:
+	 * MODE0: external hardware trigger wired to the cnvst input pin
+	 * MODE2: conversion register write
+	 */
+	if (st->cnvst_trigger)
+		st->reg |= MAX1027_CKS_MODE0;
+	else
+		st->reg |= MAX1027_CKS_MODE2;
+
+	ret = spi_write(st->spi, &st->reg, 1);
+	if (ret < 0) {
+		dev_err(&indio_dev->dev,
+			"Failed to configure register (%d)\n", ret);
+		return ret;
+	}
+
+	return 0;
+}
+
 static int max1027_read_single_value(struct iio_dev *indio_dev,
 				     struct iio_chan_spec const *chan,
 				     int *val)
@@ -248,14 +275,9 @@  static int max1027_read_single_value(struct iio_dev *indio_dev,
 		return -EBUSY;
 	}
 
-	/* Start acquisition on conversion register write */
-	st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2 | MAX1027_CKS_MODE2;
-	ret = spi_write(st->spi, &st->reg, 1);
-	if (ret < 0) {
-		dev_err(&indio_dev->dev,
-			"Failed to configure setup register\n");
+	ret = max1027_configure_trigger(indio_dev);
+	if (ret)
 		return ret;
-	}
 
 	/* Configure conversion register with the requested chan */
 	st->reg = MAX1027_CONV_REG | MAX1027_CHAN(chan->channel) |
@@ -363,12 +385,10 @@  static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 	if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
 		return -EINVAL;
 
+	st->cnvst_trigger = state;
 	if (state) {
-		/* Start acquisition on cnvst */
-		st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE0 |
-			  MAX1027_REF_MODE2;
-		ret = spi_write(st->spi, &st->reg, 1);
-		if (ret < 0)
+		ret = max1027_configure_trigger(indio_dev);
+		if (ret)
 			return ret;
 
 		/*
@@ -382,10 +402,8 @@  static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 			return ret;
 	} else {
 		/* Start acquisition on conversion register write */
-		st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE2	|
-			  MAX1027_REF_MODE2;
-		ret = spi_write(st->spi, &st->reg, 1);
-		if (ret < 0)
+		ret = max1027_configure_trigger(indio_dev);
+		if (ret)
 			return ret;
 	}