Skip to contentSkip to conversation

pwm: atmel-pwm: fix calculation of prescale value

PATCH In review Nikolaus Voss 2 participants 3 messages Sep 24, 2014 – Sep 25, 2014

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

PATCH 1/1 pwm: atmel-pwm: fix calculation of prescale value

The prescale value used for calculating the period was incremented afterwards, thus the resulting prescale value is by one too high. This resulted in a pwm frequency only half as high as requested.

This patch moves the 64 bit division out of the prescale loop to correct the above issue and make the calculation more efficient.

---
 drivers/pwm/pwm-atmel.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
drivers/pwm/pwm-atmel.c +11 -13
index 6e700a5..ff17b5d 100644
@@ -102,7 +102,7 @@ static int atmel_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 			    int duty_ns, int period_ns)
 {
 	struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
-	unsigned long clk_rate, prd, dty;
+	unsigned long prd, dty;
 	unsigned long long div;
 	unsigned int pres = 0;
 	u32 val;
@@ -113,20 +113,18 @@ static int atmel_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 		return -EBUSY;
 	}
 
-	clk_rate = clk_get_rate(atmel_pwm->clk);
-	div = clk_rate;
+	/* Calculate the period cycles and prescale value */
+	div = (unsigned long long)clk_get_rate(atmel_pwm->clk) * period_ns;
+	do_div(div, (int)1e9);
 
-	/* Calculate the period cycles */
 	while (div > PWM_MAX_PRD) {
-		div = clk_rate / (1 << pres);
-		div = div * period_ns;
-		/* 1/Hz = 100000000 ns */
-		do_div(div, 1000000000);
-
-		if (pres++ > PRD_MAX_PRES) {
-			dev_err(chip->dev, "pres exceeds the maximum value\n");
-			return -EINVAL;
-		}
+		div >>= 1;
+		++pres;
+	}
+
+	if (pres > PRD_MAX_PRES) {
+		dev_err(chip->dev, "pres exceeds the maximum value\n");
+		return -EINVAL;
 	}
 
 	/* Calculate the duty cycles */
Signature · 1 line
1.9.1
Tested-by Acked-by on v1 1/1
#1 lore raw

Hi Nikolaus Voss,

Quoted 6 lines · Nikolaus Voss

The prescale value used for calculating the period was incremented afterwards, thus the resulting prescale value is by one too high. This resulted in a pwm frequency only half as high as requested.

This patch moves the 64 bit division out of the prescale loop to correct the above issue and make the calculation more efficient.

Thanks for your patch.

Quoted 1 line
Quoted 39 lines
---
  drivers/pwm/pwm-atmel.c | 24 +++++++++++-------------
  1 file changed, 11 insertions(+), 13 deletions(-)
drivers/pwm/pwm-atmel.c +4 -13
index 6e700a5..ff17b5d 100644
@@ -102,7 +102,7 @@ static int atmel_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  			    int duty_ns, int period_ns)
  {
  	struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
-	unsigned long clk_rate, prd, dty;
+	unsigned long prd, dty;
  	unsigned long long div;
  	unsigned int pres = 0;
  	u32 val;
@@ -113,20 +113,18 @@ static int atmel_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  		return -EBUSY;
  	}
 
-	clk_rate = clk_get_rate(atmel_pwm->clk);
-	div = clk_rate;
+	/* Calculate the period cycles and prescale value */
+	div = (unsigned long long)clk_get_rate(atmel_pwm->clk) * period_ns;
+	do_div(div, (int)1e9);
 
-	/* Calculate the period cycles */
  	while (div > PWM_MAX_PRD) {
-		div = clk_rate / (1 << pres);
-		div = div * period_ns;
-		/* 1/Hz = 100000000 ns */
-		do_div(div, 1000000000);
-
-		if (pres++ > PRD_MAX_PRES) {
-			dev_err(chip->dev, "pres exceeds the maximum value\n");
-			return -EINVAL;
-		}

drivers/pwm/pwm-atmel.c static int atmel_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 130

Quoted 10 lines
drivers/pwm/pwm-atmel.c +7 -0
+		div >>= 1;
+		++pres;
+	}
+
+	if (pres > PRD_MAX_PRES) {
+		dev_err(chip->dev, "pres exceeds the maximum value\n");
+		return -EINVAL;
  	}
 
  	/* Calculate the duty cycles */

Best Regards,
Bo Shen

#2 lore raw

Hi,

Correct the PWM maintainer Thierry Reding's e-mail address.

and Add linux-arm-kernel ML.

Quoted 66 lines · Bo Shen

Hi Nikolaus Voss,

Quoted 6 lines · Nikolaus Voss

The prescale value used for calculating the period was incremented afterwards, thus the resulting prescale value is by one too high. This resulted in a pwm frequency only half as high as requested.

This patch moves the 64 bit division out of the prescale loop to correct the above issue and make the calculation more efficient.

Thanks for your patch.

Quoted 1 line
Quoted 49 lines
---
  drivers/pwm/pwm-atmel.c | 24 +++++++++++-------------
  1 file changed, 11 insertions(+), 13 deletions(-)
drivers/pwm/pwm-atmel.c +1 -1
index 6e700a5..ff17b5d 100644
@@ -102,7 +102,7 @@ static int atmel_pwm_config(struct pwm_chip *chip,
 struct pwm_device *pwm,
                  int duty_ns, int period_ns)
  {
      struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
-    unsigned long clk_rate, prd, dty;
+    unsigned long prd, dty;
      unsigned long long div;
      unsigned int pres = 0;
      u32 val;
drivers/pwm/pwm-atmel.c +10 -12
@@ -113,20 +113,18 @@ static int atmel_pwm_config(struct pwm_chip
 *chip, struct pwm_device *pwm,
          return -EBUSY;
      }
 
-    clk_rate = clk_get_rate(atmel_pwm->clk);
-    div = clk_rate;
+    /* Calculate the period cycles and prescale value */
+    div = (unsigned long long)clk_get_rate(atmel_pwm->clk) * period_ns;
+    do_div(div, (int)1e9);
 
-    /* Calculate the period cycles */
      while (div > PWM_MAX_PRD) {
-        div = clk_rate / (1 << pres);
-        div = div * period_ns;
-        /* 1/Hz = 100000000 ns */
-        do_div(div, 1000000000);
-
-        if (pres++ > PRD_MAX_PRES) {
-            dev_err(chip->dev, "pres exceeds the maximum value\n");
-            return -EINVAL;
-        }
+        div >>= 1;
+        ++pres;
+    }
+
+    if (pres > PRD_MAX_PRES) {
+        dev_err(chip->dev, "pres exceeds the maximum value\n");
+        return -EINVAL;
      }
 
Quoted 5 lines
Quoted 2 lines
      /* Calculate the duty cycles */

Best Regards,
Bo Shen

Best Regards,
Bo Shen