linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Fabrice Gasnier <fabrice.gasnier@st.com>
To: <jic23@kernel.org>
Cc: <linux-arm-kernel@lists.infradead.org>,
	<linux-kernel@vger.kernel.org>, <mcoquelin.stm32@gmail.com>,
	<alexandre.torgue@st.com>, <fabrice.gasnier@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: [PATCH v2 1/8] iio: adc: stm32-dfsdm: make spi_master_freq more accurate
Date: Thu, 21 Mar 2019 17:47:22 +0100	[thread overview]
Message-ID: <1553186849-6261-2-git-send-email-fabrice.gasnier@st.com> (raw)
In-Reply-To: <1553186849-6261-1-git-send-email-fabrice.gasnier@st.com>

Current ckout divider may be set to a value that makes ckout to exceed
spi-max-frequency. Rather use lower value (e.g. round up divider when
ckout isn't accurate).

Also when the SPI clock isn't accurate, 'spi_master_freq' is filled in
with expected frequency. Use computed value instead to be more accurate:
- e.g. source clock / (CKOUTDIV + 1)

Enforce checks on the divider: ckoutdiv range can be from 1-255 to provide
divider of 2-256.

Signed-off-by: Fabrice Gasnier <fabrice.gasnier@st.com>
---
Changes in v2:
- rework the way to determine ckoutdiv bitfield: deal with divider directly
  to explicitly handle the rounding, the range constraints of 2-256 and the
  divider = ckoutdiv + 1.
---
 drivers/iio/adc/stm32-dfsdm-core.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/adc/stm32-dfsdm-core.c b/drivers/iio/adc/stm32-dfsdm-core.c
index bf089f5..472b809 100644
--- a/drivers/iio/adc/stm32-dfsdm-core.c
+++ b/drivers/iio/adc/stm32-dfsdm-core.c
@@ -199,7 +199,7 @@ static int stm32_dfsdm_parse_of(struct platform_device *pdev,
 {
 	struct device_node *node = pdev->dev.of_node;
 	struct resource *res;
-	unsigned long clk_freq;
+	unsigned long clk_freq, divider;
 	unsigned int spi_freq, rem;
 	int ret;
 
@@ -243,13 +243,20 @@ static int stm32_dfsdm_parse_of(struct platform_device *pdev,
 		return 0;
 	}
 
-	priv->spi_clk_out_div = div_u64_rem(clk_freq, spi_freq, &rem) - 1;
-	if (!priv->spi_clk_out_div) {
-		/* spi_clk_out_div == 0 means ckout is OFF */
+	divider = div_u64_rem(clk_freq, spi_freq, &rem);
+	/* Round up divider when ckout isn't precise, not to exceed spi_freq */
+	if (rem)
+		divider++;
+
+	/* programmable divider is in range of [2:256] */
+	if (divider < 2 || divider > 256) {
 		dev_err(&pdev->dev, "spi-max-frequency not achievable\n");
 		return -EINVAL;
 	}
-	priv->dfsdm.spi_master_freq = spi_freq;
+
+	/* SPI clock output divider is: divider = CKOUTDIV + 1 */
+	priv->spi_clk_out_div = divider - 1;
+	priv->dfsdm.spi_master_freq = clk_freq / (priv->spi_clk_out_div + 1);
 
 	if (rem) {
 		dev_warn(&pdev->dev, "SPI clock not accurate\n");
-- 
2.7.4


  reply	other threads:[~2019-03-21 16:48 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-21 16:47 [PATCH v2 0/8] iio: adc: stm32-dfsdm: add buffer modes Fabrice Gasnier
2019-03-21 16:47 ` Fabrice Gasnier [this message]
2019-03-24 16:41   ` [PATCH v2 1/8] iio: adc: stm32-dfsdm: make spi_master_freq more accurate Jonathan Cameron
2019-03-21 16:47 ` [PATCH v2 2/8] iio: adc: stm32-dfsdm: continuous mode depends on current mode Fabrice Gasnier
2019-03-24 16:43   ` Jonathan Cameron
2019-03-21 16:47 ` [PATCH v2 3/8] iio: adc: stm32-dfsdm: move dma enable from start_conv() to start_dma() Fabrice Gasnier
2019-03-24 16:44   ` Jonathan Cameron
2019-03-21 16:47 ` [PATCH v2 4/8] iio: adc: stm32-dfsdm: move dma slave config to start routine Fabrice Gasnier
2019-03-24 16:45   ` Jonathan Cameron
2019-03-21 16:47 ` [PATCH v2 5/8] iio: adc: stm32-dfsdm: enable hw consumer Fabrice Gasnier
2019-03-24 16:45   ` Jonathan Cameron
2019-03-21 16:47 ` [PATCH v2 6/8] iio: adc: stm32-dfsdm: add support for scan mode Fabrice Gasnier
2019-03-24 16:46   ` Jonathan Cameron
2019-03-21 16:47 ` [PATCH v2 7/8] iio: adc: stm32-dfsdm: add support for buffer modes Fabrice Gasnier
2019-03-24 17:11   ` Jonathan Cameron
2019-03-21 16:47 ` [PATCH v2 8/8] iio: adc: stm32-dfsdm: claim direct mode for raw read and settings Fabrice Gasnier
2019-03-24 17:12   ` Jonathan Cameron

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=1553186849-6261-2-git-send-email-fabrice.gasnier@st.com \
    --to=fabrice.gasnier@st.com \
    --cc=alexandre.torgue@st.com \
    --cc=arnaud.pouliquen@st.com \
    --cc=jic23@kernel.org \
    --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).