2 * Copyright (C) 2011-2012 Freescale Semiconductor, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * @brief A common API for the Freescale Semiconductor i.MXC CPUfreq module
23 * and DVFS CORE module.
25 * The APIs are for setting bus frequency to low or high.
30 #include <linux/sched.h>
31 #include <linux/proc_fs.h>
32 #include <linux/clk.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <linux/regulator/consumer.h>
36 #include <linux/mutex.h>
37 #include <mach/iram.h>
38 #include <mach/hardware.h>
39 #include <mach/clock.h>
40 #include <mach/mxc_dvfs.h>
41 #include <mach/sdram_autogating.h>
42 #include <asm/mach/map.h>
43 #include <asm/mach-types.h>
44 #include <asm/cacheflush.h>
47 #include <linux/suspend.h>
49 #define LPAPM_CLK 24000000
50 #define DDR_MED_CLK 400000000
51 #define DDR3_NORMAL_CLK 528000000
52 #define GPC_PGC_GPU_PGCR_OFFSET 0x260
53 #define GPC_CNTR_OFFSET 0x0
55 DEFINE_SPINLOCK(ddr_freq_lock
);
57 int low_bus_freq_mode
;
58 int audio_bus_freq_mode
;
59 int high_bus_freq_mode
;
60 int med_bus_freq_mode
;
62 int bus_freq_scaling_initialized
;
63 static struct device
*busfreq_dev
;
64 static int busfreq_suspended
;
66 /* True if bus_frequency is scaled not using DVFS-PER */
67 int bus_freq_scaling_is_active
;
72 unsigned int ddr_low_rate
;
73 unsigned int ddr_med_rate
;
74 unsigned int ddr_normal_rate
;
76 int low_freq_bus_used(void);
77 void set_ddr_freq(int ddr_freq
);
79 extern int init_mmdc_settings(void);
80 extern struct cpu_op
*(*get_cpu_op
)(int *op
);
81 extern int update_ddr_freq(int ddr_rate
);
83 extern bool arm_mem_clked_in_wait
;
85 DEFINE_MUTEX(bus_freq_mutex
);
87 struct timeval start_time
;
88 struct timeval end_time
;
91 static struct cpu_op
*cpu_op_tbl
;
92 static struct clk
*pll2_400
;
93 static struct clk
*axi_clk
;
94 static struct clk
*ahb_clk
;
95 static struct clk
*periph_clk
;
96 static struct clk
*osc_clk
;
97 static struct clk
*cpu_clk
;
98 static struct clk
*pll3
;
99 static struct clk
*pll2
;
100 static struct clk
*pll3_sw_clk
;
101 static struct clk
*pll2_200
;
102 static struct clk
*mmdc_ch0_axi
;
103 struct regulator
*vddsoc_cap_regulator
;
104 static struct clk
*pll3_540
;
106 static struct delayed_work low_bus_freq_handler
;
108 static void reduce_bus_freq_handler(struct work_struct
*work
)
112 mutex_lock(&bus_freq_mutex
);
113 if (low_bus_freq_mode
|| !low_freq_bus_used()) {
114 mutex_unlock(&bus_freq_mutex
);
118 if (audio_bus_freq_mode
&& lp_audio_freq
) {
119 mutex_unlock(&bus_freq_mutex
);
123 if (!cpu_is_mx6sl()) {
124 if (cpu_is_mx6dl() &&
125 (clk_get_parent(axi_clk
) != periph_clk
))
126 /* Set the axi_clk to be sourced from the periph_clk.
127 * So that its frequency can be lowered down to 50MHz
128 * or 24MHz as the case may be.
130 clk_set_parent(axi_clk
, periph_clk
);
134 /* Need to ensure that PLL2_PFD_400M is kept ON. */
135 clk_enable(pll2_400
);
136 update_ddr_freq(50000000);
137 /* Make sure periph clk's parent also got updated */
138 clk_set_parent(periph_clk
, pll2_200
);
139 audio_bus_freq_mode
= 1;
140 low_bus_freq_mode
= 0;
142 update_ddr_freq(24000000);
143 /* Make sure periph clk's parent also got updated */
144 clk_set_parent(periph_clk
, osc_clk
);
145 if (audio_bus_freq_mode
)
146 clk_disable(pll2_400
);
147 low_bus_freq_mode
= 1;
148 audio_bus_freq_mode
= 0;
151 if (med_bus_freq_mode
)
152 clk_disable(pll2_400
);
155 med_bus_freq_mode
= 0;
157 /* Set VDDSOC_CAP to 1.1V */
158 ret
= regulator_set_voltage(vddsoc_cap_regulator
, 1100000,
162 "COULD NOT DECREASE VDDSOC_CAP VOLTAGE!!!!\n");
168 arm_mem_clked_in_wait
= true;
170 /* Set periph_clk to be sourced from OSC_CLK */
171 /* Set MMDC clk to 25MHz. */
172 /* First need to set the divider before changing the parent */
173 /* if parent clock is larger than previous one */
174 clk_set_rate(mmdc_ch0_axi
, clk_get_rate(mmdc_ch0_axi
) / 2);
175 clk_set_parent(mmdc_ch0_axi
, pll3_sw_clk
);
176 clk_set_parent(mmdc_ch0_axi
, pll2_200
);
177 clk_set_rate(mmdc_ch0_axi
,
178 clk_round_rate(mmdc_ch0_axi
, LPAPM_CLK
));
180 /* Set AXI to 24MHz. */
181 clk_set_parent(periph_clk
, osc_clk
);
182 clk_set_rate(axi_clk
, clk_round_rate(axi_clk
, LPAPM_CLK
));
183 /* Set AHB to 24MHz. */
184 clk_set_rate(ahb_clk
, clk_round_rate(ahb_clk
, LPAPM_CLK
));
186 low_bus_freq_mode
= 1;
187 audio_bus_freq_mode
= 0;
190 high_bus_freq_mode
= 0;
191 med_bus_freq_mode
= 0;
192 mutex_unlock(&bus_freq_mutex
);
194 /* Set the DDR, AHB to 24MHz.
195 * This mode will be activated only when none of the modules that
196 * need a higher DDR or AHB frequency are active.
198 int set_low_bus_freq(void)
200 if (busfreq_suspended
)
203 if (!bus_freq_scaling_initialized
|| !bus_freq_scaling_is_active
)
206 /* Don't lower the frequency immediately. Instead scheduled a delayed
207 * work and drop the freq if the conditions still remain the same.
209 schedule_delayed_work(&low_bus_freq_handler
, usecs_to_jiffies(3000000));
213 /* Set the DDR to either 528MHz or 400MHz for MX6q
214 * or 400MHz for MX6DL.
216 int set_high_bus_freq(int high_bus_freq
)
220 if (bus_freq_scaling_initialized
&& bus_freq_scaling_is_active
)
221 cancel_delayed_work_sync(&low_bus_freq_handler
);
222 mutex_lock(&bus_freq_mutex
);
223 if (busfreq_suspended
) {
224 mutex_unlock(&bus_freq_mutex
);
228 if (!bus_freq_scaling_initialized
|| !bus_freq_scaling_is_active
) {
229 mutex_unlock(&bus_freq_mutex
);
233 if (high_bus_freq_mode
&& high_bus_freq
) {
234 mutex_unlock(&bus_freq_mutex
);
238 if (med_bus_freq_mode
&& !high_bus_freq
) {
239 mutex_unlock(&bus_freq_mutex
);
243 if (cpu_is_mx6dl() && high_bus_freq
)
246 if (cpu_is_mx6dl() && med_bus_freq_mode
) {
247 mutex_unlock(&bus_freq_mutex
);
250 if ((high_bus_freq_mode
&& (high_bus_freq
|| lp_high_freq
)) ||
251 (med_bus_freq_mode
&& !high_bus_freq
&& lp_med_freq
&&
253 mutex_unlock(&bus_freq_mutex
);
256 if (cpu_is_mx6sl()) {
257 /* Set the voltage of VDDSOC to 1.2V as in normal mode. */
258 ret
= regulator_set_voltage(vddsoc_cap_regulator
, 1200000,
262 "COULD NOT INCREASE VDDSOC_CAP VOLTAGE!!!!\n");
266 /* Need to wait for the regulator to come back up */
268 * Delay time is based on the number of 24MHz clock cycles
269 * programmed in the ANA_MISC2_BASE_ADDR for each
274 /* Set periph_clk to be sourced from pll2_pfd2_400M */
275 /* First need to set the divider before changing the */
276 /* parent if parent clock is larger than previous one */
277 clk_set_rate(ahb_clk
, clk_round_rate(ahb_clk
,
279 clk_set_rate(axi_clk
,
280 clk_round_rate(axi_clk
, LPAPM_CLK
/ 2));
281 clk_set_parent(periph_clk
, pll2_400
);
283 /* Set mmdc_clk_root to be sourced */
284 /* from pll2_pfd2_400M */
285 clk_set_rate(mmdc_ch0_axi
,
286 clk_round_rate(mmdc_ch0_axi
, LPAPM_CLK
/ 2));
287 clk_set_parent(mmdc_ch0_axi
, pll3_sw_clk
);
288 clk_set_parent(mmdc_ch0_axi
, pll2_400
);
289 clk_set_rate(mmdc_ch0_axi
,
290 clk_round_rate(mmdc_ch0_axi
, DDR_MED_CLK
));
292 high_bus_freq_mode
= 1;
293 med_bus_freq_mode
= 0;
297 update_ddr_freq(ddr_normal_rate
);
298 /* Make sure periph clk's parent also got updated */
299 clk_set_parent(periph_clk
, pll2
);
300 if (med_bus_freq_mode
)
301 clk_disable(pll2_400
);
302 high_bus_freq_mode
= 1;
303 med_bus_freq_mode
= 0;
305 clk_enable(pll2_400
);
306 update_ddr_freq(ddr_med_rate
);
307 /* Make sure periph clk's parent also got updated */
308 clk_set_parent(periph_clk
, pll2_400
);
309 high_bus_freq_mode
= 0;
310 med_bus_freq_mode
= 1;
312 if (audio_bus_freq_mode
)
313 clk_disable(pll2_400
);
315 /* AXI_CLK is sourced from PLL3_PFD_540 on MX6DL */
316 if (cpu_is_mx6dl() &&
317 clk_get_parent(axi_clk
) != pll3_540
)
318 clk_set_parent(axi_clk
, pll3_540
);
323 low_bus_freq_mode
= 0;
324 audio_bus_freq_mode
= 0;
326 /* Ensure that WAIT mode can be entered in high bus freq mode. */
329 arm_mem_clked_in_wait
= false;
331 mutex_unlock(&bus_freq_mutex
);
335 int low_freq_bus_used(void)
337 if (!bus_freq_scaling_initialized
)
340 /* We only go the lowest setpoint if ARM is also
341 * at the lowest setpoint.
343 if ((clk_get_rate(cpu_clk
) >
344 cpu_op_tbl
[cpu_op_nr
- 1].cpu_rate
)
345 || (cpu_op_nr
== 1)) {
349 if ((lp_high_freq
== 0)
350 && (lp_med_freq
== 0))
356 void bus_freq_update(struct clk
*clk
, bool flag
)
358 mutex_lock(&bus_freq_mutex
);
361 if (clk
->flags
& AHB_HIGH_SET_POINT
)
363 else if (clk
->flags
& AHB_MED_SET_POINT
)
365 else if (clk
->flags
& AHB_AUDIO_SET_POINT
)
367 /* Update bus freq */
368 if ((clk
->flags
& CPU_FREQ_TRIG_UPDATE
)
369 && (clk_get_usecount(clk
) == 0)) {
371 (AHB_HIGH_SET_POINT
| AHB_MED_SET_POINT
))) {
372 if (low_freq_bus_used()) {
373 if ((clk
->flags
& AHB_AUDIO_SET_POINT
) & !audio_bus_freq_mode
)
375 else if (!low_bus_freq_mode
)
379 if ((clk
->flags
& AHB_MED_SET_POINT
)
380 && !med_bus_freq_mode
) {
381 /* Set to Medium setpoint */
382 mutex_unlock(&bus_freq_mutex
);
383 set_high_bus_freq(0);
386 else if ((clk
->flags
& AHB_HIGH_SET_POINT
)
387 && !high_bus_freq_mode
) {
388 /* Currently at low or medium set point,
389 * need to set to high setpoint
391 mutex_unlock(&bus_freq_mutex
);
392 set_high_bus_freq(1);
399 if (clk
->flags
& AHB_HIGH_SET_POINT
)
401 else if (clk
->flags
& AHB_MED_SET_POINT
)
403 else if (clk
->flags
& AHB_AUDIO_SET_POINT
)
405 /* Update bus freq */
406 if ((clk
->flags
& CPU_FREQ_TRIG_UPDATE
)
407 && (clk_get_usecount(clk
) == 0)) {
408 if (low_freq_bus_used() && !low_bus_freq_mode
)
411 /* Set to either high or medium setpoint. */
412 mutex_unlock(&bus_freq_mutex
);
413 set_high_bus_freq(0);
418 mutex_unlock(&bus_freq_mutex
);
425 static ssize_t
bus_freq_scaling_enable_show(struct device
*dev
,
426 struct device_attribute
*attr
, char *buf
)
428 if (bus_freq_scaling_is_active
)
429 return sprintf(buf
, "Bus frequency scaling is enabled\n");
431 return sprintf(buf
, "Bus frequency scaling is disabled\n");
434 static ssize_t
bus_freq_scaling_enable_store(struct device
*dev
,
435 struct device_attribute
*attr
,
436 const char *buf
, size_t size
)
438 if (strncmp(buf
, "1", 1) == 0) {
439 bus_freq_scaling_is_active
= 1;
440 set_high_bus_freq(0);
441 /* Make sure system can enter low bus mode if it should be in
443 if (low_freq_bus_used() && !low_bus_freq_mode
)
445 } else if (strncmp(buf
, "0", 1) == 0) {
446 if (bus_freq_scaling_is_active
)
447 set_high_bus_freq(1);
448 bus_freq_scaling_is_active
= 0;
453 static int busfreq_suspend(struct platform_device
*pdev
, pm_message_t message
)
458 static int bus_freq_pm_notify(struct notifier_block
*nb
, unsigned long event
,
461 if (event
== PM_SUSPEND_PREPARE
) {
462 set_high_bus_freq(1);
463 busfreq_suspended
= 1;
464 } else if (event
== PM_POST_SUSPEND
) {
465 busfreq_suspended
= 0;
470 static int busfreq_resume(struct platform_device
*pdev
)
474 static struct notifier_block imx_bus_freq_pm_notifier
= {
475 .notifier_call
= bus_freq_pm_notify
,
478 static DEVICE_ATTR(enable
, 0644, bus_freq_scaling_enable_show
,
479 bus_freq_scaling_enable_store
);
482 * This is the probe routine for the bus frequency driver.
484 * @param pdev The platform device structure
486 * @return The function returns 0 on success
489 static int __devinit
busfreq_probe(struct platform_device
*pdev
)
493 busfreq_dev
= &pdev
->dev
;
495 pll2_400
= clk_get(NULL
, "pll2_pfd_400M");
496 if (IS_ERR(pll2_400
)) {
497 printk(KERN_DEBUG
"%s: failed to get pll2_pfd_400M\n",
499 return PTR_ERR(pll2_400
);
502 pll2_200
= clk_get(NULL
, "pll2_200M");
503 if (IS_ERR(pll2_200
)) {
504 printk(KERN_DEBUG
"%s: failed to get pll2_200M\n",
506 return PTR_ERR(pll2_200
);
509 pll2
= clk_get(NULL
, "pll2");
511 printk(KERN_DEBUG
"%s: failed to get pll2\n",
513 return PTR_ERR(pll2
);
516 cpu_clk
= clk_get(NULL
, "cpu_clk");
517 if (IS_ERR(cpu_clk
)) {
518 printk(KERN_DEBUG
"%s: failed to get cpu_clk\n",
520 return PTR_ERR(cpu_clk
);
523 pll3
= clk_get(NULL
, "pll3_main_clk");
525 printk(KERN_DEBUG
"%s: failed to get pll3\n",
527 return PTR_ERR(pll3
);
530 pll3_540
= clk_get(NULL
, "pll3_pfd_540M");
531 if (IS_ERR(pll3_540
)) {
532 printk(KERN_DEBUG
"%s: failed to get periph_clk\n",
534 return PTR_ERR(pll3_540
);
537 pll3_sw_clk
= clk_get(NULL
, "pll3_sw_clk");
538 if (IS_ERR(pll3_sw_clk
)) {
539 printk(KERN_DEBUG
"%s: failed to get pll3_sw_clk\n",
541 return PTR_ERR(pll3_sw_clk
);
544 axi_clk
= clk_get(NULL
, "axi_clk");
545 if (IS_ERR(axi_clk
)) {
546 printk(KERN_DEBUG
"%s: failed to get axi_clk\n",
548 return PTR_ERR(axi_clk
);
551 ahb_clk
= clk_get(NULL
, "ahb");
552 if (IS_ERR(ahb_clk
)) {
553 printk(KERN_DEBUG
"%s: failed to get ahb_clk\n",
555 return PTR_ERR(ahb_clk
);
558 periph_clk
= clk_get(NULL
, "periph_clk");
559 if (IS_ERR(periph_clk
)) {
560 printk(KERN_DEBUG
"%s: failed to get periph_clk\n",
562 return PTR_ERR(periph_clk
);
565 osc_clk
= clk_get(NULL
, "osc");
566 if (IS_ERR(osc_clk
)) {
567 printk(KERN_DEBUG
"%s: failed to get osc_clk\n",
569 return PTR_ERR(osc_clk
);
572 mmdc_ch0_axi
= clk_get(NULL
, "mmdc_ch0_axi");
573 if (IS_ERR(mmdc_ch0_axi
)) {
574 printk(KERN_DEBUG
"%s: failed to get mmdc_ch0_axi\n",
576 return PTR_ERR(mmdc_ch0_axi
);
579 vddsoc_cap_regulator
= regulator_get(NULL
, "cpu_vddsoc");
580 if (IS_ERR(vddsoc_cap_regulator
)) {
581 printk(KERN_ERR
"%s: failed to get vddsoc_cap regulator\n",
583 return PTR_ERR(vddsoc_cap_regulator
);
586 err
= sysfs_create_file(&busfreq_dev
->kobj
, &dev_attr_enable
.attr
);
589 "Unable to register sysdev entry for BUSFREQ");
593 cpu_op_tbl
= get_cpu_op(&cpu_op_nr
);
594 low_bus_freq_mode
= 0;
595 if (cpu_is_mx6dl()) {
596 high_bus_freq_mode
= 0;
597 med_bus_freq_mode
= 1;
598 /* To make pll2_400 use count right, as when
599 system enter 24M, it will disable pll2_400 */
600 clk_enable(pll2_400
);
602 high_bus_freq_mode
= 1;
603 med_bus_freq_mode
= 0;
605 bus_freq_scaling_is_active
= 0;
606 bus_freq_scaling_initialized
= 1;
609 ddr_low_rate
= LPAPM_CLK
;
610 ddr_med_rate
= DDR_MED_CLK
;
611 ddr_normal_rate
= DDR3_NORMAL_CLK
;
613 if (cpu_is_mx6dl() || cpu_is_mx6sl()) {
614 ddr_low_rate
= LPAPM_CLK
;
615 ddr_normal_rate
= ddr_med_rate
= DDR_MED_CLK
;
618 INIT_DELAYED_WORK(&low_bus_freq_handler
, reduce_bus_freq_handler
);
619 register_pm_notifier(&imx_bus_freq_pm_notifier
);
622 init_mmdc_settings();
627 static struct platform_driver busfreq_driver
= {
629 .name
= "imx_busfreq",
631 .probe
= busfreq_probe
,
632 .suspend
= busfreq_suspend
,
633 .resume
= busfreq_resume
,
637 * Initialise the busfreq_driver.
639 * @return The function always returns 0.
642 static int __init
busfreq_init(void)
644 if (platform_driver_register(&busfreq_driver
) != 0) {
645 printk(KERN_ERR
"busfreq_driver register failed\n");
649 printk(KERN_INFO
"Bus freq driver module loaded\n");
651 /* Enable busfreq by default. */
652 bus_freq_scaling_is_active
= 1;
655 set_high_bus_freq(1);
657 set_high_bus_freq(0);
659 /* Make sure system can enter low bus mode if it should be in
661 if (low_freq_bus_used() && !low_bus_freq_mode
)
664 printk(KERN_INFO
"Bus freq driver Enabled\n");
668 static void __exit
busfreq_cleanup(void)
670 sysfs_remove_file(&busfreq_dev
->kobj
, &dev_attr_enable
.attr
);
672 /* Unregister the device structure */
673 platform_driver_unregister(&busfreq_driver
);
674 bus_freq_scaling_initialized
= 0;
677 late_initcall(busfreq_init
);
678 module_exit(busfreq_cleanup
);
680 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
681 MODULE_DESCRIPTION("BusFreq driver");
682 MODULE_LICENSE("GPL");