cpufreq-dt.c 9.6 KB

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