1 // SPDX-License-Identifier: GPL-2.0-or-later
6 #include <linux/clk-provider.h>
7 #include <linux/clkdev.h>
8 #include <linux/clk/at91_pmc.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/regmap.h>
17 #define PROG_STATUS_MASK(id) (1 << ((id) + 8))
18 #define PROG_PRES(layout, pckr) ((pckr >> layout->pres_shift) & layout->pres_mask)
19 #define PROG_MAX_RM9200_CSS 3
21 struct clk_programmable {
23 struct regmap *regmap;
25 const struct clk_programmable_layout *layout;
28 #define to_clk_programmable(hw) container_of(hw, struct clk_programmable, hw)
30 static unsigned long clk_programmable_recalc_rate(struct clk_hw *hw,
31 unsigned long parent_rate)
33 struct clk_programmable *prog = to_clk_programmable(hw);
34 const struct clk_programmable_layout *layout = prog->layout;
38 regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
40 if (layout->is_pres_direct)
41 rate = parent_rate / (PROG_PRES(layout, pckr) + 1);
43 rate = parent_rate >> PROG_PRES(layout, pckr);
48 static int clk_programmable_determine_rate(struct clk_hw *hw,
49 struct clk_rate_request *req)
51 struct clk_programmable *prog = to_clk_programmable(hw);
52 const struct clk_programmable_layout *layout = prog->layout;
53 struct clk_hw *parent;
54 long best_rate = -EINVAL;
55 unsigned long parent_rate;
56 unsigned long tmp_rate = 0;
60 for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
61 parent = clk_hw_get_parent_by_index(hw, i);
65 parent_rate = clk_hw_get_rate(parent);
66 if (layout->is_pres_direct) {
67 for (shift = 0; shift <= layout->pres_mask; shift++) {
68 tmp_rate = parent_rate / (shift + 1);
69 if (tmp_rate <= req->rate)
73 for (shift = 0; shift < layout->pres_mask; shift++) {
74 tmp_rate = parent_rate >> shift;
75 if (tmp_rate <= req->rate)
80 if (tmp_rate > req->rate)
84 (req->rate - tmp_rate) < (req->rate - best_rate)) {
86 req->best_parent_rate = parent_rate;
87 req->best_parent_hw = parent;
97 req->rate = best_rate;
101 static int clk_programmable_set_parent(struct clk_hw *hw, u8 index)
103 struct clk_programmable *prog = to_clk_programmable(hw);
104 const struct clk_programmable_layout *layout = prog->layout;
105 unsigned int mask = layout->css_mask;
106 unsigned int pckr = index;
108 if (layout->have_slck_mck)
109 mask |= AT91_PMC_CSSMCK_MCK;
111 if (index > layout->css_mask) {
112 if (index > PROG_MAX_RM9200_CSS && !layout->have_slck_mck)
115 pckr |= AT91_PMC_CSSMCK_MCK;
118 regmap_update_bits(prog->regmap, AT91_PMC_PCKR(prog->id), mask, pckr);
123 static u8 clk_programmable_get_parent(struct clk_hw *hw)
125 struct clk_programmable *prog = to_clk_programmable(hw);
126 const struct clk_programmable_layout *layout = prog->layout;
130 regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
132 ret = pckr & layout->css_mask;
134 if (layout->have_slck_mck && (pckr & AT91_PMC_CSSMCK_MCK) && !ret)
135 ret = PROG_MAX_RM9200_CSS + 1;
140 static int clk_programmable_set_rate(struct clk_hw *hw, unsigned long rate,
141 unsigned long parent_rate)
143 struct clk_programmable *prog = to_clk_programmable(hw);
144 const struct clk_programmable_layout *layout = prog->layout;
145 unsigned long div = parent_rate / rate;
151 if (layout->is_pres_direct) {
154 if (shift > layout->pres_mask)
157 shift = fls(div) - 1;
159 if (div != (1 << shift))
162 if (shift >= layout->pres_mask)
166 regmap_update_bits(prog->regmap, AT91_PMC_PCKR(prog->id),
167 layout->pres_mask << layout->pres_shift,
168 shift << layout->pres_shift);
173 static const struct clk_ops programmable_ops = {
174 .recalc_rate = clk_programmable_recalc_rate,
175 .determine_rate = clk_programmable_determine_rate,
176 .get_parent = clk_programmable_get_parent,
177 .set_parent = clk_programmable_set_parent,
178 .set_rate = clk_programmable_set_rate,
181 struct clk_hw * __init
182 at91_clk_register_programmable(struct regmap *regmap,
183 const char *name, const char **parent_names,
184 u8 num_parents, u8 id,
185 const struct clk_programmable_layout *layout)
187 struct clk_programmable *prog;
189 struct clk_init_data init;
192 if (id > PROG_ID_MAX)
193 return ERR_PTR(-EINVAL);
195 prog = kzalloc(sizeof(*prog), GFP_KERNEL);
197 return ERR_PTR(-ENOMEM);
200 init.ops = &programmable_ops;
201 init.parent_names = parent_names;
202 init.num_parents = num_parents;
203 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
206 prog->layout = layout;
207 prog->hw.init = &init;
208 prog->regmap = regmap;
211 ret = clk_hw_register(NULL, &prog->hw);
216 pmc_register_pck(id);
222 const struct clk_programmable_layout at91rm9200_programmable_layout = {
230 const struct clk_programmable_layout at91sam9g45_programmable_layout = {
238 const struct clk_programmable_layout at91sam9x5_programmable_layout = {