ls1x-cpufreq.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * CPU Frequency Scaling for Loongson 1 SoC
  3. *
  4. * Copyright (C) 2014 Zhang, Keguang <keguang.zhang@gmail.com>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/cpu.h>
  13. #include <linux/cpufreq.h>
  14. #include <linux/delay.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <cpufreq.h>
  19. #include <loongson1.h>
  20. static struct {
  21. struct device *dev;
  22. struct clk *clk; /* CPU clk */
  23. struct clk *mux_clk; /* MUX of CPU clk */
  24. struct clk *pll_clk; /* PLL clk */
  25. struct clk *osc_clk; /* OSC clk */
  26. unsigned int max_freq;
  27. unsigned int min_freq;
  28. } ls1x_cpufreq;
  29. static int ls1x_cpufreq_notifier(struct notifier_block *nb,
  30. unsigned long val, void *data)
  31. {
  32. if (val == CPUFREQ_POSTCHANGE)
  33. current_cpu_data.udelay_val = loops_per_jiffy;
  34. return NOTIFY_OK;
  35. }
  36. static struct notifier_block ls1x_cpufreq_notifier_block = {
  37. .notifier_call = ls1x_cpufreq_notifier
  38. };
  39. static int ls1x_cpufreq_target(struct cpufreq_policy *policy,
  40. unsigned int index)
  41. {
  42. unsigned int old_freq, new_freq;
  43. old_freq = policy->cur;
  44. new_freq = policy->freq_table[index].frequency;
  45. /*
  46. * The procedure of reconfiguring CPU clk is as below.
  47. *
  48. * - Reparent CPU clk to OSC clk
  49. * - Reset CPU clock (very important)
  50. * - Reconfigure CPU DIV
  51. * - Reparent CPU clk back to CPU DIV clk
  52. */
  53. dev_dbg(ls1x_cpufreq.dev, "%u KHz --> %u KHz\n", old_freq, new_freq);
  54. clk_set_parent(policy->clk, ls1x_cpufreq.osc_clk);
  55. __raw_writel(__raw_readl(LS1X_CLK_PLL_DIV) | RST_CPU_EN | RST_CPU,
  56. LS1X_CLK_PLL_DIV);
  57. __raw_writel(__raw_readl(LS1X_CLK_PLL_DIV) & ~(RST_CPU_EN | RST_CPU),
  58. LS1X_CLK_PLL_DIV);
  59. clk_set_rate(ls1x_cpufreq.mux_clk, new_freq * 1000);
  60. clk_set_parent(policy->clk, ls1x_cpufreq.mux_clk);
  61. return 0;
  62. }
  63. static int ls1x_cpufreq_init(struct cpufreq_policy *policy)
  64. {
  65. struct cpufreq_frequency_table *freq_tbl;
  66. unsigned int pll_freq, freq;
  67. int steps, i, ret;
  68. pll_freq = clk_get_rate(ls1x_cpufreq.pll_clk) / 1000;
  69. steps = 1 << DIV_CPU_WIDTH;
  70. freq_tbl = kzalloc(sizeof(*freq_tbl) * steps, GFP_KERNEL);
  71. if (!freq_tbl) {
  72. dev_err(ls1x_cpufreq.dev,
  73. "failed to alloc cpufreq_frequency_table\n");
  74. ret = -ENOMEM;
  75. goto out;
  76. }
  77. for (i = 0; i < (steps - 1); i++) {
  78. freq = pll_freq / (i + 1);
  79. if ((freq < ls1x_cpufreq.min_freq) ||
  80. (freq > ls1x_cpufreq.max_freq))
  81. freq_tbl[i].frequency = CPUFREQ_ENTRY_INVALID;
  82. else
  83. freq_tbl[i].frequency = freq;
  84. dev_dbg(ls1x_cpufreq.dev,
  85. "cpufreq table: index %d: frequency %d\n", i,
  86. freq_tbl[i].frequency);
  87. }
  88. freq_tbl[i].frequency = CPUFREQ_TABLE_END;
  89. policy->clk = ls1x_cpufreq.clk;
  90. ret = cpufreq_generic_init(policy, freq_tbl, 0);
  91. if (ret)
  92. kfree(freq_tbl);
  93. out:
  94. return ret;
  95. }
  96. static int ls1x_cpufreq_exit(struct cpufreq_policy *policy)
  97. {
  98. kfree(policy->freq_table);
  99. return 0;
  100. }
  101. static struct cpufreq_driver ls1x_cpufreq_driver = {
  102. .name = "cpufreq-ls1x",
  103. .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  104. .verify = cpufreq_generic_frequency_table_verify,
  105. .target_index = ls1x_cpufreq_target,
  106. .get = cpufreq_generic_get,
  107. .init = ls1x_cpufreq_init,
  108. .exit = ls1x_cpufreq_exit,
  109. .attr = cpufreq_generic_attr,
  110. };
  111. static int ls1x_cpufreq_remove(struct platform_device *pdev)
  112. {
  113. cpufreq_unregister_notifier(&ls1x_cpufreq_notifier_block,
  114. CPUFREQ_TRANSITION_NOTIFIER);
  115. cpufreq_unregister_driver(&ls1x_cpufreq_driver);
  116. return 0;
  117. }
  118. static int ls1x_cpufreq_probe(struct platform_device *pdev)
  119. {
  120. struct plat_ls1x_cpufreq *pdata = pdev->dev.platform_data;
  121. struct clk *clk;
  122. int ret;
  123. if (!pdata || !pdata->clk_name || !pdata->osc_clk_name)
  124. return -EINVAL;
  125. ls1x_cpufreq.dev = &pdev->dev;
  126. clk = devm_clk_get(&pdev->dev, pdata->clk_name);
  127. if (IS_ERR(clk)) {
  128. dev_err(ls1x_cpufreq.dev, "unable to get %s clock\n",
  129. pdata->clk_name);
  130. ret = PTR_ERR(clk);
  131. goto out;
  132. }
  133. ls1x_cpufreq.clk = clk;
  134. clk = clk_get_parent(clk);
  135. if (IS_ERR(clk)) {
  136. dev_err(ls1x_cpufreq.dev, "unable to get parent of %s clock\n",
  137. __clk_get_name(ls1x_cpufreq.clk));
  138. ret = PTR_ERR(clk);
  139. goto out;
  140. }
  141. ls1x_cpufreq.mux_clk = clk;
  142. clk = clk_get_parent(clk);
  143. if (IS_ERR(clk)) {
  144. dev_err(ls1x_cpufreq.dev, "unable to get parent of %s clock\n",
  145. __clk_get_name(ls1x_cpufreq.mux_clk));
  146. ret = PTR_ERR(clk);
  147. goto out;
  148. }
  149. ls1x_cpufreq.pll_clk = clk;
  150. clk = devm_clk_get(&pdev->dev, pdata->osc_clk_name);
  151. if (IS_ERR(clk)) {
  152. dev_err(ls1x_cpufreq.dev, "unable to get %s clock\n",
  153. pdata->osc_clk_name);
  154. ret = PTR_ERR(clk);
  155. goto out;
  156. }
  157. ls1x_cpufreq.osc_clk = clk;
  158. ls1x_cpufreq.max_freq = pdata->max_freq;
  159. ls1x_cpufreq.min_freq = pdata->min_freq;
  160. ret = cpufreq_register_driver(&ls1x_cpufreq_driver);
  161. if (ret) {
  162. dev_err(ls1x_cpufreq.dev,
  163. "failed to register cpufreq driver: %d\n", ret);
  164. goto out;
  165. }
  166. ret = cpufreq_register_notifier(&ls1x_cpufreq_notifier_block,
  167. CPUFREQ_TRANSITION_NOTIFIER);
  168. if (!ret)
  169. goto out;
  170. dev_err(ls1x_cpufreq.dev, "failed to register cpufreq notifier: %d\n",
  171. ret);
  172. cpufreq_unregister_driver(&ls1x_cpufreq_driver);
  173. out:
  174. return ret;
  175. }
  176. static struct platform_driver ls1x_cpufreq_platdrv = {
  177. .driver = {
  178. .name = "ls1x-cpufreq",
  179. },
  180. .probe = ls1x_cpufreq_probe,
  181. .remove = ls1x_cpufreq_remove,
  182. };
  183. module_platform_driver(ls1x_cpufreq_platdrv);
  184. MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
  185. MODULE_DESCRIPTION("Loongson 1 CPUFreq driver");
  186. MODULE_LICENSE("GPL");