s3c64xx-cpufreq.c 5.5 KB

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