sh-cpufreq.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/clk.h>
  26. #include <linux/percpu.h>
  27. #include <linux/sh_clk.h>
  28. static DEFINE_PER_CPU(struct clk, sh_cpuclk);
  29. struct cpufreq_target {
  30. struct cpufreq_policy *policy;
  31. unsigned int freq;
  32. };
  33. static unsigned int sh_cpufreq_get(unsigned int cpu)
  34. {
  35. return (clk_get_rate(&per_cpu(sh_cpuclk, cpu)) + 500) / 1000;
  36. }
  37. static long __sh_cpufreq_target(void *arg)
  38. {
  39. struct cpufreq_target *target = arg;
  40. struct cpufreq_policy *policy = target->policy;
  41. int cpu = policy->cpu;
  42. struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu);
  43. struct cpufreq_freqs freqs;
  44. struct device *dev;
  45. long freq;
  46. if (smp_processor_id() != cpu)
  47. return -ENODEV;
  48. dev = get_cpu_device(cpu);
  49. /* Convert target_freq from kHz to Hz */
  50. freq = clk_round_rate(cpuclk, target->freq * 1000);
  51. if (freq < (policy->min * 1000) || freq > (policy->max * 1000))
  52. return -EINVAL;
  53. dev_dbg(dev, "requested frequency %u Hz\n", target->freq * 1000);
  54. freqs.old = sh_cpufreq_get(cpu);
  55. freqs.new = (freq + 500) / 1000;
  56. freqs.flags = 0;
  57. cpufreq_freq_transition_begin(target->policy, &freqs);
  58. clk_set_rate(cpuclk, freq);
  59. cpufreq_freq_transition_end(target->policy, &freqs, 0);
  60. dev_dbg(dev, "set frequency %lu Hz\n", freq);
  61. return 0;
  62. }
  63. /*
  64. * Here we notify other drivers of the proposed change and the final change.
  65. */
  66. static int sh_cpufreq_target(struct cpufreq_policy *policy,
  67. unsigned int target_freq,
  68. unsigned int relation)
  69. {
  70. struct cpufreq_target data = { .policy = policy, .freq = target_freq };
  71. return work_on_cpu(policy->cpu, __sh_cpufreq_target, &data);
  72. }
  73. static int sh_cpufreq_verify(struct cpufreq_policy_data *policy)
  74. {
  75. struct clk *cpuclk = &per_cpu(sh_cpuclk, policy->cpu);
  76. if (policy->freq_table)
  77. return cpufreq_frequency_table_verify(policy);
  78. cpufreq_verify_within_cpu_limits(policy);
  79. policy->min = (clk_round_rate(cpuclk, 1) + 500) / 1000;
  80. policy->max = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
  81. cpufreq_verify_within_cpu_limits(policy);
  82. return 0;
  83. }
  84. static int sh_cpufreq_cpu_init(struct cpufreq_policy *policy)
  85. {
  86. unsigned int cpu = policy->cpu;
  87. struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu);
  88. struct cpufreq_frequency_table *freq_table;
  89. struct device *dev;
  90. dev = get_cpu_device(cpu);
  91. cpuclk = clk_get(dev, "cpu_clk");
  92. if (IS_ERR(cpuclk)) {
  93. dev_err(dev, "couldn't get CPU clk\n");
  94. return PTR_ERR(cpuclk);
  95. }
  96. freq_table = cpuclk->nr_freqs ? cpuclk->freq_table : NULL;
  97. if (freq_table) {
  98. policy->freq_table = freq_table;
  99. } else {
  100. dev_notice(dev, "no frequency table found, falling back "
  101. "to rate rounding.\n");
  102. policy->min = policy->cpuinfo.min_freq =
  103. (clk_round_rate(cpuclk, 1) + 500) / 1000;
  104. policy->max = policy->cpuinfo.max_freq =
  105. (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
  106. }
  107. return 0;
  108. }
  109. static void sh_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  110. {
  111. unsigned int cpu = policy->cpu;
  112. struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu);
  113. clk_put(cpuclk);
  114. }
  115. static struct cpufreq_driver sh_cpufreq_driver = {
  116. .name = "sh",
  117. .flags = CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING,
  118. .get = sh_cpufreq_get,
  119. .target = sh_cpufreq_target,
  120. .verify = sh_cpufreq_verify,
  121. .init = sh_cpufreq_cpu_init,
  122. .exit = sh_cpufreq_cpu_exit,
  123. };
  124. static int __init sh_cpufreq_module_init(void)
  125. {
  126. pr_notice("SuperH CPU frequency driver.\n");
  127. return cpufreq_register_driver(&sh_cpufreq_driver);
  128. }
  129. static void __exit sh_cpufreq_module_exit(void)
  130. {
  131. cpufreq_unregister_driver(&sh_cpufreq_driver);
  132. }
  133. module_init(sh_cpufreq_module_init);
  134. module_exit(sh_cpufreq_module_exit);
  135. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  136. MODULE_DESCRIPTION("cpufreq driver for SuperH");
  137. MODULE_LICENSE("GPL");