xref: /linux/drivers/media/pci/cx25821/cx25821-video.c (revision 2bc46b3ad3c15165f91459b07ff8682478683194)
1b285192aSMauro Carvalho Chehab /*
2b285192aSMauro Carvalho Chehab  *  Driver for the Conexant CX25821 PCIe bridge
3b285192aSMauro Carvalho Chehab  *
4b285192aSMauro Carvalho Chehab  *  Copyright (C) 2009 Conexant Systems Inc.
5b285192aSMauro Carvalho Chehab  *  Authors  <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6b671ae6bSHans Verkuil  *  Based on Steven Toth <stoth@linuxtv.org> cx25821 driver
7b285192aSMauro Carvalho Chehab  *  Parts adapted/taken from Eduardo Moscoso Rubino
8b285192aSMauro Carvalho Chehab  *  Copyright (C) 2009 Eduardo Moscoso Rubino <moscoso@TopoLogica.com>
9b285192aSMauro Carvalho Chehab  *
10b285192aSMauro Carvalho Chehab  *
11b285192aSMauro Carvalho Chehab  *  This program is free software; you can redistribute it and/or modify
12b285192aSMauro Carvalho Chehab  *  it under the terms of the GNU General Public License as published by
13b285192aSMauro Carvalho Chehab  *  the Free Software Foundation; either version 2 of the License, or
14b285192aSMauro Carvalho Chehab  *  (at your option) any later version.
15b285192aSMauro Carvalho Chehab  *
16b285192aSMauro Carvalho Chehab  *  This program is distributed in the hope that it will be useful,
17b285192aSMauro Carvalho Chehab  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18b285192aSMauro Carvalho Chehab  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19b285192aSMauro Carvalho Chehab  *
20b285192aSMauro Carvalho Chehab  *  GNU General Public License for more details.
21b285192aSMauro Carvalho Chehab  *
22b285192aSMauro Carvalho Chehab  *  You should have received a copy of the GNU General Public License
23b285192aSMauro Carvalho Chehab  *  along with this program; if not, write to the Free Software
24b285192aSMauro Carvalho Chehab  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25b285192aSMauro Carvalho Chehab  */
26b285192aSMauro Carvalho Chehab 
27b285192aSMauro Carvalho Chehab #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28b285192aSMauro Carvalho Chehab 
29b285192aSMauro Carvalho Chehab #include "cx25821-video.h"
30b285192aSMauro Carvalho Chehab 
31b285192aSMauro Carvalho Chehab MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
32b285192aSMauro Carvalho Chehab MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
33b285192aSMauro Carvalho Chehab MODULE_LICENSE("GPL");
34b285192aSMauro Carvalho Chehab 
35b285192aSMauro Carvalho Chehab static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
36b285192aSMauro Carvalho Chehab 
37b285192aSMauro Carvalho Chehab module_param_array(video_nr, int, NULL, 0444);
38b285192aSMauro Carvalho Chehab 
39b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_nr, "video device numbers");
40b285192aSMauro Carvalho Chehab 
41b285192aSMauro Carvalho Chehab static unsigned int video_debug = VIDEO_DEBUG;
42b285192aSMauro Carvalho Chehab module_param(video_debug, int, 0644);
43b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
44b285192aSMauro Carvalho Chehab 
45b285192aSMauro Carvalho Chehab static unsigned int irq_debug;
46b285192aSMauro Carvalho Chehab module_param(irq_debug, int, 0644);
47b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
48b285192aSMauro Carvalho Chehab 
49b285192aSMauro Carvalho Chehab #define FORMAT_FLAGS_PACKED       0x01
50b285192aSMauro Carvalho Chehab 
5195c232a2SHans Verkuil static const struct cx25821_fmt formats[] = {
52b285192aSMauro Carvalho Chehab 	{
53b285192aSMauro Carvalho Chehab 		.name = "4:1:1, packed, Y41P",
54b285192aSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_Y41P,
55b285192aSMauro Carvalho Chehab 		.depth = 12,
56b285192aSMauro Carvalho Chehab 		.flags = FORMAT_FLAGS_PACKED,
57b285192aSMauro Carvalho Chehab 	}, {
58b285192aSMauro Carvalho Chehab 		.name = "4:2:2, packed, YUYV",
59b285192aSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_YUYV,
60b285192aSMauro Carvalho Chehab 		.depth = 16,
61b285192aSMauro Carvalho Chehab 		.flags = FORMAT_FLAGS_PACKED,
62b285192aSMauro Carvalho Chehab 	},
63b285192aSMauro Carvalho Chehab };
64b285192aSMauro Carvalho Chehab 
6595c232a2SHans Verkuil static const struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
66b285192aSMauro Carvalho Chehab {
67b285192aSMauro Carvalho Chehab 	unsigned int i;
68b285192aSMauro Carvalho Chehab 
69b285192aSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(formats); i++)
70b285192aSMauro Carvalho Chehab 		if (formats[i].fourcc == fourcc)
71b285192aSMauro Carvalho Chehab 			return formats + i;
72b285192aSMauro Carvalho Chehab 	return NULL;
73b285192aSMauro Carvalho Chehab }
74b285192aSMauro Carvalho Chehab 
75b285192aSMauro Carvalho Chehab int cx25821_start_video_dma(struct cx25821_dev *dev,
76b285192aSMauro Carvalho Chehab 			    struct cx25821_dmaqueue *q,
77b285192aSMauro Carvalho Chehab 			    struct cx25821_buffer *buf,
78bfef0d35SHans Verkuil 			    const struct sram_channel *channel)
79b285192aSMauro Carvalho Chehab {
80b285192aSMauro Carvalho Chehab 	int tmp = 0;
81b285192aSMauro Carvalho Chehab 
82b285192aSMauro Carvalho Chehab 	/* setup fifo + format */
83b285192aSMauro Carvalho Chehab 	cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
84b285192aSMauro Carvalho Chehab 
85b285192aSMauro Carvalho Chehab 	/* reset counter */
86b285192aSMauro Carvalho Chehab 	cx_write(channel->gpcnt_ctl, 3);
87b285192aSMauro Carvalho Chehab 
88b285192aSMauro Carvalho Chehab 	/* enable irq */
89b285192aSMauro Carvalho Chehab 	cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
90b285192aSMauro Carvalho Chehab 	cx_set(channel->int_msk, 0x11);
91b285192aSMauro Carvalho Chehab 
92b285192aSMauro Carvalho Chehab 	/* start dma */
93b285192aSMauro Carvalho Chehab 	cx_write(channel->dma_ctl, 0x11);	/* FIFO and RISC enable */
94b285192aSMauro Carvalho Chehab 
95b285192aSMauro Carvalho Chehab 	/* make sure upstream setting if any is reversed */
96b285192aSMauro Carvalho Chehab 	tmp = cx_read(VID_CH_MODE_SEL);
97b285192aSMauro Carvalho Chehab 	cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
98b285192aSMauro Carvalho Chehab 
99b285192aSMauro Carvalho Chehab 	return 0;
100b285192aSMauro Carvalho Chehab }
101b285192aSMauro Carvalho Chehab 
102b285192aSMauro Carvalho Chehab int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
103b285192aSMauro Carvalho Chehab {
104b285192aSMauro Carvalho Chehab 	int handled = 0;
105b285192aSMauro Carvalho Chehab 	u32 mask;
106bfef0d35SHans Verkuil 	const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
107b285192aSMauro Carvalho Chehab 
108b285192aSMauro Carvalho Chehab 	mask = cx_read(channel->int_msk);
109b285192aSMauro Carvalho Chehab 	if (0 == (status & mask))
110b285192aSMauro Carvalho Chehab 		return handled;
111b285192aSMauro Carvalho Chehab 
112b285192aSMauro Carvalho Chehab 	cx_write(channel->int_stat, status);
113b285192aSMauro Carvalho Chehab 
114b285192aSMauro Carvalho Chehab 	/* risc op code error */
115b285192aSMauro Carvalho Chehab 	if (status & (1 << 16)) {
116b285192aSMauro Carvalho Chehab 		pr_warn("%s, %s: video risc op code error\n",
117b285192aSMauro Carvalho Chehab 			dev->name, channel->name);
118b285192aSMauro Carvalho Chehab 		cx_clear(channel->dma_ctl, 0x11);
119b285192aSMauro Carvalho Chehab 		cx25821_sram_channel_dump(dev, channel);
120b285192aSMauro Carvalho Chehab 	}
121b285192aSMauro Carvalho Chehab 
122b285192aSMauro Carvalho Chehab 	/* risc1 y */
123b285192aSMauro Carvalho Chehab 	if (status & FLD_VID_DST_RISC1) {
124b671ae6bSHans Verkuil 		struct cx25821_dmaqueue *dmaq =
125b671ae6bSHans Verkuil 			&dev->channels[channel->i].dma_vidq;
126b671ae6bSHans Verkuil 		struct cx25821_buffer *buf;
127b285192aSMauro Carvalho Chehab 
128b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
129b671ae6bSHans Verkuil 		if (!list_empty(&dmaq->active)) {
130b671ae6bSHans Verkuil 			buf = list_entry(dmaq->active.next,
131b671ae6bSHans Verkuil 					 struct cx25821_buffer, queue);
132b671ae6bSHans Verkuil 
133d6dd645eSJunghak Sung 			buf->vb.vb2_buf.timestamp = ktime_get_ns();
1342d700715SJunghak Sung 			buf->vb.sequence = dmaq->count++;
135b671ae6bSHans Verkuil 			list_del(&buf->queue);
1362d700715SJunghak Sung 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
137b671ae6bSHans Verkuil 		}
138b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
139b285192aSMauro Carvalho Chehab 		handled++;
140b285192aSMauro Carvalho Chehab 	}
141b285192aSMauro Carvalho Chehab 	return handled;
142b285192aSMauro Carvalho Chehab }
143b285192aSMauro Carvalho Chehab 
144df9ecb0cSHans Verkuil static int cx25821_queue_setup(struct vb2_queue *q,
145b671ae6bSHans Verkuil 			   unsigned int *num_buffers, unsigned int *num_planes,
146b671ae6bSHans Verkuil 			   unsigned int sizes[], void *alloc_ctxs[])
147b285192aSMauro Carvalho Chehab {
148b671ae6bSHans Verkuil 	struct cx25821_channel *chan = q->drv_priv;
14911c8a2dfSHans Verkuil 	unsigned size = (chan->fmt->depth * chan->width * chan->height) >> 3;
15011c8a2dfSHans Verkuil 
151df9ecb0cSHans Verkuil 	if (*num_planes)
152df9ecb0cSHans Verkuil 		return sizes[0] < size ? -EINVAL : 0;
153b285192aSMauro Carvalho Chehab 
154b671ae6bSHans Verkuil 	*num_planes = 1;
155df9ecb0cSHans Verkuil 	sizes[0] = size;
156b285192aSMauro Carvalho Chehab 	return 0;
157b285192aSMauro Carvalho Chehab }
158b285192aSMauro Carvalho Chehab 
159b671ae6bSHans Verkuil static int cx25821_buffer_prepare(struct vb2_buffer *vb)
160b285192aSMauro Carvalho Chehab {
1612d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
162b671ae6bSHans Verkuil 	struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
1632efe2cc4SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
164b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
1652d700715SJunghak Sung 		container_of(vbuf, struct cx25821_buffer, vb);
166b671ae6bSHans Verkuil 	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
167b285192aSMauro Carvalho Chehab 	u32 line0_offset;
168b285192aSMauro Carvalho Chehab 	int bpl_local = LINE_SIZE_D1;
169b671ae6bSHans Verkuil 	int ret;
170b285192aSMauro Carvalho Chehab 
1718d125c50SHans Verkuil 	if (chan->pixel_formats == PIXEL_FRMT_411)
172b671ae6bSHans Verkuil 		buf->bpl = (chan->fmt->depth * chan->width) >> 3;
173b285192aSMauro Carvalho Chehab 	else
174b671ae6bSHans Verkuil 		buf->bpl = (chan->fmt->depth >> 3) * chan->width;
175b671ae6bSHans Verkuil 
176b671ae6bSHans Verkuil 	if (vb2_plane_size(vb, 0) < chan->height * buf->bpl)
177b671ae6bSHans Verkuil 		return -EINVAL;
178b671ae6bSHans Verkuil 	vb2_set_plane_payload(vb, 0, chan->height * buf->bpl);
1792d700715SJunghak Sung 	buf->vb.field = chan->field;
180b285192aSMauro Carvalho Chehab 
1818d125c50SHans Verkuil 	if (chan->pixel_formats == PIXEL_FRMT_411) {
182b285192aSMauro Carvalho Chehab 		bpl_local = buf->bpl;
183b285192aSMauro Carvalho Chehab 	} else {
184b285192aSMauro Carvalho Chehab 		bpl_local = buf->bpl;   /* Default */
185b285192aSMauro Carvalho Chehab 
1868d125c50SHans Verkuil 		if (chan->use_cif_resolution) {
187988f7b80SHans Verkuil 			if (dev->tvnorm & V4L2_STD_625_50)
188b285192aSMauro Carvalho Chehab 				bpl_local = 352 << 1;
189b285192aSMauro Carvalho Chehab 			else
1908d125c50SHans Verkuil 				bpl_local = chan->cif_width << 1;
191b285192aSMauro Carvalho Chehab 		}
192b285192aSMauro Carvalho Chehab 	}
193b285192aSMauro Carvalho Chehab 
194b671ae6bSHans Verkuil 	switch (chan->field) {
195b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_TOP:
196b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
197b671ae6bSHans Verkuil 				sgt->sgl, 0, UNSET,
198b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height);
199b285192aSMauro Carvalho Chehab 		break;
200b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_BOTTOM:
201b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
202b671ae6bSHans Verkuil 				sgt->sgl, UNSET, 0,
203b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height);
204b285192aSMauro Carvalho Chehab 		break;
205b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_INTERLACED:
206b285192aSMauro Carvalho Chehab 		/* All other formats are top field first */
207b285192aSMauro Carvalho Chehab 		line0_offset = 0;
208b285192aSMauro Carvalho Chehab 		dprintk(1, "top field first\n");
209b285192aSMauro Carvalho Chehab 
210b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
211b671ae6bSHans Verkuil 				sgt->sgl, line0_offset,
212b285192aSMauro Carvalho Chehab 				bpl_local, bpl_local, bpl_local,
213b671ae6bSHans Verkuil 				chan->height >> 1);
214b285192aSMauro Carvalho Chehab 		break;
215b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_TB:
216b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
217b671ae6bSHans Verkuil 				sgt->sgl,
218b671ae6bSHans Verkuil 				0, buf->bpl * (chan->height >> 1),
219b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height >> 1);
220b285192aSMauro Carvalho Chehab 		break;
221b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_BT:
222b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
223b671ae6bSHans Verkuil 				sgt->sgl,
224b671ae6bSHans Verkuil 				buf->bpl * (chan->height >> 1), 0,
225b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height >> 1);
226b285192aSMauro Carvalho Chehab 		break;
227b285192aSMauro Carvalho Chehab 	default:
228b671ae6bSHans Verkuil 		WARN_ON(1);
229b671ae6bSHans Verkuil 		ret = -EINVAL;
230b671ae6bSHans Verkuil 		break;
231b285192aSMauro Carvalho Chehab 	}
232b285192aSMauro Carvalho Chehab 
233b285192aSMauro Carvalho Chehab 	dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
2342d700715SJunghak Sung 		buf, buf->vb.vb2_buf.index, chan->width, chan->height,
235b671ae6bSHans Verkuil 		chan->fmt->depth, chan->fmt->name,
236b671ae6bSHans Verkuil 		(unsigned long)buf->risc.dma);
237b285192aSMauro Carvalho Chehab 
238b671ae6bSHans Verkuil 	return ret;
239b285192aSMauro Carvalho Chehab }
240b285192aSMauro Carvalho Chehab 
241b671ae6bSHans Verkuil static void cx25821_buffer_finish(struct vb2_buffer *vb)
242b285192aSMauro Carvalho Chehab {
2432d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
244b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
2452d700715SJunghak Sung 		container_of(vbuf, struct cx25821_buffer, vb);
246b671ae6bSHans Verkuil 	struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
2472efe2cc4SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
248b671ae6bSHans Verkuil 
249b671ae6bSHans Verkuil 	cx25821_free_buffer(dev, buf);
250b671ae6bSHans Verkuil }
251b671ae6bSHans Verkuil 
252b671ae6bSHans Verkuil static void cx25821_buffer_queue(struct vb2_buffer *vb)
253b671ae6bSHans Verkuil {
2542d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
255b671ae6bSHans Verkuil 	struct cx25821_buffer *buf =
2562d700715SJunghak Sung 		container_of(vbuf, struct cx25821_buffer, vb);
257b671ae6bSHans Verkuil 	struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
258b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
259b671ae6bSHans Verkuil 	struct cx25821_buffer *prev;
2602efe2cc4SHans Verkuil 	struct cx25821_dmaqueue *q = &dev->channels[chan->id].dma_vidq;
261b285192aSMauro Carvalho Chehab 
262b671ae6bSHans Verkuil 	buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
263b671ae6bSHans Verkuil 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
264b671ae6bSHans Verkuil 	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
265b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
266b285192aSMauro Carvalho Chehab 
267b671ae6bSHans Verkuil 	if (list_empty(&q->active)) {
268b671ae6bSHans Verkuil 		list_add_tail(&buf->queue, &q->active);
269b285192aSMauro Carvalho Chehab 	} else {
270b671ae6bSHans Verkuil 		buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
271b285192aSMauro Carvalho Chehab 		prev = list_entry(q->active.prev, struct cx25821_buffer,
272b671ae6bSHans Verkuil 				queue);
273b671ae6bSHans Verkuil 		list_add_tail(&buf->queue, &q->active);
274b285192aSMauro Carvalho Chehab 		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
275b285192aSMauro Carvalho Chehab 	}
276b285192aSMauro Carvalho Chehab }
277b285192aSMauro Carvalho Chehab 
278b671ae6bSHans Verkuil static int cx25821_start_streaming(struct vb2_queue *q, unsigned int count)
279b671ae6bSHans Verkuil {
280b671ae6bSHans Verkuil 	struct cx25821_channel *chan = q->drv_priv;
281b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
282b671ae6bSHans Verkuil 	struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
283b671ae6bSHans Verkuil 	struct cx25821_buffer *buf = list_entry(dmaq->active.next,
284b671ae6bSHans Verkuil 			struct cx25821_buffer, queue);
285b671ae6bSHans Verkuil 
286b671ae6bSHans Verkuil 	dmaq->count = 0;
287b671ae6bSHans Verkuil 	cx25821_start_video_dma(dev, dmaq, buf, chan->sram_channels);
288b671ae6bSHans Verkuil 	return 0;
289b285192aSMauro Carvalho Chehab }
290b285192aSMauro Carvalho Chehab 
291b671ae6bSHans Verkuil static void cx25821_stop_streaming(struct vb2_queue *q)
292b671ae6bSHans Verkuil {
293b671ae6bSHans Verkuil 	struct cx25821_channel *chan = q->drv_priv;
294b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
295b671ae6bSHans Verkuil 	struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
296b671ae6bSHans Verkuil 	unsigned long flags;
297b671ae6bSHans Verkuil 
298b671ae6bSHans Verkuil 	cx_write(chan->sram_channels->dma_ctl, 0); /* FIFO and RISC disable */
299b671ae6bSHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
300b671ae6bSHans Verkuil 	while (!list_empty(&dmaq->active)) {
301b671ae6bSHans Verkuil 		struct cx25821_buffer *buf = list_entry(dmaq->active.next,
302b671ae6bSHans Verkuil 			struct cx25821_buffer, queue);
303b671ae6bSHans Verkuil 
304b671ae6bSHans Verkuil 		list_del(&buf->queue);
3052d700715SJunghak Sung 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
306b671ae6bSHans Verkuil 	}
307b671ae6bSHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
308b671ae6bSHans Verkuil }
309b671ae6bSHans Verkuil 
310b671ae6bSHans Verkuil static struct vb2_ops cx25821_video_qops = {
311b671ae6bSHans Verkuil 	.queue_setup    = cx25821_queue_setup,
312b285192aSMauro Carvalho Chehab 	.buf_prepare  = cx25821_buffer_prepare,
313b671ae6bSHans Verkuil 	.buf_finish = cx25821_buffer_finish,
314b671ae6bSHans Verkuil 	.buf_queue    = cx25821_buffer_queue,
315b671ae6bSHans Verkuil 	.wait_prepare = vb2_ops_wait_prepare,
316b671ae6bSHans Verkuil 	.wait_finish = vb2_ops_wait_finish,
317b671ae6bSHans Verkuil 	.start_streaming = cx25821_start_streaming,
318b671ae6bSHans Verkuil 	.stop_streaming = cx25821_stop_streaming,
319b285192aSMauro Carvalho Chehab };
320b285192aSMauro Carvalho Chehab 
32195c232a2SHans Verkuil /* VIDEO IOCTLS */
3224c1d0f73SHans Verkuil 
3234c1d0f73SHans Verkuil static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
3244c1d0f73SHans Verkuil 			    struct v4l2_fmtdesc *f)
3254c1d0f73SHans Verkuil {
3264c1d0f73SHans Verkuil 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
3274c1d0f73SHans Verkuil 		return -EINVAL;
3284c1d0f73SHans Verkuil 
3294c1d0f73SHans Verkuil 	strlcpy(f->description, formats[f->index].name, sizeof(f->description));
3304c1d0f73SHans Verkuil 	f->pixelformat = formats[f->index].fourcc;
3314c1d0f73SHans Verkuil 
3324c1d0f73SHans Verkuil 	return 0;
3334c1d0f73SHans Verkuil }
3344c1d0f73SHans Verkuil 
33595c232a2SHans Verkuil static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
33695c232a2SHans Verkuil 				 struct v4l2_format *f)
33795c232a2SHans Verkuil {
3382efe2cc4SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
33995c232a2SHans Verkuil 
3402efe2cc4SHans Verkuil 	f->fmt.pix.width = chan->width;
3412efe2cc4SHans Verkuil 	f->fmt.pix.height = chan->height;
342b671ae6bSHans Verkuil 	f->fmt.pix.field = chan->field;
3432efe2cc4SHans Verkuil 	f->fmt.pix.pixelformat = chan->fmt->fourcc;
344988f7b80SHans Verkuil 	f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
345988f7b80SHans Verkuil 	f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
346988f7b80SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
34795c232a2SHans Verkuil 
34895c232a2SHans Verkuil 	return 0;
34995c232a2SHans Verkuil }
35095c232a2SHans Verkuil 
35195c232a2SHans Verkuil static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
35295c232a2SHans Verkuil 				   struct v4l2_format *f)
35395c232a2SHans Verkuil {
354988f7b80SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
355988f7b80SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
35695c232a2SHans Verkuil 	const struct cx25821_fmt *fmt;
357988f7b80SHans Verkuil 	enum v4l2_field field = f->fmt.pix.field;
35866f93178SMauro Carvalho Chehab 	unsigned int maxh;
359988f7b80SHans Verkuil 	unsigned w;
36095c232a2SHans Verkuil 
36195c232a2SHans Verkuil 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
36295c232a2SHans Verkuil 	if (NULL == fmt)
36395c232a2SHans Verkuil 		return -EINVAL;
364988f7b80SHans Verkuil 	maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
36595c232a2SHans Verkuil 
366988f7b80SHans Verkuil 	w = f->fmt.pix.width;
367988f7b80SHans Verkuil 	if (field != V4L2_FIELD_BOTTOM)
36895c232a2SHans Verkuil 		field = V4L2_FIELD_TOP;
369988f7b80SHans Verkuil 	if (w < 352) {
370988f7b80SHans Verkuil 		w = 176;
371988f7b80SHans Verkuil 		f->fmt.pix.height = maxh / 4;
372988f7b80SHans Verkuil 	} else if (w < 720) {
373988f7b80SHans Verkuil 		w = 352;
374988f7b80SHans Verkuil 		f->fmt.pix.height = maxh / 2;
375988f7b80SHans Verkuil 	} else {
376988f7b80SHans Verkuil 		w = 720;
37795c232a2SHans Verkuil 		f->fmt.pix.height = maxh;
378988f7b80SHans Verkuil 		field = V4L2_FIELD_INTERLACED;
379988f7b80SHans Verkuil 	}
380988f7b80SHans Verkuil 	f->fmt.pix.field = field;
381988f7b80SHans Verkuil 	f->fmt.pix.width = w;
38295c232a2SHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
38395c232a2SHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
384988f7b80SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
38595c232a2SHans Verkuil 
38695c232a2SHans Verkuil 	return 0;
38795c232a2SHans Verkuil }
388b285192aSMauro Carvalho Chehab 
389b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
390b285192aSMauro Carvalho Chehab 				struct v4l2_format *f)
391b285192aSMauro Carvalho Chehab {
3922efe2cc4SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
3938d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
394b285192aSMauro Carvalho Chehab 	int pix_format = PIXEL_FRMT_422;
395a6aa0dc4SHans Verkuil 	int err;
396b285192aSMauro Carvalho Chehab 
397b285192aSMauro Carvalho Chehab 	err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
398b285192aSMauro Carvalho Chehab 
399b285192aSMauro Carvalho Chehab 	if (0 != err)
400b285192aSMauro Carvalho Chehab 		return err;
401b285192aSMauro Carvalho Chehab 
4022efe2cc4SHans Verkuil 	chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
403b671ae6bSHans Verkuil 	chan->field = f->fmt.pix.field;
4042efe2cc4SHans Verkuil 	chan->width = f->fmt.pix.width;
4052efe2cc4SHans Verkuil 	chan->height = f->fmt.pix.height;
406b285192aSMauro Carvalho Chehab 
407b285192aSMauro Carvalho Chehab 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
408b285192aSMauro Carvalho Chehab 		pix_format = PIXEL_FRMT_411;
409b285192aSMauro Carvalho Chehab 	else
410988f7b80SHans Verkuil 		pix_format = PIXEL_FRMT_422;
411b285192aSMauro Carvalho Chehab 
412b285192aSMauro Carvalho Chehab 	cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
413b285192aSMauro Carvalho Chehab 
414b285192aSMauro Carvalho Chehab 	/* check if cif resolution */
4152efe2cc4SHans Verkuil 	if (chan->width == 320 || chan->width == 352)
4162efe2cc4SHans Verkuil 		chan->use_cif_resolution = 1;
417b285192aSMauro Carvalho Chehab 	else
4182efe2cc4SHans Verkuil 		chan->use_cif_resolution = 0;
419b285192aSMauro Carvalho Chehab 
4202efe2cc4SHans Verkuil 	chan->cif_width = chan->width;
4212efe2cc4SHans Verkuil 	medusa_set_resolution(dev, chan->width, SRAM_CH00);
422b285192aSMauro Carvalho Chehab 	return 0;
423b285192aSMauro Carvalho Chehab }
424b285192aSMauro Carvalho Chehab 
425b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
426b285192aSMauro Carvalho Chehab {
4278d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4288d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
4298d125c50SHans Verkuil 	const struct sram_channel *sram_ch = chan->sram_channels;
430b285192aSMauro Carvalho Chehab 	u32 tmp = 0;
431b285192aSMauro Carvalho Chehab 
432b285192aSMauro Carvalho Chehab 	tmp = cx_read(sram_ch->dma_ctl);
433b285192aSMauro Carvalho Chehab 	pr_info("Video input 0 is %s\n",
434b285192aSMauro Carvalho Chehab 		(tmp & 0x11) ? "streaming" : "stopped");
435b285192aSMauro Carvalho Chehab 	return 0;
436b285192aSMauro Carvalho Chehab }
437b285192aSMauro Carvalho Chehab 
438b285192aSMauro Carvalho Chehab 
43995c232a2SHans Verkuil static int cx25821_vidioc_querycap(struct file *file, void *priv,
440b285192aSMauro Carvalho Chehab 			    struct v4l2_capability *cap)
441b285192aSMauro Carvalho Chehab {
4428d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4438d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
4443dd473caSHans Verkuil 	const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
4453dd473caSHans Verkuil 			V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
4460df13d99SHans Verkuil 	const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE;
447b285192aSMauro Carvalho Chehab 
448b285192aSMauro Carvalho Chehab 	strcpy(cap->driver, "cx25821");
449b285192aSMauro Carvalho Chehab 	strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
450b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
4518d125c50SHans Verkuil 	if (chan->id >= VID_CHANNEL_NUM)
4523dd473caSHans Verkuil 		cap->device_caps = cap_output;
4533dd473caSHans Verkuil 	else
4543dd473caSHans Verkuil 		cap->device_caps = cap_input;
4550df13d99SHans Verkuil 	cap->capabilities = cap_input | cap_output | V4L2_CAP_DEVICE_CAPS;
456b285192aSMauro Carvalho Chehab 	return 0;
457b285192aSMauro Carvalho Chehab }
458b285192aSMauro Carvalho Chehab 
45995c232a2SHans Verkuil static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
46018c73af6SHans Verkuil {
4618d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
46218c73af6SHans Verkuil 
4638d125c50SHans Verkuil 	*tvnorms = chan->dev->tvnorm;
46418c73af6SHans Verkuil 	return 0;
46518c73af6SHans Verkuil }
46618c73af6SHans Verkuil 
467a3f17af2SMauro Carvalho Chehab static int cx25821_vidioc_s_std(struct file *file, void *priv,
468a3f17af2SMauro Carvalho Chehab 				v4l2_std_id tvnorms)
469b285192aSMauro Carvalho Chehab {
4708d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4718d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
472b285192aSMauro Carvalho Chehab 
473314527acSHans Verkuil 	if (dev->tvnorm == tvnorms)
474b285192aSMauro Carvalho Chehab 		return 0;
475b285192aSMauro Carvalho Chehab 
476a6aa0dc4SHans Verkuil 	dev->tvnorm = tvnorms;
477988f7b80SHans Verkuil 	chan->width = 720;
478988f7b80SHans Verkuil 	chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
479b285192aSMauro Carvalho Chehab 
480b285192aSMauro Carvalho Chehab 	medusa_set_videostandard(dev);
481b285192aSMauro Carvalho Chehab 
482b285192aSMauro Carvalho Chehab 	return 0;
483b285192aSMauro Carvalho Chehab }
484b285192aSMauro Carvalho Chehab 
48595c232a2SHans Verkuil static int cx25821_vidioc_enum_input(struct file *file, void *priv,
48695c232a2SHans Verkuil 			      struct v4l2_input *i)
487b285192aSMauro Carvalho Chehab {
488a6aa0dc4SHans Verkuil 	if (i->index)
489b285192aSMauro Carvalho Chehab 		return -EINVAL;
490b285192aSMauro Carvalho Chehab 
491b285192aSMauro Carvalho Chehab 	i->type = V4L2_INPUT_TYPE_CAMERA;
492b285192aSMauro Carvalho Chehab 	i->std = CX25821_NORMS;
493a6aa0dc4SHans Verkuil 	strcpy(i->name, "Composite");
494b285192aSMauro Carvalho Chehab 	return 0;
495b285192aSMauro Carvalho Chehab }
496b285192aSMauro Carvalho Chehab 
49795c232a2SHans Verkuil static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
498b285192aSMauro Carvalho Chehab {
499a6aa0dc4SHans Verkuil 	*i = 0;
500b285192aSMauro Carvalho Chehab 	return 0;
501b285192aSMauro Carvalho Chehab }
502b285192aSMauro Carvalho Chehab 
50395c232a2SHans Verkuil static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
504b285192aSMauro Carvalho Chehab {
505a6aa0dc4SHans Verkuil 	return i ? -EINVAL : 0;
506b285192aSMauro Carvalho Chehab }
507b285192aSMauro Carvalho Chehab 
508f8d7ee70SHans Verkuil static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
509b285192aSMauro Carvalho Chehab {
510f8d7ee70SHans Verkuil 	struct cx25821_channel *chan =
511f8d7ee70SHans Verkuil 		container_of(ctrl->handler, struct cx25821_channel, hdl);
512f8d7ee70SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
513b285192aSMauro Carvalho Chehab 
514f8d7ee70SHans Verkuil 	switch (ctrl->id) {
515b285192aSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
516f8d7ee70SHans Verkuil 		medusa_set_brightness(dev, ctrl->val, chan->id);
517b285192aSMauro Carvalho Chehab 		break;
518b285192aSMauro Carvalho Chehab 	case V4L2_CID_HUE:
519f8d7ee70SHans Verkuil 		medusa_set_hue(dev, ctrl->val, chan->id);
520b285192aSMauro Carvalho Chehab 		break;
521b285192aSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
522f8d7ee70SHans Verkuil 		medusa_set_contrast(dev, ctrl->val, chan->id);
523b285192aSMauro Carvalho Chehab 		break;
524b285192aSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
525f8d7ee70SHans Verkuil 		medusa_set_saturation(dev, ctrl->val, chan->id);
526b285192aSMauro Carvalho Chehab 		break;
527b285192aSMauro Carvalho Chehab 	default:
528f8d7ee70SHans Verkuil 		return -EINVAL;
529b285192aSMauro Carvalho Chehab 	}
530f8d7ee70SHans Verkuil 	return 0;
531b285192aSMauro Carvalho Chehab }
532b285192aSMauro Carvalho Chehab 
5331f198870SHans Verkuil static int cx25821_vidioc_enum_output(struct file *file, void *priv,
5341f198870SHans Verkuil 			      struct v4l2_output *o)
535b285192aSMauro Carvalho Chehab {
5361f198870SHans Verkuil 	if (o->index)
5371f198870SHans Verkuil 		return -EINVAL;
538b285192aSMauro Carvalho Chehab 
5391f198870SHans Verkuil 	o->type = V4L2_INPUT_TYPE_CAMERA;
5401f198870SHans Verkuil 	o->std = CX25821_NORMS;
5411f198870SHans Verkuil 	strcpy(o->name, "Composite");
542b285192aSMauro Carvalho Chehab 	return 0;
543b285192aSMauro Carvalho Chehab }
544b285192aSMauro Carvalho Chehab 
5451f198870SHans Verkuil static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
546b285192aSMauro Carvalho Chehab {
5471f198870SHans Verkuil 	*o = 0;
548b285192aSMauro Carvalho Chehab 	return 0;
549b285192aSMauro Carvalho Chehab }
550b285192aSMauro Carvalho Chehab 
5511f198870SHans Verkuil static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
552b285192aSMauro Carvalho Chehab {
5531f198870SHans Verkuil 	return o ? -EINVAL : 0;
554b285192aSMauro Carvalho Chehab }
555b285192aSMauro Carvalho Chehab 
556e90878abSHans Verkuil static int cx25821_vidioc_try_fmt_vid_out(struct file *file, void *priv,
557e90878abSHans Verkuil 				   struct v4l2_format *f)
558e90878abSHans Verkuil {
559e90878abSHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
560e90878abSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
561e90878abSHans Verkuil 	const struct cx25821_fmt *fmt;
562e90878abSHans Verkuil 
563e90878abSHans Verkuil 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
564e90878abSHans Verkuil 	if (NULL == fmt)
565e90878abSHans Verkuil 		return -EINVAL;
566e90878abSHans Verkuil 	f->fmt.pix.width = 720;
567e90878abSHans Verkuil 	f->fmt.pix.height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
568e90878abSHans Verkuil 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
569e90878abSHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
570e90878abSHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
571e90878abSHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
572e90878abSHans Verkuil 	return 0;
573e90878abSHans Verkuil }
574e90878abSHans Verkuil 
575e90878abSHans Verkuil static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
576e90878abSHans Verkuil 				struct v4l2_format *f)
577e90878abSHans Verkuil {
578e90878abSHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
579e90878abSHans Verkuil 	int err;
580e90878abSHans Verkuil 
581e90878abSHans Verkuil 	err = cx25821_vidioc_try_fmt_vid_out(file, priv, f);
582e90878abSHans Verkuil 
583e90878abSHans Verkuil 	if (0 != err)
584e90878abSHans Verkuil 		return err;
585e90878abSHans Verkuil 
586e90878abSHans Verkuil 	chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
587b671ae6bSHans Verkuil 	chan->field = f->fmt.pix.field;
588e90878abSHans Verkuil 	chan->width = f->fmt.pix.width;
589e90878abSHans Verkuil 	chan->height = f->fmt.pix.height;
590e90878abSHans Verkuil 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
591e90878abSHans Verkuil 		chan->pixel_formats = PIXEL_FRMT_411;
592e90878abSHans Verkuil 	else
593e90878abSHans Verkuil 		chan->pixel_formats = PIXEL_FRMT_422;
594e90878abSHans Verkuil 	return 0;
595e90878abSHans Verkuil }
596e90878abSHans Verkuil 
597f8d7ee70SHans Verkuil static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
598f8d7ee70SHans Verkuil 	.s_ctrl = cx25821_s_ctrl,
599f8d7ee70SHans Verkuil };
600f8d7ee70SHans Verkuil 
601b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops = {
602b285192aSMauro Carvalho Chehab 	.owner = THIS_MODULE,
6038d125c50SHans Verkuil 	.open = v4l2_fh_open,
604b671ae6bSHans Verkuil 	.release        = vb2_fop_release,
605b671ae6bSHans Verkuil 	.read           = vb2_fop_read,
606b671ae6bSHans Verkuil 	.poll		= vb2_fop_poll,
6071f198870SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
608b671ae6bSHans Verkuil 	.mmap           = vb2_fop_mmap,
609b285192aSMauro Carvalho Chehab };
610b285192aSMauro Carvalho Chehab 
611b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops = {
612b285192aSMauro Carvalho Chehab 	.vidioc_querycap = cx25821_vidioc_querycap,
613b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
614b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
615b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
616b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
617b671ae6bSHans Verkuil 	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
618b671ae6bSHans Verkuil 	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
61911c8a2dfSHans Verkuil 	.vidioc_create_bufs   = vb2_ioctl_create_bufs,
620b671ae6bSHans Verkuil 	.vidioc_querybuf      = vb2_ioctl_querybuf,
621b671ae6bSHans Verkuil 	.vidioc_qbuf          = vb2_ioctl_qbuf,
622b671ae6bSHans Verkuil 	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
623b671ae6bSHans Verkuil 	.vidioc_streamon      = vb2_ioctl_streamon,
624b671ae6bSHans Verkuil 	.vidioc_streamoff     = vb2_ioctl_streamoff,
62518c73af6SHans Verkuil 	.vidioc_g_std = cx25821_vidioc_g_std,
626b285192aSMauro Carvalho Chehab 	.vidioc_s_std = cx25821_vidioc_s_std,
627b285192aSMauro Carvalho Chehab 	.vidioc_enum_input = cx25821_vidioc_enum_input,
628b285192aSMauro Carvalho Chehab 	.vidioc_g_input = cx25821_vidioc_g_input,
629b285192aSMauro Carvalho Chehab 	.vidioc_s_input = cx25821_vidioc_s_input,
630b285192aSMauro Carvalho Chehab 	.vidioc_log_status = vidioc_log_status,
6318d125c50SHans Verkuil 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
6328d125c50SHans Verkuil 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
633b285192aSMauro Carvalho Chehab };
634b285192aSMauro Carvalho Chehab 
635ffd3c233SHans Verkuil static const struct video_device cx25821_video_device = {
636ffd3c233SHans Verkuil 	.name = "cx25821-video",
637b285192aSMauro Carvalho Chehab 	.fops = &video_fops,
638467870caSHans Verkuil 	.release = video_device_release_empty,
639ffd3c233SHans Verkuil 	.minor = -1,
640b285192aSMauro Carvalho Chehab 	.ioctl_ops = &video_ioctl_ops,
641b285192aSMauro Carvalho Chehab 	.tvnorms = CX25821_NORMS,
642b285192aSMauro Carvalho Chehab };
643ffd3c233SHans Verkuil 
6441f198870SHans Verkuil static const struct v4l2_file_operations video_out_fops = {
6451f198870SHans Verkuil 	.owner = THIS_MODULE,
6461f198870SHans Verkuil 	.open = v4l2_fh_open,
647b671ae6bSHans Verkuil 	.release        = vb2_fop_release,
648b671ae6bSHans Verkuil 	.write          = vb2_fop_write,
649b671ae6bSHans Verkuil 	.poll		= vb2_fop_poll,
6501f198870SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
651b671ae6bSHans Verkuil 	.mmap           = vb2_fop_mmap,
6521f198870SHans Verkuil };
6531f198870SHans Verkuil 
6541f198870SHans Verkuil static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
6551f198870SHans Verkuil 	.vidioc_querycap = cx25821_vidioc_querycap,
656e90878abSHans Verkuil 	.vidioc_enum_fmt_vid_out = cx25821_vidioc_enum_fmt_vid_cap,
657e90878abSHans Verkuil 	.vidioc_g_fmt_vid_out = cx25821_vidioc_g_fmt_vid_cap,
658e90878abSHans Verkuil 	.vidioc_try_fmt_vid_out = cx25821_vidioc_try_fmt_vid_out,
659e90878abSHans Verkuil 	.vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
6601f198870SHans Verkuil 	.vidioc_g_std = cx25821_vidioc_g_std,
6611f198870SHans Verkuil 	.vidioc_s_std = cx25821_vidioc_s_std,
6621f198870SHans Verkuil 	.vidioc_enum_output = cx25821_vidioc_enum_output,
6631f198870SHans Verkuil 	.vidioc_g_output = cx25821_vidioc_g_output,
6641f198870SHans Verkuil 	.vidioc_s_output = cx25821_vidioc_s_output,
6651f198870SHans Verkuil 	.vidioc_log_status = vidioc_log_status,
6661f198870SHans Verkuil };
6671f198870SHans Verkuil 
6681f198870SHans Verkuil static const struct video_device cx25821_video_out_device = {
6691f198870SHans Verkuil 	.name = "cx25821-video",
6701f198870SHans Verkuil 	.fops = &video_out_fops,
6711f198870SHans Verkuil 	.release = video_device_release_empty,
6721f198870SHans Verkuil 	.minor = -1,
6731f198870SHans Verkuil 	.ioctl_ops = &video_out_ioctl_ops,
6741f198870SHans Verkuil 	.tvnorms = CX25821_NORMS,
6751f198870SHans Verkuil };
6761f198870SHans Verkuil 
677ffd3c233SHans Verkuil void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
678ffd3c233SHans Verkuil {
679ffd3c233SHans Verkuil 	cx_clear(PCI_INT_MSK, 1);
680ffd3c233SHans Verkuil 
681467870caSHans Verkuil 	if (video_is_registered(&dev->channels[chan_num].vdev)) {
682467870caSHans Verkuil 		video_unregister_device(&dev->channels[chan_num].vdev);
683f8d7ee70SHans Verkuil 		v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
684ffd3c233SHans Verkuil 	}
685ffd3c233SHans Verkuil }
686ffd3c233SHans Verkuil 
687ffd3c233SHans Verkuil int cx25821_video_register(struct cx25821_dev *dev)
688ffd3c233SHans Verkuil {
689ffd3c233SHans Verkuil 	int err;
690ffd3c233SHans Verkuil 	int i;
691ffd3c233SHans Verkuil 
692be178cb4SHans Verkuil 	/* initial device configuration */
693a6aa0dc4SHans Verkuil 	dev->tvnorm = V4L2_STD_NTSC_M;
694be178cb4SHans Verkuil 
695ffd3c233SHans Verkuil 	spin_lock_init(&dev->slock);
696ffd3c233SHans Verkuil 
697b6f21dc3SHans Verkuil 	for (i = 0; i < MAX_VID_CAP_CHANNEL_NUM - 1; ++i) {
6982efe2cc4SHans Verkuil 		struct cx25821_channel *chan = &dev->channels[i];
6992efe2cc4SHans Verkuil 		struct video_device *vdev = &chan->vdev;
7002efe2cc4SHans Verkuil 		struct v4l2_ctrl_handler *hdl = &chan->hdl;
701b671ae6bSHans Verkuil 		struct vb2_queue *q;
7021f198870SHans Verkuil 		bool is_output = i > SRAM_CH08;
703467870caSHans Verkuil 
704ffd3c233SHans Verkuil 		if (i == SRAM_CH08) /* audio channel */
705ffd3c233SHans Verkuil 			continue;
706ffd3c233SHans Verkuil 
7071f198870SHans Verkuil 		if (!is_output) {
708f8d7ee70SHans Verkuil 			v4l2_ctrl_handler_init(hdl, 4);
709f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
710f8d7ee70SHans Verkuil 					V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
711f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
712f8d7ee70SHans Verkuil 					V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
713f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
714f8d7ee70SHans Verkuil 					V4L2_CID_SATURATION, 0, 10000, 1, 5000);
715f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
716f8d7ee70SHans Verkuil 					V4L2_CID_HUE, 0, 10000, 1, 5000);
717f8d7ee70SHans Verkuil 			if (hdl->error) {
718f8d7ee70SHans Verkuil 				err = hdl->error;
719f8d7ee70SHans Verkuil 				goto fail_unreg;
720f8d7ee70SHans Verkuil 			}
721be178cb4SHans Verkuil 			err = v4l2_ctrl_handler_setup(hdl);
722be178cb4SHans Verkuil 			if (err)
723be178cb4SHans Verkuil 				goto fail_unreg;
7247087d31bSHans Verkuil 		} else {
7257087d31bSHans Verkuil 			chan->out = &dev->vid_out_data[i - SRAM_CH09];
7267087d31bSHans Verkuil 			chan->out->chan = chan;
7271f198870SHans Verkuil 		}
728ffd3c233SHans Verkuil 
7292efe2cc4SHans Verkuil 		chan->sram_channels = &cx25821_sram_channels[i];
7302efe2cc4SHans Verkuil 		chan->width = 720;
731b671ae6bSHans Verkuil 		chan->field = V4L2_FIELD_INTERLACED;
7322efe2cc4SHans Verkuil 		if (dev->tvnorm & V4L2_STD_625_50)
7332efe2cc4SHans Verkuil 			chan->height = 576;
7342efe2cc4SHans Verkuil 		else
7352efe2cc4SHans Verkuil 			chan->height = 480;
736ffd3c233SHans Verkuil 
7372efe2cc4SHans Verkuil 		if (chan->pixel_formats == PIXEL_FRMT_411)
7382efe2cc4SHans Verkuil 			chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
7392efe2cc4SHans Verkuil 		else
7402efe2cc4SHans Verkuil 			chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
741ffd3c233SHans Verkuil 
7422efe2cc4SHans Verkuil 		cx_write(chan->sram_channels->int_stat, 0xffffffff);
743ffd3c233SHans Verkuil 
7442efe2cc4SHans Verkuil 		INIT_LIST_HEAD(&chan->dma_vidq.active);
7452efe2cc4SHans Verkuil 
746b671ae6bSHans Verkuil 		q = &chan->vidq;
7472efe2cc4SHans Verkuil 
748b671ae6bSHans Verkuil 		q->type = is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
749b671ae6bSHans Verkuil 				      V4L2_BUF_TYPE_VIDEO_CAPTURE;
750b671ae6bSHans Verkuil 		q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
751b671ae6bSHans Verkuil 		q->io_modes |= is_output ? VB2_WRITE : VB2_READ;
752b671ae6bSHans Verkuil 		q->gfp_flags = GFP_DMA32;
753b671ae6bSHans Verkuil 		q->min_buffers_needed = 2;
754b671ae6bSHans Verkuil 		q->drv_priv = chan;
755b671ae6bSHans Verkuil 		q->buf_struct_size = sizeof(struct cx25821_buffer);
756b671ae6bSHans Verkuil 		q->ops = &cx25821_video_qops;
757b671ae6bSHans Verkuil 		q->mem_ops = &vb2_dma_sg_memops;
758b671ae6bSHans Verkuil 		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
759b671ae6bSHans Verkuil 		q->lock = &dev->lock;
760*2bc46b3aSHans Verkuil 		q->dev = &dev->pci->dev;
761b671ae6bSHans Verkuil 
762b671ae6bSHans Verkuil 		if (!is_output) {
763b671ae6bSHans Verkuil 			err = vb2_queue_init(q);
764b671ae6bSHans Verkuil 			if (err < 0)
765b671ae6bSHans Verkuil 				goto fail_unreg;
766b671ae6bSHans Verkuil 		}
767ffd3c233SHans Verkuil 
768ffd3c233SHans Verkuil 		/* register v4l devices */
7691f198870SHans Verkuil 		*vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
770467870caSHans Verkuil 		vdev->v4l2_dev = &dev->v4l2_dev;
7711f198870SHans Verkuil 		if (!is_output)
772f8d7ee70SHans Verkuil 			vdev->ctrl_handler = hdl;
7731f198870SHans Verkuil 		else
7741f198870SHans Verkuil 			vdev->vfl_dir = VFL_DIR_TX;
775be178cb4SHans Verkuil 		vdev->lock = &dev->lock;
776b671ae6bSHans Verkuil 		vdev->queue = q;
777467870caSHans Verkuil 		snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
7782efe2cc4SHans Verkuil 		video_set_drvdata(vdev, chan);
779ffd3c233SHans Verkuil 
780467870caSHans Verkuil 		err = video_register_device(vdev, VFL_TYPE_GRABBER,
781467870caSHans Verkuil 					    video_nr[dev->nr]);
782ffd3c233SHans Verkuil 
783ffd3c233SHans Verkuil 		if (err < 0)
784ffd3c233SHans Verkuil 			goto fail_unreg;
785ffd3c233SHans Verkuil 	}
786ffd3c233SHans Verkuil 
787ffd3c233SHans Verkuil 	/* set PCI interrupt */
788ffd3c233SHans Verkuil 	cx_set(PCI_INT_MSK, 0xff);
789ffd3c233SHans Verkuil 
790ffd3c233SHans Verkuil 	return 0;
791ffd3c233SHans Verkuil 
792ffd3c233SHans Verkuil fail_unreg:
793467870caSHans Verkuil 	while (i >= 0)
794467870caSHans Verkuil 		cx25821_video_unregister(dev, i--);
795ffd3c233SHans Verkuil 	return err;
796ffd3c233SHans Verkuil }
797