2 * Copyright (c) 2015 Linaro Ltd.
3 * Author: Pi-Cheng Chen <pi-cheng.chen@linaro.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/clk.h>
16 #include <linux/cpu.h>
17 #include <linux/cpufreq.h>
18 #include <linux/cpumask.h>
19 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_opp.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/slab.h>
25 #include <linux/thermal.h>
27 #define MIN_VOLT_SHIFT (100000)
28 #define MAX_VOLT_SHIFT (200000)
29 #define MAX_VOLT_LIMIT (1150000)
30 #define VOLT_TOL (10000)
33 * The struct mtk_cpu_dvfs_info holds necessary information for doing CPU DVFS
34 * on each CPU power/clock domain of Mediatek SoCs. Each CPU cluster in
35 * Mediatek SoCs has two voltage inputs, Vproc and Vsram. In some cases the two
36 * voltage inputs need to be controlled under a hardware limitation:
37 * 100mV < Vsram - Vproc < 200mV
39 * When scaling the clock frequency of a CPU clock domain, the clock source
40 * needs to be switched to another stable PLL clock temporarily until
41 * the original PLL becomes stable at target frequency.
43 struct mtk_cpu_dvfs_info {
45 struct device *cpu_dev;
46 struct regulator *proc_reg;
47 struct regulator *sram_reg;
49 struct clk *inter_clk;
50 struct list_head list_head;
51 int intermediate_voltage;
52 bool need_voltage_tracking;
55 static LIST_HEAD(dvfs_info_list);
57 static struct mtk_cpu_dvfs_info *mtk_cpu_dvfs_info_lookup(int cpu)
59 struct mtk_cpu_dvfs_info *info;
61 list_for_each_entry(info, &dvfs_info_list, list_head) {
62 if (cpumask_test_cpu(cpu, &info->cpus))
69 static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info,
72 struct regulator *proc_reg = info->proc_reg;
73 struct regulator *sram_reg = info->sram_reg;
74 int old_vproc, old_vsram, new_vsram, vsram, vproc, ret;
76 old_vproc = regulator_get_voltage(proc_reg);
78 pr_err("%s: invalid Vproc value: %d\n", __func__, old_vproc);
81 /* Vsram should not exceed the maximum allowed voltage of SoC. */
82 new_vsram = min(new_vproc + MIN_VOLT_SHIFT, MAX_VOLT_LIMIT);
84 if (old_vproc < new_vproc) {
86 * When scaling up voltages, Vsram and Vproc scale up step
87 * by step. At each step, set Vsram to (Vproc + 200mV) first,
88 * then set Vproc to (Vsram - 100mV).
89 * Keep doing it until Vsram and Vproc hit target voltages.
92 old_vsram = regulator_get_voltage(sram_reg);
94 pr_err("%s: invalid Vsram value: %d\n",
98 old_vproc = regulator_get_voltage(proc_reg);
100 pr_err("%s: invalid Vproc value: %d\n",
101 __func__, old_vproc);
105 vsram = min(new_vsram, old_vproc + MAX_VOLT_SHIFT);
107 if (vsram + VOLT_TOL >= MAX_VOLT_LIMIT) {
108 vsram = MAX_VOLT_LIMIT;
111 * If the target Vsram hits the maximum voltage,
112 * try to set the exact voltage value first.
114 ret = regulator_set_voltage(sram_reg, vsram,
117 ret = regulator_set_voltage(sram_reg,
123 ret = regulator_set_voltage(sram_reg, vsram,
126 vproc = vsram - MIN_VOLT_SHIFT;
131 ret = regulator_set_voltage(proc_reg, vproc,
134 regulator_set_voltage(sram_reg, old_vsram,
138 } while (vproc < new_vproc || vsram < new_vsram);
139 } else if (old_vproc > new_vproc) {
141 * When scaling down voltages, Vsram and Vproc scale down step
142 * by step. At each step, set Vproc to (Vsram - 200mV) first,
143 * then set Vproc to (Vproc + 100mV).
144 * Keep doing it until Vsram and Vproc hit target voltages.
147 old_vproc = regulator_get_voltage(proc_reg);
149 pr_err("%s: invalid Vproc value: %d\n",
150 __func__, old_vproc);
153 old_vsram = regulator_get_voltage(sram_reg);
155 pr_err("%s: invalid Vsram value: %d\n",
156 __func__, old_vsram);
160 vproc = max(new_vproc, old_vsram - MAX_VOLT_SHIFT);
161 ret = regulator_set_voltage(proc_reg, vproc,
166 if (vproc == new_vproc)
169 vsram = max(new_vsram, vproc + MIN_VOLT_SHIFT);
171 if (vsram + VOLT_TOL >= MAX_VOLT_LIMIT) {
172 vsram = MAX_VOLT_LIMIT;
175 * If the target Vsram hits the maximum voltage,
176 * try to set the exact voltage value first.
178 ret = regulator_set_voltage(sram_reg, vsram,
181 ret = regulator_set_voltage(sram_reg,
185 ret = regulator_set_voltage(sram_reg, vsram,
190 regulator_set_voltage(proc_reg, old_vproc,
194 } while (vproc > new_vproc + VOLT_TOL ||
195 vsram > new_vsram + VOLT_TOL);
201 static int mtk_cpufreq_set_voltage(struct mtk_cpu_dvfs_info *info, int vproc)
203 if (info->need_voltage_tracking)
204 return mtk_cpufreq_voltage_tracking(info, vproc);
206 return regulator_set_voltage(info->proc_reg, vproc,
210 static int mtk_cpufreq_set_target(struct cpufreq_policy *policy,
213 struct cpufreq_frequency_table *freq_table = policy->freq_table;
214 struct clk *cpu_clk = policy->clk;
215 struct clk *armpll = clk_get_parent(cpu_clk);
216 struct mtk_cpu_dvfs_info *info = policy->driver_data;
217 struct device *cpu_dev = info->cpu_dev;
218 struct dev_pm_opp *opp;
219 long freq_hz, old_freq_hz;
220 int vproc, old_vproc, inter_vproc, target_vproc, ret;
222 inter_vproc = info->intermediate_voltage;
224 old_freq_hz = clk_get_rate(cpu_clk);
225 old_vproc = regulator_get_voltage(info->proc_reg);
227 pr_err("%s: invalid Vproc value: %d\n", __func__, old_vproc);
231 freq_hz = freq_table[index].frequency * 1000;
233 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz);
235 pr_err("cpu%d: failed to find OPP for %ld\n",
236 policy->cpu, freq_hz);
239 vproc = dev_pm_opp_get_voltage(opp);
243 * If the new voltage or the intermediate voltage is higher than the
244 * current voltage, scale up voltage first.
246 target_vproc = (inter_vproc > vproc) ? inter_vproc : vproc;
247 if (old_vproc < target_vproc) {
248 ret = mtk_cpufreq_set_voltage(info, target_vproc);
250 pr_err("cpu%d: failed to scale up voltage!\n",
252 mtk_cpufreq_set_voltage(info, old_vproc);
257 /* Reparent the CPU clock to intermediate clock. */
258 ret = clk_set_parent(cpu_clk, info->inter_clk);
260 pr_err("cpu%d: failed to re-parent cpu clock!\n",
262 mtk_cpufreq_set_voltage(info, old_vproc);
267 /* Set the original PLL to target rate. */
268 ret = clk_set_rate(armpll, freq_hz);
270 pr_err("cpu%d: failed to scale cpu clock rate!\n",
272 clk_set_parent(cpu_clk, armpll);
273 mtk_cpufreq_set_voltage(info, old_vproc);
277 /* Set parent of CPU clock back to the original PLL. */
278 ret = clk_set_parent(cpu_clk, armpll);
280 pr_err("cpu%d: failed to re-parent cpu clock!\n",
282 mtk_cpufreq_set_voltage(info, inter_vproc);
288 * If the new voltage is lower than the intermediate voltage or the
289 * original voltage, scale down to the new voltage.
291 if (vproc < inter_vproc || vproc < old_vproc) {
292 ret = mtk_cpufreq_set_voltage(info, vproc);
294 pr_err("cpu%d: failed to scale down voltage!\n",
296 clk_set_parent(cpu_clk, info->inter_clk);
297 clk_set_rate(armpll, old_freq_hz);
298 clk_set_parent(cpu_clk, armpll);
306 #define DYNAMIC_POWER "dynamic-power-coefficient"
308 static int mtk_cpu_dvfs_info_init(struct mtk_cpu_dvfs_info *info, int cpu)
310 struct device *cpu_dev;
311 struct regulator *proc_reg = ERR_PTR(-ENODEV);
312 struct regulator *sram_reg = ERR_PTR(-ENODEV);
313 struct clk *cpu_clk = ERR_PTR(-ENODEV);
314 struct clk *inter_clk = ERR_PTR(-ENODEV);
315 struct dev_pm_opp *opp;
319 cpu_dev = get_cpu_device(cpu);
321 pr_err("failed to get cpu%d device\n", cpu);
325 cpu_clk = clk_get(cpu_dev, "cpu");
326 if (IS_ERR(cpu_clk)) {
327 if (PTR_ERR(cpu_clk) == -EPROBE_DEFER)
328 pr_warn("cpu clk for cpu%d not ready, retry.\n", cpu);
330 pr_err("failed to get cpu clk for cpu%d\n", cpu);
332 ret = PTR_ERR(cpu_clk);
336 inter_clk = clk_get(cpu_dev, "intermediate");
337 if (IS_ERR(inter_clk)) {
338 if (PTR_ERR(inter_clk) == -EPROBE_DEFER)
339 pr_warn("intermediate clk for cpu%d not ready, retry.\n",
342 pr_err("failed to get intermediate clk for cpu%d\n",
345 ret = PTR_ERR(inter_clk);
346 goto out_free_resources;
349 proc_reg = regulator_get_exclusive(cpu_dev, "proc");
350 if (IS_ERR(proc_reg)) {
351 if (PTR_ERR(proc_reg) == -EPROBE_DEFER)
352 pr_warn("proc regulator for cpu%d not ready, retry.\n",
355 pr_err("failed to get proc regulator for cpu%d\n",
358 ret = PTR_ERR(proc_reg);
359 goto out_free_resources;
362 /* Both presence and absence of sram regulator are valid cases. */
363 sram_reg = regulator_get_exclusive(cpu_dev, "sram");
365 /* Get OPP-sharing information from "operating-points-v2" bindings */
366 ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, &info->cpus);
368 pr_err("failed to get OPP-sharing information for cpu%d\n",
370 goto out_free_resources;
373 ret = dev_pm_opp_of_cpumask_add_table(&info->cpus);
375 pr_warn("no OPP table for cpu%d\n", cpu);
376 goto out_free_resources;
379 /* Search a safe voltage for intermediate frequency. */
380 rate = clk_get_rate(inter_clk);
381 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &rate);
383 pr_err("failed to get intermediate opp for cpu%d\n", cpu);
385 goto out_free_opp_table;
387 info->intermediate_voltage = dev_pm_opp_get_voltage(opp);
390 info->cpu_dev = cpu_dev;
391 info->proc_reg = proc_reg;
392 info->sram_reg = IS_ERR(sram_reg) ? NULL : sram_reg;
393 info->cpu_clk = cpu_clk;
394 info->inter_clk = inter_clk;
397 * If SRAM regulator is present, software "voltage tracking" is needed
398 * for this CPU power domain.
400 info->need_voltage_tracking = !IS_ERR(sram_reg);
405 dev_pm_opp_of_cpumask_remove_table(&info->cpus);
408 if (!IS_ERR(proc_reg))
409 regulator_put(proc_reg);
410 if (!IS_ERR(sram_reg))
411 regulator_put(sram_reg);
412 if (!IS_ERR(cpu_clk))
414 if (!IS_ERR(inter_clk))
420 static void mtk_cpu_dvfs_info_release(struct mtk_cpu_dvfs_info *info)
422 if (!IS_ERR(info->proc_reg))
423 regulator_put(info->proc_reg);
424 if (!IS_ERR(info->sram_reg))
425 regulator_put(info->sram_reg);
426 if (!IS_ERR(info->cpu_clk))
427 clk_put(info->cpu_clk);
428 if (!IS_ERR(info->inter_clk))
429 clk_put(info->inter_clk);
431 dev_pm_opp_of_cpumask_remove_table(&info->cpus);
434 static int mtk_cpufreq_init(struct cpufreq_policy *policy)
436 struct mtk_cpu_dvfs_info *info;
437 struct cpufreq_frequency_table *freq_table;
440 info = mtk_cpu_dvfs_info_lookup(policy->cpu);
442 pr_err("dvfs info for cpu%d is not initialized.\n",
447 ret = dev_pm_opp_init_cpufreq_table(info->cpu_dev, &freq_table);
449 pr_err("failed to init cpufreq table for cpu%d: %d\n",
454 cpumask_copy(policy->cpus, &info->cpus);
455 policy->freq_table = freq_table;
456 policy->driver_data = info;
457 policy->clk = info->cpu_clk;
459 dev_pm_opp_of_register_em(policy->cpus);
464 static int mtk_cpufreq_exit(struct cpufreq_policy *policy)
466 struct mtk_cpu_dvfs_info *info = policy->driver_data;
468 dev_pm_opp_free_cpufreq_table(info->cpu_dev, &policy->freq_table);
473 static struct cpufreq_driver mtk_cpufreq_driver = {
474 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK |
475 CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
476 CPUFREQ_IS_COOLING_DEV,
477 .verify = cpufreq_generic_frequency_table_verify,
478 .target_index = mtk_cpufreq_set_target,
479 .get = cpufreq_generic_get,
480 .init = mtk_cpufreq_init,
481 .exit = mtk_cpufreq_exit,
482 .name = "mtk-cpufreq",
483 .attr = cpufreq_generic_attr,
486 static int mtk_cpufreq_probe(struct platform_device *pdev)
488 struct mtk_cpu_dvfs_info *info, *tmp;
491 for_each_possible_cpu(cpu) {
492 info = mtk_cpu_dvfs_info_lookup(cpu);
496 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
499 goto release_dvfs_info_list;
502 ret = mtk_cpu_dvfs_info_init(info, cpu);
505 "failed to initialize dvfs info for cpu%d\n",
507 goto release_dvfs_info_list;
510 list_add(&info->list_head, &dvfs_info_list);
513 ret = cpufreq_register_driver(&mtk_cpufreq_driver);
515 dev_err(&pdev->dev, "failed to register mtk cpufreq driver\n");
516 goto release_dvfs_info_list;
521 release_dvfs_info_list:
522 list_for_each_entry_safe(info, tmp, &dvfs_info_list, list_head) {
523 mtk_cpu_dvfs_info_release(info);
524 list_del(&info->list_head);
530 static struct platform_driver mtk_cpufreq_platdrv = {
532 .name = "mtk-cpufreq",
534 .probe = mtk_cpufreq_probe,
537 /* List of machines supported by this driver */
538 static const struct of_device_id mtk_cpufreq_machines[] __initconst = {
539 { .compatible = "mediatek,mt2701", },
540 { .compatible = "mediatek,mt2712", },
541 { .compatible = "mediatek,mt7622", },
542 { .compatible = "mediatek,mt7623", },
543 { .compatible = "mediatek,mt817x", },
544 { .compatible = "mediatek,mt8173", },
545 { .compatible = "mediatek,mt8176", },
550 static int __init mtk_cpufreq_driver_init(void)
552 struct device_node *np;
553 const struct of_device_id *match;
554 struct platform_device *pdev;
557 np = of_find_node_by_path("/");
561 match = of_match_node(mtk_cpufreq_machines, np);
564 pr_debug("Machine is not compatible with mtk-cpufreq\n");
568 err = platform_driver_register(&mtk_cpufreq_platdrv);
573 * Since there's no place to hold device registration code and no
574 * device tree based way to match cpufreq driver yet, both the driver
575 * and the device registration codes are put here to handle defer
578 pdev = platform_device_register_simple("mtk-cpufreq", -1, NULL, 0);
580 pr_err("failed to register mtk-cpufreq platform device\n");
581 return PTR_ERR(pdev);
586 device_initcall(mtk_cpufreq_driver_init);
588 MODULE_DESCRIPTION("MediaTek CPUFreq driver");
589 MODULE_AUTHOR("Pi-Cheng Chen <pi-cheng.chen@linaro.org>");
590 MODULE_LICENSE("GPL v2");