omap-cpufreq.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * CPU frequency scaling for OMAP using OPP information
  3. *
  4. * Copyright (C) 2005 Nokia Corporation
  5. * Written by Tony Lindgren <tony@atomide.com>
  6. *
  7. * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
  8. *
  9. * Copyright (C) 2007-2011 Texas Instruments, Inc.
  10. * - OMAP3/4 support by Rajendra Nayak, Santosh Shilimkar
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/cpufreq.h>
  20. #include <linux/delay.h>
  21. #include <linux/init.h>
  22. #include <linux/err.h>
  23. #include <linux/clk.h>
  24. #include <linux/io.h>
  25. #include <linux/opp.h>
  26. #include <linux/cpu.h>
  27. #include <linux/module.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <asm/smp_plat.h>
  31. #include <asm/cpu.h>
  32. /* OPP tolerance in percentage */
  33. #define OPP_TOLERANCE 4
  34. static struct cpufreq_frequency_table *freq_table;
  35. static atomic_t freq_table_users = ATOMIC_INIT(0);
  36. static struct clk *mpu_clk;
  37. static struct device *mpu_dev;
  38. static struct regulator *mpu_reg;
  39. static int omap_verify_speed(struct cpufreq_policy *policy)
  40. {
  41. if (!freq_table)
  42. return -EINVAL;
  43. return cpufreq_frequency_table_verify(policy, freq_table);
  44. }
  45. static unsigned int omap_getspeed(unsigned int cpu)
  46. {
  47. unsigned long rate;
  48. if (cpu >= NR_CPUS)
  49. return 0;
  50. rate = clk_get_rate(mpu_clk) / 1000;
  51. return rate;
  52. }
  53. static int omap_target(struct cpufreq_policy *policy,
  54. unsigned int target_freq,
  55. unsigned int relation)
  56. {
  57. unsigned int i;
  58. int r, ret = 0;
  59. struct cpufreq_freqs freqs;
  60. struct opp *opp;
  61. unsigned long freq, volt = 0, volt_old = 0, tol = 0;
  62. if (!freq_table) {
  63. dev_err(mpu_dev, "%s: cpu%d: no freq table!\n", __func__,
  64. policy->cpu);
  65. return -EINVAL;
  66. }
  67. ret = cpufreq_frequency_table_target(policy, freq_table, target_freq,
  68. relation, &i);
  69. if (ret) {
  70. dev_dbg(mpu_dev, "%s: cpu%d: no freq match for %d(ret=%d)\n",
  71. __func__, policy->cpu, target_freq, ret);
  72. return ret;
  73. }
  74. freqs.new = freq_table[i].frequency;
  75. if (!freqs.new) {
  76. dev_err(mpu_dev, "%s: cpu%d: no match for freq %d\n", __func__,
  77. policy->cpu, target_freq);
  78. return -EINVAL;
  79. }
  80. freqs.old = omap_getspeed(policy->cpu);
  81. if (freqs.old == freqs.new && policy->cur == freqs.new)
  82. return ret;
  83. freq = freqs.new * 1000;
  84. ret = clk_round_rate(mpu_clk, freq);
  85. if (IS_ERR_VALUE(ret)) {
  86. dev_warn(mpu_dev,
  87. "CPUfreq: Cannot find matching frequency for %lu\n",
  88. freq);
  89. return ret;
  90. }
  91. freq = ret;
  92. if (mpu_reg) {
  93. rcu_read_lock();
  94. opp = opp_find_freq_ceil(mpu_dev, &freq);
  95. if (IS_ERR(opp)) {
  96. rcu_read_unlock();
  97. dev_err(mpu_dev, "%s: unable to find MPU OPP for %d\n",
  98. __func__, freqs.new);
  99. return -EINVAL;
  100. }
  101. volt = opp_get_voltage(opp);
  102. rcu_read_unlock();
  103. tol = volt * OPP_TOLERANCE / 100;
  104. volt_old = regulator_get_voltage(mpu_reg);
  105. }
  106. dev_dbg(mpu_dev, "cpufreq-omap: %u MHz, %ld mV --> %u MHz, %ld mV\n",
  107. freqs.old / 1000, volt_old ? volt_old / 1000 : -1,
  108. freqs.new / 1000, volt ? volt / 1000 : -1);
  109. /* notifiers */
  110. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  111. /* scaling up? scale voltage before frequency */
  112. if (mpu_reg && (freqs.new > freqs.old)) {
  113. r = regulator_set_voltage(mpu_reg, volt - tol, volt + tol);
  114. if (r < 0) {
  115. dev_warn(mpu_dev, "%s: unable to scale voltage up.\n",
  116. __func__);
  117. freqs.new = freqs.old;
  118. goto done;
  119. }
  120. }
  121. ret = clk_set_rate(mpu_clk, freqs.new * 1000);
  122. /* scaling down? scale voltage after frequency */
  123. if (mpu_reg && (freqs.new < freqs.old)) {
  124. r = regulator_set_voltage(mpu_reg, volt - tol, volt + tol);
  125. if (r < 0) {
  126. dev_warn(mpu_dev, "%s: unable to scale voltage down.\n",
  127. __func__);
  128. ret = clk_set_rate(mpu_clk, freqs.old * 1000);
  129. freqs.new = freqs.old;
  130. goto done;
  131. }
  132. }
  133. freqs.new = omap_getspeed(policy->cpu);
  134. done:
  135. /* notifiers */
  136. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  137. return ret;
  138. }
  139. static inline void freq_table_free(void)
  140. {
  141. if (atomic_dec_and_test(&freq_table_users))
  142. opp_free_cpufreq_table(mpu_dev, &freq_table);
  143. }
  144. static int omap_cpu_init(struct cpufreq_policy *policy)
  145. {
  146. int result = 0;
  147. mpu_clk = clk_get(NULL, "cpufreq_ck");
  148. if (IS_ERR(mpu_clk))
  149. return PTR_ERR(mpu_clk);
  150. if (policy->cpu >= NR_CPUS) {
  151. result = -EINVAL;
  152. goto fail_ck;
  153. }
  154. policy->cur = omap_getspeed(policy->cpu);
  155. if (!freq_table)
  156. result = opp_init_cpufreq_table(mpu_dev, &freq_table);
  157. if (result) {
  158. dev_err(mpu_dev, "%s: cpu%d: failed creating freq table[%d]\n",
  159. __func__, policy->cpu, result);
  160. goto fail_ck;
  161. }
  162. atomic_inc_return(&freq_table_users);
  163. result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
  164. if (result)
  165. goto fail_table;
  166. cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
  167. policy->cur = omap_getspeed(policy->cpu);
  168. /*
  169. * On OMAP SMP configuartion, both processors share the voltage
  170. * and clock. So both CPUs needs to be scaled together and hence
  171. * needs software co-ordination. Use cpufreq affected_cpus
  172. * interface to handle this scenario. Additional is_smp() check
  173. * is to keep SMP_ON_UP build working.
  174. */
  175. if (is_smp())
  176. cpumask_setall(policy->cpus);
  177. /* FIXME: what's the actual transition time? */
  178. policy->cpuinfo.transition_latency = 300 * 1000;
  179. return 0;
  180. fail_table:
  181. freq_table_free();
  182. fail_ck:
  183. clk_put(mpu_clk);
  184. return result;
  185. }
  186. static int omap_cpu_exit(struct cpufreq_policy *policy)
  187. {
  188. freq_table_free();
  189. clk_put(mpu_clk);
  190. return 0;
  191. }
  192. static struct freq_attr *omap_cpufreq_attr[] = {
  193. &cpufreq_freq_attr_scaling_available_freqs,
  194. NULL,
  195. };
  196. static struct cpufreq_driver omap_driver = {
  197. .flags = CPUFREQ_STICKY,
  198. .verify = omap_verify_speed,
  199. .target = omap_target,
  200. .get = omap_getspeed,
  201. .init = omap_cpu_init,
  202. .exit = omap_cpu_exit,
  203. .name = "omap",
  204. .attr = omap_cpufreq_attr,
  205. };
  206. static int omap_cpufreq_probe(struct platform_device *pdev)
  207. {
  208. mpu_dev = get_cpu_device(0);
  209. if (!mpu_dev) {
  210. pr_warning("%s: unable to get the mpu device\n", __func__);
  211. return -EINVAL;
  212. }
  213. mpu_reg = regulator_get(mpu_dev, "vcc");
  214. if (IS_ERR(mpu_reg)) {
  215. pr_warning("%s: unable to get MPU regulator\n", __func__);
  216. mpu_reg = NULL;
  217. } else {
  218. /*
  219. * Ensure physical regulator is present.
  220. * (e.g. could be dummy regulator.)
  221. */
  222. if (regulator_get_voltage(mpu_reg) < 0) {
  223. pr_warn("%s: physical regulator not present for MPU\n",
  224. __func__);
  225. regulator_put(mpu_reg);
  226. mpu_reg = NULL;
  227. }
  228. }
  229. return cpufreq_register_driver(&omap_driver);
  230. }
  231. static int omap_cpufreq_remove(struct platform_device *pdev)
  232. {
  233. return cpufreq_unregister_driver(&omap_driver);
  234. }
  235. static struct platform_driver omap_cpufreq_platdrv = {
  236. .driver = {
  237. .name = "omap-cpufreq",
  238. .owner = THIS_MODULE,
  239. },
  240. .probe = omap_cpufreq_probe,
  241. .remove = omap_cpufreq_remove,
  242. };
  243. module_platform_driver(omap_cpufreq_platdrv);
  244. MODULE_DESCRIPTION("cpufreq driver for OMAP SoCs");
  245. MODULE_LICENSE("GPL");