blob: 78b2e656ff6a6fbe9ed6759435f27bea39018484 [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
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530147static bool _is_valid_table_div(const struct clk_div_table *table,
148 unsigned int div)
149{
150 const struct clk_div_table *clkt;
151
152 for (clkt = table; clkt->div; clkt++)
153 if (clkt->div == div)
154 return true;
155 return false;
156}
157
Stephen Boydbca96902015-01-19 18:05:29 -0800158static bool _is_valid_div(const struct clk_div_table *table, unsigned int div,
159 unsigned long flags)
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530160{
Stephen Boydbca96902015-01-19 18:05:29 -0800161 if (flags & CLK_DIVIDER_POWER_OF_TWO)
James Hogan1a3cd182013-01-15 10:28:05 +0000162 return is_power_of_2(div);
Stephen Boydbca96902015-01-19 18:05:29 -0800163 if (table)
164 return _is_valid_table_div(table, div);
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530165 return true;
166}
167
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100168static int _round_up_table(const struct clk_div_table *table, int div)
169{
170 const struct clk_div_table *clkt;
Maxime COQUELINfe52e752014-05-07 18:48:52 +0200171 int up = INT_MAX;
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100172
173 for (clkt = table; clkt->div; clkt++) {
174 if (clkt->div == div)
175 return clkt->div;
176 else if (clkt->div < div)
177 continue;
178
179 if ((clkt->div - div) < (up - div))
180 up = clkt->div;
181 }
182
183 return up;
184}
185
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100186static int _round_down_table(const struct clk_div_table *table, int div)
187{
188 const struct clk_div_table *clkt;
189 int down = _get_table_mindiv(table);
190
191 for (clkt = table; clkt->div; clkt++) {
192 if (clkt->div == div)
193 return clkt->div;
194 else if (clkt->div > div)
195 continue;
196
197 if ((div - clkt->div) < (div - down))
198 down = clkt->div;
199 }
200
201 return down;
202}
203
Stephen Boydbca96902015-01-19 18:05:29 -0800204static int _div_round_up(const struct clk_div_table *table,
205 unsigned long parent_rate, unsigned long rate,
206 unsigned long flags)
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100207{
208 int div = DIV_ROUND_UP(parent_rate, rate);
209
Stephen Boydbca96902015-01-19 18:05:29 -0800210 if (flags & CLK_DIVIDER_POWER_OF_TWO)
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100211 div = __roundup_pow_of_two(div);
Stephen Boydbca96902015-01-19 18:05:29 -0800212 if (table)
213 div = _round_up_table(table, div);
Maxime COQUELINdd23c2c2014-01-29 17:24:06 +0100214
215 return div;
216}
217
Stephen Boydbca96902015-01-19 18:05:29 -0800218static int _div_round_closest(const struct clk_div_table *table,
219 unsigned long parent_rate, unsigned long rate,
220 unsigned long flags)
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100221{
222 int up, down, div;
Uwe Kleine-König26bac952015-02-21 11:40:24 +0100223 unsigned long up_rate, down_rate;
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100224
225 up = down = div = DIV_ROUND_CLOSEST(parent_rate, rate);
226
Stephen Boydbca96902015-01-19 18:05:29 -0800227 if (flags & CLK_DIVIDER_POWER_OF_TWO) {
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100228 up = __roundup_pow_of_two(div);
229 down = __rounddown_pow_of_two(div);
Stephen Boydbca96902015-01-19 18:05:29 -0800230 } else if (table) {
231 up = _round_up_table(table, div);
232 down = _round_down_table(table, div);
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100233 }
234
Uwe Kleine-König26bac952015-02-21 11:40:24 +0100235 up_rate = DIV_ROUND_UP(parent_rate, up);
236 down_rate = DIV_ROUND_UP(parent_rate, down);
237
238 return (rate - up_rate) <= (down_rate - rate) ? up : down;
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100239}
240
Stephen Boydbca96902015-01-19 18:05:29 -0800241static int _div_round(const struct clk_div_table *table,
242 unsigned long parent_rate, unsigned long rate,
243 unsigned long flags)
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100244{
Stephen Boydbca96902015-01-19 18:05:29 -0800245 if (flags & CLK_DIVIDER_ROUND_CLOSEST)
246 return _div_round_closest(table, parent_rate, rate, flags);
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100247
Stephen Boydbca96902015-01-19 18:05:29 -0800248 return _div_round_up(table, parent_rate, rate, flags);
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100249}
250
Stephen Boydbca96902015-01-19 18:05:29 -0800251static bool _is_best_div(unsigned long rate, unsigned long now,
252 unsigned long best, unsigned long flags)
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100253{
Stephen Boydbca96902015-01-19 18:05:29 -0800254 if (flags & CLK_DIVIDER_ROUND_CLOSEST)
Maxime COQUELIN774b5142014-01-29 17:24:07 +0100255 return abs(rate - now) < abs(rate - best);
256
257 return now <= rate && now > best;
258}
259
Stephen Boydbca96902015-01-19 18:05:29 -0800260static int _next_div(const struct clk_div_table *table, int div,
261 unsigned long flags)
Maxime COQUELIN0e2de782014-01-29 17:24:08 +0100262{
263 div++;
264
Stephen Boydbca96902015-01-19 18:05:29 -0800265 if (flags & CLK_DIVIDER_POWER_OF_TWO)
Maxime COQUELIN0e2de782014-01-29 17:24:08 +0100266 return __roundup_pow_of_two(div);
Stephen Boydbca96902015-01-19 18:05:29 -0800267 if (table)
268 return _round_up_table(table, div);
Maxime COQUELIN0e2de782014-01-29 17:24:08 +0100269
270 return div;
271}
272
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700273static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
Stephen Boydbca96902015-01-19 18:05:29 -0800274 unsigned long *best_parent_rate,
275 const struct clk_div_table *table, u8 width,
276 unsigned long flags)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700277{
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700278 int i, bestdiv = 0;
279 unsigned long parent_rate, best = 0, now, maxdiv;
Shawn Guo081c9022013-06-02 22:20:55 +0800280 unsigned long parent_rate_saved = *best_parent_rate;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700281
282 if (!rate)
283 rate = 1;
284
Stephen Boydbca96902015-01-19 18:05:29 -0800285 maxdiv = _get_maxdiv(table, width, flags);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700286
Shawn Guo81536e02012-04-12 20:50:17 +0800287 if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) {
288 parent_rate = *best_parent_rate;
Stephen Boydbca96902015-01-19 18:05:29 -0800289 bestdiv = _div_round(table, parent_rate, rate, flags);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700290 bestdiv = bestdiv == 0 ? 1 : bestdiv;
291 bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
292 return bestdiv;
293 }
294
295 /*
296 * The maximum divider we can use without overflowing
297 * unsigned long in rate * i below
298 */
299 maxdiv = min(ULONG_MAX / rate, maxdiv);
300
Stephen Boydbca96902015-01-19 18:05:29 -0800301 for (i = 1; i <= maxdiv; i = _next_div(table, i, flags)) {
302 if (!_is_valid_div(table, i, flags))
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530303 continue;
Shawn Guo081c9022013-06-02 22:20:55 +0800304 if (rate * i == parent_rate_saved) {
305 /*
306 * It's the most ideal case if the requested rate can be
307 * divided from parent clock without needing to change
308 * parent rate, so return the divider immediately.
309 */
310 *best_parent_rate = parent_rate_saved;
311 return i;
312 }
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700313 parent_rate = __clk_round_rate(__clk_get_parent(hw->clk),
Uwe Kleine-Königda321132015-02-21 11:40:23 +0100314 rate * i);
Tomi Valkeinenb11d2822014-02-13 12:03:59 +0200315 now = DIV_ROUND_UP(parent_rate, i);
Stephen Boydbca96902015-01-19 18:05:29 -0800316 if (_is_best_div(rate, now, best, flags)) {
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700317 bestdiv = i;
318 best = now;
319 *best_parent_rate = parent_rate;
320 }
321 }
322
323 if (!bestdiv) {
Stephen Boydbca96902015-01-19 18:05:29 -0800324 bestdiv = _get_maxdiv(table, width, flags);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700325 *best_parent_rate = __clk_round_rate(__clk_get_parent(hw->clk), 1);
326 }
327
328 return bestdiv;
329}
330
Stephen Boydbca96902015-01-19 18:05:29 -0800331long divider_round_rate(struct clk_hw *hw, unsigned long rate,
332 unsigned long *prate, const struct clk_div_table *table,
333 u8 width, unsigned long flags)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700334{
335 int div;
Stephen Boydbca96902015-01-19 18:05:29 -0800336
337 div = clk_divider_bestdiv(hw, rate, prate, table, width, flags);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700338
Tomi Valkeinenb11d2822014-02-13 12:03:59 +0200339 return DIV_ROUND_UP(*prate, div);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700340}
Stephen Boydbca96902015-01-19 18:05:29 -0800341EXPORT_SYMBOL_GPL(divider_round_rate);
342
343static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
344 unsigned long *prate)
345{
346 struct clk_divider *divider = to_clk_divider(hw);
347 int bestdiv;
348
349 /* if read only, just return current value */
350 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
351 bestdiv = readl(divider->reg) >> divider->shift;
352 bestdiv &= div_mask(divider->width);
353 bestdiv = _get_div(divider->table, bestdiv, divider->flags);
Heiko Stübner2f7bf4a2015-02-24 11:39:25 +0100354 return DIV_ROUND_UP(*prate, bestdiv);
Stephen Boydbca96902015-01-19 18:05:29 -0800355 }
356
357 return divider_round_rate(hw, rate, prate, divider->table,
358 divider->width, divider->flags);
359}
360
361int divider_get_val(unsigned long rate, unsigned long parent_rate,
362 const struct clk_div_table *table, u8 width,
363 unsigned long flags)
364{
365 unsigned int div, value;
366
367 div = DIV_ROUND_UP(parent_rate, rate);
368
369 if (!_is_valid_div(table, div, flags))
370 return -EINVAL;
371
372 value = _get_val(table, div, flags);
373
374 return min_t(unsigned int, value, div_mask(width));
375}
376EXPORT_SYMBOL_GPL(divider_get_val);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700377
Shawn Guo1c0035d2012-04-12 20:50:18 +0800378static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
379 unsigned long parent_rate)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700380{
381 struct clk_divider *divider = to_clk_divider(hw);
Stephen Boydbca96902015-01-19 18:05:29 -0800382 unsigned int value;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700383 unsigned long flags = 0;
384 u32 val;
385
Stephen Boydbca96902015-01-19 18:05:29 -0800386 value = divider_get_val(rate, parent_rate, divider->table,
387 divider->width, divider->flags);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700388
389 if (divider->lock)
390 spin_lock_irqsave(divider->lock, flags);
391
Haojian Zhuangd57dfe72013-06-08 22:47:18 +0800392 if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
Stephen Boydbca96902015-01-19 18:05:29 -0800393 val = div_mask(divider->width) << (divider->shift + 16);
Haojian Zhuangd57dfe72013-06-08 22:47:18 +0800394 } else {
Gerhard Sittigaa514ce2013-07-22 14:14:40 +0200395 val = clk_readl(divider->reg);
Stephen Boydbca96902015-01-19 18:05:29 -0800396 val &= ~(div_mask(divider->width) << divider->shift);
Haojian Zhuangd57dfe72013-06-08 22:47:18 +0800397 }
Rajendra Nayak6d9252b2012-05-17 15:52:13 +0530398 val |= value << divider->shift;
Gerhard Sittigaa514ce2013-07-22 14:14:40 +0200399 clk_writel(val, divider->reg);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700400
401 if (divider->lock)
402 spin_unlock_irqrestore(divider->lock, flags);
403
404 return 0;
405}
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700406
Shawn Guo822c2502012-03-27 15:23:22 +0800407const struct clk_ops clk_divider_ops = {
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700408 .recalc_rate = clk_divider_recalc_rate,
409 .round_rate = clk_divider_round_rate,
410 .set_rate = clk_divider_set_rate,
411};
412EXPORT_SYMBOL_GPL(clk_divider_ops);
413
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530414static struct clk *_register_divider(struct device *dev, const char *name,
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700415 const char *parent_name, unsigned long flags,
416 void __iomem *reg, u8 shift, u8 width,
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530417 u8 clk_divider_flags, const struct clk_div_table *table,
418 spinlock_t *lock)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700419{
420 struct clk_divider *div;
421 struct clk *clk;
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700422 struct clk_init_data init;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700423
Haojian Zhuangd57dfe72013-06-08 22:47:18 +0800424 if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
425 if (width + shift > 16) {
426 pr_warn("divider value exceeds LOWORD field\n");
427 return ERR_PTR(-EINVAL);
428 }
429 }
430
Mike Turquette27d54592012-03-26 17:51:03 -0700431 /* allocate the divider */
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700432 div = kzalloc(sizeof(struct clk_divider), GFP_KERNEL);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700433 if (!div) {
434 pr_err("%s: could not allocate divider clk\n", __func__);
Mike Turquette27d54592012-03-26 17:51:03 -0700435 return ERR_PTR(-ENOMEM);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700436 }
437
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700438 init.name = name;
James Hogane6d5e7d2014-11-14 15:32:09 +0000439 init.ops = &clk_divider_ops;
Rajendra Nayakf7d8caa2012-06-01 14:02:47 +0530440 init.flags = flags | CLK_IS_BASIC;
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700441 init.parent_names = (parent_name ? &parent_name: NULL);
442 init.num_parents = (parent_name ? 1 : 0);
443
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700444 /* struct clk_divider assignments */
445 div->reg = reg;
446 div->shift = shift;
447 div->width = width;
448 div->flags = clk_divider_flags;
449 div->lock = lock;
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700450 div->hw.init = &init;
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530451 div->table = table;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700452
Mike Turquette27d54592012-03-26 17:51:03 -0700453 /* register the clock */
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700454 clk = clk_register(dev, &div->hw);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700455
Mike Turquette27d54592012-03-26 17:51:03 -0700456 if (IS_ERR(clk))
457 kfree(div);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700458
Mike Turquette27d54592012-03-26 17:51:03 -0700459 return clk;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700460}
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530461
462/**
463 * clk_register_divider - register a divider clock with the clock framework
464 * @dev: device registering this clock
465 * @name: name of this clock
466 * @parent_name: name of clock's parent
467 * @flags: framework-specific flags
468 * @reg: register address to adjust divider
469 * @shift: number of bits to shift the bitfield
470 * @width: width of the bitfield
471 * @clk_divider_flags: divider-specific flags for this clock
472 * @lock: shared register lock for this clock
473 */
474struct clk *clk_register_divider(struct device *dev, const char *name,
475 const char *parent_name, unsigned long flags,
476 void __iomem *reg, u8 shift, u8 width,
477 u8 clk_divider_flags, spinlock_t *lock)
478{
479 return _register_divider(dev, name, parent_name, flags, reg, shift,
480 width, clk_divider_flags, NULL, lock);
481}
Fabio Estevam4c5eeea2013-08-02 13:14:07 -0300482EXPORT_SYMBOL_GPL(clk_register_divider);
Rajendra Nayak357c3f02012-06-29 19:06:32 +0530483
484/**
485 * clk_register_divider_table - register a table based divider clock with
486 * the clock framework
487 * @dev: device registering this clock
488 * @name: name of this clock
489 * @parent_name: name of clock's parent
490 * @flags: framework-specific flags
491 * @reg: register address to adjust divider
492 * @shift: number of bits to shift the bitfield
493 * @width: width of the bitfield
494 * @clk_divider_flags: divider-specific flags for this clock
495 * @table: array of divider/value pairs ending with a div set to 0
496 * @lock: shared register lock for this clock
497 */
498struct clk *clk_register_divider_table(struct device *dev, const char *name,
499 const char *parent_name, unsigned long flags,
500 void __iomem *reg, u8 shift, u8 width,
501 u8 clk_divider_flags, const struct clk_div_table *table,
502 spinlock_t *lock)
503{
504 return _register_divider(dev, name, parent_name, flags, reg, shift,
505 width, clk_divider_flags, table, lock);
506}
Fabio Estevam4c5eeea2013-08-02 13:14:07 -0300507EXPORT_SYMBOL_GPL(clk_register_divider_table);
Krzysztof Kozlowski4e3c0212015-01-05 10:52:40 +0100508
509void clk_unregister_divider(struct clk *clk)
510{
511 struct clk_divider *div;
512 struct clk_hw *hw;
513
514 hw = __clk_get_hw(clk);
515 if (!hw)
516 return;
517
518 div = to_clk_divider(hw);
519
520 clk_unregister(clk);
521 kfree(div);
522}
523EXPORT_SYMBOL_GPL(clk_unregister_divider);