cpufreq.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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(current, cpumask_of_cpu(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(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 clk\n");
  71. return PTR_ERR(cpuclk);
  72. }
  73. /* cpuinfo and default policy values */
  74. policy->cpuinfo.min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;
  75. policy->cpuinfo.max_freq = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
  76. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  77. policy->cur = sh_cpufreq_get(policy->cpu);
  78. policy->min = policy->cpuinfo.min_freq;
  79. policy->max = policy->cpuinfo.max_freq;
  80. /*
  81. * Catch the cases where the clock framework hasn't been wired up
  82. * properly to support scaling.
  83. */
  84. if (unlikely(policy->min == policy->max)) {
  85. printk(KERN_ERR "cpufreq: clock framework rate rounding "
  86. "not supported on this CPU.\n");
  87. clk_put(cpuclk);
  88. return -EINVAL;
  89. }
  90. printk(KERN_INFO "cpufreq: Frequencies - Minimum %u.%03u MHz, "
  91. "Maximum %u.%03u MHz.\n",
  92. policy->min / 1000, policy->min % 1000,
  93. policy->max / 1000, policy->max % 1000);
  94. return 0;
  95. }
  96. static int sh_cpufreq_verify(struct cpufreq_policy *policy)
  97. {
  98. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  99. policy->cpuinfo.max_freq);
  100. return 0;
  101. }
  102. static int sh_cpufreq_exit(struct cpufreq_policy *policy)
  103. {
  104. clk_put(cpuclk);
  105. return 0;
  106. }
  107. static struct cpufreq_driver sh_cpufreq_driver = {
  108. .owner = THIS_MODULE,
  109. .name = "sh",
  110. .init = sh_cpufreq_cpu_init,
  111. .verify = sh_cpufreq_verify,
  112. .target = sh_cpufreq_target,
  113. .get = sh_cpufreq_get,
  114. .exit = sh_cpufreq_exit,
  115. };
  116. static int __init sh_cpufreq_module_init(void)
  117. {
  118. printk(KERN_INFO "cpufreq: SuperH CPU frequency driver.\n");
  119. return cpufreq_register_driver(&sh_cpufreq_driver);
  120. }
  121. static void __exit sh_cpufreq_module_exit(void)
  122. {
  123. cpufreq_unregister_driver(&sh_cpufreq_driver);
  124. }
  125. module_init(sh_cpufreq_module_init);
  126. module_exit(sh_cpufreq_module_exit);
  127. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  128. MODULE_DESCRIPTION("cpufreq driver for SuperH");
  129. MODULE_LICENSE("GPL");