cpufreq-dt.c 8.7 KB

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