Skip to contentSkip to conversation

pwm: atmel-pwm: fix calculation of prescale value

PATCH Thierry Reding 2 participants 3 messages Sep 25, 2014 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 11 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.

---
 drivers/pwm/pwm-atmel.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

Hi Nikolaus,

Please Cc the [email protected] mailing list for PWM-related patches in the future.

Also a couple more comments:

In the patch description: "pwm frequency" should be "PWM frequency".

Quoted 14 lines
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 static int atmel_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 118

Quoted 8 lines
drivers/pwm/pwm-atmel.c +3 -2
@@ -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);

1e9 should be NSEC_PER_SEC.

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

Quoted 13 lines
drivers/pwm/pwm-atmel.c +2 -10
-	/* 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;

Unless you really need the prefix increment behaviour (you don't in this case) I prefer using the postfix operator because it is slightly more idiomatic.

No need for you to respin the patch, I've fixed up the above when applying.

Thierry

#2 lore raw

Hi Thierry,

Quoted 2 lines · Thierry Reding

Please Cc the [email protected] mailing list for PWM-related patches in the future.

ok, I ran get_maintainer.pl on an old kernel...

Quoted 3 lines

Also a couple more comments:

In the patch description: "pwm frequency" should be "PWM frequency".

ok.

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	div = (unsigned long long)clk_get_rate(atmel_pwm->clk) * period_ns;
+	do_div(div, (int)1e9);

1e9 should be NSEC_PER_SEC.

Quoted 7 lines

Review

Quoted 3 lines
(quoted diff) +2 -1
-		}
+		div >>= 1;
+		++pres;

Unless you really need the prefix increment behaviour (you don't in this case) I prefer using the postfix operator because it is slightly more idiomatic.

ok (as I mostly do C++ programming, I prefer the prefix as it is usually more efficient for user-defined types).

Quoted 2 lines

No need for you to respin the patch, I've fixed up the above when applying.

Thanks!

Niko

#3 lore raw
Quoted 1 line · Nikolaus Voss

On Thu, 25 Sep 2014, Thierry Reding wrote:

[...]

Quoted 10 lines
Quoted 7 lines

Review

Quoted 3 lines
(quoted diff) +2 -1
-		}
+		div >>= 1;
+		++pres;

Unless you really need the prefix increment behaviour (you don't in this case) I prefer using the postfix operator because it is slightly more idiomatic.

ok (as I mostly do C++ programming, I prefer the prefix as it is usually more efficient for user-defined types).

It is? That's intriguing...

Thierry