xref: /linux/drivers/media/pci/cx23885/cx23885-video.c (revision df9ecb0cad14b952a2865f8b3af86b2bbadfab45)
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 
38d647f0b7SMauro Carvalho Chehab #include <media/drv-intf/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))
103453afdd9SHans Verkuil 		return;
104b285192aSMauro Carvalho Chehab 	buf = list_entry(q->active.next,
105453afdd9SHans Verkuil 			struct cx23885_buffer, queue);
106b285192aSMauro Carvalho Chehab 
1072d700715SJunghak Sung 	buf->vb.sequence = q->count++;
1082d700715SJunghak Sung 	v4l2_get_timestamp(&buf->vb.timestamp);
1092d700715SJunghak Sung 	dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf,
1102d700715SJunghak Sung 			buf->vb.vb2_buf.index, count, q->count);
111453afdd9SHans Verkuil 	list_del(&buf->queue);
1122d700715SJunghak Sung 	vb2_buffer_done(&buf->vb.vb2_buf, 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 {
117e80bf856SHans Verkuil 	struct v4l2_subdev_format format = {
118e80bf856SHans Verkuil 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
119e80bf856SHans Verkuil 		.format.code = MEDIA_BUS_FMT_FIXED,
120e80bf856SHans Verkuil 	};
121e80bf856SHans Verkuil 
122b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
123b285192aSMauro Carvalho Chehab 		__func__,
124b285192aSMauro Carvalho Chehab 		(unsigned int)norm,
125b285192aSMauro Carvalho Chehab 		v4l2_norm_to_name(norm));
126b285192aSMauro Carvalho Chehab 
127e80bf856SHans Verkuil 	if (dev->tvnorm == norm)
128e80bf856SHans Verkuil 		return 0;
129e80bf856SHans Verkuil 
130f1b6a735SHans Verkuil 	if (dev->tvnorm != norm) {
131f1b6a735SHans Verkuil 		if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
132f1b6a735SHans Verkuil 		    vb2_is_busy(&dev->vb2_mpegq))
133f1b6a735SHans Verkuil 			return -EBUSY;
134f1b6a735SHans Verkuil 	}
135f1b6a735SHans Verkuil 
136b285192aSMauro Carvalho Chehab 	dev->tvnorm = norm;
137e80bf856SHans Verkuil 	dev->width = 720;
138e80bf856SHans Verkuil 	dev->height = norm_maxh(norm);
139e80bf856SHans Verkuil 	dev->field = V4L2_FIELD_INTERLACED;
140b285192aSMauro Carvalho Chehab 
1418774bed9SLaurent Pinchart 	call_all(dev, video, s_std, norm);
142b285192aSMauro Carvalho Chehab 
143e80bf856SHans Verkuil 	format.format.width = dev->width;
144e80bf856SHans Verkuil 	format.format.height = dev->height;
145e80bf856SHans Verkuil 	format.format.field = dev->field;
146e80bf856SHans Verkuil 	call_all(dev, pad, set_fmt, NULL, &format);
147e80bf856SHans Verkuil 
148b285192aSMauro Carvalho Chehab 	return 0;
149b285192aSMauro Carvalho Chehab }
150b285192aSMauro Carvalho Chehab 
151b285192aSMauro Carvalho Chehab static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
152b285192aSMauro Carvalho Chehab 				    struct pci_dev *pci,
153b285192aSMauro Carvalho Chehab 				    struct video_device *template,
154b285192aSMauro Carvalho Chehab 				    char *type)
155b285192aSMauro Carvalho Chehab {
156b285192aSMauro Carvalho Chehab 	struct video_device *vfd;
157b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
158b285192aSMauro Carvalho Chehab 
159b285192aSMauro Carvalho Chehab 	vfd = video_device_alloc();
160b285192aSMauro Carvalho Chehab 	if (NULL == vfd)
161b285192aSMauro Carvalho Chehab 		return NULL;
162b285192aSMauro Carvalho Chehab 	*vfd = *template;
163b285192aSMauro Carvalho Chehab 	vfd->v4l2_dev = &dev->v4l2_dev;
164b285192aSMauro Carvalho Chehab 	vfd->release = video_device_release;
16524a8f7b5SHans Verkuil 	vfd->lock = &dev->lock;
166b285192aSMauro Carvalho Chehab 	snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
167b285192aSMauro Carvalho Chehab 		 cx23885_boards[dev->board].name, type);
168b285192aSMauro Carvalho Chehab 	video_set_drvdata(vfd, dev);
169b285192aSMauro Carvalho Chehab 	return vfd;
170b285192aSMauro Carvalho Chehab }
171b285192aSMauro Carvalho Chehab 
1725bc08e19SLuis Alves int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
173b285192aSMauro Carvalho Chehab {
174b285192aSMauro Carvalho Chehab 	/* 8 bit registers, 8 bit values */
175b285192aSMauro Carvalho Chehab 	u8 buf[] = { reg, data };
176b285192aSMauro Carvalho Chehab 
177b285192aSMauro Carvalho Chehab 	struct i2c_msg msg = { .addr = 0x98 >> 1,
178b285192aSMauro Carvalho Chehab 		.flags = 0, .buf = buf, .len = 2 };
179b285192aSMauro Carvalho Chehab 
180b285192aSMauro Carvalho Chehab 	return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
181b285192aSMauro Carvalho Chehab }
182b285192aSMauro Carvalho Chehab 
1835bc08e19SLuis Alves u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
184b285192aSMauro Carvalho Chehab {
185b285192aSMauro Carvalho Chehab 	/* 8 bit registers, 8 bit values */
186b285192aSMauro Carvalho Chehab 	int ret;
187b285192aSMauro Carvalho Chehab 	u8 b0[] = { reg };
188b285192aSMauro Carvalho Chehab 	u8 b1[] = { 0 };
189b285192aSMauro Carvalho Chehab 
190b285192aSMauro Carvalho Chehab 	struct i2c_msg msg[] = {
191b285192aSMauro Carvalho Chehab 		{ .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
192b285192aSMauro Carvalho Chehab 		{ .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
193b285192aSMauro Carvalho Chehab 	};
194b285192aSMauro Carvalho Chehab 
195b285192aSMauro Carvalho Chehab 	ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
196b285192aSMauro Carvalho Chehab 	if (ret != 2)
197b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() error\n", __func__);
198b285192aSMauro Carvalho Chehab 
199b285192aSMauro Carvalho Chehab 	return b1[0];
200b285192aSMauro Carvalho Chehab }
201b285192aSMauro Carvalho Chehab 
202b285192aSMauro Carvalho Chehab static void cx23885_flatiron_dump(struct cx23885_dev *dev)
203b285192aSMauro Carvalho Chehab {
204b285192aSMauro Carvalho Chehab 	int i;
205b285192aSMauro Carvalho Chehab 	dprintk(1, "Flatiron dump\n");
206b285192aSMauro Carvalho Chehab 	for (i = 0; i < 0x24; i++) {
207b285192aSMauro Carvalho Chehab 		dprintk(1, "FI[%02x] = %02x\n", i,
208b285192aSMauro Carvalho Chehab 			cx23885_flatiron_read(dev, i));
209b285192aSMauro Carvalho Chehab 	}
210b285192aSMauro Carvalho Chehab }
211b285192aSMauro Carvalho Chehab 
212b285192aSMauro Carvalho Chehab static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
213b285192aSMauro Carvalho Chehab {
214b285192aSMauro Carvalho Chehab 	u8 val;
215b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(input = %d)\n", __func__, input);
216b285192aSMauro Carvalho Chehab 
217b285192aSMauro Carvalho Chehab 	if (input == 1)
218b285192aSMauro Carvalho Chehab 		val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
219b285192aSMauro Carvalho Chehab 	else if (input == 2)
220b285192aSMauro Carvalho Chehab 		val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
221b285192aSMauro Carvalho Chehab 	else
222b285192aSMauro Carvalho Chehab 		return -EINVAL;
223b285192aSMauro Carvalho Chehab 
224b285192aSMauro Carvalho Chehab 	val |= 0x20; /* Enable clock to delta-sigma and dec filter */
225b285192aSMauro Carvalho Chehab 
226b285192aSMauro Carvalho Chehab 	cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
227b285192aSMauro Carvalho Chehab 
228b285192aSMauro Carvalho Chehab 	/* Wake up */
229b285192aSMauro Carvalho Chehab 	cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
230b285192aSMauro Carvalho Chehab 
231b285192aSMauro Carvalho Chehab 	if (video_debug)
232b285192aSMauro Carvalho Chehab 		cx23885_flatiron_dump(dev);
233b285192aSMauro Carvalho Chehab 
234b285192aSMauro Carvalho Chehab 	return 0;
235b285192aSMauro Carvalho Chehab }
236b285192aSMauro Carvalho Chehab 
237b285192aSMauro Carvalho Chehab static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
238b285192aSMauro Carvalho Chehab {
239b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
240b285192aSMauro Carvalho Chehab 		__func__,
241b285192aSMauro Carvalho Chehab 		input, INPUT(input)->vmux,
242b285192aSMauro Carvalho Chehab 		INPUT(input)->gpio0, INPUT(input)->gpio1,
243b285192aSMauro Carvalho Chehab 		INPUT(input)->gpio2, INPUT(input)->gpio3);
244b285192aSMauro Carvalho Chehab 	dev->input = input;
245b285192aSMauro Carvalho Chehab 
246b285192aSMauro Carvalho Chehab 	if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
247b285192aSMauro Carvalho Chehab 		dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
248b285192aSMauro Carvalho Chehab 		dev->board == CX23885_BOARD_MYGICA_X8507) {
249b285192aSMauro Carvalho Chehab 		/* Select Analog TV */
250b285192aSMauro Carvalho Chehab 		if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
251b285192aSMauro Carvalho Chehab 			cx23885_gpio_clear(dev, GPIO_0);
252b285192aSMauro Carvalho Chehab 	}
253b285192aSMauro Carvalho Chehab 
254b285192aSMauro Carvalho Chehab 	/* Tell the internal A/V decoder */
255b285192aSMauro Carvalho Chehab 	v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
256b285192aSMauro Carvalho Chehab 			INPUT(input)->vmux, 0, 0);
257b285192aSMauro Carvalho Chehab 
258b285192aSMauro Carvalho Chehab 	if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
259b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_MPX885) ||
260b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
261cce11b09SHans Verkuil 		(dev->board == CX23885_BOARD_HAUPPAUGE_IMPACTVCBE) ||
262b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
263b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
264082c0576SAlfredo Jesús Delaiti 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
265e8d42373SOleh Kravchenko 		(dev->board == CX23885_BOARD_MYGICA_X8507) ||
2666c43a217SHans Verkuil 		(dev->board == CX23885_BOARD_AVERMEDIA_HC81R) ||
2676c43a217SHans Verkuil 		(dev->board == CX23885_BOARD_VIEWCAST_260E) ||
2686c43a217SHans Verkuil 		(dev->board == CX23885_BOARD_VIEWCAST_460E)) {
269b285192aSMauro Carvalho Chehab 		/* Configure audio routing */
270b285192aSMauro Carvalho Chehab 		v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
271b285192aSMauro Carvalho Chehab 			INPUT(input)->amux, 0, 0);
272b285192aSMauro Carvalho Chehab 
273b285192aSMauro Carvalho Chehab 		if (INPUT(input)->amux == CX25840_AUDIO7)
274b285192aSMauro Carvalho Chehab 			cx23885_flatiron_mux(dev, 1);
275b285192aSMauro Carvalho Chehab 		else if (INPUT(input)->amux == CX25840_AUDIO6)
276b285192aSMauro Carvalho Chehab 			cx23885_flatiron_mux(dev, 2);
277b285192aSMauro Carvalho Chehab 	}
278b285192aSMauro Carvalho Chehab 
279b285192aSMauro Carvalho Chehab 	return 0;
280b285192aSMauro Carvalho Chehab }
281b285192aSMauro Carvalho Chehab 
282b285192aSMauro Carvalho Chehab static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
283b285192aSMauro Carvalho Chehab {
284b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(input=%d)\n", __func__, input);
285b285192aSMauro Carvalho Chehab 
286b285192aSMauro Carvalho Chehab 	/* The baseband video core of the cx23885 has two audio inputs.
287b285192aSMauro Carvalho Chehab 	 * LR1 and LR2. In almost every single case so far only HVR1xxx
288b285192aSMauro Carvalho Chehab 	 * cards we've only ever supported LR1. Time to support LR2,
289b285192aSMauro Carvalho Chehab 	 * which is available via the optional white breakout header on
290b285192aSMauro Carvalho Chehab 	 * the board.
291b285192aSMauro Carvalho Chehab 	 * We'll use a could of existing enums in the card struct to allow
292b285192aSMauro Carvalho Chehab 	 * devs to specify which baseband input they need, or just default
293b285192aSMauro Carvalho Chehab 	 * to what we've always used.
294b285192aSMauro Carvalho Chehab 	 */
295b285192aSMauro Carvalho Chehab 	if (INPUT(input)->amux == CX25840_AUDIO7)
296b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 1);
297b285192aSMauro Carvalho Chehab 	else if (INPUT(input)->amux == CX25840_AUDIO6)
298b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 2);
299b285192aSMauro Carvalho Chehab 	else {
300b285192aSMauro Carvalho Chehab 		/* Not specifically defined, assume the default. */
301b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 1);
302b285192aSMauro Carvalho Chehab 	}
303b285192aSMauro Carvalho Chehab 
304b285192aSMauro Carvalho Chehab 	return 0;
305b285192aSMauro Carvalho Chehab }
306b285192aSMauro Carvalho Chehab 
307b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
308b285192aSMauro Carvalho Chehab static int cx23885_start_video_dma(struct cx23885_dev *dev,
309b285192aSMauro Carvalho Chehab 			   struct cx23885_dmaqueue *q,
310b285192aSMauro Carvalho Chehab 			   struct cx23885_buffer *buf)
311b285192aSMauro Carvalho Chehab {
312b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
313b285192aSMauro Carvalho Chehab 
314b285192aSMauro Carvalho Chehab 	/* Stop the dma/fifo before we tamper with it's risc programs */
315b285192aSMauro Carvalho Chehab 	cx_clear(VID_A_DMA_CTL, 0x11);
316b285192aSMauro Carvalho Chehab 
317b285192aSMauro Carvalho Chehab 	/* setup fifo + format */
318b285192aSMauro Carvalho Chehab 	cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
319b285192aSMauro Carvalho Chehab 				buf->bpl, buf->risc.dma);
320b285192aSMauro Carvalho Chehab 
321b285192aSMauro Carvalho Chehab 	/* reset counter */
322b285192aSMauro Carvalho Chehab 	cx_write(VID_A_GPCNT_CTL, 3);
323453afdd9SHans Verkuil 	q->count = 0;
324b285192aSMauro Carvalho Chehab 
325b285192aSMauro Carvalho Chehab 	/* enable irq */
326b285192aSMauro Carvalho Chehab 	cx23885_irq_add_enable(dev, 0x01);
327b285192aSMauro Carvalho Chehab 	cx_set(VID_A_INT_MSK, 0x000011);
328b285192aSMauro Carvalho Chehab 
329b285192aSMauro Carvalho Chehab 	/* start dma */
330b285192aSMauro Carvalho Chehab 	cx_set(DEV_CNTRL2, (1<<5));
331b285192aSMauro Carvalho Chehab 	cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
332b285192aSMauro Carvalho Chehab 
333b285192aSMauro Carvalho Chehab 	return 0;
334b285192aSMauro Carvalho Chehab }
335b285192aSMauro Carvalho Chehab 
336*df9ecb0cSHans Verkuil static int queue_setup(struct vb2_queue *q,
337453afdd9SHans Verkuil 			   unsigned int *num_buffers, unsigned int *num_planes,
338453afdd9SHans Verkuil 			   unsigned int sizes[], void *alloc_ctxs[])
339b285192aSMauro Carvalho Chehab {
340453afdd9SHans Verkuil 	struct cx23885_dev *dev = q->drv_priv;
341b285192aSMauro Carvalho Chehab 
342453afdd9SHans Verkuil 	*num_planes = 1;
343453afdd9SHans Verkuil 	sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
3440c3a14c1SHans Verkuil 	alloc_ctxs[0] = dev->alloc_ctx;
345b285192aSMauro Carvalho Chehab 	return 0;
346b285192aSMauro Carvalho Chehab }
347b285192aSMauro Carvalho Chehab 
348453afdd9SHans Verkuil static int buffer_prepare(struct vb2_buffer *vb)
349b285192aSMauro Carvalho Chehab {
3502d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
351453afdd9SHans Verkuil 	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
352b285192aSMauro Carvalho Chehab 	struct cx23885_buffer *buf =
3532d700715SJunghak Sung 		container_of(vbuf, struct cx23885_buffer, vb);
354b285192aSMauro Carvalho Chehab 	u32 line0_offset, line1_offset;
355453afdd9SHans Verkuil 	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
356b285192aSMauro Carvalho Chehab 	int field_tff;
357b285192aSMauro Carvalho Chehab 
358453afdd9SHans Verkuil 	buf->bpl = (dev->width * dev->fmt->depth) >> 3;
359453afdd9SHans Verkuil 
360453afdd9SHans Verkuil 	if (vb2_plane_size(vb, 0) < dev->height * buf->bpl)
361b285192aSMauro Carvalho Chehab 		return -EINVAL;
362453afdd9SHans Verkuil 	vb2_set_plane_payload(vb, 0, dev->height * buf->bpl);
36391d2d674SHans Verkuil 
364453afdd9SHans Verkuil 	switch (dev->field) {
365b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_TOP:
366b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
367453afdd9SHans Verkuil 				sgt->sgl, 0, UNSET,
368453afdd9SHans Verkuil 				buf->bpl, 0, dev->height);
369b285192aSMauro Carvalho Chehab 		break;
370b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_BOTTOM:
371b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
372453afdd9SHans Verkuil 				sgt->sgl, UNSET, 0,
373453afdd9SHans Verkuil 				buf->bpl, 0, dev->height);
374b285192aSMauro Carvalho Chehab 		break;
375b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_INTERLACED:
37696f233e9SHans Verkuil 		if (dev->tvnorm & V4L2_STD_525_60)
377b285192aSMauro Carvalho Chehab 			/* NTSC or  */
378b285192aSMauro Carvalho Chehab 			field_tff = 1;
379b285192aSMauro Carvalho Chehab 		else
380b285192aSMauro Carvalho Chehab 			field_tff = 0;
381b285192aSMauro Carvalho Chehab 
382b285192aSMauro Carvalho Chehab 		if (cx23885_boards[dev->board].force_bff)
383b285192aSMauro Carvalho Chehab 			/* PAL / SECAM OR 888 in NTSC MODE */
384b285192aSMauro Carvalho Chehab 			field_tff = 0;
385b285192aSMauro Carvalho Chehab 
386b285192aSMauro Carvalho Chehab 		if (field_tff) {
387b285192aSMauro Carvalho Chehab 			/* cx25840 transmits NTSC bottom field first */
388b285192aSMauro Carvalho Chehab 			dprintk(1, "%s() Creating TFF/NTSC risc\n",
389b285192aSMauro Carvalho Chehab 					__func__);
390b285192aSMauro Carvalho Chehab 			line0_offset = buf->bpl;
391b285192aSMauro Carvalho Chehab 			line1_offset = 0;
392b285192aSMauro Carvalho Chehab 		} else {
393b285192aSMauro Carvalho Chehab 			/* All other formats are top field first */
394b285192aSMauro Carvalho Chehab 			dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
395b285192aSMauro Carvalho Chehab 					__func__);
396b285192aSMauro Carvalho Chehab 			line0_offset = 0;
397b285192aSMauro Carvalho Chehab 			line1_offset = buf->bpl;
398b285192aSMauro Carvalho Chehab 		}
399b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
400453afdd9SHans Verkuil 				sgt->sgl, line0_offset,
401b285192aSMauro Carvalho Chehab 				line1_offset,
402b285192aSMauro Carvalho Chehab 				buf->bpl, buf->bpl,
403453afdd9SHans Verkuil 				dev->height >> 1);
404b285192aSMauro Carvalho Chehab 		break;
405b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_TB:
406b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
407453afdd9SHans Verkuil 				sgt->sgl,
408453afdd9SHans Verkuil 				0, buf->bpl * (dev->height >> 1),
409b285192aSMauro Carvalho Chehab 				buf->bpl, 0,
410453afdd9SHans Verkuil 				dev->height >> 1);
411b285192aSMauro Carvalho Chehab 		break;
412b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_BT:
413b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
414453afdd9SHans Verkuil 				sgt->sgl,
415453afdd9SHans Verkuil 				buf->bpl * (dev->height >> 1), 0,
416b285192aSMauro Carvalho Chehab 				buf->bpl, 0,
417453afdd9SHans Verkuil 				dev->height >> 1);
418b285192aSMauro Carvalho Chehab 		break;
419b285192aSMauro Carvalho Chehab 	default:
420b285192aSMauro Carvalho Chehab 		BUG();
421b285192aSMauro Carvalho Chehab 	}
422453afdd9SHans Verkuil 	dprintk(2, "[%p/%d] buffer_init - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
4232d700715SJunghak Sung 		buf, buf->vb.vb2_buf.index,
42491d2d674SHans Verkuil 		dev->width, dev->height, dev->fmt->depth, dev->fmt->name,
425b285192aSMauro Carvalho Chehab 		(unsigned long)buf->risc.dma);
426b285192aSMauro Carvalho Chehab 	return 0;
427b285192aSMauro Carvalho Chehab }
428b285192aSMauro Carvalho Chehab 
429453afdd9SHans Verkuil static void buffer_finish(struct vb2_buffer *vb)
430b285192aSMauro Carvalho Chehab {
4312d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
4322d700715SJunghak Sung 	struct cx23885_buffer *buf = container_of(vbuf,
433453afdd9SHans Verkuil 		struct cx23885_buffer, vb);
434453afdd9SHans Verkuil 
435453afdd9SHans Verkuil 	cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
436453afdd9SHans Verkuil }
437453afdd9SHans Verkuil 
438453afdd9SHans Verkuil /*
439453afdd9SHans Verkuil  * The risc program for each buffer works as follows: it starts with a simple
440453afdd9SHans Verkuil  * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
441453afdd9SHans Verkuil  * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
442453afdd9SHans Verkuil  * the initial JUMP).
443453afdd9SHans Verkuil  *
444453afdd9SHans Verkuil  * This is the risc program of the first buffer to be queued if the active list
445453afdd9SHans Verkuil  * is empty and it just keeps DMAing this buffer without generating any
446453afdd9SHans Verkuil  * interrupts.
447453afdd9SHans Verkuil  *
448453afdd9SHans Verkuil  * If a new buffer is added then the initial JUMP in the code for that buffer
449453afdd9SHans Verkuil  * will generate an interrupt which signals that the previous buffer has been
450453afdd9SHans Verkuil  * DMAed successfully and that it can be returned to userspace.
451453afdd9SHans Verkuil  *
452453afdd9SHans Verkuil  * It also sets the final jump of the previous buffer to the start of the new
453453afdd9SHans Verkuil  * buffer, thus chaining the new buffer into the DMA chain. This is a single
454453afdd9SHans Verkuil  * atomic u32 write, so there is no race condition.
455453afdd9SHans Verkuil  *
456453afdd9SHans Verkuil  * The end-result of all this that you only get an interrupt when a buffer
457453afdd9SHans Verkuil  * is ready, so the control flow is very easy.
458453afdd9SHans Verkuil  */
459453afdd9SHans Verkuil static void buffer_queue(struct vb2_buffer *vb)
460453afdd9SHans Verkuil {
4612d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
462453afdd9SHans Verkuil 	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
4632d700715SJunghak Sung 	struct cx23885_buffer   *buf = container_of(vbuf,
464b285192aSMauro Carvalho Chehab 		struct cx23885_buffer, vb);
465b285192aSMauro Carvalho Chehab 	struct cx23885_buffer   *prev;
466b285192aSMauro Carvalho Chehab 	struct cx23885_dmaqueue *q    = &dev->vidq;
467453afdd9SHans Verkuil 	unsigned long flags;
468b285192aSMauro Carvalho Chehab 
469453afdd9SHans Verkuil 	/* add jump to start */
470453afdd9SHans Verkuil 	buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
471453afdd9SHans Verkuil 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
472453afdd9SHans Verkuil 	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
473b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
474b285192aSMauro Carvalho Chehab 
475453afdd9SHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
476453afdd9SHans Verkuil 	if (list_empty(&q->active)) {
477453afdd9SHans Verkuil 		list_add_tail(&buf->queue, &q->active);
478b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - first active\n",
4792d700715SJunghak Sung 			buf, buf->vb.vb2_buf.index);
480b285192aSMauro Carvalho Chehab 	} else {
481453afdd9SHans Verkuil 		buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
482b285192aSMauro Carvalho Chehab 		prev = list_entry(q->active.prev, struct cx23885_buffer,
483453afdd9SHans Verkuil 			queue);
484453afdd9SHans Verkuil 		list_add_tail(&buf->queue, &q->active);
485b285192aSMauro Carvalho Chehab 		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
486b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - append to active\n",
4872d700715SJunghak Sung 				buf, buf->vb.vb2_buf.index);
488b285192aSMauro Carvalho Chehab 	}
489453afdd9SHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
490b285192aSMauro Carvalho Chehab }
491b285192aSMauro Carvalho Chehab 
492453afdd9SHans Verkuil static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
493b285192aSMauro Carvalho Chehab {
494453afdd9SHans Verkuil 	struct cx23885_dev *dev = q->drv_priv;
495453afdd9SHans Verkuil 	struct cx23885_dmaqueue *dmaq = &dev->vidq;
496453afdd9SHans Verkuil 	struct cx23885_buffer *buf = list_entry(dmaq->active.next,
497453afdd9SHans Verkuil 			struct cx23885_buffer, queue);
498b285192aSMauro Carvalho Chehab 
499453afdd9SHans Verkuil 	cx23885_start_video_dma(dev, dmaq, buf);
500453afdd9SHans Verkuil 	return 0;
501b285192aSMauro Carvalho Chehab }
502b285192aSMauro Carvalho Chehab 
503453afdd9SHans Verkuil static void cx23885_stop_streaming(struct vb2_queue *q)
504453afdd9SHans Verkuil {
505453afdd9SHans Verkuil 	struct cx23885_dev *dev = q->drv_priv;
506453afdd9SHans Verkuil 	struct cx23885_dmaqueue *dmaq = &dev->vidq;
507453afdd9SHans Verkuil 	unsigned long flags;
508453afdd9SHans Verkuil 
509453afdd9SHans Verkuil 	cx_clear(VID_A_DMA_CTL, 0x11);
510453afdd9SHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
511453afdd9SHans Verkuil 	while (!list_empty(&dmaq->active)) {
512453afdd9SHans Verkuil 		struct cx23885_buffer *buf = list_entry(dmaq->active.next,
513453afdd9SHans Verkuil 			struct cx23885_buffer, queue);
514453afdd9SHans Verkuil 
515453afdd9SHans Verkuil 		list_del(&buf->queue);
5162d700715SJunghak Sung 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
517453afdd9SHans Verkuil 	}
518453afdd9SHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
519453afdd9SHans Verkuil }
520453afdd9SHans Verkuil 
521453afdd9SHans Verkuil static struct vb2_ops cx23885_video_qops = {
522453afdd9SHans Verkuil 	.queue_setup    = queue_setup,
523b285192aSMauro Carvalho Chehab 	.buf_prepare  = buffer_prepare,
524453afdd9SHans Verkuil 	.buf_finish = buffer_finish,
525b285192aSMauro Carvalho Chehab 	.buf_queue    = buffer_queue,
526453afdd9SHans Verkuil 	.wait_prepare = vb2_ops_wait_prepare,
527453afdd9SHans Verkuil 	.wait_finish = vb2_ops_wait_finish,
528453afdd9SHans Verkuil 	.start_streaming = cx23885_start_streaming,
529453afdd9SHans Verkuil 	.stop_streaming = cx23885_stop_streaming,
530b285192aSMauro Carvalho Chehab };
531b285192aSMauro Carvalho Chehab 
532b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
533b285192aSMauro Carvalho Chehab /* VIDEO IOCTLS                                                       */
534b285192aSMauro Carvalho Chehab 
535b285192aSMauro Carvalho Chehab static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
536b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
537b285192aSMauro Carvalho Chehab {
538568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
539b285192aSMauro Carvalho Chehab 
54091d2d674SHans Verkuil 	f->fmt.pix.width        = dev->width;
54191d2d674SHans Verkuil 	f->fmt.pix.height       = dev->height;
542453afdd9SHans Verkuil 	f->fmt.pix.field        = dev->field;
54391d2d674SHans Verkuil 	f->fmt.pix.pixelformat  = dev->fmt->fourcc;
544b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline =
54591d2d674SHans Verkuil 		(f->fmt.pix.width * dev->fmt->depth) >> 3;
546b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage =
547b285192aSMauro Carvalho Chehab 		f->fmt.pix.height * f->fmt.pix.bytesperline;
5481af2ddd8SHans Verkuil 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
549b285192aSMauro Carvalho Chehab 
550b285192aSMauro Carvalho Chehab 	return 0;
551b285192aSMauro Carvalho Chehab }
552b285192aSMauro Carvalho Chehab 
553b285192aSMauro Carvalho Chehab static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
554b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
555b285192aSMauro Carvalho Chehab {
556568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
557b285192aSMauro Carvalho Chehab 	struct cx23885_fmt *fmt;
558b285192aSMauro Carvalho Chehab 	enum v4l2_field   field;
559b285192aSMauro Carvalho Chehab 	unsigned int      maxw, maxh;
560b285192aSMauro Carvalho Chehab 
561b285192aSMauro Carvalho Chehab 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
562b285192aSMauro Carvalho Chehab 	if (NULL == fmt)
563b285192aSMauro Carvalho Chehab 		return -EINVAL;
564b285192aSMauro Carvalho Chehab 
565b285192aSMauro Carvalho Chehab 	field = f->fmt.pix.field;
566e80bf856SHans Verkuil 	maxw  = 720;
567b285192aSMauro Carvalho Chehab 	maxh  = norm_maxh(dev->tvnorm);
568b285192aSMauro Carvalho Chehab 
569b285192aSMauro Carvalho Chehab 	if (V4L2_FIELD_ANY == field) {
570b285192aSMauro Carvalho Chehab 		field = (f->fmt.pix.height > maxh/2)
571b285192aSMauro Carvalho Chehab 			? V4L2_FIELD_INTERLACED
572b285192aSMauro Carvalho Chehab 			: V4L2_FIELD_BOTTOM;
573b285192aSMauro Carvalho Chehab 	}
574b285192aSMauro Carvalho Chehab 
575b285192aSMauro Carvalho Chehab 	switch (field) {
576b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_TOP:
577b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_BOTTOM:
578b285192aSMauro Carvalho Chehab 		maxh = maxh / 2;
579b285192aSMauro Carvalho Chehab 		break;
580b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_INTERLACED:
58196f233e9SHans Verkuil 	case V4L2_FIELD_SEQ_TB:
58296f233e9SHans Verkuil 	case V4L2_FIELD_SEQ_BT:
583b285192aSMauro Carvalho Chehab 		break;
584b285192aSMauro Carvalho Chehab 	default:
585200e0841SHans Verkuil 		field = V4L2_FIELD_INTERLACED;
586200e0841SHans Verkuil 		break;
587b285192aSMauro Carvalho Chehab 	}
588b285192aSMauro Carvalho Chehab 
589b285192aSMauro Carvalho Chehab 	f->fmt.pix.field = field;
590b285192aSMauro Carvalho Chehab 	v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
591b285192aSMauro Carvalho Chehab 			      &f->fmt.pix.height, 32, maxh, 0, 0);
592b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline =
593b285192aSMauro Carvalho Chehab 		(f->fmt.pix.width * fmt->depth) >> 3;
594b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage =
595b285192aSMauro Carvalho Chehab 		f->fmt.pix.height * f->fmt.pix.bytesperline;
5961af2ddd8SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
597b285192aSMauro Carvalho Chehab 
598b285192aSMauro Carvalho Chehab 	return 0;
599b285192aSMauro Carvalho Chehab }
600b285192aSMauro Carvalho Chehab 
601b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
602b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
603b285192aSMauro Carvalho Chehab {
604568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
605ebf984bbSHans Verkuil 	struct v4l2_subdev_format format = {
606ebf984bbSHans Verkuil 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
607ebf984bbSHans Verkuil 	};
608b285192aSMauro Carvalho Chehab 	int err;
609b285192aSMauro Carvalho Chehab 
610b285192aSMauro Carvalho Chehab 	dprintk(2, "%s()\n", __func__);
611b285192aSMauro Carvalho Chehab 	err = vidioc_try_fmt_vid_cap(file, priv, f);
612b285192aSMauro Carvalho Chehab 
613b285192aSMauro Carvalho Chehab 	if (0 != err)
614b285192aSMauro Carvalho Chehab 		return err;
615f1b6a735SHans Verkuil 
616f1b6a735SHans Verkuil 	if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
617f1b6a735SHans Verkuil 	    vb2_is_busy(&dev->vb2_mpegq))
618f1b6a735SHans Verkuil 		return -EBUSY;
619f1b6a735SHans Verkuil 
62091d2d674SHans Verkuil 	dev->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
62191d2d674SHans Verkuil 	dev->width      = f->fmt.pix.width;
62291d2d674SHans Verkuil 	dev->height     = f->fmt.pix.height;
623453afdd9SHans Verkuil 	dev->field	= f->fmt.pix.field;
624b285192aSMauro Carvalho Chehab 	dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
625453afdd9SHans Verkuil 		dev->width, dev->height, dev->field);
626ebf984bbSHans Verkuil 	v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
627ebf984bbSHans Verkuil 	call_all(dev, pad, set_fmt, NULL, &format);
628ebf984bbSHans Verkuil 	v4l2_fill_pix_format(&f->fmt.pix, &format.format);
629ebf984bbSHans Verkuil 	/* set_fmt overwrites f->fmt.pix.field, restore it */
63096f233e9SHans Verkuil 	f->fmt.pix.field = dev->field;
631b285192aSMauro Carvalho Chehab 	return 0;
632b285192aSMauro Carvalho Chehab }
633b285192aSMauro Carvalho Chehab 
634b285192aSMauro Carvalho Chehab static int vidioc_querycap(struct file *file, void  *priv,
635b285192aSMauro Carvalho Chehab 	struct v4l2_capability *cap)
636b285192aSMauro Carvalho Chehab {
637568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
638a7d3eabdSHans Verkuil 	struct video_device *vdev = video_devdata(file);
639b285192aSMauro Carvalho Chehab 
640b285192aSMauro Carvalho Chehab 	strcpy(cap->driver, "cx23885");
641b285192aSMauro Carvalho Chehab 	strlcpy(cap->card, cx23885_boards[dev->board].name,
642b285192aSMauro Carvalho Chehab 		sizeof(cap->card));
643b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
644d43be757SHans Verkuil 	cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | V4L2_CAP_AUDIO;
6450f3bf3dcSHans Verkuil 	if (dev->tuner_type != TUNER_ABSENT)
646a7d3eabdSHans Verkuil 		cap->device_caps |= V4L2_CAP_TUNER;
647a7d3eabdSHans Verkuil 	if (vdev->vfl_type == VFL_TYPE_VBI)
648a7d3eabdSHans Verkuil 		cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
649a7d3eabdSHans Verkuil 	else
650a7d3eabdSHans Verkuil 		cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
651a7d3eabdSHans Verkuil 	cap->capabilities = cap->device_caps | V4L2_CAP_VBI_CAPTURE |
652a7d3eabdSHans Verkuil 		V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_DEVICE_CAPS;
653b285192aSMauro Carvalho Chehab 	return 0;
654b285192aSMauro Carvalho Chehab }
655b285192aSMauro Carvalho Chehab 
656b285192aSMauro Carvalho Chehab static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
657b285192aSMauro Carvalho Chehab 	struct v4l2_fmtdesc *f)
658b285192aSMauro Carvalho Chehab {
659b285192aSMauro Carvalho Chehab 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
660b285192aSMauro Carvalho Chehab 		return -EINVAL;
661b285192aSMauro Carvalho Chehab 
662b285192aSMauro Carvalho Chehab 	strlcpy(f->description, formats[f->index].name,
663b285192aSMauro Carvalho Chehab 		sizeof(f->description));
664b285192aSMauro Carvalho Chehab 	f->pixelformat = formats[f->index].fourcc;
665b285192aSMauro Carvalho Chehab 
666b285192aSMauro Carvalho Chehab 	return 0;
667b285192aSMauro Carvalho Chehab }
668b285192aSMauro Carvalho Chehab 
669e80bf856SHans Verkuil static int vidioc_cropcap(struct file *file, void *priv,
670e80bf856SHans Verkuil 			  struct v4l2_cropcap *cc)
671e80bf856SHans Verkuil {
672e80bf856SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
673e80bf856SHans Verkuil 	bool is_50hz = dev->tvnorm & V4L2_STD_625_50;
674e80bf856SHans Verkuil 
675e80bf856SHans Verkuil 	if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
676e80bf856SHans Verkuil 		return -EINVAL;
677e80bf856SHans Verkuil 
678e80bf856SHans Verkuil 	cc->bounds.left = 0;
679e80bf856SHans Verkuil 	cc->bounds.top = 0;
680e80bf856SHans Verkuil 	cc->bounds.width = 720;
681e80bf856SHans Verkuil 	cc->bounds.height = norm_maxh(dev->tvnorm);
682e80bf856SHans Verkuil 	cc->defrect = cc->bounds;
683e80bf856SHans Verkuil 	cc->pixelaspect.numerator = is_50hz ? 54 : 11;
684e80bf856SHans Verkuil 	cc->pixelaspect.denominator = is_50hz ? 59 : 10;
685e80bf856SHans Verkuil 
686e80bf856SHans Verkuil 	return 0;
687e80bf856SHans Verkuil }
688e80bf856SHans Verkuil 
689b285192aSMauro Carvalho Chehab static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
690b285192aSMauro Carvalho Chehab {
691568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
692b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
693b285192aSMauro Carvalho Chehab 
6949c1f5df8SHans Verkuil 	*id = dev->tvnorm;
695b285192aSMauro Carvalho Chehab 	return 0;
696b285192aSMauro Carvalho Chehab }
697b285192aSMauro Carvalho Chehab 
698314527acSHans Verkuil static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
699b285192aSMauro Carvalho Chehab {
700568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
701b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
702b285192aSMauro Carvalho Chehab 
703f1b6a735SHans Verkuil 	return cx23885_set_tvnorm(dev, tvnorms);
704b285192aSMauro Carvalho Chehab }
705b285192aSMauro Carvalho Chehab 
706b285192aSMauro Carvalho Chehab int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
707b285192aSMauro Carvalho Chehab {
708b285192aSMauro Carvalho Chehab 	static const char *iname[] = {
709b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE1] = "Composite1",
710b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE2] = "Composite2",
711b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE3] = "Composite3",
712b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE4] = "Composite4",
713b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_SVIDEO]     = "S-Video",
714b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPONENT]  = "Component",
715b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_TELEVISION] = "Television",
716b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_CABLE]      = "Cable TV",
717b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_DVB]        = "DVB",
718b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_DEBUG]      = "for debug only",
719b285192aSMauro Carvalho Chehab 	};
720b285192aSMauro Carvalho Chehab 	unsigned int n;
721b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
722b285192aSMauro Carvalho Chehab 
723b285192aSMauro Carvalho Chehab 	n = i->index;
724b285192aSMauro Carvalho Chehab 	if (n >= MAX_CX23885_INPUT)
725b285192aSMauro Carvalho Chehab 		return -EINVAL;
726b285192aSMauro Carvalho Chehab 
727b285192aSMauro Carvalho Chehab 	if (0 == INPUT(n)->type)
728b285192aSMauro Carvalho Chehab 		return -EINVAL;
729b285192aSMauro Carvalho Chehab 
730b285192aSMauro Carvalho Chehab 	i->index = n;
731b285192aSMauro Carvalho Chehab 	i->type  = V4L2_INPUT_TYPE_CAMERA;
732b285192aSMauro Carvalho Chehab 	strcpy(i->name, iname[INPUT(n)->type]);
733d43be757SHans Verkuil 	i->std = CX23885_NORMS;
734b285192aSMauro Carvalho Chehab 	if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
735b285192aSMauro Carvalho Chehab 		(CX23885_VMUX_CABLE == INPUT(n)->type)) {
736b285192aSMauro Carvalho Chehab 		i->type = V4L2_INPUT_TYPE_TUNER;
737d43be757SHans Verkuil 		i->audioset = 4;
738d43be757SHans Verkuil 	} else {
739b285192aSMauro Carvalho Chehab 		/* Two selectable audio inputs for non-tv inputs */
740d43be757SHans Verkuil 		i->audioset = 3;
741d43be757SHans Verkuil 	}
742b285192aSMauro Carvalho Chehab 
743b285192aSMauro Carvalho Chehab 	if (dev->input == n) {
744b285192aSMauro Carvalho Chehab 		/* enum'd input matches our configured input.
745b285192aSMauro Carvalho Chehab 		 * Ask the video decoder to process the call
746b285192aSMauro Carvalho Chehab 		 * and give it an oppertunity to update the
747b285192aSMauro Carvalho Chehab 		 * status field.
748b285192aSMauro Carvalho Chehab 		 */
749b285192aSMauro Carvalho Chehab 		call_all(dev, video, g_input_status, &i->status);
750b285192aSMauro Carvalho Chehab 	}
751b285192aSMauro Carvalho Chehab 
752b285192aSMauro Carvalho Chehab 	return 0;
753b285192aSMauro Carvalho Chehab }
754b285192aSMauro Carvalho Chehab 
755b285192aSMauro Carvalho Chehab static int vidioc_enum_input(struct file *file, void *priv,
756b285192aSMauro Carvalho Chehab 				struct v4l2_input *i)
757b285192aSMauro Carvalho Chehab {
758568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
759b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
760b285192aSMauro Carvalho Chehab 	return cx23885_enum_input(dev, i);
761b285192aSMauro Carvalho Chehab }
762b285192aSMauro Carvalho Chehab 
763b285192aSMauro Carvalho Chehab int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
764b285192aSMauro Carvalho Chehab {
765568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
766b285192aSMauro Carvalho Chehab 
767b285192aSMauro Carvalho Chehab 	*i = dev->input;
768b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() returns %d\n", __func__, *i);
769b285192aSMauro Carvalho Chehab 	return 0;
770b285192aSMauro Carvalho Chehab }
771b285192aSMauro Carvalho Chehab 
772b285192aSMauro Carvalho Chehab static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
773b285192aSMauro Carvalho Chehab {
774b285192aSMauro Carvalho Chehab 	return cx23885_get_input(file, priv, i);
775b285192aSMauro Carvalho Chehab }
776b285192aSMauro Carvalho Chehab 
777b285192aSMauro Carvalho Chehab int cx23885_set_input(struct file *file, void *priv, unsigned int i)
778b285192aSMauro Carvalho Chehab {
779568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
780b285192aSMauro Carvalho Chehab 
781b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(%d)\n", __func__, i);
782b285192aSMauro Carvalho Chehab 
783b285192aSMauro Carvalho Chehab 	if (i >= MAX_CX23885_INPUT) {
784b285192aSMauro Carvalho Chehab 		dprintk(1, "%s() -EINVAL\n", __func__);
785b285192aSMauro Carvalho Chehab 		return -EINVAL;
786b285192aSMauro Carvalho Chehab 	}
787b285192aSMauro Carvalho Chehab 
788b285192aSMauro Carvalho Chehab 	if (INPUT(i)->type == 0)
789b285192aSMauro Carvalho Chehab 		return -EINVAL;
790b285192aSMauro Carvalho Chehab 
791b285192aSMauro Carvalho Chehab 	cx23885_video_mux(dev, i);
792b285192aSMauro Carvalho Chehab 
793b285192aSMauro Carvalho Chehab 	/* By default establish the default audio input for the card also */
794b285192aSMauro Carvalho Chehab 	/* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
795b285192aSMauro Carvalho Chehab 	cx23885_audio_mux(dev, i);
796b285192aSMauro Carvalho Chehab 	return 0;
797b285192aSMauro Carvalho Chehab }
798b285192aSMauro Carvalho Chehab 
799b285192aSMauro Carvalho Chehab static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
800b285192aSMauro Carvalho Chehab {
801b285192aSMauro Carvalho Chehab 	return cx23885_set_input(file, priv, i);
802b285192aSMauro Carvalho Chehab }
803b285192aSMauro Carvalho Chehab 
804b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
805b285192aSMauro Carvalho Chehab {
806568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
807b285192aSMauro Carvalho Chehab 
808b285192aSMauro Carvalho Chehab 	call_all(dev, core, log_status);
809b285192aSMauro Carvalho Chehab 	return 0;
810b285192aSMauro Carvalho Chehab }
811b285192aSMauro Carvalho Chehab 
812b285192aSMauro Carvalho Chehab static int cx23885_query_audinput(struct file *file, void *priv,
813b285192aSMauro Carvalho Chehab 	struct v4l2_audio *i)
814b285192aSMauro Carvalho Chehab {
815568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
816b285192aSMauro Carvalho Chehab 	static const char *iname[] = {
817b285192aSMauro Carvalho Chehab 		[0] = "Baseband L/R 1",
818b285192aSMauro Carvalho Chehab 		[1] = "Baseband L/R 2",
819d43be757SHans Verkuil 		[2] = "TV",
820b285192aSMauro Carvalho Chehab 	};
821b285192aSMauro Carvalho Chehab 	unsigned int n;
822b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
823b285192aSMauro Carvalho Chehab 
824b285192aSMauro Carvalho Chehab 	n = i->index;
825d43be757SHans Verkuil 	if (n >= 3)
826b285192aSMauro Carvalho Chehab 		return -EINVAL;
827b285192aSMauro Carvalho Chehab 
828b285192aSMauro Carvalho Chehab 	memset(i, 0, sizeof(*i));
829b285192aSMauro Carvalho Chehab 	i->index = n;
830b285192aSMauro Carvalho Chehab 	strcpy(i->name, iname[n]);
831b285192aSMauro Carvalho Chehab 	i->capability = V4L2_AUDCAP_STEREO;
832b285192aSMauro Carvalho Chehab 	return 0;
833b285192aSMauro Carvalho Chehab 
834b285192aSMauro Carvalho Chehab }
835b285192aSMauro Carvalho Chehab 
836b285192aSMauro Carvalho Chehab static int vidioc_enum_audinput(struct file *file, void *priv,
837b285192aSMauro Carvalho Chehab 				struct v4l2_audio *i)
838b285192aSMauro Carvalho Chehab {
839b285192aSMauro Carvalho Chehab 	return cx23885_query_audinput(file, priv, i);
840b285192aSMauro Carvalho Chehab }
841b285192aSMauro Carvalho Chehab 
842b285192aSMauro Carvalho Chehab static int vidioc_g_audinput(struct file *file, void *priv,
843b285192aSMauro Carvalho Chehab 	struct v4l2_audio *i)
844b285192aSMauro Carvalho Chehab {
845568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
846b285192aSMauro Carvalho Chehab 
847d43be757SHans Verkuil 	if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
848d43be757SHans Verkuil 		(CX23885_VMUX_CABLE == INPUT(dev->input)->type))
849d43be757SHans Verkuil 		i->index = 2;
850d43be757SHans Verkuil 	else
851b285192aSMauro Carvalho Chehab 		i->index = dev->audinput;
852b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(input=%d)\n", __func__, i->index);
853b285192aSMauro Carvalho Chehab 
854b285192aSMauro Carvalho Chehab 	return cx23885_query_audinput(file, priv, i);
855b285192aSMauro Carvalho Chehab }
856b285192aSMauro Carvalho Chehab 
857b285192aSMauro Carvalho Chehab static int vidioc_s_audinput(struct file *file, void *priv,
8580e8025b9SHans Verkuil 	const struct v4l2_audio *i)
859b285192aSMauro Carvalho Chehab {
860568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
861d43be757SHans Verkuil 
862d43be757SHans Verkuil 	if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
863d43be757SHans Verkuil 		(CX23885_VMUX_CABLE == INPUT(dev->input)->type)) {
864d43be757SHans Verkuil 		return i->index != 2 ? -EINVAL : 0;
865d43be757SHans Verkuil 	}
866d43be757SHans Verkuil 	if (i->index > 1)
867b285192aSMauro Carvalho Chehab 		return -EINVAL;
868b285192aSMauro Carvalho Chehab 
869b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(%d)\n", __func__, i->index);
870b285192aSMauro Carvalho Chehab 
871b285192aSMauro Carvalho Chehab 	dev->audinput = i->index;
872b285192aSMauro Carvalho Chehab 
873b285192aSMauro Carvalho Chehab 	/* Skip the audio defaults from the cards struct, caller wants
874b285192aSMauro Carvalho Chehab 	 * directly touch the audio mux hardware. */
875b285192aSMauro Carvalho Chehab 	cx23885_flatiron_mux(dev, dev->audinput + 1);
876b285192aSMauro Carvalho Chehab 	return 0;
877b285192aSMauro Carvalho Chehab }
878b285192aSMauro Carvalho Chehab 
879b285192aSMauro Carvalho Chehab static int vidioc_g_tuner(struct file *file, void *priv,
880b285192aSMauro Carvalho Chehab 				struct v4l2_tuner *t)
881b285192aSMauro Carvalho Chehab {
882568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
883b285192aSMauro Carvalho Chehab 
8840f3bf3dcSHans Verkuil 	if (dev->tuner_type == TUNER_ABSENT)
885b285192aSMauro Carvalho Chehab 		return -EINVAL;
886b285192aSMauro Carvalho Chehab 	if (0 != t->index)
887b285192aSMauro Carvalho Chehab 		return -EINVAL;
888b285192aSMauro Carvalho Chehab 
889b285192aSMauro Carvalho Chehab 	strcpy(t->name, "Television");
890b285192aSMauro Carvalho Chehab 
891b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, g_tuner, t);
892b285192aSMauro Carvalho Chehab 	return 0;
893b285192aSMauro Carvalho Chehab }
894b285192aSMauro Carvalho Chehab 
895b285192aSMauro Carvalho Chehab static int vidioc_s_tuner(struct file *file, void *priv,
8962f73c7c5SHans Verkuil 				const struct v4l2_tuner *t)
897b285192aSMauro Carvalho Chehab {
898568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
899b285192aSMauro Carvalho Chehab 
9000f3bf3dcSHans Verkuil 	if (dev->tuner_type == TUNER_ABSENT)
901b285192aSMauro Carvalho Chehab 		return -EINVAL;
902b285192aSMauro Carvalho Chehab 	if (0 != t->index)
903b285192aSMauro Carvalho Chehab 		return -EINVAL;
904b285192aSMauro Carvalho Chehab 	/* Update the A/V core */
905b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, s_tuner, t);
906b285192aSMauro Carvalho Chehab 
907b285192aSMauro Carvalho Chehab 	return 0;
908b285192aSMauro Carvalho Chehab }
909b285192aSMauro Carvalho Chehab 
910b285192aSMauro Carvalho Chehab static int vidioc_g_frequency(struct file *file, void *priv,
911b285192aSMauro Carvalho Chehab 				struct v4l2_frequency *f)
912b285192aSMauro Carvalho Chehab {
913568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
914b285192aSMauro Carvalho Chehab 
9150f3bf3dcSHans Verkuil 	if (dev->tuner_type == TUNER_ABSENT)
916b285192aSMauro Carvalho Chehab 		return -EINVAL;
917b285192aSMauro Carvalho Chehab 
91886961933SHans Verkuil 	f->type = V4L2_TUNER_ANALOG_TV;
919b285192aSMauro Carvalho Chehab 	f->frequency = dev->freq;
920b285192aSMauro Carvalho Chehab 
921b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, g_frequency, f);
922b285192aSMauro Carvalho Chehab 
923b285192aSMauro Carvalho Chehab 	return 0;
924b285192aSMauro Carvalho Chehab }
925b285192aSMauro Carvalho Chehab 
926b530a447SHans Verkuil static int cx23885_set_freq(struct cx23885_dev *dev, const struct v4l2_frequency *f)
927b285192aSMauro Carvalho Chehab {
928da59a4deSHans Verkuil 	struct v4l2_ctrl *mute;
929da59a4deSHans Verkuil 	int old_mute_val = 1;
930b285192aSMauro Carvalho Chehab 
9310f3bf3dcSHans Verkuil 	if (dev->tuner_type == TUNER_ABSENT)
932b285192aSMauro Carvalho Chehab 		return -EINVAL;
933b285192aSMauro Carvalho Chehab 	if (unlikely(f->tuner != 0))
934b285192aSMauro Carvalho Chehab 		return -EINVAL;
935b285192aSMauro Carvalho Chehab 
936b285192aSMauro Carvalho Chehab 	dev->freq = f->frequency;
937b285192aSMauro Carvalho Chehab 
938b285192aSMauro Carvalho Chehab 	/* I need to mute audio here */
939da59a4deSHans Verkuil 	mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
940da59a4deSHans Verkuil 	if (mute) {
941da59a4deSHans Verkuil 		old_mute_val = v4l2_ctrl_g_ctrl(mute);
942da59a4deSHans Verkuil 		if (!old_mute_val)
943da59a4deSHans Verkuil 			v4l2_ctrl_s_ctrl(mute, 1);
944da59a4deSHans Verkuil 	}
945b285192aSMauro Carvalho Chehab 
946b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, s_frequency, f);
947b285192aSMauro Carvalho Chehab 
948b285192aSMauro Carvalho Chehab 	/* When changing channels it is required to reset TVAUDIO */
949b285192aSMauro Carvalho Chehab 	msleep(100);
950b285192aSMauro Carvalho Chehab 
951b285192aSMauro Carvalho Chehab 	/* I need to unmute audio here */
952da59a4deSHans Verkuil 	if (old_mute_val == 0)
953da59a4deSHans Verkuil 		v4l2_ctrl_s_ctrl(mute, old_mute_val);
954b285192aSMauro Carvalho Chehab 
955b285192aSMauro Carvalho Chehab 	return 0;
956b285192aSMauro Carvalho Chehab }
957b285192aSMauro Carvalho Chehab 
958b285192aSMauro Carvalho Chehab static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
959b530a447SHans Verkuil 	const struct v4l2_frequency *f)
960b285192aSMauro Carvalho Chehab {
961da59a4deSHans Verkuil 	struct v4l2_ctrl *mute;
962da59a4deSHans Verkuil 	int old_mute_val = 1;
963453afdd9SHans Verkuil 	struct vb2_dvb_frontend *vfe;
964b285192aSMauro Carvalho Chehab 	struct dvb_frontend *fe;
965b285192aSMauro Carvalho Chehab 
966b285192aSMauro Carvalho Chehab 	struct analog_parameters params = {
967b285192aSMauro Carvalho Chehab 		.mode      = V4L2_TUNER_ANALOG_TV,
968b285192aSMauro Carvalho Chehab 		.audmode   = V4L2_TUNER_MODE_STEREO,
969b285192aSMauro Carvalho Chehab 		.std       = dev->tvnorm,
970b285192aSMauro Carvalho Chehab 		.frequency = f->frequency
971b285192aSMauro Carvalho Chehab 	};
972b285192aSMauro Carvalho Chehab 
973b285192aSMauro Carvalho Chehab 	dev->freq = f->frequency;
974b285192aSMauro Carvalho Chehab 
975b285192aSMauro Carvalho Chehab 	/* I need to mute audio here */
976da59a4deSHans Verkuil 	mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
977da59a4deSHans Verkuil 	if (mute) {
978da59a4deSHans Verkuil 		old_mute_val = v4l2_ctrl_g_ctrl(mute);
979da59a4deSHans Verkuil 		if (!old_mute_val)
980da59a4deSHans Verkuil 			v4l2_ctrl_s_ctrl(mute, 1);
981da59a4deSHans Verkuil 	}
982b285192aSMauro Carvalho Chehab 
983b285192aSMauro Carvalho Chehab 	/* If HVR1850 */
984b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
985b285192aSMauro Carvalho Chehab 		params.frequency, f->tuner, params.std);
986b285192aSMauro Carvalho Chehab 
987453afdd9SHans Verkuil 	vfe = vb2_dvb_get_frontend(&dev->ts2.frontends, 1);
988b285192aSMauro Carvalho Chehab 	if (!vfe) {
989b285192aSMauro Carvalho Chehab 		return -EINVAL;
990b285192aSMauro Carvalho Chehab 	}
991b285192aSMauro Carvalho Chehab 
992b285192aSMauro Carvalho Chehab 	fe = vfe->dvb.frontend;
993b285192aSMauro Carvalho Chehab 
994b285192aSMauro Carvalho Chehab 	if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
995b285192aSMauro Carvalho Chehab 	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
996b285192aSMauro Carvalho Chehab 	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111))
997b285192aSMauro Carvalho Chehab 		fe = &dev->ts1.analog_fe;
998b285192aSMauro Carvalho Chehab 
999b285192aSMauro Carvalho Chehab 	if (fe && fe->ops.tuner_ops.set_analog_params) {
10008774bed9SLaurent Pinchart 		call_all(dev, video, s_std, dev->tvnorm);
1001b285192aSMauro Carvalho Chehab 		fe->ops.tuner_ops.set_analog_params(fe, &params);
1002b285192aSMauro Carvalho Chehab 	}
1003b285192aSMauro Carvalho Chehab 	else
1004b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
1005b285192aSMauro Carvalho Chehab 
1006b285192aSMauro Carvalho Chehab 	/* When changing channels it is required to reset TVAUDIO */
1007b285192aSMauro Carvalho Chehab 	msleep(100);
1008b285192aSMauro Carvalho Chehab 
1009b285192aSMauro Carvalho Chehab 	/* I need to unmute audio here */
1010da59a4deSHans Verkuil 	if (old_mute_val == 0)
1011da59a4deSHans Verkuil 		v4l2_ctrl_s_ctrl(mute, old_mute_val);
1012b285192aSMauro Carvalho Chehab 
1013b285192aSMauro Carvalho Chehab 	return 0;
1014b285192aSMauro Carvalho Chehab }
1015b285192aSMauro Carvalho Chehab 
1016b285192aSMauro Carvalho Chehab int cx23885_set_frequency(struct file *file, void *priv,
1017b530a447SHans Verkuil 	const struct v4l2_frequency *f)
1018b285192aSMauro Carvalho Chehab {
1019568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
1020b285192aSMauro Carvalho Chehab 	int ret;
1021b285192aSMauro Carvalho Chehab 
1022b285192aSMauro Carvalho Chehab 	switch (dev->board) {
1023b285192aSMauro Carvalho Chehab 	case CX23885_BOARD_HAUPPAUGE_HVR1255:
1024b285192aSMauro Carvalho Chehab 	case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1025b285192aSMauro Carvalho Chehab 	case CX23885_BOARD_HAUPPAUGE_HVR1850:
1026b285192aSMauro Carvalho Chehab 		ret = cx23885_set_freq_via_ops(dev, f);
1027b285192aSMauro Carvalho Chehab 		break;
1028b285192aSMauro Carvalho Chehab 	default:
1029b285192aSMauro Carvalho Chehab 		ret = cx23885_set_freq(dev, f);
1030b285192aSMauro Carvalho Chehab 	}
1031b285192aSMauro Carvalho Chehab 
1032b285192aSMauro Carvalho Chehab 	return ret;
1033b285192aSMauro Carvalho Chehab }
1034b285192aSMauro Carvalho Chehab 
1035b285192aSMauro Carvalho Chehab static int vidioc_s_frequency(struct file *file, void *priv,
1036b530a447SHans Verkuil 	const struct v4l2_frequency *f)
1037b285192aSMauro Carvalho Chehab {
1038b285192aSMauro Carvalho Chehab 	return cx23885_set_frequency(file, priv, f);
1039b285192aSMauro Carvalho Chehab }
1040b285192aSMauro Carvalho Chehab 
1041b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------- */
1042b285192aSMauro Carvalho Chehab 
1043b285192aSMauro Carvalho Chehab int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1044b285192aSMauro Carvalho Chehab {
1045b285192aSMauro Carvalho Chehab 	u32 mask, count;
1046b285192aSMauro Carvalho Chehab 	int handled = 0;
1047b285192aSMauro Carvalho Chehab 
1048b285192aSMauro Carvalho Chehab 	mask   = cx_read(VID_A_INT_MSK);
1049b285192aSMauro Carvalho Chehab 	if (0 == (status & mask))
1050b285192aSMauro Carvalho Chehab 		return handled;
1051b285192aSMauro Carvalho Chehab 
1052b285192aSMauro Carvalho Chehab 	cx_write(VID_A_INT_STAT, status);
1053b285192aSMauro Carvalho Chehab 
1054b285192aSMauro Carvalho Chehab 	/* risc op code error, fifo overflow or line sync detection error */
1055b285192aSMauro Carvalho Chehab 	if ((status & VID_BC_MSK_OPC_ERR) ||
1056b285192aSMauro Carvalho Chehab 		(status & VID_BC_MSK_SYNC) ||
1057b285192aSMauro Carvalho Chehab 		(status & VID_BC_MSK_OF)) {
1058b285192aSMauro Carvalho Chehab 
1059b285192aSMauro Carvalho Chehab 		if (status & VID_BC_MSK_OPC_ERR) {
1060b285192aSMauro Carvalho Chehab 			dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1061b285192aSMauro Carvalho Chehab 				VID_BC_MSK_OPC_ERR);
1062b285192aSMauro Carvalho Chehab 			printk(KERN_WARNING "%s: video risc op code error\n",
1063b285192aSMauro Carvalho Chehab 				dev->name);
1064b285192aSMauro Carvalho Chehab 			cx23885_sram_channel_dump(dev,
1065b285192aSMauro Carvalho Chehab 				&dev->sram_channels[SRAM_CH01]);
1066b285192aSMauro Carvalho Chehab 		}
1067b285192aSMauro Carvalho Chehab 
1068b285192aSMauro Carvalho Chehab 		if (status & VID_BC_MSK_SYNC)
1069b285192aSMauro Carvalho Chehab 			dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
1070b285192aSMauro Carvalho Chehab 				"video lines miss-match\n",
1071b285192aSMauro Carvalho Chehab 				VID_BC_MSK_SYNC);
1072b285192aSMauro Carvalho Chehab 
1073b285192aSMauro Carvalho Chehab 		if (status & VID_BC_MSK_OF)
1074b285192aSMauro Carvalho Chehab 			dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1075b285192aSMauro Carvalho Chehab 				VID_BC_MSK_OF);
1076b285192aSMauro Carvalho Chehab 
1077b285192aSMauro Carvalho Chehab 	}
1078b285192aSMauro Carvalho Chehab 
1079b285192aSMauro Carvalho Chehab 	/* Video */
1080b285192aSMauro Carvalho Chehab 	if (status & VID_BC_MSK_RISCI1) {
1081b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
1082b285192aSMauro Carvalho Chehab 		count = cx_read(VID_A_GPCNT);
1083b285192aSMauro Carvalho Chehab 		cx23885_video_wakeup(dev, &dev->vidq, count);
1084b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
1085b285192aSMauro Carvalho Chehab 		handled++;
1086b285192aSMauro Carvalho Chehab 	}
1087b285192aSMauro Carvalho Chehab 
1088b285192aSMauro Carvalho Chehab 	/* Allow the VBI framework to process it's payload */
1089b285192aSMauro Carvalho Chehab 	handled += cx23885_vbi_irq(dev, status);
1090b285192aSMauro Carvalho Chehab 
1091b285192aSMauro Carvalho Chehab 	return handled;
1092b285192aSMauro Carvalho Chehab }
1093b285192aSMauro Carvalho Chehab 
1094b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------- */
1095b285192aSMauro Carvalho Chehab /* exported stuff                                              */
1096b285192aSMauro Carvalho Chehab 
1097b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops = {
1098b285192aSMauro Carvalho Chehab 	.owner	       = THIS_MODULE,
1099453afdd9SHans Verkuil 	.open           = v4l2_fh_open,
1100453afdd9SHans Verkuil 	.release        = vb2_fop_release,
1101453afdd9SHans Verkuil 	.read           = vb2_fop_read,
1102453afdd9SHans Verkuil 	.poll		= vb2_fop_poll,
110324a8f7b5SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
1104453afdd9SHans Verkuil 	.mmap           = vb2_fop_mmap,
1105b285192aSMauro Carvalho Chehab };
1106b285192aSMauro Carvalho Chehab 
1107b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops = {
1108b285192aSMauro Carvalho Chehab 	.vidioc_querycap      = vidioc_querycap,
1109b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1110b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1111b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1112b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1113b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vbi_cap     = cx23885_vbi_fmt,
1114b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vbi_cap   = cx23885_vbi_fmt,
1115b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vbi_cap     = cx23885_vbi_fmt,
1116453afdd9SHans Verkuil 	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
1117453afdd9SHans Verkuil 	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
1118453afdd9SHans Verkuil 	.vidioc_querybuf      = vb2_ioctl_querybuf,
1119453afdd9SHans Verkuil 	.vidioc_qbuf          = vb2_ioctl_qbuf,
1120453afdd9SHans Verkuil 	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
1121453afdd9SHans Verkuil 	.vidioc_streamon      = vb2_ioctl_streamon,
1122453afdd9SHans Verkuil 	.vidioc_streamoff     = vb2_ioctl_streamoff,
1123e80bf856SHans Verkuil 	.vidioc_cropcap       = vidioc_cropcap,
1124b285192aSMauro Carvalho Chehab 	.vidioc_s_std         = vidioc_s_std,
1125b285192aSMauro Carvalho Chehab 	.vidioc_g_std         = vidioc_g_std,
1126b285192aSMauro Carvalho Chehab 	.vidioc_enum_input    = vidioc_enum_input,
1127b285192aSMauro Carvalho Chehab 	.vidioc_g_input       = vidioc_g_input,
1128b285192aSMauro Carvalho Chehab 	.vidioc_s_input       = vidioc_s_input,
1129b285192aSMauro Carvalho Chehab 	.vidioc_log_status    = vidioc_log_status,
1130b285192aSMauro Carvalho Chehab 	.vidioc_g_tuner       = vidioc_g_tuner,
1131b285192aSMauro Carvalho Chehab 	.vidioc_s_tuner       = vidioc_s_tuner,
1132b285192aSMauro Carvalho Chehab 	.vidioc_g_frequency   = vidioc_g_frequency,
1133b285192aSMauro Carvalho Chehab 	.vidioc_s_frequency   = vidioc_s_frequency,
1134b285192aSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
113580f8568fSHans Verkuil 	.vidioc_g_chip_info   = cx23885_g_chip_info,
1136b285192aSMauro Carvalho Chehab 	.vidioc_g_register    = cx23885_g_register,
1137b285192aSMauro Carvalho Chehab 	.vidioc_s_register    = cx23885_s_register,
1138b285192aSMauro Carvalho Chehab #endif
1139b285192aSMauro Carvalho Chehab 	.vidioc_enumaudio     = vidioc_enum_audinput,
1140b285192aSMauro Carvalho Chehab 	.vidioc_g_audio       = vidioc_g_audinput,
1141b285192aSMauro Carvalho Chehab 	.vidioc_s_audio       = vidioc_s_audinput,
1142da59a4deSHans Verkuil 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1143da59a4deSHans Verkuil 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1144b285192aSMauro Carvalho Chehab };
1145b285192aSMauro Carvalho Chehab 
1146b285192aSMauro Carvalho Chehab static struct video_device cx23885_vbi_template;
1147b285192aSMauro Carvalho Chehab static struct video_device cx23885_video_template = {
1148b285192aSMauro Carvalho Chehab 	.name                 = "cx23885-video",
1149b285192aSMauro Carvalho Chehab 	.fops                 = &video_fops,
1150b285192aSMauro Carvalho Chehab 	.ioctl_ops 	      = &video_ioctl_ops,
1151b285192aSMauro Carvalho Chehab 	.tvnorms              = CX23885_NORMS,
1152b285192aSMauro Carvalho Chehab };
1153b285192aSMauro Carvalho Chehab 
1154b285192aSMauro Carvalho Chehab void cx23885_video_unregister(struct cx23885_dev *dev)
1155b285192aSMauro Carvalho Chehab {
1156b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1157b285192aSMauro Carvalho Chehab 	cx23885_irq_remove(dev, 0x01);
1158b285192aSMauro Carvalho Chehab 
1159b285192aSMauro Carvalho Chehab 	if (dev->vbi_dev) {
1160b285192aSMauro Carvalho Chehab 		if (video_is_registered(dev->vbi_dev))
1161b285192aSMauro Carvalho Chehab 			video_unregister_device(dev->vbi_dev);
1162b285192aSMauro Carvalho Chehab 		else
1163b285192aSMauro Carvalho Chehab 			video_device_release(dev->vbi_dev);
1164b285192aSMauro Carvalho Chehab 		dev->vbi_dev = NULL;
1165b285192aSMauro Carvalho Chehab 	}
1166b285192aSMauro Carvalho Chehab 	if (dev->video_dev) {
1167b285192aSMauro Carvalho Chehab 		if (video_is_registered(dev->video_dev))
1168b285192aSMauro Carvalho Chehab 			video_unregister_device(dev->video_dev);
1169b285192aSMauro Carvalho Chehab 		else
1170b285192aSMauro Carvalho Chehab 			video_device_release(dev->video_dev);
1171b285192aSMauro Carvalho Chehab 		dev->video_dev = NULL;
1172b285192aSMauro Carvalho Chehab 	}
1173b285192aSMauro Carvalho Chehab 
1174b285192aSMauro Carvalho Chehab 	if (dev->audio_dev)
1175b285192aSMauro Carvalho Chehab 		cx23885_audio_unregister(dev);
1176b285192aSMauro Carvalho Chehab }
1177b285192aSMauro Carvalho Chehab 
1178b285192aSMauro Carvalho Chehab int cx23885_video_register(struct cx23885_dev *dev)
1179b285192aSMauro Carvalho Chehab {
1180453afdd9SHans Verkuil 	struct vb2_queue *q;
1181b285192aSMauro Carvalho Chehab 	int err;
1182b285192aSMauro Carvalho Chehab 
1183b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1184b285192aSMauro Carvalho Chehab 
1185b285192aSMauro Carvalho Chehab 	/* Initialize VBI template */
11863618acabSEzequiel Garcia 	cx23885_vbi_template = cx23885_video_template;
1187b285192aSMauro Carvalho Chehab 	strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1188b285192aSMauro Carvalho Chehab 
11899c1f5df8SHans Verkuil 	dev->tvnorm = V4L2_STD_NTSC_M;
119091d2d674SHans Verkuil 	dev->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
119196f233e9SHans Verkuil 	dev->field = V4L2_FIELD_INTERLACED;
11921c5eaa23SHans Verkuil 	dev->width = 720;
119391d2d674SHans Verkuil 	dev->height = norm_maxh(dev->tvnorm);
1194b285192aSMauro Carvalho Chehab 
1195b285192aSMauro Carvalho Chehab 	/* init video dma queues */
1196b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->vidq.active);
1197b285192aSMauro Carvalho Chehab 
1198b285192aSMauro Carvalho Chehab 	/* init vbi dma queues */
1199b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->vbiq.active);
1200b285192aSMauro Carvalho Chehab 
1201b285192aSMauro Carvalho Chehab 	cx23885_irq_add_enable(dev, 0x01);
1202b285192aSMauro Carvalho Chehab 
1203b285192aSMauro Carvalho Chehab 	if ((TUNER_ABSENT != dev->tuner_type) &&
1204b285192aSMauro Carvalho Chehab 			((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1205b285192aSMauro Carvalho Chehab 		struct v4l2_subdev *sd = NULL;
1206b285192aSMauro Carvalho Chehab 
1207b285192aSMauro Carvalho Chehab 		if (dev->tuner_addr)
1208b285192aSMauro Carvalho Chehab 			sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1209b285192aSMauro Carvalho Chehab 				&dev->i2c_bus[dev->tuner_bus].i2c_adap,
1210b285192aSMauro Carvalho Chehab 				"tuner", dev->tuner_addr, NULL);
1211b285192aSMauro Carvalho Chehab 		else
1212b285192aSMauro Carvalho Chehab 			sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1213b285192aSMauro Carvalho Chehab 				&dev->i2c_bus[dev->tuner_bus].i2c_adap,
1214b285192aSMauro Carvalho Chehab 				"tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1215b285192aSMauro Carvalho Chehab 		if (sd) {
1216b285192aSMauro Carvalho Chehab 			struct tuner_setup tun_setup;
1217b285192aSMauro Carvalho Chehab 
1218b285192aSMauro Carvalho Chehab 			memset(&tun_setup, 0, sizeof(tun_setup));
1219b285192aSMauro Carvalho Chehab 			tun_setup.mode_mask = T_ANALOG_TV;
1220b285192aSMauro Carvalho Chehab 			tun_setup.type = dev->tuner_type;
1221b285192aSMauro Carvalho Chehab 			tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1222b285192aSMauro Carvalho Chehab 			tun_setup.tuner_callback = cx23885_tuner_callback;
1223b285192aSMauro Carvalho Chehab 
1224b285192aSMauro Carvalho Chehab 			v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1225b285192aSMauro Carvalho Chehab 
1226642ca1a0SAnca Emanuel 			if ((dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) ||
1227642ca1a0SAnca Emanuel 			    (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200)) {
1228b285192aSMauro Carvalho Chehab 				struct xc2028_ctrl ctrl = {
1229b285192aSMauro Carvalho Chehab 					.fname = XC2028_DEFAULT_FIRMWARE,
1230b285192aSMauro Carvalho Chehab 					.max_len = 64
1231b285192aSMauro Carvalho Chehab 				};
1232b285192aSMauro Carvalho Chehab 				struct v4l2_priv_tun_config cfg = {
1233b285192aSMauro Carvalho Chehab 					.tuner = dev->tuner_type,
1234b285192aSMauro Carvalho Chehab 					.priv = &ctrl
1235b285192aSMauro Carvalho Chehab 				};
1236b285192aSMauro Carvalho Chehab 				v4l2_subdev_call(sd, tuner, s_config, &cfg);
1237b285192aSMauro Carvalho Chehab 			}
1238e8d42373SOleh Kravchenko 
1239e8d42373SOleh Kravchenko 			if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
1240e8d42373SOleh Kravchenko 				struct xc2028_ctrl ctrl = {
1241e8d42373SOleh Kravchenko 					.fname = "xc3028L-v36.fw",
1242e8d42373SOleh Kravchenko 					.max_len = 64
1243e8d42373SOleh Kravchenko 				};
1244e8d42373SOleh Kravchenko 				struct v4l2_priv_tun_config cfg = {
1245e8d42373SOleh Kravchenko 					.tuner = dev->tuner_type,
1246e8d42373SOleh Kravchenko 					.priv = &ctrl
1247e8d42373SOleh Kravchenko 				};
1248e8d42373SOleh Kravchenko 				v4l2_subdev_call(sd, tuner, s_config, &cfg);
1249e8d42373SOleh Kravchenko 			}
1250b285192aSMauro Carvalho Chehab 		}
1251b285192aSMauro Carvalho Chehab 	}
1252b285192aSMauro Carvalho Chehab 
125324a8f7b5SHans Verkuil 	/* initial device configuration */
125424a8f7b5SHans Verkuil 	mutex_lock(&dev->lock);
125524a8f7b5SHans Verkuil 	cx23885_set_tvnorm(dev, dev->tvnorm);
125624a8f7b5SHans Verkuil 	cx23885_video_mux(dev, 0);
125724a8f7b5SHans Verkuil 	cx23885_audio_mux(dev, 0);
125824a8f7b5SHans Verkuil 	mutex_unlock(&dev->lock);
125924a8f7b5SHans Verkuil 
1260453afdd9SHans Verkuil 	q = &dev->vb2_vidq;
1261453afdd9SHans Verkuil 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1262453afdd9SHans Verkuil 	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1263453afdd9SHans Verkuil 	q->gfp_flags = GFP_DMA32;
1264453afdd9SHans Verkuil 	q->min_buffers_needed = 2;
1265453afdd9SHans Verkuil 	q->drv_priv = dev;
1266453afdd9SHans Verkuil 	q->buf_struct_size = sizeof(struct cx23885_buffer);
1267453afdd9SHans Verkuil 	q->ops = &cx23885_video_qops;
1268453afdd9SHans Verkuil 	q->mem_ops = &vb2_dma_sg_memops;
1269453afdd9SHans Verkuil 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1270453afdd9SHans Verkuil 	q->lock = &dev->lock;
1271453afdd9SHans Verkuil 
1272453afdd9SHans Verkuil 	err = vb2_queue_init(q);
1273453afdd9SHans Verkuil 	if (err < 0)
1274453afdd9SHans Verkuil 		goto fail_unreg;
1275453afdd9SHans Verkuil 
1276453afdd9SHans Verkuil 	q = &dev->vb2_vbiq;
1277453afdd9SHans Verkuil 	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1278453afdd9SHans Verkuil 	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1279453afdd9SHans Verkuil 	q->gfp_flags = GFP_DMA32;
1280453afdd9SHans Verkuil 	q->min_buffers_needed = 2;
1281453afdd9SHans Verkuil 	q->drv_priv = dev;
1282453afdd9SHans Verkuil 	q->buf_struct_size = sizeof(struct cx23885_buffer);
1283453afdd9SHans Verkuil 	q->ops = &cx23885_vbi_qops;
1284453afdd9SHans Verkuil 	q->mem_ops = &vb2_dma_sg_memops;
1285453afdd9SHans Verkuil 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1286453afdd9SHans Verkuil 	q->lock = &dev->lock;
1287453afdd9SHans Verkuil 
1288453afdd9SHans Verkuil 	err = vb2_queue_init(q);
1289453afdd9SHans Verkuil 	if (err < 0)
1290453afdd9SHans Verkuil 		goto fail_unreg;
1291453afdd9SHans Verkuil 
1292b285192aSMauro Carvalho Chehab 	/* register Video device */
1293b285192aSMauro Carvalho Chehab 	dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1294b285192aSMauro Carvalho Chehab 		&cx23885_video_template, "video");
1295453afdd9SHans Verkuil 	dev->video_dev->queue = &dev->vb2_vidq;
1296b285192aSMauro Carvalho Chehab 	err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1297b285192aSMauro Carvalho Chehab 				    video_nr[dev->nr]);
1298b285192aSMauro Carvalho Chehab 	if (err < 0) {
1299b285192aSMauro Carvalho Chehab 		printk(KERN_INFO "%s: can't register video device\n",
1300b285192aSMauro Carvalho Chehab 			dev->name);
1301b285192aSMauro Carvalho Chehab 		goto fail_unreg;
1302b285192aSMauro Carvalho Chehab 	}
1303b285192aSMauro Carvalho Chehab 	printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1304b285192aSMauro Carvalho Chehab 	       dev->name, video_device_node_name(dev->video_dev));
1305b285192aSMauro Carvalho Chehab 
1306b285192aSMauro Carvalho Chehab 	/* register VBI device */
1307b285192aSMauro Carvalho Chehab 	dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1308b285192aSMauro Carvalho Chehab 		&cx23885_vbi_template, "vbi");
1309453afdd9SHans Verkuil 	dev->vbi_dev->queue = &dev->vb2_vbiq;
1310b285192aSMauro Carvalho Chehab 	err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1311b285192aSMauro Carvalho Chehab 				    vbi_nr[dev->nr]);
1312b285192aSMauro Carvalho Chehab 	if (err < 0) {
1313b285192aSMauro Carvalho Chehab 		printk(KERN_INFO "%s: can't register vbi device\n",
1314b285192aSMauro Carvalho Chehab 			dev->name);
1315b285192aSMauro Carvalho Chehab 		goto fail_unreg;
1316b285192aSMauro Carvalho Chehab 	}
1317b285192aSMauro Carvalho Chehab 	printk(KERN_INFO "%s: registered device %s\n",
1318b285192aSMauro Carvalho Chehab 	       dev->name, video_device_node_name(dev->vbi_dev));
1319b285192aSMauro Carvalho Chehab 
1320b285192aSMauro Carvalho Chehab 	/* Register ALSA audio device */
1321b285192aSMauro Carvalho Chehab 	dev->audio_dev = cx23885_audio_register(dev);
1322b285192aSMauro Carvalho Chehab 
1323b285192aSMauro Carvalho Chehab 	return 0;
1324b285192aSMauro Carvalho Chehab 
1325b285192aSMauro Carvalho Chehab fail_unreg:
1326b285192aSMauro Carvalho Chehab 	cx23885_video_unregister(dev);
1327b285192aSMauro Carvalho Chehab 	return err;
1328b285192aSMauro Carvalho Chehab }
1329