1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics SA 2015
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 strscpy(cap->driver, DELTA_NAME, sizeof(cap->driver));
389 strscpy(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",
1013 /* pop timestamp and mark frame with it */
1014 delta_pop_dts(ctx, &frame->dts);
1016 /* release decoded frame to user */
1017 delta_frame_done(ctx, frame, 0);
1021 requeue_free_frames(ctx);
1022 delta_au_done(ctx, au, (discard ? -ENODATA : 0));
1023 mutex_unlock(&ctx->lock);
1024 v4l2_m2m_job_finish(delta->m2m_dev, ctx->fh.m2m_ctx);
1028 requeue_free_frames(ctx);
1029 delta_au_done(ctx, au, ret);
1030 mutex_unlock(&ctx->lock);
1031 v4l2_m2m_job_finish(delta->m2m_dev, ctx->fh.m2m_ctx);
1034 static void delta_device_run(void *priv)
1036 struct delta_ctx *ctx = priv;
1037 struct delta_dev *delta = ctx->dev;
1039 queue_work(delta->work_queue, &ctx->run_work);
1042 static void delta_job_abort(void *priv)
1044 struct delta_ctx *ctx = priv;
1045 struct delta_dev *delta = ctx->dev;
1047 dev_dbg(delta->dev, "%s aborting job\n", ctx->name);
1049 ctx->aborting = true;
1052 static int delta_job_ready(void *priv)
1054 struct delta_ctx *ctx = priv;
1055 struct delta_dev *delta = ctx->dev;
1056 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1059 dev_dbg(delta->dev, "%s not ready: not enough video buffers.\n",
1064 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1065 dev_dbg(delta->dev, "%s not ready: not enough video capture buffers.\n",
1070 if (ctx->aborting) {
1071 dev_dbg(delta->dev, "%s job not ready: aborting\n", ctx->name);
1075 dev_dbg(delta->dev, "%s job ready\n", ctx->name);
1080 /* mem-to-mem ops */
1081 static const struct v4l2_m2m_ops delta_m2m_ops = {
1082 .device_run = delta_device_run,
1083 .job_ready = delta_job_ready,
1084 .job_abort = delta_job_abort,
1088 * VB2 queue operations
1091 static int delta_vb2_au_queue_setup(struct vb2_queue *vq,
1092 unsigned int *num_buffers,
1093 unsigned int *num_planes,
1094 unsigned int sizes[],
1095 struct device *alloc_devs[])
1097 struct delta_ctx *ctx = vb2_get_drv_priv(vq);
1098 unsigned int size = ctx->max_au_size;
1101 return sizes[0] < size ? -EINVAL : 0;
1104 if (*num_buffers < 1)
1106 if (*num_buffers > DELTA_MAX_AUS)
1107 *num_buffers = DELTA_MAX_AUS;
1114 static int delta_vb2_au_prepare(struct vb2_buffer *vb)
1116 struct vb2_queue *q = vb->vb2_queue;
1117 struct delta_ctx *ctx = vb2_get_drv_priv(q);
1118 struct delta_dev *delta = ctx->dev;
1119 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1120 struct delta_au *au = to_au(vbuf);
1122 if (!au->prepared) {
1123 /* get memory addresses */
1124 au->vaddr = vb2_plane_vaddr(&au->vbuf.vb2_buf, 0);
1125 au->paddr = vb2_dma_contig_plane_dma_addr
1126 (&au->vbuf.vb2_buf, 0);
1127 au->prepared = true;
1128 dev_dbg(delta->dev, "%s au[%d] prepared; virt=0x%p, phy=0x%pad\n",
1129 ctx->name, vb->index, au->vaddr, &au->paddr);
1132 if (vbuf->field == V4L2_FIELD_ANY)
1133 vbuf->field = V4L2_FIELD_NONE;
1138 static int delta_setup_frame(struct delta_ctx *ctx,
1139 struct delta_frame *frame)
1141 struct delta_dev *delta = ctx->dev;
1142 const struct delta_dec *dec = ctx->dec;
1144 if (frame->index >= DELTA_MAX_FRAMES) {
1146 "%s frame index=%d exceeds output frame count (%d)\n",
1147 ctx->name, frame->index, DELTA_MAX_FRAMES);
1151 if (ctx->nb_of_frames >= DELTA_MAX_FRAMES) {
1153 "%s number of frames exceeds output frame count (%d > %d)\n",
1154 ctx->name, ctx->nb_of_frames, DELTA_MAX_FRAMES);
1158 if (frame->index != ctx->nb_of_frames) {
1159 dev_warn(delta->dev,
1160 "%s frame index discontinuity detected, expected %d, got %d\n",
1161 ctx->name, ctx->nb_of_frames, frame->index);
1164 frame->state = DELTA_FRAME_FREE;
1165 ctx->frames[ctx->nb_of_frames] = frame;
1166 ctx->nb_of_frames++;
1168 /* setup frame on decoder side */
1169 return call_dec_op(dec, setup_frame, ctx, frame);
1173 * default implementation of get_frameinfo decoder ops
1174 * matching frame information from stream information
1175 * & with default pixel format & default alignment.
1177 int delta_get_frameinfo_default(struct delta_ctx *ctx,
1178 struct delta_frameinfo *frameinfo)
1180 struct delta_streaminfo *streaminfo = &ctx->streaminfo;
1182 memset(frameinfo, 0, sizeof(*frameinfo));
1183 frameinfo->pixelformat = V4L2_PIX_FMT_NV12;
1184 frameinfo->width = streaminfo->width;
1185 frameinfo->height = streaminfo->height;
1186 frameinfo->aligned_width = ALIGN(streaminfo->width,
1187 DELTA_WIDTH_ALIGNMENT);
1188 frameinfo->aligned_height = ALIGN(streaminfo->height,
1189 DELTA_HEIGHT_ALIGNMENT);
1190 frameinfo->size = frame_size(frameinfo->aligned_width,
1191 frameinfo->aligned_height,
1192 frameinfo->pixelformat);
1193 if (streaminfo->flags & DELTA_STREAMINFO_FLAG_CROP) {
1194 frameinfo->flags |= DELTA_FRAMEINFO_FLAG_CROP;
1195 frameinfo->crop = streaminfo->crop;
1197 if (streaminfo->flags & DELTA_STREAMINFO_FLAG_PIXELASPECT) {
1198 frameinfo->flags |= DELTA_FRAMEINFO_FLAG_PIXELASPECT;
1199 frameinfo->pixelaspect = streaminfo->pixelaspect;
1201 frameinfo->field = streaminfo->field;
1207 * default implementation of recycle decoder ops
1208 * consisting to relax the "decoded" frame state
1210 int delta_recycle_default(struct delta_ctx *pctx,
1211 struct delta_frame *frame)
1213 frame->state &= ~DELTA_FRAME_DEC;
1218 static void dump_frames_status(struct delta_ctx *ctx)
1220 struct delta_dev *delta = ctx->dev;
1222 struct delta_frame *frame;
1223 unsigned char str[100] = "";
1225 dev_info(delta->dev,
1226 "%s dumping frames status...\n", ctx->name);
1228 for (i = 0; i < ctx->nb_of_frames; i++) {
1229 frame = ctx->frames[i];
1230 dev_info(delta->dev,
1231 "%s frame[%d] %s\n",
1232 ctx->name, frame->index,
1233 frame_state_str(frame->state,
1238 int delta_get_free_frame(struct delta_ctx *ctx,
1239 struct delta_frame **pframe)
1241 struct delta_dev *delta = ctx->dev;
1242 struct vb2_v4l2_buffer *vbuf;
1243 struct delta_frame *frame;
1247 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1249 dev_err(delta->dev, "%s no frame available",
1254 frame = to_frame(vbuf);
1255 frame->state &= ~DELTA_FRAME_M2M;
1256 if (frame->state != DELTA_FRAME_FREE) {
1258 "%s frame[%d] is not free\n",
1259 ctx->name, frame->index);
1260 dump_frames_status(ctx);
1265 "%s get free frame[%d]\n", ctx->name, frame->index);
1271 int delta_get_sync(struct delta_ctx *ctx)
1273 struct delta_dev *delta = ctx->dev;
1276 /* enable the hardware */
1277 ret = pm_runtime_resume_and_get(delta->dev);
1279 dev_err(delta->dev, "%s pm_runtime_resume_and_get failed (%d)\n",
1287 void delta_put_autosuspend(struct delta_ctx *ctx)
1289 struct delta_dev *delta = ctx->dev;
1291 pm_runtime_put_autosuspend(delta->dev);
1294 static void delta_vb2_au_queue(struct vb2_buffer *vb)
1296 struct vb2_queue *q = vb->vb2_queue;
1297 struct delta_ctx *ctx = vb2_get_drv_priv(q);
1298 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1300 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1303 static int delta_vb2_au_start_streaming(struct vb2_queue *q,
1306 struct delta_ctx *ctx = vb2_get_drv_priv(q);
1307 struct delta_dev *delta = ctx->dev;
1308 const struct delta_dec *dec = ctx->dec;
1309 struct delta_au *au;
1311 struct vb2_v4l2_buffer *vbuf = NULL;
1312 struct delta_streaminfo *streaminfo = &ctx->streaminfo;
1313 struct delta_frameinfo *frameinfo = &ctx->frameinfo;
1314 unsigned char str1[100] = "";
1315 unsigned char str2[100] = "";
1317 if ((ctx->state != DELTA_STATE_WF_FORMAT) &&
1318 (ctx->state != DELTA_STATE_WF_STREAMINFO))
1321 if (ctx->state == DELTA_STATE_WF_FORMAT) {
1322 /* open decoder if not yet done */
1323 ret = delta_open_decoder(ctx,
1324 ctx->streaminfo.streamformat,
1325 ctx->frameinfo.pixelformat, &dec);
1329 ctx->state = DELTA_STATE_WF_STREAMINFO;
1333 * first buffer should contain stream header,
1334 * decode it to get the infos related to stream
1335 * such as width, height, dpb, ...
1337 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1339 dev_err(delta->dev, "%s failed to start streaming, no stream header buffer enqueued\n",
1345 au->size = vb2_get_plane_payload(&vbuf->vb2_buf, 0);
1346 au->dts = vbuf->vb2_buf.timestamp;
1348 delta_push_dts(ctx, au->dts);
1350 /* dump access unit */
1353 /* decode this access unit */
1354 ret = call_dec_op(dec, decode, ctx, au);
1356 dev_err(delta->dev, "%s failed to start streaming, header decoding failed (%d)\n",
1361 ret = call_dec_op(dec, get_streaminfo, ctx, streaminfo);
1363 dev_dbg_ratelimited(delta->dev,
1364 "%s failed to start streaming, valid stream header not yet decoded\n",
1368 ctx->flags |= DELTA_FLAG_STREAMINFO;
1370 ret = call_dec_op(dec, get_frameinfo, ctx, frameinfo);
1373 ctx->flags |= DELTA_FLAG_FRAMEINFO;
1375 ctx->state = DELTA_STATE_READY;
1377 dev_dbg(delta->dev, "%s %s => %s\n", ctx->name,
1378 delta_streaminfo_str(streaminfo, str1, sizeof(str1)),
1379 delta_frameinfo_str(frameinfo, str2, sizeof(str2)));
1381 delta_au_done(ctx, au, ret);
1386 * return all buffers to vb2 in QUEUED state.
1387 * This will give ownership back to userspace
1390 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
1392 while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1393 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
1397 static void delta_vb2_au_stop_streaming(struct vb2_queue *q)
1399 struct delta_ctx *ctx = vb2_get_drv_priv(q);
1400 struct vb2_v4l2_buffer *vbuf;
1402 delta_flush_dts(ctx);
1404 /* return all buffers to vb2 in ERROR state */
1405 while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1406 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1410 ctx->aborting = false;
1413 static int delta_vb2_frame_queue_setup(struct vb2_queue *vq,
1414 unsigned int *num_buffers,
1415 unsigned int *num_planes,
1416 unsigned int sizes[],
1417 struct device *alloc_devs[])
1419 struct delta_ctx *ctx = vb2_get_drv_priv(vq);
1420 struct delta_dev *delta = ctx->dev;
1421 struct delta_streaminfo *streaminfo = &ctx->streaminfo;
1422 struct delta_frameinfo *frameinfo = &ctx->frameinfo;
1423 unsigned int size = frameinfo->size;
1426 * the number of output buffers needed for decoding =
1427 * user need (*num_buffers given, usually for display pipeline) +
1428 * stream need (streaminfo->dpb) +
1429 * decoding peak smoothing (depends on DELTA IP perf)
1431 if (*num_buffers < DELTA_MIN_FRAME_USER) {
1433 "%s num_buffers too low (%d), increasing to %d\n",
1434 ctx->name, *num_buffers, DELTA_MIN_FRAME_USER);
1435 *num_buffers = DELTA_MIN_FRAME_USER;
1438 *num_buffers += streaminfo->dpb + DELTA_PEAK_FRAME_SMOOTHING;
1440 if (*num_buffers > DELTA_MAX_FRAMES) {
1442 "%s output frame count too high (%d), cut to %d\n",
1443 ctx->name, *num_buffers, DELTA_MAX_FRAMES);
1444 *num_buffers = DELTA_MAX_FRAMES;
1448 return sizes[0] < size ? -EINVAL : 0;
1450 /* single plane for Y and CbCr */
1455 ctx->nb_of_frames = 0;
1460 static int delta_vb2_frame_prepare(struct vb2_buffer *vb)
1462 struct vb2_queue *q = vb->vb2_queue;
1463 struct delta_ctx *ctx = vb2_get_drv_priv(q);
1464 struct delta_dev *delta = ctx->dev;
1465 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1466 struct delta_frame *frame = to_frame(vbuf);
1469 if (!frame->prepared) {
1470 frame->index = vbuf->vb2_buf.index;
1471 frame->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
1472 frame->paddr = vb2_dma_contig_plane_dma_addr(&vbuf->vb2_buf, 0);
1473 frame->info = ctx->frameinfo;
1475 ret = delta_setup_frame(ctx, frame);
1478 "%s setup_frame() failed (%d)\n",
1482 frame->prepared = true;
1484 "%s frame[%d] prepared; virt=0x%p, phy=0x%pad\n",
1485 ctx->name, vb->index, frame->vaddr,
1489 frame->flags = vbuf->flags;
1494 static void delta_vb2_frame_finish(struct vb2_buffer *vb)
1496 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1497 struct delta_frame *frame = to_frame(vbuf);
1499 /* update V4L2 fields for user */
1500 vb2_set_plane_payload(&vbuf->vb2_buf, 0, frame->info.size);
1501 vb->timestamp = frame->dts;
1502 vbuf->field = frame->field;
1503 vbuf->flags = frame->flags;
1506 static void delta_vb2_frame_queue(struct vb2_buffer *vb)
1508 struct vb2_queue *q = vb->vb2_queue;
1509 struct delta_ctx *ctx = vb2_get_drv_priv(q);
1510 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1511 struct delta_frame *frame = to_frame(vbuf);
1513 if (ctx->state == DELTA_STATE_WF_EOS) {
1514 /* new frame available, EOS can now be completed */
1515 delta_complete_eos(ctx, frame);
1517 ctx->state = DELTA_STATE_EOS;
1519 /* return, no need to recycle this buffer to decoder */
1523 /* recycle this frame */
1524 delta_recycle(ctx, frame);
1527 static void delta_vb2_frame_stop_streaming(struct vb2_queue *q)
1529 struct delta_ctx *ctx = vb2_get_drv_priv(q);
1530 struct vb2_v4l2_buffer *vbuf;
1531 struct delta_frame *frame;
1532 const struct delta_dec *dec = ctx->dec;
1535 delta_flush_dts(ctx);
1537 call_dec_op(dec, flush, ctx);
1540 * return all buffers to vb2 in ERROR state
1541 * & reset each frame state to OUT
1543 for (i = 0; i < ctx->nb_of_frames; i++) {
1544 frame = ctx->frames[i];
1545 if (!(frame->state & DELTA_FRAME_OUT)) {
1546 vbuf = &frame->vbuf;
1547 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1549 frame->state = DELTA_FRAME_OUT;
1554 ctx->aborting = false;
1558 static const struct vb2_ops delta_vb2_au_ops = {
1559 .queue_setup = delta_vb2_au_queue_setup,
1560 .buf_prepare = delta_vb2_au_prepare,
1561 .buf_queue = delta_vb2_au_queue,
1562 .wait_prepare = vb2_ops_wait_prepare,
1563 .wait_finish = vb2_ops_wait_finish,
1564 .start_streaming = delta_vb2_au_start_streaming,
1565 .stop_streaming = delta_vb2_au_stop_streaming,
1568 static const struct vb2_ops delta_vb2_frame_ops = {
1569 .queue_setup = delta_vb2_frame_queue_setup,
1570 .buf_prepare = delta_vb2_frame_prepare,
1571 .buf_finish = delta_vb2_frame_finish,
1572 .buf_queue = delta_vb2_frame_queue,
1573 .wait_prepare = vb2_ops_wait_prepare,
1574 .wait_finish = vb2_ops_wait_finish,
1575 .stop_streaming = delta_vb2_frame_stop_streaming,
1579 * V4L2 file operations
1582 static int queue_init(void *priv,
1583 struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
1585 struct vb2_queue *q;
1586 struct delta_ctx *ctx = priv;
1587 struct delta_dev *delta = ctx->dev;
1590 /* setup vb2 queue for stream input */
1592 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1593 q->io_modes = VB2_MMAP | VB2_DMABUF;
1595 /* overload vb2 buf with private au struct */
1596 q->buf_struct_size = sizeof(struct delta_au);
1597 q->ops = &delta_vb2_au_ops;
1598 q->mem_ops = &vb2_dma_contig_memops;
1599 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1600 q->lock = &delta->lock;
1601 q->dev = delta->dev;
1603 ret = vb2_queue_init(q);
1607 /* setup vb2 queue for frame output */
1609 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1610 q->io_modes = VB2_MMAP | VB2_DMABUF;
1612 /* overload vb2 buf with private frame struct */
1613 q->buf_struct_size = sizeof(struct delta_frame)
1614 + DELTA_MAX_FRAME_PRIV_SIZE;
1615 q->ops = &delta_vb2_frame_ops;
1616 q->mem_ops = &vb2_dma_contig_memops;
1617 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1618 q->lock = &delta->lock;
1619 q->dev = delta->dev;
1621 return vb2_queue_init(q);
1624 static int delta_open(struct file *file)
1626 struct delta_dev *delta = video_drvdata(file);
1627 struct delta_ctx *ctx = NULL;
1630 mutex_lock(&delta->lock);
1632 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1639 v4l2_fh_init(&ctx->fh, video_devdata(file));
1640 file->private_data = &ctx->fh;
1641 v4l2_fh_add(&ctx->fh);
1643 INIT_WORK(&ctx->run_work, delta_run_work);
1644 mutex_init(&ctx->lock);
1646 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(delta->m2m_dev, ctx,
1648 if (IS_ERR(ctx->fh.m2m_ctx)) {
1649 ret = PTR_ERR(ctx->fh.m2m_ctx);
1650 dev_err(delta->dev, "%s failed to initialize m2m context (%d)\n",
1656 * wait stream format to determine which
1659 ctx->state = DELTA_STATE_WF_FORMAT;
1661 INIT_LIST_HEAD(&ctx->dts);
1663 /* set the instance name */
1664 delta->instance_id++;
1665 snprintf(ctx->name, sizeof(ctx->name), "[%3d:----]",
1666 delta->instance_id);
1668 /* default parameters for frame and stream */
1669 set_default_params(ctx);
1671 /* enable ST231 clocks */
1672 if (clk_prepare_enable(delta->clk_st231))
1673 dev_warn(delta->dev, "failed to enable st231 clk\n");
1675 /* enable FLASH_PROMIP clock */
1676 if (clk_prepare_enable(delta->clk_flash_promip))
1677 dev_warn(delta->dev, "failed to enable delta promip clk\n");
1679 mutex_unlock(&delta->lock);
1681 dev_dbg(delta->dev, "%s decoder instance created\n", ctx->name);
1686 v4l2_fh_del(&ctx->fh);
1687 v4l2_fh_exit(&ctx->fh);
1690 mutex_unlock(&delta->lock);
1695 static int delta_release(struct file *file)
1697 struct delta_ctx *ctx = to_ctx(file->private_data);
1698 struct delta_dev *delta = ctx->dev;
1699 const struct delta_dec *dec = ctx->dec;
1701 mutex_lock(&delta->lock);
1704 call_dec_op(dec, close, ctx);
1707 * trace a summary of instance
1708 * before closing (debug purpose)
1710 delta_trace_summary(ctx);
1712 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1714 v4l2_fh_del(&ctx->fh);
1715 v4l2_fh_exit(&ctx->fh);
1717 /* disable ST231 clocks */
1718 clk_disable_unprepare(delta->clk_st231);
1720 /* disable FLASH_PROMIP clock */
1721 clk_disable_unprepare(delta->clk_flash_promip);
1723 dev_dbg(delta->dev, "%s decoder instance released\n", ctx->name);
1727 mutex_unlock(&delta->lock);
1732 static const struct v4l2_file_operations delta_fops = {
1733 .owner = THIS_MODULE,
1735 .release = delta_release,
1736 .unlocked_ioctl = video_ioctl2,
1737 .mmap = v4l2_m2m_fop_mmap,
1738 .poll = v4l2_m2m_fop_poll,
1742 * Platform device operations
1745 static int delta_register_device(struct delta_dev *delta)
1748 struct video_device *vdev;
1753 delta->m2m_dev = v4l2_m2m_init(&delta_m2m_ops);
1754 if (IS_ERR(delta->m2m_dev)) {
1755 dev_err(delta->dev, "%s failed to initialize v4l2-m2m device\n",
1757 ret = PTR_ERR(delta->m2m_dev);
1761 vdev = video_device_alloc();
1763 dev_err(delta->dev, "%s failed to allocate video device\n",
1766 goto err_m2m_release;
1769 vdev->fops = &delta_fops;
1770 vdev->ioctl_ops = &delta_ioctl_ops;
1771 vdev->release = video_device_release;
1772 vdev->lock = &delta->lock;
1773 vdev->vfl_dir = VFL_DIR_M2M;
1774 vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M;
1775 vdev->v4l2_dev = &delta->v4l2_dev;
1776 snprintf(vdev->name, sizeof(vdev->name), "%s-%s",
1777 DELTA_NAME, DELTA_FW_VERSION);
1779 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1781 dev_err(delta->dev, "%s failed to register video device\n",
1783 goto err_vdev_release;
1787 video_set_drvdata(vdev, delta);
1791 video_device_release(vdev);
1793 v4l2_m2m_release(delta->m2m_dev);
1798 static void delta_unregister_device(struct delta_dev *delta)
1804 v4l2_m2m_release(delta->m2m_dev);
1806 video_unregister_device(delta->vdev);
1809 static int delta_probe(struct platform_device *pdev)
1811 struct delta_dev *delta;
1812 struct device *dev = &pdev->dev;
1815 delta = devm_kzalloc(dev, sizeof(*delta), GFP_KERNEL);
1823 platform_set_drvdata(pdev, delta);
1825 mutex_init(&delta->lock);
1827 /* get clock resources */
1828 delta->clk_delta = devm_clk_get(dev, "delta");
1829 if (IS_ERR(delta->clk_delta)) {
1830 dev_dbg(dev, "%s can't get delta clock\n", DELTA_PREFIX);
1831 delta->clk_delta = NULL;
1834 delta->clk_st231 = devm_clk_get(dev, "delta-st231");
1835 if (IS_ERR(delta->clk_st231)) {
1836 dev_dbg(dev, "%s can't get delta-st231 clock\n", DELTA_PREFIX);
1837 delta->clk_st231 = NULL;
1840 delta->clk_flash_promip = devm_clk_get(dev, "delta-flash-promip");
1841 if (IS_ERR(delta->clk_flash_promip)) {
1842 dev_dbg(dev, "%s can't get delta-flash-promip clock\n",
1844 delta->clk_flash_promip = NULL;
1847 /* init pm_runtime used for power management */
1848 pm_runtime_set_autosuspend_delay(dev, DELTA_HW_AUTOSUSPEND_DELAY_MS);
1849 pm_runtime_use_autosuspend(dev);
1850 pm_runtime_set_suspended(dev);
1851 pm_runtime_enable(dev);
1853 /* init firmware ipc channel */
1854 ret = delta_ipc_init(delta);
1856 dev_err(delta->dev, "%s failed to initialize firmware ipc channel\n",
1858 goto err_pm_disable;
1861 /* register all available decoders */
1862 register_decoders(delta);
1864 /* register all supported formats */
1865 register_formats(delta);
1867 /* register on V4L2 */
1868 ret = v4l2_device_register(dev, &delta->v4l2_dev);
1870 dev_err(delta->dev, "%s failed to register V4L2 device\n",
1872 goto err_pm_disable;
1875 delta->work_queue = create_workqueue(DELTA_NAME);
1876 if (!delta->work_queue) {
1877 dev_err(delta->dev, "%s failed to allocate work queue\n",
1883 /* register device */
1884 ret = delta_register_device(delta);
1886 goto err_work_queue;
1888 dev_info(dev, "%s %s registered as /dev/video%d\n",
1889 DELTA_PREFIX, delta->vdev->name, delta->vdev->num);
1894 destroy_workqueue(delta->work_queue);
1896 v4l2_device_unregister(&delta->v4l2_dev);
1898 pm_runtime_disable(dev);
1903 static void delta_remove(struct platform_device *pdev)
1905 struct delta_dev *delta = platform_get_drvdata(pdev);
1907 delta_ipc_exit(delta);
1909 delta_unregister_device(delta);
1911 destroy_workqueue(delta->work_queue);
1913 pm_runtime_put_autosuspend(delta->dev);
1914 pm_runtime_disable(delta->dev);
1916 v4l2_device_unregister(&delta->v4l2_dev);
1919 static int delta_runtime_suspend(struct device *dev)
1921 struct delta_dev *delta = dev_get_drvdata(dev);
1923 clk_disable_unprepare(delta->clk_delta);
1928 static int delta_runtime_resume(struct device *dev)
1930 struct delta_dev *delta = dev_get_drvdata(dev);
1932 if (clk_prepare_enable(delta->clk_delta))
1933 dev_warn(dev, "failed to prepare/enable delta clk\n");
1939 static const struct dev_pm_ops delta_pm_ops = {
1940 .runtime_suspend = delta_runtime_suspend,
1941 .runtime_resume = delta_runtime_resume,
1944 static const struct of_device_id delta_match_types[] = {
1946 .compatible = "st,st-delta",
1953 MODULE_DEVICE_TABLE(of, delta_match_types);
1955 static struct platform_driver delta_driver = {
1956 .probe = delta_probe,
1957 .remove_new = delta_remove,
1960 .of_match_table = delta_match_types,
1961 .pm = &delta_pm_ops},
1964 module_platform_driver(delta_driver);
1966 MODULE_LICENSE("GPL");
1968 MODULE_DESCRIPTION("STMicroelectronics DELTA video decoder V4L2 driver");