Skip to content
/ linux Public

Commit 1e1664e

Browse files
taniyadas20Sasha Levin
authored andcommitted
clk: qcom: rcg2: compute 2d using duty fraction directly
[ Upstream commit d6205a1 ] The duty-cycle calculation in clk_rcg2_set_duty_cycle() currently derives an intermediate percentage `duty_per = (num * 100) / den` and then computes: d = DIV_ROUND_CLOSEST(n * duty_per * 2, 100); This introduces integer truncation at the percentage step (division by `den`) and a redundant scaling by 100, which can reduce precision for large `den` and skew the final rounding. Compute `2d` directly from the duty fraction to preserve precision and avoid the unnecessary scaling: d = DIV_ROUND_CLOSEST(n * duty->num * 2, duty->den); This keeps the intended formula `d ≈ n * 2 * (num/den)` while performing a single, final rounded division, improving accuracy especially for small duty cycles or large denominators. It also removes the unused `duty_per` variable, simplifying the code. There is no functional changes beyond improved numerical accuracy. Fixes: 7f891fa ("clk: qcom: clk-rcg2: Add support for duty-cycle for RCG") Signed-off-by: Taniya Das <taniya.das@oss.qualcomm.com> Link: https://lore.kernel.org/r/20260105-duty_cycle_precision-v2-1-d1d466a6330a@oss.qualcomm.com Signed-off-by: Bjorn Andersson <andersson@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 8cb92d2 commit 1e1664e

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

drivers/clk/qcom/clk-rcg2.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ static int clk_rcg2_get_duty_cycle(struct clk_hw *hw, struct clk_duty *duty)
434434
static int clk_rcg2_set_duty_cycle(struct clk_hw *hw, struct clk_duty *duty)
435435
{
436436
struct clk_rcg2 *rcg = to_clk_rcg2(hw);
437-
u32 notn_m, n, m, d, not2d, mask, duty_per, cfg;
437+
u32 notn_m, n, m, d, not2d, mask, cfg;
438438
int ret;
439439

440440
/* Duty-cycle cannot be modified for non-MND RCGs */
@@ -453,10 +453,8 @@ static int clk_rcg2_set_duty_cycle(struct clk_hw *hw, struct clk_duty *duty)
453453

454454
n = (~(notn_m) + m) & mask;
455455

456-
duty_per = (duty->num * 100) / duty->den;
457-
458456
/* Calculate 2d value */
459-
d = DIV_ROUND_CLOSEST(n * duty_per * 2, 100);
457+
d = DIV_ROUND_CLOSEST(n * duty->num * 2, duty->den);
460458

461459
/*
462460
* Check bit widths of 2d. If D is too big reduce duty cycle.

0 commit comments

Comments
 (0)