• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2016 Maxime Ripard
4  * Maxime Ripard <maxime.ripard@free-electrons.com>
5  */
6 
7 #include <linux/clk-provider.h>
8 #include <linux/io.h>
9 
10 #include "ccu_gate.h"
11 #include "ccu_nk.h"
12 
13 struct _ccu_nk {
14 	unsigned long	n, min_n, max_n;
15 	unsigned long	k, min_k, max_k;
16 };
17 
ccu_nk_find_best(unsigned long parent,u64 rate,struct _ccu_nk * nk)18 static void ccu_nk_find_best(unsigned long parent, u64 rate,
19 			     struct _ccu_nk *nk)
20 {
21 	u64 best_rate = 0;
22 	unsigned int best_k = 0, best_n = 0;
23 	unsigned int _k, _n;
24 
25 	for (_k = nk->min_k; _k <= nk->max_k; _k++) {
26 		for (_n = nk->min_n; _n <= nk->max_n; _n++) {
27 			u64 tmp_rate = parent * _n * _k;
28 
29 			if (tmp_rate > rate)
30 				continue;
31 
32 			if ((rate - tmp_rate) < (rate - best_rate)) {
33 				best_rate = tmp_rate;
34 				best_k = _k;
35 				best_n = _n;
36 			}
37 		}
38 	}
39 
40 	nk->k = best_k;
41 	nk->n = best_n;
42 }
43 
ccu_nk_disable(struct clk_hw * hw)44 static void ccu_nk_disable(struct clk_hw *hw)
45 {
46 	struct ccu_nk *nk = hw_to_ccu_nk(hw);
47 
48 	return ccu_gate_helper_disable(&nk->common, nk->enable);
49 }
50 
ccu_nk_enable(struct clk_hw * hw)51 static int ccu_nk_enable(struct clk_hw *hw)
52 {
53 	struct ccu_nk *nk = hw_to_ccu_nk(hw);
54 
55 	return ccu_gate_helper_enable(&nk->common, nk->enable);
56 }
57 
ccu_nk_is_enabled(struct clk_hw * hw)58 static int ccu_nk_is_enabled(struct clk_hw *hw)
59 {
60 	struct ccu_nk *nk = hw_to_ccu_nk(hw);
61 
62 	return ccu_gate_helper_is_enabled(&nk->common, nk->enable);
63 }
64 
ccu_nk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)65 static unsigned long ccu_nk_recalc_rate(struct clk_hw *hw,
66 					unsigned long parent_rate)
67 {
68 	struct ccu_nk *nk = hw_to_ccu_nk(hw);
69 	unsigned long n, k;
70 	u64 rate;
71 	u32 reg;
72 
73 	reg = readl(nk->common.base + nk->common.reg);
74 
75 	n = reg >> nk->n.shift;
76 	n &= (1 << nk->n.width) - 1;
77 	n += nk->n.offset;
78 	if (!n)
79 		n++;
80 
81 	k = reg >> nk->k.shift;
82 	k &= (1 << nk->k.width) - 1;
83 	k += nk->k.offset;
84 	if (!k)
85 		k++;
86 
87 	rate = parent_rate * n * k;
88 	if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
89 		do_div(rate, nk->fixed_post_div);
90 
91 	return rate;
92 }
93 
ccu_nk_round_rate(struct clk_hw * hw,unsigned long _rate,unsigned long * parent_rate)94 static long ccu_nk_round_rate(struct clk_hw *hw, unsigned long _rate,
95 			      unsigned long *parent_rate)
96 {
97 	struct ccu_nk *nk = hw_to_ccu_nk(hw);
98 	struct _ccu_nk _nk;
99 	u64 rate = _rate;
100 
101 	if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
102 		rate *= nk->fixed_post_div;
103 
104 	_nk.min_n = nk->n.min ?: 1;
105 	_nk.max_n = nk->n.max ?: 1 << nk->n.width;
106 	_nk.min_k = nk->k.min ?: 1;
107 	_nk.max_k = nk->k.max ?: 1 << nk->k.width;
108 
109 	ccu_nk_find_best(*parent_rate, rate, &_nk);
110 	rate = *parent_rate * _nk.n * _nk.k;
111 
112 	if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
113 		do_div(rate, nk->fixed_post_div);
114 
115 	return rate;
116 }
117 
ccu_nk_set_rate(struct clk_hw * hw,unsigned long _rate,unsigned long parent_rate)118 static int ccu_nk_set_rate(struct clk_hw *hw, unsigned long _rate,
119 			   unsigned long parent_rate)
120 {
121 	struct ccu_nk *nk = hw_to_ccu_nk(hw);
122 	unsigned long flags;
123 	struct _ccu_nk _nk;
124 	u64 rate = _rate;
125 	u32 reg;
126 
127 	if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
128 		rate = rate * nk->fixed_post_div;
129 
130 	_nk.min_n = nk->n.min ?: 1;
131 	_nk.max_n = nk->n.max ?: 1 << nk->n.width;
132 	_nk.min_k = nk->k.min ?: 1;
133 	_nk.max_k = nk->k.max ?: 1 << nk->k.width;
134 
135 	ccu_nk_find_best(parent_rate, rate, &_nk);
136 
137 	spin_lock_irqsave(nk->common.lock, flags);
138 
139 	reg = readl(nk->common.base + nk->common.reg);
140 	reg &= ~GENMASK(nk->n.width + nk->n.shift - 1, nk->n.shift);
141 	reg &= ~GENMASK(nk->k.width + nk->k.shift - 1, nk->k.shift);
142 
143 	reg |= (_nk.k - nk->k.offset) << nk->k.shift;
144 	reg |= (_nk.n - nk->n.offset) << nk->n.shift;
145 	writel(reg, nk->common.base + nk->common.reg);
146 
147 	spin_unlock_irqrestore(nk->common.lock, flags);
148 
149 	ccu_helper_wait_for_lock(&nk->common, nk->lock);
150 
151 	return 0;
152 }
153 
154 const struct clk_ops ccu_nk_ops = {
155 	.disable	= ccu_nk_disable,
156 	.enable		= ccu_nk_enable,
157 	.is_enabled	= ccu_nk_is_enabled,
158 
159 	.recalc_rate	= ccu_nk_recalc_rate,
160 	.round_rate	= ccu_nk_round_rate,
161 	.set_rate	= ccu_nk_set_rate,
162 };
163