1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/slab.h>
14 
15 #include <media/v4l2-ioctl.h>
16 #include <media/v4l2-event.h>
17 #include <media/videobuf2-dma-contig.h>
18 
19 #include "delta.h"
20 #include "delta-debug.h"
21 #include "delta-ipc.h"
22 
23 #define DELTA_NAME	"st-delta"
24 
25 #define DELTA_PREFIX "[---:----]"
26 
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)
30 
31 #define call_dec_op(dec, op, args...)\
32 		((dec && (dec)->op) ? (dec)->op(args) : 0)
33 
34 /* registry of available decoders */
35 static const struct delta_dec *delta_decoders[] = {
36 #ifdef CONFIG_VIDEO_STI_DELTA_MJPEG
37 	&mjpegdec,
38 #endif
39 };
40 
41 static inline int frame_size(u32 w, u32 h, u32 fmt)
42 {
43 	switch (fmt) {
44 	case V4L2_PIX_FMT_NV12:
45 		return (w * h * 3) / 2;
46 	default:
47 		return 0;
48 	}
49 }
50 
51 static inline int frame_stride(u32 w, u32 fmt)
52 {
53 	switch (fmt) {
54 	case V4L2_PIX_FMT_NV12:
55 		return w;
56 	default:
57 		return 0;
58 	}
59 }
60 
61 static void dump_au(struct delta_ctx *ctx, struct delta_au *au)
62 {
63 	struct delta_dev *delta = ctx->dev;
64 	u32 size = 10;	/* dump first & last 10 bytes */
65 	u8 *data = (u8 *)(au->vaddr);
66 
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,
70 			au->size, data);
71 	else
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);
75 }
76 
77 static void dump_frame(struct delta_ctx *ctx, struct delta_frame *frame)
78 {
79 	struct delta_dev *delta = ctx->dev;
80 	u32 size = 10;	/* dump first 10 bytes */
81 	u8 *data = (u8 *)(frame->vaddr);
82 
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),
87 		size, data);
88 }
89 
90 static void delta_au_done(struct delta_ctx *ctx, struct delta_au *au, int err)
91 {
92 	struct vb2_v4l2_buffer *vbuf;
93 
94 	vbuf = &au->vbuf;
95 	vbuf->sequence = ctx->au_num++;
96 	v4l2_m2m_buf_done(vbuf, err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
97 }
98 
99 static void delta_frame_done(struct delta_ctx *ctx, struct delta_frame *frame,
100 			     int err)
101 {
102 	struct vb2_v4l2_buffer *vbuf;
103 
104 	dump_frame(ctx, frame);
105 
106 	/* decoded frame is now output to user */
107 	frame->state |= DELTA_FRAME_OUT;
108 
109 	vbuf = &frame->vbuf;
110 	vbuf->sequence = ctx->frame_num++;
111 	v4l2_m2m_buf_done(vbuf, err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
112 
113 	if (frame->info.size) /* ignore EOS */
114 		ctx->output_frames++;
115 }
116 
117 static void requeue_free_frames(struct delta_ctx *ctx)
118 {
119 	struct vb2_v4l2_buffer *vbuf;
120 	struct delta_frame *frame;
121 	unsigned int i;
122 
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) {
127 			vbuf = &frame->vbuf;
128 			v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
129 			frame->state = DELTA_FRAME_M2M;
130 		}
131 	}
132 }
133 
134 static int delta_recycle(struct delta_ctx *ctx, struct delta_frame *frame)
135 {
136 	const struct delta_dec *dec = ctx->dec;
137 
138 	/* recycle frame on decoder side */
139 	call_dec_op(dec, recycle, ctx, frame);
140 
141 	/* this frame is no more output */
142 	frame->state &= ~DELTA_FRAME_OUT;
143 
144 	/* requeue free frame */
145 	if (frame->state == DELTA_FRAME_FREE) {
146 		struct vb2_v4l2_buffer *vbuf = &frame->vbuf;
147 
148 		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
149 		frame->state = DELTA_FRAME_M2M;
150 	}
151 
152 	/* reset other frame fields */
153 	frame->flags = 0;
154 	frame->dts = 0;
155 
156 	return 0;
157 }
158 
159 static void delta_push_dts(struct delta_ctx *ctx, u64 val)
160 {
161 	struct delta_dts *dts;
162 
163 	dts = kzalloc(sizeof(*dts), GFP_KERNEL);
164 	if (!dts)
165 		return;
166 
167 	INIT_LIST_HEAD(&dts->list);
168 
169 	/*
170 	 * protected by global lock acquired
171 	 * by V4L2 when calling delta_vb2_au_queue
172 	 */
173 	dts->val = val;
174 	list_add_tail(&dts->list, &ctx->dts);
175 }
176 
177 static void delta_pop_dts(struct delta_ctx *ctx, u64 *val)
178 {
179 	struct delta_dev *delta = ctx->dev;
180 	struct delta_dts *dts;
181 
182 	/*
183 	 * protected by global lock acquired
184 	 * by V4L2 when calling delta_vb2_au_queue
185 	 */
186 	if (list_empty(&ctx->dts)) {
187 		dev_warn(delta->dev, "%s no dts to pop ... output dts = 0\n",
188 			 ctx->name);
189 		*val = 0;
190 		return;
191 	}
192 
193 	dts = list_first_entry(&ctx->dts, struct delta_dts, list);
194 	list_del(&dts->list);
195 
196 	*val = dts->val;
197 
198 	kfree(dts);
199 }
200 
201 static void delta_flush_dts(struct delta_ctx *ctx)
202 {
203 	struct delta_dts *dts;
204 	struct delta_dts *next;
205 
206 	/*
207 	 * protected by global lock acquired
208 	 * by V4L2 when calling delta_vb2_au_queue
209 	 */
210 
211 	/* free all pending dts */
212 	list_for_each_entry_safe(dts, next, &ctx->dts, list)
213 		kfree(dts);
214 
215 	/* reset list */
216 	INIT_LIST_HEAD(&ctx->dts);
217 }
218 
219 static inline int frame_alignment(u32 fmt)
220 {
221 	switch (fmt) {
222 	case V4L2_PIX_FMT_NV12:
223 	case V4L2_PIX_FMT_NV21:
224 		/* multiple of 2 */
225 		return 2;
226 	default:
227 		return 1;
228 	}
229 }
230 
231 static inline int estimated_au_size(u32 w, u32 h)
232 {
233 	/*
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)
238 	 */
239 	return (w * h);
240 }
241 
242 static void set_default_params(struct delta_ctx *ctx)
243 {
244 	struct delta_frameinfo *frameinfo = &ctx->frameinfo;
245 	struct delta_streaminfo *streaminfo = &ctx->streaminfo;
246 
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;
263 
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;
273 
274 	ctx->max_au_size = estimated_au_size(streaminfo->width,
275 					     streaminfo->height);
276 }
277 
278 static const struct delta_dec *delta_find_decoder(struct delta_ctx *ctx,
279 						  u32 streamformat,
280 						  u32 pixelformat)
281 {
282 	struct delta_dev *delta = ctx->dev;
283 	const struct delta_dec *dec;
284 	unsigned int i;
285 
286 	for (i = 0; i < delta->nb_of_decoders; i++) {
287 		dec = delta->decoders[i];
288 		if ((dec->pixelformat == pixelformat) &&
289 		    (dec->streamformat == streamformat))
290 			return dec;
291 	}
292 
293 	return NULL;
294 }
295 
296 static void register_format(u32 format, u32 formats[], u32 *nb_of_formats)
297 {
298 	u32 i;
299 
300 	for (i = 0; i < *nb_of_formats; i++) {
301 		if (format == formats[i])
302 			return;
303 	}
304 
305 	formats[(*nb_of_formats)++] = format;
306 }
307 
308 static void register_formats(struct delta_dev *delta)
309 {
310 	unsigned int i;
311 
312 	for (i = 0; i < delta->nb_of_decoders; i++) {
313 		register_format(delta->decoders[i]->pixelformat,
314 				delta->pixelformats,
315 				&delta->nb_of_pixelformats);
316 
317 		register_format(delta->decoders[i]->streamformat,
318 				delta->streamformats,
319 				&delta->nb_of_streamformats);
320 	}
321 }
322 
323 static void register_decoders(struct delta_dev *delta)
324 {
325 	unsigned int i;
326 
327 	for (i = 0; i < ARRAY_SIZE(delta_decoders); i++) {
328 		if (delta->nb_of_decoders >= DELTA_MAX_DECODERS) {
329 			dev_dbg(delta->dev,
330 				"%s failed to register %s decoder (%d maximum reached)\n",
331 				DELTA_PREFIX, delta_decoders[i]->name,
332 				DELTA_MAX_DECODERS);
333 			return;
334 		}
335 
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);
339 	}
340 }
341 
342 static int delta_open_decoder(struct delta_ctx *ctx, u32 streamformat,
343 			      u32 pixelformat, const struct delta_dec **pdec)
344 {
345 	struct delta_dev *delta = ctx->dev;
346 	const struct delta_dec *dec;
347 	int ret;
348 
349 	dec = delta_find_decoder(ctx, streamformat, ctx->frameinfo.pixelformat);
350 	if (!dec) {
351 		dev_err(delta->dev, "%s no decoder found matching %4.4s => %4.4s\n",
352 			ctx->name, (char *)&streamformat, (char *)&pixelformat);
353 		return -EINVAL;
354 	}
355 
356 	dev_dbg(delta->dev, "%s one decoder matching %4.4s => %4.4s\n",
357 		ctx->name, (char *)&streamformat, (char *)&pixelformat);
358 
359 	/* update instance name */
360 	snprintf(ctx->name, sizeof(ctx->name), "[%3d:%4.4s]",
361 		 delta->instance_id, (char *)&streamformat);
362 
363 	/* open decoder instance */
364 	ret = call_dec_op(dec, open, ctx);
365 	if (ret) {
366 		dev_err(delta->dev, "%s failed to open decoder instance (%d)\n",
367 			ctx->name, ret);
368 		return ret;
369 	}
370 
371 	dev_dbg(delta->dev, "%s %s decoder opened\n", ctx->name, dec->name);
372 
373 	*pdec = dec;
374 
375 	return ret;
376 }
377 
378 /*
379  * V4L2 ioctl operations
380  */
381 
382 static int delta_querycap(struct file *file, void *priv,
383 			  struct v4l2_capability *cap)
384 {
385 	struct delta_ctx *ctx = to_ctx(file->private_data);
386 	struct delta_dev *delta = ctx->dev;
387 
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",
391 		 delta->pdev->name);
392 
393 	return 0;
394 }
395 
396 static int delta_enum_fmt_stream(struct file *file, void *priv,
397 				 struct v4l2_fmtdesc *f)
398 {
399 	struct delta_ctx *ctx = to_ctx(file->private_data);
400 	struct delta_dev *delta = ctx->dev;
401 
402 	if (unlikely(f->index >= delta->nb_of_streamformats))
403 		return -EINVAL;
404 
405 	f->pixelformat = delta->streamformats[f->index];
406 
407 	return 0;
408 }
409 
410 static int delta_enum_fmt_frame(struct file *file, void *priv,
411 				struct v4l2_fmtdesc *f)
412 {
413 	struct delta_ctx *ctx = to_ctx(file->private_data);
414 	struct delta_dev *delta = ctx->dev;
415 
416 	if (unlikely(f->index >= delta->nb_of_pixelformats))
417 		return -EINVAL;
418 
419 	f->pixelformat = delta->pixelformats[f->index];
420 
421 	return 0;
422 }
423 
424 static int delta_g_fmt_stream(struct file *file, void *fh,
425 			      struct v4l2_format *f)
426 {
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] = "";
432 
433 	if (!(ctx->flags & DELTA_FLAG_STREAMINFO))
434 		dev_dbg(delta->dev,
435 			"%s V4L2 GET_FMT (OUTPUT): no stream information available, default to %s\n",
436 			ctx->name,
437 			delta_streaminfo_str(streaminfo, str, sizeof(str)));
438 
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;
449 
450 	return 0;
451 }
452 
453 static int delta_g_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
454 {
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] = "";
461 
462 	if (!(ctx->flags & DELTA_FLAG_FRAMEINFO))
463 		dev_dbg(delta->dev,
464 			"%s V4L2 GET_FMT (CAPTURE): no frame information available, default to %s\n",
465 			ctx->name,
466 			delta_frameinfo_str(frameinfo, str, sizeof(str)));
467 
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;
475 
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;
482 	}
483 	pix->colorspace = frameinfo->colorspace;
484 	pix->xfer_func = frameinfo->xfer_func;
485 	pix->ycbcr_enc = frameinfo->ycbcr_enc;
486 	pix->quantization = frameinfo->quantization;
487 
488 	return 0;
489 }
490 
491 static int delta_try_fmt_stream(struct file *file, void *priv,
492 				struct v4l2_format *f)
493 {
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;
499 	u32 width, height;
500 	u32 au_size;
501 
502 	dec = delta_find_decoder(ctx, streamformat, ctx->frameinfo.pixelformat);
503 	if (!dec) {
504 		dev_dbg(delta->dev,
505 			"%s V4L2 TRY_FMT (OUTPUT): unsupported format %4.4s\n",
506 			ctx->name, (char *)&pix->pixelformat);
507 		return -EINVAL;
508 	}
509 
510 	/* adjust width & height */
511 	width = pix->width;
512 	height = pix->height;
513 	v4l_bound_align_image
514 		(&pix->width,
515 		 DELTA_MIN_WIDTH,
516 		 dec->max_width ? dec->max_width : DELTA_MAX_WIDTH,
517 		 0,
518 		 &pix->height,
519 		 DELTA_MIN_HEIGHT,
520 		 dec->max_height ? dec->max_height : DELTA_MAX_HEIGHT,
521 		 0, 0);
522 
523 	if ((pix->width != width) || (pix->height != height))
524 		dev_dbg(delta->dev,
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);
528 
529 	au_size = estimated_au_size(pix->width, pix->height);
530 	if (pix->sizeimage < au_size) {
531 		dev_dbg(delta->dev,
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;
535 	}
536 
537 	pix->bytesperline = 0;
538 
539 	if (pix->field == V4L2_FIELD_ANY)
540 		pix->field = V4L2_FIELD_NONE;
541 
542 	return 0;
543 }
544 
545 static int delta_try_fmt_frame(struct file *file, void *priv,
546 			       struct v4l2_format *f)
547 {
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;
553 	u32 width, height;
554 
555 	dec = delta_find_decoder(ctx, ctx->streaminfo.streamformat,
556 				 pixelformat);
557 	if (!dec) {
558 		dev_dbg(delta->dev,
559 			"%s V4L2 TRY_FMT (CAPTURE): unsupported format %4.4s\n",
560 			ctx->name, (char *)&pixelformat);
561 		return -EINVAL;
562 	}
563 
564 	/* adjust width & height */
565 	width = pix->width;
566 	height = pix->height;
567 	v4l_bound_align_image(&pix->width,
568 			      DELTA_MIN_WIDTH, DELTA_MAX_WIDTH,
569 			      frame_alignment(pixelformat) - 1,
570 			      &pix->height,
571 			      DELTA_MIN_HEIGHT, DELTA_MAX_HEIGHT,
572 			      frame_alignment(pixelformat) - 1, 0);
573 
574 	if ((pix->width != width) || (pix->height != height))
575 		dev_dbg(delta->dev,
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);
578 
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))
583 		dev_dbg(delta->dev,
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);
586 
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;
592 	}
593 
594 	pix->width = width;
595 	pix->height = height;
596 	pix->bytesperline = frame_stride(pix->width, pixelformat);
597 	pix->sizeimage = frame_size(pix->width, pix->height, pixelformat);
598 
599 	if (pix->field == V4L2_FIELD_ANY)
600 		pix->field = V4L2_FIELD_NONE;
601 
602 	return 0;
603 }
604 
605 static int delta_s_fmt_stream(struct file *file, void *fh,
606 			      struct v4l2_format *f)
607 {
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;
612 	int ret;
613 
614 	ret = delta_try_fmt_stream(file, fh, f);
615 	if (ret) {
616 		dev_dbg(delta->dev,
617 			"%s V4L2 S_FMT (OUTPUT): unsupported format %4.4s\n",
618 			ctx->name, (char *)&pix->pixelformat);
619 		return ret;
620 	}
621 
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",
625 			ctx->name);
626 		return -EBUSY;
627 	}
628 
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;
638 
639 	return 0;
640 }
641 
642 static int delta_s_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
643 {
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;
651 	int ret;
652 
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",
656 			ctx->name);
657 		return -EBUSY;
658 	}
659 
660 	if (ctx->state < DELTA_STATE_READY) {
661 		/*
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
665 		 * and alignment...
666 		 */
667 		ret = delta_try_fmt_frame(file, fh, f);
668 		if (ret) {
669 			dev_dbg(delta->dev,
670 				"%s V4L2 S_FMT (CAPTURE): unsupported format %4.4s\n",
671 				ctx->name, (char *)&pix->pixelformat);
672 			return ret;
673 		}
674 
675 		return 0;
676 	}
677 
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);
692 	if (ret)
693 		return ret;
694 
695 	/* then get what decoder can really do */
696 	ret = call_dec_op(dec, get_frameinfo, ctx, &frameinfo);
697 	if (ret)
698 		return ret;
699 
700 	ctx->flags |= DELTA_FLAG_FRAMEINFO;
701 	ctx->frameinfo = frameinfo;
702 	dev_dbg(delta->dev,
703 		"%s V4L2 SET_FMT (CAPTURE): frameinfo updated to %s\n",
704 		ctx->name,
705 		delta_frameinfo_str(&frameinfo, str, sizeof(str)));
706 
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;
717 
718 	return 0;
719 }
720 
721 static int delta_g_selection(struct file *file, void *fh,
722 			     struct v4l2_selection *s)
723 {
724 	struct delta_ctx *ctx = to_ctx(fh);
725 	struct delta_frameinfo *frameinfo = &ctx->frameinfo;
726 	struct v4l2_rect crop;
727 
728 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
729 		return -EINVAL;
730 
731 	if ((ctx->flags & DELTA_FLAG_FRAMEINFO) &&
732 	    (frameinfo->flags & DELTA_FRAMEINFO_FLAG_CROP)) {
733 		crop = frameinfo->crop;
734 	} else {
735 		/* default to video dimensions */
736 		crop.left = 0;
737 		crop.top = 0;
738 		crop.width = frameinfo->width;
739 		crop.height = frameinfo->height;
740 	}
741 
742 	switch (s->target) {
743 	case V4L2_SEL_TGT_COMPOSE:
744 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
745 		/* visible area inside video */
746 		s->r = crop;
747 		break;
748 	case V4L2_SEL_TGT_COMPOSE_PADDED:
749 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
750 		/* up to aligned dimensions */
751 		s->r.left = 0;
752 		s->r.top = 0;
753 		s->r.width = frameinfo->aligned_width;
754 		s->r.height = frameinfo->aligned_height;
755 		break;
756 	default:
757 		return -EINVAL;
758 	}
759 
760 	return 0;
761 }
762 
763 static void delta_complete_eos(struct delta_ctx *ctx,
764 			       struct delta_frame *frame)
765 {
766 	struct delta_dev *delta = ctx->dev;
767 	const struct v4l2_event ev = {.type = V4L2_EVENT_EOS};
768 
769 	/*
770 	 * Send EOS to user:
771 	 * - by returning an empty frame flagged to V4L2_BUF_FLAG_LAST
772 	 * - and then send EOS event
773 	 */
774 
775 	/* empty frame */
776 	frame->info.size = 0;
777 
778 	/* set the last buffer flag */
779 	frame->flags |= V4L2_BUF_FLAG_LAST;
780 
781 	/* release frame to user */
782 	delta_frame_done(ctx, frame, 0);
783 
784 	/* send EOS event */
785 	v4l2_event_queue_fh(&ctx->fh, &ev);
786 
787 	dev_dbg(delta->dev, "%s EOS completed\n", ctx->name);
788 }
789 
790 static int delta_try_decoder_cmd(struct file *file, void *fh,
791 				 struct v4l2_decoder_cmd *cmd)
792 {
793 	if (cmd->cmd != V4L2_DEC_CMD_STOP)
794 		return -EINVAL;
795 
796 	if (cmd->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
797 		return -EINVAL;
798 
799 	if (!(cmd->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) &&
800 	    (cmd->stop.pts != 0))
801 		return -EINVAL;
802 
803 	return 0;
804 }
805 
806 static int delta_decoder_stop_cmd(struct delta_ctx *ctx, void *fh)
807 {
808 	const struct delta_dec *dec = ctx->dec;
809 	struct delta_dev *delta = ctx->dev;
810 	struct delta_frame *frame = NULL;
811 	int ret = 0;
812 
813 	dev_dbg(delta->dev, "%s EOS received\n", ctx->name);
814 
815 	if (ctx->state != DELTA_STATE_READY)
816 		return 0;
817 
818 	/* drain the decoder */
819 	call_dec_op(dec, drain, ctx);
820 
821 	/* release to user drained frames */
822 	while (1) {
823 		frame = NULL;
824 		ret = call_dec_op(dec, get_frame, ctx, &frame);
825 		if (ret == -ENODATA) {
826 			/* no more decoded frames */
827 			break;
828 		}
829 		if (frame) {
830 			dev_dbg(delta->dev, "%s drain frame[%d]\n",
831 				ctx->name, frame->index);
832 
833 			/* pop timestamp and mark frame with it */
834 			delta_pop_dts(ctx, &frame->dts);
835 
836 			/* release decoded frame to user */
837 			delta_frame_done(ctx, frame, 0);
838 		}
839 	}
840 
841 	/* try to complete EOS */
842 	ret = delta_get_free_frame(ctx, &frame);
843 	if (ret)
844 		goto delay_eos;
845 
846 	/* new frame available, EOS can now be completed */
847 	delta_complete_eos(ctx, frame);
848 
849 	ctx->state = DELTA_STATE_EOS;
850 
851 	return 0;
852 
853 delay_eos:
854 	/*
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.
859 	 */
860 	ctx->state = DELTA_STATE_WF_EOS;
861 	dev_dbg(delta->dev, "%s EOS delayed\n", ctx->name);
862 
863 	return 0;
864 }
865 
866 static int delta_decoder_cmd(struct file *file, void *fh,
867 			     struct v4l2_decoder_cmd *cmd)
868 {
869 	struct delta_ctx *ctx = to_ctx(fh);
870 	int ret = 0;
871 
872 	ret = delta_try_decoder_cmd(file, fh, cmd);
873 	if (ret)
874 		return ret;
875 
876 	return delta_decoder_stop_cmd(ctx, fh);
877 }
878 
879 static int delta_subscribe_event(struct v4l2_fh *fh,
880 				 const struct v4l2_event_subscription *sub)
881 {
882 	switch (sub->type) {
883 	case V4L2_EVENT_EOS:
884 		return v4l2_event_subscribe(fh, sub, 2, NULL);
885 	default:
886 		return -EINVAL;
887 	}
888 
889 	return 0;
890 }
891 
892 /* v4l2 ioctl ops */
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,
916 };
917 
918 /*
919  * mem-to-mem operations
920  */
921 
922 static void delta_run_work(struct work_struct *work)
923 {
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;
927 	struct delta_au *au;
928 	struct delta_frame *frame = NULL;
929 	int ret = 0;
930 	bool discard = false;
931 	struct vb2_v4l2_buffer *vbuf;
932 
933 	if (!dec) {
934 		dev_err(delta->dev, "%s no decoder opened yet\n", ctx->name);
935 		return;
936 	}
937 
938 	/* protect instance against reentrancy */
939 	mutex_lock(&ctx->lock);
940 
941 	vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
942 	if (!vbuf) {
943 		dev_err(delta->dev, "%s no buffer to decode\n", ctx->name);
944 		mutex_unlock(&ctx->lock);
945 		return;
946 	}
947 	au = to_au(vbuf);
948 	au->size = vb2_get_plane_payload(&vbuf->vb2_buf, 0);
949 	au->dts = vbuf->vb2_buf.timestamp;
950 
951 	/* dump access unit */
952 	dump_au(ctx, au);
953 
954 	/* enable the hardware */
955 	if (!dec->pm) {
956 		ret = delta_get_sync(ctx);
957 		if (ret)
958 			goto err;
959 	}
960 
961 	/* decode this access unit */
962 	ret = call_dec_op(dec, decode, ctx, au);
963 
964 	/*
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
972 	 */
973 	if (ret == -ENODATA) {
974 		discard = true;
975 	} else if (ret) {
976 		dev_err(delta->dev, "%s decoding failed (%d)\n",
977 			ctx->name, ret);
978 
979 		/* disable the hardware */
980 		if (!dec->pm)
981 			delta_put_autosuspend(ctx);
982 
983 		goto err;
984 	}
985 
986 	/* disable the hardware */
987 	if (!dec->pm)
988 		delta_put_autosuspend(ctx);
989 
990 	/* push au timestamp in FIFO */
991 	if (!discard)
992 		delta_push_dts(ctx, au->dts);
993 
994 	/* get available decoded frames */
995 	while (1) {
996 		ret = call_dec_op(dec, get_frame, ctx, &frame);
997 		if (ret == -ENODATA) {
998 			/* no more decoded frames */
999 			goto out;
1000 		}
1001 		if (ret) {
1002 			dev_err(delta->dev, "%s  cannot get decoded frame (%d)\n",
1003 				ctx->name, ret);
1004 			goto out;
1005 		}
1006 		if (!frame) {
1007 			dev_err(delta->dev,
1008 				"%s  NULL decoded frame\n",
1009 				ctx->name);
1010 			goto out;
1011 		}
1012 
1013 		/* pop timestamp and mark frame with it */
1014 		delta_pop_dts(ctx, &frame->dts);
1015 
1016 		/* release decoded frame to user */
1017 		delta_frame_done(ctx, frame, 0);
1018 	}
1019 
1020 out:
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);
1025 	return;
1026 
1027 err:
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);
1032 }
1033 
1034 static void delta_device_run(void *priv)
1035 {
1036 	struct delta_ctx *ctx = priv;
1037 	struct delta_dev *delta = ctx->dev;
1038 
1039 	queue_work(delta->work_queue, &ctx->run_work);
1040 }
1041 
1042 static void delta_job_abort(void *priv)
1043 {
1044 	struct delta_ctx *ctx = priv;
1045 	struct delta_dev *delta = ctx->dev;
1046 
1047 	dev_dbg(delta->dev, "%s aborting job\n", ctx->name);
1048 
1049 	ctx->aborting = true;
1050 }
1051 
1052 static int delta_job_ready(void *priv)
1053 {
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);
1057 
1058 	if (!src_bufs) {
1059 		dev_dbg(delta->dev, "%s not ready: not enough video buffers.\n",
1060 			ctx->name);
1061 		return 0;
1062 	}
1063 
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",
1066 			ctx->name);
1067 		return 0;
1068 	}
1069 
1070 	if (ctx->aborting) {
1071 		dev_dbg(delta->dev, "%s job not ready: aborting\n", ctx->name);
1072 		return 0;
1073 	}
1074 
1075 	dev_dbg(delta->dev, "%s job ready\n", ctx->name);
1076 
1077 	return 1;
1078 }
1079 
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,
1085 };
1086 
1087 /*
1088  * VB2 queue operations
1089  */
1090 
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[])
1096 {
1097 	struct delta_ctx *ctx = vb2_get_drv_priv(vq);
1098 	unsigned int size = ctx->max_au_size;
1099 
1100 	if (*num_planes)
1101 		return sizes[0] < size ? -EINVAL : 0;
1102 
1103 	*num_planes = 1;
1104 	if (*num_buffers < 1)
1105 		*num_buffers = 1;
1106 	if (*num_buffers > DELTA_MAX_AUS)
1107 		*num_buffers = DELTA_MAX_AUS;
1108 
1109 	sizes[0] = size;
1110 
1111 	return 0;
1112 }
1113 
1114 static int delta_vb2_au_prepare(struct vb2_buffer *vb)
1115 {
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);
1121 
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);
1130 	}
1131 
1132 	if (vbuf->field == V4L2_FIELD_ANY)
1133 		vbuf->field = V4L2_FIELD_NONE;
1134 
1135 	return 0;
1136 }
1137 
1138 static int delta_setup_frame(struct delta_ctx *ctx,
1139 			     struct delta_frame *frame)
1140 {
1141 	struct delta_dev *delta = ctx->dev;
1142 	const struct delta_dec *dec = ctx->dec;
1143 
1144 	if (frame->index >= DELTA_MAX_FRAMES) {
1145 		dev_err(delta->dev,
1146 			"%s frame index=%d exceeds output frame count (%d)\n",
1147 			ctx->name, frame->index, DELTA_MAX_FRAMES);
1148 		return -EINVAL;
1149 	}
1150 
1151 	if (ctx->nb_of_frames >= DELTA_MAX_FRAMES) {
1152 		dev_err(delta->dev,
1153 			"%s number of frames exceeds output frame count (%d > %d)\n",
1154 			ctx->name, ctx->nb_of_frames, DELTA_MAX_FRAMES);
1155 		return -EINVAL;
1156 	}
1157 
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);
1162 	}
1163 
1164 	frame->state = DELTA_FRAME_FREE;
1165 	ctx->frames[ctx->nb_of_frames] = frame;
1166 	ctx->nb_of_frames++;
1167 
1168 	/* setup frame on decoder side */
1169 	return call_dec_op(dec, setup_frame, ctx, frame);
1170 }
1171 
1172 /*
1173  * default implementation of get_frameinfo decoder ops
1174  * matching frame information from stream information
1175  * & with default pixel format & default alignment.
1176  */
1177 int delta_get_frameinfo_default(struct delta_ctx *ctx,
1178 				struct delta_frameinfo *frameinfo)
1179 {
1180 	struct delta_streaminfo *streaminfo = &ctx->streaminfo;
1181 
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;
1196 	}
1197 	if (streaminfo->flags & DELTA_STREAMINFO_FLAG_PIXELASPECT) {
1198 		frameinfo->flags |= DELTA_FRAMEINFO_FLAG_PIXELASPECT;
1199 		frameinfo->pixelaspect = streaminfo->pixelaspect;
1200 	}
1201 	frameinfo->field = streaminfo->field;
1202 
1203 	return 0;
1204 }
1205 
1206 /*
1207  * default implementation of recycle decoder ops
1208  * consisting to relax the "decoded" frame state
1209  */
1210 int delta_recycle_default(struct delta_ctx *pctx,
1211 			  struct delta_frame *frame)
1212 {
1213 	frame->state &= ~DELTA_FRAME_DEC;
1214 
1215 	return 0;
1216 }
1217 
1218 static void dump_frames_status(struct delta_ctx *ctx)
1219 {
1220 	struct delta_dev *delta = ctx->dev;
1221 	unsigned int i;
1222 	struct delta_frame *frame;
1223 	unsigned char str[100] = "";
1224 
1225 	dev_info(delta->dev,
1226 		 "%s dumping frames status...\n", ctx->name);
1227 
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,
1234 					 str, sizeof(str)));
1235 	}
1236 }
1237 
1238 int delta_get_free_frame(struct delta_ctx *ctx,
1239 			 struct delta_frame **pframe)
1240 {
1241 	struct delta_dev *delta = ctx->dev;
1242 	struct vb2_v4l2_buffer *vbuf;
1243 	struct delta_frame *frame;
1244 
1245 	*pframe = NULL;
1246 
1247 	vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1248 	if (!vbuf) {
1249 		dev_err(delta->dev, "%s no frame available",
1250 			ctx->name);
1251 		return -EIO;
1252 	}
1253 
1254 	frame = to_frame(vbuf);
1255 	frame->state &= ~DELTA_FRAME_M2M;
1256 	if (frame->state != DELTA_FRAME_FREE) {
1257 		dev_err(delta->dev,
1258 			"%s frame[%d] is not free\n",
1259 			ctx->name, frame->index);
1260 		dump_frames_status(ctx);
1261 		return -ENODATA;
1262 	}
1263 
1264 	dev_dbg(delta->dev,
1265 		"%s get free frame[%d]\n", ctx->name, frame->index);
1266 
1267 	*pframe = frame;
1268 	return 0;
1269 }
1270 
1271 int delta_get_sync(struct delta_ctx *ctx)
1272 {
1273 	struct delta_dev *delta = ctx->dev;
1274 	int ret = 0;
1275 
1276 	/* enable the hardware */
1277 	ret = pm_runtime_resume_and_get(delta->dev);
1278 	if (ret < 0) {
1279 		dev_err(delta->dev, "%s pm_runtime_resume_and_get failed (%d)\n",
1280 			__func__, ret);
1281 		return ret;
1282 	}
1283 
1284 	return 0;
1285 }
1286 
1287 void delta_put_autosuspend(struct delta_ctx *ctx)
1288 {
1289 	struct delta_dev *delta = ctx->dev;
1290 
1291 	pm_runtime_put_autosuspend(delta->dev);
1292 }
1293 
1294 static void delta_vb2_au_queue(struct vb2_buffer *vb)
1295 {
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);
1299 
1300 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1301 }
1302 
1303 static int delta_vb2_au_start_streaming(struct vb2_queue *q,
1304 					unsigned int count)
1305 {
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;
1310 	int ret = 0;
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] = "";
1316 
1317 	if ((ctx->state != DELTA_STATE_WF_FORMAT) &&
1318 	    (ctx->state != DELTA_STATE_WF_STREAMINFO))
1319 		return 0;
1320 
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);
1326 		if (ret)
1327 			goto err;
1328 		ctx->dec = dec;
1329 		ctx->state = DELTA_STATE_WF_STREAMINFO;
1330 	}
1331 
1332 	/*
1333 	 * first buffer should contain stream header,
1334 	 * decode it to get the infos related to stream
1335 	 * such as width, height, dpb, ...
1336 	 */
1337 	vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1338 	if (!vbuf) {
1339 		dev_err(delta->dev, "%s failed to start streaming, no stream header buffer enqueued\n",
1340 			ctx->name);
1341 		ret = -EINVAL;
1342 		goto err;
1343 	}
1344 	au = to_au(vbuf);
1345 	au->size = vb2_get_plane_payload(&vbuf->vb2_buf, 0);
1346 	au->dts = vbuf->vb2_buf.timestamp;
1347 
1348 	delta_push_dts(ctx, au->dts);
1349 
1350 	/* dump access unit */
1351 	dump_au(ctx, au);
1352 
1353 	/* decode this access unit */
1354 	ret = call_dec_op(dec, decode, ctx, au);
1355 	if (ret) {
1356 		dev_err(delta->dev, "%s failed to start streaming, header decoding failed (%d)\n",
1357 			ctx->name, ret);
1358 		goto err;
1359 	}
1360 
1361 	ret = call_dec_op(dec, get_streaminfo, ctx, streaminfo);
1362 	if (ret) {
1363 		dev_dbg_ratelimited(delta->dev,
1364 				    "%s failed to start streaming, valid stream header not yet decoded\n",
1365 				    ctx->name);
1366 		goto err;
1367 	}
1368 	ctx->flags |= DELTA_FLAG_STREAMINFO;
1369 
1370 	ret = call_dec_op(dec, get_frameinfo, ctx, frameinfo);
1371 	if (ret)
1372 		goto err;
1373 	ctx->flags |= DELTA_FLAG_FRAMEINFO;
1374 
1375 	ctx->state = DELTA_STATE_READY;
1376 
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)));
1380 
1381 	delta_au_done(ctx, au, ret);
1382 	return 0;
1383 
1384 err:
1385 	/*
1386 	 * return all buffers to vb2 in QUEUED state.
1387 	 * This will give ownership back to userspace
1388 	 */
1389 	if (vbuf)
1390 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
1391 
1392 	while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1393 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
1394 	return ret;
1395 }
1396 
1397 static void delta_vb2_au_stop_streaming(struct vb2_queue *q)
1398 {
1399 	struct delta_ctx *ctx = vb2_get_drv_priv(q);
1400 	struct vb2_v4l2_buffer *vbuf;
1401 
1402 	delta_flush_dts(ctx);
1403 
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);
1407 
1408 	ctx->au_num = 0;
1409 
1410 	ctx->aborting = false;
1411 }
1412 
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[])
1418 {
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;
1424 
1425 	/*
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)
1430 	 */
1431 	if (*num_buffers < DELTA_MIN_FRAME_USER) {
1432 		dev_dbg(delta->dev,
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;
1436 	}
1437 
1438 	*num_buffers += streaminfo->dpb + DELTA_PEAK_FRAME_SMOOTHING;
1439 
1440 	if (*num_buffers > DELTA_MAX_FRAMES) {
1441 		dev_dbg(delta->dev,
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;
1445 	}
1446 
1447 	if (*num_planes)
1448 		return sizes[0] < size ? -EINVAL : 0;
1449 
1450 	/* single plane for Y and CbCr */
1451 	*num_planes = 1;
1452 
1453 	sizes[0] = size;
1454 
1455 	ctx->nb_of_frames = 0;
1456 
1457 	return 0;
1458 }
1459 
1460 static int delta_vb2_frame_prepare(struct vb2_buffer *vb)
1461 {
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);
1467 	int ret = 0;
1468 
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;
1474 
1475 		ret = delta_setup_frame(ctx, frame);
1476 		if (ret) {
1477 			dev_err(delta->dev,
1478 				"%s setup_frame() failed (%d)\n",
1479 				ctx->name, ret);
1480 			return ret;
1481 		}
1482 		frame->prepared = true;
1483 		dev_dbg(delta->dev,
1484 			"%s frame[%d] prepared; virt=0x%p, phy=0x%pad\n",
1485 			ctx->name, vb->index, frame->vaddr,
1486 			&frame->paddr);
1487 	}
1488 
1489 	frame->flags = vbuf->flags;
1490 
1491 	return 0;
1492 }
1493 
1494 static void delta_vb2_frame_finish(struct vb2_buffer *vb)
1495 {
1496 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1497 	struct delta_frame *frame = to_frame(vbuf);
1498 
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;
1504 }
1505 
1506 static void delta_vb2_frame_queue(struct vb2_buffer *vb)
1507 {
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);
1512 
1513 	if (ctx->state == DELTA_STATE_WF_EOS) {
1514 		/* new frame available, EOS can now be completed */
1515 		delta_complete_eos(ctx, frame);
1516 
1517 		ctx->state = DELTA_STATE_EOS;
1518 
1519 		/* return, no need to recycle this buffer to decoder */
1520 		return;
1521 	}
1522 
1523 	/* recycle this frame */
1524 	delta_recycle(ctx, frame);
1525 }
1526 
1527 static void delta_vb2_frame_stop_streaming(struct vb2_queue *q)
1528 {
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;
1533 	unsigned int i;
1534 
1535 	delta_flush_dts(ctx);
1536 
1537 	call_dec_op(dec, flush, ctx);
1538 
1539 	/*
1540 	 * return all buffers to vb2 in ERROR state
1541 	 * & reset each frame state to OUT
1542 	 */
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);
1548 		}
1549 		frame->state = DELTA_FRAME_OUT;
1550 	}
1551 
1552 	ctx->frame_num = 0;
1553 
1554 	ctx->aborting = false;
1555 }
1556 
1557 /* VB2 queue ops */
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,
1566 };
1567 
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,
1576 };
1577 
1578 /*
1579  * V4L2 file operations
1580  */
1581 
1582 static int queue_init(void *priv,
1583 		      struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
1584 {
1585 	struct vb2_queue *q;
1586 	struct delta_ctx *ctx = priv;
1587 	struct delta_dev *delta = ctx->dev;
1588 	int ret;
1589 
1590 	/* setup vb2 queue for stream input */
1591 	q = src_vq;
1592 	q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1593 	q->io_modes = VB2_MMAP | VB2_DMABUF;
1594 	q->drv_priv = ctx;
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;
1602 
1603 	ret = vb2_queue_init(q);
1604 	if (ret)
1605 		return ret;
1606 
1607 	/* setup vb2 queue for frame output */
1608 	q = dst_vq;
1609 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1610 	q->io_modes = VB2_MMAP | VB2_DMABUF;
1611 	q->drv_priv = ctx;
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;
1620 
1621 	return vb2_queue_init(q);
1622 }
1623 
1624 static int delta_open(struct file *file)
1625 {
1626 	struct delta_dev *delta = video_drvdata(file);
1627 	struct delta_ctx *ctx = NULL;
1628 	int ret = 0;
1629 
1630 	mutex_lock(&delta->lock);
1631 
1632 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1633 	if (!ctx) {
1634 		ret = -ENOMEM;
1635 		goto err;
1636 	}
1637 	ctx->dev = delta;
1638 
1639 	v4l2_fh_init(&ctx->fh, video_devdata(file));
1640 	file->private_data = &ctx->fh;
1641 	v4l2_fh_add(&ctx->fh);
1642 
1643 	INIT_WORK(&ctx->run_work, delta_run_work);
1644 	mutex_init(&ctx->lock);
1645 
1646 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(delta->m2m_dev, ctx,
1647 					    queue_init);
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",
1651 			DELTA_PREFIX, ret);
1652 		goto err_fh_del;
1653 	}
1654 
1655 	/*
1656 	 * wait stream format to determine which
1657 	 * decoder to open
1658 	 */
1659 	ctx->state = DELTA_STATE_WF_FORMAT;
1660 
1661 	INIT_LIST_HEAD(&ctx->dts);
1662 
1663 	/* set the instance name */
1664 	delta->instance_id++;
1665 	snprintf(ctx->name, sizeof(ctx->name), "[%3d:----]",
1666 		 delta->instance_id);
1667 
1668 	/* default parameters for frame and stream */
1669 	set_default_params(ctx);
1670 
1671 	/* enable ST231 clocks */
1672 	if (clk_prepare_enable(delta->clk_st231))
1673 		dev_warn(delta->dev, "failed to enable st231 clk\n");
1674 
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");
1678 
1679 	mutex_unlock(&delta->lock);
1680 
1681 	dev_dbg(delta->dev, "%s decoder instance created\n", ctx->name);
1682 
1683 	return 0;
1684 
1685 err_fh_del:
1686 	v4l2_fh_del(&ctx->fh);
1687 	v4l2_fh_exit(&ctx->fh);
1688 	kfree(ctx);
1689 err:
1690 	mutex_unlock(&delta->lock);
1691 
1692 	return ret;
1693 }
1694 
1695 static int delta_release(struct file *file)
1696 {
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;
1700 
1701 	mutex_lock(&delta->lock);
1702 
1703 	/* close decoder */
1704 	call_dec_op(dec, close, ctx);
1705 
1706 	/*
1707 	 * trace a summary of instance
1708 	 * before closing (debug purpose)
1709 	 */
1710 	delta_trace_summary(ctx);
1711 
1712 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1713 
1714 	v4l2_fh_del(&ctx->fh);
1715 	v4l2_fh_exit(&ctx->fh);
1716 
1717 	/* disable ST231 clocks */
1718 	clk_disable_unprepare(delta->clk_st231);
1719 
1720 	/* disable FLASH_PROMIP clock */
1721 	clk_disable_unprepare(delta->clk_flash_promip);
1722 
1723 	dev_dbg(delta->dev, "%s decoder instance released\n", ctx->name);
1724 
1725 	kfree(ctx);
1726 
1727 	mutex_unlock(&delta->lock);
1728 	return 0;
1729 }
1730 
1731 /* V4L2 file ops */
1732 static const struct v4l2_file_operations delta_fops = {
1733 	.owner = THIS_MODULE,
1734 	.open = delta_open,
1735 	.release = delta_release,
1736 	.unlocked_ioctl = video_ioctl2,
1737 	.mmap = v4l2_m2m_fop_mmap,
1738 	.poll = v4l2_m2m_fop_poll,
1739 };
1740 
1741 /*
1742  * Platform device operations
1743  */
1744 
1745 static int delta_register_device(struct delta_dev *delta)
1746 {
1747 	int ret;
1748 	struct video_device *vdev;
1749 
1750 	if (!delta)
1751 		return -ENODEV;
1752 
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",
1756 			DELTA_PREFIX);
1757 		ret = PTR_ERR(delta->m2m_dev);
1758 		goto err;
1759 	}
1760 
1761 	vdev = video_device_alloc();
1762 	if (!vdev) {
1763 		dev_err(delta->dev, "%s failed to allocate video device\n",
1764 			DELTA_PREFIX);
1765 		ret = -ENOMEM;
1766 		goto err_m2m_release;
1767 	}
1768 
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);
1778 
1779 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1780 	if (ret) {
1781 		dev_err(delta->dev, "%s failed to register video device\n",
1782 			DELTA_PREFIX);
1783 		goto err_vdev_release;
1784 	}
1785 
1786 	delta->vdev = vdev;
1787 	video_set_drvdata(vdev, delta);
1788 	return 0;
1789 
1790 err_vdev_release:
1791 	video_device_release(vdev);
1792 err_m2m_release:
1793 	v4l2_m2m_release(delta->m2m_dev);
1794 err:
1795 	return ret;
1796 }
1797 
1798 static void delta_unregister_device(struct delta_dev *delta)
1799 {
1800 	if (!delta)
1801 		return;
1802 
1803 	if (delta->m2m_dev)
1804 		v4l2_m2m_release(delta->m2m_dev);
1805 
1806 	video_unregister_device(delta->vdev);
1807 }
1808 
1809 static int delta_probe(struct platform_device *pdev)
1810 {
1811 	struct delta_dev *delta;
1812 	struct device *dev = &pdev->dev;
1813 	int ret;
1814 
1815 	delta = devm_kzalloc(dev, sizeof(*delta), GFP_KERNEL);
1816 	if (!delta) {
1817 		ret = -ENOMEM;
1818 		goto err;
1819 	}
1820 
1821 	delta->dev = dev;
1822 	delta->pdev = pdev;
1823 	platform_set_drvdata(pdev, delta);
1824 
1825 	mutex_init(&delta->lock);
1826 
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;
1832 	}
1833 
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;
1838 	}
1839 
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",
1843 			DELTA_PREFIX);
1844 		delta->clk_flash_promip = NULL;
1845 	}
1846 
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);
1852 
1853 	/* init firmware ipc channel */
1854 	ret = delta_ipc_init(delta);
1855 	if (ret) {
1856 		dev_err(delta->dev, "%s failed to initialize firmware ipc channel\n",
1857 			DELTA_PREFIX);
1858 		goto err_pm_disable;
1859 	}
1860 
1861 	/* register all available decoders */
1862 	register_decoders(delta);
1863 
1864 	/* register all supported formats */
1865 	register_formats(delta);
1866 
1867 	/* register on V4L2 */
1868 	ret = v4l2_device_register(dev, &delta->v4l2_dev);
1869 	if (ret) {
1870 		dev_err(delta->dev, "%s failed to register V4L2 device\n",
1871 			DELTA_PREFIX);
1872 		goto err_pm_disable;
1873 	}
1874 
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",
1878 			DELTA_PREFIX);
1879 		ret = -ENOMEM;
1880 		goto err_v4l2;
1881 	}
1882 
1883 	/* register device */
1884 	ret = delta_register_device(delta);
1885 	if (ret)
1886 		goto err_work_queue;
1887 
1888 	dev_info(dev, "%s %s registered as /dev/video%d\n",
1889 		 DELTA_PREFIX, delta->vdev->name, delta->vdev->num);
1890 
1891 	return 0;
1892 
1893 err_work_queue:
1894 	destroy_workqueue(delta->work_queue);
1895 err_v4l2:
1896 	v4l2_device_unregister(&delta->v4l2_dev);
1897 err_pm_disable:
1898 	pm_runtime_disable(dev);
1899 err:
1900 	return ret;
1901 }
1902 
1903 static void delta_remove(struct platform_device *pdev)
1904 {
1905 	struct delta_dev *delta = platform_get_drvdata(pdev);
1906 
1907 	delta_ipc_exit(delta);
1908 
1909 	delta_unregister_device(delta);
1910 
1911 	destroy_workqueue(delta->work_queue);
1912 
1913 	pm_runtime_put_autosuspend(delta->dev);
1914 	pm_runtime_disable(delta->dev);
1915 
1916 	v4l2_device_unregister(&delta->v4l2_dev);
1917 }
1918 
1919 static int delta_runtime_suspend(struct device *dev)
1920 {
1921 	struct delta_dev *delta = dev_get_drvdata(dev);
1922 
1923 	clk_disable_unprepare(delta->clk_delta);
1924 
1925 	return 0;
1926 }
1927 
1928 static int delta_runtime_resume(struct device *dev)
1929 {
1930 	struct delta_dev *delta = dev_get_drvdata(dev);
1931 
1932 	if (clk_prepare_enable(delta->clk_delta))
1933 		dev_warn(dev, "failed to prepare/enable delta clk\n");
1934 
1935 	return 0;
1936 }
1937 
1938 /* PM ops */
1939 static const struct dev_pm_ops delta_pm_ops = {
1940 	.runtime_suspend = delta_runtime_suspend,
1941 	.runtime_resume = delta_runtime_resume,
1942 };
1943 
1944 static const struct of_device_id delta_match_types[] = {
1945 	{
1946 	 .compatible = "st,st-delta",
1947 	},
1948 	{
1949 	 /* end node */
1950 	}
1951 };
1952 
1953 MODULE_DEVICE_TABLE(of, delta_match_types);
1954 
1955 static struct platform_driver delta_driver = {
1956 	.probe = delta_probe,
1957 	.remove_new = delta_remove,
1958 	.driver = {
1959 		   .name = DELTA_NAME,
1960 		   .of_match_table = delta_match_types,
1961 		   .pm = &delta_pm_ops},
1962 };
1963 
1964 module_platform_driver(delta_driver);
1965 
1966 MODULE_LICENSE("GPL");
1967 MODULE_AUTHOR("Hugues Fruchet <hugues.fruchet@st.com>");
1968 MODULE_DESCRIPTION("STMicroelectronics DELTA video decoder V4L2 driver");
1969