17 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/i2c.h>
25 #include <linux/slab.h>
31 #define DRV_NAME "fsl_viu"
32 #define VIU_VERSION "0.5.1"
34 #define BUFFER_TIMEOUT msecs_to_jiffies(500)
36 #define VIU_VID_MEM_LIMIT 4
39 #define VIU_VIDEO_DECODER_ADDR 0x25
59 .default_value = 0x10,
84 static int info_level;
86 #define dprintk(level, fmt, arg...) \
88 if (level <= info_level) \
89 printk(KERN_DEBUG "viu: " fmt , ## arg); \
104 .name =
"RGB-16 (5/B-6/G-5/R)",
109 .name =
"RGB-32 (A-R-G-B)",
259 #define norm_maxw() 720
260 #define norm_maxh() 576
262 #define INT_ALL_STATUS (INT_FIELD_STATUS | INT_VSYNC_STATUS | \
263 INT_HSYNC_STATUS | INT_VSTART_STATUS | \
264 INT_DMA_END_STATUS | INT_ERROR_STATUS)
266 #define NUM_FORMATS ARRAY_SIZE(formats)
279 dprintk(0,
"unknown pixelformat:'%4.4s'\n", (
char *)&fourcc);
304 if (status_cfg & 0x3f0000)
327 static int restart_video_queue(
struct viu_dmaqueue *vidq)
331 dprintk(1,
"%s vidq=0x%08lx\n", __func__, (
unsigned long)vidq);
332 if (!list_empty(&vidq->
active)) {
334 dprintk(2,
"restart_queue [%p/%d]: restart dma\n",
351 if (list_empty(&vidq->
queued))
358 dprintk(1,
"Restarting video dma\n");
364 dprintk(2,
"[%p/%d] restart_queue - first active\n",
367 }
else if (prev->
vb.width == buf->
vb.width &&
368 prev->
vb.height == buf->
vb.height &&
373 dprintk(2,
"[%p/%d] restart_queue - move to active\n",
382 static void viu_vid_timeout(
unsigned long data)
388 while (!list_empty(&vidq->
active)) {
393 dprintk(1,
"viu/0: [%p/%d] timeout\n", buf, buf->
vb.i);
396 restart_video_queue(vidq);
414 dprintk(1,
"%s, count=%d, size=%d\n", __func__, *count, *size);
428 vaddr = vq->
int_ops->vaddr(vb);
444 dprintk(1,
"buffer_activate [%p/%d]: dma addr 0x%lx\n",
445 buf, buf->
vb.i, (
unsigned long)
reg_val.field_base_addr);
449 bpp = buf->
fmt->depth >> 3;
460 dprintk(0,
"doesn't support color depth(%d)\n",
466 reg_val.picture_count = (buf->
vb.height / 2) << 16 |
499 if (buf->
vb.baddr != 0 && buf->
vb.bsize < buf->
vb.size)
502 if (buf->
fmt != fh->
fmt ||
505 buf->
vb.field != field) {
539 if (!list_empty(&vidq->
queued)) {
540 dprintk(1,
"adding vb queue=0x%08lx\n",
541 (
unsigned long)&buf->
vb.queue);
542 dprintk(1,
"vidq pointer 0x%p, queued 0x%p\n",
544 dprintk(1,
"dev %p, queued: self %p, next %p, head %p\n",
549 dprintk(2,
"[%p/%d] buffer_queue - append to queued\n",
551 }
else if (list_empty(&vidq->
active)) {
552 dprintk(1,
"adding vb active=0x%08lx\n",
553 (
unsigned long)&buf->
vb.queue);
557 dprintk(2,
"[%p/%d] buffer_queue - first active\n",
562 dprintk(1,
"adding vb queue2=0x%08lx\n",
563 (
unsigned long)&buf->
vb.queue);
565 if (prev->
vb.width == buf->
vb.width &&
566 prev->
vb.height == buf->
vb.height &&
570 dprintk(2,
"[%p/%d] buffer_queue - append to active\n",
575 dprintk(2,
"[%p/%d] buffer_queue - first queued\n",
593 .buf_setup = buffer_setup,
594 .buf_prepare = buffer_prepare,
595 .buf_queue = buffer_queue,
596 .buf_release = buffer_release,
602 static int vidioc_querycap(
struct file *
file,
void *
priv,
614 static int vidioc_enum_fmt(
struct file *
file,
void *
priv,
627 static int vidioc_g_fmt_cap(
struct file *file,
void *priv,
635 f->
fmt.
pix.pixelformat = fh->
fmt->pixelformat;
637 (f->
fmt.
pix.width * fh->
fmt->depth) >> 3;
642 static int vidioc_try_fmt_cap(
struct file *file,
void *priv,
647 unsigned int maxw, maxh;
651 dprintk(1,
"Fourcc format (0x%08x) invalid.",
661 dprintk(1,
"Field type invalid.\n");
669 if (f->
fmt.
pix.height < 32)
671 if (f->
fmt.
pix.height > maxh)
673 if (f->
fmt.
pix.width < 48)
675 if (f->
fmt.
pix.width > maxw)
677 f->
fmt.
pix.width &= ~0x03;
684 static int vidioc_s_fmt_cap(
struct file *file,
void *priv,
690 ret = vidioc_try_fmt_cap(file, fh, f);
700 dprintk(1,
"set to pixelformat '%4.6s'\n", (
char *)&fh->
fmt->name);
704 static int vidioc_g_fmt_overlay(
struct file *file,
void *priv,
722 if (win->
w.width < 48 || win->
w.height < 32)
730 field = (win->
w.height > maxh/2)
746 if (win->
w.width > maxw)
748 if (win->
w.height > maxh)
749 win->
w.height = maxh;
755 struct viu_reg *vr = viu_reg;
762 static int viu_setup_preview(
struct viu_dev *dev,
struct viu_fh *fh)
766 dprintk(1,
"%s %dx%d %s\n", __func__,
767 fh->
win.w.width, fh->
win.w.height, dev->
ovfmt->name);
772 reg_val.picture_count = (fh->
win.w.height / 2) << 16 |
776 bpp = dev->
ovfmt->depth / 8;
787 dprintk(0,
"device doesn't support color depth(%d)\n",
804 static int vidioc_s_fmt_overlay(
struct file *file,
void *priv,
819 viu_setup_preview(dev, fh);
824 static int vidioc_try_fmt_overlay(
struct file *file,
void *priv,
830 static int vidioc_overlay(
struct file *file,
void *priv,
unsigned int on)
881 if (dev->
ovbuf.fmt.bytesperline == 0) {
882 dev->
ovbuf.fmt.bytesperline =
888 static int vidioc_reqbufs(
struct file *file,
void *priv,
896 static int vidioc_querybuf(
struct file *file,
void *priv,
904 static int vidioc_qbuf(
struct file *file,
void *priv,
struct v4l2_buffer *
p)
911 static int vidioc_dqbuf(
struct file *file,
void *priv,
struct v4l2_buffer *
p)
919 static int vidioc_streamon(
struct file *file,
void *priv,
enum v4l2_buf_type i)
937 static int vidioc_streamoff(
struct file *file,
void *priv,
enum v4l2_buf_type i)
951 #define decoder_call(viu, o, f, args...) \
952 v4l2_subdev_call(viu->decoder, o, f, ##args)
954 static int vidioc_querystd(
struct file *file,
void *priv,
v4l2_std_id *std_id)
962 static int vidioc_s_std(
struct file *file,
void *priv,
v4l2_std_id *
id)
971 static int vidioc_g_std(
struct file *file,
void *priv,
v4l2_std_id *std_id)
975 *std_id = fh->
dev->std;
980 static int vidioc_enum_input(
struct file *file,
void *priv,
989 inp->
std = fh->
dev->vdev->tvnorms;
994 static int vidioc_g_input(
struct file *file,
void *priv,
unsigned int *i)
1000 static int vidioc_s_input(
struct file *file,
void *priv,
unsigned int i)
1012 static int vidioc_queryctrl(
struct file *file,
void *priv,
1017 for (i = 0; i <
ARRAY_SIZE(viu_qctrl); i++) {
1018 if (qc->
id && qc->
id == viu_qctrl[i].
id) {
1019 memcpy(qc, &(viu_qctrl[i]),
sizeof(*qc));
1026 static int vidioc_g_ctrl(
struct file *file,
void *priv,
1031 for (i = 0; i <
ARRAY_SIZE(viu_qctrl); i++) {
1032 if (ctrl->
id == viu_qctrl[i].
id) {
1033 ctrl->
value = qctl_regs[
i];
1039 static int vidioc_s_ctrl(
struct file *file,
void *priv,
1044 for (i = 0; i <
ARRAY_SIZE(viu_qctrl); i++) {
1045 if (ctrl->
id == viu_qctrl[i].
id) {
1049 qctl_regs[
i] = ctrl->
value;
1063 if (!list_empty(&vidq->
active)) {
1066 dprintk(1,
"start another queued buffer: 0x%p\n", buf);
1068 }
else if (!list_empty(&vidq->
queued)) {
1073 dprintk(1,
"start another queued buffer: 0x%p\n", buf);
1082 struct viu_reg *vr = viu_reg;
1090 dprintk(1,
"status reg: 0x%08x, field base: 0x%08x\n",
1112 (status & 0xffc0ffff) |
1117 (status & 0xffc0ffff) |
1124 static void viu_capture_intr(
struct viu_dev *dev,
u32 status)
1136 if (status & INT_DMA_END_STATUS) {
1138 if (((field_num == 0) && (dev->
field == 0)) ||
1139 (field_num && (dev->
field == 1)))
1143 if (status & INT_FIELD_STATUS) {
1144 dprintk(1,
"irq: field %d, done %d\n",
1145 !!field_num, dma_done);
1147 if (field_num == 0) {
1149 dprintk(1,
"activate first buf\n");
1157 if (!list_empty(&vidq->
active)) {
1160 if (field_num && need_two) {
1162 dprintk(1,
"field 1, 0x%lx, dev field %d\n",
1163 (
unsigned long)addr, dev->
field);
1168 (status & 0xffc0ffff) |
1175 if (dma_done && field_num && (dev->
field == 2)) {
1179 dprintk(1,
"viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1184 if (waitqueue_active(&buf->
vb.done)) {
1188 buf->
vb.field_count++;
1206 dev->
irqs.error_irq++;
1209 dprintk(1,
"Err: error(%d), times:%d!\n",
1210 error >> 4, dev->
irqs.error_irq);
1213 (status & 0xffc0ffff) | INT_ERROR_STATUS);
1216 if (status & INT_DMA_END_STATUS) {
1217 dev->
irqs.dma_end_irq++;
1219 dprintk(2,
"VIU DMA end interrupt times: %d\n",
1220 dev->
irqs.dma_end_irq);
1224 dev->
irqs.hsync_irq++;
1226 if (status & INT_FIELD_STATUS) {
1227 dev->
irqs.field_irq++;
1228 dprintk(2,
"VIU field interrupt times: %d\n",
1229 dev->
irqs.field_irq);
1233 dev->
irqs.vstart_irq++;
1236 dev->
irqs.vsync_irq++;
1237 dprintk(2,
"VIU vsync interrupt times: %d\n",
1238 dev->
irqs.vsync_irq);
1247 viu_overlay_intr(dev, status);
1252 viu_capture_intr(dev, status);
1259 static int viu_open(
struct file *file)
1262 struct viu_dev *dev = video_get_drvdata(vdev);
1269 dprintk(1,
"viu: open (minor=%d)\n", minor);
1272 if (dev->
users > 1) {
1279 dprintk(1,
"open minor=%d type=%s users=%d\n", minor,
1309 dprintk(1,
"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1310 (
unsigned long)fh, (
unsigned long)dev,
1311 (
unsigned long)&dev->
vidq);
1312 dprintk(1,
"Open: list_empty queued=%d\n",
1313 list_empty(&dev->vidq.queued));
1314 dprintk(1,
"Open: list_empty active=%d\n",
1315 list_empty(&dev->vidq.active));
1330 dev->dev, &fh->vbq_lock,
1338 static ssize_t viu_read(
struct file *file,
char __user *data,
size_t count,
1377 static int viu_release(
struct file *file)
1392 dprintk(1,
"close (minor=%d, users=%d)\n",
1411 static int viu_mmap(
struct file *file,
struct vm_area_struct *vma)
1417 dprintk(1,
"mmap called, vma=0x%08lx\n", (
unsigned long)vma);
1424 dprintk(1,
"vma start=0x%08lx, size=%ld, ret=%d\n",
1435 .release = viu_release,
1444 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1445 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_cap,
1446 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_cap,
1447 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_cap,
1448 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt,
1449 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_overlay,
1450 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_overlay,
1451 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_overlay,
1476 .ioctl_ops = &viu_ioctl_ops,
1494 dev_err(&op->
dev,
"Can't parse device node resource\n");
1500 dev_err(&op->
dev,
"Error while mapping the irq\n");
1507 dev_err(&op->
dev,
"Error while requesting mem region\n");
1515 dev_err(&op->
dev,
"Can't map register set\n");
1523 dev_err(&op->
dev,
"Can't allocate private structure\n");
1528 viu_dev->
vr = viu_regs;
1529 viu_dev->
irq = viu_irq;
1533 INIT_LIST_HEAD(&viu_dev->
vidq.active);
1534 INIT_LIST_HEAD(&viu_dev->
vidq.queued);
1537 sizeof(viu_dev->
v4l2_dev.name),
"%s",
"VIU");
1540 dev_err(&op->
dev,
"v4l2_device_register() failed: %d\n", ret);
1548 viu_dev->
vidq.timeout.function = viu_vid_timeout;
1549 viu_dev->
vidq.timeout.data = (
unsigned long)viu_dev;
1560 memcpy(vdev, &viu_template,
sizeof(viu_template));
1564 viu_dev->
vdev = vdev;
1568 viu_dev->
vdev->lock = &viu_dev->
lock;
1571 video_set_drvdata(viu_dev->
vdev, viu_dev);
1583 if (IS_ERR(viu_dev->
clk)) {
1584 dev_err(&op->
dev,
"failed to find the clock module!\n");
1595 if (
request_irq(viu_dev->
irq, viu_intr, 0,
"viu", (
void *)viu_dev)) {
1596 dev_err(&op->
dev,
"Request VIU IRQ failed.\n");
1603 dev_info(&op->
dev,
"Freescale VIU Video Capture Board\n");
1623 struct viu_dev *dev =
container_of(v4l2_dev,
struct viu_dev, v4l2_dev);
1644 struct viu_dev *dev =
container_of(v4l2_dev,
struct viu_dev, v4l2_dev);
1653 struct viu_dev *dev =
container_of(v4l2_dev,
struct viu_dev, v4l2_dev);
1665 .compatible =
"fsl,mpc5121-viu",
1672 .probe = viu_of_probe,
1675 .suspend = viu_suspend,
1676 .resume = viu_resume,
1681 .of_match_table = mpc512x_viu_of_match,