imx6q-cpufreq.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. static struct device *cpu_dev;
  29. static struct cpufreq_frequency_table *freq_table;
  30. static unsigned int transition_latency;
  31. static u32 *imx6_soc_volt;
  32. static u32 soc_opp_count;
  33. static int imx6q_set_target(struct cpufreq_policy *policy, unsigned int index)
  34. {
  35. struct dev_pm_opp *opp;
  36. unsigned long freq_hz, volt, volt_old;
  37. unsigned int old_freq, new_freq;
  38. int ret;
  39. new_freq = freq_table[index].frequency;
  40. freq_hz = new_freq * 1000;
  41. old_freq = clk_get_rate(arm_clk) / 1000;
  42. rcu_read_lock();
  43. opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz);
  44. if (IS_ERR(opp)) {
  45. rcu_read_unlock();
  46. dev_err(cpu_dev, "failed to find OPP for %ld\n", freq_hz);
  47. return PTR_ERR(opp);
  48. }
  49. volt = dev_pm_opp_get_voltage(opp);
  50. rcu_read_unlock();
  51. volt_old = regulator_get_voltage(arm_reg);
  52. dev_dbg(cpu_dev, "%u MHz, %ld mV --> %u MHz, %ld mV\n",
  53. old_freq / 1000, volt_old / 1000,
  54. new_freq / 1000, volt / 1000);
  55. /* scaling up? scale voltage before frequency */
  56. if (new_freq > old_freq) {
  57. ret = regulator_set_voltage_tol(pu_reg, imx6_soc_volt[index], 0);
  58. if (ret) {
  59. dev_err(cpu_dev, "failed to scale vddpu up: %d\n", ret);
  60. return ret;
  61. }
  62. ret = regulator_set_voltage_tol(soc_reg, imx6_soc_volt[index], 0);
  63. if (ret) {
  64. dev_err(cpu_dev, "failed to scale vddsoc up: %d\n", ret);
  65. return ret;
  66. }
  67. ret = regulator_set_voltage_tol(arm_reg, volt, 0);
  68. if (ret) {
  69. dev_err(cpu_dev,
  70. "failed to scale vddarm up: %d\n", ret);
  71. return ret;
  72. }
  73. }
  74. /*
  75. * The setpoints are selected per PLL/PDF frequencies, so we need to
  76. * reprogram PLL for frequency scaling. The procedure of reprogramming
  77. * PLL1 is as below.
  78. *
  79. * - Enable pll2_pfd2_396m_clk and reparent pll1_sw_clk to it
  80. * - Reprogram pll1_sys_clk and reparent pll1_sw_clk back to it
  81. * - Disable pll2_pfd2_396m_clk
  82. */
  83. clk_set_parent(step_clk, pll2_pfd2_396m_clk);
  84. clk_set_parent(pll1_sw_clk, step_clk);
  85. if (freq_hz > clk_get_rate(pll2_pfd2_396m_clk)) {
  86. clk_set_rate(pll1_sys_clk, new_freq * 1000);
  87. clk_set_parent(pll1_sw_clk, pll1_sys_clk);
  88. }
  89. /* Ensure the arm clock divider is what we expect */
  90. ret = clk_set_rate(arm_clk, new_freq * 1000);
  91. if (ret) {
  92. dev_err(cpu_dev, "failed to set clock rate: %d\n", ret);
  93. regulator_set_voltage_tol(arm_reg, volt_old, 0);
  94. return ret;
  95. }
  96. /* scaling down? scale voltage after frequency */
  97. if (new_freq < old_freq) {
  98. ret = regulator_set_voltage_tol(arm_reg, volt, 0);
  99. if (ret) {
  100. dev_warn(cpu_dev,
  101. "failed to scale vddarm down: %d\n", ret);
  102. ret = 0;
  103. }
  104. ret = regulator_set_voltage_tol(soc_reg, imx6_soc_volt[index], 0);
  105. if (ret) {
  106. dev_warn(cpu_dev, "failed to scale vddsoc down: %d\n", ret);
  107. ret = 0;
  108. }
  109. ret = regulator_set_voltage_tol(pu_reg, imx6_soc_volt[index], 0);
  110. if (ret) {
  111. dev_warn(cpu_dev, "failed to scale vddpu down: %d\n", ret);
  112. ret = 0;
  113. }
  114. }
  115. return 0;
  116. }
  117. static int imx6q_cpufreq_init(struct cpufreq_policy *policy)
  118. {
  119. policy->clk = arm_clk;
  120. return cpufreq_generic_init(policy, freq_table, transition_latency);
  121. }
  122. static struct cpufreq_driver imx6q_cpufreq_driver = {
  123. .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  124. .verify = cpufreq_generic_frequency_table_verify,
  125. .target_index = imx6q_set_target,
  126. .get = cpufreq_generic_get,
  127. .init = imx6q_cpufreq_init,
  128. .name = "imx6q-cpufreq",
  129. .attr = cpufreq_generic_attr,
  130. };
  131. static int imx6q_cpufreq_probe(struct platform_device *pdev)
  132. {
  133. struct device_node *np;
  134. struct dev_pm_opp *opp;
  135. unsigned long min_volt, max_volt;
  136. int num, ret;
  137. const struct property *prop;
  138. const __be32 *val;
  139. u32 nr, i, j;
  140. cpu_dev = get_cpu_device(0);
  141. if (!cpu_dev) {
  142. pr_err("failed to get cpu0 device\n");
  143. return -ENODEV;
  144. }
  145. np = of_node_get(cpu_dev->of_node);
  146. if (!np) {
  147. dev_err(cpu_dev, "failed to find cpu0 node\n");
  148. return -ENOENT;
  149. }
  150. arm_clk = clk_get(cpu_dev, "arm");
  151. pll1_sys_clk = clk_get(cpu_dev, "pll1_sys");
  152. pll1_sw_clk = clk_get(cpu_dev, "pll1_sw");
  153. step_clk = clk_get(cpu_dev, "step");
  154. pll2_pfd2_396m_clk = clk_get(cpu_dev, "pll2_pfd2_396m");
  155. if (IS_ERR(arm_clk) || IS_ERR(pll1_sys_clk) || IS_ERR(pll1_sw_clk) ||
  156. IS_ERR(step_clk) || IS_ERR(pll2_pfd2_396m_clk)) {
  157. dev_err(cpu_dev, "failed to get clocks\n");
  158. ret = -ENOENT;
  159. goto put_clk;
  160. }
  161. arm_reg = regulator_get(cpu_dev, "arm");
  162. pu_reg = regulator_get(cpu_dev, "pu");
  163. soc_reg = regulator_get(cpu_dev, "soc");
  164. if (IS_ERR(arm_reg) || IS_ERR(pu_reg) || IS_ERR(soc_reg)) {
  165. dev_err(cpu_dev, "failed to get regulators\n");
  166. ret = -ENOENT;
  167. goto put_reg;
  168. }
  169. /*
  170. * We expect an OPP table supplied by platform.
  171. * Just, incase the platform did not supply the OPP
  172. * table, it will try to get it.
  173. */
  174. num = dev_pm_opp_get_opp_count(cpu_dev);
  175. if (num < 0) {
  176. ret = of_init_opp_table(cpu_dev);
  177. if (ret < 0) {
  178. dev_err(cpu_dev, "failed to init OPP table: %d\n", ret);
  179. goto put_reg;
  180. }
  181. num = dev_pm_opp_get_opp_count(cpu_dev);
  182. if (num < 0) {
  183. ret = num;
  184. dev_err(cpu_dev, "no OPP table is found: %d\n", ret);
  185. goto put_reg;
  186. }
  187. }
  188. ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
  189. if (ret) {
  190. dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
  191. goto put_reg;
  192. }
  193. /* Make imx6_soc_volt array's size same as arm opp number */
  194. imx6_soc_volt = devm_kzalloc(cpu_dev, sizeof(*imx6_soc_volt) * num, GFP_KERNEL);
  195. if (imx6_soc_volt == NULL) {
  196. ret = -ENOMEM;
  197. goto free_freq_table;
  198. }
  199. prop = of_find_property(np, "fsl,soc-operating-points", NULL);
  200. if (!prop || !prop->value)
  201. goto soc_opp_out;
  202. /*
  203. * Each OPP is a set of tuples consisting of frequency and
  204. * voltage like <freq-kHz vol-uV>.
  205. */
  206. nr = prop->length / sizeof(u32);
  207. if (nr % 2 || (nr / 2) < num)
  208. goto soc_opp_out;
  209. for (j = 0; j < num; j++) {
  210. val = prop->value;
  211. for (i = 0; i < nr / 2; i++) {
  212. unsigned long freq = be32_to_cpup(val++);
  213. unsigned long volt = be32_to_cpup(val++);
  214. if (freq_table[j].frequency == freq) {
  215. imx6_soc_volt[soc_opp_count++] = volt;
  216. break;
  217. }
  218. }
  219. }
  220. soc_opp_out:
  221. /* use fixed soc opp volt if no valid soc opp info found in dtb */
  222. if (soc_opp_count != num) {
  223. dev_warn(cpu_dev, "can NOT find valid fsl,soc-operating-points property in dtb, use default value!\n");
  224. for (j = 0; j < num; j++)
  225. imx6_soc_volt[j] = PU_SOC_VOLTAGE_NORMAL;
  226. if (freq_table[num - 1].frequency * 1000 == FREQ_1P2_GHZ)
  227. imx6_soc_volt[num - 1] = PU_SOC_VOLTAGE_HIGH;
  228. }
  229. if (of_property_read_u32(np, "clock-latency", &transition_latency))
  230. transition_latency = CPUFREQ_ETERNAL;
  231. /*
  232. * Calculate the ramp time for max voltage change in the
  233. * VDDSOC and VDDPU regulators.
  234. */
  235. ret = regulator_set_voltage_time(soc_reg, imx6_soc_volt[0], imx6_soc_volt[num - 1]);
  236. if (ret > 0)
  237. transition_latency += ret * 1000;
  238. ret = regulator_set_voltage_time(pu_reg, imx6_soc_volt[0], imx6_soc_volt[num - 1]);
  239. if (ret > 0)
  240. transition_latency += ret * 1000;
  241. /*
  242. * OPP is maintained in order of increasing frequency, and
  243. * freq_table initialised from OPP is therefore sorted in the
  244. * same order.
  245. */
  246. rcu_read_lock();
  247. opp = dev_pm_opp_find_freq_exact(cpu_dev,
  248. freq_table[0].frequency * 1000, true);
  249. min_volt = dev_pm_opp_get_voltage(opp);
  250. opp = dev_pm_opp_find_freq_exact(cpu_dev,
  251. freq_table[--num].frequency * 1000, true);
  252. max_volt = dev_pm_opp_get_voltage(opp);
  253. rcu_read_unlock();
  254. ret = regulator_set_voltage_time(arm_reg, min_volt, max_volt);
  255. if (ret > 0)
  256. transition_latency += ret * 1000;
  257. ret = cpufreq_register_driver(&imx6q_cpufreq_driver);
  258. if (ret) {
  259. dev_err(cpu_dev, "failed register driver: %d\n", ret);
  260. goto free_freq_table;
  261. }
  262. of_node_put(np);
  263. return 0;
  264. free_freq_table:
  265. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  266. put_reg:
  267. if (!IS_ERR(arm_reg))
  268. regulator_put(arm_reg);
  269. if (!IS_ERR(pu_reg))
  270. regulator_put(pu_reg);
  271. if (!IS_ERR(soc_reg))
  272. regulator_put(soc_reg);
  273. put_clk:
  274. if (!IS_ERR(arm_clk))
  275. clk_put(arm_clk);
  276. if (!IS_ERR(pll1_sys_clk))
  277. clk_put(pll1_sys_clk);
  278. if (!IS_ERR(pll1_sw_clk))
  279. clk_put(pll1_sw_clk);
  280. if (!IS_ERR(step_clk))
  281. clk_put(step_clk);
  282. if (!IS_ERR(pll2_pfd2_396m_clk))
  283. clk_put(pll2_pfd2_396m_clk);
  284. of_node_put(np);
  285. return ret;
  286. }
  287. static int imx6q_cpufreq_remove(struct platform_device *pdev)
  288. {
  289. cpufreq_unregister_driver(&imx6q_cpufreq_driver);
  290. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  291. regulator_put(arm_reg);
  292. regulator_put(pu_reg);
  293. regulator_put(soc_reg);
  294. clk_put(arm_clk);
  295. clk_put(pll1_sys_clk);
  296. clk_put(pll1_sw_clk);
  297. clk_put(step_clk);
  298. clk_put(pll2_pfd2_396m_clk);
  299. return 0;
  300. }
  301. static struct platform_driver imx6q_cpufreq_platdrv = {
  302. .driver = {
  303. .name = "imx6q-cpufreq",
  304. .owner = THIS_MODULE,
  305. },
  306. .probe = imx6q_cpufreq_probe,
  307. .remove = imx6q_cpufreq_remove,
  308. };
  309. module_platform_driver(imx6q_cpufreq_platdrv);
  310. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  311. MODULE_DESCRIPTION("Freescale i.MX6Q cpufreq driver");
  312. MODULE_LICENSE("GPL");