Linux Audio

Check our new training course

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