xref: /linux/drivers/media/pci/cx25821/cx25821-video.c (revision df9ecb0cad14b952a2865f8b3af86b2bbadfab45)
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 
1332d700715SJunghak Sung 			v4l2_get_timestamp(&buf->vb.timestamp);
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 
144*df9ecb0cSHans 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 
151*df9ecb0cSHans Verkuil 	alloc_ctxs[0] = chan->dev->alloc_ctx;
152*df9ecb0cSHans Verkuil 
153*df9ecb0cSHans Verkuil 	if (*num_planes)
154*df9ecb0cSHans Verkuil 		return sizes[0] < size ? -EINVAL : 0;
155b285192aSMauro Carvalho Chehab 
156b671ae6bSHans Verkuil 	*num_planes = 1;
157*df9ecb0cSHans Verkuil 	sizes[0] = size;
158b285192aSMauro Carvalho Chehab 	return 0;
159b285192aSMauro Carvalho Chehab }
160b285192aSMauro Carvalho Chehab 
161b671ae6bSHans Verkuil static int cx25821_buffer_prepare(struct vb2_buffer *vb)
162b285192aSMauro Carvalho Chehab {
1632d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
164b671ae6bSHans Verkuil 	struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
1652efe2cc4SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
166b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
1672d700715SJunghak Sung 		container_of(vbuf, struct cx25821_buffer, vb);
168b671ae6bSHans Verkuil 	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
169b285192aSMauro Carvalho Chehab 	u32 line0_offset;
170b285192aSMauro Carvalho Chehab 	int bpl_local = LINE_SIZE_D1;
171b671ae6bSHans Verkuil 	int ret;
172b285192aSMauro Carvalho Chehab 
1738d125c50SHans Verkuil 	if (chan->pixel_formats == PIXEL_FRMT_411)
174b671ae6bSHans Verkuil 		buf->bpl = (chan->fmt->depth * chan->width) >> 3;
175b285192aSMauro Carvalho Chehab 	else
176b671ae6bSHans Verkuil 		buf->bpl = (chan->fmt->depth >> 3) * chan->width;
177b671ae6bSHans Verkuil 
178b671ae6bSHans Verkuil 	if (vb2_plane_size(vb, 0) < chan->height * buf->bpl)
179b671ae6bSHans Verkuil 		return -EINVAL;
180b671ae6bSHans Verkuil 	vb2_set_plane_payload(vb, 0, chan->height * buf->bpl);
1812d700715SJunghak Sung 	buf->vb.field = chan->field;
182b285192aSMauro Carvalho Chehab 
1838d125c50SHans Verkuil 	if (chan->pixel_formats == PIXEL_FRMT_411) {
184b285192aSMauro Carvalho Chehab 		bpl_local = buf->bpl;
185b285192aSMauro Carvalho Chehab 	} else {
186b285192aSMauro Carvalho Chehab 		bpl_local = buf->bpl;   /* Default */
187b285192aSMauro Carvalho Chehab 
1888d125c50SHans Verkuil 		if (chan->use_cif_resolution) {
189988f7b80SHans Verkuil 			if (dev->tvnorm & V4L2_STD_625_50)
190b285192aSMauro Carvalho Chehab 				bpl_local = 352 << 1;
191b285192aSMauro Carvalho Chehab 			else
1928d125c50SHans Verkuil 				bpl_local = chan->cif_width << 1;
193b285192aSMauro Carvalho Chehab 		}
194b285192aSMauro Carvalho Chehab 	}
195b285192aSMauro Carvalho Chehab 
196b671ae6bSHans Verkuil 	switch (chan->field) {
197b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_TOP:
198b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
199b671ae6bSHans Verkuil 				sgt->sgl, 0, UNSET,
200b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height);
201b285192aSMauro Carvalho Chehab 		break;
202b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_BOTTOM:
203b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
204b671ae6bSHans Verkuil 				sgt->sgl, UNSET, 0,
205b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height);
206b285192aSMauro Carvalho Chehab 		break;
207b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_INTERLACED:
208b285192aSMauro Carvalho Chehab 		/* All other formats are top field first */
209b285192aSMauro Carvalho Chehab 		line0_offset = 0;
210b285192aSMauro Carvalho Chehab 		dprintk(1, "top field first\n");
211b285192aSMauro Carvalho Chehab 
212b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
213b671ae6bSHans Verkuil 				sgt->sgl, line0_offset,
214b285192aSMauro Carvalho Chehab 				bpl_local, bpl_local, bpl_local,
215b671ae6bSHans Verkuil 				chan->height >> 1);
216b285192aSMauro Carvalho Chehab 		break;
217b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_TB:
218b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
219b671ae6bSHans Verkuil 				sgt->sgl,
220b671ae6bSHans Verkuil 				0, buf->bpl * (chan->height >> 1),
221b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height >> 1);
222b285192aSMauro Carvalho Chehab 		break;
223b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_BT:
224b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
225b671ae6bSHans Verkuil 				sgt->sgl,
226b671ae6bSHans Verkuil 				buf->bpl * (chan->height >> 1), 0,
227b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height >> 1);
228b285192aSMauro Carvalho Chehab 		break;
229b285192aSMauro Carvalho Chehab 	default:
230b671ae6bSHans Verkuil 		WARN_ON(1);
231b671ae6bSHans Verkuil 		ret = -EINVAL;
232b671ae6bSHans Verkuil 		break;
233b285192aSMauro Carvalho Chehab 	}
234b285192aSMauro Carvalho Chehab 
235b285192aSMauro Carvalho Chehab 	dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
2362d700715SJunghak Sung 		buf, buf->vb.vb2_buf.index, chan->width, chan->height,
237b671ae6bSHans Verkuil 		chan->fmt->depth, chan->fmt->name,
238b671ae6bSHans Verkuil 		(unsigned long)buf->risc.dma);
239b285192aSMauro Carvalho Chehab 
240b671ae6bSHans Verkuil 	return ret;
241b285192aSMauro Carvalho Chehab }
242b285192aSMauro Carvalho Chehab 
243b671ae6bSHans Verkuil static void cx25821_buffer_finish(struct vb2_buffer *vb)
244b285192aSMauro Carvalho Chehab {
2452d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
246b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
2472d700715SJunghak Sung 		container_of(vbuf, struct cx25821_buffer, vb);
248b671ae6bSHans Verkuil 	struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
2492efe2cc4SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
250b671ae6bSHans Verkuil 
251b671ae6bSHans Verkuil 	cx25821_free_buffer(dev, buf);
252b671ae6bSHans Verkuil }
253b671ae6bSHans Verkuil 
254b671ae6bSHans Verkuil static void cx25821_buffer_queue(struct vb2_buffer *vb)
255b671ae6bSHans Verkuil {
2562d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
257b671ae6bSHans Verkuil 	struct cx25821_buffer *buf =
2582d700715SJunghak Sung 		container_of(vbuf, struct cx25821_buffer, vb);
259b671ae6bSHans Verkuil 	struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
260b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
261b671ae6bSHans Verkuil 	struct cx25821_buffer *prev;
2622efe2cc4SHans Verkuil 	struct cx25821_dmaqueue *q = &dev->channels[chan->id].dma_vidq;
263b285192aSMauro Carvalho Chehab 
264b671ae6bSHans Verkuil 	buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
265b671ae6bSHans Verkuil 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
266b671ae6bSHans Verkuil 	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
267b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
268b285192aSMauro Carvalho Chehab 
269b671ae6bSHans Verkuil 	if (list_empty(&q->active)) {
270b671ae6bSHans Verkuil 		list_add_tail(&buf->queue, &q->active);
271b285192aSMauro Carvalho Chehab 	} else {
272b671ae6bSHans Verkuil 		buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
273b285192aSMauro Carvalho Chehab 		prev = list_entry(q->active.prev, struct cx25821_buffer,
274b671ae6bSHans Verkuil 				queue);
275b671ae6bSHans Verkuil 		list_add_tail(&buf->queue, &q->active);
276b285192aSMauro Carvalho Chehab 		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
277b285192aSMauro Carvalho Chehab 	}
278b285192aSMauro Carvalho Chehab }
279b285192aSMauro Carvalho Chehab 
280b671ae6bSHans Verkuil static int cx25821_start_streaming(struct vb2_queue *q, unsigned int count)
281b671ae6bSHans Verkuil {
282b671ae6bSHans Verkuil 	struct cx25821_channel *chan = q->drv_priv;
283b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
284b671ae6bSHans Verkuil 	struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
285b671ae6bSHans Verkuil 	struct cx25821_buffer *buf = list_entry(dmaq->active.next,
286b671ae6bSHans Verkuil 			struct cx25821_buffer, queue);
287b671ae6bSHans Verkuil 
288b671ae6bSHans Verkuil 	dmaq->count = 0;
289b671ae6bSHans Verkuil 	cx25821_start_video_dma(dev, dmaq, buf, chan->sram_channels);
290b671ae6bSHans Verkuil 	return 0;
291b285192aSMauro Carvalho Chehab }
292b285192aSMauro Carvalho Chehab 
293b671ae6bSHans Verkuil static void cx25821_stop_streaming(struct vb2_queue *q)
294b671ae6bSHans Verkuil {
295b671ae6bSHans Verkuil 	struct cx25821_channel *chan = q->drv_priv;
296b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
297b671ae6bSHans Verkuil 	struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
298b671ae6bSHans Verkuil 	unsigned long flags;
299b671ae6bSHans Verkuil 
300b671ae6bSHans Verkuil 	cx_write(chan->sram_channels->dma_ctl, 0); /* FIFO and RISC disable */
301b671ae6bSHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
302b671ae6bSHans Verkuil 	while (!list_empty(&dmaq->active)) {
303b671ae6bSHans Verkuil 		struct cx25821_buffer *buf = list_entry(dmaq->active.next,
304b671ae6bSHans Verkuil 			struct cx25821_buffer, queue);
305b671ae6bSHans Verkuil 
306b671ae6bSHans Verkuil 		list_del(&buf->queue);
3072d700715SJunghak Sung 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
308b671ae6bSHans Verkuil 	}
309b671ae6bSHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
310b671ae6bSHans Verkuil }
311b671ae6bSHans Verkuil 
312b671ae6bSHans Verkuil static struct vb2_ops cx25821_video_qops = {
313b671ae6bSHans Verkuil 	.queue_setup    = cx25821_queue_setup,
314b285192aSMauro Carvalho Chehab 	.buf_prepare  = cx25821_buffer_prepare,
315b671ae6bSHans Verkuil 	.buf_finish = cx25821_buffer_finish,
316b671ae6bSHans Verkuil 	.buf_queue    = cx25821_buffer_queue,
317b671ae6bSHans Verkuil 	.wait_prepare = vb2_ops_wait_prepare,
318b671ae6bSHans Verkuil 	.wait_finish = vb2_ops_wait_finish,
319b671ae6bSHans Verkuil 	.start_streaming = cx25821_start_streaming,
320b671ae6bSHans Verkuil 	.stop_streaming = cx25821_stop_streaming,
321b285192aSMauro Carvalho Chehab };
322b285192aSMauro Carvalho Chehab 
32395c232a2SHans Verkuil /* VIDEO IOCTLS */
3244c1d0f73SHans Verkuil 
3254c1d0f73SHans Verkuil static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
3264c1d0f73SHans Verkuil 			    struct v4l2_fmtdesc *f)
3274c1d0f73SHans Verkuil {
3284c1d0f73SHans Verkuil 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
3294c1d0f73SHans Verkuil 		return -EINVAL;
3304c1d0f73SHans Verkuil 
3314c1d0f73SHans Verkuil 	strlcpy(f->description, formats[f->index].name, sizeof(f->description));
3324c1d0f73SHans Verkuil 	f->pixelformat = formats[f->index].fourcc;
3334c1d0f73SHans Verkuil 
3344c1d0f73SHans Verkuil 	return 0;
3354c1d0f73SHans Verkuil }
3364c1d0f73SHans Verkuil 
33795c232a2SHans Verkuil static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
33895c232a2SHans Verkuil 				 struct v4l2_format *f)
33995c232a2SHans Verkuil {
3402efe2cc4SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
34195c232a2SHans Verkuil 
3422efe2cc4SHans Verkuil 	f->fmt.pix.width = chan->width;
3432efe2cc4SHans Verkuil 	f->fmt.pix.height = chan->height;
344b671ae6bSHans Verkuil 	f->fmt.pix.field = chan->field;
3452efe2cc4SHans Verkuil 	f->fmt.pix.pixelformat = chan->fmt->fourcc;
346988f7b80SHans Verkuil 	f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
347988f7b80SHans Verkuil 	f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
348988f7b80SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
34995c232a2SHans Verkuil 
35095c232a2SHans Verkuil 	return 0;
35195c232a2SHans Verkuil }
35295c232a2SHans Verkuil 
35395c232a2SHans Verkuil static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
35495c232a2SHans Verkuil 				   struct v4l2_format *f)
35595c232a2SHans Verkuil {
356988f7b80SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
357988f7b80SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
35895c232a2SHans Verkuil 	const struct cx25821_fmt *fmt;
359988f7b80SHans Verkuil 	enum v4l2_field field = f->fmt.pix.field;
36066f93178SMauro Carvalho Chehab 	unsigned int maxh;
361988f7b80SHans Verkuil 	unsigned w;
36295c232a2SHans Verkuil 
36395c232a2SHans Verkuil 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
36495c232a2SHans Verkuil 	if (NULL == fmt)
36595c232a2SHans Verkuil 		return -EINVAL;
366988f7b80SHans Verkuil 	maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
36795c232a2SHans Verkuil 
368988f7b80SHans Verkuil 	w = f->fmt.pix.width;
369988f7b80SHans Verkuil 	if (field != V4L2_FIELD_BOTTOM)
37095c232a2SHans Verkuil 		field = V4L2_FIELD_TOP;
371988f7b80SHans Verkuil 	if (w < 352) {
372988f7b80SHans Verkuil 		w = 176;
373988f7b80SHans Verkuil 		f->fmt.pix.height = maxh / 4;
374988f7b80SHans Verkuil 	} else if (w < 720) {
375988f7b80SHans Verkuil 		w = 352;
376988f7b80SHans Verkuil 		f->fmt.pix.height = maxh / 2;
377988f7b80SHans Verkuil 	} else {
378988f7b80SHans Verkuil 		w = 720;
37995c232a2SHans Verkuil 		f->fmt.pix.height = maxh;
380988f7b80SHans Verkuil 		field = V4L2_FIELD_INTERLACED;
381988f7b80SHans Verkuil 	}
382988f7b80SHans Verkuil 	f->fmt.pix.field = field;
383988f7b80SHans Verkuil 	f->fmt.pix.width = w;
38495c232a2SHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
38595c232a2SHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
386988f7b80SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
38795c232a2SHans Verkuil 
38895c232a2SHans Verkuil 	return 0;
38995c232a2SHans Verkuil }
390b285192aSMauro Carvalho Chehab 
391b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
392b285192aSMauro Carvalho Chehab 				struct v4l2_format *f)
393b285192aSMauro Carvalho Chehab {
3942efe2cc4SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
3958d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
396b285192aSMauro Carvalho Chehab 	int pix_format = PIXEL_FRMT_422;
397a6aa0dc4SHans Verkuil 	int err;
398b285192aSMauro Carvalho Chehab 
399b285192aSMauro Carvalho Chehab 	err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
400b285192aSMauro Carvalho Chehab 
401b285192aSMauro Carvalho Chehab 	if (0 != err)
402b285192aSMauro Carvalho Chehab 		return err;
403b285192aSMauro Carvalho Chehab 
4042efe2cc4SHans Verkuil 	chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
405b671ae6bSHans Verkuil 	chan->field = f->fmt.pix.field;
4062efe2cc4SHans Verkuil 	chan->width = f->fmt.pix.width;
4072efe2cc4SHans Verkuil 	chan->height = f->fmt.pix.height;
408b285192aSMauro Carvalho Chehab 
409b285192aSMauro Carvalho Chehab 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
410b285192aSMauro Carvalho Chehab 		pix_format = PIXEL_FRMT_411;
411b285192aSMauro Carvalho Chehab 	else
412988f7b80SHans Verkuil 		pix_format = PIXEL_FRMT_422;
413b285192aSMauro Carvalho Chehab 
414b285192aSMauro Carvalho Chehab 	cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
415b285192aSMauro Carvalho Chehab 
416b285192aSMauro Carvalho Chehab 	/* check if cif resolution */
4172efe2cc4SHans Verkuil 	if (chan->width == 320 || chan->width == 352)
4182efe2cc4SHans Verkuil 		chan->use_cif_resolution = 1;
419b285192aSMauro Carvalho Chehab 	else
4202efe2cc4SHans Verkuil 		chan->use_cif_resolution = 0;
421b285192aSMauro Carvalho Chehab 
4222efe2cc4SHans Verkuil 	chan->cif_width = chan->width;
4232efe2cc4SHans Verkuil 	medusa_set_resolution(dev, chan->width, SRAM_CH00);
424b285192aSMauro Carvalho Chehab 	return 0;
425b285192aSMauro Carvalho Chehab }
426b285192aSMauro Carvalho Chehab 
427b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
428b285192aSMauro Carvalho Chehab {
4298d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4308d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
4318d125c50SHans Verkuil 	const struct sram_channel *sram_ch = chan->sram_channels;
432b285192aSMauro Carvalho Chehab 	u32 tmp = 0;
433b285192aSMauro Carvalho Chehab 
434b285192aSMauro Carvalho Chehab 	tmp = cx_read(sram_ch->dma_ctl);
435b285192aSMauro Carvalho Chehab 	pr_info("Video input 0 is %s\n",
436b285192aSMauro Carvalho Chehab 		(tmp & 0x11) ? "streaming" : "stopped");
437b285192aSMauro Carvalho Chehab 	return 0;
438b285192aSMauro Carvalho Chehab }
439b285192aSMauro Carvalho Chehab 
440b285192aSMauro Carvalho Chehab 
44195c232a2SHans Verkuil static int cx25821_vidioc_querycap(struct file *file, void *priv,
442b285192aSMauro Carvalho Chehab 			    struct v4l2_capability *cap)
443b285192aSMauro Carvalho Chehab {
4448d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4458d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
4463dd473caSHans Verkuil 	const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
4473dd473caSHans Verkuil 			V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
4480df13d99SHans Verkuil 	const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE;
449b285192aSMauro Carvalho Chehab 
450b285192aSMauro Carvalho Chehab 	strcpy(cap->driver, "cx25821");
451b285192aSMauro Carvalho Chehab 	strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
452b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
4538d125c50SHans Verkuil 	if (chan->id >= VID_CHANNEL_NUM)
4543dd473caSHans Verkuil 		cap->device_caps = cap_output;
4553dd473caSHans Verkuil 	else
4563dd473caSHans Verkuil 		cap->device_caps = cap_input;
4570df13d99SHans Verkuil 	cap->capabilities = cap_input | cap_output | V4L2_CAP_DEVICE_CAPS;
458b285192aSMauro Carvalho Chehab 	return 0;
459b285192aSMauro Carvalho Chehab }
460b285192aSMauro Carvalho Chehab 
46195c232a2SHans Verkuil static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
46218c73af6SHans Verkuil {
4638d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
46418c73af6SHans Verkuil 
4658d125c50SHans Verkuil 	*tvnorms = chan->dev->tvnorm;
46618c73af6SHans Verkuil 	return 0;
46718c73af6SHans Verkuil }
46818c73af6SHans Verkuil 
469a3f17af2SMauro Carvalho Chehab static int cx25821_vidioc_s_std(struct file *file, void *priv,
470a3f17af2SMauro Carvalho Chehab 				v4l2_std_id tvnorms)
471b285192aSMauro Carvalho Chehab {
4728d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4738d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
474b285192aSMauro Carvalho Chehab 
475314527acSHans Verkuil 	if (dev->tvnorm == tvnorms)
476b285192aSMauro Carvalho Chehab 		return 0;
477b285192aSMauro Carvalho Chehab 
478a6aa0dc4SHans Verkuil 	dev->tvnorm = tvnorms;
479988f7b80SHans Verkuil 	chan->width = 720;
480988f7b80SHans Verkuil 	chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
481b285192aSMauro Carvalho Chehab 
482b285192aSMauro Carvalho Chehab 	medusa_set_videostandard(dev);
483b285192aSMauro Carvalho Chehab 
484b285192aSMauro Carvalho Chehab 	return 0;
485b285192aSMauro Carvalho Chehab }
486b285192aSMauro Carvalho Chehab 
48795c232a2SHans Verkuil static int cx25821_vidioc_enum_input(struct file *file, void *priv,
48895c232a2SHans Verkuil 			      struct v4l2_input *i)
489b285192aSMauro Carvalho Chehab {
490a6aa0dc4SHans Verkuil 	if (i->index)
491b285192aSMauro Carvalho Chehab 		return -EINVAL;
492b285192aSMauro Carvalho Chehab 
493b285192aSMauro Carvalho Chehab 	i->type = V4L2_INPUT_TYPE_CAMERA;
494b285192aSMauro Carvalho Chehab 	i->std = CX25821_NORMS;
495a6aa0dc4SHans Verkuil 	strcpy(i->name, "Composite");
496b285192aSMauro Carvalho Chehab 	return 0;
497b285192aSMauro Carvalho Chehab }
498b285192aSMauro Carvalho Chehab 
49995c232a2SHans Verkuil static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
500b285192aSMauro Carvalho Chehab {
501a6aa0dc4SHans Verkuil 	*i = 0;
502b285192aSMauro Carvalho Chehab 	return 0;
503b285192aSMauro Carvalho Chehab }
504b285192aSMauro Carvalho Chehab 
50595c232a2SHans Verkuil static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
506b285192aSMauro Carvalho Chehab {
507a6aa0dc4SHans Verkuil 	return i ? -EINVAL : 0;
508b285192aSMauro Carvalho Chehab }
509b285192aSMauro Carvalho Chehab 
510f8d7ee70SHans Verkuil static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
511b285192aSMauro Carvalho Chehab {
512f8d7ee70SHans Verkuil 	struct cx25821_channel *chan =
513f8d7ee70SHans Verkuil 		container_of(ctrl->handler, struct cx25821_channel, hdl);
514f8d7ee70SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
515b285192aSMauro Carvalho Chehab 
516f8d7ee70SHans Verkuil 	switch (ctrl->id) {
517b285192aSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
518f8d7ee70SHans Verkuil 		medusa_set_brightness(dev, ctrl->val, chan->id);
519b285192aSMauro Carvalho Chehab 		break;
520b285192aSMauro Carvalho Chehab 	case V4L2_CID_HUE:
521f8d7ee70SHans Verkuil 		medusa_set_hue(dev, ctrl->val, chan->id);
522b285192aSMauro Carvalho Chehab 		break;
523b285192aSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
524f8d7ee70SHans Verkuil 		medusa_set_contrast(dev, ctrl->val, chan->id);
525b285192aSMauro Carvalho Chehab 		break;
526b285192aSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
527f8d7ee70SHans Verkuil 		medusa_set_saturation(dev, ctrl->val, chan->id);
528b285192aSMauro Carvalho Chehab 		break;
529b285192aSMauro Carvalho Chehab 	default:
530f8d7ee70SHans Verkuil 		return -EINVAL;
531b285192aSMauro Carvalho Chehab 	}
532f8d7ee70SHans Verkuil 	return 0;
533b285192aSMauro Carvalho Chehab }
534b285192aSMauro Carvalho Chehab 
5351f198870SHans Verkuil static int cx25821_vidioc_enum_output(struct file *file, void *priv,
5361f198870SHans Verkuil 			      struct v4l2_output *o)
537b285192aSMauro Carvalho Chehab {
5381f198870SHans Verkuil 	if (o->index)
5391f198870SHans Verkuil 		return -EINVAL;
540b285192aSMauro Carvalho Chehab 
5411f198870SHans Verkuil 	o->type = V4L2_INPUT_TYPE_CAMERA;
5421f198870SHans Verkuil 	o->std = CX25821_NORMS;
5431f198870SHans Verkuil 	strcpy(o->name, "Composite");
544b285192aSMauro Carvalho Chehab 	return 0;
545b285192aSMauro Carvalho Chehab }
546b285192aSMauro Carvalho Chehab 
5471f198870SHans Verkuil static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
548b285192aSMauro Carvalho Chehab {
5491f198870SHans Verkuil 	*o = 0;
550b285192aSMauro Carvalho Chehab 	return 0;
551b285192aSMauro Carvalho Chehab }
552b285192aSMauro Carvalho Chehab 
5531f198870SHans Verkuil static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
554b285192aSMauro Carvalho Chehab {
5551f198870SHans Verkuil 	return o ? -EINVAL : 0;
556b285192aSMauro Carvalho Chehab }
557b285192aSMauro Carvalho Chehab 
558e90878abSHans Verkuil static int cx25821_vidioc_try_fmt_vid_out(struct file *file, void *priv,
559e90878abSHans Verkuil 				   struct v4l2_format *f)
560e90878abSHans Verkuil {
561e90878abSHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
562e90878abSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
563e90878abSHans Verkuil 	const struct cx25821_fmt *fmt;
564e90878abSHans Verkuil 
565e90878abSHans Verkuil 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
566e90878abSHans Verkuil 	if (NULL == fmt)
567e90878abSHans Verkuil 		return -EINVAL;
568e90878abSHans Verkuil 	f->fmt.pix.width = 720;
569e90878abSHans Verkuil 	f->fmt.pix.height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
570e90878abSHans Verkuil 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
571e90878abSHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
572e90878abSHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
573e90878abSHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
574e90878abSHans Verkuil 	return 0;
575e90878abSHans Verkuil }
576e90878abSHans Verkuil 
577e90878abSHans Verkuil static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
578e90878abSHans Verkuil 				struct v4l2_format *f)
579e90878abSHans Verkuil {
580e90878abSHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
581e90878abSHans Verkuil 	int err;
582e90878abSHans Verkuil 
583e90878abSHans Verkuil 	err = cx25821_vidioc_try_fmt_vid_out(file, priv, f);
584e90878abSHans Verkuil 
585e90878abSHans Verkuil 	if (0 != err)
586e90878abSHans Verkuil 		return err;
587e90878abSHans Verkuil 
588e90878abSHans Verkuil 	chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
589b671ae6bSHans Verkuil 	chan->field = f->fmt.pix.field;
590e90878abSHans Verkuil 	chan->width = f->fmt.pix.width;
591e90878abSHans Verkuil 	chan->height = f->fmt.pix.height;
592e90878abSHans Verkuil 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
593e90878abSHans Verkuil 		chan->pixel_formats = PIXEL_FRMT_411;
594e90878abSHans Verkuil 	else
595e90878abSHans Verkuil 		chan->pixel_formats = PIXEL_FRMT_422;
596e90878abSHans Verkuil 	return 0;
597e90878abSHans Verkuil }
598e90878abSHans Verkuil 
599f8d7ee70SHans Verkuil static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
600f8d7ee70SHans Verkuil 	.s_ctrl = cx25821_s_ctrl,
601f8d7ee70SHans Verkuil };
602f8d7ee70SHans Verkuil 
603b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops = {
604b285192aSMauro Carvalho Chehab 	.owner = THIS_MODULE,
6058d125c50SHans Verkuil 	.open = v4l2_fh_open,
606b671ae6bSHans Verkuil 	.release        = vb2_fop_release,
607b671ae6bSHans Verkuil 	.read           = vb2_fop_read,
608b671ae6bSHans Verkuil 	.poll		= vb2_fop_poll,
6091f198870SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
610b671ae6bSHans Verkuil 	.mmap           = vb2_fop_mmap,
611b285192aSMauro Carvalho Chehab };
612b285192aSMauro Carvalho Chehab 
613b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops = {
614b285192aSMauro Carvalho Chehab 	.vidioc_querycap = cx25821_vidioc_querycap,
615b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
616b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
617b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
618b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
619b671ae6bSHans Verkuil 	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
620b671ae6bSHans Verkuil 	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
62111c8a2dfSHans Verkuil 	.vidioc_create_bufs   = vb2_ioctl_create_bufs,
622b671ae6bSHans Verkuil 	.vidioc_querybuf      = vb2_ioctl_querybuf,
623b671ae6bSHans Verkuil 	.vidioc_qbuf          = vb2_ioctl_qbuf,
624b671ae6bSHans Verkuil 	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
625b671ae6bSHans Verkuil 	.vidioc_streamon      = vb2_ioctl_streamon,
626b671ae6bSHans Verkuil 	.vidioc_streamoff     = vb2_ioctl_streamoff,
62718c73af6SHans Verkuil 	.vidioc_g_std = cx25821_vidioc_g_std,
628b285192aSMauro Carvalho Chehab 	.vidioc_s_std = cx25821_vidioc_s_std,
629b285192aSMauro Carvalho Chehab 	.vidioc_enum_input = cx25821_vidioc_enum_input,
630b285192aSMauro Carvalho Chehab 	.vidioc_g_input = cx25821_vidioc_g_input,
631b285192aSMauro Carvalho Chehab 	.vidioc_s_input = cx25821_vidioc_s_input,
632b285192aSMauro Carvalho Chehab 	.vidioc_log_status = vidioc_log_status,
6338d125c50SHans Verkuil 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
6348d125c50SHans Verkuil 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
635b285192aSMauro Carvalho Chehab };
636b285192aSMauro Carvalho Chehab 
637ffd3c233SHans Verkuil static const struct video_device cx25821_video_device = {
638ffd3c233SHans Verkuil 	.name = "cx25821-video",
639b285192aSMauro Carvalho Chehab 	.fops = &video_fops,
640467870caSHans Verkuil 	.release = video_device_release_empty,
641ffd3c233SHans Verkuil 	.minor = -1,
642b285192aSMauro Carvalho Chehab 	.ioctl_ops = &video_ioctl_ops,
643b285192aSMauro Carvalho Chehab 	.tvnorms = CX25821_NORMS,
644b285192aSMauro Carvalho Chehab };
645ffd3c233SHans Verkuil 
6461f198870SHans Verkuil static const struct v4l2_file_operations video_out_fops = {
6471f198870SHans Verkuil 	.owner = THIS_MODULE,
6481f198870SHans Verkuil 	.open = v4l2_fh_open,
649b671ae6bSHans Verkuil 	.release        = vb2_fop_release,
650b671ae6bSHans Verkuil 	.write          = vb2_fop_write,
651b671ae6bSHans Verkuil 	.poll		= vb2_fop_poll,
6521f198870SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
653b671ae6bSHans Verkuil 	.mmap           = vb2_fop_mmap,
6541f198870SHans Verkuil };
6551f198870SHans Verkuil 
6561f198870SHans Verkuil static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
6571f198870SHans Verkuil 	.vidioc_querycap = cx25821_vidioc_querycap,
658e90878abSHans Verkuil 	.vidioc_enum_fmt_vid_out = cx25821_vidioc_enum_fmt_vid_cap,
659e90878abSHans Verkuil 	.vidioc_g_fmt_vid_out = cx25821_vidioc_g_fmt_vid_cap,
660e90878abSHans Verkuil 	.vidioc_try_fmt_vid_out = cx25821_vidioc_try_fmt_vid_out,
661e90878abSHans Verkuil 	.vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
6621f198870SHans Verkuil 	.vidioc_g_std = cx25821_vidioc_g_std,
6631f198870SHans Verkuil 	.vidioc_s_std = cx25821_vidioc_s_std,
6641f198870SHans Verkuil 	.vidioc_enum_output = cx25821_vidioc_enum_output,
6651f198870SHans Verkuil 	.vidioc_g_output = cx25821_vidioc_g_output,
6661f198870SHans Verkuil 	.vidioc_s_output = cx25821_vidioc_s_output,
6671f198870SHans Verkuil 	.vidioc_log_status = vidioc_log_status,
6681f198870SHans Verkuil };
6691f198870SHans Verkuil 
6701f198870SHans Verkuil static const struct video_device cx25821_video_out_device = {
6711f198870SHans Verkuil 	.name = "cx25821-video",
6721f198870SHans Verkuil 	.fops = &video_out_fops,
6731f198870SHans Verkuil 	.release = video_device_release_empty,
6741f198870SHans Verkuil 	.minor = -1,
6751f198870SHans Verkuil 	.ioctl_ops = &video_out_ioctl_ops,
6761f198870SHans Verkuil 	.tvnorms = CX25821_NORMS,
6771f198870SHans Verkuil };
6781f198870SHans Verkuil 
679ffd3c233SHans Verkuil void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
680ffd3c233SHans Verkuil {
681ffd3c233SHans Verkuil 	cx_clear(PCI_INT_MSK, 1);
682ffd3c233SHans Verkuil 
683467870caSHans Verkuil 	if (video_is_registered(&dev->channels[chan_num].vdev)) {
684467870caSHans Verkuil 		video_unregister_device(&dev->channels[chan_num].vdev);
685f8d7ee70SHans Verkuil 		v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
686ffd3c233SHans Verkuil 	}
687ffd3c233SHans Verkuil }
688ffd3c233SHans Verkuil 
689ffd3c233SHans Verkuil int cx25821_video_register(struct cx25821_dev *dev)
690ffd3c233SHans Verkuil {
691ffd3c233SHans Verkuil 	int err;
692ffd3c233SHans Verkuil 	int i;
693ffd3c233SHans Verkuil 
694be178cb4SHans Verkuil 	/* initial device configuration */
695a6aa0dc4SHans Verkuil 	dev->tvnorm = V4L2_STD_NTSC_M;
696be178cb4SHans Verkuil 
697ffd3c233SHans Verkuil 	spin_lock_init(&dev->slock);
698ffd3c233SHans Verkuil 
699b6f21dc3SHans Verkuil 	for (i = 0; i < MAX_VID_CAP_CHANNEL_NUM - 1; ++i) {
7002efe2cc4SHans Verkuil 		struct cx25821_channel *chan = &dev->channels[i];
7012efe2cc4SHans Verkuil 		struct video_device *vdev = &chan->vdev;
7022efe2cc4SHans Verkuil 		struct v4l2_ctrl_handler *hdl = &chan->hdl;
703b671ae6bSHans Verkuil 		struct vb2_queue *q;
7041f198870SHans Verkuil 		bool is_output = i > SRAM_CH08;
705467870caSHans Verkuil 
706ffd3c233SHans Verkuil 		if (i == SRAM_CH08) /* audio channel */
707ffd3c233SHans Verkuil 			continue;
708ffd3c233SHans Verkuil 
7091f198870SHans Verkuil 		if (!is_output) {
710f8d7ee70SHans Verkuil 			v4l2_ctrl_handler_init(hdl, 4);
711f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
712f8d7ee70SHans Verkuil 					V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
713f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
714f8d7ee70SHans Verkuil 					V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
715f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
716f8d7ee70SHans Verkuil 					V4L2_CID_SATURATION, 0, 10000, 1, 5000);
717f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
718f8d7ee70SHans Verkuil 					V4L2_CID_HUE, 0, 10000, 1, 5000);
719f8d7ee70SHans Verkuil 			if (hdl->error) {
720f8d7ee70SHans Verkuil 				err = hdl->error;
721f8d7ee70SHans Verkuil 				goto fail_unreg;
722f8d7ee70SHans Verkuil 			}
723be178cb4SHans Verkuil 			err = v4l2_ctrl_handler_setup(hdl);
724be178cb4SHans Verkuil 			if (err)
725be178cb4SHans Verkuil 				goto fail_unreg;
7267087d31bSHans Verkuil 		} else {
7277087d31bSHans Verkuil 			chan->out = &dev->vid_out_data[i - SRAM_CH09];
7287087d31bSHans Verkuil 			chan->out->chan = chan;
7291f198870SHans Verkuil 		}
730ffd3c233SHans Verkuil 
7312efe2cc4SHans Verkuil 		chan->sram_channels = &cx25821_sram_channels[i];
7322efe2cc4SHans Verkuil 		chan->width = 720;
733b671ae6bSHans Verkuil 		chan->field = V4L2_FIELD_INTERLACED;
7342efe2cc4SHans Verkuil 		if (dev->tvnorm & V4L2_STD_625_50)
7352efe2cc4SHans Verkuil 			chan->height = 576;
7362efe2cc4SHans Verkuil 		else
7372efe2cc4SHans Verkuil 			chan->height = 480;
738ffd3c233SHans Verkuil 
7392efe2cc4SHans Verkuil 		if (chan->pixel_formats == PIXEL_FRMT_411)
7402efe2cc4SHans Verkuil 			chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
7412efe2cc4SHans Verkuil 		else
7422efe2cc4SHans Verkuil 			chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
743ffd3c233SHans Verkuil 
7442efe2cc4SHans Verkuil 		cx_write(chan->sram_channels->int_stat, 0xffffffff);
745ffd3c233SHans Verkuil 
7462efe2cc4SHans Verkuil 		INIT_LIST_HEAD(&chan->dma_vidq.active);
7472efe2cc4SHans Verkuil 
748b671ae6bSHans Verkuil 		q = &chan->vidq;
7492efe2cc4SHans Verkuil 
750b671ae6bSHans Verkuil 		q->type = is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
751b671ae6bSHans Verkuil 				      V4L2_BUF_TYPE_VIDEO_CAPTURE;
752b671ae6bSHans Verkuil 		q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
753b671ae6bSHans Verkuil 		q->io_modes |= is_output ? VB2_WRITE : VB2_READ;
754b671ae6bSHans Verkuil 		q->gfp_flags = GFP_DMA32;
755b671ae6bSHans Verkuil 		q->min_buffers_needed = 2;
756b671ae6bSHans Verkuil 		q->drv_priv = chan;
757b671ae6bSHans Verkuil 		q->buf_struct_size = sizeof(struct cx25821_buffer);
758b671ae6bSHans Verkuil 		q->ops = &cx25821_video_qops;
759b671ae6bSHans Verkuil 		q->mem_ops = &vb2_dma_sg_memops;
760b671ae6bSHans Verkuil 		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
761b671ae6bSHans Verkuil 		q->lock = &dev->lock;
762b671ae6bSHans Verkuil 
763b671ae6bSHans Verkuil 		if (!is_output) {
764b671ae6bSHans Verkuil 			err = vb2_queue_init(q);
765b671ae6bSHans Verkuil 			if (err < 0)
766b671ae6bSHans Verkuil 				goto fail_unreg;
767b671ae6bSHans Verkuil 		}
768ffd3c233SHans Verkuil 
769ffd3c233SHans Verkuil 		/* register v4l devices */
7701f198870SHans Verkuil 		*vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
771467870caSHans Verkuil 		vdev->v4l2_dev = &dev->v4l2_dev;
7721f198870SHans Verkuil 		if (!is_output)
773f8d7ee70SHans Verkuil 			vdev->ctrl_handler = hdl;
7741f198870SHans Verkuil 		else
7751f198870SHans Verkuil 			vdev->vfl_dir = VFL_DIR_TX;
776be178cb4SHans Verkuil 		vdev->lock = &dev->lock;
777b671ae6bSHans Verkuil 		vdev->queue = q;
778467870caSHans Verkuil 		snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
7792efe2cc4SHans Verkuil 		video_set_drvdata(vdev, chan);
780ffd3c233SHans Verkuil 
781467870caSHans Verkuil 		err = video_register_device(vdev, VFL_TYPE_GRABBER,
782467870caSHans Verkuil 					    video_nr[dev->nr]);
783ffd3c233SHans Verkuil 
784ffd3c233SHans Verkuil 		if (err < 0)
785ffd3c233SHans Verkuil 			goto fail_unreg;
786ffd3c233SHans Verkuil 	}
787ffd3c233SHans Verkuil 
788ffd3c233SHans Verkuil 	/* set PCI interrupt */
789ffd3c233SHans Verkuil 	cx_set(PCI_INT_MSK, 0xff);
790ffd3c233SHans Verkuil 
791ffd3c233SHans Verkuil 	return 0;
792ffd3c233SHans Verkuil 
793ffd3c233SHans Verkuil fail_unreg:
794467870caSHans Verkuil 	while (i >= 0)
795467870caSHans Verkuil 		cx25821_video_unregister(dev, i--);
796ffd3c233SHans Verkuil 	return err;
797ffd3c233SHans Verkuil }
798