1 From 2028cb37c941014f6a817d27a867ee1d37ccf2b6 Mon Sep 17 00:00:00 2001
2 From: "pi-cheng.chen" <pi-cheng.chen@linaro.org>
3 Date: Mon, 8 Jun 2015 20:29:21 +0800
4 Subject: [PATCH 33/76] cpufreq: mediatek: Add MT8173 cpufreq driver
6 This patch implements MT8173 cpufreq driver.
8 Signed-off-by: Pi-Cheng Chen <pi-cheng.chen@linaro.org>
10 drivers/cpufreq/Kconfig.arm | 7 +
11 drivers/cpufreq/Makefile | 1 +
12 drivers/cpufreq/mt8173-cpufreq.c | 550 ++++++++++++++++++++++++++++++++++++++
13 3 files changed, 558 insertions(+)
14 create mode 100644 drivers/cpufreq/mt8173-cpufreq.c
16 diff --git a/drivers/cpufreq/Kconfig.arm b/drivers/cpufreq/Kconfig.arm
17 index 4f3dbc8..350752b 100644
18 --- a/drivers/cpufreq/Kconfig.arm
19 +++ b/drivers/cpufreq/Kconfig.arm
20 @@ -141,6 +141,13 @@ config ARM_KIRKWOOD_CPUFREQ
21 This adds the CPUFreq driver for Marvell Kirkwood
24 +config ARM_MT8173_CPUFREQ
25 + bool "Mediatek MT8173 CPUFreq support"
26 + depends on ARCH_MEDIATEK && REGULATOR
29 + This adds the CPUFreq driver support for Mediatek MT8173 SoC.
31 config ARM_OMAP2PLUS_CPUFREQ
33 depends on ARCH_OMAP2PLUS
34 diff --git a/drivers/cpufreq/Makefile b/drivers/cpufreq/Makefile
35 index cdce92a..97f9a9b 100644
36 --- a/drivers/cpufreq/Makefile
37 +++ b/drivers/cpufreq/Makefile
38 @@ -63,6 +63,7 @@ obj-$(CONFIG_ARM_HISI_ACPU_CPUFREQ) += hisi-acpu-cpufreq.o
39 obj-$(CONFIG_ARM_IMX6Q_CPUFREQ) += imx6q-cpufreq.o
40 obj-$(CONFIG_ARM_INTEGRATOR) += integrator-cpufreq.o
41 obj-$(CONFIG_ARM_KIRKWOOD_CPUFREQ) += kirkwood-cpufreq.o
42 +obj-$(CONFIG_ARM_MT8173_CPUFREQ) += mt8173-cpufreq.o
43 obj-$(CONFIG_ARM_OMAP2PLUS_CPUFREQ) += omap-cpufreq.o
44 obj-$(CONFIG_ARM_PXA2xx_CPUFREQ) += pxa2xx-cpufreq.o
45 obj-$(CONFIG_PXA3xx) += pxa3xx-cpufreq.o
46 diff --git a/drivers/cpufreq/mt8173-cpufreq.c b/drivers/cpufreq/mt8173-cpufreq.c
48 index 0000000..d539e7b
50 +++ b/drivers/cpufreq/mt8173-cpufreq.c
53 + * Copyright (c) 2015 Linaro Ltd.
54 + * Author: Pi-Cheng Chen <pi-cheng.chen@linaro.org>
56 + * This program is free software; you can redistribute it and/or modify
57 + * it under the terms of the GNU General Public License version 2 as
58 + * published by the Free Software Foundation.
60 + * This program is distributed in the hope that it will be useful,
61 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
62 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63 + * GNU General Public License for more details.
66 +#include <linux/clk.h>
67 +#include <linux/cpu.h>
68 +#include <linux/cpufreq.h>
69 +#include <linux/cpumask.h>
70 +#include <linux/module.h>
71 +#include <linux/of.h>
72 +#include <linux/platform_device.h>
73 +#include <linux/pm_opp.h>
74 +#include <linux/regulator/consumer.h>
75 +#include <linux/slab.h>
77 +#define MIN_VOLT_SHIFT (100000)
78 +#define MAX_VOLT_SHIFT (200000)
79 +#define MAX_VOLT_LIMIT (1150000)
80 +#define VOLT_TOL (10000)
83 + * The struct mtk_cpu_dvfs_info holds necessary information for doing CPU DVFS
84 + * on each CPU power/clock domain of Mediatek SoCs. Each CPU cluster in
85 + * Mediatek SoCs has two voltage inputs, Vproc and Vsram. In some cases the two
86 + * voltage inputs need to be controlled under a hardware limitation:
87 + * 100mV < Vsram - Vproc < 200mV
89 + * When scaling the clock frequency of a CPU clock domain, the clock source
90 + * needs to be switched to another stable PLL clock temporarily until
91 + * the original PLL becomes stable at target frequency.
93 +struct mtk_cpu_dvfs_info {
94 + struct list_head node;
96 + struct cpufreq_frequency_table *freq_table;
97 + struct device *cpu_dev;
98 + struct regulator *proc_reg;
99 + struct regulator *sram_reg;
100 + struct clk *cpu_clk;
101 + struct clk *inter_clk;
102 + int intermediate_voltage;
103 + bool need_voltage_trace;
106 +static LIST_HEAD(cpu_dvfs_info_list);
108 +static inline struct mtk_cpu_dvfs_info *to_mtk_cpu_dvfs_info(
109 + struct list_head *list)
111 + return list_entry(list, struct mtk_cpu_dvfs_info, node);
114 +static inline void mtk_cpu_dvfs_info_add(struct mtk_cpu_dvfs_info *info)
116 + list_add(&info->node, &cpu_dvfs_info_list);
119 +static struct mtk_cpu_dvfs_info *mtk_cpu_dvfs_info_get(int cpu)
121 + struct mtk_cpu_dvfs_info *info;
122 + struct list_head *list;
124 + list_for_each(list, &cpu_dvfs_info_list) {
125 + info = to_mtk_cpu_dvfs_info(list);
127 + if (cpumask_test_cpu(cpu, info->cpus))
134 +static void mtk_cpu_dvfs_info_release(void)
136 + struct list_head *list, *tmp;
137 + struct mtk_cpu_dvfs_info *info;
139 + list_for_each_safe(list, tmp, &cpu_dvfs_info_list) {
140 + info = to_mtk_cpu_dvfs_info(list);
142 + dev_pm_opp_free_cpufreq_table(info->cpu_dev,
143 + &info->freq_table);
145 + if (!IS_ERR(info->proc_reg))
146 + regulator_put(info->proc_reg);
147 + if (!IS_ERR(info->sram_reg))
148 + regulator_put(info->sram_reg);
149 + if (!IS_ERR(info->cpu_clk))
150 + clk_put(info->cpu_clk);
151 + if (!IS_ERR(info->inter_clk))
152 + clk_put(info->inter_clk);
154 + of_free_opp_table(info->cpu_dev);
161 +#define MIN(a, b) ((a) < (b) ? (a) : (b))
162 +#define MAX(a, b) ((a) > (b) ? (a) : (b))
164 +static int mtk_cpufreq_voltage_trace(struct mtk_cpu_dvfs_info *info,
167 + struct regulator *proc_reg = info->proc_reg;
168 + struct regulator *sram_reg = info->sram_reg;
169 + int old_vproc, old_vsram, new_vsram, vsram, vproc, ret;
171 + old_vproc = regulator_get_voltage(proc_reg);
172 + old_vsram = regulator_get_voltage(sram_reg);
173 + /* Vsram should not exceed the maximum allowed voltage of SoC. */
174 + new_vsram = min(new_vproc + MIN_VOLT_SHIFT, MAX_VOLT_LIMIT);
176 + if (old_vproc < new_vproc) {
178 + * When scaling up voltages, Vsram and Vproc scale up step
179 + * by step. At each step, set Vsram to (Vproc + 200mV) first,
180 + * then set Vproc to (Vsram - 100mV).
181 + * Keep doing it until Vsram and Vproc hit target voltages.
184 + old_vsram = regulator_get_voltage(sram_reg);
185 + old_vproc = regulator_get_voltage(proc_reg);
187 + vsram = MIN(new_vsram, old_vproc + MAX_VOLT_SHIFT);
189 + if (vsram + VOLT_TOL >= MAX_VOLT_LIMIT) {
190 + vsram = MAX_VOLT_LIMIT;
193 + * If the target Vsram hits the maximum voltage,
194 + * try to set the exact voltage value first.
196 + ret = regulator_set_voltage(sram_reg, vsram,
199 + ret = regulator_set_voltage(sram_reg,
205 + ret = regulator_set_voltage(sram_reg, vsram,
208 + vproc = vsram - MIN_VOLT_SHIFT;
213 + ret = regulator_set_voltage(proc_reg, vproc,
216 + regulator_set_voltage(sram_reg, old_vsram,
220 + } while (vproc < new_vproc || vsram < new_vsram);
221 + } else if (old_vproc > new_vproc) {
223 + * When scaling down voltages, Vsram and Vproc scale down step
224 + * by step. At each step, set Vproc to (Vsram - 200mV) first,
225 + * then set Vproc to (Vproc + 100mV).
226 + * Keep doing it until Vsram and Vproc hit target voltages.
229 + old_vproc = regulator_get_voltage(proc_reg);
230 + old_vsram = regulator_get_voltage(sram_reg);
232 + vproc = MAX(new_vproc, old_vsram - MAX_VOLT_SHIFT);
233 + ret = regulator_set_voltage(proc_reg, vproc,
238 + if (vproc == new_vproc)
241 + vsram = MAX(new_vsram, vproc + MIN_VOLT_SHIFT);
243 + if (vsram + VOLT_TOL >= MAX_VOLT_LIMIT) {
244 + vsram = MAX_VOLT_LIMIT;
247 + * If the target Vsram hits the maximum voltage,
248 + * try to set the exact voltage value first.
250 + ret = regulator_set_voltage(sram_reg, vsram,
253 + ret = regulator_set_voltage(sram_reg,
257 + ret = regulator_set_voltage(sram_reg, vsram,
262 + regulator_set_voltage(proc_reg, old_vproc,
266 + } while (vproc > new_vproc + VOLT_TOL ||
267 + vsram > new_vsram + VOLT_TOL);
273 +static int mtk_cpufreq_set_voltage(struct mtk_cpu_dvfs_info *info, int vproc)
275 + if (info->need_voltage_trace)
276 + return mtk_cpufreq_voltage_trace(info, vproc);
278 + return regulator_set_voltage(info->proc_reg, vproc,
282 +static int mtk_cpufreq_set_target(struct cpufreq_policy *policy,
283 + unsigned int index)
285 + struct cpufreq_frequency_table *freq_table = policy->freq_table;
286 + struct clk *cpu_clk = policy->clk;
287 + struct clk *armpll = clk_get_parent(cpu_clk);
288 + struct mtk_cpu_dvfs_info *info = policy->driver_data;
289 + struct device *cpu_dev = info->cpu_dev;
290 + struct dev_pm_opp *opp;
291 + long freq_hz, old_freq_hz;
292 + int vproc, old_vproc, inter_vproc, target_vproc, ret;
294 + inter_vproc = info->intermediate_voltage;
296 + old_freq_hz = clk_get_rate(cpu_clk);
297 + old_vproc = regulator_get_voltage(info->proc_reg);
299 + freq_hz = freq_table[index].frequency * 1000;
301 + opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz);
304 + pr_err("cpu%d: failed to find OPP for %ld\n",
305 + policy->cpu, freq_hz);
306 + return PTR_ERR(opp);
308 + vproc = dev_pm_opp_get_voltage(opp);
312 + * If the new voltage or the intermediate voltage is higher than the
313 + * current voltage, scale up voltage first.
315 + target_vproc = (inter_vproc > vproc) ? inter_vproc : vproc;
316 + if (old_vproc < target_vproc) {
317 + ret = mtk_cpufreq_set_voltage(info, target_vproc);
319 + pr_err("cpu%d: failed to scale up voltage!\n",
321 + mtk_cpufreq_set_voltage(info, old_vproc);
326 + /* Reparent the CPU clock to intermediate clock. */
327 + ret = clk_set_parent(cpu_clk, info->inter_clk);
329 + pr_err("cpu%d: failed to re-parent cpu clock!\n",
331 + mtk_cpufreq_set_voltage(info, old_vproc);
336 + /* Set the original PLL to target rate. */
337 + ret = clk_set_rate(armpll, freq_hz);
339 + pr_err("cpu%d: failed to scale cpu clock rate!\n",
341 + clk_set_parent(cpu_clk, armpll);
342 + mtk_cpufreq_set_voltage(info, old_vproc);
346 + /* Set parent of CPU clock back to the original PLL. */
347 + ret = clk_set_parent(cpu_clk, armpll);
349 + pr_err("cpu%d: failed to re-parent cpu clock!\n",
351 + mtk_cpufreq_set_voltage(info, inter_vproc);
357 + * If the new voltage is lower than the intermediate voltage or the
358 + * original voltage, scale down to the new voltage.
360 + if (vproc < inter_vproc || vproc < old_vproc) {
361 + ret = mtk_cpufreq_set_voltage(info, vproc);
363 + pr_err("cpu%d: failed to scale down voltage!\n",
365 + clk_set_parent(cpu_clk, info->inter_clk);
366 + clk_set_rate(armpll, old_freq_hz);
367 + clk_set_parent(cpu_clk, armpll);
375 +static int mtk_cpufreq_init(struct cpufreq_policy *policy)
377 + struct mtk_cpu_dvfs_info *info;
380 + info = mtk_cpu_dvfs_info_get(policy->cpu);
382 + pr_err("%s: mtk cpu dvfs info for cpu%d is not initialized\n",
383 + __func__, policy->cpu);
387 + ret = cpufreq_table_validate_and_show(policy, info->freq_table);
389 + pr_err("%s: invalid frequency table: %d\n", __func__, ret);
393 + cpumask_copy(policy->cpus, info->cpus);
394 + policy->driver_data = info;
395 + policy->clk = info->cpu_clk;
400 +static struct cpufreq_driver mt8173_cpufreq_driver = {
401 + .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
402 + .verify = cpufreq_generic_frequency_table_verify,
403 + .target_index = mtk_cpufreq_set_target,
404 + .get = cpufreq_generic_get,
405 + .init = mtk_cpufreq_init,
406 + .name = "mtk-cpufreq",
407 + .attr = cpufreq_generic_attr,
410 +static int mtk_cpu_dvfs_info_init(int cpu)
412 + struct device *cpu_dev;
413 + struct regulator *proc_reg = ERR_PTR(-ENODEV);
414 + struct regulator *sram_reg = ERR_PTR(-ENODEV);
415 + struct clk *cpu_clk = ERR_PTR(-ENODEV);
416 + struct clk *inter_clk = ERR_PTR(-ENODEV);
417 + struct mtk_cpu_dvfs_info *info;
418 + struct cpufreq_frequency_table *freq_table;
419 + struct dev_pm_opp *opp;
420 + unsigned long rate;
423 + cpu_dev = get_cpu_device(cpu);
425 + pr_err("failed to get cpu%d device\n", cpu);
429 + ret = of_init_opp_table(cpu_dev);
431 + pr_warn("no OPP table for cpu%d\n", cpu);
435 + cpu_clk = clk_get(cpu_dev, "cpu");
436 + if (IS_ERR(cpu_clk)) {
437 + if (PTR_ERR(cpu_clk) == -EPROBE_DEFER)
438 + pr_warn("cpu clk for cpu%d not ready, retry.\n", cpu);
440 + pr_err("failed to get cpu clk for cpu%d\n", cpu);
442 + ret = PTR_ERR(cpu_clk);
443 + goto out_free_opp_table;
446 + inter_clk = clk_get(cpu_dev, "intermediate");
447 + if (IS_ERR(inter_clk)) {
448 + if (PTR_ERR(inter_clk) == -EPROBE_DEFER)
449 + pr_warn("intermediate clk for cpu%d not ready, retry.\n",
452 + pr_err("failed to get intermediate clk for cpu%d\n",
455 + ret = PTR_ERR(cpu_clk);
456 + goto out_free_resources;
459 + proc_reg = regulator_get_exclusive(cpu_dev, "proc");
460 + if (IS_ERR(proc_reg)) {
461 + if (PTR_ERR(proc_reg) == -EPROBE_DEFER)
462 + pr_warn("proc regulator for cpu%d not ready, retry.\n",
465 + pr_err("failed to get proc regulator for cpu%d\n",
468 + ret = PTR_ERR(proc_reg);
469 + goto out_free_resources;
472 + /* Both presence and absence of sram regulator are valid cases. */
473 + sram_reg = regulator_get_exclusive(cpu_dev, "sram");
475 + info = kzalloc(sizeof(*info), GFP_KERNEL);
478 + goto out_free_resources;
481 + ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
483 + pr_err("failed to init cpufreq table for cpu%d: %d\n",
485 + goto out_free_mtk_cpu_dvfs_info;
488 + if (!alloc_cpumask_var(&info->cpus, GFP_KERNEL))
489 + goto out_free_cpufreq_table;
491 + /* Search a safe voltage for intermediate frequency. */
492 + rate = clk_get_rate(inter_clk);
494 + opp = dev_pm_opp_find_freq_ceil(cpu_dev, &rate);
496 + pr_err("failed to get intermediate opp for cpu%d\n", cpu);
497 + ret = PTR_ERR(opp);
498 + goto out_free_cpumask;
500 + info->intermediate_voltage = dev_pm_opp_get_voltage(opp);
503 + /* CPUs in the same cluster share a clock and power domain. */
504 + cpumask_copy(info->cpus, &cpu_topology[cpu].core_sibling);
506 + info->cpu_dev = cpu_dev;
507 + info->proc_reg = proc_reg;
508 + info->sram_reg = IS_ERR(sram_reg) ? NULL : sram_reg;
509 + info->cpu_clk = cpu_clk;
510 + info->inter_clk = inter_clk;
511 + info->freq_table = freq_table;
514 + * If SRAM regulator is present, software "voltage trace" is needed
515 + * for this CPU power domain.
517 + info->need_voltage_trace = !IS_ERR(sram_reg);
519 + mtk_cpu_dvfs_info_add(info);
524 + free_cpumask_var(info->cpus);
526 +out_free_cpufreq_table:
527 + dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
529 +out_free_mtk_cpu_dvfs_info:
533 + if (!IS_ERR(proc_reg))
534 + regulator_put(proc_reg);
535 + if (!IS_ERR(sram_reg))
536 + regulator_put(sram_reg);
537 + if (!IS_ERR(cpu_clk))
539 + if (!IS_ERR(inter_clk))
540 + clk_put(inter_clk);
543 + of_free_opp_table(cpu_dev);
548 +static int mt8173_cpufreq_probe(struct platform_device *pdev)
552 + for_each_possible_cpu(cpu) {
554 + * If the struct mtk_cpu_dvfs_info for the cpu power domain
555 + * is already initialized, skip this CPU.
557 + if (!mtk_cpu_dvfs_info_get(cpu)) {
558 + ret = mtk_cpu_dvfs_info_init(cpu);
560 + if (ret != -EPROBE_DEFER)
561 + pr_err("%s probe fail\n", __func__);
563 + mtk_cpu_dvfs_info_release();
569 + ret = cpufreq_register_driver(&mt8173_cpufreq_driver);
571 + pr_err("failed to register mtk cpufreq driver\n");
572 + mtk_cpu_dvfs_info_release();
578 +static struct platform_driver mt8173_cpufreq_platdrv = {
580 + .name = "mt8173-cpufreq",
582 + .probe = mt8173_cpufreq_probe,
584 +module_platform_driver(mt8173_cpufreq_platdrv);
586 +static int mt8173_cpufreq_driver_init(void)
588 + struct platform_device *pdev;
590 + if (!of_machine_is_compatible("mediatek,mt8173"))
593 + pdev = platform_device_register_simple("mt8173-cpufreq", -1, NULL, 0);
594 + if (IS_ERR(pdev)) {
595 + pr_err("failed to register mtk-cpufreq platform device\n");
596 + return PTR_ERR(pdev);
601 +module_init(mt8173_cpufreq_driver_init);