Skip to contentSkip to conversation

drm/msm/dsi: phy_28nm: return exact PLL-achievable VCO rate in determine_rate

PATCH Superseded Vladimir Bely 2 participants 2 messages Nov 29

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/msm/dsi: phy_28nm: return exact PLL-achievable VCO rate in determine_rate

Commit 6cd33b6f4155 ("drm/msm/dsi: round 6G byte clock rate to the PLL-achievable value") relies on clk_round_rate(byte_clk) returning the rate the DSI PHY PLL actually achieves so later link_clk_set_rate() calls from msm_dsi_host_xfer_prepare() become no-ops in the clock framework and do not re-lock the live PLL during video scanout.

However, dsi_pll_28nm_clk_determine_rate() only clamps req->rate to [min_pll_rate, max_pll_rate] without quantizing it to the PLL's SDM fractional-N grid, while dsi_pll_28nm_clk_set_rate() truncates the multiplier via a 1/1000 intermediate step. For example, on MSM8916 with the Samsung AMS452EF01 panel (requested byte clock 57741000 Hz, VCO 461928000 Hz), clk_round_rate() still returns 57741000 Hz whereas recalc_rate() reads back 57739160 Hz (VCO 461913281 Hz). Every runtime DCS command (such as backlight updates) therefore still sees a rate mismatch and re-programs the live 28nm PLL, producing visible scanout corruption.

Compute the 16-bit fractional-N divider and resulting VCO rate in a shared helper dsi_pll_28nm_calc_vco_rate() used by both determine_rate() and set_rate(), matching the exact mult_frac() formula in recalc_rate(). Also set CLK_DIVIDER_ROUND_CLOSEST on analog_postdiv and CLK_MUX_ROUND_CLOSEST on byte_mux so rounding the returned byte clock a second time stays on the same divider/mux path instead of rejecting a 1 Hz higher VCO rate.

Tested on Samsung Galaxy A3 (SM-A300F, MSM8916, AMS452EF01): byte clock settles at 57740991 Hz (VCO 461927929 Hz, within 5 Hz of pixel clock) and backlight updates no longer corrupt scanout.

Fixes 6cd33b6f4155 ("drm/msm/dsi: round 6G byte clock rate to the PLL-achievable value")
---
 drivers/gpu/drm/msm/dsi/phy/dsi_phy_28nm.c | 119 +++++++++++++++------
 1 file changed, 87 insertions(+), 32 deletions(-)
drivers/gpu/drm/msm/dsi/phy/dsi_phy_28nm.c +87 -32
index d00e415b9..2d4a29b54 100644
@@ -6,6 +6,7 @@
 #include <dt-bindings/clock/qcom,dsi-phy-28nm.h>
 #include <linux/clk.h>
 #include <linux/clk-provider.h>
+#include <linux/math64.h>
 
 #include "dsi_phy.h"
 #include "dsi.xml.h"
@@ -114,31 +115,101 @@ static void pll_28nm_software_reset(struct dsi_pll_28nm *pll_28nm)
 /*
  * Clock Callbacks
  */
+static unsigned long dsi_pll_28nm_calc_vco_rate(struct dsi_pll_28nm *pll_28nm,
+						unsigned long rate,
+						u32 *div_fb, u32 *frac_n,
+						bool *frac_n_mode)
+{
+	unsigned long min_rate = pll_28nm->phy->cfg->min_pll_rate;
+	unsigned long max_rate = pll_28nm->phy->cfg->max_pll_rate;
+	unsigned long actual;
+	u32 ref_clk, div, frac;
+
+	rate = clamp_t(unsigned long, rate, min_rate, max_rate);
+
+	if (rate % VCO_REF_CLK_RATE == 0) {
+		if (frac_n_mode)
+			*frac_n_mode = false;
+		if (div_fb)
+			*div_fb = rate / VCO_REF_CLK_RATE;
+		if (frac_n)
+			*frac_n = 0;
+		return rate;
+	}
+
+	ref_clk = VCO_REF_CLK_RATE * 2;
+	div = rate / ref_clk;
+	frac = DIV_ROUND_CLOSEST_ULL((u64)(rate % ref_clk) << 16, ref_clk);
+	if (frac >= BIT(16)) {
+		div++;
+		frac = 0;
+	}
+
+	actual = ref_clk * div + mult_frac((unsigned long)ref_clk, frac, BIT(16));
+	if (actual > max_rate) {
+		if (frac > 0) {
+			frac--;
+		} else {
+			div--;
+			frac = BIT(16) - 1;
+		}
+		actual = ref_clk * div + mult_frac((unsigned long)ref_clk, frac, BIT(16));
+	} else if (actual < min_rate) {
+		frac++;
+		if (frac >= BIT(16)) {
+			div++;
+			frac = 0;
+		}
+		actual = ref_clk * div + mult_frac((unsigned long)ref_clk, frac, BIT(16));
+	}
+
+	if (frac == 0) {
+		if (frac_n_mode)
+			*frac_n_mode = false;
+		if (div_fb)
+			*div_fb = actual / VCO_REF_CLK_RATE;
+		if (frac_n)
+			*frac_n = 0;
+		return actual;
+	}
+
+	if (frac_n_mode)
+		*frac_n_mode = true;
+	if (div_fb)
+		*div_fb = div;
+	if (frac_n)
+		*frac_n = frac;
+	return actual;
+}
+
 static int dsi_pll_28nm_clk_set_rate(struct clk_hw *hw, unsigned long rate,
 		unsigned long parent_rate)
 {
 	struct dsi_pll_28nm *pll_28nm = to_pll_28nm(hw);
 	struct device *dev = &pll_28nm->phy->pdev->dev;
 	void __iomem *base = pll_28nm->phy->pll_base;
-	unsigned long div_fbx1000, gen_vco_clk;
-	u32 refclk_cfg, frac_n_mode, frac_n_value;
+	unsigned long gen_vco_clk;
+	bool frac_n_mode;
+	u32 refclk_cfg, div_fb, frac_n_value;
 	u32 sdm_cfg0, sdm_cfg1, sdm_cfg2, sdm_cfg3;
 	u32 cal_cfg10, cal_cfg11;
-	u32 rem;
 	int i;
 
 	VERB("rate=%lu, parent's=%lu", rate, parent_rate);
 
+	gen_vco_clk = dsi_pll_28nm_calc_vco_rate(pll_28nm, rate, &div_fb,
+						 &frac_n_value, &frac_n_mode);
+
 	/* Force postdiv2 to be div-4 */
 	writel(3, base + REG_DSI_28nm_PHY_PLL_POSTDIV2_CFG);
 
 	/* Configure the Loop filter resistance */
 	for (i = 0; i < LPFR_LUT_SIZE; i++)
-		if (rate <= lpfr_lut[i].vco_rate)
+		if (gen_vco_clk <= lpfr_lut[i].vco_rate)
 			break;
 	if (i == LPFR_LUT_SIZE) {
 		DRM_DEV_ERROR(dev, "unable to get loop filter resistance. vco=%lu\n",
-				rate);
+				gen_vco_clk);
 		return -EINVAL;
 	}
 	writel(lpfr_lut[i].resistance, base + REG_DSI_28nm_PHY_PLL_LPFR_CFG);
@@ -147,42 +218,26 @@ static int dsi_pll_28nm_clk_set_rate(struct clk_hw *hw, unsigned long rate,
 	writel(0x70, base + REG_DSI_28nm_PHY_PLL_LPFC1_CFG);
 	writel(0x15, base + REG_DSI_28nm_PHY_PLL_LPFC2_CFG);
 
-	rem = rate % VCO_REF_CLK_RATE;
-	if (rem) {
-		refclk_cfg = DSI_28nm_PHY_PLL_REFCLK_CFG_DBLR;
-		frac_n_mode = 1;
-		div_fbx1000 = rate / (VCO_REF_CLK_RATE / 500);
-		gen_vco_clk = div_fbx1000 * (VCO_REF_CLK_RATE / 500);
-	} else {
-		refclk_cfg = 0x0;
-		frac_n_mode = 0;
-		div_fbx1000 = rate / (VCO_REF_CLK_RATE / 1000);
-		gen_vco_clk = div_fbx1000 * (VCO_REF_CLK_RATE / 1000);
-	}
+	refclk_cfg = frac_n_mode ? DSI_28nm_PHY_PLL_REFCLK_CFG_DBLR : 0x0;
 
 	DBG("refclk_cfg = %d", refclk_cfg);
-
-	rem = div_fbx1000 % 1000;
-	frac_n_value = (rem << 16) / 1000;
-
-	DBG("div_fb = %lu", div_fbx1000);
-	DBG("frac_n_value = %d", frac_n_value);
-
+	DBG("div_fb = %u", div_fb);
+	DBG("frac_n_value = %u", frac_n_value);
 	DBG("Generated VCO Clock: %lu", gen_vco_clk);
-	rem = 0;
+
 	sdm_cfg1 = readl(base + REG_DSI_28nm_PHY_PLL_SDM_CFG1);
 	sdm_cfg1 &= ~DSI_28nm_PHY_PLL_SDM_CFG1_DC_OFFSET__MASK;
 	if (frac_n_mode) {
 		sdm_cfg0 = 0x0;
 		sdm_cfg0 |= DSI_28nm_PHY_PLL_SDM_CFG0_BYP_DIV(0);
 		sdm_cfg1 |= DSI_28nm_PHY_PLL_SDM_CFG1_DC_OFFSET(
-				(u32)(((div_fbx1000 / 1000) & 0x3f) - 1));
+				(div_fb & 0x3f) - 1);
 		sdm_cfg3 = frac_n_value >> 8;
 		sdm_cfg2 = frac_n_value & 0xff;
 	} else {
 		sdm_cfg0 = DSI_28nm_PHY_PLL_SDM_CFG0_BYP;
 		sdm_cfg0 |= DSI_28nm_PHY_PLL_SDM_CFG0_BYP_DIV(
-				(u32)(((div_fbx1000 / 1000) & 0x3f) - 1));
+				(div_fb & 0x3f) - 1);
 		sdm_cfg1 |= DSI_28nm_PHY_PLL_SDM_CFG1_DC_OFFSET(0);
 		sdm_cfg2 = 0;
 		sdm_cfg3 = 0;
@@ -538,9 +593,8 @@ static int dsi_pll_28nm_clk_determine_rate(struct clk_hw *hw,
 {
 	struct dsi_pll_28nm *pll_28nm = to_pll_28nm(hw);
 
-	req->rate = clamp_t(unsigned long, req->rate,
-			    pll_28nm->phy->cfg->min_pll_rate,
-			    pll_28nm->phy->cfg->max_pll_rate);
+	req->rate = dsi_pll_28nm_calc_vco_rate(pll_28nm, req->rate,
+					       NULL, NULL, NULL);
 
 	return 0;
 }
@@ -650,7 +704,7 @@ static int pll_28nm_register(struct dsi_pll_28nm *pll_28nm, struct clk_hw **prov
 			&pll_28nm->clk_hw, CLK_SET_RATE_PARENT,
 			pll_28nm->phy->pll_base +
 				REG_DSI_28nm_PHY_PLL_POSTDIV1_CFG,
-			0, 4, 0, NULL);
+			0, 4, CLK_DIVIDER_ROUND_CLOSEST, NULL);
 	if (IS_ERR(analog_postdiv))
 		return PTR_ERR(analog_postdiv);
 
@@ -675,7 +729,8 @@ static int pll_28nm_register(struct dsi_pll_28nm *pll_28nm, struct clk_hw **prov
 				&pll_28nm->clk_hw,
 				indirect_path_div2,
 			}), 2, CLK_SET_RATE_PARENT, pll_28nm->phy->pll_base +
-				REG_DSI_28nm_PHY_PLL_VREG_CFG, 1, 1, 0, NULL);
+				REG_DSI_28nm_PHY_PLL_VREG_CFG, 1, 1,
+			CLK_MUX_ROUND_CLOSEST, NULL);
 	if (IS_ERR(byte_mux))
 		return PTR_ERR(byte_mux);
 

base-commit: dca2f1b25860f8caa0406f858e76a3a21671a012

Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/msm/dsi: upward VCO rounding in dsi_pll_28nm_calc_vco_rate() breaks RCG/PLL --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/msm/dsi: upward VCO rounding in dsi_pll_28nm_calc_vco_rate() breaks RCG/PLL
--

commit 04136cd645856db1e70c20fc070ead60e3649cdf
Author: Vladimir Bely <[email protected]>

drm/msm/dsi: phy_28nm: return exact PLL-achievable VCO rate in determine_rate

This commit updates dsi_pll_28nm_clk_determine_rate() and dsi_pll_28nm_clk_set_rate() to compute the 16-bit fractional-N divider and resulting VCO rate via a shared dsi_pll_28nm_calc_vco_rate() helper, and enables closest-rate rounding on analog_postdiv and byte_mux.

Quoted 33 lines
drivers/gpu/drm/msm/dsi/phy/dsi_phy_28nm.c +25 -0
index d00e415b9a991..2d4a29b546c18 100644
@@ -114,31 +115,101 @@ static void pll_28nm_software_reset(struct dsi_pll_28nm *pll_28nm)
 /*
  * Clock Callbacks
  */
+static unsigned long dsi_pll_28nm_calc_vco_rate(struct dsi_pll_28nm *pll_28nm,
+						unsigned long rate,
+						u32 *div_fb, u32 *frac_n,
+						bool *frac_n_mode)
+{
+	unsigned long min_rate = pll_28nm->phy->cfg->min_pll_rate;
+	unsigned long max_rate = pll_28nm->phy->cfg->max_pll_rate;
+	unsigned long actual;
+	u32 ref_clk, div, frac;
+
+	rate = clamp_t(unsigned long, rate, min_rate, max_rate);
+
+	if (rate % VCO_REF_CLK_RATE == 0) {
+		if (frac_n_mode)
+			*frac_n_mode = false;
+		if (div_fb)
+			*div_fb = rate / VCO_REF_CLK_RATE;
+		if (frac_n)
+			*frac_n = 0;
+		return rate;
+	}
+
+	ref_clk = VCO_REF_CLK_RATE * 2;
+	div = rate / ref_clk;
+	frac = DIV_ROUND_CLOSEST_ULL((u64)(rate % ref_clk) << 16, ref_clk);

[ ... ]

Quoted 10 lines
drivers/gpu/drm/msm/dsi/phy/dsi_phy_28nm.c +1 -1
@@ -650,7 +704,7 @@ static int pll_28nm_register(struct dsi_pll_28nm *pll_28nm, struct clk_hw **prov
 			&pll_28nm->clk_hw, CLK_SET_RATE_PARENT,
 			pll_28nm->phy->pll_base +
 				REG_DSI_28nm_PHY_PLL_POSTDIV1_CFG,
-			0, 4, 0, NULL);
+			0, 4, CLK_DIVIDER_ROUND_CLOSEST, NULL);
 	if (IS_ERR(analog_postdiv))
 		return PTR_ERR(analog_postdiv);
 

drivers/gpu/drm/msm/dsi/phy/dsi_phy_28nm.c static int pll_28nm_register(struct dsi_pll_28nm *pll_28nm, struct clk_hw **prov · line 735

Quoted 8 lines
drivers/gpu/drm/msm/dsi/phy/dsi_phy_28nm.c +2 -1
@@ -675,7 +729,8 @@ static int pll_28nm_register(struct dsi_pll_28nm *pll_28nm, struct clk_hw **prov
 				&pll_28nm->clk_hw,
 				indirect_path_div2,
 			}), 2, CLK_SET_RATE_PARENT, pll_28nm->phy->pll_base +
-				REG_DSI_28nm_PHY_PLL_VREG_CFG, 1, 1, 0, NULL);
+				REG_DSI_28nm_PHY_PLL_VREG_CFG, 1, 1,
+			CLK_MUX_ROUND_CLOSEST, NULL);
 	if (IS_ERR(byte_mux))
 		return PTR_ERR(byte_mux);

[Severity: High]
Can rounding frac up with DIV_ROUND_CLOSEST_ULL() in dsi_pll_28nm_calc_vco_rate(), combined with CLK_DIVIDER_ROUND_CLOSEST and CLK_MUX_ROUND_CLOSEST, cause the downstream byte and pixel RCGs to pick the wrong dividers when the VCO rate rounds above the requested rate?

Whenever rate % ref_clk lies in the upper half of an SDM step, dsi_pll_28nm_calc_vco_rate() returns an actual VCO rate up to 292 Hz above the requested rate. With closest-rate rounding enabled on analog_postdiv and byte_mux, dsiNpllbyte can return parent_rate > rate to the downstream byte RCG:

drivers/clk/qcom/clk-rcg2.c:clk_byte2_determine_rate() {
	p = req->best_parent_hw;
	req->best_parent_rate = parent_rate = clk_hw_round_rate(p, rate);

	div = DIV_ROUND_UP((2 * parent_rate), rate) - 1;
	div = min_t(u32, div, mask);

	req->rate = calc_rate(parent_rate, 0, 0, 0, div);
}

When parent_rate exceeds rate by even 1 Hz, DIV_ROUND_UP(2 * parent_rate, rate) - 1 jumps from 1 (1:1 bypass) to 2 (a 1.5x pre-divider), so clk_round_rate(msm_host->byte_clk, ...) drops msm_host->byte_clk_rate to 2/3 of the target panel byte clock (for example, on s6d7aa0_lsl080al02_mode where byte_clk_rate is 55986750 Hz, VCO rounds up by 140 Hz to 447894140 Hz, dsi0pllbyte returns 55986767 Hz, and the byte RCG drops to 37324511 Hz).

In addition, dsiNpll (REG_DSI_28nm_PHY_PLL_POSTDIV3_CFG) is registered in pll_28nm_register() without CLK_DIVIDER_ROUND_CLOSEST. When the VCO is rounded up even by 1 Hz above M * pixel_clk_rate, dsiNpll's _div_round_up() increments the POSTDIV3 divider from M to M + 1, causing clk_pixel_determine_rate() to miss the 100 kHz delta window and fail clk_set_rate(msm_host->pixel_clk, ...) with -EINVAL.

Truncating frac downward instead of using DIV_ROUND_CLOSEST_ULL() keeps the achieved VCO rate at or below the target rate while still matching recalc_rate() once clk_round_rate() has quantized byte_clk_rate.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1