xref: /openbmc/linux/drivers/clk/clk-divider.c (revision 4e3c021fb995bcbb5d1f814d00584cb80eb904a8)
19d9f78edSMike Turquette /*
29d9f78edSMike Turquette  * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
39d9f78edSMike Turquette  * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
49d9f78edSMike Turquette  * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
59d9f78edSMike Turquette  *
69d9f78edSMike Turquette  * This program is free software; you can redistribute it and/or modify
79d9f78edSMike Turquette  * it under the terms of the GNU General Public License version 2 as
89d9f78edSMike Turquette  * published by the Free Software Foundation.
99d9f78edSMike Turquette  *
109d9f78edSMike Turquette  * Adjustable divider clock implementation
119d9f78edSMike Turquette  */
129d9f78edSMike Turquette 
139d9f78edSMike Turquette #include <linux/clk-provider.h>
149d9f78edSMike Turquette #include <linux/module.h>
159d9f78edSMike Turquette #include <linux/slab.h>
169d9f78edSMike Turquette #include <linux/io.h>
179d9f78edSMike Turquette #include <linux/err.h>
189d9f78edSMike Turquette #include <linux/string.h>
191a3cd184SJames Hogan #include <linux/log2.h>
209d9f78edSMike Turquette 
219d9f78edSMike Turquette /*
229d9f78edSMike Turquette  * DOC: basic adjustable divider clock that cannot gate
239d9f78edSMike Turquette  *
249d9f78edSMike Turquette  * Traits of this clock:
259d9f78edSMike Turquette  * prepare - clk_prepare only ensures that parents are prepared
269d9f78edSMike Turquette  * enable - clk_enable only ensures that parents are enabled
27b11d282dSTomi Valkeinen  * rate - rate is adjustable.  clk->rate = DIV_ROUND_UP(parent->rate / divisor)
289d9f78edSMike Turquette  * parent - fixed parent.  No clk_set_parent support
299d9f78edSMike Turquette  */
309d9f78edSMike Turquette 
319d9f78edSMike Turquette #define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)
329d9f78edSMike Turquette 
331a3cd184SJames Hogan #define div_mask(d)	((1 << ((d)->width)) - 1)
346d9252bdSRajendra Nayak 
35357c3f0aSRajendra Nayak static unsigned int _get_table_maxdiv(const struct clk_div_table *table)
36357c3f0aSRajendra Nayak {
37357c3f0aSRajendra Nayak 	unsigned int maxdiv = 0;
38357c3f0aSRajendra Nayak 	const struct clk_div_table *clkt;
39357c3f0aSRajendra Nayak 
40357c3f0aSRajendra Nayak 	for (clkt = table; clkt->div; clkt++)
41357c3f0aSRajendra Nayak 		if (clkt->div > maxdiv)
42357c3f0aSRajendra Nayak 			maxdiv = clkt->div;
43357c3f0aSRajendra Nayak 	return maxdiv;
44357c3f0aSRajendra Nayak }
45357c3f0aSRajendra Nayak 
46774b5143SMaxime COQUELIN static unsigned int _get_table_mindiv(const struct clk_div_table *table)
47774b5143SMaxime COQUELIN {
48774b5143SMaxime COQUELIN 	unsigned int mindiv = UINT_MAX;
49774b5143SMaxime COQUELIN 	const struct clk_div_table *clkt;
50774b5143SMaxime COQUELIN 
51774b5143SMaxime COQUELIN 	for (clkt = table; clkt->div; clkt++)
52774b5143SMaxime COQUELIN 		if (clkt->div < mindiv)
53774b5143SMaxime COQUELIN 			mindiv = clkt->div;
54774b5143SMaxime COQUELIN 	return mindiv;
55774b5143SMaxime COQUELIN }
56774b5143SMaxime COQUELIN 
576d9252bdSRajendra Nayak static unsigned int _get_maxdiv(struct clk_divider *divider)
586d9252bdSRajendra Nayak {
596d9252bdSRajendra Nayak 	if (divider->flags & CLK_DIVIDER_ONE_BASED)
606d9252bdSRajendra Nayak 		return div_mask(divider);
616d9252bdSRajendra Nayak 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
626d9252bdSRajendra Nayak 		return 1 << div_mask(divider);
63357c3f0aSRajendra Nayak 	if (divider->table)
64357c3f0aSRajendra Nayak 		return _get_table_maxdiv(divider->table);
656d9252bdSRajendra Nayak 	return div_mask(divider) + 1;
666d9252bdSRajendra Nayak }
676d9252bdSRajendra Nayak 
68357c3f0aSRajendra Nayak static unsigned int _get_table_div(const struct clk_div_table *table,
69357c3f0aSRajendra Nayak 							unsigned int val)
70357c3f0aSRajendra Nayak {
71357c3f0aSRajendra Nayak 	const struct clk_div_table *clkt;
72357c3f0aSRajendra Nayak 
73357c3f0aSRajendra Nayak 	for (clkt = table; clkt->div; clkt++)
74357c3f0aSRajendra Nayak 		if (clkt->val == val)
75357c3f0aSRajendra Nayak 			return clkt->div;
76357c3f0aSRajendra Nayak 	return 0;
77357c3f0aSRajendra Nayak }
78357c3f0aSRajendra Nayak 
796d9252bdSRajendra Nayak static unsigned int _get_div(struct clk_divider *divider, unsigned int val)
806d9252bdSRajendra Nayak {
816d9252bdSRajendra Nayak 	if (divider->flags & CLK_DIVIDER_ONE_BASED)
826d9252bdSRajendra Nayak 		return val;
836d9252bdSRajendra Nayak 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
846d9252bdSRajendra Nayak 		return 1 << val;
85357c3f0aSRajendra Nayak 	if (divider->table)
86357c3f0aSRajendra Nayak 		return _get_table_div(divider->table, val);
876d9252bdSRajendra Nayak 	return val + 1;
886d9252bdSRajendra Nayak }
896d9252bdSRajendra Nayak 
90357c3f0aSRajendra Nayak static unsigned int _get_table_val(const struct clk_div_table *table,
91357c3f0aSRajendra Nayak 							unsigned int div)
92357c3f0aSRajendra Nayak {
93357c3f0aSRajendra Nayak 	const struct clk_div_table *clkt;
94357c3f0aSRajendra Nayak 
95357c3f0aSRajendra Nayak 	for (clkt = table; clkt->div; clkt++)
96357c3f0aSRajendra Nayak 		if (clkt->div == div)
97357c3f0aSRajendra Nayak 			return clkt->val;
98357c3f0aSRajendra Nayak 	return 0;
99357c3f0aSRajendra Nayak }
100357c3f0aSRajendra Nayak 
101778037e1SJames Hogan static unsigned int _get_val(struct clk_divider *divider, unsigned int div)
1026d9252bdSRajendra Nayak {
1036d9252bdSRajendra Nayak 	if (divider->flags & CLK_DIVIDER_ONE_BASED)
1046d9252bdSRajendra Nayak 		return div;
1056d9252bdSRajendra Nayak 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
1066d9252bdSRajendra Nayak 		return __ffs(div);
107357c3f0aSRajendra Nayak 	if (divider->table)
108357c3f0aSRajendra Nayak 		return  _get_table_val(divider->table, div);
1096d9252bdSRajendra Nayak 	return div - 1;
1106d9252bdSRajendra Nayak }
1119d9f78edSMike Turquette 
1129d9f78edSMike Turquette static unsigned long clk_divider_recalc_rate(struct clk_hw *hw,
1139d9f78edSMike Turquette 		unsigned long parent_rate)
1149d9f78edSMike Turquette {
1159d9f78edSMike Turquette 	struct clk_divider *divider = to_clk_divider(hw);
1166d9252bdSRajendra Nayak 	unsigned int div, val;
1179d9f78edSMike Turquette 
118aa514ce3SGerhard Sittig 	val = clk_readl(divider->reg) >> divider->shift;
1196d9252bdSRajendra Nayak 	val &= div_mask(divider);
1209d9f78edSMike Turquette 
1216d9252bdSRajendra Nayak 	div = _get_div(divider, val);
1226d9252bdSRajendra Nayak 	if (!div) {
123056b2053SSoren Brinkmann 		WARN(!(divider->flags & CLK_DIVIDER_ALLOW_ZERO),
124056b2053SSoren Brinkmann 			"%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
1256d9252bdSRajendra Nayak 			__clk_get_name(hw->clk));
1266d9252bdSRajendra Nayak 		return parent_rate;
1276d9252bdSRajendra Nayak 	}
1289d9f78edSMike Turquette 
129b11d282dSTomi Valkeinen 	return DIV_ROUND_UP(parent_rate, div);
1309d9f78edSMike Turquette }
1319d9f78edSMike Turquette 
1329d9f78edSMike Turquette /*
1339d9f78edSMike Turquette  * The reverse of DIV_ROUND_UP: The maximum number which
1349d9f78edSMike Turquette  * divided by m is r
1359d9f78edSMike Turquette  */
1369d9f78edSMike Turquette #define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1)
1379d9f78edSMike Turquette 
138357c3f0aSRajendra Nayak static bool _is_valid_table_div(const struct clk_div_table *table,
139357c3f0aSRajendra Nayak 							 unsigned int div)
140357c3f0aSRajendra Nayak {
141357c3f0aSRajendra Nayak 	const struct clk_div_table *clkt;
142357c3f0aSRajendra Nayak 
143357c3f0aSRajendra Nayak 	for (clkt = table; clkt->div; clkt++)
144357c3f0aSRajendra Nayak 		if (clkt->div == div)
145357c3f0aSRajendra Nayak 			return true;
146357c3f0aSRajendra Nayak 	return false;
147357c3f0aSRajendra Nayak }
148357c3f0aSRajendra Nayak 
149357c3f0aSRajendra Nayak static bool _is_valid_div(struct clk_divider *divider, unsigned int div)
150357c3f0aSRajendra Nayak {
151357c3f0aSRajendra Nayak 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
1521a3cd184SJames Hogan 		return is_power_of_2(div);
153357c3f0aSRajendra Nayak 	if (divider->table)
154357c3f0aSRajendra Nayak 		return _is_valid_table_div(divider->table, div);
155357c3f0aSRajendra Nayak 	return true;
156357c3f0aSRajendra Nayak }
157357c3f0aSRajendra Nayak 
158dd23c2cdSMaxime COQUELIN static int _round_up_table(const struct clk_div_table *table, int div)
159dd23c2cdSMaxime COQUELIN {
160dd23c2cdSMaxime COQUELIN 	const struct clk_div_table *clkt;
161fe52e750SMaxime COQUELIN 	int up = INT_MAX;
162dd23c2cdSMaxime COQUELIN 
163dd23c2cdSMaxime COQUELIN 	for (clkt = table; clkt->div; clkt++) {
164dd23c2cdSMaxime COQUELIN 		if (clkt->div == div)
165dd23c2cdSMaxime COQUELIN 			return clkt->div;
166dd23c2cdSMaxime COQUELIN 		else if (clkt->div < div)
167dd23c2cdSMaxime COQUELIN 			continue;
168dd23c2cdSMaxime COQUELIN 
169dd23c2cdSMaxime COQUELIN 		if ((clkt->div - div) < (up - div))
170dd23c2cdSMaxime COQUELIN 			up = clkt->div;
171dd23c2cdSMaxime COQUELIN 	}
172dd23c2cdSMaxime COQUELIN 
173dd23c2cdSMaxime COQUELIN 	return up;
174dd23c2cdSMaxime COQUELIN }
175dd23c2cdSMaxime COQUELIN 
176774b5143SMaxime COQUELIN static int _round_down_table(const struct clk_div_table *table, int div)
177774b5143SMaxime COQUELIN {
178774b5143SMaxime COQUELIN 	const struct clk_div_table *clkt;
179774b5143SMaxime COQUELIN 	int down = _get_table_mindiv(table);
180774b5143SMaxime COQUELIN 
181774b5143SMaxime COQUELIN 	for (clkt = table; clkt->div; clkt++) {
182774b5143SMaxime COQUELIN 		if (clkt->div == div)
183774b5143SMaxime COQUELIN 			return clkt->div;
184774b5143SMaxime COQUELIN 		else if (clkt->div > div)
185774b5143SMaxime COQUELIN 			continue;
186774b5143SMaxime COQUELIN 
187774b5143SMaxime COQUELIN 		if ((div - clkt->div) < (div - down))
188774b5143SMaxime COQUELIN 			down = clkt->div;
189774b5143SMaxime COQUELIN 	}
190774b5143SMaxime COQUELIN 
191774b5143SMaxime COQUELIN 	return down;
192774b5143SMaxime COQUELIN }
193774b5143SMaxime COQUELIN 
194dd23c2cdSMaxime COQUELIN static int _div_round_up(struct clk_divider *divider,
195dd23c2cdSMaxime COQUELIN 		unsigned long parent_rate, unsigned long rate)
196dd23c2cdSMaxime COQUELIN {
197dd23c2cdSMaxime COQUELIN 	int div = DIV_ROUND_UP(parent_rate, rate);
198dd23c2cdSMaxime COQUELIN 
199dd23c2cdSMaxime COQUELIN 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
200dd23c2cdSMaxime COQUELIN 		div = __roundup_pow_of_two(div);
201dd23c2cdSMaxime COQUELIN 	if (divider->table)
202dd23c2cdSMaxime COQUELIN 		div = _round_up_table(divider->table, div);
203dd23c2cdSMaxime COQUELIN 
204dd23c2cdSMaxime COQUELIN 	return div;
205dd23c2cdSMaxime COQUELIN }
206dd23c2cdSMaxime COQUELIN 
207774b5143SMaxime COQUELIN static int _div_round_closest(struct clk_divider *divider,
208774b5143SMaxime COQUELIN 		unsigned long parent_rate, unsigned long rate)
209774b5143SMaxime COQUELIN {
210774b5143SMaxime COQUELIN 	int up, down, div;
211774b5143SMaxime COQUELIN 
212774b5143SMaxime COQUELIN 	up = down = div = DIV_ROUND_CLOSEST(parent_rate, rate);
213774b5143SMaxime COQUELIN 
214774b5143SMaxime COQUELIN 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO) {
215774b5143SMaxime COQUELIN 		up = __roundup_pow_of_two(div);
216774b5143SMaxime COQUELIN 		down = __rounddown_pow_of_two(div);
217774b5143SMaxime COQUELIN 	} else if (divider->table) {
218774b5143SMaxime COQUELIN 		up = _round_up_table(divider->table, div);
219774b5143SMaxime COQUELIN 		down = _round_down_table(divider->table, div);
220774b5143SMaxime COQUELIN 	}
221774b5143SMaxime COQUELIN 
222774b5143SMaxime COQUELIN 	return (up - div) <= (div - down) ? up : down;
223774b5143SMaxime COQUELIN }
224774b5143SMaxime COQUELIN 
225774b5143SMaxime COQUELIN static int _div_round(struct clk_divider *divider, unsigned long parent_rate,
226774b5143SMaxime COQUELIN 		unsigned long rate)
227774b5143SMaxime COQUELIN {
228774b5143SMaxime COQUELIN 	if (divider->flags & CLK_DIVIDER_ROUND_CLOSEST)
229774b5143SMaxime COQUELIN 		return _div_round_closest(divider, parent_rate, rate);
230774b5143SMaxime COQUELIN 
231774b5143SMaxime COQUELIN 	return _div_round_up(divider, parent_rate, rate);
232774b5143SMaxime COQUELIN }
233774b5143SMaxime COQUELIN 
234774b5143SMaxime COQUELIN static bool _is_best_div(struct clk_divider *divider,
2353c17296fSTomasz Figa 		unsigned long rate, unsigned long now, unsigned long best)
236774b5143SMaxime COQUELIN {
237774b5143SMaxime COQUELIN 	if (divider->flags & CLK_DIVIDER_ROUND_CLOSEST)
238774b5143SMaxime COQUELIN 		return abs(rate - now) < abs(rate - best);
239774b5143SMaxime COQUELIN 
240774b5143SMaxime COQUELIN 	return now <= rate && now > best;
241774b5143SMaxime COQUELIN }
242774b5143SMaxime COQUELIN 
2430e2de78eSMaxime COQUELIN static int _next_div(struct clk_divider *divider, int div)
2440e2de78eSMaxime COQUELIN {
2450e2de78eSMaxime COQUELIN 	div++;
2460e2de78eSMaxime COQUELIN 
2470e2de78eSMaxime COQUELIN 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
2480e2de78eSMaxime COQUELIN 		return __roundup_pow_of_two(div);
2490e2de78eSMaxime COQUELIN 	if (divider->table)
2500e2de78eSMaxime COQUELIN 		return _round_up_table(divider->table, div);
2510e2de78eSMaxime COQUELIN 
2520e2de78eSMaxime COQUELIN 	return div;
2530e2de78eSMaxime COQUELIN }
2540e2de78eSMaxime COQUELIN 
2559d9f78edSMike Turquette static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
2569d9f78edSMike Turquette 		unsigned long *best_parent_rate)
2579d9f78edSMike Turquette {
2589d9f78edSMike Turquette 	struct clk_divider *divider = to_clk_divider(hw);
2599d9f78edSMike Turquette 	int i, bestdiv = 0;
2609d9f78edSMike Turquette 	unsigned long parent_rate, best = 0, now, maxdiv;
261081c9025SShawn Guo 	unsigned long parent_rate_saved = *best_parent_rate;
2629d9f78edSMike Turquette 
2639d9f78edSMike Turquette 	if (!rate)
2649d9f78edSMike Turquette 		rate = 1;
2659d9f78edSMike Turquette 
266e6d5e7d9SJames Hogan 	/* if read only, just return current value */
267e6d5e7d9SJames Hogan 	if (divider->flags & CLK_DIVIDER_READ_ONLY) {
268e6d5e7d9SJames Hogan 		bestdiv = readl(divider->reg) >> divider->shift;
269e6d5e7d9SJames Hogan 		bestdiv &= div_mask(divider);
270e6d5e7d9SJames Hogan 		bestdiv = _get_div(divider, bestdiv);
271e6d5e7d9SJames Hogan 		return bestdiv;
272e6d5e7d9SJames Hogan 	}
273e6d5e7d9SJames Hogan 
2746d9252bdSRajendra Nayak 	maxdiv = _get_maxdiv(divider);
2759d9f78edSMike Turquette 
27681536e07SShawn Guo 	if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) {
27781536e07SShawn Guo 		parent_rate = *best_parent_rate;
278774b5143SMaxime COQUELIN 		bestdiv = _div_round(divider, parent_rate, rate);
2799d9f78edSMike Turquette 		bestdiv = bestdiv == 0 ? 1 : bestdiv;
2809d9f78edSMike Turquette 		bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
2819d9f78edSMike Turquette 		return bestdiv;
2829d9f78edSMike Turquette 	}
2839d9f78edSMike Turquette 
2849d9f78edSMike Turquette 	/*
2859d9f78edSMike Turquette 	 * The maximum divider we can use without overflowing
2869d9f78edSMike Turquette 	 * unsigned long in rate * i below
2879d9f78edSMike Turquette 	 */
2889d9f78edSMike Turquette 	maxdiv = min(ULONG_MAX / rate, maxdiv);
2899d9f78edSMike Turquette 
2900e2de78eSMaxime COQUELIN 	for (i = 1; i <= maxdiv; i = _next_div(divider, i)) {
291357c3f0aSRajendra Nayak 		if (!_is_valid_div(divider, i))
2926d9252bdSRajendra Nayak 			continue;
293081c9025SShawn Guo 		if (rate * i == parent_rate_saved) {
294081c9025SShawn Guo 			/*
295081c9025SShawn Guo 			 * It's the most ideal case if the requested rate can be
296081c9025SShawn Guo 			 * divided from parent clock without needing to change
297081c9025SShawn Guo 			 * parent rate, so return the divider immediately.
298081c9025SShawn Guo 			 */
299081c9025SShawn Guo 			*best_parent_rate = parent_rate_saved;
300081c9025SShawn Guo 			return i;
301081c9025SShawn Guo 		}
3029d9f78edSMike Turquette 		parent_rate = __clk_round_rate(__clk_get_parent(hw->clk),
3039d9f78edSMike Turquette 				MULT_ROUND_UP(rate, i));
304b11d282dSTomi Valkeinen 		now = DIV_ROUND_UP(parent_rate, i);
305774b5143SMaxime COQUELIN 		if (_is_best_div(divider, rate, now, best)) {
3069d9f78edSMike Turquette 			bestdiv = i;
3079d9f78edSMike Turquette 			best = now;
3089d9f78edSMike Turquette 			*best_parent_rate = parent_rate;
3099d9f78edSMike Turquette 		}
3109d9f78edSMike Turquette 	}
3119d9f78edSMike Turquette 
3129d9f78edSMike Turquette 	if (!bestdiv) {
3136d9252bdSRajendra Nayak 		bestdiv = _get_maxdiv(divider);
3149d9f78edSMike Turquette 		*best_parent_rate = __clk_round_rate(__clk_get_parent(hw->clk), 1);
3159d9f78edSMike Turquette 	}
3169d9f78edSMike Turquette 
3179d9f78edSMike Turquette 	return bestdiv;
3189d9f78edSMike Turquette }
3199d9f78edSMike Turquette 
3209d9f78edSMike Turquette static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
3219d9f78edSMike Turquette 				unsigned long *prate)
3229d9f78edSMike Turquette {
3239d9f78edSMike Turquette 	int div;
3249d9f78edSMike Turquette 	div = clk_divider_bestdiv(hw, rate, prate);
3259d9f78edSMike Turquette 
326b11d282dSTomi Valkeinen 	return DIV_ROUND_UP(*prate, div);
3279d9f78edSMike Turquette }
3289d9f78edSMike Turquette 
3291c0035d7SShawn Guo static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
3301c0035d7SShawn Guo 				unsigned long parent_rate)
3319d9f78edSMike Turquette {
3329d9f78edSMike Turquette 	struct clk_divider *divider = to_clk_divider(hw);
3336d9252bdSRajendra Nayak 	unsigned int div, value;
3349d9f78edSMike Turquette 	unsigned long flags = 0;
3359d9f78edSMike Turquette 	u32 val;
3369d9f78edSMike Turquette 
337b11d282dSTomi Valkeinen 	div = DIV_ROUND_UP(parent_rate, rate);
338dd23c2cdSMaxime COQUELIN 
339dd23c2cdSMaxime COQUELIN 	if (!_is_valid_div(divider, div))
340dd23c2cdSMaxime COQUELIN 		return -EINVAL;
341dd23c2cdSMaxime COQUELIN 
3426d9252bdSRajendra Nayak 	value = _get_val(divider, div);
3439d9f78edSMike Turquette 
3446d9252bdSRajendra Nayak 	if (value > div_mask(divider))
3456d9252bdSRajendra Nayak 		value = div_mask(divider);
3469d9f78edSMike Turquette 
3479d9f78edSMike Turquette 	if (divider->lock)
3489d9f78edSMike Turquette 		spin_lock_irqsave(divider->lock, flags);
3499d9f78edSMike Turquette 
350d57dfe75SHaojian Zhuang 	if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
351d57dfe75SHaojian Zhuang 		val = div_mask(divider) << (divider->shift + 16);
352d57dfe75SHaojian Zhuang 	} else {
353aa514ce3SGerhard Sittig 		val = clk_readl(divider->reg);
3549d9f78edSMike Turquette 		val &= ~(div_mask(divider) << divider->shift);
355d57dfe75SHaojian Zhuang 	}
3566d9252bdSRajendra Nayak 	val |= value << divider->shift;
357aa514ce3SGerhard Sittig 	clk_writel(val, divider->reg);
3589d9f78edSMike Turquette 
3599d9f78edSMike Turquette 	if (divider->lock)
3609d9f78edSMike Turquette 		spin_unlock_irqrestore(divider->lock, flags);
3619d9f78edSMike Turquette 
3629d9f78edSMike Turquette 	return 0;
3639d9f78edSMike Turquette }
3649d9f78edSMike Turquette 
365822c250eSShawn Guo const struct clk_ops clk_divider_ops = {
3669d9f78edSMike Turquette 	.recalc_rate = clk_divider_recalc_rate,
3679d9f78edSMike Turquette 	.round_rate = clk_divider_round_rate,
3689d9f78edSMike Turquette 	.set_rate = clk_divider_set_rate,
3699d9f78edSMike Turquette };
3709d9f78edSMike Turquette EXPORT_SYMBOL_GPL(clk_divider_ops);
3719d9f78edSMike Turquette 
372357c3f0aSRajendra Nayak static struct clk *_register_divider(struct device *dev, const char *name,
3739d9f78edSMike Turquette 		const char *parent_name, unsigned long flags,
3749d9f78edSMike Turquette 		void __iomem *reg, u8 shift, u8 width,
375357c3f0aSRajendra Nayak 		u8 clk_divider_flags, const struct clk_div_table *table,
376357c3f0aSRajendra Nayak 		spinlock_t *lock)
3779d9f78edSMike Turquette {
3789d9f78edSMike Turquette 	struct clk_divider *div;
3799d9f78edSMike Turquette 	struct clk *clk;
3800197b3eaSSaravana Kannan 	struct clk_init_data init;
3819d9f78edSMike Turquette 
382d57dfe75SHaojian Zhuang 	if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
383d57dfe75SHaojian Zhuang 		if (width + shift > 16) {
384d57dfe75SHaojian Zhuang 			pr_warn("divider value exceeds LOWORD field\n");
385d57dfe75SHaojian Zhuang 			return ERR_PTR(-EINVAL);
386d57dfe75SHaojian Zhuang 		}
387d57dfe75SHaojian Zhuang 	}
388d57dfe75SHaojian Zhuang 
38927d54591SMike Turquette 	/* allocate the divider */
3909d9f78edSMike Turquette 	div = kzalloc(sizeof(struct clk_divider), GFP_KERNEL);
3919d9f78edSMike Turquette 	if (!div) {
3929d9f78edSMike Turquette 		pr_err("%s: could not allocate divider clk\n", __func__);
39327d54591SMike Turquette 		return ERR_PTR(-ENOMEM);
3949d9f78edSMike Turquette 	}
3959d9f78edSMike Turquette 
3960197b3eaSSaravana Kannan 	init.name = name;
3970197b3eaSSaravana Kannan 	init.ops = &clk_divider_ops;
398f7d8caadSRajendra Nayak 	init.flags = flags | CLK_IS_BASIC;
3990197b3eaSSaravana Kannan 	init.parent_names = (parent_name ? &parent_name: NULL);
4000197b3eaSSaravana Kannan 	init.num_parents = (parent_name ? 1 : 0);
4010197b3eaSSaravana Kannan 
4029d9f78edSMike Turquette 	/* struct clk_divider assignments */
4039d9f78edSMike Turquette 	div->reg = reg;
4049d9f78edSMike Turquette 	div->shift = shift;
4059d9f78edSMike Turquette 	div->width = width;
4069d9f78edSMike Turquette 	div->flags = clk_divider_flags;
4079d9f78edSMike Turquette 	div->lock = lock;
4080197b3eaSSaravana Kannan 	div->hw.init = &init;
409357c3f0aSRajendra Nayak 	div->table = table;
4109d9f78edSMike Turquette 
41127d54591SMike Turquette 	/* register the clock */
4120197b3eaSSaravana Kannan 	clk = clk_register(dev, &div->hw);
4139d9f78edSMike Turquette 
41427d54591SMike Turquette 	if (IS_ERR(clk))
4159d9f78edSMike Turquette 		kfree(div);
4169d9f78edSMike Turquette 
41727d54591SMike Turquette 	return clk;
4189d9f78edSMike Turquette }
419357c3f0aSRajendra Nayak 
420357c3f0aSRajendra Nayak /**
421357c3f0aSRajendra Nayak  * clk_register_divider - register a divider clock with the clock framework
422357c3f0aSRajendra Nayak  * @dev: device registering this clock
423357c3f0aSRajendra Nayak  * @name: name of this clock
424357c3f0aSRajendra Nayak  * @parent_name: name of clock's parent
425357c3f0aSRajendra Nayak  * @flags: framework-specific flags
426357c3f0aSRajendra Nayak  * @reg: register address to adjust divider
427357c3f0aSRajendra Nayak  * @shift: number of bits to shift the bitfield
428357c3f0aSRajendra Nayak  * @width: width of the bitfield
429357c3f0aSRajendra Nayak  * @clk_divider_flags: divider-specific flags for this clock
430357c3f0aSRajendra Nayak  * @lock: shared register lock for this clock
431357c3f0aSRajendra Nayak  */
432357c3f0aSRajendra Nayak struct clk *clk_register_divider(struct device *dev, const char *name,
433357c3f0aSRajendra Nayak 		const char *parent_name, unsigned long flags,
434357c3f0aSRajendra Nayak 		void __iomem *reg, u8 shift, u8 width,
435357c3f0aSRajendra Nayak 		u8 clk_divider_flags, spinlock_t *lock)
436357c3f0aSRajendra Nayak {
437357c3f0aSRajendra Nayak 	return _register_divider(dev, name, parent_name, flags, reg, shift,
438357c3f0aSRajendra Nayak 			width, clk_divider_flags, NULL, lock);
439357c3f0aSRajendra Nayak }
4404c5eeea9SFabio Estevam EXPORT_SYMBOL_GPL(clk_register_divider);
441357c3f0aSRajendra Nayak 
442357c3f0aSRajendra Nayak /**
443357c3f0aSRajendra Nayak  * clk_register_divider_table - register a table based divider clock with
444357c3f0aSRajendra Nayak  * the clock framework
445357c3f0aSRajendra Nayak  * @dev: device registering this clock
446357c3f0aSRajendra Nayak  * @name: name of this clock
447357c3f0aSRajendra Nayak  * @parent_name: name of clock's parent
448357c3f0aSRajendra Nayak  * @flags: framework-specific flags
449357c3f0aSRajendra Nayak  * @reg: register address to adjust divider
450357c3f0aSRajendra Nayak  * @shift: number of bits to shift the bitfield
451357c3f0aSRajendra Nayak  * @width: width of the bitfield
452357c3f0aSRajendra Nayak  * @clk_divider_flags: divider-specific flags for this clock
453357c3f0aSRajendra Nayak  * @table: array of divider/value pairs ending with a div set to 0
454357c3f0aSRajendra Nayak  * @lock: shared register lock for this clock
455357c3f0aSRajendra Nayak  */
456357c3f0aSRajendra Nayak struct clk *clk_register_divider_table(struct device *dev, const char *name,
457357c3f0aSRajendra Nayak 		const char *parent_name, unsigned long flags,
458357c3f0aSRajendra Nayak 		void __iomem *reg, u8 shift, u8 width,
459357c3f0aSRajendra Nayak 		u8 clk_divider_flags, const struct clk_div_table *table,
460357c3f0aSRajendra Nayak 		spinlock_t *lock)
461357c3f0aSRajendra Nayak {
462357c3f0aSRajendra Nayak 	return _register_divider(dev, name, parent_name, flags, reg, shift,
463357c3f0aSRajendra Nayak 			width, clk_divider_flags, table, lock);
464357c3f0aSRajendra Nayak }
4654c5eeea9SFabio Estevam EXPORT_SYMBOL_GPL(clk_register_divider_table);
466*4e3c021fSKrzysztof Kozlowski 
467*4e3c021fSKrzysztof Kozlowski void clk_unregister_divider(struct clk *clk)
468*4e3c021fSKrzysztof Kozlowski {
469*4e3c021fSKrzysztof Kozlowski 	struct clk_divider *div;
470*4e3c021fSKrzysztof Kozlowski 	struct clk_hw *hw;
471*4e3c021fSKrzysztof Kozlowski 
472*4e3c021fSKrzysztof Kozlowski 	hw = __clk_get_hw(clk);
473*4e3c021fSKrzysztof Kozlowski 	if (!hw)
474*4e3c021fSKrzysztof Kozlowski 		return;
475*4e3c021fSKrzysztof Kozlowski 
476*4e3c021fSKrzysztof Kozlowski 	div = to_clk_divider(hw);
477*4e3c021fSKrzysztof Kozlowski 
478*4e3c021fSKrzysztof Kozlowski 	clk_unregister(clk);
479*4e3c021fSKrzysztof Kozlowski 	kfree(div);
480*4e3c021fSKrzysztof Kozlowski }
481*4e3c021fSKrzysztof Kozlowski EXPORT_SYMBOL_GPL(clk_unregister_divider);
482