devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Robert Hancock <robert.hancock@calian.com>
To: mturquette@baylibre.com, sboyd@kernel.org
Cc: mike.looijmans@topic.nl, robh+dt@kernel.org,
	linux-clk@vger.kernel.org, devicetree@vger.kernel.org,
	Robert Hancock <robert.hancock@calian.com>
Subject: [PATCH v3 3/9] clk: si5341: Avoid divide errors due to bogus register contents
Date: Thu, 25 Mar 2021 13:26:37 -0600	[thread overview]
Message-ID: <20210325192643.2190069-4-robert.hancock@calian.com> (raw)
In-Reply-To: <20210325192643.2190069-1-robert.hancock@calian.com>

If the Si5341 is being initially programmed and has no stored NVM
configuration, some of the register contents may contain unexpected
values, such as zeros, which could cause divide by zero errors during
driver initialization. Trap errors caused by zero registers or zero clock
rates which could result in divide errors later in the code.

Fixes: 3044a860fd ("clk: Add Si5341/Si5340 driver")
Signed-off-by: Robert Hancock <robert.hancock@calian.com>
---
 drivers/clk/clk-si5341.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/drivers/clk/clk-si5341.c b/drivers/clk/clk-si5341.c
index b8a960e927bc..ac1ccec2b681 100644
--- a/drivers/clk/clk-si5341.c
+++ b/drivers/clk/clk-si5341.c
@@ -624,6 +624,9 @@ static unsigned long si5341_synth_clk_recalc_rate(struct clk_hw *hw,
 			SI5341_SYNTH_N_NUM(synth->index), &n_num, &n_den);
 	if (err < 0)
 		return err;
+	/* Check for bogus/uninitialized settings */
+	if (!n_num || !n_den)
+		return 0;
 
 	/*
 	 * n_num and n_den are shifted left as much as possible, so to prevent
@@ -807,6 +810,9 @@ static long si5341_output_clk_round_rate(struct clk_hw *hw, unsigned long rate,
 {
 	unsigned long r;
 
+	if (!rate)
+		return 0;
+
 	r = *parent_rate >> 1;
 
 	/* If rate is an even divisor, no changes to parent required */
@@ -835,11 +841,16 @@ static int si5341_output_clk_set_rate(struct clk_hw *hw, unsigned long rate,
 		unsigned long parent_rate)
 {
 	struct clk_si5341_output *output = to_clk_si5341_output(hw);
-	/* Frequency divider is (r_div + 1) * 2 */
-	u32 r_div = (parent_rate / rate) >> 1;
+	u32 r_div;
 	int err;
 	u8 r[3];
 
+	if (!rate)
+		return -EINVAL;
+
+	/* Frequency divider is (r_div + 1) * 2 */
+	r_div = (parent_rate / rate) >> 1;
+
 	if (r_div <= 1)
 		r_div = 0;
 	else if (r_div >= BIT(24))
-- 
2.27.0


  parent reply	other threads:[~2021-03-25 19:28 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-25 19:26 [PATCH v3 0/9] Si5341 driver updates Robert Hancock
2021-03-25 19:26 ` [PATCH v3 1/9] dt-bindings: clock: clk-si5341: Add new attributes Robert Hancock
2021-03-27 15:27   ` Rob Herring
2021-06-28  3:02   ` Stephen Boyd
2021-03-25 19:26 ` [PATCH v3 2/9] clk: si5341: Wait for DEVICE_READY on startup Robert Hancock
2021-06-28  3:02   ` Stephen Boyd
2021-03-25 19:26 ` Robert Hancock [this message]
2021-06-28  3:02   ` [PATCH v3 3/9] clk: si5341: Avoid divide errors due to bogus register contents Stephen Boyd
2021-03-25 19:26 ` [PATCH v3 4/9] clk: si5341: Check for input clock presence and PLL lock on startup Robert Hancock
2021-06-28  3:02   ` Stephen Boyd
2021-03-25 19:26 ` [PATCH v3 5/9] clk: si5341: Update initialization magic Robert Hancock
2021-06-28  3:02   ` Stephen Boyd
2021-03-25 19:26 ` [PATCH v3 6/9] clk: si5341: Allow different output VDD_SEL values Robert Hancock
2021-06-28  3:02   ` Stephen Boyd
2021-03-25 19:26 ` [PATCH v3 7/9] clk: si5341: Add silabs,xaxb-ext-clk property Robert Hancock
2021-06-28  3:02   ` Stephen Boyd
2021-03-25 19:26 ` [PATCH v3 8/9] clk: si5341: Add silabs,iovdd-33 property Robert Hancock
2021-06-28  3:02   ` Stephen Boyd
2021-03-25 19:26 ` [PATCH v3 9/9] clk: si5341: Add sysfs properties to allow checking/resetting device faults Robert Hancock
2021-06-28  3:03   ` Stephen Boyd
2021-04-07 16:50 ` [PATCH v3 0/9] Si5341 driver updates Robert Hancock

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=20210325192643.2190069-4-robert.hancock@calian.com \
    --to=robert.hancock@calian.com \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=mike.looijmans@topic.nl \
    --cc=mturquette@baylibre.com \
    --cc=robh+dt@kernel.org \
    --cc=sboyd@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 NNTP newsgroup(s).