Skip to content

Commit 671f159

Browse files
ukleineknunojsa
authored andcommitted
pwm: axi-pwmgen: Check for overflows in the register value calculation
For big periods the corresponding number of clock ticks might be bigger than U32_MAX. Clamp to U32_MAX instead of just casting to u32 which just discards the upper half of the unsigned long long variable and so might introduce a big error. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
1 parent be0d2e7 commit 671f159

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

drivers/pwm/pwm-axi-pwmgen.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,21 @@ static int axi_pwmgen_apply(struct pwm_chip *chip, struct pwm_device *pwm,
116116

117117
cnt = mul_u64_u64_div_u64_roundclosest(state->period * axi_pwmgen_scales[state->time_unit],
118118
rate, PSEC_PER_SEC);
119+
if (cnt > U32_MAX)
120+
cnt = U32_MAX;
119121
axi_pwmgen_write(pwmgen, AXI_PWMGEN_CHX_PERIOD(pwmgen, ch),
120122
state->enabled ? cnt : 0);
121123

122124
cnt = mul_u64_u64_div_u64_roundclosest(state->duty_cycle * axi_pwmgen_scales[state->time_unit],
123125
rate, PSEC_PER_SEC);
126+
if (cnt > U32_MAX)
127+
cnt = U32_MAX;
124128
axi_pwmgen_write(pwmgen, AXI_PWMGEN_CHX_DUTY(pwmgen, ch), cnt);
125129

126130
cnt = mul_u64_u64_div_u64_roundclosest(state->phase * axi_pwmgen_scales[state->time_unit],
127131
rate, PSEC_PER_SEC);
132+
if (cnt > U32_MAX)
133+
cnt = U32_MAX;
128134
axi_pwmgen_write(pwmgen, AXI_PWMGEN_CHX_PHASE(pwmgen, ch), cnt);
129135

130136
/* Apply the new config */

0 commit comments

Comments
 (0)