1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
/* arch/arm/mach-msm/cpufreq.c
*
* MSM architecture cpufreq driver
*
* Copyright (C) 2007 Google, Inc.
* Copyright (c) 2007-2012, The Linux Foundation. All rights reserved.
* Author: Mike A. Chan <mikechan@google.com>
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/earlysuspend.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cpufreq.h>
#include <linux/workqueue.h>
#include <linux/completion.h>
#include <linux/cpu.h>
#include <linux/cpumask.h>
#include <linux/sched.h>
#include <linux/suspend.h>
#include <mach/socinfo.h>
#include <mach/cpufreq.h>
#include "acpuclock.h"
struct cpufreq_work_struct {
struct work_struct work;
struct cpufreq_policy *policy;
struct completion complete;
int frequency;
int status;
};
static DEFINE_PER_CPU(struct cpufreq_work_struct, cpufreq_work);
static struct workqueue_struct *msm_cpufreq_wq;
/* maxscroff */
uint32_t maxscroff_freq = 1026000;
uint32_t maxscroff = 1;
/* ex max freq */
uint32_t ex_max_freq;
struct cpufreq_suspend_t {
struct mutex suspend_mutex;
int device_suspended;
};
static DEFINE_PER_CPU(struct cpufreq_suspend_t, cpufreq_suspend);
struct cpu_freq {
uint32_t max;
uint32_t min;
uint32_t allowed_max;
uint32_t allowed_min;
uint32_t limits_init;
};
static DEFINE_PER_CPU(struct cpu_freq, cpu_freq_info);
/**maxscroff**/
static int __init cpufreq_read_arg_maxscroff(char *max_so)
{
if (strcmp(max_so, "0") == 0) {
maxscroff = 0;
} else if (strcmp(max_so, "1") == 0) {
maxscroff = 1;
} else {
maxscroff = 0;
}
return 1;
}
__setup("max_so=", cpufreq_read_arg_maxscroff);
/**end maxscroff**/
static int set_cpu_freq(struct cpufreq_policy *policy, unsigned int new_freq)
{
int ret = 0;
int saved_sched_policy = -EINVAL;
int saved_sched_rt_prio = -EINVAL;
struct cpufreq_freqs freqs;
struct cpu_freq *limit = &per_cpu(cpu_freq_info, policy->cpu);
struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
if (limit->limits_init) {
if (new_freq > limit->allowed_max) {
new_freq = limit->allowed_max;
pr_debug("max: limiting freq to %d\n", new_freq);
}
if (new_freq < limit->allowed_min) {
new_freq = limit->allowed_min;
pr_debug("min: limiting freq to %d\n", new_freq);
}
}
freqs.old = policy->cur;
freqs.new = new_freq;
freqs.cpu = policy->cpu;
/*
* Put the caller into SCHED_FIFO priority to avoid cpu starvation
* in the acpuclk_set_rate path while increasing frequencies
*/
if (freqs.new > freqs.old && current->policy != SCHED_FIFO) {
saved_sched_policy = current->policy;
saved_sched_rt_prio = current->rt_priority;
sched_setscheduler_nocheck(current, SCHED_FIFO, ¶m);
}
cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
ret = acpuclk_set_rate(policy->cpu, new_freq, SETRATE_CPUFREQ);
if (!ret)
cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
/* Restore priority after clock ramp-up */
if (freqs.new > freqs.old && saved_sched_policy >= 0) {
param.sched_priority = saved_sched_rt_prio;
sched_setscheduler_nocheck(current, saved_sched_policy, ¶m);
}
return ret;
}
static void set_cpu_work(struct work_struct *work)
{
struct cpufreq_work_struct *cpu_work =
container_of(work, struct cpufreq_work_struct, work);
cpu_work->status = set_cpu_freq(cpu_work->policy, cpu_work->frequency);
complete(&cpu_work->complete);
}
static int msm_cpufreq_target(struct cpufreq_policy *policy,
unsigned int target_freq,
unsigned int relation)
{
int ret = 0;
int index;
struct cpufreq_frequency_table *table;
struct cpufreq_work_struct *cpu_work = NULL;
cpumask_var_t mask;
if (!cpu_active(policy->cpu)) {
pr_info("cpufreq: cpu %d is not active.\n", policy->cpu);
return -ENODEV;
}
if (!alloc_cpumask_var(&mask, GFP_KERNEL))
return -ENOMEM;
mutex_lock(&per_cpu(cpufreq_suspend, policy->cpu).suspend_mutex);
if (target_freq == policy->cur)
goto done;
if (per_cpu(cpufreq_suspend, policy->cpu).device_suspended) {
pr_debug("cpufreq: cpu%d scheduling frequency change "
"in suspend.\n", policy->cpu);
ret = -EFAULT;
goto done;
}
table = cpufreq_frequency_get_table(policy->cpu);
if (cpufreq_frequency_table_target(policy, table, target_freq, relation,
&index)) {
pr_err("cpufreq: invalid target_freq: %d\n", target_freq);
ret = -EINVAL;
goto done;
}
pr_debug("CPU[%d] target %d relation %d (%d-%d) selected %d\n",
policy->cpu, target_freq, relation,
policy->min, policy->max, table[index].frequency);
cpu_work = &per_cpu(cpufreq_work, policy->cpu);
cpu_work->policy = policy;
cpu_work->frequency = table[index].frequency;
cpu_work->status = -ENODEV;
cpumask_clear(mask);
cpumask_set_cpu(policy->cpu, mask);
if (cpumask_equal(mask, ¤t->cpus_allowed)) {
ret = set_cpu_freq(cpu_work->policy, cpu_work->frequency);
goto done;
} else {
cancel_work_sync(&cpu_work->work);
INIT_COMPLETION(cpu_work->complete);
queue_work_on(policy->cpu, msm_cpufreq_wq, &cpu_work->work);
wait_for_completion(&cpu_work->complete);
}
ret = cpu_work->status;
done:
free_cpumask_var(mask);
mutex_unlock(&per_cpu(cpufreq_suspend, policy->cpu).suspend_mutex);
return ret;
}
static int msm_cpufreq_verify(struct cpufreq_policy *policy)
{
cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
policy->cpuinfo.max_freq);
return 0;
}
static unsigned int msm_cpufreq_get_freq(unsigned int cpu)
{
return acpuclk_get_rate(cpu);
}
static inline int msm_cpufreq_limits_init(void)
{
int cpu = 0;
int i = 0;
struct cpufreq_frequency_table *table = NULL;
uint32_t min = (uint32_t) -1;
uint32_t max = 0;
struct cpu_freq *limit = NULL;
for_each_possible_cpu(cpu) {
limit = &per_cpu(cpu_freq_info, cpu);
table = cpufreq_frequency_get_table(cpu);
if (table == NULL) {
pr_err("%s: error reading cpufreq table for cpu %d\n",
__func__, cpu);
continue;
}
for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
if (table[i].frequency > max)
max = table[i].frequency;
if (table[i].frequency < min)
min = table[i].frequency;
}
limit->allowed_min = min;
limit->allowed_max = max;
limit->min = min;
limit->max = max;
limit->limits_init = 1;
}
return 0;
}
int msm_cpufreq_set_freq_limits(uint32_t cpu, uint32_t min, uint32_t max)
{
struct cpu_freq *limit = &per_cpu(cpu_freq_info, cpu);
if (!limit->limits_init)
msm_cpufreq_limits_init();
if ((min != MSM_CPUFREQ_NO_LIMIT) &&
min >= limit->min && min <= limit->max)
limit->allowed_min = min;
else
limit->allowed_min = limit->min;
if ((max != MSM_CPUFREQ_NO_LIMIT) &&
max <= limit->max && max >= limit->min)
limit->allowed_max = max;
else
limit->allowed_max = limit->max;
pr_debug("%s: Limiting cpu %d min = %d, max = %d\n",
__func__, cpu,
limit->allowed_min, limit->allowed_max);
return 0;
}
EXPORT_SYMBOL(msm_cpufreq_set_freq_limits);
static int __cpuinit msm_cpufreq_init(struct cpufreq_policy *policy)
{
int cur_freq;
int index;
struct cpufreq_frequency_table *table;
struct cpufreq_work_struct *cpu_work = NULL;
table = cpufreq_frequency_get_table(policy->cpu);
if (table == NULL)
return -ENODEV;
/*
* In 8625 both cpu core's frequency can not
* be changed independently. Each cpu is bound to
* same frequency. Hence set the cpumask to all cpu.
*/
if (cpu_is_msm8625())
cpumask_setall(policy->cpus);
if (cpufreq_frequency_table_cpuinfo(policy, table)) {
#ifdef CONFIG_MSM_CPU_FREQ_SET_MIN_MAX
policy->cpuinfo.min_freq = CONFIG_MSM_CPU_FREQ_MIN;
policy->cpuinfo.max_freq = CONFIG_MSM_CPU_FREQ_MAX;
#endif
}
#ifdef CONFIG_MSM_CPU_FREQ_SET_MIN_MAX
policy->min = CONFIG_MSM_CPU_FREQ_MIN;
policy->max = CONFIG_MSM_CPU_FREQ_MAX;
#endif
cur_freq = acpuclk_get_rate(policy->cpu);
if (cpufreq_frequency_table_target(policy, table, cur_freq,
CPUFREQ_RELATION_H, &index) &&
cpufreq_frequency_table_target(policy, table, cur_freq,
CPUFREQ_RELATION_L, &index)) {
pr_info("cpufreq: cpu%d at invalid freq: %d\n",
policy->cpu, cur_freq);
return -EINVAL;
}
if (cur_freq != table[index].frequency) {
int ret = 0;
ret = acpuclk_set_rate(policy->cpu, table[index].frequency,
SETRATE_CPUFREQ);
if (ret)
return ret;
pr_info("cpufreq: cpu%d init at %d switching to %d\n",
policy->cpu, cur_freq, table[index].frequency);
cur_freq = table[index].frequency;
}
policy->cur = cur_freq;
policy->cpuinfo.transition_latency =
acpuclk_get_switch_time() * NSEC_PER_USEC;
cpu_work = &per_cpu(cpufreq_work, policy->cpu);
INIT_WORK(&cpu_work->work, set_cpu_work);
init_completion(&cpu_work->complete);
return 0;
}
static int __cpuinit msm_cpufreq_cpu_callback(struct notifier_block *nfb,
unsigned long action, void *hcpu)
{
unsigned int cpu = (unsigned long)hcpu;
switch (action) {
case CPU_ONLINE:
case CPU_ONLINE_FROZEN:
per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
break;
case CPU_DOWN_PREPARE:
case CPU_DOWN_PREPARE_FROZEN:
mutex_lock(&per_cpu(cpufreq_suspend, cpu).suspend_mutex);
per_cpu(cpufreq_suspend, cpu).device_suspended = 1;
mutex_unlock(&per_cpu(cpufreq_suspend, cpu).suspend_mutex);
break;
case CPU_DOWN_FAILED:
case CPU_DOWN_FAILED_FROZEN:
per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
break;
}
return NOTIFY_OK;
}
static struct notifier_block __refdata msm_cpufreq_cpu_notifier = {
.notifier_call = msm_cpufreq_cpu_callback,
};
/*
* Define suspend/resume for cpufreq_driver. Kernel will call
* these during suspend/resume with interrupts disabled. This
* helps the suspend/resume variable get's updated before cpufreq
* governor tries to change the frequency after coming out of suspend.
*/
static int msm_cpufreq_suspend(struct cpufreq_policy *policy)
{
int cpu;
for_each_possible_cpu(cpu) {
per_cpu(cpufreq_suspend, cpu).device_suspended = 1;
}
return 0;
}
static int msm_cpufreq_resume(struct cpufreq_policy *policy)
{
int cpu;
for_each_possible_cpu(cpu) {
per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
}
return 0;
}
/** max freq interface **/
static ssize_t show_ex_max_freq(struct cpufreq_policy *policy, char *buf)
{
if (!ex_max_freq)
ex_max_freq = policy->cpuinfo.max_freq;
return sprintf(buf, "%u\n", ex_max_freq);
}
static ssize_t store_ex_max_freq(struct cpufreq_policy *policy,
const char *buf, size_t count)
{
unsigned int freq = 0;
int ret, cpu;
int index;
struct cpufreq_frequency_table *freq_table = cpufreq_frequency_get_table(policy->cpu);
if (!freq_table)
return -EINVAL;
ret = sscanf(buf, "%u", &freq);
if (ret != 1)
return -EINVAL;
mutex_lock(&per_cpu(cpufreq_suspend, policy->cpu).suspend_mutex);
ret = cpufreq_frequency_table_target(policy, freq_table, freq,
CPUFREQ_RELATION_H, &index);
if (ret)
goto out;
ex_max_freq = freq_table[index].frequency;
for_each_possible_cpu(cpu) {
msm_cpufreq_set_freq_limits(cpu, MSM_CPUFREQ_NO_LIMIT, ex_max_freq);
}
cpufreq_update_policy(cpu);
ret = count;
out:
mutex_unlock(&per_cpu(cpufreq_suspend, policy->cpu).suspend_mutex);
return ret;
}
struct freq_attr msm_cpufreq_attr_ex_max_freq = {
.attr = { .name = "ex_max_freq",
.mode = 0666,
},
.show = show_ex_max_freq,
.store = store_ex_max_freq,
};
/** end max freq interface **/
/** maxscreen off sysfs interface **/
static ssize_t show_max_screen_off_khz(struct cpufreq_policy *policy, char *buf)
{
return sprintf(buf, "%u\n", maxscroff_freq);
}
static ssize_t store_max_screen_off_khz(struct cpufreq_policy *policy,
const char *buf, size_t count)
{
unsigned int freq = 0;
int ret;
int index;
struct cpufreq_frequency_table *freq_table = cpufreq_frequency_get_table(policy->cpu);
if (!freq_table)
return -EINVAL;
ret = sscanf(buf, "%u", &freq);
if (ret != 1)
return -EINVAL;
mutex_lock(&per_cpu(cpufreq_suspend, policy->cpu).suspend_mutex);
ret = cpufreq_frequency_table_target(policy, freq_table, freq,
CPUFREQ_RELATION_H, &index);
if (ret)
goto out;
maxscroff_freq = freq_table[index].frequency;
ret = count;
out:
mutex_unlock(&per_cpu(cpufreq_suspend, policy->cpu).suspend_mutex);
return ret;
}
struct freq_attr msm_cpufreq_attr_max_screen_off_khz = {
.attr = { .name = "screen_off_max_freq",
.mode = 0666,
},
.show = show_max_screen_off_khz,
.store = store_max_screen_off_khz,
};
static ssize_t show_max_screen_off(struct cpufreq_policy *policy, char *buf)
{
return sprintf(buf, "%u\n", maxscroff);
}
static ssize_t store_max_screen_off(struct cpufreq_policy *policy,
const char *buf, size_t count)
{
sscanf(buf, "%d ", &maxscroff);
if (maxscroff < 0 || maxscroff > 1)
maxscroff = 0;
return count;
}
struct freq_attr msm_cpufreq_attr_max_screen_off = {
.attr = { .name = "screen_off_max",
.mode = 0666,
},
.show = show_max_screen_off,
.store = store_max_screen_off,
};
/** end maxscreen off sysfs interface **/
static struct freq_attr *msm_freq_attr[] = {
&cpufreq_freq_attr_scaling_available_freqs,
/** maxscreen off sysfs interface **/
&msm_cpufreq_attr_max_screen_off_khz,
&msm_cpufreq_attr_max_screen_off,
&msm_cpufreq_attr_ex_max_freq,
NULL,
};
static struct cpufreq_driver msm_cpufreq_driver = {
/* lps calculations are handled here. */
.flags = CPUFREQ_STICKY | CPUFREQ_CONST_LOOPS,
.init = msm_cpufreq_init,
.verify = msm_cpufreq_verify,
.target = msm_cpufreq_target,
.get = msm_cpufreq_get_freq,
.suspend = msm_cpufreq_suspend,
.resume = msm_cpufreq_resume,
.name = "msm",
.attr = msm_freq_attr,
};
static int __init msm_cpufreq_register(void)
{
int cpu;
for_each_possible_cpu(cpu) {
mutex_init(&(per_cpu(cpufreq_suspend, cpu).suspend_mutex));
per_cpu(cpufreq_suspend, cpu).device_suspended = 0;
}
msm_cpufreq_wq = alloc_workqueue("msm-cpufreq", WQ_HIGHPRI, 0);
register_hotcpu_notifier(&msm_cpufreq_cpu_notifier);
return cpufreq_register_driver(&msm_cpufreq_driver);
}
late_initcall(msm_cpufreq_register);
|