Skip to contentSkip to conversation

clk: fractional-divider: Export approximation algorithm to the CCF users

PATCH Applied Andy Shevchenko 4 participants 15 messages Aug 12, 2021 – Sep 8, 2021

Subsystems (7)
lore
Share & tools
lore

Andy Shevchenko

author reviewer
#0 lore raw

PATCH v4 1/4 clk: fractional-divider: Export approximation algorithm to the CCF users

At least one user currently duplicates some functions that are provided by fractional divider module. Let's export approximation algorithm and replace the open-coded variant.

As a bonus the exported function will get better documentation in place.

---
v4: rebased on top of latest CLK codebase

 drivers/clk/clk-fractional-divider.c | 11 +++++++----
 drivers/clk/clk-fractional-divider.h |  9 +++++++++
 drivers/clk/rockchip/clk.c           | 17 +++--------------
 3 files changed, 19 insertions(+), 18 deletions(-)
 create mode 100644 drivers/clk/clk-fractional-divider.h
drivers/clk/clk-fractional-divider.c +7 -4
index b1e556f20911..535d299af646 100644
@@ -14,6 +14,8 @@
 #include <linux/slab.h>
 #include <linux/rational.h>
 
+#include "clk-fractional-divider.h"
+
 static inline u32 clk_fd_readl(struct clk_fractional_divider *fd)
 {
 	if (fd->flags & CLK_FRAC_DIVIDER_BIG_ENDIAN)
@@ -68,9 +70,10 @@ static unsigned long clk_fd_recalc_rate(struct clk_hw *hw,
 	return ret;
 }
 
-static void clk_fd_general_approximation(struct clk_hw *hw, unsigned long rate,
-					 unsigned long *parent_rate,
-					 unsigned long *m, unsigned long *n)
+void clk_fractional_divider_general_approximation(struct clk_hw *hw,
+						  unsigned long rate,
+						  unsigned long *parent_rate,
+						  unsigned long *m, unsigned long *n)
 {
 	struct clk_fractional_divider *fd = to_clk_fd(hw);
 	unsigned long scale;
@@ -102,7 +105,7 @@ static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
 	if (fd->approximation)
 		fd->approximation(hw, rate, parent_rate, &m, &n);
 	else
-		clk_fd_general_approximation(hw, rate, parent_rate, &m, &n);
+		clk_fractional_divider_general_approximation(hw, rate, parent_rate, &m, &n);
 
 	ret = (u64)*parent_rate * m;
 	do_div(ret, n);
drivers/clk/clk-fractional-divider.h new file +9 -0
new file mode 100644
index 000000000000..4fa359a12ef4
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+struct clk_hw;
+
+void clk_fractional_divider_general_approximation(struct clk_hw *hw,
+						  unsigned long rate,
+						  unsigned long *parent_rate,
+						  unsigned long *m,
+						  unsigned long *n);
drivers/clk/rockchip/clk.c +3 -14
index 049e5e0b64f6..b7be7e11b0df 100644
@@ -22,6 +22,8 @@
 #include <linux/regmap.h>
 #include <linux/reboot.h>
 #include <linux/rational.h>
+
+#include "../clk-fractional-divider.h"
 #include "clk.h"
 
 /*
@@ -178,10 +180,8 @@ static void rockchip_fractional_approximation(struct clk_hw *hw,
 		unsigned long rate, unsigned long *parent_rate,
 		unsigned long *m, unsigned long *n)
 {
-	struct clk_fractional_divider *fd = to_clk_fd(hw);
 	unsigned long p_rate, p_parent_rate;
 	struct clk_hw *p_parent;
-	unsigned long scale;
 
 	p_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
 	if ((rate * 20 > p_rate) && (p_rate % rate != 0)) {
@@ -190,18 +190,7 @@ static void rockchip_fractional_approximation(struct clk_hw *hw,
 		*parent_rate = p_parent_rate;
 	}
 
-	/*
-	 * Get rate closer to *parent_rate to guarantee there is no overflow
-	 * for m and n. In the result it will be the nearest rate left shifted
-	 * by (scale - fd->nwidth) bits.
-	 */
-	scale = fls_long(*parent_rate / rate - 1);
-	if (scale > fd->nwidth)
-		rate <<= scale - fd->nwidth;
-
-	rational_best_approximation(rate, *parent_rate,
-			GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
-			m, n);
+	clk_fractional_divider_general_approximation(hw, rate, parent_rate, m, n);
 }
 
 static struct clk *rockchip_clk_register_frac_branch(
Signature · 1 line
2.30.2
author reviewer
#1 lore raw

PATCH v4 2/4 clk: fractional-divider: Hide clk_fractional_divider_ops from wide audience

The providers are all located in drivers/clk/ and hence no need to export the clock operations to wider audience. Hide them by moving to drivers/clk/clk-fractional-divider.h.

---
v4: rebased on top of latest CLK codebase, fixed compilation error

 drivers/clk/clk-fractional-divider.h | 2 ++
 drivers/clk/imx/clk-composite-7ulp.c | 1 +
 include/linux/clk-provider.h         | 1 -
 3 files changed, 3 insertions(+), 1 deletion(-)
drivers/clk/clk-fractional-divider.h +2 -0
index 4fa359a12ef4..aaaef90b2777 100644
@@ -2,6 +2,8 @@
 
 struct clk_hw;
 
+extern const struct clk_ops clk_fractional_divider_ops;
+
 void clk_fractional_divider_general_approximation(struct clk_hw *hw,
 						  unsigned long rate,
 						  unsigned long *parent_rate,
drivers/clk/imx/clk-composite-7ulp.c +1 -0
index 7c4f31b31eb0..d85ba78abbb1 100644
@@ -10,6 +10,7 @@
 #include <linux/err.h>
 #include <linux/slab.h>
 
+#include "../clk-fractional-divider.h"
 #include "clk.h"
 
 #define PCG_PCS_SHIFT	24
include/linux/clk-provider.h +0 -1
index 7be81d5fcf8c..d8609aef2394 100644
@@ -1023,7 +1023,6 @@ struct clk_fractional_divider {
 #define CLK_FRAC_DIVIDER_ZERO_BASED	BIT(0)
 #define CLK_FRAC_DIVIDER_BIG_ENDIAN	BIT(1)
 
-extern const struct clk_ops clk_fractional_divider_ops;
 struct clk *clk_register_fractional_divider(struct device *dev,
 		const char *name, const char *parent_name, unsigned long flags,
 		void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
Signature · 1 line
2.30.2
maintainer Applied
#2 lore raw

Re: [PATCH v4 2/4] clk: fractional-divider: Hide clk_fractional_divider_ops from wide audience

Quoting Andy Shevchenko (2021-08-12 10:00:23)

Quoted 6 lines

The providers are all located in drivers/clk/ and hence no need to export the clock operations to wider audience. Hide them by moving to drivers/clk/clk-fractional-divider.h.

---

Applied to clk-next

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

author reviewer
#3 lore raw

PATCH v4 3/4 clk: fractional-divider: Introduce POWER_OF_TWO_PS flag

In reply to Andy Shevchenko

The newly introduced POWER_OF_TWO_PS flag, when set, makes the flow to skip the assumption that the caller will use an additional 2^scale prescaler to get the desired clock rate.

---
v4: rebased on top of latest CLK codebase

 drivers/acpi/acpi_lpss.c             |  4 ++--
 drivers/clk/clk-fractional-divider.c | 10 ++++++----
 drivers/mfd/intel-lpss.c             |  3 ++-
 include/linux/clk-provider.h         | 11 +++++++++--
 4 files changed, 19 insertions(+), 9 deletions(-)
drivers/acpi/acpi_lpss.c +2 -2
index 7f163074e4e4..30b1f511c2af 100644
@@ -436,8 +436,8 @@ static int register_device_clock(struct acpi_device *adev,
 		if (!clk_name)
 			return -ENOMEM;
 		clk = clk_register_fractional_divider(NULL, clk_name, parent,
-						      0, prv_base,
-						      1, 15, 16, 15, 0, NULL);
+						      CLK_FRAC_DIVIDER_POWER_OF_TWO_PS,
+						      prv_base, 1, 15, 16, 15, 0, NULL);
 		parent = clk_name;
 
 		clk_name = kasprintf(GFP_KERNEL, "%s-update", devname);
drivers/clk/clk-fractional-divider.c +6 -4
index 535d299af646..6a3ed82fdae9 100644
@@ -76,16 +76,18 @@ void clk_fractional_divider_general_approximation(struct clk_hw *hw,
 						  unsigned long *m, unsigned long *n)
 {
 	struct clk_fractional_divider *fd = to_clk_fd(hw);
-	unsigned long scale;
 
 	/*
 	 * Get rate closer to *parent_rate to guarantee there is no overflow
 	 * for m and n. In the result it will be the nearest rate left shifted
 	 * by (scale - fd->nwidth) bits.
 	 */
-	scale = fls_long(*parent_rate / rate - 1);
-	if (scale > fd->nwidth)
-		rate <<= scale - fd->nwidth;
+	if (fd->flags & CLK_FRAC_DIVIDER_POWER_OF_TWO_PS) {
+		unsigned long scale = fls_long(*parent_rate / rate - 1);
+
+		if (scale > fd->nwidth)
+			rate <<= scale - fd->nwidth;
+	}
 
 	rational_best_approximation(rate, *parent_rate,
 			GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
drivers/mfd/intel-lpss.c +2 -1
index a9bf10bee796..0e15afc39f54 100644
@@ -301,7 +301,8 @@ static int intel_lpss_register_clock_divider(struct intel_lpss *lpss,
 
 	snprintf(name, sizeof(name), "%s-div", devname);
 	tmp = clk_register_fractional_divider(NULL, name, __clk_get_name(tmp),
-					      0, lpss->priv, 1, 15, 16, 15, 0,
+					      CLK_FRAC_DIVIDER_POWER_OF_TWO_PS,
+					      lpss->priv, 1, 15, 16, 15, 0,
 					      NULL);
 	if (IS_ERR(tmp))
 		return PTR_ERR(tmp);
include/linux/clk-provider.h +9 -2
index d8609aef2394..f59c875271a0 100644
@@ -1001,6 +1001,12 @@ struct clk_hw *devm_clk_hw_register_fixed_factor(struct device *dev,
  * CLK_FRAC_DIVIDER_BIG_ENDIAN - By default little endian register accesses are
  *	used for the divider register.  Setting this flag makes the register
  *	accesses big endian.
+ * CLK_FRAC_DIVIDER_POWER_OF_TWO_PS - By default the resulting fraction might
+ *	be saturated and the caller will get quite far from the good enough
+ *	approximation. Instead the caller may require, by setting this flag,
+ *	to shift left by a few bits in case, when the asked one is quite small
+ *	to satisfy the desired range of denominator. It assumes that on the
+ *	caller's side the power-of-two capable prescaler exists.
  */
 struct clk_fractional_divider {
 	struct clk_hw	hw;
@@ -1020,8 +1026,9 @@ struct clk_fractional_divider {
 
 #define to_clk_fd(_hw) container_of(_hw, struct clk_fractional_divider, hw)
 
-#define CLK_FRAC_DIVIDER_ZERO_BASED	BIT(0)
-#define CLK_FRAC_DIVIDER_BIG_ENDIAN	BIT(1)
+#define CLK_FRAC_DIVIDER_ZERO_BASED		BIT(0)
+#define CLK_FRAC_DIVIDER_BIG_ENDIAN		BIT(1)
+#define CLK_FRAC_DIVIDER_POWER_OF_TWO_PS	BIT(2)
 
 struct clk *clk_register_fractional_divider(struct device *dev,
 		const char *name, const char *parent_name, unsigned long flags,
Signature · 1 line
2.30.2
maintainer Applied
#4 lore raw

Re: [PATCH v4 3/4] clk: fractional-divider: Introduce POWER_OF_TWO_PS flag

Quoting Andy Shevchenko (2021-08-12 10:00:24)

Quoted 7 lines

The newly introduced POWER_OF_TWO_PS flag, when set, makes the flow to skip the assumption that the caller will use an additional 2^scale prescaler to get the desired clock rate.

---

Applied to clk-next

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

author reviewer
#5 lore raw

PATCH v4 4/4 clk: fractional-divider: Document the arithmetics used behind the code

In reply to Andy Shevchenko

It appears that some code lines raise the question why they are needed and how they are participated in the calculus of the resulting values.

Document this in a form of the top comment in the module file.

---
v4: rebased on top of latest CLK codebase

 drivers/clk/clk-fractional-divider.c | 35 +++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
drivers/clk/clk-fractional-divider.c +34 -1
index 6a3ed82fdae9..b4f365d97bd0 100644
@@ -3,8 +3,39 @@
  * Copyright (C) 2014 Intel Corporation
  *
  * Adjustable fractional divider clock implementation.
- * Output rate = (m / n) * parent_rate.
  * Uses rational best approximation algorithm.
+ *
+ * Output is calculated as
+ *
+ *	rate = (m / n) * parent_rate				(1)
+ *
+ * This is useful when on die we have a prescaler block which asks for
+ * m (numerator) and n (denominator) values to be provided to satisfy
+ * the (1) as much as possible.
+ *
+ * Since m and n have the limitation by a range, e.g.
+ *
+ *	n >= 1, n < N_width, where N_width = 2^nwidth		(2)
+ *
+ * for some cases the output may be saturated. Hence, from (1) and (2),
+ * assuming the worst case when m = 1, the inequality
+ *
+ *	floor(log2(parent_rate / rate)) <= nwidth		(3)
+ *
+ * may be derived. Thus, in cases when
+ *
+ *	(parent_rate / rate) >> N_width				(4)
+ *
+ * we might scale up the rate by 2^scale (see the description of
+ * CLK_FRAC_DIVIDER_POWER_OF_TWO_PS for additional information), where
+ *
+ *	scale = floor(log2(parent_rate / rate)) - nwidth	(5)
+ *
+ * and assume that the IP, that needs m and n, has also its own
+ * prescaler, which is capable to divide by 2^scale. In this way
+ * we get the denominator to satisfy the desired range (2) and
+ * at the same time much much better result of m and n than simple
+ * saturated values.
  */
 
 #include <linux/clk-provider.h>
@@ -81,6 +112,8 @@ void clk_fractional_divider_general_approximation(struct clk_hw *hw,
 	 * Get rate closer to *parent_rate to guarantee there is no overflow
 	 * for m and n. In the result it will be the nearest rate left shifted
 	 * by (scale - fd->nwidth) bits.
+	 *
+	 * For the detailed explanation see the top comment in this file.
 	 */
 	if (fd->flags & CLK_FRAC_DIVIDER_POWER_OF_TWO_PS) {
 		unsigned long scale = fls_long(*parent_rate / rate - 1);
Signature · 1 line
2.30.2
maintainer Applied
#6 lore raw

Re: [PATCH v4 4/4] clk: fractional-divider: Document the arithmetics used behind the code

Quoting Andy Shevchenko (2021-08-12 10:00:25)

Quoted 8 lines

It appears that some code lines raise the question why they are needed and how they are participated in the calculus of the resulting values.

Document this in a form of the top comment in the module file.

---

Applied to clk-next

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

maintainer Applied
#7 lore raw
In reply to Andy Shevchenko

Quoting Andy Shevchenko (2021-08-12 10:00:22)

Quoted 10 lines

At least one user currently duplicates some functions that are provided by fractional divider module. Let's export approximation algorithm and replace the open-coded variant.

As a bonus the exported function will get better documentation in place.

---

Applied to clk-next

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

author reviewer
#8 lore raw
Quoted 7 lines · Stephen Boyd

Quoting Andy Shevchenko (2021-08-12 10:00:22)

Quoted 5 lines

At least one user currently duplicates some functions that are provided by fractional divider module. Let's export approximation algorithm and replace the open-coded variant.

As a bonus the exported function will get better documentation in place.

Applied to clk-next

Thank you, Stephen!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author reviewer
#9 lore raw
Quoted 10 lines · Andy Shevchenko
Quoted 7 lines · Stephen Boyd

Quoting Andy Shevchenko (2021-08-12 10:00:22)

Quoted 5 lines

At least one user currently duplicates some functions that are provided by fractional divider module. Let's export approximation algorithm and replace the open-coded variant.

As a bonus the exported function will get better documentation in place.

Applied to clk-next

Thank you, Stephen!

When they are expected to be visible in Linux Next?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw
In reply to Andy Shevchenko

From: Chris Morgan <[email protected]>

Unfortunately, I can confirm this breaks the DSI panel on the Rockchip PX30 (and possibly other SoCs). Tested on my Odroid Go Advance. When I revert 4e7cf74fa3b2 "clk: fractional-divider: Export approximation algorithm to the CCF users" and 928f9e268611 "clk: fractional-divider:
Hide clk_fractional_divider_ops from wide audience" the panel begins working again as expected on the master branch.

It looks like an assumption is made in the vop_crtc_mode_fixup() function in the rockchip_drm_vop.c that gets broken with this change. Specifically, the function says in the comments "When DRM gives us a mode, we should add 999 Hz to it.". I believe this is no longer true after this clk change, and when I remove the + 999 from the function the DSI panel works again. Note that I do not know the implications of removing this 999 aside from that it fixes the DSI panel on my PX30 after this change, so I don't know if it's a positive change or not.

Thank you.

author reviewer
#11 lore raw
Quoted 19 lines · Chris Morgan

From: Chris Morgan <[email protected]>

Unfortunately, I can confirm this breaks the DSI panel on the Rockchip PX30 (and possibly other SoCs). Tested on my Odroid Go Advance. When I revert 4e7cf74fa3b2 "clk: fractional-divider: Export approximation algorithm to the CCF users" and 928f9e268611 "clk: fractional-divider:
Hide clk_fractional_divider_ops from wide audience" the panel begins working again as expected on the master branch.

It looks like an assumption is made in the vop_crtc_mode_fixup() function in the rockchip_drm_vop.c that gets broken with this change. Specifically, the function says in the comments "When DRM gives us a mode, we should add 999 Hz to it.". I believe this is no longer true after this clk change, and when I remove the + 999 from the function the DSI panel works again. Note that I do not know the implications of removing this 999 aside from that it fixes the DSI panel on my PX30 after this change, so I don't know if it's a positive change or not.

Quoted 1 line

Thank you.

Thank you for the report!

I'll check this. Perhaps Heiko can help with testing as well on his side.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author reviewer
#12 lore raw
Quoted 20 lines · Andy Shevchenko
Quoted 19 lines · Chris Morgan

From: Chris Morgan <[email protected]>

Unfortunately, I can confirm this breaks the DSI panel on the Rockchip PX30 (and possibly other SoCs). Tested on my Odroid Go Advance. When I revert 4e7cf74fa3b2 "clk: fractional-divider: Export approximation algorithm to the CCF users" and 928f9e268611 "clk: fractional-divider:
Hide clk_fractional_divider_ops from wide audience" the panel begins working again as expected on the master branch.

It looks like an assumption is made in the vop_crtc_mode_fixup() function in the rockchip_drm_vop.c that gets broken with this change. Specifically, the function says in the comments "When DRM gives us a mode, we should add 999 Hz to it.". I believe this is no longer true after this clk change, and when I remove the + 999 from the function the DSI panel works again. Note that I do not know the implications of removing this 999 aside from that it fixes the DSI panel on my PX30 after this change, so I don't know if it's a positive change or not.

Quoted 5 lines
Quoted 1 line

Thank you.

Thank you for the report!

I'll check this. Perhaps Heiko can help with testing as well on his side.

On the first glance the mentioned patch may not be the culprit because it does not change the functional behaviour (if I'm not mistaken). What really changes it is the additional flag that removes the left-shift of the rate in the calculations.

To me sounds like you found a proper solution to the issue and that +999 is a hack against the (blindly?) copied part of the algorithm used in fractional divider. Have you read the top comment in clk-fractional-divider.c? It should explain how it works after my series.

In any case I'm not going to come to any conclusions right now and also want to hear from people who have better understanding of this hardware.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#13 lore raw
Quoted 23 lines · Andy Shevchenko
Quoted 22 lines · Andy Shevchenko
Quoted 20 lines · Chris Morgan

From: Chris Morgan <[email protected]>

Unfortunately, I can confirm this breaks the DSI panel on the Rockchip PX30 (and possibly other SoCs). Tested on my Odroid Go Advance. When I revert 4e7cf74fa3b2 "clk: fractional-divider: Export approximation algorithm to the CCF users" and 928f9e268611 "clk: fractional-divider:
Hide clk_fractional_divider_ops from wide audience" the panel begins working again as expected on the master branch.

It looks like an assumption is made in the vop_crtc_mode_fixup() function in the rockchip_drm_vop.c that gets broken with this change. Specifically, the function says in the comments "When DRM gives us a mode, we should add 999 Hz to it.". I believe this is no longer true after this clk change, and when I remove the + 999 from the function the DSI panel works again. Note that I do not know the implications of removing this 999 aside from that it fixes the DSI panel on my PX30 after this change, so I don't know if it's a positive change or not.

Thank you.

Quoted 8 lines
Quoted 3 lines

Thank you for the report!

I'll check this. Perhaps Heiko can help with testing as well on his side.

On the first glance the mentioned patch may not be the culprit because it does not change the functional behaviour (if I'm not mistaken). What really changes it is the additional flag that removes the left-shift of the rate in the calculations.

I noticed the behavior on the 5.14 kernel was to set the numerator at an ungodly 7649082492112076800 and the denominator at 1 (no, that's not a typo). I think it tried to write 65535 to the register though, but it would go through this a few times and eventually settle on 1:1 as the fractional ratio (which I assume is all good, because that would work).

Contrast this to the 5.15 behavior where it would try to set the ratio to 17001:17000, which would cause the DSI screen to fail to initalize.

After tracing through the code I figured out that the VOP was trying to add 999 to the clock and set it at 17000999. 17000000/17000999 gives us 0, and subtracting 1 from that gives us a -1. The fls_long function would then return 64, and if we subtract 16 (the value of fd->mwidth for my board) it would tell us to shift the 17000999 48 bits to the left, which matches the ungodly large number.

With the changes in 5.15 if I remove the + 999 from the VOP driver the clock then gets set at 17000000, since the parent is at 17000000 that gives us a 1:1 where everything works and everything is fine.

Long story short I think this is a bug that's existed all along, and this change simply exposed it in a manner where it stopped working despite the bug being present. Unfortunately I neither know enough about the hardware to be confident in this fix beyond my specific board, nor do I have enough hardware to test it on anything except a Rockchip rk3326 with a DSI panel.

Quoted 5 lines

To me sounds like you found a proper solution to the issue and that +999 is a hack against the (blindly?) copied part of the algorithm used in fractional divider. Have you read the top comment in clk-fractional-divider.c? It should explain how it works after my series.

No, but I probably should read the docs more. I just stumbled on this series doing a bisect when the DSI panel stopped working.

Quoted 3 lines

In any case I'm not going to come to any conclusions right now and also want to hear from people who have better understanding of this hardware.

Yeah, I want to see what Heiko says after some more research, or anyone who has more familiarity with clocks/DRM than I do or who has more hardware to test on than I do.

I intended to send a message informing you that "hey, this breaks upstream", but I think it turns out it's more a matter of "hey, this makes a broken upstream break instead of limp along".

Thank you.

Quoted 6 lines
Signature · 3 lines
-- 
With Best Regards,
Andy Shevchenko

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

author reviewer
#14 lore raw
Quoted 50 lines · Chris Morgan
Quoted 20 lines · Andy Shevchenko
Quoted 19 lines · Andy Shevchenko
Quoted 16 lines · Chris Morgan

Unfortunately, I can confirm this breaks the DSI panel on the Rockchip PX30 (and possibly other SoCs). Tested on my Odroid Go Advance. When I revert 4e7cf74fa3b2 "clk: fractional-divider: Export approximation algorithm to the CCF users" and 928f9e268611 "clk: fractional-divider:
Hide clk_fractional_divider_ops from wide audience" the panel begins working again as expected on the master branch.

It looks like an assumption is made in the vop_crtc_mode_fixup() function in the rockchip_drm_vop.c that gets broken with this change. Specifically, the function says in the comments "When DRM gives us a mode, we should add 999 Hz to it.". I believe this is no longer true after this clk change, and when I remove the + 999 from the function the DSI panel works again. Note that I do not know the implications of removing this 999 aside from that it fixes the DSI panel on my PX30 after this change, so I don't know if it's a positive change or not.

Quoted 8 lines
Quoted 3 lines

Thank you for the report!

I'll check this. Perhaps Heiko can help with testing as well on his side.

On the first glance the mentioned patch may not be the culprit because it does not change the functional behaviour (if I'm not mistaken). What really changes it is the additional flag that removes the left-shift of the rate in the calculations.

I noticed the behavior on the 5.14 kernel was to set the numerator at an ungodly 7649082492112076800 and the denominator at 1 (no, that's not a typo). I think it tried to write 65535 to the register though, but it would go through this a few times and eventually settle on 1:1 as the fractional ratio (which I assume is all good, because that would work).

Contrast this to the 5.15 behavior where it would try to set the ratio to 17001:17000, which would cause the DSI screen to fail to initalize.

After tracing through the code I figured out that the VOP was trying to add 999 to the clock and set it at 17000999. 17000000/17000999 gives us 0, and subtracting 1 from that gives us a -1. The fls_long function would then return 64, and if we subtract 16 (the value of fd->mwidth for my board) it would tell us to shift the 17000999 48 bits to the left, which matches the ungodly large number.

With the changes in 5.15 if I remove the + 999 from the VOP driver the clock then gets set at 17000000, since the parent is at 17000000 that gives us a 1:1 where everything works and everything is fine.

Quoted 6 lines

Long story short I think this is a bug that's existed all along, and this change simply exposed it in a manner where it stopped working despite the bug being present. Unfortunately I neither know enough about the hardware to be confident in this fix beyond my specific board, nor do I have enough hardware to test it on anything except a Rockchip rk3326 with a DSI panel.

This is a very good analysis!

Quoted 8 lines
Quoted 4 lines

To me sounds like you found a proper solution to the issue and that +999 is a hack against the (blindly?) copied part of the algorithm used in fractional divider. Have you read the top comment in clk-fractional-divider.c? It should explain how it works after my series.

No, but I probably should read the docs more. I just stumbled on this series doing a bisect when the DSI panel stopped working.

Quoted 6 lines
Quoted 2 lines

In any case I'm not going to come to any conclusions right now and also want to hear from people who have better understanding of this hardware.

Yeah, I want to see what Heiko says after some more research, or anyone who has more familiarity with clocks/DRM than I do or who has more hardware to test on than I do.

After what I read above I can't add anything and what I think is the best course of actions is to submit a patch with removal of +999 part and above explanation. It would be nice to find the real commit ID that may be used for a Fixes tag.

Then we at least will have a patch ready in case it's considered correct by people from Rockchip side.

Quoted 3 lines

I intended to send a message informing you that "hey, this breaks upstream", but I think it turns out it's more a matter of "hey, this makes a broken upstream break instead of limp along".

Understand.

Signature · 2 lines
With Best Regards,
Andy Shevchenko