33 * Copyright (C) 2014 Intel Corporation
44 *
55 * Adjustable fractional divider clock implementation.
6- * Output rate = (m / n) * parent_rate.
76 * Uses rational best approximation algorithm.
7+ *
8+ * Output is calculated as
9+ *
10+ * rate = (m / n) * parent_rate (1)
11+ *
12+ * This is useful when we have a prescaler block which asks for
13+ * m (numerator) and n (denominator) values to be provided to satisfy
14+ * the (1) as much as possible.
15+ *
16+ * Since m and n have the limitation by a range, e.g.
17+ *
18+ * n >= 1, n < N_width, where N_width = 2^nwidth (2)
19+ *
20+ * for some cases the output may be saturated. Hence, from (1) and (2),
21+ * assuming the worst case when m = 1, the inequality
22+ *
23+ * floor(log2(parent_rate / rate)) <= nwidth (3)
24+ *
25+ * may be derived. Thus, in cases when
26+ *
27+ * (parent_rate / rate) >> N_width (4)
28+ *
29+ * we might scale up the rate by 2^scale (see the description of
30+ * CLK_FRAC_DIVIDER_POWER_OF_TWO_PS for additional information), where
31+ *
32+ * scale = floor(log2(parent_rate / rate)) - nwidth (5)
33+ *
34+ * and assume that the IP, that needs m and n, has also its own
35+ * prescaler, which is capable to divide by 2^scale. In this way
36+ * we get the denominator to satisfy the desired range (2) and
37+ * at the same time much much better result of m and n than simple
38+ * saturated values.
839 */
940
1041#include <linux/clk-provider.h>
1445#include <linux/slab.h>
1546#include <linux/rational.h>
1647
48+ #include "clk-fractional-divider.h"
49+
1750static inline u32 clk_fd_readl (struct clk_fractional_divider * fd )
1851{
1952 if (fd -> flags & CLK_FRAC_DIVIDER_BIG_ENDIAN )
@@ -68,21 +101,26 @@ static unsigned long clk_fd_recalc_rate(struct clk_hw *hw,
68101 return ret ;
69102}
70103
71- static void clk_fd_general_approximation (struct clk_hw * hw , unsigned long rate ,
72- unsigned long * parent_rate ,
73- unsigned long * m , unsigned long * n )
104+ void clk_fractional_divider_general_approximation (struct clk_hw * hw ,
105+ unsigned long rate ,
106+ unsigned long * parent_rate ,
107+ unsigned long * m , unsigned long * n )
74108{
75109 struct clk_fractional_divider * fd = to_clk_fd (hw );
76- unsigned long scale ;
77110
78111 /*
79112 * Get rate closer to *parent_rate to guarantee there is no overflow
80113 * for m and n. In the result it will be the nearest rate left shifted
81114 * by (scale - fd->nwidth) bits.
115+ *
116+ * For the detailed explanation see the top comment in this file.
82117 */
83- scale = fls_long (* parent_rate / rate - 1 );
84- if (scale > fd -> nwidth )
85- rate <<= scale - fd -> nwidth ;
118+ if (fd -> flags & CLK_FRAC_DIVIDER_POWER_OF_TWO_PS ) {
119+ unsigned long scale = fls_long (* parent_rate / rate - 1 );
120+
121+ if (scale > fd -> nwidth )
122+ rate <<= scale - fd -> nwidth ;
123+ }
86124
87125 rational_best_approximation (rate , * parent_rate ,
88126 GENMASK (fd -> mwidth - 1 , 0 ), GENMASK (fd -> nwidth - 1 , 0 ),
@@ -102,7 +140,7 @@ static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
102140 if (fd -> approximation )
103141 fd -> approximation (hw , rate , parent_rate , & m , & n );
104142 else
105- clk_fd_general_approximation (hw , rate , parent_rate , & m , & n );
143+ clk_fractional_divider_general_approximation (hw , rate , parent_rate , & m , & n );
106144
107145 ret = (u64 )* parent_rate * m ;
108146 do_div (ret , n );
0 commit comments