Commit b979ed53 authored by Hyong Bin Kim's avatar Hyong Bin Kim Committed by Thierry Reding

pwm: tegra: Avoid overflow when calculating duty cycle

duty_ns * (1 << PWM_DUTY_WIDTH) could overflow in integer calculation
when the PWM rate is low. Hence do all calculation on unsigned long long
to avoid overflow.
Signed-off-by: default avatarHyong Bin Kim <hyongbink@nvidia.com>
Signed-off-by: default avatarLaxman Dewangan <ldewangan@nvidia.com>
Signed-off-by: default avatarThierry Reding <thierry.reding@gmail.com>
parent e0ee1a75
...@@ -67,7 +67,7 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, ...@@ -67,7 +67,7 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
int duty_ns, int period_ns) int duty_ns, int period_ns)
{ {
struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip); struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
unsigned long long c; unsigned long long c = duty_ns;
unsigned long rate, hz; unsigned long rate, hz;
u32 val = 0; u32 val = 0;
int err; int err;
...@@ -77,7 +77,8 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, ...@@ -77,7 +77,8 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
* per (1 << PWM_DUTY_WIDTH) cycles and make sure to round to the * per (1 << PWM_DUTY_WIDTH) cycles and make sure to round to the
* nearest integer during division. * nearest integer during division.
*/ */
c = duty_ns * (1 << PWM_DUTY_WIDTH) + period_ns / 2; c *= (1 << PWM_DUTY_WIDTH);
c += period_ns / 2;
do_div(c, period_ns); do_div(c, period_ns);
val = (u32)c << PWM_DUTY_SHIFT; val = (u32)c << PWM_DUTY_SHIFT;
......
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