cpufreq.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 <mach/hardware.h>
  28. #include <mach/cpufreq.h>
  29. #include <mach/common.h>
  30. #include "clock.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_limits(policy, policy->cpuinfo.min_freq,
  48. policy->cpuinfo.max_freq);
  49. policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000;
  50. policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000;
  51. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  52. policy->cpuinfo.max_freq);
  53. return 0;
  54. }
  55. static unsigned int davinci_getspeed(unsigned int cpu)
  56. {
  57. if (cpu)
  58. return 0;
  59. return clk_get_rate(cpufreq.armclk) / 1000;
  60. }
  61. static int davinci_target(struct cpufreq_policy *policy,
  62. unsigned int target_freq, unsigned int relation)
  63. {
  64. int ret = 0;
  65. unsigned int idx;
  66. struct cpufreq_freqs freqs;
  67. struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
  68. struct clk *armclk = cpufreq.armclk;
  69. /*
  70. * Ensure desired rate is within allowed range. Some govenors
  71. * (ondemand) will just pass target_freq=0 to get the minimum.
  72. */
  73. if (target_freq < policy->cpuinfo.min_freq)
  74. target_freq = policy->cpuinfo.min_freq;
  75. if (target_freq > policy->cpuinfo.max_freq)
  76. target_freq = policy->cpuinfo.max_freq;
  77. freqs.old = davinci_getspeed(0);
  78. freqs.new = clk_round_rate(armclk, target_freq * 1000) / 1000;
  79. freqs.cpu = 0;
  80. if (freqs.old == freqs.new)
  81. return ret;
  82. dev_dbg(&cpufreq.dev, "transition: %u --> %u\n", freqs.old, freqs.new);
  83. ret = cpufreq_frequency_table_target(policy, pdata->freq_table,
  84. freqs.new, relation, &idx);
  85. if (ret)
  86. return -EINVAL;
  87. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  88. /* if moving to higher frequency, up the voltage beforehand */
  89. if (pdata->set_voltage && freqs.new > freqs.old) {
  90. ret = pdata->set_voltage(idx);
  91. if (ret)
  92. goto out;
  93. }
  94. ret = clk_set_rate(armclk, idx);
  95. if (ret)
  96. goto out;
  97. if (cpufreq.asyncclk) {
  98. ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
  99. if (ret)
  100. goto out;
  101. }
  102. /* if moving to lower freq, lower the voltage after lowering freq */
  103. if (pdata->set_voltage && freqs.new < freqs.old)
  104. pdata->set_voltage(idx);
  105. out:
  106. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  107. return ret;
  108. }
  109. static int davinci_cpu_init(struct cpufreq_policy *policy)
  110. {
  111. int result = 0;
  112. struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
  113. struct cpufreq_frequency_table *freq_table = pdata->freq_table;
  114. if (policy->cpu != 0)
  115. return -EINVAL;
  116. /* Finish platform specific initialization */
  117. if (pdata->init) {
  118. result = pdata->init();
  119. if (result)
  120. return result;
  121. }
  122. policy->cur = policy->min = policy->max = davinci_getspeed(0);
  123. if (freq_table) {
  124. result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
  125. if (!result)
  126. cpufreq_frequency_table_get_attr(freq_table,
  127. policy->cpu);
  128. } else {
  129. policy->cpuinfo.min_freq = policy->min;
  130. policy->cpuinfo.max_freq = policy->max;
  131. }
  132. policy->min = policy->cpuinfo.min_freq;
  133. policy->max = policy->cpuinfo.max_freq;
  134. policy->cur = davinci_getspeed(0);
  135. /*
  136. * Time measurement across the target() function yields ~1500-1800us
  137. * time taken with no drivers on notification list.
  138. * Setting the latency to 2000 us to accommodate addition of drivers
  139. * to pre/post change notification list.
  140. */
  141. policy->cpuinfo.transition_latency = 2000 * 1000;
  142. return 0;
  143. }
  144. static int davinci_cpu_exit(struct cpufreq_policy *policy)
  145. {
  146. cpufreq_frequency_table_put_attr(policy->cpu);
  147. return 0;
  148. }
  149. static struct freq_attr *davinci_cpufreq_attr[] = {
  150. &cpufreq_freq_attr_scaling_available_freqs,
  151. NULL,
  152. };
  153. static struct cpufreq_driver davinci_driver = {
  154. .flags = CPUFREQ_STICKY,
  155. .verify = davinci_verify_speed,
  156. .target = davinci_target,
  157. .get = davinci_getspeed,
  158. .init = davinci_cpu_init,
  159. .exit = davinci_cpu_exit,
  160. .name = "davinci",
  161. .attr = davinci_cpufreq_attr,
  162. };
  163. static int __init davinci_cpufreq_probe(struct platform_device *pdev)
  164. {
  165. struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
  166. struct clk *asyncclk;
  167. if (!pdata)
  168. return -EINVAL;
  169. if (!pdata->freq_table)
  170. return -EINVAL;
  171. cpufreq.dev = &pdev->dev;
  172. cpufreq.armclk = clk_get(NULL, "arm");
  173. if (IS_ERR(cpufreq.armclk)) {
  174. dev_err(cpufreq.dev, "Unable to get ARM clock\n");
  175. return PTR_ERR(cpufreq.armclk);
  176. }
  177. asyncclk = clk_get(cpufreq.dev, "async");
  178. if (!IS_ERR(asyncclk)) {
  179. cpufreq.asyncclk = asyncclk;
  180. cpufreq.asyncrate = clk_get_rate(asyncclk);
  181. }
  182. return cpufreq_register_driver(&davinci_driver);
  183. }
  184. static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
  185. {
  186. clk_put(cpufreq.armclk);
  187. if (cpufreq.asyncclk)
  188. clk_put(cpufreq.asyncclk);
  189. return cpufreq_unregister_driver(&davinci_driver);
  190. }
  191. static struct platform_driver davinci_cpufreq_driver = {
  192. .driver = {
  193. .name = "cpufreq-davinci",
  194. .owner = THIS_MODULE,
  195. },
  196. .remove = __exit_p(davinci_cpufreq_remove),
  197. };
  198. static int __init davinci_cpufreq_init(void)
  199. {
  200. return platform_driver_probe(&davinci_cpufreq_driver,
  201. davinci_cpufreq_probe);
  202. }
  203. late_initcall(davinci_cpufreq_init);