blob: 841840e35e610374f3f45dad562e4e97ce399554 [file] [log] [blame]
Maxime Riparddf6561e2016-06-29 21:05:32 +02001/*
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>
Maxime Riparddf6561e2016-06-29 21:05:32 +020012
13#include "ccu_gate.h"
14#include "ccu_nkm.h"
15
16struct _ccu_nkm {
Maxime Ripard6e0d50d2016-09-29 22:57:26 +020017 unsigned long n, min_n, max_n;
18 unsigned long k, min_k, max_k;
19 unsigned long m, min_m, max_m;
Maxime Riparddf6561e2016-06-29 21:05:32 +020020};
21
22static 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
Maxime Ripard6e0d50d2016-09-29 22:57:26 +020029 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++) {
Maxime Ripardee286482016-09-29 22:53:12 +020032 unsigned long tmp_rate;
Maxime Riparddf6561e2016-06-29 21:05:32 +020033
Maxime Ripardee286482016-09-29 22:53:12 +020034 tmp_rate = parent * _n * _k / _m;
Maxime Riparddf6561e2016-06-29 21:05:32 +020035
Maxime Ripardee286482016-09-29 22:53:12 +020036 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 }
Maxime Riparddf6561e2016-06-29 21:05:32 +020045 }
46 }
47
48 nkm->n = best_n;
49 nkm->k = best_k;
50 nkm->m = best_m;
51}
52
53static 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
60static 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
67static 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
74static 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);
Icenowy Zhenga6653772017-08-12 20:43:51 +080078 unsigned long n, m, k, rate;
Maxime Riparddf6561e2016-06-29 21:05:32 +020079 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;
Maxime Riparde66f81b2016-11-08 18:12:34 +010085 n += nkm->n.offset;
86 if (!n)
87 n++;
Maxime Riparddf6561e2016-06-29 21:05:32 +020088
89 k = reg >> nkm->k.shift;
90 k &= (1 << nkm->k.width) - 1;
Maxime Riparde66f81b2016-11-08 18:12:34 +010091 k += nkm->k.offset;
92 if (!k)
93 k++;
Maxime Riparddf6561e2016-06-29 21:05:32 +020094
95 m = reg >> nkm->m.shift;
96 m &= (1 << nkm->m.width) - 1;
Maxime Riparde66f81b2016-11-08 18:12:34 +010097 m += nkm->m.offset;
98 if (!m)
99 m++;
Maxime Riparddf6561e2016-06-29 21:05:32 +0200100
Icenowy Zhenga6653772017-08-12 20:43:51 +0800101 rate = parent_rate * n * k / m;
102
103 if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
104 rate /= nkm->fixed_post_div;
105
106 return rate;
Maxime Riparddf6561e2016-06-29 21:05:32 +0200107}
108
Chen-Yu Tsaia3658352016-07-26 15:04:28 +0800109static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal *mux,
Maxime Ripard10a8d9b2017-05-17 09:40:31 +0200110 struct clk_hw *hw,
111 unsigned long *parent_rate,
Chen-Yu Tsaia3658352016-07-26 15:04:28 +0800112 unsigned long rate,
113 void *data)
Maxime Riparddf6561e2016-06-29 21:05:32 +0200114{
Chen-Yu Tsaia3658352016-07-26 15:04:28 +0800115 struct ccu_nkm *nkm = data;
Maxime Riparddf6561e2016-06-29 21:05:32 +0200116 struct _ccu_nkm _nkm;
117
Chen-Yu Tsai4162c5c2017-03-24 16:33:05 +0800118 _nkm.min_n = nkm->n.min ?: 1;
Maxime Ripard0c3c8e12016-10-14 12:08:19 +0200119 _nkm.max_n = nkm->n.max ?: 1 << nkm->n.width;
Chen-Yu Tsai4162c5c2017-03-24 16:33:05 +0800120 _nkm.min_k = nkm->k.min ?: 1;
Maxime Ripard0c3c8e12016-10-14 12:08:19 +0200121 _nkm.max_k = nkm->k.max ?: 1 << nkm->k.width;
Maxime Ripard6e0d50d2016-09-29 22:57:26 +0200122 _nkm.min_m = 1;
Maxime Ripard87ba9e52016-09-06 12:29:04 +0200123 _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
Maxime Riparddf6561e2016-06-29 21:05:32 +0200124
Icenowy Zhenga6653772017-08-12 20:43:51 +0800125 if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
126 rate *= nkm->fixed_post_div;
127
Maxime Ripard10a8d9b2017-05-17 09:40:31 +0200128 ccu_nkm_find_best(*parent_rate, rate, &_nkm);
Maxime Riparddf6561e2016-06-29 21:05:32 +0200129
Icenowy Zhenga6653772017-08-12 20:43:51 +0800130 rate = *parent_rate * _nkm.n * _nkm.k / _nkm.m;
131
132 if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
133 rate /= nkm->fixed_post_div;
134
135 return rate;
Chen-Yu Tsaia3658352016-07-26 15:04:28 +0800136}
137
138static int ccu_nkm_determine_rate(struct clk_hw *hw,
139 struct clk_rate_request *req)
140{
141 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
142
143 return ccu_mux_helper_determine_rate(&nkm->common, &nkm->mux,
144 req, ccu_nkm_round_rate, nkm);
Maxime Riparddf6561e2016-06-29 21:05:32 +0200145}
146
147static int ccu_nkm_set_rate(struct clk_hw *hw, unsigned long rate,
148 unsigned long parent_rate)
149{
150 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
151 struct _ccu_nkm _nkm;
152 unsigned long flags;
153 u32 reg;
154
Icenowy Zhenga6653772017-08-12 20:43:51 +0800155 if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
156 rate *= nkm->fixed_post_div;
157
Chen-Yu Tsai4162c5c2017-03-24 16:33:05 +0800158 _nkm.min_n = nkm->n.min ?: 1;
Maxime Ripard0c3c8e12016-10-14 12:08:19 +0200159 _nkm.max_n = nkm->n.max ?: 1 << nkm->n.width;
Chen-Yu Tsai4162c5c2017-03-24 16:33:05 +0800160 _nkm.min_k = nkm->k.min ?: 1;
Maxime Ripard0c3c8e12016-10-14 12:08:19 +0200161 _nkm.max_k = nkm->k.max ?: 1 << nkm->k.width;
Maxime Ripard6e0d50d2016-09-29 22:57:26 +0200162 _nkm.min_m = 1;
Maxime Ripard87ba9e52016-09-06 12:29:04 +0200163 _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
Maxime Riparddf6561e2016-06-29 21:05:32 +0200164
165 ccu_nkm_find_best(parent_rate, rate, &_nkm);
166
167 spin_lock_irqsave(nkm->common.lock, flags);
168
169 reg = readl(nkm->common.base + nkm->common.reg);
170 reg &= ~GENMASK(nkm->n.width + nkm->n.shift - 1, nkm->n.shift);
171 reg &= ~GENMASK(nkm->k.width + nkm->k.shift - 1, nkm->k.shift);
172 reg &= ~GENMASK(nkm->m.width + nkm->m.shift - 1, nkm->m.shift);
173
Maxime Riparde66f81b2016-11-08 18:12:34 +0100174 reg |= (_nkm.n - nkm->n.offset) << nkm->n.shift;
175 reg |= (_nkm.k - nkm->k.offset) << nkm->k.shift;
176 reg |= (_nkm.m - nkm->m.offset) << nkm->m.shift;
Maxime Riparddf6561e2016-06-29 21:05:32 +0200177 writel(reg, nkm->common.base + nkm->common.reg);
178
179 spin_unlock_irqrestore(nkm->common.lock, flags);
180
181 ccu_helper_wait_for_lock(&nkm->common, nkm->lock);
182
183 return 0;
184}
185
Chen-Yu Tsaia3658352016-07-26 15:04:28 +0800186static u8 ccu_nkm_get_parent(struct clk_hw *hw)
187{
188 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
189
190 return ccu_mux_helper_get_parent(&nkm->common, &nkm->mux);
191}
192
193static int ccu_nkm_set_parent(struct clk_hw *hw, u8 index)
194{
195 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
196
197 return ccu_mux_helper_set_parent(&nkm->common, &nkm->mux, index);
198}
199
Maxime Riparddf6561e2016-06-29 21:05:32 +0200200const struct clk_ops ccu_nkm_ops = {
201 .disable = ccu_nkm_disable,
202 .enable = ccu_nkm_enable,
203 .is_enabled = ccu_nkm_is_enabled,
204
Chen-Yu Tsaia3658352016-07-26 15:04:28 +0800205 .get_parent = ccu_nkm_get_parent,
206 .set_parent = ccu_nkm_set_parent,
207
208 .determine_rate = ccu_nkm_determine_rate,
Maxime Riparddf6561e2016-06-29 21:05:32 +0200209 .recalc_rate = ccu_nkm_recalc_rate,
Maxime Riparddf6561e2016-06-29 21:05:32 +0200210 .set_rate = ccu_nkm_set_rate,
211};