imx6q-cpufreq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright (C) 2013 Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/cpu.h>
  10. #include <linux/cpufreq.h>
  11. #include <linux/err.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/pm_opp.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regulator/consumer.h>
  17. #define PU_SOC_VOLTAGE_NORMAL 1250000
  18. #define PU_SOC_VOLTAGE_HIGH 1275000
  19. #define FREQ_1P2_GHZ 1200000000
  20. static struct regulator *arm_reg;
  21. static struct regulator *pu_reg;
  22. static struct regulator *soc_reg;
  23. static struct clk *arm_clk;
  24. static struct clk *pll1_sys_clk;
  25. static struct clk *pll1_sw_clk;
  26. static struct clk *step_clk;
  27. static struct clk *pll2_pfd2_396m_clk;
  28. /* clk used by i.MX6UL */
  29. static struct clk *pll2_bus_clk;
  30. static struct clk *secondary_sel_clk;
  31. static struct device *cpu_dev;
  32. static bool free_opp;
  33. static struct cpufreq_frequency_table *freq_table;
  34. static unsigned int transition_latency;
  35. static u32 *imx6_soc_volt;
  36. static u32 soc_opp_count;
  37. static int imx6q_set_target(struct cpufreq_policy *policy, unsigned int index)
  38. {
  39. struct dev_pm_opp *opp;
  40. unsigned long freq_hz, volt, volt_old;
  41. unsigned int old_freq, new_freq;
  42. bool pll1_sys_temp_enabled = false;
  43. int ret;
  44. new_freq = freq_table[index].frequency;
  45. freq_hz = new_freq * 1000;
  46. old_freq = clk_get_rate(arm_clk) / 1000;
  47. opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz);
  48. if (IS_ERR(opp)) {
  49. dev_err(cpu_dev, "failed to find OPP for %ld\n", freq_hz);
  50. return PTR_ERR(opp);
  51. }
  52. volt = dev_pm_opp_get_voltage(opp);
  53. dev_pm_opp_put(opp);
  54. volt_old = regulator_get_voltage(arm_reg);
  55. dev_dbg(cpu_dev, "%u MHz, %ld mV --> %u MHz, %ld mV\n",
  56. old_freq / 1000, volt_old / 1000,
  57. new_freq / 1000, volt / 1000);
  58. /* scaling up? scale voltage before frequency */
  59. if (new_freq > old_freq) {
  60. if (!IS_ERR(pu_reg)) {
  61. ret = regulator_set_voltage_tol(pu_reg, imx6_soc_volt[index], 0);
  62. if (ret) {
  63. dev_err(cpu_dev, "failed to scale vddpu up: %d\n", ret);
  64. return ret;
  65. }
  66. }
  67. ret = regulator_set_voltage_tol(soc_reg, imx6_soc_volt[index], 0);
  68. if (ret) {
  69. dev_err(cpu_dev, "failed to scale vddsoc up: %d\n", ret);
  70. return ret;
  71. }
  72. ret = regulator_set_voltage_tol(arm_reg, volt, 0);
  73. if (ret) {
  74. dev_err(cpu_dev,
  75. "failed to scale vddarm up: %d\n", ret);
  76. return ret;
  77. }
  78. }
  79. /*
  80. * The setpoints are selected per PLL/PDF frequencies, so we need to
  81. * reprogram PLL for frequency scaling. The procedure of reprogramming
  82. * PLL1 is as below.
  83. * For i.MX6UL, it has a secondary clk mux, the cpu frequency change
  84. * flow is slightly different from other i.MX6 OSC.
  85. * The cpu frequeny change flow for i.MX6(except i.MX6UL) is as below:
  86. * - Enable pll2_pfd2_396m_clk and reparent pll1_sw_clk to it
  87. * - Reprogram pll1_sys_clk and reparent pll1_sw_clk back to it
  88. * - Disable pll2_pfd2_396m_clk
  89. */
  90. if (of_machine_is_compatible("fsl,imx6ul") ||
  91. of_machine_is_compatible("fsl,imx6ull")) {
  92. /*
  93. * When changing pll1_sw_clk's parent to pll1_sys_clk,
  94. * CPU may run at higher than 528MHz, this will lead to
  95. * the system unstable if the voltage is lower than the
  96. * voltage of 528MHz, so lower the CPU frequency to one
  97. * half before changing CPU frequency.
  98. */
  99. clk_set_rate(arm_clk, (old_freq >> 1) * 1000);
  100. clk_set_parent(pll1_sw_clk, pll1_sys_clk);
  101. if (freq_hz > clk_get_rate(pll2_pfd2_396m_clk))
  102. clk_set_parent(secondary_sel_clk, pll2_bus_clk);
  103. else
  104. clk_set_parent(secondary_sel_clk, pll2_pfd2_396m_clk);
  105. clk_set_parent(step_clk, secondary_sel_clk);
  106. clk_set_parent(pll1_sw_clk, step_clk);
  107. } else {
  108. clk_set_parent(step_clk, pll2_pfd2_396m_clk);
  109. clk_set_parent(pll1_sw_clk, step_clk);
  110. if (freq_hz > clk_get_rate(pll2_pfd2_396m_clk)) {
  111. clk_set_rate(pll1_sys_clk, new_freq * 1000);
  112. clk_set_parent(pll1_sw_clk, pll1_sys_clk);
  113. } else {
  114. /* pll1_sys needs to be enabled for divider rate change to work. */
  115. pll1_sys_temp_enabled = true;
  116. clk_prepare_enable(pll1_sys_clk);
  117. }
  118. }
  119. /* Ensure the arm clock divider is what we expect */
  120. ret = clk_set_rate(arm_clk, new_freq * 1000);
  121. if (ret) {
  122. int ret1;
  123. dev_err(cpu_dev, "failed to set clock rate: %d\n", ret);
  124. ret1 = regulator_set_voltage_tol(arm_reg, volt_old, 0);
  125. if (ret1)
  126. dev_warn(cpu_dev,
  127. "failed to restore vddarm voltage: %d\n", ret1);
  128. return ret;
  129. }
  130. /* PLL1 is only needed until after ARM-PODF is set. */
  131. if (pll1_sys_temp_enabled)
  132. clk_disable_unprepare(pll1_sys_clk);
  133. /* scaling down? scale voltage after frequency */
  134. if (new_freq < old_freq) {
  135. ret = regulator_set_voltage_tol(arm_reg, volt, 0);
  136. if (ret) {
  137. dev_warn(cpu_dev,
  138. "failed to scale vddarm down: %d\n", ret);
  139. ret = 0;
  140. }
  141. ret = regulator_set_voltage_tol(soc_reg, imx6_soc_volt[index], 0);
  142. if (ret) {
  143. dev_warn(cpu_dev, "failed to scale vddsoc down: %d\n", ret);
  144. ret = 0;
  145. }
  146. if (!IS_ERR(pu_reg)) {
  147. ret = regulator_set_voltage_tol(pu_reg, imx6_soc_volt[index], 0);
  148. if (ret) {
  149. dev_warn(cpu_dev, "failed to scale vddpu down: %d\n", ret);
  150. ret = 0;
  151. }
  152. }
  153. }
  154. return 0;
  155. }
  156. static int imx6q_cpufreq_init(struct cpufreq_policy *policy)
  157. {
  158. int ret;
  159. policy->clk = arm_clk;
  160. ret = cpufreq_generic_init(policy, freq_table, transition_latency);
  161. policy->suspend_freq = policy->max;
  162. return ret;
  163. }
  164. static struct cpufreq_driver imx6q_cpufreq_driver = {
  165. .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  166. .verify = cpufreq_generic_frequency_table_verify,
  167. .target_index = imx6q_set_target,
  168. .get = cpufreq_generic_get,
  169. .init = imx6q_cpufreq_init,
  170. .name = "imx6q-cpufreq",
  171. .attr = cpufreq_generic_attr,
  172. .suspend = cpufreq_generic_suspend,
  173. };
  174. static int imx6q_cpufreq_probe(struct platform_device *pdev)
  175. {
  176. struct device_node *np;
  177. struct dev_pm_opp *opp;
  178. unsigned long min_volt, max_volt;
  179. int num, ret;
  180. const struct property *prop;
  181. const __be32 *val;
  182. u32 nr, i, j;
  183. cpu_dev = get_cpu_device(0);
  184. if (!cpu_dev) {
  185. pr_err("failed to get cpu0 device\n");
  186. return -ENODEV;
  187. }
  188. np = of_node_get(cpu_dev->of_node);
  189. if (!np) {
  190. dev_err(cpu_dev, "failed to find cpu0 node\n");
  191. return -ENOENT;
  192. }
  193. arm_clk = clk_get(cpu_dev, "arm");
  194. pll1_sys_clk = clk_get(cpu_dev, "pll1_sys");
  195. pll1_sw_clk = clk_get(cpu_dev, "pll1_sw");
  196. step_clk = clk_get(cpu_dev, "step");
  197. pll2_pfd2_396m_clk = clk_get(cpu_dev, "pll2_pfd2_396m");
  198. if (IS_ERR(arm_clk) || IS_ERR(pll1_sys_clk) || IS_ERR(pll1_sw_clk) ||
  199. IS_ERR(step_clk) || IS_ERR(pll2_pfd2_396m_clk)) {
  200. dev_err(cpu_dev, "failed to get clocks\n");
  201. ret = -ENOENT;
  202. goto put_clk;
  203. }
  204. if (of_machine_is_compatible("fsl,imx6ul") ||
  205. of_machine_is_compatible("fsl,imx6ull")) {
  206. pll2_bus_clk = clk_get(cpu_dev, "pll2_bus");
  207. secondary_sel_clk = clk_get(cpu_dev, "secondary_sel");
  208. if (IS_ERR(pll2_bus_clk) || IS_ERR(secondary_sel_clk)) {
  209. dev_err(cpu_dev, "failed to get clocks specific to imx6ul\n");
  210. ret = -ENOENT;
  211. goto put_clk;
  212. }
  213. }
  214. arm_reg = regulator_get(cpu_dev, "arm");
  215. pu_reg = regulator_get_optional(cpu_dev, "pu");
  216. soc_reg = regulator_get(cpu_dev, "soc");
  217. if (PTR_ERR(arm_reg) == -EPROBE_DEFER ||
  218. PTR_ERR(soc_reg) == -EPROBE_DEFER ||
  219. PTR_ERR(pu_reg) == -EPROBE_DEFER) {
  220. ret = -EPROBE_DEFER;
  221. dev_dbg(cpu_dev, "regulators not ready, defer\n");
  222. goto put_reg;
  223. }
  224. if (IS_ERR(arm_reg) || IS_ERR(soc_reg)) {
  225. dev_err(cpu_dev, "failed to get regulators\n");
  226. ret = -ENOENT;
  227. goto put_reg;
  228. }
  229. /*
  230. * We expect an OPP table supplied by platform.
  231. * Just, incase the platform did not supply the OPP
  232. * table, it will try to get it.
  233. */
  234. num = dev_pm_opp_get_opp_count(cpu_dev);
  235. if (num < 0) {
  236. ret = dev_pm_opp_of_add_table(cpu_dev);
  237. if (ret < 0) {
  238. dev_err(cpu_dev, "failed to init OPP table: %d\n", ret);
  239. goto put_reg;
  240. }
  241. /* Because we have added the OPPs here, we must free them */
  242. free_opp = true;
  243. num = dev_pm_opp_get_opp_count(cpu_dev);
  244. if (num < 0) {
  245. ret = num;
  246. dev_err(cpu_dev, "no OPP table is found: %d\n", ret);
  247. goto out_free_opp;
  248. }
  249. }
  250. ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
  251. if (ret) {
  252. dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
  253. goto out_free_opp;
  254. }
  255. /* Make imx6_soc_volt array's size same as arm opp number */
  256. imx6_soc_volt = devm_kzalloc(cpu_dev, sizeof(*imx6_soc_volt) * num, GFP_KERNEL);
  257. if (imx6_soc_volt == NULL) {
  258. ret = -ENOMEM;
  259. goto free_freq_table;
  260. }
  261. prop = of_find_property(np, "fsl,soc-operating-points", NULL);
  262. if (!prop || !prop->value)
  263. goto soc_opp_out;
  264. /*
  265. * Each OPP is a set of tuples consisting of frequency and
  266. * voltage like <freq-kHz vol-uV>.
  267. */
  268. nr = prop->length / sizeof(u32);
  269. if (nr % 2 || (nr / 2) < num)
  270. goto soc_opp_out;
  271. for (j = 0; j < num; j++) {
  272. val = prop->value;
  273. for (i = 0; i < nr / 2; i++) {
  274. unsigned long freq = be32_to_cpup(val++);
  275. unsigned long volt = be32_to_cpup(val++);
  276. if (freq_table[j].frequency == freq) {
  277. imx6_soc_volt[soc_opp_count++] = volt;
  278. break;
  279. }
  280. }
  281. }
  282. soc_opp_out:
  283. /* use fixed soc opp volt if no valid soc opp info found in dtb */
  284. if (soc_opp_count != num) {
  285. dev_warn(cpu_dev, "can NOT find valid fsl,soc-operating-points property in dtb, use default value!\n");
  286. for (j = 0; j < num; j++)
  287. imx6_soc_volt[j] = PU_SOC_VOLTAGE_NORMAL;
  288. if (freq_table[num - 1].frequency * 1000 == FREQ_1P2_GHZ)
  289. imx6_soc_volt[num - 1] = PU_SOC_VOLTAGE_HIGH;
  290. }
  291. if (of_property_read_u32(np, "clock-latency", &transition_latency))
  292. transition_latency = CPUFREQ_ETERNAL;
  293. /*
  294. * Calculate the ramp time for max voltage change in the
  295. * VDDSOC and VDDPU regulators.
  296. */
  297. ret = regulator_set_voltage_time(soc_reg, imx6_soc_volt[0], imx6_soc_volt[num - 1]);
  298. if (ret > 0)
  299. transition_latency += ret * 1000;
  300. if (!IS_ERR(pu_reg)) {
  301. ret = regulator_set_voltage_time(pu_reg, imx6_soc_volt[0], imx6_soc_volt[num - 1]);
  302. if (ret > 0)
  303. transition_latency += ret * 1000;
  304. }
  305. /*
  306. * OPP is maintained in order of increasing frequency, and
  307. * freq_table initialised from OPP is therefore sorted in the
  308. * same order.
  309. */
  310. opp = dev_pm_opp_find_freq_exact(cpu_dev,
  311. freq_table[0].frequency * 1000, true);
  312. min_volt = dev_pm_opp_get_voltage(opp);
  313. dev_pm_opp_put(opp);
  314. opp = dev_pm_opp_find_freq_exact(cpu_dev,
  315. freq_table[--num].frequency * 1000, true);
  316. max_volt = dev_pm_opp_get_voltage(opp);
  317. dev_pm_opp_put(opp);
  318. ret = regulator_set_voltage_time(arm_reg, min_volt, max_volt);
  319. if (ret > 0)
  320. transition_latency += ret * 1000;
  321. ret = cpufreq_register_driver(&imx6q_cpufreq_driver);
  322. if (ret) {
  323. dev_err(cpu_dev, "failed register driver: %d\n", ret);
  324. goto free_freq_table;
  325. }
  326. of_node_put(np);
  327. return 0;
  328. free_freq_table:
  329. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  330. out_free_opp:
  331. if (free_opp)
  332. dev_pm_opp_of_remove_table(cpu_dev);
  333. put_reg:
  334. if (!IS_ERR(arm_reg))
  335. regulator_put(arm_reg);
  336. if (!IS_ERR(pu_reg))
  337. regulator_put(pu_reg);
  338. if (!IS_ERR(soc_reg))
  339. regulator_put(soc_reg);
  340. put_clk:
  341. if (!IS_ERR(arm_clk))
  342. clk_put(arm_clk);
  343. if (!IS_ERR(pll1_sys_clk))
  344. clk_put(pll1_sys_clk);
  345. if (!IS_ERR(pll1_sw_clk))
  346. clk_put(pll1_sw_clk);
  347. if (!IS_ERR(step_clk))
  348. clk_put(step_clk);
  349. if (!IS_ERR(pll2_pfd2_396m_clk))
  350. clk_put(pll2_pfd2_396m_clk);
  351. if (!IS_ERR(pll2_bus_clk))
  352. clk_put(pll2_bus_clk);
  353. if (!IS_ERR(secondary_sel_clk))
  354. clk_put(secondary_sel_clk);
  355. of_node_put(np);
  356. return ret;
  357. }
  358. static int imx6q_cpufreq_remove(struct platform_device *pdev)
  359. {
  360. cpufreq_unregister_driver(&imx6q_cpufreq_driver);
  361. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  362. if (free_opp)
  363. dev_pm_opp_of_remove_table(cpu_dev);
  364. regulator_put(arm_reg);
  365. if (!IS_ERR(pu_reg))
  366. regulator_put(pu_reg);
  367. regulator_put(soc_reg);
  368. clk_put(arm_clk);
  369. clk_put(pll1_sys_clk);
  370. clk_put(pll1_sw_clk);
  371. clk_put(step_clk);
  372. clk_put(pll2_pfd2_396m_clk);
  373. clk_put(pll2_bus_clk);
  374. clk_put(secondary_sel_clk);
  375. return 0;
  376. }
  377. static struct platform_driver imx6q_cpufreq_platdrv = {
  378. .driver = {
  379. .name = "imx6q-cpufreq",
  380. },
  381. .probe = imx6q_cpufreq_probe,
  382. .remove = imx6q_cpufreq_remove,
  383. };
  384. module_platform_driver(imx6q_cpufreq_platdrv);
  385. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  386. MODULE_DESCRIPTION("Freescale i.MX6Q cpufreq driver");
  387. MODULE_LICENSE("GPL");