SCSI: mpt2sas: _scsih_smart_predicted_fault uses GFP_KERNEL in interrupt context
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / fsl-viu.c
blob908d7012c3f23652507adbc77cf8f2a5729428f8
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 <linux/version.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/videobuf-dma-contig.h>
32 #define DRV_NAME "fsl_viu"
33 #define VIU_MAJOR_VERSION 0
34 #define VIU_MINOR_VERSION 5
35 #define VIU_RELEASE 0
36 #define VIU_VERSION KERNEL_VERSION(VIU_MAJOR_VERSION, \
37 VIU_MINOR_VERSION, \
38 VIU_RELEASE)
40 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
42 #define VIU_VID_MEM_LIMIT 4 /* Video memory limit, in Mb */
44 /* I2C address of video decoder chip is 0x4A */
45 #define VIU_VIDEO_DECODER_ADDR 0x25
47 /* supported controls */
48 static struct v4l2_queryctrl viu_qctrl[] = {
50 .id = V4L2_CID_BRIGHTNESS,
51 .type = V4L2_CTRL_TYPE_INTEGER,
52 .name = "Brightness",
53 .minimum = 0,
54 .maximum = 255,
55 .step = 1,
56 .default_value = 127,
57 .flags = 0,
58 }, {
59 .id = V4L2_CID_CONTRAST,
60 .type = V4L2_CTRL_TYPE_INTEGER,
61 .name = "Contrast",
62 .minimum = 0,
63 .maximum = 255,
64 .step = 0x1,
65 .default_value = 0x10,
66 .flags = 0,
67 }, {
68 .id = V4L2_CID_SATURATION,
69 .type = V4L2_CTRL_TYPE_INTEGER,
70 .name = "Saturation",
71 .minimum = 0,
72 .maximum = 255,
73 .step = 0x1,
74 .default_value = 127,
75 .flags = 0,
76 }, {
77 .id = V4L2_CID_HUE,
78 .type = V4L2_CTRL_TYPE_INTEGER,
79 .name = "Hue",
80 .minimum = -128,
81 .maximum = 127,
82 .step = 0x1,
83 .default_value = 0,
84 .flags = 0,
88 static int qctl_regs[ARRAY_SIZE(viu_qctrl)];
90 static int info_level;
92 #define dprintk(level, fmt, arg...) \
93 do { \
94 if (level <= info_level) \
95 printk(KERN_DEBUG "viu: " fmt , ## arg); \
96 } while (0)
99 * Basic structures
101 struct viu_fmt {
102 char name[32];
103 u32 fourcc; /* v4l2 format id */
104 u32 pixelformat;
105 int depth;
108 static struct viu_fmt formats[] = {
110 .name = "RGB-16 (5/B-6/G-5/R)",
111 .fourcc = V4L2_PIX_FMT_RGB565,
112 .pixelformat = V4L2_PIX_FMT_RGB565,
113 .depth = 16,
114 }, {
115 .name = "RGB-32 (A-R-G-B)",
116 .fourcc = V4L2_PIX_FMT_RGB32,
117 .pixelformat = V4L2_PIX_FMT_RGB32,
118 .depth = 32,
122 struct viu_dev;
123 struct viu_buf;
125 /* buffer for one video frame */
126 struct viu_buf {
127 /* common v4l buffer stuff -- must be first */
128 struct videobuf_buffer vb;
129 struct viu_fmt *fmt;
132 struct viu_dmaqueue {
133 struct viu_dev *dev;
134 struct list_head active;
135 struct list_head queued;
136 struct timer_list timeout;
139 struct viu_status {
140 u32 field_irq;
141 u32 vsync_irq;
142 u32 hsync_irq;
143 u32 vstart_irq;
144 u32 dma_end_irq;
145 u32 error_irq;
148 struct viu_reg {
149 u32 status_cfg;
150 u32 luminance;
151 u32 chroma_r;
152 u32 chroma_g;
153 u32 chroma_b;
154 u32 field_base_addr;
155 u32 dma_inc;
156 u32 picture_count;
157 u32 req_alarm;
158 u32 alpha;
159 } __attribute__ ((packed));
161 struct viu_dev {
162 struct v4l2_device v4l2_dev;
163 struct mutex lock;
164 spinlock_t slock;
165 int users;
167 struct device *dev;
168 /* various device info */
169 struct video_device *vdev;
170 struct viu_dmaqueue vidq;
171 enum v4l2_field capfield;
172 int field;
173 int first;
174 int dma_done;
176 /* Hardware register area */
177 struct viu_reg *vr;
179 /* Interrupt vector */
180 int irq;
181 struct viu_status irqs;
183 /* video overlay */
184 struct v4l2_framebuffer ovbuf;
185 struct viu_fmt *ovfmt;
186 unsigned int ovenable;
187 enum v4l2_field ovfield;
189 /* crop */
190 struct v4l2_rect crop_current;
192 /* clock pointer */
193 struct clk *clk;
195 /* decoder */
196 struct v4l2_subdev *decoder;
198 v4l2_std_id std;
201 struct viu_fh {
202 struct viu_dev *dev;
204 /* video capture */
205 struct videobuf_queue vb_vidq;
206 spinlock_t vbq_lock; /* spinlock for the videobuf queue */
208 /* video overlay */
209 struct v4l2_window win;
210 struct v4l2_clip clips[1];
212 /* video capture */
213 struct viu_fmt *fmt;
214 int width, height, sizeimage;
215 enum v4l2_buf_type type;
218 static struct viu_reg reg_val;
221 * Macro definitions of VIU registers
224 /* STATUS_CONFIG register */
225 enum status_config {
226 SOFT_RST = 1 << 0,
228 ERR_MASK = 0x0f << 4, /* Error code mask */
229 ERR_NO = 0x00, /* No error */
230 ERR_DMA_V = 0x01 << 4, /* DMA in vertical active */
231 ERR_DMA_VB = 0x02 << 4, /* DMA in vertical blanking */
232 ERR_LINE_TOO_LONG = 0x04 << 4, /* Line too long */
233 ERR_TOO_MANG_LINES = 0x05 << 4, /* Too many lines in field */
234 ERR_LINE_TOO_SHORT = 0x06 << 4, /* Line too short */
235 ERR_NOT_ENOUGH_LINE = 0x07 << 4, /* Not enough lines in field */
236 ERR_FIFO_OVERFLOW = 0x08 << 4, /* FIFO overflow */
237 ERR_FIFO_UNDERFLOW = 0x09 << 4, /* FIFO underflow */
238 ERR_1bit_ECC = 0x0a << 4, /* One bit ECC error */
239 ERR_MORE_ECC = 0x0b << 4, /* Two/more bits ECC error */
241 INT_FIELD_EN = 0x01 << 8, /* Enable field interrupt */
242 INT_VSYNC_EN = 0x01 << 9, /* Enable vsync interrupt */
243 INT_HSYNC_EN = 0x01 << 10, /* Enable hsync interrupt */
244 INT_VSTART_EN = 0x01 << 11, /* Enable vstart interrupt */
245 INT_DMA_END_EN = 0x01 << 12, /* Enable DMA end interrupt */
246 INT_ERROR_EN = 0x01 << 13, /* Enable error interrupt */
247 INT_ECC_EN = 0x01 << 14, /* Enable ECC interrupt */
249 INT_FIELD_STATUS = 0x01 << 16, /* field interrupt status */
250 INT_VSYNC_STATUS = 0x01 << 17, /* vsync interrupt status */
251 INT_HSYNC_STATUS = 0x01 << 18, /* hsync interrupt status */
252 INT_VSTART_STATUS = 0x01 << 19, /* vstart interrupt status */
253 INT_DMA_END_STATUS = 0x01 << 20, /* DMA end interrupt status */
254 INT_ERROR_STATUS = 0x01 << 21, /* error interrupt status */
256 DMA_ACT = 0x01 << 27, /* Enable DMA transfer */
257 FIELD_NO = 0x01 << 28, /* Field number */
258 DITHER_ON = 0x01 << 29, /* Dithering is on */
259 ROUND_ON = 0x01 << 30, /* Round is on */
260 MODE_32BIT = 0x01 << 31, /* Data in RGBa888,
261 * 0 in RGB565
265 #define norm_maxw() 720
266 #define norm_maxh() 576
268 #define INT_ALL_STATUS (INT_FIELD_STATUS | INT_VSYNC_STATUS | \
269 INT_HSYNC_STATUS | INT_VSTART_STATUS | \
270 INT_DMA_END_STATUS | INT_ERROR_STATUS)
272 #define NUM_FORMATS ARRAY_SIZE(formats)
274 static irqreturn_t viu_intr(int irq, void *dev_id);
276 struct viu_fmt *format_by_fourcc(int fourcc)
278 int i;
280 for (i = 0; i < NUM_FORMATS; i++) {
281 if (formats[i].pixelformat == fourcc)
282 return formats + i;
285 dprintk(0, "unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
286 return NULL;
289 void viu_start_dma(struct viu_dev *dev)
291 struct viu_reg *vr = dev->vr;
293 dev->field = 0;
295 /* Enable DMA operation */
296 out_be32(&vr->status_cfg, SOFT_RST);
297 out_be32(&vr->status_cfg, INT_FIELD_EN);
300 void viu_stop_dma(struct viu_dev *dev)
302 struct viu_reg *vr = dev->vr;
303 int cnt = 100;
304 u32 status_cfg;
306 out_be32(&vr->status_cfg, 0);
308 /* Clear pending interrupts */
309 status_cfg = in_be32(&vr->status_cfg);
310 if (status_cfg & 0x3f0000)
311 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
313 if (status_cfg & DMA_ACT) {
314 do {
315 status_cfg = in_be32(&vr->status_cfg);
316 if (status_cfg & INT_DMA_END_STATUS)
317 break;
318 } while (cnt--);
320 if (cnt < 0) {
321 /* timed out, issue soft reset */
322 out_be32(&vr->status_cfg, SOFT_RST);
323 out_be32(&vr->status_cfg, 0);
324 } else {
325 /* clear DMA_END and other pending irqs */
326 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
330 dev->field = 0;
333 static int restart_video_queue(struct viu_dmaqueue *vidq)
335 struct viu_buf *buf, *prev;
337 dprintk(1, "%s vidq=0x%08lx\n", __func__, (unsigned long)vidq);
338 if (!list_empty(&vidq->active)) {
339 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
340 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
341 buf, buf->vb.i);
343 viu_stop_dma(vidq->dev);
345 /* cancel all outstanding capture requests */
346 list_for_each_entry_safe(buf, prev, &vidq->active, vb.queue) {
347 list_del(&buf->vb.queue);
348 buf->vb.state = VIDEOBUF_ERROR;
349 wake_up(&buf->vb.done);
351 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
352 return 0;
355 prev = NULL;
356 for (;;) {
357 if (list_empty(&vidq->queued))
358 return 0;
359 buf = list_entry(vidq->queued.next, struct viu_buf, vb.queue);
360 if (prev == NULL) {
361 list_del(&buf->vb.queue);
362 list_add_tail(&buf->vb.queue, &vidq->active);
364 dprintk(1, "Restarting video dma\n");
365 viu_stop_dma(vidq->dev);
366 viu_start_dma(vidq->dev);
368 buf->vb.state = VIDEOBUF_ACTIVE;
369 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
370 dprintk(2, "[%p/%d] restart_queue - first active\n",
371 buf, buf->vb.i);
373 } else if (prev->vb.width == buf->vb.width &&
374 prev->vb.height == buf->vb.height &&
375 prev->fmt == buf->fmt) {
376 list_del(&buf->vb.queue);
377 list_add_tail(&buf->vb.queue, &vidq->active);
378 buf->vb.state = VIDEOBUF_ACTIVE;
379 dprintk(2, "[%p/%d] restart_queue - move to active\n",
380 buf, buf->vb.i);
381 } else {
382 return 0;
384 prev = buf;
388 static void viu_vid_timeout(unsigned long data)
390 struct viu_dev *dev = (struct viu_dev *)data;
391 struct viu_buf *buf;
392 struct viu_dmaqueue *vidq = &dev->vidq;
394 while (!list_empty(&vidq->active)) {
395 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
396 list_del(&buf->vb.queue);
397 buf->vb.state = VIDEOBUF_ERROR;
398 wake_up(&buf->vb.done);
399 dprintk(1, "viu/0: [%p/%d] timeout\n", buf, buf->vb.i);
402 restart_video_queue(vidq);
406 * Videobuf operations
408 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
409 unsigned int *size)
411 struct viu_fh *fh = vq->priv_data;
413 *size = fh->width * fh->height * fh->fmt->depth >> 3;
414 if (*count == 0)
415 *count = 32;
417 while (*size * *count > VIU_VID_MEM_LIMIT * 1024 * 1024)
418 (*count)--;
420 dprintk(1, "%s, count=%d, size=%d\n", __func__, *count, *size);
421 return 0;
424 static void free_buffer(struct videobuf_queue *vq, struct viu_buf *buf)
426 struct videobuf_buffer *vb = &buf->vb;
427 void *vaddr = NULL;
429 BUG_ON(in_interrupt());
431 videobuf_waiton(vq, &buf->vb, 0, 0);
433 if (vq->int_ops && vq->int_ops->vaddr)
434 vaddr = vq->int_ops->vaddr(vb);
436 if (vaddr)
437 videobuf_dma_contig_free(vq, &buf->vb);
439 buf->vb.state = VIDEOBUF_NEEDS_INIT;
442 inline int buffer_activate(struct viu_dev *dev, struct viu_buf *buf)
444 struct viu_reg *vr = dev->vr;
445 int bpp;
447 /* setup the DMA base address */
448 reg_val.field_base_addr = videobuf_to_dma_contig(&buf->vb);
450 dprintk(1, "buffer_activate [%p/%d]: dma addr 0x%lx\n",
451 buf, buf->vb.i, (unsigned long)reg_val.field_base_addr);
453 /* interlace is on by default, set horizontal DMA increment */
454 reg_val.status_cfg = 0;
455 bpp = buf->fmt->depth >> 3;
456 switch (bpp) {
457 case 2:
458 reg_val.status_cfg &= ~MODE_32BIT;
459 reg_val.dma_inc = buf->vb.width * 2;
460 break;
461 case 4:
462 reg_val.status_cfg |= MODE_32BIT;
463 reg_val.dma_inc = buf->vb.width * 4;
464 break;
465 default:
466 dprintk(0, "doesn't support color depth(%d)\n",
467 bpp * 8);
468 return -EINVAL;
471 /* setup picture_count register */
472 reg_val.picture_count = (buf->vb.height / 2) << 16 |
473 buf->vb.width;
475 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
477 buf->vb.state = VIDEOBUF_ACTIVE;
478 dev->capfield = buf->vb.field;
480 /* reset dma increment if needed */
481 if (!V4L2_FIELD_HAS_BOTH(buf->vb.field))
482 reg_val.dma_inc = 0;
484 out_be32(&vr->dma_inc, reg_val.dma_inc);
485 out_be32(&vr->picture_count, reg_val.picture_count);
486 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
487 mod_timer(&dev->vidq.timeout, jiffies + BUFFER_TIMEOUT);
488 return 0;
491 static int buffer_prepare(struct videobuf_queue *vq,
492 struct videobuf_buffer *vb,
493 enum v4l2_field field)
495 struct viu_fh *fh = vq->priv_data;
496 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
497 int rc;
499 BUG_ON(fh->fmt == NULL);
501 if (fh->width < 48 || fh->width > norm_maxw() ||
502 fh->height < 32 || fh->height > norm_maxh())
503 return -EINVAL;
504 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
505 if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size)
506 return -EINVAL;
508 if (buf->fmt != fh->fmt ||
509 buf->vb.width != fh->width ||
510 buf->vb.height != fh->height ||
511 buf->vb.field != field) {
512 buf->fmt = fh->fmt;
513 buf->vb.width = fh->width;
514 buf->vb.height = fh->height;
515 buf->vb.field = field;
518 if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
519 rc = videobuf_iolock(vq, &buf->vb, NULL);
520 if (rc != 0)
521 goto fail;
523 buf->vb.width = fh->width;
524 buf->vb.height = fh->height;
525 buf->vb.field = field;
526 buf->fmt = fh->fmt;
529 buf->vb.state = VIDEOBUF_PREPARED;
530 return 0;
532 fail:
533 free_buffer(vq, buf);
534 return rc;
537 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
539 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
540 struct viu_fh *fh = vq->priv_data;
541 struct viu_dev *dev = fh->dev;
542 struct viu_dmaqueue *vidq = &dev->vidq;
543 struct viu_buf *prev;
545 if (!list_empty(&vidq->queued)) {
546 dprintk(1, "adding vb queue=0x%08lx\n",
547 (unsigned long)&buf->vb.queue);
548 dprintk(1, "vidq pointer 0x%p, queued 0x%p\n",
549 vidq, &vidq->queued);
550 dprintk(1, "dev %p, queued: self %p, next %p, head %p\n",
551 dev, &vidq->queued, vidq->queued.next,
552 vidq->queued.prev);
553 list_add_tail(&buf->vb.queue, &vidq->queued);
554 buf->vb.state = VIDEOBUF_QUEUED;
555 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
556 buf, buf->vb.i);
557 } else if (list_empty(&vidq->active)) {
558 dprintk(1, "adding vb active=0x%08lx\n",
559 (unsigned long)&buf->vb.queue);
560 list_add_tail(&buf->vb.queue, &vidq->active);
561 buf->vb.state = VIDEOBUF_ACTIVE;
562 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
563 dprintk(2, "[%p/%d] buffer_queue - first active\n",
564 buf, buf->vb.i);
566 buffer_activate(dev, buf);
567 } else {
568 dprintk(1, "adding vb queue2=0x%08lx\n",
569 (unsigned long)&buf->vb.queue);
570 prev = list_entry(vidq->active.prev, struct viu_buf, vb.queue);
571 if (prev->vb.width == buf->vb.width &&
572 prev->vb.height == buf->vb.height &&
573 prev->fmt == buf->fmt) {
574 list_add_tail(&buf->vb.queue, &vidq->active);
575 buf->vb.state = VIDEOBUF_ACTIVE;
576 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
577 buf, buf->vb.i);
578 } else {
579 list_add_tail(&buf->vb.queue, &vidq->queued);
580 buf->vb.state = VIDEOBUF_QUEUED;
581 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
582 buf, buf->vb.i);
587 static void buffer_release(struct videobuf_queue *vq,
588 struct videobuf_buffer *vb)
590 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
591 struct viu_fh *fh = vq->priv_data;
592 struct viu_dev *dev = (struct viu_dev *)fh->dev;
594 viu_stop_dma(dev);
595 free_buffer(vq, buf);
598 static struct videobuf_queue_ops viu_video_qops = {
599 .buf_setup = buffer_setup,
600 .buf_prepare = buffer_prepare,
601 .buf_queue = buffer_queue,
602 .buf_release = buffer_release,
606 * IOCTL vidioc handling
608 static int vidioc_querycap(struct file *file, void *priv,
609 struct v4l2_capability *cap)
611 strcpy(cap->driver, "viu");
612 strcpy(cap->card, "viu");
613 cap->version = VIU_VERSION;
614 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
615 V4L2_CAP_STREAMING |
616 V4L2_CAP_VIDEO_OVERLAY |
617 V4L2_CAP_READWRITE;
618 return 0;
621 static int vidioc_enum_fmt(struct file *file, void *priv,
622 struct v4l2_fmtdesc *f)
624 int index = f->index;
626 if (f->index > NUM_FORMATS)
627 return -EINVAL;
629 strlcpy(f->description, formats[index].name, sizeof(f->description));
630 f->pixelformat = formats[index].fourcc;
631 return 0;
634 static int vidioc_g_fmt_cap(struct file *file, void *priv,
635 struct v4l2_format *f)
637 struct viu_fh *fh = priv;
639 f->fmt.pix.width = fh->width;
640 f->fmt.pix.height = fh->height;
641 f->fmt.pix.field = fh->vb_vidq.field;
642 f->fmt.pix.pixelformat = fh->fmt->pixelformat;
643 f->fmt.pix.bytesperline =
644 (f->fmt.pix.width * fh->fmt->depth) >> 3;
645 f->fmt.pix.sizeimage = fh->sizeimage;
646 return 0;
649 static int vidioc_try_fmt_cap(struct file *file, void *priv,
650 struct v4l2_format *f)
652 struct viu_fmt *fmt;
653 enum v4l2_field field;
654 unsigned int maxw, maxh;
656 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
657 if (!fmt) {
658 dprintk(1, "Fourcc format (0x%08x) invalid.",
659 f->fmt.pix.pixelformat);
660 return -EINVAL;
663 field = f->fmt.pix.field;
665 if (field == V4L2_FIELD_ANY) {
666 field = V4L2_FIELD_INTERLACED;
667 } else if (field != V4L2_FIELD_INTERLACED) {
668 dprintk(1, "Field type invalid.\n");
669 return -EINVAL;
672 maxw = norm_maxw();
673 maxh = norm_maxh();
675 f->fmt.pix.field = field;
676 if (f->fmt.pix.height < 32)
677 f->fmt.pix.height = 32;
678 if (f->fmt.pix.height > maxh)
679 f->fmt.pix.height = maxh;
680 if (f->fmt.pix.width < 48)
681 f->fmt.pix.width = 48;
682 if (f->fmt.pix.width > maxw)
683 f->fmt.pix.width = maxw;
684 f->fmt.pix.width &= ~0x03;
685 f->fmt.pix.bytesperline =
686 (f->fmt.pix.width * fmt->depth) >> 3;
688 return 0;
691 static int vidioc_s_fmt_cap(struct file *file, void *priv,
692 struct v4l2_format *f)
694 struct viu_fh *fh = priv;
695 int ret;
697 ret = vidioc_try_fmt_cap(file, fh, f);
698 if (ret < 0)
699 return ret;
701 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
702 fh->width = f->fmt.pix.width;
703 fh->height = f->fmt.pix.height;
704 fh->sizeimage = f->fmt.pix.sizeimage;
705 fh->vb_vidq.field = f->fmt.pix.field;
706 fh->type = f->type;
707 dprintk(1, "set to pixelformat '%4.6s'\n", (char *)&fh->fmt->name);
708 return 0;
711 static int vidioc_g_fmt_overlay(struct file *file, void *priv,
712 struct v4l2_format *f)
714 struct viu_fh *fh = priv;
716 f->fmt.win = fh->win;
717 return 0;
720 static int verify_preview(struct viu_dev *dev, struct v4l2_window *win)
722 enum v4l2_field field;
723 int maxw, maxh;
725 if (dev->ovbuf.base == NULL)
726 return -EINVAL;
727 if (dev->ovfmt == NULL)
728 return -EINVAL;
729 if (win->w.width < 48 || win->w.height < 32)
730 return -EINVAL;
732 field = win->field;
733 maxw = dev->crop_current.width;
734 maxh = dev->crop_current.height;
736 if (field == V4L2_FIELD_ANY) {
737 field = (win->w.height > maxh/2)
738 ? V4L2_FIELD_INTERLACED
739 : V4L2_FIELD_TOP;
741 switch (field) {
742 case V4L2_FIELD_TOP:
743 case V4L2_FIELD_BOTTOM:
744 maxh = maxh / 2;
745 break;
746 case V4L2_FIELD_INTERLACED:
747 break;
748 default:
749 return -EINVAL;
752 win->field = field;
753 if (win->w.width > maxw)
754 win->w.width = maxw;
755 if (win->w.height > maxh)
756 win->w.height = maxh;
757 return 0;
760 inline void viu_activate_overlay(struct viu_reg *viu_reg)
762 struct viu_reg *vr = viu_reg;
764 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
765 out_be32(&vr->dma_inc, reg_val.dma_inc);
766 out_be32(&vr->picture_count, reg_val.picture_count);
769 static int viu_setup_preview(struct viu_dev *dev, struct viu_fh *fh)
771 int bpp;
773 dprintk(1, "%s %dx%d %s\n", __func__,
774 fh->win.w.width, fh->win.w.height, dev->ovfmt->name);
776 reg_val.status_cfg = 0;
778 /* setup window */
779 reg_val.picture_count = (fh->win.w.height / 2) << 16 |
780 fh->win.w.width;
782 /* setup color depth and dma increment */
783 bpp = dev->ovfmt->depth / 8;
784 switch (bpp) {
785 case 2:
786 reg_val.status_cfg &= ~MODE_32BIT;
787 reg_val.dma_inc = fh->win.w.width * 2;
788 break;
789 case 4:
790 reg_val.status_cfg |= MODE_32BIT;
791 reg_val.dma_inc = fh->win.w.width * 4;
792 break;
793 default:
794 dprintk(0, "device doesn't support color depth(%d)\n",
795 bpp * 8);
796 return -EINVAL;
799 dev->ovfield = fh->win.field;
800 if (!V4L2_FIELD_HAS_BOTH(dev->ovfield))
801 reg_val.dma_inc = 0;
803 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
805 /* setup the base address of the overlay buffer */
806 reg_val.field_base_addr = (u32)dev->ovbuf.base;
808 return 0;
811 static int vidioc_s_fmt_overlay(struct file *file, void *priv,
812 struct v4l2_format *f)
814 struct viu_fh *fh = priv;
815 struct viu_dev *dev = (struct viu_dev *)fh->dev;
816 unsigned long flags;
817 int err;
819 err = verify_preview(dev, &f->fmt.win);
820 if (err)
821 return err;
823 fh->win = f->fmt.win;
825 spin_lock_irqsave(&dev->slock, flags);
826 viu_setup_preview(dev, fh);
827 spin_unlock_irqrestore(&dev->slock, flags);
828 return 0;
831 static int vidioc_try_fmt_overlay(struct file *file, void *priv,
832 struct v4l2_format *f)
834 return 0;
837 static int vidioc_overlay(struct file *file, void *priv, unsigned int on)
839 struct viu_fh *fh = priv;
840 struct viu_dev *dev = (struct viu_dev *)fh->dev;
841 unsigned long flags;
843 if (on) {
844 spin_lock_irqsave(&dev->slock, flags);
845 viu_activate_overlay(dev->vr);
846 dev->ovenable = 1;
848 /* start dma */
849 viu_start_dma(dev);
850 spin_unlock_irqrestore(&dev->slock, flags);
851 } else {
852 viu_stop_dma(dev);
853 dev->ovenable = 0;
856 return 0;
859 int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
861 struct viu_fh *fh = priv;
862 struct viu_dev *dev = fh->dev;
863 struct v4l2_framebuffer *fb = arg;
865 *fb = dev->ovbuf;
866 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
867 return 0;
870 int vidioc_s_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
872 struct viu_fh *fh = priv;
873 struct viu_dev *dev = fh->dev;
874 struct v4l2_framebuffer *fb = arg;
875 struct viu_fmt *fmt;
877 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
878 return -EPERM;
880 /* check args */
881 fmt = format_by_fourcc(fb->fmt.pixelformat);
882 if (fmt == NULL)
883 return -EINVAL;
885 /* ok, accept it */
886 dev->ovbuf = *fb;
887 dev->ovfmt = fmt;
888 if (dev->ovbuf.fmt.bytesperline == 0) {
889 dev->ovbuf.fmt.bytesperline =
890 dev->ovbuf.fmt.width * fmt->depth / 8;
892 return 0;
895 static int vidioc_reqbufs(struct file *file, void *priv,
896 struct v4l2_requestbuffers *p)
898 struct viu_fh *fh = priv;
900 return videobuf_reqbufs(&fh->vb_vidq, p);
903 static int vidioc_querybuf(struct file *file, void *priv,
904 struct v4l2_buffer *p)
906 struct viu_fh *fh = priv;
908 return videobuf_querybuf(&fh->vb_vidq, p);
911 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
913 struct viu_fh *fh = priv;
915 return videobuf_qbuf(&fh->vb_vidq, p);
918 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
920 struct viu_fh *fh = priv;
922 return videobuf_dqbuf(&fh->vb_vidq, p,
923 file->f_flags & O_NONBLOCK);
926 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
928 struct viu_fh *fh = priv;
929 struct viu_dev *dev = fh->dev;
931 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
932 return -EINVAL;
933 if (fh->type != i)
934 return -EINVAL;
936 if (dev->ovenable)
937 dev->ovenable = 0;
939 viu_start_dma(fh->dev);
941 return videobuf_streamon(&fh->vb_vidq);
944 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
946 struct viu_fh *fh = priv;
948 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
949 return -EINVAL;
950 if (fh->type != i)
951 return -EINVAL;
953 viu_stop_dma(fh->dev);
955 return videobuf_streamoff(&fh->vb_vidq);
958 #define decoder_call(viu, o, f, args...) \
959 v4l2_subdev_call(viu->decoder, o, f, ##args)
961 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
963 struct viu_fh *fh = priv;
965 decoder_call(fh->dev, video, querystd, std_id);
966 return 0;
969 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
971 struct viu_fh *fh = priv;
973 fh->dev->std = *id;
974 decoder_call(fh->dev, core, s_std, *id);
975 return 0;
978 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
980 struct viu_fh *fh = priv;
982 *std_id = fh->dev->std;
983 return 0;
986 /* only one input in this driver */
987 static int vidioc_enum_input(struct file *file, void *priv,
988 struct v4l2_input *inp)
990 struct viu_fh *fh = priv;
992 if (inp->index != 0)
993 return -EINVAL;
995 inp->type = V4L2_INPUT_TYPE_CAMERA;
996 inp->std = fh->dev->vdev->tvnorms;
997 strcpy(inp->name, "Camera");
998 return 0;
1001 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1003 *i = 0;
1004 return 0;
1007 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1009 struct viu_fh *fh = priv;
1011 if (i > 1)
1012 return -EINVAL;
1014 decoder_call(fh->dev, video, s_routing, i, 0, 0);
1015 return 0;
1018 /* Controls */
1019 static int vidioc_queryctrl(struct file *file, void *priv,
1020 struct v4l2_queryctrl *qc)
1022 int i;
1024 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1025 if (qc->id && qc->id == viu_qctrl[i].id) {
1026 memcpy(qc, &(viu_qctrl[i]), sizeof(*qc));
1027 return 0;
1030 return -EINVAL;
1033 static int vidioc_g_ctrl(struct file *file, void *priv,
1034 struct v4l2_control *ctrl)
1036 int i;
1038 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1039 if (ctrl->id == viu_qctrl[i].id) {
1040 ctrl->value = qctl_regs[i];
1041 return 0;
1044 return -EINVAL;
1046 static int vidioc_s_ctrl(struct file *file, void *priv,
1047 struct v4l2_control *ctrl)
1049 int i;
1051 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1052 if (ctrl->id == viu_qctrl[i].id) {
1053 if (ctrl->value < viu_qctrl[i].minimum
1054 || ctrl->value > viu_qctrl[i].maximum)
1055 return -ERANGE;
1056 qctl_regs[i] = ctrl->value;
1057 return 0;
1060 return -EINVAL;
1063 inline void viu_activate_next_buf(struct viu_dev *dev,
1064 struct viu_dmaqueue *viuq)
1066 struct viu_dmaqueue *vidq = viuq;
1067 struct viu_buf *buf;
1069 /* launch another DMA operation for an active/queued buffer */
1070 if (!list_empty(&vidq->active)) {
1071 buf = list_entry(vidq->active.next, struct viu_buf,
1072 vb.queue);
1073 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1074 buffer_activate(dev, buf);
1075 } else if (!list_empty(&vidq->queued)) {
1076 buf = list_entry(vidq->queued.next, struct viu_buf,
1077 vb.queue);
1078 list_del(&buf->vb.queue);
1080 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1081 list_add_tail(&buf->vb.queue, &vidq->active);
1082 buf->vb.state = VIDEOBUF_ACTIVE;
1083 buffer_activate(dev, buf);
1087 inline void viu_default_settings(struct viu_reg *viu_reg)
1089 struct viu_reg *vr = viu_reg;
1091 out_be32(&vr->luminance, 0x9512A254);
1092 out_be32(&vr->chroma_r, 0x03310000);
1093 out_be32(&vr->chroma_g, 0x06600F38);
1094 out_be32(&vr->chroma_b, 0x00000409);
1095 out_be32(&vr->alpha, 0x000000ff);
1096 out_be32(&vr->req_alarm, 0x00000090);
1097 dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
1098 in_be32(&vr->status_cfg), in_be32(&vr->field_base_addr));
1101 static void viu_overlay_intr(struct viu_dev *dev, u32 status)
1103 struct viu_reg *vr = dev->vr;
1105 if (status & INT_DMA_END_STATUS)
1106 dev->dma_done = 1;
1108 if (status & INT_FIELD_STATUS) {
1109 if (dev->dma_done) {
1110 u32 addr = reg_val.field_base_addr;
1112 dev->dma_done = 0;
1113 if (status & FIELD_NO)
1114 addr += reg_val.dma_inc;
1116 out_be32(&vr->field_base_addr, addr);
1117 out_be32(&vr->dma_inc, reg_val.dma_inc);
1118 out_be32(&vr->status_cfg,
1119 (status & 0xffc0ffff) |
1120 (status & INT_ALL_STATUS) |
1121 reg_val.status_cfg);
1122 } else if (status & INT_VSYNC_STATUS) {
1123 out_be32(&vr->status_cfg,
1124 (status & 0xffc0ffff) |
1125 (status & INT_ALL_STATUS) |
1126 reg_val.status_cfg);
1131 static void viu_capture_intr(struct viu_dev *dev, u32 status)
1133 struct viu_dmaqueue *vidq = &dev->vidq;
1134 struct viu_reg *vr = dev->vr;
1135 struct viu_buf *buf;
1136 int field_num;
1137 int need_two;
1138 int dma_done = 0;
1140 field_num = status & FIELD_NO;
1141 need_two = V4L2_FIELD_HAS_BOTH(dev->capfield);
1143 if (status & INT_DMA_END_STATUS) {
1144 dma_done = 1;
1145 if (((field_num == 0) && (dev->field == 0)) ||
1146 (field_num && (dev->field == 1)))
1147 dev->field++;
1150 if (status & INT_FIELD_STATUS) {
1151 dprintk(1, "irq: field %d, done %d\n",
1152 !!field_num, dma_done);
1153 if (unlikely(dev->first)) {
1154 if (field_num == 0) {
1155 dev->first = 0;
1156 dprintk(1, "activate first buf\n");
1157 viu_activate_next_buf(dev, vidq);
1158 } else
1159 dprintk(1, "wait field 0\n");
1160 return;
1163 /* setup buffer address for next dma operation */
1164 if (!list_empty(&vidq->active)) {
1165 u32 addr = reg_val.field_base_addr;
1167 if (field_num && need_two) {
1168 addr += reg_val.dma_inc;
1169 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1170 (unsigned long)addr, dev->field);
1172 out_be32(&vr->field_base_addr, addr);
1173 out_be32(&vr->dma_inc, reg_val.dma_inc);
1174 out_be32(&vr->status_cfg,
1175 (status & 0xffc0ffff) |
1176 (status & INT_ALL_STATUS) |
1177 reg_val.status_cfg);
1178 return;
1182 if (dma_done && field_num && (dev->field == 2)) {
1183 dev->field = 0;
1184 buf = list_entry(vidq->active.next,
1185 struct viu_buf, vb.queue);
1186 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1187 buf, buf->vb.i,
1188 (unsigned long)videobuf_to_dma_contig(&buf->vb),
1189 (unsigned long)in_be32(&vr->field_base_addr));
1191 if (waitqueue_active(&buf->vb.done)) {
1192 list_del(&buf->vb.queue);
1193 do_gettimeofday(&buf->vb.ts);
1194 buf->vb.state = VIDEOBUF_DONE;
1195 buf->vb.field_count++;
1196 wake_up(&buf->vb.done);
1198 /* activate next dma buffer */
1199 viu_activate_next_buf(dev, vidq);
1203 static irqreturn_t viu_intr(int irq, void *dev_id)
1205 struct viu_dev *dev = (struct viu_dev *)dev_id;
1206 struct viu_reg *vr = dev->vr;
1207 u32 status;
1208 u32 error;
1210 status = in_be32(&vr->status_cfg);
1212 if (status & INT_ERROR_STATUS) {
1213 dev->irqs.error_irq++;
1214 error = status & ERR_MASK;
1215 if (error)
1216 dprintk(1, "Err: error(%d), times:%d!\n",
1217 error >> 4, dev->irqs.error_irq);
1218 /* Clear interrupt error bit and error flags */
1219 out_be32(&vr->status_cfg,
1220 (status & 0xffc0ffff) | INT_ERROR_STATUS);
1223 if (status & INT_DMA_END_STATUS) {
1224 dev->irqs.dma_end_irq++;
1225 dev->dma_done = 1;
1226 dprintk(2, "VIU DMA end interrupt times: %d\n",
1227 dev->irqs.dma_end_irq);
1230 if (status & INT_HSYNC_STATUS)
1231 dev->irqs.hsync_irq++;
1233 if (status & INT_FIELD_STATUS) {
1234 dev->irqs.field_irq++;
1235 dprintk(2, "VIU field interrupt times: %d\n",
1236 dev->irqs.field_irq);
1239 if (status & INT_VSTART_STATUS)
1240 dev->irqs.vstart_irq++;
1242 if (status & INT_VSYNC_STATUS) {
1243 dev->irqs.vsync_irq++;
1244 dprintk(2, "VIU vsync interrupt times: %d\n",
1245 dev->irqs.vsync_irq);
1248 /* clear all pending irqs */
1249 status = in_be32(&vr->status_cfg);
1250 out_be32(&vr->status_cfg,
1251 (status & 0xffc0ffff) | (status & INT_ALL_STATUS));
1253 if (dev->ovenable) {
1254 viu_overlay_intr(dev, status);
1255 return IRQ_HANDLED;
1258 /* Capture mode */
1259 viu_capture_intr(dev, status);
1260 return IRQ_HANDLED;
1264 * File operations for the device
1266 static int viu_open(struct file *file)
1268 struct video_device *vdev = video_devdata(file);
1269 struct viu_dev *dev = video_get_drvdata(vdev);
1270 struct viu_fh *fh;
1271 struct viu_reg *vr;
1272 int minor = vdev->minor;
1273 u32 status_cfg;
1274 int i;
1276 dprintk(1, "viu: open (minor=%d)\n", minor);
1278 dev->users++;
1279 if (dev->users > 1) {
1280 dev->users--;
1281 return -EBUSY;
1284 vr = dev->vr;
1286 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1287 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1289 /* allocate and initialize per filehandle data */
1290 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1291 if (!fh) {
1292 dev->users--;
1293 return -ENOMEM;
1296 file->private_data = fh;
1297 fh->dev = dev;
1299 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1300 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_RGB32);
1301 fh->width = norm_maxw();
1302 fh->height = norm_maxh();
1303 dev->crop_current.width = fh->width;
1304 dev->crop_current.height = fh->height;
1306 /* Put all controls at a sane state */
1307 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++)
1308 qctl_regs[i] = viu_qctrl[i].default_value;
1310 dprintk(1, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1311 (unsigned long)fh, (unsigned long)dev,
1312 (unsigned long)&dev->vidq);
1313 dprintk(1, "Open: list_empty queued=%d\n",
1314 list_empty(&dev->vidq.queued));
1315 dprintk(1, "Open: list_empty active=%d\n",
1316 list_empty(&dev->vidq.active));
1318 viu_default_settings(vr);
1320 status_cfg = in_be32(&vr->status_cfg);
1321 out_be32(&vr->status_cfg,
1322 status_cfg & ~(INT_VSYNC_EN | INT_HSYNC_EN |
1323 INT_FIELD_EN | INT_VSTART_EN |
1324 INT_DMA_END_EN | INT_ERROR_EN | INT_ECC_EN));
1326 status_cfg = in_be32(&vr->status_cfg);
1327 out_be32(&vr->status_cfg, status_cfg | INT_ALL_STATUS);
1329 spin_lock_init(&fh->vbq_lock);
1330 videobuf_queue_dma_contig_init(&fh->vb_vidq, &viu_video_qops,
1331 dev->dev, &fh->vbq_lock,
1332 fh->type, V4L2_FIELD_INTERLACED,
1333 sizeof(struct viu_buf), fh,
1334 &fh->dev->lock);
1335 return 0;
1338 static ssize_t viu_read(struct file *file, char __user *data, size_t count,
1339 loff_t *ppos)
1341 struct viu_fh *fh = file->private_data;
1342 struct viu_dev *dev = fh->dev;
1343 int ret = 0;
1345 dprintk(2, "%s\n", __func__);
1346 if (dev->ovenable)
1347 dev->ovenable = 0;
1349 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1350 viu_start_dma(dev);
1351 ret = videobuf_read_stream(&fh->vb_vidq, data, count,
1352 ppos, 0, file->f_flags & O_NONBLOCK);
1353 return ret;
1355 return 0;
1358 static unsigned int viu_poll(struct file *file, struct poll_table_struct *wait)
1360 struct viu_fh *fh = file->private_data;
1361 struct videobuf_queue *q = &fh->vb_vidq;
1363 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1364 return POLLERR;
1366 return videobuf_poll_stream(file, q, wait);
1369 static int viu_release(struct file *file)
1371 struct viu_fh *fh = file->private_data;
1372 struct viu_dev *dev = fh->dev;
1373 int minor = video_devdata(file)->minor;
1375 viu_stop_dma(dev);
1376 videobuf_stop(&fh->vb_vidq);
1377 videobuf_mmap_free(&fh->vb_vidq);
1379 kfree(fh);
1381 dev->users--;
1382 dprintk(1, "close (minor=%d, users=%d)\n",
1383 minor, dev->users);
1384 return 0;
1387 void viu_reset(struct viu_reg *reg)
1389 out_be32(&reg->status_cfg, 0);
1390 out_be32(&reg->luminance, 0x9512a254);
1391 out_be32(&reg->chroma_r, 0x03310000);
1392 out_be32(&reg->chroma_g, 0x06600f38);
1393 out_be32(&reg->chroma_b, 0x00000409);
1394 out_be32(&reg->field_base_addr, 0);
1395 out_be32(&reg->dma_inc, 0);
1396 out_be32(&reg->picture_count, 0x01e002d0);
1397 out_be32(&reg->req_alarm, 0x00000090);
1398 out_be32(&reg->alpha, 0x000000ff);
1401 static int viu_mmap(struct file *file, struct vm_area_struct *vma)
1403 struct viu_fh *fh = file->private_data;
1404 int ret;
1406 dprintk(1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1408 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1410 dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1411 (unsigned long)vma->vm_start,
1412 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1413 ret);
1415 return ret;
1418 static struct v4l2_file_operations viu_fops = {
1419 .owner = THIS_MODULE,
1420 .open = viu_open,
1421 .release = viu_release,
1422 .read = viu_read,
1423 .poll = viu_poll,
1424 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1425 .mmap = viu_mmap,
1428 static const struct v4l2_ioctl_ops viu_ioctl_ops = {
1429 .vidioc_querycap = vidioc_querycap,
1430 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1431 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_cap,
1432 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_cap,
1433 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_cap,
1434 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt,
1435 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_overlay,
1436 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_overlay,
1437 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_overlay,
1438 .vidioc_overlay = vidioc_overlay,
1439 .vidioc_g_fbuf = vidioc_g_fbuf,
1440 .vidioc_s_fbuf = vidioc_s_fbuf,
1441 .vidioc_reqbufs = vidioc_reqbufs,
1442 .vidioc_querybuf = vidioc_querybuf,
1443 .vidioc_qbuf = vidioc_qbuf,
1444 .vidioc_dqbuf = vidioc_dqbuf,
1445 .vidioc_g_std = vidioc_g_std,
1446 .vidioc_s_std = vidioc_s_std,
1447 .vidioc_querystd = vidioc_querystd,
1448 .vidioc_enum_input = vidioc_enum_input,
1449 .vidioc_g_input = vidioc_g_input,
1450 .vidioc_s_input = vidioc_s_input,
1451 .vidioc_queryctrl = vidioc_queryctrl,
1452 .vidioc_g_ctrl = vidioc_g_ctrl,
1453 .vidioc_s_ctrl = vidioc_s_ctrl,
1454 .vidioc_streamon = vidioc_streamon,
1455 .vidioc_streamoff = vidioc_streamoff,
1458 static struct video_device viu_template = {
1459 .name = "FSL viu",
1460 .fops = &viu_fops,
1461 .minor = -1,
1462 .ioctl_ops = &viu_ioctl_ops,
1463 .release = video_device_release,
1465 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1466 .current_norm = V4L2_STD_NTSC_M,
1469 static int __devinit viu_of_probe(struct platform_device *op)
1471 struct viu_dev *viu_dev;
1472 struct video_device *vdev;
1473 struct resource r;
1474 struct viu_reg __iomem *viu_regs;
1475 struct i2c_adapter *ad;
1476 int ret, viu_irq;
1478 ret = of_address_to_resource(op->dev.of_node, 0, &r);
1479 if (ret) {
1480 dev_err(&op->dev, "Can't parse device node resource\n");
1481 return -ENODEV;
1484 viu_irq = irq_of_parse_and_map(op->dev.of_node, 0);
1485 if (viu_irq == NO_IRQ) {
1486 dev_err(&op->dev, "Error while mapping the irq\n");
1487 return -EINVAL;
1490 /* request mem region */
1491 if (!devm_request_mem_region(&op->dev, r.start,
1492 sizeof(struct viu_reg), DRV_NAME)) {
1493 dev_err(&op->dev, "Error while requesting mem region\n");
1494 ret = -EBUSY;
1495 goto err;
1498 /* remap registers */
1499 viu_regs = devm_ioremap(&op->dev, r.start, sizeof(struct viu_reg));
1500 if (!viu_regs) {
1501 dev_err(&op->dev, "Can't map register set\n");
1502 ret = -ENOMEM;
1503 goto err;
1506 /* Prepare our private structure */
1507 viu_dev = devm_kzalloc(&op->dev, sizeof(struct viu_dev), GFP_ATOMIC);
1508 if (!viu_dev) {
1509 dev_err(&op->dev, "Can't allocate private structure\n");
1510 ret = -ENOMEM;
1511 goto err;
1514 viu_dev->vr = viu_regs;
1515 viu_dev->irq = viu_irq;
1516 viu_dev->dev = &op->dev;
1518 /* init video dma queues */
1519 INIT_LIST_HEAD(&viu_dev->vidq.active);
1520 INIT_LIST_HEAD(&viu_dev->vidq.queued);
1522 snprintf(viu_dev->v4l2_dev.name,
1523 sizeof(viu_dev->v4l2_dev.name), "%s", "VIU");
1524 ret = v4l2_device_register(viu_dev->dev, &viu_dev->v4l2_dev);
1525 if (ret < 0) {
1526 dev_err(&op->dev, "v4l2_device_register() failed: %d\n", ret);
1527 goto err;
1530 ad = i2c_get_adapter(0);
1531 viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
1532 "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
1534 viu_dev->vidq.timeout.function = viu_vid_timeout;
1535 viu_dev->vidq.timeout.data = (unsigned long)viu_dev;
1536 init_timer(&viu_dev->vidq.timeout);
1537 viu_dev->first = 1;
1539 /* Allocate memory for video device */
1540 vdev = video_device_alloc();
1541 if (vdev == NULL) {
1542 ret = -ENOMEM;
1543 goto err_vdev;
1546 memcpy(vdev, &viu_template, sizeof(viu_template));
1548 vdev->v4l2_dev = &viu_dev->v4l2_dev;
1550 viu_dev->vdev = vdev;
1552 /* initialize locks */
1553 mutex_init(&viu_dev->lock);
1554 viu_dev->vdev->lock = &viu_dev->lock;
1555 spin_lock_init(&viu_dev->slock);
1557 video_set_drvdata(viu_dev->vdev, viu_dev);
1559 mutex_lock(&viu_dev->lock);
1561 ret = video_register_device(viu_dev->vdev, VFL_TYPE_GRABBER, -1);
1562 if (ret < 0) {
1563 video_device_release(viu_dev->vdev);
1564 goto err_vdev;
1567 /* enable VIU clock */
1568 viu_dev->clk = clk_get(&op->dev, "viu_clk");
1569 if (IS_ERR(viu_dev->clk)) {
1570 dev_err(&op->dev, "failed to find the clock module!\n");
1571 ret = -ENODEV;
1572 goto err_clk;
1573 } else {
1574 clk_enable(viu_dev->clk);
1577 /* reset VIU module */
1578 viu_reset(viu_dev->vr);
1580 /* install interrupt handler */
1581 if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
1582 dev_err(&op->dev, "Request VIU IRQ failed.\n");
1583 ret = -ENODEV;
1584 goto err_irq;
1587 mutex_unlock(&viu_dev->lock);
1589 dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
1590 return ret;
1592 err_irq:
1593 clk_disable(viu_dev->clk);
1594 clk_put(viu_dev->clk);
1595 err_clk:
1596 video_unregister_device(viu_dev->vdev);
1597 err_vdev:
1598 mutex_unlock(&viu_dev->lock);
1599 i2c_put_adapter(ad);
1600 v4l2_device_unregister(&viu_dev->v4l2_dev);
1601 err:
1602 irq_dispose_mapping(viu_irq);
1603 return ret;
1606 static int __devexit viu_of_remove(struct platform_device *op)
1608 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1609 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1610 struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
1611 struct v4l2_subdev, list);
1612 struct i2c_client *client = v4l2_get_subdevdata(sdev);
1614 free_irq(dev->irq, (void *)dev);
1615 irq_dispose_mapping(dev->irq);
1617 clk_disable(dev->clk);
1618 clk_put(dev->clk);
1620 video_unregister_device(dev->vdev);
1621 i2c_put_adapter(client->adapter);
1622 v4l2_device_unregister(&dev->v4l2_dev);
1623 return 0;
1626 #ifdef CONFIG_PM
1627 static int viu_suspend(struct platform_device *op, pm_message_t state)
1629 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1630 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1632 clk_disable(dev->clk);
1633 return 0;
1636 static int viu_resume(struct platform_device *op)
1638 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1639 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1641 clk_enable(dev->clk);
1642 return 0;
1644 #endif
1647 * Initialization and module stuff
1649 static struct of_device_id mpc512x_viu_of_match[] = {
1651 .compatible = "fsl,mpc5121-viu",
1655 MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
1657 static struct platform_driver viu_of_platform_driver = {
1658 .probe = viu_of_probe,
1659 .remove = __devexit_p(viu_of_remove),
1660 #ifdef CONFIG_PM
1661 .suspend = viu_suspend,
1662 .resume = viu_resume,
1663 #endif
1664 .driver = {
1665 .name = DRV_NAME,
1666 .owner = THIS_MODULE,
1667 .of_match_table = mpc512x_viu_of_match,
1671 static int __init viu_init(void)
1673 return platform_driver_register(&viu_of_platform_driver);
1676 static void __exit viu_exit(void)
1678 platform_driver_unregister(&viu_of_platform_driver);
1681 module_init(viu_init);
1682 module_exit(viu_exit);
1684 MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1685 MODULE_AUTHOR("Hongjun Chen");
1686 MODULE_LICENSE("GPL");