rt2800: initialize BBP_R104 on proper subroutines
[linux/fpc-iii.git] / drivers / media / platform / fsl-viu.c
blob3a6a0dcdc3e426390fa2b9ff2ab9edbcc9c47b0d
1 /*
2 * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
4 * Freescale VIU video driver
6 * Authors: Hongjun Chen <hong-jun.chen@freescale.com>
7 * Porting to 2.6.35 by DENX Software Engineering,
8 * Anatolij Gustschin <agust@denx.de>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
17 #include <linux/module.h>
18 #include <linux/clk.h>
19 #include <linux/kernel.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/io.h>
24 #include <linux/of_platform.h>
25 #include <linux/slab.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/videobuf-dma-contig.h>
31 #define DRV_NAME "fsl_viu"
32 #define VIU_VERSION "0.5.1"
34 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
36 #define VIU_VID_MEM_LIMIT 4 /* Video memory limit, in Mb */
38 /* I2C address of video decoder chip is 0x4A */
39 #define VIU_VIDEO_DECODER_ADDR 0x25
41 /* supported controls */
42 static struct v4l2_queryctrl viu_qctrl[] = {
44 .id = V4L2_CID_BRIGHTNESS,
45 .type = V4L2_CTRL_TYPE_INTEGER,
46 .name = "Brightness",
47 .minimum = 0,
48 .maximum = 255,
49 .step = 1,
50 .default_value = 127,
51 .flags = 0,
52 }, {
53 .id = V4L2_CID_CONTRAST,
54 .type = V4L2_CTRL_TYPE_INTEGER,
55 .name = "Contrast",
56 .minimum = 0,
57 .maximum = 255,
58 .step = 0x1,
59 .default_value = 0x10,
60 .flags = 0,
61 }, {
62 .id = V4L2_CID_SATURATION,
63 .type = V4L2_CTRL_TYPE_INTEGER,
64 .name = "Saturation",
65 .minimum = 0,
66 .maximum = 255,
67 .step = 0x1,
68 .default_value = 127,
69 .flags = 0,
70 }, {
71 .id = V4L2_CID_HUE,
72 .type = V4L2_CTRL_TYPE_INTEGER,
73 .name = "Hue",
74 .minimum = -128,
75 .maximum = 127,
76 .step = 0x1,
77 .default_value = 0,
78 .flags = 0,
82 static int qctl_regs[ARRAY_SIZE(viu_qctrl)];
84 static int info_level;
86 #define dprintk(level, fmt, arg...) \
87 do { \
88 if (level <= info_level) \
89 printk(KERN_DEBUG "viu: " fmt , ## arg); \
90 } while (0)
93 * Basic structures
95 struct viu_fmt {
96 char name[32];
97 u32 fourcc; /* v4l2 format id */
98 u32 pixelformat;
99 int depth;
102 static struct viu_fmt formats[] = {
104 .name = "RGB-16 (5/B-6/G-5/R)",
105 .fourcc = V4L2_PIX_FMT_RGB565,
106 .pixelformat = V4L2_PIX_FMT_RGB565,
107 .depth = 16,
108 }, {
109 .name = "RGB-32 (A-R-G-B)",
110 .fourcc = V4L2_PIX_FMT_RGB32,
111 .pixelformat = V4L2_PIX_FMT_RGB32,
112 .depth = 32,
116 struct viu_dev;
117 struct viu_buf;
119 /* buffer for one video frame */
120 struct viu_buf {
121 /* common v4l buffer stuff -- must be first */
122 struct videobuf_buffer vb;
123 struct viu_fmt *fmt;
126 struct viu_dmaqueue {
127 struct viu_dev *dev;
128 struct list_head active;
129 struct list_head queued;
130 struct timer_list timeout;
133 struct viu_status {
134 u32 field_irq;
135 u32 vsync_irq;
136 u32 hsync_irq;
137 u32 vstart_irq;
138 u32 dma_end_irq;
139 u32 error_irq;
142 struct viu_reg {
143 u32 status_cfg;
144 u32 luminance;
145 u32 chroma_r;
146 u32 chroma_g;
147 u32 chroma_b;
148 u32 field_base_addr;
149 u32 dma_inc;
150 u32 picture_count;
151 u32 req_alarm;
152 u32 alpha;
153 } __attribute__ ((packed));
155 struct viu_dev {
156 struct v4l2_device v4l2_dev;
157 struct mutex lock;
158 spinlock_t slock;
159 int users;
161 struct device *dev;
162 /* various device info */
163 struct video_device *vdev;
164 struct viu_dmaqueue vidq;
165 enum v4l2_field capfield;
166 int field;
167 int first;
168 int dma_done;
170 /* Hardware register area */
171 struct viu_reg *vr;
173 /* Interrupt vector */
174 int irq;
175 struct viu_status irqs;
177 /* video overlay */
178 struct v4l2_framebuffer ovbuf;
179 struct viu_fmt *ovfmt;
180 unsigned int ovenable;
181 enum v4l2_field ovfield;
183 /* crop */
184 struct v4l2_rect crop_current;
186 /* clock pointer */
187 struct clk *clk;
189 /* decoder */
190 struct v4l2_subdev *decoder;
192 v4l2_std_id std;
195 struct viu_fh {
196 struct viu_dev *dev;
198 /* video capture */
199 struct videobuf_queue vb_vidq;
200 spinlock_t vbq_lock; /* spinlock for the videobuf queue */
202 /* video overlay */
203 struct v4l2_window win;
204 struct v4l2_clip clips[1];
206 /* video capture */
207 struct viu_fmt *fmt;
208 int width, height, sizeimage;
209 enum v4l2_buf_type type;
212 static struct viu_reg reg_val;
215 * Macro definitions of VIU registers
218 /* STATUS_CONFIG register */
219 enum status_config {
220 SOFT_RST = 1 << 0,
222 ERR_MASK = 0x0f << 4, /* Error code mask */
223 ERR_NO = 0x00, /* No error */
224 ERR_DMA_V = 0x01 << 4, /* DMA in vertical active */
225 ERR_DMA_VB = 0x02 << 4, /* DMA in vertical blanking */
226 ERR_LINE_TOO_LONG = 0x04 << 4, /* Line too long */
227 ERR_TOO_MANG_LINES = 0x05 << 4, /* Too many lines in field */
228 ERR_LINE_TOO_SHORT = 0x06 << 4, /* Line too short */
229 ERR_NOT_ENOUGH_LINE = 0x07 << 4, /* Not enough lines in field */
230 ERR_FIFO_OVERFLOW = 0x08 << 4, /* FIFO overflow */
231 ERR_FIFO_UNDERFLOW = 0x09 << 4, /* FIFO underflow */
232 ERR_1bit_ECC = 0x0a << 4, /* One bit ECC error */
233 ERR_MORE_ECC = 0x0b << 4, /* Two/more bits ECC error */
235 INT_FIELD_EN = 0x01 << 8, /* Enable field interrupt */
236 INT_VSYNC_EN = 0x01 << 9, /* Enable vsync interrupt */
237 INT_HSYNC_EN = 0x01 << 10, /* Enable hsync interrupt */
238 INT_VSTART_EN = 0x01 << 11, /* Enable vstart interrupt */
239 INT_DMA_END_EN = 0x01 << 12, /* Enable DMA end interrupt */
240 INT_ERROR_EN = 0x01 << 13, /* Enable error interrupt */
241 INT_ECC_EN = 0x01 << 14, /* Enable ECC interrupt */
243 INT_FIELD_STATUS = 0x01 << 16, /* field interrupt status */
244 INT_VSYNC_STATUS = 0x01 << 17, /* vsync interrupt status */
245 INT_HSYNC_STATUS = 0x01 << 18, /* hsync interrupt status */
246 INT_VSTART_STATUS = 0x01 << 19, /* vstart interrupt status */
247 INT_DMA_END_STATUS = 0x01 << 20, /* DMA end interrupt status */
248 INT_ERROR_STATUS = 0x01 << 21, /* error interrupt status */
250 DMA_ACT = 0x01 << 27, /* Enable DMA transfer */
251 FIELD_NO = 0x01 << 28, /* Field number */
252 DITHER_ON = 0x01 << 29, /* Dithering is on */
253 ROUND_ON = 0x01 << 30, /* Round is on */
254 MODE_32BIT = 0x01 << 31, /* Data in RGBa888,
255 * 0 in RGB565
259 #define norm_maxw() 720
260 #define norm_maxh() 576
262 #define INT_ALL_STATUS (INT_FIELD_STATUS | INT_VSYNC_STATUS | \
263 INT_HSYNC_STATUS | INT_VSTART_STATUS | \
264 INT_DMA_END_STATUS | INT_ERROR_STATUS)
266 #define NUM_FORMATS ARRAY_SIZE(formats)
268 static irqreturn_t viu_intr(int irq, void *dev_id);
270 struct viu_fmt *format_by_fourcc(int fourcc)
272 int i;
274 for (i = 0; i < NUM_FORMATS; i++) {
275 if (formats[i].pixelformat == fourcc)
276 return formats + i;
279 dprintk(0, "unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
280 return NULL;
283 void viu_start_dma(struct viu_dev *dev)
285 struct viu_reg *vr = dev->vr;
287 dev->field = 0;
289 /* Enable DMA operation */
290 out_be32(&vr->status_cfg, SOFT_RST);
291 out_be32(&vr->status_cfg, INT_FIELD_EN);
294 void viu_stop_dma(struct viu_dev *dev)
296 struct viu_reg *vr = dev->vr;
297 int cnt = 100;
298 u32 status_cfg;
300 out_be32(&vr->status_cfg, 0);
302 /* Clear pending interrupts */
303 status_cfg = in_be32(&vr->status_cfg);
304 if (status_cfg & 0x3f0000)
305 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
307 if (status_cfg & DMA_ACT) {
308 do {
309 status_cfg = in_be32(&vr->status_cfg);
310 if (status_cfg & INT_DMA_END_STATUS)
311 break;
312 } while (cnt--);
314 if (cnt < 0) {
315 /* timed out, issue soft reset */
316 out_be32(&vr->status_cfg, SOFT_RST);
317 out_be32(&vr->status_cfg, 0);
318 } else {
319 /* clear DMA_END and other pending irqs */
320 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
324 dev->field = 0;
327 static int restart_video_queue(struct viu_dmaqueue *vidq)
329 struct viu_buf *buf, *prev;
331 dprintk(1, "%s vidq=0x%08lx\n", __func__, (unsigned long)vidq);
332 if (!list_empty(&vidq->active)) {
333 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
334 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
335 buf, buf->vb.i);
337 viu_stop_dma(vidq->dev);
339 /* cancel all outstanding capture requests */
340 list_for_each_entry_safe(buf, prev, &vidq->active, vb.queue) {
341 list_del(&buf->vb.queue);
342 buf->vb.state = VIDEOBUF_ERROR;
343 wake_up(&buf->vb.done);
345 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
346 return 0;
349 prev = NULL;
350 for (;;) {
351 if (list_empty(&vidq->queued))
352 return 0;
353 buf = list_entry(vidq->queued.next, struct viu_buf, vb.queue);
354 if (prev == NULL) {
355 list_move_tail(&buf->vb.queue, &vidq->active);
357 dprintk(1, "Restarting video dma\n");
358 viu_stop_dma(vidq->dev);
359 viu_start_dma(vidq->dev);
361 buf->vb.state = VIDEOBUF_ACTIVE;
362 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
363 dprintk(2, "[%p/%d] restart_queue - first active\n",
364 buf, buf->vb.i);
366 } else if (prev->vb.width == buf->vb.width &&
367 prev->vb.height == buf->vb.height &&
368 prev->fmt == buf->fmt) {
369 list_move_tail(&buf->vb.queue, &vidq->active);
370 buf->vb.state = VIDEOBUF_ACTIVE;
371 dprintk(2, "[%p/%d] restart_queue - move to active\n",
372 buf, buf->vb.i);
373 } else {
374 return 0;
376 prev = buf;
380 static void viu_vid_timeout(unsigned long data)
382 struct viu_dev *dev = (struct viu_dev *)data;
383 struct viu_buf *buf;
384 struct viu_dmaqueue *vidq = &dev->vidq;
386 while (!list_empty(&vidq->active)) {
387 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
388 list_del(&buf->vb.queue);
389 buf->vb.state = VIDEOBUF_ERROR;
390 wake_up(&buf->vb.done);
391 dprintk(1, "viu/0: [%p/%d] timeout\n", buf, buf->vb.i);
394 restart_video_queue(vidq);
398 * Videobuf operations
400 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
401 unsigned int *size)
403 struct viu_fh *fh = vq->priv_data;
405 *size = fh->width * fh->height * fh->fmt->depth >> 3;
406 if (*count == 0)
407 *count = 32;
409 while (*size * *count > VIU_VID_MEM_LIMIT * 1024 * 1024)
410 (*count)--;
412 dprintk(1, "%s, count=%d, size=%d\n", __func__, *count, *size);
413 return 0;
416 static void free_buffer(struct videobuf_queue *vq, struct viu_buf *buf)
418 struct videobuf_buffer *vb = &buf->vb;
419 void *vaddr = NULL;
421 BUG_ON(in_interrupt());
423 videobuf_waiton(vq, &buf->vb, 0, 0);
425 if (vq->int_ops && vq->int_ops->vaddr)
426 vaddr = vq->int_ops->vaddr(vb);
428 if (vaddr)
429 videobuf_dma_contig_free(vq, &buf->vb);
431 buf->vb.state = VIDEOBUF_NEEDS_INIT;
434 inline int buffer_activate(struct viu_dev *dev, struct viu_buf *buf)
436 struct viu_reg *vr = dev->vr;
437 int bpp;
439 /* setup the DMA base address */
440 reg_val.field_base_addr = videobuf_to_dma_contig(&buf->vb);
442 dprintk(1, "buffer_activate [%p/%d]: dma addr 0x%lx\n",
443 buf, buf->vb.i, (unsigned long)reg_val.field_base_addr);
445 /* interlace is on by default, set horizontal DMA increment */
446 reg_val.status_cfg = 0;
447 bpp = buf->fmt->depth >> 3;
448 switch (bpp) {
449 case 2:
450 reg_val.status_cfg &= ~MODE_32BIT;
451 reg_val.dma_inc = buf->vb.width * 2;
452 break;
453 case 4:
454 reg_val.status_cfg |= MODE_32BIT;
455 reg_val.dma_inc = buf->vb.width * 4;
456 break;
457 default:
458 dprintk(0, "doesn't support color depth(%d)\n",
459 bpp * 8);
460 return -EINVAL;
463 /* setup picture_count register */
464 reg_val.picture_count = (buf->vb.height / 2) << 16 |
465 buf->vb.width;
467 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
469 buf->vb.state = VIDEOBUF_ACTIVE;
470 dev->capfield = buf->vb.field;
472 /* reset dma increment if needed */
473 if (!V4L2_FIELD_HAS_BOTH(buf->vb.field))
474 reg_val.dma_inc = 0;
476 out_be32(&vr->dma_inc, reg_val.dma_inc);
477 out_be32(&vr->picture_count, reg_val.picture_count);
478 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
479 mod_timer(&dev->vidq.timeout, jiffies + BUFFER_TIMEOUT);
480 return 0;
483 static int buffer_prepare(struct videobuf_queue *vq,
484 struct videobuf_buffer *vb,
485 enum v4l2_field field)
487 struct viu_fh *fh = vq->priv_data;
488 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
489 int rc;
491 BUG_ON(fh->fmt == NULL);
493 if (fh->width < 48 || fh->width > norm_maxw() ||
494 fh->height < 32 || fh->height > norm_maxh())
495 return -EINVAL;
496 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
497 if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size)
498 return -EINVAL;
500 if (buf->fmt != fh->fmt ||
501 buf->vb.width != fh->width ||
502 buf->vb.height != fh->height ||
503 buf->vb.field != field) {
504 buf->fmt = fh->fmt;
505 buf->vb.width = fh->width;
506 buf->vb.height = fh->height;
507 buf->vb.field = field;
510 if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
511 rc = videobuf_iolock(vq, &buf->vb, NULL);
512 if (rc != 0)
513 goto fail;
515 buf->vb.width = fh->width;
516 buf->vb.height = fh->height;
517 buf->vb.field = field;
518 buf->fmt = fh->fmt;
521 buf->vb.state = VIDEOBUF_PREPARED;
522 return 0;
524 fail:
525 free_buffer(vq, buf);
526 return rc;
529 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
531 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
532 struct viu_fh *fh = vq->priv_data;
533 struct viu_dev *dev = fh->dev;
534 struct viu_dmaqueue *vidq = &dev->vidq;
535 struct viu_buf *prev;
537 if (!list_empty(&vidq->queued)) {
538 dprintk(1, "adding vb queue=0x%08lx\n",
539 (unsigned long)&buf->vb.queue);
540 dprintk(1, "vidq pointer 0x%p, queued 0x%p\n",
541 vidq, &vidq->queued);
542 dprintk(1, "dev %p, queued: self %p, next %p, head %p\n",
543 dev, &vidq->queued, vidq->queued.next,
544 vidq->queued.prev);
545 list_add_tail(&buf->vb.queue, &vidq->queued);
546 buf->vb.state = VIDEOBUF_QUEUED;
547 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
548 buf, buf->vb.i);
549 } else if (list_empty(&vidq->active)) {
550 dprintk(1, "adding vb active=0x%08lx\n",
551 (unsigned long)&buf->vb.queue);
552 list_add_tail(&buf->vb.queue, &vidq->active);
553 buf->vb.state = VIDEOBUF_ACTIVE;
554 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
555 dprintk(2, "[%p/%d] buffer_queue - first active\n",
556 buf, buf->vb.i);
558 buffer_activate(dev, buf);
559 } else {
560 dprintk(1, "adding vb queue2=0x%08lx\n",
561 (unsigned long)&buf->vb.queue);
562 prev = list_entry(vidq->active.prev, struct viu_buf, vb.queue);
563 if (prev->vb.width == buf->vb.width &&
564 prev->vb.height == buf->vb.height &&
565 prev->fmt == buf->fmt) {
566 list_add_tail(&buf->vb.queue, &vidq->active);
567 buf->vb.state = VIDEOBUF_ACTIVE;
568 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
569 buf, buf->vb.i);
570 } else {
571 list_add_tail(&buf->vb.queue, &vidq->queued);
572 buf->vb.state = VIDEOBUF_QUEUED;
573 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
574 buf, buf->vb.i);
579 static void buffer_release(struct videobuf_queue *vq,
580 struct videobuf_buffer *vb)
582 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
583 struct viu_fh *fh = vq->priv_data;
584 struct viu_dev *dev = (struct viu_dev *)fh->dev;
586 viu_stop_dma(dev);
587 free_buffer(vq, buf);
590 static struct videobuf_queue_ops viu_video_qops = {
591 .buf_setup = buffer_setup,
592 .buf_prepare = buffer_prepare,
593 .buf_queue = buffer_queue,
594 .buf_release = buffer_release,
598 * IOCTL vidioc handling
600 static int vidioc_querycap(struct file *file, void *priv,
601 struct v4l2_capability *cap)
603 strcpy(cap->driver, "viu");
604 strcpy(cap->card, "viu");
605 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
606 V4L2_CAP_STREAMING |
607 V4L2_CAP_VIDEO_OVERLAY |
608 V4L2_CAP_READWRITE;
609 return 0;
612 static int vidioc_enum_fmt(struct file *file, void *priv,
613 struct v4l2_fmtdesc *f)
615 int index = f->index;
617 if (f->index > NUM_FORMATS)
618 return -EINVAL;
620 strlcpy(f->description, formats[index].name, sizeof(f->description));
621 f->pixelformat = formats[index].fourcc;
622 return 0;
625 static int vidioc_g_fmt_cap(struct file *file, void *priv,
626 struct v4l2_format *f)
628 struct viu_fh *fh = priv;
630 f->fmt.pix.width = fh->width;
631 f->fmt.pix.height = fh->height;
632 f->fmt.pix.field = fh->vb_vidq.field;
633 f->fmt.pix.pixelformat = fh->fmt->pixelformat;
634 f->fmt.pix.bytesperline =
635 (f->fmt.pix.width * fh->fmt->depth) >> 3;
636 f->fmt.pix.sizeimage = fh->sizeimage;
637 return 0;
640 static int vidioc_try_fmt_cap(struct file *file, void *priv,
641 struct v4l2_format *f)
643 struct viu_fmt *fmt;
644 enum v4l2_field field;
645 unsigned int maxw, maxh;
647 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
648 if (!fmt) {
649 dprintk(1, "Fourcc format (0x%08x) invalid.",
650 f->fmt.pix.pixelformat);
651 return -EINVAL;
654 field = f->fmt.pix.field;
656 if (field == V4L2_FIELD_ANY) {
657 field = V4L2_FIELD_INTERLACED;
658 } else if (field != V4L2_FIELD_INTERLACED) {
659 dprintk(1, "Field type invalid.\n");
660 return -EINVAL;
663 maxw = norm_maxw();
664 maxh = norm_maxh();
666 f->fmt.pix.field = field;
667 if (f->fmt.pix.height < 32)
668 f->fmt.pix.height = 32;
669 if (f->fmt.pix.height > maxh)
670 f->fmt.pix.height = maxh;
671 if (f->fmt.pix.width < 48)
672 f->fmt.pix.width = 48;
673 if (f->fmt.pix.width > maxw)
674 f->fmt.pix.width = maxw;
675 f->fmt.pix.width &= ~0x03;
676 f->fmt.pix.bytesperline =
677 (f->fmt.pix.width * fmt->depth) >> 3;
679 return 0;
682 static int vidioc_s_fmt_cap(struct file *file, void *priv,
683 struct v4l2_format *f)
685 struct viu_fh *fh = priv;
686 int ret;
688 ret = vidioc_try_fmt_cap(file, fh, f);
689 if (ret < 0)
690 return ret;
692 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
693 fh->width = f->fmt.pix.width;
694 fh->height = f->fmt.pix.height;
695 fh->sizeimage = f->fmt.pix.sizeimage;
696 fh->vb_vidq.field = f->fmt.pix.field;
697 fh->type = f->type;
698 dprintk(1, "set to pixelformat '%4.6s'\n", (char *)&fh->fmt->name);
699 return 0;
702 static int vidioc_g_fmt_overlay(struct file *file, void *priv,
703 struct v4l2_format *f)
705 struct viu_fh *fh = priv;
707 f->fmt.win = fh->win;
708 return 0;
711 static int verify_preview(struct viu_dev *dev, struct v4l2_window *win)
713 enum v4l2_field field;
714 int maxw, maxh;
716 if (dev->ovbuf.base == NULL)
717 return -EINVAL;
718 if (dev->ovfmt == NULL)
719 return -EINVAL;
720 if (win->w.width < 48 || win->w.height < 32)
721 return -EINVAL;
723 field = win->field;
724 maxw = dev->crop_current.width;
725 maxh = dev->crop_current.height;
727 if (field == V4L2_FIELD_ANY) {
728 field = (win->w.height > maxh/2)
729 ? V4L2_FIELD_INTERLACED
730 : V4L2_FIELD_TOP;
732 switch (field) {
733 case V4L2_FIELD_TOP:
734 case V4L2_FIELD_BOTTOM:
735 maxh = maxh / 2;
736 break;
737 case V4L2_FIELD_INTERLACED:
738 break;
739 default:
740 return -EINVAL;
743 win->field = field;
744 if (win->w.width > maxw)
745 win->w.width = maxw;
746 if (win->w.height > maxh)
747 win->w.height = maxh;
748 return 0;
751 inline void viu_activate_overlay(struct viu_reg *viu_reg)
753 struct viu_reg *vr = viu_reg;
755 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
756 out_be32(&vr->dma_inc, reg_val.dma_inc);
757 out_be32(&vr->picture_count, reg_val.picture_count);
760 static int viu_setup_preview(struct viu_dev *dev, struct viu_fh *fh)
762 int bpp;
764 dprintk(1, "%s %dx%d %s\n", __func__,
765 fh->win.w.width, fh->win.w.height, dev->ovfmt->name);
767 reg_val.status_cfg = 0;
769 /* setup window */
770 reg_val.picture_count = (fh->win.w.height / 2) << 16 |
771 fh->win.w.width;
773 /* setup color depth and dma increment */
774 bpp = dev->ovfmt->depth / 8;
775 switch (bpp) {
776 case 2:
777 reg_val.status_cfg &= ~MODE_32BIT;
778 reg_val.dma_inc = fh->win.w.width * 2;
779 break;
780 case 4:
781 reg_val.status_cfg |= MODE_32BIT;
782 reg_val.dma_inc = fh->win.w.width * 4;
783 break;
784 default:
785 dprintk(0, "device doesn't support color depth(%d)\n",
786 bpp * 8);
787 return -EINVAL;
790 dev->ovfield = fh->win.field;
791 if (!V4L2_FIELD_HAS_BOTH(dev->ovfield))
792 reg_val.dma_inc = 0;
794 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
796 /* setup the base address of the overlay buffer */
797 reg_val.field_base_addr = (u32)dev->ovbuf.base;
799 return 0;
802 static int vidioc_s_fmt_overlay(struct file *file, void *priv,
803 struct v4l2_format *f)
805 struct viu_fh *fh = priv;
806 struct viu_dev *dev = (struct viu_dev *)fh->dev;
807 unsigned long flags;
808 int err;
810 err = verify_preview(dev, &f->fmt.win);
811 if (err)
812 return err;
814 fh->win = f->fmt.win;
816 spin_lock_irqsave(&dev->slock, flags);
817 viu_setup_preview(dev, fh);
818 spin_unlock_irqrestore(&dev->slock, flags);
819 return 0;
822 static int vidioc_try_fmt_overlay(struct file *file, void *priv,
823 struct v4l2_format *f)
825 return 0;
828 static int vidioc_overlay(struct file *file, void *priv, unsigned int on)
830 struct viu_fh *fh = priv;
831 struct viu_dev *dev = (struct viu_dev *)fh->dev;
832 unsigned long flags;
834 if (on) {
835 spin_lock_irqsave(&dev->slock, flags);
836 viu_activate_overlay(dev->vr);
837 dev->ovenable = 1;
839 /* start dma */
840 viu_start_dma(dev);
841 spin_unlock_irqrestore(&dev->slock, flags);
842 } else {
843 viu_stop_dma(dev);
844 dev->ovenable = 0;
847 return 0;
850 int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
852 struct viu_fh *fh = priv;
853 struct viu_dev *dev = fh->dev;
854 struct v4l2_framebuffer *fb = arg;
856 *fb = dev->ovbuf;
857 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
858 return 0;
861 int vidioc_s_fbuf(struct file *file, void *priv, const struct v4l2_framebuffer *arg)
863 struct viu_fh *fh = priv;
864 struct viu_dev *dev = fh->dev;
865 const struct v4l2_framebuffer *fb = arg;
866 struct viu_fmt *fmt;
868 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
869 return -EPERM;
871 /* check args */
872 fmt = format_by_fourcc(fb->fmt.pixelformat);
873 if (fmt == NULL)
874 return -EINVAL;
876 /* ok, accept it */
877 dev->ovbuf = *fb;
878 dev->ovfmt = fmt;
879 if (dev->ovbuf.fmt.bytesperline == 0) {
880 dev->ovbuf.fmt.bytesperline =
881 dev->ovbuf.fmt.width * fmt->depth / 8;
883 return 0;
886 static int vidioc_reqbufs(struct file *file, void *priv,
887 struct v4l2_requestbuffers *p)
889 struct viu_fh *fh = priv;
891 return videobuf_reqbufs(&fh->vb_vidq, p);
894 static int vidioc_querybuf(struct file *file, void *priv,
895 struct v4l2_buffer *p)
897 struct viu_fh *fh = priv;
899 return videobuf_querybuf(&fh->vb_vidq, p);
902 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
904 struct viu_fh *fh = priv;
906 return videobuf_qbuf(&fh->vb_vidq, p);
909 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
911 struct viu_fh *fh = priv;
913 return videobuf_dqbuf(&fh->vb_vidq, p,
914 file->f_flags & O_NONBLOCK);
917 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
919 struct viu_fh *fh = priv;
920 struct viu_dev *dev = fh->dev;
922 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
923 return -EINVAL;
924 if (fh->type != i)
925 return -EINVAL;
927 if (dev->ovenable)
928 dev->ovenable = 0;
930 viu_start_dma(fh->dev);
932 return videobuf_streamon(&fh->vb_vidq);
935 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
937 struct viu_fh *fh = priv;
939 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
940 return -EINVAL;
941 if (fh->type != i)
942 return -EINVAL;
944 viu_stop_dma(fh->dev);
946 return videobuf_streamoff(&fh->vb_vidq);
949 #define decoder_call(viu, o, f, args...) \
950 v4l2_subdev_call(viu->decoder, o, f, ##args)
952 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
954 struct viu_fh *fh = priv;
956 decoder_call(fh->dev, video, querystd, std_id);
957 return 0;
960 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
962 struct viu_fh *fh = priv;
964 fh->dev->std = id;
965 decoder_call(fh->dev, core, s_std, id);
966 return 0;
969 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
971 struct viu_fh *fh = priv;
973 *std_id = fh->dev->std;
974 return 0;
977 /* only one input in this driver */
978 static int vidioc_enum_input(struct file *file, void *priv,
979 struct v4l2_input *inp)
981 struct viu_fh *fh = priv;
983 if (inp->index != 0)
984 return -EINVAL;
986 inp->type = V4L2_INPUT_TYPE_CAMERA;
987 inp->std = fh->dev->vdev->tvnorms;
988 strcpy(inp->name, "Camera");
989 return 0;
992 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
994 *i = 0;
995 return 0;
998 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1000 struct viu_fh *fh = priv;
1002 if (i > 1)
1003 return -EINVAL;
1005 decoder_call(fh->dev, video, s_routing, i, 0, 0);
1006 return 0;
1009 /* Controls */
1010 static int vidioc_queryctrl(struct file *file, void *priv,
1011 struct v4l2_queryctrl *qc)
1013 int i;
1015 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1016 if (qc->id && qc->id == viu_qctrl[i].id) {
1017 memcpy(qc, &(viu_qctrl[i]), sizeof(*qc));
1018 return 0;
1021 return -EINVAL;
1024 static int vidioc_g_ctrl(struct file *file, void *priv,
1025 struct v4l2_control *ctrl)
1027 int i;
1029 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1030 if (ctrl->id == viu_qctrl[i].id) {
1031 ctrl->value = qctl_regs[i];
1032 return 0;
1035 return -EINVAL;
1037 static int vidioc_s_ctrl(struct file *file, void *priv,
1038 struct v4l2_control *ctrl)
1040 int i;
1042 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1043 if (ctrl->id == viu_qctrl[i].id) {
1044 if (ctrl->value < viu_qctrl[i].minimum
1045 || ctrl->value > viu_qctrl[i].maximum)
1046 return -ERANGE;
1047 qctl_regs[i] = ctrl->value;
1048 return 0;
1051 return -EINVAL;
1054 inline void viu_activate_next_buf(struct viu_dev *dev,
1055 struct viu_dmaqueue *viuq)
1057 struct viu_dmaqueue *vidq = viuq;
1058 struct viu_buf *buf;
1060 /* launch another DMA operation for an active/queued buffer */
1061 if (!list_empty(&vidq->active)) {
1062 buf = list_entry(vidq->active.next, struct viu_buf,
1063 vb.queue);
1064 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1065 buffer_activate(dev, buf);
1066 } else if (!list_empty(&vidq->queued)) {
1067 buf = list_entry(vidq->queued.next, struct viu_buf,
1068 vb.queue);
1069 list_del(&buf->vb.queue);
1071 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1072 list_add_tail(&buf->vb.queue, &vidq->active);
1073 buf->vb.state = VIDEOBUF_ACTIVE;
1074 buffer_activate(dev, buf);
1078 inline void viu_default_settings(struct viu_reg *viu_reg)
1080 struct viu_reg *vr = viu_reg;
1082 out_be32(&vr->luminance, 0x9512A254);
1083 out_be32(&vr->chroma_r, 0x03310000);
1084 out_be32(&vr->chroma_g, 0x06600F38);
1085 out_be32(&vr->chroma_b, 0x00000409);
1086 out_be32(&vr->alpha, 0x000000ff);
1087 out_be32(&vr->req_alarm, 0x00000090);
1088 dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
1089 in_be32(&vr->status_cfg), in_be32(&vr->field_base_addr));
1092 static void viu_overlay_intr(struct viu_dev *dev, u32 status)
1094 struct viu_reg *vr = dev->vr;
1096 if (status & INT_DMA_END_STATUS)
1097 dev->dma_done = 1;
1099 if (status & INT_FIELD_STATUS) {
1100 if (dev->dma_done) {
1101 u32 addr = reg_val.field_base_addr;
1103 dev->dma_done = 0;
1104 if (status & FIELD_NO)
1105 addr += reg_val.dma_inc;
1107 out_be32(&vr->field_base_addr, addr);
1108 out_be32(&vr->dma_inc, reg_val.dma_inc);
1109 out_be32(&vr->status_cfg,
1110 (status & 0xffc0ffff) |
1111 (status & INT_ALL_STATUS) |
1112 reg_val.status_cfg);
1113 } else if (status & INT_VSYNC_STATUS) {
1114 out_be32(&vr->status_cfg,
1115 (status & 0xffc0ffff) |
1116 (status & INT_ALL_STATUS) |
1117 reg_val.status_cfg);
1122 static void viu_capture_intr(struct viu_dev *dev, u32 status)
1124 struct viu_dmaqueue *vidq = &dev->vidq;
1125 struct viu_reg *vr = dev->vr;
1126 struct viu_buf *buf;
1127 int field_num;
1128 int need_two;
1129 int dma_done = 0;
1131 field_num = status & FIELD_NO;
1132 need_two = V4L2_FIELD_HAS_BOTH(dev->capfield);
1134 if (status & INT_DMA_END_STATUS) {
1135 dma_done = 1;
1136 if (((field_num == 0) && (dev->field == 0)) ||
1137 (field_num && (dev->field == 1)))
1138 dev->field++;
1141 if (status & INT_FIELD_STATUS) {
1142 dprintk(1, "irq: field %d, done %d\n",
1143 !!field_num, dma_done);
1144 if (unlikely(dev->first)) {
1145 if (field_num == 0) {
1146 dev->first = 0;
1147 dprintk(1, "activate first buf\n");
1148 viu_activate_next_buf(dev, vidq);
1149 } else
1150 dprintk(1, "wait field 0\n");
1151 return;
1154 /* setup buffer address for next dma operation */
1155 if (!list_empty(&vidq->active)) {
1156 u32 addr = reg_val.field_base_addr;
1158 if (field_num && need_two) {
1159 addr += reg_val.dma_inc;
1160 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1161 (unsigned long)addr, dev->field);
1163 out_be32(&vr->field_base_addr, addr);
1164 out_be32(&vr->dma_inc, reg_val.dma_inc);
1165 out_be32(&vr->status_cfg,
1166 (status & 0xffc0ffff) |
1167 (status & INT_ALL_STATUS) |
1168 reg_val.status_cfg);
1169 return;
1173 if (dma_done && field_num && (dev->field == 2)) {
1174 dev->field = 0;
1175 buf = list_entry(vidq->active.next,
1176 struct viu_buf, vb.queue);
1177 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1178 buf, buf->vb.i,
1179 (unsigned long)videobuf_to_dma_contig(&buf->vb),
1180 (unsigned long)in_be32(&vr->field_base_addr));
1182 if (waitqueue_active(&buf->vb.done)) {
1183 list_del(&buf->vb.queue);
1184 v4l2_get_timestamp(&buf->vb.ts);
1185 buf->vb.state = VIDEOBUF_DONE;
1186 buf->vb.field_count++;
1187 wake_up(&buf->vb.done);
1189 /* activate next dma buffer */
1190 viu_activate_next_buf(dev, vidq);
1194 static irqreturn_t viu_intr(int irq, void *dev_id)
1196 struct viu_dev *dev = (struct viu_dev *)dev_id;
1197 struct viu_reg *vr = dev->vr;
1198 u32 status;
1199 u32 error;
1201 status = in_be32(&vr->status_cfg);
1203 if (status & INT_ERROR_STATUS) {
1204 dev->irqs.error_irq++;
1205 error = status & ERR_MASK;
1206 if (error)
1207 dprintk(1, "Err: error(%d), times:%d!\n",
1208 error >> 4, dev->irqs.error_irq);
1209 /* Clear interrupt error bit and error flags */
1210 out_be32(&vr->status_cfg,
1211 (status & 0xffc0ffff) | INT_ERROR_STATUS);
1214 if (status & INT_DMA_END_STATUS) {
1215 dev->irqs.dma_end_irq++;
1216 dev->dma_done = 1;
1217 dprintk(2, "VIU DMA end interrupt times: %d\n",
1218 dev->irqs.dma_end_irq);
1221 if (status & INT_HSYNC_STATUS)
1222 dev->irqs.hsync_irq++;
1224 if (status & INT_FIELD_STATUS) {
1225 dev->irqs.field_irq++;
1226 dprintk(2, "VIU field interrupt times: %d\n",
1227 dev->irqs.field_irq);
1230 if (status & INT_VSTART_STATUS)
1231 dev->irqs.vstart_irq++;
1233 if (status & INT_VSYNC_STATUS) {
1234 dev->irqs.vsync_irq++;
1235 dprintk(2, "VIU vsync interrupt times: %d\n",
1236 dev->irqs.vsync_irq);
1239 /* clear all pending irqs */
1240 status = in_be32(&vr->status_cfg);
1241 out_be32(&vr->status_cfg,
1242 (status & 0xffc0ffff) | (status & INT_ALL_STATUS));
1244 if (dev->ovenable) {
1245 viu_overlay_intr(dev, status);
1246 return IRQ_HANDLED;
1249 /* Capture mode */
1250 viu_capture_intr(dev, status);
1251 return IRQ_HANDLED;
1255 * File operations for the device
1257 static int viu_open(struct file *file)
1259 struct video_device *vdev = video_devdata(file);
1260 struct viu_dev *dev = video_get_drvdata(vdev);
1261 struct viu_fh *fh;
1262 struct viu_reg *vr;
1263 int minor = vdev->minor;
1264 u32 status_cfg;
1265 int i;
1267 dprintk(1, "viu: open (minor=%d)\n", minor);
1269 dev->users++;
1270 if (dev->users > 1) {
1271 dev->users--;
1272 return -EBUSY;
1275 vr = dev->vr;
1277 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1278 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1280 if (mutex_lock_interruptible(&dev->lock)) {
1281 dev->users--;
1282 return -ERESTARTSYS;
1285 /* allocate and initialize per filehandle data */
1286 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1287 if (!fh) {
1288 dev->users--;
1289 mutex_unlock(&dev->lock);
1290 return -ENOMEM;
1293 file->private_data = fh;
1294 fh->dev = dev;
1296 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1297 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_RGB32);
1298 fh->width = norm_maxw();
1299 fh->height = norm_maxh();
1300 dev->crop_current.width = fh->width;
1301 dev->crop_current.height = fh->height;
1303 /* Put all controls at a sane state */
1304 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++)
1305 qctl_regs[i] = viu_qctrl[i].default_value;
1307 dprintk(1, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1308 (unsigned long)fh, (unsigned long)dev,
1309 (unsigned long)&dev->vidq);
1310 dprintk(1, "Open: list_empty queued=%d\n",
1311 list_empty(&dev->vidq.queued));
1312 dprintk(1, "Open: list_empty active=%d\n",
1313 list_empty(&dev->vidq.active));
1315 viu_default_settings(vr);
1317 status_cfg = in_be32(&vr->status_cfg);
1318 out_be32(&vr->status_cfg,
1319 status_cfg & ~(INT_VSYNC_EN | INT_HSYNC_EN |
1320 INT_FIELD_EN | INT_VSTART_EN |
1321 INT_DMA_END_EN | INT_ERROR_EN | INT_ECC_EN));
1323 status_cfg = in_be32(&vr->status_cfg);
1324 out_be32(&vr->status_cfg, status_cfg | INT_ALL_STATUS);
1326 spin_lock_init(&fh->vbq_lock);
1327 videobuf_queue_dma_contig_init(&fh->vb_vidq, &viu_video_qops,
1328 dev->dev, &fh->vbq_lock,
1329 fh->type, V4L2_FIELD_INTERLACED,
1330 sizeof(struct viu_buf), fh,
1331 &fh->dev->lock);
1332 mutex_unlock(&dev->lock);
1333 return 0;
1336 static ssize_t viu_read(struct file *file, char __user *data, size_t count,
1337 loff_t *ppos)
1339 struct viu_fh *fh = file->private_data;
1340 struct viu_dev *dev = fh->dev;
1341 int ret = 0;
1343 dprintk(2, "%s\n", __func__);
1344 if (dev->ovenable)
1345 dev->ovenable = 0;
1347 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1348 if (mutex_lock_interruptible(&dev->lock))
1349 return -ERESTARTSYS;
1350 viu_start_dma(dev);
1351 ret = videobuf_read_stream(&fh->vb_vidq, data, count,
1352 ppos, 0, file->f_flags & O_NONBLOCK);
1353 mutex_unlock(&dev->lock);
1354 return ret;
1356 return 0;
1359 static unsigned int viu_poll(struct file *file, struct poll_table_struct *wait)
1361 struct viu_fh *fh = file->private_data;
1362 struct videobuf_queue *q = &fh->vb_vidq;
1363 struct viu_dev *dev = fh->dev;
1364 unsigned int res;
1366 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1367 return POLLERR;
1369 mutex_lock(&dev->lock);
1370 res = videobuf_poll_stream(file, q, wait);
1371 mutex_unlock(&dev->lock);
1372 return res;
1375 static int viu_release(struct file *file)
1377 struct viu_fh *fh = file->private_data;
1378 struct viu_dev *dev = fh->dev;
1379 int minor = video_devdata(file)->minor;
1381 mutex_lock(&dev->lock);
1382 viu_stop_dma(dev);
1383 videobuf_stop(&fh->vb_vidq);
1384 videobuf_mmap_free(&fh->vb_vidq);
1385 mutex_unlock(&dev->lock);
1387 kfree(fh);
1389 dev->users--;
1390 dprintk(1, "close (minor=%d, users=%d)\n",
1391 minor, dev->users);
1392 return 0;
1395 void viu_reset(struct viu_reg *reg)
1397 out_be32(&reg->status_cfg, 0);
1398 out_be32(&reg->luminance, 0x9512a254);
1399 out_be32(&reg->chroma_r, 0x03310000);
1400 out_be32(&reg->chroma_g, 0x06600f38);
1401 out_be32(&reg->chroma_b, 0x00000409);
1402 out_be32(&reg->field_base_addr, 0);
1403 out_be32(&reg->dma_inc, 0);
1404 out_be32(&reg->picture_count, 0x01e002d0);
1405 out_be32(&reg->req_alarm, 0x00000090);
1406 out_be32(&reg->alpha, 0x000000ff);
1409 static int viu_mmap(struct file *file, struct vm_area_struct *vma)
1411 struct viu_fh *fh = file->private_data;
1412 struct viu_dev *dev = fh->dev;
1413 int ret;
1415 dprintk(1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1417 if (mutex_lock_interruptible(&dev->lock))
1418 return -ERESTARTSYS;
1419 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1420 mutex_unlock(&dev->lock);
1422 dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1423 (unsigned long)vma->vm_start,
1424 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1425 ret);
1427 return ret;
1430 static struct v4l2_file_operations viu_fops = {
1431 .owner = THIS_MODULE,
1432 .open = viu_open,
1433 .release = viu_release,
1434 .read = viu_read,
1435 .poll = viu_poll,
1436 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1437 .mmap = viu_mmap,
1440 static const struct v4l2_ioctl_ops viu_ioctl_ops = {
1441 .vidioc_querycap = vidioc_querycap,
1442 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1443 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_cap,
1444 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_cap,
1445 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_cap,
1446 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt,
1447 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_overlay,
1448 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_overlay,
1449 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_overlay,
1450 .vidioc_overlay = vidioc_overlay,
1451 .vidioc_g_fbuf = vidioc_g_fbuf,
1452 .vidioc_s_fbuf = vidioc_s_fbuf,
1453 .vidioc_reqbufs = vidioc_reqbufs,
1454 .vidioc_querybuf = vidioc_querybuf,
1455 .vidioc_qbuf = vidioc_qbuf,
1456 .vidioc_dqbuf = vidioc_dqbuf,
1457 .vidioc_g_std = vidioc_g_std,
1458 .vidioc_s_std = vidioc_s_std,
1459 .vidioc_querystd = vidioc_querystd,
1460 .vidioc_enum_input = vidioc_enum_input,
1461 .vidioc_g_input = vidioc_g_input,
1462 .vidioc_s_input = vidioc_s_input,
1463 .vidioc_queryctrl = vidioc_queryctrl,
1464 .vidioc_g_ctrl = vidioc_g_ctrl,
1465 .vidioc_s_ctrl = vidioc_s_ctrl,
1466 .vidioc_streamon = vidioc_streamon,
1467 .vidioc_streamoff = vidioc_streamoff,
1470 static struct video_device viu_template = {
1471 .name = "FSL viu",
1472 .fops = &viu_fops,
1473 .minor = -1,
1474 .ioctl_ops = &viu_ioctl_ops,
1475 .release = video_device_release,
1477 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1478 .current_norm = V4L2_STD_NTSC_M,
1481 static int viu_of_probe(struct platform_device *op)
1483 struct viu_dev *viu_dev;
1484 struct video_device *vdev;
1485 struct resource r;
1486 struct viu_reg __iomem *viu_regs;
1487 struct i2c_adapter *ad;
1488 int ret, viu_irq;
1490 ret = of_address_to_resource(op->dev.of_node, 0, &r);
1491 if (ret) {
1492 dev_err(&op->dev, "Can't parse device node resource\n");
1493 return -ENODEV;
1496 viu_irq = irq_of_parse_and_map(op->dev.of_node, 0);
1497 if (viu_irq == NO_IRQ) {
1498 dev_err(&op->dev, "Error while mapping the irq\n");
1499 return -EINVAL;
1502 /* request mem region */
1503 if (!devm_request_mem_region(&op->dev, r.start,
1504 sizeof(struct viu_reg), DRV_NAME)) {
1505 dev_err(&op->dev, "Error while requesting mem region\n");
1506 ret = -EBUSY;
1507 goto err;
1510 /* remap registers */
1511 viu_regs = devm_ioremap(&op->dev, r.start, sizeof(struct viu_reg));
1512 if (!viu_regs) {
1513 dev_err(&op->dev, "Can't map register set\n");
1514 ret = -ENOMEM;
1515 goto err;
1518 /* Prepare our private structure */
1519 viu_dev = devm_kzalloc(&op->dev, sizeof(struct viu_dev), GFP_ATOMIC);
1520 if (!viu_dev) {
1521 dev_err(&op->dev, "Can't allocate private structure\n");
1522 ret = -ENOMEM;
1523 goto err;
1526 viu_dev->vr = viu_regs;
1527 viu_dev->irq = viu_irq;
1528 viu_dev->dev = &op->dev;
1530 /* init video dma queues */
1531 INIT_LIST_HEAD(&viu_dev->vidq.active);
1532 INIT_LIST_HEAD(&viu_dev->vidq.queued);
1534 snprintf(viu_dev->v4l2_dev.name,
1535 sizeof(viu_dev->v4l2_dev.name), "%s", "VIU");
1536 ret = v4l2_device_register(viu_dev->dev, &viu_dev->v4l2_dev);
1537 if (ret < 0) {
1538 dev_err(&op->dev, "v4l2_device_register() failed: %d\n", ret);
1539 goto err;
1542 ad = i2c_get_adapter(0);
1543 viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
1544 "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
1546 viu_dev->vidq.timeout.function = viu_vid_timeout;
1547 viu_dev->vidq.timeout.data = (unsigned long)viu_dev;
1548 init_timer(&viu_dev->vidq.timeout);
1549 viu_dev->first = 1;
1551 /* Allocate memory for video device */
1552 vdev = video_device_alloc();
1553 if (vdev == NULL) {
1554 ret = -ENOMEM;
1555 goto err_vdev;
1558 memcpy(vdev, &viu_template, sizeof(viu_template));
1560 vdev->v4l2_dev = &viu_dev->v4l2_dev;
1562 viu_dev->vdev = vdev;
1564 /* initialize locks */
1565 mutex_init(&viu_dev->lock);
1566 viu_dev->vdev->lock = &viu_dev->lock;
1567 spin_lock_init(&viu_dev->slock);
1569 video_set_drvdata(viu_dev->vdev, viu_dev);
1571 mutex_lock(&viu_dev->lock);
1573 ret = video_register_device(viu_dev->vdev, VFL_TYPE_GRABBER, -1);
1574 if (ret < 0) {
1575 video_device_release(viu_dev->vdev);
1576 goto err_vdev;
1579 /* enable VIU clock */
1580 viu_dev->clk = clk_get(&op->dev, "viu_clk");
1581 if (IS_ERR(viu_dev->clk)) {
1582 dev_err(&op->dev, "failed to find the clock module!\n");
1583 ret = -ENODEV;
1584 goto err_clk;
1585 } else {
1586 clk_enable(viu_dev->clk);
1589 /* reset VIU module */
1590 viu_reset(viu_dev->vr);
1592 /* install interrupt handler */
1593 if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
1594 dev_err(&op->dev, "Request VIU IRQ failed.\n");
1595 ret = -ENODEV;
1596 goto err_irq;
1599 mutex_unlock(&viu_dev->lock);
1601 dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
1602 return ret;
1604 err_irq:
1605 clk_disable(viu_dev->clk);
1606 clk_put(viu_dev->clk);
1607 err_clk:
1608 video_unregister_device(viu_dev->vdev);
1609 err_vdev:
1610 mutex_unlock(&viu_dev->lock);
1611 i2c_put_adapter(ad);
1612 v4l2_device_unregister(&viu_dev->v4l2_dev);
1613 err:
1614 irq_dispose_mapping(viu_irq);
1615 return ret;
1618 static int viu_of_remove(struct platform_device *op)
1620 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1621 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1622 struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
1623 struct v4l2_subdev, list);
1624 struct i2c_client *client = v4l2_get_subdevdata(sdev);
1626 free_irq(dev->irq, (void *)dev);
1627 irq_dispose_mapping(dev->irq);
1629 clk_disable(dev->clk);
1630 clk_put(dev->clk);
1632 video_unregister_device(dev->vdev);
1633 i2c_put_adapter(client->adapter);
1634 v4l2_device_unregister(&dev->v4l2_dev);
1635 return 0;
1638 #ifdef CONFIG_PM
1639 static int viu_suspend(struct platform_device *op, pm_message_t state)
1641 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1642 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1644 clk_disable(dev->clk);
1645 return 0;
1648 static int viu_resume(struct platform_device *op)
1650 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1651 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1653 clk_enable(dev->clk);
1654 return 0;
1656 #endif
1659 * Initialization and module stuff
1661 static struct of_device_id mpc512x_viu_of_match[] = {
1663 .compatible = "fsl,mpc5121-viu",
1667 MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
1669 static struct platform_driver viu_of_platform_driver = {
1670 .probe = viu_of_probe,
1671 .remove = viu_of_remove,
1672 #ifdef CONFIG_PM
1673 .suspend = viu_suspend,
1674 .resume = viu_resume,
1675 #endif
1676 .driver = {
1677 .name = DRV_NAME,
1678 .owner = THIS_MODULE,
1679 .of_match_table = mpc512x_viu_of_match,
1683 module_platform_driver(viu_of_platform_driver);
1685 MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1686 MODULE_AUTHOR("Hongjun Chen");
1687 MODULE_LICENSE("GPL");
1688 MODULE_VERSION(VIU_VERSION);