cpufreq.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 int at32_set_target(struct cpufreq_policy *policy,
  37. unsigned int target_freq,
  38. unsigned int relation)
  39. {
  40. struct cpufreq_freqs freqs;
  41. long freq;
  42. /* Convert target_freq from kHz to Hz */
  43. freq = clk_round_rate(cpuclk, target_freq * 1000);
  44. /* Check if policy->min <= new_freq <= policy->max */
  45. if(freq < (policy->min * 1000) || freq > (policy->max * 1000))
  46. return -EINVAL;
  47. pr_debug("cpufreq: requested frequency %u Hz\n", target_freq * 1000);
  48. freqs.old = at32_get_speed(0);
  49. freqs.new = (freq + 500) / 1000;
  50. freqs.cpu = 0;
  51. freqs.flags = 0;
  52. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  53. clk_set_rate(cpuclk, freq);
  54. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  55. pr_debug("cpufreq: set frequency %lu Hz\n", freq);
  56. return 0;
  57. }
  58. static int __init at32_cpufreq_driver_init(struct cpufreq_policy *policy)
  59. {
  60. if (policy->cpu != 0)
  61. return -EINVAL;
  62. cpuclk = clk_get(NULL, "cpu");
  63. if (IS_ERR(cpuclk)) {
  64. pr_debug("cpufreq: could not get CPU clk\n");
  65. return PTR_ERR(cpuclk);
  66. }
  67. policy->cpuinfo.min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;
  68. policy->cpuinfo.max_freq = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
  69. policy->cpuinfo.transition_latency = 0;
  70. policy->cur = at32_get_speed(0);
  71. policy->min = policy->cpuinfo.min_freq;
  72. policy->max = policy->cpuinfo.max_freq;
  73. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  74. printk("cpufreq: AT32AP CPU frequency driver\n");
  75. return 0;
  76. }
  77. static struct cpufreq_driver at32_driver = {
  78. .name = "at32ap",
  79. .owner = THIS_MODULE,
  80. .init = at32_cpufreq_driver_init,
  81. .verify = at32_verify_speed,
  82. .target = at32_set_target,
  83. .get = at32_get_speed,
  84. .flags = CPUFREQ_STICKY,
  85. };
  86. static int __init at32_cpufreq_init(void)
  87. {
  88. return cpufreq_register_driver(&at32_driver);
  89. }
  90. late_initcall(at32_cpufreq_init);