blob: eff8a862eb0829477874e066e73273f4b0d38621 [file] [log] [blame]
Mike Turquette9d9f78e2012-03-15 23:11:20 -07001/*
2 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
3 * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
4 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Adjustable divider clock implementation
11 */
12
13#include <linux/clk-provider.h>
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/io.h>
17#include <linux/err.h>
18#include <linux/string.h>
James Hogan1a3cd182013-01-15 10:28:05 +000019#include <linux/log2.h>
Mike Turquette9d9f78e2012-03-15 23:11:20 -070020
21/*
22 * DOC: basic adjustable divider clock that cannot gate
23 *
24 * Traits of this clock:
25 * prepare - clk_prepare only ensures that parents are prepared
26 * enable - clk_enable only ensures that parents are enabled
Tomi Valkeinenb11d2822014-02-13 12:03:59 +020027 * rate - rate is adjustable. clk->rate = DIV_ROUND_UP(parent->rate / divisor)
Mike Turquette9d9f78e2012-03-15 23:11:20 -070028 * parent - fixed parent. No clk_set_parent support
29 */
30
31#define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)
32
Stephen Boydbca96902015-01-19 18:05:29 -080033#define div_mask(width) ((1 << (width)) - 1)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +053034
Rajendra Nayak357c3f02012-06-29 19:06:32 +053035static unsigned int _get_table_maxdiv(const struct clk_div_table *table)
36{
37 unsigned int maxdiv = 0;
38 const struct clk_div_table *clkt;
39
40 for (clkt = table; clkt->div; clkt++)
41 if (clkt->div > maxdiv)
42 maxdiv = clkt->div;
43 return maxdiv;
44}
45
Maxime COQUELIN774b5142014-01-29 17:24:07 +010046static unsigned int _get_table_mindiv(const struct clk_div_table *table)
47{
48 unsigned int mindiv = UINT_MAX;
49 const struct clk_div_table *clkt;
50
51 for (clkt = table; clkt->div; clkt++)
52 if (clkt->div < mindiv)
53 mindiv = clkt->div;
54 return mindiv;
55}
56
Stephen Boydbca96902015-01-19 18:05:29 -080057static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width,
58 unsigned long flags)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +053059{
Stephen Boydbca96902015-01-19 18:05:29 -080060 if (flags & CLK_DIVIDER_ONE_BASED)
61 return div_mask(width);
62 if (flags & CLK_DIVIDER_POWER_OF_TWO)
63 return 1 << div_mask(width);
64 if (table)
65 return _get_table_maxdiv(table);
66 return div_mask(width) + 1;
Rajendra Nayak6d9252b2012-05-17 15:52:13 +053067}
68
Rajendra Nayak357c3f02012-06-29 19:06:32 +053069static unsigned int _get_table_div(const struct clk_div_table *table,
70 unsigned int val)
71{
72 const struct clk_div_table *clkt;
73
74 for (clkt = table; clkt->div; clkt++)
75 if (clkt->val == val)
76 return clkt->div;
77 return 0;
78}
79
Stephen Boydbca96902015-01-19 18:05:29 -080080static unsigned int _get_div(const struct clk_div_table *table,
81 unsigned int val, unsigned long flags)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +053082{
Stephen Boydbca96902015-01-19 18:05:29 -080083 if (flags & CLK_DIVIDER_ONE_BASED)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +053084 return val;
Stephen Boydbca96902015-01-19 18:05:29 -080085 if (flags & CLK_DIVIDER_POWER_OF_TWO)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +053086 return 1 << val;
Stephen Boydbca96902015-01-19 18:05:29 -080087 if (table)
88 return _get_table_div(table, val);
Rajendra Nayak6d9252b2012-05-17 15:52:13 +053089 return val + 1;
90}
91
Rajendra Nayak357c3f02012-06-29 19:06:32 +053092static unsigned int _get_table_val(const struct clk_div_table *table,
93 unsigned int div)
94{
95 const struct clk_div_table *clkt;
96
97 for (clkt = table; clkt->div; clkt++)
98 if (clkt->div == div)
99 return clkt->val;
100 return 0;
101}
102
Stephen Boydbca96902015-01-19 18:05:29 -0800103static unsigned int _get_val(const struct clk_div_table *table,
104 unsigned int div, unsigned long flags)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530105{
Stephen Boydbca96902015-01-19 18:05:29 -0800106 if (flags & CLK_DIVIDER_ONE_BASED)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530107 return div;
Stephen Boydbca96902015-01-19 18:05:29 -0800108 if (flags & CLK_DIVIDER_POWER_OF_TWO)
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530109 return __ffs(div);
Stephen Boydbca96902015-01-19 18:05:29 -0800110 if (table)
111 return _get_table_val(table, div);
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530112 return div - 1;
113}
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700114
Stephen Boydbca96902015-01-19 18:05:29 -0800115unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate,
116 unsigned int val,
117 const struct clk_div_table *table,
118 unsigned long flags)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700119{
Stephen Boydbca96902015-01-19 18:05:29 -0800120 unsigned int div;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700121
Stephen Boydbca96902015-01-19 18:05:29 -0800122 div = _get_div(table, val, flags);
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530123 if (!div) {
Stephen Boydbca96902015-01-19 18:05:29 -0800124 WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO),
Soren Brinkmann056b20532013-04-02 15:36:56 -0700125 "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
126 __clk_get_name(hw->clk));
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530127 return parent_rate;
128 }
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700129
Tomi Valkeinenb11d2822014-02-13 12:03:59 +0200130 return DIV_ROUND_UP(parent_rate, div);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700131}
Stephen Boydbca96902015-01-19 18:05:29 -0800132EXPORT_SYMBOL_GPL(divider_recalc_rate);
133
134static unsigned long clk_divider_recalc_rate(struct clk_hw *hw,
135 unsigned long parent_rate)
136{
137 struct clk_divider *divider = to_clk_divider(hw);
138 unsigned int val;
139
140 val = clk_readl(divider->reg) >> divider->shift;
141 val &= div_mask(divider->width);
142
143 return divider_recalc_rate(hw, parent_rate, val, divider->table,
144 divider->flags);
145}
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700146
147/*
148 * The reverse of DIV_ROUND_UP: The maximum number which
149 * divided by m is r
150 */
151#define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1)
152
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530153static bool _is_valid_table_div(const struct clk_div_table *table,
154 unsigned int div)
155{
156 const struct clk_div_table *clkt;
157
158 for (clkt = table; clkt->div; clkt++)
159 if (clkt->div == div)
160 return true;
161 return false;
162}
163
Stephen Boydbca96902015-01-19 18:05:29 -0800164static bool _is_valid_div(const struct clk_div_table *table, unsigned int div,
165 unsigned long flags)
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530166{
Stephen Boydbca96902015-01-19 18:05:29 -0800167 if (flags & CLK_DIVIDER_POWER_OF_TWO)
James Hogan1a3cd182013-01-15 10:28:05 +0000168 return is_power_of_2(div);
Stephen Boydbca96902015-01-19 18:05:29 -0800169 if (table)
170 return _is_valid_table_div(table, div);
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530171 return true;
172}
173
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100174static int _round_up_table(const struct clk_div_table *table, int div)
175{
176 const struct clk_div_table *clkt;
Maxime COQUELINfe52e752014-05-07 18:48:52 +0200177 int up = INT_MAX;
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100178
179 for (clkt = table; clkt->div; clkt++) {
180 if (clkt->div == div)
181 return clkt->div;
182 else if (clkt->div < div)
183 continue;
184
185 if ((clkt->div - div) < (up - div))
186 up = clkt->div;
187 }
188
189 return up;
190}
191
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100192static int _round_down_table(const struct clk_div_table *table, int div)
193{
194 const struct clk_div_table *clkt;
195 int down = _get_table_mindiv(table);
196
197 for (clkt = table; clkt->div; clkt++) {
198 if (clkt->div == div)
199 return clkt->div;
200 else if (clkt->div > div)
201 continue;
202
203 if ((div - clkt->div) < (div - down))
204 down = clkt->div;
205 }
206
207 return down;
208}
209
Stephen Boydbca96902015-01-19 18:05:29 -0800210static int _div_round_up(const struct clk_div_table *table,
211 unsigned long parent_rate, unsigned long rate,
212 unsigned long flags)
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100213{
214 int div = DIV_ROUND_UP(parent_rate, rate);
215
Stephen Boydbca96902015-01-19 18:05:29 -0800216 if (flags & CLK_DIVIDER_POWER_OF_TWO)
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100217 div = __roundup_pow_of_two(div);
Stephen Boydbca96902015-01-19 18:05:29 -0800218 if (table)
219 div = _round_up_table(table, div);
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100220
221 return div;
222}
223
Stephen Boydbca96902015-01-19 18:05:29 -0800224static int _div_round_closest(const struct clk_div_table *table,
225 unsigned long parent_rate, unsigned long rate,
226 unsigned long flags)
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100227{
228 int up, down, div;
229
230 up = down = div = DIV_ROUND_CLOSEST(parent_rate, rate);
231
Stephen Boydbca96902015-01-19 18:05:29 -0800232 if (flags & CLK_DIVIDER_POWER_OF_TWO) {
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100233 up = __roundup_pow_of_two(div);
234 down = __rounddown_pow_of_two(div);
Stephen Boydbca96902015-01-19 18:05:29 -0800235 } else if (table) {
236 up = _round_up_table(table, div);
237 down = _round_down_table(table, div);
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100238 }
239
240 return (up - div) <= (div - down) ? up : down;
241}
242
Stephen Boydbca96902015-01-19 18:05:29 -0800243static int _div_round(const struct clk_div_table *table,
244 unsigned long parent_rate, unsigned long rate,
245 unsigned long flags)
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100246{
Stephen Boydbca96902015-01-19 18:05:29 -0800247 if (flags & CLK_DIVIDER_ROUND_CLOSEST)
248 return _div_round_closest(table, parent_rate, rate, flags);
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100249
Stephen Boydbca96902015-01-19 18:05:29 -0800250 return _div_round_up(table, parent_rate, rate, flags);
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100251}
252
Stephen Boydbca96902015-01-19 18:05:29 -0800253static bool _is_best_div(unsigned long rate, unsigned long now,
254 unsigned long best, unsigned long flags)
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100255{
Stephen Boydbca96902015-01-19 18:05:29 -0800256 if (flags & CLK_DIVIDER_ROUND_CLOSEST)
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100257 return abs(rate - now) < abs(rate - best);
258
259 return now <= rate && now > best;
260}
261
Stephen Boydbca96902015-01-19 18:05:29 -0800262static int _next_div(const struct clk_div_table *table, int div,
263 unsigned long flags)
Maxime COQUELIN0e2de782014-01-29 17:24:08 +0100264{
265 div++;
266
Stephen Boydbca96902015-01-19 18:05:29 -0800267 if (flags & CLK_DIVIDER_POWER_OF_TWO)
Maxime COQUELIN0e2de782014-01-29 17:24:08 +0100268 return __roundup_pow_of_two(div);
Stephen Boydbca96902015-01-19 18:05:29 -0800269 if (table)
270 return _round_up_table(table, div);
Maxime COQUELIN0e2de782014-01-29 17:24:08 +0100271
272 return div;
273}
274
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700275static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
Stephen Boydbca96902015-01-19 18:05:29 -0800276 unsigned long *best_parent_rate,
277 const struct clk_div_table *table, u8 width,
278 unsigned long flags)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700279{
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700280 int i, bestdiv = 0;
281 unsigned long parent_rate, best = 0, now, maxdiv;
Shawn Guo081c9022013-06-02 22:20:55 +0800282 unsigned long parent_rate_saved = *best_parent_rate;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700283
284 if (!rate)
285 rate = 1;
286
Stephen Boydbca96902015-01-19 18:05:29 -0800287 maxdiv = _get_maxdiv(table, width, flags);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700288
Shawn Guo81536e02012-04-12 20:50:17 +0800289 if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) {
290 parent_rate = *best_parent_rate;
Stephen Boydbca96902015-01-19 18:05:29 -0800291 bestdiv = _div_round(table, parent_rate, rate, flags);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700292 bestdiv = bestdiv == 0 ? 1 : bestdiv;
293 bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
294 return bestdiv;
295 }
296
297 /*
298 * The maximum divider we can use without overflowing
299 * unsigned long in rate * i below
300 */
301 maxdiv = min(ULONG_MAX / rate, maxdiv);
302
Stephen Boydbca96902015-01-19 18:05:29 -0800303 for (i = 1; i <= maxdiv; i = _next_div(table, i, flags)) {
304 if (!_is_valid_div(table, i, flags))
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530305 continue;
Shawn Guo081c9022013-06-02 22:20:55 +0800306 if (rate * i == parent_rate_saved) {
307 /*
308 * It's the most ideal case if the requested rate can be
309 * divided from parent clock without needing to change
310 * parent rate, so return the divider immediately.
311 */
312 *best_parent_rate = parent_rate_saved;
313 return i;
314 }
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700315 parent_rate = __clk_round_rate(__clk_get_parent(hw->clk),
316 MULT_ROUND_UP(rate, i));
Tomi Valkeinenb11d2822014-02-13 12:03:59 +0200317 now = DIV_ROUND_UP(parent_rate, i);
Stephen Boydbca96902015-01-19 18:05:29 -0800318 if (_is_best_div(rate, now, best, flags)) {
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700319 bestdiv = i;
320 best = now;
321 *best_parent_rate = parent_rate;
322 }
323 }
324
325 if (!bestdiv) {
Stephen Boydbca96902015-01-19 18:05:29 -0800326 bestdiv = _get_maxdiv(table, width, flags);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700327 *best_parent_rate = __clk_round_rate(__clk_get_parent(hw->clk), 1);
328 }
329
330 return bestdiv;
331}
332
Stephen Boydbca96902015-01-19 18:05:29 -0800333long divider_round_rate(struct clk_hw *hw, unsigned long rate,
334 unsigned long *prate, const struct clk_div_table *table,
335 u8 width, unsigned long flags)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700336{
337 int div;
Stephen Boydbca96902015-01-19 18:05:29 -0800338
339 div = clk_divider_bestdiv(hw, rate, prate, table, width, flags);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700340
Tomi Valkeinenb11d2822014-02-13 12:03:59 +0200341 return DIV_ROUND_UP(*prate, div);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700342}
Stephen Boydbca96902015-01-19 18:05:29 -0800343EXPORT_SYMBOL_GPL(divider_round_rate);
344
345static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
346 unsigned long *prate)
347{
348 struct clk_divider *divider = to_clk_divider(hw);
349 int bestdiv;
350
351 /* if read only, just return current value */
352 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
353 bestdiv = readl(divider->reg) >> divider->shift;
354 bestdiv &= div_mask(divider->width);
355 bestdiv = _get_div(divider->table, bestdiv, divider->flags);
Heiko Stübner2f7bf4a2015-02-24 11:39:25 +0100356 return DIV_ROUND_UP(*prate, bestdiv);
Stephen Boydbca96902015-01-19 18:05:29 -0800357 }
358
359 return divider_round_rate(hw, rate, prate, divider->table,
360 divider->width, divider->flags);
361}
362
363int divider_get_val(unsigned long rate, unsigned long parent_rate,
364 const struct clk_div_table *table, u8 width,
365 unsigned long flags)
366{
367 unsigned int div, value;
368
369 div = DIV_ROUND_UP(parent_rate, rate);
370
371 if (!_is_valid_div(table, div, flags))
372 return -EINVAL;
373
374 value = _get_val(table, div, flags);
375
376 return min_t(unsigned int, value, div_mask(width));
377}
378EXPORT_SYMBOL_GPL(divider_get_val);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700379
Shawn Guo1c0035d2012-04-12 20:50:18 +0800380static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
381 unsigned long parent_rate)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700382{
383 struct clk_divider *divider = to_clk_divider(hw);
Stephen Boydbca96902015-01-19 18:05:29 -0800384 unsigned int value;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700385 unsigned long flags = 0;
386 u32 val;
387
Stephen Boydbca96902015-01-19 18:05:29 -0800388 value = divider_get_val(rate, parent_rate, divider->table,
389 divider->width, divider->flags);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700390
391 if (divider->lock)
392 spin_lock_irqsave(divider->lock, flags);
393
Haojian Zhuangd57dfe72013-06-08 22:47:18 +0800394 if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
Stephen Boydbca96902015-01-19 18:05:29 -0800395 val = div_mask(divider->width) << (divider->shift + 16);
Haojian Zhuangd57dfe72013-06-08 22:47:18 +0800396 } else {
Gerhard Sittigaa514ce2013-07-22 14:14:40 +0200397 val = clk_readl(divider->reg);
Stephen Boydbca96902015-01-19 18:05:29 -0800398 val &= ~(div_mask(divider->width) << divider->shift);
Haojian Zhuangd57dfe72013-06-08 22:47:18 +0800399 }
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530400 val |= value << divider->shift;
Gerhard Sittigaa514ce2013-07-22 14:14:40 +0200401 clk_writel(val, divider->reg);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700402
403 if (divider->lock)
404 spin_unlock_irqrestore(divider->lock, flags);
405
406 return 0;
407}
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700408
Shawn Guo822c2502012-03-27 15:23:22 +0800409const struct clk_ops clk_divider_ops = {
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700410 .recalc_rate = clk_divider_recalc_rate,
411 .round_rate = clk_divider_round_rate,
412 .set_rate = clk_divider_set_rate,
413};
414EXPORT_SYMBOL_GPL(clk_divider_ops);
415
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530416static struct clk *_register_divider(struct device *dev, const char *name,
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700417 const char *parent_name, unsigned long flags,
418 void __iomem *reg, u8 shift, u8 width,
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530419 u8 clk_divider_flags, const struct clk_div_table *table,
420 spinlock_t *lock)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700421{
422 struct clk_divider *div;
423 struct clk *clk;
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700424 struct clk_init_data init;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700425
Haojian Zhuangd57dfe72013-06-08 22:47:18 +0800426 if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
427 if (width + shift > 16) {
428 pr_warn("divider value exceeds LOWORD field\n");
429 return ERR_PTR(-EINVAL);
430 }
431 }
432
Mike Turquette27d54592012-03-26 17:51:03 -0700433 /* allocate the divider */
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700434 div = kzalloc(sizeof(struct clk_divider), GFP_KERNEL);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700435 if (!div) {
436 pr_err("%s: could not allocate divider clk\n", __func__);
Mike Turquette27d54592012-03-26 17:51:03 -0700437 return ERR_PTR(-ENOMEM);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700438 }
439
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700440 init.name = name;
James Hogane6d5e7d2014-11-14 15:32:09 +0000441 init.ops = &clk_divider_ops;
Rajendra Nayakf7d8caa2012-06-01 14:02:47 +0530442 init.flags = flags | CLK_IS_BASIC;
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700443 init.parent_names = (parent_name ? &parent_name: NULL);
444 init.num_parents = (parent_name ? 1 : 0);
445
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700446 /* struct clk_divider assignments */
447 div->reg = reg;
448 div->shift = shift;
449 div->width = width;
450 div->flags = clk_divider_flags;
451 div->lock = lock;
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700452 div->hw.init = &init;
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530453 div->table = table;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700454
Mike Turquette27d54592012-03-26 17:51:03 -0700455 /* register the clock */
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700456 clk = clk_register(dev, &div->hw);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700457
Mike Turquette27d54592012-03-26 17:51:03 -0700458 if (IS_ERR(clk))
459 kfree(div);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700460
Mike Turquette27d54592012-03-26 17:51:03 -0700461 return clk;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700462}
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530463
464/**
465 * clk_register_divider - register a divider clock with the clock framework
466 * @dev: device registering this clock
467 * @name: name of this clock
468 * @parent_name: name of clock's parent
469 * @flags: framework-specific flags
470 * @reg: register address to adjust divider
471 * @shift: number of bits to shift the bitfield
472 * @width: width of the bitfield
473 * @clk_divider_flags: divider-specific flags for this clock
474 * @lock: shared register lock for this clock
475 */
476struct clk *clk_register_divider(struct device *dev, const char *name,
477 const char *parent_name, unsigned long flags,
478 void __iomem *reg, u8 shift, u8 width,
479 u8 clk_divider_flags, spinlock_t *lock)
480{
481 return _register_divider(dev, name, parent_name, flags, reg, shift,
482 width, clk_divider_flags, NULL, lock);
483}
Fabio Estevam4c5eeea2013-08-02 13:14:07 -0300484EXPORT_SYMBOL_GPL(clk_register_divider);
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530485
486/**
487 * clk_register_divider_table - register a table based divider clock with
488 * the clock framework
489 * @dev: device registering this clock
490 * @name: name of this clock
491 * @parent_name: name of clock's parent
492 * @flags: framework-specific flags
493 * @reg: register address to adjust divider
494 * @shift: number of bits to shift the bitfield
495 * @width: width of the bitfield
496 * @clk_divider_flags: divider-specific flags for this clock
497 * @table: array of divider/value pairs ending with a div set to 0
498 * @lock: shared register lock for this clock
499 */
500struct clk *clk_register_divider_table(struct device *dev, const char *name,
501 const char *parent_name, unsigned long flags,
502 void __iomem *reg, u8 shift, u8 width,
503 u8 clk_divider_flags, const struct clk_div_table *table,
504 spinlock_t *lock)
505{
506 return _register_divider(dev, name, parent_name, flags, reg, shift,
507 width, clk_divider_flags, table, lock);
508}
Fabio Estevam4c5eeea2013-08-02 13:14:07 -0300509EXPORT_SYMBOL_GPL(clk_register_divider_table);
Krzysztof Kozlowski4e3c0212015-01-05 10:52:40 +0100510
511void clk_unregister_divider(struct clk *clk)
512{
513 struct clk_divider *div;
514 struct clk_hw *hw;
515
516 hw = __clk_get_hw(clk);
517 if (!hw)
518 return;
519
520 div = to_clk_divider(hw);
521
522 clk_unregister(clk);
523 kfree(div);
524}
525EXPORT_SYMBOL_GPL(clk_unregister_divider);