Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.45 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795958416; b=qp/KxnXU8aN+7a9L8oEc7h/y8nk9LpeP9IfeeVLGQIe3RBAjLnRNy3Pp7T+stAtGK4g+ SXUybdQRxpgy/8kDxOc2+RUbzwuxLeiM6SNJihZbGZ8FpDvkB37fXE/iAZnTGGWCm1d1z 3suPiLrEFe0rEf7p5Oook8uVXW+n4QBFcM0nuRTtc52ehR9U6oO1bGFsVq2hfUwi66Lqz 5HU8g4+SiK45FibUBWd2lnvI3p24xqz9KdK90/s5rcWrwBYL3C5uxfMaL2CKj5s5/helu 9DugoQruymH97Ju7sSz7iNle6qY3aBaKTuGFtiXvFBUA5/QbVvBd8K7n4NThr6YfqlQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795958416; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=jNFqBKFuh/qd3lNIGeNPfraKgj6FAIkRfPUIaYdwBs0=; b=M+20qqGqf5vBQRA5pg+Dp4Yetj1SuRrpjQw0chCn4beSFr+YKNHeL3RR7lpxtiXuwgC3 DEEeIIwbbY+/LneEQk8e8rkr0mCRdlP6D/ZJp5DarF9USuXbspkg/y2v37pspHdkBHGiB ELhoZIF6L+D6KB01VAV52Cj5lYJaRnnhE19e8npPE+XSoDo0T1vhS2fOdyQw5cYpveVxK il/RmSp3GrwrUMOCEuh4nOxHGWO39zZFbTOHGpJ/XavGAeK2oRcmgy/HePbCUqLJQjtRZ Ke9mqUQwAqMsuLbymnyQDMVbzRiLgxBIShcRMv2GGaVFduz5u+D0480ILGpgt5uBbvw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ek+pd33T; arc=none smtp.remote-ip=209.85.221.45 Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8AF82606A323 for ; Sun, 29 Nov 2026 13:20:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8AF82606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ek+pd33T Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-4acb44dcd7bso8543415f8f.0 for ; Sun, 29 Nov 2026 05:20:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795958414; x=1796563214; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=jNFqBKFuh/qd3lNIGeNPfraKgj6FAIkRfPUIaYdwBs0=; b=ek+pd33Tj1jd7dygb/ST7OM/PXJyijRSn89XW8TDWq8wkZS1BWK58TXie9zj+rfDr9 J9cMggQdJpeVcz2tR/x24xUhBZM46ouNjYl6sOknkgTL86/HFDtA//7WrMXxJWPfrPbm 2sboV26MWpayVkNhJlCeqBSPEngC1ZZ5WPuPr3h6jB63ohwdsuZK6npOByG/fLsMVais ZOsSkRrqTeoOwyffIvM14bjHXuMPBeHJnb2jLAIAinFnDjfvHzU9+TAZmyxqV9gYps2h 43dcTQKJPsxhkFvZ4eb4owRAc3Q+PYBQh8JXaYsnNybpVuTokpt+TQqnMg+F03mi3UFZ dKCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795958414; x=1796563214; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=jNFqBKFuh/qd3lNIGeNPfraKgj6FAIkRfPUIaYdwBs0=; b=VroDrBpApamrnjfVtyI0qB0YcxZy5iZZz9Aq+StPWrR4kJtA7SObLOdGc3rRqLqaxU rSKm3NXwx+qg1y6QO1zDZTAoQngJf+NQ4ZkPhDfAOBv+U9xJR0nSungfVq+KkZjgzAo0 zzuWH82GQiYDOUrYZkqEC3ufd5VNs4X1jS9SVDLDR2rHZndMK1RXG2xHOdR8xwkUew27 s/dLJlssKrG+JRzfFuYNDt7Lcbhbl+4Yg4sQAR0eML6n4kXeYZO6x6LJ00He9JVCGOde l+F64k7L2il+U96/dppQ+KGCCg2ZW5eL+1nfLV9Yxkcnd7H8jzALEPQplXvNfnY6zZ/U pV2g== X-Forwarded-Encrypted: i=1; AL+hErH0fElDi8SNcR+2fAfoVfCA1VhPVPRORgCGtGi3h56vgAd91rpK+sRSbyDCY/oZwuV6uUPigBoJfIIae5uC@vger.kernel.org X-Gm-Message-State: APuU4Z+G6bweXHfdLBZ2LRXJ3xyZbOZChuZ2BlHX6p8l5OElZjCoMQeo SxkFf8aacFOQqHsMSdq36LJeINSCTNfiCGZ3gN2QsMjxSl+gwcJd8/eH X-Gm-Gg: AZFdTZfOvfHLGVK4qoPH6rv8jKz7u1y/tjrG3v46/cgAdhZjWhA+EO2JY0C4Yi4Tza/ qsDZ43WMnl0165iXhbJYiccfRF1qGrAkJp4hg185BJ3LmHpBC53acfgkCkxKH4tClEbSQRB1qI9 7PiwORY/QdnkJl4h5WGVFI/BowwQRYm2+7uqsJTBVo9oVTLq2P3oQX4IfMgWwuKK07Bfmahs9Xd BVMgOzeehd5V2Hnznk8TIe14n6Uvoc/zbdW4zSWyVSk1+sga0xBArZxwkdSff2dZvCMLxS/dqRr bB7UcUVuBoniyywLpVWGQzmMxOITzq9JSxwUaIMqKRt8XJlO6xUBGJFWpLHPdPtfnDMdYDq1+Cx dHF08Flds01CwHEHaIqucEEPbyd94uTFLdof2Vg/1G3QlJHZ2ajik38lRThFuH+N3V9QpgGSSJ4 /OppHMXB+TphsbfW24YPBM7oqbllYY0xmx6G7A9/9DZhCMeaWzuA79AR9fhaEfsVuRvQ93newD0 4V3Ad/pIuxsMXKpB8eazJ+3DVHvphNVYmrjS3YGXTMqNzbn+BX0d+oupDUV6FNjDb88no+2vO01 TvmOb5xOwaEHumwciOOxfaZ67jQ/p+ObqgSNRauiNhv/FKXaxVYTD9436Bs6lLaMFLXcklEmdpM = X-Received: by 2002:a05:6000:1865:b0:4ac:5eed:1d62 with SMTP id ffacd0b85a97d-4ac5eed1d7dmr67070816f8f.10.1795958413495; Sun, 29 Nov 2026 05:20:13 -0800 (PST) Received: from vlw-PC.. (static.18.103.245.188.clients.your-server.de. [188.245.103.18]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab658e2d1bsm60834833f8f.8.2026.11.29.05.20.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 05:20:13 -0800 (PST) From: Vladimir Bely To: Rob Clark , Dmitry Baryshkov Cc: Abhinav Kumar , Jessica Zhang , Sean Paul , Marijn Suijten , David Airlie , Simona Vetter , Kavan Smith , linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org, freedreno@lists.freedesktop.org, linux-kernel@vger.kernel.org, Vladimir Bely , stable@vger.kernel.org Subject: [PATCH v4] drm/msm/dsi: phy_28nm: return exact PLL-achievable VCO rate in determine_rate Date: Sun, 29 Nov 2026 16:20:10 +0300 Message-ID: <20261129132010.639999-1-vlwwwwww@gmail.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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