Merge branch linux-tegra-2.6.36 into android-tegra-2.6.36
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-tegra / cpu-tegra.c
1 /*
2  * arch/arm/mach-tegra/cpu-tegra.c
3  *
4  * Copyright (C) 2010 Google, Inc.
5  *
6  * Author:
7  *      Colin Cross <ccross@google.com>
8  *      Based on arch/arm/plat-omap/cpu-omap.c, (C) 2005 Nokia Corporation
9  *
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.
13  *
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.
18  *
19  */
20
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>
30 #include <linux/io.h>
31 #include <linux/suspend.h>
32
33 #include <asm/smp_twd.h>
34 #include <asm/system.h>
35
36 #include <mach/hardware.h>
37 #include <mach/clk.h>
38
39 /* Frequency table index must be sequential starting at 0 */
40 static struct cpufreq_frequency_table freq_table[] = {
41         { 0, 216000 },
42         { 1, 312000 },
43         { 2, 456000 },
44         { 3, 608000 },
45         { 4, 760000 },
46         { 5, 816000 },
47         { 6, 912000 },
48         { 7, 1000000 },
49         { 8, CPUFREQ_TABLE_END },
50 };
51
52 #define NUM_CPUS        2
53
54 static struct clk *cpu_clk;
55
56 static unsigned long target_cpu_speed[NUM_CPUS];
57 static DEFINE_MUTEX(tegra_cpu_lock);
58 static bool is_suspended;
59
60 int tegra_verify_speed(struct cpufreq_policy *policy)
61 {
62         return cpufreq_frequency_table_verify(policy, freq_table);
63 }
64
65 unsigned int tegra_getspeed(unsigned int cpu)
66 {
67         unsigned long rate;
68
69         if (cpu >= NUM_CPUS)
70                 return 0;
71
72         rate = clk_get_rate(cpu_clk) / 1000;
73         return rate;
74 }
75
76 #ifdef CONFIG_HAVE_ARM_TWD
77 static void tegra_cpufreq_rescale_twd_other_cpu(void *data) {
78         unsigned long new_rate = *(unsigned long *)data;
79         twd_recalc_prescaler(new_rate);
80 }
81
82 static void tegra_cpufreq_rescale_twds(unsigned long new_rate)
83 {
84         twd_recalc_prescaler(new_rate);
85         smp_call_function(tegra_cpufreq_rescale_twd_other_cpu, &new_rate, 1);
86 }
87 #else
88 static inline void tegra_cpufreq_rescale_twds(unsigned long new_rate)
89 {
90 }
91 #endif
92
93 static int tegra_update_cpu_speed(unsigned long rate)
94 {
95         int ret = 0;
96         struct cpufreq_freqs freqs;
97
98         freqs.old = tegra_getspeed(0);
99         freqs.new = rate;
100
101         if (freqs.old == freqs.new)
102                 return ret;
103
104         for_each_online_cpu(freqs.cpu)
105                 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
106
107         if (freqs.new > freqs.old)
108                 tegra_cpufreq_rescale_twds(freqs.new * 1000);
109
110 #ifdef CONFIG_CPU_FREQ_DEBUG
111         printk(KERN_DEBUG "cpufreq-tegra: transition: %u --> %u\n",
112                freqs.old, freqs.new);
113 #endif
114
115         ret = clk_set_rate(cpu_clk, freqs.new * 1000);
116         if (ret) {
117                 pr_err("cpu-tegra: Failed to set cpu frequency to %d kHz\n",
118                         freqs.new);
119                 return ret;
120         }
121
122         if (freqs.new < freqs.old)
123                 tegra_cpufreq_rescale_twds(freqs.new * 1000);
124
125         for_each_online_cpu(freqs.cpu)
126                 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
127
128         return 0;
129 }
130
131 static unsigned long tegra_cpu_highest_speed(void) {
132         unsigned long rate = 0;
133         int i;
134
135         for_each_online_cpu(i)
136                 rate = max(rate, target_cpu_speed[i]);
137         return rate;
138 }
139
140 static int tegra_target(struct cpufreq_policy *policy,
141                        unsigned int target_freq,
142                        unsigned int relation)
143 {
144         int idx;
145         unsigned int freq;
146         int ret = 0;
147
148         mutex_lock(&tegra_cpu_lock);
149
150         if (is_suspended) {
151                 ret = -EBUSY;
152                 goto out;
153         }
154
155         cpufreq_frequency_table_target(policy, freq_table, target_freq,
156                 relation, &idx);
157
158         freq = freq_table[idx].frequency;
159
160         target_cpu_speed[policy->cpu] = freq;
161
162         ret = tegra_update_cpu_speed(tegra_cpu_highest_speed());
163
164 out:
165         mutex_unlock(&tegra_cpu_lock);
166         return ret;
167 }
168
169 static int tegra_pm_notify(struct notifier_block *nb, unsigned long event,
170         void *dummy)
171 {
172         mutex_lock(&tegra_cpu_lock);
173         if (event == PM_SUSPEND_PREPARE) {
174                 is_suspended = true;
175                 pr_info("Tegra cpufreq suspend: setting frequency to %d kHz\n",
176                         freq_table[0].frequency);
177                 tegra_update_cpu_speed(freq_table[0].frequency);
178         } else if (event == PM_POST_SUSPEND) {
179                 is_suspended = false;
180         }
181         mutex_unlock(&tegra_cpu_lock);
182
183         return NOTIFY_OK;
184 }
185
186 static struct notifier_block tegra_cpu_pm_notifier = {
187         .notifier_call = tegra_pm_notify,
188 };
189
190 static int tegra_cpu_init(struct cpufreq_policy *policy)
191 {
192         if (policy->cpu >= NUM_CPUS)
193                 return -EINVAL;
194
195         cpu_clk = clk_get_sys(NULL, "cpu");
196         if (IS_ERR(cpu_clk))
197                 return PTR_ERR(cpu_clk);
198
199         cpufreq_frequency_table_cpuinfo(policy, freq_table);
200         cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
201         policy->cur = tegra_getspeed(policy->cpu);
202         target_cpu_speed[policy->cpu] = policy->cur;
203
204         /* FIXME: what's the actual transition time? */
205         policy->cpuinfo.transition_latency = 300 * 1000;
206
207         policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
208         cpumask_copy(policy->related_cpus, cpu_possible_mask);
209
210         if (policy->cpu == 0)
211                 register_pm_notifier(&tegra_cpu_pm_notifier);
212
213         return 0;
214 }
215
216 static int tegra_cpu_exit(struct cpufreq_policy *policy)
217 {
218         cpufreq_frequency_table_cpuinfo(policy, freq_table);
219         clk_put(cpu_clk);
220         return 0;
221 }
222
223 static struct freq_attr *tegra_cpufreq_attr[] = {
224         &cpufreq_freq_attr_scaling_available_freqs,
225         NULL,
226 };
227
228 static struct cpufreq_driver tegra_cpufreq_driver = {
229         .verify         = tegra_verify_speed,
230         .target         = tegra_target,
231         .get            = tegra_getspeed,
232         .init           = tegra_cpu_init,
233         .exit           = tegra_cpu_exit,
234         .name           = "tegra",
235         .attr           = tegra_cpufreq_attr,
236 };
237
238 static int __init tegra_cpufreq_init(void)
239 {
240         return cpufreq_register_driver(&tegra_cpufreq_driver);
241 }
242
243 static void __exit tegra_cpufreq_exit(void)
244 {
245         cpufreq_unregister_driver(&tegra_cpufreq_driver);
246 }
247
248
249 MODULE_AUTHOR("Colin Cross <ccross@android.com>");
250 MODULE_DESCRIPTION("cpufreq driver for Nvidia Tegra2");
251 MODULE_LICENSE("GPL");
252 module_init(tegra_cpufreq_init);
253 module_exit(tegra_cpufreq_exit);