mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] drm/msm/dsi: phy_28nm: return exact PLL-achievable VCO rate in determine_rate
@ 2026-11-29 10:30 Vladimir Bely
  0 siblings, 0 replies; only message in thread
From: Vladimir Bely @ 2026-11-29 10:30 UTC (permalink / raw)
  To: Rob Clark, Dmitry Baryshkov
  Cc: Abhinav Kumar, Jessica Zhang, Sean Paul, Marijn Suijten,
	David Airlie, Simona Vetter, Kavan Smith, linux-arm-msm,
	dri-devel, freedreno, linux-kernel, Vladimir Bely, stable

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")
Cc: stable@vger.kernel.org
Signed-off-by: Vladimir Bely <vlwwwwww@gmail.com>
---
 drivers/gpu/drm/msm/dsi/phy/dsi_phy_28nm.c | 119 +++++++++++++++------
 1 file changed, 87 insertions(+), 32 deletions(-)

diff --git a/drivers/gpu/drm/msm/dsi/phy/dsi_phy_28nm.c b/drivers/gpu/drm/msm/dsi/phy/dsi_phy_28nm.c
index d00e415b9..2d4a29b54 100644
--- a/drivers/gpu/drm/msm/dsi/phy/dsi_phy_28nm.c
+++ b/drivers/gpu/drm/msm/dsi/phy/dsi_phy_28nm.c
@@ -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
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-29 10:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-29 10:30 [PATCH] drm/msm/dsi: phy_28nm: return exact PLL-achievable VCO rate in determine_rate Vladimir Bely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®