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_platform.h>
25 #include <linux/slab.h>
26 #include <linux/version.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/videobuf-dma-contig.h>
32 #define DRV_NAME "fsl_viu"
33 #define VIU_MAJOR_VERSION 0
34 #define VIU_MINOR_VERSION 5
36 #define VIU_VERSION KERNEL_VERSION(VIU_MAJOR_VERSION, \
40 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
42 #define VIU_VID_MEM_LIMIT 4 /* Video memory limit, in Mb */
44 /* I2C address of video decoder chip is 0x4A */
45 #define VIU_VIDEO_DECODER_ADDR 0x25
47 /* supported controls */
48 static struct v4l2_queryctrl viu_qctrl[] = {
50 .id = V4L2_CID_BRIGHTNESS,
51 .type = V4L2_CTRL_TYPE_INTEGER,
59 .id = V4L2_CID_CONTRAST,
60 .type = V4L2_CTRL_TYPE_INTEGER,
65 .default_value = 0x10,
68 .id = V4L2_CID_SATURATION,
69 .type = V4L2_CTRL_TYPE_INTEGER,
78 .type = V4L2_CTRL_TYPE_INTEGER,
88 static int qctl_regs[ARRAY_SIZE(viu_qctrl)];
90 static int info_level;
92 #define dprintk(level, fmt, arg...) \
94 if (level <= info_level) \
95 printk(KERN_DEBUG "viu: " fmt , ## arg); \
103 u32 fourcc; /* v4l2 format id */
108 static struct viu_fmt formats[] = {
110 .name = "RGB-16 (5/B-6/G-5/R)",
111 .fourcc = V4L2_PIX_FMT_RGB565,
112 .pixelformat = V4L2_PIX_FMT_RGB565,
115 .name = "RGB-32 (A-R-G-B)",
116 .fourcc = V4L2_PIX_FMT_RGB32,
117 .pixelformat = V4L2_PIX_FMT_RGB32,
125 /* buffer for one video frame */
127 /* common v4l buffer stuff -- must be first */
128 struct videobuf_buffer vb;
132 struct viu_dmaqueue {
134 struct list_head active;
135 struct list_head queued;
136 struct timer_list timeout;
159 } __attribute__ ((packed));
162 struct v4l2_device v4l2_dev;
168 /* various device info */
169 struct video_device *vdev;
170 struct viu_dmaqueue vidq;
171 enum v4l2_field capfield;
176 /* Hardware register area */
179 /* Interrupt vector */
181 struct viu_status irqs;
184 struct v4l2_framebuffer ovbuf;
185 struct viu_fmt *ovfmt;
186 unsigned int ovenable;
187 enum v4l2_field ovfield;
190 struct v4l2_rect crop_current;
196 struct v4l2_subdev *decoder;
203 struct videobuf_queue vb_vidq;
204 spinlock_t vbq_lock; /* spinlock for the videobuf queue */
207 struct v4l2_window win;
208 struct v4l2_clip clips[1];
212 int width, height, sizeimage;
213 enum v4l2_buf_type type;
216 static struct viu_reg reg_val;
219 * Macro definitions of VIU registers
222 /* STATUS_CONFIG register */
226 ERR_MASK = 0x0f << 4, /* Error code mask */
227 ERR_NO = 0x00, /* No error */
228 ERR_DMA_V = 0x01 << 4, /* DMA in vertical active */
229 ERR_DMA_VB = 0x02 << 4, /* DMA in vertical blanking */
230 ERR_LINE_TOO_LONG = 0x04 << 4, /* Line too long */
231 ERR_TOO_MANG_LINES = 0x05 << 4, /* Too many lines in field */
232 ERR_LINE_TOO_SHORT = 0x06 << 4, /* Line too short */
233 ERR_NOT_ENOUGH_LINE = 0x07 << 4, /* Not enough lines in field */
234 ERR_FIFO_OVERFLOW = 0x08 << 4, /* FIFO overflow */
235 ERR_FIFO_UNDERFLOW = 0x09 << 4, /* FIFO underflow */
236 ERR_1bit_ECC = 0x0a << 4, /* One bit ECC error */
237 ERR_MORE_ECC = 0x0b << 4, /* Two/more bits ECC error */
239 INT_FIELD_EN = 0x01 << 8, /* Enable field interrupt */
240 INT_VSYNC_EN = 0x01 << 9, /* Enable vsync interrupt */
241 INT_HSYNC_EN = 0x01 << 10, /* Enable hsync interrupt */
242 INT_VSTART_EN = 0x01 << 11, /* Enable vstart interrupt */
243 INT_DMA_END_EN = 0x01 << 12, /* Enable DMA end interrupt */
244 INT_ERROR_EN = 0x01 << 13, /* Enable error interrupt */
245 INT_ECC_EN = 0x01 << 14, /* Enable ECC interrupt */
247 INT_FIELD_STATUS = 0x01 << 16, /* field interrupt status */
248 INT_VSYNC_STATUS = 0x01 << 17, /* vsync interrupt status */
249 INT_HSYNC_STATUS = 0x01 << 18, /* hsync interrupt status */
250 INT_VSTART_STATUS = 0x01 << 19, /* vstart interrupt status */
251 INT_DMA_END_STATUS = 0x01 << 20, /* DMA end interrupt status */
252 INT_ERROR_STATUS = 0x01 << 21, /* error interrupt status */
254 DMA_ACT = 0x01 << 27, /* Enable DMA transfer */
255 FIELD_NO = 0x01 << 28, /* Field number */
256 DITHER_ON = 0x01 << 29, /* Dithering is on */
257 ROUND_ON = 0x01 << 30, /* Round is on */
258 MODE_32BIT = 0x01 << 31, /* Data in RGBa888,
263 #define norm_maxw() 720
264 #define norm_maxh() 576
266 #define INT_ALL_STATUS (INT_FIELD_STATUS | INT_VSYNC_STATUS | \
267 INT_HSYNC_STATUS | INT_VSTART_STATUS | \
268 INT_DMA_END_STATUS | INT_ERROR_STATUS)
270 #define NUM_FORMATS ARRAY_SIZE(formats)
272 static irqreturn_t viu_intr(int irq, void *dev_id);
274 struct viu_fmt *format_by_fourcc(int fourcc)
278 for (i = 0; i < NUM_FORMATS; i++) {
279 if (formats[i].pixelformat == fourcc)
283 dprintk(0, "unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
287 void viu_start_dma(struct viu_dev *dev)
289 struct viu_reg *vr = dev->vr;
293 /* Enable DMA operation */
294 out_be32(&vr->status_cfg, SOFT_RST);
295 out_be32(&vr->status_cfg, INT_FIELD_EN);
298 void viu_stop_dma(struct viu_dev *dev)
300 struct viu_reg *vr = dev->vr;
304 out_be32(&vr->status_cfg, 0);
306 /* Clear pending interrupts */
307 status_cfg = in_be32(&vr->status_cfg);
308 if (status_cfg & 0x3f0000)
309 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
311 if (status_cfg & DMA_ACT) {
313 status_cfg = in_be32(&vr->status_cfg);
314 if (status_cfg & INT_DMA_END_STATUS)
319 /* timed out, issue soft reset */
320 out_be32(&vr->status_cfg, SOFT_RST);
321 out_be32(&vr->status_cfg, 0);
323 /* clear DMA_END and other pending irqs */
324 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
331 static int restart_video_queue(struct viu_dmaqueue *vidq)
333 struct viu_buf *buf, *prev;
335 dprintk(1, "%s vidq=0x%08lx\n", __func__, (unsigned long)vidq);
336 if (!list_empty(&vidq->active)) {
337 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
338 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
341 viu_stop_dma(vidq->dev);
343 /* cancel all outstanding capture requests */
344 list_for_each_entry_safe(buf, prev, &vidq->active, vb.queue) {
345 list_del(&buf->vb.queue);
346 buf->vb.state = VIDEOBUF_ERROR;
347 wake_up(&buf->vb.done);
349 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
355 if (list_empty(&vidq->queued))
357 buf = list_entry(vidq->queued.next, struct viu_buf, vb.queue);
359 list_del(&buf->vb.queue);
360 list_add_tail(&buf->vb.queue, &vidq->active);
362 dprintk(1, "Restarting video dma\n");
363 viu_stop_dma(vidq->dev);
364 viu_start_dma(vidq->dev);
366 buf->vb.state = VIDEOBUF_ACTIVE;
367 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
368 dprintk(2, "[%p/%d] restart_queue - first active\n",
371 } else if (prev->vb.width == buf->vb.width &&
372 prev->vb.height == buf->vb.height &&
373 prev->fmt == buf->fmt) {
374 list_del(&buf->vb.queue);
375 list_add_tail(&buf->vb.queue, &vidq->active);
376 buf->vb.state = VIDEOBUF_ACTIVE;
377 dprintk(2, "[%p/%d] restart_queue - move to active\n",
386 static void viu_vid_timeout(unsigned long data)
388 struct viu_dev *dev = (struct viu_dev *)data;
390 struct viu_dmaqueue *vidq = &dev->vidq;
392 while (!list_empty(&vidq->active)) {
393 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
394 list_del(&buf->vb.queue);
395 buf->vb.state = VIDEOBUF_ERROR;
396 wake_up(&buf->vb.done);
397 dprintk(1, "viu/0: [%p/%d] timeout\n", buf, buf->vb.i);
400 restart_video_queue(vidq);
404 * Videobuf operations
406 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
409 struct viu_fh *fh = vq->priv_data;
411 *size = fh->width * fh->height * fh->fmt->depth >> 3;
415 while (*size * *count > VIU_VID_MEM_LIMIT * 1024 * 1024)
418 dprintk(1, "%s, count=%d, size=%d\n", __func__, *count, *size);
422 static void free_buffer(struct videobuf_queue *vq, struct viu_buf *buf)
424 struct videobuf_buffer *vb = &buf->vb;
427 BUG_ON(in_interrupt());
429 videobuf_waiton(vq, &buf->vb, 0, 0);
431 if (vq->int_ops && vq->int_ops->vaddr)
432 vaddr = vq->int_ops->vaddr(vb);
435 videobuf_dma_contig_free(vq, &buf->vb);
437 buf->vb.state = VIDEOBUF_NEEDS_INIT;
440 inline int buffer_activate(struct viu_dev *dev, struct viu_buf *buf)
442 struct viu_reg *vr = dev->vr;
445 /* setup the DMA base address */
446 reg_val.field_base_addr = videobuf_to_dma_contig(&buf->vb);
448 dprintk(1, "buffer_activate [%p/%d]: dma addr 0x%lx\n",
449 buf, buf->vb.i, (unsigned long)reg_val.field_base_addr);
451 /* interlace is on by default, set horizontal DMA increment */
452 reg_val.status_cfg = 0;
453 bpp = buf->fmt->depth >> 3;
456 reg_val.status_cfg &= ~MODE_32BIT;
457 reg_val.dma_inc = buf->vb.width * 2;
460 reg_val.status_cfg |= MODE_32BIT;
461 reg_val.dma_inc = buf->vb.width * 4;
464 dprintk(0, "doesn't support color depth(%d)\n",
469 /* setup picture_count register */
470 reg_val.picture_count = (buf->vb.height / 2) << 16 |
473 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
475 buf->vb.state = VIDEOBUF_ACTIVE;
476 dev->capfield = buf->vb.field;
478 /* reset dma increment if needed */
479 if (!V4L2_FIELD_HAS_BOTH(buf->vb.field))
482 out_be32(&vr->dma_inc, reg_val.dma_inc);
483 out_be32(&vr->picture_count, reg_val.picture_count);
484 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
485 mod_timer(&dev->vidq.timeout, jiffies + BUFFER_TIMEOUT);
489 static int buffer_prepare(struct videobuf_queue *vq,
490 struct videobuf_buffer *vb,
491 enum v4l2_field field)
493 struct viu_fh *fh = vq->priv_data;
494 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
497 BUG_ON(fh->fmt == NULL);
499 if (fh->width < 48 || fh->width > norm_maxw() ||
500 fh->height < 32 || fh->height > norm_maxh())
502 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
503 if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size)
506 if (buf->fmt != fh->fmt ||
507 buf->vb.width != fh->width ||
508 buf->vb.height != fh->height ||
509 buf->vb.field != field) {
511 buf->vb.width = fh->width;
512 buf->vb.height = fh->height;
513 buf->vb.field = field;
516 if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
517 rc = videobuf_iolock(vq, &buf->vb, NULL);
521 buf->vb.width = fh->width;
522 buf->vb.height = fh->height;
523 buf->vb.field = field;
527 buf->vb.state = VIDEOBUF_PREPARED;
531 free_buffer(vq, buf);
535 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
537 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
538 struct viu_fh *fh = vq->priv_data;
539 struct viu_dev *dev = fh->dev;
540 struct viu_dmaqueue *vidq = &dev->vidq;
541 struct viu_buf *prev;
543 if (!list_empty(&vidq->queued)) {
544 dprintk(1, "adding vb queue=0x%08lx\n",
545 (unsigned long)&buf->vb.queue);
546 dprintk(1, "vidq pointer 0x%p, queued 0x%p\n",
547 vidq, &vidq->queued);
548 dprintk(1, "dev %p, queued: self %p, next %p, head %p\n",
549 dev, &vidq->queued, vidq->queued.next,
551 list_add_tail(&buf->vb.queue, &vidq->queued);
552 buf->vb.state = VIDEOBUF_QUEUED;
553 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
555 } else if (list_empty(&vidq->active)) {
556 dprintk(1, "adding vb active=0x%08lx\n",
557 (unsigned long)&buf->vb.queue);
558 list_add_tail(&buf->vb.queue, &vidq->active);
559 buf->vb.state = VIDEOBUF_ACTIVE;
560 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
561 dprintk(2, "[%p/%d] buffer_queue - first active\n",
564 buffer_activate(dev, buf);
566 dprintk(1, "adding vb queue2=0x%08lx\n",
567 (unsigned long)&buf->vb.queue);
568 prev = list_entry(vidq->active.prev, struct viu_buf, vb.queue);
569 if (prev->vb.width == buf->vb.width &&
570 prev->vb.height == buf->vb.height &&
571 prev->fmt == buf->fmt) {
572 list_add_tail(&buf->vb.queue, &vidq->active);
573 buf->vb.state = VIDEOBUF_ACTIVE;
574 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
577 list_add_tail(&buf->vb.queue, &vidq->queued);
578 buf->vb.state = VIDEOBUF_QUEUED;
579 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
585 static void buffer_release(struct videobuf_queue *vq,
586 struct videobuf_buffer *vb)
588 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
589 struct viu_fh *fh = vq->priv_data;
590 struct viu_dev *dev = (struct viu_dev *)fh->dev;
593 free_buffer(vq, buf);
596 static struct videobuf_queue_ops viu_video_qops = {
597 .buf_setup = buffer_setup,
598 .buf_prepare = buffer_prepare,
599 .buf_queue = buffer_queue,
600 .buf_release = buffer_release,
604 * IOCTL vidioc handling
606 static int vidioc_querycap(struct file *file, void *priv,
607 struct v4l2_capability *cap)
609 strcpy(cap->driver, "viu");
610 strcpy(cap->card, "viu");
611 cap->version = VIU_VERSION;
612 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
614 V4L2_CAP_VIDEO_OVERLAY |
619 static int vidioc_enum_fmt(struct file *file, void *priv,
620 struct v4l2_fmtdesc *f)
622 int index = f->index;
624 if (f->index > NUM_FORMATS)
627 strlcpy(f->description, formats[index].name, sizeof(f->description));
628 f->pixelformat = formats[index].fourcc;
632 static int vidioc_g_fmt_cap(struct file *file, void *priv,
633 struct v4l2_format *f)
635 struct viu_fh *fh = priv;
637 f->fmt.pix.width = fh->width;
638 f->fmt.pix.height = fh->height;
639 f->fmt.pix.field = fh->vb_vidq.field;
640 f->fmt.pix.pixelformat = fh->fmt->pixelformat;
641 f->fmt.pix.bytesperline =
642 (f->fmt.pix.width * fh->fmt->depth) >> 3;
643 f->fmt.pix.sizeimage = fh->sizeimage;
647 static int vidioc_try_fmt_cap(struct file *file, void *priv,
648 struct v4l2_format *f)
651 enum v4l2_field field;
652 unsigned int maxw, maxh;
654 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
656 dprintk(1, "Fourcc format (0x%08x) invalid.",
657 f->fmt.pix.pixelformat);
661 field = f->fmt.pix.field;
663 if (field == V4L2_FIELD_ANY) {
664 field = V4L2_FIELD_INTERLACED;
665 } else if (field != V4L2_FIELD_INTERLACED) {
666 dprintk(1, "Field type invalid.\n");
673 f->fmt.pix.field = field;
674 if (f->fmt.pix.height < 32)
675 f->fmt.pix.height = 32;
676 if (f->fmt.pix.height > maxh)
677 f->fmt.pix.height = maxh;
678 if (f->fmt.pix.width < 48)
679 f->fmt.pix.width = 48;
680 if (f->fmt.pix.width > maxw)
681 f->fmt.pix.width = maxw;
682 f->fmt.pix.width &= ~0x03;
683 f->fmt.pix.bytesperline =
684 (f->fmt.pix.width * fmt->depth) >> 3;
689 static int vidioc_s_fmt_cap(struct file *file, void *priv,
690 struct v4l2_format *f)
692 struct viu_fh *fh = priv;
695 ret = vidioc_try_fmt_cap(file, fh, f);
699 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
700 fh->width = f->fmt.pix.width;
701 fh->height = f->fmt.pix.height;
702 fh->sizeimage = f->fmt.pix.sizeimage;
703 fh->vb_vidq.field = f->fmt.pix.field;
705 dprintk(1, "set to pixelformat '%4.6s'\n", (char *)&fh->fmt->name);
709 static int vidioc_g_fmt_overlay(struct file *file, void *priv,
710 struct v4l2_format *f)
712 struct viu_fh *fh = priv;
714 f->fmt.win = fh->win;
718 static int verify_preview(struct viu_dev *dev, struct v4l2_window *win)
720 enum v4l2_field field;
723 if (dev->ovbuf.base == NULL)
725 if (dev->ovfmt == NULL)
727 if (win->w.width < 48 || win->w.height < 32)
731 maxw = dev->crop_current.width;
732 maxh = dev->crop_current.height;
734 if (field == V4L2_FIELD_ANY) {
735 field = (win->w.height > maxh/2)
736 ? V4L2_FIELD_INTERLACED
741 case V4L2_FIELD_BOTTOM:
744 case V4L2_FIELD_INTERLACED:
751 if (win->w.width > maxw)
753 if (win->w.height > maxh)
754 win->w.height = maxh;
758 inline void viu_activate_overlay(struct viu_reg *viu_reg)
760 struct viu_reg *vr = viu_reg;
762 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
763 out_be32(&vr->dma_inc, reg_val.dma_inc);
764 out_be32(&vr->picture_count, reg_val.picture_count);
767 static int viu_start_preview(struct viu_dev *dev, struct viu_fh *fh)
771 dprintk(1, "%s %dx%d %s\n", __func__,
772 fh->win.w.width, fh->win.w.height, dev->ovfmt->name);
774 reg_val.status_cfg = 0;
777 reg_val.picture_count = (fh->win.w.height / 2) << 16 |
780 /* setup color depth and dma increment */
781 bpp = dev->ovfmt->depth / 8;
784 reg_val.status_cfg &= ~MODE_32BIT;
785 reg_val.dma_inc = fh->win.w.width * 2;
788 reg_val.status_cfg |= MODE_32BIT;
789 reg_val.dma_inc = fh->win.w.width * 4;
792 dprintk(0, "device doesn't support color depth(%d)\n",
797 dev->ovfield = fh->win.field;
798 if (!V4L2_FIELD_HAS_BOTH(dev->ovfield))
801 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
803 /* setup the base address of the overlay buffer */
804 reg_val.field_base_addr = (u32)dev->ovbuf.base;
807 viu_activate_overlay(dev->vr);
814 static int vidioc_s_fmt_overlay(struct file *file, void *priv,
815 struct v4l2_format *f)
817 struct viu_fh *fh = priv;
818 struct viu_dev *dev = (struct viu_dev *)fh->dev;
822 err = verify_preview(dev, &f->fmt.win);
826 mutex_lock(&dev->lock);
827 fh->win = f->fmt.win;
829 spin_lock_irqsave(&dev->slock, flags);
830 viu_start_preview(dev, fh);
831 spin_unlock_irqrestore(&dev->slock, flags);
832 mutex_unlock(&dev->lock);
836 static int vidioc_try_fmt_overlay(struct file *file, void *priv,
837 struct v4l2_format *f)
842 int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
844 struct viu_fh *fh = priv;
845 struct viu_dev *dev = fh->dev;
846 struct v4l2_framebuffer *fb = arg;
849 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
853 int vidioc_s_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
855 struct viu_fh *fh = priv;
856 struct viu_dev *dev = fh->dev;
857 struct v4l2_framebuffer *fb = arg;
860 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
864 fmt = format_by_fourcc(fb->fmt.pixelformat);
871 if (dev->ovbuf.fmt.bytesperline == 0) {
872 dev->ovbuf.fmt.bytesperline =
873 dev->ovbuf.fmt.width * fmt->depth / 8;
878 static int vidioc_reqbufs(struct file *file, void *priv,
879 struct v4l2_requestbuffers *p)
881 struct viu_fh *fh = priv;
883 return videobuf_reqbufs(&fh->vb_vidq, p);
886 static int vidioc_querybuf(struct file *file, void *priv,
887 struct v4l2_buffer *p)
889 struct viu_fh *fh = priv;
891 return videobuf_querybuf(&fh->vb_vidq, p);
894 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
896 struct viu_fh *fh = priv;
898 return videobuf_qbuf(&fh->vb_vidq, p);
901 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
903 struct viu_fh *fh = priv;
905 return videobuf_dqbuf(&fh->vb_vidq, p,
906 file->f_flags & O_NONBLOCK);
909 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
911 struct viu_fh *fh = priv;
913 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
918 viu_start_dma(fh->dev);
920 return videobuf_streamon(&fh->vb_vidq);
923 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
925 struct viu_fh *fh = priv;
927 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
932 viu_stop_dma(fh->dev);
934 return videobuf_streamoff(&fh->vb_vidq);
937 #define decoder_call(viu, o, f, args...) \
938 v4l2_subdev_call(viu->decoder, o, f, ##args)
940 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
942 struct viu_fh *fh = priv;
944 decoder_call(fh->dev, core, s_std, *id);
948 /* only one input in this driver */
949 static int vidioc_enum_input(struct file *file, void *priv,
950 struct v4l2_input *inp)
952 struct viu_fh *fh = priv;
957 inp->type = V4L2_INPUT_TYPE_CAMERA;
958 inp->std = fh->dev->vdev->tvnorms;
959 strcpy(inp->name, "Camera");
963 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
969 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
971 struct viu_fh *fh = priv;
976 decoder_call(fh->dev, video, s_routing, i, 0, 0);
981 static int vidioc_queryctrl(struct file *file, void *priv,
982 struct v4l2_queryctrl *qc)
986 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
987 if (qc->id && qc->id == viu_qctrl[i].id) {
988 memcpy(qc, &(viu_qctrl[i]), sizeof(*qc));
995 static int vidioc_g_ctrl(struct file *file, void *priv,
996 struct v4l2_control *ctrl)
1000 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1001 if (ctrl->id == viu_qctrl[i].id) {
1002 ctrl->value = qctl_regs[i];
1008 static int vidioc_s_ctrl(struct file *file, void *priv,
1009 struct v4l2_control *ctrl)
1013 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1014 if (ctrl->id == viu_qctrl[i].id) {
1015 if (ctrl->value < viu_qctrl[i].minimum
1016 || ctrl->value > viu_qctrl[i].maximum)
1018 qctl_regs[i] = ctrl->value;
1025 inline void viu_activate_next_buf(struct viu_dev *dev,
1026 struct viu_dmaqueue *viuq)
1028 struct viu_dmaqueue *vidq = viuq;
1029 struct viu_buf *buf;
1031 /* launch another DMA operation for an active/queued buffer */
1032 if (!list_empty(&vidq->active)) {
1033 buf = list_entry(vidq->active.next, struct viu_buf,
1035 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1036 buffer_activate(dev, buf);
1037 } else if (!list_empty(&vidq->queued)) {
1038 buf = list_entry(vidq->queued.next, struct viu_buf,
1040 list_del(&buf->vb.queue);
1042 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1043 list_add_tail(&buf->vb.queue, &vidq->active);
1044 buf->vb.state = VIDEOBUF_ACTIVE;
1045 buffer_activate(dev, buf);
1049 inline void viu_default_settings(struct viu_reg *viu_reg)
1051 struct viu_reg *vr = viu_reg;
1053 out_be32(&vr->luminance, 0x9512A254);
1054 out_be32(&vr->chroma_r, 0x03310000);
1055 out_be32(&vr->chroma_g, 0x06600F38);
1056 out_be32(&vr->chroma_b, 0x00000409);
1057 out_be32(&vr->alpha, 0x000000ff);
1058 out_be32(&vr->req_alarm, 0x00000090);
1059 dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
1060 in_be32(&vr->status_cfg), in_be32(&vr->field_base_addr));
1063 static void viu_overlay_intr(struct viu_dev *dev, u32 status)
1065 struct viu_reg *vr = dev->vr;
1067 if (status & INT_DMA_END_STATUS)
1070 if (status & INT_FIELD_STATUS) {
1071 if (dev->dma_done) {
1072 u32 addr = reg_val.field_base_addr;
1075 if (status & FIELD_NO)
1076 addr += reg_val.dma_inc;
1078 out_be32(&vr->field_base_addr, addr);
1079 out_be32(&vr->dma_inc, reg_val.dma_inc);
1080 out_be32(&vr->status_cfg,
1081 (status & 0xffc0ffff) |
1082 (status & INT_ALL_STATUS) |
1083 reg_val.status_cfg);
1084 } else if (status & INT_VSYNC_STATUS) {
1085 out_be32(&vr->status_cfg,
1086 (status & 0xffc0ffff) |
1087 (status & INT_ALL_STATUS) |
1088 reg_val.status_cfg);
1093 static void viu_capture_intr(struct viu_dev *dev, u32 status)
1095 struct viu_dmaqueue *vidq = &dev->vidq;
1096 struct viu_reg *vr = dev->vr;
1097 struct viu_buf *buf;
1102 field_num = status & FIELD_NO;
1103 need_two = V4L2_FIELD_HAS_BOTH(dev->capfield);
1105 if (status & INT_DMA_END_STATUS) {
1107 if (((field_num == 0) && (dev->field == 0)) ||
1108 (field_num && (dev->field == 1)))
1112 if (status & INT_FIELD_STATUS) {
1113 dprintk(1, "irq: field %d, done %d\n",
1114 !!field_num, dma_done);
1115 if (unlikely(dev->first)) {
1116 if (field_num == 0) {
1118 dprintk(1, "activate first buf\n");
1119 viu_activate_next_buf(dev, vidq);
1121 dprintk(1, "wait field 0\n");
1125 /* setup buffer address for next dma operation */
1126 if (!list_empty(&vidq->active)) {
1127 u32 addr = reg_val.field_base_addr;
1129 if (field_num && need_two) {
1130 addr += reg_val.dma_inc;
1131 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1132 (unsigned long)addr, dev->field);
1134 out_be32(&vr->field_base_addr, addr);
1135 out_be32(&vr->dma_inc, reg_val.dma_inc);
1136 out_be32(&vr->status_cfg,
1137 (status & 0xffc0ffff) |
1138 (status & INT_ALL_STATUS) |
1139 reg_val.status_cfg);
1144 if (dma_done && field_num && (dev->field == 2)) {
1146 buf = list_entry(vidq->active.next,
1147 struct viu_buf, vb.queue);
1148 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1150 (unsigned long)videobuf_to_dma_contig(&buf->vb),
1151 (unsigned long)in_be32(&vr->field_base_addr));
1153 if (waitqueue_active(&buf->vb.done)) {
1154 list_del(&buf->vb.queue);
1155 do_gettimeofday(&buf->vb.ts);
1156 buf->vb.state = VIDEOBUF_DONE;
1157 buf->vb.field_count++;
1158 wake_up(&buf->vb.done);
1160 /* activate next dma buffer */
1161 viu_activate_next_buf(dev, vidq);
1165 static irqreturn_t viu_intr(int irq, void *dev_id)
1167 struct viu_dev *dev = (struct viu_dev *)dev_id;
1168 struct viu_reg *vr = dev->vr;
1172 status = in_be32(&vr->status_cfg);
1174 if (status & INT_ERROR_STATUS) {
1175 dev->irqs.error_irq++;
1176 error = status & ERR_MASK;
1178 dprintk(1, "Err: error(%d), times:%d!\n",
1179 error >> 4, dev->irqs.error_irq);
1180 /* Clear interrupt error bit and error flags */
1181 out_be32(&vr->status_cfg,
1182 (status & 0xffc0ffff) | INT_ERROR_STATUS);
1185 if (status & INT_DMA_END_STATUS) {
1186 dev->irqs.dma_end_irq++;
1188 dprintk(2, "VIU DMA end interrupt times: %d\n",
1189 dev->irqs.dma_end_irq);
1192 if (status & INT_HSYNC_STATUS)
1193 dev->irqs.hsync_irq++;
1195 if (status & INT_FIELD_STATUS) {
1196 dev->irqs.field_irq++;
1197 dprintk(2, "VIU field interrupt times: %d\n",
1198 dev->irqs.field_irq);
1201 if (status & INT_VSTART_STATUS)
1202 dev->irqs.vstart_irq++;
1204 if (status & INT_VSYNC_STATUS) {
1205 dev->irqs.vsync_irq++;
1206 dprintk(2, "VIU vsync interrupt times: %d\n",
1207 dev->irqs.vsync_irq);
1210 /* clear all pending irqs */
1211 status = in_be32(&vr->status_cfg);
1212 out_be32(&vr->status_cfg,
1213 (status & 0xffc0ffff) | (status & INT_ALL_STATUS));
1215 if (dev->ovenable) {
1216 viu_overlay_intr(dev, status);
1221 viu_capture_intr(dev, status);
1226 * File operations for the device
1228 static int viu_open(struct file *file)
1230 struct video_device *vdev = video_devdata(file);
1231 struct viu_dev *dev = video_get_drvdata(vdev);
1234 int minor = vdev->minor;
1238 dprintk(1, "viu: open (minor=%d)\n", minor);
1241 if (dev->users > 1) {
1248 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1249 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1251 /* allocate and initialize per filehandle data */
1252 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1258 file->private_data = fh;
1261 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1262 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_RGB32);
1263 fh->width = norm_maxw();
1264 fh->height = norm_maxh();
1265 dev->crop_current.width = fh->width;
1266 dev->crop_current.height = fh->height;
1268 /* Put all controls at a sane state */
1269 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++)
1270 qctl_regs[i] = viu_qctrl[i].default_value;
1272 dprintk(1, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1273 (unsigned long)fh, (unsigned long)dev,
1274 (unsigned long)&dev->vidq);
1275 dprintk(1, "Open: list_empty queued=%d\n",
1276 list_empty(&dev->vidq.queued));
1277 dprintk(1, "Open: list_empty active=%d\n",
1278 list_empty(&dev->vidq.active));
1280 viu_default_settings(vr);
1282 status_cfg = in_be32(&vr->status_cfg);
1283 out_be32(&vr->status_cfg,
1284 status_cfg & ~(INT_VSYNC_EN | INT_HSYNC_EN |
1285 INT_FIELD_EN | INT_VSTART_EN |
1286 INT_DMA_END_EN | INT_ERROR_EN | INT_ECC_EN));
1288 status_cfg = in_be32(&vr->status_cfg);
1289 out_be32(&vr->status_cfg, status_cfg | INT_ALL_STATUS);
1291 spin_lock_init(&fh->vbq_lock);
1292 videobuf_queue_dma_contig_init(&fh->vb_vidq, &viu_video_qops,
1293 dev->dev, &fh->vbq_lock,
1294 fh->type, V4L2_FIELD_INTERLACED,
1295 sizeof(struct viu_buf), fh, NULL);
1299 static ssize_t viu_read(struct file *file, char __user *data, size_t count,
1302 struct viu_fh *fh = file->private_data;
1303 struct viu_dev *dev = fh->dev;
1306 dprintk(2, "%s\n", __func__);
1310 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1312 ret = videobuf_read_stream(&fh->vb_vidq, data, count,
1313 ppos, 0, file->f_flags & O_NONBLOCK);
1319 static unsigned int viu_poll(struct file *file, struct poll_table_struct *wait)
1321 struct viu_fh *fh = file->private_data;
1322 struct videobuf_queue *q = &fh->vb_vidq;
1324 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1327 return videobuf_poll_stream(file, q, wait);
1330 static int viu_release(struct file *file)
1332 struct viu_fh *fh = file->private_data;
1333 struct viu_dev *dev = fh->dev;
1334 int minor = video_devdata(file)->minor;
1337 videobuf_stop(&fh->vb_vidq);
1338 videobuf_mmap_free(&fh->vb_vidq);
1343 dprintk(1, "close (minor=%d, users=%d)\n",
1348 void viu_reset(struct viu_reg *reg)
1350 out_be32(®->status_cfg, 0);
1351 out_be32(®->luminance, 0x9512a254);
1352 out_be32(®->chroma_r, 0x03310000);
1353 out_be32(®->chroma_g, 0x06600f38);
1354 out_be32(®->chroma_b, 0x00000409);
1355 out_be32(®->field_base_addr, 0);
1356 out_be32(®->dma_inc, 0);
1357 out_be32(®->picture_count, 0x01e002d0);
1358 out_be32(®->req_alarm, 0x00000090);
1359 out_be32(®->alpha, 0x000000ff);
1362 static int viu_mmap(struct file *file, struct vm_area_struct *vma)
1364 struct viu_fh *fh = file->private_data;
1367 dprintk(1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1369 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1371 dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1372 (unsigned long)vma->vm_start,
1373 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1379 static struct v4l2_file_operations viu_fops = {
1380 .owner = THIS_MODULE,
1382 .release = viu_release,
1385 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1389 static const struct v4l2_ioctl_ops viu_ioctl_ops = {
1390 .vidioc_querycap = vidioc_querycap,
1391 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1392 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_cap,
1393 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_cap,
1394 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_cap,
1395 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt,
1396 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_overlay,
1397 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_overlay,
1398 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_overlay,
1399 .vidioc_g_fbuf = vidioc_g_fbuf,
1400 .vidioc_s_fbuf = vidioc_s_fbuf,
1401 .vidioc_reqbufs = vidioc_reqbufs,
1402 .vidioc_querybuf = vidioc_querybuf,
1403 .vidioc_qbuf = vidioc_qbuf,
1404 .vidioc_dqbuf = vidioc_dqbuf,
1405 .vidioc_s_std = vidioc_s_std,
1406 .vidioc_enum_input = vidioc_enum_input,
1407 .vidioc_g_input = vidioc_g_input,
1408 .vidioc_s_input = vidioc_s_input,
1409 .vidioc_queryctrl = vidioc_queryctrl,
1410 .vidioc_g_ctrl = vidioc_g_ctrl,
1411 .vidioc_s_ctrl = vidioc_s_ctrl,
1412 .vidioc_streamon = vidioc_streamon,
1413 .vidioc_streamoff = vidioc_streamoff,
1416 static struct video_device viu_template = {
1420 .ioctl_ops = &viu_ioctl_ops,
1421 .release = video_device_release,
1423 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1424 .current_norm = V4L2_STD_NTSC_M,
1427 static int __devinit viu_of_probe(struct platform_device *op,
1428 const struct of_device_id *match)
1430 struct viu_dev *viu_dev;
1431 struct video_device *vdev;
1433 struct viu_reg __iomem *viu_regs;
1434 struct i2c_adapter *ad;
1437 ret = of_address_to_resource(op->dev.of_node, 0, &r);
1439 dev_err(&op->dev, "Can't parse device node resource\n");
1443 viu_irq = irq_of_parse_and_map(op->dev.of_node, 0);
1444 if (viu_irq == NO_IRQ) {
1445 dev_err(&op->dev, "Error while mapping the irq\n");
1449 /* request mem region */
1450 if (!devm_request_mem_region(&op->dev, r.start,
1451 sizeof(struct viu_reg), DRV_NAME)) {
1452 dev_err(&op->dev, "Error while requesting mem region\n");
1457 /* remap registers */
1458 viu_regs = devm_ioremap(&op->dev, r.start, sizeof(struct viu_reg));
1460 dev_err(&op->dev, "Can't map register set\n");
1465 /* Prepare our private structure */
1466 viu_dev = devm_kzalloc(&op->dev, sizeof(struct viu_dev), GFP_ATOMIC);
1468 dev_err(&op->dev, "Can't allocate private structure\n");
1473 viu_dev->vr = viu_regs;
1474 viu_dev->irq = viu_irq;
1475 viu_dev->dev = &op->dev;
1477 /* init video dma queues */
1478 INIT_LIST_HEAD(&viu_dev->vidq.active);
1479 INIT_LIST_HEAD(&viu_dev->vidq.queued);
1481 /* initialize locks */
1482 mutex_init(&viu_dev->lock);
1484 snprintf(viu_dev->v4l2_dev.name,
1485 sizeof(viu_dev->v4l2_dev.name), "%s", "VIU");
1486 ret = v4l2_device_register(viu_dev->dev, &viu_dev->v4l2_dev);
1488 dev_err(&op->dev, "v4l2_device_register() failed: %d\n", ret);
1492 ad = i2c_get_adapter(0);
1493 viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
1494 "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
1496 viu_dev->vidq.timeout.function = viu_vid_timeout;
1497 viu_dev->vidq.timeout.data = (unsigned long)viu_dev;
1498 init_timer(&viu_dev->vidq.timeout);
1501 /* Allocate memory for video device */
1502 vdev = video_device_alloc();
1508 memcpy(vdev, &viu_template, sizeof(viu_template));
1510 vdev->v4l2_dev = &viu_dev->v4l2_dev;
1512 viu_dev->vdev = vdev;
1514 video_set_drvdata(viu_dev->vdev, viu_dev);
1516 ret = video_register_device(viu_dev->vdev, VFL_TYPE_GRABBER, -1);
1518 video_device_release(viu_dev->vdev);
1522 /* enable VIU clock */
1523 viu_dev->clk = clk_get(&op->dev, "viu_clk");
1524 if (IS_ERR(viu_dev->clk)) {
1525 dev_err(&op->dev, "failed to find the clock module!\n");
1529 clk_enable(viu_dev->clk);
1532 /* reset VIU module */
1533 viu_reset(viu_dev->vr);
1535 /* install interrupt handler */
1536 if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
1537 dev_err(&op->dev, "Request VIU IRQ failed.\n");
1542 dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
1546 clk_disable(viu_dev->clk);
1547 clk_put(viu_dev->clk);
1549 video_unregister_device(viu_dev->vdev);
1551 i2c_put_adapter(ad);
1552 v4l2_device_unregister(&viu_dev->v4l2_dev);
1554 irq_dispose_mapping(viu_irq);
1558 static int __devexit viu_of_remove(struct platform_device *op)
1560 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1561 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1562 struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
1563 struct v4l2_subdev, list);
1564 struct i2c_client *client = v4l2_get_subdevdata(sdev);
1566 free_irq(dev->irq, (void *)dev);
1567 irq_dispose_mapping(dev->irq);
1569 clk_disable(dev->clk);
1572 video_unregister_device(dev->vdev);
1573 i2c_put_adapter(client->adapter);
1574 v4l2_device_unregister(&dev->v4l2_dev);
1579 static int viu_suspend(struct platform_device *op, pm_message_t state)
1581 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1582 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1584 clk_disable(dev->clk);
1588 static int viu_resume(struct platform_device *op)
1590 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1591 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1593 clk_enable(dev->clk);
1599 * Initialization and module stuff
1601 static struct of_device_id mpc512x_viu_of_match[] = {
1603 .compatible = "fsl,mpc5121-viu",
1607 MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
1609 static struct of_platform_driver viu_of_platform_driver = {
1610 .probe = viu_of_probe,
1611 .remove = __devexit_p(viu_of_remove),
1613 .suspend = viu_suspend,
1614 .resume = viu_resume,
1618 .owner = THIS_MODULE,
1619 .of_match_table = mpc512x_viu_of_match,
1623 static int __init viu_init(void)
1625 return of_register_platform_driver(&viu_of_platform_driver);
1628 static void __exit viu_exit(void)
1630 of_unregister_platform_driver(&viu_of_platform_driver);
1633 module_init(viu_init);
1634 module_exit(viu_exit);
1636 MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1637 MODULE_AUTHOR("Hongjun Chen");
1638 MODULE_LICENSE("GPL");