blob: f4880a4f865bc24fc5492196af915b143d03a822 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
2 * Copyright (C) 2013 Freescale Semiconductor, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/clk.h>
10#include <linux/cpu.h>
11#include <linux/cpufreq.h>
12#include <linux/cpu_cooling.h>
13#include <linux/err.h>
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/of_address.h>
17#include <linux/pm_opp.h>
18#include <linux/platform_device.h>
19#include <linux/regulator/consumer.h>
20
21#define PU_SOC_VOLTAGE_NORMAL 1250000
22#define PU_SOC_VOLTAGE_HIGH 1275000
23#define FREQ_1P2_GHZ 1200000000
24
25static struct regulator *arm_reg;
26static struct regulator *pu_reg;
27static struct regulator *soc_reg;
28
29enum IMX6_CPUFREQ_CLKS {
30 ARM,
31 PLL1_SYS,
32 STEP,
33 PLL1_SW,
34 PLL2_PFD2_396M,
35 /* MX6UL requires two more clks */
36 PLL2_BUS,
37 SECONDARY_SEL,
38};
39#define IMX6Q_CPUFREQ_CLK_NUM 5
40#define IMX6UL_CPUFREQ_CLK_NUM 7
41
42static int num_clks;
43static struct clk_bulk_data clks[] = {
44 { .id = "arm" },
45 { .id = "pll1_sys" },
46 { .id = "step" },
47 { .id = "pll1_sw" },
48 { .id = "pll2_pfd2_396m" },
49 { .id = "pll2_bus" },
50 { .id = "secondary_sel" },
51};
52
53static struct device *cpu_dev;
54static struct thermal_cooling_device *cdev;
55static bool free_opp;
56static struct cpufreq_frequency_table *freq_table;
57static unsigned int max_freq;
58static unsigned int transition_latency;
59
60static u32 *imx6_soc_volt;
61static u32 soc_opp_count;
62
63static int imx6q_set_target(struct cpufreq_policy *policy, unsigned int index)
64{
65 struct dev_pm_opp *opp;
66 unsigned long freq_hz, volt, volt_old;
67 unsigned int old_freq, new_freq;
68 bool pll1_sys_temp_enabled = false;
69 int ret;
70
71 new_freq = freq_table[index].frequency;
72 freq_hz = new_freq * 1000;
73 old_freq = clk_get_rate(clks[ARM].clk) / 1000;
74
75 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz);
76 if (IS_ERR(opp)) {
77 dev_err(cpu_dev, "failed to find OPP for %ld\n", freq_hz);
78 return PTR_ERR(opp);
79 }
80
81 volt = dev_pm_opp_get_voltage(opp);
82 dev_pm_opp_put(opp);
83
84 volt_old = regulator_get_voltage(arm_reg);
85
86 dev_dbg(cpu_dev, "%u MHz, %ld mV --> %u MHz, %ld mV\n",
87 old_freq / 1000, volt_old / 1000,
88 new_freq / 1000, volt / 1000);
89
90 /* scaling up? scale voltage before frequency */
91 if (new_freq > old_freq) {
92 if (!IS_ERR(pu_reg)) {
93 ret = regulator_set_voltage_tol(pu_reg, imx6_soc_volt[index], 0);
94 if (ret) {
95 dev_err(cpu_dev, "failed to scale vddpu up: %d\n", ret);
96 return ret;
97 }
98 }
99 ret = regulator_set_voltage_tol(soc_reg, imx6_soc_volt[index], 0);
100 if (ret) {
101 dev_err(cpu_dev, "failed to scale vddsoc up: %d\n", ret);
102 return ret;
103 }
104 ret = regulator_set_voltage_tol(arm_reg, volt, 0);
105 if (ret) {
106 dev_err(cpu_dev,
107 "failed to scale vddarm up: %d\n", ret);
108 return ret;
109 }
110 }
111
112 /*
113 * The setpoints are selected per PLL/PDF frequencies, so we need to
114 * reprogram PLL for frequency scaling. The procedure of reprogramming
115 * PLL1 is as below.
116 * For i.MX6UL, it has a secondary clk mux, the cpu frequency change
117 * flow is slightly different from other i.MX6 OSC.
118 * The cpu frequeny change flow for i.MX6(except i.MX6UL) is as below:
119 * - Enable pll2_pfd2_396m_clk and reparent pll1_sw_clk to it
120 * - Reprogram pll1_sys_clk and reparent pll1_sw_clk back to it
121 * - Disable pll2_pfd2_396m_clk
122 */
123 if (of_machine_is_compatible("fsl,imx6ul") ||
124 of_machine_is_compatible("fsl,imx6ull")) {
125 /*
126 * When changing pll1_sw_clk's parent to pll1_sys_clk,
127 * CPU may run at higher than 528MHz, this will lead to
128 * the system unstable if the voltage is lower than the
129 * voltage of 528MHz, so lower the CPU frequency to one
130 * half before changing CPU frequency.
131 */
132 clk_set_rate(clks[ARM].clk, (old_freq >> 1) * 1000);
133 clk_set_parent(clks[PLL1_SW].clk, clks[PLL1_SYS].clk);
134 if (freq_hz > clk_get_rate(clks[PLL2_PFD2_396M].clk))
135 clk_set_parent(clks[SECONDARY_SEL].clk,
136 clks[PLL2_BUS].clk);
137 else
138 clk_set_parent(clks[SECONDARY_SEL].clk,
139 clks[PLL2_PFD2_396M].clk);
140 clk_set_parent(clks[STEP].clk, clks[SECONDARY_SEL].clk);
141 clk_set_parent(clks[PLL1_SW].clk, clks[STEP].clk);
142 if (freq_hz > clk_get_rate(clks[PLL2_BUS].clk)) {
143 clk_set_rate(clks[PLL1_SYS].clk, new_freq * 1000);
144 clk_set_parent(clks[PLL1_SW].clk, clks[PLL1_SYS].clk);
145 }
146 } else {
147 clk_set_parent(clks[STEP].clk, clks[PLL2_PFD2_396M].clk);
148 clk_set_parent(clks[PLL1_SW].clk, clks[STEP].clk);
149 if (freq_hz > clk_get_rate(clks[PLL2_PFD2_396M].clk)) {
150 clk_set_rate(clks[PLL1_SYS].clk, new_freq * 1000);
151 clk_set_parent(clks[PLL1_SW].clk, clks[PLL1_SYS].clk);
152 } else {
153 /* pll1_sys needs to be enabled for divider rate change to work. */
154 pll1_sys_temp_enabled = true;
155 clk_prepare_enable(clks[PLL1_SYS].clk);
156 }
157 }
158
159 /* Ensure the arm clock divider is what we expect */
160 ret = clk_set_rate(clks[ARM].clk, new_freq * 1000);
161 if (ret) {
162 int ret1;
163
164 dev_err(cpu_dev, "failed to set clock rate: %d\n", ret);
165 ret1 = regulator_set_voltage_tol(arm_reg, volt_old, 0);
166 if (ret1)
167 dev_warn(cpu_dev,
168 "failed to restore vddarm voltage: %d\n", ret1);
169 return ret;
170 }
171
172 /* PLL1 is only needed until after ARM-PODF is set. */
173 if (pll1_sys_temp_enabled)
174 clk_disable_unprepare(clks[PLL1_SYS].clk);
175
176 /* scaling down? scale voltage after frequency */
177 if (new_freq < old_freq) {
178 ret = regulator_set_voltage_tol(arm_reg, volt, 0);
179 if (ret) {
180 dev_warn(cpu_dev,
181 "failed to scale vddarm down: %d\n", ret);
182 ret = 0;
183 }
184 ret = regulator_set_voltage_tol(soc_reg, imx6_soc_volt[index], 0);
185 if (ret) {
186 dev_warn(cpu_dev, "failed to scale vddsoc down: %d\n", ret);
187 ret = 0;
188 }
189 if (!IS_ERR(pu_reg)) {
190 ret = regulator_set_voltage_tol(pu_reg, imx6_soc_volt[index], 0);
191 if (ret) {
192 dev_warn(cpu_dev, "failed to scale vddpu down: %d\n", ret);
193 ret = 0;
194 }
195 }
196 }
197
198 return 0;
199}
200
201static void imx6q_cpufreq_ready(struct cpufreq_policy *policy)
202{
203 cdev = of_cpufreq_cooling_register(policy);
204
205 if (!cdev)
206 dev_err(cpu_dev,
207 "running cpufreq without cooling device: %ld\n",
208 PTR_ERR(cdev));
209}
210
211static int imx6q_cpufreq_init(struct cpufreq_policy *policy)
212{
213 int ret;
214
215 policy->clk = clks[ARM].clk;
216 ret = cpufreq_generic_init(policy, freq_table, transition_latency);
217 policy->suspend_freq = max_freq;
218
219 return ret;
220}
221
222static int imx6q_cpufreq_exit(struct cpufreq_policy *policy)
223{
224 cpufreq_cooling_unregister(cdev);
225
226 return 0;
227}
228
229static struct cpufreq_driver imx6q_cpufreq_driver = {
230 .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
231 .verify = cpufreq_generic_frequency_table_verify,
232 .target_index = imx6q_set_target,
233 .get = cpufreq_generic_get,
234 .init = imx6q_cpufreq_init,
235 .exit = imx6q_cpufreq_exit,
236 .name = "imx6q-cpufreq",
237 .ready = imx6q_cpufreq_ready,
238 .attr = cpufreq_generic_attr,
239 .suspend = cpufreq_generic_suspend,
240};
241
242#define OCOTP_CFG3 0x440
243#define OCOTP_CFG3_SPEED_SHIFT 16
244#define OCOTP_CFG3_SPEED_1P2GHZ 0x3
245#define OCOTP_CFG3_SPEED_996MHZ 0x2
246#define OCOTP_CFG3_SPEED_852MHZ 0x1
247
248static void imx6q_opp_check_speed_grading(struct device *dev)
249{
250 struct device_node *np;
251 void __iomem *base;
252 u32 val;
253
254 np = of_find_compatible_node(NULL, NULL, "fsl,imx6q-ocotp");
255 if (!np)
256 return;
257
258 base = of_iomap(np, 0);
259 if (!base) {
260 dev_err(dev, "failed to map ocotp\n");
261 goto put_node;
262 }
263
264 /*
265 * SPEED_GRADING[1:0] defines the max speed of ARM:
266 * 2b'11: 1200000000Hz;
267 * 2b'10: 996000000Hz;
268 * 2b'01: 852000000Hz; -- i.MX6Q Only, exclusive with 996MHz.
269 * 2b'00: 792000000Hz;
270 * We need to set the max speed of ARM according to fuse map.
271 */
272 val = readl_relaxed(base + OCOTP_CFG3);
273 val >>= OCOTP_CFG3_SPEED_SHIFT;
274 val &= 0x3;
275
276 if (val < OCOTP_CFG3_SPEED_996MHZ)
277 if (dev_pm_opp_disable(dev, 996000000))
278 dev_warn(dev, "failed to disable 996MHz OPP\n");
279
280 if (of_machine_is_compatible("fsl,imx6q") ||
281 of_machine_is_compatible("fsl,imx6qp")) {
282 if (val != OCOTP_CFG3_SPEED_852MHZ)
283 if (dev_pm_opp_disable(dev, 852000000))
284 dev_warn(dev, "failed to disable 852MHz OPP\n");
285 if (val != OCOTP_CFG3_SPEED_1P2GHZ)
286 if (dev_pm_opp_disable(dev, 1200000000))
287 dev_warn(dev, "failed to disable 1.2GHz OPP\n");
288 }
289 iounmap(base);
290put_node:
291 of_node_put(np);
292}
293
294#define OCOTP_CFG3_6UL_SPEED_696MHZ 0x2
295#define OCOTP_CFG3_6ULL_SPEED_792MHZ 0x2
296#define OCOTP_CFG3_6ULL_SPEED_900MHZ 0x3
297
298static void imx6ul_opp_check_speed_grading(struct device *dev)
299{
300 struct device_node *np;
301 void __iomem *base;
302 u32 val;
303
304 np = of_find_compatible_node(NULL, NULL, "fsl,imx6ul-ocotp");
305 if (!np)
306 return;
307
308 base = of_iomap(np, 0);
309 if (!base) {
310 dev_err(dev, "failed to map ocotp\n");
311 goto put_node;
312 }
313
314 /*
315 * Speed GRADING[1:0] defines the max speed of ARM:
316 * 2b'00: Reserved;
317 * 2b'01: 528000000Hz;
318 * 2b'10: 696000000Hz on i.MX6UL, 792000000Hz on i.MX6ULL;
319 * 2b'11: 900000000Hz on i.MX6ULL only;
320 * We need to set the max speed of ARM according to fuse map.
321 */
322 val = readl_relaxed(base + OCOTP_CFG3);
323 val >>= OCOTP_CFG3_SPEED_SHIFT;
324 val &= 0x3;
325
326 if (of_machine_is_compatible("fsl,imx6ul")) {
327 if (val != OCOTP_CFG3_6UL_SPEED_696MHZ)
328 if (dev_pm_opp_disable(dev, 696000000))
329 dev_warn(dev, "failed to disable 696MHz OPP\n");
330 }
331
332 if (of_machine_is_compatible("fsl,imx6ull")) {
333 if (val != OCOTP_CFG3_6ULL_SPEED_792MHZ)
334 if (dev_pm_opp_disable(dev, 792000000))
335 dev_warn(dev, "failed to disable 792MHz OPP\n");
336
337 if (val != OCOTP_CFG3_6ULL_SPEED_900MHZ)
338 if (dev_pm_opp_disable(dev, 900000000))
339 dev_warn(dev, "failed to disable 900MHz OPP\n");
340 }
341
342 iounmap(base);
343put_node:
344 of_node_put(np);
345}
346
347static int imx6q_cpufreq_probe(struct platform_device *pdev)
348{
349 struct device_node *np;
350 struct dev_pm_opp *opp;
351 unsigned long min_volt, max_volt;
352 int num, ret;
353 const struct property *prop;
354 const __be32 *val;
355 u32 nr, i, j;
356
357 cpu_dev = get_cpu_device(0);
358 if (!cpu_dev) {
359 pr_err("failed to get cpu0 device\n");
360 return -ENODEV;
361 }
362
363 np = of_node_get(cpu_dev->of_node);
364 if (!np) {
365 dev_err(cpu_dev, "failed to find cpu0 node\n");
366 return -ENOENT;
367 }
368
369 if (of_machine_is_compatible("fsl,imx6ul") ||
370 of_machine_is_compatible("fsl,imx6ull"))
371 num_clks = IMX6UL_CPUFREQ_CLK_NUM;
372 else
373 num_clks = IMX6Q_CPUFREQ_CLK_NUM;
374
375 ret = clk_bulk_get(cpu_dev, num_clks, clks);
376 if (ret)
377 goto put_node;
378
379 arm_reg = regulator_get(cpu_dev, "arm");
380 pu_reg = regulator_get_optional(cpu_dev, "pu");
381 soc_reg = regulator_get(cpu_dev, "soc");
382 if (PTR_ERR(arm_reg) == -EPROBE_DEFER ||
383 PTR_ERR(soc_reg) == -EPROBE_DEFER ||
384 PTR_ERR(pu_reg) == -EPROBE_DEFER) {
385 ret = -EPROBE_DEFER;
386 dev_dbg(cpu_dev, "regulators not ready, defer\n");
387 goto put_reg;
388 }
389 if (IS_ERR(arm_reg) || IS_ERR(soc_reg)) {
390 dev_err(cpu_dev, "failed to get regulators\n");
391 ret = -ENOENT;
392 goto put_reg;
393 }
394
395 ret = dev_pm_opp_of_add_table(cpu_dev);
396 if (ret < 0) {
397 dev_err(cpu_dev, "failed to init OPP table: %d\n", ret);
398 goto put_reg;
399 }
400
401 if (of_machine_is_compatible("fsl,imx6ul") ||
402 of_machine_is_compatible("fsl,imx6ull"))
403 imx6ul_opp_check_speed_grading(cpu_dev);
404 else
405 imx6q_opp_check_speed_grading(cpu_dev);
406
407 /* Because we have added the OPPs here, we must free them */
408 free_opp = true;
409 num = dev_pm_opp_get_opp_count(cpu_dev);
410 if (num < 0) {
411 ret = num;
412 dev_err(cpu_dev, "no OPP table is found: %d\n", ret);
413 goto out_free_opp;
414 }
415
416 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
417 if (ret) {
418 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
419 goto out_free_opp;
420 }
421
422 /* Make imx6_soc_volt array's size same as arm opp number */
423 imx6_soc_volt = devm_kcalloc(cpu_dev, num, sizeof(*imx6_soc_volt),
424 GFP_KERNEL);
425 if (imx6_soc_volt == NULL) {
426 ret = -ENOMEM;
427 goto free_freq_table;
428 }
429
430 prop = of_find_property(np, "fsl,soc-operating-points", NULL);
431 if (!prop || !prop->value)
432 goto soc_opp_out;
433
434 /*
435 * Each OPP is a set of tuples consisting of frequency and
436 * voltage like <freq-kHz vol-uV>.
437 */
438 nr = prop->length / sizeof(u32);
439 if (nr % 2 || (nr / 2) < num)
440 goto soc_opp_out;
441
442 for (j = 0; j < num; j++) {
443 val = prop->value;
444 for (i = 0; i < nr / 2; i++) {
445 unsigned long freq = be32_to_cpup(val++);
446 unsigned long volt = be32_to_cpup(val++);
447 if (freq_table[j].frequency == freq) {
448 imx6_soc_volt[soc_opp_count++] = volt;
449 break;
450 }
451 }
452 }
453
454soc_opp_out:
455 /* use fixed soc opp volt if no valid soc opp info found in dtb */
456 if (soc_opp_count != num) {
457 dev_warn(cpu_dev, "can NOT find valid fsl,soc-operating-points property in dtb, use default value!\n");
458 for (j = 0; j < num; j++)
459 imx6_soc_volt[j] = PU_SOC_VOLTAGE_NORMAL;
460 if (freq_table[num - 1].frequency * 1000 == FREQ_1P2_GHZ)
461 imx6_soc_volt[num - 1] = PU_SOC_VOLTAGE_HIGH;
462 }
463
464 if (of_property_read_u32(np, "clock-latency", &transition_latency))
465 transition_latency = CPUFREQ_ETERNAL;
466
467 /*
468 * Calculate the ramp time for max voltage change in the
469 * VDDSOC and VDDPU regulators.
470 */
471 ret = regulator_set_voltage_time(soc_reg, imx6_soc_volt[0], imx6_soc_volt[num - 1]);
472 if (ret > 0)
473 transition_latency += ret * 1000;
474 if (!IS_ERR(pu_reg)) {
475 ret = regulator_set_voltage_time(pu_reg, imx6_soc_volt[0], imx6_soc_volt[num - 1]);
476 if (ret > 0)
477 transition_latency += ret * 1000;
478 }
479
480 /*
481 * OPP is maintained in order of increasing frequency, and
482 * freq_table initialised from OPP is therefore sorted in the
483 * same order.
484 */
485 max_freq = freq_table[--num].frequency;
486 opp = dev_pm_opp_find_freq_exact(cpu_dev,
487 freq_table[0].frequency * 1000, true);
488 min_volt = dev_pm_opp_get_voltage(opp);
489 dev_pm_opp_put(opp);
490 opp = dev_pm_opp_find_freq_exact(cpu_dev, max_freq * 1000, true);
491 max_volt = dev_pm_opp_get_voltage(opp);
492 dev_pm_opp_put(opp);
493
494 ret = regulator_set_voltage_time(arm_reg, min_volt, max_volt);
495 if (ret > 0)
496 transition_latency += ret * 1000;
497
498 ret = cpufreq_register_driver(&imx6q_cpufreq_driver);
499 if (ret) {
500 dev_err(cpu_dev, "failed register driver: %d\n", ret);
501 goto free_freq_table;
502 }
503
504 of_node_put(np);
505 return 0;
506
507free_freq_table:
508 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
509out_free_opp:
510 if (free_opp)
511 dev_pm_opp_of_remove_table(cpu_dev);
512put_reg:
513 if (!IS_ERR(arm_reg))
514 regulator_put(arm_reg);
515 if (!IS_ERR(pu_reg))
516 regulator_put(pu_reg);
517 if (!IS_ERR(soc_reg))
518 regulator_put(soc_reg);
519
520 clk_bulk_put(num_clks, clks);
521put_node:
522 of_node_put(np);
523
524 return ret;
525}
526
527static int imx6q_cpufreq_remove(struct platform_device *pdev)
528{
529 cpufreq_unregister_driver(&imx6q_cpufreq_driver);
530 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
531 if (free_opp)
532 dev_pm_opp_of_remove_table(cpu_dev);
533 regulator_put(arm_reg);
534 if (!IS_ERR(pu_reg))
535 regulator_put(pu_reg);
536 regulator_put(soc_reg);
537
538 clk_bulk_put(num_clks, clks);
539
540 return 0;
541}
542
543static struct platform_driver imx6q_cpufreq_platdrv = {
544 .driver = {
545 .name = "imx6q-cpufreq",
546 },
547 .probe = imx6q_cpufreq_probe,
548 .remove = imx6q_cpufreq_remove,
549};
550module_platform_driver(imx6q_cpufreq_platdrv);
551
552MODULE_ALIAS("platform:imx6q-cpufreq");
553MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
554MODULE_DESCRIPTION("Freescale i.MX6Q cpufreq driver");
555MODULE_LICENSE("GPL");