]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/clk/sunxi/clk-sun8i-mbus.c
clk: sunxi: factors: Consolidate get_factors parameters into a struct
[karo-tx-linux.git] / drivers / clk / sunxi / clk-sun8i-mbus.c
1 /*
2  * Copyright 2014 Chen-Yu Tsai
3  *
4  * Chen-Yu Tsai <wens@csie.org>
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
21 #include "clk-factors.h"
22
23 /**
24  * sun8i_a23_get_mbus_factors() - calculates m factor for MBUS clocks
25  * MBUS rate is calculated as follows
26  * rate = parent_rate / (m + 1);
27  */
28
29 static void sun8i_a23_get_mbus_factors(struct factors_request *req)
30 {
31         u8 div;
32
33         /*
34          * These clocks can only divide, so we will never be able to
35          * achieve frequencies higher than the parent frequency
36          */
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 > 8)
43                 div = 8;
44
45         req->rate = req->parent_rate / div;
46         req->m = div - 1;
47 }
48
49 static struct clk_factors_config sun8i_a23_mbus_config = {
50         .mshift = 0,
51         .mwidth = 3,
52 };
53
54 static const struct factors_data sun8i_a23_mbus_data __initconst = {
55         .enable = 31,
56         .mux = 24,
57         .muxmask = BIT(1) | BIT(0),
58         .table = &sun8i_a23_mbus_config,
59         .getter = sun8i_a23_get_mbus_factors,
60 };
61
62 static DEFINE_SPINLOCK(sun8i_a23_mbus_lock);
63
64 static void __init sun8i_a23_mbus_setup(struct device_node *node)
65 {
66         struct clk *mbus;
67         void __iomem *reg;
68
69         reg = of_iomap(node, 0);
70         if (!reg) {
71                 pr_err("Could not get registers for a23-mbus-clk\n");
72                 return;
73         }
74
75         mbus = sunxi_factors_register(node, &sun8i_a23_mbus_data,
76                                       &sun8i_a23_mbus_lock, reg);
77
78         /* The MBUS clocks needs to be always enabled */
79         __clk_get(mbus);
80         clk_prepare_enable(mbus);
81 }
82 CLK_OF_DECLARE(sun8i_a23_mbus, "allwinner,sun8i-a23-mbus-clk", sun8i_a23_mbus_setup);