clk: sunxi: factors: Support custom formulas
[cascardo/linux.git] / drivers / clk / sunxi / clk-factors.c
1 /*
2  * Copyright (C) 2013 Emilio López <emilio@elopez.com.ar>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Adjustable factor-based clock implementation
9  */
10
11 #include <linux/clk-provider.h>
12 #include <linux/delay.h>
13 #include <linux/err.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19
20 #include "clk-factors.h"
21
22 /*
23  * DOC: basic adjustable factor-based clock
24  *
25  * Traits of this clock:
26  * prepare - clk_prepare only ensures that parents are prepared
27  * enable - clk_enable only ensures that parents are enabled
28  * rate - rate is adjustable.
29  *        clk->rate = (parent->rate * N * (K + 1) >> P) / (M + 1)
30  * parent - fixed parent.  No clk_set_parent support
31  */
32
33 #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
34
35 #define FACTORS_MAX_PARENTS             5
36
37 #define SETMASK(len, pos)               (((1U << (len)) - 1) << (pos))
38 #define CLRMASK(len, pos)               (~(SETMASK(len, pos)))
39 #define FACTOR_GET(bit, len, reg)       (((reg) & SETMASK(len, bit)) >> (bit))
40
41 #define FACTOR_SET(bit, len, reg, val) \
42         (((reg) & CLRMASK(len, bit)) | (val << (bit)))
43
44 static unsigned long clk_factors_recalc_rate(struct clk_hw *hw,
45                                              unsigned long parent_rate)
46 {
47         u8 n = 1, k = 0, p = 0, m = 0;
48         u32 reg;
49         unsigned long rate;
50         struct clk_factors *factors = to_clk_factors(hw);
51         const struct clk_factors_config *config = factors->config;
52
53         /* Fetch the register value */
54         reg = readl(factors->reg);
55
56         /* Get each individual factor if applicable */
57         if (config->nwidth != SUNXI_FACTORS_NOT_APPLICABLE)
58                 n = FACTOR_GET(config->nshift, config->nwidth, reg);
59         if (config->kwidth != SUNXI_FACTORS_NOT_APPLICABLE)
60                 k = FACTOR_GET(config->kshift, config->kwidth, reg);
61         if (config->mwidth != SUNXI_FACTORS_NOT_APPLICABLE)
62                 m = FACTOR_GET(config->mshift, config->mwidth, reg);
63         if (config->pwidth != SUNXI_FACTORS_NOT_APPLICABLE)
64                 p = FACTOR_GET(config->pshift, config->pwidth, reg);
65
66         if (factors->recalc) {
67                 struct factors_request factors_req = {
68                         .parent_rate = parent_rate,
69                         .n = n,
70                         .k = k,
71                         .m = m,
72                         .p = p,
73                 };
74
75                 /* get mux details from mux clk structure */
76                 if (factors->mux)
77                         factors_req.parent_index =
78                                 (reg >> factors->mux->shift) &
79                                 factors->mux->mask;
80
81                 factors->recalc(&factors_req);
82
83                 return factors_req.rate;
84         }
85
86         /* Calculate the rate */
87         rate = (parent_rate * (n + config->n_start) * (k + 1) >> p) / (m + 1);
88
89         return rate;
90 }
91
92 static long clk_factors_round_rate(struct clk_hw *hw, unsigned long rate,
93                                    unsigned long *parent_rate)
94 {
95         struct clk_factors *factors = to_clk_factors(hw);
96         struct factors_request req = {
97                 .rate = rate,
98                 .parent_rate = *parent_rate,
99         };
100
101         factors->get_factors(&req);
102
103
104         return rate;
105 }
106
107 static int clk_factors_determine_rate(struct clk_hw *hw,
108                                       struct clk_rate_request *req)
109 {
110         struct clk_factors *factors = to_clk_factors(hw);
111         struct clk_hw *parent, *best_parent = NULL;
112         int i, num_parents;
113         unsigned long parent_rate, best = 0, child_rate, best_child_rate = 0;
114
115         /* find the parent that can help provide the fastest rate <= rate */
116         num_parents = clk_hw_get_num_parents(hw);
117         for (i = 0; i < num_parents; i++) {
118                 struct factors_request factors_req = {
119                         .rate = req->rate,
120                         .parent_index = i,
121                 };
122                 parent = clk_hw_get_parent_by_index(hw, i);
123                 if (!parent)
124                         continue;
125                 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)
126                         parent_rate = clk_hw_round_rate(parent, req->rate);
127                 else
128                         parent_rate = clk_hw_get_rate(parent);
129
130                 factors_req.parent_rate = parent_rate;
131                 factors->get_factors(&factors_req);
132                 child_rate = factors_req.rate;
133
134                 if (child_rate <= req->rate && child_rate > best_child_rate) {
135                         best_parent = parent;
136                         best = parent_rate;
137                         best_child_rate = child_rate;
138                 }
139         }
140
141         if (!best_parent)
142                 return -EINVAL;
143
144         req->best_parent_hw = best_parent;
145         req->best_parent_rate = best;
146         req->rate = best_child_rate;
147
148         return 0;
149 }
150
151 static int clk_factors_set_rate(struct clk_hw *hw, unsigned long rate,
152                                 unsigned long parent_rate)
153 {
154         struct factors_request req = {
155                 .rate = rate,
156                 .parent_rate = parent_rate,
157         };
158         u32 reg;
159         struct clk_factors *factors = to_clk_factors(hw);
160         const struct clk_factors_config *config = factors->config;
161         unsigned long flags = 0;
162
163         factors->get_factors(&req);
164
165         if (factors->lock)
166                 spin_lock_irqsave(factors->lock, flags);
167
168         /* Fetch the register value */
169         reg = readl(factors->reg);
170
171         /* Set up the new factors - macros do not do anything if width is 0 */
172         reg = FACTOR_SET(config->nshift, config->nwidth, reg, req.n);
173         reg = FACTOR_SET(config->kshift, config->kwidth, reg, req.k);
174         reg = FACTOR_SET(config->mshift, config->mwidth, reg, req.m);
175         reg = FACTOR_SET(config->pshift, config->pwidth, reg, req.p);
176
177         /* Apply them now */
178         writel(reg, factors->reg);
179
180         /* delay 500us so pll stabilizes */
181         __delay((rate >> 20) * 500 / 2);
182
183         if (factors->lock)
184                 spin_unlock_irqrestore(factors->lock, flags);
185
186         return 0;
187 }
188
189 static const struct clk_ops clk_factors_ops = {
190         .determine_rate = clk_factors_determine_rate,
191         .recalc_rate = clk_factors_recalc_rate,
192         .round_rate = clk_factors_round_rate,
193         .set_rate = clk_factors_set_rate,
194 };
195
196 struct clk *sunxi_factors_register(struct device_node *node,
197                                    const struct factors_data *data,
198                                    spinlock_t *lock,
199                                    void __iomem *reg)
200 {
201         struct clk *clk;
202         struct clk_factors *factors;
203         struct clk_gate *gate = NULL;
204         struct clk_mux *mux = NULL;
205         struct clk_hw *gate_hw = NULL;
206         struct clk_hw *mux_hw = NULL;
207         const char *clk_name = node->name;
208         const char *parents[FACTORS_MAX_PARENTS];
209         int ret, i = 0;
210
211         /* if we have a mux, we will have >1 parents */
212         i = of_clk_parent_fill(node, parents, FACTORS_MAX_PARENTS);
213
214         /*
215          * some factor clocks, such as pll5 and pll6, may have multiple
216          * outputs, and have their name designated in factors_data
217          */
218         if (data->name)
219                 clk_name = data->name;
220         else
221                 of_property_read_string(node, "clock-output-names", &clk_name);
222
223         factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
224         if (!factors)
225                 goto err_factors;
226
227         /* set up factors properties */
228         factors->reg = reg;
229         factors->config = data->table;
230         factors->get_factors = data->getter;
231         factors->recalc = data->recalc;
232         factors->lock = lock;
233
234         /* Add a gate if this factor clock can be gated */
235         if (data->enable) {
236                 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
237                 if (!gate)
238                         goto err_gate;
239
240                 factors->gate = gate;
241
242                 /* set up gate properties */
243                 gate->reg = reg;
244                 gate->bit_idx = data->enable;
245                 gate->lock = factors->lock;
246                 gate_hw = &gate->hw;
247         }
248
249         /* Add a mux if this factor clock can be muxed */
250         if (data->mux) {
251                 mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
252                 if (!mux)
253                         goto err_mux;
254
255                 factors->mux = mux;
256
257                 /* set up gate properties */
258                 mux->reg = reg;
259                 mux->shift = data->mux;
260                 mux->mask = data->muxmask;
261                 mux->lock = factors->lock;
262                 mux_hw = &mux->hw;
263         }
264
265         clk = clk_register_composite(NULL, clk_name,
266                         parents, i,
267                         mux_hw, &clk_mux_ops,
268                         &factors->hw, &clk_factors_ops,
269                         gate_hw, &clk_gate_ops, 0);
270         if (IS_ERR(clk))
271                 goto err_register;
272
273         ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
274         if (ret)
275                 goto err_provider;
276
277         ret = clk_register_clkdev(clk, clk_name, NULL);
278         if (ret)
279                 goto err_clkdev;
280
281         return clk;
282
283 err_clkdev:
284         of_clk_del_provider(node);
285 err_provider:
286         /* TODO: The composite clock stuff will leak a bit here. */
287         clk_unregister(clk);
288 err_register:
289         kfree(mux);
290 err_mux:
291         kfree(gate);
292 err_gate:
293         kfree(factors);
294 err_factors:
295         return NULL;
296 }
297
298 void sunxi_factors_unregister(struct device_node *node, struct clk *clk)
299 {
300         struct clk_hw *hw = __clk_get_hw(clk);
301         struct clk_factors *factors;
302         const char *name;
303
304         if (!hw)
305                 return;
306
307         factors = to_clk_factors(hw);
308         name = clk_hw_get_name(hw);
309
310         /* No unregister call for clkdev_* */
311         of_clk_del_provider(node);
312         /* TODO: The composite clock stuff will leak a bit here. */
313         clk_unregister(clk);
314         kfree(factors->mux);
315         kfree(factors->gate);
316         kfree(factors);
317 }