xref: /linux/drivers/media/pci/cx25821/cx25821-video.c (revision 11c8a2df2cf8c213e1970d548aee067887971ef8)
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 
133b671ae6bSHans Verkuil 			v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
134b671ae6bSHans Verkuil 			buf->vb.v4l2_buf.sequence = dmaq->count++;
135b671ae6bSHans Verkuil 			list_del(&buf->queue);
136b671ae6bSHans Verkuil 			vb2_buffer_done(&buf->vb, 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 
144b671ae6bSHans Verkuil static int cx25821_queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
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;
149*11c8a2dfSHans Verkuil 	unsigned size = (chan->fmt->depth * chan->width * chan->height) >> 3;
150*11c8a2dfSHans Verkuil 
151*11c8a2dfSHans Verkuil 	if (fmt && fmt->fmt.pix.sizeimage < size)
152*11c8a2dfSHans Verkuil 		return -EINVAL;
153b285192aSMauro Carvalho Chehab 
154b671ae6bSHans Verkuil 	*num_planes = 1;
155*11c8a2dfSHans Verkuil 	sizes[0] = fmt ? fmt->fmt.pix.sizeimage : size;
156b671ae6bSHans Verkuil 	alloc_ctxs[0] = chan->dev->alloc_ctx;
157b285192aSMauro Carvalho Chehab 	return 0;
158b285192aSMauro Carvalho Chehab }
159b285192aSMauro Carvalho Chehab 
160b671ae6bSHans Verkuil static int cx25821_buffer_prepare(struct vb2_buffer *vb)
161b285192aSMauro Carvalho Chehab {
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 =
165b285192aSMauro Carvalho Chehab 		container_of(vb, 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);
179b671ae6bSHans Verkuil 	buf->vb.v4l2_buf.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",
234b671ae6bSHans Verkuil 		buf, buf->vb.v4l2_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 {
243b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
244b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
245b671ae6bSHans Verkuil 	struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
2462efe2cc4SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
247b671ae6bSHans Verkuil 
248b671ae6bSHans Verkuil 	cx25821_free_buffer(dev, buf);
249b671ae6bSHans Verkuil }
250b671ae6bSHans Verkuil 
251b671ae6bSHans Verkuil static void cx25821_buffer_queue(struct vb2_buffer *vb)
252b671ae6bSHans Verkuil {
253b671ae6bSHans Verkuil 	struct cx25821_buffer *buf =
254b671ae6bSHans Verkuil 		container_of(vb, struct cx25821_buffer, vb);
255b671ae6bSHans Verkuil 	struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
256b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
257b671ae6bSHans Verkuil 	struct cx25821_buffer *prev;
2582efe2cc4SHans Verkuil 	struct cx25821_dmaqueue *q = &dev->channels[chan->id].dma_vidq;
259b285192aSMauro Carvalho Chehab 
260b671ae6bSHans Verkuil 	buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
261b671ae6bSHans Verkuil 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
262b671ae6bSHans Verkuil 	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
263b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
264b285192aSMauro Carvalho Chehab 
265b671ae6bSHans Verkuil 	if (list_empty(&q->active)) {
266b671ae6bSHans Verkuil 		list_add_tail(&buf->queue, &q->active);
267b285192aSMauro Carvalho Chehab 	} else {
268b671ae6bSHans Verkuil 		buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
269b285192aSMauro Carvalho Chehab 		prev = list_entry(q->active.prev, struct cx25821_buffer,
270b671ae6bSHans Verkuil 				queue);
271b671ae6bSHans Verkuil 		list_add_tail(&buf->queue, &q->active);
272b285192aSMauro Carvalho Chehab 		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
273b285192aSMauro Carvalho Chehab 	}
274b285192aSMauro Carvalho Chehab }
275b285192aSMauro Carvalho Chehab 
276b671ae6bSHans Verkuil static int cx25821_start_streaming(struct vb2_queue *q, unsigned int count)
277b671ae6bSHans Verkuil {
278b671ae6bSHans Verkuil 	struct cx25821_channel *chan = q->drv_priv;
279b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
280b671ae6bSHans Verkuil 	struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
281b671ae6bSHans Verkuil 	struct cx25821_buffer *buf = list_entry(dmaq->active.next,
282b671ae6bSHans Verkuil 			struct cx25821_buffer, queue);
283b671ae6bSHans Verkuil 
284b671ae6bSHans Verkuil 	dmaq->count = 0;
285b671ae6bSHans Verkuil 	cx25821_start_video_dma(dev, dmaq, buf, chan->sram_channels);
286b671ae6bSHans Verkuil 	return 0;
287b285192aSMauro Carvalho Chehab }
288b285192aSMauro Carvalho Chehab 
289b671ae6bSHans Verkuil static void cx25821_stop_streaming(struct vb2_queue *q)
290b671ae6bSHans Verkuil {
291b671ae6bSHans Verkuil 	struct cx25821_channel *chan = q->drv_priv;
292b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
293b671ae6bSHans Verkuil 	struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
294b671ae6bSHans Verkuil 	unsigned long flags;
295b671ae6bSHans Verkuil 
296b671ae6bSHans Verkuil 	cx_write(chan->sram_channels->dma_ctl, 0); /* FIFO and RISC disable */
297b671ae6bSHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
298b671ae6bSHans Verkuil 	while (!list_empty(&dmaq->active)) {
299b671ae6bSHans Verkuil 		struct cx25821_buffer *buf = list_entry(dmaq->active.next,
300b671ae6bSHans Verkuil 			struct cx25821_buffer, queue);
301b671ae6bSHans Verkuil 
302b671ae6bSHans Verkuil 		list_del(&buf->queue);
303b671ae6bSHans Verkuil 		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
304b671ae6bSHans Verkuil 	}
305b671ae6bSHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
306b671ae6bSHans Verkuil }
307b671ae6bSHans Verkuil 
308b671ae6bSHans Verkuil static struct vb2_ops cx25821_video_qops = {
309b671ae6bSHans Verkuil 	.queue_setup    = cx25821_queue_setup,
310b285192aSMauro Carvalho Chehab 	.buf_prepare  = cx25821_buffer_prepare,
311b671ae6bSHans Verkuil 	.buf_finish = cx25821_buffer_finish,
312b671ae6bSHans Verkuil 	.buf_queue    = cx25821_buffer_queue,
313b671ae6bSHans Verkuil 	.wait_prepare = vb2_ops_wait_prepare,
314b671ae6bSHans Verkuil 	.wait_finish = vb2_ops_wait_finish,
315b671ae6bSHans Verkuil 	.start_streaming = cx25821_start_streaming,
316b671ae6bSHans Verkuil 	.stop_streaming = cx25821_stop_streaming,
317b285192aSMauro Carvalho Chehab };
318b285192aSMauro Carvalho Chehab 
31995c232a2SHans Verkuil /* VIDEO IOCTLS */
3204c1d0f73SHans Verkuil 
3214c1d0f73SHans Verkuil static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
3224c1d0f73SHans Verkuil 			    struct v4l2_fmtdesc *f)
3234c1d0f73SHans Verkuil {
3244c1d0f73SHans Verkuil 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
3254c1d0f73SHans Verkuil 		return -EINVAL;
3264c1d0f73SHans Verkuil 
3274c1d0f73SHans Verkuil 	strlcpy(f->description, formats[f->index].name, sizeof(f->description));
3284c1d0f73SHans Verkuil 	f->pixelformat = formats[f->index].fourcc;
3294c1d0f73SHans Verkuil 
3304c1d0f73SHans Verkuil 	return 0;
3314c1d0f73SHans Verkuil }
3324c1d0f73SHans Verkuil 
33395c232a2SHans Verkuil static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
33495c232a2SHans Verkuil 				 struct v4l2_format *f)
33595c232a2SHans Verkuil {
3362efe2cc4SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
33795c232a2SHans Verkuil 
3382efe2cc4SHans Verkuil 	f->fmt.pix.width = chan->width;
3392efe2cc4SHans Verkuil 	f->fmt.pix.height = chan->height;
340b671ae6bSHans Verkuil 	f->fmt.pix.field = chan->field;
3412efe2cc4SHans Verkuil 	f->fmt.pix.pixelformat = chan->fmt->fourcc;
342988f7b80SHans Verkuil 	f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
343988f7b80SHans Verkuil 	f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
344988f7b80SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
34595c232a2SHans Verkuil 
34695c232a2SHans Verkuil 	return 0;
34795c232a2SHans Verkuil }
34895c232a2SHans Verkuil 
34995c232a2SHans Verkuil static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
35095c232a2SHans Verkuil 				   struct v4l2_format *f)
35195c232a2SHans Verkuil {
352988f7b80SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
353988f7b80SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
35495c232a2SHans Verkuil 	const struct cx25821_fmt *fmt;
355988f7b80SHans Verkuil 	enum v4l2_field field = f->fmt.pix.field;
35666f93178SMauro Carvalho Chehab 	unsigned int maxh;
357988f7b80SHans Verkuil 	unsigned w;
35895c232a2SHans Verkuil 
35995c232a2SHans Verkuil 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
36095c232a2SHans Verkuil 	if (NULL == fmt)
36195c232a2SHans Verkuil 		return -EINVAL;
362988f7b80SHans Verkuil 	maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
36395c232a2SHans Verkuil 
364988f7b80SHans Verkuil 	w = f->fmt.pix.width;
365988f7b80SHans Verkuil 	if (field != V4L2_FIELD_BOTTOM)
36695c232a2SHans Verkuil 		field = V4L2_FIELD_TOP;
367988f7b80SHans Verkuil 	if (w < 352) {
368988f7b80SHans Verkuil 		w = 176;
369988f7b80SHans Verkuil 		f->fmt.pix.height = maxh / 4;
370988f7b80SHans Verkuil 	} else if (w < 720) {
371988f7b80SHans Verkuil 		w = 352;
372988f7b80SHans Verkuil 		f->fmt.pix.height = maxh / 2;
373988f7b80SHans Verkuil 	} else {
374988f7b80SHans Verkuil 		w = 720;
37595c232a2SHans Verkuil 		f->fmt.pix.height = maxh;
376988f7b80SHans Verkuil 		field = V4L2_FIELD_INTERLACED;
377988f7b80SHans Verkuil 	}
378988f7b80SHans Verkuil 	f->fmt.pix.field = field;
379988f7b80SHans Verkuil 	f->fmt.pix.width = w;
38095c232a2SHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
38195c232a2SHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
382988f7b80SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
38395c232a2SHans Verkuil 
38495c232a2SHans Verkuil 	return 0;
38595c232a2SHans Verkuil }
386b285192aSMauro Carvalho Chehab 
387b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
388b285192aSMauro Carvalho Chehab 				struct v4l2_format *f)
389b285192aSMauro Carvalho Chehab {
3902efe2cc4SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
3918d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
392b285192aSMauro Carvalho Chehab 	int pix_format = PIXEL_FRMT_422;
393a6aa0dc4SHans Verkuil 	int err;
394b285192aSMauro Carvalho Chehab 
395b285192aSMauro Carvalho Chehab 	err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
396b285192aSMauro Carvalho Chehab 
397b285192aSMauro Carvalho Chehab 	if (0 != err)
398b285192aSMauro Carvalho Chehab 		return err;
399b285192aSMauro Carvalho Chehab 
4002efe2cc4SHans Verkuil 	chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
401b671ae6bSHans Verkuil 	chan->field = f->fmt.pix.field;
4022efe2cc4SHans Verkuil 	chan->width = f->fmt.pix.width;
4032efe2cc4SHans Verkuil 	chan->height = f->fmt.pix.height;
404b285192aSMauro Carvalho Chehab 
405b285192aSMauro Carvalho Chehab 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
406b285192aSMauro Carvalho Chehab 		pix_format = PIXEL_FRMT_411;
407b285192aSMauro Carvalho Chehab 	else
408988f7b80SHans Verkuil 		pix_format = PIXEL_FRMT_422;
409b285192aSMauro Carvalho Chehab 
410b285192aSMauro Carvalho Chehab 	cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
411b285192aSMauro Carvalho Chehab 
412b285192aSMauro Carvalho Chehab 	/* check if cif resolution */
4132efe2cc4SHans Verkuil 	if (chan->width == 320 || chan->width == 352)
4142efe2cc4SHans Verkuil 		chan->use_cif_resolution = 1;
415b285192aSMauro Carvalho Chehab 	else
4162efe2cc4SHans Verkuil 		chan->use_cif_resolution = 0;
417b285192aSMauro Carvalho Chehab 
4182efe2cc4SHans Verkuil 	chan->cif_width = chan->width;
4192efe2cc4SHans Verkuil 	medusa_set_resolution(dev, chan->width, SRAM_CH00);
420b285192aSMauro Carvalho Chehab 	return 0;
421b285192aSMauro Carvalho Chehab }
422b285192aSMauro Carvalho Chehab 
423b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
424b285192aSMauro Carvalho Chehab {
4258d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4268d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
4278d125c50SHans Verkuil 	const struct sram_channel *sram_ch = chan->sram_channels;
428b285192aSMauro Carvalho Chehab 	u32 tmp = 0;
429b285192aSMauro Carvalho Chehab 
430b285192aSMauro Carvalho Chehab 	tmp = cx_read(sram_ch->dma_ctl);
431b285192aSMauro Carvalho Chehab 	pr_info("Video input 0 is %s\n",
432b285192aSMauro Carvalho Chehab 		(tmp & 0x11) ? "streaming" : "stopped");
433b285192aSMauro Carvalho Chehab 	return 0;
434b285192aSMauro Carvalho Chehab }
435b285192aSMauro Carvalho Chehab 
436b285192aSMauro Carvalho Chehab 
43795c232a2SHans Verkuil static int cx25821_vidioc_querycap(struct file *file, void *priv,
438b285192aSMauro Carvalho Chehab 			    struct v4l2_capability *cap)
439b285192aSMauro Carvalho Chehab {
4408d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4418d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
4423dd473caSHans Verkuil 	const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
4433dd473caSHans Verkuil 			V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
4440df13d99SHans Verkuil 	const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE;
445b285192aSMauro Carvalho Chehab 
446b285192aSMauro Carvalho Chehab 	strcpy(cap->driver, "cx25821");
447b285192aSMauro Carvalho Chehab 	strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
448b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
4498d125c50SHans Verkuil 	if (chan->id >= VID_CHANNEL_NUM)
4503dd473caSHans Verkuil 		cap->device_caps = cap_output;
4513dd473caSHans Verkuil 	else
4523dd473caSHans Verkuil 		cap->device_caps = cap_input;
4530df13d99SHans Verkuil 	cap->capabilities = cap_input | cap_output | V4L2_CAP_DEVICE_CAPS;
454b285192aSMauro Carvalho Chehab 	return 0;
455b285192aSMauro Carvalho Chehab }
456b285192aSMauro Carvalho Chehab 
45795c232a2SHans Verkuil static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
45818c73af6SHans Verkuil {
4598d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
46018c73af6SHans Verkuil 
4618d125c50SHans Verkuil 	*tvnorms = chan->dev->tvnorm;
46218c73af6SHans Verkuil 	return 0;
46318c73af6SHans Verkuil }
46418c73af6SHans Verkuil 
465a3f17af2SMauro Carvalho Chehab static int cx25821_vidioc_s_std(struct file *file, void *priv,
466a3f17af2SMauro Carvalho Chehab 				v4l2_std_id tvnorms)
467b285192aSMauro Carvalho Chehab {
4688d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4698d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
470b285192aSMauro Carvalho Chehab 
471314527acSHans Verkuil 	if (dev->tvnorm == tvnorms)
472b285192aSMauro Carvalho Chehab 		return 0;
473b285192aSMauro Carvalho Chehab 
474a6aa0dc4SHans Verkuil 	dev->tvnorm = tvnorms;
475988f7b80SHans Verkuil 	chan->width = 720;
476988f7b80SHans Verkuil 	chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
477b285192aSMauro Carvalho Chehab 
478b285192aSMauro Carvalho Chehab 	medusa_set_videostandard(dev);
479b285192aSMauro Carvalho Chehab 
480b285192aSMauro Carvalho Chehab 	return 0;
481b285192aSMauro Carvalho Chehab }
482b285192aSMauro Carvalho Chehab 
48395c232a2SHans Verkuil static int cx25821_vidioc_enum_input(struct file *file, void *priv,
48495c232a2SHans Verkuil 			      struct v4l2_input *i)
485b285192aSMauro Carvalho Chehab {
486a6aa0dc4SHans Verkuil 	if (i->index)
487b285192aSMauro Carvalho Chehab 		return -EINVAL;
488b285192aSMauro Carvalho Chehab 
489b285192aSMauro Carvalho Chehab 	i->type = V4L2_INPUT_TYPE_CAMERA;
490b285192aSMauro Carvalho Chehab 	i->std = CX25821_NORMS;
491a6aa0dc4SHans Verkuil 	strcpy(i->name, "Composite");
492b285192aSMauro Carvalho Chehab 	return 0;
493b285192aSMauro Carvalho Chehab }
494b285192aSMauro Carvalho Chehab 
49595c232a2SHans Verkuil static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
496b285192aSMauro Carvalho Chehab {
497a6aa0dc4SHans Verkuil 	*i = 0;
498b285192aSMauro Carvalho Chehab 	return 0;
499b285192aSMauro Carvalho Chehab }
500b285192aSMauro Carvalho Chehab 
50195c232a2SHans Verkuil static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
502b285192aSMauro Carvalho Chehab {
503a6aa0dc4SHans Verkuil 	return i ? -EINVAL : 0;
504b285192aSMauro Carvalho Chehab }
505b285192aSMauro Carvalho Chehab 
506f8d7ee70SHans Verkuil static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
507b285192aSMauro Carvalho Chehab {
508f8d7ee70SHans Verkuil 	struct cx25821_channel *chan =
509f8d7ee70SHans Verkuil 		container_of(ctrl->handler, struct cx25821_channel, hdl);
510f8d7ee70SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
511b285192aSMauro Carvalho Chehab 
512f8d7ee70SHans Verkuil 	switch (ctrl->id) {
513b285192aSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
514f8d7ee70SHans Verkuil 		medusa_set_brightness(dev, ctrl->val, chan->id);
515b285192aSMauro Carvalho Chehab 		break;
516b285192aSMauro Carvalho Chehab 	case V4L2_CID_HUE:
517f8d7ee70SHans Verkuil 		medusa_set_hue(dev, ctrl->val, chan->id);
518b285192aSMauro Carvalho Chehab 		break;
519b285192aSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
520f8d7ee70SHans Verkuil 		medusa_set_contrast(dev, ctrl->val, chan->id);
521b285192aSMauro Carvalho Chehab 		break;
522b285192aSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
523f8d7ee70SHans Verkuil 		medusa_set_saturation(dev, ctrl->val, chan->id);
524b285192aSMauro Carvalho Chehab 		break;
525b285192aSMauro Carvalho Chehab 	default:
526f8d7ee70SHans Verkuil 		return -EINVAL;
527b285192aSMauro Carvalho Chehab 	}
528f8d7ee70SHans Verkuil 	return 0;
529b285192aSMauro Carvalho Chehab }
530b285192aSMauro Carvalho Chehab 
5311f198870SHans Verkuil static int cx25821_vidioc_enum_output(struct file *file, void *priv,
5321f198870SHans Verkuil 			      struct v4l2_output *o)
533b285192aSMauro Carvalho Chehab {
5341f198870SHans Verkuil 	if (o->index)
5351f198870SHans Verkuil 		return -EINVAL;
536b285192aSMauro Carvalho Chehab 
5371f198870SHans Verkuil 	o->type = V4L2_INPUT_TYPE_CAMERA;
5381f198870SHans Verkuil 	o->std = CX25821_NORMS;
5391f198870SHans Verkuil 	strcpy(o->name, "Composite");
540b285192aSMauro Carvalho Chehab 	return 0;
541b285192aSMauro Carvalho Chehab }
542b285192aSMauro Carvalho Chehab 
5431f198870SHans Verkuil static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
544b285192aSMauro Carvalho Chehab {
5451f198870SHans Verkuil 	*o = 0;
546b285192aSMauro Carvalho Chehab 	return 0;
547b285192aSMauro Carvalho Chehab }
548b285192aSMauro Carvalho Chehab 
5491f198870SHans Verkuil static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
550b285192aSMauro Carvalho Chehab {
5511f198870SHans Verkuil 	return o ? -EINVAL : 0;
552b285192aSMauro Carvalho Chehab }
553b285192aSMauro Carvalho Chehab 
554e90878abSHans Verkuil static int cx25821_vidioc_try_fmt_vid_out(struct file *file, void *priv,
555e90878abSHans Verkuil 				   struct v4l2_format *f)
556e90878abSHans Verkuil {
557e90878abSHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
558e90878abSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
559e90878abSHans Verkuil 	const struct cx25821_fmt *fmt;
560e90878abSHans Verkuil 
561e90878abSHans Verkuil 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
562e90878abSHans Verkuil 	if (NULL == fmt)
563e90878abSHans Verkuil 		return -EINVAL;
564e90878abSHans Verkuil 	f->fmt.pix.width = 720;
565e90878abSHans Verkuil 	f->fmt.pix.height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
566e90878abSHans Verkuil 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
567e90878abSHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
568e90878abSHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
569e90878abSHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
570e90878abSHans Verkuil 	return 0;
571e90878abSHans Verkuil }
572e90878abSHans Verkuil 
573e90878abSHans Verkuil static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
574e90878abSHans Verkuil 				struct v4l2_format *f)
575e90878abSHans Verkuil {
576e90878abSHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
577e90878abSHans Verkuil 	int err;
578e90878abSHans Verkuil 
579e90878abSHans Verkuil 	err = cx25821_vidioc_try_fmt_vid_out(file, priv, f);
580e90878abSHans Verkuil 
581e90878abSHans Verkuil 	if (0 != err)
582e90878abSHans Verkuil 		return err;
583e90878abSHans Verkuil 
584e90878abSHans Verkuil 	chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
585b671ae6bSHans Verkuil 	chan->field = f->fmt.pix.field;
586e90878abSHans Verkuil 	chan->width = f->fmt.pix.width;
587e90878abSHans Verkuil 	chan->height = f->fmt.pix.height;
588e90878abSHans Verkuil 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
589e90878abSHans Verkuil 		chan->pixel_formats = PIXEL_FRMT_411;
590e90878abSHans Verkuil 	else
591e90878abSHans Verkuil 		chan->pixel_formats = PIXEL_FRMT_422;
592e90878abSHans Verkuil 	return 0;
593e90878abSHans Verkuil }
594e90878abSHans Verkuil 
595f8d7ee70SHans Verkuil static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
596f8d7ee70SHans Verkuil 	.s_ctrl = cx25821_s_ctrl,
597f8d7ee70SHans Verkuil };
598f8d7ee70SHans Verkuil 
599b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops = {
600b285192aSMauro Carvalho Chehab 	.owner = THIS_MODULE,
6018d125c50SHans Verkuil 	.open = v4l2_fh_open,
602b671ae6bSHans Verkuil 	.release        = vb2_fop_release,
603b671ae6bSHans Verkuil 	.read           = vb2_fop_read,
604b671ae6bSHans Verkuil 	.poll		= vb2_fop_poll,
6051f198870SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
606b671ae6bSHans Verkuil 	.mmap           = vb2_fop_mmap,
607b285192aSMauro Carvalho Chehab };
608b285192aSMauro Carvalho Chehab 
609b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops = {
610b285192aSMauro Carvalho Chehab 	.vidioc_querycap = cx25821_vidioc_querycap,
611b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
612b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
613b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
614b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
615b671ae6bSHans Verkuil 	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
616b671ae6bSHans Verkuil 	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
617*11c8a2dfSHans Verkuil 	.vidioc_create_bufs   = vb2_ioctl_create_bufs,
618b671ae6bSHans Verkuil 	.vidioc_querybuf      = vb2_ioctl_querybuf,
619b671ae6bSHans Verkuil 	.vidioc_qbuf          = vb2_ioctl_qbuf,
620b671ae6bSHans Verkuil 	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
621b671ae6bSHans Verkuil 	.vidioc_streamon      = vb2_ioctl_streamon,
622b671ae6bSHans Verkuil 	.vidioc_streamoff     = vb2_ioctl_streamoff,
62318c73af6SHans Verkuil 	.vidioc_g_std = cx25821_vidioc_g_std,
624b285192aSMauro Carvalho Chehab 	.vidioc_s_std = cx25821_vidioc_s_std,
625b285192aSMauro Carvalho Chehab 	.vidioc_enum_input = cx25821_vidioc_enum_input,
626b285192aSMauro Carvalho Chehab 	.vidioc_g_input = cx25821_vidioc_g_input,
627b285192aSMauro Carvalho Chehab 	.vidioc_s_input = cx25821_vidioc_s_input,
628b285192aSMauro Carvalho Chehab 	.vidioc_log_status = vidioc_log_status,
6298d125c50SHans Verkuil 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
6308d125c50SHans Verkuil 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
631b285192aSMauro Carvalho Chehab };
632b285192aSMauro Carvalho Chehab 
633ffd3c233SHans Verkuil static const struct video_device cx25821_video_device = {
634ffd3c233SHans Verkuil 	.name = "cx25821-video",
635b285192aSMauro Carvalho Chehab 	.fops = &video_fops,
636467870caSHans Verkuil 	.release = video_device_release_empty,
637ffd3c233SHans Verkuil 	.minor = -1,
638b285192aSMauro Carvalho Chehab 	.ioctl_ops = &video_ioctl_ops,
639b285192aSMauro Carvalho Chehab 	.tvnorms = CX25821_NORMS,
640b285192aSMauro Carvalho Chehab };
641ffd3c233SHans Verkuil 
6421f198870SHans Verkuil static const struct v4l2_file_operations video_out_fops = {
6431f198870SHans Verkuil 	.owner = THIS_MODULE,
6441f198870SHans Verkuil 	.open = v4l2_fh_open,
645b671ae6bSHans Verkuil 	.release        = vb2_fop_release,
646b671ae6bSHans Verkuil 	.write          = vb2_fop_write,
647b671ae6bSHans Verkuil 	.poll		= vb2_fop_poll,
6481f198870SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
649b671ae6bSHans Verkuil 	.mmap           = vb2_fop_mmap,
6501f198870SHans Verkuil };
6511f198870SHans Verkuil 
6521f198870SHans Verkuil static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
6531f198870SHans Verkuil 	.vidioc_querycap = cx25821_vidioc_querycap,
654e90878abSHans Verkuil 	.vidioc_enum_fmt_vid_out = cx25821_vidioc_enum_fmt_vid_cap,
655e90878abSHans Verkuil 	.vidioc_g_fmt_vid_out = cx25821_vidioc_g_fmt_vid_cap,
656e90878abSHans Verkuil 	.vidioc_try_fmt_vid_out = cx25821_vidioc_try_fmt_vid_out,
657e90878abSHans Verkuil 	.vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
6581f198870SHans Verkuil 	.vidioc_g_std = cx25821_vidioc_g_std,
6591f198870SHans Verkuil 	.vidioc_s_std = cx25821_vidioc_s_std,
6601f198870SHans Verkuil 	.vidioc_enum_output = cx25821_vidioc_enum_output,
6611f198870SHans Verkuil 	.vidioc_g_output = cx25821_vidioc_g_output,
6621f198870SHans Verkuil 	.vidioc_s_output = cx25821_vidioc_s_output,
6631f198870SHans Verkuil 	.vidioc_log_status = vidioc_log_status,
6641f198870SHans Verkuil };
6651f198870SHans Verkuil 
6661f198870SHans Verkuil static const struct video_device cx25821_video_out_device = {
6671f198870SHans Verkuil 	.name = "cx25821-video",
6681f198870SHans Verkuil 	.fops = &video_out_fops,
6691f198870SHans Verkuil 	.release = video_device_release_empty,
6701f198870SHans Verkuil 	.minor = -1,
6711f198870SHans Verkuil 	.ioctl_ops = &video_out_ioctl_ops,
6721f198870SHans Verkuil 	.tvnorms = CX25821_NORMS,
6731f198870SHans Verkuil };
6741f198870SHans Verkuil 
675ffd3c233SHans Verkuil void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
676ffd3c233SHans Verkuil {
677ffd3c233SHans Verkuil 	cx_clear(PCI_INT_MSK, 1);
678ffd3c233SHans Verkuil 
679467870caSHans Verkuil 	if (video_is_registered(&dev->channels[chan_num].vdev)) {
680467870caSHans Verkuil 		video_unregister_device(&dev->channels[chan_num].vdev);
681f8d7ee70SHans Verkuil 		v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
682ffd3c233SHans Verkuil 	}
683ffd3c233SHans Verkuil }
684ffd3c233SHans Verkuil 
685ffd3c233SHans Verkuil int cx25821_video_register(struct cx25821_dev *dev)
686ffd3c233SHans Verkuil {
687ffd3c233SHans Verkuil 	int err;
688ffd3c233SHans Verkuil 	int i;
689ffd3c233SHans Verkuil 
690be178cb4SHans Verkuil 	/* initial device configuration */
691a6aa0dc4SHans Verkuil 	dev->tvnorm = V4L2_STD_NTSC_M;
692be178cb4SHans Verkuil 
693ffd3c233SHans Verkuil 	spin_lock_init(&dev->slock);
694ffd3c233SHans Verkuil 
6951f198870SHans Verkuil 	for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; ++i) {
6962efe2cc4SHans Verkuil 		struct cx25821_channel *chan = &dev->channels[i];
6972efe2cc4SHans Verkuil 		struct video_device *vdev = &chan->vdev;
6982efe2cc4SHans Verkuil 		struct v4l2_ctrl_handler *hdl = &chan->hdl;
699b671ae6bSHans Verkuil 		struct vb2_queue *q;
7001f198870SHans Verkuil 		bool is_output = i > SRAM_CH08;
701467870caSHans Verkuil 
702ffd3c233SHans Verkuil 		if (i == SRAM_CH08) /* audio channel */
703ffd3c233SHans Verkuil 			continue;
704ffd3c233SHans Verkuil 
7051f198870SHans Verkuil 		if (!is_output) {
706f8d7ee70SHans Verkuil 			v4l2_ctrl_handler_init(hdl, 4);
707f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
708f8d7ee70SHans Verkuil 					V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
709f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
710f8d7ee70SHans Verkuil 					V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
711f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
712f8d7ee70SHans Verkuil 					V4L2_CID_SATURATION, 0, 10000, 1, 5000);
713f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
714f8d7ee70SHans Verkuil 					V4L2_CID_HUE, 0, 10000, 1, 5000);
715f8d7ee70SHans Verkuil 			if (hdl->error) {
716f8d7ee70SHans Verkuil 				err = hdl->error;
717f8d7ee70SHans Verkuil 				goto fail_unreg;
718f8d7ee70SHans Verkuil 			}
719be178cb4SHans Verkuil 			err = v4l2_ctrl_handler_setup(hdl);
720be178cb4SHans Verkuil 			if (err)
721be178cb4SHans Verkuil 				goto fail_unreg;
7227087d31bSHans Verkuil 		} else {
7237087d31bSHans Verkuil 			chan->out = &dev->vid_out_data[i - SRAM_CH09];
7247087d31bSHans Verkuil 			chan->out->chan = chan;
7251f198870SHans Verkuil 		}
726ffd3c233SHans Verkuil 
7272efe2cc4SHans Verkuil 		chan->sram_channels = &cx25821_sram_channels[i];
7282efe2cc4SHans Verkuil 		chan->width = 720;
729b671ae6bSHans Verkuil 		chan->field = V4L2_FIELD_INTERLACED;
7302efe2cc4SHans Verkuil 		if (dev->tvnorm & V4L2_STD_625_50)
7312efe2cc4SHans Verkuil 			chan->height = 576;
7322efe2cc4SHans Verkuil 		else
7332efe2cc4SHans Verkuil 			chan->height = 480;
734ffd3c233SHans Verkuil 
7352efe2cc4SHans Verkuil 		if (chan->pixel_formats == PIXEL_FRMT_411)
7362efe2cc4SHans Verkuil 			chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
7372efe2cc4SHans Verkuil 		else
7382efe2cc4SHans Verkuil 			chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
739ffd3c233SHans Verkuil 
7402efe2cc4SHans Verkuil 		cx_write(chan->sram_channels->int_stat, 0xffffffff);
741ffd3c233SHans Verkuil 
7422efe2cc4SHans Verkuil 		INIT_LIST_HEAD(&chan->dma_vidq.active);
7432efe2cc4SHans Verkuil 
744b671ae6bSHans Verkuil 		q = &chan->vidq;
7452efe2cc4SHans Verkuil 
746b671ae6bSHans Verkuil 		q->type = is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
747b671ae6bSHans Verkuil 				      V4L2_BUF_TYPE_VIDEO_CAPTURE;
748b671ae6bSHans Verkuil 		q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
749b671ae6bSHans Verkuil 		q->io_modes |= is_output ? VB2_WRITE : VB2_READ;
750b671ae6bSHans Verkuil 		q->gfp_flags = GFP_DMA32;
751b671ae6bSHans Verkuil 		q->min_buffers_needed = 2;
752b671ae6bSHans Verkuil 		q->drv_priv = chan;
753b671ae6bSHans Verkuil 		q->buf_struct_size = sizeof(struct cx25821_buffer);
754b671ae6bSHans Verkuil 		q->ops = &cx25821_video_qops;
755b671ae6bSHans Verkuil 		q->mem_ops = &vb2_dma_sg_memops;
756b671ae6bSHans Verkuil 		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
757b671ae6bSHans Verkuil 		q->lock = &dev->lock;
758b671ae6bSHans Verkuil 
759b671ae6bSHans Verkuil 		if (!is_output) {
760b671ae6bSHans Verkuil 			err = vb2_queue_init(q);
761b671ae6bSHans Verkuil 			if (err < 0)
762b671ae6bSHans Verkuil 				goto fail_unreg;
763b671ae6bSHans Verkuil 		}
764ffd3c233SHans Verkuil 
765ffd3c233SHans Verkuil 		/* register v4l devices */
7661f198870SHans Verkuil 		*vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
767467870caSHans Verkuil 		vdev->v4l2_dev = &dev->v4l2_dev;
7681f198870SHans Verkuil 		if (!is_output)
769f8d7ee70SHans Verkuil 			vdev->ctrl_handler = hdl;
7701f198870SHans Verkuil 		else
7711f198870SHans Verkuil 			vdev->vfl_dir = VFL_DIR_TX;
772be178cb4SHans Verkuil 		vdev->lock = &dev->lock;
773b671ae6bSHans Verkuil 		vdev->queue = q;
774467870caSHans Verkuil 		snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
7752efe2cc4SHans Verkuil 		video_set_drvdata(vdev, chan);
776ffd3c233SHans Verkuil 
777467870caSHans Verkuil 		err = video_register_device(vdev, VFL_TYPE_GRABBER,
778467870caSHans Verkuil 					    video_nr[dev->nr]);
779ffd3c233SHans Verkuil 
780ffd3c233SHans Verkuil 		if (err < 0)
781ffd3c233SHans Verkuil 			goto fail_unreg;
782ffd3c233SHans Verkuil 	}
783ffd3c233SHans Verkuil 
784ffd3c233SHans Verkuil 	/* set PCI interrupt */
785ffd3c233SHans Verkuil 	cx_set(PCI_INT_MSK, 0xff);
786ffd3c233SHans Verkuil 
787ffd3c233SHans Verkuil 	return 0;
788ffd3c233SHans Verkuil 
789ffd3c233SHans Verkuil fail_unreg:
790467870caSHans Verkuil 	while (i >= 0)
791467870caSHans Verkuil 		cx25821_video_unregister(dev, i--);
792ffd3c233SHans Verkuil 	return err;
793ffd3c233SHans Verkuil }
794