Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1428730
| From | Laxman Dewangan <ldewangan@nvidia.com> |
|---|---|
| Newsgroups | linux.kernel |
| Subject | [PATCH 3/5] pwm: tegra: fix overflow when calculating duty cycle |
| Date | 2016-06-22 14:10 +0200 |
| Message-ID | <rMP8K-3bX-29@gated-at.bofh.it> (permalink) |
| References | <rMP8J-3bX-9@gated-at.bofh.it> |
| Organization | linux.* mail to news gateway |
From: Hyong Bin Kim <hyongbink@nvidia.com>
duty_ns * (1 << PWM_DUTY_WIDTH) could overflow in integer calcualtion
when PWM rate is low. Hence do all calculation on unsigned long long
to avoid overflow.
Signed-off-by: Hyong Bin Kim <hyongbink@nvidia.com>
Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
---
drivers/pwm/pwm-tegra.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/drivers/pwm/pwm-tegra.c b/drivers/pwm/pwm-tegra.c
index 575ca8e..49cefd5 100644
--- a/drivers/pwm/pwm-tegra.c
+++ b/drivers/pwm/pwm-tegra.c
@@ -69,7 +69,7 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
int duty_ns, int period_ns)
{
struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
- unsigned long long c;
+ unsigned long long c = duty_ns;
unsigned long rate, hz;
u32 val = 0;
int err;
@@ -79,7 +79,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
* 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);
val = (u32)c << PWM_DUTY_SHIFT;
--
2.1.4
Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
[PATCH 0/5] pwm: Fixes and support for Tegra186 Laxman Dewangan <ldewangan@nvidia.com> - 2016-06-22 14:10 +0200
[PATCH 3/5] pwm: tegra: fix overflow when calculating duty cycle Laxman Dewangan <ldewangan@nvidia.com> - 2016-06-22 14:10 +0200
[PATCH 2/5] pwm: tegra: Allow 100% duty cycle Laxman Dewangan <ldewangan@nvidia.com> - 2016-06-22 14:10 +0200
[PATCH 4/5] pwm: tegra: Add DT node compatible for Tegra186 Laxman Dewangan <ldewangan@nvidia.com> - 2016-06-22 14:10 +0200
Re: [PATCH 4/5] pwm: tegra: Add DT node compatible for Tegra186 Thierry Reding <thierry.reding@gmail.com> - 2016-06-22 14:50 +0200
[PATCH 5/5] pwm: tegra: Add support for Tegra186 Laxman Dewangan <ldewangan@nvidia.com> - 2016-06-22 14:10 +0200
csiph-web