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>
20 #define HW_INTR_STATUS 0x0010
22 #define UBWC_DEC_HW_VERSION 0x58
23 #define UBWC_STATIC 0x144
24 #define UBWC_CTRL_2 0x150
25 #define UBWC_PREDICTION_MODE 0x154
27 #define MIN_IB_BW 400000000UL /* Min ib vote 400MB */
29 struct msm_mdss_data {
31 /* can be read from register 0x58 */
43 struct clk_bulk_data *clocks;
47 unsigned long enabled_mask;
48 struct irq_domain *domain;
50 const struct msm_mdss_data *mdss_data;
51 struct icc_path *path[2];
55 static int msm_mdss_parse_data_bus_icc_path(struct device *dev,
56 struct msm_mdss *msm_mdss)
58 struct icc_path *path0;
59 struct icc_path *path1;
61 path0 = of_icc_get(dev, "mdp0-mem");
62 if (IS_ERR_OR_NULL(path0))
63 return PTR_ERR_OR_ZERO(path0);
65 msm_mdss->path[0] = path0;
66 msm_mdss->num_paths = 1;
68 path1 = of_icc_get(dev, "mdp1-mem");
69 if (!IS_ERR_OR_NULL(path1)) {
70 msm_mdss->path[1] = path1;
71 msm_mdss->num_paths++;
77 static void msm_mdss_put_icc_path(void *data)
79 struct msm_mdss *msm_mdss = data;
82 for (i = 0; i < msm_mdss->num_paths; i++)
83 icc_put(msm_mdss->path[i]);
86 static void msm_mdss_icc_request_bw(struct msm_mdss *msm_mdss, unsigned long bw)
90 for (i = 0; i < msm_mdss->num_paths; i++)
91 icc_set_bw(msm_mdss->path[i], 0, Bps_to_icc(bw));
94 static void msm_mdss_irq(struct irq_desc *desc)
96 struct msm_mdss *msm_mdss = irq_desc_get_handler_data(desc);
97 struct irq_chip *chip = irq_desc_get_chip(desc);
100 chained_irq_enter(chip, desc);
102 interrupts = readl_relaxed(msm_mdss->mmio + HW_INTR_STATUS);
105 irq_hw_number_t hwirq = fls(interrupts) - 1;
108 rc = generic_handle_domain_irq(msm_mdss->irq_controller.domain,
111 dev_err(msm_mdss->dev, "handle irq fail: irq=%lu rc=%d\n",
116 interrupts &= ~(1 << hwirq);
119 chained_irq_exit(chip, desc);
122 static void msm_mdss_irq_mask(struct irq_data *irqd)
124 struct msm_mdss *msm_mdss = irq_data_get_irq_chip_data(irqd);
127 smp_mb__before_atomic();
128 clear_bit(irqd->hwirq, &msm_mdss->irq_controller.enabled_mask);
130 smp_mb__after_atomic();
133 static void msm_mdss_irq_unmask(struct irq_data *irqd)
135 struct msm_mdss *msm_mdss = irq_data_get_irq_chip_data(irqd);
138 smp_mb__before_atomic();
139 set_bit(irqd->hwirq, &msm_mdss->irq_controller.enabled_mask);
141 smp_mb__after_atomic();
144 static struct irq_chip msm_mdss_irq_chip = {
146 .irq_mask = msm_mdss_irq_mask,
147 .irq_unmask = msm_mdss_irq_unmask,
150 static struct lock_class_key msm_mdss_lock_key, msm_mdss_request_key;
152 static int msm_mdss_irqdomain_map(struct irq_domain *domain,
153 unsigned int irq, irq_hw_number_t hwirq)
155 struct msm_mdss *msm_mdss = domain->host_data;
157 irq_set_lockdep_class(irq, &msm_mdss_lock_key, &msm_mdss_request_key);
158 irq_set_chip_and_handler(irq, &msm_mdss_irq_chip, handle_level_irq);
160 return irq_set_chip_data(irq, msm_mdss);
163 static const struct irq_domain_ops msm_mdss_irqdomain_ops = {
164 .map = msm_mdss_irqdomain_map,
165 .xlate = irq_domain_xlate_onecell,
168 static int _msm_mdss_irq_domain_add(struct msm_mdss *msm_mdss)
171 struct irq_domain *domain;
175 domain = irq_domain_add_linear(dev->of_node, 32,
176 &msm_mdss_irqdomain_ops, msm_mdss);
178 dev_err(dev, "failed to add irq_domain\n");
182 msm_mdss->irq_controller.enabled_mask = 0;
183 msm_mdss->irq_controller.domain = domain;
188 #define UBWC_1_0 0x10000000
189 #define UBWC_2_0 0x20000000
190 #define UBWC_3_0 0x30000000
191 #define UBWC_4_0 0x40000000
193 static void msm_mdss_setup_ubwc_dec_20(struct msm_mdss *msm_mdss)
195 const struct msm_mdss_data *data = msm_mdss->mdss_data;
197 writel_relaxed(data->ubwc_static, msm_mdss->mmio + UBWC_STATIC);
200 static void msm_mdss_setup_ubwc_dec_30(struct msm_mdss *msm_mdss)
202 const struct msm_mdss_data *data = msm_mdss->mdss_data;
203 u32 value = (data->ubwc_swizzle & 0x1) |
204 (data->highest_bank_bit & 0x3) << 4 |
205 (data->macrotile_mode & 0x1) << 12;
207 if (data->ubwc_version == UBWC_3_0)
210 if (data->ubwc_version == UBWC_1_0)
213 writel_relaxed(value, msm_mdss->mmio + UBWC_STATIC);
216 static void msm_mdss_setup_ubwc_dec_40(struct msm_mdss *msm_mdss)
218 const struct msm_mdss_data *data = msm_mdss->mdss_data;
219 u32 value = (data->ubwc_swizzle & 0x7) |
220 (data->ubwc_static & 0x1) << 3 |
221 (data->highest_bank_bit & 0x7) << 4 |
222 (data->macrotile_mode & 0x1) << 12;
224 writel_relaxed(value, msm_mdss->mmio + UBWC_STATIC);
226 if (data->ubwc_version == UBWC_3_0) {
227 writel_relaxed(1, msm_mdss->mmio + UBWC_CTRL_2);
228 writel_relaxed(0, msm_mdss->mmio + UBWC_PREDICTION_MODE);
230 writel_relaxed(2, msm_mdss->mmio + UBWC_CTRL_2);
231 writel_relaxed(1, msm_mdss->mmio + UBWC_PREDICTION_MODE);
235 static int msm_mdss_enable(struct msm_mdss *msm_mdss)
240 * Several components have AXI clocks that can only be turned on if
241 * the interconnect is enabled (non-zero bandwidth). Let's make sure
242 * that the interconnects are at least at a minimum amount.
244 msm_mdss_icc_request_bw(msm_mdss, MIN_IB_BW);
246 ret = clk_bulk_prepare_enable(msm_mdss->num_clocks, msm_mdss->clocks);
248 dev_err(msm_mdss->dev, "clock enable failed, ret:%d\n", ret);
253 * Register access requires MDSS_MDP_CLK, which is not enabled by the
254 * mdss on mdp5 hardware. Skip it for now.
256 if (msm_mdss->is_mdp5 || !msm_mdss->mdss_data)
260 * ubwc config is part of the "mdss" region which is not accessible
261 * from the rest of the driver. hardcode known configurations here
263 * Decoder version can be read from the UBWC_DEC_HW_VERSION reg,
264 * UBWC_n and the rest of params comes from hw data.
266 switch (msm_mdss->mdss_data->ubwc_dec_version) {
268 msm_mdss_setup_ubwc_dec_20(msm_mdss);
271 msm_mdss_setup_ubwc_dec_30(msm_mdss);
274 msm_mdss_setup_ubwc_dec_40(msm_mdss);
277 dev_err(msm_mdss->dev, "Unsupported UBWC decoder version %x\n",
278 msm_mdss->mdss_data->ubwc_dec_version);
279 dev_err(msm_mdss->dev, "HW_REV: 0x%x\n",
280 readl_relaxed(msm_mdss->mmio + HW_REV));
281 dev_err(msm_mdss->dev, "UBWC_DEC_HW_VERSION: 0x%x\n",
282 readl_relaxed(msm_mdss->mmio + UBWC_DEC_HW_VERSION));
289 static int msm_mdss_disable(struct msm_mdss *msm_mdss)
291 clk_bulk_disable_unprepare(msm_mdss->num_clocks, msm_mdss->clocks);
292 msm_mdss_icc_request_bw(msm_mdss, 0);
297 static void msm_mdss_destroy(struct msm_mdss *msm_mdss)
299 struct platform_device *pdev = to_platform_device(msm_mdss->dev);
302 pm_runtime_suspend(msm_mdss->dev);
303 pm_runtime_disable(msm_mdss->dev);
304 irq_domain_remove(msm_mdss->irq_controller.domain);
305 msm_mdss->irq_controller.domain = NULL;
306 irq = platform_get_irq(pdev, 0);
307 irq_set_chained_handler_and_data(irq, NULL, NULL);
310 static int msm_mdss_reset(struct device *dev)
312 struct reset_control *reset;
314 reset = reset_control_get_optional_exclusive(dev, NULL);
316 /* Optional reset not specified */
318 } else if (IS_ERR(reset)) {
319 return dev_err_probe(dev, PTR_ERR(reset),
320 "failed to acquire mdss reset\n");
323 reset_control_assert(reset);
325 * Tests indicate that reset has to be held for some period of time,
326 * make it one frame in a typical system
329 reset_control_deassert(reset);
331 reset_control_put(reset);
337 * MDP5 MDSS uses at most three specified clocks.
339 #define MDP5_MDSS_NUM_CLOCKS 3
340 static int mdp5_mdss_parse_clock(struct platform_device *pdev, struct clk_bulk_data **clocks)
342 struct clk_bulk_data *bulk;
349 bulk = devm_kcalloc(&pdev->dev, MDP5_MDSS_NUM_CLOCKS, sizeof(struct clk_bulk_data), GFP_KERNEL);
353 bulk[num_clocks++].id = "iface";
354 bulk[num_clocks++].id = "bus";
355 bulk[num_clocks++].id = "vsync";
357 ret = devm_clk_bulk_get_optional(&pdev->dev, num_clocks, bulk);
366 static struct msm_mdss *msm_mdss_init(struct platform_device *pdev, bool is_mdp5)
368 struct msm_mdss *msm_mdss;
372 ret = msm_mdss_reset(&pdev->dev);
376 msm_mdss = devm_kzalloc(&pdev->dev, sizeof(*msm_mdss), GFP_KERNEL);
378 return ERR_PTR(-ENOMEM);
380 msm_mdss->mmio = devm_platform_ioremap_resource_byname(pdev, is_mdp5 ? "mdss_phys" : "mdss");
381 if (IS_ERR(msm_mdss->mmio))
382 return ERR_CAST(msm_mdss->mmio);
384 dev_dbg(&pdev->dev, "mapped mdss address space @%pK\n", msm_mdss->mmio);
386 ret = msm_mdss_parse_data_bus_icc_path(&pdev->dev, msm_mdss);
389 ret = devm_add_action_or_reset(&pdev->dev, msm_mdss_put_icc_path, msm_mdss);
394 ret = mdp5_mdss_parse_clock(pdev, &msm_mdss->clocks);
396 ret = devm_clk_bulk_get_all(&pdev->dev, &msm_mdss->clocks);
398 dev_err(&pdev->dev, "failed to parse clocks, ret=%d\n", ret);
401 msm_mdss->num_clocks = ret;
402 msm_mdss->is_mdp5 = is_mdp5;
404 msm_mdss->dev = &pdev->dev;
406 irq = platform_get_irq(pdev, 0);
410 ret = _msm_mdss_irq_domain_add(msm_mdss);
414 irq_set_chained_handler_and_data(irq, msm_mdss_irq,
417 pm_runtime_enable(&pdev->dev);
422 static int __maybe_unused mdss_runtime_suspend(struct device *dev)
424 struct msm_mdss *mdss = dev_get_drvdata(dev);
428 return msm_mdss_disable(mdss);
431 static int __maybe_unused mdss_runtime_resume(struct device *dev)
433 struct msm_mdss *mdss = dev_get_drvdata(dev);
437 return msm_mdss_enable(mdss);
440 static int __maybe_unused mdss_pm_suspend(struct device *dev)
443 if (pm_runtime_suspended(dev))
446 return mdss_runtime_suspend(dev);
449 static int __maybe_unused mdss_pm_resume(struct device *dev)
451 if (pm_runtime_suspended(dev))
454 return mdss_runtime_resume(dev);
457 static const struct dev_pm_ops mdss_pm_ops = {
458 SET_SYSTEM_SLEEP_PM_OPS(mdss_pm_suspend, mdss_pm_resume)
459 SET_RUNTIME_PM_OPS(mdss_runtime_suspend, mdss_runtime_resume, NULL)
462 static int mdss_probe(struct platform_device *pdev)
464 struct msm_mdss *mdss;
465 bool is_mdp5 = of_device_is_compatible(pdev->dev.of_node, "qcom,mdss");
466 struct device *dev = &pdev->dev;
469 mdss = msm_mdss_init(pdev, is_mdp5);
471 return PTR_ERR(mdss);
473 mdss->mdss_data = of_device_get_match_data(&pdev->dev);
475 platform_set_drvdata(pdev, mdss);
478 * MDP5/DPU based devices don't have a flat hierarchy. There is a top
479 * level parent: MDSS, and children: MDP5/DPU, DSI, HDMI, eDP etc.
480 * Populate the children devices, find the MDP5/DPU node, and then add
481 * the interfaces to our components list.
483 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
485 DRM_DEV_ERROR(dev, "failed to populate children devices\n");
486 msm_mdss_destroy(mdss);
493 static int mdss_remove(struct platform_device *pdev)
495 struct msm_mdss *mdss = platform_get_drvdata(pdev);
497 of_platform_depopulate(&pdev->dev);
499 msm_mdss_destroy(mdss);
504 static const struct msm_mdss_data sc7180_data = {
505 .ubwc_version = UBWC_2_0,
506 .ubwc_dec_version = UBWC_2_0,
510 static const struct msm_mdss_data sc7280_data = {
511 .ubwc_version = UBWC_3_0,
512 .ubwc_dec_version = UBWC_4_0,
515 .highest_bank_bit = 1,
519 static const struct msm_mdss_data sc8180x_data = {
520 .ubwc_version = UBWC_3_0,
521 .ubwc_dec_version = UBWC_3_0,
522 .highest_bank_bit = 3,
526 static const struct msm_mdss_data sc8280xp_data = {
527 .ubwc_version = UBWC_4_0,
528 .ubwc_dec_version = UBWC_4_0,
531 .highest_bank_bit = 2,
535 static const struct msm_mdss_data sdm845_data = {
536 .ubwc_version = UBWC_2_0,
537 .ubwc_dec_version = UBWC_2_0,
538 .highest_bank_bit = 2,
541 static const struct msm_mdss_data sm8150_data = {
542 .ubwc_version = UBWC_3_0,
543 .ubwc_dec_version = UBWC_3_0,
544 .highest_bank_bit = 2,
547 static const struct msm_mdss_data sm6115_data = {
548 .ubwc_version = UBWC_1_0,
549 .ubwc_dec_version = UBWC_2_0,
551 .ubwc_static = 0x11f,
554 static const struct msm_mdss_data sm8250_data = {
555 .ubwc_version = UBWC_4_0,
556 .ubwc_dec_version = UBWC_4_0,
559 /* TODO: highest_bank_bit = 2 for LP_DDR4 */
560 .highest_bank_bit = 3,
564 static const struct of_device_id mdss_dt_match[] = {
565 { .compatible = "qcom,mdss" },
566 { .compatible = "qcom,msm8998-mdss" },
567 { .compatible = "qcom,qcm2290-mdss" },
568 { .compatible = "qcom,sdm845-mdss", .data = &sdm845_data },
569 { .compatible = "qcom,sc7180-mdss", .data = &sc7180_data },
570 { .compatible = "qcom,sc7280-mdss", .data = &sc7280_data },
571 { .compatible = "qcom,sc8180x-mdss", .data = &sc8180x_data },
572 { .compatible = "qcom,sc8280xp-mdss", .data = &sc8280xp_data },
573 { .compatible = "qcom,sm6115-mdss", .data = &sm6115_data },
574 { .compatible = "qcom,sm8150-mdss", .data = &sm8150_data },
575 { .compatible = "qcom,sm8250-mdss", .data = &sm8250_data },
576 { .compatible = "qcom,sm8350-mdss", .data = &sm8250_data },
577 { .compatible = "qcom,sm8450-mdss", .data = &sm8250_data },
578 { .compatible = "qcom,sm8550-mdss", .data = &sm8250_data },
581 MODULE_DEVICE_TABLE(of, mdss_dt_match);
583 static struct platform_driver mdss_platform_driver = {
585 .remove = mdss_remove,
588 .of_match_table = mdss_dt_match,
593 void __init msm_mdss_register(void)
595 platform_driver_register(&mdss_platform_driver);
598 void __exit msm_mdss_unregister(void)
600 platform_driver_unregister(&mdss_platform_driver);