cpufreq.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2004-2007 Atmel Corporation
  3. *
  4. * Based on MIPS implementation arch/mips/kernel/time.c
  5. * Copyright 2001 MontaVista Software Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. /*#define DEBUG*/
  12. #include <linux/kernel.h>
  13. #include <linux/types.h>
  14. #include <linux/init.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/io.h>
  17. #include <linux/clk.h>
  18. #include <linux/err.h>
  19. #include <linux/export.h>
  20. #include <asm/system.h>
  21. static struct clk *cpuclk;
  22. static int at32_verify_speed(struct cpufreq_policy *policy)
  23. {
  24. if (policy->cpu != 0)
  25. return -EINVAL;
  26. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  27. policy->cpuinfo.max_freq);
  28. return 0;
  29. }
  30. static unsigned int at32_get_speed(unsigned int cpu)
  31. {
  32. /* No SMP support */
  33. if (cpu)
  34. return 0;
  35. return (unsigned int)((clk_get_rate(cpuclk) + 500) / 1000);
  36. }
  37. static unsigned int ref_freq;
  38. static unsigned long loops_per_jiffy_ref;
  39. static int at32_set_target(struct cpufreq_policy *policy,
  40. unsigned int target_freq,
  41. unsigned int relation)
  42. {
  43. struct cpufreq_freqs freqs;
  44. long freq;
  45. /* Convert target_freq from kHz to Hz */
  46. freq = clk_round_rate(cpuclk, target_freq * 1000);
  47. /* Check if policy->min <= new_freq <= policy->max */
  48. if(freq < (policy->min * 1000) || freq > (policy->max * 1000))
  49. return -EINVAL;
  50. pr_debug("cpufreq: requested frequency %u Hz\n", target_freq * 1000);
  51. freqs.old = at32_get_speed(0);
  52. freqs.new = (freq + 500) / 1000;
  53. freqs.cpu = 0;
  54. freqs.flags = 0;
  55. if (!ref_freq) {
  56. ref_freq = freqs.old;
  57. loops_per_jiffy_ref = boot_cpu_data.loops_per_jiffy;
  58. }
  59. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  60. if (freqs.old < freqs.new)
  61. boot_cpu_data.loops_per_jiffy = cpufreq_scale(
  62. loops_per_jiffy_ref, ref_freq, freqs.new);
  63. clk_set_rate(cpuclk, freq);
  64. if (freqs.new < freqs.old)
  65. boot_cpu_data.loops_per_jiffy = cpufreq_scale(
  66. loops_per_jiffy_ref, ref_freq, freqs.new);
  67. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  68. pr_debug("cpufreq: set frequency %lu Hz\n", freq);
  69. return 0;
  70. }
  71. static int __init at32_cpufreq_driver_init(struct cpufreq_policy *policy)
  72. {
  73. if (policy->cpu != 0)
  74. return -EINVAL;
  75. cpuclk = clk_get(NULL, "cpu");
  76. if (IS_ERR(cpuclk)) {
  77. pr_debug("cpufreq: could not get CPU clk\n");
  78. return PTR_ERR(cpuclk);
  79. }
  80. policy->cpuinfo.min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;
  81. policy->cpuinfo.max_freq = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
  82. policy->cpuinfo.transition_latency = 0;
  83. policy->cur = at32_get_speed(0);
  84. policy->min = policy->cpuinfo.min_freq;
  85. policy->max = policy->cpuinfo.max_freq;
  86. printk("cpufreq: AT32AP CPU frequency driver\n");
  87. return 0;
  88. }
  89. static struct cpufreq_driver at32_driver = {
  90. .name = "at32ap",
  91. .owner = THIS_MODULE,
  92. .init = at32_cpufreq_driver_init,
  93. .verify = at32_verify_speed,
  94. .target = at32_set_target,
  95. .get = at32_get_speed,
  96. .flags = CPUFREQ_STICKY,
  97. };
  98. static int __init at32_cpufreq_init(void)
  99. {
  100. return cpufreq_register_driver(&at32_driver);
  101. }
  102. late_initcall(at32_cpufreq_init);