cpufreq.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * arch/sh/kernel/cpufreq.c
  3. *
  4. * cpufreq driver for the SuperH processors.
  5. *
  6. * Copyright (C) 2002 - 2007 Paul Mundt
  7. * Copyright (C) 2002 M. R. Brown
  8. *
  9. * Clock framework bits from arch/avr32/mach-at32ap/cpufreq.c
  10. *
  11. * Copyright (C) 2004-2007 Atmel Corporation
  12. *
  13. * This file is subject to the terms and conditions of the GNU General Public
  14. * License. See the file "COPYING" in the main directory of this archive
  15. * for more details.
  16. */
  17. #include <linux/types.h>
  18. #include <linux/cpufreq.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/err.h>
  23. #include <linux/cpumask.h>
  24. #include <linux/smp.h>
  25. #include <linux/sched.h> /* set_cpus_allowed() */
  26. #include <linux/clk.h>
  27. static struct clk *cpuclk;
  28. static unsigned int sh_cpufreq_get(unsigned int cpu)
  29. {
  30. return (clk_get_rate(cpuclk) + 500) / 1000;
  31. }
  32. /*
  33. * Here we notify other drivers of the proposed change and the final change.
  34. */
  35. static int sh_cpufreq_target(struct cpufreq_policy *policy,
  36. unsigned int target_freq,
  37. unsigned int relation)
  38. {
  39. unsigned int cpu = policy->cpu;
  40. cpumask_t cpus_allowed;
  41. struct cpufreq_freqs freqs;
  42. long freq;
  43. if (!cpu_online(cpu))
  44. return -ENODEV;
  45. cpus_allowed = current->cpus_allowed;
  46. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  47. BUG_ON(smp_processor_id() != cpu);
  48. /* Convert target_freq from kHz to Hz */
  49. freq = clk_round_rate(cpuclk, target_freq * 1000);
  50. if (freq < (policy->min * 1000) || freq > (policy->max * 1000))
  51. return -EINVAL;
  52. pr_debug("cpufreq: requested frequency %u Hz\n", target_freq * 1000);
  53. freqs.cpu = cpu;
  54. freqs.old = sh_cpufreq_get(cpu);
  55. freqs.new = (freq + 500) / 1000;
  56. freqs.flags = 0;
  57. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  58. set_cpus_allowed_ptr(current, &cpus_allowed);
  59. clk_set_rate(cpuclk, freq);
  60. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  61. pr_debug("cpufreq: set frequency %lu Hz\n", freq);
  62. return 0;
  63. }
  64. static int sh_cpufreq_cpu_init(struct cpufreq_policy *policy)
  65. {
  66. if (!cpu_online(policy->cpu))
  67. return -ENODEV;
  68. cpuclk = clk_get(NULL, "cpu_clk");
  69. if (IS_ERR(cpuclk)) {
  70. printk(KERN_ERR "cpufreq: couldn't get CPU#%d clk\n",
  71. policy->cpu);
  72. return PTR_ERR(cpuclk);
  73. }
  74. /* cpuinfo and default policy values */
  75. policy->cpuinfo.min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;
  76. policy->cpuinfo.max_freq = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
  77. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  78. policy->cur = sh_cpufreq_get(policy->cpu);
  79. policy->min = policy->cpuinfo.min_freq;
  80. policy->max = policy->cpuinfo.max_freq;
  81. /*
  82. * Catch the cases where the clock framework hasn't been wired up
  83. * properly to support scaling.
  84. */
  85. if (unlikely(policy->min == policy->max)) {
  86. printk(KERN_ERR "cpufreq: clock framework rate rounding "
  87. "not supported on CPU#%d.\n", policy->cpu);
  88. clk_put(cpuclk);
  89. return -EINVAL;
  90. }
  91. printk(KERN_INFO "cpufreq: CPU#%d Frequencies - Minimum %u.%03u MHz, "
  92. "Maximum %u.%03u MHz.\n",
  93. policy->cpu, policy->min / 1000, policy->min % 1000,
  94. policy->max / 1000, policy->max % 1000);
  95. return 0;
  96. }
  97. static int sh_cpufreq_verify(struct cpufreq_policy *policy)
  98. {
  99. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  100. policy->cpuinfo.max_freq);
  101. return 0;
  102. }
  103. static int sh_cpufreq_exit(struct cpufreq_policy *policy)
  104. {
  105. clk_put(cpuclk);
  106. return 0;
  107. }
  108. static struct cpufreq_driver sh_cpufreq_driver = {
  109. .owner = THIS_MODULE,
  110. .name = "sh",
  111. .init = sh_cpufreq_cpu_init,
  112. .verify = sh_cpufreq_verify,
  113. .target = sh_cpufreq_target,
  114. .get = sh_cpufreq_get,
  115. .exit = sh_cpufreq_exit,
  116. };
  117. static int __init sh_cpufreq_module_init(void)
  118. {
  119. printk(KERN_INFO "cpufreq: SuperH CPU frequency driver.\n");
  120. return cpufreq_register_driver(&sh_cpufreq_driver);
  121. }
  122. static void __exit sh_cpufreq_module_exit(void)
  123. {
  124. cpufreq_unregister_driver(&sh_cpufreq_driver);
  125. }
  126. module_init(sh_cpufreq_module_init);
  127. module_exit(sh_cpufreq_module_exit);
  128. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  129. MODULE_DESCRIPTION("cpufreq driver for SuperH");
  130. MODULE_LICENSE("GPL");