s3c64xx-cpufreq.c 6.4 KB

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