s3c64xx-cpufreq.c 5.9 KB

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