From: Wolfram Sang <wsa+renesas@sang-engineering.com>
To: linux-renesas-soc@vger.kernel.org
Cc: Wolfram Sang <wsa+renesas@sang-engineering.com>,
Andi Shyti <andi.shyti@kernel.org>,
linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 3/5] i2c: rcar: calculate divider instead of brute-forcing it
Date: Wed, 6 Sep 2023 22:00:21 +0200 [thread overview]
Message-ID: <20230906200024.5305-4-wsa+renesas@sang-engineering.com> (raw)
In-Reply-To: <20230906200024.5305-1-wsa+renesas@sang-engineering.com>
Instead of trying all values, we can actually compute it as the comment
suggests. It is unclear what the comment means with "involved", it works
nicely.
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
@Geert: I hope the formulas are more clear now?
@Andi: I don't think that replacing 0x3f with a define helps
understanding the code, but I am open for discussion.
drivers/i2c/busses/i2c-rcar.c | 24 ++++++++----------------
1 file changed, 8 insertions(+), 16 deletions(-)
diff --git a/drivers/i2c/busses/i2c-rcar.c b/drivers/i2c/busses/i2c-rcar.c
index 4bf47e35094f..2585092bed52 100644
--- a/drivers/i2c/busses/i2c-rcar.c
+++ b/drivers/i2c/busses/i2c-rcar.c
@@ -303,24 +303,16 @@ static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv)
round = (round + 500) / 1000;
/*
- * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
- *
- * Calculation result (= SCL) should be less than
- * bus_speed for hardware safety
- *
- * We could use something along the lines of
- * div = ick / (bus_speed + 1) + 1;
- * scgd = (div - 20 - round + 7) / 8;
- * scl = ick / (20 + (scgd * 8) + round);
- * (not fully verified) but that would get pretty involved
+ * SCL = ick / (20 + 8 * SCGD + F[(ticf + tr + intd) * ick])
+ * 20 + 8 * SCGD + F[...] = ick / SCL
+ * SCGD = ((ick / SCL) - 20 - F[...]) / 8
+ * Result (= SCL) should be less than bus_speed for hardware safety
*/
- for (scgd = 0; scgd < 0x40; scgd++) {
- scl = ick / (20 + (scgd * 8) + round);
- if (scl <= t.bus_freq_hz)
- break;
- }
+ scgd = DIV_ROUND_UP(ick, t.bus_freq_hz ?: 1);
+ scgd = DIV_ROUND_UP(scgd - 20 - round, 8);
+ scl = ick / (20 + 8 * scgd + round);
- if (scgd == 0x40)
+ if (scgd > 0x3f)
goto err_no_val;
dev_dbg(dev, "clk %u/%u(%lu), round %u, CDF: %u, SCGD: %u\n",
--
2.35.1
next prev parent reply other threads:[~2023-09-06 20:01 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-06 20:00 [PATCH 0/5] i2c: clock calculation cleanups for Renesas devices Wolfram Sang
2023-09-06 20:00 ` [PATCH 1/5] i2c: rcar: avoid non-standard use of goto Wolfram Sang
2023-09-08 15:03 ` Geert Uytterhoeven
2023-09-06 20:00 ` [PATCH 2/5] i2c: rcar: properly format a debug output Wolfram Sang
2023-09-08 15:04 ` Geert Uytterhoeven
2023-09-06 20:00 ` Wolfram Sang [this message]
2023-09-19 8:33 ` [PATCH 3/5] i2c: rcar: calculate divider instead of brute-forcing it Geert Uytterhoeven
2023-09-06 20:00 ` [PATCH 4/5] i2c: rcar: remove open coded DIV_ROUND_CLOSEST Wolfram Sang
2023-09-08 15:06 ` Geert Uytterhoeven
2023-09-06 20:00 ` [PATCH 5/5] i2c: riic: avoid potential division by zero Wolfram Sang
2023-09-07 6:42 ` Biju Das
2023-09-07 6:44 ` Biju Das
2023-09-07 12:08 ` Wolfram Sang
2023-09-08 15:09 ` Geert Uytterhoeven
2023-09-19 9:12 ` [PATCH 0/5] i2c: clock calculation cleanups for Renesas devices Wolfram Sang
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=20230906200024.5305-4-wsa+renesas@sang-engineering.com \
--to=wsa+renesas@sang-engineering.com \
--cc=andi.shyti@kernel.org \
--cc=linux-i2c@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-renesas-soc@vger.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