spear-cpufreq.c 5.8 KB

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