2 * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
4 * Freescale VIU video driver
7 * Porting to 2.6.35 by DENX Software Engineering,
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
17 #include <linux/module.h>
18 #include <linux/clk.h>
19 #include <linux/kernel.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
24 #include <linux/of_platform.h>
25 #include <linux/slab.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/videobuf-dma-contig.h>
31 #define DRV_NAME "fsl_viu"
32 #define VIU_VERSION "0.5.1"
34 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
36 #define VIU_VID_MEM_LIMIT 4 /* Video memory limit, in Mb */
38 /* I2C address of video decoder chip is 0x4A */
39 #define VIU_VIDEO_DECODER_ADDR 0x25
41 /* supported controls */
42 static struct v4l2_queryctrl viu_qctrl[] = {
44 .id = V4L2_CID_BRIGHTNESS,
45 .type = V4L2_CTRL_TYPE_INTEGER,
53 .id = V4L2_CID_CONTRAST,
54 .type = V4L2_CTRL_TYPE_INTEGER,
59 .default_value = 0x10,
62 .id = V4L2_CID_SATURATION,
63 .type = V4L2_CTRL_TYPE_INTEGER,
72 .type = V4L2_CTRL_TYPE_INTEGER,
82 static int qctl_regs[ARRAY_SIZE(viu_qctrl)];
84 static int info_level;
86 #define dprintk(level, fmt, arg...) \
88 if (level <= info_level) \
89 printk(KERN_DEBUG "viu: " fmt , ## arg); \
97 u32 fourcc; /* v4l2 format id */
102 static struct viu_fmt formats[] = {
104 .name = "RGB-16 (5/B-6/G-5/R)",
105 .fourcc = V4L2_PIX_FMT_RGB565,
106 .pixelformat = V4L2_PIX_FMT_RGB565,
109 .name = "RGB-32 (A-R-G-B)",
110 .fourcc = V4L2_PIX_FMT_RGB32,
111 .pixelformat = V4L2_PIX_FMT_RGB32,
119 /* buffer for one video frame */
121 /* common v4l buffer stuff -- must be first */
122 struct videobuf_buffer vb;
126 struct viu_dmaqueue {
128 struct list_head active;
129 struct list_head queued;
130 struct timer_list timeout;
153 } __attribute__ ((packed));
156 struct v4l2_device v4l2_dev;
162 /* various device info */
163 struct video_device *vdev;
164 struct viu_dmaqueue vidq;
165 enum v4l2_field capfield;
170 /* Hardware register area */
173 /* Interrupt vector */
175 struct viu_status irqs;
178 struct v4l2_framebuffer ovbuf;
179 struct viu_fmt *ovfmt;
180 unsigned int ovenable;
181 enum v4l2_field ovfield;
184 struct v4l2_rect crop_current;
190 struct v4l2_subdev *decoder;
199 struct videobuf_queue vb_vidq;
200 spinlock_t vbq_lock; /* spinlock for the videobuf queue */
203 struct v4l2_window win;
204 struct v4l2_clip clips[1];
208 int width, height, sizeimage;
209 enum v4l2_buf_type type;
212 static struct viu_reg reg_val;
215 * Macro definitions of VIU registers
218 /* STATUS_CONFIG register */
222 ERR_MASK = 0x0f << 4, /* Error code mask */
223 ERR_NO = 0x00, /* No error */
224 ERR_DMA_V = 0x01 << 4, /* DMA in vertical active */
225 ERR_DMA_VB = 0x02 << 4, /* DMA in vertical blanking */
226 ERR_LINE_TOO_LONG = 0x04 << 4, /* Line too long */
227 ERR_TOO_MANG_LINES = 0x05 << 4, /* Too many lines in field */
228 ERR_LINE_TOO_SHORT = 0x06 << 4, /* Line too short */
229 ERR_NOT_ENOUGH_LINE = 0x07 << 4, /* Not enough lines in field */
230 ERR_FIFO_OVERFLOW = 0x08 << 4, /* FIFO overflow */
231 ERR_FIFO_UNDERFLOW = 0x09 << 4, /* FIFO underflow */
232 ERR_1bit_ECC = 0x0a << 4, /* One bit ECC error */
233 ERR_MORE_ECC = 0x0b << 4, /* Two/more bits ECC error */
235 INT_FIELD_EN = 0x01 << 8, /* Enable field interrupt */
236 INT_VSYNC_EN = 0x01 << 9, /* Enable vsync interrupt */
237 INT_HSYNC_EN = 0x01 << 10, /* Enable hsync interrupt */
238 INT_VSTART_EN = 0x01 << 11, /* Enable vstart interrupt */
239 INT_DMA_END_EN = 0x01 << 12, /* Enable DMA end interrupt */
240 INT_ERROR_EN = 0x01 << 13, /* Enable error interrupt */
241 INT_ECC_EN = 0x01 << 14, /* Enable ECC interrupt */
243 INT_FIELD_STATUS = 0x01 << 16, /* field interrupt status */
244 INT_VSYNC_STATUS = 0x01 << 17, /* vsync interrupt status */
245 INT_HSYNC_STATUS = 0x01 << 18, /* hsync interrupt status */
246 INT_VSTART_STATUS = 0x01 << 19, /* vstart interrupt status */
247 INT_DMA_END_STATUS = 0x01 << 20, /* DMA end interrupt status */
248 INT_ERROR_STATUS = 0x01 << 21, /* error interrupt status */
250 DMA_ACT = 0x01 << 27, /* Enable DMA transfer */
251 FIELD_NO = 0x01 << 28, /* Field number */
252 DITHER_ON = 0x01 << 29, /* Dithering is on */
253 ROUND_ON = 0x01 << 30, /* Round is on */
254 MODE_32BIT = 0x01 << 31, /* Data in RGBa888,
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)
268 static irqreturn_t viu_intr(int irq, void *dev_id);
270 struct viu_fmt *format_by_fourcc(int fourcc)
274 for (i = 0; i < NUM_FORMATS; i++) {
275 if (formats[i].pixelformat == fourcc)
279 dprintk(0, "unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
283 void viu_start_dma(struct viu_dev *dev)
285 struct viu_reg *vr = dev->vr;
289 /* Enable DMA operation */
290 out_be32(&vr->status_cfg, SOFT_RST);
291 out_be32(&vr->status_cfg, INT_FIELD_EN);
294 void viu_stop_dma(struct viu_dev *dev)
296 struct viu_reg *vr = dev->vr;
300 out_be32(&vr->status_cfg, 0);
302 /* Clear pending interrupts */
303 status_cfg = in_be32(&vr->status_cfg);
304 if (status_cfg & 0x3f0000)
305 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
307 if (status_cfg & DMA_ACT) {
309 status_cfg = in_be32(&vr->status_cfg);
310 if (status_cfg & INT_DMA_END_STATUS)
315 /* timed out, issue soft reset */
316 out_be32(&vr->status_cfg, SOFT_RST);
317 out_be32(&vr->status_cfg, 0);
319 /* clear DMA_END and other pending irqs */
320 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
327 static int restart_video_queue(struct viu_dmaqueue *vidq)
329 struct viu_buf *buf, *prev;
331 dprintk(1, "%s vidq=0x%08lx\n", __func__, (unsigned long)vidq);
332 if (!list_empty(&vidq->active)) {
333 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
334 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
337 viu_stop_dma(vidq->dev);
339 /* cancel all outstanding capture requests */
340 list_for_each_entry_safe(buf, prev, &vidq->active, vb.queue) {
341 list_del(&buf->vb.queue);
342 buf->vb.state = VIDEOBUF_ERROR;
343 wake_up(&buf->vb.done);
345 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
351 if (list_empty(&vidq->queued))
353 buf = list_entry(vidq->queued.next, struct viu_buf, vb.queue);
355 list_move_tail(&buf->vb.queue, &vidq->active);
357 dprintk(1, "Restarting video dma\n");
358 viu_stop_dma(vidq->dev);
359 viu_start_dma(vidq->dev);
361 buf->vb.state = VIDEOBUF_ACTIVE;
362 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
363 dprintk(2, "[%p/%d] restart_queue - first active\n",
366 } else if (prev->vb.width == buf->vb.width &&
367 prev->vb.height == buf->vb.height &&
368 prev->fmt == buf->fmt) {
369 list_move_tail(&buf->vb.queue, &vidq->active);
370 buf->vb.state = VIDEOBUF_ACTIVE;
371 dprintk(2, "[%p/%d] restart_queue - move to active\n",
380 static void viu_vid_timeout(unsigned long data)
382 struct viu_dev *dev = (struct viu_dev *)data;
384 struct viu_dmaqueue *vidq = &dev->vidq;
386 while (!list_empty(&vidq->active)) {
387 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
388 list_del(&buf->vb.queue);
389 buf->vb.state = VIDEOBUF_ERROR;
390 wake_up(&buf->vb.done);
391 dprintk(1, "viu/0: [%p/%d] timeout\n", buf, buf->vb.i);
394 restart_video_queue(vidq);
398 * Videobuf operations
400 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
403 struct viu_fh *fh = vq->priv_data;
405 *size = fh->width * fh->height * fh->fmt->depth >> 3;
409 while (*size * *count > VIU_VID_MEM_LIMIT * 1024 * 1024)
412 dprintk(1, "%s, count=%d, size=%d\n", __func__, *count, *size);
416 static void free_buffer(struct videobuf_queue *vq, struct viu_buf *buf)
418 struct videobuf_buffer *vb = &buf->vb;
421 BUG_ON(in_interrupt());
423 videobuf_waiton(vq, &buf->vb, 0, 0);
425 if (vq->int_ops && vq->int_ops->vaddr)
426 vaddr = vq->int_ops->vaddr(vb);
429 videobuf_dma_contig_free(vq, &buf->vb);
431 buf->vb.state = VIDEOBUF_NEEDS_INIT;
434 inline int buffer_activate(struct viu_dev *dev, struct viu_buf *buf)
436 struct viu_reg *vr = dev->vr;
439 /* setup the DMA base address */
440 reg_val.field_base_addr = videobuf_to_dma_contig(&buf->vb);
442 dprintk(1, "buffer_activate [%p/%d]: dma addr 0x%lx\n",
443 buf, buf->vb.i, (unsigned long)reg_val.field_base_addr);
445 /* interlace is on by default, set horizontal DMA increment */
446 reg_val.status_cfg = 0;
447 bpp = buf->fmt->depth >> 3;
450 reg_val.status_cfg &= ~MODE_32BIT;
451 reg_val.dma_inc = buf->vb.width * 2;
454 reg_val.status_cfg |= MODE_32BIT;
455 reg_val.dma_inc = buf->vb.width * 4;
458 dprintk(0, "doesn't support color depth(%d)\n",
463 /* setup picture_count register */
464 reg_val.picture_count = (buf->vb.height / 2) << 16 |
467 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
469 buf->vb.state = VIDEOBUF_ACTIVE;
470 dev->capfield = buf->vb.field;
472 /* reset dma increment if needed */
473 if (!V4L2_FIELD_HAS_BOTH(buf->vb.field))
476 out_be32(&vr->dma_inc, reg_val.dma_inc);
477 out_be32(&vr->picture_count, reg_val.picture_count);
478 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
479 mod_timer(&dev->vidq.timeout, jiffies + BUFFER_TIMEOUT);
483 static int buffer_prepare(struct videobuf_queue *vq,
484 struct videobuf_buffer *vb,
485 enum v4l2_field field)
487 struct viu_fh *fh = vq->priv_data;
488 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
491 BUG_ON(fh->fmt == NULL);
493 if (fh->width < 48 || fh->width > norm_maxw() ||
494 fh->height < 32 || fh->height > norm_maxh())
496 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
497 if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size)
500 if (buf->fmt != fh->fmt ||
501 buf->vb.width != fh->width ||
502 buf->vb.height != fh->height ||
503 buf->vb.field != field) {
505 buf->vb.width = fh->width;
506 buf->vb.height = fh->height;
507 buf->vb.field = field;
510 if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
511 rc = videobuf_iolock(vq, &buf->vb, NULL);
515 buf->vb.width = fh->width;
516 buf->vb.height = fh->height;
517 buf->vb.field = field;
521 buf->vb.state = VIDEOBUF_PREPARED;
525 free_buffer(vq, buf);
529 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
531 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
532 struct viu_fh *fh = vq->priv_data;
533 struct viu_dev *dev = fh->dev;
534 struct viu_dmaqueue *vidq = &dev->vidq;
535 struct viu_buf *prev;
537 if (!list_empty(&vidq->queued)) {
538 dprintk(1, "adding vb queue=0x%08lx\n",
539 (unsigned long)&buf->vb.queue);
540 dprintk(1, "vidq pointer 0x%p, queued 0x%p\n",
541 vidq, &vidq->queued);
542 dprintk(1, "dev %p, queued: self %p, next %p, head %p\n",
543 dev, &vidq->queued, vidq->queued.next,
545 list_add_tail(&buf->vb.queue, &vidq->queued);
546 buf->vb.state = VIDEOBUF_QUEUED;
547 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
549 } else if (list_empty(&vidq->active)) {
550 dprintk(1, "adding vb active=0x%08lx\n",
551 (unsigned long)&buf->vb.queue);
552 list_add_tail(&buf->vb.queue, &vidq->active);
553 buf->vb.state = VIDEOBUF_ACTIVE;
554 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
555 dprintk(2, "[%p/%d] buffer_queue - first active\n",
558 buffer_activate(dev, buf);
560 dprintk(1, "adding vb queue2=0x%08lx\n",
561 (unsigned long)&buf->vb.queue);
562 prev = list_entry(vidq->active.prev, struct viu_buf, vb.queue);
563 if (prev->vb.width == buf->vb.width &&
564 prev->vb.height == buf->vb.height &&
565 prev->fmt == buf->fmt) {
566 list_add_tail(&buf->vb.queue, &vidq->active);
567 buf->vb.state = VIDEOBUF_ACTIVE;
568 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
571 list_add_tail(&buf->vb.queue, &vidq->queued);
572 buf->vb.state = VIDEOBUF_QUEUED;
573 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
579 static void buffer_release(struct videobuf_queue *vq,
580 struct videobuf_buffer *vb)
582 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
583 struct viu_fh *fh = vq->priv_data;
584 struct viu_dev *dev = (struct viu_dev *)fh->dev;
587 free_buffer(vq, buf);
590 static struct videobuf_queue_ops viu_video_qops = {
591 .buf_setup = buffer_setup,
592 .buf_prepare = buffer_prepare,
593 .buf_queue = buffer_queue,
594 .buf_release = buffer_release,
598 * IOCTL vidioc handling
600 static int vidioc_querycap(struct file *file, void *priv,
601 struct v4l2_capability *cap)
603 strcpy(cap->driver, "viu");
604 strcpy(cap->card, "viu");
605 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
607 V4L2_CAP_VIDEO_OVERLAY |
612 static int vidioc_enum_fmt(struct file *file, void *priv,
613 struct v4l2_fmtdesc *f)
615 int index = f->index;
617 if (f->index > NUM_FORMATS)
620 strlcpy(f->description, formats[index].name, sizeof(f->description));
621 f->pixelformat = formats[index].fourcc;
625 static int vidioc_g_fmt_cap(struct file *file, void *priv,
626 struct v4l2_format *f)
628 struct viu_fh *fh = priv;
630 f->fmt.pix.width = fh->width;
631 f->fmt.pix.height = fh->height;
632 f->fmt.pix.field = fh->vb_vidq.field;
633 f->fmt.pix.pixelformat = fh->fmt->pixelformat;
634 f->fmt.pix.bytesperline =
635 (f->fmt.pix.width * fh->fmt->depth) >> 3;
636 f->fmt.pix.sizeimage = fh->sizeimage;
640 static int vidioc_try_fmt_cap(struct file *file, void *priv,
641 struct v4l2_format *f)
644 enum v4l2_field field;
645 unsigned int maxw, maxh;
647 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
649 dprintk(1, "Fourcc format (0x%08x) invalid.",
650 f->fmt.pix.pixelformat);
654 field = f->fmt.pix.field;
656 if (field == V4L2_FIELD_ANY) {
657 field = V4L2_FIELD_INTERLACED;
658 } else if (field != V4L2_FIELD_INTERLACED) {
659 dprintk(1, "Field type invalid.\n");
666 f->fmt.pix.field = field;
667 if (f->fmt.pix.height < 32)
668 f->fmt.pix.height = 32;
669 if (f->fmt.pix.height > maxh)
670 f->fmt.pix.height = maxh;
671 if (f->fmt.pix.width < 48)
672 f->fmt.pix.width = 48;
673 if (f->fmt.pix.width > maxw)
674 f->fmt.pix.width = maxw;
675 f->fmt.pix.width &= ~0x03;
676 f->fmt.pix.bytesperline =
677 (f->fmt.pix.width * fmt->depth) >> 3;
682 static int vidioc_s_fmt_cap(struct file *file, void *priv,
683 struct v4l2_format *f)
685 struct viu_fh *fh = priv;
688 ret = vidioc_try_fmt_cap(file, fh, f);
692 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
693 fh->width = f->fmt.pix.width;
694 fh->height = f->fmt.pix.height;
695 fh->sizeimage = f->fmt.pix.sizeimage;
696 fh->vb_vidq.field = f->fmt.pix.field;
698 dprintk(1, "set to pixelformat '%4.6s'\n", (char *)&fh->fmt->name);
702 static int vidioc_g_fmt_overlay(struct file *file, void *priv,
703 struct v4l2_format *f)
705 struct viu_fh *fh = priv;
707 f->fmt.win = fh->win;
711 static int verify_preview(struct viu_dev *dev, struct v4l2_window *win)
713 enum v4l2_field field;
716 if (dev->ovbuf.base == NULL)
718 if (dev->ovfmt == NULL)
720 if (win->w.width < 48 || win->w.height < 32)
724 maxw = dev->crop_current.width;
725 maxh = dev->crop_current.height;
727 if (field == V4L2_FIELD_ANY) {
728 field = (win->w.height > maxh/2)
729 ? V4L2_FIELD_INTERLACED
734 case V4L2_FIELD_BOTTOM:
737 case V4L2_FIELD_INTERLACED:
744 if (win->w.width > maxw)
746 if (win->w.height > maxh)
747 win->w.height = maxh;
751 inline void viu_activate_overlay(struct viu_reg *viu_reg)
753 struct viu_reg *vr = viu_reg;
755 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
756 out_be32(&vr->dma_inc, reg_val.dma_inc);
757 out_be32(&vr->picture_count, reg_val.picture_count);
760 static int viu_setup_preview(struct viu_dev *dev, struct viu_fh *fh)
764 dprintk(1, "%s %dx%d %s\n", __func__,
765 fh->win.w.width, fh->win.w.height, dev->ovfmt->name);
767 reg_val.status_cfg = 0;
770 reg_val.picture_count = (fh->win.w.height / 2) << 16 |
773 /* setup color depth and dma increment */
774 bpp = dev->ovfmt->depth / 8;
777 reg_val.status_cfg &= ~MODE_32BIT;
778 reg_val.dma_inc = fh->win.w.width * 2;
781 reg_val.status_cfg |= MODE_32BIT;
782 reg_val.dma_inc = fh->win.w.width * 4;
785 dprintk(0, "device doesn't support color depth(%d)\n",
790 dev->ovfield = fh->win.field;
791 if (!V4L2_FIELD_HAS_BOTH(dev->ovfield))
794 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
796 /* setup the base address of the overlay buffer */
797 reg_val.field_base_addr = (u32)dev->ovbuf.base;
802 static int vidioc_s_fmt_overlay(struct file *file, void *priv,
803 struct v4l2_format *f)
805 struct viu_fh *fh = priv;
806 struct viu_dev *dev = (struct viu_dev *)fh->dev;
810 err = verify_preview(dev, &f->fmt.win);
814 fh->win = f->fmt.win;
816 spin_lock_irqsave(&dev->slock, flags);
817 viu_setup_preview(dev, fh);
818 spin_unlock_irqrestore(&dev->slock, flags);
822 static int vidioc_try_fmt_overlay(struct file *file, void *priv,
823 struct v4l2_format *f)
828 static int vidioc_overlay(struct file *file, void *priv, unsigned int on)
830 struct viu_fh *fh = priv;
831 struct viu_dev *dev = (struct viu_dev *)fh->dev;
835 spin_lock_irqsave(&dev->slock, flags);
836 viu_activate_overlay(dev->vr);
841 spin_unlock_irqrestore(&dev->slock, flags);
850 int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
852 struct viu_fh *fh = priv;
853 struct viu_dev *dev = fh->dev;
854 struct v4l2_framebuffer *fb = arg;
857 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
861 int vidioc_s_fbuf(struct file *file, void *priv, const struct v4l2_framebuffer *arg)
863 struct viu_fh *fh = priv;
864 struct viu_dev *dev = fh->dev;
865 const struct v4l2_framebuffer *fb = arg;
868 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
872 fmt = format_by_fourcc(fb->fmt.pixelformat);
879 if (dev->ovbuf.fmt.bytesperline == 0) {
880 dev->ovbuf.fmt.bytesperline =
881 dev->ovbuf.fmt.width * fmt->depth / 8;
886 static int vidioc_reqbufs(struct file *file, void *priv,
887 struct v4l2_requestbuffers *p)
889 struct viu_fh *fh = priv;
891 return videobuf_reqbufs(&fh->vb_vidq, p);
894 static int vidioc_querybuf(struct file *file, void *priv,
895 struct v4l2_buffer *p)
897 struct viu_fh *fh = priv;
899 return videobuf_querybuf(&fh->vb_vidq, p);
902 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
904 struct viu_fh *fh = priv;
906 return videobuf_qbuf(&fh->vb_vidq, p);
909 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
911 struct viu_fh *fh = priv;
913 return videobuf_dqbuf(&fh->vb_vidq, p,
914 file->f_flags & O_NONBLOCK);
917 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
919 struct viu_fh *fh = priv;
920 struct viu_dev *dev = fh->dev;
922 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
930 viu_start_dma(fh->dev);
932 return videobuf_streamon(&fh->vb_vidq);
935 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
937 struct viu_fh *fh = priv;
939 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
944 viu_stop_dma(fh->dev);
946 return videobuf_streamoff(&fh->vb_vidq);
949 #define decoder_call(viu, o, f, args...) \
950 v4l2_subdev_call(viu->decoder, o, f, ##args)
952 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
954 struct viu_fh *fh = priv;
956 decoder_call(fh->dev, video, querystd, std_id);
960 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
962 struct viu_fh *fh = priv;
965 decoder_call(fh->dev, core, s_std, id);
969 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
971 struct viu_fh *fh = priv;
973 *std_id = fh->dev->std;
977 /* only one input in this driver */
978 static int vidioc_enum_input(struct file *file, void *priv,
979 struct v4l2_input *inp)
981 struct viu_fh *fh = priv;
986 inp->type = V4L2_INPUT_TYPE_CAMERA;
987 inp->std = fh->dev->vdev->tvnorms;
988 strcpy(inp->name, "Camera");
992 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
998 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1000 struct viu_fh *fh = priv;
1005 decoder_call(fh->dev, video, s_routing, i, 0, 0);
1010 static int vidioc_queryctrl(struct file *file, void *priv,
1011 struct v4l2_queryctrl *qc)
1015 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1016 if (qc->id && qc->id == viu_qctrl[i].id) {
1017 memcpy(qc, &(viu_qctrl[i]), sizeof(*qc));
1024 static int vidioc_g_ctrl(struct file *file, void *priv,
1025 struct v4l2_control *ctrl)
1029 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1030 if (ctrl->id == viu_qctrl[i].id) {
1031 ctrl->value = qctl_regs[i];
1037 static int vidioc_s_ctrl(struct file *file, void *priv,
1038 struct v4l2_control *ctrl)
1042 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1043 if (ctrl->id == viu_qctrl[i].id) {
1044 if (ctrl->value < viu_qctrl[i].minimum
1045 || ctrl->value > viu_qctrl[i].maximum)
1047 qctl_regs[i] = ctrl->value;
1054 inline void viu_activate_next_buf(struct viu_dev *dev,
1055 struct viu_dmaqueue *viuq)
1057 struct viu_dmaqueue *vidq = viuq;
1058 struct viu_buf *buf;
1060 /* launch another DMA operation for an active/queued buffer */
1061 if (!list_empty(&vidq->active)) {
1062 buf = list_entry(vidq->active.next, struct viu_buf,
1064 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1065 buffer_activate(dev, buf);
1066 } else if (!list_empty(&vidq->queued)) {
1067 buf = list_entry(vidq->queued.next, struct viu_buf,
1069 list_del(&buf->vb.queue);
1071 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1072 list_add_tail(&buf->vb.queue, &vidq->active);
1073 buf->vb.state = VIDEOBUF_ACTIVE;
1074 buffer_activate(dev, buf);
1078 inline void viu_default_settings(struct viu_reg *viu_reg)
1080 struct viu_reg *vr = viu_reg;
1082 out_be32(&vr->luminance, 0x9512A254);
1083 out_be32(&vr->chroma_r, 0x03310000);
1084 out_be32(&vr->chroma_g, 0x06600F38);
1085 out_be32(&vr->chroma_b, 0x00000409);
1086 out_be32(&vr->alpha, 0x000000ff);
1087 out_be32(&vr->req_alarm, 0x00000090);
1088 dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
1089 in_be32(&vr->status_cfg), in_be32(&vr->field_base_addr));
1092 static void viu_overlay_intr(struct viu_dev *dev, u32 status)
1094 struct viu_reg *vr = dev->vr;
1096 if (status & INT_DMA_END_STATUS)
1099 if (status & INT_FIELD_STATUS) {
1100 if (dev->dma_done) {
1101 u32 addr = reg_val.field_base_addr;
1104 if (status & FIELD_NO)
1105 addr += reg_val.dma_inc;
1107 out_be32(&vr->field_base_addr, addr);
1108 out_be32(&vr->dma_inc, reg_val.dma_inc);
1109 out_be32(&vr->status_cfg,
1110 (status & 0xffc0ffff) |
1111 (status & INT_ALL_STATUS) |
1112 reg_val.status_cfg);
1113 } else if (status & INT_VSYNC_STATUS) {
1114 out_be32(&vr->status_cfg,
1115 (status & 0xffc0ffff) |
1116 (status & INT_ALL_STATUS) |
1117 reg_val.status_cfg);
1122 static void viu_capture_intr(struct viu_dev *dev, u32 status)
1124 struct viu_dmaqueue *vidq = &dev->vidq;
1125 struct viu_reg *vr = dev->vr;
1126 struct viu_buf *buf;
1131 field_num = status & FIELD_NO;
1132 need_two = V4L2_FIELD_HAS_BOTH(dev->capfield);
1134 if (status & INT_DMA_END_STATUS) {
1136 if (((field_num == 0) && (dev->field == 0)) ||
1137 (field_num && (dev->field == 1)))
1141 if (status & INT_FIELD_STATUS) {
1142 dprintk(1, "irq: field %d, done %d\n",
1143 !!field_num, dma_done);
1144 if (unlikely(dev->first)) {
1145 if (field_num == 0) {
1147 dprintk(1, "activate first buf\n");
1148 viu_activate_next_buf(dev, vidq);
1150 dprintk(1, "wait field 0\n");
1154 /* setup buffer address for next dma operation */
1155 if (!list_empty(&vidq->active)) {
1156 u32 addr = reg_val.field_base_addr;
1158 if (field_num && need_two) {
1159 addr += reg_val.dma_inc;
1160 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1161 (unsigned long)addr, dev->field);
1163 out_be32(&vr->field_base_addr, addr);
1164 out_be32(&vr->dma_inc, reg_val.dma_inc);
1165 out_be32(&vr->status_cfg,
1166 (status & 0xffc0ffff) |
1167 (status & INT_ALL_STATUS) |
1168 reg_val.status_cfg);
1173 if (dma_done && field_num && (dev->field == 2)) {
1175 buf = list_entry(vidq->active.next,
1176 struct viu_buf, vb.queue);
1177 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1179 (unsigned long)videobuf_to_dma_contig(&buf->vb),
1180 (unsigned long)in_be32(&vr->field_base_addr));
1182 if (waitqueue_active(&buf->vb.done)) {
1183 list_del(&buf->vb.queue);
1184 v4l2_get_timestamp(&buf->vb.ts);
1185 buf->vb.state = VIDEOBUF_DONE;
1186 buf->vb.field_count++;
1187 wake_up(&buf->vb.done);
1189 /* activate next dma buffer */
1190 viu_activate_next_buf(dev, vidq);
1194 static irqreturn_t viu_intr(int irq, void *dev_id)
1196 struct viu_dev *dev = (struct viu_dev *)dev_id;
1197 struct viu_reg *vr = dev->vr;
1201 status = in_be32(&vr->status_cfg);
1203 if (status & INT_ERROR_STATUS) {
1204 dev->irqs.error_irq++;
1205 error = status & ERR_MASK;
1207 dprintk(1, "Err: error(%d), times:%d!\n",
1208 error >> 4, dev->irqs.error_irq);
1209 /* Clear interrupt error bit and error flags */
1210 out_be32(&vr->status_cfg,
1211 (status & 0xffc0ffff) | INT_ERROR_STATUS);
1214 if (status & INT_DMA_END_STATUS) {
1215 dev->irqs.dma_end_irq++;
1217 dprintk(2, "VIU DMA end interrupt times: %d\n",
1218 dev->irqs.dma_end_irq);
1221 if (status & INT_HSYNC_STATUS)
1222 dev->irqs.hsync_irq++;
1224 if (status & INT_FIELD_STATUS) {
1225 dev->irqs.field_irq++;
1226 dprintk(2, "VIU field interrupt times: %d\n",
1227 dev->irqs.field_irq);
1230 if (status & INT_VSTART_STATUS)
1231 dev->irqs.vstart_irq++;
1233 if (status & INT_VSYNC_STATUS) {
1234 dev->irqs.vsync_irq++;
1235 dprintk(2, "VIU vsync interrupt times: %d\n",
1236 dev->irqs.vsync_irq);
1239 /* clear all pending irqs */
1240 status = in_be32(&vr->status_cfg);
1241 out_be32(&vr->status_cfg,
1242 (status & 0xffc0ffff) | (status & INT_ALL_STATUS));
1244 if (dev->ovenable) {
1245 viu_overlay_intr(dev, status);
1250 viu_capture_intr(dev, status);
1255 * File operations for the device
1257 static int viu_open(struct file *file)
1259 struct video_device *vdev = video_devdata(file);
1260 struct viu_dev *dev = video_get_drvdata(vdev);
1263 int minor = vdev->minor;
1267 dprintk(1, "viu: open (minor=%d)\n", minor);
1270 if (dev->users > 1) {
1277 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1278 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1280 if (mutex_lock_interruptible(&dev->lock)) {
1282 return -ERESTARTSYS;
1285 /* allocate and initialize per filehandle data */
1286 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1289 mutex_unlock(&dev->lock);
1293 file->private_data = fh;
1296 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1297 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_RGB32);
1298 fh->width = norm_maxw();
1299 fh->height = norm_maxh();
1300 dev->crop_current.width = fh->width;
1301 dev->crop_current.height = fh->height;
1303 /* Put all controls at a sane state */
1304 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++)
1305 qctl_regs[i] = viu_qctrl[i].default_value;
1307 dprintk(1, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1308 (unsigned long)fh, (unsigned long)dev,
1309 (unsigned long)&dev->vidq);
1310 dprintk(1, "Open: list_empty queued=%d\n",
1311 list_empty(&dev->vidq.queued));
1312 dprintk(1, "Open: list_empty active=%d\n",
1313 list_empty(&dev->vidq.active));
1315 viu_default_settings(vr);
1317 status_cfg = in_be32(&vr->status_cfg);
1318 out_be32(&vr->status_cfg,
1319 status_cfg & ~(INT_VSYNC_EN | INT_HSYNC_EN |
1320 INT_FIELD_EN | INT_VSTART_EN |
1321 INT_DMA_END_EN | INT_ERROR_EN | INT_ECC_EN));
1323 status_cfg = in_be32(&vr->status_cfg);
1324 out_be32(&vr->status_cfg, status_cfg | INT_ALL_STATUS);
1326 spin_lock_init(&fh->vbq_lock);
1327 videobuf_queue_dma_contig_init(&fh->vb_vidq, &viu_video_qops,
1328 dev->dev, &fh->vbq_lock,
1329 fh->type, V4L2_FIELD_INTERLACED,
1330 sizeof(struct viu_buf), fh,
1332 mutex_unlock(&dev->lock);
1336 static ssize_t viu_read(struct file *file, char __user *data, size_t count,
1339 struct viu_fh *fh = file->private_data;
1340 struct viu_dev *dev = fh->dev;
1343 dprintk(2, "%s\n", __func__);
1347 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1348 if (mutex_lock_interruptible(&dev->lock))
1349 return -ERESTARTSYS;
1351 ret = videobuf_read_stream(&fh->vb_vidq, data, count,
1352 ppos, 0, file->f_flags & O_NONBLOCK);
1353 mutex_unlock(&dev->lock);
1359 static unsigned int viu_poll(struct file *file, struct poll_table_struct *wait)
1361 struct viu_fh *fh = file->private_data;
1362 struct videobuf_queue *q = &fh->vb_vidq;
1363 struct viu_dev *dev = fh->dev;
1366 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1369 mutex_lock(&dev->lock);
1370 res = videobuf_poll_stream(file, q, wait);
1371 mutex_unlock(&dev->lock);
1375 static int viu_release(struct file *file)
1377 struct viu_fh *fh = file->private_data;
1378 struct viu_dev *dev = fh->dev;
1379 int minor = video_devdata(file)->minor;
1381 mutex_lock(&dev->lock);
1383 videobuf_stop(&fh->vb_vidq);
1384 videobuf_mmap_free(&fh->vb_vidq);
1385 mutex_unlock(&dev->lock);
1390 dprintk(1, "close (minor=%d, users=%d)\n",
1395 void viu_reset(struct viu_reg *reg)
1397 out_be32(®->status_cfg, 0);
1398 out_be32(®->luminance, 0x9512a254);
1399 out_be32(®->chroma_r, 0x03310000);
1400 out_be32(®->chroma_g, 0x06600f38);
1401 out_be32(®->chroma_b, 0x00000409);
1402 out_be32(®->field_base_addr, 0);
1403 out_be32(®->dma_inc, 0);
1404 out_be32(®->picture_count, 0x01e002d0);
1405 out_be32(®->req_alarm, 0x00000090);
1406 out_be32(®->alpha, 0x000000ff);
1409 static int viu_mmap(struct file *file, struct vm_area_struct *vma)
1411 struct viu_fh *fh = file->private_data;
1412 struct viu_dev *dev = fh->dev;
1415 dprintk(1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1417 if (mutex_lock_interruptible(&dev->lock))
1418 return -ERESTARTSYS;
1419 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1420 mutex_unlock(&dev->lock);
1422 dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1423 (unsigned long)vma->vm_start,
1424 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1430 static struct v4l2_file_operations viu_fops = {
1431 .owner = THIS_MODULE,
1433 .release = viu_release,
1436 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1440 static const struct v4l2_ioctl_ops viu_ioctl_ops = {
1441 .vidioc_querycap = vidioc_querycap,
1442 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1443 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_cap,
1444 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_cap,
1445 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_cap,
1446 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt,
1447 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_overlay,
1448 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_overlay,
1449 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_overlay,
1450 .vidioc_overlay = vidioc_overlay,
1451 .vidioc_g_fbuf = vidioc_g_fbuf,
1452 .vidioc_s_fbuf = vidioc_s_fbuf,
1453 .vidioc_reqbufs = vidioc_reqbufs,
1454 .vidioc_querybuf = vidioc_querybuf,
1455 .vidioc_qbuf = vidioc_qbuf,
1456 .vidioc_dqbuf = vidioc_dqbuf,
1457 .vidioc_g_std = vidioc_g_std,
1458 .vidioc_s_std = vidioc_s_std,
1459 .vidioc_querystd = vidioc_querystd,
1460 .vidioc_enum_input = vidioc_enum_input,
1461 .vidioc_g_input = vidioc_g_input,
1462 .vidioc_s_input = vidioc_s_input,
1463 .vidioc_queryctrl = vidioc_queryctrl,
1464 .vidioc_g_ctrl = vidioc_g_ctrl,
1465 .vidioc_s_ctrl = vidioc_s_ctrl,
1466 .vidioc_streamon = vidioc_streamon,
1467 .vidioc_streamoff = vidioc_streamoff,
1470 static struct video_device viu_template = {
1474 .ioctl_ops = &viu_ioctl_ops,
1475 .release = video_device_release,
1477 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1480 static int viu_of_probe(struct platform_device *op)
1482 struct viu_dev *viu_dev;
1483 struct video_device *vdev;
1485 struct viu_reg __iomem *viu_regs;
1486 struct i2c_adapter *ad;
1489 ret = of_address_to_resource(op->dev.of_node, 0, &r);
1491 dev_err(&op->dev, "Can't parse device node resource\n");
1495 viu_irq = irq_of_parse_and_map(op->dev.of_node, 0);
1496 if (viu_irq == NO_IRQ) {
1497 dev_err(&op->dev, "Error while mapping the irq\n");
1501 /* request mem region */
1502 if (!devm_request_mem_region(&op->dev, r.start,
1503 sizeof(struct viu_reg), DRV_NAME)) {
1504 dev_err(&op->dev, "Error while requesting mem region\n");
1509 /* remap registers */
1510 viu_regs = devm_ioremap(&op->dev, r.start, sizeof(struct viu_reg));
1512 dev_err(&op->dev, "Can't map register set\n");
1517 /* Prepare our private structure */
1518 viu_dev = devm_kzalloc(&op->dev, sizeof(struct viu_dev), GFP_ATOMIC);
1520 dev_err(&op->dev, "Can't allocate private structure\n");
1525 viu_dev->vr = viu_regs;
1526 viu_dev->irq = viu_irq;
1527 viu_dev->dev = &op->dev;
1529 /* init video dma queues */
1530 INIT_LIST_HEAD(&viu_dev->vidq.active);
1531 INIT_LIST_HEAD(&viu_dev->vidq.queued);
1533 snprintf(viu_dev->v4l2_dev.name,
1534 sizeof(viu_dev->v4l2_dev.name), "%s", "VIU");
1535 ret = v4l2_device_register(viu_dev->dev, &viu_dev->v4l2_dev);
1537 dev_err(&op->dev, "v4l2_device_register() failed: %d\n", ret);
1541 ad = i2c_get_adapter(0);
1542 viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
1543 "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
1545 viu_dev->vidq.timeout.function = viu_vid_timeout;
1546 viu_dev->vidq.timeout.data = (unsigned long)viu_dev;
1547 init_timer(&viu_dev->vidq.timeout);
1548 viu_dev->std = V4L2_STD_NTSC_M;
1551 /* Allocate memory for video device */
1552 vdev = video_device_alloc();
1558 memcpy(vdev, &viu_template, sizeof(viu_template));
1560 vdev->v4l2_dev = &viu_dev->v4l2_dev;
1562 viu_dev->vdev = vdev;
1564 /* initialize locks */
1565 mutex_init(&viu_dev->lock);
1566 viu_dev->vdev->lock = &viu_dev->lock;
1567 spin_lock_init(&viu_dev->slock);
1569 video_set_drvdata(viu_dev->vdev, viu_dev);
1571 mutex_lock(&viu_dev->lock);
1573 ret = video_register_device(viu_dev->vdev, VFL_TYPE_GRABBER, -1);
1575 video_device_release(viu_dev->vdev);
1579 /* enable VIU clock */
1580 viu_dev->clk = clk_get(&op->dev, "viu_clk");
1581 if (IS_ERR(viu_dev->clk)) {
1582 dev_err(&op->dev, "failed to find the clock module!\n");
1586 clk_enable(viu_dev->clk);
1589 /* reset VIU module */
1590 viu_reset(viu_dev->vr);
1592 /* install interrupt handler */
1593 if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
1594 dev_err(&op->dev, "Request VIU IRQ failed.\n");
1599 mutex_unlock(&viu_dev->lock);
1601 dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
1605 clk_disable(viu_dev->clk);
1606 clk_put(viu_dev->clk);
1608 video_unregister_device(viu_dev->vdev);
1610 mutex_unlock(&viu_dev->lock);
1611 i2c_put_adapter(ad);
1612 v4l2_device_unregister(&viu_dev->v4l2_dev);
1614 irq_dispose_mapping(viu_irq);
1618 static int viu_of_remove(struct platform_device *op)
1620 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1621 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1622 struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
1623 struct v4l2_subdev, list);
1624 struct i2c_client *client = v4l2_get_subdevdata(sdev);
1626 free_irq(dev->irq, (void *)dev);
1627 irq_dispose_mapping(dev->irq);
1629 clk_disable(dev->clk);
1632 video_unregister_device(dev->vdev);
1633 i2c_put_adapter(client->adapter);
1634 v4l2_device_unregister(&dev->v4l2_dev);
1639 static int viu_suspend(struct platform_device *op, pm_message_t state)
1641 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1642 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1644 clk_disable(dev->clk);
1648 static int viu_resume(struct platform_device *op)
1650 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1651 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1653 clk_enable(dev->clk);
1659 * Initialization and module stuff
1661 static struct of_device_id mpc512x_viu_of_match[] = {
1663 .compatible = "fsl,mpc5121-viu",
1667 MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
1669 static struct platform_driver viu_of_platform_driver = {
1670 .probe = viu_of_probe,
1671 .remove = viu_of_remove,
1673 .suspend = viu_suspend,
1674 .resume = viu_resume,
1678 .owner = THIS_MODULE,
1679 .of_match_table = mpc512x_viu_of_match,
1683 module_platform_driver(viu_of_platform_driver);
1685 MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1686 MODULE_AUTHOR("Hongjun Chen");
1687 MODULE_LICENSE("GPL");
1688 MODULE_VERSION(VIU_VERSION);