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>
16 static void ccu_mult_find_best(unsigned long parent
, unsigned long rate
,
17 unsigned int max_n
, unsigned int *n
)
22 static unsigned long ccu_mult_round_rate(struct ccu_mux_internal
*mux
,
23 unsigned long parent_rate
,
27 struct ccu_mult
*cm
= data
;
30 ccu_mult_find_best(parent_rate
, rate
, 1 << cm
->mult
.width
, &n
);
32 return parent_rate
* n
;
35 static void ccu_mult_disable(struct clk_hw
*hw
)
37 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
39 return ccu_gate_helper_disable(&cm
->common
, cm
->enable
);
42 static int ccu_mult_enable(struct clk_hw
*hw
)
44 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
46 return ccu_gate_helper_enable(&cm
->common
, cm
->enable
);
49 static int ccu_mult_is_enabled(struct clk_hw
*hw
)
51 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
53 return ccu_gate_helper_is_enabled(&cm
->common
, cm
->enable
);
56 static unsigned long ccu_mult_recalc_rate(struct clk_hw
*hw
,
57 unsigned long parent_rate
)
59 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
63 reg
= readl(cm
->common
.base
+ cm
->common
.reg
);
64 val
= reg
>> cm
->mult
.shift
;
65 val
&= (1 << cm
->mult
.width
) - 1;
67 ccu_mux_helper_adjust_parent_for_prediv(&cm
->common
, &cm
->mux
, -1,
70 return parent_rate
* (val
+ 1);
73 static int ccu_mult_determine_rate(struct clk_hw
*hw
,
74 struct clk_rate_request
*req
)
76 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
78 return ccu_mux_helper_determine_rate(&cm
->common
, &cm
->mux
,
79 req
, ccu_mult_round_rate
, cm
);
82 static int ccu_mult_set_rate(struct clk_hw
*hw
, unsigned long rate
,
83 unsigned long parent_rate
)
85 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
90 ccu_mux_helper_adjust_parent_for_prediv(&cm
->common
, &cm
->mux
, -1,
93 ccu_mult_find_best(parent_rate
, rate
, 1 << cm
->mult
.width
, &n
);
95 spin_lock_irqsave(cm
->common
.lock
, flags
);
97 reg
= readl(cm
->common
.base
+ cm
->common
.reg
);
98 reg
&= ~GENMASK(cm
->mult
.width
+ cm
->mult
.shift
- 1, cm
->mult
.shift
);
100 writel(reg
| ((n
- 1) << cm
->mult
.shift
),
101 cm
->common
.base
+ cm
->common
.reg
);
103 spin_unlock_irqrestore(cm
->common
.lock
, flags
);
108 static u8
ccu_mult_get_parent(struct clk_hw
*hw
)
110 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
112 return ccu_mux_helper_get_parent(&cm
->common
, &cm
->mux
);
115 static int ccu_mult_set_parent(struct clk_hw
*hw
, u8 index
)
117 struct ccu_mult
*cm
= hw_to_ccu_mult(hw
);
119 return ccu_mux_helper_set_parent(&cm
->common
, &cm
->mux
, index
);
122 const struct clk_ops ccu_mult_ops
= {
123 .disable
= ccu_mult_disable
,
124 .enable
= ccu_mult_enable
,
125 .is_enabled
= ccu_mult_is_enabled
,
127 .get_parent
= ccu_mult_get_parent
,
128 .set_parent
= ccu_mult_set_parent
,
130 .determine_rate
= ccu_mult_determine_rate
,
131 .recalc_rate
= ccu_mult_recalc_rate
,
132 .set_rate
= ccu_mult_set_rate
,