1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd.
4 * Author: Lin Huang <hl@rock-chips.com>
7 #include <linux/arm-smccc.h>
8 #include <linux/bitfield.h>
10 #include <linux/delay.h>
11 #include <linux/devfreq.h>
12 #include <linux/devfreq-event.h>
13 #include <linux/interrupt.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_opp.h>
19 #include <linux/regmap.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/rwsem.h>
22 #include <linux/suspend.h>
24 #include <soc/rockchip/pm_domains.h>
25 #include <soc/rockchip/rockchip_grf.h>
26 #include <soc/rockchip/rk3399_grf.h>
27 #include <soc/rockchip/rockchip_sip.h>
29 #define NS_TO_CYCLE(NS, MHz) (((NS) * (MHz)) / NSEC_PER_USEC)
31 #define RK3399_SET_ODT_PD_0_SR_IDLE GENMASK(7, 0)
32 #define RK3399_SET_ODT_PD_0_SR_MC_GATE_IDLE GENMASK(15, 8)
33 #define RK3399_SET_ODT_PD_0_STANDBY_IDLE GENMASK(31, 16)
35 #define RK3399_SET_ODT_PD_1_PD_IDLE GENMASK(11, 0)
36 #define RK3399_SET_ODT_PD_1_SRPD_LITE_IDLE GENMASK(27, 16)
38 #define RK3399_SET_ODT_PD_2_ODT_ENABLE BIT(0)
40 struct rk3399_dmcfreq
{
42 struct devfreq
*devfreq
;
43 struct devfreq_dev_profile profile
;
44 struct devfreq_simple_ondemand_data ondemand_data
;
46 struct devfreq_event_dev
*edev
;
48 struct regulator
*vdd_center
;
49 struct regmap
*regmap_pmu
;
50 unsigned long rate
, target_rate
;
51 unsigned long volt
, target_volt
;
52 unsigned int odt_dis_freq
;
54 unsigned int pd_idle_ns
;
55 unsigned int sr_idle_ns
;
56 unsigned int sr_mc_gate_idle_ns
;
57 unsigned int srpd_lite_idle_ns
;
58 unsigned int standby_idle_ns
;
59 unsigned int ddr3_odt_dis_freq
;
60 unsigned int lpddr3_odt_dis_freq
;
61 unsigned int lpddr4_odt_dis_freq
;
63 unsigned int pd_idle_dis_freq
;
64 unsigned int sr_idle_dis_freq
;
65 unsigned int sr_mc_gate_idle_dis_freq
;
66 unsigned int srpd_lite_idle_dis_freq
;
67 unsigned int standby_idle_dis_freq
;
70 static int rk3399_dmcfreq_target(struct device
*dev
, unsigned long *freq
,
73 struct rk3399_dmcfreq
*dmcfreq
= dev_get_drvdata(dev
);
74 struct dev_pm_opp
*opp
;
75 unsigned long old_clk_rate
= dmcfreq
->rate
;
76 unsigned long target_volt
, target_rate
;
77 unsigned int ddrcon_mhz
;
78 struct arm_smccc_res res
;
85 opp
= devfreq_recommended_opp(dev
, freq
, flags
);
89 target_rate
= dev_pm_opp_get_freq(opp
);
90 target_volt
= dev_pm_opp_get_voltage(opp
);
93 if (dmcfreq
->rate
== target_rate
)
96 mutex_lock(&dmcfreq
->lock
);
99 * Ensure power-domain transitions don't interfere with ARM Trusted
100 * Firmware power-domain idling.
102 err
= rockchip_pmu_block();
104 dev_err(dev
, "Failed to block PMU: %d\n", err
);
109 * Some idle parameters may be based on the DDR controller clock, which
110 * is half of the DDR frequency.
111 * pd_idle and standby_idle are based on the controller clock cycle.
112 * sr_idle_cycle, sr_mc_gate_idle_cycle, and srpd_lite_idle_cycle
113 * are based on the 1024 controller clock cycle
115 ddrcon_mhz
= target_rate
/ USEC_PER_SEC
/ 2;
117 u32p_replace_bits(&odt_pd_arg1
,
118 NS_TO_CYCLE(dmcfreq
->pd_idle_ns
, ddrcon_mhz
),
119 RK3399_SET_ODT_PD_1_PD_IDLE
);
120 u32p_replace_bits(&odt_pd_arg0
,
121 NS_TO_CYCLE(dmcfreq
->standby_idle_ns
, ddrcon_mhz
),
122 RK3399_SET_ODT_PD_0_STANDBY_IDLE
);
123 u32p_replace_bits(&odt_pd_arg0
,
124 DIV_ROUND_UP(NS_TO_CYCLE(dmcfreq
->sr_idle_ns
,
126 RK3399_SET_ODT_PD_0_SR_IDLE
);
127 u32p_replace_bits(&odt_pd_arg0
,
128 DIV_ROUND_UP(NS_TO_CYCLE(dmcfreq
->sr_mc_gate_idle_ns
,
130 RK3399_SET_ODT_PD_0_SR_MC_GATE_IDLE
);
131 u32p_replace_bits(&odt_pd_arg1
,
132 DIV_ROUND_UP(NS_TO_CYCLE(dmcfreq
->srpd_lite_idle_ns
,
134 RK3399_SET_ODT_PD_1_SRPD_LITE_IDLE
);
136 if (dmcfreq
->regmap_pmu
) {
137 if (target_rate
>= dmcfreq
->sr_idle_dis_freq
)
138 odt_pd_arg0
&= ~RK3399_SET_ODT_PD_0_SR_IDLE
;
140 if (target_rate
>= dmcfreq
->sr_mc_gate_idle_dis_freq
)
141 odt_pd_arg0
&= ~RK3399_SET_ODT_PD_0_SR_MC_GATE_IDLE
;
143 if (target_rate
>= dmcfreq
->standby_idle_dis_freq
)
144 odt_pd_arg0
&= ~RK3399_SET_ODT_PD_0_STANDBY_IDLE
;
146 if (target_rate
>= dmcfreq
->pd_idle_dis_freq
)
147 odt_pd_arg1
&= ~RK3399_SET_ODT_PD_1_PD_IDLE
;
149 if (target_rate
>= dmcfreq
->srpd_lite_idle_dis_freq
)
150 odt_pd_arg1
&= ~RK3399_SET_ODT_PD_1_SRPD_LITE_IDLE
;
152 if (target_rate
>= dmcfreq
->odt_dis_freq
)
153 odt_pd_arg2
|= RK3399_SET_ODT_PD_2_ODT_ENABLE
;
156 * This makes a SMC call to the TF-A to set the DDR PD
157 * (power-down) timings and to enable or disable the
158 * ODT (on-die termination) resistors.
160 arm_smccc_smc(ROCKCHIP_SIP_DRAM_FREQ
, odt_pd_arg0
, odt_pd_arg1
,
161 ROCKCHIP_SIP_CONFIG_DRAM_SET_ODT_PD
, odt_pd_arg2
,
166 * If frequency scaling from low to high, adjust voltage first.
167 * If frequency scaling from high to low, adjust frequency first.
169 if (old_clk_rate
< target_rate
) {
170 err
= regulator_set_voltage(dmcfreq
->vdd_center
, target_volt
,
173 dev_err(dev
, "Cannot set voltage %lu uV\n",
179 err
= clk_set_rate(dmcfreq
->dmc_clk
, target_rate
);
181 dev_err(dev
, "Cannot set frequency %lu (%d)\n", target_rate
,
183 regulator_set_voltage(dmcfreq
->vdd_center
, dmcfreq
->volt
,
189 * Check the dpll rate,
190 * There only two result we will get,
191 * 1. Ddr frequency scaling fail, we still get the old rate.
192 * 2. Ddr frequency scaling sucessful, we get the rate we set.
194 dmcfreq
->rate
= clk_get_rate(dmcfreq
->dmc_clk
);
196 /* If get the incorrect rate, set voltage to old value. */
197 if (dmcfreq
->rate
!= target_rate
) {
198 dev_err(dev
, "Got wrong frequency, Request %lu, Current %lu\n",
199 target_rate
, dmcfreq
->rate
);
200 regulator_set_voltage(dmcfreq
->vdd_center
, dmcfreq
->volt
,
203 } else if (old_clk_rate
> target_rate
)
204 err
= regulator_set_voltage(dmcfreq
->vdd_center
, target_volt
,
207 dev_err(dev
, "Cannot set voltage %lu uV\n", target_volt
);
209 dmcfreq
->rate
= target_rate
;
210 dmcfreq
->volt
= target_volt
;
213 rockchip_pmu_unblock();
215 mutex_unlock(&dmcfreq
->lock
);
219 static int rk3399_dmcfreq_get_dev_status(struct device
*dev
,
220 struct devfreq_dev_status
*stat
)
222 struct rk3399_dmcfreq
*dmcfreq
= dev_get_drvdata(dev
);
223 struct devfreq_event_data edata
;
226 ret
= devfreq_event_get_event(dmcfreq
->edev
, &edata
);
230 stat
->current_frequency
= dmcfreq
->rate
;
231 stat
->busy_time
= edata
.load_count
;
232 stat
->total_time
= edata
.total_count
;
237 static int rk3399_dmcfreq_get_cur_freq(struct device
*dev
, unsigned long *freq
)
239 struct rk3399_dmcfreq
*dmcfreq
= dev_get_drvdata(dev
);
241 *freq
= dmcfreq
->rate
;
246 static __maybe_unused
int rk3399_dmcfreq_suspend(struct device
*dev
)
248 struct rk3399_dmcfreq
*dmcfreq
= dev_get_drvdata(dev
);
251 ret
= devfreq_event_disable_edev(dmcfreq
->edev
);
253 dev_err(dev
, "failed to disable the devfreq-event devices\n");
257 ret
= devfreq_suspend_device(dmcfreq
->devfreq
);
259 dev_err(dev
, "failed to suspend the devfreq devices\n");
266 static __maybe_unused
int rk3399_dmcfreq_resume(struct device
*dev
)
268 struct rk3399_dmcfreq
*dmcfreq
= dev_get_drvdata(dev
);
271 ret
= devfreq_event_enable_edev(dmcfreq
->edev
);
273 dev_err(dev
, "failed to enable the devfreq-event devices\n");
277 ret
= devfreq_resume_device(dmcfreq
->devfreq
);
279 dev_err(dev
, "failed to resume the devfreq devices\n");
285 static SIMPLE_DEV_PM_OPS(rk3399_dmcfreq_pm
, rk3399_dmcfreq_suspend
,
286 rk3399_dmcfreq_resume
);
288 static int rk3399_dmcfreq_of_props(struct rk3399_dmcfreq
*data
,
289 struct device_node
*np
)
294 * These are all optional, and serve as minimum bounds. Give them large
295 * (i.e., never "disabled") values if the DT doesn't specify one.
297 data
->pd_idle_dis_freq
=
298 data
->sr_idle_dis_freq
=
299 data
->sr_mc_gate_idle_dis_freq
=
300 data
->srpd_lite_idle_dis_freq
=
301 data
->standby_idle_dis_freq
= UINT_MAX
;
303 ret
|= of_property_read_u32(np
, "rockchip,pd-idle-ns",
305 ret
|= of_property_read_u32(np
, "rockchip,sr-idle-ns",
307 ret
|= of_property_read_u32(np
, "rockchip,sr-mc-gate-idle-ns",
308 &data
->sr_mc_gate_idle_ns
);
309 ret
|= of_property_read_u32(np
, "rockchip,srpd-lite-idle-ns",
310 &data
->srpd_lite_idle_ns
);
311 ret
|= of_property_read_u32(np
, "rockchip,standby-idle-ns",
312 &data
->standby_idle_ns
);
313 ret
|= of_property_read_u32(np
, "rockchip,ddr3_odt_dis_freq",
314 &data
->ddr3_odt_dis_freq
);
315 ret
|= of_property_read_u32(np
, "rockchip,lpddr3_odt_dis_freq",
316 &data
->lpddr3_odt_dis_freq
);
317 ret
|= of_property_read_u32(np
, "rockchip,lpddr4_odt_dis_freq",
318 &data
->lpddr4_odt_dis_freq
);
320 ret
|= of_property_read_u32(np
, "rockchip,pd-idle-dis-freq-hz",
321 &data
->pd_idle_dis_freq
);
322 ret
|= of_property_read_u32(np
, "rockchip,sr-idle-dis-freq-hz",
323 &data
->sr_idle_dis_freq
);
324 ret
|= of_property_read_u32(np
, "rockchip,sr-mc-gate-idle-dis-freq-hz",
325 &data
->sr_mc_gate_idle_dis_freq
);
326 ret
|= of_property_read_u32(np
, "rockchip,srpd-lite-idle-dis-freq-hz",
327 &data
->srpd_lite_idle_dis_freq
);
328 ret
|= of_property_read_u32(np
, "rockchip,standby-idle-dis-freq-hz",
329 &data
->standby_idle_dis_freq
);
334 static int rk3399_dmcfreq_probe(struct platform_device
*pdev
)
336 struct arm_smccc_res res
;
337 struct device
*dev
= &pdev
->dev
;
338 struct device_node
*np
= pdev
->dev
.of_node
, *node
;
339 struct rk3399_dmcfreq
*data
;
341 struct dev_pm_opp
*opp
;
345 data
= devm_kzalloc(dev
, sizeof(struct rk3399_dmcfreq
), GFP_KERNEL
);
349 mutex_init(&data
->lock
);
351 data
->vdd_center
= devm_regulator_get(dev
, "center");
352 if (IS_ERR(data
->vdd_center
))
353 return dev_err_probe(dev
, PTR_ERR(data
->vdd_center
),
354 "Cannot get the regulator \"center\"\n");
356 data
->dmc_clk
= devm_clk_get(dev
, "dmc_clk");
357 if (IS_ERR(data
->dmc_clk
))
358 return dev_err_probe(dev
, PTR_ERR(data
->dmc_clk
),
359 "Cannot get the clk dmc_clk\n");
361 data
->edev
= devfreq_event_get_edev_by_phandle(dev
, "devfreq-events", 0);
362 if (IS_ERR(data
->edev
))
363 return -EPROBE_DEFER
;
365 ret
= devfreq_event_enable_edev(data
->edev
);
367 dev_err(dev
, "failed to enable devfreq-event devices\n");
371 rk3399_dmcfreq_of_props(data
, np
);
373 node
= of_parse_phandle(np
, "rockchip,pmu", 0);
377 data
->regmap_pmu
= syscon_node_to_regmap(node
);
379 if (IS_ERR(data
->regmap_pmu
)) {
380 ret
= PTR_ERR(data
->regmap_pmu
);
384 regmap_read(data
->regmap_pmu
, RK3399_PMUGRF_OS_REG2
, &val
);
385 ddr_type
= FIELD_GET(RK3399_PMUGRF_OS_REG2_DDRTYPE
, val
);
388 case ROCKCHIP_DDRTYPE_DDR3
:
389 data
->odt_dis_freq
= data
->ddr3_odt_dis_freq
;
391 case ROCKCHIP_DDRTYPE_LPDDR3
:
392 data
->odt_dis_freq
= data
->lpddr3_odt_dis_freq
;
394 case ROCKCHIP_DDRTYPE_LPDDR4
:
395 data
->odt_dis_freq
= data
->lpddr4_odt_dis_freq
;
403 arm_smccc_smc(ROCKCHIP_SIP_DRAM_FREQ
, 0, 0,
404 ROCKCHIP_SIP_CONFIG_DRAM_INIT
,
408 * We add a devfreq driver to our parent since it has a device tree node
409 * with operating points.
411 if (devm_pm_opp_of_add_table(dev
)) {
412 dev_err(dev
, "Invalid operating-points in device tree.\n");
417 data
->ondemand_data
.upthreshold
= 25;
418 data
->ondemand_data
.downdifferential
= 15;
420 data
->rate
= clk_get_rate(data
->dmc_clk
);
422 opp
= devfreq_recommended_opp(dev
, &data
->rate
, 0);
428 data
->rate
= dev_pm_opp_get_freq(opp
);
429 data
->volt
= dev_pm_opp_get_voltage(opp
);
432 data
->profile
= (struct devfreq_dev_profile
) {
434 .target
= rk3399_dmcfreq_target
,
435 .get_dev_status
= rk3399_dmcfreq_get_dev_status
,
436 .get_cur_freq
= rk3399_dmcfreq_get_cur_freq
,
437 .initial_freq
= data
->rate
,
440 data
->devfreq
= devm_devfreq_add_device(dev
,
442 DEVFREQ_GOV_SIMPLE_ONDEMAND
,
443 &data
->ondemand_data
);
444 if (IS_ERR(data
->devfreq
)) {
445 ret
= PTR_ERR(data
->devfreq
);
449 devm_devfreq_register_opp_notifier(dev
, data
->devfreq
);
452 platform_set_drvdata(pdev
, data
);
457 devfreq_event_disable_edev(data
->edev
);
462 static void rk3399_dmcfreq_remove(struct platform_device
*pdev
)
464 struct rk3399_dmcfreq
*dmcfreq
= dev_get_drvdata(&pdev
->dev
);
466 devfreq_event_disable_edev(dmcfreq
->edev
);
469 static const struct of_device_id rk3399dmc_devfreq_of_match
[] = {
470 { .compatible
= "rockchip,rk3399-dmc" },
473 MODULE_DEVICE_TABLE(of
, rk3399dmc_devfreq_of_match
);
475 static struct platform_driver rk3399_dmcfreq_driver
= {
476 .probe
= rk3399_dmcfreq_probe
,
477 .remove
= rk3399_dmcfreq_remove
,
479 .name
= "rk3399-dmc-freq",
480 .pm
= &rk3399_dmcfreq_pm
,
481 .of_match_table
= rk3399dmc_devfreq_of_match
,
484 module_platform_driver(rk3399_dmcfreq_driver
);
486 MODULE_LICENSE("GPL v2");
487 MODULE_AUTHOR("Lin Huang <hl@rock-chips.com>");
488 MODULE_DESCRIPTION("RK3399 dmcfreq driver with devfreq framework");