blob: e62f8cb2c9b53ec4f22811549878ad844e24b612 [file] [log] [blame]
Linus Walleij91b87a42012-06-11 17:29:54 +02001/*
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
4 * clock framework.
5 *
Linus Walleijd4308192015-10-13 14:29:54 +02006 * Copyright (C) 2012-2015 Linus Walleij
Linus Walleij401301c2012-11-20 22:39:31 +01007 *
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.
11 *
Linus Walleij91b87a42012-06-11 17:29:54 +020012 * 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
14 * file.
15 */
Stephen Boyd6d31e3b22015-06-19 15:00:46 -070016#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/export.h>
Linus Walleij91b87a42012-06-11 17:29:54 +020019#include <linux/err.h>
20#include <linux/clk-provider.h>
Linus Walleij7a9ad672012-11-20 23:01:04 +010021#include <linux/io.h>
Linus Walleij179c8fb2015-10-12 15:52:50 +020022#include <linux/regmap.h>
Linus Walleij384d9772015-10-12 16:14:28 +020023#include <linux/mfd/syscon.h>
Linus Walleij91b87a42012-06-11 17:29:54 +020024
25#include "clk-icst.h"
26
Linus Walleij179c8fb2015-10-12 15:52:50 +020027/* Magic unlocking token used on all Versatile boards */
28#define VERSATILE_LOCK_VAL 0xA05F
29
Linus Walleij91b87a42012-06-11 17:29:54 +020030/**
31 * struct clk_icst - ICST VCO clock wrapper
32 * @hw: corresponding clock hardware entry
Linus Walleij7a9ad672012-11-20 23:01:04 +010033 * @vcoreg: VCO register address
34 * @lockreg: VCO lock register address
Linus Walleij91b87a42012-06-11 17:29:54 +020035 * @params: parameters for this ICST instance
36 * @rate: current rate
Linus Walleij91b87a42012-06-11 17:29:54 +020037 */
38struct clk_icst {
39 struct clk_hw hw;
Linus Walleij179c8fb2015-10-12 15:52:50 +020040 struct regmap *map;
41 u32 vcoreg_off;
42 u32 lockreg_off;
Linus Walleija183da62014-01-20 21:46:46 +010043 struct icst_params *params;
Linus Walleij91b87a42012-06-11 17:29:54 +020044 unsigned long rate;
Linus Walleij91b87a42012-06-11 17:29:54 +020045};
46
47#define to_icst(_hw) container_of(_hw, struct clk_icst, hw)
48
Linus Walleij7a9ad672012-11-20 23:01:04 +010049/**
Linus Walleij179c8fb2015-10-12 15:52:50 +020050 * 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
Linus Walleij7a9ad672012-11-20 23:01:04 +010053 */
Linus Walleij179c8fb2015-10-12 15:52:50 +020054static int vco_get(struct clk_icst *icst, struct icst_vco *vco)
Linus Walleij7a9ad672012-11-20 23:01:04 +010055{
56 u32 val;
Linus Walleij179c8fb2015-10-12 15:52:50 +020057 int ret;
Linus Walleij7a9ad672012-11-20 23:01:04 +010058
Linus Walleij179c8fb2015-10-12 15:52:50 +020059 ret = regmap_read(icst->map, icst->vcoreg_off, &val);
60 if (ret)
61 return ret;
62 vco->v = val & 0x1ff;
63 vco->r = (val >> 9) & 0x7f;
64 vco->s = (val >> 16) & 03;
65 return 0;
Linus Walleij7a9ad672012-11-20 23:01:04 +010066}
67
68/**
69 * vco_set() - commit changes to an ICST VCO
Linus Walleij179c8fb2015-10-12 15:52:50 +020070 * @icst: the ICST clock to set
71 * @vco: the VCO struct to set the changes from
Linus Walleij7a9ad672012-11-20 23:01:04 +010072 */
Linus Walleij179c8fb2015-10-12 15:52:50 +020073static int vco_set(struct clk_icst *icst, struct icst_vco vco)
Linus Walleij7a9ad672012-11-20 23:01:04 +010074{
75 u32 val;
Linus Walleij179c8fb2015-10-12 15:52:50 +020076 int ret;
Linus Walleij7a9ad672012-11-20 23:01:04 +010077
Linus Walleij179c8fb2015-10-12 15:52:50 +020078 ret = regmap_read(icst->map, icst->vcoreg_off, &val);
79 if (ret)
80 return ret;
Linus Walleij7a9ad672012-11-20 23:01:04 +010081 val |= vco.v | (vco.r << 9) | (vco.s << 16);
82
83 /* This magic unlocks the VCO so it can be controlled */
Linus Walleij179c8fb2015-10-12 15:52:50 +020084 ret = regmap_write(icst->map, icst->lockreg_off, VERSATILE_LOCK_VAL);
85 if (ret)
86 return ret;
87 ret = regmap_write(icst->map, icst->vcoreg_off, val);
88 if (ret)
89 return ret;
Linus Walleij7a9ad672012-11-20 23:01:04 +010090 /* This locks the VCO again */
Linus Walleij179c8fb2015-10-12 15:52:50 +020091 ret = regmap_write(icst->map, icst->lockreg_off, 0);
92 if (ret)
93 return ret;
94 return 0;
Linus Walleij7a9ad672012-11-20 23:01:04 +010095}
96
Linus Walleij91b87a42012-06-11 17:29:54 +020097static unsigned long icst_recalc_rate(struct clk_hw *hw,
98 unsigned long parent_rate)
99{
100 struct clk_icst *icst = to_icst(hw);
101 struct icst_vco vco;
Linus Walleij179c8fb2015-10-12 15:52:50 +0200102 int ret;
Linus Walleij91b87a42012-06-11 17:29:54 +0200103
Linus Walleija183da62014-01-20 21:46:46 +0100104 if (parent_rate)
105 icst->params->ref = parent_rate;
Linus Walleij179c8fb2015-10-12 15:52:50 +0200106 ret = vco_get(icst, &vco);
107 if (ret) {
108 pr_err("ICST: could not get VCO setting\n");
109 return 0;
110 }
Linus Walleij91b87a42012-06-11 17:29:54 +0200111 icst->rate = icst_hz(icst->params, vco);
112 return icst->rate;
113}
114
115static long icst_round_rate(struct clk_hw *hw, unsigned long rate,
116 unsigned long *prate)
117{
118 struct clk_icst *icst = to_icst(hw);
119 struct icst_vco vco;
120
121 vco = icst_hz_to_vco(icst->params, rate);
122 return icst_hz(icst->params, vco);
123}
124
125static int icst_set_rate(struct clk_hw *hw, unsigned long rate,
126 unsigned long parent_rate)
127{
128 struct clk_icst *icst = to_icst(hw);
129 struct icst_vco vco;
130
Linus Walleija183da62014-01-20 21:46:46 +0100131 if (parent_rate)
132 icst->params->ref = parent_rate;
Linus Walleij91b87a42012-06-11 17:29:54 +0200133 vco = icst_hz_to_vco(icst->params, rate);
134 icst->rate = icst_hz(icst->params, vco);
Linus Walleij179c8fb2015-10-12 15:52:50 +0200135 return vco_set(icst, vco);
Linus Walleij91b87a42012-06-11 17:29:54 +0200136}
137
138static const struct clk_ops icst_ops = {
139 .recalc_rate = icst_recalc_rate,
140 .round_rate = icst_round_rate,
141 .set_rate = icst_set_rate,
142};
143
Linus Walleij384d9772015-10-12 16:14:28 +0200144static struct clk *icst_clk_setup(struct device *dev,
145 const struct clk_icst_desc *desc,
146 const char *name,
147 const char *parent_name,
148 struct regmap *map)
Linus Walleij91b87a42012-06-11 17:29:54 +0200149{
150 struct clk *clk;
151 struct clk_icst *icst;
152 struct clk_init_data init;
Linus Walleija183da62014-01-20 21:46:46 +0100153 struct icst_params *pclone;
Linus Walleij91b87a42012-06-11 17:29:54 +0200154
155 icst = kzalloc(sizeof(struct clk_icst), GFP_KERNEL);
156 if (!icst) {
157 pr_err("could not allocate ICST clock!\n");
158 return ERR_PTR(-ENOMEM);
159 }
Linus Walleija183da62014-01-20 21:46:46 +0100160
161 pclone = kmemdup(desc->params, sizeof(*pclone), GFP_KERNEL);
162 if (!pclone) {
Colin Ian Kingab7ad352014-04-12 18:59:14 +0100163 kfree(icst);
Linus Walleija183da62014-01-20 21:46:46 +0100164 pr_err("could not clone ICST params\n");
165 return ERR_PTR(-ENOMEM);
166 }
167
Linus Walleijae6e6942013-11-22 11:30:05 +0100168 init.name = name;
Linus Walleij91b87a42012-06-11 17:29:54 +0200169 init.ops = &icst_ops;
170 init.flags = CLK_IS_ROOT;
Linus Walleija183da62014-01-20 21:46:46 +0100171 init.parent_names = (parent_name ? &parent_name : NULL);
172 init.num_parents = (parent_name ? 1 : 0);
Linus Walleij384d9772015-10-12 16:14:28 +0200173 icst->map = map;
Linus Walleij91b87a42012-06-11 17:29:54 +0200174 icst->hw.init = &init;
Linus Walleija183da62014-01-20 21:46:46 +0100175 icst->params = pclone;
Linus Walleij179c8fb2015-10-12 15:52:50 +0200176 icst->vcoreg_off = desc->vco_offset;
177 icst->lockreg_off = desc->lock_offset;
Linus Walleij91b87a42012-06-11 17:29:54 +0200178
179 clk = clk_register(dev, &icst->hw);
Linus Walleij7bdccef2015-10-23 11:36:01 +0200180 if (IS_ERR(clk)) {
181 kfree(pclone);
Linus Walleij91b87a42012-06-11 17:29:54 +0200182 kfree(icst);
Linus Walleij7bdccef2015-10-23 11:36:01 +0200183 }
Linus Walleij91b87a42012-06-11 17:29:54 +0200184
185 return clk;
186}
Linus Walleij384d9772015-10-12 16:14:28 +0200187
188struct clk *icst_clk_register(struct device *dev,
189 const struct clk_icst_desc *desc,
190 const char *name,
191 const char *parent_name,
192 void __iomem *base)
193{
194 struct regmap_config icst_regmap_conf = {
195 .reg_bits = 32,
196 .val_bits = 32,
197 .reg_stride = 4,
198 };
199 struct regmap *map;
200
201 map = regmap_init_mmio(dev, base, &icst_regmap_conf);
202 if (IS_ERR(map)) {
203 pr_err("could not initialize ICST regmap\n");
204 return ERR_CAST(map);
205 }
206 return icst_clk_setup(dev, desc, name, parent_name, map);
207}
Arnd Bergmanna218d7f2014-05-08 16:56:16 +0200208EXPORT_SYMBOL_GPL(icst_clk_register);
Linus Walleijd4308192015-10-13 14:29:54 +0200209
210#ifdef CONFIG_OF
211/*
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
214 * syscon.
215 */
216
217static const struct icst_params icst525_params = {
218 .vco_max = ICST525_VCO_MAX_5V,
219 .vco_min = ICST525_VCO_MIN,
220 .vd_min = 8,
221 .vd_max = 263,
222 .rd_min = 3,
223 .rd_max = 65,
224 .s2div = icst525_s2div,
225 .idx2s = icst525_idx2s,
226};
227
228static const struct icst_params icst307_params = {
229 .vco_max = ICST307_VCO_MAX,
230 .vco_min = ICST307_VCO_MIN,
231 .vd_min = 4 + 8,
232 .vd_max = 511 + 8,
233 .rd_min = 1 + 2,
234 .rd_max = 127 + 2,
235 .s2div = icst307_s2div,
236 .idx2s = icst307_idx2s,
237};
238
239static void __init of_syscon_icst_setup(struct device_node *np)
240{
241 struct device_node *parent;
242 struct regmap *map;
243 struct clk_icst_desc icst_desc;
244 const char *name = np->name;
245 const char *parent_name;
246 struct clk *regclk;
247
248 /* We do not release this reference, we are using it perpetually */
249 parent = of_get_parent(np);
250 if (!parent) {
251 pr_err("no parent node for syscon ICST clock\n");
252 return;
253 }
254 map = syscon_node_to_regmap(parent);
255 if (IS_ERR(map)) {
256 pr_err("no regmap for syscon ICST clock parent\n");
257 return;
258 }
259
260 if (of_property_read_u32(np, "vco-offset", &icst_desc.vco_offset)) {
261 pr_err("no VCO register offset for ICST clock\n");
262 return;
263 }
264 if (of_property_read_u32(np, "lock-offset", &icst_desc.lock_offset)) {
265 pr_err("no lock register offset for ICST clock\n");
266 return;
267 }
268
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;
273 else {
274 pr_err("unknown ICST clock %s\n", name);
275 return;
276 }
277
278 /* Parent clock name is not the same as node parent */
279 parent_name = of_clk_get_parent_name(np, 0);
280
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);
284 return;
285 }
286 of_clk_add_provider(np, of_clk_src_simple_get, regclk);
287 pr_debug("registered syscon ICST clock %s\n", name);
288}
289
290CLK_OF_DECLARE(arm_syscon_icst525_clk,
291 "arm,syscon-icst525", of_syscon_icst_setup);
292CLK_OF_DECLARE(arm_syscon_icst307_clk,
293 "arm,syscon-icst307", of_syscon_icst_setup);
294
295#endif