cpufreq-dt.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Freescale Semiconductor, Inc.
  4. *
  5. * Copyright (C) 2014 Linaro.
  6. * Viresh Kumar <viresh.kumar@linaro.org>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/clk.h>
  10. #include <linux/cpu.h>
  11. #include <linux/cpufreq.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/err.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/pm_opp.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/slab.h>
  21. #include <linux/thermal.h>
  22. #include "cpufreq-dt.h"
  23. struct private_data {
  24. struct list_head node;
  25. cpumask_var_t cpus;
  26. struct device *cpu_dev;
  27. struct cpufreq_frequency_table *freq_table;
  28. bool have_static_opps;
  29. int opp_token;
  30. };
  31. static LIST_HEAD(priv_list);
  32. static struct private_data *cpufreq_dt_find_data(int cpu)
  33. {
  34. struct private_data *priv;
  35. list_for_each_entry(priv, &priv_list, node) {
  36. if (cpumask_test_cpu(cpu, priv->cpus))
  37. return priv;
  38. }
  39. return NULL;
  40. }
  41. static int set_target(struct cpufreq_policy *policy, unsigned int index)
  42. {
  43. struct private_data *priv = policy->driver_data;
  44. unsigned long freq = policy->freq_table[index].frequency;
  45. return dev_pm_opp_set_rate(priv->cpu_dev, freq * 1000);
  46. }
  47. /*
  48. * An earlier version of opp-v1 bindings used to name the regulator
  49. * "cpu0-supply", we still need to handle that for backwards compatibility.
  50. */
  51. static const char *find_supply_name(struct device *dev)
  52. {
  53. struct device_node *np __free(device_node) = of_node_get(dev->of_node);
  54. int cpu = dev->id;
  55. /* This must be valid for sure */
  56. if (WARN_ON(!np))
  57. return NULL;
  58. /* Try "cpu0" for older DTs */
  59. if (!cpu && of_property_present(np, "cpu0-supply"))
  60. return "cpu0";
  61. if (of_property_present(np, "cpu-supply"))
  62. return "cpu";
  63. dev_dbg(dev, "no regulator for cpu%d\n", cpu);
  64. return NULL;
  65. }
  66. static int cpufreq_init(struct cpufreq_policy *policy)
  67. {
  68. struct private_data *priv;
  69. struct device *cpu_dev;
  70. struct clk *cpu_clk;
  71. unsigned int transition_latency;
  72. int ret;
  73. priv = cpufreq_dt_find_data(policy->cpu);
  74. if (!priv) {
  75. pr_err("failed to find data for cpu%d\n", policy->cpu);
  76. return -ENODEV;
  77. }
  78. cpu_dev = priv->cpu_dev;
  79. cpu_clk = clk_get(cpu_dev, NULL);
  80. if (IS_ERR(cpu_clk)) {
  81. ret = PTR_ERR(cpu_clk);
  82. dev_err(cpu_dev, "%s: failed to get clk: %d\n", __func__, ret);
  83. return ret;
  84. }
  85. transition_latency = dev_pm_opp_get_max_transition_latency(cpu_dev);
  86. if (!transition_latency)
  87. transition_latency = CPUFREQ_DEFAULT_TRANSITION_LATENCY_NS;
  88. cpumask_copy(policy->cpus, priv->cpus);
  89. policy->driver_data = priv;
  90. policy->clk = cpu_clk;
  91. policy->freq_table = priv->freq_table;
  92. policy->suspend_freq = dev_pm_opp_get_suspend_opp_freq(cpu_dev) / 1000;
  93. policy->cpuinfo.transition_latency = transition_latency;
  94. policy->dvfs_possible_from_any_cpu = true;
  95. return 0;
  96. }
  97. static int cpufreq_online(struct cpufreq_policy *policy)
  98. {
  99. /* We did light-weight tear down earlier, nothing to do here */
  100. return 0;
  101. }
  102. static int cpufreq_offline(struct cpufreq_policy *policy)
  103. {
  104. /*
  105. * Preserve policy->driver_data and don't free resources on light-weight
  106. * tear down.
  107. */
  108. return 0;
  109. }
  110. static void cpufreq_exit(struct cpufreq_policy *policy)
  111. {
  112. clk_put(policy->clk);
  113. }
  114. static struct cpufreq_driver dt_cpufreq_driver = {
  115. .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK |
  116. CPUFREQ_IS_COOLING_DEV,
  117. .verify = cpufreq_generic_frequency_table_verify,
  118. .target_index = set_target,
  119. .get = cpufreq_generic_get,
  120. .init = cpufreq_init,
  121. .exit = cpufreq_exit,
  122. .online = cpufreq_online,
  123. .offline = cpufreq_offline,
  124. .register_em = cpufreq_register_em_with_opp,
  125. .name = "cpufreq-dt",
  126. .set_boost = cpufreq_boost_set_sw,
  127. .suspend = cpufreq_generic_suspend,
  128. };
  129. static int dt_cpufreq_early_init(struct device *dev, int cpu)
  130. {
  131. struct private_data *priv;
  132. struct device *cpu_dev;
  133. bool fallback = false;
  134. const char *reg_name[] = { NULL, NULL };
  135. int ret;
  136. /* Check if this CPU is already covered by some other policy */
  137. if (cpufreq_dt_find_data(cpu))
  138. return 0;
  139. cpu_dev = get_cpu_device(cpu);
  140. if (!cpu_dev)
  141. return -EPROBE_DEFER;
  142. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  143. if (!priv)
  144. return -ENOMEM;
  145. if (!zalloc_cpumask_var(&priv->cpus, GFP_KERNEL))
  146. return -ENOMEM;
  147. cpumask_set_cpu(cpu, priv->cpus);
  148. priv->cpu_dev = cpu_dev;
  149. /*
  150. * OPP layer will be taking care of regulators now, but it needs to know
  151. * the name of the regulator first.
  152. */
  153. reg_name[0] = find_supply_name(cpu_dev);
  154. if (reg_name[0]) {
  155. priv->opp_token = dev_pm_opp_set_regulators(cpu_dev, reg_name);
  156. if (priv->opp_token < 0) {
  157. ret = dev_err_probe(cpu_dev, priv->opp_token,
  158. "failed to set regulators\n");
  159. goto free_cpumask;
  160. }
  161. }
  162. /* Get OPP-sharing information from "operating-points-v2" bindings */
  163. ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, priv->cpus);
  164. if (ret) {
  165. if (ret != -ENOENT)
  166. goto out;
  167. /*
  168. * operating-points-v2 not supported, fallback to all CPUs share
  169. * OPP for backward compatibility if the platform hasn't set
  170. * sharing CPUs.
  171. */
  172. if (dev_pm_opp_get_sharing_cpus(cpu_dev, priv->cpus))
  173. fallback = true;
  174. }
  175. /*
  176. * Initialize OPP tables for all priv->cpus. They will be shared by
  177. * all CPUs which have marked their CPUs shared with OPP bindings.
  178. *
  179. * For platforms not using operating-points-v2 bindings, we do this
  180. * before updating priv->cpus. Otherwise, we will end up creating
  181. * duplicate OPPs for the CPUs.
  182. *
  183. * OPPs might be populated at runtime, don't fail for error here unless
  184. * it is -EPROBE_DEFER.
  185. */
  186. ret = dev_pm_opp_of_cpumask_add_table(priv->cpus);
  187. if (!ret) {
  188. priv->have_static_opps = true;
  189. } else if (ret == -EPROBE_DEFER) {
  190. goto out;
  191. }
  192. /*
  193. * The OPP table must be initialized, statically or dynamically, by this
  194. * point.
  195. */
  196. ret = dev_pm_opp_get_opp_count(cpu_dev);
  197. if (ret <= 0) {
  198. dev_err(cpu_dev, "OPP table can't be empty\n");
  199. ret = -ENODEV;
  200. goto out;
  201. }
  202. if (fallback) {
  203. cpumask_setall(priv->cpus);
  204. ret = dev_pm_opp_set_sharing_cpus(cpu_dev, priv->cpus);
  205. if (ret)
  206. dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
  207. __func__, ret);
  208. }
  209. ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &priv->freq_table);
  210. if (ret) {
  211. dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
  212. goto out;
  213. }
  214. list_add(&priv->node, &priv_list);
  215. return 0;
  216. out:
  217. if (priv->have_static_opps)
  218. dev_pm_opp_of_cpumask_remove_table(priv->cpus);
  219. dev_pm_opp_put_regulators(priv->opp_token);
  220. free_cpumask:
  221. free_cpumask_var(priv->cpus);
  222. return ret;
  223. }
  224. static void dt_cpufreq_release(void)
  225. {
  226. struct private_data *priv, *tmp;
  227. list_for_each_entry_safe(priv, tmp, &priv_list, node) {
  228. dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &priv->freq_table);
  229. if (priv->have_static_opps)
  230. dev_pm_opp_of_cpumask_remove_table(priv->cpus);
  231. dev_pm_opp_put_regulators(priv->opp_token);
  232. free_cpumask_var(priv->cpus);
  233. list_del(&priv->node);
  234. }
  235. }
  236. static int dt_cpufreq_probe(struct platform_device *pdev)
  237. {
  238. struct cpufreq_dt_platform_data *data = dev_get_platdata(&pdev->dev);
  239. int ret, cpu;
  240. /* Request resources early so we can return in case of -EPROBE_DEFER */
  241. for_each_present_cpu(cpu) {
  242. ret = dt_cpufreq_early_init(&pdev->dev, cpu);
  243. if (ret)
  244. goto err;
  245. }
  246. if (data) {
  247. if (data->have_governor_per_policy)
  248. dt_cpufreq_driver.flags |= CPUFREQ_HAVE_GOVERNOR_PER_POLICY;
  249. dt_cpufreq_driver.resume = data->resume;
  250. if (data->suspend)
  251. dt_cpufreq_driver.suspend = data->suspend;
  252. if (data->get_intermediate) {
  253. dt_cpufreq_driver.target_intermediate = data->target_intermediate;
  254. dt_cpufreq_driver.get_intermediate = data->get_intermediate;
  255. }
  256. }
  257. ret = cpufreq_register_driver(&dt_cpufreq_driver);
  258. if (ret) {
  259. dev_err(&pdev->dev, "failed register driver: %d\n", ret);
  260. goto err;
  261. }
  262. return 0;
  263. err:
  264. dt_cpufreq_release();
  265. return ret;
  266. }
  267. static void dt_cpufreq_remove(struct platform_device *pdev)
  268. {
  269. cpufreq_unregister_driver(&dt_cpufreq_driver);
  270. dt_cpufreq_release();
  271. }
  272. static struct platform_driver dt_cpufreq_platdrv = {
  273. .driver = {
  274. .name = "cpufreq-dt",
  275. },
  276. .probe = dt_cpufreq_probe,
  277. .remove = dt_cpufreq_remove,
  278. };
  279. module_platform_driver(dt_cpufreq_platdrv);
  280. struct platform_device *cpufreq_dt_pdev_register(struct device *dev)
  281. {
  282. struct platform_device_info cpufreq_dt_devinfo = {};
  283. cpufreq_dt_devinfo.name = "cpufreq-dt";
  284. cpufreq_dt_devinfo.parent = dev;
  285. return platform_device_register_full(&cpufreq_dt_devinfo);
  286. }
  287. EXPORT_SYMBOL_GPL(cpufreq_dt_pdev_register);
  288. MODULE_ALIAS("platform:cpufreq-dt");
  289. MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
  290. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  291. MODULE_DESCRIPTION("Generic cpufreq driver");
  292. MODULE_LICENSE("GPL");