[media] media: fsl-viu: fix support for streaming with mmap method
[linux-2.6-block.git] / drivers / media / video / fsl-viu.c
1 /*
2  * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
3  *
4  *  Freescale VIU video driver
5  *
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>
9  *
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.
14  *
15  */
16
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>
23 #include <linux/io.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>
31
32 #define DRV_NAME                "fsl_viu"
33 #define VIU_MAJOR_VERSION       0
34 #define VIU_MINOR_VERSION       5
35 #define VIU_RELEASE             0
36 #define VIU_VERSION             KERNEL_VERSION(VIU_MAJOR_VERSION, \
37                                                VIU_MINOR_VERSION, \
38                                                VIU_RELEASE)
39
40 #define BUFFER_TIMEOUT          msecs_to_jiffies(500)  /* 0.5 seconds */
41
42 #define VIU_VID_MEM_LIMIT       4       /* Video memory limit, in Mb */
43
44 /* I2C address of video decoder chip is 0x4A */
45 #define VIU_VIDEO_DECODER_ADDR  0x25
46
47 /* supported controls */
48 static struct v4l2_queryctrl viu_qctrl[] = {
49         {
50                 .id            = V4L2_CID_BRIGHTNESS,
51                 .type          = V4L2_CTRL_TYPE_INTEGER,
52                 .name          = "Brightness",
53                 .minimum       = 0,
54                 .maximum       = 255,
55                 .step          = 1,
56                 .default_value = 127,
57                 .flags         = 0,
58         }, {
59                 .id            = V4L2_CID_CONTRAST,
60                 .type          = V4L2_CTRL_TYPE_INTEGER,
61                 .name          = "Contrast",
62                 .minimum       = 0,
63                 .maximum       = 255,
64                 .step          = 0x1,
65                 .default_value = 0x10,
66                 .flags         = 0,
67         }, {
68                 .id            = V4L2_CID_SATURATION,
69                 .type          = V4L2_CTRL_TYPE_INTEGER,
70                 .name          = "Saturation",
71                 .minimum       = 0,
72                 .maximum       = 255,
73                 .step          = 0x1,
74                 .default_value = 127,
75                 .flags         = 0,
76         }, {
77                 .id            = V4L2_CID_HUE,
78                 .type          = V4L2_CTRL_TYPE_INTEGER,
79                 .name          = "Hue",
80                 .minimum       = -128,
81                 .maximum       = 127,
82                 .step          = 0x1,
83                 .default_value = 0,
84                 .flags         = 0,
85         }
86 };
87
88 static int qctl_regs[ARRAY_SIZE(viu_qctrl)];
89
90 static int info_level;
91
92 #define dprintk(level, fmt, arg...)                                     \
93         do {                                                            \
94                 if (level <= info_level)                                \
95                         printk(KERN_DEBUG "viu: " fmt , ## arg);        \
96         } while (0)
97
98 /*
99  * Basic structures
100  */
101 struct viu_fmt {
102         char  name[32];
103         u32   fourcc;           /* v4l2 format id */
104         u32   pixelformat;
105         int   depth;
106 };
107
108 static struct viu_fmt formats[] = {
109         {
110                 .name           = "RGB-16 (5/B-6/G-5/R)",
111                 .fourcc         = V4L2_PIX_FMT_RGB565,
112                 .pixelformat    = V4L2_PIX_FMT_RGB565,
113                 .depth          = 16,
114         }, {
115                 .name           = "RGB-32 (A-R-G-B)",
116                 .fourcc         = V4L2_PIX_FMT_RGB32,
117                 .pixelformat    = V4L2_PIX_FMT_RGB32,
118                 .depth          = 32,
119         }
120 };
121
122 struct viu_dev;
123 struct viu_buf;
124
125 /* buffer for one video frame */
126 struct viu_buf {
127         /* common v4l buffer stuff -- must be first */
128         struct videobuf_buffer vb;
129         struct viu_fmt *fmt;
130 };
131
132 struct viu_dmaqueue {
133         struct viu_dev          *dev;
134         struct list_head        active;
135         struct list_head        queued;
136         struct timer_list       timeout;
137 };
138
139 struct viu_status {
140         u32 field_irq;
141         u32 vsync_irq;
142         u32 hsync_irq;
143         u32 vstart_irq;
144         u32 dma_end_irq;
145         u32 error_irq;
146 };
147
148 struct viu_reg {
149         u32 status_cfg;
150         u32 luminance;
151         u32 chroma_r;
152         u32 chroma_g;
153         u32 chroma_b;
154         u32 field_base_addr;
155         u32 dma_inc;
156         u32 picture_count;
157         u32 req_alarm;
158         u32 alpha;
159 } __attribute__ ((packed));
160
161 struct viu_dev {
162         struct v4l2_device      v4l2_dev;
163         struct mutex            lock;
164         spinlock_t              slock;
165         int                     users;
166
167         struct device           *dev;
168         /* various device info */
169         struct video_device     *vdev;
170         struct viu_dmaqueue     vidq;
171         enum v4l2_field         capfield;
172         int                     field;
173         int                     first;
174         int                     dma_done;
175
176         /* Hardware register area */
177         struct viu_reg          *vr;
178
179         /* Interrupt vector */
180         int                     irq;
181         struct viu_status       irqs;
182
183         /* video overlay */
184         struct v4l2_framebuffer ovbuf;
185         struct viu_fmt          *ovfmt;
186         unsigned int            ovenable;
187         enum v4l2_field         ovfield;
188
189         /* crop */
190         struct v4l2_rect        crop_current;
191
192         /* clock pointer */
193         struct clk              *clk;
194
195         /* decoder */
196         struct v4l2_subdev      *decoder;
197 };
198
199 struct viu_fh {
200         struct viu_dev          *dev;
201
202         /* video capture */
203         struct videobuf_queue   vb_vidq;
204         spinlock_t              vbq_lock; /* spinlock for the videobuf queue */
205
206         /* video overlay */
207         struct v4l2_window      win;
208         struct v4l2_clip        clips[1];
209
210         /* video capture */
211         struct viu_fmt          *fmt;
212         int                     width, height, sizeimage;
213         enum v4l2_buf_type      type;
214 };
215
216 static struct viu_reg reg_val;
217
218 /*
219  * Macro definitions of VIU registers
220  */
221
222 /* STATUS_CONFIG register */
223 enum status_config {
224         SOFT_RST                = 1 << 0,
225
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 */
238
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 */
246
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 */
253
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,
259                                                  * 0 in RGB565
260                                                  */
261 };
262
263 #define norm_maxw()     720
264 #define norm_maxh()     576
265
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)
269
270 #define NUM_FORMATS     ARRAY_SIZE(formats)
271
272 static irqreturn_t viu_intr(int irq, void *dev_id);
273
274 struct viu_fmt *format_by_fourcc(int fourcc)
275 {
276         int i;
277
278         for (i = 0; i < NUM_FORMATS; i++) {
279                 if (formats[i].pixelformat == fourcc)
280                         return formats + i;
281         }
282
283         dprintk(0, "unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
284         return NULL;
285 }
286
287 void viu_start_dma(struct viu_dev *dev)
288 {
289         struct viu_reg *vr = dev->vr;
290
291         dev->field = 0;
292
293         /* Enable DMA operation */
294         out_be32(&vr->status_cfg, SOFT_RST);
295         out_be32(&vr->status_cfg, INT_FIELD_EN);
296 }
297
298 void viu_stop_dma(struct viu_dev *dev)
299 {
300         struct viu_reg *vr = dev->vr;
301         int cnt = 100;
302         u32 status_cfg;
303
304         out_be32(&vr->status_cfg, 0);
305
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);
310
311         if (status_cfg & DMA_ACT) {
312                 do {
313                         status_cfg = in_be32(&vr->status_cfg);
314                         if (status_cfg & INT_DMA_END_STATUS)
315                                 break;
316                 } while (cnt--);
317
318                 if (cnt < 0) {
319                         /* timed out, issue soft reset */
320                         out_be32(&vr->status_cfg, SOFT_RST);
321                         out_be32(&vr->status_cfg, 0);
322                 } else {
323                         /* clear DMA_END and other pending irqs */
324                         out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
325                 }
326         }
327
328         dev->field = 0;
329 }
330
331 static int restart_video_queue(struct viu_dmaqueue *vidq)
332 {
333         struct viu_buf *buf, *prev;
334
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",
339                         buf, buf->vb.i);
340
341                 viu_stop_dma(vidq->dev);
342
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);
348                 }
349                 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
350                 return 0;
351         }
352
353         prev = NULL;
354         for (;;) {
355                 if (list_empty(&vidq->queued))
356                         return 0;
357                 buf = list_entry(vidq->queued.next, struct viu_buf, vb.queue);
358                 if (prev == NULL) {
359                         list_del(&buf->vb.queue);
360                         list_add_tail(&buf->vb.queue, &vidq->active);
361
362                         dprintk(1, "Restarting video dma\n");
363                         viu_stop_dma(vidq->dev);
364                         viu_start_dma(vidq->dev);
365
366                         buf->vb.state = VIDEOBUF_ACTIVE;
367                         mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
368                         dprintk(2, "[%p/%d] restart_queue - first active\n",
369                                 buf, buf->vb.i);
370
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",
378                                 buf, buf->vb.i);
379                 } else {
380                         return 0;
381                 }
382                 prev = buf;
383         }
384 }
385
386 static void viu_vid_timeout(unsigned long data)
387 {
388         struct viu_dev *dev = (struct viu_dev *)data;
389         struct viu_buf *buf;
390         struct viu_dmaqueue *vidq = &dev->vidq;
391
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);
398         }
399
400         restart_video_queue(vidq);
401 }
402
403 /*
404  * Videobuf operations
405  */
406 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
407                         unsigned int *size)
408 {
409         struct viu_fh *fh = vq->priv_data;
410
411         *size = fh->width * fh->height * fh->fmt->depth >> 3;
412         if (*count == 0)
413                 *count = 32;
414
415         while (*size * *count > VIU_VID_MEM_LIMIT * 1024 * 1024)
416                 (*count)--;
417
418         dprintk(1, "%s, count=%d, size=%d\n", __func__, *count, *size);
419         return 0;
420 }
421
422 static void free_buffer(struct videobuf_queue *vq, struct viu_buf *buf)
423 {
424         struct videobuf_buffer *vb = &buf->vb;
425         void *vaddr = NULL;
426
427         BUG_ON(in_interrupt());
428
429         videobuf_waiton(vq, &buf->vb, 0, 0);
430
431         if (vq->int_ops && vq->int_ops->vaddr)
432                 vaddr = vq->int_ops->vaddr(vb);
433
434         if (vaddr)
435                 videobuf_dma_contig_free(vq, &buf->vb);
436
437         buf->vb.state = VIDEOBUF_NEEDS_INIT;
438 }
439
440 inline int buffer_activate(struct viu_dev *dev, struct viu_buf *buf)
441 {
442         struct viu_reg *vr = dev->vr;
443         int bpp;
444
445         /* setup the DMA base address */
446         reg_val.field_base_addr = videobuf_to_dma_contig(&buf->vb);
447
448         dprintk(1, "buffer_activate [%p/%d]: dma addr 0x%lx\n",
449                 buf, buf->vb.i, (unsigned long)reg_val.field_base_addr);
450
451         /* interlace is on by default, set horizontal DMA increment */
452         reg_val.status_cfg = 0;
453         bpp = buf->fmt->depth >> 3;
454         switch (bpp) {
455         case 2:
456                 reg_val.status_cfg &= ~MODE_32BIT;
457                 reg_val.dma_inc = buf->vb.width * 2;
458                 break;
459         case 4:
460                 reg_val.status_cfg |= MODE_32BIT;
461                 reg_val.dma_inc = buf->vb.width * 4;
462                 break;
463         default:
464                 dprintk(0, "doesn't support color depth(%d)\n",
465                         bpp * 8);
466                 return -EINVAL;
467         }
468
469         /* setup picture_count register */
470         reg_val.picture_count = (buf->vb.height / 2) << 16 |
471                                 buf->vb.width;
472
473         reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
474
475         buf->vb.state = VIDEOBUF_ACTIVE;
476         dev->capfield = buf->vb.field;
477
478         /* reset dma increment if needed */
479         if (!V4L2_FIELD_HAS_BOTH(buf->vb.field))
480                 reg_val.dma_inc = 0;
481
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);
486         return 0;
487 }
488
489 static int buffer_prepare(struct videobuf_queue *vq,
490                           struct videobuf_buffer *vb,
491                           enum v4l2_field field)
492 {
493         struct viu_fh  *fh  = vq->priv_data;
494         struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
495         int rc;
496
497         BUG_ON(fh->fmt == NULL);
498
499         if (fh->width  < 48 || fh->width  > norm_maxw() ||
500             fh->height < 32 || fh->height > norm_maxh())
501                 return -EINVAL;
502         buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
503         if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size)
504                 return -EINVAL;
505
506         if (buf->fmt       != fh->fmt    ||
507             buf->vb.width  != fh->width  ||
508             buf->vb.height != fh->height ||
509             buf->vb.field  != field) {
510                 buf->fmt       = fh->fmt;
511                 buf->vb.width  = fh->width;
512                 buf->vb.height = fh->height;
513                 buf->vb.field  = field;
514         }
515
516         if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
517                 rc = videobuf_iolock(vq, &buf->vb, NULL);
518                 if (rc != 0)
519                         goto fail;
520
521                 buf->vb.width  = fh->width;
522                 buf->vb.height = fh->height;
523                 buf->vb.field  = field;
524                 buf->fmt       = fh->fmt;
525         }
526
527         buf->vb.state = VIDEOBUF_PREPARED;
528         return 0;
529
530 fail:
531         free_buffer(vq, buf);
532         return rc;
533 }
534
535 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
536 {
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;
542
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,
550                         vidq->queued.prev);
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",
554                         buf, buf->vb.i);
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",
562                         buf, buf->vb.i);
563
564                 buffer_activate(dev, buf);
565         } else {
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",
575                                 buf, buf->vb.i);
576                 } else {
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",
580                                 buf, buf->vb.i);
581                 }
582         }
583 }
584
585 static void buffer_release(struct videobuf_queue *vq,
586                                 struct videobuf_buffer *vb)
587 {
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;
591
592         viu_stop_dma(dev);
593         free_buffer(vq, buf);
594 }
595
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,
601 };
602
603 /*
604  * IOCTL vidioc handling
605  */
606 static int vidioc_querycap(struct file *file, void *priv,
607                            struct v4l2_capability *cap)
608 {
609         strcpy(cap->driver, "viu");
610         strcpy(cap->card, "viu");
611         cap->version = VIU_VERSION;
612         cap->capabilities =     V4L2_CAP_VIDEO_CAPTURE |
613                                 V4L2_CAP_STREAMING     |
614                                 V4L2_CAP_VIDEO_OVERLAY |
615                                 V4L2_CAP_READWRITE;
616         return 0;
617 }
618
619 static int vidioc_enum_fmt(struct file *file, void  *priv,
620                                         struct v4l2_fmtdesc *f)
621 {
622         int index = f->index;
623
624         if (f->index > NUM_FORMATS)
625                 return -EINVAL;
626
627         strlcpy(f->description, formats[index].name, sizeof(f->description));
628         f->pixelformat = formats[index].fourcc;
629         return 0;
630 }
631
632 static int vidioc_g_fmt_cap(struct file *file, void *priv,
633                                         struct v4l2_format *f)
634 {
635         struct viu_fh *fh = priv;
636
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;
644         return 0;
645 }
646
647 static int vidioc_try_fmt_cap(struct file *file, void *priv,
648                                         struct v4l2_format *f)
649 {
650         struct viu_fmt *fmt;
651         enum v4l2_field field;
652         unsigned int maxw, maxh;
653
654         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
655         if (!fmt) {
656                 dprintk(1, "Fourcc format (0x%08x) invalid.",
657                         f->fmt.pix.pixelformat);
658                 return -EINVAL;
659         }
660
661         field = f->fmt.pix.field;
662
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");
667                 return -EINVAL;
668         }
669
670         maxw  = norm_maxw();
671         maxh  = norm_maxh();
672
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;
685
686         return 0;
687 }
688
689 static int vidioc_s_fmt_cap(struct file *file, void *priv,
690                                         struct v4l2_format *f)
691 {
692         struct viu_fh *fh = priv;
693         int ret;
694
695         ret = vidioc_try_fmt_cap(file, fh, f);
696         if (ret < 0)
697                 return ret;
698
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;
704         fh->type          = f->type;
705         dprintk(1, "set to pixelformat '%4.6s'\n", (char *)&fh->fmt->name);
706         return 0;
707 }
708
709 static int vidioc_g_fmt_overlay(struct file *file, void *priv,
710                                         struct v4l2_format *f)
711 {
712         struct viu_fh *fh = priv;
713
714         f->fmt.win = fh->win;
715         return 0;
716 }
717
718 static int verify_preview(struct viu_dev *dev, struct v4l2_window *win)
719 {
720         enum v4l2_field field;
721         int maxw, maxh;
722
723         if (dev->ovbuf.base == NULL)
724                 return -EINVAL;
725         if (dev->ovfmt == NULL)
726                 return -EINVAL;
727         if (win->w.width < 48 || win->w.height < 32)
728                 return -EINVAL;
729
730         field = win->field;
731         maxw  = dev->crop_current.width;
732         maxh  = dev->crop_current.height;
733
734         if (field == V4L2_FIELD_ANY) {
735                 field = (win->w.height > maxh/2)
736                         ? V4L2_FIELD_INTERLACED
737                         : V4L2_FIELD_TOP;
738         }
739         switch (field) {
740         case V4L2_FIELD_TOP:
741         case V4L2_FIELD_BOTTOM:
742                 maxh = maxh / 2;
743                 break;
744         case V4L2_FIELD_INTERLACED:
745                 break;
746         default:
747                 return -EINVAL;
748         }
749
750         win->field = field;
751         if (win->w.width > maxw)
752                 win->w.width = maxw;
753         if (win->w.height > maxh)
754                 win->w.height = maxh;
755         return 0;
756 }
757
758 inline void viu_activate_overlay(struct viu_reg *viu_reg)
759 {
760         struct viu_reg *vr = viu_reg;
761
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);
765 }
766
767 static int viu_start_preview(struct viu_dev *dev, struct viu_fh *fh)
768 {
769         int bpp;
770
771         dprintk(1, "%s %dx%d %s\n", __func__,
772                 fh->win.w.width, fh->win.w.height, dev->ovfmt->name);
773
774         reg_val.status_cfg = 0;
775
776         /* setup window */
777         reg_val.picture_count = (fh->win.w.height / 2) << 16 |
778                                 fh->win.w.width;
779
780         /* setup color depth and dma increment */
781         bpp = dev->ovfmt->depth / 8;
782         switch (bpp) {
783         case 2:
784                 reg_val.status_cfg &= ~MODE_32BIT;
785                 reg_val.dma_inc = fh->win.w.width * 2;
786                 break;
787         case 4:
788                 reg_val.status_cfg |= MODE_32BIT;
789                 reg_val.dma_inc = fh->win.w.width * 4;
790                 break;
791         default:
792                 dprintk(0, "device doesn't support color depth(%d)\n",
793                         bpp * 8);
794                 return -EINVAL;
795         }
796
797         dev->ovfield = fh->win.field;
798         if (!V4L2_FIELD_HAS_BOTH(dev->ovfield))
799                 reg_val.dma_inc = 0;
800
801         reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
802
803         /* setup the base address of the overlay buffer */
804         reg_val.field_base_addr = (u32)dev->ovbuf.base;
805
806         dev->ovenable = 1;
807         viu_activate_overlay(dev->vr);
808
809         /* start dma */
810         viu_start_dma(dev);
811         return 0;
812 }
813
814 static int vidioc_s_fmt_overlay(struct file *file, void *priv,
815                                         struct v4l2_format *f)
816 {
817         struct viu_fh  *fh  = priv;
818         struct viu_dev *dev = (struct viu_dev *)fh->dev;
819         unsigned long  flags;
820         int err;
821
822         err = verify_preview(dev, &f->fmt.win);
823         if (err)
824                 return err;
825
826         mutex_lock(&dev->lock);
827         fh->win = f->fmt.win;
828
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);
833         return 0;
834 }
835
836 static int vidioc_try_fmt_overlay(struct file *file, void *priv,
837                                         struct v4l2_format *f)
838 {
839         return 0;
840 }
841
842 int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
843 {
844         struct viu_fh  *fh = priv;
845         struct viu_dev *dev = fh->dev;
846         struct v4l2_framebuffer *fb = arg;
847
848         *fb = dev->ovbuf;
849         fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
850         return 0;
851 }
852
853 int vidioc_s_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
854 {
855         struct viu_fh  *fh = priv;
856         struct viu_dev *dev = fh->dev;
857         struct v4l2_framebuffer *fb = arg;
858         struct viu_fmt *fmt;
859
860         if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
861                 return -EPERM;
862
863         /* check args */
864         fmt = format_by_fourcc(fb->fmt.pixelformat);
865         if (fmt == NULL)
866                 return -EINVAL;
867
868         /* ok, accept it */
869         dev->ovbuf = *fb;
870         dev->ovfmt = fmt;
871         if (dev->ovbuf.fmt.bytesperline == 0) {
872                 dev->ovbuf.fmt.bytesperline =
873                         dev->ovbuf.fmt.width * fmt->depth / 8;
874         }
875         return 0;
876 }
877
878 static int vidioc_reqbufs(struct file *file, void *priv,
879                                 struct v4l2_requestbuffers *p)
880 {
881         struct viu_fh *fh = priv;
882
883         return videobuf_reqbufs(&fh->vb_vidq, p);
884 }
885
886 static int vidioc_querybuf(struct file *file, void *priv,
887                                         struct v4l2_buffer *p)
888 {
889         struct viu_fh *fh = priv;
890
891         return videobuf_querybuf(&fh->vb_vidq, p);
892 }
893
894 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
895 {
896         struct viu_fh *fh = priv;
897
898         return videobuf_qbuf(&fh->vb_vidq, p);
899 }
900
901 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
902 {
903         struct viu_fh *fh = priv;
904
905         return videobuf_dqbuf(&fh->vb_vidq, p,
906                                 file->f_flags & O_NONBLOCK);
907 }
908
909 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
910 {
911         struct viu_fh *fh = priv;
912
913         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
914                 return -EINVAL;
915         if (fh->type != i)
916                 return -EINVAL;
917
918         viu_start_dma(fh->dev);
919
920         return videobuf_streamon(&fh->vb_vidq);
921 }
922
923 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
924 {
925         struct viu_fh  *fh = priv;
926
927         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
928                 return -EINVAL;
929         if (fh->type != i)
930                 return -EINVAL;
931
932         viu_stop_dma(fh->dev);
933
934         return videobuf_streamoff(&fh->vb_vidq);
935 }
936
937 #define decoder_call(viu, o, f, args...) \
938         v4l2_subdev_call(viu->decoder, o, f, ##args)
939
940 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
941 {
942         struct viu_fh *fh = priv;
943
944         decoder_call(fh->dev, core, s_std, *id);
945         return 0;
946 }
947
948 /* only one input in this driver */
949 static int vidioc_enum_input(struct file *file, void *priv,
950                                         struct v4l2_input *inp)
951 {
952         struct viu_fh *fh = priv;
953
954         if (inp->index != 0)
955                 return -EINVAL;
956
957         inp->type = V4L2_INPUT_TYPE_CAMERA;
958         inp->std = fh->dev->vdev->tvnorms;
959         strcpy(inp->name, "Camera");
960         return 0;
961 }
962
963 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
964 {
965         *i = 0;
966         return 0;
967 }
968
969 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
970 {
971         struct viu_fh *fh = priv;
972
973         if (i > 1)
974                 return -EINVAL;
975
976         decoder_call(fh->dev, video, s_routing, i, 0, 0);
977         return 0;
978 }
979
980 /* Controls */
981 static int vidioc_queryctrl(struct file *file, void *priv,
982                                 struct v4l2_queryctrl *qc)
983 {
984         int i;
985
986         for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
987                 if (qc->id && qc->id == viu_qctrl[i].id) {
988                         memcpy(qc, &(viu_qctrl[i]), sizeof(*qc));
989                         return 0;
990                 }
991         }
992         return -EINVAL;
993 }
994
995 static int vidioc_g_ctrl(struct file *file, void *priv,
996                                 struct v4l2_control *ctrl)
997 {
998         int i;
999
1000         for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1001                 if (ctrl->id == viu_qctrl[i].id) {
1002                         ctrl->value = qctl_regs[i];
1003                         return 0;
1004                 }
1005         }
1006         return -EINVAL;
1007 }
1008 static int vidioc_s_ctrl(struct file *file, void *priv,
1009                                 struct v4l2_control *ctrl)
1010 {
1011         int i;
1012
1013         for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1014                 if (ctrl->id == viu_qctrl[i].id) {
1015                         if (ctrl->value < viu_qctrl[i].minimum
1016                                 || ctrl->value > viu_qctrl[i].maximum)
1017                                         return -ERANGE;
1018                         qctl_regs[i] = ctrl->value;
1019                         return 0;
1020                 }
1021         }
1022         return -EINVAL;
1023 }
1024
1025 inline void viu_activate_next_buf(struct viu_dev *dev,
1026                                 struct viu_dmaqueue *viuq)
1027 {
1028         struct viu_dmaqueue *vidq = viuq;
1029         struct viu_buf *buf;
1030
1031         /* launch another DMA operation for an active/queued buffer */
1032         if (!list_empty(&vidq->active)) {
1033                 buf = list_entry(vidq->active.next, struct viu_buf,
1034                                         vb.queue);
1035                 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1036                 buffer_activate(dev, buf);
1037         } else if (!list_empty(&vidq->queued)) {
1038                 buf = list_entry(vidq->queued.next, struct viu_buf,
1039                                         vb.queue);
1040                 list_del(&buf->vb.queue);
1041
1042                 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1043                 list_add_tail(&buf->vb.queue, &vidq->active);
1044                 buf->vb.state = VIDEOBUF_ACTIVE;
1045                 buffer_activate(dev, buf);
1046         }
1047 }
1048
1049 inline void viu_default_settings(struct viu_reg *viu_reg)
1050 {
1051         struct viu_reg *vr = viu_reg;
1052
1053         out_be32(&vr->luminance, 0x9512A254);
1054         out_be32(&vr->chroma_r, 0x03310000);
1055         out_be32(&vr->chroma_g, 0x06600F38);
1056         out_be32(&vr->chroma_b, 0x00000409);
1057         out_be32(&vr->alpha, 0x000000ff);
1058         out_be32(&vr->req_alarm, 0x00000090);
1059         dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
1060                 in_be32(&vr->status_cfg), in_be32(&vr->field_base_addr));
1061 }
1062
1063 static void viu_overlay_intr(struct viu_dev *dev, u32 status)
1064 {
1065         struct viu_reg *vr = dev->vr;
1066
1067         if (status & INT_DMA_END_STATUS)
1068                 dev->dma_done = 1;
1069
1070         if (status & INT_FIELD_STATUS) {
1071                 if (dev->dma_done) {
1072                         u32 addr = reg_val.field_base_addr;
1073
1074                         dev->dma_done = 0;
1075                         if (status & FIELD_NO)
1076                                 addr += reg_val.dma_inc;
1077
1078                         out_be32(&vr->field_base_addr, addr);
1079                         out_be32(&vr->dma_inc, reg_val.dma_inc);
1080                         out_be32(&vr->status_cfg,
1081                                  (status & 0xffc0ffff) |
1082                                  (status & INT_ALL_STATUS) |
1083                                  reg_val.status_cfg);
1084                 } else if (status & INT_VSYNC_STATUS) {
1085                         out_be32(&vr->status_cfg,
1086                                  (status & 0xffc0ffff) |
1087                                  (status & INT_ALL_STATUS) |
1088                                  reg_val.status_cfg);
1089                 }
1090         }
1091 }
1092
1093 static void viu_capture_intr(struct viu_dev *dev, u32 status)
1094 {
1095         struct viu_dmaqueue *vidq = &dev->vidq;
1096         struct viu_reg *vr = dev->vr;
1097         struct viu_buf *buf;
1098         int field_num;
1099         int need_two;
1100         int dma_done = 0;
1101
1102         field_num = status & FIELD_NO;
1103         need_two = V4L2_FIELD_HAS_BOTH(dev->capfield);
1104
1105         if (status & INT_DMA_END_STATUS) {
1106                 dma_done = 1;
1107                 if (((field_num == 0) && (dev->field == 0)) ||
1108                     (field_num && (dev->field == 1)))
1109                         dev->field++;
1110         }
1111
1112         if (status & INT_FIELD_STATUS) {
1113                 dprintk(1, "irq: field %d, done %d\n",
1114                         !!field_num, dma_done);
1115                 if (unlikely(dev->first)) {
1116                         if (field_num == 0) {
1117                                 dev->first = 0;
1118                                 dprintk(1, "activate first buf\n");
1119                                 viu_activate_next_buf(dev, vidq);
1120                         } else
1121                                 dprintk(1, "wait field 0\n");
1122                         return;
1123                 }
1124
1125                 /* setup buffer address for next dma operation */
1126                 if (!list_empty(&vidq->active)) {
1127                         u32 addr = reg_val.field_base_addr;
1128
1129                         if (field_num && need_two) {
1130                                 addr += reg_val.dma_inc;
1131                                 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1132                                         (unsigned long)addr, dev->field);
1133                         }
1134                         out_be32(&vr->field_base_addr, addr);
1135                         out_be32(&vr->dma_inc, reg_val.dma_inc);
1136                         out_be32(&vr->status_cfg,
1137                                  (status & 0xffc0ffff) |
1138                                  (status & INT_ALL_STATUS) |
1139                                  reg_val.status_cfg);
1140                         return;
1141                 }
1142         }
1143
1144         if (dma_done && field_num && (dev->field == 2)) {
1145                 dev->field = 0;
1146                 buf = list_entry(vidq->active.next,
1147                                  struct viu_buf, vb.queue);
1148                 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1149                         buf, buf->vb.i,
1150                         (unsigned long)videobuf_to_dma_contig(&buf->vb),
1151                         (unsigned long)in_be32(&vr->field_base_addr));
1152
1153                 if (waitqueue_active(&buf->vb.done)) {
1154                         list_del(&buf->vb.queue);
1155                         do_gettimeofday(&buf->vb.ts);
1156                         buf->vb.state = VIDEOBUF_DONE;
1157                         buf->vb.field_count++;
1158                         wake_up(&buf->vb.done);
1159                 }
1160                 /* activate next dma buffer */
1161                 viu_activate_next_buf(dev, vidq);
1162         }
1163 }
1164
1165 static irqreturn_t viu_intr(int irq, void *dev_id)
1166 {
1167         struct viu_dev *dev  = (struct viu_dev *)dev_id;
1168         struct viu_reg *vr = dev->vr;
1169         u32 status;
1170         u32 error;
1171
1172         status = in_be32(&vr->status_cfg);
1173
1174         if (status & INT_ERROR_STATUS) {
1175                 dev->irqs.error_irq++;
1176                 error = status & ERR_MASK;
1177                 if (error)
1178                         dprintk(1, "Err: error(%d), times:%d!\n",
1179                                 error >> 4, dev->irqs.error_irq);
1180                 /* Clear interrupt error bit and error flags */
1181                 out_be32(&vr->status_cfg,
1182                          (status & 0xffc0ffff) | INT_ERROR_STATUS);
1183         }
1184
1185         if (status & INT_DMA_END_STATUS) {
1186                 dev->irqs.dma_end_irq++;
1187                 dev->dma_done = 1;
1188                 dprintk(2, "VIU DMA end interrupt times: %d\n",
1189                                         dev->irqs.dma_end_irq);
1190         }
1191
1192         if (status & INT_HSYNC_STATUS)
1193                 dev->irqs.hsync_irq++;
1194
1195         if (status & INT_FIELD_STATUS) {
1196                 dev->irqs.field_irq++;
1197                 dprintk(2, "VIU field interrupt times: %d\n",
1198                                         dev->irqs.field_irq);
1199         }
1200
1201         if (status & INT_VSTART_STATUS)
1202                 dev->irqs.vstart_irq++;
1203
1204         if (status & INT_VSYNC_STATUS) {
1205                 dev->irqs.vsync_irq++;
1206                 dprintk(2, "VIU vsync interrupt times: %d\n",
1207                         dev->irqs.vsync_irq);
1208         }
1209
1210         /* clear all pending irqs */
1211         status = in_be32(&vr->status_cfg);
1212         out_be32(&vr->status_cfg,
1213                  (status & 0xffc0ffff) | (status & INT_ALL_STATUS));
1214
1215         if (dev->ovenable) {
1216                 viu_overlay_intr(dev, status);
1217                 return IRQ_HANDLED;
1218         }
1219
1220         /* Capture mode */
1221         viu_capture_intr(dev, status);
1222         return IRQ_HANDLED;
1223 }
1224
1225 /*
1226  * File operations for the device
1227  */
1228 static int viu_open(struct file *file)
1229 {
1230         struct video_device *vdev = video_devdata(file);
1231         struct viu_dev *dev = video_get_drvdata(vdev);
1232         struct viu_fh *fh;
1233         struct viu_reg *vr;
1234         int minor = vdev->minor;
1235         u32 status_cfg;
1236         int i;
1237
1238         dprintk(1, "viu: open (minor=%d)\n", minor);
1239
1240         dev->users++;
1241         if (dev->users > 1) {
1242                 dev->users--;
1243                 return -EBUSY;
1244         }
1245
1246         vr = dev->vr;
1247
1248         dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1249                 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1250
1251         /* allocate and initialize per filehandle data */
1252         fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1253         if (!fh) {
1254                 dev->users--;
1255                 return -ENOMEM;
1256         }
1257
1258         file->private_data = fh;
1259         fh->dev = dev;
1260
1261         fh->type     = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1262         fh->fmt      = format_by_fourcc(V4L2_PIX_FMT_RGB32);
1263         fh->width    = norm_maxw();
1264         fh->height   = norm_maxh();
1265         dev->crop_current.width  = fh->width;
1266         dev->crop_current.height = fh->height;
1267
1268         /* Put all controls at a sane state */
1269         for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++)
1270                 qctl_regs[i] = viu_qctrl[i].default_value;
1271
1272         dprintk(1, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1273                 (unsigned long)fh, (unsigned long)dev,
1274                 (unsigned long)&dev->vidq);
1275         dprintk(1, "Open: list_empty queued=%d\n",
1276                 list_empty(&dev->vidq.queued));
1277         dprintk(1, "Open: list_empty active=%d\n",
1278                 list_empty(&dev->vidq.active));
1279
1280         viu_default_settings(vr);
1281
1282         status_cfg = in_be32(&vr->status_cfg);
1283         out_be32(&vr->status_cfg,
1284                  status_cfg & ~(INT_VSYNC_EN | INT_HSYNC_EN |
1285                                 INT_FIELD_EN | INT_VSTART_EN |
1286                                 INT_DMA_END_EN | INT_ERROR_EN | INT_ECC_EN));
1287
1288         status_cfg = in_be32(&vr->status_cfg);
1289         out_be32(&vr->status_cfg, status_cfg | INT_ALL_STATUS);
1290
1291         spin_lock_init(&fh->vbq_lock);
1292         videobuf_queue_dma_contig_init(&fh->vb_vidq, &viu_video_qops,
1293                                        dev->dev, &fh->vbq_lock,
1294                                        fh->type, V4L2_FIELD_INTERLACED,
1295                                        sizeof(struct viu_buf), fh, NULL);
1296         return 0;
1297 }
1298
1299 static ssize_t viu_read(struct file *file, char __user *data, size_t count,
1300                         loff_t *ppos)
1301 {
1302         struct viu_fh *fh = file->private_data;
1303         struct viu_dev *dev = fh->dev;
1304         int ret = 0;
1305
1306         dprintk(2, "%s\n", __func__);
1307         if (dev->ovenable)
1308                 dev->ovenable = 0;
1309
1310         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1311                 viu_start_dma(dev);
1312                 ret = videobuf_read_stream(&fh->vb_vidq, data, count,
1313                                 ppos, 0, file->f_flags & O_NONBLOCK);
1314                 return ret;
1315         }
1316         return 0;
1317 }
1318
1319 static unsigned int viu_poll(struct file *file, struct poll_table_struct *wait)
1320 {
1321         struct viu_fh *fh = file->private_data;
1322         struct videobuf_queue *q = &fh->vb_vidq;
1323
1324         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1325                 return POLLERR;
1326
1327         return videobuf_poll_stream(file, q, wait);
1328 }
1329
1330 static int viu_release(struct file *file)
1331 {
1332         struct viu_fh *fh = file->private_data;
1333         struct viu_dev *dev = fh->dev;
1334         int minor = video_devdata(file)->minor;
1335
1336         viu_stop_dma(dev);
1337         videobuf_stop(&fh->vb_vidq);
1338         videobuf_mmap_free(&fh->vb_vidq);
1339
1340         kfree(fh);
1341
1342         dev->users--;
1343         dprintk(1, "close (minor=%d, users=%d)\n",
1344                 minor, dev->users);
1345         return 0;
1346 }
1347
1348 void viu_reset(struct viu_reg *reg)
1349 {
1350         out_be32(&reg->status_cfg, 0);
1351         out_be32(&reg->luminance, 0x9512a254);
1352         out_be32(&reg->chroma_r, 0x03310000);
1353         out_be32(&reg->chroma_g, 0x06600f38);
1354         out_be32(&reg->chroma_b, 0x00000409);
1355         out_be32(&reg->field_base_addr, 0);
1356         out_be32(&reg->dma_inc, 0);
1357         out_be32(&reg->picture_count, 0x01e002d0);
1358         out_be32(&reg->req_alarm, 0x00000090);
1359         out_be32(&reg->alpha, 0x000000ff);
1360 }
1361
1362 static int viu_mmap(struct file *file, struct vm_area_struct *vma)
1363 {
1364         struct viu_fh *fh = file->private_data;
1365         int ret;
1366
1367         dprintk(1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1368
1369         ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1370
1371         dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1372                 (unsigned long)vma->vm_start,
1373                 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1374                 ret);
1375
1376         return ret;
1377 }
1378
1379 static struct v4l2_file_operations viu_fops = {
1380         .owner          = THIS_MODULE,
1381         .open           = viu_open,
1382         .release        = viu_release,
1383         .read           = viu_read,
1384         .poll           = viu_poll,
1385         .ioctl          = video_ioctl2, /* V4L2 ioctl handler */
1386         .mmap           = viu_mmap,
1387 };
1388
1389 static const struct v4l2_ioctl_ops viu_ioctl_ops = {
1390         .vidioc_querycap        = vidioc_querycap,
1391         .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt,
1392         .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_cap,
1393         .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_cap,
1394         .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_cap,
1395         .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt,
1396         .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_overlay,
1397         .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_overlay,
1398         .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_overlay,
1399         .vidioc_g_fbuf        = vidioc_g_fbuf,
1400         .vidioc_s_fbuf        = vidioc_s_fbuf,
1401         .vidioc_reqbufs       = vidioc_reqbufs,
1402         .vidioc_querybuf      = vidioc_querybuf,
1403         .vidioc_qbuf          = vidioc_qbuf,
1404         .vidioc_dqbuf         = vidioc_dqbuf,
1405         .vidioc_s_std         = vidioc_s_std,
1406         .vidioc_enum_input    = vidioc_enum_input,
1407         .vidioc_g_input       = vidioc_g_input,
1408         .vidioc_s_input       = vidioc_s_input,
1409         .vidioc_queryctrl     = vidioc_queryctrl,
1410         .vidioc_g_ctrl        = vidioc_g_ctrl,
1411         .vidioc_s_ctrl        = vidioc_s_ctrl,
1412         .vidioc_streamon      = vidioc_streamon,
1413         .vidioc_streamoff     = vidioc_streamoff,
1414 };
1415
1416 static struct video_device viu_template = {
1417         .name           = "FSL viu",
1418         .fops           = &viu_fops,
1419         .minor          = -1,
1420         .ioctl_ops      = &viu_ioctl_ops,
1421         .release        = video_device_release,
1422
1423         .tvnorms        = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1424         .current_norm   = V4L2_STD_NTSC_M,
1425 };
1426
1427 static int __devinit viu_of_probe(struct platform_device *op,
1428                                   const struct of_device_id *match)
1429 {
1430         struct viu_dev *viu_dev;
1431         struct video_device *vdev;
1432         struct resource r;
1433         struct viu_reg __iomem *viu_regs;
1434         struct i2c_adapter *ad;
1435         int ret, viu_irq;
1436
1437         ret = of_address_to_resource(op->dev.of_node, 0, &r);
1438         if (ret) {
1439                 dev_err(&op->dev, "Can't parse device node resource\n");
1440                 return -ENODEV;
1441         }
1442
1443         viu_irq = irq_of_parse_and_map(op->dev.of_node, 0);
1444         if (viu_irq == NO_IRQ) {
1445                 dev_err(&op->dev, "Error while mapping the irq\n");
1446                 return -EINVAL;
1447         }
1448
1449         /* request mem region */
1450         if (!devm_request_mem_region(&op->dev, r.start,
1451                                      sizeof(struct viu_reg), DRV_NAME)) {
1452                 dev_err(&op->dev, "Error while requesting mem region\n");
1453                 ret = -EBUSY;
1454                 goto err;
1455         }
1456
1457         /* remap registers */
1458         viu_regs = devm_ioremap(&op->dev, r.start, sizeof(struct viu_reg));
1459         if (!viu_regs) {
1460                 dev_err(&op->dev, "Can't map register set\n");
1461                 ret = -ENOMEM;
1462                 goto err;
1463         }
1464
1465         /* Prepare our private structure */
1466         viu_dev = devm_kzalloc(&op->dev, sizeof(struct viu_dev), GFP_ATOMIC);
1467         if (!viu_dev) {
1468                 dev_err(&op->dev, "Can't allocate private structure\n");
1469                 ret = -ENOMEM;
1470                 goto err;
1471         }
1472
1473         viu_dev->vr = viu_regs;
1474         viu_dev->irq = viu_irq;
1475         viu_dev->dev = &op->dev;
1476
1477         /* init video dma queues */
1478         INIT_LIST_HEAD(&viu_dev->vidq.active);
1479         INIT_LIST_HEAD(&viu_dev->vidq.queued);
1480
1481         /* initialize locks */
1482         mutex_init(&viu_dev->lock);
1483
1484         snprintf(viu_dev->v4l2_dev.name,
1485                  sizeof(viu_dev->v4l2_dev.name), "%s", "VIU");
1486         ret = v4l2_device_register(viu_dev->dev, &viu_dev->v4l2_dev);
1487         if (ret < 0) {
1488                 dev_err(&op->dev, "v4l2_device_register() failed: %d\n", ret);
1489                 goto err;
1490         }
1491
1492         ad = i2c_get_adapter(0);
1493         viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
1494                         "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
1495
1496         viu_dev->vidq.timeout.function = viu_vid_timeout;
1497         viu_dev->vidq.timeout.data     = (unsigned long)viu_dev;
1498         init_timer(&viu_dev->vidq.timeout);
1499         viu_dev->first = 1;
1500
1501         /* Allocate memory for video device */
1502         vdev = video_device_alloc();
1503         if (vdev == NULL) {
1504                 ret = -ENOMEM;
1505                 goto err_vdev;
1506         }
1507
1508         memcpy(vdev, &viu_template, sizeof(viu_template));
1509
1510         vdev->v4l2_dev = &viu_dev->v4l2_dev;
1511
1512         viu_dev->vdev = vdev;
1513
1514         video_set_drvdata(viu_dev->vdev, viu_dev);
1515
1516         ret = video_register_device(viu_dev->vdev, VFL_TYPE_GRABBER, -1);
1517         if (ret < 0) {
1518                 video_device_release(viu_dev->vdev);
1519                 goto err_vdev;
1520         }
1521
1522         /* enable VIU clock */
1523         viu_dev->clk = clk_get(&op->dev, "viu_clk");
1524         if (IS_ERR(viu_dev->clk)) {
1525                 dev_err(&op->dev, "failed to find the clock module!\n");
1526                 ret = -ENODEV;
1527                 goto err_clk;
1528         } else {
1529                 clk_enable(viu_dev->clk);
1530         }
1531
1532         /* reset VIU module */
1533         viu_reset(viu_dev->vr);
1534
1535         /* install interrupt handler */
1536         if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
1537                 dev_err(&op->dev, "Request VIU IRQ failed.\n");
1538                 ret = -ENODEV;
1539                 goto err_irq;
1540         }
1541
1542         dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
1543         return ret;
1544
1545 err_irq:
1546         clk_disable(viu_dev->clk);
1547         clk_put(viu_dev->clk);
1548 err_clk:
1549         video_unregister_device(viu_dev->vdev);
1550 err_vdev:
1551         i2c_put_adapter(ad);
1552         v4l2_device_unregister(&viu_dev->v4l2_dev);
1553 err:
1554         irq_dispose_mapping(viu_irq);
1555         return ret;
1556 }
1557
1558 static int __devexit viu_of_remove(struct platform_device *op)
1559 {
1560         struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1561         struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1562         struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
1563                                               struct v4l2_subdev, list);
1564         struct i2c_client *client = v4l2_get_subdevdata(sdev);
1565
1566         free_irq(dev->irq, (void *)dev);
1567         irq_dispose_mapping(dev->irq);
1568
1569         clk_disable(dev->clk);
1570         clk_put(dev->clk);
1571
1572         video_unregister_device(dev->vdev);
1573         i2c_put_adapter(client->adapter);
1574         v4l2_device_unregister(&dev->v4l2_dev);
1575         return 0;
1576 }
1577
1578 #ifdef CONFIG_PM
1579 static int viu_suspend(struct platform_device *op, pm_message_t state)
1580 {
1581         struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1582         struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1583
1584         clk_disable(dev->clk);
1585         return 0;
1586 }
1587
1588 static int viu_resume(struct platform_device *op)
1589 {
1590         struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1591         struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1592
1593         clk_enable(dev->clk);
1594         return 0;
1595 }
1596 #endif
1597
1598 /*
1599  * Initialization and module stuff
1600  */
1601 static struct of_device_id mpc512x_viu_of_match[] = {
1602         {
1603                 .compatible = "fsl,mpc5121-viu",
1604         },
1605         {},
1606 };
1607 MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
1608
1609 static struct of_platform_driver viu_of_platform_driver = {
1610         .probe = viu_of_probe,
1611         .remove = __devexit_p(viu_of_remove),
1612 #ifdef CONFIG_PM
1613         .suspend = viu_suspend,
1614         .resume = viu_resume,
1615 #endif
1616         .driver = {
1617                 .name = DRV_NAME,
1618                 .owner = THIS_MODULE,
1619                 .of_match_table = mpc512x_viu_of_match,
1620         },
1621 };
1622
1623 static int __init viu_init(void)
1624 {
1625         return of_register_platform_driver(&viu_of_platform_driver);
1626 }
1627
1628 static void __exit viu_exit(void)
1629 {
1630         of_unregister_platform_driver(&viu_of_platform_driver);
1631 }
1632
1633 module_init(viu_init);
1634 module_exit(viu_exit);
1635
1636 MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1637 MODULE_AUTHOR("Hongjun Chen");
1638 MODULE_LICENSE("GPL");