2 * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
4 * Freescale VIU video driver
6 * Authors: Hongjun Chen <hong-jun.chen@freescale.com>
7 * Porting to 2.6.35 by DENX Software Engineering,
8 * Anatolij Gustschin <agust@denx.de>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
17 #include <linux/module.h>
18 #include <linux/clk.h>
19 #include <linux/kernel.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
24 #include <linux/of_platform.h>
25 #include <linux/slab.h>
26 #include <linux/version.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/videobuf-dma-contig.h>
32 #define DRV_NAME "fsl_viu"
33 #define VIU_MAJOR_VERSION 0
34 #define VIU_MINOR_VERSION 5
36 #define VIU_VERSION KERNEL_VERSION(VIU_MAJOR_VERSION, \
40 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
42 #define VIU_VID_MEM_LIMIT 4 /* Video memory limit, in Mb */
44 /* I2C address of video decoder chip is 0x4A */
45 #define VIU_VIDEO_DECODER_ADDR 0x25
47 /* supported controls */
48 static struct v4l2_queryctrl viu_qctrl
[] = {
50 .id
= V4L2_CID_BRIGHTNESS
,
51 .type
= V4L2_CTRL_TYPE_INTEGER
,
59 .id
= V4L2_CID_CONTRAST
,
60 .type
= V4L2_CTRL_TYPE_INTEGER
,
65 .default_value
= 0x10,
68 .id
= V4L2_CID_SATURATION
,
69 .type
= V4L2_CTRL_TYPE_INTEGER
,
78 .type
= V4L2_CTRL_TYPE_INTEGER
,
88 static int qctl_regs
[ARRAY_SIZE(viu_qctrl
)];
90 static int info_level
;
92 #define dprintk(level, fmt, arg...) \
94 if (level <= info_level) \
95 printk(KERN_DEBUG "viu: " fmt , ## arg); \
103 u32 fourcc
; /* v4l2 format id */
108 static struct viu_fmt formats
[] = {
110 .name
= "RGB-16 (5/B-6/G-5/R)",
111 .fourcc
= V4L2_PIX_FMT_RGB565
,
112 .pixelformat
= V4L2_PIX_FMT_RGB565
,
115 .name
= "RGB-32 (A-R-G-B)",
116 .fourcc
= V4L2_PIX_FMT_RGB32
,
117 .pixelformat
= V4L2_PIX_FMT_RGB32
,
125 /* buffer for one video frame */
127 /* common v4l buffer stuff -- must be first */
128 struct videobuf_buffer vb
;
132 struct viu_dmaqueue
{
134 struct list_head active
;
135 struct list_head queued
;
136 struct timer_list timeout
;
159 } __attribute__ ((packed
));
162 struct v4l2_device v4l2_dev
;
168 /* various device info */
169 struct video_device
*vdev
;
170 struct viu_dmaqueue vidq
;
171 enum v4l2_field capfield
;
176 /* Hardware register area */
179 /* Interrupt vector */
181 struct viu_status irqs
;
184 struct v4l2_framebuffer ovbuf
;
185 struct viu_fmt
*ovfmt
;
186 unsigned int ovenable
;
187 enum v4l2_field ovfield
;
190 struct v4l2_rect crop_current
;
196 struct v4l2_subdev
*decoder
;
203 struct videobuf_queue vb_vidq
;
204 spinlock_t vbq_lock
; /* spinlock for the videobuf queue */
207 struct v4l2_window win
;
208 struct v4l2_clip clips
[1];
212 int width
, height
, sizeimage
;
213 enum v4l2_buf_type type
;
216 static struct viu_reg reg_val
;
219 * Macro definitions of VIU registers
222 /* STATUS_CONFIG register */
226 ERR_MASK
= 0x0f << 4, /* Error code mask */
227 ERR_NO
= 0x00, /* No error */
228 ERR_DMA_V
= 0x01 << 4, /* DMA in vertical active */
229 ERR_DMA_VB
= 0x02 << 4, /* DMA in vertical blanking */
230 ERR_LINE_TOO_LONG
= 0x04 << 4, /* Line too long */
231 ERR_TOO_MANG_LINES
= 0x05 << 4, /* Too many lines in field */
232 ERR_LINE_TOO_SHORT
= 0x06 << 4, /* Line too short */
233 ERR_NOT_ENOUGH_LINE
= 0x07 << 4, /* Not enough lines in field */
234 ERR_FIFO_OVERFLOW
= 0x08 << 4, /* FIFO overflow */
235 ERR_FIFO_UNDERFLOW
= 0x09 << 4, /* FIFO underflow */
236 ERR_1bit_ECC
= 0x0a << 4, /* One bit ECC error */
237 ERR_MORE_ECC
= 0x0b << 4, /* Two/more bits ECC error */
239 INT_FIELD_EN
= 0x01 << 8, /* Enable field interrupt */
240 INT_VSYNC_EN
= 0x01 << 9, /* Enable vsync interrupt */
241 INT_HSYNC_EN
= 0x01 << 10, /* Enable hsync interrupt */
242 INT_VSTART_EN
= 0x01 << 11, /* Enable vstart interrupt */
243 INT_DMA_END_EN
= 0x01 << 12, /* Enable DMA end interrupt */
244 INT_ERROR_EN
= 0x01 << 13, /* Enable error interrupt */
245 INT_ECC_EN
= 0x01 << 14, /* Enable ECC interrupt */
247 INT_FIELD_STATUS
= 0x01 << 16, /* field interrupt status */
248 INT_VSYNC_STATUS
= 0x01 << 17, /* vsync interrupt status */
249 INT_HSYNC_STATUS
= 0x01 << 18, /* hsync interrupt status */
250 INT_VSTART_STATUS
= 0x01 << 19, /* vstart interrupt status */
251 INT_DMA_END_STATUS
= 0x01 << 20, /* DMA end interrupt status */
252 INT_ERROR_STATUS
= 0x01 << 21, /* error interrupt status */
254 DMA_ACT
= 0x01 << 27, /* Enable DMA transfer */
255 FIELD_NO
= 0x01 << 28, /* Field number */
256 DITHER_ON
= 0x01 << 29, /* Dithering is on */
257 ROUND_ON
= 0x01 << 30, /* Round is on */
258 MODE_32BIT
= 0x01 << 31, /* Data in RGBa888,
263 #define norm_maxw() 720
264 #define norm_maxh() 576
266 #define INT_ALL_STATUS (INT_FIELD_STATUS | INT_VSYNC_STATUS | \
267 INT_HSYNC_STATUS | INT_VSTART_STATUS | \
268 INT_DMA_END_STATUS | INT_ERROR_STATUS)
270 #define NUM_FORMATS ARRAY_SIZE(formats)
272 static irqreturn_t
viu_intr(int irq
, void *dev_id
);
274 struct viu_fmt
*format_by_fourcc(int fourcc
)
278 for (i
= 0; i
< NUM_FORMATS
; i
++) {
279 if (formats
[i
].pixelformat
== fourcc
)
283 dprintk(0, "unknown pixelformat:'%4.4s'\n", (char *)&fourcc
);
287 void viu_start_dma(struct viu_dev
*dev
)
289 struct viu_reg
*vr
= dev
->vr
;
293 /* Enable DMA operation */
294 out_be32(&vr
->status_cfg
, SOFT_RST
);
295 out_be32(&vr
->status_cfg
, INT_FIELD_EN
);
298 void viu_stop_dma(struct viu_dev
*dev
)
300 struct viu_reg
*vr
= dev
->vr
;
304 out_be32(&vr
->status_cfg
, 0);
306 /* Clear pending interrupts */
307 status_cfg
= in_be32(&vr
->status_cfg
);
308 if (status_cfg
& 0x3f0000)
309 out_be32(&vr
->status_cfg
, status_cfg
& 0x3f0000);
311 if (status_cfg
& DMA_ACT
) {
313 status_cfg
= in_be32(&vr
->status_cfg
);
314 if (status_cfg
& INT_DMA_END_STATUS
)
319 /* timed out, issue soft reset */
320 out_be32(&vr
->status_cfg
, SOFT_RST
);
321 out_be32(&vr
->status_cfg
, 0);
323 /* clear DMA_END and other pending irqs */
324 out_be32(&vr
->status_cfg
, status_cfg
& 0x3f0000);
331 static int restart_video_queue(struct viu_dmaqueue
*vidq
)
333 struct viu_buf
*buf
, *prev
;
335 dprintk(1, "%s vidq=0x%08lx\n", __func__
, (unsigned long)vidq
);
336 if (!list_empty(&vidq
->active
)) {
337 buf
= list_entry(vidq
->active
.next
, struct viu_buf
, vb
.queue
);
338 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
341 viu_stop_dma(vidq
->dev
);
343 /* cancel all outstanding capture requests */
344 list_for_each_entry_safe(buf
, prev
, &vidq
->active
, vb
.queue
) {
345 list_del(&buf
->vb
.queue
);
346 buf
->vb
.state
= VIDEOBUF_ERROR
;
347 wake_up(&buf
->vb
.done
);
349 mod_timer(&vidq
->timeout
, jiffies
+BUFFER_TIMEOUT
);
355 if (list_empty(&vidq
->queued
))
357 buf
= list_entry(vidq
->queued
.next
, struct viu_buf
, vb
.queue
);
359 list_del(&buf
->vb
.queue
);
360 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
362 dprintk(1, "Restarting video dma\n");
363 viu_stop_dma(vidq
->dev
);
364 viu_start_dma(vidq
->dev
);
366 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
367 mod_timer(&vidq
->timeout
, jiffies
+BUFFER_TIMEOUT
);
368 dprintk(2, "[%p/%d] restart_queue - first active\n",
371 } else if (prev
->vb
.width
== buf
->vb
.width
&&
372 prev
->vb
.height
== buf
->vb
.height
&&
373 prev
->fmt
== buf
->fmt
) {
374 list_del(&buf
->vb
.queue
);
375 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
376 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
377 dprintk(2, "[%p/%d] restart_queue - move to active\n",
386 static void viu_vid_timeout(unsigned long data
)
388 struct viu_dev
*dev
= (struct viu_dev
*)data
;
390 struct viu_dmaqueue
*vidq
= &dev
->vidq
;
392 while (!list_empty(&vidq
->active
)) {
393 buf
= list_entry(vidq
->active
.next
, struct viu_buf
, vb
.queue
);
394 list_del(&buf
->vb
.queue
);
395 buf
->vb
.state
= VIDEOBUF_ERROR
;
396 wake_up(&buf
->vb
.done
);
397 dprintk(1, "viu/0: [%p/%d] timeout\n", buf
, buf
->vb
.i
);
400 restart_video_queue(vidq
);
404 * Videobuf operations
406 static int buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
,
409 struct viu_fh
*fh
= vq
->priv_data
;
411 *size
= fh
->width
* fh
->height
* fh
->fmt
->depth
>> 3;
415 while (*size
* *count
> VIU_VID_MEM_LIMIT
* 1024 * 1024)
418 dprintk(1, "%s, count=%d, size=%d\n", __func__
, *count
, *size
);
422 static void free_buffer(struct videobuf_queue
*vq
, struct viu_buf
*buf
)
424 struct videobuf_buffer
*vb
= &buf
->vb
;
427 BUG_ON(in_interrupt());
429 videobuf_waiton(vq
, &buf
->vb
, 0, 0);
431 if (vq
->int_ops
&& vq
->int_ops
->vaddr
)
432 vaddr
= vq
->int_ops
->vaddr(vb
);
435 videobuf_dma_contig_free(vq
, &buf
->vb
);
437 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
440 inline int buffer_activate(struct viu_dev
*dev
, struct viu_buf
*buf
)
442 struct viu_reg
*vr
= dev
->vr
;
445 /* setup the DMA base address */
446 reg_val
.field_base_addr
= videobuf_to_dma_contig(&buf
->vb
);
448 dprintk(1, "buffer_activate [%p/%d]: dma addr 0x%lx\n",
449 buf
, buf
->vb
.i
, (unsigned long)reg_val
.field_base_addr
);
451 /* interlace is on by default, set horizontal DMA increment */
452 reg_val
.status_cfg
= 0;
453 bpp
= buf
->fmt
->depth
>> 3;
456 reg_val
.status_cfg
&= ~MODE_32BIT
;
457 reg_val
.dma_inc
= buf
->vb
.width
* 2;
460 reg_val
.status_cfg
|= MODE_32BIT
;
461 reg_val
.dma_inc
= buf
->vb
.width
* 4;
464 dprintk(0, "doesn't support color depth(%d)\n",
469 /* setup picture_count register */
470 reg_val
.picture_count
= (buf
->vb
.height
/ 2) << 16 |
473 reg_val
.status_cfg
|= DMA_ACT
| INT_DMA_END_EN
| INT_FIELD_EN
;
475 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
476 dev
->capfield
= buf
->vb
.field
;
478 /* reset dma increment if needed */
479 if (!V4L2_FIELD_HAS_BOTH(buf
->vb
.field
))
482 out_be32(&vr
->dma_inc
, reg_val
.dma_inc
);
483 out_be32(&vr
->picture_count
, reg_val
.picture_count
);
484 out_be32(&vr
->field_base_addr
, reg_val
.field_base_addr
);
485 mod_timer(&dev
->vidq
.timeout
, jiffies
+ BUFFER_TIMEOUT
);
489 static int buffer_prepare(struct videobuf_queue
*vq
,
490 struct videobuf_buffer
*vb
,
491 enum v4l2_field field
)
493 struct viu_fh
*fh
= vq
->priv_data
;
494 struct viu_buf
*buf
= container_of(vb
, struct viu_buf
, vb
);
497 BUG_ON(fh
->fmt
== NULL
);
499 if (fh
->width
< 48 || fh
->width
> norm_maxw() ||
500 fh
->height
< 32 || fh
->height
> norm_maxh())
502 buf
->vb
.size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
503 if (buf
->vb
.baddr
!= 0 && buf
->vb
.bsize
< buf
->vb
.size
)
506 if (buf
->fmt
!= fh
->fmt
||
507 buf
->vb
.width
!= fh
->width
||
508 buf
->vb
.height
!= fh
->height
||
509 buf
->vb
.field
!= field
) {
511 buf
->vb
.width
= fh
->width
;
512 buf
->vb
.height
= fh
->height
;
513 buf
->vb
.field
= field
;
516 if (buf
->vb
.state
== VIDEOBUF_NEEDS_INIT
) {
517 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
521 buf
->vb
.width
= fh
->width
;
522 buf
->vb
.height
= fh
->height
;
523 buf
->vb
.field
= field
;
527 buf
->vb
.state
= VIDEOBUF_PREPARED
;
531 free_buffer(vq
, buf
);
535 static void buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
537 struct viu_buf
*buf
= container_of(vb
, struct viu_buf
, vb
);
538 struct viu_fh
*fh
= vq
->priv_data
;
539 struct viu_dev
*dev
= fh
->dev
;
540 struct viu_dmaqueue
*vidq
= &dev
->vidq
;
541 struct viu_buf
*prev
;
543 if (!list_empty(&vidq
->queued
)) {
544 dprintk(1, "adding vb queue=0x%08lx\n",
545 (unsigned long)&buf
->vb
.queue
);
546 dprintk(1, "vidq pointer 0x%p, queued 0x%p\n",
547 vidq
, &vidq
->queued
);
548 dprintk(1, "dev %p, queued: self %p, next %p, head %p\n",
549 dev
, &vidq
->queued
, vidq
->queued
.next
,
551 list_add_tail(&buf
->vb
.queue
, &vidq
->queued
);
552 buf
->vb
.state
= VIDEOBUF_QUEUED
;
553 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
555 } else if (list_empty(&vidq
->active
)) {
556 dprintk(1, "adding vb active=0x%08lx\n",
557 (unsigned long)&buf
->vb
.queue
);
558 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
559 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
560 mod_timer(&vidq
->timeout
, jiffies
+BUFFER_TIMEOUT
);
561 dprintk(2, "[%p/%d] buffer_queue - first active\n",
564 buffer_activate(dev
, buf
);
566 dprintk(1, "adding vb queue2=0x%08lx\n",
567 (unsigned long)&buf
->vb
.queue
);
568 prev
= list_entry(vidq
->active
.prev
, struct viu_buf
, vb
.queue
);
569 if (prev
->vb
.width
== buf
->vb
.width
&&
570 prev
->vb
.height
== buf
->vb
.height
&&
571 prev
->fmt
== buf
->fmt
) {
572 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
573 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
574 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
577 list_add_tail(&buf
->vb
.queue
, &vidq
->queued
);
578 buf
->vb
.state
= VIDEOBUF_QUEUED
;
579 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
585 static void buffer_release(struct videobuf_queue
*vq
,
586 struct videobuf_buffer
*vb
)
588 struct viu_buf
*buf
= container_of(vb
, struct viu_buf
, vb
);
589 struct viu_fh
*fh
= vq
->priv_data
;
590 struct viu_dev
*dev
= (struct viu_dev
*)fh
->dev
;
593 free_buffer(vq
, buf
);
596 static struct videobuf_queue_ops viu_video_qops
= {
597 .buf_setup
= buffer_setup
,
598 .buf_prepare
= buffer_prepare
,
599 .buf_queue
= buffer_queue
,
600 .buf_release
= buffer_release
,
604 * IOCTL vidioc handling
606 static int vidioc_querycap(struct file
*file
, void *priv
,
607 struct v4l2_capability
*cap
)
609 strcpy(cap
->driver
, "viu");
610 strcpy(cap
->card
, "viu");
611 cap
->version
= VIU_VERSION
;
612 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
614 V4L2_CAP_VIDEO_OVERLAY
|
619 static int vidioc_enum_fmt(struct file
*file
, void *priv
,
620 struct v4l2_fmtdesc
*f
)
622 int index
= f
->index
;
624 if (f
->index
> NUM_FORMATS
)
627 strlcpy(f
->description
, formats
[index
].name
, sizeof(f
->description
));
628 f
->pixelformat
= formats
[index
].fourcc
;
632 static int vidioc_g_fmt_cap(struct file
*file
, void *priv
,
633 struct v4l2_format
*f
)
635 struct viu_fh
*fh
= priv
;
637 f
->fmt
.pix
.width
= fh
->width
;
638 f
->fmt
.pix
.height
= fh
->height
;
639 f
->fmt
.pix
.field
= fh
->vb_vidq
.field
;
640 f
->fmt
.pix
.pixelformat
= fh
->fmt
->pixelformat
;
641 f
->fmt
.pix
.bytesperline
=
642 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
643 f
->fmt
.pix
.sizeimage
= fh
->sizeimage
;
647 static int vidioc_try_fmt_cap(struct file
*file
, void *priv
,
648 struct v4l2_format
*f
)
651 enum v4l2_field field
;
652 unsigned int maxw
, maxh
;
654 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
656 dprintk(1, "Fourcc format (0x%08x) invalid.",
657 f
->fmt
.pix
.pixelformat
);
661 field
= f
->fmt
.pix
.field
;
663 if (field
== V4L2_FIELD_ANY
) {
664 field
= V4L2_FIELD_INTERLACED
;
665 } else if (field
!= V4L2_FIELD_INTERLACED
) {
666 dprintk(1, "Field type invalid.\n");
673 f
->fmt
.pix
.field
= field
;
674 if (f
->fmt
.pix
.height
< 32)
675 f
->fmt
.pix
.height
= 32;
676 if (f
->fmt
.pix
.height
> maxh
)
677 f
->fmt
.pix
.height
= maxh
;
678 if (f
->fmt
.pix
.width
< 48)
679 f
->fmt
.pix
.width
= 48;
680 if (f
->fmt
.pix
.width
> maxw
)
681 f
->fmt
.pix
.width
= maxw
;
682 f
->fmt
.pix
.width
&= ~0x03;
683 f
->fmt
.pix
.bytesperline
=
684 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
689 static int vidioc_s_fmt_cap(struct file
*file
, void *priv
,
690 struct v4l2_format
*f
)
692 struct viu_fh
*fh
= priv
;
695 ret
= vidioc_try_fmt_cap(file
, fh
, f
);
699 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
700 fh
->width
= f
->fmt
.pix
.width
;
701 fh
->height
= f
->fmt
.pix
.height
;
702 fh
->sizeimage
= f
->fmt
.pix
.sizeimage
;
703 fh
->vb_vidq
.field
= f
->fmt
.pix
.field
;
705 dprintk(1, "set to pixelformat '%4.6s'\n", (char *)&fh
->fmt
->name
);
709 static int vidioc_g_fmt_overlay(struct file
*file
, void *priv
,
710 struct v4l2_format
*f
)
712 struct viu_fh
*fh
= priv
;
714 f
->fmt
.win
= fh
->win
;
718 static int verify_preview(struct viu_dev
*dev
, struct v4l2_window
*win
)
720 enum v4l2_field field
;
723 if (dev
->ovbuf
.base
== NULL
)
725 if (dev
->ovfmt
== NULL
)
727 if (win
->w
.width
< 48 || win
->w
.height
< 32)
731 maxw
= dev
->crop_current
.width
;
732 maxh
= dev
->crop_current
.height
;
734 if (field
== V4L2_FIELD_ANY
) {
735 field
= (win
->w
.height
> maxh
/2)
736 ? V4L2_FIELD_INTERLACED
741 case V4L2_FIELD_BOTTOM
:
744 case V4L2_FIELD_INTERLACED
:
751 if (win
->w
.width
> maxw
)
753 if (win
->w
.height
> maxh
)
754 win
->w
.height
= maxh
;
758 inline void viu_activate_overlay(struct viu_reg
*viu_reg
)
760 struct viu_reg
*vr
= viu_reg
;
762 out_be32(&vr
->field_base_addr
, reg_val
.field_base_addr
);
763 out_be32(&vr
->dma_inc
, reg_val
.dma_inc
);
764 out_be32(&vr
->picture_count
, reg_val
.picture_count
);
767 static int viu_start_preview(struct viu_dev
*dev
, struct viu_fh
*fh
)
771 dprintk(1, "%s %dx%d %s\n", __func__
,
772 fh
->win
.w
.width
, fh
->win
.w
.height
, dev
->ovfmt
->name
);
774 reg_val
.status_cfg
= 0;
777 reg_val
.picture_count
= (fh
->win
.w
.height
/ 2) << 16 |
780 /* setup color depth and dma increment */
781 bpp
= dev
->ovfmt
->depth
/ 8;
784 reg_val
.status_cfg
&= ~MODE_32BIT
;
785 reg_val
.dma_inc
= fh
->win
.w
.width
* 2;
788 reg_val
.status_cfg
|= MODE_32BIT
;
789 reg_val
.dma_inc
= fh
->win
.w
.width
* 4;
792 dprintk(0, "device doesn't support color depth(%d)\n",
797 dev
->ovfield
= fh
->win
.field
;
798 if (!V4L2_FIELD_HAS_BOTH(dev
->ovfield
))
801 reg_val
.status_cfg
|= DMA_ACT
| INT_DMA_END_EN
| INT_FIELD_EN
;
803 /* setup the base address of the overlay buffer */
804 reg_val
.field_base_addr
= (u32
)dev
->ovbuf
.base
;
807 viu_activate_overlay(dev
->vr
);
814 static int vidioc_s_fmt_overlay(struct file
*file
, void *priv
,
815 struct v4l2_format
*f
)
817 struct viu_fh
*fh
= priv
;
818 struct viu_dev
*dev
= (struct viu_dev
*)fh
->dev
;
822 err
= verify_preview(dev
, &f
->fmt
.win
);
826 mutex_lock(&dev
->lock
);
827 fh
->win
= f
->fmt
.win
;
829 spin_lock_irqsave(&dev
->slock
, flags
);
830 viu_start_preview(dev
, fh
);
831 spin_unlock_irqrestore(&dev
->slock
, flags
);
832 mutex_unlock(&dev
->lock
);
836 static int vidioc_try_fmt_overlay(struct file
*file
, void *priv
,
837 struct v4l2_format
*f
)
842 int vidioc_g_fbuf(struct file
*file
, void *priv
, struct v4l2_framebuffer
*arg
)
844 struct viu_fh
*fh
= priv
;
845 struct viu_dev
*dev
= fh
->dev
;
846 struct v4l2_framebuffer
*fb
= arg
;
849 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
853 int vidioc_s_fbuf(struct file
*file
, void *priv
, struct v4l2_framebuffer
*arg
)
855 struct viu_fh
*fh
= priv
;
856 struct viu_dev
*dev
= fh
->dev
;
857 struct v4l2_framebuffer
*fb
= arg
;
860 if (!capable(CAP_SYS_ADMIN
) && !capable(CAP_SYS_RAWIO
))
864 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
871 if (dev
->ovbuf
.fmt
.bytesperline
== 0) {
872 dev
->ovbuf
.fmt
.bytesperline
=
873 dev
->ovbuf
.fmt
.width
* fmt
->depth
/ 8;
878 static int vidioc_reqbufs(struct file
*file
, void *priv
,
879 struct v4l2_requestbuffers
*p
)
881 struct viu_fh
*fh
= priv
;
883 return videobuf_reqbufs(&fh
->vb_vidq
, p
);
886 static int vidioc_querybuf(struct file
*file
, void *priv
,
887 struct v4l2_buffer
*p
)
889 struct viu_fh
*fh
= priv
;
891 return videobuf_querybuf(&fh
->vb_vidq
, p
);
894 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
896 struct viu_fh
*fh
= priv
;
898 return videobuf_qbuf(&fh
->vb_vidq
, p
);
901 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
903 struct viu_fh
*fh
= priv
;
905 return videobuf_dqbuf(&fh
->vb_vidq
, p
,
906 file
->f_flags
& O_NONBLOCK
);
909 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
911 struct viu_fh
*fh
= priv
;
913 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
918 return videobuf_streamon(&fh
->vb_vidq
);
921 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
923 struct viu_fh
*fh
= priv
;
925 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
930 return videobuf_streamoff(&fh
->vb_vidq
);
933 #define decoder_call(viu, o, f, args...) \
934 v4l2_subdev_call(viu->decoder, o, f, ##args)
936 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
938 struct viu_fh
*fh
= priv
;
940 decoder_call(fh
->dev
, core
, s_std
, *id
);
944 /* only one input in this driver */
945 static int vidioc_enum_input(struct file
*file
, void *priv
,
946 struct v4l2_input
*inp
)
948 struct viu_fh
*fh
= priv
;
953 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
954 inp
->std
= fh
->dev
->vdev
->tvnorms
;
955 strcpy(inp
->name
, "Camera");
959 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
965 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
967 struct viu_fh
*fh
= priv
;
972 decoder_call(fh
->dev
, video
, s_routing
, i
, 0, 0);
977 static int vidioc_queryctrl(struct file
*file
, void *priv
,
978 struct v4l2_queryctrl
*qc
)
982 for (i
= 0; i
< ARRAY_SIZE(viu_qctrl
); i
++) {
983 if (qc
->id
&& qc
->id
== viu_qctrl
[i
].id
) {
984 memcpy(qc
, &(viu_qctrl
[i
]), sizeof(*qc
));
991 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
992 struct v4l2_control
*ctrl
)
996 for (i
= 0; i
< ARRAY_SIZE(viu_qctrl
); i
++) {
997 if (ctrl
->id
== viu_qctrl
[i
].id
) {
998 ctrl
->value
= qctl_regs
[i
];
1004 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1005 struct v4l2_control
*ctrl
)
1009 for (i
= 0; i
< ARRAY_SIZE(viu_qctrl
); i
++) {
1010 if (ctrl
->id
== viu_qctrl
[i
].id
) {
1011 if (ctrl
->value
< viu_qctrl
[i
].minimum
1012 || ctrl
->value
> viu_qctrl
[i
].maximum
)
1014 qctl_regs
[i
] = ctrl
->value
;
1021 inline void viu_activate_next_buf(struct viu_dev
*dev
,
1022 struct viu_dmaqueue
*viuq
)
1024 struct viu_dmaqueue
*vidq
= viuq
;
1025 struct viu_buf
*buf
;
1027 /* launch another DMA operation for an active/queued buffer */
1028 if (!list_empty(&vidq
->active
)) {
1029 buf
= list_entry(vidq
->active
.next
, struct viu_buf
,
1031 dprintk(1, "start another queued buffer: 0x%p\n", buf
);
1032 buffer_activate(dev
, buf
);
1033 } else if (!list_empty(&vidq
->queued
)) {
1034 buf
= list_entry(vidq
->queued
.next
, struct viu_buf
,
1036 list_del(&buf
->vb
.queue
);
1038 dprintk(1, "start another queued buffer: 0x%p\n", buf
);
1039 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
1040 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
1041 buffer_activate(dev
, buf
);
1045 inline void viu_default_settings(struct viu_reg
*viu_reg
)
1047 struct viu_reg
*vr
= viu_reg
;
1049 out_be32(&vr
->luminance
, 0x9512A254);
1050 out_be32(&vr
->chroma_r
, 0x03310000);
1051 out_be32(&vr
->chroma_g
, 0x06600F38);
1052 out_be32(&vr
->chroma_b
, 0x00000409);
1053 out_be32(&vr
->alpha
, 0x000000ff);
1054 out_be32(&vr
->req_alarm
, 0x00000090);
1055 dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
1056 in_be32(&vr
->status_cfg
), in_be32(&vr
->field_base_addr
));
1059 static void viu_overlay_intr(struct viu_dev
*dev
, u32 status
)
1061 struct viu_reg
*vr
= dev
->vr
;
1063 if (status
& INT_DMA_END_STATUS
)
1066 if (status
& INT_FIELD_STATUS
) {
1067 if (dev
->dma_done
) {
1068 u32 addr
= reg_val
.field_base_addr
;
1071 if (status
& FIELD_NO
)
1072 addr
+= reg_val
.dma_inc
;
1074 out_be32(&vr
->field_base_addr
, addr
);
1075 out_be32(&vr
->dma_inc
, reg_val
.dma_inc
);
1076 out_be32(&vr
->status_cfg
,
1077 (status
& 0xffc0ffff) |
1078 (status
& INT_ALL_STATUS
) |
1079 reg_val
.status_cfg
);
1080 } else if (status
& INT_VSYNC_STATUS
) {
1081 out_be32(&vr
->status_cfg
,
1082 (status
& 0xffc0ffff) |
1083 (status
& INT_ALL_STATUS
) |
1084 reg_val
.status_cfg
);
1089 static void viu_capture_intr(struct viu_dev
*dev
, u32 status
)
1091 struct viu_dmaqueue
*vidq
= &dev
->vidq
;
1092 struct viu_reg
*vr
= dev
->vr
;
1093 struct viu_buf
*buf
;
1098 field_num
= status
& FIELD_NO
;
1099 need_two
= V4L2_FIELD_HAS_BOTH(dev
->capfield
);
1101 if (status
& INT_DMA_END_STATUS
) {
1103 if (((field_num
== 0) && (dev
->field
== 0)) ||
1104 (field_num
&& (dev
->field
== 1)))
1108 if (status
& INT_FIELD_STATUS
) {
1109 dprintk(1, "irq: field %d, done %d\n",
1110 !!field_num
, dma_done
);
1111 if (unlikely(dev
->first
)) {
1112 if (field_num
== 0) {
1114 dprintk(1, "activate first buf\n");
1115 viu_activate_next_buf(dev
, vidq
);
1117 dprintk(1, "wait field 0\n");
1121 /* setup buffer address for next dma operation */
1122 if (!list_empty(&vidq
->active
)) {
1123 u32 addr
= reg_val
.field_base_addr
;
1125 if (field_num
&& need_two
) {
1126 addr
+= reg_val
.dma_inc
;
1127 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1128 (unsigned long)addr
, dev
->field
);
1130 out_be32(&vr
->field_base_addr
, addr
);
1131 out_be32(&vr
->dma_inc
, reg_val
.dma_inc
);
1132 out_be32(&vr
->status_cfg
,
1133 (status
& 0xffc0ffff) |
1134 (status
& INT_ALL_STATUS
) |
1135 reg_val
.status_cfg
);
1140 if (dma_done
&& field_num
&& (dev
->field
== 2)) {
1142 buf
= list_entry(vidq
->active
.next
,
1143 struct viu_buf
, vb
.queue
);
1144 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1146 (unsigned long)videobuf_to_dma_contig(&buf
->vb
),
1147 (unsigned long)in_be32(&vr
->field_base_addr
));
1149 if (waitqueue_active(&buf
->vb
.done
)) {
1150 list_del(&buf
->vb
.queue
);
1151 do_gettimeofday(&buf
->vb
.ts
);
1152 buf
->vb
.state
= VIDEOBUF_DONE
;
1153 buf
->vb
.field_count
++;
1154 wake_up(&buf
->vb
.done
);
1156 /* activate next dma buffer */
1157 viu_activate_next_buf(dev
, vidq
);
1161 static irqreturn_t
viu_intr(int irq
, void *dev_id
)
1163 struct viu_dev
*dev
= (struct viu_dev
*)dev_id
;
1164 struct viu_reg
*vr
= dev
->vr
;
1168 status
= in_be32(&vr
->status_cfg
);
1170 if (status
& INT_ERROR_STATUS
) {
1171 dev
->irqs
.error_irq
++;
1172 error
= status
& ERR_MASK
;
1174 dprintk(1, "Err: error(%d), times:%d!\n",
1175 error
>> 4, dev
->irqs
.error_irq
);
1176 /* Clear interrupt error bit and error flags */
1177 out_be32(&vr
->status_cfg
,
1178 (status
& 0xffc0ffff) | INT_ERROR_STATUS
);
1181 if (status
& INT_DMA_END_STATUS
) {
1182 dev
->irqs
.dma_end_irq
++;
1184 dprintk(2, "VIU DMA end interrupt times: %d\n",
1185 dev
->irqs
.dma_end_irq
);
1188 if (status
& INT_HSYNC_STATUS
)
1189 dev
->irqs
.hsync_irq
++;
1191 if (status
& INT_FIELD_STATUS
) {
1192 dev
->irqs
.field_irq
++;
1193 dprintk(2, "VIU field interrupt times: %d\n",
1194 dev
->irqs
.field_irq
);
1197 if (status
& INT_VSTART_STATUS
)
1198 dev
->irqs
.vstart_irq
++;
1200 if (status
& INT_VSYNC_STATUS
) {
1201 dev
->irqs
.vsync_irq
++;
1202 dprintk(2, "VIU vsync interrupt times: %d\n",
1203 dev
->irqs
.vsync_irq
);
1206 /* clear all pending irqs */
1207 status
= in_be32(&vr
->status_cfg
);
1208 out_be32(&vr
->status_cfg
,
1209 (status
& 0xffc0ffff) | (status
& INT_ALL_STATUS
));
1211 if (dev
->ovenable
) {
1212 viu_overlay_intr(dev
, status
);
1217 viu_capture_intr(dev
, status
);
1222 * File operations for the device
1224 static int viu_open(struct file
*file
)
1226 struct video_device
*vdev
= video_devdata(file
);
1227 struct viu_dev
*dev
= video_get_drvdata(vdev
);
1230 int minor
= vdev
->minor
;
1234 dprintk(1, "viu: open (minor=%d)\n", minor
);
1237 if (dev
->users
> 1) {
1244 dprintk(1, "open minor=%d type=%s users=%d\n", minor
,
1245 v4l2_type_names
[V4L2_BUF_TYPE_VIDEO_CAPTURE
], dev
->users
);
1247 /* allocate and initialize per filehandle data */
1248 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
1254 file
->private_data
= fh
;
1257 fh
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1258 fh
->fmt
= format_by_fourcc(V4L2_PIX_FMT_RGB32
);
1259 fh
->width
= norm_maxw();
1260 fh
->height
= norm_maxh();
1261 dev
->crop_current
.width
= fh
->width
;
1262 dev
->crop_current
.height
= fh
->height
;
1264 /* Put all controls at a sane state */
1265 for (i
= 0; i
< ARRAY_SIZE(viu_qctrl
); i
++)
1266 qctl_regs
[i
] = viu_qctrl
[i
].default_value
;
1268 dprintk(1, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1269 (unsigned long)fh
, (unsigned long)dev
,
1270 (unsigned long)&dev
->vidq
);
1271 dprintk(1, "Open: list_empty queued=%d\n",
1272 list_empty(&dev
->vidq
.queued
));
1273 dprintk(1, "Open: list_empty active=%d\n",
1274 list_empty(&dev
->vidq
.active
));
1276 viu_default_settings(vr
);
1278 status_cfg
= in_be32(&vr
->status_cfg
);
1279 out_be32(&vr
->status_cfg
,
1280 status_cfg
& ~(INT_VSYNC_EN
| INT_HSYNC_EN
|
1281 INT_FIELD_EN
| INT_VSTART_EN
|
1282 INT_DMA_END_EN
| INT_ERROR_EN
| INT_ECC_EN
));
1284 status_cfg
= in_be32(&vr
->status_cfg
);
1285 out_be32(&vr
->status_cfg
, status_cfg
| INT_ALL_STATUS
);
1287 spin_lock_init(&fh
->vbq_lock
);
1288 videobuf_queue_dma_contig_init(&fh
->vb_vidq
, &viu_video_qops
,
1289 dev
->dev
, &fh
->vbq_lock
,
1290 fh
->type
, V4L2_FIELD_INTERLACED
,
1291 sizeof(struct viu_buf
), fh
, NULL
);
1295 static ssize_t
viu_read(struct file
*file
, char __user
*data
, size_t count
,
1298 struct viu_fh
*fh
= file
->private_data
;
1299 struct viu_dev
*dev
= fh
->dev
;
1302 dprintk(2, "%s\n", __func__
);
1306 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1308 ret
= videobuf_read_stream(&fh
->vb_vidq
, data
, count
,
1309 ppos
, 0, file
->f_flags
& O_NONBLOCK
);
1315 static unsigned int viu_poll(struct file
*file
, struct poll_table_struct
*wait
)
1317 struct viu_fh
*fh
= file
->private_data
;
1318 struct videobuf_queue
*q
= &fh
->vb_vidq
;
1320 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1323 return videobuf_poll_stream(file
, q
, wait
);
1326 static int viu_release(struct file
*file
)
1328 struct viu_fh
*fh
= file
->private_data
;
1329 struct viu_dev
*dev
= fh
->dev
;
1330 int minor
= video_devdata(file
)->minor
;
1333 videobuf_stop(&fh
->vb_vidq
);
1338 dprintk(1, "close (minor=%d, users=%d)\n",
1343 void viu_reset(struct viu_reg
*reg
)
1345 out_be32(®
->status_cfg
, 0);
1346 out_be32(®
->luminance
, 0x9512a254);
1347 out_be32(®
->chroma_r
, 0x03310000);
1348 out_be32(®
->chroma_g
, 0x06600f38);
1349 out_be32(®
->chroma_b
, 0x00000409);
1350 out_be32(®
->field_base_addr
, 0);
1351 out_be32(®
->dma_inc
, 0);
1352 out_be32(®
->picture_count
, 0x01e002d0);
1353 out_be32(®
->req_alarm
, 0x00000090);
1354 out_be32(®
->alpha
, 0x000000ff);
1357 static int viu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1359 struct viu_fh
*fh
= file
->private_data
;
1362 dprintk(1, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
1364 ret
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1366 dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1367 (unsigned long)vma
->vm_start
,
1368 (unsigned long)vma
->vm_end
-(unsigned long)vma
->vm_start
,
1374 static struct v4l2_file_operations viu_fops
= {
1375 .owner
= THIS_MODULE
,
1377 .release
= viu_release
,
1380 .ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1384 static const struct v4l2_ioctl_ops viu_ioctl_ops
= {
1385 .vidioc_querycap
= vidioc_querycap
,
1386 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt
,
1387 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_cap
,
1388 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_cap
,
1389 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_cap
,
1390 .vidioc_enum_fmt_vid_overlay
= vidioc_enum_fmt
,
1391 .vidioc_g_fmt_vid_overlay
= vidioc_g_fmt_overlay
,
1392 .vidioc_try_fmt_vid_overlay
= vidioc_try_fmt_overlay
,
1393 .vidioc_s_fmt_vid_overlay
= vidioc_s_fmt_overlay
,
1394 .vidioc_g_fbuf
= vidioc_g_fbuf
,
1395 .vidioc_s_fbuf
= vidioc_s_fbuf
,
1396 .vidioc_reqbufs
= vidioc_reqbufs
,
1397 .vidioc_querybuf
= vidioc_querybuf
,
1398 .vidioc_qbuf
= vidioc_qbuf
,
1399 .vidioc_dqbuf
= vidioc_dqbuf
,
1400 .vidioc_s_std
= vidioc_s_std
,
1401 .vidioc_enum_input
= vidioc_enum_input
,
1402 .vidioc_g_input
= vidioc_g_input
,
1403 .vidioc_s_input
= vidioc_s_input
,
1404 .vidioc_queryctrl
= vidioc_queryctrl
,
1405 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1406 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1407 .vidioc_streamon
= vidioc_streamon
,
1408 .vidioc_streamoff
= vidioc_streamoff
,
1411 static struct video_device viu_template
= {
1415 .ioctl_ops
= &viu_ioctl_ops
,
1416 .release
= video_device_release
,
1418 .tvnorms
= V4L2_STD_NTSC_M
| V4L2_STD_PAL
,
1419 .current_norm
= V4L2_STD_NTSC_M
,
1422 static int __devinit
viu_of_probe(struct platform_device
*op
,
1423 const struct of_device_id
*match
)
1425 struct viu_dev
*viu_dev
;
1426 struct video_device
*vdev
;
1428 struct viu_reg __iomem
*viu_regs
;
1429 struct i2c_adapter
*ad
;
1432 ret
= of_address_to_resource(op
->dev
.of_node
, 0, &r
);
1434 dev_err(&op
->dev
, "Can't parse device node resource\n");
1438 viu_irq
= irq_of_parse_and_map(op
->dev
.of_node
, 0);
1439 if (viu_irq
== NO_IRQ
) {
1440 dev_err(&op
->dev
, "Error while mapping the irq\n");
1444 /* request mem region */
1445 if (!devm_request_mem_region(&op
->dev
, r
.start
,
1446 sizeof(struct viu_reg
), DRV_NAME
)) {
1447 dev_err(&op
->dev
, "Error while requesting mem region\n");
1452 /* remap registers */
1453 viu_regs
= devm_ioremap(&op
->dev
, r
.start
, sizeof(struct viu_reg
));
1455 dev_err(&op
->dev
, "Can't map register set\n");
1460 /* Prepare our private structure */
1461 viu_dev
= devm_kzalloc(&op
->dev
, sizeof(struct viu_dev
), GFP_ATOMIC
);
1463 dev_err(&op
->dev
, "Can't allocate private structure\n");
1468 viu_dev
->vr
= viu_regs
;
1469 viu_dev
->irq
= viu_irq
;
1470 viu_dev
->dev
= &op
->dev
;
1472 /* init video dma queues */
1473 INIT_LIST_HEAD(&viu_dev
->vidq
.active
);
1474 INIT_LIST_HEAD(&viu_dev
->vidq
.queued
);
1476 /* initialize locks */
1477 mutex_init(&viu_dev
->lock
);
1479 snprintf(viu_dev
->v4l2_dev
.name
,
1480 sizeof(viu_dev
->v4l2_dev
.name
), "%s", "VIU");
1481 ret
= v4l2_device_register(viu_dev
->dev
, &viu_dev
->v4l2_dev
);
1483 dev_err(&op
->dev
, "v4l2_device_register() failed: %d\n", ret
);
1487 ad
= i2c_get_adapter(0);
1488 viu_dev
->decoder
= v4l2_i2c_new_subdev(&viu_dev
->v4l2_dev
, ad
,
1489 NULL
, "saa7113", VIU_VIDEO_DECODER_ADDR
, NULL
);
1491 viu_dev
->vidq
.timeout
.function
= viu_vid_timeout
;
1492 viu_dev
->vidq
.timeout
.data
= (unsigned long)viu_dev
;
1493 init_timer(&viu_dev
->vidq
.timeout
);
1496 /* Allocate memory for video device */
1497 vdev
= video_device_alloc();
1503 memcpy(vdev
, &viu_template
, sizeof(viu_template
));
1505 vdev
->v4l2_dev
= &viu_dev
->v4l2_dev
;
1507 viu_dev
->vdev
= vdev
;
1509 video_set_drvdata(viu_dev
->vdev
, viu_dev
);
1511 ret
= video_register_device(viu_dev
->vdev
, VFL_TYPE_GRABBER
, -1);
1513 video_device_release(viu_dev
->vdev
);
1517 /* enable VIU clock */
1518 viu_dev
->clk
= clk_get(&op
->dev
, "viu_clk");
1519 if (IS_ERR(viu_dev
->clk
)) {
1520 dev_err(&op
->dev
, "failed to find the clock module!\n");
1524 clk_enable(viu_dev
->clk
);
1527 /* reset VIU module */
1528 viu_reset(viu_dev
->vr
);
1530 /* install interrupt handler */
1531 if (request_irq(viu_dev
->irq
, viu_intr
, 0, "viu", (void *)viu_dev
)) {
1532 dev_err(&op
->dev
, "Request VIU IRQ failed.\n");
1537 dev_info(&op
->dev
, "Freescale VIU Video Capture Board\n");
1541 clk_disable(viu_dev
->clk
);
1542 clk_put(viu_dev
->clk
);
1544 video_unregister_device(viu_dev
->vdev
);
1546 i2c_put_adapter(ad
);
1547 v4l2_device_unregister(&viu_dev
->v4l2_dev
);
1549 irq_dispose_mapping(viu_irq
);
1553 static int __devexit
viu_of_remove(struct platform_device
*op
)
1555 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(&op
->dev
);
1556 struct viu_dev
*dev
= container_of(v4l2_dev
, struct viu_dev
, v4l2_dev
);
1557 struct v4l2_subdev
*sdev
= list_entry(v4l2_dev
->subdevs
.next
,
1558 struct v4l2_subdev
, list
);
1559 struct i2c_client
*client
= v4l2_get_subdevdata(sdev
);
1561 free_irq(dev
->irq
, (void *)dev
);
1562 irq_dispose_mapping(dev
->irq
);
1564 clk_disable(dev
->clk
);
1567 video_unregister_device(dev
->vdev
);
1568 i2c_put_adapter(client
->adapter
);
1569 v4l2_device_unregister(&dev
->v4l2_dev
);
1574 static int viu_suspend(struct platform_device
*op
, pm_message_t state
)
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_disable(dev
->clk
);
1583 static int viu_resume(struct platform_device
*op
)
1585 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(&op
->dev
);
1586 struct viu_dev
*dev
= container_of(v4l2_dev
, struct viu_dev
, v4l2_dev
);
1588 clk_enable(dev
->clk
);
1594 * Initialization and module stuff
1596 static struct of_device_id mpc512x_viu_of_match
[] = {
1598 .compatible
= "fsl,mpc5121-viu",
1602 MODULE_DEVICE_TABLE(of
, mpc512x_viu_of_match
);
1604 static struct of_platform_driver viu_of_platform_driver
= {
1605 .probe
= viu_of_probe
,
1606 .remove
= __devexit_p(viu_of_remove
),
1608 .suspend
= viu_suspend
,
1609 .resume
= viu_resume
,
1613 .owner
= THIS_MODULE
,
1614 .of_match_table
= mpc512x_viu_of_match
,
1618 static int __init
viu_init(void)
1620 return of_register_platform_driver(&viu_of_platform_driver
);
1623 static void __exit
viu_exit(void)
1625 of_unregister_platform_driver(&viu_of_platform_driver
);
1628 module_init(viu_init
);
1629 module_exit(viu_exit
);
1631 MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1632 MODULE_AUTHOR("Hongjun Chen");
1633 MODULE_LICENSE("GPL");