1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
5 * Freescale VIU video driver
8 * Porting to 2.6.35 by DENX Software Engineering,
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 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
36 #define VIU_VID_MEM_LIMIT 4 /* Video memory limit, in Mb */
38 /* I2C address of video decoder chip is 0x4A */
39 #define VIU_VIDEO_DECODER_ADDR 0x25
41 static int info_level;
43 #define dprintk(level, fmt, arg...) \
45 if (level <= info_level) \
46 printk(KERN_DEBUG "viu: " fmt , ## arg); \
53 u32 fourcc; /* v4l2 format id */
58 static struct viu_fmt formats[] = {
60 .fourcc = V4L2_PIX_FMT_RGB565,
61 .pixelformat = V4L2_PIX_FMT_RGB565,
64 .fourcc = V4L2_PIX_FMT_RGB32,
65 .pixelformat = V4L2_PIX_FMT_RGB32,
73 /* buffer for one video frame */
75 /* common v4l buffer stuff -- must be first */
76 struct videobuf_buffer vb;
82 struct list_head active;
83 struct list_head queued;
84 struct timer_list timeout;
107 } __attribute__ ((packed));
110 struct v4l2_device v4l2_dev;
111 struct v4l2_ctrl_handler hdl;
117 /* various device info */
118 struct video_device *vdev;
119 struct viu_dmaqueue vidq;
120 enum v4l2_field capfield;
125 /* Hardware register area */
126 struct viu_reg __iomem *vr;
128 /* Interrupt vector */
130 struct viu_status irqs;
133 struct v4l2_framebuffer ovbuf;
134 struct viu_fmt *ovfmt;
135 unsigned int ovenable;
136 enum v4l2_field ovfield;
139 struct v4l2_rect crop_current;
145 struct v4l2_subdev *decoder;
151 /* must remain the first field of this struct */
156 struct videobuf_queue vb_vidq;
157 spinlock_t vbq_lock; /* spinlock for the videobuf queue */
160 struct v4l2_window win;
161 struct v4l2_clip clips[1];
165 int width, height, sizeimage;
166 enum v4l2_buf_type type;
169 static struct viu_reg reg_val;
172 * Macro definitions of VIU registers
175 /* STATUS_CONFIG register */
179 ERR_MASK = 0x0f << 4, /* Error code mask */
180 ERR_NO = 0x00, /* No error */
181 ERR_DMA_V = 0x01 << 4, /* DMA in vertical active */
182 ERR_DMA_VB = 0x02 << 4, /* DMA in vertical blanking */
183 ERR_LINE_TOO_LONG = 0x04 << 4, /* Line too long */
184 ERR_TOO_MANG_LINES = 0x05 << 4, /* Too many lines in field */
185 ERR_LINE_TOO_SHORT = 0x06 << 4, /* Line too short */
186 ERR_NOT_ENOUGH_LINE = 0x07 << 4, /* Not enough lines in field */
187 ERR_FIFO_OVERFLOW = 0x08 << 4, /* FIFO overflow */
188 ERR_FIFO_UNDERFLOW = 0x09 << 4, /* FIFO underflow */
189 ERR_1bit_ECC = 0x0a << 4, /* One bit ECC error */
190 ERR_MORE_ECC = 0x0b << 4, /* Two/more bits ECC error */
192 INT_FIELD_EN = 0x01 << 8, /* Enable field interrupt */
193 INT_VSYNC_EN = 0x01 << 9, /* Enable vsync interrupt */
194 INT_HSYNC_EN = 0x01 << 10, /* Enable hsync interrupt */
195 INT_VSTART_EN = 0x01 << 11, /* Enable vstart interrupt */
196 INT_DMA_END_EN = 0x01 << 12, /* Enable DMA end interrupt */
197 INT_ERROR_EN = 0x01 << 13, /* Enable error interrupt */
198 INT_ECC_EN = 0x01 << 14, /* Enable ECC interrupt */
200 INT_FIELD_STATUS = 0x01 << 16, /* field interrupt status */
201 INT_VSYNC_STATUS = 0x01 << 17, /* vsync interrupt status */
202 INT_HSYNC_STATUS = 0x01 << 18, /* hsync interrupt status */
203 INT_VSTART_STATUS = 0x01 << 19, /* vstart interrupt status */
204 INT_DMA_END_STATUS = 0x01 << 20, /* DMA end interrupt status */
205 INT_ERROR_STATUS = 0x01 << 21, /* error interrupt status */
207 DMA_ACT = 0x01 << 27, /* Enable DMA transfer */
208 FIELD_NO = 0x01 << 28, /* Field number */
209 DITHER_ON = 0x01 << 29, /* Dithering is on */
210 ROUND_ON = 0x01 << 30, /* Round is on */
211 MODE_32BIT = 1UL << 31, /* Data in RGBa888,
216 #define norm_maxw() 720
217 #define norm_maxh() 576
219 #define INT_ALL_STATUS (INT_FIELD_STATUS | INT_VSYNC_STATUS | \
220 INT_HSYNC_STATUS | INT_VSTART_STATUS | \
221 INT_DMA_END_STATUS | INT_ERROR_STATUS)
223 #define NUM_FORMATS ARRAY_SIZE(formats)
225 static irqreturn_t viu_intr(int irq, void *dev_id);
227 static struct viu_fmt *format_by_fourcc(int fourcc)
231 for (i = 0; i < NUM_FORMATS; i++) {
232 if (formats[i].pixelformat == fourcc)
236 dprintk(0, "unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
240 static void viu_start_dma(struct viu_dev *dev)
242 struct viu_reg __iomem *vr = dev->vr;
246 /* Enable DMA operation */
247 iowrite32be(SOFT_RST, &vr->status_cfg);
248 iowrite32be(INT_FIELD_EN, &vr->status_cfg);
251 static void viu_stop_dma(struct viu_dev *dev)
253 struct viu_reg __iomem *vr = dev->vr;
257 iowrite32be(0, &vr->status_cfg);
259 /* Clear pending interrupts */
260 status_cfg = ioread32be(&vr->status_cfg);
261 if (status_cfg & 0x3f0000)
262 iowrite32be(status_cfg & 0x3f0000, &vr->status_cfg);
264 if (status_cfg & DMA_ACT) {
266 status_cfg = ioread32be(&vr->status_cfg);
267 if (status_cfg & INT_DMA_END_STATUS)
272 /* timed out, issue soft reset */
273 iowrite32be(SOFT_RST, &vr->status_cfg);
274 iowrite32be(0, &vr->status_cfg);
276 /* clear DMA_END and other pending irqs */
277 iowrite32be(status_cfg & 0x3f0000, &vr->status_cfg);
284 static int restart_video_queue(struct viu_dmaqueue *vidq)
286 struct viu_buf *buf, *prev;
288 dprintk(1, "%s vidq=%p\n", __func__, vidq);
289 if (!list_empty(&vidq->active)) {
290 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
291 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
294 viu_stop_dma(vidq->dev);
296 /* cancel all outstanding capture requests */
297 list_for_each_entry_safe(buf, prev, &vidq->active, vb.queue) {
298 list_del(&buf->vb.queue);
299 buf->vb.state = VIDEOBUF_ERROR;
300 wake_up(&buf->vb.done);
302 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
308 if (list_empty(&vidq->queued))
310 buf = list_entry(vidq->queued.next, struct viu_buf, vb.queue);
312 list_move_tail(&buf->vb.queue, &vidq->active);
314 dprintk(1, "Restarting video dma\n");
315 viu_stop_dma(vidq->dev);
316 viu_start_dma(vidq->dev);
318 buf->vb.state = VIDEOBUF_ACTIVE;
319 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
320 dprintk(2, "[%p/%d] restart_queue - first active\n",
323 } else if (prev->vb.width == buf->vb.width &&
324 prev->vb.height == buf->vb.height &&
325 prev->fmt == buf->fmt) {
326 list_move_tail(&buf->vb.queue, &vidq->active);
327 buf->vb.state = VIDEOBUF_ACTIVE;
328 dprintk(2, "[%p/%d] restart_queue - move to active\n",
337 static void viu_vid_timeout(struct timer_list *t)
339 struct viu_dev *dev = from_timer(dev, t, vidq.timeout);
341 struct viu_dmaqueue *vidq = &dev->vidq;
343 while (!list_empty(&vidq->active)) {
344 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
345 list_del(&buf->vb.queue);
346 buf->vb.state = VIDEOBUF_ERROR;
347 wake_up(&buf->vb.done);
348 dprintk(1, "viu/0: [%p/%d] timeout\n", buf, buf->vb.i);
351 restart_video_queue(vidq);
355 * Videobuf operations
357 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
360 struct viu_fh *fh = vq->priv_data;
362 *size = fh->width * fh->height * fh->fmt->depth >> 3;
366 while (*size * *count > VIU_VID_MEM_LIMIT * 1024 * 1024)
369 dprintk(1, "%s, count=%d, size=%d\n", __func__, *count, *size);
373 static void free_buffer(struct videobuf_queue *vq, struct viu_buf *buf)
375 struct videobuf_buffer *vb = &buf->vb;
378 videobuf_waiton(vq, &buf->vb, 0, 0);
380 if (vq->int_ops && vq->int_ops->vaddr)
381 vaddr = vq->int_ops->vaddr(vb);
384 videobuf_dma_contig_free(vq, &buf->vb);
386 buf->vb.state = VIDEOBUF_NEEDS_INIT;
389 inline int buffer_activate(struct viu_dev *dev, struct viu_buf *buf)
391 struct viu_reg __iomem *vr = dev->vr;
394 /* setup the DMA base address */
395 reg_val.field_base_addr = videobuf_to_dma_contig(&buf->vb);
397 dprintk(1, "buffer_activate [%p/%d]: dma addr 0x%lx\n",
398 buf, buf->vb.i, (unsigned long)reg_val.field_base_addr);
400 /* interlace is on by default, set horizontal DMA increment */
401 reg_val.status_cfg = 0;
402 bpp = buf->fmt->depth >> 3;
405 reg_val.status_cfg &= ~MODE_32BIT;
406 reg_val.dma_inc = buf->vb.width * 2;
409 reg_val.status_cfg |= MODE_32BIT;
410 reg_val.dma_inc = buf->vb.width * 4;
413 dprintk(0, "doesn't support color depth(%d)\n",
418 /* setup picture_count register */
419 reg_val.picture_count = (buf->vb.height / 2) << 16 |
422 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
424 buf->vb.state = VIDEOBUF_ACTIVE;
425 dev->capfield = buf->vb.field;
427 /* reset dma increment if needed */
428 if (!V4L2_FIELD_HAS_BOTH(buf->vb.field))
431 iowrite32be(reg_val.dma_inc, &vr->dma_inc);
432 iowrite32be(reg_val.picture_count, &vr->picture_count);
433 iowrite32be(reg_val.field_base_addr, &vr->field_base_addr);
434 mod_timer(&dev->vidq.timeout, jiffies + BUFFER_TIMEOUT);
438 static int buffer_prepare(struct videobuf_queue *vq,
439 struct videobuf_buffer *vb,
440 enum v4l2_field field)
442 struct viu_fh *fh = vq->priv_data;
443 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
446 BUG_ON(fh->fmt == NULL);
448 if (fh->width < 48 || fh->width > norm_maxw() ||
449 fh->height < 32 || fh->height > norm_maxh())
451 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
452 if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size)
455 if (buf->fmt != fh->fmt ||
456 buf->vb.width != fh->width ||
457 buf->vb.height != fh->height ||
458 buf->vb.field != field) {
460 buf->vb.width = fh->width;
461 buf->vb.height = fh->height;
462 buf->vb.field = field;
465 if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
466 rc = videobuf_iolock(vq, &buf->vb, NULL);
470 buf->vb.width = fh->width;
471 buf->vb.height = fh->height;
472 buf->vb.field = field;
476 buf->vb.state = VIDEOBUF_PREPARED;
480 free_buffer(vq, buf);
484 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
486 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
487 struct viu_fh *fh = vq->priv_data;
488 struct viu_dev *dev = fh->dev;
489 struct viu_dmaqueue *vidq = &dev->vidq;
490 struct viu_buf *prev;
492 if (!list_empty(&vidq->queued)) {
493 dprintk(1, "adding vb queue=%p\n", &buf->vb.queue);
494 dprintk(1, "vidq pointer 0x%p, queued 0x%p\n",
495 vidq, &vidq->queued);
496 dprintk(1, "dev %p, queued: self %p, next %p, head %p\n",
497 dev, &vidq->queued, vidq->queued.next,
499 list_add_tail(&buf->vb.queue, &vidq->queued);
500 buf->vb.state = VIDEOBUF_QUEUED;
501 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
503 } else if (list_empty(&vidq->active)) {
504 dprintk(1, "adding vb active=%p\n", &buf->vb.queue);
505 list_add_tail(&buf->vb.queue, &vidq->active);
506 buf->vb.state = VIDEOBUF_ACTIVE;
507 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
508 dprintk(2, "[%p/%d] buffer_queue - first active\n",
511 buffer_activate(dev, buf);
513 dprintk(1, "adding vb queue2=%p\n", &buf->vb.queue);
514 prev = list_entry(vidq->active.prev, struct viu_buf, vb.queue);
515 if (prev->vb.width == buf->vb.width &&
516 prev->vb.height == buf->vb.height &&
517 prev->fmt == buf->fmt) {
518 list_add_tail(&buf->vb.queue, &vidq->active);
519 buf->vb.state = VIDEOBUF_ACTIVE;
520 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
523 list_add_tail(&buf->vb.queue, &vidq->queued);
524 buf->vb.state = VIDEOBUF_QUEUED;
525 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
531 static void buffer_release(struct videobuf_queue *vq,
532 struct videobuf_buffer *vb)
534 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
535 struct viu_fh *fh = vq->priv_data;
536 struct viu_dev *dev = (struct viu_dev *)fh->dev;
539 free_buffer(vq, buf);
542 static const struct videobuf_queue_ops viu_video_qops = {
543 .buf_setup = buffer_setup,
544 .buf_prepare = buffer_prepare,
545 .buf_queue = buffer_queue,
546 .buf_release = buffer_release,
550 * IOCTL vidioc handling
552 static int vidioc_querycap(struct file *file, void *priv,
553 struct v4l2_capability *cap)
555 strscpy(cap->driver, "viu", sizeof(cap->driver));
556 strscpy(cap->card, "viu", sizeof(cap->card));
557 strscpy(cap->bus_info, "platform:viu", sizeof(cap->bus_info));
561 static int vidioc_enum_fmt(struct file *file, void *priv,
562 struct v4l2_fmtdesc *f)
564 int index = f->index;
566 if (f->index >= NUM_FORMATS)
569 f->pixelformat = formats[index].fourcc;
573 static int vidioc_g_fmt_cap(struct file *file, void *priv,
574 struct v4l2_format *f)
576 struct viu_fh *fh = priv;
578 f->fmt.pix.width = fh->width;
579 f->fmt.pix.height = fh->height;
580 f->fmt.pix.field = fh->vb_vidq.field;
581 f->fmt.pix.pixelformat = fh->fmt->pixelformat;
582 f->fmt.pix.bytesperline =
583 (f->fmt.pix.width * fh->fmt->depth) >> 3;
584 f->fmt.pix.sizeimage = fh->sizeimage;
585 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
589 static int vidioc_try_fmt_cap(struct file *file, void *priv,
590 struct v4l2_format *f)
593 unsigned int maxw, maxh;
595 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
597 dprintk(1, "Fourcc format (0x%08x) invalid.",
598 f->fmt.pix.pixelformat);
605 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
606 if (f->fmt.pix.height < 32)
607 f->fmt.pix.height = 32;
608 if (f->fmt.pix.height > maxh)
609 f->fmt.pix.height = maxh;
610 if (f->fmt.pix.width < 48)
611 f->fmt.pix.width = 48;
612 if (f->fmt.pix.width > maxw)
613 f->fmt.pix.width = maxw;
614 f->fmt.pix.width &= ~0x03;
615 f->fmt.pix.bytesperline =
616 (f->fmt.pix.width * fmt->depth) >> 3;
617 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
618 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
623 static int vidioc_s_fmt_cap(struct file *file, void *priv,
624 struct v4l2_format *f)
626 struct viu_fh *fh = priv;
629 ret = vidioc_try_fmt_cap(file, fh, f);
633 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
634 fh->width = f->fmt.pix.width;
635 fh->height = f->fmt.pix.height;
636 fh->sizeimage = f->fmt.pix.sizeimage;
637 fh->vb_vidq.field = f->fmt.pix.field;
642 static int vidioc_g_fmt_overlay(struct file *file, void *priv,
643 struct v4l2_format *f)
645 struct viu_fh *fh = priv;
647 f->fmt.win = fh->win;
651 static int verify_preview(struct viu_dev *dev, struct v4l2_window *win)
653 enum v4l2_field field;
656 if (dev->ovbuf.base == NULL)
658 if (dev->ovfmt == NULL)
660 if (win->w.width < 48 || win->w.height < 32)
664 maxw = dev->crop_current.width;
665 maxh = dev->crop_current.height;
667 if (field == V4L2_FIELD_ANY) {
668 field = (win->w.height > maxh/2)
669 ? V4L2_FIELD_INTERLACED
674 case V4L2_FIELD_BOTTOM:
677 case V4L2_FIELD_INTERLACED:
684 if (win->w.width > maxw)
686 if (win->w.height > maxh)
687 win->w.height = maxh;
691 inline void viu_activate_overlay(struct viu_reg __iomem *vr)
693 iowrite32be(reg_val.field_base_addr, &vr->field_base_addr);
694 iowrite32be(reg_val.dma_inc, &vr->dma_inc);
695 iowrite32be(reg_val.picture_count, &vr->picture_count);
698 static int viu_setup_preview(struct viu_dev *dev, struct viu_fh *fh)
702 dprintk(1, "%s %dx%d\n", __func__,
703 fh->win.w.width, fh->win.w.height);
705 reg_val.status_cfg = 0;
708 reg_val.picture_count = (fh->win.w.height / 2) << 16 |
711 /* setup color depth and dma increment */
712 bpp = dev->ovfmt->depth / 8;
715 reg_val.status_cfg &= ~MODE_32BIT;
716 reg_val.dma_inc = fh->win.w.width * 2;
719 reg_val.status_cfg |= MODE_32BIT;
720 reg_val.dma_inc = fh->win.w.width * 4;
723 dprintk(0, "device doesn't support color depth(%d)\n",
728 dev->ovfield = fh->win.field;
729 if (!V4L2_FIELD_HAS_BOTH(dev->ovfield))
732 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
734 /* setup the base address of the overlay buffer */
735 reg_val.field_base_addr = (u32)(long)dev->ovbuf.base;
740 static int vidioc_s_fmt_overlay(struct file *file, void *priv,
741 struct v4l2_format *f)
743 struct viu_fh *fh = priv;
744 struct viu_dev *dev = (struct viu_dev *)fh->dev;
748 err = verify_preview(dev, &f->fmt.win);
752 fh->win = f->fmt.win;
754 spin_lock_irqsave(&dev->slock, flags);
755 viu_setup_preview(dev, fh);
756 spin_unlock_irqrestore(&dev->slock, flags);
760 static int vidioc_try_fmt_overlay(struct file *file, void *priv,
761 struct v4l2_format *f)
766 static int vidioc_overlay(struct file *file, void *priv, unsigned int on)
768 struct viu_fh *fh = priv;
769 struct viu_dev *dev = (struct viu_dev *)fh->dev;
773 spin_lock_irqsave(&dev->slock, flags);
774 viu_activate_overlay(dev->vr);
779 spin_unlock_irqrestore(&dev->slock, flags);
788 static int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
790 struct viu_fh *fh = priv;
791 struct viu_dev *dev = fh->dev;
792 struct v4l2_framebuffer *fb = arg;
795 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
799 static int vidioc_s_fbuf(struct file *file, void *priv, const struct v4l2_framebuffer *arg)
801 struct viu_fh *fh = priv;
802 struct viu_dev *dev = fh->dev;
803 const struct v4l2_framebuffer *fb = arg;
806 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
810 fmt = format_by_fourcc(fb->fmt.pixelformat);
817 if (dev->ovbuf.fmt.bytesperline == 0) {
818 dev->ovbuf.fmt.bytesperline =
819 dev->ovbuf.fmt.width * fmt->depth / 8;
824 static int vidioc_reqbufs(struct file *file, void *priv,
825 struct v4l2_requestbuffers *p)
827 struct viu_fh *fh = priv;
829 return videobuf_reqbufs(&fh->vb_vidq, p);
832 static int vidioc_querybuf(struct file *file, void *priv,
833 struct v4l2_buffer *p)
835 struct viu_fh *fh = priv;
837 return videobuf_querybuf(&fh->vb_vidq, p);
840 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
842 struct viu_fh *fh = priv;
844 return videobuf_qbuf(&fh->vb_vidq, p);
847 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
849 struct viu_fh *fh = priv;
851 return videobuf_dqbuf(&fh->vb_vidq, p,
852 file->f_flags & O_NONBLOCK);
855 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
857 struct viu_fh *fh = priv;
858 struct viu_dev *dev = fh->dev;
860 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
868 viu_start_dma(fh->dev);
870 return videobuf_streamon(&fh->vb_vidq);
873 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
875 struct viu_fh *fh = priv;
877 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
882 viu_stop_dma(fh->dev);
884 return videobuf_streamoff(&fh->vb_vidq);
887 #define decoder_call(viu, o, f, args...) \
888 v4l2_subdev_call(viu->decoder, o, f, ##args)
890 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
892 struct viu_fh *fh = priv;
894 decoder_call(fh->dev, video, querystd, std_id);
898 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
900 struct viu_fh *fh = priv;
903 decoder_call(fh->dev, video, s_std, id);
907 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
909 struct viu_fh *fh = priv;
911 *std_id = fh->dev->std;
915 /* only one input in this driver */
916 static int vidioc_enum_input(struct file *file, void *priv,
917 struct v4l2_input *inp)
919 struct viu_fh *fh = priv;
924 inp->type = V4L2_INPUT_TYPE_CAMERA;
925 inp->std = fh->dev->vdev->tvnorms;
926 strscpy(inp->name, "Camera", sizeof(inp->name));
930 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
936 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
938 struct viu_fh *fh = priv;
943 decoder_call(fh->dev, video, s_routing, i, 0, 0);
947 inline void viu_activate_next_buf(struct viu_dev *dev,
948 struct viu_dmaqueue *viuq)
950 struct viu_dmaqueue *vidq = viuq;
953 /* launch another DMA operation for an active/queued buffer */
954 if (!list_empty(&vidq->active)) {
955 buf = list_entry(vidq->active.next, struct viu_buf,
957 dprintk(1, "start another queued buffer: 0x%p\n", buf);
958 buffer_activate(dev, buf);
959 } else if (!list_empty(&vidq->queued)) {
960 buf = list_entry(vidq->queued.next, struct viu_buf,
962 list_del(&buf->vb.queue);
964 dprintk(1, "start another queued buffer: 0x%p\n", buf);
965 list_add_tail(&buf->vb.queue, &vidq->active);
966 buf->vb.state = VIDEOBUF_ACTIVE;
967 buffer_activate(dev, buf);
971 inline void viu_default_settings(struct viu_reg __iomem *vr)
973 iowrite32be(0x9512A254, &vr->luminance);
974 iowrite32be(0x03310000, &vr->chroma_r);
975 iowrite32be(0x06600F38, &vr->chroma_g);
976 iowrite32be(0x00000409, &vr->chroma_b);
977 iowrite32be(0x000000ff, &vr->alpha);
978 iowrite32be(0x00000090, &vr->req_alarm);
979 dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
980 ioread32be(&vr->status_cfg), ioread32be(&vr->field_base_addr));
983 static void viu_overlay_intr(struct viu_dev *dev, u32 status)
985 struct viu_reg __iomem *vr = dev->vr;
987 if (status & INT_DMA_END_STATUS)
990 if (status & INT_FIELD_STATUS) {
992 u32 addr = reg_val.field_base_addr;
995 if (status & FIELD_NO)
996 addr += reg_val.dma_inc;
998 iowrite32be(addr, &vr->field_base_addr);
999 iowrite32be(reg_val.dma_inc, &vr->dma_inc);
1000 iowrite32be((status & 0xffc0ffff) |
1001 (status & INT_ALL_STATUS) |
1002 reg_val.status_cfg, &vr->status_cfg);
1003 } else if (status & INT_VSYNC_STATUS) {
1004 iowrite32be((status & 0xffc0ffff) |
1005 (status & INT_ALL_STATUS) |
1006 reg_val.status_cfg, &vr->status_cfg);
1011 static void viu_capture_intr(struct viu_dev *dev, u32 status)
1013 struct viu_dmaqueue *vidq = &dev->vidq;
1014 struct viu_reg __iomem *vr = dev->vr;
1015 struct viu_buf *buf;
1020 field_num = status & FIELD_NO;
1021 need_two = V4L2_FIELD_HAS_BOTH(dev->capfield);
1023 if (status & INT_DMA_END_STATUS) {
1025 if (((field_num == 0) && (dev->field == 0)) ||
1026 (field_num && (dev->field == 1)))
1030 if (status & INT_FIELD_STATUS) {
1031 dprintk(1, "irq: field %d, done %d\n",
1032 !!field_num, dma_done);
1033 if (unlikely(dev->first)) {
1034 if (field_num == 0) {
1036 dprintk(1, "activate first buf\n");
1037 viu_activate_next_buf(dev, vidq);
1039 dprintk(1, "wait field 0\n");
1043 /* setup buffer address for next dma operation */
1044 if (!list_empty(&vidq->active)) {
1045 u32 addr = reg_val.field_base_addr;
1047 if (field_num && need_two) {
1048 addr += reg_val.dma_inc;
1049 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1050 (unsigned long)addr, dev->field);
1052 iowrite32be(addr, &vr->field_base_addr);
1053 iowrite32be(reg_val.dma_inc, &vr->dma_inc);
1054 iowrite32be((status & 0xffc0ffff) |
1055 (status & INT_ALL_STATUS) |
1056 reg_val.status_cfg, &vr->status_cfg);
1061 if (dma_done && field_num && (dev->field == 2)) {
1063 buf = list_entry(vidq->active.next,
1064 struct viu_buf, vb.queue);
1065 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1067 (unsigned long)videobuf_to_dma_contig(&buf->vb),
1068 (unsigned long)ioread32be(&vr->field_base_addr));
1070 if (waitqueue_active(&buf->vb.done)) {
1071 list_del(&buf->vb.queue);
1072 buf->vb.ts = ktime_get_ns();
1073 buf->vb.state = VIDEOBUF_DONE;
1074 buf->vb.field_count++;
1075 wake_up(&buf->vb.done);
1077 /* activate next dma buffer */
1078 viu_activate_next_buf(dev, vidq);
1082 static irqreturn_t viu_intr(int irq, void *dev_id)
1084 struct viu_dev *dev = (struct viu_dev *)dev_id;
1085 struct viu_reg __iomem *vr = dev->vr;
1089 status = ioread32be(&vr->status_cfg);
1091 if (status & INT_ERROR_STATUS) {
1092 dev->irqs.error_irq++;
1093 error = status & ERR_MASK;
1095 dprintk(1, "Err: error(%d), times:%d!\n",
1096 error >> 4, dev->irqs.error_irq);
1097 /* Clear interrupt error bit and error flags */
1098 iowrite32be((status & 0xffc0ffff) | INT_ERROR_STATUS,
1102 if (status & INT_DMA_END_STATUS) {
1103 dev->irqs.dma_end_irq++;
1105 dprintk(2, "VIU DMA end interrupt times: %d\n",
1106 dev->irqs.dma_end_irq);
1109 if (status & INT_HSYNC_STATUS)
1110 dev->irqs.hsync_irq++;
1112 if (status & INT_FIELD_STATUS) {
1113 dev->irqs.field_irq++;
1114 dprintk(2, "VIU field interrupt times: %d\n",
1115 dev->irqs.field_irq);
1118 if (status & INT_VSTART_STATUS)
1119 dev->irqs.vstart_irq++;
1121 if (status & INT_VSYNC_STATUS) {
1122 dev->irqs.vsync_irq++;
1123 dprintk(2, "VIU vsync interrupt times: %d\n",
1124 dev->irqs.vsync_irq);
1127 /* clear all pending irqs */
1128 status = ioread32be(&vr->status_cfg);
1129 iowrite32be((status & 0xffc0ffff) | (status & INT_ALL_STATUS),
1132 if (dev->ovenable) {
1133 viu_overlay_intr(dev, status);
1138 viu_capture_intr(dev, status);
1143 * File operations for the device
1145 static int viu_open(struct file *file)
1147 struct video_device *vdev = video_devdata(file);
1148 struct viu_dev *dev = video_get_drvdata(vdev);
1150 struct viu_reg __iomem *vr;
1151 int minor = vdev->minor;
1154 dprintk(1, "viu: open (minor=%d)\n", minor);
1157 if (dev->users > 1) {
1164 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1165 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1167 if (mutex_lock_interruptible(&dev->lock)) {
1169 return -ERESTARTSYS;
1172 /* allocate and initialize per filehandle data */
1173 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1176 mutex_unlock(&dev->lock);
1180 v4l2_fh_init(&fh->fh, vdev);
1181 file->private_data = fh;
1184 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1185 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_RGB32);
1186 fh->width = norm_maxw();
1187 fh->height = norm_maxh();
1188 dev->crop_current.width = fh->width;
1189 dev->crop_current.height = fh->height;
1191 dprintk(1, "Open: fh=%p, dev=%p, dev->vidq=%p\n", fh, dev, &dev->vidq);
1192 dprintk(1, "Open: list_empty queued=%d\n",
1193 list_empty(&dev->vidq.queued));
1194 dprintk(1, "Open: list_empty active=%d\n",
1195 list_empty(&dev->vidq.active));
1197 viu_default_settings(vr);
1199 status_cfg = ioread32be(&vr->status_cfg);
1200 iowrite32be(status_cfg & ~(INT_VSYNC_EN | INT_HSYNC_EN |
1201 INT_FIELD_EN | INT_VSTART_EN |
1202 INT_DMA_END_EN | INT_ERROR_EN | INT_ECC_EN),
1205 status_cfg = ioread32be(&vr->status_cfg);
1206 iowrite32be(status_cfg | INT_ALL_STATUS, &vr->status_cfg);
1208 spin_lock_init(&fh->vbq_lock);
1209 videobuf_queue_dma_contig_init(&fh->vb_vidq, &viu_video_qops,
1210 dev->dev, &fh->vbq_lock,
1211 fh->type, V4L2_FIELD_INTERLACED,
1212 sizeof(struct viu_buf), fh,
1214 v4l2_fh_add(&fh->fh);
1215 mutex_unlock(&dev->lock);
1219 static ssize_t viu_read(struct file *file, char __user *data, size_t count,
1222 struct viu_fh *fh = file->private_data;
1223 struct viu_dev *dev = fh->dev;
1226 dprintk(2, "%s\n", __func__);
1230 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1231 if (mutex_lock_interruptible(&dev->lock))
1232 return -ERESTARTSYS;
1234 ret = videobuf_read_stream(&fh->vb_vidq, data, count,
1235 ppos, 0, file->f_flags & O_NONBLOCK);
1236 mutex_unlock(&dev->lock);
1242 static __poll_t viu_poll(struct file *file, struct poll_table_struct *wait)
1244 struct viu_fh *fh = file->private_data;
1245 struct videobuf_queue *q = &fh->vb_vidq;
1246 struct viu_dev *dev = fh->dev;
1247 __poll_t req_events = poll_requested_events(wait);
1248 __poll_t res = v4l2_ctrl_poll(file, wait);
1250 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1253 if (!(req_events & (EPOLLIN | EPOLLRDNORM)))
1256 mutex_lock(&dev->lock);
1257 res |= videobuf_poll_stream(file, q, wait);
1258 mutex_unlock(&dev->lock);
1262 static int viu_release(struct file *file)
1264 struct viu_fh *fh = file->private_data;
1265 struct viu_dev *dev = fh->dev;
1266 int minor = video_devdata(file)->minor;
1268 mutex_lock(&dev->lock);
1270 videobuf_stop(&fh->vb_vidq);
1271 videobuf_mmap_free(&fh->vb_vidq);
1272 v4l2_fh_del(&fh->fh);
1273 v4l2_fh_exit(&fh->fh);
1274 mutex_unlock(&dev->lock);
1279 dprintk(1, "close (minor=%d, users=%d)\n",
1284 static void viu_reset(struct viu_reg __iomem *reg)
1286 iowrite32be(0, ®->status_cfg);
1287 iowrite32be(0x9512a254, ®->luminance);
1288 iowrite32be(0x03310000, ®->chroma_r);
1289 iowrite32be(0x06600f38, ®->chroma_g);
1290 iowrite32be(0x00000409, ®->chroma_b);
1291 iowrite32be(0, ®->field_base_addr);
1292 iowrite32be(0, ®->dma_inc);
1293 iowrite32be(0x01e002d0, ®->picture_count);
1294 iowrite32be(0x00000090, ®->req_alarm);
1295 iowrite32be(0x000000ff, ®->alpha);
1298 static int viu_mmap(struct file *file, struct vm_area_struct *vma)
1300 struct viu_fh *fh = file->private_data;
1301 struct viu_dev *dev = fh->dev;
1304 dprintk(1, "mmap called, vma=%p\n", vma);
1306 if (mutex_lock_interruptible(&dev->lock))
1307 return -ERESTARTSYS;
1308 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1309 mutex_unlock(&dev->lock);
1311 dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1312 (unsigned long)vma->vm_start,
1313 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1319 static const struct v4l2_file_operations viu_fops = {
1320 .owner = THIS_MODULE,
1322 .release = viu_release,
1325 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1329 static const struct v4l2_ioctl_ops viu_ioctl_ops = {
1330 .vidioc_querycap = vidioc_querycap,
1331 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1332 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_cap,
1333 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_cap,
1334 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_cap,
1335 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt,
1336 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_overlay,
1337 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_overlay,
1338 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_overlay,
1339 .vidioc_overlay = vidioc_overlay,
1340 .vidioc_g_fbuf = vidioc_g_fbuf,
1341 .vidioc_s_fbuf = vidioc_s_fbuf,
1342 .vidioc_reqbufs = vidioc_reqbufs,
1343 .vidioc_querybuf = vidioc_querybuf,
1344 .vidioc_qbuf = vidioc_qbuf,
1345 .vidioc_dqbuf = vidioc_dqbuf,
1346 .vidioc_g_std = vidioc_g_std,
1347 .vidioc_s_std = vidioc_s_std,
1348 .vidioc_querystd = vidioc_querystd,
1349 .vidioc_enum_input = vidioc_enum_input,
1350 .vidioc_g_input = vidioc_g_input,
1351 .vidioc_s_input = vidioc_s_input,
1352 .vidioc_streamon = vidioc_streamon,
1353 .vidioc_streamoff = vidioc_streamoff,
1354 .vidioc_log_status = v4l2_ctrl_log_status,
1355 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1356 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1359 static const struct video_device viu_template = {
1363 .ioctl_ops = &viu_ioctl_ops,
1364 .release = video_device_release,
1366 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1367 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1368 V4L2_CAP_VIDEO_OVERLAY | V4L2_CAP_READWRITE,
1371 static int viu_of_probe(struct platform_device *op)
1373 struct viu_dev *viu_dev;
1374 struct video_device *vdev;
1376 struct viu_reg __iomem *viu_regs;
1377 struct i2c_adapter *ad;
1381 ret = of_address_to_resource(op->dev.of_node, 0, &r);
1383 dev_err(&op->dev, "Can't parse device node resource\n");
1387 viu_irq = irq_of_parse_and_map(op->dev.of_node, 0);
1389 dev_err(&op->dev, "Error while mapping the irq\n");
1393 /* request mem region */
1394 if (!devm_request_mem_region(&op->dev, r.start,
1395 sizeof(struct viu_reg), DRV_NAME)) {
1396 dev_err(&op->dev, "Error while requesting mem region\n");
1401 /* remap registers */
1402 viu_regs = devm_ioremap(&op->dev, r.start, sizeof(struct viu_reg));
1404 dev_err(&op->dev, "Can't map register set\n");
1409 /* Prepare our private structure */
1410 viu_dev = devm_kzalloc(&op->dev, sizeof(struct viu_dev), GFP_ATOMIC);
1412 dev_err(&op->dev, "Can't allocate private structure\n");
1417 viu_dev->vr = viu_regs;
1418 viu_dev->irq = viu_irq;
1419 viu_dev->dev = &op->dev;
1421 /* init video dma queues */
1422 INIT_LIST_HEAD(&viu_dev->vidq.active);
1423 INIT_LIST_HEAD(&viu_dev->vidq.queued);
1425 snprintf(viu_dev->v4l2_dev.name,
1426 sizeof(viu_dev->v4l2_dev.name), "%s", "VIU");
1427 ret = v4l2_device_register(viu_dev->dev, &viu_dev->v4l2_dev);
1429 dev_err(&op->dev, "v4l2_device_register() failed: %d\n", ret);
1433 ad = i2c_get_adapter(0);
1436 dev_err(&op->dev, "couldn't get i2c adapter\n");
1440 v4l2_ctrl_handler_init(&viu_dev->hdl, 5);
1441 if (viu_dev->hdl.error) {
1442 ret = viu_dev->hdl.error;
1443 dev_err(&op->dev, "couldn't register control\n");
1446 /* This control handler will inherit the control(s) from the
1448 viu_dev->v4l2_dev.ctrl_handler = &viu_dev->hdl;
1449 viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
1450 "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
1452 timer_setup(&viu_dev->vidq.timeout, viu_vid_timeout, 0);
1453 viu_dev->std = V4L2_STD_NTSC_M;
1456 /* Allocate memory for video device */
1457 vdev = video_device_alloc();
1463 *vdev = viu_template;
1465 vdev->v4l2_dev = &viu_dev->v4l2_dev;
1467 viu_dev->vdev = vdev;
1469 /* initialize locks */
1470 mutex_init(&viu_dev->lock);
1471 viu_dev->vdev->lock = &viu_dev->lock;
1472 spin_lock_init(&viu_dev->slock);
1474 video_set_drvdata(viu_dev->vdev, viu_dev);
1476 mutex_lock(&viu_dev->lock);
1478 ret = video_register_device(viu_dev->vdev, VFL_TYPE_VIDEO, -1);
1480 video_device_release(viu_dev->vdev);
1484 /* enable VIU clock */
1485 clk = devm_clk_get(&op->dev, "ipg");
1487 dev_err(&op->dev, "failed to lookup the clock!\n");
1491 ret = clk_prepare_enable(clk);
1493 dev_err(&op->dev, "failed to enable the clock!\n");
1498 /* reset VIU module */
1499 viu_reset(viu_dev->vr);
1501 /* install interrupt handler */
1502 if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
1503 dev_err(&op->dev, "Request VIU IRQ failed.\n");
1508 mutex_unlock(&viu_dev->lock);
1510 dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
1514 clk_disable_unprepare(viu_dev->clk);
1516 video_unregister_device(viu_dev->vdev);
1518 mutex_unlock(&viu_dev->lock);
1520 v4l2_ctrl_handler_free(&viu_dev->hdl);
1522 i2c_put_adapter(ad);
1524 v4l2_device_unregister(&viu_dev->v4l2_dev);
1526 irq_dispose_mapping(viu_irq);
1530 static int viu_of_remove(struct platform_device *op)
1532 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1533 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1534 struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
1535 struct v4l2_subdev, list);
1536 struct i2c_client *client = v4l2_get_subdevdata(sdev);
1538 free_irq(dev->irq, (void *)dev);
1539 irq_dispose_mapping(dev->irq);
1541 clk_disable_unprepare(dev->clk);
1543 v4l2_ctrl_handler_free(&dev->hdl);
1544 video_unregister_device(dev->vdev);
1545 i2c_put_adapter(client->adapter);
1546 v4l2_device_unregister(&dev->v4l2_dev);
1551 static int viu_suspend(struct platform_device *op, pm_message_t state)
1553 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1554 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1556 clk_disable(dev->clk);
1560 static int viu_resume(struct platform_device *op)
1562 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1563 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1565 clk_enable(dev->clk);
1571 * Initialization and module stuff
1573 static const struct of_device_id mpc512x_viu_of_match[] = {
1575 .compatible = "fsl,mpc5121-viu",
1579 MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
1581 static struct platform_driver viu_of_platform_driver = {
1582 .probe = viu_of_probe,
1583 .remove = viu_of_remove,
1585 .suspend = viu_suspend,
1586 .resume = viu_resume,
1590 .of_match_table = mpc512x_viu_of_match,
1594 module_platform_driver(viu_of_platform_driver);
1596 MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1597 MODULE_AUTHOR("Hongjun Chen");
1598 MODULE_LICENSE("GPL");
1599 MODULE_VERSION(VIU_VERSION);