Skip to contentSkip to conversation

i2c: clock calculation cleanups for Renesas devices

PATCH Applied Wolfram Sang 3 participants 15 messages Sep 6, 2023 – Sep 19, 2023

Subsystems (4)
lore
Share & tools
lore

Wolfram Sang

author maintainer
#0 lore raw

cover i2c: clock calculation cleanups for Renesas devices

While implementing FastMode+ support for the R-Car IP core, I noticed potential for other cleanups. It turned out that it makes sense to apply them first, so here is the series. Tested on a Renesas Falcon board with an R-Car V3U. The calculated values are identical for 100 and 400kHz. The RIIC patch is build tested only.

Looking forward to comments!

Wolfram Sang (5):
i2c: rcar: avoid non-standard use of goto
i2c: rcar: properly format a debug output
i2c: rcar: calculate divider instead of brute-forcing it
i2c: rcar: remove open coded DIV_ROUND_CLOSEST
i2c: riic: avoid potential division by zero

 drivers/i2c/busses/i2c-rcar.c | 41 +++++++++++++++--------------------
 drivers/i2c/busses/i2c-riic.c |  2 +-
 2 files changed, 19 insertions(+), 24 deletions(-)
Signature · 1 line
2.35.1
author maintainer
#1 lore raw

PATCH 1/5 i2c: rcar: avoid non-standard use of goto

Kernel functions goto somewhere on error conditions. Using goto for the default path is irritating. Let's bail out on error instead and use a proper retval.

---

I dropped the Rev-bys because I think the changes are not trivial. YMMV.

 drivers/i2c/busses/i2c-rcar.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-rcar.c +8 -4
index a32a93f9a60d..49dfbeebf6b8 100644
@@ -317,12 +317,12 @@ static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv)
 	for (scgd = 0; scgd < 0x40; scgd++) {
 		scl = ick / (20 + (scgd * 8) + round);
 		if (scl <= t.bus_freq_hz)
-			goto scgd_find;
+			break;
 	}
-	dev_err(dev, "it is impossible to calculate best SCL\n");
-	return -EIO;
 
-scgd_find:
+	if (scgd == 0x40)
+		goto err_no_val;
+
 	dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
 		scl, t.bus_freq_hz, rate, round, cdf, scgd);
 
@@ -330,6 +330,10 @@ static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv)
 	priv->icccr = scgd << cdf_width | cdf;
 
 	return 0;
+
+err_no_val:
+	dev_err(dev, "it is impossible to calculate best SCL\n");
+	return -EINVAL;
 }
 
 /*
Signature · 1 line
2.35.1
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] i2c: rcar: avoid non-standard use of goto

Quoted 5 lines · Wolfram Sang

Kernel functions goto somewhere on error conditions. Using goto for the default path is irritating. Let's bail out on error instead and use a proper retval.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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

author maintainer
#3 lore raw

PATCH 2/5 i2c: rcar: properly format a debug output

In reply to Wolfram Sang

Use proper types and spacing.

---
 drivers/i2c/busses/i2c-rcar.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-rcar.c +1 -1
index 49dfbeebf6b8..4bf47e35094f 100644
@@ -323,7 +323,7 @@ static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv)
 	if (scgd == 0x40)
 		goto err_no_val;
 
-	dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
+	dev_dbg(dev, "clk %u/%u(%lu), round %u, CDF: %u, SCGD: %u\n",
 		scl, t.bus_freq_hz, rate, round, cdf, scgd);
 
 	/* keep icccr value */
Signature · 1 line
2.35.1
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] i2c: rcar: properly format a debug output

Quoted 3 lines · Wolfram Sang

Use proper types and spacing.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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

author maintainer
#5 lore raw

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

In reply to Wolfram Sang

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.

---

@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(-)
drivers/i2c/busses/i2c-rcar.c +8 -16
index 4bf47e35094f..2585092bed52 100644
@@ -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",
Signature · 1 line
2.35.1
Reviewed-by on v1 3/5
#6 lore raw

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

Quoted 5 lines · Wolfram Sang

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.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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

author maintainer
#7 lore raw

PATCH 4/5 i2c: rcar: remove open coded DIV_ROUND_CLOSEST

In reply to Wolfram Sang

It improves readability if we use the available helper.

---
 drivers/i2c/busses/i2c-rcar.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-rcar.c +3 -4
index 2585092bed52..5e97635faf78 100644
@@ -291,16 +291,15 @@ static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv)
 	ick = rate / (cdf + 1);
 
 	/*
-	 * it is impossible to calculate large scale
-	 * number on u32. separate it
+	 * It is impossible to calculate a large scale number on u32. Separate it.
 	 *
 	 * F[(ticf + tr + intd) * ick] with sum = (ticf + tr + intd)
 	 *  = F[sum * ick / 1000000000]
 	 *  = F[(ick / 1000000) * sum / 1000]
 	 */
 	sum = t.scl_fall_ns + t.scl_rise_ns + t.scl_int_delay_ns;
-	round = (ick + 500000) / 1000000 * sum;
-	round = (round + 500) / 1000;
+	round = DIV_ROUND_CLOSEST(ick, 1000000);
+	round = DIV_ROUND_CLOSEST(round * sum, 1000);
 
 	/*
 	 * SCL	= ick / (20 + 8 * SCGD + F[(ticf + tr + intd) * ick])
Signature · 1 line
2.35.1
Reviewed-by on v1 4/5
#8 lore raw

Re: [PATCH 4/5] i2c: rcar: remove open coded DIV_ROUND_CLOSEST

Quoted 3 lines · Wolfram Sang

It improves readability if we use the available helper.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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

author maintainer
#9 lore raw

PATCH 5/5 i2c: riic: avoid potential division by zero

In reply to Wolfram Sang

Value comes from DT, so it could be 0. Unlikely, but could be.

---
 drivers/i2c/busses/i2c-riic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-riic.c +1 -1
index f0ee8871d5ae..e43ff483c56e 100644
@@ -313,7 +313,7 @@ static int riic_init_hw(struct riic_dev *riic, struct i2c_timings *t)
 	 * frequency with only 62 clock ticks max (31 high, 31 low).
 	 * Aim for a duty of 60% LOW, 40% HIGH.
 	 */
-	total_ticks = DIV_ROUND_UP(rate, t->bus_freq_hz);
+	total_ticks = DIV_ROUND_UP(rate, t->bus_freq_hz ?: 1);
 
 	for (cks = 0; cks < 7; cks++) {
 		/*
Signature · 1 line
2.35.1
#10 lore raw

RE: [PATCH 5/5] i2c: riic: avoid potential division by zero

Hi Wolfram Sang,

Quoted 14 lines

Subject: [PATCH 5/5] i2c: riic: avoid potential division by zero

Value comes from DT, so it could be 0. Unlikely, but could be.

---
 drivers/i2c/busses/i2c-riic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-riic.c +0 -0
index f0ee8871d5ae..e43ff483c56e 100644

drivers/i2c/busses/i2c-riic.c static int riic_init_hw(struct riic_dev *riic, struct · line 317

Quoted 6 lines
drivers/i2c/busses/i2c-riic.c +1 -1
@@ -313,7 +313,7 @@ static int riic_init_hw(struct riic_dev *riic, struct
 i2c_timings *t)
 	 * frequency with only 62 clock ticks max (31 high, 31 low).
 	 * Aim for a duty of 60% LOW, 40% HIGH.
 	 */
-	total_ticks = DIV_ROUND_UP(rate, t->bus_freq_hz);
+	total_ticks = DIV_ROUND_UP(rate, t->bus_freq_hz ?: 1);

Not sure clamping function min_t/min(t->bus_freq_hz, 1)
Or ternary condition is good in this case for avoiding potential division by 0?

Cheers,
Biju

Quoted 5 lines
drivers/i2c/busses/i2c-riic.c +0 -1
 	for (cks = 0; cks < 7; cks++) {
 		/*
--

2.35.1

#11 lore raw

RE: [PATCH 5/5] i2c: riic: avoid potential division by zero

Quoted 13 lines

Subject: RE: [PATCH 5/5] i2c: riic: avoid potential division by zero

Hi Wolfram Sang,

Quoted 9 lines

Subject: [PATCH 5/5] i2c: riic: avoid potential division by zero

Value comes from DT, so it could be 0. Unlikely, but could be.

---
 drivers/i2c/busses/i2c-riic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

drivers/i2c/busses/i2c-riic.c static int riic_init_hw(struct riic_dev *riic, · line 317

Quoted 12 lines
(quoted diff) +0 -0

b/drivers/i2c/busses/i2c-riic.c index f0ee8871d5ae..e43ff483c56e 100644

drivers/i2c/busses/i2c-riic.c +1 -1
@@ -313,7 +313,7 @@ static int riic_init_hw(struct riic_dev *riic,
 struct i2c_timings *t)
 	 * frequency with only 62 clock ticks max (31 high, 31 low).
 	 * Aim for a duty of 60% LOW, 40% HIGH.
 	 */
-	total_ticks = DIV_ROUND_UP(rate, t->bus_freq_hz);
+	total_ticks = DIV_ROUND_UP(rate, t->bus_freq_hz ?: 1);

Not sure clamping function min_t/min(t->bus_freq_hz, 1)

Typo min->max.

author maintainer
#12 lore raw

Re: [PATCH 5/5] i2c: riic: avoid potential division by zero

In reply to Biju Das
Quoted 6 lines

drivers/i2c/busses/i2c-riic.c static int riic_init_hw(struct riic_dev *riic, struct i2c_timings *t) · line 316

Quoted 3 lines
drivers/i2c/busses/i2c-riic.c +1 -1
-	total_ticks = DIV_ROUND_UP(rate, t->bus_freq_hz);
+	total_ticks = DIV_ROUND_UP(rate, t->bus_freq_hz ?: 1);

Not sure clamping function min_t/min(t->bus_freq_hz, 1)
Or ternary condition is good in this case for avoiding potential division by 0?

Both work. I chose the ternary because I believe it involves less computation and a tad tighter syntax.

Reviewed-by on v1 5/5
#13 lore raw

Re: [PATCH 5/5] i2c: riic: avoid potential division by zero

In reply to Wolfram Sang
Quoted 3 lines · Wolfram Sang

Value comes from DT, so it could be 0. Unlikely, but could be.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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

author maintainer Applied
#14 lore raw
In reply to Wolfram Sang
Quoted 9 lines · Wolfram Sang

While implementing FastMode+ support for the R-Car IP core, I noticed potential for other cleanups. It turned out that it makes sense to apply them first, so here is the series. Tested on a Renesas Falcon board with an R-Car V3U. The calculated values are identical for 100 and 400kHz. The RIIC patch is build tested only.

Looking forward to comments!

Quoted 7 lines

Wolfram Sang (5):
i2c: rcar: avoid non-standard use of goto
i2c: rcar: properly format a debug output
i2c: rcar: calculate divider instead of brute-forcing it
i2c: rcar: remove open coded DIV_ROUND_CLOSEST
i2c: riic: avoid potential division by zero

Applied to for-next, thanks!