xref: /openbmc/linux/drivers/clk/ti/divider.c (revision b97d6790d03b763eca08847a9a5869a4291b9f9a)
15a729246SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b4761198STero Kristo /*
3b4761198STero Kristo  * TI Divider Clock
4b4761198STero Kristo  *
5b4761198STero Kristo  * Copyright (C) 2013 Texas Instruments, Inc.
6b4761198STero Kristo  *
7b4761198STero Kristo  * Tero Kristo <t-kristo@ti.com>
8b4761198STero Kristo  */
9b4761198STero Kristo 
10b4761198STero Kristo #include <linux/clk-provider.h>
11b4761198STero Kristo #include <linux/slab.h>
12b4761198STero Kristo #include <linux/err.h>
13b4761198STero Kristo #include <linux/of.h>
14b4761198STero Kristo #include <linux/of_address.h>
15b4761198STero Kristo #include <linux/clk/ti.h>
16d96f774bSTero Kristo #include "clock.h"
17b4761198STero Kristo 
18b4761198STero Kristo #undef pr_fmt
19b4761198STero Kristo #define pr_fmt(fmt) "%s: " fmt, __func__
20b4761198STero Kristo 
_get_table_div(const struct clk_div_table * table,unsigned int val)21b4761198STero Kristo static unsigned int _get_table_div(const struct clk_div_table *table,
22b4761198STero Kristo 				   unsigned int val)
23b4761198STero Kristo {
24b4761198STero Kristo 	const struct clk_div_table *clkt;
25b4761198STero Kristo 
26b4761198STero Kristo 	for (clkt = table; clkt->div; clkt++)
27b4761198STero Kristo 		if (clkt->val == val)
28b4761198STero Kristo 			return clkt->div;
29b4761198STero Kristo 	return 0;
30b4761198STero Kristo }
31b4761198STero Kristo 
_setup_mask(struct clk_omap_divider * divider)328ffea6eeSTero Kristo static void _setup_mask(struct clk_omap_divider *divider)
338ffea6eeSTero Kristo {
348ffea6eeSTero Kristo 	u16 mask;
358ffea6eeSTero Kristo 	u32 max_val;
368ffea6eeSTero Kristo 	const struct clk_div_table *clkt;
378ffea6eeSTero Kristo 
388ffea6eeSTero Kristo 	if (divider->table) {
398ffea6eeSTero Kristo 		max_val = 0;
408ffea6eeSTero Kristo 
418ffea6eeSTero Kristo 		for (clkt = divider->table; clkt->div; clkt++)
428ffea6eeSTero Kristo 			if (clkt->val > max_val)
438ffea6eeSTero Kristo 				max_val = clkt->val;
448ffea6eeSTero Kristo 	} else {
458ffea6eeSTero Kristo 		max_val = divider->max;
468ffea6eeSTero Kristo 
478ffea6eeSTero Kristo 		if (!(divider->flags & CLK_DIVIDER_ONE_BASED) &&
488ffea6eeSTero Kristo 		    !(divider->flags & CLK_DIVIDER_POWER_OF_TWO))
498ffea6eeSTero Kristo 			max_val--;
508ffea6eeSTero Kristo 	}
518ffea6eeSTero Kristo 
528ffea6eeSTero Kristo 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
538ffea6eeSTero Kristo 		mask = fls(max_val) - 1;
548ffea6eeSTero Kristo 	else
558ffea6eeSTero Kristo 		mask = max_val;
568ffea6eeSTero Kristo 
578ffea6eeSTero Kristo 	divider->mask = (1 << fls(mask)) - 1;
588ffea6eeSTero Kristo }
598ffea6eeSTero Kristo 
_get_div(struct clk_omap_divider * divider,unsigned int val)606dbde947STero Kristo static unsigned int _get_div(struct clk_omap_divider *divider, unsigned int val)
61b4761198STero Kristo {
62b4761198STero Kristo 	if (divider->flags & CLK_DIVIDER_ONE_BASED)
63b4761198STero Kristo 		return val;
64b4761198STero Kristo 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
65b4761198STero Kristo 		return 1 << val;
66b4761198STero Kristo 	if (divider->table)
67b4761198STero Kristo 		return _get_table_div(divider->table, val);
68b4761198STero Kristo 	return val + 1;
69b4761198STero Kristo }
70b4761198STero Kristo 
_get_table_val(const struct clk_div_table * table,unsigned int div)71b4761198STero Kristo static unsigned int _get_table_val(const struct clk_div_table *table,
72b4761198STero Kristo 				   unsigned int div)
73b4761198STero Kristo {
74b4761198STero Kristo 	const struct clk_div_table *clkt;
75b4761198STero Kristo 
76b4761198STero Kristo 	for (clkt = table; clkt->div; clkt++)
77b4761198STero Kristo 		if (clkt->div == div)
78b4761198STero Kristo 			return clkt->val;
79b4761198STero Kristo 	return 0;
80b4761198STero Kristo }
81b4761198STero Kristo 
_get_val(struct clk_omap_divider * divider,u8 div)826dbde947STero Kristo static unsigned int _get_val(struct clk_omap_divider *divider, u8 div)
83b4761198STero Kristo {
84b4761198STero Kristo 	if (divider->flags & CLK_DIVIDER_ONE_BASED)
85b4761198STero Kristo 		return div;
86b4761198STero Kristo 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
87b4761198STero Kristo 		return __ffs(div);
88b4761198STero Kristo 	if (divider->table)
89b4761198STero Kristo 		return  _get_table_val(divider->table, div);
90b4761198STero Kristo 	return div - 1;
91b4761198STero Kristo }
92b4761198STero Kristo 
ti_clk_divider_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)93b4761198STero Kristo static unsigned long ti_clk_divider_recalc_rate(struct clk_hw *hw,
94b4761198STero Kristo 						unsigned long parent_rate)
95b4761198STero Kristo {
966dbde947STero Kristo 	struct clk_omap_divider *divider = to_clk_omap_divider(hw);
97b4761198STero Kristo 	unsigned int div, val;
98b4761198STero Kristo 
996c0afb50STero Kristo 	val = ti_clk_ll_ops->clk_readl(&divider->reg) >> divider->shift;
1008ffea6eeSTero Kristo 	val &= divider->mask;
101b4761198STero Kristo 
102b4761198STero Kristo 	div = _get_div(divider, val);
103b4761198STero Kristo 	if (!div) {
104b4761198STero Kristo 		WARN(!(divider->flags & CLK_DIVIDER_ALLOW_ZERO),
105b4761198STero Kristo 		     "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
106a53ad8efSStephen Boyd 		     clk_hw_get_name(hw));
107b4761198STero Kristo 		return parent_rate;
108b4761198STero Kristo 	}
109b4761198STero Kristo 
1107e50e7e1STomi Valkeinen 	return DIV_ROUND_UP(parent_rate, div);
111b4761198STero Kristo }
112b4761198STero Kristo 
113b4761198STero Kristo /*
114b4761198STero Kristo  * The reverse of DIV_ROUND_UP: The maximum number which
115b4761198STero Kristo  * divided by m is r
116b4761198STero Kristo  */
117b4761198STero Kristo #define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1)
118b4761198STero Kristo 
_is_valid_table_div(const struct clk_div_table * table,unsigned int div)119b4761198STero Kristo static bool _is_valid_table_div(const struct clk_div_table *table,
120b4761198STero Kristo 				unsigned int div)
121b4761198STero Kristo {
122b4761198STero Kristo 	const struct clk_div_table *clkt;
123b4761198STero Kristo 
124b4761198STero Kristo 	for (clkt = table; clkt->div; clkt++)
125b4761198STero Kristo 		if (clkt->div == div)
126b4761198STero Kristo 			return true;
127b4761198STero Kristo 	return false;
128b4761198STero Kristo }
129b4761198STero Kristo 
_is_valid_div(struct clk_omap_divider * divider,unsigned int div)1306dbde947STero Kristo static bool _is_valid_div(struct clk_omap_divider *divider, unsigned int div)
131b4761198STero Kristo {
132b4761198STero Kristo 	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
133b4761198STero Kristo 		return is_power_of_2(div);
134b4761198STero Kristo 	if (divider->table)
135b4761198STero Kristo 		return _is_valid_table_div(divider->table, div);
136b4761198STero Kristo 	return true;
137b4761198STero Kristo }
138b4761198STero Kristo 
_div_round_up(const struct clk_div_table * table,unsigned long parent_rate,unsigned long rate)139ced30683SKeerthy static int _div_round_up(const struct clk_div_table *table,
140ced30683SKeerthy 			 unsigned long parent_rate, unsigned long rate)
141ced30683SKeerthy {
142ced30683SKeerthy 	const struct clk_div_table *clkt;
143ced30683SKeerthy 	int up = INT_MAX;
144ced30683SKeerthy 	int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
145ced30683SKeerthy 
146ced30683SKeerthy 	for (clkt = table; clkt->div; clkt++) {
147ced30683SKeerthy 		if (clkt->div == div)
148ced30683SKeerthy 			return clkt->div;
149ced30683SKeerthy 		else if (clkt->div < div)
150ced30683SKeerthy 			continue;
151ced30683SKeerthy 
152ced30683SKeerthy 		if ((clkt->div - div) < (up - div))
153ced30683SKeerthy 			up = clkt->div;
154ced30683SKeerthy 	}
155ced30683SKeerthy 
156ced30683SKeerthy 	return up;
157ced30683SKeerthy }
158ced30683SKeerthy 
_div_round(const struct clk_div_table * table,unsigned long parent_rate,unsigned long rate)159ced30683SKeerthy static int _div_round(const struct clk_div_table *table,
160ced30683SKeerthy 		      unsigned long parent_rate, unsigned long rate)
161ced30683SKeerthy {
162ced30683SKeerthy 	if (!table)
163ced30683SKeerthy 		return DIV_ROUND_UP(parent_rate, rate);
164ced30683SKeerthy 
165ced30683SKeerthy 	return _div_round_up(table, parent_rate, rate);
166ced30683SKeerthy }
167ced30683SKeerthy 
ti_clk_divider_bestdiv(struct clk_hw * hw,unsigned long rate,unsigned long * best_parent_rate)168b4761198STero Kristo static int ti_clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
169b4761198STero Kristo 				  unsigned long *best_parent_rate)
170b4761198STero Kristo {
1716dbde947STero Kristo 	struct clk_omap_divider *divider = to_clk_omap_divider(hw);
172b4761198STero Kristo 	int i, bestdiv = 0;
173b4761198STero Kristo 	unsigned long parent_rate, best = 0, now, maxdiv;
174b4761198STero Kristo 	unsigned long parent_rate_saved = *best_parent_rate;
175b4761198STero Kristo 
176b4761198STero Kristo 	if (!rate)
177b4761198STero Kristo 		rate = 1;
178b4761198STero Kristo 
1798ffea6eeSTero Kristo 	maxdiv = divider->max;
180b4761198STero Kristo 
18198d8a60eSStephen Boyd 	if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
182b4761198STero Kristo 		parent_rate = *best_parent_rate;
183ced30683SKeerthy 		bestdiv = _div_round(divider->table, parent_rate, rate);
184b4761198STero Kristo 		bestdiv = bestdiv == 0 ? 1 : bestdiv;
185b4761198STero Kristo 		bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
186b4761198STero Kristo 		return bestdiv;
187b4761198STero Kristo 	}
188b4761198STero Kristo 
189b4761198STero Kristo 	/*
190b4761198STero Kristo 	 * The maximum divider we can use without overflowing
191b4761198STero Kristo 	 * unsigned long in rate * i below
192b4761198STero Kristo 	 */
193b4761198STero Kristo 	maxdiv = min(ULONG_MAX / rate, maxdiv);
194b4761198STero Kristo 
195b4761198STero Kristo 	for (i = 1; i <= maxdiv; i++) {
196b4761198STero Kristo 		if (!_is_valid_div(divider, i))
197b4761198STero Kristo 			continue;
198b4761198STero Kristo 		if (rate * i == parent_rate_saved) {
199b4761198STero Kristo 			/*
200b4761198STero Kristo 			 * It's the most ideal case if the requested rate can be
201b4761198STero Kristo 			 * divided from parent clock without needing to change
202b4761198STero Kristo 			 * parent rate, so return the divider immediately.
203b4761198STero Kristo 			 */
204b4761198STero Kristo 			*best_parent_rate = parent_rate_saved;
205b4761198STero Kristo 			return i;
206b4761198STero Kristo 		}
207a53ad8efSStephen Boyd 		parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw),
208b4761198STero Kristo 				MULT_ROUND_UP(rate, i));
2097e50e7e1STomi Valkeinen 		now = DIV_ROUND_UP(parent_rate, i);
210b4761198STero Kristo 		if (now <= rate && now > best) {
211b4761198STero Kristo 			bestdiv = i;
212b4761198STero Kristo 			best = now;
213b4761198STero Kristo 			*best_parent_rate = parent_rate;
214b4761198STero Kristo 		}
215b4761198STero Kristo 	}
216b4761198STero Kristo 
217b4761198STero Kristo 	if (!bestdiv) {
2188ffea6eeSTero Kristo 		bestdiv = divider->max;
219b4761198STero Kristo 		*best_parent_rate =
220a53ad8efSStephen Boyd 			clk_hw_round_rate(clk_hw_get_parent(hw), 1);
221b4761198STero Kristo 	}
222b4761198STero Kristo 
223b4761198STero Kristo 	return bestdiv;
224b4761198STero Kristo }
225b4761198STero Kristo 
ti_clk_divider_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * prate)226b4761198STero Kristo static long ti_clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
227b4761198STero Kristo 				      unsigned long *prate)
228b4761198STero Kristo {
229b4761198STero Kristo 	int div;
230b4761198STero Kristo 	div = ti_clk_divider_bestdiv(hw, rate, prate);
231b4761198STero Kristo 
2327e50e7e1STomi Valkeinen 	return DIV_ROUND_UP(*prate, div);
233b4761198STero Kristo }
234b4761198STero Kristo 
ti_clk_divider_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)235b4761198STero Kristo static int ti_clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
236b4761198STero Kristo 				   unsigned long parent_rate)
237b4761198STero Kristo {
2386dbde947STero Kristo 	struct clk_omap_divider *divider;
239b4761198STero Kristo 	unsigned int div, value;
240b4761198STero Kristo 	u32 val;
241b4761198STero Kristo 
2422f103251SNishanth Menon 	if (!hw || !rate)
2432f103251SNishanth Menon 		return -EINVAL;
2442f103251SNishanth Menon 
2456dbde947STero Kristo 	divider = to_clk_omap_divider(hw);
2462f103251SNishanth Menon 
2477e50e7e1STomi Valkeinen 	div = DIV_ROUND_UP(parent_rate, rate);
2488ffea6eeSTero Kristo 
2498ffea6eeSTero Kristo 	if (div > divider->max)
2508ffea6eeSTero Kristo 		div = divider->max;
2518ffea6eeSTero Kristo 	if (div < divider->min)
2528ffea6eeSTero Kristo 		div = divider->min;
2538ffea6eeSTero Kristo 
254b4761198STero Kristo 	value = _get_val(divider, div);
255b4761198STero Kristo 
2566c0afb50STero Kristo 	val = ti_clk_ll_ops->clk_readl(&divider->reg);
2578ffea6eeSTero Kristo 	val &= ~(divider->mask << divider->shift);
258b4761198STero Kristo 	val |= value << divider->shift;
2596c0afb50STero Kristo 	ti_clk_ll_ops->clk_writel(val, &divider->reg);
260b4761198STero Kristo 
261b44a0300STero Kristo 	ti_clk_latch(&divider->reg, divider->latch);
262b44a0300STero Kristo 
263b4761198STero Kristo 	return 0;
264b4761198STero Kristo }
265b4761198STero Kristo 
266d6e7bbc1SRuss Dill /**
267d6e7bbc1SRuss Dill  * clk_divider_save_context - Save the divider value
268d6e7bbc1SRuss Dill  * @hw: pointer  struct clk_hw
269d6e7bbc1SRuss Dill  *
270d6e7bbc1SRuss Dill  * Save the divider value
271d6e7bbc1SRuss Dill  */
clk_divider_save_context(struct clk_hw * hw)272d6e7bbc1SRuss Dill static int clk_divider_save_context(struct clk_hw *hw)
273d6e7bbc1SRuss Dill {
274d6e7bbc1SRuss Dill 	struct clk_omap_divider *divider = to_clk_omap_divider(hw);
275d6e7bbc1SRuss Dill 	u32 val;
276d6e7bbc1SRuss Dill 
277d6e7bbc1SRuss Dill 	val = ti_clk_ll_ops->clk_readl(&divider->reg) >> divider->shift;
2788ffea6eeSTero Kristo 	divider->context = val & divider->mask;
279d6e7bbc1SRuss Dill 
280d6e7bbc1SRuss Dill 	return 0;
281d6e7bbc1SRuss Dill }
282d6e7bbc1SRuss Dill 
283d6e7bbc1SRuss Dill /**
284d6e7bbc1SRuss Dill  * clk_divider_restore_context - restore the saved the divider value
285d6e7bbc1SRuss Dill  * @hw: pointer  struct clk_hw
286d6e7bbc1SRuss Dill  *
287d6e7bbc1SRuss Dill  * Restore the saved the divider value
288d6e7bbc1SRuss Dill  */
clk_divider_restore_context(struct clk_hw * hw)289d6e7bbc1SRuss Dill static void clk_divider_restore_context(struct clk_hw *hw)
290d6e7bbc1SRuss Dill {
291d6e7bbc1SRuss Dill 	struct clk_omap_divider *divider = to_clk_omap_divider(hw);
292d6e7bbc1SRuss Dill 	u32 val;
293d6e7bbc1SRuss Dill 
294d6e7bbc1SRuss Dill 	val = ti_clk_ll_ops->clk_readl(&divider->reg);
2958ffea6eeSTero Kristo 	val &= ~(divider->mask << divider->shift);
296d6e7bbc1SRuss Dill 	val |= divider->context << divider->shift;
297d6e7bbc1SRuss Dill 	ti_clk_ll_ops->clk_writel(val, &divider->reg);
298d6e7bbc1SRuss Dill }
299d6e7bbc1SRuss Dill 
300b4761198STero Kristo const struct clk_ops ti_clk_divider_ops = {
301b4761198STero Kristo 	.recalc_rate = ti_clk_divider_recalc_rate,
302b4761198STero Kristo 	.round_rate = ti_clk_divider_round_rate,
303b4761198STero Kristo 	.set_rate = ti_clk_divider_set_rate,
304d6e7bbc1SRuss Dill 	.save_context = clk_divider_save_context,
305d6e7bbc1SRuss Dill 	.restore_context = clk_divider_restore_context,
306b4761198STero Kristo };
307b4761198STero Kristo 
_register_divider(struct device_node * node,u32 flags,struct clk_omap_divider * div)308fbbc1859STero Kristo static struct clk *_register_divider(struct device_node *node,
309fbbc1859STero Kristo 				     u32 flags,
310fbbc1859STero Kristo 				     struct clk_omap_divider *div)
311b4761198STero Kristo {
312b4761198STero Kristo 	struct clk_init_data init;
313fbbc1859STero Kristo 	const char *parent_name;
314ed06099cSTony Lindgren 	const char *name;
315b4761198STero Kristo 
316fbbc1859STero Kristo 	parent_name = of_clk_get_parent_name(node, 0);
317b4761198STero Kristo 
318ed06099cSTony Lindgren 	name = ti_dt_clk_name(node);
319ed06099cSTony Lindgren 	init.name = name;
320b4761198STero Kristo 	init.ops = &ti_clk_divider_ops;
3218aa09cf3STero Kristo 	init.flags = flags;
322b4761198STero Kristo 	init.parent_names = (parent_name ? &parent_name : NULL);
323b4761198STero Kristo 	init.num_parents = (parent_name ? 1 : 0);
324b4761198STero Kristo 
325b4761198STero Kristo 	div->hw.init = &init;
326b4761198STero Kristo 
327b4761198STero Kristo 	/* register the clock */
328*46e12284SDan Carpenter 	return of_ti_clk_register(node, &div->hw, name);
329b4761198STero Kristo }
330b4761198STero Kristo 
ti_clk_parse_divider_data(int * div_table,int num_dividers,int max_div,u8 flags,struct clk_omap_divider * divider)3314f6be565STero Kristo int ti_clk_parse_divider_data(int *div_table, int num_dividers, int max_div,
332a229965cSTero Kristo 			      u8 flags, struct clk_omap_divider *divider)
333d96f774bSTero Kristo {
334d96f774bSTero Kristo 	int valid_div = 0;
3354f6be565STero Kristo 	int i;
3364f6be565STero Kristo 	struct clk_div_table *tmp;
3378ffea6eeSTero Kristo 	u16 min_div = 0;
338d96f774bSTero Kristo 
3394f6be565STero Kristo 	if (!div_table) {
3408ffea6eeSTero Kristo 		divider->min = 1;
3418ffea6eeSTero Kristo 		divider->max = max_div;
3428ffea6eeSTero Kristo 		_setup_mask(divider);
3434f6be565STero Kristo 		return 0;
344d96f774bSTero Kristo 	}
345d96f774bSTero Kristo 
3464f6be565STero Kristo 	i = 0;
347d96f774bSTero Kristo 
3484f6be565STero Kristo 	while (!num_dividers || i < num_dividers) {
3494f6be565STero Kristo 		if (div_table[i] == -1)
3504f6be565STero Kristo 			break;
3514f6be565STero Kristo 		if (div_table[i])
3524f6be565STero Kristo 			valid_div++;
3534f6be565STero Kristo 		i++;
3544f6be565STero Kristo 	}
3554f6be565STero Kristo 
3564f6be565STero Kristo 	num_dividers = i;
3574f6be565STero Kristo 
3586396bb22SKees Cook 	tmp = kcalloc(valid_div + 1, sizeof(*tmp), GFP_KERNEL);
359a229965cSTero Kristo 	if (!tmp)
3604f6be565STero Kristo 		return -ENOMEM;
361d96f774bSTero Kristo 
362d96f774bSTero Kristo 	valid_div = 0;
363d96f774bSTero Kristo 
3644f6be565STero Kristo 	for (i = 0; i < num_dividers; i++)
3654f6be565STero Kristo 		if (div_table[i] > 0) {
3664f6be565STero Kristo 			tmp[valid_div].div = div_table[i];
3674f6be565STero Kristo 			tmp[valid_div].val = i;
368d96f774bSTero Kristo 			valid_div++;
3698ffea6eeSTero Kristo 			if (div_table[i] > max_div)
3708ffea6eeSTero Kristo 				max_div = div_table[i];
3718ffea6eeSTero Kristo 			if (!min_div || div_table[i] < min_div)
3728ffea6eeSTero Kristo 				min_div = div_table[i];
373d96f774bSTero Kristo 		}
374d96f774bSTero Kristo 
3758ffea6eeSTero Kristo 	divider->min = min_div;
3768ffea6eeSTero Kristo 	divider->max = max_div;
377a229965cSTero Kristo 	divider->table = tmp;
3788ffea6eeSTero Kristo 	_setup_mask(divider);
3794f6be565STero Kristo 
3804f6be565STero Kristo 	return 0;
3814f6be565STero Kristo }
3824f6be565STero Kristo 
ti_clk_get_div_table(struct device_node * node,struct clk_omap_divider * div)383fbbc1859STero Kristo static int __init ti_clk_get_div_table(struct device_node *node,
384fbbc1859STero Kristo 				       struct clk_omap_divider *div)
385b4761198STero Kristo {
386b4761198STero Kristo 	struct clk_div_table *table;
387b4761198STero Kristo 	const __be32 *divspec;
388b4761198STero Kristo 	u32 val;
389b4761198STero Kristo 	u32 num_div;
390b4761198STero Kristo 	u32 valid_div;
391b4761198STero Kristo 	int i;
392b4761198STero Kristo 
393b4761198STero Kristo 	divspec = of_get_property(node, "ti,dividers", &num_div);
394b4761198STero Kristo 
395b4761198STero Kristo 	if (!divspec)
396fbbc1859STero Kristo 		return 0;
397b4761198STero Kristo 
398b4761198STero Kristo 	num_div /= 4;
399b4761198STero Kristo 
400b4761198STero Kristo 	valid_div = 0;
401b4761198STero Kristo 
402b4761198STero Kristo 	/* Determine required size for divider table */
403b4761198STero Kristo 	for (i = 0; i < num_div; i++) {
404b4761198STero Kristo 		of_property_read_u32_index(node, "ti,dividers", i, &val);
405b4761198STero Kristo 		if (val)
406b4761198STero Kristo 			valid_div++;
407b4761198STero Kristo 	}
408b4761198STero Kristo 
409b4761198STero Kristo 	if (!valid_div) {
410e665f029SRob Herring 		pr_err("no valid dividers for %pOFn table\n", node);
411fbbc1859STero Kristo 		return -EINVAL;
412b4761198STero Kristo 	}
413b4761198STero Kristo 
4146396bb22SKees Cook 	table = kcalloc(valid_div + 1, sizeof(*table), GFP_KERNEL);
415b4761198STero Kristo 	if (!table)
416fbbc1859STero Kristo 		return -ENOMEM;
417b4761198STero Kristo 
418b4761198STero Kristo 	valid_div = 0;
419b4761198STero Kristo 
420b4761198STero Kristo 	for (i = 0; i < num_div; i++) {
421b4761198STero Kristo 		of_property_read_u32_index(node, "ti,dividers", i, &val);
422b4761198STero Kristo 		if (val) {
423b4761198STero Kristo 			table[valid_div].div = val;
424b4761198STero Kristo 			table[valid_div].val = i;
425b4761198STero Kristo 			valid_div++;
426b4761198STero Kristo 		}
427b4761198STero Kristo 	}
428b4761198STero Kristo 
429fbbc1859STero Kristo 	div->table = table;
430fbbc1859STero Kristo 
431fbbc1859STero Kristo 	return 0;
432b4761198STero Kristo }
433b4761198STero Kristo 
_populate_divider_min_max(struct device_node * node,struct clk_omap_divider * divider)4348ffea6eeSTero Kristo static int _populate_divider_min_max(struct device_node *node,
4358ffea6eeSTero Kristo 				     struct clk_omap_divider *divider)
436b4761198STero Kristo {
4378ffea6eeSTero Kristo 	u32 min_div = 0;
4388ffea6eeSTero Kristo 	u32 max_div = 0;
4398ffea6eeSTero Kristo 	u32 val;
4408ffea6eeSTero Kristo 	const struct clk_div_table *clkt;
441b4761198STero Kristo 
4428ffea6eeSTero Kristo 	if (!divider->table) {
443b4761198STero Kristo 		/* Clk divider table not provided, determine min/max divs */
444b4761198STero Kristo 		if (of_property_read_u32(node, "ti,min-div", &min_div))
445b4761198STero Kristo 			min_div = 1;
446b4761198STero Kristo 
447b4761198STero Kristo 		if (of_property_read_u32(node, "ti,max-div", &max_div)) {
448e665f029SRob Herring 			pr_err("no max-div for %pOFn!\n", node);
449b4761198STero Kristo 			return -EINVAL;
450b4761198STero Kristo 		}
451b4761198STero Kristo 	} else {
452b4761198STero Kristo 
4538ffea6eeSTero Kristo 		for (clkt = divider->table; clkt->div; clkt++) {
4548ffea6eeSTero Kristo 			val = clkt->div;
4558ffea6eeSTero Kristo 			if (val > max_div)
4568ffea6eeSTero Kristo 				max_div = val;
4578ffea6eeSTero Kristo 			if (!min_div || val < min_div)
4588ffea6eeSTero Kristo 				min_div = val;
459b4761198STero Kristo 		}
460b4761198STero Kristo 	}
461b4761198STero Kristo 
4628ffea6eeSTero Kristo 	divider->min = min_div;
4638ffea6eeSTero Kristo 	divider->max = max_div;
4648ffea6eeSTero Kristo 	_setup_mask(divider);
4658ffea6eeSTero Kristo 
4668ffea6eeSTero Kristo 	return 0;
467b4761198STero Kristo }
468b4761198STero Kristo 
ti_clk_divider_populate(struct device_node * node,struct clk_omap_divider * div,u32 * flags)469b4761198STero Kristo static int __init ti_clk_divider_populate(struct device_node *node,
470fbbc1859STero Kristo 					  struct clk_omap_divider *div,
471fbbc1859STero Kristo 					  u32 *flags)
472b4761198STero Kristo {
473b4761198STero Kristo 	u32 val;
4746c0afb50STero Kristo 	int ret;
475b4761198STero Kristo 
476fbbc1859STero Kristo 	ret = ti_clk_get_reg_addr(node, 0, &div->reg);
4776c0afb50STero Kristo 	if (ret)
4786c0afb50STero Kristo 		return ret;
479b4761198STero Kristo 
480b4761198STero Kristo 	if (!of_property_read_u32(node, "ti,bit-shift", &val))
481fbbc1859STero Kristo 		div->shift = val;
482b4761198STero Kristo 	else
483fbbc1859STero Kristo 		div->shift = 0;
484b4761198STero Kristo 
485b44a0300STero Kristo 	if (!of_property_read_u32(node, "ti,latch-bit", &val))
486fbbc1859STero Kristo 		div->latch = val;
487b44a0300STero Kristo 	else
488fbbc1859STero Kristo 		div->latch = -EINVAL;
489b44a0300STero Kristo 
490b4761198STero Kristo 	*flags = 0;
491fbbc1859STero Kristo 	div->flags = 0;
492b4761198STero Kristo 
493b4761198STero Kristo 	if (of_property_read_bool(node, "ti,index-starts-at-one"))
494fbbc1859STero Kristo 		div->flags |= CLK_DIVIDER_ONE_BASED;
495b4761198STero Kristo 
496b4761198STero Kristo 	if (of_property_read_bool(node, "ti,index-power-of-two"))
497fbbc1859STero Kristo 		div->flags |= CLK_DIVIDER_POWER_OF_TWO;
498b4761198STero Kristo 
499b4761198STero Kristo 	if (of_property_read_bool(node, "ti,set-rate-parent"))
500b4761198STero Kristo 		*flags |= CLK_SET_RATE_PARENT;
501b4761198STero Kristo 
502fbbc1859STero Kristo 	ret = ti_clk_get_div_table(node, div);
503fbbc1859STero Kristo 	if (ret)
504fbbc1859STero Kristo 		return ret;
505b4761198STero Kristo 
5068ffea6eeSTero Kristo 	return _populate_divider_min_max(node, div);
507b4761198STero Kristo }
508b4761198STero Kristo 
509b4761198STero Kristo /**
510b4761198STero Kristo  * of_ti_divider_clk_setup - Setup function for simple div rate clock
511b4761198STero Kristo  * @node: device node for this clock
512b4761198STero Kristo  *
513b4761198STero Kristo  * Sets up a basic divider clock.
514b4761198STero Kristo  */
of_ti_divider_clk_setup(struct device_node * node)515b4761198STero Kristo static void __init of_ti_divider_clk_setup(struct device_node *node)
516b4761198STero Kristo {
517b4761198STero Kristo 	struct clk *clk;
518b4761198STero Kristo 	u32 flags = 0;
519fbbc1859STero Kristo 	struct clk_omap_divider *div;
520b4761198STero Kristo 
521fbbc1859STero Kristo 	div = kzalloc(sizeof(*div), GFP_KERNEL);
522fbbc1859STero Kristo 	if (!div)
523fbbc1859STero Kristo 		return;
524b4761198STero Kristo 
525fbbc1859STero Kristo 	if (ti_clk_divider_populate(node, div, &flags))
526b4761198STero Kristo 		goto cleanup;
527b4761198STero Kristo 
528fbbc1859STero Kristo 	clk = _register_divider(node, flags, div);
529b4761198STero Kristo 	if (!IS_ERR(clk)) {
530b4761198STero Kristo 		of_clk_add_provider(node, of_clk_src_simple_get, clk);
531b4761198STero Kristo 		of_ti_clk_autoidle_setup(node);
532b4761198STero Kristo 		return;
533b4761198STero Kristo 	}
534b4761198STero Kristo 
535b4761198STero Kristo cleanup:
536fbbc1859STero Kristo 	kfree(div->table);
537fbbc1859STero Kristo 	kfree(div);
538b4761198STero Kristo }
539b4761198STero Kristo CLK_OF_DECLARE(divider_clk, "ti,divider-clock", of_ti_divider_clk_setup);
540b4761198STero Kristo 
of_ti_composite_divider_clk_setup(struct device_node * node)541b4761198STero Kristo static void __init of_ti_composite_divider_clk_setup(struct device_node *node)
542b4761198STero Kristo {
5436dbde947STero Kristo 	struct clk_omap_divider *div;
544fbbc1859STero Kristo 	u32 tmp;
545b4761198STero Kristo 
546b4761198STero Kristo 	div = kzalloc(sizeof(*div), GFP_KERNEL);
547b4761198STero Kristo 	if (!div)
548b4761198STero Kristo 		return;
549b4761198STero Kristo 
550fbbc1859STero Kristo 	if (ti_clk_divider_populate(node, div, &tmp))
551b4761198STero Kristo 		goto cleanup;
552b4761198STero Kristo 
553b4761198STero Kristo 	if (!ti_clk_add_component(node, &div->hw, CLK_COMPONENT_TYPE_DIVIDER))
554b4761198STero Kristo 		return;
555b4761198STero Kristo 
556b4761198STero Kristo cleanup:
557b4761198STero Kristo 	kfree(div->table);
558b4761198STero Kristo 	kfree(div);
559b4761198STero Kristo }
560b4761198STero Kristo CLK_OF_DECLARE(ti_composite_divider_clk, "ti,composite-divider-clock",
561b4761198STero Kristo 	       of_ti_composite_divider_clk_setup);
562