14 #define pr_fmt(fmt) "s5p-tv (mixer): " fmt
19 #include <linux/videodev2.h>
21 #include <linux/module.h>
22 #include <linux/version.h>
26 static int find_reg_callback(
struct device *
dev,
void *
p)
35 static struct v4l2_subdev *find_and_register_subdev(
45 mxr_warn(mdev,
"module %s is missing\n", module_name);
52 mxr_warn(mdev,
"module %s provides no subdev!\n", module_name);
58 mxr_warn(mdev,
"failed to register subdev %s\n", sd->
name);
79 mxr_err(mdev,
"could not register v4l2 device.\n");
85 mxr_err(mdev,
"could not acquire vb2 allocator\n");
95 sd = find_and_register_subdev(mdev, conf->
module_name);
101 mxr_err(mdev,
"no memory for '%s'\n",
111 mxr_info(mdev,
"added output '%s' from module '%s'\n",
119 mxr_err(mdev,
"failed to register any output\n");
122 goto fail_vb2_allocator;
160 struct mxr_layer *layer = video_drvdata(file);
162 mxr_dbg(layer->
mdev,
"%s:%d\n", __func__, __LINE__);
175 mxr_dbg(mdev,
"src.full_size = (%u, %u)\n",
176 geo->
src.full_width, geo->
src.full_height);
177 mxr_dbg(mdev,
"src.size = (%u, %u)\n",
178 geo->
src.width, geo->
src.height);
179 mxr_dbg(mdev,
"src.offset = (%u, %u)\n",
180 geo->
src.x_offset, geo->
src.y_offset);
181 mxr_dbg(mdev,
"dst.full_size = (%u, %u)\n",
182 geo->
dst.full_width, geo->
dst.full_height);
183 mxr_dbg(mdev,
"dst.size = (%u, %u)\n",
184 geo->
dst.width, geo->
dst.height);
185 mxr_dbg(mdev,
"dst.offset = (%u, %u)\n",
186 geo->
dst.x_offset, geo->
dst.y_offset);
187 mxr_dbg(mdev,
"ratio = (%u, %u)\n",
191 static void mxr_layer_default_geo(
struct mxr_layer *layer)
194 struct v4l2_mbus_framefmt mbus_fmt;
200 layer->
geo.dst.full_width = mbus_fmt.width;
201 layer->
geo.dst.full_height = mbus_fmt.height;
202 layer->
geo.dst.width = layer->
geo.dst.full_width;
203 layer->
geo.dst.height = layer->
geo.dst.full_height;
204 layer->
geo.dst.field = mbus_fmt.field;
206 layer->
geo.src.full_width = mbus_fmt.width;
207 layer->
geo.src.full_height = mbus_fmt.height;
208 layer->
geo.src.width = layer->
geo.src.full_width;
209 layer->
geo.src.height = layer->
geo.src.full_height;
211 mxr_geometry_dump(mdev, &layer->
geo);
213 mxr_geometry_dump(mdev, &layer->
geo);
216 static void mxr_layer_update_output(
struct mxr_layer *layer)
219 struct v4l2_mbus_framefmt mbus_fmt;
223 if (layer->
geo.dst.full_width == mbus_fmt.width &&
224 layer->
geo.dst.full_height == mbus_fmt.width)
227 layer->
geo.dst.full_width = mbus_fmt.width;
228 layer->
geo.dst.full_height = mbus_fmt.height;
229 layer->
geo.dst.field = mbus_fmt.field;
232 mxr_geometry_dump(mdev, &layer->
geo);
235 static const struct mxr_format *find_format_by_fourcc(
237 static const struct mxr_format *find_format_by_index(
243 struct mxr_layer *layer = video_drvdata(file);
247 mxr_dbg(mdev,
"%s\n", __func__);
248 fmt = find_format_by_index(layer, f->
index);
258 static unsigned int divup(
unsigned int divident,
unsigned int divisor)
260 return (divident + divisor - 1) /
divisor;
266 unsigned int bl_width = divup(width, blk->
width);
267 unsigned int bl_height = divup(height, blk->
height);
269 return bl_width * bl_height * blk->
size;
286 u32 bl_width = divup(width, blk->
width);
287 u32 bl_height = divup(height, blk->
height);
296 static int mxr_g_fmt(
struct file *file,
void *priv,
299 struct mxr_layer *layer = video_drvdata(file);
302 mxr_dbg(layer->
mdev,
"%s:%d\n", __func__, __LINE__);
304 pix->
width = layer->
geo.src.full_width;
305 pix->
height = layer->
geo.src.full_height;
314 static int mxr_s_fmt(
struct file *file,
void *priv,
317 struct mxr_layer *layer = video_drvdata(file);
323 mxr_dbg(mdev,
"%s:%d\n", __func__, __LINE__);
326 fmt = find_format_by_fourcc(layer, pix->
pixelformat);
328 mxr_warn(mdev,
"not recognized fourcc: %08x\n",
337 mxr_geometry_dump(mdev, &layer->
geo);
341 geo->
src.x_offset = 0;
342 geo->
src.y_offset = 0;
345 mxr_geometry_dump(mdev, &layer->
geo);
347 geo->
src.full_width = 0;
348 geo->
src.full_height = 0;
350 mxr_geometry_dump(mdev, &layer->
geo);
353 mxr_g_fmt(file, priv, f);
358 static int mxr_g_selection(
struct file *file,
void *fh,
361 struct mxr_layer *layer = video_drvdata(file);
364 mxr_dbg(layer->
mdev,
"%s:%d\n", __func__, __LINE__);
372 s->
r.left = geo->
src.x_offset;
373 s->
r.top = geo->
src.y_offset;
374 s->
r.width = geo->
src.width;
375 s->
r.height = geo->
src.height;
381 s->
r.width = geo->
src.full_width;
382 s->
r.height = geo->
src.full_height;
386 s->
r.left = geo->
dst.x_offset;
387 s->
r.top = geo->
dst.y_offset;
388 s->
r.width = geo->
dst.width;
389 s->
r.height = geo->
dst.height;
395 s->
r.width = geo->
dst.full_width;
396 s->
r.height = geo->
dst.full_height;
419 static int mxr_s_selection(
struct file *file,
void *fh,
422 struct mxr_layer *layer = video_drvdata(file);
431 mxr_dbg(layer->
mdev,
"%s: rect: %dx%d@%d,%d\n", __func__,
432 s->
r.width, s->
r.height, s->
r.left, s->
r.top);
442 res.width = geo->
src.full_width;
443 res.height = geo->
src.full_height;
449 res.width = geo->
dst.full_width;
450 res.height = geo->
dst.full_height;
473 target->
width = s->
r.width;
476 layer->
ops.fix_geometry(layer, stage, s->
flags);
484 mxr_geometry_dump(layer->
mdev, &layer->
geo);
504 static int mxr_enum_dv_presets(
struct file *file,
void *fh,
507 struct mxr_layer *layer = video_drvdata(file);
519 static int mxr_s_dv_preset(
struct file *file,
void *fh,
522 struct mxr_layer *layer = video_drvdata(file);
541 mxr_layer_update_output(layer);
547 static int mxr_g_dv_preset(
struct file *file,
void *fh,
550 struct mxr_layer *layer = video_drvdata(file);
562 static int mxr_s_std(
struct file *file,
void *fh,
v4l2_std_id *norm)
564 struct mxr_layer *layer = video_drvdata(file);
583 mxr_layer_update_output(layer);
588 static int mxr_g_std(
struct file *file,
void *fh,
v4l2_std_id *norm)
590 struct mxr_layer *layer = video_drvdata(file);
602 static int mxr_enum_output(
struct file *file,
void *fh,
struct v4l2_output *a)
604 struct mxr_layer *layer = video_drvdata(file);
619 if (sd->
ops->video && sd->
ops->video->s_dv_preset)
621 if (sd->
ops->video && sd->
ops->video->s_std_output)
628 static int mxr_s_output(
struct file *file,
void *fh,
unsigned int i)
631 struct mxr_layer *layer = video_drvdata(file);
649 mxr_layer_update_output(layer);
656 static int mxr_g_output(
struct file *file,
void *fh,
unsigned int *
p)
658 struct mxr_layer *layer = video_drvdata(file);
668 static int mxr_reqbufs(
struct file *file,
void *priv,
671 struct mxr_layer *layer = video_drvdata(file);
673 mxr_dbg(layer->
mdev,
"%s:%d\n", __func__, __LINE__);
677 static int mxr_querybuf(
struct file *file,
void *priv,
struct v4l2_buffer *
p)
679 struct mxr_layer *layer = video_drvdata(file);
681 mxr_dbg(layer->
mdev,
"%s:%d\n", __func__, __LINE__);
685 static int mxr_qbuf(
struct file *file,
void *priv,
struct v4l2_buffer *
p)
687 struct mxr_layer *layer = video_drvdata(file);
693 static int mxr_dqbuf(
struct file *file,
void *priv,
struct v4l2_buffer *
p)
695 struct mxr_layer *layer = video_drvdata(file);
697 mxr_dbg(layer->
mdev,
"%s:%d\n", __func__, __LINE__);
701 static int mxr_streamon(
struct file *file,
void *priv,
enum v4l2_buf_type i)
703 struct mxr_layer *layer = video_drvdata(file);
705 mxr_dbg(layer->
mdev,
"%s:%d\n", __func__, __LINE__);
709 static int mxr_streamoff(
struct file *file,
void *priv,
enum v4l2_buf_type i)
711 struct mxr_layer *layer = video_drvdata(file);
713 mxr_dbg(layer->
mdev,
"%s:%d\n", __func__, __LINE__);
718 .vidioc_querycap = mxr_querycap,
720 .vidioc_enum_fmt_vid_out_mplane = mxr_enum_fmt,
721 .vidioc_s_fmt_vid_out_mplane = mxr_s_fmt,
722 .vidioc_g_fmt_vid_out_mplane = mxr_g_fmt,
724 .vidioc_reqbufs = mxr_reqbufs,
725 .vidioc_querybuf = mxr_querybuf,
726 .vidioc_qbuf = mxr_qbuf,
727 .vidioc_dqbuf = mxr_dqbuf,
729 .vidioc_streamon = mxr_streamon,
730 .vidioc_streamoff = mxr_streamoff,
732 .vidioc_enum_dv_presets = mxr_enum_dv_presets,
733 .vidioc_s_dv_preset = mxr_s_dv_preset,
734 .vidioc_g_dv_preset = mxr_g_dv_preset,
736 .vidioc_s_std = mxr_s_std,
737 .vidioc_g_std = mxr_g_std,
739 .vidioc_enum_output = mxr_enum_output,
740 .vidioc_s_output = mxr_s_output,
741 .vidioc_g_output = mxr_g_output,
743 .vidioc_g_selection = mxr_g_selection,
744 .vidioc_s_selection = mxr_s_selection,
747 static int mxr_video_open(
struct file *file)
749 struct mxr_layer *layer = video_drvdata(file);
753 mxr_dbg(mdev,
"%s:%d\n", __func__, __LINE__);
761 mxr_err(mdev,
"v4l2_fh_open failed\n");
766 if (!v4l2_fh_is_singular_file(file))
772 mxr_err(mdev,
"power on failed\n");
778 mxr_err(mdev,
"failed to initialize vb2 queue\n");
784 mxr_layer_default_geo(layer);
804 struct mxr_layer *layer = video_drvdata(file);
807 mxr_dbg(layer->
mdev,
"%s:%d\n", __func__, __LINE__);
815 static int mxr_video_mmap(
struct file *file,
struct vm_area_struct *vma)
817 struct mxr_layer *layer = video_drvdata(file);
820 mxr_dbg(layer->
mdev,
"%s:%d\n", __func__, __LINE__);
829 static int mxr_video_release(
struct file *file)
831 struct mxr_layer *layer = video_drvdata(file);
833 mxr_dbg(layer->
mdev,
"%s:%d\n", __func__, __LINE__);
835 if (v4l2_fh_is_singular_file(file)) {
846 .open = mxr_video_open,
847 .poll = mxr_video_poll,
848 .mmap = mxr_video_mmap,
849 .release = mxr_video_release,
854 unsigned int *nbuffers,
unsigned int *nplanes,
unsigned int sizes[],
857 struct mxr_layer *layer = vb2_get_drv_priv(vq);
863 mxr_dbg(mdev,
"%s\n", __func__);
868 mxr_mplane_fill(planes, fmt, layer->
geo.src.full_width,
869 layer->
geo.src.full_height);
873 alloc_ctxs[
i] = layer->
mdev->alloc_ctx;
875 mxr_dbg(mdev,
"size[%d] = %08x\n", i, sizes[i]);
893 spin_unlock_irqrestore(&layer->
enq_slock, flags);
895 mxr_dbg(mdev,
"queuing buffer\n");
898 static void wait_lock(
struct vb2_queue *vq)
900 struct mxr_layer *layer = vb2_get_drv_priv(vq);
906 static void wait_unlock(
struct vb2_queue *vq)
908 struct mxr_layer *layer = vb2_get_drv_priv(vq);
914 static int start_streaming(
struct vb2_queue *vq,
unsigned int count)
916 struct mxr_layer *layer = vb2_get_drv_priv(vq);
920 mxr_dbg(mdev,
"%s\n", __func__);
923 mxr_dbg(mdev,
"no output buffers queued\n");
930 mxr_layer_update_output(layer);
931 layer->
ops.format_set(layer);
935 spin_unlock_irqrestore(&layer->
enq_slock, flags);
943 static void mxr_watchdog(
unsigned long arg)
949 mxr_err(mdev,
"watchdog fired for layer %s\n", layer->
vfd.name);
963 spin_unlock_irqrestore(&layer->
enq_slock, flags);
966 static int stop_streaming(
struct vb2_queue *vq)
968 struct mxr_layer *layer = vb2_get_drv_priv(vq);
974 mxr_dbg(mdev,
"%s\n", __func__);
987 spin_unlock_irqrestore(&layer->
enq_slock, flags);
991 (
unsigned long)layer);
1004 spin_unlock_irqrestore(&layer->
enq_slock, flags);
1015 static struct vb2_ops mxr_video_qops = {
1018 .wait_prepare = wait_unlock,
1019 .wait_finish = wait_lock,
1032 mxr_err(mdev,
"failed to register video device\n");
1034 mxr_info(mdev,
"registered layer %s as /dev/video%d\n",
1035 layer->
vfd.name, layer->
vfd.num);
1046 if (layer->
ops.release)
1047 layer->
ops.release(layer);
1057 pr_info(
"video device release\n");
1066 if (layer ==
NULL) {
1067 mxr_err(mdev,
"not enough memory for layer.\n");
1081 .release = mxr_vfd_release,
1084 .ioctl_ops = &mxr_ioctl_ops,
1090 video_set_drvdata(&layer->
vfd, layer);
1098 .buf_struct_size =
sizeof(
struct mxr_buffer),
1099 .ops = &mxr_video_qops,
1109 static const struct mxr_format *find_format_by_fourcc(
1115 if (layer->
fmt_array[i]->fourcc == fourcc)
1120 static const struct mxr_format *find_format_by_index(