media: rcar-vin: Do not reset the crop and compose rectangles in s_fmt
[sfrench/cifs-2.6.git] / drivers / media / platform / rcar-vin / rcar-v4l2.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for Renesas R-Car VIN
4  *
5  * Copyright (C) 2016 Renesas Electronics Corp.
6  * Copyright (C) 2011-2013 Renesas Solutions Corp.
7  * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
8  * Copyright (C) 2008 Magnus Damm
9  *
10  * Based on the soc-camera rcar_vin driver
11  */
12
13 #include <linux/pm_runtime.h>
14
15 #include <media/v4l2-event.h>
16 #include <media/v4l2-ioctl.h>
17 #include <media/v4l2-mc.h>
18 #include <media/v4l2-rect.h>
19
20 #include "rcar-vin.h"
21
22 #define RVIN_DEFAULT_FORMAT     V4L2_PIX_FMT_YUYV
23 #define RVIN_DEFAULT_WIDTH      800
24 #define RVIN_DEFAULT_HEIGHT     600
25 #define RVIN_DEFAULT_FIELD      V4L2_FIELD_NONE
26 #define RVIN_DEFAULT_COLORSPACE V4L2_COLORSPACE_SRGB
27
28 /* -----------------------------------------------------------------------------
29  * Format Conversions
30  */
31
32 static const struct rvin_video_format rvin_formats[] = {
33         {
34                 .fourcc                 = V4L2_PIX_FMT_NV16,
35                 .bpp                    = 1,
36         },
37         {
38                 .fourcc                 = V4L2_PIX_FMT_YUYV,
39                 .bpp                    = 2,
40         },
41         {
42                 .fourcc                 = V4L2_PIX_FMT_UYVY,
43                 .bpp                    = 2,
44         },
45         {
46                 .fourcc                 = V4L2_PIX_FMT_RGB565,
47                 .bpp                    = 2,
48         },
49         {
50                 .fourcc                 = V4L2_PIX_FMT_XRGB555,
51                 .bpp                    = 2,
52         },
53         {
54                 .fourcc                 = V4L2_PIX_FMT_XBGR32,
55                 .bpp                    = 4,
56         },
57         {
58                 .fourcc                 = V4L2_PIX_FMT_ARGB555,
59                 .bpp                    = 2,
60         },
61         {
62                 .fourcc                 = V4L2_PIX_FMT_ABGR32,
63                 .bpp                    = 4,
64         },
65 };
66
67 const struct rvin_video_format *rvin_format_from_pixel(struct rvin_dev *vin,
68                                                        u32 pixelformat)
69 {
70         int i;
71
72         if (vin->info->model == RCAR_M1 && pixelformat == V4L2_PIX_FMT_XBGR32)
73                 return NULL;
74
75         for (i = 0; i < ARRAY_SIZE(rvin_formats); i++)
76                 if (rvin_formats[i].fourcc == pixelformat)
77                         return rvin_formats + i;
78
79         return NULL;
80 }
81
82 static u32 rvin_format_bytesperline(struct rvin_dev *vin,
83                                     struct v4l2_pix_format *pix)
84 {
85         const struct rvin_video_format *fmt;
86         u32 align;
87
88         fmt = rvin_format_from_pixel(vin, pix->pixelformat);
89
90         if (WARN_ON(!fmt))
91                 return -EINVAL;
92
93         align = pix->pixelformat == V4L2_PIX_FMT_NV16 ? 0x20 : 0x10;
94
95         return ALIGN(pix->width, align) * fmt->bpp;
96 }
97
98 static u32 rvin_format_sizeimage(struct v4l2_pix_format *pix)
99 {
100         if (pix->pixelformat == V4L2_PIX_FMT_NV16)
101                 return pix->bytesperline * pix->height * 2;
102
103         return pix->bytesperline * pix->height;
104 }
105
106 static void rvin_format_align(struct rvin_dev *vin, struct v4l2_pix_format *pix)
107 {
108         u32 walign;
109
110         if (!rvin_format_from_pixel(vin, pix->pixelformat))
111                 pix->pixelformat = RVIN_DEFAULT_FORMAT;
112
113         switch (pix->field) {
114         case V4L2_FIELD_TOP:
115         case V4L2_FIELD_BOTTOM:
116         case V4L2_FIELD_NONE:
117         case V4L2_FIELD_INTERLACED_TB:
118         case V4L2_FIELD_INTERLACED_BT:
119         case V4L2_FIELD_INTERLACED:
120                 break;
121         case V4L2_FIELD_ALTERNATE:
122                 /*
123                  * Driver does not (yet) support outputting ALTERNATE to a
124                  * userspace. It does support outputting INTERLACED so use
125                  * the VIN hardware to combine the two fields.
126                  */
127                 pix->field = V4L2_FIELD_INTERLACED;
128                 pix->height *= 2;
129                 break;
130         default:
131                 pix->field = RVIN_DEFAULT_FIELD;
132                 break;
133         }
134
135         /* HW limit width to a multiple of 32 (2^5) for NV16 else 2 (2^1) */
136         walign = vin->format.pixelformat == V4L2_PIX_FMT_NV16 ? 5 : 1;
137
138         /* Limit to VIN capabilities */
139         v4l_bound_align_image(&pix->width, 2, vin->info->max_width, walign,
140                               &pix->height, 4, vin->info->max_height, 2, 0);
141
142         pix->bytesperline = rvin_format_bytesperline(vin, pix);
143         pix->sizeimage = rvin_format_sizeimage(pix);
144
145         vin_dbg(vin, "Format %ux%u bpl: %u size: %u\n",
146                 pix->width, pix->height, pix->bytesperline, pix->sizeimage);
147 }
148
149 /* -----------------------------------------------------------------------------
150  * V4L2
151  */
152
153 static int rvin_reset_format(struct rvin_dev *vin)
154 {
155         struct v4l2_subdev_format fmt = {
156                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
157                 .pad = vin->parallel->source_pad,
158         };
159         int ret;
160
161         ret = v4l2_subdev_call(vin_to_source(vin), pad, get_fmt, NULL, &fmt);
162         if (ret)
163                 return ret;
164
165         v4l2_fill_pix_format(&vin->format, &fmt.format);
166
167         rvin_format_align(vin, &vin->format);
168
169         vin->src_rect.top = 0;
170         vin->src_rect.left = 0;
171         vin->src_rect.width = vin->format.width;
172         vin->src_rect.height = vin->format.height;
173
174         vin->crop = vin->src_rect;
175         vin->compose = vin->src_rect;
176
177         return 0;
178 }
179
180 static int rvin_try_format(struct rvin_dev *vin, u32 which,
181                            struct v4l2_pix_format *pix,
182                            struct v4l2_rect *crop, struct v4l2_rect *compose)
183 {
184         struct v4l2_subdev *sd = vin_to_source(vin);
185         struct v4l2_subdev_pad_config *pad_cfg;
186         struct v4l2_subdev_format format = {
187                 .which = which,
188                 .pad = vin->parallel->source_pad,
189         };
190         enum v4l2_field field;
191         u32 width, height;
192         int ret;
193
194         pad_cfg = v4l2_subdev_alloc_pad_config(sd);
195         if (pad_cfg == NULL)
196                 return -ENOMEM;
197
198         if (!rvin_format_from_pixel(vin, pix->pixelformat))
199                 pix->pixelformat = RVIN_DEFAULT_FORMAT;
200
201         v4l2_fill_mbus_format(&format.format, pix, vin->mbus_code);
202
203         /* Allow the video device to override field and to scale */
204         field = pix->field;
205         width = pix->width;
206         height = pix->height;
207
208         ret = v4l2_subdev_call(sd, pad, set_fmt, pad_cfg, &format);
209         if (ret < 0 && ret != -ENOIOCTLCMD)
210                 goto done;
211         ret = 0;
212
213         v4l2_fill_pix_format(pix, &format.format);
214
215         if (crop) {
216                 crop->top = 0;
217                 crop->left = 0;
218                 crop->width = pix->width;
219                 crop->height = pix->height;
220
221                 /*
222                  * If source is ALTERNATE the driver will use the VIN hardware
223                  * to INTERLACE it. The crop height then needs to be doubled.
224                  */
225                 if (pix->field == V4L2_FIELD_ALTERNATE)
226                         crop->height *= 2;
227         }
228
229         if (field != V4L2_FIELD_ANY)
230                 pix->field = field;
231
232         pix->width = width;
233         pix->height = height;
234
235         rvin_format_align(vin, pix);
236
237         if (compose) {
238                 compose->top = 0;
239                 compose->left = 0;
240                 compose->width = pix->width;
241                 compose->height = pix->height;
242         }
243 done:
244         v4l2_subdev_free_pad_config(pad_cfg);
245
246         return ret;
247 }
248
249 static int rvin_querycap(struct file *file, void *priv,
250                          struct v4l2_capability *cap)
251 {
252         struct rvin_dev *vin = video_drvdata(file);
253
254         strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
255         strscpy(cap->card, "R_Car_VIN", sizeof(cap->card));
256         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
257                  dev_name(vin->dev));
258         return 0;
259 }
260
261 static int rvin_try_fmt_vid_cap(struct file *file, void *priv,
262                                 struct v4l2_format *f)
263 {
264         struct rvin_dev *vin = video_drvdata(file);
265
266         return rvin_try_format(vin, V4L2_SUBDEV_FORMAT_TRY, &f->fmt.pix, NULL,
267                                NULL);
268 }
269
270 static int rvin_s_fmt_vid_cap(struct file *file, void *priv,
271                               struct v4l2_format *f)
272 {
273         struct rvin_dev *vin = video_drvdata(file);
274         struct v4l2_rect crop, compose;
275         int ret;
276
277         if (vb2_is_busy(&vin->queue))
278                 return -EBUSY;
279
280         ret = rvin_try_format(vin, V4L2_SUBDEV_FORMAT_ACTIVE, &f->fmt.pix,
281                               &crop, &compose);
282         if (ret)
283                 return ret;
284
285         vin->format = f->fmt.pix;
286         v4l2_rect_map_inside(&vin->crop, &crop);
287         v4l2_rect_map_inside(&vin->compose, &compose);
288         vin->src_rect = crop;
289
290         return 0;
291 }
292
293 static int rvin_g_fmt_vid_cap(struct file *file, void *priv,
294                               struct v4l2_format *f)
295 {
296         struct rvin_dev *vin = video_drvdata(file);
297
298         f->fmt.pix = vin->format;
299
300         return 0;
301 }
302
303 static int rvin_enum_fmt_vid_cap(struct file *file, void *priv,
304                                  struct v4l2_fmtdesc *f)
305 {
306         if (f->index >= ARRAY_SIZE(rvin_formats))
307                 return -EINVAL;
308
309         f->pixelformat = rvin_formats[f->index].fourcc;
310
311         return 0;
312 }
313
314 static int rvin_g_selection(struct file *file, void *fh,
315                             struct v4l2_selection *s)
316 {
317         struct rvin_dev *vin = video_drvdata(file);
318
319         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
320                 return -EINVAL;
321
322         switch (s->target) {
323         case V4L2_SEL_TGT_CROP_BOUNDS:
324         case V4L2_SEL_TGT_CROP_DEFAULT:
325                 s->r.left = s->r.top = 0;
326                 s->r.width = vin->src_rect.width;
327                 s->r.height = vin->src_rect.height;
328                 break;
329         case V4L2_SEL_TGT_CROP:
330                 s->r = vin->crop;
331                 break;
332         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
333         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
334                 s->r.left = s->r.top = 0;
335                 s->r.width = vin->format.width;
336                 s->r.height = vin->format.height;
337                 break;
338         case V4L2_SEL_TGT_COMPOSE:
339                 s->r = vin->compose;
340                 break;
341         default:
342                 return -EINVAL;
343         }
344
345         return 0;
346 }
347
348 static int rvin_s_selection(struct file *file, void *fh,
349                             struct v4l2_selection *s)
350 {
351         struct rvin_dev *vin = video_drvdata(file);
352         const struct rvin_video_format *fmt;
353         struct v4l2_rect r = s->r;
354         struct v4l2_rect max_rect;
355         struct v4l2_rect min_rect = {
356                 .width = 6,
357                 .height = 2,
358         };
359
360         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
361                 return -EINVAL;
362
363         v4l2_rect_set_min_size(&r, &min_rect);
364
365         switch (s->target) {
366         case V4L2_SEL_TGT_CROP:
367                 /* Can't crop outside of source input */
368                 max_rect.top = max_rect.left = 0;
369                 max_rect.width = vin->src_rect.width;
370                 max_rect.height = vin->src_rect.height;
371                 v4l2_rect_map_inside(&r, &max_rect);
372
373                 v4l_bound_align_image(&r.width, 6, vin->src_rect.width, 0,
374                                       &r.height, 2, vin->src_rect.height, 0, 0);
375
376                 r.top  = clamp_t(s32, r.top, 0,
377                                  vin->src_rect.height - r.height);
378                 r.left = clamp_t(s32, r.left, 0, vin->src_rect.width - r.width);
379
380                 vin->crop = s->r = r;
381
382                 vin_dbg(vin, "Cropped %dx%d@%d:%d of %dx%d\n",
383                         r.width, r.height, r.left, r.top,
384                         vin->src_rect.width, vin->src_rect.height);
385                 break;
386         case V4L2_SEL_TGT_COMPOSE:
387                 /* Make sure compose rect fits inside output format */
388                 max_rect.top = max_rect.left = 0;
389                 max_rect.width = vin->format.width;
390                 max_rect.height = vin->format.height;
391                 v4l2_rect_map_inside(&r, &max_rect);
392
393                 /*
394                  * Composing is done by adding a offset to the buffer address,
395                  * the HW wants this address to be aligned to HW_BUFFER_MASK.
396                  * Make sure the top and left values meets this requirement.
397                  */
398                 while ((r.top * vin->format.bytesperline) & HW_BUFFER_MASK)
399                         r.top--;
400
401                 fmt = rvin_format_from_pixel(vin, vin->format.pixelformat);
402                 while ((r.left * fmt->bpp) & HW_BUFFER_MASK)
403                         r.left--;
404
405                 vin->compose = s->r = r;
406
407                 vin_dbg(vin, "Compose %dx%d@%d:%d in %dx%d\n",
408                         r.width, r.height, r.left, r.top,
409                         vin->format.width, vin->format.height);
410                 break;
411         default:
412                 return -EINVAL;
413         }
414
415         /* HW supports modifying configuration while running */
416         rvin_crop_scale_comp(vin);
417
418         return 0;
419 }
420
421 static int rvin_g_pixelaspect(struct file *file, void *priv,
422                               int type, struct v4l2_fract *f)
423 {
424         struct rvin_dev *vin = video_drvdata(file);
425         struct v4l2_subdev *sd = vin_to_source(vin);
426
427         if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
428                 return -EINVAL;
429
430         return v4l2_subdev_call(sd, video, g_pixelaspect, f);
431 }
432
433 static int rvin_enum_input(struct file *file, void *priv,
434                            struct v4l2_input *i)
435 {
436         struct rvin_dev *vin = video_drvdata(file);
437         struct v4l2_subdev *sd = vin_to_source(vin);
438         int ret;
439
440         if (i->index != 0)
441                 return -EINVAL;
442
443         ret = v4l2_subdev_call(sd, video, g_input_status, &i->status);
444         if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
445                 return ret;
446
447         i->type = V4L2_INPUT_TYPE_CAMERA;
448
449         if (v4l2_subdev_has_op(sd, pad, dv_timings_cap)) {
450                 i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
451                 i->std = 0;
452         } else {
453                 i->capabilities = V4L2_IN_CAP_STD;
454                 i->std = vin->vdev.tvnorms;
455         }
456
457         strscpy(i->name, "Camera", sizeof(i->name));
458
459         return 0;
460 }
461
462 static int rvin_g_input(struct file *file, void *priv, unsigned int *i)
463 {
464         *i = 0;
465         return 0;
466 }
467
468 static int rvin_s_input(struct file *file, void *priv, unsigned int i)
469 {
470         if (i > 0)
471                 return -EINVAL;
472         return 0;
473 }
474
475 static int rvin_querystd(struct file *file, void *priv, v4l2_std_id *a)
476 {
477         struct rvin_dev *vin = video_drvdata(file);
478         struct v4l2_subdev *sd = vin_to_source(vin);
479
480         return v4l2_subdev_call(sd, video, querystd, a);
481 }
482
483 static int rvin_s_std(struct file *file, void *priv, v4l2_std_id a)
484 {
485         struct rvin_dev *vin = video_drvdata(file);
486         int ret;
487
488         ret = v4l2_subdev_call(vin_to_source(vin), video, s_std, a);
489         if (ret < 0)
490                 return ret;
491
492         vin->std = a;
493
494         /* Changing the standard will change the width/height */
495         return rvin_reset_format(vin);
496 }
497
498 static int rvin_g_std(struct file *file, void *priv, v4l2_std_id *a)
499 {
500         struct rvin_dev *vin = video_drvdata(file);
501
502         if (v4l2_subdev_has_op(vin_to_source(vin), pad, dv_timings_cap))
503                 return -ENOIOCTLCMD;
504
505         *a = vin->std;
506
507         return 0;
508 }
509
510 static int rvin_subscribe_event(struct v4l2_fh *fh,
511                                 const struct v4l2_event_subscription *sub)
512 {
513         switch (sub->type) {
514         case V4L2_EVENT_SOURCE_CHANGE:
515                 return v4l2_event_subscribe(fh, sub, 4, NULL);
516         }
517         return v4l2_ctrl_subscribe_event(fh, sub);
518 }
519
520 static int rvin_enum_dv_timings(struct file *file, void *priv_fh,
521                                 struct v4l2_enum_dv_timings *timings)
522 {
523         struct rvin_dev *vin = video_drvdata(file);
524         struct v4l2_subdev *sd = vin_to_source(vin);
525         int ret;
526
527         if (timings->pad)
528                 return -EINVAL;
529
530         timings->pad = vin->parallel->sink_pad;
531
532         ret = v4l2_subdev_call(sd, pad, enum_dv_timings, timings);
533
534         timings->pad = 0;
535
536         return ret;
537 }
538
539 static int rvin_s_dv_timings(struct file *file, void *priv_fh,
540                              struct v4l2_dv_timings *timings)
541 {
542         struct rvin_dev *vin = video_drvdata(file);
543         struct v4l2_subdev *sd = vin_to_source(vin);
544         int ret;
545
546         ret = v4l2_subdev_call(sd, video, s_dv_timings, timings);
547         if (ret)
548                 return ret;
549
550         /* Changing the timings will change the width/height */
551         return rvin_reset_format(vin);
552 }
553
554 static int rvin_g_dv_timings(struct file *file, void *priv_fh,
555                              struct v4l2_dv_timings *timings)
556 {
557         struct rvin_dev *vin = video_drvdata(file);
558         struct v4l2_subdev *sd = vin_to_source(vin);
559
560         return v4l2_subdev_call(sd, video, g_dv_timings, timings);
561 }
562
563 static int rvin_query_dv_timings(struct file *file, void *priv_fh,
564                                  struct v4l2_dv_timings *timings)
565 {
566         struct rvin_dev *vin = video_drvdata(file);
567         struct v4l2_subdev *sd = vin_to_source(vin);
568
569         return v4l2_subdev_call(sd, video, query_dv_timings, timings);
570 }
571
572 static int rvin_dv_timings_cap(struct file *file, void *priv_fh,
573                                struct v4l2_dv_timings_cap *cap)
574 {
575         struct rvin_dev *vin = video_drvdata(file);
576         struct v4l2_subdev *sd = vin_to_source(vin);
577         int ret;
578
579         if (cap->pad)
580                 return -EINVAL;
581
582         cap->pad = vin->parallel->sink_pad;
583
584         ret = v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
585
586         cap->pad = 0;
587
588         return ret;
589 }
590
591 static int rvin_g_edid(struct file *file, void *fh, struct v4l2_edid *edid)
592 {
593         struct rvin_dev *vin = video_drvdata(file);
594         struct v4l2_subdev *sd = vin_to_source(vin);
595         int ret;
596
597         if (edid->pad)
598                 return -EINVAL;
599
600         edid->pad = vin->parallel->sink_pad;
601
602         ret = v4l2_subdev_call(sd, pad, get_edid, edid);
603
604         edid->pad = 0;
605
606         return ret;
607 }
608
609 static int rvin_s_edid(struct file *file, void *fh, struct v4l2_edid *edid)
610 {
611         struct rvin_dev *vin = video_drvdata(file);
612         struct v4l2_subdev *sd = vin_to_source(vin);
613         int ret;
614
615         if (edid->pad)
616                 return -EINVAL;
617
618         edid->pad = vin->parallel->sink_pad;
619
620         ret = v4l2_subdev_call(sd, pad, set_edid, edid);
621
622         edid->pad = 0;
623
624         return ret;
625 }
626
627 static const struct v4l2_ioctl_ops rvin_ioctl_ops = {
628         .vidioc_querycap                = rvin_querycap,
629         .vidioc_try_fmt_vid_cap         = rvin_try_fmt_vid_cap,
630         .vidioc_g_fmt_vid_cap           = rvin_g_fmt_vid_cap,
631         .vidioc_s_fmt_vid_cap           = rvin_s_fmt_vid_cap,
632         .vidioc_enum_fmt_vid_cap        = rvin_enum_fmt_vid_cap,
633
634         .vidioc_g_selection             = rvin_g_selection,
635         .vidioc_s_selection             = rvin_s_selection,
636
637         .vidioc_g_pixelaspect           = rvin_g_pixelaspect,
638
639         .vidioc_enum_input              = rvin_enum_input,
640         .vidioc_g_input                 = rvin_g_input,
641         .vidioc_s_input                 = rvin_s_input,
642
643         .vidioc_dv_timings_cap          = rvin_dv_timings_cap,
644         .vidioc_enum_dv_timings         = rvin_enum_dv_timings,
645         .vidioc_g_dv_timings            = rvin_g_dv_timings,
646         .vidioc_s_dv_timings            = rvin_s_dv_timings,
647         .vidioc_query_dv_timings        = rvin_query_dv_timings,
648
649         .vidioc_g_edid                  = rvin_g_edid,
650         .vidioc_s_edid                  = rvin_s_edid,
651
652         .vidioc_querystd                = rvin_querystd,
653         .vidioc_g_std                   = rvin_g_std,
654         .vidioc_s_std                   = rvin_s_std,
655
656         .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
657         .vidioc_create_bufs             = vb2_ioctl_create_bufs,
658         .vidioc_querybuf                = vb2_ioctl_querybuf,
659         .vidioc_qbuf                    = vb2_ioctl_qbuf,
660         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
661         .vidioc_expbuf                  = vb2_ioctl_expbuf,
662         .vidioc_prepare_buf             = vb2_ioctl_prepare_buf,
663         .vidioc_streamon                = vb2_ioctl_streamon,
664         .vidioc_streamoff               = vb2_ioctl_streamoff,
665
666         .vidioc_log_status              = v4l2_ctrl_log_status,
667         .vidioc_subscribe_event         = rvin_subscribe_event,
668         .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
669 };
670
671 /* -----------------------------------------------------------------------------
672  * V4L2 Media Controller
673  */
674
675 static void rvin_mc_try_format(struct rvin_dev *vin,
676                                struct v4l2_pix_format *pix)
677 {
678         /*
679          * The V4L2 specification clearly documents the colorspace fields
680          * as being set by drivers for capture devices. Using the values
681          * supplied by userspace thus wouldn't comply with the API. Until
682          * the API is updated force fixed values.
683          */
684         pix->colorspace = RVIN_DEFAULT_COLORSPACE;
685         pix->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix->colorspace);
686         pix->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix->colorspace);
687         pix->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, pix->colorspace,
688                                                           pix->ycbcr_enc);
689
690         rvin_format_align(vin, pix);
691 }
692
693 static int rvin_mc_try_fmt_vid_cap(struct file *file, void *priv,
694                                    struct v4l2_format *f)
695 {
696         struct rvin_dev *vin = video_drvdata(file);
697
698         rvin_mc_try_format(vin, &f->fmt.pix);
699
700         return 0;
701 }
702
703 static int rvin_mc_s_fmt_vid_cap(struct file *file, void *priv,
704                                  struct v4l2_format *f)
705 {
706         struct rvin_dev *vin = video_drvdata(file);
707
708         if (vb2_is_busy(&vin->queue))
709                 return -EBUSY;
710
711         rvin_mc_try_format(vin, &f->fmt.pix);
712
713         vin->format = f->fmt.pix;
714
715         vin->crop.top = 0;
716         vin->crop.left = 0;
717         vin->crop.width = vin->format.width;
718         vin->crop.height = vin->format.height;
719         vin->compose = vin->crop;
720
721         return 0;
722 }
723
724 static int rvin_mc_enum_input(struct file *file, void *priv,
725                               struct v4l2_input *i)
726 {
727         if (i->index != 0)
728                 return -EINVAL;
729
730         i->type = V4L2_INPUT_TYPE_CAMERA;
731         strscpy(i->name, "Camera", sizeof(i->name));
732
733         return 0;
734 }
735
736 static const struct v4l2_ioctl_ops rvin_mc_ioctl_ops = {
737         .vidioc_querycap                = rvin_querycap,
738         .vidioc_try_fmt_vid_cap         = rvin_mc_try_fmt_vid_cap,
739         .vidioc_g_fmt_vid_cap           = rvin_g_fmt_vid_cap,
740         .vidioc_s_fmt_vid_cap           = rvin_mc_s_fmt_vid_cap,
741         .vidioc_enum_fmt_vid_cap        = rvin_enum_fmt_vid_cap,
742
743         .vidioc_enum_input              = rvin_mc_enum_input,
744         .vidioc_g_input                 = rvin_g_input,
745         .vidioc_s_input                 = rvin_s_input,
746
747         .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
748         .vidioc_create_bufs             = vb2_ioctl_create_bufs,
749         .vidioc_querybuf                = vb2_ioctl_querybuf,
750         .vidioc_qbuf                    = vb2_ioctl_qbuf,
751         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
752         .vidioc_expbuf                  = vb2_ioctl_expbuf,
753         .vidioc_prepare_buf             = vb2_ioctl_prepare_buf,
754         .vidioc_streamon                = vb2_ioctl_streamon,
755         .vidioc_streamoff               = vb2_ioctl_streamoff,
756
757         .vidioc_log_status              = v4l2_ctrl_log_status,
758         .vidioc_subscribe_event         = rvin_subscribe_event,
759         .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
760 };
761
762 /* -----------------------------------------------------------------------------
763  * File Operations
764  */
765
766 static int rvin_power_parallel(struct rvin_dev *vin, bool on)
767 {
768         struct v4l2_subdev *sd = vin_to_source(vin);
769         int power = on ? 1 : 0;
770         int ret;
771
772         ret = v4l2_subdev_call(sd, core, s_power, power);
773         if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
774                 return ret;
775
776         return 0;
777 }
778
779 static int rvin_open(struct file *file)
780 {
781         struct rvin_dev *vin = video_drvdata(file);
782         int ret;
783
784         ret = pm_runtime_get_sync(vin->dev);
785         if (ret < 0)
786                 return ret;
787
788         ret = mutex_lock_interruptible(&vin->lock);
789         if (ret)
790                 goto err_pm;
791
792         file->private_data = vin;
793
794         ret = v4l2_fh_open(file);
795         if (ret)
796                 goto err_unlock;
797
798         if (vin->info->use_mc)
799                 ret = v4l2_pipeline_pm_use(&vin->vdev.entity, 1);
800         else if (v4l2_fh_is_singular_file(file))
801                 ret = rvin_power_parallel(vin, true);
802
803         if (ret < 0)
804                 goto err_open;
805
806         ret = v4l2_ctrl_handler_setup(&vin->ctrl_handler);
807         if (ret)
808                 goto err_power;
809
810         mutex_unlock(&vin->lock);
811
812         return 0;
813 err_power:
814         if (vin->info->use_mc)
815                 v4l2_pipeline_pm_use(&vin->vdev.entity, 0);
816         else if (v4l2_fh_is_singular_file(file))
817                 rvin_power_parallel(vin, false);
818 err_open:
819         v4l2_fh_release(file);
820 err_unlock:
821         mutex_unlock(&vin->lock);
822 err_pm:
823         pm_runtime_put(vin->dev);
824
825         return ret;
826 }
827
828 static int rvin_release(struct file *file)
829 {
830         struct rvin_dev *vin = video_drvdata(file);
831         bool fh_singular;
832         int ret;
833
834         mutex_lock(&vin->lock);
835
836         /* Save the singular status before we call the clean-up helper */
837         fh_singular = v4l2_fh_is_singular_file(file);
838
839         /* the release helper will cleanup any on-going streaming */
840         ret = _vb2_fop_release(file, NULL);
841
842         if (vin->info->use_mc) {
843                 v4l2_pipeline_pm_use(&vin->vdev.entity, 0);
844         } else {
845                 if (fh_singular)
846                         rvin_power_parallel(vin, false);
847         }
848
849         mutex_unlock(&vin->lock);
850
851         pm_runtime_put(vin->dev);
852
853         return ret;
854 }
855
856 static const struct v4l2_file_operations rvin_fops = {
857         .owner          = THIS_MODULE,
858         .unlocked_ioctl = video_ioctl2,
859         .open           = rvin_open,
860         .release        = rvin_release,
861         .poll           = vb2_fop_poll,
862         .mmap           = vb2_fop_mmap,
863         .read           = vb2_fop_read,
864 };
865
866 void rvin_v4l2_unregister(struct rvin_dev *vin)
867 {
868         if (!video_is_registered(&vin->vdev))
869                 return;
870
871         v4l2_info(&vin->v4l2_dev, "Removing %s\n",
872                   video_device_node_name(&vin->vdev));
873
874         /* Checks internally if vdev have been init or not */
875         video_unregister_device(&vin->vdev);
876 }
877
878 static void rvin_notify(struct v4l2_subdev *sd,
879                         unsigned int notification, void *arg)
880 {
881         struct rvin_dev *vin =
882                 container_of(sd->v4l2_dev, struct rvin_dev, v4l2_dev);
883
884         switch (notification) {
885         case V4L2_DEVICE_NOTIFY_EVENT:
886                 v4l2_event_queue(&vin->vdev, arg);
887                 break;
888         default:
889                 break;
890         }
891 }
892
893 int rvin_v4l2_register(struct rvin_dev *vin)
894 {
895         struct video_device *vdev = &vin->vdev;
896         int ret;
897
898         vin->v4l2_dev.notify = rvin_notify;
899
900         /* video node */
901         vdev->v4l2_dev = &vin->v4l2_dev;
902         vdev->queue = &vin->queue;
903         snprintf(vdev->name, sizeof(vdev->name), "VIN%u output", vin->id);
904         vdev->release = video_device_release_empty;
905         vdev->lock = &vin->lock;
906         vdev->fops = &rvin_fops;
907         vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
908                 V4L2_CAP_READWRITE;
909
910         /* Set a default format */
911         vin->format.pixelformat = RVIN_DEFAULT_FORMAT;
912         vin->format.width = RVIN_DEFAULT_WIDTH;
913         vin->format.height = RVIN_DEFAULT_HEIGHT;
914         vin->format.field = RVIN_DEFAULT_FIELD;
915         vin->format.colorspace = RVIN_DEFAULT_COLORSPACE;
916
917         if (vin->info->use_mc) {
918                 vdev->ioctl_ops = &rvin_mc_ioctl_ops;
919         } else {
920                 vdev->ioctl_ops = &rvin_ioctl_ops;
921                 rvin_reset_format(vin);
922         }
923
924         rvin_format_align(vin, &vin->format);
925
926         ret = video_register_device(&vin->vdev, VFL_TYPE_GRABBER, -1);
927         if (ret) {
928                 vin_err(vin, "Failed to register video device\n");
929                 return ret;
930         }
931
932         video_set_drvdata(&vin->vdev, vin);
933
934         v4l2_info(&vin->v4l2_dev, "Device registered as %s\n",
935                   video_device_node_name(&vin->vdev));
936
937         return ret;
938 }