spear-cpufreq.c 5.9 KB

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