spear-cpufreq.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_device.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 struct clk *spear1340_cpu_get_possible_parent(unsigned long newfreq)
  30. {
  31. struct clk *sys_pclk;
  32. int pclk;
  33. /*
  34. * In SPEAr1340, cpu clk's parent sys clk can take input from
  35. * following sources
  36. */
  37. const char *sys_clk_src[] = {
  38. "sys_syn_clk",
  39. "pll1_clk",
  40. "pll2_clk",
  41. "pll3_clk",
  42. };
  43. /*
  44. * As sys clk can have multiple source with their own range
  45. * limitation so we choose possible sources accordingly
  46. */
  47. if (newfreq <= 300000000)
  48. pclk = 0; /* src is sys_syn_clk */
  49. else if (newfreq > 300000000 && newfreq <= 500000000)
  50. pclk = 3; /* src is pll3_clk */
  51. else if (newfreq == 600000000)
  52. pclk = 1; /* src is pll1_clk */
  53. else
  54. return ERR_PTR(-EINVAL);
  55. /* Get parent to sys clock */
  56. sys_pclk = clk_get(NULL, sys_clk_src[pclk]);
  57. if (IS_ERR(sys_pclk))
  58. pr_err("Failed to get %s clock\n", sys_clk_src[pclk]);
  59. return sys_pclk;
  60. }
  61. /*
  62. * In SPEAr1340, we cannot use newfreq directly because we need to actually
  63. * access a source clock (clk) which might not be ancestor of cpu at present.
  64. * Hence in SPEAr1340 we would operate on source clock directly before switching
  65. * cpu clock to it.
  66. */
  67. static int spear1340_set_cpu_rate(struct clk *sys_pclk, unsigned long newfreq)
  68. {
  69. struct clk *sys_clk;
  70. int ret = 0;
  71. sys_clk = clk_get_parent(spear_cpufreq.clk);
  72. if (IS_ERR(sys_clk)) {
  73. pr_err("failed to get cpu's parent (sys) clock\n");
  74. return PTR_ERR(sys_clk);
  75. }
  76. /* Set the rate of the source clock before changing the parent */
  77. ret = clk_set_rate(sys_pclk, newfreq);
  78. if (ret) {
  79. pr_err("Failed to set sys clk rate to %lu\n", newfreq);
  80. return ret;
  81. }
  82. ret = clk_set_parent(sys_clk, sys_pclk);
  83. if (ret) {
  84. pr_err("Failed to set sys clk parent\n");
  85. return ret;
  86. }
  87. return 0;
  88. }
  89. static int spear_cpufreq_target(struct cpufreq_policy *policy,
  90. unsigned int index)
  91. {
  92. long newfreq;
  93. struct clk *srcclk;
  94. int ret, mult = 1;
  95. newfreq = spear_cpufreq.freq_tbl[index].frequency * 1000;
  96. if (of_machine_is_compatible("st,spear1340")) {
  97. /*
  98. * SPEAr1340 is special in the sense that due to the possibility
  99. * of multiple clock sources for cpu clk's parent we can have
  100. * different clock source for different frequency of cpu clk.
  101. * Hence we need to choose one from amongst these possible clock
  102. * sources.
  103. */
  104. srcclk = spear1340_cpu_get_possible_parent(newfreq);
  105. if (IS_ERR(srcclk)) {
  106. pr_err("Failed to get src clk\n");
  107. return PTR_ERR(srcclk);
  108. }
  109. /* SPEAr1340: src clk is always 2 * intended cpu clk */
  110. mult = 2;
  111. } else {
  112. /*
  113. * src clock to be altered is ancestor of cpu clock. Hence we
  114. * can directly work on cpu clk
  115. */
  116. srcclk = spear_cpufreq.clk;
  117. }
  118. newfreq = clk_round_rate(srcclk, newfreq * mult);
  119. if (newfreq <= 0) {
  120. pr_err("clk_round_rate failed for cpu src clock\n");
  121. return newfreq;
  122. }
  123. if (mult == 2)
  124. ret = spear1340_set_cpu_rate(srcclk, newfreq);
  125. else
  126. ret = clk_set_rate(spear_cpufreq.clk, newfreq);
  127. if (ret)
  128. pr_err("CPU Freq: cpu clk_set_rate failed: %d\n", ret);
  129. return ret;
  130. }
  131. static int spear_cpufreq_init(struct cpufreq_policy *policy)
  132. {
  133. policy->clk = spear_cpufreq.clk;
  134. return cpufreq_generic_init(policy, spear_cpufreq.freq_tbl,
  135. spear_cpufreq.transition_latency);
  136. }
  137. static struct cpufreq_driver spear_cpufreq_driver = {
  138. .name = "cpufreq-spear",
  139. .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  140. .verify = cpufreq_generic_frequency_table_verify,
  141. .target_index = spear_cpufreq_target,
  142. .get = cpufreq_generic_get,
  143. .init = spear_cpufreq_init,
  144. .exit = cpufreq_generic_exit,
  145. .attr = cpufreq_generic_attr,
  146. };
  147. static int spear_cpufreq_driver_init(void)
  148. {
  149. struct device_node *np;
  150. const struct property *prop;
  151. struct cpufreq_frequency_table *freq_tbl;
  152. const __be32 *val;
  153. int cnt, i, ret;
  154. np = of_cpu_device_node_get(0);
  155. if (!np) {
  156. pr_err("No cpu node found");
  157. return -ENODEV;
  158. }
  159. if (of_property_read_u32(np, "clock-latency",
  160. &spear_cpufreq.transition_latency))
  161. spear_cpufreq.transition_latency = CPUFREQ_ETERNAL;
  162. prop = of_find_property(np, "cpufreq_tbl", NULL);
  163. if (!prop || !prop->value) {
  164. pr_err("Invalid cpufreq_tbl");
  165. ret = -ENODEV;
  166. goto out_put_node;
  167. }
  168. cnt = prop->length / sizeof(u32);
  169. val = prop->value;
  170. freq_tbl = kmalloc(sizeof(*freq_tbl) * (cnt + 1), GFP_KERNEL);
  171. if (!freq_tbl) {
  172. ret = -ENOMEM;
  173. goto out_put_node;
  174. }
  175. for (i = 0; i < cnt; i++) {
  176. freq_tbl[i].driver_data = i;
  177. freq_tbl[i].frequency = be32_to_cpup(val++);
  178. }
  179. freq_tbl[i].driver_data = i;
  180. freq_tbl[i].frequency = CPUFREQ_TABLE_END;
  181. spear_cpufreq.freq_tbl = freq_tbl;
  182. of_node_put(np);
  183. spear_cpufreq.clk = clk_get(NULL, "cpu_clk");
  184. if (IS_ERR(spear_cpufreq.clk)) {
  185. pr_err("Unable to get CPU clock\n");
  186. ret = PTR_ERR(spear_cpufreq.clk);
  187. goto out_put_mem;
  188. }
  189. ret = cpufreq_register_driver(&spear_cpufreq_driver);
  190. if (!ret)
  191. return 0;
  192. pr_err("failed register driver: %d\n", ret);
  193. clk_put(spear_cpufreq.clk);
  194. out_put_mem:
  195. kfree(freq_tbl);
  196. return ret;
  197. out_put_node:
  198. of_node_put(np);
  199. return ret;
  200. }
  201. late_initcall(spear_cpufreq_driver_init);
  202. MODULE_AUTHOR("Deepak Sikri <deepak.sikri@st.com>");
  203. MODULE_DESCRIPTION("SPEAr CPUFreq driver");
  204. MODULE_LICENSE("GPL");