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 = 0x01 << 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));
566 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
568 V4L2_CAP_VIDEO_OVERLAY |
570 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
574 static int vidioc_enum_fmt(struct file *file, void *priv,
575 struct v4l2_fmtdesc *f)
577 int index = f->index;
579 if (f->index >= NUM_FORMATS)
582 f->pixelformat = formats[index].fourcc;
586 static int vidioc_g_fmt_cap(struct file *file, void *priv,
587 struct v4l2_format *f)
589 struct viu_fh *fh = priv;
591 f->fmt.pix.width = fh->width;
592 f->fmt.pix.height = fh->height;
593 f->fmt.pix.field = fh->vb_vidq.field;
594 f->fmt.pix.pixelformat = fh->fmt->pixelformat;
595 f->fmt.pix.bytesperline =
596 (f->fmt.pix.width * fh->fmt->depth) >> 3;
597 f->fmt.pix.sizeimage = fh->sizeimage;
598 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
602 static int vidioc_try_fmt_cap(struct file *file, void *priv,
603 struct v4l2_format *f)
606 unsigned int maxw, maxh;
608 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
610 dprintk(1, "Fourcc format (0x%08x) invalid.",
611 f->fmt.pix.pixelformat);
618 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
619 if (f->fmt.pix.height < 32)
620 f->fmt.pix.height = 32;
621 if (f->fmt.pix.height > maxh)
622 f->fmt.pix.height = maxh;
623 if (f->fmt.pix.width < 48)
624 f->fmt.pix.width = 48;
625 if (f->fmt.pix.width > maxw)
626 f->fmt.pix.width = maxw;
627 f->fmt.pix.width &= ~0x03;
628 f->fmt.pix.bytesperline =
629 (f->fmt.pix.width * fmt->depth) >> 3;
630 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
631 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
636 static int vidioc_s_fmt_cap(struct file *file, void *priv,
637 struct v4l2_format *f)
639 struct viu_fh *fh = priv;
642 ret = vidioc_try_fmt_cap(file, fh, f);
646 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
647 fh->width = f->fmt.pix.width;
648 fh->height = f->fmt.pix.height;
649 fh->sizeimage = f->fmt.pix.sizeimage;
650 fh->vb_vidq.field = f->fmt.pix.field;
655 static int vidioc_g_fmt_overlay(struct file *file, void *priv,
656 struct v4l2_format *f)
658 struct viu_fh *fh = priv;
660 f->fmt.win = fh->win;
664 static int verify_preview(struct viu_dev *dev, struct v4l2_window *win)
666 enum v4l2_field field;
669 if (dev->ovbuf.base == NULL)
671 if (dev->ovfmt == NULL)
673 if (win->w.width < 48 || win->w.height < 32)
677 maxw = dev->crop_current.width;
678 maxh = dev->crop_current.height;
680 if (field == V4L2_FIELD_ANY) {
681 field = (win->w.height > maxh/2)
682 ? V4L2_FIELD_INTERLACED
687 case V4L2_FIELD_BOTTOM:
690 case V4L2_FIELD_INTERLACED:
697 if (win->w.width > maxw)
699 if (win->w.height > maxh)
700 win->w.height = maxh;
704 inline void viu_activate_overlay(struct viu_reg __iomem *vr)
706 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
707 out_be32(&vr->dma_inc, reg_val.dma_inc);
708 out_be32(&vr->picture_count, reg_val.picture_count);
711 static int viu_setup_preview(struct viu_dev *dev, struct viu_fh *fh)
715 dprintk(1, "%s %dx%d\n", __func__,
716 fh->win.w.width, fh->win.w.height);
718 reg_val.status_cfg = 0;
721 reg_val.picture_count = (fh->win.w.height / 2) << 16 |
724 /* setup color depth and dma increment */
725 bpp = dev->ovfmt->depth / 8;
728 reg_val.status_cfg &= ~MODE_32BIT;
729 reg_val.dma_inc = fh->win.w.width * 2;
732 reg_val.status_cfg |= MODE_32BIT;
733 reg_val.dma_inc = fh->win.w.width * 4;
736 dprintk(0, "device doesn't support color depth(%d)\n",
741 dev->ovfield = fh->win.field;
742 if (!V4L2_FIELD_HAS_BOTH(dev->ovfield))
745 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
747 /* setup the base address of the overlay buffer */
748 reg_val.field_base_addr = (u32)(long)dev->ovbuf.base;
753 static int vidioc_s_fmt_overlay(struct file *file, void *priv,
754 struct v4l2_format *f)
756 struct viu_fh *fh = priv;
757 struct viu_dev *dev = (struct viu_dev *)fh->dev;
761 err = verify_preview(dev, &f->fmt.win);
765 fh->win = f->fmt.win;
767 spin_lock_irqsave(&dev->slock, flags);
768 viu_setup_preview(dev, fh);
769 spin_unlock_irqrestore(&dev->slock, flags);
773 static int vidioc_try_fmt_overlay(struct file *file, void *priv,
774 struct v4l2_format *f)
779 static int vidioc_overlay(struct file *file, void *priv, unsigned int on)
781 struct viu_fh *fh = priv;
782 struct viu_dev *dev = (struct viu_dev *)fh->dev;
786 spin_lock_irqsave(&dev->slock, flags);
787 viu_activate_overlay(dev->vr);
792 spin_unlock_irqrestore(&dev->slock, flags);
801 static int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
803 struct viu_fh *fh = priv;
804 struct viu_dev *dev = fh->dev;
805 struct v4l2_framebuffer *fb = arg;
808 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
812 static int vidioc_s_fbuf(struct file *file, void *priv, const struct v4l2_framebuffer *arg)
814 struct viu_fh *fh = priv;
815 struct viu_dev *dev = fh->dev;
816 const struct v4l2_framebuffer *fb = arg;
819 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
823 fmt = format_by_fourcc(fb->fmt.pixelformat);
830 if (dev->ovbuf.fmt.bytesperline == 0) {
831 dev->ovbuf.fmt.bytesperline =
832 dev->ovbuf.fmt.width * fmt->depth / 8;
837 static int vidioc_reqbufs(struct file *file, void *priv,
838 struct v4l2_requestbuffers *p)
840 struct viu_fh *fh = priv;
842 return videobuf_reqbufs(&fh->vb_vidq, p);
845 static int vidioc_querybuf(struct file *file, void *priv,
846 struct v4l2_buffer *p)
848 struct viu_fh *fh = priv;
850 return videobuf_querybuf(&fh->vb_vidq, p);
853 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
855 struct viu_fh *fh = priv;
857 return videobuf_qbuf(&fh->vb_vidq, p);
860 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
862 struct viu_fh *fh = priv;
864 return videobuf_dqbuf(&fh->vb_vidq, p,
865 file->f_flags & O_NONBLOCK);
868 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
870 struct viu_fh *fh = priv;
871 struct viu_dev *dev = fh->dev;
873 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
881 viu_start_dma(fh->dev);
883 return videobuf_streamon(&fh->vb_vidq);
886 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
888 struct viu_fh *fh = priv;
890 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
895 viu_stop_dma(fh->dev);
897 return videobuf_streamoff(&fh->vb_vidq);
900 #define decoder_call(viu, o, f, args...) \
901 v4l2_subdev_call(viu->decoder, o, f, ##args)
903 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
905 struct viu_fh *fh = priv;
907 decoder_call(fh->dev, video, querystd, std_id);
911 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
913 struct viu_fh *fh = priv;
916 decoder_call(fh->dev, video, s_std, id);
920 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
922 struct viu_fh *fh = priv;
924 *std_id = fh->dev->std;
928 /* only one input in this driver */
929 static int vidioc_enum_input(struct file *file, void *priv,
930 struct v4l2_input *inp)
932 struct viu_fh *fh = priv;
937 inp->type = V4L2_INPUT_TYPE_CAMERA;
938 inp->std = fh->dev->vdev->tvnorms;
939 strscpy(inp->name, "Camera", sizeof(inp->name));
943 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
949 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
951 struct viu_fh *fh = priv;
956 decoder_call(fh->dev, video, s_routing, i, 0, 0);
960 inline void viu_activate_next_buf(struct viu_dev *dev,
961 struct viu_dmaqueue *viuq)
963 struct viu_dmaqueue *vidq = viuq;
966 /* launch another DMA operation for an active/queued buffer */
967 if (!list_empty(&vidq->active)) {
968 buf = list_entry(vidq->active.next, struct viu_buf,
970 dprintk(1, "start another queued buffer: 0x%p\n", buf);
971 buffer_activate(dev, buf);
972 } else if (!list_empty(&vidq->queued)) {
973 buf = list_entry(vidq->queued.next, struct viu_buf,
975 list_del(&buf->vb.queue);
977 dprintk(1, "start another queued buffer: 0x%p\n", buf);
978 list_add_tail(&buf->vb.queue, &vidq->active);
979 buf->vb.state = VIDEOBUF_ACTIVE;
980 buffer_activate(dev, buf);
984 inline void viu_default_settings(struct viu_reg __iomem *vr)
986 out_be32(&vr->luminance, 0x9512A254);
987 out_be32(&vr->chroma_r, 0x03310000);
988 out_be32(&vr->chroma_g, 0x06600F38);
989 out_be32(&vr->chroma_b, 0x00000409);
990 out_be32(&vr->alpha, 0x000000ff);
991 out_be32(&vr->req_alarm, 0x00000090);
992 dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
993 in_be32(&vr->status_cfg), in_be32(&vr->field_base_addr));
996 static void viu_overlay_intr(struct viu_dev *dev, u32 status)
998 struct viu_reg __iomem *vr = dev->vr;
1000 if (status & INT_DMA_END_STATUS)
1003 if (status & INT_FIELD_STATUS) {
1004 if (dev->dma_done) {
1005 u32 addr = reg_val.field_base_addr;
1008 if (status & FIELD_NO)
1009 addr += reg_val.dma_inc;
1011 out_be32(&vr->field_base_addr, addr);
1012 out_be32(&vr->dma_inc, reg_val.dma_inc);
1013 out_be32(&vr->status_cfg,
1014 (status & 0xffc0ffff) |
1015 (status & INT_ALL_STATUS) |
1016 reg_val.status_cfg);
1017 } else if (status & INT_VSYNC_STATUS) {
1018 out_be32(&vr->status_cfg,
1019 (status & 0xffc0ffff) |
1020 (status & INT_ALL_STATUS) |
1021 reg_val.status_cfg);
1026 static void viu_capture_intr(struct viu_dev *dev, u32 status)
1028 struct viu_dmaqueue *vidq = &dev->vidq;
1029 struct viu_reg __iomem *vr = dev->vr;
1030 struct viu_buf *buf;
1035 field_num = status & FIELD_NO;
1036 need_two = V4L2_FIELD_HAS_BOTH(dev->capfield);
1038 if (status & INT_DMA_END_STATUS) {
1040 if (((field_num == 0) && (dev->field == 0)) ||
1041 (field_num && (dev->field == 1)))
1045 if (status & INT_FIELD_STATUS) {
1046 dprintk(1, "irq: field %d, done %d\n",
1047 !!field_num, dma_done);
1048 if (unlikely(dev->first)) {
1049 if (field_num == 0) {
1051 dprintk(1, "activate first buf\n");
1052 viu_activate_next_buf(dev, vidq);
1054 dprintk(1, "wait field 0\n");
1058 /* setup buffer address for next dma operation */
1059 if (!list_empty(&vidq->active)) {
1060 u32 addr = reg_val.field_base_addr;
1062 if (field_num && need_two) {
1063 addr += reg_val.dma_inc;
1064 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1065 (unsigned long)addr, dev->field);
1067 out_be32(&vr->field_base_addr, addr);
1068 out_be32(&vr->dma_inc, reg_val.dma_inc);
1069 out_be32(&vr->status_cfg,
1070 (status & 0xffc0ffff) |
1071 (status & INT_ALL_STATUS) |
1072 reg_val.status_cfg);
1077 if (dma_done && field_num && (dev->field == 2)) {
1079 buf = list_entry(vidq->active.next,
1080 struct viu_buf, vb.queue);
1081 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1083 (unsigned long)videobuf_to_dma_contig(&buf->vb),
1084 (unsigned long)in_be32(&vr->field_base_addr));
1086 if (waitqueue_active(&buf->vb.done)) {
1087 list_del(&buf->vb.queue);
1088 buf->vb.ts = ktime_get_ns();
1089 buf->vb.state = VIDEOBUF_DONE;
1090 buf->vb.field_count++;
1091 wake_up(&buf->vb.done);
1093 /* activate next dma buffer */
1094 viu_activate_next_buf(dev, vidq);
1098 static irqreturn_t viu_intr(int irq, void *dev_id)
1100 struct viu_dev *dev = (struct viu_dev *)dev_id;
1101 struct viu_reg __iomem *vr = dev->vr;
1105 status = in_be32(&vr->status_cfg);
1107 if (status & INT_ERROR_STATUS) {
1108 dev->irqs.error_irq++;
1109 error = status & ERR_MASK;
1111 dprintk(1, "Err: error(%d), times:%d!\n",
1112 error >> 4, dev->irqs.error_irq);
1113 /* Clear interrupt error bit and error flags */
1114 out_be32(&vr->status_cfg,
1115 (status & 0xffc0ffff) | INT_ERROR_STATUS);
1118 if (status & INT_DMA_END_STATUS) {
1119 dev->irqs.dma_end_irq++;
1121 dprintk(2, "VIU DMA end interrupt times: %d\n",
1122 dev->irqs.dma_end_irq);
1125 if (status & INT_HSYNC_STATUS)
1126 dev->irqs.hsync_irq++;
1128 if (status & INT_FIELD_STATUS) {
1129 dev->irqs.field_irq++;
1130 dprintk(2, "VIU field interrupt times: %d\n",
1131 dev->irqs.field_irq);
1134 if (status & INT_VSTART_STATUS)
1135 dev->irqs.vstart_irq++;
1137 if (status & INT_VSYNC_STATUS) {
1138 dev->irqs.vsync_irq++;
1139 dprintk(2, "VIU vsync interrupt times: %d\n",
1140 dev->irqs.vsync_irq);
1143 /* clear all pending irqs */
1144 status = in_be32(&vr->status_cfg);
1145 out_be32(&vr->status_cfg,
1146 (status & 0xffc0ffff) | (status & INT_ALL_STATUS));
1148 if (dev->ovenable) {
1149 viu_overlay_intr(dev, status);
1154 viu_capture_intr(dev, status);
1159 * File operations for the device
1161 static int viu_open(struct file *file)
1163 struct video_device *vdev = video_devdata(file);
1164 struct viu_dev *dev = video_get_drvdata(vdev);
1166 struct viu_reg __iomem *vr;
1167 int minor = vdev->minor;
1170 dprintk(1, "viu: open (minor=%d)\n", minor);
1173 if (dev->users > 1) {
1180 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1181 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1183 if (mutex_lock_interruptible(&dev->lock)) {
1185 return -ERESTARTSYS;
1188 /* allocate and initialize per filehandle data */
1189 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1192 mutex_unlock(&dev->lock);
1196 v4l2_fh_init(&fh->fh, vdev);
1197 file->private_data = fh;
1200 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1201 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_RGB32);
1202 fh->width = norm_maxw();
1203 fh->height = norm_maxh();
1204 dev->crop_current.width = fh->width;
1205 dev->crop_current.height = fh->height;
1207 dprintk(1, "Open: fh=%p, dev=%p, dev->vidq=%p\n", fh, dev, &dev->vidq);
1208 dprintk(1, "Open: list_empty queued=%d\n",
1209 list_empty(&dev->vidq.queued));
1210 dprintk(1, "Open: list_empty active=%d\n",
1211 list_empty(&dev->vidq.active));
1213 viu_default_settings(vr);
1215 status_cfg = in_be32(&vr->status_cfg);
1216 out_be32(&vr->status_cfg,
1217 status_cfg & ~(INT_VSYNC_EN | INT_HSYNC_EN |
1218 INT_FIELD_EN | INT_VSTART_EN |
1219 INT_DMA_END_EN | INT_ERROR_EN | INT_ECC_EN));
1221 status_cfg = in_be32(&vr->status_cfg);
1222 out_be32(&vr->status_cfg, status_cfg | INT_ALL_STATUS);
1224 spin_lock_init(&fh->vbq_lock);
1225 videobuf_queue_dma_contig_init(&fh->vb_vidq, &viu_video_qops,
1226 dev->dev, &fh->vbq_lock,
1227 fh->type, V4L2_FIELD_INTERLACED,
1228 sizeof(struct viu_buf), fh,
1230 v4l2_fh_add(&fh->fh);
1231 mutex_unlock(&dev->lock);
1235 static ssize_t viu_read(struct file *file, char __user *data, size_t count,
1238 struct viu_fh *fh = file->private_data;
1239 struct viu_dev *dev = fh->dev;
1242 dprintk(2, "%s\n", __func__);
1246 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1247 if (mutex_lock_interruptible(&dev->lock))
1248 return -ERESTARTSYS;
1250 ret = videobuf_read_stream(&fh->vb_vidq, data, count,
1251 ppos, 0, file->f_flags & O_NONBLOCK);
1252 mutex_unlock(&dev->lock);
1258 static __poll_t viu_poll(struct file *file, struct poll_table_struct *wait)
1260 struct viu_fh *fh = file->private_data;
1261 struct videobuf_queue *q = &fh->vb_vidq;
1262 struct viu_dev *dev = fh->dev;
1263 __poll_t req_events = poll_requested_events(wait);
1264 __poll_t res = v4l2_ctrl_poll(file, wait);
1266 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1269 if (!(req_events & (EPOLLIN | EPOLLRDNORM)))
1272 mutex_lock(&dev->lock);
1273 res |= videobuf_poll_stream(file, q, wait);
1274 mutex_unlock(&dev->lock);
1278 static int viu_release(struct file *file)
1280 struct viu_fh *fh = file->private_data;
1281 struct viu_dev *dev = fh->dev;
1282 int minor = video_devdata(file)->minor;
1284 mutex_lock(&dev->lock);
1286 videobuf_stop(&fh->vb_vidq);
1287 videobuf_mmap_free(&fh->vb_vidq);
1288 v4l2_fh_del(&fh->fh);
1289 v4l2_fh_exit(&fh->fh);
1290 mutex_unlock(&dev->lock);
1295 dprintk(1, "close (minor=%d, users=%d)\n",
1300 static void viu_reset(struct viu_reg __iomem *reg)
1302 out_be32(®->status_cfg, 0);
1303 out_be32(®->luminance, 0x9512a254);
1304 out_be32(®->chroma_r, 0x03310000);
1305 out_be32(®->chroma_g, 0x06600f38);
1306 out_be32(®->chroma_b, 0x00000409);
1307 out_be32(®->field_base_addr, 0);
1308 out_be32(®->dma_inc, 0);
1309 out_be32(®->picture_count, 0x01e002d0);
1310 out_be32(®->req_alarm, 0x00000090);
1311 out_be32(®->alpha, 0x000000ff);
1314 static int viu_mmap(struct file *file, struct vm_area_struct *vma)
1316 struct viu_fh *fh = file->private_data;
1317 struct viu_dev *dev = fh->dev;
1320 dprintk(1, "mmap called, vma=%p\n", vma);
1322 if (mutex_lock_interruptible(&dev->lock))
1323 return -ERESTARTSYS;
1324 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1325 mutex_unlock(&dev->lock);
1327 dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1328 (unsigned long)vma->vm_start,
1329 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1335 static const struct v4l2_file_operations viu_fops = {
1336 .owner = THIS_MODULE,
1338 .release = viu_release,
1341 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1345 static const struct v4l2_ioctl_ops viu_ioctl_ops = {
1346 .vidioc_querycap = vidioc_querycap,
1347 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1348 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_cap,
1349 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_cap,
1350 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_cap,
1351 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt,
1352 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_overlay,
1353 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_overlay,
1354 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_overlay,
1355 .vidioc_overlay = vidioc_overlay,
1356 .vidioc_g_fbuf = vidioc_g_fbuf,
1357 .vidioc_s_fbuf = vidioc_s_fbuf,
1358 .vidioc_reqbufs = vidioc_reqbufs,
1359 .vidioc_querybuf = vidioc_querybuf,
1360 .vidioc_qbuf = vidioc_qbuf,
1361 .vidioc_dqbuf = vidioc_dqbuf,
1362 .vidioc_g_std = vidioc_g_std,
1363 .vidioc_s_std = vidioc_s_std,
1364 .vidioc_querystd = vidioc_querystd,
1365 .vidioc_enum_input = vidioc_enum_input,
1366 .vidioc_g_input = vidioc_g_input,
1367 .vidioc_s_input = vidioc_s_input,
1368 .vidioc_streamon = vidioc_streamon,
1369 .vidioc_streamoff = vidioc_streamoff,
1370 .vidioc_log_status = v4l2_ctrl_log_status,
1371 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1372 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1375 static const struct video_device viu_template = {
1379 .ioctl_ops = &viu_ioctl_ops,
1380 .release = video_device_release,
1382 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1385 static int viu_of_probe(struct platform_device *op)
1387 struct viu_dev *viu_dev;
1388 struct video_device *vdev;
1390 struct viu_reg __iomem *viu_regs;
1391 struct i2c_adapter *ad;
1395 ret = of_address_to_resource(op->dev.of_node, 0, &r);
1397 dev_err(&op->dev, "Can't parse device node resource\n");
1401 viu_irq = irq_of_parse_and_map(op->dev.of_node, 0);
1403 dev_err(&op->dev, "Error while mapping the irq\n");
1407 /* request mem region */
1408 if (!devm_request_mem_region(&op->dev, r.start,
1409 sizeof(struct viu_reg), DRV_NAME)) {
1410 dev_err(&op->dev, "Error while requesting mem region\n");
1415 /* remap registers */
1416 viu_regs = devm_ioremap(&op->dev, r.start, sizeof(struct viu_reg));
1418 dev_err(&op->dev, "Can't map register set\n");
1423 /* Prepare our private structure */
1424 viu_dev = devm_kzalloc(&op->dev, sizeof(struct viu_dev), GFP_ATOMIC);
1426 dev_err(&op->dev, "Can't allocate private structure\n");
1431 viu_dev->vr = viu_regs;
1432 viu_dev->irq = viu_irq;
1433 viu_dev->dev = &op->dev;
1435 /* init video dma queues */
1436 INIT_LIST_HEAD(&viu_dev->vidq.active);
1437 INIT_LIST_HEAD(&viu_dev->vidq.queued);
1439 snprintf(viu_dev->v4l2_dev.name,
1440 sizeof(viu_dev->v4l2_dev.name), "%s", "VIU");
1441 ret = v4l2_device_register(viu_dev->dev, &viu_dev->v4l2_dev);
1443 dev_err(&op->dev, "v4l2_device_register() failed: %d\n", ret);
1447 ad = i2c_get_adapter(0);
1450 dev_err(&op->dev, "couldn't get i2c adapter\n");
1454 v4l2_ctrl_handler_init(&viu_dev->hdl, 5);
1455 if (viu_dev->hdl.error) {
1456 ret = viu_dev->hdl.error;
1457 dev_err(&op->dev, "couldn't register control\n");
1460 /* This control handler will inherit the control(s) from the
1462 viu_dev->v4l2_dev.ctrl_handler = &viu_dev->hdl;
1463 viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
1464 "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
1466 timer_setup(&viu_dev->vidq.timeout, viu_vid_timeout, 0);
1467 viu_dev->std = V4L2_STD_NTSC_M;
1470 /* Allocate memory for video device */
1471 vdev = video_device_alloc();
1477 *vdev = viu_template;
1479 vdev->v4l2_dev = &viu_dev->v4l2_dev;
1481 viu_dev->vdev = vdev;
1483 /* initialize locks */
1484 mutex_init(&viu_dev->lock);
1485 viu_dev->vdev->lock = &viu_dev->lock;
1486 spin_lock_init(&viu_dev->slock);
1488 video_set_drvdata(viu_dev->vdev, viu_dev);
1490 mutex_lock(&viu_dev->lock);
1492 ret = video_register_device(viu_dev->vdev, VFL_TYPE_GRABBER, -1);
1494 video_device_release(viu_dev->vdev);
1498 /* enable VIU clock */
1499 clk = devm_clk_get(&op->dev, "ipg");
1501 dev_err(&op->dev, "failed to lookup the clock!\n");
1505 ret = clk_prepare_enable(clk);
1507 dev_err(&op->dev, "failed to enable the clock!\n");
1512 /* reset VIU module */
1513 viu_reset(viu_dev->vr);
1515 /* install interrupt handler */
1516 if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
1517 dev_err(&op->dev, "Request VIU IRQ failed.\n");
1522 mutex_unlock(&viu_dev->lock);
1524 dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
1528 clk_disable_unprepare(viu_dev->clk);
1530 video_unregister_device(viu_dev->vdev);
1532 mutex_unlock(&viu_dev->lock);
1534 v4l2_ctrl_handler_free(&viu_dev->hdl);
1536 i2c_put_adapter(ad);
1538 v4l2_device_unregister(&viu_dev->v4l2_dev);
1540 irq_dispose_mapping(viu_irq);
1544 static int viu_of_remove(struct platform_device *op)
1546 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1547 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1548 struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
1549 struct v4l2_subdev, list);
1550 struct i2c_client *client = v4l2_get_subdevdata(sdev);
1552 free_irq(dev->irq, (void *)dev);
1553 irq_dispose_mapping(dev->irq);
1555 clk_disable_unprepare(dev->clk);
1557 v4l2_ctrl_handler_free(&dev->hdl);
1558 video_unregister_device(dev->vdev);
1559 i2c_put_adapter(client->adapter);
1560 v4l2_device_unregister(&dev->v4l2_dev);
1565 static int viu_suspend(struct platform_device *op, pm_message_t state)
1567 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1568 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1570 clk_disable(dev->clk);
1574 static int viu_resume(struct platform_device *op)
1576 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1577 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1579 clk_enable(dev->clk);
1585 * Initialization and module stuff
1587 static const struct of_device_id mpc512x_viu_of_match[] = {
1589 .compatible = "fsl,mpc5121-viu",
1593 MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
1595 static struct platform_driver viu_of_platform_driver = {
1596 .probe = viu_of_probe,
1597 .remove = viu_of_remove,
1599 .suspend = viu_suspend,
1600 .resume = viu_resume,
1604 .of_match_table = mpc512x_viu_of_match,
1608 module_platform_driver(viu_of_platform_driver);
1610 MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1611 MODULE_AUTHOR("Hongjun Chen");
1612 MODULE_LICENSE("GPL");
1613 MODULE_VERSION(VIU_VERSION);