1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <pc.chen@mediatek.com>
5 *	Tiffany Lin <tiffany.lin@mediatek.com>
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.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_runtime.h>
15 #include <media/v4l2-event.h>
16 #include <media/v4l2-mem2mem.h>
17 #include <media/videobuf2-dma-contig.h>
18 
19 #include "mtk_vcodec_enc.h"
20 #include "mtk_vcodec_enc_pm.h"
21 #include "../common/mtk_vcodec_intr.h"
22 
23 static const struct mtk_video_fmt mtk_video_formats_output[] = {
24 	{
25 		.fourcc = V4L2_PIX_FMT_NV12M,
26 		.type = MTK_FMT_FRAME,
27 		.num_planes = 2,
28 	},
29 	{
30 		.fourcc = V4L2_PIX_FMT_NV21M,
31 		.type = MTK_FMT_FRAME,
32 		.num_planes = 2,
33 	},
34 	{
35 		.fourcc = V4L2_PIX_FMT_YUV420M,
36 		.type = MTK_FMT_FRAME,
37 		.num_planes = 3,
38 	},
39 	{
40 		.fourcc = V4L2_PIX_FMT_YVU420M,
41 		.type = MTK_FMT_FRAME,
42 		.num_planes = 3,
43 	},
44 };
45 
46 static const struct mtk_video_fmt mtk_video_formats_capture_h264[] =  {
47 	{
48 		.fourcc = V4L2_PIX_FMT_H264,
49 		.type = MTK_FMT_ENC,
50 		.num_planes = 1,
51 	},
52 };
53 
54 static const struct mtk_video_fmt mtk_video_formats_capture_vp8[] =  {
55 	{
56 		.fourcc = V4L2_PIX_FMT_VP8,
57 		.type = MTK_FMT_ENC,
58 		.num_planes = 1,
59 	},
60 };
61 
clean_irq_status(unsigned int irq_status,void __iomem * addr)62 static void clean_irq_status(unsigned int irq_status, void __iomem *addr)
63 {
64 	if (irq_status & MTK_VENC_IRQ_STATUS_PAUSE)
65 		writel(MTK_VENC_IRQ_STATUS_PAUSE, addr);
66 
67 	if (irq_status & MTK_VENC_IRQ_STATUS_SWITCH)
68 		writel(MTK_VENC_IRQ_STATUS_SWITCH, addr);
69 
70 	if (irq_status & MTK_VENC_IRQ_STATUS_DRAM)
71 		writel(MTK_VENC_IRQ_STATUS_DRAM, addr);
72 
73 	if (irq_status & MTK_VENC_IRQ_STATUS_SPS)
74 		writel(MTK_VENC_IRQ_STATUS_SPS, addr);
75 
76 	if (irq_status & MTK_VENC_IRQ_STATUS_PPS)
77 		writel(MTK_VENC_IRQ_STATUS_PPS, addr);
78 
79 	if (irq_status & MTK_VENC_IRQ_STATUS_FRM)
80 		writel(MTK_VENC_IRQ_STATUS_FRM, addr);
81 
82 }
mtk_vcodec_enc_irq_handler(int irq,void * priv)83 static irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv)
84 {
85 	struct mtk_vcodec_enc_dev *dev = priv;
86 	struct mtk_vcodec_enc_ctx *ctx;
87 	unsigned long flags;
88 	void __iomem *addr;
89 	int core_id;
90 
91 	spin_lock_irqsave(&dev->irqlock, flags);
92 	ctx = dev->curr_ctx;
93 	spin_unlock_irqrestore(&dev->irqlock, flags);
94 
95 	core_id = dev->venc_pdata->core_id;
96 	if (core_id < 0 || core_id >= NUM_MAX_VCODEC_REG_BASE) {
97 		mtk_v4l2_venc_err(ctx, "Invalid core id: %d, ctx id: %d", core_id, ctx->id);
98 		return IRQ_HANDLED;
99 	}
100 
101 	mtk_v4l2_venc_dbg(1, ctx, "id: %d, core id: %d", ctx->id, core_id);
102 
103 	addr = dev->reg_base[core_id] + MTK_VENC_IRQ_ACK_OFFSET;
104 
105 	ctx->irq_status = readl(dev->reg_base[core_id] +
106 				(MTK_VENC_IRQ_STATUS_OFFSET));
107 
108 	clean_irq_status(ctx->irq_status, addr);
109 
110 	wake_up_enc_ctx(ctx, MTK_INST_IRQ_RECEIVED, 0);
111 	return IRQ_HANDLED;
112 }
113 
fops_vcodec_open(struct file * file)114 static int fops_vcodec_open(struct file *file)
115 {
116 	struct mtk_vcodec_enc_dev *dev = video_drvdata(file);
117 	struct mtk_vcodec_enc_ctx *ctx = NULL;
118 	int ret = 0;
119 	struct vb2_queue *src_vq;
120 
121 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
122 	if (!ctx)
123 		return -ENOMEM;
124 
125 	mutex_lock(&dev->dev_mutex);
126 	/*
127 	 * Use simple counter to uniquely identify this context. Only
128 	 * used for logging.
129 	 */
130 	ctx->id = dev->id_counter++;
131 	v4l2_fh_init(&ctx->fh, video_devdata(file));
132 	file->private_data = &ctx->fh;
133 	v4l2_fh_add(&ctx->fh);
134 	INIT_LIST_HEAD(&ctx->list);
135 	ctx->dev = dev;
136 	init_waitqueue_head(&ctx->queue[0]);
137 	mutex_init(&ctx->q_mutex);
138 
139 	ctx->type = MTK_INST_ENCODER;
140 	ret = mtk_vcodec_enc_ctrls_setup(ctx);
141 	if (ret) {
142 		mtk_v4l2_venc_err(ctx, "Failed to setup controls() (%d)", ret);
143 		goto err_ctrls_setup;
144 	}
145 	ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_enc, ctx,
146 					 &mtk_vcodec_enc_queue_init);
147 	if (IS_ERR((__force void *)ctx->m2m_ctx)) {
148 		ret = PTR_ERR((__force void *)ctx->m2m_ctx);
149 		mtk_v4l2_venc_err(ctx, "Failed to v4l2_m2m_ctx_init() (%d)", ret);
150 		goto err_m2m_ctx_init;
151 	}
152 	src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
153 				 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
154 	ctx->empty_flush_buf.vb.vb2_buf.vb2_queue = src_vq;
155 	mtk_vcodec_enc_set_default_params(ctx);
156 
157 	if (v4l2_fh_is_singular(&ctx->fh)) {
158 		/*
159 		 * load fireware to checks if it was loaded already and
160 		 * does nothing in that case
161 		 */
162 		ret = mtk_vcodec_fw_load_firmware(dev->fw_handler);
163 		if (ret < 0) {
164 			/*
165 			 * Return 0 if downloading firmware successfully,
166 			 * otherwise it is failed
167 			 */
168 			mtk_v4l2_venc_err(ctx, "vpu_load_firmware failed!");
169 			goto err_load_fw;
170 		}
171 
172 		dev->enc_capability =
173 			mtk_vcodec_fw_get_venc_capa(dev->fw_handler);
174 		mtk_v4l2_venc_dbg(0, ctx, "encoder capability %x", dev->enc_capability);
175 	}
176 
177 	mtk_v4l2_venc_dbg(2, ctx, "Create instance [%d]@%p m2m_ctx=%p ",
178 			  ctx->id, ctx, ctx->m2m_ctx);
179 
180 	mutex_lock(&dev->dev_ctx_lock);
181 	list_add(&ctx->list, &dev->ctx_list);
182 	mutex_unlock(&dev->dev_ctx_lock);
183 
184 	mutex_unlock(&dev->dev_mutex);
185 	mtk_v4l2_venc_dbg(0, ctx, "%s encoder [%d]", dev_name(&dev->plat_dev->dev),
186 			  ctx->id);
187 	return ret;
188 
189 	/* Deinit when failure occurred */
190 err_load_fw:
191 	v4l2_m2m_ctx_release(ctx->m2m_ctx);
192 err_m2m_ctx_init:
193 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
194 err_ctrls_setup:
195 	v4l2_fh_del(&ctx->fh);
196 	v4l2_fh_exit(&ctx->fh);
197 	kfree(ctx);
198 	mutex_unlock(&dev->dev_mutex);
199 
200 	return ret;
201 }
202 
fops_vcodec_release(struct file * file)203 static int fops_vcodec_release(struct file *file)
204 {
205 	struct mtk_vcodec_enc_dev *dev = video_drvdata(file);
206 	struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(file->private_data);
207 
208 	mtk_v4l2_venc_dbg(1, ctx, "[%d] encoder", ctx->id);
209 	mutex_lock(&dev->dev_mutex);
210 
211 	v4l2_m2m_ctx_release(ctx->m2m_ctx);
212 	mtk_vcodec_enc_release(ctx);
213 	v4l2_fh_del(&ctx->fh);
214 	v4l2_fh_exit(&ctx->fh);
215 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
216 
217 	mutex_lock(&dev->dev_ctx_lock);
218 	list_del_init(&ctx->list);
219 	mutex_unlock(&dev->dev_ctx_lock);
220 	kfree(ctx);
221 	mutex_unlock(&dev->dev_mutex);
222 	return 0;
223 }
224 
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,
232 };
233 
mtk_vcodec_probe(struct platform_device * pdev)234 static int mtk_vcodec_probe(struct platform_device *pdev)
235 {
236 	struct mtk_vcodec_enc_dev *dev;
237 	struct video_device *vfd_enc;
238 	phandle rproc_phandle;
239 	enum mtk_vcodec_fw_type fw_type;
240 	int ret;
241 
242 	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
243 	if (!dev)
244 		return -ENOMEM;
245 
246 	INIT_LIST_HEAD(&dev->ctx_list);
247 	dev->plat_dev = pdev;
248 
249 	if (!of_property_read_u32(pdev->dev.of_node, "mediatek,vpu",
250 				  &rproc_phandle)) {
251 		fw_type = VPU;
252 	} else if (!of_property_read_u32(pdev->dev.of_node, "mediatek,scp",
253 					 &rproc_phandle)) {
254 		fw_type = SCP;
255 	} else {
256 		dev_err(&pdev->dev, "[MTK VCODEC] Could not get venc IPI device");
257 		return -ENODEV;
258 	}
259 	dma_set_max_seg_size(&pdev->dev, UINT_MAX);
260 
261 	dev->fw_handler = mtk_vcodec_fw_select(dev, fw_type, ENCODER);
262 	if (IS_ERR(dev->fw_handler))
263 		return PTR_ERR(dev->fw_handler);
264 
265 	dev->venc_pdata = of_device_get_match_data(&pdev->dev);
266 	ret = mtk_vcodec_init_enc_clk(dev);
267 	if (ret < 0) {
268 		dev_err(&pdev->dev, "[MTK VCODEC] Failed to get mtk vcodec clock source!");
269 		goto err_enc_pm;
270 	}
271 
272 	pm_runtime_enable(&pdev->dev);
273 
274 	dev->reg_base[dev->venc_pdata->core_id] =
275 		devm_platform_ioremap_resource(pdev, 0);
276 	if (IS_ERR(dev->reg_base[dev->venc_pdata->core_id])) {
277 		ret = PTR_ERR(dev->reg_base[dev->venc_pdata->core_id]);
278 		goto err_res;
279 	}
280 
281 	dev->enc_irq = platform_get_irq(pdev, 0);
282 	if (dev->enc_irq < 0) {
283 		ret = dev->enc_irq;
284 		goto err_res;
285 	}
286 
287 	irq_set_status_flags(dev->enc_irq, IRQ_NOAUTOEN);
288 	ret = devm_request_irq(&pdev->dev, dev->enc_irq,
289 			       mtk_vcodec_enc_irq_handler,
290 			       0, pdev->name, dev);
291 	if (ret) {
292 		dev_err(&pdev->dev,
293 			"[MTK VCODEC] Failed to install dev->enc_irq %d (%d) core_id (%d)",
294 			dev->enc_irq, ret, dev->venc_pdata->core_id);
295 		ret = -EINVAL;
296 		goto err_res;
297 	}
298 
299 	mutex_init(&dev->enc_mutex);
300 	mutex_init(&dev->dev_mutex);
301 	mutex_init(&dev->dev_ctx_lock);
302 	spin_lock_init(&dev->irqlock);
303 
304 	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
305 		 "[MTK_V4L2_VENC]");
306 
307 	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
308 	if (ret) {
309 		dev_err(&pdev->dev, "[MTK VCODEC] v4l2_device_register err=%d", ret);
310 		goto err_res;
311 	}
312 
313 	/* allocate video device for encoder and register it */
314 	vfd_enc = video_device_alloc();
315 	if (!vfd_enc) {
316 		dev_err(&pdev->dev, "[MTK VCODEC] Failed to allocate video device");
317 		ret = -ENOMEM;
318 		goto err_enc_alloc;
319 	}
320 	vfd_enc->fops           = &mtk_vcodec_fops;
321 	vfd_enc->ioctl_ops      = &mtk_venc_ioctl_ops;
322 	vfd_enc->release        = video_device_release;
323 	vfd_enc->lock           = &dev->dev_mutex;
324 	vfd_enc->v4l2_dev       = &dev->v4l2_dev;
325 	vfd_enc->vfl_dir        = VFL_DIR_M2M;
326 	vfd_enc->device_caps	= V4L2_CAP_VIDEO_M2M_MPLANE |
327 					V4L2_CAP_STREAMING;
328 
329 	snprintf(vfd_enc->name, sizeof(vfd_enc->name), "%s",
330 		 MTK_VCODEC_ENC_NAME);
331 	video_set_drvdata(vfd_enc, dev);
332 	dev->vfd_enc = vfd_enc;
333 	platform_set_drvdata(pdev, dev);
334 
335 	dev->m2m_dev_enc = v4l2_m2m_init(&mtk_venc_m2m_ops);
336 	if (IS_ERR((__force void *)dev->m2m_dev_enc)) {
337 		dev_err(&pdev->dev, "[MTK VCODEC] Failed to init mem2mem enc device");
338 		ret = PTR_ERR((__force void *)dev->m2m_dev_enc);
339 		goto err_enc_mem_init;
340 	}
341 
342 	dev->encode_workqueue =
343 			alloc_ordered_workqueue(MTK_VCODEC_ENC_NAME,
344 						WQ_MEM_RECLAIM |
345 						WQ_FREEZABLE);
346 	if (!dev->encode_workqueue) {
347 		dev_err(&pdev->dev, "[MTK VCODEC] Failed to create encode workqueue");
348 		ret = -EINVAL;
349 		goto err_event_workq;
350 	}
351 
352 	ret = video_register_device(vfd_enc, VFL_TYPE_VIDEO, -1);
353 	if (ret) {
354 		dev_err(&pdev->dev, "[MTK VCODEC] Failed to register video device");
355 		goto err_enc_reg;
356 	}
357 
358 	mtk_vcodec_dbgfs_init(dev, true);
359 	dev_dbg(&pdev->dev,  "[MTK VCODEC] encoder %d registered as /dev/video%d",
360 		dev->venc_pdata->core_id, vfd_enc->num);
361 
362 	return 0;
363 
364 err_enc_reg:
365 	destroy_workqueue(dev->encode_workqueue);
366 err_event_workq:
367 	v4l2_m2m_release(dev->m2m_dev_enc);
368 err_enc_mem_init:
369 	video_unregister_device(vfd_enc);
370 err_enc_alloc:
371 	v4l2_device_unregister(&dev->v4l2_dev);
372 err_res:
373 	pm_runtime_disable(dev->pm.dev);
374 err_enc_pm:
375 	mtk_vcodec_fw_release(dev->fw_handler);
376 	return ret;
377 }
378 
379 static const struct mtk_vcodec_enc_pdata mt8173_avc_pdata = {
380 	.capture_formats = mtk_video_formats_capture_h264,
381 	.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
382 	.output_formats = mtk_video_formats_output,
383 	.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
384 	.min_bitrate = 64,
385 	.max_bitrate = 60000000,
386 	.core_id = VENC_SYS,
387 };
388 
389 static const struct mtk_vcodec_enc_pdata mt8173_vp8_pdata = {
390 	.capture_formats = mtk_video_formats_capture_vp8,
391 	.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_vp8),
392 	.output_formats = mtk_video_formats_output,
393 	.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
394 	.min_bitrate = 64,
395 	.max_bitrate = 9000000,
396 	.core_id = VENC_LT_SYS,
397 };
398 
399 static const struct mtk_vcodec_enc_pdata mt8183_pdata = {
400 	.uses_ext = true,
401 	.capture_formats = mtk_video_formats_capture_h264,
402 	.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
403 	.output_formats = mtk_video_formats_output,
404 	.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
405 	.min_bitrate = 64,
406 	.max_bitrate = 40000000,
407 	.core_id = VENC_SYS,
408 };
409 
410 static const struct mtk_vcodec_enc_pdata mt8188_pdata = {
411 	.uses_ext = true,
412 	.capture_formats = mtk_video_formats_capture_h264,
413 	.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
414 	.output_formats = mtk_video_formats_output,
415 	.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
416 	.min_bitrate = 64,
417 	.max_bitrate = 50000000,
418 	.core_id = VENC_SYS,
419 	.uses_34bit = true,
420 };
421 
422 static const struct mtk_vcodec_enc_pdata mt8192_pdata = {
423 	.uses_ext = true,
424 	.capture_formats = mtk_video_formats_capture_h264,
425 	.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
426 	.output_formats = mtk_video_formats_output,
427 	.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
428 	.min_bitrate = 64,
429 	.max_bitrate = 100000000,
430 	.core_id = VENC_SYS,
431 };
432 
433 static const struct mtk_vcodec_enc_pdata mt8195_pdata = {
434 	.uses_ext = true,
435 	.capture_formats = mtk_video_formats_capture_h264,
436 	.num_capture_formats = ARRAY_SIZE(mtk_video_formats_capture_h264),
437 	.output_formats = mtk_video_formats_output,
438 	.num_output_formats = ARRAY_SIZE(mtk_video_formats_output),
439 	.min_bitrate = 64,
440 	.max_bitrate = 100000000,
441 	.core_id = VENC_SYS,
442 };
443 
444 static const struct of_device_id mtk_vcodec_enc_match[] = {
445 	{.compatible = "mediatek,mt8173-vcodec-enc",
446 			.data = &mt8173_avc_pdata},
447 	{.compatible = "mediatek,mt8173-vcodec-enc-vp8",
448 			.data = &mt8173_vp8_pdata},
449 	{.compatible = "mediatek,mt8183-vcodec-enc", .data = &mt8183_pdata},
450 	{.compatible = "mediatek,mt8188-vcodec-enc", .data = &mt8188_pdata},
451 	{.compatible = "mediatek,mt8192-vcodec-enc", .data = &mt8192_pdata},
452 	{.compatible = "mediatek,mt8195-vcodec-enc", .data = &mt8195_pdata},
453 	{},
454 };
455 MODULE_DEVICE_TABLE(of, mtk_vcodec_enc_match);
456 
mtk_vcodec_enc_remove(struct platform_device * pdev)457 static void mtk_vcodec_enc_remove(struct platform_device *pdev)
458 {
459 	struct mtk_vcodec_enc_dev *dev = platform_get_drvdata(pdev);
460 
461 	destroy_workqueue(dev->encode_workqueue);
462 	if (dev->m2m_dev_enc)
463 		v4l2_m2m_release(dev->m2m_dev_enc);
464 
465 	if (dev->vfd_enc)
466 		video_unregister_device(dev->vfd_enc);
467 
468 	mtk_vcodec_dbgfs_deinit(&dev->dbgfs);
469 	v4l2_device_unregister(&dev->v4l2_dev);
470 	pm_runtime_disable(dev->pm.dev);
471 	mtk_vcodec_fw_release(dev->fw_handler);
472 }
473 
474 static struct platform_driver mtk_vcodec_enc_driver = {
475 	.probe	= mtk_vcodec_probe,
476 	.remove_new = mtk_vcodec_enc_remove,
477 	.driver	= {
478 		.name	= MTK_VCODEC_ENC_NAME,
479 		.of_match_table = mtk_vcodec_enc_match,
480 	},
481 };
482 
483 module_platform_driver(mtk_vcodec_enc_driver);
484 
485 
486 MODULE_LICENSE("GPL v2");
487 MODULE_DESCRIPTION("Mediatek video codec V4L2 encoder driver");
488