blob: 06627263e0719e4ebef9aa83419202508e23051b [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.
Duy Truonge833aca2013-02-12 13:35:08 -08006 * Copyright (c) 2007-2012, The Linux Foundation. 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
Praveen Chidambaram241ded32013-03-11 14:50:06 -060035struct cpufreq_work_struct {
36 struct work_struct work;
37 struct cpufreq_policy *policy;
38 struct completion complete;
39 int frequency;
40 int status;
41};
42
43static DEFINE_PER_CPU(struct cpufreq_work_struct, cpufreq_work);
44static struct workqueue_struct *msm_cpufreq_wq;
45
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046struct cpufreq_suspend_t {
47 struct mutex suspend_mutex;
48 int device_suspended;
49};
50
51static DEFINE_PER_CPU(struct cpufreq_suspend_t, cpufreq_suspend);
52
Praveen Chidambaram696a5612012-05-25 17:29:11 -060053struct cpu_freq {
54 uint32_t max;
55 uint32_t min;
56 uint32_t allowed_max;
57 uint32_t allowed_min;
58 uint32_t limits_init;
59};
60
61static DEFINE_PER_CPU(struct cpu_freq, cpu_freq_info);
62
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070063static int set_cpu_freq(struct cpufreq_policy *policy, unsigned int new_freq)
64{
65 int ret = 0;
66 struct cpufreq_freqs freqs;
Praveen Chidambaram696a5612012-05-25 17:29:11 -060067 struct cpu_freq *limit = &per_cpu(cpu_freq_info, policy->cpu);
68
69 if (limit->limits_init) {
70 if (new_freq > limit->allowed_max) {
71 new_freq = limit->allowed_max;
72 pr_debug("max: limiting freq to %d\n", new_freq);
73 }
74
75 if (new_freq < limit->allowed_min) {
76 new_freq = limit->allowed_min;
77 pr_debug("min: limiting freq to %d\n", new_freq);
78 }
79 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080
81 freqs.old = policy->cur;
David Ng3f76d272012-02-08 10:43:37 -080082 freqs.new = new_freq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083 freqs.cpu = policy->cpu;
84 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
85 ret = acpuclk_set_rate(policy->cpu, new_freq, SETRATE_CPUFREQ);
86 if (!ret)
87 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
88
89 return ret;
90}
91
Praveen Chidambaram241ded32013-03-11 14:50:06 -060092static void set_cpu_work(struct work_struct *work)
93{
94 struct cpufreq_work_struct *cpu_work =
95 container_of(work, struct cpufreq_work_struct, work);
96
97 cpu_work->status = set_cpu_freq(cpu_work->policy, cpu_work->frequency);
98 complete(&cpu_work->complete);
99}
100
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101static int msm_cpufreq_target(struct cpufreq_policy *policy,
102 unsigned int target_freq,
103 unsigned int relation)
104{
105 int ret = -EFAULT;
106 int index;
107 struct cpufreq_frequency_table *table;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108
Praveen Chidambaram241ded32013-03-11 14:50:06 -0600109 struct cpufreq_work_struct *cpu_work = NULL;
110 cpumask_var_t mask;
111
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112 if (!cpu_active(policy->cpu)) {
113 pr_info("cpufreq: cpu %d is not active.\n", policy->cpu);
114 return -ENODEV;
115 }
Praveen Chidambaramfdf788b2012-04-03 17:46:09 -0600116
Praveen Chidambaram241ded32013-03-11 14:50:06 -0600117 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
118 return -ENOMEM;
119
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120 mutex_lock(&per_cpu(cpufreq_suspend, policy->cpu).suspend_mutex);
121
122 if (per_cpu(cpufreq_suspend, policy->cpu).device_suspended) {
123 pr_debug("cpufreq: cpu%d scheduling frequency change "
124 "in suspend.\n", policy->cpu);
125 ret = -EFAULT;
126 goto done;
127 }
128
129 table = cpufreq_frequency_get_table(policy->cpu);
130 if (cpufreq_frequency_table_target(policy, table, target_freq, relation,
131 &index)) {
132 pr_err("cpufreq: invalid target_freq: %d\n", target_freq);
133 ret = -EINVAL;
134 goto done;
135 }
136
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137 pr_debug("CPU[%d] target %d relation %d (%d-%d) selected %d\n",
138 policy->cpu, target_freq, relation,
139 policy->min, policy->max, table[index].frequency);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140
Praveen Chidambaram241ded32013-03-11 14:50:06 -0600141 cpu_work = &per_cpu(cpufreq_work, policy->cpu);
142 cpu_work->policy = policy;
143 cpu_work->frequency = table[index].frequency;
144 cpu_work->status = -ENODEV;
145
146 cpumask_clear(mask);
147 cpumask_set_cpu(policy->cpu, mask);
148 if (cpumask_equal(mask, &current->cpus_allowed)) {
149 ret = set_cpu_freq(cpu_work->policy, cpu_work->frequency);
150 goto done;
151 } else {
152 cancel_work_sync(&cpu_work->work);
153 INIT_COMPLETION(cpu_work->complete);
154 queue_work_on(policy->cpu, msm_cpufreq_wq, &cpu_work->work);
155 wait_for_completion(&cpu_work->complete);
156 }
157
158 ret = cpu_work->status;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159
160done:
Praveen Chidambaram241ded32013-03-11 14:50:06 -0600161 free_cpumask_var(mask);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700162 mutex_unlock(&per_cpu(cpufreq_suspend, policy->cpu).suspend_mutex);
163 return ret;
164}
165
166static int msm_cpufreq_verify(struct cpufreq_policy *policy)
167{
168 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
169 policy->cpuinfo.max_freq);
170 return 0;
171}
172
Praveen Chidambaram696a5612012-05-25 17:29:11 -0600173static unsigned int msm_cpufreq_get_freq(unsigned int cpu)
174{
175 return acpuclk_get_rate(cpu);
176}
177
178static inline int msm_cpufreq_limits_init(void)
179{
180 int cpu = 0;
181 int i = 0;
182 struct cpufreq_frequency_table *table = NULL;
183 uint32_t min = (uint32_t) -1;
184 uint32_t max = 0;
185 struct cpu_freq *limit = NULL;
186
187 for_each_possible_cpu(cpu) {
188 limit = &per_cpu(cpu_freq_info, cpu);
189 table = cpufreq_frequency_get_table(cpu);
190 if (table == NULL) {
191 pr_err("%s: error reading cpufreq table for cpu %d\n",
192 __func__, cpu);
193 continue;
194 }
195 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
196 if (table[i].frequency > max)
197 max = table[i].frequency;
198 if (table[i].frequency < min)
199 min = table[i].frequency;
200 }
201 limit->allowed_min = min;
202 limit->allowed_max = max;
203 limit->min = min;
204 limit->max = max;
205 limit->limits_init = 1;
206 }
207
208 return 0;
209}
210
211int msm_cpufreq_set_freq_limits(uint32_t cpu, uint32_t min, uint32_t max)
212{
213 struct cpu_freq *limit = &per_cpu(cpu_freq_info, cpu);
214
215 if (!limit->limits_init)
216 msm_cpufreq_limits_init();
217
218 if ((min != MSM_CPUFREQ_NO_LIMIT) &&
219 min >= limit->min && min <= limit->max)
220 limit->allowed_min = min;
221 else
222 limit->allowed_min = limit->min;
223
224
225 if ((max != MSM_CPUFREQ_NO_LIMIT) &&
226 max <= limit->max && max >= limit->min)
227 limit->allowed_max = max;
228 else
229 limit->allowed_max = limit->max;
230
231 pr_debug("%s: Limiting cpu %d min = %d, max = %d\n",
232 __func__, cpu,
233 limit->allowed_min, limit->allowed_max);
234
235 return 0;
236}
237EXPORT_SYMBOL(msm_cpufreq_set_freq_limits);
238
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239static int __cpuinit msm_cpufreq_init(struct cpufreq_policy *policy)
240{
241 int cur_freq;
242 int index;
243 struct cpufreq_frequency_table *table;
Praveen Chidambaram241ded32013-03-11 14:50:06 -0600244 struct cpufreq_work_struct *cpu_work = NULL;
Pankaj Kumaref7c3942012-04-03 18:00:52 +0530245
246 table = cpufreq_frequency_get_table(policy->cpu);
247 if (table == NULL)
248 return -ENODEV;
Pankaj Kumar0f263682012-01-05 17:01:57 +0530249 /*
250 * In 8625 both cpu core's frequency can not
251 * be changed independently. Each cpu is bound to
252 * same frequency. Hence set the cpumask to all cpu.
253 */
254 if (cpu_is_msm8625())
255 cpumask_setall(policy->cpus);
256
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257 if (cpufreq_frequency_table_cpuinfo(policy, table)) {
258#ifdef CONFIG_MSM_CPU_FREQ_SET_MIN_MAX
259 policy->cpuinfo.min_freq = CONFIG_MSM_CPU_FREQ_MIN;
260 policy->cpuinfo.max_freq = CONFIG_MSM_CPU_FREQ_MAX;
261#endif
262 }
263#ifdef CONFIG_MSM_CPU_FREQ_SET_MIN_MAX
264 policy->min = CONFIG_MSM_CPU_FREQ_MIN;
265 policy->max = CONFIG_MSM_CPU_FREQ_MAX;
266#endif
267
268 cur_freq = acpuclk_get_rate(policy->cpu);
269 if (cpufreq_frequency_table_target(policy, table, cur_freq,
Matt Wagantallb31e4682011-10-12 12:50:27 -0700270 CPUFREQ_RELATION_H, &index) &&
271 cpufreq_frequency_table_target(policy, table, cur_freq,
272 CPUFREQ_RELATION_L, &index)) {
Praveen Chidambaram241ded32013-03-11 14:50:06 -0600273 pr_info("cpufreq: cpu%d at invalid freq: %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274 policy->cpu, cur_freq);
275 return -EINVAL;
276 }
277
278 if (cur_freq != table[index].frequency) {
279 int ret = 0;
280 ret = acpuclk_set_rate(policy->cpu, table[index].frequency,
281 SETRATE_CPUFREQ);
282 if (ret)
283 return ret;
284 pr_info("cpufreq: cpu%d init at %d switching to %d\n",
285 policy->cpu, cur_freq, table[index].frequency);
286 cur_freq = table[index].frequency;
287 }
288
289 policy->cur = cur_freq;
290
291 policy->cpuinfo.transition_latency =
292 acpuclk_get_switch_time() * NSEC_PER_USEC;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293
Praveen Chidambaram241ded32013-03-11 14:50:06 -0600294 cpu_work = &per_cpu(cpufreq_work, policy->cpu);
295 INIT_WORK(&cpu_work->work, set_cpu_work);
296 init_completion(&cpu_work->complete);
297
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 return 0;
299}
300
Praveen Chidambaramaa75efa2012-09-18 13:42:40 -0600301static int __cpuinit msm_cpufreq_cpu_callback(struct notifier_block *nfb,
302 unsigned long action, void *hcpu)
303{
304 unsigned int cpu = (unsigned long)hcpu;
305
306 switch (action) {
307 case CPU_ONLINE:
308 case CPU_ONLINE_FROZEN:
309 per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
310 break;
311 case CPU_DOWN_PREPARE:
312 case CPU_DOWN_PREPARE_FROZEN:
313 mutex_lock(&per_cpu(cpufreq_suspend, cpu).suspend_mutex);
314 per_cpu(cpufreq_suspend, cpu).device_suspended = 1;
315 mutex_unlock(&per_cpu(cpufreq_suspend, cpu).suspend_mutex);
316 break;
317 case CPU_DOWN_FAILED:
318 case CPU_DOWN_FAILED_FROZEN:
319 per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
320 break;
321 }
322
323 return NOTIFY_OK;
324}
325
326static struct notifier_block __refdata msm_cpufreq_cpu_notifier = {
327 .notifier_call = msm_cpufreq_cpu_callback,
328};
329
Anji Jonnalab2408f42012-12-13 14:03:54 +0530330/*
331 * Define suspend/resume for cpufreq_driver. Kernel will call
332 * these during suspend/resume with interrupts disabled. This
333 * helps the suspend/resume variable get's updated before cpufreq
334 * governor tries to change the frequency after coming out of suspend.
335 */
336static int msm_cpufreq_suspend(struct cpufreq_policy *policy)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337{
338 int cpu;
339
340 for_each_possible_cpu(cpu) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341 per_cpu(cpufreq_suspend, cpu).device_suspended = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342 }
343
Anji Jonnalab2408f42012-12-13 14:03:54 +0530344 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700345}
346
Anji Jonnalab2408f42012-12-13 14:03:54 +0530347static int msm_cpufreq_resume(struct cpufreq_policy *policy)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348{
349 int cpu;
350
351 for_each_possible_cpu(cpu) {
352 per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
353 }
354
Anji Jonnalab2408f42012-12-13 14:03:54 +0530355 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356}
357
Stepan Moskovchenko5627bb42011-10-13 16:25:41 -0700358static struct freq_attr *msm_freq_attr[] = {
359 &cpufreq_freq_attr_scaling_available_freqs,
360 NULL,
361};
362
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700363static struct cpufreq_driver msm_cpufreq_driver = {
364 /* lps calculations are handled here. */
365 .flags = CPUFREQ_STICKY | CPUFREQ_CONST_LOOPS,
366 .init = msm_cpufreq_init,
367 .verify = msm_cpufreq_verify,
368 .target = msm_cpufreq_target,
Praveen Chidambaram696a5612012-05-25 17:29:11 -0600369 .get = msm_cpufreq_get_freq,
Anji Jonnalab2408f42012-12-13 14:03:54 +0530370 .suspend = msm_cpufreq_suspend,
371 .resume = msm_cpufreq_resume,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372 .name = "msm",
Stepan Moskovchenko5627bb42011-10-13 16:25:41 -0700373 .attr = msm_freq_attr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700374};
375
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376static int __init msm_cpufreq_register(void)
377{
378 int cpu;
379
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700380 for_each_possible_cpu(cpu) {
381 mutex_init(&(per_cpu(cpufreq_suspend, cpu).suspend_mutex));
382 per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
383 }
384
Praveen Chidambaram241ded32013-03-11 14:50:06 -0600385 msm_cpufreq_wq = create_workqueue("msm-cpufreq");
Narayanan Gopalakrishnan4f5e7132012-07-17 16:07:50 -0700386 register_hotcpu_notifier(&msm_cpufreq_cpu_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388 return cpufreq_register_driver(&msm_cpufreq_driver);
389}
390
391late_initcall(msm_cpufreq_register);