clk: sunxi: factors: Consolidate get_factors parameters into a struct
[cascardo/linux.git] / drivers / clk / sunxi / clk-mod0.c
1 /*
2  * Copyright 2013 Emilio López
3  *
4  * Emilio López <emilio@elopez.com.ar>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/clk.h>
18 #include <linux/clk-provider.h>
19 #include <linux/of_address.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22
23 #include "clk-factors.h"
24
25 /**
26  * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
27  * MOD0 rate is calculated as follows
28  * rate = (parent_rate >> p) / (m + 1);
29  */
30
31 static void sun4i_a10_get_mod0_factors(struct factors_request *req)
32 {
33         u8 div, calcm, calcp;
34
35         /* These clocks can only divide, so we will never be able to achieve
36          * frequencies higher than the parent frequency */
37         if (req->rate > req->parent_rate)
38                 req->rate = req->parent_rate;
39
40         div = DIV_ROUND_UP(req->parent_rate, req->rate);
41
42         if (div < 16)
43                 calcp = 0;
44         else if (div / 2 < 16)
45                 calcp = 1;
46         else if (div / 4 < 16)
47                 calcp = 2;
48         else
49                 calcp = 3;
50
51         calcm = DIV_ROUND_UP(div, 1 << calcp);
52
53         req->rate = (req->parent_rate >> calcp) / calcm;
54         req->m = calcm - 1;
55         req->p = calcp;
56 }
57
58 /* user manual says "n" but it's really "p" */
59 static const struct clk_factors_config sun4i_a10_mod0_config = {
60         .mshift = 0,
61         .mwidth = 4,
62         .pshift = 16,
63         .pwidth = 2,
64 };
65
66 static const struct factors_data sun4i_a10_mod0_data = {
67         .enable = 31,
68         .mux = 24,
69         .muxmask = BIT(1) | BIT(0),
70         .table = &sun4i_a10_mod0_config,
71         .getter = sun4i_a10_get_mod0_factors,
72 };
73
74 static DEFINE_SPINLOCK(sun4i_a10_mod0_lock);
75
76 static void __init sun4i_a10_mod0_setup(struct device_node *node)
77 {
78         void __iomem *reg;
79
80         reg = of_iomap(node, 0);
81         if (!reg) {
82                 /*
83                  * This happens with mod0 clk nodes instantiated through
84                  * mfd, as those do not have their resources assigned at
85                  * CLK_OF_DECLARE time yet, so do not print an error.
86                  */
87                 return;
88         }
89
90         sunxi_factors_register(node, &sun4i_a10_mod0_data,
91                                &sun4i_a10_mod0_lock, reg);
92 }
93 CLK_OF_DECLARE(sun4i_a10_mod0, "allwinner,sun4i-a10-mod0-clk", sun4i_a10_mod0_setup);
94
95 static int sun4i_a10_mod0_clk_probe(struct platform_device *pdev)
96 {
97         struct device_node *np = pdev->dev.of_node;
98         struct resource *r;
99         void __iomem *reg;
100
101         if (!np)
102                 return -ENODEV;
103
104         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
105         reg = devm_ioremap_resource(&pdev->dev, r);
106         if (IS_ERR(reg))
107                 return PTR_ERR(reg);
108
109         sunxi_factors_register(np, &sun4i_a10_mod0_data,
110                                &sun4i_a10_mod0_lock, reg);
111         return 0;
112 }
113
114 static const struct of_device_id sun4i_a10_mod0_clk_dt_ids[] = {
115         { .compatible = "allwinner,sun4i-a10-mod0-clk" },
116         { /* sentinel */ }
117 };
118
119 static struct platform_driver sun4i_a10_mod0_clk_driver = {
120         .driver = {
121                 .name = "sun4i-a10-mod0-clk",
122                 .of_match_table = sun4i_a10_mod0_clk_dt_ids,
123         },
124         .probe = sun4i_a10_mod0_clk_probe,
125 };
126 builtin_platform_driver(sun4i_a10_mod0_clk_driver);
127
128 static const struct factors_data sun9i_a80_mod0_data __initconst = {
129         .enable = 31,
130         .mux = 24,
131         .muxmask = BIT(3) | BIT(2) | BIT(1) | BIT(0),
132         .table = &sun4i_a10_mod0_config,
133         .getter = sun4i_a10_get_mod0_factors,
134 };
135
136 static void __init sun9i_a80_mod0_setup(struct device_node *node)
137 {
138         void __iomem *reg;
139
140         reg = of_io_request_and_map(node, 0, of_node_full_name(node));
141         if (IS_ERR(reg)) {
142                 pr_err("Could not get registers for mod0-clk: %s\n",
143                        node->name);
144                 return;
145         }
146
147         sunxi_factors_register(node, &sun9i_a80_mod0_data,
148                                &sun4i_a10_mod0_lock, reg);
149 }
150 CLK_OF_DECLARE(sun9i_a80_mod0, "allwinner,sun9i-a80-mod0-clk", sun9i_a80_mod0_setup);
151
152 static DEFINE_SPINLOCK(sun5i_a13_mbus_lock);
153
154 static void __init sun5i_a13_mbus_setup(struct device_node *node)
155 {
156         struct clk *mbus;
157         void __iomem *reg;
158
159         reg = of_iomap(node, 0);
160         if (!reg) {
161                 pr_err("Could not get registers for a13-mbus-clk\n");
162                 return;
163         }
164
165         mbus = sunxi_factors_register(node, &sun4i_a10_mod0_data,
166                                       &sun5i_a13_mbus_lock, reg);
167
168         /* The MBUS clocks needs to be always enabled */
169         __clk_get(mbus);
170         clk_prepare_enable(mbus);
171 }
172 CLK_OF_DECLARE(sun5i_a13_mbus, "allwinner,sun5i-a13-mbus-clk", sun5i_a13_mbus_setup);
173
174 struct mmc_phase {
175         struct clk_hw           hw;
176         u8                      offset;
177         void __iomem            *reg;
178         spinlock_t              *lock;
179 };
180
181 #define to_mmc_phase(_hw) container_of(_hw, struct mmc_phase, hw)
182
183 static int mmc_get_phase(struct clk_hw *hw)
184 {
185         struct clk *mmc, *mmc_parent, *clk = hw->clk;
186         struct mmc_phase *phase = to_mmc_phase(hw);
187         unsigned int mmc_rate, mmc_parent_rate;
188         u16 step, mmc_div;
189         u32 value;
190         u8 delay;
191
192         value = readl(phase->reg);
193         delay = (value >> phase->offset) & 0x3;
194
195         if (!delay)
196                 return 180;
197
198         /* Get the main MMC clock */
199         mmc = clk_get_parent(clk);
200         if (!mmc)
201                 return -EINVAL;
202
203         /* And its rate */
204         mmc_rate = clk_get_rate(mmc);
205         if (!mmc_rate)
206                 return -EINVAL;
207
208         /* Now, get the MMC parent (most likely some PLL) */
209         mmc_parent = clk_get_parent(mmc);
210         if (!mmc_parent)
211                 return -EINVAL;
212
213         /* And its rate */
214         mmc_parent_rate = clk_get_rate(mmc_parent);
215         if (!mmc_parent_rate)
216                 return -EINVAL;
217
218         /* Get MMC clock divider */
219         mmc_div = mmc_parent_rate / mmc_rate;
220
221         step = DIV_ROUND_CLOSEST(360, mmc_div);
222         return delay * step;
223 }
224
225 static int mmc_set_phase(struct clk_hw *hw, int degrees)
226 {
227         struct clk *mmc, *mmc_parent, *clk = hw->clk;
228         struct mmc_phase *phase = to_mmc_phase(hw);
229         unsigned int mmc_rate, mmc_parent_rate;
230         unsigned long flags;
231         u32 value;
232         u8 delay;
233
234         /* Get the main MMC clock */
235         mmc = clk_get_parent(clk);
236         if (!mmc)
237                 return -EINVAL;
238
239         /* And its rate */
240         mmc_rate = clk_get_rate(mmc);
241         if (!mmc_rate)
242                 return -EINVAL;
243
244         /* Now, get the MMC parent (most likely some PLL) */
245         mmc_parent = clk_get_parent(mmc);
246         if (!mmc_parent)
247                 return -EINVAL;
248
249         /* And its rate */
250         mmc_parent_rate = clk_get_rate(mmc_parent);
251         if (!mmc_parent_rate)
252                 return -EINVAL;
253
254         if (degrees != 180) {
255                 u16 step, mmc_div;
256
257                 /* Get MMC clock divider */
258                 mmc_div = mmc_parent_rate / mmc_rate;
259
260                 /*
261                  * We can only outphase the clocks by multiple of the
262                  * PLL's period.
263                  *
264                  * Since the MMC clock in only a divider, and the
265                  * formula to get the outphasing in degrees is deg =
266                  * 360 * delta / period
267                  *
268                  * If we simplify this formula, we can see that the
269                  * only thing that we're concerned about is the number
270                  * of period we want to outphase our clock from, and
271                  * the divider set by the MMC clock.
272                  */
273                 step = DIV_ROUND_CLOSEST(360, mmc_div);
274                 delay = DIV_ROUND_CLOSEST(degrees, step);
275         } else {
276                 delay = 0;
277         }
278
279         spin_lock_irqsave(phase->lock, flags);
280         value = readl(phase->reg);
281         value &= ~GENMASK(phase->offset + 3, phase->offset);
282         value |= delay << phase->offset;
283         writel(value, phase->reg);
284         spin_unlock_irqrestore(phase->lock, flags);
285
286         return 0;
287 }
288
289 static const struct clk_ops mmc_clk_ops = {
290         .get_phase      = mmc_get_phase,
291         .set_phase      = mmc_set_phase,
292 };
293
294 /*
295  * sunxi_mmc_setup - Common setup function for mmc module clocks
296  *
297  * The only difference between module clocks on different platforms is the
298  * width of the mux register bits and the valid values, which are passed in
299  * through struct factors_data. The phase clocks parts are identical.
300  */
301 static void __init sunxi_mmc_setup(struct device_node *node,
302                                    const struct factors_data *data,
303                                    spinlock_t *lock)
304 {
305         struct clk_onecell_data *clk_data;
306         const char *parent;
307         void __iomem *reg;
308         int i;
309
310         reg = of_io_request_and_map(node, 0, of_node_full_name(node));
311         if (IS_ERR(reg)) {
312                 pr_err("Couldn't map the %s clock registers\n", node->name);
313                 return;
314         }
315
316         clk_data = kmalloc(sizeof(*clk_data), GFP_KERNEL);
317         if (!clk_data)
318                 return;
319
320         clk_data->clks = kcalloc(3, sizeof(*clk_data->clks), GFP_KERNEL);
321         if (!clk_data->clks)
322                 goto err_free_data;
323
324         clk_data->clk_num = 3;
325         clk_data->clks[0] = sunxi_factors_register(node, data, lock, reg);
326         if (!clk_data->clks[0])
327                 goto err_free_clks;
328
329         parent = __clk_get_name(clk_data->clks[0]);
330
331         for (i = 1; i < 3; i++) {
332                 struct clk_init_data init = {
333                         .num_parents    = 1,
334                         .parent_names   = &parent,
335                         .ops            = &mmc_clk_ops,
336                 };
337                 struct mmc_phase *phase;
338
339                 phase = kmalloc(sizeof(*phase), GFP_KERNEL);
340                 if (!phase)
341                         continue;
342
343                 phase->hw.init = &init;
344                 phase->reg = reg;
345                 phase->lock = lock;
346
347                 if (i == 1)
348                         phase->offset = 8;
349                 else
350                         phase->offset = 20;
351
352                 if (of_property_read_string_index(node, "clock-output-names",
353                                                   i, &init.name))
354                         init.name = node->name;
355
356                 clk_data->clks[i] = clk_register(NULL, &phase->hw);
357                 if (IS_ERR(clk_data->clks[i])) {
358                         kfree(phase);
359                         continue;
360                 }
361         }
362
363         of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
364
365         return;
366
367 err_free_clks:
368         kfree(clk_data->clks);
369 err_free_data:
370         kfree(clk_data);
371 }
372
373 static DEFINE_SPINLOCK(sun4i_a10_mmc_lock);
374
375 static void __init sun4i_a10_mmc_setup(struct device_node *node)
376 {
377         sunxi_mmc_setup(node, &sun4i_a10_mod0_data, &sun4i_a10_mmc_lock);
378 }
379 CLK_OF_DECLARE(sun4i_a10_mmc, "allwinner,sun4i-a10-mmc-clk", sun4i_a10_mmc_setup);
380
381 static DEFINE_SPINLOCK(sun9i_a80_mmc_lock);
382
383 static void __init sun9i_a80_mmc_setup(struct device_node *node)
384 {
385         sunxi_mmc_setup(node, &sun9i_a80_mod0_data, &sun9i_a80_mmc_lock);
386 }
387 CLK_OF_DECLARE(sun9i_a80_mmc, "allwinner,sun9i-a80-mmc-clk", sun9i_a80_mmc_setup);