imx6q-cpufreq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. #define OCOTP_CFG3_6ULL_SPEED_792MHZ 0x2
  239. #define OCOTP_CFG3_6ULL_SPEED_900MHZ 0x3
  240. static void imx6ul_opp_check_speed_grading(struct device *dev)
  241. {
  242. struct device_node *np;
  243. void __iomem *base;
  244. u32 val;
  245. np = of_find_compatible_node(NULL, NULL, "fsl,imx6ul-ocotp");
  246. if (!np)
  247. return;
  248. base = of_iomap(np, 0);
  249. if (!base) {
  250. dev_err(dev, "failed to map ocotp\n");
  251. goto put_node;
  252. }
  253. /*
  254. * Speed GRADING[1:0] defines the max speed of ARM:
  255. * 2b'00: Reserved;
  256. * 2b'01: 528000000Hz;
  257. * 2b'10: 696000000Hz on i.MX6UL, 792000000Hz on i.MX6ULL;
  258. * 2b'11: 900000000Hz on i.MX6ULL only;
  259. * We need to set the max speed of ARM according to fuse map.
  260. */
  261. val = readl_relaxed(base + OCOTP_CFG3);
  262. val >>= OCOTP_CFG3_SPEED_SHIFT;
  263. val &= 0x3;
  264. if (of_machine_is_compatible("fsl,imx6ul")) {
  265. if (val != OCOTP_CFG3_6UL_SPEED_696MHZ)
  266. if (dev_pm_opp_disable(dev, 696000000))
  267. dev_warn(dev, "failed to disable 696MHz OPP\n");
  268. }
  269. if (of_machine_is_compatible("fsl,imx6ull")) {
  270. if (val != OCOTP_CFG3_6ULL_SPEED_792MHZ)
  271. if (dev_pm_opp_disable(dev, 792000000))
  272. dev_warn(dev, "failed to disable 792MHz OPP\n");
  273. if (val != OCOTP_CFG3_6ULL_SPEED_900MHZ)
  274. if (dev_pm_opp_disable(dev, 900000000))
  275. dev_warn(dev, "failed to disable 900MHz OPP\n");
  276. }
  277. iounmap(base);
  278. put_node:
  279. of_node_put(np);
  280. }
  281. static int imx6q_cpufreq_probe(struct platform_device *pdev)
  282. {
  283. struct device_node *np;
  284. struct dev_pm_opp *opp;
  285. unsigned long min_volt, max_volt;
  286. int num, ret;
  287. const struct property *prop;
  288. const __be32 *val;
  289. u32 nr, i, j;
  290. cpu_dev = get_cpu_device(0);
  291. if (!cpu_dev) {
  292. pr_err("failed to get cpu0 device\n");
  293. return -ENODEV;
  294. }
  295. np = of_node_get(cpu_dev->of_node);
  296. if (!np) {
  297. dev_err(cpu_dev, "failed to find cpu0 node\n");
  298. return -ENOENT;
  299. }
  300. if (of_machine_is_compatible("fsl,imx6ul") ||
  301. of_machine_is_compatible("fsl,imx6ull"))
  302. num_clks = IMX6UL_CPUFREQ_CLK_NUM;
  303. else
  304. num_clks = IMX6Q_CPUFREQ_CLK_NUM;
  305. ret = clk_bulk_get(cpu_dev, num_clks, clks);
  306. if (ret)
  307. goto put_node;
  308. arm_reg = regulator_get(cpu_dev, "arm");
  309. pu_reg = regulator_get_optional(cpu_dev, "pu");
  310. soc_reg = regulator_get(cpu_dev, "soc");
  311. if (PTR_ERR(arm_reg) == -EPROBE_DEFER ||
  312. PTR_ERR(soc_reg) == -EPROBE_DEFER ||
  313. PTR_ERR(pu_reg) == -EPROBE_DEFER) {
  314. ret = -EPROBE_DEFER;
  315. dev_dbg(cpu_dev, "regulators not ready, defer\n");
  316. goto put_reg;
  317. }
  318. if (IS_ERR(arm_reg) || IS_ERR(soc_reg)) {
  319. dev_err(cpu_dev, "failed to get regulators\n");
  320. ret = -ENOENT;
  321. goto put_reg;
  322. }
  323. ret = dev_pm_opp_of_add_table(cpu_dev);
  324. if (ret < 0) {
  325. dev_err(cpu_dev, "failed to init OPP table: %d\n", ret);
  326. goto put_reg;
  327. }
  328. if (of_machine_is_compatible("fsl,imx6ul") ||
  329. of_machine_is_compatible("fsl,imx6ull"))
  330. imx6ul_opp_check_speed_grading(cpu_dev);
  331. else
  332. imx6q_opp_check_speed_grading(cpu_dev);
  333. /* Because we have added the OPPs here, we must free them */
  334. free_opp = true;
  335. num = dev_pm_opp_get_opp_count(cpu_dev);
  336. if (num < 0) {
  337. ret = num;
  338. dev_err(cpu_dev, "no OPP table is found: %d\n", ret);
  339. goto out_free_opp;
  340. }
  341. ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
  342. if (ret) {
  343. dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
  344. goto out_free_opp;
  345. }
  346. /* Make imx6_soc_volt array's size same as arm opp number */
  347. imx6_soc_volt = devm_kcalloc(cpu_dev, num, sizeof(*imx6_soc_volt),
  348. GFP_KERNEL);
  349. if (imx6_soc_volt == NULL) {
  350. ret = -ENOMEM;
  351. goto free_freq_table;
  352. }
  353. prop = of_find_property(np, "fsl,soc-operating-points", NULL);
  354. if (!prop || !prop->value)
  355. goto soc_opp_out;
  356. /*
  357. * Each OPP is a set of tuples consisting of frequency and
  358. * voltage like <freq-kHz vol-uV>.
  359. */
  360. nr = prop->length / sizeof(u32);
  361. if (nr % 2 || (nr / 2) < num)
  362. goto soc_opp_out;
  363. for (j = 0; j < num; j++) {
  364. val = prop->value;
  365. for (i = 0; i < nr / 2; i++) {
  366. unsigned long freq = be32_to_cpup(val++);
  367. unsigned long volt = be32_to_cpup(val++);
  368. if (freq_table[j].frequency == freq) {
  369. imx6_soc_volt[soc_opp_count++] = volt;
  370. break;
  371. }
  372. }
  373. }
  374. soc_opp_out:
  375. /* use fixed soc opp volt if no valid soc opp info found in dtb */
  376. if (soc_opp_count != num) {
  377. dev_warn(cpu_dev, "can NOT find valid fsl,soc-operating-points property in dtb, use default value!\n");
  378. for (j = 0; j < num; j++)
  379. imx6_soc_volt[j] = PU_SOC_VOLTAGE_NORMAL;
  380. if (freq_table[num - 1].frequency * 1000 == FREQ_1P2_GHZ)
  381. imx6_soc_volt[num - 1] = PU_SOC_VOLTAGE_HIGH;
  382. }
  383. if (of_property_read_u32(np, "clock-latency", &transition_latency))
  384. transition_latency = CPUFREQ_ETERNAL;
  385. /*
  386. * Calculate the ramp time for max voltage change in the
  387. * VDDSOC and VDDPU regulators.
  388. */
  389. ret = regulator_set_voltage_time(soc_reg, imx6_soc_volt[0], imx6_soc_volt[num - 1]);
  390. if (ret > 0)
  391. transition_latency += ret * 1000;
  392. if (!IS_ERR(pu_reg)) {
  393. ret = regulator_set_voltage_time(pu_reg, imx6_soc_volt[0], imx6_soc_volt[num - 1]);
  394. if (ret > 0)
  395. transition_latency += ret * 1000;
  396. }
  397. /*
  398. * OPP is maintained in order of increasing frequency, and
  399. * freq_table initialised from OPP is therefore sorted in the
  400. * same order.
  401. */
  402. max_freq = freq_table[--num].frequency;
  403. opp = dev_pm_opp_find_freq_exact(cpu_dev,
  404. freq_table[0].frequency * 1000, true);
  405. min_volt = dev_pm_opp_get_voltage(opp);
  406. dev_pm_opp_put(opp);
  407. opp = dev_pm_opp_find_freq_exact(cpu_dev, max_freq * 1000, true);
  408. max_volt = dev_pm_opp_get_voltage(opp);
  409. dev_pm_opp_put(opp);
  410. ret = regulator_set_voltage_time(arm_reg, min_volt, max_volt);
  411. if (ret > 0)
  412. transition_latency += ret * 1000;
  413. ret = cpufreq_register_driver(&imx6q_cpufreq_driver);
  414. if (ret) {
  415. dev_err(cpu_dev, "failed register driver: %d\n", ret);
  416. goto free_freq_table;
  417. }
  418. of_node_put(np);
  419. return 0;
  420. free_freq_table:
  421. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  422. out_free_opp:
  423. if (free_opp)
  424. dev_pm_opp_of_remove_table(cpu_dev);
  425. put_reg:
  426. if (!IS_ERR(arm_reg))
  427. regulator_put(arm_reg);
  428. if (!IS_ERR(pu_reg))
  429. regulator_put(pu_reg);
  430. if (!IS_ERR(soc_reg))
  431. regulator_put(soc_reg);
  432. clk_bulk_put(num_clks, clks);
  433. put_node:
  434. of_node_put(np);
  435. return ret;
  436. }
  437. static int imx6q_cpufreq_remove(struct platform_device *pdev)
  438. {
  439. cpufreq_unregister_driver(&imx6q_cpufreq_driver);
  440. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
  441. if (free_opp)
  442. dev_pm_opp_of_remove_table(cpu_dev);
  443. regulator_put(arm_reg);
  444. if (!IS_ERR(pu_reg))
  445. regulator_put(pu_reg);
  446. regulator_put(soc_reg);
  447. clk_bulk_put(num_clks, clks);
  448. return 0;
  449. }
  450. static struct platform_driver imx6q_cpufreq_platdrv = {
  451. .driver = {
  452. .name = "imx6q-cpufreq",
  453. },
  454. .probe = imx6q_cpufreq_probe,
  455. .remove = imx6q_cpufreq_remove,
  456. };
  457. module_platform_driver(imx6q_cpufreq_platdrv);
  458. MODULE_ALIAS("platform:imx6q-cpufreq");
  459. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  460. MODULE_DESCRIPTION("Freescale i.MX6Q cpufreq driver");
  461. MODULE_LICENSE("GPL");