2 * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd.
3 * Author: Lin Huang <hl@rock-chips.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 #include <dt-bindings/clock/rockchip-ddr.h>
16 #include <linux/arm-smccc.h>
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/devfreq.h>
20 #include <linux/devfreq-event.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_opp.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/rockchip/rockchip_sip.h>
28 #include <linux/rwsem.h>
29 #include <linux/slab.h>
30 #include <linux/suspend.h>
32 #include <soc/rockchip/rkfb_dmc.h>
33 #include <soc/rockchip/rockchip_sip.h>
34 #include <soc/rockchip/scpi.h>
36 #define FIQ_INIT_HANDLER (0x1)
37 #define FIQ_CPU_TGT_BOOT (0x0) /* to booting cpu */
38 #define FIQ_NUM_FOR_DCF (143) /* NA irq map to fiq for dcf */
40 struct rk3368_dram_timing {
44 u32 dram_dll_dis_freq;
46 u32 dram_odt_dis_freq;
59 struct rk3399_dram_timing {
60 unsigned int ddr3_speed_bin;
63 unsigned int sr_mc_gate_idle;
64 unsigned int srpd_lite_idle;
65 unsigned int standby_idle;
66 unsigned int dram_dll_dis_freq;
67 unsigned int phy_dll_dis_freq;
68 unsigned int ddr3_odt_dis_freq;
69 unsigned int ddr3_drv;
70 unsigned int ddr3_odt;
71 unsigned int phy_ddr3_ca_drv;
72 unsigned int phy_ddr3_dq_drv;
73 unsigned int phy_ddr3_odt;
74 unsigned int lpddr3_odt_dis_freq;
75 unsigned int lpddr3_drv;
76 unsigned int lpddr3_odt;
77 unsigned int phy_lpddr3_ca_drv;
78 unsigned int phy_lpddr3_dq_drv;
79 unsigned int phy_lpddr3_odt;
80 unsigned int lpddr4_odt_dis_freq;
81 unsigned int lpddr4_drv;
82 unsigned int lpddr4_dq_odt;
83 unsigned int lpddr4_ca_odt;
84 unsigned int phy_lpddr4_ca_drv;
85 unsigned int phy_lpddr4_ck_cs_drv;
86 unsigned int phy_lpddr4_dq_drv;
87 unsigned int phy_lpddr4_odt;
90 struct rockchip_dmcfreq {
92 struct devfreq *devfreq;
93 struct devfreq_simple_ondemand_data ondemand_data;
95 struct devfreq_event_dev *edev;
96 struct mutex lock; /* scaling frequency lock */
97 struct dram_timing *timing;
98 struct regulator *vdd_center;
99 unsigned long rate, target_rate;
100 unsigned long volt, target_volt;
103 static int rockchip_dmcfreq_target(struct device *dev, unsigned long *freq,
106 struct rockchip_dmcfreq *dmcfreq = dev_get_drvdata(dev);
107 struct dev_pm_opp *opp;
108 unsigned long old_clk_rate = dmcfreq->rate;
109 unsigned long temp_rate, target_volt, target_rate;
113 opp = devfreq_recommended_opp(dev, freq, flags);
119 temp_rate = dev_pm_opp_get_freq(opp);
120 target_rate = clk_round_rate(dmcfreq->dmc_clk, temp_rate);
121 if ((long)target_rate <= 0)
122 target_rate = temp_rate;
123 target_volt = dev_pm_opp_get_voltage(opp);
127 if (dmcfreq->rate == target_rate) {
128 if (dmcfreq->volt == target_volt)
130 err = regulator_set_voltage(dmcfreq->vdd_center, target_volt,
133 dev_err(dev, "Cannot set voltage %lu uV\n",
139 mutex_lock(&dmcfreq->lock);
142 * If frequency scaling from low to high, adjust voltage first.
143 * If frequency scaling from high to low, adjust frequency first.
145 if (old_clk_rate < target_rate) {
146 err = regulator_set_voltage(dmcfreq->vdd_center, target_volt,
149 dev_err(dev, "Cannot set voltage %lu uV\n",
155 err = clk_set_rate(dmcfreq->dmc_clk, target_rate);
157 dev_err(dev, "Cannot set frequency %lu (%d)\n",
159 regulator_set_voltage(dmcfreq->vdd_center, dmcfreq->volt,
165 * Check the dpll rate,
166 * There only two result we will get,
167 * 1. Ddr frequency scaling fail, we still get the old rate.
168 * 2. Ddr frequency scaling sucessful, we get the rate we set.
170 dmcfreq->rate = clk_get_rate(dmcfreq->dmc_clk);
172 /* If get the incorrect rate, set voltage to old value. */
173 if (dmcfreq->rate != target_rate) {
174 dev_err(dev, "Get wrong frequency, Request %lu, Current %lu\n",
175 target_rate, dmcfreq->rate);
176 regulator_set_voltage(dmcfreq->vdd_center, dmcfreq->volt,
179 } else if (old_clk_rate > target_rate) {
180 err = regulator_set_voltage(dmcfreq->vdd_center, target_volt,
183 dev_err(dev, "Cannot set vol %lu uV\n", target_volt);
188 dmcfreq->volt = target_volt;
190 mutex_unlock(&dmcfreq->lock);
194 static int rockchip_dmcfreq_get_dev_status(struct device *dev,
195 struct devfreq_dev_status *stat)
197 struct rockchip_dmcfreq *dmcfreq = dev_get_drvdata(dev);
198 struct devfreq_event_data edata;
201 ret = devfreq_event_get_event(dmcfreq->edev, &edata);
205 stat->current_frequency = dmcfreq->rate;
206 stat->busy_time = edata.load_count;
207 stat->total_time = edata.total_count;
212 static int rockchip_dmcfreq_get_cur_freq(struct device *dev,
215 struct rockchip_dmcfreq *dmcfreq = dev_get_drvdata(dev);
217 *freq = dmcfreq->rate;
222 static struct devfreq_dev_profile rockchip_devfreq_dmc_profile = {
224 .target = rockchip_dmcfreq_target,
225 .get_dev_status = rockchip_dmcfreq_get_dev_status,
226 .get_cur_freq = rockchip_dmcfreq_get_cur_freq,
229 static __maybe_unused int rockchip_dmcfreq_suspend(struct device *dev)
231 struct rockchip_dmcfreq *dmcfreq = dev_get_drvdata(dev);
234 ret = devfreq_event_disable_edev(dmcfreq->edev);
236 dev_err(dev, "failed to disable the devfreq-event devices\n");
240 ret = devfreq_suspend_device(dmcfreq->devfreq);
242 dev_err(dev, "failed to suspend the devfreq devices\n");
249 static __maybe_unused int rockchip_dmcfreq_resume(struct device *dev)
251 struct rockchip_dmcfreq *dmcfreq = dev_get_drvdata(dev);
254 ret = devfreq_event_enable_edev(dmcfreq->edev);
256 dev_err(dev, "failed to enable the devfreq-event devices\n");
260 ret = devfreq_resume_device(dmcfreq->devfreq);
262 dev_err(dev, "failed to resume the devfreq devices\n");
268 static SIMPLE_DEV_PM_OPS(rockchip_dmcfreq_pm, rockchip_dmcfreq_suspend,
269 rockchip_dmcfreq_resume);
271 static int rockchip_dmcfreq_init_freq_table(struct device *dev,
272 struct devfreq_dev_profile *devp)
276 unsigned long freq = 0;
277 struct dev_pm_opp *opp;
280 count = dev_pm_opp_get_opp_count(dev);
287 devp->freq_table = kmalloc_array(count, sizeof(devp->freq_table[0]),
289 if (!devp->freq_table)
293 for (i = 0; i < count; i++, freq++) {
294 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
298 devp->freq_table[i] = freq;
303 dev_warn(dev, "Unable to enumerate all OPPs (%d!=%d)\n",
310 static struct rk3368_dram_timing *of_get_rk3368_timings(struct device *dev,
311 struct device_node *np)
313 struct rk3368_dram_timing *timing = NULL;
314 struct device_node *np_tim;
317 np_tim = of_parse_phandle(np, "ddr_timing", 0);
319 timing = devm_kzalloc(dev, sizeof(*timing), GFP_KERNEL);
323 ret |= of_property_read_u32(np_tim, "dram_spd_bin",
324 &timing->dram_spd_bin);
325 ret |= of_property_read_u32(np_tim, "sr_idle",
327 ret |= of_property_read_u32(np_tim, "pd_idle",
329 ret |= of_property_read_u32(np_tim, "dram_dll_disb_freq",
330 &timing->dram_dll_dis_freq);
331 ret |= of_property_read_u32(np_tim, "phy_dll_disb_freq",
332 &timing->phy_dll_dis_freq);
333 ret |= of_property_read_u32(np_tim, "dram_odt_disb_freq",
334 &timing->dram_odt_dis_freq);
335 ret |= of_property_read_u32(np_tim, "phy_odt_disb_freq",
336 &timing->phy_odt_dis_freq);
337 ret |= of_property_read_u32(np_tim, "ddr3_drv",
339 ret |= of_property_read_u32(np_tim, "ddr3_odt",
341 ret |= of_property_read_u32(np_tim, "lpddr3_drv",
342 &timing->lpddr3_drv);
343 ret |= of_property_read_u32(np_tim, "lpddr3_odt",
344 &timing->lpddr3_odt);
345 ret |= of_property_read_u32(np_tim, "lpddr2_drv",
346 &timing->lpddr2_drv);
347 ret |= of_property_read_u32(np_tim, "phy_clk_drv",
348 &timing->phy_clk_drv);
349 ret |= of_property_read_u32(np_tim, "phy_cmd_drv",
350 &timing->phy_cmd_drv);
351 ret |= of_property_read_u32(np_tim, "phy_dqs_drv",
352 &timing->phy_dqs_drv);
353 ret |= of_property_read_u32(np_tim, "phy_odt",
356 devm_kfree(dev, timing);
365 devm_kfree(dev, timing);
372 static int rk3368_dmc_init(struct platform_device *pdev)
374 struct device *dev = &pdev->dev;
375 struct device_node *np = pdev->dev.of_node;
376 struct arm_smccc_res res;
377 struct rk3368_dram_timing *dram_timing;
378 struct clk *pclk_phy, *pclk_upctl;
385 pclk_phy = devm_clk_get(dev, "pclk_phy");
386 if (IS_ERR(pclk_phy)) {
387 dev_err(dev, "Cannot get the clk pclk_phy\n");
388 return PTR_ERR(pclk_phy);
390 ret = clk_prepare_enable(pclk_phy);
392 dev_err(dev, "failed to prepare/enable pclk_phy\n");
395 pclk_upctl = devm_clk_get(dev, "pclk_upctl");
396 if (IS_ERR(pclk_phy)) {
397 dev_err(dev, "Cannot get the clk pclk_upctl\n");
398 return PTR_ERR(pclk_upctl);
400 ret = clk_prepare_enable(pclk_upctl);
402 dev_err(dev, "failed to prepare/enable pclk_upctl\n");
407 * Get dram timing and pass it to arm trust firmware,
408 * the dram drvier in arm trust firmware will get these
409 * timing and to do dram initial.
411 dram_timing = of_get_rk3368_timings(dev, np);
413 dram_spd_bin = dram_timing->dram_spd_bin;
414 if (scpi_ddr_send_timing((u32 *)dram_timing,
415 sizeof(struct rk3368_dram_timing)))
416 dev_err(dev, "send ddr timing timeout\n");
418 dev_err(dev, "get ddr timing from dts error\n");
419 dram_spd_bin = DDR3_DEFAULT;
422 res = sip_smc_mcu_el3fiq(FIQ_INIT_HANDLER,
425 if ((res.a0) || (res.a1 == 0) || (res.a1 > 0x80000))
426 dev_err(dev, "Trust version error, pls check trust version\n");
427 addr_mcu_el3 = res.a1;
429 if (of_property_read_u32(np, "vop-dclk-mode", &dclk_mode) == 0)
430 scpi_ddr_dclk_mode(dclk_mode);
434 if (scpi_ddr_init(dram_spd_bin, 0, lcdc_type,
436 dev_err(dev, "ddr init error\n");
438 dev_dbg(dev, ("%s out\n"), __func__);
443 static struct rk3399_dram_timing *of_get_rk3399_timings(struct device *dev,
444 struct device_node *np)
446 struct rk3399_dram_timing *timing = NULL;
447 struct device_node *np_tim;
450 np_tim = of_parse_phandle(np, "ddr_timing", 0);
452 timing = devm_kzalloc(dev, sizeof(*timing), GFP_KERNEL);
456 ret = of_property_read_u32(np_tim, "ddr3_speed_bin",
457 &timing->ddr3_speed_bin);
458 ret |= of_property_read_u32(np_tim, "pd_idle",
460 ret |= of_property_read_u32(np_tim, "sr_idle",
462 ret |= of_property_read_u32(np_tim, "sr_mc_gate_idle",
463 &timing->sr_mc_gate_idle);
464 ret |= of_property_read_u32(np_tim, "srpd_lite_idle",
465 &timing->srpd_lite_idle);
466 ret |= of_property_read_u32(np_tim, "standby_idle",
467 &timing->standby_idle);
468 ret |= of_property_read_u32(np_tim, "dram_dll_dis_freq",
469 &timing->dram_dll_dis_freq);
470 ret |= of_property_read_u32(np_tim, "phy_dll_dis_freq",
471 &timing->phy_dll_dis_freq);
472 ret |= of_property_read_u32(np_tim, "ddr3_odt_dis_freq",
473 &timing->ddr3_odt_dis_freq);
474 ret |= of_property_read_u32(np_tim, "ddr3_drv",
476 ret |= of_property_read_u32(np_tim, "ddr3_odt",
478 ret |= of_property_read_u32(np_tim, "phy_ddr3_ca_drv",
479 &timing->phy_ddr3_ca_drv);
480 ret |= of_property_read_u32(np_tim, "phy_ddr3_dq_drv",
481 &timing->phy_ddr3_dq_drv);
482 ret |= of_property_read_u32(np_tim, "phy_ddr3_odt",
483 &timing->phy_ddr3_odt);
484 ret |= of_property_read_u32(np_tim, "lpddr3_odt_dis_freq",
485 &timing->lpddr3_odt_dis_freq);
486 ret |= of_property_read_u32(np_tim, "lpddr3_drv",
487 &timing->lpddr3_drv);
488 ret |= of_property_read_u32(np_tim, "lpddr3_odt",
489 &timing->lpddr3_odt);
490 ret |= of_property_read_u32(np_tim, "phy_lpddr3_ca_drv",
491 &timing->phy_lpddr3_ca_drv);
492 ret |= of_property_read_u32(np_tim, "phy_lpddr3_dq_drv",
493 &timing->phy_lpddr3_dq_drv);
494 ret |= of_property_read_u32(np_tim, "phy_lpddr3_odt",
495 &timing->phy_lpddr3_odt);
496 ret |= of_property_read_u32(np_tim, "lpddr4_odt_dis_freq",
497 &timing->lpddr4_odt_dis_freq);
498 ret |= of_property_read_u32(np_tim, "lpddr4_drv",
499 &timing->lpddr4_drv);
500 ret |= of_property_read_u32(np_tim, "lpddr4_dq_odt",
501 &timing->lpddr4_dq_odt);
502 ret |= of_property_read_u32(np_tim, "lpddr4_ca_odt",
503 &timing->lpddr4_ca_odt);
504 ret |= of_property_read_u32(np_tim, "phy_lpddr4_ca_drv",
505 &timing->phy_lpddr4_ca_drv);
506 ret |= of_property_read_u32(np_tim, "phy_lpddr4_ck_cs_drv",
507 &timing->phy_lpddr4_ck_cs_drv);
508 ret |= of_property_read_u32(np_tim, "phy_lpddr4_dq_drv",
509 &timing->phy_lpddr4_dq_drv);
510 ret |= of_property_read_u32(np_tim, "phy_lpddr4_odt",
511 &timing->phy_lpddr4_odt);
513 devm_kfree(dev, timing);
522 devm_kfree(dev, timing);
529 static int rk3399_dmc_init(struct platform_device *pdev)
531 struct device *dev = &pdev->dev;
532 struct device_node *np = pdev->dev.of_node;
533 struct arm_smccc_res res;
534 struct rk3399_dram_timing *dram_timing;
539 * Get dram timing and pass it to arm trust firmware,
540 * the dram drvier in arm trust firmware will get these
541 * timing and to do dram initial.
543 dram_timing = of_get_rk3399_timings(dev, np);
545 timing = (u32 *)dram_timing;
546 size = sizeof(struct rk3399_dram_timing) / 4;
547 for (index = 0; index < size; index++) {
548 arm_smccc_smc(ROCKCHIP_SIP_DRAM_FREQ, *timing++, index,
549 ROCKCHIP_SIP_CONFIG_DRAM_SET_PARAM,
552 dev_err(dev, "Failed to set dram param: %ld\n",
559 arm_smccc_smc(ROCKCHIP_SIP_DRAM_FREQ, 0, 0,
560 ROCKCHIP_SIP_CONFIG_DRAM_INIT,
566 static const struct of_device_id rockchip_dmcfreq_of_match[] = {
567 { .compatible = "rockchip,rk3368-dmc", .data = rk3368_dmc_init },
568 { .compatible = "rockchip,rk3399-dmc", .data = rk3399_dmc_init },
571 MODULE_DEVICE_TABLE(of, rockchip_dmcfreq_of_match);
573 static int rockchip_dmcfreq_probe(struct platform_device *pdev)
575 struct device *dev = &pdev->dev;
576 struct device_node *np = pdev->dev.of_node;
577 struct rockchip_dmcfreq *data;
578 struct devfreq_dev_profile *devp = &rockchip_devfreq_dmc_profile;
579 const struct of_device_id *match;
580 int (*init)(struct platform_device *pdev,
581 struct rockchip_dmcfreq *data);
584 data = devm_kzalloc(dev, sizeof(struct rockchip_dmcfreq), GFP_KERNEL);
588 mutex_init(&data->lock);
590 data->vdd_center = devm_regulator_get(dev, "center");
591 if (IS_ERR(data->vdd_center)) {
592 dev_err(dev, "Cannot get the regulator \"center\"\n");
593 return PTR_ERR(data->vdd_center);
596 data->dmc_clk = devm_clk_get(dev, "dmc_clk");
597 if (IS_ERR(data->dmc_clk)) {
598 dev_err(dev, "Cannot get the clk dmc_clk\n");
599 return PTR_ERR(data->dmc_clk);
602 data->edev = devfreq_event_get_edev_by_phandle(dev, 0);
603 if (IS_ERR(data->edev))
604 return -EPROBE_DEFER;
606 ret = devfreq_event_enable_edev(data->edev);
608 dev_err(dev, "failed to enable devfreq-event devices\n");
612 match = of_match_node(rockchip_dmcfreq_of_match, pdev->dev.of_node);
616 if (init(pdev, data))
622 * We add a devfreq driver to our parent since it has a device tree node
623 * with operating points.
625 if (dev_pm_opp_of_add_table(dev)) {
626 dev_err(dev, "Invalid operating-points in device tree.\n");
630 if (rockchip_dmcfreq_init_freq_table(dev, devp))
633 of_property_read_u32(np, "upthreshold",
634 &data->ondemand_data.upthreshold);
635 of_property_read_u32(np, "downdifferential",
636 &data->ondemand_data.downdifferential);
638 data->rate = clk_get_rate(data->dmc_clk);
639 data->volt = regulator_get_voltage(data->vdd_center);
641 devp->initial_freq = data->rate;
642 data->devfreq = devm_devfreq_add_device(dev, devp,
644 &data->ondemand_data);
645 if (IS_ERR(data->devfreq))
646 return PTR_ERR(data->devfreq);
647 devm_devfreq_register_opp_notifier(dev, data->devfreq);
649 data->devfreq->min_freq = devp->freq_table[0];
650 data->devfreq->max_freq =
651 devp->freq_table[devp->max_state ? devp->max_state - 1 : 0];
654 platform_set_drvdata(pdev, data);
656 if (vop_register_dmc())
657 dev_err(dev, "fail to register notify to vop.\n");
662 static struct platform_driver rockchip_dmcfreq_driver = {
663 .probe = rockchip_dmcfreq_probe,
665 .name = "rockchip-dmc",
666 .pm = &rockchip_dmcfreq_pm,
667 .of_match_table = rockchip_dmcfreq_of_match,
670 module_platform_driver(rockchip_dmcfreq_driver);
672 MODULE_LICENSE("GPL v2");
673 MODULE_AUTHOR("Lin Huang <hl@rock-chips.com>");
674 MODULE_DESCRIPTION("rockchip dmcfreq driver with devfreq framework");