Commit 32c44e1f authored by George Stark's avatar George Stark Committed by Uwe Kleine-König

pwm: meson: Use mul_u64_u64_div_u64() for frequency calculating

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: 329db102 ("pwm: meson: make full use of common clock framework")
Suggested-by: default avatarUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: default avatarGeorge Stark <gnstark@salutedevices.com>
Link: https://lore.kernel.org/r/20240425171253.2752877-4-gnstark@salutedevices.comSigned-off-by: default avatarUwe Kleine-König <u.kleine-koenig@pengutronix.de>
parent 3e551115
...@@ -176,7 +176,7 @@ static int meson_pwm_calc(struct pwm_chip *chip, struct pwm_device *pwm, ...@@ -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); 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) { if (cnt > 0xffff) {
dev_err(pwmchip_parent(chip), "unable to get period cnt\n"); dev_err(pwmchip_parent(chip), "unable to get period cnt\n");
return -EINVAL; return -EINVAL;
...@@ -191,7 +191,7 @@ static int meson_pwm_calc(struct pwm_chip *chip, struct pwm_device *pwm, ...@@ -191,7 +191,7 @@ static int meson_pwm_calc(struct pwm_chip *chip, struct pwm_device *pwm,
channel->hi = 0; channel->hi = 0;
channel->lo = cnt; channel->lo = cnt;
} else { } 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); dev_dbg(pwmchip_parent(chip), "duty=%llu duty_cnt=%u\n", duty, duty_cnt);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment