21 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
29 #include <linux/string.h>
30 #include <linux/videodev2.h>
31 #include <linux/wait.h>
32 #include <linux/time.h>
33 #include <linux/i2c.h>
36 #include <linux/slab.h>
48 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
49 #define vpif_dbg(level, debug, fmt, arg...) \
50 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
53 static u32 ch0_numbuffers = 3;
54 static u32 ch1_numbuffers = 3;
55 static u32 ch0_bufsize = 1920 * 1080 * 2;
56 static u32 ch1_bufsize = 720 * 576 * 2;
67 MODULE_PARM_DESC(ch2_bufsize,
"Channel0 buffer size (default:1920 x 1080 x 2)");
68 MODULE_PARM_DESC(ch3_bufsize,
"Channel1 buffer size (default:720 x 576 x 2)");
74 .min_bufsize[0] = 720 * 480 * 2,
75 .min_bufsize[1] = 720 * 480 * 2,
76 .channel_bufsize[0] = 1920 * 1080 * 2,
77 .channel_bufsize[1] = 720 * 576 * 2,
82 static struct device *vpif_dev;
83 static void vpif_calculate_offsets(
struct channel_obj *ch);
84 static void vpif_config_addr(
struct channel_obj *ch,
int muxmode);
94 static int vpif_buffer_prepare(
struct vb2_buffer *vb)
109 vb2_set_plane_payload(vb, 0, common->
fmt.fmt.pix.sizeimage);
111 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
113 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
125 vpif_dbg(1,
debug,
"buffer_prepare:offset is not aligned to 8 bytes\n");
141 static int vpif_buffer_queue_setup(
struct vb2_queue *vq,
143 unsigned int *nbuffers,
unsigned int *nplanes,
144 unsigned int sizes[],
void *alloc_ctxs[])
147 struct vpif_fh *fh = vb2_get_drv_priv(vq);
165 if (ch->
vpifparams.std_info.ycmux_mode == 0) {
167 while (size * *nbuffers >
173 while (size * *nbuffers >
179 size = common->
fmt.fmt.pix.sizeimage;
196 static void vpif_buffer_queue(
struct vb2_buffer *vb)
202 struct vpif_cap_buffer, vb);
220 static void vpif_buf_cleanup(
struct vb2_buffer *vb)
225 struct vpif_cap_buffer, vb);
234 list_del_init(&buf->list);
235 spin_unlock_irqrestore(&common->
irqlock, flags);
239 static void vpif_wait_prepare(
struct vb2_queue *vq)
241 struct vpif_fh *fh = vb2_get_drv_priv(vq);
249 static void vpif_wait_finish(
struct vb2_queue *vq)
251 struct vpif_fh *fh = vb2_get_drv_priv(vq);
259 static int vpif_buffer_init(
struct vb2_buffer *vb)
262 struct vpif_cap_buffer, vb);
264 INIT_LIST_HEAD(&buf->list);
269 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
272 static int vpif_start_streaming(
struct vb2_queue *vq,
unsigned int count)
276 struct vpif_fh *fh = vb2_get_drv_priv(vq);
280 unsigned long addr = 0;
291 struct vpif_cap_buffer,
list);
299 addr = vb2_dma_contig_plane_dma_addr(&common->
cur_frm->vb, 0);
302 vpif_calculate_offsets(ch);
309 vpif_dbg(1,
debug,
"conflict in field format and std format\n");
315 ret = vpif_config_data->
316 setup_input_channel_mode(vpif->
std_info.ycmux_mode);
332 vpif_config_addr(ch, ret);
344 if ((VPIF_CHANNEL0_VIDEO == ch->
channel_id)) {
346 channel0_intr_enable(1);
349 if ((VPIF_CHANNEL1_VIDEO == ch->
channel_id) ||
352 channel1_intr_enable(1);
360 static int vpif_stop_streaming(
struct vb2_queue *vq)
362 struct vpif_fh *fh = vb2_get_drv_priv(vq);
366 if (!vb2_is_streaming(vq))
372 while (!list_empty(&common->
dma_queue)) {
374 struct vpif_cap_buffer,
list);
382 static struct vb2_ops video_qops = {
383 .queue_setup = vpif_buffer_queue_setup,
384 .wait_prepare = vpif_wait_prepare,
385 .wait_finish = vpif_wait_finish,
386 .buf_init = vpif_buffer_init,
387 .buf_prepare = vpif_buffer_prepare,
388 .start_streaming = vpif_start_streaming,
389 .stop_streaming = vpif_stop_streaming,
390 .buf_cleanup = vpif_buf_cleanup,
391 .buf_queue = vpif_buffer_queue,
402 static void vpif_process_buffer_complete(
struct common_obj *common)
419 static void vpif_schedule_next_buffer(
struct common_obj *common)
421 unsigned long addr = 0;
424 struct vpif_cap_buffer,
list);
428 addr = vb2_dma_contig_plane_dma_addr(&common->
next_frm->vb, 0);
454 channel_id = *(
int *)(dev_id);
455 if (!vpif_intr_status(channel_id))
462 for (
i = 0;
i < VPIF_NUMBER_OF_OBJECTS;
i++) {
474 if (!channel_first_int[
i][channel_id])
475 vpif_process_buffer_complete(common);
479 vpif_schedule_next_buffer(common);
488 if (channel_first_int[
i][channel_id]) {
513 vpif_process_buffer_complete(common);
514 }
else if (1 == fid) {
520 vpif_schedule_next_buffer(common);
534 static int vpif_update_std_info(
struct channel_obj *ch)
547 if (config->
hd_sd == 0) {
550 memcpy(std_info, config,
sizeof(*config));
557 memcpy(std_info, config,
sizeof(*config));
564 if (index == vpif_ch_params_count)
567 common->
fmt.fmt.pix.width = std_info->
width;
569 common->
fmt.fmt.pix.height = std_info->
height;
571 common->
fmt.fmt.pix.bytesperline = std_info->
width;
585 static void vpif_calculate_offsets(
struct channel_obj *ch)
603 sizeimage = common->
fmt.fmt.pix.sizeimage;
605 hpitch = common->
fmt.fmt.pix.bytesperline;
606 vpitch = sizeimage / (hpitch * 2);
614 common->
cbtm_off = sizeimage / 2 + hpitch;
634 if (1 == vpifparams->
std_info.frm_fmt)
636 common->
fmt.fmt.pix.bytesperline;
641 common->
fmt.fmt.pix.bytesperline * 2;
644 common->
fmt.fmt.pix.bytesperline;
654 static void vpif_config_format(
struct channel_obj *ch)
666 common->
fmt.fmt.pix.sizeimage
680 static inline enum v4l2_field vpif_get_default_field(
697 static int vpif_check_format(
struct channel_obj *ch,
740 field = vpif_get_default_field(&vpif_params->
iface);
743 field = vpif_get_default_field(&vpif_params->
iface);
747 if (hpitch < vpif_params->std_info.
width) {
752 hpitch = vpif_params->
std_info.width;
757 vpitch = sizeimage / (hpitch * 2);
760 if (vpitch < vpif_params->std_info.
height) {
765 vpitch = vpif_params->
std_info.height;
769 if (!
ALIGN(hpitch, 8)) {
775 hpitch = (((hpitch + 7) / 8) * 8);
786 pixfmt->
width = common->
fmt.fmt.pix.width;
787 pixfmt->
height = common->
fmt.fmt.pix.height;
798 static void vpif_config_addr(
struct channel_obj *ch,
int muxmode)
807 common->
set_addr = ch1_set_videobuf_addr;
808 else if (2 == muxmode)
809 common->
set_addr = ch0_set_videobuf_addr_yc_nmux;
811 common->
set_addr = ch0_set_videobuf_addr;
846 unsigned int res = 0;
865 static int vpif_open(
struct file *filep)
875 ch = video_get_drvdata(vdev);
883 vpif_err(
"unable to allocate memory for file handle object\n");
919 static int vpif_release(
struct file *filep)
937 channel0_intr_enable(0);
939 if ((VPIF_CHANNEL1_VIDEO == ch->
channel_id) ||
942 channel1_intr_enable(0);
1008 vpif_err(
"Failed to get the context\n");
1015 q->
ops = &video_qops;
1021 vpif_err(
"vpif_capture: vb2_queue_init() failed\n");
1043 static int vpif_querybuf(
struct file *file,
void *priv,
1052 if (common->
fmt.type != buf->
type)
1069 static int vpif_qbuf(
struct file *file,
void *priv,
struct v4l2_buffer *buf)
1079 if (common->
fmt.type != tbuf.
type) {
1098 static int vpif_dqbuf(
struct file *file,
void *priv,
struct v4l2_buffer *buf)
1116 static int vpif_streamon(
struct file *file,
void *priv,
1124 struct vpif_params *vpif;
1148 if ((ch->
channel_id == VPIF_CHANNEL0_VIDEO &&
1157 ret = vpif_check_format(ch, &common->
fmt.fmt.pix, 0);
1185 static int vpif_streamoff(
struct file *file,
void *priv,
1216 channel0_intr_enable(0);
1219 channel1_intr_enable(0);
1242 static int vpif_input_to_subdev(
1248 const char *subdev_name;
1253 subdev_name = chan_cfg->
inputs[input_index].subdev_name;
1254 if (subdev_name ==
NULL)
1274 static int vpif_set_input(
1287 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1288 if (sd_index >= 0) {
1289 sd = vpif_obj.
sd[sd_index];
1299 " sub device %s, for input index %d\n",
1300 subdev_info->
name, index);
1315 ch->input_idx =
index;
1333 static int vpif_querystd(
struct file *file,
void *priv,
v4l2_std_id *std_id)
1347 vpif_dbg(1,
debug,
"Failed to query standard for sub devices\n");
1360 static int vpif_g_std(
struct file *file,
void *priv,
v4l2_std_id *
std)
1367 *std = ch->
video.stdid;
1377 static int vpif_s_std(
struct file *file,
void *priv,
v4l2_std_id *std_id)
1387 vpif_err(
"streaming in progress\n");
1391 if ((VPIF_CHANNEL0_VIDEO == ch->
channel_id) ||
1406 ch->
video.stdid = *std_id;
1410 if (vpif_update_std_info(ch)) {
1411 vpif_err(
"Error getting the standard info\n");
1416 vpif_config_format(ch);
1421 vpif_dbg(1,
debug,
"Failed to set standard for sub devices\n");
1433 static int vpif_enum_input(
struct file *file,
void *priv,
1460 static int vpif_g_input(
struct file *file,
void *priv,
unsigned int *index)
1465 *index = ch->input_idx;
1475 static int vpif_s_input(
struct file *file,
void *priv,
unsigned int index)
1490 vpif_err(
"Streaming in progress\n");
1494 if ((VPIF_CHANNEL0_VIDEO == ch->
channel_id) ||
1507 return vpif_set_input(config, ch, index);
1516 static int vpif_enum_fmt_vid_cap(
struct file *file,
void *priv,
1522 if (fmt->
index != 0) {
1546 static int vpif_try_fmt_vid_cap(
struct file *file,
void *priv,
1553 return vpif_check_format(ch, pixfmt, 1);
1563 static int vpif_g_fmt_vid_cap(
struct file *file,
void *priv,
1571 if (common->
fmt.type != fmt->
type)
1585 static int vpif_s_fmt_vid_cap(
struct file *file,
void *priv,
1602 if ((VPIF_CHANNEL0_VIDEO == ch->
channel_id) ||
1618 ret = vpif_check_format(ch, pixfmt, 0);
1633 static int vpif_querycap(
struct file *file,
void *priv,
1642 dev_name(vpif_dev));
1654 static int vpif_g_priority(
struct file *file,
void *priv,
1671 static int vpif_s_priority(
struct file *file,
void *priv,
enum v4l2_priority p)
1685 static int vpif_cropcap(
struct file *file,
void *priv,
1710 vpif_enum_dv_timings(
struct file *file,
void *priv,
1730 vpif_query_dv_timings(
struct file *file,
void *priv,
1749 static int vpif_s_dv_timings(
struct file *file,
void *priv,
1754 struct vpif_params *vpifparams = &ch->
vpifparams;
1774 if (!(timings->
bt.width && timings->
bt.height &&
1775 (timings->
bt.hbackporch ||
1776 timings->
bt.hfrontporch ||
1777 timings->
bt.hsync) &&
1778 timings->
bt.vfrontporch &&
1779 (timings->
bt.vbackporch ||
1780 timings->
bt.vsync))) {
1782 "horizontal back porch, horizontal sync, "
1783 "horizontal front porch, vertical back porch, "
1784 "vertical sync and vertical back porch "
1785 "must be defined\n");
1806 std_info->
l5 = std_info->
vsize/2 -
1808 std_info->
l7 = std_info->
vsize/2 + 1;
1815 "interlaced BT format missing\n");
1830 std_info->
hd_sd = 1;
1831 std_info->
stdid = 0;
1843 static int vpif_g_dv_timings(
struct file *file,
void *priv,
1863 static int vpif_g_chip_ident(
struct file *file,
void *priv,
1875 g_chip_ident, chip);
1878 #ifdef CONFIG_VIDEO_ADV_DEBUG
1888 static int vpif_dbg_g_register(
struct file *file,
void *priv,
1905 static int vpif_dbg_s_register(
struct file *file,
void *priv,
1921 static int vpif_log_status(
struct file *filep,
void *priv)
1931 .vidioc_querycap = vpif_querycap,
1932 .vidioc_g_priority = vpif_g_priority,
1933 .vidioc_s_priority = vpif_s_priority,
1934 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1935 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1936 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1937 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1938 .vidioc_enum_input = vpif_enum_input,
1939 .vidioc_s_input = vpif_s_input,
1940 .vidioc_g_input = vpif_g_input,
1941 .vidioc_reqbufs = vpif_reqbufs,
1942 .vidioc_querybuf = vpif_querybuf,
1943 .vidioc_querystd = vpif_querystd,
1944 .vidioc_s_std = vpif_s_std,
1945 .vidioc_g_std = vpif_g_std,
1946 .vidioc_qbuf = vpif_qbuf,
1947 .vidioc_dqbuf = vpif_dqbuf,
1948 .vidioc_streamon = vpif_streamon,
1949 .vidioc_streamoff = vpif_streamoff,
1950 .vidioc_cropcap = vpif_cropcap,
1951 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1952 .vidioc_query_dv_timings = vpif_query_dv_timings,
1953 .vidioc_s_dv_timings = vpif_s_dv_timings,
1954 .vidioc_g_dv_timings = vpif_g_dv_timings,
1955 .vidioc_g_chip_ident = vpif_g_chip_ident,
1956 #ifdef CONFIG_VIDEO_ADV_DEBUG
1957 .vidioc_g_register = vpif_dbg_g_register,
1958 .vidioc_s_register = vpif_dbg_s_register,
1960 .vidioc_log_status = vpif_log_status,
1967 .release = vpif_release,
1978 .ioctl_ops = &vpif_ioctl_ops,
1986 static int initialize_vpif(
void)
1989 int free_channel_objects_index;
1992 if ((ch0_numbuffers > 0) &&
1995 if ((ch1_numbuffers > 0) &&
2000 if (ch0_bufsize < config_params.
min_bufsize[VPIF_CHANNEL0_VIDEO])
2003 if (ch1_bufsize < config_params.
min_bufsize[VPIF_CHANNEL1_VIDEO])
2007 config_params.
numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
2008 config_params.
numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
2009 if (ch0_numbuffers) {
2013 if (ch1_numbuffers) {
2019 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2023 if (!vpif_obj.
dev[i]) {
2024 free_channel_objects_index =
i;
2026 goto vpif_init_free_channel_objects;
2031 vpif_init_free_channel_objects:
2032 for (j = 0; j < free_channel_objects_index; j++)
2058 vpif_dev = &pdev->
dev;
2060 err = initialize_vpif();
2073 for (i = res->
start; i <= res->
end; i++) {
2075 "VPIF_Capture", (
void *)
2076 (&vpif_obj.
dev[res_idx]->channel_id))) {
2078 for (j = 0; j <
i; j++)
2080 (&vpif_obj.
dev[res_idx]->channel_id));
2087 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2089 ch = vpif_obj.
dev[
i];
2093 for (j = 0; j <
i; j++) {
2094 ch = vpif_obj.
dev[
j];
2102 *vfd = vpif_video_template;
2106 "VPIF_Capture_DRIVER_V%s",
2107 VPIF_CAPTURE_VERSION);
2114 size = resource_size(res);
2118 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2119 ch = vpif_obj.
dev[
j];
2130 config = pdev->
dev.platform_data;
2133 vpif_obj.
sd = kzalloc(
sizeof(
struct v4l2_subdev *) * subdev_count,
2135 if (vpif_obj.
sd ==
NULL) {
2136 vpif_err(
"unable to allocate memory for subdevice pointers\n");
2141 for (i = 0; i < subdev_count; i++) {
2149 if (!vpif_obj.
sd[i]) {
2150 vpif_err(
"Error registering v4l2 subdevice\n");
2151 goto probe_subdev_out;
2157 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2158 ch = vpif_obj.
dev[
j];
2169 err = vpif_set_input(config, ch, 0);
2173 err = video_register_device(ch->
video_dev,
2182 for (k = 0; k <
j; k++) {
2184 ch = vpif_obj.
dev[
k];
2193 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2194 ch = vpif_obj.
dev[
i];
2200 for (i = 0; i < res_idx; i++) {
2202 for (j = res->
start; j <= res->
end; j++)
2203 free_irq(j, (
void *)(&vpif_obj.
dev[i]->channel_id));
2222 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2224 ch = vpif_obj.
dev[
i];
2235 static int vpif_suspend(
struct device *dev)
2242 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2244 ch = vpif_obj.
dev[
i];
2251 channel0_intr_enable(0);
2256 channel1_intr_enable(0);
2268 static int vpif_resume(
struct device *dev)
2274 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2276 ch = vpif_obj.
dev[
i];
2283 channel0_intr_enable(1);
2288 channel1_intr_enable(1);
2297 static const struct dev_pm_ops vpif_dev_pm_ops = {
2299 .resume = vpif_resume,
2302 #define vpif_pm_ops (&vpif_dev_pm_ops)
2304 #define vpif_pm_ops NULL
2309 .name =
"vpif_capture",
2313 .probe = vpif_probe,
2314 .remove = vpif_remove,
2324 static __init int vpif_init(
void)
2336 static void vpif_cleanup(
void)
2345 for (irq_num = res->
start; irq_num <= res->
end; irq_num++)
2347 (
void *)(&vpif_obj.
dev[i]->channel_id));
2354 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)