blob: 4229fcc313105292db9fc28e70daca5546ee43e4 [file] [log] [blame]
Pi-Cheng Chen14538632015-08-19 10:05:06 +08001/*
2 * Copyright (c) 2015 Linaro Ltd.
3 * Author: Pi-Cheng Chen <pi-cheng.chen@linaro.org>
4 *
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.
8 *
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.
13 */
14
15#include <linux/clk.h>
16#include <linux/cpu.h>
Pi-Cheng Chen14538632015-08-19 10:05:06 +080017#include <linux/cpufreq.h>
18#include <linux/cpumask.h>
Arnd Bergmann3c2002a2016-02-29 17:04:21 +010019#include <linux/module.h>
Pi-Cheng Chen14538632015-08-19 10:05:06 +080020#include <linux/of.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>
26
27#define MIN_VOLT_SHIFT (100000)
28#define MAX_VOLT_SHIFT (200000)
29#define MAX_VOLT_LIMIT (1150000)
30#define VOLT_TOL (10000)
31
32/*
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
38 *
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.
42 */
43struct mtk_cpu_dvfs_info {
Pi-Cheng Chen89b56042015-12-10 11:48:13 +080044 struct cpumask cpus;
Pi-Cheng Chen14538632015-08-19 10:05:06 +080045 struct device *cpu_dev;
46 struct regulator *proc_reg;
47 struct regulator *sram_reg;
48 struct clk *cpu_clk;
49 struct clk *inter_clk;
Pi-Cheng Chen89b56042015-12-10 11:48:13 +080050 struct list_head list_head;
Pi-Cheng Chen14538632015-08-19 10:05:06 +080051 int intermediate_voltage;
52 bool need_voltage_tracking;
53};
54
Pi-Cheng Chen89b56042015-12-10 11:48:13 +080055static LIST_HEAD(dvfs_info_list);
56
57static struct mtk_cpu_dvfs_info *mtk_cpu_dvfs_info_lookup(int cpu)
58{
59 struct mtk_cpu_dvfs_info *info;
Pi-Cheng Chen89b56042015-12-10 11:48:13 +080060
Geliang Tangd2499d02016-04-05 10:38:06 +080061 list_for_each_entry(info, &dvfs_info_list, list_head) {
Pi-Cheng Chen89b56042015-12-10 11:48:13 +080062 if (cpumask_test_cpu(cpu, &info->cpus))
63 return info;
64 }
65
66 return NULL;
67}
68
Pi-Cheng Chen14538632015-08-19 10:05:06 +080069static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info,
70 int new_vproc)
71{
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;
75
76 old_vproc = regulator_get_voltage(proc_reg);
Pi-Cheng Chen40be4c32015-11-29 16:31:37 +080077 if (old_vproc < 0) {
78 pr_err("%s: invalid Vproc value: %d\n", __func__, old_vproc);
79 return old_vproc;
80 }
Pi-Cheng Chen14538632015-08-19 10:05:06 +080081 /* Vsram should not exceed the maximum allowed voltage of SoC. */
82 new_vsram = min(new_vproc + MIN_VOLT_SHIFT, MAX_VOLT_LIMIT);
83
84 if (old_vproc < new_vproc) {
85 /*
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.
90 */
91 do {
92 old_vsram = regulator_get_voltage(sram_reg);
Pi-Cheng Chen40be4c32015-11-29 16:31:37 +080093 if (old_vsram < 0) {
94 pr_err("%s: invalid Vsram value: %d\n",
95 __func__, old_vsram);
96 return old_vsram;
97 }
Pi-Cheng Chen14538632015-08-19 10:05:06 +080098 old_vproc = regulator_get_voltage(proc_reg);
Pi-Cheng Chen40be4c32015-11-29 16:31:37 +080099 if (old_vproc < 0) {
100 pr_err("%s: invalid Vproc value: %d\n",
101 __func__, old_vproc);
102 return old_vproc;
103 }
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800104
105 vsram = min(new_vsram, old_vproc + MAX_VOLT_SHIFT);
106
107 if (vsram + VOLT_TOL >= MAX_VOLT_LIMIT) {
108 vsram = MAX_VOLT_LIMIT;
109
110 /*
111 * If the target Vsram hits the maximum voltage,
112 * try to set the exact voltage value first.
113 */
114 ret = regulator_set_voltage(sram_reg, vsram,
115 vsram);
116 if (ret)
117 ret = regulator_set_voltage(sram_reg,
118 vsram - VOLT_TOL,
119 vsram);
120
121 vproc = new_vproc;
122 } else {
123 ret = regulator_set_voltage(sram_reg, vsram,
124 vsram + VOLT_TOL);
125
126 vproc = vsram - MIN_VOLT_SHIFT;
127 }
128 if (ret)
129 return ret;
130
131 ret = regulator_set_voltage(proc_reg, vproc,
132 vproc + VOLT_TOL);
133 if (ret) {
134 regulator_set_voltage(sram_reg, old_vsram,
135 old_vsram);
136 return ret;
137 }
138 } while (vproc < new_vproc || vsram < new_vsram);
139 } else if (old_vproc > new_vproc) {
140 /*
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.
145 */
146 do {
147 old_vproc = regulator_get_voltage(proc_reg);
Pi-Cheng Chen40be4c32015-11-29 16:31:37 +0800148 if (old_vproc < 0) {
149 pr_err("%s: invalid Vproc value: %d\n",
150 __func__, old_vproc);
151 return old_vproc;
152 }
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800153 old_vsram = regulator_get_voltage(sram_reg);
Pi-Cheng Chen40be4c32015-11-29 16:31:37 +0800154 if (old_vsram < 0) {
155 pr_err("%s: invalid Vsram value: %d\n",
156 __func__, old_vsram);
157 return old_vsram;
158 }
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800159
160 vproc = max(new_vproc, old_vsram - MAX_VOLT_SHIFT);
161 ret = regulator_set_voltage(proc_reg, vproc,
162 vproc + VOLT_TOL);
163 if (ret)
164 return ret;
165
166 if (vproc == new_vproc)
167 vsram = new_vsram;
168 else
169 vsram = max(new_vsram, vproc + MIN_VOLT_SHIFT);
170
171 if (vsram + VOLT_TOL >= MAX_VOLT_LIMIT) {
172 vsram = MAX_VOLT_LIMIT;
173
174 /*
175 * If the target Vsram hits the maximum voltage,
176 * try to set the exact voltage value first.
177 */
178 ret = regulator_set_voltage(sram_reg, vsram,
179 vsram);
180 if (ret)
181 ret = regulator_set_voltage(sram_reg,
182 vsram - VOLT_TOL,
183 vsram);
184 } else {
185 ret = regulator_set_voltage(sram_reg, vsram,
186 vsram + VOLT_TOL);
187 }
188
189 if (ret) {
190 regulator_set_voltage(proc_reg, old_vproc,
191 old_vproc);
192 return ret;
193 }
194 } while (vproc > new_vproc + VOLT_TOL ||
195 vsram > new_vsram + VOLT_TOL);
196 }
197
198 return 0;
199}
200
201static int mtk_cpufreq_set_voltage(struct mtk_cpu_dvfs_info *info, int vproc)
202{
203 if (info->need_voltage_tracking)
204 return mtk_cpufreq_voltage_tracking(info, vproc);
205 else
206 return regulator_set_voltage(info->proc_reg, vproc,
207 vproc + VOLT_TOL);
208}
209
210static int mtk_cpufreq_set_target(struct cpufreq_policy *policy,
211 unsigned int index)
212{
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;
221
222 inter_vproc = info->intermediate_voltage;
223
224 old_freq_hz = clk_get_rate(cpu_clk);
225 old_vproc = regulator_get_voltage(info->proc_reg);
Pi-Cheng Chen40be4c32015-11-29 16:31:37 +0800226 if (old_vproc < 0) {
227 pr_err("%s: invalid Vproc value: %d\n", __func__, old_vproc);
228 return old_vproc;
229 }
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800230
231 freq_hz = freq_table[index].frequency * 1000;
232
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800233 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz);
234 if (IS_ERR(opp)) {
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800235 pr_err("cpu%d: failed to find OPP for %ld\n",
236 policy->cpu, freq_hz);
237 return PTR_ERR(opp);
238 }
239 vproc = dev_pm_opp_get_voltage(opp);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530240 dev_pm_opp_put(opp);
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800241
242 /*
243 * If the new voltage or the intermediate voltage is higher than the
244 * current voltage, scale up voltage first.
245 */
246 target_vproc = (inter_vproc > vproc) ? inter_vproc : vproc;
247 if (old_vproc < target_vproc) {
248 ret = mtk_cpufreq_set_voltage(info, target_vproc);
249 if (ret) {
250 pr_err("cpu%d: failed to scale up voltage!\n",
251 policy->cpu);
252 mtk_cpufreq_set_voltage(info, old_vproc);
253 return ret;
254 }
255 }
256
257 /* Reparent the CPU clock to intermediate clock. */
258 ret = clk_set_parent(cpu_clk, info->inter_clk);
259 if (ret) {
260 pr_err("cpu%d: failed to re-parent cpu clock!\n",
261 policy->cpu);
262 mtk_cpufreq_set_voltage(info, old_vproc);
263 WARN_ON(1);
264 return ret;
265 }
266
267 /* Set the original PLL to target rate. */
268 ret = clk_set_rate(armpll, freq_hz);
269 if (ret) {
270 pr_err("cpu%d: failed to scale cpu clock rate!\n",
271 policy->cpu);
272 clk_set_parent(cpu_clk, armpll);
273 mtk_cpufreq_set_voltage(info, old_vproc);
274 return ret;
275 }
276
277 /* Set parent of CPU clock back to the original PLL. */
278 ret = clk_set_parent(cpu_clk, armpll);
279 if (ret) {
280 pr_err("cpu%d: failed to re-parent cpu clock!\n",
281 policy->cpu);
282 mtk_cpufreq_set_voltage(info, inter_vproc);
283 WARN_ON(1);
284 return ret;
285 }
286
287 /*
288 * If the new voltage is lower than the intermediate voltage or the
289 * original voltage, scale down to the new voltage.
290 */
291 if (vproc < inter_vproc || vproc < old_vproc) {
292 ret = mtk_cpufreq_set_voltage(info, vproc);
293 if (ret) {
294 pr_err("cpu%d: failed to scale down voltage!\n",
295 policy->cpu);
296 clk_set_parent(cpu_clk, info->inter_clk);
297 clk_set_rate(armpll, old_freq_hz);
298 clk_set_parent(cpu_clk, armpll);
299 return ret;
300 }
301 }
302
303 return 0;
304}
305
Dawei Chiend2901602015-12-16 21:29:14 +0800306#define DYNAMIC_POWER "dynamic-power-coefficient"
307
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800308static int mtk_cpu_dvfs_info_init(struct mtk_cpu_dvfs_info *info, int cpu)
309{
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;
316 unsigned long rate;
317 int ret;
318
319 cpu_dev = get_cpu_device(cpu);
320 if (!cpu_dev) {
321 pr_err("failed to get cpu%d device\n", cpu);
322 return -ENODEV;
323 }
324
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);
329 else
330 pr_err("failed to get cpu clk for cpu%d\n", cpu);
331
332 ret = PTR_ERR(cpu_clk);
333 return ret;
334 }
335
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",
340 cpu);
341 else
342 pr_err("failed to get intermediate clk for cpu%d\n",
343 cpu);
344
345 ret = PTR_ERR(inter_clk);
346 goto out_free_resources;
347 }
348
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",
353 cpu);
354 else
355 pr_err("failed to get proc regulator for cpu%d\n",
356 cpu);
357
358 ret = PTR_ERR(proc_reg);
359 goto out_free_resources;
360 }
361
362 /* Both presence and absence of sram regulator are valid cases. */
363 sram_reg = regulator_get_exclusive(cpu_dev, "sram");
364
Pi-Cheng Chena8893312015-12-27 14:21:57 +0800365 /* Get OPP-sharing information from "operating-points-v2" bindings */
366 ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, &info->cpus);
367 if (ret) {
368 pr_err("failed to get OPP-sharing information for cpu%d\n",
369 cpu);
370 goto out_free_resources;
371 }
372
373 ret = dev_pm_opp_of_cpumask_add_table(&info->cpus);
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800374 if (ret) {
375 pr_warn("no OPP table for cpu%d\n", cpu);
376 goto out_free_resources;
377 }
378
379 /* Search a safe voltage for intermediate frequency. */
380 rate = clk_get_rate(inter_clk);
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800381 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &rate);
382 if (IS_ERR(opp)) {
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800383 pr_err("failed to get intermediate opp for cpu%d\n", cpu);
384 ret = PTR_ERR(opp);
385 goto out_free_opp_table;
386 }
387 info->intermediate_voltage = dev_pm_opp_get_voltage(opp);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530388 dev_pm_opp_put(opp);
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800389
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;
395
396 /*
397 * If SRAM regulator is present, software "voltage tracking" is needed
398 * for this CPU power domain.
399 */
400 info->need_voltage_tracking = !IS_ERR(sram_reg);
401
402 return 0;
403
404out_free_opp_table:
Pi-Cheng Chena8893312015-12-27 14:21:57 +0800405 dev_pm_opp_of_cpumask_remove_table(&info->cpus);
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800406
407out_free_resources:
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))
413 clk_put(cpu_clk);
414 if (!IS_ERR(inter_clk))
415 clk_put(inter_clk);
416
417 return ret;
418}
419
420static void mtk_cpu_dvfs_info_release(struct mtk_cpu_dvfs_info *info)
421{
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);
430
Pi-Cheng Chena8893312015-12-27 14:21:57 +0800431 dev_pm_opp_of_cpumask_remove_table(&info->cpus);
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800432}
433
434static int mtk_cpufreq_init(struct cpufreq_policy *policy)
435{
436 struct mtk_cpu_dvfs_info *info;
437 struct cpufreq_frequency_table *freq_table;
438 int ret;
439
Pi-Cheng Chen89b56042015-12-10 11:48:13 +0800440 info = mtk_cpu_dvfs_info_lookup(policy->cpu);
441 if (!info) {
442 pr_err("dvfs info for cpu%d is not initialized.\n",
443 policy->cpu);
444 return -EINVAL;
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800445 }
446
447 ret = dev_pm_opp_init_cpufreq_table(info->cpu_dev, &freq_table);
448 if (ret) {
449 pr_err("failed to init cpufreq table for cpu%d: %d\n",
450 policy->cpu, ret);
Pi-Cheng Chen89b56042015-12-10 11:48:13 +0800451 return ret;
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800452 }
453
Pi-Cheng Chen89b56042015-12-10 11:48:13 +0800454 cpumask_copy(policy->cpus, &info->cpus);
Viresh Kumarb563afb2018-02-26 10:38:55 +0530455 policy->freq_table = freq_table;
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800456 policy->driver_data = info;
457 policy->clk = info->cpu_clk;
458
459 return 0;
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800460}
461
462static int mtk_cpufreq_exit(struct cpufreq_policy *policy)
463{
464 struct mtk_cpu_dvfs_info *info = policy->driver_data;
465
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800466 dev_pm_opp_free_cpufreq_table(info->cpu_dev, &policy->freq_table);
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800467
468 return 0;
469}
470
Sean Wang862e0102017-08-09 18:12:38 +0800471static struct cpufreq_driver mtk_cpufreq_driver = {
Pi-Cheng Chen9bb46b82015-11-29 16:31:35 +0800472 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK |
Amit Kucheria0db60d62019-01-29 10:25:12 +0530473 CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
474 CPUFREQ_IS_COOLING_DEV,
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800475 .verify = cpufreq_generic_frequency_table_verify,
476 .target_index = mtk_cpufreq_set_target,
477 .get = cpufreq_generic_get,
478 .init = mtk_cpufreq_init,
479 .exit = mtk_cpufreq_exit,
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800480 .name = "mtk-cpufreq",
481 .attr = cpufreq_generic_attr,
482};
483
Sean Wang862e0102017-08-09 18:12:38 +0800484static int mtk_cpufreq_probe(struct platform_device *pdev)
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800485{
Geliang Tangd2499d02016-04-05 10:38:06 +0800486 struct mtk_cpu_dvfs_info *info, *tmp;
Pi-Cheng Chen89b56042015-12-10 11:48:13 +0800487 int cpu, ret;
488
489 for_each_possible_cpu(cpu) {
490 info = mtk_cpu_dvfs_info_lookup(cpu);
491 if (info)
492 continue;
493
494 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
495 if (!info) {
496 ret = -ENOMEM;
497 goto release_dvfs_info_list;
498 }
499
500 ret = mtk_cpu_dvfs_info_init(info, cpu);
501 if (ret) {
502 dev_err(&pdev->dev,
503 "failed to initialize dvfs info for cpu%d\n",
504 cpu);
505 goto release_dvfs_info_list;
506 }
507
508 list_add(&info->list_head, &dvfs_info_list);
509 }
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800510
Sean Wang862e0102017-08-09 18:12:38 +0800511 ret = cpufreq_register_driver(&mtk_cpufreq_driver);
Pi-Cheng Chen89b56042015-12-10 11:48:13 +0800512 if (ret) {
513 dev_err(&pdev->dev, "failed to register mtk cpufreq driver\n");
514 goto release_dvfs_info_list;
515 }
516
517 return 0;
518
519release_dvfs_info_list:
Geliang Tangd2499d02016-04-05 10:38:06 +0800520 list_for_each_entry_safe(info, tmp, &dvfs_info_list, list_head) {
Pi-Cheng Chen89b56042015-12-10 11:48:13 +0800521 mtk_cpu_dvfs_info_release(info);
Geliang Tangd2499d02016-04-05 10:38:06 +0800522 list_del(&info->list_head);
Pi-Cheng Chen89b56042015-12-10 11:48:13 +0800523 }
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800524
525 return ret;
526}
527
Sean Wang862e0102017-08-09 18:12:38 +0800528static struct platform_driver mtk_cpufreq_platdrv = {
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800529 .driver = {
Sean Wang862e0102017-08-09 18:12:38 +0800530 .name = "mtk-cpufreq",
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800531 },
Sean Wang862e0102017-08-09 18:12:38 +0800532 .probe = mtk_cpufreq_probe,
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800533};
534
Daniel Kurtzcf9a2432017-03-02 19:03:45 +0800535/* List of machines supported by this driver */
Sean Wang862e0102017-08-09 18:12:38 +0800536static const struct of_device_id mtk_cpufreq_machines[] __initconst = {
Sean Wang501c5742017-07-18 14:01:43 +0800537 { .compatible = "mediatek,mt2701", },
Andrew-sh Chenga9596db2017-12-08 14:07:55 +0800538 { .compatible = "mediatek,mt2712", },
Sean Wangccc03d82017-08-09 18:12:39 +0800539 { .compatible = "mediatek,mt7622", },
Sean Wang501c5742017-07-18 14:01:43 +0800540 { .compatible = "mediatek,mt7623", },
Daniel Kurtzcf9a2432017-03-02 19:03:45 +0800541 { .compatible = "mediatek,mt817x", },
542 { .compatible = "mediatek,mt8173", },
543 { .compatible = "mediatek,mt8176", },
544
545 { }
546};
547
Sean Wang862e0102017-08-09 18:12:38 +0800548static int __init mtk_cpufreq_driver_init(void)
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800549{
Daniel Kurtzcf9a2432017-03-02 19:03:45 +0800550 struct device_node *np;
551 const struct of_device_id *match;
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800552 struct platform_device *pdev;
553 int err;
554
Daniel Kurtzcf9a2432017-03-02 19:03:45 +0800555 np = of_find_node_by_path("/");
556 if (!np)
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800557 return -ENODEV;
558
Sean Wang862e0102017-08-09 18:12:38 +0800559 match = of_match_node(mtk_cpufreq_machines, np);
Daniel Kurtzcf9a2432017-03-02 19:03:45 +0800560 of_node_put(np);
561 if (!match) {
Viresh Kumarcb8bd2f2018-02-22 11:26:31 +0530562 pr_debug("Machine is not compatible with mtk-cpufreq\n");
Daniel Kurtzcf9a2432017-03-02 19:03:45 +0800563 return -ENODEV;
564 }
565
Sean Wang862e0102017-08-09 18:12:38 +0800566 err = platform_driver_register(&mtk_cpufreq_platdrv);
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800567 if (err)
568 return err;
569
570 /*
571 * Since there's no place to hold device registration code and no
572 * device tree based way to match cpufreq driver yet, both the driver
573 * and the device registration codes are put here to handle defer
574 * probing.
575 */
Sean Wang862e0102017-08-09 18:12:38 +0800576 pdev = platform_device_register_simple("mtk-cpufreq", -1, NULL, 0);
Pi-Cheng Chen14538632015-08-19 10:05:06 +0800577 if (IS_ERR(pdev)) {
578 pr_err("failed to register mtk-cpufreq platform device\n");
579 return PTR_ERR(pdev);
580 }
581
582 return 0;
583}
Sean Wang862e0102017-08-09 18:12:38 +0800584device_initcall(mtk_cpufreq_driver_init);
Jesse Chan7e8a09e2017-11-20 13:32:01 -0800585
586MODULE_DESCRIPTION("MediaTek CPUFreq driver");
587MODULE_AUTHOR("Pi-Cheng Chen <pi-cheng.chen@linaro.org>");
588MODULE_LICENSE("GPL v2");