Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / clk / meson / sclk-div.c
blob9c4945234f2682ea4c4e9c2fc2158fe37dc46f74
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /*
3 * Copyright (c) 2018 BayLibre, SAS.
4 * Author: Jerome Brunet <jbrunet@baylibre.com>
6 * Sample clock generator divider:
7 * This HW divider gates with value 0 but is otherwise a zero based divider:
9 * val >= 1
10 * divider = val + 1
12 * The duty cycle may also be set for the LR clock variant. The duty cycle
13 * ratio is:
15 * hi = [0 - val]
16 * duty_cycle = (1 + hi) / (1 + val)
19 #include <linux/clk-provider.h>
20 #include <linux/module.h>
22 #include "clk-regmap.h"
23 #include "sclk-div.h"
25 static inline struct meson_sclk_div_data *
26 meson_sclk_div_data(struct clk_regmap *clk)
28 return (struct meson_sclk_div_data *)clk->data;
31 static int sclk_div_maxval(struct meson_sclk_div_data *sclk)
33 return (1 << sclk->div.width) - 1;
36 static int sclk_div_maxdiv(struct meson_sclk_div_data *sclk)
38 return sclk_div_maxval(sclk) + 1;
41 static int sclk_div_getdiv(struct clk_hw *hw, unsigned long rate,
42 unsigned long prate, int maxdiv)
44 int div = DIV_ROUND_CLOSEST_ULL((u64)prate, rate);
46 return clamp(div, 2, maxdiv);
49 static int sclk_div_bestdiv(struct clk_hw *hw, unsigned long rate,
50 unsigned long *prate,
51 struct meson_sclk_div_data *sclk)
53 struct clk_hw *parent = clk_hw_get_parent(hw);
54 int bestdiv = 0, i;
55 unsigned long maxdiv, now, parent_now;
56 unsigned long best = 0, best_parent = 0;
58 if (!rate)
59 rate = 1;
61 maxdiv = sclk_div_maxdiv(sclk);
63 if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT))
64 return sclk_div_getdiv(hw, rate, *prate, maxdiv);
67 * The maximum divider we can use without overflowing
68 * unsigned long in rate * i below
70 maxdiv = min(ULONG_MAX / rate, maxdiv);
72 for (i = 2; i <= maxdiv; i++) {
74 * It's the most ideal case if the requested rate can be
75 * divided from parent clock without needing to change
76 * parent rate, so return the divider immediately.
78 if (rate * i == *prate)
79 return i;
81 parent_now = clk_hw_round_rate(parent, rate * i);
82 now = DIV_ROUND_UP_ULL((u64)parent_now, i);
84 if (abs(rate - now) < abs(rate - best)) {
85 bestdiv = i;
86 best = now;
87 best_parent = parent_now;
91 if (!bestdiv)
92 bestdiv = sclk_div_maxdiv(sclk);
93 else
94 *prate = best_parent;
96 return bestdiv;
99 static int sclk_div_determine_rate(struct clk_hw *hw,
100 struct clk_rate_request *req)
102 struct clk_regmap *clk = to_clk_regmap(hw);
103 struct meson_sclk_div_data *sclk = meson_sclk_div_data(clk);
104 int div;
106 div = sclk_div_bestdiv(hw, req->rate, &req->best_parent_rate, sclk);
107 req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div);
109 return 0;
112 static void sclk_apply_ratio(struct clk_regmap *clk,
113 struct meson_sclk_div_data *sclk)
115 unsigned int hi = DIV_ROUND_CLOSEST(sclk->cached_div *
116 sclk->cached_duty.num,
117 sclk->cached_duty.den);
119 if (hi)
120 hi -= 1;
122 meson_parm_write(clk->map, &sclk->hi, hi);
125 static int sclk_div_set_duty_cycle(struct clk_hw *hw,
126 struct clk_duty *duty)
128 struct clk_regmap *clk = to_clk_regmap(hw);
129 struct meson_sclk_div_data *sclk = meson_sclk_div_data(clk);
131 if (MESON_PARM_APPLICABLE(&sclk->hi)) {
132 memcpy(&sclk->cached_duty, duty, sizeof(*duty));
133 sclk_apply_ratio(clk, sclk);
136 return 0;
139 static int sclk_div_get_duty_cycle(struct clk_hw *hw,
140 struct clk_duty *duty)
142 struct clk_regmap *clk = to_clk_regmap(hw);
143 struct meson_sclk_div_data *sclk = meson_sclk_div_data(clk);
144 int hi;
146 if (!MESON_PARM_APPLICABLE(&sclk->hi)) {
147 duty->num = 1;
148 duty->den = 2;
149 return 0;
152 hi = meson_parm_read(clk->map, &sclk->hi);
153 duty->num = hi + 1;
154 duty->den = sclk->cached_div;
155 return 0;
158 static void sclk_apply_divider(struct clk_regmap *clk,
159 struct meson_sclk_div_data *sclk)
161 if (MESON_PARM_APPLICABLE(&sclk->hi))
162 sclk_apply_ratio(clk, sclk);
164 meson_parm_write(clk->map, &sclk->div, sclk->cached_div - 1);
167 static int sclk_div_set_rate(struct clk_hw *hw, unsigned long rate,
168 unsigned long prate)
170 struct clk_regmap *clk = to_clk_regmap(hw);
171 struct meson_sclk_div_data *sclk = meson_sclk_div_data(clk);
172 unsigned long maxdiv = sclk_div_maxdiv(sclk);
174 sclk->cached_div = sclk_div_getdiv(hw, rate, prate, maxdiv);
176 if (clk_hw_is_enabled(hw))
177 sclk_apply_divider(clk, sclk);
179 return 0;
182 static unsigned long sclk_div_recalc_rate(struct clk_hw *hw,
183 unsigned long prate)
185 struct clk_regmap *clk = to_clk_regmap(hw);
186 struct meson_sclk_div_data *sclk = meson_sclk_div_data(clk);
188 return DIV_ROUND_UP_ULL((u64)prate, sclk->cached_div);
191 static int sclk_div_enable(struct clk_hw *hw)
193 struct clk_regmap *clk = to_clk_regmap(hw);
194 struct meson_sclk_div_data *sclk = meson_sclk_div_data(clk);
196 sclk_apply_divider(clk, sclk);
198 return 0;
201 static void sclk_div_disable(struct clk_hw *hw)
203 struct clk_regmap *clk = to_clk_regmap(hw);
204 struct meson_sclk_div_data *sclk = meson_sclk_div_data(clk);
206 meson_parm_write(clk->map, &sclk->div, 0);
209 static int sclk_div_is_enabled(struct clk_hw *hw)
211 struct clk_regmap *clk = to_clk_regmap(hw);
212 struct meson_sclk_div_data *sclk = meson_sclk_div_data(clk);
214 if (meson_parm_read(clk->map, &sclk->div))
215 return 1;
217 return 0;
220 static int sclk_div_init(struct clk_hw *hw)
222 struct clk_regmap *clk = to_clk_regmap(hw);
223 struct meson_sclk_div_data *sclk = meson_sclk_div_data(clk);
224 unsigned int val;
226 val = meson_parm_read(clk->map, &sclk->div);
228 /* if the divider is initially disabled, assume max */
229 if (!val)
230 sclk->cached_div = sclk_div_maxdiv(sclk);
231 else
232 sclk->cached_div = val + 1;
234 sclk_div_get_duty_cycle(hw, &sclk->cached_duty);
236 return 0;
239 const struct clk_ops meson_sclk_div_ops = {
240 .recalc_rate = sclk_div_recalc_rate,
241 .determine_rate = sclk_div_determine_rate,
242 .set_rate = sclk_div_set_rate,
243 .enable = sclk_div_enable,
244 .disable = sclk_div_disable,
245 .is_enabled = sclk_div_is_enabled,
246 .get_duty_cycle = sclk_div_get_duty_cycle,
247 .set_duty_cycle = sclk_div_set_duty_cycle,
248 .init = sclk_div_init,
250 EXPORT_SYMBOL_NS_GPL(meson_sclk_div_ops, "CLK_MESON");
252 MODULE_DESCRIPTION("Amlogic Sample divider driver");
253 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
254 MODULE_LICENSE("GPL");
255 MODULE_IMPORT_NS("CLK_MESON");