diff mbox series

[3/5] i2c: rcar: calculate divider instead of brute-forcing it

Message ID 20230906200024.5305-4-wsa+renesas@sang-engineering.com
State New
Headers show
Series i2c: clock calculation cleanups for Renesas devices | expand

Commit Message

Wolfram Sang Sept. 6, 2023, 8 p.m. UTC
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(-)

Comments

Geert Uytterhoeven Sept. 19, 2023, 8:33 a.m. UTC | #1
On Wed, 6 Sep 2023, Wolfram Sang wrote:
> 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>

Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>

Gr{oetje,eeting}s,

 						Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
 							    -- Linus Torvalds
diff mbox series

Patch

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",