xref: /linux/drivers/media/pci/cx25821/cx25821-video.c (revision b671ae6bdc1ad77a63536c6f74940976b9b89322)
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>
6*b671ae6bSHans 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) {
124*b671ae6bSHans Verkuil 		struct cx25821_dmaqueue *dmaq =
125*b671ae6bSHans Verkuil 			&dev->channels[channel->i].dma_vidq;
126*b671ae6bSHans Verkuil 		struct cx25821_buffer *buf;
127b285192aSMauro Carvalho Chehab 
128b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
129*b671ae6bSHans Verkuil 		if (!list_empty(&dmaq->active)) {
130*b671ae6bSHans Verkuil 			buf = list_entry(dmaq->active.next,
131*b671ae6bSHans Verkuil 					 struct cx25821_buffer, queue);
132*b671ae6bSHans Verkuil 
133*b671ae6bSHans Verkuil 			v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
134*b671ae6bSHans Verkuil 			buf->vb.v4l2_buf.sequence = dmaq->count++;
135*b671ae6bSHans Verkuil 			list_del(&buf->queue);
136*b671ae6bSHans Verkuil 			vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
137*b671ae6bSHans 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*b671ae6bSHans Verkuil static int cx25821_queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
145*b671ae6bSHans Verkuil 			   unsigned int *num_buffers, unsigned int *num_planes,
146*b671ae6bSHans Verkuil 			   unsigned int sizes[], void *alloc_ctxs[])
147b285192aSMauro Carvalho Chehab {
148*b671ae6bSHans Verkuil 	struct cx25821_channel *chan = q->drv_priv;
149b285192aSMauro Carvalho Chehab 
150*b671ae6bSHans Verkuil 	*num_planes = 1;
151*b671ae6bSHans Verkuil 	sizes[0] = (chan->fmt->depth * chan->width * chan->height) >> 3;
152*b671ae6bSHans Verkuil 	alloc_ctxs[0] = chan->dev->alloc_ctx;
153b285192aSMauro Carvalho Chehab 	return 0;
154b285192aSMauro Carvalho Chehab }
155b285192aSMauro Carvalho Chehab 
156*b671ae6bSHans Verkuil static int cx25821_buffer_prepare(struct vb2_buffer *vb)
157b285192aSMauro Carvalho Chehab {
158*b671ae6bSHans Verkuil 	struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
1592efe2cc4SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
160b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
161b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
162*b671ae6bSHans Verkuil 	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
163b285192aSMauro Carvalho Chehab 	u32 line0_offset;
164b285192aSMauro Carvalho Chehab 	int bpl_local = LINE_SIZE_D1;
165*b671ae6bSHans Verkuil 	int ret;
166b285192aSMauro Carvalho Chehab 
1678d125c50SHans Verkuil 	if (chan->pixel_formats == PIXEL_FRMT_411)
168*b671ae6bSHans Verkuil 		buf->bpl = (chan->fmt->depth * chan->width) >> 3;
169b285192aSMauro Carvalho Chehab 	else
170*b671ae6bSHans Verkuil 		buf->bpl = (chan->fmt->depth >> 3) * chan->width;
171*b671ae6bSHans Verkuil 
172*b671ae6bSHans Verkuil 	if (vb2_plane_size(vb, 0) < chan->height * buf->bpl)
173*b671ae6bSHans Verkuil 		return -EINVAL;
174*b671ae6bSHans Verkuil 	vb2_set_plane_payload(vb, 0, chan->height * buf->bpl);
175*b671ae6bSHans Verkuil 	buf->vb.v4l2_buf.field = chan->field;
176b285192aSMauro Carvalho Chehab 
1778d125c50SHans Verkuil 	if (chan->pixel_formats == PIXEL_FRMT_411) {
178b285192aSMauro Carvalho Chehab 		bpl_local = buf->bpl;
179b285192aSMauro Carvalho Chehab 	} else {
180b285192aSMauro Carvalho Chehab 		bpl_local = buf->bpl;   /* Default */
181b285192aSMauro Carvalho Chehab 
1828d125c50SHans Verkuil 		if (chan->use_cif_resolution) {
183988f7b80SHans Verkuil 			if (dev->tvnorm & V4L2_STD_625_50)
184b285192aSMauro Carvalho Chehab 				bpl_local = 352 << 1;
185b285192aSMauro Carvalho Chehab 			else
1868d125c50SHans Verkuil 				bpl_local = chan->cif_width << 1;
187b285192aSMauro Carvalho Chehab 		}
188b285192aSMauro Carvalho Chehab 	}
189b285192aSMauro Carvalho Chehab 
190*b671ae6bSHans Verkuil 	switch (chan->field) {
191b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_TOP:
192*b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
193*b671ae6bSHans Verkuil 				sgt->sgl, 0, UNSET,
194*b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height);
195b285192aSMauro Carvalho Chehab 		break;
196b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_BOTTOM:
197*b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
198*b671ae6bSHans Verkuil 				sgt->sgl, UNSET, 0,
199*b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height);
200b285192aSMauro Carvalho Chehab 		break;
201b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_INTERLACED:
202b285192aSMauro Carvalho Chehab 		/* All other formats are top field first */
203b285192aSMauro Carvalho Chehab 		line0_offset = 0;
204b285192aSMauro Carvalho Chehab 		dprintk(1, "top field first\n");
205b285192aSMauro Carvalho Chehab 
206*b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
207*b671ae6bSHans Verkuil 				sgt->sgl, line0_offset,
208b285192aSMauro Carvalho Chehab 				bpl_local, bpl_local, bpl_local,
209*b671ae6bSHans Verkuil 				chan->height >> 1);
210b285192aSMauro Carvalho Chehab 		break;
211b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_TB:
212*b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
213*b671ae6bSHans Verkuil 				sgt->sgl,
214*b671ae6bSHans Verkuil 				0, buf->bpl * (chan->height >> 1),
215*b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height >> 1);
216b285192aSMauro Carvalho Chehab 		break;
217b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_BT:
218*b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
219*b671ae6bSHans Verkuil 				sgt->sgl,
220*b671ae6bSHans Verkuil 				buf->bpl * (chan->height >> 1), 0,
221*b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height >> 1);
222b285192aSMauro Carvalho Chehab 		break;
223b285192aSMauro Carvalho Chehab 	default:
224*b671ae6bSHans Verkuil 		WARN_ON(1);
225*b671ae6bSHans Verkuil 		ret = -EINVAL;
226*b671ae6bSHans Verkuil 		break;
227b285192aSMauro Carvalho Chehab 	}
228b285192aSMauro Carvalho Chehab 
229b285192aSMauro Carvalho Chehab 	dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
230*b671ae6bSHans Verkuil 		buf, buf->vb.v4l2_buf.index, chan->width, chan->height,
231*b671ae6bSHans Verkuil 		chan->fmt->depth, chan->fmt->name,
232*b671ae6bSHans Verkuil 		(unsigned long)buf->risc.dma);
233b285192aSMauro Carvalho Chehab 
234*b671ae6bSHans Verkuil 	return ret;
235b285192aSMauro Carvalho Chehab }
236b285192aSMauro Carvalho Chehab 
237*b671ae6bSHans Verkuil static void cx25821_buffer_finish(struct vb2_buffer *vb)
238b285192aSMauro Carvalho Chehab {
239b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
240b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
241*b671ae6bSHans Verkuil 	struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
2422efe2cc4SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
243*b671ae6bSHans Verkuil 
244*b671ae6bSHans Verkuil 	cx25821_free_buffer(dev, buf);
245*b671ae6bSHans Verkuil }
246*b671ae6bSHans Verkuil 
247*b671ae6bSHans Verkuil static void cx25821_buffer_queue(struct vb2_buffer *vb)
248*b671ae6bSHans Verkuil {
249*b671ae6bSHans Verkuil 	struct cx25821_buffer *buf =
250*b671ae6bSHans Verkuil 		container_of(vb, struct cx25821_buffer, vb);
251*b671ae6bSHans Verkuil 	struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
252*b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
253*b671ae6bSHans Verkuil 	struct cx25821_buffer *prev;
2542efe2cc4SHans Verkuil 	struct cx25821_dmaqueue *q = &dev->channels[chan->id].dma_vidq;
255b285192aSMauro Carvalho Chehab 
256*b671ae6bSHans Verkuil 	buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
257*b671ae6bSHans Verkuil 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
258*b671ae6bSHans Verkuil 	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
259b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
260b285192aSMauro Carvalho Chehab 
261*b671ae6bSHans Verkuil 	if (list_empty(&q->active)) {
262*b671ae6bSHans Verkuil 		list_add_tail(&buf->queue, &q->active);
263b285192aSMauro Carvalho Chehab 	} else {
264*b671ae6bSHans Verkuil 		buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
265b285192aSMauro Carvalho Chehab 		prev = list_entry(q->active.prev, struct cx25821_buffer,
266*b671ae6bSHans Verkuil 				queue);
267*b671ae6bSHans Verkuil 		list_add_tail(&buf->queue, &q->active);
268b285192aSMauro Carvalho Chehab 		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
269b285192aSMauro Carvalho Chehab 	}
270b285192aSMauro Carvalho Chehab }
271b285192aSMauro Carvalho Chehab 
272*b671ae6bSHans Verkuil static int cx25821_start_streaming(struct vb2_queue *q, unsigned int count)
273*b671ae6bSHans Verkuil {
274*b671ae6bSHans Verkuil 	struct cx25821_channel *chan = q->drv_priv;
275*b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
276*b671ae6bSHans Verkuil 	struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
277*b671ae6bSHans Verkuil 	struct cx25821_buffer *buf = list_entry(dmaq->active.next,
278*b671ae6bSHans Verkuil 			struct cx25821_buffer, queue);
279*b671ae6bSHans Verkuil 
280*b671ae6bSHans Verkuil 	dmaq->count = 0;
281*b671ae6bSHans Verkuil 	cx25821_start_video_dma(dev, dmaq, buf, chan->sram_channels);
282*b671ae6bSHans Verkuil 	return 0;
283b285192aSMauro Carvalho Chehab }
284b285192aSMauro Carvalho Chehab 
285*b671ae6bSHans Verkuil static void cx25821_stop_streaming(struct vb2_queue *q)
286*b671ae6bSHans Verkuil {
287*b671ae6bSHans Verkuil 	struct cx25821_channel *chan = q->drv_priv;
288*b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
289*b671ae6bSHans Verkuil 	struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
290*b671ae6bSHans Verkuil 	unsigned long flags;
291*b671ae6bSHans Verkuil 
292*b671ae6bSHans Verkuil 	cx_write(chan->sram_channels->dma_ctl, 0); /* FIFO and RISC disable */
293*b671ae6bSHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
294*b671ae6bSHans Verkuil 	while (!list_empty(&dmaq->active)) {
295*b671ae6bSHans Verkuil 		struct cx25821_buffer *buf = list_entry(dmaq->active.next,
296*b671ae6bSHans Verkuil 			struct cx25821_buffer, queue);
297*b671ae6bSHans Verkuil 
298*b671ae6bSHans Verkuil 		list_del(&buf->queue);
299*b671ae6bSHans Verkuil 		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
300*b671ae6bSHans Verkuil 	}
301*b671ae6bSHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
302*b671ae6bSHans Verkuil }
303*b671ae6bSHans Verkuil 
304*b671ae6bSHans Verkuil static struct vb2_ops cx25821_video_qops = {
305*b671ae6bSHans Verkuil 	.queue_setup    = cx25821_queue_setup,
306b285192aSMauro Carvalho Chehab 	.buf_prepare  = cx25821_buffer_prepare,
307*b671ae6bSHans Verkuil 	.buf_finish = cx25821_buffer_finish,
308*b671ae6bSHans Verkuil 	.buf_queue    = cx25821_buffer_queue,
309*b671ae6bSHans Verkuil 	.wait_prepare = vb2_ops_wait_prepare,
310*b671ae6bSHans Verkuil 	.wait_finish = vb2_ops_wait_finish,
311*b671ae6bSHans Verkuil 	.start_streaming = cx25821_start_streaming,
312*b671ae6bSHans Verkuil 	.stop_streaming = cx25821_stop_streaming,
313b285192aSMauro Carvalho Chehab };
314b285192aSMauro Carvalho Chehab 
31595c232a2SHans Verkuil /* VIDEO IOCTLS */
3164c1d0f73SHans Verkuil 
3174c1d0f73SHans Verkuil static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
3184c1d0f73SHans Verkuil 			    struct v4l2_fmtdesc *f)
3194c1d0f73SHans Verkuil {
3204c1d0f73SHans Verkuil 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
3214c1d0f73SHans Verkuil 		return -EINVAL;
3224c1d0f73SHans Verkuil 
3234c1d0f73SHans Verkuil 	strlcpy(f->description, formats[f->index].name, sizeof(f->description));
3244c1d0f73SHans Verkuil 	f->pixelformat = formats[f->index].fourcc;
3254c1d0f73SHans Verkuil 
3264c1d0f73SHans Verkuil 	return 0;
3274c1d0f73SHans Verkuil }
3284c1d0f73SHans Verkuil 
32995c232a2SHans Verkuil static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
33095c232a2SHans Verkuil 				 struct v4l2_format *f)
33195c232a2SHans Verkuil {
3322efe2cc4SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
33395c232a2SHans Verkuil 
3342efe2cc4SHans Verkuil 	f->fmt.pix.width = chan->width;
3352efe2cc4SHans Verkuil 	f->fmt.pix.height = chan->height;
336*b671ae6bSHans Verkuil 	f->fmt.pix.field = chan->field;
3372efe2cc4SHans Verkuil 	f->fmt.pix.pixelformat = chan->fmt->fourcc;
338988f7b80SHans Verkuil 	f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
339988f7b80SHans Verkuil 	f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
340988f7b80SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
34195c232a2SHans Verkuil 
34295c232a2SHans Verkuil 	return 0;
34395c232a2SHans Verkuil }
34495c232a2SHans Verkuil 
34595c232a2SHans Verkuil static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
34695c232a2SHans Verkuil 				   struct v4l2_format *f)
34795c232a2SHans Verkuil {
348988f7b80SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
349988f7b80SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
35095c232a2SHans Verkuil 	const struct cx25821_fmt *fmt;
351988f7b80SHans Verkuil 	enum v4l2_field field = f->fmt.pix.field;
35266f93178SMauro Carvalho Chehab 	unsigned int maxh;
353988f7b80SHans Verkuil 	unsigned w;
35495c232a2SHans Verkuil 
35595c232a2SHans Verkuil 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
35695c232a2SHans Verkuil 	if (NULL == fmt)
35795c232a2SHans Verkuil 		return -EINVAL;
358988f7b80SHans Verkuil 	maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
35995c232a2SHans Verkuil 
360988f7b80SHans Verkuil 	w = f->fmt.pix.width;
361988f7b80SHans Verkuil 	if (field != V4L2_FIELD_BOTTOM)
36295c232a2SHans Verkuil 		field = V4L2_FIELD_TOP;
363988f7b80SHans Verkuil 	if (w < 352) {
364988f7b80SHans Verkuil 		w = 176;
365988f7b80SHans Verkuil 		f->fmt.pix.height = maxh / 4;
366988f7b80SHans Verkuil 	} else if (w < 720) {
367988f7b80SHans Verkuil 		w = 352;
368988f7b80SHans Verkuil 		f->fmt.pix.height = maxh / 2;
369988f7b80SHans Verkuil 	} else {
370988f7b80SHans Verkuil 		w = 720;
37195c232a2SHans Verkuil 		f->fmt.pix.height = maxh;
372988f7b80SHans Verkuil 		field = V4L2_FIELD_INTERLACED;
373988f7b80SHans Verkuil 	}
374988f7b80SHans Verkuil 	f->fmt.pix.field = field;
375988f7b80SHans Verkuil 	f->fmt.pix.width = w;
37695c232a2SHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
37795c232a2SHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
378988f7b80SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
37995c232a2SHans Verkuil 
38095c232a2SHans Verkuil 	return 0;
38195c232a2SHans Verkuil }
382b285192aSMauro Carvalho Chehab 
383b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
384b285192aSMauro Carvalho Chehab 				struct v4l2_format *f)
385b285192aSMauro Carvalho Chehab {
3862efe2cc4SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
3878d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
388b285192aSMauro Carvalho Chehab 	int pix_format = PIXEL_FRMT_422;
389a6aa0dc4SHans Verkuil 	int err;
390b285192aSMauro Carvalho Chehab 
391b285192aSMauro Carvalho Chehab 	err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
392b285192aSMauro Carvalho Chehab 
393b285192aSMauro Carvalho Chehab 	if (0 != err)
394b285192aSMauro Carvalho Chehab 		return err;
395b285192aSMauro Carvalho Chehab 
3962efe2cc4SHans Verkuil 	chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
397*b671ae6bSHans Verkuil 	chan->field = f->fmt.pix.field;
3982efe2cc4SHans Verkuil 	chan->width = f->fmt.pix.width;
3992efe2cc4SHans Verkuil 	chan->height = f->fmt.pix.height;
400b285192aSMauro Carvalho Chehab 
401b285192aSMauro Carvalho Chehab 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
402b285192aSMauro Carvalho Chehab 		pix_format = PIXEL_FRMT_411;
403b285192aSMauro Carvalho Chehab 	else
404988f7b80SHans Verkuil 		pix_format = PIXEL_FRMT_422;
405b285192aSMauro Carvalho Chehab 
406b285192aSMauro Carvalho Chehab 	cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
407b285192aSMauro Carvalho Chehab 
408b285192aSMauro Carvalho Chehab 	/* check if cif resolution */
4092efe2cc4SHans Verkuil 	if (chan->width == 320 || chan->width == 352)
4102efe2cc4SHans Verkuil 		chan->use_cif_resolution = 1;
411b285192aSMauro Carvalho Chehab 	else
4122efe2cc4SHans Verkuil 		chan->use_cif_resolution = 0;
413b285192aSMauro Carvalho Chehab 
4142efe2cc4SHans Verkuil 	chan->cif_width = chan->width;
4152efe2cc4SHans Verkuil 	medusa_set_resolution(dev, chan->width, SRAM_CH00);
416b285192aSMauro Carvalho Chehab 	return 0;
417b285192aSMauro Carvalho Chehab }
418b285192aSMauro Carvalho Chehab 
419b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
420b285192aSMauro Carvalho Chehab {
4218d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4228d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
4238d125c50SHans Verkuil 	const struct sram_channel *sram_ch = chan->sram_channels;
424b285192aSMauro Carvalho Chehab 	u32 tmp = 0;
425b285192aSMauro Carvalho Chehab 
426b285192aSMauro Carvalho Chehab 	tmp = cx_read(sram_ch->dma_ctl);
427b285192aSMauro Carvalho Chehab 	pr_info("Video input 0 is %s\n",
428b285192aSMauro Carvalho Chehab 		(tmp & 0x11) ? "streaming" : "stopped");
429b285192aSMauro Carvalho Chehab 	return 0;
430b285192aSMauro Carvalho Chehab }
431b285192aSMauro Carvalho Chehab 
432b285192aSMauro Carvalho Chehab 
43395c232a2SHans Verkuil static int cx25821_vidioc_querycap(struct file *file, void *priv,
434b285192aSMauro Carvalho Chehab 			    struct v4l2_capability *cap)
435b285192aSMauro Carvalho Chehab {
4368d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4378d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
4383dd473caSHans Verkuil 	const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
4393dd473caSHans Verkuil 			V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
4400df13d99SHans Verkuil 	const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE;
441b285192aSMauro Carvalho Chehab 
442b285192aSMauro Carvalho Chehab 	strcpy(cap->driver, "cx25821");
443b285192aSMauro Carvalho Chehab 	strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
444b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
4458d125c50SHans Verkuil 	if (chan->id >= VID_CHANNEL_NUM)
4463dd473caSHans Verkuil 		cap->device_caps = cap_output;
4473dd473caSHans Verkuil 	else
4483dd473caSHans Verkuil 		cap->device_caps = cap_input;
4490df13d99SHans Verkuil 	cap->capabilities = cap_input | cap_output | V4L2_CAP_DEVICE_CAPS;
450b285192aSMauro Carvalho Chehab 	return 0;
451b285192aSMauro Carvalho Chehab }
452b285192aSMauro Carvalho Chehab 
45395c232a2SHans Verkuil static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
45418c73af6SHans Verkuil {
4558d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
45618c73af6SHans Verkuil 
4578d125c50SHans Verkuil 	*tvnorms = chan->dev->tvnorm;
45818c73af6SHans Verkuil 	return 0;
45918c73af6SHans Verkuil }
46018c73af6SHans Verkuil 
461a3f17af2SMauro Carvalho Chehab static int cx25821_vidioc_s_std(struct file *file, void *priv,
462a3f17af2SMauro Carvalho Chehab 				v4l2_std_id tvnorms)
463b285192aSMauro Carvalho Chehab {
4648d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4658d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
466b285192aSMauro Carvalho Chehab 
467314527acSHans Verkuil 	if (dev->tvnorm == tvnorms)
468b285192aSMauro Carvalho Chehab 		return 0;
469b285192aSMauro Carvalho Chehab 
470a6aa0dc4SHans Verkuil 	dev->tvnorm = tvnorms;
471988f7b80SHans Verkuil 	chan->width = 720;
472988f7b80SHans Verkuil 	chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
473b285192aSMauro Carvalho Chehab 
474b285192aSMauro Carvalho Chehab 	medusa_set_videostandard(dev);
475b285192aSMauro Carvalho Chehab 
476b285192aSMauro Carvalho Chehab 	return 0;
477b285192aSMauro Carvalho Chehab }
478b285192aSMauro Carvalho Chehab 
47995c232a2SHans Verkuil static int cx25821_vidioc_enum_input(struct file *file, void *priv,
48095c232a2SHans Verkuil 			      struct v4l2_input *i)
481b285192aSMauro Carvalho Chehab {
482a6aa0dc4SHans Verkuil 	if (i->index)
483b285192aSMauro Carvalho Chehab 		return -EINVAL;
484b285192aSMauro Carvalho Chehab 
485b285192aSMauro Carvalho Chehab 	i->type = V4L2_INPUT_TYPE_CAMERA;
486b285192aSMauro Carvalho Chehab 	i->std = CX25821_NORMS;
487a6aa0dc4SHans Verkuil 	strcpy(i->name, "Composite");
488b285192aSMauro Carvalho Chehab 	return 0;
489b285192aSMauro Carvalho Chehab }
490b285192aSMauro Carvalho Chehab 
49195c232a2SHans Verkuil static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
492b285192aSMauro Carvalho Chehab {
493a6aa0dc4SHans Verkuil 	*i = 0;
494b285192aSMauro Carvalho Chehab 	return 0;
495b285192aSMauro Carvalho Chehab }
496b285192aSMauro Carvalho Chehab 
49795c232a2SHans Verkuil static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
498b285192aSMauro Carvalho Chehab {
499a6aa0dc4SHans Verkuil 	return i ? -EINVAL : 0;
500b285192aSMauro Carvalho Chehab }
501b285192aSMauro Carvalho Chehab 
502f8d7ee70SHans Verkuil static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
503b285192aSMauro Carvalho Chehab {
504f8d7ee70SHans Verkuil 	struct cx25821_channel *chan =
505f8d7ee70SHans Verkuil 		container_of(ctrl->handler, struct cx25821_channel, hdl);
506f8d7ee70SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
507b285192aSMauro Carvalho Chehab 
508f8d7ee70SHans Verkuil 	switch (ctrl->id) {
509b285192aSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
510f8d7ee70SHans Verkuil 		medusa_set_brightness(dev, ctrl->val, chan->id);
511b285192aSMauro Carvalho Chehab 		break;
512b285192aSMauro Carvalho Chehab 	case V4L2_CID_HUE:
513f8d7ee70SHans Verkuil 		medusa_set_hue(dev, ctrl->val, chan->id);
514b285192aSMauro Carvalho Chehab 		break;
515b285192aSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
516f8d7ee70SHans Verkuil 		medusa_set_contrast(dev, ctrl->val, chan->id);
517b285192aSMauro Carvalho Chehab 		break;
518b285192aSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
519f8d7ee70SHans Verkuil 		medusa_set_saturation(dev, ctrl->val, chan->id);
520b285192aSMauro Carvalho Chehab 		break;
521b285192aSMauro Carvalho Chehab 	default:
522f8d7ee70SHans Verkuil 		return -EINVAL;
523b285192aSMauro Carvalho Chehab 	}
524f8d7ee70SHans Verkuil 	return 0;
525b285192aSMauro Carvalho Chehab }
526b285192aSMauro Carvalho Chehab 
5271f198870SHans Verkuil static int cx25821_vidioc_enum_output(struct file *file, void *priv,
5281f198870SHans Verkuil 			      struct v4l2_output *o)
529b285192aSMauro Carvalho Chehab {
5301f198870SHans Verkuil 	if (o->index)
5311f198870SHans Verkuil 		return -EINVAL;
532b285192aSMauro Carvalho Chehab 
5331f198870SHans Verkuil 	o->type = V4L2_INPUT_TYPE_CAMERA;
5341f198870SHans Verkuil 	o->std = CX25821_NORMS;
5351f198870SHans Verkuil 	strcpy(o->name, "Composite");
536b285192aSMauro Carvalho Chehab 	return 0;
537b285192aSMauro Carvalho Chehab }
538b285192aSMauro Carvalho Chehab 
5391f198870SHans Verkuil static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
540b285192aSMauro Carvalho Chehab {
5411f198870SHans Verkuil 	*o = 0;
542b285192aSMauro Carvalho Chehab 	return 0;
543b285192aSMauro Carvalho Chehab }
544b285192aSMauro Carvalho Chehab 
5451f198870SHans Verkuil static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
546b285192aSMauro Carvalho Chehab {
5471f198870SHans Verkuil 	return o ? -EINVAL : 0;
548b285192aSMauro Carvalho Chehab }
549b285192aSMauro Carvalho Chehab 
550e90878abSHans Verkuil static int cx25821_vidioc_try_fmt_vid_out(struct file *file, void *priv,
551e90878abSHans Verkuil 				   struct v4l2_format *f)
552e90878abSHans Verkuil {
553e90878abSHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
554e90878abSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
555e90878abSHans Verkuil 	const struct cx25821_fmt *fmt;
556e90878abSHans Verkuil 
557e90878abSHans Verkuil 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
558e90878abSHans Verkuil 	if (NULL == fmt)
559e90878abSHans Verkuil 		return -EINVAL;
560e90878abSHans Verkuil 	f->fmt.pix.width = 720;
561e90878abSHans Verkuil 	f->fmt.pix.height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
562e90878abSHans Verkuil 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
563e90878abSHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
564e90878abSHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
565e90878abSHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
566e90878abSHans Verkuil 	return 0;
567e90878abSHans Verkuil }
568e90878abSHans Verkuil 
569e90878abSHans Verkuil static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
570e90878abSHans Verkuil 				struct v4l2_format *f)
571e90878abSHans Verkuil {
572e90878abSHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
573e90878abSHans Verkuil 	int err;
574e90878abSHans Verkuil 
575e90878abSHans Verkuil 	err = cx25821_vidioc_try_fmt_vid_out(file, priv, f);
576e90878abSHans Verkuil 
577e90878abSHans Verkuil 	if (0 != err)
578e90878abSHans Verkuil 		return err;
579e90878abSHans Verkuil 
580e90878abSHans Verkuil 	chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
581*b671ae6bSHans Verkuil 	chan->field = f->fmt.pix.field;
582e90878abSHans Verkuil 	chan->width = f->fmt.pix.width;
583e90878abSHans Verkuil 	chan->height = f->fmt.pix.height;
584e90878abSHans Verkuil 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
585e90878abSHans Verkuil 		chan->pixel_formats = PIXEL_FRMT_411;
586e90878abSHans Verkuil 	else
587e90878abSHans Verkuil 		chan->pixel_formats = PIXEL_FRMT_422;
588e90878abSHans Verkuil 	return 0;
589e90878abSHans Verkuil }
590e90878abSHans Verkuil 
591f8d7ee70SHans Verkuil static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
592f8d7ee70SHans Verkuil 	.s_ctrl = cx25821_s_ctrl,
593f8d7ee70SHans Verkuil };
594f8d7ee70SHans Verkuil 
595b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops = {
596b285192aSMauro Carvalho Chehab 	.owner = THIS_MODULE,
5978d125c50SHans Verkuil 	.open = v4l2_fh_open,
598*b671ae6bSHans Verkuil 	.release        = vb2_fop_release,
599*b671ae6bSHans Verkuil 	.read           = vb2_fop_read,
600*b671ae6bSHans Verkuil 	.poll		= vb2_fop_poll,
6011f198870SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
602*b671ae6bSHans Verkuil 	.mmap           = vb2_fop_mmap,
603b285192aSMauro Carvalho Chehab };
604b285192aSMauro Carvalho Chehab 
605b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops = {
606b285192aSMauro Carvalho Chehab 	.vidioc_querycap = cx25821_vidioc_querycap,
607b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
608b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
609b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
610b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
611*b671ae6bSHans Verkuil 	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
612*b671ae6bSHans Verkuil 	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
613*b671ae6bSHans Verkuil 	.vidioc_querybuf      = vb2_ioctl_querybuf,
614*b671ae6bSHans Verkuil 	.vidioc_qbuf          = vb2_ioctl_qbuf,
615*b671ae6bSHans Verkuil 	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
616*b671ae6bSHans Verkuil 	.vidioc_streamon      = vb2_ioctl_streamon,
617*b671ae6bSHans Verkuil 	.vidioc_streamoff     = vb2_ioctl_streamoff,
61818c73af6SHans Verkuil 	.vidioc_g_std = cx25821_vidioc_g_std,
619b285192aSMauro Carvalho Chehab 	.vidioc_s_std = cx25821_vidioc_s_std,
620b285192aSMauro Carvalho Chehab 	.vidioc_enum_input = cx25821_vidioc_enum_input,
621b285192aSMauro Carvalho Chehab 	.vidioc_g_input = cx25821_vidioc_g_input,
622b285192aSMauro Carvalho Chehab 	.vidioc_s_input = cx25821_vidioc_s_input,
623b285192aSMauro Carvalho Chehab 	.vidioc_log_status = vidioc_log_status,
6248d125c50SHans Verkuil 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
6258d125c50SHans Verkuil 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
626b285192aSMauro Carvalho Chehab };
627b285192aSMauro Carvalho Chehab 
628ffd3c233SHans Verkuil static const struct video_device cx25821_video_device = {
629ffd3c233SHans Verkuil 	.name = "cx25821-video",
630b285192aSMauro Carvalho Chehab 	.fops = &video_fops,
631467870caSHans Verkuil 	.release = video_device_release_empty,
632ffd3c233SHans Verkuil 	.minor = -1,
633b285192aSMauro Carvalho Chehab 	.ioctl_ops = &video_ioctl_ops,
634b285192aSMauro Carvalho Chehab 	.tvnorms = CX25821_NORMS,
635b285192aSMauro Carvalho Chehab };
636ffd3c233SHans Verkuil 
6371f198870SHans Verkuil static const struct v4l2_file_operations video_out_fops = {
6381f198870SHans Verkuil 	.owner = THIS_MODULE,
6391f198870SHans Verkuil 	.open = v4l2_fh_open,
640*b671ae6bSHans Verkuil 	.release        = vb2_fop_release,
641*b671ae6bSHans Verkuil 	.write          = vb2_fop_write,
642*b671ae6bSHans Verkuil 	.poll		= vb2_fop_poll,
6431f198870SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
644*b671ae6bSHans Verkuil 	.mmap           = vb2_fop_mmap,
6451f198870SHans Verkuil };
6461f198870SHans Verkuil 
6471f198870SHans Verkuil static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
6481f198870SHans Verkuil 	.vidioc_querycap = cx25821_vidioc_querycap,
649e90878abSHans Verkuil 	.vidioc_enum_fmt_vid_out = cx25821_vidioc_enum_fmt_vid_cap,
650e90878abSHans Verkuil 	.vidioc_g_fmt_vid_out = cx25821_vidioc_g_fmt_vid_cap,
651e90878abSHans Verkuil 	.vidioc_try_fmt_vid_out = cx25821_vidioc_try_fmt_vid_out,
652e90878abSHans Verkuil 	.vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
6531f198870SHans Verkuil 	.vidioc_g_std = cx25821_vidioc_g_std,
6541f198870SHans Verkuil 	.vidioc_s_std = cx25821_vidioc_s_std,
6551f198870SHans Verkuil 	.vidioc_enum_output = cx25821_vidioc_enum_output,
6561f198870SHans Verkuil 	.vidioc_g_output = cx25821_vidioc_g_output,
6571f198870SHans Verkuil 	.vidioc_s_output = cx25821_vidioc_s_output,
6581f198870SHans Verkuil 	.vidioc_log_status = vidioc_log_status,
6591f198870SHans Verkuil };
6601f198870SHans Verkuil 
6611f198870SHans Verkuil static const struct video_device cx25821_video_out_device = {
6621f198870SHans Verkuil 	.name = "cx25821-video",
6631f198870SHans Verkuil 	.fops = &video_out_fops,
6641f198870SHans Verkuil 	.release = video_device_release_empty,
6651f198870SHans Verkuil 	.minor = -1,
6661f198870SHans Verkuil 	.ioctl_ops = &video_out_ioctl_ops,
6671f198870SHans Verkuil 	.tvnorms = CX25821_NORMS,
6681f198870SHans Verkuil };
6691f198870SHans Verkuil 
670ffd3c233SHans Verkuil void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
671ffd3c233SHans Verkuil {
672ffd3c233SHans Verkuil 	cx_clear(PCI_INT_MSK, 1);
673ffd3c233SHans Verkuil 
674467870caSHans Verkuil 	if (video_is_registered(&dev->channels[chan_num].vdev)) {
675467870caSHans Verkuil 		video_unregister_device(&dev->channels[chan_num].vdev);
676f8d7ee70SHans Verkuil 		v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
677ffd3c233SHans Verkuil 	}
678ffd3c233SHans Verkuil }
679ffd3c233SHans Verkuil 
680ffd3c233SHans Verkuil int cx25821_video_register(struct cx25821_dev *dev)
681ffd3c233SHans Verkuil {
682ffd3c233SHans Verkuil 	int err;
683ffd3c233SHans Verkuil 	int i;
684ffd3c233SHans Verkuil 
685be178cb4SHans Verkuil 	/* initial device configuration */
686a6aa0dc4SHans Verkuil 	dev->tvnorm = V4L2_STD_NTSC_M;
687be178cb4SHans Verkuil 
688ffd3c233SHans Verkuil 	spin_lock_init(&dev->slock);
689ffd3c233SHans Verkuil 
6901f198870SHans Verkuil 	for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; ++i) {
6912efe2cc4SHans Verkuil 		struct cx25821_channel *chan = &dev->channels[i];
6922efe2cc4SHans Verkuil 		struct video_device *vdev = &chan->vdev;
6932efe2cc4SHans Verkuil 		struct v4l2_ctrl_handler *hdl = &chan->hdl;
694*b671ae6bSHans Verkuil 		struct vb2_queue *q;
6951f198870SHans Verkuil 		bool is_output = i > SRAM_CH08;
696467870caSHans Verkuil 
697ffd3c233SHans Verkuil 		if (i == SRAM_CH08) /* audio channel */
698ffd3c233SHans Verkuil 			continue;
699ffd3c233SHans Verkuil 
7001f198870SHans Verkuil 		if (!is_output) {
701f8d7ee70SHans Verkuil 			v4l2_ctrl_handler_init(hdl, 4);
702f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
703f8d7ee70SHans Verkuil 					V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
704f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
705f8d7ee70SHans Verkuil 					V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
706f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
707f8d7ee70SHans Verkuil 					V4L2_CID_SATURATION, 0, 10000, 1, 5000);
708f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
709f8d7ee70SHans Verkuil 					V4L2_CID_HUE, 0, 10000, 1, 5000);
710f8d7ee70SHans Verkuil 			if (hdl->error) {
711f8d7ee70SHans Verkuil 				err = hdl->error;
712f8d7ee70SHans Verkuil 				goto fail_unreg;
713f8d7ee70SHans Verkuil 			}
714be178cb4SHans Verkuil 			err = v4l2_ctrl_handler_setup(hdl);
715be178cb4SHans Verkuil 			if (err)
716be178cb4SHans Verkuil 				goto fail_unreg;
7177087d31bSHans Verkuil 		} else {
7187087d31bSHans Verkuil 			chan->out = &dev->vid_out_data[i - SRAM_CH09];
7197087d31bSHans Verkuil 			chan->out->chan = chan;
7201f198870SHans Verkuil 		}
721ffd3c233SHans Verkuil 
7222efe2cc4SHans Verkuil 		chan->sram_channels = &cx25821_sram_channels[i];
7232efe2cc4SHans Verkuil 		chan->width = 720;
724*b671ae6bSHans Verkuil 		chan->field = V4L2_FIELD_INTERLACED;
7252efe2cc4SHans Verkuil 		if (dev->tvnorm & V4L2_STD_625_50)
7262efe2cc4SHans Verkuil 			chan->height = 576;
7272efe2cc4SHans Verkuil 		else
7282efe2cc4SHans Verkuil 			chan->height = 480;
729ffd3c233SHans Verkuil 
7302efe2cc4SHans Verkuil 		if (chan->pixel_formats == PIXEL_FRMT_411)
7312efe2cc4SHans Verkuil 			chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
7322efe2cc4SHans Verkuil 		else
7332efe2cc4SHans Verkuil 			chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
734ffd3c233SHans Verkuil 
7352efe2cc4SHans Verkuil 		cx_write(chan->sram_channels->int_stat, 0xffffffff);
736ffd3c233SHans Verkuil 
7372efe2cc4SHans Verkuil 		INIT_LIST_HEAD(&chan->dma_vidq.active);
7382efe2cc4SHans Verkuil 
739*b671ae6bSHans Verkuil 		q = &chan->vidq;
7402efe2cc4SHans Verkuil 
741*b671ae6bSHans Verkuil 		q->type = is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
742*b671ae6bSHans Verkuil 				      V4L2_BUF_TYPE_VIDEO_CAPTURE;
743*b671ae6bSHans Verkuil 		q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
744*b671ae6bSHans Verkuil 		q->io_modes |= is_output ? VB2_WRITE : VB2_READ;
745*b671ae6bSHans Verkuil 		q->gfp_flags = GFP_DMA32;
746*b671ae6bSHans Verkuil 		q->min_buffers_needed = 2;
747*b671ae6bSHans Verkuil 		q->drv_priv = chan;
748*b671ae6bSHans Verkuil 		q->buf_struct_size = sizeof(struct cx25821_buffer);
749*b671ae6bSHans Verkuil 		q->ops = &cx25821_video_qops;
750*b671ae6bSHans Verkuil 		q->mem_ops = &vb2_dma_sg_memops;
751*b671ae6bSHans Verkuil 		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
752*b671ae6bSHans Verkuil 		q->lock = &dev->lock;
753*b671ae6bSHans Verkuil 
754*b671ae6bSHans Verkuil 		if (!is_output) {
755*b671ae6bSHans Verkuil 			err = vb2_queue_init(q);
756*b671ae6bSHans Verkuil 			if (err < 0)
757*b671ae6bSHans Verkuil 				goto fail_unreg;
758*b671ae6bSHans Verkuil 		}
759ffd3c233SHans Verkuil 
760ffd3c233SHans Verkuil 		/* register v4l devices */
7611f198870SHans Verkuil 		*vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
762467870caSHans Verkuil 		vdev->v4l2_dev = &dev->v4l2_dev;
7631f198870SHans Verkuil 		if (!is_output)
764f8d7ee70SHans Verkuil 			vdev->ctrl_handler = hdl;
7651f198870SHans Verkuil 		else
7661f198870SHans Verkuil 			vdev->vfl_dir = VFL_DIR_TX;
767be178cb4SHans Verkuil 		vdev->lock = &dev->lock;
768*b671ae6bSHans Verkuil 		vdev->queue = q;
769467870caSHans Verkuil 		snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
7702efe2cc4SHans Verkuil 		video_set_drvdata(vdev, chan);
771ffd3c233SHans Verkuil 
772467870caSHans Verkuil 		err = video_register_device(vdev, VFL_TYPE_GRABBER,
773467870caSHans Verkuil 					    video_nr[dev->nr]);
774ffd3c233SHans Verkuil 
775ffd3c233SHans Verkuil 		if (err < 0)
776ffd3c233SHans Verkuil 			goto fail_unreg;
777ffd3c233SHans Verkuil 	}
778ffd3c233SHans Verkuil 
779ffd3c233SHans Verkuil 	/* set PCI interrupt */
780ffd3c233SHans Verkuil 	cx_set(PCI_INT_MSK, 0xff);
781ffd3c233SHans Verkuil 
782ffd3c233SHans Verkuil 	return 0;
783ffd3c233SHans Verkuil 
784ffd3c233SHans Verkuil fail_unreg:
785467870caSHans Verkuil 	while (i >= 0)
786467870caSHans Verkuil 		cx25821_video_unregister(dev, i--);
787ffd3c233SHans Verkuil 	return err;
788ffd3c233SHans Verkuil }
789