Skip to contentSkip to conversation

clk: Add fractional scale clock support

PATCH Superseded Hoan Tran 4 participants 9 messages Jun 16, 2016 – Jun 20, 2016

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hoan Tran

author
#0 lore raw

cover clk: Add fractional scale clock support

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.

Hoan Tran (2):
Documentation: dt: clock: Add fractional scale binding
clk: Add fractional scale clock support

 .../bindings/clock/fractional-scale-clock.txt      |  31 +++
 drivers/clk/Makefile                               |   1 +
 drivers/clk/clk-fractional-scale.c                 | 253 +++++++++++++++++++++
 include/linux/clk-provider.h                       |  41 ++++
 4 files changed, 326 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/fractional-scale-clock.txt
 create mode 100644 drivers/clk/clk-fractional-scale.c
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH 1/2 Documentation: dt: clock: Add fractional scale binding

Add fractional scale clock DTS binding.

---
 .../bindings/clock/fractional-scale-clock.txt      | 31 ++++++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/fractional-scale-clock.txt
Documentation/devicetree/bindings/clock/fractional-scale-clock.txt new file +31 -0
new file mode 100644
index 0000000..864a77ea
@@ -0,0 +1,31 @@
+Binding for fractional scale clock source.
+
+This binding uses the common clock binding[1].
+
+[1] Documentation/devicetree/bindings/clock/clock-bindings.txt
+
+Required properties:
+- compatible : shall be "fractional-scale-clock".
+- clocks : shall be the input parent clock phandle for the clock.
+- #clock-cells : shall be set to 1.
+- reg : shall be the physical register address for the clock.
+- clock-output-names : shall be the name of the clock referenced by derive clock.
+- clock-shift: Bit shift of the clock register. Default is 0.
+- clock-width: Width of the clock register. Default is 32.
+- clock-denom: shall be the denominator value. Default is 2^32.
+
+Optional properties:
+- clock-inverted: shall be 1 if the numerator is inverted.
+- clock-names : shall be the name of the clock. If missing, use the device name.
+
+Example:
+	clock {
+		compatible = "fractional-scale-clock";
+		clocks = <&parentclk>;
+		#clock-cells = <1>;
+		reg = <0x0 0xFFFF0000 0x0 0x10>
+		clock-shift = <8>;
+		clock-width = <3>;
+		clock-denom = <8>;
+		clock-inverted = <1>;
+	};
Signature · 1 line
1.9.1
maintainer
#2 lore raw

Re: [PATCH 1/2] Documentation: dt: clock: Add fractional scale binding

Quoted 1 line · Hoan Tran

Add fractional scale clock DTS binding.

We've generally decided against putting this level of clock details into DT.

Quoted 7 lines
---
 .../bindings/clock/fractional-scale-clock.txt      | 31 ++++++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/fractional-scale-clock.txt
author
#3 lore raw

PATCH 2/2 clk: Add fractional scale clock support

In reply to Hoan Tran

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.

---
 drivers/clk/Makefile               |   1 +
 drivers/clk/clk-fractional-scale.c | 253 +++++++++++++++++++++++++++++++++++++
 include/linux/clk-provider.h       |  41 ++++++
 3 files changed, 295 insertions(+)
 create mode 100644 drivers/clk/clk-fractional-scale.c
drivers/clk/Makefile +1 -0
index dcc5e69..d7deddf 100644
@@ -10,6 +10,7 @@ obj-$(CONFIG_COMMON_CLK)	+= clk-multiplier.o
 obj-$(CONFIG_COMMON_CLK)	+= clk-mux.o
 obj-$(CONFIG_COMMON_CLK)	+= clk-composite.o
 obj-$(CONFIG_COMMON_CLK)	+= clk-fractional-divider.o
+obj-$(CONFIG_COMMON_CLK)	+= clk-fractional-scale.o
 obj-$(CONFIG_COMMON_CLK)	+= clk-gpio.o
 ifeq ($(CONFIG_OF), y)
 obj-$(CONFIG_COMMON_CLK)	+= clk-conf.o
drivers/clk/clk-fractional-scale.c new file +253 -0

253 lines not on this page · raw message

include/linux/clk-provider.h +41 -0

47 lines not on this page · raw message

Signature · 1 line
1.9.1
#4 lore raw
reproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…

Hi,

[auto build test ERROR on clk/clk-next]
[also build test ERROR on v4.7-rc3 next-20160616]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Hoan-Tran/clk-Add-fractional-scale-clock-support/20160617-074656
base:   https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git clk-next
config: microblaze-mmu_defconfig (attached as .config)
compiler: microblaze-linux-gcc (GCC) 4.9.0

reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=microblaze

All errors (new ones prefixed by >>):

drivers/built-in.o: In function `clk_fs_set_rate':

>> drivers/clk/clk-fractional-scale.c:99: undefined reference to `__udivdi3'

drivers/built-in.o: In function `clk_fs_round_rate':
drivers/clk/clk-fractional-scale.c:76: undefined reference to `__udivdi3'

vim +99 drivers/clk/clk-fractional-scale.c

93 * Compute the scaler:
94 *
95 * freq = parent_rate * scaler / denom, or
96 * scaler = freq * denom / parent_rate
97 */
98 ret = rate * fd->denom;

  > 99		scale = ret / (u64)parent_rate;

100
101 /* Check if inverted */
102 if (fd->flags & CLK_FRACTIONAL_SCALE_INVERTED)

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#5 lore raw
In reply to Hoan Tran
reproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…

Hi,

[auto build test ERROR on clk/clk-next]
[also build test ERROR on v4.7-rc3 next-20160616]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Hoan-Tran/clk-Add-fractional-scale-clock-support/20160617-074656
base:   https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git clk-next
config: arm-shmobile_defconfig (attached as .config)
compiler: arm-linux-gnueabi-gcc (Debian 5.3.1-8) 5.3.1 20160205

reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=arm

All errors (new ones prefixed by >>):

drivers/built-in.o: In function `clk_fs_set_rate':

>> core.c:(.text+0x1fbee8): undefined reference to `__aeabi_uldivmod'

drivers/built-in.o: In function `clk_fs_round_rate':
core.c:(.text+0x1fbfb4): undefined reference to `__aeabi_uldivmod'

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#6 lore raw
In reply to Hoan Tran
reproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…

Hi,

[auto build test ERROR on clk/clk-next]
[also build test ERROR on v4.7-rc3 next-20160616]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Hoan-Tran/clk-Add-fractional-scale-clock-support/20160617-074656
base:   https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git clk-next
config: arm-at91_dt_defconfig (attached as .config)
compiler: arm-linux-gnueabi-gcc (Debian 5.3.1-8) 5.3.1 20160205

reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=arm

All errors (new ones prefixed by >>):

drivers/built-in.o: In function `clk_fs_set_rate':

>> powercap_sys.c:(.text+0x17e7cc): undefined reference to `__aeabi_uldivmod'

drivers/built-in.o: In function `clk_fs_round_rate':
powercap_sys.c:(.text+0x17e884): undefined reference to `__aeabi_uldivmod'

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#7 lore raw
In reply to Hoan Tran
Quoted 13 lines · Hoan Tran

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.

drivers/clk/clk-fractional-scale.c new file +1 -0
 
+static long clk_fs_round_rate(struct clk_hw *hw, unsigned long rate,

drivers/clk/clk-fractional-scale.c line 75

Quoted 10 lines
drivers/clk/clk-fractional-scale.c new file +10 -0
+                             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.

Quoted 1 line

+ scale = ret / *parent_rate;

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

Quoted 12 lines
drivers/clk/clk-fractional-scale.c new file +12 -0
+
+       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;

drivers/clk/clk-fractional-scale.c line 98

Quoted 10 lines
drivers/clk/clk-fractional-scale.c new file +10 -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.

Quoted 1 line

+ 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().

include/linux/clk-provider.h struct clk_hw *clk_hw_register_fractional_divider(struct device *dev, · line 607

Quoted 9 lines
include/linux/clk-provider.h +6 -0
 
+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?

include/linux/clk-provider.h struct clk_hw *clk_hw_register_fractional_divider(struct device *dev, · line 610

Quoted 3 lines
include/linux/clk-provider.h +3 -0
+       u32             flags;
+       spinlock_t      *lock;
+};

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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

author
#8 lore raw

Hi Geert,

Quoted 28 lines · Geert Uytterhoeven
Quoted 13 lines · Hoan Tran

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.

drivers/clk/clk-fractional-scale.c new file +1 -0
 
+static long clk_fs_round_rate(struct clk_hw *hw, unsigned long rate,

drivers/clk/clk-fractional-scale.c line 75

Quoted 10 lines
drivers/clk/clk-fractional-scale.c new file +10 -0
+                             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.

Quoted 3 lines
Quoted 1 line

+ scale = ret / *parent_rate;

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

Yes, my mistake. I'll fix it.

Quoted 27 lines
Quoted 13 lines
drivers/clk/clk-fractional-scale.c new file +12 -0
+
+       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;

drivers/clk/clk-fractional-scale.c line 98

Quoted 10 lines
drivers/clk/clk-fractional-scale.c new file +10 -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.

Quoted 5 lines
Quoted 1 line

+ 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().

Will fix it.

Thanks
Hoan

Quoted 28 lines

include/linux/clk-provider.h struct clk_hw *clk_hw_register_fractional_divider(struct device *dev, · line 607

Quoted 10 lines
include/linux/clk-provider.h +6 -0
 
+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?

include/linux/clk-provider.h struct clk_hw *clk_hw_register_fractional_divider(struct device *dev, · line 610

Quoted 3 lines
include/linux/clk-provider.h +3 -0
+       u32             flags;
+       spinlock_t      *lock;
+};

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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