aboutsummaryrefslogtreecommitdiff
path: root/drivers/devfreq/governor_msm_cpufreq.c
blob: 9b13e26a3c3e9b595b9cea2bd930b1eb355a115f (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
/*
 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * 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/devfreq.h>
#include <mach/cpufreq.h>
#include "governor.h"

DEFINE_MUTEX(df_lock);
static struct devfreq *df;

static int devfreq_msm_cpufreq_get_freq(struct devfreq *df,
					unsigned long *freq,
					u32 *flag)
{
	*freq = msm_cpufreq_get_bw();
	return 0;
}

int devfreq_msm_cpufreq_update_bw(void)
{
	int ret = 0;

	mutex_lock(&df_lock);
	if (df) {
		mutex_lock(&df->lock);
		ret = update_devfreq(df);
		mutex_unlock(&df->lock);
	}
	mutex_unlock(&df_lock);
	return ret;
}

static int devfreq_msm_cpufreq_ev_handler(struct devfreq *devfreq,
					unsigned int event, void *data)
{
	int ret;

	switch (event) {
	case DEVFREQ_GOV_START:
		mutex_lock(&df_lock);
		df = devfreq;
		mutex_unlock(&df_lock);

		ret = devfreq_msm_cpufreq_update_bw();
		if (ret) {
			pr_err("Unable to update BW! Gov start failed!\n");
			return ret;
		}

		devfreq_monitor_stop(df);
		pr_debug("Enabled MSM CPUfreq governor\n");
		break;

	case DEVFREQ_GOV_STOP:
		mutex_lock(&df_lock);
		df = NULL;
		mutex_unlock(&df_lock);

		pr_debug("Disabled MSM CPUfreq governor\n");
		break;
	}

	return 0;
}

static struct devfreq_governor devfreq_msm_cpufreq = {
	.name = "msm_cpufreq",
	.get_target_freq = devfreq_msm_cpufreq_get_freq,
	.event_handler = devfreq_msm_cpufreq_ev_handler,
};

int register_devfreq_msm_cpufreq(void)
{
	return devfreq_add_governor(&devfreq_msm_cpufreq);
}