summaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-mx2/cpufreq_imx27.c
blob: db6ad6b757729da4eef5f54bce534e46dac91f16 (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
/*
 *  linux/arch/arm/mach-mx2/cpufreq.c
 *
 *  Copyright (C) 2009 Sascha Hauer, Pengutronix
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cpufreq.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <mach/hardware.h>

static struct cpufreq_frequency_table mx27_freq_table[4 + 1];

static struct clk *clk_cpu, *clk_mpll;
static unsigned long mpll_rate;

static int mx27_verify_policy(struct cpufreq_policy *policy)
{	return cpufreq_frequency_table_verify(policy, mx27_freq_table);
}

static unsigned int mx27_cpufreq_get(unsigned int cpu)
{
	return clk_get_rate(clk_cpu) / 1000;
}

static unsigned char cpu_dividers[] = {2, 3, 6};

static int mx27_set_target(struct cpufreq_policy *policy,
			  unsigned int target_freq,
			  unsigned int relation)
{
	struct cpufreq_freqs freqs;
	unsigned int idx;

	/* Lookup the next frequency */
	if (cpufreq_frequency_table_target(policy, mx27_freq_table,
					   target_freq, relation, &idx))
		return -EINVAL;

	freqs.old = policy->cur;
	freqs.new = mx27_freq_table[idx].frequency;
	freqs.cpu = policy->cpu;

	/*
	 * Tell everyone what we're about to do...
	 * you should add a notify client with any platform specific
	 * Vcc changing capability
	 */
	cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);

	clk_set_rate(clk_cpu, mpll_rate * 2 / cpu_dividers[idx]);

	/*
	 * Tell everyone what we've just done...
	 * you should add a notify client with any platform specific
	 * SDRAM refresh timer adjustments
	 */
	cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);

	return 0;
}

static __init int mx27_cpufreq_init(struct cpufreq_policy *policy)
{
	int ret, i;

	clk_cpu = clk_get(NULL, "cpu");
	if (IS_ERR(clk_cpu))
		return PTR_ERR(clk_cpu);

	clk_mpll = clk_get(NULL, "mpll");
	if (IS_ERR(clk_mpll)) {
		ret = PTR_ERR(clk_mpll);
		goto out;
	}

	mpll_rate = clk_get_rate(clk_mpll);

	/* set default policy and cpuinfo */
	policy->cpuinfo.transition_latency = 10;
	policy->cur = clk_get_rate(clk_cpu) / 1000;
	policy->min = mpll_rate / 3;
	policy->max = mpll_rate;

	for (i = 0; i < ARRAY_SIZE(cpu_dividers); i++) {
		mx27_freq_table[i].index = i;
		mx27_freq_table[i].frequency = mpll_rate * 2 / cpu_dividers[i] / 1000;
	}
	mx27_freq_table[i].frequency = CPUFREQ_TABLE_END;

	cpufreq_frequency_table_cpuinfo(policy, mx27_freq_table);

	printk(KERN_INFO "i.MX27 CPU frequency change support initialized %d %d\n",
			policy->min, policy->max);

	return 0;
out:
	clk_put(clk_cpu);

	return ret;
}

static struct cpufreq_driver mx27_cpufreq_driver = {
	.verify	= mx27_verify_policy,
	.target	= mx27_set_target,
	.init	= mx27_cpufreq_init,
	.get	= mx27_cpufreq_get,
	.name	= "i.MX27",
};

static int __init mx27_cpu_init(void)
{
	if (!cpu_is_mx27())
		return -ENODEV;

	return cpufreq_register_driver(&mx27_cpufreq_driver);
}

static void __exit mx27_cpu_exit(void)
{
	cpufreq_unregister_driver(&mx27_cpufreq_driver);
}


MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
MODULE_DESCRIPTION("CPU frequency changing driver for the i.MX27");
MODULE_LICENSE("GPL");
module_init(mx27_cpu_init);
module_exit(mx27_cpu_exit);