xref: /openbmc/linux/drivers/clk/sunxi-ng/ccu_nkm.c (revision 8730046c)
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>
12 
13 #include "ccu_gate.h"
14 #include "ccu_nkm.h"
15 
16 struct _ccu_nkm {
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 };
21 
22 static void ccu_nkm_find_best(unsigned long parent, unsigned long rate,
23 			      struct _ccu_nkm *nkm)
24 {
25 	unsigned long best_rate = 0;
26 	unsigned long best_n = 0, best_k = 0, best_m = 0;
27 	unsigned long _n, _k, _m;
28 
29 	for (_k = nkm->min_k; _k <= nkm->max_k; _k++) {
30 		for (_n = nkm->min_n; _n <= nkm->max_n; _n++) {
31 			for (_m = nkm->min_m; _m <= nkm->max_m; _m++) {
32 				unsigned long tmp_rate;
33 
34 				tmp_rate = parent * _n * _k / _m;
35 
36 				if (tmp_rate > rate)
37 					continue;
38 				if ((rate - tmp_rate) < (rate - best_rate)) {
39 					best_rate = tmp_rate;
40 					best_n = _n;
41 					best_k = _k;
42 					best_m = _m;
43 				}
44 			}
45 		}
46 	}
47 
48 	nkm->n = best_n;
49 	nkm->k = best_k;
50 	nkm->m = best_m;
51 }
52 
53 static void ccu_nkm_disable(struct clk_hw *hw)
54 {
55 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
56 
57 	return ccu_gate_helper_disable(&nkm->common, nkm->enable);
58 }
59 
60 static int ccu_nkm_enable(struct clk_hw *hw)
61 {
62 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
63 
64 	return ccu_gate_helper_enable(&nkm->common, nkm->enable);
65 }
66 
67 static int ccu_nkm_is_enabled(struct clk_hw *hw)
68 {
69 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
70 
71 	return ccu_gate_helper_is_enabled(&nkm->common, nkm->enable);
72 }
73 
74 static unsigned long ccu_nkm_recalc_rate(struct clk_hw *hw,
75 					unsigned long parent_rate)
76 {
77 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
78 	unsigned long n, m, k;
79 	u32 reg;
80 
81 	reg = readl(nkm->common.base + nkm->common.reg);
82 
83 	n = reg >> nkm->n.shift;
84 	n &= (1 << nkm->n.width) - 1;
85 
86 	k = reg >> nkm->k.shift;
87 	k &= (1 << nkm->k.width) - 1;
88 
89 	m = reg >> nkm->m.shift;
90 	m &= (1 << nkm->m.width) - 1;
91 
92 	return parent_rate * (n + 1) * (k + 1) / (m + 1);
93 }
94 
95 static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal *mux,
96 					unsigned long parent_rate,
97 					unsigned long rate,
98 					void *data)
99 {
100 	struct ccu_nkm *nkm = data;
101 	struct _ccu_nkm _nkm;
102 
103 	_nkm.min_n = nkm->n.min;
104 	_nkm.max_n = 1 << nkm->n.width;
105 	_nkm.min_k = nkm->k.min;
106 	_nkm.max_k = 1 << nkm->k.width;
107 	_nkm.min_m = 1;
108 	_nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
109 
110 	ccu_nkm_find_best(parent_rate, rate, &_nkm);
111 
112 	return parent_rate * _nkm.n * _nkm.k / _nkm.m;
113 }
114 
115 static int ccu_nkm_determine_rate(struct clk_hw *hw,
116 				  struct clk_rate_request *req)
117 {
118 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
119 
120 	return ccu_mux_helper_determine_rate(&nkm->common, &nkm->mux,
121 					     req, ccu_nkm_round_rate, nkm);
122 }
123 
124 static int ccu_nkm_set_rate(struct clk_hw *hw, unsigned long rate,
125 			   unsigned long parent_rate)
126 {
127 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
128 	struct _ccu_nkm _nkm;
129 	unsigned long flags;
130 	u32 reg;
131 
132 	_nkm.min_n = nkm->n.min;
133 	_nkm.max_n = 1 << nkm->n.width;
134 	_nkm.min_k = nkm->k.min;
135 	_nkm.max_k = 1 << nkm->k.width;
136 	_nkm.min_m = 1;
137 	_nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
138 
139 	ccu_nkm_find_best(parent_rate, rate, &_nkm);
140 
141 	spin_lock_irqsave(nkm->common.lock, flags);
142 
143 	reg = readl(nkm->common.base + nkm->common.reg);
144 	reg &= ~GENMASK(nkm->n.width + nkm->n.shift - 1, nkm->n.shift);
145 	reg &= ~GENMASK(nkm->k.width + nkm->k.shift - 1, nkm->k.shift);
146 	reg &= ~GENMASK(nkm->m.width + nkm->m.shift - 1, nkm->m.shift);
147 
148 	reg |= (_nkm.n - 1) << nkm->n.shift;
149 	reg |= (_nkm.k - 1) << nkm->k.shift;
150 	reg |= (_nkm.m - 1) << nkm->m.shift;
151 
152 	writel(reg, nkm->common.base + nkm->common.reg);
153 
154 	spin_unlock_irqrestore(nkm->common.lock, flags);
155 
156 	ccu_helper_wait_for_lock(&nkm->common, nkm->lock);
157 
158 	return 0;
159 }
160 
161 static u8 ccu_nkm_get_parent(struct clk_hw *hw)
162 {
163 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
164 
165 	return ccu_mux_helper_get_parent(&nkm->common, &nkm->mux);
166 }
167 
168 static int ccu_nkm_set_parent(struct clk_hw *hw, u8 index)
169 {
170 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
171 
172 	return ccu_mux_helper_set_parent(&nkm->common, &nkm->mux, index);
173 }
174 
175 const struct clk_ops ccu_nkm_ops = {
176 	.disable	= ccu_nkm_disable,
177 	.enable		= ccu_nkm_enable,
178 	.is_enabled	= ccu_nkm_is_enabled,
179 
180 	.get_parent	= ccu_nkm_get_parent,
181 	.set_parent	= ccu_nkm_set_parent,
182 
183 	.determine_rate	= ccu_nkm_determine_rate,
184 	.recalc_rate	= ccu_nkm_recalc_rate,
185 	.set_rate	= ccu_nkm_set_rate,
186 };
187