12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
26114067eSBoris BREZILLON /*
36114067eSBoris BREZILLON * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
46114067eSBoris BREZILLON */
56114067eSBoris BREZILLON
6cb4f4949SAlexandre Belloni #include <linux/bitops.h>
76114067eSBoris BREZILLON #include <linux/clk-provider.h>
86114067eSBoris BREZILLON #include <linux/clkdev.h>
96114067eSBoris BREZILLON #include <linux/clk/at91_pmc.h>
106114067eSBoris BREZILLON #include <linux/of.h>
111bdf0232SBoris Brezillon #include <linux/mfd/syscon.h>
121bdf0232SBoris Brezillon #include <linux/regmap.h>
136114067eSBoris BREZILLON
146114067eSBoris BREZILLON #include "pmc.h"
156114067eSBoris BREZILLON
161bdf0232SBoris Brezillon DEFINE_SPINLOCK(pmc_pcr_lock);
171bdf0232SBoris Brezillon
186114067eSBoris BREZILLON #define PERIPHERAL_ID_MIN 2
196114067eSBoris BREZILLON #define PERIPHERAL_ID_MAX 31
206114067eSBoris BREZILLON #define PERIPHERAL_MASK(id) (1 << ((id) & PERIPHERAL_ID_MAX))
216114067eSBoris BREZILLON
2286e4404aSBoris Brezillon #define PERIPHERAL_MAX_SHIFT 3
236114067eSBoris BREZILLON
246114067eSBoris BREZILLON struct clk_peripheral {
256114067eSBoris BREZILLON struct clk_hw hw;
261bdf0232SBoris Brezillon struct regmap *regmap;
276114067eSBoris BREZILLON u32 id;
286114067eSBoris BREZILLON };
296114067eSBoris BREZILLON
306114067eSBoris BREZILLON #define to_clk_peripheral(hw) container_of(hw, struct clk_peripheral, hw)
316114067eSBoris BREZILLON
326114067eSBoris BREZILLON struct clk_sam9x5_peripheral {
336114067eSBoris BREZILLON struct clk_hw hw;
341bdf0232SBoris Brezillon struct regmap *regmap;
356114067eSBoris BREZILLON struct clk_range range;
361bdf0232SBoris Brezillon spinlock_t *lock;
376114067eSBoris BREZILLON u32 id;
386114067eSBoris BREZILLON u32 div;
39cb4f4949SAlexandre Belloni const struct clk_pcr_layout *layout;
4036971566SClaudiu Beznea struct at91_clk_pms pms;
416114067eSBoris BREZILLON bool auto_div;
42b4c115c7SClaudiu Beznea int chg_pid;
436114067eSBoris BREZILLON };
446114067eSBoris BREZILLON
456114067eSBoris BREZILLON #define to_clk_sam9x5_peripheral(hw) \
466114067eSBoris BREZILLON container_of(hw, struct clk_sam9x5_peripheral, hw)
476114067eSBoris BREZILLON
clk_peripheral_enable(struct clk_hw * hw)486114067eSBoris BREZILLON static int clk_peripheral_enable(struct clk_hw *hw)
496114067eSBoris BREZILLON {
506114067eSBoris BREZILLON struct clk_peripheral *periph = to_clk_peripheral(hw);
516114067eSBoris BREZILLON int offset = AT91_PMC_PCER;
526114067eSBoris BREZILLON u32 id = periph->id;
536114067eSBoris BREZILLON
546114067eSBoris BREZILLON if (id < PERIPHERAL_ID_MIN)
556114067eSBoris BREZILLON return 0;
566114067eSBoris BREZILLON if (id > PERIPHERAL_ID_MAX)
576114067eSBoris BREZILLON offset = AT91_PMC_PCER1;
581bdf0232SBoris Brezillon regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
591bdf0232SBoris Brezillon
606114067eSBoris BREZILLON return 0;
616114067eSBoris BREZILLON }
626114067eSBoris BREZILLON
clk_peripheral_disable(struct clk_hw * hw)636114067eSBoris BREZILLON static void clk_peripheral_disable(struct clk_hw *hw)
646114067eSBoris BREZILLON {
656114067eSBoris BREZILLON struct clk_peripheral *periph = to_clk_peripheral(hw);
666114067eSBoris BREZILLON int offset = AT91_PMC_PCDR;
676114067eSBoris BREZILLON u32 id = periph->id;
686114067eSBoris BREZILLON
696114067eSBoris BREZILLON if (id < PERIPHERAL_ID_MIN)
706114067eSBoris BREZILLON return;
716114067eSBoris BREZILLON if (id > PERIPHERAL_ID_MAX)
726114067eSBoris BREZILLON offset = AT91_PMC_PCDR1;
731bdf0232SBoris Brezillon regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
746114067eSBoris BREZILLON }
756114067eSBoris BREZILLON
clk_peripheral_is_enabled(struct clk_hw * hw)766114067eSBoris BREZILLON static int clk_peripheral_is_enabled(struct clk_hw *hw)
776114067eSBoris BREZILLON {
786114067eSBoris BREZILLON struct clk_peripheral *periph = to_clk_peripheral(hw);
796114067eSBoris BREZILLON int offset = AT91_PMC_PCSR;
801bdf0232SBoris Brezillon unsigned int status;
816114067eSBoris BREZILLON u32 id = periph->id;
826114067eSBoris BREZILLON
836114067eSBoris BREZILLON if (id < PERIPHERAL_ID_MIN)
846114067eSBoris BREZILLON return 1;
856114067eSBoris BREZILLON if (id > PERIPHERAL_ID_MAX)
866114067eSBoris BREZILLON offset = AT91_PMC_PCSR1;
871bdf0232SBoris Brezillon regmap_read(periph->regmap, offset, &status);
881bdf0232SBoris Brezillon
891bdf0232SBoris Brezillon return status & PERIPHERAL_MASK(id) ? 1 : 0;
906114067eSBoris BREZILLON }
916114067eSBoris BREZILLON
926114067eSBoris BREZILLON static const struct clk_ops peripheral_ops = {
936114067eSBoris BREZILLON .enable = clk_peripheral_enable,
946114067eSBoris BREZILLON .disable = clk_peripheral_disable,
956114067eSBoris BREZILLON .is_enabled = clk_peripheral_is_enabled,
966114067eSBoris BREZILLON };
976114067eSBoris BREZILLON
98b2e39dc0SAlexandre Belloni struct clk_hw * __init
at91_clk_register_peripheral(struct regmap * regmap,const char * name,const char * parent_name,struct clk_hw * parent_hw,u32 id)991bdf0232SBoris Brezillon at91_clk_register_peripheral(struct regmap *regmap, const char *name,
100*c2f2ca0bSClaudiu Beznea const char *parent_name, struct clk_hw *parent_hw,
101*c2f2ca0bSClaudiu Beznea u32 id)
1026114067eSBoris BREZILLON {
1036114067eSBoris BREZILLON struct clk_peripheral *periph;
104*c2f2ca0bSClaudiu Beznea struct clk_init_data init = {};
105f5644f10SStephen Boyd struct clk_hw *hw;
106f5644f10SStephen Boyd int ret;
1076114067eSBoris BREZILLON
108*c2f2ca0bSClaudiu Beznea if (!name || !(parent_name || parent_hw) || id > PERIPHERAL_ID_MAX)
1096114067eSBoris BREZILLON return ERR_PTR(-EINVAL);
1106114067eSBoris BREZILLON
1116114067eSBoris BREZILLON periph = kzalloc(sizeof(*periph), GFP_KERNEL);
1126114067eSBoris BREZILLON if (!periph)
1136114067eSBoris BREZILLON return ERR_PTR(-ENOMEM);
1146114067eSBoris BREZILLON
1156114067eSBoris BREZILLON init.name = name;
1166114067eSBoris BREZILLON init.ops = &peripheral_ops;
117*c2f2ca0bSClaudiu Beznea if (parent_hw)
118*c2f2ca0bSClaudiu Beznea init.parent_hws = (const struct clk_hw **)&parent_hw;
119*c2f2ca0bSClaudiu Beznea else
120eddfb2e1SClaudiu Beznea init.parent_names = &parent_name;
121eddfb2e1SClaudiu Beznea init.num_parents = 1;
1226114067eSBoris BREZILLON init.flags = 0;
1236114067eSBoris BREZILLON
1246114067eSBoris BREZILLON periph->id = id;
1256114067eSBoris BREZILLON periph->hw.init = &init;
1261bdf0232SBoris Brezillon periph->regmap = regmap;
1276114067eSBoris BREZILLON
128f5644f10SStephen Boyd hw = &periph->hw;
129f5644f10SStephen Boyd ret = clk_hw_register(NULL, &periph->hw);
130f5644f10SStephen Boyd if (ret) {
1316114067eSBoris BREZILLON kfree(periph);
132f5644f10SStephen Boyd hw = ERR_PTR(ret);
133f5644f10SStephen Boyd }
1346114067eSBoris BREZILLON
135f5644f10SStephen Boyd return hw;
1366114067eSBoris BREZILLON }
1376114067eSBoris BREZILLON
clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral * periph)1386114067eSBoris BREZILLON static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral *periph)
1396114067eSBoris BREZILLON {
140d0979335SStephen Boyd struct clk_hw *parent;
1416114067eSBoris BREZILLON unsigned long parent_rate;
1426114067eSBoris BREZILLON int shift = 0;
1436114067eSBoris BREZILLON
1446114067eSBoris BREZILLON if (!periph->auto_div)
1456114067eSBoris BREZILLON return;
1466114067eSBoris BREZILLON
1476114067eSBoris BREZILLON if (periph->range.max) {
148d0979335SStephen Boyd parent = clk_hw_get_parent_by_index(&periph->hw, 0);
149d0979335SStephen Boyd parent_rate = clk_hw_get_rate(parent);
1506114067eSBoris BREZILLON if (!parent_rate)
1516114067eSBoris BREZILLON return;
1526114067eSBoris BREZILLON
1536114067eSBoris BREZILLON for (; shift < PERIPHERAL_MAX_SHIFT; shift++) {
1546114067eSBoris BREZILLON if (parent_rate >> shift <= periph->range.max)
1556114067eSBoris BREZILLON break;
1566114067eSBoris BREZILLON }
1576114067eSBoris BREZILLON }
1586114067eSBoris BREZILLON
1596114067eSBoris BREZILLON periph->auto_div = false;
1606114067eSBoris BREZILLON periph->div = shift;
1616114067eSBoris BREZILLON }
1626114067eSBoris BREZILLON
clk_sam9x5_peripheral_set(struct clk_sam9x5_peripheral * periph,unsigned int status)16336971566SClaudiu Beznea static int clk_sam9x5_peripheral_set(struct clk_sam9x5_peripheral *periph,
16436971566SClaudiu Beznea unsigned int status)
1656114067eSBoris BREZILLON {
1661bdf0232SBoris Brezillon unsigned long flags;
16736971566SClaudiu Beznea unsigned int enable = status ? AT91_PMC_PCR_EN : 0;
1686114067eSBoris BREZILLON
1696114067eSBoris BREZILLON if (periph->id < PERIPHERAL_ID_MIN)
1706114067eSBoris BREZILLON return 0;
1716114067eSBoris BREZILLON
1721bdf0232SBoris Brezillon spin_lock_irqsave(periph->lock, flags);
173cb4f4949SAlexandre Belloni regmap_write(periph->regmap, periph->layout->offset,
174cb4f4949SAlexandre Belloni (periph->id & periph->layout->pid_mask));
175cb4f4949SAlexandre Belloni regmap_update_bits(periph->regmap, periph->layout->offset,
176cb4f4949SAlexandre Belloni periph->layout->div_mask | periph->layout->cmd |
17736971566SClaudiu Beznea enable,
178cb4f4949SAlexandre Belloni field_prep(periph->layout->div_mask, periph->div) |
17936971566SClaudiu Beznea periph->layout->cmd | enable);
1801bdf0232SBoris Brezillon spin_unlock_irqrestore(periph->lock, flags);
1811bdf0232SBoris Brezillon
1826114067eSBoris BREZILLON return 0;
1836114067eSBoris BREZILLON }
1846114067eSBoris BREZILLON
clk_sam9x5_peripheral_enable(struct clk_hw * hw)18536971566SClaudiu Beznea static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
18636971566SClaudiu Beznea {
18736971566SClaudiu Beznea struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
18836971566SClaudiu Beznea
18936971566SClaudiu Beznea return clk_sam9x5_peripheral_set(periph, 1);
19036971566SClaudiu Beznea }
19136971566SClaudiu Beznea
clk_sam9x5_peripheral_disable(struct clk_hw * hw)1926114067eSBoris BREZILLON static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
1936114067eSBoris BREZILLON {
1946114067eSBoris BREZILLON struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
1951bdf0232SBoris Brezillon unsigned long flags;
1966114067eSBoris BREZILLON
1976114067eSBoris BREZILLON if (periph->id < PERIPHERAL_ID_MIN)
1986114067eSBoris BREZILLON return;
1996114067eSBoris BREZILLON
2001bdf0232SBoris Brezillon spin_lock_irqsave(periph->lock, flags);
201cb4f4949SAlexandre Belloni regmap_write(periph->regmap, periph->layout->offset,
202cb4f4949SAlexandre Belloni (periph->id & periph->layout->pid_mask));
203cb4f4949SAlexandre Belloni regmap_update_bits(periph->regmap, periph->layout->offset,
204cb4f4949SAlexandre Belloni AT91_PMC_PCR_EN | periph->layout->cmd,
205cb4f4949SAlexandre Belloni periph->layout->cmd);
2061bdf0232SBoris Brezillon spin_unlock_irqrestore(periph->lock, flags);
2076114067eSBoris BREZILLON }
2086114067eSBoris BREZILLON
clk_sam9x5_peripheral_is_enabled(struct clk_hw * hw)2096114067eSBoris BREZILLON static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
2106114067eSBoris BREZILLON {
2116114067eSBoris BREZILLON struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
2121bdf0232SBoris Brezillon unsigned long flags;
2131bdf0232SBoris Brezillon unsigned int status;
2146114067eSBoris BREZILLON
2156114067eSBoris BREZILLON if (periph->id < PERIPHERAL_ID_MIN)
2166114067eSBoris BREZILLON return 1;
2176114067eSBoris BREZILLON
2181bdf0232SBoris Brezillon spin_lock_irqsave(periph->lock, flags);
219cb4f4949SAlexandre Belloni regmap_write(periph->regmap, periph->layout->offset,
220cb4f4949SAlexandre Belloni (periph->id & periph->layout->pid_mask));
221cb4f4949SAlexandre Belloni regmap_read(periph->regmap, periph->layout->offset, &status);
2221bdf0232SBoris Brezillon spin_unlock_irqrestore(periph->lock, flags);
2236114067eSBoris BREZILLON
22442324d95SClaudiu Beznea return !!(status & AT91_PMC_PCR_EN);
2256114067eSBoris BREZILLON }
2266114067eSBoris BREZILLON
2276114067eSBoris BREZILLON static unsigned long
clk_sam9x5_peripheral_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)2286114067eSBoris BREZILLON clk_sam9x5_peripheral_recalc_rate(struct clk_hw *hw,
2296114067eSBoris BREZILLON unsigned long parent_rate)
2306114067eSBoris BREZILLON {
2316114067eSBoris BREZILLON struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
2321bdf0232SBoris Brezillon unsigned long flags;
2331bdf0232SBoris Brezillon unsigned int status;
2346114067eSBoris BREZILLON
2356114067eSBoris BREZILLON if (periph->id < PERIPHERAL_ID_MIN)
2366114067eSBoris BREZILLON return parent_rate;
2376114067eSBoris BREZILLON
2381bdf0232SBoris Brezillon spin_lock_irqsave(periph->lock, flags);
239cb4f4949SAlexandre Belloni regmap_write(periph->regmap, periph->layout->offset,
240cb4f4949SAlexandre Belloni (periph->id & periph->layout->pid_mask));
241cb4f4949SAlexandre Belloni regmap_read(periph->regmap, periph->layout->offset, &status);
2421bdf0232SBoris Brezillon spin_unlock_irqrestore(periph->lock, flags);
2436114067eSBoris BREZILLON
2441bdf0232SBoris Brezillon if (status & AT91_PMC_PCR_EN) {
245cb4f4949SAlexandre Belloni periph->div = field_get(periph->layout->div_mask, status);
2466114067eSBoris BREZILLON periph->auto_div = false;
2476114067eSBoris BREZILLON } else {
2486114067eSBoris BREZILLON clk_sam9x5_peripheral_autodiv(periph);
2496114067eSBoris BREZILLON }
2506114067eSBoris BREZILLON
2516114067eSBoris BREZILLON return parent_rate >> periph->div;
2526114067eSBoris BREZILLON }
2536114067eSBoris BREZILLON
clk_sam9x5_peripheral_best_diff(struct clk_rate_request * req,struct clk_hw * parent,unsigned long parent_rate,u32 shift,long * best_diff,long * best_rate)254b4c115c7SClaudiu Beznea static void clk_sam9x5_peripheral_best_diff(struct clk_rate_request *req,
255b4c115c7SClaudiu Beznea struct clk_hw *parent,
256b4c115c7SClaudiu Beznea unsigned long parent_rate,
257b4c115c7SClaudiu Beznea u32 shift, long *best_diff,
258b4c115c7SClaudiu Beznea long *best_rate)
259b4c115c7SClaudiu Beznea {
260b4c115c7SClaudiu Beznea unsigned long tmp_rate = parent_rate >> shift;
261b4c115c7SClaudiu Beznea unsigned long tmp_diff = abs(req->rate - tmp_rate);
262b4c115c7SClaudiu Beznea
263b4c115c7SClaudiu Beznea if (*best_diff < 0 || *best_diff >= tmp_diff) {
264b4c115c7SClaudiu Beznea *best_rate = tmp_rate;
265b4c115c7SClaudiu Beznea *best_diff = tmp_diff;
266b4c115c7SClaudiu Beznea req->best_parent_rate = parent_rate;
267b4c115c7SClaudiu Beznea req->best_parent_hw = parent;
268b4c115c7SClaudiu Beznea }
269b4c115c7SClaudiu Beznea }
270b4c115c7SClaudiu Beznea
clk_sam9x5_peripheral_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)271b4c115c7SClaudiu Beznea static int clk_sam9x5_peripheral_determine_rate(struct clk_hw *hw,
272b4c115c7SClaudiu Beznea struct clk_rate_request *req)
273b4c115c7SClaudiu Beznea {
274b4c115c7SClaudiu Beznea struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
275b4c115c7SClaudiu Beznea struct clk_hw *parent = clk_hw_get_parent(hw);
276b4c115c7SClaudiu Beznea unsigned long parent_rate = clk_hw_get_rate(parent);
277b4c115c7SClaudiu Beznea unsigned long tmp_rate;
278b4c115c7SClaudiu Beznea long best_rate = LONG_MIN;
279b4c115c7SClaudiu Beznea long best_diff = LONG_MIN;
280b4c115c7SClaudiu Beznea u32 shift;
281b4c115c7SClaudiu Beznea
282b4c115c7SClaudiu Beznea if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
283b4c115c7SClaudiu Beznea return parent_rate;
284b4c115c7SClaudiu Beznea
285b4c115c7SClaudiu Beznea /* Fist step: check the available dividers. */
286b4c115c7SClaudiu Beznea for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
287b4c115c7SClaudiu Beznea tmp_rate = parent_rate >> shift;
288b4c115c7SClaudiu Beznea
289b4c115c7SClaudiu Beznea if (periph->range.max && tmp_rate > periph->range.max)
290b4c115c7SClaudiu Beznea continue;
291b4c115c7SClaudiu Beznea
292b4c115c7SClaudiu Beznea clk_sam9x5_peripheral_best_diff(req, parent, parent_rate,
293b4c115c7SClaudiu Beznea shift, &best_diff, &best_rate);
294b4c115c7SClaudiu Beznea
295b4c115c7SClaudiu Beznea if (!best_diff || best_rate <= req->rate)
296b4c115c7SClaudiu Beznea break;
297b4c115c7SClaudiu Beznea }
298b4c115c7SClaudiu Beznea
299b4c115c7SClaudiu Beznea if (periph->chg_pid < 0)
300b4c115c7SClaudiu Beznea goto end;
301b4c115c7SClaudiu Beznea
302b4c115c7SClaudiu Beznea /* Step two: try to request rate from parent. */
303b4c115c7SClaudiu Beznea parent = clk_hw_get_parent_by_index(hw, periph->chg_pid);
304b4c115c7SClaudiu Beznea if (!parent)
305b4c115c7SClaudiu Beznea goto end;
306b4c115c7SClaudiu Beznea
307b4c115c7SClaudiu Beznea for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
308262ca38fSMaxime Ripard struct clk_rate_request req_parent;
309b4c115c7SClaudiu Beznea
310262ca38fSMaxime Ripard clk_hw_forward_rate_request(hw, req, parent, &req_parent, req->rate << shift);
311b4c115c7SClaudiu Beznea if (__clk_determine_rate(parent, &req_parent))
312b4c115c7SClaudiu Beznea continue;
313b4c115c7SClaudiu Beznea
314b4c115c7SClaudiu Beznea clk_sam9x5_peripheral_best_diff(req, parent, req_parent.rate,
315b4c115c7SClaudiu Beznea shift, &best_diff, &best_rate);
316b4c115c7SClaudiu Beznea
317b4c115c7SClaudiu Beznea if (!best_diff)
318b4c115c7SClaudiu Beznea break;
319b4c115c7SClaudiu Beznea }
320b4c115c7SClaudiu Beznea end:
321b4c115c7SClaudiu Beznea if (best_rate < 0 ||
322b4c115c7SClaudiu Beznea (periph->range.max && best_rate > periph->range.max))
323b4c115c7SClaudiu Beznea return -EINVAL;
324b4c115c7SClaudiu Beznea
325b4c115c7SClaudiu Beznea pr_debug("PCK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
326b4c115c7SClaudiu Beznea __func__, best_rate,
327b4c115c7SClaudiu Beznea __clk_get_name((req->best_parent_hw)->clk),
328b4c115c7SClaudiu Beznea req->best_parent_rate);
329b4c115c7SClaudiu Beznea
330b4c115c7SClaudiu Beznea req->rate = best_rate;
331b4c115c7SClaudiu Beznea
332b4c115c7SClaudiu Beznea return 0;
333b4c115c7SClaudiu Beznea }
334b4c115c7SClaudiu Beznea
clk_sam9x5_peripheral_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)3356114067eSBoris BREZILLON static long clk_sam9x5_peripheral_round_rate(struct clk_hw *hw,
3366114067eSBoris BREZILLON unsigned long rate,
3376114067eSBoris BREZILLON unsigned long *parent_rate)
3386114067eSBoris BREZILLON {
3396114067eSBoris BREZILLON int shift = 0;
3406114067eSBoris BREZILLON unsigned long best_rate;
3416114067eSBoris BREZILLON unsigned long best_diff;
3426114067eSBoris BREZILLON unsigned long cur_rate = *parent_rate;
3436114067eSBoris BREZILLON unsigned long cur_diff;
3446114067eSBoris BREZILLON struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
3456114067eSBoris BREZILLON
3466114067eSBoris BREZILLON if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
3476114067eSBoris BREZILLON return *parent_rate;
3486114067eSBoris BREZILLON
3496114067eSBoris BREZILLON if (periph->range.max) {
35086e4404aSBoris Brezillon for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
3516114067eSBoris BREZILLON cur_rate = *parent_rate >> shift;
3526114067eSBoris BREZILLON if (cur_rate <= periph->range.max)
3536114067eSBoris BREZILLON break;
3546114067eSBoris BREZILLON }
3556114067eSBoris BREZILLON }
3566114067eSBoris BREZILLON
3576114067eSBoris BREZILLON if (rate >= cur_rate)
3586114067eSBoris BREZILLON return cur_rate;
3596114067eSBoris BREZILLON
3606114067eSBoris BREZILLON best_diff = cur_rate - rate;
3616114067eSBoris BREZILLON best_rate = cur_rate;
36286e4404aSBoris Brezillon for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
3636114067eSBoris BREZILLON cur_rate = *parent_rate >> shift;
3646114067eSBoris BREZILLON if (cur_rate < rate)
3656114067eSBoris BREZILLON cur_diff = rate - cur_rate;
3666114067eSBoris BREZILLON else
3676114067eSBoris BREZILLON cur_diff = cur_rate - rate;
3686114067eSBoris BREZILLON
3696114067eSBoris BREZILLON if (cur_diff < best_diff) {
3706114067eSBoris BREZILLON best_diff = cur_diff;
3716114067eSBoris BREZILLON best_rate = cur_rate;
3726114067eSBoris BREZILLON }
3736114067eSBoris BREZILLON
3746114067eSBoris BREZILLON if (!best_diff || cur_rate < rate)
3756114067eSBoris BREZILLON break;
3766114067eSBoris BREZILLON }
3776114067eSBoris BREZILLON
3786114067eSBoris BREZILLON return best_rate;
3796114067eSBoris BREZILLON }
3806114067eSBoris BREZILLON
clk_sam9x5_peripheral_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)3816114067eSBoris BREZILLON static int clk_sam9x5_peripheral_set_rate(struct clk_hw *hw,
3826114067eSBoris BREZILLON unsigned long rate,
3836114067eSBoris BREZILLON unsigned long parent_rate)
3846114067eSBoris BREZILLON {
3856114067eSBoris BREZILLON int shift;
3866114067eSBoris BREZILLON struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
3876114067eSBoris BREZILLON if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max) {
3886114067eSBoris BREZILLON if (parent_rate == rate)
3896114067eSBoris BREZILLON return 0;
3906114067eSBoris BREZILLON else
3916114067eSBoris BREZILLON return -EINVAL;
3926114067eSBoris BREZILLON }
3936114067eSBoris BREZILLON
3946114067eSBoris BREZILLON if (periph->range.max && rate > periph->range.max)
3956114067eSBoris BREZILLON return -EINVAL;
3966114067eSBoris BREZILLON
39786e4404aSBoris Brezillon for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
3986114067eSBoris BREZILLON if (parent_rate >> shift == rate) {
3996114067eSBoris BREZILLON periph->auto_div = false;
4006114067eSBoris BREZILLON periph->div = shift;
4016114067eSBoris BREZILLON return 0;
4026114067eSBoris BREZILLON }
4036114067eSBoris BREZILLON }
4046114067eSBoris BREZILLON
4056114067eSBoris BREZILLON return -EINVAL;
4066114067eSBoris BREZILLON }
4076114067eSBoris BREZILLON
clk_sam9x5_peripheral_save_context(struct clk_hw * hw)40836971566SClaudiu Beznea static int clk_sam9x5_peripheral_save_context(struct clk_hw *hw)
40936971566SClaudiu Beznea {
41036971566SClaudiu Beznea struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
41136971566SClaudiu Beznea
41236971566SClaudiu Beznea periph->pms.status = clk_sam9x5_peripheral_is_enabled(hw);
41336971566SClaudiu Beznea
41436971566SClaudiu Beznea return 0;
41536971566SClaudiu Beznea }
41636971566SClaudiu Beznea
clk_sam9x5_peripheral_restore_context(struct clk_hw * hw)41736971566SClaudiu Beznea static void clk_sam9x5_peripheral_restore_context(struct clk_hw *hw)
41836971566SClaudiu Beznea {
41936971566SClaudiu Beznea struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
42036971566SClaudiu Beznea
42136971566SClaudiu Beznea if (periph->pms.status)
42236971566SClaudiu Beznea clk_sam9x5_peripheral_set(periph, periph->pms.status);
42336971566SClaudiu Beznea }
42436971566SClaudiu Beznea
4256114067eSBoris BREZILLON static const struct clk_ops sam9x5_peripheral_ops = {
4266114067eSBoris BREZILLON .enable = clk_sam9x5_peripheral_enable,
4276114067eSBoris BREZILLON .disable = clk_sam9x5_peripheral_disable,
4286114067eSBoris BREZILLON .is_enabled = clk_sam9x5_peripheral_is_enabled,
4296114067eSBoris BREZILLON .recalc_rate = clk_sam9x5_peripheral_recalc_rate,
4306114067eSBoris BREZILLON .round_rate = clk_sam9x5_peripheral_round_rate,
4316114067eSBoris BREZILLON .set_rate = clk_sam9x5_peripheral_set_rate,
43236971566SClaudiu Beznea .save_context = clk_sam9x5_peripheral_save_context,
43336971566SClaudiu Beznea .restore_context = clk_sam9x5_peripheral_restore_context,
4346114067eSBoris BREZILLON };
4356114067eSBoris BREZILLON
436b4c115c7SClaudiu Beznea static const struct clk_ops sam9x5_peripheral_chg_ops = {
437b4c115c7SClaudiu Beznea .enable = clk_sam9x5_peripheral_enable,
438b4c115c7SClaudiu Beznea .disable = clk_sam9x5_peripheral_disable,
439b4c115c7SClaudiu Beznea .is_enabled = clk_sam9x5_peripheral_is_enabled,
440b4c115c7SClaudiu Beznea .recalc_rate = clk_sam9x5_peripheral_recalc_rate,
441b4c115c7SClaudiu Beznea .determine_rate = clk_sam9x5_peripheral_determine_rate,
442b4c115c7SClaudiu Beznea .set_rate = clk_sam9x5_peripheral_set_rate,
44336971566SClaudiu Beznea .save_context = clk_sam9x5_peripheral_save_context,
44436971566SClaudiu Beznea .restore_context = clk_sam9x5_peripheral_restore_context,
445b4c115c7SClaudiu Beznea };
446b4c115c7SClaudiu Beznea
447b2e39dc0SAlexandre Belloni struct clk_hw * __init
at91_clk_register_sam9x5_peripheral(struct regmap * regmap,spinlock_t * lock,const struct clk_pcr_layout * layout,const char * name,const char * parent_name,struct clk_hw * parent_hw,u32 id,const struct clk_range * range,int chg_pid,unsigned long flags)4481bdf0232SBoris Brezillon at91_clk_register_sam9x5_peripheral(struct regmap *regmap, spinlock_t *lock,
449cb4f4949SAlexandre Belloni const struct clk_pcr_layout *layout,
4501bdf0232SBoris Brezillon const char *name, const char *parent_name,
451*c2f2ca0bSClaudiu Beznea struct clk_hw *parent_hw,
452b4c115c7SClaudiu Beznea u32 id, const struct clk_range *range,
45368b3b6f1SClaudiu Beznea int chg_pid, unsigned long flags)
4546114067eSBoris BREZILLON {
4556114067eSBoris BREZILLON struct clk_sam9x5_peripheral *periph;
456*c2f2ca0bSClaudiu Beznea struct clk_init_data init = {};
457f5644f10SStephen Boyd struct clk_hw *hw;
458f5644f10SStephen Boyd int ret;
4596114067eSBoris BREZILLON
460*c2f2ca0bSClaudiu Beznea if (!name || !(parent_name || parent_hw))
4616114067eSBoris BREZILLON return ERR_PTR(-EINVAL);
4626114067eSBoris BREZILLON
4636114067eSBoris BREZILLON periph = kzalloc(sizeof(*periph), GFP_KERNEL);
4646114067eSBoris BREZILLON if (!periph)
4656114067eSBoris BREZILLON return ERR_PTR(-ENOMEM);
4666114067eSBoris BREZILLON
4676114067eSBoris BREZILLON init.name = name;
468*c2f2ca0bSClaudiu Beznea if (parent_hw)
469*c2f2ca0bSClaudiu Beznea init.parent_hws = (const struct clk_hw **)&parent_hw;
470*c2f2ca0bSClaudiu Beznea else
471b4c115c7SClaudiu Beznea init.parent_names = &parent_name;
472b4c115c7SClaudiu Beznea init.num_parents = 1;
47368b3b6f1SClaudiu Beznea init.flags = flags;
474b4c115c7SClaudiu Beznea if (chg_pid < 0) {
475b4c115c7SClaudiu Beznea init.ops = &sam9x5_peripheral_ops;
476b4c115c7SClaudiu Beznea } else {
47768b3b6f1SClaudiu Beznea init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
478b4c115c7SClaudiu Beznea CLK_SET_RATE_PARENT;
479b4c115c7SClaudiu Beznea init.ops = &sam9x5_peripheral_chg_ops;
480b4c115c7SClaudiu Beznea }
4816114067eSBoris BREZILLON
4826114067eSBoris BREZILLON periph->id = id;
4836114067eSBoris BREZILLON periph->hw.init = &init;
4846114067eSBoris BREZILLON periph->div = 0;
4851bdf0232SBoris Brezillon periph->regmap = regmap;
4861bdf0232SBoris Brezillon periph->lock = lock;
487cb4f4949SAlexandre Belloni if (layout->div_mask)
4886114067eSBoris BREZILLON periph->auto_div = true;
489cb4f4949SAlexandre Belloni periph->layout = layout;
4906114067eSBoris BREZILLON periph->range = *range;
491b4c115c7SClaudiu Beznea periph->chg_pid = chg_pid;
4926114067eSBoris BREZILLON
493f5644f10SStephen Boyd hw = &periph->hw;
494f5644f10SStephen Boyd ret = clk_hw_register(NULL, &periph->hw);
495f5644f10SStephen Boyd if (ret) {
4966114067eSBoris BREZILLON kfree(periph);
497f5644f10SStephen Boyd hw = ERR_PTR(ret);
498b3b02eacSAlexandre Belloni } else {
4996114067eSBoris BREZILLON clk_sam9x5_peripheral_autodiv(periph);
500b3b02eacSAlexandre Belloni }
5016114067eSBoris BREZILLON
502f5644f10SStephen Boyd return hw;
5036114067eSBoris BREZILLON }
504