1 // SPDX-License-Identifier: GPL-2.0
3 * Raspberry Pi HEVC driver
5 * Copyright (C) 2020 Raspberry Pi (Trading) Ltd
7 * Based on the Cedrus VPU driver, that is:
9 * Copyright (C) 2016 Florent Revest <florent.revest@free-electrons.com>
10 * Copyright (C) 2018 Paul Kocialkowski <paul.kocialkowski@bootlin.com>
11 * Copyright (C) 2018 Bootlin
14 #include <linux/platform_device.h>
15 #include <linux/module.h>
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-ioctl.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-mem2mem.h>
24 #include "rpivid_video.h"
25 #include "rpivid_hw.h"
26 #include "rpivid_dec.h"
29 * Default /dev/videoN node number.
30 * Deliberately avoid the very low numbers as these are often taken by webcams
31 * etc, and simple apps tend to only go for /dev/video0.
33 static int video_nr = 19;
34 module_param(video_nr, int, 0644);
35 MODULE_PARM_DESC(video_nr, "decoder video device number");
37 static const struct rpivid_control rpivid_ctrls[] = {
40 .id = V4L2_CID_MPEG_VIDEO_HEVC_SPS,
41 .ops = &rpivid_hevc_sps_ctrl_ops,
47 .id = V4L2_CID_MPEG_VIDEO_HEVC_PPS,
48 .ops = &rpivid_hevc_pps_ctrl_ops,
54 .id = V4L2_CID_MPEG_VIDEO_HEVC_SCALING_MATRIX,
60 .id = V4L2_CID_MPEG_VIDEO_HEVC_DECODE_PARAMS,
66 .name = "Slice param array",
67 .id = V4L2_CID_MPEG_VIDEO_HEVC_SLICE_PARAMS,
68 .type = V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS,
69 .flags = V4L2_CTRL_FLAG_DYNAMIC_ARRAY,
76 .id = V4L2_CID_MPEG_VIDEO_HEVC_DECODE_MODE,
77 .max = V4L2_MPEG_VIDEO_HEVC_DECODE_MODE_SLICE_BASED,
78 .def = V4L2_MPEG_VIDEO_HEVC_DECODE_MODE_SLICE_BASED,
84 .id = V4L2_CID_MPEG_VIDEO_HEVC_START_CODE,
85 .max = V4L2_MPEG_VIDEO_HEVC_START_CODE_NONE,
86 .def = V4L2_MPEG_VIDEO_HEVC_START_CODE_NONE,
92 #define rpivid_ctrls_COUNT ARRAY_SIZE(rpivid_ctrls)
94 struct v4l2_ctrl *rpivid_find_ctrl(struct rpivid_ctx *ctx, u32 id)
98 for (i = 0; ctx->ctrls[i]; i++)
99 if (ctx->ctrls[i]->id == id)
100 return ctx->ctrls[i];
105 void *rpivid_find_control_data(struct rpivid_ctx *ctx, u32 id)
107 struct v4l2_ctrl *const ctrl = rpivid_find_ctrl(ctx, id);
109 return !ctrl ? NULL : ctrl->p_cur.p;
112 static int rpivid_init_ctrls(struct rpivid_dev *dev, struct rpivid_ctx *ctx)
114 struct v4l2_ctrl_handler *hdl = &ctx->hdl;
115 struct v4l2_ctrl *ctrl;
116 unsigned int ctrl_size;
119 v4l2_ctrl_handler_init(hdl, rpivid_ctrls_COUNT);
121 v4l2_err(&dev->v4l2_dev,
122 "Failed to initialize control handler\n");
126 ctrl_size = sizeof(ctrl) * rpivid_ctrls_COUNT + 1;
128 ctx->ctrls = kzalloc(ctrl_size, GFP_KERNEL);
132 for (i = 0; i < rpivid_ctrls_COUNT; i++) {
133 ctrl = v4l2_ctrl_new_custom(hdl, &rpivid_ctrls[i].cfg,
136 v4l2_err(&dev->v4l2_dev,
137 "Failed to create new custom control id=%#x\n",
138 rpivid_ctrls[i].cfg.id);
140 v4l2_ctrl_handler_free(hdl);
145 ctx->ctrls[i] = ctrl;
148 ctx->fh.ctrl_handler = hdl;
149 v4l2_ctrl_handler_setup(hdl);
154 static int rpivid_request_validate(struct media_request *req)
156 struct media_request_object *obj;
157 struct v4l2_ctrl_handler *parent_hdl, *hdl;
158 struct rpivid_ctx *ctx = NULL;
159 struct v4l2_ctrl *ctrl_test;
163 list_for_each_entry(obj, &req->objects, list) {
164 struct vb2_buffer *vb;
166 if (vb2_request_object_is_buffer(obj)) {
167 vb = container_of(obj, struct vb2_buffer, req_obj);
168 ctx = vb2_get_drv_priv(vb->vb2_queue);
177 count = vb2_request_buffer_cnt(req);
179 v4l2_info(&ctx->dev->v4l2_dev,
180 "No buffer was provided with the request\n");
182 } else if (count > 1) {
183 v4l2_info(&ctx->dev->v4l2_dev,
184 "More than one buffer was provided with the request\n");
188 parent_hdl = &ctx->hdl;
190 hdl = v4l2_ctrl_request_hdl_find(req, parent_hdl);
192 v4l2_info(&ctx->dev->v4l2_dev, "Missing codec control(s)\n");
196 for (i = 0; i < rpivid_ctrls_COUNT; i++) {
197 if (!rpivid_ctrls[i].required)
201 v4l2_ctrl_request_hdl_ctrl_find(hdl,
202 rpivid_ctrls[i].cfg.id);
204 v4l2_info(&ctx->dev->v4l2_dev,
205 "Missing required codec control\n");
206 v4l2_ctrl_request_hdl_put(hdl);
211 v4l2_ctrl_request_hdl_put(hdl);
213 return vb2_request_validate(req);
216 static int rpivid_open(struct file *file)
218 struct rpivid_dev *dev = video_drvdata(file);
219 struct rpivid_ctx *ctx = NULL;
222 if (mutex_lock_interruptible(&dev->dev_mutex))
225 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
227 mutex_unlock(&dev->dev_mutex);
232 mutex_init(&ctx->ctx_mutex);
234 v4l2_fh_init(&ctx->fh, video_devdata(file));
235 file->private_data = &ctx->fh;
238 ret = rpivid_init_ctrls(dev, ctx);
242 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
244 if (IS_ERR(ctx->fh.m2m_ctx)) {
245 ret = PTR_ERR(ctx->fh.m2m_ctx);
249 /* The only bit of format info that we can guess now is H265 src
250 * Everything else we need more info for
252 rpivid_prepare_src_format(&ctx->src_fmt);
254 v4l2_fh_add(&ctx->fh);
256 mutex_unlock(&dev->dev_mutex);
261 v4l2_ctrl_handler_free(&ctx->hdl);
263 mutex_destroy(&ctx->ctx_mutex);
266 mutex_unlock(&dev->dev_mutex);
271 static int rpivid_release(struct file *file)
273 struct rpivid_dev *dev = video_drvdata(file);
274 struct rpivid_ctx *ctx = container_of(file->private_data,
275 struct rpivid_ctx, fh);
277 mutex_lock(&dev->dev_mutex);
279 v4l2_fh_del(&ctx->fh);
280 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
282 v4l2_ctrl_handler_free(&ctx->hdl);
285 v4l2_fh_exit(&ctx->fh);
286 mutex_destroy(&ctx->ctx_mutex);
290 mutex_unlock(&dev->dev_mutex);
295 static const struct v4l2_file_operations rpivid_fops = {
296 .owner = THIS_MODULE,
298 .release = rpivid_release,
299 .poll = v4l2_m2m_fop_poll,
300 .unlocked_ioctl = video_ioctl2,
301 .mmap = v4l2_m2m_fop_mmap,
304 static const struct video_device rpivid_video_device = {
306 .vfl_dir = VFL_DIR_M2M,
307 .fops = &rpivid_fops,
308 .ioctl_ops = &rpivid_ioctl_ops,
310 .release = video_device_release_empty,
311 .device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING,
314 static const struct v4l2_m2m_ops rpivid_m2m_ops = {
315 .device_run = rpivid_device_run,
318 static const struct media_device_ops rpivid_m2m_media_ops = {
319 .req_validate = rpivid_request_validate,
320 .req_queue = v4l2_m2m_request_queue,
323 static int rpivid_probe(struct platform_device *pdev)
325 struct rpivid_dev *dev;
326 struct video_device *vfd;
329 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
333 dev->vfd = rpivid_video_device;
334 dev->dev = &pdev->dev;
338 ret = rpivid_hw_probe(dev);
340 dev_err(&pdev->dev, "Failed to probe hardware\n");
344 dev->dec_ops = &rpivid_dec_ops_h265;
346 mutex_init(&dev->dev_mutex);
348 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
350 dev_err(&pdev->dev, "Failed to register V4L2 device\n");
355 vfd->lock = &dev->dev_mutex;
356 vfd->v4l2_dev = &dev->v4l2_dev;
358 snprintf(vfd->name, sizeof(vfd->name), "%s", rpivid_video_device.name);
359 video_set_drvdata(vfd, dev);
361 dev->m2m_dev = v4l2_m2m_init(&rpivid_m2m_ops);
362 if (IS_ERR(dev->m2m_dev)) {
363 v4l2_err(&dev->v4l2_dev,
364 "Failed to initialize V4L2 M2M device\n");
365 ret = PTR_ERR(dev->m2m_dev);
370 dev->mdev.dev = &pdev->dev;
371 strscpy(dev->mdev.model, RPIVID_NAME, sizeof(dev->mdev.model));
372 strscpy(dev->mdev.bus_info, "platform:" RPIVID_NAME,
373 sizeof(dev->mdev.bus_info));
375 media_device_init(&dev->mdev);
376 dev->mdev.ops = &rpivid_m2m_media_ops;
377 dev->v4l2_dev.mdev = &dev->mdev;
379 ret = video_register_device(vfd, VFL_TYPE_VIDEO, video_nr);
381 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
385 v4l2_info(&dev->v4l2_dev,
386 "Device registered as /dev/video%d\n", vfd->num);
388 ret = v4l2_m2m_register_media_controller(dev->m2m_dev, vfd,
389 MEDIA_ENT_F_PROC_VIDEO_DECODER);
391 v4l2_err(&dev->v4l2_dev,
392 "Failed to initialize V4L2 M2M media controller\n");
396 ret = media_device_register(&dev->mdev);
398 v4l2_err(&dev->v4l2_dev, "Failed to register media device\n");
402 platform_set_drvdata(pdev, dev);
407 v4l2_m2m_unregister_media_controller(dev->m2m_dev);
409 video_unregister_device(&dev->vfd);
411 v4l2_m2m_release(dev->m2m_dev);
413 v4l2_device_unregister(&dev->v4l2_dev);
418 static int rpivid_remove(struct platform_device *pdev)
420 struct rpivid_dev *dev = platform_get_drvdata(pdev);
422 if (media_devnode_is_registered(dev->mdev.devnode)) {
423 media_device_unregister(&dev->mdev);
424 v4l2_m2m_unregister_media_controller(dev->m2m_dev);
425 media_device_cleanup(&dev->mdev);
428 v4l2_m2m_release(dev->m2m_dev);
429 video_unregister_device(&dev->vfd);
430 v4l2_device_unregister(&dev->v4l2_dev);
432 rpivid_hw_remove(dev);
437 static const struct of_device_id rpivid_dt_match[] = {
439 .compatible = "raspberrypi,rpivid-vid-decoder",
443 MODULE_DEVICE_TABLE(of, rpivid_dt_match);
445 static struct platform_driver rpivid_driver = {
446 .probe = rpivid_probe,
447 .remove = rpivid_remove,
450 .of_match_table = of_match_ptr(rpivid_dt_match),
453 module_platform_driver(rpivid_driver);
455 MODULE_LICENSE("GPL v2");
456 MODULE_AUTHOR("John Cox <jc@kynesim.co.uk>");
457 MODULE_DESCRIPTION("Raspberry Pi HEVC V4L2 driver");