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