Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * This software is licensed under the terms of the GNU General Public |
| 5 | * License version 2, as published by the Free Software Foundation, and |
| 6 | * may be copied, distributed, and modified under those terms. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/bitops.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/export.h> |
| 18 | #include <linux/clk-provider.h> |
| 19 | #include <linux/regmap.h> |
| 20 | |
| 21 | #include <asm/div64.h> |
| 22 | |
| 23 | #include "clk-rcg.h" |
Stephen Boyd | 50c6a50 | 2014-09-04 13:21:50 -0700 | [diff] [blame] | 24 | #include "common.h" |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 25 | |
| 26 | static u32 ns_to_src(struct src_sel *s, u32 ns) |
| 27 | { |
| 28 | ns >>= s->src_sel_shift; |
| 29 | ns &= SRC_SEL_MASK; |
| 30 | return ns; |
| 31 | } |
| 32 | |
| 33 | static u32 src_to_ns(struct src_sel *s, u8 src, u32 ns) |
| 34 | { |
| 35 | u32 mask; |
| 36 | |
| 37 | mask = SRC_SEL_MASK; |
| 38 | mask <<= s->src_sel_shift; |
| 39 | ns &= ~mask; |
| 40 | |
| 41 | ns |= src << s->src_sel_shift; |
| 42 | return ns; |
| 43 | } |
| 44 | |
| 45 | static u8 clk_rcg_get_parent(struct clk_hw *hw) |
| 46 | { |
| 47 | struct clk_rcg *rcg = to_clk_rcg(hw); |
| 48 | int num_parents = __clk_get_num_parents(hw->clk); |
| 49 | u32 ns; |
Georgi Djakov | 7f21897 | 2015-03-20 18:30:24 +0200 | [diff] [blame] | 50 | int i, ret; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 51 | |
Georgi Djakov | 7f21897 | 2015-03-20 18:30:24 +0200 | [diff] [blame] | 52 | ret = regmap_read(rcg->clkr.regmap, rcg->ns_reg, &ns); |
| 53 | if (ret) |
| 54 | goto err; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 55 | ns = ns_to_src(&rcg->s, ns); |
| 56 | for (i = 0; i < num_parents; i++) |
| 57 | if (ns == rcg->s.parent_map[i]) |
| 58 | return i; |
| 59 | |
Georgi Djakov | 7f21897 | 2015-03-20 18:30:24 +0200 | [diff] [blame] | 60 | err: |
| 61 | pr_debug("%s: Clock %s has invalid parent, using default.\n", |
| 62 | __func__, __clk_get_name(hw->clk)); |
| 63 | return 0; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static int reg_to_bank(struct clk_dyn_rcg *rcg, u32 bank) |
| 67 | { |
| 68 | bank &= BIT(rcg->mux_sel_bit); |
| 69 | return !!bank; |
| 70 | } |
| 71 | |
| 72 | static u8 clk_dyn_rcg_get_parent(struct clk_hw *hw) |
| 73 | { |
| 74 | struct clk_dyn_rcg *rcg = to_clk_dyn_rcg(hw); |
| 75 | int num_parents = __clk_get_num_parents(hw->clk); |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 76 | u32 ns, reg; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 77 | int bank; |
Georgi Djakov | 7f21897 | 2015-03-20 18:30:24 +0200 | [diff] [blame] | 78 | int i, ret; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 79 | struct src_sel *s; |
| 80 | |
Georgi Djakov | 7f21897 | 2015-03-20 18:30:24 +0200 | [diff] [blame] | 81 | ret = regmap_read(rcg->clkr.regmap, rcg->bank_reg, ®); |
| 82 | if (ret) |
| 83 | goto err; |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 84 | bank = reg_to_bank(rcg, reg); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 85 | s = &rcg->s[bank]; |
| 86 | |
Georgi Djakov | 7f21897 | 2015-03-20 18:30:24 +0200 | [diff] [blame] | 87 | ret = regmap_read(rcg->clkr.regmap, rcg->ns_reg[bank], &ns); |
| 88 | if (ret) |
| 89 | goto err; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 90 | ns = ns_to_src(s, ns); |
| 91 | |
| 92 | for (i = 0; i < num_parents; i++) |
| 93 | if (ns == s->parent_map[i]) |
| 94 | return i; |
| 95 | |
Georgi Djakov | 7f21897 | 2015-03-20 18:30:24 +0200 | [diff] [blame] | 96 | err: |
| 97 | pr_debug("%s: Clock %s has invalid parent, using default.\n", |
| 98 | __func__, __clk_get_name(hw->clk)); |
| 99 | return 0; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static int clk_rcg_set_parent(struct clk_hw *hw, u8 index) |
| 103 | { |
| 104 | struct clk_rcg *rcg = to_clk_rcg(hw); |
| 105 | u32 ns; |
| 106 | |
| 107 | regmap_read(rcg->clkr.regmap, rcg->ns_reg, &ns); |
| 108 | ns = src_to_ns(&rcg->s, rcg->s.parent_map[index], ns); |
| 109 | regmap_write(rcg->clkr.regmap, rcg->ns_reg, ns); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static u32 md_to_m(struct mn *mn, u32 md) |
| 115 | { |
| 116 | md >>= mn->m_val_shift; |
| 117 | md &= BIT(mn->width) - 1; |
| 118 | return md; |
| 119 | } |
| 120 | |
| 121 | static u32 ns_to_pre_div(struct pre_div *p, u32 ns) |
| 122 | { |
| 123 | ns >>= p->pre_div_shift; |
| 124 | ns &= BIT(p->pre_div_width) - 1; |
| 125 | return ns; |
| 126 | } |
| 127 | |
| 128 | static u32 pre_div_to_ns(struct pre_div *p, u8 pre_div, u32 ns) |
| 129 | { |
| 130 | u32 mask; |
| 131 | |
| 132 | mask = BIT(p->pre_div_width) - 1; |
| 133 | mask <<= p->pre_div_shift; |
| 134 | ns &= ~mask; |
| 135 | |
| 136 | ns |= pre_div << p->pre_div_shift; |
| 137 | return ns; |
| 138 | } |
| 139 | |
| 140 | static u32 mn_to_md(struct mn *mn, u32 m, u32 n, u32 md) |
| 141 | { |
| 142 | u32 mask, mask_w; |
| 143 | |
| 144 | mask_w = BIT(mn->width) - 1; |
| 145 | mask = (mask_w << mn->m_val_shift) | mask_w; |
| 146 | md &= ~mask; |
| 147 | |
| 148 | if (n) { |
| 149 | m <<= mn->m_val_shift; |
| 150 | md |= m; |
| 151 | md |= ~n & mask_w; |
| 152 | } |
| 153 | |
| 154 | return md; |
| 155 | } |
| 156 | |
| 157 | static u32 ns_m_to_n(struct mn *mn, u32 ns, u32 m) |
| 158 | { |
| 159 | ns = ~ns >> mn->n_val_shift; |
| 160 | ns &= BIT(mn->width) - 1; |
| 161 | return ns + m; |
| 162 | } |
| 163 | |
| 164 | static u32 reg_to_mnctr_mode(struct mn *mn, u32 val) |
| 165 | { |
| 166 | val >>= mn->mnctr_mode_shift; |
| 167 | val &= MNCTR_MODE_MASK; |
| 168 | return val; |
| 169 | } |
| 170 | |
| 171 | static u32 mn_to_ns(struct mn *mn, u32 m, u32 n, u32 ns) |
| 172 | { |
| 173 | u32 mask; |
| 174 | |
| 175 | mask = BIT(mn->width) - 1; |
| 176 | mask <<= mn->n_val_shift; |
| 177 | ns &= ~mask; |
| 178 | |
| 179 | if (n) { |
| 180 | n = n - m; |
| 181 | n = ~n; |
| 182 | n &= BIT(mn->width) - 1; |
| 183 | n <<= mn->n_val_shift; |
| 184 | ns |= n; |
| 185 | } |
| 186 | |
| 187 | return ns; |
| 188 | } |
| 189 | |
| 190 | static u32 mn_to_reg(struct mn *mn, u32 m, u32 n, u32 val) |
| 191 | { |
| 192 | u32 mask; |
| 193 | |
| 194 | mask = MNCTR_MODE_MASK << mn->mnctr_mode_shift; |
| 195 | mask |= BIT(mn->mnctr_en_bit); |
| 196 | val &= ~mask; |
| 197 | |
| 198 | if (n) { |
| 199 | val |= BIT(mn->mnctr_en_bit); |
| 200 | val |= MNCTR_MODE_DUAL << mn->mnctr_mode_shift; |
| 201 | } |
| 202 | |
| 203 | return val; |
| 204 | } |
| 205 | |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 206 | static int configure_bank(struct clk_dyn_rcg *rcg, const struct freq_tbl *f) |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 207 | { |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 208 | u32 ns, md, reg; |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 209 | int bank, new_bank, ret; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 210 | struct mn *mn; |
| 211 | struct pre_div *p; |
| 212 | struct src_sel *s; |
| 213 | bool enabled; |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 214 | u32 md_reg, ns_reg; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 215 | bool banked_mn = !!rcg->mn[1].width; |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 216 | bool banked_p = !!rcg->p[1].pre_div_width; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 217 | struct clk_hw *hw = &rcg->clkr.hw; |
| 218 | |
| 219 | enabled = __clk_is_enabled(hw->clk); |
| 220 | |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 221 | ret = regmap_read(rcg->clkr.regmap, rcg->bank_reg, ®); |
| 222 | if (ret) |
| 223 | return ret; |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 224 | bank = reg_to_bank(rcg, reg); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 225 | new_bank = enabled ? !bank : bank; |
| 226 | |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 227 | ns_reg = rcg->ns_reg[new_bank]; |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 228 | ret = regmap_read(rcg->clkr.regmap, ns_reg, &ns); |
| 229 | if (ret) |
| 230 | return ret; |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 231 | |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 232 | if (banked_mn) { |
| 233 | mn = &rcg->mn[new_bank]; |
| 234 | md_reg = rcg->md_reg[new_bank]; |
| 235 | |
| 236 | ns |= BIT(mn->mnctr_reset_bit); |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 237 | ret = regmap_write(rcg->clkr.regmap, ns_reg, ns); |
| 238 | if (ret) |
| 239 | return ret; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 240 | |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 241 | ret = regmap_read(rcg->clkr.regmap, md_reg, &md); |
| 242 | if (ret) |
| 243 | return ret; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 244 | md = mn_to_md(mn, f->m, f->n, md); |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 245 | ret = regmap_write(rcg->clkr.regmap, md_reg, md); |
| 246 | if (ret) |
| 247 | return ret; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 248 | ns = mn_to_ns(mn, f->m, f->n, ns); |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 249 | ret = regmap_write(rcg->clkr.regmap, ns_reg, ns); |
| 250 | if (ret) |
| 251 | return ret; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 252 | |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 253 | /* Two NS registers means mode control is in NS register */ |
| 254 | if (rcg->ns_reg[0] != rcg->ns_reg[1]) { |
| 255 | ns = mn_to_reg(mn, f->m, f->n, ns); |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 256 | ret = regmap_write(rcg->clkr.regmap, ns_reg, ns); |
| 257 | if (ret) |
| 258 | return ret; |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 259 | } else { |
| 260 | reg = mn_to_reg(mn, f->m, f->n, reg); |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 261 | ret = regmap_write(rcg->clkr.regmap, rcg->bank_reg, |
| 262 | reg); |
| 263 | if (ret) |
| 264 | return ret; |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 265 | } |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 266 | |
| 267 | ns &= ~BIT(mn->mnctr_reset_bit); |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 268 | ret = regmap_write(rcg->clkr.regmap, ns_reg, ns); |
| 269 | if (ret) |
| 270 | return ret; |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | if (banked_p) { |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 274 | p = &rcg->p[new_bank]; |
| 275 | ns = pre_div_to_ns(p, f->pre_div - 1, ns); |
| 276 | } |
| 277 | |
| 278 | s = &rcg->s[new_bank]; |
| 279 | ns = src_to_ns(s, s->parent_map[f->src], ns); |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 280 | ret = regmap_write(rcg->clkr.regmap, ns_reg, ns); |
| 281 | if (ret) |
| 282 | return ret; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 283 | |
| 284 | if (enabled) { |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 285 | ret = regmap_read(rcg->clkr.regmap, rcg->bank_reg, ®); |
| 286 | if (ret) |
| 287 | return ret; |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 288 | reg ^= BIT(rcg->mux_sel_bit); |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 289 | ret = regmap_write(rcg->clkr.regmap, rcg->bank_reg, reg); |
| 290 | if (ret) |
| 291 | return ret; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 292 | } |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 293 | return 0; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static int clk_dyn_rcg_set_parent(struct clk_hw *hw, u8 index) |
| 297 | { |
| 298 | struct clk_dyn_rcg *rcg = to_clk_dyn_rcg(hw); |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 299 | u32 ns, md, reg; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 300 | int bank; |
| 301 | struct freq_tbl f = { 0 }; |
| 302 | bool banked_mn = !!rcg->mn[1].width; |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 303 | bool banked_p = !!rcg->p[1].pre_div_width; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 304 | |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 305 | regmap_read(rcg->clkr.regmap, rcg->bank_reg, ®); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 306 | bank = reg_to_bank(rcg, reg); |
| 307 | |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 308 | regmap_read(rcg->clkr.regmap, rcg->ns_reg[bank], &ns); |
| 309 | |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 310 | if (banked_mn) { |
| 311 | regmap_read(rcg->clkr.regmap, rcg->md_reg[bank], &md); |
| 312 | f.m = md_to_m(&rcg->mn[bank], md); |
| 313 | f.n = ns_m_to_n(&rcg->mn[bank], ns, f.m); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 314 | } |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 315 | |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 316 | if (banked_p) |
| 317 | f.pre_div = ns_to_pre_div(&rcg->p[bank], ns) + 1; |
| 318 | |
| 319 | f.src = index; |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 320 | return configure_bank(rcg, &f); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /* |
| 324 | * Calculate m/n:d rate |
| 325 | * |
| 326 | * parent_rate m |
| 327 | * rate = ----------- x --- |
| 328 | * pre_div n |
| 329 | */ |
| 330 | static unsigned long |
| 331 | calc_rate(unsigned long rate, u32 m, u32 n, u32 mode, u32 pre_div) |
| 332 | { |
| 333 | if (pre_div) |
| 334 | rate /= pre_div + 1; |
| 335 | |
| 336 | if (mode) { |
| 337 | u64 tmp = rate; |
| 338 | tmp *= m; |
| 339 | do_div(tmp, n); |
| 340 | rate = tmp; |
| 341 | } |
| 342 | |
| 343 | return rate; |
| 344 | } |
| 345 | |
| 346 | static unsigned long |
| 347 | clk_rcg_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) |
| 348 | { |
| 349 | struct clk_rcg *rcg = to_clk_rcg(hw); |
| 350 | u32 pre_div, m = 0, n = 0, ns, md, mode = 0; |
| 351 | struct mn *mn = &rcg->mn; |
| 352 | |
| 353 | regmap_read(rcg->clkr.regmap, rcg->ns_reg, &ns); |
| 354 | pre_div = ns_to_pre_div(&rcg->p, ns); |
| 355 | |
| 356 | if (rcg->mn.width) { |
| 357 | regmap_read(rcg->clkr.regmap, rcg->md_reg, &md); |
| 358 | m = md_to_m(mn, md); |
| 359 | n = ns_m_to_n(mn, ns, m); |
| 360 | /* MN counter mode is in hw.enable_reg sometimes */ |
| 361 | if (rcg->clkr.enable_reg != rcg->ns_reg) |
| 362 | regmap_read(rcg->clkr.regmap, rcg->clkr.enable_reg, &mode); |
| 363 | else |
| 364 | mode = ns; |
| 365 | mode = reg_to_mnctr_mode(mn, mode); |
| 366 | } |
| 367 | |
| 368 | return calc_rate(parent_rate, m, n, mode, pre_div); |
| 369 | } |
| 370 | |
| 371 | static unsigned long |
| 372 | clk_dyn_rcg_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) |
| 373 | { |
| 374 | struct clk_dyn_rcg *rcg = to_clk_dyn_rcg(hw); |
| 375 | u32 m, n, pre_div, ns, md, mode, reg; |
| 376 | int bank; |
| 377 | struct mn *mn; |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 378 | bool banked_p = !!rcg->p[1].pre_div_width; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 379 | bool banked_mn = !!rcg->mn[1].width; |
| 380 | |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 381 | regmap_read(rcg->clkr.regmap, rcg->bank_reg, ®); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 382 | bank = reg_to_bank(rcg, reg); |
| 383 | |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 384 | regmap_read(rcg->clkr.regmap, rcg->ns_reg[bank], &ns); |
| 385 | m = n = pre_div = mode = 0; |
| 386 | |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 387 | if (banked_mn) { |
| 388 | mn = &rcg->mn[bank]; |
| 389 | regmap_read(rcg->clkr.regmap, rcg->md_reg[bank], &md); |
| 390 | m = md_to_m(mn, md); |
| 391 | n = ns_m_to_n(mn, ns, m); |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 392 | /* Two NS registers means mode control is in NS register */ |
| 393 | if (rcg->ns_reg[0] != rcg->ns_reg[1]) |
| 394 | reg = ns; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 395 | mode = reg_to_mnctr_mode(mn, reg); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 396 | } |
Stephen Boyd | 229fd4a | 2014-04-28 15:59:16 -0700 | [diff] [blame] | 397 | |
| 398 | if (banked_p) |
| 399 | pre_div = ns_to_pre_div(&rcg->p[bank], ns); |
| 400 | |
| 401 | return calc_rate(parent_rate, m, n, mode, pre_div); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 404 | static long _freq_tbl_determine_rate(struct clk_hw *hw, |
| 405 | const struct freq_tbl *f, unsigned long rate, |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 406 | unsigned long min_rate, unsigned long max_rate, |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 407 | unsigned long *p_rate, struct clk_hw **p_hw) |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 408 | { |
| 409 | unsigned long clk_flags; |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 410 | struct clk *p; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 411 | |
Stephen Boyd | 50c6a50 | 2014-09-04 13:21:50 -0700 | [diff] [blame] | 412 | f = qcom_find_freq(f, rate); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 413 | if (!f) |
| 414 | return -EINVAL; |
| 415 | |
| 416 | clk_flags = __clk_get_flags(hw->clk); |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 417 | p = clk_get_parent_by_index(hw->clk, f->src); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 418 | if (clk_flags & CLK_SET_RATE_PARENT) { |
| 419 | rate = rate * f->pre_div; |
| 420 | if (f->n) { |
| 421 | u64 tmp = rate; |
| 422 | tmp = tmp * f->n; |
| 423 | do_div(tmp, f->m); |
| 424 | rate = tmp; |
| 425 | } |
| 426 | } else { |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 427 | rate = __clk_get_rate(p); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 428 | } |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 429 | *p_hw = __clk_get_hw(p); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 430 | *p_rate = rate; |
| 431 | |
| 432 | return f->freq; |
| 433 | } |
| 434 | |
| 435 | static long clk_rcg_determine_rate(struct clk_hw *hw, unsigned long rate, |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 436 | unsigned long min_rate, unsigned long max_rate, |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 437 | unsigned long *p_rate, struct clk_hw **p) |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 438 | { |
| 439 | struct clk_rcg *rcg = to_clk_rcg(hw); |
| 440 | |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 441 | return _freq_tbl_determine_rate(hw, rcg->freq_tbl, rate, min_rate, |
| 442 | max_rate, p_rate, p); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static long clk_dyn_rcg_determine_rate(struct clk_hw *hw, unsigned long rate, |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 446 | unsigned long min_rate, unsigned long max_rate, |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 447 | unsigned long *p_rate, struct clk_hw **p) |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 448 | { |
| 449 | struct clk_dyn_rcg *rcg = to_clk_dyn_rcg(hw); |
| 450 | |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 451 | return _freq_tbl_determine_rate(hw, rcg->freq_tbl, rate, min_rate, |
| 452 | max_rate, p_rate, p); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 453 | } |
| 454 | |
Stephen Boyd | 404c1ff | 2014-07-11 12:55:27 -0700 | [diff] [blame] | 455 | static long clk_rcg_bypass_determine_rate(struct clk_hw *hw, unsigned long rate, |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 456 | unsigned long min_rate, unsigned long max_rate, |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 457 | unsigned long *p_rate, struct clk_hw **p_hw) |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 458 | { |
| 459 | struct clk_rcg *rcg = to_clk_rcg(hw); |
Stephen Boyd | 404c1ff | 2014-07-11 12:55:27 -0700 | [diff] [blame] | 460 | const struct freq_tbl *f = rcg->freq_tbl; |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 461 | struct clk *p; |
Stephen Boyd | 404c1ff | 2014-07-11 12:55:27 -0700 | [diff] [blame] | 462 | |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 463 | p = clk_get_parent_by_index(hw->clk, f->src); |
| 464 | *p_hw = __clk_get_hw(p); |
| 465 | *p_rate = __clk_round_rate(p, rate); |
Stephen Boyd | 404c1ff | 2014-07-11 12:55:27 -0700 | [diff] [blame] | 466 | |
| 467 | return *p_rate; |
| 468 | } |
| 469 | |
| 470 | static int __clk_rcg_set_rate(struct clk_rcg *rcg, const struct freq_tbl *f) |
| 471 | { |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 472 | u32 ns, md, ctl; |
| 473 | struct mn *mn = &rcg->mn; |
| 474 | u32 mask = 0; |
| 475 | unsigned int reset_reg; |
| 476 | |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 477 | if (rcg->mn.reset_in_cc) |
| 478 | reset_reg = rcg->clkr.enable_reg; |
| 479 | else |
| 480 | reset_reg = rcg->ns_reg; |
| 481 | |
| 482 | if (rcg->mn.width) { |
| 483 | mask = BIT(mn->mnctr_reset_bit); |
| 484 | regmap_update_bits(rcg->clkr.regmap, reset_reg, mask, mask); |
| 485 | |
| 486 | regmap_read(rcg->clkr.regmap, rcg->md_reg, &md); |
| 487 | md = mn_to_md(mn, f->m, f->n, md); |
| 488 | regmap_write(rcg->clkr.regmap, rcg->md_reg, md); |
| 489 | |
| 490 | regmap_read(rcg->clkr.regmap, rcg->ns_reg, &ns); |
| 491 | /* MN counter mode is in hw.enable_reg sometimes */ |
| 492 | if (rcg->clkr.enable_reg != rcg->ns_reg) { |
| 493 | regmap_read(rcg->clkr.regmap, rcg->clkr.enable_reg, &ctl); |
| 494 | ctl = mn_to_reg(mn, f->m, f->n, ctl); |
| 495 | regmap_write(rcg->clkr.regmap, rcg->clkr.enable_reg, ctl); |
| 496 | } else { |
| 497 | ns = mn_to_reg(mn, f->m, f->n, ns); |
| 498 | } |
| 499 | ns = mn_to_ns(mn, f->m, f->n, ns); |
| 500 | } else { |
| 501 | regmap_read(rcg->clkr.regmap, rcg->ns_reg, &ns); |
| 502 | } |
| 503 | |
| 504 | ns = pre_div_to_ns(&rcg->p, f->pre_div - 1, ns); |
| 505 | regmap_write(rcg->clkr.regmap, rcg->ns_reg, ns); |
| 506 | |
| 507 | regmap_update_bits(rcg->clkr.regmap, reset_reg, mask, 0); |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
Stephen Boyd | 404c1ff | 2014-07-11 12:55:27 -0700 | [diff] [blame] | 512 | static int clk_rcg_set_rate(struct clk_hw *hw, unsigned long rate, |
| 513 | unsigned long parent_rate) |
| 514 | { |
| 515 | struct clk_rcg *rcg = to_clk_rcg(hw); |
| 516 | const struct freq_tbl *f; |
| 517 | |
Stephen Boyd | 50c6a50 | 2014-09-04 13:21:50 -0700 | [diff] [blame] | 518 | f = qcom_find_freq(rcg->freq_tbl, rate); |
Stephen Boyd | 404c1ff | 2014-07-11 12:55:27 -0700 | [diff] [blame] | 519 | if (!f) |
| 520 | return -EINVAL; |
| 521 | |
| 522 | return __clk_rcg_set_rate(rcg, f); |
| 523 | } |
| 524 | |
| 525 | static int clk_rcg_bypass_set_rate(struct clk_hw *hw, unsigned long rate, |
| 526 | unsigned long parent_rate) |
| 527 | { |
| 528 | struct clk_rcg *rcg = to_clk_rcg(hw); |
| 529 | |
| 530 | return __clk_rcg_set_rate(rcg, rcg->freq_tbl); |
| 531 | } |
| 532 | |
Stephen Boyd | 9d3745d | 2015-03-06 15:41:53 -0800 | [diff] [blame] | 533 | /* |
| 534 | * This type of clock has a glitch-free mux that switches between the output of |
| 535 | * the M/N counter and an always on clock source (XO). When clk_set_rate() is |
| 536 | * called we need to make sure that we don't switch to the M/N counter if it |
| 537 | * isn't clocking because the mux will get stuck and the clock will stop |
| 538 | * outputting a clock. This can happen if the framework isn't aware that this |
| 539 | * clock is on and so clk_set_rate() doesn't turn on the new parent. To fix |
| 540 | * this we switch the mux in the enable/disable ops and reprogram the M/N |
| 541 | * counter in the set_rate op. We also make sure to switch away from the M/N |
| 542 | * counter in set_rate if software thinks the clock is off. |
| 543 | */ |
| 544 | static int clk_rcg_lcc_set_rate(struct clk_hw *hw, unsigned long rate, |
| 545 | unsigned long parent_rate) |
| 546 | { |
| 547 | struct clk_rcg *rcg = to_clk_rcg(hw); |
| 548 | const struct freq_tbl *f; |
| 549 | int ret; |
| 550 | u32 gfm = BIT(10); |
| 551 | |
| 552 | f = qcom_find_freq(rcg->freq_tbl, rate); |
| 553 | if (!f) |
| 554 | return -EINVAL; |
| 555 | |
| 556 | /* Switch to XO to avoid glitches */ |
| 557 | regmap_update_bits(rcg->clkr.regmap, rcg->ns_reg, gfm, 0); |
| 558 | ret = __clk_rcg_set_rate(rcg, f); |
| 559 | /* Switch back to M/N if it's clocking */ |
| 560 | if (__clk_is_enabled(hw->clk)) |
| 561 | regmap_update_bits(rcg->clkr.regmap, rcg->ns_reg, gfm, gfm); |
| 562 | |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | static int clk_rcg_lcc_enable(struct clk_hw *hw) |
| 567 | { |
| 568 | struct clk_rcg *rcg = to_clk_rcg(hw); |
| 569 | u32 gfm = BIT(10); |
| 570 | |
| 571 | /* Use M/N */ |
| 572 | return regmap_update_bits(rcg->clkr.regmap, rcg->ns_reg, gfm, gfm); |
| 573 | } |
| 574 | |
| 575 | static void clk_rcg_lcc_disable(struct clk_hw *hw) |
| 576 | { |
| 577 | struct clk_rcg *rcg = to_clk_rcg(hw); |
| 578 | u32 gfm = BIT(10); |
| 579 | |
| 580 | /* Use XO */ |
| 581 | regmap_update_bits(rcg->clkr.regmap, rcg->ns_reg, gfm, 0); |
| 582 | } |
| 583 | |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 584 | static int __clk_dyn_rcg_set_rate(struct clk_hw *hw, unsigned long rate) |
| 585 | { |
| 586 | struct clk_dyn_rcg *rcg = to_clk_dyn_rcg(hw); |
| 587 | const struct freq_tbl *f; |
| 588 | |
Stephen Boyd | 50c6a50 | 2014-09-04 13:21:50 -0700 | [diff] [blame] | 589 | f = qcom_find_freq(rcg->freq_tbl, rate); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 590 | if (!f) |
| 591 | return -EINVAL; |
| 592 | |
Georgi Djakov | fae507a | 2015-03-20 18:30:25 +0200 | [diff] [blame^] | 593 | return configure_bank(rcg, f); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | static int clk_dyn_rcg_set_rate(struct clk_hw *hw, unsigned long rate, |
| 597 | unsigned long parent_rate) |
| 598 | { |
| 599 | return __clk_dyn_rcg_set_rate(hw, rate); |
| 600 | } |
| 601 | |
| 602 | static int clk_dyn_rcg_set_rate_and_parent(struct clk_hw *hw, |
| 603 | unsigned long rate, unsigned long parent_rate, u8 index) |
| 604 | { |
| 605 | return __clk_dyn_rcg_set_rate(hw, rate); |
| 606 | } |
| 607 | |
| 608 | const struct clk_ops clk_rcg_ops = { |
| 609 | .enable = clk_enable_regmap, |
| 610 | .disable = clk_disable_regmap, |
| 611 | .get_parent = clk_rcg_get_parent, |
| 612 | .set_parent = clk_rcg_set_parent, |
| 613 | .recalc_rate = clk_rcg_recalc_rate, |
| 614 | .determine_rate = clk_rcg_determine_rate, |
| 615 | .set_rate = clk_rcg_set_rate, |
| 616 | }; |
| 617 | EXPORT_SYMBOL_GPL(clk_rcg_ops); |
| 618 | |
Stephen Boyd | 404c1ff | 2014-07-11 12:55:27 -0700 | [diff] [blame] | 619 | const struct clk_ops clk_rcg_bypass_ops = { |
| 620 | .enable = clk_enable_regmap, |
| 621 | .disable = clk_disable_regmap, |
| 622 | .get_parent = clk_rcg_get_parent, |
| 623 | .set_parent = clk_rcg_set_parent, |
| 624 | .recalc_rate = clk_rcg_recalc_rate, |
| 625 | .determine_rate = clk_rcg_bypass_determine_rate, |
| 626 | .set_rate = clk_rcg_bypass_set_rate, |
| 627 | }; |
| 628 | EXPORT_SYMBOL_GPL(clk_rcg_bypass_ops); |
| 629 | |
Stephen Boyd | 9d3745d | 2015-03-06 15:41:53 -0800 | [diff] [blame] | 630 | const struct clk_ops clk_rcg_lcc_ops = { |
| 631 | .enable = clk_rcg_lcc_enable, |
| 632 | .disable = clk_rcg_lcc_disable, |
| 633 | .get_parent = clk_rcg_get_parent, |
| 634 | .set_parent = clk_rcg_set_parent, |
| 635 | .recalc_rate = clk_rcg_recalc_rate, |
| 636 | .determine_rate = clk_rcg_determine_rate, |
| 637 | .set_rate = clk_rcg_lcc_set_rate, |
| 638 | }; |
| 639 | EXPORT_SYMBOL_GPL(clk_rcg_lcc_ops); |
| 640 | |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 641 | const struct clk_ops clk_dyn_rcg_ops = { |
| 642 | .enable = clk_enable_regmap, |
| 643 | .is_enabled = clk_is_enabled_regmap, |
| 644 | .disable = clk_disable_regmap, |
| 645 | .get_parent = clk_dyn_rcg_get_parent, |
| 646 | .set_parent = clk_dyn_rcg_set_parent, |
| 647 | .recalc_rate = clk_dyn_rcg_recalc_rate, |
| 648 | .determine_rate = clk_dyn_rcg_determine_rate, |
| 649 | .set_rate = clk_dyn_rcg_set_rate, |
| 650 | .set_rate_and_parent = clk_dyn_rcg_set_rate_and_parent, |
| 651 | }; |
| 652 | EXPORT_SYMBOL_GPL(clk_dyn_rcg_ops); |