cpufreq-cpu0.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  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. /* OPPs might be populated at runtime, don't check for error here */
  135. of_init_opp_table(cpu_dev);
  136. ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
  137. if (ret) {
  138. pr_err("failed to init cpufreq table: %d\n", ret);
  139. goto out_put_clk;
  140. }
  141. of_property_read_u32(np, "voltage-tolerance", &voltage_tolerance);
  142. if (of_property_read_u32(np, "clock-latency", &transition_latency))
  143. transition_latency = CPUFREQ_ETERNAL;
  144. if (!IS_ERR(cpu_reg)) {
  145. struct dev_pm_opp *opp;
  146. unsigned long min_uV, max_uV;
  147. int i;
  148. /*
  149. * OPP is maintained in order of increasing frequency, and
  150. * freq_table initialised from OPP is therefore sorted in the
  151. * same order.
  152. */
  153. for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++)
  154. ;
  155. rcu_read_lock();
  156. opp = dev_pm_opp_find_freq_exact(cpu_dev,
  157. freq_table[0].frequency * 1000, true);
  158. min_uV = dev_pm_opp_get_voltage(opp);
  159. opp = dev_pm_opp_find_freq_exact(cpu_dev,
  160. freq_table[i-1].frequency * 1000, true);
  161. max_uV = dev_pm_opp_get_voltage(opp);
  162. rcu_read_unlock();
  163. ret = regulator_set_voltage_time(cpu_reg, min_uV, max_uV);
  164. if (ret > 0)
  165. transition_latency += ret * 1000;
  166. }
  167. ret = cpufreq_register_driver(&cpu0_cpufreq_driver);
  168. if (ret) {
  169. pr_err("failed register driver: %d\n", ret);
  170. goto out_free_table;
  171. }
  172. /*
  173. * For now, just loading the cooling device;
  174. * thermal DT code takes care of matching them.
  175. */
  176. if (of_find_property(np, "#cooling-cells", NULL)) {
  177. cdev = of_cpufreq_cooling_register(np, cpu_present_mask);
  178. if (IS_ERR(cdev))
  179. pr_err("running cpufreq without cooling device: %ld\n",
  180. PTR_ERR(cdev));
  181. }
  182. of_node_put(np);
  183. return 0;
  184. out_free_table:
  185. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  186. out_put_clk:
  187. if (!IS_ERR(cpu_clk))
  188. clk_put(cpu_clk);
  189. out_put_reg:
  190. if (!IS_ERR(cpu_reg))
  191. regulator_put(cpu_reg);
  192. out_put_node:
  193. of_node_put(np);
  194. return ret;
  195. }
  196. static int cpu0_cpufreq_remove(struct platform_device *pdev)
  197. {
  198. cpufreq_cooling_unregister(cdev);
  199. cpufreq_unregister_driver(&cpu0_cpufreq_driver);
  200. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  201. return 0;
  202. }
  203. static struct platform_driver cpu0_cpufreq_platdrv = {
  204. .driver = {
  205. .name = "cpufreq-cpu0",
  206. .owner = THIS_MODULE,
  207. },
  208. .probe = cpu0_cpufreq_probe,
  209. .remove = cpu0_cpufreq_remove,
  210. };
  211. module_platform_driver(cpu0_cpufreq_platdrv);
  212. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  213. MODULE_DESCRIPTION("Generic CPU0 cpufreq driver");
  214. MODULE_LICENSE("GPL");