blob: 9bc8eb39522f7282c7e3bf09fcd430669426d9d3 [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
David Ngc79a2e02011-03-26 06:13:32 -070053static int override_cpu;
54
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055static int set_cpu_freq(struct cpufreq_policy *policy, unsigned int new_freq)
56{
57 int ret = 0;
58 struct cpufreq_freqs freqs;
59
60 freqs.old = policy->cur;
David Ngc79a2e02011-03-26 06:13:32 -070061 if (override_cpu) {
62 if (policy->cur == policy->max)
63 return 0;
64 else
65 freqs.new = policy->max;
66 } else
67 freqs.new = new_freq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068 freqs.cpu = policy->cpu;
69 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
70 ret = acpuclk_set_rate(policy->cpu, new_freq, SETRATE_CPUFREQ);
71 if (!ret)
72 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
73
74 return ret;
75}
76
77#ifdef CONFIG_SMP
78static void set_cpu_work(struct work_struct *work)
79{
80 struct cpufreq_work_struct *cpu_work =
81 container_of(work, struct cpufreq_work_struct, work);
82
83 cpu_work->status = set_cpu_freq(cpu_work->policy, cpu_work->frequency);
84 complete(&cpu_work->complete);
85}
86#endif
87
88static int msm_cpufreq_target(struct cpufreq_policy *policy,
89 unsigned int target_freq,
90 unsigned int relation)
91{
92 int ret = -EFAULT;
93 int index;
94 struct cpufreq_frequency_table *table;
95#ifdef CONFIG_SMP
96 struct cpufreq_work_struct *cpu_work = NULL;
97 cpumask_var_t mask;
98
99 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
100 return -ENOMEM;
101
102 if (!cpu_active(policy->cpu)) {
103 pr_info("cpufreq: cpu %d is not active.\n", policy->cpu);
104 return -ENODEV;
105 }
106#endif
107
108 mutex_lock(&per_cpu(cpufreq_suspend, policy->cpu).suspend_mutex);
109
110 if (per_cpu(cpufreq_suspend, policy->cpu).device_suspended) {
111 pr_debug("cpufreq: cpu%d scheduling frequency change "
112 "in suspend.\n", policy->cpu);
113 ret = -EFAULT;
114 goto done;
115 }
116
117 table = cpufreq_frequency_get_table(policy->cpu);
118 if (cpufreq_frequency_table_target(policy, table, target_freq, relation,
119 &index)) {
120 pr_err("cpufreq: invalid target_freq: %d\n", target_freq);
121 ret = -EINVAL;
122 goto done;
123 }
124
125#ifdef CONFIG_CPU_FREQ_DEBUG
126 pr_debug("CPU[%d] target %d relation %d (%d-%d) selected %d\n",
127 policy->cpu, target_freq, relation,
128 policy->min, policy->max, table[index].frequency);
129#endif
130
131#ifdef CONFIG_SMP
132 cpu_work = &per_cpu(cpufreq_work, policy->cpu);
133 cpu_work->policy = policy;
134 cpu_work->frequency = table[index].frequency;
135 cpu_work->status = -ENODEV;
136
137 cpumask_clear(mask);
138 cpumask_set_cpu(policy->cpu, mask);
139 if (cpumask_equal(mask, &current->cpus_allowed)) {
140 ret = set_cpu_freq(cpu_work->policy, cpu_work->frequency);
141 goto done;
142 } else {
143 cancel_work_sync(&cpu_work->work);
144 INIT_COMPLETION(cpu_work->complete);
145 queue_work_on(policy->cpu, msm_cpufreq_wq, &cpu_work->work);
146 wait_for_completion(&cpu_work->complete);
147 }
148
149 free_cpumask_var(mask);
150 ret = cpu_work->status;
151#else
152 ret = set_cpu_freq(policy, table[index].frequency);
153#endif
154
155done:
156 mutex_unlock(&per_cpu(cpufreq_suspend, policy->cpu).suspend_mutex);
157 return ret;
158}
159
160static int msm_cpufreq_verify(struct cpufreq_policy *policy)
161{
162 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
163 policy->cpuinfo.max_freq);
164 return 0;
165}
166
167static int __cpuinit msm_cpufreq_init(struct cpufreq_policy *policy)
168{
169 int cur_freq;
170 int index;
171 struct cpufreq_frequency_table *table;
172#ifdef CONFIG_SMP
173 struct cpufreq_work_struct *cpu_work = NULL;
174#endif
175
176 table = cpufreq_frequency_get_table(policy->cpu);
177 if (cpufreq_frequency_table_cpuinfo(policy, table)) {
178#ifdef CONFIG_MSM_CPU_FREQ_SET_MIN_MAX
179 policy->cpuinfo.min_freq = CONFIG_MSM_CPU_FREQ_MIN;
180 policy->cpuinfo.max_freq = CONFIG_MSM_CPU_FREQ_MAX;
181#endif
182 }
183#ifdef CONFIG_MSM_CPU_FREQ_SET_MIN_MAX
184 policy->min = CONFIG_MSM_CPU_FREQ_MIN;
185 policy->max = CONFIG_MSM_CPU_FREQ_MAX;
186#endif
187
188 cur_freq = acpuclk_get_rate(policy->cpu);
189 if (cpufreq_frequency_table_target(policy, table, cur_freq,
Matt Wagantallb31e4682011-10-12 12:50:27 -0700190 CPUFREQ_RELATION_H, &index) &&
191 cpufreq_frequency_table_target(policy, table, cur_freq,
192 CPUFREQ_RELATION_L, &index)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193 pr_info("cpufreq: cpu%d at invalid freq: %d\n",
194 policy->cpu, cur_freq);
195 return -EINVAL;
196 }
197
198 if (cur_freq != table[index].frequency) {
199 int ret = 0;
200 ret = acpuclk_set_rate(policy->cpu, table[index].frequency,
201 SETRATE_CPUFREQ);
202 if (ret)
203 return ret;
204 pr_info("cpufreq: cpu%d init at %d switching to %d\n",
205 policy->cpu, cur_freq, table[index].frequency);
206 cur_freq = table[index].frequency;
207 }
208
209 policy->cur = cur_freq;
210
211 policy->cpuinfo.transition_latency =
212 acpuclk_get_switch_time() * NSEC_PER_USEC;
213#ifdef CONFIG_SMP
214 cpu_work = &per_cpu(cpufreq_work, policy->cpu);
215 INIT_WORK(&cpu_work->work, set_cpu_work);
216 init_completion(&cpu_work->complete);
217#endif
218
219 return 0;
220}
221
222static int msm_cpufreq_suspend(void)
223{
224 int cpu;
225
226 for_each_possible_cpu(cpu) {
227 mutex_lock(&per_cpu(cpufreq_suspend, cpu).suspend_mutex);
228 per_cpu(cpufreq_suspend, cpu).device_suspended = 1;
229 mutex_unlock(&per_cpu(cpufreq_suspend, cpu).suspend_mutex);
230 }
231
232 return NOTIFY_DONE;
233}
234
235static int msm_cpufreq_resume(void)
236{
237 int cpu;
238
239 for_each_possible_cpu(cpu) {
240 per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
241 }
242
243 return NOTIFY_DONE;
244}
245
246static int msm_cpufreq_pm_event(struct notifier_block *this,
247 unsigned long event, void *ptr)
248{
249 switch (event) {
250 case PM_POST_HIBERNATION:
251 case PM_POST_SUSPEND:
252 return msm_cpufreq_resume();
253 case PM_HIBERNATION_PREPARE:
254 case PM_SUSPEND_PREPARE:
255 return msm_cpufreq_suspend();
256 default:
257 return NOTIFY_DONE;
258 }
259}
260
David Ngc79a2e02011-03-26 06:13:32 -0700261static ssize_t store_mfreq(struct sysdev_class *class,
262 struct sysdev_class_attribute *attr,
263 const char *buf, size_t count)
264{
265 u64 val;
266
267 if (strict_strtoull(buf, 0, &val) < 0) {
268 pr_err("Invalid parameter to mfreq\n");
269 return 0;
270 }
271 if (val)
272 override_cpu = 1;
273 else
274 override_cpu = 0;
275 return count;
276}
277
278static SYSDEV_CLASS_ATTR(mfreq, 0200, NULL, store_mfreq);
279
Stepan Moskovchenko5627bb42011-10-13 16:25:41 -0700280static struct freq_attr *msm_freq_attr[] = {
281 &cpufreq_freq_attr_scaling_available_freqs,
282 NULL,
283};
284
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700285static struct cpufreq_driver msm_cpufreq_driver = {
286 /* lps calculations are handled here. */
287 .flags = CPUFREQ_STICKY | CPUFREQ_CONST_LOOPS,
288 .init = msm_cpufreq_init,
289 .verify = msm_cpufreq_verify,
290 .target = msm_cpufreq_target,
291 .name = "msm",
Stepan Moskovchenko5627bb42011-10-13 16:25:41 -0700292 .attr = msm_freq_attr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293};
294
295static struct notifier_block msm_cpufreq_pm_notifier = {
296 .notifier_call = msm_cpufreq_pm_event,
297};
298
299static int __init msm_cpufreq_register(void)
300{
301 int cpu;
302
David Ngc79a2e02011-03-26 06:13:32 -0700303 int err = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
304 &attr_mfreq.attr);
305 if (err)
306 pr_err("Failed to create sysfs mfreq\n");
307
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308 for_each_possible_cpu(cpu) {
309 mutex_init(&(per_cpu(cpufreq_suspend, cpu).suspend_mutex));
310 per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
311 }
312
313#ifdef CONFIG_SMP
314 msm_cpufreq_wq = create_workqueue("msm-cpufreq");
315#endif
316
317 register_pm_notifier(&msm_cpufreq_pm_notifier);
318 return cpufreq_register_driver(&msm_cpufreq_driver);
319}
320
321late_initcall(msm_cpufreq_register);
322