cpufreq.c 4.7 KB

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