blob: e29d006ab85e9d39cfbda5cdc86bf7cf6e3ee9e1 [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;
Chao Xie61256132014-10-31 10:13:42 +080032 spinlock_t *lock;
Chao Xie6b63f022012-08-20 02:55:11 +000033};
34
35static long clk_factor_round_rate(struct clk_hw *hw, unsigned long drate,
36 unsigned long *prate)
37{
Chao Xie2bd1e252014-10-31 10:13:41 +080038 struct mmp_clk_factor *factor = to_clk_factor(hw);
Chao Xie6b63f022012-08-20 02:55:11 +000039 unsigned long rate = 0, prev_rate;
40 int i;
41
42 for (i = 0; i < factor->ftbl_cnt; i++) {
43 prev_rate = rate;
Chao Xiec45693a62014-01-23 10:47:41 +080044 rate = (((*prate / 10000) * factor->ftbl[i].den) /
45 (factor->ftbl[i].num * factor->masks->factor)) * 10000;
Chao Xie6b63f022012-08-20 02:55:11 +000046 if (rate > drate)
47 break;
48 }
Chao Xie5d26c152014-01-23 10:47:42 +080049 if ((i == 0) || (i == factor->ftbl_cnt)) {
Chao Xie6b63f022012-08-20 02:55:11 +000050 return rate;
Chao Xie5d26c152014-01-23 10:47:42 +080051 } else {
52 if ((drate - prev_rate) > (rate - drate))
53 return rate;
54 else
55 return prev_rate;
56 }
Chao Xie6b63f022012-08-20 02:55:11 +000057}
58
59static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
60 unsigned long parent_rate)
61{
Chao Xie2bd1e252014-10-31 10:13:41 +080062 struct mmp_clk_factor *factor = to_clk_factor(hw);
63 struct mmp_clk_factor_masks *masks = factor->masks;
Chao Xie6b63f022012-08-20 02:55:11 +000064 unsigned int val, num, den;
65
66 val = readl_relaxed(factor->base);
67
68 /* calculate numerator */
69 num = (val >> masks->num_shift) & masks->num_mask;
70
71 /* calculate denominator */
Chao Xie7433ab42014-01-23 10:47:40 +080072 den = (val >> masks->den_shift) & masks->den_mask;
Chao Xie6b63f022012-08-20 02:55:11 +000073
74 if (!den)
75 return 0;
76
77 return (((parent_rate / 10000) * den) /
78 (num * factor->masks->factor)) * 10000;
79}
80
81/* Configures new clock rate*/
82static int clk_factor_set_rate(struct clk_hw *hw, unsigned long drate,
83 unsigned long prate)
84{
Chao Xie2bd1e252014-10-31 10:13:41 +080085 struct mmp_clk_factor *factor = to_clk_factor(hw);
86 struct mmp_clk_factor_masks *masks = factor->masks;
Chao Xie6b63f022012-08-20 02:55:11 +000087 int i;
88 unsigned long val;
89 unsigned long prev_rate, rate = 0;
Chao Xie61256132014-10-31 10:13:42 +080090 unsigned long flags = 0;
Chao Xie6b63f022012-08-20 02:55:11 +000091
92 for (i = 0; i < factor->ftbl_cnt; i++) {
93 prev_rate = rate;
Chao Xiec45693a62014-01-23 10:47:41 +080094 rate = (((prate / 10000) * factor->ftbl[i].den) /
95 (factor->ftbl[i].num * factor->masks->factor)) * 10000;
Chao Xie6b63f022012-08-20 02:55:11 +000096 if (rate > drate)
97 break;
98 }
99 if (i > 0)
100 i--;
101
Chao Xie61256132014-10-31 10:13:42 +0800102 if (factor->lock)
103 spin_lock_irqsave(factor->lock, flags);
104
Chao Xie6b63f022012-08-20 02:55:11 +0000105 val = readl_relaxed(factor->base);
106
107 val &= ~(masks->num_mask << masks->num_shift);
108 val |= (factor->ftbl[i].num & masks->num_mask) << masks->num_shift;
109
110 val &= ~(masks->den_mask << masks->den_shift);
111 val |= (factor->ftbl[i].den & masks->den_mask) << masks->den_shift;
112
113 writel_relaxed(val, factor->base);
114
Chao Xie61256132014-10-31 10:13:42 +0800115 if (factor->lock)
116 spin_unlock_irqrestore(factor->lock, flags);
117
Chao Xie6b63f022012-08-20 02:55:11 +0000118 return 0;
119}
120
121static struct clk_ops clk_factor_ops = {
122 .recalc_rate = clk_factor_recalc_rate,
123 .round_rate = clk_factor_round_rate,
124 .set_rate = clk_factor_set_rate,
125};
126
127struct clk *mmp_clk_register_factor(const char *name, const char *parent_name,
128 unsigned long flags, void __iomem *base,
Chao Xie2bd1e252014-10-31 10:13:41 +0800129 struct mmp_clk_factor_masks *masks,
130 struct mmp_clk_factor_tbl *ftbl,
Chao Xie61256132014-10-31 10:13:42 +0800131 unsigned int ftbl_cnt, spinlock_t *lock)
Chao Xie6b63f022012-08-20 02:55:11 +0000132{
Chao Xie2bd1e252014-10-31 10:13:41 +0800133 struct mmp_clk_factor *factor;
Chao Xie6b63f022012-08-20 02:55:11 +0000134 struct clk_init_data init;
135 struct clk *clk;
136
137 if (!masks) {
138 pr_err("%s: must pass a clk_factor_mask\n", __func__);
139 return ERR_PTR(-EINVAL);
140 }
141
142 factor = kzalloc(sizeof(*factor), GFP_KERNEL);
143 if (!factor) {
144 pr_err("%s: could not allocate factor clk\n", __func__);
145 return ERR_PTR(-ENOMEM);
146 }
147
148 /* struct clk_aux assignments */
149 factor->base = base;
150 factor->masks = masks;
151 factor->ftbl = ftbl;
152 factor->ftbl_cnt = ftbl_cnt;
153 factor->hw.init = &init;
Chao Xie61256132014-10-31 10:13:42 +0800154 factor->lock = lock;
Chao Xie6b63f022012-08-20 02:55:11 +0000155
156 init.name = name;
157 init.ops = &clk_factor_ops;
158 init.flags = flags;
159 init.parent_names = &parent_name;
160 init.num_parents = 1;
161
162 clk = clk_register(NULL, &factor->hw);
163 if (IS_ERR_OR_NULL(clk))
164 kfree(factor);
165
166 return clk;
167}