1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
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>
15 #define SMD_DIV_SHIFT 8
16 #define SMD_MAX_DIV 0xf
18 struct at91sam9x5_clk_smd {
20 struct regmap *regmap;
23 #define to_at91sam9x5_clk_smd(hw) \
24 container_of(hw, struct at91sam9x5_clk_smd, hw)
26 static unsigned long at91sam9x5_clk_smd_recalc_rate(struct clk_hw *hw,
27 unsigned long parent_rate)
29 struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
33 regmap_read(smd->regmap, AT91_PMC_SMD, &smdr);
34 smddiv = (smdr & AT91_PMC_SMD_DIV) >> SMD_DIV_SHIFT;
36 return parent_rate / (smddiv + 1);
39 static int at91sam9x5_clk_smd_determine_rate(struct clk_hw *hw,
40 struct clk_rate_request *req)
43 unsigned long bestrate;
46 if (req->rate >= req->best_parent_rate) {
47 req->rate = req->best_parent_rate;
51 div = req->best_parent_rate / req->rate;
52 if (div > SMD_MAX_DIV) {
53 req->rate = req->best_parent_rate / (SMD_MAX_DIV + 1);
57 bestrate = req->best_parent_rate / div;
58 tmp = req->best_parent_rate / (div + 1);
59 if (bestrate - req->rate > req->rate - tmp)
66 static int at91sam9x5_clk_smd_set_parent(struct clk_hw *hw, u8 index)
68 struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
73 regmap_update_bits(smd->regmap, AT91_PMC_SMD, AT91_PMC_SMDS,
74 index ? AT91_PMC_SMDS : 0);
79 static u8 at91sam9x5_clk_smd_get_parent(struct clk_hw *hw)
81 struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
84 regmap_read(smd->regmap, AT91_PMC_SMD, &smdr);
86 return smdr & AT91_PMC_SMDS;
89 static int at91sam9x5_clk_smd_set_rate(struct clk_hw *hw, unsigned long rate,
90 unsigned long parent_rate)
92 struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
93 unsigned long div = parent_rate / rate;
95 if (parent_rate % rate || div < 1 || div > (SMD_MAX_DIV + 1))
98 regmap_update_bits(smd->regmap, AT91_PMC_SMD, AT91_PMC_SMD_DIV,
99 (div - 1) << SMD_DIV_SHIFT);
104 static const struct clk_ops at91sam9x5_smd_ops = {
105 .recalc_rate = at91sam9x5_clk_smd_recalc_rate,
106 .determine_rate = at91sam9x5_clk_smd_determine_rate,
107 .get_parent = at91sam9x5_clk_smd_get_parent,
108 .set_parent = at91sam9x5_clk_smd_set_parent,
109 .set_rate = at91sam9x5_clk_smd_set_rate,
112 struct clk_hw * __init
113 at91sam9x5_clk_register_smd(struct regmap *regmap, const char *name,
114 const char **parent_names, u8 num_parents)
116 struct at91sam9x5_clk_smd *smd;
118 struct clk_init_data init;
121 smd = kzalloc(sizeof(*smd), GFP_KERNEL);
123 return ERR_PTR(-ENOMEM);
126 init.ops = &at91sam9x5_smd_ops;
127 init.parent_names = parent_names;
128 init.num_parents = num_parents;
129 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
131 smd->hw.init = &init;
132 smd->regmap = regmap;
135 ret = clk_hw_register(NULL, &smd->hw);