1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2016 MediaTek Inc. 4 * Author: PC Chen <pc.chen@mediatek.com> 5 * Tiffany Lin <tiffany.lin@mediatek.com> 6 */ 7 8 #include <linux/module.h> 9 #include <linux/of.h> 10 #include <linux/regmap.h> 11 12 #include "../decoder/mtk_vcodec_dec_drv.h" 13 #include "../encoder/mtk_vcodec_enc_drv.h" 14 #include "../decoder/mtk_vcodec_dec_hw.h" 15 16 #if defined(CONFIG_DEBUG_FS) 17 int mtk_vcodec_dbg; 18 EXPORT_SYMBOL(mtk_vcodec_dbg); 19 20 int mtk_v4l2_dbg_level; 21 EXPORT_SYMBOL(mtk_v4l2_dbg_level); 22 #endif 23 24 void __iomem *mtk_vcodec_get_reg_addr(void __iomem **reg_base, unsigned int reg_idx) 25 { 26 if (reg_idx >= NUM_MAX_VCODEC_REG_BASE) { 27 pr_err(MTK_DBG_V4L2_STR "Invalid arguments, reg_idx=%d", reg_idx); 28 return NULL; 29 } 30 return reg_base[reg_idx]; 31 } 32 EXPORT_SYMBOL(mtk_vcodec_get_reg_addr); 33 34 int mtk_vcodec_write_vdecsys(struct mtk_vcodec_dec_ctx *ctx, unsigned int reg, 35 unsigned int val) 36 { 37 struct mtk_vcodec_dec_dev *dev = ctx->dev; 38 39 if (dev->vdecsys_regmap) 40 return regmap_write(dev->vdecsys_regmap, reg, val); 41 42 writel(val, dev->reg_base[VDEC_SYS] + reg); 43 44 return 0; 45 } 46 EXPORT_SYMBOL(mtk_vcodec_write_vdecsys); 47 48 int mtk_vcodec_mem_alloc(void *priv, struct mtk_vcodec_mem *mem) 49 { 50 unsigned long size = mem->size; 51 struct mtk_vcodec_dec_ctx *ctx = priv; 52 struct device *dev = &ctx->dev->plat_dev->dev; 53 54 mem->va = dma_alloc_coherent(dev, size, &mem->dma_addr, GFP_KERNEL); 55 if (!mem->va) { 56 mtk_v4l2_vdec_err(ctx, "%s dma_alloc size=%ld failed!", dev_name(dev), size); 57 return -ENOMEM; 58 } 59 60 mtk_v4l2_vdec_dbg(3, ctx, "[%d] - va = %p", ctx->id, mem->va); 61 mtk_v4l2_vdec_dbg(3, ctx, "[%d] - dma = 0x%lx", ctx->id, 62 (unsigned long)mem->dma_addr); 63 mtk_v4l2_vdec_dbg(3, ctx, "[%d] size = 0x%lx", ctx->id, size); 64 65 return 0; 66 } 67 EXPORT_SYMBOL(mtk_vcodec_mem_alloc); 68 69 void mtk_vcodec_mem_free(void *priv, struct mtk_vcodec_mem *mem) 70 { 71 unsigned long size = mem->size; 72 struct mtk_vcodec_dec_ctx *ctx = priv; 73 struct device *dev = &ctx->dev->plat_dev->dev; 74 75 if (!mem->va) { 76 mtk_v4l2_vdec_err(ctx, "%s dma_free size=%ld failed!", dev_name(dev), size); 77 return; 78 } 79 80 mtk_v4l2_vdec_dbg(3, ctx, "[%d] - va = %p", ctx->id, mem->va); 81 mtk_v4l2_vdec_dbg(3, ctx, "[%d] - dma = 0x%lx", ctx->id, 82 (unsigned long)mem->dma_addr); 83 mtk_v4l2_vdec_dbg(3, ctx, "[%d] size = 0x%lx", ctx->id, size); 84 85 dma_free_coherent(dev, size, mem->va, mem->dma_addr); 86 mem->va = NULL; 87 mem->dma_addr = 0; 88 mem->size = 0; 89 } 90 EXPORT_SYMBOL(mtk_vcodec_mem_free); 91 92 void *mtk_vcodec_get_hw_dev(struct mtk_vcodec_dec_dev *dev, int hw_idx) 93 { 94 if (hw_idx >= MTK_VDEC_HW_MAX || hw_idx < 0 || !dev->subdev_dev[hw_idx]) { 95 dev_err(&dev->plat_dev->dev, "hw idx is out of range:%d", hw_idx); 96 return NULL; 97 } 98 99 return dev->subdev_dev[hw_idx]; 100 } 101 EXPORT_SYMBOL(mtk_vcodec_get_hw_dev); 102 103 void mtk_vcodec_set_curr_ctx(struct mtk_vcodec_dec_dev *vdec_dev, 104 struct mtk_vcodec_dec_ctx *ctx, int hw_idx) 105 { 106 unsigned long flags; 107 struct mtk_vdec_hw_dev *subdev_dev; 108 109 spin_lock_irqsave(&vdec_dev->irqlock, flags); 110 if (vdec_dev->vdec_pdata->is_subdev_supported) { 111 subdev_dev = mtk_vcodec_get_hw_dev(vdec_dev, hw_idx); 112 if (!subdev_dev) { 113 dev_err(&vdec_dev->plat_dev->dev, "Failed to get hw dev"); 114 spin_unlock_irqrestore(&vdec_dev->irqlock, flags); 115 return; 116 } 117 subdev_dev->curr_ctx = ctx; 118 } else { 119 vdec_dev->curr_ctx = ctx; 120 } 121 spin_unlock_irqrestore(&vdec_dev->irqlock, flags); 122 } 123 EXPORT_SYMBOL(mtk_vcodec_set_curr_ctx); 124 125 struct mtk_vcodec_dec_ctx *mtk_vcodec_get_curr_ctx(struct mtk_vcodec_dec_dev *vdec_dev, 126 unsigned int hw_idx) 127 { 128 unsigned long flags; 129 struct mtk_vcodec_dec_ctx *ctx; 130 struct mtk_vdec_hw_dev *subdev_dev; 131 132 spin_lock_irqsave(&vdec_dev->irqlock, flags); 133 if (vdec_dev->vdec_pdata->is_subdev_supported) { 134 subdev_dev = mtk_vcodec_get_hw_dev(vdec_dev, hw_idx); 135 if (!subdev_dev) { 136 dev_err(&vdec_dev->plat_dev->dev, "Failed to get hw dev"); 137 spin_unlock_irqrestore(&vdec_dev->irqlock, flags); 138 return NULL; 139 } 140 ctx = subdev_dev->curr_ctx; 141 } else { 142 ctx = vdec_dev->curr_ctx; 143 } 144 spin_unlock_irqrestore(&vdec_dev->irqlock, flags); 145 return ctx; 146 } 147 EXPORT_SYMBOL(mtk_vcodec_get_curr_ctx); 148 149 MODULE_LICENSE("GPL v2"); 150 MODULE_DESCRIPTION("Mediatek video codec driver"); 151