xref: /linux/drivers/media/pci/cx23885/cx23885-video.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b285192aSMauro Carvalho Chehab /*
3b285192aSMauro Carvalho Chehab  *  Driver for the Conexant CX23885 PCIe bridge
4b285192aSMauro Carvalho Chehab  *
5b285192aSMauro Carvalho Chehab  *  Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
6b285192aSMauro Carvalho Chehab  */
7b285192aSMauro Carvalho Chehab 
8e39682b5SMauro Carvalho Chehab #include "cx23885.h"
9e39682b5SMauro Carvalho Chehab #include "cx23885-video.h"
10e39682b5SMauro Carvalho Chehab 
11b285192aSMauro Carvalho Chehab #include <linux/init.h>
12b285192aSMauro Carvalho Chehab #include <linux/list.h>
13b285192aSMauro Carvalho Chehab #include <linux/module.h>
14b285192aSMauro Carvalho Chehab #include <linux/moduleparam.h>
15b285192aSMauro Carvalho Chehab #include <linux/kmod.h>
16b285192aSMauro Carvalho Chehab #include <linux/kernel.h>
17b285192aSMauro Carvalho Chehab #include <linux/slab.h>
18b285192aSMauro Carvalho Chehab #include <linux/interrupt.h>
19b285192aSMauro Carvalho Chehab #include <linux/delay.h>
20b285192aSMauro Carvalho Chehab #include <linux/kthread.h>
21b285192aSMauro Carvalho Chehab #include <asm/div64.h>
22b285192aSMauro Carvalho Chehab 
23b285192aSMauro Carvalho Chehab #include <media/v4l2-common.h>
24b285192aSMauro Carvalho Chehab #include <media/v4l2-ioctl.h>
25da59a4deSHans Verkuil #include <media/v4l2-event.h>
26b285192aSMauro Carvalho Chehab #include "cx23885-ioctl.h"
27d76231e4SMauro Carvalho Chehab #include "xc2028.h"
28b285192aSMauro Carvalho Chehab 
29d647f0b7SMauro Carvalho Chehab #include <media/drv-intf/cx25840.h>
30b285192aSMauro Carvalho Chehab 
31b285192aSMauro Carvalho Chehab MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
32b285192aSMauro Carvalho Chehab MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
33b285192aSMauro Carvalho Chehab MODULE_LICENSE("GPL");
34b285192aSMauro Carvalho Chehab 
35b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
36b285192aSMauro Carvalho Chehab 
37b285192aSMauro Carvalho Chehab static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
38b285192aSMauro Carvalho Chehab static unsigned int vbi_nr[]   = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
39b285192aSMauro Carvalho Chehab 
40b285192aSMauro Carvalho Chehab module_param_array(video_nr, int, NULL, 0444);
41b285192aSMauro Carvalho Chehab module_param_array(vbi_nr,   int, NULL, 0444);
42b285192aSMauro Carvalho Chehab 
43b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_nr, "video device numbers");
44b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
45b285192aSMauro Carvalho Chehab 
46b285192aSMauro Carvalho Chehab static unsigned int video_debug;
47b285192aSMauro Carvalho Chehab module_param(video_debug, int, 0644);
48b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
49b285192aSMauro Carvalho Chehab 
50b285192aSMauro Carvalho Chehab static unsigned int irq_debug;
51b285192aSMauro Carvalho Chehab module_param(irq_debug, int, 0644);
52b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
53b285192aSMauro Carvalho Chehab 
54b285192aSMauro Carvalho Chehab static unsigned int vid_limit = 16;
55b285192aSMauro Carvalho Chehab module_param(vid_limit, int, 0644);
56b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
57b285192aSMauro Carvalho Chehab 
58b285192aSMauro Carvalho Chehab #define dprintk(level, fmt, arg...)\
59b285192aSMauro Carvalho Chehab 	do { if (video_debug >= level)\
60e39682b5SMauro Carvalho Chehab 		printk(KERN_DEBUG pr_fmt("%s: video:" fmt), \
61e39682b5SMauro Carvalho Chehab 			__func__, ##arg); \
62b285192aSMauro Carvalho Chehab 	} while (0)
63b285192aSMauro Carvalho Chehab 
64b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------- */
65b285192aSMauro Carvalho Chehab /* static data                                                         */
66b285192aSMauro Carvalho Chehab 
67b285192aSMauro Carvalho Chehab #define FORMAT_FLAGS_PACKED       0x01
68b285192aSMauro Carvalho Chehab static struct cx23885_fmt formats[] = {
69b285192aSMauro Carvalho Chehab 	{
70b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_YUYV,
71b285192aSMauro Carvalho Chehab 		.depth    = 16,
72b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
73b285192aSMauro Carvalho Chehab 	}
74b285192aSMauro Carvalho Chehab };
75b285192aSMauro Carvalho Chehab 
format_by_fourcc(unsigned int fourcc)76b285192aSMauro Carvalho Chehab static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
77b285192aSMauro Carvalho Chehab {
78b285192aSMauro Carvalho Chehab 	unsigned int i;
79b285192aSMauro Carvalho Chehab 
80b285192aSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(formats); i++)
81b285192aSMauro Carvalho Chehab 		if (formats[i].fourcc == fourcc)
82b285192aSMauro Carvalho Chehab 			return formats+i;
83b285192aSMauro Carvalho Chehab 	return NULL;
84b285192aSMauro Carvalho Chehab }
85b285192aSMauro Carvalho Chehab 
86b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------- */
87b285192aSMauro Carvalho Chehab 
cx23885_video_wakeup(struct cx23885_dev * dev,struct cx23885_dmaqueue * q,u32 count)88b285192aSMauro Carvalho Chehab void cx23885_video_wakeup(struct cx23885_dev *dev,
89b285192aSMauro Carvalho Chehab 	struct cx23885_dmaqueue *q, u32 count)
90b285192aSMauro Carvalho Chehab {
91b285192aSMauro Carvalho Chehab 	struct cx23885_buffer *buf;
92b285192aSMauro Carvalho Chehab 
93b285192aSMauro Carvalho Chehab 	if (list_empty(&q->active))
94453afdd9SHans Verkuil 		return;
95b285192aSMauro Carvalho Chehab 	buf = list_entry(q->active.next,
96453afdd9SHans Verkuil 			struct cx23885_buffer, queue);
97b285192aSMauro Carvalho Chehab 
982d700715SJunghak Sung 	buf->vb.sequence = q->count++;
99d6dd645eSJunghak Sung 	buf->vb.vb2_buf.timestamp = ktime_get_ns();
1002d700715SJunghak Sung 	dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf,
1012d700715SJunghak Sung 			buf->vb.vb2_buf.index, count, q->count);
102453afdd9SHans Verkuil 	list_del(&buf->queue);
1032d700715SJunghak Sung 	vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
104b285192aSMauro Carvalho Chehab }
105b285192aSMauro Carvalho Chehab 
cx23885_set_tvnorm(struct cx23885_dev * dev,v4l2_std_id norm)106b285192aSMauro Carvalho Chehab int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
107b285192aSMauro Carvalho Chehab {
108e80bf856SHans Verkuil 	struct v4l2_subdev_format format = {
109e80bf856SHans Verkuil 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
110e80bf856SHans Verkuil 		.format.code = MEDIA_BUS_FMT_FIXED,
111e80bf856SHans Verkuil 	};
112e80bf856SHans Verkuil 
113b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
114b285192aSMauro Carvalho Chehab 		__func__,
115b285192aSMauro Carvalho Chehab 		(unsigned int)norm,
116b285192aSMauro Carvalho Chehab 		v4l2_norm_to_name(norm));
117b285192aSMauro Carvalho Chehab 
118e80bf856SHans Verkuil 	if (dev->tvnorm == norm)
119e80bf856SHans Verkuil 		return 0;
120e80bf856SHans Verkuil 
121f1b6a735SHans Verkuil 	if (dev->tvnorm != norm) {
122f1b6a735SHans Verkuil 		if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
123f1b6a735SHans Verkuil 		    vb2_is_busy(&dev->vb2_mpegq))
124f1b6a735SHans Verkuil 			return -EBUSY;
125f1b6a735SHans Verkuil 	}
126f1b6a735SHans Verkuil 
127b285192aSMauro Carvalho Chehab 	dev->tvnorm = norm;
128e80bf856SHans Verkuil 	dev->width = 720;
129e80bf856SHans Verkuil 	dev->height = norm_maxh(norm);
130e80bf856SHans Verkuil 	dev->field = V4L2_FIELD_INTERLACED;
131b285192aSMauro Carvalho Chehab 
1328774bed9SLaurent Pinchart 	call_all(dev, video, s_std, norm);
133b285192aSMauro Carvalho Chehab 
134e80bf856SHans Verkuil 	format.format.width = dev->width;
135e80bf856SHans Verkuil 	format.format.height = dev->height;
136e80bf856SHans Verkuil 	format.format.field = dev->field;
137e80bf856SHans Verkuil 	call_all(dev, pad, set_fmt, NULL, &format);
138e80bf856SHans Verkuil 
139b285192aSMauro Carvalho Chehab 	return 0;
140b285192aSMauro Carvalho Chehab }
141b285192aSMauro Carvalho Chehab 
cx23885_vdev_init(struct cx23885_dev * dev,struct pci_dev * pci,struct video_device * template,char * type)142b285192aSMauro Carvalho Chehab static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
143b285192aSMauro Carvalho Chehab 				    struct pci_dev *pci,
144b285192aSMauro Carvalho Chehab 				    struct video_device *template,
145b285192aSMauro Carvalho Chehab 				    char *type)
146b285192aSMauro Carvalho Chehab {
147b285192aSMauro Carvalho Chehab 	struct video_device *vfd;
148b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
149b285192aSMauro Carvalho Chehab 
150b285192aSMauro Carvalho Chehab 	vfd = video_device_alloc();
151b285192aSMauro Carvalho Chehab 	if (NULL == vfd)
152b285192aSMauro Carvalho Chehab 		return NULL;
153b285192aSMauro Carvalho Chehab 	*vfd = *template;
154b285192aSMauro Carvalho Chehab 	vfd->v4l2_dev = &dev->v4l2_dev;
155b285192aSMauro Carvalho Chehab 	vfd->release = video_device_release;
15624a8f7b5SHans Verkuil 	vfd->lock = &dev->lock;
157b285192aSMauro Carvalho Chehab 	snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
158b285192aSMauro Carvalho Chehab 		 cx23885_boards[dev->board].name, type);
159b285192aSMauro Carvalho Chehab 	video_set_drvdata(vfd, dev);
160b285192aSMauro Carvalho Chehab 	return vfd;
161b285192aSMauro Carvalho Chehab }
162b285192aSMauro Carvalho Chehab 
cx23885_flatiron_write(struct cx23885_dev * dev,u8 reg,u8 data)1635bc08e19SLuis Alves int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
164b285192aSMauro Carvalho Chehab {
165b285192aSMauro Carvalho Chehab 	/* 8 bit registers, 8 bit values */
166b285192aSMauro Carvalho Chehab 	u8 buf[] = { reg, data };
167b285192aSMauro Carvalho Chehab 
168b285192aSMauro Carvalho Chehab 	struct i2c_msg msg = { .addr = 0x98 >> 1,
169b285192aSMauro Carvalho Chehab 		.flags = 0, .buf = buf, .len = 2 };
170b285192aSMauro Carvalho Chehab 
171b285192aSMauro Carvalho Chehab 	return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
172b285192aSMauro Carvalho Chehab }
173b285192aSMauro Carvalho Chehab 
cx23885_flatiron_read(struct cx23885_dev * dev,u8 reg)1745bc08e19SLuis Alves u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
175b285192aSMauro Carvalho Chehab {
176b285192aSMauro Carvalho Chehab 	/* 8 bit registers, 8 bit values */
177b285192aSMauro Carvalho Chehab 	int ret;
178b285192aSMauro Carvalho Chehab 	u8 b0[] = { reg };
179b285192aSMauro Carvalho Chehab 	u8 b1[] = { 0 };
180b285192aSMauro Carvalho Chehab 
181b285192aSMauro Carvalho Chehab 	struct i2c_msg msg[] = {
182b285192aSMauro Carvalho Chehab 		{ .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
183b285192aSMauro Carvalho Chehab 		{ .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
184b285192aSMauro Carvalho Chehab 	};
185b285192aSMauro Carvalho Chehab 
186b285192aSMauro Carvalho Chehab 	ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
187b285192aSMauro Carvalho Chehab 	if (ret != 2)
188e39682b5SMauro Carvalho Chehab 		pr_err("%s() error\n", __func__);
189b285192aSMauro Carvalho Chehab 
190b285192aSMauro Carvalho Chehab 	return b1[0];
191b285192aSMauro Carvalho Chehab }
192b285192aSMauro Carvalho Chehab 
cx23885_flatiron_dump(struct cx23885_dev * dev)193b285192aSMauro Carvalho Chehab static void cx23885_flatiron_dump(struct cx23885_dev *dev)
194b285192aSMauro Carvalho Chehab {
195b285192aSMauro Carvalho Chehab 	int i;
196b285192aSMauro Carvalho Chehab 	dprintk(1, "Flatiron dump\n");
197b285192aSMauro Carvalho Chehab 	for (i = 0; i < 0x24; i++) {
198b285192aSMauro Carvalho Chehab 		dprintk(1, "FI[%02x] = %02x\n", i,
199b285192aSMauro Carvalho Chehab 			cx23885_flatiron_read(dev, i));
200b285192aSMauro Carvalho Chehab 	}
201b285192aSMauro Carvalho Chehab }
202b285192aSMauro Carvalho Chehab 
cx23885_flatiron_mux(struct cx23885_dev * dev,int input)203b285192aSMauro Carvalho Chehab static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
204b285192aSMauro Carvalho Chehab {
205b285192aSMauro Carvalho Chehab 	u8 val;
206b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(input = %d)\n", __func__, input);
207b285192aSMauro Carvalho Chehab 
208b285192aSMauro Carvalho Chehab 	if (input == 1)
209b285192aSMauro Carvalho Chehab 		val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
210b285192aSMauro Carvalho Chehab 	else if (input == 2)
211b285192aSMauro Carvalho Chehab 		val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
212b285192aSMauro Carvalho Chehab 	else
213b285192aSMauro Carvalho Chehab 		return -EINVAL;
214b285192aSMauro Carvalho Chehab 
215b285192aSMauro Carvalho Chehab 	val |= 0x20; /* Enable clock to delta-sigma and dec filter */
216b285192aSMauro Carvalho Chehab 
217b285192aSMauro Carvalho Chehab 	cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
218b285192aSMauro Carvalho Chehab 
219b285192aSMauro Carvalho Chehab 	/* Wake up */
220b285192aSMauro Carvalho Chehab 	cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
221b285192aSMauro Carvalho Chehab 
222b285192aSMauro Carvalho Chehab 	if (video_debug)
223b285192aSMauro Carvalho Chehab 		cx23885_flatiron_dump(dev);
224b285192aSMauro Carvalho Chehab 
225b285192aSMauro Carvalho Chehab 	return 0;
226b285192aSMauro Carvalho Chehab }
227b285192aSMauro Carvalho Chehab 
cx23885_video_mux(struct cx23885_dev * dev,unsigned int input)228b285192aSMauro Carvalho Chehab static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
229b285192aSMauro Carvalho Chehab {
230b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
231b285192aSMauro Carvalho Chehab 		__func__,
232b285192aSMauro Carvalho Chehab 		input, INPUT(input)->vmux,
233b285192aSMauro Carvalho Chehab 		INPUT(input)->gpio0, INPUT(input)->gpio1,
234b285192aSMauro Carvalho Chehab 		INPUT(input)->gpio2, INPUT(input)->gpio3);
235b285192aSMauro Carvalho Chehab 	dev->input = input;
236b285192aSMauro Carvalho Chehab 
237b285192aSMauro Carvalho Chehab 	if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
238b285192aSMauro Carvalho Chehab 		dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
239b285192aSMauro Carvalho Chehab 		dev->board == CX23885_BOARD_MYGICA_X8507) {
240b285192aSMauro Carvalho Chehab 		/* Select Analog TV */
241b285192aSMauro Carvalho Chehab 		if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
242b285192aSMauro Carvalho Chehab 			cx23885_gpio_clear(dev, GPIO_0);
243b285192aSMauro Carvalho Chehab 	}
244b285192aSMauro Carvalho Chehab 
245b285192aSMauro Carvalho Chehab 	/* Tell the internal A/V decoder */
246b285192aSMauro Carvalho Chehab 	v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
247b285192aSMauro Carvalho Chehab 			INPUT(input)->vmux, 0, 0);
248b285192aSMauro Carvalho Chehab 
249b285192aSMauro Carvalho Chehab 	if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
250b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_MPX885) ||
251b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
252cce11b09SHans Verkuil 		(dev->board == CX23885_BOARD_HAUPPAUGE_IMPACTVCBE) ||
253b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
254b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
25594f11518SBrad Love 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1265_K4) ||
25681118817SBrad Love 		(dev->board == CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC) ||
25781118817SBrad Love 		(dev->board == CX23885_BOARD_HAUPPAUGE_QUADHD_DVB) ||
258082c0576SAlfredo Jesús Delaiti 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
2592be355a0SBrad Love 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR5525) ||
260e8d42373SOleh Kravchenko 		(dev->board == CX23885_BOARD_MYGICA_X8507) ||
2616c43a217SHans Verkuil 		(dev->board == CX23885_BOARD_AVERMEDIA_HC81R) ||
2626c43a217SHans Verkuil 		(dev->board == CX23885_BOARD_VIEWCAST_260E) ||
263dc4cac67SForest Crossman 		(dev->board == CX23885_BOARD_VIEWCAST_460E) ||
264dc4cac67SForest Crossman 		(dev->board == CX23885_BOARD_AVERMEDIA_CE310B)) {
265b285192aSMauro Carvalho Chehab 		/* Configure audio routing */
266b285192aSMauro Carvalho Chehab 		v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
267b285192aSMauro Carvalho Chehab 			INPUT(input)->amux, 0, 0);
268b285192aSMauro Carvalho Chehab 
269b285192aSMauro Carvalho Chehab 		if (INPUT(input)->amux == CX25840_AUDIO7)
270b285192aSMauro Carvalho Chehab 			cx23885_flatiron_mux(dev, 1);
271b285192aSMauro Carvalho Chehab 		else if (INPUT(input)->amux == CX25840_AUDIO6)
272b285192aSMauro Carvalho Chehab 			cx23885_flatiron_mux(dev, 2);
273b285192aSMauro Carvalho Chehab 	}
274b285192aSMauro Carvalho Chehab 
275b285192aSMauro Carvalho Chehab 	return 0;
276b285192aSMauro Carvalho Chehab }
277b285192aSMauro Carvalho Chehab 
cx23885_audio_mux(struct cx23885_dev * dev,unsigned int input)278b285192aSMauro Carvalho Chehab static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
279b285192aSMauro Carvalho Chehab {
280b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(input=%d)\n", __func__, input);
281b285192aSMauro Carvalho Chehab 
282b285192aSMauro Carvalho Chehab 	/* The baseband video core of the cx23885 has two audio inputs.
283b285192aSMauro Carvalho Chehab 	 * LR1 and LR2. In almost every single case so far only HVR1xxx
284b285192aSMauro Carvalho Chehab 	 * cards we've only ever supported LR1. Time to support LR2,
285b285192aSMauro Carvalho Chehab 	 * which is available via the optional white breakout header on
286b285192aSMauro Carvalho Chehab 	 * the board.
287b285192aSMauro Carvalho Chehab 	 * We'll use a could of existing enums in the card struct to allow
288b285192aSMauro Carvalho Chehab 	 * devs to specify which baseband input they need, or just default
289b285192aSMauro Carvalho Chehab 	 * to what we've always used.
290b285192aSMauro Carvalho Chehab 	 */
291b285192aSMauro Carvalho Chehab 	if (INPUT(input)->amux == CX25840_AUDIO7)
292b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 1);
293b285192aSMauro Carvalho Chehab 	else if (INPUT(input)->amux == CX25840_AUDIO6)
294b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 2);
295b285192aSMauro Carvalho Chehab 	else {
296b285192aSMauro Carvalho Chehab 		/* Not specifically defined, assume the default. */
297b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 1);
298b285192aSMauro Carvalho Chehab 	}
299b285192aSMauro Carvalho Chehab 
300b285192aSMauro Carvalho Chehab 	return 0;
301b285192aSMauro Carvalho Chehab }
302b285192aSMauro Carvalho Chehab 
303b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
cx23885_start_video_dma(struct cx23885_dev * dev,struct cx23885_dmaqueue * q,struct cx23885_buffer * buf)304b285192aSMauro Carvalho Chehab static int cx23885_start_video_dma(struct cx23885_dev *dev,
305b285192aSMauro Carvalho Chehab 			   struct cx23885_dmaqueue *q,
306b285192aSMauro Carvalho Chehab 			   struct cx23885_buffer *buf)
307b285192aSMauro Carvalho Chehab {
308b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
309b285192aSMauro Carvalho Chehab 
310b285192aSMauro Carvalho Chehab 	/* Stop the dma/fifo before we tamper with it's risc programs */
311b285192aSMauro Carvalho Chehab 	cx_clear(VID_A_DMA_CTL, 0x11);
312b285192aSMauro Carvalho Chehab 
313b285192aSMauro Carvalho Chehab 	/* setup fifo + format */
314b285192aSMauro Carvalho Chehab 	cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
315b285192aSMauro Carvalho Chehab 				buf->bpl, buf->risc.dma);
316b285192aSMauro Carvalho Chehab 
317b285192aSMauro Carvalho Chehab 	/* reset counter */
318b285192aSMauro Carvalho Chehab 	cx_write(VID_A_GPCNT_CTL, 3);
319453afdd9SHans Verkuil 	q->count = 0;
320b285192aSMauro Carvalho Chehab 
321b285192aSMauro Carvalho Chehab 	/* enable irq */
322b285192aSMauro Carvalho Chehab 	cx23885_irq_add_enable(dev, 0x01);
323b285192aSMauro Carvalho Chehab 	cx_set(VID_A_INT_MSK, 0x000011);
324b285192aSMauro Carvalho Chehab 
325b285192aSMauro Carvalho Chehab 	/* start dma */
326b285192aSMauro Carvalho Chehab 	cx_set(DEV_CNTRL2, (1<<5));
327b285192aSMauro Carvalho Chehab 	cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
328b285192aSMauro Carvalho Chehab 
329b285192aSMauro Carvalho Chehab 	return 0;
330b285192aSMauro Carvalho Chehab }
331b285192aSMauro Carvalho Chehab 
queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])332df9ecb0cSHans Verkuil static int queue_setup(struct vb2_queue *q,
333453afdd9SHans Verkuil 			   unsigned int *num_buffers, unsigned int *num_planes,
33436c0f8b3SHans Verkuil 			   unsigned int sizes[], struct device *alloc_devs[])
335b285192aSMauro Carvalho Chehab {
336453afdd9SHans Verkuil 	struct cx23885_dev *dev = q->drv_priv;
337b285192aSMauro Carvalho Chehab 
338453afdd9SHans Verkuil 	*num_planes = 1;
339453afdd9SHans Verkuil 	sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
340b285192aSMauro Carvalho Chehab 	return 0;
341b285192aSMauro Carvalho Chehab }
342b285192aSMauro Carvalho Chehab 
buffer_prepare(struct vb2_buffer * vb)343453afdd9SHans Verkuil static int buffer_prepare(struct vb2_buffer *vb)
344b285192aSMauro Carvalho Chehab {
34547e8b73bSharperchen 	int ret;
3462d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
347453afdd9SHans Verkuil 	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
348b285192aSMauro Carvalho Chehab 	struct cx23885_buffer *buf =
3492d700715SJunghak Sung 		container_of(vbuf, struct cx23885_buffer, vb);
350b285192aSMauro Carvalho Chehab 	u32 line0_offset, line1_offset;
351453afdd9SHans Verkuil 	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
352b285192aSMauro Carvalho Chehab 	int field_tff;
353b285192aSMauro Carvalho Chehab 
354453afdd9SHans Verkuil 	buf->bpl = (dev->width * dev->fmt->depth) >> 3;
355453afdd9SHans Verkuil 
356453afdd9SHans Verkuil 	if (vb2_plane_size(vb, 0) < dev->height * buf->bpl)
357b285192aSMauro Carvalho Chehab 		return -EINVAL;
358453afdd9SHans Verkuil 	vb2_set_plane_payload(vb, 0, dev->height * buf->bpl);
35991d2d674SHans Verkuil 
360453afdd9SHans Verkuil 	switch (dev->field) {
361b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_TOP:
36247e8b73bSharperchen 		ret = cx23885_risc_buffer(dev->pci, &buf->risc,
363453afdd9SHans Verkuil 				sgt->sgl, 0, UNSET,
364453afdd9SHans Verkuil 				buf->bpl, 0, dev->height);
365b285192aSMauro Carvalho Chehab 		break;
366b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_BOTTOM:
36747e8b73bSharperchen 		ret = cx23885_risc_buffer(dev->pci, &buf->risc,
368453afdd9SHans Verkuil 				sgt->sgl, UNSET, 0,
369453afdd9SHans Verkuil 				buf->bpl, 0, dev->height);
370b285192aSMauro Carvalho Chehab 		break;
371b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_INTERLACED:
37296f233e9SHans Verkuil 		if (dev->tvnorm & V4L2_STD_525_60)
373b285192aSMauro Carvalho Chehab 			/* NTSC or  */
374b285192aSMauro Carvalho Chehab 			field_tff = 1;
375b285192aSMauro Carvalho Chehab 		else
376b285192aSMauro Carvalho Chehab 			field_tff = 0;
377b285192aSMauro Carvalho Chehab 
378b285192aSMauro Carvalho Chehab 		if (cx23885_boards[dev->board].force_bff)
379b285192aSMauro Carvalho Chehab 			/* PAL / SECAM OR 888 in NTSC MODE */
380b285192aSMauro Carvalho Chehab 			field_tff = 0;
381b285192aSMauro Carvalho Chehab 
382b285192aSMauro Carvalho Chehab 		if (field_tff) {
383b285192aSMauro Carvalho Chehab 			/* cx25840 transmits NTSC bottom field first */
384b285192aSMauro Carvalho Chehab 			dprintk(1, "%s() Creating TFF/NTSC risc\n",
385b285192aSMauro Carvalho Chehab 					__func__);
386b285192aSMauro Carvalho Chehab 			line0_offset = buf->bpl;
387b285192aSMauro Carvalho Chehab 			line1_offset = 0;
388b285192aSMauro Carvalho Chehab 		} else {
389b285192aSMauro Carvalho Chehab 			/* All other formats are top field first */
390b285192aSMauro Carvalho Chehab 			dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
391b285192aSMauro Carvalho Chehab 					__func__);
392b285192aSMauro Carvalho Chehab 			line0_offset = 0;
393b285192aSMauro Carvalho Chehab 			line1_offset = buf->bpl;
394b285192aSMauro Carvalho Chehab 		}
39547e8b73bSharperchen 		ret = cx23885_risc_buffer(dev->pci, &buf->risc,
396453afdd9SHans Verkuil 				sgt->sgl, line0_offset,
397b285192aSMauro Carvalho Chehab 				line1_offset,
398b285192aSMauro Carvalho Chehab 				buf->bpl, buf->bpl,
399453afdd9SHans Verkuil 				dev->height >> 1);
400b285192aSMauro Carvalho Chehab 		break;
401b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_TB:
40247e8b73bSharperchen 		ret = cx23885_risc_buffer(dev->pci, &buf->risc,
403453afdd9SHans Verkuil 				sgt->sgl,
404453afdd9SHans Verkuil 				0, buf->bpl * (dev->height >> 1),
405b285192aSMauro Carvalho Chehab 				buf->bpl, 0,
406453afdd9SHans Verkuil 				dev->height >> 1);
407b285192aSMauro Carvalho Chehab 		break;
408b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_SEQ_BT:
40947e8b73bSharperchen 		ret = cx23885_risc_buffer(dev->pci, &buf->risc,
410453afdd9SHans Verkuil 				sgt->sgl,
411453afdd9SHans Verkuil 				buf->bpl * (dev->height >> 1), 0,
412b285192aSMauro Carvalho Chehab 				buf->bpl, 0,
413453afdd9SHans Verkuil 				dev->height >> 1);
414b285192aSMauro Carvalho Chehab 		break;
415b285192aSMauro Carvalho Chehab 	default:
4162e1796fdSHans Verkuil 		return -EINVAL; /* should not happen */
417b285192aSMauro Carvalho Chehab 	}
4182169e6daSHans Verkuil 	dprintk(2, "[%p/%d] buffer_init - %dx%d %dbpp 0x%08x - dma=0x%08lx\n",
4192d700715SJunghak Sung 		buf, buf->vb.vb2_buf.index,
4202169e6daSHans Verkuil 		dev->width, dev->height, dev->fmt->depth, dev->fmt->fourcc,
421b285192aSMauro Carvalho Chehab 		(unsigned long)buf->risc.dma);
42247e8b73bSharperchen 	return ret;
423b285192aSMauro Carvalho Chehab }
424b285192aSMauro Carvalho Chehab 
buffer_finish(struct vb2_buffer * vb)425453afdd9SHans Verkuil static void buffer_finish(struct vb2_buffer *vb)
426b285192aSMauro Carvalho Chehab {
4272d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
4282d700715SJunghak Sung 	struct cx23885_buffer *buf = container_of(vbuf,
429453afdd9SHans Verkuil 		struct cx23885_buffer, vb);
430453afdd9SHans Verkuil 
431453afdd9SHans Verkuil 	cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
432453afdd9SHans Verkuil }
433453afdd9SHans Verkuil 
434453afdd9SHans Verkuil /*
435453afdd9SHans Verkuil  * The risc program for each buffer works as follows: it starts with a simple
436453afdd9SHans Verkuil  * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
437453afdd9SHans Verkuil  * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
438453afdd9SHans Verkuil  * the initial JUMP).
439453afdd9SHans Verkuil  *
440453afdd9SHans Verkuil  * This is the risc program of the first buffer to be queued if the active list
441453afdd9SHans Verkuil  * is empty and it just keeps DMAing this buffer without generating any
442453afdd9SHans Verkuil  * interrupts.
443453afdd9SHans Verkuil  *
444453afdd9SHans Verkuil  * If a new buffer is added then the initial JUMP in the code for that buffer
445453afdd9SHans Verkuil  * will generate an interrupt which signals that the previous buffer has been
446453afdd9SHans Verkuil  * DMAed successfully and that it can be returned to userspace.
447453afdd9SHans Verkuil  *
448453afdd9SHans Verkuil  * It also sets the final jump of the previous buffer to the start of the new
449453afdd9SHans Verkuil  * buffer, thus chaining the new buffer into the DMA chain. This is a single
450453afdd9SHans Verkuil  * atomic u32 write, so there is no race condition.
451453afdd9SHans Verkuil  *
452453afdd9SHans Verkuil  * The end-result of all this that you only get an interrupt when a buffer
453453afdd9SHans Verkuil  * is ready, so the control flow is very easy.
454453afdd9SHans Verkuil  */
buffer_queue(struct vb2_buffer * vb)455453afdd9SHans Verkuil static void buffer_queue(struct vb2_buffer *vb)
456453afdd9SHans Verkuil {
4572d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
458453afdd9SHans Verkuil 	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
4592d700715SJunghak Sung 	struct cx23885_buffer   *buf = container_of(vbuf,
460b285192aSMauro Carvalho Chehab 		struct cx23885_buffer, vb);
461b285192aSMauro Carvalho Chehab 	struct cx23885_buffer   *prev;
462b285192aSMauro Carvalho Chehab 	struct cx23885_dmaqueue *q    = &dev->vidq;
463453afdd9SHans Verkuil 	unsigned long flags;
464b285192aSMauro Carvalho Chehab 
465453afdd9SHans Verkuil 	/* add jump to start */
466453afdd9SHans Verkuil 	buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
467453afdd9SHans Verkuil 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
468453afdd9SHans Verkuil 	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
469b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
470b285192aSMauro Carvalho Chehab 
471453afdd9SHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
472453afdd9SHans Verkuil 	if (list_empty(&q->active)) {
473453afdd9SHans Verkuil 		list_add_tail(&buf->queue, &q->active);
474b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - first active\n",
4752d700715SJunghak Sung 			buf, buf->vb.vb2_buf.index);
476b285192aSMauro Carvalho Chehab 	} else {
477453afdd9SHans Verkuil 		buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
478b285192aSMauro Carvalho Chehab 		prev = list_entry(q->active.prev, struct cx23885_buffer,
479453afdd9SHans Verkuil 			queue);
480453afdd9SHans Verkuil 		list_add_tail(&buf->queue, &q->active);
481b285192aSMauro Carvalho Chehab 		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
482b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - append to active\n",
4832d700715SJunghak Sung 				buf, buf->vb.vb2_buf.index);
484b285192aSMauro Carvalho Chehab 	}
485453afdd9SHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
486b285192aSMauro Carvalho Chehab }
487b285192aSMauro Carvalho Chehab 
cx23885_start_streaming(struct vb2_queue * q,unsigned int count)488453afdd9SHans Verkuil static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
489b285192aSMauro Carvalho Chehab {
490453afdd9SHans Verkuil 	struct cx23885_dev *dev = q->drv_priv;
491453afdd9SHans Verkuil 	struct cx23885_dmaqueue *dmaq = &dev->vidq;
492453afdd9SHans Verkuil 	struct cx23885_buffer *buf = list_entry(dmaq->active.next,
493453afdd9SHans Verkuil 			struct cx23885_buffer, queue);
494b285192aSMauro Carvalho Chehab 
495453afdd9SHans Verkuil 	cx23885_start_video_dma(dev, dmaq, buf);
496453afdd9SHans Verkuil 	return 0;
497b285192aSMauro Carvalho Chehab }
498b285192aSMauro Carvalho Chehab 
cx23885_stop_streaming(struct vb2_queue * q)499453afdd9SHans Verkuil static void cx23885_stop_streaming(struct vb2_queue *q)
500453afdd9SHans Verkuil {
501453afdd9SHans Verkuil 	struct cx23885_dev *dev = q->drv_priv;
502453afdd9SHans Verkuil 	struct cx23885_dmaqueue *dmaq = &dev->vidq;
503453afdd9SHans Verkuil 	unsigned long flags;
504453afdd9SHans Verkuil 
505453afdd9SHans Verkuil 	cx_clear(VID_A_DMA_CTL, 0x11);
506453afdd9SHans Verkuil 	spin_lock_irqsave(&dev->slock, flags);
507453afdd9SHans Verkuil 	while (!list_empty(&dmaq->active)) {
508453afdd9SHans Verkuil 		struct cx23885_buffer *buf = list_entry(dmaq->active.next,
509453afdd9SHans Verkuil 			struct cx23885_buffer, queue);
510453afdd9SHans Verkuil 
511453afdd9SHans Verkuil 		list_del(&buf->queue);
5122d700715SJunghak Sung 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
513453afdd9SHans Verkuil 	}
514453afdd9SHans Verkuil 	spin_unlock_irqrestore(&dev->slock, flags);
515453afdd9SHans Verkuil }
516453afdd9SHans Verkuil 
51710accd2eSJulia Lawall static const struct vb2_ops cx23885_video_qops = {
518453afdd9SHans Verkuil 	.queue_setup    = queue_setup,
519b285192aSMauro Carvalho Chehab 	.buf_prepare  = buffer_prepare,
520453afdd9SHans Verkuil 	.buf_finish = buffer_finish,
521b285192aSMauro Carvalho Chehab 	.buf_queue    = buffer_queue,
522453afdd9SHans Verkuil 	.wait_prepare = vb2_ops_wait_prepare,
523453afdd9SHans Verkuil 	.wait_finish = vb2_ops_wait_finish,
524453afdd9SHans Verkuil 	.start_streaming = cx23885_start_streaming,
525453afdd9SHans Verkuil 	.stop_streaming = cx23885_stop_streaming,
526b285192aSMauro Carvalho Chehab };
527b285192aSMauro Carvalho Chehab 
528b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
529b285192aSMauro Carvalho Chehab /* VIDEO IOCTLS                                                       */
530b285192aSMauro Carvalho Chehab 
vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)531b285192aSMauro Carvalho Chehab static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
532b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
533b285192aSMauro Carvalho Chehab {
534568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
535b285192aSMauro Carvalho Chehab 
53691d2d674SHans Verkuil 	f->fmt.pix.width        = dev->width;
53791d2d674SHans Verkuil 	f->fmt.pix.height       = dev->height;
538453afdd9SHans Verkuil 	f->fmt.pix.field        = dev->field;
53991d2d674SHans Verkuil 	f->fmt.pix.pixelformat  = dev->fmt->fourcc;
540b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline =
54191d2d674SHans Verkuil 		(f->fmt.pix.width * dev->fmt->depth) >> 3;
542b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage =
543b285192aSMauro Carvalho Chehab 		f->fmt.pix.height * f->fmt.pix.bytesperline;
5441af2ddd8SHans Verkuil 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
545b285192aSMauro Carvalho Chehab 
546b285192aSMauro Carvalho Chehab 	return 0;
547b285192aSMauro Carvalho Chehab }
548b285192aSMauro Carvalho Chehab 
vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)549b285192aSMauro Carvalho Chehab static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
550b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
551b285192aSMauro Carvalho Chehab {
552568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
553b285192aSMauro Carvalho Chehab 	struct cx23885_fmt *fmt;
554b285192aSMauro Carvalho Chehab 	enum v4l2_field   field;
555b285192aSMauro Carvalho Chehab 	unsigned int      maxw, maxh;
556b285192aSMauro Carvalho Chehab 
557b285192aSMauro Carvalho Chehab 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
558b285192aSMauro Carvalho Chehab 	if (NULL == fmt)
559b285192aSMauro Carvalho Chehab 		return -EINVAL;
560b285192aSMauro Carvalho Chehab 
561b285192aSMauro Carvalho Chehab 	field = f->fmt.pix.field;
562e80bf856SHans Verkuil 	maxw  = 720;
563b285192aSMauro Carvalho Chehab 	maxh  = norm_maxh(dev->tvnorm);
564b285192aSMauro Carvalho Chehab 
565b285192aSMauro Carvalho Chehab 	if (V4L2_FIELD_ANY == field) {
566b285192aSMauro Carvalho Chehab 		field = (f->fmt.pix.height > maxh/2)
567b285192aSMauro Carvalho Chehab 			? V4L2_FIELD_INTERLACED
568b285192aSMauro Carvalho Chehab 			: V4L2_FIELD_BOTTOM;
569b285192aSMauro Carvalho Chehab 	}
570b285192aSMauro Carvalho Chehab 
571b285192aSMauro Carvalho Chehab 	switch (field) {
572b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_TOP:
573b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_BOTTOM:
574b285192aSMauro Carvalho Chehab 		maxh = maxh / 2;
575b285192aSMauro Carvalho Chehab 		break;
576b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_INTERLACED:
57796f233e9SHans Verkuil 	case V4L2_FIELD_SEQ_TB:
57896f233e9SHans Verkuil 	case V4L2_FIELD_SEQ_BT:
579b285192aSMauro Carvalho Chehab 		break;
580b285192aSMauro Carvalho Chehab 	default:
581200e0841SHans Verkuil 		field = V4L2_FIELD_INTERLACED;
582200e0841SHans Verkuil 		break;
583b285192aSMauro Carvalho Chehab 	}
584b285192aSMauro Carvalho Chehab 
585b285192aSMauro Carvalho Chehab 	f->fmt.pix.field = field;
586b285192aSMauro Carvalho Chehab 	v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
587b285192aSMauro Carvalho Chehab 			      &f->fmt.pix.height, 32, maxh, 0, 0);
588b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline =
589b285192aSMauro Carvalho Chehab 		(f->fmt.pix.width * fmt->depth) >> 3;
590b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage =
591b285192aSMauro Carvalho Chehab 		f->fmt.pix.height * f->fmt.pix.bytesperline;
5921af2ddd8SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
593b285192aSMauro Carvalho Chehab 
594b285192aSMauro Carvalho Chehab 	return 0;
595b285192aSMauro Carvalho Chehab }
596b285192aSMauro Carvalho Chehab 
vidioc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)597b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
598b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
599b285192aSMauro Carvalho Chehab {
600568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
601ebf984bbSHans Verkuil 	struct v4l2_subdev_format format = {
602ebf984bbSHans Verkuil 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
603ebf984bbSHans Verkuil 	};
604b285192aSMauro Carvalho Chehab 	int err;
605b285192aSMauro Carvalho Chehab 
606b285192aSMauro Carvalho Chehab 	dprintk(2, "%s()\n", __func__);
607b285192aSMauro Carvalho Chehab 	err = vidioc_try_fmt_vid_cap(file, priv, f);
608b285192aSMauro Carvalho Chehab 
609b285192aSMauro Carvalho Chehab 	if (0 != err)
610b285192aSMauro Carvalho Chehab 		return err;
611f1b6a735SHans Verkuil 
612f1b6a735SHans Verkuil 	if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
613f1b6a735SHans Verkuil 	    vb2_is_busy(&dev->vb2_mpegq))
614f1b6a735SHans Verkuil 		return -EBUSY;
615f1b6a735SHans Verkuil 
61691d2d674SHans Verkuil 	dev->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
61791d2d674SHans Verkuil 	dev->width      = f->fmt.pix.width;
61891d2d674SHans Verkuil 	dev->height     = f->fmt.pix.height;
619453afdd9SHans Verkuil 	dev->field	= f->fmt.pix.field;
620b285192aSMauro Carvalho Chehab 	dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
621453afdd9SHans Verkuil 		dev->width, dev->height, dev->field);
622ebf984bbSHans Verkuil 	v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
623ebf984bbSHans Verkuil 	call_all(dev, pad, set_fmt, NULL, &format);
624ebf984bbSHans Verkuil 	v4l2_fill_pix_format(&f->fmt.pix, &format.format);
625ebf984bbSHans Verkuil 	/* set_fmt overwrites f->fmt.pix.field, restore it */
62696f233e9SHans Verkuil 	f->fmt.pix.field = dev->field;
627b285192aSMauro Carvalho Chehab 	return 0;
628b285192aSMauro Carvalho Chehab }
629b285192aSMauro Carvalho Chehab 
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)630b285192aSMauro Carvalho Chehab static int vidioc_querycap(struct file *file, void  *priv,
631b285192aSMauro Carvalho Chehab 	struct v4l2_capability *cap)
632b285192aSMauro Carvalho Chehab {
633568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
634b285192aSMauro Carvalho Chehab 
635cc1e6315SMauro Carvalho Chehab 	strscpy(cap->driver, "cx23885", sizeof(cap->driver));
636c0decac1SMauro Carvalho Chehab 	strscpy(cap->card, cx23885_boards[dev->board].name,
637b285192aSMauro Carvalho Chehab 		sizeof(cap->card));
638b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
63921615365SHans Verkuil 	cap->capabilities = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
64021615365SHans Verkuil 			    V4L2_CAP_AUDIO | V4L2_CAP_VBI_CAPTURE |
64177efe8aeSDaniel W. S. Almeida 			    V4L2_CAP_VIDEO_CAPTURE |
64221615365SHans Verkuil 			    V4L2_CAP_DEVICE_CAPS;
643fbc9a49cSBrad Love 	switch (dev->board) { /* i2c device tuners */
644fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
645fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR5525:
646fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
647fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
648fbc9a49cSBrad Love 		cap->capabilities |= V4L2_CAP_TUNER;
649fbc9a49cSBrad Love 		break;
650fbc9a49cSBrad Love 	default:
6510f3bf3dcSHans Verkuil 		if (dev->tuner_type != TUNER_ABSENT)
65221615365SHans Verkuil 			cap->capabilities |= V4L2_CAP_TUNER;
653fbc9a49cSBrad Love 		break;
654fbc9a49cSBrad Love 	}
655b285192aSMauro Carvalho Chehab 	return 0;
656b285192aSMauro Carvalho Chehab }
657b285192aSMauro Carvalho Chehab 
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)658b285192aSMauro Carvalho Chehab static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
659b285192aSMauro Carvalho Chehab 	struct v4l2_fmtdesc *f)
660b285192aSMauro Carvalho Chehab {
661b285192aSMauro Carvalho Chehab 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
662b285192aSMauro Carvalho Chehab 		return -EINVAL;
663b285192aSMauro Carvalho Chehab 
664b285192aSMauro Carvalho Chehab 	f->pixelformat = formats[f->index].fourcc;
665b285192aSMauro Carvalho Chehab 
666b285192aSMauro Carvalho Chehab 	return 0;
667b285192aSMauro Carvalho Chehab }
668b285192aSMauro Carvalho Chehab 
vidioc_g_pixelaspect(struct file * file,void * priv,int type,struct v4l2_fract * f)6695200ab6aSHans Verkuil static int vidioc_g_pixelaspect(struct file *file, void *priv,
6705200ab6aSHans Verkuil 				int type, struct v4l2_fract *f)
671e80bf856SHans Verkuil {
672e80bf856SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
673e80bf856SHans Verkuil 	bool is_50hz = dev->tvnorm & V4L2_STD_625_50;
674e80bf856SHans Verkuil 
6755200ab6aSHans Verkuil 	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
676e80bf856SHans Verkuil 		return -EINVAL;
677e80bf856SHans Verkuil 
6785200ab6aSHans Verkuil 	f->numerator = is_50hz ? 54 : 11;
6795200ab6aSHans Verkuil 	f->denominator = is_50hz ? 59 : 10;
680e80bf856SHans Verkuil 
681e80bf856SHans Verkuil 	return 0;
682e80bf856SHans Verkuil }
683e80bf856SHans Verkuil 
vidioc_g_selection(struct file * file,void * fh,struct v4l2_selection * sel)684ee10dc36SHans Verkuil static int vidioc_g_selection(struct file *file, void *fh,
685ee10dc36SHans Verkuil 			      struct v4l2_selection *sel)
686ee10dc36SHans Verkuil {
687ee10dc36SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
688ee10dc36SHans Verkuil 
689ee10dc36SHans Verkuil 	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
690ee10dc36SHans Verkuil 		return -EINVAL;
691ee10dc36SHans Verkuil 
692ee10dc36SHans Verkuil 	switch (sel->target) {
693ee10dc36SHans Verkuil 	case V4L2_SEL_TGT_CROP_BOUNDS:
694ee10dc36SHans Verkuil 	case V4L2_SEL_TGT_CROP_DEFAULT:
695ee10dc36SHans Verkuil 		sel->r.top = 0;
696ee10dc36SHans Verkuil 		sel->r.left = 0;
697ee10dc36SHans Verkuil 		sel->r.width = 720;
698ee10dc36SHans Verkuil 		sel->r.height = norm_maxh(dev->tvnorm);
699ee10dc36SHans Verkuil 		break;
700ee10dc36SHans Verkuil 	default:
701ee10dc36SHans Verkuil 		return -EINVAL;
702ee10dc36SHans Verkuil 	}
703ee10dc36SHans Verkuil 	return 0;
704ee10dc36SHans Verkuil }
705ee10dc36SHans Verkuil 
vidioc_g_std(struct file * file,void * priv,v4l2_std_id * id)706b285192aSMauro Carvalho Chehab static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
707b285192aSMauro Carvalho Chehab {
708568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
709b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
710b285192aSMauro Carvalho Chehab 
7119c1f5df8SHans Verkuil 	*id = dev->tvnorm;
712b285192aSMauro Carvalho Chehab 	return 0;
713b285192aSMauro Carvalho Chehab }
714b285192aSMauro Carvalho Chehab 
vidioc_s_std(struct file * file,void * priv,v4l2_std_id tvnorms)715314527acSHans Verkuil static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
716b285192aSMauro Carvalho Chehab {
717568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
718b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
719b285192aSMauro Carvalho Chehab 
720f1b6a735SHans Verkuil 	return cx23885_set_tvnorm(dev, tvnorms);
721b285192aSMauro Carvalho Chehab }
722b285192aSMauro Carvalho Chehab 
cx23885_enum_input(struct cx23885_dev * dev,struct v4l2_input * i)723b285192aSMauro Carvalho Chehab int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
724b285192aSMauro Carvalho Chehab {
725b285192aSMauro Carvalho Chehab 	static const char *iname[] = {
726b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE1] = "Composite1",
727b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE2] = "Composite2",
728b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE3] = "Composite3",
729b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE4] = "Composite4",
730b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_SVIDEO]     = "S-Video",
731b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPONENT]  = "Component",
732b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_TELEVISION] = "Television",
733b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_CABLE]      = "Cable TV",
734b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_DVB]        = "DVB",
735b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_DEBUG]      = "for debug only",
736b285192aSMauro Carvalho Chehab 	};
737b285192aSMauro Carvalho Chehab 	unsigned int n;
738b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
739b285192aSMauro Carvalho Chehab 
740b285192aSMauro Carvalho Chehab 	n = i->index;
741b285192aSMauro Carvalho Chehab 	if (n >= MAX_CX23885_INPUT)
742b285192aSMauro Carvalho Chehab 		return -EINVAL;
743b285192aSMauro Carvalho Chehab 
744b285192aSMauro Carvalho Chehab 	if (0 == INPUT(n)->type)
745b285192aSMauro Carvalho Chehab 		return -EINVAL;
746b285192aSMauro Carvalho Chehab 
747b285192aSMauro Carvalho Chehab 	i->index = n;
748b285192aSMauro Carvalho Chehab 	i->type  = V4L2_INPUT_TYPE_CAMERA;
749cc1e6315SMauro Carvalho Chehab 	strscpy(i->name, iname[INPUT(n)->type], sizeof(i->name));
750d43be757SHans Verkuil 	i->std = CX23885_NORMS;
751b285192aSMauro Carvalho Chehab 	if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
752b285192aSMauro Carvalho Chehab 		(CX23885_VMUX_CABLE == INPUT(n)->type)) {
753b285192aSMauro Carvalho Chehab 		i->type = V4L2_INPUT_TYPE_TUNER;
754d43be757SHans Verkuil 		i->audioset = 4;
755d43be757SHans Verkuil 	} else {
756b285192aSMauro Carvalho Chehab 		/* Two selectable audio inputs for non-tv inputs */
757d43be757SHans Verkuil 		i->audioset = 3;
758d43be757SHans Verkuil 	}
759b285192aSMauro Carvalho Chehab 
760b285192aSMauro Carvalho Chehab 	if (dev->input == n) {
761b285192aSMauro Carvalho Chehab 		/* enum'd input matches our configured input.
762b285192aSMauro Carvalho Chehab 		 * Ask the video decoder to process the call
763b285192aSMauro Carvalho Chehab 		 * and give it an oppertunity to update the
764b285192aSMauro Carvalho Chehab 		 * status field.
765b285192aSMauro Carvalho Chehab 		 */
766b285192aSMauro Carvalho Chehab 		call_all(dev, video, g_input_status, &i->status);
767b285192aSMauro Carvalho Chehab 	}
768b285192aSMauro Carvalho Chehab 
769b285192aSMauro Carvalho Chehab 	return 0;
770b285192aSMauro Carvalho Chehab }
771b285192aSMauro Carvalho Chehab 
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * i)772b285192aSMauro Carvalho Chehab static int vidioc_enum_input(struct file *file, void *priv,
773b285192aSMauro Carvalho Chehab 				struct v4l2_input *i)
774b285192aSMauro Carvalho Chehab {
775568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
776b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
777b285192aSMauro Carvalho Chehab 	return cx23885_enum_input(dev, i);
778b285192aSMauro Carvalho Chehab }
779b285192aSMauro Carvalho Chehab 
cx23885_get_input(struct file * file,void * priv,unsigned int * i)780b285192aSMauro Carvalho Chehab int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
781b285192aSMauro Carvalho Chehab {
782568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
783b285192aSMauro Carvalho Chehab 
784b285192aSMauro Carvalho Chehab 	*i = dev->input;
785b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() returns %d\n", __func__, *i);
786b285192aSMauro Carvalho Chehab 	return 0;
787b285192aSMauro Carvalho Chehab }
788b285192aSMauro Carvalho Chehab 
vidioc_g_input(struct file * file,void * priv,unsigned int * i)789b285192aSMauro Carvalho Chehab static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
790b285192aSMauro Carvalho Chehab {
791b285192aSMauro Carvalho Chehab 	return cx23885_get_input(file, priv, i);
792b285192aSMauro Carvalho Chehab }
793b285192aSMauro Carvalho Chehab 
cx23885_set_input(struct file * file,void * priv,unsigned int i)794b285192aSMauro Carvalho Chehab int cx23885_set_input(struct file *file, void *priv, unsigned int i)
795b285192aSMauro Carvalho Chehab {
796568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
797b285192aSMauro Carvalho Chehab 
798b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(%d)\n", __func__, i);
799b285192aSMauro Carvalho Chehab 
800b285192aSMauro Carvalho Chehab 	if (i >= MAX_CX23885_INPUT) {
801b285192aSMauro Carvalho Chehab 		dprintk(1, "%s() -EINVAL\n", __func__);
802b285192aSMauro Carvalho Chehab 		return -EINVAL;
803b285192aSMauro Carvalho Chehab 	}
804b285192aSMauro Carvalho Chehab 
805b285192aSMauro Carvalho Chehab 	if (INPUT(i)->type == 0)
806b285192aSMauro Carvalho Chehab 		return -EINVAL;
807b285192aSMauro Carvalho Chehab 
808b285192aSMauro Carvalho Chehab 	cx23885_video_mux(dev, i);
809b285192aSMauro Carvalho Chehab 
810b285192aSMauro Carvalho Chehab 	/* By default establish the default audio input for the card also */
811b285192aSMauro Carvalho Chehab 	/* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
812b285192aSMauro Carvalho Chehab 	cx23885_audio_mux(dev, i);
813b285192aSMauro Carvalho Chehab 	return 0;
814b285192aSMauro Carvalho Chehab }
815b285192aSMauro Carvalho Chehab 
vidioc_s_input(struct file * file,void * priv,unsigned int i)816b285192aSMauro Carvalho Chehab static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
817b285192aSMauro Carvalho Chehab {
818b285192aSMauro Carvalho Chehab 	return cx23885_set_input(file, priv, i);
819b285192aSMauro Carvalho Chehab }
820b285192aSMauro Carvalho Chehab 
vidioc_log_status(struct file * file,void * priv)821b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
822b285192aSMauro Carvalho Chehab {
823568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
824b285192aSMauro Carvalho Chehab 
825b285192aSMauro Carvalho Chehab 	call_all(dev, core, log_status);
826b285192aSMauro Carvalho Chehab 	return 0;
827b285192aSMauro Carvalho Chehab }
828b285192aSMauro Carvalho Chehab 
cx23885_query_audinput(struct file * file,void * priv,struct v4l2_audio * i)829b285192aSMauro Carvalho Chehab static int cx23885_query_audinput(struct file *file, void *priv,
830b285192aSMauro Carvalho Chehab 	struct v4l2_audio *i)
831b285192aSMauro Carvalho Chehab {
832b285192aSMauro Carvalho Chehab 	static const char *iname[] = {
833b285192aSMauro Carvalho Chehab 		[0] = "Baseband L/R 1",
834b285192aSMauro Carvalho Chehab 		[1] = "Baseband L/R 2",
835d43be757SHans Verkuil 		[2] = "TV",
836b285192aSMauro Carvalho Chehab 	};
837b285192aSMauro Carvalho Chehab 	unsigned int n;
838b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
839b285192aSMauro Carvalho Chehab 
840b285192aSMauro Carvalho Chehab 	n = i->index;
841d43be757SHans Verkuil 	if (n >= 3)
842b285192aSMauro Carvalho Chehab 		return -EINVAL;
843b285192aSMauro Carvalho Chehab 
844b285192aSMauro Carvalho Chehab 	memset(i, 0, sizeof(*i));
845b285192aSMauro Carvalho Chehab 	i->index = n;
846cc1e6315SMauro Carvalho Chehab 	strscpy(i->name, iname[n], sizeof(i->name));
847b285192aSMauro Carvalho Chehab 	i->capability = V4L2_AUDCAP_STEREO;
848b285192aSMauro Carvalho Chehab 	return 0;
849b285192aSMauro Carvalho Chehab 
850b285192aSMauro Carvalho Chehab }
851b285192aSMauro Carvalho Chehab 
vidioc_enum_audinput(struct file * file,void * priv,struct v4l2_audio * i)852b285192aSMauro Carvalho Chehab static int vidioc_enum_audinput(struct file *file, void *priv,
853b285192aSMauro Carvalho Chehab 				struct v4l2_audio *i)
854b285192aSMauro Carvalho Chehab {
855b285192aSMauro Carvalho Chehab 	return cx23885_query_audinput(file, priv, i);
856b285192aSMauro Carvalho Chehab }
857b285192aSMauro Carvalho Chehab 
vidioc_g_audinput(struct file * file,void * priv,struct v4l2_audio * i)858b285192aSMauro Carvalho Chehab static int vidioc_g_audinput(struct file *file, void *priv,
859b285192aSMauro Carvalho Chehab 	struct v4l2_audio *i)
860b285192aSMauro Carvalho Chehab {
861568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
862b285192aSMauro Carvalho Chehab 
863d43be757SHans Verkuil 	if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
864d43be757SHans Verkuil 		(CX23885_VMUX_CABLE == INPUT(dev->input)->type))
865d43be757SHans Verkuil 		i->index = 2;
866d43be757SHans Verkuil 	else
867b285192aSMauro Carvalho Chehab 		i->index = dev->audinput;
868b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(input=%d)\n", __func__, i->index);
869b285192aSMauro Carvalho Chehab 
870b285192aSMauro Carvalho Chehab 	return cx23885_query_audinput(file, priv, i);
871b285192aSMauro Carvalho Chehab }
872b285192aSMauro Carvalho Chehab 
vidioc_s_audinput(struct file * file,void * priv,const struct v4l2_audio * i)873b285192aSMauro Carvalho Chehab static int vidioc_s_audinput(struct file *file, void *priv,
8740e8025b9SHans Verkuil 	const struct v4l2_audio *i)
875b285192aSMauro Carvalho Chehab {
876568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
877d43be757SHans Verkuil 
878d43be757SHans Verkuil 	if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
879d43be757SHans Verkuil 		(CX23885_VMUX_CABLE == INPUT(dev->input)->type)) {
880d43be757SHans Verkuil 		return i->index != 2 ? -EINVAL : 0;
881d43be757SHans Verkuil 	}
882d43be757SHans Verkuil 	if (i->index > 1)
883b285192aSMauro Carvalho Chehab 		return -EINVAL;
884b285192aSMauro Carvalho Chehab 
885b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(%d)\n", __func__, i->index);
886b285192aSMauro Carvalho Chehab 
887b285192aSMauro Carvalho Chehab 	dev->audinput = i->index;
888b285192aSMauro Carvalho Chehab 
889b285192aSMauro Carvalho Chehab 	/* Skip the audio defaults from the cards struct, caller wants
890b285192aSMauro Carvalho Chehab 	 * directly touch the audio mux hardware. */
891b285192aSMauro Carvalho Chehab 	cx23885_flatiron_mux(dev, dev->audinput + 1);
892b285192aSMauro Carvalho Chehab 	return 0;
893b285192aSMauro Carvalho Chehab }
894b285192aSMauro Carvalho Chehab 
vidioc_g_tuner(struct file * file,void * priv,struct v4l2_tuner * t)895b285192aSMauro Carvalho Chehab static int vidioc_g_tuner(struct file *file, void *priv,
896b285192aSMauro Carvalho Chehab 				struct v4l2_tuner *t)
897b285192aSMauro Carvalho Chehab {
898568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
899b285192aSMauro Carvalho Chehab 
900fbc9a49cSBrad Love 	switch (dev->board) { /* i2c device tuners */
901fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
902fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR5525:
903fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
904fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
905fbc9a49cSBrad Love 		break;
906fbc9a49cSBrad Love 	default:
9070f3bf3dcSHans Verkuil 		if (dev->tuner_type == TUNER_ABSENT)
908b285192aSMauro Carvalho Chehab 			return -EINVAL;
909fbc9a49cSBrad Love 		break;
910fbc9a49cSBrad Love 	}
911b285192aSMauro Carvalho Chehab 	if (0 != t->index)
912b285192aSMauro Carvalho Chehab 		return -EINVAL;
913b285192aSMauro Carvalho Chehab 
914cc1e6315SMauro Carvalho Chehab 	strscpy(t->name, "Television", sizeof(t->name));
915b285192aSMauro Carvalho Chehab 
916b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, g_tuner, t);
917b285192aSMauro Carvalho Chehab 	return 0;
918b285192aSMauro Carvalho Chehab }
919b285192aSMauro Carvalho Chehab 
vidioc_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * t)920b285192aSMauro Carvalho Chehab static int vidioc_s_tuner(struct file *file, void *priv,
9212f73c7c5SHans Verkuil 				const struct v4l2_tuner *t)
922b285192aSMauro Carvalho Chehab {
923568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
924b285192aSMauro Carvalho Chehab 
925fbc9a49cSBrad Love 	switch (dev->board) { /* i2c device tuners */
926fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
927fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR5525:
928fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
929fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
930fbc9a49cSBrad Love 		break;
931fbc9a49cSBrad Love 	default:
9320f3bf3dcSHans Verkuil 		if (dev->tuner_type == TUNER_ABSENT)
933b285192aSMauro Carvalho Chehab 			return -EINVAL;
934fbc9a49cSBrad Love 		break;
935fbc9a49cSBrad Love 	}
936b285192aSMauro Carvalho Chehab 	if (0 != t->index)
937b285192aSMauro Carvalho Chehab 		return -EINVAL;
938b285192aSMauro Carvalho Chehab 	/* Update the A/V core */
939b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, s_tuner, t);
940b285192aSMauro Carvalho Chehab 
941b285192aSMauro Carvalho Chehab 	return 0;
942b285192aSMauro Carvalho Chehab }
943b285192aSMauro Carvalho Chehab 
vidioc_g_frequency(struct file * file,void * priv,struct v4l2_frequency * f)944b285192aSMauro Carvalho Chehab static int vidioc_g_frequency(struct file *file, void *priv,
945b285192aSMauro Carvalho Chehab 				struct v4l2_frequency *f)
946b285192aSMauro Carvalho Chehab {
947568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
948b285192aSMauro Carvalho Chehab 
949fbc9a49cSBrad Love 	switch (dev->board) { /* i2c device tuners */
950fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
951fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR5525:
952fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
953fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
954fbc9a49cSBrad Love 		break;
955fbc9a49cSBrad Love 	default:
9560f3bf3dcSHans Verkuil 		if (dev->tuner_type == TUNER_ABSENT)
957b285192aSMauro Carvalho Chehab 			return -EINVAL;
958fbc9a49cSBrad Love 		break;
959fbc9a49cSBrad Love 	}
96086961933SHans Verkuil 	f->type = V4L2_TUNER_ANALOG_TV;
961b285192aSMauro Carvalho Chehab 	f->frequency = dev->freq;
962b285192aSMauro Carvalho Chehab 
963b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, g_frequency, f);
964b285192aSMauro Carvalho Chehab 
965b285192aSMauro Carvalho Chehab 	return 0;
966b285192aSMauro Carvalho Chehab }
967b285192aSMauro Carvalho Chehab 
cx23885_set_freq(struct cx23885_dev * dev,const struct v4l2_frequency * f)968b530a447SHans Verkuil static int cx23885_set_freq(struct cx23885_dev *dev, const struct v4l2_frequency *f)
969b285192aSMauro Carvalho Chehab {
970da59a4deSHans Verkuil 	struct v4l2_ctrl *mute;
971da59a4deSHans Verkuil 	int old_mute_val = 1;
972b285192aSMauro Carvalho Chehab 
973fbc9a49cSBrad Love 	switch (dev->board) { /* i2c device tuners */
974fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
975fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR5525:
976fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
977fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
978fbc9a49cSBrad Love 		break;
979fbc9a49cSBrad Love 	default:
9800f3bf3dcSHans Verkuil 		if (dev->tuner_type == TUNER_ABSENT)
981b285192aSMauro Carvalho Chehab 			return -EINVAL;
982fbc9a49cSBrad Love 		break;
983fbc9a49cSBrad Love 	}
984b285192aSMauro Carvalho Chehab 	if (unlikely(f->tuner != 0))
985b285192aSMauro Carvalho Chehab 		return -EINVAL;
986b285192aSMauro Carvalho Chehab 
987b285192aSMauro Carvalho Chehab 	dev->freq = f->frequency;
988b285192aSMauro Carvalho Chehab 
989b285192aSMauro Carvalho Chehab 	/* I need to mute audio here */
990da59a4deSHans Verkuil 	mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
991da59a4deSHans Verkuil 	if (mute) {
992da59a4deSHans Verkuil 		old_mute_val = v4l2_ctrl_g_ctrl(mute);
993da59a4deSHans Verkuil 		if (!old_mute_val)
994da59a4deSHans Verkuil 			v4l2_ctrl_s_ctrl(mute, 1);
995da59a4deSHans Verkuil 	}
996b285192aSMauro Carvalho Chehab 
997b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, s_frequency, f);
998b285192aSMauro Carvalho Chehab 
999b285192aSMauro Carvalho Chehab 	/* When changing channels it is required to reset TVAUDIO */
1000b285192aSMauro Carvalho Chehab 	msleep(100);
1001b285192aSMauro Carvalho Chehab 
1002b285192aSMauro Carvalho Chehab 	/* I need to unmute audio here */
1003da59a4deSHans Verkuil 	if (old_mute_val == 0)
1004da59a4deSHans Verkuil 		v4l2_ctrl_s_ctrl(mute, old_mute_val);
1005b285192aSMauro Carvalho Chehab 
1006b285192aSMauro Carvalho Chehab 	return 0;
1007b285192aSMauro Carvalho Chehab }
1008b285192aSMauro Carvalho Chehab 
cx23885_set_freq_via_ops(struct cx23885_dev * dev,const struct v4l2_frequency * f)1009b285192aSMauro Carvalho Chehab static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
1010b530a447SHans Verkuil 	const struct v4l2_frequency *f)
1011b285192aSMauro Carvalho Chehab {
1012da59a4deSHans Verkuil 	struct v4l2_ctrl *mute;
1013da59a4deSHans Verkuil 	int old_mute_val = 1;
1014453afdd9SHans Verkuil 	struct vb2_dvb_frontend *vfe;
1015b285192aSMauro Carvalho Chehab 	struct dvb_frontend *fe;
1016b285192aSMauro Carvalho Chehab 
1017b285192aSMauro Carvalho Chehab 	struct analog_parameters params = {
1018b285192aSMauro Carvalho Chehab 		.mode      = V4L2_TUNER_ANALOG_TV,
1019b285192aSMauro Carvalho Chehab 		.audmode   = V4L2_TUNER_MODE_STEREO,
1020b285192aSMauro Carvalho Chehab 		.std       = dev->tvnorm,
1021b285192aSMauro Carvalho Chehab 		.frequency = f->frequency
1022b285192aSMauro Carvalho Chehab 	};
1023b285192aSMauro Carvalho Chehab 
1024b285192aSMauro Carvalho Chehab 	dev->freq = f->frequency;
1025b285192aSMauro Carvalho Chehab 
1026b285192aSMauro Carvalho Chehab 	/* I need to mute audio here */
1027da59a4deSHans Verkuil 	mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1028da59a4deSHans Verkuil 	if (mute) {
1029da59a4deSHans Verkuil 		old_mute_val = v4l2_ctrl_g_ctrl(mute);
1030da59a4deSHans Verkuil 		if (!old_mute_val)
1031da59a4deSHans Verkuil 			v4l2_ctrl_s_ctrl(mute, 1);
1032da59a4deSHans Verkuil 	}
1033b285192aSMauro Carvalho Chehab 
1034b285192aSMauro Carvalho Chehab 	/* If HVR1850 */
1035b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
1036b285192aSMauro Carvalho Chehab 		params.frequency, f->tuner, params.std);
1037b285192aSMauro Carvalho Chehab 
1038453afdd9SHans Verkuil 	vfe = vb2_dvb_get_frontend(&dev->ts2.frontends, 1);
1039b285192aSMauro Carvalho Chehab 	if (!vfe) {
1040b285192aSMauro Carvalho Chehab 		return -EINVAL;
1041b285192aSMauro Carvalho Chehab 	}
1042b285192aSMauro Carvalho Chehab 
1043b285192aSMauro Carvalho Chehab 	fe = vfe->dvb.frontend;
1044b285192aSMauro Carvalho Chehab 
1045b285192aSMauro Carvalho Chehab 	if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
1046b285192aSMauro Carvalho Chehab 	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
104794f11518SBrad Love 	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
104881118817SBrad Love 	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1265_K4) ||
10492be355a0SBrad Love 	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR5525) ||
105081118817SBrad Love 	    (dev->board == CX23885_BOARD_HAUPPAUGE_QUADHD_DVB) ||
105181118817SBrad Love 	    (dev->board == CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC))
1052b285192aSMauro Carvalho Chehab 		fe = &dev->ts1.analog_fe;
1053b285192aSMauro Carvalho Chehab 
1054b285192aSMauro Carvalho Chehab 	if (fe && fe->ops.tuner_ops.set_analog_params) {
10558774bed9SLaurent Pinchart 		call_all(dev, video, s_std, dev->tvnorm);
1056b285192aSMauro Carvalho Chehab 		fe->ops.tuner_ops.set_analog_params(fe, &params);
1057b285192aSMauro Carvalho Chehab 	}
1058b285192aSMauro Carvalho Chehab 	else
1059e39682b5SMauro Carvalho Chehab 		pr_err("%s() No analog tuner, aborting\n", __func__);
1060b285192aSMauro Carvalho Chehab 
1061b285192aSMauro Carvalho Chehab 	/* When changing channels it is required to reset TVAUDIO */
1062b285192aSMauro Carvalho Chehab 	msleep(100);
1063b285192aSMauro Carvalho Chehab 
1064b285192aSMauro Carvalho Chehab 	/* I need to unmute audio here */
1065da59a4deSHans Verkuil 	if (old_mute_val == 0)
1066da59a4deSHans Verkuil 		v4l2_ctrl_s_ctrl(mute, old_mute_val);
1067b285192aSMauro Carvalho Chehab 
1068b285192aSMauro Carvalho Chehab 	return 0;
1069b285192aSMauro Carvalho Chehab }
1070b285192aSMauro Carvalho Chehab 
cx23885_set_frequency(struct file * file,void * priv,const struct v4l2_frequency * f)1071b285192aSMauro Carvalho Chehab int cx23885_set_frequency(struct file *file, void *priv,
1072b530a447SHans Verkuil 	const struct v4l2_frequency *f)
1073b285192aSMauro Carvalho Chehab {
1074568f44a1SHans Verkuil 	struct cx23885_dev *dev = video_drvdata(file);
1075b285192aSMauro Carvalho Chehab 	int ret;
1076b285192aSMauro Carvalho Chehab 
1077b285192aSMauro Carvalho Chehab 	switch (dev->board) {
1078b285192aSMauro Carvalho Chehab 	case CX23885_BOARD_HAUPPAUGE_HVR1255:
1079b285192aSMauro Carvalho Chehab 	case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
108094f11518SBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
1081b285192aSMauro Carvalho Chehab 	case CX23885_BOARD_HAUPPAUGE_HVR1850:
10822be355a0SBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR5525:
108381118817SBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
108481118817SBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
1085b285192aSMauro Carvalho Chehab 		ret = cx23885_set_freq_via_ops(dev, f);
1086b285192aSMauro Carvalho Chehab 		break;
1087b285192aSMauro Carvalho Chehab 	default:
1088b285192aSMauro Carvalho Chehab 		ret = cx23885_set_freq(dev, f);
1089b285192aSMauro Carvalho Chehab 	}
1090b285192aSMauro Carvalho Chehab 
1091b285192aSMauro Carvalho Chehab 	return ret;
1092b285192aSMauro Carvalho Chehab }
1093b285192aSMauro Carvalho Chehab 
vidioc_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * f)1094b285192aSMauro Carvalho Chehab static int vidioc_s_frequency(struct file *file, void *priv,
1095b530a447SHans Verkuil 	const struct v4l2_frequency *f)
1096b285192aSMauro Carvalho Chehab {
1097b285192aSMauro Carvalho Chehab 	return cx23885_set_frequency(file, priv, f);
1098b285192aSMauro Carvalho Chehab }
1099b285192aSMauro Carvalho Chehab 
1100b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------- */
1101b285192aSMauro Carvalho Chehab 
cx23885_video_irq(struct cx23885_dev * dev,u32 status)1102b285192aSMauro Carvalho Chehab int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1103b285192aSMauro Carvalho Chehab {
1104b285192aSMauro Carvalho Chehab 	u32 mask, count;
1105b285192aSMauro Carvalho Chehab 	int handled = 0;
1106b285192aSMauro Carvalho Chehab 
1107b285192aSMauro Carvalho Chehab 	mask   = cx_read(VID_A_INT_MSK);
1108b285192aSMauro Carvalho Chehab 	if (0 == (status & mask))
1109b285192aSMauro Carvalho Chehab 		return handled;
1110b285192aSMauro Carvalho Chehab 
1111b285192aSMauro Carvalho Chehab 	cx_write(VID_A_INT_STAT, status);
1112b285192aSMauro Carvalho Chehab 
1113b285192aSMauro Carvalho Chehab 	/* risc op code error, fifo overflow or line sync detection error */
1114b285192aSMauro Carvalho Chehab 	if ((status & VID_BC_MSK_OPC_ERR) ||
1115b285192aSMauro Carvalho Chehab 		(status & VID_BC_MSK_SYNC) ||
1116b285192aSMauro Carvalho Chehab 		(status & VID_BC_MSK_OF)) {
1117b285192aSMauro Carvalho Chehab 
1118b285192aSMauro Carvalho Chehab 		if (status & VID_BC_MSK_OPC_ERR) {
1119b285192aSMauro Carvalho Chehab 			dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1120b285192aSMauro Carvalho Chehab 				VID_BC_MSK_OPC_ERR);
1121e39682b5SMauro Carvalho Chehab 			pr_warn("%s: video risc op code error\n",
1122b285192aSMauro Carvalho Chehab 				dev->name);
1123b285192aSMauro Carvalho Chehab 			cx23885_sram_channel_dump(dev,
1124b285192aSMauro Carvalho Chehab 				&dev->sram_channels[SRAM_CH01]);
1125b285192aSMauro Carvalho Chehab 		}
1126b285192aSMauro Carvalho Chehab 
1127b285192aSMauro Carvalho Chehab 		if (status & VID_BC_MSK_SYNC)
112807ab29e1SMauro Carvalho Chehab 			dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) video lines miss-match\n",
1129b285192aSMauro Carvalho Chehab 				VID_BC_MSK_SYNC);
1130b285192aSMauro Carvalho Chehab 
1131b285192aSMauro Carvalho Chehab 		if (status & VID_BC_MSK_OF)
1132b285192aSMauro Carvalho Chehab 			dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1133b285192aSMauro Carvalho Chehab 				VID_BC_MSK_OF);
1134b285192aSMauro Carvalho Chehab 
1135b285192aSMauro Carvalho Chehab 	}
1136b285192aSMauro Carvalho Chehab 
1137b285192aSMauro Carvalho Chehab 	/* Video */
1138b285192aSMauro Carvalho Chehab 	if (status & VID_BC_MSK_RISCI1) {
1139b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
1140b285192aSMauro Carvalho Chehab 		count = cx_read(VID_A_GPCNT);
1141b285192aSMauro Carvalho Chehab 		cx23885_video_wakeup(dev, &dev->vidq, count);
1142b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
1143b285192aSMauro Carvalho Chehab 		handled++;
1144b285192aSMauro Carvalho Chehab 	}
1145b285192aSMauro Carvalho Chehab 
1146b285192aSMauro Carvalho Chehab 	/* Allow the VBI framework to process it's payload */
1147b285192aSMauro Carvalho Chehab 	handled += cx23885_vbi_irq(dev, status);
1148b285192aSMauro Carvalho Chehab 
1149b285192aSMauro Carvalho Chehab 	return handled;
1150b285192aSMauro Carvalho Chehab }
1151b285192aSMauro Carvalho Chehab 
1152b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------- */
1153b285192aSMauro Carvalho Chehab /* exported stuff                                              */
1154b285192aSMauro Carvalho Chehab 
1155b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops = {
1156b285192aSMauro Carvalho Chehab 	.owner	       = THIS_MODULE,
1157453afdd9SHans Verkuil 	.open           = v4l2_fh_open,
1158453afdd9SHans Verkuil 	.release        = vb2_fop_release,
1159453afdd9SHans Verkuil 	.read           = vb2_fop_read,
1160453afdd9SHans Verkuil 	.poll		= vb2_fop_poll,
116124a8f7b5SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
1162453afdd9SHans Verkuil 	.mmap           = vb2_fop_mmap,
1163b285192aSMauro Carvalho Chehab };
1164b285192aSMauro Carvalho Chehab 
1165b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops = {
1166b285192aSMauro Carvalho Chehab 	.vidioc_querycap      = vidioc_querycap,
1167b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1168b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1169b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1170b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1171b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vbi_cap     = cx23885_vbi_fmt,
1172b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vbi_cap   = cx23885_vbi_fmt,
1173b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vbi_cap     = cx23885_vbi_fmt,
1174453afdd9SHans Verkuil 	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
1175453afdd9SHans Verkuil 	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
1176453afdd9SHans Verkuil 	.vidioc_querybuf      = vb2_ioctl_querybuf,
1177453afdd9SHans Verkuil 	.vidioc_qbuf          = vb2_ioctl_qbuf,
1178453afdd9SHans Verkuil 	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
1179453afdd9SHans Verkuil 	.vidioc_streamon      = vb2_ioctl_streamon,
1180453afdd9SHans Verkuil 	.vidioc_streamoff     = vb2_ioctl_streamoff,
11815200ab6aSHans Verkuil 	.vidioc_g_pixelaspect = vidioc_g_pixelaspect,
1182ee10dc36SHans Verkuil 	.vidioc_g_selection   = vidioc_g_selection,
1183b285192aSMauro Carvalho Chehab 	.vidioc_s_std         = vidioc_s_std,
1184b285192aSMauro Carvalho Chehab 	.vidioc_g_std         = vidioc_g_std,
1185b285192aSMauro Carvalho Chehab 	.vidioc_enum_input    = vidioc_enum_input,
1186b285192aSMauro Carvalho Chehab 	.vidioc_g_input       = vidioc_g_input,
1187b285192aSMauro Carvalho Chehab 	.vidioc_s_input       = vidioc_s_input,
1188b285192aSMauro Carvalho Chehab 	.vidioc_log_status    = vidioc_log_status,
1189b285192aSMauro Carvalho Chehab 	.vidioc_g_tuner       = vidioc_g_tuner,
1190b285192aSMauro Carvalho Chehab 	.vidioc_s_tuner       = vidioc_s_tuner,
1191b285192aSMauro Carvalho Chehab 	.vidioc_g_frequency   = vidioc_g_frequency,
1192b285192aSMauro Carvalho Chehab 	.vidioc_s_frequency   = vidioc_s_frequency,
1193b285192aSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
119480f8568fSHans Verkuil 	.vidioc_g_chip_info   = cx23885_g_chip_info,
1195b285192aSMauro Carvalho Chehab 	.vidioc_g_register    = cx23885_g_register,
1196b285192aSMauro Carvalho Chehab 	.vidioc_s_register    = cx23885_s_register,
1197b285192aSMauro Carvalho Chehab #endif
1198b285192aSMauro Carvalho Chehab 	.vidioc_enumaudio     = vidioc_enum_audinput,
1199b285192aSMauro Carvalho Chehab 	.vidioc_g_audio       = vidioc_g_audinput,
1200b285192aSMauro Carvalho Chehab 	.vidioc_s_audio       = vidioc_s_audinput,
1201da59a4deSHans Verkuil 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1202da59a4deSHans Verkuil 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1203b285192aSMauro Carvalho Chehab };
1204b285192aSMauro Carvalho Chehab 
1205b285192aSMauro Carvalho Chehab static struct video_device cx23885_vbi_template;
1206b285192aSMauro Carvalho Chehab static struct video_device cx23885_video_template = {
1207b285192aSMauro Carvalho Chehab 	.name                 = "cx23885-video",
1208b285192aSMauro Carvalho Chehab 	.fops                 = &video_fops,
1209b285192aSMauro Carvalho Chehab 	.ioctl_ops	      = &video_ioctl_ops,
1210b285192aSMauro Carvalho Chehab 	.tvnorms              = CX23885_NORMS,
1211b285192aSMauro Carvalho Chehab };
1212b285192aSMauro Carvalho Chehab 
cx23885_video_unregister(struct cx23885_dev * dev)1213b285192aSMauro Carvalho Chehab void cx23885_video_unregister(struct cx23885_dev *dev)
1214b285192aSMauro Carvalho Chehab {
1215b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1216b285192aSMauro Carvalho Chehab 	cx23885_irq_remove(dev, 0x01);
1217b285192aSMauro Carvalho Chehab 
1218b285192aSMauro Carvalho Chehab 	if (dev->vbi_dev) {
1219b285192aSMauro Carvalho Chehab 		if (video_is_registered(dev->vbi_dev))
1220b285192aSMauro Carvalho Chehab 			video_unregister_device(dev->vbi_dev);
1221b285192aSMauro Carvalho Chehab 		else
1222b285192aSMauro Carvalho Chehab 			video_device_release(dev->vbi_dev);
1223b285192aSMauro Carvalho Chehab 		dev->vbi_dev = NULL;
1224b285192aSMauro Carvalho Chehab 	}
1225b285192aSMauro Carvalho Chehab 	if (dev->video_dev) {
1226b285192aSMauro Carvalho Chehab 		if (video_is_registered(dev->video_dev))
1227b285192aSMauro Carvalho Chehab 			video_unregister_device(dev->video_dev);
1228b285192aSMauro Carvalho Chehab 		else
1229b285192aSMauro Carvalho Chehab 			video_device_release(dev->video_dev);
1230b285192aSMauro Carvalho Chehab 		dev->video_dev = NULL;
1231b285192aSMauro Carvalho Chehab 	}
1232b285192aSMauro Carvalho Chehab 
1233b285192aSMauro Carvalho Chehab 	if (dev->audio_dev)
1234b285192aSMauro Carvalho Chehab 		cx23885_audio_unregister(dev);
1235b285192aSMauro Carvalho Chehab }
1236b285192aSMauro Carvalho Chehab 
cx23885_video_register(struct cx23885_dev * dev)1237b285192aSMauro Carvalho Chehab int cx23885_video_register(struct cx23885_dev *dev)
1238b285192aSMauro Carvalho Chehab {
1239453afdd9SHans Verkuil 	struct vb2_queue *q;
1240b285192aSMauro Carvalho Chehab 	int err;
1241b285192aSMauro Carvalho Chehab 
1242b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1243b285192aSMauro Carvalho Chehab 
1244b285192aSMauro Carvalho Chehab 	/* Initialize VBI template */
12453618acabSEzequiel Garcia 	cx23885_vbi_template = cx23885_video_template;
1246cc1e6315SMauro Carvalho Chehab 	strscpy(cx23885_vbi_template.name, "cx23885-vbi",
1247cc1e6315SMauro Carvalho Chehab 		sizeof(cx23885_vbi_template.name));
1248b285192aSMauro Carvalho Chehab 
12499c1f5df8SHans Verkuil 	dev->tvnorm = V4L2_STD_NTSC_M;
125091d2d674SHans Verkuil 	dev->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
125196f233e9SHans Verkuil 	dev->field = V4L2_FIELD_INTERLACED;
12521c5eaa23SHans Verkuil 	dev->width = 720;
125391d2d674SHans Verkuil 	dev->height = norm_maxh(dev->tvnorm);
1254b285192aSMauro Carvalho Chehab 
1255b285192aSMauro Carvalho Chehab 	/* init video dma queues */
1256b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->vidq.active);
1257b285192aSMauro Carvalho Chehab 
1258b285192aSMauro Carvalho Chehab 	/* init vbi dma queues */
1259b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->vbiq.active);
1260b285192aSMauro Carvalho Chehab 
1261b285192aSMauro Carvalho Chehab 	cx23885_irq_add_enable(dev, 0x01);
1262b285192aSMauro Carvalho Chehab 
1263b285192aSMauro Carvalho Chehab 	if ((TUNER_ABSENT != dev->tuner_type) &&
1264b285192aSMauro Carvalho Chehab 			((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1265b285192aSMauro Carvalho Chehab 		struct v4l2_subdev *sd = NULL;
1266b285192aSMauro Carvalho Chehab 
1267b285192aSMauro Carvalho Chehab 		if (dev->tuner_addr)
1268b285192aSMauro Carvalho Chehab 			sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1269b285192aSMauro Carvalho Chehab 				&dev->i2c_bus[dev->tuner_bus].i2c_adap,
1270b285192aSMauro Carvalho Chehab 				"tuner", dev->tuner_addr, NULL);
1271b285192aSMauro Carvalho Chehab 		else
1272b285192aSMauro Carvalho Chehab 			sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1273b285192aSMauro Carvalho Chehab 				&dev->i2c_bus[dev->tuner_bus].i2c_adap,
1274b285192aSMauro Carvalho Chehab 				"tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1275b285192aSMauro Carvalho Chehab 		if (sd) {
1276b285192aSMauro Carvalho Chehab 			struct tuner_setup tun_setup;
1277b285192aSMauro Carvalho Chehab 
1278b285192aSMauro Carvalho Chehab 			memset(&tun_setup, 0, sizeof(tun_setup));
1279b285192aSMauro Carvalho Chehab 			tun_setup.mode_mask = T_ANALOG_TV;
1280b285192aSMauro Carvalho Chehab 			tun_setup.type = dev->tuner_type;
1281b285192aSMauro Carvalho Chehab 			tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1282b285192aSMauro Carvalho Chehab 			tun_setup.tuner_callback = cx23885_tuner_callback;
1283b285192aSMauro Carvalho Chehab 
1284b285192aSMauro Carvalho Chehab 			v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1285b285192aSMauro Carvalho Chehab 
1286642ca1a0SAnca Emanuel 			if ((dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) ||
1287642ca1a0SAnca Emanuel 			    (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200)) {
1288b285192aSMauro Carvalho Chehab 				struct xc2028_ctrl ctrl = {
1289b285192aSMauro Carvalho Chehab 					.fname = XC2028_DEFAULT_FIRMWARE,
1290b285192aSMauro Carvalho Chehab 					.max_len = 64
1291b285192aSMauro Carvalho Chehab 				};
1292b285192aSMauro Carvalho Chehab 				struct v4l2_priv_tun_config cfg = {
1293b285192aSMauro Carvalho Chehab 					.tuner = dev->tuner_type,
1294b285192aSMauro Carvalho Chehab 					.priv = &ctrl
1295b285192aSMauro Carvalho Chehab 				};
1296b285192aSMauro Carvalho Chehab 				v4l2_subdev_call(sd, tuner, s_config, &cfg);
1297b285192aSMauro Carvalho Chehab 			}
1298e8d42373SOleh Kravchenko 
1299e8d42373SOleh Kravchenko 			if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
1300e8d42373SOleh Kravchenko 				struct xc2028_ctrl ctrl = {
1301e8d42373SOleh Kravchenko 					.fname = "xc3028L-v36.fw",
1302e8d42373SOleh Kravchenko 					.max_len = 64
1303e8d42373SOleh Kravchenko 				};
1304e8d42373SOleh Kravchenko 				struct v4l2_priv_tun_config cfg = {
1305e8d42373SOleh Kravchenko 					.tuner = dev->tuner_type,
1306e8d42373SOleh Kravchenko 					.priv = &ctrl
1307e8d42373SOleh Kravchenko 				};
1308e8d42373SOleh Kravchenko 				v4l2_subdev_call(sd, tuner, s_config, &cfg);
1309e8d42373SOleh Kravchenko 			}
1310b285192aSMauro Carvalho Chehab 		}
1311b285192aSMauro Carvalho Chehab 	}
1312b285192aSMauro Carvalho Chehab 
131324a8f7b5SHans Verkuil 	/* initial device configuration */
131424a8f7b5SHans Verkuil 	mutex_lock(&dev->lock);
131524a8f7b5SHans Verkuil 	cx23885_set_tvnorm(dev, dev->tvnorm);
131624a8f7b5SHans Verkuil 	cx23885_video_mux(dev, 0);
131724a8f7b5SHans Verkuil 	cx23885_audio_mux(dev, 0);
131824a8f7b5SHans Verkuil 	mutex_unlock(&dev->lock);
131924a8f7b5SHans Verkuil 
1320453afdd9SHans Verkuil 	q = &dev->vb2_vidq;
1321453afdd9SHans Verkuil 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1322453afdd9SHans Verkuil 	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1323453afdd9SHans Verkuil 	q->gfp_flags = GFP_DMA32;
132480c2b40aSBenjamin Gaignard 	q->min_queued_buffers = 2;
1325453afdd9SHans Verkuil 	q->drv_priv = dev;
1326453afdd9SHans Verkuil 	q->buf_struct_size = sizeof(struct cx23885_buffer);
1327453afdd9SHans Verkuil 	q->ops = &cx23885_video_qops;
1328453afdd9SHans Verkuil 	q->mem_ops = &vb2_dma_sg_memops;
1329453afdd9SHans Verkuil 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1330453afdd9SHans Verkuil 	q->lock = &dev->lock;
13312bc46b3aSHans Verkuil 	q->dev = &dev->pci->dev;
1332453afdd9SHans Verkuil 
1333453afdd9SHans Verkuil 	err = vb2_queue_init(q);
1334453afdd9SHans Verkuil 	if (err < 0)
1335453afdd9SHans Verkuil 		goto fail_unreg;
1336453afdd9SHans Verkuil 
1337453afdd9SHans Verkuil 	q = &dev->vb2_vbiq;
1338453afdd9SHans Verkuil 	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1339453afdd9SHans Verkuil 	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1340453afdd9SHans Verkuil 	q->gfp_flags = GFP_DMA32;
134180c2b40aSBenjamin Gaignard 	q->min_queued_buffers = 2;
1342453afdd9SHans Verkuil 	q->drv_priv = dev;
1343453afdd9SHans Verkuil 	q->buf_struct_size = sizeof(struct cx23885_buffer);
1344453afdd9SHans Verkuil 	q->ops = &cx23885_vbi_qops;
1345453afdd9SHans Verkuil 	q->mem_ops = &vb2_dma_sg_memops;
1346453afdd9SHans Verkuil 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1347453afdd9SHans Verkuil 	q->lock = &dev->lock;
13482bc46b3aSHans Verkuil 	q->dev = &dev->pci->dev;
1349453afdd9SHans Verkuil 
1350453afdd9SHans Verkuil 	err = vb2_queue_init(q);
1351453afdd9SHans Verkuil 	if (err < 0)
1352453afdd9SHans Verkuil 		goto fail_unreg;
1353453afdd9SHans Verkuil 
1354b285192aSMauro Carvalho Chehab 	/* register Video device */
1355b285192aSMauro Carvalho Chehab 	dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1356b285192aSMauro Carvalho Chehab 		&cx23885_video_template, "video");
1357*15126b91SHans Verkuil 	if (!dev->video_dev) {
1358*15126b91SHans Verkuil 		err = -ENOMEM;
1359*15126b91SHans Verkuil 		goto fail_unreg;
1360*15126b91SHans Verkuil 	}
1361453afdd9SHans Verkuil 	dev->video_dev->queue = &dev->vb2_vidq;
136221615365SHans Verkuil 	dev->video_dev->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
136321615365SHans Verkuil 				      V4L2_CAP_AUDIO | V4L2_CAP_VIDEO_CAPTURE;
1364fbc9a49cSBrad Love 	switch (dev->board) { /* i2c device tuners */
1365fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
1366fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR5525:
1367fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
1368fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
1369fbc9a49cSBrad Love 		dev->video_dev->device_caps |= V4L2_CAP_TUNER;
1370fbc9a49cSBrad Love 		break;
1371fbc9a49cSBrad Love 	default:
137221615365SHans Verkuil 		if (dev->tuner_type != TUNER_ABSENT)
137321615365SHans Verkuil 			dev->video_dev->device_caps |= V4L2_CAP_TUNER;
1374fbc9a49cSBrad Love 	}
1375fbc9a49cSBrad Love 
13763e30a927SHans Verkuil 	err = video_register_device(dev->video_dev, VFL_TYPE_VIDEO,
1377b285192aSMauro Carvalho Chehab 				    video_nr[dev->nr]);
1378b285192aSMauro Carvalho Chehab 	if (err < 0) {
1379e39682b5SMauro Carvalho Chehab 		pr_info("%s: can't register video device\n",
1380b285192aSMauro Carvalho Chehab 			dev->name);
1381b285192aSMauro Carvalho Chehab 		goto fail_unreg;
1382b285192aSMauro Carvalho Chehab 	}
1383e39682b5SMauro Carvalho Chehab 	pr_info("%s: registered device %s [v4l2]\n",
1384b285192aSMauro Carvalho Chehab 	       dev->name, video_device_node_name(dev->video_dev));
1385b285192aSMauro Carvalho Chehab 
1386b285192aSMauro Carvalho Chehab 	/* register VBI device */
1387b285192aSMauro Carvalho Chehab 	dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1388b285192aSMauro Carvalho Chehab 		&cx23885_vbi_template, "vbi");
1389*15126b91SHans Verkuil 	if (!dev->vbi_dev) {
1390*15126b91SHans Verkuil 		err = -ENOMEM;
1391*15126b91SHans Verkuil 		goto fail_unreg;
1392*15126b91SHans Verkuil 	}
1393453afdd9SHans Verkuil 	dev->vbi_dev->queue = &dev->vb2_vbiq;
139421615365SHans Verkuil 	dev->vbi_dev->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
139521615365SHans Verkuil 				    V4L2_CAP_AUDIO | V4L2_CAP_VBI_CAPTURE;
1396fbc9a49cSBrad Love 	switch (dev->board) { /* i2c device tuners */
1397fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
1398fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_HVR5525:
1399fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
1400fbc9a49cSBrad Love 	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
1401fbc9a49cSBrad Love 		dev->vbi_dev->device_caps |= V4L2_CAP_TUNER;
1402fbc9a49cSBrad Love 		break;
1403fbc9a49cSBrad Love 	default:
140421615365SHans Verkuil 		if (dev->tuner_type != TUNER_ABSENT)
140521615365SHans Verkuil 			dev->vbi_dev->device_caps |= V4L2_CAP_TUNER;
1406fbc9a49cSBrad Love 	}
1407b285192aSMauro Carvalho Chehab 	err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1408b285192aSMauro Carvalho Chehab 				    vbi_nr[dev->nr]);
1409b285192aSMauro Carvalho Chehab 	if (err < 0) {
1410e39682b5SMauro Carvalho Chehab 		pr_info("%s: can't register vbi device\n",
1411b285192aSMauro Carvalho Chehab 			dev->name);
1412b285192aSMauro Carvalho Chehab 		goto fail_unreg;
1413b285192aSMauro Carvalho Chehab 	}
1414e39682b5SMauro Carvalho Chehab 	pr_info("%s: registered device %s\n",
1415b285192aSMauro Carvalho Chehab 	       dev->name, video_device_node_name(dev->vbi_dev));
1416b285192aSMauro Carvalho Chehab 
1417b285192aSMauro Carvalho Chehab 	/* Register ALSA audio device */
1418b285192aSMauro Carvalho Chehab 	dev->audio_dev = cx23885_audio_register(dev);
1419b285192aSMauro Carvalho Chehab 
1420b285192aSMauro Carvalho Chehab 	return 0;
1421b285192aSMauro Carvalho Chehab 
1422b285192aSMauro Carvalho Chehab fail_unreg:
1423b285192aSMauro Carvalho Chehab 	cx23885_video_unregister(dev);
1424b285192aSMauro Carvalho Chehab 	return err;
1425b285192aSMauro Carvalho Chehab }
1426