1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * tw68 functions to handle video data
5 * Much of this code is derived from the cx88 and sa7134 drivers, which
6 * were in turn derived from the bt87x driver. The original work was by
7 * Gerd Knorr; more recently the code was enhanced by Mauro Carvalho Chehab,
8 * Hans Verkuil, Andy Walls and many others. Their work is gratefully
9 * acknowledged. Full credit goes to them - any problems within this code
12 * Copyright (C) 2009 William M. Brack
14 * Refactored and updated to the latest v4l core frameworks:
16 * Copyright (C) 2014 Hans Verkuil <hverkuil@xs4all.nl>
19 #include <linux/module.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-event.h>
22 #include <media/videobuf2-dma-sg.h>
27 /* ------------------------------------------------------------------ */
28 /* data structs for video */
31 * Note that the saa7134 has formats, e.g. YUV420, which are classified
32 * as "planar". These affect overlay mode, and are flagged with a field
33 * ".planar" in the format. Do we need to implement this in this driver?
35 static const struct tw68_format formats[] = {
37 .name = "15 bpp RGB, le",
38 .fourcc = V4L2_PIX_FMT_RGB555,
40 .twformat = ColorFormatRGB15,
42 .name = "15 bpp RGB, be",
43 .fourcc = V4L2_PIX_FMT_RGB555X,
45 .twformat = ColorFormatRGB15 | ColorFormatBSWAP,
47 .name = "16 bpp RGB, le",
48 .fourcc = V4L2_PIX_FMT_RGB565,
50 .twformat = ColorFormatRGB16,
52 .name = "16 bpp RGB, be",
53 .fourcc = V4L2_PIX_FMT_RGB565X,
55 .twformat = ColorFormatRGB16 | ColorFormatBSWAP,
57 .name = "24 bpp RGB, le",
58 .fourcc = V4L2_PIX_FMT_BGR24,
60 .twformat = ColorFormatRGB24,
62 .name = "24 bpp RGB, be",
63 .fourcc = V4L2_PIX_FMT_RGB24,
65 .twformat = ColorFormatRGB24 | ColorFormatBSWAP,
67 .name = "32 bpp RGB, le",
68 .fourcc = V4L2_PIX_FMT_BGR32,
70 .twformat = ColorFormatRGB32,
72 .name = "32 bpp RGB, be",
73 .fourcc = V4L2_PIX_FMT_RGB32,
75 .twformat = ColorFormatRGB32 | ColorFormatBSWAP |
78 .name = "4:2:2 packed, YUYV",
79 .fourcc = V4L2_PIX_FMT_YUYV,
81 .twformat = ColorFormatYUY2,
83 .name = "4:2:2 packed, UYVY",
84 .fourcc = V4L2_PIX_FMT_UYVY,
86 .twformat = ColorFormatYUY2 | ColorFormatBSWAP,
89 #define FORMATS ARRAY_SIZE(formats)
99 .video_v_start = 24, \
100 .video_v_stop = 311, \
103 #define NORM_525_60 \
109 .vbi_v_start_0 = 10, \
110 .vbi_v_stop_0 = 21, \
111 .video_v_start = 22, \
112 .video_v_stop = 262, \
116 * The following table is searched by tw68_s_std, first for a specific
117 * match, then for an entry which contains the desired id. The table
118 * entries should therefore be ordered in ascending order of specificity.
120 static const struct tw68_tvnorm tvnorms[] = {
122 .name = "PAL", /* autodetect */
126 .sync_control = 0x18,
127 .luma_control = 0x40,
128 .chroma_ctrl1 = 0x81,
130 .chroma_ctrl2 = 0x06,
132 .format = VideoFormatPALBDGHI,
138 .sync_control = 0x59,
139 .luma_control = 0x40,
140 .chroma_ctrl1 = 0x89,
142 .chroma_ctrl2 = 0x0e,
144 .format = VideoFormatNTSC,
147 .id = V4L2_STD_SECAM,
150 .sync_control = 0x18,
151 .luma_control = 0x1b,
152 .chroma_ctrl1 = 0xd1,
154 .chroma_ctrl2 = 0x00,
156 .format = VideoFormatSECAM,
159 .id = V4L2_STD_PAL_M,
162 .sync_control = 0x59,
163 .luma_control = 0x40,
164 .chroma_ctrl1 = 0xb9,
166 .chroma_ctrl2 = 0x0e,
168 .format = VideoFormatPALM,
171 .id = V4L2_STD_PAL_Nc,
174 .sync_control = 0x18,
175 .luma_control = 0x40,
176 .chroma_ctrl1 = 0xa1,
178 .chroma_ctrl2 = 0x06,
180 .format = VideoFormatPALNC,
183 .id = V4L2_STD_PAL_60,
192 .vbi_v_start_1 = 273,
194 .sync_control = 0x18,
195 .luma_control = 0x40,
196 .chroma_ctrl1 = 0x81,
198 .chroma_ctrl2 = 0x06,
200 .format = VideoFormatPAL60,
203 #define TVNORMS ARRAY_SIZE(tvnorms)
205 static const struct tw68_format *format_by_fourcc(unsigned int fourcc)
209 for (i = 0; i < FORMATS; i++)
210 if (formats[i].fourcc == fourcc)
216 /* ------------------------------------------------------------------ */
218 * Note that the cropping rectangles are described in terms of a single
219 * frame, i.e. line positions are only 1/2 the interlaced equivalent
221 static void set_tvnorm(struct tw68_dev *dev, const struct tw68_tvnorm *norm)
223 if (norm != dev->tvnorm) {
225 dev->height = (norm->id & V4L2_STD_525_60) ? 480 : 576;
227 tw68_set_tvnorm_hw(dev);
234 * Scaling and Cropping for video decoding
236 * We are working with 3 values for horizontal and vertical - scale,
239 * HACTIVE represent the actual number of pixels in the "usable" image,
240 * before scaling. HDELAY represents the number of pixels skipped
241 * between the start of the horizontal sync and the start of the image.
242 * HSCALE is calculated using the formula
243 * HSCALE = (HACTIVE / (#pixels desired)) * 256
245 * The vertical registers are similar, except based upon the total number
246 * of lines in the image, and the first line of the image (i.e. ignoring
247 * vertical sync and VBI).
249 * Note that the number of bytes reaching the FIFO (and hence needing
250 * to be processed by the DMAP program) is completely dependent upon
251 * these values, especially HSCALE.
254 * @dev pointer to the device structure, needed for
255 * getting current norm (as well as debug print)
256 * @width actual image width (from user buffer)
257 * @height actual image height
258 * @field indicates Top, Bottom or Interlaced
260 static int tw68_set_scale(struct tw68_dev *dev, unsigned int width,
261 unsigned int height, enum v4l2_field field)
263 const struct tw68_tvnorm *norm = dev->tvnorm;
264 /* set individually for debugging clarity */
265 int hactive, hdelay, hscale;
266 int vactive, vdelay, vscale;
269 if (V4L2_FIELD_HAS_BOTH(field)) /* if field is interlaced */
270 height /= 2; /* we must set for 1-frame */
272 pr_debug("%s: width=%d, height=%d, both=%d\n"
273 " tvnorm h_delay=%d, h_start=%d, h_stop=%d, v_delay=%d, v_start=%d, v_stop=%d\n",
274 __func__, width, height, V4L2_FIELD_HAS_BOTH(field),
275 norm->h_delay, norm->h_start, norm->h_stop,
276 norm->v_delay, norm->video_v_start,
279 switch (dev->vdecoder) {
281 hdelay = norm->h_delay0;
284 hdelay = norm->h_delay;
288 hdelay += norm->h_start;
289 hactive = norm->h_stop - norm->h_start + 1;
291 hscale = (hactive * 256) / (width);
293 vdelay = norm->v_delay;
294 vactive = ((norm->id & V4L2_STD_525_60) ? 524 : 624) / 2 - norm->video_v_start;
295 vscale = (vactive * 256) / height;
297 pr_debug("%s: %dx%d [%s%s,%s]\n", __func__,
299 V4L2_FIELD_HAS_TOP(field) ? "T" : "",
300 V4L2_FIELD_HAS_BOTTOM(field) ? "B" : "",
301 v4l2_norm_to_name(dev->tvnorm->id));
302 pr_debug("%s: hactive=%d, hdelay=%d, hscale=%d; vactive=%d, vdelay=%d, vscale=%d\n",
304 hactive, hdelay, hscale, vactive, vdelay, vscale);
306 comb = ((vdelay & 0x300) >> 2) |
307 ((vactive & 0x300) >> 4) |
308 ((hdelay & 0x300) >> 6) |
309 ((hactive & 0x300) >> 8);
310 pr_debug("%s: setting CROP_HI=%02x, VDELAY_LO=%02x, VACTIVE_LO=%02x, HDELAY_LO=%02x, HACTIVE_LO=%02x\n",
311 __func__, comb, vdelay, vactive, hdelay, hactive);
312 tw_writeb(TW68_CROP_HI, comb);
313 tw_writeb(TW68_VDELAY_LO, vdelay & 0xff);
314 tw_writeb(TW68_VACTIVE_LO, vactive & 0xff);
315 tw_writeb(TW68_HDELAY_LO, hdelay & 0xff);
316 tw_writeb(TW68_HACTIVE_LO, hactive & 0xff);
318 comb = ((vscale & 0xf00) >> 4) | ((hscale & 0xf00) >> 8);
319 pr_debug("%s: setting SCALE_HI=%02x, VSCALE_LO=%02x, HSCALE_LO=%02x\n",
320 __func__, comb, vscale, hscale);
321 tw_writeb(TW68_SCALE_HI, comb);
322 tw_writeb(TW68_VSCALE_LO, vscale);
323 tw_writeb(TW68_HSCALE_LO, hscale);
328 /* ------------------------------------------------------------------ */
330 int tw68_video_start_dma(struct tw68_dev *dev, struct tw68_buf *buf)
332 /* Set cropping and scaling */
333 tw68_set_scale(dev, dev->width, dev->height, dev->field);
335 * Set start address for RISC program. Note that if the DMAP
336 * processor is currently running, it must be stopped before
337 * a new address can be set.
339 tw_clearl(TW68_DMAC, TW68_DMAP_EN);
340 tw_writel(TW68_DMAP_SA, buf->dma);
341 /* Clear any pending interrupts */
342 tw_writel(TW68_INTSTAT, dev->board_virqmask);
343 /* Enable the risc engine and the fifo */
344 tw_andorl(TW68_DMAC, 0xff, dev->fmt->twformat |
345 ColorFormatGamma | TW68_DMAP_EN | TW68_FIFO_EN);
346 dev->pci_irqmask |= dev->board_virqmask;
347 tw_setl(TW68_INTMASK, dev->pci_irqmask);
351 /* ------------------------------------------------------------------ */
353 /* calc max # of buffers from size (must not exceed the 4MB virtual
354 * address space per DMA channel) */
355 static int tw68_buffer_count(unsigned int size, unsigned int count)
357 unsigned int maxcount;
359 maxcount = (4 * 1024 * 1024) / roundup(size, PAGE_SIZE);
360 if (count > maxcount)
365 /* ------------------------------------------------------------- */
366 /* vb2 queue operations */
368 static int tw68_queue_setup(struct vb2_queue *q,
369 unsigned int *num_buffers, unsigned int *num_planes,
370 unsigned int sizes[], struct device *alloc_devs[])
372 struct tw68_dev *dev = vb2_get_drv_priv(q);
373 unsigned tot_bufs = q->num_buffers + *num_buffers;
374 unsigned size = (dev->fmt->depth * dev->width * dev->height) >> 3;
378 tot_bufs = tw68_buffer_count(size, tot_bufs);
379 *num_buffers = tot_bufs - q->num_buffers;
381 * We allow create_bufs, but only if the sizeimage is >= as the
382 * current sizeimage. The tw68_buffer_count calculation becomes quite
383 * difficult otherwise.
386 return sizes[0] < size ? -EINVAL : 0;
394 * The risc program for each buffers works as follows: it starts with a simple
395 * 'JUMP to addr + 8', which is effectively a NOP. Then the program to DMA the
396 * buffer follows and at the end we have a JUMP back to the start + 8 (skipping
399 * This is the program of the first buffer to be queued if the active list is
400 * empty and it just keeps DMAing this buffer without generating any interrupts.
402 * If a new buffer is added then the initial JUMP in the program generates an
403 * interrupt as well which signals that the previous buffer has been DMAed
404 * successfully and that it can be returned to userspace.
406 * It also sets the final jump of the previous buffer to the start of the new
407 * buffer, thus chaining the new buffer into the DMA chain. This is a single
408 * atomic u32 write, so there is no race condition.
410 * The end-result of all this that you only get an interrupt when a buffer
411 * is ready, so the control flow is very easy.
413 static void tw68_buf_queue(struct vb2_buffer *vb)
415 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
416 struct vb2_queue *vq = vb->vb2_queue;
417 struct tw68_dev *dev = vb2_get_drv_priv(vq);
418 struct tw68_buf *buf = container_of(vbuf, struct tw68_buf, vb);
419 struct tw68_buf *prev;
422 spin_lock_irqsave(&dev->slock, flags);
424 /* append a 'JUMP to start of buffer' to the buffer risc program */
425 buf->jmp[0] = cpu_to_le32(RISC_JUMP);
426 buf->jmp[1] = cpu_to_le32(buf->dma + 8);
428 if (!list_empty(&dev->active)) {
429 prev = list_entry(dev->active.prev, struct tw68_buf, list);
430 buf->cpu[0] |= cpu_to_le32(RISC_INT_BIT);
431 prev->jmp[1] = cpu_to_le32(buf->dma);
433 list_add_tail(&buf->list, &dev->active);
434 spin_unlock_irqrestore(&dev->slock, flags);
440 * Set the ancillary information into the buffer structure. This
441 * includes generating the necessary risc program if it hasn't already
442 * been done for the current buffer format.
443 * The structure fh contains the details of the format requested by the
444 * user - type, width, height and #fields. This is compared with the
445 * last format set for the current buffer. If they differ, the risc
446 * code (which controls the filling of the buffer) is (re-)generated.
448 static int tw68_buf_prepare(struct vb2_buffer *vb)
450 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
451 struct vb2_queue *vq = vb->vb2_queue;
452 struct tw68_dev *dev = vb2_get_drv_priv(vq);
453 struct tw68_buf *buf = container_of(vbuf, struct tw68_buf, vb);
454 struct sg_table *dma = vb2_dma_sg_plane_desc(vb, 0);
457 size = (dev->width * dev->height * dev->fmt->depth) >> 3;
458 if (vb2_plane_size(vb, 0) < size)
460 vb2_set_plane_payload(vb, 0, size);
462 bpl = (dev->width * dev->fmt->depth) >> 3;
463 switch (dev->field) {
465 tw68_risc_buffer(dev->pci, buf, dma->sgl,
466 0, UNSET, bpl, 0, dev->height);
468 case V4L2_FIELD_BOTTOM:
469 tw68_risc_buffer(dev->pci, buf, dma->sgl,
470 UNSET, 0, bpl, 0, dev->height);
472 case V4L2_FIELD_SEQ_TB:
473 tw68_risc_buffer(dev->pci, buf, dma->sgl,
474 0, bpl * (dev->height >> 1),
475 bpl, 0, dev->height >> 1);
477 case V4L2_FIELD_SEQ_BT:
478 tw68_risc_buffer(dev->pci, buf, dma->sgl,
479 bpl * (dev->height >> 1), 0,
480 bpl, 0, dev->height >> 1);
482 case V4L2_FIELD_INTERLACED:
484 tw68_risc_buffer(dev->pci, buf, dma->sgl,
485 0, bpl, bpl, bpl, dev->height >> 1);
491 static void tw68_buf_finish(struct vb2_buffer *vb)
493 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
494 struct vb2_queue *vq = vb->vb2_queue;
495 struct tw68_dev *dev = vb2_get_drv_priv(vq);
496 struct tw68_buf *buf = container_of(vbuf, struct tw68_buf, vb);
498 pci_free_consistent(dev->pci, buf->size, buf->cpu, buf->dma);
501 static int tw68_start_streaming(struct vb2_queue *q, unsigned int count)
503 struct tw68_dev *dev = vb2_get_drv_priv(q);
504 struct tw68_buf *buf =
505 container_of(dev->active.next, struct tw68_buf, list);
508 tw68_video_start_dma(dev, buf);
512 static void tw68_stop_streaming(struct vb2_queue *q)
514 struct tw68_dev *dev = vb2_get_drv_priv(q);
516 /* Stop risc & fifo */
517 tw_clearl(TW68_DMAC, TW68_DMAP_EN | TW68_FIFO_EN);
518 while (!list_empty(&dev->active)) {
519 struct tw68_buf *buf =
520 container_of(dev->active.next, struct tw68_buf, list);
522 list_del(&buf->list);
523 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
527 static const struct vb2_ops tw68_video_qops = {
528 .queue_setup = tw68_queue_setup,
529 .buf_queue = tw68_buf_queue,
530 .buf_prepare = tw68_buf_prepare,
531 .buf_finish = tw68_buf_finish,
532 .start_streaming = tw68_start_streaming,
533 .stop_streaming = tw68_stop_streaming,
534 .wait_prepare = vb2_ops_wait_prepare,
535 .wait_finish = vb2_ops_wait_finish,
538 /* ------------------------------------------------------------------ */
540 static int tw68_s_ctrl(struct v4l2_ctrl *ctrl)
542 struct tw68_dev *dev =
543 container_of(ctrl->handler, struct tw68_dev, hdl);
546 case V4L2_CID_BRIGHTNESS:
547 tw_writeb(TW68_BRIGHT, ctrl->val);
550 tw_writeb(TW68_HUE, ctrl->val);
552 case V4L2_CID_CONTRAST:
553 tw_writeb(TW68_CONTRAST, ctrl->val);
555 case V4L2_CID_SATURATION:
556 tw_writeb(TW68_SAT_U, ctrl->val);
557 tw_writeb(TW68_SAT_V, ctrl->val);
559 case V4L2_CID_COLOR_KILLER:
561 tw_andorb(TW68_MISC2, 0xe0, 0xe0);
563 tw_andorb(TW68_MISC2, 0xe0, 0x00);
565 case V4L2_CID_CHROMA_AGC:
567 tw_andorb(TW68_LOOP, 0x30, 0x20);
569 tw_andorb(TW68_LOOP, 0x30, 0x00);
575 /* ------------------------------------------------------------------ */
578 * Note that this routine returns what is stored in the fh structure, and
579 * does not interrogate any of the device registers.
581 static int tw68_g_fmt_vid_cap(struct file *file, void *priv,
582 struct v4l2_format *f)
584 struct tw68_dev *dev = video_drvdata(file);
586 f->fmt.pix.width = dev->width;
587 f->fmt.pix.height = dev->height;
588 f->fmt.pix.field = dev->field;
589 f->fmt.pix.pixelformat = dev->fmt->fourcc;
590 f->fmt.pix.bytesperline =
591 (f->fmt.pix.width * (dev->fmt->depth)) >> 3;
592 f->fmt.pix.sizeimage =
593 f->fmt.pix.height * f->fmt.pix.bytesperline;
594 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
599 static int tw68_try_fmt_vid_cap(struct file *file, void *priv,
600 struct v4l2_format *f)
602 struct tw68_dev *dev = video_drvdata(file);
603 const struct tw68_format *fmt;
604 enum v4l2_field field;
607 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
611 field = f->fmt.pix.field;
612 maxh = (dev->tvnorm->id & V4L2_STD_525_60) ? 480 : 576;
616 case V4L2_FIELD_BOTTOM:
618 case V4L2_FIELD_INTERLACED:
619 case V4L2_FIELD_SEQ_BT:
620 case V4L2_FIELD_SEQ_TB:
624 field = (f->fmt.pix.height > maxh / 2)
625 ? V4L2_FIELD_INTERLACED
630 f->fmt.pix.field = field;
631 if (f->fmt.pix.width < 48)
632 f->fmt.pix.width = 48;
633 if (f->fmt.pix.height < 32)
634 f->fmt.pix.height = 32;
635 if (f->fmt.pix.width > 720)
636 f->fmt.pix.width = 720;
637 if (f->fmt.pix.height > maxh)
638 f->fmt.pix.height = maxh;
639 f->fmt.pix.width &= ~0x03;
640 f->fmt.pix.bytesperline =
641 (f->fmt.pix.width * (fmt->depth)) >> 3;
642 f->fmt.pix.sizeimage =
643 f->fmt.pix.height * f->fmt.pix.bytesperline;
644 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
649 * Note that tw68_s_fmt_vid_cap sets the information into the fh structure,
650 * and it will be used for all future new buffers. However, there could be
651 * some number of buffers on the "active" chain which will be filled before
652 * the change takes place.
654 static int tw68_s_fmt_vid_cap(struct file *file, void *priv,
655 struct v4l2_format *f)
657 struct tw68_dev *dev = video_drvdata(file);
660 err = tw68_try_fmt_vid_cap(file, priv, f);
664 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
665 dev->width = f->fmt.pix.width;
666 dev->height = f->fmt.pix.height;
667 dev->field = f->fmt.pix.field;
671 static int tw68_enum_input(struct file *file, void *priv,
672 struct v4l2_input *i)
674 struct tw68_dev *dev = video_drvdata(file);
678 if (n >= TW68_INPUT_MAX)
681 i->type = V4L2_INPUT_TYPE_CAMERA;
682 snprintf(i->name, sizeof(i->name), "Composite %d", n);
684 /* If the query is for the current input, get live data */
685 if (n == dev->input) {
686 int v1 = tw_readb(TW68_STATUS1);
687 int v2 = tw_readb(TW68_MVSN);
689 if (0 != (v1 & (1 << 7)))
690 i->status |= V4L2_IN_ST_NO_SYNC;
691 if (0 != (v1 & (1 << 6)))
692 i->status |= V4L2_IN_ST_NO_H_LOCK;
693 if (0 != (v1 & (1 << 2)))
694 i->status |= V4L2_IN_ST_NO_SIGNAL;
695 if (0 != (v1 & 1 << 1))
696 i->status |= V4L2_IN_ST_NO_COLOR;
697 if (0 != (v2 & (1 << 2)))
698 i->status |= V4L2_IN_ST_MACROVISION;
700 i->std = video_devdata(file)->tvnorms;
704 static int tw68_g_input(struct file *file, void *priv, unsigned int *i)
706 struct tw68_dev *dev = video_drvdata(file);
712 static int tw68_s_input(struct file *file, void *priv, unsigned int i)
714 struct tw68_dev *dev = video_drvdata(file);
716 if (i >= TW68_INPUT_MAX)
719 tw_andorb(TW68_INFORM, 0x03 << 2, dev->input << 2);
723 static int tw68_querycap(struct file *file, void *priv,
724 struct v4l2_capability *cap)
726 struct tw68_dev *dev = video_drvdata(file);
728 strscpy(cap->driver, "tw68", sizeof(cap->driver));
729 strscpy(cap->card, "Techwell Capture Card",
731 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
735 static int tw68_s_std(struct file *file, void *priv, v4l2_std_id id)
737 struct tw68_dev *dev = video_drvdata(file);
740 if (vb2_is_busy(&dev->vidq))
743 /* Look for match on complete norm id (may have mult bits) */
744 for (i = 0; i < TVNORMS; i++) {
745 if (id == tvnorms[i].id)
749 /* If no exact match, look for norm which contains this one */
751 for (i = 0; i < TVNORMS; i++)
752 if (id & tvnorms[i].id)
755 /* If still not matched, give up */
759 set_tvnorm(dev, &tvnorms[i]); /* do the actual setting */
763 static int tw68_g_std(struct file *file, void *priv, v4l2_std_id *id)
765 struct tw68_dev *dev = video_drvdata(file);
767 *id = dev->tvnorm->id;
771 static int tw68_enum_fmt_vid_cap(struct file *file, void *priv,
772 struct v4l2_fmtdesc *f)
774 if (f->index >= FORMATS)
777 strscpy(f->description, formats[f->index].name,
778 sizeof(f->description));
780 f->pixelformat = formats[f->index].fourcc;
786 * Used strictly for internal development and debugging, this routine
787 * prints out the current register contents for the tw68xx device.
789 static void tw68_dump_regs(struct tw68_dev *dev)
791 unsigned char line[80];
795 pr_info("Full dump of TW68 registers:\n");
796 /* First we do the PCI regs, 8 4-byte regs per line */
797 for (i = 0; i < 0x100; i += 32) {
799 cptr += sprintf(cptr, "%03x ", i);
800 /* j steps through the next 4 words */
801 for (j = i; j < i + 16; j += 4)
802 cptr += sprintf(cptr, "%08x ", tw_readl(j));
804 for (; j < i + 32; j += 4)
805 cptr += sprintf(cptr, "%08x ", tw_readl(j));
810 /* Next the control regs, which are single-byte, address mod 4 */
813 cptr += sprintf(cptr, "%03x ", i);
814 /* Print out 4 groups of 4 bytes */
815 for (j = 0; j < 4; j++) {
816 for (k = 0; k < 4; k++) {
817 cptr += sprintf(cptr, "%02x ",
829 static int vidioc_log_status(struct file *file, void *priv)
831 struct tw68_dev *dev = video_drvdata(file);
834 return v4l2_ctrl_log_status(file, priv);
837 #ifdef CONFIG_VIDEO_ADV_DEBUG
838 static int vidioc_g_register(struct file *file, void *priv,
839 struct v4l2_dbg_register *reg)
841 struct tw68_dev *dev = video_drvdata(file);
844 reg->val = tw_readb(reg->reg);
846 reg->val = tw_readl(reg->reg);
850 static int vidioc_s_register(struct file *file, void *priv,
851 const struct v4l2_dbg_register *reg)
853 struct tw68_dev *dev = video_drvdata(file);
856 tw_writeb(reg->reg, reg->val);
858 tw_writel(reg->reg & 0xffff, reg->val);
863 static const struct v4l2_ctrl_ops tw68_ctrl_ops = {
864 .s_ctrl = tw68_s_ctrl,
867 static const struct v4l2_file_operations video_fops = {
868 .owner = THIS_MODULE,
869 .open = v4l2_fh_open,
870 .release = vb2_fop_release,
871 .read = vb2_fop_read,
872 .poll = vb2_fop_poll,
873 .mmap = vb2_fop_mmap,
874 .unlocked_ioctl = video_ioctl2,
877 static const struct v4l2_ioctl_ops video_ioctl_ops = {
878 .vidioc_querycap = tw68_querycap,
879 .vidioc_enum_fmt_vid_cap = tw68_enum_fmt_vid_cap,
880 .vidioc_reqbufs = vb2_ioctl_reqbufs,
881 .vidioc_create_bufs = vb2_ioctl_create_bufs,
882 .vidioc_querybuf = vb2_ioctl_querybuf,
883 .vidioc_qbuf = vb2_ioctl_qbuf,
884 .vidioc_dqbuf = vb2_ioctl_dqbuf,
885 .vidioc_s_std = tw68_s_std,
886 .vidioc_g_std = tw68_g_std,
887 .vidioc_enum_input = tw68_enum_input,
888 .vidioc_g_input = tw68_g_input,
889 .vidioc_s_input = tw68_s_input,
890 .vidioc_streamon = vb2_ioctl_streamon,
891 .vidioc_streamoff = vb2_ioctl_streamoff,
892 .vidioc_g_fmt_vid_cap = tw68_g_fmt_vid_cap,
893 .vidioc_try_fmt_vid_cap = tw68_try_fmt_vid_cap,
894 .vidioc_s_fmt_vid_cap = tw68_s_fmt_vid_cap,
895 .vidioc_log_status = vidioc_log_status,
896 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
897 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
898 #ifdef CONFIG_VIDEO_ADV_DEBUG
899 .vidioc_g_register = vidioc_g_register,
900 .vidioc_s_register = vidioc_s_register,
904 static const struct video_device tw68_video_template = {
905 .name = "tw68_video",
907 .ioctl_ops = &video_ioctl_ops,
908 .release = video_device_release_empty,
909 .tvnorms = TW68_NORMS,
910 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
914 /* ------------------------------------------------------------------ */
916 void tw68_set_tvnorm_hw(struct tw68_dev *dev)
918 tw_andorb(TW68_SDT, 0x07, dev->tvnorm->format);
921 int tw68_video_init1(struct tw68_dev *dev)
923 struct v4l2_ctrl_handler *hdl = &dev->hdl;
925 v4l2_ctrl_handler_init(hdl, 6);
926 v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
927 V4L2_CID_BRIGHTNESS, -128, 127, 1, 20);
928 v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
929 V4L2_CID_CONTRAST, 0, 255, 1, 100);
930 v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
931 V4L2_CID_SATURATION, 0, 255, 1, 128);
933 v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
934 V4L2_CID_HUE, -128, 127, 1, 0);
935 v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
936 V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
937 v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
938 V4L2_CID_CHROMA_AGC, 0, 1, 1, 1);
940 v4l2_ctrl_handler_free(hdl);
943 dev->v4l2_dev.ctrl_handler = hdl;
944 v4l2_ctrl_handler_setup(hdl);
948 int tw68_video_init2(struct tw68_dev *dev, int video_nr)
952 set_tvnorm(dev, &tvnorms[0]);
954 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
957 dev->field = V4L2_FIELD_INTERLACED;
959 INIT_LIST_HEAD(&dev->active);
960 dev->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
961 dev->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
962 dev->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ | VB2_DMABUF;
963 dev->vidq.ops = &tw68_video_qops;
964 dev->vidq.mem_ops = &vb2_dma_sg_memops;
965 dev->vidq.drv_priv = dev;
966 dev->vidq.gfp_flags = __GFP_DMA32 | __GFP_KSWAPD_RECLAIM;
967 dev->vidq.buf_struct_size = sizeof(struct tw68_buf);
968 dev->vidq.lock = &dev->lock;
969 dev->vidq.min_buffers_needed = 2;
970 dev->vidq.dev = &dev->pci->dev;
971 ret = vb2_queue_init(&dev->vidq);
974 dev->vdev = tw68_video_template;
975 dev->vdev.v4l2_dev = &dev->v4l2_dev;
976 dev->vdev.lock = &dev->lock;
977 dev->vdev.queue = &dev->vidq;
978 video_set_drvdata(&dev->vdev, dev);
979 return video_register_device(&dev->vdev, VFL_TYPE_GRABBER, video_nr);
983 * tw68_irq_video_done
985 void tw68_irq_video_done(struct tw68_dev *dev, unsigned long status)
989 /* reset interrupts handled by this routine */
990 tw_writel(TW68_INTSTAT, status);
992 * Check most likely first
994 * DMAPI shows we have reached the end of the risc code
995 * for the current buffer.
997 if (status & TW68_DMAPI) {
998 struct tw68_buf *buf;
1000 spin_lock(&dev->slock);
1001 buf = list_entry(dev->active.next, struct tw68_buf, list);
1002 list_del(&buf->list);
1003 spin_unlock(&dev->slock);
1004 buf->vb.vb2_buf.timestamp = ktime_get_ns();
1005 buf->vb.field = dev->field;
1006 buf->vb.sequence = dev->seqnr++;
1007 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
1008 status &= ~(TW68_DMAPI);
1012 if (status & (TW68_VLOCK | TW68_HLOCK))
1013 dev_dbg(&dev->pci->dev, "Lost sync\n");
1014 if (status & TW68_PABORT)
1015 dev_err(&dev->pci->dev, "PABORT interrupt\n");
1016 if (status & TW68_DMAPERR)
1017 dev_err(&dev->pci->dev, "DMAPERR interrupt\n");
1019 * On TW6800, FDMIS is apparently generated if video input is switched
1020 * during operation. Therefore, it is not enabled for that chip.
1022 if (status & TW68_FDMIS)
1023 dev_dbg(&dev->pci->dev, "FDMIS interrupt\n");
1024 if (status & TW68_FFOF) {
1025 /* probably a logic error */
1026 reg = tw_readl(TW68_DMAC) & TW68_FIFO_EN;
1027 tw_clearl(TW68_DMAC, TW68_FIFO_EN);
1028 dev_dbg(&dev->pci->dev, "FFOF interrupt\n");
1029 tw_setl(TW68_DMAC, reg);
1031 if (status & TW68_FFERR)
1032 dev_dbg(&dev->pci->dev, "FFERR interrupt\n");