cpufreq.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * arch/sh/kernel/cpufreq.c
  3. *
  4. * cpufreq driver for the SuperH processors.
  5. *
  6. * Copyright (C) 2002 - 2012 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. #define pr_fmt(fmt) "cpufreq: " fmt
  18. #include <linux/types.h>
  19. #include <linux/cpufreq.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/err.h>
  24. #include <linux/cpumask.h>
  25. #include <linux/cpu.h>
  26. #include <linux/smp.h>
  27. #include <linux/sched.h> /* set_cpus_allowed() */
  28. #include <linux/clk.h>
  29. #include <linux/percpu.h>
  30. #include <linux/sh_clk.h>
  31. static DEFINE_PER_CPU(struct clk, sh_cpuclk);
  32. static unsigned int sh_cpufreq_get(unsigned int cpu)
  33. {
  34. return (clk_get_rate(&per_cpu(sh_cpuclk, cpu)) + 500) / 1000;
  35. }
  36. /*
  37. * Here we notify other drivers of the proposed change and the final change.
  38. */
  39. static int sh_cpufreq_target(struct cpufreq_policy *policy,
  40. unsigned int target_freq,
  41. unsigned int relation)
  42. {
  43. unsigned int cpu = policy->cpu;
  44. struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu);
  45. cpumask_t cpus_allowed;
  46. struct cpufreq_freqs freqs;
  47. struct device *dev;
  48. long freq;
  49. if (!cpu_online(cpu))
  50. return -ENODEV;
  51. cpus_allowed = current->cpus_allowed;
  52. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  53. BUG_ON(smp_processor_id() != cpu);
  54. dev = get_cpu_device(cpu);
  55. /* Convert target_freq from kHz to Hz */
  56. freq = clk_round_rate(cpuclk, target_freq * 1000);
  57. if (freq < (policy->min * 1000) || freq > (policy->max * 1000))
  58. return -EINVAL;
  59. dev_dbg(dev, "requested frequency %u Hz\n", target_freq * 1000);
  60. freqs.cpu = cpu;
  61. freqs.old = sh_cpufreq_get(cpu);
  62. freqs.new = (freq + 500) / 1000;
  63. freqs.flags = 0;
  64. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  65. set_cpus_allowed_ptr(current, &cpus_allowed);
  66. clk_set_rate(cpuclk, freq);
  67. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  68. dev_dbg(dev, "set frequency %lu Hz\n", freq);
  69. return 0;
  70. }
  71. static int sh_cpufreq_verify(struct cpufreq_policy *policy)
  72. {
  73. struct clk *cpuclk = &per_cpu(sh_cpuclk, policy->cpu);
  74. struct cpufreq_frequency_table *freq_table;
  75. freq_table = cpuclk->nr_freqs ? cpuclk->freq_table : NULL;
  76. if (freq_table)
  77. return cpufreq_frequency_table_verify(policy, freq_table);
  78. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  79. policy->cpuinfo.max_freq);
  80. policy->min = (clk_round_rate(cpuclk, 1) + 500) / 1000;
  81. policy->max = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
  82. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  83. policy->cpuinfo.max_freq);
  84. return 0;
  85. }
  86. static int sh_cpufreq_cpu_init(struct cpufreq_policy *policy)
  87. {
  88. unsigned int cpu = policy->cpu;
  89. struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu);
  90. struct cpufreq_frequency_table *freq_table;
  91. struct device *dev;
  92. if (!cpu_online(cpu))
  93. return -ENODEV;
  94. dev = get_cpu_device(cpu);
  95. cpuclk = clk_get(dev, "cpu_clk");
  96. if (IS_ERR(cpuclk)) {
  97. dev_err(dev, "couldn't get CPU clk\n");
  98. return PTR_ERR(cpuclk);
  99. }
  100. policy->cur = policy->min = policy->max = sh_cpufreq_get(cpu);
  101. freq_table = cpuclk->nr_freqs ? cpuclk->freq_table : NULL;
  102. if (freq_table) {
  103. int result;
  104. result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
  105. if (!result)
  106. cpufreq_frequency_table_get_attr(freq_table, cpu);
  107. } else {
  108. dev_notice(dev, "no frequency table found, falling back "
  109. "to rate rounding.\n");
  110. policy->cpuinfo.min_freq =
  111. (clk_round_rate(cpuclk, 1) + 500) / 1000;
  112. policy->cpuinfo.max_freq =
  113. (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
  114. }
  115. policy->min = policy->cpuinfo.min_freq;
  116. policy->max = policy->cpuinfo.max_freq;
  117. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  118. dev_info(dev, "CPU Frequencies - Minimum %u.%03u MHz, "
  119. "Maximum %u.%03u MHz.\n",
  120. policy->min / 1000, policy->min % 1000,
  121. policy->max / 1000, policy->max % 1000);
  122. return 0;
  123. }
  124. static int sh_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  125. {
  126. unsigned int cpu = policy->cpu;
  127. struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu);
  128. cpufreq_frequency_table_put_attr(cpu);
  129. clk_put(cpuclk);
  130. return 0;
  131. }
  132. static struct freq_attr *sh_freq_attr[] = {
  133. &cpufreq_freq_attr_scaling_available_freqs,
  134. NULL,
  135. };
  136. static struct cpufreq_driver sh_cpufreq_driver = {
  137. .owner = THIS_MODULE,
  138. .name = "sh",
  139. .get = sh_cpufreq_get,
  140. .target = sh_cpufreq_target,
  141. .verify = sh_cpufreq_verify,
  142. .init = sh_cpufreq_cpu_init,
  143. .exit = sh_cpufreq_cpu_exit,
  144. .attr = sh_freq_attr,
  145. };
  146. static int __init sh_cpufreq_module_init(void)
  147. {
  148. pr_notice("SuperH CPU frequency driver.\n");
  149. return cpufreq_register_driver(&sh_cpufreq_driver);
  150. }
  151. static void __exit sh_cpufreq_module_exit(void)
  152. {
  153. cpufreq_unregister_driver(&sh_cpufreq_driver);
  154. }
  155. module_init(sh_cpufreq_module_init);
  156. module_exit(sh_cpufreq_module_exit);
  157. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  158. MODULE_DESCRIPTION("cpufreq driver for SuperH");
  159. MODULE_LICENSE("GPL");