blob: 788f7e77565db9c9e0fda8ad58105f7017150311 [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
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020027static 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
Haavard Skinnemoene3f91ca2008-10-23 11:23:08 +020035static unsigned int ref_freq;
36static unsigned long loops_per_jiffy_ref;
37
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020038static 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;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020056 freqs.flags = 0;
57
Haavard Skinnemoene3f91ca2008-10-23 11:23:08 +020058 if (!ref_freq) {
59 ref_freq = freqs.old;
60 loops_per_jiffy_ref = boot_cpu_data.loops_per_jiffy;
61 }
62
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053063 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Haavard Skinnemoene3f91ca2008-10-23 11:23:08 +020064 if (freqs.old < freqs.new)
65 boot_cpu_data.loops_per_jiffy = cpufreq_scale(
66 loops_per_jiffy_ref, ref_freq, freqs.new);
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020067 clk_set_rate(cpuclk, freq);
Haavard Skinnemoene3f91ca2008-10-23 11:23:08 +020068 if (freqs.new < freqs.old)
69 boot_cpu_data.loops_per_jiffy = cpufreq_scale(
70 loops_per_jiffy_ref, ref_freq, freqs.new);
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053071 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020072
73 pr_debug("cpufreq: set frequency %lu Hz\n", freq);
74
75 return 0;
76}
77
78static int __init at32_cpufreq_driver_init(struct cpufreq_policy *policy)
79{
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +053080 unsigned int frequency, rate;
81 int retval, steps, i;
82
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020083 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");
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +053089 retval = PTR_ERR(cpuclk);
90 goto out_err;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020091 }
92
93 policy->cpuinfo.min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;
94 policy->cpuinfo.max_freq = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
95 policy->cpuinfo.transition_latency = 0;
96 policy->cur = at32_get_speed(0);
97 policy->min = policy->cpuinfo.min_freq;
98 policy->max = policy->cpuinfo.max_freq;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020099
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +0530100 /*
101 * AVR32 CPU frequency rate scales in power of two between maximum and
102 * minimum, also add space for the table end marker.
103 *
104 * Further validate that the frequency is usable, and append it to the
105 * frequency table.
106 */
107 steps = fls(policy->cpuinfo.max_freq / policy->cpuinfo.min_freq) + 1;
108 freq_table = kzalloc(steps * sizeof(struct cpufreq_frequency_table),
109 GFP_KERNEL);
110 if (!freq_table) {
111 retval = -ENOMEM;
112 goto out_err_put_clk;
113 }
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200114
Hans-Christian Egtvedt848cb942013-09-16 18:56:41 +0530115 frequency = policy->cpuinfo.max_freq;
116 for (i = 0; i < (steps - 1); i++) {
117 rate = clk_round_rate(cpuclk, frequency * 1000) / 1000;
118
119 if (rate != frequency)
120 freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
121 else
122 freq_table[i].frequency = frequency;
123
124 frequency /= 2;
125 }
126
127 freq_table[steps - 1].frequency = CPUFREQ_TABLE_END;
128
129 retval = cpufreq_table_validate_and_show(policy, freq_table);
130 if (!retval) {
131 printk("cpufreq: AT32AP CPU frequency driver\n");
132 return 0;
133 }
134
135 kfree(freq_table);
136out_err_put_clk:
137 clk_put(cpuclk);
138out_err:
139 return retval;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200140}
141
142static struct cpufreq_driver at32_driver = {
143 .name = "at32ap",
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200144 .init = at32_cpufreq_driver_init,
Viresh Kumar5ae68f42013-10-03 20:27:58 +0530145 .verify = cpufreq_generic_frequency_table_verify,
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200146 .target = at32_set_target,
147 .get = at32_get_speed,
148 .flags = CPUFREQ_STICKY,
149};
150
151static int __init at32_cpufreq_init(void)
152{
153 return cpufreq_register_driver(&at32_driver);
154}
Haavard Skinnemoenf04d2642008-05-27 09:37:42 +0200155late_initcall(at32_cpufreq_init);