s3c64xx-cpufreq.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2009 Wolfson Microelectronics plc
  4. *
  5. * S3C64xx CPUfreq Support
  6. */
  7. #define pr_fmt(fmt) "cpufreq: " fmt
  8. #include <linux/kernel.h>
  9. #include <linux/types.h>
  10. #include <linux/init.h>
  11. #include <linux/cpufreq.h>
  12. #include <linux/clk.h>
  13. #include <linux/err.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/module.h>
  16. static struct regulator *vddarm;
  17. static unsigned long regulator_latency;
  18. struct s3c64xx_dvfs {
  19. unsigned int vddarm_min;
  20. unsigned int vddarm_max;
  21. };
  22. #ifdef CONFIG_REGULATOR
  23. static struct s3c64xx_dvfs s3c64xx_dvfs_table[] = {
  24. [0] = { 1000000, 1150000 },
  25. [1] = { 1050000, 1150000 },
  26. [2] = { 1100000, 1150000 },
  27. [3] = { 1200000, 1350000 },
  28. [4] = { 1300000, 1350000 },
  29. };
  30. #endif
  31. static struct cpufreq_frequency_table s3c64xx_freq_table[] = {
  32. { 0, 0, 66000 },
  33. { 0, 0, 100000 },
  34. { 0, 0, 133000 },
  35. { 0, 1, 200000 },
  36. { 0, 1, 222000 },
  37. { 0, 1, 266000 },
  38. { 0, 2, 333000 },
  39. { 0, 2, 400000 },
  40. { 0, 2, 532000 },
  41. { 0, 2, 533000 },
  42. { 0, 3, 667000 },
  43. { 0, 4, 800000 },
  44. { 0, 0, CPUFREQ_TABLE_END },
  45. };
  46. static int s3c64xx_cpufreq_set_target(struct cpufreq_policy *policy,
  47. unsigned int index)
  48. {
  49. unsigned int new_freq = s3c64xx_freq_table[index].frequency;
  50. int ret;
  51. #ifdef CONFIG_REGULATOR
  52. struct s3c64xx_dvfs *dvfs;
  53. unsigned int old_freq;
  54. old_freq = clk_get_rate(policy->clk) / 1000;
  55. dvfs = &s3c64xx_dvfs_table[s3c64xx_freq_table[index].driver_data];
  56. if (vddarm && new_freq > old_freq) {
  57. ret = regulator_set_voltage(vddarm,
  58. dvfs->vddarm_min,
  59. dvfs->vddarm_max);
  60. if (ret != 0) {
  61. pr_err("Failed to set VDDARM for %dkHz: %d\n",
  62. new_freq, ret);
  63. return ret;
  64. }
  65. }
  66. #endif
  67. ret = clk_set_rate(policy->clk, new_freq * 1000);
  68. if (ret < 0) {
  69. pr_err("Failed to set rate %dkHz: %d\n",
  70. new_freq, ret);
  71. return ret;
  72. }
  73. #ifdef CONFIG_REGULATOR
  74. if (vddarm && new_freq < old_freq) {
  75. ret = regulator_set_voltage(vddarm,
  76. dvfs->vddarm_min,
  77. dvfs->vddarm_max);
  78. if (ret != 0) {
  79. pr_err("Failed to set VDDARM for %dkHz: %d\n",
  80. new_freq, ret);
  81. if (clk_set_rate(policy->clk, old_freq * 1000) < 0)
  82. pr_err("Failed to restore original clock rate\n");
  83. return ret;
  84. }
  85. }
  86. #endif
  87. pr_debug("Set actual frequency %lukHz\n",
  88. clk_get_rate(policy->clk) / 1000);
  89. return 0;
  90. }
  91. #ifdef CONFIG_REGULATOR
  92. static void s3c64xx_cpufreq_config_regulator(void)
  93. {
  94. int count, v, i, found;
  95. struct cpufreq_frequency_table *freq;
  96. struct s3c64xx_dvfs *dvfs;
  97. count = regulator_count_voltages(vddarm);
  98. if (count < 0) {
  99. pr_err("Unable to check supported voltages\n");
  100. }
  101. if (!count)
  102. goto out;
  103. cpufreq_for_each_valid_entry(freq, s3c64xx_freq_table) {
  104. dvfs = &s3c64xx_dvfs_table[freq->driver_data];
  105. found = 0;
  106. for (i = 0; i < count; i++) {
  107. v = regulator_list_voltage(vddarm, i);
  108. if (v >= dvfs->vddarm_min && v <= dvfs->vddarm_max)
  109. found = 1;
  110. }
  111. if (!found) {
  112. pr_debug("%dkHz unsupported by regulator\n",
  113. freq->frequency);
  114. freq->frequency = CPUFREQ_ENTRY_INVALID;
  115. }
  116. }
  117. out:
  118. /* Guess based on having to do an I2C/SPI write; in future we
  119. * will be able to query the regulator performance here. */
  120. regulator_latency = 1 * 1000 * 1000;
  121. }
  122. #endif
  123. static int s3c64xx_cpufreq_driver_init(struct cpufreq_policy *policy)
  124. {
  125. struct cpufreq_frequency_table *freq;
  126. if (policy->cpu != 0)
  127. return -EINVAL;
  128. policy->clk = clk_get(NULL, "armclk");
  129. if (IS_ERR(policy->clk)) {
  130. pr_err("Unable to obtain ARMCLK: %ld\n",
  131. PTR_ERR(policy->clk));
  132. return PTR_ERR(policy->clk);
  133. }
  134. #ifdef CONFIG_REGULATOR
  135. vddarm = regulator_get(NULL, "vddarm");
  136. if (IS_ERR(vddarm)) {
  137. pr_err("Failed to obtain VDDARM: %ld\n", PTR_ERR(vddarm));
  138. pr_err("Only frequency scaling available\n");
  139. vddarm = NULL;
  140. } else {
  141. s3c64xx_cpufreq_config_regulator();
  142. }
  143. #endif
  144. cpufreq_for_each_entry(freq, s3c64xx_freq_table) {
  145. unsigned long r;
  146. /* Check for frequencies we can generate */
  147. r = clk_round_rate(policy->clk, freq->frequency * 1000);
  148. r /= 1000;
  149. if (r != freq->frequency) {
  150. pr_debug("%dkHz unsupported by clock\n",
  151. freq->frequency);
  152. freq->frequency = CPUFREQ_ENTRY_INVALID;
  153. }
  154. /* If we have no regulator then assume startup
  155. * frequency is the maximum we can support. */
  156. if (!vddarm && freq->frequency > clk_get_rate(policy->clk) / 1000)
  157. freq->frequency = CPUFREQ_ENTRY_INVALID;
  158. }
  159. /* Datasheet says PLL stabalisation time (if we were to use
  160. * the PLLs, which we don't currently) is ~300us worst case,
  161. * but add some fudge.
  162. */
  163. cpufreq_generic_init(policy, s3c64xx_freq_table,
  164. (500 * 1000) + regulator_latency);
  165. return 0;
  166. }
  167. static struct cpufreq_driver s3c64xx_cpufreq_driver = {
  168. .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  169. .verify = cpufreq_generic_frequency_table_verify,
  170. .target_index = s3c64xx_cpufreq_set_target,
  171. .get = cpufreq_generic_get,
  172. .init = s3c64xx_cpufreq_driver_init,
  173. .name = "s3c",
  174. };
  175. static int __init s3c64xx_cpufreq_init(void)
  176. {
  177. return cpufreq_register_driver(&s3c64xx_cpufreq_driver);
  178. }
  179. module_init(s3c64xx_cpufreq_init);