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