Skip to content

Commit 331e049

Browse files
mediatek-jitaothierryreding
authored andcommitted
pwm: mtk-disp: Fix overflow in period and duty calculation
Current calculation for period and high_width may have 64-bit overflow. state->period and rate are u64. rate * state->period will overflow. clk_div = div_u64(rate * state->period, NSEC_PER_SEC) period = div64_u64(rate * state->period, div); high_width = div64_u64(rate * state->duty_cycle, div); This patch is to resolve it by using mul_u64_u64_div_u64(). Signed-off-by: Jitao Shi <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent 888a623 commit 331e049

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

drivers/pwm/pwm-mtk-disp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static int mtk_disp_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
119119
* high_width = (PWM_CLK_RATE * duty_ns) / (10^9 * (clk_div + 1))
120120
*/
121121
rate = clk_get_rate(mdp->clk_main);
122-
clk_div = div_u64(rate * state->period, NSEC_PER_SEC) >>
122+
clk_div = mul_u64_u64_div_u64(state->period, rate, NSEC_PER_SEC) >>
123123
PWM_PERIOD_BIT_WIDTH;
124124
if (clk_div > PWM_CLKDIV_MAX) {
125125
if (!mdp->enabled) {
@@ -130,11 +130,11 @@ static int mtk_disp_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
130130
}
131131

132132
div = NSEC_PER_SEC * (clk_div + 1);
133-
period = div64_u64(rate * state->period, div);
133+
period = mul_u64_u64_div_u64(state->period, rate, div);
134134
if (period > 0)
135135
period--;
136136

137-
high_width = div64_u64(rate * state->duty_cycle, div);
137+
high_width = mul_u64_u64_div_u64(state->duty_cycle, rate, div);
138138
value = period | (high_width << PWM_HIGH_WIDTH_SHIFT);
139139

140140
mtk_disp_pwm_update_bits(mdp, mdp->data->con0,

0 commit comments

Comments
 (0)