blob: 77bb00f43aa29c1c62aa3a20c1c30f51ef11fccd [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* arch/arm/mach-msm/cpufreq.c
2 *
3 * MSM architecture cpufreq driver
4 *
5 * Copyright (C) 2007 Google, Inc.
Vikram Mulukutlabc2e9572011-11-04 03:41:38 -07006 * Copyright (c) 2007-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07007 * Author: Mike A. Chan <mikechan@google.com>
8 *
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 */
19
20#include <linux/earlysuspend.h>
21#include <linux/init.h>
Praveen Chidambaram696a5612012-05-25 17:29:11 -060022#include <linux/module.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023#include <linux/cpufreq.h>
24#include <linux/workqueue.h>
25#include <linux/completion.h>
26#include <linux/cpu.h>
27#include <linux/cpumask.h>
28#include <linux/sched.h>
29#include <linux/suspend.h>
Stepan Moskovchenkoaf25dd92011-08-05 18:12:48 -070030#include <mach/socinfo.h>
Praveen Chidambaram696a5612012-05-25 17:29:11 -060031#include <mach/cpufreq.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032
33#include "acpuclock.h"
34
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035struct cpufreq_suspend_t {
36 struct mutex suspend_mutex;
37 int device_suspended;
38};
39
40static DEFINE_PER_CPU(struct cpufreq_suspend_t, cpufreq_suspend);
41
Praveen Chidambaram696a5612012-05-25 17:29:11 -060042struct cpu_freq {
43 uint32_t max;
44 uint32_t min;
45 uint32_t allowed_max;
46 uint32_t allowed_min;
47 uint32_t limits_init;
48};
49
50static DEFINE_PER_CPU(struct cpu_freq, cpu_freq_info);
51
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052static int set_cpu_freq(struct cpufreq_policy *policy, unsigned int new_freq)
53{
54 int ret = 0;
55 struct cpufreq_freqs freqs;
Praveen Chidambaram696a5612012-05-25 17:29:11 -060056 struct cpu_freq *limit = &per_cpu(cpu_freq_info, policy->cpu);
57
58 if (limit->limits_init) {
59 if (new_freq > limit->allowed_max) {
60 new_freq = limit->allowed_max;
61 pr_debug("max: limiting freq to %d\n", new_freq);
62 }
63
64 if (new_freq < limit->allowed_min) {
65 new_freq = limit->allowed_min;
66 pr_debug("min: limiting freq to %d\n", new_freq);
67 }
68 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069
70 freqs.old = policy->cur;
David Ng3f76d272012-02-08 10:43:37 -080071 freqs.new = new_freq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072 freqs.cpu = policy->cpu;
73 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
74 ret = acpuclk_set_rate(policy->cpu, new_freq, SETRATE_CPUFREQ);
75 if (!ret)
76 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
77
78 return ret;
79}
80
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081static int msm_cpufreq_target(struct cpufreq_policy *policy,
82 unsigned int target_freq,
83 unsigned int relation)
84{
85 int ret = -EFAULT;
86 int index;
87 struct cpufreq_frequency_table *table;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089 if (!cpu_active(policy->cpu)) {
90 pr_info("cpufreq: cpu %d is not active.\n", policy->cpu);
91 return -ENODEV;
92 }
Praveen Chidambaramfdf788b2012-04-03 17:46:09 -060093
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070094 mutex_lock(&per_cpu(cpufreq_suspend, policy->cpu).suspend_mutex);
95
96 if (per_cpu(cpufreq_suspend, policy->cpu).device_suspended) {
97 pr_debug("cpufreq: cpu%d scheduling frequency change "
98 "in suspend.\n", policy->cpu);
99 ret = -EFAULT;
100 goto done;
101 }
102
103 table = cpufreq_frequency_get_table(policy->cpu);
104 if (cpufreq_frequency_table_target(policy, table, target_freq, relation,
105 &index)) {
106 pr_err("cpufreq: invalid target_freq: %d\n", target_freq);
107 ret = -EINVAL;
108 goto done;
109 }
110
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111 pr_debug("CPU[%d] target %d relation %d (%d-%d) selected %d\n",
112 policy->cpu, target_freq, relation,
113 policy->min, policy->max, table[index].frequency);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115 ret = set_cpu_freq(policy, table[index].frequency);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116
117done:
118 mutex_unlock(&per_cpu(cpufreq_suspend, policy->cpu).suspend_mutex);
119 return ret;
120}
121
122static int msm_cpufreq_verify(struct cpufreq_policy *policy)
123{
124 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
125 policy->cpuinfo.max_freq);
126 return 0;
127}
128
Praveen Chidambaram696a5612012-05-25 17:29:11 -0600129static unsigned int msm_cpufreq_get_freq(unsigned int cpu)
130{
131 return acpuclk_get_rate(cpu);
132}
133
134static inline int msm_cpufreq_limits_init(void)
135{
136 int cpu = 0;
137 int i = 0;
138 struct cpufreq_frequency_table *table = NULL;
139 uint32_t min = (uint32_t) -1;
140 uint32_t max = 0;
141 struct cpu_freq *limit = NULL;
142
143 for_each_possible_cpu(cpu) {
144 limit = &per_cpu(cpu_freq_info, cpu);
145 table = cpufreq_frequency_get_table(cpu);
146 if (table == NULL) {
147 pr_err("%s: error reading cpufreq table for cpu %d\n",
148 __func__, cpu);
149 continue;
150 }
151 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
152 if (table[i].frequency > max)
153 max = table[i].frequency;
154 if (table[i].frequency < min)
155 min = table[i].frequency;
156 }
157 limit->allowed_min = min;
158 limit->allowed_max = max;
159 limit->min = min;
160 limit->max = max;
161 limit->limits_init = 1;
162 }
163
164 return 0;
165}
166
167int msm_cpufreq_set_freq_limits(uint32_t cpu, uint32_t min, uint32_t max)
168{
169 struct cpu_freq *limit = &per_cpu(cpu_freq_info, cpu);
170
171 if (!limit->limits_init)
172 msm_cpufreq_limits_init();
173
174 if ((min != MSM_CPUFREQ_NO_LIMIT) &&
175 min >= limit->min && min <= limit->max)
176 limit->allowed_min = min;
177 else
178 limit->allowed_min = limit->min;
179
180
181 if ((max != MSM_CPUFREQ_NO_LIMIT) &&
182 max <= limit->max && max >= limit->min)
183 limit->allowed_max = max;
184 else
185 limit->allowed_max = limit->max;
186
187 pr_debug("%s: Limiting cpu %d min = %d, max = %d\n",
188 __func__, cpu,
189 limit->allowed_min, limit->allowed_max);
190
191 return 0;
192}
193EXPORT_SYMBOL(msm_cpufreq_set_freq_limits);
194
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195static int __cpuinit msm_cpufreq_init(struct cpufreq_policy *policy)
196{
197 int cur_freq;
198 int index;
199 struct cpufreq_frequency_table *table;
Pankaj Kumaref7c3942012-04-03 18:00:52 +0530200
201 table = cpufreq_frequency_get_table(policy->cpu);
202 if (table == NULL)
203 return -ENODEV;
Pankaj Kumar0f263682012-01-05 17:01:57 +0530204 /*
205 * In 8625 both cpu core's frequency can not
206 * be changed independently. Each cpu is bound to
207 * same frequency. Hence set the cpumask to all cpu.
208 */
209 if (cpu_is_msm8625())
210 cpumask_setall(policy->cpus);
211
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212 if (cpufreq_frequency_table_cpuinfo(policy, table)) {
213#ifdef CONFIG_MSM_CPU_FREQ_SET_MIN_MAX
214 policy->cpuinfo.min_freq = CONFIG_MSM_CPU_FREQ_MIN;
215 policy->cpuinfo.max_freq = CONFIG_MSM_CPU_FREQ_MAX;
216#endif
217 }
218#ifdef CONFIG_MSM_CPU_FREQ_SET_MIN_MAX
219 policy->min = CONFIG_MSM_CPU_FREQ_MIN;
220 policy->max = CONFIG_MSM_CPU_FREQ_MAX;
221#endif
222
223 cur_freq = acpuclk_get_rate(policy->cpu);
224 if (cpufreq_frequency_table_target(policy, table, cur_freq,
Matt Wagantallb31e4682011-10-12 12:50:27 -0700225 CPUFREQ_RELATION_H, &index) &&
226 cpufreq_frequency_table_target(policy, table, cur_freq,
227 CPUFREQ_RELATION_L, &index)) {
Praveen Chidambaramaa75efa2012-09-18 13:42:40 -0600228 pr_info("%s: cpu%d at invalid freq: %d\n", __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229 policy->cpu, cur_freq);
230 return -EINVAL;
231 }
232
233 if (cur_freq != table[index].frequency) {
234 int ret = 0;
235 ret = acpuclk_set_rate(policy->cpu, table[index].frequency,
236 SETRATE_CPUFREQ);
237 if (ret)
238 return ret;
239 pr_info("cpufreq: cpu%d init at %d switching to %d\n",
240 policy->cpu, cur_freq, table[index].frequency);
241 cur_freq = table[index].frequency;
242 }
243
244 policy->cur = cur_freq;
245
246 policy->cpuinfo.transition_latency =
247 acpuclk_get_switch_time() * NSEC_PER_USEC;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248
249 return 0;
250}
251
Praveen Chidambaramaa75efa2012-09-18 13:42:40 -0600252static int __cpuinit msm_cpufreq_cpu_callback(struct notifier_block *nfb,
253 unsigned long action, void *hcpu)
254{
255 unsigned int cpu = (unsigned long)hcpu;
256
257 switch (action) {
258 case CPU_ONLINE:
259 case CPU_ONLINE_FROZEN:
260 per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
261 break;
262 case CPU_DOWN_PREPARE:
263 case CPU_DOWN_PREPARE_FROZEN:
264 mutex_lock(&per_cpu(cpufreq_suspend, cpu).suspend_mutex);
265 per_cpu(cpufreq_suspend, cpu).device_suspended = 1;
266 mutex_unlock(&per_cpu(cpufreq_suspend, cpu).suspend_mutex);
267 break;
268 case CPU_DOWN_FAILED:
269 case CPU_DOWN_FAILED_FROZEN:
270 per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
271 break;
272 }
273
274 return NOTIFY_OK;
275}
276
277static struct notifier_block __refdata msm_cpufreq_cpu_notifier = {
278 .notifier_call = msm_cpufreq_cpu_callback,
279};
280
Anji Jonnalab2408f42012-12-13 14:03:54 +0530281/*
282 * Define suspend/resume for cpufreq_driver. Kernel will call
283 * these during suspend/resume with interrupts disabled. This
284 * helps the suspend/resume variable get's updated before cpufreq
285 * governor tries to change the frequency after coming out of suspend.
286 */
287static int msm_cpufreq_suspend(struct cpufreq_policy *policy)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288{
289 int cpu;
290
291 for_each_possible_cpu(cpu) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 per_cpu(cpufreq_suspend, cpu).device_suspended = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 }
294
Anji Jonnalab2408f42012-12-13 14:03:54 +0530295 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296}
297
Anji Jonnalab2408f42012-12-13 14:03:54 +0530298static int msm_cpufreq_resume(struct cpufreq_policy *policy)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299{
300 int cpu;
301
302 for_each_possible_cpu(cpu) {
303 per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
304 }
305
Anji Jonnalab2408f42012-12-13 14:03:54 +0530306 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307}
308
Stepan Moskovchenko5627bb42011-10-13 16:25:41 -0700309static struct freq_attr *msm_freq_attr[] = {
310 &cpufreq_freq_attr_scaling_available_freqs,
311 NULL,
312};
313
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700314static struct cpufreq_driver msm_cpufreq_driver = {
315 /* lps calculations are handled here. */
316 .flags = CPUFREQ_STICKY | CPUFREQ_CONST_LOOPS,
317 .init = msm_cpufreq_init,
318 .verify = msm_cpufreq_verify,
319 .target = msm_cpufreq_target,
Praveen Chidambaram696a5612012-05-25 17:29:11 -0600320 .get = msm_cpufreq_get_freq,
Anji Jonnalab2408f42012-12-13 14:03:54 +0530321 .suspend = msm_cpufreq_suspend,
322 .resume = msm_cpufreq_resume,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323 .name = "msm",
Stepan Moskovchenko5627bb42011-10-13 16:25:41 -0700324 .attr = msm_freq_attr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325};
326
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327static int __init msm_cpufreq_register(void)
328{
329 int cpu;
330
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331 for_each_possible_cpu(cpu) {
332 mutex_init(&(per_cpu(cpufreq_suspend, cpu).suspend_mutex));
333 per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
334 }
335
Narayanan Gopalakrishnan4f5e7132012-07-17 16:07:50 -0700336 register_hotcpu_notifier(&msm_cpufreq_cpu_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 return cpufreq_register_driver(&msm_cpufreq_driver);
339}
340
341late_initcall(msm_cpufreq_register);