Skip to content

Commit 4e7cf74

Browse files
andy-shevbebarino
authored andcommitted
clk: fractional-divider: Export approximation algorithm to the CCF users
At least one user currently duplicates some functions that are provided by fractional divider module. Let's export approximation algorithm and replace the open-coded variant. As a bonus the exported function will get better documentation in place. Signed-off-by: Andy Shevchenko <[email protected]> Tested-by: Heiko Stuebner <[email protected]> Acked-by: Heiko Stuebner <[email protected]> Link: https://lore.kernel.org/r/[email protected] [[email protected]: Add header guard because why not] Signed-off-by: Stephen Boyd <[email protected]>
1 parent e73f0f0 commit 4e7cf74

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

drivers/clk/clk-fractional-divider.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <linux/slab.h>
1515
#include <linux/rational.h>
1616

17+
#include "clk-fractional-divider.h"
18+
1719
static inline u32 clk_fd_readl(struct clk_fractional_divider *fd)
1820
{
1921
if (fd->flags & CLK_FRAC_DIVIDER_BIG_ENDIAN)
@@ -68,9 +70,10 @@ static unsigned long clk_fd_recalc_rate(struct clk_hw *hw,
6870
return ret;
6971
}
7072

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)
73+
void clk_fractional_divider_general_approximation(struct clk_hw *hw,
74+
unsigned long rate,
75+
unsigned long *parent_rate,
76+
unsigned long *m, unsigned long *n)
7477
{
7578
struct clk_fractional_divider *fd = to_clk_fd(hw);
7679
unsigned long scale;
@@ -102,7 +105,7 @@ static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
102105
if (fd->approximation)
103106
fd->approximation(hw, rate, parent_rate, &m, &n);
104107
else
105-
clk_fd_general_approximation(hw, rate, parent_rate, &m, &n);
108+
clk_fractional_divider_general_approximation(hw, rate, parent_rate, &m, &n);
106109

107110
ret = (u64)*parent_rate * m;
108111
do_div(ret, n);

drivers/clk/clk-fractional-divider.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _CLK_FRACTIONAL_DIV_H
3+
#define _CLK_FRACTIONAL_DIV_H
4+
5+
struct clk_hw;
6+
7+
void clk_fractional_divider_general_approximation(struct clk_hw *hw,
8+
unsigned long rate,
9+
unsigned long *parent_rate,
10+
unsigned long *m,
11+
unsigned long *n);
12+
13+
#endif

drivers/clk/rockchip/clk.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <linux/regmap.h>
2323
#include <linux/reboot.h>
2424
#include <linux/rational.h>
25+
26+
#include "../clk-fractional-divider.h"
2527
#include "clk.h"
2628

2729
/*
@@ -178,10 +180,8 @@ static void rockchip_fractional_approximation(struct clk_hw *hw,
178180
unsigned long rate, unsigned long *parent_rate,
179181
unsigned long *m, unsigned long *n)
180182
{
181-
struct clk_fractional_divider *fd = to_clk_fd(hw);
182183
unsigned long p_rate, p_parent_rate;
183184
struct clk_hw *p_parent;
184-
unsigned long scale;
185185

186186
p_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
187187
if ((rate * 20 > p_rate) && (p_rate % rate != 0)) {
@@ -190,18 +190,7 @@ static void rockchip_fractional_approximation(struct clk_hw *hw,
190190
*parent_rate = p_parent_rate;
191191
}
192192

193-
/*
194-
* Get rate closer to *parent_rate to guarantee there is no overflow
195-
* for m and n. In the result it will be the nearest rate left shifted
196-
* by (scale - fd->nwidth) bits.
197-
*/
198-
scale = fls_long(*parent_rate / rate - 1);
199-
if (scale > fd->nwidth)
200-
rate <<= scale - fd->nwidth;
201-
202-
rational_best_approximation(rate, *parent_rate,
203-
GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
204-
m, n);
193+
clk_fractional_divider_general_approximation(hw, rate, parent_rate, m, n);
205194
}
206195

207196
static struct clk *rockchip_clk_register_frac_branch(

0 commit comments

Comments
 (0)