Linux-Devicetree Archive mirror
 help / color / mirror / Atom feed
From: "Nuno Sá" <noname.nuno@gmail.com>
To: "Julien Stephan" <jstephan@baylibre.com>,
	"Lars-Peter Clausen" <lars@metafoo.de>,
	"Michael Hennerich" <Michael.Hennerich@analog.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"David Lechner" <dlechner@baylibre.com>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzysztof.kozlowski+dt@linaro.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Liam Girdwood" <lgirdwood@gmail.com>,
	"Mark Brown" <broonie@kernel.org>
Cc: kernel test robot <lkp@intel.com>,
	linux-iio@vger.kernel.org,  devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH RFC v6 08/10] iio: adc: ad7380: add oversampling support
Date: Mon, 06 May 2024 10:20:15 +0200	[thread overview]
Message-ID: <1be8a603c1db69278181089e3653b6312bcc99da.camel@gmail.com> (raw)
In-Reply-To: <20240501-adding-new-ad738x-driver-v6-8-3c0741154728@baylibre.com>

Hi Julien,

On Wed, 2024-05-01 at 16:55 +0200, Julien Stephan wrote:
> ad7380x(-4) parts are able to do oversampling to increase accuracy.
> They support 2 average modes: normal average and rolling overage.
> This commits focus on enabling normal average oversampling, which is the
> default one.
> 
> Normal averaging involves taking a number of samples, adding them together,
> and dividing the result by the number of samples taken.
> This result is then output from the device. The sample data is cleared when
> the process completes. Because we need more samples to output a value,
> the data output rate decrease with the oversampling ratio.
> 
> Signed-off-by: Julien Stephan <jstephan@baylibre.com>
> ---
>  drivers/iio/adc/ad7380.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 114 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/adc/ad7380.c b/drivers/iio/adc/ad7380.c
> index 020959759170..1e3869f5e48c 100644
> --- a/drivers/iio/adc/ad7380.c
> +++ b/drivers/iio/adc/ad7380.c
> @@ -88,7 +88,10 @@ struct ad7380_chip_info {
>  	.type = IIO_VOLTAGE,					\
>  	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
>  		((diff) ? 0 : BIT(IIO_CHAN_INFO_OFFSET)),	\
> -	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
> +	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |	\
> +		BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),		\
> +	.info_mask_shared_by_type_available =			\
> +		BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),		\
>  	.indexed = 1,						\
>  	.differential = (diff),					\
>  	.channel = (diff) ? (2 * (index)) : (index),		\
> @@ -156,6 +159,16 @@ static const struct ad7380_timing_specs ad7380_4_timing = {
>  	.t_csh_ns = 20,
>  };
>  
> +/*
> + * Available oversampling ratios. The indices correspond
> + * with the bit value expected by the chip.
> + * The available ratios depend on the averaging mode,
> + * only normal averaging is supported for now
> + */
> +static const int ad7380_normal_average_oversampling_ratios[] = {
> +	1, 2, 4, 8, 16, 32,
> +};
> +
>  static const struct ad7380_chip_info ad7380_chip_info = {
>  	.name = "ad7380",
>  	.channels = ad7380_channels,
> @@ -231,6 +244,7 @@ static const struct ad7380_chip_info ad7384_4_chip_info = {
>  struct ad7380_state {
>  	const struct ad7380_chip_info *chip_info;
>  	struct spi_device *spi;
> +	unsigned int oversampling_ratio;

nit: move this to the other 'unsigned int' fields...

>  	struct regmap *regmap;
>  	unsigned int vref_mv;
>  	unsigned int vcm_mv[MAX_NUM_CHANNELS];
> @@ -386,6 +400,12 @@ static int ad7380_read_direct(struct ad7380_state *st,
>  	};
>  	int ret;
>  
> +	/*
> +	 * In normal average oversampling we need to wait for multiple conversions
> to be done
> +	 */
> +	if (st->oversampling_ratio > 1)
> +		xfers[0].delay.value = T_CONVERT_NS + 500 * st-
> >oversampling_ratio;
> +
>  	ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
>  	if (ret < 0)
>  		return ret;
> @@ -428,6 +448,91 @@ static int ad7380_read_raw(struct iio_dev *indio_dev,
>  			/ st->vref_mv;
>  
>  		return IIO_VAL_INT;
> +	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
> +		*val = st->oversampling_ratio;
> +
> +		return IIO_VAL_INT;
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int ad7380_read_avail(struct iio_dev *indio_dev,
> +			     struct iio_chan_spec const *chan,
> +			     const int **vals, int *type, int *length,
> +			     long mask)
> +{
> +	switch (mask) {
> +	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
> +		*vals = ad7380_normal_average_oversampling_ratios;
> +		*length = ARRAY_SIZE(ad7380_normal_average_oversampling_ratios);
> +		*type = IIO_VAL_INT;
> +
> +		return IIO_AVAIL_LIST;
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +/**
> + * check_osr - Check the oversampling ratio
> + * @available_ratio: available ratios's array
> + * @size: size of the available_ratio array
> + * ratio: ratio to check
> + *
> + * Check if ratio is present in @available_ratio. Check for exact match.
> + * @available_ratio is an array of the available ratios (depending on the
> oversampling mode).
> + * The indices must correspond with the bit value expected by the chip.
> + */
> +static inline int check_osr(const int *available_ratio, int size, int ratio)

Please name the function ad7380_check_osr(). Also, drop the inline... The compiler
should be smart enough to take of that for us.

> +{
> +	int i;
> +
> +	for (i = 0; i < size; i++) {
> +		if (ratio == available_ratio[i])
> +			return i;
> +	}
> +
> +	return -EINVAL;
> +}
> +
> +static int ad7380_write_raw(struct iio_dev *indio_dev,
> +			    struct iio_chan_spec const *chan, int val,
> +			    int val2, long mask)
> +{
> +	struct ad7380_state *st = iio_priv(indio_dev);
> +	int ret, osr;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
> +		osr = check_osr(ad7380_normal_average_oversampling_ratios,
> +				ARRAY_SIZE(ad7380_normal_average_oversampling_rati
> os),
> +				val);
> +
> +		if (osr < 0)
> +			return osr;
> +
> +		iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
> +			ret = regmap_update_bits(st->regmap,
> AD7380_REG_ADDR_CONFIG1,
> +						 AD7380_CONFIG1_OSR,
> +						 FIELD_PREP(AD7380_CONFIG1_OSR,
> osr));
> +
> +			if (ret)
> +				return ret;
> +
> +			st->oversampling_ratio = val;
> +
> +			/*
> +			 * Perform a soft reset.
> +			 * This will flush the oversampling block and FIFO but
> will
> +			 * maintain the content of the configurable registers.
> +			 */
> +			ret = regmap_update_bits(st->regmap,
> AD7380_REG_ADDR_CONFIG2,
> +						 AD7380_CONFIG2_RESET,
> +						 FIELD_PREP(AD7380_CONFIG2_RESET,
> +							   
> AD7380_CONFIG2_RESET_SOFT));
> +		}
> +		return 0;

return ret;

Or you may be asked to directly return in regmap_update_bits() and use unreachable()
here.

>  	default:
>  		return -EINVAL;
>  	}
> @@ -435,6 +540,8 @@ static int ad7380_read_raw(struct iio_dev *indio_dev,
>  
>  static const struct iio_info ad7380_info = {
>  	.read_raw = &ad7380_read_raw,
> +	.read_avail = &ad7380_read_avail,
> +	.write_raw = &ad7380_write_raw,
>  	.debugfs_reg_access = &ad7380_debugfs_reg_access,
>  };
>  
> @@ -458,6 +565,12 @@ static int ad7380_init(struct ad7380_state *st, struct
> regulator *vref)
>  	if (ret < 0)
>  		return ret;
>  
> +	/* Disable oversampling by default.
> +	 * This is the default value after reset,
> +	 * so just initialize internal data
> +	 */

Your comment block is not in accordance with coding style. checkpatch should complain
about this.

> +	st->oversampling_ratio = 1;
> +
>  	/* SPI 1-wire mode */
>  	return regmap_update_bits(st->regmap, AD7380_REG_ADDR_CONFIG2,
>  				  AD7380_CONFIG2_SDO,
> 


  reply	other threads:[~2024-05-06  8:20 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-01 14:55 [PATCH RFC v6 00/10] iio: adc: add new ad7380 driver Julien Stephan
2024-05-01 14:55 ` [PATCH RFC v6 01/10] dt-bindings: iio: adc: Add binding for AD7380 ADCs Julien Stephan
2024-05-01 14:55 ` [PATCH RFC v6 02/10] iio: adc: ad7380: new driver " Julien Stephan
2024-05-06 13:56   ` Jonathan Cameron
2024-05-01 14:55 ` [PATCH RFC v6 03/10] dt-bindings: iio: adc: ad7380: add pseudo-differential parts Julien Stephan
2024-05-01 14:55 ` [PATCH RFC v6 04/10] iio: adc: ad7380: add support for " Julien Stephan
2024-05-01 14:55 ` [PATCH RFC v6 05/10] iio: adc: ad7380: prepare for parts with more channels Julien Stephan
2024-05-01 14:55 ` [PATCH RFC v6 06/10] dt-bindings: iio: adc: ad7380: add support for ad738x-4 4 channels variants Julien Stephan
2024-05-01 14:55 ` [PATCH RFC v6 07/10] " Julien Stephan
2024-05-01 14:55 ` [PATCH RFC v6 08/10] iio: adc: ad7380: add oversampling support Julien Stephan
2024-05-06  8:20   ` Nuno Sá [this message]
2024-05-06 14:05   ` Jonathan Cameron
2024-05-01 14:55 ` [PATCH RFC v6 09/10] iio: adc: ad7380: add support for rolling average oversampling mode Julien Stephan
2024-05-06  8:33   ` Nuno Sá
2024-05-06 14:17   ` Jonathan Cameron
2024-05-06 15:04     ` David Lechner
2024-05-08 11:25       ` Jonathan Cameron
2024-05-09 22:01         ` David Lechner
2024-05-11 11:47           ` Jonathan Cameron
2024-05-01 14:55 ` [PATCH RFC v6 10/10] iio: adc: ad7380: add support for resolution boost Julien Stephan
2024-05-06  8:55   ` Nuno Sá
2024-05-06 13:46     ` Jonathan Cameron
2024-05-06 14:20       ` Jonathan Cameron
2024-05-06 14:44       ` David Lechner
2024-05-08 11:32         ` Jonathan Cameron
2024-05-06 15:09     ` David Lechner
2024-05-06 14:29   ` Jonathan Cameron
2024-05-06 21:45   ` David Lechner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1be8a603c1db69278181089e3653b6312bcc99da.camel@gmail.com \
    --to=noname.nuno@gmail.com \
    --cc=Michael.Hennerich@analog.com \
    --cc=broonie@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=jstephan@baylibre.com \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=lars@metafoo.de \
    --cc=lgirdwood@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lkp@intel.com \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).