blob: 3fbc9cab53a6a901742f9d5d8dcc2b562fb5827a [file] [log] [blame]
Chao Xie6b63f022012-08-20 02:55:11 +00001/*
2 * mmp factor clock operation source file
3 *
4 * Copyright (C) 2012 Marvell
5 * Chao Xie <xiechao.mail@gmail.com>
6 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#include <linux/clk-provider.h>
13#include <linux/slab.h>
14#include <linux/io.h>
15#include <linux/err.h>
16
17#include "clk.h"
18/*
19 * It is M/N clock
20 *
21 * Fout from synthesizer can be given from two equations:
22 * numerator/denominator = Fin / (Fout * factor)
23 */
24
Chao Xie2bd1e252014-10-31 10:13:41 +080025#define to_clk_factor(hw) container_of(hw, struct mmp_clk_factor, hw)
26struct mmp_clk_factor {
Chao Xie6b63f022012-08-20 02:55:11 +000027 struct clk_hw hw;
28 void __iomem *base;
Chao Xie2bd1e252014-10-31 10:13:41 +080029 struct mmp_clk_factor_masks *masks;
30 struct mmp_clk_factor_tbl *ftbl;
Chao Xie6b63f022012-08-20 02:55:11 +000031 unsigned int ftbl_cnt;
32};
33
34static long clk_factor_round_rate(struct clk_hw *hw, unsigned long drate,
35 unsigned long *prate)
36{
Chao Xie2bd1e252014-10-31 10:13:41 +080037 struct mmp_clk_factor *factor = to_clk_factor(hw);
Chao Xie6b63f022012-08-20 02:55:11 +000038 unsigned long rate = 0, prev_rate;
39 int i;
40
41 for (i = 0; i < factor->ftbl_cnt; i++) {
42 prev_rate = rate;
Chao Xiec45693a62014-01-23 10:47:41 +080043 rate = (((*prate / 10000) * factor->ftbl[i].den) /
44 (factor->ftbl[i].num * factor->masks->factor)) * 10000;
Chao Xie6b63f022012-08-20 02:55:11 +000045 if (rate > drate)
46 break;
47 }
Chao Xie5d26c152014-01-23 10:47:42 +080048 if ((i == 0) || (i == factor->ftbl_cnt)) {
Chao Xie6b63f022012-08-20 02:55:11 +000049 return rate;
Chao Xie5d26c152014-01-23 10:47:42 +080050 } else {
51 if ((drate - prev_rate) > (rate - drate))
52 return rate;
53 else
54 return prev_rate;
55 }
Chao Xie6b63f022012-08-20 02:55:11 +000056}
57
58static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
59 unsigned long parent_rate)
60{
Chao Xie2bd1e252014-10-31 10:13:41 +080061 struct mmp_clk_factor *factor = to_clk_factor(hw);
62 struct mmp_clk_factor_masks *masks = factor->masks;
Chao Xie6b63f022012-08-20 02:55:11 +000063 unsigned int val, num, den;
64
65 val = readl_relaxed(factor->base);
66
67 /* calculate numerator */
68 num = (val >> masks->num_shift) & masks->num_mask;
69
70 /* calculate denominator */
Chao Xie7433ab42014-01-23 10:47:40 +080071 den = (val >> masks->den_shift) & masks->den_mask;
Chao Xie6b63f022012-08-20 02:55:11 +000072
73 if (!den)
74 return 0;
75
76 return (((parent_rate / 10000) * den) /
77 (num * factor->masks->factor)) * 10000;
78}
79
80/* Configures new clock rate*/
81static int clk_factor_set_rate(struct clk_hw *hw, unsigned long drate,
82 unsigned long prate)
83{
Chao Xie2bd1e252014-10-31 10:13:41 +080084 struct mmp_clk_factor *factor = to_clk_factor(hw);
85 struct mmp_clk_factor_masks *masks = factor->masks;
Chao Xie6b63f022012-08-20 02:55:11 +000086 int i;
87 unsigned long val;
88 unsigned long prev_rate, rate = 0;
89
90 for (i = 0; i < factor->ftbl_cnt; i++) {
91 prev_rate = rate;
Chao Xiec45693a62014-01-23 10:47:41 +080092 rate = (((prate / 10000) * factor->ftbl[i].den) /
93 (factor->ftbl[i].num * factor->masks->factor)) * 10000;
Chao Xie6b63f022012-08-20 02:55:11 +000094 if (rate > drate)
95 break;
96 }
97 if (i > 0)
98 i--;
99
100 val = readl_relaxed(factor->base);
101
102 val &= ~(masks->num_mask << masks->num_shift);
103 val |= (factor->ftbl[i].num & masks->num_mask) << masks->num_shift;
104
105 val &= ~(masks->den_mask << masks->den_shift);
106 val |= (factor->ftbl[i].den & masks->den_mask) << masks->den_shift;
107
108 writel_relaxed(val, factor->base);
109
110 return 0;
111}
112
113static struct clk_ops clk_factor_ops = {
114 .recalc_rate = clk_factor_recalc_rate,
115 .round_rate = clk_factor_round_rate,
116 .set_rate = clk_factor_set_rate,
117};
118
119struct clk *mmp_clk_register_factor(const char *name, const char *parent_name,
120 unsigned long flags, void __iomem *base,
Chao Xie2bd1e252014-10-31 10:13:41 +0800121 struct mmp_clk_factor_masks *masks,
122 struct mmp_clk_factor_tbl *ftbl,
Chao Xie6b63f022012-08-20 02:55:11 +0000123 unsigned int ftbl_cnt)
124{
Chao Xie2bd1e252014-10-31 10:13:41 +0800125 struct mmp_clk_factor *factor;
Chao Xie6b63f022012-08-20 02:55:11 +0000126 struct clk_init_data init;
127 struct clk *clk;
128
129 if (!masks) {
130 pr_err("%s: must pass a clk_factor_mask\n", __func__);
131 return ERR_PTR(-EINVAL);
132 }
133
134 factor = kzalloc(sizeof(*factor), GFP_KERNEL);
135 if (!factor) {
136 pr_err("%s: could not allocate factor clk\n", __func__);
137 return ERR_PTR(-ENOMEM);
138 }
139
140 /* struct clk_aux assignments */
141 factor->base = base;
142 factor->masks = masks;
143 factor->ftbl = ftbl;
144 factor->ftbl_cnt = ftbl_cnt;
145 factor->hw.init = &init;
146
147 init.name = name;
148 init.ops = &clk_factor_ops;
149 init.flags = flags;
150 init.parent_names = &parent_name;
151 init.num_parents = 1;
152
153 clk = clk_register(NULL, &factor->hw);
154 if (IS_ERR_OR_NULL(clk))
155 kfree(factor);
156
157 return clk;
158}