imx6q-cpufreq.c 15 KB

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