All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bo Shen <voice.shen@atmel.com>
To: Nikolaus Voss <n.voss@weinmann-emt.de>,
	Nicolas Ferre <nicolas.ferre@atmel.com>
Cc: Thierry Reding <thierry.reding@avionic-design.de>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] pwm: atmel-pwm: fix calculation of prescale value
Date: Thu, 25 Sep 2014 09:50:11 +0800	[thread overview]
Message-ID: <542374D3.3080308@atmel.com> (raw)
In-Reply-To: <20140924055138.477E540656@mail.steuer-voss.de>

Hi Nikolaus Voss,

On 09/23/2014 09:30 PM, Nikolaus Voss wrote:
> 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.

> Signed-off-by: Nikolaus Voss <n.voss@weinmann-emt.de>

Tested-by: Bo Shen <voice.shen@atmel.com>
Acked-by: Bo Shen <voice.shen@atmel.com>

> ---
>   drivers/pwm/pwm-atmel.c | 24 +++++++++++-------------
>   1 file changed, 11 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/pwm/pwm-atmel.c b/drivers/pwm/pwm-atmel.c
> index 6e700a5..ff17b5d 100644
> --- a/drivers/pwm/pwm-atmel.c
> +++ b/drivers/pwm/pwm-atmel.c
> @@ -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 */
>

Best Regards,
Bo Shen

  reply	other threads:[~2014-09-25  1:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-23 13:30 [PATCH] pwm: atmel-pwm: fix calculation of prescale value Nikolaus Voss
2014-09-25  1:50 ` Bo Shen [this message]
2014-09-25  1:53   ` Bo Shen
2014-09-25  1:53     ` Bo Shen
     [not found] <20140924055851.CE51140656@mail.steuer-voss.de>
2014-09-25  6:52 ` Thierry Reding
2014-09-25  7:47   ` Nikolaus Voss
2014-09-25  7:49     ` 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=542374D3.3080308@atmel.com \
    --to=voice.shen@atmel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=n.voss@weinmann-emt.de \
    --cc=nicolas.ferre@atmel.com \
    --cc=thierry.reding@avionic-design.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.