s3c2416-cpufreq.c 13 KB

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