rk3399_dmc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd.
  3. * Author: Lin Huang <hl@rock-chips.com>
  4. *
  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.
  8. *
  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
  12. * more details.
  13. */
  14. #include <linux/arm-smccc.h>
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/devfreq.h>
  18. #include <linux/devfreq-event.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_opp.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/rwsem.h>
  26. #include <linux/suspend.h>
  27. #include <soc/rockchip/rockchip_sip.h>
  28. struct dram_timing {
  29. unsigned int ddr3_speed_bin;
  30. unsigned int pd_idle;
  31. unsigned int sr_idle;
  32. unsigned int sr_mc_gate_idle;
  33. unsigned int srpd_lite_idle;
  34. unsigned int standby_idle;
  35. unsigned int auto_pd_dis_freq;
  36. unsigned int dram_dll_dis_freq;
  37. unsigned int phy_dll_dis_freq;
  38. unsigned int ddr3_odt_dis_freq;
  39. unsigned int ddr3_drv;
  40. unsigned int ddr3_odt;
  41. unsigned int phy_ddr3_ca_drv;
  42. unsigned int phy_ddr3_dq_drv;
  43. unsigned int phy_ddr3_odt;
  44. unsigned int lpddr3_odt_dis_freq;
  45. unsigned int lpddr3_drv;
  46. unsigned int lpddr3_odt;
  47. unsigned int phy_lpddr3_ca_drv;
  48. unsigned int phy_lpddr3_dq_drv;
  49. unsigned int phy_lpddr3_odt;
  50. unsigned int lpddr4_odt_dis_freq;
  51. unsigned int lpddr4_drv;
  52. unsigned int lpddr4_dq_odt;
  53. unsigned int lpddr4_ca_odt;
  54. unsigned int phy_lpddr4_ca_drv;
  55. unsigned int phy_lpddr4_ck_cs_drv;
  56. unsigned int phy_lpddr4_dq_drv;
  57. unsigned int phy_lpddr4_odt;
  58. };
  59. struct rk3399_dmcfreq {
  60. struct device *dev;
  61. struct devfreq *devfreq;
  62. struct devfreq_simple_ondemand_data ondemand_data;
  63. struct clk *dmc_clk;
  64. struct devfreq_event_dev *edev;
  65. struct mutex lock;
  66. struct dram_timing timing;
  67. /*
  68. * DDR Converser of Frequency (DCF) is used to implement DDR frequency
  69. * conversion without the participation of CPU, we will implement and
  70. * control it in arm trust firmware.
  71. */
  72. wait_queue_head_t wait_dcf_queue;
  73. int irq;
  74. int wait_dcf_flag;
  75. struct regulator *vdd_center;
  76. unsigned long rate, target_rate;
  77. unsigned long volt, target_volt;
  78. struct dev_pm_opp *curr_opp;
  79. };
  80. static int rk3399_dmcfreq_target(struct device *dev, unsigned long *freq,
  81. u32 flags)
  82. {
  83. struct rk3399_dmcfreq *dmcfreq = dev_get_drvdata(dev);
  84. struct dev_pm_opp *opp;
  85. unsigned long old_clk_rate = dmcfreq->rate;
  86. unsigned long target_volt, target_rate;
  87. int err;
  88. rcu_read_lock();
  89. opp = devfreq_recommended_opp(dev, freq, flags);
  90. if (IS_ERR(opp)) {
  91. rcu_read_unlock();
  92. return PTR_ERR(opp);
  93. }
  94. target_rate = dev_pm_opp_get_freq(opp);
  95. target_volt = dev_pm_opp_get_voltage(opp);
  96. dmcfreq->rate = dev_pm_opp_get_freq(dmcfreq->curr_opp);
  97. dmcfreq->volt = dev_pm_opp_get_voltage(dmcfreq->curr_opp);
  98. rcu_read_unlock();
  99. if (dmcfreq->rate == target_rate)
  100. return 0;
  101. mutex_lock(&dmcfreq->lock);
  102. /*
  103. * If frequency scaling from low to high, adjust voltage first.
  104. * If frequency scaling from high to low, adjust frequency first.
  105. */
  106. if (old_clk_rate < target_rate) {
  107. err = regulator_set_voltage(dmcfreq->vdd_center, target_volt,
  108. target_volt);
  109. if (err) {
  110. dev_err(dev, "Cannot to set voltage %lu uV\n",
  111. target_volt);
  112. goto out;
  113. }
  114. }
  115. dmcfreq->wait_dcf_flag = 1;
  116. err = clk_set_rate(dmcfreq->dmc_clk, target_rate);
  117. if (err) {
  118. dev_err(dev, "Cannot to set frequency %lu (%d)\n",
  119. target_rate, err);
  120. regulator_set_voltage(dmcfreq->vdd_center, dmcfreq->volt,
  121. dmcfreq->volt);
  122. goto out;
  123. }
  124. /*
  125. * Wait until bcf irq happen, it means freq scaling finish in
  126. * arm trust firmware, use 100ms as timeout time.
  127. */
  128. if (!wait_event_timeout(dmcfreq->wait_dcf_queue,
  129. !dmcfreq->wait_dcf_flag, HZ / 10))
  130. dev_warn(dev, "Timeout waiting for dcf interrupt\n");
  131. /*
  132. * Check the dpll rate,
  133. * There only two result we will get,
  134. * 1. Ddr frequency scaling fail, we still get the old rate.
  135. * 2. Ddr frequency scaling sucessful, we get the rate we set.
  136. */
  137. dmcfreq->rate = clk_get_rate(dmcfreq->dmc_clk);
  138. /* If get the incorrect rate, set voltage to old value. */
  139. if (dmcfreq->rate != target_rate) {
  140. dev_err(dev, "Get wrong ddr frequency, Request frequency %lu,\
  141. Current frequency %lu\n", target_rate, dmcfreq->rate);
  142. regulator_set_voltage(dmcfreq->vdd_center, dmcfreq->volt,
  143. dmcfreq->volt);
  144. goto out;
  145. } else if (old_clk_rate > target_rate)
  146. err = regulator_set_voltage(dmcfreq->vdd_center, target_volt,
  147. target_volt);
  148. if (err)
  149. dev_err(dev, "Cannot to set vol %lu uV\n", target_volt);
  150. dmcfreq->curr_opp = opp;
  151. out:
  152. mutex_unlock(&dmcfreq->lock);
  153. return err;
  154. }
  155. static int rk3399_dmcfreq_get_dev_status(struct device *dev,
  156. struct devfreq_dev_status *stat)
  157. {
  158. struct rk3399_dmcfreq *dmcfreq = dev_get_drvdata(dev);
  159. struct devfreq_event_data edata;
  160. int ret = 0;
  161. ret = devfreq_event_get_event(dmcfreq->edev, &edata);
  162. if (ret < 0)
  163. return ret;
  164. stat->current_frequency = dmcfreq->rate;
  165. stat->busy_time = edata.load_count;
  166. stat->total_time = edata.total_count;
  167. return ret;
  168. }
  169. static int rk3399_dmcfreq_get_cur_freq(struct device *dev, unsigned long *freq)
  170. {
  171. struct rk3399_dmcfreq *dmcfreq = dev_get_drvdata(dev);
  172. *freq = dmcfreq->rate;
  173. return 0;
  174. }
  175. static struct devfreq_dev_profile rk3399_devfreq_dmc_profile = {
  176. .polling_ms = 200,
  177. .target = rk3399_dmcfreq_target,
  178. .get_dev_status = rk3399_dmcfreq_get_dev_status,
  179. .get_cur_freq = rk3399_dmcfreq_get_cur_freq,
  180. };
  181. static __maybe_unused int rk3399_dmcfreq_suspend(struct device *dev)
  182. {
  183. struct rk3399_dmcfreq *dmcfreq = dev_get_drvdata(dev);
  184. int ret = 0;
  185. ret = devfreq_event_disable_edev(dmcfreq->edev);
  186. if (ret < 0) {
  187. dev_err(dev, "failed to disable the devfreq-event devices\n");
  188. return ret;
  189. }
  190. ret = devfreq_suspend_device(dmcfreq->devfreq);
  191. if (ret < 0) {
  192. dev_err(dev, "failed to suspend the devfreq devices\n");
  193. return ret;
  194. }
  195. return 0;
  196. }
  197. static __maybe_unused int rk3399_dmcfreq_resume(struct device *dev)
  198. {
  199. struct rk3399_dmcfreq *dmcfreq = dev_get_drvdata(dev);
  200. int ret = 0;
  201. ret = devfreq_event_enable_edev(dmcfreq->edev);
  202. if (ret < 0) {
  203. dev_err(dev, "failed to enable the devfreq-event devices\n");
  204. return ret;
  205. }
  206. ret = devfreq_resume_device(dmcfreq->devfreq);
  207. if (ret < 0) {
  208. dev_err(dev, "failed to resume the devfreq devices\n");
  209. return ret;
  210. }
  211. return ret;
  212. }
  213. static SIMPLE_DEV_PM_OPS(rk3399_dmcfreq_pm, rk3399_dmcfreq_suspend,
  214. rk3399_dmcfreq_resume);
  215. static irqreturn_t rk3399_dmc_irq(int irq, void *dev_id)
  216. {
  217. struct rk3399_dmcfreq *dmcfreq = dev_id;
  218. struct arm_smccc_res res;
  219. dmcfreq->wait_dcf_flag = 0;
  220. wake_up(&dmcfreq->wait_dcf_queue);
  221. /* Clear the DCF interrupt */
  222. arm_smccc_smc(ROCKCHIP_SIP_DRAM_FREQ, 0, 0,
  223. ROCKCHIP_SIP_CONFIG_DRAM_CLR_IRQ,
  224. 0, 0, 0, 0, &res);
  225. return IRQ_HANDLED;
  226. }
  227. static int of_get_ddr_timings(struct dram_timing *timing,
  228. struct device_node *np)
  229. {
  230. int ret = 0;
  231. ret = of_property_read_u32(np, "rockchip,ddr3_speed_bin",
  232. &timing->ddr3_speed_bin);
  233. ret |= of_property_read_u32(np, "rockchip,pd_idle",
  234. &timing->pd_idle);
  235. ret |= of_property_read_u32(np, "rockchip,sr_idle",
  236. &timing->sr_idle);
  237. ret |= of_property_read_u32(np, "rockchip,sr_mc_gate_idle",
  238. &timing->sr_mc_gate_idle);
  239. ret |= of_property_read_u32(np, "rockchip,srpd_lite_idle",
  240. &timing->srpd_lite_idle);
  241. ret |= of_property_read_u32(np, "rockchip,standby_idle",
  242. &timing->standby_idle);
  243. ret |= of_property_read_u32(np, "rockchip,auto_pd_dis_freq",
  244. &timing->auto_pd_dis_freq);
  245. ret |= of_property_read_u32(np, "rockchip,dram_dll_dis_freq",
  246. &timing->dram_dll_dis_freq);
  247. ret |= of_property_read_u32(np, "rockchip,phy_dll_dis_freq",
  248. &timing->phy_dll_dis_freq);
  249. ret |= of_property_read_u32(np, "rockchip,ddr3_odt_dis_freq",
  250. &timing->ddr3_odt_dis_freq);
  251. ret |= of_property_read_u32(np, "rockchip,ddr3_drv",
  252. &timing->ddr3_drv);
  253. ret |= of_property_read_u32(np, "rockchip,ddr3_odt",
  254. &timing->ddr3_odt);
  255. ret |= of_property_read_u32(np, "rockchip,phy_ddr3_ca_drv",
  256. &timing->phy_ddr3_ca_drv);
  257. ret |= of_property_read_u32(np, "rockchip,phy_ddr3_dq_drv",
  258. &timing->phy_ddr3_dq_drv);
  259. ret |= of_property_read_u32(np, "rockchip,phy_ddr3_odt",
  260. &timing->phy_ddr3_odt);
  261. ret |= of_property_read_u32(np, "rockchip,lpddr3_odt_dis_freq",
  262. &timing->lpddr3_odt_dis_freq);
  263. ret |= of_property_read_u32(np, "rockchip,lpddr3_drv",
  264. &timing->lpddr3_drv);
  265. ret |= of_property_read_u32(np, "rockchip,lpddr3_odt",
  266. &timing->lpddr3_odt);
  267. ret |= of_property_read_u32(np, "rockchip,phy_lpddr3_ca_drv",
  268. &timing->phy_lpddr3_ca_drv);
  269. ret |= of_property_read_u32(np, "rockchip,phy_lpddr3_dq_drv",
  270. &timing->phy_lpddr3_dq_drv);
  271. ret |= of_property_read_u32(np, "rockchip,phy_lpddr3_odt",
  272. &timing->phy_lpddr3_odt);
  273. ret |= of_property_read_u32(np, "rockchip,lpddr4_odt_dis_freq",
  274. &timing->lpddr4_odt_dis_freq);
  275. ret |= of_property_read_u32(np, "rockchip,lpddr4_drv",
  276. &timing->lpddr4_drv);
  277. ret |= of_property_read_u32(np, "rockchip,lpddr4_dq_odt",
  278. &timing->lpddr4_dq_odt);
  279. ret |= of_property_read_u32(np, "rockchip,lpddr4_ca_odt",
  280. &timing->lpddr4_ca_odt);
  281. ret |= of_property_read_u32(np, "rockchip,phy_lpddr4_ca_drv",
  282. &timing->phy_lpddr4_ca_drv);
  283. ret |= of_property_read_u32(np, "rockchip,phy_lpddr4_ck_cs_drv",
  284. &timing->phy_lpddr4_ck_cs_drv);
  285. ret |= of_property_read_u32(np, "rockchip,phy_lpddr4_dq_drv",
  286. &timing->phy_lpddr4_dq_drv);
  287. ret |= of_property_read_u32(np, "rockchip,phy_lpddr4_odt",
  288. &timing->phy_lpddr4_odt);
  289. return ret;
  290. }
  291. static int rk3399_dmcfreq_probe(struct platform_device *pdev)
  292. {
  293. struct arm_smccc_res res;
  294. struct device *dev = &pdev->dev;
  295. struct device_node *np = pdev->dev.of_node;
  296. struct rk3399_dmcfreq *data;
  297. int ret, irq, index, size;
  298. uint32_t *timing;
  299. struct dev_pm_opp *opp;
  300. irq = platform_get_irq(pdev, 0);
  301. if (irq < 0) {
  302. dev_err(&pdev->dev, "Cannot get the dmc interrupt resource\n");
  303. return -EINVAL;
  304. }
  305. data = devm_kzalloc(dev, sizeof(struct rk3399_dmcfreq), GFP_KERNEL);
  306. if (!data)
  307. return -ENOMEM;
  308. mutex_init(&data->lock);
  309. data->vdd_center = devm_regulator_get(dev, "center");
  310. if (IS_ERR(data->vdd_center)) {
  311. dev_err(dev, "Cannot get the regulator \"center\"\n");
  312. return PTR_ERR(data->vdd_center);
  313. }
  314. data->dmc_clk = devm_clk_get(dev, "dmc_clk");
  315. if (IS_ERR(data->dmc_clk)) {
  316. dev_err(dev, "Cannot get the clk dmc_clk\n");
  317. return PTR_ERR(data->dmc_clk);
  318. };
  319. data->irq = irq;
  320. ret = devm_request_irq(dev, irq, rk3399_dmc_irq, 0,
  321. dev_name(dev), data);
  322. if (ret) {
  323. dev_err(dev, "Failed to request dmc irq: %d\n", ret);
  324. return ret;
  325. }
  326. init_waitqueue_head(&data->wait_dcf_queue);
  327. data->wait_dcf_flag = 0;
  328. data->edev = devfreq_event_get_edev_by_phandle(dev, 0);
  329. if (IS_ERR(data->edev))
  330. return -EPROBE_DEFER;
  331. ret = devfreq_event_enable_edev(data->edev);
  332. if (ret < 0) {
  333. dev_err(dev, "failed to enable devfreq-event devices\n");
  334. return ret;
  335. }
  336. /*
  337. * Get dram timing and pass it to arm trust firmware,
  338. * the dram drvier in arm trust firmware will get these
  339. * timing and to do dram initial.
  340. */
  341. if (!of_get_ddr_timings(&data->timing, np)) {
  342. timing = &data->timing.ddr3_speed_bin;
  343. size = sizeof(struct dram_timing) / 4;
  344. for (index = 0; index < size; index++) {
  345. arm_smccc_smc(ROCKCHIP_SIP_DRAM_FREQ, *timing++, index,
  346. ROCKCHIP_SIP_CONFIG_DRAM_SET_PARAM,
  347. 0, 0, 0, 0, &res);
  348. if (res.a0) {
  349. dev_err(dev, "Failed to set dram param: %ld\n",
  350. res.a0);
  351. return -EINVAL;
  352. }
  353. }
  354. }
  355. arm_smccc_smc(ROCKCHIP_SIP_DRAM_FREQ, 0, 0,
  356. ROCKCHIP_SIP_CONFIG_DRAM_INIT,
  357. 0, 0, 0, 0, &res);
  358. /*
  359. * We add a devfreq driver to our parent since it has a device tree node
  360. * with operating points.
  361. */
  362. if (dev_pm_opp_of_add_table(dev)) {
  363. dev_err(dev, "Invalid operating-points in device tree.\n");
  364. rcu_read_unlock();
  365. return -EINVAL;
  366. }
  367. of_property_read_u32(np, "upthreshold",
  368. &data->ondemand_data.upthreshold);
  369. of_property_read_u32(np, "downdifferential",
  370. &data->ondemand_data.downdifferential);
  371. data->rate = clk_get_rate(data->dmc_clk);
  372. rcu_read_lock();
  373. opp = devfreq_recommended_opp(dev, &data->rate, 0);
  374. if (IS_ERR(opp)) {
  375. rcu_read_unlock();
  376. return PTR_ERR(opp);
  377. }
  378. rcu_read_unlock();
  379. data->curr_opp = opp;
  380. rk3399_devfreq_dmc_profile.initial_freq = data->rate;
  381. data->devfreq = devfreq_add_device(dev,
  382. &rk3399_devfreq_dmc_profile,
  383. "simple_ondemand",
  384. &data->ondemand_data);
  385. if (IS_ERR(data->devfreq))
  386. return PTR_ERR(data->devfreq);
  387. devm_devfreq_register_opp_notifier(dev, data->devfreq);
  388. data->dev = dev;
  389. platform_set_drvdata(pdev, data);
  390. return 0;
  391. }
  392. static const struct of_device_id rk3399dmc_devfreq_of_match[] = {
  393. { .compatible = "rockchip,rk3399-dmc" },
  394. { },
  395. };
  396. static struct platform_driver rk3399_dmcfreq_driver = {
  397. .probe = rk3399_dmcfreq_probe,
  398. .driver = {
  399. .name = "rk3399-dmc-freq",
  400. .pm = &rk3399_dmcfreq_pm,
  401. .of_match_table = rk3399dmc_devfreq_of_match,
  402. },
  403. };
  404. module_platform_driver(rk3399_dmcfreq_driver);
  405. MODULE_LICENSE("GPL v2");
  406. MODULE_AUTHOR("Lin Huang <hl@rock-chips.com>");
  407. MODULE_DESCRIPTION("RK3399 dmcfreq driver with devfreq framework");