17 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/errno.h>
25 #include <linux/string.h>
26 #include <linux/videodev2.h>
27 #include <linux/wait.h>
28 #include <linux/time.h>
29 #include <linux/i2c.h>
32 #include <linux/slab.h>
49 #define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
51 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
52 #define vpif_dbg(level, debug, fmt, arg...) \
53 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
56 static u32 ch2_numbuffers = 3;
57 static u32 ch3_numbuffers = 3;
58 static u32 ch2_bufsize = 1920 * 1080 * 2;
59 static u32 ch3_bufsize = 720 * 576 * 2;
70 MODULE_PARM_DESC(ch2_bufsize,
"Channel2 buffer size (default:1920 x 1080 x 2)");
71 MODULE_PARM_DESC(ch3_bufsize,
"Channel3 buffer size (default:720 x 576 x 2)");
77 .min_bufsize[0] = 720 * 480 * 2,
78 .min_bufsize[1] = 720 * 480 * 2,
79 .channel_bufsize[0] = 1920 * 1080 * 2,
80 .channel_bufsize[1] = 720 * 576 * 2,
84 static struct device *vpif_dev;
85 static void vpif_calculate_offsets(
struct channel_obj *ch);
86 static void vpif_config_addr(
struct channel_obj *ch,
int muxmode);
93 static int vpif_buffer_prepare(
struct vb2_buffer *vb)
103 vb2_set_plane_payload(vb, 0, common->
fmt.fmt.pix.sizeimage);
105 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
108 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
121 vpif_err(
"buffer offset not aligned to 8 bytes\n");
128 static int vpif_buffer_queue_setup(
struct vb2_queue *vq,
130 unsigned int *nbuffers,
unsigned int *nplanes,
131 unsigned int sizes[],
void *alloc_ctxs[])
133 struct vpif_fh *fh = vb2_get_drv_priv(vq);
145 if (ch->
vpifparams.std_info.ycmux_mode == 0) {
147 while (size * *nbuffers >
153 while (size * *nbuffers >
158 size = common->
fmt.fmt.pix.sizeimage;
173 static void vpif_buffer_queue(
struct vb2_buffer *vb)
191 static void vpif_buf_cleanup(
struct vb2_buffer *vb)
204 list_del_init(&buf->
list);
205 spin_unlock_irqrestore(&common->
irqlock, flags);
208 static void vpif_wait_prepare(
struct vb2_queue *vq)
210 struct vpif_fh *fh = vb2_get_drv_priv(vq);
218 static void vpif_wait_finish(
struct vb2_queue *vq)
220 struct vpif_fh *fh = vb2_get_drv_priv(vq);
228 static int vpif_buffer_init(
struct vb2_buffer *vb)
233 INIT_LIST_HEAD(&buf->
list);
240 static int vpif_start_streaming(
struct vb2_queue *vq,
unsigned int count)
244 struct vpif_fh *fh = vb2_get_drv_priv(vq);
248 unsigned long addr = 0;
253 vpif_err(
"buffer queue is empty\n");
269 addr = vb2_dma_contig_plane_dma_addr(&common->
cur_frm->vb, 0);
271 vpif_calculate_offsets(ch);
278 vpif_err(
"conflict in field format and std format\n");
298 vpif_config_addr(ch, ret);
309 channel2_intr_enable(1);
312 channel2_clipping_enable(1);
318 channel3_intr_enable(1);
321 channel3_clipping_enable(1);
328 static int vpif_stop_streaming(
struct vb2_queue *vq)
330 struct vpif_fh *fh = vb2_get_drv_priv(vq);
334 if (!vb2_is_streaming(vq))
340 while (!list_empty(&common->
dma_queue)) {
350 static struct vb2_ops video_qops = {
351 .queue_setup = vpif_buffer_queue_setup,
352 .wait_prepare = vpif_wait_prepare,
353 .wait_finish = vpif_wait_finish,
354 .buf_init = vpif_buffer_init,
355 .buf_prepare = vpif_buffer_prepare,
356 .start_streaming = vpif_start_streaming,
357 .stop_streaming = vpif_stop_streaming,
358 .buf_cleanup = vpif_buf_cleanup,
359 .buf_queue = vpif_buffer_queue,
362 static void process_progressive_mode(
struct common_obj *common)
364 unsigned long addr = 0;
375 addr = vb2_dma_contig_plane_dma_addr(&common->
next_frm->vb, 0);
382 static void process_interlaced_mode(
int fid,
struct common_obj *common)
400 }
else if (1 == fid) {
409 process_progressive_mode(common);
427 channel_id = *(
int *)(dev_id);
428 if (!vpif_intr_status(channel_id + 2))
431 ch = dev->
dev[channel_id];
444 if (!channel_first_int[
i][channel_id]) {
455 channel_first_int[
i][channel_id] = 0;
456 process_progressive_mode(common);
461 if (channel_first_int[
i][channel_id]) {
462 channel_first_int[
i][channel_id] = 0;
479 process_interlaced_mode(fid, common);
486 static int vpif_update_std_info(
struct channel_obj *ch)
497 if (config->
hd_sd == 0) {
500 memcpy(std_info, config,
sizeof(*config));
506 if (i == vpif_ch_params_count) {
514 static int vpif_update_resolution(
struct channel_obj *ch)
525 if (vpif_update_std_info(ch))
529 common->
fmt.fmt.pix.width = std_info->
width;
530 common->
fmt.fmt.pix.height = std_info->
height;
531 vpif_dbg(1,
debug,
"Pixel details: Width = %d,Height = %d\n",
532 common->
fmt.fmt.pix.width, common->
fmt.fmt.pix.height);
545 static void vpif_calculate_offsets(
struct channel_obj *ch)
562 sizeimage = common->
fmt.fmt.pix.sizeimage;
564 hpitch = common->
fmt.fmt.pix.bytesperline;
565 vpitch = sizeimage / (hpitch * 2);
571 common->
cbtm_off = sizeimage / 2 + hpitch;
593 common->
fmt.fmt.pix.bytesperline;
598 common->
fmt.fmt.pix.bytesperline * 2;
601 common->
fmt.fmt.pix.bytesperline;
607 static void vpif_config_format(
struct channel_obj *ch)
617 common->
fmt.fmt.pix.sizeimage =
623 static int vpif_check_format(
struct channel_obj *ch,
631 goto invalid_fmt_exit;
634 goto invalid_fmt_exit;
637 goto invalid_pitch_exit;
641 if (vpif_update_resolution(ch))
645 vpitch = sizeimage / (hpitch * 2);
648 if ((hpitch < ch->vpifparams.
std_info.width) ||
650 goto invalid_pitch_exit;
654 vpif_err(
"invalid pitch alignment\n");
657 pixfmt->
width = common->
fmt.fmt.pix.width;
658 pixfmt->
height = common->
fmt.fmt.pix.height;
671 static void vpif_config_addr(
struct channel_obj *ch,
int muxmode)
676 common->
set_addr = ch3_set_videobuf_addr;
679 common->
set_addr = ch2_set_videobuf_addr_yc_nmux;
681 common->
set_addr = ch2_set_videobuf_addr;
712 unsigned int res = 0;
727 static int vpif_open(
struct file *filep)
737 vpif_err(
"unable to allocate memory for file handle object\n");
771 static int vpif_release(
struct file *filep)
785 channel2_intr_enable(0);
790 channel3_intr_enable(0);
840 static int vpif_enum_fmt_vid_out(
struct file *file,
void *priv,
843 if (fmt->
index != 0) {
856 static int vpif_g_fmt_vid_out(
struct file *file,
void *priv,
864 if (common->
fmt.type != fmt->
type)
867 if (vpif_update_resolution(ch))
873 static int vpif_s_fmt_vid_out(
struct file *file,
void *priv,
903 ret = vpif_check_format(ch, pixfmt);
908 common->
fmt.fmt.pix = *pixfmt;
914 static int vpif_try_fmt_vid_out(
struct file *file,
void *priv,
923 ret = vpif_check_format(ch, pixfmt);
925 *pixfmt = common->
fmt.fmt.pix;
932 static int vpif_reqbufs(
struct file *file,
void *priv,
960 if (common->
fmt.type != reqbuf->
type || !vpif_dev)
969 field = common->
fmt.fmt.pix.field;
976 vpif_err(
"Failed to get the context\n");
983 q->
ops = &video_qops;
989 vpif_err(
"vpif_display: vb2_queue_init() failed\n");
1004 static int vpif_querybuf(
struct file *file,
void *priv,
1011 if (common->
fmt.type != tbuf->
type)
1017 static int vpif_qbuf(
struct file *file,
void *priv,
struct v4l2_buffer *buf)
1032 if (common->
fmt.type != buf->
type)
1043 static int vpif_s_std(
struct file *file,
void *priv,
v4l2_std_id *std_id)
1054 vpif_err(
"streaming in progress\n");
1059 ch->
video.stdid = *std_id;
1062 if (vpif_update_resolution(ch))
1068 vpif_err(
"invalid std for this size\n");
1072 common->
fmt.fmt.pix.bytesperline = common->
fmt.fmt.pix.width;
1074 vpif_config_format(ch);
1077 s_std_output, *std_id);
1079 vpif_err(
"Failed to set output standard\n");
1086 vpif_err(
"Failed to set standard for sub devices\n");
1090 static int vpif_g_std(
struct file *file,
void *priv,
v4l2_std_id *
std)
1095 *std = ch->
video.stdid;
1099 static int vpif_dqbuf(
struct file *file,
void *priv,
struct v4l2_buffer *
p)
1109 static int vpif_streamon(
struct file *file,
void *priv,
1119 vpif_err(
"buffer type not supported\n");
1139 vpif_err(
"other channel is using\n");
1143 ret = vpif_check_format(ch, &common->
fmt.fmt.pix);
1157 static int vpif_streamoff(
struct file *file,
void *priv,
1167 vpif_err(
"buffer type not supported\n");
1184 if (vpif_config_data->
1186 channel2_clipping_enable(0);
1188 channel2_intr_enable(0);
1192 if (vpif_config_data->
1194 channel3_clipping_enable(0);
1196 channel3_intr_enable(0);
1204 static int vpif_cropcap(
struct file *file,
void *priv,
1221 static int vpif_enum_output(
struct file *file,
void *fh,
1227 struct vpif_fh *vpif_handler = fh;
1255 const char *subdev_name;
1264 if (subdev_name ==
NULL)
1295 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
1296 if (sd_index >= 0) {
1297 sd = vpif_obj.
sd[sd_index];
1298 subdev_info = &vpif_cfg->
subdevinfo[sd_index];
1306 vpif_err(
"Failed to set output\n");
1319 static int vpif_s_output(
struct file *file,
void *priv,
unsigned int i)
1333 vpif_err(
"Streaming in progress\n");
1337 return vpif_set_output(config, ch, i);
1340 static int vpif_g_output(
struct file *file,
void *priv,
unsigned int *i)
1350 static int vpif_g_priority(
struct file *file,
void *priv,
enum v4l2_priority *
p)
1360 static int vpif_s_priority(
struct file *file,
void *priv,
enum v4l2_priority p)
1375 vpif_enum_dv_timings(
struct file *file,
void *priv,
1394 static int vpif_s_dv_timings(
struct file *file,
void *priv,
1419 if (!(timings->
bt.width && timings->
bt.height &&
1420 (timings->
bt.hbackporch ||
1421 timings->
bt.hfrontporch ||
1422 timings->
bt.hsync) &&
1423 timings->
bt.vfrontporch &&
1424 (timings->
bt.vbackporch ||
1425 timings->
bt.vsync))) {
1427 "horizontal back porch, horizontal sync, "
1428 "horizontal front porch, vertical back porch, "
1429 "vertical sync and vertical back porch "
1430 "must be defined\n");
1451 std_info->
l5 = std_info->
vsize/2 -
1453 std_info->
l7 = std_info->
vsize/2 + 1;
1460 "interlaced BT format missing\n");
1468 strncpy(std_info->
name,
"Custom timings BT656/1120",
1476 std_info->
hd_sd = 1;
1477 std_info->
stdid = 0;
1489 static int vpif_g_dv_timings(
struct file *file,
void *priv,
1509 static int vpif_g_chip_ident(
struct file *file,
void *priv,
1521 g_chip_ident, chip);
1524 #ifdef CONFIG_VIDEO_ADV_DEBUG
1534 static int vpif_dbg_g_register(
struct file *file,
void *priv,
1551 static int vpif_dbg_s_register(
struct file *file,
void *priv,
1567 static int vpif_log_status(
struct file *filep,
void *priv)
1577 .vidioc_querycap = vpif_querycap,
1578 .vidioc_g_priority = vpif_g_priority,
1579 .vidioc_s_priority = vpif_s_priority,
1580 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1581 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1582 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1583 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1584 .vidioc_reqbufs = vpif_reqbufs,
1585 .vidioc_querybuf = vpif_querybuf,
1586 .vidioc_qbuf = vpif_qbuf,
1587 .vidioc_dqbuf = vpif_dqbuf,
1588 .vidioc_streamon = vpif_streamon,
1589 .vidioc_streamoff = vpif_streamoff,
1590 .vidioc_s_std = vpif_s_std,
1591 .vidioc_g_std = vpif_g_std,
1592 .vidioc_enum_output = vpif_enum_output,
1593 .vidioc_s_output = vpif_s_output,
1594 .vidioc_g_output = vpif_g_output,
1595 .vidioc_cropcap = vpif_cropcap,
1596 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1597 .vidioc_s_dv_timings = vpif_s_dv_timings,
1598 .vidioc_g_dv_timings = vpif_g_dv_timings,
1599 .vidioc_g_chip_ident = vpif_g_chip_ident,
1600 #ifdef CONFIG_VIDEO_ADV_DEBUG
1601 .vidioc_g_register = vpif_dbg_g_register,
1602 .vidioc_s_register = vpif_dbg_s_register,
1604 .vidioc_log_status = vpif_log_status,
1610 .release = vpif_release,
1619 .ioctl_ops = &vpif_ioctl_ops,
1623 static int initialize_vpif(
void)
1625 int free_channel_objects_index;
1626 int free_buffer_channel_index;
1627 int free_buffer_index;
1631 if ((ch2_numbuffers > 0) &&
1634 if ((ch3_numbuffers > 0) &&
1649 if (ch2_numbuffers) {
1655 if (ch3_numbuffers) {
1665 if (!vpif_obj.
dev[i]) {
1666 free_channel_objects_index =
i;
1668 goto vpif_init_free_channel_objects;
1674 free_buffer_index = config_params.
numbuffers[i - 1];
1678 vpif_init_free_channel_objects:
1679 for (j = 0; j < free_channel_objects_index; j++)
1692 int i, j = 0,
k, err = 0;
1702 vpif_dev = &pdev->
dev;
1703 err = initialize_vpif();
1717 for (i = res->
start; i <= res->
end; i++) {
1719 "VPIF_Display", (
void *)
1720 (&vpif_obj.
dev[res_idx]->channel_id))) {
1722 for (j = 0; j <
i; j++)
1724 (&vpif_obj.
dev[res_idx]->channel_id));
1733 ch = vpif_obj.
dev[
i];
1738 for (j = 0; j <
i; j++) {
1739 ch = vpif_obj.
dev[
j];
1747 *vfd = vpif_video_template;
1752 "VPIF_Display_DRIVER_V%s",
1761 size = resource_size(res);
1766 ch = vpif_obj.
dev[
j];
1778 config = pdev->
dev.platform_data;
1781 vpif_obj.
sd = kzalloc(
sizeof(
struct v4l2_subdev *) * subdev_count,
1783 if (vpif_obj.
sd ==
NULL) {
1784 vpif_err(
"unable to allocate memory for subdevice pointers\n");
1789 for (i = 0; i < subdev_count; i++) {
1794 if (!vpif_obj.
sd[i]) {
1795 vpif_err(
"Error registering v4l2 subdevice\n");
1796 goto probe_subdev_out;
1800 vpif_obj.
sd[
i]->grp_id = 1 <<
i;
1804 ch = vpif_obj.
dev[
j];
1825 ch->
sd = vpif_obj.
sd[0];
1843 err = vpif_set_output(config, ch, 0);
1851 err = video_register_device(ch->video_dev,
1858 " VPIF display driver initialized\n");
1862 for (
k = 0;
k <
j;
k++) {
1863 ch = vpif_obj.
dev[
k];
1866 ch->video_dev =
NULL;
1872 ch = vpif_obj.
dev[
i];
1878 vpif_err(
"VPIF IRQ request failed\n");
1879 for (i = 0; i < res_idx; i++) {
1881 for (j = res->
start; j <= res->
end; j++)
1882 free_irq(j, (
void *)(&vpif_obj.
dev[i]->channel_id));
1901 ch = vpif_obj.
dev[
i];
1912 static int vpif_suspend(
struct device *dev)
1920 ch = vpif_obj.
dev[
i];
1927 channel2_intr_enable(0);
1932 channel3_intr_enable(0);
1941 static int vpif_resume(
struct device *dev)
1950 ch = vpif_obj.
dev[
i];
1957 channel2_intr_enable(1);
1962 channel3_intr_enable(1);
1973 .resume = vpif_resume,
1976 #define vpif_pm_ops (&vpif_pm)
1978 #define vpif_pm_ops NULL
1983 .name =
"vpif_display",
1987 .probe = vpif_probe,
1988 .remove = vpif_remove,
1991 static __init int vpif_init(
void)
2001 static void vpif_cleanup(
void)
2011 for (irq_num = res->
start; irq_num <= res->
end; irq_num++)
2013 (
void *)(&vpif_obj.
dev[i]->channel_id));