cpufreq-dt.c 9.5 KB

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