clk: at91: clk-generated: check best_rate against ranges
[linux-2.6-microblaze.git] / drivers / clk / at91 / clk-generated.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (C) 2015 Atmel Corporation,
4  *                     Nicolas Ferre <nicolas.ferre@atmel.com>
5  *
6  * Based on clk-programmable & clk-peripheral drivers by Boris BREZILLON.
7  */
8
9 #include <linux/bitfield.h>
10 #include <linux/clk-provider.h>
11 #include <linux/clkdev.h>
12 #include <linux/clk/at91_pmc.h>
13 #include <linux/of.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/regmap.h>
16
17 #include "pmc.h"
18
19 #define GENERATED_MAX_DIV       255
20
21 #define GCK_INDEX_DT_AUDIO_PLL  5
22
23 struct clk_generated {
24         struct clk_hw hw;
25         struct regmap *regmap;
26         struct clk_range range;
27         spinlock_t *lock;
28         u32 id;
29         u32 gckdiv;
30         const struct clk_pcr_layout *layout;
31         u8 parent_id;
32         bool audio_pll_allowed;
33 };
34
35 #define to_clk_generated(hw) \
36         container_of(hw, struct clk_generated, hw)
37
38 static int clk_generated_enable(struct clk_hw *hw)
39 {
40         struct clk_generated *gck = to_clk_generated(hw);
41         unsigned long flags;
42
43         pr_debug("GCLK: %s, gckdiv = %d, parent id = %d\n",
44                  __func__, gck->gckdiv, gck->parent_id);
45
46         spin_lock_irqsave(gck->lock, flags);
47         regmap_write(gck->regmap, gck->layout->offset,
48                      (gck->id & gck->layout->pid_mask));
49         regmap_update_bits(gck->regmap, gck->layout->offset,
50                            AT91_PMC_PCR_GCKDIV_MASK | gck->layout->gckcss_mask |
51                            gck->layout->cmd | AT91_PMC_PCR_GCKEN,
52                            field_prep(gck->layout->gckcss_mask, gck->parent_id) |
53                            gck->layout->cmd |
54                            FIELD_PREP(AT91_PMC_PCR_GCKDIV_MASK, gck->gckdiv) |
55                            AT91_PMC_PCR_GCKEN);
56         spin_unlock_irqrestore(gck->lock, flags);
57         return 0;
58 }
59
60 static void clk_generated_disable(struct clk_hw *hw)
61 {
62         struct clk_generated *gck = to_clk_generated(hw);
63         unsigned long flags;
64
65         spin_lock_irqsave(gck->lock, flags);
66         regmap_write(gck->regmap, gck->layout->offset,
67                      (gck->id & gck->layout->pid_mask));
68         regmap_update_bits(gck->regmap, gck->layout->offset,
69                            gck->layout->cmd | AT91_PMC_PCR_GCKEN,
70                            gck->layout->cmd);
71         spin_unlock_irqrestore(gck->lock, flags);
72 }
73
74 static int clk_generated_is_enabled(struct clk_hw *hw)
75 {
76         struct clk_generated *gck = to_clk_generated(hw);
77         unsigned long flags;
78         unsigned int status;
79
80         spin_lock_irqsave(gck->lock, flags);
81         regmap_write(gck->regmap, gck->layout->offset,
82                      (gck->id & gck->layout->pid_mask));
83         regmap_read(gck->regmap, gck->layout->offset, &status);
84         spin_unlock_irqrestore(gck->lock, flags);
85
86         return status & AT91_PMC_PCR_GCKEN ? 1 : 0;
87 }
88
89 static unsigned long
90 clk_generated_recalc_rate(struct clk_hw *hw,
91                           unsigned long parent_rate)
92 {
93         struct clk_generated *gck = to_clk_generated(hw);
94
95         return DIV_ROUND_CLOSEST(parent_rate, gck->gckdiv + 1);
96 }
97
98 static void clk_generated_best_diff(struct clk_rate_request *req,
99                                     struct clk_hw *parent,
100                                     unsigned long parent_rate, u32 div,
101                                     int *best_diff, long *best_rate)
102 {
103         unsigned long tmp_rate;
104         int tmp_diff;
105
106         if (!div)
107                 tmp_rate = parent_rate;
108         else
109                 tmp_rate = parent_rate / div;
110         tmp_diff = abs(req->rate - tmp_rate);
111
112         if (*best_diff < 0 || *best_diff > tmp_diff) {
113                 *best_rate = tmp_rate;
114                 *best_diff = tmp_diff;
115                 req->best_parent_rate = parent_rate;
116                 req->best_parent_hw = parent;
117         }
118 }
119
120 static int clk_generated_determine_rate(struct clk_hw *hw,
121                                         struct clk_rate_request *req)
122 {
123         struct clk_generated *gck = to_clk_generated(hw);
124         struct clk_hw *parent = NULL;
125         struct clk_rate_request req_parent = *req;
126         long best_rate = -EINVAL;
127         unsigned long min_rate, parent_rate;
128         int best_diff = -1;
129         int i;
130         u32 div;
131
132         for (i = 0; i < clk_hw_get_num_parents(hw) - 1; i++) {
133                 parent = clk_hw_get_parent_by_index(hw, i);
134                 if (!parent)
135                         continue;
136
137                 parent_rate = clk_hw_get_rate(parent);
138                 min_rate = DIV_ROUND_CLOSEST(parent_rate, GENERATED_MAX_DIV + 1);
139                 if (!parent_rate ||
140                     (gck->range.max && min_rate > gck->range.max))
141                         continue;
142
143                 div = DIV_ROUND_CLOSEST(parent_rate, req->rate);
144                 if (div > GENERATED_MAX_DIV + 1)
145                         div = GENERATED_MAX_DIV + 1;
146
147                 clk_generated_best_diff(req, parent, parent_rate, div,
148                                         &best_diff, &best_rate);
149
150                 if (!best_diff)
151                         break;
152         }
153
154         /*
155          * The audio_pll rate can be modified, unlike the five others clocks
156          * that should never be altered.
157          * The audio_pll can technically be used by multiple consumers. However,
158          * with the rate locking, the first consumer to enable to clock will be
159          * the one definitely setting the rate of the clock.
160          * Since audio IPs are most likely to request the same rate, we enforce
161          * that the only clks able to modify gck rate are those of audio IPs.
162          */
163
164         if (!gck->audio_pll_allowed)
165                 goto end;
166
167         parent = clk_hw_get_parent_by_index(hw, GCK_INDEX_DT_AUDIO_PLL);
168         if (!parent)
169                 goto end;
170
171         for (div = 1; div < GENERATED_MAX_DIV + 2; div++) {
172                 req_parent.rate = req->rate * div;
173                 if (__clk_determine_rate(parent, &req_parent))
174                         continue;
175                 clk_generated_best_diff(req, parent, req_parent.rate, div,
176                                         &best_diff, &best_rate);
177
178                 if (!best_diff)
179                         break;
180         }
181
182 end:
183         pr_debug("GCLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
184                  __func__, best_rate,
185                  __clk_get_name((req->best_parent_hw)->clk),
186                  req->best_parent_rate);
187
188         if (best_rate < 0 || (gck->range.max && best_rate > gck->range.max))
189                 return -EINVAL;
190
191         req->rate = best_rate;
192         return 0;
193 }
194
195 /* No modification of hardware as we have the flag CLK_SET_PARENT_GATE set */
196 static int clk_generated_set_parent(struct clk_hw *hw, u8 index)
197 {
198         struct clk_generated *gck = to_clk_generated(hw);
199
200         if (index >= clk_hw_get_num_parents(hw))
201                 return -EINVAL;
202
203         gck->parent_id = index;
204         return 0;
205 }
206
207 static u8 clk_generated_get_parent(struct clk_hw *hw)
208 {
209         struct clk_generated *gck = to_clk_generated(hw);
210
211         return gck->parent_id;
212 }
213
214 /* No modification of hardware as we have the flag CLK_SET_RATE_GATE set */
215 static int clk_generated_set_rate(struct clk_hw *hw,
216                                   unsigned long rate,
217                                   unsigned long parent_rate)
218 {
219         struct clk_generated *gck = to_clk_generated(hw);
220         u32 div;
221
222         if (!rate)
223                 return -EINVAL;
224
225         if (gck->range.max && rate > gck->range.max)
226                 return -EINVAL;
227
228         div = DIV_ROUND_CLOSEST(parent_rate, rate);
229         if (div > GENERATED_MAX_DIV + 1 || !div)
230                 return -EINVAL;
231
232         gck->gckdiv = div - 1;
233         return 0;
234 }
235
236 static const struct clk_ops generated_ops = {
237         .enable = clk_generated_enable,
238         .disable = clk_generated_disable,
239         .is_enabled = clk_generated_is_enabled,
240         .recalc_rate = clk_generated_recalc_rate,
241         .determine_rate = clk_generated_determine_rate,
242         .get_parent = clk_generated_get_parent,
243         .set_parent = clk_generated_set_parent,
244         .set_rate = clk_generated_set_rate,
245 };
246
247 /**
248  * clk_generated_startup - Initialize a given clock to its default parent and
249  * divisor parameter.
250  *
251  * @gck:        Generated clock to set the startup parameters for.
252  *
253  * Take parameters from the hardware and update local clock configuration
254  * accordingly.
255  */
256 static void clk_generated_startup(struct clk_generated *gck)
257 {
258         u32 tmp;
259         unsigned long flags;
260
261         spin_lock_irqsave(gck->lock, flags);
262         regmap_write(gck->regmap, gck->layout->offset,
263                      (gck->id & gck->layout->pid_mask));
264         regmap_read(gck->regmap, gck->layout->offset, &tmp);
265         spin_unlock_irqrestore(gck->lock, flags);
266
267         gck->parent_id = field_get(gck->layout->gckcss_mask, tmp);
268         gck->gckdiv = FIELD_GET(AT91_PMC_PCR_GCKDIV_MASK, tmp);
269 }
270
271 struct clk_hw * __init
272 at91_clk_register_generated(struct regmap *regmap, spinlock_t *lock,
273                             const struct clk_pcr_layout *layout,
274                             const char *name, const char **parent_names,
275                             u8 num_parents, u8 id, bool pll_audio,
276                             const struct clk_range *range)
277 {
278         struct clk_generated *gck;
279         struct clk_init_data init;
280         struct clk_hw *hw;
281         int ret;
282
283         gck = kzalloc(sizeof(*gck), GFP_KERNEL);
284         if (!gck)
285                 return ERR_PTR(-ENOMEM);
286
287         init.name = name;
288         init.ops = &generated_ops;
289         init.parent_names = parent_names;
290         init.num_parents = num_parents;
291         init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
292                 CLK_SET_RATE_PARENT;
293
294         gck->id = id;
295         gck->hw.init = &init;
296         gck->regmap = regmap;
297         gck->lock = lock;
298         gck->range = *range;
299         gck->audio_pll_allowed = pll_audio;
300         gck->layout = layout;
301
302         clk_generated_startup(gck);
303         hw = &gck->hw;
304         ret = clk_hw_register(NULL, &gck->hw);
305         if (ret) {
306                 kfree(gck);
307                 hw = ERR_PTR(ret);
308         } else {
309                 pmc_register_id(id);
310         }
311
312         return hw;
313 }