blob: 651287207fbd0498fec121d267fb17b5e0e62389 [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>
22#include <linux/cpufreq.h>
23#include <linux/workqueue.h>
24#include <linux/completion.h>
25#include <linux/cpu.h>
26#include <linux/cpumask.h>
27#include <linux/sched.h>
28#include <linux/suspend.h>
Stepan Moskovchenkoaf25dd92011-08-05 18:12:48 -070029#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030
31#include "acpuclock.h"
32
33#ifdef CONFIG_SMP
34struct cpufreq_work_struct {
35 struct work_struct work;
36 struct cpufreq_policy *policy;
37 struct completion complete;
38 int frequency;
39 int status;
40};
41
42static DEFINE_PER_CPU(struct cpufreq_work_struct, cpufreq_work);
43static struct workqueue_struct *msm_cpufreq_wq;
44#endif
45
46struct 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
53static int set_cpu_freq(struct cpufreq_policy *policy, unsigned int new_freq)
54{
55 int ret = 0;
56 struct cpufreq_freqs freqs;
57
58 freqs.old = policy->cur;
David Ng3f76d272012-02-08 10:43:37 -080059 freqs.new = new_freq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070060 freqs.cpu = policy->cpu;
61 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
62 ret = acpuclk_set_rate(policy->cpu, new_freq, SETRATE_CPUFREQ);
63 if (!ret)
64 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
65
66 return ret;
67}
68
69#ifdef CONFIG_SMP
70static void set_cpu_work(struct work_struct *work)
71{
72 struct cpufreq_work_struct *cpu_work =
73 container_of(work, struct cpufreq_work_struct, work);
74
75 cpu_work->status = set_cpu_freq(cpu_work->policy, cpu_work->frequency);
76 complete(&cpu_work->complete);
77}
78#endif
79
80static int msm_cpufreq_target(struct cpufreq_policy *policy,
81 unsigned int target_freq,
82 unsigned int relation)
83{
84 int ret = -EFAULT;
85 int index;
86 struct cpufreq_frequency_table *table;
87#ifdef CONFIG_SMP
88 struct cpufreq_work_struct *cpu_work = NULL;
89 cpumask_var_t mask;
90
91 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
92 return -ENOMEM;
93
94 if (!cpu_active(policy->cpu)) {
95 pr_info("cpufreq: cpu %d is not active.\n", policy->cpu);
96 return -ENODEV;
97 }
98#endif
99
100 mutex_lock(&per_cpu(cpufreq_suspend, policy->cpu).suspend_mutex);
101
102 if (per_cpu(cpufreq_suspend, policy->cpu).device_suspended) {
103 pr_debug("cpufreq: cpu%d scheduling frequency change "
104 "in suspend.\n", policy->cpu);
105 ret = -EFAULT;
106 goto done;
107 }
108
109 table = cpufreq_frequency_get_table(policy->cpu);
110 if (cpufreq_frequency_table_target(policy, table, target_freq, relation,
111 &index)) {
112 pr_err("cpufreq: invalid target_freq: %d\n", target_freq);
113 ret = -EINVAL;
114 goto done;
115 }
116
117#ifdef CONFIG_CPU_FREQ_DEBUG
118 pr_debug("CPU[%d] target %d relation %d (%d-%d) selected %d\n",
119 policy->cpu, target_freq, relation,
120 policy->min, policy->max, table[index].frequency);
121#endif
122
123#ifdef CONFIG_SMP
124 cpu_work = &per_cpu(cpufreq_work, policy->cpu);
125 cpu_work->policy = policy;
126 cpu_work->frequency = table[index].frequency;
127 cpu_work->status = -ENODEV;
128
129 cpumask_clear(mask);
130 cpumask_set_cpu(policy->cpu, mask);
131 if (cpumask_equal(mask, &current->cpus_allowed)) {
132 ret = set_cpu_freq(cpu_work->policy, cpu_work->frequency);
133 goto done;
134 } else {
135 cancel_work_sync(&cpu_work->work);
136 INIT_COMPLETION(cpu_work->complete);
137 queue_work_on(policy->cpu, msm_cpufreq_wq, &cpu_work->work);
138 wait_for_completion(&cpu_work->complete);
139 }
140
141 free_cpumask_var(mask);
142 ret = cpu_work->status;
143#else
144 ret = set_cpu_freq(policy, table[index].frequency);
145#endif
146
147done:
148 mutex_unlock(&per_cpu(cpufreq_suspend, policy->cpu).suspend_mutex);
149 return ret;
150}
151
152static int msm_cpufreq_verify(struct cpufreq_policy *policy)
153{
154 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
155 policy->cpuinfo.max_freq);
156 return 0;
157}
158
159static int __cpuinit msm_cpufreq_init(struct cpufreq_policy *policy)
160{
161 int cur_freq;
162 int index;
163 struct cpufreq_frequency_table *table;
164#ifdef CONFIG_SMP
165 struct cpufreq_work_struct *cpu_work = NULL;
166#endif
167
Pankaj Kumaref7c3942012-04-03 18:00:52 +0530168
169 table = cpufreq_frequency_get_table(policy->cpu);
170 if (table == NULL)
171 return -ENODEV;
Pankaj Kumar0f263682012-01-05 17:01:57 +0530172 /*
173 * In 8625 both cpu core's frequency can not
174 * be changed independently. Each cpu is bound to
175 * same frequency. Hence set the cpumask to all cpu.
176 */
177 if (cpu_is_msm8625())
178 cpumask_setall(policy->cpus);
179
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180 if (cpufreq_frequency_table_cpuinfo(policy, table)) {
181#ifdef CONFIG_MSM_CPU_FREQ_SET_MIN_MAX
182 policy->cpuinfo.min_freq = CONFIG_MSM_CPU_FREQ_MIN;
183 policy->cpuinfo.max_freq = CONFIG_MSM_CPU_FREQ_MAX;
184#endif
185 }
186#ifdef CONFIG_MSM_CPU_FREQ_SET_MIN_MAX
187 policy->min = CONFIG_MSM_CPU_FREQ_MIN;
188 policy->max = CONFIG_MSM_CPU_FREQ_MAX;
189#endif
190
191 cur_freq = acpuclk_get_rate(policy->cpu);
192 if (cpufreq_frequency_table_target(policy, table, cur_freq,
Matt Wagantallb31e4682011-10-12 12:50:27 -0700193 CPUFREQ_RELATION_H, &index) &&
194 cpufreq_frequency_table_target(policy, table, cur_freq,
195 CPUFREQ_RELATION_L, &index)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196 pr_info("cpufreq: cpu%d at invalid freq: %d\n",
197 policy->cpu, cur_freq);
198 return -EINVAL;
199 }
200
201 if (cur_freq != table[index].frequency) {
202 int ret = 0;
203 ret = acpuclk_set_rate(policy->cpu, table[index].frequency,
204 SETRATE_CPUFREQ);
205 if (ret)
206 return ret;
207 pr_info("cpufreq: cpu%d init at %d switching to %d\n",
208 policy->cpu, cur_freq, table[index].frequency);
209 cur_freq = table[index].frequency;
210 }
211
212 policy->cur = cur_freq;
213
214 policy->cpuinfo.transition_latency =
215 acpuclk_get_switch_time() * NSEC_PER_USEC;
216#ifdef CONFIG_SMP
217 cpu_work = &per_cpu(cpufreq_work, policy->cpu);
218 INIT_WORK(&cpu_work->work, set_cpu_work);
219 init_completion(&cpu_work->complete);
220#endif
221
222 return 0;
223}
224
225static int msm_cpufreq_suspend(void)
226{
227 int cpu;
228
229 for_each_possible_cpu(cpu) {
230 mutex_lock(&per_cpu(cpufreq_suspend, cpu).suspend_mutex);
231 per_cpu(cpufreq_suspend, cpu).device_suspended = 1;
232 mutex_unlock(&per_cpu(cpufreq_suspend, cpu).suspend_mutex);
233 }
234
235 return NOTIFY_DONE;
236}
237
238static int msm_cpufreq_resume(void)
239{
240 int cpu;
241
242 for_each_possible_cpu(cpu) {
243 per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
244 }
245
246 return NOTIFY_DONE;
247}
248
249static int msm_cpufreq_pm_event(struct notifier_block *this,
250 unsigned long event, void *ptr)
251{
252 switch (event) {
253 case PM_POST_HIBERNATION:
254 case PM_POST_SUSPEND:
255 return msm_cpufreq_resume();
256 case PM_HIBERNATION_PREPARE:
257 case PM_SUSPEND_PREPARE:
258 return msm_cpufreq_suspend();
259 default:
260 return NOTIFY_DONE;
261 }
262}
263
Stepan Moskovchenko5627bb42011-10-13 16:25:41 -0700264static struct freq_attr *msm_freq_attr[] = {
265 &cpufreq_freq_attr_scaling_available_freqs,
266 NULL,
267};
268
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269static struct cpufreq_driver msm_cpufreq_driver = {
270 /* lps calculations are handled here. */
271 .flags = CPUFREQ_STICKY | CPUFREQ_CONST_LOOPS,
272 .init = msm_cpufreq_init,
273 .verify = msm_cpufreq_verify,
274 .target = msm_cpufreq_target,
275 .name = "msm",
Stepan Moskovchenko5627bb42011-10-13 16:25:41 -0700276 .attr = msm_freq_attr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700277};
278
279static struct notifier_block msm_cpufreq_pm_notifier = {
280 .notifier_call = msm_cpufreq_pm_event,
281};
282
283static int __init msm_cpufreq_register(void)
284{
285 int cpu;
286
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 for_each_possible_cpu(cpu) {
288 mutex_init(&(per_cpu(cpufreq_suspend, cpu).suspend_mutex));
289 per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
290 }
291
292#ifdef CONFIG_SMP
293 msm_cpufreq_wq = create_workqueue("msm-cpufreq");
294#endif
295
296 register_pm_notifier(&msm_cpufreq_pm_notifier);
297 return cpufreq_register_driver(&msm_cpufreq_driver);
298}
299
300late_initcall(msm_cpufreq_register);
301