]>
Commit | Line | Data |
---|---|---|
4f728b5d MR |
1 | /* |
2 | * Copyright (C) 2016 Maxime Ripard | |
3 | * Maxime Ripard <maxime.ripard@free-electrons.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU General Public License as | |
7 | * published by the Free Software Foundation; either version 2 of | |
8 | * the License, or (at your option) any later version. | |
9 | */ | |
10 | ||
11 | #include <linux/clk-provider.h> | |
4f728b5d MR |
12 | |
13 | #include "ccu_gate.h" | |
14 | #include "ccu_nkmp.h" | |
15 | ||
16 | struct _ccu_nkmp { | |
6e0d50da MR |
17 | unsigned long n, min_n, max_n; |
18 | unsigned long k, min_k, max_k; | |
19 | unsigned long m, min_m, max_m; | |
20 | unsigned long p, min_p, max_p; | |
4f728b5d MR |
21 | }; |
22 | ||
23 | static void ccu_nkmp_find_best(unsigned long parent, unsigned long rate, | |
24 | struct _ccu_nkmp *nkmp) | |
25 | { | |
26 | unsigned long best_rate = 0; | |
27 | unsigned long best_n = 0, best_k = 0, best_m = 0, best_p = 0; | |
28 | unsigned long _n, _k, _m, _p; | |
29 | ||
6e0d50da MR |
30 | for (_k = nkmp->min_k; _k <= nkmp->max_k; _k++) { |
31 | for (_n = nkmp->min_n; _n <= nkmp->max_n; _n++) { | |
32 | for (_m = nkmp->min_m; _m <= nkmp->max_m; _m++) { | |
33 | for (_p = nkmp->min_p; _p <= nkmp->max_p; _p <<= 1) { | |
ee28648c MR |
34 | unsigned long tmp_rate; |
35 | ||
36 | tmp_rate = parent * _n * _k / (_m * _p); | |
37 | ||
38 | if (tmp_rate > rate) | |
39 | continue; | |
40 | ||
41 | if ((rate - tmp_rate) < (rate - best_rate)) { | |
42 | best_rate = tmp_rate; | |
43 | best_n = _n; | |
44 | best_k = _k; | |
45 | best_m = _m; | |
46 | best_p = _p; | |
47 | } | |
48 | } | |
4f728b5d MR |
49 | } |
50 | } | |
51 | } | |
52 | ||
53 | nkmp->n = best_n; | |
54 | nkmp->k = best_k; | |
55 | nkmp->m = best_m; | |
56 | nkmp->p = best_p; | |
57 | } | |
58 | ||
59 | static void ccu_nkmp_disable(struct clk_hw *hw) | |
60 | { | |
61 | struct ccu_nkmp *nkmp = hw_to_ccu_nkmp(hw); | |
62 | ||
63 | return ccu_gate_helper_disable(&nkmp->common, nkmp->enable); | |
64 | } | |
65 | ||
66 | static int ccu_nkmp_enable(struct clk_hw *hw) | |
67 | { | |
68 | struct ccu_nkmp *nkmp = hw_to_ccu_nkmp(hw); | |
69 | ||
70 | return ccu_gate_helper_enable(&nkmp->common, nkmp->enable); | |
71 | } | |
72 | ||
73 | static int ccu_nkmp_is_enabled(struct clk_hw *hw) | |
74 | { | |
75 | struct ccu_nkmp *nkmp = hw_to_ccu_nkmp(hw); | |
76 | ||
77 | return ccu_gate_helper_is_enabled(&nkmp->common, nkmp->enable); | |
78 | } | |
79 | ||
80 | static unsigned long ccu_nkmp_recalc_rate(struct clk_hw *hw, | |
81 | unsigned long parent_rate) | |
82 | { | |
83 | struct ccu_nkmp *nkmp = hw_to_ccu_nkmp(hw); | |
84 | unsigned long n, m, k, p; | |
85 | u32 reg; | |
86 | ||
87 | reg = readl(nkmp->common.base + nkmp->common.reg); | |
88 | ||
89 | n = reg >> nkmp->n.shift; | |
90 | n &= (1 << nkmp->n.width) - 1; | |
e66f81bb MR |
91 | n += nkmp->n.offset; |
92 | if (!n) | |
93 | n++; | |
4f728b5d MR |
94 | |
95 | k = reg >> nkmp->k.shift; | |
96 | k &= (1 << nkmp->k.width) - 1; | |
e66f81bb MR |
97 | k += nkmp->k.offset; |
98 | if (!k) | |
99 | k++; | |
4f728b5d MR |
100 | |
101 | m = reg >> nkmp->m.shift; | |
102 | m &= (1 << nkmp->m.width) - 1; | |
e66f81bb MR |
103 | m += nkmp->m.offset; |
104 | if (!m) | |
105 | m++; | |
4f728b5d MR |
106 | |
107 | p = reg >> nkmp->p.shift; | |
108 | p &= (1 << nkmp->p.width) - 1; | |
109 | ||
b467e08a | 110 | return (parent_rate * n * k >> p) / m; |
4f728b5d MR |
111 | } |
112 | ||
113 | static long ccu_nkmp_round_rate(struct clk_hw *hw, unsigned long rate, | |
114 | unsigned long *parent_rate) | |
115 | { | |
116 | struct ccu_nkmp *nkmp = hw_to_ccu_nkmp(hw); | |
117 | struct _ccu_nkmp _nkmp; | |
118 | ||
4162c5ce | 119 | _nkmp.min_n = nkmp->n.min ?: 1; |
0c3c8e13 | 120 | _nkmp.max_n = nkmp->n.max ?: 1 << nkmp->n.width; |
4162c5ce | 121 | _nkmp.min_k = nkmp->k.min ?: 1; |
0c3c8e13 | 122 | _nkmp.max_k = nkmp->k.max ?: 1 << nkmp->k.width; |
6e0d50da | 123 | _nkmp.min_m = 1; |
87ba9e59 | 124 | _nkmp.max_m = nkmp->m.max ?: 1 << nkmp->m.width; |
6e0d50da | 125 | _nkmp.min_p = 1; |
87ba9e59 | 126 | _nkmp.max_p = nkmp->p.max ?: 1 << ((1 << nkmp->p.width) - 1); |
4f728b5d | 127 | |
87ba9e59 | 128 | ccu_nkmp_find_best(*parent_rate, rate, &_nkmp); |
4f728b5d | 129 | |
87ba9e59 | 130 | return *parent_rate * _nkmp.n * _nkmp.k / (_nkmp.m * _nkmp.p); |
4f728b5d MR |
131 | } |
132 | ||
133 | static int ccu_nkmp_set_rate(struct clk_hw *hw, unsigned long rate, | |
134 | unsigned long parent_rate) | |
135 | { | |
136 | struct ccu_nkmp *nkmp = hw_to_ccu_nkmp(hw); | |
137 | struct _ccu_nkmp _nkmp; | |
138 | unsigned long flags; | |
139 | u32 reg; | |
140 | ||
95ad8ed9 | 141 | _nkmp.min_n = nkmp->n.min ?: 1; |
0c3c8e13 | 142 | _nkmp.max_n = nkmp->n.max ?: 1 << nkmp->n.width; |
95ad8ed9 | 143 | _nkmp.min_k = nkmp->k.min ?: 1; |
0c3c8e13 | 144 | _nkmp.max_k = nkmp->k.max ?: 1 << nkmp->k.width; |
6e0d50da | 145 | _nkmp.min_m = 1; |
87ba9e59 | 146 | _nkmp.max_m = nkmp->m.max ?: 1 << nkmp->m.width; |
6e0d50da | 147 | _nkmp.min_p = 1; |
87ba9e59 | 148 | _nkmp.max_p = nkmp->p.max ?: 1 << ((1 << nkmp->p.width) - 1); |
4f728b5d MR |
149 | |
150 | ccu_nkmp_find_best(parent_rate, rate, &_nkmp); | |
151 | ||
152 | spin_lock_irqsave(nkmp->common.lock, flags); | |
153 | ||
154 | reg = readl(nkmp->common.base + nkmp->common.reg); | |
155 | reg &= ~GENMASK(nkmp->n.width + nkmp->n.shift - 1, nkmp->n.shift); | |
156 | reg &= ~GENMASK(nkmp->k.width + nkmp->k.shift - 1, nkmp->k.shift); | |
157 | reg &= ~GENMASK(nkmp->m.width + nkmp->m.shift - 1, nkmp->m.shift); | |
158 | reg &= ~GENMASK(nkmp->p.width + nkmp->p.shift - 1, nkmp->p.shift); | |
159 | ||
e66f81bb MR |
160 | reg |= (_nkmp.n - nkmp->n.offset) << nkmp->n.shift; |
161 | reg |= (_nkmp.k - nkmp->k.offset) << nkmp->k.shift; | |
162 | reg |= (_nkmp.m - nkmp->m.offset) << nkmp->m.shift; | |
87ba9e59 | 163 | reg |= ilog2(_nkmp.p) << nkmp->p.shift; |
4f728b5d MR |
164 | |
165 | writel(reg, nkmp->common.base + nkmp->common.reg); | |
166 | ||
167 | spin_unlock_irqrestore(nkmp->common.lock, flags); | |
168 | ||
169 | ccu_helper_wait_for_lock(&nkmp->common, nkmp->lock); | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
174 | const struct clk_ops ccu_nkmp_ops = { | |
175 | .disable = ccu_nkmp_disable, | |
176 | .enable = ccu_nkmp_enable, | |
177 | .is_enabled = ccu_nkmp_is_enabled, | |
178 | ||
179 | .recalc_rate = ccu_nkmp_recalc_rate, | |
180 | .round_rate = ccu_nkmp_round_rate, | |
181 | .set_rate = ccu_nkmp_set_rate, | |
182 | }; |