s3c64xx.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/init.h>
  13. #include <linux/cpufreq.h>
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/regulator/consumer.h>
  17. static struct clk *armclk;
  18. static struct regulator *vddarm;
  19. static unsigned long regulator_latency;
  20. #ifdef CONFIG_CPU_S3C6410
  21. struct s3c64xx_dvfs {
  22. unsigned int vddarm_min;
  23. unsigned int vddarm_max;
  24. };
  25. static struct s3c64xx_dvfs s3c64xx_dvfs_table[] = {
  26. [0] = { 1000000, 1150000 },
  27. [1] = { 1050000, 1150000 },
  28. [2] = { 1100000, 1150000 },
  29. [3] = { 1200000, 1350000 },
  30. };
  31. static struct cpufreq_frequency_table s3c64xx_freq_table[] = {
  32. { 0, 66000 },
  33. { 0, 133000 },
  34. { 1, 222000 },
  35. { 1, 266000 },
  36. { 2, 333000 },
  37. { 2, 400000 },
  38. { 2, 532000 },
  39. { 2, 533000 },
  40. { 3, 667000 },
  41. { 0, CPUFREQ_TABLE_END },
  42. };
  43. #endif
  44. static int s3c64xx_cpufreq_verify_speed(struct cpufreq_policy *policy)
  45. {
  46. if (policy->cpu != 0)
  47. return -EINVAL;
  48. return cpufreq_frequency_table_verify(policy, s3c64xx_freq_table);
  49. }
  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 target_freq,
  58. unsigned int relation)
  59. {
  60. int ret;
  61. unsigned int i;
  62. struct cpufreq_freqs freqs;
  63. struct s3c64xx_dvfs *dvfs;
  64. ret = cpufreq_frequency_table_target(policy, s3c64xx_freq_table,
  65. target_freq, relation, &i);
  66. if (ret != 0)
  67. return ret;
  68. freqs.cpu = 0;
  69. freqs.old = clk_get_rate(armclk) / 1000;
  70. freqs.new = s3c64xx_freq_table[i].frequency;
  71. freqs.flags = 0;
  72. dvfs = &s3c64xx_dvfs_table[s3c64xx_freq_table[i].index];
  73. if (freqs.old == freqs.new)
  74. return 0;
  75. pr_debug("cpufreq: Transition %d-%dkHz\n", freqs.old, freqs.new);
  76. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  77. #ifdef CONFIG_REGULATOR
  78. if (vddarm && freqs.new > freqs.old) {
  79. ret = regulator_set_voltage(vddarm,
  80. dvfs->vddarm_min,
  81. dvfs->vddarm_max);
  82. if (ret != 0) {
  83. pr_err("cpufreq: Failed to set VDDARM for %dkHz: %d\n",
  84. freqs.new, ret);
  85. goto err;
  86. }
  87. }
  88. #endif
  89. ret = clk_set_rate(armclk, freqs.new * 1000);
  90. if (ret < 0) {
  91. pr_err("cpufreq: Failed to set rate %dkHz: %d\n",
  92. freqs.new, ret);
  93. goto err;
  94. }
  95. #ifdef CONFIG_REGULATOR
  96. if (vddarm && freqs.new < freqs.old) {
  97. ret = regulator_set_voltage(vddarm,
  98. dvfs->vddarm_min,
  99. dvfs->vddarm_max);
  100. if (ret != 0) {
  101. pr_err("cpufreq: Failed to set VDDARM for %dkHz: %d\n",
  102. freqs.new, ret);
  103. goto err_clk;
  104. }
  105. }
  106. #endif
  107. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  108. pr_debug("cpufreq: Set actual frequency %lukHz\n",
  109. clk_get_rate(armclk) / 1000);
  110. return 0;
  111. err_clk:
  112. if (clk_set_rate(armclk, freqs.old * 1000) < 0)
  113. pr_err("Failed to restore original clock rate\n");
  114. err:
  115. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  116. return ret;
  117. }
  118. #ifdef CONFIG_REGULATOR
  119. static void __init s3c64xx_cpufreq_config_regulator(void)
  120. {
  121. int count, v, i, found;
  122. struct cpufreq_frequency_table *freq;
  123. struct s3c64xx_dvfs *dvfs;
  124. count = regulator_count_voltages(vddarm);
  125. if (count < 0) {
  126. pr_err("cpufreq: Unable to check supported voltages\n");
  127. }
  128. freq = s3c64xx_freq_table;
  129. while (count > 0 && freq->frequency != CPUFREQ_TABLE_END) {
  130. if (freq->frequency == CPUFREQ_ENTRY_INVALID)
  131. continue;
  132. dvfs = &s3c64xx_dvfs_table[freq->index];
  133. found = 0;
  134. for (i = 0; i < count; i++) {
  135. v = regulator_list_voltage(vddarm, i);
  136. if (v >= dvfs->vddarm_min && v <= dvfs->vddarm_max)
  137. found = 1;
  138. }
  139. if (!found) {
  140. pr_debug("cpufreq: %dkHz unsupported by regulator\n",
  141. freq->frequency);
  142. freq->frequency = CPUFREQ_ENTRY_INVALID;
  143. }
  144. freq++;
  145. }
  146. /* Guess based on having to do an I2C/SPI write; in future we
  147. * will be able to query the regulator performance here. */
  148. regulator_latency = 1 * 1000 * 1000;
  149. }
  150. #endif
  151. static int s3c64xx_cpufreq_driver_init(struct cpufreq_policy *policy)
  152. {
  153. int ret;
  154. struct cpufreq_frequency_table *freq;
  155. if (policy->cpu != 0)
  156. return -EINVAL;
  157. if (s3c64xx_freq_table == NULL) {
  158. pr_err("cpufreq: No frequency information for this CPU\n");
  159. return -ENODEV;
  160. }
  161. armclk = clk_get(NULL, "armclk");
  162. if (IS_ERR(armclk)) {
  163. pr_err("cpufreq: Unable to obtain ARMCLK: %ld\n",
  164. PTR_ERR(armclk));
  165. return PTR_ERR(armclk);
  166. }
  167. #ifdef CONFIG_REGULATOR
  168. vddarm = regulator_get(NULL, "vddarm");
  169. if (IS_ERR(vddarm)) {
  170. ret = PTR_ERR(vddarm);
  171. pr_err("cpufreq: Failed to obtain VDDARM: %d\n", ret);
  172. pr_err("cpufreq: Only frequency scaling available\n");
  173. vddarm = NULL;
  174. } else {
  175. s3c64xx_cpufreq_config_regulator();
  176. }
  177. #endif
  178. freq = s3c64xx_freq_table;
  179. while (freq->frequency != CPUFREQ_TABLE_END) {
  180. unsigned long r;
  181. /* Check for frequencies we can generate */
  182. r = clk_round_rate(armclk, freq->frequency * 1000);
  183. r /= 1000;
  184. if (r != freq->frequency) {
  185. pr_debug("cpufreq: %dkHz unsupported by clock\n",
  186. freq->frequency);
  187. freq->frequency = CPUFREQ_ENTRY_INVALID;
  188. }
  189. /* If we have no regulator then assume startup
  190. * frequency is the maximum we can support. */
  191. if (!vddarm && freq->frequency > s3c64xx_cpufreq_get_speed(0))
  192. freq->frequency = CPUFREQ_ENTRY_INVALID;
  193. freq++;
  194. }
  195. policy->cur = clk_get_rate(armclk) / 1000;
  196. /* Datasheet says PLL stabalisation time (if we were to use
  197. * the PLLs, which we don't currently) is ~300us worst case,
  198. * but add some fudge.
  199. */
  200. policy->cpuinfo.transition_latency = (500 * 1000) + regulator_latency;
  201. ret = cpufreq_frequency_table_cpuinfo(policy, s3c64xx_freq_table);
  202. if (ret != 0) {
  203. pr_err("cpufreq: Failed to configure frequency table: %d\n",
  204. ret);
  205. regulator_put(vddarm);
  206. clk_put(armclk);
  207. }
  208. return ret;
  209. }
  210. static struct cpufreq_driver s3c64xx_cpufreq_driver = {
  211. .owner = THIS_MODULE,
  212. .flags = 0,
  213. .verify = s3c64xx_cpufreq_verify_speed,
  214. .target = s3c64xx_cpufreq_set_target,
  215. .get = s3c64xx_cpufreq_get_speed,
  216. .init = s3c64xx_cpufreq_driver_init,
  217. .name = "s3c",
  218. };
  219. static int __init s3c64xx_cpufreq_init(void)
  220. {
  221. return cpufreq_register_driver(&s3c64xx_cpufreq_driver);
  222. }
  223. module_init(s3c64xx_cpufreq_init);