Blame view

drivers/cpufreq/sh-cpufreq.c 4.76 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
   * cpufreq driver for the SuperH processors.
   *
3bccf4677   Paul Mundt   sh: cpufreq: perc...
4
   * Copyright (C) 2002 - 2012 Paul Mundt
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
6
   * Copyright (C) 2002 M. R. Brown
   *
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
7
8
9
10
11
12
13
   * Clock framework bits from arch/avr32/mach-at32ap/cpufreq.c
   *
   *   Copyright (C) 2004-2007 Atmel Corporation
   *
   * This file is subject to the terms and conditions of the GNU General Public
   * License.  See the file "COPYING" in the main directory of this archive
   * for more details.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
   */
ecbef17ad   Paul Mundt   sh: cpufreq: stru...
15
  #define pr_fmt(fmt) "cpufreq: " fmt
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
17
18
19
  #include <linux/types.h>
  #include <linux/cpufreq.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
  #include <linux/init.h>
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
21
  #include <linux/err.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
  #include <linux/cpumask.h>
ecbef17ad   Paul Mundt   sh: cpufreq: stru...
23
  #include <linux/cpu.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
  #include <linux/smp.h>
4e57b6817   Tim Schmielau   [PATCH] fix missi...
25
  #include <linux/sched.h>	/* set_cpus_allowed() */
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
26
  #include <linux/clk.h>
3bccf4677   Paul Mundt   sh: cpufreq: perc...
27
28
  #include <linux/percpu.h>
  #include <linux/sh_clk.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29

3bccf4677   Paul Mundt   sh: cpufreq: perc...
30
  static DEFINE_PER_CPU(struct clk, sh_cpuclk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31

205dcc1ec   Thomas Gleixner   cpufreq/sh: Repla...
32
33
34
35
  struct cpufreq_target {
  	struct cpufreq_policy	*policy;
  	unsigned int		freq;
  };
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
36
  static unsigned int sh_cpufreq_get(unsigned int cpu)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
  {
3bccf4677   Paul Mundt   sh: cpufreq: perc...
38
  	return (clk_get_rate(&per_cpu(sh_cpuclk, cpu)) + 500) / 1000;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
  }
205dcc1ec   Thomas Gleixner   cpufreq/sh: Repla...
40
  static long __sh_cpufreq_target(void *arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
  {
205dcc1ec   Thomas Gleixner   cpufreq/sh: Repla...
42
43
44
  	struct cpufreq_target *target = arg;
  	struct cpufreq_policy *policy = target->policy;
  	int cpu = policy->cpu;
3bccf4677   Paul Mundt   sh: cpufreq: perc...
45
  	struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
  	struct cpufreq_freqs freqs;
ecbef17ad   Paul Mundt   sh: cpufreq: stru...
47
  	struct device *dev;
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
48
  	long freq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49

205dcc1ec   Thomas Gleixner   cpufreq/sh: Repla...
50
51
  	if (smp_processor_id() != cpu)
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52

ecbef17ad   Paul Mundt   sh: cpufreq: stru...
53
  	dev = get_cpu_device(cpu);
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
54
  	/* Convert target_freq from kHz to Hz */
205dcc1ec   Thomas Gleixner   cpufreq/sh: Repla...
55
  	freq = clk_round_rate(cpuclk, target->freq * 1000);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56

cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
57
58
  	if (freq < (policy->min * 1000) || freq > (policy->max * 1000))
  		return -EINVAL;
205dcc1ec   Thomas Gleixner   cpufreq/sh: Repla...
59
60
  	dev_dbg(dev, "requested frequency %u Hz
  ", target->freq * 1000);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61

cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
62
63
64
  	freqs.old	= sh_cpufreq_get(cpu);
  	freqs.new	= (freq + 500) / 1000;
  	freqs.flags	= 0;
205dcc1ec   Thomas Gleixner   cpufreq/sh: Repla...
65
  	cpufreq_freq_transition_begin(target->policy, &freqs);
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
66
  	clk_set_rate(cpuclk, freq);
205dcc1ec   Thomas Gleixner   cpufreq/sh: Repla...
67
  	cpufreq_freq_transition_end(target->policy, &freqs, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
68

ecbef17ad   Paul Mundt   sh: cpufreq: stru...
69
70
  	dev_dbg(dev, "set frequency %lu Hz
  ", freq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
  	return 0;
  }
205dcc1ec   Thomas Gleixner   cpufreq/sh: Repla...
73
74
75
76
77
78
79
80
81
82
83
  /*
   * Here we notify other drivers of the proposed change and the final change.
   */
  static int sh_cpufreq_target(struct cpufreq_policy *policy,
  			     unsigned int target_freq,
  			     unsigned int relation)
  {
  	struct cpufreq_target data = { .policy = policy, .freq = target_freq };
  
  	return work_on_cpu(policy->cpu, __sh_cpufreq_target, &data);
  }
f5f68d165   Rafael J. Wysocki   cpufreq: Avoid cr...
84
  static int sh_cpufreq_verify(struct cpufreq_policy_data *policy)
1bcfc723c   Paul Mundt   sh: cpufreq: Supp...
85
86
87
88
89
90
91
  {
  	struct clk *cpuclk = &per_cpu(sh_cpuclk, policy->cpu);
  	struct cpufreq_frequency_table *freq_table;
  
  	freq_table = cpuclk->nr_freqs ? cpuclk->freq_table : NULL;
  	if (freq_table)
  		return cpufreq_frequency_table_verify(policy, freq_table);
be49e3465   Viresh Kumar   cpufreq: add new ...
92
  	cpufreq_verify_within_cpu_limits(policy);
1bcfc723c   Paul Mundt   sh: cpufreq: Supp...
93
94
95
  
  	policy->min = (clk_round_rate(cpuclk, 1) + 500) / 1000;
  	policy->max = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
be49e3465   Viresh Kumar   cpufreq: add new ...
96
  	cpufreq_verify_within_cpu_limits(policy);
1bcfc723c   Paul Mundt   sh: cpufreq: Supp...
97
98
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
  static int sh_cpufreq_cpu_init(struct cpufreq_policy *policy)
  {
3bccf4677   Paul Mundt   sh: cpufreq: perc...
101
102
  	unsigned int cpu = policy->cpu;
  	struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu);
1bcfc723c   Paul Mundt   sh: cpufreq: Supp...
103
  	struct cpufreq_frequency_table *freq_table;
ecbef17ad   Paul Mundt   sh: cpufreq: stru...
104
  	struct device *dev;
3bccf4677   Paul Mundt   sh: cpufreq: perc...
105

ecbef17ad   Paul Mundt   sh: cpufreq: stru...
106
107
108
  	dev = get_cpu_device(cpu);
  
  	cpuclk = clk_get(dev, "cpu_clk");
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
109
  	if (IS_ERR(cpuclk)) {
ecbef17ad   Paul Mundt   sh: cpufreq: stru...
110
111
  		dev_err(dev, "couldn't get CPU clk
  ");
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
112
113
  		return PTR_ERR(cpuclk);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114

1bcfc723c   Paul Mundt   sh: cpufreq: Supp...
115
116
  	freq_table = cpuclk->nr_freqs ? cpuclk->freq_table : NULL;
  	if (freq_table) {
ec8d2cc62   Viresh Kumar   cpufreq: sh: Don'...
117
  		policy->freq_table = freq_table;
1bcfc723c   Paul Mundt   sh: cpufreq: Supp...
118
  	} else {
1a565cf07   Paul Mundt   sh: cpufreq: noti...
119
120
121
  		dev_notice(dev, "no frequency table found, falling back "
  			   "to rate rounding.
  ");
eb2f50ff9   Viresh Kumar   cpufreq: drivers:...
122
  		policy->min = policy->cpuinfo.min_freq =
1a565cf07   Paul Mundt   sh: cpufreq: noti...
123
  			(clk_round_rate(cpuclk, 1) + 500) / 1000;
eb2f50ff9   Viresh Kumar   cpufreq: drivers:...
124
  		policy->max = policy->cpuinfo.max_freq =
1a565cf07   Paul Mundt   sh: cpufreq: noti...
125
  			(clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
126
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127

cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
128
129
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130

1bcfc723c   Paul Mundt   sh: cpufreq: Supp...
131
  static int sh_cpufreq_cpu_exit(struct cpufreq_policy *policy)
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
132
  {
1bcfc723c   Paul Mundt   sh: cpufreq: Supp...
133
134
  	unsigned int cpu = policy->cpu;
  	struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu);
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
135

cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
136
  	clk_put(cpuclk);
1bcfc723c   Paul Mundt   sh: cpufreq: Supp...
137

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
139
  	return 0;
  }
ec8d2cc62   Viresh Kumar   cpufreq: sh: Don'...
140
141
142
143
144
145
146
147
148
149
  static void sh_cpufreq_cpu_ready(struct cpufreq_policy *policy)
  {
  	struct device *dev = get_cpu_device(policy->cpu);
  
  	dev_info(dev, "CPU Frequencies - Minimum %u.%03u MHz, "
  	       "Maximum %u.%03u MHz.
  ",
  	       policy->min / 1000, policy->min % 1000,
  	       policy->max / 1000, policy->max % 1000);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
  static struct cpufreq_driver sh_cpufreq_driver = {
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
151
  	.name		= "sh",
fe829ed8e   Viresh Kumar   cpufreq: Add CPUF...
152
  	.flags		= CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING,
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
153
  	.get		= sh_cpufreq_get,
1bcfc723c   Paul Mundt   sh: cpufreq: Supp...
154
155
156
157
  	.target		= sh_cpufreq_target,
  	.verify		= sh_cpufreq_verify,
  	.init		= sh_cpufreq_cpu_init,
  	.exit		= sh_cpufreq_cpu_exit,
ec8d2cc62   Viresh Kumar   cpufreq: sh: Don'...
158
  	.ready		= sh_cpufreq_cpu_ready,
a8f64decf   Viresh Kumar   cpufreq: sh: Use ...
159
  	.attr		= cpufreq_generic_attr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
  };
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
161
  static int __init sh_cpufreq_module_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
  {
ecbef17ad   Paul Mundt   sh: cpufreq: stru...
163
164
  	pr_notice("SuperH CPU frequency driver.
  ");
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
165
  	return cpufreq_register_driver(&sh_cpufreq_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
  }
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
167
  static void __exit sh_cpufreq_module_exit(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
169
170
  {
  	cpufreq_unregister_driver(&sh_cpufreq_driver);
  }
cb5ec75b8   Paul Mundt   sh: cpufreq: cloc...
171
172
  module_init(sh_cpufreq_module_init);
  module_exit(sh_cpufreq_module_exit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
174
175
176
  
  MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  MODULE_DESCRIPTION("cpufreq driver for SuperH");
  MODULE_LICENSE("GPL");