Skip to contentSkip to conversation

i2c: rk3x: fix divisor calculation for SCL frequency

PATCH Superseded Addy Ke 3 participants 7 messages Sep 5, 2014 – Sep 20, 2014

Subsystems (2)
lore
Share & tools
lore

Addy Ke

author
#0 lore raw

PATCH 1/1 i2c: rk3x: fix divisor calculation for SCL frequency

I2C_CLKDIV register descripted in the previous version of RK3x chip manual is incorrect. Plus 1 is required.

The correct formula:
- T(SCL_HIGH) = T(PCLK) * (CLKDIVH + 1) * 8
- T(SCL_LOW) = T(PCLK) * (CLKDIVL + 1) * 8
- (SCL Divsor) = 8 * ((CLKDIVL + 1) + (CLKDIVH + 1))
- SCL = PCLK / (CLK Divsor)

It will be updated to the latest version of chip manual.

---
 drivers/i2c/busses/i2c-rk3x.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-rk3x.c +2 -2
index e637c32..76b6604 100644
@@ -433,8 +433,8 @@ static void rk3x_i2c_set_scl_rate(struct rk3x_i2c *i2c, unsigned long scl_rate)
 	unsigned long i2c_rate = clk_get_rate(i2c->clk);
 	unsigned int div;
 
-	/* SCL rate = (clk rate) / (8 * DIV) */
-	div = DIV_ROUND_UP(i2c_rate, scl_rate * 8);
+	/* SCL rate = (clk rate) / (8 * (DIV + 2)) */
+	div = DIV_ROUND_UP(i2c_rate, scl_rate * 8) - 2;
 
 	/* The lower and upper half of the CLKDIV reg describe the length of
 	 * SCL low & high periods. */
Signature · 1 line
1.8.3.2
#1 lore raw

Addy,

Quoted 8 lines · Addy Ke

I2C_CLKDIV register descripted in the previous version of RK3x chip manual is incorrect. Plus 1 is required.

The correct formula:
- T(SCL_HIGH) = T(PCLK) * (CLKDIVH + 1) * 8
- T(SCL_LOW) = T(PCLK) * (CLKDIVL + 1) * 8
- (SCL Divsor) = 8 * ((CLKDIVL + 1) + (CLKDIVH + 1))
- SCL = PCLK / (CLK Divsor)

I'll trust that you tested this with a scope

Quoted 12 lines

It will be updated to the latest version of chip manual.

---
 drivers/i2c/busses/i2c-rk3x.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-rk3x.c +0 -0
index e637c32..76b6604 100644

drivers/i2c/busses/i2c-rk3x.c static void rk3x_i2c_set_scl_rate(struct rk3x_i2c *i2c, unsigned long scl_rate) · line 437

Quoted 7 lines
drivers/i2c/busses/i2c-rk3x.c +2 -2
@@ -433,8 +433,8 @@ static void rk3x_i2c_set_scl_rate(struct rk3x_i2c *i2c, unsigned long scl_rate)
        unsigned long i2c_rate = clk_get_rate(i2c->clk);
        unsigned int div;
 
-       /* SCL rate = (clk rate) / (8 * DIV) */
-       div = DIV_ROUND_UP(i2c_rate, scl_rate * 8);
+       /* SCL rate = (clk rate) / (8 * (DIV + 2)) */
+       div = DIV_ROUND_UP(i2c_rate, scl_rate * 8) - 2;

Given the bug I just fixed in the Rockchip SPI driver, I was a little worried about div becoming -1 (and thus being a really large positive number since div is unsigned).

However, it seems that you get saved by the next statement:
div = DIV_ROUND_UP(div, 2);

In the testing I did with the Linux macros, that magically transformed a div of 0xFFFFFFFF (-1) to 0, so it's not technically a bug. ...but it's very non-obvious. Can you do something a little cleaner?

-Doug

author
#2 lore raw
Quoted 6 lines

Addy,

Quoted 3 lines · Addy Ke

I2C_CLKDIV register descripted in the previous version of RK3x chip manual is incorrect. Plus 1 is required.

Quoted 8 lines
Quoted 5 lines

The correct formula:
- T(SCL_HIGH) = T(PCLK) * (CLKDIVH + 1) * 8
- T(SCL_LOW) = T(PCLK) * (CLKDIVL + 1) * 8
- (SCL Divsor) = 8 * ((CLKDIVL + 1) + (CLKDIVH + 1))
- SCL = PCLK / (CLK Divsor)

I'll trust that you tested this with a scope

Yes ,I have tested on RK3188 and RK3288, and confirmed by oscilloscope.

Quoted 25 lines
Quoted 13 lines

It will be updated to the latest version of chip manual.

---
 drivers/i2c/busses/i2c-rk3x.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-rk3x.c +0 -0
index e637c32..76b6604 100644

drivers/i2c/busses/i2c-rk3x.c static void rk3x_i2c_set_scl_rate(struct rk3x_i2c *i2c, unsigned long scl_rate) · line 437

Quoted 7 lines
drivers/i2c/busses/i2c-rk3x.c +2 -2
@@ -433,8 +433,8 @@ static void rk3x_i2c_set_scl_rate(struct rk3x_i2c *i2c, unsigned long scl_rate)
        unsigned long i2c_rate = clk_get_rate(i2c->clk);
        unsigned int div;
 
-       /* SCL rate = (clk rate) / (8 * DIV) */
-       div = DIV_ROUND_UP(i2c_rate, scl_rate * 8);
+       /* SCL rate = (clk rate) / (8 * (DIV + 2)) */
+       div = DIV_ROUND_UP(i2c_rate, scl_rate * 8) - 2;

Given the bug I just fixed in the Rockchip SPI driver, I was a little worried about div becoming -1 (and thus being a really large positive number since div is unsigned).

Quoted 6 lines

However, it seems that you get saved by the next statement:
div = DIV_ROUND_UP(div, 2);

In the testing I did with the Linux macros, that magically transformed a div of 0xFFFFFFFF (-1) to 0, so it's not technically a bug. ...but it's very non-obvious. Can you do something a little cleaner?

The following modifications is reasonable?

static void rk3x_i2c_set_scl_rate(struct rk3x_i2c *i2c, unsigned long scl_rate)

{
        unsigned long i2c_rate = clk_get_rate(i2c->clk);
        unsigned int div;

        /* set DIV = DIVH = DIVL
         * SCL rate = (clk rate) / (8 * (DIVH + 1 + DIVL + 1))
         *          = (clk rate) / (16 * (DIV + 1))
         */
        div = DIV_ROUND_UP(i2c_rate, scl_rate * 16) - 1;

        i2c_writel(i2c, (div << 16) | (div & 0xffff), REG_CLKDIV);
}
Quoted 5 lines

-Doug

#3 lore raw

Addy,

Quoted 15 lines · addy ke

The following modifications is reasonable?

static void rk3x_i2c_set_scl_rate(struct rk3x_i2c *i2c, unsigned long scl_rate)

{
        unsigned long i2c_rate = clk_get_rate(i2c->clk);
        unsigned int div;

        /* set DIV = DIVH = DIVL
         * SCL rate = (clk rate) / (8 * (DIVH + 1 + DIVL + 1))
         *          = (clk rate) / (16 * (DIV + 1))
         */
        div = DIV_ROUND_UP(i2c_rate, scl_rate * 16) - 1;

        i2c_writel(i2c, (div << 16) | (div & 0xffff), REG_CLKDIV);
}

Yes, that looks much cleaner and is a nice solution, thanks! Can you send up a new patch version?

-Doug

author
#4 lore raw

PATCH v2 1/1 i2c: rk3x: fix divisor calculation for SCL frequency

In reply to Addy Ke

I2C_CLKDIV register descripted in the previous version of RK3x chip manual is incorrect. Plus 1 is required.

The correct formula:
- T(SCL_HIGH) = T(PCLK) * (CLKDIVH + 1) * 8
- T(SCL_LOW) = T(PCLK) * (CLKDIVL + 1) * 8
- (SCL Divsor) = 8 * ((CLKDIVL + 1) + (CLKDIVH + 1))
- SCL = PCLK / (CLK Divsor)

It will be updated to the latest version of chip manual.

---
changes since v1:
- make it more cleaner, suggested by Doug Anderson

 drivers/i2c/busses/i2c-rk3x.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/i2c/busses/i2c-rk3x.c +5 -6
index e637c32..93cfc83 100644
@@ -433,12 +433,11 @@ static void rk3x_i2c_set_scl_rate(struct rk3x_i2c *i2c, unsigned long scl_rate)
 	unsigned long i2c_rate = clk_get_rate(i2c->clk);
 	unsigned int div;
 
-	/* SCL rate = (clk rate) / (8 * DIV) */
-	div = DIV_ROUND_UP(i2c_rate, scl_rate * 8);
-
-	/* The lower and upper half of the CLKDIV reg describe the length of
-	 * SCL low & high periods. */
-	div = DIV_ROUND_UP(div, 2);
+	/* set DIV = DIVH = DIVL
+	 * SCL rate = (clk rate) / (8 * (DIVH + 1 + DIVL + 1))
+	 *          = (clk rate) / (16 * (DIV + 1))
+	 */
+	div = DIV_ROUND_UP(i2c_rate, scl_rate * 16) - 1;
 
 	i2c_writel(i2c, (div << 16) | (div & 0xffff), REG_CLKDIV);
 }
Signature · 1 line
1.8.3.2
Reviewed-by on v2 1/1
#5 lore raw

Addy,

Quoted 11 lines · Addy Ke

I2C_CLKDIV register descripted in the previous version of RK3x chip manual is incorrect. Plus 1 is required.

The correct formula:
- T(SCL_HIGH) = T(PCLK) * (CLKDIVH + 1) * 8
- T(SCL_LOW) = T(PCLK) * (CLKDIVL + 1) * 8
- (SCL Divsor) = 8 * ((CLKDIVL + 1) + (CLKDIVH + 1))
- SCL = PCLK / (CLK Divsor)

It will be updated to the latest version of chip manual.

Quoted 7 lines

---
changes since v1:
- make it more cleaner, suggested by Doug Anderson

 drivers/i2c/busses/i2c-rk3x.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
Applied
#6 lore raw
In reply to Addy Ke
Quoted 9 lines · Addy Ke

I2C_CLKDIV register descripted in the previous version of RK3x chip manual is incorrect. Plus 1 is required.

The correct formula:
- T(SCL_HIGH) = T(PCLK) * (CLKDIVH + 1) * 8
- T(SCL_LOW) = T(PCLK) * (CLKDIVL + 1) * 8
- (SCL Divsor) = 8 * ((CLKDIVL + 1) + (CLKDIVH + 1))
- SCL = PCLK / (CLK Divsor)

Quoted 3 lines

It will be updated to the latest version of chip manual.

Applied to for-next, thanks!

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20140920/133ac9df/attachment.sig>