cpufreq-cpu0.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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_frequency_table_cpuinfo(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. cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
  126. return 0;
  127. }
  128. static int cpu0_cpufreq_exit(struct cpufreq_policy *policy)
  129. {
  130. cpufreq_frequency_table_put_attr(policy->cpu);
  131. return 0;
  132. }
  133. static struct freq_attr *cpu0_cpufreq_attr[] = {
  134. &cpufreq_freq_attr_scaling_available_freqs,
  135. NULL,
  136. };
  137. static struct cpufreq_driver cpu0_cpufreq_driver = {
  138. .flags = CPUFREQ_STICKY,
  139. .verify = cpu0_verify_speed,
  140. .target = cpu0_set_target,
  141. .get = cpu0_get_speed,
  142. .init = cpu0_cpufreq_init,
  143. .exit = cpu0_cpufreq_exit,
  144. .name = "generic_cpu0",
  145. .attr = cpu0_cpufreq_attr,
  146. };
  147. static int cpu0_cpufreq_probe(struct platform_device *pdev)
  148. {
  149. struct device_node *np;
  150. int ret;
  151. cpu_dev = get_cpu_device(0);
  152. if (!cpu_dev) {
  153. pr_err("failed to get cpu0 device\n");
  154. return -ENODEV;
  155. }
  156. np = of_node_get(cpu_dev->of_node);
  157. if (!np) {
  158. pr_err("failed to find cpu0 node\n");
  159. return -ENOENT;
  160. }
  161. cpu_reg = devm_regulator_get_optional(cpu_dev, "cpu0");
  162. if (IS_ERR(cpu_reg)) {
  163. /*
  164. * If cpu0 regulator supply node is present, but regulator is
  165. * not yet registered, we should try defering probe.
  166. */
  167. if (PTR_ERR(cpu_reg) == -EPROBE_DEFER) {
  168. dev_err(cpu_dev, "cpu0 regulator not ready, retry\n");
  169. ret = -EPROBE_DEFER;
  170. goto out_put_node;
  171. }
  172. pr_warn("failed to get cpu0 regulator: %ld\n",
  173. PTR_ERR(cpu_reg));
  174. }
  175. cpu_clk = devm_clk_get(cpu_dev, NULL);
  176. if (IS_ERR(cpu_clk)) {
  177. ret = PTR_ERR(cpu_clk);
  178. pr_err("failed to get cpu0 clock: %d\n", ret);
  179. goto out_put_node;
  180. }
  181. ret = of_init_opp_table(cpu_dev);
  182. if (ret) {
  183. pr_err("failed to init OPP table: %d\n", ret);
  184. goto out_put_node;
  185. }
  186. ret = opp_init_cpufreq_table(cpu_dev, &freq_table);
  187. if (ret) {
  188. pr_err("failed to init cpufreq table: %d\n", ret);
  189. goto out_put_node;
  190. }
  191. of_property_read_u32(np, "voltage-tolerance", &voltage_tolerance);
  192. if (of_property_read_u32(np, "clock-latency", &transition_latency))
  193. transition_latency = CPUFREQ_ETERNAL;
  194. if (cpu_reg) {
  195. struct opp *opp;
  196. unsigned long min_uV, max_uV;
  197. int i;
  198. /*
  199. * OPP is maintained in order of increasing frequency, and
  200. * freq_table initialised from OPP is therefore sorted in the
  201. * same order.
  202. */
  203. for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++)
  204. ;
  205. rcu_read_lock();
  206. opp = opp_find_freq_exact(cpu_dev,
  207. freq_table[0].frequency * 1000, true);
  208. min_uV = opp_get_voltage(opp);
  209. opp = opp_find_freq_exact(cpu_dev,
  210. freq_table[i-1].frequency * 1000, true);
  211. max_uV = opp_get_voltage(opp);
  212. rcu_read_unlock();
  213. ret = regulator_set_voltage_time(cpu_reg, min_uV, max_uV);
  214. if (ret > 0)
  215. transition_latency += ret * 1000;
  216. }
  217. ret = cpufreq_register_driver(&cpu0_cpufreq_driver);
  218. if (ret) {
  219. pr_err("failed register driver: %d\n", ret);
  220. goto out_free_table;
  221. }
  222. of_node_put(np);
  223. return 0;
  224. out_free_table:
  225. opp_free_cpufreq_table(cpu_dev, &freq_table);
  226. out_put_node:
  227. of_node_put(np);
  228. return ret;
  229. }
  230. static int cpu0_cpufreq_remove(struct platform_device *pdev)
  231. {
  232. cpufreq_unregister_driver(&cpu0_cpufreq_driver);
  233. opp_free_cpufreq_table(cpu_dev, &freq_table);
  234. return 0;
  235. }
  236. static struct platform_driver cpu0_cpufreq_platdrv = {
  237. .driver = {
  238. .name = "cpufreq-cpu0",
  239. .owner = THIS_MODULE,
  240. },
  241. .probe = cpu0_cpufreq_probe,
  242. .remove = cpu0_cpufreq_remove,
  243. };
  244. module_platform_driver(cpu0_cpufreq_platdrv);
  245. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  246. MODULE_DESCRIPTION("Generic CPU0 cpufreq driver");
  247. MODULE_LICENSE("GPL");