linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Fabrice Gasnier <fabrice.gasnier@st.com>
Cc: <linux-arm-kernel@lists.infradead.org>,
	<linux-kernel@vger.kernel.org>, <mcoquelin.stm32@gmail.com>,
	<alexandre.torgue@st.com>, <linux-iio@vger.kernel.org>,
	<lars@metafoo.de>, <knaack.h@gmx.de>, <pmeerw@pmeerw.net>,
	<linux-stm32@st-md-mailman.stormreply.com>,
	<arnaud.pouliquen@st.com>, <olivier.moysan@st.com>
Subject: Re: [PATCH] iio: adc: stm32-dfsdm: improve sampling frequency accuracy
Date: Sun, 31 Mar 2019 10:46:02 +0100	[thread overview]
Message-ID: <20190331104602.209cb9a0@archlinux> (raw)
In-Reply-To: <1553523841-16315-1-git-send-email-fabrice.gasnier@st.com>

On Mon, 25 Mar 2019 15:24:01 +0100
Fabrice Gasnier <fabrice.gasnier@st.com> wrote:

> The sample frequency is driven using the oversampling ratio depending
> on the SPI bus frequency.
> Currently, oversampling ratio is computed by an entire division:
> - spi_freq / sample_freq. This may result in inaccurate value.
> Using DIV_ROUND_CLOSEST improves resulting sample frequency, which is
> useful for audio that requests fixed rates (such as: 8, 16 or 32 kHz).
> BTW, introduce new routine to re-factor sample frequency setting, and
> move frequency accuracy message from warning to debug level.
> 
> Signed-off-by: Fabrice Gasnier <fabrice.gasnier@st.com>

Would have preferred this as two patches as it would have made review easier
by making it clear what the functional change is.

Still that is mostly me just wanting an easy life ;)

Applied to the togreg branch of iio.git and pushed out as testing for
the autobuilders to play with it.

Thanks,

Jonathan

> ---
>  drivers/iio/adc/stm32-dfsdm-adc.c | 56 +++++++++++++++++++++------------------
>  1 file changed, 30 insertions(+), 26 deletions(-)
> 
> diff --git a/drivers/iio/adc/stm32-dfsdm-adc.c b/drivers/iio/adc/stm32-dfsdm-adc.c
> index 531ca7e..051561c 100644
> --- a/drivers/iio/adc/stm32-dfsdm-adc.c
> +++ b/drivers/iio/adc/stm32-dfsdm-adc.c
> @@ -558,13 +558,38 @@ static ssize_t dfsdm_adc_audio_get_spiclk(struct iio_dev *indio_dev,
>  	return snprintf(buf, PAGE_SIZE, "%d\n", adc->spi_freq);
>  }
>  
> +static int dfsdm_adc_set_samp_freq(struct iio_dev *indio_dev,
> +				   unsigned int sample_freq,
> +				   unsigned int spi_freq)
> +{
> +	struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
> +	struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id];
> +	unsigned int oversamp;
> +	int ret;
> +
> +	oversamp = DIV_ROUND_CLOSEST(spi_freq, sample_freq);
> +	if (spi_freq % sample_freq)
> +		dev_dbg(&indio_dev->dev,
> +			"Rate not accurate. requested (%u), actual (%u)\n",
> +			sample_freq, spi_freq / oversamp);
> +
> +	ret = stm32_dfsdm_set_osrs(fl, 0, oversamp);
> +	if (ret < 0) {
> +		dev_err(&indio_dev->dev, "No filter parameters that match!\n");
> +		return ret;
> +	}
> +	adc->sample_freq = spi_freq / oversamp;
> +	adc->oversamp = oversamp;
> +
> +	return 0;
> +}
> +
>  static ssize_t dfsdm_adc_audio_set_spiclk(struct iio_dev *indio_dev,
>  					  uintptr_t priv,
>  					  const struct iio_chan_spec *chan,
>  					  const char *buf, size_t len)
>  {
>  	struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
> -	struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id];
>  	struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel];
>  	unsigned int sample_freq = adc->sample_freq;
>  	unsigned int spi_freq;
> @@ -583,17 +608,9 @@ static ssize_t dfsdm_adc_audio_set_spiclk(struct iio_dev *indio_dev,
>  		return -EINVAL;
>  
>  	if (sample_freq) {
> -		if (spi_freq % sample_freq)
> -			dev_warn(&indio_dev->dev,
> -				 "Sampling rate not accurate (%d)\n",
> -				 spi_freq / (spi_freq / sample_freq));
> -
> -		ret = stm32_dfsdm_set_osrs(fl, 0, (spi_freq / sample_freq));
> -		if (ret < 0) {
> -			dev_err(&indio_dev->dev,
> -				"No filter parameters that match!\n");
> +		ret = dfsdm_adc_set_samp_freq(indio_dev, sample_freq, spi_freq);
> +		if (ret < 0)
>  			return ret;
> -		}
>  	}
>  	adc->spi_freq = spi_freq;
>  
> @@ -1068,22 +1085,9 @@ static int stm32_dfsdm_write_raw(struct iio_dev *indio_dev,
>  			spi_freq = adc->spi_freq;
>  		}
>  
> -		if (spi_freq % val)
> -			dev_warn(&indio_dev->dev,
> -				 "Sampling rate not accurate (%d)\n",
> -				 spi_freq / (spi_freq / val));
> -
> -		ret = stm32_dfsdm_set_osrs(fl, 0, (spi_freq / val));
> -		if (ret < 0) {
> -			dev_err(&indio_dev->dev,
> -				"Not able to find parameter that match!\n");
> -			iio_device_release_direct_mode(indio_dev);
> -			return ret;
> -		}
> -		adc->sample_freq = val;
> +		ret = dfsdm_adc_set_samp_freq(indio_dev, val, spi_freq);
>  		iio_device_release_direct_mode(indio_dev);
> -
> -		return 0;
> +		return ret;
>  	}
>  
>  	return -EINVAL;


      reply	other threads:[~2019-03-31  9:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-25 14:24 [PATCH] iio: adc: stm32-dfsdm: improve sampling frequency accuracy Fabrice Gasnier
2019-03-31  9:46 ` Jonathan Cameron [this message]

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=20190331104602.209cb9a0@archlinux \
    --to=jic23@kernel.org \
    --cc=alexandre.torgue@st.com \
    --cc=arnaud.pouliquen@st.com \
    --cc=fabrice.gasnier@st.com \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-stm32@st-md-mailman.stormreply.com \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=olivier.moysan@st.com \
    --cc=pmeerw@pmeerw.net \
    /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 NNTP newsgroup(s).