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
192 #define UBWC_4_3 0x40030000
194 static void msm_mdss_setup_ubwc_dec_20(struct msm_mdss *msm_mdss)
196 const struct msm_mdss_data *data = msm_mdss->mdss_data;
198 writel_relaxed(data->ubwc_static, msm_mdss->mmio + UBWC_STATIC);
201 static void msm_mdss_setup_ubwc_dec_30(struct msm_mdss *msm_mdss)
203 const struct msm_mdss_data *data = msm_mdss->mdss_data;
204 u32 value = (data->ubwc_swizzle & 0x1) |
205 (data->highest_bank_bit & 0x3) << 4 |
206 (data->macrotile_mode & 0x1) << 12;
208 if (data->ubwc_version == UBWC_3_0)
211 if (data->ubwc_version == UBWC_1_0)
214 writel_relaxed(value, msm_mdss->mmio + UBWC_STATIC);
217 static void msm_mdss_setup_ubwc_dec_40(struct msm_mdss *msm_mdss)
219 const struct msm_mdss_data *data = msm_mdss->mdss_data;
220 u32 value = (data->ubwc_swizzle & 0x7) |
221 (data->ubwc_static & 0x1) << 3 |
222 (data->highest_bank_bit & 0x7) << 4 |
223 (data->macrotile_mode & 0x1) << 12;
225 writel_relaxed(value, msm_mdss->mmio + UBWC_STATIC);
227 if (data->ubwc_version == UBWC_3_0) {
228 writel_relaxed(1, msm_mdss->mmio + UBWC_CTRL_2);
229 writel_relaxed(0, msm_mdss->mmio + UBWC_PREDICTION_MODE);
231 if (data->ubwc_dec_version == UBWC_4_3)
232 writel_relaxed(3, msm_mdss->mmio + UBWC_CTRL_2);
234 writel_relaxed(2, msm_mdss->mmio + UBWC_CTRL_2);
235 writel_relaxed(1, msm_mdss->mmio + UBWC_PREDICTION_MODE);
239 static int msm_mdss_enable(struct msm_mdss *msm_mdss)
244 * Several components have AXI clocks that can only be turned on if
245 * the interconnect is enabled (non-zero bandwidth). Let's make sure
246 * that the interconnects are at least at a minimum amount.
248 msm_mdss_icc_request_bw(msm_mdss, MIN_IB_BW);
250 ret = clk_bulk_prepare_enable(msm_mdss->num_clocks, msm_mdss->clocks);
252 dev_err(msm_mdss->dev, "clock enable failed, ret:%d\n", ret);
257 * Register access requires MDSS_MDP_CLK, which is not enabled by the
258 * mdss on mdp5 hardware. Skip it for now.
260 if (msm_mdss->is_mdp5 || !msm_mdss->mdss_data)
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 data.
270 switch (msm_mdss->mdss_data->ubwc_dec_version) {
272 msm_mdss_setup_ubwc_dec_20(msm_mdss);
275 msm_mdss_setup_ubwc_dec_30(msm_mdss);
279 msm_mdss_setup_ubwc_dec_40(msm_mdss);
282 dev_err(msm_mdss->dev, "Unsupported UBWC decoder version %x\n",
283 msm_mdss->mdss_data->ubwc_dec_version);
284 dev_err(msm_mdss->dev, "HW_REV: 0x%x\n",
285 readl_relaxed(msm_mdss->mmio + HW_REV));
286 dev_err(msm_mdss->dev, "UBWC_DEC_HW_VERSION: 0x%x\n",
287 readl_relaxed(msm_mdss->mmio + UBWC_DEC_HW_VERSION));
294 static int msm_mdss_disable(struct msm_mdss *msm_mdss)
296 clk_bulk_disable_unprepare(msm_mdss->num_clocks, msm_mdss->clocks);
297 msm_mdss_icc_request_bw(msm_mdss, 0);
302 static void msm_mdss_destroy(struct msm_mdss *msm_mdss)
304 struct platform_device *pdev = to_platform_device(msm_mdss->dev);
307 pm_runtime_suspend(msm_mdss->dev);
308 pm_runtime_disable(msm_mdss->dev);
309 irq_domain_remove(msm_mdss->irq_controller.domain);
310 msm_mdss->irq_controller.domain = NULL;
311 irq = platform_get_irq(pdev, 0);
312 irq_set_chained_handler_and_data(irq, NULL, NULL);
315 static int msm_mdss_reset(struct device *dev)
317 struct reset_control *reset;
319 reset = reset_control_get_optional_exclusive(dev, NULL);
321 /* Optional reset not specified */
323 } else if (IS_ERR(reset)) {
324 return dev_err_probe(dev, PTR_ERR(reset),
325 "failed to acquire mdss reset\n");
328 reset_control_assert(reset);
330 * Tests indicate that reset has to be held for some period of time,
331 * make it one frame in a typical system
334 reset_control_deassert(reset);
336 reset_control_put(reset);
342 * MDP5 MDSS uses at most three specified clocks.
344 #define MDP5_MDSS_NUM_CLOCKS 3
345 static int mdp5_mdss_parse_clock(struct platform_device *pdev, struct clk_bulk_data **clocks)
347 struct clk_bulk_data *bulk;
354 bulk = devm_kcalloc(&pdev->dev, MDP5_MDSS_NUM_CLOCKS, sizeof(struct clk_bulk_data), GFP_KERNEL);
358 bulk[num_clocks++].id = "iface";
359 bulk[num_clocks++].id = "bus";
360 bulk[num_clocks++].id = "vsync";
362 ret = devm_clk_bulk_get_optional(&pdev->dev, num_clocks, bulk);
371 static struct msm_mdss *msm_mdss_init(struct platform_device *pdev, bool is_mdp5)
373 struct msm_mdss *msm_mdss;
377 ret = msm_mdss_reset(&pdev->dev);
381 msm_mdss = devm_kzalloc(&pdev->dev, sizeof(*msm_mdss), GFP_KERNEL);
383 return ERR_PTR(-ENOMEM);
385 msm_mdss->mmio = devm_platform_ioremap_resource_byname(pdev, is_mdp5 ? "mdss_phys" : "mdss");
386 if (IS_ERR(msm_mdss->mmio))
387 return ERR_CAST(msm_mdss->mmio);
389 dev_dbg(&pdev->dev, "mapped mdss address space @%pK\n", msm_mdss->mmio);
391 ret = msm_mdss_parse_data_bus_icc_path(&pdev->dev, msm_mdss);
394 ret = devm_add_action_or_reset(&pdev->dev, msm_mdss_put_icc_path, msm_mdss);
399 ret = mdp5_mdss_parse_clock(pdev, &msm_mdss->clocks);
401 ret = devm_clk_bulk_get_all(&pdev->dev, &msm_mdss->clocks);
403 dev_err(&pdev->dev, "failed to parse clocks, ret=%d\n", ret);
406 msm_mdss->num_clocks = ret;
407 msm_mdss->is_mdp5 = is_mdp5;
409 msm_mdss->dev = &pdev->dev;
411 irq = platform_get_irq(pdev, 0);
415 ret = _msm_mdss_irq_domain_add(msm_mdss);
419 irq_set_chained_handler_and_data(irq, msm_mdss_irq,
422 pm_runtime_enable(&pdev->dev);
427 static int __maybe_unused mdss_runtime_suspend(struct device *dev)
429 struct msm_mdss *mdss = dev_get_drvdata(dev);
433 return msm_mdss_disable(mdss);
436 static int __maybe_unused mdss_runtime_resume(struct device *dev)
438 struct msm_mdss *mdss = dev_get_drvdata(dev);
442 return msm_mdss_enable(mdss);
445 static int __maybe_unused mdss_pm_suspend(struct device *dev)
448 if (pm_runtime_suspended(dev))
451 return mdss_runtime_suspend(dev);
454 static int __maybe_unused mdss_pm_resume(struct device *dev)
456 if (pm_runtime_suspended(dev))
459 return mdss_runtime_resume(dev);
462 static const struct dev_pm_ops mdss_pm_ops = {
463 SET_SYSTEM_SLEEP_PM_OPS(mdss_pm_suspend, mdss_pm_resume)
464 SET_RUNTIME_PM_OPS(mdss_runtime_suspend, mdss_runtime_resume, NULL)
467 static int mdss_probe(struct platform_device *pdev)
469 struct msm_mdss *mdss;
470 bool is_mdp5 = of_device_is_compatible(pdev->dev.of_node, "qcom,mdss");
471 struct device *dev = &pdev->dev;
474 mdss = msm_mdss_init(pdev, is_mdp5);
476 return PTR_ERR(mdss);
478 mdss->mdss_data = of_device_get_match_data(&pdev->dev);
480 platform_set_drvdata(pdev, mdss);
483 * MDP5/DPU based devices don't have a flat hierarchy. There is a top
484 * level parent: MDSS, and children: MDP5/DPU, DSI, HDMI, eDP etc.
485 * Populate the children devices, find the MDP5/DPU node, and then add
486 * the interfaces to our components list.
488 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
490 DRM_DEV_ERROR(dev, "failed to populate children devices\n");
491 msm_mdss_destroy(mdss);
498 static int mdss_remove(struct platform_device *pdev)
500 struct msm_mdss *mdss = platform_get_drvdata(pdev);
502 of_platform_depopulate(&pdev->dev);
504 msm_mdss_destroy(mdss);
509 static const struct msm_mdss_data sc7180_data = {
510 .ubwc_version = UBWC_2_0,
511 .ubwc_dec_version = UBWC_2_0,
515 static const struct msm_mdss_data sc7280_data = {
516 .ubwc_version = UBWC_3_0,
517 .ubwc_dec_version = UBWC_4_0,
520 .highest_bank_bit = 1,
524 static const struct msm_mdss_data sc8180x_data = {
525 .ubwc_version = UBWC_3_0,
526 .ubwc_dec_version = UBWC_3_0,
527 .highest_bank_bit = 3,
531 static const struct msm_mdss_data sc8280xp_data = {
532 .ubwc_version = UBWC_4_0,
533 .ubwc_dec_version = UBWC_4_0,
536 .highest_bank_bit = 2,
540 static const struct msm_mdss_data sdm845_data = {
541 .ubwc_version = UBWC_2_0,
542 .ubwc_dec_version = UBWC_2_0,
543 .highest_bank_bit = 2,
546 static const struct msm_mdss_data sm6350_data = {
547 .ubwc_version = UBWC_2_0,
548 .ubwc_dec_version = UBWC_2_0,
551 .highest_bank_bit = 1,
554 static const struct msm_mdss_data sm8150_data = {
555 .ubwc_version = UBWC_3_0,
556 .ubwc_dec_version = UBWC_3_0,
557 .highest_bank_bit = 2,
560 static const struct msm_mdss_data sm6115_data = {
561 .ubwc_version = UBWC_1_0,
562 .ubwc_dec_version = UBWC_2_0,
564 .ubwc_static = 0x11f,
567 static const struct msm_mdss_data sm8250_data = {
568 .ubwc_version = UBWC_4_0,
569 .ubwc_dec_version = UBWC_4_0,
572 /* TODO: highest_bank_bit = 2 for LP_DDR4 */
573 .highest_bank_bit = 3,
577 static const struct msm_mdss_data sm8550_data = {
578 .ubwc_version = UBWC_4_0,
579 .ubwc_dec_version = UBWC_4_3,
582 /* TODO: highest_bank_bit = 2 for LP_DDR4 */
583 .highest_bank_bit = 3,
587 static const struct of_device_id mdss_dt_match[] = {
588 { .compatible = "qcom,mdss" },
589 { .compatible = "qcom,msm8998-mdss" },
590 { .compatible = "qcom,qcm2290-mdss" },
591 { .compatible = "qcom,sdm845-mdss", .data = &sdm845_data },
592 { .compatible = "qcom,sc7180-mdss", .data = &sc7180_data },
593 { .compatible = "qcom,sc7280-mdss", .data = &sc7280_data },
594 { .compatible = "qcom,sc8180x-mdss", .data = &sc8180x_data },
595 { .compatible = "qcom,sc8280xp-mdss", .data = &sc8280xp_data },
596 { .compatible = "qcom,sm6115-mdss", .data = &sm6115_data },
597 { .compatible = "qcom,sm6350-mdss", .data = &sm6350_data },
598 { .compatible = "qcom,sm6375-mdss", .data = &sm6350_data },
599 { .compatible = "qcom,sm8150-mdss", .data = &sm8150_data },
600 { .compatible = "qcom,sm8250-mdss", .data = &sm8250_data },
601 { .compatible = "qcom,sm8350-mdss", .data = &sm8250_data },
602 { .compatible = "qcom,sm8450-mdss", .data = &sm8250_data },
603 { .compatible = "qcom,sm8550-mdss", .data = &sm8550_data },
606 MODULE_DEVICE_TABLE(of, mdss_dt_match);
608 static struct platform_driver mdss_platform_driver = {
610 .remove = mdss_remove,
613 .of_match_table = mdss_dt_match,
618 void __init msm_mdss_register(void)
620 platform_driver_register(&mdss_platform_driver);
623 void __exit msm_mdss_unregister(void)
625 platform_driver_unregister(&mdss_platform_driver);