2 * Copyright (C) 2010 Google, Inc.
5 * Colin Cross <ccross@google.com>
6 * Based on arch/arm/plat-omap/cpu-omap.c, (C) 2005 Nokia Corporation
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/sched.h>
23 #include <linux/cpufreq.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/err.h>
27 #include <linux/clk.h>
29 #include <linux/suspend.h>
31 /* Frequency table index must be sequential starting at 0 */
32 static struct cpufreq_frequency_table freq_table
[] = {
41 { 8, CPUFREQ_TABLE_END
},
46 static struct clk
*cpu_clk
;
47 static struct clk
*pll_x_clk
;
48 static struct clk
*pll_p_clk
;
49 static struct clk
*emc_clk
;
51 static unsigned long target_cpu_speed
[NUM_CPUS
];
52 static DEFINE_MUTEX(tegra_cpu_lock
);
53 static bool is_suspended
;
55 static int tegra_verify_speed(struct cpufreq_policy
*policy
)
57 return cpufreq_frequency_table_verify(policy
, freq_table
);
60 static unsigned int tegra_getspeed(unsigned int cpu
)
67 rate
= clk_get_rate(cpu_clk
) / 1000;
71 static int tegra_cpu_clk_set_rate(unsigned long rate
)
76 * Take an extra reference to the main pll so it doesn't turn
77 * off when we move the cpu off of it
79 clk_prepare_enable(pll_x_clk
);
81 ret
= clk_set_parent(cpu_clk
, pll_p_clk
);
83 pr_err("Failed to switch cpu to clock pll_p\n");
87 if (rate
== clk_get_rate(pll_p_clk
))
90 ret
= clk_set_rate(pll_x_clk
, rate
);
92 pr_err("Failed to change pll_x to %lu\n", rate
);
96 ret
= clk_set_parent(cpu_clk
, pll_x_clk
);
98 pr_err("Failed to switch cpu to clock pll_x\n");
103 clk_disable_unprepare(pll_x_clk
);
107 static int tegra_update_cpu_speed(struct cpufreq_policy
*policy
,
111 struct cpufreq_freqs freqs
;
113 freqs
.old
= tegra_getspeed(0);
116 if (freqs
.old
== freqs
.new)
120 * Vote on memory bus frequency based on cpu frequency
121 * This sets the minimum frequency, display or avp may request higher
124 clk_set_rate(emc_clk
, 600000000); /* cpu 816 MHz, emc max */
125 else if (rate
>= 456000)
126 clk_set_rate(emc_clk
, 300000000); /* cpu 456 MHz, emc 150Mhz */
128 clk_set_rate(emc_clk
, 100000000); /* emc 50Mhz */
130 cpufreq_notify_transition(policy
, &freqs
, CPUFREQ_PRECHANGE
);
132 #ifdef CONFIG_CPU_FREQ_DEBUG
133 printk(KERN_DEBUG
"cpufreq-tegra: transition: %u --> %u\n",
134 freqs
.old
, freqs
.new);
137 ret
= tegra_cpu_clk_set_rate(freqs
.new * 1000);
139 pr_err("cpu-tegra: Failed to set cpu frequency to %d kHz\n",
144 cpufreq_notify_transition(policy
, &freqs
, CPUFREQ_POSTCHANGE
);
149 static unsigned long tegra_cpu_highest_speed(void)
151 unsigned long rate
= 0;
154 for_each_online_cpu(i
)
155 rate
= max(rate
, target_cpu_speed
[i
]);
159 static int tegra_target(struct cpufreq_policy
*policy
,
160 unsigned int target_freq
,
161 unsigned int relation
)
167 mutex_lock(&tegra_cpu_lock
);
174 cpufreq_frequency_table_target(policy
, freq_table
, target_freq
,
177 freq
= freq_table
[idx
].frequency
;
179 target_cpu_speed
[policy
->cpu
] = freq
;
181 ret
= tegra_update_cpu_speed(policy
, tegra_cpu_highest_speed());
184 mutex_unlock(&tegra_cpu_lock
);
188 static int tegra_pm_notify(struct notifier_block
*nb
, unsigned long event
,
191 mutex_lock(&tegra_cpu_lock
);
192 if (event
== PM_SUSPEND_PREPARE
) {
193 struct cpufreq_policy
*policy
= cpufreq_cpu_get(0);
195 pr_info("Tegra cpufreq suspend: setting frequency to %d kHz\n",
196 freq_table
[0].frequency
);
197 tegra_update_cpu_speed(policy
, freq_table
[0].frequency
);
198 cpufreq_cpu_put(policy
);
199 } else if (event
== PM_POST_SUSPEND
) {
200 is_suspended
= false;
202 mutex_unlock(&tegra_cpu_lock
);
207 static struct notifier_block tegra_cpu_pm_notifier
= {
208 .notifier_call
= tegra_pm_notify
,
211 static int tegra_cpu_init(struct cpufreq_policy
*policy
)
213 if (policy
->cpu
>= NUM_CPUS
)
216 clk_prepare_enable(emc_clk
);
217 clk_prepare_enable(cpu_clk
);
219 cpufreq_frequency_table_cpuinfo(policy
, freq_table
);
220 cpufreq_frequency_table_get_attr(freq_table
, policy
->cpu
);
221 policy
->cur
= tegra_getspeed(policy
->cpu
);
222 target_cpu_speed
[policy
->cpu
] = policy
->cur
;
224 /* FIXME: what's the actual transition time? */
225 policy
->cpuinfo
.transition_latency
= 300 * 1000;
227 cpumask_copy(policy
->cpus
, cpu_possible_mask
);
229 if (policy
->cpu
== 0)
230 register_pm_notifier(&tegra_cpu_pm_notifier
);
235 static int tegra_cpu_exit(struct cpufreq_policy
*policy
)
237 cpufreq_frequency_table_cpuinfo(policy
, freq_table
);
238 clk_disable_unprepare(emc_clk
);
242 static struct freq_attr
*tegra_cpufreq_attr
[] = {
243 &cpufreq_freq_attr_scaling_available_freqs
,
247 static struct cpufreq_driver tegra_cpufreq_driver
= {
248 .verify
= tegra_verify_speed
,
249 .target
= tegra_target
,
250 .get
= tegra_getspeed
,
251 .init
= tegra_cpu_init
,
252 .exit
= tegra_cpu_exit
,
254 .attr
= tegra_cpufreq_attr
,
257 static int __init
tegra_cpufreq_init(void)
259 cpu_clk
= clk_get_sys(NULL
, "cpu");
261 return PTR_ERR(cpu_clk
);
263 pll_x_clk
= clk_get_sys(NULL
, "pll_x");
264 if (IS_ERR(pll_x_clk
))
265 return PTR_ERR(pll_x_clk
);
267 pll_p_clk
= clk_get_sys(NULL
, "pll_p_cclk");
268 if (IS_ERR(pll_p_clk
))
269 return PTR_ERR(pll_p_clk
);
271 emc_clk
= clk_get_sys("cpu", "emc");
272 if (IS_ERR(emc_clk
)) {
274 return PTR_ERR(emc_clk
);
277 return cpufreq_register_driver(&tegra_cpufreq_driver
);
280 static void __exit
tegra_cpufreq_exit(void)
282 cpufreq_unregister_driver(&tegra_cpufreq_driver
);
288 MODULE_AUTHOR("Colin Cross <ccross@android.com>");
289 MODULE_DESCRIPTION("cpufreq driver for Nvidia Tegra2");
290 MODULE_LICENSE("GPL");
291 module_init(tegra_cpufreq_init
);
292 module_exit(tegra_cpufreq_exit
);