Skip to content

Commit 47280af

Browse files
Wolfram Sangwsakernel
authored andcommitted
i2c: rcar: calculate divider instead of brute-forcing it
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> Signed-off-by: Wolfram Sang <wsa@kernel.org>
1 parent d728579 commit 47280af

1 file changed

Lines changed: 8 additions & 16 deletions

File tree

drivers/i2c/busses/i2c-rcar.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -303,24 +303,16 @@ static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv)
303303
round = (round + 500) / 1000;
304304

305305
/*
306-
* SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
307-
*
308-
* Calculation result (= SCL) should be less than
309-
* bus_speed for hardware safety
310-
*
311-
* We could use something along the lines of
312-
* div = ick / (bus_speed + 1) + 1;
313-
* scgd = (div - 20 - round + 7) / 8;
314-
* scl = ick / (20 + (scgd * 8) + round);
315-
* (not fully verified) but that would get pretty involved
306+
* SCL = ick / (20 + 8 * SCGD + F[(ticf + tr + intd) * ick])
307+
* 20 + 8 * SCGD + F[...] = ick / SCL
308+
* SCGD = ((ick / SCL) - 20 - F[...]) / 8
309+
* Result (= SCL) should be less than bus_speed for hardware safety
316310
*/
317-
for (scgd = 0; scgd < 0x40; scgd++) {
318-
scl = ick / (20 + (scgd * 8) + round);
319-
if (scl <= t.bus_freq_hz)
320-
break;
321-
}
311+
scgd = DIV_ROUND_UP(ick, t.bus_freq_hz ?: 1);
312+
scgd = DIV_ROUND_UP(scgd - 20 - round, 8);
313+
scl = ick / (20 + 8 * scgd + round);
322314

323-
if (scgd == 0x40)
315+
if (scgd > 0x3f)
324316
goto err_no_val;
325317

326318
dev_dbg(dev, "clk %u/%u(%lu), round %u, CDF: %u, SCGD: %u\n",

0 commit comments

Comments
 (0)