xref: /linux/drivers/media/pci/cx25821/cx25821-video.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b285192aSMauro Carvalho Chehab /*
3b285192aSMauro Carvalho Chehab  *  Driver for the Conexant CX25821 PCIe bridge
4b285192aSMauro Carvalho Chehab  *
5b285192aSMauro Carvalho Chehab  *  Copyright (C) 2009 Conexant Systems Inc.
6b285192aSMauro Carvalho Chehab  *  Authors  <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
7b671ae6bSHans Verkuil  *  Based on Steven Toth <stoth@linuxtv.org> cx25821 driver
8b285192aSMauro Carvalho Chehab  *  Parts adapted/taken from Eduardo Moscoso Rubino
9b285192aSMauro Carvalho Chehab  *  Copyright (C) 2009 Eduardo Moscoso Rubino <moscoso@TopoLogica.com>
10b285192aSMauro Carvalho Chehab  */
11b285192aSMauro Carvalho Chehab 
12b285192aSMauro Carvalho Chehab #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13b285192aSMauro Carvalho Chehab 
14b285192aSMauro Carvalho Chehab #include "cx25821-video.h"
15b285192aSMauro Carvalho Chehab 
16b285192aSMauro Carvalho Chehab MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
17b285192aSMauro Carvalho Chehab MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
18b285192aSMauro Carvalho Chehab MODULE_LICENSE("GPL");
19b285192aSMauro Carvalho Chehab 
20b285192aSMauro Carvalho Chehab static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
21b285192aSMauro Carvalho Chehab 
22b285192aSMauro Carvalho Chehab module_param_array(video_nr, int, NULL, 0444);
23b285192aSMauro Carvalho Chehab 
24b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_nr, "video device numbers");
25b285192aSMauro Carvalho Chehab 
26b285192aSMauro Carvalho Chehab static unsigned int video_debug = VIDEO_DEBUG;
27b285192aSMauro Carvalho Chehab module_param(video_debug, int, 0644);
28b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
29b285192aSMauro Carvalho Chehab 
30b285192aSMauro Carvalho Chehab static unsigned int irq_debug;
31b285192aSMauro Carvalho Chehab module_param(irq_debug, int, 0644);
32b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
33b285192aSMauro Carvalho Chehab 
34b285192aSMauro Carvalho Chehab #define FORMAT_FLAGS_PACKED       0x01
35b285192aSMauro Carvalho Chehab 
3695c232a2SHans Verkuil static const struct cx25821_fmt formats[] = {
37b285192aSMauro Carvalho Chehab 	{
38b285192aSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_Y41P,
39b285192aSMauro Carvalho Chehab 		.depth = 12,
40b285192aSMauro Carvalho Chehab 		.flags = FORMAT_FLAGS_PACKED,
41b285192aSMauro Carvalho Chehab 	}, {
42b285192aSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_YUYV,
43b285192aSMauro Carvalho Chehab 		.depth = 16,
44b285192aSMauro Carvalho Chehab 		.flags = FORMAT_FLAGS_PACKED,
45b285192aSMauro Carvalho Chehab 	},
46b285192aSMauro Carvalho Chehab };
47b285192aSMauro Carvalho Chehab 
cx25821_format_by_fourcc(unsigned int fourcc)4895c232a2SHans Verkuil static const struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
49b285192aSMauro Carvalho Chehab {
50b285192aSMauro Carvalho Chehab 	unsigned int i;
51b285192aSMauro Carvalho Chehab 
52b285192aSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(formats); i++)
53b285192aSMauro Carvalho Chehab 		if (formats[i].fourcc == fourcc)
54b285192aSMauro Carvalho Chehab 			return formats + i;
55b285192aSMauro Carvalho Chehab 	return NULL;
56b285192aSMauro Carvalho Chehab }
57b285192aSMauro Carvalho Chehab 
cx25821_start_video_dma(struct cx25821_dev * dev,struct cx25821_dmaqueue * q,struct cx25821_buffer * buf,const struct sram_channel * channel)58b285192aSMauro Carvalho Chehab int cx25821_start_video_dma(struct cx25821_dev *dev,
59b285192aSMauro Carvalho Chehab 			    struct cx25821_dmaqueue *q,
60b285192aSMauro Carvalho Chehab 			    struct cx25821_buffer *buf,
61bfef0d35SHans Verkuil 			    const struct sram_channel *channel)
62b285192aSMauro Carvalho Chehab {
63b285192aSMauro Carvalho Chehab 	int tmp = 0;
64b285192aSMauro Carvalho Chehab 
65b285192aSMauro Carvalho Chehab 	/* setup fifo + format */
66b285192aSMauro Carvalho Chehab 	cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
67b285192aSMauro Carvalho Chehab 
68b285192aSMauro Carvalho Chehab 	/* reset counter */
69b285192aSMauro Carvalho Chehab 	cx_write(channel->gpcnt_ctl, 3);
70b285192aSMauro Carvalho Chehab 
71b285192aSMauro Carvalho Chehab 	/* enable irq */
72b285192aSMauro Carvalho Chehab 	cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
73b285192aSMauro Carvalho Chehab 	cx_set(channel->int_msk, 0x11);
74b285192aSMauro Carvalho Chehab 
75b285192aSMauro Carvalho Chehab 	/* start dma */
76b285192aSMauro Carvalho Chehab 	cx_write(channel->dma_ctl, 0x11);	/* FIFO and RISC enable */
77b285192aSMauro Carvalho Chehab 
78b285192aSMauro Carvalho Chehab 	/* make sure upstream setting if any is reversed */
79b285192aSMauro Carvalho Chehab 	tmp = cx_read(VID_CH_MODE_SEL);
80b285192aSMauro Carvalho Chehab 	cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
81b285192aSMauro Carvalho Chehab 
82b285192aSMauro Carvalho Chehab 	return 0;
83b285192aSMauro Carvalho Chehab }
84b285192aSMauro Carvalho Chehab 
cx25821_video_irq(struct cx25821_dev * dev,int chan_num,u32 status)85b285192aSMauro Carvalho Chehab int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
86b285192aSMauro Carvalho Chehab {
87b285192aSMauro Carvalho Chehab 	int handled = 0;
88b285192aSMauro Carvalho Chehab 	u32 mask;
89bfef0d35SHans Verkuil 	const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
90b285192aSMauro Carvalho Chehab 
91b285192aSMauro Carvalho Chehab 	mask = cx_read(channel->int_msk);
92b285192aSMauro Carvalho Chehab 	if (0 == (status & mask))
93b285192aSMauro Carvalho Chehab 		return handled;
94b285192aSMauro Carvalho Chehab 
95b285192aSMauro Carvalho Chehab 	cx_write(channel->int_stat, status);
96b285192aSMauro Carvalho Chehab 
97b285192aSMauro Carvalho Chehab 	/* risc op code error */
98b285192aSMauro Carvalho Chehab 	if (status & (1 << 16)) {
99b285192aSMauro Carvalho Chehab 		pr_warn("%s, %s: video risc op code error\n",
100b285192aSMauro Carvalho Chehab 			dev->name, channel->name);
101b285192aSMauro Carvalho Chehab 		cx_clear(channel->dma_ctl, 0x11);
102b285192aSMauro Carvalho Chehab 		cx25821_sram_channel_dump(dev, channel);
103b285192aSMauro Carvalho Chehab 	}
104b285192aSMauro Carvalho Chehab 
105b285192aSMauro Carvalho Chehab 	/* risc1 y */
106b285192aSMauro Carvalho Chehab 	if (status & FLD_VID_DST_RISC1) {
107b671ae6bSHans Verkuil 		struct cx25821_dmaqueue *dmaq =
108b671ae6bSHans Verkuil 			&dev->channels[channel->i].dma_vidq;
109b671ae6bSHans Verkuil 		struct cx25821_buffer *buf;
110b285192aSMauro Carvalho Chehab 
111b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
112b671ae6bSHans Verkuil 		if (!list_empty(&dmaq->active)) {
113b671ae6bSHans Verkuil 			buf = list_entry(dmaq->active.next,
114b671ae6bSHans Verkuil 					 struct cx25821_buffer, queue);
115b671ae6bSHans Verkuil 
116d6dd645eSJunghak Sung 			buf->vb.vb2_buf.timestamp = ktime_get_ns();
1172d700715SJunghak Sung 			buf->vb.sequence = dmaq->count++;
118b671ae6bSHans Verkuil 			list_del(&buf->queue);
1192d700715SJunghak Sung 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
120b671ae6bSHans Verkuil 		}
121b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
122b285192aSMauro Carvalho Chehab 		handled++;
123b285192aSMauro Carvalho Chehab 	}
124b285192aSMauro Carvalho Chehab 	return handled;
125b285192aSMauro Carvalho Chehab }
126b285192aSMauro Carvalho Chehab 
cx25821_queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])127df9ecb0cSHans Verkuil static int cx25821_queue_setup(struct vb2_queue *q,
128b671ae6bSHans Verkuil 			   unsigned int *num_buffers, unsigned int *num_planes,
12936c0f8b3SHans Verkuil 			   unsigned int sizes[], struct device *alloc_devs[])
130b285192aSMauro Carvalho Chehab {
131b671ae6bSHans Verkuil 	struct cx25821_channel *chan = q->drv_priv;
13211c8a2dfSHans Verkuil 	unsigned size = (chan->fmt->depth * chan->width * chan->height) >> 3;
13311c8a2dfSHans Verkuil 
134df9ecb0cSHans Verkuil 	if (*num_planes)
135df9ecb0cSHans Verkuil 		return sizes[0] < size ? -EINVAL : 0;
136b285192aSMauro Carvalho Chehab 
137b671ae6bSHans Verkuil 	*num_planes = 1;
138df9ecb0cSHans Verkuil 	sizes[0] = size;
139b285192aSMauro Carvalho Chehab 	return 0;
140b285192aSMauro Carvalho Chehab }
141b285192aSMauro Carvalho Chehab 
cx25821_buffer_prepare(struct vb2_buffer * vb)142b671ae6bSHans Verkuil static int cx25821_buffer_prepare(struct vb2_buffer *vb)
143b285192aSMauro Carvalho Chehab {
1442d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
145b671ae6bSHans Verkuil 	struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
1462efe2cc4SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
147b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
1482d700715SJunghak Sung 		container_of(vbuf, struct cx25821_buffer, vb);
149b671ae6bSHans Verkuil 	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
150b285192aSMauro Carvalho Chehab 	u32 line0_offset;
151b285192aSMauro Carvalho Chehab 	int bpl_local = LINE_SIZE_D1;
152b671ae6bSHans Verkuil 	int ret;
153b285192aSMauro Carvalho Chehab 
1548d125c50SHans Verkuil 	if (chan->pixel_formats == PIXEL_FRMT_411)
155b671ae6bSHans Verkuil 		buf->bpl = (chan->fmt->depth * chan->width) >> 3;
156b285192aSMauro Carvalho Chehab 	else
157b671ae6bSHans Verkuil 		buf->bpl = (chan->fmt->depth >> 3) * chan->width;
158b671ae6bSHans Verkuil 
159b671ae6bSHans Verkuil 	if (vb2_plane_size(vb, 0) < chan->height * buf->bpl)
160b671ae6bSHans Verkuil 		return -EINVAL;
161b671ae6bSHans Verkuil 	vb2_set_plane_payload(vb, 0, chan->height * buf->bpl);
1622d700715SJunghak Sung 	buf->vb.field = chan->field;
163b285192aSMauro Carvalho Chehab 
1648d125c50SHans Verkuil 	if (chan->pixel_formats == PIXEL_FRMT_411) {
165b285192aSMauro Carvalho Chehab 		bpl_local = buf->bpl;
166b285192aSMauro Carvalho Chehab 	} else {
167b285192aSMauro Carvalho Chehab 		bpl_local = buf->bpl;   /* Default */
168b285192aSMauro Carvalho Chehab 
1698d125c50SHans Verkuil 		if (chan->use_cif_resolution) {
170988f7b80SHans Verkuil 			if (dev->tvnorm & V4L2_STD_625_50)
171b285192aSMauro Carvalho Chehab 				bpl_local = 352 << 1;
172b285192aSMauro Carvalho Chehab 			else
1738d125c50SHans Verkuil 				bpl_local = chan->cif_width << 1;
174b285192aSMauro Carvalho Chehab 		}
175b285192aSMauro Carvalho Chehab 	}
176b285192aSMauro Carvalho Chehab 
177b671ae6bSHans Verkuil 	switch (chan->field) {
178b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_TOP:
179b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
180b671ae6bSHans Verkuil 				sgt->sgl, 0, UNSET,
181b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height);
182b285192aSMauro Carvalho Chehab 		break;
183b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_BOTTOM:
184b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
185b671ae6bSHans Verkuil 				sgt->sgl, UNSET, 0,
186b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height);
187b285192aSMauro Carvalho Chehab 		break;
188b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_INTERLACED:
189b285192aSMauro Carvalho Chehab 		/* All other formats are top field first */
190b285192aSMauro Carvalho Chehab 		line0_offset = 0;
191b285192aSMauro Carvalho Chehab 		dprintk(1, "top field first\n");
192b285192aSMauro Carvalho Chehab 
193b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
194b671ae6bSHans Verkuil 				sgt->sgl, line0_offset,
195b285192aSMauro Carvalho Chehab 				bpl_local, bpl_local, bpl_local,
196b671ae6bSHans Verkuil 				chan->height >> 1);
197b285192aSMauro Carvalho Chehab 		break;
198b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_TB:
199b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
200b671ae6bSHans Verkuil 				sgt->sgl,
201b671ae6bSHans Verkuil 				0, buf->bpl * (chan->height >> 1),
202b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height >> 1);
203b285192aSMauro Carvalho Chehab 		break;
204b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_BT:
205b671ae6bSHans Verkuil 		ret = cx25821_risc_buffer(dev->pci, &buf->risc,
206b671ae6bSHans Verkuil 				sgt->sgl,
207b671ae6bSHans Verkuil 				buf->bpl * (chan->height >> 1), 0,
208b671ae6bSHans Verkuil 				buf->bpl, 0, chan->height >> 1);
209b285192aSMauro Carvalho Chehab 		break;
210b285192aSMauro Carvalho Chehab 	default:
211b671ae6bSHans Verkuil 		WARN_ON(1);
212b671ae6bSHans Verkuil 		ret = -EINVAL;
213b671ae6bSHans Verkuil 		break;
214b285192aSMauro Carvalho Chehab 	}
215b285192aSMauro Carvalho Chehab 
2162169e6daSHans Verkuil 	dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp 0x%08x - dma=0x%08lx\n",
2172d700715SJunghak Sung 		buf, buf->vb.vb2_buf.index, chan->width, chan->height,
2182169e6daSHans Verkuil 		chan->fmt->depth, chan->fmt->fourcc,
219b671ae6bSHans Verkuil 		(unsigned long)buf->risc.dma);
220b285192aSMauro Carvalho Chehab 
221b671ae6bSHans Verkuil 	return ret;
222b285192aSMauro Carvalho Chehab }
223b285192aSMauro Carvalho Chehab 
cx25821_buffer_finish(struct vb2_buffer * vb)224b671ae6bSHans Verkuil static void cx25821_buffer_finish(struct vb2_buffer *vb)
225b285192aSMauro Carvalho Chehab {
2262d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
227b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
2282d700715SJunghak Sung 		container_of(vbuf, struct cx25821_buffer, vb);
229b671ae6bSHans Verkuil 	struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
2302efe2cc4SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
231b671ae6bSHans Verkuil 
232b671ae6bSHans Verkuil 	cx25821_free_buffer(dev, buf);
233b671ae6bSHans Verkuil }
234b671ae6bSHans Verkuil 
cx25821_buffer_queue(struct vb2_buffer * vb)235b671ae6bSHans Verkuil static void cx25821_buffer_queue(struct vb2_buffer *vb)
236b671ae6bSHans Verkuil {
2372d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
238b671ae6bSHans Verkuil 	struct cx25821_buffer *buf =
2392d700715SJunghak Sung 		container_of(vbuf, struct cx25821_buffer, vb);
240b671ae6bSHans Verkuil 	struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
241b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
242b671ae6bSHans Verkuil 	struct cx25821_buffer *prev;
2432efe2cc4SHans Verkuil 	struct cx25821_dmaqueue *q = &dev->channels[chan->id].dma_vidq;
244b285192aSMauro Carvalho Chehab 
245b671ae6bSHans Verkuil 	buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
246b671ae6bSHans Verkuil 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
247b671ae6bSHans Verkuil 	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
248b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
249b285192aSMauro Carvalho Chehab 
250b671ae6bSHans Verkuil 	if (list_empty(&q->active)) {
251b671ae6bSHans Verkuil 		list_add_tail(&buf->queue, &q->active);
252b285192aSMauro Carvalho Chehab 	} else {
253b671ae6bSHans Verkuil 		buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
254b285192aSMauro Carvalho Chehab 		prev = list_entry(q->active.prev, struct cx25821_buffer,
255b671ae6bSHans Verkuil 				queue);
256b671ae6bSHans Verkuil 		list_add_tail(&buf->queue, &q->active);
257b285192aSMauro Carvalho Chehab 		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
258b285192aSMauro Carvalho Chehab 	}
259b285192aSMauro Carvalho Chehab }
260b285192aSMauro Carvalho Chehab 
cx25821_start_streaming(struct vb2_queue * q,unsigned int count)261b671ae6bSHans Verkuil static int cx25821_start_streaming(struct vb2_queue *q, unsigned int count)
262b671ae6bSHans Verkuil {
263b671ae6bSHans Verkuil 	struct cx25821_channel *chan = q->drv_priv;
264b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
265b671ae6bSHans Verkuil 	struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
266b671ae6bSHans Verkuil 	struct cx25821_buffer *buf = list_entry(dmaq->active.next,
267b671ae6bSHans Verkuil 			struct cx25821_buffer, queue);
268b671ae6bSHans Verkuil 
269b671ae6bSHans Verkuil 	dmaq->count = 0;
270b671ae6bSHans Verkuil 	cx25821_start_video_dma(dev, dmaq, buf, chan->sram_channels);
271b671ae6bSHans Verkuil 	return 0;
272b285192aSMauro Carvalho Chehab }
273b285192aSMauro Carvalho Chehab 
cx25821_stop_streaming(struct vb2_queue * q)274b671ae6bSHans Verkuil static void cx25821_stop_streaming(struct vb2_queue *q)
275b671ae6bSHans Verkuil {
276b671ae6bSHans Verkuil 	struct cx25821_channel *chan = q->drv_priv;
277b671ae6bSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
278b671ae6bSHans Verkuil 	struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
279b671ae6bSHans Verkuil 	unsigned long flags;
280b671ae6bSHans Verkuil 
281b671ae6bSHans Verkuil 	cx_write(chan->sram_channels->dma_ctl, 0); /* FIFO and RISC disable */
282b671ae6bSHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
283b671ae6bSHans Verkuil 	while (!list_empty(&dmaq->active)) {
284b671ae6bSHans Verkuil 		struct cx25821_buffer *buf = list_entry(dmaq->active.next,
285b671ae6bSHans Verkuil 			struct cx25821_buffer, queue);
286b671ae6bSHans Verkuil 
287b671ae6bSHans Verkuil 		list_del(&buf->queue);
2882d700715SJunghak Sung 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
289b671ae6bSHans Verkuil 	}
290b671ae6bSHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
291b671ae6bSHans Verkuil }
292b671ae6bSHans Verkuil 
29310accd2eSJulia Lawall static const struct vb2_ops cx25821_video_qops = {
294b671ae6bSHans Verkuil 	.queue_setup    = cx25821_queue_setup,
295b285192aSMauro Carvalho Chehab 	.buf_prepare  = cx25821_buffer_prepare,
296b671ae6bSHans Verkuil 	.buf_finish = cx25821_buffer_finish,
297b671ae6bSHans Verkuil 	.buf_queue    = cx25821_buffer_queue,
298b671ae6bSHans Verkuil 	.wait_prepare = vb2_ops_wait_prepare,
299b671ae6bSHans Verkuil 	.wait_finish = vb2_ops_wait_finish,
300b671ae6bSHans Verkuil 	.start_streaming = cx25821_start_streaming,
301b671ae6bSHans Verkuil 	.stop_streaming = cx25821_stop_streaming,
302b285192aSMauro Carvalho Chehab };
303b285192aSMauro Carvalho Chehab 
30495c232a2SHans Verkuil /* VIDEO IOCTLS */
3054c1d0f73SHans Verkuil 
cx25821_vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)3064c1d0f73SHans Verkuil static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
3074c1d0f73SHans Verkuil 			    struct v4l2_fmtdesc *f)
3084c1d0f73SHans Verkuil {
3094c1d0f73SHans Verkuil 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
3104c1d0f73SHans Verkuil 		return -EINVAL;
3114c1d0f73SHans Verkuil 
3124c1d0f73SHans Verkuil 	f->pixelformat = formats[f->index].fourcc;
3134c1d0f73SHans Verkuil 
3144c1d0f73SHans Verkuil 	return 0;
3154c1d0f73SHans Verkuil }
3164c1d0f73SHans Verkuil 
cx25821_vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)31795c232a2SHans Verkuil static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
31895c232a2SHans Verkuil 				 struct v4l2_format *f)
31995c232a2SHans Verkuil {
3202efe2cc4SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
32195c232a2SHans Verkuil 
3222efe2cc4SHans Verkuil 	f->fmt.pix.width = chan->width;
3232efe2cc4SHans Verkuil 	f->fmt.pix.height = chan->height;
324b671ae6bSHans Verkuil 	f->fmt.pix.field = chan->field;
3252efe2cc4SHans Verkuil 	f->fmt.pix.pixelformat = chan->fmt->fourcc;
326988f7b80SHans Verkuil 	f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
327988f7b80SHans Verkuil 	f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
328988f7b80SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
32995c232a2SHans Verkuil 
33095c232a2SHans Verkuil 	return 0;
33195c232a2SHans Verkuil }
33295c232a2SHans Verkuil 
cx25821_vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)33395c232a2SHans Verkuil static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
33495c232a2SHans Verkuil 				   struct v4l2_format *f)
33595c232a2SHans Verkuil {
336988f7b80SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
337988f7b80SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
33895c232a2SHans Verkuil 	const struct cx25821_fmt *fmt;
339988f7b80SHans Verkuil 	enum v4l2_field field = f->fmt.pix.field;
34066f93178SMauro Carvalho Chehab 	unsigned int maxh;
341988f7b80SHans Verkuil 	unsigned w;
34295c232a2SHans Verkuil 
34395c232a2SHans Verkuil 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
34495c232a2SHans Verkuil 	if (NULL == fmt)
34595c232a2SHans Verkuil 		return -EINVAL;
346988f7b80SHans Verkuil 	maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
34795c232a2SHans Verkuil 
348988f7b80SHans Verkuil 	w = f->fmt.pix.width;
349988f7b80SHans Verkuil 	if (field != V4L2_FIELD_BOTTOM)
35095c232a2SHans Verkuil 		field = V4L2_FIELD_TOP;
351988f7b80SHans Verkuil 	if (w < 352) {
352988f7b80SHans Verkuil 		w = 176;
353988f7b80SHans Verkuil 		f->fmt.pix.height = maxh / 4;
354988f7b80SHans Verkuil 	} else if (w < 720) {
355988f7b80SHans Verkuil 		w = 352;
356988f7b80SHans Verkuil 		f->fmt.pix.height = maxh / 2;
357988f7b80SHans Verkuil 	} else {
358988f7b80SHans Verkuil 		w = 720;
35995c232a2SHans Verkuil 		f->fmt.pix.height = maxh;
360988f7b80SHans Verkuil 		field = V4L2_FIELD_INTERLACED;
361988f7b80SHans Verkuil 	}
362988f7b80SHans Verkuil 	f->fmt.pix.field = field;
363988f7b80SHans Verkuil 	f->fmt.pix.width = w;
36495c232a2SHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
36595c232a2SHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
366988f7b80SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
36795c232a2SHans Verkuil 
36895c232a2SHans Verkuil 	return 0;
36995c232a2SHans Verkuil }
370b285192aSMauro Carvalho Chehab 
vidioc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)371b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
372b285192aSMauro Carvalho Chehab 				struct v4l2_format *f)
373b285192aSMauro Carvalho Chehab {
3742efe2cc4SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
3758d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
376b285192aSMauro Carvalho Chehab 	int pix_format = PIXEL_FRMT_422;
377a6aa0dc4SHans Verkuil 	int err;
378b285192aSMauro Carvalho Chehab 
379b285192aSMauro Carvalho Chehab 	err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
380b285192aSMauro Carvalho Chehab 
381b285192aSMauro Carvalho Chehab 	if (0 != err)
382b285192aSMauro Carvalho Chehab 		return err;
383b285192aSMauro Carvalho Chehab 
3842efe2cc4SHans Verkuil 	chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
385b671ae6bSHans Verkuil 	chan->field = f->fmt.pix.field;
3862efe2cc4SHans Verkuil 	chan->width = f->fmt.pix.width;
3872efe2cc4SHans Verkuil 	chan->height = f->fmt.pix.height;
388b285192aSMauro Carvalho Chehab 
389b285192aSMauro Carvalho Chehab 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
390b285192aSMauro Carvalho Chehab 		pix_format = PIXEL_FRMT_411;
391b285192aSMauro Carvalho Chehab 	else
392988f7b80SHans Verkuil 		pix_format = PIXEL_FRMT_422;
393b285192aSMauro Carvalho Chehab 
394b285192aSMauro Carvalho Chehab 	cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
395b285192aSMauro Carvalho Chehab 
396b285192aSMauro Carvalho Chehab 	/* check if cif resolution */
3972efe2cc4SHans Verkuil 	if (chan->width == 320 || chan->width == 352)
3982efe2cc4SHans Verkuil 		chan->use_cif_resolution = 1;
399b285192aSMauro Carvalho Chehab 	else
4002efe2cc4SHans Verkuil 		chan->use_cif_resolution = 0;
401b285192aSMauro Carvalho Chehab 
4022efe2cc4SHans Verkuil 	chan->cif_width = chan->width;
4032efe2cc4SHans Verkuil 	medusa_set_resolution(dev, chan->width, SRAM_CH00);
404b285192aSMauro Carvalho Chehab 	return 0;
405b285192aSMauro Carvalho Chehab }
406b285192aSMauro Carvalho Chehab 
vidioc_log_status(struct file * file,void * priv)407b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
408b285192aSMauro Carvalho Chehab {
4098d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4108d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
4118d125c50SHans Verkuil 	const struct sram_channel *sram_ch = chan->sram_channels;
412b285192aSMauro Carvalho Chehab 	u32 tmp = 0;
413b285192aSMauro Carvalho Chehab 
414b285192aSMauro Carvalho Chehab 	tmp = cx_read(sram_ch->dma_ctl);
415b285192aSMauro Carvalho Chehab 	pr_info("Video input 0 is %s\n",
416b285192aSMauro Carvalho Chehab 		(tmp & 0x11) ? "streaming" : "stopped");
417b285192aSMauro Carvalho Chehab 	return 0;
418b285192aSMauro Carvalho Chehab }
419b285192aSMauro Carvalho Chehab 
420b285192aSMauro Carvalho Chehab 
cx25821_vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)42195c232a2SHans Verkuil static int cx25821_vidioc_querycap(struct file *file, void *priv,
422b285192aSMauro Carvalho Chehab 			    struct v4l2_capability *cap)
423b285192aSMauro Carvalho Chehab {
4248d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4258d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
426b285192aSMauro Carvalho Chehab 
427cc1e6315SMauro Carvalho Chehab 	strscpy(cap->driver, "cx25821", sizeof(cap->driver));
428c0decac1SMauro Carvalho Chehab 	strscpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
429b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
43021615365SHans Verkuil 	cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
43121615365SHans Verkuil 			    V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
43221615365SHans Verkuil 			    V4L2_CAP_DEVICE_CAPS;
433b285192aSMauro Carvalho Chehab 	return 0;
434b285192aSMauro Carvalho Chehab }
435b285192aSMauro Carvalho Chehab 
cx25821_vidioc_g_std(struct file * file,void * priv,v4l2_std_id * tvnorms)43695c232a2SHans Verkuil static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
43718c73af6SHans Verkuil {
4388d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
43918c73af6SHans Verkuil 
4408d125c50SHans Verkuil 	*tvnorms = chan->dev->tvnorm;
44118c73af6SHans Verkuil 	return 0;
44218c73af6SHans Verkuil }
44318c73af6SHans Verkuil 
cx25821_vidioc_s_std(struct file * file,void * priv,v4l2_std_id tvnorms)444a3f17af2SMauro Carvalho Chehab static int cx25821_vidioc_s_std(struct file *file, void *priv,
445a3f17af2SMauro Carvalho Chehab 				v4l2_std_id tvnorms)
446b285192aSMauro Carvalho Chehab {
4478d125c50SHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
4488d125c50SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
449b285192aSMauro Carvalho Chehab 
450314527acSHans Verkuil 	if (dev->tvnorm == tvnorms)
451b285192aSMauro Carvalho Chehab 		return 0;
452b285192aSMauro Carvalho Chehab 
453a6aa0dc4SHans Verkuil 	dev->tvnorm = tvnorms;
454988f7b80SHans Verkuil 	chan->width = 720;
455988f7b80SHans Verkuil 	chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
456b285192aSMauro Carvalho Chehab 
457b285192aSMauro Carvalho Chehab 	medusa_set_videostandard(dev);
458b285192aSMauro Carvalho Chehab 
459b285192aSMauro Carvalho Chehab 	return 0;
460b285192aSMauro Carvalho Chehab }
461b285192aSMauro Carvalho Chehab 
cx25821_vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * i)46295c232a2SHans Verkuil static int cx25821_vidioc_enum_input(struct file *file, void *priv,
46395c232a2SHans Verkuil 			      struct v4l2_input *i)
464b285192aSMauro Carvalho Chehab {
465a6aa0dc4SHans Verkuil 	if (i->index)
466b285192aSMauro Carvalho Chehab 		return -EINVAL;
467b285192aSMauro Carvalho Chehab 
468b285192aSMauro Carvalho Chehab 	i->type = V4L2_INPUT_TYPE_CAMERA;
469b285192aSMauro Carvalho Chehab 	i->std = CX25821_NORMS;
470cc1e6315SMauro Carvalho Chehab 	strscpy(i->name, "Composite", sizeof(i->name));
471b285192aSMauro Carvalho Chehab 	return 0;
472b285192aSMauro Carvalho Chehab }
473b285192aSMauro Carvalho Chehab 
cx25821_vidioc_g_input(struct file * file,void * priv,unsigned int * i)47495c232a2SHans Verkuil static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
475b285192aSMauro Carvalho Chehab {
476a6aa0dc4SHans Verkuil 	*i = 0;
477b285192aSMauro Carvalho Chehab 	return 0;
478b285192aSMauro Carvalho Chehab }
479b285192aSMauro Carvalho Chehab 
cx25821_vidioc_s_input(struct file * file,void * priv,unsigned int i)48095c232a2SHans Verkuil static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
481b285192aSMauro Carvalho Chehab {
482a6aa0dc4SHans Verkuil 	return i ? -EINVAL : 0;
483b285192aSMauro Carvalho Chehab }
484b285192aSMauro Carvalho Chehab 
cx25821_s_ctrl(struct v4l2_ctrl * ctrl)485f8d7ee70SHans Verkuil static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
486b285192aSMauro Carvalho Chehab {
487f8d7ee70SHans Verkuil 	struct cx25821_channel *chan =
488f8d7ee70SHans Verkuil 		container_of(ctrl->handler, struct cx25821_channel, hdl);
489f8d7ee70SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
490b285192aSMauro Carvalho Chehab 
491f8d7ee70SHans Verkuil 	switch (ctrl->id) {
492b285192aSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
493f8d7ee70SHans Verkuil 		medusa_set_brightness(dev, ctrl->val, chan->id);
494b285192aSMauro Carvalho Chehab 		break;
495b285192aSMauro Carvalho Chehab 	case V4L2_CID_HUE:
496f8d7ee70SHans Verkuil 		medusa_set_hue(dev, ctrl->val, chan->id);
497b285192aSMauro Carvalho Chehab 		break;
498b285192aSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
499f8d7ee70SHans Verkuil 		medusa_set_contrast(dev, ctrl->val, chan->id);
500b285192aSMauro Carvalho Chehab 		break;
501b285192aSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
502f8d7ee70SHans Verkuil 		medusa_set_saturation(dev, ctrl->val, chan->id);
503b285192aSMauro Carvalho Chehab 		break;
504b285192aSMauro Carvalho Chehab 	default:
505f8d7ee70SHans Verkuil 		return -EINVAL;
506b285192aSMauro Carvalho Chehab 	}
507f8d7ee70SHans Verkuil 	return 0;
508b285192aSMauro Carvalho Chehab }
509b285192aSMauro Carvalho Chehab 
cx25821_vidioc_enum_output(struct file * file,void * priv,struct v4l2_output * o)5101f198870SHans Verkuil static int cx25821_vidioc_enum_output(struct file *file, void *priv,
5111f198870SHans Verkuil 			      struct v4l2_output *o)
512b285192aSMauro Carvalho Chehab {
5131f198870SHans Verkuil 	if (o->index)
5141f198870SHans Verkuil 		return -EINVAL;
515b285192aSMauro Carvalho Chehab 
5161f198870SHans Verkuil 	o->type = V4L2_INPUT_TYPE_CAMERA;
5171f198870SHans Verkuil 	o->std = CX25821_NORMS;
518cc1e6315SMauro Carvalho Chehab 	strscpy(o->name, "Composite", sizeof(o->name));
519b285192aSMauro Carvalho Chehab 	return 0;
520b285192aSMauro Carvalho Chehab }
521b285192aSMauro Carvalho Chehab 
cx25821_vidioc_g_output(struct file * file,void * priv,unsigned int * o)5221f198870SHans Verkuil static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
523b285192aSMauro Carvalho Chehab {
5241f198870SHans Verkuil 	*o = 0;
525b285192aSMauro Carvalho Chehab 	return 0;
526b285192aSMauro Carvalho Chehab }
527b285192aSMauro Carvalho Chehab 
cx25821_vidioc_s_output(struct file * file,void * priv,unsigned int o)5281f198870SHans Verkuil static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
529b285192aSMauro Carvalho Chehab {
5301f198870SHans Verkuil 	return o ? -EINVAL : 0;
531b285192aSMauro Carvalho Chehab }
532b285192aSMauro Carvalho Chehab 
cx25821_vidioc_try_fmt_vid_out(struct file * file,void * priv,struct v4l2_format * f)533e90878abSHans Verkuil static int cx25821_vidioc_try_fmt_vid_out(struct file *file, void *priv,
534e90878abSHans Verkuil 				   struct v4l2_format *f)
535e90878abSHans Verkuil {
536e90878abSHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
537e90878abSHans Verkuil 	struct cx25821_dev *dev = chan->dev;
538e90878abSHans Verkuil 	const struct cx25821_fmt *fmt;
539e90878abSHans Verkuil 
540e90878abSHans Verkuil 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
541e90878abSHans Verkuil 	if (NULL == fmt)
542e90878abSHans Verkuil 		return -EINVAL;
543e90878abSHans Verkuil 	f->fmt.pix.width = 720;
544e90878abSHans Verkuil 	f->fmt.pix.height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
545e90878abSHans Verkuil 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
546e90878abSHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
547e90878abSHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
548e90878abSHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
549e90878abSHans Verkuil 	return 0;
550e90878abSHans Verkuil }
551e90878abSHans Verkuil 
vidioc_s_fmt_vid_out(struct file * file,void * priv,struct v4l2_format * f)552e90878abSHans Verkuil static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
553e90878abSHans Verkuil 				struct v4l2_format *f)
554e90878abSHans Verkuil {
555e90878abSHans Verkuil 	struct cx25821_channel *chan = video_drvdata(file);
556e90878abSHans Verkuil 	int err;
557e90878abSHans Verkuil 
558e90878abSHans Verkuil 	err = cx25821_vidioc_try_fmt_vid_out(file, priv, f);
559e90878abSHans Verkuil 
560e90878abSHans Verkuil 	if (0 != err)
561e90878abSHans Verkuil 		return err;
562e90878abSHans Verkuil 
563e90878abSHans Verkuil 	chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
564b671ae6bSHans Verkuil 	chan->field = f->fmt.pix.field;
565e90878abSHans Verkuil 	chan->width = f->fmt.pix.width;
566e90878abSHans Verkuil 	chan->height = f->fmt.pix.height;
567e90878abSHans Verkuil 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
568e90878abSHans Verkuil 		chan->pixel_formats = PIXEL_FRMT_411;
569e90878abSHans Verkuil 	else
570e90878abSHans Verkuil 		chan->pixel_formats = PIXEL_FRMT_422;
571e90878abSHans Verkuil 	return 0;
572e90878abSHans Verkuil }
573e90878abSHans Verkuil 
574f8d7ee70SHans Verkuil static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
575f8d7ee70SHans Verkuil 	.s_ctrl = cx25821_s_ctrl,
576f8d7ee70SHans Verkuil };
577f8d7ee70SHans Verkuil 
578b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops = {
579b285192aSMauro Carvalho Chehab 	.owner = THIS_MODULE,
5808d125c50SHans Verkuil 	.open = v4l2_fh_open,
581b671ae6bSHans Verkuil 	.release        = vb2_fop_release,
582b671ae6bSHans Verkuil 	.read           = vb2_fop_read,
583b671ae6bSHans Verkuil 	.poll		= vb2_fop_poll,
5841f198870SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
585b671ae6bSHans Verkuil 	.mmap           = vb2_fop_mmap,
586b285192aSMauro Carvalho Chehab };
587b285192aSMauro Carvalho Chehab 
588b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops = {
589b285192aSMauro Carvalho Chehab 	.vidioc_querycap = cx25821_vidioc_querycap,
590b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
591b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
592b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
593b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
594b671ae6bSHans Verkuil 	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
595b671ae6bSHans Verkuil 	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
59611c8a2dfSHans Verkuil 	.vidioc_create_bufs   = vb2_ioctl_create_bufs,
597b671ae6bSHans Verkuil 	.vidioc_querybuf      = vb2_ioctl_querybuf,
598b671ae6bSHans Verkuil 	.vidioc_qbuf          = vb2_ioctl_qbuf,
599b671ae6bSHans Verkuil 	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
600b671ae6bSHans Verkuil 	.vidioc_streamon      = vb2_ioctl_streamon,
601b671ae6bSHans Verkuil 	.vidioc_streamoff     = vb2_ioctl_streamoff,
60218c73af6SHans Verkuil 	.vidioc_g_std = cx25821_vidioc_g_std,
603b285192aSMauro Carvalho Chehab 	.vidioc_s_std = cx25821_vidioc_s_std,
604b285192aSMauro Carvalho Chehab 	.vidioc_enum_input = cx25821_vidioc_enum_input,
605b285192aSMauro Carvalho Chehab 	.vidioc_g_input = cx25821_vidioc_g_input,
606b285192aSMauro Carvalho Chehab 	.vidioc_s_input = cx25821_vidioc_s_input,
607b285192aSMauro Carvalho Chehab 	.vidioc_log_status = vidioc_log_status,
6088d125c50SHans Verkuil 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
6098d125c50SHans Verkuil 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
610b285192aSMauro Carvalho Chehab };
611b285192aSMauro Carvalho Chehab 
612ffd3c233SHans Verkuil static const struct video_device cx25821_video_device = {
613ffd3c233SHans Verkuil 	.name = "cx25821-video",
614b285192aSMauro Carvalho Chehab 	.fops = &video_fops,
615467870caSHans Verkuil 	.release = video_device_release_empty,
616ffd3c233SHans Verkuil 	.minor = -1,
617b285192aSMauro Carvalho Chehab 	.ioctl_ops = &video_ioctl_ops,
618b285192aSMauro Carvalho Chehab 	.tvnorms = CX25821_NORMS,
61921615365SHans Verkuil 	.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
62021615365SHans Verkuil 		       V4L2_CAP_STREAMING,
621b285192aSMauro Carvalho Chehab };
622ffd3c233SHans Verkuil 
6231f198870SHans Verkuil static const struct v4l2_file_operations video_out_fops = {
6241f198870SHans Verkuil 	.owner = THIS_MODULE,
6251f198870SHans Verkuil 	.open = v4l2_fh_open,
626b671ae6bSHans Verkuil 	.release        = vb2_fop_release,
627b671ae6bSHans Verkuil 	.write          = vb2_fop_write,
628b671ae6bSHans Verkuil 	.poll		= vb2_fop_poll,
6291f198870SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
630b671ae6bSHans Verkuil 	.mmap           = vb2_fop_mmap,
6311f198870SHans Verkuil };
6321f198870SHans Verkuil 
6331f198870SHans Verkuil static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
6341f198870SHans Verkuil 	.vidioc_querycap = cx25821_vidioc_querycap,
635e90878abSHans Verkuil 	.vidioc_enum_fmt_vid_out = cx25821_vidioc_enum_fmt_vid_cap,
636e90878abSHans Verkuil 	.vidioc_g_fmt_vid_out = cx25821_vidioc_g_fmt_vid_cap,
637e90878abSHans Verkuil 	.vidioc_try_fmt_vid_out = cx25821_vidioc_try_fmt_vid_out,
638e90878abSHans Verkuil 	.vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
6391f198870SHans Verkuil 	.vidioc_g_std = cx25821_vidioc_g_std,
6401f198870SHans Verkuil 	.vidioc_s_std = cx25821_vidioc_s_std,
6411f198870SHans Verkuil 	.vidioc_enum_output = cx25821_vidioc_enum_output,
6421f198870SHans Verkuil 	.vidioc_g_output = cx25821_vidioc_g_output,
6431f198870SHans Verkuil 	.vidioc_s_output = cx25821_vidioc_s_output,
6441f198870SHans Verkuil 	.vidioc_log_status = vidioc_log_status,
6451f198870SHans Verkuil };
6461f198870SHans Verkuil 
6471f198870SHans Verkuil static const struct video_device cx25821_video_out_device = {
6481f198870SHans Verkuil 	.name = "cx25821-video",
6491f198870SHans Verkuil 	.fops = &video_out_fops,
6501f198870SHans Verkuil 	.release = video_device_release_empty,
6511f198870SHans Verkuil 	.minor = -1,
6521f198870SHans Verkuil 	.ioctl_ops = &video_out_ioctl_ops,
6531f198870SHans Verkuil 	.tvnorms = CX25821_NORMS,
65421615365SHans Verkuil 	.device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE,
6551f198870SHans Verkuil };
6561f198870SHans Verkuil 
cx25821_video_unregister(struct cx25821_dev * dev,int chan_num)657ffd3c233SHans Verkuil void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
658ffd3c233SHans Verkuil {
659ffd3c233SHans Verkuil 	cx_clear(PCI_INT_MSK, 1);
660ffd3c233SHans Verkuil 
661467870caSHans Verkuil 	if (video_is_registered(&dev->channels[chan_num].vdev)) {
662467870caSHans Verkuil 		video_unregister_device(&dev->channels[chan_num].vdev);
663f8d7ee70SHans Verkuil 		v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
664ffd3c233SHans Verkuil 	}
665ffd3c233SHans Verkuil }
666ffd3c233SHans Verkuil 
cx25821_video_register(struct cx25821_dev * dev)667ffd3c233SHans Verkuil int cx25821_video_register(struct cx25821_dev *dev)
668ffd3c233SHans Verkuil {
669ffd3c233SHans Verkuil 	int err;
670ffd3c233SHans Verkuil 	int i;
671ffd3c233SHans Verkuil 
672be178cb4SHans Verkuil 	/* initial device configuration */
673a6aa0dc4SHans Verkuil 	dev->tvnorm = V4L2_STD_NTSC_M;
674be178cb4SHans Verkuil 
675ffd3c233SHans Verkuil 	spin_lock_init(&dev->slock);
676ffd3c233SHans Verkuil 
677b6f21dc3SHans Verkuil 	for (i = 0; i < MAX_VID_CAP_CHANNEL_NUM - 1; ++i) {
6782efe2cc4SHans Verkuil 		struct cx25821_channel *chan = &dev->channels[i];
6792efe2cc4SHans Verkuil 		struct video_device *vdev = &chan->vdev;
6802efe2cc4SHans Verkuil 		struct v4l2_ctrl_handler *hdl = &chan->hdl;
681b671ae6bSHans Verkuil 		struct vb2_queue *q;
6821f198870SHans Verkuil 		bool is_output = i > SRAM_CH08;
683467870caSHans Verkuil 
684ffd3c233SHans Verkuil 		if (i == SRAM_CH08) /* audio channel */
685ffd3c233SHans Verkuil 			continue;
686ffd3c233SHans Verkuil 
6871f198870SHans Verkuil 		if (!is_output) {
688f8d7ee70SHans Verkuil 			v4l2_ctrl_handler_init(hdl, 4);
689f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
690f8d7ee70SHans Verkuil 					V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
691f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
692f8d7ee70SHans Verkuil 					V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
693f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
694f8d7ee70SHans Verkuil 					V4L2_CID_SATURATION, 0, 10000, 1, 5000);
695f8d7ee70SHans Verkuil 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
696f8d7ee70SHans Verkuil 					V4L2_CID_HUE, 0, 10000, 1, 5000);
697f8d7ee70SHans Verkuil 			if (hdl->error) {
698f8d7ee70SHans Verkuil 				err = hdl->error;
699f8d7ee70SHans Verkuil 				goto fail_unreg;
700f8d7ee70SHans Verkuil 			}
701be178cb4SHans Verkuil 			err = v4l2_ctrl_handler_setup(hdl);
702be178cb4SHans Verkuil 			if (err)
703be178cb4SHans Verkuil 				goto fail_unreg;
7047087d31bSHans Verkuil 		} else {
7057087d31bSHans Verkuil 			chan->out = &dev->vid_out_data[i - SRAM_CH09];
7067087d31bSHans Verkuil 			chan->out->chan = chan;
7071f198870SHans Verkuil 		}
708ffd3c233SHans Verkuil 
7092efe2cc4SHans Verkuil 		chan->sram_channels = &cx25821_sram_channels[i];
7102efe2cc4SHans Verkuil 		chan->width = 720;
711b671ae6bSHans Verkuil 		chan->field = V4L2_FIELD_INTERLACED;
7122efe2cc4SHans Verkuil 		if (dev->tvnorm & V4L2_STD_625_50)
7132efe2cc4SHans Verkuil 			chan->height = 576;
7142efe2cc4SHans Verkuil 		else
7152efe2cc4SHans Verkuil 			chan->height = 480;
716ffd3c233SHans Verkuil 
7172efe2cc4SHans Verkuil 		if (chan->pixel_formats == PIXEL_FRMT_411)
7182efe2cc4SHans Verkuil 			chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
7192efe2cc4SHans Verkuil 		else
7202efe2cc4SHans Verkuil 			chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
721ffd3c233SHans Verkuil 
7222efe2cc4SHans Verkuil 		cx_write(chan->sram_channels->int_stat, 0xffffffff);
723ffd3c233SHans Verkuil 
7242efe2cc4SHans Verkuil 		INIT_LIST_HEAD(&chan->dma_vidq.active);
7252efe2cc4SHans Verkuil 
726b671ae6bSHans Verkuil 		q = &chan->vidq;
7272efe2cc4SHans Verkuil 
728b671ae6bSHans Verkuil 		q->type = is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
729b671ae6bSHans Verkuil 				      V4L2_BUF_TYPE_VIDEO_CAPTURE;
730b671ae6bSHans Verkuil 		q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
731b671ae6bSHans Verkuil 		q->io_modes |= is_output ? VB2_WRITE : VB2_READ;
732b671ae6bSHans Verkuil 		q->gfp_flags = GFP_DMA32;
733*80c2b40aSBenjamin Gaignard 		q->min_queued_buffers = 2;
734b671ae6bSHans Verkuil 		q->drv_priv = chan;
735b671ae6bSHans Verkuil 		q->buf_struct_size = sizeof(struct cx25821_buffer);
736b671ae6bSHans Verkuil 		q->ops = &cx25821_video_qops;
737b671ae6bSHans Verkuil 		q->mem_ops = &vb2_dma_sg_memops;
738b671ae6bSHans Verkuil 		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
739b671ae6bSHans Verkuil 		q->lock = &dev->lock;
7402bc46b3aSHans Verkuil 		q->dev = &dev->pci->dev;
741b671ae6bSHans Verkuil 
742b671ae6bSHans Verkuil 		if (!is_output) {
743b671ae6bSHans Verkuil 			err = vb2_queue_init(q);
744b671ae6bSHans Verkuil 			if (err < 0)
745b671ae6bSHans Verkuil 				goto fail_unreg;
746b671ae6bSHans Verkuil 		}
747ffd3c233SHans Verkuil 
748ffd3c233SHans Verkuil 		/* register v4l devices */
7491f198870SHans Verkuil 		*vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
750467870caSHans Verkuil 		vdev->v4l2_dev = &dev->v4l2_dev;
7511f198870SHans Verkuil 		if (!is_output)
752f8d7ee70SHans Verkuil 			vdev->ctrl_handler = hdl;
7531f198870SHans Verkuil 		else
7541f198870SHans Verkuil 			vdev->vfl_dir = VFL_DIR_TX;
755be178cb4SHans Verkuil 		vdev->lock = &dev->lock;
756b671ae6bSHans Verkuil 		vdev->queue = q;
757467870caSHans Verkuil 		snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
7582efe2cc4SHans Verkuil 		video_set_drvdata(vdev, chan);
759ffd3c233SHans Verkuil 
7603e30a927SHans Verkuil 		err = video_register_device(vdev, VFL_TYPE_VIDEO,
761467870caSHans Verkuil 					    video_nr[dev->nr]);
762ffd3c233SHans Verkuil 
763ffd3c233SHans Verkuil 		if (err < 0)
764ffd3c233SHans Verkuil 			goto fail_unreg;
765ffd3c233SHans Verkuil 	}
766ffd3c233SHans Verkuil 
767ffd3c233SHans Verkuil 	/* set PCI interrupt */
768ffd3c233SHans Verkuil 	cx_set(PCI_INT_MSK, 0xff);
769ffd3c233SHans Verkuil 
770ffd3c233SHans Verkuil 	return 0;
771ffd3c233SHans Verkuil 
772ffd3c233SHans Verkuil fail_unreg:
773467870caSHans Verkuil 	while (i >= 0)
774467870caSHans Verkuil 		cx25821_video_unregister(dev, i--);
775ffd3c233SHans Verkuil 	return err;
776ffd3c233SHans Verkuil }
777