Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/lrg/voltage-2.6
[linux-2.6/mini2440.git] / drivers / cpufreq / freq_table.c
bloba9bd3a05a684548cc7f36aa34fe8a1057d2bfbb3
1 /*
2 * linux/drivers/cpufreq/freq_table.c
4 * Copyright (C) 2002 - 2003 Dominik Brodowski
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/cpufreq.h>
17 #define dprintk(msg...) \
18 cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "freq-table", msg)
20 /*********************************************************************
21 * FREQUENCY TABLE HELPERS *
22 *********************************************************************/
24 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
25 struct cpufreq_frequency_table *table)
27 unsigned int min_freq = ~0;
28 unsigned int max_freq = 0;
29 unsigned int i;
31 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
32 unsigned int freq = table[i].frequency;
33 if (freq == CPUFREQ_ENTRY_INVALID) {
34 dprintk("table entry %u is invalid, skipping\n", i);
36 continue;
38 dprintk("table entry %u: %u kHz, %u index\n",
39 i, freq, table[i].index);
40 if (freq < min_freq)
41 min_freq = freq;
42 if (freq > max_freq)
43 max_freq = freq;
46 policy->min = policy->cpuinfo.min_freq = min_freq;
47 policy->max = policy->cpuinfo.max_freq = max_freq;
49 if (policy->min == ~0)
50 return -EINVAL;
51 else
52 return 0;
54 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);
57 int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
58 struct cpufreq_frequency_table *table)
60 unsigned int next_larger = ~0;
61 unsigned int i;
62 unsigned int count = 0;
64 dprintk("request for verification of policy (%u - %u kHz) for cpu %u\n",
65 policy->min, policy->max, policy->cpu);
67 if (!cpu_online(policy->cpu))
68 return -EINVAL;
70 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
71 policy->cpuinfo.max_freq);
73 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
74 unsigned int freq = table[i].frequency;
75 if (freq == CPUFREQ_ENTRY_INVALID)
76 continue;
77 if ((freq >= policy->min) && (freq <= policy->max))
78 count++;
79 else if ((next_larger > freq) && (freq > policy->max))
80 next_larger = freq;
83 if (!count)
84 policy->max = next_larger;
86 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
87 policy->cpuinfo.max_freq);
89 dprintk("verification lead to (%u - %u kHz) for cpu %u\n",
90 policy->min, policy->max, policy->cpu);
92 return 0;
94 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
97 int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
98 struct cpufreq_frequency_table *table,
99 unsigned int target_freq,
100 unsigned int relation,
101 unsigned int *index)
103 struct cpufreq_frequency_table optimal = {
104 .index = ~0,
105 .frequency = 0,
107 struct cpufreq_frequency_table suboptimal = {
108 .index = ~0,
109 .frequency = 0,
111 unsigned int i;
113 dprintk("request for target %u kHz (relation: %u) for cpu %u\n",
114 target_freq, relation, policy->cpu);
116 switch (relation) {
117 case CPUFREQ_RELATION_H:
118 suboptimal.frequency = ~0;
119 break;
120 case CPUFREQ_RELATION_L:
121 optimal.frequency = ~0;
122 break;
125 if (!cpu_online(policy->cpu))
126 return -EINVAL;
128 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
129 unsigned int freq = table[i].frequency;
130 if (freq == CPUFREQ_ENTRY_INVALID)
131 continue;
132 if ((freq < policy->min) || (freq > policy->max))
133 continue;
134 switch (relation) {
135 case CPUFREQ_RELATION_H:
136 if (freq <= target_freq) {
137 if (freq >= optimal.frequency) {
138 optimal.frequency = freq;
139 optimal.index = i;
141 } else {
142 if (freq <= suboptimal.frequency) {
143 suboptimal.frequency = freq;
144 suboptimal.index = i;
147 break;
148 case CPUFREQ_RELATION_L:
149 if (freq >= target_freq) {
150 if (freq <= optimal.frequency) {
151 optimal.frequency = freq;
152 optimal.index = i;
154 } else {
155 if (freq >= suboptimal.frequency) {
156 suboptimal.frequency = freq;
157 suboptimal.index = i;
160 break;
163 if (optimal.index > i) {
164 if (suboptimal.index > i)
165 return -EINVAL;
166 *index = suboptimal.index;
167 } else
168 *index = optimal.index;
170 dprintk("target is %u (%u kHz, %u)\n", *index, table[*index].frequency,
171 table[*index].index);
173 return 0;
175 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
177 static DEFINE_PER_CPU(struct cpufreq_frequency_table *, show_table);
179 * show_available_freqs - show available frequencies for the specified CPU
181 static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf)
183 unsigned int i = 0;
184 unsigned int cpu = policy->cpu;
185 ssize_t count = 0;
186 struct cpufreq_frequency_table *table;
188 if (!per_cpu(show_table, cpu))
189 return -ENODEV;
191 table = per_cpu(show_table, cpu);
193 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
194 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
195 continue;
196 count += sprintf(&buf[count], "%d ", table[i].frequency);
198 count += sprintf(&buf[count], "\n");
200 return count;
204 struct freq_attr cpufreq_freq_attr_scaling_available_freqs = {
205 .attr = { .name = "scaling_available_frequencies",
206 .mode = 0444,
208 .show = show_available_freqs,
210 EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
213 * if you use these, you must assure that the frequency table is valid
214 * all the time between get_attr and put_attr!
216 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
217 unsigned int cpu)
219 dprintk("setting show_table for cpu %u to %p\n", cpu, table);
220 per_cpu(show_table, cpu) = table;
222 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr);
224 void cpufreq_frequency_table_put_attr(unsigned int cpu)
226 dprintk("clearing show_table for cpu %u\n", cpu);
227 per_cpu(show_table, cpu) = NULL;
229 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr);
231 struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
233 return per_cpu(show_table, cpu);
235 EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
237 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
238 MODULE_DESCRIPTION("CPUfreq frequency table helpers");
239 MODULE_LICENSE("GPL");