davinci-cpufreq.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 unsigned int davinci_getspeed(unsigned int cpu)
  55. {
  56. if (cpu)
  57. return 0;
  58. return clk_get_rate(cpufreq.armclk) / 1000;
  59. }
  60. static int davinci_target(struct cpufreq_policy *policy, unsigned int idx)
  61. {
  62. struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
  63. struct clk *armclk = cpufreq.armclk;
  64. unsigned int old_freq, new_freq;
  65. int ret = 0;
  66. old_freq = davinci_getspeed(0);
  67. new_freq = pdata->freq_table[idx].frequency;
  68. /* if moving to higher frequency, up the voltage beforehand */
  69. if (pdata->set_voltage && new_freq > old_freq) {
  70. ret = pdata->set_voltage(idx);
  71. if (ret)
  72. return ret;
  73. }
  74. ret = clk_set_rate(armclk, idx);
  75. if (ret)
  76. return ret;
  77. if (cpufreq.asyncclk) {
  78. ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
  79. if (ret)
  80. return ret;
  81. }
  82. /* if moving to lower freq, lower the voltage after lowering freq */
  83. if (pdata->set_voltage && new_freq < old_freq)
  84. pdata->set_voltage(idx);
  85. return 0;
  86. }
  87. static int davinci_cpu_init(struct cpufreq_policy *policy)
  88. {
  89. int result = 0;
  90. struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
  91. struct cpufreq_frequency_table *freq_table = pdata->freq_table;
  92. if (policy->cpu != 0)
  93. return -EINVAL;
  94. /* Finish platform specific initialization */
  95. if (pdata->init) {
  96. result = pdata->init();
  97. if (result)
  98. return result;
  99. }
  100. /*
  101. * Time measurement across the target() function yields ~1500-1800us
  102. * time taken with no drivers on notification list.
  103. * Setting the latency to 2000 us to accommodate addition of drivers
  104. * to pre/post change notification list.
  105. */
  106. return cpufreq_generic_init(policy, freq_table, 2000 * 1000);
  107. }
  108. static struct cpufreq_driver davinci_driver = {
  109. .flags = CPUFREQ_STICKY,
  110. .verify = davinci_verify_speed,
  111. .target_index = davinci_target,
  112. .get = davinci_getspeed,
  113. .init = davinci_cpu_init,
  114. .exit = cpufreq_generic_exit,
  115. .name = "davinci",
  116. .attr = cpufreq_generic_attr,
  117. };
  118. static int __init davinci_cpufreq_probe(struct platform_device *pdev)
  119. {
  120. struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
  121. struct clk *asyncclk;
  122. if (!pdata)
  123. return -EINVAL;
  124. if (!pdata->freq_table)
  125. return -EINVAL;
  126. cpufreq.dev = &pdev->dev;
  127. cpufreq.armclk = clk_get(NULL, "arm");
  128. if (IS_ERR(cpufreq.armclk)) {
  129. dev_err(cpufreq.dev, "Unable to get ARM clock\n");
  130. return PTR_ERR(cpufreq.armclk);
  131. }
  132. asyncclk = clk_get(cpufreq.dev, "async");
  133. if (!IS_ERR(asyncclk)) {
  134. cpufreq.asyncclk = asyncclk;
  135. cpufreq.asyncrate = clk_get_rate(asyncclk);
  136. }
  137. return cpufreq_register_driver(&davinci_driver);
  138. }
  139. static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
  140. {
  141. clk_put(cpufreq.armclk);
  142. if (cpufreq.asyncclk)
  143. clk_put(cpufreq.asyncclk);
  144. return cpufreq_unregister_driver(&davinci_driver);
  145. }
  146. static struct platform_driver davinci_cpufreq_driver = {
  147. .driver = {
  148. .name = "cpufreq-davinci",
  149. .owner = THIS_MODULE,
  150. },
  151. .remove = __exit_p(davinci_cpufreq_remove),
  152. };
  153. int __init davinci_cpufreq_init(void)
  154. {
  155. return platform_driver_probe(&davinci_cpufreq_driver,
  156. davinci_cpufreq_probe);
  157. }