s3c64xx-cpufreq.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright 2009 Wolfson Microelectronics plc
  3. *
  4. * S3C64xx CPUfreq Support
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) "cpufreq: " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/init.h>
  14. #include <linux/cpufreq.h>
  15. #include <linux/clk.h>
  16. #include <linux/err.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/module.h>
  19. static struct clk *armclk;
  20. static struct regulator *vddarm;
  21. static unsigned long regulator_latency;
  22. #ifdef CONFIG_CPU_S3C6410
  23. struct s3c64xx_dvfs {
  24. unsigned int vddarm_min;
  25. unsigned int vddarm_max;
  26. };
  27. static struct s3c64xx_dvfs s3c64xx_dvfs_table[] = {
  28. [0] = { 1000000, 1150000 },
  29. [1] = { 1050000, 1150000 },
  30. [2] = { 1100000, 1150000 },
  31. [3] = { 1200000, 1350000 },
  32. [4] = { 1300000, 1350000 },
  33. };
  34. static struct cpufreq_frequency_table s3c64xx_freq_table[] = {
  35. { 0, 66000 },
  36. { 0, 100000 },
  37. { 0, 133000 },
  38. { 1, 200000 },
  39. { 1, 222000 },
  40. { 1, 266000 },
  41. { 2, 333000 },
  42. { 2, 400000 },
  43. { 2, 532000 },
  44. { 2, 533000 },
  45. { 3, 667000 },
  46. { 4, 800000 },
  47. { 0, CPUFREQ_TABLE_END },
  48. };
  49. #endif
  50. static int s3c64xx_cpufreq_verify_speed(struct cpufreq_policy *policy)
  51. {
  52. if (policy->cpu != 0)
  53. return -EINVAL;
  54. return cpufreq_frequency_table_verify(policy, s3c64xx_freq_table);
  55. }
  56. static unsigned int s3c64xx_cpufreq_get_speed(unsigned int cpu)
  57. {
  58. if (cpu != 0)
  59. return 0;
  60. return clk_get_rate(armclk) / 1000;
  61. }
  62. static int s3c64xx_cpufreq_set_target(struct cpufreq_policy *policy,
  63. unsigned int target_freq,
  64. unsigned int relation)
  65. {
  66. int ret;
  67. unsigned int i;
  68. struct cpufreq_freqs freqs;
  69. struct s3c64xx_dvfs *dvfs;
  70. ret = cpufreq_frequency_table_target(policy, s3c64xx_freq_table,
  71. target_freq, relation, &i);
  72. if (ret != 0)
  73. return ret;
  74. freqs.cpu = 0;
  75. freqs.old = clk_get_rate(armclk) / 1000;
  76. freqs.new = s3c64xx_freq_table[i].frequency;
  77. freqs.flags = 0;
  78. dvfs = &s3c64xx_dvfs_table[s3c64xx_freq_table[i].index];
  79. if (freqs.old == freqs.new)
  80. return 0;
  81. pr_debug("Transition %d-%dkHz\n", freqs.old, freqs.new);
  82. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  83. #ifdef CONFIG_REGULATOR
  84. if (vddarm && freqs.new > freqs.old) {
  85. ret = regulator_set_voltage(vddarm,
  86. dvfs->vddarm_min,
  87. dvfs->vddarm_max);
  88. if (ret != 0) {
  89. pr_err("Failed to set VDDARM for %dkHz: %d\n",
  90. freqs.new, ret);
  91. goto err;
  92. }
  93. }
  94. #endif
  95. ret = clk_set_rate(armclk, freqs.new * 1000);
  96. if (ret < 0) {
  97. pr_err("Failed to set rate %dkHz: %d\n",
  98. freqs.new, ret);
  99. goto err;
  100. }
  101. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  102. #ifdef CONFIG_REGULATOR
  103. if (vddarm && freqs.new < freqs.old) {
  104. ret = regulator_set_voltage(vddarm,
  105. dvfs->vddarm_min,
  106. dvfs->vddarm_max);
  107. if (ret != 0) {
  108. pr_err("Failed to set VDDARM for %dkHz: %d\n",
  109. freqs.new, ret);
  110. goto err_clk;
  111. }
  112. }
  113. #endif
  114. pr_debug("Set actual frequency %lukHz\n",
  115. clk_get_rate(armclk) / 1000);
  116. return 0;
  117. err_clk:
  118. if (clk_set_rate(armclk, freqs.old * 1000) < 0)
  119. pr_err("Failed to restore original clock rate\n");
  120. err:
  121. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  122. return ret;
  123. }
  124. #ifdef CONFIG_REGULATOR
  125. static void __init s3c64xx_cpufreq_config_regulator(void)
  126. {
  127. int count, v, i, found;
  128. struct cpufreq_frequency_table *freq;
  129. struct s3c64xx_dvfs *dvfs;
  130. count = regulator_count_voltages(vddarm);
  131. if (count < 0) {
  132. pr_err("Unable to check supported voltages\n");
  133. }
  134. freq = s3c64xx_freq_table;
  135. while (count > 0 && freq->frequency != CPUFREQ_TABLE_END) {
  136. if (freq->frequency == CPUFREQ_ENTRY_INVALID)
  137. continue;
  138. dvfs = &s3c64xx_dvfs_table[freq->index];
  139. found = 0;
  140. for (i = 0; i < count; i++) {
  141. v = regulator_list_voltage(vddarm, i);
  142. if (v >= dvfs->vddarm_min && v <= dvfs->vddarm_max)
  143. found = 1;
  144. }
  145. if (!found) {
  146. pr_debug("%dkHz unsupported by regulator\n",
  147. freq->frequency);
  148. freq->frequency = CPUFREQ_ENTRY_INVALID;
  149. }
  150. freq++;
  151. }
  152. /* Guess based on having to do an I2C/SPI write; in future we
  153. * will be able to query the regulator performance here. */
  154. regulator_latency = 1 * 1000 * 1000;
  155. }
  156. #endif
  157. static int s3c64xx_cpufreq_driver_init(struct cpufreq_policy *policy)
  158. {
  159. int ret;
  160. struct cpufreq_frequency_table *freq;
  161. if (policy->cpu != 0)
  162. return -EINVAL;
  163. if (s3c64xx_freq_table == NULL) {
  164. pr_err("No frequency information for this CPU\n");
  165. return -ENODEV;
  166. }
  167. armclk = clk_get(NULL, "armclk");
  168. if (IS_ERR(armclk)) {
  169. pr_err("Unable to obtain ARMCLK: %ld\n",
  170. PTR_ERR(armclk));
  171. return PTR_ERR(armclk);
  172. }
  173. #ifdef CONFIG_REGULATOR
  174. vddarm = regulator_get(NULL, "vddarm");
  175. if (IS_ERR(vddarm)) {
  176. ret = PTR_ERR(vddarm);
  177. pr_err("Failed to obtain VDDARM: %d\n", ret);
  178. pr_err("Only frequency scaling available\n");
  179. vddarm = NULL;
  180. } else {
  181. s3c64xx_cpufreq_config_regulator();
  182. }
  183. #endif
  184. freq = s3c64xx_freq_table;
  185. while (freq->frequency != CPUFREQ_TABLE_END) {
  186. unsigned long r;
  187. /* Check for frequencies we can generate */
  188. r = clk_round_rate(armclk, freq->frequency * 1000);
  189. r /= 1000;
  190. if (r != freq->frequency) {
  191. pr_debug("%dkHz unsupported by clock\n",
  192. freq->frequency);
  193. freq->frequency = CPUFREQ_ENTRY_INVALID;
  194. }
  195. /* If we have no regulator then assume startup
  196. * frequency is the maximum we can support. */
  197. if (!vddarm && freq->frequency > s3c64xx_cpufreq_get_speed(0))
  198. freq->frequency = CPUFREQ_ENTRY_INVALID;
  199. freq++;
  200. }
  201. policy->cur = clk_get_rate(armclk) / 1000;
  202. /* Datasheet says PLL stabalisation time (if we were to use
  203. * the PLLs, which we don't currently) is ~300us worst case,
  204. * but add some fudge.
  205. */
  206. policy->cpuinfo.transition_latency = (500 * 1000) + regulator_latency;
  207. ret = cpufreq_frequency_table_cpuinfo(policy, s3c64xx_freq_table);
  208. if (ret != 0) {
  209. pr_err("Failed to configure frequency table: %d\n",
  210. ret);
  211. regulator_put(vddarm);
  212. clk_put(armclk);
  213. }
  214. return ret;
  215. }
  216. static struct cpufreq_driver s3c64xx_cpufreq_driver = {
  217. .owner = THIS_MODULE,
  218. .flags = 0,
  219. .verify = s3c64xx_cpufreq_verify_speed,
  220. .target = s3c64xx_cpufreq_set_target,
  221. .get = s3c64xx_cpufreq_get_speed,
  222. .init = s3c64xx_cpufreq_driver_init,
  223. .name = "s3c",
  224. };
  225. static int __init s3c64xx_cpufreq_init(void)
  226. {
  227. return cpufreq_register_driver(&s3c64xx_cpufreq_driver);
  228. }
  229. module_init(s3c64xx_cpufreq_init);