1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2021 MediaTek Inc.
7 #include <linux/interrupt.h>
9 #include <linux/module.h>
11 #include <linux/of_device.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/slab.h>
15 #include "mtk_vcodec_dec.h"
16 #include "mtk_vcodec_dec_hw.h"
17 #include "mtk_vcodec_dec_pm.h"
18 #include "../common/mtk_vcodec_intr.h"
20 static const struct of_device_id mtk_vdec_hw_match[] = {
22 .compatible = "mediatek,mtk-vcodec-lat",
23 .data = (void *)MTK_VDEC_LAT0,
26 .compatible = "mediatek,mtk-vcodec-core",
27 .data = (void *)MTK_VDEC_CORE,
30 .compatible = "mediatek,mtk-vcodec-lat-soc",
31 .data = (void *)MTK_VDEC_LAT_SOC,
35 MODULE_DEVICE_TABLE(of, mtk_vdec_hw_match);
37 static int mtk_vdec_hw_prob_done(struct mtk_vcodec_dec_dev *vdec_dev)
39 struct platform_device *pdev = vdec_dev->plat_dev;
40 struct device_node *subdev_node;
41 enum mtk_vdec_hw_id hw_idx;
42 const struct of_device_id *of_id;
45 for (i = 0; i < ARRAY_SIZE(mtk_vdec_hw_match); i++) {
46 of_id = &mtk_vdec_hw_match[i];
47 subdev_node = of_find_compatible_node(NULL, NULL,
52 of_node_put(subdev_node);
54 hw_idx = (enum mtk_vdec_hw_id)(uintptr_t)of_id->data;
55 if (!test_bit(hw_idx, vdec_dev->subdev_bitmap)) {
56 dev_err(&pdev->dev, "vdec %d is not ready", hw_idx);
64 static irqreturn_t mtk_vdec_hw_irq_handler(int irq, void *priv)
66 struct mtk_vdec_hw_dev *dev = priv;
67 struct mtk_vcodec_dec_ctx *ctx;
69 unsigned int dec_done_status;
70 void __iomem *vdec_misc_addr = dev->reg_base[VDEC_HW_MISC] +
73 ctx = mtk_vcodec_get_curr_ctx(dev->main_dev, dev->hw_idx);
75 /* check if HW active or not */
76 cg_status = readl(dev->reg_base[VDEC_HW_SYS] + VDEC_HW_ACTIVE_ADDR);
77 if (cg_status & VDEC_HW_ACTIVE_MASK) {
78 mtk_v4l2_vdec_err(ctx, "vdec active is not 0x0 (0x%08x)", cg_status);
82 dec_done_status = readl(vdec_misc_addr);
83 if ((dec_done_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) !=
84 MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
88 writel(dec_done_status | VDEC_IRQ_CFG, vdec_misc_addr);
89 writel(dec_done_status & ~VDEC_IRQ_CLR, vdec_misc_addr);
91 wake_up_dec_ctx(ctx, MTK_INST_IRQ_RECEIVED, dev->hw_idx);
93 mtk_v4l2_vdec_dbg(3, ctx, "wake up ctx %d, dec_done_status=%x",
94 ctx->id, dec_done_status);
99 static int mtk_vdec_hw_init_irq(struct mtk_vdec_hw_dev *dev)
101 struct platform_device *pdev = dev->plat_dev;
104 dev->dec_irq = platform_get_irq(pdev, 0);
105 if (dev->dec_irq < 0)
108 irq_set_status_flags(dev->dec_irq, IRQ_NOAUTOEN);
109 ret = devm_request_irq(&pdev->dev, dev->dec_irq,
110 mtk_vdec_hw_irq_handler, 0, pdev->name, dev);
112 dev_err(&pdev->dev, "Failed to install dev->dec_irq %d (%d)",
120 static int mtk_vdec_hw_probe(struct platform_device *pdev)
122 struct device *dev = &pdev->dev;
123 struct mtk_vdec_hw_dev *subdev_dev;
124 struct mtk_vcodec_dec_dev *main_dev;
125 const struct of_device_id *of_id;
130 dev_err(dev, "no parent for hardware devices.\n");
134 main_dev = dev_get_drvdata(dev->parent);
136 dev_err(dev, "failed to get parent driver data");
140 subdev_dev = devm_kzalloc(dev, sizeof(*subdev_dev), GFP_KERNEL);
144 subdev_dev->plat_dev = pdev;
145 ret = mtk_vcodec_init_dec_clk(pdev, &subdev_dev->pm);
149 ret = devm_pm_runtime_enable(&pdev->dev);
153 of_id = of_match_device(mtk_vdec_hw_match, dev);
155 dev_err(dev, "Can't get vdec subdev id.\n");
159 hw_idx = (enum mtk_vdec_hw_id)(uintptr_t)of_id->data;
160 if (hw_idx >= MTK_VDEC_HW_MAX) {
161 dev_err(dev, "Hardware index %d not correct.\n", hw_idx);
165 main_dev->subdev_dev[hw_idx] = subdev_dev;
166 subdev_dev->hw_idx = hw_idx;
167 subdev_dev->main_dev = main_dev;
168 subdev_dev->reg_base[VDEC_HW_SYS] = main_dev->reg_base[VDEC_HW_SYS];
169 set_bit(subdev_dev->hw_idx, main_dev->subdev_bitmap);
171 if (IS_SUPPORT_VDEC_HW_IRQ(hw_idx)) {
172 ret = mtk_vdec_hw_init_irq(subdev_dev);
177 subdev_dev->reg_base[VDEC_HW_MISC] =
178 devm_platform_ioremap_resource(pdev, 0);
179 if (IS_ERR((__force void *)subdev_dev->reg_base[VDEC_HW_MISC])) {
180 ret = PTR_ERR((__force void *)subdev_dev->reg_base[VDEC_HW_MISC]);
184 if (!main_dev->subdev_prob_done)
185 main_dev->subdev_prob_done = mtk_vdec_hw_prob_done;
187 platform_set_drvdata(pdev, subdev_dev);
191 static struct platform_driver mtk_vdec_driver = {
192 .probe = mtk_vdec_hw_probe,
194 .name = "mtk-vdec-comp",
195 .of_match_table = mtk_vdec_hw_match,
198 module_platform_driver(mtk_vdec_driver);
200 MODULE_LICENSE("GPL v2");
201 MODULE_DESCRIPTION("Mediatek video decoder hardware driver");