]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/cpufreq/at32ap-cpufreq.c
Merge remote-tracking branch 'block/for-next'
[karo-tx-linux.git] / drivers / cpufreq / at32ap-cpufreq.c
1 /*
2  * Copyright (C) 2004-2007 Atmel Corporation
3  *
4  * Based on MIPS implementation arch/mips/kernel/time.c
5  *   Copyright 2001 MontaVista Software Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 /*#define DEBUG*/
13
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/init.h>
17 #include <linux/cpufreq.h>
18 #include <linux/io.h>
19 #include <linux/clk.h>
20 #include <linux/err.h>
21 #include <linux/export.h>
22 #include <linux/slab.h>
23
24 static struct clk *cpuclk;
25 static struct cpufreq_frequency_table *freq_table;
26
27 static unsigned int at32_get_speed(unsigned int cpu)
28 {
29         /* No SMP support */
30         if (cpu)
31                 return 0;
32         return (unsigned int)((clk_get_rate(cpuclk) + 500) / 1000);
33 }
34
35 static unsigned int     ref_freq;
36 static unsigned long    loops_per_jiffy_ref;
37
38 static int at32_set_target(struct cpufreq_policy *policy,
39                           unsigned int target_freq,
40                           unsigned int relation)
41 {
42         struct cpufreq_freqs freqs;
43         long freq;
44
45         /* Convert target_freq from kHz to Hz */
46         freq = clk_round_rate(cpuclk, target_freq * 1000);
47
48         /* Check if policy->min <= new_freq <= policy->max */
49         if(freq < (policy->min * 1000) || freq > (policy->max * 1000))
50                 return -EINVAL;
51
52         pr_debug("cpufreq: requested frequency %u Hz\n", target_freq * 1000);
53
54         freqs.old = at32_get_speed(0);
55         freqs.new = (freq + 500) / 1000;
56         freqs.flags = 0;
57
58         if (!ref_freq) {
59                 ref_freq = freqs.old;
60                 loops_per_jiffy_ref = boot_cpu_data.loops_per_jiffy;
61         }
62
63         cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
64         if (freqs.old < freqs.new)
65                 boot_cpu_data.loops_per_jiffy = cpufreq_scale(
66                                 loops_per_jiffy_ref, ref_freq, freqs.new);
67         clk_set_rate(cpuclk, freq);
68         if (freqs.new < freqs.old)
69                 boot_cpu_data.loops_per_jiffy = cpufreq_scale(
70                                 loops_per_jiffy_ref, ref_freq, freqs.new);
71         cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
72
73         pr_debug("cpufreq: set frequency %lu Hz\n", freq);
74
75         return 0;
76 }
77
78 static int __init at32_cpufreq_driver_init(struct cpufreq_policy *policy)
79 {
80         unsigned int frequency, rate, min_freq;
81         int retval, steps, i;
82
83         if (policy->cpu != 0)
84                 return -EINVAL;
85
86         cpuclk = clk_get(NULL, "cpu");
87         if (IS_ERR(cpuclk)) {
88                 pr_debug("cpufreq: could not get CPU clk\n");
89                 retval = PTR_ERR(cpuclk);
90                 goto out_err;
91         }
92
93         min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;
94         frequency = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
95         policy->cpuinfo.transition_latency = 0;
96
97         /*
98          * AVR32 CPU frequency rate scales in power of two between maximum and
99          * minimum, also add space for the table end marker.
100          *
101          * Further validate that the frequency is usable, and append it to the
102          * frequency table.
103          */
104         steps = fls(frequency / min_freq) + 1;
105         freq_table = kzalloc(steps * sizeof(struct cpufreq_frequency_table),
106                         GFP_KERNEL);
107         if (!freq_table) {
108                 retval = -ENOMEM;
109                 goto out_err_put_clk;
110         }
111
112         for (i = 0; i < (steps - 1); i++) {
113                 rate = clk_round_rate(cpuclk, frequency * 1000) / 1000;
114
115                 if (rate != frequency)
116                         freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
117                 else
118                         freq_table[i].frequency = frequency;
119
120                 frequency /= 2;
121         }
122
123         freq_table[steps - 1].frequency = CPUFREQ_TABLE_END;
124
125         retval = cpufreq_table_validate_and_show(policy, freq_table);
126         if (!retval) {
127                 printk("cpufreq: AT32AP CPU frequency driver\n");
128                 return 0;
129         }
130
131         kfree(freq_table);
132 out_err_put_clk:
133         clk_put(cpuclk);
134 out_err:
135         return retval;
136 }
137
138 static struct cpufreq_driver at32_driver = {
139         .name           = "at32ap",
140         .init           = at32_cpufreq_driver_init,
141         .verify         = cpufreq_generic_frequency_table_verify,
142         .target         = at32_set_target,
143         .get            = at32_get_speed,
144         .flags          = CPUFREQ_STICKY,
145 };
146
147 static int __init at32_cpufreq_init(void)
148 {
149         return cpufreq_register_driver(&at32_driver);
150 }
151 late_initcall(at32_cpufreq_init);