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