sh-cpufreq.c 4.8 KB

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