Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * clk-flexgen.c |
| 3 | * |
| 4 | * Copyright (C) ST-Microelectronics SA 2013 |
| 5 | * Author: Maxime Coquelin <maxime.coquelin@st.com> for ST-Microelectronics. |
| 6 | * License terms: GNU General Public License (GPL), version 2 */ |
| 7 | |
Stephen Boyd | d5f728a | 2015-06-19 15:00:46 -0700 | [diff] [blame] | 8 | #include <linux/clk.h> |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 9 | #include <linux/clk-provider.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/of.h> |
| 16 | #include <linux/of_address.h> |
| 17 | |
Gabriel Fernandez | 26bd0a5 | 2016-08-29 14:26:57 +0200 | [diff] [blame^] | 18 | struct clkgen_data { |
| 19 | unsigned long flags; |
| 20 | }; |
| 21 | |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 22 | struct flexgen { |
| 23 | struct clk_hw hw; |
| 24 | |
| 25 | /* Crossbar */ |
| 26 | struct clk_mux mux; |
| 27 | /* Pre-divisor's gate */ |
| 28 | struct clk_gate pgate; |
| 29 | /* Pre-divisor */ |
| 30 | struct clk_divider pdiv; |
| 31 | /* Final divisor's gate */ |
| 32 | struct clk_gate fgate; |
| 33 | /* Final divisor */ |
| 34 | struct clk_divider fdiv; |
| 35 | }; |
| 36 | |
| 37 | #define to_flexgen(_hw) container_of(_hw, struct flexgen, hw) |
| 38 | |
| 39 | static int flexgen_enable(struct clk_hw *hw) |
| 40 | { |
| 41 | struct flexgen *flexgen = to_flexgen(hw); |
| 42 | struct clk_hw *pgate_hw = &flexgen->pgate.hw; |
| 43 | struct clk_hw *fgate_hw = &flexgen->fgate.hw; |
| 44 | |
Javier Martinez Canillas | 4e907ef | 2015-02-12 14:58:30 +0100 | [diff] [blame] | 45 | __clk_hw_set_clk(pgate_hw, hw); |
| 46 | __clk_hw_set_clk(fgate_hw, hw); |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 47 | |
| 48 | clk_gate_ops.enable(pgate_hw); |
| 49 | |
| 50 | clk_gate_ops.enable(fgate_hw); |
| 51 | |
Stephen Boyd | 836ee0f | 2015-08-12 11:42:23 -0700 | [diff] [blame] | 52 | pr_debug("%s: flexgen output enabled\n", clk_hw_get_name(hw)); |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static void flexgen_disable(struct clk_hw *hw) |
| 57 | { |
| 58 | struct flexgen *flexgen = to_flexgen(hw); |
| 59 | struct clk_hw *fgate_hw = &flexgen->fgate.hw; |
| 60 | |
| 61 | /* disable only the final gate */ |
Javier Martinez Canillas | 4e907ef | 2015-02-12 14:58:30 +0100 | [diff] [blame] | 62 | __clk_hw_set_clk(fgate_hw, hw); |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 63 | |
| 64 | clk_gate_ops.disable(fgate_hw); |
| 65 | |
Stephen Boyd | 836ee0f | 2015-08-12 11:42:23 -0700 | [diff] [blame] | 66 | pr_debug("%s: flexgen output disabled\n", clk_hw_get_name(hw)); |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static int flexgen_is_enabled(struct clk_hw *hw) |
| 70 | { |
| 71 | struct flexgen *flexgen = to_flexgen(hw); |
| 72 | struct clk_hw *fgate_hw = &flexgen->fgate.hw; |
| 73 | |
Javier Martinez Canillas | 4e907ef | 2015-02-12 14:58:30 +0100 | [diff] [blame] | 74 | __clk_hw_set_clk(fgate_hw, hw); |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 75 | |
| 76 | if (!clk_gate_ops.is_enabled(fgate_hw)) |
| 77 | return 0; |
| 78 | |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | static u8 flexgen_get_parent(struct clk_hw *hw) |
| 83 | { |
| 84 | struct flexgen *flexgen = to_flexgen(hw); |
| 85 | struct clk_hw *mux_hw = &flexgen->mux.hw; |
| 86 | |
Javier Martinez Canillas | 4e907ef | 2015-02-12 14:58:30 +0100 | [diff] [blame] | 87 | __clk_hw_set_clk(mux_hw, hw); |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 88 | |
| 89 | return clk_mux_ops.get_parent(mux_hw); |
| 90 | } |
| 91 | |
| 92 | static int flexgen_set_parent(struct clk_hw *hw, u8 index) |
| 93 | { |
| 94 | struct flexgen *flexgen = to_flexgen(hw); |
| 95 | struct clk_hw *mux_hw = &flexgen->mux.hw; |
| 96 | |
Javier Martinez Canillas | 4e907ef | 2015-02-12 14:58:30 +0100 | [diff] [blame] | 97 | __clk_hw_set_clk(mux_hw, hw); |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 98 | |
| 99 | return clk_mux_ops.set_parent(mux_hw, index); |
| 100 | } |
| 101 | |
| 102 | static inline unsigned long |
| 103 | clk_best_div(unsigned long parent_rate, unsigned long rate) |
| 104 | { |
| 105 | return parent_rate / rate + ((rate > (2*(parent_rate % rate))) ? 0 : 1); |
| 106 | } |
| 107 | |
| 108 | static long flexgen_round_rate(struct clk_hw *hw, unsigned long rate, |
| 109 | unsigned long *prate) |
| 110 | { |
| 111 | unsigned long div; |
| 112 | |
| 113 | /* Round div according to exact prate and wished rate */ |
| 114 | div = clk_best_div(*prate, rate); |
| 115 | |
Stephen Boyd | 98d8a60 | 2015-06-29 16:56:30 -0700 | [diff] [blame] | 116 | if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) { |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 117 | *prate = rate * div; |
| 118 | return rate; |
| 119 | } |
| 120 | |
| 121 | return *prate / div; |
| 122 | } |
| 123 | |
Stephen Boyd | 8e6dd77 | 2015-05-01 12:45:53 -0700 | [diff] [blame] | 124 | static unsigned long flexgen_recalc_rate(struct clk_hw *hw, |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 125 | unsigned long parent_rate) |
| 126 | { |
| 127 | struct flexgen *flexgen = to_flexgen(hw); |
| 128 | struct clk_hw *pdiv_hw = &flexgen->pdiv.hw; |
| 129 | struct clk_hw *fdiv_hw = &flexgen->fdiv.hw; |
| 130 | unsigned long mid_rate; |
| 131 | |
Javier Martinez Canillas | 4e907ef | 2015-02-12 14:58:30 +0100 | [diff] [blame] | 132 | __clk_hw_set_clk(pdiv_hw, hw); |
| 133 | __clk_hw_set_clk(fdiv_hw, hw); |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 134 | |
| 135 | mid_rate = clk_divider_ops.recalc_rate(pdiv_hw, parent_rate); |
| 136 | |
| 137 | return clk_divider_ops.recalc_rate(fdiv_hw, mid_rate); |
| 138 | } |
| 139 | |
| 140 | static int flexgen_set_rate(struct clk_hw *hw, unsigned long rate, |
| 141 | unsigned long parent_rate) |
| 142 | { |
| 143 | struct flexgen *flexgen = to_flexgen(hw); |
| 144 | struct clk_hw *pdiv_hw = &flexgen->pdiv.hw; |
| 145 | struct clk_hw *fdiv_hw = &flexgen->fdiv.hw; |
Peter Griffin | edc3007 | 2015-01-20 15:32:41 +0000 | [diff] [blame] | 146 | unsigned long div = 0; |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 147 | int ret = 0; |
| 148 | |
Javier Martinez Canillas | 4e907ef | 2015-02-12 14:58:30 +0100 | [diff] [blame] | 149 | __clk_hw_set_clk(pdiv_hw, hw); |
| 150 | __clk_hw_set_clk(fdiv_hw, hw); |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 151 | |
Peter Griffin | edc3007 | 2015-01-20 15:32:41 +0000 | [diff] [blame] | 152 | div = clk_best_div(parent_rate, rate); |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 153 | |
Peter Griffin | edc3007 | 2015-01-20 15:32:41 +0000 | [diff] [blame] | 154 | /* |
| 155 | * pdiv is mainly targeted for low freq results, while fdiv |
| 156 | * should be used for div <= 64. The other way round can |
| 157 | * lead to 'duty cycle' issues. |
| 158 | */ |
| 159 | |
| 160 | if (div <= 64) { |
| 161 | clk_divider_ops.set_rate(pdiv_hw, parent_rate, parent_rate); |
| 162 | ret = clk_divider_ops.set_rate(fdiv_hw, rate, rate * div); |
| 163 | } else { |
| 164 | clk_divider_ops.set_rate(fdiv_hw, parent_rate, parent_rate); |
| 165 | ret = clk_divider_ops.set_rate(pdiv_hw, rate, rate * div); |
| 166 | } |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | static const struct clk_ops flexgen_ops = { |
| 172 | .enable = flexgen_enable, |
| 173 | .disable = flexgen_disable, |
| 174 | .is_enabled = flexgen_is_enabled, |
| 175 | .get_parent = flexgen_get_parent, |
| 176 | .set_parent = flexgen_set_parent, |
| 177 | .round_rate = flexgen_round_rate, |
| 178 | .recalc_rate = flexgen_recalc_rate, |
| 179 | .set_rate = flexgen_set_rate, |
| 180 | }; |
| 181 | |
Stephen Boyd | 8e6dd77 | 2015-05-01 12:45:53 -0700 | [diff] [blame] | 182 | static struct clk *clk_register_flexgen(const char *name, |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 183 | const char **parent_names, u8 num_parents, |
| 184 | void __iomem *reg, spinlock_t *lock, u32 idx, |
| 185 | unsigned long flexgen_flags) { |
| 186 | struct flexgen *fgxbar; |
| 187 | struct clk *clk; |
| 188 | struct clk_init_data init; |
| 189 | u32 xbar_shift; |
| 190 | void __iomem *xbar_reg, *fdiv_reg; |
| 191 | |
| 192 | fgxbar = kzalloc(sizeof(struct flexgen), GFP_KERNEL); |
| 193 | if (!fgxbar) |
| 194 | return ERR_PTR(-ENOMEM); |
| 195 | |
| 196 | init.name = name; |
| 197 | init.ops = &flexgen_ops; |
Pankaj Dev | 18fee45 | 2015-06-23 16:09:24 +0200 | [diff] [blame] | 198 | init.flags = CLK_IS_BASIC | CLK_GET_RATE_NOCACHE | flexgen_flags; |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 199 | init.parent_names = parent_names; |
| 200 | init.num_parents = num_parents; |
| 201 | |
| 202 | xbar_reg = reg + 0x18 + (idx & ~0x3); |
| 203 | xbar_shift = (idx % 4) * 0x8; |
| 204 | fdiv_reg = reg + 0x164 + idx * 4; |
| 205 | |
| 206 | /* Crossbar element config */ |
| 207 | fgxbar->mux.lock = lock; |
| 208 | fgxbar->mux.mask = BIT(6) - 1; |
| 209 | fgxbar->mux.reg = xbar_reg; |
| 210 | fgxbar->mux.shift = xbar_shift; |
| 211 | fgxbar->mux.table = NULL; |
| 212 | |
| 213 | |
| 214 | /* Pre-divider's gate config (in xbar register)*/ |
| 215 | fgxbar->pgate.lock = lock; |
| 216 | fgxbar->pgate.reg = xbar_reg; |
| 217 | fgxbar->pgate.bit_idx = xbar_shift + 6; |
| 218 | |
| 219 | /* Pre-divider config */ |
| 220 | fgxbar->pdiv.lock = lock; |
| 221 | fgxbar->pdiv.reg = reg + 0x58 + idx * 4; |
| 222 | fgxbar->pdiv.width = 10; |
| 223 | |
| 224 | /* Final divider's gate config */ |
| 225 | fgxbar->fgate.lock = lock; |
| 226 | fgxbar->fgate.reg = fdiv_reg; |
| 227 | fgxbar->fgate.bit_idx = 6; |
| 228 | |
| 229 | /* Final divider config */ |
| 230 | fgxbar->fdiv.lock = lock; |
| 231 | fgxbar->fdiv.reg = fdiv_reg; |
| 232 | fgxbar->fdiv.width = 6; |
| 233 | |
| 234 | fgxbar->hw.init = &init; |
| 235 | |
| 236 | clk = clk_register(NULL, &fgxbar->hw); |
| 237 | if (IS_ERR(clk)) |
| 238 | kfree(fgxbar); |
| 239 | else |
| 240 | pr_debug("%s: parent %s rate %u\n", |
| 241 | __clk_get_name(clk), |
| 242 | __clk_get_name(clk_get_parent(clk)), |
| 243 | (unsigned int)clk_get_rate(clk)); |
| 244 | return clk; |
| 245 | } |
| 246 | |
| 247 | static const char ** __init flexgen_get_parents(struct device_node *np, |
| 248 | int *num_parents) |
| 249 | { |
| 250 | const char **parents; |
Stephen Boyd | caeb057 | 2016-02-19 17:43:30 -0800 | [diff] [blame] | 251 | unsigned int nparents; |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 252 | |
Geert Uytterhoeven | 0a65239 | 2015-05-29 11:25:46 +0200 | [diff] [blame] | 253 | nparents = of_clk_get_parent_count(np); |
Stephen Boyd | caeb057 | 2016-02-19 17:43:30 -0800 | [diff] [blame] | 254 | if (WARN_ON(!nparents)) |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 255 | return NULL; |
| 256 | |
| 257 | parents = kcalloc(nparents, sizeof(const char *), GFP_KERNEL); |
| 258 | if (!parents) |
| 259 | return NULL; |
| 260 | |
Dinh Nguyen | 0b4e7f0 | 2015-07-06 22:59:04 -0500 | [diff] [blame] | 261 | *num_parents = of_clk_parent_fill(np, parents, nparents); |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 262 | |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 263 | return parents; |
| 264 | } |
| 265 | |
Gabriel Fernandez | 26bd0a5 | 2016-08-29 14:26:57 +0200 | [diff] [blame^] | 266 | static const struct clkgen_data clkgen_audio = { |
| 267 | .flags = CLK_SET_RATE_PARENT, |
| 268 | }; |
| 269 | |
| 270 | static const struct of_device_id flexgen_of_match[] = { |
| 271 | { |
| 272 | .compatible = "st,flexgen-audio", |
| 273 | .data = &clkgen_audio, |
| 274 | }, |
| 275 | {} |
| 276 | }; |
| 277 | |
Stephen Boyd | 8e6dd77 | 2015-05-01 12:45:53 -0700 | [diff] [blame] | 278 | static void __init st_of_flexgen_setup(struct device_node *np) |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 279 | { |
| 280 | struct device_node *pnode; |
| 281 | void __iomem *reg; |
| 282 | struct clk_onecell_data *clk_data; |
| 283 | const char **parents; |
| 284 | int num_parents, i; |
| 285 | spinlock_t *rlock = NULL; |
Gabriel Fernandez | 26bd0a5 | 2016-08-29 14:26:57 +0200 | [diff] [blame^] | 286 | const struct of_device_id *match; |
| 287 | struct clkgen_data *data = NULL; |
| 288 | unsigned long flex_flags = 0; |
Andrzej Hajda | a1c22a4 | 2015-09-24 16:00:16 +0200 | [diff] [blame] | 289 | int ret; |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 290 | |
| 291 | pnode = of_get_parent(np); |
| 292 | if (!pnode) |
| 293 | return; |
| 294 | |
| 295 | reg = of_iomap(pnode, 0); |
| 296 | if (!reg) |
| 297 | return; |
| 298 | |
| 299 | parents = flexgen_get_parents(np, &num_parents); |
| 300 | if (!parents) |
| 301 | return; |
| 302 | |
Gabriel Fernandez | 26bd0a5 | 2016-08-29 14:26:57 +0200 | [diff] [blame^] | 303 | match = of_match_node(flexgen_of_match, np); |
| 304 | if (match) { |
| 305 | data = (struct clkgen_data *)match->data; |
| 306 | flex_flags = data->flags; |
| 307 | } |
| 308 | |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 309 | clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL); |
| 310 | if (!clk_data) |
| 311 | goto err; |
| 312 | |
Andrzej Hajda | a1c22a4 | 2015-09-24 16:00:16 +0200 | [diff] [blame] | 313 | ret = of_property_count_strings(np, "clock-output-names"); |
| 314 | if (ret <= 0) { |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 315 | pr_err("%s: Failed to get number of output clocks (%d)", |
| 316 | __func__, clk_data->clk_num); |
| 317 | goto err; |
| 318 | } |
Andrzej Hajda | a1c22a4 | 2015-09-24 16:00:16 +0200 | [diff] [blame] | 319 | clk_data->clk_num = ret; |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 320 | |
| 321 | clk_data->clks = kcalloc(clk_data->clk_num, sizeof(struct clk *), |
| 322 | GFP_KERNEL); |
| 323 | if (!clk_data->clks) |
| 324 | goto err; |
| 325 | |
| 326 | rlock = kzalloc(sizeof(spinlock_t), GFP_KERNEL); |
| 327 | if (!rlock) |
| 328 | goto err; |
| 329 | |
Giuseppe Cavallaro | 0f4f2af | 2015-06-23 16:09:23 +0200 | [diff] [blame] | 330 | spin_lock_init(rlock); |
| 331 | |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 332 | for (i = 0; i < clk_data->clk_num; i++) { |
| 333 | struct clk *clk; |
| 334 | const char *clk_name; |
| 335 | |
| 336 | if (of_property_read_string_index(np, "clock-output-names", |
| 337 | i, &clk_name)) { |
| 338 | break; |
| 339 | } |
| 340 | |
Lee Jones | fa6415a | 2016-06-07 12:19:25 +0100 | [diff] [blame] | 341 | of_clk_detect_critical(np, i, &flex_flags); |
| 342 | |
Gabriel FERNANDEZ | b116517 | 2014-07-15 17:20:22 +0200 | [diff] [blame] | 343 | /* |
| 344 | * If we read an empty clock name then the output is unused |
| 345 | */ |
| 346 | if (*clk_name == '\0') |
| 347 | continue; |
| 348 | |
| 349 | clk = clk_register_flexgen(clk_name, parents, num_parents, |
| 350 | reg, rlock, i, flex_flags); |
| 351 | |
| 352 | if (IS_ERR(clk)) |
| 353 | goto err; |
| 354 | |
| 355 | clk_data->clks[i] = clk; |
| 356 | } |
| 357 | |
| 358 | kfree(parents); |
| 359 | of_clk_add_provider(np, of_clk_src_onecell_get, clk_data); |
| 360 | |
| 361 | return; |
| 362 | |
| 363 | err: |
| 364 | if (clk_data) |
| 365 | kfree(clk_data->clks); |
| 366 | kfree(clk_data); |
| 367 | kfree(parents); |
| 368 | kfree(rlock); |
| 369 | } |
| 370 | CLK_OF_DECLARE(flexgen, "st,flexgen", st_of_flexgen_setup); |