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(). Quantize downward (largest frac whose truncated mult_frac() does not exceed the requested remainder) so the PLL never returns a rate above the request on the byte-clock path, which would otherwise cause clk_byte2_determine_rate() to jump from 1:1 bypass to a 1.5x divider. Also propagate the integer-division remainder (rate * div + div - 1) through the 28nm byte-clock dividers (analog_postdiv, indirect_path_div2, and dsiNpllbyte) so clk_round_rate(byte_clk, ...) is idempotent under downward truncation instead of stepping down by one SDM increment on each call, and give dsiNpll (POSTDIV3) a closest-divider clk_ops that handles rate >= parent_rate safely (avoiding the division by zero in generic clk-divider's _div_round_closest()) so an SDM step sitting a few Hz above M * pixel_clk_rate still selects divider M within clk_pixel_determine_rate()'s +/-100 kHz window. Tested on Samsung Galaxy A3 (SM-A300F, MSM8916, AMS452EF01): byte clock settles at 57740991 Hz (VCO 461927929 Hz) with zero drift across repeated backlight updates and no scanout corruption. 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 --- Changes in v4: - Replace generic clk_divider + CLK_DIVIDER_ROUND_CLOSEST on dsiNpll (POSTDIV3) with dsi_pll_28nm_postdiv3_ops so requests with rate >= parent_rate (such as clk_pixel_determine_rate() probing frac_table_pixel { 2, 9 } when VCO < 4.5 * pixel_clk_rate) clamp to div = 1 instead of triggering a division by zero in drivers/clk/clk-divider.c:_div_round_closest(). Changes in v3: - Select the closest divider on dsiNpll (POSTDIV3) so when the byte-path remainder budget lets the VCO lock a few Hz above M * pixel_clk_rate (e.g. 4-lane RGB888 at 32018 kHz), POSTDIV3 picks divider M instead of stepping up to M + 1 and failing clk_pixel_determine_rate() with -EINVAL. Changes in v2: - Quantize frac downward in dsi_pll_28nm_calc_vco_rate() instead of using DIV_ROUND_CLOSEST_ULL(), and drop CLK_DIVIDER_ROUND_CLOSEST / CLK_MUX_ROUND_CLOSEST on the byte-clock path so dsiNpllbyte never exceeds the requested rate. - Replace the generic fixed-factor/divider ops on the 28nm byte-clock path (analog_postdiv, indirect_path_div2, dsiNpllbyte) with floor-exact determine_rate() callbacks that pass (rate * div + div - 1) upstream so clk_round_rate() remains idempotent without downward drift. drivers/gpu/drm/msm/dsi/phy/dsi_phy_28nm.c | 391 ++++++++++++++++++--- 1 file changed, 336 insertions(+), 55 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..cdee62e44 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,9 @@ #include #include #include +#include +#include +#include #include "dsi_phy.h" #include "dsi.xml.h" @@ -67,8 +70,22 @@ struct pll_28nm_cached_state { u8 byte_mux; }; +struct dsi_pll_28nm_fixed_div { + struct clk_hw hw; + u32 div; +}; + +struct dsi_pll_28nm_postdiv { + struct clk_hw hw; + void __iomem *reg; +}; + struct dsi_pll_28nm { struct clk_hw clk_hw; + struct dsi_pll_28nm_postdiv postdiv1; + struct dsi_pll_28nm_postdiv postdiv3; + struct dsi_pll_28nm_fixed_div indirect_div2; + struct dsi_pll_28nm_fixed_div pllbyte; struct msm_dsi_phy *phy; @@ -76,6 +93,8 @@ struct dsi_pll_28nm { }; #define to_pll_28nm(x) container_of(x, struct dsi_pll_28nm, clk_hw) +#define to_pll_28nm_fixed_div(x) container_of(x, struct dsi_pll_28nm_fixed_div, hw) +#define to_pll_28nm_postdiv(x) container_of(x, struct dsi_pll_28nm_postdiv, hw) static bool pll_28nm_poll_for_ready(struct dsi_pll_28nm *pll_28nm, u32 nb_tries, u32 timeout_us) @@ -114,31 +133,98 @@ 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, rem, 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; + rem = rate % ref_clk; + /* + * Largest frac whose truncated mult_frac() does not exceed rem, + * keeping calc_vco_rate() idempotent without rounding above rate. + */ + frac = div_u64(((u64)(rem + 1) << 16) - 1, ref_clk); + if (frac >= BIT(16)) { + div++; + frac = 0; + } + + actual = ref_clk * div + mult_frac((unsigned long)ref_clk, frac, BIT(16)); + 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 +233,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 +608,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; } @@ -615,6 +684,183 @@ static int dsi_28nm_pll_restore_state(struct msm_dsi_phy *phy) return 0; } +static unsigned long dsi_pll_28nm_fixed_div_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + struct dsi_pll_28nm_fixed_div *fdiv = to_pll_28nm_fixed_div(hw); + + return parent_rate / fdiv->div; +} + +static int dsi_pll_28nm_fixed_div_determine_rate(struct clk_hw *hw, + struct clk_rate_request *req) +{ + struct dsi_pll_28nm_fixed_div *fdiv = to_pll_28nm_fixed_div(hw); + struct clk_hw *parent = clk_hw_get_parent(hw); + unsigned long parent_req; + + if (!parent) + return -EINVAL; + + /* Include remainder so floor(parent / div) stays idempotent */ + if (req->rate > (ULONG_MAX - (fdiv->div - 1)) / fdiv->div) + parent_req = ULONG_MAX; + else + parent_req = req->rate * fdiv->div + fdiv->div - 1; + + req->best_parent_rate = clk_hw_round_rate(parent, parent_req); + req->rate = req->best_parent_rate / fdiv->div; + + return 0; +} + +static int dsi_pll_28nm_fixed_div_set_rate(struct clk_hw *hw, + unsigned long rate, + unsigned long parent_rate) +{ + return 0; +} + +static const struct clk_ops dsi_pll_28nm_fixed_div_ops = { + .recalc_rate = dsi_pll_28nm_fixed_div_recalc_rate, + .determine_rate = dsi_pll_28nm_fixed_div_determine_rate, + .set_rate = dsi_pll_28nm_fixed_div_set_rate, +}; + +static unsigned long dsi_pll_28nm_postdiv1_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + struct dsi_pll_28nm_postdiv *pdiv = to_pll_28nm_postdiv(hw); + u32 div = (readl(pdiv->reg) & 0xf) + 1; + + return parent_rate / div; +} + +static int dsi_pll_28nm_postdiv1_determine_rate(struct clk_hw *hw, + struct clk_rate_request *req) +{ + struct clk_hw *parent = clk_hw_get_parent(hw); + unsigned long best_rate = 0, best_parent = 0; + unsigned long rate = req->rate ?: 1; + u32 d; + + if (!parent) + return -EINVAL; + + for (d = 1; d <= 16; d++) { + unsigned long parent_req, p_rate, now; + + if (rate > (ULONG_MAX - (d - 1)) / d) + parent_req = ULONG_MAX; + else + parent_req = rate * d + d - 1; + + p_rate = clk_hw_round_rate(parent, parent_req); + now = p_rate / d; + if (now <= rate && now > best_rate) { + best_rate = now; + best_parent = p_rate; + } + } + + if (!best_rate) { + best_parent = clk_hw_round_rate(parent, 0); + best_rate = best_parent / 16; + } + + req->best_parent_rate = best_parent; + req->rate = best_rate; + + return 0; +} + +static int dsi_pll_28nm_postdiv1_set_rate(struct clk_hw *hw, + unsigned long rate, + unsigned long parent_rate) +{ + struct dsi_pll_28nm_postdiv *pdiv = to_pll_28nm_postdiv(hw); + u32 val, div; + + if (!rate) + return -EINVAL; + + div = clamp_t(unsigned long, parent_rate / rate, 1, 16); + val = readl(pdiv->reg); + val &= ~0xf; + val |= div - 1; + writel(val, pdiv->reg); + + return 0; +} + +static const struct clk_ops dsi_pll_28nm_postdiv1_ops = { + .recalc_rate = dsi_pll_28nm_postdiv1_recalc_rate, + .determine_rate = dsi_pll_28nm_postdiv1_determine_rate, + .set_rate = dsi_pll_28nm_postdiv1_set_rate, +}; + +static unsigned long dsi_pll_28nm_postdiv3_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + struct dsi_pll_28nm_postdiv *pdiv = to_pll_28nm_postdiv(hw); + u32 div = (readl(pdiv->reg) & 0xff) + 1; + + return parent_rate / div; +} + +static int dsi_pll_28nm_postdiv3_determine_rate(struct clk_hw *hw, + struct clk_rate_request *req) +{ + unsigned long parent_rate = req->best_parent_rate; + unsigned long rate = req->rate ?: 1; + u32 d_lo, d_hi, div; + + if (rate >= parent_rate) { + div = 1; + } else { + d_lo = clamp_t(unsigned long, parent_rate / rate, 1, 256); + d_hi = clamp_t(unsigned long, DIV_ROUND_UP(parent_rate, rate), 1, 256); + if (abs_diff(parent_rate / d_lo, rate) < + abs_diff(parent_rate / d_hi, rate)) + div = d_lo; + else + div = d_hi; + } + + req->rate = parent_rate / div; + + return 0; +} + +static int dsi_pll_28nm_postdiv3_set_rate(struct clk_hw *hw, + unsigned long rate, + unsigned long parent_rate) +{ + struct dsi_pll_28nm_postdiv *pdiv = to_pll_28nm_postdiv(hw); + u32 val, div; + + if (!rate) + return -EINVAL; + + if (rate >= parent_rate) + div = 1; + else + div = clamp_t(unsigned long, DIV_ROUND_CLOSEST(parent_rate, rate), + 1, 256); + val = readl(pdiv->reg); + val &= ~0xff; + val |= div - 1; + writel(val, pdiv->reg); + + return 0; +} + +static const struct clk_ops dsi_pll_28nm_postdiv3_ops = { + .recalc_rate = dsi_pll_28nm_postdiv3_recalc_rate, + .determine_rate = dsi_pll_28nm_postdiv3_determine_rate, + .set_rate = dsi_pll_28nm_postdiv3_set_rate, +}; + static int pll_28nm_register(struct dsi_pll_28nm *pll_28nm, struct clk_hw **provided_clocks) { char clk_name[32]; @@ -626,8 +872,34 @@ static int pll_28nm_register(struct dsi_pll_28nm *pll_28nm, struct clk_hw **prov .name = clk_name, .flags = CLK_IGNORE_UNUSED, }; + struct clk_init_data postdiv1_init = { + .parent_hws = (const struct clk_hw *[]) { + &pll_28nm->clk_hw, + }, + .num_parents = 1, + .name = clk_name, + .ops = &dsi_pll_28nm_postdiv1_ops, + .flags = CLK_SET_RATE_PARENT, + }; + struct clk_init_data div2_init = { + .parent_hws = (const struct clk_hw *[]) { + &pll_28nm->postdiv1.hw, + }, + .num_parents = 1, + .name = clk_name, + .ops = &dsi_pll_28nm_fixed_div_ops, + .flags = CLK_SET_RATE_PARENT, + }; + struct clk_init_data postdiv3_init = { + .parent_hws = (const struct clk_hw *[]) { + &pll_28nm->clk_hw, + }, + .num_parents = 1, + .name = clk_name, + .ops = &dsi_pll_28nm_postdiv3_ops, + }; struct device *dev = &pll_28nm->phy->pdev->dev; - struct clk_hw *hw, *analog_postdiv, *indirect_path_div2, *byte_mux; + struct clk_hw *byte_mux; int ret; DBG("%d", pll_28nm->phy->id); @@ -646,45 +918,54 @@ static int pll_28nm_register(struct dsi_pll_28nm *pll_28nm, struct clk_hw **prov return ret; snprintf(clk_name, sizeof(clk_name), "dsi%danalog_postdiv_clk", pll_28nm->phy->id); - analog_postdiv = devm_clk_hw_register_divider_parent_hw(dev, clk_name, - &pll_28nm->clk_hw, CLK_SET_RATE_PARENT, - pll_28nm->phy->pll_base + - REG_DSI_28nm_PHY_PLL_POSTDIV1_CFG, - 0, 4, 0, NULL); - if (IS_ERR(analog_postdiv)) - return PTR_ERR(analog_postdiv); + pll_28nm->postdiv1.reg = pll_28nm->phy->pll_base + + REG_DSI_28nm_PHY_PLL_POSTDIV1_CFG; + pll_28nm->postdiv1.hw.init = &postdiv1_init; + ret = devm_clk_hw_register(dev, &pll_28nm->postdiv1.hw); + if (ret) + return ret; snprintf(clk_name, sizeof(clk_name), "dsi%dindirect_path_div2_clk", pll_28nm->phy->id); - indirect_path_div2 = devm_clk_hw_register_fixed_factor_parent_hw(dev, - clk_name, analog_postdiv, CLK_SET_RATE_PARENT, 1, 2); - if (IS_ERR(indirect_path_div2)) - return PTR_ERR(indirect_path_div2); + pll_28nm->indirect_div2.div = 2; + pll_28nm->indirect_div2.hw.init = &div2_init; + ret = devm_clk_hw_register(dev, &pll_28nm->indirect_div2.hw); + if (ret) + return ret; snprintf(clk_name, sizeof(clk_name), "dsi%dpll", pll_28nm->phy->id); - hw = devm_clk_hw_register_divider_parent_hw(dev, clk_name, - &pll_28nm->clk_hw, 0, pll_28nm->phy->pll_base + - REG_DSI_28nm_PHY_PLL_POSTDIV3_CFG, - 0, 8, 0, NULL); - if (IS_ERR(hw)) - return PTR_ERR(hw); - provided_clocks[DSI_PIXEL_PLL_CLK] = hw; + pll_28nm->postdiv3.reg = pll_28nm->phy->pll_base + + REG_DSI_28nm_PHY_PLL_POSTDIV3_CFG; + pll_28nm->postdiv3.hw.init = &postdiv3_init; + ret = devm_clk_hw_register(dev, &pll_28nm->postdiv3.hw); + if (ret) + return ret; + provided_clocks[DSI_PIXEL_PLL_CLK] = &pll_28nm->postdiv3.hw; snprintf(clk_name, sizeof(clk_name), "dsi%dbyte_mux", pll_28nm->phy->id); byte_mux = devm_clk_hw_register_mux_parent_hws(dev, clk_name, ((const struct clk_hw *[]){ &pll_28nm->clk_hw, - indirect_path_div2, + &pll_28nm->indirect_div2.hw, }), 2, CLK_SET_RATE_PARENT, pll_28nm->phy->pll_base + REG_DSI_28nm_PHY_PLL_VREG_CFG, 1, 1, 0, NULL); if (IS_ERR(byte_mux)) return PTR_ERR(byte_mux); snprintf(clk_name, sizeof(clk_name), "dsi%dpllbyte", pll_28nm->phy->id); - hw = devm_clk_hw_register_fixed_factor_parent_hw(dev, clk_name, - byte_mux, CLK_SET_RATE_PARENT, 1, 4); - if (IS_ERR(hw)) - return PTR_ERR(hw); - provided_clocks[DSI_BYTE_PLL_CLK] = hw; + pll_28nm->pllbyte.div = 4; + pll_28nm->pllbyte.hw.init = &(const struct clk_init_data) { + .parent_hws = (const struct clk_hw *[]) { + byte_mux, + }, + .num_parents = 1, + .name = clk_name, + .ops = &dsi_pll_28nm_fixed_div_ops, + .flags = CLK_SET_RATE_PARENT, + }; + ret = devm_clk_hw_register(dev, &pll_28nm->pllbyte.hw); + if (ret) + return ret; + provided_clocks[DSI_BYTE_PLL_CLK] = &pll_28nm->pllbyte.hw; return 0; } base-commit: dca2f1b25860f8caa0406f858e76a3a21671a012 -- 2.43.0