Blame view
drivers/cpufreq/s3c2416-cpufreq.c
12.2 KB
34ee55075 [CPUFREQ] Add S3C... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
/* * S3C2416/2450 CPUfreq Support * * Copyright 2011 Heiko Stuebner <heiko@sntech.de> * * based on s3c64xx_cpufreq.c * * Copyright 2009 Wolfson Microelectronics plc * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ #include <linux/kernel.h> #include <linux/types.h> #include <linux/init.h> #include <linux/cpufreq.h> #include <linux/clk.h> #include <linux/err.h> #include <linux/regulator/consumer.h> #include <linux/reboot.h> #include <linux/module.h> static DEFINE_MUTEX(cpufreq_lock); struct s3c2416_data { struct clk *armdiv; struct clk *armclk; struct clk *hclk; unsigned long regulator_latency; #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE struct regulator *vddarm; #endif struct cpufreq_frequency_table *freq_table; bool is_dvs; bool disable_dvs; }; static struct s3c2416_data s3c2416_cpufreq; struct s3c2416_dvfs { unsigned int vddarm_min; unsigned int vddarm_max; }; /* pseudo-frequency for dvs mode */ #define FREQ_DVS 132333 /* frequency to sleep and reboot in * it's essential to leave dvs, as some boards do not reconfigure the * regulator on reboot */ #define FREQ_SLEEP 133333 /* Sources for the ARMCLK */ #define SOURCE_HCLK 0 #define SOURCE_ARMDIV 1 #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE /* S3C2416 only supports changing the voltage in the dvs-mode. * Voltages down to 1.0V seem to work, so we take what the regulator * can get us. */ static struct s3c2416_dvfs s3c2416_dvfs_table[] = { [SOURCE_HCLK] = { 950000, 1250000 }, [SOURCE_ARMDIV] = { 1250000, 1350000 }, }; #endif static struct cpufreq_frequency_table s3c2416_freq_table[] = { |
7f4b04614 cpufreq: create a... |
75 76 77 78 79 |
{ 0, SOURCE_HCLK, FREQ_DVS }, { 0, SOURCE_ARMDIV, 133333 }, { 0, SOURCE_ARMDIV, 266666 }, { 0, SOURCE_ARMDIV, 400000 }, { 0, 0, CPUFREQ_TABLE_END }, |
34ee55075 [CPUFREQ] Add S3C... |
80 81 82 |
}; static struct cpufreq_frequency_table s3c2450_freq_table[] = { |
7f4b04614 cpufreq: create a... |
83 84 85 86 87 |
{ 0, SOURCE_HCLK, FREQ_DVS }, { 0, SOURCE_ARMDIV, 133500 }, { 0, SOURCE_ARMDIV, 267000 }, { 0, SOURCE_ARMDIV, 534000 }, { 0, 0, CPUFREQ_TABLE_END }, |
34ee55075 [CPUFREQ] Add S3C... |
88 |
}; |
34ee55075 [CPUFREQ] Add S3C... |
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
static unsigned int s3c2416_cpufreq_get_speed(unsigned int cpu) { struct s3c2416_data *s3c_freq = &s3c2416_cpufreq; if (cpu != 0) return 0; /* return our pseudo-frequency when in dvs mode */ if (s3c_freq->is_dvs) return FREQ_DVS; return clk_get_rate(s3c_freq->armclk) / 1000; } static int s3c2416_cpufreq_set_armdiv(struct s3c2416_data *s3c_freq, unsigned int freq) { int ret; if (clk_get_rate(s3c_freq->armdiv) / 1000 != freq) { ret = clk_set_rate(s3c_freq->armdiv, freq * 1000); if (ret < 0) { pr_err("cpufreq: Failed to set armdiv rate %dkHz: %d ", freq, ret); return ret; } } return 0; } static int s3c2416_cpufreq_enter_dvs(struct s3c2416_data *s3c_freq, int idx) { #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE struct s3c2416_dvfs *dvfs; #endif int ret; if (s3c_freq->is_dvs) { pr_debug("cpufreq: already in dvs mode, nothing to do "); return 0; } pr_debug("cpufreq: switching armclk to hclk (%lukHz) ", clk_get_rate(s3c_freq->hclk) / 1000); ret = clk_set_parent(s3c_freq->armclk, s3c_freq->hclk); if (ret < 0) { pr_err("cpufreq: Failed to switch armclk to hclk: %d ", ret); return ret; } #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE /* changing the core voltage is only allowed when in dvs mode */ if (s3c_freq->vddarm) { dvfs = &s3c2416_dvfs_table[idx]; |
c03c30137 cpufreq: Fix typo... |
148 149 |
pr_debug("cpufreq: setting regulator to %d-%d ", |
34ee55075 [CPUFREQ] Add S3C... |
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
dvfs->vddarm_min, dvfs->vddarm_max); ret = regulator_set_voltage(s3c_freq->vddarm, dvfs->vddarm_min, dvfs->vddarm_max); /* when lowering the voltage failed, there is nothing to do */ if (ret != 0) pr_err("cpufreq: Failed to set VDDARM: %d ", ret); } #endif s3c_freq->is_dvs = 1; return 0; } static int s3c2416_cpufreq_leave_dvs(struct s3c2416_data *s3c_freq, int idx) { #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE struct s3c2416_dvfs *dvfs; #endif int ret; if (!s3c_freq->is_dvs) { pr_debug("cpufreq: not in dvs mode, so can't leave "); return 0; } #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE if (s3c_freq->vddarm) { dvfs = &s3c2416_dvfs_table[idx]; |
c03c30137 cpufreq: Fix typo... |
183 184 |
pr_debug("cpufreq: setting regulator to %d-%d ", |
34ee55075 [CPUFREQ] Add S3C... |
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
dvfs->vddarm_min, dvfs->vddarm_max); ret = regulator_set_voltage(s3c_freq->vddarm, dvfs->vddarm_min, dvfs->vddarm_max); if (ret != 0) { pr_err("cpufreq: Failed to set VDDARM: %d ", ret); return ret; } } #endif /* force armdiv to hclk frequency for transition from dvs*/ if (clk_get_rate(s3c_freq->armdiv) > clk_get_rate(s3c_freq->hclk)) { pr_debug("cpufreq: force armdiv to hclk frequency (%lukHz) ", clk_get_rate(s3c_freq->hclk) / 1000); ret = s3c2416_cpufreq_set_armdiv(s3c_freq, clk_get_rate(s3c_freq->hclk) / 1000); if (ret < 0) { |
278cee051 treewide: Fix typ... |
205 206 |
pr_err("cpufreq: Failed to set the armdiv to %lukHz: %d ", |
34ee55075 [CPUFREQ] Add S3C... |
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
clk_get_rate(s3c_freq->hclk) / 1000, ret); return ret; } } pr_debug("cpufreq: switching armclk parent to armdiv (%lukHz) ", clk_get_rate(s3c_freq->armdiv) / 1000); ret = clk_set_parent(s3c_freq->armclk, s3c_freq->armdiv); if (ret < 0) { pr_err("cpufreq: Failed to switch armclk clock parent to armdiv: %d ", ret); return ret; } s3c_freq->is_dvs = 0; return 0; } static int s3c2416_cpufreq_set_target(struct cpufreq_policy *policy, |
9c0ebcf78 cpufreq: Implemen... |
230 |
unsigned int index) |
34ee55075 [CPUFREQ] Add S3C... |
231 232 |
{ struct s3c2416_data *s3c_freq = &s3c2416_cpufreq; |
d4019f0a9 cpufreq: move fre... |
233 |
unsigned int new_freq; |
34ee55075 [CPUFREQ] Add S3C... |
234 |
int idx, ret, to_dvs = 0; |
34ee55075 [CPUFREQ] Add S3C... |
235 236 |
mutex_lock(&cpufreq_lock); |
9c0ebcf78 cpufreq: Implemen... |
237 |
idx = s3c_freq->freq_table[index].driver_data; |
34ee55075 [CPUFREQ] Add S3C... |
238 239 240 241 242 243 244 245 246 247 248 |
if (idx == SOURCE_HCLK) to_dvs = 1; /* switching to dvs when it's not allowed */ if (to_dvs && s3c_freq->disable_dvs) { pr_debug("cpufreq: entering dvs mode not allowed "); ret = -EINVAL; goto out; } |
34ee55075 [CPUFREQ] Add S3C... |
249 250 251 252 |
/* When leavin dvs mode, always switch the armdiv to the hclk rate * The S3C2416 has stability issues when switching directly to * higher frequencies. */ |
d4019f0a9 cpufreq: move fre... |
253 |
new_freq = (s3c_freq->is_dvs && !to_dvs) |
34ee55075 [CPUFREQ] Add S3C... |
254 |
? clk_get_rate(s3c_freq->hclk) / 1000 |
9c0ebcf78 cpufreq: Implemen... |
255 |
: s3c_freq->freq_table[index].frequency; |
34ee55075 [CPUFREQ] Add S3C... |
256 |
|
34ee55075 [CPUFREQ] Add S3C... |
257 258 259 260 261 262 263 264 265 |
if (to_dvs) { pr_debug("cpufreq: enter dvs "); ret = s3c2416_cpufreq_enter_dvs(s3c_freq, idx); } else if (s3c_freq->is_dvs) { pr_debug("cpufreq: leave dvs "); ret = s3c2416_cpufreq_leave_dvs(s3c_freq, idx); } else { |
d4019f0a9 cpufreq: move fre... |
266 267 268 |
pr_debug("cpufreq: change armdiv to %dkHz ", new_freq); ret = s3c2416_cpufreq_set_armdiv(s3c_freq, new_freq); |
34ee55075 [CPUFREQ] Add S3C... |
269 |
} |
34ee55075 [CPUFREQ] Add S3C... |
270 271 272 273 274 275 276 |
out: mutex_unlock(&cpufreq_lock); return ret; } #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE |
61882b631 cpufreq: s3c: rem... |
277 |
static void s3c2416_cpufreq_cfg_regulator(struct s3c2416_data *s3c_freq) |
34ee55075 [CPUFREQ] Add S3C... |
278 279 |
{ int count, v, i, found; |
041526f91 cpufreq: Use cpuf... |
280 |
struct cpufreq_frequency_table *pos; |
34ee55075 [CPUFREQ] Add S3C... |
281 282 283 284 285 286 287 288 |
struct s3c2416_dvfs *dvfs; count = regulator_count_voltages(s3c_freq->vddarm); if (count < 0) { pr_err("cpufreq: Unable to check supported voltages "); return; } |
041526f91 cpufreq: Use cpuf... |
289 290 |
if (!count) goto out; |
34ee55075 [CPUFREQ] Add S3C... |
291 |
|
041526f91 cpufreq: Use cpuf... |
292 293 |
cpufreq_for_each_valid_entry(pos, s3c_freq->freq_table) { dvfs = &s3c2416_dvfs_table[pos->driver_data]; |
34ee55075 [CPUFREQ] Add S3C... |
294 295 296 297 298 299 300 301 302 303 304 305 |
found = 0; /* Check only the min-voltage, more is always ok on S3C2416 */ for (i = 0; i < count; i++) { v = regulator_list_voltage(s3c_freq->vddarm, i); if (v >= dvfs->vddarm_min) found = 1; } if (!found) { pr_debug("cpufreq: %dkHz unsupported by regulator ", |
041526f91 cpufreq: Use cpuf... |
306 307 |
pos->frequency); pos->frequency = CPUFREQ_ENTRY_INVALID; |
34ee55075 [CPUFREQ] Add S3C... |
308 |
} |
34ee55075 [CPUFREQ] Add S3C... |
309 |
} |
041526f91 cpufreq: Use cpuf... |
310 |
out: |
34ee55075 [CPUFREQ] Add S3C... |
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
/* Guessed */ s3c_freq->regulator_latency = 1 * 1000 * 1000; } #endif static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this, unsigned long event, void *ptr) { struct s3c2416_data *s3c_freq = &s3c2416_cpufreq; int ret; mutex_lock(&cpufreq_lock); /* disable further changes */ s3c_freq->disable_dvs = 1; mutex_unlock(&cpufreq_lock); /* some boards don't reconfigure the regulator on reboot, which * could lead to undervolting the cpu when the clock is reset. * Therefore we always leave the DVS mode on reboot. */ if (s3c_freq->is_dvs) { pr_debug("cpufreq: leave dvs on reboot "); ret = cpufreq_driver_target(cpufreq_cpu_get(0), FREQ_SLEEP, 0); if (ret < 0) return NOTIFY_BAD; } return NOTIFY_DONE; } static struct notifier_block s3c2416_cpufreq_reboot_notifier = { .notifier_call = s3c2416_cpufreq_reboot_notifier_evt, }; |
61882b631 cpufreq: s3c: rem... |
347 |
static int s3c2416_cpufreq_driver_init(struct cpufreq_policy *policy) |
34ee55075 [CPUFREQ] Add S3C... |
348 349 |
{ struct s3c2416_data *s3c_freq = &s3c2416_cpufreq; |
041526f91 cpufreq: Use cpuf... |
350 |
struct cpufreq_frequency_table *pos; |
34ee55075 [CPUFREQ] Add S3C... |
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
struct clk *msysclk; unsigned long rate; int ret; if (policy->cpu != 0) return -EINVAL; msysclk = clk_get(NULL, "msysclk"); if (IS_ERR(msysclk)) { ret = PTR_ERR(msysclk); pr_err("cpufreq: Unable to obtain msysclk: %d ", ret); return ret; } /* * S3C2416 and S3C2450 share the same processor-ID and also provide no * other means to distinguish them other than through the rate of * msysclk. On S3C2416 msysclk runs at 800MHz and on S3C2450 at 533MHz. */ rate = clk_get_rate(msysclk); if (rate == 800 * 1000 * 1000) { pr_info("cpufreq: msysclk running at %lukHz, using S3C2416 frequency table ", rate / 1000); s3c_freq->freq_table = s3c2416_freq_table; policy->cpuinfo.max_freq = 400000; } else if (rate / 1000 == 534000) { pr_info("cpufreq: msysclk running at %lukHz, using S3C2450 frequency table ", rate / 1000); s3c_freq->freq_table = s3c2450_freq_table; policy->cpuinfo.max_freq = 534000; } /* not needed anymore */ clk_put(msysclk); if (s3c_freq->freq_table == NULL) { pr_err("cpufreq: No frequency information for this CPU, msysclk at %lukHz ", rate / 1000); return -ENODEV; } s3c_freq->is_dvs = 0; s3c_freq->armdiv = clk_get(NULL, "armdiv"); if (IS_ERR(s3c_freq->armdiv)) { ret = PTR_ERR(s3c_freq->armdiv); pr_err("cpufreq: Unable to obtain ARMDIV: %d ", ret); return ret; } s3c_freq->hclk = clk_get(NULL, "hclk"); if (IS_ERR(s3c_freq->hclk)) { ret = PTR_ERR(s3c_freq->hclk); pr_err("cpufreq: Unable to obtain HCLK: %d ", ret); goto err_hclk; } /* chech hclk rate, we only support the common 133MHz for now * hclk could also run at 66MHz, but this not often used */ rate = clk_get_rate(s3c_freq->hclk); if (rate < 133 * 1000 * 1000) { pr_err("cpufreq: HCLK not at 133MHz "); |
34ee55075 [CPUFREQ] Add S3C... |
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
ret = -EINVAL; goto err_armclk; } s3c_freq->armclk = clk_get(NULL, "armclk"); if (IS_ERR(s3c_freq->armclk)) { ret = PTR_ERR(s3c_freq->armclk); pr_err("cpufreq: Unable to obtain ARMCLK: %d ", ret); goto err_armclk; } #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE s3c_freq->vddarm = regulator_get(NULL, "vddarm"); if (IS_ERR(s3c_freq->vddarm)) { ret = PTR_ERR(s3c_freq->vddarm); pr_err("cpufreq: Failed to obtain VDDARM: %d ", ret); goto err_vddarm; } s3c2416_cpufreq_cfg_regulator(s3c_freq); #else s3c_freq->regulator_latency = 0; #endif |
041526f91 cpufreq: Use cpuf... |
446 |
cpufreq_for_each_entry(pos, s3c_freq->freq_table) { |
34ee55075 [CPUFREQ] Add S3C... |
447 |
/* special handling for dvs mode */ |
041526f91 cpufreq: Use cpuf... |
448 |
if (pos->driver_data == 0) { |
34ee55075 [CPUFREQ] Add S3C... |
449 450 451 |
if (!s3c_freq->hclk) { pr_debug("cpufreq: %dkHz unsupported as it would need unavailable dvs mode ", |
041526f91 cpufreq: Use cpuf... |
452 453 |
pos->frequency); pos->frequency = CPUFREQ_ENTRY_INVALID; |
34ee55075 [CPUFREQ] Add S3C... |
454 |
} else { |
34ee55075 [CPUFREQ] Add S3C... |
455 456 457 458 459 460 |
continue; } } /* Check for frequencies we can generate */ rate = clk_round_rate(s3c_freq->armdiv, |
041526f91 cpufreq: Use cpuf... |
461 |
pos->frequency * 1000); |
34ee55075 [CPUFREQ] Add S3C... |
462 |
rate /= 1000; |
041526f91 cpufreq: Use cpuf... |
463 |
if (rate != pos->frequency) { |
34ee55075 [CPUFREQ] Add S3C... |
464 465 |
pr_debug("cpufreq: %dkHz unsupported by clock (clk_round_rate return %lu) ", |
041526f91 cpufreq: Use cpuf... |
466 467 |
pos->frequency, rate); pos->frequency = CPUFREQ_ENTRY_INVALID; |
34ee55075 [CPUFREQ] Add S3C... |
468 |
} |
34ee55075 [CPUFREQ] Add S3C... |
469 |
} |
34ee55075 [CPUFREQ] Add S3C... |
470 471 472 |
/* Datasheet says PLL stabalisation time must be at least 300us, * so but add some fudge. (reference in LOCKCON0 register description) */ |
a307a1e6b cpufreq: s3c: use... |
473 474 |
ret = cpufreq_generic_init(policy, s3c_freq->freq_table, (500 * 1000) + s3c_freq->regulator_latency); |
34ee55075 [CPUFREQ] Add S3C... |
475 476 |
if (ret) goto err_freq_table; |
34ee55075 [CPUFREQ] Add S3C... |
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
register_reboot_notifier(&s3c2416_cpufreq_reboot_notifier); return 0; err_freq_table: #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE regulator_put(s3c_freq->vddarm); err_vddarm: #endif clk_put(s3c_freq->armclk); err_armclk: clk_put(s3c_freq->hclk); err_hclk: clk_put(s3c_freq->armdiv); return ret; } |
34ee55075 [CPUFREQ] Add S3C... |
494 |
static struct cpufreq_driver s3c2416_cpufreq_driver = { |
ae6b42713 cpufreq: Mark ARM... |
495 |
.flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK, |
e96a41054 cpufreq: s3cx4xx:... |
496 |
.verify = cpufreq_generic_frequency_table_verify, |
9c0ebcf78 cpufreq: Implemen... |
497 |
.target_index = s3c2416_cpufreq_set_target, |
34ee55075 [CPUFREQ] Add S3C... |
498 499 500 |
.get = s3c2416_cpufreq_get_speed, .init = s3c2416_cpufreq_driver_init, .name = "s3c2416", |
e96a41054 cpufreq: s3cx4xx:... |
501 |
.attr = cpufreq_generic_attr, |
34ee55075 [CPUFREQ] Add S3C... |
502 503 504 505 506 507 508 |
}; static int __init s3c2416_cpufreq_init(void) { return cpufreq_register_driver(&s3c2416_cpufreq_driver); } module_init(s3c2416_cpufreq_init); |