s3c64xx-cpufreq.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.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].driver_data];
  78. if (freqs.old == freqs.new)
  79. return 0;
  80. pr_debug("Transition %d-%dkHz\n", freqs.old, freqs.new);
  81. cpufreq_notify_transition(policy, &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("Failed to set VDDARM for %dkHz: %d\n",
  89. freqs.new, ret);
  90. freqs.new = freqs.old;
  91. goto post_notify;
  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. freqs.new = freqs.old;
  100. }
  101. post_notify:
  102. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  103. if (ret)
  104. goto err;
  105. #ifdef CONFIG_REGULATOR
  106. if (vddarm && freqs.new < freqs.old) {
  107. ret = regulator_set_voltage(vddarm,
  108. dvfs->vddarm_min,
  109. dvfs->vddarm_max);
  110. if (ret != 0) {
  111. pr_err("Failed to set VDDARM for %dkHz: %d\n",
  112. freqs.new, ret);
  113. goto err_clk;
  114. }
  115. }
  116. #endif
  117. pr_debug("Set actual frequency %lukHz\n",
  118. clk_get_rate(armclk) / 1000);
  119. return 0;
  120. err_clk:
  121. if (clk_set_rate(armclk, freqs.old * 1000) < 0)
  122. pr_err("Failed to restore original clock rate\n");
  123. err:
  124. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  125. return ret;
  126. }
  127. #ifdef CONFIG_REGULATOR
  128. static void __init s3c64xx_cpufreq_config_regulator(void)
  129. {
  130. int count, v, i, found;
  131. struct cpufreq_frequency_table *freq;
  132. struct s3c64xx_dvfs *dvfs;
  133. count = regulator_count_voltages(vddarm);
  134. if (count < 0) {
  135. pr_err("Unable to check supported voltages\n");
  136. }
  137. freq = s3c64xx_freq_table;
  138. while (count > 0 && freq->frequency != CPUFREQ_TABLE_END) {
  139. if (freq->frequency == CPUFREQ_ENTRY_INVALID)
  140. continue;
  141. dvfs = &s3c64xx_dvfs_table[freq->index];
  142. found = 0;
  143. for (i = 0; i < count; i++) {
  144. v = regulator_list_voltage(vddarm, i);
  145. if (v >= dvfs->vddarm_min && v <= dvfs->vddarm_max)
  146. found = 1;
  147. }
  148. if (!found) {
  149. pr_debug("%dkHz unsupported by regulator\n",
  150. freq->frequency);
  151. freq->frequency = CPUFREQ_ENTRY_INVALID;
  152. }
  153. freq++;
  154. }
  155. /* Guess based on having to do an I2C/SPI write; in future we
  156. * will be able to query the regulator performance here. */
  157. regulator_latency = 1 * 1000 * 1000;
  158. }
  159. #endif
  160. static int s3c64xx_cpufreq_driver_init(struct cpufreq_policy *policy)
  161. {
  162. int ret;
  163. struct cpufreq_frequency_table *freq;
  164. if (policy->cpu != 0)
  165. return -EINVAL;
  166. if (s3c64xx_freq_table == NULL) {
  167. pr_err("No frequency information for this CPU\n");
  168. return -ENODEV;
  169. }
  170. armclk = clk_get(NULL, "armclk");
  171. if (IS_ERR(armclk)) {
  172. pr_err("Unable to obtain ARMCLK: %ld\n",
  173. PTR_ERR(armclk));
  174. return PTR_ERR(armclk);
  175. }
  176. #ifdef CONFIG_REGULATOR
  177. vddarm = regulator_get(NULL, "vddarm");
  178. if (IS_ERR(vddarm)) {
  179. ret = PTR_ERR(vddarm);
  180. pr_err("Failed to obtain VDDARM: %d\n", ret);
  181. pr_err("Only frequency scaling available\n");
  182. vddarm = NULL;
  183. } else {
  184. s3c64xx_cpufreq_config_regulator();
  185. }
  186. #endif
  187. freq = s3c64xx_freq_table;
  188. while (freq->frequency != CPUFREQ_TABLE_END) {
  189. unsigned long r;
  190. /* Check for frequencies we can generate */
  191. r = clk_round_rate(armclk, freq->frequency * 1000);
  192. r /= 1000;
  193. if (r != freq->frequency) {
  194. pr_debug("%dkHz unsupported by clock\n",
  195. freq->frequency);
  196. freq->frequency = CPUFREQ_ENTRY_INVALID;
  197. }
  198. /* If we have no regulator then assume startup
  199. * frequency is the maximum we can support. */
  200. if (!vddarm && freq->frequency > s3c64xx_cpufreq_get_speed(0))
  201. freq->frequency = CPUFREQ_ENTRY_INVALID;
  202. freq++;
  203. }
  204. policy->cur = clk_get_rate(armclk) / 1000;
  205. /* Datasheet says PLL stabalisation time (if we were to use
  206. * the PLLs, which we don't currently) is ~300us worst case,
  207. * but add some fudge.
  208. */
  209. policy->cpuinfo.transition_latency = (500 * 1000) + regulator_latency;
  210. ret = cpufreq_frequency_table_cpuinfo(policy, s3c64xx_freq_table);
  211. if (ret != 0) {
  212. pr_err("Failed to configure frequency table: %d\n",
  213. ret);
  214. regulator_put(vddarm);
  215. clk_put(armclk);
  216. }
  217. return ret;
  218. }
  219. static struct cpufreq_driver s3c64xx_cpufreq_driver = {
  220. .flags = 0,
  221. .verify = s3c64xx_cpufreq_verify_speed,
  222. .target = s3c64xx_cpufreq_set_target,
  223. .get = s3c64xx_cpufreq_get_speed,
  224. .init = s3c64xx_cpufreq_driver_init,
  225. .name = "s3c",
  226. };
  227. static int __init s3c64xx_cpufreq_init(void)
  228. {
  229. return cpufreq_register_driver(&s3c64xx_cpufreq_driver);
  230. }
  231. module_init(s3c64xx_cpufreq_init);