162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  Driver for the Conexant CX23885 PCIe bridge
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include "cx23885.h"
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/kernel.h>
1162306a36Sopenharmony_ci#include <linux/module.h>
1262306a36Sopenharmony_ci#include <linux/moduleparam.h>
1362306a36Sopenharmony_ci#include <linux/init.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_cistatic unsigned int vbibufs = 4;
1662306a36Sopenharmony_cimodule_param(vbibufs, int, 0644);
1762306a36Sopenharmony_ciMODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32");
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistatic unsigned int vbi_debug;
2062306a36Sopenharmony_cimodule_param(vbi_debug, int, 0644);
2162306a36Sopenharmony_ciMODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define dprintk(level, fmt, arg...)\
2462306a36Sopenharmony_ci	do { if (vbi_debug >= level)\
2562306a36Sopenharmony_ci		printk(KERN_DEBUG pr_fmt("%s: vbi:" fmt), \
2662306a36Sopenharmony_ci			__func__, ##arg); \
2762306a36Sopenharmony_ci	} while (0)
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/* ------------------------------------------------------------------ */
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#define VBI_LINE_LENGTH 1440
3262306a36Sopenharmony_ci#define VBI_NTSC_LINE_COUNT 12
3362306a36Sopenharmony_ci#define VBI_PAL_LINE_COUNT 18
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciint cx23885_vbi_fmt(struct file *file, void *priv,
3762306a36Sopenharmony_ci	struct v4l2_format *f)
3862306a36Sopenharmony_ci{
3962306a36Sopenharmony_ci	struct cx23885_dev *dev = video_drvdata(file);
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	f->fmt.vbi.sampling_rate = 27000000;
4262306a36Sopenharmony_ci	f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
4362306a36Sopenharmony_ci	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
4462306a36Sopenharmony_ci	f->fmt.vbi.offset = 0;
4562306a36Sopenharmony_ci	f->fmt.vbi.flags = 0;
4662306a36Sopenharmony_ci	if (dev->tvnorm & V4L2_STD_525_60) {
4762306a36Sopenharmony_ci		/* ntsc */
4862306a36Sopenharmony_ci		f->fmt.vbi.start[0] = V4L2_VBI_ITU_525_F1_START + 9;
4962306a36Sopenharmony_ci		f->fmt.vbi.start[1] = V4L2_VBI_ITU_525_F2_START + 9;
5062306a36Sopenharmony_ci		f->fmt.vbi.count[0] = VBI_NTSC_LINE_COUNT;
5162306a36Sopenharmony_ci		f->fmt.vbi.count[1] = VBI_NTSC_LINE_COUNT;
5262306a36Sopenharmony_ci	} else if (dev->tvnorm & V4L2_STD_625_50) {
5362306a36Sopenharmony_ci		/* pal */
5462306a36Sopenharmony_ci		f->fmt.vbi.start[0] = V4L2_VBI_ITU_625_F1_START + 5;
5562306a36Sopenharmony_ci		f->fmt.vbi.start[1] = V4L2_VBI_ITU_625_F2_START + 5;
5662306a36Sopenharmony_ci		f->fmt.vbi.count[0] = VBI_PAL_LINE_COUNT;
5762306a36Sopenharmony_ci		f->fmt.vbi.count[1] = VBI_PAL_LINE_COUNT;
5862306a36Sopenharmony_ci	}
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	return 0;
6162306a36Sopenharmony_ci}
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/* We're given the Video Interrupt status register.
6462306a36Sopenharmony_ci * The cx23885_video_irq() func has already validated
6562306a36Sopenharmony_ci * the potential error bits, we just need to
6662306a36Sopenharmony_ci * deal with vbi payload and return indication if
6762306a36Sopenharmony_ci * we actually processed any payload.
6862306a36Sopenharmony_ci */
6962306a36Sopenharmony_ciint cx23885_vbi_irq(struct cx23885_dev *dev, u32 status)
7062306a36Sopenharmony_ci{
7162306a36Sopenharmony_ci	u32 count;
7262306a36Sopenharmony_ci	int handled = 0;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	if (status & VID_BC_MSK_VBI_RISCI1) {
7562306a36Sopenharmony_ci		dprintk(1, "%s() VID_BC_MSK_VBI_RISCI1\n", __func__);
7662306a36Sopenharmony_ci		spin_lock(&dev->slock);
7762306a36Sopenharmony_ci		count = cx_read(VBI_A_GPCNT);
7862306a36Sopenharmony_ci		cx23885_video_wakeup(dev, &dev->vbiq, count);
7962306a36Sopenharmony_ci		spin_unlock(&dev->slock);
8062306a36Sopenharmony_ci		handled++;
8162306a36Sopenharmony_ci	}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	return handled;
8462306a36Sopenharmony_ci}
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_cistatic int cx23885_start_vbi_dma(struct cx23885_dev    *dev,
8762306a36Sopenharmony_ci			 struct cx23885_dmaqueue *q,
8862306a36Sopenharmony_ci			 struct cx23885_buffer   *buf)
8962306a36Sopenharmony_ci{
9062306a36Sopenharmony_ci	dprintk(1, "%s()\n", __func__);
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	/* setup fifo + format */
9362306a36Sopenharmony_ci	cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02],
9462306a36Sopenharmony_ci				VBI_LINE_LENGTH, buf->risc.dma);
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	/* reset counter */
9762306a36Sopenharmony_ci	cx_write(VID_A_VBI_CTRL, 3);
9862306a36Sopenharmony_ci	cx_write(VBI_A_GPCNT_CTL, 3);
9962306a36Sopenharmony_ci	q->count = 0;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	/* enable irq */
10262306a36Sopenharmony_ci	cx23885_irq_add_enable(dev, 0x01);
10362306a36Sopenharmony_ci	cx_set(VID_A_INT_MSK, 0x000022);
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	/* start dma */
10662306a36Sopenharmony_ci	cx_set(DEV_CNTRL2, (1<<5));
10762306a36Sopenharmony_ci	cx_set(VID_A_DMA_CTL, 0x22); /* FIFO and RISC enable */
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	return 0;
11062306a36Sopenharmony_ci}
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci/* ------------------------------------------------------------------ */
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_cistatic int queue_setup(struct vb2_queue *q,
11562306a36Sopenharmony_ci			   unsigned int *num_buffers, unsigned int *num_planes,
11662306a36Sopenharmony_ci			   unsigned int sizes[], struct device *alloc_devs[])
11762306a36Sopenharmony_ci{
11862306a36Sopenharmony_ci	struct cx23885_dev *dev = q->drv_priv;
11962306a36Sopenharmony_ci	unsigned lines = VBI_PAL_LINE_COUNT;
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	if (dev->tvnorm & V4L2_STD_525_60)
12262306a36Sopenharmony_ci		lines = VBI_NTSC_LINE_COUNT;
12362306a36Sopenharmony_ci	*num_planes = 1;
12462306a36Sopenharmony_ci	sizes[0] = lines * VBI_LINE_LENGTH * 2;
12562306a36Sopenharmony_ci	return 0;
12662306a36Sopenharmony_ci}
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_cistatic int buffer_prepare(struct vb2_buffer *vb)
12962306a36Sopenharmony_ci{
13062306a36Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
13162306a36Sopenharmony_ci	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
13262306a36Sopenharmony_ci	struct cx23885_buffer *buf = container_of(vbuf,
13362306a36Sopenharmony_ci		struct cx23885_buffer, vb);
13462306a36Sopenharmony_ci	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
13562306a36Sopenharmony_ci	unsigned lines = VBI_PAL_LINE_COUNT;
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	if (dev->tvnorm & V4L2_STD_525_60)
13862306a36Sopenharmony_ci		lines = VBI_NTSC_LINE_COUNT;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	if (vb2_plane_size(vb, 0) < lines * VBI_LINE_LENGTH * 2)
14162306a36Sopenharmony_ci		return -EINVAL;
14262306a36Sopenharmony_ci	vb2_set_plane_payload(vb, 0, lines * VBI_LINE_LENGTH * 2);
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	cx23885_risc_vbibuffer(dev->pci, &buf->risc,
14562306a36Sopenharmony_ci			 sgt->sgl,
14662306a36Sopenharmony_ci			 0, VBI_LINE_LENGTH * lines,
14762306a36Sopenharmony_ci			 VBI_LINE_LENGTH, 0,
14862306a36Sopenharmony_ci			 lines);
14962306a36Sopenharmony_ci	return 0;
15062306a36Sopenharmony_ci}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_cistatic void buffer_finish(struct vb2_buffer *vb)
15362306a36Sopenharmony_ci{
15462306a36Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
15562306a36Sopenharmony_ci	struct cx23885_buffer *buf = container_of(vbuf,
15662306a36Sopenharmony_ci		struct cx23885_buffer, vb);
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci/*
16262306a36Sopenharmony_ci * The risc program for each buffer works as follows: it starts with a simple
16362306a36Sopenharmony_ci * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
16462306a36Sopenharmony_ci * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
16562306a36Sopenharmony_ci * the initial JUMP).
16662306a36Sopenharmony_ci *
16762306a36Sopenharmony_ci * This is the risc program of the first buffer to be queued if the active list
16862306a36Sopenharmony_ci * is empty and it just keeps DMAing this buffer without generating any
16962306a36Sopenharmony_ci * interrupts.
17062306a36Sopenharmony_ci *
17162306a36Sopenharmony_ci * If a new buffer is added then the initial JUMP in the code for that buffer
17262306a36Sopenharmony_ci * will generate an interrupt which signals that the previous buffer has been
17362306a36Sopenharmony_ci * DMAed successfully and that it can be returned to userspace.
17462306a36Sopenharmony_ci *
17562306a36Sopenharmony_ci * It also sets the final jump of the previous buffer to the start of the new
17662306a36Sopenharmony_ci * buffer, thus chaining the new buffer into the DMA chain. This is a single
17762306a36Sopenharmony_ci * atomic u32 write, so there is no race condition.
17862306a36Sopenharmony_ci *
17962306a36Sopenharmony_ci * The end-result of all this that you only get an interrupt when a buffer
18062306a36Sopenharmony_ci * is ready, so the control flow is very easy.
18162306a36Sopenharmony_ci */
18262306a36Sopenharmony_cistatic void buffer_queue(struct vb2_buffer *vb)
18362306a36Sopenharmony_ci{
18462306a36Sopenharmony_ci	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
18562306a36Sopenharmony_ci	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
18662306a36Sopenharmony_ci	struct cx23885_buffer *buf = container_of(vbuf,
18762306a36Sopenharmony_ci			struct cx23885_buffer, vb);
18862306a36Sopenharmony_ci	struct cx23885_buffer *prev;
18962306a36Sopenharmony_ci	struct cx23885_dmaqueue *q = &dev->vbiq;
19062306a36Sopenharmony_ci	unsigned long flags;
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci	buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
19362306a36Sopenharmony_ci	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
19462306a36Sopenharmony_ci	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
19562306a36Sopenharmony_ci	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci	if (list_empty(&q->active)) {
19862306a36Sopenharmony_ci		spin_lock_irqsave(&dev->slock, flags);
19962306a36Sopenharmony_ci		list_add_tail(&buf->queue, &q->active);
20062306a36Sopenharmony_ci		spin_unlock_irqrestore(&dev->slock, flags);
20162306a36Sopenharmony_ci		dprintk(2, "[%p/%d] vbi_queue - first active\n",
20262306a36Sopenharmony_ci			buf, buf->vb.vb2_buf.index);
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	} else {
20562306a36Sopenharmony_ci		buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
20662306a36Sopenharmony_ci		prev = list_entry(q->active.prev, struct cx23885_buffer,
20762306a36Sopenharmony_ci			queue);
20862306a36Sopenharmony_ci		spin_lock_irqsave(&dev->slock, flags);
20962306a36Sopenharmony_ci		list_add_tail(&buf->queue, &q->active);
21062306a36Sopenharmony_ci		spin_unlock_irqrestore(&dev->slock, flags);
21162306a36Sopenharmony_ci		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
21262306a36Sopenharmony_ci		dprintk(2, "[%p/%d] buffer_queue - append to active\n",
21362306a36Sopenharmony_ci			buf, buf->vb.vb2_buf.index);
21462306a36Sopenharmony_ci	}
21562306a36Sopenharmony_ci}
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_cistatic int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
21862306a36Sopenharmony_ci{
21962306a36Sopenharmony_ci	struct cx23885_dev *dev = q->drv_priv;
22062306a36Sopenharmony_ci	struct cx23885_dmaqueue *dmaq = &dev->vbiq;
22162306a36Sopenharmony_ci	struct cx23885_buffer *buf = list_entry(dmaq->active.next,
22262306a36Sopenharmony_ci			struct cx23885_buffer, queue);
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	cx23885_start_vbi_dma(dev, dmaq, buf);
22562306a36Sopenharmony_ci	return 0;
22662306a36Sopenharmony_ci}
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_cistatic void cx23885_stop_streaming(struct vb2_queue *q)
22962306a36Sopenharmony_ci{
23062306a36Sopenharmony_ci	struct cx23885_dev *dev = q->drv_priv;
23162306a36Sopenharmony_ci	struct cx23885_dmaqueue *dmaq = &dev->vbiq;
23262306a36Sopenharmony_ci	unsigned long flags;
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	cx_clear(VID_A_DMA_CTL, 0x22); /* FIFO and RISC enable */
23562306a36Sopenharmony_ci	spin_lock_irqsave(&dev->slock, flags);
23662306a36Sopenharmony_ci	while (!list_empty(&dmaq->active)) {
23762306a36Sopenharmony_ci		struct cx23885_buffer *buf = list_entry(dmaq->active.next,
23862306a36Sopenharmony_ci			struct cx23885_buffer, queue);
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci		list_del(&buf->queue);
24162306a36Sopenharmony_ci		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
24262306a36Sopenharmony_ci	}
24362306a36Sopenharmony_ci	spin_unlock_irqrestore(&dev->slock, flags);
24462306a36Sopenharmony_ci}
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ciconst struct vb2_ops cx23885_vbi_qops = {
24862306a36Sopenharmony_ci	.queue_setup    = queue_setup,
24962306a36Sopenharmony_ci	.buf_prepare  = buffer_prepare,
25062306a36Sopenharmony_ci	.buf_finish = buffer_finish,
25162306a36Sopenharmony_ci	.buf_queue    = buffer_queue,
25262306a36Sopenharmony_ci	.wait_prepare = vb2_ops_wait_prepare,
25362306a36Sopenharmony_ci	.wait_finish = vb2_ops_wait_finish,
25462306a36Sopenharmony_ci	.start_streaming = cx23885_start_streaming,
25562306a36Sopenharmony_ci	.stop_streaming = cx23885_stop_streaming,
25662306a36Sopenharmony_ci};
257