1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics SA 2015
4 * Authors: Hugues Fruchet <hugues.fruchet@st.com>
5 * Jean-Christophe Trotin <jean-christophe.trotin@st.com>
6 * for STMicroelectronics.
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/slab.h>
15 #include <media/v4l2-ioctl.h>
16 #include <media/v4l2-event.h>
17 #include <media/videobuf2-dma-contig.h>
20 #include "delta-debug.h"
21 #include "delta-ipc.h"
23 #define DELTA_NAME "st-delta"
25 #define DELTA_PREFIX "[---:----]"
27 #define to_ctx(__fh) container_of(__fh, struct delta_ctx, fh)
28 #define to_au(__vbuf) container_of(__vbuf, struct delta_au, vbuf)
29 #define to_frame(__vbuf) container_of(__vbuf, struct delta_frame, vbuf)
31 #define call_dec_op(dec, op, args...)\
32 ((dec && (dec)->op) ? (dec)->op(args) : 0)
34 /* registry of available decoders */
35 static const struct delta_dec
*delta_decoders
[] = {
36 #ifdef CONFIG_VIDEO_STI_DELTA_MJPEG
41 static inline int frame_size(u32 w
, u32 h
, u32 fmt
)
44 case V4L2_PIX_FMT_NV12
:
45 return (w
* h
* 3) / 2;
51 static inline int frame_stride(u32 w
, u32 fmt
)
54 case V4L2_PIX_FMT_NV12
:
61 static void dump_au(struct delta_ctx
*ctx
, struct delta_au
*au
)
63 struct delta_dev
*delta
= ctx
->dev
;
64 u32 size
= 10; /* dump first & last 10 bytes */
65 u8
*data
= (u8
*)(au
->vaddr
);
67 if (au
->size
<= (size
* 2))
68 dev_dbg(delta
->dev
, "%s dump au[%d] dts=%lld size=%d data=%*ph\n",
69 ctx
->name
, au
->vbuf
.vb2_buf
.index
, au
->dts
, au
->size
,
72 dev_dbg(delta
->dev
, "%s dump au[%d] dts=%lld size=%d data=%*ph..%*ph\n",
73 ctx
->name
, au
->vbuf
.vb2_buf
.index
, au
->dts
, au
->size
,
74 size
, data
, size
, data
+ au
->size
- size
);
77 static void dump_frame(struct delta_ctx
*ctx
, struct delta_frame
*frame
)
79 struct delta_dev
*delta
= ctx
->dev
;
80 u32 size
= 10; /* dump first 10 bytes */
81 u8
*data
= (u8
*)(frame
->vaddr
);
83 dev_dbg(delta
->dev
, "%s dump frame[%d] dts=%lld type=%s field=%s data=%*ph\n",
84 ctx
->name
, frame
->index
, frame
->dts
,
85 frame_type_str(frame
->flags
),
86 frame_field_str(frame
->field
),
90 static void delta_au_done(struct delta_ctx
*ctx
, struct delta_au
*au
, int err
)
92 struct vb2_v4l2_buffer
*vbuf
;
95 vbuf
->sequence
= ctx
->au_num
++;
96 v4l2_m2m_buf_done(vbuf
, err
? VB2_BUF_STATE_ERROR
: VB2_BUF_STATE_DONE
);
99 static void delta_frame_done(struct delta_ctx
*ctx
, struct delta_frame
*frame
,
102 struct vb2_v4l2_buffer
*vbuf
;
104 dump_frame(ctx
, frame
);
106 /* decoded frame is now output to user */
107 frame
->state
|= DELTA_FRAME_OUT
;
110 vbuf
->sequence
= ctx
->frame_num
++;
111 v4l2_m2m_buf_done(vbuf
, err
? VB2_BUF_STATE_ERROR
: VB2_BUF_STATE_DONE
);
113 if (frame
->info
.size
) /* ignore EOS */
114 ctx
->output_frames
++;
117 static void requeue_free_frames(struct delta_ctx
*ctx
)
119 struct vb2_v4l2_buffer
*vbuf
;
120 struct delta_frame
*frame
;
123 /* requeue all free frames */
124 for (i
= 0; i
< ctx
->nb_of_frames
; i
++) {
125 frame
= ctx
->frames
[i
];
126 if (frame
->state
== DELTA_FRAME_FREE
) {
128 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vbuf
);
129 frame
->state
= DELTA_FRAME_M2M
;
134 static int delta_recycle(struct delta_ctx
*ctx
, struct delta_frame
*frame
)
136 const struct delta_dec
*dec
= ctx
->dec
;
138 /* recycle frame on decoder side */
139 call_dec_op(dec
, recycle
, ctx
, frame
);
141 /* this frame is no more output */
142 frame
->state
&= ~DELTA_FRAME_OUT
;
144 /* requeue free frame */
145 if (frame
->state
== DELTA_FRAME_FREE
) {
146 struct vb2_v4l2_buffer
*vbuf
= &frame
->vbuf
;
148 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vbuf
);
149 frame
->state
= DELTA_FRAME_M2M
;
152 /* reset other frame fields */
159 static void delta_push_dts(struct delta_ctx
*ctx
, u64 val
)
161 struct delta_dts
*dts
;
163 dts
= kzalloc(sizeof(*dts
), GFP_KERNEL
);
167 INIT_LIST_HEAD(&dts
->list
);
170 * protected by global lock acquired
171 * by V4L2 when calling delta_vb2_au_queue
174 list_add_tail(&dts
->list
, &ctx
->dts
);
177 static void delta_pop_dts(struct delta_ctx
*ctx
, u64
*val
)
179 struct delta_dev
*delta
= ctx
->dev
;
180 struct delta_dts
*dts
;
183 * protected by global lock acquired
184 * by V4L2 when calling delta_vb2_au_queue
186 if (list_empty(&ctx
->dts
)) {
187 dev_warn(delta
->dev
, "%s no dts to pop ... output dts = 0\n",
193 dts
= list_first_entry(&ctx
->dts
, struct delta_dts
, list
);
194 list_del(&dts
->list
);
201 static void delta_flush_dts(struct delta_ctx
*ctx
)
203 struct delta_dts
*dts
;
204 struct delta_dts
*next
;
207 * protected by global lock acquired
208 * by V4L2 when calling delta_vb2_au_queue
211 /* free all pending dts */
212 list_for_each_entry_safe(dts
, next
, &ctx
->dts
, list
)
216 INIT_LIST_HEAD(&ctx
->dts
);
219 static inline int frame_alignment(u32 fmt
)
222 case V4L2_PIX_FMT_NV12
:
223 case V4L2_PIX_FMT_NV21
:
231 static inline int estimated_au_size(u32 w
, u32 h
)
234 * for a MJPEG stream encoded from YUV422 pixel format,
235 * assuming a compression ratio of 2, the maximum size
236 * of an access unit is (width x height x 2) / 2,
237 * so (width x height)
242 static void set_default_params(struct delta_ctx
*ctx
)
244 struct delta_frameinfo
*frameinfo
= &ctx
->frameinfo
;
245 struct delta_streaminfo
*streaminfo
= &ctx
->streaminfo
;
247 memset(frameinfo
, 0, sizeof(*frameinfo
));
248 frameinfo
->pixelformat
= V4L2_PIX_FMT_NV12
;
249 frameinfo
->width
= DELTA_DEFAULT_WIDTH
;
250 frameinfo
->height
= DELTA_DEFAULT_HEIGHT
;
251 frameinfo
->aligned_width
= ALIGN(frameinfo
->width
,
252 DELTA_WIDTH_ALIGNMENT
);
253 frameinfo
->aligned_height
= ALIGN(frameinfo
->height
,
254 DELTA_HEIGHT_ALIGNMENT
);
255 frameinfo
->size
= frame_size(frameinfo
->aligned_width
,
256 frameinfo
->aligned_height
,
257 frameinfo
->pixelformat
);
258 frameinfo
->field
= V4L2_FIELD_NONE
;
259 frameinfo
->colorspace
= V4L2_COLORSPACE_REC709
;
260 frameinfo
->xfer_func
= V4L2_XFER_FUNC_DEFAULT
;
261 frameinfo
->ycbcr_enc
= V4L2_YCBCR_ENC_DEFAULT
;
262 frameinfo
->quantization
= V4L2_QUANTIZATION_DEFAULT
;
264 memset(streaminfo
, 0, sizeof(*streaminfo
));
265 streaminfo
->streamformat
= DELTA_DEFAULT_STREAMFORMAT
;
266 streaminfo
->width
= DELTA_DEFAULT_WIDTH
;
267 streaminfo
->height
= DELTA_DEFAULT_HEIGHT
;
268 streaminfo
->field
= V4L2_FIELD_NONE
;
269 streaminfo
->colorspace
= V4L2_COLORSPACE_REC709
;
270 streaminfo
->xfer_func
= V4L2_XFER_FUNC_DEFAULT
;
271 streaminfo
->ycbcr_enc
= V4L2_YCBCR_ENC_DEFAULT
;
272 streaminfo
->quantization
= V4L2_QUANTIZATION_DEFAULT
;
274 ctx
->max_au_size
= estimated_au_size(streaminfo
->width
,
278 static const struct delta_dec
*delta_find_decoder(struct delta_ctx
*ctx
,
282 struct delta_dev
*delta
= ctx
->dev
;
283 const struct delta_dec
*dec
;
286 for (i
= 0; i
< delta
->nb_of_decoders
; i
++) {
287 dec
= delta
->decoders
[i
];
288 if ((dec
->pixelformat
== pixelformat
) &&
289 (dec
->streamformat
== streamformat
))
296 static void register_format(u32 format
, u32 formats
[], u32
*nb_of_formats
)
300 for (i
= 0; i
< *nb_of_formats
; i
++) {
301 if (format
== formats
[i
])
305 formats
[(*nb_of_formats
)++] = format
;
308 static void register_formats(struct delta_dev
*delta
)
312 for (i
= 0; i
< delta
->nb_of_decoders
; i
++) {
313 register_format(delta
->decoders
[i
]->pixelformat
,
315 &delta
->nb_of_pixelformats
);
317 register_format(delta
->decoders
[i
]->streamformat
,
318 delta
->streamformats
,
319 &delta
->nb_of_streamformats
);
323 static void register_decoders(struct delta_dev
*delta
)
327 for (i
= 0; i
< ARRAY_SIZE(delta_decoders
); i
++) {
328 if (delta
->nb_of_decoders
>= DELTA_MAX_DECODERS
) {
330 "%s failed to register %s decoder (%d maximum reached)\n",
331 DELTA_PREFIX
, delta_decoders
[i
]->name
,
336 delta
->decoders
[delta
->nb_of_decoders
++] = delta_decoders
[i
];
337 dev_info(delta
->dev
, "%s %s decoder registered\n",
338 DELTA_PREFIX
, delta_decoders
[i
]->name
);
342 static void delta_lock(void *priv
)
344 struct delta_ctx
*ctx
= priv
;
345 struct delta_dev
*delta
= ctx
->dev
;
347 mutex_lock(&delta
->lock
);
350 static void delta_unlock(void *priv
)
352 struct delta_ctx
*ctx
= priv
;
353 struct delta_dev
*delta
= ctx
->dev
;
355 mutex_unlock(&delta
->lock
);
358 static int delta_open_decoder(struct delta_ctx
*ctx
, u32 streamformat
,
359 u32 pixelformat
, const struct delta_dec
**pdec
)
361 struct delta_dev
*delta
= ctx
->dev
;
362 const struct delta_dec
*dec
;
365 dec
= delta_find_decoder(ctx
, streamformat
, ctx
->frameinfo
.pixelformat
);
367 dev_err(delta
->dev
, "%s no decoder found matching %4.4s => %4.4s\n",
368 ctx
->name
, (char *)&streamformat
, (char *)&pixelformat
);
372 dev_dbg(delta
->dev
, "%s one decoder matching %4.4s => %4.4s\n",
373 ctx
->name
, (char *)&streamformat
, (char *)&pixelformat
);
375 /* update instance name */
376 snprintf(ctx
->name
, sizeof(ctx
->name
), "[%3d:%4.4s]",
377 delta
->instance_id
, (char *)&streamformat
);
379 /* open decoder instance */
380 ret
= call_dec_op(dec
, open
, ctx
);
382 dev_err(delta
->dev
, "%s failed to open decoder instance (%d)\n",
387 dev_dbg(delta
->dev
, "%s %s decoder opened\n", ctx
->name
, dec
->name
);
395 * V4L2 ioctl operations
398 static int delta_querycap(struct file
*file
, void *priv
,
399 struct v4l2_capability
*cap
)
401 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
402 struct delta_dev
*delta
= ctx
->dev
;
404 strlcpy(cap
->driver
, DELTA_NAME
, sizeof(cap
->driver
));
405 strlcpy(cap
->card
, delta
->vdev
->name
, sizeof(cap
->card
));
406 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "platform:%s",
412 static int delta_enum_fmt_stream(struct file
*file
, void *priv
,
413 struct v4l2_fmtdesc
*f
)
415 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
416 struct delta_dev
*delta
= ctx
->dev
;
418 if (unlikely(f
->index
>= delta
->nb_of_streamformats
))
421 f
->pixelformat
= delta
->streamformats
[f
->index
];
426 static int delta_enum_fmt_frame(struct file
*file
, void *priv
,
427 struct v4l2_fmtdesc
*f
)
429 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
430 struct delta_dev
*delta
= ctx
->dev
;
432 if (unlikely(f
->index
>= delta
->nb_of_pixelformats
))
435 f
->pixelformat
= delta
->pixelformats
[f
->index
];
440 static int delta_g_fmt_stream(struct file
*file
, void *fh
,
441 struct v4l2_format
*f
)
443 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
444 struct delta_dev
*delta
= ctx
->dev
;
445 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
446 struct delta_streaminfo
*streaminfo
= &ctx
->streaminfo
;
447 unsigned char str
[100] = "";
449 if (!(ctx
->flags
& DELTA_FLAG_STREAMINFO
))
451 "%s V4L2 GET_FMT (OUTPUT): no stream information available, default to %s\n",
453 delta_streaminfo_str(streaminfo
, str
, sizeof(str
)));
455 pix
->pixelformat
= streaminfo
->streamformat
;
456 pix
->width
= streaminfo
->width
;
457 pix
->height
= streaminfo
->height
;
458 pix
->field
= streaminfo
->field
;
459 pix
->bytesperline
= 0;
460 pix
->sizeimage
= ctx
->max_au_size
;
461 pix
->colorspace
= streaminfo
->colorspace
;
462 pix
->xfer_func
= streaminfo
->xfer_func
;
463 pix
->ycbcr_enc
= streaminfo
->ycbcr_enc
;
464 pix
->quantization
= streaminfo
->quantization
;
469 static int delta_g_fmt_frame(struct file
*file
, void *fh
, struct v4l2_format
*f
)
471 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
472 struct delta_dev
*delta
= ctx
->dev
;
473 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
474 struct delta_frameinfo
*frameinfo
= &ctx
->frameinfo
;
475 struct delta_streaminfo
*streaminfo
= &ctx
->streaminfo
;
476 unsigned char str
[100] = "";
478 if (!(ctx
->flags
& DELTA_FLAG_FRAMEINFO
))
480 "%s V4L2 GET_FMT (CAPTURE): no frame information available, default to %s\n",
482 delta_frameinfo_str(frameinfo
, str
, sizeof(str
)));
484 pix
->pixelformat
= frameinfo
->pixelformat
;
485 pix
->width
= frameinfo
->aligned_width
;
486 pix
->height
= frameinfo
->aligned_height
;
487 pix
->field
= frameinfo
->field
;
488 pix
->bytesperline
= frame_stride(frameinfo
->aligned_width
,
489 frameinfo
->pixelformat
);
490 pix
->sizeimage
= frameinfo
->size
;
492 if (ctx
->flags
& DELTA_FLAG_STREAMINFO
) {
493 /* align colorspace & friends on stream ones if any set */
494 frameinfo
->colorspace
= streaminfo
->colorspace
;
495 frameinfo
->xfer_func
= streaminfo
->xfer_func
;
496 frameinfo
->ycbcr_enc
= streaminfo
->ycbcr_enc
;
497 frameinfo
->quantization
= streaminfo
->quantization
;
499 pix
->colorspace
= frameinfo
->colorspace
;
500 pix
->xfer_func
= frameinfo
->xfer_func
;
501 pix
->ycbcr_enc
= frameinfo
->ycbcr_enc
;
502 pix
->quantization
= frameinfo
->quantization
;
507 static int delta_try_fmt_stream(struct file
*file
, void *priv
,
508 struct v4l2_format
*f
)
510 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
511 struct delta_dev
*delta
= ctx
->dev
;
512 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
513 u32 streamformat
= pix
->pixelformat
;
514 const struct delta_dec
*dec
;
518 dec
= delta_find_decoder(ctx
, streamformat
, ctx
->frameinfo
.pixelformat
);
521 "%s V4L2 TRY_FMT (OUTPUT): unsupported format %4.4s\n",
522 ctx
->name
, (char *)&pix
->pixelformat
);
526 /* adjust width & height */
528 height
= pix
->height
;
529 v4l_bound_align_image
532 dec
->max_width
? dec
->max_width
: DELTA_MAX_WIDTH
,
536 dec
->max_height
? dec
->max_height
: DELTA_MAX_HEIGHT
,
539 if ((pix
->width
!= width
) || (pix
->height
!= height
))
541 "%s V4L2 TRY_FMT (OUTPUT): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
542 ctx
->name
, width
, height
,
543 pix
->width
, pix
->height
);
545 au_size
= estimated_au_size(pix
->width
, pix
->height
);
546 if (pix
->sizeimage
< au_size
) {
548 "%s V4L2 TRY_FMT (OUTPUT): size updated %d -> %d to fit estimated size\n",
549 ctx
->name
, pix
->sizeimage
, au_size
);
550 pix
->sizeimage
= au_size
;
553 pix
->bytesperline
= 0;
555 if (pix
->field
== V4L2_FIELD_ANY
)
556 pix
->field
= V4L2_FIELD_NONE
;
561 static int delta_try_fmt_frame(struct file
*file
, void *priv
,
562 struct v4l2_format
*f
)
564 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
565 struct delta_dev
*delta
= ctx
->dev
;
566 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
567 u32 pixelformat
= pix
->pixelformat
;
568 const struct delta_dec
*dec
;
571 dec
= delta_find_decoder(ctx
, ctx
->streaminfo
.streamformat
,
575 "%s V4L2 TRY_FMT (CAPTURE): unsupported format %4.4s\n",
576 ctx
->name
, (char *)&pixelformat
);
580 /* adjust width & height */
582 height
= pix
->height
;
583 v4l_bound_align_image(&pix
->width
,
584 DELTA_MIN_WIDTH
, DELTA_MAX_WIDTH
,
585 frame_alignment(pixelformat
) - 1,
587 DELTA_MIN_HEIGHT
, DELTA_MAX_HEIGHT
,
588 frame_alignment(pixelformat
) - 1, 0);
590 if ((pix
->width
!= width
) || (pix
->height
!= height
))
592 "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
593 ctx
->name
, width
, height
, pix
->width
, pix
->height
);
595 /* default decoder alignment constraint */
596 width
= ALIGN(pix
->width
, DELTA_WIDTH_ALIGNMENT
);
597 height
= ALIGN(pix
->height
, DELTA_HEIGHT_ALIGNMENT
);
598 if ((pix
->width
!= width
) || (pix
->height
!= height
))
600 "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit decoder alignment\n",
601 ctx
->name
, width
, height
, pix
->width
, pix
->height
);
603 if (!pix
->colorspace
) {
604 pix
->colorspace
= V4L2_COLORSPACE_REC709
;
605 pix
->xfer_func
= V4L2_XFER_FUNC_DEFAULT
;
606 pix
->ycbcr_enc
= V4L2_YCBCR_ENC_DEFAULT
;
607 pix
->quantization
= V4L2_QUANTIZATION_DEFAULT
;
611 pix
->height
= height
;
612 pix
->bytesperline
= frame_stride(pix
->width
, pixelformat
);
613 pix
->sizeimage
= frame_size(pix
->width
, pix
->height
, pixelformat
);
615 if (pix
->field
== V4L2_FIELD_ANY
)
616 pix
->field
= V4L2_FIELD_NONE
;
621 static int delta_s_fmt_stream(struct file
*file
, void *fh
,
622 struct v4l2_format
*f
)
624 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
625 struct delta_dev
*delta
= ctx
->dev
;
626 struct vb2_queue
*vq
;
627 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
630 ret
= delta_try_fmt_stream(file
, fh
, f
);
633 "%s V4L2 S_FMT (OUTPUT): unsupported format %4.4s\n",
634 ctx
->name
, (char *)&pix
->pixelformat
);
638 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
639 if (vb2_is_streaming(vq
)) {
640 dev_dbg(delta
->dev
, "%s V4L2 S_FMT (OUTPUT): queue busy\n",
645 ctx
->max_au_size
= pix
->sizeimage
;
646 ctx
->streaminfo
.width
= pix
->width
;
647 ctx
->streaminfo
.height
= pix
->height
;
648 ctx
->streaminfo
.streamformat
= pix
->pixelformat
;
649 ctx
->streaminfo
.colorspace
= pix
->colorspace
;
650 ctx
->streaminfo
.xfer_func
= pix
->xfer_func
;
651 ctx
->streaminfo
.ycbcr_enc
= pix
->ycbcr_enc
;
652 ctx
->streaminfo
.quantization
= pix
->quantization
;
653 ctx
->flags
|= DELTA_FLAG_STREAMINFO
;
658 static int delta_s_fmt_frame(struct file
*file
, void *fh
, struct v4l2_format
*f
)
660 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
661 struct delta_dev
*delta
= ctx
->dev
;
662 const struct delta_dec
*dec
= ctx
->dec
;
663 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
664 struct delta_frameinfo frameinfo
;
665 unsigned char str
[100] = "";
666 struct vb2_queue
*vq
;
669 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
670 if (vb2_is_streaming(vq
)) {
671 dev_dbg(delta
->dev
, "%s V4L2 S_FMT (CAPTURE): queue busy\n",
676 if (ctx
->state
< DELTA_STATE_READY
) {
678 * decoder not yet opened and valid stream header not found,
679 * could not negotiate format with decoder, check at least
680 * pixel format & negotiate resolution boundaries
683 ret
= delta_try_fmt_frame(file
, fh
, f
);
686 "%s V4L2 S_FMT (CAPTURE): unsupported format %4.4s\n",
687 ctx
->name
, (char *)&pix
->pixelformat
);
694 /* set frame information to decoder */
695 memset(&frameinfo
, 0, sizeof(frameinfo
));
696 frameinfo
.pixelformat
= pix
->pixelformat
;
697 frameinfo
.width
= pix
->width
;
698 frameinfo
.height
= pix
->height
;
699 frameinfo
.aligned_width
= pix
->width
;
700 frameinfo
.aligned_height
= pix
->height
;
701 frameinfo
.size
= pix
->sizeimage
;
702 frameinfo
.field
= pix
->field
;
703 frameinfo
.colorspace
= pix
->colorspace
;
704 frameinfo
.xfer_func
= pix
->xfer_func
;
705 frameinfo
.ycbcr_enc
= pix
->ycbcr_enc
;
706 frameinfo
.quantization
= pix
->quantization
;
707 ret
= call_dec_op(dec
, set_frameinfo
, ctx
, &frameinfo
);
711 /* then get what decoder can really do */
712 ret
= call_dec_op(dec
, get_frameinfo
, ctx
, &frameinfo
);
716 ctx
->flags
|= DELTA_FLAG_FRAMEINFO
;
717 ctx
->frameinfo
= frameinfo
;
719 "%s V4L2 SET_FMT (CAPTURE): frameinfo updated to %s\n",
721 delta_frameinfo_str(&frameinfo
, str
, sizeof(str
)));
723 pix
->pixelformat
= frameinfo
.pixelformat
;
724 pix
->width
= frameinfo
.aligned_width
;
725 pix
->height
= frameinfo
.aligned_height
;
726 pix
->bytesperline
= frame_stride(pix
->width
, pix
->pixelformat
);
727 pix
->sizeimage
= frameinfo
.size
;
728 pix
->field
= frameinfo
.field
;
729 pix
->colorspace
= frameinfo
.colorspace
;
730 pix
->xfer_func
= frameinfo
.xfer_func
;
731 pix
->ycbcr_enc
= frameinfo
.ycbcr_enc
;
732 pix
->quantization
= frameinfo
.quantization
;
737 static int delta_g_selection(struct file
*file
, void *fh
,
738 struct v4l2_selection
*s
)
740 struct delta_ctx
*ctx
= to_ctx(fh
);
741 struct delta_frameinfo
*frameinfo
= &ctx
->frameinfo
;
742 struct v4l2_rect crop
;
744 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
747 if ((ctx
->flags
& DELTA_FLAG_FRAMEINFO
) &&
748 (frameinfo
->flags
& DELTA_FRAMEINFO_FLAG_CROP
)) {
749 crop
= frameinfo
->crop
;
751 /* default to video dimensions */
754 crop
.width
= frameinfo
->width
;
755 crop
.height
= frameinfo
->height
;
759 case V4L2_SEL_TGT_COMPOSE
:
760 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
761 /* visible area inside video */
764 case V4L2_SEL_TGT_COMPOSE_PADDED
:
765 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
766 /* up to aligned dimensions */
769 s
->r
.width
= frameinfo
->aligned_width
;
770 s
->r
.height
= frameinfo
->aligned_height
;
779 static void delta_complete_eos(struct delta_ctx
*ctx
,
780 struct delta_frame
*frame
)
782 struct delta_dev
*delta
= ctx
->dev
;
783 const struct v4l2_event ev
= {.type
= V4L2_EVENT_EOS
};
787 * - by returning an empty frame flagged to V4L2_BUF_FLAG_LAST
788 * - and then send EOS event
792 frame
->info
.size
= 0;
794 /* set the last buffer flag */
795 frame
->flags
|= V4L2_BUF_FLAG_LAST
;
797 /* release frame to user */
798 delta_frame_done(ctx
, frame
, 0);
801 v4l2_event_queue_fh(&ctx
->fh
, &ev
);
803 dev_dbg(delta
->dev
, "%s EOS completed\n", ctx
->name
);
806 static int delta_try_decoder_cmd(struct file
*file
, void *fh
,
807 struct v4l2_decoder_cmd
*cmd
)
809 if (cmd
->cmd
!= V4L2_DEC_CMD_STOP
)
812 if (cmd
->flags
& V4L2_DEC_CMD_STOP_TO_BLACK
)
815 if (!(cmd
->flags
& V4L2_DEC_CMD_STOP_IMMEDIATELY
) &&
816 (cmd
->stop
.pts
!= 0))
822 static int delta_decoder_stop_cmd(struct delta_ctx
*ctx
, void *fh
)
824 const struct delta_dec
*dec
= ctx
->dec
;
825 struct delta_dev
*delta
= ctx
->dev
;
826 struct delta_frame
*frame
= NULL
;
829 dev_dbg(delta
->dev
, "%s EOS received\n", ctx
->name
);
831 if (ctx
->state
!= DELTA_STATE_READY
)
834 /* drain the decoder */
835 call_dec_op(dec
, drain
, ctx
);
837 /* release to user drained frames */
840 ret
= call_dec_op(dec
, get_frame
, ctx
, &frame
);
841 if (ret
== -ENODATA
) {
842 /* no more decoded frames */
846 dev_dbg(delta
->dev
, "%s drain frame[%d]\n",
847 ctx
->name
, frame
->index
);
849 /* pop timestamp and mark frame with it */
850 delta_pop_dts(ctx
, &frame
->dts
);
852 /* release decoded frame to user */
853 delta_frame_done(ctx
, frame
, 0);
857 /* try to complete EOS */
858 ret
= delta_get_free_frame(ctx
, &frame
);
862 /* new frame available, EOS can now be completed */
863 delta_complete_eos(ctx
, frame
);
865 ctx
->state
= DELTA_STATE_EOS
;
871 * EOS completion from driver is delayed because
872 * we don't have a free empty frame available.
873 * EOS completion is so delayed till next frame_queue() call
874 * to be sure to have a free empty frame available.
876 ctx
->state
= DELTA_STATE_WF_EOS
;
877 dev_dbg(delta
->dev
, "%s EOS delayed\n", ctx
->name
);
882 static int delta_decoder_cmd(struct file
*file
, void *fh
,
883 struct v4l2_decoder_cmd
*cmd
)
885 struct delta_ctx
*ctx
= to_ctx(fh
);
888 ret
= delta_try_decoder_cmd(file
, fh
, cmd
);
892 return delta_decoder_stop_cmd(ctx
, fh
);
895 static int delta_subscribe_event(struct v4l2_fh
*fh
,
896 const struct v4l2_event_subscription
*sub
)
900 return v4l2_event_subscribe(fh
, sub
, 2, NULL
);
909 static const struct v4l2_ioctl_ops delta_ioctl_ops
= {
910 .vidioc_querycap
= delta_querycap
,
911 .vidioc_enum_fmt_vid_cap
= delta_enum_fmt_frame
,
912 .vidioc_g_fmt_vid_cap
= delta_g_fmt_frame
,
913 .vidioc_try_fmt_vid_cap
= delta_try_fmt_frame
,
914 .vidioc_s_fmt_vid_cap
= delta_s_fmt_frame
,
915 .vidioc_enum_fmt_vid_out
= delta_enum_fmt_stream
,
916 .vidioc_g_fmt_vid_out
= delta_g_fmt_stream
,
917 .vidioc_try_fmt_vid_out
= delta_try_fmt_stream
,
918 .vidioc_s_fmt_vid_out
= delta_s_fmt_stream
,
919 .vidioc_reqbufs
= v4l2_m2m_ioctl_reqbufs
,
920 .vidioc_create_bufs
= v4l2_m2m_ioctl_create_bufs
,
921 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
922 .vidioc_expbuf
= v4l2_m2m_ioctl_expbuf
,
923 .vidioc_qbuf
= v4l2_m2m_ioctl_qbuf
,
924 .vidioc_dqbuf
= v4l2_m2m_ioctl_dqbuf
,
925 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
926 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
927 .vidioc_g_selection
= delta_g_selection
,
928 .vidioc_try_decoder_cmd
= delta_try_decoder_cmd
,
929 .vidioc_decoder_cmd
= delta_decoder_cmd
,
930 .vidioc_subscribe_event
= delta_subscribe_event
,
931 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
935 * mem-to-mem operations
938 static void delta_run_work(struct work_struct
*work
)
940 struct delta_ctx
*ctx
= container_of(work
, struct delta_ctx
, run_work
);
941 struct delta_dev
*delta
= ctx
->dev
;
942 const struct delta_dec
*dec
= ctx
->dec
;
944 struct delta_frame
*frame
= NULL
;
946 bool discard
= false;
947 struct vb2_v4l2_buffer
*vbuf
;
950 dev_err(delta
->dev
, "%s no decoder opened yet\n", ctx
->name
);
954 /* protect instance against reentrancy */
955 mutex_lock(&ctx
->lock
);
957 vbuf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
);
959 dev_err(delta
->dev
, "%s no buffer to decode\n", ctx
->name
);
960 mutex_unlock(&ctx
->lock
);
964 au
->size
= vb2_get_plane_payload(&vbuf
->vb2_buf
, 0);
965 au
->dts
= vbuf
->vb2_buf
.timestamp
;
967 /* dump access unit */
970 /* enable the hardware */
972 ret
= delta_get_sync(ctx
);
977 /* decode this access unit */
978 ret
= call_dec_op(dec
, decode
, ctx
, au
);
981 * if the (-ENODATA) value is returned, it refers to the interlaced
982 * stream case for which 2 access units are needed to get 1 frame.
983 * So, this returned value doesn't mean that the decoding fails, but
984 * indicates that the timestamp information of the access unit shall
985 * not be taken into account, and that the V4L2 buffer associated with
986 * the access unit shall be flagged with V4L2_BUF_FLAG_ERROR to inform
987 * the user of this situation
989 if (ret
== -ENODATA
) {
992 dev_err(delta
->dev
, "%s decoding failed (%d)\n",
995 /* disable the hardware */
997 delta_put_autosuspend(ctx
);
1002 /* disable the hardware */
1004 delta_put_autosuspend(ctx
);
1006 /* push au timestamp in FIFO */
1008 delta_push_dts(ctx
, au
->dts
);
1010 /* get available decoded frames */
1012 ret
= call_dec_op(dec
, get_frame
, ctx
, &frame
);
1013 if (ret
== -ENODATA
) {
1014 /* no more decoded frames */
1018 dev_err(delta
->dev
, "%s cannot get decoded frame (%d)\n",
1024 "%s NULL decoded frame\n",
1030 /* pop timestamp and mark frame with it */
1031 delta_pop_dts(ctx
, &frame
->dts
);
1033 /* release decoded frame to user */
1034 delta_frame_done(ctx
, frame
, 0);
1038 requeue_free_frames(ctx
);
1039 delta_au_done(ctx
, au
, (discard
? -ENODATA
: 0));
1040 mutex_unlock(&ctx
->lock
);
1041 v4l2_m2m_job_finish(delta
->m2m_dev
, ctx
->fh
.m2m_ctx
);
1045 requeue_free_frames(ctx
);
1046 delta_au_done(ctx
, au
, ret
);
1047 mutex_unlock(&ctx
->lock
);
1048 v4l2_m2m_job_finish(delta
->m2m_dev
, ctx
->fh
.m2m_ctx
);
1051 static void delta_device_run(void *priv
)
1053 struct delta_ctx
*ctx
= priv
;
1054 struct delta_dev
*delta
= ctx
->dev
;
1056 queue_work(delta
->work_queue
, &ctx
->run_work
);
1059 static void delta_job_abort(void *priv
)
1061 struct delta_ctx
*ctx
= priv
;
1062 struct delta_dev
*delta
= ctx
->dev
;
1064 dev_dbg(delta
->dev
, "%s aborting job\n", ctx
->name
);
1066 ctx
->aborting
= true;
1069 static int delta_job_ready(void *priv
)
1071 struct delta_ctx
*ctx
= priv
;
1072 struct delta_dev
*delta
= ctx
->dev
;
1073 int src_bufs
= v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
);
1076 dev_dbg(delta
->dev
, "%s not ready: not enough video buffers.\n",
1081 if (!v4l2_m2m_num_dst_bufs_ready(ctx
->fh
.m2m_ctx
)) {
1082 dev_dbg(delta
->dev
, "%s not ready: not enough video capture buffers.\n",
1087 if (ctx
->aborting
) {
1088 dev_dbg(delta
->dev
, "%s job not ready: aborting\n", ctx
->name
);
1092 dev_dbg(delta
->dev
, "%s job ready\n", ctx
->name
);
1097 /* mem-to-mem ops */
1098 static const struct v4l2_m2m_ops delta_m2m_ops
= {
1099 .device_run
= delta_device_run
,
1100 .job_ready
= delta_job_ready
,
1101 .job_abort
= delta_job_abort
,
1103 .unlock
= delta_unlock
,
1107 * VB2 queue operations
1110 static int delta_vb2_au_queue_setup(struct vb2_queue
*vq
,
1111 unsigned int *num_buffers
,
1112 unsigned int *num_planes
,
1113 unsigned int sizes
[],
1114 struct device
*alloc_devs
[])
1116 struct delta_ctx
*ctx
= vb2_get_drv_priv(vq
);
1117 unsigned int size
= ctx
->max_au_size
;
1120 return sizes
[0] < size
? -EINVAL
: 0;
1123 if (*num_buffers
< 1)
1125 if (*num_buffers
> DELTA_MAX_AUS
)
1126 *num_buffers
= DELTA_MAX_AUS
;
1133 static int delta_vb2_au_prepare(struct vb2_buffer
*vb
)
1135 struct vb2_queue
*q
= vb
->vb2_queue
;
1136 struct delta_ctx
*ctx
= vb2_get_drv_priv(q
);
1137 struct delta_dev
*delta
= ctx
->dev
;
1138 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1139 struct delta_au
*au
= to_au(vbuf
);
1141 if (!au
->prepared
) {
1142 /* get memory addresses */
1143 au
->vaddr
= vb2_plane_vaddr(&au
->vbuf
.vb2_buf
, 0);
1144 au
->paddr
= vb2_dma_contig_plane_dma_addr
1145 (&au
->vbuf
.vb2_buf
, 0);
1146 au
->prepared
= true;
1147 dev_dbg(delta
->dev
, "%s au[%d] prepared; virt=0x%p, phy=0x%pad\n",
1148 ctx
->name
, vb
->index
, au
->vaddr
, &au
->paddr
);
1151 if (vbuf
->field
== V4L2_FIELD_ANY
)
1152 vbuf
->field
= V4L2_FIELD_NONE
;
1157 static int delta_setup_frame(struct delta_ctx
*ctx
,
1158 struct delta_frame
*frame
)
1160 struct delta_dev
*delta
= ctx
->dev
;
1161 const struct delta_dec
*dec
= ctx
->dec
;
1163 if (frame
->index
>= DELTA_MAX_FRAMES
) {
1165 "%s frame index=%d exceeds output frame count (%d)\n",
1166 ctx
->name
, frame
->index
, DELTA_MAX_FRAMES
);
1170 if (ctx
->nb_of_frames
>= DELTA_MAX_FRAMES
) {
1172 "%s number of frames exceeds output frame count (%d > %d)\n",
1173 ctx
->name
, ctx
->nb_of_frames
, DELTA_MAX_FRAMES
);
1177 if (frame
->index
!= ctx
->nb_of_frames
) {
1178 dev_warn(delta
->dev
,
1179 "%s frame index discontinuity detected, expected %d, got %d\n",
1180 ctx
->name
, ctx
->nb_of_frames
, frame
->index
);
1183 frame
->state
= DELTA_FRAME_FREE
;
1184 ctx
->frames
[ctx
->nb_of_frames
] = frame
;
1185 ctx
->nb_of_frames
++;
1187 /* setup frame on decoder side */
1188 return call_dec_op(dec
, setup_frame
, ctx
, frame
);
1192 * default implementation of get_frameinfo decoder ops
1193 * matching frame information from stream information
1194 * & with default pixel format & default alignment.
1196 int delta_get_frameinfo_default(struct delta_ctx
*ctx
,
1197 struct delta_frameinfo
*frameinfo
)
1199 struct delta_streaminfo
*streaminfo
= &ctx
->streaminfo
;
1201 memset(frameinfo
, 0, sizeof(*frameinfo
));
1202 frameinfo
->pixelformat
= V4L2_PIX_FMT_NV12
;
1203 frameinfo
->width
= streaminfo
->width
;
1204 frameinfo
->height
= streaminfo
->height
;
1205 frameinfo
->aligned_width
= ALIGN(streaminfo
->width
,
1206 DELTA_WIDTH_ALIGNMENT
);
1207 frameinfo
->aligned_height
= ALIGN(streaminfo
->height
,
1208 DELTA_HEIGHT_ALIGNMENT
);
1209 frameinfo
->size
= frame_size(frameinfo
->aligned_width
,
1210 frameinfo
->aligned_height
,
1211 frameinfo
->pixelformat
);
1212 if (streaminfo
->flags
& DELTA_STREAMINFO_FLAG_CROP
) {
1213 frameinfo
->flags
|= DELTA_FRAMEINFO_FLAG_CROP
;
1214 frameinfo
->crop
= streaminfo
->crop
;
1216 if (streaminfo
->flags
& DELTA_STREAMINFO_FLAG_PIXELASPECT
) {
1217 frameinfo
->flags
|= DELTA_FRAMEINFO_FLAG_PIXELASPECT
;
1218 frameinfo
->pixelaspect
= streaminfo
->pixelaspect
;
1220 frameinfo
->field
= streaminfo
->field
;
1226 * default implementation of recycle decoder ops
1227 * consisting to relax the "decoded" frame state
1229 int delta_recycle_default(struct delta_ctx
*pctx
,
1230 struct delta_frame
*frame
)
1232 frame
->state
&= ~DELTA_FRAME_DEC
;
1237 static void dump_frames_status(struct delta_ctx
*ctx
)
1239 struct delta_dev
*delta
= ctx
->dev
;
1241 struct delta_frame
*frame
;
1242 unsigned char str
[100] = "";
1244 dev_info(delta
->dev
,
1245 "%s dumping frames status...\n", ctx
->name
);
1247 for (i
= 0; i
< ctx
->nb_of_frames
; i
++) {
1248 frame
= ctx
->frames
[i
];
1249 dev_info(delta
->dev
,
1250 "%s frame[%d] %s\n",
1251 ctx
->name
, frame
->index
,
1252 frame_state_str(frame
->state
,
1257 int delta_get_free_frame(struct delta_ctx
*ctx
,
1258 struct delta_frame
**pframe
)
1260 struct delta_dev
*delta
= ctx
->dev
;
1261 struct vb2_v4l2_buffer
*vbuf
;
1262 struct delta_frame
*frame
;
1266 vbuf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
);
1268 dev_err(delta
->dev
, "%s no frame available",
1273 frame
= to_frame(vbuf
);
1274 frame
->state
&= ~DELTA_FRAME_M2M
;
1275 if (frame
->state
!= DELTA_FRAME_FREE
) {
1277 "%s frame[%d] is not free\n",
1278 ctx
->name
, frame
->index
);
1279 dump_frames_status(ctx
);
1284 "%s get free frame[%d]\n", ctx
->name
, frame
->index
);
1290 int delta_get_sync(struct delta_ctx
*ctx
)
1292 struct delta_dev
*delta
= ctx
->dev
;
1295 /* enable the hardware */
1296 ret
= pm_runtime_get_sync(delta
->dev
);
1298 dev_err(delta
->dev
, "%s pm_runtime_get_sync failed (%d)\n",
1306 void delta_put_autosuspend(struct delta_ctx
*ctx
)
1308 struct delta_dev
*delta
= ctx
->dev
;
1310 pm_runtime_put_autosuspend(delta
->dev
);
1313 static void delta_vb2_au_queue(struct vb2_buffer
*vb
)
1315 struct vb2_queue
*q
= vb
->vb2_queue
;
1316 struct delta_ctx
*ctx
= vb2_get_drv_priv(q
);
1317 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1319 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vbuf
);
1322 static int delta_vb2_au_start_streaming(struct vb2_queue
*q
,
1325 struct delta_ctx
*ctx
= vb2_get_drv_priv(q
);
1326 struct delta_dev
*delta
= ctx
->dev
;
1327 const struct delta_dec
*dec
= ctx
->dec
;
1328 struct delta_au
*au
;
1330 struct vb2_v4l2_buffer
*vbuf
= NULL
;
1331 struct delta_streaminfo
*streaminfo
= &ctx
->streaminfo
;
1332 struct delta_frameinfo
*frameinfo
= &ctx
->frameinfo
;
1333 unsigned char str1
[100] = "";
1334 unsigned char str2
[100] = "";
1336 if ((ctx
->state
!= DELTA_STATE_WF_FORMAT
) &&
1337 (ctx
->state
!= DELTA_STATE_WF_STREAMINFO
))
1340 if (ctx
->state
== DELTA_STATE_WF_FORMAT
) {
1341 /* open decoder if not yet done */
1342 ret
= delta_open_decoder(ctx
,
1343 ctx
->streaminfo
.streamformat
,
1344 ctx
->frameinfo
.pixelformat
, &dec
);
1348 ctx
->state
= DELTA_STATE_WF_STREAMINFO
;
1352 * first buffer should contain stream header,
1353 * decode it to get the infos related to stream
1354 * such as width, height, dpb, ...
1356 vbuf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
);
1358 dev_err(delta
->dev
, "%s failed to start streaming, no stream header buffer enqueued\n",
1364 au
->size
= vb2_get_plane_payload(&vbuf
->vb2_buf
, 0);
1365 au
->dts
= vbuf
->vb2_buf
.timestamp
;
1367 delta_push_dts(ctx
, au
->dts
);
1369 /* dump access unit */
1372 /* decode this access unit */
1373 ret
= call_dec_op(dec
, decode
, ctx
, au
);
1375 dev_err(delta
->dev
, "%s failed to start streaming, header decoding failed (%d)\n",
1380 ret
= call_dec_op(dec
, get_streaminfo
, ctx
, streaminfo
);
1382 dev_dbg_ratelimited(delta
->dev
,
1383 "%s failed to start streaming, valid stream header not yet decoded\n",
1387 ctx
->flags
|= DELTA_FLAG_STREAMINFO
;
1389 ret
= call_dec_op(dec
, get_frameinfo
, ctx
, frameinfo
);
1392 ctx
->flags
|= DELTA_FLAG_FRAMEINFO
;
1394 ctx
->state
= DELTA_STATE_READY
;
1396 dev_dbg(delta
->dev
, "%s %s => %s\n", ctx
->name
,
1397 delta_streaminfo_str(streaminfo
, str1
, sizeof(str1
)),
1398 delta_frameinfo_str(frameinfo
, str2
, sizeof(str2
)));
1400 delta_au_done(ctx
, au
, ret
);
1405 * return all buffers to vb2 in QUEUED state.
1406 * This will give ownership back to userspace
1409 v4l2_m2m_buf_done(vbuf
, VB2_BUF_STATE_QUEUED
);
1411 while ((vbuf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
)))
1412 v4l2_m2m_buf_done(vbuf
, VB2_BUF_STATE_QUEUED
);
1416 static void delta_vb2_au_stop_streaming(struct vb2_queue
*q
)
1418 struct delta_ctx
*ctx
= vb2_get_drv_priv(q
);
1419 struct vb2_v4l2_buffer
*vbuf
;
1421 delta_flush_dts(ctx
);
1423 /* return all buffers to vb2 in ERROR state */
1424 while ((vbuf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
)))
1425 v4l2_m2m_buf_done(vbuf
, VB2_BUF_STATE_ERROR
);
1429 ctx
->aborting
= false;
1432 static int delta_vb2_frame_queue_setup(struct vb2_queue
*vq
,
1433 unsigned int *num_buffers
,
1434 unsigned int *num_planes
,
1435 unsigned int sizes
[],
1436 struct device
*alloc_devs
[])
1438 struct delta_ctx
*ctx
= vb2_get_drv_priv(vq
);
1439 struct delta_dev
*delta
= ctx
->dev
;
1440 struct delta_streaminfo
*streaminfo
= &ctx
->streaminfo
;
1441 struct delta_frameinfo
*frameinfo
= &ctx
->frameinfo
;
1442 unsigned int size
= frameinfo
->size
;
1445 * the number of output buffers needed for decoding =
1446 * user need (*num_buffers given, usually for display pipeline) +
1447 * stream need (streaminfo->dpb) +
1448 * decoding peak smoothing (depends on DELTA IP perf)
1450 if (*num_buffers
< DELTA_MIN_FRAME_USER
) {
1452 "%s num_buffers too low (%d), increasing to %d\n",
1453 ctx
->name
, *num_buffers
, DELTA_MIN_FRAME_USER
);
1454 *num_buffers
= DELTA_MIN_FRAME_USER
;
1457 *num_buffers
+= streaminfo
->dpb
+ DELTA_PEAK_FRAME_SMOOTHING
;
1459 if (*num_buffers
> DELTA_MAX_FRAMES
) {
1461 "%s output frame count too high (%d), cut to %d\n",
1462 ctx
->name
, *num_buffers
, DELTA_MAX_FRAMES
);
1463 *num_buffers
= DELTA_MAX_FRAMES
;
1467 return sizes
[0] < size
? -EINVAL
: 0;
1469 /* single plane for Y and CbCr */
1474 ctx
->nb_of_frames
= 0;
1479 static int delta_vb2_frame_prepare(struct vb2_buffer
*vb
)
1481 struct vb2_queue
*q
= vb
->vb2_queue
;
1482 struct delta_ctx
*ctx
= vb2_get_drv_priv(q
);
1483 struct delta_dev
*delta
= ctx
->dev
;
1484 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1485 struct delta_frame
*frame
= to_frame(vbuf
);
1488 if (!frame
->prepared
) {
1489 frame
->index
= vbuf
->vb2_buf
.index
;
1490 frame
->vaddr
= vb2_plane_vaddr(&vbuf
->vb2_buf
, 0);
1491 frame
->paddr
= vb2_dma_contig_plane_dma_addr(&vbuf
->vb2_buf
, 0);
1492 frame
->info
= ctx
->frameinfo
;
1494 ret
= delta_setup_frame(ctx
, frame
);
1497 "%s setup_frame() failed (%d)\n",
1501 frame
->prepared
= true;
1503 "%s frame[%d] prepared; virt=0x%p, phy=0x%pad\n",
1504 ctx
->name
, vb
->index
, frame
->vaddr
,
1508 frame
->flags
= vbuf
->flags
;
1513 static void delta_vb2_frame_finish(struct vb2_buffer
*vb
)
1515 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1516 struct delta_frame
*frame
= to_frame(vbuf
);
1518 /* update V4L2 fields for user */
1519 vb2_set_plane_payload(&vbuf
->vb2_buf
, 0, frame
->info
.size
);
1520 vb
->timestamp
= frame
->dts
;
1521 vbuf
->field
= frame
->field
;
1522 vbuf
->flags
= frame
->flags
;
1525 static void delta_vb2_frame_queue(struct vb2_buffer
*vb
)
1527 struct vb2_queue
*q
= vb
->vb2_queue
;
1528 struct delta_ctx
*ctx
= vb2_get_drv_priv(q
);
1529 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1530 struct delta_frame
*frame
= to_frame(vbuf
);
1532 if (ctx
->state
== DELTA_STATE_WF_EOS
) {
1533 /* new frame available, EOS can now be completed */
1534 delta_complete_eos(ctx
, frame
);
1536 ctx
->state
= DELTA_STATE_EOS
;
1538 /* return, no need to recycle this buffer to decoder */
1542 /* recycle this frame */
1543 delta_recycle(ctx
, frame
);
1546 static void delta_vb2_frame_stop_streaming(struct vb2_queue
*q
)
1548 struct delta_ctx
*ctx
= vb2_get_drv_priv(q
);
1549 struct vb2_v4l2_buffer
*vbuf
;
1550 struct delta_frame
*frame
;
1551 const struct delta_dec
*dec
= ctx
->dec
;
1554 delta_flush_dts(ctx
);
1556 call_dec_op(dec
, flush
, ctx
);
1559 * return all buffers to vb2 in ERROR state
1560 * & reset each frame state to OUT
1562 for (i
= 0; i
< ctx
->nb_of_frames
; i
++) {
1563 frame
= ctx
->frames
[i
];
1564 if (!(frame
->state
& DELTA_FRAME_OUT
)) {
1565 vbuf
= &frame
->vbuf
;
1566 v4l2_m2m_buf_done(vbuf
, VB2_BUF_STATE_ERROR
);
1568 frame
->state
= DELTA_FRAME_OUT
;
1573 ctx
->aborting
= false;
1577 static const struct vb2_ops delta_vb2_au_ops
= {
1578 .queue_setup
= delta_vb2_au_queue_setup
,
1579 .buf_prepare
= delta_vb2_au_prepare
,
1580 .buf_queue
= delta_vb2_au_queue
,
1581 .wait_prepare
= vb2_ops_wait_prepare
,
1582 .wait_finish
= vb2_ops_wait_finish
,
1583 .start_streaming
= delta_vb2_au_start_streaming
,
1584 .stop_streaming
= delta_vb2_au_stop_streaming
,
1587 static const struct vb2_ops delta_vb2_frame_ops
= {
1588 .queue_setup
= delta_vb2_frame_queue_setup
,
1589 .buf_prepare
= delta_vb2_frame_prepare
,
1590 .buf_finish
= delta_vb2_frame_finish
,
1591 .buf_queue
= delta_vb2_frame_queue
,
1592 .wait_prepare
= vb2_ops_wait_prepare
,
1593 .wait_finish
= vb2_ops_wait_finish
,
1594 .stop_streaming
= delta_vb2_frame_stop_streaming
,
1598 * V4L2 file operations
1601 static int queue_init(void *priv
,
1602 struct vb2_queue
*src_vq
, struct vb2_queue
*dst_vq
)
1604 struct vb2_queue
*q
;
1605 struct delta_ctx
*ctx
= priv
;
1606 struct delta_dev
*delta
= ctx
->dev
;
1609 /* setup vb2 queue for stream input */
1611 q
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1612 q
->io_modes
= VB2_MMAP
| VB2_DMABUF
;
1614 /* overload vb2 buf with private au struct */
1615 q
->buf_struct_size
= sizeof(struct delta_au
);
1616 q
->ops
= &delta_vb2_au_ops
;
1617 q
->mem_ops
= &vb2_dma_contig_memops
;
1618 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
1619 q
->lock
= &delta
->lock
;
1620 q
->dev
= delta
->dev
;
1622 ret
= vb2_queue_init(q
);
1626 /* setup vb2 queue for frame output */
1628 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1629 q
->io_modes
= VB2_MMAP
| VB2_DMABUF
;
1631 /* overload vb2 buf with private frame struct */
1632 q
->buf_struct_size
= sizeof(struct delta_frame
)
1633 + DELTA_MAX_FRAME_PRIV_SIZE
;
1634 q
->ops
= &delta_vb2_frame_ops
;
1635 q
->mem_ops
= &vb2_dma_contig_memops
;
1636 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
1637 q
->lock
= &delta
->lock
;
1638 q
->dev
= delta
->dev
;
1640 return vb2_queue_init(q
);
1643 static int delta_open(struct file
*file
)
1645 struct delta_dev
*delta
= video_drvdata(file
);
1646 struct delta_ctx
*ctx
= NULL
;
1649 mutex_lock(&delta
->lock
);
1651 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
1658 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
1659 file
->private_data
= &ctx
->fh
;
1660 v4l2_fh_add(&ctx
->fh
);
1662 INIT_WORK(&ctx
->run_work
, delta_run_work
);
1663 mutex_init(&ctx
->lock
);
1665 ctx
->fh
.m2m_ctx
= v4l2_m2m_ctx_init(delta
->m2m_dev
, ctx
,
1667 if (IS_ERR(ctx
->fh
.m2m_ctx
)) {
1668 ret
= PTR_ERR(ctx
->fh
.m2m_ctx
);
1669 dev_err(delta
->dev
, "%s failed to initialize m2m context (%d)\n",
1675 * wait stream format to determine which
1678 ctx
->state
= DELTA_STATE_WF_FORMAT
;
1680 INIT_LIST_HEAD(&ctx
->dts
);
1682 /* set the instance name */
1683 delta
->instance_id
++;
1684 snprintf(ctx
->name
, sizeof(ctx
->name
), "[%3d:----]",
1685 delta
->instance_id
);
1687 /* default parameters for frame and stream */
1688 set_default_params(ctx
);
1690 /* enable ST231 clocks */
1691 if (delta
->clk_st231
)
1692 if (clk_prepare_enable(delta
->clk_st231
))
1693 dev_warn(delta
->dev
, "failed to enable st231 clk\n");
1695 /* enable FLASH_PROMIP clock */
1696 if (delta
->clk_flash_promip
)
1697 if (clk_prepare_enable(delta
->clk_flash_promip
))
1698 dev_warn(delta
->dev
, "failed to enable delta promip clk\n");
1700 mutex_unlock(&delta
->lock
);
1702 dev_dbg(delta
->dev
, "%s decoder instance created\n", ctx
->name
);
1707 v4l2_fh_del(&ctx
->fh
);
1708 v4l2_fh_exit(&ctx
->fh
);
1711 mutex_unlock(&delta
->lock
);
1716 static int delta_release(struct file
*file
)
1718 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
1719 struct delta_dev
*delta
= ctx
->dev
;
1720 const struct delta_dec
*dec
= ctx
->dec
;
1722 mutex_lock(&delta
->lock
);
1725 call_dec_op(dec
, close
, ctx
);
1728 * trace a summary of instance
1729 * before closing (debug purpose)
1731 delta_trace_summary(ctx
);
1733 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
1735 v4l2_fh_del(&ctx
->fh
);
1736 v4l2_fh_exit(&ctx
->fh
);
1738 /* disable ST231 clocks */
1739 if (delta
->clk_st231
)
1740 clk_disable_unprepare(delta
->clk_st231
);
1742 /* disable FLASH_PROMIP clock */
1743 if (delta
->clk_flash_promip
)
1744 clk_disable_unprepare(delta
->clk_flash_promip
);
1746 dev_dbg(delta
->dev
, "%s decoder instance released\n", ctx
->name
);
1750 mutex_unlock(&delta
->lock
);
1755 static const struct v4l2_file_operations delta_fops
= {
1756 .owner
= THIS_MODULE
,
1758 .release
= delta_release
,
1759 .unlocked_ioctl
= video_ioctl2
,
1760 .mmap
= v4l2_m2m_fop_mmap
,
1761 .poll
= v4l2_m2m_fop_poll
,
1765 * Platform device operations
1768 static int delta_register_device(struct delta_dev
*delta
)
1771 struct video_device
*vdev
;
1776 delta
->m2m_dev
= v4l2_m2m_init(&delta_m2m_ops
);
1777 if (IS_ERR(delta
->m2m_dev
)) {
1778 dev_err(delta
->dev
, "%s failed to initialize v4l2-m2m device\n",
1780 ret
= PTR_ERR(delta
->m2m_dev
);
1784 vdev
= video_device_alloc();
1786 dev_err(delta
->dev
, "%s failed to allocate video device\n",
1789 goto err_m2m_release
;
1792 vdev
->fops
= &delta_fops
;
1793 vdev
->ioctl_ops
= &delta_ioctl_ops
;
1794 vdev
->release
= video_device_release
;
1795 vdev
->lock
= &delta
->lock
;
1796 vdev
->vfl_dir
= VFL_DIR_M2M
;
1797 vdev
->device_caps
= V4L2_CAP_STREAMING
| V4L2_CAP_VIDEO_M2M
;
1798 vdev
->v4l2_dev
= &delta
->v4l2_dev
;
1799 snprintf(vdev
->name
, sizeof(vdev
->name
), "%s-%s",
1800 DELTA_NAME
, DELTA_FW_VERSION
);
1802 ret
= video_register_device(vdev
, VFL_TYPE_GRABBER
, -1);
1804 dev_err(delta
->dev
, "%s failed to register video device\n",
1806 goto err_vdev_release
;
1810 video_set_drvdata(vdev
, delta
);
1814 video_device_release(vdev
);
1816 v4l2_m2m_release(delta
->m2m_dev
);
1821 static void delta_unregister_device(struct delta_dev
*delta
)
1827 v4l2_m2m_release(delta
->m2m_dev
);
1829 video_unregister_device(delta
->vdev
);
1832 static int delta_probe(struct platform_device
*pdev
)
1834 struct delta_dev
*delta
;
1835 struct device
*dev
= &pdev
->dev
;
1838 delta
= devm_kzalloc(dev
, sizeof(*delta
), GFP_KERNEL
);
1846 platform_set_drvdata(pdev
, delta
);
1848 mutex_init(&delta
->lock
);
1850 /* get clock resources */
1851 delta
->clk_delta
= devm_clk_get(dev
, "delta");
1852 if (IS_ERR(delta
->clk_delta
)) {
1853 dev_dbg(dev
, "%s can't get delta clock\n", DELTA_PREFIX
);
1854 delta
->clk_delta
= NULL
;
1857 delta
->clk_st231
= devm_clk_get(dev
, "delta-st231");
1858 if (IS_ERR(delta
->clk_st231
)) {
1859 dev_dbg(dev
, "%s can't get delta-st231 clock\n", DELTA_PREFIX
);
1860 delta
->clk_st231
= NULL
;
1863 delta
->clk_flash_promip
= devm_clk_get(dev
, "delta-flash-promip");
1864 if (IS_ERR(delta
->clk_flash_promip
)) {
1865 dev_dbg(dev
, "%s can't get delta-flash-promip clock\n",
1867 delta
->clk_flash_promip
= NULL
;
1870 /* init pm_runtime used for power management */
1871 pm_runtime_set_autosuspend_delay(dev
, DELTA_HW_AUTOSUSPEND_DELAY_MS
);
1872 pm_runtime_use_autosuspend(dev
);
1873 pm_runtime_set_suspended(dev
);
1874 pm_runtime_enable(dev
);
1876 /* init firmware ipc channel */
1877 ret
= delta_ipc_init(delta
);
1879 dev_err(delta
->dev
, "%s failed to initialize firmware ipc channel\n",
1884 /* register all available decoders */
1885 register_decoders(delta
);
1887 /* register all supported formats */
1888 register_formats(delta
);
1890 /* register on V4L2 */
1891 ret
= v4l2_device_register(dev
, &delta
->v4l2_dev
);
1893 dev_err(delta
->dev
, "%s failed to register V4L2 device\n",
1898 delta
->work_queue
= create_workqueue(DELTA_NAME
);
1899 if (!delta
->work_queue
) {
1900 dev_err(delta
->dev
, "%s failed to allocate work queue\n",
1906 /* register device */
1907 ret
= delta_register_device(delta
);
1909 goto err_work_queue
;
1911 dev_info(dev
, "%s %s registered as /dev/video%d\n",
1912 DELTA_PREFIX
, delta
->vdev
->name
, delta
->vdev
->num
);
1917 destroy_workqueue(delta
->work_queue
);
1919 v4l2_device_unregister(&delta
->v4l2_dev
);
1924 static int delta_remove(struct platform_device
*pdev
)
1926 struct delta_dev
*delta
= platform_get_drvdata(pdev
);
1928 delta_ipc_exit(delta
);
1930 delta_unregister_device(delta
);
1932 destroy_workqueue(delta
->work_queue
);
1934 pm_runtime_put_autosuspend(delta
->dev
);
1935 pm_runtime_disable(delta
->dev
);
1937 v4l2_device_unregister(&delta
->v4l2_dev
);
1942 static int delta_runtime_suspend(struct device
*dev
)
1944 struct delta_dev
*delta
= dev_get_drvdata(dev
);
1946 if (delta
->clk_delta
)
1947 clk_disable_unprepare(delta
->clk_delta
);
1952 static int delta_runtime_resume(struct device
*dev
)
1954 struct delta_dev
*delta
= dev_get_drvdata(dev
);
1956 if (delta
->clk_delta
)
1957 if (clk_prepare_enable(delta
->clk_delta
))
1958 dev_warn(dev
, "failed to prepare/enable delta clk\n");
1964 static const struct dev_pm_ops delta_pm_ops
= {
1965 .runtime_suspend
= delta_runtime_suspend
,
1966 .runtime_resume
= delta_runtime_resume
,
1969 static const struct of_device_id delta_match_types
[] = {
1971 .compatible
= "st,st-delta",
1978 MODULE_DEVICE_TABLE(of
, delta_match_types
);
1980 static struct platform_driver delta_driver
= {
1981 .probe
= delta_probe
,
1982 .remove
= delta_remove
,
1985 .of_match_table
= delta_match_types
,
1986 .pm
= &delta_pm_ops
},
1989 module_platform_driver(delta_driver
);
1991 MODULE_LICENSE("GPL");
1992 MODULE_AUTHOR("Hugues Fruchet <hugues.fruchet@st.com>");
1993 MODULE_DESCRIPTION("STMicroelectronics DELTA video decoder V4L2 driver");