devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Laxman Dewangan <ldewangan@nvidia.com>
To: thierry.reding@gmail.com, robh+dt@kernel.org,
	swarren@wwwdotorg.org, gnurou@gmail.com
Cc: linux-pwm@vger.kernel.org, devicetree@vger.kernel.org,
	linux-tegra@vger.kernel.org, linux-kernel@vger.kernel.org,
	Hyong Bin Kim <hyongbink@nvidia.com>,
	Laxman Dewangan <ldewangan@nvidia.com>
Subject: [PATCH 3/5] pwm: tegra: fix overflow when calculating duty cycle
Date: Wed, 22 Jun 2016 17:17:21 +0530	[thread overview]
Message-ID: <1466596043-27262-4-git-send-email-ldewangan@nvidia.com> (raw)
In-Reply-To: <1466596043-27262-1-git-send-email-ldewangan@nvidia.com>

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

  parent reply	other threads:[~2016-06-22 11:47 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-22 11:47 [PATCH 0/5] pwm: Fixes and support for Tegra186 Laxman Dewangan
2016-06-22 11:47 ` [PATCH 1/5] pwm: tegra: Add support for reset control Laxman Dewangan
2016-06-22 12:40   ` Thierry Reding
2016-06-22 11:47 ` [PATCH 2/5] pwm: tegra: Allow 100% duty cycle Laxman Dewangan
2016-06-22 11:47 ` Laxman Dewangan [this message]
2016-06-22 11:47 ` [PATCH 4/5] pwm: tegra: Add DT node compatible for Tegra186 Laxman Dewangan
2016-06-22 12:46   ` Thierry Reding
     [not found]     ` <20160622124614.GG26943-EkSeR96xj6Pcmrwk2tT4+A@public.gmane.org>
2016-06-24 16:10       ` Rob Herring
2016-06-24 21:30         ` Stephen Warren
     [not found] ` <1466596043-27262-1-git-send-email-ldewangan-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2016-06-22 11:47   ` [PATCH 5/5] pwm: tegra: Add support " Laxman Dewangan
     [not found]     ` <1466596043-27262-6-git-send-email-ldewangan-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2016-06-30  9:15       ` Alexandre Courbot
2016-07-11  9:31 ` [PATCH 0/5] pwm: Fixes and " Thierry Reding

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1466596043-27262-4-git-send-email-ldewangan@nvidia.com \
    --to=ldewangan@nvidia.com \
    --cc=devicetree@vger.kernel.org \
    --cc=gnurou@gmail.com \
    --cc=hyongbink@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=linux-tegra@vger.kernel.org \
    --cc=robh+dt@kernel.org \
    --cc=swarren@wwwdotorg.org \
    --cc=thierry.reding@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).