davinci-cpufreq.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * CPU frequency scaling for DaVinci
  3. *
  4. * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. * Based on linux/arch/arm/plat-omap/cpu-omap.c. Original Copyright follows:
  7. *
  8. * Copyright (C) 2005 Nokia Corporation
  9. * Written by Tony Lindgren <tony@atomide.com>
  10. *
  11. * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
  12. *
  13. * Copyright (C) 2007-2008 Texas Instruments, Inc.
  14. * Updated to support OMAP3
  15. * Rajendra Nayak <rnayak@ti.com>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License version 2 as
  19. * published by the Free Software Foundation.
  20. */
  21. #include <linux/types.h>
  22. #include <linux/cpufreq.h>
  23. #include <linux/init.h>
  24. #include <linux/err.h>
  25. #include <linux/clk.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/export.h>
  28. #include <mach/hardware.h>
  29. #include <mach/cpufreq.h>
  30. #include <mach/common.h>
  31. struct davinci_cpufreq {
  32. struct device *dev;
  33. struct clk *armclk;
  34. struct clk *asyncclk;
  35. unsigned long asyncrate;
  36. };
  37. static struct davinci_cpufreq cpufreq;
  38. static int davinci_verify_speed(struct cpufreq_policy *policy)
  39. {
  40. struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
  41. struct cpufreq_frequency_table *freq_table = pdata->freq_table;
  42. struct clk *armclk = cpufreq.armclk;
  43. if (freq_table)
  44. return cpufreq_frequency_table_verify(policy, freq_table);
  45. if (policy->cpu)
  46. return -EINVAL;
  47. cpufreq_verify_within_cpu_limits(policy);
  48. policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000;
  49. policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000;
  50. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  51. policy->cpuinfo.max_freq);
  52. return 0;
  53. }
  54. static int davinci_target(struct cpufreq_policy *policy, unsigned int idx)
  55. {
  56. struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
  57. struct clk *armclk = cpufreq.armclk;
  58. unsigned int old_freq, new_freq;
  59. int ret = 0;
  60. old_freq = policy->cur;
  61. new_freq = pdata->freq_table[idx].frequency;
  62. /* if moving to higher frequency, up the voltage beforehand */
  63. if (pdata->set_voltage && new_freq > old_freq) {
  64. ret = pdata->set_voltage(idx);
  65. if (ret)
  66. return ret;
  67. }
  68. ret = clk_set_rate(armclk, idx);
  69. if (ret)
  70. return ret;
  71. if (cpufreq.asyncclk) {
  72. ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
  73. if (ret)
  74. return ret;
  75. }
  76. /* if moving to lower freq, lower the voltage after lowering freq */
  77. if (pdata->set_voltage && new_freq < old_freq)
  78. pdata->set_voltage(idx);
  79. return 0;
  80. }
  81. static int davinci_cpu_init(struct cpufreq_policy *policy)
  82. {
  83. int result = 0;
  84. struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
  85. struct cpufreq_frequency_table *freq_table = pdata->freq_table;
  86. if (policy->cpu != 0)
  87. return -EINVAL;
  88. /* Finish platform specific initialization */
  89. if (pdata->init) {
  90. result = pdata->init();
  91. if (result)
  92. return result;
  93. }
  94. policy->clk = cpufreq.armclk;
  95. /*
  96. * Time measurement across the target() function yields ~1500-1800us
  97. * time taken with no drivers on notification list.
  98. * Setting the latency to 2000 us to accommodate addition of drivers
  99. * to pre/post change notification list.
  100. */
  101. return cpufreq_generic_init(policy, freq_table, 2000 * 1000);
  102. }
  103. static struct cpufreq_driver davinci_driver = {
  104. .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  105. .verify = davinci_verify_speed,
  106. .target_index = davinci_target,
  107. .get = cpufreq_generic_get,
  108. .init = davinci_cpu_init,
  109. .name = "davinci",
  110. .attr = cpufreq_generic_attr,
  111. };
  112. static int __init davinci_cpufreq_probe(struct platform_device *pdev)
  113. {
  114. struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
  115. struct clk *asyncclk;
  116. if (!pdata)
  117. return -EINVAL;
  118. if (!pdata->freq_table)
  119. return -EINVAL;
  120. cpufreq.dev = &pdev->dev;
  121. cpufreq.armclk = clk_get(NULL, "arm");
  122. if (IS_ERR(cpufreq.armclk)) {
  123. dev_err(cpufreq.dev, "Unable to get ARM clock\n");
  124. return PTR_ERR(cpufreq.armclk);
  125. }
  126. asyncclk = clk_get(cpufreq.dev, "async");
  127. if (!IS_ERR(asyncclk)) {
  128. cpufreq.asyncclk = asyncclk;
  129. cpufreq.asyncrate = clk_get_rate(asyncclk);
  130. }
  131. return cpufreq_register_driver(&davinci_driver);
  132. }
  133. static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
  134. {
  135. clk_put(cpufreq.armclk);
  136. if (cpufreq.asyncclk)
  137. clk_put(cpufreq.asyncclk);
  138. return cpufreq_unregister_driver(&davinci_driver);
  139. }
  140. static struct platform_driver davinci_cpufreq_driver = {
  141. .driver = {
  142. .name = "cpufreq-davinci",
  143. },
  144. .remove = __exit_p(davinci_cpufreq_remove),
  145. };
  146. int __init davinci_cpufreq_init(void)
  147. {
  148. return platform_driver_probe(&davinci_cpufreq_driver,
  149. davinci_cpufreq_probe);
  150. }