2 * Copyright (C) 2014 Intel Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Adjustable fractional divider clock implementation.
9 * Output rate = (m / n) * parent_rate.
10 * Uses rational best approximation algorithm.
13 #include <linux/clk-provider.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/slab.h>
17 #include <linux/rational.h>
19 static unsigned long clk_fd_recalc_rate(struct clk_hw *hw,
20 unsigned long parent_rate)
22 struct clk_fractional_divider *fd = to_clk_fd(hw);
23 unsigned long flags = 0;
29 spin_lock_irqsave(fd->lock, flags);
33 val = clk_readl(fd->reg);
36 spin_unlock_irqrestore(fd->lock, flags);
40 m = (val & fd->mmask) >> fd->mshift;
41 n = (val & fd->nmask) >> fd->nshift;
46 ret = (u64)parent_rate * m;
52 static void clk_fd_general_approximation(struct clk_hw *hw, unsigned long rate,
53 unsigned long *parent_rate,
54 unsigned long *m, unsigned long *n)
56 struct clk_fractional_divider *fd = to_clk_fd(hw);
60 * Get rate closer to *parent_rate to guarantee there is no overflow
61 * for m and n. In the result it will be the nearest rate left shifted
62 * by (scale - fd->nwidth) bits.
64 scale = fls_long(*parent_rate / rate - 1);
65 if (scale > fd->nwidth)
66 rate <<= scale - fd->nwidth;
68 rational_best_approximation(rate, *parent_rate,
69 GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
73 static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
74 unsigned long *parent_rate)
76 struct clk_fractional_divider *fd = to_clk_fd(hw);
80 if (!rate || rate >= *parent_rate)
83 if (fd->approximation)
84 fd->approximation(hw, rate, parent_rate, &m, &n);
86 clk_fd_general_approximation(hw, rate, parent_rate, &m, &n);
88 ret = (u64)*parent_rate * m;
94 static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
95 unsigned long parent_rate)
97 struct clk_fractional_divider *fd = to_clk_fd(hw);
98 unsigned long flags = 0;
102 rational_best_approximation(rate, parent_rate,
103 GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
107 spin_lock_irqsave(fd->lock, flags);
111 val = clk_readl(fd->reg);
112 val &= ~(fd->mmask | fd->nmask);
113 val |= (m << fd->mshift) | (n << fd->nshift);
114 clk_writel(val, fd->reg);
117 spin_unlock_irqrestore(fd->lock, flags);
124 const struct clk_ops clk_fractional_divider_ops = {
125 .recalc_rate = clk_fd_recalc_rate,
126 .round_rate = clk_fd_round_rate,
127 .set_rate = clk_fd_set_rate,
129 EXPORT_SYMBOL_GPL(clk_fractional_divider_ops);
131 struct clk_hw *clk_hw_register_fractional_divider(struct device *dev,
132 const char *name, const char *parent_name, unsigned long flags,
133 void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
134 u8 clk_divider_flags, spinlock_t *lock)
136 struct clk_fractional_divider *fd;
137 struct clk_init_data init;
141 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
143 return ERR_PTR(-ENOMEM);
146 init.ops = &clk_fractional_divider_ops;
147 init.flags = flags | CLK_IS_BASIC;
148 init.parent_names = parent_name ? &parent_name : NULL;
149 init.num_parents = parent_name ? 1 : 0;
154 fd->mmask = GENMASK(mwidth - 1, 0) << mshift;
157 fd->nmask = GENMASK(nwidth - 1, 0) << nshift;
158 fd->flags = clk_divider_flags;
163 ret = clk_hw_register(dev, hw);
171 EXPORT_SYMBOL_GPL(clk_hw_register_fractional_divider);
173 struct clk *clk_register_fractional_divider(struct device *dev,
174 const char *name, const char *parent_name, unsigned long flags,
175 void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
176 u8 clk_divider_flags, spinlock_t *lock)
180 hw = clk_hw_register_fractional_divider(dev, name, parent_name, flags,
181 reg, mshift, mwidth, nshift, nwidth, clk_divider_flags,
187 EXPORT_SYMBOL_GPL(clk_register_fractional_divider);
189 void clk_hw_unregister_fractional_divider(struct clk_hw *hw)
191 struct clk_fractional_divider *fd;
195 clk_hw_unregister(hw);