at32ap-cpufreq.c 3.1 KB

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