2 * Copyright (C) 2016 Maxime Ripard
3 * Maxime Ripard <maxime.ripard@free-electrons.com>
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.
11 #include <linux/clk-provider.h>
17 unsigned long mult
, min
, max
;
20 static void ccu_mult_find_best(unsigned long parent
, unsigned long rate
,
21 struct _ccu_mult
*mult
)
25 _mult
= rate
/ parent
;
26 if (_mult
< mult
->min
)
29 if (_mult
> mult
->max
)
35 static unsigned long ccu_mult_round_rate(struct ccu_mux_internal
*mux
,
36 struct clk_hw
*parent
,
37 unsigned long *parent_rate
,
41 struct ccu_mult
*cm
= data
;
44 _cm
.min
= cm
->mult
.min
;
47 _cm
.max
= cm
->mult
.max
;
49 _cm
.max
= (1 << cm
->mult
.width
) + cm
->mult
.offset
- 1;
51 ccu_mult_find_best(*parent_rate
, rate
, &_cm
);
53 return *parent_rate
* _cm
.mult
;
56 static void ccu_mult_disable(struct clk_hw
*hw
)
58 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
60 return ccu_gate_helper_disable(&cm
->common
, cm
->enable
);
63 static int ccu_mult_enable(struct clk_hw
*hw
)
65 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
67 return ccu_gate_helper_enable(&cm
->common
, cm
->enable
);
70 static int ccu_mult_is_enabled(struct clk_hw
*hw
)
72 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
74 return ccu_gate_helper_is_enabled(&cm
->common
, cm
->enable
);
77 static unsigned long ccu_mult_recalc_rate(struct clk_hw
*hw
,
78 unsigned long parent_rate
)
80 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
84 if (ccu_frac_helper_is_enabled(&cm
->common
, &cm
->frac
))
85 return ccu_frac_helper_read_rate(&cm
->common
, &cm
->frac
);
87 reg
= readl(cm
->common
.base
+ cm
->common
.reg
);
88 val
= reg
>> cm
->mult
.shift
;
89 val
&= (1 << cm
->mult
.width
) - 1;
91 parent_rate
= ccu_mux_helper_apply_prediv(&cm
->common
, &cm
->mux
, -1,
94 return parent_rate
* (val
+ cm
->mult
.offset
);
97 static int ccu_mult_determine_rate(struct clk_hw
*hw
,
98 struct clk_rate_request
*req
)
100 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
102 return ccu_mux_helper_determine_rate(&cm
->common
, &cm
->mux
,
103 req
, ccu_mult_round_rate
, cm
);
106 static int ccu_mult_set_rate(struct clk_hw
*hw
, unsigned long rate
,
107 unsigned long parent_rate
)
109 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
110 struct _ccu_mult _cm
;
114 if (ccu_frac_helper_has_rate(&cm
->common
, &cm
->frac
, rate
)) {
115 ccu_frac_helper_enable(&cm
->common
, &cm
->frac
);
117 return ccu_frac_helper_set_rate(&cm
->common
, &cm
->frac
,
120 ccu_frac_helper_disable(&cm
->common
, &cm
->frac
);
123 parent_rate
= ccu_mux_helper_apply_prediv(&cm
->common
, &cm
->mux
, -1,
126 _cm
.min
= cm
->mult
.min
;
129 _cm
.max
= cm
->mult
.max
;
131 _cm
.max
= (1 << cm
->mult
.width
) + cm
->mult
.offset
- 1;
133 ccu_mult_find_best(parent_rate
, rate
, &_cm
);
135 spin_lock_irqsave(cm
->common
.lock
, flags
);
137 reg
= readl(cm
->common
.base
+ cm
->common
.reg
);
138 reg
&= ~GENMASK(cm
->mult
.width
+ cm
->mult
.shift
- 1, cm
->mult
.shift
);
139 reg
|= ((_cm
.mult
- cm
->mult
.offset
) << cm
->mult
.shift
);
141 writel(reg
, cm
->common
.base
+ cm
->common
.reg
);
143 spin_unlock_irqrestore(cm
->common
.lock
, flags
);
145 ccu_helper_wait_for_lock(&cm
->common
, cm
->lock
);
150 static u8
ccu_mult_get_parent(struct clk_hw
*hw
)
152 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
154 return ccu_mux_helper_get_parent(&cm
->common
, &cm
->mux
);
157 static int ccu_mult_set_parent(struct clk_hw
*hw
, u8 index
)
159 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
161 return ccu_mux_helper_set_parent(&cm
->common
, &cm
->mux
, index
);
164 const struct clk_ops ccu_mult_ops
= {
165 .disable
= ccu_mult_disable
,
166 .enable
= ccu_mult_enable
,
167 .is_enabled
= ccu_mult_is_enabled
,
169 .get_parent
= ccu_mult_get_parent
,
170 .set_parent
= ccu_mult_set_parent
,
172 .determine_rate
= ccu_mult_determine_rate
,
173 .recalc_rate
= ccu_mult_recalc_rate
,
174 .set_rate
= ccu_mult_set_rate
,