blob: 9f5d6368766e9e11e02780198fff23128bccbc33 [file] [log] [blame]
Tony Lindgrenec6bced2005-07-10 19:58:20 +01001/*
Nishanth Menonffe4f0f2011-05-26 19:39:18 -07002 * CPU frequency scaling for OMAP using OPP information
Tony Lindgrenec6bced2005-07-10 19:58:20 +01003 *
4 * Copyright (C) 2005 Nokia Corporation
5 * Written by Tony Lindgren <tony@atomide.com>
6 *
7 * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
8 *
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -07009 * Copyright (C) 2007-2011 Texas Instruments, Inc.
10 * - OMAP3/4 support by Rajendra Nayak, Santosh Shilimkar
11 *
Tony Lindgrenec6bced2005-07-10 19:58:20 +010012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16#include <linux/types.h>
17#include <linux/kernel.h>
18#include <linux/sched.h>
19#include <linux/cpufreq.h>
20#include <linux/delay.h>
21#include <linux/init.h>
22#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000023#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010024#include <linux/io.h>
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -070025#include <linux/opp.h>
Russell King46c12212011-09-21 16:53:00 -070026#include <linux/cpu.h>
Kevin Hilmanc1b547b2011-09-30 10:41:26 -070027#include <linux/module.h>
Kevin Hilman53dfe8a2011-07-15 15:05:04 -070028#include <linux/regulator/consumer.h>
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -070029
30#include <asm/system.h>
31#include <asm/smp_plat.h>
Russell King46c12212011-09-21 16:53:00 -070032#include <asm/cpu.h>
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -070033
34#include <plat/clock.h>
35#include <plat/omap-pm.h>
36#include <plat/common.h>
Kevin Hilmanc1b547b2011-09-30 10:41:26 -070037#include <plat/omap_device.h>
Tony Lindgrenec6bced2005-07-10 19:58:20 +010038
Russell Kinga09e64f2008-08-05 16:14:15 +010039#include <mach/hardware.h>
Tony Lindgrenec6bced2005-07-10 19:58:20 +010040
Russell King46c12212011-09-21 16:53:00 -070041#ifdef CONFIG_SMP
42struct lpj_info {
43 unsigned long ref;
44 unsigned int freq;
45};
46
47static DEFINE_PER_CPU(struct lpj_info, lpj_ref);
48static struct lpj_info global_lpj_ref;
49#endif
50
Kevin Hilmanaeec2992009-01-27 19:13:38 -070051static struct cpufreq_frequency_table *freq_table;
Nishanth Menon1c782172011-05-26 19:39:20 -070052static atomic_t freq_table_users = ATOMIC_INIT(0);
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +030053static struct clk *mpu_clk;
Nishanth Menon08ca3e3b2011-05-25 16:38:46 -070054static char *mpu_clk_name;
Nishanth Menona820ffa2011-05-25 16:38:47 -070055static struct device *mpu_dev;
Kevin Hilman53dfe8a2011-07-15 15:05:04 -070056static struct regulator *mpu_reg;
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +030057
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070058static int omap_verify_speed(struct cpufreq_policy *policy)
Tony Lindgrenec6bced2005-07-10 19:58:20 +010059{
Nishanth Menonbf2a3592011-05-26 19:39:17 -070060 if (!freq_table)
Tony Lindgrenec6bced2005-07-10 19:58:20 +010061 return -EINVAL;
Nishanth Menonbf2a3592011-05-26 19:39:17 -070062 return cpufreq_frequency_table_verify(policy, freq_table);
Tony Lindgrenec6bced2005-07-10 19:58:20 +010063}
64
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070065static unsigned int omap_getspeed(unsigned int cpu)
Tony Lindgrenec6bced2005-07-10 19:58:20 +010066{
Tony Lindgrenec6bced2005-07-10 19:58:20 +010067 unsigned long rate;
68
Russell King46c12212011-09-21 16:53:00 -070069 if (cpu >= NR_CPUS)
Tony Lindgrenec6bced2005-07-10 19:58:20 +010070 return 0;
71
Tony Lindgrenec6bced2005-07-10 19:58:20 +010072 rate = clk_get_rate(mpu_clk) / 1000;
Tony Lindgrenec6bced2005-07-10 19:58:20 +010073 return rate;
74}
75
76static int omap_target(struct cpufreq_policy *policy,
77 unsigned int target_freq,
78 unsigned int relation)
79{
Nishanth Menonbf2a3592011-05-26 19:39:17 -070080 unsigned int i;
Kevin Hilman53dfe8a2011-07-15 15:05:04 -070081 int r, ret = 0;
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -070082 struct cpufreq_freqs freqs;
Kevin Hilman53dfe8a2011-07-15 15:05:04 -070083 struct opp *opp;
84 unsigned long freq, volt = 0, volt_old = 0;
Tony Lindgrenec6bced2005-07-10 19:58:20 +010085
Nishanth Menonbf2a3592011-05-26 19:39:17 -070086 if (!freq_table) {
87 dev_err(mpu_dev, "%s: cpu%d: no freq table!\n", __func__,
88 policy->cpu);
89 return -EINVAL;
90 }
91
92 ret = cpufreq_frequency_table_target(policy, freq_table, target_freq,
93 relation, &i);
94 if (ret) {
95 dev_dbg(mpu_dev, "%s: cpu%d: no freq match for %d(ret=%d)\n",
96 __func__, policy->cpu, target_freq, ret);
97 return ret;
98 }
99 freqs.new = freq_table[i].frequency;
100 if (!freqs.new) {
101 dev_err(mpu_dev, "%s: cpu%d: no match for freq %d\n", __func__,
102 policy->cpu, target_freq);
103 return -EINVAL;
104 }
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700105
Russell King46c12212011-09-21 16:53:00 -0700106 freqs.old = omap_getspeed(policy->cpu);
Russell King46c12212011-09-21 16:53:00 -0700107 freqs.cpu = policy->cpu;
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100108
Colin Cross022ac032011-06-06 21:05:29 -0500109 if (freqs.old == freqs.new && policy->cur == freqs.new)
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700110 return ret;
111
Russell King46c12212011-09-21 16:53:00 -0700112 /* notifiers */
113 for_each_cpu(i, policy->cpus) {
114 freqs.cpu = i;
115 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
116 }
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700117
Kevin Hilman53dfe8a2011-07-15 15:05:04 -0700118 freq = freqs.new * 1000;
119
120 if (mpu_reg) {
121 opp = opp_find_freq_ceil(mpu_dev, &freq);
122 if (IS_ERR(opp)) {
123 dev_err(mpu_dev, "%s: unable to find MPU OPP for %d\n",
124 __func__, freqs.new);
125 return -EINVAL;
126 }
127 volt = opp_get_voltage(opp);
128 volt_old = regulator_get_voltage(mpu_reg);
129 }
130
131 dev_dbg(mpu_dev, "cpufreq-omap: %u MHz, %ld mV --> %u MHz, %ld mV\n",
132 freqs.old / 1000, volt_old ? volt_old / 1000 : -1,
133 freqs.new / 1000, volt ? volt / 1000 : -1);
134
135 /* scaling up? scale voltage before frequency */
136 if (mpu_reg && (freqs.new > freqs.old)) {
137 r = regulator_set_voltage(mpu_reg, volt, volt);
138 if (r < 0) {
139 dev_warn(mpu_dev, "%s: unable to scale voltage up.\n",
140 __func__);
141 freqs.new = freqs.old;
142 goto done;
143 }
144 }
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700145
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700146 ret = clk_set_rate(mpu_clk, freqs.new * 1000);
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700147
Kevin Hilman53dfe8a2011-07-15 15:05:04 -0700148 /* scaling down? scale voltage after frequency */
149 if (mpu_reg && (freqs.new < freqs.old)) {
150 r = regulator_set_voltage(mpu_reg, volt, volt);
151 if (r < 0) {
152 dev_warn(mpu_dev, "%s: unable to scale voltage down.\n",
153 __func__);
154 ret = clk_set_rate(mpu_clk, freqs.old * 1000);
155 freqs.new = freqs.old;
156 goto done;
157 }
158 }
159
160 freqs.new = omap_getspeed(policy->cpu);
Russell King46c12212011-09-21 16:53:00 -0700161#ifdef CONFIG_SMP
162 /*
163 * Note that loops_per_jiffy is not updated on SMP systems in
164 * cpufreq driver. So, update the per-CPU loops_per_jiffy value
165 * on frequency transition. We need to update all dependent CPUs.
166 */
167 for_each_cpu(i, policy->cpus) {
168 struct lpj_info *lpj = &per_cpu(lpj_ref, i);
169 if (!lpj->freq) {
170 lpj->ref = per_cpu(cpu_data, i).loops_per_jiffy;
171 lpj->freq = freqs.old;
172 }
173
174 per_cpu(cpu_data, i).loops_per_jiffy =
175 cpufreq_scale(lpj->ref, lpj->freq, freqs.new);
176 }
177
178 /* And don't forget to adjust the global one */
179 if (!global_lpj_ref.freq) {
180 global_lpj_ref.ref = loops_per_jiffy;
181 global_lpj_ref.freq = freqs.old;
182 }
183 loops_per_jiffy = cpufreq_scale(global_lpj_ref.ref, global_lpj_ref.freq,
184 freqs.new);
185#endif
186
Kevin Hilman53dfe8a2011-07-15 15:05:04 -0700187done:
Russell King46c12212011-09-21 16:53:00 -0700188 /* notifiers */
189 for_each_cpu(i, policy->cpus) {
190 freqs.cpu = i;
191 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
192 }
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100193
194 return ret;
195}
196
Nishanth Menon1c782172011-05-26 19:39:20 -0700197static inline void freq_table_free(void)
198{
199 if (atomic_dec_and_test(&freq_table_users))
200 opp_free_cpufreq_table(mpu_dev, &freq_table);
201}
202
Ming Lei790ab7e2011-03-03 06:41:46 +0800203static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100204{
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700205 int result = 0;
206
Nishanth Menon08ca3e3b2011-05-25 16:38:46 -0700207 mpu_clk = clk_get(NULL, mpu_clk_name);
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100208 if (IS_ERR(mpu_clk))
209 return PTR_ERR(mpu_clk);
210
Nishanth Menon11e04fd2011-05-26 19:39:19 -0700211 if (policy->cpu >= NR_CPUS) {
212 result = -EINVAL;
213 goto fail_ck;
214 }
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700215
Russell King46c12212011-09-21 16:53:00 -0700216 policy->cur = policy->min = policy->max = omap_getspeed(policy->cpu);
Nishanth Menon1c782172011-05-26 19:39:20 -0700217
218 if (atomic_inc_return(&freq_table_users) == 1)
219 result = opp_init_cpufreq_table(mpu_dev, &freq_table);
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700220
Nishanth Menonbf2a3592011-05-26 19:39:17 -0700221 if (result) {
222 dev_err(mpu_dev, "%s: cpu%d: failed creating freq table[%d]\n",
223 __func__, policy->cpu, result);
Nishanth Menon11e04fd2011-05-26 19:39:19 -0700224 goto fail_ck;
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700225 }
226
Nishanth Menonbf2a3592011-05-26 19:39:17 -0700227 result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
Nishanth Menon1c782172011-05-26 19:39:20 -0700228 if (result)
229 goto fail_table;
230
231 cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
Nishanth Menonbf2a3592011-05-26 19:39:17 -0700232
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700233 policy->min = policy->cpuinfo.min_freq;
234 policy->max = policy->cpuinfo.max_freq;
Russell King46c12212011-09-21 16:53:00 -0700235 policy->cur = omap_getspeed(policy->cpu);
236
237 /*
238 * On OMAP SMP configuartion, both processors share the voltage
239 * and clock. So both CPUs needs to be scaled together and hence
240 * needs software co-ordination. Use cpufreq affected_cpus
241 * interface to handle this scenario. Additional is_smp() check
242 * is to keep SMP_ON_UP build working.
243 */
244 if (is_smp()) {
245 policy->shared_type = CPUFREQ_SHARED_TYPE_ANY;
Todd Poynored8ce002011-06-07 13:57:52 -0700246 cpumask_setall(policy->cpus);
Russell King46c12212011-09-21 16:53:00 -0700247 }
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700248
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700249 /* FIXME: what's the actual transition time? */
Mike Turquetteb0298392009-11-11 11:00:38 -0800250 policy->cpuinfo.transition_latency = 300 * 1000;
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100251
252 return 0;
Nishanth Menon11e04fd2011-05-26 19:39:19 -0700253
Nishanth Menon1c782172011-05-26 19:39:20 -0700254fail_table:
255 freq_table_free();
Nishanth Menon11e04fd2011-05-26 19:39:19 -0700256fail_ck:
257 clk_put(mpu_clk);
258 return result;
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100259}
260
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +0300261static int omap_cpu_exit(struct cpufreq_policy *policy)
262{
Nishanth Menon1c782172011-05-26 19:39:20 -0700263 freq_table_free();
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +0300264 clk_put(mpu_clk);
265 return 0;
266}
267
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700268static struct freq_attr *omap_cpufreq_attr[] = {
269 &cpufreq_freq_attr_scaling_available_freqs,
270 NULL,
271};
272
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100273static struct cpufreq_driver omap_driver = {
274 .flags = CPUFREQ_STICKY,
275 .verify = omap_verify_speed,
276 .target = omap_target,
277 .get = omap_getspeed,
278 .init = omap_cpu_init,
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +0300279 .exit = omap_cpu_exit,
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100280 .name = "omap",
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700281 .attr = omap_cpufreq_attr,
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100282};
283
284static int __init omap_cpufreq_init(void)
285{
Nishanth Menon08ca3e3b2011-05-25 16:38:46 -0700286 if (cpu_is_omap24xx())
287 mpu_clk_name = "virt_prcm_set";
288 else if (cpu_is_omap34xx())
289 mpu_clk_name = "dpll1_ck";
290 else if (cpu_is_omap44xx())
291 mpu_clk_name = "dpll_mpu_ck";
292
293 if (!mpu_clk_name) {
294 pr_err("%s: unsupported Silicon?\n", __func__);
295 return -EINVAL;
296 }
Nishanth Menona820ffa2011-05-25 16:38:47 -0700297
Kevin Hilmanc1b547b2011-09-30 10:41:26 -0700298 mpu_dev = omap_device_get_by_hwmod_name("mpu");
Nishanth Menona820ffa2011-05-25 16:38:47 -0700299 if (!mpu_dev) {
300 pr_warning("%s: unable to get the mpu device\n", __func__);
301 return -EINVAL;
302 }
303
Kevin Hilman53dfe8a2011-07-15 15:05:04 -0700304 mpu_reg = regulator_get(mpu_dev, "vcc");
305 if (IS_ERR(mpu_reg)) {
306 pr_warning("%s: unable to get MPU regulator\n", __func__);
307 mpu_reg = NULL;
308 } else {
309 /*
310 * Ensure physical regulator is present.
311 * (e.g. could be dummy regulator.)
312 */
313 if (regulator_get_voltage(mpu_reg) < 0) {
314 pr_warn("%s: physical regulator not present for MPU\n",
315 __func__);
316 regulator_put(mpu_reg);
317 mpu_reg = NULL;
318 }
319 }
320
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100321 return cpufreq_register_driver(&omap_driver);
322}
323
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700324static void __exit omap_cpufreq_exit(void)
325{
326 cpufreq_unregister_driver(&omap_driver);
327}
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700328
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700329MODULE_DESCRIPTION("cpufreq driver for OMAP SoCs");
330MODULE_LICENSE("GPL");
331module_init(omap_cpufreq_init);
332module_exit(omap_cpufreq_exit);