Kukjin Kim | f7d7707 | 2011-06-01 14:18:22 -0700 | [diff] [blame] | 1 | /* |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 2 | * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 3 | * http://www.samsung.com |
| 4 | * |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 5 | * EXYNOS4 - CPU frequency scaling support |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/regulator/consumer.h> |
| 19 | #include <linux/cpufreq.h> |
MyungJoo Ham | 0073f53 | 2011-08-18 19:45:16 +0900 | [diff] [blame] | 20 | #include <linux/notifier.h> |
| 21 | #include <linux/suspend.h> |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 22 | |
| 23 | #include <mach/map.h> |
| 24 | #include <mach/regs-clock.h> |
| 25 | #include <mach/regs-mem.h> |
| 26 | |
| 27 | #include <plat/clock.h> |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 28 | #include <plat/pm.h> |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 29 | |
| 30 | static struct clk *cpu_clk; |
| 31 | static struct clk *moutcore; |
| 32 | static struct clk *mout_mpll; |
| 33 | static struct clk *mout_apll; |
| 34 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 35 | static struct regulator *arm_regulator; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 36 | |
| 37 | static struct cpufreq_freqs freqs; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 38 | |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame^] | 39 | struct cpufreq_clkdiv { |
| 40 | unsigned int clkdiv; |
| 41 | }; |
| 42 | |
MyungJoo Ham | 0073f53 | 2011-08-18 19:45:16 +0900 | [diff] [blame] | 43 | static unsigned int locking_frequency; |
| 44 | static bool frequency_locked; |
| 45 | static DEFINE_MUTEX(cpufreq_lock); |
| 46 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 47 | enum cpufreq_level_index { |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 48 | L0, L1, L2, L3, L4, CPUFREQ_LEVEL_END, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 49 | }; |
| 50 | |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame^] | 51 | static struct cpufreq_clkdiv exynos4_clkdiv_table[CPUFREQ_LEVEL_END]; |
| 52 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 53 | static struct cpufreq_frequency_table exynos4_freq_table[] = { |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 54 | {L0, 1200*1000}, |
| 55 | {L1, 1000*1000}, |
| 56 | {L2, 800*1000}, |
| 57 | {L3, 500*1000}, |
| 58 | {L4, 200*1000}, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 59 | {0, CPUFREQ_TABLE_END}, |
| 60 | }; |
| 61 | |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 62 | static unsigned int clkdiv_cpu0[CPUFREQ_LEVEL_END][7] = { |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 63 | /* |
| 64 | * Clock divider value for following |
| 65 | * { DIVCORE, DIVCOREM0, DIVCOREM1, DIVPERIPH, |
| 66 | * DIVATB, DIVPCLK_DBG, DIVAPLL } |
| 67 | */ |
| 68 | |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 69 | /* ARM L0: 1200MHz */ |
| 70 | { 0, 3, 7, 3, 4, 1, 7 }, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 71 | |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 72 | /* ARM L1: 1000MHz */ |
| 73 | { 0, 3, 7, 3, 4, 1, 7 }, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 74 | |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 75 | /* ARM L2: 800MHz */ |
| 76 | { 0, 3, 7, 3, 3, 1, 7 }, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 77 | |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 78 | /* ARM L3: 500MHz */ |
| 79 | { 0, 3, 7, 3, 3, 1, 7 }, |
| 80 | |
| 81 | /* ARM L4: 200MHz */ |
| 82 | { 0, 1, 3, 1, 3, 1, 0 }, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 83 | }; |
| 84 | |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 85 | static unsigned int clkdiv_cpu1[CPUFREQ_LEVEL_END][2] = { |
| 86 | /* |
| 87 | * Clock divider value for following |
| 88 | * { DIVCOPY, DIVHPM } |
| 89 | */ |
| 90 | |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 91 | /* ARM L0: 1200MHz */ |
| 92 | { 5, 0 }, |
| 93 | |
| 94 | /* ARM L1: 1000MHz */ |
| 95 | { 4, 0 }, |
| 96 | |
| 97 | /* ARM L2: 800MHz */ |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 98 | { 3, 0 }, |
| 99 | |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 100 | /* ARM L3: 500MHz */ |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 101 | { 3, 0 }, |
| 102 | |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 103 | /* ARM L4: 200MHz */ |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 104 | { 3, 0 }, |
| 105 | }; |
| 106 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 107 | struct cpufreq_voltage_table { |
| 108 | unsigned int index; /* any */ |
| 109 | unsigned int arm_volt; /* uV */ |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 110 | }; |
| 111 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 112 | static struct cpufreq_voltage_table exynos4_volt_table[CPUFREQ_LEVEL_END] = { |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 113 | { |
| 114 | .index = L0, |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 115 | .arm_volt = 1350000, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 116 | }, { |
| 117 | .index = L1, |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 118 | .arm_volt = 1300000, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 119 | }, { |
| 120 | .index = L2, |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 121 | .arm_volt = 1200000, |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 122 | }, { |
| 123 | .index = L3, |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 124 | .arm_volt = 1100000, |
| 125 | }, { |
| 126 | .index = L4, |
| 127 | .arm_volt = 1050000, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 128 | }, |
| 129 | }; |
| 130 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 131 | static unsigned int exynos4_apll_pms_table[CPUFREQ_LEVEL_END] = { |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 132 | /* APLL FOUT L0: 1200MHz */ |
| 133 | ((150 << 16) | (3 << 8) | 1), |
| 134 | |
| 135 | /* APLL FOUT L1: 1000MHz */ |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 136 | ((250 << 16) | (6 << 8) | 1), |
| 137 | |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 138 | /* APLL FOUT L2: 800MHz */ |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 139 | ((200 << 16) | (6 << 8) | 1), |
| 140 | |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 141 | /* APLL FOUT L3: 500MHz */ |
| 142 | ((250 << 16) | (6 << 8) | 2), |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 143 | |
Jaecheol Lee | ba9d780 | 2011-12-07 11:43:56 +0900 | [diff] [blame] | 144 | /* APLL FOUT L4: 200MHz */ |
| 145 | ((200 << 16) | (6 << 8) | 3), |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 146 | }; |
| 147 | |
Axel Lin | 2f0d6f2 | 2011-07-08 14:20:44 +0800 | [diff] [blame] | 148 | static int exynos4_verify_speed(struct cpufreq_policy *policy) |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 149 | { |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 150 | return cpufreq_frequency_table_verify(policy, exynos4_freq_table); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 151 | } |
| 152 | |
Axel Lin | 2f0d6f2 | 2011-07-08 14:20:44 +0800 | [diff] [blame] | 153 | static unsigned int exynos4_getspeed(unsigned int cpu) |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 154 | { |
| 155 | return clk_get_rate(cpu_clk) / 1000; |
| 156 | } |
| 157 | |
Axel Lin | 2f0d6f2 | 2011-07-08 14:20:44 +0800 | [diff] [blame] | 158 | static void exynos4_set_clkdiv(unsigned int div_index) |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 159 | { |
| 160 | unsigned int tmp; |
| 161 | |
| 162 | /* Change Divider - CPU0 */ |
| 163 | |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame^] | 164 | tmp = exynos4_clkdiv_table[div_index].clkdiv; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 165 | |
| 166 | __raw_writel(tmp, S5P_CLKDIV_CPU); |
| 167 | |
| 168 | do { |
| 169 | tmp = __raw_readl(S5P_CLKDIV_STATCPU); |
| 170 | } while (tmp & 0x1111111); |
| 171 | |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 172 | /* Change Divider - CPU1 */ |
| 173 | |
| 174 | tmp = __raw_readl(S5P_CLKDIV_CPU1); |
| 175 | |
| 176 | tmp &= ~((0x7 << 4) | 0x7); |
| 177 | |
| 178 | tmp |= ((clkdiv_cpu1[div_index][0] << 4) | |
| 179 | (clkdiv_cpu1[div_index][1] << 0)); |
| 180 | |
| 181 | __raw_writel(tmp, S5P_CLKDIV_CPU1); |
| 182 | |
| 183 | do { |
| 184 | tmp = __raw_readl(S5P_CLKDIV_STATCPU1); |
| 185 | } while (tmp & 0x11); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 186 | } |
| 187 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 188 | static void exynos4_set_apll(unsigned int index) |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 189 | { |
| 190 | unsigned int tmp; |
| 191 | |
| 192 | /* 1. MUX_CORE_SEL = MPLL, ARMCLK uses MPLL for lock time */ |
| 193 | clk_set_parent(moutcore, mout_mpll); |
| 194 | |
| 195 | do { |
| 196 | tmp = (__raw_readl(S5P_CLKMUX_STATCPU) |
| 197 | >> S5P_CLKSRC_CPU_MUXCORE_SHIFT); |
| 198 | tmp &= 0x7; |
| 199 | } while (tmp != 0x2); |
| 200 | |
| 201 | /* 2. Set APLL Lock time */ |
| 202 | __raw_writel(S5P_APLL_LOCKTIME, S5P_APLL_LOCK); |
| 203 | |
| 204 | /* 3. Change PLL PMS values */ |
| 205 | tmp = __raw_readl(S5P_APLL_CON0); |
| 206 | tmp &= ~((0x3ff << 16) | (0x3f << 8) | (0x7 << 0)); |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 207 | tmp |= exynos4_apll_pms_table[index]; |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 208 | __raw_writel(tmp, S5P_APLL_CON0); |
| 209 | |
| 210 | /* 4. wait_lock_time */ |
| 211 | do { |
| 212 | tmp = __raw_readl(S5P_APLL_CON0); |
| 213 | } while (!(tmp & (0x1 << S5P_APLLCON0_LOCKED_SHIFT))); |
| 214 | |
| 215 | /* 5. MUX_CORE_SEL = APLL */ |
| 216 | clk_set_parent(moutcore, mout_apll); |
| 217 | |
| 218 | do { |
| 219 | tmp = __raw_readl(S5P_CLKMUX_STATCPU); |
| 220 | tmp &= S5P_CLKMUX_STATCPU_MUXCORE_MASK; |
| 221 | } while (tmp != (0x1 << S5P_CLKSRC_CPU_MUXCORE_SHIFT)); |
| 222 | } |
| 223 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 224 | static void exynos4_set_frequency(unsigned int old_index, unsigned int new_index) |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 225 | { |
| 226 | unsigned int tmp; |
| 227 | |
| 228 | if (old_index > new_index) { |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame^] | 229 | /* |
| 230 | * L1/L3, L2/L4 Level change require |
| 231 | * to only change s divider value |
| 232 | */ |
| 233 | if (((old_index == L3) && (new_index == L1)) || |
| 234 | ((old_index == L4) && (new_index == L2))) { |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 235 | /* 1. Change the system clock divider values */ |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 236 | exynos4_set_clkdiv(new_index); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 237 | |
| 238 | /* 2. Change just s value in apll m,p,s value */ |
| 239 | tmp = __raw_readl(S5P_APLL_CON0); |
| 240 | tmp &= ~(0x7 << 0); |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 241 | tmp |= (exynos4_apll_pms_table[new_index] & 0x7); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 242 | __raw_writel(tmp, S5P_APLL_CON0); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 243 | } else { |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame^] | 244 | /* Clock Configuration Procedure */ |
| 245 | /* 1. Change the system clock divider values */ |
| 246 | exynos4_set_clkdiv(new_index); |
| 247 | /* 2. Change the apll m,p,s value */ |
| 248 | exynos4_set_apll(new_index); |
| 249 | } |
| 250 | } else if (old_index < new_index) { |
| 251 | /* |
| 252 | * L1/L3, L2/L4 Level change require |
| 253 | * to only change s divider value |
| 254 | */ |
| 255 | if (((old_index == L1) && (new_index == L3)) || |
| 256 | ((old_index == L2) && (new_index == L4))) { |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 257 | /* 1. Change just s value in apll m,p,s value */ |
| 258 | tmp = __raw_readl(S5P_APLL_CON0); |
| 259 | tmp &= ~(0x7 << 0); |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 260 | tmp |= (exynos4_apll_pms_table[new_index] & 0x7); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 261 | __raw_writel(tmp, S5P_APLL_CON0); |
| 262 | |
| 263 | /* 2. Change the system clock divider values */ |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 264 | exynos4_set_clkdiv(new_index); |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame^] | 265 | } else { |
| 266 | /* Clock Configuration Procedure */ |
| 267 | /* 1. Change the apll m,p,s value */ |
| 268 | exynos4_set_apll(new_index); |
| 269 | /* 2. Change the system clock divider values */ |
| 270 | exynos4_set_clkdiv(new_index); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 275 | static int exynos4_target(struct cpufreq_policy *policy, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 276 | unsigned int target_freq, |
| 277 | unsigned int relation) |
| 278 | { |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 279 | unsigned int index, old_index; |
Jaecheol Lee | c8c430e | 2011-12-07 11:43:42 +0900 | [diff] [blame] | 280 | unsigned int arm_volt; |
MyungJoo Ham | 0073f53 | 2011-08-18 19:45:16 +0900 | [diff] [blame] | 281 | int err = -EINVAL; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 282 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 283 | freqs.old = exynos4_getspeed(policy->cpu); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 284 | |
MyungJoo Ham | 0073f53 | 2011-08-18 19:45:16 +0900 | [diff] [blame] | 285 | mutex_lock(&cpufreq_lock); |
| 286 | |
| 287 | if (frequency_locked && target_freq != locking_frequency) { |
| 288 | err = -EAGAIN; |
| 289 | goto out; |
| 290 | } |
| 291 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 292 | if (cpufreq_frequency_table_target(policy, exynos4_freq_table, |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 293 | freqs.old, relation, &old_index)) |
MyungJoo Ham | 0073f53 | 2011-08-18 19:45:16 +0900 | [diff] [blame] | 294 | goto out; |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 295 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 296 | if (cpufreq_frequency_table_target(policy, exynos4_freq_table, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 297 | target_freq, relation, &index)) |
MyungJoo Ham | 0073f53 | 2011-08-18 19:45:16 +0900 | [diff] [blame] | 298 | goto out; |
| 299 | |
| 300 | err = 0; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 301 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 302 | freqs.new = exynos4_freq_table[index].frequency; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 303 | freqs.cpu = policy->cpu; |
| 304 | |
| 305 | if (freqs.new == freqs.old) |
MyungJoo Ham | 0073f53 | 2011-08-18 19:45:16 +0900 | [diff] [blame] | 306 | goto out; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 307 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 308 | /* get the voltage value */ |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 309 | arm_volt = exynos4_volt_table[index].arm_volt; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 310 | |
| 311 | cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); |
| 312 | |
| 313 | /* control regulator */ |
| 314 | if (freqs.new > freqs.old) { |
| 315 | /* Voltage up */ |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 316 | regulator_set_voltage(arm_regulator, arm_volt, arm_volt); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /* Clock Configuration Procedure */ |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 320 | exynos4_set_frequency(old_index, index); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 321 | |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame^] | 322 | cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); |
| 323 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 324 | /* control regulator */ |
| 325 | if (freqs.new < freqs.old) { |
| 326 | /* Voltage down */ |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 327 | regulator_set_voltage(arm_regulator, arm_volt, arm_volt); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 328 | } |
| 329 | |
MyungJoo Ham | 0073f53 | 2011-08-18 19:45:16 +0900 | [diff] [blame] | 330 | out: |
| 331 | mutex_unlock(&cpufreq_lock); |
| 332 | return err; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | #ifdef CONFIG_PM |
MyungJoo Ham | 0073f53 | 2011-08-18 19:45:16 +0900 | [diff] [blame] | 336 | /* |
| 337 | * These suspend/resume are used as syscore_ops, it is already too |
| 338 | * late to set regulator voltages at this stage. |
| 339 | */ |
Linus Torvalds | 411f5c7 | 2011-03-17 19:08:06 -0700 | [diff] [blame] | 340 | static int exynos4_cpufreq_suspend(struct cpufreq_policy *policy) |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 341 | { |
| 342 | return 0; |
| 343 | } |
| 344 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 345 | static int exynos4_cpufreq_resume(struct cpufreq_policy *policy) |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 346 | { |
| 347 | return 0; |
| 348 | } |
| 349 | #endif |
| 350 | |
MyungJoo Ham | 0073f53 | 2011-08-18 19:45:16 +0900 | [diff] [blame] | 351 | /** |
| 352 | * exynos4_cpufreq_pm_notifier - block CPUFREQ's activities in suspend-resume |
| 353 | * context |
| 354 | * @notifier |
| 355 | * @pm_event |
| 356 | * @v |
| 357 | * |
| 358 | * While frequency_locked == true, target() ignores every frequency but |
| 359 | * locking_frequency. The locking_frequency value is the initial frequency, |
| 360 | * which is set by the bootloader. In order to eliminate possible |
| 361 | * inconsistency in clock values, we save and restore frequencies during |
| 362 | * suspend and resume and block CPUFREQ activities. Note that the standard |
| 363 | * suspend/resume cannot be used as they are too deep (syscore_ops) for |
| 364 | * regulator actions. |
| 365 | */ |
| 366 | static int exynos4_cpufreq_pm_notifier(struct notifier_block *notifier, |
| 367 | unsigned long pm_event, void *v) |
| 368 | { |
| 369 | struct cpufreq_policy *policy = cpufreq_cpu_get(0); /* boot CPU */ |
| 370 | static unsigned int saved_frequency; |
| 371 | unsigned int temp; |
| 372 | |
| 373 | mutex_lock(&cpufreq_lock); |
| 374 | switch (pm_event) { |
| 375 | case PM_SUSPEND_PREPARE: |
| 376 | if (frequency_locked) |
| 377 | goto out; |
| 378 | frequency_locked = true; |
| 379 | |
| 380 | if (locking_frequency) { |
| 381 | saved_frequency = exynos4_getspeed(0); |
| 382 | |
| 383 | mutex_unlock(&cpufreq_lock); |
| 384 | exynos4_target(policy, locking_frequency, |
| 385 | CPUFREQ_RELATION_H); |
| 386 | mutex_lock(&cpufreq_lock); |
| 387 | } |
| 388 | |
| 389 | break; |
| 390 | case PM_POST_SUSPEND: |
| 391 | |
| 392 | if (saved_frequency) { |
| 393 | /* |
| 394 | * While frequency_locked, only locking_frequency |
| 395 | * is valid for target(). In order to use |
| 396 | * saved_frequency while keeping frequency_locked, |
| 397 | * we temporarly overwrite locking_frequency. |
| 398 | */ |
| 399 | temp = locking_frequency; |
| 400 | locking_frequency = saved_frequency; |
| 401 | |
| 402 | mutex_unlock(&cpufreq_lock); |
| 403 | exynos4_target(policy, locking_frequency, |
| 404 | CPUFREQ_RELATION_H); |
| 405 | mutex_lock(&cpufreq_lock); |
| 406 | |
| 407 | locking_frequency = temp; |
| 408 | } |
| 409 | |
| 410 | frequency_locked = false; |
| 411 | break; |
| 412 | } |
| 413 | out: |
| 414 | mutex_unlock(&cpufreq_lock); |
| 415 | |
| 416 | return NOTIFY_OK; |
| 417 | } |
| 418 | |
| 419 | static struct notifier_block exynos4_cpufreq_nb = { |
| 420 | .notifier_call = exynos4_cpufreq_pm_notifier, |
| 421 | }; |
| 422 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 423 | static int exynos4_cpufreq_cpu_init(struct cpufreq_policy *policy) |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 424 | { |
Donggeun Kim | 5beae3b | 2011-07-19 14:41:57 +0900 | [diff] [blame] | 425 | int ret; |
| 426 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 427 | policy->cur = policy->min = policy->max = exynos4_getspeed(policy->cpu); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 428 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 429 | cpufreq_frequency_table_get_attr(exynos4_freq_table, policy->cpu); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 430 | |
| 431 | /* set the transition latency value */ |
| 432 | policy->cpuinfo.transition_latency = 100000; |
| 433 | |
| 434 | /* |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 435 | * EXYNOS4 multi-core processors has 2 cores |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 436 | * that the frequency cannot be set independently. |
| 437 | * Each cpu is bound to the same speed. |
| 438 | * So the affected cpu is all of the cpus. |
| 439 | */ |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame^] | 440 | if (!cpu_online(1)) { |
| 441 | cpumask_copy(policy->related_cpus, cpu_possible_mask); |
| 442 | cpumask_copy(policy->cpus, cpu_online_mask); |
| 443 | } else { |
| 444 | cpumask_setall(policy->cpus); |
| 445 | } |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 446 | |
Donggeun Kim | 5beae3b | 2011-07-19 14:41:57 +0900 | [diff] [blame] | 447 | ret = cpufreq_frequency_table_cpuinfo(policy, exynos4_freq_table); |
| 448 | if (ret) |
| 449 | return ret; |
| 450 | |
| 451 | cpufreq_frequency_table_get_attr(exynos4_freq_table, policy->cpu); |
| 452 | |
| 453 | return 0; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 454 | } |
| 455 | |
Donggeun Kim | 5beae3b | 2011-07-19 14:41:57 +0900 | [diff] [blame] | 456 | static int exynos4_cpufreq_cpu_exit(struct cpufreq_policy *policy) |
| 457 | { |
| 458 | cpufreq_frequency_table_put_attr(policy->cpu); |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static struct freq_attr *exynos4_cpufreq_attr[] = { |
| 463 | &cpufreq_freq_attr_scaling_available_freqs, |
| 464 | NULL, |
| 465 | }; |
| 466 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 467 | static struct cpufreq_driver exynos4_driver = { |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 468 | .flags = CPUFREQ_STICKY, |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 469 | .verify = exynos4_verify_speed, |
| 470 | .target = exynos4_target, |
| 471 | .get = exynos4_getspeed, |
| 472 | .init = exynos4_cpufreq_cpu_init, |
Donggeun Kim | 5beae3b | 2011-07-19 14:41:57 +0900 | [diff] [blame] | 473 | .exit = exynos4_cpufreq_cpu_exit, |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 474 | .name = "exynos4_cpufreq", |
Donggeun Kim | 5beae3b | 2011-07-19 14:41:57 +0900 | [diff] [blame] | 475 | .attr = exynos4_cpufreq_attr, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 476 | #ifdef CONFIG_PM |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 477 | .suspend = exynos4_cpufreq_suspend, |
| 478 | .resume = exynos4_cpufreq_resume, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 479 | #endif |
| 480 | }; |
| 481 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 482 | static int __init exynos4_cpufreq_init(void) |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 483 | { |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame^] | 484 | int i; |
| 485 | unsigned int tmp; |
| 486 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 487 | cpu_clk = clk_get(NULL, "armclk"); |
| 488 | if (IS_ERR(cpu_clk)) |
| 489 | return PTR_ERR(cpu_clk); |
| 490 | |
MyungJoo Ham | 0073f53 | 2011-08-18 19:45:16 +0900 | [diff] [blame] | 491 | locking_frequency = exynos4_getspeed(0); |
| 492 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 493 | moutcore = clk_get(NULL, "moutcore"); |
| 494 | if (IS_ERR(moutcore)) |
| 495 | goto out; |
| 496 | |
| 497 | mout_mpll = clk_get(NULL, "mout_mpll"); |
| 498 | if (IS_ERR(mout_mpll)) |
| 499 | goto out; |
| 500 | |
| 501 | mout_apll = clk_get(NULL, "mout_apll"); |
| 502 | if (IS_ERR(mout_apll)) |
| 503 | goto out; |
| 504 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 505 | arm_regulator = regulator_get(NULL, "vdd_arm"); |
| 506 | if (IS_ERR(arm_regulator)) { |
| 507 | printk(KERN_ERR "failed to get resource %s\n", "vdd_arm"); |
| 508 | goto out; |
| 509 | } |
| 510 | |
MyungJoo Ham | 0073f53 | 2011-08-18 19:45:16 +0900 | [diff] [blame] | 511 | register_pm_notifier(&exynos4_cpufreq_nb); |
| 512 | |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame^] | 513 | tmp = __raw_readl(S5P_CLKDIV_CPU); |
| 514 | |
| 515 | for (i = L0; i < CPUFREQ_LEVEL_END; i++) { |
| 516 | tmp &= ~(S5P_CLKDIV_CPU0_CORE_MASK | |
| 517 | S5P_CLKDIV_CPU0_COREM0_MASK | |
| 518 | S5P_CLKDIV_CPU0_COREM1_MASK | |
| 519 | S5P_CLKDIV_CPU0_PERIPH_MASK | |
| 520 | S5P_CLKDIV_CPU0_ATB_MASK | |
| 521 | S5P_CLKDIV_CPU0_PCLKDBG_MASK | |
| 522 | S5P_CLKDIV_CPU0_APLL_MASK); |
| 523 | |
| 524 | tmp |= ((clkdiv_cpu0[i][0] << S5P_CLKDIV_CPU0_CORE_SHIFT) | |
| 525 | (clkdiv_cpu0[i][1] << S5P_CLKDIV_CPU0_COREM0_SHIFT) | |
| 526 | (clkdiv_cpu0[i][2] << S5P_CLKDIV_CPU0_COREM1_SHIFT) | |
| 527 | (clkdiv_cpu0[i][3] << S5P_CLKDIV_CPU0_PERIPH_SHIFT) | |
| 528 | (clkdiv_cpu0[i][4] << S5P_CLKDIV_CPU0_ATB_SHIFT) | |
| 529 | (clkdiv_cpu0[i][5] << S5P_CLKDIV_CPU0_PCLKDBG_SHIFT) | |
| 530 | (clkdiv_cpu0[i][6] << S5P_CLKDIV_CPU0_APLL_SHIFT)); |
| 531 | |
| 532 | exynos4_clkdiv_table[i].clkdiv = tmp; |
| 533 | } |
| 534 | |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 535 | return cpufreq_register_driver(&exynos4_driver); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 536 | |
| 537 | out: |
| 538 | if (!IS_ERR(cpu_clk)) |
| 539 | clk_put(cpu_clk); |
| 540 | |
| 541 | if (!IS_ERR(moutcore)) |
| 542 | clk_put(moutcore); |
| 543 | |
| 544 | if (!IS_ERR(mout_mpll)) |
| 545 | clk_put(mout_mpll); |
| 546 | |
| 547 | if (!IS_ERR(mout_apll)) |
| 548 | clk_put(mout_apll); |
| 549 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 550 | if (!IS_ERR(arm_regulator)) |
| 551 | regulator_put(arm_regulator); |
| 552 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 553 | printk(KERN_ERR "%s: failed initialization\n", __func__); |
| 554 | |
| 555 | return -EINVAL; |
| 556 | } |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 557 | late_initcall(exynos4_cpufreq_init); |