Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1424723

Re: [PATCH 2/2] clk: Add fractional scale clock support

From Geert Uytterhoeven <geert@linux-m68k.org>
Newsgroups linux.kernel
Subject Re: [PATCH 2/2] clk: Add fractional scale clock support
Date 2016-06-17 08:50 +0200
Message-ID <rKVLj-33B-5@gated-at.bofh.it> (permalink)
References <rKPcR-6J3-5@gated-at.bofh.it> <rKPcS-6J3-21@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


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

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 0/2] clk: Add fractional scale clock support Hoan Tran <hotran@apm.com> - 2016-06-17 01:50 +0200
  [PATCH 1/2] Documentation: dt: clock: Add fractional scale binding Hoan Tran <hotran@apm.com> - 2016-06-17 01:50 +0200
    Re: [PATCH 1/2] Documentation: dt: clock: Add fractional scale  binding Rob Herring <robh@kernel.org> - 2016-06-20 15:20 +0200
  [PATCH 2/2] clk: Add fractional scale clock support Hoan Tran <hotran@apm.com> - 2016-06-17 01:50 +0200
    Re: [PATCH 2/2] clk: Add fractional scale clock support kbuild test robot <lkp@intel.com> - 2016-06-17 03:30 +0200
    Re: [PATCH 2/2] clk: Add fractional scale clock support kbuild test robot <lkp@intel.com> - 2016-06-17 04:00 +0200
    Re: [PATCH 2/2] clk: Add fractional scale clock support kbuild test robot <lkp@intel.com> - 2016-06-17 04:00 +0200
    Re: [PATCH 2/2] clk: Add fractional scale clock support Geert Uytterhoeven <geert@linux-m68k.org> - 2016-06-17 08:50 +0200
      Re: [PATCH 2/2] clk: Add fractional scale clock support Hoan Tran <hotran@apm.com> - 2016-06-17 18:40 +0200

csiph-web