at32ap-cpufreq.c 3.4 KB

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