2 * Driver for the ICST307 VCO clock found in the ARM Reference designs.
3 * We wrap the custom interface from <asm/hardware/icst.h> into the generic
6 * Copyright (C) 2012-2015 Linus Walleij
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * TODO: when all ARM reference designs are migrated to generic clocks, the
13 * ICST clock code from the ARM tree should probably be merged into this
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/export.h>
19 #include <linux/err.h>
20 #include <linux/clk-provider.h>
22 #include <linux/regmap.h>
23 #include <linux/mfd/syscon.h>
27 /* Magic unlocking token used on all Versatile boards */
28 #define VERSATILE_LOCK_VAL 0xA05F
31 * struct clk_icst - ICST VCO clock wrapper
32 * @hw: corresponding clock hardware entry
33 * @vcoreg: VCO register address
34 * @lockreg: VCO lock register address
35 * @params: parameters for this ICST instance
43 struct icst_params *params;
47 #define to_icst(_hw) container_of(_hw, struct clk_icst, hw)
50 * vco_get() - get ICST VCO settings from a certain ICST
51 * @icst: the ICST clock to get
52 * @vco: the VCO struct to return the value in
54 static int vco_get(struct clk_icst *icst, struct icst_vco *vco)
59 ret = regmap_read(icst->map, icst->vcoreg_off, &val);
63 vco->r = (val >> 9) & 0x7f;
64 vco->s = (val >> 16) & 03;
69 * vco_set() - commit changes to an ICST VCO
70 * @icst: the ICST clock to set
71 * @vco: the VCO struct to set the changes from
73 static int vco_set(struct clk_icst *icst, struct icst_vco vco)
78 ret = regmap_read(icst->map, icst->vcoreg_off, &val);
81 val |= vco.v | (vco.r << 9) | (vco.s << 16);
83 /* This magic unlocks the VCO so it can be controlled */
84 ret = regmap_write(icst->map, icst->lockreg_off, VERSATILE_LOCK_VAL);
87 ret = regmap_write(icst->map, icst->vcoreg_off, val);
90 /* This locks the VCO again */
91 ret = regmap_write(icst->map, icst->lockreg_off, 0);
97 static unsigned long icst_recalc_rate(struct clk_hw *hw,
98 unsigned long parent_rate)
100 struct clk_icst *icst = to_icst(hw);
105 icst->params->ref = parent_rate;
106 ret = vco_get(icst, &vco);
108 pr_err("ICST: could not get VCO setting\n");
111 icst->rate = icst_hz(icst->params, vco);
115 static long icst_round_rate(struct clk_hw *hw, unsigned long rate,
116 unsigned long *prate)
118 struct clk_icst *icst = to_icst(hw);
121 vco = icst_hz_to_vco(icst->params, rate);
122 return icst_hz(icst->params, vco);
125 static int icst_set_rate(struct clk_hw *hw, unsigned long rate,
126 unsigned long parent_rate)
128 struct clk_icst *icst = to_icst(hw);
132 icst->params->ref = parent_rate;
133 vco = icst_hz_to_vco(icst->params, rate);
134 icst->rate = icst_hz(icst->params, vco);
135 return vco_set(icst, vco);
138 static const struct clk_ops icst_ops = {
139 .recalc_rate = icst_recalc_rate,
140 .round_rate = icst_round_rate,
141 .set_rate = icst_set_rate,
144 static struct clk *icst_clk_setup(struct device *dev,
145 const struct clk_icst_desc *desc,
147 const char *parent_name,
151 struct clk_icst *icst;
152 struct clk_init_data init;
153 struct icst_params *pclone;
155 icst = kzalloc(sizeof(struct clk_icst), GFP_KERNEL);
157 pr_err("could not allocate ICST clock!\n");
158 return ERR_PTR(-ENOMEM);
161 pclone = kmemdup(desc->params, sizeof(*pclone), GFP_KERNEL);
164 pr_err("could not clone ICST params\n");
165 return ERR_PTR(-ENOMEM);
169 init.ops = &icst_ops;
170 init.flags = CLK_IS_ROOT;
171 init.parent_names = (parent_name ? &parent_name : NULL);
172 init.num_parents = (parent_name ? 1 : 0);
174 icst->hw.init = &init;
175 icst->params = pclone;
176 icst->vcoreg_off = desc->vco_offset;
177 icst->lockreg_off = desc->lock_offset;
179 clk = clk_register(dev, &icst->hw);
188 struct clk *icst_clk_register(struct device *dev,
189 const struct clk_icst_desc *desc,
191 const char *parent_name,
194 struct regmap_config icst_regmap_conf = {
201 map = regmap_init_mmio(dev, base, &icst_regmap_conf);
203 pr_err("could not initialize ICST regmap\n");
204 return ERR_CAST(map);
206 return icst_clk_setup(dev, desc, name, parent_name, map);
208 EXPORT_SYMBOL_GPL(icst_clk_register);
212 * In a device tree, an memory-mapped ICST clock appear as a child
213 * of a syscon node. Assume this and probe it only as a child of a
217 static const struct icst_params icst525_params = {
218 .vco_max = ICST525_VCO_MAX_5V,
219 .vco_min = ICST525_VCO_MIN,
224 .s2div = icst525_s2div,
225 .idx2s = icst525_idx2s,
228 static const struct icst_params icst307_params = {
229 .vco_max = ICST307_VCO_MAX,
230 .vco_min = ICST307_VCO_MIN,
235 .s2div = icst307_s2div,
236 .idx2s = icst307_idx2s,
239 static void __init of_syscon_icst_setup(struct device_node *np)
241 struct device_node *parent;
243 struct clk_icst_desc icst_desc;
244 const char *name = np->name;
245 const char *parent_name;
248 /* We do not release this reference, we are using it perpetually */
249 parent = of_get_parent(np);
251 pr_err("no parent node for syscon ICST clock\n");
254 map = syscon_node_to_regmap(parent);
256 pr_err("no regmap for syscon ICST clock parent\n");
260 if (of_property_read_u32(np, "vco-offset", &icst_desc.vco_offset)) {
261 pr_err("no VCO register offset for ICST clock\n");
264 if (of_property_read_u32(np, "lock-offset", &icst_desc.lock_offset)) {
265 pr_err("no lock register offset for ICST clock\n");
269 if (of_device_is_compatible(np, "arm,syscon-icst525"))
270 icst_desc.params = &icst525_params;
271 else if (of_device_is_compatible(np, "arm,syscon-icst307"))
272 icst_desc.params = &icst307_params;
274 pr_err("unknown ICST clock %s\n", name);
278 /* Parent clock name is not the same as node parent */
279 parent_name = of_clk_get_parent_name(np, 0);
281 regclk = icst_clk_setup(NULL, &icst_desc, name, parent_name, map);
282 if (IS_ERR(regclk)) {
283 pr_err("error setting up syscon ICST clock %s\n", name);
286 of_clk_add_provider(np, of_clk_src_simple_get, regclk);
287 pr_debug("registered syscon ICST clock %s\n", name);
290 CLK_OF_DECLARE(arm_syscon_icst525_clk,
291 "arm,syscon-icst525", of_syscon_icst_setup);
292 CLK_OF_DECLARE(arm_syscon_icst307_clk,
293 "arm,syscon-icst307", of_syscon_icst_setup);