All of lore.kernel.org
 help / color / mirror / Atom feed
From: Geert Uytterhoeven <geert@linux-m68k.org>
To: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
Cc: Biju Das <biju.das.jz@bp.renesas.com>,
	Thierry Reding <thierry.reding@gmail.com>,
	 Philipp Zabel <p.zabel@pengutronix.de>,
	Fabrizio Castro <fabrizio.castro.jz@renesas.com>,
	 Magnus Damm <magnus.damm@gmail.com>,
	linux-pwm@vger.kernel.org,  linux-renesas-soc@vger.kernel.org,
	 Prabhakar Mahadev Lad <prabhakar.mahadev-lad.rj@bp.renesas.com>,
	Biju Das <biju.das.au@gmail.com>,
	 kernel@pengutronix.de
Subject: Re: [PATCH v18 3/4] pwm: Add support for RZ/G2L GPT
Date: Tue, 12 Mar 2024 09:11:42 +0100	[thread overview]
Message-ID: <CAMuHMdV8SnMgawrMspemJNfsAHW-wSboXeEOgZ6F37QqrmiLSA@mail.gmail.com> (raw)
In-Reply-To: <hy5crf2leuvewkn5omgrk2bmkndivwmhst4yrefnd3mepy4nzd@xw3rtkxdnb2g>

Hi Uwe,

On Tue, Mar 12, 2024 at 8:20 AM Uwe Kleine-König
<u.kleine-koenig@pengutronix.de> wrote:
> On Tue, Feb 20, 2024 at 07:43:17PM +0000, Biju Das wrote:
> > --- /dev/null
> > +++ b/drivers/pwm/pwm-rzg2l-gpt.c
> > +static u64 calculate_period_or_duty(struct rzg2l_gpt_chip *rzg2l_gpt, u32 val, u8 prescale)
> > +{
> > +     u64 tmp, d;
> > +
> > +     /*
> > +      * Rate is in MHz and is always integer for peripheral clk
> > +      * 2^32 * 2^10 (prescalar) * 10^9 > 2^64
> > +      * 2^32 * 2^10 (prescalar) * 10^6 < 2^64
> > +      * Multiply val with prescalar first, if the result is less than
> > +      * 2^34, then multiply by 10^9. Otherwise divide nr and dr by 10^3
> > +      * so that it will never overflow.
> > +      */
> > +
> > +     tmp = (u64)val << (2 * prescale);
> > +     if (tmp <= (1ULL << 34)) {
>
> I would have written that as:
>
>         if (tmp >> 34 == 0)
>
> (which implements tmp < (1ULL << 34), which doesn't matter much).
>
> > +             tmp *= NSEC_PER_SEC;
> > +             d = rzg2l_gpt->rate;
> > +     } else {
> > +             tmp *= div64_u64(NSEC_PER_SEC, KILO);
>
> I don't know if the compiler is clever enough to not calculate that
> every time?

Not on 32-bit when written that way.

> Also using div64_u64 is too heavy given that both values fit
> into an u32.

Indeed, so "NSEC_PER_SEC / KILO" should be fine.
I guess NSEC_PER_MSEC would be too obfuscating?

>
> > +             d = div64_u64(rzg2l_gpt->rate, KILO);
>
> At first I thought you could better use 1024 as the common divisor here
> as it could be implemented using a shift operation. But I understood
> with the comment above that we're not losing precision here as both
> NSEC_PER_SEC and rate are a multiple of 1000.
>
> Maybe s/Rate is in MHz and is always integer for peripheral clk/Rate is
> a multiple of 1000000, and so dividing by 1000 is an exact operation./ ?
>
> > +     }
> > +
> > +     return DIV64_U64_ROUND_UP(tmp, d);
> > +}

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

  reply	other threads:[~2024-03-12  8:11 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-20 19:43 [PATCH v18 0/4] Add support for RZ/G2L GPT Biju Das
2024-02-20 19:43 ` [PATCH v18 1/4] dt-bindings: pwm: Add RZ/G2L GPT binding Biju Das
2024-02-20 19:43 ` [PATCH v18 2/4] dt-bindings: pwm: rzg2l-gpt: Document renesas,poegs property Biju Das
2024-02-20 19:43 ` [PATCH v18 3/4] pwm: Add support for RZ/G2L GPT Biju Das
2024-03-12  7:20   ` Uwe Kleine-König
2024-03-12  8:11     ` Geert Uytterhoeven [this message]
2024-03-12  9:27       ` Uwe Kleine-König
2024-03-14 18:10     ` Biju Das
2024-03-14 22:33       ` Uwe Kleine-König
2024-03-15  7:19         ` Biju Das
2024-02-20 19:43 ` [PATCH v18 4/4] pwm: rzg2l-gpt: Add support for gpt linking with poeg Biju Das

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=CAMuHMdV8SnMgawrMspemJNfsAHW-wSboXeEOgZ6F37QqrmiLSA@mail.gmail.com \
    --to=geert@linux-m68k.org \
    --cc=biju.das.au@gmail.com \
    --cc=biju.das.jz@bp.renesas.com \
    --cc=fabrizio.castro.jz@renesas.com \
    --cc=kernel@pengutronix.de \
    --cc=linux-pwm@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=magnus.damm@gmail.com \
    --cc=p.zabel@pengutronix.de \
    --cc=prabhakar.mahadev-lad.rj@bp.renesas.com \
    --cc=thierry.reding@gmail.com \
    --cc=u.kleine-koenig@pengutronix.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.