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>
12 #include <linux/rational.h>
18 static void ccu_nm_disable(struct clk_hw
*hw
)
20 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
22 return ccu_gate_helper_disable(&nm
->common
, nm
->enable
);
25 static int ccu_nm_enable(struct clk_hw
*hw
)
27 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
29 return ccu_gate_helper_enable(&nm
->common
, nm
->enable
);
32 static int ccu_nm_is_enabled(struct clk_hw
*hw
)
34 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
36 return ccu_gate_helper_is_enabled(&nm
->common
, nm
->enable
);
39 static unsigned long ccu_nm_recalc_rate(struct clk_hw
*hw
,
40 unsigned long parent_rate
)
42 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
46 if (ccu_frac_helper_is_enabled(&nm
->common
, &nm
->frac
))
47 return ccu_frac_helper_read_rate(&nm
->common
, &nm
->frac
);
49 reg
= readl(nm
->common
.base
+ nm
->common
.reg
);
51 n
= reg
>> nm
->n
.shift
;
52 n
&= (1 << nm
->n
.width
) - 1;
54 m
= reg
>> nm
->m
.shift
;
55 m
&= (1 << nm
->m
.width
) - 1;
57 return parent_rate
* (n
+ 1) / (m
+ 1);
60 static long ccu_nm_round_rate(struct clk_hw
*hw
, unsigned long rate
,
61 unsigned long *parent_rate
)
63 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
64 unsigned long max_n
, max_m
;
67 max_n
= 1 << nm
->n
.width
;
68 max_m
= nm
->m
.max
?: 1 << nm
->m
.width
;
70 rational_best_approximation(rate
, *parent_rate
, max_n
, max_m
, &n
, &m
);
72 return *parent_rate
* n
/ m
;
75 static int ccu_nm_set_rate(struct clk_hw
*hw
, unsigned long rate
,
76 unsigned long parent_rate
)
78 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
80 unsigned long max_n
, max_m
;
84 if (ccu_frac_helper_has_rate(&nm
->common
, &nm
->frac
, rate
))
85 return ccu_frac_helper_set_rate(&nm
->common
, &nm
->frac
, rate
);
87 ccu_frac_helper_disable(&nm
->common
, &nm
->frac
);
89 max_n
= 1 << nm
->n
.width
;
90 max_m
= nm
->m
.max
?: 1 << nm
->m
.width
;
92 rational_best_approximation(rate
, parent_rate
, max_n
, max_m
, &n
, &m
);
94 spin_lock_irqsave(nm
->common
.lock
, flags
);
96 reg
= readl(nm
->common
.base
+ nm
->common
.reg
);
97 reg
&= ~GENMASK(nm
->n
.width
+ nm
->n
.shift
- 1, nm
->n
.shift
);
98 reg
&= ~GENMASK(nm
->m
.width
+ nm
->m
.shift
- 1, nm
->m
.shift
);
100 writel(reg
| ((m
- 1) << nm
->m
.shift
) | ((n
- 1) << nm
->n
.shift
),
101 nm
->common
.base
+ nm
->common
.reg
);
103 spin_unlock_irqrestore(nm
->common
.lock
, flags
);
105 ccu_helper_wait_for_lock(&nm
->common
, nm
->lock
);
110 const struct clk_ops ccu_nm_ops
= {
111 .disable
= ccu_nm_disable
,
112 .enable
= ccu_nm_enable
,
113 .is_enabled
= ccu_nm_is_enabled
,
115 .recalc_rate
= ccu_nm_recalc_rate
,
116 .round_rate
= ccu_nm_round_rate
,
117 .set_rate
= ccu_nm_set_rate
,