1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2014 Intel Corporation
5 * Adjustable fractional divider clock implementation.
6 * Output rate = (m / n) * parent_rate.
7 * Uses rational best approximation algorithm.
10 #include <linux/clk-provider.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/slab.h>
14 #include <linux/rational.h>
16 static unsigned long clk_fd_recalc_rate(struct clk_hw *hw,
17 unsigned long parent_rate)
19 struct clk_fractional_divider *fd = to_clk_fd(hw);
20 unsigned long flags = 0;
26 spin_lock_irqsave(fd->lock, flags);
30 val = clk_readl(fd->reg);
33 spin_unlock_irqrestore(fd->lock, flags);
37 m = (val & fd->mmask) >> fd->mshift;
38 n = (val & fd->nmask) >> fd->nshift;
43 ret = (u64)parent_rate * m;
49 static void clk_fd_general_approximation(struct clk_hw *hw, unsigned long rate,
50 unsigned long *parent_rate,
51 unsigned long *m, unsigned long *n)
53 struct clk_fractional_divider *fd = to_clk_fd(hw);
57 * Get rate closer to *parent_rate to guarantee there is no overflow
58 * for m and n. In the result it will be the nearest rate left shifted
59 * by (scale - fd->nwidth) bits.
61 scale = fls_long(*parent_rate / rate - 1);
62 if (scale > fd->nwidth)
63 rate <<= scale - fd->nwidth;
65 rational_best_approximation(rate, *parent_rate,
66 GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
70 static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
71 unsigned long *parent_rate)
73 struct clk_fractional_divider *fd = to_clk_fd(hw);
77 if (!rate || rate >= *parent_rate)
80 if (fd->approximation)
81 fd->approximation(hw, rate, parent_rate, &m, &n);
83 clk_fd_general_approximation(hw, rate, parent_rate, &m, &n);
85 ret = (u64)*parent_rate * m;
91 static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
92 unsigned long parent_rate)
94 struct clk_fractional_divider *fd = to_clk_fd(hw);
95 unsigned long flags = 0;
99 rational_best_approximation(rate, parent_rate,
100 GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
104 spin_lock_irqsave(fd->lock, flags);
108 val = clk_readl(fd->reg);
109 val &= ~(fd->mmask | fd->nmask);
110 val |= (m << fd->mshift) | (n << fd->nshift);
111 clk_writel(val, fd->reg);
114 spin_unlock_irqrestore(fd->lock, flags);
121 const struct clk_ops clk_fractional_divider_ops = {
122 .recalc_rate = clk_fd_recalc_rate,
123 .round_rate = clk_fd_round_rate,
124 .set_rate = clk_fd_set_rate,
126 EXPORT_SYMBOL_GPL(clk_fractional_divider_ops);
128 struct clk_hw *clk_hw_register_fractional_divider(struct device *dev,
129 const char *name, const char *parent_name, unsigned long flags,
130 void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
131 u8 clk_divider_flags, spinlock_t *lock)
133 struct clk_fractional_divider *fd;
134 struct clk_init_data init;
138 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
140 return ERR_PTR(-ENOMEM);
143 init.ops = &clk_fractional_divider_ops;
144 init.flags = flags | CLK_IS_BASIC;
145 init.parent_names = parent_name ? &parent_name : NULL;
146 init.num_parents = parent_name ? 1 : 0;
151 fd->mmask = GENMASK(mwidth - 1, 0) << mshift;
154 fd->nmask = GENMASK(nwidth - 1, 0) << nshift;
155 fd->flags = clk_divider_flags;
160 ret = clk_hw_register(dev, hw);
168 EXPORT_SYMBOL_GPL(clk_hw_register_fractional_divider);
170 struct clk *clk_register_fractional_divider(struct device *dev,
171 const char *name, const char *parent_name, unsigned long flags,
172 void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
173 u8 clk_divider_flags, spinlock_t *lock)
177 hw = clk_hw_register_fractional_divider(dev, name, parent_name, flags,
178 reg, mshift, mwidth, nshift, nwidth, clk_divider_flags,
184 EXPORT_SYMBOL_GPL(clk_register_fractional_divider);
186 void clk_hw_unregister_fractional_divider(struct clk_hw *hw)
188 struct clk_fractional_divider *fd;
192 clk_hw_unregister(hw);