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/v4l2-ctrls.h>
32 #include <media/v4l2-fh.h>
33 #include <media/v4l2-event.h>
34 #include <media/videobuf-dma-contig.h>
36 #define DRV_NAME "fsl_viu"
37 #define VIU_VERSION "0.5.1"
39 /* Allow building this driver with COMPILE_TEST */
40 #if !defined(CONFIG_PPC) && !defined(CONFIG_MICROBLAZE)
41 #define out_be32(v, a) iowrite32be(a, (void __iomem *)v)
42 #define in_be32(a) ioread32be((void __iomem *)a)
45 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
47 #define VIU_VID_MEM_LIMIT 4 /* Video memory limit, in Mb */
49 /* I2C address of video decoder chip is 0x4A */
50 #define VIU_VIDEO_DECODER_ADDR 0x25
52 static int info_level;
54 #define dprintk(level, fmt, arg...) \
56 if (level <= info_level) \
57 printk(KERN_DEBUG "viu: " fmt , ## arg); \
64 u32 fourcc; /* v4l2 format id */
69 static struct viu_fmt formats[] = {
71 .fourcc = V4L2_PIX_FMT_RGB565,
72 .pixelformat = V4L2_PIX_FMT_RGB565,
75 .fourcc = V4L2_PIX_FMT_RGB32,
76 .pixelformat = V4L2_PIX_FMT_RGB32,
84 /* buffer for one video frame */
86 /* common v4l buffer stuff -- must be first */
87 struct videobuf_buffer vb;
93 struct list_head active;
94 struct list_head queued;
95 struct timer_list timeout;
118 } __attribute__ ((packed));
121 struct v4l2_device v4l2_dev;
122 struct v4l2_ctrl_handler hdl;
128 /* various device info */
129 struct video_device *vdev;
130 struct viu_dmaqueue vidq;
131 enum v4l2_field capfield;
136 /* Hardware register area */
137 struct viu_reg __iomem *vr;
139 /* Interrupt vector */
141 struct viu_status irqs;
144 struct v4l2_framebuffer ovbuf;
145 struct viu_fmt *ovfmt;
146 unsigned int ovenable;
147 enum v4l2_field ovfield;
150 struct v4l2_rect crop_current;
156 struct v4l2_subdev *decoder;
162 /* must remain the first field of this struct */
167 struct videobuf_queue vb_vidq;
168 spinlock_t vbq_lock; /* spinlock for the videobuf queue */
171 struct v4l2_window win;
172 struct v4l2_clip clips[1];
176 int width, height, sizeimage;
177 enum v4l2_buf_type type;
180 static struct viu_reg reg_val;
183 * Macro definitions of VIU registers
186 /* STATUS_CONFIG register */
190 ERR_MASK = 0x0f << 4, /* Error code mask */
191 ERR_NO = 0x00, /* No error */
192 ERR_DMA_V = 0x01 << 4, /* DMA in vertical active */
193 ERR_DMA_VB = 0x02 << 4, /* DMA in vertical blanking */
194 ERR_LINE_TOO_LONG = 0x04 << 4, /* Line too long */
195 ERR_TOO_MANG_LINES = 0x05 << 4, /* Too many lines in field */
196 ERR_LINE_TOO_SHORT = 0x06 << 4, /* Line too short */
197 ERR_NOT_ENOUGH_LINE = 0x07 << 4, /* Not enough lines in field */
198 ERR_FIFO_OVERFLOW = 0x08 << 4, /* FIFO overflow */
199 ERR_FIFO_UNDERFLOW = 0x09 << 4, /* FIFO underflow */
200 ERR_1bit_ECC = 0x0a << 4, /* One bit ECC error */
201 ERR_MORE_ECC = 0x0b << 4, /* Two/more bits ECC error */
203 INT_FIELD_EN = 0x01 << 8, /* Enable field interrupt */
204 INT_VSYNC_EN = 0x01 << 9, /* Enable vsync interrupt */
205 INT_HSYNC_EN = 0x01 << 10, /* Enable hsync interrupt */
206 INT_VSTART_EN = 0x01 << 11, /* Enable vstart interrupt */
207 INT_DMA_END_EN = 0x01 << 12, /* Enable DMA end interrupt */
208 INT_ERROR_EN = 0x01 << 13, /* Enable error interrupt */
209 INT_ECC_EN = 0x01 << 14, /* Enable ECC interrupt */
211 INT_FIELD_STATUS = 0x01 << 16, /* field interrupt status */
212 INT_VSYNC_STATUS = 0x01 << 17, /* vsync interrupt status */
213 INT_HSYNC_STATUS = 0x01 << 18, /* hsync interrupt status */
214 INT_VSTART_STATUS = 0x01 << 19, /* vstart interrupt status */
215 INT_DMA_END_STATUS = 0x01 << 20, /* DMA end interrupt status */
216 INT_ERROR_STATUS = 0x01 << 21, /* error interrupt status */
218 DMA_ACT = 0x01 << 27, /* Enable DMA transfer */
219 FIELD_NO = 0x01 << 28, /* Field number */
220 DITHER_ON = 0x01 << 29, /* Dithering is on */
221 ROUND_ON = 0x01 << 30, /* Round is on */
222 MODE_32BIT = 0x01 << 31, /* Data in RGBa888,
227 #define norm_maxw() 720
228 #define norm_maxh() 576
230 #define INT_ALL_STATUS (INT_FIELD_STATUS | INT_VSYNC_STATUS | \
231 INT_HSYNC_STATUS | INT_VSTART_STATUS | \
232 INT_DMA_END_STATUS | INT_ERROR_STATUS)
234 #define NUM_FORMATS ARRAY_SIZE(formats)
236 static irqreturn_t viu_intr(int irq, void *dev_id);
238 static struct viu_fmt *format_by_fourcc(int fourcc)
242 for (i = 0; i < NUM_FORMATS; i++) {
243 if (formats[i].pixelformat == fourcc)
247 dprintk(0, "unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
251 static void viu_start_dma(struct viu_dev *dev)
253 struct viu_reg __iomem *vr = dev->vr;
257 /* Enable DMA operation */
258 out_be32(&vr->status_cfg, SOFT_RST);
259 out_be32(&vr->status_cfg, INT_FIELD_EN);
262 static void viu_stop_dma(struct viu_dev *dev)
264 struct viu_reg __iomem *vr = dev->vr;
268 out_be32(&vr->status_cfg, 0);
270 /* Clear pending interrupts */
271 status_cfg = in_be32(&vr->status_cfg);
272 if (status_cfg & 0x3f0000)
273 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
275 if (status_cfg & DMA_ACT) {
277 status_cfg = in_be32(&vr->status_cfg);
278 if (status_cfg & INT_DMA_END_STATUS)
283 /* timed out, issue soft reset */
284 out_be32(&vr->status_cfg, SOFT_RST);
285 out_be32(&vr->status_cfg, 0);
287 /* clear DMA_END and other pending irqs */
288 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
295 static int restart_video_queue(struct viu_dmaqueue *vidq)
297 struct viu_buf *buf, *prev;
299 dprintk(1, "%s vidq=%p\n", __func__, vidq);
300 if (!list_empty(&vidq->active)) {
301 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
302 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
305 viu_stop_dma(vidq->dev);
307 /* cancel all outstanding capture requests */
308 list_for_each_entry_safe(buf, prev, &vidq->active, vb.queue) {
309 list_del(&buf->vb.queue);
310 buf->vb.state = VIDEOBUF_ERROR;
311 wake_up(&buf->vb.done);
313 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
319 if (list_empty(&vidq->queued))
321 buf = list_entry(vidq->queued.next, struct viu_buf, vb.queue);
323 list_move_tail(&buf->vb.queue, &vidq->active);
325 dprintk(1, "Restarting video dma\n");
326 viu_stop_dma(vidq->dev);
327 viu_start_dma(vidq->dev);
329 buf->vb.state = VIDEOBUF_ACTIVE;
330 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
331 dprintk(2, "[%p/%d] restart_queue - first active\n",
334 } else if (prev->vb.width == buf->vb.width &&
335 prev->vb.height == buf->vb.height &&
336 prev->fmt == buf->fmt) {
337 list_move_tail(&buf->vb.queue, &vidq->active);
338 buf->vb.state = VIDEOBUF_ACTIVE;
339 dprintk(2, "[%p/%d] restart_queue - move to active\n",
348 static void viu_vid_timeout(struct timer_list *t)
350 struct viu_dev *dev = from_timer(dev, t, vidq.timeout);
352 struct viu_dmaqueue *vidq = &dev->vidq;
354 while (!list_empty(&vidq->active)) {
355 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
356 list_del(&buf->vb.queue);
357 buf->vb.state = VIDEOBUF_ERROR;
358 wake_up(&buf->vb.done);
359 dprintk(1, "viu/0: [%p/%d] timeout\n", buf, buf->vb.i);
362 restart_video_queue(vidq);
366 * Videobuf operations
368 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
371 struct viu_fh *fh = vq->priv_data;
373 *size = fh->width * fh->height * fh->fmt->depth >> 3;
377 while (*size * *count > VIU_VID_MEM_LIMIT * 1024 * 1024)
380 dprintk(1, "%s, count=%d, size=%d\n", __func__, *count, *size);
384 static void free_buffer(struct videobuf_queue *vq, struct viu_buf *buf)
386 struct videobuf_buffer *vb = &buf->vb;
389 BUG_ON(in_interrupt());
391 videobuf_waiton(vq, &buf->vb, 0, 0);
393 if (vq->int_ops && vq->int_ops->vaddr)
394 vaddr = vq->int_ops->vaddr(vb);
397 videobuf_dma_contig_free(vq, &buf->vb);
399 buf->vb.state = VIDEOBUF_NEEDS_INIT;
402 inline int buffer_activate(struct viu_dev *dev, struct viu_buf *buf)
404 struct viu_reg __iomem *vr = dev->vr;
407 /* setup the DMA base address */
408 reg_val.field_base_addr = videobuf_to_dma_contig(&buf->vb);
410 dprintk(1, "buffer_activate [%p/%d]: dma addr 0x%lx\n",
411 buf, buf->vb.i, (unsigned long)reg_val.field_base_addr);
413 /* interlace is on by default, set horizontal DMA increment */
414 reg_val.status_cfg = 0;
415 bpp = buf->fmt->depth >> 3;
418 reg_val.status_cfg &= ~MODE_32BIT;
419 reg_val.dma_inc = buf->vb.width * 2;
422 reg_val.status_cfg |= MODE_32BIT;
423 reg_val.dma_inc = buf->vb.width * 4;
426 dprintk(0, "doesn't support color depth(%d)\n",
431 /* setup picture_count register */
432 reg_val.picture_count = (buf->vb.height / 2) << 16 |
435 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
437 buf->vb.state = VIDEOBUF_ACTIVE;
438 dev->capfield = buf->vb.field;
440 /* reset dma increment if needed */
441 if (!V4L2_FIELD_HAS_BOTH(buf->vb.field))
444 out_be32(&vr->dma_inc, reg_val.dma_inc);
445 out_be32(&vr->picture_count, reg_val.picture_count);
446 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
447 mod_timer(&dev->vidq.timeout, jiffies + BUFFER_TIMEOUT);
451 static int buffer_prepare(struct videobuf_queue *vq,
452 struct videobuf_buffer *vb,
453 enum v4l2_field field)
455 struct viu_fh *fh = vq->priv_data;
456 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
459 BUG_ON(fh->fmt == NULL);
461 if (fh->width < 48 || fh->width > norm_maxw() ||
462 fh->height < 32 || fh->height > norm_maxh())
464 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
465 if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size)
468 if (buf->fmt != fh->fmt ||
469 buf->vb.width != fh->width ||
470 buf->vb.height != fh->height ||
471 buf->vb.field != field) {
473 buf->vb.width = fh->width;
474 buf->vb.height = fh->height;
475 buf->vb.field = field;
478 if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
479 rc = videobuf_iolock(vq, &buf->vb, NULL);
483 buf->vb.width = fh->width;
484 buf->vb.height = fh->height;
485 buf->vb.field = field;
489 buf->vb.state = VIDEOBUF_PREPARED;
493 free_buffer(vq, buf);
497 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
499 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
500 struct viu_fh *fh = vq->priv_data;
501 struct viu_dev *dev = fh->dev;
502 struct viu_dmaqueue *vidq = &dev->vidq;
503 struct viu_buf *prev;
505 if (!list_empty(&vidq->queued)) {
506 dprintk(1, "adding vb queue=%p\n", &buf->vb.queue);
507 dprintk(1, "vidq pointer 0x%p, queued 0x%p\n",
508 vidq, &vidq->queued);
509 dprintk(1, "dev %p, queued: self %p, next %p, head %p\n",
510 dev, &vidq->queued, vidq->queued.next,
512 list_add_tail(&buf->vb.queue, &vidq->queued);
513 buf->vb.state = VIDEOBUF_QUEUED;
514 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
516 } else if (list_empty(&vidq->active)) {
517 dprintk(1, "adding vb active=%p\n", &buf->vb.queue);
518 list_add_tail(&buf->vb.queue, &vidq->active);
519 buf->vb.state = VIDEOBUF_ACTIVE;
520 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
521 dprintk(2, "[%p/%d] buffer_queue - first active\n",
524 buffer_activate(dev, buf);
526 dprintk(1, "adding vb queue2=%p\n", &buf->vb.queue);
527 prev = list_entry(vidq->active.prev, struct viu_buf, vb.queue);
528 if (prev->vb.width == buf->vb.width &&
529 prev->vb.height == buf->vb.height &&
530 prev->fmt == buf->fmt) {
531 list_add_tail(&buf->vb.queue, &vidq->active);
532 buf->vb.state = VIDEOBUF_ACTIVE;
533 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
536 list_add_tail(&buf->vb.queue, &vidq->queued);
537 buf->vb.state = VIDEOBUF_QUEUED;
538 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
544 static void buffer_release(struct videobuf_queue *vq,
545 struct videobuf_buffer *vb)
547 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
548 struct viu_fh *fh = vq->priv_data;
549 struct viu_dev *dev = (struct viu_dev *)fh->dev;
552 free_buffer(vq, buf);
555 static const struct videobuf_queue_ops viu_video_qops = {
556 .buf_setup = buffer_setup,
557 .buf_prepare = buffer_prepare,
558 .buf_queue = buffer_queue,
559 .buf_release = buffer_release,
563 * IOCTL vidioc handling
565 static int vidioc_querycap(struct file *file, void *priv,
566 struct v4l2_capability *cap)
568 strcpy(cap->driver, "viu");
569 strcpy(cap->card, "viu");
570 strcpy(cap->bus_info, "platform:viu");
571 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
573 V4L2_CAP_VIDEO_OVERLAY |
575 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
579 static int vidioc_enum_fmt(struct file *file, void *priv,
580 struct v4l2_fmtdesc *f)
582 int index = f->index;
584 if (f->index >= NUM_FORMATS)
587 f->pixelformat = formats[index].fourcc;
591 static int vidioc_g_fmt_cap(struct file *file, void *priv,
592 struct v4l2_format *f)
594 struct viu_fh *fh = priv;
596 f->fmt.pix.width = fh->width;
597 f->fmt.pix.height = fh->height;
598 f->fmt.pix.field = fh->vb_vidq.field;
599 f->fmt.pix.pixelformat = fh->fmt->pixelformat;
600 f->fmt.pix.bytesperline =
601 (f->fmt.pix.width * fh->fmt->depth) >> 3;
602 f->fmt.pix.sizeimage = fh->sizeimage;
603 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
607 static int vidioc_try_fmt_cap(struct file *file, void *priv,
608 struct v4l2_format *f)
611 unsigned int maxw, maxh;
613 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
615 dprintk(1, "Fourcc format (0x%08x) invalid.",
616 f->fmt.pix.pixelformat);
623 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
624 if (f->fmt.pix.height < 32)
625 f->fmt.pix.height = 32;
626 if (f->fmt.pix.height > maxh)
627 f->fmt.pix.height = maxh;
628 if (f->fmt.pix.width < 48)
629 f->fmt.pix.width = 48;
630 if (f->fmt.pix.width > maxw)
631 f->fmt.pix.width = maxw;
632 f->fmt.pix.width &= ~0x03;
633 f->fmt.pix.bytesperline =
634 (f->fmt.pix.width * fmt->depth) >> 3;
635 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
636 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
641 static int vidioc_s_fmt_cap(struct file *file, void *priv,
642 struct v4l2_format *f)
644 struct viu_fh *fh = priv;
647 ret = vidioc_try_fmt_cap(file, fh, f);
651 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
652 fh->width = f->fmt.pix.width;
653 fh->height = f->fmt.pix.height;
654 fh->sizeimage = f->fmt.pix.sizeimage;
655 fh->vb_vidq.field = f->fmt.pix.field;
660 static int vidioc_g_fmt_overlay(struct file *file, void *priv,
661 struct v4l2_format *f)
663 struct viu_fh *fh = priv;
665 f->fmt.win = fh->win;
669 static int verify_preview(struct viu_dev *dev, struct v4l2_window *win)
671 enum v4l2_field field;
674 if (dev->ovbuf.base == NULL)
676 if (dev->ovfmt == NULL)
678 if (win->w.width < 48 || win->w.height < 32)
682 maxw = dev->crop_current.width;
683 maxh = dev->crop_current.height;
685 if (field == V4L2_FIELD_ANY) {
686 field = (win->w.height > maxh/2)
687 ? V4L2_FIELD_INTERLACED
692 case V4L2_FIELD_BOTTOM:
695 case V4L2_FIELD_INTERLACED:
702 if (win->w.width > maxw)
704 if (win->w.height > maxh)
705 win->w.height = maxh;
709 inline void viu_activate_overlay(struct viu_reg __iomem *vr)
711 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
712 out_be32(&vr->dma_inc, reg_val.dma_inc);
713 out_be32(&vr->picture_count, reg_val.picture_count);
716 static int viu_setup_preview(struct viu_dev *dev, struct viu_fh *fh)
720 dprintk(1, "%s %dx%d\n", __func__,
721 fh->win.w.width, fh->win.w.height);
723 reg_val.status_cfg = 0;
726 reg_val.picture_count = (fh->win.w.height / 2) << 16 |
729 /* setup color depth and dma increment */
730 bpp = dev->ovfmt->depth / 8;
733 reg_val.status_cfg &= ~MODE_32BIT;
734 reg_val.dma_inc = fh->win.w.width * 2;
737 reg_val.status_cfg |= MODE_32BIT;
738 reg_val.dma_inc = fh->win.w.width * 4;
741 dprintk(0, "device doesn't support color depth(%d)\n",
746 dev->ovfield = fh->win.field;
747 if (!V4L2_FIELD_HAS_BOTH(dev->ovfield))
750 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
752 /* setup the base address of the overlay buffer */
753 reg_val.field_base_addr = (u32)(long)dev->ovbuf.base;
758 static int vidioc_s_fmt_overlay(struct file *file, void *priv,
759 struct v4l2_format *f)
761 struct viu_fh *fh = priv;
762 struct viu_dev *dev = (struct viu_dev *)fh->dev;
766 err = verify_preview(dev, &f->fmt.win);
770 fh->win = f->fmt.win;
772 spin_lock_irqsave(&dev->slock, flags);
773 viu_setup_preview(dev, fh);
774 spin_unlock_irqrestore(&dev->slock, flags);
778 static int vidioc_try_fmt_overlay(struct file *file, void *priv,
779 struct v4l2_format *f)
784 static int vidioc_overlay(struct file *file, void *priv, unsigned int on)
786 struct viu_fh *fh = priv;
787 struct viu_dev *dev = (struct viu_dev *)fh->dev;
791 spin_lock_irqsave(&dev->slock, flags);
792 viu_activate_overlay(dev->vr);
797 spin_unlock_irqrestore(&dev->slock, flags);
806 static int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
808 struct viu_fh *fh = priv;
809 struct viu_dev *dev = fh->dev;
810 struct v4l2_framebuffer *fb = arg;
813 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
817 static int vidioc_s_fbuf(struct file *file, void *priv, const struct v4l2_framebuffer *arg)
819 struct viu_fh *fh = priv;
820 struct viu_dev *dev = fh->dev;
821 const struct v4l2_framebuffer *fb = arg;
824 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
828 fmt = format_by_fourcc(fb->fmt.pixelformat);
835 if (dev->ovbuf.fmt.bytesperline == 0) {
836 dev->ovbuf.fmt.bytesperline =
837 dev->ovbuf.fmt.width * fmt->depth / 8;
842 static int vidioc_reqbufs(struct file *file, void *priv,
843 struct v4l2_requestbuffers *p)
845 struct viu_fh *fh = priv;
847 return videobuf_reqbufs(&fh->vb_vidq, p);
850 static int vidioc_querybuf(struct file *file, void *priv,
851 struct v4l2_buffer *p)
853 struct viu_fh *fh = priv;
855 return videobuf_querybuf(&fh->vb_vidq, p);
858 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
860 struct viu_fh *fh = priv;
862 return videobuf_qbuf(&fh->vb_vidq, p);
865 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
867 struct viu_fh *fh = priv;
869 return videobuf_dqbuf(&fh->vb_vidq, p,
870 file->f_flags & O_NONBLOCK);
873 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
875 struct viu_fh *fh = priv;
876 struct viu_dev *dev = fh->dev;
878 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
886 viu_start_dma(fh->dev);
888 return videobuf_streamon(&fh->vb_vidq);
891 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
893 struct viu_fh *fh = priv;
895 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
900 viu_stop_dma(fh->dev);
902 return videobuf_streamoff(&fh->vb_vidq);
905 #define decoder_call(viu, o, f, args...) \
906 v4l2_subdev_call(viu->decoder, o, f, ##args)
908 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
910 struct viu_fh *fh = priv;
912 decoder_call(fh->dev, video, querystd, std_id);
916 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
918 struct viu_fh *fh = priv;
921 decoder_call(fh->dev, video, s_std, id);
925 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
927 struct viu_fh *fh = priv;
929 *std_id = fh->dev->std;
933 /* only one input in this driver */
934 static int vidioc_enum_input(struct file *file, void *priv,
935 struct v4l2_input *inp)
937 struct viu_fh *fh = priv;
942 inp->type = V4L2_INPUT_TYPE_CAMERA;
943 inp->std = fh->dev->vdev->tvnorms;
944 strcpy(inp->name, "Camera");
948 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
954 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
956 struct viu_fh *fh = priv;
961 decoder_call(fh->dev, video, s_routing, i, 0, 0);
965 inline void viu_activate_next_buf(struct viu_dev *dev,
966 struct viu_dmaqueue *viuq)
968 struct viu_dmaqueue *vidq = viuq;
971 /* launch another DMA operation for an active/queued buffer */
972 if (!list_empty(&vidq->active)) {
973 buf = list_entry(vidq->active.next, struct viu_buf,
975 dprintk(1, "start another queued buffer: 0x%p\n", buf);
976 buffer_activate(dev, buf);
977 } else if (!list_empty(&vidq->queued)) {
978 buf = list_entry(vidq->queued.next, struct viu_buf,
980 list_del(&buf->vb.queue);
982 dprintk(1, "start another queued buffer: 0x%p\n", buf);
983 list_add_tail(&buf->vb.queue, &vidq->active);
984 buf->vb.state = VIDEOBUF_ACTIVE;
985 buffer_activate(dev, buf);
989 inline void viu_default_settings(struct viu_reg __iomem *vr)
991 out_be32(&vr->luminance, 0x9512A254);
992 out_be32(&vr->chroma_r, 0x03310000);
993 out_be32(&vr->chroma_g, 0x06600F38);
994 out_be32(&vr->chroma_b, 0x00000409);
995 out_be32(&vr->alpha, 0x000000ff);
996 out_be32(&vr->req_alarm, 0x00000090);
997 dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
998 in_be32(&vr->status_cfg), in_be32(&vr->field_base_addr));
1001 static void viu_overlay_intr(struct viu_dev *dev, u32 status)
1003 struct viu_reg __iomem *vr = dev->vr;
1005 if (status & INT_DMA_END_STATUS)
1008 if (status & INT_FIELD_STATUS) {
1009 if (dev->dma_done) {
1010 u32 addr = reg_val.field_base_addr;
1013 if (status & FIELD_NO)
1014 addr += reg_val.dma_inc;
1016 out_be32(&vr->field_base_addr, addr);
1017 out_be32(&vr->dma_inc, reg_val.dma_inc);
1018 out_be32(&vr->status_cfg,
1019 (status & 0xffc0ffff) |
1020 (status & INT_ALL_STATUS) |
1021 reg_val.status_cfg);
1022 } else if (status & INT_VSYNC_STATUS) {
1023 out_be32(&vr->status_cfg,
1024 (status & 0xffc0ffff) |
1025 (status & INT_ALL_STATUS) |
1026 reg_val.status_cfg);
1031 static void viu_capture_intr(struct viu_dev *dev, u32 status)
1033 struct viu_dmaqueue *vidq = &dev->vidq;
1034 struct viu_reg __iomem *vr = dev->vr;
1035 struct viu_buf *buf;
1040 field_num = status & FIELD_NO;
1041 need_two = V4L2_FIELD_HAS_BOTH(dev->capfield);
1043 if (status & INT_DMA_END_STATUS) {
1045 if (((field_num == 0) && (dev->field == 0)) ||
1046 (field_num && (dev->field == 1)))
1050 if (status & INT_FIELD_STATUS) {
1051 dprintk(1, "irq: field %d, done %d\n",
1052 !!field_num, dma_done);
1053 if (unlikely(dev->first)) {
1054 if (field_num == 0) {
1056 dprintk(1, "activate first buf\n");
1057 viu_activate_next_buf(dev, vidq);
1059 dprintk(1, "wait field 0\n");
1063 /* setup buffer address for next dma operation */
1064 if (!list_empty(&vidq->active)) {
1065 u32 addr = reg_val.field_base_addr;
1067 if (field_num && need_two) {
1068 addr += reg_val.dma_inc;
1069 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1070 (unsigned long)addr, dev->field);
1072 out_be32(&vr->field_base_addr, addr);
1073 out_be32(&vr->dma_inc, reg_val.dma_inc);
1074 out_be32(&vr->status_cfg,
1075 (status & 0xffc0ffff) |
1076 (status & INT_ALL_STATUS) |
1077 reg_val.status_cfg);
1082 if (dma_done && field_num && (dev->field == 2)) {
1084 buf = list_entry(vidq->active.next,
1085 struct viu_buf, vb.queue);
1086 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1088 (unsigned long)videobuf_to_dma_contig(&buf->vb),
1089 (unsigned long)in_be32(&vr->field_base_addr));
1091 if (waitqueue_active(&buf->vb.done)) {
1092 list_del(&buf->vb.queue);
1093 v4l2_get_timestamp(&buf->vb.ts);
1094 buf->vb.state = VIDEOBUF_DONE;
1095 buf->vb.field_count++;
1096 wake_up(&buf->vb.done);
1098 /* activate next dma buffer */
1099 viu_activate_next_buf(dev, vidq);
1103 static irqreturn_t viu_intr(int irq, void *dev_id)
1105 struct viu_dev *dev = (struct viu_dev *)dev_id;
1106 struct viu_reg __iomem *vr = dev->vr;
1110 status = in_be32(&vr->status_cfg);
1112 if (status & INT_ERROR_STATUS) {
1113 dev->irqs.error_irq++;
1114 error = status & ERR_MASK;
1116 dprintk(1, "Err: error(%d), times:%d!\n",
1117 error >> 4, dev->irqs.error_irq);
1118 /* Clear interrupt error bit and error flags */
1119 out_be32(&vr->status_cfg,
1120 (status & 0xffc0ffff) | INT_ERROR_STATUS);
1123 if (status & INT_DMA_END_STATUS) {
1124 dev->irqs.dma_end_irq++;
1126 dprintk(2, "VIU DMA end interrupt times: %d\n",
1127 dev->irqs.dma_end_irq);
1130 if (status & INT_HSYNC_STATUS)
1131 dev->irqs.hsync_irq++;
1133 if (status & INT_FIELD_STATUS) {
1134 dev->irqs.field_irq++;
1135 dprintk(2, "VIU field interrupt times: %d\n",
1136 dev->irqs.field_irq);
1139 if (status & INT_VSTART_STATUS)
1140 dev->irqs.vstart_irq++;
1142 if (status & INT_VSYNC_STATUS) {
1143 dev->irqs.vsync_irq++;
1144 dprintk(2, "VIU vsync interrupt times: %d\n",
1145 dev->irqs.vsync_irq);
1148 /* clear all pending irqs */
1149 status = in_be32(&vr->status_cfg);
1150 out_be32(&vr->status_cfg,
1151 (status & 0xffc0ffff) | (status & INT_ALL_STATUS));
1153 if (dev->ovenable) {
1154 viu_overlay_intr(dev, status);
1159 viu_capture_intr(dev, status);
1164 * File operations for the device
1166 static int viu_open(struct file *file)
1168 struct video_device *vdev = video_devdata(file);
1169 struct viu_dev *dev = video_get_drvdata(vdev);
1171 struct viu_reg __iomem *vr;
1172 int minor = vdev->minor;
1175 dprintk(1, "viu: open (minor=%d)\n", minor);
1178 if (dev->users > 1) {
1185 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1186 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1188 if (mutex_lock_interruptible(&dev->lock)) {
1190 return -ERESTARTSYS;
1193 /* allocate and initialize per filehandle data */
1194 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1197 mutex_unlock(&dev->lock);
1201 v4l2_fh_init(&fh->fh, vdev);
1202 file->private_data = fh;
1205 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1206 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_RGB32);
1207 fh->width = norm_maxw();
1208 fh->height = norm_maxh();
1209 dev->crop_current.width = fh->width;
1210 dev->crop_current.height = fh->height;
1212 dprintk(1, "Open: fh=%p, dev=%p, dev->vidq=%p\n", fh, dev, &dev->vidq);
1213 dprintk(1, "Open: list_empty queued=%d\n",
1214 list_empty(&dev->vidq.queued));
1215 dprintk(1, "Open: list_empty active=%d\n",
1216 list_empty(&dev->vidq.active));
1218 viu_default_settings(vr);
1220 status_cfg = in_be32(&vr->status_cfg);
1221 out_be32(&vr->status_cfg,
1222 status_cfg & ~(INT_VSYNC_EN | INT_HSYNC_EN |
1223 INT_FIELD_EN | INT_VSTART_EN |
1224 INT_DMA_END_EN | INT_ERROR_EN | INT_ECC_EN));
1226 status_cfg = in_be32(&vr->status_cfg);
1227 out_be32(&vr->status_cfg, status_cfg | INT_ALL_STATUS);
1229 spin_lock_init(&fh->vbq_lock);
1230 videobuf_queue_dma_contig_init(&fh->vb_vidq, &viu_video_qops,
1231 dev->dev, &fh->vbq_lock,
1232 fh->type, V4L2_FIELD_INTERLACED,
1233 sizeof(struct viu_buf), fh,
1235 v4l2_fh_add(&fh->fh);
1236 mutex_unlock(&dev->lock);
1240 static ssize_t viu_read(struct file *file, char __user *data, size_t count,
1243 struct viu_fh *fh = file->private_data;
1244 struct viu_dev *dev = fh->dev;
1247 dprintk(2, "%s\n", __func__);
1251 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1252 if (mutex_lock_interruptible(&dev->lock))
1253 return -ERESTARTSYS;
1255 ret = videobuf_read_stream(&fh->vb_vidq, data, count,
1256 ppos, 0, file->f_flags & O_NONBLOCK);
1257 mutex_unlock(&dev->lock);
1263 static __poll_t viu_poll(struct file *file, struct poll_table_struct *wait)
1265 struct viu_fh *fh = file->private_data;
1266 struct videobuf_queue *q = &fh->vb_vidq;
1267 struct viu_dev *dev = fh->dev;
1268 __poll_t req_events = poll_requested_events(wait);
1269 __poll_t res = v4l2_ctrl_poll(file, wait);
1271 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1274 if (!(req_events & (EPOLLIN | EPOLLRDNORM)))
1277 mutex_lock(&dev->lock);
1278 res |= videobuf_poll_stream(file, q, wait);
1279 mutex_unlock(&dev->lock);
1283 static int viu_release(struct file *file)
1285 struct viu_fh *fh = file->private_data;
1286 struct viu_dev *dev = fh->dev;
1287 int minor = video_devdata(file)->minor;
1289 mutex_lock(&dev->lock);
1291 videobuf_stop(&fh->vb_vidq);
1292 videobuf_mmap_free(&fh->vb_vidq);
1293 v4l2_fh_del(&fh->fh);
1294 v4l2_fh_exit(&fh->fh);
1295 mutex_unlock(&dev->lock);
1300 dprintk(1, "close (minor=%d, users=%d)\n",
1305 static void viu_reset(struct viu_reg __iomem *reg)
1307 out_be32(®->status_cfg, 0);
1308 out_be32(®->luminance, 0x9512a254);
1309 out_be32(®->chroma_r, 0x03310000);
1310 out_be32(®->chroma_g, 0x06600f38);
1311 out_be32(®->chroma_b, 0x00000409);
1312 out_be32(®->field_base_addr, 0);
1313 out_be32(®->dma_inc, 0);
1314 out_be32(®->picture_count, 0x01e002d0);
1315 out_be32(®->req_alarm, 0x00000090);
1316 out_be32(®->alpha, 0x000000ff);
1319 static int viu_mmap(struct file *file, struct vm_area_struct *vma)
1321 struct viu_fh *fh = file->private_data;
1322 struct viu_dev *dev = fh->dev;
1325 dprintk(1, "mmap called, vma=%p\n", vma);
1327 if (mutex_lock_interruptible(&dev->lock))
1328 return -ERESTARTSYS;
1329 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1330 mutex_unlock(&dev->lock);
1332 dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1333 (unsigned long)vma->vm_start,
1334 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1340 static const struct v4l2_file_operations viu_fops = {
1341 .owner = THIS_MODULE,
1343 .release = viu_release,
1346 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1350 static const struct v4l2_ioctl_ops viu_ioctl_ops = {
1351 .vidioc_querycap = vidioc_querycap,
1352 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1353 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_cap,
1354 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_cap,
1355 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_cap,
1356 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt,
1357 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_overlay,
1358 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_overlay,
1359 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_overlay,
1360 .vidioc_overlay = vidioc_overlay,
1361 .vidioc_g_fbuf = vidioc_g_fbuf,
1362 .vidioc_s_fbuf = vidioc_s_fbuf,
1363 .vidioc_reqbufs = vidioc_reqbufs,
1364 .vidioc_querybuf = vidioc_querybuf,
1365 .vidioc_qbuf = vidioc_qbuf,
1366 .vidioc_dqbuf = vidioc_dqbuf,
1367 .vidioc_g_std = vidioc_g_std,
1368 .vidioc_s_std = vidioc_s_std,
1369 .vidioc_querystd = vidioc_querystd,
1370 .vidioc_enum_input = vidioc_enum_input,
1371 .vidioc_g_input = vidioc_g_input,
1372 .vidioc_s_input = vidioc_s_input,
1373 .vidioc_streamon = vidioc_streamon,
1374 .vidioc_streamoff = vidioc_streamoff,
1375 .vidioc_log_status = v4l2_ctrl_log_status,
1376 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1377 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1380 static const struct video_device viu_template = {
1384 .ioctl_ops = &viu_ioctl_ops,
1385 .release = video_device_release,
1387 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1390 static int viu_of_probe(struct platform_device *op)
1392 struct viu_dev *viu_dev;
1393 struct video_device *vdev;
1395 struct viu_reg __iomem *viu_regs;
1396 struct i2c_adapter *ad;
1400 ret = of_address_to_resource(op->dev.of_node, 0, &r);
1402 dev_err(&op->dev, "Can't parse device node resource\n");
1406 viu_irq = irq_of_parse_and_map(op->dev.of_node, 0);
1408 dev_err(&op->dev, "Error while mapping the irq\n");
1412 /* request mem region */
1413 if (!devm_request_mem_region(&op->dev, r.start,
1414 sizeof(struct viu_reg), DRV_NAME)) {
1415 dev_err(&op->dev, "Error while requesting mem region\n");
1420 /* remap registers */
1421 viu_regs = devm_ioremap(&op->dev, r.start, sizeof(struct viu_reg));
1423 dev_err(&op->dev, "Can't map register set\n");
1428 /* Prepare our private structure */
1429 viu_dev = devm_kzalloc(&op->dev, sizeof(struct viu_dev), GFP_ATOMIC);
1431 dev_err(&op->dev, "Can't allocate private structure\n");
1436 viu_dev->vr = viu_regs;
1437 viu_dev->irq = viu_irq;
1438 viu_dev->dev = &op->dev;
1440 /* init video dma queues */
1441 INIT_LIST_HEAD(&viu_dev->vidq.active);
1442 INIT_LIST_HEAD(&viu_dev->vidq.queued);
1444 snprintf(viu_dev->v4l2_dev.name,
1445 sizeof(viu_dev->v4l2_dev.name), "%s", "VIU");
1446 ret = v4l2_device_register(viu_dev->dev, &viu_dev->v4l2_dev);
1448 dev_err(&op->dev, "v4l2_device_register() failed: %d\n", ret);
1452 ad = i2c_get_adapter(0);
1455 dev_err(&op->dev, "couldn't get i2c adapter\n");
1459 v4l2_ctrl_handler_init(&viu_dev->hdl, 5);
1460 if (viu_dev->hdl.error) {
1461 ret = viu_dev->hdl.error;
1462 dev_err(&op->dev, "couldn't register control\n");
1465 /* This control handler will inherit the control(s) from the
1467 viu_dev->v4l2_dev.ctrl_handler = &viu_dev->hdl;
1468 viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
1469 "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
1471 timer_setup(&viu_dev->vidq.timeout, viu_vid_timeout, 0);
1472 viu_dev->std = V4L2_STD_NTSC_M;
1475 /* Allocate memory for video device */
1476 vdev = video_device_alloc();
1482 *vdev = viu_template;
1484 vdev->v4l2_dev = &viu_dev->v4l2_dev;
1486 viu_dev->vdev = vdev;
1488 /* initialize locks */
1489 mutex_init(&viu_dev->lock);
1490 viu_dev->vdev->lock = &viu_dev->lock;
1491 spin_lock_init(&viu_dev->slock);
1493 video_set_drvdata(viu_dev->vdev, viu_dev);
1495 mutex_lock(&viu_dev->lock);
1497 ret = video_register_device(viu_dev->vdev, VFL_TYPE_GRABBER, -1);
1499 video_device_release(viu_dev->vdev);
1503 /* enable VIU clock */
1504 clk = devm_clk_get(&op->dev, "ipg");
1506 dev_err(&op->dev, "failed to lookup the clock!\n");
1510 ret = clk_prepare_enable(clk);
1512 dev_err(&op->dev, "failed to enable the clock!\n");
1517 /* reset VIU module */
1518 viu_reset(viu_dev->vr);
1520 /* install interrupt handler */
1521 if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
1522 dev_err(&op->dev, "Request VIU IRQ failed.\n");
1527 mutex_unlock(&viu_dev->lock);
1529 dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
1533 clk_disable_unprepare(viu_dev->clk);
1535 video_unregister_device(viu_dev->vdev);
1537 mutex_unlock(&viu_dev->lock);
1539 v4l2_ctrl_handler_free(&viu_dev->hdl);
1541 i2c_put_adapter(ad);
1543 v4l2_device_unregister(&viu_dev->v4l2_dev);
1545 irq_dispose_mapping(viu_irq);
1549 static int viu_of_remove(struct platform_device *op)
1551 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1552 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1553 struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
1554 struct v4l2_subdev, list);
1555 struct i2c_client *client = v4l2_get_subdevdata(sdev);
1557 free_irq(dev->irq, (void *)dev);
1558 irq_dispose_mapping(dev->irq);
1560 clk_disable_unprepare(dev->clk);
1562 v4l2_ctrl_handler_free(&dev->hdl);
1563 video_unregister_device(dev->vdev);
1564 i2c_put_adapter(client->adapter);
1565 v4l2_device_unregister(&dev->v4l2_dev);
1570 static int viu_suspend(struct platform_device *op, pm_message_t state)
1572 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1573 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1575 clk_disable(dev->clk);
1579 static int viu_resume(struct platform_device *op)
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_enable(dev->clk);
1590 * Initialization and module stuff
1592 static const struct of_device_id mpc512x_viu_of_match[] = {
1594 .compatible = "fsl,mpc5121-viu",
1598 MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
1600 static struct platform_driver viu_of_platform_driver = {
1601 .probe = viu_of_probe,
1602 .remove = viu_of_remove,
1604 .suspend = viu_suspend,
1605 .resume = viu_resume,
1609 .of_match_table = mpc512x_viu_of_match,
1613 module_platform_driver(viu_of_platform_driver);
1615 MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1616 MODULE_AUTHOR("Hongjun Chen");
1617 MODULE_LICENSE("GPL");
1618 MODULE_VERSION(VIU_VERSION);