Skip to content

Commit

Permalink
pwm: meson: Use mul_u64_u64_div_u64() for frequency calculating
Browse files Browse the repository at this point in the history
While calculating frequency for the given period u64 numbers are
multiplied before division what can lead to overflow in theory so use
secure mul_u64_u64_div_u64() which handles overflow correctly.

Fixes: 329db10 ("pwm: meson: make full use of common clock framework")
Suggested-by: Uwe Kleine-König <[email protected]>
Signed-off-by: George Stark <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Uwe Kleine-König <[email protected]>
  • Loading branch information
geo-stark authored and Uwe Kleine-König committed Apr 30, 2024
1 parent 3e55111 commit 32c44e1
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions drivers/pwm/pwm-meson.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ static int meson_pwm_calc(struct pwm_chip *chip, struct pwm_device *pwm,

dev_dbg(pwmchip_parent(chip), "fin_freq: %ld Hz\n", fin_freq);

cnt = div_u64(fin_freq * period, NSEC_PER_SEC);
cnt = mul_u64_u64_div_u64(fin_freq, period, NSEC_PER_SEC);
if (cnt > 0xffff) {
dev_err(pwmchip_parent(chip), "unable to get period cnt\n");
return -EINVAL;
Expand All @@ -191,7 +191,7 @@ static int meson_pwm_calc(struct pwm_chip *chip, struct pwm_device *pwm,
channel->hi = 0;
channel->lo = cnt;
} else {
duty_cnt = div_u64(fin_freq * duty, NSEC_PER_SEC);
duty_cnt = mul_u64_u64_div_u64(fin_freq, duty, NSEC_PER_SEC);

dev_dbg(pwmchip_parent(chip), "duty=%llu duty_cnt=%u\n", duty, duty_cnt);

Expand Down

0 comments on commit 32c44e1

Please sign in to comment.