Skip to contentSkip to conversation

pwm: Check for negative duty-cycle and period

PATCH In review Thierry Reding 2 participants 2 messages Oct 1, 2012 – Oct 4, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pwm: Check for negative duty-cycle and period

Make sure the duty-cycle and period passed in are not negative. This should eventually be made implicit by making them unsigned. While at it, the drivers' .config() implementations can have the equivalent checks removed.

Cc Shawn Guo <[email protected]>
Cc Mark Brown <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Sachin Kamat <[email protected]>
Cc Axel Lin <[email protected]>
Cc Kukjin Kim <[email protected]>
Cc Jingoo Han <[email protected]>
Cc Jonghwan Choi <[email protected]>
Cc Sascha Hauer <[email protected]>
Cc "Philip, Avinash" <[email protected]>
Cc Vaibhav Bedia <[email protected]>
---
 drivers/pwm/core.c         | 2 +-
 drivers/pwm/pwm-bfin.c     | 3 ---
 drivers/pwm/pwm-pxa.c      | 3 ---
 drivers/pwm/pwm-samsung.c  | 3 ---
 drivers/pwm/pwm-tiecap.c   | 2 +-
 drivers/pwm/pwm-tiehrpwm.c | 2 +-
 6 files changed, 3 insertions(+), 12 deletions(-)
drivers/pwm/core.c +1 -1
index 92b1782..f5acdaa 100644
@@ -371,7 +371,7 @@ EXPORT_SYMBOL_GPL(pwm_free);
  */
 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
 {
-	if (!pwm || period_ns == 0 || duty_ns > period_ns)
+	if (!pwm || duty_ns < 0 || period_ns <= 0 || duty_ns > period_ns)
 		return -EINVAL;
 
 	return pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns);
drivers/pwm/pwm-bfin.c +0 -3
index d53c4e7..5da8e18 100644
@@ -69,9 +69,6 @@ static int bfin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	unsigned long period, duty;
 	unsigned long long val;
 
-	if (duty_ns < 0 || duty_ns > period_ns)
-		return -EINVAL;
-
 	val = (unsigned long long)get_sclk() * period_ns;
 	do_div(val, NSEC_PER_SEC);
 	period = val;
drivers/pwm/pwm-pxa.c +0 -3
index bd5867a..260c3a8 100644
@@ -70,9 +70,6 @@ static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	unsigned long offset;
 	int rc;
 
-	if (period_ns == 0 || duty_ns > period_ns)
-		return -EINVAL;
-
 	offset = pwm->hwpwm ? 0x10 : 0;
 
 	c = clk_get_rate(pc->clk);
drivers/pwm/pwm-samsung.c +0 -3
index e5187c0..023a3be 100644
@@ -126,9 +126,6 @@ static int s3c_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
 		return -ERANGE;
 
-	if (duty_ns > period_ns)
-		return -EINVAL;
-
 	if (period_ns == s3c->period_ns &&
 	    duty_ns == s3c->duty_ns)
 		return 0;
drivers/pwm/pwm-tiecap.c +1 -1
index 081471f..d6d4cf0 100644
@@ -60,7 +60,7 @@ static int ecap_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	unsigned long period_cycles, duty_cycles;
 	unsigned int reg_val;
 
-	if (period_ns < 0 || duty_ns < 0 || period_ns > NSEC_PER_SEC)
+	if (period_ns > NSEC_PER_SEC)
 		return -ERANGE;
 
 	c = pc->clk_rate;
drivers/pwm/pwm-tiehrpwm.c +1 -1
index caf00fe..d3c1dff 100644
@@ -221,7 +221,7 @@ static int ehrpwm_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	unsigned short ps_divval, tb_divval;
 	int i, cmp_reg;
 
-	if (period_ns < 0 || duty_ns < 0 || period_ns > NSEC_PER_SEC)
+	if (period_ns > NSEC_PER_SEC)
 		return -ERANGE;
 
 	c = pc->clk_rate;
Signature · 1 line
1.7.12.2
Acked-by on v1 1/1
#1 lore raw

On Monday, October 01, 2012 3:35 PM Thierry Reding wrote

Quoted 6 lines

Make sure the duty-cycle and period passed in are not negative. This should eventually be made implicit by making them unsigned. While at it, the drivers' .config() implementations can have the equivalent checks removed.

Quoted 8 lines
Cc Shawn Guo <[email protected]>
Cc Mark Brown <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Sachin Kamat <[email protected]>
Cc Axel Lin <[email protected]>
Cc Kukjin Kim <[email protected]>
Cc Jingoo Han <[email protected]>
Quoted 96 lines
Cc Jonghwan Choi <[email protected]>
Cc Sascha Hauer <[email protected]>
Cc "Philip, Avinash" <[email protected]>
Cc Vaibhav Bedia <[email protected]>
---
 drivers/pwm/core.c         | 2 +-
 drivers/pwm/pwm-bfin.c     | 3 ---
 drivers/pwm/pwm-pxa.c      | 3 ---
 drivers/pwm/pwm-samsung.c  | 3 ---
 drivers/pwm/pwm-tiecap.c   | 2 +-
 drivers/pwm/pwm-tiehrpwm.c | 2 +-
 6 files changed, 3 insertions(+), 12 deletions(-)
drivers/pwm/core.c +1 -1
index 92b1782..f5acdaa 100644
@@ -371,7 +371,7 @@ EXPORT_SYMBOL_GPL(pwm_free);
  */
 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
 {
-	if (!pwm || period_ns == 0 || duty_ns > period_ns)
+	if (!pwm || duty_ns < 0 || period_ns <= 0 || duty_ns > period_ns)
 		return -EINVAL;
 
 	return pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns);
drivers/pwm/pwm-bfin.c +0 -3
index d53c4e7..5da8e18 100644
@@ -69,9 +69,6 @@ static int bfin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	unsigned long period, duty;
 	unsigned long long val;
 
-	if (duty_ns < 0 || duty_ns > period_ns)
-		return -EINVAL;
-
 	val = (unsigned long long)get_sclk() * period_ns;
 	do_div(val, NSEC_PER_SEC);
 	period = val;
drivers/pwm/pwm-pxa.c +0 -3
index bd5867a..260c3a8 100644
@@ -70,9 +70,6 @@ static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	unsigned long offset;
 	int rc;
 
-	if (period_ns == 0 || duty_ns > period_ns)
-		return -EINVAL;
-
 	offset = pwm->hwpwm ? 0x10 : 0;
 
 	c = clk_get_rate(pc->clk);
drivers/pwm/pwm-samsung.c +0 -3
index e5187c0..023a3be 100644
@@ -126,9 +126,6 @@ static int s3c_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
 		return -ERANGE;
 
-	if (duty_ns > period_ns)
-		return -EINVAL;
-
 	if (period_ns == s3c->period_ns &&
 	    duty_ns == s3c->duty_ns)
 		return 0;
drivers/pwm/pwm-tiecap.c +1 -1
index 081471f..d6d4cf0 100644
@@ -60,7 +60,7 @@ static int ecap_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	unsigned long period_cycles, duty_cycles;
 	unsigned int reg_val;
 
-	if (period_ns < 0 || duty_ns < 0 || period_ns > NSEC_PER_SEC)
+	if (period_ns > NSEC_PER_SEC)
 		return -ERANGE;
 
 	c = pc->clk_rate;
drivers/pwm/pwm-tiehrpwm.c +1 -2
index caf00fe..d3c1dff 100644
@@ -221,7 +221,7 @@ static int ehrpwm_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	unsigned short ps_divval, tb_divval;
 	int i, cmp_reg;
 
-	if (period_ns < 0 || duty_ns < 0 || period_ns > NSEC_PER_SEC)
+	if (period_ns > NSEC_PER_SEC)
 		return -ERANGE;
 
 	c = pc->clk_rate;
--

1.7.12.2