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