2 * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
4 * Freescale VIU video driver
6 * Authors: Hongjun Chen <hong-jun.chen@freescale.com>
7 * Porting to 2.6.35 by DENX Software Engineering,
8 * Anatolij Gustschin <agust@denx.de>
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_address.h>
25 #include <linux/of_irq.h>
26 #include <linux/of_platform.h>
27 #include <linux/slab.h>
28 #include <media/v4l2-common.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/videobuf-dma-contig.h>
33 #define DRV_NAME "fsl_viu"
34 #define VIU_VERSION "0.5.1"
36 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
38 #define VIU_VID_MEM_LIMIT 4 /* Video memory limit, in Mb */
40 /* I2C address of video decoder chip is 0x4A */
41 #define VIU_VIDEO_DECODER_ADDR 0x25
43 /* supported controls */
44 static struct v4l2_queryctrl viu_qctrl[] = {
46 .id = V4L2_CID_BRIGHTNESS,
47 .type = V4L2_CTRL_TYPE_INTEGER,
55 .id = V4L2_CID_CONTRAST,
56 .type = V4L2_CTRL_TYPE_INTEGER,
61 .default_value = 0x10,
64 .id = V4L2_CID_SATURATION,
65 .type = V4L2_CTRL_TYPE_INTEGER,
74 .type = V4L2_CTRL_TYPE_INTEGER,
84 static int qctl_regs[ARRAY_SIZE(viu_qctrl)];
86 static int info_level;
88 #define dprintk(level, fmt, arg...) \
90 if (level <= info_level) \
91 printk(KERN_DEBUG "viu: " fmt , ## arg); \
99 u32 fourcc; /* v4l2 format id */
104 static struct viu_fmt formats[] = {
106 .name = "RGB-16 (5/B-6/G-5/R)",
107 .fourcc = V4L2_PIX_FMT_RGB565,
108 .pixelformat = V4L2_PIX_FMT_RGB565,
111 .name = "RGB-32 (A-R-G-B)",
112 .fourcc = V4L2_PIX_FMT_RGB32,
113 .pixelformat = V4L2_PIX_FMT_RGB32,
121 /* buffer for one video frame */
123 /* common v4l buffer stuff -- must be first */
124 struct videobuf_buffer vb;
128 struct viu_dmaqueue {
130 struct list_head active;
131 struct list_head queued;
132 struct timer_list timeout;
155 } __attribute__ ((packed));
158 struct v4l2_device v4l2_dev;
164 /* various device info */
165 struct video_device *vdev;
166 struct viu_dmaqueue vidq;
167 enum v4l2_field capfield;
172 /* Hardware register area */
175 /* Interrupt vector */
177 struct viu_status irqs;
180 struct v4l2_framebuffer ovbuf;
181 struct viu_fmt *ovfmt;
182 unsigned int ovenable;
183 enum v4l2_field ovfield;
186 struct v4l2_rect crop_current;
192 struct v4l2_subdev *decoder;
201 struct videobuf_queue vb_vidq;
202 spinlock_t vbq_lock; /* spinlock for the videobuf queue */
205 struct v4l2_window win;
206 struct v4l2_clip clips[1];
210 int width, height, sizeimage;
211 enum v4l2_buf_type type;
214 static struct viu_reg reg_val;
217 * Macro definitions of VIU registers
220 /* STATUS_CONFIG register */
224 ERR_MASK = 0x0f << 4, /* Error code mask */
225 ERR_NO = 0x00, /* No error */
226 ERR_DMA_V = 0x01 << 4, /* DMA in vertical active */
227 ERR_DMA_VB = 0x02 << 4, /* DMA in vertical blanking */
228 ERR_LINE_TOO_LONG = 0x04 << 4, /* Line too long */
229 ERR_TOO_MANG_LINES = 0x05 << 4, /* Too many lines in field */
230 ERR_LINE_TOO_SHORT = 0x06 << 4, /* Line too short */
231 ERR_NOT_ENOUGH_LINE = 0x07 << 4, /* Not enough lines in field */
232 ERR_FIFO_OVERFLOW = 0x08 << 4, /* FIFO overflow */
233 ERR_FIFO_UNDERFLOW = 0x09 << 4, /* FIFO underflow */
234 ERR_1bit_ECC = 0x0a << 4, /* One bit ECC error */
235 ERR_MORE_ECC = 0x0b << 4, /* Two/more bits ECC error */
237 INT_FIELD_EN = 0x01 << 8, /* Enable field interrupt */
238 INT_VSYNC_EN = 0x01 << 9, /* Enable vsync interrupt */
239 INT_HSYNC_EN = 0x01 << 10, /* Enable hsync interrupt */
240 INT_VSTART_EN = 0x01 << 11, /* Enable vstart interrupt */
241 INT_DMA_END_EN = 0x01 << 12, /* Enable DMA end interrupt */
242 INT_ERROR_EN = 0x01 << 13, /* Enable error interrupt */
243 INT_ECC_EN = 0x01 << 14, /* Enable ECC interrupt */
245 INT_FIELD_STATUS = 0x01 << 16, /* field interrupt status */
246 INT_VSYNC_STATUS = 0x01 << 17, /* vsync interrupt status */
247 INT_HSYNC_STATUS = 0x01 << 18, /* hsync interrupt status */
248 INT_VSTART_STATUS = 0x01 << 19, /* vstart interrupt status */
249 INT_DMA_END_STATUS = 0x01 << 20, /* DMA end interrupt status */
250 INT_ERROR_STATUS = 0x01 << 21, /* error interrupt status */
252 DMA_ACT = 0x01 << 27, /* Enable DMA transfer */
253 FIELD_NO = 0x01 << 28, /* Field number */
254 DITHER_ON = 0x01 << 29, /* Dithering is on */
255 ROUND_ON = 0x01 << 30, /* Round is on */
256 MODE_32BIT = 0x01 << 31, /* Data in RGBa888,
261 #define norm_maxw() 720
262 #define norm_maxh() 576
264 #define INT_ALL_STATUS (INT_FIELD_STATUS | INT_VSYNC_STATUS | \
265 INT_HSYNC_STATUS | INT_VSTART_STATUS | \
266 INT_DMA_END_STATUS | INT_ERROR_STATUS)
268 #define NUM_FORMATS ARRAY_SIZE(formats)
270 static irqreturn_t viu_intr(int irq, void *dev_id);
272 struct viu_fmt *format_by_fourcc(int fourcc)
276 for (i = 0; i < NUM_FORMATS; i++) {
277 if (formats[i].pixelformat == fourcc)
281 dprintk(0, "unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
285 void viu_start_dma(struct viu_dev *dev)
287 struct viu_reg *vr = dev->vr;
291 /* Enable DMA operation */
292 out_be32(&vr->status_cfg, SOFT_RST);
293 out_be32(&vr->status_cfg, INT_FIELD_EN);
296 void viu_stop_dma(struct viu_dev *dev)
298 struct viu_reg *vr = dev->vr;
302 out_be32(&vr->status_cfg, 0);
304 /* Clear pending interrupts */
305 status_cfg = in_be32(&vr->status_cfg);
306 if (status_cfg & 0x3f0000)
307 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
309 if (status_cfg & DMA_ACT) {
311 status_cfg = in_be32(&vr->status_cfg);
312 if (status_cfg & INT_DMA_END_STATUS)
317 /* timed out, issue soft reset */
318 out_be32(&vr->status_cfg, SOFT_RST);
319 out_be32(&vr->status_cfg, 0);
321 /* clear DMA_END and other pending irqs */
322 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
329 static int restart_video_queue(struct viu_dmaqueue *vidq)
331 struct viu_buf *buf, *prev;
333 dprintk(1, "%s vidq=0x%08lx\n", __func__, (unsigned long)vidq);
334 if (!list_empty(&vidq->active)) {
335 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
336 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
339 viu_stop_dma(vidq->dev);
341 /* cancel all outstanding capture requests */
342 list_for_each_entry_safe(buf, prev, &vidq->active, vb.queue) {
343 list_del(&buf->vb.queue);
344 buf->vb.state = VIDEOBUF_ERROR;
345 wake_up(&buf->vb.done);
347 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
353 if (list_empty(&vidq->queued))
355 buf = list_entry(vidq->queued.next, struct viu_buf, vb.queue);
357 list_move_tail(&buf->vb.queue, &vidq->active);
359 dprintk(1, "Restarting video dma\n");
360 viu_stop_dma(vidq->dev);
361 viu_start_dma(vidq->dev);
363 buf->vb.state = VIDEOBUF_ACTIVE;
364 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
365 dprintk(2, "[%p/%d] restart_queue - first active\n",
368 } else if (prev->vb.width == buf->vb.width &&
369 prev->vb.height == buf->vb.height &&
370 prev->fmt == buf->fmt) {
371 list_move_tail(&buf->vb.queue, &vidq->active);
372 buf->vb.state = VIDEOBUF_ACTIVE;
373 dprintk(2, "[%p/%d] restart_queue - move to active\n",
382 static void viu_vid_timeout(unsigned long data)
384 struct viu_dev *dev = (struct viu_dev *)data;
386 struct viu_dmaqueue *vidq = &dev->vidq;
388 while (!list_empty(&vidq->active)) {
389 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
390 list_del(&buf->vb.queue);
391 buf->vb.state = VIDEOBUF_ERROR;
392 wake_up(&buf->vb.done);
393 dprintk(1, "viu/0: [%p/%d] timeout\n", buf, buf->vb.i);
396 restart_video_queue(vidq);
400 * Videobuf operations
402 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
405 struct viu_fh *fh = vq->priv_data;
407 *size = fh->width * fh->height * fh->fmt->depth >> 3;
411 while (*size * *count > VIU_VID_MEM_LIMIT * 1024 * 1024)
414 dprintk(1, "%s, count=%d, size=%d\n", __func__, *count, *size);
418 static void free_buffer(struct videobuf_queue *vq, struct viu_buf *buf)
420 struct videobuf_buffer *vb = &buf->vb;
423 BUG_ON(in_interrupt());
425 videobuf_waiton(vq, &buf->vb, 0, 0);
427 if (vq->int_ops && vq->int_ops->vaddr)
428 vaddr = vq->int_ops->vaddr(vb);
431 videobuf_dma_contig_free(vq, &buf->vb);
433 buf->vb.state = VIDEOBUF_NEEDS_INIT;
436 inline int buffer_activate(struct viu_dev *dev, struct viu_buf *buf)
438 struct viu_reg *vr = dev->vr;
441 /* setup the DMA base address */
442 reg_val.field_base_addr = videobuf_to_dma_contig(&buf->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);
447 /* interlace is on by default, set horizontal DMA increment */
448 reg_val.status_cfg = 0;
449 bpp = buf->fmt->depth >> 3;
452 reg_val.status_cfg &= ~MODE_32BIT;
453 reg_val.dma_inc = buf->vb.width * 2;
456 reg_val.status_cfg |= MODE_32BIT;
457 reg_val.dma_inc = buf->vb.width * 4;
460 dprintk(0, "doesn't support color depth(%d)\n",
465 /* setup picture_count register */
466 reg_val.picture_count = (buf->vb.height / 2) << 16 |
469 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
471 buf->vb.state = VIDEOBUF_ACTIVE;
472 dev->capfield = buf->vb.field;
474 /* reset dma increment if needed */
475 if (!V4L2_FIELD_HAS_BOTH(buf->vb.field))
478 out_be32(&vr->dma_inc, reg_val.dma_inc);
479 out_be32(&vr->picture_count, reg_val.picture_count);
480 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
481 mod_timer(&dev->vidq.timeout, jiffies + BUFFER_TIMEOUT);
485 static int buffer_prepare(struct videobuf_queue *vq,
486 struct videobuf_buffer *vb,
487 enum v4l2_field field)
489 struct viu_fh *fh = vq->priv_data;
490 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
493 BUG_ON(fh->fmt == NULL);
495 if (fh->width < 48 || fh->width > norm_maxw() ||
496 fh->height < 32 || fh->height > norm_maxh())
498 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
499 if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size)
502 if (buf->fmt != fh->fmt ||
503 buf->vb.width != fh->width ||
504 buf->vb.height != fh->height ||
505 buf->vb.field != field) {
507 buf->vb.width = fh->width;
508 buf->vb.height = fh->height;
509 buf->vb.field = field;
512 if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
513 rc = videobuf_iolock(vq, &buf->vb, NULL);
517 buf->vb.width = fh->width;
518 buf->vb.height = fh->height;
519 buf->vb.field = field;
523 buf->vb.state = VIDEOBUF_PREPARED;
527 free_buffer(vq, buf);
531 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
533 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
534 struct viu_fh *fh = vq->priv_data;
535 struct viu_dev *dev = fh->dev;
536 struct viu_dmaqueue *vidq = &dev->vidq;
537 struct viu_buf *prev;
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",
543 vidq, &vidq->queued);
544 dprintk(1, "dev %p, queued: self %p, next %p, head %p\n",
545 dev, &vidq->queued, vidq->queued.next,
547 list_add_tail(&buf->vb.queue, &vidq->queued);
548 buf->vb.state = VIDEOBUF_QUEUED;
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);
554 list_add_tail(&buf->vb.queue, &vidq->active);
555 buf->vb.state = VIDEOBUF_ACTIVE;
556 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
557 dprintk(2, "[%p/%d] buffer_queue - first active\n",
560 buffer_activate(dev, buf);
562 dprintk(1, "adding vb queue2=0x%08lx\n",
563 (unsigned long)&buf->vb.queue);
564 prev = list_entry(vidq->active.prev, struct viu_buf, vb.queue);
565 if (prev->vb.width == buf->vb.width &&
566 prev->vb.height == buf->vb.height &&
567 prev->fmt == buf->fmt) {
568 list_add_tail(&buf->vb.queue, &vidq->active);
569 buf->vb.state = VIDEOBUF_ACTIVE;
570 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
573 list_add_tail(&buf->vb.queue, &vidq->queued);
574 buf->vb.state = VIDEOBUF_QUEUED;
575 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
581 static void buffer_release(struct videobuf_queue *vq,
582 struct videobuf_buffer *vb)
584 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
585 struct viu_fh *fh = vq->priv_data;
586 struct viu_dev *dev = (struct viu_dev *)fh->dev;
589 free_buffer(vq, buf);
592 static struct videobuf_queue_ops viu_video_qops = {
593 .buf_setup = buffer_setup,
594 .buf_prepare = buffer_prepare,
595 .buf_queue = buffer_queue,
596 .buf_release = buffer_release,
600 * IOCTL vidioc handling
602 static int vidioc_querycap(struct file *file, void *priv,
603 struct v4l2_capability *cap)
605 strcpy(cap->driver, "viu");
606 strcpy(cap->card, "viu");
607 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
609 V4L2_CAP_VIDEO_OVERLAY |
614 static int vidioc_enum_fmt(struct file *file, void *priv,
615 struct v4l2_fmtdesc *f)
617 int index = f->index;
619 if (f->index > NUM_FORMATS)
622 strlcpy(f->description, formats[index].name, sizeof(f->description));
623 f->pixelformat = formats[index].fourcc;
627 static int vidioc_g_fmt_cap(struct file *file, void *priv,
628 struct v4l2_format *f)
630 struct viu_fh *fh = priv;
632 f->fmt.pix.width = fh->width;
633 f->fmt.pix.height = fh->height;
634 f->fmt.pix.field = fh->vb_vidq.field;
635 f->fmt.pix.pixelformat = fh->fmt->pixelformat;
636 f->fmt.pix.bytesperline =
637 (f->fmt.pix.width * fh->fmt->depth) >> 3;
638 f->fmt.pix.sizeimage = fh->sizeimage;
642 static int vidioc_try_fmt_cap(struct file *file, void *priv,
643 struct v4l2_format *f)
646 enum v4l2_field field;
647 unsigned int maxw, maxh;
649 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
651 dprintk(1, "Fourcc format (0x%08x) invalid.",
652 f->fmt.pix.pixelformat);
656 field = f->fmt.pix.field;
658 if (field == V4L2_FIELD_ANY) {
659 field = V4L2_FIELD_INTERLACED;
660 } else if (field != V4L2_FIELD_INTERLACED) {
661 dprintk(1, "Field type invalid.\n");
668 f->fmt.pix.field = field;
669 if (f->fmt.pix.height < 32)
670 f->fmt.pix.height = 32;
671 if (f->fmt.pix.height > maxh)
672 f->fmt.pix.height = maxh;
673 if (f->fmt.pix.width < 48)
674 f->fmt.pix.width = 48;
675 if (f->fmt.pix.width > maxw)
676 f->fmt.pix.width = maxw;
677 f->fmt.pix.width &= ~0x03;
678 f->fmt.pix.bytesperline =
679 (f->fmt.pix.width * fmt->depth) >> 3;
684 static int vidioc_s_fmt_cap(struct file *file, void *priv,
685 struct v4l2_format *f)
687 struct viu_fh *fh = priv;
690 ret = vidioc_try_fmt_cap(file, fh, f);
694 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
695 fh->width = f->fmt.pix.width;
696 fh->height = f->fmt.pix.height;
697 fh->sizeimage = f->fmt.pix.sizeimage;
698 fh->vb_vidq.field = f->fmt.pix.field;
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,
705 struct v4l2_format *f)
707 struct viu_fh *fh = priv;
709 f->fmt.win = fh->win;
713 static int verify_preview(struct viu_dev *dev, struct v4l2_window *win)
715 enum v4l2_field field;
718 if (dev->ovbuf.base == NULL)
720 if (dev->ovfmt == NULL)
722 if (win->w.width < 48 || win->w.height < 32)
726 maxw = dev->crop_current.width;
727 maxh = dev->crop_current.height;
729 if (field == V4L2_FIELD_ANY) {
730 field = (win->w.height > maxh/2)
731 ? V4L2_FIELD_INTERLACED
736 case V4L2_FIELD_BOTTOM:
739 case V4L2_FIELD_INTERLACED:
746 if (win->w.width > maxw)
748 if (win->w.height > maxh)
749 win->w.height = maxh;
753 inline void viu_activate_overlay(struct viu_reg *viu_reg)
755 struct viu_reg *vr = viu_reg;
757 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
758 out_be32(&vr->dma_inc, reg_val.dma_inc);
759 out_be32(&vr->picture_count, reg_val.picture_count);
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);
769 reg_val.status_cfg = 0;
772 reg_val.picture_count = (fh->win.w.height / 2) << 16 |
775 /* setup color depth and dma increment */
776 bpp = dev->ovfmt->depth / 8;
779 reg_val.status_cfg &= ~MODE_32BIT;
780 reg_val.dma_inc = fh->win.w.width * 2;
783 reg_val.status_cfg |= MODE_32BIT;
784 reg_val.dma_inc = fh->win.w.width * 4;
787 dprintk(0, "device doesn't support color depth(%d)\n",
792 dev->ovfield = fh->win.field;
793 if (!V4L2_FIELD_HAS_BOTH(dev->ovfield))
796 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
798 /* setup the base address of the overlay buffer */
799 reg_val.field_base_addr = (u32)dev->ovbuf.base;
804 static int vidioc_s_fmt_overlay(struct file *file, void *priv,
805 struct v4l2_format *f)
807 struct viu_fh *fh = priv;
808 struct viu_dev *dev = (struct viu_dev *)fh->dev;
812 err = verify_preview(dev, &f->fmt.win);
816 fh->win = f->fmt.win;
818 spin_lock_irqsave(&dev->slock, flags);
819 viu_setup_preview(dev, fh);
820 spin_unlock_irqrestore(&dev->slock, flags);
824 static int vidioc_try_fmt_overlay(struct file *file, void *priv,
825 struct v4l2_format *f)
830 static int vidioc_overlay(struct file *file, void *priv, unsigned int on)
832 struct viu_fh *fh = priv;
833 struct viu_dev *dev = (struct viu_dev *)fh->dev;
837 spin_lock_irqsave(&dev->slock, flags);
838 viu_activate_overlay(dev->vr);
843 spin_unlock_irqrestore(&dev->slock, flags);
852 int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
854 struct viu_fh *fh = priv;
855 struct viu_dev *dev = fh->dev;
856 struct v4l2_framebuffer *fb = arg;
859 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
863 int vidioc_s_fbuf(struct file *file, void *priv, const struct v4l2_framebuffer *arg)
865 struct viu_fh *fh = priv;
866 struct viu_dev *dev = fh->dev;
867 const struct v4l2_framebuffer *fb = arg;
870 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
874 fmt = format_by_fourcc(fb->fmt.pixelformat);
881 if (dev->ovbuf.fmt.bytesperline == 0) {
882 dev->ovbuf.fmt.bytesperline =
883 dev->ovbuf.fmt.width * fmt->depth / 8;
888 static int vidioc_reqbufs(struct file *file, void *priv,
889 struct v4l2_requestbuffers *p)
891 struct viu_fh *fh = priv;
893 return videobuf_reqbufs(&fh->vb_vidq, p);
896 static int vidioc_querybuf(struct file *file, void *priv,
897 struct v4l2_buffer *p)
899 struct viu_fh *fh = priv;
901 return videobuf_querybuf(&fh->vb_vidq, p);
904 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
906 struct viu_fh *fh = priv;
908 return videobuf_qbuf(&fh->vb_vidq, p);
911 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
913 struct viu_fh *fh = priv;
915 return videobuf_dqbuf(&fh->vb_vidq, p,
916 file->f_flags & O_NONBLOCK);
919 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
921 struct viu_fh *fh = priv;
922 struct viu_dev *dev = fh->dev;
924 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
932 viu_start_dma(fh->dev);
934 return videobuf_streamon(&fh->vb_vidq);
937 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
939 struct viu_fh *fh = priv;
941 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
946 viu_stop_dma(fh->dev);
948 return videobuf_streamoff(&fh->vb_vidq);
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)
956 struct viu_fh *fh = priv;
958 decoder_call(fh->dev, video, querystd, std_id);
962 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
964 struct viu_fh *fh = priv;
967 decoder_call(fh->dev, video, s_std, id);
971 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
973 struct viu_fh *fh = priv;
975 *std_id = fh->dev->std;
979 /* only one input in this driver */
980 static int vidioc_enum_input(struct file *file, void *priv,
981 struct v4l2_input *inp)
983 struct viu_fh *fh = priv;
988 inp->type = V4L2_INPUT_TYPE_CAMERA;
989 inp->std = fh->dev->vdev->tvnorms;
990 strcpy(inp->name, "Camera");
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)
1002 struct viu_fh *fh = priv;
1007 decoder_call(fh->dev, video, s_routing, i, 0, 0);
1012 static int vidioc_queryctrl(struct file *file, void *priv,
1013 struct v4l2_queryctrl *qc)
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,
1027 struct v4l2_control *ctrl)
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,
1040 struct v4l2_control *ctrl)
1044 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1045 if (ctrl->id == viu_qctrl[i].id) {
1046 if (ctrl->value < viu_qctrl[i].minimum
1047 || ctrl->value > viu_qctrl[i].maximum)
1049 qctl_regs[i] = ctrl->value;
1056 inline void viu_activate_next_buf(struct viu_dev *dev,
1057 struct viu_dmaqueue *viuq)
1059 struct viu_dmaqueue *vidq = viuq;
1060 struct viu_buf *buf;
1062 /* launch another DMA operation for an active/queued buffer */
1063 if (!list_empty(&vidq->active)) {
1064 buf = list_entry(vidq->active.next, struct viu_buf,
1066 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1067 buffer_activate(dev, buf);
1068 } else if (!list_empty(&vidq->queued)) {
1069 buf = list_entry(vidq->queued.next, struct viu_buf,
1071 list_del(&buf->vb.queue);
1073 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1074 list_add_tail(&buf->vb.queue, &vidq->active);
1075 buf->vb.state = VIDEOBUF_ACTIVE;
1076 buffer_activate(dev, buf);
1080 inline void viu_default_settings(struct viu_reg *viu_reg)
1082 struct viu_reg *vr = viu_reg;
1084 out_be32(&vr->luminance, 0x9512A254);
1085 out_be32(&vr->chroma_r, 0x03310000);
1086 out_be32(&vr->chroma_g, 0x06600F38);
1087 out_be32(&vr->chroma_b, 0x00000409);
1088 out_be32(&vr->alpha, 0x000000ff);
1089 out_be32(&vr->req_alarm, 0x00000090);
1090 dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
1091 in_be32(&vr->status_cfg), in_be32(&vr->field_base_addr));
1094 static void viu_overlay_intr(struct viu_dev *dev, u32 status)
1096 struct viu_reg *vr = dev->vr;
1098 if (status & INT_DMA_END_STATUS)
1101 if (status & INT_FIELD_STATUS) {
1102 if (dev->dma_done) {
1103 u32 addr = reg_val.field_base_addr;
1106 if (status & FIELD_NO)
1107 addr += reg_val.dma_inc;
1109 out_be32(&vr->field_base_addr, addr);
1110 out_be32(&vr->dma_inc, reg_val.dma_inc);
1111 out_be32(&vr->status_cfg,
1112 (status & 0xffc0ffff) |
1113 (status & INT_ALL_STATUS) |
1114 reg_val.status_cfg);
1115 } else if (status & INT_VSYNC_STATUS) {
1116 out_be32(&vr->status_cfg,
1117 (status & 0xffc0ffff) |
1118 (status & INT_ALL_STATUS) |
1119 reg_val.status_cfg);
1124 static void viu_capture_intr(struct viu_dev *dev, u32 status)
1126 struct viu_dmaqueue *vidq = &dev->vidq;
1127 struct viu_reg *vr = dev->vr;
1128 struct viu_buf *buf;
1133 field_num = status & FIELD_NO;
1134 need_two = V4L2_FIELD_HAS_BOTH(dev->capfield);
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);
1146 if (unlikely(dev->first)) {
1147 if (field_num == 0) {
1149 dprintk(1, "activate first buf\n");
1150 viu_activate_next_buf(dev, vidq);
1152 dprintk(1, "wait field 0\n");
1156 /* setup buffer address for next dma operation */
1157 if (!list_empty(&vidq->active)) {
1158 u32 addr = reg_val.field_base_addr;
1160 if (field_num && need_two) {
1161 addr += reg_val.dma_inc;
1162 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1163 (unsigned long)addr, dev->field);
1165 out_be32(&vr->field_base_addr, addr);
1166 out_be32(&vr->dma_inc, reg_val.dma_inc);
1167 out_be32(&vr->status_cfg,
1168 (status & 0xffc0ffff) |
1169 (status & INT_ALL_STATUS) |
1170 reg_val.status_cfg);
1175 if (dma_done && field_num && (dev->field == 2)) {
1177 buf = list_entry(vidq->active.next,
1178 struct viu_buf, vb.queue);
1179 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1181 (unsigned long)videobuf_to_dma_contig(&buf->vb),
1182 (unsigned long)in_be32(&vr->field_base_addr));
1184 if (waitqueue_active(&buf->vb.done)) {
1185 list_del(&buf->vb.queue);
1186 v4l2_get_timestamp(&buf->vb.ts);
1187 buf->vb.state = VIDEOBUF_DONE;
1188 buf->vb.field_count++;
1189 wake_up(&buf->vb.done);
1191 /* activate next dma buffer */
1192 viu_activate_next_buf(dev, vidq);
1196 static irqreturn_t viu_intr(int irq, void *dev_id)
1198 struct viu_dev *dev = (struct viu_dev *)dev_id;
1199 struct viu_reg *vr = dev->vr;
1203 status = in_be32(&vr->status_cfg);
1205 if (status & INT_ERROR_STATUS) {
1206 dev->irqs.error_irq++;
1207 error = status & ERR_MASK;
1209 dprintk(1, "Err: error(%d), times:%d!\n",
1210 error >> 4, dev->irqs.error_irq);
1211 /* Clear interrupt error bit and error flags */
1212 out_be32(&vr->status_cfg,
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);
1223 if (status & INT_HSYNC_STATUS)
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);
1232 if (status & INT_VSTART_STATUS)
1233 dev->irqs.vstart_irq++;
1235 if (status & INT_VSYNC_STATUS) {
1236 dev->irqs.vsync_irq++;
1237 dprintk(2, "VIU vsync interrupt times: %d\n",
1238 dev->irqs.vsync_irq);
1241 /* clear all pending irqs */
1242 status = in_be32(&vr->status_cfg);
1243 out_be32(&vr->status_cfg,
1244 (status & 0xffc0ffff) | (status & INT_ALL_STATUS));
1246 if (dev->ovenable) {
1247 viu_overlay_intr(dev, status);
1252 viu_capture_intr(dev, status);
1257 * File operations for the device
1259 static int viu_open(struct file *file)
1261 struct video_device *vdev = video_devdata(file);
1262 struct viu_dev *dev = video_get_drvdata(vdev);
1265 int minor = vdev->minor;
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,
1280 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1282 if (mutex_lock_interruptible(&dev->lock)) {
1284 return -ERESTARTSYS;
1287 /* allocate and initialize per filehandle data */
1288 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1291 mutex_unlock(&dev->lock);
1295 file->private_data = fh;
1298 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1299 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_RGB32);
1300 fh->width = norm_maxw();
1301 fh->height = norm_maxh();
1302 dev->crop_current.width = fh->width;
1303 dev->crop_current.height = fh->height;
1305 /* Put all controls at a sane state */
1306 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++)
1307 qctl_regs[i] = viu_qctrl[i].default_value;
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));
1317 viu_default_settings(vr);
1319 status_cfg = in_be32(&vr->status_cfg);
1320 out_be32(&vr->status_cfg,
1321 status_cfg & ~(INT_VSYNC_EN | INT_HSYNC_EN |
1322 INT_FIELD_EN | INT_VSTART_EN |
1323 INT_DMA_END_EN | INT_ERROR_EN | INT_ECC_EN));
1325 status_cfg = in_be32(&vr->status_cfg);
1326 out_be32(&vr->status_cfg, status_cfg | INT_ALL_STATUS);
1328 spin_lock_init(&fh->vbq_lock);
1329 videobuf_queue_dma_contig_init(&fh->vb_vidq, &viu_video_qops,
1330 dev->dev, &fh->vbq_lock,
1331 fh->type, V4L2_FIELD_INTERLACED,
1332 sizeof(struct viu_buf), fh,
1334 mutex_unlock(&dev->lock);
1338 static ssize_t viu_read(struct file *file, char __user *data, size_t count,
1341 struct viu_fh *fh = file->private_data;
1342 struct viu_dev *dev = fh->dev;
1345 dprintk(2, "%s\n", __func__);
1349 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1350 if (mutex_lock_interruptible(&dev->lock))
1351 return -ERESTARTSYS;
1353 ret = videobuf_read_stream(&fh->vb_vidq, data, count,
1354 ppos, 0, file->f_flags & O_NONBLOCK);
1355 mutex_unlock(&dev->lock);
1361 static unsigned int viu_poll(struct file *file, struct poll_table_struct *wait)
1363 struct viu_fh *fh = file->private_data;
1364 struct videobuf_queue *q = &fh->vb_vidq;
1365 struct viu_dev *dev = fh->dev;
1368 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1371 mutex_lock(&dev->lock);
1372 res = videobuf_poll_stream(file, q, wait);
1373 mutex_unlock(&dev->lock);
1377 static int viu_release(struct file *file)
1379 struct viu_fh *fh = file->private_data;
1380 struct viu_dev *dev = fh->dev;
1381 int minor = video_devdata(file)->minor;
1383 mutex_lock(&dev->lock);
1385 videobuf_stop(&fh->vb_vidq);
1386 videobuf_mmap_free(&fh->vb_vidq);
1387 mutex_unlock(&dev->lock);
1392 dprintk(1, "close (minor=%d, users=%d)\n",
1397 void viu_reset(struct viu_reg *reg)
1399 out_be32(®->status_cfg, 0);
1400 out_be32(®->luminance, 0x9512a254);
1401 out_be32(®->chroma_r, 0x03310000);
1402 out_be32(®->chroma_g, 0x06600f38);
1403 out_be32(®->chroma_b, 0x00000409);
1404 out_be32(®->field_base_addr, 0);
1405 out_be32(®->dma_inc, 0);
1406 out_be32(®->picture_count, 0x01e002d0);
1407 out_be32(®->req_alarm, 0x00000090);
1408 out_be32(®->alpha, 0x000000ff);
1411 static int viu_mmap(struct file *file, struct vm_area_struct *vma)
1413 struct viu_fh *fh = file->private_data;
1414 struct viu_dev *dev = fh->dev;
1417 dprintk(1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1419 if (mutex_lock_interruptible(&dev->lock))
1420 return -ERESTARTSYS;
1421 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1422 mutex_unlock(&dev->lock);
1424 dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1425 (unsigned long)vma->vm_start,
1426 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1432 static struct v4l2_file_operations viu_fops = {
1433 .owner = THIS_MODULE,
1435 .release = viu_release,
1438 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1442 static const struct v4l2_ioctl_ops viu_ioctl_ops = {
1443 .vidioc_querycap = vidioc_querycap,
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,
1452 .vidioc_overlay = vidioc_overlay,
1453 .vidioc_g_fbuf = vidioc_g_fbuf,
1454 .vidioc_s_fbuf = vidioc_s_fbuf,
1455 .vidioc_reqbufs = vidioc_reqbufs,
1456 .vidioc_querybuf = vidioc_querybuf,
1457 .vidioc_qbuf = vidioc_qbuf,
1458 .vidioc_dqbuf = vidioc_dqbuf,
1459 .vidioc_g_std = vidioc_g_std,
1460 .vidioc_s_std = vidioc_s_std,
1461 .vidioc_querystd = vidioc_querystd,
1462 .vidioc_enum_input = vidioc_enum_input,
1463 .vidioc_g_input = vidioc_g_input,
1464 .vidioc_s_input = vidioc_s_input,
1465 .vidioc_queryctrl = vidioc_queryctrl,
1466 .vidioc_g_ctrl = vidioc_g_ctrl,
1467 .vidioc_s_ctrl = vidioc_s_ctrl,
1468 .vidioc_streamon = vidioc_streamon,
1469 .vidioc_streamoff = vidioc_streamoff,
1472 static struct video_device viu_template = {
1476 .ioctl_ops = &viu_ioctl_ops,
1477 .release = video_device_release,
1479 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1482 static int viu_of_probe(struct platform_device *op)
1484 struct viu_dev *viu_dev;
1485 struct video_device *vdev;
1487 struct viu_reg __iomem *viu_regs;
1488 struct i2c_adapter *ad;
1492 ret = of_address_to_resource(op->dev.of_node, 0, &r);
1494 dev_err(&op->dev, "Can't parse device node resource\n");
1498 viu_irq = irq_of_parse_and_map(op->dev.of_node, 0);
1499 if (viu_irq == NO_IRQ) {
1500 dev_err(&op->dev, "Error while mapping the irq\n");
1504 /* request mem region */
1505 if (!devm_request_mem_region(&op->dev, r.start,
1506 sizeof(struct viu_reg), DRV_NAME)) {
1507 dev_err(&op->dev, "Error while requesting mem region\n");
1512 /* remap registers */
1513 viu_regs = devm_ioremap(&op->dev, r.start, sizeof(struct viu_reg));
1515 dev_err(&op->dev, "Can't map register set\n");
1520 /* Prepare our private structure */
1521 viu_dev = devm_kzalloc(&op->dev, sizeof(struct viu_dev), GFP_ATOMIC);
1523 dev_err(&op->dev, "Can't allocate private structure\n");
1528 viu_dev->vr = viu_regs;
1529 viu_dev->irq = viu_irq;
1530 viu_dev->dev = &op->dev;
1532 /* init video dma queues */
1533 INIT_LIST_HEAD(&viu_dev->vidq.active);
1534 INIT_LIST_HEAD(&viu_dev->vidq.queued);
1536 snprintf(viu_dev->v4l2_dev.name,
1537 sizeof(viu_dev->v4l2_dev.name), "%s", "VIU");
1538 ret = v4l2_device_register(viu_dev->dev, &viu_dev->v4l2_dev);
1540 dev_err(&op->dev, "v4l2_device_register() failed: %d\n", ret);
1544 ad = i2c_get_adapter(0);
1545 viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
1546 "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
1548 viu_dev->vidq.timeout.function = viu_vid_timeout;
1549 viu_dev->vidq.timeout.data = (unsigned long)viu_dev;
1550 init_timer(&viu_dev->vidq.timeout);
1551 viu_dev->std = V4L2_STD_NTSC_M;
1554 /* Allocate memory for video device */
1555 vdev = video_device_alloc();
1561 memcpy(vdev, &viu_template, sizeof(viu_template));
1563 vdev->v4l2_dev = &viu_dev->v4l2_dev;
1565 viu_dev->vdev = vdev;
1567 /* initialize locks */
1568 mutex_init(&viu_dev->lock);
1569 viu_dev->vdev->lock = &viu_dev->lock;
1570 spin_lock_init(&viu_dev->slock);
1572 video_set_drvdata(viu_dev->vdev, viu_dev);
1574 mutex_lock(&viu_dev->lock);
1576 ret = video_register_device(viu_dev->vdev, VFL_TYPE_GRABBER, -1);
1578 video_device_release(viu_dev->vdev);
1582 /* enable VIU clock */
1583 clk = devm_clk_get(&op->dev, "ipg");
1585 dev_err(&op->dev, "failed to lookup the clock!\n");
1589 ret = clk_prepare_enable(clk);
1591 dev_err(&op->dev, "failed to enable the clock!\n");
1596 /* reset VIU module */
1597 viu_reset(viu_dev->vr);
1599 /* install interrupt handler */
1600 if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
1601 dev_err(&op->dev, "Request VIU IRQ failed.\n");
1606 mutex_unlock(&viu_dev->lock);
1608 dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
1612 clk_disable_unprepare(viu_dev->clk);
1614 video_unregister_device(viu_dev->vdev);
1616 mutex_unlock(&viu_dev->lock);
1617 i2c_put_adapter(ad);
1618 v4l2_device_unregister(&viu_dev->v4l2_dev);
1620 irq_dispose_mapping(viu_irq);
1624 static int viu_of_remove(struct platform_device *op)
1626 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1627 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1628 struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
1629 struct v4l2_subdev, list);
1630 struct i2c_client *client = v4l2_get_subdevdata(sdev);
1632 free_irq(dev->irq, (void *)dev);
1633 irq_dispose_mapping(dev->irq);
1635 clk_disable_unprepare(dev->clk);
1637 video_unregister_device(dev->vdev);
1638 i2c_put_adapter(client->adapter);
1639 v4l2_device_unregister(&dev->v4l2_dev);
1644 static int viu_suspend(struct platform_device *op, pm_message_t state)
1646 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1647 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1649 clk_disable(dev->clk);
1653 static int viu_resume(struct platform_device *op)
1655 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1656 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1658 clk_enable(dev->clk);
1664 * Initialization and module stuff
1666 static struct of_device_id mpc512x_viu_of_match[] = {
1668 .compatible = "fsl,mpc5121-viu",
1672 MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
1674 static struct platform_driver viu_of_platform_driver = {
1675 .probe = viu_of_probe,
1676 .remove = viu_of_remove,
1678 .suspend = viu_suspend,
1679 .resume = viu_resume,
1683 .owner = THIS_MODULE,
1684 .of_match_table = mpc512x_viu_of_match,
1688 module_platform_driver(viu_of_platform_driver);
1690 MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1691 MODULE_AUTHOR("Hongjun Chen");
1692 MODULE_LICENSE("GPL");
1693 MODULE_VERSION(VIU_VERSION);