1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
5 * Freescale VIU video driver
7 * Authors: Hongjun Chen <hong-jun.chen@freescale.com>
8 * Porting to 2.6.35 by DENX Software Engineering,
9 * Anatolij Gustschin <agust@denx.de>
12 #include <linux/module.h>
13 #include <linux/clk.h>
14 #include <linux/kernel.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
19 #include <linux/of_address.h>
20 #include <linux/of_irq.h>
21 #include <linux/of_platform.h>
22 #include <linux/slab.h>
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-fh.h>
28 #include <media/v4l2-event.h>
29 #include <media/videobuf-dma-contig.h>
31 #define DRV_NAME "fsl_viu"
32 #define VIU_VERSION "0.5.1"
34 /* Allow building this driver with COMPILE_TEST */
35 #if !defined(CONFIG_PPC) && !defined(CONFIG_MICROBLAZE)
36 #define out_be32(v, a) iowrite32be(a, (void __iomem *)v)
37 #define in_be32(a) ioread32be((void __iomem *)a)
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 static int info_level;
49 #define dprintk(level, fmt, arg...) \
51 if (level <= info_level) \
52 printk(KERN_DEBUG "viu: " fmt , ## arg); \
59 u32 fourcc; /* v4l2 format id */
64 static struct viu_fmt formats[] = {
66 .fourcc = V4L2_PIX_FMT_RGB565,
67 .pixelformat = V4L2_PIX_FMT_RGB565,
70 .fourcc = V4L2_PIX_FMT_RGB32,
71 .pixelformat = V4L2_PIX_FMT_RGB32,
79 /* buffer for one video frame */
81 /* common v4l buffer stuff -- must be first */
82 struct videobuf_buffer vb;
88 struct list_head active;
89 struct list_head queued;
90 struct timer_list timeout;
113 } __attribute__ ((packed));
116 struct v4l2_device v4l2_dev;
117 struct v4l2_ctrl_handler hdl;
123 /* various device info */
124 struct video_device *vdev;
125 struct viu_dmaqueue vidq;
126 enum v4l2_field capfield;
131 /* Hardware register area */
132 struct viu_reg __iomem *vr;
134 /* Interrupt vector */
136 struct viu_status irqs;
139 struct v4l2_framebuffer ovbuf;
140 struct viu_fmt *ovfmt;
141 unsigned int ovenable;
142 enum v4l2_field ovfield;
145 struct v4l2_rect crop_current;
151 struct v4l2_subdev *decoder;
157 /* must remain the first field of this struct */
162 struct videobuf_queue vb_vidq;
163 spinlock_t vbq_lock; /* spinlock for the videobuf queue */
166 struct v4l2_window win;
167 struct v4l2_clip clips[1];
171 int width, height, sizeimage;
172 enum v4l2_buf_type type;
175 static struct viu_reg reg_val;
178 * Macro definitions of VIU registers
181 /* STATUS_CONFIG register */
185 ERR_MASK = 0x0f << 4, /* Error code mask */
186 ERR_NO = 0x00, /* No error */
187 ERR_DMA_V = 0x01 << 4, /* DMA in vertical active */
188 ERR_DMA_VB = 0x02 << 4, /* DMA in vertical blanking */
189 ERR_LINE_TOO_LONG = 0x04 << 4, /* Line too long */
190 ERR_TOO_MANG_LINES = 0x05 << 4, /* Too many lines in field */
191 ERR_LINE_TOO_SHORT = 0x06 << 4, /* Line too short */
192 ERR_NOT_ENOUGH_LINE = 0x07 << 4, /* Not enough lines in field */
193 ERR_FIFO_OVERFLOW = 0x08 << 4, /* FIFO overflow */
194 ERR_FIFO_UNDERFLOW = 0x09 << 4, /* FIFO underflow */
195 ERR_1bit_ECC = 0x0a << 4, /* One bit ECC error */
196 ERR_MORE_ECC = 0x0b << 4, /* Two/more bits ECC error */
198 INT_FIELD_EN = 0x01 << 8, /* Enable field interrupt */
199 INT_VSYNC_EN = 0x01 << 9, /* Enable vsync interrupt */
200 INT_HSYNC_EN = 0x01 << 10, /* Enable hsync interrupt */
201 INT_VSTART_EN = 0x01 << 11, /* Enable vstart interrupt */
202 INT_DMA_END_EN = 0x01 << 12, /* Enable DMA end interrupt */
203 INT_ERROR_EN = 0x01 << 13, /* Enable error interrupt */
204 INT_ECC_EN = 0x01 << 14, /* Enable ECC interrupt */
206 INT_FIELD_STATUS = 0x01 << 16, /* field interrupt status */
207 INT_VSYNC_STATUS = 0x01 << 17, /* vsync interrupt status */
208 INT_HSYNC_STATUS = 0x01 << 18, /* hsync interrupt status */
209 INT_VSTART_STATUS = 0x01 << 19, /* vstart interrupt status */
210 INT_DMA_END_STATUS = 0x01 << 20, /* DMA end interrupt status */
211 INT_ERROR_STATUS = 0x01 << 21, /* error interrupt status */
213 DMA_ACT = 0x01 << 27, /* Enable DMA transfer */
214 FIELD_NO = 0x01 << 28, /* Field number */
215 DITHER_ON = 0x01 << 29, /* Dithering is on */
216 ROUND_ON = 0x01 << 30, /* Round is on */
217 MODE_32BIT = 1UL << 31, /* Data in RGBa888,
222 #define norm_maxw() 720
223 #define norm_maxh() 576
225 #define INT_ALL_STATUS (INT_FIELD_STATUS | INT_VSYNC_STATUS | \
226 INT_HSYNC_STATUS | INT_VSTART_STATUS | \
227 INT_DMA_END_STATUS | INT_ERROR_STATUS)
229 #define NUM_FORMATS ARRAY_SIZE(formats)
231 static irqreturn_t viu_intr(int irq, void *dev_id);
233 static struct viu_fmt *format_by_fourcc(int fourcc)
237 for (i = 0; i < NUM_FORMATS; i++) {
238 if (formats[i].pixelformat == fourcc)
242 dprintk(0, "unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
246 static void viu_start_dma(struct viu_dev *dev)
248 struct viu_reg __iomem *vr = dev->vr;
252 /* Enable DMA operation */
253 out_be32(&vr->status_cfg, SOFT_RST);
254 out_be32(&vr->status_cfg, INT_FIELD_EN);
257 static void viu_stop_dma(struct viu_dev *dev)
259 struct viu_reg __iomem *vr = dev->vr;
263 out_be32(&vr->status_cfg, 0);
265 /* Clear pending interrupts */
266 status_cfg = in_be32(&vr->status_cfg);
267 if (status_cfg & 0x3f0000)
268 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
270 if (status_cfg & DMA_ACT) {
272 status_cfg = in_be32(&vr->status_cfg);
273 if (status_cfg & INT_DMA_END_STATUS)
278 /* timed out, issue soft reset */
279 out_be32(&vr->status_cfg, SOFT_RST);
280 out_be32(&vr->status_cfg, 0);
282 /* clear DMA_END and other pending irqs */
283 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
290 static int restart_video_queue(struct viu_dmaqueue *vidq)
292 struct viu_buf *buf, *prev;
294 dprintk(1, "%s vidq=%p\n", __func__, vidq);
295 if (!list_empty(&vidq->active)) {
296 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
297 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
300 viu_stop_dma(vidq->dev);
302 /* cancel all outstanding capture requests */
303 list_for_each_entry_safe(buf, prev, &vidq->active, vb.queue) {
304 list_del(&buf->vb.queue);
305 buf->vb.state = VIDEOBUF_ERROR;
306 wake_up(&buf->vb.done);
308 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
314 if (list_empty(&vidq->queued))
316 buf = list_entry(vidq->queued.next, struct viu_buf, vb.queue);
318 list_move_tail(&buf->vb.queue, &vidq->active);
320 dprintk(1, "Restarting video dma\n");
321 viu_stop_dma(vidq->dev);
322 viu_start_dma(vidq->dev);
324 buf->vb.state = VIDEOBUF_ACTIVE;
325 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
326 dprintk(2, "[%p/%d] restart_queue - first active\n",
329 } else if (prev->vb.width == buf->vb.width &&
330 prev->vb.height == buf->vb.height &&
331 prev->fmt == buf->fmt) {
332 list_move_tail(&buf->vb.queue, &vidq->active);
333 buf->vb.state = VIDEOBUF_ACTIVE;
334 dprintk(2, "[%p/%d] restart_queue - move to active\n",
343 static void viu_vid_timeout(struct timer_list *t)
345 struct viu_dev *dev = from_timer(dev, t, vidq.timeout);
347 struct viu_dmaqueue *vidq = &dev->vidq;
349 while (!list_empty(&vidq->active)) {
350 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
351 list_del(&buf->vb.queue);
352 buf->vb.state = VIDEOBUF_ERROR;
353 wake_up(&buf->vb.done);
354 dprintk(1, "viu/0: [%p/%d] timeout\n", buf, buf->vb.i);
357 restart_video_queue(vidq);
361 * Videobuf operations
363 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
366 struct viu_fh *fh = vq->priv_data;
368 *size = fh->width * fh->height * fh->fmt->depth >> 3;
372 while (*size * *count > VIU_VID_MEM_LIMIT * 1024 * 1024)
375 dprintk(1, "%s, count=%d, size=%d\n", __func__, *count, *size);
379 static void free_buffer(struct videobuf_queue *vq, struct viu_buf *buf)
381 struct videobuf_buffer *vb = &buf->vb;
384 BUG_ON(in_interrupt());
386 videobuf_waiton(vq, &buf->vb, 0, 0);
388 if (vq->int_ops && vq->int_ops->vaddr)
389 vaddr = vq->int_ops->vaddr(vb);
392 videobuf_dma_contig_free(vq, &buf->vb);
394 buf->vb.state = VIDEOBUF_NEEDS_INIT;
397 inline int buffer_activate(struct viu_dev *dev, struct viu_buf *buf)
399 struct viu_reg __iomem *vr = dev->vr;
402 /* setup the DMA base address */
403 reg_val.field_base_addr = videobuf_to_dma_contig(&buf->vb);
405 dprintk(1, "buffer_activate [%p/%d]: dma addr 0x%lx\n",
406 buf, buf->vb.i, (unsigned long)reg_val.field_base_addr);
408 /* interlace is on by default, set horizontal DMA increment */
409 reg_val.status_cfg = 0;
410 bpp = buf->fmt->depth >> 3;
413 reg_val.status_cfg &= ~MODE_32BIT;
414 reg_val.dma_inc = buf->vb.width * 2;
417 reg_val.status_cfg |= MODE_32BIT;
418 reg_val.dma_inc = buf->vb.width * 4;
421 dprintk(0, "doesn't support color depth(%d)\n",
426 /* setup picture_count register */
427 reg_val.picture_count = (buf->vb.height / 2) << 16 |
430 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
432 buf->vb.state = VIDEOBUF_ACTIVE;
433 dev->capfield = buf->vb.field;
435 /* reset dma increment if needed */
436 if (!V4L2_FIELD_HAS_BOTH(buf->vb.field))
439 out_be32(&vr->dma_inc, reg_val.dma_inc);
440 out_be32(&vr->picture_count, reg_val.picture_count);
441 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
442 mod_timer(&dev->vidq.timeout, jiffies + BUFFER_TIMEOUT);
446 static int buffer_prepare(struct videobuf_queue *vq,
447 struct videobuf_buffer *vb,
448 enum v4l2_field field)
450 struct viu_fh *fh = vq->priv_data;
451 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
454 BUG_ON(fh->fmt == NULL);
456 if (fh->width < 48 || fh->width > norm_maxw() ||
457 fh->height < 32 || fh->height > norm_maxh())
459 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
460 if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size)
463 if (buf->fmt != fh->fmt ||
464 buf->vb.width != fh->width ||
465 buf->vb.height != fh->height ||
466 buf->vb.field != field) {
468 buf->vb.width = fh->width;
469 buf->vb.height = fh->height;
470 buf->vb.field = field;
473 if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
474 rc = videobuf_iolock(vq, &buf->vb, NULL);
478 buf->vb.width = fh->width;
479 buf->vb.height = fh->height;
480 buf->vb.field = field;
484 buf->vb.state = VIDEOBUF_PREPARED;
488 free_buffer(vq, buf);
492 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
494 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
495 struct viu_fh *fh = vq->priv_data;
496 struct viu_dev *dev = fh->dev;
497 struct viu_dmaqueue *vidq = &dev->vidq;
498 struct viu_buf *prev;
500 if (!list_empty(&vidq->queued)) {
501 dprintk(1, "adding vb queue=%p\n", &buf->vb.queue);
502 dprintk(1, "vidq pointer 0x%p, queued 0x%p\n",
503 vidq, &vidq->queued);
504 dprintk(1, "dev %p, queued: self %p, next %p, head %p\n",
505 dev, &vidq->queued, vidq->queued.next,
507 list_add_tail(&buf->vb.queue, &vidq->queued);
508 buf->vb.state = VIDEOBUF_QUEUED;
509 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
511 } else if (list_empty(&vidq->active)) {
512 dprintk(1, "adding vb active=%p\n", &buf->vb.queue);
513 list_add_tail(&buf->vb.queue, &vidq->active);
514 buf->vb.state = VIDEOBUF_ACTIVE;
515 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
516 dprintk(2, "[%p/%d] buffer_queue - first active\n",
519 buffer_activate(dev, buf);
521 dprintk(1, "adding vb queue2=%p\n", &buf->vb.queue);
522 prev = list_entry(vidq->active.prev, struct viu_buf, vb.queue);
523 if (prev->vb.width == buf->vb.width &&
524 prev->vb.height == buf->vb.height &&
525 prev->fmt == buf->fmt) {
526 list_add_tail(&buf->vb.queue, &vidq->active);
527 buf->vb.state = VIDEOBUF_ACTIVE;
528 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
531 list_add_tail(&buf->vb.queue, &vidq->queued);
532 buf->vb.state = VIDEOBUF_QUEUED;
533 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
539 static void buffer_release(struct videobuf_queue *vq,
540 struct videobuf_buffer *vb)
542 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
543 struct viu_fh *fh = vq->priv_data;
544 struct viu_dev *dev = (struct viu_dev *)fh->dev;
547 free_buffer(vq, buf);
550 static const struct videobuf_queue_ops viu_video_qops = {
551 .buf_setup = buffer_setup,
552 .buf_prepare = buffer_prepare,
553 .buf_queue = buffer_queue,
554 .buf_release = buffer_release,
558 * IOCTL vidioc handling
560 static int vidioc_querycap(struct file *file, void *priv,
561 struct v4l2_capability *cap)
563 strscpy(cap->driver, "viu", sizeof(cap->driver));
564 strscpy(cap->card, "viu", sizeof(cap->card));
565 strscpy(cap->bus_info, "platform:viu", sizeof(cap->bus_info));
569 static int vidioc_enum_fmt(struct file *file, void *priv,
570 struct v4l2_fmtdesc *f)
572 int index = f->index;
574 if (f->index >= NUM_FORMATS)
577 f->pixelformat = formats[index].fourcc;
581 static int vidioc_g_fmt_cap(struct file *file, void *priv,
582 struct v4l2_format *f)
584 struct viu_fh *fh = priv;
586 f->fmt.pix.width = fh->width;
587 f->fmt.pix.height = fh->height;
588 f->fmt.pix.field = fh->vb_vidq.field;
589 f->fmt.pix.pixelformat = fh->fmt->pixelformat;
590 f->fmt.pix.bytesperline =
591 (f->fmt.pix.width * fh->fmt->depth) >> 3;
592 f->fmt.pix.sizeimage = fh->sizeimage;
593 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
597 static int vidioc_try_fmt_cap(struct file *file, void *priv,
598 struct v4l2_format *f)
601 unsigned int maxw, maxh;
603 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
605 dprintk(1, "Fourcc format (0x%08x) invalid.",
606 f->fmt.pix.pixelformat);
613 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
614 if (f->fmt.pix.height < 32)
615 f->fmt.pix.height = 32;
616 if (f->fmt.pix.height > maxh)
617 f->fmt.pix.height = maxh;
618 if (f->fmt.pix.width < 48)
619 f->fmt.pix.width = 48;
620 if (f->fmt.pix.width > maxw)
621 f->fmt.pix.width = maxw;
622 f->fmt.pix.width &= ~0x03;
623 f->fmt.pix.bytesperline =
624 (f->fmt.pix.width * fmt->depth) >> 3;
625 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
626 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
631 static int vidioc_s_fmt_cap(struct file *file, void *priv,
632 struct v4l2_format *f)
634 struct viu_fh *fh = priv;
637 ret = vidioc_try_fmt_cap(file, fh, f);
641 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
642 fh->width = f->fmt.pix.width;
643 fh->height = f->fmt.pix.height;
644 fh->sizeimage = f->fmt.pix.sizeimage;
645 fh->vb_vidq.field = f->fmt.pix.field;
650 static int vidioc_g_fmt_overlay(struct file *file, void *priv,
651 struct v4l2_format *f)
653 struct viu_fh *fh = priv;
655 f->fmt.win = fh->win;
659 static int verify_preview(struct viu_dev *dev, struct v4l2_window *win)
661 enum v4l2_field field;
664 if (dev->ovbuf.base == NULL)
666 if (dev->ovfmt == NULL)
668 if (win->w.width < 48 || win->w.height < 32)
672 maxw = dev->crop_current.width;
673 maxh = dev->crop_current.height;
675 if (field == V4L2_FIELD_ANY) {
676 field = (win->w.height > maxh/2)
677 ? V4L2_FIELD_INTERLACED
682 case V4L2_FIELD_BOTTOM:
685 case V4L2_FIELD_INTERLACED:
692 if (win->w.width > maxw)
694 if (win->w.height > maxh)
695 win->w.height = maxh;
699 inline void viu_activate_overlay(struct viu_reg __iomem *vr)
701 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
702 out_be32(&vr->dma_inc, reg_val.dma_inc);
703 out_be32(&vr->picture_count, reg_val.picture_count);
706 static int viu_setup_preview(struct viu_dev *dev, struct viu_fh *fh)
710 dprintk(1, "%s %dx%d\n", __func__,
711 fh->win.w.width, fh->win.w.height);
713 reg_val.status_cfg = 0;
716 reg_val.picture_count = (fh->win.w.height / 2) << 16 |
719 /* setup color depth and dma increment */
720 bpp = dev->ovfmt->depth / 8;
723 reg_val.status_cfg &= ~MODE_32BIT;
724 reg_val.dma_inc = fh->win.w.width * 2;
727 reg_val.status_cfg |= MODE_32BIT;
728 reg_val.dma_inc = fh->win.w.width * 4;
731 dprintk(0, "device doesn't support color depth(%d)\n",
736 dev->ovfield = fh->win.field;
737 if (!V4L2_FIELD_HAS_BOTH(dev->ovfield))
740 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
742 /* setup the base address of the overlay buffer */
743 reg_val.field_base_addr = (u32)(long)dev->ovbuf.base;
748 static int vidioc_s_fmt_overlay(struct file *file, void *priv,
749 struct v4l2_format *f)
751 struct viu_fh *fh = priv;
752 struct viu_dev *dev = (struct viu_dev *)fh->dev;
756 err = verify_preview(dev, &f->fmt.win);
760 fh->win = f->fmt.win;
762 spin_lock_irqsave(&dev->slock, flags);
763 viu_setup_preview(dev, fh);
764 spin_unlock_irqrestore(&dev->slock, flags);
768 static int vidioc_try_fmt_overlay(struct file *file, void *priv,
769 struct v4l2_format *f)
774 static int vidioc_overlay(struct file *file, void *priv, unsigned int on)
776 struct viu_fh *fh = priv;
777 struct viu_dev *dev = (struct viu_dev *)fh->dev;
781 spin_lock_irqsave(&dev->slock, flags);
782 viu_activate_overlay(dev->vr);
787 spin_unlock_irqrestore(&dev->slock, flags);
796 static int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
798 struct viu_fh *fh = priv;
799 struct viu_dev *dev = fh->dev;
800 struct v4l2_framebuffer *fb = arg;
803 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
807 static int vidioc_s_fbuf(struct file *file, void *priv, const struct v4l2_framebuffer *arg)
809 struct viu_fh *fh = priv;
810 struct viu_dev *dev = fh->dev;
811 const struct v4l2_framebuffer *fb = arg;
814 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
818 fmt = format_by_fourcc(fb->fmt.pixelformat);
825 if (dev->ovbuf.fmt.bytesperline == 0) {
826 dev->ovbuf.fmt.bytesperline =
827 dev->ovbuf.fmt.width * fmt->depth / 8;
832 static int vidioc_reqbufs(struct file *file, void *priv,
833 struct v4l2_requestbuffers *p)
835 struct viu_fh *fh = priv;
837 return videobuf_reqbufs(&fh->vb_vidq, p);
840 static int vidioc_querybuf(struct file *file, void *priv,
841 struct v4l2_buffer *p)
843 struct viu_fh *fh = priv;
845 return videobuf_querybuf(&fh->vb_vidq, p);
848 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
850 struct viu_fh *fh = priv;
852 return videobuf_qbuf(&fh->vb_vidq, p);
855 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
857 struct viu_fh *fh = priv;
859 return videobuf_dqbuf(&fh->vb_vidq, p,
860 file->f_flags & O_NONBLOCK);
863 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
865 struct viu_fh *fh = priv;
866 struct viu_dev *dev = fh->dev;
868 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
876 viu_start_dma(fh->dev);
878 return videobuf_streamon(&fh->vb_vidq);
881 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
883 struct viu_fh *fh = priv;
885 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
890 viu_stop_dma(fh->dev);
892 return videobuf_streamoff(&fh->vb_vidq);
895 #define decoder_call(viu, o, f, args...) \
896 v4l2_subdev_call(viu->decoder, o, f, ##args)
898 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
900 struct viu_fh *fh = priv;
902 decoder_call(fh->dev, video, querystd, std_id);
906 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
908 struct viu_fh *fh = priv;
911 decoder_call(fh->dev, video, s_std, id);
915 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
917 struct viu_fh *fh = priv;
919 *std_id = fh->dev->std;
923 /* only one input in this driver */
924 static int vidioc_enum_input(struct file *file, void *priv,
925 struct v4l2_input *inp)
927 struct viu_fh *fh = priv;
932 inp->type = V4L2_INPUT_TYPE_CAMERA;
933 inp->std = fh->dev->vdev->tvnorms;
934 strscpy(inp->name, "Camera", sizeof(inp->name));
938 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
944 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
946 struct viu_fh *fh = priv;
951 decoder_call(fh->dev, video, s_routing, i, 0, 0);
955 inline void viu_activate_next_buf(struct viu_dev *dev,
956 struct viu_dmaqueue *viuq)
958 struct viu_dmaqueue *vidq = viuq;
961 /* launch another DMA operation for an active/queued buffer */
962 if (!list_empty(&vidq->active)) {
963 buf = list_entry(vidq->active.next, struct viu_buf,
965 dprintk(1, "start another queued buffer: 0x%p\n", buf);
966 buffer_activate(dev, buf);
967 } else if (!list_empty(&vidq->queued)) {
968 buf = list_entry(vidq->queued.next, struct viu_buf,
970 list_del(&buf->vb.queue);
972 dprintk(1, "start another queued buffer: 0x%p\n", buf);
973 list_add_tail(&buf->vb.queue, &vidq->active);
974 buf->vb.state = VIDEOBUF_ACTIVE;
975 buffer_activate(dev, buf);
979 inline void viu_default_settings(struct viu_reg __iomem *vr)
981 out_be32(&vr->luminance, 0x9512A254);
982 out_be32(&vr->chroma_r, 0x03310000);
983 out_be32(&vr->chroma_g, 0x06600F38);
984 out_be32(&vr->chroma_b, 0x00000409);
985 out_be32(&vr->alpha, 0x000000ff);
986 out_be32(&vr->req_alarm, 0x00000090);
987 dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
988 in_be32(&vr->status_cfg), in_be32(&vr->field_base_addr));
991 static void viu_overlay_intr(struct viu_dev *dev, u32 status)
993 struct viu_reg __iomem *vr = dev->vr;
995 if (status & INT_DMA_END_STATUS)
998 if (status & INT_FIELD_STATUS) {
1000 u32 addr = reg_val.field_base_addr;
1003 if (status & FIELD_NO)
1004 addr += reg_val.dma_inc;
1006 out_be32(&vr->field_base_addr, addr);
1007 out_be32(&vr->dma_inc, reg_val.dma_inc);
1008 out_be32(&vr->status_cfg,
1009 (status & 0xffc0ffff) |
1010 (status & INT_ALL_STATUS) |
1011 reg_val.status_cfg);
1012 } else if (status & INT_VSYNC_STATUS) {
1013 out_be32(&vr->status_cfg,
1014 (status & 0xffc0ffff) |
1015 (status & INT_ALL_STATUS) |
1016 reg_val.status_cfg);
1021 static void viu_capture_intr(struct viu_dev *dev, u32 status)
1023 struct viu_dmaqueue *vidq = &dev->vidq;
1024 struct viu_reg __iomem *vr = dev->vr;
1025 struct viu_buf *buf;
1030 field_num = status & FIELD_NO;
1031 need_two = V4L2_FIELD_HAS_BOTH(dev->capfield);
1033 if (status & INT_DMA_END_STATUS) {
1035 if (((field_num == 0) && (dev->field == 0)) ||
1036 (field_num && (dev->field == 1)))
1040 if (status & INT_FIELD_STATUS) {
1041 dprintk(1, "irq: field %d, done %d\n",
1042 !!field_num, dma_done);
1043 if (unlikely(dev->first)) {
1044 if (field_num == 0) {
1046 dprintk(1, "activate first buf\n");
1047 viu_activate_next_buf(dev, vidq);
1049 dprintk(1, "wait field 0\n");
1053 /* setup buffer address for next dma operation */
1054 if (!list_empty(&vidq->active)) {
1055 u32 addr = reg_val.field_base_addr;
1057 if (field_num && need_two) {
1058 addr += reg_val.dma_inc;
1059 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1060 (unsigned long)addr, dev->field);
1062 out_be32(&vr->field_base_addr, addr);
1063 out_be32(&vr->dma_inc, reg_val.dma_inc);
1064 out_be32(&vr->status_cfg,
1065 (status & 0xffc0ffff) |
1066 (status & INT_ALL_STATUS) |
1067 reg_val.status_cfg);
1072 if (dma_done && field_num && (dev->field == 2)) {
1074 buf = list_entry(vidq->active.next,
1075 struct viu_buf, vb.queue);
1076 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1078 (unsigned long)videobuf_to_dma_contig(&buf->vb),
1079 (unsigned long)in_be32(&vr->field_base_addr));
1081 if (waitqueue_active(&buf->vb.done)) {
1082 list_del(&buf->vb.queue);
1083 buf->vb.ts = ktime_get_ns();
1084 buf->vb.state = VIDEOBUF_DONE;
1085 buf->vb.field_count++;
1086 wake_up(&buf->vb.done);
1088 /* activate next dma buffer */
1089 viu_activate_next_buf(dev, vidq);
1093 static irqreturn_t viu_intr(int irq, void *dev_id)
1095 struct viu_dev *dev = (struct viu_dev *)dev_id;
1096 struct viu_reg __iomem *vr = dev->vr;
1100 status = in_be32(&vr->status_cfg);
1102 if (status & INT_ERROR_STATUS) {
1103 dev->irqs.error_irq++;
1104 error = status & ERR_MASK;
1106 dprintk(1, "Err: error(%d), times:%d!\n",
1107 error >> 4, dev->irqs.error_irq);
1108 /* Clear interrupt error bit and error flags */
1109 out_be32(&vr->status_cfg,
1110 (status & 0xffc0ffff) | INT_ERROR_STATUS);
1113 if (status & INT_DMA_END_STATUS) {
1114 dev->irqs.dma_end_irq++;
1116 dprintk(2, "VIU DMA end interrupt times: %d\n",
1117 dev->irqs.dma_end_irq);
1120 if (status & INT_HSYNC_STATUS)
1121 dev->irqs.hsync_irq++;
1123 if (status & INT_FIELD_STATUS) {
1124 dev->irqs.field_irq++;
1125 dprintk(2, "VIU field interrupt times: %d\n",
1126 dev->irqs.field_irq);
1129 if (status & INT_VSTART_STATUS)
1130 dev->irqs.vstart_irq++;
1132 if (status & INT_VSYNC_STATUS) {
1133 dev->irqs.vsync_irq++;
1134 dprintk(2, "VIU vsync interrupt times: %d\n",
1135 dev->irqs.vsync_irq);
1138 /* clear all pending irqs */
1139 status = in_be32(&vr->status_cfg);
1140 out_be32(&vr->status_cfg,
1141 (status & 0xffc0ffff) | (status & INT_ALL_STATUS));
1143 if (dev->ovenable) {
1144 viu_overlay_intr(dev, status);
1149 viu_capture_intr(dev, status);
1154 * File operations for the device
1156 static int viu_open(struct file *file)
1158 struct video_device *vdev = video_devdata(file);
1159 struct viu_dev *dev = video_get_drvdata(vdev);
1161 struct viu_reg __iomem *vr;
1162 int minor = vdev->minor;
1165 dprintk(1, "viu: open (minor=%d)\n", minor);
1168 if (dev->users > 1) {
1175 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1176 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1178 if (mutex_lock_interruptible(&dev->lock)) {
1180 return -ERESTARTSYS;
1183 /* allocate and initialize per filehandle data */
1184 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1187 mutex_unlock(&dev->lock);
1191 v4l2_fh_init(&fh->fh, vdev);
1192 file->private_data = fh;
1195 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1196 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_RGB32);
1197 fh->width = norm_maxw();
1198 fh->height = norm_maxh();
1199 dev->crop_current.width = fh->width;
1200 dev->crop_current.height = fh->height;
1202 dprintk(1, "Open: fh=%p, dev=%p, dev->vidq=%p\n", fh, dev, &dev->vidq);
1203 dprintk(1, "Open: list_empty queued=%d\n",
1204 list_empty(&dev->vidq.queued));
1205 dprintk(1, "Open: list_empty active=%d\n",
1206 list_empty(&dev->vidq.active));
1208 viu_default_settings(vr);
1210 status_cfg = in_be32(&vr->status_cfg);
1211 out_be32(&vr->status_cfg,
1212 status_cfg & ~(INT_VSYNC_EN | INT_HSYNC_EN |
1213 INT_FIELD_EN | INT_VSTART_EN |
1214 INT_DMA_END_EN | INT_ERROR_EN | INT_ECC_EN));
1216 status_cfg = in_be32(&vr->status_cfg);
1217 out_be32(&vr->status_cfg, status_cfg | INT_ALL_STATUS);
1219 spin_lock_init(&fh->vbq_lock);
1220 videobuf_queue_dma_contig_init(&fh->vb_vidq, &viu_video_qops,
1221 dev->dev, &fh->vbq_lock,
1222 fh->type, V4L2_FIELD_INTERLACED,
1223 sizeof(struct viu_buf), fh,
1225 v4l2_fh_add(&fh->fh);
1226 mutex_unlock(&dev->lock);
1230 static ssize_t viu_read(struct file *file, char __user *data, size_t count,
1233 struct viu_fh *fh = file->private_data;
1234 struct viu_dev *dev = fh->dev;
1237 dprintk(2, "%s\n", __func__);
1241 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1242 if (mutex_lock_interruptible(&dev->lock))
1243 return -ERESTARTSYS;
1245 ret = videobuf_read_stream(&fh->vb_vidq, data, count,
1246 ppos, 0, file->f_flags & O_NONBLOCK);
1247 mutex_unlock(&dev->lock);
1253 static __poll_t viu_poll(struct file *file, struct poll_table_struct *wait)
1255 struct viu_fh *fh = file->private_data;
1256 struct videobuf_queue *q = &fh->vb_vidq;
1257 struct viu_dev *dev = fh->dev;
1258 __poll_t req_events = poll_requested_events(wait);
1259 __poll_t res = v4l2_ctrl_poll(file, wait);
1261 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1264 if (!(req_events & (EPOLLIN | EPOLLRDNORM)))
1267 mutex_lock(&dev->lock);
1268 res |= videobuf_poll_stream(file, q, wait);
1269 mutex_unlock(&dev->lock);
1273 static int viu_release(struct file *file)
1275 struct viu_fh *fh = file->private_data;
1276 struct viu_dev *dev = fh->dev;
1277 int minor = video_devdata(file)->minor;
1279 mutex_lock(&dev->lock);
1281 videobuf_stop(&fh->vb_vidq);
1282 videobuf_mmap_free(&fh->vb_vidq);
1283 v4l2_fh_del(&fh->fh);
1284 v4l2_fh_exit(&fh->fh);
1285 mutex_unlock(&dev->lock);
1290 dprintk(1, "close (minor=%d, users=%d)\n",
1295 static void viu_reset(struct viu_reg __iomem *reg)
1297 out_be32(®->status_cfg, 0);
1298 out_be32(®->luminance, 0x9512a254);
1299 out_be32(®->chroma_r, 0x03310000);
1300 out_be32(®->chroma_g, 0x06600f38);
1301 out_be32(®->chroma_b, 0x00000409);
1302 out_be32(®->field_base_addr, 0);
1303 out_be32(®->dma_inc, 0);
1304 out_be32(®->picture_count, 0x01e002d0);
1305 out_be32(®->req_alarm, 0x00000090);
1306 out_be32(®->alpha, 0x000000ff);
1309 static int viu_mmap(struct file *file, struct vm_area_struct *vma)
1311 struct viu_fh *fh = file->private_data;
1312 struct viu_dev *dev = fh->dev;
1315 dprintk(1, "mmap called, vma=%p\n", vma);
1317 if (mutex_lock_interruptible(&dev->lock))
1318 return -ERESTARTSYS;
1319 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1320 mutex_unlock(&dev->lock);
1322 dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1323 (unsigned long)vma->vm_start,
1324 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1330 static const struct v4l2_file_operations viu_fops = {
1331 .owner = THIS_MODULE,
1333 .release = viu_release,
1336 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1340 static const struct v4l2_ioctl_ops viu_ioctl_ops = {
1341 .vidioc_querycap = vidioc_querycap,
1342 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1343 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_cap,
1344 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_cap,
1345 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_cap,
1346 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt,
1347 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_overlay,
1348 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_overlay,
1349 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_overlay,
1350 .vidioc_overlay = vidioc_overlay,
1351 .vidioc_g_fbuf = vidioc_g_fbuf,
1352 .vidioc_s_fbuf = vidioc_s_fbuf,
1353 .vidioc_reqbufs = vidioc_reqbufs,
1354 .vidioc_querybuf = vidioc_querybuf,
1355 .vidioc_qbuf = vidioc_qbuf,
1356 .vidioc_dqbuf = vidioc_dqbuf,
1357 .vidioc_g_std = vidioc_g_std,
1358 .vidioc_s_std = vidioc_s_std,
1359 .vidioc_querystd = vidioc_querystd,
1360 .vidioc_enum_input = vidioc_enum_input,
1361 .vidioc_g_input = vidioc_g_input,
1362 .vidioc_s_input = vidioc_s_input,
1363 .vidioc_streamon = vidioc_streamon,
1364 .vidioc_streamoff = vidioc_streamoff,
1365 .vidioc_log_status = v4l2_ctrl_log_status,
1366 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1367 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1370 static const struct video_device viu_template = {
1374 .ioctl_ops = &viu_ioctl_ops,
1375 .release = video_device_release,
1377 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1378 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1379 V4L2_CAP_VIDEO_OVERLAY | V4L2_CAP_READWRITE,
1382 static int viu_of_probe(struct platform_device *op)
1384 struct viu_dev *viu_dev;
1385 struct video_device *vdev;
1387 struct viu_reg __iomem *viu_regs;
1388 struct i2c_adapter *ad;
1392 ret = of_address_to_resource(op->dev.of_node, 0, &r);
1394 dev_err(&op->dev, "Can't parse device node resource\n");
1398 viu_irq = irq_of_parse_and_map(op->dev.of_node, 0);
1400 dev_err(&op->dev, "Error while mapping the irq\n");
1404 /* request mem region */
1405 if (!devm_request_mem_region(&op->dev, r.start,
1406 sizeof(struct viu_reg), DRV_NAME)) {
1407 dev_err(&op->dev, "Error while requesting mem region\n");
1412 /* remap registers */
1413 viu_regs = devm_ioremap(&op->dev, r.start, sizeof(struct viu_reg));
1415 dev_err(&op->dev, "Can't map register set\n");
1420 /* Prepare our private structure */
1421 viu_dev = devm_kzalloc(&op->dev, sizeof(struct viu_dev), GFP_ATOMIC);
1423 dev_err(&op->dev, "Can't allocate private structure\n");
1428 viu_dev->vr = viu_regs;
1429 viu_dev->irq = viu_irq;
1430 viu_dev->dev = &op->dev;
1432 /* init video dma queues */
1433 INIT_LIST_HEAD(&viu_dev->vidq.active);
1434 INIT_LIST_HEAD(&viu_dev->vidq.queued);
1436 snprintf(viu_dev->v4l2_dev.name,
1437 sizeof(viu_dev->v4l2_dev.name), "%s", "VIU");
1438 ret = v4l2_device_register(viu_dev->dev, &viu_dev->v4l2_dev);
1440 dev_err(&op->dev, "v4l2_device_register() failed: %d\n", ret);
1444 ad = i2c_get_adapter(0);
1447 dev_err(&op->dev, "couldn't get i2c adapter\n");
1451 v4l2_ctrl_handler_init(&viu_dev->hdl, 5);
1452 if (viu_dev->hdl.error) {
1453 ret = viu_dev->hdl.error;
1454 dev_err(&op->dev, "couldn't register control\n");
1457 /* This control handler will inherit the control(s) from the
1459 viu_dev->v4l2_dev.ctrl_handler = &viu_dev->hdl;
1460 viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
1461 "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
1463 timer_setup(&viu_dev->vidq.timeout, viu_vid_timeout, 0);
1464 viu_dev->std = V4L2_STD_NTSC_M;
1467 /* Allocate memory for video device */
1468 vdev = video_device_alloc();
1474 *vdev = viu_template;
1476 vdev->v4l2_dev = &viu_dev->v4l2_dev;
1478 viu_dev->vdev = vdev;
1480 /* initialize locks */
1481 mutex_init(&viu_dev->lock);
1482 viu_dev->vdev->lock = &viu_dev->lock;
1483 spin_lock_init(&viu_dev->slock);
1485 video_set_drvdata(viu_dev->vdev, viu_dev);
1487 mutex_lock(&viu_dev->lock);
1489 ret = video_register_device(viu_dev->vdev, VFL_TYPE_VIDEO, -1);
1491 video_device_release(viu_dev->vdev);
1495 /* enable VIU clock */
1496 clk = devm_clk_get(&op->dev, "ipg");
1498 dev_err(&op->dev, "failed to lookup the clock!\n");
1502 ret = clk_prepare_enable(clk);
1504 dev_err(&op->dev, "failed to enable the clock!\n");
1509 /* reset VIU module */
1510 viu_reset(viu_dev->vr);
1512 /* install interrupt handler */
1513 if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
1514 dev_err(&op->dev, "Request VIU IRQ failed.\n");
1519 mutex_unlock(&viu_dev->lock);
1521 dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
1525 clk_disable_unprepare(viu_dev->clk);
1527 video_unregister_device(viu_dev->vdev);
1529 mutex_unlock(&viu_dev->lock);
1531 v4l2_ctrl_handler_free(&viu_dev->hdl);
1533 i2c_put_adapter(ad);
1535 v4l2_device_unregister(&viu_dev->v4l2_dev);
1537 irq_dispose_mapping(viu_irq);
1541 static int viu_of_remove(struct platform_device *op)
1543 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1544 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1545 struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
1546 struct v4l2_subdev, list);
1547 struct i2c_client *client = v4l2_get_subdevdata(sdev);
1549 free_irq(dev->irq, (void *)dev);
1550 irq_dispose_mapping(dev->irq);
1552 clk_disable_unprepare(dev->clk);
1554 v4l2_ctrl_handler_free(&dev->hdl);
1555 video_unregister_device(dev->vdev);
1556 i2c_put_adapter(client->adapter);
1557 v4l2_device_unregister(&dev->v4l2_dev);
1562 static int viu_suspend(struct platform_device *op, pm_message_t state)
1564 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1565 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1567 clk_disable(dev->clk);
1571 static int viu_resume(struct platform_device *op)
1573 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1574 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1576 clk_enable(dev->clk);
1582 * Initialization and module stuff
1584 static const struct of_device_id mpc512x_viu_of_match[] = {
1586 .compatible = "fsl,mpc5121-viu",
1590 MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
1592 static struct platform_driver viu_of_platform_driver = {
1593 .probe = viu_of_probe,
1594 .remove = viu_of_remove,
1596 .suspend = viu_suspend,
1597 .resume = viu_resume,
1601 .of_match_table = mpc512x_viu_of_match,
1605 module_platform_driver(viu_of_platform_driver);
1607 MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1608 MODULE_AUTHOR("Hongjun Chen");
1609 MODULE_LICENSE("GPL");
1610 MODULE_VERSION(VIU_VERSION);