blob: 1d19fa59bc2c68c00fab1aca87b77486d04f9551 [file] [log] [blame]
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +02001/*
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>
Paul Gortmaker09cf6a22011-08-01 12:55:26 -040021#include <linux/export.h>
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +053022#include <linux/slab.h>
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020023
24static struct clk *cpuclk;
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +053025static struct cpufreq_frequency_table *freq_table;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020026
27static int at32_verify_speed(struct cpufreq_policy *policy)
28{
29 if (policy->cpu != 0)
30 return -EINVAL;
31
32 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
33 policy->cpuinfo.max_freq);
34 return 0;
35}
36
37static unsigned int at32_get_speed(unsigned int cpu)
38{
39 /* No SMP support */
40 if (cpu)
41 return 0;
42 return (unsigned int)((clk_get_rate(cpuclk) + 500) / 1000);
43}
44
Haavard Skinnemoene3f91ca2008-10-23 11:23:08 +020045static unsigned int ref_freq;
46static unsigned long loops_per_jiffy_ref;
47
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020048static int at32_set_target(struct cpufreq_policy *policy,
49 unsigned int target_freq,
50 unsigned int relation)
51{
52 struct cpufreq_freqs freqs;
53 long freq;
54
55 /* Convert target_freq from kHz to Hz */
56 freq = clk_round_rate(cpuclk, target_freq * 1000);
57
58 /* Check if policy->min <= new_freq <= policy->max */
59 if(freq < (policy->min * 1000) || freq > (policy->max * 1000))
60 return -EINVAL;
61
62 pr_debug("cpufreq: requested frequency %u Hz\n", target_freq * 1000);
63
64 freqs.old = at32_get_speed(0);
65 freqs.new = (freq + 500) / 1000;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020066 freqs.flags = 0;
67
Haavard Skinnemoene3f91ca2008-10-23 11:23:08 +020068 if (!ref_freq) {
69 ref_freq = freqs.old;
70 loops_per_jiffy_ref = boot_cpu_data.loops_per_jiffy;
71 }
72
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053073 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Haavard Skinnemoene3f91ca2008-10-23 11:23:08 +020074 if (freqs.old < freqs.new)
75 boot_cpu_data.loops_per_jiffy = cpufreq_scale(
76 loops_per_jiffy_ref, ref_freq, freqs.new);
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020077 clk_set_rate(cpuclk, freq);
Haavard Skinnemoene3f91ca2008-10-23 11:23:08 +020078 if (freqs.new < freqs.old)
79 boot_cpu_data.loops_per_jiffy = cpufreq_scale(
80 loops_per_jiffy_ref, ref_freq, freqs.new);
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053081 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020082
83 pr_debug("cpufreq: set frequency %lu Hz\n", freq);
84
85 return 0;
86}
87
88static int __init at32_cpufreq_driver_init(struct cpufreq_policy *policy)
89{
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +053090 unsigned int frequency, rate;
91 int retval, steps, i;
92
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020093 if (policy->cpu != 0)
94 return -EINVAL;
95
96 cpuclk = clk_get(NULL, "cpu");
97 if (IS_ERR(cpuclk)) {
98 pr_debug("cpufreq: could not get CPU clk\n");
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +053099 retval = PTR_ERR(cpuclk);
100 goto out_err;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200101 }
102
103 policy->cpuinfo.min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;
104 policy->cpuinfo.max_freq = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
105 policy->cpuinfo.transition_latency = 0;
106 policy->cur = at32_get_speed(0);
107 policy->min = policy->cpuinfo.min_freq;
108 policy->max = policy->cpuinfo.max_freq;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200109
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +0530110 /*
111 * AVR32 CPU frequency rate scales in power of two between maximum and
112 * minimum, also add space for the table end marker.
113 *
114 * Further validate that the frequency is usable, and append it to the
115 * frequency table.
116 */
117 steps = fls(policy->cpuinfo.max_freq / policy->cpuinfo.min_freq) + 1;
118 freq_table = kzalloc(steps * sizeof(struct cpufreq_frequency_table),
119 GFP_KERNEL);
120 if (!freq_table) {
121 retval = -ENOMEM;
122 goto out_err_put_clk;
123 }
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200124
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +0530125 frequency = policy->cpuinfo.max_freq;
126 for (i = 0; i < (steps - 1); i++) {
127 rate = clk_round_rate(cpuclk, frequency * 1000) / 1000;
128
129 if (rate != frequency)
130 freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
131 else
132 freq_table[i].frequency = frequency;
133
134 frequency /= 2;
135 }
136
137 freq_table[steps - 1].frequency = CPUFREQ_TABLE_END;
138
139 retval = cpufreq_table_validate_and_show(policy, freq_table);
140 if (!retval) {
141 printk("cpufreq: AT32AP CPU frequency driver\n");
142 return 0;
143 }
144
145 kfree(freq_table);
146out_err_put_clk:
147 clk_put(cpuclk);
148out_err:
149 return retval;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200150}
151
152static struct cpufreq_driver at32_driver = {
153 .name = "at32ap",
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200154 .init = at32_cpufreq_driver_init,
155 .verify = at32_verify_speed,
156 .target = at32_set_target,
157 .get = at32_get_speed,
158 .flags = CPUFREQ_STICKY,
159};
160
161static int __init at32_cpufreq_init(void)
162{
163 return cpufreq_register_driver(&at32_driver);
164}
Haavard Skinnemoenf04d2642008-05-27 09:37:42 +0200165late_initcall(at32_cpufreq_init);