blob: 247722148c458e559600162f2d42474d432ae76f [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.
6 * Copyright (c) 2007-2010, Code Aurora Forum. All rights reserved.
7 * 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
Stepan Moskovchenkoaf25dd92011-08-05 18:12:48 -0700176 if (cpu_is_apq8064())
177 return -ENODEV;
178
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179 table = cpufreq_frequency_get_table(policy->cpu);
180 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
David Ngc79a2e02011-03-26 06:13:32 -0700264static ssize_t store_mfreq(struct sysdev_class *class,
265 struct sysdev_class_attribute *attr,
266 const char *buf, size_t count)
267{
268 u64 val;
269
270 if (strict_strtoull(buf, 0, &val) < 0) {
271 pr_err("Invalid parameter to mfreq\n");
272 return 0;
273 }
274 if (val)
275 override_cpu = 1;
276 else
277 override_cpu = 0;
278 return count;
279}
280
281static SYSDEV_CLASS_ATTR(mfreq, 0200, NULL, store_mfreq);
282
Stepan Moskovchenko5627bb42011-10-13 16:25:41 -0700283static struct freq_attr *msm_freq_attr[] = {
284 &cpufreq_freq_attr_scaling_available_freqs,
285 NULL,
286};
287
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288static struct cpufreq_driver msm_cpufreq_driver = {
289 /* lps calculations are handled here. */
290 .flags = CPUFREQ_STICKY | CPUFREQ_CONST_LOOPS,
291 .init = msm_cpufreq_init,
292 .verify = msm_cpufreq_verify,
293 .target = msm_cpufreq_target,
294 .name = "msm",
Stepan Moskovchenko5627bb42011-10-13 16:25:41 -0700295 .attr = msm_freq_attr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296};
297
298static struct notifier_block msm_cpufreq_pm_notifier = {
299 .notifier_call = msm_cpufreq_pm_event,
300};
301
302static int __init msm_cpufreq_register(void)
303{
304 int cpu;
305
David Ngc79a2e02011-03-26 06:13:32 -0700306 int err = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
307 &attr_mfreq.attr);
308 if (err)
309 pr_err("Failed to create sysfs mfreq\n");
310
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311 for_each_possible_cpu(cpu) {
312 mutex_init(&(per_cpu(cpufreq_suspend, cpu).suspend_mutex));
313 per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
314 }
315
316#ifdef CONFIG_SMP
317 msm_cpufreq_wq = create_workqueue("msm-cpufreq");
318#endif
319
320 register_pm_notifier(&msm_cpufreq_pm_notifier);
321 return cpufreq_register_driver(&msm_cpufreq_driver);
322}
323
324late_initcall(msm_cpufreq_register);
325