2 * Copyright (C) 2013 Freescale Semiconductor, Inc.
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.
10 #include <linux/cpu.h>
11 #include <linux/cpufreq.h>
12 #include <linux/err.h>
13 #include <linux/module.h>
15 #include <linux/of_address.h>
16 #include <linux/pm_opp.h>
17 #include <linux/platform_device.h>
18 #include <linux/regulator/consumer.h>
20 #define PU_SOC_VOLTAGE_NORMAL 1250000
21 #define PU_SOC_VOLTAGE_HIGH 1275000
22 #define FREQ_1P2_GHZ 1200000000
24 static struct regulator *arm_reg;
25 static struct regulator *pu_reg;
26 static struct regulator *soc_reg;
28 enum IMX6_CPUFREQ_CLKS {
34 /* MX6UL requires two more clks */
38 #define IMX6Q_CPUFREQ_CLK_NUM 5
39 #define IMX6UL_CPUFREQ_CLK_NUM 7
42 static struct clk_bulk_data clks[] = {
47 { .id = "pll2_pfd2_396m" },
49 { .id = "secondary_sel" },
52 static struct device *cpu_dev;
54 static struct cpufreq_frequency_table *freq_table;
55 static unsigned int transition_latency;
57 static u32 *imx6_soc_volt;
58 static u32 soc_opp_count;
60 static int imx6q_set_target(struct cpufreq_policy *policy, unsigned int index)
62 struct dev_pm_opp *opp;
63 unsigned long freq_hz, volt, volt_old;
64 unsigned int old_freq, new_freq;
65 bool pll1_sys_temp_enabled = false;
68 new_freq = freq_table[index].frequency;
69 freq_hz = new_freq * 1000;
70 old_freq = clk_get_rate(clks[ARM].clk) / 1000;
72 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz);
74 dev_err(cpu_dev, "failed to find OPP for %ld\n", freq_hz);
78 volt = dev_pm_opp_get_voltage(opp);
81 volt_old = regulator_get_voltage(arm_reg);
83 dev_dbg(cpu_dev, "%u MHz, %ld mV --> %u MHz, %ld mV\n",
84 old_freq / 1000, volt_old / 1000,
85 new_freq / 1000, volt / 1000);
87 /* scaling up? scale voltage before frequency */
88 if (new_freq > old_freq) {
89 if (!IS_ERR(pu_reg)) {
90 ret = regulator_set_voltage_tol(pu_reg, imx6_soc_volt[index], 0);
92 dev_err(cpu_dev, "failed to scale vddpu up: %d\n", ret);
96 ret = regulator_set_voltage_tol(soc_reg, imx6_soc_volt[index], 0);
98 dev_err(cpu_dev, "failed to scale vddsoc up: %d\n", ret);
101 ret = regulator_set_voltage_tol(arm_reg, volt, 0);
104 "failed to scale vddarm up: %d\n", ret);
110 * The setpoints are selected per PLL/PDF frequencies, so we need to
111 * reprogram PLL for frequency scaling. The procedure of reprogramming
113 * For i.MX6UL, it has a secondary clk mux, the cpu frequency change
114 * flow is slightly different from other i.MX6 OSC.
115 * The cpu frequeny change flow for i.MX6(except i.MX6UL) is as below:
116 * - Enable pll2_pfd2_396m_clk and reparent pll1_sw_clk to it
117 * - Reprogram pll1_sys_clk and reparent pll1_sw_clk back to it
118 * - Disable pll2_pfd2_396m_clk
120 if (of_machine_is_compatible("fsl,imx6ul") ||
121 of_machine_is_compatible("fsl,imx6ull")) {
123 * When changing pll1_sw_clk's parent to pll1_sys_clk,
124 * CPU may run at higher than 528MHz, this will lead to
125 * the system unstable if the voltage is lower than the
126 * voltage of 528MHz, so lower the CPU frequency to one
127 * half before changing CPU frequency.
129 clk_set_rate(clks[ARM].clk, (old_freq >> 1) * 1000);
130 clk_set_parent(clks[PLL1_SW].clk, clks[PLL1_SYS].clk);
131 if (freq_hz > clk_get_rate(clks[PLL2_PFD2_396M].clk))
132 clk_set_parent(clks[SECONDARY_SEL].clk,
135 clk_set_parent(clks[SECONDARY_SEL].clk,
136 clks[PLL2_PFD2_396M].clk);
137 clk_set_parent(clks[STEP].clk, clks[SECONDARY_SEL].clk);
138 clk_set_parent(clks[PLL1_SW].clk, clks[STEP].clk);
139 if (freq_hz > clk_get_rate(clks[PLL2_BUS].clk)) {
140 clk_set_rate(clks[PLL1_SYS].clk, new_freq * 1000);
141 clk_set_parent(clks[PLL1_SW].clk, clks[PLL1_SYS].clk);
144 clk_set_parent(clks[STEP].clk, clks[PLL2_PFD2_396M].clk);
145 clk_set_parent(clks[PLL1_SW].clk, clks[STEP].clk);
146 if (freq_hz > clk_get_rate(clks[PLL2_PFD2_396M].clk)) {
147 clk_set_rate(clks[PLL1_SYS].clk, new_freq * 1000);
148 clk_set_parent(clks[PLL1_SW].clk, clks[PLL1_SYS].clk);
150 /* pll1_sys needs to be enabled for divider rate change to work. */
151 pll1_sys_temp_enabled = true;
152 clk_prepare_enable(clks[PLL1_SYS].clk);
156 /* Ensure the arm clock divider is what we expect */
157 ret = clk_set_rate(clks[ARM].clk, new_freq * 1000);
159 dev_err(cpu_dev, "failed to set clock rate: %d\n", ret);
160 regulator_set_voltage_tol(arm_reg, volt_old, 0);
164 /* PLL1 is only needed until after ARM-PODF is set. */
165 if (pll1_sys_temp_enabled)
166 clk_disable_unprepare(clks[PLL1_SYS].clk);
168 /* scaling down? scale voltage after frequency */
169 if (new_freq < old_freq) {
170 ret = regulator_set_voltage_tol(arm_reg, volt, 0);
173 "failed to scale vddarm down: %d\n", ret);
176 ret = regulator_set_voltage_tol(soc_reg, imx6_soc_volt[index], 0);
178 dev_warn(cpu_dev, "failed to scale vddsoc down: %d\n", ret);
181 if (!IS_ERR(pu_reg)) {
182 ret = regulator_set_voltage_tol(pu_reg, imx6_soc_volt[index], 0);
184 dev_warn(cpu_dev, "failed to scale vddpu down: %d\n", ret);
193 static int imx6q_cpufreq_init(struct cpufreq_policy *policy)
197 policy->clk = clks[ARM].clk;
198 ret = cpufreq_generic_init(policy, freq_table, transition_latency);
199 policy->suspend_freq = policy->max;
204 static struct cpufreq_driver imx6q_cpufreq_driver = {
205 .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
206 .verify = cpufreq_generic_frequency_table_verify,
207 .target_index = imx6q_set_target,
208 .get = cpufreq_generic_get,
209 .init = imx6q_cpufreq_init,
210 .name = "imx6q-cpufreq",
211 .attr = cpufreq_generic_attr,
212 .suspend = cpufreq_generic_suspend,
215 #define OCOTP_CFG3 0x440
216 #define OCOTP_CFG3_SPEED_SHIFT 16
217 #define OCOTP_CFG3_SPEED_1P2GHZ 0x3
218 #define OCOTP_CFG3_SPEED_996MHZ 0x2
219 #define OCOTP_CFG3_SPEED_852MHZ 0x1
221 static void imx6q_opp_check_speed_grading(struct device *dev)
223 struct device_node *np;
227 np = of_find_compatible_node(NULL, NULL, "fsl,imx6q-ocotp");
231 base = of_iomap(np, 0);
233 dev_err(dev, "failed to map ocotp\n");
238 * SPEED_GRADING[1:0] defines the max speed of ARM:
239 * 2b'11: 1200000000Hz;
240 * 2b'10: 996000000Hz;
241 * 2b'01: 852000000Hz; -- i.MX6Q Only, exclusive with 996MHz.
242 * 2b'00: 792000000Hz;
243 * We need to set the max speed of ARM according to fuse map.
245 val = readl_relaxed(base + OCOTP_CFG3);
246 val >>= OCOTP_CFG3_SPEED_SHIFT;
249 if (val < OCOTP_CFG3_SPEED_996MHZ)
250 if (dev_pm_opp_disable(dev, 996000000))
251 dev_warn(dev, "failed to disable 996MHz OPP\n");
253 if (of_machine_is_compatible("fsl,imx6q") ||
254 of_machine_is_compatible("fsl,imx6qp")) {
255 if (val != OCOTP_CFG3_SPEED_852MHZ)
256 if (dev_pm_opp_disable(dev, 852000000))
257 dev_warn(dev, "failed to disable 852MHz OPP\n");
258 if (val != OCOTP_CFG3_SPEED_1P2GHZ)
259 if (dev_pm_opp_disable(dev, 1200000000))
260 dev_warn(dev, "failed to disable 1.2GHz OPP\n");
267 #define OCOTP_CFG3_6UL_SPEED_696MHZ 0x2
269 static void imx6ul_opp_check_speed_grading(struct device *dev)
271 struct device_node *np;
275 np = of_find_compatible_node(NULL, NULL, "fsl,imx6ul-ocotp");
279 base = of_iomap(np, 0);
281 dev_err(dev, "failed to map ocotp\n");
286 * Speed GRADING[1:0] defines the max speed of ARM:
288 * 2b'01: 528000000Hz;
289 * 2b'10: 696000000Hz;
291 * We need to set the max speed of ARM according to fuse map.
293 val = readl_relaxed(base + OCOTP_CFG3);
294 val >>= OCOTP_CFG3_SPEED_SHIFT;
296 if (val != OCOTP_CFG3_6UL_SPEED_696MHZ)
297 if (dev_pm_opp_disable(dev, 696000000))
298 dev_warn(dev, "failed to disable 696MHz OPP\n");
304 static int imx6q_cpufreq_probe(struct platform_device *pdev)
306 struct device_node *np;
307 struct dev_pm_opp *opp;
308 unsigned long min_volt, max_volt;
310 const struct property *prop;
314 cpu_dev = get_cpu_device(0);
316 pr_err("failed to get cpu0 device\n");
320 np = of_node_get(cpu_dev->of_node);
322 dev_err(cpu_dev, "failed to find cpu0 node\n");
326 if (of_machine_is_compatible("fsl,imx6ul") ||
327 of_machine_is_compatible("fsl,imx6ull"))
328 num_clks = IMX6UL_CPUFREQ_CLK_NUM;
330 num_clks = IMX6Q_CPUFREQ_CLK_NUM;
332 ret = clk_bulk_get(cpu_dev, num_clks, clks);
336 arm_reg = regulator_get(cpu_dev, "arm");
337 pu_reg = regulator_get_optional(cpu_dev, "pu");
338 soc_reg = regulator_get(cpu_dev, "soc");
339 if (PTR_ERR(arm_reg) == -EPROBE_DEFER ||
340 PTR_ERR(soc_reg) == -EPROBE_DEFER ||
341 PTR_ERR(pu_reg) == -EPROBE_DEFER) {
343 dev_dbg(cpu_dev, "regulators not ready, defer\n");
346 if (IS_ERR(arm_reg) || IS_ERR(soc_reg)) {
347 dev_err(cpu_dev, "failed to get regulators\n");
352 ret = dev_pm_opp_of_add_table(cpu_dev);
354 dev_err(cpu_dev, "failed to init OPP table: %d\n", ret);
358 if (of_machine_is_compatible("fsl,imx6ul"))
359 imx6ul_opp_check_speed_grading(cpu_dev);
361 imx6q_opp_check_speed_grading(cpu_dev);
363 /* Because we have added the OPPs here, we must free them */
365 num = dev_pm_opp_get_opp_count(cpu_dev);
368 dev_err(cpu_dev, "no OPP table is found: %d\n", ret);
372 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
374 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
378 /* Make imx6_soc_volt array's size same as arm opp number */
379 imx6_soc_volt = devm_kzalloc(cpu_dev, sizeof(*imx6_soc_volt) * num, GFP_KERNEL);
380 if (imx6_soc_volt == NULL) {
382 goto free_freq_table;
385 prop = of_find_property(np, "fsl,soc-operating-points", NULL);
386 if (!prop || !prop->value)
390 * Each OPP is a set of tuples consisting of frequency and
391 * voltage like <freq-kHz vol-uV>.
393 nr = prop->length / sizeof(u32);
394 if (nr % 2 || (nr / 2) < num)
397 for (j = 0; j < num; j++) {
399 for (i = 0; i < nr / 2; i++) {
400 unsigned long freq = be32_to_cpup(val++);
401 unsigned long volt = be32_to_cpup(val++);
402 if (freq_table[j].frequency == freq) {
403 imx6_soc_volt[soc_opp_count++] = volt;
410 /* use fixed soc opp volt if no valid soc opp info found in dtb */
411 if (soc_opp_count != num) {
412 dev_warn(cpu_dev, "can NOT find valid fsl,soc-operating-points property in dtb, use default value!\n");
413 for (j = 0; j < num; j++)
414 imx6_soc_volt[j] = PU_SOC_VOLTAGE_NORMAL;
415 if (freq_table[num - 1].frequency * 1000 == FREQ_1P2_GHZ)
416 imx6_soc_volt[num - 1] = PU_SOC_VOLTAGE_HIGH;
419 if (of_property_read_u32(np, "clock-latency", &transition_latency))
420 transition_latency = CPUFREQ_ETERNAL;
423 * Calculate the ramp time for max voltage change in the
424 * VDDSOC and VDDPU regulators.
426 ret = regulator_set_voltage_time(soc_reg, imx6_soc_volt[0], imx6_soc_volt[num - 1]);
428 transition_latency += ret * 1000;
429 if (!IS_ERR(pu_reg)) {
430 ret = regulator_set_voltage_time(pu_reg, imx6_soc_volt[0], imx6_soc_volt[num - 1]);
432 transition_latency += ret * 1000;
436 * OPP is maintained in order of increasing frequency, and
437 * freq_table initialised from OPP is therefore sorted in the
440 opp = dev_pm_opp_find_freq_exact(cpu_dev,
441 freq_table[0].frequency * 1000, true);
442 min_volt = dev_pm_opp_get_voltage(opp);
444 opp = dev_pm_opp_find_freq_exact(cpu_dev,
445 freq_table[--num].frequency * 1000, true);
446 max_volt = dev_pm_opp_get_voltage(opp);
449 ret = regulator_set_voltage_time(arm_reg, min_volt, max_volt);
451 transition_latency += ret * 1000;
453 ret = cpufreq_register_driver(&imx6q_cpufreq_driver);
455 dev_err(cpu_dev, "failed register driver: %d\n", ret);
456 goto free_freq_table;
463 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
466 dev_pm_opp_of_remove_table(cpu_dev);
468 if (!IS_ERR(arm_reg))
469 regulator_put(arm_reg);
471 regulator_put(pu_reg);
472 if (!IS_ERR(soc_reg))
473 regulator_put(soc_reg);
475 clk_bulk_put(num_clks, clks);
482 static int imx6q_cpufreq_remove(struct platform_device *pdev)
484 cpufreq_unregister_driver(&imx6q_cpufreq_driver);
485 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
487 dev_pm_opp_of_remove_table(cpu_dev);
488 regulator_put(arm_reg);
490 regulator_put(pu_reg);
491 regulator_put(soc_reg);
493 clk_bulk_put(num_clks, clks);
498 static struct platform_driver imx6q_cpufreq_platdrv = {
500 .name = "imx6q-cpufreq",
502 .probe = imx6q_cpufreq_probe,
503 .remove = imx6q_cpufreq_remove,
505 module_platform_driver(imx6q_cpufreq_platdrv);
507 MODULE_ALIAS("platform:imx6q-cpufreq");
509 MODULE_DESCRIPTION("Freescale i.MX6Q cpufreq driver");
510 MODULE_LICENSE("GPL");