cpufreq.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved.
  3. */
  4. /*
  5. * The code contained herein is licensed under the GNU General Public
  6. * License. You may obtain a copy of the GNU General Public License
  7. * Version 2 or later at the following locations:
  8. *
  9. * http://www.opensource.org/licenses/gpl-license.html
  10. * http://www.gnu.org/copyleft/gpl.html
  11. */
  12. /*
  13. * A driver for the Freescale Semiconductor i.MXC CPUfreq module.
  14. * The CPUFREQ driver is for controlling CPU frequency. It allows you to change
  15. * the CPU clock speed on the fly.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/cpufreq.h>
  19. #include <linux/clk.h>
  20. #include <linux/err.h>
  21. #include <linux/slab.h>
  22. #include <mach/hardware.h>
  23. #include <mach/clock.h>
  24. #define CLK32_FREQ 32768
  25. #define NANOSECOND (1000 * 1000 * 1000)
  26. struct cpu_op *(*get_cpu_op)(int *op);
  27. static int cpu_freq_khz_min;
  28. static int cpu_freq_khz_max;
  29. static struct clk *cpu_clk;
  30. static struct cpufreq_frequency_table *imx_freq_table;
  31. static int cpu_op_nr;
  32. static struct cpu_op *cpu_op_tbl;
  33. static int set_cpu_freq(int freq)
  34. {
  35. int ret = 0;
  36. int org_cpu_rate;
  37. org_cpu_rate = clk_get_rate(cpu_clk);
  38. if (org_cpu_rate == freq)
  39. return ret;
  40. ret = clk_set_rate(cpu_clk, freq);
  41. if (ret != 0) {
  42. printk(KERN_DEBUG "cannot set CPU clock rate\n");
  43. return ret;
  44. }
  45. return ret;
  46. }
  47. static int mxc_verify_speed(struct cpufreq_policy *policy)
  48. {
  49. if (policy->cpu != 0)
  50. return -EINVAL;
  51. return cpufreq_frequency_table_verify(policy, imx_freq_table);
  52. }
  53. static unsigned int mxc_get_speed(unsigned int cpu)
  54. {
  55. if (cpu)
  56. return 0;
  57. return clk_get_rate(cpu_clk) / 1000;
  58. }
  59. static int mxc_set_target(struct cpufreq_policy *policy,
  60. unsigned int target_freq, unsigned int relation)
  61. {
  62. struct cpufreq_freqs freqs;
  63. int freq_Hz;
  64. int ret = 0;
  65. unsigned int index;
  66. cpufreq_frequency_table_target(policy, imx_freq_table,
  67. target_freq, relation, &index);
  68. freq_Hz = imx_freq_table[index].frequency * 1000;
  69. freqs.old = clk_get_rate(cpu_clk) / 1000;
  70. freqs.new = freq_Hz / 1000;
  71. freqs.cpu = 0;
  72. freqs.flags = 0;
  73. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  74. ret = set_cpu_freq(freq_Hz);
  75. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  76. return ret;
  77. }
  78. static int mxc_cpufreq_init(struct cpufreq_policy *policy)
  79. {
  80. int ret;
  81. int i;
  82. printk(KERN_INFO "i.MXC CPU frequency driver\n");
  83. if (policy->cpu != 0)
  84. return -EINVAL;
  85. if (!get_cpu_op)
  86. return -EINVAL;
  87. cpu_clk = clk_get(NULL, "cpu_clk");
  88. if (IS_ERR(cpu_clk)) {
  89. printk(KERN_ERR "%s: failed to get cpu clock\n", __func__);
  90. return PTR_ERR(cpu_clk);
  91. }
  92. cpu_op_tbl = get_cpu_op(&cpu_op_nr);
  93. cpu_freq_khz_min = cpu_op_tbl[0].cpu_rate / 1000;
  94. cpu_freq_khz_max = cpu_op_tbl[0].cpu_rate / 1000;
  95. imx_freq_table = kmalloc(
  96. sizeof(struct cpufreq_frequency_table) * (cpu_op_nr + 1),
  97. GFP_KERNEL);
  98. if (!imx_freq_table) {
  99. ret = -ENOMEM;
  100. goto err1;
  101. }
  102. for (i = 0; i < cpu_op_nr; i++) {
  103. imx_freq_table[i].index = i;
  104. imx_freq_table[i].frequency = cpu_op_tbl[i].cpu_rate / 1000;
  105. if ((cpu_op_tbl[i].cpu_rate / 1000) < cpu_freq_khz_min)
  106. cpu_freq_khz_min = cpu_op_tbl[i].cpu_rate / 1000;
  107. if ((cpu_op_tbl[i].cpu_rate / 1000) > cpu_freq_khz_max)
  108. cpu_freq_khz_max = cpu_op_tbl[i].cpu_rate / 1000;
  109. }
  110. imx_freq_table[i].index = i;
  111. imx_freq_table[i].frequency = CPUFREQ_TABLE_END;
  112. policy->cur = clk_get_rate(cpu_clk) / 1000;
  113. policy->min = policy->cpuinfo.min_freq = cpu_freq_khz_min;
  114. policy->max = policy->cpuinfo.max_freq = cpu_freq_khz_max;
  115. /* Manual states, that PLL stabilizes in two CLK32 periods */
  116. policy->cpuinfo.transition_latency = 2 * NANOSECOND / CLK32_FREQ;
  117. ret = cpufreq_frequency_table_cpuinfo(policy, imx_freq_table);
  118. if (ret < 0) {
  119. printk(KERN_ERR "%s: failed to register i.MXC CPUfreq with error code %d\n",
  120. __func__, ret);
  121. goto err;
  122. }
  123. cpufreq_frequency_table_get_attr(imx_freq_table, policy->cpu);
  124. return 0;
  125. err:
  126. kfree(imx_freq_table);
  127. err1:
  128. clk_put(cpu_clk);
  129. return ret;
  130. }
  131. static int mxc_cpufreq_exit(struct cpufreq_policy *policy)
  132. {
  133. cpufreq_frequency_table_put_attr(policy->cpu);
  134. set_cpu_freq(cpu_freq_khz_max * 1000);
  135. clk_put(cpu_clk);
  136. kfree(imx_freq_table);
  137. return 0;
  138. }
  139. static struct cpufreq_driver mxc_driver = {
  140. .flags = CPUFREQ_STICKY,
  141. .verify = mxc_verify_speed,
  142. .target = mxc_set_target,
  143. .get = mxc_get_speed,
  144. .init = mxc_cpufreq_init,
  145. .exit = mxc_cpufreq_exit,
  146. .name = "imx",
  147. };
  148. static int __devinit mxc_cpufreq_driver_init(void)
  149. {
  150. return cpufreq_register_driver(&mxc_driver);
  151. }
  152. static void mxc_cpufreq_driver_exit(void)
  153. {
  154. cpufreq_unregister_driver(&mxc_driver);
  155. }
  156. module_init(mxc_cpufreq_driver_init);
  157. module_exit(mxc_cpufreq_driver_exit);
  158. MODULE_AUTHOR("Freescale Semiconductor Inc. Yong Shen <yong.shen@linaro.org>");
  159. MODULE_DESCRIPTION("CPUfreq driver for i.MX");
  160. MODULE_LICENSE("GPL");