blob: a6b2be7ea5a9bb9f0d961aa37f964da73d004259 [file] [log] [blame]
Tony Lindgrenec6bced2005-07-10 19:58:20 +01001/*
Tony Lindgrenec6bced2005-07-10 19:58:20 +01002 * CPU frequency scaling for OMAP
3 *
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>
26
27#include <asm/system.h>
28#include <asm/smp_plat.h>
29
30#include <plat/clock.h>
31#include <plat/omap-pm.h>
32#include <plat/common.h>
Tony Lindgrenec6bced2005-07-10 19:58:20 +010033
Russell Kinga09e64f2008-08-05 16:14:15 +010034#include <mach/hardware.h>
Tony Lindgrenec6bced2005-07-10 19:58:20 +010035
Tony Lindgrena7ca9d22006-06-26 16:16:17 -070036#define VERY_HI_RATE 900000000
37
Kevin Hilmanaeec2992009-01-27 19:13:38 -070038static struct cpufreq_frequency_table *freq_table;
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +030039static struct clk *mpu_clk;
40
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070041static int omap_verify_speed(struct cpufreq_policy *policy)
Tony Lindgrenec6bced2005-07-10 19:58:20 +010042{
Kevin Hilmanaeec2992009-01-27 19:13:38 -070043 if (freq_table)
44 return cpufreq_frequency_table_verify(policy, freq_table);
45
Tony Lindgrenec6bced2005-07-10 19:58:20 +010046 if (policy->cpu)
47 return -EINVAL;
48
49 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
50 policy->cpuinfo.max_freq);
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +030051
Tony Lindgrenec6bced2005-07-10 19:58:20 +010052 policy->min = clk_round_rate(mpu_clk, policy->min * 1000) / 1000;
53 policy->max = clk_round_rate(mpu_clk, policy->max * 1000) / 1000;
54 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
55 policy->cpuinfo.max_freq);
Tony Lindgrenec6bced2005-07-10 19:58:20 +010056 return 0;
57}
58
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070059static unsigned int omap_getspeed(unsigned int cpu)
Tony Lindgrenec6bced2005-07-10 19:58:20 +010060{
Tony Lindgrenec6bced2005-07-10 19:58:20 +010061 unsigned long rate;
62
63 if (cpu)
64 return 0;
65
Tony Lindgrenec6bced2005-07-10 19:58:20 +010066 rate = clk_get_rate(mpu_clk) / 1000;
Tony Lindgrenec6bced2005-07-10 19:58:20 +010067 return rate;
68}
69
70static int omap_target(struct cpufreq_policy *policy,
71 unsigned int target_freq,
72 unsigned int relation)
73{
Tony Lindgrenec6bced2005-07-10 19:58:20 +010074 int ret = 0;
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -070075 struct cpufreq_freqs freqs;
Tony Lindgrenec6bced2005-07-10 19:58:20 +010076
Kevin Hilmanaeec2992009-01-27 19:13:38 -070077 /* Ensure desired rate is within allowed range. Some govenors
78 * (ondemand) will just pass target_freq=0 to get the minimum. */
Eero Nurkkala60c45ae2009-06-23 12:53:29 +030079 if (target_freq < policy->min)
80 target_freq = policy->min;
81 if (target_freq > policy->max)
82 target_freq = policy->max;
Kevin Hilmanaeec2992009-01-27 19:13:38 -070083
Tony Lindgrenec6bced2005-07-10 19:58:20 +010084 freqs.old = omap_getspeed(0);
85 freqs.new = clk_round_rate(mpu_clk, target_freq * 1000) / 1000;
86 freqs.cpu = 0;
87
Kevin Hilmanaeec2992009-01-27 19:13:38 -070088 if (freqs.old == freqs.new)
89 return ret;
90
Tony Lindgrenec6bced2005-07-10 19:58:20 +010091 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -070092
Kevin Hilmanaeec2992009-01-27 19:13:38 -070093#ifdef CONFIG_CPU_FREQ_DEBUG
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -070094 pr_info("cpufreq-omap: transition: %u --> %u\n", freqs.old, freqs.new);
Kevin Hilmanaeec2992009-01-27 19:13:38 -070095#endif
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -070096
Kevin Hilmanaeec2992009-01-27 19:13:38 -070097 ret = clk_set_rate(mpu_clk, freqs.new * 1000);
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -070098
Tony Lindgrenec6bced2005-07-10 19:58:20 +010099 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100100
101 return ret;
102}
103
Ming Lei790ab7e2011-03-03 06:41:46 +0800104static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100105{
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700106 int result = 0;
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700107 struct device *mpu_dev;
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700108
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700109 if (cpu_is_omap24xx())
110 mpu_clk = clk_get(NULL, "virt_prcm_set");
111 else if (cpu_is_omap34xx())
112 mpu_clk = clk_get(NULL, "dpll1_ck");
113 else if (cpu_is_omap44xx())
114 mpu_clk = clk_get(NULL, "dpll_mpu_ck");
115
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100116 if (IS_ERR(mpu_clk))
117 return PTR_ERR(mpu_clk);
118
119 if (policy->cpu != 0)
120 return -EINVAL;
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700121
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100122 policy->cur = policy->min = policy->max = omap_getspeed(0);
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700123
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700124 mpu_dev = omap2_get_mpuss_device();
125 if (!mpu_dev) {
126 pr_warning("%s: unable to get the mpu device\n", __func__);
127 return -EINVAL;
128 }
129 opp_init_cpufreq_table(mpu_dev, &freq_table);
130
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700131 if (freq_table) {
132 result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
133 if (!result)
134 cpufreq_frequency_table_get_attr(freq_table,
135 policy->cpu);
136 } else {
137 policy->cpuinfo.min_freq = clk_round_rate(mpu_clk, 0) / 1000;
138 policy->cpuinfo.max_freq = clk_round_rate(mpu_clk,
139 VERY_HI_RATE) / 1000;
140 }
141
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700142 policy->min = policy->cpuinfo.min_freq;
143 policy->max = policy->cpuinfo.max_freq;
144 policy->cur = omap_getspeed(0);
145
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700146 /* FIXME: what's the actual transition time? */
Mike Turquetteb0298392009-11-11 11:00:38 -0800147 policy->cpuinfo.transition_latency = 300 * 1000;
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100148
149 return 0;
150}
151
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +0300152static int omap_cpu_exit(struct cpufreq_policy *policy)
153{
Paul Walmsley4e37c102010-01-08 15:23:16 -0700154 clk_exit_cpufreq_table(&freq_table);
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +0300155 clk_put(mpu_clk);
156 return 0;
157}
158
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700159static struct freq_attr *omap_cpufreq_attr[] = {
160 &cpufreq_freq_attr_scaling_available_freqs,
161 NULL,
162};
163
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100164static struct cpufreq_driver omap_driver = {
165 .flags = CPUFREQ_STICKY,
166 .verify = omap_verify_speed,
167 .target = omap_target,
168 .get = omap_getspeed,
169 .init = omap_cpu_init,
Hiroshi DOYUb8488fb2007-08-30 12:46:39 +0300170 .exit = omap_cpu_exit,
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100171 .name = "omap",
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700172 .attr = omap_cpufreq_attr,
Tony Lindgrenec6bced2005-07-10 19:58:20 +0100173};
174
175static int __init omap_cpufreq_init(void)
176{
177 return cpufreq_register_driver(&omap_driver);
178}
179
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700180static void __exit omap_cpufreq_exit(void)
181{
182 cpufreq_unregister_driver(&omap_driver);
183}
Kevin Hilmanaeec2992009-01-27 19:13:38 -0700184
Santosh Shilimkar731e0cc2010-08-11 17:02:43 -0700185MODULE_DESCRIPTION("cpufreq driver for OMAP SoCs");
186MODULE_LICENSE("GPL");
187module_init(omap_cpufreq_init);
188module_exit(omap_cpufreq_exit);