xref: /linux/drivers/media/pci/cx23885/cx23885-video.c (revision 453afdd9ce33293f640e84dc17e5f366701516e8)
1b285192aSMauro Carvalho Chehab /*
2b285192aSMauro Carvalho Chehab  *  Driver for the Conexant CX23885 PCIe bridge
3b285192aSMauro Carvalho Chehab  *
4b285192aSMauro Carvalho Chehab  *  Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
5b285192aSMauro Carvalho Chehab  *
6b285192aSMauro Carvalho Chehab  *  This program is free software; you can redistribute it and/or modify
7b285192aSMauro Carvalho Chehab  *  it under the terms of the GNU General Public License as published by
8b285192aSMauro Carvalho Chehab  *  the Free Software Foundation; either version 2 of the License, or
9b285192aSMauro Carvalho Chehab  *  (at your option) any later version.
10b285192aSMauro Carvalho Chehab  *
11b285192aSMauro Carvalho Chehab  *  This program is distributed in the hope that it will be useful,
12b285192aSMauro Carvalho Chehab  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13b285192aSMauro Carvalho Chehab  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14b285192aSMauro Carvalho Chehab  *
15b285192aSMauro Carvalho Chehab  *  GNU General Public License for more details.
16b285192aSMauro Carvalho Chehab  */
17b285192aSMauro Carvalho Chehab 
18b285192aSMauro Carvalho Chehab #include <linux/init.h>
19b285192aSMauro Carvalho Chehab #include <linux/list.h>
20b285192aSMauro Carvalho Chehab #include <linux/module.h>
21b285192aSMauro Carvalho Chehab #include <linux/moduleparam.h>
22b285192aSMauro Carvalho Chehab #include <linux/kmod.h>
23b285192aSMauro Carvalho Chehab #include <linux/kernel.h>
24b285192aSMauro Carvalho Chehab #include <linux/slab.h>
25b285192aSMauro Carvalho Chehab #include <linux/interrupt.h>
26b285192aSMauro Carvalho Chehab #include <linux/delay.h>
27b285192aSMauro Carvalho Chehab #include <linux/kthread.h>
28b285192aSMauro Carvalho Chehab #include <asm/div64.h>
29b285192aSMauro Carvalho Chehab 
30b285192aSMauro Carvalho Chehab #include "cx23885.h"
319f159524SMauro Carvalho Chehab #include "cx23885-video.h"
32b285192aSMauro Carvalho Chehab #include <media/v4l2-common.h>
33b285192aSMauro Carvalho Chehab #include <media/v4l2-ioctl.h>
34da59a4deSHans Verkuil #include <media/v4l2-event.h>
35b285192aSMauro Carvalho Chehab #include "cx23885-ioctl.h"
36b285192aSMauro Carvalho Chehab #include "tuner-xc2028.h"
37b285192aSMauro Carvalho Chehab 
38b285192aSMauro Carvalho Chehab #include <media/cx25840.h>
39b285192aSMauro Carvalho Chehab 
40b285192aSMauro Carvalho Chehab MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
41b285192aSMauro Carvalho Chehab MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
42b285192aSMauro Carvalho Chehab MODULE_LICENSE("GPL");
43b285192aSMauro Carvalho Chehab 
44b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
45b285192aSMauro Carvalho Chehab 
46b285192aSMauro Carvalho Chehab static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
47b285192aSMauro Carvalho Chehab static unsigned int vbi_nr[]   = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
48b285192aSMauro Carvalho Chehab 
49b285192aSMauro Carvalho Chehab module_param_array(video_nr, int, NULL, 0444);
50b285192aSMauro Carvalho Chehab module_param_array(vbi_nr,   int, NULL, 0444);
51b285192aSMauro Carvalho Chehab 
52b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_nr, "video device numbers");
53b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
54b285192aSMauro Carvalho Chehab 
55b285192aSMauro Carvalho Chehab static unsigned int video_debug;
56b285192aSMauro Carvalho Chehab module_param(video_debug, int, 0644);
57b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
58b285192aSMauro Carvalho Chehab 
59b285192aSMauro Carvalho Chehab static unsigned int irq_debug;
60b285192aSMauro Carvalho Chehab module_param(irq_debug, int, 0644);
61b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
62b285192aSMauro Carvalho Chehab 
63b285192aSMauro Carvalho Chehab static unsigned int vid_limit = 16;
64b285192aSMauro Carvalho Chehab module_param(vid_limit, int, 0644);
65b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
66b285192aSMauro Carvalho Chehab 
67b285192aSMauro Carvalho Chehab #define dprintk(level, fmt, arg...)\
68b285192aSMauro Carvalho Chehab 	do { if (video_debug >= level)\
69b285192aSMauro Carvalho Chehab 		printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
70b285192aSMauro Carvalho Chehab 	} while (0)
71b285192aSMauro Carvalho Chehab 
72b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------- */
73b285192aSMauro Carvalho Chehab /* static data                                                         */
74b285192aSMauro Carvalho Chehab 
75b285192aSMauro Carvalho Chehab #define FORMAT_FLAGS_PACKED       0x01
76b285192aSMauro Carvalho Chehab static struct cx23885_fmt formats[] = {
77b285192aSMauro Carvalho Chehab 	{
78b285192aSMauro Carvalho Chehab 		.name     = "4:2:2, packed, YUYV",
79b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_YUYV,
80b285192aSMauro Carvalho Chehab 		.depth    = 16,
81b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
82b285192aSMauro Carvalho Chehab 	}
83b285192aSMauro Carvalho Chehab };
84b285192aSMauro Carvalho Chehab 
85b285192aSMauro Carvalho Chehab static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
86b285192aSMauro Carvalho Chehab {
87b285192aSMauro Carvalho Chehab 	unsigned int i;
88b285192aSMauro Carvalho Chehab 
89b285192aSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(formats); i++)
90b285192aSMauro Carvalho Chehab 		if (formats[i].fourcc == fourcc)
91b285192aSMauro Carvalho Chehab 			return formats+i;
92b285192aSMauro Carvalho Chehab 	return NULL;
93b285192aSMauro Carvalho Chehab }
94b285192aSMauro Carvalho Chehab 
95b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------- */
96b285192aSMauro Carvalho Chehab 
97b285192aSMauro Carvalho Chehab void cx23885_video_wakeup(struct cx23885_dev *dev,
98b285192aSMauro Carvalho Chehab 	struct cx23885_dmaqueue *q, u32 count)
99b285192aSMauro Carvalho Chehab {
100b285192aSMauro Carvalho Chehab 	struct cx23885_buffer *buf;
101b285192aSMauro Carvalho Chehab 
102b285192aSMauro Carvalho Chehab 	if (list_empty(&q->active))
103*453afdd9SHans Verkuil 		return;
104b285192aSMauro Carvalho Chehab 	buf = list_entry(q->active.next,
105*453afdd9SHans Verkuil 			struct cx23885_buffer, queue);
106b285192aSMauro Carvalho Chehab 
107*453afdd9SHans Verkuil 	buf->vb.v4l2_buf.sequence = q->count++;
108*453afdd9SHans Verkuil 	v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
109*453afdd9SHans Verkuil 	dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.v4l2_buf.index,
110*453afdd9SHans Verkuil 			count, q->count);
111*453afdd9SHans Verkuil 	list_del(&buf->queue);
112*453afdd9SHans Verkuil 	vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
113b285192aSMauro Carvalho Chehab }
114b285192aSMauro Carvalho Chehab 
115b285192aSMauro Carvalho Chehab int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
116b285192aSMauro Carvalho Chehab {
117b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
118b285192aSMauro Carvalho Chehab 		__func__,
119b285192aSMauro Carvalho Chehab 		(unsigned int)norm,
120b285192aSMauro Carvalho Chehab 		v4l2_norm_to_name(norm));
121b285192aSMauro Carvalho Chehab 
122b285192aSMauro Carvalho Chehab 	dev->tvnorm = norm;
123b285192aSMauro Carvalho Chehab 
1248774bed9SLaurent Pinchart 	call_all(dev, video, s_std, norm);
125b285192aSMauro Carvalho Chehab 
126b285192aSMauro Carvalho Chehab 	return 0;
127b285192aSMauro Carvalho Chehab }
128b285192aSMauro Carvalho Chehab 
129b285192aSMauro Carvalho Chehab static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
130b285192aSMauro Carvalho Chehab 				    struct pci_dev *pci,
131b285192aSMauro Carvalho Chehab 				    struct video_device *template,
132b285192aSMauro Carvalho Chehab 				    char *type)
133b285192aSMauro Carvalho Chehab {
134b285192aSMauro Carvalho Chehab 	struct video_device *vfd;
135b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
136b285192aSMauro Carvalho Chehab 
137b285192aSMauro Carvalho Chehab 	vfd = video_device_alloc();
138b285192aSMauro Carvalho Chehab 	if (NULL == vfd)
139b285192aSMauro Carvalho Chehab 		return NULL;
140b285192aSMauro Carvalho Chehab 	*vfd = *template;
141b285192aSMauro Carvalho Chehab 	vfd->v4l2_dev = &dev->v4l2_dev;
142b285192aSMauro Carvalho Chehab 	vfd->release = video_device_release;
14324a8f7b5SHans Verkuil 	vfd->lock = &dev->lock;
144b285192aSMauro Carvalho Chehab 	snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
145b285192aSMauro Carvalho Chehab 		 cx23885_boards[dev->board].name, type);
146b285192aSMauro Carvalho Chehab 	video_set_drvdata(vfd, dev);
147b285192aSMauro Carvalho Chehab 	return vfd;
148b285192aSMauro Carvalho Chehab }
149b285192aSMauro Carvalho Chehab 
1505bc08e19SLuis Alves int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
151b285192aSMauro Carvalho Chehab {
152b285192aSMauro Carvalho Chehab 	/* 8 bit registers, 8 bit values */
153b285192aSMauro Carvalho Chehab 	u8 buf[] = { reg, data };
154b285192aSMauro Carvalho Chehab 
155b285192aSMauro Carvalho Chehab 	struct i2c_msg msg = { .addr = 0x98 >> 1,
156b285192aSMauro Carvalho Chehab 		.flags = 0, .buf = buf, .len = 2 };
157b285192aSMauro Carvalho Chehab 
158b285192aSMauro Carvalho Chehab 	return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
159b285192aSMauro Carvalho Chehab }
160b285192aSMauro Carvalho Chehab 
1615bc08e19SLuis Alves u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
162b285192aSMauro Carvalho Chehab {
163b285192aSMauro Carvalho Chehab 	/* 8 bit registers, 8 bit values */
164b285192aSMauro Carvalho Chehab 	int ret;
165b285192aSMauro Carvalho Chehab 	u8 b0[] = { reg };
166b285192aSMauro Carvalho Chehab 	u8 b1[] = { 0 };
167b285192aSMauro Carvalho Chehab 
168b285192aSMauro Carvalho Chehab 	struct i2c_msg msg[] = {
169b285192aSMauro Carvalho Chehab 		{ .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
170b285192aSMauro Carvalho Chehab 		{ .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
171b285192aSMauro Carvalho Chehab 	};
172b285192aSMauro Carvalho Chehab 
173b285192aSMauro Carvalho Chehab 	ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
174b285192aSMauro Carvalho Chehab 	if (ret != 2)
175b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() error\n", __func__);
176b285192aSMauro Carvalho Chehab 
177b285192aSMauro Carvalho Chehab 	return b1[0];
178b285192aSMauro Carvalho Chehab }
179b285192aSMauro Carvalho Chehab 
180b285192aSMauro Carvalho Chehab static void cx23885_flatiron_dump(struct cx23885_dev *dev)
181b285192aSMauro Carvalho Chehab {
182b285192aSMauro Carvalho Chehab 	int i;
183b285192aSMauro Carvalho Chehab 	dprintk(1, "Flatiron dump\n");
184b285192aSMauro Carvalho Chehab 	for (i = 0; i < 0x24; i++) {
185b285192aSMauro Carvalho Chehab 		dprintk(1, "FI[%02x] = %02x\n", i,
186b285192aSMauro Carvalho Chehab 			cx23885_flatiron_read(dev, i));
187b285192aSMauro Carvalho Chehab 	}
188b285192aSMauro Carvalho Chehab }
189b285192aSMauro Carvalho Chehab 
190b285192aSMauro Carvalho Chehab static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
191b285192aSMauro Carvalho Chehab {
192b285192aSMauro Carvalho Chehab 	u8 val;
193b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(input = %d)\n", __func__, input);
194b285192aSMauro Carvalho Chehab 
195b285192aSMauro Carvalho Chehab 	if (input == 1)
196b285192aSMauro Carvalho Chehab 		val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
197b285192aSMauro Carvalho Chehab 	else if (input == 2)
198b285192aSMauro Carvalho Chehab 		val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
199b285192aSMauro Carvalho Chehab 	else
200b285192aSMauro Carvalho Chehab 		return -EINVAL;
201b285192aSMauro Carvalho Chehab 
202b285192aSMauro Carvalho Chehab 	val |= 0x20; /* Enable clock to delta-sigma and dec filter */
203b285192aSMauro Carvalho Chehab 
204b285192aSMauro Carvalho Chehab 	cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
205b285192aSMauro Carvalho Chehab 
206b285192aSMauro Carvalho Chehab 	/* Wake up */
207b285192aSMauro Carvalho Chehab 	cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
208b285192aSMauro Carvalho Chehab 
209b285192aSMauro Carvalho Chehab 	if (video_debug)
210b285192aSMauro Carvalho Chehab 		cx23885_flatiron_dump(dev);
211b285192aSMauro Carvalho Chehab 
212b285192aSMauro Carvalho Chehab 	return 0;
213b285192aSMauro Carvalho Chehab }
214b285192aSMauro Carvalho Chehab 
215b285192aSMauro Carvalho Chehab static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
216b285192aSMauro Carvalho Chehab {
217b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
218b285192aSMauro Carvalho Chehab 		__func__,
219b285192aSMauro Carvalho Chehab 		input, INPUT(input)->vmux,
220b285192aSMauro Carvalho Chehab 		INPUT(input)->gpio0, INPUT(input)->gpio1,
221b285192aSMauro Carvalho Chehab 		INPUT(input)->gpio2, INPUT(input)->gpio3);
222b285192aSMauro Carvalho Chehab 	dev->input = input;
223b285192aSMauro Carvalho Chehab 
224b285192aSMauro Carvalho Chehab 	if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
225b285192aSMauro Carvalho Chehab 		dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
226b285192aSMauro Carvalho Chehab 		dev->board == CX23885_BOARD_MYGICA_X8507) {
227b285192aSMauro Carvalho Chehab 		/* Select Analog TV */
228b285192aSMauro Carvalho Chehab 		if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
229b285192aSMauro Carvalho Chehab 			cx23885_gpio_clear(dev, GPIO_0);
230b285192aSMauro Carvalho Chehab 	}
231b285192aSMauro Carvalho Chehab 
232b285192aSMauro Carvalho Chehab 	/* Tell the internal A/V decoder */
233b285192aSMauro Carvalho Chehab 	v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
234b285192aSMauro Carvalho Chehab 			INPUT(input)->vmux, 0, 0);
235b285192aSMauro Carvalho Chehab 
236b285192aSMauro Carvalho Chehab 	if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
237b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_MPX885) ||
238b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
239cce11b09SHans Verkuil 		(dev->board == CX23885_BOARD_HAUPPAUGE_IMPACTVCBE) ||
240b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
241b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
242082c0576SAlfredo Jesús Delaiti 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
243e8d42373SOleh Kravchenko 		(dev->board == CX23885_BOARD_MYGICA_X8507) ||
244e8d42373SOleh Kravchenko 		(dev->board == CX23885_BOARD_AVERMEDIA_HC81R)) {
245b285192aSMauro Carvalho Chehab 		/* Configure audio routing */
246b285192aSMauro Carvalho Chehab 		v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
247b285192aSMauro Carvalho Chehab 			INPUT(input)->amux, 0, 0);
248b285192aSMauro Carvalho Chehab 
249b285192aSMauro Carvalho Chehab 		if (INPUT(input)->amux == CX25840_AUDIO7)
250b285192aSMauro Carvalho Chehab 			cx23885_flatiron_mux(dev, 1);
251b285192aSMauro Carvalho Chehab 		else if (INPUT(input)->amux == CX25840_AUDIO6)
252b285192aSMauro Carvalho Chehab 			cx23885_flatiron_mux(dev, 2);
253b285192aSMauro Carvalho Chehab 	}
254b285192aSMauro Carvalho Chehab 
255b285192aSMauro Carvalho Chehab 	return 0;
256b285192aSMauro Carvalho Chehab }
257b285192aSMauro Carvalho Chehab 
258b285192aSMauro Carvalho Chehab static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
259b285192aSMauro Carvalho Chehab {
260b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(input=%d)\n", __func__, input);
261b285192aSMauro Carvalho Chehab 
262b285192aSMauro Carvalho Chehab 	/* The baseband video core of the cx23885 has two audio inputs.
263b285192aSMauro Carvalho Chehab 	 * LR1 and LR2. In almost every single case so far only HVR1xxx
264b285192aSMauro Carvalho Chehab 	 * cards we've only ever supported LR1. Time to support LR2,
265b285192aSMauro Carvalho Chehab 	 * which is available via the optional white breakout header on
266b285192aSMauro Carvalho Chehab 	 * the board.
267b285192aSMauro Carvalho Chehab 	 * We'll use a could of existing enums in the card struct to allow
268b285192aSMauro Carvalho Chehab 	 * devs to specify which baseband input they need, or just default
269b285192aSMauro Carvalho Chehab 	 * to what we've always used.
270b285192aSMauro Carvalho Chehab 	 */
271b285192aSMauro Carvalho Chehab 	if (INPUT(input)->amux == CX25840_AUDIO7)
272b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 1);
273b285192aSMauro Carvalho Chehab 	else if (INPUT(input)->amux == CX25840_AUDIO6)
274b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 2);
275b285192aSMauro Carvalho Chehab 	else {
276b285192aSMauro Carvalho Chehab 		/* Not specifically defined, assume the default. */
277b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 1);
278b285192aSMauro Carvalho Chehab 	}
279b285192aSMauro Carvalho Chehab 
280b285192aSMauro Carvalho Chehab 	return 0;
281b285192aSMauro Carvalho Chehab }
282b285192aSMauro Carvalho Chehab 
283b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
284b285192aSMauro Carvalho Chehab static int cx23885_start_video_dma(struct cx23885_dev *dev,
285b285192aSMauro Carvalho Chehab 			   struct cx23885_dmaqueue *q,
286b285192aSMauro Carvalho Chehab 			   struct cx23885_buffer *buf)
287b285192aSMauro Carvalho Chehab {
288b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
289b285192aSMauro Carvalho Chehab 
290b285192aSMauro Carvalho Chehab 	/* Stop the dma/fifo before we tamper with it's risc programs */
291b285192aSMauro Carvalho Chehab 	cx_clear(VID_A_DMA_CTL, 0x11);
292b285192aSMauro Carvalho Chehab 
293b285192aSMauro Carvalho Chehab 	/* setup fifo + format */
294b285192aSMauro Carvalho Chehab 	cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
295b285192aSMauro Carvalho Chehab 				buf->bpl, buf->risc.dma);
296b285192aSMauro Carvalho Chehab 
297b285192aSMauro Carvalho Chehab 	/* reset counter */
298b285192aSMauro Carvalho Chehab 	cx_write(VID_A_GPCNT_CTL, 3);
299*453afdd9SHans Verkuil 	q->count = 0;
300b285192aSMauro Carvalho Chehab 
301b285192aSMauro Carvalho Chehab 	/* enable irq */
302b285192aSMauro Carvalho Chehab 	cx23885_irq_add_enable(dev, 0x01);
303b285192aSMauro Carvalho Chehab 	cx_set(VID_A_INT_MSK, 0x000011);
304b285192aSMauro Carvalho Chehab 
305b285192aSMauro Carvalho Chehab 	/* start dma */
306b285192aSMauro Carvalho Chehab 	cx_set(DEV_CNTRL2, (1<<5));
307b285192aSMauro Carvalho Chehab 	cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
308b285192aSMauro Carvalho Chehab 
309b285192aSMauro Carvalho Chehab 	return 0;
310b285192aSMauro Carvalho Chehab }
311b285192aSMauro Carvalho Chehab 
312*453afdd9SHans Verkuil static int queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
313*453afdd9SHans Verkuil 			   unsigned int *num_buffers, unsigned int *num_planes,
314*453afdd9SHans Verkuil 			   unsigned int sizes[], void *alloc_ctxs[])
315b285192aSMauro Carvalho Chehab {
316*453afdd9SHans Verkuil 	struct cx23885_dev *dev = q->drv_priv;
317b285192aSMauro Carvalho Chehab 
318*453afdd9SHans Verkuil 	*num_planes = 1;
319*453afdd9SHans Verkuil 	sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
320b285192aSMauro Carvalho Chehab 	return 0;
321b285192aSMauro Carvalho Chehab }
322b285192aSMauro Carvalho Chehab 
323*453afdd9SHans Verkuil static int buffer_prepare(struct vb2_buffer *vb)
324b285192aSMauro Carvalho Chehab {
325*453afdd9SHans Verkuil 	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
326b285192aSMauro Carvalho Chehab 	struct cx23885_buffer *buf =
327b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx23885_buffer, vb);
328b285192aSMauro Carvalho Chehab 	u32 line0_offset, line1_offset;
329*453afdd9SHans Verkuil 	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
330b285192aSMauro Carvalho Chehab 	int field_tff;
331*453afdd9SHans Verkuil 	int ret;
332b285192aSMauro Carvalho Chehab 
333*453afdd9SHans Verkuil 	buf->bpl = (dev->width * dev->fmt->depth) >> 3;
334*453afdd9SHans Verkuil 
335*453afdd9SHans Verkuil 	if (vb2_plane_size(vb, 0) < dev->height * buf->bpl)
336b285192aSMauro Carvalho Chehab 		return -EINVAL;
337*453afdd9SHans Verkuil 	vb2_set_plane_payload(vb, 0, dev->height * buf->bpl);
33891d2d674SHans Verkuil 
339*453afdd9SHans Verkuil 	ret = dma_map_sg(&dev->pci->dev, sgt->sgl, sgt->nents, DMA_FROM_DEVICE);
340*453afdd9SHans Verkuil 	if (!ret)
341*453afdd9SHans Verkuil 		return -EIO;
342b285192aSMauro Carvalho Chehab 
343*453afdd9SHans Verkuil 	switch (dev->field) {
344b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_TOP:
345b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
346*453afdd9SHans Verkuil 				sgt->sgl, 0, UNSET,
347*453afdd9SHans Verkuil 				buf->bpl, 0, dev->height);
348b285192aSMauro Carvalho Chehab 		break;
349b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_BOTTOM:
350b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
351*453afdd9SHans Verkuil 				sgt->sgl, UNSET, 0,
352*453afdd9SHans Verkuil 				buf->bpl, 0, dev->height);
353b285192aSMauro Carvalho Chehab 		break;
354b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_INTERLACED:
355b285192aSMauro Carvalho Chehab 		if (dev->tvnorm & V4L2_STD_NTSC)
356b285192aSMauro Carvalho Chehab 			/* NTSC or  */
357b285192aSMauro Carvalho Chehab 			field_tff = 1;
358b285192aSMauro Carvalho Chehab 		else
359b285192aSMauro Carvalho Chehab 			field_tff = 0;
360b285192aSMauro Carvalho Chehab 
361b285192aSMauro Carvalho Chehab 		if (cx23885_boards[dev->board].force_bff)
362b285192aSMauro Carvalho Chehab 			/* PAL / SECAM OR 888 in NTSC MODE */
363b285192aSMauro Carvalho Chehab 			field_tff = 0;
364b285192aSMauro Carvalho Chehab 
365b285192aSMauro Carvalho Chehab 		if (field_tff) {
366b285192aSMauro Carvalho Chehab 			/* cx25840 transmits NTSC bottom field first */
367b285192aSMauro Carvalho Chehab 			dprintk(1, "%s() Creating TFF/NTSC risc\n",
368b285192aSMauro Carvalho Chehab 					__func__);
369b285192aSMauro Carvalho Chehab 			line0_offset = buf->bpl;
370b285192aSMauro Carvalho Chehab 			line1_offset = 0;
371b285192aSMauro Carvalho Chehab 		} else {
372b285192aSMauro Carvalho Chehab 			/* All other formats are top field first */
373b285192aSMauro Carvalho Chehab 			dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
374b285192aSMauro Carvalho Chehab 					__func__);
375b285192aSMauro Carvalho Chehab 			line0_offset = 0;
376b285192aSMauro Carvalho Chehab 			line1_offset = buf->bpl;
377b285192aSMauro Carvalho Chehab 		}
378b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
379*453afdd9SHans Verkuil 				sgt->sgl, line0_offset,
380b285192aSMauro Carvalho Chehab 				line1_offset,
381b285192aSMauro Carvalho Chehab 				buf->bpl, buf->bpl,
382*453afdd9SHans Verkuil 				dev->height >> 1);
383b285192aSMauro Carvalho Chehab 		break;
384b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_TB:
385b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
386*453afdd9SHans Verkuil 				sgt->sgl,
387*453afdd9SHans Verkuil 				0, buf->bpl * (dev->height >> 1),
388b285192aSMauro Carvalho Chehab 				buf->bpl, 0,
389*453afdd9SHans Verkuil 				dev->height >> 1);
390b285192aSMauro Carvalho Chehab 		break;
391b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_BT:
392b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
393*453afdd9SHans Verkuil 				sgt->sgl,
394*453afdd9SHans Verkuil 				buf->bpl * (dev->height >> 1), 0,
395b285192aSMauro Carvalho Chehab 				buf->bpl, 0,
396*453afdd9SHans Verkuil 				dev->height >> 1);
397b285192aSMauro Carvalho Chehab 		break;
398b285192aSMauro Carvalho Chehab 	default:
399b285192aSMauro Carvalho Chehab 		BUG();
400b285192aSMauro Carvalho Chehab 	}
401*453afdd9SHans Verkuil 	dprintk(2, "[%p/%d] buffer_init - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
402*453afdd9SHans Verkuil 		buf, buf->vb.v4l2_buf.index,
40391d2d674SHans Verkuil 		dev->width, dev->height, dev->fmt->depth, dev->fmt->name,
404b285192aSMauro Carvalho Chehab 		(unsigned long)buf->risc.dma);
405b285192aSMauro Carvalho Chehab 	return 0;
406b285192aSMauro Carvalho Chehab }
407b285192aSMauro Carvalho Chehab 
408*453afdd9SHans Verkuil static void buffer_finish(struct vb2_buffer *vb)
409b285192aSMauro Carvalho Chehab {
410*453afdd9SHans Verkuil 	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
411*453afdd9SHans Verkuil 	struct cx23885_buffer *buf = container_of(vb,
412*453afdd9SHans Verkuil 		struct cx23885_buffer, vb);
413*453afdd9SHans Verkuil 	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
414*453afdd9SHans Verkuil 
415*453afdd9SHans Verkuil 	cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
416*453afdd9SHans Verkuil 
417*453afdd9SHans Verkuil 	dma_unmap_sg(&dev->pci->dev, sgt->sgl, sgt->nents, DMA_FROM_DEVICE);
418*453afdd9SHans Verkuil }
419*453afdd9SHans Verkuil 
420*453afdd9SHans Verkuil /*
421*453afdd9SHans Verkuil  * The risc program for each buffer works as follows: it starts with a simple
422*453afdd9SHans Verkuil  * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
423*453afdd9SHans Verkuil  * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
424*453afdd9SHans Verkuil  * the initial JUMP).
425*453afdd9SHans Verkuil  *
426*453afdd9SHans Verkuil  * This is the risc program of the first buffer to be queued if the active list
427*453afdd9SHans Verkuil  * is empty and it just keeps DMAing this buffer without generating any
428*453afdd9SHans Verkuil  * interrupts.
429*453afdd9SHans Verkuil  *
430*453afdd9SHans Verkuil  * If a new buffer is added then the initial JUMP in the code for that buffer
431*453afdd9SHans Verkuil  * will generate an interrupt which signals that the previous buffer has been
432*453afdd9SHans Verkuil  * DMAed successfully and that it can be returned to userspace.
433*453afdd9SHans Verkuil  *
434*453afdd9SHans Verkuil  * It also sets the final jump of the previous buffer to the start of the new
435*453afdd9SHans Verkuil  * buffer, thus chaining the new buffer into the DMA chain. This is a single
436*453afdd9SHans Verkuil  * atomic u32 write, so there is no race condition.
437*453afdd9SHans Verkuil  *
438*453afdd9SHans Verkuil  * The end-result of all this that you only get an interrupt when a buffer
439*453afdd9SHans Verkuil  * is ready, so the control flow is very easy.
440*453afdd9SHans Verkuil  */
441*453afdd9SHans Verkuil static void buffer_queue(struct vb2_buffer *vb)
442*453afdd9SHans Verkuil {
443*453afdd9SHans Verkuil 	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
444b285192aSMauro Carvalho Chehab 	struct cx23885_buffer   *buf = container_of(vb,
445b285192aSMauro Carvalho Chehab 		struct cx23885_buffer, vb);
446b285192aSMauro Carvalho Chehab 	struct cx23885_buffer   *prev;
447b285192aSMauro Carvalho Chehab 	struct cx23885_dmaqueue *q    = &dev->vidq;
448*453afdd9SHans Verkuil 	unsigned long flags;
449b285192aSMauro Carvalho Chehab 
450*453afdd9SHans Verkuil 	/* add jump to start */
451*453afdd9SHans Verkuil 	buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
452*453afdd9SHans Verkuil 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
453*453afdd9SHans Verkuil 	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
454b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
455b285192aSMauro Carvalho Chehab 
456*453afdd9SHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
457*453afdd9SHans Verkuil 	if (list_empty(&q->active)) {
458*453afdd9SHans Verkuil 		list_add_tail(&buf->queue, &q->active);
459b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - first active\n",
460*453afdd9SHans Verkuil 			buf, buf->vb.v4l2_buf.index);
461b285192aSMauro Carvalho Chehab 	} else {
462*453afdd9SHans Verkuil 		buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
463b285192aSMauro Carvalho Chehab 		prev = list_entry(q->active.prev, struct cx23885_buffer,
464*453afdd9SHans Verkuil 			queue);
465*453afdd9SHans Verkuil 		list_add_tail(&buf->queue, &q->active);
466b285192aSMauro Carvalho Chehab 		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
467b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - append to active\n",
468*453afdd9SHans Verkuil 				buf, buf->vb.v4l2_buf.index);
469b285192aSMauro Carvalho Chehab 	}
470*453afdd9SHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
471b285192aSMauro Carvalho Chehab }
472b285192aSMauro Carvalho Chehab 
473*453afdd9SHans Verkuil static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
474b285192aSMauro Carvalho Chehab {
475*453afdd9SHans Verkuil 	struct cx23885_dev *dev = q->drv_priv;
476*453afdd9SHans Verkuil 	struct cx23885_dmaqueue *dmaq = &dev->vidq;
477*453afdd9SHans Verkuil 	struct cx23885_buffer *buf = list_entry(dmaq->active.next,
478*453afdd9SHans Verkuil 			struct cx23885_buffer, queue);
479b285192aSMauro Carvalho Chehab 
480*453afdd9SHans Verkuil 	cx23885_start_video_dma(dev, dmaq, buf);
481*453afdd9SHans Verkuil 	return 0;
482b285192aSMauro Carvalho Chehab }
483b285192aSMauro Carvalho Chehab 
484*453afdd9SHans Verkuil static void cx23885_stop_streaming(struct vb2_queue *q)
485*453afdd9SHans Verkuil {
486*453afdd9SHans Verkuil 	struct cx23885_dev *dev = q->drv_priv;
487*453afdd9SHans Verkuil 	struct cx23885_dmaqueue *dmaq = &dev->vidq;
488*453afdd9SHans Verkuil 	unsigned long flags;
489*453afdd9SHans Verkuil 
490*453afdd9SHans Verkuil 	cx_clear(VID_A_DMA_CTL, 0x11);
491*453afdd9SHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
492*453afdd9SHans Verkuil 	while (!list_empty(&dmaq->active)) {
493*453afdd9SHans Verkuil 		struct cx23885_buffer *buf = list_entry(dmaq->active.next,
494*453afdd9SHans Verkuil 			struct cx23885_buffer, queue);
495*453afdd9SHans Verkuil 
496*453afdd9SHans Verkuil 		list_del(&buf->queue);
497*453afdd9SHans Verkuil 		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
498*453afdd9SHans Verkuil 	}
499*453afdd9SHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
500*453afdd9SHans Verkuil }
501*453afdd9SHans Verkuil 
502*453afdd9SHans Verkuil static struct vb2_ops cx23885_video_qops = {
503*453afdd9SHans Verkuil 	.queue_setup    = queue_setup,
504b285192aSMauro Carvalho Chehab 	.buf_prepare  = buffer_prepare,
505*453afdd9SHans Verkuil 	.buf_finish = buffer_finish,
506b285192aSMauro Carvalho Chehab 	.buf_queue    = buffer_queue,
507*453afdd9SHans Verkuil 	.wait_prepare = vb2_ops_wait_prepare,
508*453afdd9SHans Verkuil 	.wait_finish = vb2_ops_wait_finish,
509*453afdd9SHans Verkuil 	.start_streaming = cx23885_start_streaming,
510*453afdd9SHans Verkuil 	.stop_streaming = cx23885_stop_streaming,
511b285192aSMauro Carvalho Chehab };
512b285192aSMauro Carvalho Chehab 
513b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
514b285192aSMauro Carvalho Chehab /* VIDEO IOCTLS                                                       */
515b285192aSMauro Carvalho Chehab 
516b285192aSMauro Carvalho Chehab static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
517b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
518b285192aSMauro Carvalho Chehab {
519568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
520b285192aSMauro Carvalho Chehab 
52191d2d674SHans Verkuil 	f->fmt.pix.width        = dev->width;
52291d2d674SHans Verkuil 	f->fmt.pix.height       = dev->height;
523*453afdd9SHans Verkuil 	f->fmt.pix.field        = dev->field;
52491d2d674SHans Verkuil 	f->fmt.pix.pixelformat  = dev->fmt->fourcc;
525b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline =
52691d2d674SHans Verkuil 		(f->fmt.pix.width * dev->fmt->depth) >> 3;
527b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage =
528b285192aSMauro Carvalho Chehab 		f->fmt.pix.height * f->fmt.pix.bytesperline;
5291af2ddd8SHans Verkuil 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
530b285192aSMauro Carvalho Chehab 
531b285192aSMauro Carvalho Chehab 	return 0;
532b285192aSMauro Carvalho Chehab }
533b285192aSMauro Carvalho Chehab 
534b285192aSMauro Carvalho Chehab static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
535b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
536b285192aSMauro Carvalho Chehab {
537568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
538b285192aSMauro Carvalho Chehab 	struct cx23885_fmt *fmt;
539b285192aSMauro Carvalho Chehab 	enum v4l2_field   field;
540b285192aSMauro Carvalho Chehab 	unsigned int      maxw, maxh;
541b285192aSMauro Carvalho Chehab 
542b285192aSMauro Carvalho Chehab 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
543b285192aSMauro Carvalho Chehab 	if (NULL == fmt)
544b285192aSMauro Carvalho Chehab 		return -EINVAL;
545b285192aSMauro Carvalho Chehab 
546b285192aSMauro Carvalho Chehab 	field = f->fmt.pix.field;
547b285192aSMauro Carvalho Chehab 	maxw  = norm_maxw(dev->tvnorm);
548b285192aSMauro Carvalho Chehab 	maxh  = norm_maxh(dev->tvnorm);
549b285192aSMauro Carvalho Chehab 
550b285192aSMauro Carvalho Chehab 	if (V4L2_FIELD_ANY == field) {
551b285192aSMauro Carvalho Chehab 		field = (f->fmt.pix.height > maxh/2)
552b285192aSMauro Carvalho Chehab 			? V4L2_FIELD_INTERLACED
553b285192aSMauro Carvalho Chehab 			: V4L2_FIELD_BOTTOM;
554b285192aSMauro Carvalho Chehab 	}
555b285192aSMauro Carvalho Chehab 
556b285192aSMauro Carvalho Chehab 	switch (field) {
557b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_TOP:
558b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_BOTTOM:
559b285192aSMauro Carvalho Chehab 		maxh = maxh / 2;
560b285192aSMauro Carvalho Chehab 		break;
561b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_INTERLACED:
562b285192aSMauro Carvalho Chehab 		break;
563b285192aSMauro Carvalho Chehab 	default:
564200e0841SHans Verkuil 		field = V4L2_FIELD_INTERLACED;
565200e0841SHans Verkuil 		break;
566b285192aSMauro Carvalho Chehab 	}
567b285192aSMauro Carvalho Chehab 
568b285192aSMauro Carvalho Chehab 	f->fmt.pix.field = field;
569b285192aSMauro Carvalho Chehab 	v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
570b285192aSMauro Carvalho Chehab 			      &f->fmt.pix.height, 32, maxh, 0, 0);
571b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline =
572b285192aSMauro Carvalho Chehab 		(f->fmt.pix.width * fmt->depth) >> 3;
573b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage =
574b285192aSMauro Carvalho Chehab 		f->fmt.pix.height * f->fmt.pix.bytesperline;
5751af2ddd8SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
576b285192aSMauro Carvalho Chehab 
577b285192aSMauro Carvalho Chehab 	return 0;
578b285192aSMauro Carvalho Chehab }
579b285192aSMauro Carvalho Chehab 
580b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
581b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
582b285192aSMauro Carvalho Chehab {
583568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
584b285192aSMauro Carvalho Chehab 	struct v4l2_mbus_framefmt mbus_fmt;
585b285192aSMauro Carvalho Chehab 	int err;
586b285192aSMauro Carvalho Chehab 
587b285192aSMauro Carvalho Chehab 	dprintk(2, "%s()\n", __func__);
588b285192aSMauro Carvalho Chehab 	err = vidioc_try_fmt_vid_cap(file, priv, f);
589b285192aSMauro Carvalho Chehab 
590b285192aSMauro Carvalho Chehab 	if (0 != err)
591b285192aSMauro Carvalho Chehab 		return err;
59291d2d674SHans Verkuil 	dev->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
59391d2d674SHans Verkuil 	dev->width      = f->fmt.pix.width;
59491d2d674SHans Verkuil 	dev->height     = f->fmt.pix.height;
595*453afdd9SHans Verkuil 	dev->field	= f->fmt.pix.field;
596b285192aSMauro Carvalho Chehab 	dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
597*453afdd9SHans Verkuil 		dev->width, dev->height, dev->field);
598b285192aSMauro Carvalho Chehab 	v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
599b285192aSMauro Carvalho Chehab 	call_all(dev, video, s_mbus_fmt, &mbus_fmt);
600b285192aSMauro Carvalho Chehab 	v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
601b285192aSMauro Carvalho Chehab 	return 0;
602b285192aSMauro Carvalho Chehab }
603b285192aSMauro Carvalho Chehab 
604b285192aSMauro Carvalho Chehab static int vidioc_querycap(struct file *file, void  *priv,
605b285192aSMauro Carvalho Chehab 	struct v4l2_capability *cap)
606b285192aSMauro Carvalho Chehab {
607568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
608a7d3eabdSHans Verkuil 	struct video_device *vdev = video_devdata(file);
609b285192aSMauro Carvalho Chehab 
610b285192aSMauro Carvalho Chehab 	strcpy(cap->driver, "cx23885");
611b285192aSMauro Carvalho Chehab 	strlcpy(cap->card, cx23885_boards[dev->board].name,
612b285192aSMauro Carvalho Chehab 		sizeof(cap->card));
613b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
614d43be757SHans Verkuil 	cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | V4L2_CAP_AUDIO;
6150f3bf3dcSHans Verkuil 	if (dev->tuner_type != TUNER_ABSENT)
616a7d3eabdSHans Verkuil 		cap->device_caps |= V4L2_CAP_TUNER;
617a7d3eabdSHans Verkuil 	if (vdev->vfl_type == VFL_TYPE_VBI)
618a7d3eabdSHans Verkuil 		cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
619a7d3eabdSHans Verkuil 	else
620a7d3eabdSHans Verkuil 		cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
621a7d3eabdSHans Verkuil 	cap->capabilities = cap->device_caps | V4L2_CAP_VBI_CAPTURE |
622a7d3eabdSHans Verkuil 		V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_DEVICE_CAPS;
623b285192aSMauro Carvalho Chehab 	return 0;
624b285192aSMauro Carvalho Chehab }
625b285192aSMauro Carvalho Chehab 
626b285192aSMauro Carvalho Chehab static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
627b285192aSMauro Carvalho Chehab 	struct v4l2_fmtdesc *f)
628b285192aSMauro Carvalho Chehab {
629b285192aSMauro Carvalho Chehab 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
630b285192aSMauro Carvalho Chehab 		return -EINVAL;
631b285192aSMauro Carvalho Chehab 
632b285192aSMauro Carvalho Chehab 	strlcpy(f->description, formats[f->index].name,
633b285192aSMauro Carvalho Chehab 		sizeof(f->description));
634b285192aSMauro Carvalho Chehab 	f->pixelformat = formats[f->index].fourcc;
635b285192aSMauro Carvalho Chehab 
636b285192aSMauro Carvalho Chehab 	return 0;
637b285192aSMauro Carvalho Chehab }
638b285192aSMauro Carvalho Chehab 
639b285192aSMauro Carvalho Chehab static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
640b285192aSMauro Carvalho Chehab {
641568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
642b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
643b285192aSMauro Carvalho Chehab 
6449c1f5df8SHans Verkuil 	*id = dev->tvnorm;
645b285192aSMauro Carvalho Chehab 	return 0;
646b285192aSMauro Carvalho Chehab }
647b285192aSMauro Carvalho Chehab 
648314527acSHans Verkuil static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
649b285192aSMauro Carvalho Chehab {
650568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
651b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
652b285192aSMauro Carvalho Chehab 
653314527acSHans Verkuil 	cx23885_set_tvnorm(dev, tvnorms);
654b285192aSMauro Carvalho Chehab 
655b285192aSMauro Carvalho Chehab 	return 0;
656b285192aSMauro Carvalho Chehab }
657b285192aSMauro Carvalho Chehab 
658b285192aSMauro Carvalho Chehab int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
659b285192aSMauro Carvalho Chehab {
660b285192aSMauro Carvalho Chehab 	static const char *iname[] = {
661b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE1] = "Composite1",
662b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE2] = "Composite2",
663b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE3] = "Composite3",
664b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE4] = "Composite4",
665b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_SVIDEO]     = "S-Video",
666b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPONENT]  = "Component",
667b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_TELEVISION] = "Television",
668b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_CABLE]      = "Cable TV",
669b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_DVB]        = "DVB",
670b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_DEBUG]      = "for debug only",
671b285192aSMauro Carvalho Chehab 	};
672b285192aSMauro Carvalho Chehab 	unsigned int n;
673b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
674b285192aSMauro Carvalho Chehab 
675b285192aSMauro Carvalho Chehab 	n = i->index;
676b285192aSMauro Carvalho Chehab 	if (n >= MAX_CX23885_INPUT)
677b285192aSMauro Carvalho Chehab 		return -EINVAL;
678b285192aSMauro Carvalho Chehab 
679b285192aSMauro Carvalho Chehab 	if (0 == INPUT(n)->type)
680b285192aSMauro Carvalho Chehab 		return -EINVAL;
681b285192aSMauro Carvalho Chehab 
682b285192aSMauro Carvalho Chehab 	i->index = n;
683b285192aSMauro Carvalho Chehab 	i->type  = V4L2_INPUT_TYPE_CAMERA;
684b285192aSMauro Carvalho Chehab 	strcpy(i->name, iname[INPUT(n)->type]);
685d43be757SHans Verkuil 	i->std = CX23885_NORMS;
686b285192aSMauro Carvalho Chehab 	if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
687b285192aSMauro Carvalho Chehab 		(CX23885_VMUX_CABLE == INPUT(n)->type)) {
688b285192aSMauro Carvalho Chehab 		i->type = V4L2_INPUT_TYPE_TUNER;
689d43be757SHans Verkuil 		i->audioset = 4;
690d43be757SHans Verkuil 	} else {
691b285192aSMauro Carvalho Chehab 		/* Two selectable audio inputs for non-tv inputs */
692d43be757SHans Verkuil 		i->audioset = 3;
693d43be757SHans Verkuil 	}
694b285192aSMauro Carvalho Chehab 
695b285192aSMauro Carvalho Chehab 	if (dev->input == n) {
696b285192aSMauro Carvalho Chehab 		/* enum'd input matches our configured input.
697b285192aSMauro Carvalho Chehab 		 * Ask the video decoder to process the call
698b285192aSMauro Carvalho Chehab 		 * and give it an oppertunity to update the
699b285192aSMauro Carvalho Chehab 		 * status field.
700b285192aSMauro Carvalho Chehab 		 */
701b285192aSMauro Carvalho Chehab 		call_all(dev, video, g_input_status, &i->status);
702b285192aSMauro Carvalho Chehab 	}
703b285192aSMauro Carvalho Chehab 
704b285192aSMauro Carvalho Chehab 	return 0;
705b285192aSMauro Carvalho Chehab }
706b285192aSMauro Carvalho Chehab 
707b285192aSMauro Carvalho Chehab static int vidioc_enum_input(struct file *file, void *priv,
708b285192aSMauro Carvalho Chehab 				struct v4l2_input *i)
709b285192aSMauro Carvalho Chehab {
710568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
711b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
712b285192aSMauro Carvalho Chehab 	return cx23885_enum_input(dev, i);
713b285192aSMauro Carvalho Chehab }
714b285192aSMauro Carvalho Chehab 
715b285192aSMauro Carvalho Chehab int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
716b285192aSMauro Carvalho Chehab {
717568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
718b285192aSMauro Carvalho Chehab 
719b285192aSMauro Carvalho Chehab 	*i = dev->input;
720b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() returns %d\n", __func__, *i);
721b285192aSMauro Carvalho Chehab 	return 0;
722b285192aSMauro Carvalho Chehab }
723b285192aSMauro Carvalho Chehab 
724b285192aSMauro Carvalho Chehab static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
725b285192aSMauro Carvalho Chehab {
726b285192aSMauro Carvalho Chehab 	return cx23885_get_input(file, priv, i);
727b285192aSMauro Carvalho Chehab }
728b285192aSMauro Carvalho Chehab 
729b285192aSMauro Carvalho Chehab int cx23885_set_input(struct file *file, void *priv, unsigned int i)
730b285192aSMauro Carvalho Chehab {
731568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
732b285192aSMauro Carvalho Chehab 
733b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(%d)\n", __func__, i);
734b285192aSMauro Carvalho Chehab 
735b285192aSMauro Carvalho Chehab 	if (i >= MAX_CX23885_INPUT) {
736b285192aSMauro Carvalho Chehab 		dprintk(1, "%s() -EINVAL\n", __func__);
737b285192aSMauro Carvalho Chehab 		return -EINVAL;
738b285192aSMauro Carvalho Chehab 	}
739b285192aSMauro Carvalho Chehab 
740b285192aSMauro Carvalho Chehab 	if (INPUT(i)->type == 0)
741b285192aSMauro Carvalho Chehab 		return -EINVAL;
742b285192aSMauro Carvalho Chehab 
743b285192aSMauro Carvalho Chehab 	cx23885_video_mux(dev, i);
744b285192aSMauro Carvalho Chehab 
745b285192aSMauro Carvalho Chehab 	/* By default establish the default audio input for the card also */
746b285192aSMauro Carvalho Chehab 	/* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
747b285192aSMauro Carvalho Chehab 	cx23885_audio_mux(dev, i);
748b285192aSMauro Carvalho Chehab 	return 0;
749b285192aSMauro Carvalho Chehab }
750b285192aSMauro Carvalho Chehab 
751b285192aSMauro Carvalho Chehab static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
752b285192aSMauro Carvalho Chehab {
753b285192aSMauro Carvalho Chehab 	return cx23885_set_input(file, priv, i);
754b285192aSMauro Carvalho Chehab }
755b285192aSMauro Carvalho Chehab 
756b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
757b285192aSMauro Carvalho Chehab {
758568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
759b285192aSMauro Carvalho Chehab 
760b285192aSMauro Carvalho Chehab 	call_all(dev, core, log_status);
761b285192aSMauro Carvalho Chehab 	return 0;
762b285192aSMauro Carvalho Chehab }
763b285192aSMauro Carvalho Chehab 
764b285192aSMauro Carvalho Chehab static int cx23885_query_audinput(struct file *file, void *priv,
765b285192aSMauro Carvalho Chehab 	struct v4l2_audio *i)
766b285192aSMauro Carvalho Chehab {
767568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
768b285192aSMauro Carvalho Chehab 	static const char *iname[] = {
769b285192aSMauro Carvalho Chehab 		[0] = "Baseband L/R 1",
770b285192aSMauro Carvalho Chehab 		[1] = "Baseband L/R 2",
771d43be757SHans Verkuil 		[2] = "TV",
772b285192aSMauro Carvalho Chehab 	};
773b285192aSMauro Carvalho Chehab 	unsigned int n;
774b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
775b285192aSMauro Carvalho Chehab 
776b285192aSMauro Carvalho Chehab 	n = i->index;
777d43be757SHans Verkuil 	if (n >= 3)
778b285192aSMauro Carvalho Chehab 		return -EINVAL;
779b285192aSMauro Carvalho Chehab 
780b285192aSMauro Carvalho Chehab 	memset(i, 0, sizeof(*i));
781b285192aSMauro Carvalho Chehab 	i->index = n;
782b285192aSMauro Carvalho Chehab 	strcpy(i->name, iname[n]);
783b285192aSMauro Carvalho Chehab 	i->capability = V4L2_AUDCAP_STEREO;
784b285192aSMauro Carvalho Chehab 	return 0;
785b285192aSMauro Carvalho Chehab 
786b285192aSMauro Carvalho Chehab }
787b285192aSMauro Carvalho Chehab 
788b285192aSMauro Carvalho Chehab static int vidioc_enum_audinput(struct file *file, void *priv,
789b285192aSMauro Carvalho Chehab 				struct v4l2_audio *i)
790b285192aSMauro Carvalho Chehab {
791b285192aSMauro Carvalho Chehab 	return cx23885_query_audinput(file, priv, i);
792b285192aSMauro Carvalho Chehab }
793b285192aSMauro Carvalho Chehab 
794b285192aSMauro Carvalho Chehab static int vidioc_g_audinput(struct file *file, void *priv,
795b285192aSMauro Carvalho Chehab 	struct v4l2_audio *i)
796b285192aSMauro Carvalho Chehab {
797568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
798b285192aSMauro Carvalho Chehab 
799d43be757SHans Verkuil 	if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
800d43be757SHans Verkuil 		(CX23885_VMUX_CABLE == INPUT(dev->input)->type))
801d43be757SHans Verkuil 		i->index = 2;
802d43be757SHans Verkuil 	else
803b285192aSMauro Carvalho Chehab 		i->index = dev->audinput;
804b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(input=%d)\n", __func__, i->index);
805b285192aSMauro Carvalho Chehab 
806b285192aSMauro Carvalho Chehab 	return cx23885_query_audinput(file, priv, i);
807b285192aSMauro Carvalho Chehab }
808b285192aSMauro Carvalho Chehab 
809b285192aSMauro Carvalho Chehab static int vidioc_s_audinput(struct file *file, void *priv,
8100e8025b9SHans Verkuil 	const struct v4l2_audio *i)
811b285192aSMauro Carvalho Chehab {
812568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
813d43be757SHans Verkuil 
814d43be757SHans Verkuil 	if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
815d43be757SHans Verkuil 		(CX23885_VMUX_CABLE == INPUT(dev->input)->type)) {
816d43be757SHans Verkuil 		return i->index != 2 ? -EINVAL : 0;
817d43be757SHans Verkuil 	}
818d43be757SHans Verkuil 	if (i->index > 1)
819b285192aSMauro Carvalho Chehab 		return -EINVAL;
820b285192aSMauro Carvalho Chehab 
821b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(%d)\n", __func__, i->index);
822b285192aSMauro Carvalho Chehab 
823b285192aSMauro Carvalho Chehab 	dev->audinput = i->index;
824b285192aSMauro Carvalho Chehab 
825b285192aSMauro Carvalho Chehab 	/* Skip the audio defaults from the cards struct, caller wants
826b285192aSMauro Carvalho Chehab 	 * directly touch the audio mux hardware. */
827b285192aSMauro Carvalho Chehab 	cx23885_flatiron_mux(dev, dev->audinput + 1);
828b285192aSMauro Carvalho Chehab 	return 0;
829b285192aSMauro Carvalho Chehab }
830b285192aSMauro Carvalho Chehab 
831b285192aSMauro Carvalho Chehab static int vidioc_g_tuner(struct file *file, void *priv,
832b285192aSMauro Carvalho Chehab 				struct v4l2_tuner *t)
833b285192aSMauro Carvalho Chehab {
834568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
835b285192aSMauro Carvalho Chehab 
8360f3bf3dcSHans Verkuil 	if (dev->tuner_type == TUNER_ABSENT)
837b285192aSMauro Carvalho Chehab 		return -EINVAL;
838b285192aSMauro Carvalho Chehab 	if (0 != t->index)
839b285192aSMauro Carvalho Chehab 		return -EINVAL;
840b285192aSMauro Carvalho Chehab 
841b285192aSMauro Carvalho Chehab 	strcpy(t->name, "Television");
842b285192aSMauro Carvalho Chehab 
843b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, g_tuner, t);
844b285192aSMauro Carvalho Chehab 	return 0;
845b285192aSMauro Carvalho Chehab }
846b285192aSMauro Carvalho Chehab 
847b285192aSMauro Carvalho Chehab static int vidioc_s_tuner(struct file *file, void *priv,
8482f73c7c5SHans Verkuil 				const struct v4l2_tuner *t)
849b285192aSMauro Carvalho Chehab {
850568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
851b285192aSMauro Carvalho Chehab 
8520f3bf3dcSHans Verkuil 	if (dev->tuner_type == TUNER_ABSENT)
853b285192aSMauro Carvalho Chehab 		return -EINVAL;
854b285192aSMauro Carvalho Chehab 	if (0 != t->index)
855b285192aSMauro Carvalho Chehab 		return -EINVAL;
856b285192aSMauro Carvalho Chehab 	/* Update the A/V core */
857b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, s_tuner, t);
858b285192aSMauro Carvalho Chehab 
859b285192aSMauro Carvalho Chehab 	return 0;
860b285192aSMauro Carvalho Chehab }
861b285192aSMauro Carvalho Chehab 
862b285192aSMauro Carvalho Chehab static int vidioc_g_frequency(struct file *file, void *priv,
863b285192aSMauro Carvalho Chehab 				struct v4l2_frequency *f)
864b285192aSMauro Carvalho Chehab {
865568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
866b285192aSMauro Carvalho Chehab 
8670f3bf3dcSHans Verkuil 	if (dev->tuner_type == TUNER_ABSENT)
868b285192aSMauro Carvalho Chehab 		return -EINVAL;
869b285192aSMauro Carvalho Chehab 
87086961933SHans Verkuil 	f->type = V4L2_TUNER_ANALOG_TV;
871b285192aSMauro Carvalho Chehab 	f->frequency = dev->freq;
872b285192aSMauro Carvalho Chehab 
873b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, g_frequency, f);
874b285192aSMauro Carvalho Chehab 
875b285192aSMauro Carvalho Chehab 	return 0;
876b285192aSMauro Carvalho Chehab }
877b285192aSMauro Carvalho Chehab 
878b530a447SHans Verkuil static int cx23885_set_freq(struct cx23885_dev *dev, const struct v4l2_frequency *f)
879b285192aSMauro Carvalho Chehab {
880da59a4deSHans Verkuil 	struct v4l2_ctrl *mute;
881da59a4deSHans Verkuil 	int old_mute_val = 1;
882b285192aSMauro Carvalho Chehab 
8830f3bf3dcSHans Verkuil 	if (dev->tuner_type == TUNER_ABSENT)
884b285192aSMauro Carvalho Chehab 		return -EINVAL;
885b285192aSMauro Carvalho Chehab 	if (unlikely(f->tuner != 0))
886b285192aSMauro Carvalho Chehab 		return -EINVAL;
887b285192aSMauro Carvalho Chehab 
888b285192aSMauro Carvalho Chehab 	dev->freq = f->frequency;
889b285192aSMauro Carvalho Chehab 
890b285192aSMauro Carvalho Chehab 	/* I need to mute audio here */
891da59a4deSHans Verkuil 	mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
892da59a4deSHans Verkuil 	if (mute) {
893da59a4deSHans Verkuil 		old_mute_val = v4l2_ctrl_g_ctrl(mute);
894da59a4deSHans Verkuil 		if (!old_mute_val)
895da59a4deSHans Verkuil 			v4l2_ctrl_s_ctrl(mute, 1);
896da59a4deSHans Verkuil 	}
897b285192aSMauro Carvalho Chehab 
898b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, s_frequency, f);
899b285192aSMauro Carvalho Chehab 
900b285192aSMauro Carvalho Chehab 	/* When changing channels it is required to reset TVAUDIO */
901b285192aSMauro Carvalho Chehab 	msleep(100);
902b285192aSMauro Carvalho Chehab 
903b285192aSMauro Carvalho Chehab 	/* I need to unmute audio here */
904da59a4deSHans Verkuil 	if (old_mute_val == 0)
905da59a4deSHans Verkuil 		v4l2_ctrl_s_ctrl(mute, old_mute_val);
906b285192aSMauro Carvalho Chehab 
907b285192aSMauro Carvalho Chehab 	return 0;
908b285192aSMauro Carvalho Chehab }
909b285192aSMauro Carvalho Chehab 
910b285192aSMauro Carvalho Chehab static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
911b530a447SHans Verkuil 	const struct v4l2_frequency *f)
912b285192aSMauro Carvalho Chehab {
913da59a4deSHans Verkuil 	struct v4l2_ctrl *mute;
914da59a4deSHans Verkuil 	int old_mute_val = 1;
915*453afdd9SHans Verkuil 	struct vb2_dvb_frontend *vfe;
916b285192aSMauro Carvalho Chehab 	struct dvb_frontend *fe;
917b285192aSMauro Carvalho Chehab 
918b285192aSMauro Carvalho Chehab 	struct analog_parameters params = {
919b285192aSMauro Carvalho Chehab 		.mode      = V4L2_TUNER_ANALOG_TV,
920b285192aSMauro Carvalho Chehab 		.audmode   = V4L2_TUNER_MODE_STEREO,
921b285192aSMauro Carvalho Chehab 		.std       = dev->tvnorm,
922b285192aSMauro Carvalho Chehab 		.frequency = f->frequency
923b285192aSMauro Carvalho Chehab 	};
924b285192aSMauro Carvalho Chehab 
925b285192aSMauro Carvalho Chehab 	dev->freq = f->frequency;
926b285192aSMauro Carvalho Chehab 
927b285192aSMauro Carvalho Chehab 	/* I need to mute audio here */
928da59a4deSHans Verkuil 	mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
929da59a4deSHans Verkuil 	if (mute) {
930da59a4deSHans Verkuil 		old_mute_val = v4l2_ctrl_g_ctrl(mute);
931da59a4deSHans Verkuil 		if (!old_mute_val)
932da59a4deSHans Verkuil 			v4l2_ctrl_s_ctrl(mute, 1);
933da59a4deSHans Verkuil 	}
934b285192aSMauro Carvalho Chehab 
935b285192aSMauro Carvalho Chehab 	/* If HVR1850 */
936b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
937b285192aSMauro Carvalho Chehab 		params.frequency, f->tuner, params.std);
938b285192aSMauro Carvalho Chehab 
939*453afdd9SHans Verkuil 	vfe = vb2_dvb_get_frontend(&dev->ts2.frontends, 1);
940b285192aSMauro Carvalho Chehab 	if (!vfe) {
941b285192aSMauro Carvalho Chehab 		return -EINVAL;
942b285192aSMauro Carvalho Chehab 	}
943b285192aSMauro Carvalho Chehab 
944b285192aSMauro Carvalho Chehab 	fe = vfe->dvb.frontend;
945b285192aSMauro Carvalho Chehab 
946b285192aSMauro Carvalho Chehab 	if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
947b285192aSMauro Carvalho Chehab 	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
948b285192aSMauro Carvalho Chehab 	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111))
949b285192aSMauro Carvalho Chehab 		fe = &dev->ts1.analog_fe;
950b285192aSMauro Carvalho Chehab 
951b285192aSMauro Carvalho Chehab 	if (fe && fe->ops.tuner_ops.set_analog_params) {
9528774bed9SLaurent Pinchart 		call_all(dev, video, s_std, dev->tvnorm);
953b285192aSMauro Carvalho Chehab 		fe->ops.tuner_ops.set_analog_params(fe, &params);
954b285192aSMauro Carvalho Chehab 	}
955b285192aSMauro Carvalho Chehab 	else
956b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
957b285192aSMauro Carvalho Chehab 
958b285192aSMauro Carvalho Chehab 	/* When changing channels it is required to reset TVAUDIO */
959b285192aSMauro Carvalho Chehab 	msleep(100);
960b285192aSMauro Carvalho Chehab 
961b285192aSMauro Carvalho Chehab 	/* I need to unmute audio here */
962da59a4deSHans Verkuil 	if (old_mute_val == 0)
963da59a4deSHans Verkuil 		v4l2_ctrl_s_ctrl(mute, old_mute_val);
964b285192aSMauro Carvalho Chehab 
965b285192aSMauro Carvalho Chehab 	return 0;
966b285192aSMauro Carvalho Chehab }
967b285192aSMauro Carvalho Chehab 
968b285192aSMauro Carvalho Chehab int cx23885_set_frequency(struct file *file, void *priv,
969b530a447SHans Verkuil 	const struct v4l2_frequency *f)
970b285192aSMauro Carvalho Chehab {
971568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
972b285192aSMauro Carvalho Chehab 	int ret;
973b285192aSMauro Carvalho Chehab 
974b285192aSMauro Carvalho Chehab 	switch (dev->board) {
975b285192aSMauro Carvalho Chehab 	case CX23885_BOARD_HAUPPAUGE_HVR1255:
976b285192aSMauro Carvalho Chehab 	case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
977b285192aSMauro Carvalho Chehab 	case CX23885_BOARD_HAUPPAUGE_HVR1850:
978b285192aSMauro Carvalho Chehab 		ret = cx23885_set_freq_via_ops(dev, f);
979b285192aSMauro Carvalho Chehab 		break;
980b285192aSMauro Carvalho Chehab 	default:
981b285192aSMauro Carvalho Chehab 		ret = cx23885_set_freq(dev, f);
982b285192aSMauro Carvalho Chehab 	}
983b285192aSMauro Carvalho Chehab 
984b285192aSMauro Carvalho Chehab 	return ret;
985b285192aSMauro Carvalho Chehab }
986b285192aSMauro Carvalho Chehab 
987b285192aSMauro Carvalho Chehab static int vidioc_s_frequency(struct file *file, void *priv,
988b530a447SHans Verkuil 	const struct v4l2_frequency *f)
989b285192aSMauro Carvalho Chehab {
990b285192aSMauro Carvalho Chehab 	return cx23885_set_frequency(file, priv, f);
991b285192aSMauro Carvalho Chehab }
992b285192aSMauro Carvalho Chehab 
993b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------- */
994b285192aSMauro Carvalho Chehab 
995b285192aSMauro Carvalho Chehab int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
996b285192aSMauro Carvalho Chehab {
997b285192aSMauro Carvalho Chehab 	u32 mask, count;
998b285192aSMauro Carvalho Chehab 	int handled = 0;
999b285192aSMauro Carvalho Chehab 
1000b285192aSMauro Carvalho Chehab 	mask   = cx_read(VID_A_INT_MSK);
1001b285192aSMauro Carvalho Chehab 	if (0 == (status & mask))
1002b285192aSMauro Carvalho Chehab 		return handled;
1003b285192aSMauro Carvalho Chehab 
1004b285192aSMauro Carvalho Chehab 	cx_write(VID_A_INT_STAT, status);
1005b285192aSMauro Carvalho Chehab 
1006b285192aSMauro Carvalho Chehab 	/* risc op code error, fifo overflow or line sync detection error */
1007b285192aSMauro Carvalho Chehab 	if ((status & VID_BC_MSK_OPC_ERR) ||
1008b285192aSMauro Carvalho Chehab 		(status & VID_BC_MSK_SYNC) ||
1009b285192aSMauro Carvalho Chehab 		(status & VID_BC_MSK_OF)) {
1010b285192aSMauro Carvalho Chehab 
1011b285192aSMauro Carvalho Chehab 		if (status & VID_BC_MSK_OPC_ERR) {
1012b285192aSMauro Carvalho Chehab 			dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1013b285192aSMauro Carvalho Chehab 				VID_BC_MSK_OPC_ERR);
1014b285192aSMauro Carvalho Chehab 			printk(KERN_WARNING "%s: video risc op code error\n",
1015b285192aSMauro Carvalho Chehab 				dev->name);
1016b285192aSMauro Carvalho Chehab 			cx23885_sram_channel_dump(dev,
1017b285192aSMauro Carvalho Chehab 				&dev->sram_channels[SRAM_CH01]);
1018b285192aSMauro Carvalho Chehab 		}
1019b285192aSMauro Carvalho Chehab 
1020b285192aSMauro Carvalho Chehab 		if (status & VID_BC_MSK_SYNC)
1021b285192aSMauro Carvalho Chehab 			dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
1022b285192aSMauro Carvalho Chehab 				"video lines miss-match\n",
1023b285192aSMauro Carvalho Chehab 				VID_BC_MSK_SYNC);
1024b285192aSMauro Carvalho Chehab 
1025b285192aSMauro Carvalho Chehab 		if (status & VID_BC_MSK_OF)
1026b285192aSMauro Carvalho Chehab 			dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1027b285192aSMauro Carvalho Chehab 				VID_BC_MSK_OF);
1028b285192aSMauro Carvalho Chehab 
1029b285192aSMauro Carvalho Chehab 	}
1030b285192aSMauro Carvalho Chehab 
1031b285192aSMauro Carvalho Chehab 	/* Video */
1032b285192aSMauro Carvalho Chehab 	if (status & VID_BC_MSK_RISCI1) {
1033b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
1034b285192aSMauro Carvalho Chehab 		count = cx_read(VID_A_GPCNT);
1035b285192aSMauro Carvalho Chehab 		cx23885_video_wakeup(dev, &dev->vidq, count);
1036b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
1037b285192aSMauro Carvalho Chehab 		handled++;
1038b285192aSMauro Carvalho Chehab 	}
1039b285192aSMauro Carvalho Chehab 
1040b285192aSMauro Carvalho Chehab 	/* Allow the VBI framework to process it's payload */
1041b285192aSMauro Carvalho Chehab 	handled += cx23885_vbi_irq(dev, status);
1042b285192aSMauro Carvalho Chehab 
1043b285192aSMauro Carvalho Chehab 	return handled;
1044b285192aSMauro Carvalho Chehab }
1045b285192aSMauro Carvalho Chehab 
1046b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------- */
1047b285192aSMauro Carvalho Chehab /* exported stuff                                              */
1048b285192aSMauro Carvalho Chehab 
1049b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops = {
1050b285192aSMauro Carvalho Chehab 	.owner	       = THIS_MODULE,
1051*453afdd9SHans Verkuil 	.open           = v4l2_fh_open,
1052*453afdd9SHans Verkuil 	.release        = vb2_fop_release,
1053*453afdd9SHans Verkuil 	.read           = vb2_fop_read,
1054*453afdd9SHans Verkuil 	.poll		= vb2_fop_poll,
105524a8f7b5SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
1056*453afdd9SHans Verkuil 	.mmap           = vb2_fop_mmap,
1057b285192aSMauro Carvalho Chehab };
1058b285192aSMauro Carvalho Chehab 
1059b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops = {
1060b285192aSMauro Carvalho Chehab 	.vidioc_querycap      = vidioc_querycap,
1061b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1062b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1063b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1064b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1065b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vbi_cap     = cx23885_vbi_fmt,
1066b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vbi_cap   = cx23885_vbi_fmt,
1067b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vbi_cap     = cx23885_vbi_fmt,
1068*453afdd9SHans Verkuil 	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
1069*453afdd9SHans Verkuil 	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
1070*453afdd9SHans Verkuil 	.vidioc_querybuf      = vb2_ioctl_querybuf,
1071*453afdd9SHans Verkuil 	.vidioc_qbuf          = vb2_ioctl_qbuf,
1072*453afdd9SHans Verkuil 	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
1073*453afdd9SHans Verkuil 	.vidioc_streamon      = vb2_ioctl_streamon,
1074*453afdd9SHans Verkuil 	.vidioc_streamoff     = vb2_ioctl_streamoff,
1075b285192aSMauro Carvalho Chehab 	.vidioc_s_std         = vidioc_s_std,
1076b285192aSMauro Carvalho Chehab 	.vidioc_g_std         = vidioc_g_std,
1077b285192aSMauro Carvalho Chehab 	.vidioc_enum_input    = vidioc_enum_input,
1078b285192aSMauro Carvalho Chehab 	.vidioc_g_input       = vidioc_g_input,
1079b285192aSMauro Carvalho Chehab 	.vidioc_s_input       = vidioc_s_input,
1080b285192aSMauro Carvalho Chehab 	.vidioc_log_status    = vidioc_log_status,
1081b285192aSMauro Carvalho Chehab 	.vidioc_g_tuner       = vidioc_g_tuner,
1082b285192aSMauro Carvalho Chehab 	.vidioc_s_tuner       = vidioc_s_tuner,
1083b285192aSMauro Carvalho Chehab 	.vidioc_g_frequency   = vidioc_g_frequency,
1084b285192aSMauro Carvalho Chehab 	.vidioc_s_frequency   = vidioc_s_frequency,
1085b285192aSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
108680f8568fSHans Verkuil 	.vidioc_g_chip_info   = cx23885_g_chip_info,
1087b285192aSMauro Carvalho Chehab 	.vidioc_g_register    = cx23885_g_register,
1088b285192aSMauro Carvalho Chehab 	.vidioc_s_register    = cx23885_s_register,
1089b285192aSMauro Carvalho Chehab #endif
1090b285192aSMauro Carvalho Chehab 	.vidioc_enumaudio     = vidioc_enum_audinput,
1091b285192aSMauro Carvalho Chehab 	.vidioc_g_audio       = vidioc_g_audinput,
1092b285192aSMauro Carvalho Chehab 	.vidioc_s_audio       = vidioc_s_audinput,
1093da59a4deSHans Verkuil 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1094da59a4deSHans Verkuil 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1095b285192aSMauro Carvalho Chehab };
1096b285192aSMauro Carvalho Chehab 
1097b285192aSMauro Carvalho Chehab static struct video_device cx23885_vbi_template;
1098b285192aSMauro Carvalho Chehab static struct video_device cx23885_video_template = {
1099b285192aSMauro Carvalho Chehab 	.name                 = "cx23885-video",
1100b285192aSMauro Carvalho Chehab 	.fops                 = &video_fops,
1101b285192aSMauro Carvalho Chehab 	.ioctl_ops 	      = &video_ioctl_ops,
1102b285192aSMauro Carvalho Chehab 	.tvnorms              = CX23885_NORMS,
1103b285192aSMauro Carvalho Chehab };
1104b285192aSMauro Carvalho Chehab 
1105b285192aSMauro Carvalho Chehab void cx23885_video_unregister(struct cx23885_dev *dev)
1106b285192aSMauro Carvalho Chehab {
1107b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1108b285192aSMauro Carvalho Chehab 	cx23885_irq_remove(dev, 0x01);
1109b285192aSMauro Carvalho Chehab 
1110b285192aSMauro Carvalho Chehab 	if (dev->vbi_dev) {
1111b285192aSMauro Carvalho Chehab 		if (video_is_registered(dev->vbi_dev))
1112b285192aSMauro Carvalho Chehab 			video_unregister_device(dev->vbi_dev);
1113b285192aSMauro Carvalho Chehab 		else
1114b285192aSMauro Carvalho Chehab 			video_device_release(dev->vbi_dev);
1115b285192aSMauro Carvalho Chehab 		dev->vbi_dev = NULL;
1116b285192aSMauro Carvalho Chehab 	}
1117b285192aSMauro Carvalho Chehab 	if (dev->video_dev) {
1118b285192aSMauro Carvalho Chehab 		if (video_is_registered(dev->video_dev))
1119b285192aSMauro Carvalho Chehab 			video_unregister_device(dev->video_dev);
1120b285192aSMauro Carvalho Chehab 		else
1121b285192aSMauro Carvalho Chehab 			video_device_release(dev->video_dev);
1122b285192aSMauro Carvalho Chehab 		dev->video_dev = NULL;
1123b285192aSMauro Carvalho Chehab 	}
1124b285192aSMauro Carvalho Chehab 
1125b285192aSMauro Carvalho Chehab 	if (dev->audio_dev)
1126b285192aSMauro Carvalho Chehab 		cx23885_audio_unregister(dev);
1127b285192aSMauro Carvalho Chehab }
1128b285192aSMauro Carvalho Chehab 
1129b285192aSMauro Carvalho Chehab int cx23885_video_register(struct cx23885_dev *dev)
1130b285192aSMauro Carvalho Chehab {
1131*453afdd9SHans Verkuil 	struct vb2_queue *q;
1132b285192aSMauro Carvalho Chehab 	int err;
1133b285192aSMauro Carvalho Chehab 
1134b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1135b285192aSMauro Carvalho Chehab 	spin_lock_init(&dev->slock);
1136b285192aSMauro Carvalho Chehab 
1137b285192aSMauro Carvalho Chehab 	/* Initialize VBI template */
11383618acabSEzequiel Garcia 	cx23885_vbi_template = cx23885_video_template;
1139b285192aSMauro Carvalho Chehab 	strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1140b285192aSMauro Carvalho Chehab 
11419c1f5df8SHans Verkuil 	dev->tvnorm = V4L2_STD_NTSC_M;
114291d2d674SHans Verkuil 	dev->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
114391d2d674SHans Verkuil 	dev->width = norm_maxw(dev->tvnorm);
114491d2d674SHans Verkuil 	dev->height = norm_maxh(dev->tvnorm);
1145b285192aSMauro Carvalho Chehab 
1146b285192aSMauro Carvalho Chehab 	/* init video dma queues */
1147b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->vidq.active);
1148b285192aSMauro Carvalho Chehab 
1149b285192aSMauro Carvalho Chehab 	/* init vbi dma queues */
1150b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->vbiq.active);
1151b285192aSMauro Carvalho Chehab 
1152b285192aSMauro Carvalho Chehab 	cx23885_irq_add_enable(dev, 0x01);
1153b285192aSMauro Carvalho Chehab 
1154b285192aSMauro Carvalho Chehab 	if ((TUNER_ABSENT != dev->tuner_type) &&
1155b285192aSMauro Carvalho Chehab 			((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1156b285192aSMauro Carvalho Chehab 		struct v4l2_subdev *sd = NULL;
1157b285192aSMauro Carvalho Chehab 
1158b285192aSMauro Carvalho Chehab 		if (dev->tuner_addr)
1159b285192aSMauro Carvalho Chehab 			sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1160b285192aSMauro Carvalho Chehab 				&dev->i2c_bus[dev->tuner_bus].i2c_adap,
1161b285192aSMauro Carvalho Chehab 				"tuner", dev->tuner_addr, NULL);
1162b285192aSMauro Carvalho Chehab 		else
1163b285192aSMauro Carvalho Chehab 			sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1164b285192aSMauro Carvalho Chehab 				&dev->i2c_bus[dev->tuner_bus].i2c_adap,
1165b285192aSMauro Carvalho Chehab 				"tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1166b285192aSMauro Carvalho Chehab 		if (sd) {
1167b285192aSMauro Carvalho Chehab 			struct tuner_setup tun_setup;
1168b285192aSMauro Carvalho Chehab 
1169b285192aSMauro Carvalho Chehab 			memset(&tun_setup, 0, sizeof(tun_setup));
1170b285192aSMauro Carvalho Chehab 			tun_setup.mode_mask = T_ANALOG_TV;
1171b285192aSMauro Carvalho Chehab 			tun_setup.type = dev->tuner_type;
1172b285192aSMauro Carvalho Chehab 			tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1173b285192aSMauro Carvalho Chehab 			tun_setup.tuner_callback = cx23885_tuner_callback;
1174b285192aSMauro Carvalho Chehab 
1175b285192aSMauro Carvalho Chehab 			v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1176b285192aSMauro Carvalho Chehab 
1177642ca1a0SAnca Emanuel 			if ((dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) ||
1178642ca1a0SAnca Emanuel 			    (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200)) {
1179b285192aSMauro Carvalho Chehab 				struct xc2028_ctrl ctrl = {
1180b285192aSMauro Carvalho Chehab 					.fname = XC2028_DEFAULT_FIRMWARE,
1181b285192aSMauro Carvalho Chehab 					.max_len = 64
1182b285192aSMauro Carvalho Chehab 				};
1183b285192aSMauro Carvalho Chehab 				struct v4l2_priv_tun_config cfg = {
1184b285192aSMauro Carvalho Chehab 					.tuner = dev->tuner_type,
1185b285192aSMauro Carvalho Chehab 					.priv = &ctrl
1186b285192aSMauro Carvalho Chehab 				};
1187b285192aSMauro Carvalho Chehab 				v4l2_subdev_call(sd, tuner, s_config, &cfg);
1188b285192aSMauro Carvalho Chehab 			}
1189e8d42373SOleh Kravchenko 
1190e8d42373SOleh Kravchenko 			if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
1191e8d42373SOleh Kravchenko 				struct xc2028_ctrl ctrl = {
1192e8d42373SOleh Kravchenko 					.fname = "xc3028L-v36.fw",
1193e8d42373SOleh Kravchenko 					.max_len = 64
1194e8d42373SOleh Kravchenko 				};
1195e8d42373SOleh Kravchenko 				struct v4l2_priv_tun_config cfg = {
1196e8d42373SOleh Kravchenko 					.tuner = dev->tuner_type,
1197e8d42373SOleh Kravchenko 					.priv = &ctrl
1198e8d42373SOleh Kravchenko 				};
1199e8d42373SOleh Kravchenko 				v4l2_subdev_call(sd, tuner, s_config, &cfg);
1200e8d42373SOleh Kravchenko 			}
1201b285192aSMauro Carvalho Chehab 		}
1202b285192aSMauro Carvalho Chehab 	}
1203b285192aSMauro Carvalho Chehab 
120424a8f7b5SHans Verkuil 	/* initial device configuration */
120524a8f7b5SHans Verkuil 	mutex_lock(&dev->lock);
120624a8f7b5SHans Verkuil 	cx23885_set_tvnorm(dev, dev->tvnorm);
120724a8f7b5SHans Verkuil 	cx23885_video_mux(dev, 0);
120824a8f7b5SHans Verkuil 	cx23885_audio_mux(dev, 0);
120924a8f7b5SHans Verkuil 	mutex_unlock(&dev->lock);
121024a8f7b5SHans Verkuil 
1211*453afdd9SHans Verkuil 	q = &dev->vb2_vidq;
1212*453afdd9SHans Verkuil 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1213*453afdd9SHans Verkuil 	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1214*453afdd9SHans Verkuil 	q->gfp_flags = GFP_DMA32;
1215*453afdd9SHans Verkuil 	q->min_buffers_needed = 2;
1216*453afdd9SHans Verkuil 	q->drv_priv = dev;
1217*453afdd9SHans Verkuil 	q->buf_struct_size = sizeof(struct cx23885_buffer);
1218*453afdd9SHans Verkuil 	q->ops = &cx23885_video_qops;
1219*453afdd9SHans Verkuil 	q->mem_ops = &vb2_dma_sg_memops;
1220*453afdd9SHans Verkuil 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1221*453afdd9SHans Verkuil 	q->lock = &dev->lock;
1222*453afdd9SHans Verkuil 
1223*453afdd9SHans Verkuil 	err = vb2_queue_init(q);
1224*453afdd9SHans Verkuil 	if (err < 0)
1225*453afdd9SHans Verkuil 		goto fail_unreg;
1226*453afdd9SHans Verkuil 
1227*453afdd9SHans Verkuil 	q = &dev->vb2_vbiq;
1228*453afdd9SHans Verkuil 	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1229*453afdd9SHans Verkuil 	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1230*453afdd9SHans Verkuil 	q->gfp_flags = GFP_DMA32;
1231*453afdd9SHans Verkuil 	q->min_buffers_needed = 2;
1232*453afdd9SHans Verkuil 	q->drv_priv = dev;
1233*453afdd9SHans Verkuil 	q->buf_struct_size = sizeof(struct cx23885_buffer);
1234*453afdd9SHans Verkuil 	q->ops = &cx23885_vbi_qops;
1235*453afdd9SHans Verkuil 	q->mem_ops = &vb2_dma_sg_memops;
1236*453afdd9SHans Verkuil 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1237*453afdd9SHans Verkuil 	q->lock = &dev->lock;
1238*453afdd9SHans Verkuil 
1239*453afdd9SHans Verkuil 	err = vb2_queue_init(q);
1240*453afdd9SHans Verkuil 	if (err < 0)
1241*453afdd9SHans Verkuil 		goto fail_unreg;
1242*453afdd9SHans Verkuil 
1243b285192aSMauro Carvalho Chehab 	/* register Video device */
1244b285192aSMauro Carvalho Chehab 	dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1245b285192aSMauro Carvalho Chehab 		&cx23885_video_template, "video");
1246*453afdd9SHans Verkuil 	dev->video_dev->queue = &dev->vb2_vidq;
1247b285192aSMauro Carvalho Chehab 	err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1248b285192aSMauro Carvalho Chehab 				    video_nr[dev->nr]);
1249b285192aSMauro Carvalho Chehab 	if (err < 0) {
1250b285192aSMauro Carvalho Chehab 		printk(KERN_INFO "%s: can't register video device\n",
1251b285192aSMauro Carvalho Chehab 			dev->name);
1252b285192aSMauro Carvalho Chehab 		goto fail_unreg;
1253b285192aSMauro Carvalho Chehab 	}
1254b285192aSMauro Carvalho Chehab 	printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1255b285192aSMauro Carvalho Chehab 	       dev->name, video_device_node_name(dev->video_dev));
1256b285192aSMauro Carvalho Chehab 
1257b285192aSMauro Carvalho Chehab 	/* register VBI device */
1258b285192aSMauro Carvalho Chehab 	dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1259b285192aSMauro Carvalho Chehab 		&cx23885_vbi_template, "vbi");
1260*453afdd9SHans Verkuil 	dev->vbi_dev->queue = &dev->vb2_vbiq;
1261b285192aSMauro Carvalho Chehab 	err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1262b285192aSMauro Carvalho Chehab 				    vbi_nr[dev->nr]);
1263b285192aSMauro Carvalho Chehab 	if (err < 0) {
1264b285192aSMauro Carvalho Chehab 		printk(KERN_INFO "%s: can't register vbi device\n",
1265b285192aSMauro Carvalho Chehab 			dev->name);
1266b285192aSMauro Carvalho Chehab 		goto fail_unreg;
1267b285192aSMauro Carvalho Chehab 	}
1268b285192aSMauro Carvalho Chehab 	printk(KERN_INFO "%s: registered device %s\n",
1269b285192aSMauro Carvalho Chehab 	       dev->name, video_device_node_name(dev->vbi_dev));
1270b285192aSMauro Carvalho Chehab 
1271b285192aSMauro Carvalho Chehab 	/* Register ALSA audio device */
1272b285192aSMauro Carvalho Chehab 	dev->audio_dev = cx23885_audio_register(dev);
1273b285192aSMauro Carvalho Chehab 
1274b285192aSMauro Carvalho Chehab 	return 0;
1275b285192aSMauro Carvalho Chehab 
1276b285192aSMauro Carvalho Chehab fail_unreg:
1277b285192aSMauro Carvalho Chehab 	cx23885_video_unregister(dev);
1278b285192aSMauro Carvalho Chehab 	return err;
1279b285192aSMauro Carvalho Chehab }
1280