10934d375SYunfei Dong // SPDX-License-Identifier: GPL-2.0
20934d375SYunfei Dong /*
30934d375SYunfei Dong * Copyright (c) 2016 MediaTek Inc.
40934d375SYunfei Dong * Author: PC Chen <pc.chen@mediatek.com>
50934d375SYunfei Dong *	Tiffany Lin <tiffany.lin@mediatek.com>
60934d375SYunfei Dong */
70934d375SYunfei Dong 
80934d375SYunfei Dong #include <linux/slab.h>
90934d375SYunfei Dong #include <linux/interrupt.h>
100934d375SYunfei Dong #include <linux/irq.h>
110934d375SYunfei Dong #include <linux/module.h>
120934d375SYunfei Dong #include <linux/of.h>
130934d375SYunfei Dong #include <linux/platform_device.h>
140934d375SYunfei Dong #include <linux/pm_runtime.h>
150934d375SYunfei Dong #include <media/v4l2-event.h>
160934d375SYunfei Dong #include <media/v4l2-mem2mem.h>
170934d375SYunfei Dong #include <media/videobuf2-dma-contig.h>
180934d375SYunfei Dong 
190934d375SYunfei Dong #include "mtk_vcodec_enc.h"
200934d375SYunfei Dong #include "mtk_vcodec_enc_pm.h"
210934d375SYunfei Dong #include "../common/mtk_vcodec_intr.h"
220934d375SYunfei Dong 
230934d375SYunfei Dong static const struct mtk_video_fmt mtk_video_formats_output[] = {
240934d375SYunfei Dong 	{
250934d375SYunfei Dong 		.fourcc = V4L2_PIX_FMT_NV12M,
260934d375SYunfei Dong 		.type = MTK_FMT_FRAME,
270934d375SYunfei Dong 		.num_planes = 2,
280934d375SYunfei Dong 	},
290934d375SYunfei Dong 	{
300934d375SYunfei Dong 		.fourcc = V4L2_PIX_FMT_NV21M,
310934d375SYunfei Dong 		.type = MTK_FMT_FRAME,
320934d375SYunfei Dong 		.num_planes = 2,
330934d375SYunfei Dong 	},
340934d375SYunfei Dong 	{
350934d375SYunfei Dong 		.fourcc = V4L2_PIX_FMT_YUV420M,
360934d375SYunfei Dong 		.type = MTK_FMT_FRAME,
370934d375SYunfei Dong 		.num_planes = 3,
380934d375SYunfei Dong 	},
390934d375SYunfei Dong 	{
400934d375SYunfei Dong 		.fourcc = V4L2_PIX_FMT_YVU420M,
410934d375SYunfei Dong 		.type = MTK_FMT_FRAME,
420934d375SYunfei Dong 		.num_planes = 3,
430934d375SYunfei Dong 	},
440934d375SYunfei Dong };
450934d375SYunfei Dong 
460934d375SYunfei Dong static const struct mtk_video_fmt mtk_video_formats_capture_h264[] =  {
470934d375SYunfei Dong 	{
480934d375SYunfei Dong 		.fourcc = V4L2_PIX_FMT_H264,
490934d375SYunfei Dong 		.type = MTK_FMT_ENC,
500934d375SYunfei Dong 		.num_planes = 1,
510934d375SYunfei Dong 	},
520934d375SYunfei Dong };
530934d375SYunfei Dong 
540934d375SYunfei Dong static const struct mtk_video_fmt mtk_video_formats_capture_vp8[] =  {
550934d375SYunfei Dong 	{
560934d375SYunfei Dong 		.fourcc = V4L2_PIX_FMT_VP8,
570934d375SYunfei Dong 		.type = MTK_FMT_ENC,
580934d375SYunfei Dong 		.num_planes = 1,
590934d375SYunfei Dong 	},
600934d375SYunfei Dong };
610934d375SYunfei Dong 
clean_irq_status(unsigned int irq_status,void __iomem * addr)620934d375SYunfei Dong static void clean_irq_status(unsigned int irq_status, void __iomem *addr)
630934d375SYunfei Dong {
640934d375SYunfei Dong 	if (irq_status & MTK_VENC_IRQ_STATUS_PAUSE)
650934d375SYunfei Dong 		writel(MTK_VENC_IRQ_STATUS_PAUSE, addr);
660934d375SYunfei Dong 
670934d375SYunfei Dong 	if (irq_status & MTK_VENC_IRQ_STATUS_SWITCH)
680934d375SYunfei Dong 		writel(MTK_VENC_IRQ_STATUS_SWITCH, addr);
690934d375SYunfei Dong 
700934d375SYunfei Dong 	if (irq_status & MTK_VENC_IRQ_STATUS_DRAM)
710934d375SYunfei Dong 		writel(MTK_VENC_IRQ_STATUS_DRAM, addr);
720934d375SYunfei Dong 
730934d375SYunfei Dong 	if (irq_status & MTK_VENC_IRQ_STATUS_SPS)
740934d375SYunfei Dong 		writel(MTK_VENC_IRQ_STATUS_SPS, addr);
750934d375SYunfei Dong 
760934d375SYunfei Dong 	if (irq_status & MTK_VENC_IRQ_STATUS_PPS)
770934d375SYunfei Dong 		writel(MTK_VENC_IRQ_STATUS_PPS, addr);
780934d375SYunfei Dong 
790934d375SYunfei Dong 	if (irq_status & MTK_VENC_IRQ_STATUS_FRM)
800934d375SYunfei Dong 		writel(MTK_VENC_IRQ_STATUS_FRM, addr);
810934d375SYunfei Dong 
820934d375SYunfei Dong }
mtk_vcodec_enc_irq_handler(int irq,void * priv)830934d375SYunfei Dong static irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv)
840934d375SYunfei Dong {
850934d375SYunfei Dong 	struct mtk_vcodec_enc_dev *dev = priv;
860934d375SYunfei Dong 	struct mtk_vcodec_enc_ctx *ctx;
870934d375SYunfei Dong 	unsigned long flags;
880934d375SYunfei Dong 	void __iomem *addr;
890934d375SYunfei Dong 	int core_id;
900934d375SYunfei Dong 
910934d375SYunfei Dong 	spin_lock_irqsave(&dev->irqlock, flags);
920934d375SYunfei Dong 	ctx = dev->curr_ctx;
930934d375SYunfei Dong 	spin_unlock_irqrestore(&dev->irqlock, flags);
940934d375SYunfei Dong 
950934d375SYunfei Dong 	core_id = dev->venc_pdata->core_id;
960934d375SYunfei Dong 	if (core_id < 0 || core_id >= NUM_MAX_VCODEC_REG_BASE) {
970934d375SYunfei Dong 		mtk_v4l2_venc_err(ctx, "Invalid core id: %d, ctx id: %d", core_id, ctx->id);
980934d375SYunfei Dong 		return IRQ_HANDLED;
990934d375SYunfei Dong 	}
1000934d375SYunfei Dong 
1010934d375SYunfei Dong 	mtk_v4l2_venc_dbg(1, ctx, "id: %d, core id: %d", ctx->id, core_id);
1020934d375SYunfei Dong 
1030934d375SYunfei Dong 	addr = dev->reg_base[core_id] + MTK_VENC_IRQ_ACK_OFFSET;
1040934d375SYunfei Dong 
1050934d375SYunfei Dong 	ctx->irq_status = readl(dev->reg_base[core_id] +
1060934d375SYunfei Dong 				(MTK_VENC_IRQ_STATUS_OFFSET));
1070934d375SYunfei Dong 
1080934d375SYunfei Dong 	clean_irq_status(ctx->irq_status, addr);
1090934d375SYunfei Dong 
1100934d375SYunfei Dong 	wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED, 0);
1110934d375SYunfei Dong 	return IRQ_HANDLED;
1120934d375SYunfei Dong }
1130934d375SYunfei Dong 
fops_vcodec_open(struct file * file)1140934d375SYunfei Dong static int fops_vcodec_open(struct file *file)
1150934d375SYunfei Dong {
1160934d375SYunfei Dong 	struct mtk_vcodec_enc_dev *dev = video_drvdata(file);
1170934d375SYunfei Dong 	struct mtk_vcodec_enc_ctx *ctx = NULL;
1180934d375SYunfei Dong 	int ret = 0;
1190934d375SYunfei Dong 	struct vb2_queue *src_vq;
1200934d375SYunfei Dong 
1210934d375SYunfei Dong 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1220934d375SYunfei Dong 	if (!ctx)
1230934d375SYunfei Dong 		return -ENOMEM;
1240934d375SYunfei Dong 
1250934d375SYunfei Dong 	mutex_lock(&dev->dev_mutex);
1260934d375SYunfei Dong 	/*
1270934d375SYunfei Dong 	 * Use simple counter to uniquely identify this context. Only
1280934d375SYunfei Dong 	 * used for logging.
1290934d375SYunfei Dong 	 */
1300934d375SYunfei Dong 	ctx->id = dev->id_counter++;
1310934d375SYunfei Dong 	v4l2_fh_init(&ctx->fh, video_devdata(file));
1320934d375SYunfei Dong 	file->private_data = &ctx->fh;
1330934d375SYunfei Dong 	v4l2_fh_add(&ctx->fh);
1340934d375SYunfei Dong 	INIT_LIST_HEAD(&ctx->list);
1350934d375SYunfei Dong 	ctx->dev = dev;
1360934d375SYunfei Dong 	init_waitqueue_head(&ctx->queue[0]);
1370934d375SYunfei Dong 	mutex_init(&ctx->q_mutex);
1380934d375SYunfei Dong 
1390934d375SYunfei Dong 	ctx->type = MTK_INST_ENCODER;
1400934d375SYunfei Dong 	ret = mtk_vcodec_enc_ctrls_setup(ctx);
1410934d375SYunfei Dong 	if (ret) {
1420934d375SYunfei Dong 		mtk_v4l2_venc_err(ctx, "Failed to setup controls() (%d)", ret);
1430934d375SYunfei Dong 		goto err_ctrls_setup;
1440934d375SYunfei Dong 	}
1450934d375SYunfei Dong 	ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_enc, ctx,
1460934d375SYunfei Dong 					 &mtk_vcodec_enc_queue_init);
1470934d375SYunfei Dong 	if (IS_ERR((__force void *)ctx->m2m_ctx)) {
1480934d375SYunfei Dong 		ret = PTR_ERR((__force void *)ctx->m2m_ctx);
1490934d375SYunfei Dong 		mtk_v4l2_venc_err(ctx, "Failed to v4l2_m2m_ctx_init() (%d)", ret);
1500934d375SYunfei Dong 		goto err_m2m_ctx_init;
1510934d375SYunfei Dong 	}
1520934d375SYunfei Dong 	src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
1530934d375SYunfei Dong 				 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
1540934d375SYunfei Dong 	ctx->empty_flush_buf.vb.vb2_buf.vb2_queue = src_vq;
1550934d375SYunfei Dong 	mtk_vcodec_enc_set_default_params(ctx);
1560934d375SYunfei Dong 
1570934d375SYunfei Dong 	if (v4l2_fh_is_singular(&ctx->fh)) {
1580934d375SYunfei Dong 		/*
1590934d375SYunfei Dong 		 * load fireware to checks if it was loaded already and
1600934d375SYunfei Dong 		 * does nothing in that case
1610934d375SYunfei Dong 		 */
1620934d375SYunfei Dong 		ret = mtk_vcodec_fw_load_firmware(dev->fw_handler);
1630934d375SYunfei Dong 		if (ret < 0) {
1640934d375SYunfei Dong 			/*
1650934d375SYunfei Dong 			 * Return 0 if downloading firmware successfully,
1660934d375SYunfei Dong 			 * otherwise it is failed
1670934d375SYunfei Dong 			 */
1680934d375SYunfei Dong 			mtk_v4l2_venc_err(ctx, "vpu_load_firmware failed!");
1690934d375SYunfei Dong 			goto err_load_fw;
1700934d375SYunfei Dong 		}
1710934d375SYunfei Dong 
1720934d375SYunfei Dong 		dev->enc_capability =
1730934d375SYunfei Dong 			mtk_vcodec_fw_get_venc_capa(dev->fw_handler);
1740934d375SYunfei Dong 		mtk_v4l2_venc_dbg(0, ctx, "encoder capability %x", dev->enc_capability);
1750934d375SYunfei Dong 	}
1760934d375SYunfei Dong 
1770934d375SYunfei Dong 	mtk_v4l2_venc_dbg(2, ctx, "Create instance [%d]@%p m2m_ctx=%p ",
1780934d375SYunfei Dong 			  ctx->id, ctx, ctx->m2m_ctx);
1790934d375SYunfei Dong 
18041671f0cSYunfei Dong 	mutex_lock(&dev->dev_ctx_lock);
1810934d375SYunfei Dong 	list_add(&ctx->list, &dev->ctx_list);
18241671f0cSYunfei Dong 	mutex_unlock(&dev->dev_ctx_lock);
1830934d375SYunfei Dong 
1840934d375SYunfei Dong 	mutex_unlock(&dev->dev_mutex);
1850934d375SYunfei Dong 	mtk_v4l2_venc_dbg(0, ctx, "%s encoder [%d]", dev_name(&dev->plat_dev->dev),
1860934d375SYunfei Dong 			  ctx->id);
1870934d375SYunfei Dong 	return ret;
1880934d375SYunfei Dong 
1890934d375SYunfei Dong 	/* Deinit when failure occurred */
1900934d375SYunfei Dong err_load_fw:
1910934d375SYunfei Dong 	v4l2_m2m_ctx_release(ctx->m2m_ctx);
1920934d375SYunfei Dong err_m2m_ctx_init:
1930934d375SYunfei Dong 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
1940934d375SYunfei Dong err_ctrls_setup:
1950934d375SYunfei Dong 	v4l2_fh_del(&ctx->fh);
1960934d375SYunfei Dong 	v4l2_fh_exit(&ctx->fh);
1970934d375SYunfei Dong 	kfree(ctx);
1980934d375SYunfei Dong 	mutex_unlock(&dev->dev_mutex);
1990934d375SYunfei Dong 
2000934d375SYunfei Dong 	return ret;
2010934d375SYunfei Dong }
2020934d375SYunfei Dong 
fops_vcodec_release(struct file * file)2030934d375SYunfei Dong static int fops_vcodec_release(struct file *file)
2040934d375SYunfei Dong {
2050934d375SYunfei Dong 	struct mtk_vcodec_enc_dev *dev = video_drvdata(file);
2060934d375SYunfei Dong 	struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(file->private_data);
2070934d375SYunfei Dong 
2080934d375SYunfei Dong 	mtk_v4l2_venc_dbg(1, ctx, "[%d] encoder", ctx->id);
2090934d375SYunfei Dong 	mutex_lock(&dev->dev_mutex);
2100934d375SYunfei Dong 
2110934d375SYunfei Dong 	v4l2_m2m_ctx_release(ctx->m2m_ctx);
2120934d375SYunfei Dong 	mtk_vcodec_enc_release(ctx);
2130934d375SYunfei Dong 	v4l2_fh_del(&ctx->fh);
2140934d375SYunfei Dong 	v4l2_fh_exit(&ctx->fh);
2150934d375SYunfei Dong 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
2160934d375SYunfei Dong 
21741671f0cSYunfei Dong 	mutex_lock(&dev->dev_ctx_lock);
2180934d375SYunfei Dong 	list_del_init(&ctx->list);
21941671f0cSYunfei Dong 	mutex_unlock(&dev->dev_ctx_lock);
2200934d375SYunfei Dong 	kfree(ctx);
2210934d375SYunfei Dong 	mutex_unlock(&dev->dev_mutex);
2220934d375SYunfei Dong 	return 0;
2230934d375SYunfei Dong }
2240934d375SYunfei Dong 
2250934d375SYunfei Dong static const struct v4l2_file_operations mtk_vcodec_fops = {
2260934d375SYunfei Dong 	.owner		= THIS_MODULE,
2270934d375SYunfei Dong 	.open		= fops_vcodec_open,
2280934d375SYunfei Dong 	.release	= fops_vcodec_release,
2290934d375SYunfei Dong 	.poll		= v4l2_m2m_fop_poll,
2300934d375SYunfei Dong 	.unlocked_ioctl	= video_ioctl2,
2310934d375SYunfei Dong 	.mmap		= v4l2_m2m_fop_mmap,
2320934d375SYunfei Dong };
2330934d375SYunfei Dong 
mtk_vcodec_probe(struct platform_device * pdev)2340934d375SYunfei Dong static int mtk_vcodec_probe(struct platform_device *pdev)
2350934d375SYunfei Dong {
2360934d375SYunfei Dong 	struct mtk_vcodec_enc_dev *dev;
2370934d375SYunfei Dong 	struct video_device *vfd_enc;
2380934d375SYunfei Dong 	phandle rproc_phandle;
2390934d375SYunfei Dong 	enum mtk_vcodec_fw_type fw_type;
2400934d375SYunfei Dong 	int ret;
2410934d375SYunfei Dong 
2420934d375SYunfei Dong 	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2430934d375SYunfei Dong 	if (!dev)
2440934d375SYunfei Dong 		return -ENOMEM;
2450934d375SYunfei Dong 
2460934d375SYunfei Dong 	INIT_LIST_HEAD(&dev->ctx_list);
2470934d375SYunfei Dong 	dev->plat_dev = pdev;
2480934d375SYunfei Dong 
2490934d375SYunfei Dong 	if (!of_property_read_u32(pdev->dev.of_node, "mediatek,vpu",
2500934d375SYunfei Dong 				  &rproc_phandle)) {
2510934d375SYunfei Dong 		fw_type = VPU;
2520934d375SYunfei Dong 	} else if (!of_property_read_u32(pdev->dev.of_node, "mediatek,scp",
2530934d375SYunfei Dong 					 &rproc_phandle)) {
2540934d375SYunfei Dong 		fw_type = SCP;
2550934d375SYunfei Dong 	} else {
2560934d375SYunfei Dong 		dev_err(&pdev->dev, "[MTK VCODEC] Could not get venc IPI device");
2570934d375SYunfei Dong 		return -ENODEV;
2580934d375SYunfei Dong 	}
2590934d375SYunfei Dong 	dma_set_max_seg_size(&pdev->dev, UINT_MAX);
2600934d375SYunfei Dong 
2610934d375SYunfei Dong 	dev->fw_handler = mtk_vcodec_fw_select(dev, fw_type, ENCODER);
2620934d375SYunfei Dong 	if (IS_ERR(dev->fw_handler))
2630934d375SYunfei Dong 		return PTR_ERR(dev->fw_handler);
2640934d375SYunfei Dong 
2650934d375SYunfei Dong 	dev->venc_pdata = of_device_get_match_data(&pdev->dev);
2660934d375SYunfei Dong 	ret = mtk_vcodec_init_enc_clk(dev);
2670934d375SYunfei Dong 	if (ret < 0) {
2680934d375SYunfei Dong 		dev_err(&pdev->dev, "[MTK VCODEC] Failed to get mtk vcodec clock source!");
2690934d375SYunfei Dong 		goto err_enc_pm;
2700934d375SYunfei Dong 	}
2710934d375SYunfei Dong 
2720934d375SYunfei Dong 	pm_runtime_enable(&pdev->dev);
2730934d375SYunfei Dong 
2740934d375SYunfei Dong 	dev->reg_base[dev->venc_pdata->core_id] =
2750934d375SYunfei Dong 		devm_platform_ioremap_resource(pdev, 0);
2760934d375SYunfei Dong 	if (IS_ERR(dev->reg_base[dev->venc_pdata->core_id])) {
2770934d375SYunfei Dong 		ret = PTR_ERR(dev->reg_base[dev->venc_pdata->core_id]);
2780934d375SYunfei Dong 		goto err_res;
2790934d375SYunfei Dong 	}
2800934d375SYunfei Dong 
2810934d375SYunfei Dong 	dev->enc_irq = platform_get_irq(pdev, 0);
2820934d375SYunfei Dong 	if (dev->enc_irq < 0) {
2830934d375SYunfei Dong 		ret = dev->enc_irq;
2840934d375SYunfei Dong 		goto err_res;
2850934d375SYunfei Dong 	}
2860934d375SYunfei Dong 
2870934d375SYunfei Dong 	irq_set_status_flags(dev->enc_irq, IRQ_NOAUTOEN);
2880934d375SYunfei Dong 	ret = devm_request_irq(&pdev->dev, dev->enc_irq,
2890934d375SYunfei Dong 			       mtk_vcodec_enc_irq_handler,
2900934d375SYunfei Dong 			       0, pdev->name, dev);
2910934d375SYunfei Dong 	if (ret) {
2920934d375SYunfei Dong 		dev_err(&pdev->dev,
2930934d375SYunfei Dong 			"[MTK VCODEC] Failed to install dev->enc_irq %d (%d) core_id (%d)",
2940934d375SYunfei Dong 			dev->enc_irq, ret, dev->venc_pdata->core_id);
2950934d375SYunfei Dong 		ret = -EINVAL;
2960934d375SYunfei Dong 		goto err_res;
2970934d375SYunfei Dong 	}
2980934d375SYunfei Dong 
2990934d375SYunfei Dong 	mutex_init(&dev->enc_mutex);
3000934d375SYunfei Dong 	mutex_init(&dev->dev_mutex);
30141671f0cSYunfei Dong 	mutex_init(&dev->dev_ctx_lock);
3020934d375SYunfei Dong 	spin_lock_init(&dev->irqlock);
3030934d375SYunfei Dong 
3040934d375SYunfei Dong 	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
3050934d375SYunfei Dong 		 "[MTK_V4L2_VENC]");
3060934d375SYunfei Dong 
3070934d375SYunfei Dong 	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3080934d375SYunfei Dong 	if (ret) {
3090934d375SYunfei Dong 		dev_err(&pdev->dev, "[MTK VCODEC] v4l2_device_register err=%d", ret);
3100934d375SYunfei Dong 		goto err_res;
3110934d375SYunfei Dong 	}
3120934d375SYunfei Dong 
3130934d375SYunfei Dong 	/* allocate video device for encoder and register it */
3140934d375SYunfei Dong 	vfd_enc = video_device_alloc();
3150934d375SYunfei Dong 	if (!vfd_enc) {
3160934d375SYunfei Dong 		dev_err(&pdev->dev, "[MTK VCODEC] Failed to allocate video device");
3170934d375SYunfei Dong 		ret = -ENOMEM;
3180934d375SYunfei Dong 		goto err_enc_alloc;
3190934d375SYunfei Dong 	}
3200934d375SYunfei Dong 	vfd_enc->fops           = &mtk_vcodec_fops;
3210934d375SYunfei Dong 	vfd_enc->ioctl_ops      = &mtk_venc_ioctl_ops;
3220934d375SYunfei Dong 	vfd_enc->release        = video_device_release;
3230934d375SYunfei Dong 	vfd_enc->lock           = &dev->dev_mutex;
3240934d375SYunfei Dong 	vfd_enc->v4l2_dev       = &dev->v4l2_dev;
3250934d375SYunfei Dong 	vfd_enc->vfl_dir        = VFL_DIR_M2M;
3260934d375SYunfei Dong 	vfd_enc->device_caps	= V4L2_CAP_VIDEO_M2M_MPLANE |
3270934d375SYunfei Dong 					V4L2_CAP_STREAMING;
3280934d375SYunfei Dong 
3290934d375SYunfei Dong 	snprintf(vfd_enc->name, sizeof(vfd_enc->name), "%s",
3300934d375SYunfei Dong 		 MTK_VCODEC_ENC_NAME);
3310934d375SYunfei Dong 	video_set_drvdata(vfd_enc, dev);
3320934d375SYunfei Dong 	dev->vfd_enc = vfd_enc;
3330934d375SYunfei Dong 	platform_set_drvdata(pdev, dev);
3340934d375SYunfei Dong 
3350934d375SYunfei Dong 	dev->m2m_dev_enc = v4l2_m2m_init(&mtk_venc_m2m_ops);
3360934d375SYunfei Dong 	if (IS_ERR((__force void *)dev->m2m_dev_enc)) {
3370934d375SYunfei Dong 		dev_err(&pdev->dev, "[MTK VCODEC] Failed to init mem2mem enc device");
3380934d375SYunfei Dong 		ret = PTR_ERR((__force void *)dev->m2m_dev_enc);
3390934d375SYunfei Dong 		goto err_enc_mem_init;
3400934d375SYunfei Dong 	}
3410934d375SYunfei Dong 
3420934d375SYunfei Dong 	dev->encode_workqueue =
3430934d375SYunfei Dong 			alloc_ordered_workqueue(MTK_VCODEC_ENC_NAME,
3440934d375SYunfei Dong 						WQ_MEM_RECLAIM |
3450934d375SYunfei Dong 						WQ_FREEZABLE);
3460934d375SYunfei Dong 	if (!dev->encode_workqueue) {
3470934d375SYunfei Dong 		dev_err(&pdev->dev, "[MTK VCODEC] Failed to create encode workqueue");
3480934d375SYunfei Dong 		ret = -EINVAL;
3490934d375SYunfei Dong 		goto err_event_workq;
3500934d375SYunfei Dong 	}
3510934d375SYunfei Dong 
3520934d375SYunfei Dong 	ret = video_register_device(vfd_enc, VFL_TYPE_VIDEO, -1);
3530934d375SYunfei Dong 	if (ret) {
3540934d375SYunfei Dong 		dev_err(&pdev->dev, "[MTK VCODEC] Failed to register video device");
3550934d375SYunfei Dong 		goto err_enc_reg;
3560934d375SYunfei Dong 	}
3570934d375SYunfei Dong 
3580934d375SYunfei Dong 	mtk_vcodec_dbgfs_init(dev, true);
3590934d375SYunfei Dong 	dev_dbg(&pdev->dev,  "[MTK VCODEC] encoder %d registered as /dev/video%d",
3600934d375SYunfei Dong 		dev->venc_pdata->core_id, vfd_enc->num);
3610934d375SYunfei Dong 
3620934d375SYunfei Dong 	return 0;
3630934d375SYunfei Dong 
3640934d375SYunfei Dong err_enc_reg:
3650934d375SYunfei Dong 	destroy_workqueue(dev->encode_workqueue);
3660934d375SYunfei Dong err_event_workq:
3670934d375SYunfei Dong 	v4l2_m2m_release(dev->m2m_dev_enc);
3680934d375SYunfei Dong err_enc_mem_init:
3690934d375SYunfei Dong 	video_unregister_device(vfd_enc);
3700934d375SYunfei Dong err_enc_alloc:
3710934d375SYunfei Dong 	v4l2_device_unregister(&dev->v4l2_dev);
3720934d375SYunfei Dong err_res:
3730934d375SYunfei Dong 	pm_runtime_disable(dev->pm.dev);
3740934d375SYunfei Dong err_enc_pm:
3750934d375SYunfei Dong 	mtk_vcodec_fw_release(dev->fw_handler);
3760934d375SYunfei Dong 	return ret;
3770934d375SYunfei Dong }
3780934d375SYunfei Dong 
3790934d375SYunfei Dong static const struct mtk_vcodec_enc_pdata mt8173_avc_pdata = {
3800934d375SYunfei Dong 	.capture_formats = mtk_video_formats_capture_h264,
3810934d375SYunfei Dong 	.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
3820934d375SYunfei Dong 	.output_formats = mtk_video_formats_output,
3830934d375SYunfei Dong 	.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
3840934d375SYunfei Dong 	.min_bitrate = 64,
3850934d375SYunfei Dong 	.max_bitrate = 60000000,
3860934d375SYunfei Dong 	.core_id = VENC_SYS,
3870934d375SYunfei Dong };
3880934d375SYunfei Dong 
3890934d375SYunfei Dong static const struct mtk_vcodec_enc_pdata mt8173_vp8_pdata = {
3900934d375SYunfei Dong 	.capture_formats = mtk_video_formats_capture_vp8,
3910934d375SYunfei Dong 	.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_vp8),
3920934d375SYunfei Dong 	.output_formats = mtk_video_formats_output,
3930934d375SYunfei Dong 	.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
3940934d375SYunfei Dong 	.min_bitrate = 64,
3950934d375SYunfei Dong 	.max_bitrate = 9000000,
3960934d375SYunfei Dong 	.core_id = VENC_LT_SYS,
3970934d375SYunfei Dong };
3980934d375SYunfei Dong 
3990934d375SYunfei Dong static const struct mtk_vcodec_enc_pdata mt8183_pdata = {
4000934d375SYunfei Dong 	.uses_ext = true,
4010934d375SYunfei Dong 	.capture_formats = mtk_video_formats_capture_h264,
4020934d375SYunfei Dong 	.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
4030934d375SYunfei Dong 	.output_formats = mtk_video_formats_output,
4040934d375SYunfei Dong 	.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
4050934d375SYunfei Dong 	.min_bitrate = 64,
4060934d375SYunfei Dong 	.max_bitrate = 40000000,
4070934d375SYunfei Dong 	.core_id = VENC_SYS,
4080934d375SYunfei Dong };
4090934d375SYunfei Dong 
4100934d375SYunfei Dong static const struct mtk_vcodec_enc_pdata mt8188_pdata = {
4110934d375SYunfei Dong 	.uses_ext = true,
4120934d375SYunfei Dong 	.capture_formats = mtk_video_formats_capture_h264,
4130934d375SYunfei Dong 	.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
4140934d375SYunfei Dong 	.output_formats = mtk_video_formats_output,
4150934d375SYunfei Dong 	.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
4160934d375SYunfei Dong 	.min_bitrate = 64,
4170934d375SYunfei Dong 	.max_bitrate = 50000000,
4180934d375SYunfei Dong 	.core_id = VENC_SYS,
4190934d375SYunfei Dong 	.uses_34bit = true,
4200934d375SYunfei Dong };
4210934d375SYunfei Dong 
4220934d375SYunfei Dong static const struct mtk_vcodec_enc_pdata mt8192_pdata = {
4230934d375SYunfei Dong 	.uses_ext = true,
4240934d375SYunfei Dong 	.capture_formats = mtk_video_formats_capture_h264,
4250934d375SYunfei Dong 	.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
4260934d375SYunfei Dong 	.output_formats = mtk_video_formats_output,
4270934d375SYunfei Dong 	.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
4280934d375SYunfei Dong 	.min_bitrate = 64,
4290934d375SYunfei Dong 	.max_bitrate = 100000000,
4300934d375SYunfei Dong 	.core_id = VENC_SYS,
4310934d375SYunfei Dong };
4320934d375SYunfei Dong 
4330934d375SYunfei Dong static const struct mtk_vcodec_enc_pdata mt8195_pdata = {
4340934d375SYunfei Dong 	.uses_ext = true,
4350934d375SYunfei Dong 	.capture_formats = mtk_video_formats_capture_h264,
4360934d375SYunfei Dong 	.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
4370934d375SYunfei Dong 	.output_formats = mtk_video_formats_output,
4380934d375SYunfei Dong 	.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
4390934d375SYunfei Dong 	.min_bitrate = 64,
4400934d375SYunfei Dong 	.max_bitrate = 100000000,
4410934d375SYunfei Dong 	.core_id = VENC_SYS,
4420934d375SYunfei Dong };
4430934d375SYunfei Dong 
4440934d375SYunfei Dong static const struct of_device_id mtk_vcodec_enc_match[] = {
4450934d375SYunfei Dong 	{.compatible = "mediatek,mt8173-vcodec-enc",
4460934d375SYunfei Dong 			.data = &mt8173_avc_pdata},
4470934d375SYunfei Dong 	{.compatible = "mediatek,mt8173-vcodec-enc-vp8",
4480934d375SYunfei Dong 			.data = &mt8173_vp8_pdata},
4490934d375SYunfei Dong 	{.compatible = "mediatek,mt8183-vcodec-enc", .data = &mt8183_pdata},
4500934d375SYunfei Dong 	{.compatible = "mediatek,mt8188-vcodec-enc", .data = &mt8188_pdata},
4510934d375SYunfei Dong 	{.compatible = "mediatek,mt8192-vcodec-enc", .data = &mt8192_pdata},
4520934d375SYunfei Dong 	{.compatible = "mediatek,mt8195-vcodec-enc", .data = &mt8195_pdata},
4530934d375SYunfei Dong 	{},
4540934d375SYunfei Dong };
4550934d375SYunfei Dong MODULE_DEVICE_TABLE(of, mtk_vcodec_enc_match);
4560934d375SYunfei Dong 
mtk_vcodec_enc_remove(struct platform_device * pdev)4570934d375SYunfei Dong static void mtk_vcodec_enc_remove(struct platform_device *pdev)
4580934d375SYunfei Dong {
4590934d375SYunfei Dong 	struct mtk_vcodec_enc_dev *dev = platform_get_drvdata(pdev);
4600934d375SYunfei Dong 
4610934d375SYunfei Dong 	destroy_workqueue(dev->encode_workqueue);
4620934d375SYunfei Dong 	if (dev->m2m_dev_enc)
4630934d375SYunfei Dong 		v4l2_m2m_release(dev->m2m_dev_enc);
4640934d375SYunfei Dong 
4650934d375SYunfei Dong 	if (dev->vfd_enc)
4660934d375SYunfei Dong 		video_unregister_device(dev->vfd_enc);
4670934d375SYunfei Dong 
4680934d375SYunfei Dong 	mtk_vcodec_dbgfs_deinit(&dev->dbgfs);
4690934d375SYunfei Dong 	v4l2_device_unregister(&dev->v4l2_dev);
4700934d375SYunfei Dong 	pm_runtime_disable(dev->pm.dev);
4710934d375SYunfei Dong 	mtk_vcodec_fw_release(dev->fw_handler);
4720934d375SYunfei Dong }
4730934d375SYunfei Dong 
4740934d375SYunfei Dong static struct platform_driver mtk_vcodec_enc_driver = {
4750934d375SYunfei Dong 	.probe	= mtk_vcodec_probe,
4760934d375SYunfei Dong 	.remove_new = mtk_vcodec_enc_remove,
4770934d375SYunfei Dong 	.driver	= {
4780934d375SYunfei Dong 		.name	= MTK_VCODEC_ENC_NAME,
4790934d375SYunfei Dong 		.of_match_table = mtk_vcodec_enc_match,
4800934d375SYunfei Dong 	},
4810934d375SYunfei Dong };
4820934d375SYunfei Dong 
4830934d375SYunfei Dong module_platform_driver(mtk_vcodec_enc_driver);
4840934d375SYunfei Dong 
4850934d375SYunfei Dong 
4860934d375SYunfei Dong MODULE_LICENSE("GPL v2");
4870934d375SYunfei Dong MODULE_DESCRIPTION("Mediatek video codec V4L2 encoder driver");
488