xref: /openbmc/linux/drivers/clk/sunxi/clk-factors.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e874a669SEmilio López /*
3e874a669SEmilio López  * Copyright (C) 2013 Emilio López <emilio@elopez.com.ar>
4e874a669SEmilio López  *
5e874a669SEmilio López  * Adjustable factor-based clock implementation
6e874a669SEmilio López  */
7e874a669SEmilio López 
8e874a669SEmilio López #include <linux/clk-provider.h>
9e874a669SEmilio López #include <linux/delay.h>
10601da9d0SMaxime Ripard #include <linux/err.h>
11601da9d0SMaxime Ripard #include <linux/io.h>
12601da9d0SMaxime Ripard #include <linux/of_address.h>
13601da9d0SMaxime Ripard #include <linux/slab.h>
14601da9d0SMaxime Ripard #include <linux/string.h>
15e874a669SEmilio López 
16e874a669SEmilio López #include "clk-factors.h"
17e874a669SEmilio López 
18e874a669SEmilio López /*
19601da9d0SMaxime Ripard  * DOC: basic adjustable factor-based clock
20e874a669SEmilio López  *
21e874a669SEmilio López  * Traits of this clock:
22e874a669SEmilio López  * prepare - clk_prepare only ensures that parents are prepared
23e874a669SEmilio López  * enable - clk_enable only ensures that parents are enabled
24e874a669SEmilio López  * rate - rate is adjustable.
25e874a669SEmilio López  *        clk->rate = (parent->rate * N * (K + 1) >> P) / (M + 1)
26e874a669SEmilio López  * parent - fixed parent.  No clk_set_parent support
27e874a669SEmilio López  */
28e874a669SEmilio López 
29e874a669SEmilio López #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
30e874a669SEmilio López 
31601da9d0SMaxime Ripard #define FACTORS_MAX_PARENTS		5
32601da9d0SMaxime Ripard 
33c518e84cSEmilio López #define SETMASK(len, pos)		(((1U << (len)) - 1) << (pos))
34e874a669SEmilio López #define CLRMASK(len, pos)		(~(SETMASK(len, pos)))
35e874a669SEmilio López #define FACTOR_GET(bit, len, reg)	(((reg) & SETMASK(len, bit)) >> (bit))
36e874a669SEmilio López 
37e874a669SEmilio López #define FACTOR_SET(bit, len, reg, val) \
38e874a669SEmilio López 	(((reg) & CLRMASK(len, bit)) | (val << (bit)))
39e874a669SEmilio López 
clk_factors_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)40e874a669SEmilio López static unsigned long clk_factors_recalc_rate(struct clk_hw *hw,
41e874a669SEmilio López 					     unsigned long parent_rate)
42e874a669SEmilio López {
43e874a669SEmilio López 	u8 n = 1, k = 0, p = 0, m = 0;
44e874a669SEmilio López 	u32 reg;
45e874a669SEmilio López 	unsigned long rate;
46e874a669SEmilio López 	struct clk_factors *factors = to_clk_factors(hw);
47b3e919e0SChen-Yu Tsai 	const struct clk_factors_config *config = factors->config;
48e874a669SEmilio López 
49e874a669SEmilio López 	/* Fetch the register value */
50e874a669SEmilio López 	reg = readl(factors->reg);
51e874a669SEmilio López 
52e874a669SEmilio López 	/* Get each individual factor if applicable */
53e874a669SEmilio López 	if (config->nwidth != SUNXI_FACTORS_NOT_APPLICABLE)
54e874a669SEmilio López 		n = FACTOR_GET(config->nshift, config->nwidth, reg);
55e874a669SEmilio López 	if (config->kwidth != SUNXI_FACTORS_NOT_APPLICABLE)
56e874a669SEmilio López 		k = FACTOR_GET(config->kshift, config->kwidth, reg);
57e874a669SEmilio López 	if (config->mwidth != SUNXI_FACTORS_NOT_APPLICABLE)
58e874a669SEmilio López 		m = FACTOR_GET(config->mshift, config->mwidth, reg);
59e874a669SEmilio López 	if (config->pwidth != SUNXI_FACTORS_NOT_APPLICABLE)
60e874a669SEmilio López 		p = FACTOR_GET(config->pshift, config->pwidth, reg);
61e874a669SEmilio López 
62435b7be1SChen-Yu Tsai 	if (factors->recalc) {
63435b7be1SChen-Yu Tsai 		struct factors_request factors_req = {
64435b7be1SChen-Yu Tsai 			.parent_rate = parent_rate,
65435b7be1SChen-Yu Tsai 			.n = n,
66435b7be1SChen-Yu Tsai 			.k = k,
67435b7be1SChen-Yu Tsai 			.m = m,
68435b7be1SChen-Yu Tsai 			.p = p,
69435b7be1SChen-Yu Tsai 		};
70435b7be1SChen-Yu Tsai 
71435b7be1SChen-Yu Tsai 		/* get mux details from mux clk structure */
72435b7be1SChen-Yu Tsai 		if (factors->mux)
73435b7be1SChen-Yu Tsai 			factors_req.parent_index =
74435b7be1SChen-Yu Tsai 				(reg >> factors->mux->shift) &
75435b7be1SChen-Yu Tsai 				factors->mux->mask;
76435b7be1SChen-Yu Tsai 
77435b7be1SChen-Yu Tsai 		factors->recalc(&factors_req);
78435b7be1SChen-Yu Tsai 
79435b7be1SChen-Yu Tsai 		return factors_req.rate;
80435b7be1SChen-Yu Tsai 	}
81435b7be1SChen-Yu Tsai 
82e874a669SEmilio López 	/* Calculate the rate */
839a5e6c7eSChen-Yu Tsai 	rate = (parent_rate * (n + config->n_start) * (k + 1) >> p) / (m + 1);
84e874a669SEmilio López 
85e874a669SEmilio López 	return rate;
86e874a669SEmilio López }
87e874a669SEmilio López 
clk_factors_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)880817b62cSBoris Brezillon static int clk_factors_determine_rate(struct clk_hw *hw,
890817b62cSBoris Brezillon 				      struct clk_rate_request *req)
90862b7283SEmilio López {
91435b7be1SChen-Yu Tsai 	struct clk_factors *factors = to_clk_factors(hw);
921b14afa6SStephen Boyd 	struct clk_hw *parent, *best_parent = NULL;
93862b7283SEmilio López 	int i, num_parents;
94862b7283SEmilio López 	unsigned long parent_rate, best = 0, child_rate, best_child_rate = 0;
95862b7283SEmilio López 
96862b7283SEmilio López 	/* find the parent that can help provide the fastest rate <= rate */
97497295afSStephen Boyd 	num_parents = clk_hw_get_num_parents(hw);
98862b7283SEmilio López 	for (i = 0; i < num_parents; i++) {
99435b7be1SChen-Yu Tsai 		struct factors_request factors_req = {
100435b7be1SChen-Yu Tsai 			.rate = req->rate,
101435b7be1SChen-Yu Tsai 			.parent_index = i,
102435b7be1SChen-Yu Tsai 		};
1031b14afa6SStephen Boyd 		parent = clk_hw_get_parent_by_index(hw, i);
104862b7283SEmilio López 		if (!parent)
105862b7283SEmilio López 			continue;
10698d8a60eSStephen Boyd 		if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)
1071b14afa6SStephen Boyd 			parent_rate = clk_hw_round_rate(parent, req->rate);
108862b7283SEmilio López 		else
1091b14afa6SStephen Boyd 			parent_rate = clk_hw_get_rate(parent);
110862b7283SEmilio López 
111435b7be1SChen-Yu Tsai 		factors_req.parent_rate = parent_rate;
112435b7be1SChen-Yu Tsai 		factors->get_factors(&factors_req);
113435b7be1SChen-Yu Tsai 		child_rate = factors_req.rate;
114862b7283SEmilio López 
1150817b62cSBoris Brezillon 		if (child_rate <= req->rate && child_rate > best_child_rate) {
116862b7283SEmilio López 			best_parent = parent;
117862b7283SEmilio López 			best = parent_rate;
118862b7283SEmilio López 			best_child_rate = child_rate;
119862b7283SEmilio López 		}
120862b7283SEmilio López 	}
121862b7283SEmilio López 
12257d866e6SBoris Brezillon 	if (!best_parent)
12357d866e6SBoris Brezillon 		return -EINVAL;
12457d866e6SBoris Brezillon 
1251b14afa6SStephen Boyd 	req->best_parent_hw = best_parent;
1260817b62cSBoris Brezillon 	req->best_parent_rate = best;
1270817b62cSBoris Brezillon 	req->rate = best_child_rate;
128862b7283SEmilio López 
1290817b62cSBoris Brezillon 	return 0;
130862b7283SEmilio López }
131862b7283SEmilio López 
clk_factors_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)132e874a669SEmilio López static int clk_factors_set_rate(struct clk_hw *hw, unsigned long rate,
133e874a669SEmilio López 				unsigned long parent_rate)
134e874a669SEmilio López {
135cfa63688SChen-Yu Tsai 	struct factors_request req = {
136cfa63688SChen-Yu Tsai 		.rate = rate,
137cfa63688SChen-Yu Tsai 		.parent_rate = parent_rate,
138cfa63688SChen-Yu Tsai 	};
139e874a669SEmilio López 	u32 reg;
140e874a669SEmilio López 	struct clk_factors *factors = to_clk_factors(hw);
141b3e919e0SChen-Yu Tsai 	const struct clk_factors_config *config = factors->config;
142e874a669SEmilio López 	unsigned long flags = 0;
143e874a669SEmilio López 
144cfa63688SChen-Yu Tsai 	factors->get_factors(&req);
145e874a669SEmilio López 
146e874a669SEmilio López 	if (factors->lock)
147e874a669SEmilio López 		spin_lock_irqsave(factors->lock, flags);
148e874a669SEmilio López 
149e874a669SEmilio López 	/* Fetch the register value */
150e874a669SEmilio López 	reg = readl(factors->reg);
151e874a669SEmilio López 
152e874a669SEmilio López 	/* Set up the new factors - macros do not do anything if width is 0 */
153cfa63688SChen-Yu Tsai 	reg = FACTOR_SET(config->nshift, config->nwidth, reg, req.n);
154cfa63688SChen-Yu Tsai 	reg = FACTOR_SET(config->kshift, config->kwidth, reg, req.k);
155cfa63688SChen-Yu Tsai 	reg = FACTOR_SET(config->mshift, config->mwidth, reg, req.m);
156cfa63688SChen-Yu Tsai 	reg = FACTOR_SET(config->pshift, config->pwidth, reg, req.p);
157e874a669SEmilio López 
158e874a669SEmilio López 	/* Apply them now */
159e874a669SEmilio López 	writel(reg, factors->reg);
160e874a669SEmilio López 
161e874a669SEmilio López 	/* delay 500us so pll stabilizes */
162e874a669SEmilio López 	__delay((rate >> 20) * 500 / 2);
163e874a669SEmilio López 
164e874a669SEmilio López 	if (factors->lock)
165e874a669SEmilio López 		spin_unlock_irqrestore(factors->lock, flags);
166e874a669SEmilio López 
167e874a669SEmilio López 	return 0;
168e874a669SEmilio López }
169e874a669SEmilio López 
170601da9d0SMaxime Ripard static const struct clk_ops clk_factors_ops = {
171862b7283SEmilio López 	.determine_rate = clk_factors_determine_rate,
172e874a669SEmilio López 	.recalc_rate = clk_factors_recalc_rate,
173e874a669SEmilio López 	.set_rate = clk_factors_set_rate,
174e874a669SEmilio López };
175601da9d0SMaxime Ripard 
__sunxi_factors_register(struct device_node * node,const struct factors_data * data,spinlock_t * lock,void __iomem * reg,unsigned long flags)1769919d44fSStephen Boyd static struct clk *__sunxi_factors_register(struct device_node *node,
177601da9d0SMaxime Ripard 					    const struct factors_data *data,
1789919d44fSStephen Boyd 					    spinlock_t *lock, void __iomem *reg,
1799919d44fSStephen Boyd 					    unsigned long flags)
180601da9d0SMaxime Ripard {
181601da9d0SMaxime Ripard 	struct clk *clk;
182601da9d0SMaxime Ripard 	struct clk_factors *factors;
183601da9d0SMaxime Ripard 	struct clk_gate *gate = NULL;
184601da9d0SMaxime Ripard 	struct clk_mux *mux = NULL;
185601da9d0SMaxime Ripard 	struct clk_hw *gate_hw = NULL;
186601da9d0SMaxime Ripard 	struct clk_hw *mux_hw = NULL;
187601da9d0SMaxime Ripard 	const char *clk_name = node->name;
188601da9d0SMaxime Ripard 	const char *parents[FACTORS_MAX_PARENTS];
18978ca95c7SChen-Yu Tsai 	int ret, i = 0;
190601da9d0SMaxime Ripard 
191601da9d0SMaxime Ripard 	/* if we have a mux, we will have >1 parents */
1928a53fb2bSDinh Nguyen 	i = of_clk_parent_fill(node, parents, FACTORS_MAX_PARENTS);
193601da9d0SMaxime Ripard 
194601da9d0SMaxime Ripard 	/*
195601da9d0SMaxime Ripard 	 * some factor clocks, such as pll5 and pll6, may have multiple
196601da9d0SMaxime Ripard 	 * outputs, and have their name designated in factors_data
197601da9d0SMaxime Ripard 	 */
198601da9d0SMaxime Ripard 	if (data->name)
199601da9d0SMaxime Ripard 		clk_name = data->name;
200601da9d0SMaxime Ripard 	else
201601da9d0SMaxime Ripard 		of_property_read_string(node, "clock-output-names", &clk_name);
202601da9d0SMaxime Ripard 
203601da9d0SMaxime Ripard 	factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
204601da9d0SMaxime Ripard 	if (!factors)
20578ca95c7SChen-Yu Tsai 		goto err_factors;
206601da9d0SMaxime Ripard 
207601da9d0SMaxime Ripard 	/* set up factors properties */
208601da9d0SMaxime Ripard 	factors->reg = reg;
209601da9d0SMaxime Ripard 	factors->config = data->table;
210601da9d0SMaxime Ripard 	factors->get_factors = data->getter;
211435b7be1SChen-Yu Tsai 	factors->recalc = data->recalc;
212601da9d0SMaxime Ripard 	factors->lock = lock;
213601da9d0SMaxime Ripard 
214601da9d0SMaxime Ripard 	/* Add a gate if this factor clock can be gated */
215601da9d0SMaxime Ripard 	if (data->enable) {
216601da9d0SMaxime Ripard 		gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
21778ca95c7SChen-Yu Tsai 		if (!gate)
21878ca95c7SChen-Yu Tsai 			goto err_gate;
219601da9d0SMaxime Ripard 
2204cbeaebbSChen-Yu Tsai 		factors->gate = gate;
2214cbeaebbSChen-Yu Tsai 
222601da9d0SMaxime Ripard 		/* set up gate properties */
223601da9d0SMaxime Ripard 		gate->reg = reg;
224601da9d0SMaxime Ripard 		gate->bit_idx = data->enable;
225601da9d0SMaxime Ripard 		gate->lock = factors->lock;
226601da9d0SMaxime Ripard 		gate_hw = &gate->hw;
227601da9d0SMaxime Ripard 	}
228601da9d0SMaxime Ripard 
229601da9d0SMaxime Ripard 	/* Add a mux if this factor clock can be muxed */
230601da9d0SMaxime Ripard 	if (data->mux) {
231601da9d0SMaxime Ripard 		mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
23278ca95c7SChen-Yu Tsai 		if (!mux)
23378ca95c7SChen-Yu Tsai 			goto err_mux;
234601da9d0SMaxime Ripard 
2354cbeaebbSChen-Yu Tsai 		factors->mux = mux;
2364cbeaebbSChen-Yu Tsai 
237601da9d0SMaxime Ripard 		/* set up gate properties */
238601da9d0SMaxime Ripard 		mux->reg = reg;
239601da9d0SMaxime Ripard 		mux->shift = data->mux;
240e94f8cb3SChen-Yu Tsai 		mux->mask = data->muxmask;
241601da9d0SMaxime Ripard 		mux->lock = factors->lock;
242601da9d0SMaxime Ripard 		mux_hw = &mux->hw;
243601da9d0SMaxime Ripard 	}
244601da9d0SMaxime Ripard 
245601da9d0SMaxime Ripard 	clk = clk_register_composite(NULL, clk_name,
246601da9d0SMaxime Ripard 			parents, i,
247601da9d0SMaxime Ripard 			mux_hw, &clk_mux_ops,
248601da9d0SMaxime Ripard 			&factors->hw, &clk_factors_ops,
2499919d44fSStephen Boyd 			gate_hw, &clk_gate_ops, CLK_IS_CRITICAL);
25078ca95c7SChen-Yu Tsai 	if (IS_ERR(clk))
25178ca95c7SChen-Yu Tsai 		goto err_register;
252601da9d0SMaxime Ripard 
25378ca95c7SChen-Yu Tsai 	ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
25478ca95c7SChen-Yu Tsai 	if (ret)
25578ca95c7SChen-Yu Tsai 		goto err_provider;
25678ca95c7SChen-Yu Tsai 
257601da9d0SMaxime Ripard 	return clk;
25878ca95c7SChen-Yu Tsai 
25978ca95c7SChen-Yu Tsai err_provider:
26078ca95c7SChen-Yu Tsai 	/* TODO: The composite clock stuff will leak a bit here. */
26178ca95c7SChen-Yu Tsai 	clk_unregister(clk);
26278ca95c7SChen-Yu Tsai err_register:
26378ca95c7SChen-Yu Tsai 	kfree(mux);
26478ca95c7SChen-Yu Tsai err_mux:
26578ca95c7SChen-Yu Tsai 	kfree(gate);
26678ca95c7SChen-Yu Tsai err_gate:
26778ca95c7SChen-Yu Tsai 	kfree(factors);
26878ca95c7SChen-Yu Tsai err_factors:
26978ca95c7SChen-Yu Tsai 	return NULL;
270601da9d0SMaxime Ripard }
2714cbeaebbSChen-Yu Tsai 
sunxi_factors_register(struct device_node * node,const struct factors_data * data,spinlock_t * lock,void __iomem * reg)2729919d44fSStephen Boyd struct clk *sunxi_factors_register(struct device_node *node,
2739919d44fSStephen Boyd 				   const struct factors_data *data,
2749919d44fSStephen Boyd 				   spinlock_t *lock,
2759919d44fSStephen Boyd 				   void __iomem *reg)
2769919d44fSStephen Boyd {
2779919d44fSStephen Boyd 	return __sunxi_factors_register(node, data, lock, reg, 0);
2789919d44fSStephen Boyd }
2799919d44fSStephen Boyd 
sunxi_factors_register_critical(struct device_node * node,const struct factors_data * data,spinlock_t * lock,void __iomem * reg)2809919d44fSStephen Boyd struct clk *sunxi_factors_register_critical(struct device_node *node,
2819919d44fSStephen Boyd 					    const struct factors_data *data,
2829919d44fSStephen Boyd 					    spinlock_t *lock,
2839919d44fSStephen Boyd 					    void __iomem *reg)
2849919d44fSStephen Boyd {
2859919d44fSStephen Boyd 	return __sunxi_factors_register(node, data, lock, reg, CLK_IS_CRITICAL);
2869919d44fSStephen Boyd }
2879919d44fSStephen Boyd 
sunxi_factors_unregister(struct device_node * node,struct clk * clk)2884cbeaebbSChen-Yu Tsai void sunxi_factors_unregister(struct device_node *node, struct clk *clk)
2894cbeaebbSChen-Yu Tsai {
2904cbeaebbSChen-Yu Tsai 	struct clk_hw *hw = __clk_get_hw(clk);
2914cbeaebbSChen-Yu Tsai 	struct clk_factors *factors;
2924cbeaebbSChen-Yu Tsai 
2934cbeaebbSChen-Yu Tsai 	if (!hw)
2944cbeaebbSChen-Yu Tsai 		return;
2954cbeaebbSChen-Yu Tsai 
2964cbeaebbSChen-Yu Tsai 	factors = to_clk_factors(hw);
2974cbeaebbSChen-Yu Tsai 
2984cbeaebbSChen-Yu Tsai 	of_clk_del_provider(node);
2994cbeaebbSChen-Yu Tsai 	/* TODO: The composite clock stuff will leak a bit here. */
3004cbeaebbSChen-Yu Tsai 	clk_unregister(clk);
3014cbeaebbSChen-Yu Tsai 	kfree(factors->mux);
3024cbeaebbSChen-Yu Tsai 	kfree(factors->gate);
3034cbeaebbSChen-Yu Tsai 	kfree(factors);
3044cbeaebbSChen-Yu Tsai }
305