xref: /linux/drivers/media/pci/cx23885/cx23885-video.c (revision e39682b5d96ae7a33a0f6b5578911913be8f14b6)
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 
18*e39682b5SMauro Carvalho Chehab #include "cx23885.h"
19*e39682b5SMauro Carvalho Chehab #include "cx23885-video.h"
20*e39682b5SMauro Carvalho Chehab 
21b285192aSMauro Carvalho Chehab #include <linux/init.h>
22b285192aSMauro Carvalho Chehab #include <linux/list.h>
23b285192aSMauro Carvalho Chehab #include <linux/module.h>
24b285192aSMauro Carvalho Chehab #include <linux/moduleparam.h>
25b285192aSMauro Carvalho Chehab #include <linux/kmod.h>
26b285192aSMauro Carvalho Chehab #include <linux/kernel.h>
27b285192aSMauro Carvalho Chehab #include <linux/slab.h>
28b285192aSMauro Carvalho Chehab #include <linux/interrupt.h>
29b285192aSMauro Carvalho Chehab #include <linux/delay.h>
30b285192aSMauro Carvalho Chehab #include <linux/kthread.h>
31b285192aSMauro Carvalho Chehab #include <asm/div64.h>
32b285192aSMauro Carvalho Chehab 
33b285192aSMauro Carvalho Chehab #include <media/v4l2-common.h>
34b285192aSMauro Carvalho Chehab #include <media/v4l2-ioctl.h>
35da59a4deSHans Verkuil #include <media/v4l2-event.h>
36b285192aSMauro Carvalho Chehab #include "cx23885-ioctl.h"
37b285192aSMauro Carvalho Chehab #include "tuner-xc2028.h"
38b285192aSMauro Carvalho Chehab 
39d647f0b7SMauro Carvalho Chehab #include <media/drv-intf/cx25840.h>
40b285192aSMauro Carvalho Chehab 
41b285192aSMauro Carvalho Chehab MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
42b285192aSMauro Carvalho Chehab MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
43b285192aSMauro Carvalho Chehab MODULE_LICENSE("GPL");
44b285192aSMauro Carvalho Chehab 
45b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
46b285192aSMauro Carvalho Chehab 
47b285192aSMauro Carvalho Chehab static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
48b285192aSMauro Carvalho Chehab static unsigned int vbi_nr[]   = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
49b285192aSMauro Carvalho Chehab 
50b285192aSMauro Carvalho Chehab module_param_array(video_nr, int, NULL, 0444);
51b285192aSMauro Carvalho Chehab module_param_array(vbi_nr,   int, NULL, 0444);
52b285192aSMauro Carvalho Chehab 
53b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_nr, "video device numbers");
54b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
55b285192aSMauro Carvalho Chehab 
56b285192aSMauro Carvalho Chehab static unsigned int video_debug;
57b285192aSMauro Carvalho Chehab module_param(video_debug, int, 0644);
58b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
59b285192aSMauro Carvalho Chehab 
60b285192aSMauro Carvalho Chehab static unsigned int irq_debug;
61b285192aSMauro Carvalho Chehab module_param(irq_debug, int, 0644);
62b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
63b285192aSMauro Carvalho Chehab 
64b285192aSMauro Carvalho Chehab static unsigned int vid_limit = 16;
65b285192aSMauro Carvalho Chehab module_param(vid_limit, int, 0644);
66b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
67b285192aSMauro Carvalho Chehab 
68b285192aSMauro Carvalho Chehab #define dprintk(level, fmt, arg...)\
69b285192aSMauro Carvalho Chehab 	do { if (video_debug >= level)\
70*e39682b5SMauro Carvalho Chehab 		printk(KERN_DEBUG pr_fmt("%s: video:" fmt), \
71*e39682b5SMauro Carvalho Chehab 			__func__, ##arg); \
72b285192aSMauro Carvalho Chehab 	} while (0)
73b285192aSMauro Carvalho Chehab 
74b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------- */
75b285192aSMauro Carvalho Chehab /* static data                                                         */
76b285192aSMauro Carvalho Chehab 
77b285192aSMauro Carvalho Chehab #define FORMAT_FLAGS_PACKED       0x01
78b285192aSMauro Carvalho Chehab static struct cx23885_fmt formats[] = {
79b285192aSMauro Carvalho Chehab 	{
80b285192aSMauro Carvalho Chehab 		.name     = "4:2:2, packed, YUYV",
81b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_YUYV,
82b285192aSMauro Carvalho Chehab 		.depth    = 16,
83b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
84b285192aSMauro Carvalho Chehab 	}
85b285192aSMauro Carvalho Chehab };
86b285192aSMauro Carvalho Chehab 
87b285192aSMauro Carvalho Chehab static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
88b285192aSMauro Carvalho Chehab {
89b285192aSMauro Carvalho Chehab 	unsigned int i;
90b285192aSMauro Carvalho Chehab 
91b285192aSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(formats); i++)
92b285192aSMauro Carvalho Chehab 		if (formats[i].fourcc == fourcc)
93b285192aSMauro Carvalho Chehab 			return formats+i;
94b285192aSMauro Carvalho Chehab 	return NULL;
95b285192aSMauro Carvalho Chehab }
96b285192aSMauro Carvalho Chehab 
97b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------- */
98b285192aSMauro Carvalho Chehab 
99b285192aSMauro Carvalho Chehab void cx23885_video_wakeup(struct cx23885_dev *dev,
100b285192aSMauro Carvalho Chehab 	struct cx23885_dmaqueue *q, u32 count)
101b285192aSMauro Carvalho Chehab {
102b285192aSMauro Carvalho Chehab 	struct cx23885_buffer *buf;
103b285192aSMauro Carvalho Chehab 
104b285192aSMauro Carvalho Chehab 	if (list_empty(&q->active))
105453afdd9SHans Verkuil 		return;
106b285192aSMauro Carvalho Chehab 	buf = list_entry(q->active.next,
107453afdd9SHans Verkuil 			struct cx23885_buffer, queue);
108b285192aSMauro Carvalho Chehab 
1092d700715SJunghak Sung 	buf->vb.sequence = q->count++;
110d6dd645eSJunghak Sung 	buf->vb.vb2_buf.timestamp = ktime_get_ns();
1112d700715SJunghak Sung 	dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf,
1122d700715SJunghak Sung 			buf->vb.vb2_buf.index, count, q->count);
113453afdd9SHans Verkuil 	list_del(&buf->queue);
1142d700715SJunghak Sung 	vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
115b285192aSMauro Carvalho Chehab }
116b285192aSMauro Carvalho Chehab 
117b285192aSMauro Carvalho Chehab int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
118b285192aSMauro Carvalho Chehab {
119e80bf856SHans Verkuil 	struct v4l2_subdev_format format = {
120e80bf856SHans Verkuil 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
121e80bf856SHans Verkuil 		.format.code = MEDIA_BUS_FMT_FIXED,
122e80bf856SHans Verkuil 	};
123e80bf856SHans Verkuil 
124b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
125b285192aSMauro Carvalho Chehab 		__func__,
126b285192aSMauro Carvalho Chehab 		(unsigned int)norm,
127b285192aSMauro Carvalho Chehab 		v4l2_norm_to_name(norm));
128b285192aSMauro Carvalho Chehab 
129e80bf856SHans Verkuil 	if (dev->tvnorm == norm)
130e80bf856SHans Verkuil 		return 0;
131e80bf856SHans Verkuil 
132f1b6a735SHans Verkuil 	if (dev->tvnorm != norm) {
133f1b6a735SHans Verkuil 		if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
134f1b6a735SHans Verkuil 		    vb2_is_busy(&dev->vb2_mpegq))
135f1b6a735SHans Verkuil 			return -EBUSY;
136f1b6a735SHans Verkuil 	}
137f1b6a735SHans Verkuil 
138b285192aSMauro Carvalho Chehab 	dev->tvnorm = norm;
139e80bf856SHans Verkuil 	dev->width = 720;
140e80bf856SHans Verkuil 	dev->height = norm_maxh(norm);
141e80bf856SHans Verkuil 	dev->field = V4L2_FIELD_INTERLACED;
142b285192aSMauro Carvalho Chehab 
1438774bed9SLaurent Pinchart 	call_all(dev, video, s_std, norm);
144b285192aSMauro Carvalho Chehab 
145e80bf856SHans Verkuil 	format.format.width = dev->width;
146e80bf856SHans Verkuil 	format.format.height = dev->height;
147e80bf856SHans Verkuil 	format.format.field = dev->field;
148e80bf856SHans Verkuil 	call_all(dev, pad, set_fmt, NULL, &format);
149e80bf856SHans Verkuil 
150b285192aSMauro Carvalho Chehab 	return 0;
151b285192aSMauro Carvalho Chehab }
152b285192aSMauro Carvalho Chehab 
153b285192aSMauro Carvalho Chehab static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
154b285192aSMauro Carvalho Chehab 				    struct pci_dev *pci,
155b285192aSMauro Carvalho Chehab 				    struct video_device *template,
156b285192aSMauro Carvalho Chehab 				    char *type)
157b285192aSMauro Carvalho Chehab {
158b285192aSMauro Carvalho Chehab 	struct video_device *vfd;
159b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
160b285192aSMauro Carvalho Chehab 
161b285192aSMauro Carvalho Chehab 	vfd = video_device_alloc();
162b285192aSMauro Carvalho Chehab 	if (NULL == vfd)
163b285192aSMauro Carvalho Chehab 		return NULL;
164b285192aSMauro Carvalho Chehab 	*vfd = *template;
165b285192aSMauro Carvalho Chehab 	vfd->v4l2_dev = &dev->v4l2_dev;
166b285192aSMauro Carvalho Chehab 	vfd->release = video_device_release;
16724a8f7b5SHans Verkuil 	vfd->lock = &dev->lock;
168b285192aSMauro Carvalho Chehab 	snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
169b285192aSMauro Carvalho Chehab 		 cx23885_boards[dev->board].name, type);
170b285192aSMauro Carvalho Chehab 	video_set_drvdata(vfd, dev);
171b285192aSMauro Carvalho Chehab 	return vfd;
172b285192aSMauro Carvalho Chehab }
173b285192aSMauro Carvalho Chehab 
1745bc08e19SLuis Alves int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
175b285192aSMauro Carvalho Chehab {
176b285192aSMauro Carvalho Chehab 	/* 8 bit registers, 8 bit values */
177b285192aSMauro Carvalho Chehab 	u8 buf[] = { reg, data };
178b285192aSMauro Carvalho Chehab 
179b285192aSMauro Carvalho Chehab 	struct i2c_msg msg = { .addr = 0x98 >> 1,
180b285192aSMauro Carvalho Chehab 		.flags = 0, .buf = buf, .len = 2 };
181b285192aSMauro Carvalho Chehab 
182b285192aSMauro Carvalho Chehab 	return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
183b285192aSMauro Carvalho Chehab }
184b285192aSMauro Carvalho Chehab 
1855bc08e19SLuis Alves u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
186b285192aSMauro Carvalho Chehab {
187b285192aSMauro Carvalho Chehab 	/* 8 bit registers, 8 bit values */
188b285192aSMauro Carvalho Chehab 	int ret;
189b285192aSMauro Carvalho Chehab 	u8 b0[] = { reg };
190b285192aSMauro Carvalho Chehab 	u8 b1[] = { 0 };
191b285192aSMauro Carvalho Chehab 
192b285192aSMauro Carvalho Chehab 	struct i2c_msg msg[] = {
193b285192aSMauro Carvalho Chehab 		{ .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
194b285192aSMauro Carvalho Chehab 		{ .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
195b285192aSMauro Carvalho Chehab 	};
196b285192aSMauro Carvalho Chehab 
197b285192aSMauro Carvalho Chehab 	ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
198b285192aSMauro Carvalho Chehab 	if (ret != 2)
199*e39682b5SMauro Carvalho Chehab 		pr_err("%s() error\n", __func__);
200b285192aSMauro Carvalho Chehab 
201b285192aSMauro Carvalho Chehab 	return b1[0];
202b285192aSMauro Carvalho Chehab }
203b285192aSMauro Carvalho Chehab 
204b285192aSMauro Carvalho Chehab static void cx23885_flatiron_dump(struct cx23885_dev *dev)
205b285192aSMauro Carvalho Chehab {
206b285192aSMauro Carvalho Chehab 	int i;
207b285192aSMauro Carvalho Chehab 	dprintk(1, "Flatiron dump\n");
208b285192aSMauro Carvalho Chehab 	for (i = 0; i < 0x24; i++) {
209b285192aSMauro Carvalho Chehab 		dprintk(1, "FI[%02x] = %02x\n", i,
210b285192aSMauro Carvalho Chehab 			cx23885_flatiron_read(dev, i));
211b285192aSMauro Carvalho Chehab 	}
212b285192aSMauro Carvalho Chehab }
213b285192aSMauro Carvalho Chehab 
214b285192aSMauro Carvalho Chehab static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
215b285192aSMauro Carvalho Chehab {
216b285192aSMauro Carvalho Chehab 	u8 val;
217b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(input = %d)\n", __func__, input);
218b285192aSMauro Carvalho Chehab 
219b285192aSMauro Carvalho Chehab 	if (input == 1)
220b285192aSMauro Carvalho Chehab 		val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
221b285192aSMauro Carvalho Chehab 	else if (input == 2)
222b285192aSMauro Carvalho Chehab 		val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
223b285192aSMauro Carvalho Chehab 	else
224b285192aSMauro Carvalho Chehab 		return -EINVAL;
225b285192aSMauro Carvalho Chehab 
226b285192aSMauro Carvalho Chehab 	val |= 0x20; /* Enable clock to delta-sigma and dec filter */
227b285192aSMauro Carvalho Chehab 
228b285192aSMauro Carvalho Chehab 	cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
229b285192aSMauro Carvalho Chehab 
230b285192aSMauro Carvalho Chehab 	/* Wake up */
231b285192aSMauro Carvalho Chehab 	cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
232b285192aSMauro Carvalho Chehab 
233b285192aSMauro Carvalho Chehab 	if (video_debug)
234b285192aSMauro Carvalho Chehab 		cx23885_flatiron_dump(dev);
235b285192aSMauro Carvalho Chehab 
236b285192aSMauro Carvalho Chehab 	return 0;
237b285192aSMauro Carvalho Chehab }
238b285192aSMauro Carvalho Chehab 
239b285192aSMauro Carvalho Chehab static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
240b285192aSMauro Carvalho Chehab {
241b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
242b285192aSMauro Carvalho Chehab 		__func__,
243b285192aSMauro Carvalho Chehab 		input, INPUT(input)->vmux,
244b285192aSMauro Carvalho Chehab 		INPUT(input)->gpio0, INPUT(input)->gpio1,
245b285192aSMauro Carvalho Chehab 		INPUT(input)->gpio2, INPUT(input)->gpio3);
246b285192aSMauro Carvalho Chehab 	dev->input = input;
247b285192aSMauro Carvalho Chehab 
248b285192aSMauro Carvalho Chehab 	if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
249b285192aSMauro Carvalho Chehab 		dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
250b285192aSMauro Carvalho Chehab 		dev->board == CX23885_BOARD_MYGICA_X8507) {
251b285192aSMauro Carvalho Chehab 		/* Select Analog TV */
252b285192aSMauro Carvalho Chehab 		if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
253b285192aSMauro Carvalho Chehab 			cx23885_gpio_clear(dev, GPIO_0);
254b285192aSMauro Carvalho Chehab 	}
255b285192aSMauro Carvalho Chehab 
256b285192aSMauro Carvalho Chehab 	/* Tell the internal A/V decoder */
257b285192aSMauro Carvalho Chehab 	v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
258b285192aSMauro Carvalho Chehab 			INPUT(input)->vmux, 0, 0);
259b285192aSMauro Carvalho Chehab 
260b285192aSMauro Carvalho Chehab 	if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
261b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_MPX885) ||
262b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
263cce11b09SHans Verkuil 		(dev->board == CX23885_BOARD_HAUPPAUGE_IMPACTVCBE) ||
264b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
265b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
266082c0576SAlfredo Jesús Delaiti 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
267e8d42373SOleh Kravchenko 		(dev->board == CX23885_BOARD_MYGICA_X8507) ||
2686c43a217SHans Verkuil 		(dev->board == CX23885_BOARD_AVERMEDIA_HC81R) ||
2696c43a217SHans Verkuil 		(dev->board == CX23885_BOARD_VIEWCAST_260E) ||
2706c43a217SHans Verkuil 		(dev->board == CX23885_BOARD_VIEWCAST_460E)) {
271b285192aSMauro Carvalho Chehab 		/* Configure audio routing */
272b285192aSMauro Carvalho Chehab 		v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
273b285192aSMauro Carvalho Chehab 			INPUT(input)->amux, 0, 0);
274b285192aSMauro Carvalho Chehab 
275b285192aSMauro Carvalho Chehab 		if (INPUT(input)->amux == CX25840_AUDIO7)
276b285192aSMauro Carvalho Chehab 			cx23885_flatiron_mux(dev, 1);
277b285192aSMauro Carvalho Chehab 		else if (INPUT(input)->amux == CX25840_AUDIO6)
278b285192aSMauro Carvalho Chehab 			cx23885_flatiron_mux(dev, 2);
279b285192aSMauro Carvalho Chehab 	}
280b285192aSMauro Carvalho Chehab 
281b285192aSMauro Carvalho Chehab 	return 0;
282b285192aSMauro Carvalho Chehab }
283b285192aSMauro Carvalho Chehab 
284b285192aSMauro Carvalho Chehab static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
285b285192aSMauro Carvalho Chehab {
286b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(input=%d)\n", __func__, input);
287b285192aSMauro Carvalho Chehab 
288b285192aSMauro Carvalho Chehab 	/* The baseband video core of the cx23885 has two audio inputs.
289b285192aSMauro Carvalho Chehab 	 * LR1 and LR2. In almost every single case so far only HVR1xxx
290b285192aSMauro Carvalho Chehab 	 * cards we've only ever supported LR1. Time to support LR2,
291b285192aSMauro Carvalho Chehab 	 * which is available via the optional white breakout header on
292b285192aSMauro Carvalho Chehab 	 * the board.
293b285192aSMauro Carvalho Chehab 	 * We'll use a could of existing enums in the card struct to allow
294b285192aSMauro Carvalho Chehab 	 * devs to specify which baseband input they need, or just default
295b285192aSMauro Carvalho Chehab 	 * to what we've always used.
296b285192aSMauro Carvalho Chehab 	 */
297b285192aSMauro Carvalho Chehab 	if (INPUT(input)->amux == CX25840_AUDIO7)
298b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 1);
299b285192aSMauro Carvalho Chehab 	else if (INPUT(input)->amux == CX25840_AUDIO6)
300b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 2);
301b285192aSMauro Carvalho Chehab 	else {
302b285192aSMauro Carvalho Chehab 		/* Not specifically defined, assume the default. */
303b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 1);
304b285192aSMauro Carvalho Chehab 	}
305b285192aSMauro Carvalho Chehab 
306b285192aSMauro Carvalho Chehab 	return 0;
307b285192aSMauro Carvalho Chehab }
308b285192aSMauro Carvalho Chehab 
309b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
310b285192aSMauro Carvalho Chehab static int cx23885_start_video_dma(struct cx23885_dev *dev,
311b285192aSMauro Carvalho Chehab 			   struct cx23885_dmaqueue *q,
312b285192aSMauro Carvalho Chehab 			   struct cx23885_buffer *buf)
313b285192aSMauro Carvalho Chehab {
314b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
315b285192aSMauro Carvalho Chehab 
316b285192aSMauro Carvalho Chehab 	/* Stop the dma/fifo before we tamper with it's risc programs */
317b285192aSMauro Carvalho Chehab 	cx_clear(VID_A_DMA_CTL, 0x11);
318b285192aSMauro Carvalho Chehab 
319b285192aSMauro Carvalho Chehab 	/* setup fifo + format */
320b285192aSMauro Carvalho Chehab 	cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
321b285192aSMauro Carvalho Chehab 				buf->bpl, buf->risc.dma);
322b285192aSMauro Carvalho Chehab 
323b285192aSMauro Carvalho Chehab 	/* reset counter */
324b285192aSMauro Carvalho Chehab 	cx_write(VID_A_GPCNT_CTL, 3);
325453afdd9SHans Verkuil 	q->count = 0;
326b285192aSMauro Carvalho Chehab 
327b285192aSMauro Carvalho Chehab 	/* enable irq */
328b285192aSMauro Carvalho Chehab 	cx23885_irq_add_enable(dev, 0x01);
329b285192aSMauro Carvalho Chehab 	cx_set(VID_A_INT_MSK, 0x000011);
330b285192aSMauro Carvalho Chehab 
331b285192aSMauro Carvalho Chehab 	/* start dma */
332b285192aSMauro Carvalho Chehab 	cx_set(DEV_CNTRL2, (1<<5));
333b285192aSMauro Carvalho Chehab 	cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
334b285192aSMauro Carvalho Chehab 
335b285192aSMauro Carvalho Chehab 	return 0;
336b285192aSMauro Carvalho Chehab }
337b285192aSMauro Carvalho Chehab 
338df9ecb0cSHans Verkuil static int queue_setup(struct vb2_queue *q,
339453afdd9SHans Verkuil 			   unsigned int *num_buffers, unsigned int *num_planes,
34036c0f8b3SHans Verkuil 			   unsigned int sizes[], struct device *alloc_devs[])
341b285192aSMauro Carvalho Chehab {
342453afdd9SHans Verkuil 	struct cx23885_dev *dev = q->drv_priv;
343b285192aSMauro Carvalho Chehab 
344453afdd9SHans Verkuil 	*num_planes = 1;
345453afdd9SHans Verkuil 	sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
346b285192aSMauro Carvalho Chehab 	return 0;
347b285192aSMauro Carvalho Chehab }
348b285192aSMauro Carvalho Chehab 
349453afdd9SHans Verkuil static int buffer_prepare(struct vb2_buffer *vb)
350b285192aSMauro Carvalho Chehab {
3512d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
352453afdd9SHans Verkuil 	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
353b285192aSMauro Carvalho Chehab 	struct cx23885_buffer *buf =
3542d700715SJunghak Sung 		container_of(vbuf, struct cx23885_buffer, vb);
355b285192aSMauro Carvalho Chehab 	u32 line0_offset, line1_offset;
356453afdd9SHans Verkuil 	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
357b285192aSMauro Carvalho Chehab 	int field_tff;
358b285192aSMauro Carvalho Chehab 
359453afdd9SHans Verkuil 	buf->bpl = (dev->width * dev->fmt->depth) >> 3;
360453afdd9SHans Verkuil 
361453afdd9SHans Verkuil 	if (vb2_plane_size(vb, 0) < dev->height * buf->bpl)
362b285192aSMauro Carvalho Chehab 		return -EINVAL;
363453afdd9SHans Verkuil 	vb2_set_plane_payload(vb, 0, dev->height * buf->bpl);
36491d2d674SHans Verkuil 
365453afdd9SHans Verkuil 	switch (dev->field) {
366b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_TOP:
367b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
368453afdd9SHans Verkuil 				sgt->sgl, 0, UNSET,
369453afdd9SHans Verkuil 				buf->bpl, 0, dev->height);
370b285192aSMauro Carvalho Chehab 		break;
371b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_BOTTOM:
372b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
373453afdd9SHans Verkuil 				sgt->sgl, UNSET, 0,
374453afdd9SHans Verkuil 				buf->bpl, 0, dev->height);
375b285192aSMauro Carvalho Chehab 		break;
376b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_INTERLACED:
37796f233e9SHans Verkuil 		if (dev->tvnorm & V4L2_STD_525_60)
378b285192aSMauro Carvalho Chehab 			/* NTSC or  */
379b285192aSMauro Carvalho Chehab 			field_tff = 1;
380b285192aSMauro Carvalho Chehab 		else
381b285192aSMauro Carvalho Chehab 			field_tff = 0;
382b285192aSMauro Carvalho Chehab 
383b285192aSMauro Carvalho Chehab 		if (cx23885_boards[dev->board].force_bff)
384b285192aSMauro Carvalho Chehab 			/* PAL / SECAM OR 888 in NTSC MODE */
385b285192aSMauro Carvalho Chehab 			field_tff = 0;
386b285192aSMauro Carvalho Chehab 
387b285192aSMauro Carvalho Chehab 		if (field_tff) {
388b285192aSMauro Carvalho Chehab 			/* cx25840 transmits NTSC bottom field first */
389b285192aSMauro Carvalho Chehab 			dprintk(1, "%s() Creating TFF/NTSC risc\n",
390b285192aSMauro Carvalho Chehab 					__func__);
391b285192aSMauro Carvalho Chehab 			line0_offset = buf->bpl;
392b285192aSMauro Carvalho Chehab 			line1_offset = 0;
393b285192aSMauro Carvalho Chehab 		} else {
394b285192aSMauro Carvalho Chehab 			/* All other formats are top field first */
395b285192aSMauro Carvalho Chehab 			dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
396b285192aSMauro Carvalho Chehab 					__func__);
397b285192aSMauro Carvalho Chehab 			line0_offset = 0;
398b285192aSMauro Carvalho Chehab 			line1_offset = buf->bpl;
399b285192aSMauro Carvalho Chehab 		}
400b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
401453afdd9SHans Verkuil 				sgt->sgl, line0_offset,
402b285192aSMauro Carvalho Chehab 				line1_offset,
403b285192aSMauro Carvalho Chehab 				buf->bpl, buf->bpl,
404453afdd9SHans Verkuil 				dev->height >> 1);
405b285192aSMauro Carvalho Chehab 		break;
406b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_TB:
407b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
408453afdd9SHans Verkuil 				sgt->sgl,
409453afdd9SHans Verkuil 				0, buf->bpl * (dev->height >> 1),
410b285192aSMauro Carvalho Chehab 				buf->bpl, 0,
411453afdd9SHans Verkuil 				dev->height >> 1);
412b285192aSMauro Carvalho Chehab 		break;
413b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_BT:
414b285192aSMauro Carvalho Chehab 		cx23885_risc_buffer(dev->pci, &buf->risc,
415453afdd9SHans Verkuil 				sgt->sgl,
416453afdd9SHans Verkuil 				buf->bpl * (dev->height >> 1), 0,
417b285192aSMauro Carvalho Chehab 				buf->bpl, 0,
418453afdd9SHans Verkuil 				dev->height >> 1);
419b285192aSMauro Carvalho Chehab 		break;
420b285192aSMauro Carvalho Chehab 	default:
421b285192aSMauro Carvalho Chehab 		BUG();
422b285192aSMauro Carvalho Chehab 	}
423453afdd9SHans Verkuil 	dprintk(2, "[%p/%d] buffer_init - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
4242d700715SJunghak Sung 		buf, buf->vb.vb2_buf.index,
42591d2d674SHans Verkuil 		dev->width, dev->height, dev->fmt->depth, dev->fmt->name,
426b285192aSMauro Carvalho Chehab 		(unsigned long)buf->risc.dma);
427b285192aSMauro Carvalho Chehab 	return 0;
428b285192aSMauro Carvalho Chehab }
429b285192aSMauro Carvalho Chehab 
430453afdd9SHans Verkuil static void buffer_finish(struct vb2_buffer *vb)
431b285192aSMauro Carvalho Chehab {
4322d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
4332d700715SJunghak Sung 	struct cx23885_buffer *buf = container_of(vbuf,
434453afdd9SHans Verkuil 		struct cx23885_buffer, vb);
435453afdd9SHans Verkuil 
436453afdd9SHans Verkuil 	cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
437453afdd9SHans Verkuil }
438453afdd9SHans Verkuil 
439453afdd9SHans Verkuil /*
440453afdd9SHans Verkuil  * The risc program for each buffer works as follows: it starts with a simple
441453afdd9SHans Verkuil  * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
442453afdd9SHans Verkuil  * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
443453afdd9SHans Verkuil  * the initial JUMP).
444453afdd9SHans Verkuil  *
445453afdd9SHans Verkuil  * This is the risc program of the first buffer to be queued if the active list
446453afdd9SHans Verkuil  * is empty and it just keeps DMAing this buffer without generating any
447453afdd9SHans Verkuil  * interrupts.
448453afdd9SHans Verkuil  *
449453afdd9SHans Verkuil  * If a new buffer is added then the initial JUMP in the code for that buffer
450453afdd9SHans Verkuil  * will generate an interrupt which signals that the previous buffer has been
451453afdd9SHans Verkuil  * DMAed successfully and that it can be returned to userspace.
452453afdd9SHans Verkuil  *
453453afdd9SHans Verkuil  * It also sets the final jump of the previous buffer to the start of the new
454453afdd9SHans Verkuil  * buffer, thus chaining the new buffer into the DMA chain. This is a single
455453afdd9SHans Verkuil  * atomic u32 write, so there is no race condition.
456453afdd9SHans Verkuil  *
457453afdd9SHans Verkuil  * The end-result of all this that you only get an interrupt when a buffer
458453afdd9SHans Verkuil  * is ready, so the control flow is very easy.
459453afdd9SHans Verkuil  */
460453afdd9SHans Verkuil static void buffer_queue(struct vb2_buffer *vb)
461453afdd9SHans Verkuil {
4622d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
463453afdd9SHans Verkuil 	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
4642d700715SJunghak Sung 	struct cx23885_buffer   *buf = container_of(vbuf,
465b285192aSMauro Carvalho Chehab 		struct cx23885_buffer, vb);
466b285192aSMauro Carvalho Chehab 	struct cx23885_buffer   *prev;
467b285192aSMauro Carvalho Chehab 	struct cx23885_dmaqueue *q    = &dev->vidq;
468453afdd9SHans Verkuil 	unsigned long flags;
469b285192aSMauro Carvalho Chehab 
470453afdd9SHans Verkuil 	/* add jump to start */
471453afdd9SHans Verkuil 	buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
472453afdd9SHans Verkuil 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
473453afdd9SHans Verkuil 	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
474b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
475b285192aSMauro Carvalho Chehab 
476453afdd9SHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
477453afdd9SHans Verkuil 	if (list_empty(&q->active)) {
478453afdd9SHans Verkuil 		list_add_tail(&buf->queue, &q->active);
479b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - first active\n",
4802d700715SJunghak Sung 			buf, buf->vb.vb2_buf.index);
481b285192aSMauro Carvalho Chehab 	} else {
482453afdd9SHans Verkuil 		buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
483b285192aSMauro Carvalho Chehab 		prev = list_entry(q->active.prev, struct cx23885_buffer,
484453afdd9SHans Verkuil 			queue);
485453afdd9SHans Verkuil 		list_add_tail(&buf->queue, &q->active);
486b285192aSMauro Carvalho Chehab 		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
487b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - append to active\n",
4882d700715SJunghak Sung 				buf, buf->vb.vb2_buf.index);
489b285192aSMauro Carvalho Chehab 	}
490453afdd9SHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
491b285192aSMauro Carvalho Chehab }
492b285192aSMauro Carvalho Chehab 
493453afdd9SHans Verkuil static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
494b285192aSMauro Carvalho Chehab {
495453afdd9SHans Verkuil 	struct cx23885_dev *dev = q->drv_priv;
496453afdd9SHans Verkuil 	struct cx23885_dmaqueue *dmaq = &dev->vidq;
497453afdd9SHans Verkuil 	struct cx23885_buffer *buf = list_entry(dmaq->active.next,
498453afdd9SHans Verkuil 			struct cx23885_buffer, queue);
499b285192aSMauro Carvalho Chehab 
500453afdd9SHans Verkuil 	cx23885_start_video_dma(dev, dmaq, buf);
501453afdd9SHans Verkuil 	return 0;
502b285192aSMauro Carvalho Chehab }
503b285192aSMauro Carvalho Chehab 
504453afdd9SHans Verkuil static void cx23885_stop_streaming(struct vb2_queue *q)
505453afdd9SHans Verkuil {
506453afdd9SHans Verkuil 	struct cx23885_dev *dev = q->drv_priv;
507453afdd9SHans Verkuil 	struct cx23885_dmaqueue *dmaq = &dev->vidq;
508453afdd9SHans Verkuil 	unsigned long flags;
509453afdd9SHans Verkuil 
510453afdd9SHans Verkuil 	cx_clear(VID_A_DMA_CTL, 0x11);
511453afdd9SHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
512453afdd9SHans Verkuil 	while (!list_empty(&dmaq->active)) {
513453afdd9SHans Verkuil 		struct cx23885_buffer *buf = list_entry(dmaq->active.next,
514453afdd9SHans Verkuil 			struct cx23885_buffer, queue);
515453afdd9SHans Verkuil 
516453afdd9SHans Verkuil 		list_del(&buf->queue);
5172d700715SJunghak Sung 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
518453afdd9SHans Verkuil 	}
519453afdd9SHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
520453afdd9SHans Verkuil }
521453afdd9SHans Verkuil 
52210accd2eSJulia Lawall static const struct vb2_ops cx23885_video_qops = {
523453afdd9SHans Verkuil 	.queue_setup    = queue_setup,
524b285192aSMauro Carvalho Chehab 	.buf_prepare  = buffer_prepare,
525453afdd9SHans Verkuil 	.buf_finish = buffer_finish,
526b285192aSMauro Carvalho Chehab 	.buf_queue    = buffer_queue,
527453afdd9SHans Verkuil 	.wait_prepare = vb2_ops_wait_prepare,
528453afdd9SHans Verkuil 	.wait_finish = vb2_ops_wait_finish,
529453afdd9SHans Verkuil 	.start_streaming = cx23885_start_streaming,
530453afdd9SHans Verkuil 	.stop_streaming = cx23885_stop_streaming,
531b285192aSMauro Carvalho Chehab };
532b285192aSMauro Carvalho Chehab 
533b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
534b285192aSMauro Carvalho Chehab /* VIDEO IOCTLS                                                       */
535b285192aSMauro Carvalho Chehab 
536b285192aSMauro Carvalho Chehab static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
537b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
538b285192aSMauro Carvalho Chehab {
539568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
540b285192aSMauro Carvalho Chehab 
54191d2d674SHans Verkuil 	f->fmt.pix.width        = dev->width;
54291d2d674SHans Verkuil 	f->fmt.pix.height       = dev->height;
543453afdd9SHans Verkuil 	f->fmt.pix.field        = dev->field;
54491d2d674SHans Verkuil 	f->fmt.pix.pixelformat  = dev->fmt->fourcc;
545b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline =
54691d2d674SHans Verkuil 		(f->fmt.pix.width * dev->fmt->depth) >> 3;
547b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage =
548b285192aSMauro Carvalho Chehab 		f->fmt.pix.height * f->fmt.pix.bytesperline;
5491af2ddd8SHans Verkuil 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
550b285192aSMauro Carvalho Chehab 
551b285192aSMauro Carvalho Chehab 	return 0;
552b285192aSMauro Carvalho Chehab }
553b285192aSMauro Carvalho Chehab 
554b285192aSMauro Carvalho Chehab static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
555b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
556b285192aSMauro Carvalho Chehab {
557568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
558b285192aSMauro Carvalho Chehab 	struct cx23885_fmt *fmt;
559b285192aSMauro Carvalho Chehab 	enum v4l2_field   field;
560b285192aSMauro Carvalho Chehab 	unsigned int      maxw, maxh;
561b285192aSMauro Carvalho Chehab 
562b285192aSMauro Carvalho Chehab 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
563b285192aSMauro Carvalho Chehab 	if (NULL == fmt)
564b285192aSMauro Carvalho Chehab 		return -EINVAL;
565b285192aSMauro Carvalho Chehab 
566b285192aSMauro Carvalho Chehab 	field = f->fmt.pix.field;
567e80bf856SHans Verkuil 	maxw  = 720;
568b285192aSMauro Carvalho Chehab 	maxh  = norm_maxh(dev->tvnorm);
569b285192aSMauro Carvalho Chehab 
570b285192aSMauro Carvalho Chehab 	if (V4L2_FIELD_ANY == field) {
571b285192aSMauro Carvalho Chehab 		field = (f->fmt.pix.height > maxh/2)
572b285192aSMauro Carvalho Chehab 			? V4L2_FIELD_INTERLACED
573b285192aSMauro Carvalho Chehab 			: V4L2_FIELD_BOTTOM;
574b285192aSMauro Carvalho Chehab 	}
575b285192aSMauro Carvalho Chehab 
576b285192aSMauro Carvalho Chehab 	switch (field) {
577b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_TOP:
578b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_BOTTOM:
579b285192aSMauro Carvalho Chehab 		maxh = maxh / 2;
580b285192aSMauro Carvalho Chehab 		break;
581b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_INTERLACED:
58296f233e9SHans Verkuil 	case V4L2_FIELD_SEQ_TB:
58396f233e9SHans Verkuil 	case V4L2_FIELD_SEQ_BT:
584b285192aSMauro Carvalho Chehab 		break;
585b285192aSMauro Carvalho Chehab 	default:
586200e0841SHans Verkuil 		field = V4L2_FIELD_INTERLACED;
587200e0841SHans Verkuil 		break;
588b285192aSMauro Carvalho Chehab 	}
589b285192aSMauro Carvalho Chehab 
590b285192aSMauro Carvalho Chehab 	f->fmt.pix.field = field;
591b285192aSMauro Carvalho Chehab 	v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
592b285192aSMauro Carvalho Chehab 			      &f->fmt.pix.height, 32, maxh, 0, 0);
593b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline =
594b285192aSMauro Carvalho Chehab 		(f->fmt.pix.width * fmt->depth) >> 3;
595b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage =
596b285192aSMauro Carvalho Chehab 		f->fmt.pix.height * f->fmt.pix.bytesperline;
5971af2ddd8SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
598b285192aSMauro Carvalho Chehab 
599b285192aSMauro Carvalho Chehab 	return 0;
600b285192aSMauro Carvalho Chehab }
601b285192aSMauro Carvalho Chehab 
602b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
603b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
604b285192aSMauro Carvalho Chehab {
605568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
606ebf984bbSHans Verkuil 	struct v4l2_subdev_format format = {
607ebf984bbSHans Verkuil 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
608ebf984bbSHans Verkuil 	};
609b285192aSMauro Carvalho Chehab 	int err;
610b285192aSMauro Carvalho Chehab 
611b285192aSMauro Carvalho Chehab 	dprintk(2, "%s()\n", __func__);
612b285192aSMauro Carvalho Chehab 	err = vidioc_try_fmt_vid_cap(file, priv, f);
613b285192aSMauro Carvalho Chehab 
614b285192aSMauro Carvalho Chehab 	if (0 != err)
615b285192aSMauro Carvalho Chehab 		return err;
616f1b6a735SHans Verkuil 
617f1b6a735SHans Verkuil 	if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
618f1b6a735SHans Verkuil 	    vb2_is_busy(&dev->vb2_mpegq))
619f1b6a735SHans Verkuil 		return -EBUSY;
620f1b6a735SHans Verkuil 
62191d2d674SHans Verkuil 	dev->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
62291d2d674SHans Verkuil 	dev->width      = f->fmt.pix.width;
62391d2d674SHans Verkuil 	dev->height     = f->fmt.pix.height;
624453afdd9SHans Verkuil 	dev->field	= f->fmt.pix.field;
625b285192aSMauro Carvalho Chehab 	dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
626453afdd9SHans Verkuil 		dev->width, dev->height, dev->field);
627ebf984bbSHans Verkuil 	v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
628ebf984bbSHans Verkuil 	call_all(dev, pad, set_fmt, NULL, &format);
629ebf984bbSHans Verkuil 	v4l2_fill_pix_format(&f->fmt.pix, &format.format);
630ebf984bbSHans Verkuil 	/* set_fmt overwrites f->fmt.pix.field, restore it */
63196f233e9SHans Verkuil 	f->fmt.pix.field = dev->field;
632b285192aSMauro Carvalho Chehab 	return 0;
633b285192aSMauro Carvalho Chehab }
634b285192aSMauro Carvalho Chehab 
635b285192aSMauro Carvalho Chehab static int vidioc_querycap(struct file *file, void  *priv,
636b285192aSMauro Carvalho Chehab 	struct v4l2_capability *cap)
637b285192aSMauro Carvalho Chehab {
638568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
639a7d3eabdSHans Verkuil 	struct video_device *vdev = video_devdata(file);
640b285192aSMauro Carvalho Chehab 
641b285192aSMauro Carvalho Chehab 	strcpy(cap->driver, "cx23885");
642b285192aSMauro Carvalho Chehab 	strlcpy(cap->card, cx23885_boards[dev->board].name,
643b285192aSMauro Carvalho Chehab 		sizeof(cap->card));
644b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
645d43be757SHans Verkuil 	cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | V4L2_CAP_AUDIO;
6460f3bf3dcSHans Verkuil 	if (dev->tuner_type != TUNER_ABSENT)
647a7d3eabdSHans Verkuil 		cap->device_caps |= V4L2_CAP_TUNER;
648a7d3eabdSHans Verkuil 	if (vdev->vfl_type == VFL_TYPE_VBI)
649a7d3eabdSHans Verkuil 		cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
650a7d3eabdSHans Verkuil 	else
651a7d3eabdSHans Verkuil 		cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
652a7d3eabdSHans Verkuil 	cap->capabilities = cap->device_caps | V4L2_CAP_VBI_CAPTURE |
653a7d3eabdSHans Verkuil 		V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_DEVICE_CAPS;
654b285192aSMauro Carvalho Chehab 	return 0;
655b285192aSMauro Carvalho Chehab }
656b285192aSMauro Carvalho Chehab 
657b285192aSMauro Carvalho Chehab static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
658b285192aSMauro Carvalho Chehab 	struct v4l2_fmtdesc *f)
659b285192aSMauro Carvalho Chehab {
660b285192aSMauro Carvalho Chehab 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
661b285192aSMauro Carvalho Chehab 		return -EINVAL;
662b285192aSMauro Carvalho Chehab 
663b285192aSMauro Carvalho Chehab 	strlcpy(f->description, formats[f->index].name,
664b285192aSMauro Carvalho Chehab 		sizeof(f->description));
665b285192aSMauro Carvalho Chehab 	f->pixelformat = formats[f->index].fourcc;
666b285192aSMauro Carvalho Chehab 
667b285192aSMauro Carvalho Chehab 	return 0;
668b285192aSMauro Carvalho Chehab }
669b285192aSMauro Carvalho Chehab 
670e80bf856SHans Verkuil static int vidioc_cropcap(struct file *file, void *priv,
671e80bf856SHans Verkuil 			  struct v4l2_cropcap *cc)
672e80bf856SHans Verkuil {
673e80bf856SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
674e80bf856SHans Verkuil 	bool is_50hz = dev->tvnorm & V4L2_STD_625_50;
675e80bf856SHans Verkuil 
676e80bf856SHans Verkuil 	if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
677e80bf856SHans Verkuil 		return -EINVAL;
678e80bf856SHans Verkuil 
679e80bf856SHans Verkuil 	cc->bounds.left = 0;
680e80bf856SHans Verkuil 	cc->bounds.top = 0;
681e80bf856SHans Verkuil 	cc->bounds.width = 720;
682e80bf856SHans Verkuil 	cc->bounds.height = norm_maxh(dev->tvnorm);
683e80bf856SHans Verkuil 	cc->defrect = cc->bounds;
684e80bf856SHans Verkuil 	cc->pixelaspect.numerator = is_50hz ? 54 : 11;
685e80bf856SHans Verkuil 	cc->pixelaspect.denominator = is_50hz ? 59 : 10;
686e80bf856SHans Verkuil 
687e80bf856SHans Verkuil 	return 0;
688e80bf856SHans Verkuil }
689e80bf856SHans Verkuil 
690b285192aSMauro Carvalho Chehab static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
691b285192aSMauro Carvalho Chehab {
692568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
693b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
694b285192aSMauro Carvalho Chehab 
6959c1f5df8SHans Verkuil 	*id = dev->tvnorm;
696b285192aSMauro Carvalho Chehab 	return 0;
697b285192aSMauro Carvalho Chehab }
698b285192aSMauro Carvalho Chehab 
699314527acSHans Verkuil static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
700b285192aSMauro Carvalho Chehab {
701568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
702b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
703b285192aSMauro Carvalho Chehab 
704f1b6a735SHans Verkuil 	return cx23885_set_tvnorm(dev, tvnorms);
705b285192aSMauro Carvalho Chehab }
706b285192aSMauro Carvalho Chehab 
707b285192aSMauro Carvalho Chehab int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
708b285192aSMauro Carvalho Chehab {
709b285192aSMauro Carvalho Chehab 	static const char *iname[] = {
710b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE1] = "Composite1",
711b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE2] = "Composite2",
712b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE3] = "Composite3",
713b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE4] = "Composite4",
714b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_SVIDEO]     = "S-Video",
715b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPONENT]  = "Component",
716b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_TELEVISION] = "Television",
717b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_CABLE]      = "Cable TV",
718b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_DVB]        = "DVB",
719b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_DEBUG]      = "for debug only",
720b285192aSMauro Carvalho Chehab 	};
721b285192aSMauro Carvalho Chehab 	unsigned int n;
722b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
723b285192aSMauro Carvalho Chehab 
724b285192aSMauro Carvalho Chehab 	n = i->index;
725b285192aSMauro Carvalho Chehab 	if (n >= MAX_CX23885_INPUT)
726b285192aSMauro Carvalho Chehab 		return -EINVAL;
727b285192aSMauro Carvalho Chehab 
728b285192aSMauro Carvalho Chehab 	if (0 == INPUT(n)->type)
729b285192aSMauro Carvalho Chehab 		return -EINVAL;
730b285192aSMauro Carvalho Chehab 
731b285192aSMauro Carvalho Chehab 	i->index = n;
732b285192aSMauro Carvalho Chehab 	i->type  = V4L2_INPUT_TYPE_CAMERA;
733b285192aSMauro Carvalho Chehab 	strcpy(i->name, iname[INPUT(n)->type]);
734d43be757SHans Verkuil 	i->std = CX23885_NORMS;
735b285192aSMauro Carvalho Chehab 	if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
736b285192aSMauro Carvalho Chehab 		(CX23885_VMUX_CABLE == INPUT(n)->type)) {
737b285192aSMauro Carvalho Chehab 		i->type = V4L2_INPUT_TYPE_TUNER;
738d43be757SHans Verkuil 		i->audioset = 4;
739d43be757SHans Verkuil 	} else {
740b285192aSMauro Carvalho Chehab 		/* Two selectable audio inputs for non-tv inputs */
741d43be757SHans Verkuil 		i->audioset = 3;
742d43be757SHans Verkuil 	}
743b285192aSMauro Carvalho Chehab 
744b285192aSMauro Carvalho Chehab 	if (dev->input == n) {
745b285192aSMauro Carvalho Chehab 		/* enum'd input matches our configured input.
746b285192aSMauro Carvalho Chehab 		 * Ask the video decoder to process the call
747b285192aSMauro Carvalho Chehab 		 * and give it an oppertunity to update the
748b285192aSMauro Carvalho Chehab 		 * status field.
749b285192aSMauro Carvalho Chehab 		 */
750b285192aSMauro Carvalho Chehab 		call_all(dev, video, g_input_status, &i->status);
751b285192aSMauro Carvalho Chehab 	}
752b285192aSMauro Carvalho Chehab 
753b285192aSMauro Carvalho Chehab 	return 0;
754b285192aSMauro Carvalho Chehab }
755b285192aSMauro Carvalho Chehab 
756b285192aSMauro Carvalho Chehab static int vidioc_enum_input(struct file *file, void *priv,
757b285192aSMauro Carvalho Chehab 				struct v4l2_input *i)
758b285192aSMauro Carvalho Chehab {
759568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
760b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
761b285192aSMauro Carvalho Chehab 	return cx23885_enum_input(dev, i);
762b285192aSMauro Carvalho Chehab }
763b285192aSMauro Carvalho Chehab 
764b285192aSMauro Carvalho Chehab int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
765b285192aSMauro Carvalho Chehab {
766568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
767b285192aSMauro Carvalho Chehab 
768b285192aSMauro Carvalho Chehab 	*i = dev->input;
769b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() returns %d\n", __func__, *i);
770b285192aSMauro Carvalho Chehab 	return 0;
771b285192aSMauro Carvalho Chehab }
772b285192aSMauro Carvalho Chehab 
773b285192aSMauro Carvalho Chehab static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
774b285192aSMauro Carvalho Chehab {
775b285192aSMauro Carvalho Chehab 	return cx23885_get_input(file, priv, i);
776b285192aSMauro Carvalho Chehab }
777b285192aSMauro Carvalho Chehab 
778b285192aSMauro Carvalho Chehab int cx23885_set_input(struct file *file, void *priv, unsigned int i)
779b285192aSMauro Carvalho Chehab {
780568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
781b285192aSMauro Carvalho Chehab 
782b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(%d)\n", __func__, i);
783b285192aSMauro Carvalho Chehab 
784b285192aSMauro Carvalho Chehab 	if (i >= MAX_CX23885_INPUT) {
785b285192aSMauro Carvalho Chehab 		dprintk(1, "%s() -EINVAL\n", __func__);
786b285192aSMauro Carvalho Chehab 		return -EINVAL;
787b285192aSMauro Carvalho Chehab 	}
788b285192aSMauro Carvalho Chehab 
789b285192aSMauro Carvalho Chehab 	if (INPUT(i)->type == 0)
790b285192aSMauro Carvalho Chehab 		return -EINVAL;
791b285192aSMauro Carvalho Chehab 
792b285192aSMauro Carvalho Chehab 	cx23885_video_mux(dev, i);
793b285192aSMauro Carvalho Chehab 
794b285192aSMauro Carvalho Chehab 	/* By default establish the default audio input for the card also */
795b285192aSMauro Carvalho Chehab 	/* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
796b285192aSMauro Carvalho Chehab 	cx23885_audio_mux(dev, i);
797b285192aSMauro Carvalho Chehab 	return 0;
798b285192aSMauro Carvalho Chehab }
799b285192aSMauro Carvalho Chehab 
800b285192aSMauro Carvalho Chehab static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
801b285192aSMauro Carvalho Chehab {
802b285192aSMauro Carvalho Chehab 	return cx23885_set_input(file, priv, i);
803b285192aSMauro Carvalho Chehab }
804b285192aSMauro Carvalho Chehab 
805b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
806b285192aSMauro Carvalho Chehab {
807568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
808b285192aSMauro Carvalho Chehab 
809b285192aSMauro Carvalho Chehab 	call_all(dev, core, log_status);
810b285192aSMauro Carvalho Chehab 	return 0;
811b285192aSMauro Carvalho Chehab }
812b285192aSMauro Carvalho Chehab 
813b285192aSMauro Carvalho Chehab static int cx23885_query_audinput(struct file *file, void *priv,
814b285192aSMauro Carvalho Chehab 	struct v4l2_audio *i)
815b285192aSMauro Carvalho Chehab {
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
1004*e39682b5SMauro Carvalho Chehab 		pr_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);
1062*e39682b5SMauro Carvalho Chehab 			pr_warn("%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)
106907ab29e1SMauro Carvalho Chehab 			dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) video lines miss-match\n",
1070b285192aSMauro Carvalho Chehab 				VID_BC_MSK_SYNC);
1071b285192aSMauro Carvalho Chehab 
1072b285192aSMauro Carvalho Chehab 		if (status & VID_BC_MSK_OF)
1073b285192aSMauro Carvalho Chehab 			dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1074b285192aSMauro Carvalho Chehab 				VID_BC_MSK_OF);
1075b285192aSMauro Carvalho Chehab 
1076b285192aSMauro Carvalho Chehab 	}
1077b285192aSMauro Carvalho Chehab 
1078b285192aSMauro Carvalho Chehab 	/* Video */
1079b285192aSMauro Carvalho Chehab 	if (status & VID_BC_MSK_RISCI1) {
1080b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
1081b285192aSMauro Carvalho Chehab 		count = cx_read(VID_A_GPCNT);
1082b285192aSMauro Carvalho Chehab 		cx23885_video_wakeup(dev, &dev->vidq, count);
1083b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
1084b285192aSMauro Carvalho Chehab 		handled++;
1085b285192aSMauro Carvalho Chehab 	}
1086b285192aSMauro Carvalho Chehab 
1087b285192aSMauro Carvalho Chehab 	/* Allow the VBI framework to process it's payload */
1088b285192aSMauro Carvalho Chehab 	handled += cx23885_vbi_irq(dev, status);
1089b285192aSMauro Carvalho Chehab 
1090b285192aSMauro Carvalho Chehab 	return handled;
1091b285192aSMauro Carvalho Chehab }
1092b285192aSMauro Carvalho Chehab 
1093b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------- */
1094b285192aSMauro Carvalho Chehab /* exported stuff                                              */
1095b285192aSMauro Carvalho Chehab 
1096b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops = {
1097b285192aSMauro Carvalho Chehab 	.owner	       = THIS_MODULE,
1098453afdd9SHans Verkuil 	.open           = v4l2_fh_open,
1099453afdd9SHans Verkuil 	.release        = vb2_fop_release,
1100453afdd9SHans Verkuil 	.read           = vb2_fop_read,
1101453afdd9SHans Verkuil 	.poll		= vb2_fop_poll,
110224a8f7b5SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
1103453afdd9SHans Verkuil 	.mmap           = vb2_fop_mmap,
1104b285192aSMauro Carvalho Chehab };
1105b285192aSMauro Carvalho Chehab 
1106b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops = {
1107b285192aSMauro Carvalho Chehab 	.vidioc_querycap      = vidioc_querycap,
1108b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1109b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1110b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1111b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1112b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vbi_cap     = cx23885_vbi_fmt,
1113b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vbi_cap   = cx23885_vbi_fmt,
1114b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vbi_cap     = cx23885_vbi_fmt,
1115453afdd9SHans Verkuil 	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
1116453afdd9SHans Verkuil 	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
1117453afdd9SHans Verkuil 	.vidioc_querybuf      = vb2_ioctl_querybuf,
1118453afdd9SHans Verkuil 	.vidioc_qbuf          = vb2_ioctl_qbuf,
1119453afdd9SHans Verkuil 	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
1120453afdd9SHans Verkuil 	.vidioc_streamon      = vb2_ioctl_streamon,
1121453afdd9SHans Verkuil 	.vidioc_streamoff     = vb2_ioctl_streamoff,
1122e80bf856SHans Verkuil 	.vidioc_cropcap       = vidioc_cropcap,
1123b285192aSMauro Carvalho Chehab 	.vidioc_s_std         = vidioc_s_std,
1124b285192aSMauro Carvalho Chehab 	.vidioc_g_std         = vidioc_g_std,
1125b285192aSMauro Carvalho Chehab 	.vidioc_enum_input    = vidioc_enum_input,
1126b285192aSMauro Carvalho Chehab 	.vidioc_g_input       = vidioc_g_input,
1127b285192aSMauro Carvalho Chehab 	.vidioc_s_input       = vidioc_s_input,
1128b285192aSMauro Carvalho Chehab 	.vidioc_log_status    = vidioc_log_status,
1129b285192aSMauro Carvalho Chehab 	.vidioc_g_tuner       = vidioc_g_tuner,
1130b285192aSMauro Carvalho Chehab 	.vidioc_s_tuner       = vidioc_s_tuner,
1131b285192aSMauro Carvalho Chehab 	.vidioc_g_frequency   = vidioc_g_frequency,
1132b285192aSMauro Carvalho Chehab 	.vidioc_s_frequency   = vidioc_s_frequency,
1133b285192aSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
113480f8568fSHans Verkuil 	.vidioc_g_chip_info   = cx23885_g_chip_info,
1135b285192aSMauro Carvalho Chehab 	.vidioc_g_register    = cx23885_g_register,
1136b285192aSMauro Carvalho Chehab 	.vidioc_s_register    = cx23885_s_register,
1137b285192aSMauro Carvalho Chehab #endif
1138b285192aSMauro Carvalho Chehab 	.vidioc_enumaudio     = vidioc_enum_audinput,
1139b285192aSMauro Carvalho Chehab 	.vidioc_g_audio       = vidioc_g_audinput,
1140b285192aSMauro Carvalho Chehab 	.vidioc_s_audio       = vidioc_s_audinput,
1141da59a4deSHans Verkuil 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1142da59a4deSHans Verkuil 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1143b285192aSMauro Carvalho Chehab };
1144b285192aSMauro Carvalho Chehab 
1145b285192aSMauro Carvalho Chehab static struct video_device cx23885_vbi_template;
1146b285192aSMauro Carvalho Chehab static struct video_device cx23885_video_template = {
1147b285192aSMauro Carvalho Chehab 	.name                 = "cx23885-video",
1148b285192aSMauro Carvalho Chehab 	.fops                 = &video_fops,
1149b285192aSMauro Carvalho Chehab 	.ioctl_ops 	      = &video_ioctl_ops,
1150b285192aSMauro Carvalho Chehab 	.tvnorms              = CX23885_NORMS,
1151b285192aSMauro Carvalho Chehab };
1152b285192aSMauro Carvalho Chehab 
1153b285192aSMauro Carvalho Chehab void cx23885_video_unregister(struct cx23885_dev *dev)
1154b285192aSMauro Carvalho Chehab {
1155b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1156b285192aSMauro Carvalho Chehab 	cx23885_irq_remove(dev, 0x01);
1157b285192aSMauro Carvalho Chehab 
1158b285192aSMauro Carvalho Chehab 	if (dev->vbi_dev) {
1159b285192aSMauro Carvalho Chehab 		if (video_is_registered(dev->vbi_dev))
1160b285192aSMauro Carvalho Chehab 			video_unregister_device(dev->vbi_dev);
1161b285192aSMauro Carvalho Chehab 		else
1162b285192aSMauro Carvalho Chehab 			video_device_release(dev->vbi_dev);
1163b285192aSMauro Carvalho Chehab 		dev->vbi_dev = NULL;
1164b285192aSMauro Carvalho Chehab 	}
1165b285192aSMauro Carvalho Chehab 	if (dev->video_dev) {
1166b285192aSMauro Carvalho Chehab 		if (video_is_registered(dev->video_dev))
1167b285192aSMauro Carvalho Chehab 			video_unregister_device(dev->video_dev);
1168b285192aSMauro Carvalho Chehab 		else
1169b285192aSMauro Carvalho Chehab 			video_device_release(dev->video_dev);
1170b285192aSMauro Carvalho Chehab 		dev->video_dev = NULL;
1171b285192aSMauro Carvalho Chehab 	}
1172b285192aSMauro Carvalho Chehab 
1173b285192aSMauro Carvalho Chehab 	if (dev->audio_dev)
1174b285192aSMauro Carvalho Chehab 		cx23885_audio_unregister(dev);
1175b285192aSMauro Carvalho Chehab }
1176b285192aSMauro Carvalho Chehab 
1177b285192aSMauro Carvalho Chehab int cx23885_video_register(struct cx23885_dev *dev)
1178b285192aSMauro Carvalho Chehab {
1179453afdd9SHans Verkuil 	struct vb2_queue *q;
1180b285192aSMauro Carvalho Chehab 	int err;
1181b285192aSMauro Carvalho Chehab 
1182b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1183b285192aSMauro Carvalho Chehab 
1184b285192aSMauro Carvalho Chehab 	/* Initialize VBI template */
11853618acabSEzequiel Garcia 	cx23885_vbi_template = cx23885_video_template;
1186b285192aSMauro Carvalho Chehab 	strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1187b285192aSMauro Carvalho Chehab 
11889c1f5df8SHans Verkuil 	dev->tvnorm = V4L2_STD_NTSC_M;
118991d2d674SHans Verkuil 	dev->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
119096f233e9SHans Verkuil 	dev->field = V4L2_FIELD_INTERLACED;
11911c5eaa23SHans Verkuil 	dev->width = 720;
119291d2d674SHans Verkuil 	dev->height = norm_maxh(dev->tvnorm);
1193b285192aSMauro Carvalho Chehab 
1194b285192aSMauro Carvalho Chehab 	/* init video dma queues */
1195b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->vidq.active);
1196b285192aSMauro Carvalho Chehab 
1197b285192aSMauro Carvalho Chehab 	/* init vbi dma queues */
1198b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->vbiq.active);
1199b285192aSMauro Carvalho Chehab 
1200b285192aSMauro Carvalho Chehab 	cx23885_irq_add_enable(dev, 0x01);
1201b285192aSMauro Carvalho Chehab 
1202b285192aSMauro Carvalho Chehab 	if ((TUNER_ABSENT != dev->tuner_type) &&
1203b285192aSMauro Carvalho Chehab 			((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1204b285192aSMauro Carvalho Chehab 		struct v4l2_subdev *sd = NULL;
1205b285192aSMauro Carvalho Chehab 
1206b285192aSMauro Carvalho Chehab 		if (dev->tuner_addr)
1207b285192aSMauro Carvalho Chehab 			sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1208b285192aSMauro Carvalho Chehab 				&dev->i2c_bus[dev->tuner_bus].i2c_adap,
1209b285192aSMauro Carvalho Chehab 				"tuner", dev->tuner_addr, NULL);
1210b285192aSMauro Carvalho Chehab 		else
1211b285192aSMauro Carvalho Chehab 			sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1212b285192aSMauro Carvalho Chehab 				&dev->i2c_bus[dev->tuner_bus].i2c_adap,
1213b285192aSMauro Carvalho Chehab 				"tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1214b285192aSMauro Carvalho Chehab 		if (sd) {
1215b285192aSMauro Carvalho Chehab 			struct tuner_setup tun_setup;
1216b285192aSMauro Carvalho Chehab 
1217b285192aSMauro Carvalho Chehab 			memset(&tun_setup, 0, sizeof(tun_setup));
1218b285192aSMauro Carvalho Chehab 			tun_setup.mode_mask = T_ANALOG_TV;
1219b285192aSMauro Carvalho Chehab 			tun_setup.type = dev->tuner_type;
1220b285192aSMauro Carvalho Chehab 			tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1221b285192aSMauro Carvalho Chehab 			tun_setup.tuner_callback = cx23885_tuner_callback;
1222b285192aSMauro Carvalho Chehab 
1223b285192aSMauro Carvalho Chehab 			v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1224b285192aSMauro Carvalho Chehab 
1225642ca1a0SAnca Emanuel 			if ((dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) ||
1226642ca1a0SAnca Emanuel 			    (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200)) {
1227b285192aSMauro Carvalho Chehab 				struct xc2028_ctrl ctrl = {
1228b285192aSMauro Carvalho Chehab 					.fname = XC2028_DEFAULT_FIRMWARE,
1229b285192aSMauro Carvalho Chehab 					.max_len = 64
1230b285192aSMauro Carvalho Chehab 				};
1231b285192aSMauro Carvalho Chehab 				struct v4l2_priv_tun_config cfg = {
1232b285192aSMauro Carvalho Chehab 					.tuner = dev->tuner_type,
1233b285192aSMauro Carvalho Chehab 					.priv = &ctrl
1234b285192aSMauro Carvalho Chehab 				};
1235b285192aSMauro Carvalho Chehab 				v4l2_subdev_call(sd, tuner, s_config, &cfg);
1236b285192aSMauro Carvalho Chehab 			}
1237e8d42373SOleh Kravchenko 
1238e8d42373SOleh Kravchenko 			if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
1239e8d42373SOleh Kravchenko 				struct xc2028_ctrl ctrl = {
1240e8d42373SOleh Kravchenko 					.fname = "xc3028L-v36.fw",
1241e8d42373SOleh Kravchenko 					.max_len = 64
1242e8d42373SOleh Kravchenko 				};
1243e8d42373SOleh Kravchenko 				struct v4l2_priv_tun_config cfg = {
1244e8d42373SOleh Kravchenko 					.tuner = dev->tuner_type,
1245e8d42373SOleh Kravchenko 					.priv = &ctrl
1246e8d42373SOleh Kravchenko 				};
1247e8d42373SOleh Kravchenko 				v4l2_subdev_call(sd, tuner, s_config, &cfg);
1248e8d42373SOleh Kravchenko 			}
1249b285192aSMauro Carvalho Chehab 		}
1250b285192aSMauro Carvalho Chehab 	}
1251b285192aSMauro Carvalho Chehab 
125224a8f7b5SHans Verkuil 	/* initial device configuration */
125324a8f7b5SHans Verkuil 	mutex_lock(&dev->lock);
125424a8f7b5SHans Verkuil 	cx23885_set_tvnorm(dev, dev->tvnorm);
125524a8f7b5SHans Verkuil 	cx23885_video_mux(dev, 0);
125624a8f7b5SHans Verkuil 	cx23885_audio_mux(dev, 0);
125724a8f7b5SHans Verkuil 	mutex_unlock(&dev->lock);
125824a8f7b5SHans Verkuil 
1259453afdd9SHans Verkuil 	q = &dev->vb2_vidq;
1260453afdd9SHans Verkuil 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1261453afdd9SHans Verkuil 	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1262453afdd9SHans Verkuil 	q->gfp_flags = GFP_DMA32;
1263453afdd9SHans Verkuil 	q->min_buffers_needed = 2;
1264453afdd9SHans Verkuil 	q->drv_priv = dev;
1265453afdd9SHans Verkuil 	q->buf_struct_size = sizeof(struct cx23885_buffer);
1266453afdd9SHans Verkuil 	q->ops = &cx23885_video_qops;
1267453afdd9SHans Verkuil 	q->mem_ops = &vb2_dma_sg_memops;
1268453afdd9SHans Verkuil 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1269453afdd9SHans Verkuil 	q->lock = &dev->lock;
12702bc46b3aSHans Verkuil 	q->dev = &dev->pci->dev;
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;
12872bc46b3aSHans Verkuil 	q->dev = &dev->pci->dev;
1288453afdd9SHans Verkuil 
1289453afdd9SHans Verkuil 	err = vb2_queue_init(q);
1290453afdd9SHans Verkuil 	if (err < 0)
1291453afdd9SHans Verkuil 		goto fail_unreg;
1292453afdd9SHans Verkuil 
1293b285192aSMauro Carvalho Chehab 	/* register Video device */
1294b285192aSMauro Carvalho Chehab 	dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1295b285192aSMauro Carvalho Chehab 		&cx23885_video_template, "video");
1296453afdd9SHans Verkuil 	dev->video_dev->queue = &dev->vb2_vidq;
1297b285192aSMauro Carvalho Chehab 	err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1298b285192aSMauro Carvalho Chehab 				    video_nr[dev->nr]);
1299b285192aSMauro Carvalho Chehab 	if (err < 0) {
1300*e39682b5SMauro Carvalho Chehab 		pr_info("%s: can't register video device\n",
1301b285192aSMauro Carvalho Chehab 			dev->name);
1302b285192aSMauro Carvalho Chehab 		goto fail_unreg;
1303b285192aSMauro Carvalho Chehab 	}
1304*e39682b5SMauro Carvalho Chehab 	pr_info("%s: registered device %s [v4l2]\n",
1305b285192aSMauro Carvalho Chehab 	       dev->name, video_device_node_name(dev->video_dev));
1306b285192aSMauro Carvalho Chehab 
1307b285192aSMauro Carvalho Chehab 	/* register VBI device */
1308b285192aSMauro Carvalho Chehab 	dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1309b285192aSMauro Carvalho Chehab 		&cx23885_vbi_template, "vbi");
1310453afdd9SHans Verkuil 	dev->vbi_dev->queue = &dev->vb2_vbiq;
1311b285192aSMauro Carvalho Chehab 	err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1312b285192aSMauro Carvalho Chehab 				    vbi_nr[dev->nr]);
1313b285192aSMauro Carvalho Chehab 	if (err < 0) {
1314*e39682b5SMauro Carvalho Chehab 		pr_info("%s: can't register vbi device\n",
1315b285192aSMauro Carvalho Chehab 			dev->name);
1316b285192aSMauro Carvalho Chehab 		goto fail_unreg;
1317b285192aSMauro Carvalho Chehab 	}
1318*e39682b5SMauro Carvalho Chehab 	pr_info("%s: registered device %s\n",
1319b285192aSMauro Carvalho Chehab 	       dev->name, video_device_node_name(dev->vbi_dev));
1320b285192aSMauro Carvalho Chehab 
1321b285192aSMauro Carvalho Chehab 	/* Register ALSA audio device */
1322b285192aSMauro Carvalho Chehab 	dev->audio_dev = cx23885_audio_register(dev);
1323b285192aSMauro Carvalho Chehab 
1324b285192aSMauro Carvalho Chehab 	return 0;
1325b285192aSMauro Carvalho Chehab 
1326b285192aSMauro Carvalho Chehab fail_unreg:
1327b285192aSMauro Carvalho Chehab 	cx23885_video_unregister(dev);
1328b285192aSMauro Carvalho Chehab 	return err;
1329b285192aSMauro Carvalho Chehab }
1330