Skip to contentSkip to conversation

i2c: busses: Introduce and use HZ_PER_GHZ

PATCH Applied Andy Shevchenko 7 participants 17 messages Jan 12 – Jan 14

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

cover i2c: busses: Introduce and use HZ_PER_GHZ

A few drivers for I²C host controllers use HZ_PER_GHZ in numeric form. This is less readable and prone to subtle mistakes. Instead, introduce a well defined constant and use it in the drivers. No functional change implied except one case where 64-bit division replaced with 32-bit one.

Andy Shevchenko (7):
units: Add HZ_PER_GHZ
i2c: mlxbf: Use HZ_PER_GHZ constant instead of custom one
i2c: mt65xx: Use HZ_PER_GHZ constant instead of plain number
i2c: nomadik: Use HZ_PER_GHZ constant instead of plain number
i2c: rk3x: Use HZ_PER_GHZ constant instead of plain number
i2c: st: Use HZ_PER_GHZ constant instead of plain number
i2c: synquacer: Use HZ_PER_GHZ constant instead of plain number

 drivers/i2c/busses/i2c-mlxbf.c     |  5 ++---
 drivers/i2c/busses/i2c-mt65xx.c    | 11 ++++++-----
 drivers/i2c/busses/i2c-nomadik.c   |  7 ++++---
 drivers/i2c/busses/i2c-rk3x.c      | 12 ++++++------
 drivers/i2c/busses/i2c-st.c        |  3 ++-
 drivers/i2c/busses/i2c-synquacer.c |  3 ++-
 include/linux/units.h              |  3 +++
 7 files changed, 25 insertions(+), 19 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH 1/7 units: Add HZ_PER_GHZ

The is going to be a new user of the HZ_PER_GHZ definition besides possibly existing ones. Add that one to the header.

While at it, split Hz and kHz groups of the multipliers for better maintenance and readability.

---
 include/linux/units.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/units.h +3 -0
index 00e15de33eca..0c296a004e89 100644
@@ -25,9 +25,12 @@
 #define MICROHZ_PER_HZ		1000000UL
 #define MILLIHZ_PER_HZ		1000UL
 
+/* Hz based multipliers */
 #define HZ_PER_KHZ		1000UL
 #define HZ_PER_MHZ		1000000UL
+#define HZ_PER_GHZ		1000000000UL
 
+/* kHz based multipliers */
 #define KHZ_PER_MHZ		1000UL
 #define KHZ_PER_GHZ		1000000UL
 
Signature · 1 line
2.50.1
maintainer Reviewed-by on v1 1/7
#2 lore raw

Re: [PATCH v1 1/7] units: Add HZ_PER_GHZ

Quoted 7 lines · Andy Shevchenko

The is going to be a new user of the HZ_PER_GHZ definition besides possibly existing ones. Add that one to the header.

While at it, split Hz and kHz groups of the multipliers for better maintenance and readability.

This looks right to me:

Yours,
Linus Walleij

maintainer Reviewed-by on v1 1/7
#3 lore raw

Re: [PATCH v1 1/7] units: Add HZ_PER_GHZ

In reply to Andy Shevchenko

Hi Andy,

Quoted 7 lines · Andy Shevchenko

The is going to be a new user of the HZ_PER_GHZ definition besides possibly existing ones. Add that one to the header.

While at it, split Hz and kHz groups of the multipliers for better maintenance and readability.

I'm taking this via i2c if no one is against.

Andi

#4 lore raw

PATCH 2/7 i2c: mlxbf: Use HZ_PER_GHZ constant instead of custom one

In reply to Andy Shevchenko

Use HZ_PER_GHZ constant instead of custom one. No functional changes.

---
 drivers/i2c/busses/i2c-mlxbf.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-mlxbf.c +2 -3
index 8345f7e6385d..746f65989138 100644
@@ -20,6 +20,7 @@
 #include <linux/platform_device.h>
 #include <linux/string.h>
 #include <linux/string_choices.h>
+#include <linux/units.h>
 
 /* Defines what functionality is present. */
 #define MLXBF_I2C_FUNC_SMBUS_BLOCK \
@@ -72,8 +73,6 @@
 /* Constant used to determine the PLL frequency. */
 #define MLNXBF_I2C_COREPLL_CONST    16384ULL
 
-#define MLXBF_I2C_FREQUENCY_1GHZ  1000000000ULL
-
 /* PLL registers. */
 #define MLXBF_I2C_CORE_PLL_REG1         0x4
 #define MLXBF_I2C_CORE_PLL_REG2         0x8
@@ -1083,7 +1082,7 @@ static u32 mlxbf_i2c_get_ticks(struct mlxbf_i2c_priv *priv, u64 nanoseconds,
 	 *         Frequency
 	 */
 	frequency = priv->frequency;
-	ticks = div_u64(nanoseconds * frequency, MLXBF_I2C_FREQUENCY_1GHZ);
+	ticks = div_u64(nanoseconds * frequency, HZ_PER_GHZ);
 	/*
 	 * The number of ticks is rounded down and if minimum is equal to 1
 	 * then add one tick.
Signature · 1 line
2.50.1
#5 lore raw

PATCH 3/7 i2c: mt65xx: Use HZ_PER_GHZ constant instead of plain number

In reply to Andy Shevchenko

Use defined constant to avoid the possible mistakes and to provide an additional information on the units.

---
 drivers/i2c/busses/i2c-mt65xx.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/i2c/busses/i2c-mt65xx.c +6 -5
index aefdbee1f03c..cb4d3aa709d0 100644
@@ -24,6 +24,7 @@
 #include <linux/scatterlist.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
+#include <linux/units.h>
 
 #define I2C_RS_TRANSFER			(1 << 4)
 #define I2C_ARB_LOST			(1 << 3)
@@ -685,7 +686,7 @@ static int mtk_i2c_get_clk_div_restri(struct mtk_i2c *i2c,
  * Check and Calculate i2c ac-timing
  *
  * Hardware design:
- * sample_ns = (1000000000 * (sample_cnt + 1)) / clk_src
+ * sample_ns = (HZ_PER_GHZ * (sample_cnt + 1)) / clk_src
  * xxx_cnt_div =  spec->min_xxx_ns / sample_ns
  *
  * Sample_ns is rounded down for xxx_cnt_div would be greater
@@ -701,9 +702,8 @@ static int mtk_i2c_check_ac_timing(struct mtk_i2c *i2c,
 {
 	const struct i2c_spec_values *spec;
 	unsigned int su_sta_cnt, low_cnt, high_cnt, max_step_cnt;
-	unsigned int sda_max, sda_min, clk_ns, max_sta_cnt = 0x3f;
-	unsigned int sample_ns = div_u64(1000000000ULL * (sample_cnt + 1),
-					 clk_src);
+	unsigned int sda_max, sda_min, max_sta_cnt = 0x3f;
+	unsigned int clk_ns, sample_ns;
 
 	if (!i2c->dev_comp->timing_adjust)
 		return 0;
@@ -713,8 +713,9 @@ static int mtk_i2c_check_ac_timing(struct mtk_i2c *i2c,
 
 	spec = mtk_i2c_get_spec(check_speed);
 
+	sample_ns = div_u64(1ULL * HZ_PER_GHZ * (sample_cnt + 1), clk_src);
 	if (i2c->dev_comp->ltiming_adjust)
-		clk_ns = 1000000000 / clk_src;
+		clk_ns = HZ_PER_GHZ / clk_src;
 	else
 		clk_ns = sample_ns / 2;
 
Signature · 1 line
2.50.1
#6 lore raw

PATCH 4/7 i2c: nomadik: Use HZ_PER_GHZ constant instead of plain number

In reply to Andy Shevchenko

Use defined constant to avoid the possible mistakes and to provide an additional information on the units.

While at it, drop unneeded 64-bit division, all operands fit 32-bit.

---
 drivers/i2c/busses/i2c-nomadik.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-nomadik.c +4 -3
index 19b648fc094d..b63ee51c1652 100644
@@ -31,6 +31,7 @@
 #include <linux/pm_runtime.h>
 #include <linux/regmap.h>
 #include <linux/slab.h>
+#include <linux/units.h>
 
 #define DRIVER_NAME "nmk-i2c"
 
@@ -419,10 +420,10 @@ static void setup_i2c_controller(struct nmk_i2c_dev *priv)
 	 * modes are 250ns, 100ns, 10ns respectively.
 	 *
 	 * As the time for one cycle T in nanoseconds is
-	 * T = (1/f) * 1000000000 =>
-	 * slsu = cycles / (1000000000 / f) + 1
+	 * T = (1/f) * HZ_PER_GHZ =>
+	 * slsu = cycles / (HZ_PER_GHZ / f) + 1
 	 */
-	ns = DIV_ROUND_UP_ULL(1000000000ULL, i2c_clk);
+	ns = DIV_ROUND_UP(HZ_PER_GHZ, i2c_clk);
 	switch (priv->sm) {
 	case I2C_FREQ_MODE_FAST:
 	case I2C_FREQ_MODE_FAST_PLUS:
Signature · 1 line
2.50.1
maintainer Reviewed-by on v1 4/7
#7 lore raw

Re: [PATCH v1 4/7] i2c: nomadik: Use HZ_PER_GHZ constant instead of plain number

Quoted 6 lines · Andy Shevchenko

Use defined constant to avoid the possible mistakes and to provide an additional information on the units.

While at it, drop unneeded 64-bit division, all operands fit 32-bit.

It makes perfect sense, I don't remember why I put ULL in there anymore :/

Yours,
Linus Walleij

#8 lore raw

PATCH 5/7 i2c: rk3x: Use HZ_PER_GHZ constant instead of plain number

In reply to Andy Shevchenko

Use defined constant to avoid the possible mistakes and to provide an additional information on the units.

---
 drivers/i2c/busses/i2c-rk3x.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/i2c/busses/i2c-rk3x.c +6 -6
index d4e9196445c0..fcede9f6ed54 100644
@@ -19,6 +19,7 @@
 #include <linux/of_irq.h>
 #include <linux/spinlock.h>
 #include <linux/clk.h>
+#include <linux/units.h>
 #include <linux/wait.h>
 #include <linux/mfd/syscon.h>
 #include <linux/regmap.h>
@@ -896,13 +897,12 @@ static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate)
 
 	clk_disable(i2c->pclk);
 
-	t_low_ns = div_u64(((u64)calc.div_low + 1) * 8 * 1000000000, clk_rate);
-	t_high_ns = div_u64(((u64)calc.div_high + 1) * 8 * 1000000000,
-			    clk_rate);
+	t_low_ns = div_u64(8ULL * HZ_PER_GHZ * (calc.div_low + 1), clk_rate);
+	t_high_ns = div_u64(8ULL * HZ_PER_GHZ * (calc.div_high + 1), clk_rate);
 	dev_dbg(i2c->dev,
-		"CLK %lukhz, Req %uns, Act low %lluns high %lluns\n",
-		clk_rate / 1000,
-		1000000000 / t->bus_freq_hz,
+		"CLK %lukHz, Req %luns, Act low %lluns high %lluns\n",
+		clk_rate / HZ_PER_KHZ,
+		HZ_PER_GHZ / t->bus_freq_hz,
 		t_low_ns, t_high_ns);
 }
 
Signature · 1 line
2.50.1
maintainer Reviewed-by on v1 5/7
#9 lore raw

Re: [PATCH v1 5/7] i2c: rk3x: Use HZ_PER_GHZ constant instead of plain number

Am Montag, 12. Januar 2026, 14:46:13 Mitteleuropäische Normalzeit schrieb Andy Shevchenko:

Quoted 4 lines

Use defined constant to avoid the possible mistakes and to provide an additional information on the units.

#10 lore raw

PATCH 6/7 i2c: st: Use HZ_PER_GHZ constant instead of plain number

In reply to Andy Shevchenko

Use defined constant to avoid the possible mistakes and to provide an additional information on the units.

---
 drivers/i2c/busses/i2c-st.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-st.c +2 -1
index 97d70e667227..751ea421caaf 100644
@@ -20,6 +20,7 @@
 #include <linux/of.h>
 #include <linux/pinctrl/consumer.h>
 #include <linux/platform_device.h>
+#include <linux/units.h>
 
 /* SSC registers */
 #define SSC_BRG				0x000
@@ -285,7 +286,7 @@ static void st_i2c_hw_config(struct st_i2c_dev *i2c_dev)
 	writel_relaxed(val, i2c_dev->base + SSC_CTL);
 
 	rate = clk_get_rate(i2c_dev->clk);
-	ns_per_clk = 1000000000 / rate;
+	ns_per_clk = HZ_PER_GHZ / rate;
 
 	/* Baudrate */
 	val = rate / (2 * t->rate);
Signature · 1 line
2.50.1
maintainer Reviewed-by on v1 6/7
#11 lore raw

Re: [PATCH v1 6/7] i2c: st: Use HZ_PER_GHZ constant instead of plain number

Quoted 21 lines · Andy Shevchenko

Use defined constant to avoid the possible mistakes and to provide an additional information on the units.

---
 drivers/i2c/busses/i2c-st.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-st.c +1 -0
index 97d70e667227..751ea421caaf 100644
@@ -20,6 +20,7 @@
 #include <linux/of.h>
 #include <linux/pinctrl/consumer.h>
 #include <linux/platform_device.h>
+#include <linux/units.h>
 
 /* SSC registers */
 #define SSC_BRG				0x000

drivers/i2c/busses/i2c-st.c static void st_i2c_hw_config(struct st_i2c_dev *i2c_dev) · line 292

Quoted 8 lines
drivers/i2c/busses/i2c-st.c +1 -1
@@ -285,7 +286,7 @@ static void st_i2c_hw_config(struct st_i2c_dev *i2c_dev)
 	writel_relaxed(val, i2c_dev->base + SSC_CTL);
 
 	rate = clk_get_rate(i2c_dev->clk);
-	ns_per_clk = 1000000000 / rate;
+	ns_per_clk = HZ_PER_GHZ / rate;
 
 	/* Baudrate */
 	val = rate / (2 * t->rate);

Thanks
Patrice

#12 lore raw

PATCH 7/7 i2c: synquacer: Use HZ_PER_GHZ constant instead of plain number

In reply to Andy Shevchenko

Use defined constant to avoid the possible mistakes and to provide an additional information on the units.

---
 drivers/i2c/busses/i2c-synquacer.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-synquacer.c +2 -1
index 1230f51e1624..4891d68bf0ee 100644
@@ -18,9 +18,10 @@
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
+#include <linux/units.h>
 
 #define WAIT_PCLK(n, rate)	\
-	ndelay(DIV_ROUND_UP(DIV_ROUND_UP(1000000000, rate), n) + 10)
+	ndelay(DIV_ROUND_UP(DIV_ROUND_UP(HZ_PER_GHZ, rate), n) + 10)
 
 /* I2C register address definitions */
 #define SYNQUACER_I2C_REG_BSR		(0x00 << 2) // Bus Status
Signature · 1 line
2.50.1
maintainer Reviewed-by on the v1 series
#13 lore raw
In reply to Andy Shevchenko

Il 12/01/26 14:46, Andy Shevchenko ha scritto:

Quoted 14 lines

A few drivers for I²C host controllers use HZ_PER_GHZ in numeric form. This is less readable and prone to subtle mistakes. Instead, introduce a well defined constant and use it in the drivers. No functional change implied except one case where 64-bit division replaced with 32-bit one.

Andy Shevchenko (7):
units: Add HZ_PER_GHZ
i2c: mlxbf: Use HZ_PER_GHZ constant instead of custom one
i2c: mt65xx: Use HZ_PER_GHZ constant instead of plain number
i2c: nomadik: Use HZ_PER_GHZ constant instead of plain number
i2c: rk3x: Use HZ_PER_GHZ constant instead of plain number
i2c: st: Use HZ_PER_GHZ constant instead of plain number
i2c: synquacer: Use HZ_PER_GHZ constant instead of plain number

Quoted 9 lines
  drivers/i2c/busses/i2c-mlxbf.c     |  5 ++---
  drivers/i2c/busses/i2c-mt65xx.c    | 11 ++++++-----
  drivers/i2c/busses/i2c-nomadik.c   |  7 ++++---
  drivers/i2c/busses/i2c-rk3x.c      | 12 ++++++------
  drivers/i2c/busses/i2c-st.c        |  3 ++-
  drivers/i2c/busses/i2c-synquacer.c |  3 ++-
  include/linux/units.h              |  3 +++
  7 files changed, 25 insertions(+), 19 deletions(-)

Whole series is

Reviewed-by on the v1 series
#14 lore raw
In reply to Andy Shevchenko
Quoted 5 lines · Andy Shevchenko

A few drivers for I²C host controllers use HZ_PER_GHZ in numeric form. This is less readable and prone to subtle mistakes. Instead, introduce a well defined constant and use it in the drivers. No functional change implied except one case where 64-bit division replaced with 32-bit one.

Quoted 9 lines

Andy Shevchenko (7):
units: Add HZ_PER_GHZ
i2c: mlxbf: Use HZ_PER_GHZ constant instead of custom one
i2c: mt65xx: Use HZ_PER_GHZ constant instead of plain number
i2c: nomadik: Use HZ_PER_GHZ constant instead of plain number
i2c: rk3x: Use HZ_PER_GHZ constant instead of plain number
i2c: st: Use HZ_PER_GHZ constant instead of plain number
i2c: synquacer: Use HZ_PER_GHZ constant instead of plain number

Fine with me:

maintainer Applied
#15 lore raw
In reply to Andy Shevchenko

Hi Andy,

Quoted 8 lines

Andy Shevchenko (7):
units: Add HZ_PER_GHZ
i2c: mlxbf: Use HZ_PER_GHZ constant instead of custom one
i2c: mt65xx: Use HZ_PER_GHZ constant instead of plain number
i2c: nomadik: Use HZ_PER_GHZ constant instead of plain number
i2c: rk3x: Use HZ_PER_GHZ constant instead of plain number
i2c: st: Use HZ_PER_GHZ constant instead of plain number
i2c: synquacer: Use HZ_PER_GHZ constant instead of plain number

merged to i2c/i2c-host.

Thanks,
Andi

#16 lore raw
Quoted 10 lines · Andi Shyti
Quoted 8 lines

Andy Shevchenko (7):
units: Add HZ_PER_GHZ
i2c: mlxbf: Use HZ_PER_GHZ constant instead of custom one
i2c: mt65xx: Use HZ_PER_GHZ constant instead of plain number
i2c: nomadik: Use HZ_PER_GHZ constant instead of plain number
i2c: rk3x: Use HZ_PER_GHZ constant instead of plain number
i2c: st: Use HZ_PER_GHZ constant instead of plain number
i2c: synquacer: Use HZ_PER_GHZ constant instead of plain number

merged to i2c/i2c-host.

Thank you!

Signature · 2 lines
With Best Regards,
Andy Shevchenko