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