summaryrefslogtreecommitdiff
path: root/drivers/media/platform/ti-vpe/cal-video.c
blob: 15fb5360cf13ca1721fc08ce879d5918af264327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
// SPDX-License-Identifier: GPL-2.0-only
/*
 * TI Camera Access Layer (CAL) - Video Device
 *
 * Copyright (c) 2015-2020 Texas Instruments Inc.
 *
 * Authors:
 *	Benoit Parrot <bparrot@ti.com>
 *	Laurent Pinchart <laurent.pinchart@ideasonboard.com>
 */

#include <linux/ioctl.h>
#include <linux/pm_runtime.h>
#include <linux/videodev2.h>

#include <media/media-device.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-event.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-ioctl.h>
#include <media/videobuf2-core.h>
#include <media/videobuf2-dma-contig.h>

#include "cal.h"

/*  Print Four-character-code (FOURCC) */
static char *fourcc_to_str(u32 fmt)
{
	static char code[5];

	code[0] = (unsigned char)(fmt & 0xff);
	code[1] = (unsigned char)((fmt >> 8) & 0xff);
	code[2] = (unsigned char)((fmt >> 16) & 0xff);
	code[3] = (unsigned char)((fmt >> 24) & 0xff);
	code[4] = '\0';

	return code;
}

/* ------------------------------------------------------------------
 *	V4L2 Common IOCTLs
 * ------------------------------------------------------------------
 */

static int cal_querycap(struct file *file, void *priv,
			struct v4l2_capability *cap)
{
	struct cal_ctx *ctx = video_drvdata(file);

	strscpy(cap->driver, CAL_MODULE_NAME, sizeof(cap->driver));
	strscpy(cap->card, CAL_MODULE_NAME, sizeof(cap->card));

	snprintf(cap->bus_info, sizeof(cap->bus_info),
		 "platform:%s", dev_name(ctx->cal->dev));
	return 0;
}

static int cal_g_fmt_vid_cap(struct file *file, void *priv,
			     struct v4l2_format *f)
{
	struct cal_ctx *ctx = video_drvdata(file);

	*f = ctx->v_fmt;

	return 0;
}

/* ------------------------------------------------------------------
 *	V4L2 Video Node Centric IOCTLs
 * ------------------------------------------------------------------
 */

static const struct cal_format_info *find_format_by_pix(struct cal_ctx *ctx,
							u32 pixelformat)
{
	const struct cal_format_info *fmtinfo;
	unsigned int k;

	for (k = 0; k < ctx->num_active_fmt; k++) {
		fmtinfo = ctx->active_fmt[k];
		if (fmtinfo->fourcc == pixelformat)
			return fmtinfo;
	}

	return NULL;
}

static const struct cal_format_info *find_format_by_code(struct cal_ctx *ctx,
							 u32 code)
{
	const struct cal_format_info *fmtinfo;
	unsigned int k;

	for (k = 0; k < ctx->num_active_fmt; k++) {
		fmtinfo = ctx->active_fmt[k];
		if (fmtinfo->code == code)
			return fmtinfo;
	}

	return NULL;
}

static int cal_enum_fmt_vid_cap(struct file *file, void  *priv,
				struct v4l2_fmtdesc *f)
{
	struct cal_ctx *ctx = video_drvdata(file);
	const struct cal_format_info *fmtinfo;

	if (f->index >= ctx->num_active_fmt)
		return -EINVAL;

	fmtinfo = ctx->active_fmt[f->index];

	f->pixelformat = fmtinfo->fourcc;
	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	return 0;
}

static int __subdev_get_format(struct cal_ctx *ctx,
			       struct v4l2_mbus_framefmt *fmt)
{
	struct v4l2_subdev_format sd_fmt;
	struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
	int ret;

	sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
	sd_fmt.pad = 0;

	ret = v4l2_subdev_call(ctx->phy->sensor, pad, get_fmt, NULL, &sd_fmt);
	if (ret)
		return ret;

	*fmt = *mbus_fmt;

	ctx_dbg(1, ctx, "%s %dx%d code:%04X\n", __func__,
		fmt->width, fmt->height, fmt->code);

	return 0;
}

static int __subdev_set_format(struct cal_ctx *ctx,
			       struct v4l2_mbus_framefmt *fmt)
{
	struct v4l2_subdev_format sd_fmt;
	struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
	int ret;

	sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
	sd_fmt.pad = 0;
	*mbus_fmt = *fmt;

	ret = v4l2_subdev_call(ctx->phy->sensor, pad, set_fmt, NULL, &sd_fmt);
	if (ret)
		return ret;

	ctx_dbg(1, ctx, "%s %dx%d code:%04X\n", __func__,
		fmt->width, fmt->height, fmt->code);

	return 0;
}

static void cal_calc_format_size(struct cal_ctx *ctx,
				 const struct cal_format_info *fmtinfo,
				 struct v4l2_format *f)
{
	u32 bpl, max_width;

	/*
	 * Maximum width is bound by the DMA max width in bytes.
	 * We need to recalculate the actual maxi width depending on the
	 * number of bytes per pixels required.
	 */
	max_width = CAL_MAX_WIDTH_BYTES / (ALIGN(fmtinfo->bpp, 8) >> 3);
	v4l_bound_align_image(&f->fmt.pix.width, 48, max_width, 2,
			      &f->fmt.pix.height, 32, CAL_MAX_HEIGHT_LINES,
			      0, 0);

	bpl = (f->fmt.pix.width * ALIGN(fmtinfo->bpp, 8)) >> 3;
	f->fmt.pix.bytesperline = ALIGN(bpl, 16);

	f->fmt.pix.sizeimage = f->fmt.pix.height *
			       f->fmt.pix.bytesperline;

	ctx_dbg(3, ctx, "%s: fourcc: %s size: %dx%d bpl:%d img_size:%d\n",
		__func__, fourcc_to_str(f->fmt.pix.pixelformat),
		f->fmt.pix.width, f->fmt.pix.height,
		f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
}

static int cal_try_fmt_vid_cap(struct file *file, void *priv,
			       struct v4l2_format *f)
{
	struct cal_ctx *ctx = video_drvdata(file);
	const struct cal_format_info *fmtinfo;
	struct v4l2_subdev_frame_size_enum fse;
	int ret, found;

	fmtinfo = find_format_by_pix(ctx, f->fmt.pix.pixelformat);
	if (!fmtinfo) {
		ctx_dbg(3, ctx, "Fourcc format (0x%08x) not found.\n",
			f->fmt.pix.pixelformat);

		/* Just get the first one enumerated */
		fmtinfo = ctx->active_fmt[0];
		f->fmt.pix.pixelformat = fmtinfo->fourcc;
	}

	f->fmt.pix.field = ctx->v_fmt.fmt.pix.field;

	/* check for/find a valid width/height */
	ret = 0;
	found = false;
	fse.pad = 0;
	fse.code = fmtinfo->code;
	fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
	for (fse.index = 0; ; fse.index++) {
		ret = v4l2_subdev_call(ctx->phy->sensor, pad, enum_frame_size,
				       NULL, &fse);
		if (ret)
			break;

		if ((f->fmt.pix.width == fse.max_width) &&
		    (f->fmt.pix.height == fse.max_height)) {
			found = true;
			break;
		} else if ((f->fmt.pix.width >= fse.min_width) &&
			 (f->fmt.pix.width <= fse.max_width) &&
			 (f->fmt.pix.height >= fse.min_height) &&
			 (f->fmt.pix.height <= fse.max_height)) {
			found = true;
			break;
		}
	}

	if (!found) {
		/* use existing values as default */
		f->fmt.pix.width = ctx->v_fmt.fmt.pix.width;
		f->fmt.pix.height =  ctx->v_fmt.fmt.pix.height;
	}

	/*
	 * Use current colorspace for now, it will get
	 * updated properly during s_fmt
	 */
	f->fmt.pix.colorspace = ctx->v_fmt.fmt.pix.colorspace;
	cal_calc_format_size(ctx, fmtinfo, f);
	return 0;
}

static int cal_s_fmt_vid_cap(struct file *file, void *priv,
			     struct v4l2_format *f)
{
	struct cal_ctx *ctx = video_drvdata(file);
	struct vb2_queue *q = &ctx->vb_vidq;
	struct v4l2_subdev_format sd_fmt = {
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
		.pad = CAL_CAMERARX_PAD_SINK,
	};
	const struct cal_format_info *fmtinfo;
	int ret;

	if (vb2_is_busy(q)) {
		ctx_dbg(3, ctx, "%s device busy\n", __func__);
		return -EBUSY;
	}

	ret = cal_try_fmt_vid_cap(file, priv, f);
	if (ret < 0)
		return ret;

	fmtinfo = find_format_by_pix(ctx, f->fmt.pix.pixelformat);

	v4l2_fill_mbus_format(&sd_fmt.format, &f->fmt.pix, fmtinfo->code);

	ret = __subdev_set_format(ctx, &sd_fmt.format);
	if (ret)
		return ret;

	/* Just double check nothing has gone wrong */
	if (sd_fmt.format.code != fmtinfo->code) {
		ctx_dbg(3, ctx,
			"%s subdev changed format on us, this should not happen\n",
			__func__);
		return -EINVAL;
	}

	v4l2_fill_pix_format(&ctx->v_fmt.fmt.pix, &sd_fmt.format);
	ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	ctx->v_fmt.fmt.pix.pixelformat = fmtinfo->fourcc;
	ctx->v_fmt.fmt.pix.field = sd_fmt.format.field;
	cal_calc_format_size(ctx, fmtinfo, &ctx->v_fmt);

	v4l2_subdev_call(&ctx->phy->subdev, pad, set_fmt, NULL, &sd_fmt);

	ctx->fmtinfo = fmtinfo;
	*f = ctx->v_fmt;

	return 0;
}

static int cal_enum_framesizes(struct file *file, void *fh,
			       struct v4l2_frmsizeenum *fsize)
{
	struct cal_ctx *ctx = video_drvdata(file);
	const struct cal_format_info *fmtinfo;
	struct v4l2_subdev_frame_size_enum fse;
	int ret;

	/* check for valid format */
	fmtinfo = find_format_by_pix(ctx, fsize->pixel_format);
	if (!fmtinfo) {
		ctx_dbg(3, ctx, "Invalid pixel code: %x\n",
			fsize->pixel_format);
		return -EINVAL;
	}

	fse.index = fsize->index;
	fse.pad = 0;
	fse.code = fmtinfo->code;
	fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;

	ret = v4l2_subdev_call(ctx->phy->sensor, pad, enum_frame_size, NULL,
			       &fse);
	if (ret)
		return ret;

	ctx_dbg(1, ctx, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
		__func__, fse.index, fse.code, fse.min_width, fse.max_width,
		fse.min_height, fse.max_height);

	fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
	fsize->discrete.width = fse.max_width;
	fsize->discrete.height = fse.max_height;

	return 0;
}

static int cal_enum_input(struct file *file, void *priv,
			  struct v4l2_input *inp)
{
	if (inp->index > 0)
		return -EINVAL;

	inp->type = V4L2_INPUT_TYPE_CAMERA;
	sprintf(inp->name, "Camera %u", inp->index);
	return 0;
}

static int cal_g_input(struct file *file, void *priv, unsigned int *i)
{
	*i = 0;
	return 0;
}

static int cal_s_input(struct file *file, void *priv, unsigned int i)
{
	return i > 0 ? -EINVAL : 0;
}

/* timeperframe is arbitrary and continuous */
static int cal_enum_frameintervals(struct file *file, void *priv,
				   struct v4l2_frmivalenum *fival)
{
	struct cal_ctx *ctx = video_drvdata(file);
	const struct cal_format_info *fmtinfo;
	struct v4l2_subdev_frame_interval_enum fie = {
		.index = fival->index,
		.width = fival->width,
		.height = fival->height,
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
	};
	int ret;

	fmtinfo = find_format_by_pix(ctx, fival->pixel_format);
	if (!fmtinfo)
		return -EINVAL;

	fie.code = fmtinfo->code;
	ret = v4l2_subdev_call(ctx->phy->sensor, pad, enum_frame_interval,
			       NULL, &fie);
	if (ret)
		return ret;
	fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
	fival->discrete = fie.interval;

	return 0;
}

static const struct v4l2_ioctl_ops cal_ioctl_video_ops = {
	.vidioc_querycap      = cal_querycap,
	.vidioc_enum_fmt_vid_cap  = cal_enum_fmt_vid_cap,
	.vidioc_g_fmt_vid_cap     = cal_g_fmt_vid_cap,
	.vidioc_try_fmt_vid_cap   = cal_try_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap     = cal_s_fmt_vid_cap,
	.vidioc_enum_framesizes   = cal_enum_framesizes,
	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
	.vidioc_create_bufs   = vb2_ioctl_create_bufs,
	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
	.vidioc_querybuf      = vb2_ioctl_querybuf,
	.vidioc_qbuf          = vb2_ioctl_qbuf,
	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
	.vidioc_expbuf        = vb2_ioctl_expbuf,
	.vidioc_enum_input    = cal_enum_input,
	.vidioc_g_input       = cal_g_input,
	.vidioc_s_input       = cal_s_input,
	.vidioc_enum_frameintervals = cal_enum_frameintervals,
	.vidioc_streamon      = vb2_ioctl_streamon,
	.vidioc_streamoff     = vb2_ioctl_streamoff,
	.vidioc_log_status    = v4l2_ctrl_log_status,
	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};

/* ------------------------------------------------------------------
 *	V4L2 Media Controller Centric IOCTLs
 * ------------------------------------------------------------------
 */

static int cal_mc_enum_fmt_vid_cap(struct file *file, void  *priv,
				   struct v4l2_fmtdesc *f)
{
	if (f->index >= cal_num_formats)
		return -EINVAL;

	f->pixelformat = cal_formats[f->index].fourcc;
	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

	return 0;
}

static void cal_mc_try_fmt(struct cal_ctx *ctx, struct v4l2_format *f,
			   const struct cal_format_info **info)
{
	struct v4l2_pix_format *format = &f->fmt.pix;
	const struct cal_format_info *fmtinfo;
	unsigned int bpp;

	/*
	 * Default to the first format if the requested pixel format code isn't
	 * supported.
	 */
	fmtinfo = cal_format_by_fourcc(f->fmt.pix.pixelformat);
	if (!fmtinfo)
		fmtinfo = &cal_formats[0];

	/*
	 * Clamp the size, update the pixel format. The field and colorspace are
	 * accepted as-is, except for V4L2_FIELD_ANY that is turned into
	 * V4L2_FIELD_NONE.
	 */
	bpp = ALIGN(fmtinfo->bpp, 8);

	format->width = clamp_t(unsigned int, format->width,
				CAL_MIN_WIDTH_BYTES * 8 / bpp,
				CAL_MAX_WIDTH_BYTES * 8 / bpp);
	format->height = clamp_t(unsigned int, format->height,
				 CAL_MIN_HEIGHT_LINES, CAL_MAX_HEIGHT_LINES);
	format->pixelformat = fmtinfo->fourcc;

	if (format->field == V4L2_FIELD_ANY)
		format->field = V4L2_FIELD_NONE;

	/*
	 * Calculate the number of bytes per line and the image size. The
	 * hardware stores the stride as a number of 16 bytes words, in a
	 * signed 15-bit value. Only 14 bits are thus usable.
	 */
	format->bytesperline = ALIGN(clamp(format->bytesperline,
					   format->width * bpp / 8,
					   ((1U << 14) - 1) * 16), 16);

	format->sizeimage = format->height * format->bytesperline;

	format->colorspace = ctx->v_fmt.fmt.pix.colorspace;

	if (info)
		*info = fmtinfo;

	ctx_dbg(3, ctx, "%s: %s %ux%u (bytesperline %u sizeimage %u)\n",
		__func__, fourcc_to_str(format->pixelformat),
		format->width, format->height,
		format->bytesperline, format->sizeimage);
}

static int cal_mc_try_fmt_vid_cap(struct file *file, void *priv,
				  struct v4l2_format *f)
{
	struct cal_ctx *ctx = video_drvdata(file);

	cal_mc_try_fmt(ctx, f, NULL);
	return 0;
}

static int cal_mc_s_fmt_vid_cap(struct file *file, void *priv,
				struct v4l2_format *f)
{
	struct cal_ctx *ctx = video_drvdata(file);
	const struct cal_format_info *fmtinfo;

	if (vb2_is_busy(&ctx->vb_vidq)) {
		ctx_dbg(3, ctx, "%s device busy\n", __func__);
		return -EBUSY;
	}

	cal_mc_try_fmt(ctx, f, &fmtinfo);

	ctx->v_fmt = *f;
	ctx->fmtinfo = fmtinfo;

	return 0;
}

static int cal_mc_enum_framesizes(struct file *file, void *fh,
				  struct v4l2_frmsizeenum *fsize)
{
	struct cal_ctx *ctx = video_drvdata(file);
	const struct cal_format_info *fmtinfo;
	unsigned int bpp;

	if (fsize->index > 0)
		return -EINVAL;

	fmtinfo = cal_format_by_fourcc(fsize->pixel_format);
	if (!fmtinfo) {
		ctx_dbg(3, ctx, "Invalid pixel format 0x%08x\n",
			fsize->pixel_format);
		return -EINVAL;
	}

	bpp = ALIGN(fmtinfo->bpp, 8);

	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
	fsize->stepwise.min_width = CAL_MIN_WIDTH_BYTES * 8 / bpp;
	fsize->stepwise.max_width = CAL_MAX_WIDTH_BYTES * 8 / bpp;
	fsize->stepwise.step_width = 64 / bpp;
	fsize->stepwise.min_height = CAL_MIN_HEIGHT_LINES;
	fsize->stepwise.max_height = CAL_MAX_HEIGHT_LINES;
	fsize->stepwise.step_height = 1;

	return 0;
}

static const struct v4l2_ioctl_ops cal_ioctl_mc_ops = {
	.vidioc_querycap      = cal_querycap,
	.vidioc_enum_fmt_vid_cap  = cal_mc_enum_fmt_vid_cap,
	.vidioc_g_fmt_vid_cap     = cal_g_fmt_vid_cap,
	.vidioc_try_fmt_vid_cap   = cal_mc_try_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap     = cal_mc_s_fmt_vid_cap,
	.vidioc_enum_framesizes   = cal_mc_enum_framesizes,
	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
	.vidioc_create_bufs   = vb2_ioctl_create_bufs,
	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
	.vidioc_querybuf      = vb2_ioctl_querybuf,
	.vidioc_qbuf          = vb2_ioctl_qbuf,
	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
	.vidioc_expbuf        = vb2_ioctl_expbuf,
	.vidioc_streamon      = vb2_ioctl_streamon,
	.vidioc_streamoff     = vb2_ioctl_streamoff,
	.vidioc_log_status    = v4l2_ctrl_log_status,
};

/* ------------------------------------------------------------------
 *	videobuf2 Common Operations
 * ------------------------------------------------------------------
 */

static int cal_queue_setup(struct vb2_queue *vq,
			   unsigned int *nbuffers, unsigned int *nplanes,
			   unsigned int sizes[], struct device *alloc_devs[])
{
	struct cal_ctx *ctx = vb2_get_drv_priv(vq);
	unsigned int size = ctx->v_fmt.fmt.pix.sizeimage;

	if (vq->num_buffers + *nbuffers < 3)
		*nbuffers = 3 - vq->num_buffers;

	if (*nplanes) {
		if (sizes[0] < size)
			return -EINVAL;
		size = sizes[0];
	}

	*nplanes = 1;
	sizes[0] = size;

	ctx_dbg(3, ctx, "nbuffers=%d, size=%d\n", *nbuffers, sizes[0]);

	return 0;
}

static int cal_buffer_prepare(struct vb2_buffer *vb)
{
	struct cal_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
	struct cal_buffer *buf = container_of(vb, struct cal_buffer,
					      vb.vb2_buf);
	unsigned long size;

	size = ctx->v_fmt.fmt.pix.sizeimage;
	if (vb2_plane_size(vb, 0) < size) {
		ctx_err(ctx,
			"data will not fit into plane (%lu < %lu)\n",
			vb2_plane_size(vb, 0), size);
		return -EINVAL;
	}

	vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
	return 0;
}

static void cal_buffer_queue(struct vb2_buffer *vb)
{
	struct cal_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
	struct cal_buffer *buf = container_of(vb, struct cal_buffer,
					      vb.vb2_buf);
	unsigned long flags;

	/* recheck locking */
	spin_lock_irqsave(&ctx->dma.lock, flags);
	list_add_tail(&buf->list, &ctx->dma.queue);
	spin_unlock_irqrestore(&ctx->dma.lock, flags);
}

static void cal_release_buffers(struct cal_ctx *ctx,
				enum vb2_buffer_state state)
{
	struct cal_buffer *buf, *tmp;

	/* Release all queued buffers. */
	spin_lock_irq(&ctx->dma.lock);

	list_for_each_entry_safe(buf, tmp, &ctx->dma.queue, list) {
		list_del(&buf->list);
		vb2_buffer_done(&buf->vb.vb2_buf, state);
	}

	if (ctx->dma.pending) {
		vb2_buffer_done(&ctx->dma.pending->vb.vb2_buf, state);
		ctx->dma.pending = NULL;
	}

	if (ctx->dma.active) {
		vb2_buffer_done(&ctx->dma.active->vb.vb2_buf, state);
		ctx->dma.active = NULL;
	}

	spin_unlock_irq(&ctx->dma.lock);
}

/* ------------------------------------------------------------------
 *	videobuf2 Operations
 * ------------------------------------------------------------------
 */

static int cal_video_check_format(struct cal_ctx *ctx)
{
	const struct v4l2_mbus_framefmt *format;

	format = &ctx->phy->formats[CAL_CAMERARX_PAD_SOURCE];

	if (ctx->fmtinfo->code != format->code ||
	    ctx->v_fmt.fmt.pix.height != format->height ||
	    ctx->v_fmt.fmt.pix.width != format->width ||
	    ctx->v_fmt.fmt.pix.field != format->field)
		return -EPIPE;

	return 0;
}

static int cal_start_streaming(struct vb2_queue *vq, unsigned int count)
{
	struct cal_ctx *ctx = vb2_get_drv_priv(vq);
	struct cal_buffer *buf;
	dma_addr_t addr;
	int ret;

	ret = media_pipeline_start(&ctx->vdev.entity, &ctx->phy->pipe);
	if (ret < 0) {
		ctx_err(ctx, "Failed to start media pipeline: %d\n", ret);
		goto error_release_buffers;
	}

	/*
	 * Verify that the currently configured format matches the output of
	 * the connected CAMERARX.
	 */
	ret = cal_video_check_format(ctx);
	if (ret < 0) {
		ctx_dbg(3, ctx,
			"Format mismatch between CAMERARX and video node\n");
		goto error_pipeline;
	}

	spin_lock_irq(&ctx->dma.lock);
	buf = list_first_entry(&ctx->dma.queue, struct cal_buffer, list);
	ctx->dma.pending = buf;
	list_del(&buf->list);
	spin_unlock_irq(&ctx->dma.lock);

	addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);

	ret = pm_runtime_resume_and_get(ctx->cal->dev);
	if (ret < 0)
		goto error_pipeline;

	cal_ctx_set_dma_addr(ctx, addr);
	cal_ctx_start(ctx);

	ret = v4l2_subdev_call(&ctx->phy->subdev, video, s_stream, 1);
	if (ret)
		goto error_stop;

	if (cal_debug >= 4)
		cal_quickdump_regs(ctx->cal);

	return 0;

error_stop:
	cal_ctx_stop(ctx);
	pm_runtime_put_sync(ctx->cal->dev);

error_pipeline:
	media_pipeline_stop(&ctx->vdev.entity);
error_release_buffers:
	cal_release_buffers(ctx, VB2_BUF_STATE_QUEUED);

	return ret;
}

static void cal_stop_streaming(struct vb2_queue *vq)
{
	struct cal_ctx *ctx = vb2_get_drv_priv(vq);

	cal_ctx_stop(ctx);

	v4l2_subdev_call(&ctx->phy->subdev, video, s_stream, 0);

	pm_runtime_put_sync(ctx->cal->dev);

	cal_release_buffers(ctx, VB2_BUF_STATE_ERROR);

	media_pipeline_stop(&ctx->vdev.entity);
}

static const struct vb2_ops cal_video_qops = {
	.queue_setup		= cal_queue_setup,
	.buf_prepare		= cal_buffer_prepare,
	.buf_queue		= cal_buffer_queue,
	.start_streaming	= cal_start_streaming,
	.stop_streaming		= cal_stop_streaming,
	.wait_prepare		= vb2_ops_wait_prepare,
	.wait_finish		= vb2_ops_wait_finish,
};

/* ------------------------------------------------------------------
 *	V4L2 Initialization and Registration
 * ------------------------------------------------------------------
 */

static const struct v4l2_file_operations cal_fops = {
	.owner		= THIS_MODULE,
	.open           = v4l2_fh_open,
	.release        = vb2_fop_release,
	.poll		= vb2_fop_poll,
	.unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
	.mmap           = vb2_fop_mmap,
};

static int cal_ctx_v4l2_init_formats(struct cal_ctx *ctx)
{
	struct v4l2_subdev_mbus_code_enum mbus_code;
	struct v4l2_mbus_framefmt mbus_fmt;
	const struct cal_format_info *fmtinfo;
	unsigned int i, j, k;
	int ret = 0;

	/* Enumerate sub device formats and enable all matching local formats */
	ctx->active_fmt = devm_kcalloc(ctx->cal->dev, cal_num_formats,
				       sizeof(*ctx->active_fmt), GFP_KERNEL);
	ctx->num_active_fmt = 0;

	for (j = 0, i = 0; ; ++j) {

		memset(&mbus_code, 0, sizeof(mbus_code));
		mbus_code.index = j;
		mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
		ret = v4l2_subdev_call(ctx->phy->sensor, pad, enum_mbus_code,
				       NULL, &mbus_code);
		if (ret == -EINVAL)
			break;

		if (ret) {
			ctx_err(ctx, "Error enumerating mbus codes in subdev %s: %d\n",
				ctx->phy->sensor->name, ret);
			return ret;
		}

		ctx_dbg(2, ctx,
			"subdev %s: code: %04x idx: %u\n",
			ctx->phy->sensor->name, mbus_code.code, j);

		for (k = 0; k < cal_num_formats; k++) {
			fmtinfo = &cal_formats[k];

			if (mbus_code.code == fmtinfo->code) {
				ctx->active_fmt[i] = fmtinfo;
				ctx_dbg(2, ctx,
					"matched fourcc: %s: code: %04x idx: %u\n",
					fourcc_to_str(fmtinfo->fourcc),
					fmtinfo->code, i);
				ctx->num_active_fmt = ++i;
			}
		}
	}

	if (i == 0) {
		ctx_err(ctx, "No suitable format reported by subdev %s\n",
			ctx->phy->sensor->name);
		return -EINVAL;
	}

	ret = __subdev_get_format(ctx, &mbus_fmt);
	if (ret)
		return ret;

	fmtinfo = find_format_by_code(ctx, mbus_fmt.code);
	if (!fmtinfo) {
		ctx_dbg(3, ctx, "mbus code format (0x%08x) not found.\n",
			mbus_fmt.code);
		return -EINVAL;
	}

	/* Save current format */
	v4l2_fill_pix_format(&ctx->v_fmt.fmt.pix, &mbus_fmt);
	ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	ctx->v_fmt.fmt.pix.pixelformat = fmtinfo->fourcc;
	cal_calc_format_size(ctx, fmtinfo, &ctx->v_fmt);
	ctx->fmtinfo = fmtinfo;

	return 0;
}

int cal_ctx_v4l2_register(struct cal_ctx *ctx)
{
	struct video_device *vfd = &ctx->vdev;
	int ret;

	ret = cal_ctx_v4l2_init_formats(ctx);
	if (ret)
		return ret;

	if (!cal_mc_api) {
		struct v4l2_ctrl_handler *hdl = &ctx->ctrl_handler;

		ret = v4l2_ctrl_add_handler(hdl, ctx->phy->sensor->ctrl_handler,
					    NULL, true);
		if (ret < 0) {
			ctx_err(ctx, "Failed to add sensor ctrl handler\n");
			return ret;
		}
	}

	ret = video_register_device(vfd, VFL_TYPE_VIDEO, cal_video_nr);
	if (ret < 0) {
		ctx_err(ctx, "Failed to register video device\n");
		return ret;
	}

	ret = media_create_pad_link(&ctx->phy->subdev.entity,
				    CAL_CAMERARX_PAD_SOURCE,
				    &vfd->entity, 0,
				    MEDIA_LNK_FL_IMMUTABLE |
				    MEDIA_LNK_FL_ENABLED);
	if (ret) {
		ctx_err(ctx, "Failed to create media link for context %u\n",
			ctx->index);
		video_unregister_device(vfd);
		return ret;
	}

	ctx_info(ctx, "V4L2 device registered as %s\n",
		 video_device_node_name(vfd));

	return 0;
}

void cal_ctx_v4l2_unregister(struct cal_ctx *ctx)
{
	ctx_dbg(1, ctx, "unregistering %s\n",
		video_device_node_name(&ctx->vdev));

	video_unregister_device(&ctx->vdev);
}

int cal_ctx_v4l2_init(struct cal_ctx *ctx)
{
	struct video_device *vfd = &ctx->vdev;
	struct vb2_queue *q = &ctx->vb_vidq;
	int ret;

	INIT_LIST_HEAD(&ctx->dma.queue);
	spin_lock_init(&ctx->dma.lock);
	mutex_init(&ctx->mutex);
	init_waitqueue_head(&ctx->dma.wait);

	/* Initialize the vb2 queue. */
	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	q->io_modes = VB2_MMAP | VB2_DMABUF;
	q->drv_priv = ctx;
	q->buf_struct_size = sizeof(struct cal_buffer);
	q->ops = &cal_video_qops;
	q->mem_ops = &vb2_dma_contig_memops;
	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
	q->lock = &ctx->mutex;
	q->min_buffers_needed = 3;
	q->dev = ctx->cal->dev;

	ret = vb2_queue_init(q);
	if (ret)
		return ret;

	/* Initialize the video device and media entity. */
	vfd->fops = &cal_fops;
	vfd->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING
			 | (cal_mc_api ? V4L2_CAP_IO_MC : 0);
	vfd->v4l2_dev = &ctx->cal->v4l2_dev;
	vfd->queue = q;
	snprintf(vfd->name, sizeof(vfd->name), "CAL output %u", ctx->index);
	vfd->release = video_device_release_empty;
	vfd->ioctl_ops = cal_mc_api ? &cal_ioctl_mc_ops : &cal_ioctl_video_ops;
	vfd->lock = &ctx->mutex;
	video_set_drvdata(vfd, ctx);

	ctx->pad.flags = MEDIA_PAD_FL_SINK;
	ret = media_entity_pads_init(&vfd->entity, 1, &ctx->pad);
	if (ret < 0)
		return ret;

	if (!cal_mc_api) {
		/* Initialize the control handler. */
		struct v4l2_ctrl_handler *hdl = &ctx->ctrl_handler;

		ret = v4l2_ctrl_handler_init(hdl, 11);
		if (ret < 0) {
			ctx_err(ctx, "Failed to init ctrl handler\n");
			goto error;
		}

		vfd->ctrl_handler = hdl;
	}

	return 0;

error:
	media_entity_cleanup(&vfd->entity);
	return ret;
}

void cal_ctx_v4l2_cleanup(struct cal_ctx *ctx)
{
	if (!cal_mc_api)
		v4l2_ctrl_handler_free(&ctx->ctrl_handler);

	media_entity_cleanup(&ctx->vdev.entity);
}