aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaxime Ripard <maxime.ripard@free-electrons.com>2016-06-29 15:05:30 -0400
committerMichael Turquette <mturquette@baylibre.com>2016-07-08 21:04:56 -0400
commitadbfb0056e03d556b98ffe93da0d7126bd630096 (patch)
treea69864c7f6532408c6c4991cf06eb4988936fa01
parent2ab836db5097427c3bfa58f9bd7230513c2f3665 (diff)
clk: sunxi-ng: Add N-K-factor clock support
Introduce support for clocks that use a combination of two linear multipliers. Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com> Signed-off-by: Michael Turquette <mturquette@baylibre.com> Link: lkml.kernel.org/r/20160629190535.11855-10-maxime.ripard@free-electrons.com
-rw-r--r--drivers/clk/sunxi-ng/Kconfig4
-rw-r--r--drivers/clk/sunxi-ng/Makefile1
-rw-r--r--drivers/clk/sunxi-ng/ccu_nk.c147
-rw-r--r--drivers/clk/sunxi-ng/ccu_nk.h71
4 files changed, 223 insertions, 0 deletions
diff --git a/drivers/clk/sunxi-ng/Kconfig b/drivers/clk/sunxi-ng/Kconfig
index fc970b5a28c6..2eb0fc6b41d5 100644
--- a/drivers/clk/sunxi-ng/Kconfig
+++ b/drivers/clk/sunxi-ng/Kconfig
@@ -24,6 +24,10 @@ config SUNXI_CCU_PHASE
24 24
25# Multi-factor clocks 25# Multi-factor clocks
26 26
27config SUNXI_CCU_NK
28 bool
29 select SUNXI_CCU_GATE
30
27config SUNXI_CCU_MP 31config SUNXI_CCU_MP
28 bool 32 bool
29 select SUNXI_CCU_GATE 33 select SUNXI_CCU_GATE
diff --git a/drivers/clk/sunxi-ng/Makefile b/drivers/clk/sunxi-ng/Makefile
index 501dec9451f4..22951e60f876 100644
--- a/drivers/clk/sunxi-ng/Makefile
+++ b/drivers/clk/sunxi-ng/Makefile
@@ -10,4 +10,5 @@ obj-$(CONFIG_SUNXI_CCU_MUX) += ccu_mux.o
10obj-$(CONFIG_SUNXI_CCU_PHASE) += ccu_phase.o 10obj-$(CONFIG_SUNXI_CCU_PHASE) += ccu_phase.o
11 11
12# Multi-factor clocks 12# Multi-factor clocks
13obj-$(CONFIG_SUNXI_CCU_NK) += ccu_nk.o
13obj-$(CONFIG_SUNXI_CCU_MP) += ccu_mp.o 14obj-$(CONFIG_SUNXI_CCU_MP) += ccu_mp.o
diff --git a/drivers/clk/sunxi-ng/ccu_nk.c b/drivers/clk/sunxi-ng/ccu_nk.c
new file mode 100644
index 000000000000..4470ffc8cf0d
--- /dev/null
+++ b/drivers/clk/sunxi-ng/ccu_nk.c
@@ -0,0 +1,147 @@
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#include <linux/rational.h>
13
14#include "ccu_gate.h"
15#include "ccu_nk.h"
16
17void ccu_nk_find_best(unsigned long parent, unsigned long rate,
18 unsigned int max_n, unsigned int max_k,
19 unsigned int *n, unsigned int *k)
20{
21 unsigned long best_rate = 0;
22 unsigned int best_k = 0, best_n = 0;
23 unsigned int _k, _n;
24
25 for (_k = 1; _k <= max_k; _k++) {
26 for (_n = 1; _n <= max_n; _n++) {
27 unsigned long 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 *k = best_k;
41 *n = best_n;
42}
43
44static 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
51static 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
58static 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
65static 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 rate, n, k;
70 u32 reg;
71
72 reg = readl(nk->common.base + nk->common.reg);
73
74 n = reg >> nk->n.shift;
75 n &= (1 << nk->n.width) - 1;
76
77 k = reg >> nk->k.shift;
78 k &= (1 << nk->k.width) - 1;
79
80 rate = parent_rate * (n + 1) * (k + 1);
81
82 if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
83 rate /= nk->fixed_post_div;
84
85 return rate;
86}
87
88static long ccu_nk_round_rate(struct clk_hw *hw, unsigned long rate,
89 unsigned long *parent_rate)
90{
91 struct ccu_nk *nk = hw_to_ccu_nk(hw);
92 unsigned int n, k;
93
94 if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
95 rate *= nk->fixed_post_div;
96
97 ccu_nk_find_best(*parent_rate, rate,
98 1 << nk->n.width, 1 << nk->k.width,
99 &n, &k);
100
101 rate = *parent_rate * n * k;
102 if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
103 rate = rate / nk->fixed_post_div;
104
105 return rate;
106}
107
108static int ccu_nk_set_rate(struct clk_hw *hw, unsigned long rate,
109 unsigned long parent_rate)
110{
111 struct ccu_nk *nk = hw_to_ccu_nk(hw);
112 unsigned long flags;
113 unsigned int n, k;
114 u32 reg;
115
116 if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
117 rate = rate * nk->fixed_post_div;
118
119 ccu_nk_find_best(parent_rate, rate,
120 1 << nk->n.width, 1 << nk->k.width,
121 &n, &k);
122
123 spin_lock_irqsave(nk->common.lock, flags);
124
125 reg = readl(nk->common.base + nk->common.reg);
126 reg &= ~GENMASK(nk->n.width + nk->n.shift - 1, nk->n.shift);
127 reg &= ~GENMASK(nk->k.width + nk->k.shift - 1, nk->k.shift);
128
129 writel(reg | ((k - 1) << nk->k.shift) | ((n - 1) << nk->n.shift),
130 nk->common.base + nk->common.reg);
131
132 spin_unlock_irqrestore(nk->common.lock, flags);
133
134 ccu_helper_wait_for_lock(&nk->common, nk->lock);
135
136 return 0;
137}
138
139const struct clk_ops ccu_nk_ops = {
140 .disable = ccu_nk_disable,
141 .enable = ccu_nk_enable,
142 .is_enabled = ccu_nk_is_enabled,
143
144 .recalc_rate = ccu_nk_recalc_rate,
145 .round_rate = ccu_nk_round_rate,
146 .set_rate = ccu_nk_set_rate,
147};
diff --git a/drivers/clk/sunxi-ng/ccu_nk.h b/drivers/clk/sunxi-ng/ccu_nk.h
new file mode 100644
index 000000000000..4b52da0c29fe
--- /dev/null
+++ b/drivers/clk/sunxi-ng/ccu_nk.h
@@ -0,0 +1,71 @@
1/*
2 * Copyright (c) 2016 Maxime Ripard. All rights reserved.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#ifndef _CCU_NK_H_
15#define _CCU_NK_H_
16
17#include <linux/clk-provider.h>
18
19#include "ccu_common.h"
20#include "ccu_div.h"
21#include "ccu_mult.h"
22
23/*
24 * struct ccu_nk - Definition of an N-K clock
25 *
26 * Clocks based on the formula parent * N * K
27 */
28struct ccu_nk {
29 u16 reg;
30 u32 enable;
31 u32 lock;
32
33 struct _ccu_mult n;
34 struct _ccu_mult k;
35
36 unsigned int fixed_post_div;
37
38 struct ccu_common common;
39};
40
41#define SUNXI_CCU_NK_WITH_GATE_LOCK_POSTDIV(_struct, _name, _parent, _reg, \
42 _nshift, _nwidth, \
43 _kshift, _kwidth, \
44 _gate, _lock, _postdiv, \
45 _flags) \
46 struct ccu_nk _struct = { \
47 .enable = _gate, \
48 .lock = _lock, \
49 .k = _SUNXI_CCU_MULT(_kshift, _kwidth), \
50 .n = _SUNXI_CCU_MULT(_nshift, _nwidth), \
51 .fixed_post_div = _postdiv, \
52 .common = { \
53 .reg = _reg, \
54 .features = CCU_FEATURE_FIXED_POSTDIV, \
55 .hw.init = CLK_HW_INIT(_name, \
56 _parent, \
57 &ccu_nk_ops, \
58 _flags), \
59 }, \
60 }
61
62static inline struct ccu_nk *hw_to_ccu_nk(struct clk_hw *hw)
63{
64 struct ccu_common *common = hw_to_ccu_common(hw);
65
66 return container_of(common, struct ccu_nk, common);
67}
68
69extern const struct clk_ops ccu_nk_ops;
70
71#endif /* _CCU_NK_H_ */