summaryrefslogtreecommitdiff
path: root/drivers/amvdec_ports/aml_vcodec_dec_drv.c (plain)
blob: 7758ca854be9571915d28250502f68340d21e8f3
1/*
2* Copyright (C) 2017 Amlogic, Inc. All rights reserved.
3*
4* This program is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation; either version 2 of the License, or
7* (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful, but WITHOUT
10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12* more details.
13*
14* You should have received a copy of the GNU General Public License along
15* with this program; if not, write to the Free Software Foundation, Inc.,
16* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*
18* Description:
19*/
20
21#define DEBUG
22#include <linux/slab.h>
23#include <linux/interrupt.h>
24#include <linux/irq.h>
25#include <linux/module.h>
26#include <linux/of_device.h>
27#include <linux/of.h>
28#include <media/v4l2-event.h>
29#include <media/v4l2-mem2mem.h>
30#include <media/videobuf2-dma-contig.h>
31#include <linux/kthread.h>
32
33#include "aml_vcodec_drv.h"
34#include "aml_vcodec_dec.h"
35#include "aml_vcodec_util.h"
36#include "aml_vcodec_vfm.h"
37#include <linux/file.h>
38#include <linux/anon_inodes.h>
39
40#define VDEC_HW_ACTIVE 0x10
41#define VDEC_IRQ_CFG 0x11
42#define VDEC_IRQ_CLR 0x10
43#define VDEC_IRQ_CFG_REG 0xa4
44
45#define V4LVIDEO_IOC_MAGIC 'I'
46#define V4LVIDEO_IOCTL_ALLOC_FD _IOW(V4LVIDEO_IOC_MAGIC, 0x02, int)
47#define V4LVIDEO_IOCTL_CHECK_FD _IOW(V4LVIDEO_IOC_MAGIC, 0x03, int)
48#define V4LVIDEO_IOCTL_SET_CONFIG_PARAMS _IOWR(V4LVIDEO_IOC_MAGIC, 0x04, struct v4l2_config_parm)
49#define V4LVIDEO_IOCTL_GET_CONFIG_PARAMS _IOWR(V4LVIDEO_IOC_MAGIC, 0x05, struct v4l2_config_parm)
50
51bool param_sets_from_ucode = 1;
52bool enable_drm_mode;
53
54static int fops_vcodec_open(struct file *file)
55{
56 struct aml_vcodec_dev *dev = video_drvdata(file);
57 struct aml_vcodec_ctx *ctx = NULL;
58 struct aml_video_dec_buf *aml_buf = NULL;
59 int ret = 0;
60 struct vb2_queue *src_vq;
61
62 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
63 if (!ctx)
64 return -ENOMEM;
65 aml_buf = kzalloc(sizeof(*aml_buf), GFP_KERNEL);
66 if (!aml_buf) {
67 kfree(ctx);
68 return -ENOMEM;
69 }
70
71 mutex_lock(&dev->dev_mutex);
72 ctx->empty_flush_buf = aml_buf;
73 ctx->id = dev->id_counter++;
74 v4l2_fh_init(&ctx->fh, video_devdata(file));
75 file->private_data = &ctx->fh;
76 v4l2_fh_add(&ctx->fh);
77 INIT_LIST_HEAD(&ctx->list);
78 INIT_LIST_HEAD(&ctx->vdec_thread_list);
79 dev->filp = file;
80 ctx->dev = dev;
81 init_waitqueue_head(&ctx->queue);
82 mutex_init(&ctx->state_lock);
83 mutex_init(&ctx->lock);
84 spin_lock_init(&ctx->slock);
85 init_completion(&ctx->comp);
86
87 ctx->param_sets_from_ucode = param_sets_from_ucode ? 1 : 0;
88
89 if (enable_drm_mode) {
90 ctx->is_drm_mode = true;
91 ctx->param_sets_from_ucode = true;
92 }
93
94 ctx->type = AML_INST_DECODER;
95 ret = aml_vcodec_dec_ctrls_setup(ctx);
96 if (ret) {
97 v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
98 "Failed to setup vcodec controls\n");
99 goto err_ctrls_setup;
100 }
101 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_dec, ctx,
102 &aml_vcodec_dec_queue_init);
103 if (IS_ERR((__force void *)ctx->m2m_ctx)) {
104 ret = PTR_ERR((__force void *)ctx->m2m_ctx);
105 v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
106 "Failed to v4l2_m2m_ctx_init() (%d)\n", ret);
107 goto err_m2m_ctx_init;
108 }
109 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
110 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
111 ctx->output_thread_ready = true;
112 ctx->empty_flush_buf->vb.vb2_buf.vb2_queue = src_vq;
113 ctx->empty_flush_buf->lastframe = true;
114 aml_vcodec_dec_set_default_params(ctx);
115
116 ret = aml_thread_start(ctx, try_to_capture, AML_THREAD_CAPTURE, "cap");
117 if (ret) {
118 v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
119 "Failed to creat capture thread.\n");
120 goto err_creat_thread;
121 }
122
123 list_add(&ctx->list, &dev->ctx_list);
124
125 mutex_unlock(&dev->dev_mutex);
126 v4l_dbg(ctx, V4L_DEBUG_CODEC_PRINFO, "%s decoder %lx\n",
127 dev_name(&dev->plat_dev->dev), (ulong)ctx);
128
129 return ret;
130
131 /* Deinit when failure occurred */
132err_creat_thread:
133 v4l2_m2m_ctx_release(ctx->m2m_ctx);
134err_m2m_ctx_init:
135 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
136err_ctrls_setup:
137 v4l2_fh_del(&ctx->fh);
138 v4l2_fh_exit(&ctx->fh);
139 kfree(ctx->empty_flush_buf);
140 kfree(ctx);
141 mutex_unlock(&dev->dev_mutex);
142
143 return ret;
144}
145
146static int fops_vcodec_release(struct file *file)
147{
148 struct aml_vcodec_dev *dev = video_drvdata(file);
149 struct aml_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
150
151 v4l_dbg(ctx, V4L_DEBUG_CODEC_PRINFO, "release decoder %lx\n", (ulong) ctx);
152 mutex_lock(&dev->dev_mutex);
153
154 /*
155 * Call v4l2_m2m_ctx_release before aml_vcodec_dec_release. First, it
156 * makes sure the worker thread is not running after vdec_if_deinit.
157 * Second, the decoder will be flushed and all the buffers will be
158 * returned in stop_streaming.
159 */
160 aml_thread_stop(ctx);
161 wait_vcodec_ending(ctx);
162 v4l2_m2m_ctx_release(ctx->m2m_ctx);
163 aml_vcodec_dec_release(ctx);
164
165 v4l2_fh_del(&ctx->fh);
166 v4l2_fh_exit(&ctx->fh);
167 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
168
169 list_del_init(&ctx->list);
170 kfree(ctx->empty_flush_buf);
171 kfree(ctx);
172 mutex_unlock(&dev->dev_mutex);
173 return 0;
174}
175
176static int v4l2video_file_release(struct inode *inode, struct file *file)
177{
178 v4l_dbg(0, V4L_DEBUG_CODEC_BUFMGR, "file: %lx, data: %lx\n",
179 (ulong) file, (ulong) file->private_data);
180
181 if (file->private_data)
182 vdec_frame_buffer_release(file->private_data);
183
184 return 0;
185}
186
187const struct file_operations v4l2_file_fops = {
188 .release = v4l2video_file_release,
189};
190
191int v4l2_alloc_fd(int *fd)
192{
193 struct file *file = NULL;
194 int file_fd = get_unused_fd_flags(O_CLOEXEC);
195
196 if (file_fd < 0) {
197 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
198 "get unused fd fail\n");
199 return -ENODEV;
200 }
201
202 file = anon_inode_getfile("v4l2_meta_file", &v4l2_file_fops, NULL, 0);
203 if (IS_ERR(file)) {
204 put_unused_fd(file_fd);
205 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
206 "anon_inode_getfile fail\n");
207 return -ENODEV;
208 }
209
210 file->private_data =
211 kzalloc(sizeof(struct file_private_data), GFP_KERNEL);
212 if (!file->private_data) {
213 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
214 "alloc priv data faild.\n");
215 return -ENOMEM;
216 }
217
218 v4l_dbg(0, V4L_DEBUG_CODEC_BUFMGR, "fd %d, file %lx, data: %lx\n",
219 file_fd, (ulong) file, (ulong) file->private_data);
220
221 fd_install(file_fd, file);
222 *fd = file_fd;
223
224 return 0;
225}
226
227extern const struct file_operations v4l2_file_fops;
228bool is_v4l2_buf_file(struct file *file)
229{
230 return file->f_op == &v4l2_file_fops;
231}
232
233int v4l2_check_fd(int fd)
234{
235 struct file *file;
236
237 file = fget(fd);
238
239 if (!file) {
240 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
241 "fget fd %d fail!\n", fd);
242 return -EBADF;
243 }
244
245 if (!is_v4l2_buf_file(file)) {
246 fput(file);
247 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
248 "is_v4l2_buf_file fail!\n");
249 return -1;
250 }
251
252 fput(file);
253
254 v4l_dbg(0, V4L_DEBUG_CODEC_EXINFO,
255 "ioctl ok, comm %s, pid %d\n",
256 current->comm, current->pid);
257
258 return 0;
259}
260
261int dmabuf_fd_install_data(int fd, void* data, u32 size)
262{
263 struct file *file;
264
265 file = fget(fd);
266
267 if (!file) {
268 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
269 "fget fd %d fail!, comm %s, pid %d\n",
270 fd, current->comm, current->pid);
271 return -EBADF;
272 }
273
274 if (!is_v4l2_buf_file(file)) {
275 fput(file);
276 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
277 "the buf file checked fail!\n");
278 return -EBADF;
279 }
280
281 memcpy(file->private_data, data, size);
282
283 fput(file);
284
285 return 0;
286}
287
288void* v4l_get_vf_handle(int fd)
289{
290 struct file *file;
291 struct file_private_data *data = NULL;
292 void *vf_handle = 0;
293
294 file = fget(fd);
295
296 if (!file) {
297 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
298 "fget fd %d fail!, comm %s, pid %d\n",
299 fd, current->comm, current->pid);
300 return NULL;
301 }
302
303 if (!is_v4l2_buf_file(file)) {
304 fput(file);
305 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
306 "the buf file checked fail!\n");
307 return NULL;
308 }
309
310 data = (struct file_private_data*) file->private_data;
311 if (data) {
312 vf_handle = &data->vf;
313 v4l_dbg(0, V4L_DEBUG_CODEC_BUFMGR, "file: %lx, data: %lx\n",
314 (ulong) file, (ulong) data);
315 }
316
317 fput(file);
318
319 return vf_handle;
320}
321
322
323static long v4l2_vcodec_ioctl(struct file *file,
324 unsigned int cmd,
325 ulong arg)
326{
327 long ret = 0;
328 void __user *argp = (void __user *)arg;
329
330 switch (cmd) {
331 case V4LVIDEO_IOCTL_ALLOC_FD:
332 {
333 u32 v4lvideo_fd = 0;
334
335 ret = v4l2_alloc_fd(&v4lvideo_fd);
336 if (ret != 0)
337 break;
338 put_user(v4lvideo_fd, (u32 __user *)argp);
339 v4l_dbg(0, V4L_DEBUG_CODEC_EXINFO,
340 "V4LVIDEO_IOCTL_ALLOC_FD fd %d\n",
341 v4lvideo_fd);
342 break;
343 }
344 case V4LVIDEO_IOCTL_CHECK_FD:
345 {
346 u32 v4lvideo_fd = 0;
347
348 get_user(v4lvideo_fd, (u32 __user *)argp);
349 ret = v4l2_check_fd(v4lvideo_fd);
350 if (ret != 0)
351 break;
352 v4l_dbg(0, V4L_DEBUG_CODEC_EXINFO,
353 "V4LVIDEO_IOCTL_CHECK_FD fd %d\n",
354 v4lvideo_fd);
355 break;
356 }
357 case V4LVIDEO_IOCTL_SET_CONFIG_PARAMS:
358 {
359 struct aml_vcodec_ctx *ctx = NULL;
360
361 if (is_v4l2_buf_file(file))
362 break;
363
364 ctx = fh_to_ctx(file->private_data);
365 if (copy_from_user((void *)&ctx->config,
366 (void *)argp, sizeof(ctx->config))) {
367 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
368 "set config parm err\n");
369 return -EFAULT;
370 }
371 break;
372 }
373 case V4LVIDEO_IOCTL_GET_CONFIG_PARAMS:
374 {
375 struct aml_vcodec_ctx *ctx = NULL;
376
377 if (is_v4l2_buf_file(file))
378 break;
379
380 ctx = fh_to_ctx(file->private_data);
381 if (copy_to_user((void *)argp,
382 (void *)&ctx->config, sizeof(ctx->config))) {
383 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
384 "get config parm err\n");
385 return -EFAULT;
386 }
387 break;
388 }
389 default:
390 return video_ioctl2(file, cmd, arg);
391 }
392 return ret;
393}
394
395#ifdef CONFIG_COMPAT
396static long v4l2_compat_ioctl(struct file *file,
397 unsigned int cmd, ulong arg)
398{
399 long ret = 0;
400
401 ret = v4l2_vcodec_ioctl(file, cmd, (ulong)compat_ptr(arg));
402 return ret;
403}
404#endif
405
406static const struct v4l2_file_operations aml_vcodec_fops = {
407 .owner = THIS_MODULE,
408 .open = fops_vcodec_open,
409 .release = fops_vcodec_release,
410 .poll = v4l2_m2m_fop_poll,
411 .unlocked_ioctl = v4l2_vcodec_ioctl,
412#ifdef CONFIG_COMPAT
413 .compat_ioctl32 = v4l2_compat_ioctl,
414#endif
415 .mmap = v4l2_m2m_fop_mmap,
416};
417
418static int aml_vcodec_probe(struct platform_device *pdev)
419{
420 struct aml_vcodec_dev *dev;
421 struct video_device *vfd_dec;
422 int ret = 0;
423
424 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
425 if (!dev)
426 return -ENOMEM;
427
428 INIT_LIST_HEAD(&dev->ctx_list);
429 dev->plat_dev = pdev;
430
431 mutex_init(&dev->dec_mutex);
432 mutex_init(&dev->dev_mutex);
433 spin_lock_init(&dev->irqlock);
434
435 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
436 "[/AML_V4L2_VDEC]");
437
438 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
439 if (ret) {
440 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
441 "v4l2_device_register err=%d\n", ret);
442 goto err_res;
443 }
444
445 init_waitqueue_head(&dev->queue);
446
447 vfd_dec = video_device_alloc();
448 if (!vfd_dec) {
449 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
450 "Failed to allocate video device\n");
451 ret = -ENOMEM;
452 goto err_dec_alloc;
453 }
454
455 vfd_dec->fops = &aml_vcodec_fops;
456 vfd_dec->ioctl_ops = &aml_vdec_ioctl_ops;
457 vfd_dec->release = video_device_release;
458 vfd_dec->lock = &dev->dev_mutex;
459 vfd_dec->v4l2_dev = &dev->v4l2_dev;
460 vfd_dec->vfl_dir = VFL_DIR_M2M;
461 vfd_dec->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE |
462 V4L2_CAP_STREAMING;
463
464 snprintf(vfd_dec->name, sizeof(vfd_dec->name), "%s",
465 AML_VCODEC_DEC_NAME);
466 video_set_drvdata(vfd_dec, dev);
467 dev->vfd_dec = vfd_dec;
468 platform_set_drvdata(pdev, dev);
469
470 dev->m2m_dev_dec = v4l2_m2m_init(&aml_vdec_m2m_ops);
471 if (IS_ERR((__force void *)dev->m2m_dev_dec)) {
472 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
473 "Failed to init mem2mem dec device\n");
474 ret = PTR_ERR((__force void *)dev->m2m_dev_dec);
475 goto err_dec_mem_init;
476 }
477
478 dev->decode_workqueue =
479 alloc_ordered_workqueue(AML_VCODEC_DEC_NAME,
480 WQ_MEM_RECLAIM | WQ_FREEZABLE);
481 if (!dev->decode_workqueue) {
482 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
483 "Failed to create decode workqueue\n");
484 ret = -EINVAL;
485 goto err_event_workq;
486 }
487
488 //dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
489
490 ret = video_register_device(vfd_dec, VFL_TYPE_GRABBER, 26);
491 if (ret) {
492 v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
493 "Failed to register video device\n");
494 goto err_dec_reg;
495 }
496
497 v4l_dbg(0, V4L_DEBUG_CODEC_PRINFO,
498 "decoder registered as /dev/video%d\n", vfd_dec->num);
499
500 return 0;
501
502err_dec_reg:
503 destroy_workqueue(dev->decode_workqueue);
504err_event_workq:
505 v4l2_m2m_release(dev->m2m_dev_dec);
506err_dec_mem_init:
507 video_unregister_device(vfd_dec);
508err_dec_alloc:
509 v4l2_device_unregister(&dev->v4l2_dev);
510err_res:
511
512 return ret;
513}
514
515static const struct of_device_id aml_vcodec_match[] = {
516 {.compatible = "amlogic, vcodec-dec",},
517 {},
518};
519
520MODULE_DEVICE_TABLE(of, aml_vcodec_match);
521
522static int aml_vcodec_dec_remove(struct platform_device *pdev)
523{
524 struct aml_vcodec_dev *dev = platform_get_drvdata(pdev);
525
526 flush_workqueue(dev->decode_workqueue);
527 destroy_workqueue(dev->decode_workqueue);
528
529 if (dev->m2m_dev_dec)
530 v4l2_m2m_release(dev->m2m_dev_dec);
531
532 if (dev->vfd_dec)
533 video_unregister_device(dev->vfd_dec);
534
535 v4l2_device_unregister(&dev->v4l2_dev);
536
537 return 0;
538}
539
540/*static void aml_vcodec_dev_release(struct device *dev)
541{
542}*/
543
544static struct platform_driver aml_vcodec_dec_driver = {
545 .probe = aml_vcodec_probe,
546 .remove = aml_vcodec_dec_remove,
547 .driver = {
548 .name = AML_VCODEC_DEC_NAME,
549 .of_match_table = aml_vcodec_match,
550 },
551};
552
553/*
554static struct platform_device aml_vcodec_dec_device = {
555 .name = AML_VCODEC_DEC_NAME,
556 .dev.release = aml_vcodec_dev_release,
557};*/
558
559module_platform_driver(aml_vcodec_dec_driver);
560
561/*
562static int __init amvdec_ports_init(void)
563{
564 int ret;
565
566 ret = platform_device_register(&aml_vcodec_dec_device);
567 if (ret)
568 return ret;
569
570 ret = platform_driver_register(&aml_vcodec_dec_driver);
571 if (ret)
572 platform_device_unregister(&aml_vcodec_dec_device);
573
574 return ret;
575}
576
577static void __exit amvdec_ports_exit(void)
578{
579 platform_driver_unregister(&aml_vcodec_dec_driver);
580 platform_device_unregister(&aml_vcodec_dec_device);
581}
582
583module_init(amvdec_ports_init);
584module_exit(amvdec_ports_exit);
585*/
586
587u32 debug_mode;
588EXPORT_SYMBOL(debug_mode);
589module_param(debug_mode, uint, 0644);
590
591bool aml_set_vfm_enable;
592EXPORT_SYMBOL(aml_set_vfm_enable);
593module_param(aml_set_vfm_enable, bool, 0644);
594
595int aml_set_vfm_path;
596EXPORT_SYMBOL(aml_set_vfm_path);
597module_param(aml_set_vfm_path, int, 0644);
598
599bool aml_set_vdec_type_enable;
600EXPORT_SYMBOL(aml_set_vdec_type_enable);
601module_param(aml_set_vdec_type_enable, bool, 0644);
602
603int aml_set_vdec_type;
604EXPORT_SYMBOL(aml_set_vdec_type);
605module_param(aml_set_vdec_type, int, 0644);
606
607int vp9_need_prefix;
608EXPORT_SYMBOL(vp9_need_prefix);
609module_param(vp9_need_prefix, int, 0644);
610
611bool multiplanar;
612EXPORT_SYMBOL(multiplanar);
613module_param(multiplanar, bool, 0644);
614
615bool dump_capture_frame;
616EXPORT_SYMBOL(dump_capture_frame);
617module_param(dump_capture_frame, bool, 0644);
618
619EXPORT_SYMBOL(param_sets_from_ucode);
620module_param(param_sets_from_ucode, bool, 0644);
621
622EXPORT_SYMBOL(enable_drm_mode);
623module_param(enable_drm_mode, bool, 0644);
624
625MODULE_LICENSE("GPL v2");
626MODULE_DESCRIPTION("AML video codec V4L2 decoder driver");
627
628