s3c64xx-cpufreq.c 6.4 KB

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