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