cpufreq-cpu0.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (C) 2012 Freescale Semiconductor, Inc.
  3. *
  4. * The OPP code in function cpu0_set_target() is reused from
  5. * drivers/cpufreq/omap-cpufreq.c
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/clk.h>
  13. #include <linux/cpu.h>
  14. #include <linux/cpu_cooling.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/cpumask.h>
  17. #include <linux/err.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/pm_opp.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/slab.h>
  24. #include <linux/thermal.h>
  25. static unsigned int transition_latency;
  26. static unsigned int voltage_tolerance; /* in percentage */
  27. static struct device *cpu_dev;
  28. static struct clk *cpu_clk;
  29. static struct regulator *cpu_reg;
  30. static struct cpufreq_frequency_table *freq_table;
  31. static struct thermal_cooling_device *cdev;
  32. static int cpu0_set_target(struct cpufreq_policy *policy, unsigned int index)
  33. {
  34. struct dev_pm_opp *opp;
  35. unsigned long volt = 0, volt_old = 0, tol = 0;
  36. unsigned int old_freq, new_freq;
  37. long freq_Hz, freq_exact;
  38. int ret;
  39. freq_Hz = clk_round_rate(cpu_clk, freq_table[index].frequency * 1000);
  40. if (freq_Hz <= 0)
  41. freq_Hz = freq_table[index].frequency * 1000;
  42. freq_exact = freq_Hz;
  43. new_freq = freq_Hz / 1000;
  44. old_freq = clk_get_rate(cpu_clk) / 1000;
  45. if (!IS_ERR(cpu_reg)) {
  46. rcu_read_lock();
  47. opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_Hz);
  48. if (IS_ERR(opp)) {
  49. rcu_read_unlock();
  50. pr_err("failed to find OPP for %ld\n", freq_Hz);
  51. return PTR_ERR(opp);
  52. }
  53. volt = dev_pm_opp_get_voltage(opp);
  54. rcu_read_unlock();
  55. tol = volt * voltage_tolerance / 100;
  56. volt_old = regulator_get_voltage(cpu_reg);
  57. }
  58. pr_debug("%u MHz, %ld mV --> %u MHz, %ld mV\n",
  59. old_freq / 1000, volt_old ? volt_old / 1000 : -1,
  60. new_freq / 1000, volt ? volt / 1000 : -1);
  61. /* scaling up? scale voltage before frequency */
  62. if (!IS_ERR(cpu_reg) && new_freq > old_freq) {
  63. ret = regulator_set_voltage_tol(cpu_reg, volt, tol);
  64. if (ret) {
  65. pr_err("failed to scale voltage up: %d\n", ret);
  66. return ret;
  67. }
  68. }
  69. ret = clk_set_rate(cpu_clk, freq_exact);
  70. if (ret) {
  71. pr_err("failed to set clock rate: %d\n", ret);
  72. if (!IS_ERR(cpu_reg))
  73. regulator_set_voltage_tol(cpu_reg, volt_old, tol);
  74. return ret;
  75. }
  76. /* scaling down? scale voltage after frequency */
  77. if (!IS_ERR(cpu_reg) && new_freq < old_freq) {
  78. ret = regulator_set_voltage_tol(cpu_reg, volt, tol);
  79. if (ret) {
  80. pr_err("failed to scale voltage down: %d\n", ret);
  81. clk_set_rate(cpu_clk, old_freq * 1000);
  82. }
  83. }
  84. return ret;
  85. }
  86. static int cpu0_cpufreq_init(struct cpufreq_policy *policy)
  87. {
  88. policy->clk = cpu_clk;
  89. return cpufreq_generic_init(policy, freq_table, transition_latency);
  90. }
  91. static struct cpufreq_driver cpu0_cpufreq_driver = {
  92. .flags = CPUFREQ_STICKY,
  93. .verify = cpufreq_generic_frequency_table_verify,
  94. .target_index = cpu0_set_target,
  95. .get = cpufreq_generic_get,
  96. .init = cpu0_cpufreq_init,
  97. .name = "generic_cpu0",
  98. .attr = cpufreq_generic_attr,
  99. };
  100. static int cpu0_cpufreq_probe(struct platform_device *pdev)
  101. {
  102. struct device_node *np;
  103. int ret;
  104. cpu_dev = get_cpu_device(0);
  105. if (!cpu_dev) {
  106. pr_err("failed to get cpu0 device\n");
  107. return -ENODEV;
  108. }
  109. np = of_node_get(cpu_dev->of_node);
  110. if (!np) {
  111. pr_err("failed to find cpu0 node\n");
  112. return -ENOENT;
  113. }
  114. cpu_reg = regulator_get_optional(cpu_dev, "cpu0");
  115. if (IS_ERR(cpu_reg)) {
  116. /*
  117. * If cpu0 regulator supply node is present, but regulator is
  118. * not yet registered, we should try defering probe.
  119. */
  120. if (PTR_ERR(cpu_reg) == -EPROBE_DEFER) {
  121. dev_err(cpu_dev, "cpu0 regulator not ready, retry\n");
  122. ret = -EPROBE_DEFER;
  123. goto out_put_node;
  124. }
  125. pr_warn("failed to get cpu0 regulator: %ld\n",
  126. PTR_ERR(cpu_reg));
  127. }
  128. cpu_clk = clk_get(cpu_dev, NULL);
  129. if (IS_ERR(cpu_clk)) {
  130. ret = PTR_ERR(cpu_clk);
  131. pr_err("failed to get cpu0 clock: %d\n", ret);
  132. goto out_put_reg;
  133. }
  134. ret = of_init_opp_table(cpu_dev);
  135. if (ret) {
  136. pr_err("failed to init OPP table: %d\n", ret);
  137. goto out_put_clk;
  138. }
  139. ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
  140. if (ret) {
  141. pr_err("failed to init cpufreq table: %d\n", ret);
  142. goto out_put_clk;
  143. }
  144. of_property_read_u32(np, "voltage-tolerance", &voltage_tolerance);
  145. if (of_property_read_u32(np, "clock-latency", &transition_latency))
  146. transition_latency = CPUFREQ_ETERNAL;
  147. if (!IS_ERR(cpu_reg)) {
  148. struct dev_pm_opp *opp;
  149. unsigned long min_uV, max_uV;
  150. int i;
  151. /*
  152. * OPP is maintained in order of increasing frequency, and
  153. * freq_table initialised from OPP is therefore sorted in the
  154. * same order.
  155. */
  156. for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++)
  157. ;
  158. rcu_read_lock();
  159. opp = dev_pm_opp_find_freq_exact(cpu_dev,
  160. freq_table[0].frequency * 1000, true);
  161. min_uV = dev_pm_opp_get_voltage(opp);
  162. opp = dev_pm_opp_find_freq_exact(cpu_dev,
  163. freq_table[i-1].frequency * 1000, true);
  164. max_uV = dev_pm_opp_get_voltage(opp);
  165. rcu_read_unlock();
  166. ret = regulator_set_voltage_time(cpu_reg, min_uV, max_uV);
  167. if (ret > 0)
  168. transition_latency += ret * 1000;
  169. }
  170. ret = cpufreq_register_driver(&cpu0_cpufreq_driver);
  171. if (ret) {
  172. pr_err("failed register driver: %d\n", ret);
  173. goto out_free_table;
  174. }
  175. /*
  176. * For now, just loading the cooling device;
  177. * thermal DT code takes care of matching them.
  178. */
  179. if (of_find_property(np, "#cooling-cells", NULL)) {
  180. cdev = of_cpufreq_cooling_register(np, cpu_present_mask);
  181. if (IS_ERR(cdev))
  182. pr_err("running cpufreq without cooling device: %ld\n",
  183. PTR_ERR(cdev));
  184. }
  185. of_node_put(np);
  186. return 0;
  187. out_free_table:
  188. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  189. out_put_clk:
  190. if (!IS_ERR(cpu_clk))
  191. clk_put(cpu_clk);
  192. out_put_reg:
  193. if (!IS_ERR(cpu_reg))
  194. regulator_put(cpu_reg);
  195. out_put_node:
  196. of_node_put(np);
  197. return ret;
  198. }
  199. static int cpu0_cpufreq_remove(struct platform_device *pdev)
  200. {
  201. cpufreq_cooling_unregister(cdev);
  202. cpufreq_unregister_driver(&cpu0_cpufreq_driver);
  203. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  204. return 0;
  205. }
  206. static struct platform_driver cpu0_cpufreq_platdrv = {
  207. .driver = {
  208. .name = "cpufreq-cpu0",
  209. .owner = THIS_MODULE,
  210. },
  211. .probe = cpu0_cpufreq_probe,
  212. .remove = cpu0_cpufreq_remove,
  213. };
  214. module_platform_driver(cpu0_cpufreq_platdrv);
  215. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  216. MODULE_DESCRIPTION("Generic CPU0 cpufreq driver");
  217. MODULE_LICENSE("GPL");