armada-37xx-cpufreq.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * CPU frequency scaling support for Armada 37xx platform.
  4. *
  5. * Copyright (C) 2017 Marvell
  6. *
  7. * Gregory CLEMENT <[email protected]>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/cpu.h>
  11. #include <linux/cpufreq.h>
  12. #include <linux/err.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/module.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_opp.h>
  22. #include <linux/regmap.h>
  23. #include <linux/slab.h>
  24. #include "cpufreq-dt.h"
  25. /* Clk register set */
  26. #define ARMADA_37XX_CLK_TBG_SEL 0
  27. #define ARMADA_37XX_CLK_TBG_SEL_CPU_OFF 22
  28. /* Power management in North Bridge register set */
  29. #define ARMADA_37XX_NB_L0L1 0x18
  30. #define ARMADA_37XX_NB_L2L3 0x1C
  31. #define ARMADA_37XX_NB_TBG_DIV_OFF 13
  32. #define ARMADA_37XX_NB_TBG_DIV_MASK 0x7
  33. #define ARMADA_37XX_NB_CLK_SEL_OFF 11
  34. #define ARMADA_37XX_NB_CLK_SEL_MASK 0x1
  35. #define ARMADA_37XX_NB_CLK_SEL_TBG 0x1
  36. #define ARMADA_37XX_NB_TBG_SEL_OFF 9
  37. #define ARMADA_37XX_NB_TBG_SEL_MASK 0x3
  38. #define ARMADA_37XX_NB_VDD_SEL_OFF 6
  39. #define ARMADA_37XX_NB_VDD_SEL_MASK 0x3
  40. #define ARMADA_37XX_NB_CONFIG_SHIFT 16
  41. #define ARMADA_37XX_NB_DYN_MOD 0x24
  42. #define ARMADA_37XX_NB_CLK_SEL_EN BIT(26)
  43. #define ARMADA_37XX_NB_TBG_EN BIT(28)
  44. #define ARMADA_37XX_NB_DIV_EN BIT(29)
  45. #define ARMADA_37XX_NB_VDD_EN BIT(30)
  46. #define ARMADA_37XX_NB_DFS_EN BIT(31)
  47. #define ARMADA_37XX_NB_CPU_LOAD 0x30
  48. #define ARMADA_37XX_NB_CPU_LOAD_MASK 0x3
  49. #define ARMADA_37XX_DVFS_LOAD_0 0
  50. #define ARMADA_37XX_DVFS_LOAD_1 1
  51. #define ARMADA_37XX_DVFS_LOAD_2 2
  52. #define ARMADA_37XX_DVFS_LOAD_3 3
  53. /* AVS register set */
  54. #define ARMADA_37XX_AVS_CTL0 0x0
  55. #define ARMADA_37XX_AVS_ENABLE BIT(30)
  56. #define ARMADA_37XX_AVS_HIGH_VDD_LIMIT 16
  57. #define ARMADA_37XX_AVS_LOW_VDD_LIMIT 22
  58. #define ARMADA_37XX_AVS_VDD_MASK 0x3F
  59. #define ARMADA_37XX_AVS_CTL2 0x8
  60. #define ARMADA_37XX_AVS_LOW_VDD_EN BIT(6)
  61. #define ARMADA_37XX_AVS_VSET(x) (0x1C + 4 * (x))
  62. /*
  63. * On Armada 37xx the Power management manages 4 level of CPU load,
  64. * each level can be associated with a CPU clock source, a CPU
  65. * divider, a VDD level, etc...
  66. */
  67. #define LOAD_LEVEL_NR 4
  68. #define MIN_VOLT_MV 1000
  69. #define MIN_VOLT_MV_FOR_L1_1000MHZ 1108
  70. #define MIN_VOLT_MV_FOR_L1_1200MHZ 1155
  71. /* AVS value for the corresponding voltage (in mV) */
  72. static int avs_map[] = {
  73. 747, 758, 770, 782, 793, 805, 817, 828, 840, 852, 863, 875, 887, 898,
  74. 910, 922, 933, 945, 957, 968, 980, 992, 1003, 1015, 1027, 1038, 1050,
  75. 1062, 1073, 1085, 1097, 1108, 1120, 1132, 1143, 1155, 1167, 1178, 1190,
  76. 1202, 1213, 1225, 1237, 1248, 1260, 1272, 1283, 1295, 1307, 1318, 1330,
  77. 1342
  78. };
  79. struct armada37xx_cpufreq_state {
  80. struct platform_device *pdev;
  81. struct device *cpu_dev;
  82. struct regmap *regmap;
  83. u32 nb_l0l1;
  84. u32 nb_l2l3;
  85. u32 nb_dyn_mod;
  86. u32 nb_cpu_load;
  87. };
  88. static struct armada37xx_cpufreq_state *armada37xx_cpufreq_state;
  89. struct armada_37xx_dvfs {
  90. u32 cpu_freq_max;
  91. u8 divider[LOAD_LEVEL_NR];
  92. u32 avs[LOAD_LEVEL_NR];
  93. };
  94. static struct armada_37xx_dvfs armada_37xx_dvfs[] = {
  95. /*
  96. * The cpufreq scaling for 1.2 GHz variant of the SOC is currently
  97. * unstable because we do not know how to configure it properly.
  98. */
  99. /* {.cpu_freq_max = 1200*1000*1000, .divider = {1, 2, 4, 6} }, */
  100. {.cpu_freq_max = 1000*1000*1000, .divider = {1, 2, 4, 5} },
  101. {.cpu_freq_max = 800*1000*1000, .divider = {1, 2, 3, 4} },
  102. {.cpu_freq_max = 600*1000*1000, .divider = {2, 4, 5, 6} },
  103. };
  104. static struct armada_37xx_dvfs *armada_37xx_cpu_freq_info_get(u32 freq)
  105. {
  106. int i;
  107. for (i = 0; i < ARRAY_SIZE(armada_37xx_dvfs); i++) {
  108. if (freq == armada_37xx_dvfs[i].cpu_freq_max)
  109. return &armada_37xx_dvfs[i];
  110. }
  111. pr_err("Unsupported CPU frequency %d MHz\n", freq/1000000);
  112. return NULL;
  113. }
  114. /*
  115. * Setup the four level managed by the hardware. Once the four level
  116. * will be configured then the DVFS will be enabled.
  117. */
  118. static void __init armada37xx_cpufreq_dvfs_setup(struct regmap *base,
  119. struct regmap *clk_base, u8 *divider)
  120. {
  121. u32 cpu_tbg_sel;
  122. int load_lvl;
  123. /* Determine to which TBG clock is CPU connected */
  124. regmap_read(clk_base, ARMADA_37XX_CLK_TBG_SEL, &cpu_tbg_sel);
  125. cpu_tbg_sel >>= ARMADA_37XX_CLK_TBG_SEL_CPU_OFF;
  126. cpu_tbg_sel &= ARMADA_37XX_NB_TBG_SEL_MASK;
  127. for (load_lvl = 0; load_lvl < LOAD_LEVEL_NR; load_lvl++) {
  128. unsigned int reg, mask, val, offset = 0;
  129. if (load_lvl <= ARMADA_37XX_DVFS_LOAD_1)
  130. reg = ARMADA_37XX_NB_L0L1;
  131. else
  132. reg = ARMADA_37XX_NB_L2L3;
  133. if (load_lvl == ARMADA_37XX_DVFS_LOAD_0 ||
  134. load_lvl == ARMADA_37XX_DVFS_LOAD_2)
  135. offset += ARMADA_37XX_NB_CONFIG_SHIFT;
  136. /* Set cpu clock source, for all the level we use TBG */
  137. val = ARMADA_37XX_NB_CLK_SEL_TBG << ARMADA_37XX_NB_CLK_SEL_OFF;
  138. mask = (ARMADA_37XX_NB_CLK_SEL_MASK
  139. << ARMADA_37XX_NB_CLK_SEL_OFF);
  140. /* Set TBG index, for all levels we use the same TBG */
  141. val = cpu_tbg_sel << ARMADA_37XX_NB_TBG_SEL_OFF;
  142. mask = (ARMADA_37XX_NB_TBG_SEL_MASK
  143. << ARMADA_37XX_NB_TBG_SEL_OFF);
  144. /*
  145. * Set cpu divider based on the pre-computed array in
  146. * order to have balanced step.
  147. */
  148. val |= divider[load_lvl] << ARMADA_37XX_NB_TBG_DIV_OFF;
  149. mask |= (ARMADA_37XX_NB_TBG_DIV_MASK
  150. << ARMADA_37XX_NB_TBG_DIV_OFF);
  151. /* Set VDD divider which is actually the load level. */
  152. val |= load_lvl << ARMADA_37XX_NB_VDD_SEL_OFF;
  153. mask |= (ARMADA_37XX_NB_VDD_SEL_MASK
  154. << ARMADA_37XX_NB_VDD_SEL_OFF);
  155. val <<= offset;
  156. mask <<= offset;
  157. regmap_update_bits(base, reg, mask, val);
  158. }
  159. }
  160. /*
  161. * Find out the armada 37x supported AVS value whose voltage value is
  162. * the round-up closest to the target voltage value.
  163. */
  164. static u32 armada_37xx_avs_val_match(int target_vm)
  165. {
  166. u32 avs;
  167. /* Find out the round-up closest supported voltage value */
  168. for (avs = 0; avs < ARRAY_SIZE(avs_map); avs++)
  169. if (avs_map[avs] >= target_vm)
  170. break;
  171. /*
  172. * If all supported voltages are smaller than target one,
  173. * choose the largest supported voltage
  174. */
  175. if (avs == ARRAY_SIZE(avs_map))
  176. avs = ARRAY_SIZE(avs_map) - 1;
  177. return avs;
  178. }
  179. /*
  180. * For Armada 37xx soc, L0(VSET0) VDD AVS value is set to SVC revision
  181. * value or a default value when SVC is not supported.
  182. * - L0 can be read out from the register of AVS_CTRL_0 and L0 voltage
  183. * can be got from the mapping table of avs_map.
  184. * - L1 voltage should be about 100mv smaller than L0 voltage
  185. * - L2 & L3 voltage should be about 150mv smaller than L0 voltage.
  186. * This function calculates L1 & L2 & L3 AVS values dynamically based
  187. * on L0 voltage and fill all AVS values to the AVS value table.
  188. * When base CPU frequency is 1000 or 1200 MHz then there is additional
  189. * minimal avs value for load L1.
  190. */
  191. static void __init armada37xx_cpufreq_avs_configure(struct regmap *base,
  192. struct armada_37xx_dvfs *dvfs)
  193. {
  194. unsigned int target_vm;
  195. int load_level = 0;
  196. u32 l0_vdd_min;
  197. if (base == NULL)
  198. return;
  199. /* Get L0 VDD min value */
  200. regmap_read(base, ARMADA_37XX_AVS_CTL0, &l0_vdd_min);
  201. l0_vdd_min = (l0_vdd_min >> ARMADA_37XX_AVS_LOW_VDD_LIMIT) &
  202. ARMADA_37XX_AVS_VDD_MASK;
  203. if (l0_vdd_min >= ARRAY_SIZE(avs_map)) {
  204. pr_err("L0 VDD MIN %d is not correct.\n", l0_vdd_min);
  205. return;
  206. }
  207. dvfs->avs[0] = l0_vdd_min;
  208. if (avs_map[l0_vdd_min] <= MIN_VOLT_MV) {
  209. /*
  210. * If L0 voltage is smaller than 1000mv, then all VDD sets
  211. * use L0 voltage;
  212. */
  213. u32 avs_min = armada_37xx_avs_val_match(MIN_VOLT_MV);
  214. for (load_level = 1; load_level < LOAD_LEVEL_NR; load_level++)
  215. dvfs->avs[load_level] = avs_min;
  216. /*
  217. * Set the avs values for load L0 and L1 when base CPU frequency
  218. * is 1000/1200 MHz to its typical initial values according to
  219. * the Armada 3700 Hardware Specifications.
  220. */
  221. if (dvfs->cpu_freq_max >= 1000*1000*1000) {
  222. if (dvfs->cpu_freq_max >= 1200*1000*1000)
  223. avs_min = armada_37xx_avs_val_match(MIN_VOLT_MV_FOR_L1_1200MHZ);
  224. else
  225. avs_min = armada_37xx_avs_val_match(MIN_VOLT_MV_FOR_L1_1000MHZ);
  226. dvfs->avs[0] = dvfs->avs[1] = avs_min;
  227. }
  228. return;
  229. }
  230. /*
  231. * L1 voltage is equal to L0 voltage - 100mv and it must be
  232. * larger than 1000mv
  233. */
  234. target_vm = avs_map[l0_vdd_min] - 100;
  235. target_vm = target_vm > MIN_VOLT_MV ? target_vm : MIN_VOLT_MV;
  236. dvfs->avs[1] = armada_37xx_avs_val_match(target_vm);
  237. /*
  238. * L2 & L3 voltage is equal to L0 voltage - 150mv and it must
  239. * be larger than 1000mv
  240. */
  241. target_vm = avs_map[l0_vdd_min] - 150;
  242. target_vm = target_vm > MIN_VOLT_MV ? target_vm : MIN_VOLT_MV;
  243. dvfs->avs[2] = dvfs->avs[3] = armada_37xx_avs_val_match(target_vm);
  244. /*
  245. * Fix the avs value for load L1 when base CPU frequency is 1000/1200 MHz,
  246. * otherwise the CPU gets stuck when switching from load L1 to load L0.
  247. * Also ensure that avs value for load L1 is not higher than for L0.
  248. */
  249. if (dvfs->cpu_freq_max >= 1000*1000*1000) {
  250. u32 avs_min_l1;
  251. if (dvfs->cpu_freq_max >= 1200*1000*1000)
  252. avs_min_l1 = armada_37xx_avs_val_match(MIN_VOLT_MV_FOR_L1_1200MHZ);
  253. else
  254. avs_min_l1 = armada_37xx_avs_val_match(MIN_VOLT_MV_FOR_L1_1000MHZ);
  255. if (avs_min_l1 > dvfs->avs[0])
  256. avs_min_l1 = dvfs->avs[0];
  257. if (dvfs->avs[1] < avs_min_l1)
  258. dvfs->avs[1] = avs_min_l1;
  259. }
  260. }
  261. static void __init armada37xx_cpufreq_avs_setup(struct regmap *base,
  262. struct armada_37xx_dvfs *dvfs)
  263. {
  264. unsigned int avs_val = 0;
  265. int load_level = 0;
  266. if (base == NULL)
  267. return;
  268. /* Disable AVS before the configuration */
  269. regmap_update_bits(base, ARMADA_37XX_AVS_CTL0,
  270. ARMADA_37XX_AVS_ENABLE, 0);
  271. /* Enable low voltage mode */
  272. regmap_update_bits(base, ARMADA_37XX_AVS_CTL2,
  273. ARMADA_37XX_AVS_LOW_VDD_EN,
  274. ARMADA_37XX_AVS_LOW_VDD_EN);
  275. for (load_level = 1; load_level < LOAD_LEVEL_NR; load_level++) {
  276. avs_val = dvfs->avs[load_level];
  277. regmap_update_bits(base, ARMADA_37XX_AVS_VSET(load_level-1),
  278. ARMADA_37XX_AVS_VDD_MASK << ARMADA_37XX_AVS_HIGH_VDD_LIMIT |
  279. ARMADA_37XX_AVS_VDD_MASK << ARMADA_37XX_AVS_LOW_VDD_LIMIT,
  280. avs_val << ARMADA_37XX_AVS_HIGH_VDD_LIMIT |
  281. avs_val << ARMADA_37XX_AVS_LOW_VDD_LIMIT);
  282. }
  283. /* Enable AVS after the configuration */
  284. regmap_update_bits(base, ARMADA_37XX_AVS_CTL0,
  285. ARMADA_37XX_AVS_ENABLE,
  286. ARMADA_37XX_AVS_ENABLE);
  287. }
  288. static void armada37xx_cpufreq_disable_dvfs(struct regmap *base)
  289. {
  290. unsigned int reg = ARMADA_37XX_NB_DYN_MOD,
  291. mask = ARMADA_37XX_NB_DFS_EN;
  292. regmap_update_bits(base, reg, mask, 0);
  293. }
  294. static void __init armada37xx_cpufreq_enable_dvfs(struct regmap *base)
  295. {
  296. unsigned int val, reg = ARMADA_37XX_NB_CPU_LOAD,
  297. mask = ARMADA_37XX_NB_CPU_LOAD_MASK;
  298. /* Start with the highest load (0) */
  299. val = ARMADA_37XX_DVFS_LOAD_0;
  300. regmap_update_bits(base, reg, mask, val);
  301. /* Now enable DVFS for the CPUs */
  302. reg = ARMADA_37XX_NB_DYN_MOD;
  303. mask = ARMADA_37XX_NB_CLK_SEL_EN | ARMADA_37XX_NB_TBG_EN |
  304. ARMADA_37XX_NB_DIV_EN | ARMADA_37XX_NB_VDD_EN |
  305. ARMADA_37XX_NB_DFS_EN;
  306. regmap_update_bits(base, reg, mask, mask);
  307. }
  308. static int armada37xx_cpufreq_suspend(struct cpufreq_policy *policy)
  309. {
  310. struct armada37xx_cpufreq_state *state = armada37xx_cpufreq_state;
  311. regmap_read(state->regmap, ARMADA_37XX_NB_L0L1, &state->nb_l0l1);
  312. regmap_read(state->regmap, ARMADA_37XX_NB_L2L3, &state->nb_l2l3);
  313. regmap_read(state->regmap, ARMADA_37XX_NB_CPU_LOAD,
  314. &state->nb_cpu_load);
  315. regmap_read(state->regmap, ARMADA_37XX_NB_DYN_MOD, &state->nb_dyn_mod);
  316. return 0;
  317. }
  318. static int armada37xx_cpufreq_resume(struct cpufreq_policy *policy)
  319. {
  320. struct armada37xx_cpufreq_state *state = armada37xx_cpufreq_state;
  321. /* Ensure DVFS is disabled otherwise the following registers are RO */
  322. armada37xx_cpufreq_disable_dvfs(state->regmap);
  323. regmap_write(state->regmap, ARMADA_37XX_NB_L0L1, state->nb_l0l1);
  324. regmap_write(state->regmap, ARMADA_37XX_NB_L2L3, state->nb_l2l3);
  325. regmap_write(state->regmap, ARMADA_37XX_NB_CPU_LOAD,
  326. state->nb_cpu_load);
  327. /*
  328. * NB_DYN_MOD register is the one that actually enable back DVFS if it
  329. * was enabled before the suspend operation. This must be done last
  330. * otherwise other registers are not writable.
  331. */
  332. regmap_write(state->regmap, ARMADA_37XX_NB_DYN_MOD, state->nb_dyn_mod);
  333. return 0;
  334. }
  335. static int __init armada37xx_cpufreq_driver_init(void)
  336. {
  337. struct cpufreq_dt_platform_data pdata;
  338. struct armada_37xx_dvfs *dvfs;
  339. struct platform_device *pdev;
  340. unsigned long freq;
  341. unsigned int base_frequency;
  342. struct regmap *nb_clk_base, *nb_pm_base, *avs_base;
  343. struct device *cpu_dev;
  344. int load_lvl, ret;
  345. struct clk *clk, *parent;
  346. nb_clk_base =
  347. syscon_regmap_lookup_by_compatible("marvell,armada-3700-periph-clock-nb");
  348. if (IS_ERR(nb_clk_base))
  349. return -ENODEV;
  350. nb_pm_base =
  351. syscon_regmap_lookup_by_compatible("marvell,armada-3700-nb-pm");
  352. if (IS_ERR(nb_pm_base))
  353. return -ENODEV;
  354. avs_base =
  355. syscon_regmap_lookup_by_compatible("marvell,armada-3700-avs");
  356. /* if AVS is not present don't use it but still try to setup dvfs */
  357. if (IS_ERR(avs_base)) {
  358. pr_info("Syscon failed for Adapting Voltage Scaling: skip it\n");
  359. avs_base = NULL;
  360. }
  361. /* Before doing any configuration on the DVFS first, disable it */
  362. armada37xx_cpufreq_disable_dvfs(nb_pm_base);
  363. /*
  364. * On CPU 0 register the operating points supported (which are
  365. * the nominal CPU frequency and full integer divisions of
  366. * it).
  367. */
  368. cpu_dev = get_cpu_device(0);
  369. if (!cpu_dev) {
  370. dev_err(cpu_dev, "Cannot get CPU\n");
  371. return -ENODEV;
  372. }
  373. clk = clk_get(cpu_dev, NULL);
  374. if (IS_ERR(clk)) {
  375. dev_err(cpu_dev, "Cannot get clock for CPU0\n");
  376. return PTR_ERR(clk);
  377. }
  378. parent = clk_get_parent(clk);
  379. if (IS_ERR(parent)) {
  380. dev_err(cpu_dev, "Cannot get parent clock for CPU0\n");
  381. clk_put(clk);
  382. return PTR_ERR(parent);
  383. }
  384. /* Get parent CPU frequency */
  385. base_frequency = clk_get_rate(parent);
  386. if (!base_frequency) {
  387. dev_err(cpu_dev, "Failed to get parent clock rate for CPU\n");
  388. clk_put(clk);
  389. return -EINVAL;
  390. }
  391. dvfs = armada_37xx_cpu_freq_info_get(base_frequency);
  392. if (!dvfs) {
  393. clk_put(clk);
  394. return -EINVAL;
  395. }
  396. armada37xx_cpufreq_state = kmalloc(sizeof(*armada37xx_cpufreq_state),
  397. GFP_KERNEL);
  398. if (!armada37xx_cpufreq_state) {
  399. clk_put(clk);
  400. return -ENOMEM;
  401. }
  402. armada37xx_cpufreq_state->regmap = nb_pm_base;
  403. armada37xx_cpufreq_avs_configure(avs_base, dvfs);
  404. armada37xx_cpufreq_avs_setup(avs_base, dvfs);
  405. armada37xx_cpufreq_dvfs_setup(nb_pm_base, nb_clk_base, dvfs->divider);
  406. clk_put(clk);
  407. for (load_lvl = ARMADA_37XX_DVFS_LOAD_0; load_lvl < LOAD_LEVEL_NR;
  408. load_lvl++) {
  409. unsigned long u_volt = avs_map[dvfs->avs[load_lvl]] * 1000;
  410. freq = base_frequency / dvfs->divider[load_lvl];
  411. ret = dev_pm_opp_add(cpu_dev, freq, u_volt);
  412. if (ret)
  413. goto remove_opp;
  414. }
  415. /* Now that everything is setup, enable the DVFS at hardware level */
  416. armada37xx_cpufreq_enable_dvfs(nb_pm_base);
  417. memset(&pdata, 0, sizeof(pdata));
  418. pdata.suspend = armada37xx_cpufreq_suspend;
  419. pdata.resume = armada37xx_cpufreq_resume;
  420. pdev = platform_device_register_data(NULL, "cpufreq-dt", -1, &pdata,
  421. sizeof(pdata));
  422. ret = PTR_ERR_OR_ZERO(pdev);
  423. if (ret)
  424. goto disable_dvfs;
  425. armada37xx_cpufreq_state->cpu_dev = cpu_dev;
  426. armada37xx_cpufreq_state->pdev = pdev;
  427. platform_set_drvdata(pdev, dvfs);
  428. return 0;
  429. disable_dvfs:
  430. armada37xx_cpufreq_disable_dvfs(nb_pm_base);
  431. remove_opp:
  432. /* clean-up the already added opp before leaving */
  433. while (load_lvl-- > ARMADA_37XX_DVFS_LOAD_0) {
  434. freq = base_frequency / dvfs->divider[load_lvl];
  435. dev_pm_opp_remove(cpu_dev, freq);
  436. }
  437. kfree(armada37xx_cpufreq_state);
  438. return ret;
  439. }
  440. /* late_initcall, to guarantee the driver is loaded after A37xx clock driver */
  441. late_initcall(armada37xx_cpufreq_driver_init);
  442. static void __exit armada37xx_cpufreq_driver_exit(void)
  443. {
  444. struct platform_device *pdev = armada37xx_cpufreq_state->pdev;
  445. struct armada_37xx_dvfs *dvfs = platform_get_drvdata(pdev);
  446. unsigned long freq;
  447. int load_lvl;
  448. platform_device_unregister(pdev);
  449. armada37xx_cpufreq_disable_dvfs(armada37xx_cpufreq_state->regmap);
  450. for (load_lvl = ARMADA_37XX_DVFS_LOAD_0; load_lvl < LOAD_LEVEL_NR; load_lvl++) {
  451. freq = dvfs->cpu_freq_max / dvfs->divider[load_lvl];
  452. dev_pm_opp_remove(armada37xx_cpufreq_state->cpu_dev, freq);
  453. }
  454. kfree(armada37xx_cpufreq_state);
  455. }
  456. module_exit(armada37xx_cpufreq_driver_exit);
  457. static const struct of_device_id __maybe_unused armada37xx_cpufreq_of_match[] = {
  458. { .compatible = "marvell,armada-3700-nb-pm" },
  459. { },
  460. };
  461. MODULE_DEVICE_TABLE(of, armada37xx_cpufreq_of_match);
  462. MODULE_AUTHOR("Gregory CLEMENT <[email protected]>");
  463. MODULE_DESCRIPTION("Armada 37xx cpufreq driver");
  464. MODULE_LICENSE("GPL");