cpufreq.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <asm/system.h>
  20. static struct clk *cpuclk;
  21. static int at32_verify_speed(struct cpufreq_policy *policy)
  22. {
  23. if (policy->cpu != 0)
  24. return -EINVAL;
  25. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  26. policy->cpuinfo.max_freq);
  27. return 0;
  28. }
  29. static unsigned int at32_get_speed(unsigned int cpu)
  30. {
  31. /* No SMP support */
  32. if (cpu)
  33. return 0;
  34. return (unsigned int)((clk_get_rate(cpuclk) + 500) / 1000);
  35. }
  36. static unsigned int ref_freq;
  37. static unsigned long loops_per_jiffy_ref;
  38. static int at32_set_target(struct cpufreq_policy *policy,
  39. unsigned int target_freq,
  40. unsigned int relation)
  41. {
  42. struct cpufreq_freqs freqs;
  43. long freq;
  44. /* Convert target_freq from kHz to Hz */
  45. freq = clk_round_rate(cpuclk, target_freq * 1000);
  46. /* Check if policy->min <= new_freq <= policy->max */
  47. if(freq < (policy->min * 1000) || freq > (policy->max * 1000))
  48. return -EINVAL;
  49. pr_debug("cpufreq: requested frequency %u Hz\n", target_freq * 1000);
  50. freqs.old = at32_get_speed(0);
  51. freqs.new = (freq + 500) / 1000;
  52. freqs.cpu = 0;
  53. freqs.flags = 0;
  54. if (!ref_freq) {
  55. ref_freq = freqs.old;
  56. loops_per_jiffy_ref = boot_cpu_data.loops_per_jiffy;
  57. }
  58. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  59. if (freqs.old < freqs.new)
  60. boot_cpu_data.loops_per_jiffy = cpufreq_scale(
  61. loops_per_jiffy_ref, ref_freq, freqs.new);
  62. clk_set_rate(cpuclk, freq);
  63. if (freqs.new < freqs.old)
  64. boot_cpu_data.loops_per_jiffy = cpufreq_scale(
  65. loops_per_jiffy_ref, ref_freq, freqs.new);
  66. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  67. pr_debug("cpufreq: set frequency %lu Hz\n", freq);
  68. return 0;
  69. }
  70. static int __init at32_cpufreq_driver_init(struct cpufreq_policy *policy)
  71. {
  72. if (policy->cpu != 0)
  73. return -EINVAL;
  74. cpuclk = clk_get(NULL, "cpu");
  75. if (IS_ERR(cpuclk)) {
  76. pr_debug("cpufreq: could not get CPU clk\n");
  77. return PTR_ERR(cpuclk);
  78. }
  79. policy->cpuinfo.min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;
  80. policy->cpuinfo.max_freq = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
  81. policy->cpuinfo.transition_latency = 0;
  82. policy->cur = at32_get_speed(0);
  83. policy->min = policy->cpuinfo.min_freq;
  84. policy->max = policy->cpuinfo.max_freq;
  85. printk("cpufreq: AT32AP CPU frequency driver\n");
  86. return 0;
  87. }
  88. static struct cpufreq_driver at32_driver = {
  89. .name = "at32ap",
  90. .owner = THIS_MODULE,
  91. .init = at32_cpufreq_driver_init,
  92. .verify = at32_verify_speed,
  93. .target = at32_set_target,
  94. .get = at32_get_speed,
  95. .flags = CPUFREQ_STICKY,
  96. };
  97. static int __init at32_cpufreq_init(void)
  98. {
  99. return cpufreq_register_driver(&at32_driver);
  100. }
  101. late_initcall(at32_cpufreq_init);