davinci-cpufreq.c 4.0 KB

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