blob: f6b2bf5584867e115c0fb39185b41d0607087b22 [file] [log] [blame]
Stephen Boyde1bd55e2018-12-11 09:57:48 -08001// SPDX-License-Identifier: GPL-2.0
Mike Turquette9d9f78e2012-03-15 23:11:20 -07002/*
3 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
4 * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
5 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
6 *
Mike Turquette9d9f78e2012-03-15 23:11:20 -07007 * Adjustable divider clock implementation
8 */
9
10#include <linux/clk-provider.h>
Michael Walle26792692020-11-08 19:51:09 +010011#include <linux/device.h>
Mike Turquette9d9f78e2012-03-15 23:11:20 -070012#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/io.h>
15#include <linux/err.h>
16#include <linux/string.h>
James Hogan1a3cd182013-01-15 10:28:05 +000017#include <linux/log2.h>
Mike Turquette9d9f78e2012-03-15 23:11:20 -070018
19/*
20 * DOC: basic adjustable divider clock that cannot gate
21 *
22 * Traits of this clock:
23 * prepare - clk_prepare only ensures that parents are prepared
24 * enable - clk_enable only ensures that parents are enabled
Brian Norris9556f9d2015-04-13 16:03:21 -070025 * rate - rate is adjustable. clk->rate = ceiling(parent->rate / divisor)
Mike Turquette9d9f78e2012-03-15 23:11:20 -070026 * parent - fixed parent. No clk_set_parent support
27 */
28
Jonas Gorski434d69f2019-04-18 13:12:04 +020029static inline u32 clk_div_readl(struct clk_divider *divider)
30{
31 if (divider->flags & CLK_DIVIDER_BIG_ENDIAN)
32 return ioread32be(divider->reg);
33
Jonas Gorski5834fd72019-04-18 13:12:11 +020034 return readl(divider->reg);
Jonas Gorski434d69f2019-04-18 13:12:04 +020035}
36
37static inline void clk_div_writel(struct clk_divider *divider, u32 val)
38{
39 if (divider->flags & CLK_DIVIDER_BIG_ENDIAN)
40 iowrite32be(val, divider->reg);
41 else
Jonas Gorski5834fd72019-04-18 13:12:11 +020042 writel(val, divider->reg);
Jonas Gorski434d69f2019-04-18 13:12:04 +020043}
44
Stephen Boydfab88ca2015-11-30 17:31:38 -080045static unsigned int _get_table_maxdiv(const struct clk_div_table *table,
46 u8 width)
Rajendra Nayak357c3f02012-06-29 19:06:32 +053047{
Jerome Brunete6d3cc72018-02-14 14:43:33 +010048 unsigned int maxdiv = 0, mask = clk_div_mask(width);
Rajendra Nayak357c3f02012-06-29 19:06:32 +053049 const struct clk_div_table *clkt;
50
51 for (clkt = table; clkt->div; clkt++)
Stephen Boydfab88ca2015-11-30 17:31:38 -080052 if (clkt->div > maxdiv && clkt->val <= mask)
Rajendra Nayak357c3f02012-06-29 19:06:32 +053053 maxdiv = clkt->div;
54 return maxdiv;
55}
56
Maxime COQUELIN774b5142014-01-29 17:24:07 +010057static unsigned int _get_table_mindiv(const struct clk_div_table *table)
58{
59 unsigned int mindiv = UINT_MAX;
60 const struct clk_div_table *clkt;
61
62 for (clkt = table; clkt->div; clkt++)
63 if (clkt->div < mindiv)
64 mindiv = clkt->div;
65 return mindiv;
66}
67
Stephen Boydbca96902015-01-19 18:05:29 -080068static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width,
69 unsigned long flags)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +053070{
Stephen Boydbca96902015-01-19 18:05:29 -080071 if (flags & CLK_DIVIDER_ONE_BASED)
Jerome Brunete6d3cc72018-02-14 14:43:33 +010072 return clk_div_mask(width);
Stephen Boydbca96902015-01-19 18:05:29 -080073 if (flags & CLK_DIVIDER_POWER_OF_TWO)
Jerome Brunete6d3cc72018-02-14 14:43:33 +010074 return 1 << clk_div_mask(width);
Stephen Boydbca96902015-01-19 18:05:29 -080075 if (table)
Stephen Boydfab88ca2015-11-30 17:31:38 -080076 return _get_table_maxdiv(table, width);
Jerome Brunete6d3cc72018-02-14 14:43:33 +010077 return clk_div_mask(width) + 1;
Rajendra Nayak6d9252b2012-05-17 15:52:13 +053078}
79
Rajendra Nayak357c3f02012-06-29 19:06:32 +053080static unsigned int _get_table_div(const struct clk_div_table *table,
81 unsigned int val)
82{
83 const struct clk_div_table *clkt;
84
85 for (clkt = table; clkt->div; clkt++)
86 if (clkt->val == val)
87 return clkt->div;
88 return 0;
89}
90
Stephen Boydbca96902015-01-19 18:05:29 -080091static unsigned int _get_div(const struct clk_div_table *table,
Jim Quinlanafe76c8f2015-05-15 15:45:47 -040092 unsigned int val, unsigned long flags, u8 width)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +053093{
Stephen Boydbca96902015-01-19 18:05:29 -080094 if (flags & CLK_DIVIDER_ONE_BASED)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +053095 return val;
Stephen Boydbca96902015-01-19 18:05:29 -080096 if (flags & CLK_DIVIDER_POWER_OF_TWO)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +053097 return 1 << val;
Jim Quinlanafe76c8f2015-05-15 15:45:47 -040098 if (flags & CLK_DIVIDER_MAX_AT_ZERO)
Jerome Brunete6d3cc72018-02-14 14:43:33 +010099 return val ? val : clk_div_mask(width) + 1;
Stephen Boydbca96902015-01-19 18:05:29 -0800100 if (table)
101 return _get_table_div(table, val);
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530102 return val + 1;
103}
104
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530105static unsigned int _get_table_val(const struct clk_div_table *table,
106 unsigned int div)
107{
108 const struct clk_div_table *clkt;
109
110 for (clkt = table; clkt->div; clkt++)
111 if (clkt->div == div)
112 return clkt->val;
113 return 0;
114}
115
Stephen Boydbca96902015-01-19 18:05:29 -0800116static unsigned int _get_val(const struct clk_div_table *table,
Jim Quinlanafe76c8f2015-05-15 15:45:47 -0400117 unsigned int div, unsigned long flags, u8 width)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530118{
Stephen Boydbca96902015-01-19 18:05:29 -0800119 if (flags & CLK_DIVIDER_ONE_BASED)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530120 return div;
Stephen Boydbca96902015-01-19 18:05:29 -0800121 if (flags & CLK_DIVIDER_POWER_OF_TWO)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530122 return __ffs(div);
Jim Quinlanafe76c8f2015-05-15 15:45:47 -0400123 if (flags & CLK_DIVIDER_MAX_AT_ZERO)
Jerome Brunete6d3cc72018-02-14 14:43:33 +0100124 return (div == clk_div_mask(width) + 1) ? 0 : div;
Stephen Boydbca96902015-01-19 18:05:29 -0800125 if (table)
126 return _get_table_val(table, div);
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530127 return div - 1;
128}
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700129
Stephen Boydbca96902015-01-19 18:05:29 -0800130unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate,
131 unsigned int val,
132 const struct clk_div_table *table,
Jerome Brunet12a26c22017-12-21 17:30:54 +0100133 unsigned long flags, unsigned long width)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700134{
Stephen Boydbca96902015-01-19 18:05:29 -0800135 unsigned int div;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700136
Jerome Brunet12a26c22017-12-21 17:30:54 +0100137 div = _get_div(table, val, flags, width);
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530138 if (!div) {
Stephen Boydbca96902015-01-19 18:05:29 -0800139 WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO),
Soren Brinkmann056b20532013-04-02 15:36:56 -0700140 "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
Stephen Boyd2f508a92015-07-30 17:20:57 -0700141 clk_hw_get_name(hw));
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530142 return parent_rate;
143 }
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700144
Brian Norris9556f9d2015-04-13 16:03:21 -0700145 return DIV_ROUND_UP_ULL((u64)parent_rate, div);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700146}
Stephen Boydbca96902015-01-19 18:05:29 -0800147EXPORT_SYMBOL_GPL(divider_recalc_rate);
148
149static unsigned long clk_divider_recalc_rate(struct clk_hw *hw,
150 unsigned long parent_rate)
151{
152 struct clk_divider *divider = to_clk_divider(hw);
153 unsigned int val;
154
Jonas Gorski434d69f2019-04-18 13:12:04 +0200155 val = clk_div_readl(divider) >> divider->shift;
Jerome Brunete6d3cc72018-02-14 14:43:33 +0100156 val &= clk_div_mask(divider->width);
Stephen Boydbca96902015-01-19 18:05:29 -0800157
158 return divider_recalc_rate(hw, parent_rate, val, divider->table,
Jerome Brunet12a26c22017-12-21 17:30:54 +0100159 divider->flags, divider->width);
Stephen Boydbca96902015-01-19 18:05:29 -0800160}
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700161
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530162static bool _is_valid_table_div(const struct clk_div_table *table,
163 unsigned int div)
164{
165 const struct clk_div_table *clkt;
166
167 for (clkt = table; clkt->div; clkt++)
168 if (clkt->div == div)
169 return true;
170 return false;
171}
172
Stephen Boydbca96902015-01-19 18:05:29 -0800173static bool _is_valid_div(const struct clk_div_table *table, unsigned int div,
174 unsigned long flags)
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530175{
Stephen Boydbca96902015-01-19 18:05:29 -0800176 if (flags & CLK_DIVIDER_POWER_OF_TWO)
James Hogan1a3cd182013-01-15 10:28:05 +0000177 return is_power_of_2(div);
Stephen Boydbca96902015-01-19 18:05:29 -0800178 if (table)
179 return _is_valid_table_div(table, div);
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530180 return true;
181}
182
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100183static int _round_up_table(const struct clk_div_table *table, int div)
184{
185 const struct clk_div_table *clkt;
Maxime COQUELINfe52e752014-05-07 18:48:52 +0200186 int up = INT_MAX;
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100187
188 for (clkt = table; clkt->div; clkt++) {
189 if (clkt->div == div)
190 return clkt->div;
191 else if (clkt->div < div)
192 continue;
193
194 if ((clkt->div - div) < (up - div))
195 up = clkt->div;
196 }
197
198 return up;
199}
200
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100201static int _round_down_table(const struct clk_div_table *table, int div)
202{
203 const struct clk_div_table *clkt;
204 int down = _get_table_mindiv(table);
205
206 for (clkt = table; clkt->div; clkt++) {
207 if (clkt->div == div)
208 return clkt->div;
209 else if (clkt->div > div)
210 continue;
211
212 if ((div - clkt->div) < (div - down))
213 down = clkt->div;
214 }
215
216 return down;
217}
218
Stephen Boydbca96902015-01-19 18:05:29 -0800219static int _div_round_up(const struct clk_div_table *table,
220 unsigned long parent_rate, unsigned long rate,
221 unsigned long flags)
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100222{
Brian Norris9556f9d2015-04-13 16:03:21 -0700223 int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100224
Stephen Boydbca96902015-01-19 18:05:29 -0800225 if (flags & CLK_DIVIDER_POWER_OF_TWO)
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100226 div = __roundup_pow_of_two(div);
Stephen Boydbca96902015-01-19 18:05:29 -0800227 if (table)
228 div = _round_up_table(table, div);
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100229
230 return div;
231}
232
Stephen Boydbca96902015-01-19 18:05:29 -0800233static int _div_round_closest(const struct clk_div_table *table,
234 unsigned long parent_rate, unsigned long rate,
235 unsigned long flags)
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100236{
Uwe Kleine-König93155142015-02-21 11:40:25 +0100237 int up, down;
Uwe Kleine-König26bac952015-02-21 11:40:24 +0100238 unsigned long up_rate, down_rate;
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100239
Brian Norris9556f9d2015-04-13 16:03:21 -0700240 up = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
Uwe Kleine-König93155142015-02-21 11:40:25 +0100241 down = parent_rate / rate;
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100242
Stephen Boydbca96902015-01-19 18:05:29 -0800243 if (flags & CLK_DIVIDER_POWER_OF_TWO) {
Uwe Kleine-König93155142015-02-21 11:40:25 +0100244 up = __roundup_pow_of_two(up);
245 down = __rounddown_pow_of_two(down);
Stephen Boydbca96902015-01-19 18:05:29 -0800246 } else if (table) {
Uwe Kleine-König93155142015-02-21 11:40:25 +0100247 up = _round_up_table(table, up);
248 down = _round_down_table(table, down);
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100249 }
250
Brian Norris9556f9d2015-04-13 16:03:21 -0700251 up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up);
252 down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down);
Uwe Kleine-König26bac952015-02-21 11:40:24 +0100253
254 return (rate - up_rate) <= (down_rate - rate) ? up : down;
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100255}
256
Stephen Boydbca96902015-01-19 18:05:29 -0800257static int _div_round(const struct clk_div_table *table,
258 unsigned long parent_rate, unsigned long rate,
259 unsigned long flags)
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100260{
Stephen Boydbca96902015-01-19 18:05:29 -0800261 if (flags & CLK_DIVIDER_ROUND_CLOSEST)
262 return _div_round_closest(table, parent_rate, rate, flags);
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100263
Stephen Boydbca96902015-01-19 18:05:29 -0800264 return _div_round_up(table, parent_rate, rate, flags);
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100265}
266
Stephen Boydbca96902015-01-19 18:05:29 -0800267static bool _is_best_div(unsigned long rate, unsigned long now,
268 unsigned long best, unsigned long flags)
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100269{
Stephen Boydbca96902015-01-19 18:05:29 -0800270 if (flags & CLK_DIVIDER_ROUND_CLOSEST)
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100271 return abs(rate - now) < abs(rate - best);
272
273 return now <= rate && now > best;
274}
275
Stephen Boydbca96902015-01-19 18:05:29 -0800276static int _next_div(const struct clk_div_table *table, int div,
277 unsigned long flags)
Maxime COQUELIN0e2de782014-01-29 17:24:08 +0100278{
279 div++;
280
Stephen Boydbca96902015-01-19 18:05:29 -0800281 if (flags & CLK_DIVIDER_POWER_OF_TWO)
Maxime COQUELIN0e2de782014-01-29 17:24:08 +0100282 return __roundup_pow_of_two(div);
Stephen Boydbca96902015-01-19 18:05:29 -0800283 if (table)
284 return _round_up_table(table, div);
Maxime COQUELIN0e2de782014-01-29 17:24:08 +0100285
286 return div;
287}
288
Maxime Ripard22833a92017-05-17 09:40:30 +0200289static int clk_divider_bestdiv(struct clk_hw *hw, struct clk_hw *parent,
290 unsigned long rate,
Stephen Boydbca96902015-01-19 18:05:29 -0800291 unsigned long *best_parent_rate,
292 const struct clk_div_table *table, u8 width,
293 unsigned long flags)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700294{
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700295 int i, bestdiv = 0;
296 unsigned long parent_rate, best = 0, now, maxdiv;
Shawn Guo081c9022013-06-02 22:20:55 +0800297 unsigned long parent_rate_saved = *best_parent_rate;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700298
299 if (!rate)
300 rate = 1;
301
Stephen Boydbca96902015-01-19 18:05:29 -0800302 maxdiv = _get_maxdiv(table, width, flags);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700303
Stephen Boyd98d8a602015-06-29 16:56:30 -0700304 if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
Shawn Guo81536e02012-04-12 20:50:17 +0800305 parent_rate = *best_parent_rate;
Stephen Boydbca96902015-01-19 18:05:29 -0800306 bestdiv = _div_round(table, parent_rate, rate, flags);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700307 bestdiv = bestdiv == 0 ? 1 : bestdiv;
308 bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
309 return bestdiv;
310 }
311
312 /*
313 * The maximum divider we can use without overflowing
314 * unsigned long in rate * i below
315 */
316 maxdiv = min(ULONG_MAX / rate, maxdiv);
317
Masahiro Yamada653d1452016-01-05 12:43:41 +0900318 for (i = _next_div(table, 0, flags); i <= maxdiv;
319 i = _next_div(table, i, flags)) {
Shawn Guo081c9022013-06-02 22:20:55 +0800320 if (rate * i == parent_rate_saved) {
321 /*
322 * It's the most ideal case if the requested rate can be
323 * divided from parent clock without needing to change
324 * parent rate, so return the divider immediately.
325 */
326 *best_parent_rate = parent_rate_saved;
327 return i;
328 }
Maxime Ripard22833a92017-05-17 09:40:30 +0200329 parent_rate = clk_hw_round_rate(parent, rate * i);
Brian Norris9556f9d2015-04-13 16:03:21 -0700330 now = DIV_ROUND_UP_ULL((u64)parent_rate, i);
Stephen Boydbca96902015-01-19 18:05:29 -0800331 if (_is_best_div(rate, now, best, flags)) {
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700332 bestdiv = i;
333 best = now;
334 *best_parent_rate = parent_rate;
335 }
336 }
337
338 if (!bestdiv) {
Stephen Boydbca96902015-01-19 18:05:29 -0800339 bestdiv = _get_maxdiv(table, width, flags);
Maxime Ripard22833a92017-05-17 09:40:30 +0200340 *best_parent_rate = clk_hw_round_rate(parent, 1);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700341 }
342
343 return bestdiv;
344}
345
Martin Blumenstinglbbd7a6c2021-06-28 00:39:57 +0200346int divider_determine_rate(struct clk_hw *hw, struct clk_rate_request *req,
347 const struct clk_div_table *table, u8 width,
348 unsigned long flags)
349{
350 int div;
351
352 div = clk_divider_bestdiv(hw, req->best_parent_hw, req->rate,
353 &req->best_parent_rate, table, width, flags);
354
355 req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div);
356
357 return 0;
358}
359EXPORT_SYMBOL_GPL(divider_determine_rate);
360
361int divider_ro_determine_rate(struct clk_hw *hw, struct clk_rate_request *req,
362 const struct clk_div_table *table, u8 width,
363 unsigned long flags, unsigned int val)
364{
365 int div;
366
367 div = _get_div(table, val, flags, width);
368
369 /* Even a read-only clock can propagate a rate change */
370 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
371 if (!req->best_parent_hw)
372 return -EINVAL;
373
374 req->best_parent_rate = clk_hw_round_rate(req->best_parent_hw,
375 req->rate * div);
376 }
377
378 req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div);
379
380 return 0;
381}
382EXPORT_SYMBOL_GPL(divider_ro_determine_rate);
383
Maxime Ripard22833a92017-05-17 09:40:30 +0200384long divider_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
385 unsigned long rate, unsigned long *prate,
386 const struct clk_div_table *table,
387 u8 width, unsigned long flags)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700388{
Martin Blumenstinglbbd7a6c2021-06-28 00:39:57 +0200389 struct clk_rate_request req = {
390 .rate = rate,
391 .best_parent_rate = *prate,
392 .best_parent_hw = parent,
393 };
394 int ret;
Stephen Boydbca96902015-01-19 18:05:29 -0800395
Martin Blumenstinglbbd7a6c2021-06-28 00:39:57 +0200396 ret = divider_determine_rate(hw, &req, table, width, flags);
397 if (ret)
398 return ret;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700399
Martin Blumenstinglbbd7a6c2021-06-28 00:39:57 +0200400 *prate = req.best_parent_rate;
401
402 return req.rate;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700403}
Maxime Ripard22833a92017-05-17 09:40:30 +0200404EXPORT_SYMBOL_GPL(divider_round_rate_parent);
Stephen Boydbca96902015-01-19 18:05:29 -0800405
Jerome Brunetb15ee492018-02-14 14:43:39 +0100406long divider_ro_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
407 unsigned long rate, unsigned long *prate,
408 const struct clk_div_table *table, u8 width,
409 unsigned long flags, unsigned int val)
410{
Martin Blumenstinglbbd7a6c2021-06-28 00:39:57 +0200411 struct clk_rate_request req = {
412 .rate = rate,
413 .best_parent_rate = *prate,
414 .best_parent_hw = parent,
415 };
416 int ret;
Jerome Brunetb15ee492018-02-14 14:43:39 +0100417
Martin Blumenstinglbbd7a6c2021-06-28 00:39:57 +0200418 ret = divider_ro_determine_rate(hw, &req, table, width, flags, val);
419 if (ret)
420 return ret;
Jerome Brunetb15ee492018-02-14 14:43:39 +0100421
Martin Blumenstinglbbd7a6c2021-06-28 00:39:57 +0200422 *prate = req.best_parent_rate;
Jerome Brunetb15ee492018-02-14 14:43:39 +0100423
Martin Blumenstinglbbd7a6c2021-06-28 00:39:57 +0200424 return req.rate;
Jerome Brunetb15ee492018-02-14 14:43:39 +0100425}
426EXPORT_SYMBOL_GPL(divider_ro_round_rate_parent);
427
Stephen Boyd783d08b2021-07-01 18:10:58 -0700428static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
429 unsigned long *prate)
Stephen Boydbca96902015-01-19 18:05:29 -0800430{
431 struct clk_divider *divider = to_clk_divider(hw);
Stephen Boydbca96902015-01-19 18:05:29 -0800432
433 /* if read only, just return current value */
434 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
Jerome Brunetb15ee492018-02-14 14:43:39 +0100435 u32 val;
436
Jonas Gorski434d69f2019-04-18 13:12:04 +0200437 val = clk_div_readl(divider) >> divider->shift;
Jerome Brunetb15ee492018-02-14 14:43:39 +0100438 val &= clk_div_mask(divider->width);
439
Stephen Boyd783d08b2021-07-01 18:10:58 -0700440 return divider_ro_round_rate(hw, rate, prate, divider->table,
441 divider->width, divider->flags,
442 val);
Stephen Boydbca96902015-01-19 18:05:29 -0800443 }
444
Stephen Boyd783d08b2021-07-01 18:10:58 -0700445 return divider_round_rate(hw, rate, prate, divider->table,
446 divider->width, divider->flags);
Stephen Boydbca96902015-01-19 18:05:29 -0800447}
448
Martin Blumenstingl69a00fb2021-07-03 00:51:40 +0200449static int clk_divider_determine_rate(struct clk_hw *hw,
450 struct clk_rate_request *req)
451{
452 struct clk_divider *divider = to_clk_divider(hw);
453
454 /* if read only, just return current value */
455 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
456 u32 val;
457
458 val = clk_div_readl(divider) >> divider->shift;
459 val &= clk_div_mask(divider->width);
460
461 return divider_ro_determine_rate(hw, req, divider->table,
462 divider->width,
463 divider->flags, val);
464 }
465
466 return divider_determine_rate(hw, req, divider->table, divider->width,
467 divider->flags);
468}
469
Stephen Boydbca96902015-01-19 18:05:29 -0800470int divider_get_val(unsigned long rate, unsigned long parent_rate,
471 const struct clk_div_table *table, u8 width,
472 unsigned long flags)
473{
474 unsigned int div, value;
475
Brian Norris9556f9d2015-04-13 16:03:21 -0700476 div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
Stephen Boydbca96902015-01-19 18:05:29 -0800477
478 if (!_is_valid_div(table, div, flags))
479 return -EINVAL;
480
Jim Quinlanafe76c8f2015-05-15 15:45:47 -0400481 value = _get_val(table, div, flags, width);
Stephen Boydbca96902015-01-19 18:05:29 -0800482
Jerome Brunete6d3cc72018-02-14 14:43:33 +0100483 return min_t(unsigned int, value, clk_div_mask(width));
Stephen Boydbca96902015-01-19 18:05:29 -0800484}
485EXPORT_SYMBOL_GPL(divider_get_val);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700486
Shawn Guo1c0035d2012-04-12 20:50:18 +0800487static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
488 unsigned long parent_rate)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700489{
490 struct clk_divider *divider = to_clk_divider(hw);
Alex Frid2316a7a2017-07-25 13:18:40 +0300491 int value;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700492 unsigned long flags = 0;
493 u32 val;
494
Stephen Boydbca96902015-01-19 18:05:29 -0800495 value = divider_get_val(rate, parent_rate, divider->table,
496 divider->width, divider->flags);
Alex Frid2316a7a2017-07-25 13:18:40 +0300497 if (value < 0)
498 return value;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700499
500 if (divider->lock)
501 spin_lock_irqsave(divider->lock, flags);
Stephen Boyd661e2182015-07-24 12:21:12 -0700502 else
503 __acquire(divider->lock);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700504
Haojian Zhuangd57dfe72013-06-08 22:47:18 +0800505 if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
Jerome Brunete6d3cc72018-02-14 14:43:33 +0100506 val = clk_div_mask(divider->width) << (divider->shift + 16);
Haojian Zhuangd57dfe72013-06-08 22:47:18 +0800507 } else {
Jonas Gorski434d69f2019-04-18 13:12:04 +0200508 val = clk_div_readl(divider);
Jerome Brunete6d3cc72018-02-14 14:43:33 +0100509 val &= ~(clk_div_mask(divider->width) << divider->shift);
Haojian Zhuangd57dfe72013-06-08 22:47:18 +0800510 }
Alex Frid2316a7a2017-07-25 13:18:40 +0300511 val |= (u32)value << divider->shift;
Jonas Gorski434d69f2019-04-18 13:12:04 +0200512 clk_div_writel(divider, val);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700513
514 if (divider->lock)
515 spin_unlock_irqrestore(divider->lock, flags);
Stephen Boyd661e2182015-07-24 12:21:12 -0700516 else
517 __release(divider->lock);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700518
519 return 0;
520}
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700521
Shawn Guo822c2502012-03-27 15:23:22 +0800522const struct clk_ops clk_divider_ops = {
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700523 .recalc_rate = clk_divider_recalc_rate,
Stephen Boyd783d08b2021-07-01 18:10:58 -0700524 .round_rate = clk_divider_round_rate,
Martin Blumenstingl69a00fb2021-07-03 00:51:40 +0200525 .determine_rate = clk_divider_determine_rate,
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700526 .set_rate = clk_divider_set_rate,
527};
528EXPORT_SYMBOL_GPL(clk_divider_ops);
529
Heiko Stuebner50359812016-01-21 21:53:09 +0100530const struct clk_ops clk_divider_ro_ops = {
531 .recalc_rate = clk_divider_recalc_rate,
Stephen Boyd783d08b2021-07-01 18:10:58 -0700532 .round_rate = clk_divider_round_rate,
Martin Blumenstingl69a00fb2021-07-03 00:51:40 +0200533 .determine_rate = clk_divider_determine_rate,
Heiko Stuebner50359812016-01-21 21:53:09 +0100534};
535EXPORT_SYMBOL_GPL(clk_divider_ro_ops);
536
Stephen Boydff258812019-08-30 08:09:23 -0700537struct clk_hw *__clk_hw_register_divider(struct device *dev,
538 struct device_node *np, const char *name,
539 const char *parent_name, const struct clk_hw *parent_hw,
540 const struct clk_parent_data *parent_data, unsigned long flags,
541 void __iomem *reg, u8 shift, u8 width, u8 clk_divider_flags,
542 const struct clk_div_table *table, spinlock_t *lock)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700543{
544 struct clk_divider *div;
Stephen Boydeb7d2642016-02-06 23:26:37 -0800545 struct clk_hw *hw;
Manivannan Sadhasivamcc819cf2019-11-15 21:58:55 +0530546 struct clk_init_data init = {};
Stephen Boydeb7d2642016-02-06 23:26:37 -0800547 int ret;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700548
Haojian Zhuangd57dfe72013-06-08 22:47:18 +0800549 if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
550 if (width + shift > 16) {
551 pr_warn("divider value exceeds LOWORD field\n");
552 return ERR_PTR(-EINVAL);
553 }
554 }
555
Mike Turquette27d54592012-03-26 17:51:03 -0700556 /* allocate the divider */
Stephen Boydd122db72015-05-14 16:47:10 -0700557 div = kzalloc(sizeof(*div), GFP_KERNEL);
558 if (!div)
Mike Turquette27d54592012-03-26 17:51:03 -0700559 return ERR_PTR(-ENOMEM);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700560
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700561 init.name = name;
Heiko Stuebner50359812016-01-21 21:53:09 +0100562 if (clk_divider_flags & CLK_DIVIDER_READ_ONLY)
563 init.ops = &clk_divider_ro_ops;
564 else
565 init.ops = &clk_divider_ops;
Stephen Boyd90b6c5c2019-04-25 10:57:37 -0700566 init.flags = flags;
Michael Tretter0225dae2021-01-21 08:16:46 +0100567 init.parent_names = parent_name ? &parent_name : NULL;
568 init.parent_hws = parent_hw ? &parent_hw : NULL;
569 init.parent_data = parent_data;
570 if (parent_name || parent_hw || parent_data)
571 init.num_parents = 1;
572 else
573 init.num_parents = 0;
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700574
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700575 /* struct clk_divider assignments */
576 div->reg = reg;
577 div->shift = shift;
578 div->width = width;
579 div->flags = clk_divider_flags;
580 div->lock = lock;
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700581 div->hw.init = &init;
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530582 div->table = table;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700583
Mike Turquette27d54592012-03-26 17:51:03 -0700584 /* register the clock */
Stephen Boydeb7d2642016-02-06 23:26:37 -0800585 hw = &div->hw;
586 ret = clk_hw_register(dev, hw);
587 if (ret) {
Mike Turquette27d54592012-03-26 17:51:03 -0700588 kfree(div);
Stephen Boydeb7d2642016-02-06 23:26:37 -0800589 hw = ERR_PTR(ret);
590 }
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700591
Stephen Boydeb7d2642016-02-06 23:26:37 -0800592 return hw;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700593}
Stephen Boydff258812019-08-30 08:09:23 -0700594EXPORT_SYMBOL_GPL(__clk_hw_register_divider);
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530595
596/**
597 * clk_register_divider_table - register a table based divider clock with
598 * the clock framework
599 * @dev: device registering this clock
600 * @name: name of this clock
601 * @parent_name: name of clock's parent
602 * @flags: framework-specific flags
603 * @reg: register address to adjust divider
604 * @shift: number of bits to shift the bitfield
605 * @width: width of the bitfield
606 * @clk_divider_flags: divider-specific flags for this clock
607 * @table: array of divider/value pairs ending with a div set to 0
608 * @lock: shared register lock for this clock
609 */
610struct clk *clk_register_divider_table(struct device *dev, const char *name,
611 const char *parent_name, unsigned long flags,
612 void __iomem *reg, u8 shift, u8 width,
613 u8 clk_divider_flags, const struct clk_div_table *table,
614 spinlock_t *lock)
615{
Stephen Boydeb7d2642016-02-06 23:26:37 -0800616 struct clk_hw *hw;
617
Stephen Boydff258812019-08-30 08:09:23 -0700618 hw = __clk_hw_register_divider(dev, NULL, name, parent_name, NULL,
619 NULL, flags, reg, shift, width, clk_divider_flags,
620 table, lock);
Stephen Boydeb7d2642016-02-06 23:26:37 -0800621 if (IS_ERR(hw))
622 return ERR_CAST(hw);
623 return hw->clk;
624}
625EXPORT_SYMBOL_GPL(clk_register_divider_table);
626
Krzysztof Kozlowski4e3c0212015-01-05 10:52:40 +0100627void clk_unregister_divider(struct clk *clk)
628{
629 struct clk_divider *div;
630 struct clk_hw *hw;
631
632 hw = __clk_get_hw(clk);
633 if (!hw)
634 return;
635
636 div = to_clk_divider(hw);
637
638 clk_unregister(clk);
639 kfree(div);
640}
641EXPORT_SYMBOL_GPL(clk_unregister_divider);
Stephen Boydeb7d2642016-02-06 23:26:37 -0800642
643/**
644 * clk_hw_unregister_divider - unregister a clk divider
645 * @hw: hardware-specific clock data to unregister
646 */
647void clk_hw_unregister_divider(struct clk_hw *hw)
648{
649 struct clk_divider *div;
650
651 div = to_clk_divider(hw);
652
653 clk_hw_unregister(hw);
654 kfree(div);
655}
656EXPORT_SYMBOL_GPL(clk_hw_unregister_divider);
Michael Walle26792692020-11-08 19:51:09 +0100657
658static void devm_clk_hw_release_divider(struct device *dev, void *res)
659{
660 clk_hw_unregister_divider(*(struct clk_hw **)res);
661}
662
663struct clk_hw *__devm_clk_hw_register_divider(struct device *dev,
664 struct device_node *np, const char *name,
665 const char *parent_name, const struct clk_hw *parent_hw,
666 const struct clk_parent_data *parent_data, unsigned long flags,
667 void __iomem *reg, u8 shift, u8 width, u8 clk_divider_flags,
668 const struct clk_div_table *table, spinlock_t *lock)
669{
670 struct clk_hw **ptr, *hw;
671
672 ptr = devres_alloc(devm_clk_hw_release_divider, sizeof(*ptr), GFP_KERNEL);
673 if (!ptr)
674 return ERR_PTR(-ENOMEM);
675
676 hw = __clk_hw_register_divider(dev, np, name, parent_name, parent_hw,
677 parent_data, flags, reg, shift, width,
678 clk_divider_flags, table, lock);
679
680 if (!IS_ERR(hw)) {
681 *ptr = hw;
682 devres_add(dev, ptr);
683 } else {
684 devres_free(ptr);
685 }
686
687 return hw;
688}
689EXPORT_SYMBOL_GPL(__devm_clk_hw_register_divider);