cpufreq-dt.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (C) 2012 Freescale Semiconductor, Inc.
  3. *
  4. * Copyright (C) 2014 Linaro.
  5. * Viresh Kumar <viresh.kumar@linaro.org>
  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. #include "cpufreq-dt.h"
  26. struct private_data {
  27. struct opp_table *opp_table;
  28. struct device *cpu_dev;
  29. struct thermal_cooling_device *cdev;
  30. const char *reg_name;
  31. bool have_static_opps;
  32. };
  33. static struct freq_attr *cpufreq_dt_attr[] = {
  34. &cpufreq_freq_attr_scaling_available_freqs,
  35. NULL, /* Extra space for boost-attr if required */
  36. NULL,
  37. };
  38. static int set_target(struct cpufreq_policy *policy, unsigned int index)
  39. {
  40. struct private_data *priv = policy->driver_data;
  41. unsigned long freq = policy->freq_table[index].frequency;
  42. int ret;
  43. ret = dev_pm_opp_set_rate(priv->cpu_dev, freq * 1000);
  44. if (!ret) {
  45. arch_set_freq_scale(policy->related_cpus, freq,
  46. policy->cpuinfo.max_freq);
  47. }
  48. return ret;
  49. }
  50. /*
  51. * An earlier version of opp-v1 bindings used to name the regulator
  52. * "cpu0-supply", we still need to handle that for backwards compatibility.
  53. */
  54. static const char *find_supply_name(struct device *dev)
  55. {
  56. struct device_node *np;
  57. struct property *pp;
  58. int cpu = dev->id;
  59. const char *name = NULL;
  60. np = of_node_get(dev->of_node);
  61. /* This must be valid for sure */
  62. if (WARN_ON(!np))
  63. return NULL;
  64. /* Try "cpu0" for older DTs */
  65. if (!cpu) {
  66. pp = of_find_property(np, "cpu0-supply", NULL);
  67. if (pp) {
  68. name = "cpu0";
  69. goto node_put;
  70. }
  71. }
  72. pp = of_find_property(np, "cpu-supply", NULL);
  73. if (pp) {
  74. name = "cpu";
  75. goto node_put;
  76. }
  77. dev_dbg(dev, "no regulator for cpu%d\n", cpu);
  78. node_put:
  79. of_node_put(np);
  80. return name;
  81. }
  82. static int resources_available(void)
  83. {
  84. struct device *cpu_dev;
  85. struct regulator *cpu_reg;
  86. struct clk *cpu_clk;
  87. int ret = 0;
  88. const char *name;
  89. cpu_dev = get_cpu_device(0);
  90. if (!cpu_dev) {
  91. pr_err("failed to get cpu0 device\n");
  92. return -ENODEV;
  93. }
  94. cpu_clk = clk_get(cpu_dev, NULL);
  95. ret = PTR_ERR_OR_ZERO(cpu_clk);
  96. if (ret) {
  97. /*
  98. * If cpu's clk node is present, but clock is not yet
  99. * registered, we should try defering probe.
  100. */
  101. if (ret == -EPROBE_DEFER)
  102. dev_dbg(cpu_dev, "clock not ready, retry\n");
  103. else
  104. dev_err(cpu_dev, "failed to get clock: %d\n", ret);
  105. return ret;
  106. }
  107. clk_put(cpu_clk);
  108. name = find_supply_name(cpu_dev);
  109. /* Platform doesn't require regulator */
  110. if (!name)
  111. return 0;
  112. cpu_reg = regulator_get_optional(cpu_dev, name);
  113. ret = PTR_ERR_OR_ZERO(cpu_reg);
  114. if (ret) {
  115. /*
  116. * If cpu's regulator supply node is present, but regulator is
  117. * not yet registered, we should try defering probe.
  118. */
  119. if (ret == -EPROBE_DEFER)
  120. dev_dbg(cpu_dev, "cpu0 regulator not ready, retry\n");
  121. else
  122. dev_dbg(cpu_dev, "no regulator for cpu0: %d\n", ret);
  123. return ret;
  124. }
  125. regulator_put(cpu_reg);
  126. return 0;
  127. }
  128. static int cpufreq_init(struct cpufreq_policy *policy)
  129. {
  130. struct cpufreq_frequency_table *freq_table;
  131. struct opp_table *opp_table = NULL;
  132. struct private_data *priv;
  133. struct device *cpu_dev;
  134. struct clk *cpu_clk;
  135. unsigned int transition_latency;
  136. bool fallback = false;
  137. const char *name;
  138. int ret;
  139. cpu_dev = get_cpu_device(policy->cpu);
  140. if (!cpu_dev) {
  141. pr_err("failed to get cpu%d device\n", policy->cpu);
  142. return -ENODEV;
  143. }
  144. cpu_clk = clk_get(cpu_dev, NULL);
  145. if (IS_ERR(cpu_clk)) {
  146. ret = PTR_ERR(cpu_clk);
  147. dev_err(cpu_dev, "%s: failed to get clk: %d\n", __func__, ret);
  148. return ret;
  149. }
  150. /* Get OPP-sharing information from "operating-points-v2" bindings */
  151. ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, policy->cpus);
  152. if (ret) {
  153. if (ret != -ENOENT)
  154. goto out_put_clk;
  155. /*
  156. * operating-points-v2 not supported, fallback to old method of
  157. * finding shared-OPPs for backward compatibility if the
  158. * platform hasn't set sharing CPUs.
  159. */
  160. if (dev_pm_opp_get_sharing_cpus(cpu_dev, policy->cpus))
  161. fallback = true;
  162. }
  163. /*
  164. * OPP layer will be taking care of regulators now, but it needs to know
  165. * the name of the regulator first.
  166. */
  167. name = find_supply_name(cpu_dev);
  168. if (name) {
  169. opp_table = dev_pm_opp_set_regulators(cpu_dev, &name, 1);
  170. if (IS_ERR(opp_table)) {
  171. ret = PTR_ERR(opp_table);
  172. dev_err(cpu_dev, "Failed to set regulator for cpu%d: %d\n",
  173. policy->cpu, ret);
  174. goto out_put_clk;
  175. }
  176. }
  177. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  178. if (!priv) {
  179. ret = -ENOMEM;
  180. goto out_put_regulator;
  181. }
  182. priv->reg_name = name;
  183. priv->opp_table = opp_table;
  184. /*
  185. * Initialize OPP tables for all policy->cpus. They will be shared by
  186. * all CPUs which have marked their CPUs shared with OPP bindings.
  187. *
  188. * For platforms not using operating-points-v2 bindings, we do this
  189. * before updating policy->cpus. Otherwise, we will end up creating
  190. * duplicate OPPs for policy->cpus.
  191. *
  192. * OPPs might be populated at runtime, don't check for error here
  193. */
  194. if (!dev_pm_opp_of_cpumask_add_table(policy->cpus))
  195. priv->have_static_opps = true;
  196. /*
  197. * But we need OPP table to function so if it is not there let's
  198. * give platform code chance to provide it for us.
  199. */
  200. ret = dev_pm_opp_get_opp_count(cpu_dev);
  201. if (ret <= 0) {
  202. dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n");
  203. ret = -EPROBE_DEFER;
  204. goto out_free_opp;
  205. }
  206. if (fallback) {
  207. cpumask_setall(policy->cpus);
  208. /*
  209. * OPP tables are initialized only for policy->cpu, do it for
  210. * others as well.
  211. */
  212. ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus);
  213. if (ret)
  214. dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
  215. __func__, ret);
  216. }
  217. ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
  218. if (ret) {
  219. dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
  220. goto out_free_opp;
  221. }
  222. priv->cpu_dev = cpu_dev;
  223. policy->driver_data = priv;
  224. policy->clk = cpu_clk;
  225. policy->suspend_freq = dev_pm_opp_get_suspend_opp_freq(cpu_dev) / 1000;
  226. ret = cpufreq_table_validate_and_show(policy, freq_table);
  227. if (ret) {
  228. dev_err(cpu_dev, "%s: invalid frequency table: %d\n", __func__,
  229. ret);
  230. goto out_free_cpufreq_table;
  231. }
  232. /* Support turbo/boost mode */
  233. if (policy_has_boost_freq(policy)) {
  234. /* This gets disabled by core on driver unregister */
  235. ret = cpufreq_enable_boost_support();
  236. if (ret)
  237. goto out_free_cpufreq_table;
  238. cpufreq_dt_attr[1] = &cpufreq_freq_attr_scaling_boost_freqs;
  239. }
  240. transition_latency = dev_pm_opp_get_max_transition_latency(cpu_dev);
  241. if (!transition_latency)
  242. transition_latency = CPUFREQ_ETERNAL;
  243. policy->cpuinfo.transition_latency = transition_latency;
  244. policy->dvfs_possible_from_any_cpu = true;
  245. return 0;
  246. out_free_cpufreq_table:
  247. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  248. out_free_opp:
  249. if (priv->have_static_opps)
  250. dev_pm_opp_of_cpumask_remove_table(policy->cpus);
  251. kfree(priv);
  252. out_put_regulator:
  253. if (name)
  254. dev_pm_opp_put_regulators(opp_table);
  255. out_put_clk:
  256. clk_put(cpu_clk);
  257. return ret;
  258. }
  259. static int cpufreq_exit(struct cpufreq_policy *policy)
  260. {
  261. struct private_data *priv = policy->driver_data;
  262. cpufreq_cooling_unregister(priv->cdev);
  263. dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
  264. if (priv->have_static_opps)
  265. dev_pm_opp_of_cpumask_remove_table(policy->related_cpus);
  266. if (priv->reg_name)
  267. dev_pm_opp_put_regulators(priv->opp_table);
  268. clk_put(policy->clk);
  269. kfree(priv);
  270. return 0;
  271. }
  272. static void cpufreq_ready(struct cpufreq_policy *policy)
  273. {
  274. struct private_data *priv = policy->driver_data;
  275. struct device_node *np = of_node_get(priv->cpu_dev->of_node);
  276. if (WARN_ON(!np))
  277. return;
  278. /*
  279. * For now, just loading the cooling device;
  280. * thermal DT code takes care of matching them.
  281. */
  282. if (of_find_property(np, "#cooling-cells", NULL)) {
  283. u32 power_coefficient = 0;
  284. of_property_read_u32(np, "dynamic-power-coefficient",
  285. &power_coefficient);
  286. priv->cdev = of_cpufreq_power_cooling_register(np,
  287. policy, power_coefficient, NULL);
  288. if (IS_ERR(priv->cdev)) {
  289. dev_err(priv->cpu_dev,
  290. "running cpufreq without cooling device: %ld\n",
  291. PTR_ERR(priv->cdev));
  292. priv->cdev = NULL;
  293. }
  294. }
  295. of_node_put(np);
  296. }
  297. static struct cpufreq_driver dt_cpufreq_driver = {
  298. .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  299. .verify = cpufreq_generic_frequency_table_verify,
  300. .target_index = set_target,
  301. .get = cpufreq_generic_get,
  302. .init = cpufreq_init,
  303. .exit = cpufreq_exit,
  304. .ready = cpufreq_ready,
  305. .name = "cpufreq-dt",
  306. .attr = cpufreq_dt_attr,
  307. .suspend = cpufreq_generic_suspend,
  308. };
  309. static int dt_cpufreq_probe(struct platform_device *pdev)
  310. {
  311. struct cpufreq_dt_platform_data *data = dev_get_platdata(&pdev->dev);
  312. int ret;
  313. /*
  314. * All per-cluster (CPUs sharing clock/voltages) initialization is done
  315. * from ->init(). In probe(), we just need to make sure that clk and
  316. * regulators are available. Else defer probe and retry.
  317. *
  318. * FIXME: Is checking this only for CPU0 sufficient ?
  319. */
  320. ret = resources_available();
  321. if (ret)
  322. return ret;
  323. if (data && data->have_governor_per_policy)
  324. dt_cpufreq_driver.flags |= CPUFREQ_HAVE_GOVERNOR_PER_POLICY;
  325. ret = cpufreq_register_driver(&dt_cpufreq_driver);
  326. if (ret)
  327. dev_err(&pdev->dev, "failed register driver: %d\n", ret);
  328. return ret;
  329. }
  330. static int dt_cpufreq_remove(struct platform_device *pdev)
  331. {
  332. cpufreq_unregister_driver(&dt_cpufreq_driver);
  333. return 0;
  334. }
  335. static struct platform_driver dt_cpufreq_platdrv = {
  336. .driver = {
  337. .name = "cpufreq-dt",
  338. },
  339. .probe = dt_cpufreq_probe,
  340. .remove = dt_cpufreq_remove,
  341. };
  342. module_platform_driver(dt_cpufreq_platdrv);
  343. MODULE_ALIAS("platform:cpufreq-dt");
  344. MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
  345. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  346. MODULE_DESCRIPTION("Generic cpufreq driver");
  347. MODULE_LICENSE("GPL");