at32ap-cpufreq.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <linux/slab.h>
  21. static struct clk *cpuclk;
  22. static struct cpufreq_frequency_table *freq_table;
  23. static unsigned int at32_get_speed(unsigned int cpu)
  24. {
  25. /* No SMP support */
  26. if (cpu)
  27. return 0;
  28. return (unsigned int)((clk_get_rate(cpuclk) + 500) / 1000);
  29. }
  30. static unsigned int ref_freq;
  31. static unsigned long loops_per_jiffy_ref;
  32. static int at32_set_target(struct cpufreq_policy *policy,
  33. unsigned int target_freq,
  34. unsigned int relation)
  35. {
  36. struct cpufreq_freqs freqs;
  37. long freq;
  38. /* Convert target_freq from kHz to Hz */
  39. freq = clk_round_rate(cpuclk, target_freq * 1000);
  40. /* Check if policy->min <= new_freq <= policy->max */
  41. if(freq < (policy->min * 1000) || freq > (policy->max * 1000))
  42. return -EINVAL;
  43. pr_debug("cpufreq: requested frequency %u Hz\n", target_freq * 1000);
  44. freqs.old = at32_get_speed(0);
  45. freqs.new = (freq + 500) / 1000;
  46. freqs.flags = 0;
  47. if (!ref_freq) {
  48. ref_freq = freqs.old;
  49. loops_per_jiffy_ref = boot_cpu_data.loops_per_jiffy;
  50. }
  51. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  52. if (freqs.old < freqs.new)
  53. boot_cpu_data.loops_per_jiffy = cpufreq_scale(
  54. loops_per_jiffy_ref, ref_freq, freqs.new);
  55. clk_set_rate(cpuclk, freq);
  56. if (freqs.new < freqs.old)
  57. boot_cpu_data.loops_per_jiffy = cpufreq_scale(
  58. loops_per_jiffy_ref, ref_freq, freqs.new);
  59. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  60. pr_debug("cpufreq: set frequency %lu Hz\n", freq);
  61. return 0;
  62. }
  63. static int __init at32_cpufreq_driver_init(struct cpufreq_policy *policy)
  64. {
  65. unsigned int frequency, rate, min_freq;
  66. int retval, steps, i;
  67. if (policy->cpu != 0)
  68. return -EINVAL;
  69. cpuclk = clk_get(NULL, "cpu");
  70. if (IS_ERR(cpuclk)) {
  71. pr_debug("cpufreq: could not get CPU clk\n");
  72. retval = PTR_ERR(cpuclk);
  73. goto out_err;
  74. }
  75. min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;
  76. frequency = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
  77. policy->cpuinfo.transition_latency = 0;
  78. /*
  79. * AVR32 CPU frequency rate scales in power of two between maximum and
  80. * minimum, also add space for the table end marker.
  81. *
  82. * Further validate that the frequency is usable, and append it to the
  83. * frequency table.
  84. */
  85. steps = fls(frequency / min_freq) + 1;
  86. freq_table = kzalloc(steps * sizeof(struct cpufreq_frequency_table),
  87. GFP_KERNEL);
  88. if (!freq_table) {
  89. retval = -ENOMEM;
  90. goto out_err_put_clk;
  91. }
  92. for (i = 0; i < (steps - 1); i++) {
  93. rate = clk_round_rate(cpuclk, frequency * 1000) / 1000;
  94. if (rate != frequency)
  95. freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  96. else
  97. freq_table[i].frequency = frequency;
  98. frequency /= 2;
  99. }
  100. freq_table[steps - 1].frequency = CPUFREQ_TABLE_END;
  101. retval = cpufreq_table_validate_and_show(policy, freq_table);
  102. if (!retval) {
  103. printk("cpufreq: AT32AP CPU frequency driver\n");
  104. return 0;
  105. }
  106. kfree(freq_table);
  107. out_err_put_clk:
  108. clk_put(cpuclk);
  109. out_err:
  110. return retval;
  111. }
  112. static struct cpufreq_driver at32_driver = {
  113. .name = "at32ap",
  114. .init = at32_cpufreq_driver_init,
  115. .verify = cpufreq_generic_frequency_table_verify,
  116. .target = at32_set_target,
  117. .get = at32_get_speed,
  118. .flags = CPUFREQ_STICKY,
  119. };
  120. static int __init at32_cpufreq_init(void)
  121. {
  122. return cpufreq_register_driver(&at32_driver);
  123. }
  124. late_initcall(at32_cpufreq_init);