2 * Copyright (C) STMicroelectronics SA 2015
3 * Authors: Yannick Fertre <yannick.fertre@st.com>
4 * Hugues Fruchet <hugues.fruchet@st.com>
5 * License terms: GNU General Public License (GPL), version 2
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/slab.h>
11 #include <media/v4l2-event.h>
12 #include <media/v4l2-ioctl.h>
13 #include <media/videobuf2-dma-contig.h>
18 #define HVA_NAME "st-hva"
23 #define HVA_MIN_WIDTH 32
24 #define HVA_MAX_WIDTH 1920
25 #define HVA_MIN_HEIGHT 32
26 #define HVA_MAX_HEIGHT 1920
28 /* HVA requires a 16x16 pixels alignment for frames */
29 #define HVA_WIDTH_ALIGNMENT 16
30 #define HVA_HEIGHT_ALIGNMENT 16
32 #define HVA_DEFAULT_WIDTH HVA_MIN_WIDTH
33 #define HVA_DEFAULT_HEIGHT HVA_MIN_HEIGHT
34 #define HVA_DEFAULT_FRAME_NUM 1
35 #define HVA_DEFAULT_FRAME_DEN 30
37 #define to_type_str(type) (type == V4L2_BUF_TYPE_VIDEO_OUTPUT ? \
40 #define fh_to_ctx(f) (container_of(f, struct hva_ctx, fh))
42 /* registry of available encoders */
43 static const struct hva_enc
*hva_encoders
[] = {
48 static inline int frame_size(u32 w
, u32 h
, u32 fmt
)
51 case V4L2_PIX_FMT_NV12
:
52 case V4L2_PIX_FMT_NV21
:
53 return (w
* h
* 3) / 2;
59 static inline int frame_stride(u32 w
, u32 fmt
)
62 case V4L2_PIX_FMT_NV12
:
63 case V4L2_PIX_FMT_NV21
:
70 static inline int frame_alignment(u32 fmt
)
73 case V4L2_PIX_FMT_NV12
:
74 case V4L2_PIX_FMT_NV21
:
82 static inline int estimated_stream_size(u32 w
, u32 h
)
85 * HVA only encodes in YUV420 format, whatever the frame format.
86 * A compression ratio of 2 is assumed: thus, the maximum size
87 * of a stream is estimated to ((width x height x 3 / 2) / 2)
89 return (w
* h
* 3) / 4;
92 static void set_default_params(struct hva_ctx
*ctx
)
94 struct hva_frameinfo
*frameinfo
= &ctx
->frameinfo
;
95 struct hva_streaminfo
*streaminfo
= &ctx
->streaminfo
;
97 frameinfo
->pixelformat
= V4L2_PIX_FMT_NV12
;
98 frameinfo
->width
= HVA_DEFAULT_WIDTH
;
99 frameinfo
->height
= HVA_DEFAULT_HEIGHT
;
100 frameinfo
->aligned_width
= ALIGN(frameinfo
->width
,
101 HVA_WIDTH_ALIGNMENT
);
102 frameinfo
->aligned_height
= ALIGN(frameinfo
->height
,
103 HVA_HEIGHT_ALIGNMENT
);
104 frameinfo
->size
= frame_size(frameinfo
->aligned_width
,
105 frameinfo
->aligned_height
,
106 frameinfo
->pixelformat
);
108 streaminfo
->streamformat
= V4L2_PIX_FMT_H264
;
109 streaminfo
->width
= HVA_DEFAULT_WIDTH
;
110 streaminfo
->height
= HVA_DEFAULT_HEIGHT
;
112 ctx
->colorspace
= V4L2_COLORSPACE_REC709
;
113 ctx
->xfer_func
= V4L2_XFER_FUNC_DEFAULT
;
114 ctx
->ycbcr_enc
= V4L2_YCBCR_ENC_DEFAULT
;
115 ctx
->quantization
= V4L2_QUANTIZATION_DEFAULT
;
117 ctx
->max_stream_size
= estimated_stream_size(streaminfo
->width
,
121 static const struct hva_enc
*hva_find_encoder(struct hva_ctx
*ctx
,
125 struct hva_dev
*hva
= ctx_to_hdev(ctx
);
126 const struct hva_enc
*enc
;
129 for (i
= 0; i
< hva
->nb_of_encoders
; i
++) {
130 enc
= hva
->encoders
[i
];
131 if ((enc
->pixelformat
== pixelformat
) &&
132 (enc
->streamformat
== streamformat
))
139 static void register_format(u32 format
, u32 formats
[], u32
*nb_of_formats
)
144 for (i
= 0; i
< *nb_of_formats
; i
++) {
145 if (format
== formats
[i
]) {
152 formats
[(*nb_of_formats
)++] = format
;
155 static void register_formats(struct hva_dev
*hva
)
159 for (i
= 0; i
< hva
->nb_of_encoders
; i
++) {
160 register_format(hva
->encoders
[i
]->pixelformat
,
162 &hva
->nb_of_pixelformats
);
164 register_format(hva
->encoders
[i
]->streamformat
,
166 &hva
->nb_of_streamformats
);
170 static void register_encoders(struct hva_dev
*hva
)
172 struct device
*dev
= hva_to_dev(hva
);
175 for (i
= 0; i
< ARRAY_SIZE(hva_encoders
); i
++) {
176 if (hva
->nb_of_encoders
>= HVA_MAX_ENCODERS
) {
178 "%s failed to register %s encoder (%d maximum reached)\n",
179 HVA_PREFIX
, hva_encoders
[i
]->name
,
184 hva
->encoders
[hva
->nb_of_encoders
++] = hva_encoders
[i
];
185 dev_info(dev
, "%s %s encoder registered\n", HVA_PREFIX
,
186 hva_encoders
[i
]->name
);
190 static int hva_open_encoder(struct hva_ctx
*ctx
, u32 streamformat
,
191 u32 pixelformat
, struct hva_enc
**penc
)
193 struct hva_dev
*hva
= ctx_to_hdev(ctx
);
194 struct device
*dev
= ctx_to_dev(ctx
);
198 /* find an encoder which can deal with these formats */
199 enc
= (struct hva_enc
*)hva_find_encoder(ctx
, pixelformat
,
202 dev_err(dev
, "%s no encoder found matching %4.4s => %4.4s\n",
203 ctx
->name
, (char *)&pixelformat
, (char *)&streamformat
);
207 dev_dbg(dev
, "%s one encoder matching %4.4s => %4.4s\n",
208 ctx
->name
, (char *)&pixelformat
, (char *)&streamformat
);
210 /* update instance name */
211 snprintf(ctx
->name
, sizeof(ctx
->name
), "[%3d:%4.4s]",
212 hva
->instance_id
, (char *)&streamformat
);
214 /* open encoder instance */
215 ret
= enc
->open(ctx
);
217 dev_err(dev
, "%s failed to open encoder instance (%d)\n",
222 dev_dbg(dev
, "%s %s encoder opened\n", ctx
->name
, enc
->name
);
230 * V4L2 ioctl operations
233 static int hva_querycap(struct file
*file
, void *priv
,
234 struct v4l2_capability
*cap
)
236 struct hva_ctx
*ctx
= fh_to_ctx(file
->private_data
);
237 struct hva_dev
*hva
= ctx_to_hdev(ctx
);
239 strlcpy(cap
->driver
, HVA_NAME
, sizeof(cap
->driver
));
240 strlcpy(cap
->card
, hva
->vdev
->name
, sizeof(cap
->card
));
241 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "platform:%s",
247 static int hva_enum_fmt_stream(struct file
*file
, void *priv
,
248 struct v4l2_fmtdesc
*f
)
250 struct hva_ctx
*ctx
= fh_to_ctx(file
->private_data
);
251 struct hva_dev
*hva
= ctx_to_hdev(ctx
);
253 if (unlikely(f
->index
>= hva
->nb_of_streamformats
))
256 f
->pixelformat
= hva
->streamformats
[f
->index
];
261 static int hva_enum_fmt_frame(struct file
*file
, void *priv
,
262 struct v4l2_fmtdesc
*f
)
264 struct hva_ctx
*ctx
= fh_to_ctx(file
->private_data
);
265 struct hva_dev
*hva
= ctx_to_hdev(ctx
);
267 if (unlikely(f
->index
>= hva
->nb_of_pixelformats
))
270 f
->pixelformat
= hva
->pixelformats
[f
->index
];
275 static int hva_g_fmt_stream(struct file
*file
, void *fh
, struct v4l2_format
*f
)
277 struct hva_ctx
*ctx
= fh_to_ctx(file
->private_data
);
278 struct hva_streaminfo
*streaminfo
= &ctx
->streaminfo
;
280 f
->fmt
.pix
.width
= streaminfo
->width
;
281 f
->fmt
.pix
.height
= streaminfo
->height
;
282 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
283 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
284 f
->fmt
.pix
.xfer_func
= ctx
->xfer_func
;
285 f
->fmt
.pix
.ycbcr_enc
= ctx
->ycbcr_enc
;
286 f
->fmt
.pix
.quantization
= ctx
->quantization
;
287 f
->fmt
.pix
.pixelformat
= streaminfo
->streamformat
;
288 f
->fmt
.pix
.bytesperline
= 0;
289 f
->fmt
.pix
.sizeimage
= ctx
->max_stream_size
;
294 static int hva_g_fmt_frame(struct file
*file
, void *fh
, struct v4l2_format
*f
)
296 struct hva_ctx
*ctx
= fh_to_ctx(file
->private_data
);
297 struct hva_frameinfo
*frameinfo
= &ctx
->frameinfo
;
299 f
->fmt
.pix
.width
= frameinfo
->width
;
300 f
->fmt
.pix
.height
= frameinfo
->height
;
301 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
302 f
->fmt
.pix
.colorspace
= ctx
->colorspace
;
303 f
->fmt
.pix
.xfer_func
= ctx
->xfer_func
;
304 f
->fmt
.pix
.ycbcr_enc
= ctx
->ycbcr_enc
;
305 f
->fmt
.pix
.quantization
= ctx
->quantization
;
306 f
->fmt
.pix
.pixelformat
= frameinfo
->pixelformat
;
307 f
->fmt
.pix
.bytesperline
= frame_stride(frameinfo
->aligned_width
,
308 frameinfo
->pixelformat
);
309 f
->fmt
.pix
.sizeimage
= frameinfo
->size
;
314 static int hva_try_fmt_stream(struct file
*file
, void *priv
,
315 struct v4l2_format
*f
)
317 struct hva_ctx
*ctx
= fh_to_ctx(file
->private_data
);
318 struct device
*dev
= ctx_to_dev(ctx
);
319 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
320 u32 streamformat
= pix
->pixelformat
;
321 const struct hva_enc
*enc
;
325 enc
= hva_find_encoder(ctx
, ctx
->frameinfo
.pixelformat
, streamformat
);
328 "%s V4L2 TRY_FMT (CAPTURE): unsupported format %.4s\n",
329 ctx
->name
, (char *)&pix
->pixelformat
);
334 height
= pix
->height
;
335 if (ctx
->flags
& HVA_FLAG_FRAMEINFO
) {
337 * if the frame resolution is already fixed, only allow the
338 * same stream resolution
340 pix
->width
= ctx
->frameinfo
.width
;
341 pix
->height
= ctx
->frameinfo
.height
;
342 if ((pix
->width
!= width
) || (pix
->height
!= height
))
344 "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit frame resolution\n",
345 ctx
->name
, width
, height
,
346 pix
->width
, pix
->height
);
348 /* adjust width & height */
349 v4l_bound_align_image(&pix
->width
,
350 HVA_MIN_WIDTH
, enc
->max_width
,
353 HVA_MIN_HEIGHT
, enc
->max_height
,
357 if ((pix
->width
!= width
) || (pix
->height
!= height
))
359 "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
360 ctx
->name
, width
, height
,
361 pix
->width
, pix
->height
);
364 stream_size
= estimated_stream_size(pix
->width
, pix
->height
);
365 if (pix
->sizeimage
< stream_size
)
366 pix
->sizeimage
= stream_size
;
368 pix
->bytesperline
= 0;
369 pix
->colorspace
= ctx
->colorspace
;
370 pix
->xfer_func
= ctx
->xfer_func
;
371 pix
->ycbcr_enc
= ctx
->ycbcr_enc
;
372 pix
->quantization
= ctx
->quantization
;
373 pix
->field
= V4L2_FIELD_NONE
;
378 static int hva_try_fmt_frame(struct file
*file
, void *priv
,
379 struct v4l2_format
*f
)
381 struct hva_ctx
*ctx
= fh_to_ctx(file
->private_data
);
382 struct device
*dev
= ctx_to_dev(ctx
);
383 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
384 u32 pixelformat
= pix
->pixelformat
;
385 const struct hva_enc
*enc
;
388 enc
= hva_find_encoder(ctx
, pixelformat
, ctx
->streaminfo
.streamformat
);
391 "%s V4L2 TRY_FMT (OUTPUT): unsupported format %.4s\n",
392 ctx
->name
, (char *)&pixelformat
);
396 /* adjust width & height */
398 height
= pix
->height
;
399 v4l_bound_align_image(&pix
->width
,
400 HVA_MIN_WIDTH
, HVA_MAX_WIDTH
,
401 frame_alignment(pixelformat
) - 1,
403 HVA_MIN_HEIGHT
, HVA_MAX_HEIGHT
,
404 frame_alignment(pixelformat
) - 1,
407 if ((pix
->width
!= width
) || (pix
->height
!= height
))
409 "%s V4L2 TRY_FMT (OUTPUT): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
410 ctx
->name
, width
, height
, pix
->width
, pix
->height
);
412 width
= ALIGN(pix
->width
, HVA_WIDTH_ALIGNMENT
);
413 height
= ALIGN(pix
->height
, HVA_HEIGHT_ALIGNMENT
);
415 if (!pix
->colorspace
) {
416 pix
->colorspace
= V4L2_COLORSPACE_REC709
;
417 pix
->xfer_func
= V4L2_XFER_FUNC_DEFAULT
;
418 pix
->ycbcr_enc
= V4L2_YCBCR_ENC_DEFAULT
;
419 pix
->quantization
= V4L2_QUANTIZATION_DEFAULT
;
422 pix
->bytesperline
= frame_stride(width
, pixelformat
);
423 pix
->sizeimage
= frame_size(width
, height
, pixelformat
);
424 pix
->field
= V4L2_FIELD_NONE
;
429 static int hva_s_fmt_stream(struct file
*file
, void *fh
, struct v4l2_format
*f
)
431 struct hva_ctx
*ctx
= fh_to_ctx(file
->private_data
);
432 struct device
*dev
= ctx_to_dev(ctx
);
433 struct vb2_queue
*vq
;
436 ret
= hva_try_fmt_stream(file
, fh
, f
);
438 dev_dbg(dev
, "%s V4L2 S_FMT (CAPTURE): unsupported format %.4s\n",
439 ctx
->name
, (char *)&f
->fmt
.pix
.pixelformat
);
443 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
444 if (vb2_is_streaming(vq
)) {
445 dev_dbg(dev
, "%s V4L2 S_FMT (CAPTURE): queue busy\n",
450 ctx
->max_stream_size
= f
->fmt
.pix
.sizeimage
;
451 ctx
->streaminfo
.width
= f
->fmt
.pix
.width
;
452 ctx
->streaminfo
.height
= f
->fmt
.pix
.height
;
453 ctx
->streaminfo
.streamformat
= f
->fmt
.pix
.pixelformat
;
454 ctx
->flags
|= HVA_FLAG_STREAMINFO
;
459 static int hva_s_fmt_frame(struct file
*file
, void *fh
, struct v4l2_format
*f
)
461 struct hva_ctx
*ctx
= fh_to_ctx(file
->private_data
);
462 struct device
*dev
= ctx_to_dev(ctx
);
463 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
464 struct vb2_queue
*vq
;
467 ret
= hva_try_fmt_frame(file
, fh
, f
);
469 dev_dbg(dev
, "%s V4L2 S_FMT (OUTPUT): unsupported format %.4s\n",
470 ctx
->name
, (char *)&pix
->pixelformat
);
474 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
475 if (vb2_is_streaming(vq
)) {
476 dev_dbg(dev
, "%s V4L2 S_FMT (OUTPUT): queue busy\n", ctx
->name
);
480 ctx
->colorspace
= pix
->colorspace
;
481 ctx
->xfer_func
= pix
->xfer_func
;
482 ctx
->ycbcr_enc
= pix
->ycbcr_enc
;
483 ctx
->quantization
= pix
->quantization
;
485 ctx
->frameinfo
.aligned_width
= ALIGN(pix
->width
, HVA_WIDTH_ALIGNMENT
);
486 ctx
->frameinfo
.aligned_height
= ALIGN(pix
->height
,
487 HVA_HEIGHT_ALIGNMENT
);
488 ctx
->frameinfo
.size
= pix
->sizeimage
;
489 ctx
->frameinfo
.pixelformat
= pix
->pixelformat
;
490 ctx
->frameinfo
.width
= pix
->width
;
491 ctx
->frameinfo
.height
= pix
->height
;
492 ctx
->flags
|= HVA_FLAG_FRAMEINFO
;
497 static int hva_g_parm(struct file
*file
, void *fh
, struct v4l2_streamparm
*sp
)
499 struct hva_ctx
*ctx
= fh_to_ctx(file
->private_data
);
500 struct v4l2_fract
*time_per_frame
= &ctx
->ctrls
.time_per_frame
;
502 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
505 sp
->parm
.output
.capability
= V4L2_CAP_TIMEPERFRAME
;
506 sp
->parm
.output
.timeperframe
.numerator
= time_per_frame
->numerator
;
507 sp
->parm
.output
.timeperframe
.denominator
=
508 time_per_frame
->denominator
;
513 static int hva_s_parm(struct file
*file
, void *fh
, struct v4l2_streamparm
*sp
)
515 struct hva_ctx
*ctx
= fh_to_ctx(file
->private_data
);
516 struct v4l2_fract
*time_per_frame
= &ctx
->ctrls
.time_per_frame
;
518 if (sp
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
521 if (!sp
->parm
.output
.timeperframe
.numerator
||
522 !sp
->parm
.output
.timeperframe
.denominator
)
523 return hva_g_parm(file
, fh
, sp
);
525 sp
->parm
.output
.capability
= V4L2_CAP_TIMEPERFRAME
;
526 time_per_frame
->numerator
= sp
->parm
.output
.timeperframe
.numerator
;
527 time_per_frame
->denominator
=
528 sp
->parm
.output
.timeperframe
.denominator
;
533 static int hva_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
535 struct hva_ctx
*ctx
= fh_to_ctx(file
->private_data
);
536 struct device
*dev
= ctx_to_dev(ctx
);
538 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
540 * depending on the targeted compressed video format, the
541 * capture buffer might contain headers (e.g. H.264 SPS/PPS)
542 * filled in by the driver client; the size of these data is
543 * copied from the bytesused field of the V4L2 buffer in the
544 * payload field of the hva stream buffer
546 struct vb2_queue
*vq
;
547 struct hva_stream
*stream
;
549 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, buf
->type
);
551 if (buf
->index
>= vq
->num_buffers
) {
552 dev_dbg(dev
, "%s buffer index %d out of range (%d)\n",
553 ctx
->name
, buf
->index
, vq
->num_buffers
);
557 stream
= (struct hva_stream
*)vq
->bufs
[buf
->index
];
558 stream
->bytesused
= buf
->bytesused
;
561 return v4l2_m2m_qbuf(file
, ctx
->fh
.m2m_ctx
, buf
);
565 static const struct v4l2_ioctl_ops hva_ioctl_ops
= {
566 .vidioc_querycap
= hva_querycap
,
567 .vidioc_enum_fmt_vid_cap
= hva_enum_fmt_stream
,
568 .vidioc_enum_fmt_vid_out
= hva_enum_fmt_frame
,
569 .vidioc_g_fmt_vid_cap
= hva_g_fmt_stream
,
570 .vidioc_g_fmt_vid_out
= hva_g_fmt_frame
,
571 .vidioc_try_fmt_vid_cap
= hva_try_fmt_stream
,
572 .vidioc_try_fmt_vid_out
= hva_try_fmt_frame
,
573 .vidioc_s_fmt_vid_cap
= hva_s_fmt_stream
,
574 .vidioc_s_fmt_vid_out
= hva_s_fmt_frame
,
575 .vidioc_g_parm
= hva_g_parm
,
576 .vidioc_s_parm
= hva_s_parm
,
577 .vidioc_reqbufs
= v4l2_m2m_ioctl_reqbufs
,
578 .vidioc_create_bufs
= v4l2_m2m_ioctl_create_bufs
,
579 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
580 .vidioc_expbuf
= v4l2_m2m_ioctl_expbuf
,
581 .vidioc_qbuf
= hva_qbuf
,
582 .vidioc_dqbuf
= v4l2_m2m_ioctl_dqbuf
,
583 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
584 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
585 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
586 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
590 * V4L2 control operations
593 static int hva_s_ctrl(struct v4l2_ctrl
*ctrl
)
595 struct hva_ctx
*ctx
= container_of(ctrl
->handler
, struct hva_ctx
,
597 struct device
*dev
= ctx_to_dev(ctx
);
599 dev_dbg(dev
, "%s S_CTRL: id = %d, val = %d\n", ctx
->name
,
600 ctrl
->id
, ctrl
->val
);
603 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
604 ctx
->ctrls
.bitrate_mode
= ctrl
->val
;
606 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
607 ctx
->ctrls
.gop_size
= ctrl
->val
;
609 case V4L2_CID_MPEG_VIDEO_BITRATE
:
610 ctx
->ctrls
.bitrate
= ctrl
->val
;
612 case V4L2_CID_MPEG_VIDEO_ASPECT
:
613 ctx
->ctrls
.aspect
= ctrl
->val
;
615 case V4L2_CID_MPEG_VIDEO_H264_PROFILE
:
616 ctx
->ctrls
.profile
= ctrl
->val
;
617 if (ctx
->flags
& HVA_FLAG_STREAMINFO
)
618 snprintf(ctx
->streaminfo
.profile
,
619 sizeof(ctx
->streaminfo
.profile
),
621 v4l2_ctrl_get_menu(ctrl
->id
)[ctrl
->val
]);
623 case V4L2_CID_MPEG_VIDEO_H264_LEVEL
:
624 ctx
->ctrls
.level
= ctrl
->val
;
625 if (ctx
->flags
& HVA_FLAG_STREAMINFO
)
626 snprintf(ctx
->streaminfo
.level
,
627 sizeof(ctx
->streaminfo
.level
),
629 v4l2_ctrl_get_menu(ctrl
->id
)[ctrl
->val
]);
631 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE
:
632 ctx
->ctrls
.entropy_mode
= ctrl
->val
;
634 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE
:
635 ctx
->ctrls
.cpb_size
= ctrl
->val
;
637 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM
:
638 ctx
->ctrls
.dct8x8
= ctrl
->val
;
640 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP
:
641 ctx
->ctrls
.qpmin
= ctrl
->val
;
643 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP
:
644 ctx
->ctrls
.qpmax
= ctrl
->val
;
646 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE
:
647 ctx
->ctrls
.vui_sar
= ctrl
->val
;
649 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC
:
650 ctx
->ctrls
.vui_sar_idc
= ctrl
->val
;
652 case V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING
:
653 ctx
->ctrls
.sei_fp
= ctrl
->val
;
655 case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE
:
656 ctx
->ctrls
.sei_fp_type
= ctrl
->val
;
659 dev_dbg(dev
, "%s S_CTRL: invalid control (id = %d)\n",
660 ctx
->name
, ctrl
->id
);
667 /* V4L2 control ops */
668 static const struct v4l2_ctrl_ops hva_ctrl_ops
= {
669 .s_ctrl
= hva_s_ctrl
,
672 static int hva_ctrls_setup(struct hva_ctx
*ctx
)
674 struct device
*dev
= ctx_to_dev(ctx
);
676 enum v4l2_mpeg_video_h264_sei_fp_arrangement_type sei_fp_type
=
677 V4L2_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE_TOP_BOTTOM
;
679 v4l2_ctrl_handler_init(&ctx
->ctrl_handler
, 15);
681 v4l2_ctrl_new_std_menu(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
682 V4L2_CID_MPEG_VIDEO_BITRATE_MODE
,
683 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
,
685 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
);
687 v4l2_ctrl_new_std(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
688 V4L2_CID_MPEG_VIDEO_GOP_SIZE
,
691 v4l2_ctrl_new_std(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
692 V4L2_CID_MPEG_VIDEO_BITRATE
,
693 1000, 60000000, 1000, 20000000);
695 mask
= ~(1 << V4L2_MPEG_VIDEO_ASPECT_1x1
);
696 v4l2_ctrl_new_std_menu(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
697 V4L2_CID_MPEG_VIDEO_ASPECT
,
698 V4L2_MPEG_VIDEO_ASPECT_1x1
,
700 V4L2_MPEG_VIDEO_ASPECT_1x1
);
702 mask
= ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
) |
703 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN
) |
704 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH
) |
705 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH
));
706 v4l2_ctrl_new_std_menu(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
707 V4L2_CID_MPEG_VIDEO_H264_PROFILE
,
708 V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH
,
710 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH
);
712 v4l2_ctrl_new_std_menu(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
713 V4L2_CID_MPEG_VIDEO_H264_LEVEL
,
714 V4L2_MPEG_VIDEO_H264_LEVEL_4_2
,
716 V4L2_MPEG_VIDEO_H264_LEVEL_4_0
);
718 v4l2_ctrl_new_std_menu(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
719 V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE
,
720 V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC
,
722 V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC
);
724 v4l2_ctrl_new_std(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
725 V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE
,
728 v4l2_ctrl_new_std(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
729 V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM
,
732 v4l2_ctrl_new_std(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
733 V4L2_CID_MPEG_VIDEO_H264_MIN_QP
,
736 v4l2_ctrl_new_std(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
737 V4L2_CID_MPEG_VIDEO_H264_MAX_QP
,
740 v4l2_ctrl_new_std(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
741 V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE
,
744 mask
= ~(1 << V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1
);
745 v4l2_ctrl_new_std_menu(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
746 V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC
,
747 V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1
,
749 V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1
);
751 v4l2_ctrl_new_std(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
752 V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING
,
755 mask
= ~(1 << sei_fp_type
);
756 v4l2_ctrl_new_std_menu(&ctx
->ctrl_handler
, &hva_ctrl_ops
,
757 V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE
,
762 if (ctx
->ctrl_handler
.error
) {
763 int err
= ctx
->ctrl_handler
.error
;
765 dev_dbg(dev
, "%s controls setup failed (%d)\n",
767 v4l2_ctrl_handler_free(&ctx
->ctrl_handler
);
771 v4l2_ctrl_handler_setup(&ctx
->ctrl_handler
);
773 /* set default time per frame */
774 ctx
->ctrls
.time_per_frame
.numerator
= HVA_DEFAULT_FRAME_NUM
;
775 ctx
->ctrls
.time_per_frame
.denominator
= HVA_DEFAULT_FRAME_DEN
;
781 * mem-to-mem operations
784 static void hva_run_work(struct work_struct
*work
)
786 struct hva_ctx
*ctx
= container_of(work
, struct hva_ctx
, run_work
);
787 struct vb2_v4l2_buffer
*src_buf
, *dst_buf
;
788 const struct hva_enc
*enc
= ctx
->enc
;
789 struct hva_frame
*frame
;
790 struct hva_stream
*stream
;
793 /* protect instance against reentrancy */
794 mutex_lock(&ctx
->lock
);
796 src_buf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
);
797 dst_buf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
);
799 frame
= to_hva_frame(src_buf
);
800 stream
= to_hva_stream(dst_buf
);
801 frame
->vbuf
.sequence
= ctx
->frame_num
++;
803 ret
= enc
->encode(ctx
, frame
, stream
);
805 vb2_set_plane_payload(&dst_buf
->vb2_buf
, 0, stream
->bytesused
);
807 v4l2_m2m_buf_done(src_buf
, VB2_BUF_STATE_ERROR
);
808 v4l2_m2m_buf_done(dst_buf
, VB2_BUF_STATE_ERROR
);
810 /* propagate frame timestamp */
811 dst_buf
->vb2_buf
.timestamp
= src_buf
->vb2_buf
.timestamp
;
812 dst_buf
->field
= V4L2_FIELD_NONE
;
813 dst_buf
->sequence
= ctx
->stream_num
- 1;
815 v4l2_m2m_buf_done(src_buf
, VB2_BUF_STATE_DONE
);
816 v4l2_m2m_buf_done(dst_buf
, VB2_BUF_STATE_DONE
);
819 mutex_unlock(&ctx
->lock
);
821 v4l2_m2m_job_finish(ctx
->hva_dev
->m2m_dev
, ctx
->fh
.m2m_ctx
);
824 static void hva_device_run(void *priv
)
826 struct hva_ctx
*ctx
= priv
;
827 struct hva_dev
*hva
= ctx_to_hdev(ctx
);
829 queue_work(hva
->work_queue
, &ctx
->run_work
);
832 static void hva_job_abort(void *priv
)
834 struct hva_ctx
*ctx
= priv
;
835 struct device
*dev
= ctx_to_dev(ctx
);
837 dev_dbg(dev
, "%s aborting job\n", ctx
->name
);
839 ctx
->aborting
= true;
842 static int hva_job_ready(void *priv
)
844 struct hva_ctx
*ctx
= priv
;
845 struct device
*dev
= ctx_to_dev(ctx
);
847 if (!v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
)) {
848 dev_dbg(dev
, "%s job not ready: no frame buffers\n",
853 if (!v4l2_m2m_num_dst_bufs_ready(ctx
->fh
.m2m_ctx
)) {
854 dev_dbg(dev
, "%s job not ready: no stream buffers\n",
860 dev_dbg(dev
, "%s job not ready: aborting\n", ctx
->name
);
868 static const struct v4l2_m2m_ops hva_m2m_ops
= {
869 .device_run
= hva_device_run
,
870 .job_abort
= hva_job_abort
,
871 .job_ready
= hva_job_ready
,
875 * VB2 queue operations
878 static int hva_queue_setup(struct vb2_queue
*vq
,
879 unsigned int *num_buffers
, unsigned int *num_planes
,
880 unsigned int sizes
[], struct device
*alloc_devs
[])
882 struct hva_ctx
*ctx
= vb2_get_drv_priv(vq
);
883 struct device
*dev
= ctx_to_dev(ctx
);
886 dev_dbg(dev
, "%s %s queue setup: num_buffers %d\n", ctx
->name
,
887 to_type_str(vq
->type
), *num_buffers
);
889 size
= vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
?
890 ctx
->frameinfo
.size
: ctx
->max_stream_size
;
893 return sizes
[0] < size
? -EINVAL
: 0;
895 /* only one plane supported */
902 static int hva_buf_prepare(struct vb2_buffer
*vb
)
904 struct hva_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
905 struct device
*dev
= ctx_to_dev(ctx
);
906 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
908 if (vb
->vb2_queue
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
909 struct hva_frame
*frame
= to_hva_frame(vbuf
);
911 if (vbuf
->field
== V4L2_FIELD_ANY
)
912 vbuf
->field
= V4L2_FIELD_NONE
;
913 if (vbuf
->field
!= V4L2_FIELD_NONE
) {
915 "%s frame[%d] prepare: %d field not supported\n",
916 ctx
->name
, vb
->index
, vbuf
->field
);
920 if (!frame
->prepared
) {
921 /* get memory addresses */
922 frame
->vaddr
= vb2_plane_vaddr(&vbuf
->vb2_buf
, 0);
923 frame
->paddr
= vb2_dma_contig_plane_dma_addr(
925 frame
->info
= ctx
->frameinfo
;
926 frame
->prepared
= true;
929 "%s frame[%d] prepared; virt=%p, phy=%pad\n",
930 ctx
->name
, vb
->index
,
931 frame
->vaddr
, &frame
->paddr
);
934 struct hva_stream
*stream
= to_hva_stream(vbuf
);
936 if (!stream
->prepared
) {
937 /* get memory addresses */
938 stream
->vaddr
= vb2_plane_vaddr(&vbuf
->vb2_buf
, 0);
939 stream
->paddr
= vb2_dma_contig_plane_dma_addr(
941 stream
->size
= vb2_plane_size(&vbuf
->vb2_buf
, 0);
942 stream
->prepared
= true;
945 "%s stream[%d] prepared; virt=%p, phy=%pad\n",
946 ctx
->name
, vb
->index
,
947 stream
->vaddr
, &stream
->paddr
);
954 static void hva_buf_queue(struct vb2_buffer
*vb
)
956 struct hva_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
957 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
960 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vbuf
);
963 static int hva_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
965 struct hva_ctx
*ctx
= vb2_get_drv_priv(vq
);
966 struct hva_dev
*hva
= ctx_to_hdev(ctx
);
967 struct device
*dev
= ctx_to_dev(ctx
);
968 struct vb2_v4l2_buffer
*vbuf
;
973 dev_dbg(dev
, "%s %s start streaming\n", ctx
->name
,
974 to_type_str(vq
->type
));
976 /* open encoder when both start_streaming have been called */
977 if (V4L2_TYPE_IS_OUTPUT(vq
->type
)) {
978 if (!vb2_start_streaming_called(&ctx
->fh
.m2m_ctx
->cap_q_ctx
.q
))
981 if (!vb2_start_streaming_called(&ctx
->fh
.m2m_ctx
->out_q_ctx
.q
))
985 /* store the instance context in the instances array */
986 for (i
= 0; i
< HVA_MAX_INSTANCES
; i
++) {
987 if (!hva
->instances
[i
]) {
988 hva
->instances
[i
] = ctx
;
989 /* save the context identifier in the context */
997 dev_err(dev
, "%s maximum instances reached\n", ctx
->name
);
1002 hva
->nb_of_instances
++;
1005 ret
= hva_open_encoder(ctx
,
1006 ctx
->streaminfo
.streamformat
,
1007 ctx
->frameinfo
.pixelformat
,
1016 hva
->instances
[ctx
->id
] = NULL
;
1017 hva
->nb_of_instances
--;
1019 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1020 /* return of all pending buffers to vb2 (in queued state) */
1021 while ((vbuf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
)))
1022 v4l2_m2m_buf_done(vbuf
, VB2_BUF_STATE_QUEUED
);
1024 /* return of all pending buffers to vb2 (in queued state) */
1025 while ((vbuf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
)))
1026 v4l2_m2m_buf_done(vbuf
, VB2_BUF_STATE_QUEUED
);
1032 static void hva_stop_streaming(struct vb2_queue
*vq
)
1034 struct hva_ctx
*ctx
= vb2_get_drv_priv(vq
);
1035 struct hva_dev
*hva
= ctx_to_hdev(ctx
);
1036 struct device
*dev
= ctx_to_dev(ctx
);
1037 const struct hva_enc
*enc
= ctx
->enc
;
1038 struct vb2_v4l2_buffer
*vbuf
;
1040 dev_dbg(dev
, "%s %s stop streaming\n", ctx
->name
,
1041 to_type_str(vq
->type
));
1043 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1044 /* return of all pending buffers to vb2 (in error state) */
1046 while ((vbuf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
)))
1047 v4l2_m2m_buf_done(vbuf
, VB2_BUF_STATE_ERROR
);
1049 /* return of all pending buffers to vb2 (in error state) */
1050 ctx
->stream_num
= 0;
1051 while ((vbuf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
)))
1052 v4l2_m2m_buf_done(vbuf
, VB2_BUF_STATE_ERROR
);
1055 if ((V4L2_TYPE_IS_OUTPUT(vq
->type
) &&
1056 vb2_is_streaming(&ctx
->fh
.m2m_ctx
->cap_q_ctx
.q
)) ||
1057 (!V4L2_TYPE_IS_OUTPUT(vq
->type
) &&
1058 vb2_is_streaming(&ctx
->fh
.m2m_ctx
->out_q_ctx
.q
))) {
1059 dev_dbg(dev
, "%s %s out=%d cap=%d\n",
1060 ctx
->name
, to_type_str(vq
->type
),
1061 vb2_is_streaming(&ctx
->fh
.m2m_ctx
->out_q_ctx
.q
),
1062 vb2_is_streaming(&ctx
->fh
.m2m_ctx
->cap_q_ctx
.q
));
1066 /* close encoder when both stop_streaming have been called */
1068 dev_dbg(dev
, "%s %s encoder closed\n", ctx
->name
, enc
->name
);
1072 /* clear instance context in instances array */
1073 hva
->instances
[ctx
->id
] = NULL
;
1074 hva
->nb_of_instances
--;
1077 ctx
->aborting
= false;
1081 static const struct vb2_ops hva_qops
= {
1082 .queue_setup
= hva_queue_setup
,
1083 .buf_prepare
= hva_buf_prepare
,
1084 .buf_queue
= hva_buf_queue
,
1085 .start_streaming
= hva_start_streaming
,
1086 .stop_streaming
= hva_stop_streaming
,
1087 .wait_prepare
= vb2_ops_wait_prepare
,
1088 .wait_finish
= vb2_ops_wait_finish
,
1092 * V4L2 file operations
1095 static int queue_init(struct hva_ctx
*ctx
, struct vb2_queue
*vq
)
1097 vq
->io_modes
= VB2_MMAP
| VB2_DMABUF
;
1099 vq
->ops
= &hva_qops
;
1100 vq
->mem_ops
= &vb2_dma_contig_memops
;
1101 vq
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
1102 vq
->lock
= &ctx
->hva_dev
->lock
;
1104 return vb2_queue_init(vq
);
1107 static int hva_queue_init(void *priv
, struct vb2_queue
*src_vq
,
1108 struct vb2_queue
*dst_vq
)
1110 struct hva_ctx
*ctx
= priv
;
1113 src_vq
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1114 src_vq
->buf_struct_size
= sizeof(struct hva_frame
);
1115 src_vq
->min_buffers_needed
= MIN_FRAMES
;
1116 src_vq
->dev
= ctx
->hva_dev
->dev
;
1118 ret
= queue_init(ctx
, src_vq
);
1122 dst_vq
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1123 dst_vq
->buf_struct_size
= sizeof(struct hva_stream
);
1124 dst_vq
->min_buffers_needed
= MIN_STREAMS
;
1125 dst_vq
->dev
= ctx
->hva_dev
->dev
;
1127 return queue_init(ctx
, dst_vq
);
1130 static int hva_open(struct file
*file
)
1132 struct hva_dev
*hva
= video_drvdata(file
);
1133 struct device
*dev
= hva_to_dev(hva
);
1134 struct hva_ctx
*ctx
;
1137 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
1144 INIT_WORK(&ctx
->run_work
, hva_run_work
);
1145 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
1146 file
->private_data
= &ctx
->fh
;
1147 v4l2_fh_add(&ctx
->fh
);
1149 ret
= hva_ctrls_setup(ctx
);
1151 dev_err(dev
, "%s [x:x] failed to setup controls\n",
1155 ctx
->fh
.ctrl_handler
= &ctx
->ctrl_handler
;
1157 mutex_init(&ctx
->lock
);
1159 ctx
->fh
.m2m_ctx
= v4l2_m2m_ctx_init(hva
->m2m_dev
, ctx
,
1161 if (IS_ERR(ctx
->fh
.m2m_ctx
)) {
1162 ret
= PTR_ERR(ctx
->fh
.m2m_ctx
);
1163 dev_err(dev
, "%s failed to initialize m2m context (%d)\n",
1168 /* set the instance name */
1169 mutex_lock(&hva
->lock
);
1171 snprintf(ctx
->name
, sizeof(ctx
->name
), "[%3d:----]",
1173 mutex_unlock(&hva
->lock
);
1175 /* default parameters for frame and stream */
1176 set_default_params(ctx
);
1178 dev_info(dev
, "%s encoder instance created\n", ctx
->name
);
1183 v4l2_ctrl_handler_free(&ctx
->ctrl_handler
);
1185 v4l2_fh_del(&ctx
->fh
);
1186 v4l2_fh_exit(&ctx
->fh
);
1192 static int hva_release(struct file
*file
)
1194 struct hva_ctx
*ctx
= fh_to_ctx(file
->private_data
);
1195 struct hva_dev
*hva
= ctx_to_hdev(ctx
);
1196 struct device
*dev
= ctx_to_dev(ctx
);
1197 const struct hva_enc
*enc
= ctx
->enc
;
1200 dev_dbg(dev
, "%s %s encoder closed\n", ctx
->name
, enc
->name
);
1204 /* clear instance context in instances array */
1205 hva
->instances
[ctx
->id
] = NULL
;
1206 hva
->nb_of_instances
--;
1209 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
1211 v4l2_ctrl_handler_free(&ctx
->ctrl_handler
);
1213 v4l2_fh_del(&ctx
->fh
);
1214 v4l2_fh_exit(&ctx
->fh
);
1216 dev_info(dev
, "%s encoder instance released\n", ctx
->name
);
1224 static const struct v4l2_file_operations hva_fops
= {
1225 .owner
= THIS_MODULE
,
1227 .release
= hva_release
,
1228 .unlocked_ioctl
= video_ioctl2
,
1229 .mmap
= v4l2_m2m_fop_mmap
,
1230 .poll
= v4l2_m2m_fop_poll
,
1234 * Platform device operations
1237 static int hva_register_device(struct hva_dev
*hva
)
1240 struct video_device
*vdev
;
1245 dev
= hva_to_dev(hva
);
1247 hva
->m2m_dev
= v4l2_m2m_init(&hva_m2m_ops
);
1248 if (IS_ERR(hva
->m2m_dev
)) {
1249 dev_err(dev
, "%s failed to initialize v4l2-m2m device\n",
1251 ret
= PTR_ERR(hva
->m2m_dev
);
1255 vdev
= video_device_alloc();
1257 dev_err(dev
, "%s failed to allocate video device\n",
1260 goto err_m2m_release
;
1263 vdev
->fops
= &hva_fops
;
1264 vdev
->ioctl_ops
= &hva_ioctl_ops
;
1265 vdev
->release
= video_device_release
;
1266 vdev
->lock
= &hva
->lock
;
1267 vdev
->vfl_dir
= VFL_DIR_M2M
;
1268 vdev
->device_caps
= V4L2_CAP_STREAMING
| V4L2_CAP_VIDEO_M2M
;
1269 vdev
->v4l2_dev
= &hva
->v4l2_dev
;
1270 snprintf(vdev
->name
, sizeof(vdev
->name
), "%s%lx", HVA_NAME
,
1273 ret
= video_register_device(vdev
, VFL_TYPE_GRABBER
, -1);
1275 dev_err(dev
, "%s failed to register video device\n",
1277 goto err_vdev_release
;
1281 video_set_drvdata(vdev
, hva
);
1285 video_device_release(vdev
);
1287 v4l2_m2m_release(hva
->m2m_dev
);
1292 static void hva_unregister_device(struct hva_dev
*hva
)
1298 v4l2_m2m_release(hva
->m2m_dev
);
1300 video_unregister_device(hva
->vdev
);
1303 static int hva_probe(struct platform_device
*pdev
)
1305 struct hva_dev
*hva
;
1306 struct device
*dev
= &pdev
->dev
;
1309 hva
= devm_kzalloc(dev
, sizeof(*hva
), GFP_KERNEL
);
1317 platform_set_drvdata(pdev
, hva
);
1319 mutex_init(&hva
->lock
);
1321 /* probe hardware */
1322 ret
= hva_hw_probe(pdev
, hva
);
1326 /* register all available encoders */
1327 register_encoders(hva
);
1329 /* register all supported formats */
1330 register_formats(hva
);
1332 /* register on V4L2 */
1333 ret
= v4l2_device_register(dev
, &hva
->v4l2_dev
);
1335 dev_err(dev
, "%s %s failed to register V4L2 device\n",
1336 HVA_PREFIX
, HVA_NAME
);
1340 hva
->work_queue
= create_workqueue(HVA_NAME
);
1341 if (!hva
->work_queue
) {
1342 dev_err(dev
, "%s %s failed to allocate work queue\n",
1343 HVA_PREFIX
, HVA_NAME
);
1348 /* register device */
1349 ret
= hva_register_device(hva
);
1351 goto err_work_queue
;
1353 dev_info(dev
, "%s %s registered as /dev/video%d\n", HVA_PREFIX
,
1354 HVA_NAME
, hva
->vdev
->num
);
1359 destroy_workqueue(hva
->work_queue
);
1361 v4l2_device_unregister(&hva
->v4l2_dev
);
1368 static int hva_remove(struct platform_device
*pdev
)
1370 struct hva_dev
*hva
= platform_get_drvdata(pdev
);
1371 struct device
*dev
= hva_to_dev(hva
);
1373 hva_unregister_device(hva
);
1375 destroy_workqueue(hva
->work_queue
);
1379 v4l2_device_unregister(&hva
->v4l2_dev
);
1381 dev_info(dev
, "%s %s removed\n", HVA_PREFIX
, pdev
->name
);
1387 static const struct dev_pm_ops hva_pm_ops
= {
1388 .runtime_suspend
= hva_hw_runtime_suspend
,
1389 .runtime_resume
= hva_hw_runtime_resume
,
1392 static const struct of_device_id hva_match_types
[] = {
1394 .compatible
= "st,st-hva",
1399 MODULE_DEVICE_TABLE(of
, hva_match_types
);
1401 static struct platform_driver hva_driver
= {
1403 .remove
= hva_remove
,
1406 .of_match_table
= hva_match_types
,
1411 module_platform_driver(hva_driver
);
1413 MODULE_LICENSE("GPL");
1414 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
1415 MODULE_DESCRIPTION("STMicroelectronics HVA video encoder V4L2 driver");