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 int delta_open_decoder(struct delta_ctx
*ctx
, u32 streamformat
,
343 u32 pixelformat
, const struct delta_dec
**pdec
)
345 struct delta_dev
*delta
= ctx
->dev
;
346 const struct delta_dec
*dec
;
349 dec
= delta_find_decoder(ctx
, streamformat
, ctx
->frameinfo
.pixelformat
);
351 dev_err(delta
->dev
, "%s no decoder found matching %4.4s => %4.4s\n",
352 ctx
->name
, (char *)&streamformat
, (char *)&pixelformat
);
356 dev_dbg(delta
->dev
, "%s one decoder matching %4.4s => %4.4s\n",
357 ctx
->name
, (char *)&streamformat
, (char *)&pixelformat
);
359 /* update instance name */
360 snprintf(ctx
->name
, sizeof(ctx
->name
), "[%3d:%4.4s]",
361 delta
->instance_id
, (char *)&streamformat
);
363 /* open decoder instance */
364 ret
= call_dec_op(dec
, open
, ctx
);
366 dev_err(delta
->dev
, "%s failed to open decoder instance (%d)\n",
371 dev_dbg(delta
->dev
, "%s %s decoder opened\n", ctx
->name
, dec
->name
);
379 * V4L2 ioctl operations
382 static int delta_querycap(struct file
*file
, void *priv
,
383 struct v4l2_capability
*cap
)
385 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
386 struct delta_dev
*delta
= ctx
->dev
;
388 strlcpy(cap
->driver
, DELTA_NAME
, sizeof(cap
->driver
));
389 strlcpy(cap
->card
, delta
->vdev
->name
, sizeof(cap
->card
));
390 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "platform:%s",
396 static int delta_enum_fmt_stream(struct file
*file
, void *priv
,
397 struct v4l2_fmtdesc
*f
)
399 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
400 struct delta_dev
*delta
= ctx
->dev
;
402 if (unlikely(f
->index
>= delta
->nb_of_streamformats
))
405 f
->pixelformat
= delta
->streamformats
[f
->index
];
410 static int delta_enum_fmt_frame(struct file
*file
, void *priv
,
411 struct v4l2_fmtdesc
*f
)
413 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
414 struct delta_dev
*delta
= ctx
->dev
;
416 if (unlikely(f
->index
>= delta
->nb_of_pixelformats
))
419 f
->pixelformat
= delta
->pixelformats
[f
->index
];
424 static int delta_g_fmt_stream(struct file
*file
, void *fh
,
425 struct v4l2_format
*f
)
427 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
428 struct delta_dev
*delta
= ctx
->dev
;
429 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
430 struct delta_streaminfo
*streaminfo
= &ctx
->streaminfo
;
431 unsigned char str
[100] = "";
433 if (!(ctx
->flags
& DELTA_FLAG_STREAMINFO
))
435 "%s V4L2 GET_FMT (OUTPUT): no stream information available, default to %s\n",
437 delta_streaminfo_str(streaminfo
, str
, sizeof(str
)));
439 pix
->pixelformat
= streaminfo
->streamformat
;
440 pix
->width
= streaminfo
->width
;
441 pix
->height
= streaminfo
->height
;
442 pix
->field
= streaminfo
->field
;
443 pix
->bytesperline
= 0;
444 pix
->sizeimage
= ctx
->max_au_size
;
445 pix
->colorspace
= streaminfo
->colorspace
;
446 pix
->xfer_func
= streaminfo
->xfer_func
;
447 pix
->ycbcr_enc
= streaminfo
->ycbcr_enc
;
448 pix
->quantization
= streaminfo
->quantization
;
453 static int delta_g_fmt_frame(struct file
*file
, void *fh
, struct v4l2_format
*f
)
455 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
456 struct delta_dev
*delta
= ctx
->dev
;
457 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
458 struct delta_frameinfo
*frameinfo
= &ctx
->frameinfo
;
459 struct delta_streaminfo
*streaminfo
= &ctx
->streaminfo
;
460 unsigned char str
[100] = "";
462 if (!(ctx
->flags
& DELTA_FLAG_FRAMEINFO
))
464 "%s V4L2 GET_FMT (CAPTURE): no frame information available, default to %s\n",
466 delta_frameinfo_str(frameinfo
, str
, sizeof(str
)));
468 pix
->pixelformat
= frameinfo
->pixelformat
;
469 pix
->width
= frameinfo
->aligned_width
;
470 pix
->height
= frameinfo
->aligned_height
;
471 pix
->field
= frameinfo
->field
;
472 pix
->bytesperline
= frame_stride(frameinfo
->aligned_width
,
473 frameinfo
->pixelformat
);
474 pix
->sizeimage
= frameinfo
->size
;
476 if (ctx
->flags
& DELTA_FLAG_STREAMINFO
) {
477 /* align colorspace & friends on stream ones if any set */
478 frameinfo
->colorspace
= streaminfo
->colorspace
;
479 frameinfo
->xfer_func
= streaminfo
->xfer_func
;
480 frameinfo
->ycbcr_enc
= streaminfo
->ycbcr_enc
;
481 frameinfo
->quantization
= streaminfo
->quantization
;
483 pix
->colorspace
= frameinfo
->colorspace
;
484 pix
->xfer_func
= frameinfo
->xfer_func
;
485 pix
->ycbcr_enc
= frameinfo
->ycbcr_enc
;
486 pix
->quantization
= frameinfo
->quantization
;
491 static int delta_try_fmt_stream(struct file
*file
, void *priv
,
492 struct v4l2_format
*f
)
494 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
495 struct delta_dev
*delta
= ctx
->dev
;
496 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
497 u32 streamformat
= pix
->pixelformat
;
498 const struct delta_dec
*dec
;
502 dec
= delta_find_decoder(ctx
, streamformat
, ctx
->frameinfo
.pixelformat
);
505 "%s V4L2 TRY_FMT (OUTPUT): unsupported format %4.4s\n",
506 ctx
->name
, (char *)&pix
->pixelformat
);
510 /* adjust width & height */
512 height
= pix
->height
;
513 v4l_bound_align_image
516 dec
->max_width
? dec
->max_width
: DELTA_MAX_WIDTH
,
520 dec
->max_height
? dec
->max_height
: DELTA_MAX_HEIGHT
,
523 if ((pix
->width
!= width
) || (pix
->height
!= height
))
525 "%s V4L2 TRY_FMT (OUTPUT): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
526 ctx
->name
, width
, height
,
527 pix
->width
, pix
->height
);
529 au_size
= estimated_au_size(pix
->width
, pix
->height
);
530 if (pix
->sizeimage
< au_size
) {
532 "%s V4L2 TRY_FMT (OUTPUT): size updated %d -> %d to fit estimated size\n",
533 ctx
->name
, pix
->sizeimage
, au_size
);
534 pix
->sizeimage
= au_size
;
537 pix
->bytesperline
= 0;
539 if (pix
->field
== V4L2_FIELD_ANY
)
540 pix
->field
= V4L2_FIELD_NONE
;
545 static int delta_try_fmt_frame(struct file
*file
, void *priv
,
546 struct v4l2_format
*f
)
548 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
549 struct delta_dev
*delta
= ctx
->dev
;
550 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
551 u32 pixelformat
= pix
->pixelformat
;
552 const struct delta_dec
*dec
;
555 dec
= delta_find_decoder(ctx
, ctx
->streaminfo
.streamformat
,
559 "%s V4L2 TRY_FMT (CAPTURE): unsupported format %4.4s\n",
560 ctx
->name
, (char *)&pixelformat
);
564 /* adjust width & height */
566 height
= pix
->height
;
567 v4l_bound_align_image(&pix
->width
,
568 DELTA_MIN_WIDTH
, DELTA_MAX_WIDTH
,
569 frame_alignment(pixelformat
) - 1,
571 DELTA_MIN_HEIGHT
, DELTA_MAX_HEIGHT
,
572 frame_alignment(pixelformat
) - 1, 0);
574 if ((pix
->width
!= width
) || (pix
->height
!= height
))
576 "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
577 ctx
->name
, width
, height
, pix
->width
, pix
->height
);
579 /* default decoder alignment constraint */
580 width
= ALIGN(pix
->width
, DELTA_WIDTH_ALIGNMENT
);
581 height
= ALIGN(pix
->height
, DELTA_HEIGHT_ALIGNMENT
);
582 if ((pix
->width
!= width
) || (pix
->height
!= height
))
584 "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit decoder alignment\n",
585 ctx
->name
, width
, height
, pix
->width
, pix
->height
);
587 if (!pix
->colorspace
) {
588 pix
->colorspace
= V4L2_COLORSPACE_REC709
;
589 pix
->xfer_func
= V4L2_XFER_FUNC_DEFAULT
;
590 pix
->ycbcr_enc
= V4L2_YCBCR_ENC_DEFAULT
;
591 pix
->quantization
= V4L2_QUANTIZATION_DEFAULT
;
595 pix
->height
= height
;
596 pix
->bytesperline
= frame_stride(pix
->width
, pixelformat
);
597 pix
->sizeimage
= frame_size(pix
->width
, pix
->height
, pixelformat
);
599 if (pix
->field
== V4L2_FIELD_ANY
)
600 pix
->field
= V4L2_FIELD_NONE
;
605 static int delta_s_fmt_stream(struct file
*file
, void *fh
,
606 struct v4l2_format
*f
)
608 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
609 struct delta_dev
*delta
= ctx
->dev
;
610 struct vb2_queue
*vq
;
611 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
614 ret
= delta_try_fmt_stream(file
, fh
, f
);
617 "%s V4L2 S_FMT (OUTPUT): unsupported format %4.4s\n",
618 ctx
->name
, (char *)&pix
->pixelformat
);
622 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
623 if (vb2_is_streaming(vq
)) {
624 dev_dbg(delta
->dev
, "%s V4L2 S_FMT (OUTPUT): queue busy\n",
629 ctx
->max_au_size
= pix
->sizeimage
;
630 ctx
->streaminfo
.width
= pix
->width
;
631 ctx
->streaminfo
.height
= pix
->height
;
632 ctx
->streaminfo
.streamformat
= pix
->pixelformat
;
633 ctx
->streaminfo
.colorspace
= pix
->colorspace
;
634 ctx
->streaminfo
.xfer_func
= pix
->xfer_func
;
635 ctx
->streaminfo
.ycbcr_enc
= pix
->ycbcr_enc
;
636 ctx
->streaminfo
.quantization
= pix
->quantization
;
637 ctx
->flags
|= DELTA_FLAG_STREAMINFO
;
642 static int delta_s_fmt_frame(struct file
*file
, void *fh
, struct v4l2_format
*f
)
644 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
645 struct delta_dev
*delta
= ctx
->dev
;
646 const struct delta_dec
*dec
= ctx
->dec
;
647 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
648 struct delta_frameinfo frameinfo
;
649 unsigned char str
[100] = "";
650 struct vb2_queue
*vq
;
653 vq
= v4l2_m2m_get_vq(ctx
->fh
.m2m_ctx
, f
->type
);
654 if (vb2_is_streaming(vq
)) {
655 dev_dbg(delta
->dev
, "%s V4L2 S_FMT (CAPTURE): queue busy\n",
660 if (ctx
->state
< DELTA_STATE_READY
) {
662 * decoder not yet opened and valid stream header not found,
663 * could not negotiate format with decoder, check at least
664 * pixel format & negotiate resolution boundaries
667 ret
= delta_try_fmt_frame(file
, fh
, f
);
670 "%s V4L2 S_FMT (CAPTURE): unsupported format %4.4s\n",
671 ctx
->name
, (char *)&pix
->pixelformat
);
678 /* set frame information to decoder */
679 memset(&frameinfo
, 0, sizeof(frameinfo
));
680 frameinfo
.pixelformat
= pix
->pixelformat
;
681 frameinfo
.width
= pix
->width
;
682 frameinfo
.height
= pix
->height
;
683 frameinfo
.aligned_width
= pix
->width
;
684 frameinfo
.aligned_height
= pix
->height
;
685 frameinfo
.size
= pix
->sizeimage
;
686 frameinfo
.field
= pix
->field
;
687 frameinfo
.colorspace
= pix
->colorspace
;
688 frameinfo
.xfer_func
= pix
->xfer_func
;
689 frameinfo
.ycbcr_enc
= pix
->ycbcr_enc
;
690 frameinfo
.quantization
= pix
->quantization
;
691 ret
= call_dec_op(dec
, set_frameinfo
, ctx
, &frameinfo
);
695 /* then get what decoder can really do */
696 ret
= call_dec_op(dec
, get_frameinfo
, ctx
, &frameinfo
);
700 ctx
->flags
|= DELTA_FLAG_FRAMEINFO
;
701 ctx
->frameinfo
= frameinfo
;
703 "%s V4L2 SET_FMT (CAPTURE): frameinfo updated to %s\n",
705 delta_frameinfo_str(&frameinfo
, str
, sizeof(str
)));
707 pix
->pixelformat
= frameinfo
.pixelformat
;
708 pix
->width
= frameinfo
.aligned_width
;
709 pix
->height
= frameinfo
.aligned_height
;
710 pix
->bytesperline
= frame_stride(pix
->width
, pix
->pixelformat
);
711 pix
->sizeimage
= frameinfo
.size
;
712 pix
->field
= frameinfo
.field
;
713 pix
->colorspace
= frameinfo
.colorspace
;
714 pix
->xfer_func
= frameinfo
.xfer_func
;
715 pix
->ycbcr_enc
= frameinfo
.ycbcr_enc
;
716 pix
->quantization
= frameinfo
.quantization
;
721 static int delta_g_selection(struct file
*file
, void *fh
,
722 struct v4l2_selection
*s
)
724 struct delta_ctx
*ctx
= to_ctx(fh
);
725 struct delta_frameinfo
*frameinfo
= &ctx
->frameinfo
;
726 struct v4l2_rect crop
;
728 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
731 if ((ctx
->flags
& DELTA_FLAG_FRAMEINFO
) &&
732 (frameinfo
->flags
& DELTA_FRAMEINFO_FLAG_CROP
)) {
733 crop
= frameinfo
->crop
;
735 /* default to video dimensions */
738 crop
.width
= frameinfo
->width
;
739 crop
.height
= frameinfo
->height
;
743 case V4L2_SEL_TGT_COMPOSE
:
744 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
745 /* visible area inside video */
748 case V4L2_SEL_TGT_COMPOSE_PADDED
:
749 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
750 /* up to aligned dimensions */
753 s
->r
.width
= frameinfo
->aligned_width
;
754 s
->r
.height
= frameinfo
->aligned_height
;
763 static void delta_complete_eos(struct delta_ctx
*ctx
,
764 struct delta_frame
*frame
)
766 struct delta_dev
*delta
= ctx
->dev
;
767 const struct v4l2_event ev
= {.type
= V4L2_EVENT_EOS
};
771 * - by returning an empty frame flagged to V4L2_BUF_FLAG_LAST
772 * - and then send EOS event
776 frame
->info
.size
= 0;
778 /* set the last buffer flag */
779 frame
->flags
|= V4L2_BUF_FLAG_LAST
;
781 /* release frame to user */
782 delta_frame_done(ctx
, frame
, 0);
785 v4l2_event_queue_fh(&ctx
->fh
, &ev
);
787 dev_dbg(delta
->dev
, "%s EOS completed\n", ctx
->name
);
790 static int delta_try_decoder_cmd(struct file
*file
, void *fh
,
791 struct v4l2_decoder_cmd
*cmd
)
793 if (cmd
->cmd
!= V4L2_DEC_CMD_STOP
)
796 if (cmd
->flags
& V4L2_DEC_CMD_STOP_TO_BLACK
)
799 if (!(cmd
->flags
& V4L2_DEC_CMD_STOP_IMMEDIATELY
) &&
800 (cmd
->stop
.pts
!= 0))
806 static int delta_decoder_stop_cmd(struct delta_ctx
*ctx
, void *fh
)
808 const struct delta_dec
*dec
= ctx
->dec
;
809 struct delta_dev
*delta
= ctx
->dev
;
810 struct delta_frame
*frame
= NULL
;
813 dev_dbg(delta
->dev
, "%s EOS received\n", ctx
->name
);
815 if (ctx
->state
!= DELTA_STATE_READY
)
818 /* drain the decoder */
819 call_dec_op(dec
, drain
, ctx
);
821 /* release to user drained frames */
824 ret
= call_dec_op(dec
, get_frame
, ctx
, &frame
);
825 if (ret
== -ENODATA
) {
826 /* no more decoded frames */
830 dev_dbg(delta
->dev
, "%s drain frame[%d]\n",
831 ctx
->name
, frame
->index
);
833 /* pop timestamp and mark frame with it */
834 delta_pop_dts(ctx
, &frame
->dts
);
836 /* release decoded frame to user */
837 delta_frame_done(ctx
, frame
, 0);
841 /* try to complete EOS */
842 ret
= delta_get_free_frame(ctx
, &frame
);
846 /* new frame available, EOS can now be completed */
847 delta_complete_eos(ctx
, frame
);
849 ctx
->state
= DELTA_STATE_EOS
;
855 * EOS completion from driver is delayed because
856 * we don't have a free empty frame available.
857 * EOS completion is so delayed till next frame_queue() call
858 * to be sure to have a free empty frame available.
860 ctx
->state
= DELTA_STATE_WF_EOS
;
861 dev_dbg(delta
->dev
, "%s EOS delayed\n", ctx
->name
);
866 static int delta_decoder_cmd(struct file
*file
, void *fh
,
867 struct v4l2_decoder_cmd
*cmd
)
869 struct delta_ctx
*ctx
= to_ctx(fh
);
872 ret
= delta_try_decoder_cmd(file
, fh
, cmd
);
876 return delta_decoder_stop_cmd(ctx
, fh
);
879 static int delta_subscribe_event(struct v4l2_fh
*fh
,
880 const struct v4l2_event_subscription
*sub
)
884 return v4l2_event_subscribe(fh
, sub
, 2, NULL
);
893 static const struct v4l2_ioctl_ops delta_ioctl_ops
= {
894 .vidioc_querycap
= delta_querycap
,
895 .vidioc_enum_fmt_vid_cap
= delta_enum_fmt_frame
,
896 .vidioc_g_fmt_vid_cap
= delta_g_fmt_frame
,
897 .vidioc_try_fmt_vid_cap
= delta_try_fmt_frame
,
898 .vidioc_s_fmt_vid_cap
= delta_s_fmt_frame
,
899 .vidioc_enum_fmt_vid_out
= delta_enum_fmt_stream
,
900 .vidioc_g_fmt_vid_out
= delta_g_fmt_stream
,
901 .vidioc_try_fmt_vid_out
= delta_try_fmt_stream
,
902 .vidioc_s_fmt_vid_out
= delta_s_fmt_stream
,
903 .vidioc_reqbufs
= v4l2_m2m_ioctl_reqbufs
,
904 .vidioc_create_bufs
= v4l2_m2m_ioctl_create_bufs
,
905 .vidioc_querybuf
= v4l2_m2m_ioctl_querybuf
,
906 .vidioc_expbuf
= v4l2_m2m_ioctl_expbuf
,
907 .vidioc_qbuf
= v4l2_m2m_ioctl_qbuf
,
908 .vidioc_dqbuf
= v4l2_m2m_ioctl_dqbuf
,
909 .vidioc_streamon
= v4l2_m2m_ioctl_streamon
,
910 .vidioc_streamoff
= v4l2_m2m_ioctl_streamoff
,
911 .vidioc_g_selection
= delta_g_selection
,
912 .vidioc_try_decoder_cmd
= delta_try_decoder_cmd
,
913 .vidioc_decoder_cmd
= delta_decoder_cmd
,
914 .vidioc_subscribe_event
= delta_subscribe_event
,
915 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
919 * mem-to-mem operations
922 static void delta_run_work(struct work_struct
*work
)
924 struct delta_ctx
*ctx
= container_of(work
, struct delta_ctx
, run_work
);
925 struct delta_dev
*delta
= ctx
->dev
;
926 const struct delta_dec
*dec
= ctx
->dec
;
928 struct delta_frame
*frame
= NULL
;
930 bool discard
= false;
931 struct vb2_v4l2_buffer
*vbuf
;
934 dev_err(delta
->dev
, "%s no decoder opened yet\n", ctx
->name
);
938 /* protect instance against reentrancy */
939 mutex_lock(&ctx
->lock
);
941 vbuf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
);
943 dev_err(delta
->dev
, "%s no buffer to decode\n", ctx
->name
);
944 mutex_unlock(&ctx
->lock
);
948 au
->size
= vb2_get_plane_payload(&vbuf
->vb2_buf
, 0);
949 au
->dts
= vbuf
->vb2_buf
.timestamp
;
951 /* dump access unit */
954 /* enable the hardware */
956 ret
= delta_get_sync(ctx
);
961 /* decode this access unit */
962 ret
= call_dec_op(dec
, decode
, ctx
, au
);
965 * if the (-ENODATA) value is returned, it refers to the interlaced
966 * stream case for which 2 access units are needed to get 1 frame.
967 * So, this returned value doesn't mean that the decoding fails, but
968 * indicates that the timestamp information of the access unit shall
969 * not be taken into account, and that the V4L2 buffer associated with
970 * the access unit shall be flagged with V4L2_BUF_FLAG_ERROR to inform
971 * the user of this situation
973 if (ret
== -ENODATA
) {
976 dev_err(delta
->dev
, "%s decoding failed (%d)\n",
979 /* disable the hardware */
981 delta_put_autosuspend(ctx
);
986 /* disable the hardware */
988 delta_put_autosuspend(ctx
);
990 /* push au timestamp in FIFO */
992 delta_push_dts(ctx
, au
->dts
);
994 /* get available decoded frames */
996 ret
= call_dec_op(dec
, get_frame
, ctx
, &frame
);
997 if (ret
== -ENODATA
) {
998 /* no more decoded frames */
1002 dev_err(delta
->dev
, "%s cannot get decoded frame (%d)\n",
1008 "%s NULL decoded frame\n",
1014 /* pop timestamp and mark frame with it */
1015 delta_pop_dts(ctx
, &frame
->dts
);
1017 /* release decoded frame to user */
1018 delta_frame_done(ctx
, frame
, 0);
1022 requeue_free_frames(ctx
);
1023 delta_au_done(ctx
, au
, (discard
? -ENODATA
: 0));
1024 mutex_unlock(&ctx
->lock
);
1025 v4l2_m2m_job_finish(delta
->m2m_dev
, ctx
->fh
.m2m_ctx
);
1029 requeue_free_frames(ctx
);
1030 delta_au_done(ctx
, au
, ret
);
1031 mutex_unlock(&ctx
->lock
);
1032 v4l2_m2m_job_finish(delta
->m2m_dev
, ctx
->fh
.m2m_ctx
);
1035 static void delta_device_run(void *priv
)
1037 struct delta_ctx
*ctx
= priv
;
1038 struct delta_dev
*delta
= ctx
->dev
;
1040 queue_work(delta
->work_queue
, &ctx
->run_work
);
1043 static void delta_job_abort(void *priv
)
1045 struct delta_ctx
*ctx
= priv
;
1046 struct delta_dev
*delta
= ctx
->dev
;
1048 dev_dbg(delta
->dev
, "%s aborting job\n", ctx
->name
);
1050 ctx
->aborting
= true;
1053 static int delta_job_ready(void *priv
)
1055 struct delta_ctx
*ctx
= priv
;
1056 struct delta_dev
*delta
= ctx
->dev
;
1057 int src_bufs
= v4l2_m2m_num_src_bufs_ready(ctx
->fh
.m2m_ctx
);
1060 dev_dbg(delta
->dev
, "%s not ready: not enough video buffers.\n",
1065 if (!v4l2_m2m_num_dst_bufs_ready(ctx
->fh
.m2m_ctx
)) {
1066 dev_dbg(delta
->dev
, "%s not ready: not enough video capture buffers.\n",
1071 if (ctx
->aborting
) {
1072 dev_dbg(delta
->dev
, "%s job not ready: aborting\n", ctx
->name
);
1076 dev_dbg(delta
->dev
, "%s job ready\n", ctx
->name
);
1081 /* mem-to-mem ops */
1082 static const struct v4l2_m2m_ops delta_m2m_ops
= {
1083 .device_run
= delta_device_run
,
1084 .job_ready
= delta_job_ready
,
1085 .job_abort
= delta_job_abort
,
1089 * VB2 queue operations
1092 static int delta_vb2_au_queue_setup(struct vb2_queue
*vq
,
1093 unsigned int *num_buffers
,
1094 unsigned int *num_planes
,
1095 unsigned int sizes
[],
1096 struct device
*alloc_devs
[])
1098 struct delta_ctx
*ctx
= vb2_get_drv_priv(vq
);
1099 unsigned int size
= ctx
->max_au_size
;
1102 return sizes
[0] < size
? -EINVAL
: 0;
1105 if (*num_buffers
< 1)
1107 if (*num_buffers
> DELTA_MAX_AUS
)
1108 *num_buffers
= DELTA_MAX_AUS
;
1115 static int delta_vb2_au_prepare(struct vb2_buffer
*vb
)
1117 struct vb2_queue
*q
= vb
->vb2_queue
;
1118 struct delta_ctx
*ctx
= vb2_get_drv_priv(q
);
1119 struct delta_dev
*delta
= ctx
->dev
;
1120 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1121 struct delta_au
*au
= to_au(vbuf
);
1123 if (!au
->prepared
) {
1124 /* get memory addresses */
1125 au
->vaddr
= vb2_plane_vaddr(&au
->vbuf
.vb2_buf
, 0);
1126 au
->paddr
= vb2_dma_contig_plane_dma_addr
1127 (&au
->vbuf
.vb2_buf
, 0);
1128 au
->prepared
= true;
1129 dev_dbg(delta
->dev
, "%s au[%d] prepared; virt=0x%p, phy=0x%pad\n",
1130 ctx
->name
, vb
->index
, au
->vaddr
, &au
->paddr
);
1133 if (vbuf
->field
== V4L2_FIELD_ANY
)
1134 vbuf
->field
= V4L2_FIELD_NONE
;
1139 static int delta_setup_frame(struct delta_ctx
*ctx
,
1140 struct delta_frame
*frame
)
1142 struct delta_dev
*delta
= ctx
->dev
;
1143 const struct delta_dec
*dec
= ctx
->dec
;
1145 if (frame
->index
>= DELTA_MAX_FRAMES
) {
1147 "%s frame index=%d exceeds output frame count (%d)\n",
1148 ctx
->name
, frame
->index
, DELTA_MAX_FRAMES
);
1152 if (ctx
->nb_of_frames
>= DELTA_MAX_FRAMES
) {
1154 "%s number of frames exceeds output frame count (%d > %d)\n",
1155 ctx
->name
, ctx
->nb_of_frames
, DELTA_MAX_FRAMES
);
1159 if (frame
->index
!= ctx
->nb_of_frames
) {
1160 dev_warn(delta
->dev
,
1161 "%s frame index discontinuity detected, expected %d, got %d\n",
1162 ctx
->name
, ctx
->nb_of_frames
, frame
->index
);
1165 frame
->state
= DELTA_FRAME_FREE
;
1166 ctx
->frames
[ctx
->nb_of_frames
] = frame
;
1167 ctx
->nb_of_frames
++;
1169 /* setup frame on decoder side */
1170 return call_dec_op(dec
, setup_frame
, ctx
, frame
);
1174 * default implementation of get_frameinfo decoder ops
1175 * matching frame information from stream information
1176 * & with default pixel format & default alignment.
1178 int delta_get_frameinfo_default(struct delta_ctx
*ctx
,
1179 struct delta_frameinfo
*frameinfo
)
1181 struct delta_streaminfo
*streaminfo
= &ctx
->streaminfo
;
1183 memset(frameinfo
, 0, sizeof(*frameinfo
));
1184 frameinfo
->pixelformat
= V4L2_PIX_FMT_NV12
;
1185 frameinfo
->width
= streaminfo
->width
;
1186 frameinfo
->height
= streaminfo
->height
;
1187 frameinfo
->aligned_width
= ALIGN(streaminfo
->width
,
1188 DELTA_WIDTH_ALIGNMENT
);
1189 frameinfo
->aligned_height
= ALIGN(streaminfo
->height
,
1190 DELTA_HEIGHT_ALIGNMENT
);
1191 frameinfo
->size
= frame_size(frameinfo
->aligned_width
,
1192 frameinfo
->aligned_height
,
1193 frameinfo
->pixelformat
);
1194 if (streaminfo
->flags
& DELTA_STREAMINFO_FLAG_CROP
) {
1195 frameinfo
->flags
|= DELTA_FRAMEINFO_FLAG_CROP
;
1196 frameinfo
->crop
= streaminfo
->crop
;
1198 if (streaminfo
->flags
& DELTA_STREAMINFO_FLAG_PIXELASPECT
) {
1199 frameinfo
->flags
|= DELTA_FRAMEINFO_FLAG_PIXELASPECT
;
1200 frameinfo
->pixelaspect
= streaminfo
->pixelaspect
;
1202 frameinfo
->field
= streaminfo
->field
;
1208 * default implementation of recycle decoder ops
1209 * consisting to relax the "decoded" frame state
1211 int delta_recycle_default(struct delta_ctx
*pctx
,
1212 struct delta_frame
*frame
)
1214 frame
->state
&= ~DELTA_FRAME_DEC
;
1219 static void dump_frames_status(struct delta_ctx
*ctx
)
1221 struct delta_dev
*delta
= ctx
->dev
;
1223 struct delta_frame
*frame
;
1224 unsigned char str
[100] = "";
1226 dev_info(delta
->dev
,
1227 "%s dumping frames status...\n", ctx
->name
);
1229 for (i
= 0; i
< ctx
->nb_of_frames
; i
++) {
1230 frame
= ctx
->frames
[i
];
1231 dev_info(delta
->dev
,
1232 "%s frame[%d] %s\n",
1233 ctx
->name
, frame
->index
,
1234 frame_state_str(frame
->state
,
1239 int delta_get_free_frame(struct delta_ctx
*ctx
,
1240 struct delta_frame
**pframe
)
1242 struct delta_dev
*delta
= ctx
->dev
;
1243 struct vb2_v4l2_buffer
*vbuf
;
1244 struct delta_frame
*frame
;
1248 vbuf
= v4l2_m2m_dst_buf_remove(ctx
->fh
.m2m_ctx
);
1250 dev_err(delta
->dev
, "%s no frame available",
1255 frame
= to_frame(vbuf
);
1256 frame
->state
&= ~DELTA_FRAME_M2M
;
1257 if (frame
->state
!= DELTA_FRAME_FREE
) {
1259 "%s frame[%d] is not free\n",
1260 ctx
->name
, frame
->index
);
1261 dump_frames_status(ctx
);
1266 "%s get free frame[%d]\n", ctx
->name
, frame
->index
);
1272 int delta_get_sync(struct delta_ctx
*ctx
)
1274 struct delta_dev
*delta
= ctx
->dev
;
1277 /* enable the hardware */
1278 ret
= pm_runtime_get_sync(delta
->dev
);
1280 dev_err(delta
->dev
, "%s pm_runtime_get_sync failed (%d)\n",
1288 void delta_put_autosuspend(struct delta_ctx
*ctx
)
1290 struct delta_dev
*delta
= ctx
->dev
;
1292 pm_runtime_put_autosuspend(delta
->dev
);
1295 static void delta_vb2_au_queue(struct vb2_buffer
*vb
)
1297 struct vb2_queue
*q
= vb
->vb2_queue
;
1298 struct delta_ctx
*ctx
= vb2_get_drv_priv(q
);
1299 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1301 v4l2_m2m_buf_queue(ctx
->fh
.m2m_ctx
, vbuf
);
1304 static int delta_vb2_au_start_streaming(struct vb2_queue
*q
,
1307 struct delta_ctx
*ctx
= vb2_get_drv_priv(q
);
1308 struct delta_dev
*delta
= ctx
->dev
;
1309 const struct delta_dec
*dec
= ctx
->dec
;
1310 struct delta_au
*au
;
1312 struct vb2_v4l2_buffer
*vbuf
= NULL
;
1313 struct delta_streaminfo
*streaminfo
= &ctx
->streaminfo
;
1314 struct delta_frameinfo
*frameinfo
= &ctx
->frameinfo
;
1315 unsigned char str1
[100] = "";
1316 unsigned char str2
[100] = "";
1318 if ((ctx
->state
!= DELTA_STATE_WF_FORMAT
) &&
1319 (ctx
->state
!= DELTA_STATE_WF_STREAMINFO
))
1322 if (ctx
->state
== DELTA_STATE_WF_FORMAT
) {
1323 /* open decoder if not yet done */
1324 ret
= delta_open_decoder(ctx
,
1325 ctx
->streaminfo
.streamformat
,
1326 ctx
->frameinfo
.pixelformat
, &dec
);
1330 ctx
->state
= DELTA_STATE_WF_STREAMINFO
;
1334 * first buffer should contain stream header,
1335 * decode it to get the infos related to stream
1336 * such as width, height, dpb, ...
1338 vbuf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
);
1340 dev_err(delta
->dev
, "%s failed to start streaming, no stream header buffer enqueued\n",
1346 au
->size
= vb2_get_plane_payload(&vbuf
->vb2_buf
, 0);
1347 au
->dts
= vbuf
->vb2_buf
.timestamp
;
1349 delta_push_dts(ctx
, au
->dts
);
1351 /* dump access unit */
1354 /* decode this access unit */
1355 ret
= call_dec_op(dec
, decode
, ctx
, au
);
1357 dev_err(delta
->dev
, "%s failed to start streaming, header decoding failed (%d)\n",
1362 ret
= call_dec_op(dec
, get_streaminfo
, ctx
, streaminfo
);
1364 dev_dbg_ratelimited(delta
->dev
,
1365 "%s failed to start streaming, valid stream header not yet decoded\n",
1369 ctx
->flags
|= DELTA_FLAG_STREAMINFO
;
1371 ret
= call_dec_op(dec
, get_frameinfo
, ctx
, frameinfo
);
1374 ctx
->flags
|= DELTA_FLAG_FRAMEINFO
;
1376 ctx
->state
= DELTA_STATE_READY
;
1378 dev_dbg(delta
->dev
, "%s %s => %s\n", ctx
->name
,
1379 delta_streaminfo_str(streaminfo
, str1
, sizeof(str1
)),
1380 delta_frameinfo_str(frameinfo
, str2
, sizeof(str2
)));
1382 delta_au_done(ctx
, au
, ret
);
1387 * return all buffers to vb2 in QUEUED state.
1388 * This will give ownership back to userspace
1391 v4l2_m2m_buf_done(vbuf
, VB2_BUF_STATE_QUEUED
);
1393 while ((vbuf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
)))
1394 v4l2_m2m_buf_done(vbuf
, VB2_BUF_STATE_QUEUED
);
1398 static void delta_vb2_au_stop_streaming(struct vb2_queue
*q
)
1400 struct delta_ctx
*ctx
= vb2_get_drv_priv(q
);
1401 struct vb2_v4l2_buffer
*vbuf
;
1403 delta_flush_dts(ctx
);
1405 /* return all buffers to vb2 in ERROR state */
1406 while ((vbuf
= v4l2_m2m_src_buf_remove(ctx
->fh
.m2m_ctx
)))
1407 v4l2_m2m_buf_done(vbuf
, VB2_BUF_STATE_ERROR
);
1411 ctx
->aborting
= false;
1414 static int delta_vb2_frame_queue_setup(struct vb2_queue
*vq
,
1415 unsigned int *num_buffers
,
1416 unsigned int *num_planes
,
1417 unsigned int sizes
[],
1418 struct device
*alloc_devs
[])
1420 struct delta_ctx
*ctx
= vb2_get_drv_priv(vq
);
1421 struct delta_dev
*delta
= ctx
->dev
;
1422 struct delta_streaminfo
*streaminfo
= &ctx
->streaminfo
;
1423 struct delta_frameinfo
*frameinfo
= &ctx
->frameinfo
;
1424 unsigned int size
= frameinfo
->size
;
1427 * the number of output buffers needed for decoding =
1428 * user need (*num_buffers given, usually for display pipeline) +
1429 * stream need (streaminfo->dpb) +
1430 * decoding peak smoothing (depends on DELTA IP perf)
1432 if (*num_buffers
< DELTA_MIN_FRAME_USER
) {
1434 "%s num_buffers too low (%d), increasing to %d\n",
1435 ctx
->name
, *num_buffers
, DELTA_MIN_FRAME_USER
);
1436 *num_buffers
= DELTA_MIN_FRAME_USER
;
1439 *num_buffers
+= streaminfo
->dpb
+ DELTA_PEAK_FRAME_SMOOTHING
;
1441 if (*num_buffers
> DELTA_MAX_FRAMES
) {
1443 "%s output frame count too high (%d), cut to %d\n",
1444 ctx
->name
, *num_buffers
, DELTA_MAX_FRAMES
);
1445 *num_buffers
= DELTA_MAX_FRAMES
;
1449 return sizes
[0] < size
? -EINVAL
: 0;
1451 /* single plane for Y and CbCr */
1456 ctx
->nb_of_frames
= 0;
1461 static int delta_vb2_frame_prepare(struct vb2_buffer
*vb
)
1463 struct vb2_queue
*q
= vb
->vb2_queue
;
1464 struct delta_ctx
*ctx
= vb2_get_drv_priv(q
);
1465 struct delta_dev
*delta
= ctx
->dev
;
1466 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1467 struct delta_frame
*frame
= to_frame(vbuf
);
1470 if (!frame
->prepared
) {
1471 frame
->index
= vbuf
->vb2_buf
.index
;
1472 frame
->vaddr
= vb2_plane_vaddr(&vbuf
->vb2_buf
, 0);
1473 frame
->paddr
= vb2_dma_contig_plane_dma_addr(&vbuf
->vb2_buf
, 0);
1474 frame
->info
= ctx
->frameinfo
;
1476 ret
= delta_setup_frame(ctx
, frame
);
1479 "%s setup_frame() failed (%d)\n",
1483 frame
->prepared
= true;
1485 "%s frame[%d] prepared; virt=0x%p, phy=0x%pad\n",
1486 ctx
->name
, vb
->index
, frame
->vaddr
,
1490 frame
->flags
= vbuf
->flags
;
1495 static void delta_vb2_frame_finish(struct vb2_buffer
*vb
)
1497 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1498 struct delta_frame
*frame
= to_frame(vbuf
);
1500 /* update V4L2 fields for user */
1501 vb2_set_plane_payload(&vbuf
->vb2_buf
, 0, frame
->info
.size
);
1502 vb
->timestamp
= frame
->dts
;
1503 vbuf
->field
= frame
->field
;
1504 vbuf
->flags
= frame
->flags
;
1507 static void delta_vb2_frame_queue(struct vb2_buffer
*vb
)
1509 struct vb2_queue
*q
= vb
->vb2_queue
;
1510 struct delta_ctx
*ctx
= vb2_get_drv_priv(q
);
1511 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1512 struct delta_frame
*frame
= to_frame(vbuf
);
1514 if (ctx
->state
== DELTA_STATE_WF_EOS
) {
1515 /* new frame available, EOS can now be completed */
1516 delta_complete_eos(ctx
, frame
);
1518 ctx
->state
= DELTA_STATE_EOS
;
1520 /* return, no need to recycle this buffer to decoder */
1524 /* recycle this frame */
1525 delta_recycle(ctx
, frame
);
1528 static void delta_vb2_frame_stop_streaming(struct vb2_queue
*q
)
1530 struct delta_ctx
*ctx
= vb2_get_drv_priv(q
);
1531 struct vb2_v4l2_buffer
*vbuf
;
1532 struct delta_frame
*frame
;
1533 const struct delta_dec
*dec
= ctx
->dec
;
1536 delta_flush_dts(ctx
);
1538 call_dec_op(dec
, flush
, ctx
);
1541 * return all buffers to vb2 in ERROR state
1542 * & reset each frame state to OUT
1544 for (i
= 0; i
< ctx
->nb_of_frames
; i
++) {
1545 frame
= ctx
->frames
[i
];
1546 if (!(frame
->state
& DELTA_FRAME_OUT
)) {
1547 vbuf
= &frame
->vbuf
;
1548 v4l2_m2m_buf_done(vbuf
, VB2_BUF_STATE_ERROR
);
1550 frame
->state
= DELTA_FRAME_OUT
;
1555 ctx
->aborting
= false;
1559 static const struct vb2_ops delta_vb2_au_ops
= {
1560 .queue_setup
= delta_vb2_au_queue_setup
,
1561 .buf_prepare
= delta_vb2_au_prepare
,
1562 .buf_queue
= delta_vb2_au_queue
,
1563 .wait_prepare
= vb2_ops_wait_prepare
,
1564 .wait_finish
= vb2_ops_wait_finish
,
1565 .start_streaming
= delta_vb2_au_start_streaming
,
1566 .stop_streaming
= delta_vb2_au_stop_streaming
,
1569 static const struct vb2_ops delta_vb2_frame_ops
= {
1570 .queue_setup
= delta_vb2_frame_queue_setup
,
1571 .buf_prepare
= delta_vb2_frame_prepare
,
1572 .buf_finish
= delta_vb2_frame_finish
,
1573 .buf_queue
= delta_vb2_frame_queue
,
1574 .wait_prepare
= vb2_ops_wait_prepare
,
1575 .wait_finish
= vb2_ops_wait_finish
,
1576 .stop_streaming
= delta_vb2_frame_stop_streaming
,
1580 * V4L2 file operations
1583 static int queue_init(void *priv
,
1584 struct vb2_queue
*src_vq
, struct vb2_queue
*dst_vq
)
1586 struct vb2_queue
*q
;
1587 struct delta_ctx
*ctx
= priv
;
1588 struct delta_dev
*delta
= ctx
->dev
;
1591 /* setup vb2 queue for stream input */
1593 q
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1594 q
->io_modes
= VB2_MMAP
| VB2_DMABUF
;
1596 /* overload vb2 buf with private au struct */
1597 q
->buf_struct_size
= sizeof(struct delta_au
);
1598 q
->ops
= &delta_vb2_au_ops
;
1599 q
->mem_ops
= &vb2_dma_contig_memops
;
1600 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
1601 q
->lock
= &delta
->lock
;
1602 q
->dev
= delta
->dev
;
1604 ret
= vb2_queue_init(q
);
1608 /* setup vb2 queue for frame output */
1610 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1611 q
->io_modes
= VB2_MMAP
| VB2_DMABUF
;
1613 /* overload vb2 buf with private frame struct */
1614 q
->buf_struct_size
= sizeof(struct delta_frame
)
1615 + DELTA_MAX_FRAME_PRIV_SIZE
;
1616 q
->ops
= &delta_vb2_frame_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 return vb2_queue_init(q
);
1625 static int delta_open(struct file
*file
)
1627 struct delta_dev
*delta
= video_drvdata(file
);
1628 struct delta_ctx
*ctx
= NULL
;
1631 mutex_lock(&delta
->lock
);
1633 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
1640 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
1641 file
->private_data
= &ctx
->fh
;
1642 v4l2_fh_add(&ctx
->fh
);
1644 INIT_WORK(&ctx
->run_work
, delta_run_work
);
1645 mutex_init(&ctx
->lock
);
1647 ctx
->fh
.m2m_ctx
= v4l2_m2m_ctx_init(delta
->m2m_dev
, ctx
,
1649 if (IS_ERR(ctx
->fh
.m2m_ctx
)) {
1650 ret
= PTR_ERR(ctx
->fh
.m2m_ctx
);
1651 dev_err(delta
->dev
, "%s failed to initialize m2m context (%d)\n",
1657 * wait stream format to determine which
1660 ctx
->state
= DELTA_STATE_WF_FORMAT
;
1662 INIT_LIST_HEAD(&ctx
->dts
);
1664 /* set the instance name */
1665 delta
->instance_id
++;
1666 snprintf(ctx
->name
, sizeof(ctx
->name
), "[%3d:----]",
1667 delta
->instance_id
);
1669 /* default parameters for frame and stream */
1670 set_default_params(ctx
);
1672 /* enable ST231 clocks */
1673 if (delta
->clk_st231
)
1674 if (clk_prepare_enable(delta
->clk_st231
))
1675 dev_warn(delta
->dev
, "failed to enable st231 clk\n");
1677 /* enable FLASH_PROMIP clock */
1678 if (delta
->clk_flash_promip
)
1679 if (clk_prepare_enable(delta
->clk_flash_promip
))
1680 dev_warn(delta
->dev
, "failed to enable delta promip clk\n");
1682 mutex_unlock(&delta
->lock
);
1684 dev_dbg(delta
->dev
, "%s decoder instance created\n", ctx
->name
);
1689 v4l2_fh_del(&ctx
->fh
);
1690 v4l2_fh_exit(&ctx
->fh
);
1693 mutex_unlock(&delta
->lock
);
1698 static int delta_release(struct file
*file
)
1700 struct delta_ctx
*ctx
= to_ctx(file
->private_data
);
1701 struct delta_dev
*delta
= ctx
->dev
;
1702 const struct delta_dec
*dec
= ctx
->dec
;
1704 mutex_lock(&delta
->lock
);
1707 call_dec_op(dec
, close
, ctx
);
1710 * trace a summary of instance
1711 * before closing (debug purpose)
1713 delta_trace_summary(ctx
);
1715 v4l2_m2m_ctx_release(ctx
->fh
.m2m_ctx
);
1717 v4l2_fh_del(&ctx
->fh
);
1718 v4l2_fh_exit(&ctx
->fh
);
1720 /* disable ST231 clocks */
1721 if (delta
->clk_st231
)
1722 clk_disable_unprepare(delta
->clk_st231
);
1724 /* disable FLASH_PROMIP clock */
1725 if (delta
->clk_flash_promip
)
1726 clk_disable_unprepare(delta
->clk_flash_promip
);
1728 dev_dbg(delta
->dev
, "%s decoder instance released\n", ctx
->name
);
1732 mutex_unlock(&delta
->lock
);
1737 static const struct v4l2_file_operations delta_fops
= {
1738 .owner
= THIS_MODULE
,
1740 .release
= delta_release
,
1741 .unlocked_ioctl
= video_ioctl2
,
1742 .mmap
= v4l2_m2m_fop_mmap
,
1743 .poll
= v4l2_m2m_fop_poll
,
1747 * Platform device operations
1750 static int delta_register_device(struct delta_dev
*delta
)
1753 struct video_device
*vdev
;
1758 delta
->m2m_dev
= v4l2_m2m_init(&delta_m2m_ops
);
1759 if (IS_ERR(delta
->m2m_dev
)) {
1760 dev_err(delta
->dev
, "%s failed to initialize v4l2-m2m device\n",
1762 ret
= PTR_ERR(delta
->m2m_dev
);
1766 vdev
= video_device_alloc();
1768 dev_err(delta
->dev
, "%s failed to allocate video device\n",
1771 goto err_m2m_release
;
1774 vdev
->fops
= &delta_fops
;
1775 vdev
->ioctl_ops
= &delta_ioctl_ops
;
1776 vdev
->release
= video_device_release
;
1777 vdev
->lock
= &delta
->lock
;
1778 vdev
->vfl_dir
= VFL_DIR_M2M
;
1779 vdev
->device_caps
= V4L2_CAP_STREAMING
| V4L2_CAP_VIDEO_M2M
;
1780 vdev
->v4l2_dev
= &delta
->v4l2_dev
;
1781 snprintf(vdev
->name
, sizeof(vdev
->name
), "%s-%s",
1782 DELTA_NAME
, DELTA_FW_VERSION
);
1784 ret
= video_register_device(vdev
, VFL_TYPE_GRABBER
, -1);
1786 dev_err(delta
->dev
, "%s failed to register video device\n",
1788 goto err_vdev_release
;
1792 video_set_drvdata(vdev
, delta
);
1796 video_device_release(vdev
);
1798 v4l2_m2m_release(delta
->m2m_dev
);
1803 static void delta_unregister_device(struct delta_dev
*delta
)
1809 v4l2_m2m_release(delta
->m2m_dev
);
1811 video_unregister_device(delta
->vdev
);
1814 static int delta_probe(struct platform_device
*pdev
)
1816 struct delta_dev
*delta
;
1817 struct device
*dev
= &pdev
->dev
;
1820 delta
= devm_kzalloc(dev
, sizeof(*delta
), GFP_KERNEL
);
1828 platform_set_drvdata(pdev
, delta
);
1830 mutex_init(&delta
->lock
);
1832 /* get clock resources */
1833 delta
->clk_delta
= devm_clk_get(dev
, "delta");
1834 if (IS_ERR(delta
->clk_delta
)) {
1835 dev_dbg(dev
, "%s can't get delta clock\n", DELTA_PREFIX
);
1836 delta
->clk_delta
= NULL
;
1839 delta
->clk_st231
= devm_clk_get(dev
, "delta-st231");
1840 if (IS_ERR(delta
->clk_st231
)) {
1841 dev_dbg(dev
, "%s can't get delta-st231 clock\n", DELTA_PREFIX
);
1842 delta
->clk_st231
= NULL
;
1845 delta
->clk_flash_promip
= devm_clk_get(dev
, "delta-flash-promip");
1846 if (IS_ERR(delta
->clk_flash_promip
)) {
1847 dev_dbg(dev
, "%s can't get delta-flash-promip clock\n",
1849 delta
->clk_flash_promip
= NULL
;
1852 /* init pm_runtime used for power management */
1853 pm_runtime_set_autosuspend_delay(dev
, DELTA_HW_AUTOSUSPEND_DELAY_MS
);
1854 pm_runtime_use_autosuspend(dev
);
1855 pm_runtime_set_suspended(dev
);
1856 pm_runtime_enable(dev
);
1858 /* init firmware ipc channel */
1859 ret
= delta_ipc_init(delta
);
1861 dev_err(delta
->dev
, "%s failed to initialize firmware ipc channel\n",
1866 /* register all available decoders */
1867 register_decoders(delta
);
1869 /* register all supported formats */
1870 register_formats(delta
);
1872 /* register on V4L2 */
1873 ret
= v4l2_device_register(dev
, &delta
->v4l2_dev
);
1875 dev_err(delta
->dev
, "%s failed to register V4L2 device\n",
1880 delta
->work_queue
= create_workqueue(DELTA_NAME
);
1881 if (!delta
->work_queue
) {
1882 dev_err(delta
->dev
, "%s failed to allocate work queue\n",
1888 /* register device */
1889 ret
= delta_register_device(delta
);
1891 goto err_work_queue
;
1893 dev_info(dev
, "%s %s registered as /dev/video%d\n",
1894 DELTA_PREFIX
, delta
->vdev
->name
, delta
->vdev
->num
);
1899 destroy_workqueue(delta
->work_queue
);
1901 v4l2_device_unregister(&delta
->v4l2_dev
);
1906 static int delta_remove(struct platform_device
*pdev
)
1908 struct delta_dev
*delta
= platform_get_drvdata(pdev
);
1910 delta_ipc_exit(delta
);
1912 delta_unregister_device(delta
);
1914 destroy_workqueue(delta
->work_queue
);
1916 pm_runtime_put_autosuspend(delta
->dev
);
1917 pm_runtime_disable(delta
->dev
);
1919 v4l2_device_unregister(&delta
->v4l2_dev
);
1924 static int delta_runtime_suspend(struct device
*dev
)
1926 struct delta_dev
*delta
= dev_get_drvdata(dev
);
1928 if (delta
->clk_delta
)
1929 clk_disable_unprepare(delta
->clk_delta
);
1934 static int delta_runtime_resume(struct device
*dev
)
1936 struct delta_dev
*delta
= dev_get_drvdata(dev
);
1938 if (delta
->clk_delta
)
1939 if (clk_prepare_enable(delta
->clk_delta
))
1940 dev_warn(dev
, "failed to prepare/enable delta clk\n");
1946 static const struct dev_pm_ops delta_pm_ops
= {
1947 .runtime_suspend
= delta_runtime_suspend
,
1948 .runtime_resume
= delta_runtime_resume
,
1951 static const struct of_device_id delta_match_types
[] = {
1953 .compatible
= "st,st-delta",
1960 MODULE_DEVICE_TABLE(of
, delta_match_types
);
1962 static struct platform_driver delta_driver
= {
1963 .probe
= delta_probe
,
1964 .remove
= delta_remove
,
1967 .of_match_table
= delta_match_types
,
1968 .pm
= &delta_pm_ops
},
1971 module_platform_driver(delta_driver
);
1973 MODULE_LICENSE("GPL");
1974 MODULE_AUTHOR("Hugues Fruchet <hugues.fruchet@st.com>");
1975 MODULE_DESCRIPTION("STMicroelectronics DELTA video decoder V4L2 driver");