1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <pc.chen@mediatek.com>
5 * Tiffany Lin <tiffany.lin@mediatek.com>
8 #include <linux/slab.h>
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
14 #include <media/v4l2-event.h>
15 #include <media/v4l2-mem2mem.h>
16 #include <media/videobuf2-dma-contig.h>
17 #include <linux/pm_runtime.h>
19 #include "mtk_vcodec_drv.h"
20 #include "mtk_vcodec_enc.h"
21 #include "mtk_vcodec_enc_pm.h"
22 #include "mtk_vcodec_intr.h"
23 #include "mtk_vcodec_util.h"
26 module_param(mtk_v4l2_dbg_level
, int, S_IRUGO
| S_IWUSR
);
27 module_param(mtk_vcodec_dbg
, bool, S_IRUGO
| S_IWUSR
);
29 /* Wake up context wait_queue */
30 static void wake_up_ctx(struct mtk_vcodec_ctx
*ctx
, unsigned int reason
)
33 ctx
->int_type
= reason
;
34 wake_up_interruptible(&ctx
->queue
);
37 static void clean_irq_status(unsigned int irq_status
, void __iomem
*addr
)
39 if (irq_status
& MTK_VENC_IRQ_STATUS_PAUSE
)
40 writel(MTK_VENC_IRQ_STATUS_PAUSE
, addr
);
42 if (irq_status
& MTK_VENC_IRQ_STATUS_SWITCH
)
43 writel(MTK_VENC_IRQ_STATUS_SWITCH
, addr
);
45 if (irq_status
& MTK_VENC_IRQ_STATUS_DRAM
)
46 writel(MTK_VENC_IRQ_STATUS_DRAM
, addr
);
48 if (irq_status
& MTK_VENC_IRQ_STATUS_SPS
)
49 writel(MTK_VENC_IRQ_STATUS_SPS
, addr
);
51 if (irq_status
& MTK_VENC_IRQ_STATUS_PPS
)
52 writel(MTK_VENC_IRQ_STATUS_PPS
, addr
);
54 if (irq_status
& MTK_VENC_IRQ_STATUS_FRM
)
55 writel(MTK_VENC_IRQ_STATUS_FRM
, addr
);
58 static irqreturn_t
mtk_vcodec_enc_irq_handler(int irq
, void *priv
)
60 struct mtk_vcodec_dev
*dev
= priv
;
61 struct mtk_vcodec_ctx
*ctx
;
65 spin_lock_irqsave(&dev
->irqlock
, flags
);
67 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
69 mtk_v4l2_debug(1, "id=%d", ctx
->id
);
70 addr
= dev
->reg_base
[VENC_SYS
] + MTK_VENC_IRQ_ACK_OFFSET
;
72 ctx
->irq_status
= readl(dev
->reg_base
[VENC_SYS
] +
73 (MTK_VENC_IRQ_STATUS_OFFSET
));
75 clean_irq_status(ctx
->irq_status
, addr
);
77 wake_up_ctx(ctx
, MTK_INST_IRQ_RECEIVED
);
81 static irqreturn_t
mtk_vcodec_enc_lt_irq_handler(int irq
, void *priv
)
83 struct mtk_vcodec_dev
*dev
= priv
;
84 struct mtk_vcodec_ctx
*ctx
;
88 spin_lock_irqsave(&dev
->irqlock
, flags
);
90 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
92 mtk_v4l2_debug(1, "id=%d", ctx
->id
);
93 ctx
->irq_status
= readl(dev
->reg_base
[VENC_LT_SYS
] +
94 (MTK_VENC_IRQ_STATUS_OFFSET
));
96 addr
= dev
->reg_base
[VENC_LT_SYS
] + MTK_VENC_IRQ_ACK_OFFSET
;
98 clean_irq_status(ctx
->irq_status
, addr
);
100 wake_up_ctx(ctx
, MTK_INST_IRQ_RECEIVED
);
104 static void mtk_vcodec_enc_reset_handler(void *priv
)
106 struct mtk_vcodec_dev
*dev
= priv
;
107 struct mtk_vcodec_ctx
*ctx
;
109 mtk_v4l2_debug(0, "Watchdog timeout!!");
111 mutex_lock(&dev
->dev_mutex
);
112 list_for_each_entry(ctx
, &dev
->ctx_list
, list
) {
113 ctx
->state
= MTK_STATE_ABORT
;
114 mtk_v4l2_debug(0, "[%d] Change to state MTK_STATE_ABORT",
117 mutex_unlock(&dev
->dev_mutex
);
120 static int fops_vcodec_open(struct file
*file
)
122 struct mtk_vcodec_dev
*dev
= video_drvdata(file
);
123 struct mtk_vcodec_ctx
*ctx
= NULL
;
126 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
130 mutex_lock(&dev
->dev_mutex
);
132 * Use simple counter to uniquely identify this context. Only
135 ctx
->id
= dev
->id_counter
++;
136 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
137 file
->private_data
= &ctx
->fh
;
138 v4l2_fh_add(&ctx
->fh
);
139 INIT_LIST_HEAD(&ctx
->list
);
141 init_waitqueue_head(&ctx
->queue
);
143 ctx
->type
= MTK_INST_ENCODER
;
144 ret
= mtk_vcodec_enc_ctrls_setup(ctx
);
146 mtk_v4l2_err("Failed to setup controls() (%d)",
148 goto err_ctrls_setup
;
150 ctx
->m2m_ctx
= v4l2_m2m_ctx_init(dev
->m2m_dev_enc
, ctx
,
151 &mtk_vcodec_enc_queue_init
);
152 if (IS_ERR((__force
void *)ctx
->m2m_ctx
)) {
153 ret
= PTR_ERR((__force
void *)ctx
->m2m_ctx
);
154 mtk_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)",
156 goto err_m2m_ctx_init
;
158 mtk_vcodec_enc_set_default_params(ctx
);
160 if (v4l2_fh_is_singular(&ctx
->fh
)) {
162 * vpu_load_firmware checks if it was loaded already and
163 * does nothing in that case
165 ret
= vpu_load_firmware(dev
->vpu_plat_dev
);
168 * Return 0 if downloading firmware successfully,
169 * otherwise it is failed
171 mtk_v4l2_err("vpu_load_firmware failed!");
175 dev
->enc_capability
=
176 vpu_get_venc_hw_capa(dev
->vpu_plat_dev
);
177 mtk_v4l2_debug(0, "encoder capability %x", dev
->enc_capability
);
180 mtk_v4l2_debug(2, "Create instance [%d]@%p m2m_ctx=%p ",
181 ctx
->id
, ctx
, ctx
->m2m_ctx
);
183 list_add(&ctx
->list
, &dev
->ctx_list
);
185 mutex_unlock(&dev
->dev_mutex
);
186 mtk_v4l2_debug(0, "%s encoder [%d]", dev_name(&dev
->plat_dev
->dev
),
190 /* Deinit when failure occurred */
192 v4l2_m2m_ctx_release(ctx
->m2m_ctx
);
194 v4l2_ctrl_handler_free(&ctx
->ctrl_hdl
);
196 v4l2_fh_del(&ctx
->fh
);
197 v4l2_fh_exit(&ctx
->fh
);
199 mutex_unlock(&dev
->dev_mutex
);
204 static int fops_vcodec_release(struct file
*file
)
206 struct mtk_vcodec_dev
*dev
= video_drvdata(file
);
207 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(file
->private_data
);
209 mtk_v4l2_debug(1, "[%d] encoder", ctx
->id
);
210 mutex_lock(&dev
->dev_mutex
);
212 mtk_vcodec_enc_release(ctx
);
213 v4l2_fh_del(&ctx
->fh
);
214 v4l2_fh_exit(&ctx
->fh
);
215 v4l2_ctrl_handler_free(&ctx
->ctrl_hdl
);
216 v4l2_m2m_ctx_release(ctx
->m2m_ctx
);
218 list_del_init(&ctx
->list
);
220 mutex_unlock(&dev
->dev_mutex
);
224 static const struct v4l2_file_operations mtk_vcodec_fops
= {
225 .owner
= THIS_MODULE
,
226 .open
= fops_vcodec_open
,
227 .release
= fops_vcodec_release
,
228 .poll
= v4l2_m2m_fop_poll
,
229 .unlocked_ioctl
= video_ioctl2
,
230 .mmap
= v4l2_m2m_fop_mmap
,
233 static int mtk_vcodec_probe(struct platform_device
*pdev
)
235 struct mtk_vcodec_dev
*dev
;
236 struct video_device
*vfd_enc
;
237 struct resource
*res
;
240 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
244 INIT_LIST_HEAD(&dev
->ctx_list
);
245 dev
->plat_dev
= pdev
;
247 dev
->vpu_plat_dev
= vpu_get_plat_device(dev
->plat_dev
);
248 if (dev
->vpu_plat_dev
== NULL
) {
249 mtk_v4l2_err("[VPU] vpu device in not ready");
250 return -EPROBE_DEFER
;
253 vpu_wdt_reg_handler(dev
->vpu_plat_dev
, mtk_vcodec_enc_reset_handler
,
256 ret
= mtk_vcodec_init_enc_pm(dev
);
258 dev_err(&pdev
->dev
, "Failed to get mt vcodec clock source!");
262 for (i
= VENC_SYS
, j
= 0; i
< NUM_MAX_VCODEC_REG_BASE
; i
++, j
++) {
263 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, j
);
264 dev
->reg_base
[i
] = devm_ioremap_resource(&pdev
->dev
, res
);
265 if (IS_ERR((__force
void *)dev
->reg_base
[i
])) {
266 ret
= PTR_ERR((__force
void *)dev
->reg_base
[i
]);
269 mtk_v4l2_debug(2, "reg[%d] base=0x%p", i
, dev
->reg_base
[i
]);
272 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
274 dev_err(&pdev
->dev
, "failed to get irq resource");
279 dev
->enc_irq
= platform_get_irq(pdev
, 0);
280 ret
= devm_request_irq(&pdev
->dev
, dev
->enc_irq
,
281 mtk_vcodec_enc_irq_handler
,
284 dev_err(&pdev
->dev
, "Failed to install dev->enc_irq %d (%d)",
291 dev
->enc_lt_irq
= platform_get_irq(pdev
, 1);
292 ret
= devm_request_irq(&pdev
->dev
,
293 dev
->enc_lt_irq
, mtk_vcodec_enc_lt_irq_handler
,
297 "Failed to install dev->enc_lt_irq %d (%d)",
298 dev
->enc_lt_irq
, ret
);
303 disable_irq(dev
->enc_irq
);
304 disable_irq(dev
->enc_lt_irq
); /* VENC_LT */
305 mutex_init(&dev
->enc_mutex
);
306 mutex_init(&dev
->dev_mutex
);
307 spin_lock_init(&dev
->irqlock
);
309 snprintf(dev
->v4l2_dev
.name
, sizeof(dev
->v4l2_dev
.name
), "%s",
312 ret
= v4l2_device_register(&pdev
->dev
, &dev
->v4l2_dev
);
314 mtk_v4l2_err("v4l2_device_register err=%d", ret
);
318 init_waitqueue_head(&dev
->queue
);
320 /* allocate video device for encoder and register it */
321 vfd_enc
= video_device_alloc();
323 mtk_v4l2_err("Failed to allocate video device");
327 vfd_enc
->fops
= &mtk_vcodec_fops
;
328 vfd_enc
->ioctl_ops
= &mtk_venc_ioctl_ops
;
329 vfd_enc
->release
= video_device_release
;
330 vfd_enc
->lock
= &dev
->dev_mutex
;
331 vfd_enc
->v4l2_dev
= &dev
->v4l2_dev
;
332 vfd_enc
->vfl_dir
= VFL_DIR_M2M
;
333 vfd_enc
->device_caps
= V4L2_CAP_VIDEO_M2M_MPLANE
|
336 snprintf(vfd_enc
->name
, sizeof(vfd_enc
->name
), "%s",
337 MTK_VCODEC_ENC_NAME
);
338 video_set_drvdata(vfd_enc
, dev
);
339 dev
->vfd_enc
= vfd_enc
;
340 platform_set_drvdata(pdev
, dev
);
342 dev
->m2m_dev_enc
= v4l2_m2m_init(&mtk_venc_m2m_ops
);
343 if (IS_ERR((__force
void *)dev
->m2m_dev_enc
)) {
344 mtk_v4l2_err("Failed to init mem2mem enc device");
345 ret
= PTR_ERR((__force
void *)dev
->m2m_dev_enc
);
346 goto err_enc_mem_init
;
349 dev
->encode_workqueue
=
350 alloc_ordered_workqueue(MTK_VCODEC_ENC_NAME
,
353 if (!dev
->encode_workqueue
) {
354 mtk_v4l2_err("Failed to create encode workqueue");
356 goto err_event_workq
;
359 ret
= video_register_device(vfd_enc
, VFL_TYPE_GRABBER
, 1);
361 mtk_v4l2_err("Failed to register video device");
365 mtk_v4l2_debug(0, "encoder registered as /dev/video%d",
371 destroy_workqueue(dev
->encode_workqueue
);
373 v4l2_m2m_release(dev
->m2m_dev_enc
);
375 video_unregister_device(vfd_enc
);
377 v4l2_device_unregister(&dev
->v4l2_dev
);
379 mtk_vcodec_release_enc_pm(dev
);
383 static const struct of_device_id mtk_vcodec_enc_match
[] = {
384 {.compatible
= "mediatek,mt8173-vcodec-enc",},
387 MODULE_DEVICE_TABLE(of
, mtk_vcodec_enc_match
);
389 static int mtk_vcodec_enc_remove(struct platform_device
*pdev
)
391 struct mtk_vcodec_dev
*dev
= platform_get_drvdata(pdev
);
393 mtk_v4l2_debug_enter();
394 flush_workqueue(dev
->encode_workqueue
);
395 destroy_workqueue(dev
->encode_workqueue
);
396 if (dev
->m2m_dev_enc
)
397 v4l2_m2m_release(dev
->m2m_dev_enc
);
400 video_unregister_device(dev
->vfd_enc
);
402 v4l2_device_unregister(&dev
->v4l2_dev
);
403 mtk_vcodec_release_enc_pm(dev
);
407 static struct platform_driver mtk_vcodec_enc_driver
= {
408 .probe
= mtk_vcodec_probe
,
409 .remove
= mtk_vcodec_enc_remove
,
411 .name
= MTK_VCODEC_ENC_NAME
,
412 .of_match_table
= mtk_vcodec_enc_match
,
416 module_platform_driver(mtk_vcodec_enc_driver
);
419 MODULE_LICENSE("GPL v2");
420 MODULE_DESCRIPTION("Mediatek video codec V4L2 encoder driver");