devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Geert Uytterhoeven <geert@linux-m68k.org>
To: Hoan Tran <hotran@apm.com>
Cc: Rob Herring <robh+dt@kernel.org>, Pawel Moll <pawel.moll@arm.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Michael Turquette <mturquette@baylibre.com>,
	Stephen Boyd <sboyd@codeaurora.org>,
	Ian Campbell <ijc+devicetree@hellion.org.uk>,
	Kumar Gala <galak@codeaurora.org>,
	"devicetree@vger.kernel.org" <devicetree@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	linux-clk <linux-clk@vger.kernel.org>,
	lho@apm.com, Duc Dang <dhdang@apm.com>
Subject: Re: [PATCH 2/2] clk: Add fractional scale clock support
Date: Fri, 17 Jun 2016 08:43:27 +0200	[thread overview]
Message-ID: <CAMuHMdUMm5BWWEmKX3xOBkMX7HWTYgD6R0xz=uggs_Uya8=3Gw@mail.gmail.com> (raw)
In-Reply-To: <1466120433-30648-3-git-send-email-hotran@apm.com>

On Fri, Jun 17, 2016 at 1:40 AM, Hoan Tran <hotran@apm.com> wrote:
> This patch adds fractional scale clock support.
> Fractional scale clock is implemented for a single register field.
>   Output rate = parent_rate * scale / denominator
> For example, for 1 / 8 fractional scale, denominator will be 8 and scale
> will be computed and programmed accordingly.
>
> Signed-off-by: Hoan Tran <hotran@apm.com>
> Signed-off-by: Loc Ho <lho@apm.com>

> --- /dev/null
> +++ b/drivers/clk/clk-fractional-scale.c

> +static long clk_fs_round_rate(struct clk_hw *hw, unsigned long rate,
> +                             unsigned long *parent_rate)
> +{
> +       struct clk_fractional_scale *fd = to_clk_sf(hw);
> +       u64 ret, scale;
> +
> +       if (!rate || rate >= *parent_rate)
> +               return *parent_rate;
> +
> +       /* freq = parent_rate * scaler / denom */
> +       ret = rate * fd->denom;

Can this overflow? No, because fd->denom is u64.
But if fd->denom is changed to u32, it needs a cast to u64 here.

> +       scale = ret / *parent_rate;

As detected by the kbuild test robot, this should use do_div().

> +
> +       ret = (u64) *parent_rate * scale;
> +       do_div(ret, fd->denom);
> +
> +       return ret;
> +}
> +
> +static int clk_fs_set_rate(struct clk_hw *hw, unsigned long rate,
> +                          unsigned long parent_rate)
> +{
> +       struct clk_fractional_scale *fd = to_clk_sf(hw);
> +       unsigned long flags = 0;
> +       u64 scale, ret;
> +       u32 val;
> +
> +       /*
> +        * Compute the scaler:
> +        *
> +        * freq = parent_rate * scaler / denom, or
> +        * scaler = freq * denom / parent_rate
> +        */
> +       ret = rate * fd->denom;

Can this overflow? No, because fd->denom is u64.
But if fd->denom is changed to u32, it needs a cast to u64 here.

> +       scale = ret / (u64)parent_rate;

Don't cast the divider to u64, as this will turn a 64-by-32 division into a
64-by-64 division.
As detected by the kbuild test robot, this should use do_div().

> --- a/include/linux/clk-provider.h
> +++ b/include/linux/clk-provider.h

> +struct clk_fractional_scale {
> +       struct clk_hw   hw;
> +       void __iomem    *reg;
> +       u8              shift;
> +       u32             mask;
> +       u64             denom;

u64 sounds overkill to me, but it looks like that was done to avoid overflow in
the multiplications?

> +       u32             flags;
> +       spinlock_t      *lock;
> +};

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

  parent reply	other threads:[~2016-06-17  6:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-16 23:40 [PATCH 0/2] clk: Add fractional scale clock support Hoan Tran
2016-06-16 23:40 ` [PATCH 1/2] Documentation: dt: clock: Add fractional scale binding Hoan Tran
2016-06-20 13:18   ` Rob Herring
2016-06-16 23:40 ` [PATCH 2/2] clk: Add fractional scale clock support Hoan Tran
2016-06-17  1:22   ` kbuild test robot
     [not found]   ` <1466120433-30648-3-git-send-email-hotran-qTEPVZfXA3Y@public.gmane.org>
2016-06-17  1:49     ` kbuild test robot
2016-06-17  1:55   ` kbuild test robot
2016-06-17  6:43   ` Geert Uytterhoeven [this message]
2016-06-17 16:30     ` Hoan Tran

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='CAMuHMdUMm5BWWEmKX3xOBkMX7HWTYgD6R0xz=uggs_Uya8=3Gw@mail.gmail.com' \
    --to=geert@linux-m68k.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dhdang@apm.com \
    --cc=galak@codeaurora.org \
    --cc=hotran@apm.com \
    --cc=ijc+devicetree@hellion.org.uk \
    --cc=lho@apm.com \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mturquette@baylibre.com \
    --cc=pawel.moll@arm.com \
    --cc=robh+dt@kernel.org \
    --cc=sboyd@codeaurora.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).