blob: 5d1f5e442ecc841c38f916753c4b6f806d64de00 [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
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -070030#include <asm/smp_plat.h>
Russell King46c12212011-09-21 16:53:00 -070031#include <asm/cpu.h>
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -070032
Kevin Hilmanc1b547b2011-09-30 10:41:26 -070033#include <plat/omap_device.h>
Tony Lindgrenec6bced2005-07-10 19:58:20 +010034
Afzal Mohammed42daffd2012-02-23 19:19:24 +053035/* OPP tolerance in percentage */
36#define OPP_TOLERANCE 4
37
Kevin Hilmanaeec2992009-01-27 19:13:38 -070038static struct cpufreq_frequency_table *freq_table;
Nishanth Menon1c782172011-05-26 19:39:20 -070039static atomic_t freq_table_users = ATOMIC_INIT(0);
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +030040static struct clk *mpu_clk;
Nishanth Menona820ffa2011-05-25 16:38:47 -070041static struct device *mpu_dev;
Kevin Hilman53dfe8a2011-07-15 15:05:04 -070042static struct regulator *mpu_reg;
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +030043
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070044static int omap_verify_speed(struct cpufreq_policy *policy)
Tony Lindgrenec6bced2005-07-10 19:58:20 +010045{
Nishanth Menonbf2a3592011-05-26 19:39:17 -070046 if (!freq_table)
Tony Lindgrenec6bced2005-07-10 19:58:20 +010047 return -EINVAL;
Nishanth Menonbf2a3592011-05-26 19:39:17 -070048 return cpufreq_frequency_table_verify(policy, freq_table);
Tony Lindgrenec6bced2005-07-10 19:58:20 +010049}
50
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070051static unsigned int omap_getspeed(unsigned int cpu)
Tony Lindgrenec6bced2005-07-10 19:58:20 +010052{
Tony Lindgrenec6bced2005-07-10 19:58:20 +010053 unsigned long rate;
54
Russell King46c12212011-09-21 16:53:00 -070055 if (cpu >= NR_CPUS)
Tony Lindgrenec6bced2005-07-10 19:58:20 +010056 return 0;
57
Tony Lindgrenec6bced2005-07-10 19:58:20 +010058 rate = clk_get_rate(mpu_clk) / 1000;
Tony Lindgrenec6bced2005-07-10 19:58:20 +010059 return rate;
60}
61
62static int omap_target(struct cpufreq_policy *policy,
63 unsigned int target_freq,
64 unsigned int relation)
65{
Nishanth Menonbf2a3592011-05-26 19:39:17 -070066 unsigned int i;
Kevin Hilman53dfe8a2011-07-15 15:05:04 -070067 int r, ret = 0;
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -070068 struct cpufreq_freqs freqs;
Kevin Hilman53dfe8a2011-07-15 15:05:04 -070069 struct opp *opp;
Afzal Mohammed42daffd2012-02-23 19:19:24 +053070 unsigned long freq, volt = 0, volt_old = 0, tol = 0;
Tony Lindgrenec6bced2005-07-10 19:58:20 +010071
Nishanth Menonbf2a3592011-05-26 19:39:17 -070072 if (!freq_table) {
73 dev_err(mpu_dev, "%s: cpu%d: no freq table!\n", __func__,
74 policy->cpu);
75 return -EINVAL;
76 }
77
78 ret = cpufreq_frequency_table_target(policy, freq_table, target_freq,
79 relation, &i);
80 if (ret) {
81 dev_dbg(mpu_dev, "%s: cpu%d: no freq match for %d(ret=%d)\n",
82 __func__, policy->cpu, target_freq, ret);
83 return ret;
84 }
85 freqs.new = freq_table[i].frequency;
86 if (!freqs.new) {
87 dev_err(mpu_dev, "%s: cpu%d: no match for freq %d\n", __func__,
88 policy->cpu, target_freq);
89 return -EINVAL;
90 }
Kevin Hilmanaeec2992009-01-27 19:13:38 -070091
Russell King46c12212011-09-21 16:53:00 -070092 freqs.old = omap_getspeed(policy->cpu);
Russell King46c12212011-09-21 16:53:00 -070093 freqs.cpu = policy->cpu;
Tony Lindgrenec6bced2005-07-10 19:58:20 +010094
Colin Cross022ac032011-06-06 21:05:29 -050095 if (freqs.old == freqs.new && policy->cur == freqs.new)
Kevin Hilmanaeec2992009-01-27 19:13:38 -070096 return ret;
97
Russell King46c12212011-09-21 16:53:00 -070098 /* notifiers */
99 for_each_cpu(i, policy->cpus) {
100 freqs.cpu = i;
101 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
102 }
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700103
Kevin Hilman53dfe8a2011-07-15 15:05:04 -0700104 freq = freqs.new * 1000;
Kevin Hilman8df0a662012-10-02 15:39:03 -0700105 ret = clk_round_rate(mpu_clk, freq);
106 if (IS_ERR_VALUE(ret)) {
107 dev_warn(mpu_dev,
108 "CPUfreq: Cannot find matching frequency for %lu\n",
109 freq);
110 return ret;
111 }
112 freq = ret;
Kevin Hilman53dfe8a2011-07-15 15:05:04 -0700113
114 if (mpu_reg) {
115 opp = opp_find_freq_ceil(mpu_dev, &freq);
116 if (IS_ERR(opp)) {
117 dev_err(mpu_dev, "%s: unable to find MPU OPP for %d\n",
118 __func__, freqs.new);
119 return -EINVAL;
120 }
121 volt = opp_get_voltage(opp);
Afzal Mohammed42daffd2012-02-23 19:19:24 +0530122 tol = volt * OPP_TOLERANCE / 100;
Kevin Hilman53dfe8a2011-07-15 15:05:04 -0700123 volt_old = regulator_get_voltage(mpu_reg);
124 }
125
126 dev_dbg(mpu_dev, "cpufreq-omap: %u MHz, %ld mV --> %u MHz, %ld mV\n",
127 freqs.old / 1000, volt_old ? volt_old / 1000 : -1,
128 freqs.new / 1000, volt ? volt / 1000 : -1);
129
130 /* scaling up? scale voltage before frequency */
131 if (mpu_reg && (freqs.new > freqs.old)) {
Afzal Mohammed42daffd2012-02-23 19:19:24 +0530132 r = regulator_set_voltage(mpu_reg, volt - tol, volt + tol);
Kevin Hilman53dfe8a2011-07-15 15:05:04 -0700133 if (r < 0) {
134 dev_warn(mpu_dev, "%s: unable to scale voltage up.\n",
135 __func__);
136 freqs.new = freqs.old;
137 goto done;
138 }
139 }
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700140
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700141 ret = clk_set_rate(mpu_clk, freqs.new * 1000);
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700142
Kevin Hilman53dfe8a2011-07-15 15:05:04 -0700143 /* scaling down? scale voltage after frequency */
144 if (mpu_reg && (freqs.new < freqs.old)) {
Afzal Mohammed42daffd2012-02-23 19:19:24 +0530145 r = regulator_set_voltage(mpu_reg, volt - tol, volt + tol);
Kevin Hilman53dfe8a2011-07-15 15:05:04 -0700146 if (r < 0) {
147 dev_warn(mpu_dev, "%s: unable to scale voltage down.\n",
148 __func__);
149 ret = clk_set_rate(mpu_clk, freqs.old * 1000);
150 freqs.new = freqs.old;
151 goto done;
152 }
153 }
154
155 freqs.new = omap_getspeed(policy->cpu);
Russell King46c12212011-09-21 16:53:00 -0700156
Kevin Hilman53dfe8a2011-07-15 15:05:04 -0700157done:
Russell King46c12212011-09-21 16:53:00 -0700158 /* notifiers */
159 for_each_cpu(i, policy->cpus) {
160 freqs.cpu = i;
161 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
162 }
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100163
164 return ret;
165}
166
Nishanth Menon1c782172011-05-26 19:39:20 -0700167static inline void freq_table_free(void)
168{
169 if (atomic_dec_and_test(&freq_table_users))
170 opp_free_cpufreq_table(mpu_dev, &freq_table);
171}
172
Ming Lei790ab7e2011-03-03 06:41:46 +0800173static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100174{
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700175 int result = 0;
176
Paul Walmsleye2ee1b42012-09-07 18:16:35 +0000177 mpu_clk = clk_get(NULL, "cpufreq_ck");
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100178 if (IS_ERR(mpu_clk))
179 return PTR_ERR(mpu_clk);
180
Nishanth Menon11e04fd2011-05-26 19:39:19 -0700181 if (policy->cpu >= NR_CPUS) {
182 result = -EINVAL;
183 goto fail_ck;
184 }
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700185
Russell King46c12212011-09-21 16:53:00 -0700186 policy->cur = policy->min = policy->max = omap_getspeed(policy->cpu);
Nishanth Menon1c782172011-05-26 19:39:20 -0700187
Rajendra Nayak1b865212012-08-09 12:38:21 +0530188 if (!freq_table)
Nishanth Menon1c782172011-05-26 19:39:20 -0700189 result = opp_init_cpufreq_table(mpu_dev, &freq_table);
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700190
Nishanth Menonbf2a3592011-05-26 19:39:17 -0700191 if (result) {
192 dev_err(mpu_dev, "%s: cpu%d: failed creating freq table[%d]\n",
193 __func__, policy->cpu, result);
Nishanth Menon11e04fd2011-05-26 19:39:19 -0700194 goto fail_ck;
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700195 }
196
Rajendra Nayak1b865212012-08-09 12:38:21 +0530197 atomic_inc_return(&freq_table_users);
198
Nishanth Menonbf2a3592011-05-26 19:39:17 -0700199 result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
Nishanth Menon1c782172011-05-26 19:39:20 -0700200 if (result)
201 goto fail_table;
202
203 cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
Nishanth Menonbf2a3592011-05-26 19:39:17 -0700204
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700205 policy->min = policy->cpuinfo.min_freq;
206 policy->max = policy->cpuinfo.max_freq;
Russell King46c12212011-09-21 16:53:00 -0700207 policy->cur = omap_getspeed(policy->cpu);
208
209 /*
210 * On OMAP SMP configuartion, both processors share the voltage
211 * and clock. So both CPUs needs to be scaled together and hence
212 * needs software co-ordination. Use cpufreq affected_cpus
213 * interface to handle this scenario. Additional is_smp() check
214 * is to keep SMP_ON_UP build working.
215 */
216 if (is_smp()) {
217 policy->shared_type = CPUFREQ_SHARED_TYPE_ANY;
Todd Poynored8ce002011-06-07 13:57:52 -0700218 cpumask_setall(policy->cpus);
Russell King46c12212011-09-21 16:53:00 -0700219 }
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700220
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700221 /* FIXME: what's the actual transition time? */
Mike Turquetteb0298392009-11-11 11:00:38 -0800222 policy->cpuinfo.transition_latency = 300 * 1000;
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100223
224 return 0;
Nishanth Menon11e04fd2011-05-26 19:39:19 -0700225
Nishanth Menon1c782172011-05-26 19:39:20 -0700226fail_table:
227 freq_table_free();
Nishanth Menon11e04fd2011-05-26 19:39:19 -0700228fail_ck:
229 clk_put(mpu_clk);
230 return result;
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100231}
232
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +0300233static int omap_cpu_exit(struct cpufreq_policy *policy)
234{
Nishanth Menon1c782172011-05-26 19:39:20 -0700235 freq_table_free();
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +0300236 clk_put(mpu_clk);
237 return 0;
238}
239
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700240static struct freq_attr *omap_cpufreq_attr[] = {
241 &cpufreq_freq_attr_scaling_available_freqs,
242 NULL,
243};
244
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100245static struct cpufreq_driver omap_driver = {
246 .flags = CPUFREQ_STICKY,
247 .verify = omap_verify_speed,
248 .target = omap_target,
249 .get = omap_getspeed,
250 .init = omap_cpu_init,
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +0300251 .exit = omap_cpu_exit,
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100252 .name = "omap",
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700253 .attr = omap_cpufreq_attr,
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100254};
255
256static int __init omap_cpufreq_init(void)
257{
Kevin Hilmanc1b547b2011-09-30 10:41:26 -0700258 mpu_dev = omap_device_get_by_hwmod_name("mpu");
Axel Lin1bae9952012-09-19 22:00:10 +0200259 if (IS_ERR(mpu_dev)) {
Nishanth Menona820ffa2011-05-25 16:38:47 -0700260 pr_warning("%s: unable to get the mpu device\n", __func__);
Axel Lin1bae9952012-09-19 22:00:10 +0200261 return PTR_ERR(mpu_dev);
Nishanth Menona820ffa2011-05-25 16:38:47 -0700262 }
263
Kevin Hilman53dfe8a2011-07-15 15:05:04 -0700264 mpu_reg = regulator_get(mpu_dev, "vcc");
265 if (IS_ERR(mpu_reg)) {
266 pr_warning("%s: unable to get MPU regulator\n", __func__);
267 mpu_reg = NULL;
268 } else {
269 /*
270 * Ensure physical regulator is present.
271 * (e.g. could be dummy regulator.)
272 */
273 if (regulator_get_voltage(mpu_reg) < 0) {
274 pr_warn("%s: physical regulator not present for MPU\n",
275 __func__);
276 regulator_put(mpu_reg);
277 mpu_reg = NULL;
278 }
279 }
280
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100281 return cpufreq_register_driver(&omap_driver);
282}
283
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700284static void __exit omap_cpufreq_exit(void)
285{
286 cpufreq_unregister_driver(&omap_driver);
287}
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700288
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700289MODULE_DESCRIPTION("cpufreq driver for OMAP SoCs");
290MODULE_LICENSE("GPL");
291module_init(omap_cpufreq_init);
292module_exit(omap_cpufreq_exit);