1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018, The Linux Foundation. All rights reserved.
4 #include <linux/kernel.h>
5 #include <linux/init.h>
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
11 #include <linux/of_device.h>
12 #include <linux/clk.h>
13 #include <linux/clk-provider.h>
14 #include <linux/slab.h>
16 #include "clk-krait.h"
18 static unsigned int sec_mux_map[] = {
23 static unsigned int pri_mux_map[] = {
30 * Notifier function for switching the muxes to safe parent
31 * while the hfpll is getting reprogrammed.
33 static int krait_notifier_cb(struct notifier_block *nb,
38 struct krait_mux_clk *mux = container_of(nb, struct krait_mux_clk,
40 /* Switch to safe parent */
41 if (event == PRE_RATE_CHANGE) {
42 mux->old_index = krait_mux_clk_ops.get_parent(&mux->hw);
43 ret = krait_mux_clk_ops.set_parent(&mux->hw, mux->safe_sel);
44 mux->reparent = false;
46 * By the time POST_RATE_CHANGE notifier is called,
47 * clk framework itself would have changed the parent for the new rate.
48 * Only otherwise, put back to the old parent.
50 } else if (event == POST_RATE_CHANGE) {
52 ret = krait_mux_clk_ops.set_parent(&mux->hw,
56 return notifier_from_errno(ret);
59 static int krait_notifier_register(struct device *dev, struct clk *clk,
60 struct krait_mux_clk *mux)
64 mux->clk_nb.notifier_call = krait_notifier_cb;
65 ret = clk_notifier_register(clk, &mux->clk_nb);
67 dev_err(dev, "failed to register clock notifier: %d\n", ret);
73 krait_add_div(struct device *dev, int id, const char *s, unsigned int offset)
75 struct krait_div2_clk *div;
76 struct clk_init_data init = {
78 .ops = &krait_div2_clk_ops,
79 .flags = CLK_SET_RATE_PARENT,
81 const char *p_names[1];
84 div = devm_kzalloc(dev, sizeof(*div), GFP_KERNEL);
94 init.name = kasprintf(GFP_KERNEL, "hfpll%s_div", s);
98 init.parent_names = p_names;
99 p_names[0] = kasprintf(GFP_KERNEL, "hfpll%s", s);
105 clk = devm_clk_register(dev, &div->hw);
109 return PTR_ERR_OR_ZERO(clk);
113 krait_add_sec_mux(struct device *dev, int id, const char *s,
114 unsigned int offset, bool unique_aux)
117 struct krait_mux_clk *mux;
118 static const char *sec_mux_list[] = {
122 struct clk_init_data init = {
123 .parent_names = sec_mux_list,
124 .num_parents = ARRAY_SIZE(sec_mux_list),
125 .ops = &krait_mux_clk_ops,
126 .flags = CLK_SET_RATE_PARENT,
130 mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
134 mux->offset = offset;
138 mux->parent_map = sec_mux_map;
139 mux->hw.init = &init;
142 /* Checking for qcom,krait-cc-v1 or qcom,krait-cc-v2 is not
143 * enough to limit this to apq/ipq8064. Directly check machine
144 * compatible to correctly handle this errata.
146 if (of_machine_is_compatible("qcom,ipq8064") ||
147 of_machine_is_compatible("qcom,apq8064"))
148 mux->disable_sec_src_gating = true;
150 init.name = kasprintf(GFP_KERNEL, "krait%s_sec_mux", s);
155 sec_mux_list[0] = kasprintf(GFP_KERNEL, "acpu%s_aux", s);
156 if (!sec_mux_list[0]) {
157 clk = ERR_PTR(-ENOMEM);
162 clk = devm_clk_register(dev, &mux->hw);
164 ret = krait_notifier_register(dev, clk, mux);
170 kfree(sec_mux_list[0]);
173 return PTR_ERR_OR_ZERO(clk);
177 krait_add_pri_mux(struct device *dev, int id, const char *s,
181 struct krait_mux_clk *mux;
182 const char *p_names[3];
183 struct clk_init_data init = {
184 .parent_names = p_names,
185 .num_parents = ARRAY_SIZE(p_names),
186 .ops = &krait_mux_clk_ops,
187 .flags = CLK_SET_RATE_PARENT,
191 mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
193 return ERR_PTR(-ENOMEM);
197 mux->offset = offset;
199 mux->parent_map = pri_mux_map;
200 mux->hw.init = &init;
203 init.name = kasprintf(GFP_KERNEL, "krait%s_pri_mux", s);
205 return ERR_PTR(-ENOMEM);
207 p_names[0] = kasprintf(GFP_KERNEL, "hfpll%s", s);
209 clk = ERR_PTR(-ENOMEM);
213 p_names[1] = kasprintf(GFP_KERNEL, "hfpll%s_div", s);
215 clk = ERR_PTR(-ENOMEM);
219 p_names[2] = kasprintf(GFP_KERNEL, "krait%s_sec_mux", s);
221 clk = ERR_PTR(-ENOMEM);
225 clk = devm_clk_register(dev, &mux->hw);
227 ret = krait_notifier_register(dev, clk, mux);
241 /* id < 0 for L2, otherwise id == physical CPU number */
242 static struct clk *krait_add_clks(struct device *dev, int id, bool unique_aux)
251 offset = 0x4501 + (0x1000 * id);
252 s = p = kasprintf(GFP_KERNEL, "%d", id);
254 return ERR_PTR(-ENOMEM);
260 ret = krait_add_div(dev, id, s, offset);
266 ret = krait_add_sec_mux(dev, id, s, offset, unique_aux);
272 clk = krait_add_pri_mux(dev, id, s, offset);
278 static struct clk *krait_of_get(struct of_phandle_args *clkspec, void *data)
280 unsigned int idx = clkspec->args[0];
281 struct clk **clks = data;
284 pr_err("%s: invalid clock index %d\n", __func__, idx);
285 return ERR_PTR(-EINVAL);
288 return clks[idx] ? : ERR_PTR(-ENODEV);
291 static const struct of_device_id krait_cc_match_table[] = {
292 { .compatible = "qcom,krait-cc-v1", (void *)1UL },
293 { .compatible = "qcom,krait-cc-v2" },
296 MODULE_DEVICE_TABLE(of, krait_cc_match_table);
298 static int krait_cc_probe(struct platform_device *pdev)
300 struct device *dev = &pdev->dev;
301 const struct of_device_id *id;
302 unsigned long cur_rate, aux_rate;
306 struct clk *l2_pri_mux_clk;
308 id = of_match_device(krait_cc_match_table, dev);
312 /* Rate is 1 because 0 causes problems for __clk_mux_determine_rate */
313 clk = clk_register_fixed_rate(dev, "qsb", NULL, 0, 1);
318 clk = clk_register_fixed_factor(dev, "acpu_aux",
319 "gpll0_vote", 0, 1, 2);
324 /* Krait configurations have at most 4 CPUs and one L2 */
325 clks = devm_kcalloc(dev, 5, sizeof(*clks), GFP_KERNEL);
329 for_each_possible_cpu(cpu) {
330 clk = krait_add_clks(dev, cpu, id->data);
336 l2_pri_mux_clk = krait_add_clks(dev, -1, id->data);
337 if (IS_ERR(l2_pri_mux_clk))
338 return PTR_ERR(l2_pri_mux_clk);
339 clks[4] = l2_pri_mux_clk;
342 * We don't want the CPU or L2 clocks to be turned off at late init
343 * if CPUFREQ or HOTPLUG configs are disabled. So, bump up the
344 * refcount of these clocks. Any cpufreq/hotplug manager can assume
345 * that the clocks have already been prepared and enabled by the time
348 for_each_online_cpu(cpu) {
349 clk_prepare_enable(l2_pri_mux_clk);
350 WARN(clk_prepare_enable(clks[cpu]),
351 "Unable to turn on CPU%d clock", cpu);
355 * Force reinit of HFPLLs and muxes to overwrite any potential
356 * incorrect configuration of HFPLLs and muxes by the bootloader.
357 * While at it, also make sure the cores are running at known rates
358 * and print the current rate.
360 * The clocks are set to aux clock rate first to make sure the
361 * secondary mux is not sourcing off of QSB. The rate is then set to
362 * two different rates to force a HFPLL reinit under all
365 cur_rate = clk_get_rate(l2_pri_mux_clk);
366 aux_rate = 384000000;
368 pr_info("L2 @ QSB rate. Forcing new rate.\n");
371 clk_set_rate(l2_pri_mux_clk, aux_rate);
372 clk_set_rate(l2_pri_mux_clk, 2);
373 clk_set_rate(l2_pri_mux_clk, cur_rate);
374 pr_info("L2 @ %lu KHz\n", clk_get_rate(l2_pri_mux_clk) / 1000);
375 for_each_possible_cpu(cpu) {
377 cur_rate = clk_get_rate(clk);
379 pr_info("CPU%d @ QSB rate. Forcing new rate.\n", cpu);
383 clk_set_rate(clk, aux_rate);
384 clk_set_rate(clk, 2);
385 clk_set_rate(clk, cur_rate);
386 pr_info("CPU%d @ %lu KHz\n", cpu, clk_get_rate(clk) / 1000);
389 of_clk_add_provider(dev->of_node, krait_of_get, clks);
394 static struct platform_driver krait_cc_driver = {
395 .probe = krait_cc_probe,
398 .of_match_table = krait_cc_match_table,
401 module_platform_driver(krait_cc_driver);
403 MODULE_DESCRIPTION("Krait CPU Clock Driver");
404 MODULE_LICENSE("GPL v2");
405 MODULE_ALIAS("platform:krait-cc");