cpufreq-dt.c 8.8 KB

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