xref: /openbmc/linux/drivers/clk/clk-divider.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1e1bd55e5SStephen Boyd // SPDX-License-Identifier: GPL-2.0
29d9f78edSMike Turquette /*
39d9f78edSMike Turquette  * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
49d9f78edSMike Turquette  * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
59d9f78edSMike Turquette  * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
69d9f78edSMike Turquette  *
79d9f78edSMike Turquette  * Adjustable divider clock implementation
89d9f78edSMike Turquette  */
99d9f78edSMike Turquette 
109d9f78edSMike Turquette #include <linux/clk-provider.h>
1126792699SMichael Walle #include <linux/device.h>
129d9f78edSMike Turquette #include <linux/module.h>
139d9f78edSMike Turquette #include <linux/slab.h>
149d9f78edSMike Turquette #include <linux/io.h>
159d9f78edSMike Turquette #include <linux/err.h>
169d9f78edSMike Turquette #include <linux/string.h>
171a3cd184SJames Hogan #include <linux/log2.h>
189d9f78edSMike Turquette 
199d9f78edSMike Turquette /*
209d9f78edSMike Turquette  * DOC: basic adjustable divider clock that cannot gate
219d9f78edSMike Turquette  *
229d9f78edSMike Turquette  * Traits of this clock:
239d9f78edSMike Turquette  * prepare - clk_prepare only ensures that parents are prepared
249d9f78edSMike Turquette  * enable - clk_enable only ensures that parents are enabled
259556f9daSBrian Norris  * rate - rate is adjustable.  clk->rate = ceiling(parent->rate / divisor)
269d9f78edSMike Turquette  * parent - fixed parent.  No clk_set_parent support
279d9f78edSMike Turquette  */
289d9f78edSMike Turquette 
clk_div_readl(struct clk_divider * divider)29434d69faSJonas Gorski static inline u32 clk_div_readl(struct clk_divider *divider)
30434d69faSJonas Gorski {
31434d69faSJonas Gorski 	if (divider->flags & CLK_DIVIDER_BIG_ENDIAN)
32434d69faSJonas Gorski 		return ioread32be(divider->reg);
33434d69faSJonas Gorski 
345834fd75SJonas Gorski 	return readl(divider->reg);
35434d69faSJonas Gorski }
36434d69faSJonas Gorski 
clk_div_writel(struct clk_divider * divider,u32 val)37434d69faSJonas Gorski static inline void clk_div_writel(struct clk_divider *divider, u32 val)
38434d69faSJonas Gorski {
39434d69faSJonas Gorski 	if (divider->flags & CLK_DIVIDER_BIG_ENDIAN)
40434d69faSJonas Gorski 		iowrite32be(val, divider->reg);
41434d69faSJonas Gorski 	else
425834fd75SJonas Gorski 		writel(val, divider->reg);
43434d69faSJonas Gorski }
44434d69faSJonas Gorski 
_get_table_maxdiv(const struct clk_div_table * table,u8 width)45fab88ca7SStephen Boyd static unsigned int _get_table_maxdiv(const struct clk_div_table *table,
46fab88ca7SStephen Boyd 				      u8 width)
47357c3f0aSRajendra Nayak {
48e6d3cc7bSJerome Brunet 	unsigned int maxdiv = 0, mask = clk_div_mask(width);
49357c3f0aSRajendra Nayak 	const struct clk_div_table *clkt;
50357c3f0aSRajendra Nayak 
51357c3f0aSRajendra Nayak 	for (clkt = table; clkt->div; clkt++)
52fab88ca7SStephen Boyd 		if (clkt->div > maxdiv && clkt->val <= mask)
53357c3f0aSRajendra Nayak 			maxdiv = clkt->div;
54357c3f0aSRajendra Nayak 	return maxdiv;
55357c3f0aSRajendra Nayak }
56357c3f0aSRajendra Nayak 
_get_table_mindiv(const struct clk_div_table * table)57774b5143SMaxime COQUELIN static unsigned int _get_table_mindiv(const struct clk_div_table *table)
58774b5143SMaxime COQUELIN {
59774b5143SMaxime COQUELIN 	unsigned int mindiv = UINT_MAX;
60774b5143SMaxime COQUELIN 	const struct clk_div_table *clkt;
61774b5143SMaxime COQUELIN 
62774b5143SMaxime COQUELIN 	for (clkt = table; clkt->div; clkt++)
63774b5143SMaxime COQUELIN 		if (clkt->div < mindiv)
64774b5143SMaxime COQUELIN 			mindiv = clkt->div;
65774b5143SMaxime COQUELIN 	return mindiv;
66774b5143SMaxime COQUELIN }
67774b5143SMaxime COQUELIN 
_get_maxdiv(const struct clk_div_table * table,u8 width,unsigned long flags)68bca9690bSStephen Boyd static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width,
69bca9690bSStephen Boyd 				unsigned long flags)
706d9252bdSRajendra Nayak {
71bca9690bSStephen Boyd 	if (flags & CLK_DIVIDER_ONE_BASED)
72e6d3cc7bSJerome Brunet 		return clk_div_mask(width);
73bca9690bSStephen Boyd 	if (flags & CLK_DIVIDER_POWER_OF_TWO)
74e6d3cc7bSJerome Brunet 		return 1 << clk_div_mask(width);
75bca9690bSStephen Boyd 	if (table)
76fab88ca7SStephen Boyd 		return _get_table_maxdiv(table, width);
77e6d3cc7bSJerome Brunet 	return clk_div_mask(width) + 1;
786d9252bdSRajendra Nayak }
796d9252bdSRajendra Nayak 
_get_table_div(const struct clk_div_table * table,unsigned int val)80357c3f0aSRajendra Nayak static unsigned int _get_table_div(const struct clk_div_table *table,
81357c3f0aSRajendra Nayak 							unsigned int val)
82357c3f0aSRajendra Nayak {
83357c3f0aSRajendra Nayak 	const struct clk_div_table *clkt;
84357c3f0aSRajendra Nayak 
85357c3f0aSRajendra Nayak 	for (clkt = table; clkt->div; clkt++)
86357c3f0aSRajendra Nayak 		if (clkt->val == val)
87357c3f0aSRajendra Nayak 			return clkt->div;
88357c3f0aSRajendra Nayak 	return 0;
89357c3f0aSRajendra Nayak }
90357c3f0aSRajendra Nayak 
_get_div(const struct clk_div_table * table,unsigned int val,unsigned long flags,u8 width)91bca9690bSStephen Boyd static unsigned int _get_div(const struct clk_div_table *table,
92afe76c8fSJim Quinlan 			     unsigned int val, unsigned long flags, u8 width)
936d9252bdSRajendra Nayak {
94bca9690bSStephen Boyd 	if (flags & CLK_DIVIDER_ONE_BASED)
956d9252bdSRajendra Nayak 		return val;
96bca9690bSStephen Boyd 	if (flags & CLK_DIVIDER_POWER_OF_TWO)
976d9252bdSRajendra Nayak 		return 1 << val;
98afe76c8fSJim Quinlan 	if (flags & CLK_DIVIDER_MAX_AT_ZERO)
99e6d3cc7bSJerome Brunet 		return val ? val : clk_div_mask(width) + 1;
100bca9690bSStephen Boyd 	if (table)
101bca9690bSStephen Boyd 		return _get_table_div(table, val);
1026d9252bdSRajendra Nayak 	return val + 1;
1036d9252bdSRajendra Nayak }
1046d9252bdSRajendra Nayak 
_get_table_val(const struct clk_div_table * table,unsigned int div)105357c3f0aSRajendra Nayak static unsigned int _get_table_val(const struct clk_div_table *table,
106357c3f0aSRajendra Nayak 							unsigned int div)
107357c3f0aSRajendra Nayak {
108357c3f0aSRajendra Nayak 	const struct clk_div_table *clkt;
109357c3f0aSRajendra Nayak 
110357c3f0aSRajendra Nayak 	for (clkt = table; clkt->div; clkt++)
111357c3f0aSRajendra Nayak 		if (clkt->div == div)
112357c3f0aSRajendra Nayak 			return clkt->val;
113357c3f0aSRajendra Nayak 	return 0;
114357c3f0aSRajendra Nayak }
115357c3f0aSRajendra Nayak 
_get_val(const struct clk_div_table * table,unsigned int div,unsigned long flags,u8 width)116bca9690bSStephen Boyd static unsigned int _get_val(const struct clk_div_table *table,
117afe76c8fSJim Quinlan 			     unsigned int div, unsigned long flags, u8 width)
1186d9252bdSRajendra Nayak {
119bca9690bSStephen Boyd 	if (flags & CLK_DIVIDER_ONE_BASED)
1206d9252bdSRajendra Nayak 		return div;
121bca9690bSStephen Boyd 	if (flags & CLK_DIVIDER_POWER_OF_TWO)
1226d9252bdSRajendra Nayak 		return __ffs(div);
123afe76c8fSJim Quinlan 	if (flags & CLK_DIVIDER_MAX_AT_ZERO)
124e6d3cc7bSJerome Brunet 		return (div == clk_div_mask(width) + 1) ? 0 : div;
125bca9690bSStephen Boyd 	if (table)
126bca9690bSStephen Boyd 		return  _get_table_val(table, div);
1276d9252bdSRajendra Nayak 	return div - 1;
1286d9252bdSRajendra Nayak }
1299d9f78edSMike Turquette 
divider_recalc_rate(struct clk_hw * hw,unsigned long parent_rate,unsigned int val,const struct clk_div_table * table,unsigned long flags,unsigned long width)130bca9690bSStephen Boyd unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate,
131bca9690bSStephen Boyd 				  unsigned int val,
132bca9690bSStephen Boyd 				  const struct clk_div_table *table,
13312a26c29SJerome Brunet 				  unsigned long flags, unsigned long width)
1349d9f78edSMike Turquette {
135bca9690bSStephen Boyd 	unsigned int div;
1369d9f78edSMike Turquette 
13712a26c29SJerome Brunet 	div = _get_div(table, val, flags, width);
1386d9252bdSRajendra Nayak 	if (!div) {
139bca9690bSStephen Boyd 		WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO),
140056b2053SSoren Brinkmann 			"%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
1412f508a95SStephen Boyd 			clk_hw_get_name(hw));
1426d9252bdSRajendra Nayak 		return parent_rate;
1436d9252bdSRajendra Nayak 	}
1449d9f78edSMike Turquette 
1459556f9daSBrian Norris 	return DIV_ROUND_UP_ULL((u64)parent_rate, div);
1469d9f78edSMike Turquette }
147bca9690bSStephen Boyd EXPORT_SYMBOL_GPL(divider_recalc_rate);
148bca9690bSStephen Boyd 
clk_divider_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)149bca9690bSStephen Boyd static unsigned long clk_divider_recalc_rate(struct clk_hw *hw,
150bca9690bSStephen Boyd 		unsigned long parent_rate)
151bca9690bSStephen Boyd {
152bca9690bSStephen Boyd 	struct clk_divider *divider = to_clk_divider(hw);
153bca9690bSStephen Boyd 	unsigned int val;
154bca9690bSStephen Boyd 
155434d69faSJonas Gorski 	val = clk_div_readl(divider) >> divider->shift;
156e6d3cc7bSJerome Brunet 	val &= clk_div_mask(divider->width);
157bca9690bSStephen Boyd 
158bca9690bSStephen Boyd 	return divider_recalc_rate(hw, parent_rate, val, divider->table,
15912a26c29SJerome Brunet 				   divider->flags, divider->width);
160bca9690bSStephen Boyd }
1619d9f78edSMike Turquette 
_is_valid_table_div(const struct clk_div_table * table,unsigned int div)162357c3f0aSRajendra Nayak static bool _is_valid_table_div(const struct clk_div_table *table,
163357c3f0aSRajendra Nayak 							 unsigned int div)
164357c3f0aSRajendra Nayak {
165357c3f0aSRajendra Nayak 	const struct clk_div_table *clkt;
166357c3f0aSRajendra Nayak 
167357c3f0aSRajendra Nayak 	for (clkt = table; clkt->div; clkt++)
168357c3f0aSRajendra Nayak 		if (clkt->div == div)
169357c3f0aSRajendra Nayak 			return true;
170357c3f0aSRajendra Nayak 	return false;
171357c3f0aSRajendra Nayak }
172357c3f0aSRajendra Nayak 
_is_valid_div(const struct clk_div_table * table,unsigned int div,unsigned long flags)173bca9690bSStephen Boyd static bool _is_valid_div(const struct clk_div_table *table, unsigned int div,
174bca9690bSStephen Boyd 			  unsigned long flags)
175357c3f0aSRajendra Nayak {
176bca9690bSStephen Boyd 	if (flags & CLK_DIVIDER_POWER_OF_TWO)
1771a3cd184SJames Hogan 		return is_power_of_2(div);
178bca9690bSStephen Boyd 	if (table)
179bca9690bSStephen Boyd 		return _is_valid_table_div(table, div);
180357c3f0aSRajendra Nayak 	return true;
181357c3f0aSRajendra Nayak }
182357c3f0aSRajendra Nayak 
_round_up_table(const struct clk_div_table * table,int div)183dd23c2cdSMaxime COQUELIN static int _round_up_table(const struct clk_div_table *table, int div)
184dd23c2cdSMaxime COQUELIN {
185dd23c2cdSMaxime COQUELIN 	const struct clk_div_table *clkt;
186fe52e750SMaxime COQUELIN 	int up = INT_MAX;
187dd23c2cdSMaxime COQUELIN 
188dd23c2cdSMaxime COQUELIN 	for (clkt = table; clkt->div; clkt++) {
189dd23c2cdSMaxime COQUELIN 		if (clkt->div == div)
190dd23c2cdSMaxime COQUELIN 			return clkt->div;
191dd23c2cdSMaxime COQUELIN 		else if (clkt->div < div)
192dd23c2cdSMaxime COQUELIN 			continue;
193dd23c2cdSMaxime COQUELIN 
194dd23c2cdSMaxime COQUELIN 		if ((clkt->div - div) < (up - div))
195dd23c2cdSMaxime COQUELIN 			up = clkt->div;
196dd23c2cdSMaxime COQUELIN 	}
197dd23c2cdSMaxime COQUELIN 
198dd23c2cdSMaxime COQUELIN 	return up;
199dd23c2cdSMaxime COQUELIN }
200dd23c2cdSMaxime COQUELIN 
_round_down_table(const struct clk_div_table * table,int div)201774b5143SMaxime COQUELIN static int _round_down_table(const struct clk_div_table *table, int div)
202774b5143SMaxime COQUELIN {
203774b5143SMaxime COQUELIN 	const struct clk_div_table *clkt;
204774b5143SMaxime COQUELIN 	int down = _get_table_mindiv(table);
205774b5143SMaxime COQUELIN 
206774b5143SMaxime COQUELIN 	for (clkt = table; clkt->div; clkt++) {
207774b5143SMaxime COQUELIN 		if (clkt->div == div)
208774b5143SMaxime COQUELIN 			return clkt->div;
209774b5143SMaxime COQUELIN 		else if (clkt->div > div)
210774b5143SMaxime COQUELIN 			continue;
211774b5143SMaxime COQUELIN 
212774b5143SMaxime COQUELIN 		if ((div - clkt->div) < (div - down))
213774b5143SMaxime COQUELIN 			down = clkt->div;
214774b5143SMaxime COQUELIN 	}
215774b5143SMaxime COQUELIN 
216774b5143SMaxime COQUELIN 	return down;
217774b5143SMaxime COQUELIN }
218774b5143SMaxime COQUELIN 
_div_round_up(const struct clk_div_table * table,unsigned long parent_rate,unsigned long rate,unsigned long flags)219bca9690bSStephen Boyd static int _div_round_up(const struct clk_div_table *table,
220bca9690bSStephen Boyd 			 unsigned long parent_rate, unsigned long rate,
221bca9690bSStephen Boyd 			 unsigned long flags)
222dd23c2cdSMaxime COQUELIN {
2239556f9daSBrian Norris 	int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
224dd23c2cdSMaxime COQUELIN 
225bca9690bSStephen Boyd 	if (flags & CLK_DIVIDER_POWER_OF_TWO)
226dd23c2cdSMaxime COQUELIN 		div = __roundup_pow_of_two(div);
227bca9690bSStephen Boyd 	if (table)
228bca9690bSStephen Boyd 		div = _round_up_table(table, div);
229dd23c2cdSMaxime COQUELIN 
230dd23c2cdSMaxime COQUELIN 	return div;
231dd23c2cdSMaxime COQUELIN }
232dd23c2cdSMaxime COQUELIN 
_div_round_closest(const struct clk_div_table * table,unsigned long parent_rate,unsigned long rate,unsigned long flags)233bca9690bSStephen Boyd static int _div_round_closest(const struct clk_div_table *table,
234bca9690bSStephen Boyd 			      unsigned long parent_rate, unsigned long rate,
235bca9690bSStephen Boyd 			      unsigned long flags)
236774b5143SMaxime COQUELIN {
23793155142SUwe Kleine-König 	int up, down;
23826bac95aSUwe Kleine-König 	unsigned long up_rate, down_rate;
239774b5143SMaxime COQUELIN 
2409556f9daSBrian Norris 	up = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
24193155142SUwe Kleine-König 	down = parent_rate / rate;
242774b5143SMaxime COQUELIN 
243bca9690bSStephen Boyd 	if (flags & CLK_DIVIDER_POWER_OF_TWO) {
24493155142SUwe Kleine-König 		up = __roundup_pow_of_two(up);
24593155142SUwe Kleine-König 		down = __rounddown_pow_of_two(down);
246bca9690bSStephen Boyd 	} else if (table) {
24793155142SUwe Kleine-König 		up = _round_up_table(table, up);
24893155142SUwe Kleine-König 		down = _round_down_table(table, down);
249774b5143SMaxime COQUELIN 	}
250774b5143SMaxime COQUELIN 
2519556f9daSBrian Norris 	up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up);
2529556f9daSBrian Norris 	down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down);
25326bac95aSUwe Kleine-König 
25426bac95aSUwe Kleine-König 	return (rate - up_rate) <= (down_rate - rate) ? up : down;
255774b5143SMaxime COQUELIN }
256774b5143SMaxime COQUELIN 
_div_round(const struct clk_div_table * table,unsigned long parent_rate,unsigned long rate,unsigned long flags)257bca9690bSStephen Boyd static int _div_round(const struct clk_div_table *table,
258bca9690bSStephen Boyd 		      unsigned long parent_rate, unsigned long rate,
259bca9690bSStephen Boyd 		      unsigned long flags)
260774b5143SMaxime COQUELIN {
261bca9690bSStephen Boyd 	if (flags & CLK_DIVIDER_ROUND_CLOSEST)
262bca9690bSStephen Boyd 		return _div_round_closest(table, parent_rate, rate, flags);
263774b5143SMaxime COQUELIN 
264bca9690bSStephen Boyd 	return _div_round_up(table, parent_rate, rate, flags);
265774b5143SMaxime COQUELIN }
266774b5143SMaxime COQUELIN 
_is_best_div(unsigned long rate,unsigned long now,unsigned long best,unsigned long flags)267bca9690bSStephen Boyd static bool _is_best_div(unsigned long rate, unsigned long now,
268bca9690bSStephen Boyd 			 unsigned long best, unsigned long flags)
269774b5143SMaxime COQUELIN {
270bca9690bSStephen Boyd 	if (flags & CLK_DIVIDER_ROUND_CLOSEST)
271774b5143SMaxime COQUELIN 		return abs(rate - now) < abs(rate - best);
272774b5143SMaxime COQUELIN 
273774b5143SMaxime COQUELIN 	return now <= rate && now > best;
274774b5143SMaxime COQUELIN }
275774b5143SMaxime COQUELIN 
_next_div(const struct clk_div_table * table,int div,unsigned long flags)276bca9690bSStephen Boyd static int _next_div(const struct clk_div_table *table, int div,
277bca9690bSStephen Boyd 		     unsigned long flags)
2780e2de78eSMaxime COQUELIN {
2790e2de78eSMaxime COQUELIN 	div++;
2800e2de78eSMaxime COQUELIN 
281bca9690bSStephen Boyd 	if (flags & CLK_DIVIDER_POWER_OF_TWO)
2820e2de78eSMaxime COQUELIN 		return __roundup_pow_of_two(div);
283bca9690bSStephen Boyd 	if (table)
284bca9690bSStephen Boyd 		return _round_up_table(table, div);
2850e2de78eSMaxime COQUELIN 
2860e2de78eSMaxime COQUELIN 	return div;
2870e2de78eSMaxime COQUELIN }
2880e2de78eSMaxime COQUELIN 
clk_divider_bestdiv(struct clk_hw * hw,struct clk_hw * parent,unsigned long rate,unsigned long * best_parent_rate,const struct clk_div_table * table,u8 width,unsigned long flags)28922833a91SMaxime Ripard static int clk_divider_bestdiv(struct clk_hw *hw, struct clk_hw *parent,
29022833a91SMaxime Ripard 			       unsigned long rate,
291bca9690bSStephen Boyd 			       unsigned long *best_parent_rate,
292bca9690bSStephen Boyd 			       const struct clk_div_table *table, u8 width,
293bca9690bSStephen Boyd 			       unsigned long flags)
2949d9f78edSMike Turquette {
2959d9f78edSMike Turquette 	int i, bestdiv = 0;
2969d9f78edSMike Turquette 	unsigned long parent_rate, best = 0, now, maxdiv;
297081c9025SShawn Guo 	unsigned long parent_rate_saved = *best_parent_rate;
2989d9f78edSMike Turquette 
2999d9f78edSMike Turquette 	if (!rate)
3009d9f78edSMike Turquette 		rate = 1;
3019d9f78edSMike Turquette 
302bca9690bSStephen Boyd 	maxdiv = _get_maxdiv(table, width, flags);
3039d9f78edSMike Turquette 
30498d8a60eSStephen Boyd 	if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
30581536e07SShawn Guo 		parent_rate = *best_parent_rate;
306bca9690bSStephen Boyd 		bestdiv = _div_round(table, parent_rate, rate, flags);
3079d9f78edSMike Turquette 		bestdiv = bestdiv == 0 ? 1 : bestdiv;
3089d9f78edSMike Turquette 		bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
3099d9f78edSMike Turquette 		return bestdiv;
3109d9f78edSMike Turquette 	}
3119d9f78edSMike Turquette 
3129d9f78edSMike Turquette 	/*
3139d9f78edSMike Turquette 	 * The maximum divider we can use without overflowing
3149d9f78edSMike Turquette 	 * unsigned long in rate * i below
3159d9f78edSMike Turquette 	 */
3169d9f78edSMike Turquette 	maxdiv = min(ULONG_MAX / rate, maxdiv);
3179d9f78edSMike Turquette 
318653d1452SMasahiro Yamada 	for (i = _next_div(table, 0, flags); i <= maxdiv;
319653d1452SMasahiro Yamada 					     i = _next_div(table, i, flags)) {
320081c9025SShawn Guo 		if (rate * i == parent_rate_saved) {
321081c9025SShawn Guo 			/*
322081c9025SShawn Guo 			 * It's the most ideal case if the requested rate can be
323081c9025SShawn Guo 			 * divided from parent clock without needing to change
324081c9025SShawn Guo 			 * parent rate, so return the divider immediately.
325081c9025SShawn Guo 			 */
326081c9025SShawn Guo 			*best_parent_rate = parent_rate_saved;
327081c9025SShawn Guo 			return i;
328081c9025SShawn Guo 		}
32922833a91SMaxime Ripard 		parent_rate = clk_hw_round_rate(parent, rate * i);
3309556f9daSBrian Norris 		now = DIV_ROUND_UP_ULL((u64)parent_rate, i);
331bca9690bSStephen Boyd 		if (_is_best_div(rate, now, best, flags)) {
3329d9f78edSMike Turquette 			bestdiv = i;
3339d9f78edSMike Turquette 			best = now;
3349d9f78edSMike Turquette 			*best_parent_rate = parent_rate;
3359d9f78edSMike Turquette 		}
3369d9f78edSMike Turquette 	}
3379d9f78edSMike Turquette 
3389d9f78edSMike Turquette 	if (!bestdiv) {
339bca9690bSStephen Boyd 		bestdiv = _get_maxdiv(table, width, flags);
34022833a91SMaxime Ripard 		*best_parent_rate = clk_hw_round_rate(parent, 1);
3419d9f78edSMike Turquette 	}
3429d9f78edSMike Turquette 
3439d9f78edSMike Turquette 	return bestdiv;
3449d9f78edSMike Turquette }
3459d9f78edSMike Turquette 
divider_determine_rate(struct clk_hw * hw,struct clk_rate_request * req,const struct clk_div_table * table,u8 width,unsigned long flags)346bbd7a6ccSMartin Blumenstingl int divider_determine_rate(struct clk_hw *hw, struct clk_rate_request *req,
347bbd7a6ccSMartin Blumenstingl 			   const struct clk_div_table *table, u8 width,
348bbd7a6ccSMartin Blumenstingl 			   unsigned long flags)
3499d9f78edSMike Turquette {
3509d9f78edSMike Turquette 	int div;
351bca9690bSStephen Boyd 
352bbd7a6ccSMartin Blumenstingl 	div = clk_divider_bestdiv(hw, req->best_parent_hw, req->rate,
353bbd7a6ccSMartin Blumenstingl 				  &req->best_parent_rate, table, width, flags);
3549d9f78edSMike Turquette 
355bbd7a6ccSMartin Blumenstingl 	req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div);
356bbd7a6ccSMartin Blumenstingl 
357bbd7a6ccSMartin Blumenstingl 	return 0;
3589d9f78edSMike Turquette }
359bbd7a6ccSMartin Blumenstingl EXPORT_SYMBOL_GPL(divider_determine_rate);
360bca9690bSStephen Boyd 
divider_ro_determine_rate(struct clk_hw * hw,struct clk_rate_request * req,const struct clk_div_table * table,u8 width,unsigned long flags,unsigned int val)361bbd7a6ccSMartin Blumenstingl int divider_ro_determine_rate(struct clk_hw *hw, struct clk_rate_request *req,
362b15ee490SJerome Brunet 			      const struct clk_div_table *table, u8 width,
363b15ee490SJerome Brunet 			      unsigned long flags, unsigned int val)
364b15ee490SJerome Brunet {
365b15ee490SJerome Brunet 	int div;
366b15ee490SJerome Brunet 
367b15ee490SJerome Brunet 	div = _get_div(table, val, flags, width);
368b15ee490SJerome Brunet 
369b15ee490SJerome Brunet 	/* Even a read-only clock can propagate a rate change */
370b15ee490SJerome Brunet 	if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
371bbd7a6ccSMartin Blumenstingl 		if (!req->best_parent_hw)
372b15ee490SJerome Brunet 			return -EINVAL;
373b15ee490SJerome Brunet 
374bbd7a6ccSMartin Blumenstingl 		req->best_parent_rate = clk_hw_round_rate(req->best_parent_hw,
375bbd7a6ccSMartin Blumenstingl 							  req->rate * div);
376b15ee490SJerome Brunet 	}
377b15ee490SJerome Brunet 
378bbd7a6ccSMartin Blumenstingl 	req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div);
379bbd7a6ccSMartin Blumenstingl 
380bbd7a6ccSMartin Blumenstingl 	return 0;
381bbd7a6ccSMartin Blumenstingl }
382bbd7a6ccSMartin Blumenstingl EXPORT_SYMBOL_GPL(divider_ro_determine_rate);
383bbd7a6ccSMartin Blumenstingl 
divider_round_rate_parent(struct clk_hw * hw,struct clk_hw * parent,unsigned long rate,unsigned long * prate,const struct clk_div_table * table,u8 width,unsigned long flags)384bbd7a6ccSMartin Blumenstingl long divider_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
385bbd7a6ccSMartin Blumenstingl 			       unsigned long rate, unsigned long *prate,
386bbd7a6ccSMartin Blumenstingl 			       const struct clk_div_table *table,
387bbd7a6ccSMartin Blumenstingl 			       u8 width, unsigned long flags)
388bbd7a6ccSMartin Blumenstingl {
389*c35e84b0SMaxime Ripard 	struct clk_rate_request req;
390bbd7a6ccSMartin Blumenstingl 	int ret;
391bbd7a6ccSMartin Blumenstingl 
392*c35e84b0SMaxime Ripard 	clk_hw_init_rate_request(hw, &req, rate);
393*c35e84b0SMaxime Ripard 	req.best_parent_rate = *prate;
394*c35e84b0SMaxime Ripard 	req.best_parent_hw = parent;
395*c35e84b0SMaxime Ripard 
396bbd7a6ccSMartin Blumenstingl 	ret = divider_determine_rate(hw, &req, table, width, flags);
397bbd7a6ccSMartin Blumenstingl 	if (ret)
398bbd7a6ccSMartin Blumenstingl 		return ret;
399bbd7a6ccSMartin Blumenstingl 
400bbd7a6ccSMartin Blumenstingl 	*prate = req.best_parent_rate;
401bbd7a6ccSMartin Blumenstingl 
402bbd7a6ccSMartin Blumenstingl 	return req.rate;
403bbd7a6ccSMartin Blumenstingl }
404bbd7a6ccSMartin Blumenstingl EXPORT_SYMBOL_GPL(divider_round_rate_parent);
405bbd7a6ccSMartin Blumenstingl 
divider_ro_round_rate_parent(struct clk_hw * hw,struct clk_hw * parent,unsigned long rate,unsigned long * prate,const struct clk_div_table * table,u8 width,unsigned long flags,unsigned int val)406bbd7a6ccSMartin Blumenstingl long divider_ro_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
407bbd7a6ccSMartin Blumenstingl 				  unsigned long rate, unsigned long *prate,
408bbd7a6ccSMartin Blumenstingl 				  const struct clk_div_table *table, u8 width,
409bbd7a6ccSMartin Blumenstingl 				  unsigned long flags, unsigned int val)
410bbd7a6ccSMartin Blumenstingl {
411*c35e84b0SMaxime Ripard 	struct clk_rate_request req;
412bbd7a6ccSMartin Blumenstingl 	int ret;
413bbd7a6ccSMartin Blumenstingl 
414*c35e84b0SMaxime Ripard 	clk_hw_init_rate_request(hw, &req, rate);
415*c35e84b0SMaxime Ripard 	req.best_parent_rate = *prate;
416*c35e84b0SMaxime Ripard 	req.best_parent_hw = parent;
417*c35e84b0SMaxime Ripard 
418bbd7a6ccSMartin Blumenstingl 	ret = divider_ro_determine_rate(hw, &req, table, width, flags, val);
419bbd7a6ccSMartin Blumenstingl 	if (ret)
420bbd7a6ccSMartin Blumenstingl 		return ret;
421bbd7a6ccSMartin Blumenstingl 
422bbd7a6ccSMartin Blumenstingl 	*prate = req.best_parent_rate;
423bbd7a6ccSMartin Blumenstingl 
424bbd7a6ccSMartin Blumenstingl 	return req.rate;
425b15ee490SJerome Brunet }
426b15ee490SJerome Brunet EXPORT_SYMBOL_GPL(divider_ro_round_rate_parent);
427b15ee490SJerome Brunet 
clk_divider_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * prate)428783d08bdSStephen Boyd static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
429783d08bdSStephen Boyd 				unsigned long *prate)
430bca9690bSStephen Boyd {
431bca9690bSStephen Boyd 	struct clk_divider *divider = to_clk_divider(hw);
432bca9690bSStephen Boyd 
433bca9690bSStephen Boyd 	/* if read only, just return current value */
434bca9690bSStephen Boyd 	if (divider->flags & CLK_DIVIDER_READ_ONLY) {
435b15ee490SJerome Brunet 		u32 val;
436b15ee490SJerome Brunet 
437434d69faSJonas Gorski 		val = clk_div_readl(divider) >> divider->shift;
438b15ee490SJerome Brunet 		val &= clk_div_mask(divider->width);
439b15ee490SJerome Brunet 
440783d08bdSStephen Boyd 		return divider_ro_round_rate(hw, rate, prate, divider->table,
441783d08bdSStephen Boyd 					     divider->width, divider->flags,
442783d08bdSStephen Boyd 					     val);
443bca9690bSStephen Boyd 	}
444bca9690bSStephen Boyd 
445783d08bdSStephen Boyd 	return divider_round_rate(hw, rate, prate, divider->table,
446783d08bdSStephen Boyd 				  divider->width, divider->flags);
447bca9690bSStephen Boyd }
448bca9690bSStephen Boyd 
clk_divider_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)44969a00fb3SMartin Blumenstingl static int clk_divider_determine_rate(struct clk_hw *hw,
45069a00fb3SMartin Blumenstingl 				      struct clk_rate_request *req)
45169a00fb3SMartin Blumenstingl {
45269a00fb3SMartin Blumenstingl 	struct clk_divider *divider = to_clk_divider(hw);
45369a00fb3SMartin Blumenstingl 
45469a00fb3SMartin Blumenstingl 	/* if read only, just return current value */
45569a00fb3SMartin Blumenstingl 	if (divider->flags & CLK_DIVIDER_READ_ONLY) {
45669a00fb3SMartin Blumenstingl 		u32 val;
45769a00fb3SMartin Blumenstingl 
45869a00fb3SMartin Blumenstingl 		val = clk_div_readl(divider) >> divider->shift;
45969a00fb3SMartin Blumenstingl 		val &= clk_div_mask(divider->width);
46069a00fb3SMartin Blumenstingl 
46169a00fb3SMartin Blumenstingl 		return divider_ro_determine_rate(hw, req, divider->table,
46269a00fb3SMartin Blumenstingl 						 divider->width,
46369a00fb3SMartin Blumenstingl 						 divider->flags, val);
46469a00fb3SMartin Blumenstingl 	}
46569a00fb3SMartin Blumenstingl 
46669a00fb3SMartin Blumenstingl 	return divider_determine_rate(hw, req, divider->table, divider->width,
46769a00fb3SMartin Blumenstingl 				      divider->flags);
46869a00fb3SMartin Blumenstingl }
46969a00fb3SMartin Blumenstingl 
divider_get_val(unsigned long rate,unsigned long parent_rate,const struct clk_div_table * table,u8 width,unsigned long flags)470bca9690bSStephen Boyd int divider_get_val(unsigned long rate, unsigned long parent_rate,
471bca9690bSStephen Boyd 		    const struct clk_div_table *table, u8 width,
472bca9690bSStephen Boyd 		    unsigned long flags)
473bca9690bSStephen Boyd {
474bca9690bSStephen Boyd 	unsigned int div, value;
475bca9690bSStephen Boyd 
4769556f9daSBrian Norris 	div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
477bca9690bSStephen Boyd 
478bca9690bSStephen Boyd 	if (!_is_valid_div(table, div, flags))
479bca9690bSStephen Boyd 		return -EINVAL;
480bca9690bSStephen Boyd 
481afe76c8fSJim Quinlan 	value = _get_val(table, div, flags, width);
482bca9690bSStephen Boyd 
483e6d3cc7bSJerome Brunet 	return min_t(unsigned int, value, clk_div_mask(width));
484bca9690bSStephen Boyd }
485bca9690bSStephen Boyd EXPORT_SYMBOL_GPL(divider_get_val);
4869d9f78edSMike Turquette 
clk_divider_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)4871c0035d7SShawn Guo static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
4881c0035d7SShawn Guo 				unsigned long parent_rate)
4899d9f78edSMike Turquette {
4909d9f78edSMike Turquette 	struct clk_divider *divider = to_clk_divider(hw);
4912316a7a3SAlex Frid 	int value;
4929d9f78edSMike Turquette 	unsigned long flags = 0;
4939d9f78edSMike Turquette 	u32 val;
4949d9f78edSMike Turquette 
495bca9690bSStephen Boyd 	value = divider_get_val(rate, parent_rate, divider->table,
496bca9690bSStephen Boyd 				divider->width, divider->flags);
4972316a7a3SAlex Frid 	if (value < 0)
4982316a7a3SAlex Frid 		return value;
4999d9f78edSMike Turquette 
5009d9f78edSMike Turquette 	if (divider->lock)
5019d9f78edSMike Turquette 		spin_lock_irqsave(divider->lock, flags);
502661e2180SStephen Boyd 	else
503661e2180SStephen Boyd 		__acquire(divider->lock);
5049d9f78edSMike Turquette 
505d57dfe75SHaojian Zhuang 	if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
506e6d3cc7bSJerome Brunet 		val = clk_div_mask(divider->width) << (divider->shift + 16);
507d57dfe75SHaojian Zhuang 	} else {
508434d69faSJonas Gorski 		val = clk_div_readl(divider);
509e6d3cc7bSJerome Brunet 		val &= ~(clk_div_mask(divider->width) << divider->shift);
510d57dfe75SHaojian Zhuang 	}
5112316a7a3SAlex Frid 	val |= (u32)value << divider->shift;
512434d69faSJonas Gorski 	clk_div_writel(divider, val);
5139d9f78edSMike Turquette 
5149d9f78edSMike Turquette 	if (divider->lock)
5159d9f78edSMike Turquette 		spin_unlock_irqrestore(divider->lock, flags);
516661e2180SStephen Boyd 	else
517661e2180SStephen Boyd 		__release(divider->lock);
5189d9f78edSMike Turquette 
5199d9f78edSMike Turquette 	return 0;
5209d9f78edSMike Turquette }
5219d9f78edSMike Turquette 
522822c250eSShawn Guo const struct clk_ops clk_divider_ops = {
5239d9f78edSMike Turquette 	.recalc_rate = clk_divider_recalc_rate,
524783d08bdSStephen Boyd 	.round_rate = clk_divider_round_rate,
52569a00fb3SMartin Blumenstingl 	.determine_rate = clk_divider_determine_rate,
5269d9f78edSMike Turquette 	.set_rate = clk_divider_set_rate,
5279d9f78edSMike Turquette };
5289d9f78edSMike Turquette EXPORT_SYMBOL_GPL(clk_divider_ops);
5299d9f78edSMike Turquette 
53050359819SHeiko Stuebner const struct clk_ops clk_divider_ro_ops = {
53150359819SHeiko Stuebner 	.recalc_rate = clk_divider_recalc_rate,
532783d08bdSStephen Boyd 	.round_rate = clk_divider_round_rate,
53369a00fb3SMartin Blumenstingl 	.determine_rate = clk_divider_determine_rate,
53450359819SHeiko Stuebner };
53550359819SHeiko Stuebner EXPORT_SYMBOL_GPL(clk_divider_ro_ops);
53650359819SHeiko Stuebner 
__clk_hw_register_divider(struct device * dev,struct device_node * np,const char * name,const char * parent_name,const struct clk_hw * parent_hw,const struct clk_parent_data * parent_data,unsigned long flags,void __iomem * reg,u8 shift,u8 width,u8 clk_divider_flags,const struct clk_div_table * table,spinlock_t * lock)537ff258817SStephen Boyd struct clk_hw *__clk_hw_register_divider(struct device *dev,
538ff258817SStephen Boyd 		struct device_node *np, const char *name,
539ff258817SStephen Boyd 		const char *parent_name, const struct clk_hw *parent_hw,
540ff258817SStephen Boyd 		const struct clk_parent_data *parent_data, unsigned long flags,
541ff258817SStephen Boyd 		void __iomem *reg, u8 shift, u8 width, u8 clk_divider_flags,
542ff258817SStephen Boyd 		const struct clk_div_table *table, spinlock_t *lock)
5439d9f78edSMike Turquette {
5449d9f78edSMike Turquette 	struct clk_divider *div;
545eb7d264fSStephen Boyd 	struct clk_hw *hw;
546cc819cf8SManivannan Sadhasivam 	struct clk_init_data init = {};
547eb7d264fSStephen Boyd 	int ret;
5489d9f78edSMike Turquette 
549d57dfe75SHaojian Zhuang 	if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
550d57dfe75SHaojian Zhuang 		if (width + shift > 16) {
551d57dfe75SHaojian Zhuang 			pr_warn("divider value exceeds LOWORD field\n");
552d57dfe75SHaojian Zhuang 			return ERR_PTR(-EINVAL);
553d57dfe75SHaojian Zhuang 		}
554d57dfe75SHaojian Zhuang 	}
555d57dfe75SHaojian Zhuang 
55627d54591SMike Turquette 	/* allocate the divider */
557d122db7eSStephen Boyd 	div = kzalloc(sizeof(*div), GFP_KERNEL);
558d122db7eSStephen Boyd 	if (!div)
55927d54591SMike Turquette 		return ERR_PTR(-ENOMEM);
5609d9f78edSMike Turquette 
5610197b3eaSSaravana Kannan 	init.name = name;
56250359819SHeiko Stuebner 	if (clk_divider_flags & CLK_DIVIDER_READ_ONLY)
56350359819SHeiko Stuebner 		init.ops = &clk_divider_ro_ops;
56450359819SHeiko Stuebner 	else
5650197b3eaSSaravana Kannan 		init.ops = &clk_divider_ops;
56690b6c5c7SStephen Boyd 	init.flags = flags;
5670225daeaSMichael Tretter 	init.parent_names = parent_name ? &parent_name : NULL;
5680225daeaSMichael Tretter 	init.parent_hws = parent_hw ? &parent_hw : NULL;
5690225daeaSMichael Tretter 	init.parent_data = parent_data;
5700225daeaSMichael Tretter 	if (parent_name || parent_hw || parent_data)
5710225daeaSMichael Tretter 		init.num_parents = 1;
5720225daeaSMichael Tretter 	else
5730225daeaSMichael Tretter 		init.num_parents = 0;
5740197b3eaSSaravana Kannan 
5759d9f78edSMike Turquette 	/* struct clk_divider assignments */
5769d9f78edSMike Turquette 	div->reg = reg;
5779d9f78edSMike Turquette 	div->shift = shift;
5789d9f78edSMike Turquette 	div->width = width;
5799d9f78edSMike Turquette 	div->flags = clk_divider_flags;
5809d9f78edSMike Turquette 	div->lock = lock;
5810197b3eaSSaravana Kannan 	div->hw.init = &init;
582357c3f0aSRajendra Nayak 	div->table = table;
5839d9f78edSMike Turquette 
58427d54591SMike Turquette 	/* register the clock */
585eb7d264fSStephen Boyd 	hw = &div->hw;
586eb7d264fSStephen Boyd 	ret = clk_hw_register(dev, hw);
587eb7d264fSStephen Boyd 	if (ret) {
5889d9f78edSMike Turquette 		kfree(div);
589eb7d264fSStephen Boyd 		hw = ERR_PTR(ret);
590eb7d264fSStephen Boyd 	}
5919d9f78edSMike Turquette 
592eb7d264fSStephen Boyd 	return hw;
5939d9f78edSMike Turquette }
594ff258817SStephen Boyd EXPORT_SYMBOL_GPL(__clk_hw_register_divider);
595357c3f0aSRajendra Nayak 
596357c3f0aSRajendra Nayak /**
597357c3f0aSRajendra Nayak  * clk_register_divider_table - register a table based divider clock with
598357c3f0aSRajendra Nayak  * the clock framework
599357c3f0aSRajendra Nayak  * @dev: device registering this clock
600357c3f0aSRajendra Nayak  * @name: name of this clock
601357c3f0aSRajendra Nayak  * @parent_name: name of clock's parent
602357c3f0aSRajendra Nayak  * @flags: framework-specific flags
603357c3f0aSRajendra Nayak  * @reg: register address to adjust divider
604357c3f0aSRajendra Nayak  * @shift: number of bits to shift the bitfield
605357c3f0aSRajendra Nayak  * @width: width of the bitfield
606357c3f0aSRajendra Nayak  * @clk_divider_flags: divider-specific flags for this clock
607357c3f0aSRajendra Nayak  * @table: array of divider/value pairs ending with a div set to 0
608357c3f0aSRajendra Nayak  * @lock: shared register lock for this clock
609357c3f0aSRajendra Nayak  */
clk_register_divider_table(struct device * dev,const char * name,const char * parent_name,unsigned long flags,void __iomem * reg,u8 shift,u8 width,u8 clk_divider_flags,const struct clk_div_table * table,spinlock_t * lock)610357c3f0aSRajendra Nayak struct clk *clk_register_divider_table(struct device *dev, const char *name,
611357c3f0aSRajendra Nayak 		const char *parent_name, unsigned long flags,
612357c3f0aSRajendra Nayak 		void __iomem *reg, u8 shift, u8 width,
613357c3f0aSRajendra Nayak 		u8 clk_divider_flags, const struct clk_div_table *table,
614357c3f0aSRajendra Nayak 		spinlock_t *lock)
615357c3f0aSRajendra Nayak {
616eb7d264fSStephen Boyd 	struct clk_hw *hw;
617eb7d264fSStephen Boyd 
618ff258817SStephen Boyd 	hw =  __clk_hw_register_divider(dev, NULL, name, parent_name, NULL,
619ff258817SStephen Boyd 			NULL, flags, reg, shift, width, clk_divider_flags,
620ff258817SStephen Boyd 			table, lock);
621eb7d264fSStephen Boyd 	if (IS_ERR(hw))
622eb7d264fSStephen Boyd 		return ERR_CAST(hw);
623eb7d264fSStephen Boyd 	return hw->clk;
624eb7d264fSStephen Boyd }
625eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_register_divider_table);
626eb7d264fSStephen Boyd 
clk_unregister_divider(struct clk * clk)6274e3c021fSKrzysztof Kozlowski void clk_unregister_divider(struct clk *clk)
6284e3c021fSKrzysztof Kozlowski {
6294e3c021fSKrzysztof Kozlowski 	struct clk_divider *div;
6304e3c021fSKrzysztof Kozlowski 	struct clk_hw *hw;
6314e3c021fSKrzysztof Kozlowski 
6324e3c021fSKrzysztof Kozlowski 	hw = __clk_get_hw(clk);
6334e3c021fSKrzysztof Kozlowski 	if (!hw)
6344e3c021fSKrzysztof Kozlowski 		return;
6354e3c021fSKrzysztof Kozlowski 
6364e3c021fSKrzysztof Kozlowski 	div = to_clk_divider(hw);
6374e3c021fSKrzysztof Kozlowski 
6384e3c021fSKrzysztof Kozlowski 	clk_unregister(clk);
6394e3c021fSKrzysztof Kozlowski 	kfree(div);
6404e3c021fSKrzysztof Kozlowski }
6414e3c021fSKrzysztof Kozlowski EXPORT_SYMBOL_GPL(clk_unregister_divider);
642eb7d264fSStephen Boyd 
643eb7d264fSStephen Boyd /**
644eb7d264fSStephen Boyd  * clk_hw_unregister_divider - unregister a clk divider
645eb7d264fSStephen Boyd  * @hw: hardware-specific clock data to unregister
646eb7d264fSStephen Boyd  */
clk_hw_unregister_divider(struct clk_hw * hw)647eb7d264fSStephen Boyd void clk_hw_unregister_divider(struct clk_hw *hw)
648eb7d264fSStephen Boyd {
649eb7d264fSStephen Boyd 	struct clk_divider *div;
650eb7d264fSStephen Boyd 
651eb7d264fSStephen Boyd 	div = to_clk_divider(hw);
652eb7d264fSStephen Boyd 
653eb7d264fSStephen Boyd 	clk_hw_unregister(hw);
654eb7d264fSStephen Boyd 	kfree(div);
655eb7d264fSStephen Boyd }
656eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_hw_unregister_divider);
65726792699SMichael Walle 
devm_clk_hw_release_divider(struct device * dev,void * res)65826792699SMichael Walle static void devm_clk_hw_release_divider(struct device *dev, void *res)
65926792699SMichael Walle {
66026792699SMichael Walle 	clk_hw_unregister_divider(*(struct clk_hw **)res);
66126792699SMichael Walle }
66226792699SMichael Walle 
__devm_clk_hw_register_divider(struct device * dev,struct device_node * np,const char * name,const char * parent_name,const struct clk_hw * parent_hw,const struct clk_parent_data * parent_data,unsigned long flags,void __iomem * reg,u8 shift,u8 width,u8 clk_divider_flags,const struct clk_div_table * table,spinlock_t * lock)66326792699SMichael Walle struct clk_hw *__devm_clk_hw_register_divider(struct device *dev,
66426792699SMichael Walle 		struct device_node *np, const char *name,
66526792699SMichael Walle 		const char *parent_name, const struct clk_hw *parent_hw,
66626792699SMichael Walle 		const struct clk_parent_data *parent_data, unsigned long flags,
66726792699SMichael Walle 		void __iomem *reg, u8 shift, u8 width, u8 clk_divider_flags,
66826792699SMichael Walle 		const struct clk_div_table *table, spinlock_t *lock)
66926792699SMichael Walle {
67026792699SMichael Walle 	struct clk_hw **ptr, *hw;
67126792699SMichael Walle 
67226792699SMichael Walle 	ptr = devres_alloc(devm_clk_hw_release_divider, sizeof(*ptr), GFP_KERNEL);
67326792699SMichael Walle 	if (!ptr)
67426792699SMichael Walle 		return ERR_PTR(-ENOMEM);
67526792699SMichael Walle 
67626792699SMichael Walle 	hw = __clk_hw_register_divider(dev, np, name, parent_name, parent_hw,
67726792699SMichael Walle 				       parent_data, flags, reg, shift, width,
67826792699SMichael Walle 				       clk_divider_flags, table, lock);
67926792699SMichael Walle 
68026792699SMichael Walle 	if (!IS_ERR(hw)) {
68126792699SMichael Walle 		*ptr = hw;
68226792699SMichael Walle 		devres_add(dev, ptr);
68326792699SMichael Walle 	} else {
68426792699SMichael Walle 		devres_free(ptr);
68526792699SMichael Walle 	}
68626792699SMichael Walle 
68726792699SMichael Walle 	return hw;
68826792699SMichael Walle }
68926792699SMichael Walle EXPORT_SYMBOL_GPL(__devm_clk_hw_register_divider);
690