2 * arch/arm/mach-tegra/cpu-tegra.c
4 * Copyright (C) 2010 Google, Inc.
7 * Colin Cross <ccross@google.com>
8 * Based on arch/arm/plat-omap/cpu-omap.c, (C) 2005 Nokia Corporation
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/sched.h>
25 #include <linux/cpufreq.h>
26 #include <linux/delay.h>
27 #include <linux/init.h>
28 #include <linux/err.h>
29 #include <linux/clk.h>
32 #include <asm/system.h>
34 #include <mach/hardware.h>
37 /* Frequency table index must be sequential starting at 0 */
38 static struct cpufreq_frequency_table freq_table[] = {
46 { 7, CPUFREQ_TABLE_END },
51 static struct clk *cpu_clk;
53 static unsigned long target_cpu_speed[NUM_CPUS];
55 int tegra_verify_speed(struct cpufreq_policy *policy)
57 return cpufreq_frequency_table_verify(policy, freq_table);
60 unsigned int tegra_getspeed(unsigned int cpu)
67 rate = clk_get_rate(cpu_clk) / 1000;
71 static int tegra_update_cpu_speed(void)
74 unsigned long rate = 0;
76 struct cpufreq_freqs freqs;
78 for_each_online_cpu(i)
79 rate = max(rate, target_cpu_speed[i]);
81 freqs.old = tegra_getspeed(0);
84 if (freqs.old == freqs.new)
87 for_each_online_cpu(freqs.cpu)
88 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
90 #ifdef CONFIG_CPU_FREQ_DEBUG
91 printk(KERN_DEBUG "cpufreq-tegra: transition: %u --> %u\n",
92 freqs.old, freqs.new);
95 ret = clk_set_rate_cansleep(cpu_clk, freqs.new * 1000);
97 pr_err("cpu-tegra: Failed to set cpu frequency to %d kHz\n",
102 for_each_online_cpu(freqs.cpu)
103 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
108 static int tegra_target(struct cpufreq_policy *policy,
109 unsigned int target_freq,
110 unsigned int relation)
115 cpufreq_frequency_table_target(policy, freq_table, target_freq,
118 freq = freq_table[idx].frequency;
120 target_cpu_speed[policy->cpu] = freq;
122 return tegra_update_cpu_speed();
125 static int tegra_cpu_init(struct cpufreq_policy *policy)
127 if (policy->cpu >= NUM_CPUS)
130 cpu_clk = clk_get_sys(NULL, "cpu");
132 return PTR_ERR(cpu_clk);
134 cpufreq_frequency_table_cpuinfo(policy, freq_table);
135 cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
136 policy->cur = tegra_getspeed(policy->cpu);
137 target_cpu_speed[policy->cpu] = policy->cur;
139 /* FIXME: what's the actual transition time? */
140 policy->cpuinfo.transition_latency = 300 * 1000;
142 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
143 cpumask_copy(policy->related_cpus, cpu_possible_mask);
148 static int tegra_cpu_exit(struct cpufreq_policy *policy)
150 cpufreq_frequency_table_cpuinfo(policy, freq_table);
155 static struct freq_attr *tegra_cpufreq_attr[] = {
156 &cpufreq_freq_attr_scaling_available_freqs,
160 static struct cpufreq_driver tegra_cpufreq_driver = {
161 .verify = tegra_verify_speed,
162 .target = tegra_target,
163 .get = tegra_getspeed,
164 .init = tegra_cpu_init,
165 .exit = tegra_cpu_exit,
167 .attr = tegra_cpufreq_attr,
170 static int __init tegra_cpufreq_init(void)
172 return cpufreq_register_driver(&tegra_cpufreq_driver);
175 static void __exit tegra_cpufreq_exit(void)
177 cpufreq_unregister_driver(&tegra_cpufreq_driver);
181 MODULE_AUTHOR("Colin Cross <ccross@android.com>");
182 MODULE_DESCRIPTION("cpufreq driver for Nvidia Tegra2");
183 MODULE_LICENSE("GPL");
184 module_init(tegra_cpufreq_init);
185 module_exit(tegra_cpufreq_exit);