2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: PC Chen <pc.chen@mediatek.com>
4 * Tiffany Lin <tiffany.lin@mediatek.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
22 #include <media/v4l2-event.h>
23 #include <media/v4l2-mem2mem.h>
24 #include <media/videobuf2-dma-contig.h>
26 #include "mtk_vcodec_drv.h"
27 #include "mtk_vcodec_dec.h"
28 #include "mtk_vcodec_dec_pm.h"
29 #include "mtk_vcodec_intr.h"
30 #include "mtk_vcodec_util.h"
33 #define VDEC_HW_ACTIVE 0x10
34 #define VDEC_IRQ_CFG 0x11
35 #define VDEC_IRQ_CLR 0x10
36 #define VDEC_IRQ_CFG_REG 0xa4
38 module_param(mtk_v4l2_dbg_level
, int, 0644);
39 module_param(mtk_vcodec_dbg
, bool, 0644);
41 /* Wake up context wait_queue */
42 static void wake_up_ctx(struct mtk_vcodec_ctx
*ctx
)
45 wake_up_interruptible(&ctx
->queue
);
48 static irqreturn_t
mtk_vcodec_dec_irq_handler(int irq
, void *priv
)
50 struct mtk_vcodec_dev
*dev
= priv
;
51 struct mtk_vcodec_ctx
*ctx
;
53 unsigned int dec_done_status
= 0;
54 void __iomem
*vdec_misc_addr
= dev
->reg_base
[VDEC_MISC
] +
57 ctx
= mtk_vcodec_get_curr_ctx(dev
);
59 /* check if HW active or not */
60 cg_status
= readl(dev
->reg_base
[0]);
61 if ((cg_status
& VDEC_HW_ACTIVE
) != 0) {
62 mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
67 dec_done_status
= readl(vdec_misc_addr
);
68 ctx
->irq_status
= dec_done_status
;
69 if ((dec_done_status
& MTK_VDEC_IRQ_STATUS_DEC_SUCCESS
) !=
70 MTK_VDEC_IRQ_STATUS_DEC_SUCCESS
)
74 writel((readl(vdec_misc_addr
) | VDEC_IRQ_CFG
),
75 dev
->reg_base
[VDEC_MISC
] + VDEC_IRQ_CFG_REG
);
76 writel((readl(vdec_misc_addr
) & ~VDEC_IRQ_CLR
),
77 dev
->reg_base
[VDEC_MISC
] + VDEC_IRQ_CFG_REG
);
82 "mtk_vcodec_dec_irq_handler :wake up ctx %d, dec_done_status=%x",
83 ctx
->id
, dec_done_status
);
88 static void mtk_vcodec_dec_reset_handler(void *priv
)
90 struct mtk_vcodec_dev
*dev
= priv
;
91 struct mtk_vcodec_ctx
*ctx
;
93 mtk_v4l2_err("Watchdog timeout!!");
95 mutex_lock(&dev
->dev_mutex
);
96 list_for_each_entry(ctx
, &dev
->ctx_list
, list
) {
97 ctx
->state
= MTK_STATE_ABORT
;
98 mtk_v4l2_debug(0, "[%d] Change to state MTK_STATE_ERROR",
101 mutex_unlock(&dev
->dev_mutex
);
104 static int fops_vcodec_open(struct file
*file
)
106 struct mtk_vcodec_dev
*dev
= video_drvdata(file
);
107 struct mtk_vcodec_ctx
*ctx
= NULL
;
108 struct mtk_video_dec_buf
*mtk_buf
= NULL
;
110 struct vb2_queue
*src_vq
;
112 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
115 mtk_buf
= kzalloc(sizeof(*mtk_buf
), GFP_KERNEL
);
121 mutex_lock(&dev
->dev_mutex
);
122 ctx
->empty_flush_buf
= mtk_buf
;
123 ctx
->id
= dev
->id_counter
++;
124 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
125 file
->private_data
= &ctx
->fh
;
126 v4l2_fh_add(&ctx
->fh
);
127 INIT_LIST_HEAD(&ctx
->list
);
129 init_waitqueue_head(&ctx
->queue
);
130 mutex_init(&ctx
->lock
);
132 ctx
->type
= MTK_INST_DECODER
;
133 ret
= mtk_vcodec_dec_ctrls_setup(ctx
);
135 mtk_v4l2_err("Failed to setup mt vcodec controls");
136 goto err_ctrls_setup
;
138 ctx
->m2m_ctx
= v4l2_m2m_ctx_init(dev
->m2m_dev_dec
, ctx
,
139 &mtk_vcodec_dec_queue_init
);
140 if (IS_ERR((__force
void *)ctx
->m2m_ctx
)) {
141 ret
= PTR_ERR((__force
void *)ctx
->m2m_ctx
);
142 mtk_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)",
144 goto err_m2m_ctx_init
;
146 src_vq
= v4l2_m2m_get_vq(ctx
->m2m_ctx
,
147 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
);
148 ctx
->empty_flush_buf
->vb
.vb2_buf
.vb2_queue
= src_vq
;
149 ctx
->empty_flush_buf
->lastframe
= true;
150 mtk_vcodec_dec_set_default_params(ctx
);
152 if (v4l2_fh_is_singular(&ctx
->fh
)) {
153 mtk_vcodec_dec_pw_on(&dev
->pm
);
155 * vpu_load_firmware checks if it was loaded already and
156 * does nothing in that case
158 ret
= vpu_load_firmware(dev
->vpu_plat_dev
);
161 * Return 0 if downloading firmware successfully,
162 * otherwise it is failed
164 mtk_v4l2_err("vpu_load_firmware failed!");
168 dev
->dec_capability
=
169 vpu_get_vdec_hw_capa(dev
->vpu_plat_dev
);
170 mtk_v4l2_debug(0, "decoder capability %x", dev
->dec_capability
);
173 list_add(&ctx
->list
, &dev
->ctx_list
);
175 mutex_unlock(&dev
->dev_mutex
);
176 mtk_v4l2_debug(0, "%s decoder [%d]", dev_name(&dev
->plat_dev
->dev
),
180 /* Deinit when failure occurred */
182 v4l2_m2m_ctx_release(ctx
->m2m_ctx
);
184 v4l2_ctrl_handler_free(&ctx
->ctrl_hdl
);
186 v4l2_fh_del(&ctx
->fh
);
187 v4l2_fh_exit(&ctx
->fh
);
188 kfree(ctx
->empty_flush_buf
);
190 mutex_unlock(&dev
->dev_mutex
);
195 static int fops_vcodec_release(struct file
*file
)
197 struct mtk_vcodec_dev
*dev
= video_drvdata(file
);
198 struct mtk_vcodec_ctx
*ctx
= fh_to_ctx(file
->private_data
);
200 mtk_v4l2_debug(0, "[%d] decoder", ctx
->id
);
201 mutex_lock(&dev
->dev_mutex
);
204 * Call v4l2_m2m_ctx_release before mtk_vcodec_dec_release. First, it
205 * makes sure the worker thread is not running after vdec_if_deinit.
206 * Second, the decoder will be flushed and all the buffers will be
207 * returned in stop_streaming.
209 v4l2_m2m_ctx_release(ctx
->m2m_ctx
);
210 mtk_vcodec_dec_release(ctx
);
212 if (v4l2_fh_is_singular(&ctx
->fh
))
213 mtk_vcodec_dec_pw_off(&dev
->pm
);
214 v4l2_fh_del(&ctx
->fh
);
215 v4l2_fh_exit(&ctx
->fh
);
216 v4l2_ctrl_handler_free(&ctx
->ctrl_hdl
);
218 list_del_init(&ctx
->list
);
219 kfree(ctx
->empty_flush_buf
);
221 mutex_unlock(&dev
->dev_mutex
);
225 static const struct v4l2_file_operations mtk_vcodec_fops
= {
226 .owner
= THIS_MODULE
,
227 .open
= fops_vcodec_open
,
228 .release
= fops_vcodec_release
,
229 .poll
= v4l2_m2m_fop_poll
,
230 .unlocked_ioctl
= video_ioctl2
,
231 .mmap
= v4l2_m2m_fop_mmap
,
234 static int mtk_vcodec_probe(struct platform_device
*pdev
)
236 struct mtk_vcodec_dev
*dev
;
237 struct video_device
*vfd_dec
;
238 struct resource
*res
;
241 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
245 INIT_LIST_HEAD(&dev
->ctx_list
);
246 dev
->plat_dev
= pdev
;
248 dev
->vpu_plat_dev
= vpu_get_plat_device(dev
->plat_dev
);
249 if (dev
->vpu_plat_dev
== NULL
) {
250 mtk_v4l2_err("[VPU] vpu device in not ready");
251 return -EPROBE_DEFER
;
254 vpu_wdt_reg_handler(dev
->vpu_plat_dev
, mtk_vcodec_dec_reset_handler
,
257 ret
= mtk_vcodec_init_dec_pm(dev
);
259 dev_err(&pdev
->dev
, "Failed to get mt vcodec clock source");
263 for (i
= 0; i
< NUM_MAX_VDEC_REG_BASE
; i
++) {
264 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
266 dev_err(&pdev
->dev
, "get memory resource failed.");
270 dev
->reg_base
[i
] = devm_ioremap_resource(&pdev
->dev
, res
);
271 if (IS_ERR((__force
void *)dev
->reg_base
[i
])) {
272 ret
= PTR_ERR((__force
void *)dev
->reg_base
[i
]);
275 mtk_v4l2_debug(2, "reg[%d] base=%p", i
, dev
->reg_base
[i
]);
278 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
280 dev_err(&pdev
->dev
, "failed to get irq resource");
285 dev
->dec_irq
= platform_get_irq(pdev
, 0);
286 ret
= devm_request_irq(&pdev
->dev
, dev
->dec_irq
,
287 mtk_vcodec_dec_irq_handler
, 0, pdev
->name
, dev
);
289 dev_err(&pdev
->dev
, "Failed to install dev->dec_irq %d (%d)",
295 disable_irq(dev
->dec_irq
);
296 mutex_init(&dev
->dec_mutex
);
297 mutex_init(&dev
->dev_mutex
);
298 spin_lock_init(&dev
->irqlock
);
300 snprintf(dev
->v4l2_dev
.name
, sizeof(dev
->v4l2_dev
.name
), "%s",
303 ret
= v4l2_device_register(&pdev
->dev
, &dev
->v4l2_dev
);
305 mtk_v4l2_err("v4l2_device_register err=%d", ret
);
309 init_waitqueue_head(&dev
->queue
);
311 vfd_dec
= video_device_alloc();
313 mtk_v4l2_err("Failed to allocate video device");
317 vfd_dec
->fops
= &mtk_vcodec_fops
;
318 vfd_dec
->ioctl_ops
= &mtk_vdec_ioctl_ops
;
319 vfd_dec
->release
= video_device_release
;
320 vfd_dec
->lock
= &dev
->dev_mutex
;
321 vfd_dec
->v4l2_dev
= &dev
->v4l2_dev
;
322 vfd_dec
->vfl_dir
= VFL_DIR_M2M
;
323 vfd_dec
->device_caps
= V4L2_CAP_VIDEO_M2M_MPLANE
|
326 snprintf(vfd_dec
->name
, sizeof(vfd_dec
->name
), "%s",
327 MTK_VCODEC_DEC_NAME
);
328 video_set_drvdata(vfd_dec
, dev
);
329 dev
->vfd_dec
= vfd_dec
;
330 platform_set_drvdata(pdev
, dev
);
332 dev
->m2m_dev_dec
= v4l2_m2m_init(&mtk_vdec_m2m_ops
);
333 if (IS_ERR((__force
void *)dev
->m2m_dev_dec
)) {
334 mtk_v4l2_err("Failed to init mem2mem dec device");
335 ret
= PTR_ERR((__force
void *)dev
->m2m_dev_dec
);
336 goto err_dec_mem_init
;
339 dev
->decode_workqueue
=
340 alloc_ordered_workqueue(MTK_VCODEC_DEC_NAME
,
341 WQ_MEM_RECLAIM
| WQ_FREEZABLE
);
342 if (!dev
->decode_workqueue
) {
343 mtk_v4l2_err("Failed to create decode workqueue");
345 goto err_event_workq
;
348 ret
= video_register_device(vfd_dec
, VFL_TYPE_GRABBER
, 0);
350 mtk_v4l2_err("Failed to register video device");
354 mtk_v4l2_debug(0, "decoder registered as /dev/video%d",
360 destroy_workqueue(dev
->decode_workqueue
);
362 v4l2_m2m_release(dev
->m2m_dev_dec
);
364 video_unregister_device(vfd_dec
);
366 v4l2_device_unregister(&dev
->v4l2_dev
);
368 mtk_vcodec_release_dec_pm(dev
);
372 static const struct of_device_id mtk_vcodec_match
[] = {
373 {.compatible
= "mediatek,mt8173-vcodec-dec",},
377 MODULE_DEVICE_TABLE(of
, mtk_vcodec_match
);
379 static int mtk_vcodec_dec_remove(struct platform_device
*pdev
)
381 struct mtk_vcodec_dev
*dev
= platform_get_drvdata(pdev
);
383 flush_workqueue(dev
->decode_workqueue
);
384 destroy_workqueue(dev
->decode_workqueue
);
385 if (dev
->m2m_dev_dec
)
386 v4l2_m2m_release(dev
->m2m_dev_dec
);
389 video_unregister_device(dev
->vfd_dec
);
391 v4l2_device_unregister(&dev
->v4l2_dev
);
392 mtk_vcodec_release_dec_pm(dev
);
396 static struct platform_driver mtk_vcodec_dec_driver
= {
397 .probe
= mtk_vcodec_probe
,
398 .remove
= mtk_vcodec_dec_remove
,
400 .name
= MTK_VCODEC_DEC_NAME
,
401 .of_match_table
= mtk_vcodec_match
,
405 module_platform_driver(mtk_vcodec_dec_driver
);
407 MODULE_LICENSE("GPL v2");
408 MODULE_DESCRIPTION("Mediatek video codec V4L2 decoder driver");