xref: /openbmc/linux/drivers/clk/clk-divider.c (revision b11d282dbea27db1788893115dfca8a7856bf205)
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
27*b11d282dSTomi 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 
466d9252bdSRajendra Nayak static unsigned int _get_maxdiv(struct clk_divider *divider)
476d9252bdSRajendra Nayak {
486d9252bdSRajendra Nayak 	if (divider->flags & CLK_DIVIDER_ONE_BASED)
496d9252bdSRajendra Nayak 		return div_mask(divider);
506d9252bdSRajendra Nayak 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
516d9252bdSRajendra Nayak 		return 1 << div_mask(divider);
52357c3f0aSRajendra Nayak 	if (divider->table)
53357c3f0aSRajendra Nayak 		return _get_table_maxdiv(divider->table);
546d9252bdSRajendra Nayak 	return div_mask(divider) + 1;
556d9252bdSRajendra Nayak }
566d9252bdSRajendra Nayak 
57357c3f0aSRajendra Nayak static unsigned int _get_table_div(const struct clk_div_table *table,
58357c3f0aSRajendra Nayak 							unsigned int val)
59357c3f0aSRajendra Nayak {
60357c3f0aSRajendra Nayak 	const struct clk_div_table *clkt;
61357c3f0aSRajendra Nayak 
62357c3f0aSRajendra Nayak 	for (clkt = table; clkt->div; clkt++)
63357c3f0aSRajendra Nayak 		if (clkt->val == val)
64357c3f0aSRajendra Nayak 			return clkt->div;
65357c3f0aSRajendra Nayak 	return 0;
66357c3f0aSRajendra Nayak }
67357c3f0aSRajendra Nayak 
686d9252bdSRajendra Nayak static unsigned int _get_div(struct clk_divider *divider, unsigned int val)
696d9252bdSRajendra Nayak {
706d9252bdSRajendra Nayak 	if (divider->flags & CLK_DIVIDER_ONE_BASED)
716d9252bdSRajendra Nayak 		return val;
726d9252bdSRajendra Nayak 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
736d9252bdSRajendra Nayak 		return 1 << val;
74357c3f0aSRajendra Nayak 	if (divider->table)
75357c3f0aSRajendra Nayak 		return _get_table_div(divider->table, val);
766d9252bdSRajendra Nayak 	return val + 1;
776d9252bdSRajendra Nayak }
786d9252bdSRajendra Nayak 
79357c3f0aSRajendra Nayak static unsigned int _get_table_val(const struct clk_div_table *table,
80357c3f0aSRajendra Nayak 							unsigned int div)
81357c3f0aSRajendra Nayak {
82357c3f0aSRajendra Nayak 	const struct clk_div_table *clkt;
83357c3f0aSRajendra Nayak 
84357c3f0aSRajendra Nayak 	for (clkt = table; clkt->div; clkt++)
85357c3f0aSRajendra Nayak 		if (clkt->div == div)
86357c3f0aSRajendra Nayak 			return clkt->val;
87357c3f0aSRajendra Nayak 	return 0;
88357c3f0aSRajendra Nayak }
89357c3f0aSRajendra Nayak 
90778037e1SJames Hogan static unsigned int _get_val(struct clk_divider *divider, unsigned int div)
916d9252bdSRajendra Nayak {
926d9252bdSRajendra Nayak 	if (divider->flags & CLK_DIVIDER_ONE_BASED)
936d9252bdSRajendra Nayak 		return div;
946d9252bdSRajendra Nayak 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
956d9252bdSRajendra Nayak 		return __ffs(div);
96357c3f0aSRajendra Nayak 	if (divider->table)
97357c3f0aSRajendra Nayak 		return  _get_table_val(divider->table, div);
986d9252bdSRajendra Nayak 	return div - 1;
996d9252bdSRajendra Nayak }
1009d9f78edSMike Turquette 
1019d9f78edSMike Turquette static unsigned long clk_divider_recalc_rate(struct clk_hw *hw,
1029d9f78edSMike Turquette 		unsigned long parent_rate)
1039d9f78edSMike Turquette {
1049d9f78edSMike Turquette 	struct clk_divider *divider = to_clk_divider(hw);
1056d9252bdSRajendra Nayak 	unsigned int div, val;
1069d9f78edSMike Turquette 
107aa514ce3SGerhard Sittig 	val = clk_readl(divider->reg) >> divider->shift;
1086d9252bdSRajendra Nayak 	val &= div_mask(divider);
1099d9f78edSMike Turquette 
1106d9252bdSRajendra Nayak 	div = _get_div(divider, val);
1116d9252bdSRajendra Nayak 	if (!div) {
112056b2053SSoren Brinkmann 		WARN(!(divider->flags & CLK_DIVIDER_ALLOW_ZERO),
113056b2053SSoren Brinkmann 			"%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
1146d9252bdSRajendra Nayak 			__clk_get_name(hw->clk));
1156d9252bdSRajendra Nayak 		return parent_rate;
1166d9252bdSRajendra Nayak 	}
1179d9f78edSMike Turquette 
118*b11d282dSTomi Valkeinen 	return DIV_ROUND_UP(parent_rate, div);
1199d9f78edSMike Turquette }
1209d9f78edSMike Turquette 
1219d9f78edSMike Turquette /*
1229d9f78edSMike Turquette  * The reverse of DIV_ROUND_UP: The maximum number which
1239d9f78edSMike Turquette  * divided by m is r
1249d9f78edSMike Turquette  */
1259d9f78edSMike Turquette #define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1)
1269d9f78edSMike Turquette 
127357c3f0aSRajendra Nayak static bool _is_valid_table_div(const struct clk_div_table *table,
128357c3f0aSRajendra Nayak 							 unsigned int div)
129357c3f0aSRajendra Nayak {
130357c3f0aSRajendra Nayak 	const struct clk_div_table *clkt;
131357c3f0aSRajendra Nayak 
132357c3f0aSRajendra Nayak 	for (clkt = table; clkt->div; clkt++)
133357c3f0aSRajendra Nayak 		if (clkt->div == div)
134357c3f0aSRajendra Nayak 			return true;
135357c3f0aSRajendra Nayak 	return false;
136357c3f0aSRajendra Nayak }
137357c3f0aSRajendra Nayak 
138357c3f0aSRajendra Nayak static bool _is_valid_div(struct clk_divider *divider, unsigned int div)
139357c3f0aSRajendra Nayak {
140357c3f0aSRajendra Nayak 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
1411a3cd184SJames Hogan 		return is_power_of_2(div);
142357c3f0aSRajendra Nayak 	if (divider->table)
143357c3f0aSRajendra Nayak 		return _is_valid_table_div(divider->table, div);
144357c3f0aSRajendra Nayak 	return true;
145357c3f0aSRajendra Nayak }
146357c3f0aSRajendra Nayak 
1479d9f78edSMike Turquette static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
1489d9f78edSMike Turquette 		unsigned long *best_parent_rate)
1499d9f78edSMike Turquette {
1509d9f78edSMike Turquette 	struct clk_divider *divider = to_clk_divider(hw);
1519d9f78edSMike Turquette 	int i, bestdiv = 0;
1529d9f78edSMike Turquette 	unsigned long parent_rate, best = 0, now, maxdiv;
153081c9025SShawn Guo 	unsigned long parent_rate_saved = *best_parent_rate;
1549d9f78edSMike Turquette 
1559d9f78edSMike Turquette 	if (!rate)
1569d9f78edSMike Turquette 		rate = 1;
1579d9f78edSMike Turquette 
1586d9252bdSRajendra Nayak 	maxdiv = _get_maxdiv(divider);
1599d9f78edSMike Turquette 
16081536e07SShawn Guo 	if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) {
16181536e07SShawn Guo 		parent_rate = *best_parent_rate;
1629d9f78edSMike Turquette 		bestdiv = DIV_ROUND_UP(parent_rate, rate);
1639d9f78edSMike Turquette 		bestdiv = bestdiv == 0 ? 1 : bestdiv;
1649d9f78edSMike Turquette 		bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
1659d9f78edSMike Turquette 		return bestdiv;
1669d9f78edSMike Turquette 	}
1679d9f78edSMike Turquette 
1689d9f78edSMike Turquette 	/*
1699d9f78edSMike Turquette 	 * The maximum divider we can use without overflowing
1709d9f78edSMike Turquette 	 * unsigned long in rate * i below
1719d9f78edSMike Turquette 	 */
1729d9f78edSMike Turquette 	maxdiv = min(ULONG_MAX / rate, maxdiv);
1739d9f78edSMike Turquette 
1749d9f78edSMike Turquette 	for (i = 1; i <= maxdiv; i++) {
175357c3f0aSRajendra Nayak 		if (!_is_valid_div(divider, i))
1766d9252bdSRajendra Nayak 			continue;
177081c9025SShawn Guo 		if (rate * i == parent_rate_saved) {
178081c9025SShawn Guo 			/*
179081c9025SShawn Guo 			 * It's the most ideal case if the requested rate can be
180081c9025SShawn Guo 			 * divided from parent clock without needing to change
181081c9025SShawn Guo 			 * parent rate, so return the divider immediately.
182081c9025SShawn Guo 			 */
183081c9025SShawn Guo 			*best_parent_rate = parent_rate_saved;
184081c9025SShawn Guo 			return i;
185081c9025SShawn Guo 		}
1869d9f78edSMike Turquette 		parent_rate = __clk_round_rate(__clk_get_parent(hw->clk),
1879d9f78edSMike Turquette 				MULT_ROUND_UP(rate, i));
188*b11d282dSTomi Valkeinen 		now = DIV_ROUND_UP(parent_rate, i);
1899d9f78edSMike Turquette 		if (now <= rate && now > best) {
1909d9f78edSMike Turquette 			bestdiv = i;
1919d9f78edSMike Turquette 			best = now;
1929d9f78edSMike Turquette 			*best_parent_rate = parent_rate;
1939d9f78edSMike Turquette 		}
1949d9f78edSMike Turquette 	}
1959d9f78edSMike Turquette 
1969d9f78edSMike Turquette 	if (!bestdiv) {
1976d9252bdSRajendra Nayak 		bestdiv = _get_maxdiv(divider);
1989d9f78edSMike Turquette 		*best_parent_rate = __clk_round_rate(__clk_get_parent(hw->clk), 1);
1999d9f78edSMike Turquette 	}
2009d9f78edSMike Turquette 
2019d9f78edSMike Turquette 	return bestdiv;
2029d9f78edSMike Turquette }
2039d9f78edSMike Turquette 
2049d9f78edSMike Turquette static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
2059d9f78edSMike Turquette 				unsigned long *prate)
2069d9f78edSMike Turquette {
2079d9f78edSMike Turquette 	int div;
2089d9f78edSMike Turquette 	div = clk_divider_bestdiv(hw, rate, prate);
2099d9f78edSMike Turquette 
210*b11d282dSTomi Valkeinen 	return DIV_ROUND_UP(*prate, div);
2119d9f78edSMike Turquette }
2129d9f78edSMike Turquette 
2131c0035d7SShawn Guo static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
2141c0035d7SShawn Guo 				unsigned long parent_rate)
2159d9f78edSMike Turquette {
2169d9f78edSMike Turquette 	struct clk_divider *divider = to_clk_divider(hw);
2176d9252bdSRajendra Nayak 	unsigned int div, value;
2189d9f78edSMike Turquette 	unsigned long flags = 0;
2199d9f78edSMike Turquette 	u32 val;
2209d9f78edSMike Turquette 
221*b11d282dSTomi Valkeinen 	div = DIV_ROUND_UP(parent_rate, rate);
2226d9252bdSRajendra Nayak 	value = _get_val(divider, div);
2239d9f78edSMike Turquette 
2246d9252bdSRajendra Nayak 	if (value > div_mask(divider))
2256d9252bdSRajendra Nayak 		value = div_mask(divider);
2269d9f78edSMike Turquette 
2279d9f78edSMike Turquette 	if (divider->lock)
2289d9f78edSMike Turquette 		spin_lock_irqsave(divider->lock, flags);
2299d9f78edSMike Turquette 
230d57dfe75SHaojian Zhuang 	if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
231d57dfe75SHaojian Zhuang 		val = div_mask(divider) << (divider->shift + 16);
232d57dfe75SHaojian Zhuang 	} else {
233aa514ce3SGerhard Sittig 		val = clk_readl(divider->reg);
2349d9f78edSMike Turquette 		val &= ~(div_mask(divider) << divider->shift);
235d57dfe75SHaojian Zhuang 	}
2366d9252bdSRajendra Nayak 	val |= value << divider->shift;
237aa514ce3SGerhard Sittig 	clk_writel(val, divider->reg);
2389d9f78edSMike Turquette 
2399d9f78edSMike Turquette 	if (divider->lock)
2409d9f78edSMike Turquette 		spin_unlock_irqrestore(divider->lock, flags);
2419d9f78edSMike Turquette 
2429d9f78edSMike Turquette 	return 0;
2439d9f78edSMike Turquette }
2449d9f78edSMike Turquette 
245822c250eSShawn Guo const struct clk_ops clk_divider_ops = {
2469d9f78edSMike Turquette 	.recalc_rate = clk_divider_recalc_rate,
2479d9f78edSMike Turquette 	.round_rate = clk_divider_round_rate,
2489d9f78edSMike Turquette 	.set_rate = clk_divider_set_rate,
2499d9f78edSMike Turquette };
2509d9f78edSMike Turquette EXPORT_SYMBOL_GPL(clk_divider_ops);
2519d9f78edSMike Turquette 
252357c3f0aSRajendra Nayak static struct clk *_register_divider(struct device *dev, const char *name,
2539d9f78edSMike Turquette 		const char *parent_name, unsigned long flags,
2549d9f78edSMike Turquette 		void __iomem *reg, u8 shift, u8 width,
255357c3f0aSRajendra Nayak 		u8 clk_divider_flags, const struct clk_div_table *table,
256357c3f0aSRajendra Nayak 		spinlock_t *lock)
2579d9f78edSMike Turquette {
2589d9f78edSMike Turquette 	struct clk_divider *div;
2599d9f78edSMike Turquette 	struct clk *clk;
2600197b3eaSSaravana Kannan 	struct clk_init_data init;
2619d9f78edSMike Turquette 
262d57dfe75SHaojian Zhuang 	if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
263d57dfe75SHaojian Zhuang 		if (width + shift > 16) {
264d57dfe75SHaojian Zhuang 			pr_warn("divider value exceeds LOWORD field\n");
265d57dfe75SHaojian Zhuang 			return ERR_PTR(-EINVAL);
266d57dfe75SHaojian Zhuang 		}
267d57dfe75SHaojian Zhuang 	}
268d57dfe75SHaojian Zhuang 
26927d54591SMike Turquette 	/* allocate the divider */
2709d9f78edSMike Turquette 	div = kzalloc(sizeof(struct clk_divider), GFP_KERNEL);
2719d9f78edSMike Turquette 	if (!div) {
2729d9f78edSMike Turquette 		pr_err("%s: could not allocate divider clk\n", __func__);
27327d54591SMike Turquette 		return ERR_PTR(-ENOMEM);
2749d9f78edSMike Turquette 	}
2759d9f78edSMike Turquette 
2760197b3eaSSaravana Kannan 	init.name = name;
2770197b3eaSSaravana Kannan 	init.ops = &clk_divider_ops;
278f7d8caadSRajendra Nayak 	init.flags = flags | CLK_IS_BASIC;
2790197b3eaSSaravana Kannan 	init.parent_names = (parent_name ? &parent_name: NULL);
2800197b3eaSSaravana Kannan 	init.num_parents = (parent_name ? 1 : 0);
2810197b3eaSSaravana Kannan 
2829d9f78edSMike Turquette 	/* struct clk_divider assignments */
2839d9f78edSMike Turquette 	div->reg = reg;
2849d9f78edSMike Turquette 	div->shift = shift;
2859d9f78edSMike Turquette 	div->width = width;
2869d9f78edSMike Turquette 	div->flags = clk_divider_flags;
2879d9f78edSMike Turquette 	div->lock = lock;
2880197b3eaSSaravana Kannan 	div->hw.init = &init;
289357c3f0aSRajendra Nayak 	div->table = table;
2909d9f78edSMike Turquette 
29127d54591SMike Turquette 	/* register the clock */
2920197b3eaSSaravana Kannan 	clk = clk_register(dev, &div->hw);
2939d9f78edSMike Turquette 
29427d54591SMike Turquette 	if (IS_ERR(clk))
2959d9f78edSMike Turquette 		kfree(div);
2969d9f78edSMike Turquette 
29727d54591SMike Turquette 	return clk;
2989d9f78edSMike Turquette }
299357c3f0aSRajendra Nayak 
300357c3f0aSRajendra Nayak /**
301357c3f0aSRajendra Nayak  * clk_register_divider - register a divider clock with the clock framework
302357c3f0aSRajendra Nayak  * @dev: device registering this clock
303357c3f0aSRajendra Nayak  * @name: name of this clock
304357c3f0aSRajendra Nayak  * @parent_name: name of clock's parent
305357c3f0aSRajendra Nayak  * @flags: framework-specific flags
306357c3f0aSRajendra Nayak  * @reg: register address to adjust divider
307357c3f0aSRajendra Nayak  * @shift: number of bits to shift the bitfield
308357c3f0aSRajendra Nayak  * @width: width of the bitfield
309357c3f0aSRajendra Nayak  * @clk_divider_flags: divider-specific flags for this clock
310357c3f0aSRajendra Nayak  * @lock: shared register lock for this clock
311357c3f0aSRajendra Nayak  */
312357c3f0aSRajendra Nayak struct clk *clk_register_divider(struct device *dev, const char *name,
313357c3f0aSRajendra Nayak 		const char *parent_name, unsigned long flags,
314357c3f0aSRajendra Nayak 		void __iomem *reg, u8 shift, u8 width,
315357c3f0aSRajendra Nayak 		u8 clk_divider_flags, spinlock_t *lock)
316357c3f0aSRajendra Nayak {
317357c3f0aSRajendra Nayak 	return _register_divider(dev, name, parent_name, flags, reg, shift,
318357c3f0aSRajendra Nayak 			width, clk_divider_flags, NULL, lock);
319357c3f0aSRajendra Nayak }
3204c5eeea9SFabio Estevam EXPORT_SYMBOL_GPL(clk_register_divider);
321357c3f0aSRajendra Nayak 
322357c3f0aSRajendra Nayak /**
323357c3f0aSRajendra Nayak  * clk_register_divider_table - register a table based divider clock with
324357c3f0aSRajendra Nayak  * the clock framework
325357c3f0aSRajendra Nayak  * @dev: device registering this clock
326357c3f0aSRajendra Nayak  * @name: name of this clock
327357c3f0aSRajendra Nayak  * @parent_name: name of clock's parent
328357c3f0aSRajendra Nayak  * @flags: framework-specific flags
329357c3f0aSRajendra Nayak  * @reg: register address to adjust divider
330357c3f0aSRajendra Nayak  * @shift: number of bits to shift the bitfield
331357c3f0aSRajendra Nayak  * @width: width of the bitfield
332357c3f0aSRajendra Nayak  * @clk_divider_flags: divider-specific flags for this clock
333357c3f0aSRajendra Nayak  * @table: array of divider/value pairs ending with a div set to 0
334357c3f0aSRajendra Nayak  * @lock: shared register lock for this clock
335357c3f0aSRajendra Nayak  */
336357c3f0aSRajendra Nayak struct clk *clk_register_divider_table(struct device *dev, const char *name,
337357c3f0aSRajendra Nayak 		const char *parent_name, unsigned long flags,
338357c3f0aSRajendra Nayak 		void __iomem *reg, u8 shift, u8 width,
339357c3f0aSRajendra Nayak 		u8 clk_divider_flags, const struct clk_div_table *table,
340357c3f0aSRajendra Nayak 		spinlock_t *lock)
341357c3f0aSRajendra Nayak {
342357c3f0aSRajendra Nayak 	return _register_divider(dev, name, parent_name, flags, reg, shift,
343357c3f0aSRajendra Nayak 			width, clk_divider_flags, table, lock);
344357c3f0aSRajendra Nayak }
3454c5eeea9SFabio Estevam EXPORT_SYMBOL_GPL(clk_register_divider_table);
346