1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Maxime Ripard
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
7 #include <linux/clk-provider.h>
14 unsigned long mult
, min
, max
;
17 static void ccu_mult_find_best(unsigned long parent
, unsigned long rate
,
18 struct _ccu_mult
*mult
)
22 _mult
= rate
/ parent
;
23 if (_mult
< mult
->min
)
26 if (_mult
> mult
->max
)
32 static unsigned long ccu_mult_round_rate(struct ccu_mux_internal
*mux
,
33 struct clk_hw
*parent
,
34 unsigned long *parent_rate
,
38 struct ccu_mult
*cm
= data
;
41 _cm
.min
= cm
->mult
.min
;
44 _cm
.max
= cm
->mult
.max
;
46 _cm
.max
= (1 << cm
->mult
.width
) + cm
->mult
.offset
- 1;
48 ccu_mult_find_best(*parent_rate
, rate
, &_cm
);
50 return *parent_rate
* _cm
.mult
;
53 static void ccu_mult_disable(struct clk_hw
*hw
)
55 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
57 return ccu_gate_helper_disable(&cm
->common
, cm
->enable
);
60 static int ccu_mult_enable(struct clk_hw
*hw
)
62 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
64 return ccu_gate_helper_enable(&cm
->common
, cm
->enable
);
67 static int ccu_mult_is_enabled(struct clk_hw
*hw
)
69 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
71 return ccu_gate_helper_is_enabled(&cm
->common
, cm
->enable
);
74 static unsigned long ccu_mult_recalc_rate(struct clk_hw
*hw
,
75 unsigned long parent_rate
)
77 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
81 if (ccu_frac_helper_is_enabled(&cm
->common
, &cm
->frac
))
82 return ccu_frac_helper_read_rate(&cm
->common
, &cm
->frac
);
84 reg
= readl(cm
->common
.base
+ cm
->common
.reg
);
85 val
= reg
>> cm
->mult
.shift
;
86 val
&= (1 << cm
->mult
.width
) - 1;
88 parent_rate
= ccu_mux_helper_apply_prediv(&cm
->common
, &cm
->mux
, -1,
91 return parent_rate
* (val
+ cm
->mult
.offset
);
94 static int ccu_mult_determine_rate(struct clk_hw
*hw
,
95 struct clk_rate_request
*req
)
97 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
99 return ccu_mux_helper_determine_rate(&cm
->common
, &cm
->mux
,
100 req
, ccu_mult_round_rate
, cm
);
103 static int ccu_mult_set_rate(struct clk_hw
*hw
, unsigned long rate
,
104 unsigned long parent_rate
)
106 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
107 struct _ccu_mult _cm
;
111 if (ccu_frac_helper_has_rate(&cm
->common
, &cm
->frac
, rate
)) {
112 ccu_frac_helper_enable(&cm
->common
, &cm
->frac
);
114 return ccu_frac_helper_set_rate(&cm
->common
, &cm
->frac
,
117 ccu_frac_helper_disable(&cm
->common
, &cm
->frac
);
120 parent_rate
= ccu_mux_helper_apply_prediv(&cm
->common
, &cm
->mux
, -1,
123 _cm
.min
= cm
->mult
.min
;
126 _cm
.max
= cm
->mult
.max
;
128 _cm
.max
= (1 << cm
->mult
.width
) + cm
->mult
.offset
- 1;
130 ccu_mult_find_best(parent_rate
, rate
, &_cm
);
132 spin_lock_irqsave(cm
->common
.lock
, flags
);
134 reg
= readl(cm
->common
.base
+ cm
->common
.reg
);
135 reg
&= ~GENMASK(cm
->mult
.width
+ cm
->mult
.shift
- 1, cm
->mult
.shift
);
136 reg
|= ((_cm
.mult
- cm
->mult
.offset
) << cm
->mult
.shift
);
138 writel(reg
, cm
->common
.base
+ cm
->common
.reg
);
140 spin_unlock_irqrestore(cm
->common
.lock
, flags
);
142 ccu_helper_wait_for_lock(&cm
->common
, cm
->lock
);
147 static u8
ccu_mult_get_parent(struct clk_hw
*hw
)
149 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
151 return ccu_mux_helper_get_parent(&cm
->common
, &cm
->mux
);
154 static int ccu_mult_set_parent(struct clk_hw
*hw
, u8 index
)
156 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
158 return ccu_mux_helper_set_parent(&cm
->common
, &cm
->mux
, index
);
161 const struct clk_ops ccu_mult_ops
= {
162 .disable
= ccu_mult_disable
,
163 .enable
= ccu_mult_enable
,
164 .is_enabled
= ccu_mult_is_enabled
,
166 .get_parent
= ccu_mult_get_parent
,
167 .set_parent
= ccu_mult_set_parent
,
169 .determine_rate
= ccu_mult_determine_rate
,
170 .recalc_rate
= ccu_mult_recalc_rate
,
171 .set_rate
= ccu_mult_set_rate
,
173 EXPORT_SYMBOL_NS_GPL(ccu_mult_ops
, "SUNXI_CCU");