s3c64xx-cpufreq.c 6.3 KB

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