mt8173-cpufreq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * Copyright (c) 2015 Linaro Ltd.
  3. * Author: Pi-Cheng Chen <pi-cheng.chen@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/cpu.h>
  16. #include <linux/cpu_cooling.h>
  17. #include <linux/cpufreq.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_opp.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/slab.h>
  24. #include <linux/thermal.h>
  25. #define MIN_VOLT_SHIFT (100000)
  26. #define MAX_VOLT_SHIFT (200000)
  27. #define MAX_VOLT_LIMIT (1150000)
  28. #define VOLT_TOL (10000)
  29. /*
  30. * The struct mtk_cpu_dvfs_info holds necessary information for doing CPU DVFS
  31. * on each CPU power/clock domain of Mediatek SoCs. Each CPU cluster in
  32. * Mediatek SoCs has two voltage inputs, Vproc and Vsram. In some cases the two
  33. * voltage inputs need to be controlled under a hardware limitation:
  34. * 100mV < Vsram - Vproc < 200mV
  35. *
  36. * When scaling the clock frequency of a CPU clock domain, the clock source
  37. * needs to be switched to another stable PLL clock temporarily until
  38. * the original PLL becomes stable at target frequency.
  39. */
  40. struct mtk_cpu_dvfs_info {
  41. struct device *cpu_dev;
  42. struct regulator *proc_reg;
  43. struct regulator *sram_reg;
  44. struct clk *cpu_clk;
  45. struct clk *inter_clk;
  46. struct thermal_cooling_device *cdev;
  47. int intermediate_voltage;
  48. bool need_voltage_tracking;
  49. };
  50. static int mtk_cpufreq_voltage_tracking(struct mtk_cpu_dvfs_info *info,
  51. int new_vproc)
  52. {
  53. struct regulator *proc_reg = info->proc_reg;
  54. struct regulator *sram_reg = info->sram_reg;
  55. int old_vproc, old_vsram, new_vsram, vsram, vproc, ret;
  56. old_vproc = regulator_get_voltage(proc_reg);
  57. old_vsram = regulator_get_voltage(sram_reg);
  58. /* Vsram should not exceed the maximum allowed voltage of SoC. */
  59. new_vsram = min(new_vproc + MIN_VOLT_SHIFT, MAX_VOLT_LIMIT);
  60. if (old_vproc < new_vproc) {
  61. /*
  62. * When scaling up voltages, Vsram and Vproc scale up step
  63. * by step. At each step, set Vsram to (Vproc + 200mV) first,
  64. * then set Vproc to (Vsram - 100mV).
  65. * Keep doing it until Vsram and Vproc hit target voltages.
  66. */
  67. do {
  68. old_vsram = regulator_get_voltage(sram_reg);
  69. old_vproc = regulator_get_voltage(proc_reg);
  70. vsram = min(new_vsram, old_vproc + MAX_VOLT_SHIFT);
  71. if (vsram + VOLT_TOL >= MAX_VOLT_LIMIT) {
  72. vsram = MAX_VOLT_LIMIT;
  73. /*
  74. * If the target Vsram hits the maximum voltage,
  75. * try to set the exact voltage value first.
  76. */
  77. ret = regulator_set_voltage(sram_reg, vsram,
  78. vsram);
  79. if (ret)
  80. ret = regulator_set_voltage(sram_reg,
  81. vsram - VOLT_TOL,
  82. vsram);
  83. vproc = new_vproc;
  84. } else {
  85. ret = regulator_set_voltage(sram_reg, vsram,
  86. vsram + VOLT_TOL);
  87. vproc = vsram - MIN_VOLT_SHIFT;
  88. }
  89. if (ret)
  90. return ret;
  91. ret = regulator_set_voltage(proc_reg, vproc,
  92. vproc + VOLT_TOL);
  93. if (ret) {
  94. regulator_set_voltage(sram_reg, old_vsram,
  95. old_vsram);
  96. return ret;
  97. }
  98. } while (vproc < new_vproc || vsram < new_vsram);
  99. } else if (old_vproc > new_vproc) {
  100. /*
  101. * When scaling down voltages, Vsram and Vproc scale down step
  102. * by step. At each step, set Vproc to (Vsram - 200mV) first,
  103. * then set Vproc to (Vproc + 100mV).
  104. * Keep doing it until Vsram and Vproc hit target voltages.
  105. */
  106. do {
  107. old_vproc = regulator_get_voltage(proc_reg);
  108. old_vsram = regulator_get_voltage(sram_reg);
  109. vproc = max(new_vproc, old_vsram - MAX_VOLT_SHIFT);
  110. ret = regulator_set_voltage(proc_reg, vproc,
  111. vproc + VOLT_TOL);
  112. if (ret)
  113. return ret;
  114. if (vproc == new_vproc)
  115. vsram = new_vsram;
  116. else
  117. vsram = max(new_vsram, vproc + MIN_VOLT_SHIFT);
  118. if (vsram + VOLT_TOL >= MAX_VOLT_LIMIT) {
  119. vsram = MAX_VOLT_LIMIT;
  120. /*
  121. * If the target Vsram hits the maximum voltage,
  122. * try to set the exact voltage value first.
  123. */
  124. ret = regulator_set_voltage(sram_reg, vsram,
  125. vsram);
  126. if (ret)
  127. ret = regulator_set_voltage(sram_reg,
  128. vsram - VOLT_TOL,
  129. vsram);
  130. } else {
  131. ret = regulator_set_voltage(sram_reg, vsram,
  132. vsram + VOLT_TOL);
  133. }
  134. if (ret) {
  135. regulator_set_voltage(proc_reg, old_vproc,
  136. old_vproc);
  137. return ret;
  138. }
  139. } while (vproc > new_vproc + VOLT_TOL ||
  140. vsram > new_vsram + VOLT_TOL);
  141. }
  142. return 0;
  143. }
  144. static int mtk_cpufreq_set_voltage(struct mtk_cpu_dvfs_info *info, int vproc)
  145. {
  146. if (info->need_voltage_tracking)
  147. return mtk_cpufreq_voltage_tracking(info, vproc);
  148. else
  149. return regulator_set_voltage(info->proc_reg, vproc,
  150. vproc + VOLT_TOL);
  151. }
  152. static int mtk_cpufreq_set_target(struct cpufreq_policy *policy,
  153. unsigned int index)
  154. {
  155. struct cpufreq_frequency_table *freq_table = policy->freq_table;
  156. struct clk *cpu_clk = policy->clk;
  157. struct clk *armpll = clk_get_parent(cpu_clk);
  158. struct mtk_cpu_dvfs_info *info = policy->driver_data;
  159. struct device *cpu_dev = info->cpu_dev;
  160. struct dev_pm_opp *opp;
  161. long freq_hz, old_freq_hz;
  162. int vproc, old_vproc, inter_vproc, target_vproc, ret;
  163. inter_vproc = info->intermediate_voltage;
  164. old_freq_hz = clk_get_rate(cpu_clk);
  165. old_vproc = regulator_get_voltage(info->proc_reg);
  166. freq_hz = freq_table[index].frequency * 1000;
  167. rcu_read_lock();
  168. opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz);
  169. if (IS_ERR(opp)) {
  170. rcu_read_unlock();
  171. pr_err("cpu%d: failed to find OPP for %ld\n",
  172. policy->cpu, freq_hz);
  173. return PTR_ERR(opp);
  174. }
  175. vproc = dev_pm_opp_get_voltage(opp);
  176. rcu_read_unlock();
  177. /*
  178. * If the new voltage or the intermediate voltage is higher than the
  179. * current voltage, scale up voltage first.
  180. */
  181. target_vproc = (inter_vproc > vproc) ? inter_vproc : vproc;
  182. if (old_vproc < target_vproc) {
  183. ret = mtk_cpufreq_set_voltage(info, target_vproc);
  184. if (ret) {
  185. pr_err("cpu%d: failed to scale up voltage!\n",
  186. policy->cpu);
  187. mtk_cpufreq_set_voltage(info, old_vproc);
  188. return ret;
  189. }
  190. }
  191. /* Reparent the CPU clock to intermediate clock. */
  192. ret = clk_set_parent(cpu_clk, info->inter_clk);
  193. if (ret) {
  194. pr_err("cpu%d: failed to re-parent cpu clock!\n",
  195. policy->cpu);
  196. mtk_cpufreq_set_voltage(info, old_vproc);
  197. WARN_ON(1);
  198. return ret;
  199. }
  200. /* Set the original PLL to target rate. */
  201. ret = clk_set_rate(armpll, freq_hz);
  202. if (ret) {
  203. pr_err("cpu%d: failed to scale cpu clock rate!\n",
  204. policy->cpu);
  205. clk_set_parent(cpu_clk, armpll);
  206. mtk_cpufreq_set_voltage(info, old_vproc);
  207. return ret;
  208. }
  209. /* Set parent of CPU clock back to the original PLL. */
  210. ret = clk_set_parent(cpu_clk, armpll);
  211. if (ret) {
  212. pr_err("cpu%d: failed to re-parent cpu clock!\n",
  213. policy->cpu);
  214. mtk_cpufreq_set_voltage(info, inter_vproc);
  215. WARN_ON(1);
  216. return ret;
  217. }
  218. /*
  219. * If the new voltage is lower than the intermediate voltage or the
  220. * original voltage, scale down to the new voltage.
  221. */
  222. if (vproc < inter_vproc || vproc < old_vproc) {
  223. ret = mtk_cpufreq_set_voltage(info, vproc);
  224. if (ret) {
  225. pr_err("cpu%d: failed to scale down voltage!\n",
  226. policy->cpu);
  227. clk_set_parent(cpu_clk, info->inter_clk);
  228. clk_set_rate(armpll, old_freq_hz);
  229. clk_set_parent(cpu_clk, armpll);
  230. return ret;
  231. }
  232. }
  233. return 0;
  234. }
  235. static void mtk_cpufreq_ready(struct cpufreq_policy *policy)
  236. {
  237. struct mtk_cpu_dvfs_info *info = policy->driver_data;
  238. struct device_node *np = of_node_get(info->cpu_dev->of_node);
  239. if (WARN_ON(!np))
  240. return;
  241. if (of_find_property(np, "#cooling-cells", NULL)) {
  242. info->cdev = of_cpufreq_cooling_register(np,
  243. policy->related_cpus);
  244. if (IS_ERR(info->cdev)) {
  245. dev_err(info->cpu_dev,
  246. "running cpufreq without cooling device: %ld\n",
  247. PTR_ERR(info->cdev));
  248. info->cdev = NULL;
  249. }
  250. }
  251. of_node_put(np);
  252. }
  253. static int mtk_cpu_dvfs_info_init(struct mtk_cpu_dvfs_info *info, int cpu)
  254. {
  255. struct device *cpu_dev;
  256. struct regulator *proc_reg = ERR_PTR(-ENODEV);
  257. struct regulator *sram_reg = ERR_PTR(-ENODEV);
  258. struct clk *cpu_clk = ERR_PTR(-ENODEV);
  259. struct clk *inter_clk = ERR_PTR(-ENODEV);
  260. struct dev_pm_opp *opp;
  261. unsigned long rate;
  262. int ret;
  263. cpu_dev = get_cpu_device(cpu);
  264. if (!cpu_dev) {
  265. pr_err("failed to get cpu%d device\n", cpu);
  266. return -ENODEV;
  267. }
  268. cpu_clk = clk_get(cpu_dev, "cpu");
  269. if (IS_ERR(cpu_clk)) {
  270. if (PTR_ERR(cpu_clk) == -EPROBE_DEFER)
  271. pr_warn("cpu clk for cpu%d not ready, retry.\n", cpu);
  272. else
  273. pr_err("failed to get cpu clk for cpu%d\n", cpu);
  274. ret = PTR_ERR(cpu_clk);
  275. return ret;
  276. }
  277. inter_clk = clk_get(cpu_dev, "intermediate");
  278. if (IS_ERR(inter_clk)) {
  279. if (PTR_ERR(inter_clk) == -EPROBE_DEFER)
  280. pr_warn("intermediate clk for cpu%d not ready, retry.\n",
  281. cpu);
  282. else
  283. pr_err("failed to get intermediate clk for cpu%d\n",
  284. cpu);
  285. ret = PTR_ERR(inter_clk);
  286. goto out_free_resources;
  287. }
  288. proc_reg = regulator_get_exclusive(cpu_dev, "proc");
  289. if (IS_ERR(proc_reg)) {
  290. if (PTR_ERR(proc_reg) == -EPROBE_DEFER)
  291. pr_warn("proc regulator for cpu%d not ready, retry.\n",
  292. cpu);
  293. else
  294. pr_err("failed to get proc regulator for cpu%d\n",
  295. cpu);
  296. ret = PTR_ERR(proc_reg);
  297. goto out_free_resources;
  298. }
  299. /* Both presence and absence of sram regulator are valid cases. */
  300. sram_reg = regulator_get_exclusive(cpu_dev, "sram");
  301. ret = dev_pm_opp_of_add_table(cpu_dev);
  302. if (ret) {
  303. pr_warn("no OPP table for cpu%d\n", cpu);
  304. goto out_free_resources;
  305. }
  306. /* Search a safe voltage for intermediate frequency. */
  307. rate = clk_get_rate(inter_clk);
  308. rcu_read_lock();
  309. opp = dev_pm_opp_find_freq_ceil(cpu_dev, &rate);
  310. if (IS_ERR(opp)) {
  311. rcu_read_unlock();
  312. pr_err("failed to get intermediate opp for cpu%d\n", cpu);
  313. ret = PTR_ERR(opp);
  314. goto out_free_opp_table;
  315. }
  316. info->intermediate_voltage = dev_pm_opp_get_voltage(opp);
  317. rcu_read_unlock();
  318. info->cpu_dev = cpu_dev;
  319. info->proc_reg = proc_reg;
  320. info->sram_reg = IS_ERR(sram_reg) ? NULL : sram_reg;
  321. info->cpu_clk = cpu_clk;
  322. info->inter_clk = inter_clk;
  323. /*
  324. * If SRAM regulator is present, software "voltage tracking" is needed
  325. * for this CPU power domain.
  326. */
  327. info->need_voltage_tracking = !IS_ERR(sram_reg);
  328. return 0;
  329. out_free_opp_table:
  330. dev_pm_opp_of_remove_table(cpu_dev);
  331. out_free_resources:
  332. if (!IS_ERR(proc_reg))
  333. regulator_put(proc_reg);
  334. if (!IS_ERR(sram_reg))
  335. regulator_put(sram_reg);
  336. if (!IS_ERR(cpu_clk))
  337. clk_put(cpu_clk);
  338. if (!IS_ERR(inter_clk))
  339. clk_put(inter_clk);
  340. return ret;
  341. }
  342. static void mtk_cpu_dvfs_info_release(struct mtk_cpu_dvfs_info *info)
  343. {
  344. if (!IS_ERR(info->proc_reg))
  345. regulator_put(info->proc_reg);
  346. if (!IS_ERR(info->sram_reg))
  347. regulator_put(info->sram_reg);
  348. if (!IS_ERR(info->cpu_clk))
  349. clk_put(info->cpu_clk);
  350. if (!IS_ERR(info->inter_clk))
  351. clk_put(info->inter_clk);
  352. dev_pm_opp_of_remove_table(info->cpu_dev);
  353. }
  354. static int mtk_cpufreq_init(struct cpufreq_policy *policy)
  355. {
  356. struct mtk_cpu_dvfs_info *info;
  357. struct cpufreq_frequency_table *freq_table;
  358. int ret;
  359. info = kzalloc(sizeof(*info), GFP_KERNEL);
  360. if (!info)
  361. return -ENOMEM;
  362. ret = mtk_cpu_dvfs_info_init(info, policy->cpu);
  363. if (ret) {
  364. pr_err("%s failed to initialize dvfs info for cpu%d\n",
  365. __func__, policy->cpu);
  366. goto out_free_dvfs_info;
  367. }
  368. ret = dev_pm_opp_init_cpufreq_table(info->cpu_dev, &freq_table);
  369. if (ret) {
  370. pr_err("failed to init cpufreq table for cpu%d: %d\n",
  371. policy->cpu, ret);
  372. goto out_release_dvfs_info;
  373. }
  374. ret = cpufreq_table_validate_and_show(policy, freq_table);
  375. if (ret) {
  376. pr_err("%s: invalid frequency table: %d\n", __func__, ret);
  377. goto out_free_cpufreq_table;
  378. }
  379. /* CPUs in the same cluster share a clock and power domain. */
  380. cpumask_copy(policy->cpus, &cpu_topology[policy->cpu].core_sibling);
  381. policy->driver_data = info;
  382. policy->clk = info->cpu_clk;
  383. return 0;
  384. out_free_cpufreq_table:
  385. dev_pm_opp_free_cpufreq_table(info->cpu_dev, &freq_table);
  386. out_release_dvfs_info:
  387. mtk_cpu_dvfs_info_release(info);
  388. out_free_dvfs_info:
  389. kfree(info);
  390. return ret;
  391. }
  392. static int mtk_cpufreq_exit(struct cpufreq_policy *policy)
  393. {
  394. struct mtk_cpu_dvfs_info *info = policy->driver_data;
  395. cpufreq_cooling_unregister(info->cdev);
  396. dev_pm_opp_free_cpufreq_table(info->cpu_dev, &policy->freq_table);
  397. mtk_cpu_dvfs_info_release(info);
  398. kfree(info);
  399. return 0;
  400. }
  401. static struct cpufreq_driver mt8173_cpufreq_driver = {
  402. .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  403. .verify = cpufreq_generic_frequency_table_verify,
  404. .target_index = mtk_cpufreq_set_target,
  405. .get = cpufreq_generic_get,
  406. .init = mtk_cpufreq_init,
  407. .exit = mtk_cpufreq_exit,
  408. .ready = mtk_cpufreq_ready,
  409. .name = "mtk-cpufreq",
  410. .attr = cpufreq_generic_attr,
  411. };
  412. static int mt8173_cpufreq_probe(struct platform_device *pdev)
  413. {
  414. int ret;
  415. ret = cpufreq_register_driver(&mt8173_cpufreq_driver);
  416. if (ret)
  417. pr_err("failed to register mtk cpufreq driver\n");
  418. return ret;
  419. }
  420. static struct platform_driver mt8173_cpufreq_platdrv = {
  421. .driver = {
  422. .name = "mt8173-cpufreq",
  423. },
  424. .probe = mt8173_cpufreq_probe,
  425. };
  426. static int mt8173_cpufreq_driver_init(void)
  427. {
  428. struct platform_device *pdev;
  429. int err;
  430. if (!of_machine_is_compatible("mediatek,mt8173"))
  431. return -ENODEV;
  432. err = platform_driver_register(&mt8173_cpufreq_platdrv);
  433. if (err)
  434. return err;
  435. /*
  436. * Since there's no place to hold device registration code and no
  437. * device tree based way to match cpufreq driver yet, both the driver
  438. * and the device registration codes are put here to handle defer
  439. * probing.
  440. */
  441. pdev = platform_device_register_simple("mt8173-cpufreq", -1, NULL, 0);
  442. if (IS_ERR(pdev)) {
  443. pr_err("failed to register mtk-cpufreq platform device\n");
  444. return PTR_ERR(pdev);
  445. }
  446. return 0;
  447. }
  448. device_initcall(mt8173_cpufreq_driver_init);