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