cpufreq.c 6.0 KB

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