cpufreq.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. static unsigned long regulator_latency;
  21. #ifdef CONFIG_CPU_S3C6410
  22. struct s3c64xx_dvfs {
  23. unsigned int vddarm_min;
  24. unsigned int vddarm_max;
  25. };
  26. static struct s3c64xx_dvfs s3c64xx_dvfs_table[] = {
  27. [0] = { 1000000, 1150000 },
  28. [1] = { 1050000, 1150000 },
  29. [2] = { 1100000, 1150000 },
  30. [3] = { 1200000, 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. { 2, 532000 },
  40. { 2, 533000 },
  41. { 3, 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_config_regulator(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. }
  129. freq = s3c64xx_freq_table;
  130. while (count > 0 && freq->frequency != CPUFREQ_TABLE_END) {
  131. if (freq->frequency == CPUFREQ_ENTRY_INVALID)
  132. continue;
  133. dvfs = &s3c64xx_dvfs_table[freq->index];
  134. found = 0;
  135. for (i = 0; i < count; i++) {
  136. v = regulator_list_voltage(vddarm, i);
  137. if (v >= dvfs->vddarm_min && v <= dvfs->vddarm_max)
  138. found = 1;
  139. }
  140. if (!found) {
  141. pr_debug("cpufreq: %dkHz unsupported by regulator\n",
  142. freq->frequency);
  143. freq->frequency = CPUFREQ_ENTRY_INVALID;
  144. }
  145. freq++;
  146. }
  147. /* Guess based on having to do an I2C/SPI write; in future we
  148. * will be able to query the regulator performance here. */
  149. regulator_latency = 1 * 1000 * 1000;
  150. }
  151. #endif
  152. static int __init s3c64xx_cpufreq_driver_init(struct cpufreq_policy *policy)
  153. {
  154. int ret;
  155. struct cpufreq_frequency_table *freq;
  156. if (policy->cpu != 0)
  157. return -EINVAL;
  158. if (s3c64xx_freq_table == NULL) {
  159. pr_err("cpufreq: No frequency information for this CPU\n");
  160. return -ENODEV;
  161. }
  162. armclk = clk_get(NULL, "armclk");
  163. if (IS_ERR(armclk)) {
  164. pr_err("cpufreq: Unable to obtain ARMCLK: %ld\n",
  165. PTR_ERR(armclk));
  166. return PTR_ERR(armclk);
  167. }
  168. #ifdef CONFIG_REGULATOR
  169. vddarm = regulator_get(NULL, "vddarm");
  170. if (IS_ERR(vddarm)) {
  171. ret = PTR_ERR(vddarm);
  172. pr_err("cpufreq: Failed to obtain VDDARM: %d\n", ret);
  173. pr_err("cpufreq: Only frequency scaling available\n");
  174. vddarm = NULL;
  175. } else {
  176. s3c64xx_cpufreq_config_regulator();
  177. }
  178. #endif
  179. freq = s3c64xx_freq_table;
  180. while (freq->frequency != CPUFREQ_TABLE_END) {
  181. unsigned long r;
  182. /* Check for frequencies we can generate */
  183. r = clk_round_rate(armclk, freq->frequency * 1000);
  184. r /= 1000;
  185. if (r != freq->frequency) {
  186. pr_debug("cpufreq: %dkHz unsupported by clock\n",
  187. freq->frequency);
  188. freq->frequency = CPUFREQ_ENTRY_INVALID;
  189. }
  190. /* If we have no regulator then assume startup
  191. * frequency is the maximum we can support. */
  192. if (!vddarm && freq->frequency > s3c64xx_cpufreq_get_speed(0))
  193. freq->frequency = CPUFREQ_ENTRY_INVALID;
  194. freq++;
  195. }
  196. policy->cur = clk_get_rate(armclk) / 1000;
  197. /* Datasheet says PLL stabalisation time (if we were to use
  198. * the PLLs, which we don't currently) is ~300us worst case,
  199. * but add some fudge.
  200. */
  201. policy->cpuinfo.transition_latency = (500 * 1000) + regulator_latency;
  202. ret = cpufreq_frequency_table_cpuinfo(policy, s3c64xx_freq_table);
  203. if (ret != 0) {
  204. pr_err("cpufreq: Failed to configure frequency table: %d\n",
  205. ret);
  206. regulator_put(vddarm);
  207. clk_put(armclk);
  208. }
  209. return ret;
  210. }
  211. static struct cpufreq_driver s3c64xx_cpufreq_driver = {
  212. .owner = THIS_MODULE,
  213. .flags = 0,
  214. .verify = s3c64xx_cpufreq_verify_speed,
  215. .target = s3c64xx_cpufreq_set_target,
  216. .get = s3c64xx_cpufreq_get_speed,
  217. .init = s3c64xx_cpufreq_driver_init,
  218. .name = "s3c",
  219. };
  220. static int __init s3c64xx_cpufreq_init(void)
  221. {
  222. return cpufreq_register_driver(&s3c64xx_cpufreq_driver);
  223. }
  224. module_init(s3c64xx_cpufreq_init);