Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 1 | /* |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 2 | * Copyright (C) 2010 Google, Inc. |
| 3 | * |
| 4 | * Author: |
| 5 | * Colin Cross <ccross@google.com> |
| 6 | * Based on arch/arm/plat-omap/cpu-omap.c, (C) 2005 Nokia Corporation |
| 7 | * |
| 8 | * This software is licensed under the terms of the GNU General Public |
| 9 | * License version 2, as published by the Free Software Foundation, and |
| 10 | * may be copied, distributed, and modified under those terms. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/cpufreq.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/clk.h> |
| 28 | #include <linux/io.h> |
Colin Cross | 1eb2ecf | 2010-08-05 17:40:39 -0700 | [diff] [blame] | 29 | #include <linux/suspend.h> |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 30 | |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 31 | /* Frequency table index must be sequential starting at 0 */ |
| 32 | static struct cpufreq_frequency_table freq_table[] = { |
Colin Cross | 1eb2ecf | 2010-08-05 17:40:39 -0700 | [diff] [blame] | 33 | { 0, 216000 }, |
| 34 | { 1, 312000 }, |
| 35 | { 2, 456000 }, |
| 36 | { 3, 608000 }, |
| 37 | { 4, 760000 }, |
| 38 | { 5, 816000 }, |
| 39 | { 6, 912000 }, |
| 40 | { 7, 1000000 }, |
| 41 | { 8, CPUFREQ_TABLE_END }, |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | #define NUM_CPUS 2 |
| 45 | |
| 46 | static struct clk *cpu_clk; |
Stephen Warren | ce32dda | 2012-09-10 17:05:01 -0600 | [diff] [blame] | 47 | static struct clk *pll_x_clk; |
| 48 | static struct clk *pll_p_clk; |
Colin Cross | 7a28128 | 2010-11-22 18:54:36 -0800 | [diff] [blame] | 49 | static struct clk *emc_clk; |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 50 | |
| 51 | static unsigned long target_cpu_speed[NUM_CPUS]; |
Colin Cross | 1eb2ecf | 2010-08-05 17:40:39 -0700 | [diff] [blame] | 52 | static DEFINE_MUTEX(tegra_cpu_lock); |
| 53 | static bool is_suspended; |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 54 | |
Olof Johansson | 6686c73 | 2011-10-09 21:57:04 -0700 | [diff] [blame] | 55 | static int tegra_verify_speed(struct cpufreq_policy *policy) |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 56 | { |
| 57 | return cpufreq_frequency_table_verify(policy, freq_table); |
| 58 | } |
| 59 | |
Olof Johansson | 6686c73 | 2011-10-09 21:57:04 -0700 | [diff] [blame] | 60 | static unsigned int tegra_getspeed(unsigned int cpu) |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 61 | { |
| 62 | unsigned long rate; |
| 63 | |
| 64 | if (cpu >= NUM_CPUS) |
| 65 | return 0; |
| 66 | |
| 67 | rate = clk_get_rate(cpu_clk) / 1000; |
| 68 | return rate; |
| 69 | } |
| 70 | |
Stephen Warren | ce32dda | 2012-09-10 17:05:01 -0600 | [diff] [blame] | 71 | static int tegra_cpu_clk_set_rate(unsigned long rate) |
| 72 | { |
| 73 | int ret; |
| 74 | |
| 75 | /* |
| 76 | * Take an extra reference to the main pll so it doesn't turn |
| 77 | * off when we move the cpu off of it |
| 78 | */ |
| 79 | clk_prepare_enable(pll_x_clk); |
| 80 | |
| 81 | ret = clk_set_parent(cpu_clk, pll_p_clk); |
| 82 | if (ret) { |
| 83 | pr_err("Failed to switch cpu to clock pll_p\n"); |
| 84 | goto out; |
| 85 | } |
| 86 | |
| 87 | if (rate == clk_get_rate(pll_p_clk)) |
| 88 | goto out; |
| 89 | |
| 90 | ret = clk_set_rate(pll_x_clk, rate); |
| 91 | if (ret) { |
| 92 | pr_err("Failed to change pll_x to %lu\n", rate); |
| 93 | goto out; |
| 94 | } |
| 95 | |
| 96 | ret = clk_set_parent(cpu_clk, pll_x_clk); |
| 97 | if (ret) { |
| 98 | pr_err("Failed to switch cpu to clock pll_x\n"); |
| 99 | goto out; |
| 100 | } |
| 101 | |
| 102 | out: |
| 103 | clk_disable_unprepare(pll_x_clk); |
| 104 | return ret; |
| 105 | } |
| 106 | |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 107 | static int tegra_update_cpu_speed(struct cpufreq_policy *policy, |
| 108 | unsigned long rate) |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 109 | { |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 110 | int ret = 0; |
| 111 | struct cpufreq_freqs freqs; |
| 112 | |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 113 | freqs.old = tegra_getspeed(0); |
| 114 | freqs.new = rate; |
| 115 | |
| 116 | if (freqs.old == freqs.new) |
| 117 | return ret; |
| 118 | |
Colin Cross | 7a28128 | 2010-11-22 18:54:36 -0800 | [diff] [blame] | 119 | /* |
| 120 | * Vote on memory bus frequency based on cpu frequency |
| 121 | * This sets the minimum frequency, display or avp may request higher |
| 122 | */ |
| 123 | if (rate >= 816000) |
| 124 | clk_set_rate(emc_clk, 600000000); /* cpu 816 MHz, emc max */ |
| 125 | else if (rate >= 456000) |
| 126 | clk_set_rate(emc_clk, 300000000); /* cpu 456 MHz, emc 150Mhz */ |
| 127 | else |
| 128 | clk_set_rate(emc_clk, 100000000); /* emc 50Mhz */ |
| 129 | |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 130 | cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE); |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 131 | |
| 132 | #ifdef CONFIG_CPU_FREQ_DEBUG |
| 133 | printk(KERN_DEBUG "cpufreq-tegra: transition: %u --> %u\n", |
| 134 | freqs.old, freqs.new); |
| 135 | #endif |
| 136 | |
Stephen Warren | ce32dda | 2012-09-10 17:05:01 -0600 | [diff] [blame] | 137 | ret = tegra_cpu_clk_set_rate(freqs.new * 1000); |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 138 | if (ret) { |
| 139 | pr_err("cpu-tegra: Failed to set cpu frequency to %d kHz\n", |
| 140 | freqs.new); |
Viresh Kumar | f56cc99 | 2013-06-19 11:18:20 +0530 | [diff] [blame^] | 141 | freqs.new = freqs.old; |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 144 | cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE); |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 145 | |
Viresh Kumar | f56cc99 | 2013-06-19 11:18:20 +0530 | [diff] [blame^] | 146 | return ret; |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Colin Cross | 1eb2ecf | 2010-08-05 17:40:39 -0700 | [diff] [blame] | 149 | static unsigned long tegra_cpu_highest_speed(void) |
| 150 | { |
| 151 | unsigned long rate = 0; |
| 152 | int i; |
| 153 | |
| 154 | for_each_online_cpu(i) |
| 155 | rate = max(rate, target_cpu_speed[i]); |
| 156 | return rate; |
| 157 | } |
| 158 | |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 159 | static int tegra_target(struct cpufreq_policy *policy, |
| 160 | unsigned int target_freq, |
| 161 | unsigned int relation) |
| 162 | { |
Olof Johansson | fdb684a | 2011-10-09 21:31:23 -0700 | [diff] [blame] | 163 | unsigned int idx; |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 164 | unsigned int freq; |
Colin Cross | 1eb2ecf | 2010-08-05 17:40:39 -0700 | [diff] [blame] | 165 | int ret = 0; |
| 166 | |
| 167 | mutex_lock(&tegra_cpu_lock); |
| 168 | |
| 169 | if (is_suspended) { |
| 170 | ret = -EBUSY; |
| 171 | goto out; |
| 172 | } |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 173 | |
| 174 | cpufreq_frequency_table_target(policy, freq_table, target_freq, |
| 175 | relation, &idx); |
| 176 | |
| 177 | freq = freq_table[idx].frequency; |
| 178 | |
| 179 | target_cpu_speed[policy->cpu] = freq; |
| 180 | |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 181 | ret = tegra_update_cpu_speed(policy, tegra_cpu_highest_speed()); |
Colin Cross | 1eb2ecf | 2010-08-05 17:40:39 -0700 | [diff] [blame] | 182 | |
| 183 | out: |
| 184 | mutex_unlock(&tegra_cpu_lock); |
| 185 | return ret; |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Colin Cross | 1eb2ecf | 2010-08-05 17:40:39 -0700 | [diff] [blame] | 188 | static int tegra_pm_notify(struct notifier_block *nb, unsigned long event, |
| 189 | void *dummy) |
| 190 | { |
| 191 | mutex_lock(&tegra_cpu_lock); |
| 192 | if (event == PM_SUSPEND_PREPARE) { |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 193 | struct cpufreq_policy *policy = cpufreq_cpu_get(0); |
Colin Cross | 1eb2ecf | 2010-08-05 17:40:39 -0700 | [diff] [blame] | 194 | is_suspended = true; |
| 195 | pr_info("Tegra cpufreq suspend: setting frequency to %d kHz\n", |
| 196 | freq_table[0].frequency); |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 197 | tegra_update_cpu_speed(policy, freq_table[0].frequency); |
| 198 | cpufreq_cpu_put(policy); |
Colin Cross | 1eb2ecf | 2010-08-05 17:40:39 -0700 | [diff] [blame] | 199 | } else if (event == PM_POST_SUSPEND) { |
| 200 | is_suspended = false; |
| 201 | } |
| 202 | mutex_unlock(&tegra_cpu_lock); |
| 203 | |
| 204 | return NOTIFY_OK; |
| 205 | } |
| 206 | |
| 207 | static struct notifier_block tegra_cpu_pm_notifier = { |
| 208 | .notifier_call = tegra_pm_notify, |
| 209 | }; |
| 210 | |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 211 | static int tegra_cpu_init(struct cpufreq_policy *policy) |
| 212 | { |
| 213 | if (policy->cpu >= NUM_CPUS) |
| 214 | return -EINVAL; |
| 215 | |
Prashant Gaikwad | 6a5278d | 2012-06-05 09:59:35 +0530 | [diff] [blame] | 216 | clk_prepare_enable(emc_clk); |
| 217 | clk_prepare_enable(cpu_clk); |
Colin Cross | 89a5fb8 | 2010-10-20 17:47:59 -0700 | [diff] [blame] | 218 | |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 219 | cpufreq_frequency_table_cpuinfo(policy, freq_table); |
| 220 | cpufreq_frequency_table_get_attr(freq_table, policy->cpu); |
| 221 | policy->cur = tegra_getspeed(policy->cpu); |
| 222 | target_cpu_speed[policy->cpu] = policy->cur; |
| 223 | |
| 224 | /* FIXME: what's the actual transition time? */ |
| 225 | policy->cpuinfo.transition_latency = 300 * 1000; |
| 226 | |
Viresh Kumar | 16a44f8 | 2013-02-01 06:40:00 +0000 | [diff] [blame] | 227 | cpumask_copy(policy->cpus, cpu_possible_mask); |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 228 | |
Colin Cross | 1eb2ecf | 2010-08-05 17:40:39 -0700 | [diff] [blame] | 229 | if (policy->cpu == 0) |
| 230 | register_pm_notifier(&tegra_cpu_pm_notifier); |
| 231 | |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static int tegra_cpu_exit(struct cpufreq_policy *policy) |
| 236 | { |
| 237 | cpufreq_frequency_table_cpuinfo(policy, freq_table); |
Prashant Gaikwad | 6a5278d | 2012-06-05 09:59:35 +0530 | [diff] [blame] | 238 | clk_disable_unprepare(emc_clk); |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static struct freq_attr *tegra_cpufreq_attr[] = { |
| 243 | &cpufreq_freq_attr_scaling_available_freqs, |
| 244 | NULL, |
| 245 | }; |
| 246 | |
| 247 | static struct cpufreq_driver tegra_cpufreq_driver = { |
| 248 | .verify = tegra_verify_speed, |
| 249 | .target = tegra_target, |
| 250 | .get = tegra_getspeed, |
| 251 | .init = tegra_cpu_init, |
| 252 | .exit = tegra_cpu_exit, |
| 253 | .name = "tegra", |
| 254 | .attr = tegra_cpufreq_attr, |
| 255 | }; |
| 256 | |
| 257 | static int __init tegra_cpufreq_init(void) |
| 258 | { |
Richard Zhao | c26cefd | 2012-12-21 00:09:55 +0000 | [diff] [blame] | 259 | cpu_clk = clk_get_sys(NULL, "cpu"); |
| 260 | if (IS_ERR(cpu_clk)) |
| 261 | return PTR_ERR(cpu_clk); |
| 262 | |
| 263 | pll_x_clk = clk_get_sys(NULL, "pll_x"); |
| 264 | if (IS_ERR(pll_x_clk)) |
| 265 | return PTR_ERR(pll_x_clk); |
| 266 | |
Prashant Gaikwad | 61fd290 | 2013-01-11 13:16:26 +0530 | [diff] [blame] | 267 | pll_p_clk = clk_get_sys(NULL, "pll_p_cclk"); |
Richard Zhao | c26cefd | 2012-12-21 00:09:55 +0000 | [diff] [blame] | 268 | if (IS_ERR(pll_p_clk)) |
| 269 | return PTR_ERR(pll_p_clk); |
| 270 | |
| 271 | emc_clk = clk_get_sys("cpu", "emc"); |
| 272 | if (IS_ERR(emc_clk)) { |
| 273 | clk_put(cpu_clk); |
| 274 | return PTR_ERR(emc_clk); |
| 275 | } |
| 276 | |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 277 | return cpufreq_register_driver(&tegra_cpufreq_driver); |
| 278 | } |
| 279 | |
| 280 | static void __exit tegra_cpufreq_exit(void) |
| 281 | { |
| 282 | cpufreq_unregister_driver(&tegra_cpufreq_driver); |
Richard Zhao | c26cefd | 2012-12-21 00:09:55 +0000 | [diff] [blame] | 283 | clk_put(emc_clk); |
| 284 | clk_put(cpu_clk); |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
| 288 | MODULE_AUTHOR("Colin Cross <ccross@android.com>"); |
| 289 | MODULE_DESCRIPTION("cpufreq driver for Nvidia Tegra2"); |
| 290 | MODULE_LICENSE("GPL"); |
| 291 | module_init(tegra_cpufreq_init); |
| 292 | module_exit(tegra_cpufreq_exit); |