s3c64xx-cpufreq.c 5.5 KB

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