linux-amlogic.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: thierry.reding@gmail.com (Thierry Reding)
To: linus-amlogic@lists.infradead.org
Subject: [PATCH v2 3/3] pwm: meson: improve pwm calculation precision.
Date: Thu, 6 Jul 2017 23:21:57 +0200	[thread overview]
Message-ID: <20170706212154.GA28918@mithrandir> (raw)
In-Reply-To: <20170608122416.1993-4-jbrunet@baylibre.com>

On Thu, Jun 08, 2017 at 02:24:16PM +0200, Jerome Brunet wrote:
> When using input clocks with high rates, such as clk81 (166MHz), the
> fin_ns = NSEC_PER_SEC / fin_freq can introduce a significant error.
> 
> Ex: fin_freq = 166666667, NSEC_PER_SEC = 1000000000
>     fin_ns = 5,9999999
> 
> which is, of course, rounded down to 5. This introduce an error of ~20%
> on the period requested from the pwm.
> 
> This patch use ps instead of ns (and 64bits integer) to perform the
> calculation. This should give a good enough precision.
> 
> Fixes: 211ed630753d ("pwm: Add support for Meson PWM Controller")
> Signed-off-by: Jerome Brunet <jbrunet@baylibre.com>
> ---
>  drivers/pwm/pwm-meson.c | 15 +++++++++------
>  1 file changed, 9 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c
> index b911a944744a..4cdc66f7f718 100644
> --- a/drivers/pwm/pwm-meson.c
> +++ b/drivers/pwm/pwm-meson.c
> @@ -163,7 +163,8 @@ static int meson_pwm_calc(struct meson_pwm *meson,
>  			  unsigned int duty, unsigned int period)
>  {
>  	unsigned int pre_div, cnt, duty_cnt;
> -	unsigned long fin_freq = -1, fin_ns;
> +	unsigned long fin_freq = -1;
> +	u64 fin_ps;
>  
>  	if (~(meson->inverter_mask >> id) & 0x1)
>  		duty = period - duty;
> @@ -179,13 +180,14 @@ static int meson_pwm_calc(struct meson_pwm *meson,
>  	}
>  
>  	dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq);
> -	fin_ns = NSEC_PER_SEC / fin_freq;
> +	fin_ps = ((u64)NSEC_PER_SEC * 1000) / fin_freq;

This failed to build, so I had to change this division to a do_div().

Thierry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-amlogic/attachments/20170706/ec75893f/attachment.sig>

  parent reply	other threads:[~2017-07-06 21:21 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-08 12:24 [PATCH v2 0/3] pwm: add pwm AO on meson gx Jerome Brunet
2017-06-08 12:24 ` [PATCH v2 1/3] dt-bindings: pwm: meson: add comptabible for gxbb ao pwms Jerome Brunet
2017-06-13 22:18   ` Rob Herring
2017-06-08 12:24 ` [PATCH v2 2/3] pwm: meson: add compatible for the " Jerome Brunet
2017-06-14 17:38   ` Kevin Hilman
2017-06-08 12:24 ` [PATCH v2 3/3] pwm: meson: improve pwm calculation precision Jerome Brunet
2017-06-08 12:35   ` Neil Armstrong
2017-07-06 21:21   ` Thierry Reding [this message]
2017-07-07  8:57     ` Jerome Brunet
2017-07-06  6:59 ` [PATCH v2 0/3] pwm: add pwm AO on meson gx Thierry Reding
2017-07-06  9:02   ` Jerome Brunet
2017-07-06  9:17     ` Thierry Reding
2017-07-14  5:51       ` Jeremy Kerr
2017-07-18 14:52         ` 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=20170706212154.GA28918@mithrandir \
    --to=thierry.reding@gmail.com \
    --cc=linus-amlogic@lists.infradead.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).