cpufreq.c 6.2 KB

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