1 // SPDX-License-Identifier: GPL-2.0-or-later
6 #include <linux/bitops.h>
7 #include <linux/clk-provider.h>
8 #include <linux/clkdev.h>
9 #include <linux/clk/at91_pmc.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/regmap.h>
16 DEFINE_SPINLOCK(pmc_pcr_lock);
18 #define PERIPHERAL_ID_MIN 2
19 #define PERIPHERAL_ID_MAX 31
20 #define PERIPHERAL_MASK(id) (1 << ((id) & PERIPHERAL_ID_MAX))
22 #define PERIPHERAL_MAX_SHIFT 3
24 struct clk_peripheral {
26 struct regmap *regmap;
30 #define to_clk_peripheral(hw) container_of(hw, struct clk_peripheral, hw)
32 struct clk_sam9x5_peripheral {
34 struct regmap *regmap;
35 struct clk_range range;
39 const struct clk_pcr_layout *layout;
40 struct at91_clk_pms pms;
45 #define to_clk_sam9x5_peripheral(hw) \
46 container_of(hw, struct clk_sam9x5_peripheral, hw)
48 static int clk_peripheral_enable(struct clk_hw *hw)
50 struct clk_peripheral *periph = to_clk_peripheral(hw);
51 int offset = AT91_PMC_PCER;
54 if (id < PERIPHERAL_ID_MIN)
56 if (id > PERIPHERAL_ID_MAX)
57 offset = AT91_PMC_PCER1;
58 regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
63 static void clk_peripheral_disable(struct clk_hw *hw)
65 struct clk_peripheral *periph = to_clk_peripheral(hw);
66 int offset = AT91_PMC_PCDR;
69 if (id < PERIPHERAL_ID_MIN)
71 if (id > PERIPHERAL_ID_MAX)
72 offset = AT91_PMC_PCDR1;
73 regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
76 static int clk_peripheral_is_enabled(struct clk_hw *hw)
78 struct clk_peripheral *periph = to_clk_peripheral(hw);
79 int offset = AT91_PMC_PCSR;
83 if (id < PERIPHERAL_ID_MIN)
85 if (id > PERIPHERAL_ID_MAX)
86 offset = AT91_PMC_PCSR1;
87 regmap_read(periph->regmap, offset, &status);
89 return status & PERIPHERAL_MASK(id) ? 1 : 0;
92 static const struct clk_ops peripheral_ops = {
93 .enable = clk_peripheral_enable,
94 .disable = clk_peripheral_disable,
95 .is_enabled = clk_peripheral_is_enabled,
98 struct clk_hw * __init
99 at91_clk_register_peripheral(struct regmap *regmap, const char *name,
100 const char *parent_name, u32 id)
102 struct clk_peripheral *periph;
103 struct clk_init_data init;
107 if (!name || !parent_name || id > PERIPHERAL_ID_MAX)
108 return ERR_PTR(-EINVAL);
110 periph = kzalloc(sizeof(*periph), GFP_KERNEL);
112 return ERR_PTR(-ENOMEM);
115 init.ops = &peripheral_ops;
116 init.parent_names = &parent_name;
117 init.num_parents = 1;
121 periph->hw.init = &init;
122 periph->regmap = regmap;
125 ret = clk_hw_register(NULL, &periph->hw);
134 static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral *periph)
136 struct clk_hw *parent;
137 unsigned long parent_rate;
140 if (!periph->auto_div)
143 if (periph->range.max) {
144 parent = clk_hw_get_parent_by_index(&periph->hw, 0);
145 parent_rate = clk_hw_get_rate(parent);
149 for (; shift < PERIPHERAL_MAX_SHIFT; shift++) {
150 if (parent_rate >> shift <= periph->range.max)
155 periph->auto_div = false;
159 static int clk_sam9x5_peripheral_set(struct clk_sam9x5_peripheral *periph,
163 unsigned int enable = status ? AT91_PMC_PCR_EN : 0;
165 if (periph->id < PERIPHERAL_ID_MIN)
168 spin_lock_irqsave(periph->lock, flags);
169 regmap_write(periph->regmap, periph->layout->offset,
170 (periph->id & periph->layout->pid_mask));
171 regmap_update_bits(periph->regmap, periph->layout->offset,
172 periph->layout->div_mask | periph->layout->cmd |
174 field_prep(periph->layout->div_mask, periph->div) |
175 periph->layout->cmd | enable);
176 spin_unlock_irqrestore(periph->lock, flags);
181 static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
183 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
185 return clk_sam9x5_peripheral_set(periph, 1);
188 static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
190 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
193 if (periph->id < PERIPHERAL_ID_MIN)
196 spin_lock_irqsave(periph->lock, flags);
197 regmap_write(periph->regmap, periph->layout->offset,
198 (periph->id & periph->layout->pid_mask));
199 regmap_update_bits(periph->regmap, periph->layout->offset,
200 AT91_PMC_PCR_EN | periph->layout->cmd,
201 periph->layout->cmd);
202 spin_unlock_irqrestore(periph->lock, flags);
205 static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
207 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
211 if (periph->id < PERIPHERAL_ID_MIN)
214 spin_lock_irqsave(periph->lock, flags);
215 regmap_write(periph->regmap, periph->layout->offset,
216 (periph->id & periph->layout->pid_mask));
217 regmap_read(periph->regmap, periph->layout->offset, &status);
218 spin_unlock_irqrestore(periph->lock, flags);
220 return !!(status & AT91_PMC_PCR_EN);
224 clk_sam9x5_peripheral_recalc_rate(struct clk_hw *hw,
225 unsigned long parent_rate)
227 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
231 if (periph->id < PERIPHERAL_ID_MIN)
234 spin_lock_irqsave(periph->lock, flags);
235 regmap_write(periph->regmap, periph->layout->offset,
236 (periph->id & periph->layout->pid_mask));
237 regmap_read(periph->regmap, periph->layout->offset, &status);
238 spin_unlock_irqrestore(periph->lock, flags);
240 if (status & AT91_PMC_PCR_EN) {
241 periph->div = field_get(periph->layout->div_mask, status);
242 periph->auto_div = false;
244 clk_sam9x5_peripheral_autodiv(periph);
247 return parent_rate >> periph->div;
250 static void clk_sam9x5_peripheral_best_diff(struct clk_rate_request *req,
251 struct clk_hw *parent,
252 unsigned long parent_rate,
253 u32 shift, long *best_diff,
256 unsigned long tmp_rate = parent_rate >> shift;
257 unsigned long tmp_diff = abs(req->rate - tmp_rate);
259 if (*best_diff < 0 || *best_diff >= tmp_diff) {
260 *best_rate = tmp_rate;
261 *best_diff = tmp_diff;
262 req->best_parent_rate = parent_rate;
263 req->best_parent_hw = parent;
267 static int clk_sam9x5_peripheral_determine_rate(struct clk_hw *hw,
268 struct clk_rate_request *req)
270 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
271 struct clk_hw *parent = clk_hw_get_parent(hw);
272 struct clk_rate_request req_parent = *req;
273 unsigned long parent_rate = clk_hw_get_rate(parent);
274 unsigned long tmp_rate;
275 long best_rate = LONG_MIN;
276 long best_diff = LONG_MIN;
279 if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
282 /* Fist step: check the available dividers. */
283 for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
284 tmp_rate = parent_rate >> shift;
286 if (periph->range.max && tmp_rate > periph->range.max)
289 clk_sam9x5_peripheral_best_diff(req, parent, parent_rate,
290 shift, &best_diff, &best_rate);
292 if (!best_diff || best_rate <= req->rate)
296 if (periph->chg_pid < 0)
299 /* Step two: try to request rate from parent. */
300 parent = clk_hw_get_parent_by_index(hw, periph->chg_pid);
304 for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
305 req_parent.rate = req->rate << shift;
307 if (__clk_determine_rate(parent, &req_parent))
310 clk_sam9x5_peripheral_best_diff(req, parent, req_parent.rate,
311 shift, &best_diff, &best_rate);
318 (periph->range.max && best_rate > periph->range.max))
321 pr_debug("PCK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
323 __clk_get_name((req->best_parent_hw)->clk),
324 req->best_parent_rate);
326 req->rate = best_rate;
331 static long clk_sam9x5_peripheral_round_rate(struct clk_hw *hw,
333 unsigned long *parent_rate)
336 unsigned long best_rate;
337 unsigned long best_diff;
338 unsigned long cur_rate = *parent_rate;
339 unsigned long cur_diff;
340 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
342 if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
345 if (periph->range.max) {
346 for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
347 cur_rate = *parent_rate >> shift;
348 if (cur_rate <= periph->range.max)
353 if (rate >= cur_rate)
356 best_diff = cur_rate - rate;
357 best_rate = cur_rate;
358 for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
359 cur_rate = *parent_rate >> shift;
361 cur_diff = rate - cur_rate;
363 cur_diff = cur_rate - rate;
365 if (cur_diff < best_diff) {
366 best_diff = cur_diff;
367 best_rate = cur_rate;
370 if (!best_diff || cur_rate < rate)
377 static int clk_sam9x5_peripheral_set_rate(struct clk_hw *hw,
379 unsigned long parent_rate)
382 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
383 if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max) {
384 if (parent_rate == rate)
390 if (periph->range.max && rate > periph->range.max)
393 for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
394 if (parent_rate >> shift == rate) {
395 periph->auto_div = false;
404 static int clk_sam9x5_peripheral_save_context(struct clk_hw *hw)
406 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
408 periph->pms.status = clk_sam9x5_peripheral_is_enabled(hw);
413 static void clk_sam9x5_peripheral_restore_context(struct clk_hw *hw)
415 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
417 if (periph->pms.status)
418 clk_sam9x5_peripheral_set(periph, periph->pms.status);
421 static const struct clk_ops sam9x5_peripheral_ops = {
422 .enable = clk_sam9x5_peripheral_enable,
423 .disable = clk_sam9x5_peripheral_disable,
424 .is_enabled = clk_sam9x5_peripheral_is_enabled,
425 .recalc_rate = clk_sam9x5_peripheral_recalc_rate,
426 .round_rate = clk_sam9x5_peripheral_round_rate,
427 .set_rate = clk_sam9x5_peripheral_set_rate,
428 .save_context = clk_sam9x5_peripheral_save_context,
429 .restore_context = clk_sam9x5_peripheral_restore_context,
432 static const struct clk_ops sam9x5_peripheral_chg_ops = {
433 .enable = clk_sam9x5_peripheral_enable,
434 .disable = clk_sam9x5_peripheral_disable,
435 .is_enabled = clk_sam9x5_peripheral_is_enabled,
436 .recalc_rate = clk_sam9x5_peripheral_recalc_rate,
437 .determine_rate = clk_sam9x5_peripheral_determine_rate,
438 .set_rate = clk_sam9x5_peripheral_set_rate,
439 .save_context = clk_sam9x5_peripheral_save_context,
440 .restore_context = clk_sam9x5_peripheral_restore_context,
443 struct clk_hw * __init
444 at91_clk_register_sam9x5_peripheral(struct regmap *regmap, spinlock_t *lock,
445 const struct clk_pcr_layout *layout,
446 const char *name, const char *parent_name,
447 u32 id, const struct clk_range *range,
450 struct clk_sam9x5_peripheral *periph;
451 struct clk_init_data init;
455 if (!name || !parent_name)
456 return ERR_PTR(-EINVAL);
458 periph = kzalloc(sizeof(*periph), GFP_KERNEL);
460 return ERR_PTR(-ENOMEM);
463 init.parent_names = &parent_name;
464 init.num_parents = 1;
467 init.ops = &sam9x5_peripheral_ops;
469 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
471 init.ops = &sam9x5_peripheral_chg_ops;
475 periph->hw.init = &init;
477 periph->regmap = regmap;
479 if (layout->div_mask)
480 periph->auto_div = true;
481 periph->layout = layout;
482 periph->range = *range;
483 periph->chg_pid = chg_pid;
486 ret = clk_hw_register(NULL, &periph->hw);
491 clk_sam9x5_peripheral_autodiv(periph);