s3c64xx-cpufreq.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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, 0, 66000 },
  35. { 0, 0, 100000 },
  36. { 0, 0, 133000 },
  37. { 0, 1, 200000 },
  38. { 0, 1, 222000 },
  39. { 0, 1, 266000 },
  40. { 0, 2, 333000 },
  41. { 0, 2, 400000 },
  42. { 0, 2, 532000 },
  43. { 0, 2, 533000 },
  44. { 0, 3, 667000 },
  45. { 0, 4, 800000 },
  46. { 0, 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. if (!count)
  105. goto out;
  106. cpufreq_for_each_valid_entry(freq, s3c64xx_freq_table) {
  107. dvfs = &s3c64xx_dvfs_table[freq->driver_data];
  108. found = 0;
  109. for (i = 0; i < count; i++) {
  110. v = regulator_list_voltage(vddarm, i);
  111. if (v >= dvfs->vddarm_min && v <= dvfs->vddarm_max)
  112. found = 1;
  113. }
  114. if (!found) {
  115. pr_debug("%dkHz unsupported by regulator\n",
  116. freq->frequency);
  117. freq->frequency = CPUFREQ_ENTRY_INVALID;
  118. }
  119. }
  120. out:
  121. /* Guess based on having to do an I2C/SPI write; in future we
  122. * will be able to query the regulator performance here. */
  123. regulator_latency = 1 * 1000 * 1000;
  124. }
  125. #endif
  126. static int s3c64xx_cpufreq_driver_init(struct cpufreq_policy *policy)
  127. {
  128. int ret;
  129. struct cpufreq_frequency_table *freq;
  130. if (policy->cpu != 0)
  131. return -EINVAL;
  132. if (s3c64xx_freq_table == NULL) {
  133. pr_err("No frequency information for this CPU\n");
  134. return -ENODEV;
  135. }
  136. policy->clk = clk_get(NULL, "armclk");
  137. if (IS_ERR(policy->clk)) {
  138. pr_err("Unable to obtain ARMCLK: %ld\n",
  139. PTR_ERR(policy->clk));
  140. return PTR_ERR(policy->clk);
  141. }
  142. #ifdef CONFIG_REGULATOR
  143. vddarm = regulator_get(NULL, "vddarm");
  144. if (IS_ERR(vddarm)) {
  145. ret = PTR_ERR(vddarm);
  146. pr_err("Failed to obtain VDDARM: %d\n", ret);
  147. pr_err("Only frequency scaling available\n");
  148. vddarm = NULL;
  149. } else {
  150. s3c64xx_cpufreq_config_regulator();
  151. }
  152. #endif
  153. cpufreq_for_each_entry(freq, s3c64xx_freq_table) {
  154. unsigned long r;
  155. /* Check for frequencies we can generate */
  156. r = clk_round_rate(policy->clk, freq->frequency * 1000);
  157. r /= 1000;
  158. if (r != freq->frequency) {
  159. pr_debug("%dkHz unsupported by clock\n",
  160. freq->frequency);
  161. freq->frequency = CPUFREQ_ENTRY_INVALID;
  162. }
  163. /* If we have no regulator then assume startup
  164. * frequency is the maximum we can support. */
  165. if (!vddarm && freq->frequency > clk_get_rate(policy->clk) / 1000)
  166. freq->frequency = CPUFREQ_ENTRY_INVALID;
  167. }
  168. /* Datasheet says PLL stabalisation time (if we were to use
  169. * the PLLs, which we don't currently) is ~300us worst case,
  170. * but add some fudge.
  171. */
  172. ret = cpufreq_generic_init(policy, s3c64xx_freq_table,
  173. (500 * 1000) + regulator_latency);
  174. if (ret != 0) {
  175. pr_err("Failed to configure frequency table: %d\n",
  176. ret);
  177. regulator_put(vddarm);
  178. clk_put(policy->clk);
  179. }
  180. return ret;
  181. }
  182. static struct cpufreq_driver s3c64xx_cpufreq_driver = {
  183. .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  184. .verify = cpufreq_generic_frequency_table_verify,
  185. .target_index = s3c64xx_cpufreq_set_target,
  186. .get = cpufreq_generic_get,
  187. .init = s3c64xx_cpufreq_driver_init,
  188. .name = "s3c",
  189. };
  190. static int __init s3c64xx_cpufreq_init(void)
  191. {
  192. return cpufreq_register_driver(&s3c64xx_cpufreq_driver);
  193. }
  194. module_init(s3c64xx_cpufreq_init);