1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright 2019 NXP
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/module.h>
7*4882a593Smuzhiyun #include <linux/device.h>
8*4882a593Smuzhiyun #include <linux/of_device.h>
9*4882a593Smuzhiyun #include <linux/platform_device.h>
10*4882a593Smuzhiyun #include <linux/devfreq.h>
11*4882a593Smuzhiyun #include <linux/pm_opp.h>
12*4882a593Smuzhiyun #include <linux/clk.h>
13*4882a593Smuzhiyun #include <linux/clk-provider.h>
14*4882a593Smuzhiyun #include <linux/arm-smccc.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #define IMX_SIP_DDR_DVFS 0xc2000004
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /* Query available frequencies. */
19*4882a593Smuzhiyun #define IMX_SIP_DDR_DVFS_GET_FREQ_COUNT 0x10
20*4882a593Smuzhiyun #define IMX_SIP_DDR_DVFS_GET_FREQ_INFO 0x11
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun * This should be in a 1:1 mapping with devicetree OPPs but
24*4882a593Smuzhiyun * firmware provides additional info.
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun struct imx8m_ddrc_freq {
27*4882a593Smuzhiyun unsigned long rate;
28*4882a593Smuzhiyun unsigned long smcarg;
29*4882a593Smuzhiyun int dram_core_parent_index;
30*4882a593Smuzhiyun int dram_alt_parent_index;
31*4882a593Smuzhiyun int dram_apb_parent_index;
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /* Hardware limitation */
35*4882a593Smuzhiyun #define IMX8M_DDRC_MAX_FREQ_COUNT 4
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun * i.MX8M DRAM Controller clocks have the following structure (abridged):
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * +----------+ |\ +------+
41*4882a593Smuzhiyun * | dram_pll |-------|M| dram_core | |
42*4882a593Smuzhiyun * +----------+ |U|---------->| D |
43*4882a593Smuzhiyun * /--|X| | D |
44*4882a593Smuzhiyun * dram_alt_root | |/ | R |
45*4882a593Smuzhiyun * | | C |
46*4882a593Smuzhiyun * +---------+ | |
47*4882a593Smuzhiyun * |FIX DIV/4| | |
48*4882a593Smuzhiyun * +---------+ | |
49*4882a593Smuzhiyun * composite: | | |
50*4882a593Smuzhiyun * +----------+ | | |
51*4882a593Smuzhiyun * | dram_alt |----/ | |
52*4882a593Smuzhiyun * +----------+ | |
53*4882a593Smuzhiyun * | dram_apb |-------------------->| |
54*4882a593Smuzhiyun * +----------+ +------+
55*4882a593Smuzhiyun *
56*4882a593Smuzhiyun * The dram_pll is used for higher rates and dram_alt is used for lower rates.
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun * Frequency switching is implemented in TF-A (via SMC call) and can change the
59*4882a593Smuzhiyun * configuration of the clocks, including mux parents. The dram_alt and
60*4882a593Smuzhiyun * dram_apb clocks are "imx composite" and their parent can change too.
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * We need to prepare/enable the new mux parents head of switching and update
63*4882a593Smuzhiyun * their information afterwards.
64*4882a593Smuzhiyun */
65*4882a593Smuzhiyun struct imx8m_ddrc {
66*4882a593Smuzhiyun struct devfreq_dev_profile profile;
67*4882a593Smuzhiyun struct devfreq *devfreq;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* For frequency switching: */
70*4882a593Smuzhiyun struct clk *dram_core;
71*4882a593Smuzhiyun struct clk *dram_pll;
72*4882a593Smuzhiyun struct clk *dram_alt;
73*4882a593Smuzhiyun struct clk *dram_apb;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun int freq_count;
76*4882a593Smuzhiyun struct imx8m_ddrc_freq freq_table[IMX8M_DDRC_MAX_FREQ_COUNT];
77*4882a593Smuzhiyun };
78*4882a593Smuzhiyun
imx8m_ddrc_find_freq(struct imx8m_ddrc * priv,unsigned long rate)79*4882a593Smuzhiyun static struct imx8m_ddrc_freq *imx8m_ddrc_find_freq(struct imx8m_ddrc *priv,
80*4882a593Smuzhiyun unsigned long rate)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun struct imx8m_ddrc_freq *freq;
83*4882a593Smuzhiyun int i;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun * Firmware reports values in MT/s, so we round-down from Hz
87*4882a593Smuzhiyun * Rounding is extra generous to ensure a match.
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun rate = DIV_ROUND_CLOSEST(rate, 250000);
90*4882a593Smuzhiyun for (i = 0; i < priv->freq_count; ++i) {
91*4882a593Smuzhiyun freq = &priv->freq_table[i];
92*4882a593Smuzhiyun if (freq->rate == rate ||
93*4882a593Smuzhiyun freq->rate + 1 == rate ||
94*4882a593Smuzhiyun freq->rate - 1 == rate)
95*4882a593Smuzhiyun return freq;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun return NULL;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
imx8m_ddrc_smc_set_freq(int target_freq)101*4882a593Smuzhiyun static void imx8m_ddrc_smc_set_freq(int target_freq)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun struct arm_smccc_res res;
104*4882a593Smuzhiyun u32 online_cpus = 0;
105*4882a593Smuzhiyun int cpu;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun local_irq_disable();
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun for_each_online_cpu(cpu)
110*4882a593Smuzhiyun online_cpus |= (1 << (cpu * 8));
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun /* change the ddr freqency */
113*4882a593Smuzhiyun arm_smccc_smc(IMX_SIP_DDR_DVFS, target_freq, online_cpus,
114*4882a593Smuzhiyun 0, 0, 0, 0, 0, &res);
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun local_irq_enable();
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
clk_get_parent_by_index(struct clk * clk,int index)119*4882a593Smuzhiyun static struct clk *clk_get_parent_by_index(struct clk *clk, int index)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun struct clk_hw *hw;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun hw = clk_hw_get_parent_by_index(__clk_get_hw(clk), index);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun return hw ? hw->clk : NULL;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
imx8m_ddrc_set_freq(struct device * dev,struct imx8m_ddrc_freq * freq)128*4882a593Smuzhiyun static int imx8m_ddrc_set_freq(struct device *dev, struct imx8m_ddrc_freq *freq)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun struct imx8m_ddrc *priv = dev_get_drvdata(dev);
131*4882a593Smuzhiyun struct clk *new_dram_core_parent;
132*4882a593Smuzhiyun struct clk *new_dram_alt_parent;
133*4882a593Smuzhiyun struct clk *new_dram_apb_parent;
134*4882a593Smuzhiyun int ret;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /*
137*4882a593Smuzhiyun * Fetch new parents
138*4882a593Smuzhiyun *
139*4882a593Smuzhiyun * new_dram_alt_parent and new_dram_apb_parent are optional but
140*4882a593Smuzhiyun * new_dram_core_parent is not.
141*4882a593Smuzhiyun */
142*4882a593Smuzhiyun new_dram_core_parent = clk_get_parent_by_index(
143*4882a593Smuzhiyun priv->dram_core, freq->dram_core_parent_index - 1);
144*4882a593Smuzhiyun if (!new_dram_core_parent) {
145*4882a593Smuzhiyun dev_err(dev, "failed to fetch new dram_core parent\n");
146*4882a593Smuzhiyun return -EINVAL;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun if (freq->dram_alt_parent_index) {
149*4882a593Smuzhiyun new_dram_alt_parent = clk_get_parent_by_index(
150*4882a593Smuzhiyun priv->dram_alt,
151*4882a593Smuzhiyun freq->dram_alt_parent_index - 1);
152*4882a593Smuzhiyun if (!new_dram_alt_parent) {
153*4882a593Smuzhiyun dev_err(dev, "failed to fetch new dram_alt parent\n");
154*4882a593Smuzhiyun return -EINVAL;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun } else
157*4882a593Smuzhiyun new_dram_alt_parent = NULL;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun if (freq->dram_apb_parent_index) {
160*4882a593Smuzhiyun new_dram_apb_parent = clk_get_parent_by_index(
161*4882a593Smuzhiyun priv->dram_apb,
162*4882a593Smuzhiyun freq->dram_apb_parent_index - 1);
163*4882a593Smuzhiyun if (!new_dram_apb_parent) {
164*4882a593Smuzhiyun dev_err(dev, "failed to fetch new dram_apb parent\n");
165*4882a593Smuzhiyun return -EINVAL;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun } else
168*4882a593Smuzhiyun new_dram_apb_parent = NULL;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /* increase reference counts and ensure clks are ON before switch */
171*4882a593Smuzhiyun ret = clk_prepare_enable(new_dram_core_parent);
172*4882a593Smuzhiyun if (ret) {
173*4882a593Smuzhiyun dev_err(dev, "failed to enable new dram_core parent: %d\n",
174*4882a593Smuzhiyun ret);
175*4882a593Smuzhiyun goto out;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun ret = clk_prepare_enable(new_dram_alt_parent);
178*4882a593Smuzhiyun if (ret) {
179*4882a593Smuzhiyun dev_err(dev, "failed to enable new dram_alt parent: %d\n",
180*4882a593Smuzhiyun ret);
181*4882a593Smuzhiyun goto out_disable_core_parent;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun ret = clk_prepare_enable(new_dram_apb_parent);
184*4882a593Smuzhiyun if (ret) {
185*4882a593Smuzhiyun dev_err(dev, "failed to enable new dram_apb parent: %d\n",
186*4882a593Smuzhiyun ret);
187*4882a593Smuzhiyun goto out_disable_alt_parent;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun imx8m_ddrc_smc_set_freq(freq->smcarg);
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun /* update parents in clk tree after switch. */
193*4882a593Smuzhiyun ret = clk_set_parent(priv->dram_core, new_dram_core_parent);
194*4882a593Smuzhiyun if (ret)
195*4882a593Smuzhiyun dev_warn(dev, "failed to set dram_core parent: %d\n", ret);
196*4882a593Smuzhiyun if (new_dram_alt_parent) {
197*4882a593Smuzhiyun ret = clk_set_parent(priv->dram_alt, new_dram_alt_parent);
198*4882a593Smuzhiyun if (ret)
199*4882a593Smuzhiyun dev_warn(dev, "failed to set dram_alt parent: %d\n",
200*4882a593Smuzhiyun ret);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun if (new_dram_apb_parent) {
203*4882a593Smuzhiyun ret = clk_set_parent(priv->dram_apb, new_dram_apb_parent);
204*4882a593Smuzhiyun if (ret)
205*4882a593Smuzhiyun dev_warn(dev, "failed to set dram_apb parent: %d\n",
206*4882a593Smuzhiyun ret);
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /*
210*4882a593Smuzhiyun * Explicitly refresh dram PLL rate.
211*4882a593Smuzhiyun *
212*4882a593Smuzhiyun * Even if it's marked with CLK_GET_RATE_NOCACHE the rate will not be
213*4882a593Smuzhiyun * automatically refreshed when clk_get_rate is called on children.
214*4882a593Smuzhiyun */
215*4882a593Smuzhiyun clk_get_rate(priv->dram_pll);
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /*
218*4882a593Smuzhiyun * clk_set_parent transfer the reference count from old parent.
219*4882a593Smuzhiyun * now we drop extra reference counts used during the switch
220*4882a593Smuzhiyun */
221*4882a593Smuzhiyun clk_disable_unprepare(new_dram_apb_parent);
222*4882a593Smuzhiyun out_disable_alt_parent:
223*4882a593Smuzhiyun clk_disable_unprepare(new_dram_alt_parent);
224*4882a593Smuzhiyun out_disable_core_parent:
225*4882a593Smuzhiyun clk_disable_unprepare(new_dram_core_parent);
226*4882a593Smuzhiyun out:
227*4882a593Smuzhiyun return ret;
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun
imx8m_ddrc_target(struct device * dev,unsigned long * freq,u32 flags)230*4882a593Smuzhiyun static int imx8m_ddrc_target(struct device *dev, unsigned long *freq, u32 flags)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun struct imx8m_ddrc *priv = dev_get_drvdata(dev);
233*4882a593Smuzhiyun struct imx8m_ddrc_freq *freq_info;
234*4882a593Smuzhiyun struct dev_pm_opp *new_opp;
235*4882a593Smuzhiyun unsigned long old_freq, new_freq;
236*4882a593Smuzhiyun int ret;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun new_opp = devfreq_recommended_opp(dev, freq, flags);
239*4882a593Smuzhiyun if (IS_ERR(new_opp)) {
240*4882a593Smuzhiyun ret = PTR_ERR(new_opp);
241*4882a593Smuzhiyun dev_err(dev, "failed to get recommended opp: %d\n", ret);
242*4882a593Smuzhiyun return ret;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun dev_pm_opp_put(new_opp);
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun old_freq = clk_get_rate(priv->dram_core);
247*4882a593Smuzhiyun if (*freq == old_freq)
248*4882a593Smuzhiyun return 0;
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun freq_info = imx8m_ddrc_find_freq(priv, *freq);
251*4882a593Smuzhiyun if (!freq_info)
252*4882a593Smuzhiyun return -EINVAL;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /*
255*4882a593Smuzhiyun * Read back the clk rate to verify switch was correct and so that
256*4882a593Smuzhiyun * we can report it on all error paths.
257*4882a593Smuzhiyun */
258*4882a593Smuzhiyun ret = imx8m_ddrc_set_freq(dev, freq_info);
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun new_freq = clk_get_rate(priv->dram_core);
261*4882a593Smuzhiyun if (ret)
262*4882a593Smuzhiyun dev_err(dev, "ddrc failed freq switch to %lu from %lu: error %d. now at %lu\n",
263*4882a593Smuzhiyun *freq, old_freq, ret, new_freq);
264*4882a593Smuzhiyun else if (*freq != new_freq)
265*4882a593Smuzhiyun dev_err(dev, "ddrc failed freq update to %lu from %lu, now at %lu\n",
266*4882a593Smuzhiyun *freq, old_freq, new_freq);
267*4882a593Smuzhiyun else
268*4882a593Smuzhiyun dev_dbg(dev, "ddrc freq set to %lu (was %lu)\n",
269*4882a593Smuzhiyun *freq, old_freq);
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun return ret;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
imx8m_ddrc_get_cur_freq(struct device * dev,unsigned long * freq)274*4882a593Smuzhiyun static int imx8m_ddrc_get_cur_freq(struct device *dev, unsigned long *freq)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun struct imx8m_ddrc *priv = dev_get_drvdata(dev);
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun *freq = clk_get_rate(priv->dram_core);
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun return 0;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun
imx8m_ddrc_get_dev_status(struct device * dev,struct devfreq_dev_status * stat)283*4882a593Smuzhiyun static int imx8m_ddrc_get_dev_status(struct device *dev,
284*4882a593Smuzhiyun struct devfreq_dev_status *stat)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun struct imx8m_ddrc *priv = dev_get_drvdata(dev);
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun stat->busy_time = 0;
289*4882a593Smuzhiyun stat->total_time = 0;
290*4882a593Smuzhiyun stat->current_frequency = clk_get_rate(priv->dram_core);
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun return 0;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
imx8m_ddrc_init_freq_info(struct device * dev)295*4882a593Smuzhiyun static int imx8m_ddrc_init_freq_info(struct device *dev)
296*4882a593Smuzhiyun {
297*4882a593Smuzhiyun struct imx8m_ddrc *priv = dev_get_drvdata(dev);
298*4882a593Smuzhiyun struct arm_smccc_res res;
299*4882a593Smuzhiyun int index;
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun /* An error here means DDR DVFS API not supported by firmware */
302*4882a593Smuzhiyun arm_smccc_smc(IMX_SIP_DDR_DVFS, IMX_SIP_DDR_DVFS_GET_FREQ_COUNT,
303*4882a593Smuzhiyun 0, 0, 0, 0, 0, 0, &res);
304*4882a593Smuzhiyun priv->freq_count = res.a0;
305*4882a593Smuzhiyun if (priv->freq_count <= 0 ||
306*4882a593Smuzhiyun priv->freq_count > IMX8M_DDRC_MAX_FREQ_COUNT)
307*4882a593Smuzhiyun return -ENODEV;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun for (index = 0; index < priv->freq_count; ++index) {
310*4882a593Smuzhiyun struct imx8m_ddrc_freq *freq = &priv->freq_table[index];
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun arm_smccc_smc(IMX_SIP_DDR_DVFS, IMX_SIP_DDR_DVFS_GET_FREQ_INFO,
313*4882a593Smuzhiyun index, 0, 0, 0, 0, 0, &res);
314*4882a593Smuzhiyun /* Result should be strictly positive */
315*4882a593Smuzhiyun if ((long)res.a0 <= 0)
316*4882a593Smuzhiyun return -ENODEV;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun freq->rate = res.a0;
319*4882a593Smuzhiyun freq->smcarg = index;
320*4882a593Smuzhiyun freq->dram_core_parent_index = res.a1;
321*4882a593Smuzhiyun freq->dram_alt_parent_index = res.a2;
322*4882a593Smuzhiyun freq->dram_apb_parent_index = res.a3;
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun /* dram_core has 2 options: dram_pll or dram_alt_root */
325*4882a593Smuzhiyun if (freq->dram_core_parent_index != 1 &&
326*4882a593Smuzhiyun freq->dram_core_parent_index != 2)
327*4882a593Smuzhiyun return -ENODEV;
328*4882a593Smuzhiyun /* dram_apb and dram_alt have exactly 8 possible parents */
329*4882a593Smuzhiyun if (freq->dram_alt_parent_index > 8 ||
330*4882a593Smuzhiyun freq->dram_apb_parent_index > 8)
331*4882a593Smuzhiyun return -ENODEV;
332*4882a593Smuzhiyun /* dram_core from alt requires explicit dram_alt parent */
333*4882a593Smuzhiyun if (freq->dram_core_parent_index == 2 &&
334*4882a593Smuzhiyun freq->dram_alt_parent_index == 0)
335*4882a593Smuzhiyun return -ENODEV;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun return 0;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun
imx8m_ddrc_check_opps(struct device * dev)341*4882a593Smuzhiyun static int imx8m_ddrc_check_opps(struct device *dev)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun struct imx8m_ddrc *priv = dev_get_drvdata(dev);
344*4882a593Smuzhiyun struct imx8m_ddrc_freq *freq_info;
345*4882a593Smuzhiyun struct dev_pm_opp *opp;
346*4882a593Smuzhiyun unsigned long freq;
347*4882a593Smuzhiyun int i, opp_count;
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun /* Enumerate DT OPPs and disable those not supported by firmware */
350*4882a593Smuzhiyun opp_count = dev_pm_opp_get_opp_count(dev);
351*4882a593Smuzhiyun if (opp_count < 0)
352*4882a593Smuzhiyun return opp_count;
353*4882a593Smuzhiyun for (i = 0, freq = 0; i < opp_count; ++i, ++freq) {
354*4882a593Smuzhiyun opp = dev_pm_opp_find_freq_ceil(dev, &freq);
355*4882a593Smuzhiyun if (IS_ERR(opp)) {
356*4882a593Smuzhiyun dev_err(dev, "Failed enumerating OPPs: %ld\n",
357*4882a593Smuzhiyun PTR_ERR(opp));
358*4882a593Smuzhiyun return PTR_ERR(opp);
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun dev_pm_opp_put(opp);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun freq_info = imx8m_ddrc_find_freq(priv, freq);
363*4882a593Smuzhiyun if (!freq_info) {
364*4882a593Smuzhiyun dev_info(dev, "Disable unsupported OPP %luHz %luMT/s\n",
365*4882a593Smuzhiyun freq, DIV_ROUND_CLOSEST(freq, 250000));
366*4882a593Smuzhiyun dev_pm_opp_disable(dev, freq);
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun return 0;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
imx8m_ddrc_exit(struct device * dev)373*4882a593Smuzhiyun static void imx8m_ddrc_exit(struct device *dev)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun dev_pm_opp_of_remove_table(dev);
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun
imx8m_ddrc_probe(struct platform_device * pdev)378*4882a593Smuzhiyun static int imx8m_ddrc_probe(struct platform_device *pdev)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun struct device *dev = &pdev->dev;
381*4882a593Smuzhiyun struct imx8m_ddrc *priv;
382*4882a593Smuzhiyun const char *gov = DEVFREQ_GOV_USERSPACE;
383*4882a593Smuzhiyun int ret;
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
386*4882a593Smuzhiyun if (!priv)
387*4882a593Smuzhiyun return -ENOMEM;
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun platform_set_drvdata(pdev, priv);
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun ret = imx8m_ddrc_init_freq_info(dev);
392*4882a593Smuzhiyun if (ret) {
393*4882a593Smuzhiyun dev_err(dev, "failed to init firmware freq info: %d\n", ret);
394*4882a593Smuzhiyun return ret;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun priv->dram_core = devm_clk_get(dev, "core");
398*4882a593Smuzhiyun if (IS_ERR(priv->dram_core)) {
399*4882a593Smuzhiyun ret = PTR_ERR(priv->dram_core);
400*4882a593Smuzhiyun dev_err(dev, "failed to fetch core clock: %d\n", ret);
401*4882a593Smuzhiyun return ret;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun priv->dram_pll = devm_clk_get(dev, "pll");
404*4882a593Smuzhiyun if (IS_ERR(priv->dram_pll)) {
405*4882a593Smuzhiyun ret = PTR_ERR(priv->dram_pll);
406*4882a593Smuzhiyun dev_err(dev, "failed to fetch pll clock: %d\n", ret);
407*4882a593Smuzhiyun return ret;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun priv->dram_alt = devm_clk_get(dev, "alt");
410*4882a593Smuzhiyun if (IS_ERR(priv->dram_alt)) {
411*4882a593Smuzhiyun ret = PTR_ERR(priv->dram_alt);
412*4882a593Smuzhiyun dev_err(dev, "failed to fetch alt clock: %d\n", ret);
413*4882a593Smuzhiyun return ret;
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun priv->dram_apb = devm_clk_get(dev, "apb");
416*4882a593Smuzhiyun if (IS_ERR(priv->dram_apb)) {
417*4882a593Smuzhiyun ret = PTR_ERR(priv->dram_apb);
418*4882a593Smuzhiyun dev_err(dev, "failed to fetch apb clock: %d\n", ret);
419*4882a593Smuzhiyun return ret;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun ret = dev_pm_opp_of_add_table(dev);
423*4882a593Smuzhiyun if (ret < 0) {
424*4882a593Smuzhiyun dev_err(dev, "failed to get OPP table\n");
425*4882a593Smuzhiyun return ret;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun ret = imx8m_ddrc_check_opps(dev);
429*4882a593Smuzhiyun if (ret < 0)
430*4882a593Smuzhiyun goto err;
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun priv->profile.polling_ms = 1000;
433*4882a593Smuzhiyun priv->profile.target = imx8m_ddrc_target;
434*4882a593Smuzhiyun priv->profile.get_dev_status = imx8m_ddrc_get_dev_status;
435*4882a593Smuzhiyun priv->profile.exit = imx8m_ddrc_exit;
436*4882a593Smuzhiyun priv->profile.get_cur_freq = imx8m_ddrc_get_cur_freq;
437*4882a593Smuzhiyun priv->profile.initial_freq = clk_get_rate(priv->dram_core);
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun priv->devfreq = devm_devfreq_add_device(dev, &priv->profile,
440*4882a593Smuzhiyun gov, NULL);
441*4882a593Smuzhiyun if (IS_ERR(priv->devfreq)) {
442*4882a593Smuzhiyun ret = PTR_ERR(priv->devfreq);
443*4882a593Smuzhiyun dev_err(dev, "failed to add devfreq device: %d\n", ret);
444*4882a593Smuzhiyun goto err;
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun return 0;
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun err:
450*4882a593Smuzhiyun dev_pm_opp_of_remove_table(dev);
451*4882a593Smuzhiyun return ret;
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun static const struct of_device_id imx8m_ddrc_of_match[] = {
455*4882a593Smuzhiyun { .compatible = "fsl,imx8m-ddrc", },
456*4882a593Smuzhiyun { /* sentinel */ },
457*4882a593Smuzhiyun };
458*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, imx8m_ddrc_of_match);
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun static struct platform_driver imx8m_ddrc_platdrv = {
461*4882a593Smuzhiyun .probe = imx8m_ddrc_probe,
462*4882a593Smuzhiyun .driver = {
463*4882a593Smuzhiyun .name = "imx8m-ddrc-devfreq",
464*4882a593Smuzhiyun .of_match_table = of_match_ptr(imx8m_ddrc_of_match),
465*4882a593Smuzhiyun },
466*4882a593Smuzhiyun };
467*4882a593Smuzhiyun module_platform_driver(imx8m_ddrc_platdrv);
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun MODULE_DESCRIPTION("i.MX8M DDR Controller frequency driver");
470*4882a593Smuzhiyun MODULE_AUTHOR("Leonard Crestez <leonard.crestez@nxp.com>");
471*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
472