summaryrefslogtreecommitdiff
path: root/drivers/amvdec_ports/aml_vcodec_dec_drv.c (plain)
blob: a50f9b17bc4b1e50a92415dd647b8edcffd04a5f
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_dec_pm.h"
36#include "aml_vcodec_util.h"
37#include "aml_vcodec_vfm.h"
38
39#define VDEC_HW_ACTIVE 0x10
40#define VDEC_IRQ_CFG 0x11
41#define VDEC_IRQ_CLR 0x10
42#define VDEC_IRQ_CFG_REG 0xa4
43
44bool scatter_mem_enable;
45bool param_sets_from_ucode;
46
47static int fops_vcodec_open(struct file *file)
48{
49 struct aml_vcodec_dev *dev = video_drvdata(file);
50 struct aml_vcodec_ctx *ctx = NULL;
51 struct aml_video_dec_buf *aml_buf = NULL;
52 int ret = 0;
53 struct vb2_queue *src_vq;
54
55 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
56 if (!ctx)
57 return -ENOMEM;
58 aml_buf = kzalloc(sizeof(*aml_buf), GFP_KERNEL);
59 if (!aml_buf) {
60 kfree(ctx);
61 return -ENOMEM;
62 }
63
64 mutex_lock(&dev->dev_mutex);
65 ctx->empty_flush_buf = aml_buf;
66 ctx->id = dev->id_counter++;
67 v4l2_fh_init(&ctx->fh, video_devdata(file));
68 file->private_data = &ctx->fh;
69 v4l2_fh_add(&ctx->fh);
70 INIT_LIST_HEAD(&ctx->list);
71 INIT_LIST_HEAD(&ctx->capture_list);
72 INIT_LIST_HEAD(&ctx->vdec_thread_list);
73 dev->filp = file;
74 ctx->dev = dev;
75 init_waitqueue_head(&ctx->queue);
76 mutex_init(&ctx->state_lock);
77 mutex_init(&ctx->lock);
78 spin_lock_init(&ctx->slock);
79 init_waitqueue_head(&ctx->wq);
80 init_completion(&ctx->comp);
81
82 ctx->scatter_mem_enable = scatter_mem_enable ? 1 : 0;
83 ctx->param_sets_from_ucode = param_sets_from_ucode ? 1 : 0;
84
85 ctx->type = AML_INST_DECODER;
86 ret = aml_vcodec_dec_ctrls_setup(ctx);
87 if (ret) {
88 aml_v4l2_err("Failed to setup vcodec controls");
89 goto err_ctrls_setup;
90 }
91 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_dec, ctx,
92 &aml_vcodec_dec_queue_init);
93 if (IS_ERR((__force void *)ctx->m2m_ctx)) {
94 ret = PTR_ERR((__force void *)ctx->m2m_ctx);
95 aml_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)", ret);
96 goto err_m2m_ctx_init;
97 }
98 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
99 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
100 ctx->empty_flush_buf->vb.vb2_buf.vb2_queue = src_vq;
101 ctx->empty_flush_buf->lastframe = true;
102 aml_vcodec_dec_set_default_params(ctx);
103
104 ret = aml_thread_start(ctx, try_to_capture, AML_THREAD_CAPTURE, "cap");
105 if (ret) {
106 aml_v4l2_err("Failed to creat capture thread.");
107 goto err_creat_thread;
108 }
109
110 list_add(&ctx->list, &dev->ctx_list);
111
112 mutex_unlock(&dev->dev_mutex);
113 pr_info("[%d] %s decoder\n", ctx->id, dev_name(&dev->plat_dev->dev));
114
115 return ret;
116
117 /* Deinit when failure occurred */
118err_creat_thread:
119 v4l2_m2m_ctx_release(ctx->m2m_ctx);
120err_m2m_ctx_init:
121 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
122err_ctrls_setup:
123 v4l2_fh_del(&ctx->fh);
124 v4l2_fh_exit(&ctx->fh);
125 kfree(ctx->empty_flush_buf);
126 kfree(ctx);
127 mutex_unlock(&dev->dev_mutex);
128
129 return ret;
130}
131
132static int fops_vcodec_release(struct file *file)
133{
134 struct aml_vcodec_dev *dev = video_drvdata(file);
135 struct aml_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
136
137 pr_info("[%d] release decoder\n", ctx->id);
138 mutex_lock(&dev->dev_mutex);
139
140 /*
141 * Call v4l2_m2m_ctx_release before aml_vcodec_dec_release. First, it
142 * makes sure the worker thread is not running after vdec_if_deinit.
143 * Second, the decoder will be flushed and all the buffers will be
144 * returned in stop_streaming.
145 */
146 aml_thread_stop(ctx);
147 wait_vcodec_ending(ctx);
148 v4l2_m2m_ctx_release(ctx->m2m_ctx);
149 aml_vcodec_dec_release(ctx);
150
151 v4l2_fh_del(&ctx->fh);
152 v4l2_fh_exit(&ctx->fh);
153 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
154
155 list_del_init(&ctx->list);
156 kfree(ctx->empty_flush_buf);
157 kfree(ctx);
158 mutex_unlock(&dev->dev_mutex);
159 return 0;
160}
161
162static const struct v4l2_file_operations aml_vcodec_fops = {
163 .owner = THIS_MODULE,
164 .open = fops_vcodec_open,
165 .release = fops_vcodec_release,
166 .poll = v4l2_m2m_fop_poll,
167 .unlocked_ioctl = video_ioctl2,
168 .mmap = v4l2_m2m_fop_mmap,
169};
170
171static int aml_vcodec_probe(struct platform_device *pdev)
172{
173 struct aml_vcodec_dev *dev;
174 struct video_device *vfd_dec;
175 int ret = 0;
176
177 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
178 if (!dev)
179 return -ENOMEM;
180
181 INIT_LIST_HEAD(&dev->ctx_list);
182 dev->plat_dev = pdev;
183
184 mutex_init(&dev->dec_mutex);
185 mutex_init(&dev->dev_mutex);
186 spin_lock_init(&dev->irqlock);
187
188 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
189 "[/AML_V4L2_VDEC]");
190
191 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
192 if (ret) {
193 aml_v4l2_err("v4l2_device_register err=%d", ret);
194 goto err_res;
195 }
196
197 init_waitqueue_head(&dev->queue);
198
199 vfd_dec = video_device_alloc();
200 if (!vfd_dec) {
201 aml_v4l2_err("Failed to allocate video device");
202 ret = -ENOMEM;
203 goto err_dec_alloc;
204 }
205
206 vfd_dec->fops = &aml_vcodec_fops;
207 vfd_dec->ioctl_ops = &aml_vdec_ioctl_ops;
208 vfd_dec->release = video_device_release;
209 vfd_dec->lock = &dev->dev_mutex;
210 vfd_dec->v4l2_dev = &dev->v4l2_dev;
211 vfd_dec->vfl_dir = VFL_DIR_M2M;
212 vfd_dec->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE |
213 V4L2_CAP_STREAMING;
214
215 snprintf(vfd_dec->name, sizeof(vfd_dec->name), "%s",
216 AML_VCODEC_DEC_NAME);
217 video_set_drvdata(vfd_dec, dev);
218 dev->vfd_dec = vfd_dec;
219 platform_set_drvdata(pdev, dev);
220
221 dev->m2m_dev_dec = v4l2_m2m_init(&aml_vdec_m2m_ops);
222 if (IS_ERR((__force void *)dev->m2m_dev_dec)) {
223 aml_v4l2_err("Failed to init mem2mem dec device");
224 ret = PTR_ERR((__force void *)dev->m2m_dev_dec);
225 goto err_dec_mem_init;
226 }
227
228 dev->decode_workqueue =
229 alloc_ordered_workqueue(AML_VCODEC_DEC_NAME,
230 WQ_MEM_RECLAIM | WQ_FREEZABLE);
231 if (!dev->decode_workqueue) {
232 aml_v4l2_err("Failed to create decode workqueue");
233 ret = -EINVAL;
234 goto err_event_workq;
235 }
236
237 //dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
238
239 ret = video_register_device(vfd_dec, VFL_TYPE_GRABBER, 26);
240 if (ret) {
241 pr_err("Failed to register video device\n");
242 goto err_dec_reg;
243 }
244
245 pr_info("decoder registered as /dev/video%d\n", vfd_dec->num);
246
247 return 0;
248
249err_dec_reg:
250 destroy_workqueue(dev->decode_workqueue);
251err_event_workq:
252 v4l2_m2m_release(dev->m2m_dev_dec);
253err_dec_mem_init:
254 video_unregister_device(vfd_dec);
255err_dec_alloc:
256 v4l2_device_unregister(&dev->v4l2_dev);
257err_res:
258
259 return ret;
260}
261
262static const struct of_device_id aml_vcodec_match[] = {
263 {.compatible = "amlogic, vcodec-dec",},
264 {},
265};
266
267MODULE_DEVICE_TABLE(of, aml_vcodec_match);
268
269static int aml_vcodec_dec_remove(struct platform_device *pdev)
270{
271 struct aml_vcodec_dev *dev = platform_get_drvdata(pdev);
272
273 flush_workqueue(dev->decode_workqueue);
274 destroy_workqueue(dev->decode_workqueue);
275
276 if (dev->m2m_dev_dec)
277 v4l2_m2m_release(dev->m2m_dev_dec);
278
279 if (dev->vfd_dec)
280 video_unregister_device(dev->vfd_dec);
281
282 v4l2_device_unregister(&dev->v4l2_dev);
283
284 return 0;
285}
286
287/*static void aml_vcodec_dev_release(struct device *dev)
288{
289}*/
290
291static struct platform_driver aml_vcodec_dec_driver = {
292 .probe = aml_vcodec_probe,
293 .remove = aml_vcodec_dec_remove,
294 .driver = {
295 .name = AML_VCODEC_DEC_NAME,
296 .of_match_table = aml_vcodec_match,
297 },
298};
299
300/*
301static struct platform_device aml_vcodec_dec_device = {
302 .name = AML_VCODEC_DEC_NAME,
303 .dev.release = aml_vcodec_dev_release,
304};*/
305
306module_platform_driver(aml_vcodec_dec_driver);
307
308/*
309static int __init amvdec_ports_init(void)
310{
311 int ret;
312
313 ret = platform_device_register(&aml_vcodec_dec_device);
314 if (ret)
315 return ret;
316
317 ret = platform_driver_register(&aml_vcodec_dec_driver);
318 if (ret)
319 platform_device_unregister(&aml_vcodec_dec_device);
320
321 return ret;
322}
323
324static void __exit amvdec_ports_exit(void)
325{
326 platform_driver_unregister(&aml_vcodec_dec_driver);
327 platform_device_unregister(&aml_vcodec_dec_device);
328}
329
330module_init(amvdec_ports_init);
331module_exit(amvdec_ports_exit);
332*/
333
334module_param(aml_v4l2_dbg_level, int, 0644);
335module_param(aml_vcodec_dbg, bool, 0644);
336
337bool aml_set_vfm_enable;
338EXPORT_SYMBOL(aml_set_vfm_enable);
339module_param(aml_set_vfm_enable, bool, 0644);
340
341int aml_set_vfm_path;
342EXPORT_SYMBOL(aml_set_vfm_path);
343module_param(aml_set_vfm_path, int, 0644);
344
345bool aml_set_vdec_type_enable;
346EXPORT_SYMBOL(aml_set_vdec_type_enable);
347module_param(aml_set_vdec_type_enable, bool, 0644);
348
349int aml_set_vdec_type;
350EXPORT_SYMBOL(aml_set_vdec_type);
351module_param(aml_set_vdec_type, int, 0644);
352
353int vp9_need_prefix;
354EXPORT_SYMBOL(vp9_need_prefix);
355module_param(vp9_need_prefix, int, 0644);
356
357bool multiplanar;
358EXPORT_SYMBOL(multiplanar);
359module_param(multiplanar, bool, 0644);
360
361EXPORT_SYMBOL(scatter_mem_enable);
362module_param(scatter_mem_enable, bool, 0644);
363
364EXPORT_SYMBOL(param_sets_from_ucode);
365module_param(param_sets_from_ucode, bool, 0644);
366
367MODULE_LICENSE("GPL v2");
368MODULE_DESCRIPTION("AML video codec V4L2 decoder driver");
369
370