1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the Conexant CX23885 PCIe bridge
5 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
9 #include "cx23885-video.h"
11 #include <linux/init.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/kmod.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/delay.h>
20 #include <linux/kthread.h>
21 #include <asm/div64.h>
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-event.h>
26 #include "cx23885-ioctl.h"
27 #include "tuner-xc2028.h"
29 #include <media/drv-intf/cx25840.h>
31 MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
32 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
33 MODULE_LICENSE("GPL");
35 /* ------------------------------------------------------------------ */
37 static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
38 static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
40 module_param_array(video_nr, int, NULL, 0444);
41 module_param_array(vbi_nr, int, NULL, 0444);
43 MODULE_PARM_DESC(video_nr, "video device numbers");
44 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
46 static unsigned int video_debug;
47 module_param(video_debug, int, 0644);
48 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
50 static unsigned int irq_debug;
51 module_param(irq_debug, int, 0644);
52 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
54 static unsigned int vid_limit = 16;
55 module_param(vid_limit, int, 0644);
56 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
58 #define dprintk(level, fmt, arg...)\
59 do { if (video_debug >= level)\
60 printk(KERN_DEBUG pr_fmt("%s: video:" fmt), \
64 /* ------------------------------------------------------------------- */
67 #define FORMAT_FLAGS_PACKED 0x01
68 static struct cx23885_fmt formats[] = {
70 .name = "4:2:2, packed, YUYV",
71 .fourcc = V4L2_PIX_FMT_YUYV,
73 .flags = FORMAT_FLAGS_PACKED,
77 static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
81 for (i = 0; i < ARRAY_SIZE(formats); i++)
82 if (formats[i].fourcc == fourcc)
87 /* ------------------------------------------------------------------- */
89 void cx23885_video_wakeup(struct cx23885_dev *dev,
90 struct cx23885_dmaqueue *q, u32 count)
92 struct cx23885_buffer *buf;
94 if (list_empty(&q->active))
96 buf = list_entry(q->active.next,
97 struct cx23885_buffer, queue);
99 buf->vb.sequence = q->count++;
100 buf->vb.vb2_buf.timestamp = ktime_get_ns();
101 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf,
102 buf->vb.vb2_buf.index, count, q->count);
103 list_del(&buf->queue);
104 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
107 int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
109 struct v4l2_subdev_format format = {
110 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
111 .format.code = MEDIA_BUS_FMT_FIXED,
114 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
117 v4l2_norm_to_name(norm));
119 if (dev->tvnorm == norm)
122 if (dev->tvnorm != norm) {
123 if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
124 vb2_is_busy(&dev->vb2_mpegq))
130 dev->height = norm_maxh(norm);
131 dev->field = V4L2_FIELD_INTERLACED;
133 call_all(dev, video, s_std, norm);
135 format.format.width = dev->width;
136 format.format.height = dev->height;
137 format.format.field = dev->field;
138 call_all(dev, pad, set_fmt, NULL, &format);
143 static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
145 struct video_device *template,
148 struct video_device *vfd;
149 dprintk(1, "%s()\n", __func__);
151 vfd = video_device_alloc();
155 vfd->v4l2_dev = &dev->v4l2_dev;
156 vfd->release = video_device_release;
157 vfd->lock = &dev->lock;
158 snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
159 cx23885_boards[dev->board].name, type);
160 video_set_drvdata(vfd, dev);
164 int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
166 /* 8 bit registers, 8 bit values */
167 u8 buf[] = { reg, data };
169 struct i2c_msg msg = { .addr = 0x98 >> 1,
170 .flags = 0, .buf = buf, .len = 2 };
172 return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
175 u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
177 /* 8 bit registers, 8 bit values */
182 struct i2c_msg msg[] = {
183 { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
184 { .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
187 ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
189 pr_err("%s() error\n", __func__);
194 static void cx23885_flatiron_dump(struct cx23885_dev *dev)
197 dprintk(1, "Flatiron dump\n");
198 for (i = 0; i < 0x24; i++) {
199 dprintk(1, "FI[%02x] = %02x\n", i,
200 cx23885_flatiron_read(dev, i));
204 static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
207 dprintk(1, "%s(input = %d)\n", __func__, input);
210 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
212 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
216 val |= 0x20; /* Enable clock to delta-sigma and dec filter */
218 cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
221 cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
224 cx23885_flatiron_dump(dev);
229 static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
231 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
233 input, INPUT(input)->vmux,
234 INPUT(input)->gpio0, INPUT(input)->gpio1,
235 INPUT(input)->gpio2, INPUT(input)->gpio3);
238 if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
239 dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
240 dev->board == CX23885_BOARD_MYGICA_X8507) {
241 /* Select Analog TV */
242 if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
243 cx23885_gpio_clear(dev, GPIO_0);
246 /* Tell the internal A/V decoder */
247 v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
248 INPUT(input)->vmux, 0, 0);
250 if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
251 (dev->board == CX23885_BOARD_MPX885) ||
252 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
253 (dev->board == CX23885_BOARD_HAUPPAUGE_IMPACTVCBE) ||
254 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
255 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
256 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1265_K4) ||
257 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
258 (dev->board == CX23885_BOARD_MYGICA_X8507) ||
259 (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) ||
260 (dev->board == CX23885_BOARD_VIEWCAST_260E) ||
261 (dev->board == CX23885_BOARD_VIEWCAST_460E)) {
262 /* Configure audio routing */
263 v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
264 INPUT(input)->amux, 0, 0);
266 if (INPUT(input)->amux == CX25840_AUDIO7)
267 cx23885_flatiron_mux(dev, 1);
268 else if (INPUT(input)->amux == CX25840_AUDIO6)
269 cx23885_flatiron_mux(dev, 2);
275 static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
277 dprintk(1, "%s(input=%d)\n", __func__, input);
279 /* The baseband video core of the cx23885 has two audio inputs.
280 * LR1 and LR2. In almost every single case so far only HVR1xxx
281 * cards we've only ever supported LR1. Time to support LR2,
282 * which is available via the optional white breakout header on
284 * We'll use a could of existing enums in the card struct to allow
285 * devs to specify which baseband input they need, or just default
286 * to what we've always used.
288 if (INPUT(input)->amux == CX25840_AUDIO7)
289 cx23885_flatiron_mux(dev, 1);
290 else if (INPUT(input)->amux == CX25840_AUDIO6)
291 cx23885_flatiron_mux(dev, 2);
293 /* Not specifically defined, assume the default. */
294 cx23885_flatiron_mux(dev, 1);
300 /* ------------------------------------------------------------------ */
301 static int cx23885_start_video_dma(struct cx23885_dev *dev,
302 struct cx23885_dmaqueue *q,
303 struct cx23885_buffer *buf)
305 dprintk(1, "%s()\n", __func__);
307 /* Stop the dma/fifo before we tamper with it's risc programs */
308 cx_clear(VID_A_DMA_CTL, 0x11);
310 /* setup fifo + format */
311 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
312 buf->bpl, buf->risc.dma);
315 cx_write(VID_A_GPCNT_CTL, 3);
319 cx23885_irq_add_enable(dev, 0x01);
320 cx_set(VID_A_INT_MSK, 0x000011);
323 cx_set(DEV_CNTRL2, (1<<5));
324 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
329 static int queue_setup(struct vb2_queue *q,
330 unsigned int *num_buffers, unsigned int *num_planes,
331 unsigned int sizes[], struct device *alloc_devs[])
333 struct cx23885_dev *dev = q->drv_priv;
336 sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
340 static int buffer_prepare(struct vb2_buffer *vb)
342 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
343 struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
344 struct cx23885_buffer *buf =
345 container_of(vbuf, struct cx23885_buffer, vb);
346 u32 line0_offset, line1_offset;
347 struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
350 buf->bpl = (dev->width * dev->fmt->depth) >> 3;
352 if (vb2_plane_size(vb, 0) < dev->height * buf->bpl)
354 vb2_set_plane_payload(vb, 0, dev->height * buf->bpl);
356 switch (dev->field) {
358 cx23885_risc_buffer(dev->pci, &buf->risc,
360 buf->bpl, 0, dev->height);
362 case V4L2_FIELD_BOTTOM:
363 cx23885_risc_buffer(dev->pci, &buf->risc,
365 buf->bpl, 0, dev->height);
367 case V4L2_FIELD_INTERLACED:
368 if (dev->tvnorm & V4L2_STD_525_60)
374 if (cx23885_boards[dev->board].force_bff)
375 /* PAL / SECAM OR 888 in NTSC MODE */
379 /* cx25840 transmits NTSC bottom field first */
380 dprintk(1, "%s() Creating TFF/NTSC risc\n",
382 line0_offset = buf->bpl;
385 /* All other formats are top field first */
386 dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
389 line1_offset = buf->bpl;
391 cx23885_risc_buffer(dev->pci, &buf->risc,
392 sgt->sgl, line0_offset,
397 case V4L2_FIELD_SEQ_TB:
398 cx23885_risc_buffer(dev->pci, &buf->risc,
400 0, buf->bpl * (dev->height >> 1),
404 case V4L2_FIELD_SEQ_BT:
405 cx23885_risc_buffer(dev->pci, &buf->risc,
407 buf->bpl * (dev->height >> 1), 0,
414 dprintk(2, "[%p/%d] buffer_init - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
415 buf, buf->vb.vb2_buf.index,
416 dev->width, dev->height, dev->fmt->depth, dev->fmt->name,
417 (unsigned long)buf->risc.dma);
421 static void buffer_finish(struct vb2_buffer *vb)
423 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
424 struct cx23885_buffer *buf = container_of(vbuf,
425 struct cx23885_buffer, vb);
427 cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
431 * The risc program for each buffer works as follows: it starts with a simple
432 * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
433 * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
436 * This is the risc program of the first buffer to be queued if the active list
437 * is empty and it just keeps DMAing this buffer without generating any
440 * If a new buffer is added then the initial JUMP in the code for that buffer
441 * will generate an interrupt which signals that the previous buffer has been
442 * DMAed successfully and that it can be returned to userspace.
444 * It also sets the final jump of the previous buffer to the start of the new
445 * buffer, thus chaining the new buffer into the DMA chain. This is a single
446 * atomic u32 write, so there is no race condition.
448 * The end-result of all this that you only get an interrupt when a buffer
449 * is ready, so the control flow is very easy.
451 static void buffer_queue(struct vb2_buffer *vb)
453 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
454 struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
455 struct cx23885_buffer *buf = container_of(vbuf,
456 struct cx23885_buffer, vb);
457 struct cx23885_buffer *prev;
458 struct cx23885_dmaqueue *q = &dev->vidq;
461 /* add jump to start */
462 buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
463 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
464 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
465 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
467 spin_lock_irqsave(&dev->slock, flags);
468 if (list_empty(&q->active)) {
469 list_add_tail(&buf->queue, &q->active);
470 dprintk(2, "[%p/%d] buffer_queue - first active\n",
471 buf, buf->vb.vb2_buf.index);
473 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
474 prev = list_entry(q->active.prev, struct cx23885_buffer,
476 list_add_tail(&buf->queue, &q->active);
477 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
478 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
479 buf, buf->vb.vb2_buf.index);
481 spin_unlock_irqrestore(&dev->slock, flags);
484 static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
486 struct cx23885_dev *dev = q->drv_priv;
487 struct cx23885_dmaqueue *dmaq = &dev->vidq;
488 struct cx23885_buffer *buf = list_entry(dmaq->active.next,
489 struct cx23885_buffer, queue);
491 cx23885_start_video_dma(dev, dmaq, buf);
495 static void cx23885_stop_streaming(struct vb2_queue *q)
497 struct cx23885_dev *dev = q->drv_priv;
498 struct cx23885_dmaqueue *dmaq = &dev->vidq;
501 cx_clear(VID_A_DMA_CTL, 0x11);
502 spin_lock_irqsave(&dev->slock, flags);
503 while (!list_empty(&dmaq->active)) {
504 struct cx23885_buffer *buf = list_entry(dmaq->active.next,
505 struct cx23885_buffer, queue);
507 list_del(&buf->queue);
508 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
510 spin_unlock_irqrestore(&dev->slock, flags);
513 static const struct vb2_ops cx23885_video_qops = {
514 .queue_setup = queue_setup,
515 .buf_prepare = buffer_prepare,
516 .buf_finish = buffer_finish,
517 .buf_queue = buffer_queue,
518 .wait_prepare = vb2_ops_wait_prepare,
519 .wait_finish = vb2_ops_wait_finish,
520 .start_streaming = cx23885_start_streaming,
521 .stop_streaming = cx23885_stop_streaming,
524 /* ------------------------------------------------------------------ */
527 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
528 struct v4l2_format *f)
530 struct cx23885_dev *dev = video_drvdata(file);
532 f->fmt.pix.width = dev->width;
533 f->fmt.pix.height = dev->height;
534 f->fmt.pix.field = dev->field;
535 f->fmt.pix.pixelformat = dev->fmt->fourcc;
536 f->fmt.pix.bytesperline =
537 (f->fmt.pix.width * dev->fmt->depth) >> 3;
538 f->fmt.pix.sizeimage =
539 f->fmt.pix.height * f->fmt.pix.bytesperline;
540 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
545 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
546 struct v4l2_format *f)
548 struct cx23885_dev *dev = video_drvdata(file);
549 struct cx23885_fmt *fmt;
550 enum v4l2_field field;
551 unsigned int maxw, maxh;
553 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
557 field = f->fmt.pix.field;
559 maxh = norm_maxh(dev->tvnorm);
561 if (V4L2_FIELD_ANY == field) {
562 field = (f->fmt.pix.height > maxh/2)
563 ? V4L2_FIELD_INTERLACED
569 case V4L2_FIELD_BOTTOM:
572 case V4L2_FIELD_INTERLACED:
573 case V4L2_FIELD_SEQ_TB:
574 case V4L2_FIELD_SEQ_BT:
577 field = V4L2_FIELD_INTERLACED;
581 f->fmt.pix.field = field;
582 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
583 &f->fmt.pix.height, 32, maxh, 0, 0);
584 f->fmt.pix.bytesperline =
585 (f->fmt.pix.width * fmt->depth) >> 3;
586 f->fmt.pix.sizeimage =
587 f->fmt.pix.height * f->fmt.pix.bytesperline;
588 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
593 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
594 struct v4l2_format *f)
596 struct cx23885_dev *dev = video_drvdata(file);
597 struct v4l2_subdev_format format = {
598 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
602 dprintk(2, "%s()\n", __func__);
603 err = vidioc_try_fmt_vid_cap(file, priv, f);
608 if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
609 vb2_is_busy(&dev->vb2_mpegq))
612 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
613 dev->width = f->fmt.pix.width;
614 dev->height = f->fmt.pix.height;
615 dev->field = f->fmt.pix.field;
616 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
617 dev->width, dev->height, dev->field);
618 v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
619 call_all(dev, pad, set_fmt, NULL, &format);
620 v4l2_fill_pix_format(&f->fmt.pix, &format.format);
621 /* set_fmt overwrites f->fmt.pix.field, restore it */
622 f->fmt.pix.field = dev->field;
626 static int vidioc_querycap(struct file *file, void *priv,
627 struct v4l2_capability *cap)
629 struct cx23885_dev *dev = video_drvdata(file);
631 strscpy(cap->driver, "cx23885", sizeof(cap->driver));
632 strscpy(cap->card, cx23885_boards[dev->board].name,
634 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
635 cap->capabilities = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
636 V4L2_CAP_AUDIO | V4L2_CAP_VBI_CAPTURE |
637 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
638 V4L2_CAP_DEVICE_CAPS;
639 if (dev->tuner_type != TUNER_ABSENT)
640 cap->capabilities |= V4L2_CAP_TUNER;
644 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
645 struct v4l2_fmtdesc *f)
647 if (unlikely(f->index >= ARRAY_SIZE(formats)))
650 strscpy(f->description, formats[f->index].name,
651 sizeof(f->description));
652 f->pixelformat = formats[f->index].fourcc;
657 static int vidioc_g_pixelaspect(struct file *file, void *priv,
658 int type, struct v4l2_fract *f)
660 struct cx23885_dev *dev = video_drvdata(file);
661 bool is_50hz = dev->tvnorm & V4L2_STD_625_50;
663 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
666 f->numerator = is_50hz ? 54 : 11;
667 f->denominator = is_50hz ? 59 : 10;
672 static int vidioc_g_selection(struct file *file, void *fh,
673 struct v4l2_selection *sel)
675 struct cx23885_dev *dev = video_drvdata(file);
677 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
680 switch (sel->target) {
681 case V4L2_SEL_TGT_CROP_BOUNDS:
682 case V4L2_SEL_TGT_CROP_DEFAULT:
686 sel->r.height = norm_maxh(dev->tvnorm);
694 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
696 struct cx23885_dev *dev = video_drvdata(file);
697 dprintk(1, "%s()\n", __func__);
703 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
705 struct cx23885_dev *dev = video_drvdata(file);
706 dprintk(1, "%s()\n", __func__);
708 return cx23885_set_tvnorm(dev, tvnorms);
711 int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
713 static const char *iname[] = {
714 [CX23885_VMUX_COMPOSITE1] = "Composite1",
715 [CX23885_VMUX_COMPOSITE2] = "Composite2",
716 [CX23885_VMUX_COMPOSITE3] = "Composite3",
717 [CX23885_VMUX_COMPOSITE4] = "Composite4",
718 [CX23885_VMUX_SVIDEO] = "S-Video",
719 [CX23885_VMUX_COMPONENT] = "Component",
720 [CX23885_VMUX_TELEVISION] = "Television",
721 [CX23885_VMUX_CABLE] = "Cable TV",
722 [CX23885_VMUX_DVB] = "DVB",
723 [CX23885_VMUX_DEBUG] = "for debug only",
726 dprintk(1, "%s()\n", __func__);
729 if (n >= MAX_CX23885_INPUT)
732 if (0 == INPUT(n)->type)
736 i->type = V4L2_INPUT_TYPE_CAMERA;
737 strscpy(i->name, iname[INPUT(n)->type], sizeof(i->name));
738 i->std = CX23885_NORMS;
739 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
740 (CX23885_VMUX_CABLE == INPUT(n)->type)) {
741 i->type = V4L2_INPUT_TYPE_TUNER;
744 /* Two selectable audio inputs for non-tv inputs */
748 if (dev->input == n) {
749 /* enum'd input matches our configured input.
750 * Ask the video decoder to process the call
751 * and give it an oppertunity to update the
754 call_all(dev, video, g_input_status, &i->status);
760 static int vidioc_enum_input(struct file *file, void *priv,
761 struct v4l2_input *i)
763 struct cx23885_dev *dev = video_drvdata(file);
764 dprintk(1, "%s()\n", __func__);
765 return cx23885_enum_input(dev, i);
768 int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
770 struct cx23885_dev *dev = video_drvdata(file);
773 dprintk(1, "%s() returns %d\n", __func__, *i);
777 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
779 return cx23885_get_input(file, priv, i);
782 int cx23885_set_input(struct file *file, void *priv, unsigned int i)
784 struct cx23885_dev *dev = video_drvdata(file);
786 dprintk(1, "%s(%d)\n", __func__, i);
788 if (i >= MAX_CX23885_INPUT) {
789 dprintk(1, "%s() -EINVAL\n", __func__);
793 if (INPUT(i)->type == 0)
796 cx23885_video_mux(dev, i);
798 /* By default establish the default audio input for the card also */
799 /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
800 cx23885_audio_mux(dev, i);
804 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
806 return cx23885_set_input(file, priv, i);
809 static int vidioc_log_status(struct file *file, void *priv)
811 struct cx23885_dev *dev = video_drvdata(file);
813 call_all(dev, core, log_status);
817 static int cx23885_query_audinput(struct file *file, void *priv,
818 struct v4l2_audio *i)
820 static const char *iname[] = {
821 [0] = "Baseband L/R 1",
822 [1] = "Baseband L/R 2",
826 dprintk(1, "%s()\n", __func__);
832 memset(i, 0, sizeof(*i));
834 strscpy(i->name, iname[n], sizeof(i->name));
835 i->capability = V4L2_AUDCAP_STEREO;
840 static int vidioc_enum_audinput(struct file *file, void *priv,
841 struct v4l2_audio *i)
843 return cx23885_query_audinput(file, priv, i);
846 static int vidioc_g_audinput(struct file *file, void *priv,
847 struct v4l2_audio *i)
849 struct cx23885_dev *dev = video_drvdata(file);
851 if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
852 (CX23885_VMUX_CABLE == INPUT(dev->input)->type))
855 i->index = dev->audinput;
856 dprintk(1, "%s(input=%d)\n", __func__, i->index);
858 return cx23885_query_audinput(file, priv, i);
861 static int vidioc_s_audinput(struct file *file, void *priv,
862 const struct v4l2_audio *i)
864 struct cx23885_dev *dev = video_drvdata(file);
866 if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
867 (CX23885_VMUX_CABLE == INPUT(dev->input)->type)) {
868 return i->index != 2 ? -EINVAL : 0;
873 dprintk(1, "%s(%d)\n", __func__, i->index);
875 dev->audinput = i->index;
877 /* Skip the audio defaults from the cards struct, caller wants
878 * directly touch the audio mux hardware. */
879 cx23885_flatiron_mux(dev, dev->audinput + 1);
883 static int vidioc_g_tuner(struct file *file, void *priv,
884 struct v4l2_tuner *t)
886 struct cx23885_dev *dev = video_drvdata(file);
888 if (dev->tuner_type == TUNER_ABSENT)
893 strscpy(t->name, "Television", sizeof(t->name));
895 call_all(dev, tuner, g_tuner, t);
899 static int vidioc_s_tuner(struct file *file, void *priv,
900 const struct v4l2_tuner *t)
902 struct cx23885_dev *dev = video_drvdata(file);
904 if (dev->tuner_type == TUNER_ABSENT)
908 /* Update the A/V core */
909 call_all(dev, tuner, s_tuner, t);
914 static int vidioc_g_frequency(struct file *file, void *priv,
915 struct v4l2_frequency *f)
917 struct cx23885_dev *dev = video_drvdata(file);
919 if (dev->tuner_type == TUNER_ABSENT)
922 f->type = V4L2_TUNER_ANALOG_TV;
923 f->frequency = dev->freq;
925 call_all(dev, tuner, g_frequency, f);
930 static int cx23885_set_freq(struct cx23885_dev *dev, const struct v4l2_frequency *f)
932 struct v4l2_ctrl *mute;
933 int old_mute_val = 1;
935 if (dev->tuner_type == TUNER_ABSENT)
937 if (unlikely(f->tuner != 0))
940 dev->freq = f->frequency;
942 /* I need to mute audio here */
943 mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
945 old_mute_val = v4l2_ctrl_g_ctrl(mute);
947 v4l2_ctrl_s_ctrl(mute, 1);
950 call_all(dev, tuner, s_frequency, f);
952 /* When changing channels it is required to reset TVAUDIO */
955 /* I need to unmute audio here */
956 if (old_mute_val == 0)
957 v4l2_ctrl_s_ctrl(mute, old_mute_val);
962 static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
963 const struct v4l2_frequency *f)
965 struct v4l2_ctrl *mute;
966 int old_mute_val = 1;
967 struct vb2_dvb_frontend *vfe;
968 struct dvb_frontend *fe;
970 struct analog_parameters params = {
971 .mode = V4L2_TUNER_ANALOG_TV,
972 .audmode = V4L2_TUNER_MODE_STEREO,
974 .frequency = f->frequency
977 dev->freq = f->frequency;
979 /* I need to mute audio here */
980 mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
982 old_mute_val = v4l2_ctrl_g_ctrl(mute);
984 v4l2_ctrl_s_ctrl(mute, 1);
988 dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
989 params.frequency, f->tuner, params.std);
991 vfe = vb2_dvb_get_frontend(&dev->ts2.frontends, 1);
996 fe = vfe->dvb.frontend;
998 if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
999 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
1000 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
1001 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1265_K4))
1002 fe = &dev->ts1.analog_fe;
1004 if (fe && fe->ops.tuner_ops.set_analog_params) {
1005 call_all(dev, video, s_std, dev->tvnorm);
1006 fe->ops.tuner_ops.set_analog_params(fe, ¶ms);
1009 pr_err("%s() No analog tuner, aborting\n", __func__);
1011 /* When changing channels it is required to reset TVAUDIO */
1014 /* I need to unmute audio here */
1015 if (old_mute_val == 0)
1016 v4l2_ctrl_s_ctrl(mute, old_mute_val);
1021 int cx23885_set_frequency(struct file *file, void *priv,
1022 const struct v4l2_frequency *f)
1024 struct cx23885_dev *dev = video_drvdata(file);
1027 switch (dev->board) {
1028 case CX23885_BOARD_HAUPPAUGE_HVR1255:
1029 case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1030 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
1031 case CX23885_BOARD_HAUPPAUGE_HVR1850:
1032 ret = cx23885_set_freq_via_ops(dev, f);
1035 ret = cx23885_set_freq(dev, f);
1041 static int vidioc_s_frequency(struct file *file, void *priv,
1042 const struct v4l2_frequency *f)
1044 return cx23885_set_frequency(file, priv, f);
1047 /* ----------------------------------------------------------- */
1049 int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1054 mask = cx_read(VID_A_INT_MSK);
1055 if (0 == (status & mask))
1058 cx_write(VID_A_INT_STAT, status);
1060 /* risc op code error, fifo overflow or line sync detection error */
1061 if ((status & VID_BC_MSK_OPC_ERR) ||
1062 (status & VID_BC_MSK_SYNC) ||
1063 (status & VID_BC_MSK_OF)) {
1065 if (status & VID_BC_MSK_OPC_ERR) {
1066 dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1067 VID_BC_MSK_OPC_ERR);
1068 pr_warn("%s: video risc op code error\n",
1070 cx23885_sram_channel_dump(dev,
1071 &dev->sram_channels[SRAM_CH01]);
1074 if (status & VID_BC_MSK_SYNC)
1075 dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) video lines miss-match\n",
1078 if (status & VID_BC_MSK_OF)
1079 dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1085 if (status & VID_BC_MSK_RISCI1) {
1086 spin_lock(&dev->slock);
1087 count = cx_read(VID_A_GPCNT);
1088 cx23885_video_wakeup(dev, &dev->vidq, count);
1089 spin_unlock(&dev->slock);
1093 /* Allow the VBI framework to process it's payload */
1094 handled += cx23885_vbi_irq(dev, status);
1099 /* ----------------------------------------------------------- */
1100 /* exported stuff */
1102 static const struct v4l2_file_operations video_fops = {
1103 .owner = THIS_MODULE,
1104 .open = v4l2_fh_open,
1105 .release = vb2_fop_release,
1106 .read = vb2_fop_read,
1107 .poll = vb2_fop_poll,
1108 .unlocked_ioctl = video_ioctl2,
1109 .mmap = vb2_fop_mmap,
1112 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1113 .vidioc_querycap = vidioc_querycap,
1114 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1115 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1116 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1117 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1118 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1119 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1120 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
1121 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1122 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1123 .vidioc_querybuf = vb2_ioctl_querybuf,
1124 .vidioc_qbuf = vb2_ioctl_qbuf,
1125 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1126 .vidioc_streamon = vb2_ioctl_streamon,
1127 .vidioc_streamoff = vb2_ioctl_streamoff,
1128 .vidioc_g_pixelaspect = vidioc_g_pixelaspect,
1129 .vidioc_g_selection = vidioc_g_selection,
1130 .vidioc_s_std = vidioc_s_std,
1131 .vidioc_g_std = vidioc_g_std,
1132 .vidioc_enum_input = vidioc_enum_input,
1133 .vidioc_g_input = vidioc_g_input,
1134 .vidioc_s_input = vidioc_s_input,
1135 .vidioc_log_status = vidioc_log_status,
1136 .vidioc_g_tuner = vidioc_g_tuner,
1137 .vidioc_s_tuner = vidioc_s_tuner,
1138 .vidioc_g_frequency = vidioc_g_frequency,
1139 .vidioc_s_frequency = vidioc_s_frequency,
1140 #ifdef CONFIG_VIDEO_ADV_DEBUG
1141 .vidioc_g_chip_info = cx23885_g_chip_info,
1142 .vidioc_g_register = cx23885_g_register,
1143 .vidioc_s_register = cx23885_s_register,
1145 .vidioc_enumaudio = vidioc_enum_audinput,
1146 .vidioc_g_audio = vidioc_g_audinput,
1147 .vidioc_s_audio = vidioc_s_audinput,
1148 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1149 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1152 static struct video_device cx23885_vbi_template;
1153 static struct video_device cx23885_video_template = {
1154 .name = "cx23885-video",
1155 .fops = &video_fops,
1156 .ioctl_ops = &video_ioctl_ops,
1157 .tvnorms = CX23885_NORMS,
1160 void cx23885_video_unregister(struct cx23885_dev *dev)
1162 dprintk(1, "%s()\n", __func__);
1163 cx23885_irq_remove(dev, 0x01);
1166 if (video_is_registered(dev->vbi_dev))
1167 video_unregister_device(dev->vbi_dev);
1169 video_device_release(dev->vbi_dev);
1170 dev->vbi_dev = NULL;
1172 if (dev->video_dev) {
1173 if (video_is_registered(dev->video_dev))
1174 video_unregister_device(dev->video_dev);
1176 video_device_release(dev->video_dev);
1177 dev->video_dev = NULL;
1181 cx23885_audio_unregister(dev);
1184 int cx23885_video_register(struct cx23885_dev *dev)
1186 struct vb2_queue *q;
1189 dprintk(1, "%s()\n", __func__);
1191 /* Initialize VBI template */
1192 cx23885_vbi_template = cx23885_video_template;
1193 strscpy(cx23885_vbi_template.name, "cx23885-vbi",
1194 sizeof(cx23885_vbi_template.name));
1196 dev->tvnorm = V4L2_STD_NTSC_M;
1197 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
1198 dev->field = V4L2_FIELD_INTERLACED;
1200 dev->height = norm_maxh(dev->tvnorm);
1202 /* init video dma queues */
1203 INIT_LIST_HEAD(&dev->vidq.active);
1205 /* init vbi dma queues */
1206 INIT_LIST_HEAD(&dev->vbiq.active);
1208 cx23885_irq_add_enable(dev, 0x01);
1210 if ((TUNER_ABSENT != dev->tuner_type) &&
1211 ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1212 struct v4l2_subdev *sd = NULL;
1214 if (dev->tuner_addr)
1215 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1216 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1217 "tuner", dev->tuner_addr, NULL);
1219 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1220 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1221 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1223 struct tuner_setup tun_setup;
1225 memset(&tun_setup, 0, sizeof(tun_setup));
1226 tun_setup.mode_mask = T_ANALOG_TV;
1227 tun_setup.type = dev->tuner_type;
1228 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1229 tun_setup.tuner_callback = cx23885_tuner_callback;
1231 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1233 if ((dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) ||
1234 (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200)) {
1235 struct xc2028_ctrl ctrl = {
1236 .fname = XC2028_DEFAULT_FIRMWARE,
1239 struct v4l2_priv_tun_config cfg = {
1240 .tuner = dev->tuner_type,
1243 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1246 if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
1247 struct xc2028_ctrl ctrl = {
1248 .fname = "xc3028L-v36.fw",
1251 struct v4l2_priv_tun_config cfg = {
1252 .tuner = dev->tuner_type,
1255 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1260 /* initial device configuration */
1261 mutex_lock(&dev->lock);
1262 cx23885_set_tvnorm(dev, dev->tvnorm);
1263 cx23885_video_mux(dev, 0);
1264 cx23885_audio_mux(dev, 0);
1265 mutex_unlock(&dev->lock);
1268 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1269 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1270 q->gfp_flags = GFP_DMA32;
1271 q->min_buffers_needed = 2;
1273 q->buf_struct_size = sizeof(struct cx23885_buffer);
1274 q->ops = &cx23885_video_qops;
1275 q->mem_ops = &vb2_dma_sg_memops;
1276 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1277 q->lock = &dev->lock;
1278 q->dev = &dev->pci->dev;
1280 err = vb2_queue_init(q);
1285 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1286 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1287 q->gfp_flags = GFP_DMA32;
1288 q->min_buffers_needed = 2;
1290 q->buf_struct_size = sizeof(struct cx23885_buffer);
1291 q->ops = &cx23885_vbi_qops;
1292 q->mem_ops = &vb2_dma_sg_memops;
1293 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1294 q->lock = &dev->lock;
1295 q->dev = &dev->pci->dev;
1297 err = vb2_queue_init(q);
1301 /* register Video device */
1302 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1303 &cx23885_video_template, "video");
1304 dev->video_dev->queue = &dev->vb2_vidq;
1305 dev->video_dev->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1306 V4L2_CAP_AUDIO | V4L2_CAP_VIDEO_CAPTURE;
1307 if (dev->tuner_type != TUNER_ABSENT)
1308 dev->video_dev->device_caps |= V4L2_CAP_TUNER;
1309 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1312 pr_info("%s: can't register video device\n",
1316 pr_info("%s: registered device %s [v4l2]\n",
1317 dev->name, video_device_node_name(dev->video_dev));
1319 /* register VBI device */
1320 dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1321 &cx23885_vbi_template, "vbi");
1322 dev->vbi_dev->queue = &dev->vb2_vbiq;
1323 dev->vbi_dev->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1324 V4L2_CAP_AUDIO | V4L2_CAP_VBI_CAPTURE;
1325 if (dev->tuner_type != TUNER_ABSENT)
1326 dev->vbi_dev->device_caps |= V4L2_CAP_TUNER;
1327 err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1330 pr_info("%s: can't register vbi device\n",
1334 pr_info("%s: registered device %s\n",
1335 dev->name, video_device_node_name(dev->vbi_dev));
1337 /* Register ALSA audio device */
1338 dev->audio_dev = cx23885_audio_register(dev);
1343 cx23885_video_unregister(dev);