aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-exynos/dvfs-hotplug.c
blob: 519e9f315583bf25b4b52553c1625603e31056cb (plain)
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
/* linux/arch/arm/mach-exynos/dvfs-hotplug.c
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com/
 *
 * EXYNOS4 - Integrated DVFS CPU hotplug
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
*/

#include <linux/sched.h>
#include <linux/cpufreq.h>
#include <linux/cpu.h>
#include <linux/err.h>
#include <linux/notifier.h>
#include <linux/reboot.h>
#include <linux/suspend.h>
#include <linux/io.h>

#include <plat/cpu.h>

static unsigned int total_num_target_freq;
static unsigned int consecutv_highestlevel_cnt;
static unsigned int consecutv_lowestlevel_cnt;

static unsigned int freq_max;
static unsigned int freq_in_trg;
static unsigned int freq_min = -1UL;

static unsigned int can_hotplug;

static void exynos4_integrated_dvfs_hotplug(unsigned int freq_old,
					unsigned int freq_new)
{
	total_num_target_freq++;
	freq_in_trg = 800000;

	if ((freq_old >= freq_in_trg) && (freq_new >= freq_in_trg)) {
		if (soc_is_exynos4412()) {
			if (cpu_online(3) == 0) {
				if (consecutv_highestlevel_cnt >= 5) {
					cpu_up(3);
					consecutv_highestlevel_cnt = 0;
				}
			} else if (cpu_online(2) == 0) {
				if (consecutv_highestlevel_cnt >= 5) {
					cpu_up(2);
					consecutv_highestlevel_cnt = 0;
				}
			} else if (cpu_online(1) == 0) {
				if (consecutv_highestlevel_cnt >= 5) {
					cpu_up(1);
					consecutv_highestlevel_cnt = 0;
				}
			}
			consecutv_highestlevel_cnt++;
		} else {
			if (cpu_online(1) == 0) {
				if (consecutv_highestlevel_cnt >= 5) {
					cpu_up(1);
					consecutv_highestlevel_cnt = 0;
				}
			}
			consecutv_highestlevel_cnt++;
		}
	} else if ((freq_old <= freq_min) && (freq_new <= freq_min)) {
		if (soc_is_exynos4412()) {
			if (cpu_online(1) == 1) {
				if (consecutv_lowestlevel_cnt >= 5) {
					cpu_down(1);
					consecutv_lowestlevel_cnt = 0;
				} else
					consecutv_lowestlevel_cnt++;
			} else if (cpu_online(2) == 1) {
				if (consecutv_lowestlevel_cnt >= 5) {
					cpu_down(2);
					consecutv_lowestlevel_cnt = 0;
				} else
					consecutv_lowestlevel_cnt++;
			} else if (cpu_online(3) == 1) {
				if (consecutv_lowestlevel_cnt >= 5) {
					cpu_down(3);
					consecutv_lowestlevel_cnt = 0;
				} else
					consecutv_lowestlevel_cnt++;
			}
		} else {
			if (cpu_online(1) == 1) {
				if (consecutv_lowestlevel_cnt >= 5) {
					cpu_down(1);
					consecutv_lowestlevel_cnt = 0;
				} else
					consecutv_lowestlevel_cnt++;
			}
		}
	} else {
		consecutv_highestlevel_cnt = 0;
		consecutv_lowestlevel_cnt = 0;
	}
}

static int hotplug_cpufreq_transition(struct notifier_block *nb,
					unsigned long val, void *data)
{
	struct cpufreq_freqs *freqs = (struct cpufreq_freqs *)data;

	if ((val == CPUFREQ_POSTCHANGE) && can_hotplug)
		exynos4_integrated_dvfs_hotplug(freqs->old, freqs->new);

	return 0;
}

static struct notifier_block dvfs_hotplug = {
	.notifier_call = hotplug_cpufreq_transition,
};

static int hotplug_pm_transition(struct notifier_block *nb,
					unsigned long val, void *data)
{
	switch (val) {
	case PM_SUSPEND_PREPARE:
		can_hotplug = 0;
		consecutv_highestlevel_cnt = 0;
		consecutv_lowestlevel_cnt = 0;
		break;
	case PM_POST_RESTORE:
	case PM_POST_SUSPEND:
		can_hotplug = 1;
		break;
	}

	return 0;
}

static struct notifier_block pm_hotplug = {
	.notifier_call = hotplug_pm_transition,
};

/*
 * Note : This function should be called after intialization of CPUFreq
 * driver for exynos4. The cpufreq_frequency_table for exynos4 should be
 * established before calling this function.
 */
static int __init exynos4_integrated_dvfs_hotplug_init(void)
{
	int i;
	struct cpufreq_frequency_table *table;
	unsigned int freq;

	total_num_target_freq = 0;
	consecutv_highestlevel_cnt = 0;
	consecutv_lowestlevel_cnt = 0;
	can_hotplug = 1;

	table = cpufreq_frequency_get_table(0);
	if (IS_ERR(table)) {
		printk(KERN_ERR "%s: Check loading cpufreq before\n", __func__);
		return PTR_ERR(table);
	}

	for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
		freq = table[i].frequency;

		if (freq != CPUFREQ_ENTRY_INVALID && freq > freq_max)
			freq_max = freq;
		else if (freq != CPUFREQ_ENTRY_INVALID && freq_min > freq)
			freq_min = freq;
	}

	printk(KERN_INFO "%s, max(%d),min(%d)\n", __func__, freq_max, freq_min);

	register_pm_notifier(&pm_hotplug);

	return cpufreq_register_notifier(&dvfs_hotplug,
					 CPUFREQ_TRANSITION_NOTIFIER);
}

late_initcall(exynos4_integrated_dvfs_hotplug_init);