2 * SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018, The Linux Foundation
7 #include <linux/delay.h>
8 #include <linux/interconnect.h>
10 #include <linux/irqchip.h>
11 #include <linux/irqdesc.h>
12 #include <linux/irqchip/chained_irq.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/reset.h>
19 /* for DPU_HW_* defines */
20 #include "disp/dpu1/dpu_hw_catalog.h"
23 #define HW_INTR_STATUS 0x0010
25 #define UBWC_DEC_HW_VERSION 0x58
26 #define UBWC_STATIC 0x144
27 #define UBWC_CTRL_2 0x150
28 #define UBWC_PREDICTION_MODE 0x154
30 #define MIN_IB_BW 400000000UL /* Min ib vote 400MB */
36 struct clk_bulk_data *clocks;
40 unsigned long enabled_mask;
41 struct irq_domain *domain;
43 struct icc_path *path[2];
47 static int msm_mdss_parse_data_bus_icc_path(struct device *dev,
48 struct msm_mdss *msm_mdss)
50 struct icc_path *path0;
51 struct icc_path *path1;
53 path0 = of_icc_get(dev, "mdp0-mem");
54 if (IS_ERR_OR_NULL(path0))
55 return PTR_ERR_OR_ZERO(path0);
57 msm_mdss->path[0] = path0;
58 msm_mdss->num_paths = 1;
60 path1 = of_icc_get(dev, "mdp1-mem");
61 if (!IS_ERR_OR_NULL(path1)) {
62 msm_mdss->path[1] = path1;
63 msm_mdss->num_paths++;
69 static void msm_mdss_put_icc_path(void *data)
71 struct msm_mdss *msm_mdss = data;
74 for (i = 0; i < msm_mdss->num_paths; i++)
75 icc_put(msm_mdss->path[i]);
78 static void msm_mdss_icc_request_bw(struct msm_mdss *msm_mdss, unsigned long bw)
82 for (i = 0; i < msm_mdss->num_paths; i++)
83 icc_set_bw(msm_mdss->path[i], 0, Bps_to_icc(bw));
86 static void msm_mdss_irq(struct irq_desc *desc)
88 struct msm_mdss *msm_mdss = irq_desc_get_handler_data(desc);
89 struct irq_chip *chip = irq_desc_get_chip(desc);
92 chained_irq_enter(chip, desc);
94 interrupts = readl_relaxed(msm_mdss->mmio + HW_INTR_STATUS);
97 irq_hw_number_t hwirq = fls(interrupts) - 1;
100 rc = generic_handle_domain_irq(msm_mdss->irq_controller.domain,
103 dev_err(msm_mdss->dev, "handle irq fail: irq=%lu rc=%d\n",
108 interrupts &= ~(1 << hwirq);
111 chained_irq_exit(chip, desc);
114 static void msm_mdss_irq_mask(struct irq_data *irqd)
116 struct msm_mdss *msm_mdss = irq_data_get_irq_chip_data(irqd);
119 smp_mb__before_atomic();
120 clear_bit(irqd->hwirq, &msm_mdss->irq_controller.enabled_mask);
122 smp_mb__after_atomic();
125 static void msm_mdss_irq_unmask(struct irq_data *irqd)
127 struct msm_mdss *msm_mdss = irq_data_get_irq_chip_data(irqd);
130 smp_mb__before_atomic();
131 set_bit(irqd->hwirq, &msm_mdss->irq_controller.enabled_mask);
133 smp_mb__after_atomic();
136 static struct irq_chip msm_mdss_irq_chip = {
138 .irq_mask = msm_mdss_irq_mask,
139 .irq_unmask = msm_mdss_irq_unmask,
142 static struct lock_class_key msm_mdss_lock_key, msm_mdss_request_key;
144 static int msm_mdss_irqdomain_map(struct irq_domain *domain,
145 unsigned int irq, irq_hw_number_t hwirq)
147 struct msm_mdss *msm_mdss = domain->host_data;
149 irq_set_lockdep_class(irq, &msm_mdss_lock_key, &msm_mdss_request_key);
150 irq_set_chip_and_handler(irq, &msm_mdss_irq_chip, handle_level_irq);
152 return irq_set_chip_data(irq, msm_mdss);
155 static const struct irq_domain_ops msm_mdss_irqdomain_ops = {
156 .map = msm_mdss_irqdomain_map,
157 .xlate = irq_domain_xlate_onecell,
160 static int _msm_mdss_irq_domain_add(struct msm_mdss *msm_mdss)
163 struct irq_domain *domain;
167 domain = irq_domain_add_linear(dev->of_node, 32,
168 &msm_mdss_irqdomain_ops, msm_mdss);
170 dev_err(dev, "failed to add irq_domain\n");
174 msm_mdss->irq_controller.enabled_mask = 0;
175 msm_mdss->irq_controller.domain = domain;
180 #define UBWC_1_0 0x10000000
181 #define UBWC_2_0 0x20000000
182 #define UBWC_3_0 0x30000000
183 #define UBWC_4_0 0x40000000
185 static void msm_mdss_setup_ubwc_dec_20(struct msm_mdss *msm_mdss,
188 writel_relaxed(ubwc_static, msm_mdss->mmio + UBWC_STATIC);
191 static void msm_mdss_setup_ubwc_dec_30(struct msm_mdss *msm_mdss,
192 unsigned int ubwc_version,
194 u32 highest_bank_bit,
197 u32 value = (ubwc_swizzle & 0x1) |
198 (highest_bank_bit & 0x3) << 4 |
199 (macrotile_mode & 0x1) << 12;
201 if (ubwc_version == UBWC_3_0)
204 if (ubwc_version == UBWC_1_0)
207 writel_relaxed(value, msm_mdss->mmio + UBWC_STATIC);
210 static void msm_mdss_setup_ubwc_dec_40(struct msm_mdss *msm_mdss,
211 unsigned int ubwc_version,
214 u32 highest_bank_bit,
217 u32 value = (ubwc_swizzle & 0x7) |
218 (ubwc_static & 0x1) << 3 |
219 (highest_bank_bit & 0x7) << 4 |
220 (macrotile_mode & 0x1) << 12;
222 writel_relaxed(value, msm_mdss->mmio + UBWC_STATIC);
224 if (ubwc_version == UBWC_3_0) {
225 writel_relaxed(1, msm_mdss->mmio + UBWC_CTRL_2);
226 writel_relaxed(0, msm_mdss->mmio + UBWC_PREDICTION_MODE);
228 writel_relaxed(2, msm_mdss->mmio + UBWC_CTRL_2);
229 writel_relaxed(1, msm_mdss->mmio + UBWC_PREDICTION_MODE);
233 static int msm_mdss_enable(struct msm_mdss *msm_mdss)
239 * Several components have AXI clocks that can only be turned on if
240 * the interconnect is enabled (non-zero bandwidth). Let's make sure
241 * that the interconnects are at least at a minimum amount.
243 msm_mdss_icc_request_bw(msm_mdss, MIN_IB_BW);
245 ret = clk_bulk_prepare_enable(msm_mdss->num_clocks, msm_mdss->clocks);
247 dev_err(msm_mdss->dev, "clock enable failed, ret:%d\n", ret);
252 * HW_REV requires MDSS_MDP_CLK, which is not enabled by the mdss on
253 * mdp5 hardware. Skip reading it for now.
255 if (msm_mdss->is_mdp5)
258 hw_rev = readl_relaxed(msm_mdss->mmio + HW_REV);
259 dev_dbg(msm_mdss->dev, "HW_REV: 0x%x\n", hw_rev);
260 dev_dbg(msm_mdss->dev, "UBWC_DEC_HW_VERSION: 0x%x\n",
261 readl_relaxed(msm_mdss->mmio + UBWC_DEC_HW_VERSION));
264 * ubwc config is part of the "mdss" region which is not accessible
265 * from the rest of the driver. hardcode known configurations here
267 * Decoder version can be read from the UBWC_DEC_HW_VERSION reg,
268 * UBWC_n and the rest of params comes from hw_catalog.
269 * Unforunately this driver can not access hw catalog, so we have to
270 * hardcode them here.
275 msm_mdss_setup_ubwc_dec_30(msm_mdss, UBWC_3_0, 0, 2, 0);
278 /* TODO: highest_bank_bit = 2 for LP_DDR4 */
279 msm_mdss_setup_ubwc_dec_40(msm_mdss, UBWC_4_0, 6, 1, 3, 1);
283 msm_mdss_setup_ubwc_dec_20(msm_mdss, 0x1e);
287 msm_mdss_setup_ubwc_dec_20(msm_mdss, 0x11f);
290 /* TODO: highest_bank_bit = 2 for LP_DDR4 */
291 msm_mdss_setup_ubwc_dec_40(msm_mdss, UBWC_4_0, 6, 1, 3, 1);
294 msm_mdss_setup_ubwc_dec_40(msm_mdss, UBWC_3_0, 6, 1, 1, 1);
297 msm_mdss_setup_ubwc_dec_40(msm_mdss, UBWC_4_0, 6, 1, 2, 1);
301 /* TODO: highest_bank_bit = 2 for LP_DDR4 */
302 msm_mdss_setup_ubwc_dec_40(msm_mdss, UBWC_4_0, 6, 1, 3, 1);
309 static int msm_mdss_disable(struct msm_mdss *msm_mdss)
311 clk_bulk_disable_unprepare(msm_mdss->num_clocks, msm_mdss->clocks);
312 msm_mdss_icc_request_bw(msm_mdss, 0);
317 static void msm_mdss_destroy(struct msm_mdss *msm_mdss)
319 struct platform_device *pdev = to_platform_device(msm_mdss->dev);
322 pm_runtime_suspend(msm_mdss->dev);
323 pm_runtime_disable(msm_mdss->dev);
324 irq_domain_remove(msm_mdss->irq_controller.domain);
325 msm_mdss->irq_controller.domain = NULL;
326 irq = platform_get_irq(pdev, 0);
327 irq_set_chained_handler_and_data(irq, NULL, NULL);
330 static int msm_mdss_reset(struct device *dev)
332 struct reset_control *reset;
334 reset = reset_control_get_optional_exclusive(dev, NULL);
336 /* Optional reset not specified */
338 } else if (IS_ERR(reset)) {
339 return dev_err_probe(dev, PTR_ERR(reset),
340 "failed to acquire mdss reset\n");
343 reset_control_assert(reset);
345 * Tests indicate that reset has to be held for some period of time,
346 * make it one frame in a typical system
349 reset_control_deassert(reset);
351 reset_control_put(reset);
357 * MDP5 MDSS uses at most three specified clocks.
359 #define MDP5_MDSS_NUM_CLOCKS 3
360 static int mdp5_mdss_parse_clock(struct platform_device *pdev, struct clk_bulk_data **clocks)
362 struct clk_bulk_data *bulk;
369 bulk = devm_kcalloc(&pdev->dev, MDP5_MDSS_NUM_CLOCKS, sizeof(struct clk_bulk_data), GFP_KERNEL);
373 bulk[num_clocks++].id = "iface";
374 bulk[num_clocks++].id = "bus";
375 bulk[num_clocks++].id = "vsync";
377 ret = devm_clk_bulk_get_optional(&pdev->dev, num_clocks, bulk);
386 static struct msm_mdss *msm_mdss_init(struct platform_device *pdev, bool is_mdp5)
388 struct msm_mdss *msm_mdss;
392 ret = msm_mdss_reset(&pdev->dev);
396 msm_mdss = devm_kzalloc(&pdev->dev, sizeof(*msm_mdss), GFP_KERNEL);
398 return ERR_PTR(-ENOMEM);
400 msm_mdss->mmio = devm_platform_ioremap_resource_byname(pdev, is_mdp5 ? "mdss_phys" : "mdss");
401 if (IS_ERR(msm_mdss->mmio))
402 return ERR_CAST(msm_mdss->mmio);
404 dev_dbg(&pdev->dev, "mapped mdss address space @%pK\n", msm_mdss->mmio);
406 ret = msm_mdss_parse_data_bus_icc_path(&pdev->dev, msm_mdss);
409 ret = devm_add_action_or_reset(&pdev->dev, msm_mdss_put_icc_path, msm_mdss);
414 ret = mdp5_mdss_parse_clock(pdev, &msm_mdss->clocks);
416 ret = devm_clk_bulk_get_all(&pdev->dev, &msm_mdss->clocks);
418 dev_err(&pdev->dev, "failed to parse clocks, ret=%d\n", ret);
421 msm_mdss->num_clocks = ret;
422 msm_mdss->is_mdp5 = is_mdp5;
424 msm_mdss->dev = &pdev->dev;
426 irq = platform_get_irq(pdev, 0);
430 ret = _msm_mdss_irq_domain_add(msm_mdss);
434 irq_set_chained_handler_and_data(irq, msm_mdss_irq,
437 pm_runtime_enable(&pdev->dev);
442 static int __maybe_unused mdss_runtime_suspend(struct device *dev)
444 struct msm_mdss *mdss = dev_get_drvdata(dev);
448 return msm_mdss_disable(mdss);
451 static int __maybe_unused mdss_runtime_resume(struct device *dev)
453 struct msm_mdss *mdss = dev_get_drvdata(dev);
457 return msm_mdss_enable(mdss);
460 static int __maybe_unused mdss_pm_suspend(struct device *dev)
463 if (pm_runtime_suspended(dev))
466 return mdss_runtime_suspend(dev);
469 static int __maybe_unused mdss_pm_resume(struct device *dev)
471 if (pm_runtime_suspended(dev))
474 return mdss_runtime_resume(dev);
477 static const struct dev_pm_ops mdss_pm_ops = {
478 SET_SYSTEM_SLEEP_PM_OPS(mdss_pm_suspend, mdss_pm_resume)
479 SET_RUNTIME_PM_OPS(mdss_runtime_suspend, mdss_runtime_resume, NULL)
482 static int mdss_probe(struct platform_device *pdev)
484 struct msm_mdss *mdss;
485 bool is_mdp5 = of_device_is_compatible(pdev->dev.of_node, "qcom,mdss");
486 struct device *dev = &pdev->dev;
489 mdss = msm_mdss_init(pdev, is_mdp5);
491 return PTR_ERR(mdss);
493 platform_set_drvdata(pdev, mdss);
496 * MDP5/DPU based devices don't have a flat hierarchy. There is a top
497 * level parent: MDSS, and children: MDP5/DPU, DSI, HDMI, eDP etc.
498 * Populate the children devices, find the MDP5/DPU node, and then add
499 * the interfaces to our components list.
501 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
503 DRM_DEV_ERROR(dev, "failed to populate children devices\n");
504 msm_mdss_destroy(mdss);
511 static int mdss_remove(struct platform_device *pdev)
513 struct msm_mdss *mdss = platform_get_drvdata(pdev);
515 of_platform_depopulate(&pdev->dev);
517 msm_mdss_destroy(mdss);
522 static const struct of_device_id mdss_dt_match[] = {
523 { .compatible = "qcom,mdss" },
524 { .compatible = "qcom,msm8998-mdss" },
525 { .compatible = "qcom,qcm2290-mdss" },
526 { .compatible = "qcom,sdm845-mdss" },
527 { .compatible = "qcom,sc7180-mdss" },
528 { .compatible = "qcom,sc7280-mdss" },
529 { .compatible = "qcom,sc8180x-mdss" },
530 { .compatible = "qcom,sc8280xp-mdss" },
531 { .compatible = "qcom,sm6115-mdss" },
532 { .compatible = "qcom,sm8150-mdss" },
533 { .compatible = "qcom,sm8250-mdss" },
534 { .compatible = "qcom,sm8350-mdss" },
535 { .compatible = "qcom,sm8450-mdss" },
536 { .compatible = "qcom,sm8550-mdss" },
539 MODULE_DEVICE_TABLE(of, mdss_dt_match);
541 static struct platform_driver mdss_platform_driver = {
543 .remove = mdss_remove,
546 .of_match_table = mdss_dt_match,
551 void __init msm_mdss_register(void)
553 platform_driver_register(&mdss_platform_driver);
556 void __exit msm_mdss_unregister(void)
558 platform_driver_unregister(&mdss_platform_driver);