1
2
3
4
5
6
7
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>
13#include <linux/of.h>
14#include <media/v4l2-event.h>
15#include <media/v4l2-mem2mem.h>
16#include <media/videobuf2-dma-contig.h>
17#include <media/v4l2-device.h>
18
19#include "mtk_vcodec_drv.h"
20#include "mtk_vcodec_dec.h"
21#include "mtk_vcodec_dec_pm.h"
22#include "mtk_vcodec_intr.h"
23#include "mtk_vcodec_util.h"
24#include "mtk_vcodec_fw.h"
25
26#define VDEC_HW_ACTIVE 0x10
27#define VDEC_IRQ_CFG 0x11
28#define VDEC_IRQ_CLR 0x10
29#define VDEC_IRQ_CFG_REG 0xa4
30
31module_param(mtk_v4l2_dbg_level, int, 0644);
32module_param(mtk_vcodec_dbg, bool, 0644);
33
34
35static void wake_up_ctx(struct mtk_vcodec_ctx *ctx)
36{
37 ctx->int_cond = 1;
38 wake_up_interruptible(&ctx->queue);
39}
40
41static irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
42{
43 struct mtk_vcodec_dev *dev = priv;
44 struct mtk_vcodec_ctx *ctx;
45 u32 cg_status = 0;
46 unsigned int dec_done_status = 0;
47 void __iomem *vdec_misc_addr = dev->reg_base[VDEC_MISC] +
48 VDEC_IRQ_CFG_REG;
49
50 ctx = mtk_vcodec_get_curr_ctx(dev);
51
52
53 cg_status = readl(dev->reg_base[0]);
54 if ((cg_status & VDEC_HW_ACTIVE) != 0) {
55 mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
56 cg_status);
57 return IRQ_HANDLED;
58 }
59
60 dec_done_status = readl(vdec_misc_addr);
61 ctx->irq_status = dec_done_status;
62 if ((dec_done_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) !=
63 MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
64 return IRQ_HANDLED;
65
66
67 writel((readl(vdec_misc_addr) | VDEC_IRQ_CFG),
68 dev->reg_base[VDEC_MISC] + VDEC_IRQ_CFG_REG);
69 writel((readl(vdec_misc_addr) & ~VDEC_IRQ_CLR),
70 dev->reg_base[VDEC_MISC] + VDEC_IRQ_CFG_REG);
71
72 wake_up_ctx(ctx);
73
74 mtk_v4l2_debug(3,
75 "mtk_vcodec_dec_irq_handler :wake up ctx %d, dec_done_status=%x",
76 ctx->id, dec_done_status);
77
78 return IRQ_HANDLED;
79}
80
81static int fops_vcodec_open(struct file *file)
82{
83 struct mtk_vcodec_dev *dev = video_drvdata(file);
84 struct mtk_vcodec_ctx *ctx = NULL;
85 int ret = 0;
86 struct vb2_queue *src_vq;
87
88 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
89 if (!ctx)
90 return -ENOMEM;
91
92 mutex_lock(&dev->dev_mutex);
93 ctx->id = dev->id_counter++;
94 v4l2_fh_init(&ctx->fh, video_devdata(file));
95 file->private_data = &ctx->fh;
96 v4l2_fh_add(&ctx->fh);
97 INIT_LIST_HEAD(&ctx->list);
98 ctx->dev = dev;
99 init_waitqueue_head(&ctx->queue);
100 mutex_init(&ctx->lock);
101
102 ctx->type = MTK_INST_DECODER;
103 ret = dev->vdec_pdata->ctrls_setup(ctx);
104 if (ret) {
105 mtk_v4l2_err("Failed to setup mt vcodec controls");
106 goto err_ctrls_setup;
107 }
108 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_dec, ctx,
109 &mtk_vcodec_dec_queue_init);
110 if (IS_ERR((__force void *)ctx->m2m_ctx)) {
111 ret = PTR_ERR((__force void *)ctx->m2m_ctx);
112 mtk_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)",
113 ret);
114 goto err_m2m_ctx_init;
115 }
116 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
117 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
118 ctx->empty_flush_buf.vb.vb2_buf.vb2_queue = src_vq;
119 mtk_vcodec_dec_set_default_params(ctx);
120
121 if (v4l2_fh_is_singular(&ctx->fh)) {
122 ret = mtk_vcodec_dec_pw_on(&dev->pm);
123 if (ret < 0)
124 goto err_load_fw;
125
126
127
128 ret = mtk_vcodec_fw_load_firmware(dev->fw_handler);
129 if (ret < 0) {
130
131
132
133
134 mtk_v4l2_err("failed to load firmware!");
135 goto err_load_fw;
136 }
137
138 dev->dec_capability =
139 mtk_vcodec_fw_get_vdec_capa(dev->fw_handler);
140 mtk_v4l2_debug(0, "decoder capability %x", dev->dec_capability);
141 }
142
143 list_add(&ctx->list, &dev->ctx_list);
144
145 mutex_unlock(&dev->dev_mutex);
146 mtk_v4l2_debug(0, "%s decoder [%d]", dev_name(&dev->plat_dev->dev),
147 ctx->id);
148 return ret;
149
150
151err_load_fw:
152 v4l2_m2m_ctx_release(ctx->m2m_ctx);
153err_m2m_ctx_init:
154 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
155err_ctrls_setup:
156 v4l2_fh_del(&ctx->fh);
157 v4l2_fh_exit(&ctx->fh);
158 kfree(ctx);
159 mutex_unlock(&dev->dev_mutex);
160
161 return ret;
162}
163
164static int fops_vcodec_release(struct file *file)
165{
166 struct mtk_vcodec_dev *dev = video_drvdata(file);
167 struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
168
169 mtk_v4l2_debug(0, "[%d] decoder", ctx->id);
170 mutex_lock(&dev->dev_mutex);
171
172
173
174
175
176
177
178 v4l2_m2m_ctx_release(ctx->m2m_ctx);
179 mtk_vcodec_dec_release(ctx);
180
181 if (v4l2_fh_is_singular(&ctx->fh))
182 mtk_vcodec_dec_pw_off(&dev->pm);
183 v4l2_fh_del(&ctx->fh);
184 v4l2_fh_exit(&ctx->fh);
185 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
186
187 list_del_init(&ctx->list);
188 kfree(ctx);
189 mutex_unlock(&dev->dev_mutex);
190 return 0;
191}
192
193static const struct v4l2_file_operations mtk_vcodec_fops = {
194 .owner = THIS_MODULE,
195 .open = fops_vcodec_open,
196 .release = fops_vcodec_release,
197 .poll = v4l2_m2m_fop_poll,
198 .unlocked_ioctl = video_ioctl2,
199 .mmap = v4l2_m2m_fop_mmap,
200};
201
202static int mtk_vcodec_probe(struct platform_device *pdev)
203{
204 struct mtk_vcodec_dev *dev;
205 struct video_device *vfd_dec;
206 struct resource *res;
207 phandle rproc_phandle;
208 enum mtk_vcodec_fw_type fw_type;
209 int i, ret;
210
211 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
212 if (!dev)
213 return -ENOMEM;
214
215 INIT_LIST_HEAD(&dev->ctx_list);
216 dev->plat_dev = pdev;
217
218 dev->vdec_pdata = of_device_get_match_data(&pdev->dev);
219 if (!of_property_read_u32(pdev->dev.of_node, "mediatek,vpu",
220 &rproc_phandle)) {
221 fw_type = VPU;
222 } else if (!of_property_read_u32(pdev->dev.of_node, "mediatek,scp",
223 &rproc_phandle)) {
224 fw_type = SCP;
225 } else {
226 mtk_v4l2_err("Could not get vdec IPI device");
227 return -ENODEV;
228 }
229 dma_set_max_seg_size(&pdev->dev, UINT_MAX);
230
231 dev->fw_handler = mtk_vcodec_fw_select(dev, fw_type, DECODER);
232 if (IS_ERR(dev->fw_handler))
233 return PTR_ERR(dev->fw_handler);
234
235 ret = mtk_vcodec_init_dec_pm(dev);
236 if (ret < 0) {
237 dev_err(&pdev->dev, "Failed to get mt vcodec clock source");
238 goto err_dec_pm;
239 }
240
241 for (i = 0; i < NUM_MAX_VDEC_REG_BASE; i++) {
242 dev->reg_base[i] = devm_platform_ioremap_resource(pdev, i);
243 if (IS_ERR((__force void *)dev->reg_base[i])) {
244 ret = PTR_ERR((__force void *)dev->reg_base[i]);
245 goto err_res;
246 }
247 mtk_v4l2_debug(2, "reg[%d] base=%p", i, dev->reg_base[i]);
248 }
249
250 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
251 if (res == NULL) {
252 dev_err(&pdev->dev, "failed to get irq resource");
253 ret = -ENOENT;
254 goto err_res;
255 }
256
257 dev->dec_irq = platform_get_irq(pdev, 0);
258 irq_set_status_flags(dev->dec_irq, IRQ_NOAUTOEN);
259 ret = devm_request_irq(&pdev->dev, dev->dec_irq,
260 mtk_vcodec_dec_irq_handler, 0, pdev->name, dev);
261 if (ret) {
262 dev_err(&pdev->dev, "Failed to install dev->dec_irq %d (%d)",
263 dev->dec_irq,
264 ret);
265 goto err_res;
266 }
267
268 mutex_init(&dev->dec_mutex);
269 mutex_init(&dev->dev_mutex);
270 spin_lock_init(&dev->irqlock);
271
272 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
273 "[/MTK_V4L2_VDEC]");
274
275 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
276 if (ret) {
277 mtk_v4l2_err("v4l2_device_register err=%d", ret);
278 goto err_res;
279 }
280
281 init_waitqueue_head(&dev->queue);
282
283 vfd_dec = video_device_alloc();
284 if (!vfd_dec) {
285 mtk_v4l2_err("Failed to allocate video device");
286 ret = -ENOMEM;
287 goto err_dec_alloc;
288 }
289 vfd_dec->fops = &mtk_vcodec_fops;
290 vfd_dec->ioctl_ops = &mtk_vdec_ioctl_ops;
291 vfd_dec->release = video_device_release;
292 vfd_dec->lock = &dev->dev_mutex;
293 vfd_dec->v4l2_dev = &dev->v4l2_dev;
294 vfd_dec->vfl_dir = VFL_DIR_M2M;
295 vfd_dec->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE |
296 V4L2_CAP_STREAMING;
297
298 snprintf(vfd_dec->name, sizeof(vfd_dec->name), "%s",
299 MTK_VCODEC_DEC_NAME);
300 video_set_drvdata(vfd_dec, dev);
301 dev->vfd_dec = vfd_dec;
302 platform_set_drvdata(pdev, dev);
303
304 dev->m2m_dev_dec = v4l2_m2m_init(&mtk_vdec_m2m_ops);
305 if (IS_ERR((__force void *)dev->m2m_dev_dec)) {
306 mtk_v4l2_err("Failed to init mem2mem dec device");
307 ret = PTR_ERR((__force void *)dev->m2m_dev_dec);
308 goto err_dec_mem_init;
309 }
310
311 dev->decode_workqueue =
312 alloc_ordered_workqueue(MTK_VCODEC_DEC_NAME,
313 WQ_MEM_RECLAIM | WQ_FREEZABLE);
314 if (!dev->decode_workqueue) {
315 mtk_v4l2_err("Failed to create decode workqueue");
316 ret = -EINVAL;
317 goto err_event_workq;
318 }
319
320 if (dev->vdec_pdata->uses_stateless_api) {
321 dev->mdev_dec.dev = &pdev->dev;
322 strscpy(dev->mdev_dec.model, MTK_VCODEC_DEC_NAME,
323 sizeof(dev->mdev_dec.model));
324
325 media_device_init(&dev->mdev_dec);
326 dev->mdev_dec.ops = &mtk_vcodec_media_ops;
327 dev->v4l2_dev.mdev = &dev->mdev_dec;
328
329 ret = v4l2_m2m_register_media_controller(dev->m2m_dev_dec, dev->vfd_dec,
330 MEDIA_ENT_F_PROC_VIDEO_DECODER);
331 if (ret) {
332 mtk_v4l2_err("Failed to register media controller");
333 goto err_reg_cont;
334 }
335
336 ret = media_device_register(&dev->mdev_dec);
337 if (ret) {
338 mtk_v4l2_err("Failed to register media device");
339 goto err_media_reg;
340 }
341
342 mtk_v4l2_debug(0, "media registered as /dev/media%d", vfd_dec->minor);
343 }
344 ret = video_register_device(vfd_dec, VFL_TYPE_VIDEO, 0);
345 if (ret) {
346 mtk_v4l2_err("Failed to register video device");
347 goto err_dec_reg;
348 }
349
350 mtk_v4l2_debug(0, "decoder registered as /dev/video%d", vfd_dec->minor);
351
352 return 0;
353
354err_dec_reg:
355 if (dev->vdec_pdata->uses_stateless_api)
356 media_device_unregister(&dev->mdev_dec);
357err_media_reg:
358 if (dev->vdec_pdata->uses_stateless_api)
359 v4l2_m2m_unregister_media_controller(dev->m2m_dev_dec);
360err_reg_cont:
361 destroy_workqueue(dev->decode_workqueue);
362err_event_workq:
363 v4l2_m2m_release(dev->m2m_dev_dec);
364err_dec_mem_init:
365 video_unregister_device(vfd_dec);
366err_dec_alloc:
367 v4l2_device_unregister(&dev->v4l2_dev);
368err_res:
369 mtk_vcodec_release_dec_pm(dev);
370err_dec_pm:
371 mtk_vcodec_fw_release(dev->fw_handler);
372 return ret;
373}
374
375static const struct of_device_id mtk_vcodec_match[] = {
376 {
377 .compatible = "mediatek,mt8173-vcodec-dec",
378 .data = &mtk_vdec_8173_pdata,
379 },
380 {
381 .compatible = "mediatek,mt8183-vcodec-dec",
382 .data = &mtk_vdec_8183_pdata,
383 },
384 {},
385};
386
387MODULE_DEVICE_TABLE(of, mtk_vcodec_match);
388
389static int mtk_vcodec_dec_remove(struct platform_device *pdev)
390{
391 struct mtk_vcodec_dev *dev = platform_get_drvdata(pdev);
392
393 flush_workqueue(dev->decode_workqueue);
394 destroy_workqueue(dev->decode_workqueue);
395
396 if (media_devnode_is_registered(dev->mdev_dec.devnode)) {
397 media_device_unregister(&dev->mdev_dec);
398 v4l2_m2m_unregister_media_controller(dev->m2m_dev_dec);
399 media_device_cleanup(&dev->mdev_dec);
400 }
401
402 if (dev->m2m_dev_dec)
403 v4l2_m2m_release(dev->m2m_dev_dec);
404
405 if (dev->vfd_dec)
406 video_unregister_device(dev->vfd_dec);
407
408 v4l2_device_unregister(&dev->v4l2_dev);
409 mtk_vcodec_release_dec_pm(dev);
410 mtk_vcodec_fw_release(dev->fw_handler);
411 return 0;
412}
413
414static struct platform_driver mtk_vcodec_dec_driver = {
415 .probe = mtk_vcodec_probe,
416 .remove = mtk_vcodec_dec_remove,
417 .driver = {
418 .name = MTK_VCODEC_DEC_NAME,
419 .of_match_table = mtk_vcodec_match,
420 },
421};
422
423module_platform_driver(mtk_vcodec_dec_driver);
424
425MODULE_LICENSE("GPL v2");
426MODULE_DESCRIPTION("Mediatek video codec V4L2 decoder driver");
427