cpufreq-dt.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 opp_table *opp_table;
  28. struct opp_table *reg_opp_table;
  29. bool have_static_opps;
  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;
  59. struct property *pp;
  60. int cpu = dev->id;
  61. const char *name = NULL;
  62. np = of_node_get(dev->of_node);
  63. /* This must be valid for sure */
  64. if (WARN_ON(!np))
  65. return NULL;
  66. /* Try "cpu0" for older DTs */
  67. if (!cpu) {
  68. pp = of_find_property(np, "cpu0-supply", NULL);
  69. if (pp) {
  70. name = "cpu0";
  71. goto node_put;
  72. }
  73. }
  74. pp = of_find_property(np, "cpu-supply", NULL);
  75. if (pp) {
  76. name = "cpu";
  77. goto node_put;
  78. }
  79. dev_dbg(dev, "no regulator for cpu%d\n", cpu);
  80. node_put:
  81. of_node_put(np);
  82. return name;
  83. }
  84. static int cpufreq_init(struct cpufreq_policy *policy)
  85. {
  86. struct cpufreq_frequency_table *freq_table;
  87. struct private_data *priv;
  88. struct device *cpu_dev;
  89. struct clk *cpu_clk;
  90. unsigned int transition_latency;
  91. int ret;
  92. priv = cpufreq_dt_find_data(policy->cpu);
  93. if (!priv) {
  94. pr_err("failed to find data for cpu%d\n", policy->cpu);
  95. return -ENODEV;
  96. }
  97. cpu_dev = priv->cpu_dev;
  98. cpumask_copy(policy->cpus, priv->cpus);
  99. cpu_clk = clk_get(cpu_dev, NULL);
  100. if (IS_ERR(cpu_clk)) {
  101. ret = PTR_ERR(cpu_clk);
  102. dev_err(cpu_dev, "%s: failed to get clk: %d\n", __func__, ret);
  103. return ret;
  104. }
  105. /*
  106. * Initialize OPP tables for all policy->cpus. They will be shared by
  107. * all CPUs which have marked their CPUs shared with OPP bindings.
  108. *
  109. * For platforms not using operating-points-v2 bindings, we do this
  110. * before updating policy->cpus. Otherwise, we will end up creating
  111. * duplicate OPPs for policy->cpus.
  112. *
  113. * OPPs might be populated at runtime, don't check for error here
  114. */
  115. if (!dev_pm_opp_of_cpumask_add_table(policy->cpus))
  116. priv->have_static_opps = true;
  117. /*
  118. * But we need OPP table to function so if it is not there let's
  119. * give platform code chance to provide it for us.
  120. */
  121. ret = dev_pm_opp_get_opp_count(cpu_dev);
  122. if (ret <= 0) {
  123. dev_err(cpu_dev, "OPP table can't be empty\n");
  124. ret = -ENODEV;
  125. goto out_free_opp;
  126. }
  127. ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
  128. if (ret) {
  129. dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
  130. goto out_free_opp;
  131. }
  132. policy->driver_data = priv;
  133. policy->clk = cpu_clk;
  134. policy->freq_table = freq_table;
  135. policy->suspend_freq = dev_pm_opp_get_suspend_opp_freq(cpu_dev) / 1000;
  136. /* Support turbo/boost mode */
  137. if (policy_has_boost_freq(policy)) {
  138. /* This gets disabled by core on driver unregister */
  139. ret = cpufreq_enable_boost_support();
  140. if (ret)
  141. goto out_free_cpufreq_table;
  142. cpufreq_dt_attr[1] = &cpufreq_freq_attr_scaling_boost_freqs;
  143. }
  144. transition_latency = dev_pm_opp_get_max_transition_latency(cpu_dev);
  145. if (!transition_latency)
  146. transition_latency = CPUFREQ_ETERNAL;
  147. policy->cpuinfo.transition_latency = transition_latency;
  148. policy->dvfs_possible_from_any_cpu = true;
  149. dev_pm_opp_of_register_em(cpu_dev, policy->cpus);
  150. return 0;
  151. out_free_cpufreq_table:
  152. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  153. out_free_opp:
  154. if (priv->have_static_opps)
  155. dev_pm_opp_of_cpumask_remove_table(policy->cpus);
  156. clk_put(cpu_clk);
  157. return ret;
  158. }
  159. static int cpufreq_online(struct cpufreq_policy *policy)
  160. {
  161. /* We did light-weight tear down earlier, nothing to do here */
  162. return 0;
  163. }
  164. static int cpufreq_offline(struct cpufreq_policy *policy)
  165. {
  166. /*
  167. * Preserve policy->driver_data and don't free resources on light-weight
  168. * tear down.
  169. */
  170. return 0;
  171. }
  172. static int cpufreq_exit(struct cpufreq_policy *policy)
  173. {
  174. struct private_data *priv = policy->driver_data;
  175. dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
  176. if (priv->have_static_opps)
  177. dev_pm_opp_of_cpumask_remove_table(policy->related_cpus);
  178. clk_put(policy->clk);
  179. return 0;
  180. }
  181. static struct cpufreq_driver dt_cpufreq_driver = {
  182. .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK |
  183. CPUFREQ_IS_COOLING_DEV,
  184. .verify = cpufreq_generic_frequency_table_verify,
  185. .target_index = set_target,
  186. .get = cpufreq_generic_get,
  187. .init = cpufreq_init,
  188. .exit = cpufreq_exit,
  189. .online = cpufreq_online,
  190. .offline = cpufreq_offline,
  191. .name = "cpufreq-dt",
  192. .attr = cpufreq_dt_attr,
  193. .suspend = cpufreq_generic_suspend,
  194. };
  195. static int dt_cpufreq_early_init(struct device *dev, int cpu)
  196. {
  197. struct private_data *priv;
  198. struct device *cpu_dev;
  199. const char *reg_name;
  200. int ret;
  201. /* Check if this CPU is already covered by some other policy */
  202. if (cpufreq_dt_find_data(cpu))
  203. return 0;
  204. cpu_dev = get_cpu_device(cpu);
  205. if (!cpu_dev)
  206. return -EPROBE_DEFER;
  207. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  208. if (!priv)
  209. return -ENOMEM;
  210. if (!alloc_cpumask_var(&priv->cpus, GFP_KERNEL))
  211. return -ENOMEM;
  212. priv->cpu_dev = cpu_dev;
  213. /* Try to get OPP table early to ensure resources are available */
  214. priv->opp_table = dev_pm_opp_get_opp_table(cpu_dev);
  215. if (IS_ERR(priv->opp_table)) {
  216. ret = PTR_ERR(priv->opp_table);
  217. if (ret != -EPROBE_DEFER)
  218. dev_err(cpu_dev, "failed to get OPP table: %d\n", ret);
  219. goto free_cpumask;
  220. }
  221. /*
  222. * OPP layer will be taking care of regulators now, but it needs to know
  223. * the name of the regulator first.
  224. */
  225. reg_name = find_supply_name(cpu_dev);
  226. if (reg_name) {
  227. priv->reg_opp_table = dev_pm_opp_set_regulators(cpu_dev,
  228. &reg_name, 1);
  229. if (IS_ERR(priv->reg_opp_table)) {
  230. ret = PTR_ERR(priv->reg_opp_table);
  231. if (ret != -EPROBE_DEFER)
  232. dev_err(cpu_dev, "failed to set regulators: %d\n",
  233. ret);
  234. goto put_table;
  235. }
  236. }
  237. /* Find OPP sharing information so we can fill pri->cpus here */
  238. /* Get OPP-sharing information from "operating-points-v2" bindings */
  239. ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, priv->cpus);
  240. if (ret) {
  241. if (ret != -ENOENT)
  242. goto put_reg;
  243. /*
  244. * operating-points-v2 not supported, fallback to all CPUs share
  245. * OPP for backward compatibility if the platform hasn't set
  246. * sharing CPUs.
  247. */
  248. if (dev_pm_opp_get_sharing_cpus(cpu_dev, priv->cpus)) {
  249. cpumask_setall(priv->cpus);
  250. /*
  251. * OPP tables are initialized only for cpu, do it for
  252. * others as well.
  253. */
  254. ret = dev_pm_opp_set_sharing_cpus(cpu_dev, priv->cpus);
  255. if (ret)
  256. dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
  257. __func__, ret);
  258. }
  259. }
  260. list_add(&priv->node, &priv_list);
  261. return 0;
  262. put_reg:
  263. if (priv->reg_opp_table)
  264. dev_pm_opp_put_regulators(priv->reg_opp_table);
  265. put_table:
  266. dev_pm_opp_put_opp_table(priv->opp_table);
  267. free_cpumask:
  268. free_cpumask_var(priv->cpus);
  269. return ret;
  270. }
  271. static void dt_cpufreq_release(void)
  272. {
  273. struct private_data *priv, *tmp;
  274. list_for_each_entry_safe(priv, tmp, &priv_list, node) {
  275. if (priv->reg_opp_table)
  276. dev_pm_opp_put_regulators(priv->reg_opp_table);
  277. dev_pm_opp_put_opp_table(priv->opp_table);
  278. free_cpumask_var(priv->cpus);
  279. list_del(&priv->node);
  280. }
  281. }
  282. static int dt_cpufreq_probe(struct platform_device *pdev)
  283. {
  284. struct cpufreq_dt_platform_data *data = dev_get_platdata(&pdev->dev);
  285. int ret, cpu;
  286. /* Request resources early so we can return in case of -EPROBE_DEFER */
  287. for_each_possible_cpu(cpu) {
  288. ret = dt_cpufreq_early_init(&pdev->dev, cpu);
  289. if (ret)
  290. goto err;
  291. }
  292. if (data) {
  293. if (data->have_governor_per_policy)
  294. dt_cpufreq_driver.flags |= CPUFREQ_HAVE_GOVERNOR_PER_POLICY;
  295. dt_cpufreq_driver.resume = data->resume;
  296. if (data->suspend)
  297. dt_cpufreq_driver.suspend = data->suspend;
  298. if (data->get_intermediate) {
  299. dt_cpufreq_driver.target_intermediate = data->target_intermediate;
  300. dt_cpufreq_driver.get_intermediate = data->get_intermediate;
  301. }
  302. }
  303. ret = cpufreq_register_driver(&dt_cpufreq_driver);
  304. if (ret) {
  305. dev_err(&pdev->dev, "failed register driver: %d\n", ret);
  306. goto err;
  307. }
  308. return 0;
  309. err:
  310. dt_cpufreq_release();
  311. return ret;
  312. }
  313. static int dt_cpufreq_remove(struct platform_device *pdev)
  314. {
  315. cpufreq_unregister_driver(&dt_cpufreq_driver);
  316. dt_cpufreq_release();
  317. return 0;
  318. }
  319. static struct platform_driver dt_cpufreq_platdrv = {
  320. .driver = {
  321. .name = "cpufreq-dt",
  322. },
  323. .probe = dt_cpufreq_probe,
  324. .remove = dt_cpufreq_remove,
  325. };
  326. module_platform_driver(dt_cpufreq_platdrv);
  327. MODULE_ALIAS("platform:cpufreq-dt");
  328. MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
  329. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  330. MODULE_DESCRIPTION("Generic cpufreq driver");
  331. MODULE_LICENSE("GPL");