Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 1 | /* |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 2 | * 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 Shilimkar | 731e0cc | 2010-08-11 17:02:43 -0700 | [diff] [blame^] | 9 | * Copyright (C) 2007-2011 Texas Instruments, Inc. |
| 10 | * - OMAP3/4 support by Rajendra Nayak, Santosh Shilimkar |
| 11 | * |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 12 | * 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 King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 23 | #include <linux/clk.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 24 | #include <linux/io.h> |
Santosh Shilimkar | 731e0cc | 2010-08-11 17:02:43 -0700 | [diff] [blame^] | 25 | #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 Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 33 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 34 | #include <mach/hardware.h> |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 35 | |
Tony Lindgren | a7ca9d2 | 2006-06-26 16:16:17 -0700 | [diff] [blame] | 36 | #define VERY_HI_RATE 900000000 |
| 37 | |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 38 | static struct cpufreq_frequency_table *freq_table; |
Hiroshi DOYU | b8488fb | 2007-08-30 12:46:39 +0300 | [diff] [blame] | 39 | static struct clk *mpu_clk; |
| 40 | |
Manjunath Kondaiah G | b0a330d | 2010-10-08 10:00:19 -0700 | [diff] [blame] | 41 | static int omap_verify_speed(struct cpufreq_policy *policy) |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 42 | { |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 43 | if (freq_table) |
| 44 | return cpufreq_frequency_table_verify(policy, freq_table); |
| 45 | |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 46 | if (policy->cpu) |
| 47 | return -EINVAL; |
| 48 | |
| 49 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, |
| 50 | policy->cpuinfo.max_freq); |
Hiroshi DOYU | b8488fb | 2007-08-30 12:46:39 +0300 | [diff] [blame] | 51 | |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 52 | 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 Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
Manjunath Kondaiah G | b0a330d | 2010-10-08 10:00:19 -0700 | [diff] [blame] | 59 | static unsigned int omap_getspeed(unsigned int cpu) |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 60 | { |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 61 | unsigned long rate; |
| 62 | |
| 63 | if (cpu) |
| 64 | return 0; |
| 65 | |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 66 | rate = clk_get_rate(mpu_clk) / 1000; |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 67 | return rate; |
| 68 | } |
| 69 | |
| 70 | static int omap_target(struct cpufreq_policy *policy, |
| 71 | unsigned int target_freq, |
| 72 | unsigned int relation) |
| 73 | { |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 74 | int ret = 0; |
Santosh Shilimkar | 731e0cc | 2010-08-11 17:02:43 -0700 | [diff] [blame^] | 75 | struct cpufreq_freqs freqs; |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 76 | |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 77 | /* Ensure desired rate is within allowed range. Some govenors |
| 78 | * (ondemand) will just pass target_freq=0 to get the minimum. */ |
Eero Nurkkala | 60c45ae | 2009-06-23 12:53:29 +0300 | [diff] [blame] | 79 | if (target_freq < policy->min) |
| 80 | target_freq = policy->min; |
| 81 | if (target_freq > policy->max) |
| 82 | target_freq = policy->max; |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 83 | |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 84 | freqs.old = omap_getspeed(0); |
| 85 | freqs.new = clk_round_rate(mpu_clk, target_freq * 1000) / 1000; |
| 86 | freqs.cpu = 0; |
| 87 | |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 88 | if (freqs.old == freqs.new) |
| 89 | return ret; |
| 90 | |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 91 | cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); |
Santosh Shilimkar | 731e0cc | 2010-08-11 17:02:43 -0700 | [diff] [blame^] | 92 | |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 93 | #ifdef CONFIG_CPU_FREQ_DEBUG |
Santosh Shilimkar | 731e0cc | 2010-08-11 17:02:43 -0700 | [diff] [blame^] | 94 | pr_info("cpufreq-omap: transition: %u --> %u\n", freqs.old, freqs.new); |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 95 | #endif |
Santosh Shilimkar | 731e0cc | 2010-08-11 17:02:43 -0700 | [diff] [blame^] | 96 | |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 97 | ret = clk_set_rate(mpu_clk, freqs.new * 1000); |
Santosh Shilimkar | 731e0cc | 2010-08-11 17:02:43 -0700 | [diff] [blame^] | 98 | |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 99 | cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
Ming Lei | 790ab7e | 2011-03-03 06:41:46 +0800 | [diff] [blame] | 104 | static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy) |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 105 | { |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 106 | int result = 0; |
Santosh Shilimkar | 731e0cc | 2010-08-11 17:02:43 -0700 | [diff] [blame^] | 107 | struct device *mpu_dev; |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 108 | |
Santosh Shilimkar | 731e0cc | 2010-08-11 17:02:43 -0700 | [diff] [blame^] | 109 | 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 Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 116 | if (IS_ERR(mpu_clk)) |
| 117 | return PTR_ERR(mpu_clk); |
| 118 | |
| 119 | if (policy->cpu != 0) |
| 120 | return -EINVAL; |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 121 | |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 122 | policy->cur = policy->min = policy->max = omap_getspeed(0); |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 123 | |
Santosh Shilimkar | 731e0cc | 2010-08-11 17:02:43 -0700 | [diff] [blame^] | 124 | 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 Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 131 | 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 Shilimkar | 731e0cc | 2010-08-11 17:02:43 -0700 | [diff] [blame^] | 142 | policy->min = policy->cpuinfo.min_freq; |
| 143 | policy->max = policy->cpuinfo.max_freq; |
| 144 | policy->cur = omap_getspeed(0); |
| 145 | |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 146 | /* FIXME: what's the actual transition time? */ |
Mike Turquette | b029839 | 2009-11-11 11:00:38 -0800 | [diff] [blame] | 147 | policy->cpuinfo.transition_latency = 300 * 1000; |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Hiroshi DOYU | b8488fb | 2007-08-30 12:46:39 +0300 | [diff] [blame] | 152 | static int omap_cpu_exit(struct cpufreq_policy *policy) |
| 153 | { |
Paul Walmsley | 4e37c10 | 2010-01-08 15:23:16 -0700 | [diff] [blame] | 154 | clk_exit_cpufreq_table(&freq_table); |
Hiroshi DOYU | b8488fb | 2007-08-30 12:46:39 +0300 | [diff] [blame] | 155 | clk_put(mpu_clk); |
| 156 | return 0; |
| 157 | } |
| 158 | |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 159 | static struct freq_attr *omap_cpufreq_attr[] = { |
| 160 | &cpufreq_freq_attr_scaling_available_freqs, |
| 161 | NULL, |
| 162 | }; |
| 163 | |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 164 | static 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 DOYU | b8488fb | 2007-08-30 12:46:39 +0300 | [diff] [blame] | 170 | .exit = omap_cpu_exit, |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 171 | .name = "omap", |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 172 | .attr = omap_cpufreq_attr, |
Tony Lindgren | ec6bced | 2005-07-10 19:58:20 +0100 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | static int __init omap_cpufreq_init(void) |
| 176 | { |
| 177 | return cpufreq_register_driver(&omap_driver); |
| 178 | } |
| 179 | |
Santosh Shilimkar | 731e0cc | 2010-08-11 17:02:43 -0700 | [diff] [blame^] | 180 | static void __exit omap_cpufreq_exit(void) |
| 181 | { |
| 182 | cpufreq_unregister_driver(&omap_driver); |
| 183 | } |
Kevin Hilman | aeec299 | 2009-01-27 19:13:38 -0700 | [diff] [blame] | 184 | |
Santosh Shilimkar | 731e0cc | 2010-08-11 17:02:43 -0700 | [diff] [blame^] | 185 | MODULE_DESCRIPTION("cpufreq driver for OMAP SoCs"); |
| 186 | MODULE_LICENSE("GPL"); |
| 187 | module_init(omap_cpufreq_init); |
| 188 | module_exit(omap_cpufreq_exit); |