Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 1 | /* |
| 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 Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 6 | * Copyright (C) 2012-2015 Linus Walleij |
Linus Walleij | 401301c | 2012-11-20 22:39:31 +0100 | [diff] [blame] | 7 | * |
| 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 Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 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 |
| 14 | * file. |
| 15 | */ |
Stephen Boyd | 6d31e3b2 | 2015-06-19 15:00:46 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/export.h> |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 19 | #include <linux/err.h> |
| 20 | #include <linux/clk-provider.h> |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 21 | #include <linux/io.h> |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 22 | #include <linux/regmap.h> |
Linus Walleij | 384d977 | 2015-10-12 16:14:28 +0200 | [diff] [blame] | 23 | #include <linux/mfd/syscon.h> |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 24 | |
| 25 | #include "clk-icst.h" |
| 26 | |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 27 | /* Magic unlocking token used on all Versatile boards */ |
| 28 | #define VERSATILE_LOCK_VAL 0xA05F |
| 29 | |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 30 | /** |
| 31 | * struct clk_icst - ICST VCO clock wrapper |
| 32 | * @hw: corresponding clock hardware entry |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 33 | * @vcoreg: VCO register address |
| 34 | * @lockreg: VCO lock register address |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 35 | * @params: parameters for this ICST instance |
| 36 | * @rate: current rate |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 37 | */ |
| 38 | struct clk_icst { |
| 39 | struct clk_hw hw; |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 40 | struct regmap *map; |
| 41 | u32 vcoreg_off; |
| 42 | u32 lockreg_off; |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 43 | struct icst_params *params; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 44 | unsigned long rate; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | #define to_icst(_hw) container_of(_hw, struct clk_icst, hw) |
| 48 | |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 49 | /** |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 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 |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 53 | */ |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 54 | static int vco_get(struct clk_icst *icst, struct icst_vco *vco) |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 55 | { |
| 56 | u32 val; |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 57 | int ret; |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 58 | |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 59 | 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 Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * vco_set() - commit changes to an ICST VCO |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 70 | * @icst: the ICST clock to set |
| 71 | * @vco: the VCO struct to set the changes from |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 72 | */ |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 73 | static int vco_set(struct clk_icst *icst, struct icst_vco vco) |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 74 | { |
| 75 | u32 val; |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 76 | int ret; |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 77 | |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 78 | ret = regmap_read(icst->map, icst->vcoreg_off, &val); |
| 79 | if (ret) |
| 80 | return ret; |
Linus Walleij | df9cd56 | 2016-02-03 14:47:08 +0100 | [diff] [blame] | 81 | |
| 82 | /* Mask the 18 bits used by the VCO */ |
| 83 | val &= ~0x7ffff; |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 84 | val |= vco.v | (vco.r << 9) | (vco.s << 16); |
| 85 | |
| 86 | /* This magic unlocks the VCO so it can be controlled */ |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 87 | ret = regmap_write(icst->map, icst->lockreg_off, VERSATILE_LOCK_VAL); |
| 88 | if (ret) |
| 89 | return ret; |
| 90 | ret = regmap_write(icst->map, icst->vcoreg_off, val); |
| 91 | if (ret) |
| 92 | return ret; |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 93 | /* This locks the VCO again */ |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 94 | ret = regmap_write(icst->map, icst->lockreg_off, 0); |
| 95 | if (ret) |
| 96 | return ret; |
| 97 | return 0; |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 100 | static unsigned long icst_recalc_rate(struct clk_hw *hw, |
| 101 | unsigned long parent_rate) |
| 102 | { |
| 103 | struct clk_icst *icst = to_icst(hw); |
| 104 | struct icst_vco vco; |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 105 | int ret; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 106 | |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 107 | if (parent_rate) |
| 108 | icst->params->ref = parent_rate; |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 109 | ret = vco_get(icst, &vco); |
| 110 | if (ret) { |
| 111 | pr_err("ICST: could not get VCO setting\n"); |
| 112 | return 0; |
| 113 | } |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 114 | icst->rate = icst_hz(icst->params, vco); |
| 115 | return icst->rate; |
| 116 | } |
| 117 | |
| 118 | static long icst_round_rate(struct clk_hw *hw, unsigned long rate, |
| 119 | unsigned long *prate) |
| 120 | { |
| 121 | struct clk_icst *icst = to_icst(hw); |
| 122 | struct icst_vco vco; |
| 123 | |
| 124 | vco = icst_hz_to_vco(icst->params, rate); |
| 125 | return icst_hz(icst->params, vco); |
| 126 | } |
| 127 | |
| 128 | static int icst_set_rate(struct clk_hw *hw, unsigned long rate, |
| 129 | unsigned long parent_rate) |
| 130 | { |
| 131 | struct clk_icst *icst = to_icst(hw); |
| 132 | struct icst_vco vco; |
| 133 | |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 134 | if (parent_rate) |
| 135 | icst->params->ref = parent_rate; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 136 | vco = icst_hz_to_vco(icst->params, rate); |
| 137 | icst->rate = icst_hz(icst->params, vco); |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 138 | return vco_set(icst, vco); |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static const struct clk_ops icst_ops = { |
| 142 | .recalc_rate = icst_recalc_rate, |
| 143 | .round_rate = icst_round_rate, |
| 144 | .set_rate = icst_set_rate, |
| 145 | }; |
| 146 | |
Linus Walleij | 384d977 | 2015-10-12 16:14:28 +0200 | [diff] [blame] | 147 | static struct clk *icst_clk_setup(struct device *dev, |
| 148 | const struct clk_icst_desc *desc, |
| 149 | const char *name, |
| 150 | const char *parent_name, |
| 151 | struct regmap *map) |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 152 | { |
| 153 | struct clk *clk; |
| 154 | struct clk_icst *icst; |
| 155 | struct clk_init_data init; |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 156 | struct icst_params *pclone; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 157 | |
| 158 | icst = kzalloc(sizeof(struct clk_icst), GFP_KERNEL); |
| 159 | if (!icst) { |
| 160 | pr_err("could not allocate ICST clock!\n"); |
| 161 | return ERR_PTR(-ENOMEM); |
| 162 | } |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 163 | |
| 164 | pclone = kmemdup(desc->params, sizeof(*pclone), GFP_KERNEL); |
| 165 | if (!pclone) { |
Colin Ian King | ab7ad35 | 2014-04-12 18:59:14 +0100 | [diff] [blame] | 166 | kfree(icst); |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 167 | pr_err("could not clone ICST params\n"); |
| 168 | return ERR_PTR(-ENOMEM); |
| 169 | } |
| 170 | |
Linus Walleij | ae6e694 | 2013-11-22 11:30:05 +0100 | [diff] [blame] | 171 | init.name = name; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 172 | init.ops = &icst_ops; |
| 173 | init.flags = CLK_IS_ROOT; |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 174 | init.parent_names = (parent_name ? &parent_name : NULL); |
| 175 | init.num_parents = (parent_name ? 1 : 0); |
Linus Walleij | 384d977 | 2015-10-12 16:14:28 +0200 | [diff] [blame] | 176 | icst->map = map; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 177 | icst->hw.init = &init; |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 178 | icst->params = pclone; |
Linus Walleij | 179c8fb | 2015-10-12 15:52:50 +0200 | [diff] [blame] | 179 | icst->vcoreg_off = desc->vco_offset; |
| 180 | icst->lockreg_off = desc->lock_offset; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 181 | |
| 182 | clk = clk_register(dev, &icst->hw); |
Linus Walleij | 7bdccef | 2015-10-23 11:36:01 +0200 | [diff] [blame] | 183 | if (IS_ERR(clk)) { |
| 184 | kfree(pclone); |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 185 | kfree(icst); |
Linus Walleij | 7bdccef | 2015-10-23 11:36:01 +0200 | [diff] [blame] | 186 | } |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 187 | |
| 188 | return clk; |
| 189 | } |
Linus Walleij | 384d977 | 2015-10-12 16:14:28 +0200 | [diff] [blame] | 190 | |
| 191 | struct clk *icst_clk_register(struct device *dev, |
| 192 | const struct clk_icst_desc *desc, |
| 193 | const char *name, |
| 194 | const char *parent_name, |
| 195 | void __iomem *base) |
| 196 | { |
| 197 | struct regmap_config icst_regmap_conf = { |
| 198 | .reg_bits = 32, |
| 199 | .val_bits = 32, |
| 200 | .reg_stride = 4, |
| 201 | }; |
| 202 | struct regmap *map; |
| 203 | |
| 204 | map = regmap_init_mmio(dev, base, &icst_regmap_conf); |
| 205 | if (IS_ERR(map)) { |
| 206 | pr_err("could not initialize ICST regmap\n"); |
| 207 | return ERR_CAST(map); |
| 208 | } |
| 209 | return icst_clk_setup(dev, desc, name, parent_name, map); |
| 210 | } |
Arnd Bergmann | a218d7f | 2014-05-08 16:56:16 +0200 | [diff] [blame] | 211 | EXPORT_SYMBOL_GPL(icst_clk_register); |
Linus Walleij | d430819 | 2015-10-13 14:29:54 +0200 | [diff] [blame] | 212 | |
| 213 | #ifdef CONFIG_OF |
| 214 | /* |
| 215 | * In a device tree, an memory-mapped ICST clock appear as a child |
| 216 | * of a syscon node. Assume this and probe it only as a child of a |
| 217 | * syscon. |
| 218 | */ |
| 219 | |
| 220 | static const struct icst_params icst525_params = { |
| 221 | .vco_max = ICST525_VCO_MAX_5V, |
| 222 | .vco_min = ICST525_VCO_MIN, |
| 223 | .vd_min = 8, |
| 224 | .vd_max = 263, |
| 225 | .rd_min = 3, |
| 226 | .rd_max = 65, |
| 227 | .s2div = icst525_s2div, |
| 228 | .idx2s = icst525_idx2s, |
| 229 | }; |
| 230 | |
| 231 | static const struct icst_params icst307_params = { |
| 232 | .vco_max = ICST307_VCO_MAX, |
| 233 | .vco_min = ICST307_VCO_MIN, |
| 234 | .vd_min = 4 + 8, |
| 235 | .vd_max = 511 + 8, |
| 236 | .rd_min = 1 + 2, |
| 237 | .rd_max = 127 + 2, |
| 238 | .s2div = icst307_s2div, |
| 239 | .idx2s = icst307_idx2s, |
| 240 | }; |
| 241 | |
| 242 | static void __init of_syscon_icst_setup(struct device_node *np) |
| 243 | { |
| 244 | struct device_node *parent; |
| 245 | struct regmap *map; |
| 246 | struct clk_icst_desc icst_desc; |
| 247 | const char *name = np->name; |
| 248 | const char *parent_name; |
| 249 | struct clk *regclk; |
| 250 | |
| 251 | /* We do not release this reference, we are using it perpetually */ |
| 252 | parent = of_get_parent(np); |
| 253 | if (!parent) { |
| 254 | pr_err("no parent node for syscon ICST clock\n"); |
| 255 | return; |
| 256 | } |
| 257 | map = syscon_node_to_regmap(parent); |
| 258 | if (IS_ERR(map)) { |
| 259 | pr_err("no regmap for syscon ICST clock parent\n"); |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | if (of_property_read_u32(np, "vco-offset", &icst_desc.vco_offset)) { |
| 264 | pr_err("no VCO register offset for ICST clock\n"); |
| 265 | return; |
| 266 | } |
| 267 | if (of_property_read_u32(np, "lock-offset", &icst_desc.lock_offset)) { |
| 268 | pr_err("no lock register offset for ICST clock\n"); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | if (of_device_is_compatible(np, "arm,syscon-icst525")) |
| 273 | icst_desc.params = &icst525_params; |
| 274 | else if (of_device_is_compatible(np, "arm,syscon-icst307")) |
| 275 | icst_desc.params = &icst307_params; |
| 276 | else { |
| 277 | pr_err("unknown ICST clock %s\n", name); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | /* Parent clock name is not the same as node parent */ |
| 282 | parent_name = of_clk_get_parent_name(np, 0); |
| 283 | |
| 284 | regclk = icst_clk_setup(NULL, &icst_desc, name, parent_name, map); |
| 285 | if (IS_ERR(regclk)) { |
| 286 | pr_err("error setting up syscon ICST clock %s\n", name); |
| 287 | return; |
| 288 | } |
| 289 | of_clk_add_provider(np, of_clk_src_simple_get, regclk); |
| 290 | pr_debug("registered syscon ICST clock %s\n", name); |
| 291 | } |
| 292 | |
| 293 | CLK_OF_DECLARE(arm_syscon_icst525_clk, |
| 294 | "arm,syscon-icst525", of_syscon_icst_setup); |
| 295 | CLK_OF_DECLARE(arm_syscon_icst307_clk, |
| 296 | "arm,syscon-icst307", of_syscon_icst_setup); |
| 297 | |
| 298 | #endif |