cpufreq-cpu0.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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/cpufreq.h>
  15. #include <linux/err.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/opp.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/slab.h>
  22. static unsigned int transition_latency;
  23. static unsigned int voltage_tolerance; /* in percentage */
  24. static struct device *cpu_dev;
  25. static struct clk *cpu_clk;
  26. static struct regulator *cpu_reg;
  27. static struct cpufreq_frequency_table *freq_table;
  28. static int cpu0_verify_speed(struct cpufreq_policy *policy)
  29. {
  30. return cpufreq_frequency_table_verify(policy, freq_table);
  31. }
  32. static unsigned int cpu0_get_speed(unsigned int cpu)
  33. {
  34. return clk_get_rate(cpu_clk) / 1000;
  35. }
  36. static int cpu0_set_target(struct cpufreq_policy *policy,
  37. unsigned int target_freq, unsigned int relation)
  38. {
  39. struct cpufreq_freqs freqs;
  40. struct opp *opp;
  41. unsigned long volt = 0, volt_old = 0, tol = 0;
  42. long freq_Hz, freq_exact;
  43. unsigned int index;
  44. int ret;
  45. ret = cpufreq_frequency_table_target(policy, freq_table, target_freq,
  46. relation, &index);
  47. if (ret) {
  48. pr_err("failed to match target freqency %d: %d\n",
  49. target_freq, ret);
  50. return ret;
  51. }
  52. freq_Hz = clk_round_rate(cpu_clk, freq_table[index].frequency * 1000);
  53. if (freq_Hz < 0)
  54. freq_Hz = freq_table[index].frequency * 1000;
  55. freq_exact = freq_Hz;
  56. freqs.new = freq_Hz / 1000;
  57. freqs.old = clk_get_rate(cpu_clk) / 1000;
  58. if (freqs.old == freqs.new)
  59. return 0;
  60. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  61. if (!IS_ERR(cpu_reg)) {
  62. rcu_read_lock();
  63. opp = opp_find_freq_ceil(cpu_dev, &freq_Hz);
  64. if (IS_ERR(opp)) {
  65. rcu_read_unlock();
  66. pr_err("failed to find OPP for %ld\n", freq_Hz);
  67. freqs.new = freqs.old;
  68. ret = PTR_ERR(opp);
  69. goto post_notify;
  70. }
  71. volt = opp_get_voltage(opp);
  72. rcu_read_unlock();
  73. tol = volt * voltage_tolerance / 100;
  74. volt_old = regulator_get_voltage(cpu_reg);
  75. }
  76. pr_debug("%u MHz, %ld mV --> %u MHz, %ld mV\n",
  77. freqs.old / 1000, volt_old ? volt_old / 1000 : -1,
  78. freqs.new / 1000, volt ? volt / 1000 : -1);
  79. /* scaling up? scale voltage before frequency */
  80. if (!IS_ERR(cpu_reg) && freqs.new > freqs.old) {
  81. ret = regulator_set_voltage_tol(cpu_reg, volt, tol);
  82. if (ret) {
  83. pr_err("failed to scale voltage up: %d\n", ret);
  84. freqs.new = freqs.old;
  85. goto post_notify;
  86. }
  87. }
  88. ret = clk_set_rate(cpu_clk, freq_exact);
  89. if (ret) {
  90. pr_err("failed to set clock rate: %d\n", ret);
  91. if (!IS_ERR(cpu_reg))
  92. regulator_set_voltage_tol(cpu_reg, volt_old, tol);
  93. freqs.new = freqs.old;
  94. goto post_notify;
  95. }
  96. /* scaling down? scale voltage after frequency */
  97. if (!IS_ERR(cpu_reg) && freqs.new < freqs.old) {
  98. ret = regulator_set_voltage_tol(cpu_reg, volt, tol);
  99. if (ret) {
  100. pr_err("failed to scale voltage down: %d\n", ret);
  101. clk_set_rate(cpu_clk, freqs.old * 1000);
  102. freqs.new = freqs.old;
  103. }
  104. }
  105. post_notify:
  106. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  107. return ret;
  108. }
  109. static int cpu0_cpufreq_init(struct cpufreq_policy *policy)
  110. {
  111. int ret;
  112. ret = cpufreq_table_validate_and_show(policy, freq_table);
  113. if (ret) {
  114. pr_err("invalid frequency table: %d\n", ret);
  115. return ret;
  116. }
  117. policy->cpuinfo.transition_latency = transition_latency;
  118. policy->cur = clk_get_rate(cpu_clk) / 1000;
  119. /*
  120. * The driver only supports the SMP configuartion where all processors
  121. * share the clock and voltage and clock. Use cpufreq affected_cpus
  122. * interface to have all CPUs scaled together.
  123. */
  124. cpumask_setall(policy->cpus);
  125. return 0;
  126. }
  127. static int cpu0_cpufreq_exit(struct cpufreq_policy *policy)
  128. {
  129. cpufreq_frequency_table_put_attr(policy->cpu);
  130. return 0;
  131. }
  132. static struct freq_attr *cpu0_cpufreq_attr[] = {
  133. &cpufreq_freq_attr_scaling_available_freqs,
  134. NULL,
  135. };
  136. static struct cpufreq_driver cpu0_cpufreq_driver = {
  137. .flags = CPUFREQ_STICKY,
  138. .verify = cpu0_verify_speed,
  139. .target = cpu0_set_target,
  140. .get = cpu0_get_speed,
  141. .init = cpu0_cpufreq_init,
  142. .exit = cpu0_cpufreq_exit,
  143. .name = "generic_cpu0",
  144. .attr = cpu0_cpufreq_attr,
  145. };
  146. static int cpu0_cpufreq_probe(struct platform_device *pdev)
  147. {
  148. struct device_node *np;
  149. int ret;
  150. cpu_dev = get_cpu_device(0);
  151. if (!cpu_dev) {
  152. pr_err("failed to get cpu0 device\n");
  153. return -ENODEV;
  154. }
  155. np = of_node_get(cpu_dev->of_node);
  156. if (!np) {
  157. pr_err("failed to find cpu0 node\n");
  158. return -ENOENT;
  159. }
  160. cpu_reg = devm_regulator_get_optional(cpu_dev, "cpu0");
  161. if (IS_ERR(cpu_reg)) {
  162. /*
  163. * If cpu0 regulator supply node is present, but regulator is
  164. * not yet registered, we should try defering probe.
  165. */
  166. if (PTR_ERR(cpu_reg) == -EPROBE_DEFER) {
  167. dev_err(cpu_dev, "cpu0 regulator not ready, retry\n");
  168. ret = -EPROBE_DEFER;
  169. goto out_put_node;
  170. }
  171. pr_warn("failed to get cpu0 regulator: %ld\n",
  172. PTR_ERR(cpu_reg));
  173. }
  174. cpu_clk = devm_clk_get(cpu_dev, NULL);
  175. if (IS_ERR(cpu_clk)) {
  176. ret = PTR_ERR(cpu_clk);
  177. pr_err("failed to get cpu0 clock: %d\n", ret);
  178. goto out_put_node;
  179. }
  180. ret = of_init_opp_table(cpu_dev);
  181. if (ret) {
  182. pr_err("failed to init OPP table: %d\n", ret);
  183. goto out_put_node;
  184. }
  185. ret = opp_init_cpufreq_table(cpu_dev, &freq_table);
  186. if (ret) {
  187. pr_err("failed to init cpufreq table: %d\n", ret);
  188. goto out_put_node;
  189. }
  190. of_property_read_u32(np, "voltage-tolerance", &voltage_tolerance);
  191. if (of_property_read_u32(np, "clock-latency", &transition_latency))
  192. transition_latency = CPUFREQ_ETERNAL;
  193. if (cpu_reg) {
  194. struct opp *opp;
  195. unsigned long min_uV, max_uV;
  196. int i;
  197. /*
  198. * OPP is maintained in order of increasing frequency, and
  199. * freq_table initialised from OPP is therefore sorted in the
  200. * same order.
  201. */
  202. for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++)
  203. ;
  204. rcu_read_lock();
  205. opp = opp_find_freq_exact(cpu_dev,
  206. freq_table[0].frequency * 1000, true);
  207. min_uV = opp_get_voltage(opp);
  208. opp = opp_find_freq_exact(cpu_dev,
  209. freq_table[i-1].frequency * 1000, true);
  210. max_uV = opp_get_voltage(opp);
  211. rcu_read_unlock();
  212. ret = regulator_set_voltage_time(cpu_reg, min_uV, max_uV);
  213. if (ret > 0)
  214. transition_latency += ret * 1000;
  215. }
  216. ret = cpufreq_register_driver(&cpu0_cpufreq_driver);
  217. if (ret) {
  218. pr_err("failed register driver: %d\n", ret);
  219. goto out_free_table;
  220. }
  221. of_node_put(np);
  222. return 0;
  223. out_free_table:
  224. opp_free_cpufreq_table(cpu_dev, &freq_table);
  225. out_put_node:
  226. of_node_put(np);
  227. return ret;
  228. }
  229. static int cpu0_cpufreq_remove(struct platform_device *pdev)
  230. {
  231. cpufreq_unregister_driver(&cpu0_cpufreq_driver);
  232. opp_free_cpufreq_table(cpu_dev, &freq_table);
  233. return 0;
  234. }
  235. static struct platform_driver cpu0_cpufreq_platdrv = {
  236. .driver = {
  237. .name = "cpufreq-cpu0",
  238. .owner = THIS_MODULE,
  239. },
  240. .probe = cpu0_cpufreq_probe,
  241. .remove = cpu0_cpufreq_remove,
  242. };
  243. module_platform_driver(cpu0_cpufreq_platdrv);
  244. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  245. MODULE_DESCRIPTION("Generic CPU0 cpufreq driver");
  246. MODULE_LICENSE("GPL");