Commit e2e08970 authored by Nikolaus Voss's avatar Nikolaus Voss Committed by Thierry Reding

pwm: atmel: 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.
Signed-off-by: default avatarNikolaus Voss <n.voss@weinmann-emt.de>
Tested-by: default avatarBo Shen <voice.shen@atmel.com>
Acked-by: default avatarBo Shen <voice.shen@atmel.com>
Signed-off-by: default avatarThierry Reding <thierry.reding@gmail.com>
parent 70145f87
...@@ -102,7 +102,7 @@ static int atmel_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, ...@@ -102,7 +102,7 @@ static int atmel_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
int duty_ns, int period_ns) int duty_ns, int period_ns)
{ {
struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip); 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 long long div;
unsigned int pres = 0; unsigned int pres = 0;
u32 val; u32 val;
...@@ -113,21 +113,19 @@ static int atmel_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, ...@@ -113,21 +113,19 @@ static int atmel_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
return -EBUSY; return -EBUSY;
} }
clk_rate = clk_get_rate(atmel_pwm->clk); /* Calculate the period cycles and prescale value */
div = clk_rate; div = (unsigned long long)clk_get_rate(atmel_pwm->clk) * period_ns;
do_div(div, NSEC_PER_SEC);
/* Calculate the period cycles */
while (div > PWM_MAX_PRD) { while (div > PWM_MAX_PRD) {
div = clk_rate / (1 << pres); div >>= 1;
div = div * period_ns; pres++;
/* 1/Hz = 100000000 ns */ }
do_div(div, 1000000000);
if (pres++ > PRD_MAX_PRES) { if (pres > PRD_MAX_PRES) {
dev_err(chip->dev, "pres exceeds the maximum value\n"); dev_err(chip->dev, "pres exceeds the maximum value\n");
return -EINVAL; return -EINVAL;
} }
}
/* Calculate the duty cycles */ /* Calculate the duty cycles */
prd = div; prd = div;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment