at32ap-cpufreq.c 3.2 KB

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