spear-cpufreq.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * drivers/cpufreq/spear-cpufreq.c
  3. *
  4. * CPU Frequency Scaling for SPEAr platform
  5. *
  6. * Copyright (C) 2012 ST Microelectronics
  7. * Deepak Sikri <deepak.sikri@st.com>
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/clk.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/slab.h>
  21. #include <linux/types.h>
  22. /* SPEAr CPUFreq driver data structure */
  23. static struct {
  24. struct clk *clk;
  25. unsigned int transition_latency;
  26. struct cpufreq_frequency_table *freq_tbl;
  27. u32 cnt;
  28. } spear_cpufreq;
  29. static int spear_cpufreq_verify(struct cpufreq_policy *policy)
  30. {
  31. return cpufreq_frequency_table_verify(policy, spear_cpufreq.freq_tbl);
  32. }
  33. static unsigned int spear_cpufreq_get(unsigned int cpu)
  34. {
  35. return clk_get_rate(spear_cpufreq.clk) / 1000;
  36. }
  37. static struct clk *spear1340_cpu_get_possible_parent(unsigned long newfreq)
  38. {
  39. struct clk *sys_pclk;
  40. int pclk;
  41. /*
  42. * In SPEAr1340, cpu clk's parent sys clk can take input from
  43. * following sources
  44. */
  45. const char *sys_clk_src[] = {
  46. "sys_syn_clk",
  47. "pll1_clk",
  48. "pll2_clk",
  49. "pll3_clk",
  50. };
  51. /*
  52. * As sys clk can have multiple source with their own range
  53. * limitation so we choose possible sources accordingly
  54. */
  55. if (newfreq <= 300000000)
  56. pclk = 0; /* src is sys_syn_clk */
  57. else if (newfreq > 300000000 && newfreq <= 500000000)
  58. pclk = 3; /* src is pll3_clk */
  59. else if (newfreq == 600000000)
  60. pclk = 1; /* src is pll1_clk */
  61. else
  62. return ERR_PTR(-EINVAL);
  63. /* Get parent to sys clock */
  64. sys_pclk = clk_get(NULL, sys_clk_src[pclk]);
  65. if (IS_ERR(sys_pclk))
  66. pr_err("Failed to get %s clock\n", sys_clk_src[pclk]);
  67. return sys_pclk;
  68. }
  69. /*
  70. * In SPEAr1340, we cannot use newfreq directly because we need to actually
  71. * access a source clock (clk) which might not be ancestor of cpu at present.
  72. * Hence in SPEAr1340 we would operate on source clock directly before switching
  73. * cpu clock to it.
  74. */
  75. static int spear1340_set_cpu_rate(struct clk *sys_pclk, unsigned long newfreq)
  76. {
  77. struct clk *sys_clk;
  78. int ret = 0;
  79. sys_clk = clk_get_parent(spear_cpufreq.clk);
  80. if (IS_ERR(sys_clk)) {
  81. pr_err("failed to get cpu's parent (sys) clock\n");
  82. return PTR_ERR(sys_clk);
  83. }
  84. /* Set the rate of the source clock before changing the parent */
  85. ret = clk_set_rate(sys_pclk, newfreq);
  86. if (ret) {
  87. pr_err("Failed to set sys clk rate to %lu\n", newfreq);
  88. return ret;
  89. }
  90. ret = clk_set_parent(sys_clk, sys_pclk);
  91. if (ret) {
  92. pr_err("Failed to set sys clk parent\n");
  93. return ret;
  94. }
  95. return 0;
  96. }
  97. static int spear_cpufreq_target(struct cpufreq_policy *policy,
  98. unsigned int target_freq, unsigned int relation)
  99. {
  100. struct cpufreq_freqs freqs;
  101. unsigned long newfreq;
  102. struct clk *srcclk;
  103. int index, ret, mult = 1;
  104. if (cpufreq_frequency_table_target(policy, spear_cpufreq.freq_tbl,
  105. target_freq, relation, &index))
  106. return -EINVAL;
  107. freqs.cpu = policy->cpu;
  108. freqs.old = spear_cpufreq_get(0);
  109. newfreq = spear_cpufreq.freq_tbl[index].frequency * 1000;
  110. if (of_machine_is_compatible("st,spear1340")) {
  111. /*
  112. * SPEAr1340 is special in the sense that due to the possibility
  113. * of multiple clock sources for cpu clk's parent we can have
  114. * different clock source for different frequency of cpu clk.
  115. * Hence we need to choose one from amongst these possible clock
  116. * sources.
  117. */
  118. srcclk = spear1340_cpu_get_possible_parent(newfreq);
  119. if (IS_ERR(srcclk)) {
  120. pr_err("Failed to get src clk\n");
  121. return PTR_ERR(srcclk);
  122. }
  123. /* SPEAr1340: src clk is always 2 * intended cpu clk */
  124. mult = 2;
  125. } else {
  126. /*
  127. * src clock to be altered is ancestor of cpu clock. Hence we
  128. * can directly work on cpu clk
  129. */
  130. srcclk = spear_cpufreq.clk;
  131. }
  132. newfreq = clk_round_rate(srcclk, newfreq * mult);
  133. if (newfreq < 0) {
  134. pr_err("clk_round_rate failed for cpu src clock\n");
  135. return newfreq;
  136. }
  137. freqs.new = newfreq / 1000;
  138. freqs.new /= mult;
  139. for_each_cpu(freqs.cpu, policy->cpus)
  140. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  141. if (mult == 2)
  142. ret = spear1340_set_cpu_rate(srcclk, newfreq);
  143. else
  144. ret = clk_set_rate(spear_cpufreq.clk, newfreq);
  145. /* Get current rate after clk_set_rate, in case of failure */
  146. if (ret) {
  147. pr_err("CPU Freq: cpu clk_set_rate failed: %d\n", ret);
  148. freqs.new = clk_get_rate(spear_cpufreq.clk) / 1000;
  149. }
  150. for_each_cpu(freqs.cpu, policy->cpus)
  151. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  152. return ret;
  153. }
  154. static int spear_cpufreq_init(struct cpufreq_policy *policy)
  155. {
  156. int ret;
  157. ret = cpufreq_frequency_table_cpuinfo(policy, spear_cpufreq.freq_tbl);
  158. if (ret) {
  159. pr_err("cpufreq_frequency_table_cpuinfo() failed");
  160. return ret;
  161. }
  162. cpufreq_frequency_table_get_attr(spear_cpufreq.freq_tbl, policy->cpu);
  163. policy->cpuinfo.transition_latency = spear_cpufreq.transition_latency;
  164. policy->cur = spear_cpufreq_get(0);
  165. cpumask_setall(policy->cpus);
  166. return 0;
  167. }
  168. static int spear_cpufreq_exit(struct cpufreq_policy *policy)
  169. {
  170. cpufreq_frequency_table_put_attr(policy->cpu);
  171. return 0;
  172. }
  173. static struct freq_attr *spear_cpufreq_attr[] = {
  174. &cpufreq_freq_attr_scaling_available_freqs,
  175. NULL,
  176. };
  177. static struct cpufreq_driver spear_cpufreq_driver = {
  178. .name = "cpufreq-spear",
  179. .flags = CPUFREQ_STICKY,
  180. .verify = spear_cpufreq_verify,
  181. .target = spear_cpufreq_target,
  182. .get = spear_cpufreq_get,
  183. .init = spear_cpufreq_init,
  184. .exit = spear_cpufreq_exit,
  185. .attr = spear_cpufreq_attr,
  186. };
  187. static int spear_cpufreq_driver_init(void)
  188. {
  189. struct device_node *np;
  190. const struct property *prop;
  191. struct cpufreq_frequency_table *freq_tbl;
  192. const __be32 *val;
  193. int cnt, i, ret;
  194. np = of_find_node_by_path("/cpus/cpu@0");
  195. if (!np) {
  196. pr_err("No cpu node found");
  197. return -ENODEV;
  198. }
  199. if (of_property_read_u32(np, "clock-latency",
  200. &spear_cpufreq.transition_latency))
  201. spear_cpufreq.transition_latency = CPUFREQ_ETERNAL;
  202. prop = of_find_property(np, "cpufreq_tbl", NULL);
  203. if (!prop || !prop->value) {
  204. pr_err("Invalid cpufreq_tbl");
  205. ret = -ENODEV;
  206. goto out_put_node;
  207. }
  208. cnt = prop->length / sizeof(u32);
  209. val = prop->value;
  210. freq_tbl = kmalloc(sizeof(*freq_tbl) * (cnt + 1), GFP_KERNEL);
  211. if (!freq_tbl) {
  212. ret = -ENOMEM;
  213. goto out_put_node;
  214. }
  215. for (i = 0; i < cnt; i++) {
  216. freq_tbl[i].index = i;
  217. freq_tbl[i].frequency = be32_to_cpup(val++);
  218. }
  219. freq_tbl[i].index = i;
  220. freq_tbl[i].frequency = CPUFREQ_TABLE_END;
  221. spear_cpufreq.freq_tbl = freq_tbl;
  222. of_node_put(np);
  223. spear_cpufreq.clk = clk_get(NULL, "cpu_clk");
  224. if (IS_ERR(spear_cpufreq.clk)) {
  225. pr_err("Unable to get CPU clock\n");
  226. ret = PTR_ERR(spear_cpufreq.clk);
  227. goto out_put_mem;
  228. }
  229. ret = cpufreq_register_driver(&spear_cpufreq_driver);
  230. if (!ret)
  231. return 0;
  232. pr_err("failed register driver: %d\n", ret);
  233. clk_put(spear_cpufreq.clk);
  234. out_put_mem:
  235. kfree(freq_tbl);
  236. return ret;
  237. out_put_node:
  238. of_node_put(np);
  239. return ret;
  240. }
  241. late_initcall(spear_cpufreq_driver_init);
  242. MODULE_AUTHOR("Deepak Sikri <deepak.sikri@st.com>");
  243. MODULE_DESCRIPTION("SPEAr CPUFreq driver");
  244. MODULE_LICENSE("GPL");