2 * Samsung TV Mixer driver
4 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
6 * Tomasz Stanislawski, <t.stanislaws@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation. either version 2 of the License,
11 * or (at your option) any later version
14 #define pr_fmt(fmt) "s5p-tv (mixer): " fmt
18 #include <media/v4l2-ioctl.h>
19 #include <linux/videodev2.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/timer.h>
24 #include <media/videobuf2-dma-contig.h>
26 static int find_reg_callback(struct device *dev, void *p)
28 struct v4l2_subdev **sd = p;
30 *sd = dev_get_drvdata(dev);
31 /* non-zero value stops iteration */
35 static struct v4l2_subdev *find_and_register_subdev(
36 struct mxr_device *mdev, char *module_name)
38 struct device_driver *drv;
39 struct v4l2_subdev *sd = NULL;
42 /* TODO: add waiting until probe is finished */
43 drv = driver_find(module_name, &platform_bus_type);
45 mxr_warn(mdev, "module %s is missing\n", module_name);
48 /* driver refcnt is increased, it is safe to iterate over devices */
49 ret = driver_for_each_device(drv, NULL, &sd, find_reg_callback);
50 /* ret == 0 means that find_reg_callback was never executed */
52 mxr_warn(mdev, "module %s provides no subdev!\n", module_name);
55 /* v4l2_device_register_subdev detects if sd is NULL */
56 ret = v4l2_device_register_subdev(&mdev->v4l2_dev, sd);
58 mxr_warn(mdev, "failed to register subdev %s\n", sd->name);
66 int mxr_acquire_video(struct mxr_device *mdev,
67 struct mxr_output_conf *output_conf, int output_count)
69 struct device *dev = mdev->dev;
70 struct v4l2_device *v4l2_dev = &mdev->v4l2_dev;
73 struct v4l2_subdev *sd;
75 strlcpy(v4l2_dev->name, dev_name(mdev->dev), sizeof(v4l2_dev->name));
76 /* prepare context for V4L2 device */
77 ret = v4l2_device_register(dev, v4l2_dev);
79 mxr_err(mdev, "could not register v4l2 device.\n");
83 vb2_dma_contig_set_max_seg_size(mdev->dev, DMA_BIT_MASK(32));
84 mdev->alloc_ctx = vb2_dma_contig_init_ctx(mdev->dev);
85 if (IS_ERR(mdev->alloc_ctx)) {
86 mxr_err(mdev, "could not acquire vb2 allocator\n");
87 ret = PTR_ERR(mdev->alloc_ctx);
91 /* registering outputs */
93 for (i = 0; i < output_count; ++i) {
94 struct mxr_output_conf *conf = &output_conf[i];
95 struct mxr_output *out;
97 sd = find_and_register_subdev(mdev, conf->module_name);
98 /* trying to register next output */
101 out = kzalloc(sizeof(*out), GFP_KERNEL);
103 mxr_err(mdev, "no memory for '%s'\n",
106 /* registered subdevs are removed in fail_v4l2_dev */
109 strlcpy(out->name, conf->output_name, sizeof(out->name));
111 out->cookie = conf->cookie;
112 mdev->output[mdev->output_cnt++] = out;
113 mxr_info(mdev, "added output '%s' from module '%s'\n",
114 conf->output_name, conf->module_name);
115 /* checking if maximal number of outputs is reached */
116 if (mdev->output_cnt >= MXR_MAX_OUTPUTS)
120 if (mdev->output_cnt == 0) {
121 mxr_err(mdev, "failed to register any output\n");
123 /* skipping fail_output because there is nothing to free */
124 goto fail_vb2_allocator;
130 /* kfree is NULL-safe */
131 for (i = 0; i < mdev->output_cnt; ++i)
132 kfree(mdev->output[i]);
133 memset(mdev->output, 0, sizeof(mdev->output));
136 /* freeing allocator context */
137 vb2_dma_contig_cleanup_ctx(mdev->alloc_ctx);
140 /* NOTE: automatically unregister all subdevs */
141 v4l2_device_unregister(v4l2_dev);
147 void mxr_release_video(struct mxr_device *mdev)
151 /* kfree is NULL-safe */
152 for (i = 0; i < mdev->output_cnt; ++i)
153 kfree(mdev->output[i]);
155 vb2_dma_contig_cleanup_ctx(mdev->alloc_ctx);
156 vb2_dma_contig_clear_max_seg_size(mdev->dev);
157 v4l2_device_unregister(&mdev->v4l2_dev);
160 static int mxr_querycap(struct file *file, void *priv,
161 struct v4l2_capability *cap)
163 struct mxr_layer *layer = video_drvdata(file);
165 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
167 strlcpy(cap->driver, MXR_DRIVER_NAME, sizeof(cap->driver));
168 strlcpy(cap->card, layer->vfd.name, sizeof(cap->card));
169 sprintf(cap->bus_info, "%d", layer->idx);
170 cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
171 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
176 static void mxr_geometry_dump(struct mxr_device *mdev, struct mxr_geometry *geo)
178 mxr_dbg(mdev, "src.full_size = (%u, %u)\n",
179 geo->src.full_width, geo->src.full_height);
180 mxr_dbg(mdev, "src.size = (%u, %u)\n",
181 geo->src.width, geo->src.height);
182 mxr_dbg(mdev, "src.offset = (%u, %u)\n",
183 geo->src.x_offset, geo->src.y_offset);
184 mxr_dbg(mdev, "dst.full_size = (%u, %u)\n",
185 geo->dst.full_width, geo->dst.full_height);
186 mxr_dbg(mdev, "dst.size = (%u, %u)\n",
187 geo->dst.width, geo->dst.height);
188 mxr_dbg(mdev, "dst.offset = (%u, %u)\n",
189 geo->dst.x_offset, geo->dst.y_offset);
190 mxr_dbg(mdev, "ratio = (%u, %u)\n",
191 geo->x_ratio, geo->y_ratio);
194 static void mxr_layer_default_geo(struct mxr_layer *layer)
196 struct mxr_device *mdev = layer->mdev;
197 struct v4l2_mbus_framefmt mbus_fmt;
199 memset(&layer->geo, 0, sizeof(layer->geo));
201 mxr_get_mbus_fmt(mdev, &mbus_fmt);
203 layer->geo.dst.full_width = mbus_fmt.width;
204 layer->geo.dst.full_height = mbus_fmt.height;
205 layer->geo.dst.width = layer->geo.dst.full_width;
206 layer->geo.dst.height = layer->geo.dst.full_height;
207 layer->geo.dst.field = mbus_fmt.field;
209 layer->geo.src.full_width = mbus_fmt.width;
210 layer->geo.src.full_height = mbus_fmt.height;
211 layer->geo.src.width = layer->geo.src.full_width;
212 layer->geo.src.height = layer->geo.src.full_height;
214 mxr_geometry_dump(mdev, &layer->geo);
215 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SINK, 0);
216 mxr_geometry_dump(mdev, &layer->geo);
219 static void mxr_layer_update_output(struct mxr_layer *layer)
221 struct mxr_device *mdev = layer->mdev;
222 struct v4l2_mbus_framefmt mbus_fmt;
224 mxr_get_mbus_fmt(mdev, &mbus_fmt);
225 /* checking if update is needed */
226 if (layer->geo.dst.full_width == mbus_fmt.width &&
227 layer->geo.dst.full_height == mbus_fmt.width)
230 layer->geo.dst.full_width = mbus_fmt.width;
231 layer->geo.dst.full_height = mbus_fmt.height;
232 layer->geo.dst.field = mbus_fmt.field;
233 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SINK, 0);
235 mxr_geometry_dump(mdev, &layer->geo);
238 static const struct mxr_format *find_format_by_fourcc(
239 struct mxr_layer *layer, unsigned long fourcc);
240 static const struct mxr_format *find_format_by_index(
241 struct mxr_layer *layer, unsigned long index);
243 static int mxr_enum_fmt(struct file *file, void *priv,
244 struct v4l2_fmtdesc *f)
246 struct mxr_layer *layer = video_drvdata(file);
247 struct mxr_device *mdev = layer->mdev;
248 const struct mxr_format *fmt;
250 mxr_dbg(mdev, "%s\n", __func__);
251 fmt = find_format_by_index(layer, f->index);
255 strlcpy(f->description, fmt->name, sizeof(f->description));
256 f->pixelformat = fmt->fourcc;
261 static unsigned int divup(unsigned int divident, unsigned int divisor)
263 return (divident + divisor - 1) / divisor;
266 unsigned long mxr_get_plane_size(const struct mxr_block *blk,
267 unsigned int width, unsigned int height)
269 unsigned int bl_width = divup(width, blk->width);
270 unsigned int bl_height = divup(height, blk->height);
272 return bl_width * bl_height * blk->size;
275 static void mxr_mplane_fill(struct v4l2_plane_pix_format *planes,
276 const struct mxr_format *fmt, u32 width, u32 height)
280 /* checking if nothing to fill */
284 memset(planes, 0, sizeof(*planes) * fmt->num_subframes);
285 for (i = 0; i < fmt->num_planes; ++i) {
286 struct v4l2_plane_pix_format *plane = planes
287 + fmt->plane2subframe[i];
288 const struct mxr_block *blk = &fmt->plane[i];
289 u32 bl_width = divup(width, blk->width);
290 u32 bl_height = divup(height, blk->height);
291 u32 sizeimage = bl_width * bl_height * blk->size;
292 u32 bytesperline = bl_width * blk->size / blk->height;
294 plane->sizeimage += sizeimage;
295 plane->bytesperline = max(plane->bytesperline, bytesperline);
299 static int mxr_g_fmt(struct file *file, void *priv,
300 struct v4l2_format *f)
302 struct mxr_layer *layer = video_drvdata(file);
303 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
305 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
307 pix->width = layer->geo.src.full_width;
308 pix->height = layer->geo.src.full_height;
309 pix->field = V4L2_FIELD_NONE;
310 pix->pixelformat = layer->fmt->fourcc;
311 pix->colorspace = layer->fmt->colorspace;
312 mxr_mplane_fill(pix->plane_fmt, layer->fmt, pix->width, pix->height);
317 static int mxr_s_fmt(struct file *file, void *priv,
318 struct v4l2_format *f)
320 struct mxr_layer *layer = video_drvdata(file);
321 const struct mxr_format *fmt;
322 struct v4l2_pix_format_mplane *pix;
323 struct mxr_device *mdev = layer->mdev;
324 struct mxr_geometry *geo = &layer->geo;
326 mxr_dbg(mdev, "%s:%d\n", __func__, __LINE__);
328 pix = &f->fmt.pix_mp;
329 fmt = find_format_by_fourcc(layer, pix->pixelformat);
331 mxr_warn(mdev, "not recognized fourcc: %08x\n",
336 /* set source size to highest accepted value */
337 geo->src.full_width = max(geo->dst.full_width, pix->width);
338 geo->src.full_height = max(geo->dst.full_height, pix->height);
339 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SOURCE, 0);
340 mxr_geometry_dump(mdev, &layer->geo);
341 /* set cropping to total visible screen */
342 geo->src.width = pix->width;
343 geo->src.height = pix->height;
344 geo->src.x_offset = 0;
345 geo->src.y_offset = 0;
346 /* assure consistency of geometry */
347 layer->ops.fix_geometry(layer, MXR_GEOMETRY_CROP, MXR_NO_OFFSET);
348 mxr_geometry_dump(mdev, &layer->geo);
349 /* set full size to lowest possible value */
350 geo->src.full_width = 0;
351 geo->src.full_height = 0;
352 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SOURCE, 0);
353 mxr_geometry_dump(mdev, &layer->geo);
355 /* returning results */
356 mxr_g_fmt(file, priv, f);
361 static int mxr_g_selection(struct file *file, void *fh,
362 struct v4l2_selection *s)
364 struct mxr_layer *layer = video_drvdata(file);
365 struct mxr_geometry *geo = &layer->geo;
367 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
369 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
370 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
374 case V4L2_SEL_TGT_CROP:
375 s->r.left = geo->src.x_offset;
376 s->r.top = geo->src.y_offset;
377 s->r.width = geo->src.width;
378 s->r.height = geo->src.height;
380 case V4L2_SEL_TGT_CROP_DEFAULT:
381 case V4L2_SEL_TGT_CROP_BOUNDS:
384 s->r.width = geo->src.full_width;
385 s->r.height = geo->src.full_height;
387 case V4L2_SEL_TGT_COMPOSE:
388 case V4L2_SEL_TGT_COMPOSE_PADDED:
389 s->r.left = geo->dst.x_offset;
390 s->r.top = geo->dst.y_offset;
391 s->r.width = geo->dst.width;
392 s->r.height = geo->dst.height;
394 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
395 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
398 s->r.width = geo->dst.full_width;
399 s->r.height = geo->dst.full_height;
408 /* returns 1 if rectangle 'a' is inside 'b' */
409 static int mxr_is_rect_inside(struct v4l2_rect *a, struct v4l2_rect *b)
411 if (a->left < b->left)
415 if (a->left + a->width > b->left + b->width)
417 if (a->top + a->height > b->top + b->height)
422 static int mxr_s_selection(struct file *file, void *fh,
423 struct v4l2_selection *s)
425 struct mxr_layer *layer = video_drvdata(file);
426 struct mxr_geometry *geo = &layer->geo;
427 struct mxr_crop *target = NULL;
428 enum mxr_geometry_stage stage;
429 struct mxr_geometry tmp;
430 struct v4l2_rect res;
432 memset(&res, 0, sizeof(res));
434 mxr_dbg(layer->mdev, "%s: rect: %dx%d@%d,%d\n", __func__,
435 s->r.width, s->r.height, s->r.left, s->r.top);
437 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
438 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
442 /* ignore read-only targets */
443 case V4L2_SEL_TGT_CROP_DEFAULT:
444 case V4L2_SEL_TGT_CROP_BOUNDS:
445 res.width = geo->src.full_width;
446 res.height = geo->src.full_height;
449 /* ignore read-only targets */
450 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
451 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
452 res.width = geo->dst.full_width;
453 res.height = geo->dst.full_height;
456 case V4L2_SEL_TGT_CROP:
458 stage = MXR_GEOMETRY_CROP;
460 case V4L2_SEL_TGT_COMPOSE:
461 case V4L2_SEL_TGT_COMPOSE_PADDED:
463 stage = MXR_GEOMETRY_COMPOSE;
468 /* apply change and update geometry if needed */
470 /* backup current geometry if setup fails */
471 memcpy(&tmp, geo, sizeof(tmp));
473 /* apply requested selection */
474 target->x_offset = s->r.left;
475 target->y_offset = s->r.top;
476 target->width = s->r.width;
477 target->height = s->r.height;
479 layer->ops.fix_geometry(layer, stage, s->flags);
481 /* retrieve update selection rectangle */
482 res.left = target->x_offset;
483 res.top = target->y_offset;
484 res.width = target->width;
485 res.height = target->height;
487 mxr_geometry_dump(layer->mdev, &layer->geo);
490 /* checking if the rectangle satisfies constraints */
491 if ((s->flags & V4L2_SEL_FLAG_LE) && !mxr_is_rect_inside(&res, &s->r))
493 if ((s->flags & V4L2_SEL_FLAG_GE) && !mxr_is_rect_inside(&s->r, &res))
496 /* return result rectangle */
501 /* restore old geometry, which is not touched if target is NULL */
503 memcpy(geo, &tmp, sizeof(tmp));
507 static int mxr_enum_dv_timings(struct file *file, void *fh,
508 struct v4l2_enum_dv_timings *timings)
510 struct mxr_layer *layer = video_drvdata(file);
511 struct mxr_device *mdev = layer->mdev;
516 /* lock protects from changing sd_out */
517 mutex_lock(&mdev->mutex);
518 ret = v4l2_subdev_call(to_outsd(mdev), pad, enum_dv_timings, timings);
519 mutex_unlock(&mdev->mutex);
521 return ret ? -EINVAL : 0;
524 static int mxr_s_dv_timings(struct file *file, void *fh,
525 struct v4l2_dv_timings *timings)
527 struct mxr_layer *layer = video_drvdata(file);
528 struct mxr_device *mdev = layer->mdev;
531 /* lock protects from changing sd_out */
532 mutex_lock(&mdev->mutex);
534 /* timings change cannot be done while there is an entity
535 * dependent on output configuration
537 if (mdev->n_output > 0) {
538 mutex_unlock(&mdev->mutex);
542 ret = v4l2_subdev_call(to_outsd(mdev), video, s_dv_timings, timings);
544 mutex_unlock(&mdev->mutex);
546 mxr_layer_update_output(layer);
548 /* any failure should return EINVAL according to V4L2 doc */
549 return ret ? -EINVAL : 0;
552 static int mxr_g_dv_timings(struct file *file, void *fh,
553 struct v4l2_dv_timings *timings)
555 struct mxr_layer *layer = video_drvdata(file);
556 struct mxr_device *mdev = layer->mdev;
559 /* lock protects from changing sd_out */
560 mutex_lock(&mdev->mutex);
561 ret = v4l2_subdev_call(to_outsd(mdev), video, g_dv_timings, timings);
562 mutex_unlock(&mdev->mutex);
564 return ret ? -EINVAL : 0;
567 static int mxr_dv_timings_cap(struct file *file, void *fh,
568 struct v4l2_dv_timings_cap *cap)
570 struct mxr_layer *layer = video_drvdata(file);
571 struct mxr_device *mdev = layer->mdev;
576 /* lock protects from changing sd_out */
577 mutex_lock(&mdev->mutex);
578 ret = v4l2_subdev_call(to_outsd(mdev), pad, dv_timings_cap, cap);
579 mutex_unlock(&mdev->mutex);
581 return ret ? -EINVAL : 0;
584 static int mxr_s_std(struct file *file, void *fh, v4l2_std_id norm)
586 struct mxr_layer *layer = video_drvdata(file);
587 struct mxr_device *mdev = layer->mdev;
590 /* lock protects from changing sd_out */
591 mutex_lock(&mdev->mutex);
593 /* standard change cannot be done while there is an entity
594 * dependent on output configuration
596 if (mdev->n_output > 0) {
597 mutex_unlock(&mdev->mutex);
601 ret = v4l2_subdev_call(to_outsd(mdev), video, s_std_output, norm);
603 mutex_unlock(&mdev->mutex);
605 mxr_layer_update_output(layer);
607 return ret ? -EINVAL : 0;
610 static int mxr_g_std(struct file *file, void *fh, v4l2_std_id *norm)
612 struct mxr_layer *layer = video_drvdata(file);
613 struct mxr_device *mdev = layer->mdev;
616 /* lock protects from changing sd_out */
617 mutex_lock(&mdev->mutex);
618 ret = v4l2_subdev_call(to_outsd(mdev), video, g_std_output, norm);
619 mutex_unlock(&mdev->mutex);
621 return ret ? -EINVAL : 0;
624 static int mxr_enum_output(struct file *file, void *fh, struct v4l2_output *a)
626 struct mxr_layer *layer = video_drvdata(file);
627 struct mxr_device *mdev = layer->mdev;
628 struct mxr_output *out;
629 struct v4l2_subdev *sd;
631 if (a->index >= mdev->output_cnt)
633 out = mdev->output[a->index];
636 strlcpy(a->name, out->name, sizeof(a->name));
638 /* try to obtain supported tv norms */
639 v4l2_subdev_call(sd, video, g_tvnorms_output, &a->std);
641 if (sd->ops->video && sd->ops->video->s_dv_timings)
642 a->capabilities |= V4L2_OUT_CAP_DV_TIMINGS;
643 if (sd->ops->video && sd->ops->video->s_std_output)
644 a->capabilities |= V4L2_OUT_CAP_STD;
645 a->type = V4L2_OUTPUT_TYPE_ANALOG;
650 static int mxr_s_output(struct file *file, void *fh, unsigned int i)
652 struct video_device *vfd = video_devdata(file);
653 struct mxr_layer *layer = video_drvdata(file);
654 struct mxr_device *mdev = layer->mdev;
656 if (i >= mdev->output_cnt || mdev->output[i] == NULL)
659 mutex_lock(&mdev->mutex);
660 if (mdev->n_output > 0) {
661 mutex_unlock(&mdev->mutex);
664 mdev->current_output = i;
666 v4l2_subdev_call(to_outsd(mdev), video, g_tvnorms_output,
668 mutex_unlock(&mdev->mutex);
670 /* update layers geometry */
671 mxr_layer_update_output(layer);
673 mxr_dbg(mdev, "tvnorms = %08llx\n", vfd->tvnorms);
678 static int mxr_g_output(struct file *file, void *fh, unsigned int *p)
680 struct mxr_layer *layer = video_drvdata(file);
681 struct mxr_device *mdev = layer->mdev;
683 mutex_lock(&mdev->mutex);
684 *p = mdev->current_output;
685 mutex_unlock(&mdev->mutex);
690 static int mxr_reqbufs(struct file *file, void *priv,
691 struct v4l2_requestbuffers *p)
693 struct mxr_layer *layer = video_drvdata(file);
695 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
696 return vb2_reqbufs(&layer->vb_queue, p);
699 static int mxr_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
701 struct mxr_layer *layer = video_drvdata(file);
703 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
704 return vb2_querybuf(&layer->vb_queue, p);
707 static int mxr_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
709 struct mxr_layer *layer = video_drvdata(file);
711 mxr_dbg(layer->mdev, "%s:%d(%d)\n", __func__, __LINE__, p->index);
712 return vb2_qbuf(&layer->vb_queue, p);
715 static int mxr_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
717 struct mxr_layer *layer = video_drvdata(file);
719 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
720 return vb2_dqbuf(&layer->vb_queue, p, file->f_flags & O_NONBLOCK);
723 static int mxr_expbuf(struct file *file, void *priv,
724 struct v4l2_exportbuffer *eb)
726 struct mxr_layer *layer = video_drvdata(file);
728 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
729 return vb2_expbuf(&layer->vb_queue, eb);
732 static int mxr_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
734 struct mxr_layer *layer = video_drvdata(file);
736 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
737 return vb2_streamon(&layer->vb_queue, i);
740 static int mxr_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
742 struct mxr_layer *layer = video_drvdata(file);
744 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
745 return vb2_streamoff(&layer->vb_queue, i);
748 static const struct v4l2_ioctl_ops mxr_ioctl_ops = {
749 .vidioc_querycap = mxr_querycap,
750 /* format handling */
751 .vidioc_enum_fmt_vid_out_mplane = mxr_enum_fmt,
752 .vidioc_s_fmt_vid_out_mplane = mxr_s_fmt,
753 .vidioc_g_fmt_vid_out_mplane = mxr_g_fmt,
755 .vidioc_reqbufs = mxr_reqbufs,
756 .vidioc_querybuf = mxr_querybuf,
757 .vidioc_qbuf = mxr_qbuf,
758 .vidioc_dqbuf = mxr_dqbuf,
759 .vidioc_expbuf = mxr_expbuf,
760 /* Streaming control */
761 .vidioc_streamon = mxr_streamon,
762 .vidioc_streamoff = mxr_streamoff,
763 /* DV Timings functions */
764 .vidioc_enum_dv_timings = mxr_enum_dv_timings,
765 .vidioc_s_dv_timings = mxr_s_dv_timings,
766 .vidioc_g_dv_timings = mxr_g_dv_timings,
767 .vidioc_dv_timings_cap = mxr_dv_timings_cap,
768 /* analog TV standard functions */
769 .vidioc_s_std = mxr_s_std,
770 .vidioc_g_std = mxr_g_std,
771 /* Output handling */
772 .vidioc_enum_output = mxr_enum_output,
773 .vidioc_s_output = mxr_s_output,
774 .vidioc_g_output = mxr_g_output,
775 /* selection ioctls */
776 .vidioc_g_selection = mxr_g_selection,
777 .vidioc_s_selection = mxr_s_selection,
780 static int mxr_video_open(struct file *file)
782 struct mxr_layer *layer = video_drvdata(file);
783 struct mxr_device *mdev = layer->mdev;
786 mxr_dbg(mdev, "%s:%d\n", __func__, __LINE__);
787 if (mutex_lock_interruptible(&layer->mutex))
789 /* assure device probe is finished */
790 wait_for_device_probe();
791 /* creating context for file descriptor */
792 ret = v4l2_fh_open(file);
794 mxr_err(mdev, "v4l2_fh_open failed\n");
798 /* leaving if layer is already initialized */
799 if (!v4l2_fh_is_singular_file(file))
802 /* FIXME: should power be enabled on open? */
803 ret = mxr_power_get(mdev);
805 mxr_err(mdev, "power on failed\n");
809 ret = vb2_queue_init(&layer->vb_queue);
811 mxr_err(mdev, "failed to initialize vb2 queue\n");
814 /* set default format, first on the list */
815 layer->fmt = layer->fmt_array[0];
816 /* setup default geometry */
817 mxr_layer_default_geo(layer);
818 mutex_unlock(&layer->mutex);
826 v4l2_fh_release(file);
829 mutex_unlock(&layer->mutex);
835 mxr_video_poll(struct file *file, struct poll_table_struct *wait)
837 struct mxr_layer *layer = video_drvdata(file);
840 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
842 mutex_lock(&layer->mutex);
843 res = vb2_poll(&layer->vb_queue, file, wait);
844 mutex_unlock(&layer->mutex);
848 static int mxr_video_mmap(struct file *file, struct vm_area_struct *vma)
850 struct mxr_layer *layer = video_drvdata(file);
853 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
855 if (mutex_lock_interruptible(&layer->mutex))
857 ret = vb2_mmap(&layer->vb_queue, vma);
858 mutex_unlock(&layer->mutex);
862 static int mxr_video_release(struct file *file)
864 struct mxr_layer *layer = video_drvdata(file);
866 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
867 mutex_lock(&layer->mutex);
868 if (v4l2_fh_is_singular_file(file)) {
869 vb2_queue_release(&layer->vb_queue);
870 mxr_power_put(layer->mdev);
872 v4l2_fh_release(file);
873 mutex_unlock(&layer->mutex);
877 static const struct v4l2_file_operations mxr_fops = {
878 .owner = THIS_MODULE,
879 .open = mxr_video_open,
880 .poll = mxr_video_poll,
881 .mmap = mxr_video_mmap,
882 .release = mxr_video_release,
883 .unlocked_ioctl = video_ioctl2,
886 static int queue_setup(struct vb2_queue *vq,
887 unsigned int *nbuffers, unsigned int *nplanes, unsigned int sizes[],
890 struct mxr_layer *layer = vb2_get_drv_priv(vq);
891 const struct mxr_format *fmt = layer->fmt;
893 struct mxr_device *mdev = layer->mdev;
894 struct v4l2_plane_pix_format planes[3];
896 mxr_dbg(mdev, "%s\n", __func__);
897 /* checking if format was configured */
900 mxr_dbg(mdev, "fmt = %s\n", fmt->name);
901 mxr_mplane_fill(planes, fmt, layer->geo.src.full_width,
902 layer->geo.src.full_height);
904 *nplanes = fmt->num_subframes;
905 for (i = 0; i < fmt->num_subframes; ++i) {
906 alloc_ctxs[i] = layer->mdev->alloc_ctx;
907 sizes[i] = planes[i].sizeimage;
908 mxr_dbg(mdev, "size[%d] = %08x\n", i, sizes[i]);
917 static void buf_queue(struct vb2_buffer *vb)
919 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
920 struct mxr_buffer *buffer = container_of(vbuf, struct mxr_buffer, vb);
921 struct mxr_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
922 struct mxr_device *mdev = layer->mdev;
925 spin_lock_irqsave(&layer->enq_slock, flags);
926 list_add_tail(&buffer->list, &layer->enq_list);
927 spin_unlock_irqrestore(&layer->enq_slock, flags);
929 mxr_dbg(mdev, "queuing buffer\n");
932 static int start_streaming(struct vb2_queue *vq, unsigned int count)
934 struct mxr_layer *layer = vb2_get_drv_priv(vq);
935 struct mxr_device *mdev = layer->mdev;
938 mxr_dbg(mdev, "%s\n", __func__);
940 /* block any changes in output configuration */
941 mxr_output_get(mdev);
943 mxr_layer_update_output(layer);
944 layer->ops.format_set(layer);
945 /* enabling layer in hardware */
946 spin_lock_irqsave(&layer->enq_slock, flags);
947 layer->state = MXR_LAYER_STREAMING;
948 spin_unlock_irqrestore(&layer->enq_slock, flags);
950 layer->ops.stream_set(layer, MXR_ENABLE);
951 mxr_streamer_get(mdev);
956 static void mxr_watchdog(unsigned long arg)
958 struct mxr_layer *layer = (struct mxr_layer *) arg;
959 struct mxr_device *mdev = layer->mdev;
962 mxr_err(mdev, "watchdog fired for layer %s\n", layer->vfd.name);
964 spin_lock_irqsave(&layer->enq_slock, flags);
966 if (layer->update_buf == layer->shadow_buf)
967 layer->update_buf = NULL;
968 if (layer->update_buf) {
969 vb2_buffer_done(&layer->update_buf->vb.vb2_buf,
970 VB2_BUF_STATE_ERROR);
971 layer->update_buf = NULL;
973 if (layer->shadow_buf) {
974 vb2_buffer_done(&layer->shadow_buf->vb.vb2_buf,
975 VB2_BUF_STATE_ERROR);
976 layer->shadow_buf = NULL;
978 spin_unlock_irqrestore(&layer->enq_slock, flags);
981 static void stop_streaming(struct vb2_queue *vq)
983 struct mxr_layer *layer = vb2_get_drv_priv(vq);
984 struct mxr_device *mdev = layer->mdev;
986 struct timer_list watchdog;
987 struct mxr_buffer *buf, *buf_tmp;
989 mxr_dbg(mdev, "%s\n", __func__);
991 spin_lock_irqsave(&layer->enq_slock, flags);
994 layer->state = MXR_LAYER_STREAMING_FINISH;
996 /* set all buffer to be done */
997 list_for_each_entry_safe(buf, buf_tmp, &layer->enq_list, list) {
998 list_del(&buf->list);
999 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1002 spin_unlock_irqrestore(&layer->enq_slock, flags);
1004 /* give 1 seconds to complete to complete last buffers */
1005 setup_timer_on_stack(&watchdog, mxr_watchdog,
1006 (unsigned long)layer);
1007 mod_timer(&watchdog, jiffies + msecs_to_jiffies(1000));
1009 /* wait until all buffers are goes to done state */
1010 vb2_wait_for_all_buffers(vq);
1012 /* stop timer if all synchronization is done */
1013 del_timer_sync(&watchdog);
1014 destroy_timer_on_stack(&watchdog);
1016 /* stopping hardware */
1017 spin_lock_irqsave(&layer->enq_slock, flags);
1018 layer->state = MXR_LAYER_IDLE;
1019 spin_unlock_irqrestore(&layer->enq_slock, flags);
1021 /* disabling layer in hardware */
1022 layer->ops.stream_set(layer, MXR_DISABLE);
1023 /* remove one streamer */
1024 mxr_streamer_put(mdev);
1025 /* allow changes in output configuration */
1026 mxr_output_put(mdev);
1029 static struct vb2_ops mxr_video_qops = {
1030 .queue_setup = queue_setup,
1031 .buf_queue = buf_queue,
1032 .wait_prepare = vb2_ops_wait_prepare,
1033 .wait_finish = vb2_ops_wait_finish,
1034 .start_streaming = start_streaming,
1035 .stop_streaming = stop_streaming,
1038 /* FIXME: try to put this functions to mxr_base_layer_create */
1039 int mxr_base_layer_register(struct mxr_layer *layer)
1041 struct mxr_device *mdev = layer->mdev;
1044 ret = video_register_device(&layer->vfd, VFL_TYPE_GRABBER, -1);
1046 mxr_err(mdev, "failed to register video device\n");
1048 mxr_info(mdev, "registered layer %s as /dev/video%d\n",
1049 layer->vfd.name, layer->vfd.num);
1053 void mxr_base_layer_unregister(struct mxr_layer *layer)
1055 video_unregister_device(&layer->vfd);
1058 void mxr_layer_release(struct mxr_layer *layer)
1060 if (layer->ops.release)
1061 layer->ops.release(layer);
1064 void mxr_base_layer_release(struct mxr_layer *layer)
1069 static void mxr_vfd_release(struct video_device *vdev)
1071 pr_info("video device release\n");
1074 struct mxr_layer *mxr_base_layer_create(struct mxr_device *mdev,
1075 int idx, char *name, const struct mxr_layer_ops *ops)
1077 struct mxr_layer *layer;
1079 layer = kzalloc(sizeof(*layer), GFP_KERNEL);
1080 if (layer == NULL) {
1081 mxr_err(mdev, "not enough memory for layer.\n");
1089 spin_lock_init(&layer->enq_slock);
1090 INIT_LIST_HEAD(&layer->enq_list);
1091 mutex_init(&layer->mutex);
1093 layer->vfd = (struct video_device) {
1095 .release = mxr_vfd_release,
1097 .vfl_dir = VFL_DIR_TX,
1098 .ioctl_ops = &mxr_ioctl_ops,
1100 strlcpy(layer->vfd.name, name, sizeof(layer->vfd.name));
1102 video_set_drvdata(&layer->vfd, layer);
1103 layer->vfd.lock = &layer->mutex;
1104 layer->vfd.v4l2_dev = &mdev->v4l2_dev;
1106 layer->vb_queue = (struct vb2_queue) {
1107 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
1108 .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF,
1110 .buf_struct_size = sizeof(struct mxr_buffer),
1111 .ops = &mxr_video_qops,
1112 .min_buffers_needed = 1,
1113 .mem_ops = &vb2_dma_contig_memops,
1114 .lock = &layer->mutex,
1123 static const struct mxr_format *find_format_by_fourcc(
1124 struct mxr_layer *layer, unsigned long fourcc)
1128 for (i = 0; i < layer->fmt_array_size; ++i)
1129 if (layer->fmt_array[i]->fourcc == fourcc)
1130 return layer->fmt_array[i];
1134 static const struct mxr_format *find_format_by_index(
1135 struct mxr_layer *layer, unsigned long index)
1137 if (index >= layer->fmt_array_size)
1139 return layer->fmt_array[index];