davinci-cpufreq.c 5.1 KB

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