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 <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. cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER,
  83. dev_driver_string(cpufreq.dev),
  84. "transition: %u --> %u\n", freqs.old, freqs.new);
  85. ret = cpufreq_frequency_table_target(policy, pdata->freq_table,
  86. freqs.new, relation, &idx);
  87. if (ret)
  88. return -EINVAL;
  89. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  90. /* if moving to higher frequency, up the voltage beforehand */
  91. if (pdata->set_voltage && freqs.new > freqs.old) {
  92. ret = pdata->set_voltage(idx);
  93. if (ret)
  94. goto out;
  95. }
  96. ret = clk_set_rate(armclk, idx);
  97. if (ret)
  98. goto out;
  99. if (cpufreq.asyncclk) {
  100. ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
  101. if (ret)
  102. goto out;
  103. }
  104. /* if moving to lower freq, lower the voltage after lowering freq */
  105. if (pdata->set_voltage && freqs.new < freqs.old)
  106. pdata->set_voltage(idx);
  107. out:
  108. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  109. return ret;
  110. }
  111. static int davinci_cpu_init(struct cpufreq_policy *policy)
  112. {
  113. int result = 0;
  114. struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
  115. struct cpufreq_frequency_table *freq_table = pdata->freq_table;
  116. if (policy->cpu != 0)
  117. return -EINVAL;
  118. /* Finish platform specific initialization */
  119. if (pdata->init) {
  120. result = pdata->init();
  121. if (result)
  122. return result;
  123. }
  124. policy->cur = policy->min = policy->max = davinci_getspeed(0);
  125. if (freq_table) {
  126. result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
  127. if (!result)
  128. cpufreq_frequency_table_get_attr(freq_table,
  129. policy->cpu);
  130. } else {
  131. policy->cpuinfo.min_freq = policy->min;
  132. policy->cpuinfo.max_freq = policy->max;
  133. }
  134. policy->min = policy->cpuinfo.min_freq;
  135. policy->max = policy->cpuinfo.max_freq;
  136. policy->cur = davinci_getspeed(0);
  137. /*
  138. * Time measurement across the target() function yields ~1500-1800us
  139. * time taken with no drivers on notification list.
  140. * Setting the latency to 2000 us to accomodate addition of drivers
  141. * to pre/post change notification list.
  142. */
  143. policy->cpuinfo.transition_latency = 2000 * 1000;
  144. return 0;
  145. }
  146. static int davinci_cpu_exit(struct cpufreq_policy *policy)
  147. {
  148. cpufreq_frequency_table_put_attr(policy->cpu);
  149. return 0;
  150. }
  151. static struct freq_attr *davinci_cpufreq_attr[] = {
  152. &cpufreq_freq_attr_scaling_available_freqs,
  153. NULL,
  154. };
  155. static struct cpufreq_driver davinci_driver = {
  156. .flags = CPUFREQ_STICKY,
  157. .verify = davinci_verify_speed,
  158. .target = davinci_target,
  159. .get = davinci_getspeed,
  160. .init = davinci_cpu_init,
  161. .exit = davinci_cpu_exit,
  162. .name = "davinci",
  163. .attr = davinci_cpufreq_attr,
  164. };
  165. static int __init davinci_cpufreq_probe(struct platform_device *pdev)
  166. {
  167. struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
  168. struct clk *asyncclk;
  169. if (!pdata)
  170. return -EINVAL;
  171. if (!pdata->freq_table)
  172. return -EINVAL;
  173. cpufreq.dev = &pdev->dev;
  174. cpufreq.armclk = clk_get(NULL, "arm");
  175. if (IS_ERR(cpufreq.armclk)) {
  176. dev_err(cpufreq.dev, "Unable to get ARM clock\n");
  177. return PTR_ERR(cpufreq.armclk);
  178. }
  179. asyncclk = clk_get(cpufreq.dev, "async");
  180. if (!IS_ERR(asyncclk)) {
  181. cpufreq.asyncclk = asyncclk;
  182. cpufreq.asyncrate = clk_get_rate(asyncclk);
  183. }
  184. return cpufreq_register_driver(&davinci_driver);
  185. }
  186. static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
  187. {
  188. clk_put(cpufreq.armclk);
  189. if (cpufreq.asyncclk)
  190. clk_put(cpufreq.asyncclk);
  191. return cpufreq_unregister_driver(&davinci_driver);
  192. }
  193. static struct platform_driver davinci_cpufreq_driver = {
  194. .driver = {
  195. .name = "cpufreq-davinci",
  196. .owner = THIS_MODULE,
  197. },
  198. .remove = __exit_p(davinci_cpufreq_remove),
  199. };
  200. static int __init davinci_cpufreq_init(void)
  201. {
  202. return platform_driver_probe(&davinci_cpufreq_driver,
  203. davinci_cpufreq_probe);
  204. }
  205. late_initcall(davinci_cpufreq_init);