s3c2416-cpufreq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. { 0, SOURCE_HCLK, FREQ_DVS },
  63. { 0, SOURCE_ARMDIV, 133333 },
  64. { 0, SOURCE_ARMDIV, 266666 },
  65. { 0, SOURCE_ARMDIV, 400000 },
  66. { 0, 0, CPUFREQ_TABLE_END },
  67. };
  68. static struct cpufreq_frequency_table s3c2450_freq_table[] = {
  69. { 0, SOURCE_HCLK, FREQ_DVS },
  70. { 0, SOURCE_ARMDIV, 133500 },
  71. { 0, SOURCE_ARMDIV, 267000 },
  72. { 0, SOURCE_ARMDIV, 534000 },
  73. { 0, 0, CPUFREQ_TABLE_END },
  74. };
  75. static unsigned int s3c2416_cpufreq_get_speed(unsigned int cpu)
  76. {
  77. struct s3c2416_data *s3c_freq = &s3c2416_cpufreq;
  78. if (cpu != 0)
  79. return 0;
  80. /* return our pseudo-frequency when in dvs mode */
  81. if (s3c_freq->is_dvs)
  82. return FREQ_DVS;
  83. return clk_get_rate(s3c_freq->armclk) / 1000;
  84. }
  85. static int s3c2416_cpufreq_set_armdiv(struct s3c2416_data *s3c_freq,
  86. unsigned int freq)
  87. {
  88. int ret;
  89. if (clk_get_rate(s3c_freq->armdiv) / 1000 != freq) {
  90. ret = clk_set_rate(s3c_freq->armdiv, freq * 1000);
  91. if (ret < 0) {
  92. pr_err("cpufreq: Failed to set armdiv rate %dkHz: %d\n",
  93. freq, ret);
  94. return ret;
  95. }
  96. }
  97. return 0;
  98. }
  99. static int s3c2416_cpufreq_enter_dvs(struct s3c2416_data *s3c_freq, int idx)
  100. {
  101. #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
  102. struct s3c2416_dvfs *dvfs;
  103. #endif
  104. int ret;
  105. if (s3c_freq->is_dvs) {
  106. pr_debug("cpufreq: already in dvs mode, nothing to do\n");
  107. return 0;
  108. }
  109. pr_debug("cpufreq: switching armclk to hclk (%lukHz)\n",
  110. clk_get_rate(s3c_freq->hclk) / 1000);
  111. ret = clk_set_parent(s3c_freq->armclk, s3c_freq->hclk);
  112. if (ret < 0) {
  113. pr_err("cpufreq: Failed to switch armclk to hclk: %d\n", ret);
  114. return ret;
  115. }
  116. #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
  117. /* changing the core voltage is only allowed when in dvs mode */
  118. if (s3c_freq->vddarm) {
  119. dvfs = &s3c2416_dvfs_table[idx];
  120. pr_debug("cpufreq: setting regulator to %d-%d\n",
  121. dvfs->vddarm_min, dvfs->vddarm_max);
  122. ret = regulator_set_voltage(s3c_freq->vddarm,
  123. dvfs->vddarm_min,
  124. dvfs->vddarm_max);
  125. /* when lowering the voltage failed, there is nothing to do */
  126. if (ret != 0)
  127. pr_err("cpufreq: Failed to set VDDARM: %d\n", ret);
  128. }
  129. #endif
  130. s3c_freq->is_dvs = 1;
  131. return 0;
  132. }
  133. static int s3c2416_cpufreq_leave_dvs(struct s3c2416_data *s3c_freq, int idx)
  134. {
  135. #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
  136. struct s3c2416_dvfs *dvfs;
  137. #endif
  138. int ret;
  139. if (!s3c_freq->is_dvs) {
  140. pr_debug("cpufreq: not in dvs mode, so can't leave\n");
  141. return 0;
  142. }
  143. #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
  144. if (s3c_freq->vddarm) {
  145. dvfs = &s3c2416_dvfs_table[idx];
  146. pr_debug("cpufreq: setting regulator to %d-%d\n",
  147. dvfs->vddarm_min, dvfs->vddarm_max);
  148. ret = regulator_set_voltage(s3c_freq->vddarm,
  149. dvfs->vddarm_min,
  150. dvfs->vddarm_max);
  151. if (ret != 0) {
  152. pr_err("cpufreq: Failed to set VDDARM: %d\n", ret);
  153. return ret;
  154. }
  155. }
  156. #endif
  157. /* force armdiv to hclk frequency for transition from dvs*/
  158. if (clk_get_rate(s3c_freq->armdiv) > clk_get_rate(s3c_freq->hclk)) {
  159. pr_debug("cpufreq: force armdiv to hclk frequency (%lukHz)\n",
  160. clk_get_rate(s3c_freq->hclk) / 1000);
  161. ret = s3c2416_cpufreq_set_armdiv(s3c_freq,
  162. clk_get_rate(s3c_freq->hclk) / 1000);
  163. if (ret < 0) {
  164. pr_err("cpufreq: Failed to set the armdiv to %lukHz: %d\n",
  165. clk_get_rate(s3c_freq->hclk) / 1000, ret);
  166. return ret;
  167. }
  168. }
  169. pr_debug("cpufreq: switching armclk parent to armdiv (%lukHz)\n",
  170. clk_get_rate(s3c_freq->armdiv) / 1000);
  171. ret = clk_set_parent(s3c_freq->armclk, s3c_freq->armdiv);
  172. if (ret < 0) {
  173. pr_err("cpufreq: Failed to switch armclk clock parent to armdiv: %d\n",
  174. ret);
  175. return ret;
  176. }
  177. s3c_freq->is_dvs = 0;
  178. return 0;
  179. }
  180. static int s3c2416_cpufreq_set_target(struct cpufreq_policy *policy,
  181. unsigned int index)
  182. {
  183. struct s3c2416_data *s3c_freq = &s3c2416_cpufreq;
  184. unsigned int new_freq;
  185. int idx, ret, to_dvs = 0;
  186. mutex_lock(&cpufreq_lock);
  187. idx = s3c_freq->freq_table[index].driver_data;
  188. if (idx == SOURCE_HCLK)
  189. to_dvs = 1;
  190. /* switching to dvs when it's not allowed */
  191. if (to_dvs && s3c_freq->disable_dvs) {
  192. pr_debug("cpufreq: entering dvs mode not allowed\n");
  193. ret = -EINVAL;
  194. goto out;
  195. }
  196. /* When leavin dvs mode, always switch the armdiv to the hclk rate
  197. * The S3C2416 has stability issues when switching directly to
  198. * higher frequencies.
  199. */
  200. new_freq = (s3c_freq->is_dvs && !to_dvs)
  201. ? clk_get_rate(s3c_freq->hclk) / 1000
  202. : s3c_freq->freq_table[index].frequency;
  203. if (to_dvs) {
  204. pr_debug("cpufreq: enter dvs\n");
  205. ret = s3c2416_cpufreq_enter_dvs(s3c_freq, idx);
  206. } else if (s3c_freq->is_dvs) {
  207. pr_debug("cpufreq: leave dvs\n");
  208. ret = s3c2416_cpufreq_leave_dvs(s3c_freq, idx);
  209. } else {
  210. pr_debug("cpufreq: change armdiv to %dkHz\n", new_freq);
  211. ret = s3c2416_cpufreq_set_armdiv(s3c_freq, new_freq);
  212. }
  213. out:
  214. mutex_unlock(&cpufreq_lock);
  215. return ret;
  216. }
  217. #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
  218. static void s3c2416_cpufreq_cfg_regulator(struct s3c2416_data *s3c_freq)
  219. {
  220. int count, v, i, found;
  221. struct cpufreq_frequency_table *pos;
  222. struct s3c2416_dvfs *dvfs;
  223. count = regulator_count_voltages(s3c_freq->vddarm);
  224. if (count < 0) {
  225. pr_err("cpufreq: Unable to check supported voltages\n");
  226. return;
  227. }
  228. if (!count)
  229. goto out;
  230. cpufreq_for_each_valid_entry(pos, s3c_freq->freq_table) {
  231. dvfs = &s3c2416_dvfs_table[pos->driver_data];
  232. found = 0;
  233. /* Check only the min-voltage, more is always ok on S3C2416 */
  234. for (i = 0; i < count; i++) {
  235. v = regulator_list_voltage(s3c_freq->vddarm, i);
  236. if (v >= dvfs->vddarm_min)
  237. found = 1;
  238. }
  239. if (!found) {
  240. pr_debug("cpufreq: %dkHz unsupported by regulator\n",
  241. pos->frequency);
  242. pos->frequency = CPUFREQ_ENTRY_INVALID;
  243. }
  244. }
  245. out:
  246. /* Guessed */
  247. s3c_freq->regulator_latency = 1 * 1000 * 1000;
  248. }
  249. #endif
  250. static int s3c2416_cpufreq_reboot_notifier_evt(struct notifier_block *this,
  251. unsigned long event, void *ptr)
  252. {
  253. struct s3c2416_data *s3c_freq = &s3c2416_cpufreq;
  254. int ret;
  255. mutex_lock(&cpufreq_lock);
  256. /* disable further changes */
  257. s3c_freq->disable_dvs = 1;
  258. mutex_unlock(&cpufreq_lock);
  259. /* some boards don't reconfigure the regulator on reboot, which
  260. * could lead to undervolting the cpu when the clock is reset.
  261. * Therefore we always leave the DVS mode on reboot.
  262. */
  263. if (s3c_freq->is_dvs) {
  264. pr_debug("cpufreq: leave dvs on reboot\n");
  265. ret = cpufreq_driver_target(cpufreq_cpu_get(0), FREQ_SLEEP, 0);
  266. if (ret < 0)
  267. return NOTIFY_BAD;
  268. }
  269. return NOTIFY_DONE;
  270. }
  271. static struct notifier_block s3c2416_cpufreq_reboot_notifier = {
  272. .notifier_call = s3c2416_cpufreq_reboot_notifier_evt,
  273. };
  274. static int s3c2416_cpufreq_driver_init(struct cpufreq_policy *policy)
  275. {
  276. struct s3c2416_data *s3c_freq = &s3c2416_cpufreq;
  277. struct cpufreq_frequency_table *pos;
  278. struct clk *msysclk;
  279. unsigned long rate;
  280. int ret;
  281. if (policy->cpu != 0)
  282. return -EINVAL;
  283. msysclk = clk_get(NULL, "msysclk");
  284. if (IS_ERR(msysclk)) {
  285. ret = PTR_ERR(msysclk);
  286. pr_err("cpufreq: Unable to obtain msysclk: %d\n", ret);
  287. return ret;
  288. }
  289. /*
  290. * S3C2416 and S3C2450 share the same processor-ID and also provide no
  291. * other means to distinguish them other than through the rate of
  292. * msysclk. On S3C2416 msysclk runs at 800MHz and on S3C2450 at 533MHz.
  293. */
  294. rate = clk_get_rate(msysclk);
  295. if (rate == 800 * 1000 * 1000) {
  296. pr_info("cpufreq: msysclk running at %lukHz, using S3C2416 frequency table\n",
  297. rate / 1000);
  298. s3c_freq->freq_table = s3c2416_freq_table;
  299. policy->cpuinfo.max_freq = 400000;
  300. } else if (rate / 1000 == 534000) {
  301. pr_info("cpufreq: msysclk running at %lukHz, using S3C2450 frequency table\n",
  302. rate / 1000);
  303. s3c_freq->freq_table = s3c2450_freq_table;
  304. policy->cpuinfo.max_freq = 534000;
  305. }
  306. /* not needed anymore */
  307. clk_put(msysclk);
  308. if (s3c_freq->freq_table == NULL) {
  309. pr_err("cpufreq: No frequency information for this CPU, msysclk at %lukHz\n",
  310. rate / 1000);
  311. return -ENODEV;
  312. }
  313. s3c_freq->is_dvs = 0;
  314. s3c_freq->armdiv = clk_get(NULL, "armdiv");
  315. if (IS_ERR(s3c_freq->armdiv)) {
  316. ret = PTR_ERR(s3c_freq->armdiv);
  317. pr_err("cpufreq: Unable to obtain ARMDIV: %d\n", ret);
  318. return ret;
  319. }
  320. s3c_freq->hclk = clk_get(NULL, "hclk");
  321. if (IS_ERR(s3c_freq->hclk)) {
  322. ret = PTR_ERR(s3c_freq->hclk);
  323. pr_err("cpufreq: Unable to obtain HCLK: %d\n", ret);
  324. goto err_hclk;
  325. }
  326. /* chech hclk rate, we only support the common 133MHz for now
  327. * hclk could also run at 66MHz, but this not often used
  328. */
  329. rate = clk_get_rate(s3c_freq->hclk);
  330. if (rate < 133 * 1000 * 1000) {
  331. pr_err("cpufreq: HCLK not at 133MHz\n");
  332. ret = -EINVAL;
  333. goto err_armclk;
  334. }
  335. s3c_freq->armclk = clk_get(NULL, "armclk");
  336. if (IS_ERR(s3c_freq->armclk)) {
  337. ret = PTR_ERR(s3c_freq->armclk);
  338. pr_err("cpufreq: Unable to obtain ARMCLK: %d\n", ret);
  339. goto err_armclk;
  340. }
  341. #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
  342. s3c_freq->vddarm = regulator_get(NULL, "vddarm");
  343. if (IS_ERR(s3c_freq->vddarm)) {
  344. ret = PTR_ERR(s3c_freq->vddarm);
  345. pr_err("cpufreq: Failed to obtain VDDARM: %d\n", ret);
  346. goto err_vddarm;
  347. }
  348. s3c2416_cpufreq_cfg_regulator(s3c_freq);
  349. #else
  350. s3c_freq->regulator_latency = 0;
  351. #endif
  352. cpufreq_for_each_entry(pos, s3c_freq->freq_table) {
  353. /* special handling for dvs mode */
  354. if (pos->driver_data == 0) {
  355. if (!s3c_freq->hclk) {
  356. pr_debug("cpufreq: %dkHz unsupported as it would need unavailable dvs mode\n",
  357. pos->frequency);
  358. pos->frequency = CPUFREQ_ENTRY_INVALID;
  359. } else {
  360. continue;
  361. }
  362. }
  363. /* Check for frequencies we can generate */
  364. rate = clk_round_rate(s3c_freq->armdiv,
  365. pos->frequency * 1000);
  366. rate /= 1000;
  367. if (rate != pos->frequency) {
  368. pr_debug("cpufreq: %dkHz unsupported by clock (clk_round_rate return %lu)\n",
  369. pos->frequency, rate);
  370. pos->frequency = CPUFREQ_ENTRY_INVALID;
  371. }
  372. }
  373. /* Datasheet says PLL stabalisation time must be at least 300us,
  374. * so but add some fudge. (reference in LOCKCON0 register description)
  375. */
  376. ret = cpufreq_generic_init(policy, s3c_freq->freq_table,
  377. (500 * 1000) + s3c_freq->regulator_latency);
  378. if (ret)
  379. goto err_freq_table;
  380. register_reboot_notifier(&s3c2416_cpufreq_reboot_notifier);
  381. return 0;
  382. err_freq_table:
  383. #ifdef CONFIG_ARM_S3C2416_CPUFREQ_VCORESCALE
  384. regulator_put(s3c_freq->vddarm);
  385. err_vddarm:
  386. #endif
  387. clk_put(s3c_freq->armclk);
  388. err_armclk:
  389. clk_put(s3c_freq->hclk);
  390. err_hclk:
  391. clk_put(s3c_freq->armdiv);
  392. return ret;
  393. }
  394. static struct cpufreq_driver s3c2416_cpufreq_driver = {
  395. .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  396. .verify = cpufreq_generic_frequency_table_verify,
  397. .target_index = s3c2416_cpufreq_set_target,
  398. .get = s3c2416_cpufreq_get_speed,
  399. .init = s3c2416_cpufreq_driver_init,
  400. .name = "s3c2416",
  401. .attr = cpufreq_generic_attr,
  402. };
  403. static int __init s3c2416_cpufreq_init(void)
  404. {
  405. return cpufreq_register_driver(&s3c2416_cpufreq_driver);
  406. }
  407. module_init(s3c2416_cpufreq_init);