blob: 8203865e16b70801f9217a7c89997d90d03c53f8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/sh/kernel/cpufreq.c
3 *
4 * cpufreq driver for the SuperH processors.
5 *
Paul Mundt3bccf462012-01-27 17:49:16 +09006 * Copyright (C) 2002 - 2012 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Copyright (C) 2002 M. R. Brown
8 *
Paul Mundtcb5ec752007-07-20 13:38:19 +09009 * Clock framework bits from arch/avr32/mach-at32ap/cpufreq.c
10 *
11 * Copyright (C) 2004-2007 Atmel Corporation
12 *
13 * This file is subject to the terms and conditions of the GNU General Public
14 * License. See the file "COPYING" in the main directory of this archive
15 * for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17#include <linux/types.h>
18#include <linux/cpufreq.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/init.h>
Paul Mundtcb5ec752007-07-20 13:38:19 +090022#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/cpumask.h>
24#include <linux/smp.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080025#include <linux/sched.h> /* set_cpus_allowed() */
Paul Mundtcb5ec752007-07-20 13:38:19 +090026#include <linux/clk.h>
Paul Mundt3bccf462012-01-27 17:49:16 +090027#include <linux/percpu.h>
28#include <linux/sh_clk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Paul Mundt3bccf462012-01-27 17:49:16 +090030static DEFINE_PER_CPU(struct clk, sh_cpuclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Paul Mundtcb5ec752007-07-20 13:38:19 +090032static unsigned int sh_cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Paul Mundt3bccf462012-01-27 17:49:16 +090034 return (clk_get_rate(&per_cpu(sh_cpuclk, cpu)) + 500) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * Here we notify other drivers of the proposed change and the final change.
39 */
Paul Mundtcb5ec752007-07-20 13:38:19 +090040static int sh_cpufreq_target(struct cpufreq_policy *policy,
41 unsigned int target_freq,
42 unsigned int relation)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Paul Mundtcb5ec752007-07-20 13:38:19 +090044 unsigned int cpu = policy->cpu;
Paul Mundt3bccf462012-01-27 17:49:16 +090045 struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 cpumask_t cpus_allowed;
47 struct cpufreq_freqs freqs;
Paul Mundtcb5ec752007-07-20 13:38:19 +090048 long freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 if (!cpu_online(cpu))
51 return -ENODEV;
52
53 cpus_allowed = current->cpus_allowed;
Julia Lawall59a2f7d2010-03-26 22:03:49 +000054 set_cpus_allowed_ptr(current, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 BUG_ON(smp_processor_id() != cpu);
57
Paul Mundtcb5ec752007-07-20 13:38:19 +090058 /* Convert target_freq from kHz to Hz */
59 freq = clk_round_rate(cpuclk, target_freq * 1000);
60
61 if (freq < (policy->min * 1000) || freq > (policy->max * 1000))
62 return -EINVAL;
63
64 pr_debug("cpufreq: requested frequency %u Hz\n", target_freq * 1000);
65
66 freqs.cpu = cpu;
67 freqs.old = sh_cpufreq_get(cpu);
68 freqs.new = (freq + 500) / 1000;
69 freqs.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
Julia Lawall59a2f7d2010-03-26 22:03:49 +000072 set_cpus_allowed_ptr(current, &cpus_allowed);
Paul Mundtcb5ec752007-07-20 13:38:19 +090073 clk_set_rate(cpuclk, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
75
Paul Mundtcb5ec752007-07-20 13:38:19 +090076 pr_debug("cpufreq: set frequency %lu Hz\n", freq);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return 0;
79}
80
81static int sh_cpufreq_cpu_init(struct cpufreq_policy *policy)
82{
Paul Mundt3bccf462012-01-27 17:49:16 +090083 unsigned int cpu = policy->cpu;
84 struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu);
85
86 if (!cpu_online(cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return -ENODEV;
88
Paul Mundtcb5ec752007-07-20 13:38:19 +090089 cpuclk = clk_get(NULL, "cpu_clk");
90 if (IS_ERR(cpuclk)) {
Paul Mundt3bccf462012-01-27 17:49:16 +090091 printk(KERN_ERR "cpufreq: couldn't get CPU#%d clk\n", cpu);
Paul Mundtcb5ec752007-07-20 13:38:19 +090092 return PTR_ERR(cpuclk);
93 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 /* cpuinfo and default policy values */
Paul Mundtcb5ec752007-07-20 13:38:19 +090096 policy->cpuinfo.min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;
97 policy->cpuinfo.max_freq = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Paul Mundt3bccf462012-01-27 17:49:16 +0900100 policy->cur = sh_cpufreq_get(cpu);
Paul Mundtcb5ec752007-07-20 13:38:19 +0900101 policy->min = policy->cpuinfo.min_freq;
102 policy->max = policy->cpuinfo.max_freq;
103
Paul Mundtcb5ec752007-07-20 13:38:19 +0900104 /*
105 * Catch the cases where the clock framework hasn't been wired up
106 * properly to support scaling.
107 */
108 if (unlikely(policy->min == policy->max)) {
109 printk(KERN_ERR "cpufreq: clock framework rate rounding "
Paul Mundt3bccf462012-01-27 17:49:16 +0900110 "not supported on CPU#%d.\n", cpu);
Paul Mundtcb5ec752007-07-20 13:38:19 +0900111
112 clk_put(cpuclk);
113 return -EINVAL;
114 }
115
Paul Mundt033eb0a2009-09-15 09:26:04 +0900116 printk(KERN_INFO "cpufreq: CPU#%d Frequencies - Minimum %u.%03u MHz, "
Paul Mundtcb5ec752007-07-20 13:38:19 +0900117 "Maximum %u.%03u MHz.\n",
Paul Mundt3bccf462012-01-27 17:49:16 +0900118 cpu, policy->min / 1000, policy->min % 1000,
Paul Mundtcb5ec752007-07-20 13:38:19 +0900119 policy->max / 1000, policy->max % 1000);
120
121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static int sh_cpufreq_verify(struct cpufreq_policy *policy)
125{
Paul Mundtcb5ec752007-07-20 13:38:19 +0900126 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
127 policy->cpuinfo.max_freq);
128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Paul Mundtcb5ec752007-07-20 13:38:19 +0900131static int sh_cpufreq_exit(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Paul Mundt3bccf462012-01-27 17:49:16 +0900133 struct clk *cpuclk = &per_cpu(sh_cpuclk, policy->cpu);
Paul Mundtcb5ec752007-07-20 13:38:19 +0900134 clk_put(cpuclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 0;
136}
137
138static struct cpufreq_driver sh_cpufreq_driver = {
139 .owner = THIS_MODULE,
Paul Mundtcb5ec752007-07-20 13:38:19 +0900140 .name = "sh",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .init = sh_cpufreq_cpu_init,
142 .verify = sh_cpufreq_verify,
143 .target = sh_cpufreq_target,
Paul Mundtcb5ec752007-07-20 13:38:19 +0900144 .get = sh_cpufreq_get,
145 .exit = sh_cpufreq_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
Paul Mundtcb5ec752007-07-20 13:38:19 +0900148static int __init sh_cpufreq_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Paul Mundt00765c82007-09-21 17:53:26 +0900150 printk(KERN_INFO "cpufreq: SuperH CPU frequency driver.\n");
Paul Mundtcb5ec752007-07-20 13:38:19 +0900151 return cpufreq_register_driver(&sh_cpufreq_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Paul Mundtcb5ec752007-07-20 13:38:19 +0900154static void __exit sh_cpufreq_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 cpufreq_unregister_driver(&sh_cpufreq_driver);
157}
158
Paul Mundtcb5ec752007-07-20 13:38:19 +0900159module_init(sh_cpufreq_module_init);
160module_exit(sh_cpufreq_module_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
163MODULE_DESCRIPTION("cpufreq driver for SuperH");
164MODULE_LICENSE("GPL");