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. cpufreq_generic_init(policy, spear_cpufreq.freq_tbl,
  136. spear_cpufreq.transition_latency);
  137. return 0;
  138. }
  139. static struct cpufreq_driver spear_cpufreq_driver = {
  140. .name = "cpufreq-spear",
  141. .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  142. .verify = cpufreq_generic_frequency_table_verify,
  143. .target_index = spear_cpufreq_target,
  144. .get = cpufreq_generic_get,
  145. .init = spear_cpufreq_init,
  146. .attr = cpufreq_generic_attr,
  147. };
  148. static int spear_cpufreq_probe(struct platform_device *pdev)
  149. {
  150. struct device_node *np;
  151. const struct property *prop;
  152. struct cpufreq_frequency_table *freq_tbl;
  153. const __be32 *val;
  154. int cnt, i, ret;
  155. np = of_cpu_device_node_get(0);
  156. if (!np) {
  157. pr_err("No cpu node found\n");
  158. return -ENODEV;
  159. }
  160. if (of_property_read_u32(np, "clock-latency",
  161. &spear_cpufreq.transition_latency))
  162. spear_cpufreq.transition_latency = CPUFREQ_ETERNAL;
  163. prop = of_find_property(np, "cpufreq_tbl", NULL);
  164. if (!prop || !prop->value) {
  165. pr_err("Invalid cpufreq_tbl\n");
  166. ret = -ENODEV;
  167. goto out_put_node;
  168. }
  169. cnt = prop->length / sizeof(u32);
  170. val = prop->value;
  171. freq_tbl = kcalloc(cnt + 1, sizeof(*freq_tbl), GFP_KERNEL);
  172. if (!freq_tbl) {
  173. ret = -ENOMEM;
  174. goto out_put_node;
  175. }
  176. for (i = 0; i < cnt; i++)
  177. freq_tbl[i].frequency = be32_to_cpup(val++);
  178. freq_tbl[i].frequency = CPUFREQ_TABLE_END;
  179. spear_cpufreq.freq_tbl = freq_tbl;
  180. of_node_put(np);
  181. spear_cpufreq.clk = clk_get(NULL, "cpu_clk");
  182. if (IS_ERR(spear_cpufreq.clk)) {
  183. pr_err("Unable to get CPU clock\n");
  184. ret = PTR_ERR(spear_cpufreq.clk);
  185. goto out_put_mem;
  186. }
  187. ret = cpufreq_register_driver(&spear_cpufreq_driver);
  188. if (!ret)
  189. return 0;
  190. pr_err("failed register driver: %d\n", ret);
  191. clk_put(spear_cpufreq.clk);
  192. out_put_mem:
  193. kfree(freq_tbl);
  194. return ret;
  195. out_put_node:
  196. of_node_put(np);
  197. return ret;
  198. }
  199. static struct platform_driver spear_cpufreq_platdrv = {
  200. .driver = {
  201. .name = "spear-cpufreq",
  202. },
  203. .probe = spear_cpufreq_probe,
  204. };
  205. module_platform_driver(spear_cpufreq_platdrv);
  206. MODULE_AUTHOR("Deepak Sikri <deepak.sikri@st.com>");
  207. MODULE_DESCRIPTION("SPEAr CPUFreq driver");
  208. MODULE_LICENSE("GPL");