1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 Linaro Ltd.
4 * Author: Pi-Cheng Chen <pi-cheng.chen@linaro.org>
9 #include <linux/cpufreq.h>
10 #include <linux/cpumask.h>
11 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_opp.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/slab.h>
17 #include <linux/thermal.h>
19 #define MIN_VOLT_SHIFT (100000)
20 #define MAX_VOLT_SHIFT (200000)
21 #define MAX_VOLT_LIMIT (1150000)
22 #define VOLT_TOL (10000)
25 * The struct mtk_cpu_dvfs_info holds necessary information for doing CPU DVFS
26 * on each CPU power/clock domain of Mediatek SoCs. Each CPU cluster in
27 * Mediatek SoCs has two voltage inputs, Vproc and Vsram. In some cases the two
28 * voltage inputs need to be controlled under a hardware limitation:
29 * 100mV < Vsram - Vproc < 200mV
31 * When scaling the clock frequency of a CPU clock domain, the clock source
32 * needs to be switched to another stable PLL clock temporarily until
33 * the original PLL becomes stable at target frequency.
35 struct mtk_cpu_dvfs_info
{
37 struct device
*cpu_dev
;
38 struct regulator
*proc_reg
;
39 struct regulator
*sram_reg
;
41 struct clk
*inter_clk
;
42 struct list_head list_head
;
43 int intermediate_voltage
;
44 bool need_voltage_tracking
;
47 static LIST_HEAD(dvfs_info_list
);
49 static struct mtk_cpu_dvfs_info
*mtk_cpu_dvfs_info_lookup(int cpu
)
51 struct mtk_cpu_dvfs_info
*info
;
53 list_for_each_entry(info
, &dvfs_info_list
, list_head
) {
54 if (cpumask_test_cpu(cpu
, &info
->cpus
))
61 static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info
*info
,
64 struct regulator
*proc_reg
= info
->proc_reg
;
65 struct regulator
*sram_reg
= info
->sram_reg
;
66 int old_vproc
, old_vsram
, new_vsram
, vsram
, vproc
, ret
;
68 old_vproc
= regulator_get_voltage(proc_reg
);
70 pr_err("%s: invalid Vproc value: %d\n", __func__
, old_vproc
);
73 /* Vsram should not exceed the maximum allowed voltage of SoC. */
74 new_vsram
= min(new_vproc
+ MIN_VOLT_SHIFT
, MAX_VOLT_LIMIT
);
76 if (old_vproc
< new_vproc
) {
78 * When scaling up voltages, Vsram and Vproc scale up step
79 * by step. At each step, set Vsram to (Vproc + 200mV) first,
80 * then set Vproc to (Vsram - 100mV).
81 * Keep doing it until Vsram and Vproc hit target voltages.
84 old_vsram
= regulator_get_voltage(sram_reg
);
86 pr_err("%s: invalid Vsram value: %d\n",
90 old_vproc
= regulator_get_voltage(proc_reg
);
92 pr_err("%s: invalid Vproc value: %d\n",
97 vsram
= min(new_vsram
, old_vproc
+ MAX_VOLT_SHIFT
);
99 if (vsram
+ VOLT_TOL
>= MAX_VOLT_LIMIT
) {
100 vsram
= MAX_VOLT_LIMIT
;
103 * If the target Vsram hits the maximum voltage,
104 * try to set the exact voltage value first.
106 ret
= regulator_set_voltage(sram_reg
, vsram
,
109 ret
= regulator_set_voltage(sram_reg
,
115 ret
= regulator_set_voltage(sram_reg
, vsram
,
118 vproc
= vsram
- MIN_VOLT_SHIFT
;
123 ret
= regulator_set_voltage(proc_reg
, vproc
,
126 regulator_set_voltage(sram_reg
, old_vsram
,
130 } while (vproc
< new_vproc
|| vsram
< new_vsram
);
131 } else if (old_vproc
> new_vproc
) {
133 * When scaling down voltages, Vsram and Vproc scale down step
134 * by step. At each step, set Vproc to (Vsram - 200mV) first,
135 * then set Vproc to (Vproc + 100mV).
136 * Keep doing it until Vsram and Vproc hit target voltages.
139 old_vproc
= regulator_get_voltage(proc_reg
);
141 pr_err("%s: invalid Vproc value: %d\n",
142 __func__
, old_vproc
);
145 old_vsram
= regulator_get_voltage(sram_reg
);
147 pr_err("%s: invalid Vsram value: %d\n",
148 __func__
, old_vsram
);
152 vproc
= max(new_vproc
, old_vsram
- MAX_VOLT_SHIFT
);
153 ret
= regulator_set_voltage(proc_reg
, vproc
,
158 if (vproc
== new_vproc
)
161 vsram
= max(new_vsram
, vproc
+ MIN_VOLT_SHIFT
);
163 if (vsram
+ VOLT_TOL
>= MAX_VOLT_LIMIT
) {
164 vsram
= MAX_VOLT_LIMIT
;
167 * If the target Vsram hits the maximum voltage,
168 * try to set the exact voltage value first.
170 ret
= regulator_set_voltage(sram_reg
, vsram
,
173 ret
= regulator_set_voltage(sram_reg
,
177 ret
= regulator_set_voltage(sram_reg
, vsram
,
182 regulator_set_voltage(proc_reg
, old_vproc
,
186 } while (vproc
> new_vproc
+ VOLT_TOL
||
187 vsram
> new_vsram
+ VOLT_TOL
);
193 static int mtk_cpufreq_set_voltage(struct mtk_cpu_dvfs_info
*info
, int vproc
)
195 if (info
->need_voltage_tracking
)
196 return mtk_cpufreq_voltage_tracking(info
, vproc
);
198 return regulator_set_voltage(info
->proc_reg
, vproc
,
202 static int mtk_cpufreq_set_target(struct cpufreq_policy
*policy
,
205 struct cpufreq_frequency_table
*freq_table
= policy
->freq_table
;
206 struct clk
*cpu_clk
= policy
->clk
;
207 struct clk
*armpll
= clk_get_parent(cpu_clk
);
208 struct mtk_cpu_dvfs_info
*info
= policy
->driver_data
;
209 struct device
*cpu_dev
= info
->cpu_dev
;
210 struct dev_pm_opp
*opp
;
211 long freq_hz
, old_freq_hz
;
212 int vproc
, old_vproc
, inter_vproc
, target_vproc
, ret
;
214 inter_vproc
= info
->intermediate_voltage
;
216 old_freq_hz
= clk_get_rate(cpu_clk
);
217 old_vproc
= regulator_get_voltage(info
->proc_reg
);
219 pr_err("%s: invalid Vproc value: %d\n", __func__
, old_vproc
);
223 freq_hz
= freq_table
[index
].frequency
* 1000;
225 opp
= dev_pm_opp_find_freq_ceil(cpu_dev
, &freq_hz
);
227 pr_err("cpu%d: failed to find OPP for %ld\n",
228 policy
->cpu
, freq_hz
);
231 vproc
= dev_pm_opp_get_voltage(opp
);
235 * If the new voltage or the intermediate voltage is higher than the
236 * current voltage, scale up voltage first.
238 target_vproc
= (inter_vproc
> vproc
) ? inter_vproc
: vproc
;
239 if (old_vproc
< target_vproc
) {
240 ret
= mtk_cpufreq_set_voltage(info
, target_vproc
);
242 pr_err("cpu%d: failed to scale up voltage!\n",
244 mtk_cpufreq_set_voltage(info
, old_vproc
);
249 /* Reparent the CPU clock to intermediate clock. */
250 ret
= clk_set_parent(cpu_clk
, info
->inter_clk
);
252 pr_err("cpu%d: failed to re-parent cpu clock!\n",
254 mtk_cpufreq_set_voltage(info
, old_vproc
);
259 /* Set the original PLL to target rate. */
260 ret
= clk_set_rate(armpll
, freq_hz
);
262 pr_err("cpu%d: failed to scale cpu clock rate!\n",
264 clk_set_parent(cpu_clk
, armpll
);
265 mtk_cpufreq_set_voltage(info
, old_vproc
);
269 /* Set parent of CPU clock back to the original PLL. */
270 ret
= clk_set_parent(cpu_clk
, armpll
);
272 pr_err("cpu%d: failed to re-parent cpu clock!\n",
274 mtk_cpufreq_set_voltage(info
, inter_vproc
);
280 * If the new voltage is lower than the intermediate voltage or the
281 * original voltage, scale down to the new voltage.
283 if (vproc
< inter_vproc
|| vproc
< old_vproc
) {
284 ret
= mtk_cpufreq_set_voltage(info
, vproc
);
286 pr_err("cpu%d: failed to scale down voltage!\n",
288 clk_set_parent(cpu_clk
, info
->inter_clk
);
289 clk_set_rate(armpll
, old_freq_hz
);
290 clk_set_parent(cpu_clk
, armpll
);
298 #define DYNAMIC_POWER "dynamic-power-coefficient"
300 static int mtk_cpu_dvfs_info_init(struct mtk_cpu_dvfs_info
*info
, int cpu
)
302 struct device
*cpu_dev
;
303 struct regulator
*proc_reg
= ERR_PTR(-ENODEV
);
304 struct regulator
*sram_reg
= ERR_PTR(-ENODEV
);
305 struct clk
*cpu_clk
= ERR_PTR(-ENODEV
);
306 struct clk
*inter_clk
= ERR_PTR(-ENODEV
);
307 struct dev_pm_opp
*opp
;
311 cpu_dev
= get_cpu_device(cpu
);
313 pr_err("failed to get cpu%d device\n", cpu
);
317 cpu_clk
= clk_get(cpu_dev
, "cpu");
318 if (IS_ERR(cpu_clk
)) {
319 if (PTR_ERR(cpu_clk
) == -EPROBE_DEFER
)
320 pr_warn("cpu clk for cpu%d not ready, retry.\n", cpu
);
322 pr_err("failed to get cpu clk for cpu%d\n", cpu
);
324 ret
= PTR_ERR(cpu_clk
);
328 inter_clk
= clk_get(cpu_dev
, "intermediate");
329 if (IS_ERR(inter_clk
)) {
330 if (PTR_ERR(inter_clk
) == -EPROBE_DEFER
)
331 pr_warn("intermediate clk for cpu%d not ready, retry.\n",
334 pr_err("failed to get intermediate clk for cpu%d\n",
337 ret
= PTR_ERR(inter_clk
);
338 goto out_free_resources
;
341 proc_reg
= regulator_get_optional(cpu_dev
, "proc");
342 if (IS_ERR(proc_reg
)) {
343 if (PTR_ERR(proc_reg
) == -EPROBE_DEFER
)
344 pr_warn("proc regulator for cpu%d not ready, retry.\n",
347 pr_err("failed to get proc regulator for cpu%d\n",
350 ret
= PTR_ERR(proc_reg
);
351 goto out_free_resources
;
354 /* Both presence and absence of sram regulator are valid cases. */
355 sram_reg
= regulator_get_exclusive(cpu_dev
, "sram");
357 /* Get OPP-sharing information from "operating-points-v2" bindings */
358 ret
= dev_pm_opp_of_get_sharing_cpus(cpu_dev
, &info
->cpus
);
360 pr_err("failed to get OPP-sharing information for cpu%d\n",
362 goto out_free_resources
;
365 ret
= dev_pm_opp_of_cpumask_add_table(&info
->cpus
);
367 pr_warn("no OPP table for cpu%d\n", cpu
);
368 goto out_free_resources
;
371 /* Search a safe voltage for intermediate frequency. */
372 rate
= clk_get_rate(inter_clk
);
373 opp
= dev_pm_opp_find_freq_ceil(cpu_dev
, &rate
);
375 pr_err("failed to get intermediate opp for cpu%d\n", cpu
);
377 goto out_free_opp_table
;
379 info
->intermediate_voltage
= dev_pm_opp_get_voltage(opp
);
382 info
->cpu_dev
= cpu_dev
;
383 info
->proc_reg
= proc_reg
;
384 info
->sram_reg
= IS_ERR(sram_reg
) ? NULL
: sram_reg
;
385 info
->cpu_clk
= cpu_clk
;
386 info
->inter_clk
= inter_clk
;
389 * If SRAM regulator is present, software "voltage tracking" is needed
390 * for this CPU power domain.
392 info
->need_voltage_tracking
= !IS_ERR(sram_reg
);
397 dev_pm_opp_of_cpumask_remove_table(&info
->cpus
);
400 if (!IS_ERR(proc_reg
))
401 regulator_put(proc_reg
);
402 if (!IS_ERR(sram_reg
))
403 regulator_put(sram_reg
);
404 if (!IS_ERR(cpu_clk
))
406 if (!IS_ERR(inter_clk
))
412 static void mtk_cpu_dvfs_info_release(struct mtk_cpu_dvfs_info
*info
)
414 if (!IS_ERR(info
->proc_reg
))
415 regulator_put(info
->proc_reg
);
416 if (!IS_ERR(info
->sram_reg
))
417 regulator_put(info
->sram_reg
);
418 if (!IS_ERR(info
->cpu_clk
))
419 clk_put(info
->cpu_clk
);
420 if (!IS_ERR(info
->inter_clk
))
421 clk_put(info
->inter_clk
);
423 dev_pm_opp_of_cpumask_remove_table(&info
->cpus
);
426 static int mtk_cpufreq_init(struct cpufreq_policy
*policy
)
428 struct mtk_cpu_dvfs_info
*info
;
429 struct cpufreq_frequency_table
*freq_table
;
432 info
= mtk_cpu_dvfs_info_lookup(policy
->cpu
);
434 pr_err("dvfs info for cpu%d is not initialized.\n",
439 ret
= dev_pm_opp_init_cpufreq_table(info
->cpu_dev
, &freq_table
);
441 pr_err("failed to init cpufreq table for cpu%d: %d\n",
446 cpumask_copy(policy
->cpus
, &info
->cpus
);
447 policy
->freq_table
= freq_table
;
448 policy
->driver_data
= info
;
449 policy
->clk
= info
->cpu_clk
;
451 dev_pm_opp_of_register_em(info
->cpu_dev
, policy
->cpus
);
456 static int mtk_cpufreq_exit(struct cpufreq_policy
*policy
)
458 struct mtk_cpu_dvfs_info
*info
= policy
->driver_data
;
460 dev_pm_opp_free_cpufreq_table(info
->cpu_dev
, &policy
->freq_table
);
465 static struct cpufreq_driver mtk_cpufreq_driver
= {
466 .flags
= CPUFREQ_STICKY
| CPUFREQ_NEED_INITIAL_FREQ_CHECK
|
467 CPUFREQ_HAVE_GOVERNOR_PER_POLICY
|
468 CPUFREQ_IS_COOLING_DEV
,
469 .verify
= cpufreq_generic_frequency_table_verify
,
470 .target_index
= mtk_cpufreq_set_target
,
471 .get
= cpufreq_generic_get
,
472 .init
= mtk_cpufreq_init
,
473 .exit
= mtk_cpufreq_exit
,
474 .name
= "mtk-cpufreq",
475 .attr
= cpufreq_generic_attr
,
478 static int mtk_cpufreq_probe(struct platform_device
*pdev
)
480 struct mtk_cpu_dvfs_info
*info
, *tmp
;
483 for_each_possible_cpu(cpu
) {
484 info
= mtk_cpu_dvfs_info_lookup(cpu
);
488 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
491 goto release_dvfs_info_list
;
494 ret
= mtk_cpu_dvfs_info_init(info
, cpu
);
497 "failed to initialize dvfs info for cpu%d\n",
499 goto release_dvfs_info_list
;
502 list_add(&info
->list_head
, &dvfs_info_list
);
505 ret
= cpufreq_register_driver(&mtk_cpufreq_driver
);
507 dev_err(&pdev
->dev
, "failed to register mtk cpufreq driver\n");
508 goto release_dvfs_info_list
;
513 release_dvfs_info_list
:
514 list_for_each_entry_safe(info
, tmp
, &dvfs_info_list
, list_head
) {
515 mtk_cpu_dvfs_info_release(info
);
516 list_del(&info
->list_head
);
522 static struct platform_driver mtk_cpufreq_platdrv
= {
524 .name
= "mtk-cpufreq",
526 .probe
= mtk_cpufreq_probe
,
529 /* List of machines supported by this driver */
530 static const struct of_device_id mtk_cpufreq_machines
[] __initconst
= {
531 { .compatible
= "mediatek,mt2701", },
532 { .compatible
= "mediatek,mt2712", },
533 { .compatible
= "mediatek,mt7622", },
534 { .compatible
= "mediatek,mt7623", },
535 { .compatible
= "mediatek,mt8167", },
536 { .compatible
= "mediatek,mt817x", },
537 { .compatible
= "mediatek,mt8173", },
538 { .compatible
= "mediatek,mt8176", },
539 { .compatible
= "mediatek,mt8183", },
540 { .compatible
= "mediatek,mt8516", },
544 MODULE_DEVICE_TABLE(of
, mtk_cpufreq_machines
);
546 static int __init
mtk_cpufreq_driver_init(void)
548 struct device_node
*np
;
549 const struct of_device_id
*match
;
550 struct platform_device
*pdev
;
553 np
= of_find_node_by_path("/");
557 match
= of_match_node(mtk_cpufreq_machines
, np
);
560 pr_debug("Machine is not compatible with mtk-cpufreq\n");
564 err
= platform_driver_register(&mtk_cpufreq_platdrv
);
569 * Since there's no place to hold device registration code and no
570 * device tree based way to match cpufreq driver yet, both the driver
571 * and the device registration codes are put here to handle defer
574 pdev
= platform_device_register_simple("mtk-cpufreq", -1, NULL
, 0);
576 pr_err("failed to register mtk-cpufreq platform device\n");
577 platform_driver_unregister(&mtk_cpufreq_platdrv
);
578 return PTR_ERR(pdev
);
583 device_initcall(mtk_cpufreq_driver_init
);
585 MODULE_DESCRIPTION("MediaTek CPUFreq driver");
586 MODULE_AUTHOR("Pi-Cheng Chen <pi-cheng.chen@linaro.org>");
587 MODULE_LICENSE("GPL v2");