Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp413709yba; Sun, 31 Mar 2019 02:47:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqyjSiyQ4gzVGODP7EWz0/7XcUWXn2BTHLFqQ2o5onrX3PqmmwBvzDcfBw9ElvNze+ioi67Z X-Received: by 2002:a63:1048:: with SMTP id 8mr31642321pgq.70.1554025628338; Sun, 31 Mar 2019 02:47:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554025628; cv=none; d=google.com; s=arc-20160816; b=q8YCqx3bP32I/XZO1PXorA6/2pIpdJTtCoZpG/QZLzrI6YA/+Mfdr/Fxm9fLOn9te/ tbg0q82dm1QfRQZ6Ei7/fTpxvUXXkP3I5ubPquiA7TtAwV55QwU17MSqy5Eur0q2Pe5P W1Si4dCn9CH9uE4woZxS9S/pM9M8H29KIe01bXADDbyqjHE+eg2qno6kxiMs2x8RZEFy XN2o5Y6QHsMrlQi6vvir8TS2wPJ9jk4hmRj1m7GoXXVoCpqrt/6Z8Vhu4RsZY7zcCclM hBZS7uI0q1EfhWwuvYMPYgRHSjI3cqs2jKGlXDcjf5SsNkVa/xTFhpPnBO8uEkNwAOJh GoDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=bkdb82msWZWleX6QGEmB1hwWhlzOlvpfWwuV/uEgMHs=; b=FQ2r6smnNldrqcUUd9jCE+/IcUaBAbXLaeMuRX2XVeNoIbSTxbGQOzByZNT9JweSsh TnHH4asHijZbB4VJ5X6E3+WXL4F8Em3cVDB8eRqARU2JuXDjkY3z4YKVD8c4PVp6irKH pv/cB+4WrCrOp8Om5lRh9KCorHHs7nUpr3xyCH/ajrWRPED0qNxoY8LmHk/1J2oKnzIp KWBLDixYDtwWhkrYhhXl79/KotQvzGBMdRKUtZHp+0mKMbwyNeJYav+KNZlcnRItvKLt Czjh5fXNrnB4x/gyekes1nwnv7B5EJEBxDgfgmoVXRdtKiJZDipjYjBEOpCN724IAAzv UDJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wbu3ASz6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 89si6622545pld.265.2019.03.31.02.46.50; Sun, 31 Mar 2019 02:47:08 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wbu3ASz6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726863AbfCaJqL (ORCPT + 99 others); Sun, 31 Mar 2019 05:46:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:55260 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726490AbfCaJqL (ORCPT ); Sun, 31 Mar 2019 05:46:11 -0400 Received: from archlinux (cpc91196-cmbg18-2-0-cust659.5-4.cable.virginm.net [81.96.234.148]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3D6AE20850; Sun, 31 Mar 2019 09:46:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1554025569; bh=mvatAZ8EMowd44cb0Yyla/0+OwnoQ3rNaVBn3xAnz4c=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=wbu3ASz6Loab3F9fmV43p9D6pUTLuvgNJ/qs4QrwEiAS1luQCMlC4O46HG7gxLVSa ElGtUTUSQu9DAtfrTii8e2MU5pvtrjB7Drbptmoc48ke2U6TiD6htUQAd6O4y9J0rK CzCmEig2wHjUKH6tgQm4fLEKsL3JrTj4bX+cRvFA= Date: Sun, 31 Mar 2019 10:46:02 +0100 From: Jonathan Cameron To: Fabrice Gasnier Cc: , , , , , , , , , , Subject: Re: [PATCH] iio: adc: stm32-dfsdm: improve sampling frequency accuracy Message-ID: <20190331104602.209cb9a0@archlinux> In-Reply-To: <1553523841-16315-1-git-send-email-fabrice.gasnier@st.com> References: <1553523841-16315-1-git-send-email-fabrice.gasnier@st.com> X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 25 Mar 2019 15:24:01 +0100 Fabrice Gasnier 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 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;