imx6q-cpufreq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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/of_address.h>
  15. #include <linux/pm_opp.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regulator/consumer.h>
  18. #define PU_SOC_VOLTAGE_NORMAL 1250000
  19. #define PU_SOC_VOLTAGE_HIGH 1275000
  20. #define FREQ_1P2_GHZ 1200000000
  21. static struct regulator *arm_reg;
  22. static struct regulator *pu_reg;
  23. static struct regulator *soc_reg;
  24. enum IMX6_CPUFREQ_CLKS {
  25. ARM,
  26. PLL1_SYS,
  27. STEP,
  28. PLL1_SW,
  29. PLL2_PFD2_396M,
  30. /* MX6UL requires two more clks */
  31. PLL2_BUS,
  32. SECONDARY_SEL,
  33. };
  34. #define IMX6Q_CPUFREQ_CLK_NUM 5
  35. #define IMX6UL_CPUFREQ_CLK_NUM 7
  36. static int num_clks;
  37. static struct clk_bulk_data clks[] = {
  38. { .id = "arm" },
  39. { .id = "pll1_sys" },
  40. { .id = "step" },
  41. { .id = "pll1_sw" },
  42. { .id = "pll2_pfd2_396m" },
  43. { .id = "pll2_bus" },
  44. { .id = "secondary_sel" },
  45. };
  46. static struct device *cpu_dev;
  47. static bool free_opp;
  48. static struct cpufreq_frequency_table *freq_table;
  49. static unsigned int max_freq;
  50. static unsigned int transition_latency;
  51. static u32 *imx6_soc_volt;
  52. static u32 soc_opp_count;
  53. static int imx6q_set_target(struct cpufreq_policy *policy, unsigned int index)
  54. {
  55. struct dev_pm_opp *opp;
  56. unsigned long freq_hz, volt, volt_old;
  57. unsigned int old_freq, new_freq;
  58. bool pll1_sys_temp_enabled = false;
  59. int ret;
  60. new_freq = freq_table[index].frequency;
  61. freq_hz = new_freq * 1000;
  62. old_freq = clk_get_rate(clks[ARM].clk) / 1000;
  63. opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz);
  64. if (IS_ERR(opp)) {
  65. dev_err(cpu_dev, "failed to find OPP for %ld\n", freq_hz);
  66. return PTR_ERR(opp);
  67. }
  68. volt = dev_pm_opp_get_voltage(opp);
  69. dev_pm_opp_put(opp);
  70. volt_old = regulator_get_voltage(arm_reg);
  71. dev_dbg(cpu_dev, "%u MHz, %ld mV --> %u MHz, %ld mV\n",
  72. old_freq / 1000, volt_old / 1000,
  73. new_freq / 1000, volt / 1000);
  74. /* scaling up? scale voltage before frequency */
  75. if (new_freq > old_freq) {
  76. if (!IS_ERR(pu_reg)) {
  77. ret = regulator_set_voltage_tol(pu_reg, imx6_soc_volt[index], 0);
  78. if (ret) {
  79. dev_err(cpu_dev, "failed to scale vddpu up: %d\n", ret);
  80. return ret;
  81. }
  82. }
  83. ret = regulator_set_voltage_tol(soc_reg, imx6_soc_volt[index], 0);
  84. if (ret) {
  85. dev_err(cpu_dev, "failed to scale vddsoc up: %d\n", ret);
  86. return ret;
  87. }
  88. ret = regulator_set_voltage_tol(arm_reg, volt, 0);
  89. if (ret) {
  90. dev_err(cpu_dev,
  91. "failed to scale vddarm up: %d\n", ret);
  92. return ret;
  93. }
  94. }
  95. /*
  96. * The setpoints are selected per PLL/PDF frequencies, so we need to
  97. * reprogram PLL for frequency scaling. The procedure of reprogramming
  98. * PLL1 is as below.
  99. * For i.MX6UL, it has a secondary clk mux, the cpu frequency change
  100. * flow is slightly different from other i.MX6 OSC.
  101. * The cpu frequeny change flow for i.MX6(except i.MX6UL) is as below:
  102. * - Enable pll2_pfd2_396m_clk and reparent pll1_sw_clk to it
  103. * - Reprogram pll1_sys_clk and reparent pll1_sw_clk back to it
  104. * - Disable pll2_pfd2_396m_clk
  105. */
  106. if (of_machine_is_compatible("fsl,imx6ul") ||
  107. of_machine_is_compatible("fsl,imx6ull")) {
  108. /*
  109. * When changing pll1_sw_clk's parent to pll1_sys_clk,
  110. * CPU may run at higher than 528MHz, this will lead to
  111. * the system unstable if the voltage is lower than the
  112. * voltage of 528MHz, so lower the CPU frequency to one
  113. * half before changing CPU frequency.
  114. */
  115. clk_set_rate(clks[ARM].clk, (old_freq >> 1) * 1000);
  116. clk_set_parent(clks[PLL1_SW].clk, clks[PLL1_SYS].clk);
  117. if (freq_hz > clk_get_rate(clks[PLL2_PFD2_396M].clk))
  118. clk_set_parent(clks[SECONDARY_SEL].clk,
  119. clks[PLL2_BUS].clk);
  120. else
  121. clk_set_parent(clks[SECONDARY_SEL].clk,
  122. clks[PLL2_PFD2_396M].clk);
  123. clk_set_parent(clks[STEP].clk, clks[SECONDARY_SEL].clk);
  124. clk_set_parent(clks[PLL1_SW].clk, clks[STEP].clk);
  125. if (freq_hz > clk_get_rate(clks[PLL2_BUS].clk)) {
  126. clk_set_rate(clks[PLL1_SYS].clk, new_freq * 1000);
  127. clk_set_parent(clks[PLL1_SW].clk, clks[PLL1_SYS].clk);
  128. }
  129. } else {
  130. clk_set_parent(clks[STEP].clk, clks[PLL2_PFD2_396M].clk);
  131. clk_set_parent(clks[PLL1_SW].clk, clks[STEP].clk);
  132. if (freq_hz > clk_get_rate(clks[PLL2_PFD2_396M].clk)) {
  133. clk_set_rate(clks[PLL1_SYS].clk, new_freq * 1000);
  134. clk_set_parent(clks[PLL1_SW].clk, clks[PLL1_SYS].clk);
  135. } else {
  136. /* pll1_sys needs to be enabled for divider rate change to work. */
  137. pll1_sys_temp_enabled = true;
  138. clk_prepare_enable(clks[PLL1_SYS].clk);
  139. }
  140. }
  141. /* Ensure the arm clock divider is what we expect */
  142. ret = clk_set_rate(clks[ARM].clk, new_freq * 1000);
  143. if (ret) {
  144. dev_err(cpu_dev, "failed to set clock rate: %d\n", ret);
  145. regulator_set_voltage_tol(arm_reg, volt_old, 0);
  146. return ret;
  147. }
  148. /* PLL1 is only needed until after ARM-PODF is set. */
  149. if (pll1_sys_temp_enabled)
  150. clk_disable_unprepare(clks[PLL1_SYS].clk);
  151. /* scaling down? scale voltage after frequency */
  152. if (new_freq < old_freq) {
  153. ret = regulator_set_voltage_tol(arm_reg, volt, 0);
  154. if (ret) {
  155. dev_warn(cpu_dev,
  156. "failed to scale vddarm down: %d\n", ret);
  157. ret = 0;
  158. }
  159. ret = regulator_set_voltage_tol(soc_reg, imx6_soc_volt[index], 0);
  160. if (ret) {
  161. dev_warn(cpu_dev, "failed to scale vddsoc down: %d\n", ret);
  162. ret = 0;
  163. }
  164. if (!IS_ERR(pu_reg)) {
  165. ret = regulator_set_voltage_tol(pu_reg, imx6_soc_volt[index], 0);
  166. if (ret) {
  167. dev_warn(cpu_dev, "failed to scale vddpu down: %d\n", ret);
  168. ret = 0;
  169. }
  170. }
  171. }
  172. return 0;
  173. }
  174. static int imx6q_cpufreq_init(struct cpufreq_policy *policy)
  175. {
  176. int ret;
  177. policy->clk = clks[ARM].clk;
  178. ret = cpufreq_generic_init(policy, freq_table, transition_latency);
  179. policy->suspend_freq = max_freq;
  180. return ret;
  181. }
  182. static struct cpufreq_driver imx6q_cpufreq_driver = {
  183. .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  184. .verify = cpufreq_generic_frequency_table_verify,
  185. .target_index = imx6q_set_target,
  186. .get = cpufreq_generic_get,
  187. .init = imx6q_cpufreq_init,
  188. .name = "imx6q-cpufreq",
  189. .attr = cpufreq_generic_attr,
  190. .suspend = cpufreq_generic_suspend,
  191. };
  192. #define OCOTP_CFG3 0x440
  193. #define OCOTP_CFG3_SPEED_SHIFT 16
  194. #define OCOTP_CFG3_SPEED_1P2GHZ 0x3
  195. #define OCOTP_CFG3_SPEED_996MHZ 0x2
  196. #define OCOTP_CFG3_SPEED_852MHZ 0x1
  197. static void imx6q_opp_check_speed_grading(struct device *dev)
  198. {
  199. struct device_node *np;
  200. void __iomem *base;
  201. u32 val;
  202. np = of_find_compatible_node(NULL, NULL, "fsl,imx6q-ocotp");
  203. if (!np)
  204. return;
  205. base = of_iomap(np, 0);
  206. if (!base) {
  207. dev_err(dev, "failed to map ocotp\n");
  208. goto put_node;
  209. }
  210. /*
  211. * SPEED_GRADING[1:0] defines the max speed of ARM:
  212. * 2b'11: 1200000000Hz;
  213. * 2b'10: 996000000Hz;
  214. * 2b'01: 852000000Hz; -- i.MX6Q Only, exclusive with 996MHz.
  215. * 2b'00: 792000000Hz;
  216. * We need to set the max speed of ARM according to fuse map.
  217. */
  218. val = readl_relaxed(base + OCOTP_CFG3);
  219. val >>= OCOTP_CFG3_SPEED_SHIFT;
  220. val &= 0x3;
  221. if (val < OCOTP_CFG3_SPEED_996MHZ)
  222. if (dev_pm_opp_disable(dev, 996000000))
  223. dev_warn(dev, "failed to disable 996MHz OPP\n");
  224. if (of_machine_is_compatible("fsl,imx6q") ||
  225. of_machine_is_compatible("fsl,imx6qp")) {
  226. if (val != OCOTP_CFG3_SPEED_852MHZ)
  227. if (dev_pm_opp_disable(dev, 852000000))
  228. dev_warn(dev, "failed to disable 852MHz OPP\n");
  229. if (val != OCOTP_CFG3_SPEED_1P2GHZ)
  230. if (dev_pm_opp_disable(dev, 1200000000))
  231. dev_warn(dev, "failed to disable 1.2GHz OPP\n");
  232. }
  233. iounmap(base);
  234. put_node:
  235. of_node_put(np);
  236. }
  237. #define OCOTP_CFG3_6UL_SPEED_696MHZ 0x2
  238. static void imx6ul_opp_check_speed_grading(struct device *dev)
  239. {
  240. struct device_node *np;
  241. void __iomem *base;
  242. u32 val;
  243. np = of_find_compatible_node(NULL, NULL, "fsl,imx6ul-ocotp");
  244. if (!np)
  245. return;
  246. base = of_iomap(np, 0);
  247. if (!base) {
  248. dev_err(dev, "failed to map ocotp\n");
  249. goto put_node;
  250. }
  251. /*
  252. * Speed GRADING[1:0] defines the max speed of ARM:
  253. * 2b'00: Reserved;
  254. * 2b'01: 528000000Hz;
  255. * 2b'10: 696000000Hz;
  256. * 2b'11: Reserved;
  257. * We need to set the max speed of ARM according to fuse map.
  258. */
  259. val = readl_relaxed(base + OCOTP_CFG3);
  260. val >>= OCOTP_CFG3_SPEED_SHIFT;
  261. val &= 0x3;
  262. if (val != OCOTP_CFG3_6UL_SPEED_696MHZ)
  263. if (dev_pm_opp_disable(dev, 696000000))
  264. dev_warn(dev, "failed to disable 696MHz OPP\n");
  265. iounmap(base);
  266. put_node:
  267. of_node_put(np);
  268. }
  269. static int imx6q_cpufreq_probe(struct platform_device *pdev)
  270. {
  271. struct device_node *np;
  272. struct dev_pm_opp *opp;
  273. unsigned long min_volt, max_volt;
  274. int num, ret;
  275. const struct property *prop;
  276. const __be32 *val;
  277. u32 nr, i, j;
  278. cpu_dev = get_cpu_device(0);
  279. if (!cpu_dev) {
  280. pr_err("failed to get cpu0 device\n");
  281. return -ENODEV;
  282. }
  283. np = of_node_get(cpu_dev->of_node);
  284. if (!np) {
  285. dev_err(cpu_dev, "failed to find cpu0 node\n");
  286. return -ENOENT;
  287. }
  288. if (of_machine_is_compatible("fsl,imx6ul") ||
  289. of_machine_is_compatible("fsl,imx6ull"))
  290. num_clks = IMX6UL_CPUFREQ_CLK_NUM;
  291. else
  292. num_clks = IMX6Q_CPUFREQ_CLK_NUM;
  293. ret = clk_bulk_get(cpu_dev, num_clks, clks);
  294. if (ret)
  295. goto put_node;
  296. arm_reg = regulator_get(cpu_dev, "arm");
  297. pu_reg = regulator_get_optional(cpu_dev, "pu");
  298. soc_reg = regulator_get(cpu_dev, "soc");
  299. if (PTR_ERR(arm_reg) == -EPROBE_DEFER ||
  300. PTR_ERR(soc_reg) == -EPROBE_DEFER ||
  301. PTR_ERR(pu_reg) == -EPROBE_DEFER) {
  302. ret = -EPROBE_DEFER;
  303. dev_dbg(cpu_dev, "regulators not ready, defer\n");
  304. goto put_reg;
  305. }
  306. if (IS_ERR(arm_reg) || IS_ERR(soc_reg)) {
  307. dev_err(cpu_dev, "failed to get regulators\n");
  308. ret = -ENOENT;
  309. goto put_reg;
  310. }
  311. ret = dev_pm_opp_of_add_table(cpu_dev);
  312. if (ret < 0) {
  313. dev_err(cpu_dev, "failed to init OPP table: %d\n", ret);
  314. goto put_reg;
  315. }
  316. if (of_machine_is_compatible("fsl,imx6ul"))
  317. imx6ul_opp_check_speed_grading(cpu_dev);
  318. else
  319. imx6q_opp_check_speed_grading(cpu_dev);
  320. /* Because we have added the OPPs here, we must free them */
  321. free_opp = true;
  322. num = dev_pm_opp_get_opp_count(cpu_dev);
  323. if (num < 0) {
  324. ret = num;
  325. dev_err(cpu_dev, "no OPP table is found: %d\n", ret);
  326. goto out_free_opp;
  327. }
  328. ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
  329. if (ret) {
  330. dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
  331. goto out_free_opp;
  332. }
  333. /* Make imx6_soc_volt array's size same as arm opp number */
  334. imx6_soc_volt = devm_kzalloc(cpu_dev, sizeof(*imx6_soc_volt) * num, GFP_KERNEL);
  335. if (imx6_soc_volt == NULL) {
  336. ret = -ENOMEM;
  337. goto free_freq_table;
  338. }
  339. prop = of_find_property(np, "fsl,soc-operating-points", NULL);
  340. if (!prop || !prop->value)
  341. goto soc_opp_out;
  342. /*
  343. * Each OPP is a set of tuples consisting of frequency and
  344. * voltage like <freq-kHz vol-uV>.
  345. */
  346. nr = prop->length / sizeof(u32);
  347. if (nr % 2 || (nr / 2) < num)
  348. goto soc_opp_out;
  349. for (j = 0; j < num; j++) {
  350. val = prop->value;
  351. for (i = 0; i < nr / 2; i++) {
  352. unsigned long freq = be32_to_cpup(val++);
  353. unsigned long volt = be32_to_cpup(val++);
  354. if (freq_table[j].frequency == freq) {
  355. imx6_soc_volt[soc_opp_count++] = volt;
  356. break;
  357. }
  358. }
  359. }
  360. soc_opp_out:
  361. /* use fixed soc opp volt if no valid soc opp info found in dtb */
  362. if (soc_opp_count != num) {
  363. dev_warn(cpu_dev, "can NOT find valid fsl,soc-operating-points property in dtb, use default value!\n");
  364. for (j = 0; j < num; j++)
  365. imx6_soc_volt[j] = PU_SOC_VOLTAGE_NORMAL;
  366. if (freq_table[num - 1].frequency * 1000 == FREQ_1P2_GHZ)
  367. imx6_soc_volt[num - 1] = PU_SOC_VOLTAGE_HIGH;
  368. }
  369. if (of_property_read_u32(np, "clock-latency", &transition_latency))
  370. transition_latency = CPUFREQ_ETERNAL;
  371. /*
  372. * Calculate the ramp time for max voltage change in the
  373. * VDDSOC and VDDPU regulators.
  374. */
  375. ret = regulator_set_voltage_time(soc_reg, imx6_soc_volt[0], imx6_soc_volt[num - 1]);
  376. if (ret > 0)
  377. transition_latency += ret * 1000;
  378. if (!IS_ERR(pu_reg)) {
  379. ret = regulator_set_voltage_time(pu_reg, imx6_soc_volt[0], imx6_soc_volt[num - 1]);
  380. if (ret > 0)
  381. transition_latency += ret * 1000;
  382. }
  383. /*
  384. * OPP is maintained in order of increasing frequency, and
  385. * freq_table initialised from OPP is therefore sorted in the
  386. * same order.
  387. */
  388. max_freq = freq_table[--num].frequency;
  389. opp = dev_pm_opp_find_freq_exact(cpu_dev,
  390. freq_table[0].frequency * 1000, true);
  391. min_volt = dev_pm_opp_get_voltage(opp);
  392. dev_pm_opp_put(opp);
  393. opp = dev_pm_opp_find_freq_exact(cpu_dev, max_freq * 1000, true);
  394. max_volt = dev_pm_opp_get_voltage(opp);
  395. dev_pm_opp_put(opp);
  396. ret = regulator_set_voltage_time(arm_reg, min_volt, max_volt);
  397. if (ret > 0)
  398. transition_latency += ret * 1000;
  399. ret = cpufreq_register_driver(&imx6q_cpufreq_driver);
  400. if (ret) {
  401. dev_err(cpu_dev, "failed register driver: %d\n", ret);
  402. goto free_freq_table;
  403. }
  404. of_node_put(np);
  405. return 0;
  406. free_freq_table:
  407. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  408. out_free_opp:
  409. if (free_opp)
  410. dev_pm_opp_of_remove_table(cpu_dev);
  411. put_reg:
  412. if (!IS_ERR(arm_reg))
  413. regulator_put(arm_reg);
  414. if (!IS_ERR(pu_reg))
  415. regulator_put(pu_reg);
  416. if (!IS_ERR(soc_reg))
  417. regulator_put(soc_reg);
  418. clk_bulk_put(num_clks, clks);
  419. put_node:
  420. of_node_put(np);
  421. return ret;
  422. }
  423. static int imx6q_cpufreq_remove(struct platform_device *pdev)
  424. {
  425. cpufreq_unregister_driver(&imx6q_cpufreq_driver);
  426. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  427. if (free_opp)
  428. dev_pm_opp_of_remove_table(cpu_dev);
  429. regulator_put(arm_reg);
  430. if (!IS_ERR(pu_reg))
  431. regulator_put(pu_reg);
  432. regulator_put(soc_reg);
  433. clk_bulk_put(num_clks, clks);
  434. return 0;
  435. }
  436. static struct platform_driver imx6q_cpufreq_platdrv = {
  437. .driver = {
  438. .name = "imx6q-cpufreq",
  439. },
  440. .probe = imx6q_cpufreq_probe,
  441. .remove = imx6q_cpufreq_remove,
  442. };
  443. module_platform_driver(imx6q_cpufreq_platdrv);
  444. MODULE_ALIAS("platform:imx6q-cpufreq");
  445. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  446. MODULE_DESCRIPTION("Freescale i.MX6Q cpufreq driver");
  447. MODULE_LICENSE("GPL");