Linux Audio

Check our new training course

Buildroot integration, development and maintenance

Need a Buildroot system for your embedded project?
Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  4 * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
  5 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
  6 *
  7 * Adjustable divider clock implementation
  8 */
  9
 10#include <linux/clk-provider.h>
 11#include <linux/device.h>
 12#include <linux/module.h>
 13#include <linux/slab.h>
 14#include <linux/io.h>
 15#include <linux/err.h>
 16#include <linux/string.h>
 17#include <linux/log2.h>
 18
 19/*
 20 * DOC: basic adjustable divider clock that cannot gate
 21 *
 22 * Traits of this clock:
 23 * prepare - clk_prepare only ensures that parents are prepared
 24 * enable - clk_enable only ensures that parents are enabled
 25 * rate - rate is adjustable.  clk->rate = ceiling(parent->rate / divisor)
 26 * parent - fixed parent.  No clk_set_parent support
 27 */
 28
 29static inline u32 clk_div_readl(struct clk_divider *divider)
 30{
 31	if (divider->flags & CLK_DIVIDER_BIG_ENDIAN)
 32		return ioread32be(divider->reg);
 33
 34	return readl(divider->reg);
 35}
 36
 37static inline void clk_div_writel(struct clk_divider *divider, u32 val)
 38{
 39	if (divider->flags & CLK_DIVIDER_BIG_ENDIAN)
 40		iowrite32be(val, divider->reg);
 41	else
 42		writel(val, divider->reg);
 43}
 44
 45static unsigned int _get_table_maxdiv(const struct clk_div_table *table,
 46				      u8 width)
 47{
 48	unsigned int maxdiv = 0, mask = clk_div_mask(width);
 49	const struct clk_div_table *clkt;
 50
 51	for (clkt = table; clkt->div; clkt++)
 52		if (clkt->div > maxdiv && clkt->val <= mask)
 53			maxdiv = clkt->div;
 54	return maxdiv;
 55}
 56
 57static unsigned int _get_table_mindiv(const struct clk_div_table *table)
 58{
 59	unsigned int mindiv = UINT_MAX;
 60	const struct clk_div_table *clkt;
 61
 62	for (clkt = table; clkt->div; clkt++)
 63		if (clkt->div < mindiv)
 64			mindiv = clkt->div;
 65	return mindiv;
 66}
 67
 68static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width,
 69				unsigned long flags)
 70{
 71	if (flags & CLK_DIVIDER_ONE_BASED)
 72		return clk_div_mask(width);
 73	if (flags & CLK_DIVIDER_POWER_OF_TWO)
 74		return 1 << clk_div_mask(width);
 75	if (flags & CLK_DIVIDER_EVEN_INTEGERS)
 76		return 2 * (clk_div_mask(width) + 1);
 77	if (table)
 78		return _get_table_maxdiv(table, width);
 79	return clk_div_mask(width) + 1;
 80}
 81
 82static unsigned int _get_table_div(const struct clk_div_table *table,
 83							unsigned int val)
 84{
 85	const struct clk_div_table *clkt;
 86
 87	for (clkt = table; clkt->div; clkt++)
 88		if (clkt->val == val)
 89			return clkt->div;
 90	return 0;
 91}
 92
 93static unsigned int _get_div(const struct clk_div_table *table,
 94			     unsigned int val, unsigned long flags, u8 width)
 95{
 96	if (flags & CLK_DIVIDER_ONE_BASED)
 97		return val;
 98	if (flags & CLK_DIVIDER_POWER_OF_TWO)
 99		return 1 << val;
100	if (flags & CLK_DIVIDER_MAX_AT_ZERO)
101		return val ? val : clk_div_mask(width) + 1;
102	if (flags & CLK_DIVIDER_EVEN_INTEGERS)
103		return 2 * (val + 1);
104	if (table)
105		return _get_table_div(table, val);
106	return val + 1;
107}
108
109static unsigned int _get_table_val(const struct clk_div_table *table,
110							unsigned int div)
111{
112	const struct clk_div_table *clkt;
113
114	for (clkt = table; clkt->div; clkt++)
115		if (clkt->div == div)
116			return clkt->val;
117	return 0;
118}
119
120static unsigned int _get_val(const struct clk_div_table *table,
121			     unsigned int div, unsigned long flags, u8 width)
122{
123	if (flags & CLK_DIVIDER_ONE_BASED)
124		return div;
125	if (flags & CLK_DIVIDER_POWER_OF_TWO)
126		return __ffs(div);
127	if (flags & CLK_DIVIDER_MAX_AT_ZERO)
128		return (div == clk_div_mask(width) + 1) ? 0 : div;
129	if (flags & CLK_DIVIDER_EVEN_INTEGERS)
130		return (div >> 1) - 1;
131	if (table)
132		return  _get_table_val(table, div);
133	return div - 1;
134}
135
136unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate,
137				  unsigned int val,
138				  const struct clk_div_table *table,
139				  unsigned long flags, unsigned long width)
140{
141	unsigned int div;
142
143	div = _get_div(table, val, flags, width);
144	if (!div) {
145		WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO),
146			"%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
147			clk_hw_get_name(hw));
148		return parent_rate;
149	}
150
151	return DIV_ROUND_UP_ULL((u64)parent_rate, div);
152}
153EXPORT_SYMBOL_GPL(divider_recalc_rate);
154
155static unsigned long clk_divider_recalc_rate(struct clk_hw *hw,
156		unsigned long parent_rate)
157{
158	struct clk_divider *divider = to_clk_divider(hw);
159	unsigned int val;
160
161	val = clk_div_readl(divider) >> divider->shift;
162	val &= clk_div_mask(divider->width);
163
164	return divider_recalc_rate(hw, parent_rate, val, divider->table,
165				   divider->flags, divider->width);
166}
167
168static bool _is_valid_table_div(const struct clk_div_table *table,
169							 unsigned int div)
170{
171	const struct clk_div_table *clkt;
172
173	for (clkt = table; clkt->div; clkt++)
174		if (clkt->div == div)
175			return true;
176	return false;
177}
178
179static bool _is_valid_div(const struct clk_div_table *table, unsigned int div,
180			  unsigned long flags)
181{
182	if (flags & CLK_DIVIDER_POWER_OF_TWO)
183		return is_power_of_2(div);
184	if (table)
185		return _is_valid_table_div(table, div);
186	return true;
187}
188
189static int _round_up_table(const struct clk_div_table *table, int div)
190{
191	const struct clk_div_table *clkt;
192	int up = INT_MAX;
193
194	for (clkt = table; clkt->div; clkt++) {
195		if (clkt->div == div)
196			return clkt->div;
197		else if (clkt->div < div)
198			continue;
199
200		if ((clkt->div - div) < (up - div))
201			up = clkt->div;
202	}
203
204	return up;
205}
206
207static int _round_down_table(const struct clk_div_table *table, int div)
208{
209	const struct clk_div_table *clkt;
210	int down = _get_table_mindiv(table);
211
212	for (clkt = table; clkt->div; clkt++) {
213		if (clkt->div == div)
214			return clkt->div;
215		else if (clkt->div > div)
216			continue;
217
218		if ((div - clkt->div) < (div - down))
219			down = clkt->div;
220	}
221
222	return down;
223}
224
225static int _div_round_up(const struct clk_div_table *table,
226			 unsigned long parent_rate, unsigned long rate,
227			 unsigned long flags)
228{
229	int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
230
231	if (flags & CLK_DIVIDER_POWER_OF_TWO)
232		div = __roundup_pow_of_two(div);
233	if (table)
234		div = _round_up_table(table, div);
235
236	return div;
237}
238
239static int _div_round_closest(const struct clk_div_table *table,
240			      unsigned long parent_rate, unsigned long rate,
241			      unsigned long flags)
242{
243	int up, down;
244	unsigned long up_rate, down_rate;
245
246	up = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
247	down = parent_rate / rate;
248
249	if (flags & CLK_DIVIDER_POWER_OF_TWO) {
250		up = __roundup_pow_of_two(up);
251		down = __rounddown_pow_of_two(down);
252	} else if (table) {
253		up = _round_up_table(table, up);
254		down = _round_down_table(table, down);
255	}
256
257	up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up);
258	down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down);
259
260	return (rate - up_rate) <= (down_rate - rate) ? up : down;
261}
262
263static int _div_round(const struct clk_div_table *table,
264		      unsigned long parent_rate, unsigned long rate,
265		      unsigned long flags)
266{
267	if (flags & CLK_DIVIDER_ROUND_CLOSEST)
268		return _div_round_closest(table, parent_rate, rate, flags);
269
270	return _div_round_up(table, parent_rate, rate, flags);
271}
272
273static bool _is_best_div(unsigned long rate, unsigned long now,
274			 unsigned long best, unsigned long flags)
275{
276	if (flags & CLK_DIVIDER_ROUND_CLOSEST)
277		return abs(rate - now) < abs(rate - best);
278
279	return now <= rate && now > best;
280}
281
282static int _next_div(const struct clk_div_table *table, int div,
283		     unsigned long flags)
284{
285	div++;
286
287	if (flags & CLK_DIVIDER_POWER_OF_TWO)
288		return __roundup_pow_of_two(div);
289	if (table)
290		return _round_up_table(table, div);
291
292	return div;
293}
294
295static int clk_divider_bestdiv(struct clk_hw *hw, struct clk_hw *parent,
296			       unsigned long rate,
297			       unsigned long *best_parent_rate,
298			       const struct clk_div_table *table, u8 width,
299			       unsigned long flags)
300{
301	int i, bestdiv = 0;
302	unsigned long parent_rate, best = 0, now, maxdiv;
303	unsigned long parent_rate_saved = *best_parent_rate;
304
305	if (!rate)
306		rate = 1;
307
308	maxdiv = _get_maxdiv(table, width, flags);
309
310	if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
311		parent_rate = *best_parent_rate;
312		bestdiv = _div_round(table, parent_rate, rate, flags);
313		bestdiv = bestdiv == 0 ? 1 : bestdiv;
314		bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
315		return bestdiv;
316	}
317
318	/*
319	 * The maximum divider we can use without overflowing
320	 * unsigned long in rate * i below
321	 */
322	maxdiv = min(ULONG_MAX / rate, maxdiv);
323
324	for (i = _next_div(table, 0, flags); i <= maxdiv;
325					     i = _next_div(table, i, flags)) {
326		if (rate * i == parent_rate_saved) {
327			/*
328			 * It's the most ideal case if the requested rate can be
329			 * divided from parent clock without needing to change
330			 * parent rate, so return the divider immediately.
331			 */
332			*best_parent_rate = parent_rate_saved;
333			return i;
334		}
335		parent_rate = clk_hw_round_rate(parent, rate * i);
336		now = DIV_ROUND_UP_ULL((u64)parent_rate, i);
337		if (_is_best_div(rate, now, best, flags)) {
338			bestdiv = i;
339			best = now;
340			*best_parent_rate = parent_rate;
341		}
342	}
343
344	if (!bestdiv) {
345		bestdiv = _get_maxdiv(table, width, flags);
346		*best_parent_rate = clk_hw_round_rate(parent, 1);
347	}
348
349	return bestdiv;
350}
351
352int divider_determine_rate(struct clk_hw *hw, struct clk_rate_request *req,
353			   const struct clk_div_table *table, u8 width,
354			   unsigned long flags)
355{
356	int div;
357
358	div = clk_divider_bestdiv(hw, req->best_parent_hw, req->rate,
359				  &req->best_parent_rate, table, width, flags);
360
361	req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div);
362
363	return 0;
364}
365EXPORT_SYMBOL_GPL(divider_determine_rate);
366
367int divider_ro_determine_rate(struct clk_hw *hw, struct clk_rate_request *req,
368			      const struct clk_div_table *table, u8 width,
369			      unsigned long flags, unsigned int val)
370{
371	int div;
372
373	div = _get_div(table, val, flags, width);
374
375	/* Even a read-only clock can propagate a rate change */
376	if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
377		if (!req->best_parent_hw)
378			return -EINVAL;
379
380		req->best_parent_rate = clk_hw_round_rate(req->best_parent_hw,
381							  req->rate * div);
382	}
383
384	req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div);
385
386	return 0;
387}
388EXPORT_SYMBOL_GPL(divider_ro_determine_rate);
389
390long divider_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
391			       unsigned long rate, unsigned long *prate,
392			       const struct clk_div_table *table,
393			       u8 width, unsigned long flags)
394{
395	struct clk_rate_request req;
396	int ret;
397
398	clk_hw_init_rate_request(hw, &req, rate);
399	req.best_parent_rate = *prate;
400	req.best_parent_hw = parent;
401
402	ret = divider_determine_rate(hw, &req, table, width, flags);
403	if (ret)
404		return ret;
405
406	*prate = req.best_parent_rate;
407
408	return req.rate;
409}
410EXPORT_SYMBOL_GPL(divider_round_rate_parent);
411
412long divider_ro_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
413				  unsigned long rate, unsigned long *prate,
414				  const struct clk_div_table *table, u8 width,
415				  unsigned long flags, unsigned int val)
416{
417	struct clk_rate_request req;
418	int ret;
419
420	clk_hw_init_rate_request(hw, &req, rate);
421	req.best_parent_rate = *prate;
422	req.best_parent_hw = parent;
423
424	ret = divider_ro_determine_rate(hw, &req, table, width, flags, val);
425	if (ret)
426		return ret;
 
427
428	*prate = req.best_parent_rate;
 
429
430	return req.rate;
431}
432EXPORT_SYMBOL_GPL(divider_ro_round_rate_parent);
433
 
434static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
435				unsigned long *prate)
436{
437	struct clk_divider *divider = to_clk_divider(hw);
438
439	/* if read only, just return current value */
440	if (divider->flags & CLK_DIVIDER_READ_ONLY) {
441		u32 val;
442
443		val = clk_div_readl(divider) >> divider->shift;
444		val &= clk_div_mask(divider->width);
445
446		return divider_ro_round_rate(hw, rate, prate, divider->table,
447					     divider->width, divider->flags,
448					     val);
449	}
450
451	return divider_round_rate(hw, rate, prate, divider->table,
452				  divider->width, divider->flags);
453}
454
455static int clk_divider_determine_rate(struct clk_hw *hw,
456				      struct clk_rate_request *req)
457{
458	struct clk_divider *divider = to_clk_divider(hw);
459
460	/* if read only, just return current value */
461	if (divider->flags & CLK_DIVIDER_READ_ONLY) {
462		u32 val;
463
464		val = clk_div_readl(divider) >> divider->shift;
465		val &= clk_div_mask(divider->width);
466
467		return divider_ro_determine_rate(hw, req, divider->table,
468						 divider->width,
469						 divider->flags, val);
470	}
471
472	return divider_determine_rate(hw, req, divider->table, divider->width,
473				      divider->flags);
474}
475
476int divider_get_val(unsigned long rate, unsigned long parent_rate,
477		    const struct clk_div_table *table, u8 width,
478		    unsigned long flags)
479{
480	unsigned int div, value;
481
482	div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
483
484	if (!_is_valid_div(table, div, flags))
485		return -EINVAL;
486
487	value = _get_val(table, div, flags, width);
488
489	return min_t(unsigned int, value, clk_div_mask(width));
490}
491EXPORT_SYMBOL_GPL(divider_get_val);
492
493static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
494				unsigned long parent_rate)
495{
496	struct clk_divider *divider = to_clk_divider(hw);
497	int value;
498	unsigned long flags = 0;
499	u32 val;
500
501	value = divider_get_val(rate, parent_rate, divider->table,
502				divider->width, divider->flags);
503	if (value < 0)
504		return value;
505
506	if (divider->lock)
507		spin_lock_irqsave(divider->lock, flags);
508	else
509		__acquire(divider->lock);
510
511	if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
512		val = clk_div_mask(divider->width) << (divider->shift + 16);
513	} else {
514		val = clk_div_readl(divider);
515		val &= ~(clk_div_mask(divider->width) << divider->shift);
516	}
517	val |= (u32)value << divider->shift;
518	clk_div_writel(divider, val);
519
520	if (divider->lock)
521		spin_unlock_irqrestore(divider->lock, flags);
522	else
523		__release(divider->lock);
524
525	return 0;
526}
527
528const struct clk_ops clk_divider_ops = {
529	.recalc_rate = clk_divider_recalc_rate,
530	.round_rate = clk_divider_round_rate,
531	.determine_rate = clk_divider_determine_rate,
532	.set_rate = clk_divider_set_rate,
533};
534EXPORT_SYMBOL_GPL(clk_divider_ops);
535
536const struct clk_ops clk_divider_ro_ops = {
537	.recalc_rate = clk_divider_recalc_rate,
538	.round_rate = clk_divider_round_rate,
539	.determine_rate = clk_divider_determine_rate,
540};
541EXPORT_SYMBOL_GPL(clk_divider_ro_ops);
542
543struct clk_hw *__clk_hw_register_divider(struct device *dev,
544		struct device_node *np, const char *name,
545		const char *parent_name, const struct clk_hw *parent_hw,
546		const struct clk_parent_data *parent_data, unsigned long flags,
547		void __iomem *reg, u8 shift, u8 width,
548		unsigned long clk_divider_flags,
549		const struct clk_div_table *table, spinlock_t *lock)
550{
551	struct clk_divider *div;
552	struct clk_hw *hw;
553	struct clk_init_data init = {};
554	int ret;
555
556	if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
557		if (width + shift > 16) {
558			pr_warn("divider value exceeds LOWORD field\n");
559			return ERR_PTR(-EINVAL);
560		}
561	}
562
563	/* allocate the divider */
564	div = kzalloc(sizeof(*div), GFP_KERNEL);
565	if (!div)
566		return ERR_PTR(-ENOMEM);
567
568	init.name = name;
569	if (clk_divider_flags & CLK_DIVIDER_READ_ONLY)
570		init.ops = &clk_divider_ro_ops;
571	else
572		init.ops = &clk_divider_ops;
573	init.flags = flags;
574	init.parent_names = parent_name ? &parent_name : NULL;
575	init.parent_hws = parent_hw ? &parent_hw : NULL;
576	init.parent_data = parent_data;
577	if (parent_name || parent_hw || parent_data)
578		init.num_parents = 1;
579	else
580		init.num_parents = 0;
581
582	/* struct clk_divider assignments */
583	div->reg = reg;
584	div->shift = shift;
585	div->width = width;
586	div->flags = clk_divider_flags;
587	div->lock = lock;
588	div->hw.init = &init;
589	div->table = table;
590
591	/* register the clock */
592	hw = &div->hw;
593	ret = clk_hw_register(dev, hw);
594	if (ret) {
595		kfree(div);
596		hw = ERR_PTR(ret);
597	}
598
599	return hw;
600}
601EXPORT_SYMBOL_GPL(__clk_hw_register_divider);
602
603/**
604 * clk_register_divider_table - register a table based divider clock with
605 * the clock framework
606 * @dev: device registering this clock
607 * @name: name of this clock
608 * @parent_name: name of clock's parent
609 * @flags: framework-specific flags
610 * @reg: register address to adjust divider
611 * @shift: number of bits to shift the bitfield
612 * @width: width of the bitfield
613 * @clk_divider_flags: divider-specific flags for this clock
614 * @table: array of divider/value pairs ending with a div set to 0
615 * @lock: shared register lock for this clock
616 */
617struct clk *clk_register_divider_table(struct device *dev, const char *name,
618		const char *parent_name, unsigned long flags,
619		void __iomem *reg, u8 shift, u8 width,
620		unsigned long clk_divider_flags,
621		const struct clk_div_table *table, spinlock_t *lock)
622{
623	struct clk_hw *hw;
624
625	hw =  __clk_hw_register_divider(dev, NULL, name, parent_name, NULL,
626			NULL, flags, reg, shift, width, clk_divider_flags,
627			table, lock);
628	if (IS_ERR(hw))
629		return ERR_CAST(hw);
630	return hw->clk;
631}
632EXPORT_SYMBOL_GPL(clk_register_divider_table);
633
634void clk_unregister_divider(struct clk *clk)
635{
636	struct clk_divider *div;
637	struct clk_hw *hw;
638
639	hw = __clk_get_hw(clk);
640	if (!hw)
641		return;
642
643	div = to_clk_divider(hw);
644
645	clk_unregister(clk);
646	kfree(div);
647}
648EXPORT_SYMBOL_GPL(clk_unregister_divider);
649
650/**
651 * clk_hw_unregister_divider - unregister a clk divider
652 * @hw: hardware-specific clock data to unregister
653 */
654void clk_hw_unregister_divider(struct clk_hw *hw)
655{
656	struct clk_divider *div;
657
658	div = to_clk_divider(hw);
659
660	clk_hw_unregister(hw);
661	kfree(div);
662}
663EXPORT_SYMBOL_GPL(clk_hw_unregister_divider);
664
665static void devm_clk_hw_release_divider(struct device *dev, void *res)
666{
667	clk_hw_unregister_divider(*(struct clk_hw **)res);
668}
669
670struct clk_hw *__devm_clk_hw_register_divider(struct device *dev,
671		struct device_node *np, const char *name,
672		const char *parent_name, const struct clk_hw *parent_hw,
673		const struct clk_parent_data *parent_data, unsigned long flags,
674		void __iomem *reg, u8 shift, u8 width,
675		unsigned long clk_divider_flags,
676		const struct clk_div_table *table, spinlock_t *lock)
677{
678	struct clk_hw **ptr, *hw;
679
680	ptr = devres_alloc(devm_clk_hw_release_divider, sizeof(*ptr), GFP_KERNEL);
681	if (!ptr)
682		return ERR_PTR(-ENOMEM);
683
684	hw = __clk_hw_register_divider(dev, np, name, parent_name, parent_hw,
685				       parent_data, flags, reg, shift, width,
686				       clk_divider_flags, table, lock);
687
688	if (!IS_ERR(hw)) {
689		*ptr = hw;
690		devres_add(dev, ptr);
691	} else {
692		devres_free(ptr);
693	}
694
695	return hw;
696}
697EXPORT_SYMBOL_GPL(__devm_clk_hw_register_divider);
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  4 * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
  5 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
  6 *
  7 * Adjustable divider clock implementation
  8 */
  9
 10#include <linux/clk-provider.h>
 
 11#include <linux/module.h>
 12#include <linux/slab.h>
 13#include <linux/io.h>
 14#include <linux/err.h>
 15#include <linux/string.h>
 16#include <linux/log2.h>
 17
 18/*
 19 * DOC: basic adjustable divider clock that cannot gate
 20 *
 21 * Traits of this clock:
 22 * prepare - clk_prepare only ensures that parents are prepared
 23 * enable - clk_enable only ensures that parents are enabled
 24 * rate - rate is adjustable.  clk->rate = ceiling(parent->rate / divisor)
 25 * parent - fixed parent.  No clk_set_parent support
 26 */
 27
 28static inline u32 clk_div_readl(struct clk_divider *divider)
 29{
 30	if (divider->flags & CLK_DIVIDER_BIG_ENDIAN)
 31		return ioread32be(divider->reg);
 32
 33	return readl(divider->reg);
 34}
 35
 36static inline void clk_div_writel(struct clk_divider *divider, u32 val)
 37{
 38	if (divider->flags & CLK_DIVIDER_BIG_ENDIAN)
 39		iowrite32be(val, divider->reg);
 40	else
 41		writel(val, divider->reg);
 42}
 43
 44static unsigned int _get_table_maxdiv(const struct clk_div_table *table,
 45				      u8 width)
 46{
 47	unsigned int maxdiv = 0, mask = clk_div_mask(width);
 48	const struct clk_div_table *clkt;
 49
 50	for (clkt = table; clkt->div; clkt++)
 51		if (clkt->div > maxdiv && clkt->val <= mask)
 52			maxdiv = clkt->div;
 53	return maxdiv;
 54}
 55
 56static unsigned int _get_table_mindiv(const struct clk_div_table *table)
 57{
 58	unsigned int mindiv = UINT_MAX;
 59	const struct clk_div_table *clkt;
 60
 61	for (clkt = table; clkt->div; clkt++)
 62		if (clkt->div < mindiv)
 63			mindiv = clkt->div;
 64	return mindiv;
 65}
 66
 67static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width,
 68				unsigned long flags)
 69{
 70	if (flags & CLK_DIVIDER_ONE_BASED)
 71		return clk_div_mask(width);
 72	if (flags & CLK_DIVIDER_POWER_OF_TWO)
 73		return 1 << clk_div_mask(width);
 
 
 74	if (table)
 75		return _get_table_maxdiv(table, width);
 76	return clk_div_mask(width) + 1;
 77}
 78
 79static unsigned int _get_table_div(const struct clk_div_table *table,
 80							unsigned int val)
 81{
 82	const struct clk_div_table *clkt;
 83
 84	for (clkt = table; clkt->div; clkt++)
 85		if (clkt->val == val)
 86			return clkt->div;
 87	return 0;
 88}
 89
 90static unsigned int _get_div(const struct clk_div_table *table,
 91			     unsigned int val, unsigned long flags, u8 width)
 92{
 93	if (flags & CLK_DIVIDER_ONE_BASED)
 94		return val;
 95	if (flags & CLK_DIVIDER_POWER_OF_TWO)
 96		return 1 << val;
 97	if (flags & CLK_DIVIDER_MAX_AT_ZERO)
 98		return val ? val : clk_div_mask(width) + 1;
 
 
 99	if (table)
100		return _get_table_div(table, val);
101	return val + 1;
102}
103
104static unsigned int _get_table_val(const struct clk_div_table *table,
105							unsigned int div)
106{
107	const struct clk_div_table *clkt;
108
109	for (clkt = table; clkt->div; clkt++)
110		if (clkt->div == div)
111			return clkt->val;
112	return 0;
113}
114
115static unsigned int _get_val(const struct clk_div_table *table,
116			     unsigned int div, unsigned long flags, u8 width)
117{
118	if (flags & CLK_DIVIDER_ONE_BASED)
119		return div;
120	if (flags & CLK_DIVIDER_POWER_OF_TWO)
121		return __ffs(div);
122	if (flags & CLK_DIVIDER_MAX_AT_ZERO)
123		return (div == clk_div_mask(width) + 1) ? 0 : div;
 
 
124	if (table)
125		return  _get_table_val(table, div);
126	return div - 1;
127}
128
129unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate,
130				  unsigned int val,
131				  const struct clk_div_table *table,
132				  unsigned long flags, unsigned long width)
133{
134	unsigned int div;
135
136	div = _get_div(table, val, flags, width);
137	if (!div) {
138		WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO),
139			"%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
140			clk_hw_get_name(hw));
141		return parent_rate;
142	}
143
144	return DIV_ROUND_UP_ULL((u64)parent_rate, div);
145}
146EXPORT_SYMBOL_GPL(divider_recalc_rate);
147
148static unsigned long clk_divider_recalc_rate(struct clk_hw *hw,
149		unsigned long parent_rate)
150{
151	struct clk_divider *divider = to_clk_divider(hw);
152	unsigned int val;
153
154	val = clk_div_readl(divider) >> divider->shift;
155	val &= clk_div_mask(divider->width);
156
157	return divider_recalc_rate(hw, parent_rate, val, divider->table,
158				   divider->flags, divider->width);
159}
160
161static bool _is_valid_table_div(const struct clk_div_table *table,
162							 unsigned int div)
163{
164	const struct clk_div_table *clkt;
165
166	for (clkt = table; clkt->div; clkt++)
167		if (clkt->div == div)
168			return true;
169	return false;
170}
171
172static bool _is_valid_div(const struct clk_div_table *table, unsigned int div,
173			  unsigned long flags)
174{
175	if (flags & CLK_DIVIDER_POWER_OF_TWO)
176		return is_power_of_2(div);
177	if (table)
178		return _is_valid_table_div(table, div);
179	return true;
180}
181
182static int _round_up_table(const struct clk_div_table *table, int div)
183{
184	const struct clk_div_table *clkt;
185	int up = INT_MAX;
186
187	for (clkt = table; clkt->div; clkt++) {
188		if (clkt->div == div)
189			return clkt->div;
190		else if (clkt->div < div)
191			continue;
192
193		if ((clkt->div - div) < (up - div))
194			up = clkt->div;
195	}
196
197	return up;
198}
199
200static int _round_down_table(const struct clk_div_table *table, int div)
201{
202	const struct clk_div_table *clkt;
203	int down = _get_table_mindiv(table);
204
205	for (clkt = table; clkt->div; clkt++) {
206		if (clkt->div == div)
207			return clkt->div;
208		else if (clkt->div > div)
209			continue;
210
211		if ((div - clkt->div) < (div - down))
212			down = clkt->div;
213	}
214
215	return down;
216}
217
218static int _div_round_up(const struct clk_div_table *table,
219			 unsigned long parent_rate, unsigned long rate,
220			 unsigned long flags)
221{
222	int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
223
224	if (flags & CLK_DIVIDER_POWER_OF_TWO)
225		div = __roundup_pow_of_two(div);
226	if (table)
227		div = _round_up_table(table, div);
228
229	return div;
230}
231
232static int _div_round_closest(const struct clk_div_table *table,
233			      unsigned long parent_rate, unsigned long rate,
234			      unsigned long flags)
235{
236	int up, down;
237	unsigned long up_rate, down_rate;
238
239	up = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
240	down = parent_rate / rate;
241
242	if (flags & CLK_DIVIDER_POWER_OF_TWO) {
243		up = __roundup_pow_of_two(up);
244		down = __rounddown_pow_of_two(down);
245	} else if (table) {
246		up = _round_up_table(table, up);
247		down = _round_down_table(table, down);
248	}
249
250	up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up);
251	down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down);
252
253	return (rate - up_rate) <= (down_rate - rate) ? up : down;
254}
255
256static int _div_round(const struct clk_div_table *table,
257		      unsigned long parent_rate, unsigned long rate,
258		      unsigned long flags)
259{
260	if (flags & CLK_DIVIDER_ROUND_CLOSEST)
261		return _div_round_closest(table, parent_rate, rate, flags);
262
263	return _div_round_up(table, parent_rate, rate, flags);
264}
265
266static bool _is_best_div(unsigned long rate, unsigned long now,
267			 unsigned long best, unsigned long flags)
268{
269	if (flags & CLK_DIVIDER_ROUND_CLOSEST)
270		return abs(rate - now) < abs(rate - best);
271
272	return now <= rate && now > best;
273}
274
275static int _next_div(const struct clk_div_table *table, int div,
276		     unsigned long flags)
277{
278	div++;
279
280	if (flags & CLK_DIVIDER_POWER_OF_TWO)
281		return __roundup_pow_of_two(div);
282	if (table)
283		return _round_up_table(table, div);
284
285	return div;
286}
287
288static int clk_divider_bestdiv(struct clk_hw *hw, struct clk_hw *parent,
289			       unsigned long rate,
290			       unsigned long *best_parent_rate,
291			       const struct clk_div_table *table, u8 width,
292			       unsigned long flags)
293{
294	int i, bestdiv = 0;
295	unsigned long parent_rate, best = 0, now, maxdiv;
296	unsigned long parent_rate_saved = *best_parent_rate;
297
298	if (!rate)
299		rate = 1;
300
301	maxdiv = _get_maxdiv(table, width, flags);
302
303	if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
304		parent_rate = *best_parent_rate;
305		bestdiv = _div_round(table, parent_rate, rate, flags);
306		bestdiv = bestdiv == 0 ? 1 : bestdiv;
307		bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
308		return bestdiv;
309	}
310
311	/*
312	 * The maximum divider we can use without overflowing
313	 * unsigned long in rate * i below
314	 */
315	maxdiv = min(ULONG_MAX / rate, maxdiv);
316
317	for (i = _next_div(table, 0, flags); i <= maxdiv;
318					     i = _next_div(table, i, flags)) {
319		if (rate * i == parent_rate_saved) {
320			/*
321			 * It's the most ideal case if the requested rate can be
322			 * divided from parent clock without needing to change
323			 * parent rate, so return the divider immediately.
324			 */
325			*best_parent_rate = parent_rate_saved;
326			return i;
327		}
328		parent_rate = clk_hw_round_rate(parent, rate * i);
329		now = DIV_ROUND_UP_ULL((u64)parent_rate, i);
330		if (_is_best_div(rate, now, best, flags)) {
331			bestdiv = i;
332			best = now;
333			*best_parent_rate = parent_rate;
334		}
335	}
336
337	if (!bestdiv) {
338		bestdiv = _get_maxdiv(table, width, flags);
339		*best_parent_rate = clk_hw_round_rate(parent, 1);
340	}
341
342	return bestdiv;
343}
344
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345long divider_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
346			       unsigned long rate, unsigned long *prate,
347			       const struct clk_div_table *table,
348			       u8 width, unsigned long flags)
349{
350	int div;
 
 
 
 
 
 
 
 
 
351
352	div = clk_divider_bestdiv(hw, parent, rate, prate, table, width, flags);
353
354	return DIV_ROUND_UP_ULL((u64)*prate, div);
355}
356EXPORT_SYMBOL_GPL(divider_round_rate_parent);
357
358long divider_ro_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
359				  unsigned long rate, unsigned long *prate,
360				  const struct clk_div_table *table, u8 width,
361				  unsigned long flags, unsigned int val)
362{
363	int div;
 
364
365	div = _get_div(table, val, flags, width);
 
 
366
367	/* Even a read-only clock can propagate a rate change */
368	if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
369		if (!parent)
370			return -EINVAL;
371
372		*prate = clk_hw_round_rate(parent, rate * div);
373	}
374
375	return DIV_ROUND_UP_ULL((u64)*prate, div);
376}
377EXPORT_SYMBOL_GPL(divider_ro_round_rate_parent);
378
379
380static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
381				unsigned long *prate)
382{
383	struct clk_divider *divider = to_clk_divider(hw);
384
385	/* if read only, just return current value */
386	if (divider->flags & CLK_DIVIDER_READ_ONLY) {
387		u32 val;
388
389		val = clk_div_readl(divider) >> divider->shift;
390		val &= clk_div_mask(divider->width);
391
392		return divider_ro_round_rate(hw, rate, prate, divider->table,
393					     divider->width, divider->flags,
394					     val);
395	}
396
397	return divider_round_rate(hw, rate, prate, divider->table,
398				  divider->width, divider->flags);
399}
400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401int divider_get_val(unsigned long rate, unsigned long parent_rate,
402		    const struct clk_div_table *table, u8 width,
403		    unsigned long flags)
404{
405	unsigned int div, value;
406
407	div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
408
409	if (!_is_valid_div(table, div, flags))
410		return -EINVAL;
411
412	value = _get_val(table, div, flags, width);
413
414	return min_t(unsigned int, value, clk_div_mask(width));
415}
416EXPORT_SYMBOL_GPL(divider_get_val);
417
418static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
419				unsigned long parent_rate)
420{
421	struct clk_divider *divider = to_clk_divider(hw);
422	int value;
423	unsigned long flags = 0;
424	u32 val;
425
426	value = divider_get_val(rate, parent_rate, divider->table,
427				divider->width, divider->flags);
428	if (value < 0)
429		return value;
430
431	if (divider->lock)
432		spin_lock_irqsave(divider->lock, flags);
433	else
434		__acquire(divider->lock);
435
436	if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
437		val = clk_div_mask(divider->width) << (divider->shift + 16);
438	} else {
439		val = clk_div_readl(divider);
440		val &= ~(clk_div_mask(divider->width) << divider->shift);
441	}
442	val |= (u32)value << divider->shift;
443	clk_div_writel(divider, val);
444
445	if (divider->lock)
446		spin_unlock_irqrestore(divider->lock, flags);
447	else
448		__release(divider->lock);
449
450	return 0;
451}
452
453const struct clk_ops clk_divider_ops = {
454	.recalc_rate = clk_divider_recalc_rate,
455	.round_rate = clk_divider_round_rate,
 
456	.set_rate = clk_divider_set_rate,
457};
458EXPORT_SYMBOL_GPL(clk_divider_ops);
459
460const struct clk_ops clk_divider_ro_ops = {
461	.recalc_rate = clk_divider_recalc_rate,
462	.round_rate = clk_divider_round_rate,
 
463};
464EXPORT_SYMBOL_GPL(clk_divider_ro_ops);
465
466struct clk_hw *__clk_hw_register_divider(struct device *dev,
467		struct device_node *np, const char *name,
468		const char *parent_name, const struct clk_hw *parent_hw,
469		const struct clk_parent_data *parent_data, unsigned long flags,
470		void __iomem *reg, u8 shift, u8 width, u8 clk_divider_flags,
 
471		const struct clk_div_table *table, spinlock_t *lock)
472{
473	struct clk_divider *div;
474	struct clk_hw *hw;
475	struct clk_init_data init = {};
476	int ret;
477
478	if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
479		if (width + shift > 16) {
480			pr_warn("divider value exceeds LOWORD field\n");
481			return ERR_PTR(-EINVAL);
482		}
483	}
484
485	/* allocate the divider */
486	div = kzalloc(sizeof(*div), GFP_KERNEL);
487	if (!div)
488		return ERR_PTR(-ENOMEM);
489
490	init.name = name;
491	if (clk_divider_flags & CLK_DIVIDER_READ_ONLY)
492		init.ops = &clk_divider_ro_ops;
493	else
494		init.ops = &clk_divider_ops;
495	init.flags = flags;
496	init.parent_names = (parent_name ? &parent_name: NULL);
497	init.num_parents = (parent_name ? 1 : 0);
 
 
 
 
 
498
499	/* struct clk_divider assignments */
500	div->reg = reg;
501	div->shift = shift;
502	div->width = width;
503	div->flags = clk_divider_flags;
504	div->lock = lock;
505	div->hw.init = &init;
506	div->table = table;
507
508	/* register the clock */
509	hw = &div->hw;
510	ret = clk_hw_register(dev, hw);
511	if (ret) {
512		kfree(div);
513		hw = ERR_PTR(ret);
514	}
515
516	return hw;
517}
518EXPORT_SYMBOL_GPL(__clk_hw_register_divider);
519
520/**
521 * clk_register_divider_table - register a table based divider clock with
522 * the clock framework
523 * @dev: device registering this clock
524 * @name: name of this clock
525 * @parent_name: name of clock's parent
526 * @flags: framework-specific flags
527 * @reg: register address to adjust divider
528 * @shift: number of bits to shift the bitfield
529 * @width: width of the bitfield
530 * @clk_divider_flags: divider-specific flags for this clock
531 * @table: array of divider/value pairs ending with a div set to 0
532 * @lock: shared register lock for this clock
533 */
534struct clk *clk_register_divider_table(struct device *dev, const char *name,
535		const char *parent_name, unsigned long flags,
536		void __iomem *reg, u8 shift, u8 width,
537		u8 clk_divider_flags, const struct clk_div_table *table,
538		spinlock_t *lock)
539{
540	struct clk_hw *hw;
541
542	hw =  __clk_hw_register_divider(dev, NULL, name, parent_name, NULL,
543			NULL, flags, reg, shift, width, clk_divider_flags,
544			table, lock);
545	if (IS_ERR(hw))
546		return ERR_CAST(hw);
547	return hw->clk;
548}
549EXPORT_SYMBOL_GPL(clk_register_divider_table);
550
551void clk_unregister_divider(struct clk *clk)
552{
553	struct clk_divider *div;
554	struct clk_hw *hw;
555
556	hw = __clk_get_hw(clk);
557	if (!hw)
558		return;
559
560	div = to_clk_divider(hw);
561
562	clk_unregister(clk);
563	kfree(div);
564}
565EXPORT_SYMBOL_GPL(clk_unregister_divider);
566
567/**
568 * clk_hw_unregister_divider - unregister a clk divider
569 * @hw: hardware-specific clock data to unregister
570 */
571void clk_hw_unregister_divider(struct clk_hw *hw)
572{
573	struct clk_divider *div;
574
575	div = to_clk_divider(hw);
576
577	clk_hw_unregister(hw);
578	kfree(div);
579}
580EXPORT_SYMBOL_GPL(clk_hw_unregister_divider);