blob: 627743326253f553e2da7fc2d7f95f5c9d809c28 [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 Egtvedt9e58e182007-06-04 16:10:57 +020022#include <asm/system.h>
23
24static struct clk *cpuclk;
25
26static int at32_verify_speed(struct cpufreq_policy *policy)
27{
28 if (policy->cpu != 0)
29 return -EINVAL;
30
31 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
32 policy->cpuinfo.max_freq);
33 return 0;
34}
35
36static unsigned int at32_get_speed(unsigned int cpu)
37{
38 /* No SMP support */
39 if (cpu)
40 return 0;
41 return (unsigned int)((clk_get_rate(cpuclk) + 500) / 1000);
42}
43
Haavard Skinnemoene3f91ca2008-10-23 11:23:08 +020044static unsigned int ref_freq;
45static unsigned long loops_per_jiffy_ref;
46
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020047static int at32_set_target(struct cpufreq_policy *policy,
48 unsigned int target_freq,
49 unsigned int relation)
50{
51 struct cpufreq_freqs freqs;
52 long freq;
53
54 /* Convert target_freq from kHz to Hz */
55 freq = clk_round_rate(cpuclk, target_freq * 1000);
56
57 /* Check if policy->min <= new_freq <= policy->max */
58 if(freq < (policy->min * 1000) || freq > (policy->max * 1000))
59 return -EINVAL;
60
61 pr_debug("cpufreq: requested frequency %u Hz\n", target_freq * 1000);
62
63 freqs.old = at32_get_speed(0);
64 freqs.new = (freq + 500) / 1000;
65 freqs.cpu = 0;
66 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
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020073 cpufreq_notify_transition(&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);
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +020081 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
82
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{
90 if (policy->cpu != 0)
91 return -EINVAL;
92
93 cpuclk = clk_get(NULL, "cpu");
94 if (IS_ERR(cpuclk)) {
95 pr_debug("cpufreq: could not get CPU clk\n");
96 return PTR_ERR(cpuclk);
97 }
98
99 policy->cpuinfo.min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;
100 policy->cpuinfo.max_freq = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
101 policy->cpuinfo.transition_latency = 0;
102 policy->cur = at32_get_speed(0);
103 policy->min = policy->cpuinfo.min_freq;
104 policy->max = policy->cpuinfo.max_freq;
Hans-Christian Egtvedt9e58e182007-06-04 16:10:57 +0200105
106 printk("cpufreq: AT32AP CPU frequency driver\n");
107
108 return 0;
109}
110
111static struct cpufreq_driver at32_driver = {
112 .name = "at32ap",
113 .owner = THIS_MODULE,
114 .init = at32_cpufreq_driver_init,
115 .verify = at32_verify_speed,
116 .target = at32_set_target,
117 .get = at32_get_speed,
118 .flags = CPUFREQ_STICKY,
119};
120
121static int __init at32_cpufreq_init(void)
122{
123 return cpufreq_register_driver(&at32_driver);
124}
Haavard Skinnemoenf04d2642008-05-27 09:37:42 +0200125late_initcall(at32_cpufreq_init);